1 // SPDX-License-Identifier: GPL-2.0
5 // Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
12 #include <linux/regmap.h>
14 #include <linux/delay.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/initval.h>
20 #include <sound/soc.h>
21 #include <sound/tlv.h>
23 #define JZ4740_REG_CODEC_1 0x0
24 #define JZ4740_REG_CODEC_2 0x4
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)
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
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
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
65 static const struct reg_default jz4740_codec_reg_defaults
[] = {
66 { JZ4740_REG_CODEC_1
, 0x021b2302 },
67 { JZ4740_REG_CODEC_2
, 0x00170803 },
71 struct regmap
*regmap
;
74 static const DECLARE_TLV_DB_RANGE(jz4740_mic_tlv
,
75 0, 2, TLV_DB_SCALE_ITEM(0, 600, 0),
76 3, 3, TLV_DB_SCALE_ITEM(2000, 0, 0)
79 static const DECLARE_TLV_DB_SCALE(jz4740_out_tlv
, 0, 200, 0);
80 static const DECLARE_TLV_DB_SCALE(jz4740_in_tlv
, -3450, 150, 0);
82 static const struct snd_kcontrol_new jz4740_codec_controls
[] = {
83 SOC_SINGLE_TLV("Master Playback Volume", JZ4740_REG_CODEC_2
,
84 JZ4740_CODEC_2_HEADPHONE_VOLUME_OFFSET
, 3, 0,
86 SOC_SINGLE_TLV("Master Capture Volume", JZ4740_REG_CODEC_2
,
87 JZ4740_CODEC_2_INPUT_VOLUME_OFFSET
, 31, 0,
89 SOC_SINGLE("Master Playback Switch", JZ4740_REG_CODEC_1
,
90 JZ4740_CODEC_1_HEADPHONE_DISABLE_OFFSET
, 1, 1),
91 SOC_SINGLE_TLV("Mic Capture Volume", JZ4740_REG_CODEC_2
,
92 JZ4740_CODEC_2_MIC_BOOST_GAIN_OFFSET
, 3, 0,
96 static 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),
103 static 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),
110 static 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),
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
)),
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),
126 SND_SOC_DAPM_OUTPUT("LOUT"),
127 SND_SOC_DAPM_OUTPUT("ROUT"),
129 SND_SOC_DAPM_INPUT("MIC"),
130 SND_SOC_DAPM_INPUT("LIN"),
131 SND_SOC_DAPM_INPUT("RIN"),
134 static const struct snd_soc_dapm_route jz4740_codec_dapm_routes
[] = {
135 {"Line Input", NULL
, "LIN"},
136 {"Line Input", NULL
, "RIN"},
138 {"Input Mixer", "Line Capture Switch", "Line Input"},
139 {"Input Mixer", "Mic Capture Switch", "MIC"},
141 {"ADC", NULL
, "Input Mixer"},
143 {"Output Mixer", "Bypass Switch", "Input Mixer"},
144 {"Output Mixer", "DAC Switch", "DAC"},
146 {"LOUT", NULL
, "Output Mixer"},
147 {"ROUT", NULL
, "Output Mixer"},
150 static int jz4740_codec_hw_params(struct snd_pcm_substream
*substream
,
151 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
153 struct jz4740_codec
*jz4740_codec
= snd_soc_component_get_drvdata(dai
->component
);
156 switch (params_rate(params
)) {
188 val
<<= JZ4740_CODEC_2_SAMPLE_RATE_OFFSET
;
190 regmap_update_bits(jz4740_codec
->regmap
, JZ4740_REG_CODEC_2
,
191 JZ4740_CODEC_2_SAMPLE_RATE_MASK
, val
);
196 static const struct snd_soc_dai_ops jz4740_codec_dai_ops
= {
197 .hw_params
= jz4740_codec_hw_params
,
200 static struct snd_soc_dai_driver jz4740_codec_dai
= {
201 .name
= "jz4740-hifi",
203 .stream_name
= "Playback",
206 .rates
= SNDRV_PCM_RATE_8000_48000
,
207 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S8
,
210 .stream_name
= "Capture",
213 .rates
= SNDRV_PCM_RATE_8000_48000
,
214 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S8
,
216 .ops
= &jz4740_codec_dai_ops
,
217 .symmetric_rates
= 1,
220 static void jz4740_codec_wakeup(struct regmap
*regmap
)
222 regmap_update_bits(regmap
, JZ4740_REG_CODEC_1
,
223 JZ4740_CODEC_1_RESET
, JZ4740_CODEC_1_RESET
);
226 regmap_update_bits(regmap
, JZ4740_REG_CODEC_1
,
227 JZ4740_CODEC_1_SUSPEND
| JZ4740_CODEC_1_RESET
, 0);
229 regcache_sync(regmap
);
232 static int jz4740_codec_set_bias_level(struct snd_soc_component
*component
,
233 enum snd_soc_bias_level level
)
235 struct jz4740_codec
*jz4740_codec
= snd_soc_component_get_drvdata(component
);
236 struct regmap
*regmap
= jz4740_codec
->regmap
;
241 case SND_SOC_BIAS_ON
:
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
;
249 regmap_update_bits(regmap
, JZ4740_REG_CODEC_1
, mask
, value
);
251 case SND_SOC_BIAS_STANDBY
:
252 /* The only way to clear the suspend flag is to reset the codec */
253 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
)
254 jz4740_codec_wakeup(regmap
);
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
;
263 regmap_update_bits(regmap
, JZ4740_REG_CODEC_1
, mask
, value
);
265 case SND_SOC_BIAS_OFF
:
266 mask
= JZ4740_CODEC_1_SUSPEND
;
267 value
= JZ4740_CODEC_1_SUSPEND
;
269 regmap_update_bits(regmap
, JZ4740_REG_CODEC_1
, mask
, value
);
270 regcache_mark_dirty(regmap
);
279 static int jz4740_codec_dev_probe(struct snd_soc_component
*component
)
281 struct jz4740_codec
*jz4740_codec
= snd_soc_component_get_drvdata(component
);
283 regmap_update_bits(jz4740_codec
->regmap
, JZ4740_REG_CODEC_1
,
284 JZ4740_CODEC_1_SW2_ENABLE
, JZ4740_CODEC_1_SW2_ENABLE
);
289 static 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,
300 .use_pmdown_time
= 1,
302 .non_legacy_dai_naming
= 1,
306 static const struct regmap_config jz4740_codec_regmap_config
= {
310 .max_register
= JZ4740_REG_CODEC_2
,
312 .reg_defaults
= jz4740_codec_reg_defaults
,
313 .num_reg_defaults
= ARRAY_SIZE(jz4740_codec_reg_defaults
),
314 .cache_type
= REGCACHE_RBTREE
,
317 static int jz4740_codec_probe(struct platform_device
*pdev
)
320 struct jz4740_codec
*jz4740_codec
;
323 jz4740_codec
= devm_kzalloc(&pdev
->dev
, sizeof(*jz4740_codec
),
328 base
= devm_platform_ioremap_resource(pdev
, 0);
330 return PTR_ERR(base
);
332 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
);
337 platform_set_drvdata(pdev
, jz4740_codec
);
339 ret
= devm_snd_soc_register_component(&pdev
->dev
,
340 &soc_codec_dev_jz4740_codec
, &jz4740_codec_dai
, 1);
342 dev_err(&pdev
->dev
, "Failed to register codec\n");
348 static const struct of_device_id jz4740_codec_of_matches
[] = {
349 { .compatible
= "ingenic,jz4740-codec", },
352 MODULE_DEVICE_TABLE(of
, jz4740_codec_of_matches
);
355 static struct platform_driver jz4740_codec_driver
= {
356 .probe
= jz4740_codec_probe
,
358 .name
= "jz4740-codec",
359 .of_match_table
= of_match_ptr(jz4740_codec_of_matches
),
363 module_platform_driver(jz4740_codec_driver
);
365 MODULE_DESCRIPTION("JZ4740 SoC internal codec driver");
366 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
367 MODULE_LICENSE("GPL v2");
368 MODULE_ALIAS("platform:jz4740-codec");