blob: c9900d1cd5c2107295d88791ede6e419b784c02e [file] [log] [blame]
Paul Cercueil06a334a2019-02-07 10:31:40 -03001// SPDX-License-Identifier: GPL-2.0
2//
3// JZ4740 CODEC driver
4//
5// Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +02006
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/platform_device.h>
10#include <linux/slab.h>
Axel Lin68bfcaf2011-12-06 15:19:00 +080011#include <linux/io.h>
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +020012#include <linux/regmap.h>
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020013
14#include <linux/delay.h>
15
16#include <sound/core.h>
17#include <sound/pcm.h>
18#include <sound/pcm_params.h>
19#include <sound/initval.h>
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020020#include <sound/soc.h>
Lars-Peter Clausena484a9a2012-10-27 17:15:06 +020021#include <sound/tlv.h>
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020022
23#define JZ4740_REG_CODEC_1 0x0
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +020024#define JZ4740_REG_CODEC_2 0x4
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020025
26#define JZ4740_CODEC_1_LINE_ENABLE BIT(29)
27#define JZ4740_CODEC_1_MIC_ENABLE BIT(28)
28#define JZ4740_CODEC_1_SW1_ENABLE BIT(27)
29#define JZ4740_CODEC_1_ADC_ENABLE BIT(26)
30#define JZ4740_CODEC_1_SW2_ENABLE BIT(25)
31#define JZ4740_CODEC_1_DAC_ENABLE BIT(24)
32#define JZ4740_CODEC_1_VREF_DISABLE BIT(20)
33#define JZ4740_CODEC_1_VREF_AMP_DISABLE BIT(19)
34#define JZ4740_CODEC_1_VREF_PULLDOWN BIT(18)
35#define JZ4740_CODEC_1_VREF_LOW_CURRENT BIT(17)
36#define JZ4740_CODEC_1_VREF_HIGH_CURRENT BIT(16)
37#define JZ4740_CODEC_1_HEADPHONE_DISABLE BIT(14)
38#define JZ4740_CODEC_1_HEADPHONE_AMP_CHANGE_ANY BIT(13)
39#define JZ4740_CODEC_1_HEADPHONE_CHARGE BIT(12)
40#define JZ4740_CODEC_1_HEADPHONE_PULLDOWN (BIT(11) | BIT(10))
41#define JZ4740_CODEC_1_HEADPHONE_POWERDOWN_M BIT(9)
42#define JZ4740_CODEC_1_HEADPHONE_POWERDOWN BIT(8)
43#define JZ4740_CODEC_1_SUSPEND BIT(1)
44#define JZ4740_CODEC_1_RESET BIT(0)
45
46#define JZ4740_CODEC_1_LINE_ENABLE_OFFSET 29
47#define JZ4740_CODEC_1_MIC_ENABLE_OFFSET 28
48#define JZ4740_CODEC_1_SW1_ENABLE_OFFSET 27
49#define JZ4740_CODEC_1_ADC_ENABLE_OFFSET 26
50#define JZ4740_CODEC_1_SW2_ENABLE_OFFSET 25
51#define JZ4740_CODEC_1_DAC_ENABLE_OFFSET 24
52#define JZ4740_CODEC_1_HEADPHONE_DISABLE_OFFSET 14
53#define JZ4740_CODEC_1_HEADPHONE_POWERDOWN_OFFSET 8
54
55#define JZ4740_CODEC_2_INPUT_VOLUME_MASK 0x1f0000
56#define JZ4740_CODEC_2_SAMPLE_RATE_MASK 0x000f00
57#define JZ4740_CODEC_2_MIC_BOOST_GAIN_MASK 0x000030
58#define JZ4740_CODEC_2_HEADPHONE_VOLUME_MASK 0x000003
59
60#define JZ4740_CODEC_2_INPUT_VOLUME_OFFSET 16
61#define JZ4740_CODEC_2_SAMPLE_RATE_OFFSET 8
62#define JZ4740_CODEC_2_MIC_BOOST_GAIN_OFFSET 4
63#define JZ4740_CODEC_2_HEADPHONE_VOLUME_OFFSET 0
64
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +020065static const struct reg_default jz4740_codec_reg_defaults[] = {
66 { JZ4740_REG_CODEC_1, 0x021b2302 },
67 { JZ4740_REG_CODEC_2, 0x00170803 },
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020068};
69
70struct jz4740_codec {
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +020071 struct regmap *regmap;
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020072};
73
Lars-Peter Clausen4689ac52015-08-02 17:19:40 +020074static const DECLARE_TLV_DB_RANGE(jz4740_mic_tlv,
Lars-Peter Clausena484a9a2012-10-27 17:15:06 +020075 0, 2, TLV_DB_SCALE_ITEM(0, 600, 0),
Lars-Peter Clausen4689ac52015-08-02 17:19:40 +020076 3, 3, TLV_DB_SCALE_ITEM(2000, 0, 0)
77);
Lars-Peter Clausena484a9a2012-10-27 17:15:06 +020078
79static const DECLARE_TLV_DB_SCALE(jz4740_out_tlv, 0, 200, 0);
80static const DECLARE_TLV_DB_SCALE(jz4740_in_tlv, -3450, 150, 0);
81
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020082static const struct snd_kcontrol_new jz4740_codec_controls[] = {
Lars-Peter Clausena484a9a2012-10-27 17:15:06 +020083 SOC_SINGLE_TLV("Master Playback Volume", JZ4740_REG_CODEC_2,
84 JZ4740_CODEC_2_HEADPHONE_VOLUME_OFFSET, 3, 0,
85 jz4740_out_tlv),
86 SOC_SINGLE_TLV("Master Capture Volume", JZ4740_REG_CODEC_2,
87 JZ4740_CODEC_2_INPUT_VOLUME_OFFSET, 31, 0,
88 jz4740_in_tlv),
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020089 SOC_SINGLE("Master Playback Switch", JZ4740_REG_CODEC_1,
90 JZ4740_CODEC_1_HEADPHONE_DISABLE_OFFSET, 1, 1),
Lars-Peter Clausena484a9a2012-10-27 17:15:06 +020091 SOC_SINGLE_TLV("Mic Capture Volume", JZ4740_REG_CODEC_2,
92 JZ4740_CODEC_2_MIC_BOOST_GAIN_OFFSET, 3, 0,
93 jz4740_mic_tlv),
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +020094};
95
96static const struct snd_kcontrol_new jz4740_codec_output_controls[] = {
97 SOC_DAPM_SINGLE("Bypass Switch", JZ4740_REG_CODEC_1,
98 JZ4740_CODEC_1_SW1_ENABLE_OFFSET, 1, 0),
99 SOC_DAPM_SINGLE("DAC Switch", JZ4740_REG_CODEC_1,
100 JZ4740_CODEC_1_SW2_ENABLE_OFFSET, 1, 0),
101};
102
103static const struct snd_kcontrol_new jz4740_codec_input_controls[] = {
104 SOC_DAPM_SINGLE("Line Capture Switch", JZ4740_REG_CODEC_1,
105 JZ4740_CODEC_1_LINE_ENABLE_OFFSET, 1, 0),
106 SOC_DAPM_SINGLE("Mic Capture Switch", JZ4740_REG_CODEC_1,
107 JZ4740_CODEC_1_MIC_ENABLE_OFFSET, 1, 0),
108};
109
110static const struct snd_soc_dapm_widget jz4740_codec_dapm_widgets[] = {
111 SND_SOC_DAPM_ADC("ADC", "Capture", JZ4740_REG_CODEC_1,
112 JZ4740_CODEC_1_ADC_ENABLE_OFFSET, 0),
113 SND_SOC_DAPM_DAC("DAC", "Playback", JZ4740_REG_CODEC_1,
114 JZ4740_CODEC_1_DAC_ENABLE_OFFSET, 0),
115
116 SND_SOC_DAPM_MIXER("Output Mixer", JZ4740_REG_CODEC_1,
117 JZ4740_CODEC_1_HEADPHONE_POWERDOWN_OFFSET, 1,
118 jz4740_codec_output_controls,
119 ARRAY_SIZE(jz4740_codec_output_controls)),
120
121 SND_SOC_DAPM_MIXER_NAMED_CTL("Input Mixer", SND_SOC_NOPM, 0, 0,
122 jz4740_codec_input_controls,
123 ARRAY_SIZE(jz4740_codec_input_controls)),
124 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
125
126 SND_SOC_DAPM_OUTPUT("LOUT"),
127 SND_SOC_DAPM_OUTPUT("ROUT"),
128
129 SND_SOC_DAPM_INPUT("MIC"),
130 SND_SOC_DAPM_INPUT("LIN"),
131 SND_SOC_DAPM_INPUT("RIN"),
132};
133
134static const struct snd_soc_dapm_route jz4740_codec_dapm_routes[] = {
135 {"Line Input", NULL, "LIN"},
136 {"Line Input", NULL, "RIN"},
137
138 {"Input Mixer", "Line Capture Switch", "Line Input"},
139 {"Input Mixer", "Mic Capture Switch", "MIC"},
140
141 {"ADC", NULL, "Input Mixer"},
142
143 {"Output Mixer", "Bypass Switch", "Input Mixer"},
144 {"Output Mixer", "DAC Switch", "DAC"},
145
146 {"LOUT", NULL, "Output Mixer"},
147 {"ROUT", NULL, "Output Mixer"},
148};
149
150static int jz4740_codec_hw_params(struct snd_pcm_substream *substream,
151 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
152{
Kuninori Morimotob48e6ef2018-01-29 04:43:36 +0000153 struct jz4740_codec *jz4740_codec = snd_soc_component_get_drvdata(dai->component);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200154 uint32_t val;
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200155
156 switch (params_rate(params)) {
157 case 8000:
158 val = 0;
159 break;
160 case 11025:
161 val = 1;
162 break;
163 case 12000:
164 val = 2;
165 break;
166 case 16000:
167 val = 3;
168 break;
169 case 22050:
170 val = 4;
171 break;
172 case 24000:
173 val = 5;
174 break;
175 case 32000:
176 val = 6;
177 break;
178 case 44100:
179 val = 7;
180 break;
181 case 48000:
182 val = 8;
183 break;
184 default:
185 return -EINVAL;
186 }
187
188 val <<= JZ4740_CODEC_2_SAMPLE_RATE_OFFSET;
189
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200190 regmap_update_bits(jz4740_codec->regmap, JZ4740_REG_CODEC_2,
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200191 JZ4740_CODEC_2_SAMPLE_RATE_MASK, val);
192
193 return 0;
194}
195
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100196static const struct snd_soc_dai_ops jz4740_codec_dai_ops = {
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200197 .hw_params = jz4740_codec_hw_params,
198};
199
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000200static struct snd_soc_dai_driver jz4740_codec_dai = {
201 .name = "jz4740-hifi",
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200202 .playback = {
203 .stream_name = "Playback",
204 .channels_min = 2,
205 .channels_max = 2,
206 .rates = SNDRV_PCM_RATE_8000_48000,
207 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
208 },
209 .capture = {
210 .stream_name = "Capture",
211 .channels_min = 2,
212 .channels_max = 2,
213 .rates = SNDRV_PCM_RATE_8000_48000,
214 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
215 },
216 .ops = &jz4740_codec_dai_ops,
217 .symmetric_rates = 1,
218};
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200219
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200220static void jz4740_codec_wakeup(struct regmap *regmap)
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200221{
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200222 regmap_update_bits(regmap, JZ4740_REG_CODEC_1,
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200223 JZ4740_CODEC_1_RESET, JZ4740_CODEC_1_RESET);
224 udelay(2);
225
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200226 regmap_update_bits(regmap, JZ4740_REG_CODEC_1,
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200227 JZ4740_CODEC_1_SUSPEND | JZ4740_CODEC_1_RESET, 0);
228
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200229 regcache_sync(regmap);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200230}
231
Kuninori Morimotob48e6ef2018-01-29 04:43:36 +0000232static int jz4740_codec_set_bias_level(struct snd_soc_component *component,
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200233 enum snd_soc_bias_level level)
234{
Kuninori Morimotob48e6ef2018-01-29 04:43:36 +0000235 struct jz4740_codec *jz4740_codec = snd_soc_component_get_drvdata(component);
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200236 struct regmap *regmap = jz4740_codec->regmap;
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200237 unsigned int mask;
238 unsigned int value;
239
240 switch (level) {
241 case SND_SOC_BIAS_ON:
242 break;
243 case SND_SOC_BIAS_PREPARE:
244 mask = JZ4740_CODEC_1_VREF_DISABLE |
245 JZ4740_CODEC_1_VREF_AMP_DISABLE |
246 JZ4740_CODEC_1_HEADPHONE_POWERDOWN_M;
247 value = 0;
248
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200249 regmap_update_bits(regmap, JZ4740_REG_CODEC_1, mask, value);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200250 break;
251 case SND_SOC_BIAS_STANDBY:
252 /* The only way to clear the suspend flag is to reset the codec */
Kuninori Morimotob48e6ef2018-01-29 04:43:36 +0000253 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200254 jz4740_codec_wakeup(regmap);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200255
256 mask = JZ4740_CODEC_1_VREF_DISABLE |
257 JZ4740_CODEC_1_VREF_AMP_DISABLE |
258 JZ4740_CODEC_1_HEADPHONE_POWERDOWN_M;
259 value = JZ4740_CODEC_1_VREF_DISABLE |
260 JZ4740_CODEC_1_VREF_AMP_DISABLE |
261 JZ4740_CODEC_1_HEADPHONE_POWERDOWN_M;
262
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200263 regmap_update_bits(regmap, JZ4740_REG_CODEC_1, mask, value);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200264 break;
265 case SND_SOC_BIAS_OFF:
266 mask = JZ4740_CODEC_1_SUSPEND;
267 value = JZ4740_CODEC_1_SUSPEND;
268
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200269 regmap_update_bits(regmap, JZ4740_REG_CODEC_1, mask, value);
270 regcache_mark_dirty(regmap);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200271 break;
272 default:
273 break;
274 }
275
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200276 return 0;
277}
278
Kuninori Morimotob48e6ef2018-01-29 04:43:36 +0000279static int jz4740_codec_dev_probe(struct snd_soc_component *component)
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200280{
Kuninori Morimotob48e6ef2018-01-29 04:43:36 +0000281 struct jz4740_codec *jz4740_codec = snd_soc_component_get_drvdata(component);
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200282
283 regmap_update_bits(jz4740_codec->regmap, JZ4740_REG_CODEC_1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000284 JZ4740_CODEC_1_SW2_ENABLE, JZ4740_CODEC_1_SW2_ENABLE);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200285
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200286 return 0;
287}
288
Kuninori Morimotob48e6ef2018-01-29 04:43:36 +0000289static const struct snd_soc_component_driver soc_codec_dev_jz4740_codec = {
290 .probe = jz4740_codec_dev_probe,
291 .set_bias_level = jz4740_codec_set_bias_level,
292 .controls = jz4740_codec_controls,
293 .num_controls = ARRAY_SIZE(jz4740_codec_controls),
294 .dapm_widgets = jz4740_codec_dapm_widgets,
295 .num_dapm_widgets = ARRAY_SIZE(jz4740_codec_dapm_widgets),
296 .dapm_routes = jz4740_codec_dapm_routes,
297 .num_dapm_routes = ARRAY_SIZE(jz4740_codec_dapm_routes),
298 .suspend_bias_off = 1,
299 .idle_bias_on = 1,
300 .use_pmdown_time = 1,
301 .endianness = 1,
302 .non_legacy_dai_naming = 1,
Lars-Peter Clausen67447912011-04-12 19:33:29 +0200303
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200304};
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200305
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200306static const struct regmap_config jz4740_codec_regmap_config = {
307 .reg_bits = 32,
308 .reg_stride = 4,
309 .val_bits = 32,
310 .max_register = JZ4740_REG_CODEC_2,
311
312 .reg_defaults = jz4740_codec_reg_defaults,
313 .num_reg_defaults = ARRAY_SIZE(jz4740_codec_reg_defaults),
314 .cache_type = REGCACHE_RBTREE,
315};
316
Bill Pemberton7a79e942012-12-07 09:26:37 -0500317static int jz4740_codec_probe(struct platform_device *pdev)
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200318{
319 int ret;
320 struct jz4740_codec *jz4740_codec;
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200321 void __iomem *base;
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200322
Axel Lin558460c2011-12-26 20:55:01 +0800323 jz4740_codec = devm_kzalloc(&pdev->dev, sizeof(*jz4740_codec),
324 GFP_KERNEL);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200325 if (!jz4740_codec)
326 return -ENOMEM;
327
YueHaibinga8dc1062019-07-27 23:07:23 +0800328 base = devm_platform_ioremap_resource(pdev, 0);
Thierry Redingb25b5aa2013-01-21 11:09:26 +0100329 if (IS_ERR(base))
330 return PTR_ERR(base);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200331
Lars-Peter Clausendd1b18a2012-10-27 17:15:08 +0200332 jz4740_codec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
333 &jz4740_codec_regmap_config);
334 if (IS_ERR(jz4740_codec->regmap))
335 return PTR_ERR(jz4740_codec->regmap);
336
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200337 platform_set_drvdata(pdev, jz4740_codec);
338
Kuninori Morimotob48e6ef2018-01-29 04:43:36 +0000339 ret = devm_snd_soc_register_component(&pdev->dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000340 &soc_codec_dev_jz4740_codec, &jz4740_codec_dai, 1);
Lars-Peter Clausen355d74b2012-10-27 17:15:07 +0200341 if (ret)
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200342 dev_err(&pdev->dev, "Failed to register codec\n");
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200343
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200344 return ret;
345}
346
Paul Cercueil030a79e2019-02-07 10:31:41 -0300347static const struct of_device_id jz4740_codec_of_matches[] = {
348 { .compatible = "ingenic,jz4740-codec", },
349 { }
350};
351MODULE_DEVICE_TABLE(of, jz4740_codec_of_matches);
Paul Cercueil030a79e2019-02-07 10:31:41 -0300352
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200353static struct platform_driver jz4740_codec_driver = {
354 .probe = jz4740_codec_probe,
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200355 .driver = {
356 .name = "jz4740-codec",
Paul Cercueile6825ba2020-05-23 14:54:55 +0200357 .of_match_table = jz4740_codec_of_matches,
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200358 },
359};
360
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000361module_platform_driver(jz4740_codec_driver);
Lars-Peter Clausen3b097d62010-06-22 00:46:31 +0200362
363MODULE_DESCRIPTION("JZ4740 SoC internal codec driver");
364MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
365MODULE_LICENSE("GPL v2");
366MODULE_ALIAS("platform:jz4740-codec");