2 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * You should have received a copy of the GNU General Public License along
9 * with this program; if not, write to the Free Software Foundation, Inc.,
10 * 675 Mass Ave, Cambridge, MA 02139, USA.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/initval.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/soc.h>
26 #define JZ_REG_CODEC_1 0x0
27 #define JZ_REG_CODEC_2 0x1
29 #define JZ_CODEC_1_LINE_ENABLE BIT(29)
30 #define JZ_CODEC_1_MIC_ENABLE BIT(28)
31 #define JZ_CODEC_1_SW1_ENABLE BIT(27)
32 #define JZ_CODEC_1_ADC_ENABLE BIT(26)
33 #define JZ_CODEC_1_SW2_ENABLE BIT(25)
34 #define JZ_CODEC_1_DAC_ENABLE BIT(24)
35 #define JZ_CODEC_1_VREF_DISABLE BIT(20)
36 #define JZ_CODEC_1_VREF_AMP_DISABLE BIT(19)
37 #define JZ_CODEC_1_VREF_PULL_DOWN BIT(18)
38 #define JZ_CODEC_1_VREF_LOW_CURRENT BIT(17)
39 #define JZ_CODEC_1_VREF_HIGH_CURRENT BIT(16)
40 #define JZ_CODEC_1_HEADPHONE_DISABLE BIT(14)
41 #define JZ_CODEC_1_HEADPHONE_AMP_CHANGE_ANY BIT(13)
42 #define JZ_CODEC_1_HEADPHONE_CHANGE BIT(12)
43 #define JZ_CODEC_1_HEADPHONE_PULL_DOWN_M BIT(11)
44 #define JZ_CODEC_1_HEADPHONE_PULL_DOWN_R BIT(10)
45 #define JZ_CODEC_1_HEADPHONE_POWER_DOWN_M BIT(9)
46 #define JZ_CODEC_1_HEADPHONE_POWER_DOWN BIT(8)
47 #define JZ_CODEC_1_SUSPEND BIT(1)
48 #define JZ_CODEC_1_RESET BIT(0)
50 #define JZ_CODEC_1_LINE_ENABLE_OFFSET 29
51 #define JZ_CODEC_1_MIC_ENABLE_OFFSET 28
52 #define JZ_CODEC_1_SW1_ENABLE_OFFSET 27
53 #define JZ_CODEC_1_ADC_ENABLE_OFFSET 26
54 #define JZ_CODEC_1_SW2_ENABLE_OFFSET 25
55 #define JZ_CODEC_1_DAC_ENABLE_OFFSET 24
56 #define JZ_CODEC_1_HEADPHONE_DISABLE_OFFSET 14
57 #define JZ_CODEC_1_HEADPHONE_POWER_DOWN_OFFSET 8
59 #define JZ_CODEC_2_INPUT_VOLUME_MASK 0x1f0000
60 #define JZ_CODEC_2_SAMPLE_RATE_MASK 0x000f00
61 #define JZ_CODEC_2_MIC_BOOST_GAIN_MASK 0x000030
62 #define JZ_CODEC_2_HEADPHONE_VOLUME_MASK 0x000003
64 #define JZ_CODEC_2_INPUT_VOLUME_OFFSET 16
65 #define JZ_CODEC_2_SAMPLE_RATE_OFFSET 8
66 #define JZ_CODEC_2_MIC_BOOST_GAIN_OFFSET 4
67 #define JZ_CODEC_2_HEADPHONE_VOLUME_OFFSET 0
73 uint32_t reg_cache
[2];
74 struct snd_soc_codec codec
;
77 inline static struct jz_codec
*codec_to_jz(struct snd_soc_codec
*codec
)
79 return container_of(codec
, struct jz_codec
, codec
);
82 static unsigned int jz_codec_read(struct snd_soc_codec
*codec
, unsigned int reg
)
84 struct jz_codec
*jz_codec
= codec_to_jz(codec
);
85 return readl(jz_codec
->base
+ (reg
<< 2));
88 static int jz_codec_write(struct snd_soc_codec
*codec
, unsigned int reg
,
91 struct jz_codec
*jz_codec
= codec_to_jz(codec
);
92 jz_codec
->reg_cache
[reg
] = val
;
94 writel(val
, jz_codec
->base
+ (reg
<< 2));
98 static const struct snd_kcontrol_new jz_codec_controls
[] = {
99 SOC_SINGLE("Master Playback Volume", JZ_REG_CODEC_2
,
100 JZ_CODEC_2_HEADPHONE_VOLUME_OFFSET
, 3, 0),
101 SOC_SINGLE("Capture Volume", JZ_REG_CODEC_2
,
102 JZ_CODEC_2_INPUT_VOLUME_OFFSET
, 31, 0),
103 SOC_SINGLE("Master Playback Switch", JZ_REG_CODEC_1
,
104 JZ_CODEC_1_HEADPHONE_DISABLE_OFFSET
, 1, 1),
105 SOC_SINGLE("Mic Capture Volume", JZ_REG_CODEC_2
,
106 JZ_CODEC_2_MIC_BOOST_GAIN_OFFSET
, 3, 0),
109 static const struct snd_kcontrol_new jz_codec_output_controls
[] = {
110 SOC_DAPM_SINGLE("Bypass Switch", JZ_REG_CODEC_1
,
111 JZ_CODEC_1_SW1_ENABLE_OFFSET
, 1, 0),
112 SOC_DAPM_SINGLE("DAC Switch", JZ_REG_CODEC_1
,
113 JZ_CODEC_1_SW2_ENABLE_OFFSET
, 1, 0),
116 static const struct snd_kcontrol_new jz_codec_input_controls
[] =
118 SOC_DAPM_SINGLE("Line Capture Switch", JZ_REG_CODEC_1
,
119 JZ_CODEC_1_LINE_ENABLE_OFFSET
, 1, 0),
120 SOC_DAPM_SINGLE("Mic Capture Switch", JZ_REG_CODEC_1
,
121 JZ_CODEC_1_MIC_ENABLE_OFFSET
, 1, 0),
124 static const struct snd_soc_dapm_widget jz_codec_dapm_widgets
[] = {
125 SND_SOC_DAPM_ADC("ADC", "Capture", JZ_REG_CODEC_1
,
126 JZ_CODEC_1_ADC_ENABLE_OFFSET
, 0),
127 SND_SOC_DAPM_DAC("DAC", "Playback", JZ_REG_CODEC_1
,
128 JZ_CODEC_1_DAC_ENABLE_OFFSET
, 0),
130 SND_SOC_DAPM_MIXER("Output Mixer", JZ_REG_CODEC_1
,
131 JZ_CODEC_1_HEADPHONE_POWER_DOWN_OFFSET
, 1,
132 jz_codec_output_controls
,
133 ARRAY_SIZE(jz_codec_output_controls
)),
135 SND_SOC_DAPM_MIXER_NAMED_CTL("Input Mixer", SND_SOC_NOPM
, 0, 0,
136 jz_codec_input_controls
,
137 ARRAY_SIZE(jz_codec_input_controls
)),
138 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM
, 0, 0, NULL
, 0),
140 SND_SOC_DAPM_OUTPUT("LOUT"),
141 SND_SOC_DAPM_OUTPUT("ROUT"),
143 SND_SOC_DAPM_INPUT("MIC"),
144 SND_SOC_DAPM_INPUT("LIN"),
145 SND_SOC_DAPM_INPUT("RIN"),
148 static const struct snd_soc_dapm_route jz_codec_dapm_routes
[] = {
150 {"Line Input", NULL
, "LIN"},
151 {"Line Input", NULL
, "RIN"},
153 {"Input Mixer", "Line Capture Switch", "Line Input"},
154 {"Input Mixer", "Mic Capture Switch", "MIC"},
156 {"ADC", NULL
, "Input Mixer"},
158 {"Output Mixer", "Bypass Switch", "Input Mixer"},
159 {"Output Mixer", "DAC Switch", "DAC"},
161 {"LOUT", NULL
, "Output Mixer"},
162 {"ROUT", NULL
, "Output Mixer"},
165 static int jz_codec_hw_params(struct snd_pcm_substream
*substream
, struct
166 snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
169 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
170 struct snd_soc_device
*socdev
= rtd
->socdev
;
171 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
173 switch (params_format(params
)) {
174 case SNDRV_PCM_FORMAT_S8
:
175 case SNDRV_PCM_FORMAT_S16_LE
:
176 case SNDRV_PCM_FORMAT_S18_3LE
:
183 switch (params_rate(params
)) {
215 val
<<= JZ_CODEC_2_SAMPLE_RATE_OFFSET
;
217 snd_soc_update_bits(codec
, JZ_REG_CODEC_2
,
218 JZ_CODEC_2_SAMPLE_RATE_MASK
, val
);
223 static int jz_codec_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
225 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
226 case SND_SOC_DAIFMT_CBM_CFM
:
232 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
233 case SND_SOC_DAIFMT_I2S
:
239 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
240 case SND_SOC_DAIFMT_NB_NF
:
249 static int jz_codec_set_sysclk(struct snd_soc_dai
*codec_dai
,
250 int clk_id
, unsigned int freq
, int dir
)
256 static struct snd_soc_dai_ops jz_codec_dai_ops
= {
257 .hw_params
= jz_codec_hw_params
,
258 .set_fmt
= jz_codec_set_fmt
,
259 /* .set_clkdiv = jz_codec_set_clkdiv,*/
260 .set_sysclk
= jz_codec_set_sysclk
,
263 struct snd_soc_dai jz_codec_dai
= {
266 .stream_name
= "Playback",
269 .rates
= SNDRV_PCM_RATE_8000_44100
,
270 .formats
= SNDRV_PCM_FORMAT_S18_3LE
,
273 .stream_name
= "Capture",
276 .rates
= SNDRV_PCM_RATE_8000_44100
,
277 .formats
= SNDRV_PCM_FORMAT_S16_LE
,
279 .ops
= &jz_codec_dai_ops
,
280 .symmetric_rates
= 1,
282 EXPORT_SYMBOL_GPL(jz_codec_dai
);
284 static int jz_codec_set_bias_level(struct snd_soc_codec
*codec
,
285 enum snd_soc_bias_level level
)
288 if (codec
->bias_level
== SND_SOC_BIAS_OFF
&& level
!= SND_SOC_BIAS_OFF
) {
289 snd_soc_update_bits(codec
, JZ_REG_CODEC_1
,
290 JZ_CODEC_1_RESET
, JZ_CODEC_1_RESET
);
293 snd_soc_update_bits(codec
, JZ_REG_CODEC_1
,
294 JZ_CODEC_1_SUSPEND
| JZ_CODEC_1_RESET
, 0);
297 case SND_SOC_BIAS_ON
:
298 snd_soc_update_bits(codec
, JZ_REG_CODEC_1
,
299 JZ_CODEC_1_VREF_DISABLE
| JZ_CODEC_1_VREF_AMP_DISABLE
|
300 JZ_CODEC_1_HEADPHONE_POWER_DOWN_M
|
301 JZ_CODEC_1_VREF_LOW_CURRENT
| JZ_CODEC_1_VREF_HIGH_CURRENT
,
304 case SND_SOC_BIAS_PREPARE
:
305 snd_soc_update_bits(codec
, JZ_REG_CODEC_1
,
306 JZ_CODEC_1_VREF_LOW_CURRENT
| JZ_CODEC_1_VREF_HIGH_CURRENT
,
307 JZ_CODEC_1_VREF_LOW_CURRENT
| JZ_CODEC_1_VREF_HIGH_CURRENT
);
309 case SND_SOC_BIAS_STANDBY
:
310 snd_soc_update_bits(codec
, JZ_REG_CODEC_1
,
311 JZ_CODEC_1_VREF_DISABLE
| JZ_CODEC_1_VREF_AMP_DISABLE
,
312 JZ_CODEC_1_VREF_DISABLE
| JZ_CODEC_1_VREF_AMP_DISABLE
);
314 case SND_SOC_BIAS_OFF
:
315 snd_soc_update_bits(codec
, JZ_REG_CODEC_1
,
316 JZ_CODEC_1_SUSPEND
, JZ_CODEC_1_SUSPEND
);
319 codec
->bias_level
= level
;
325 static struct snd_soc_codec
*jz_codec_codec
;
327 static int jz_codec_dev_probe(struct platform_device
*pdev
)
330 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
331 struct snd_soc_codec
*codec
= jz_codec_codec
;
335 socdev
->card
->codec
= codec
;
337 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
339 dev_err(&pdev
->dev
, "Failed to create pcms: %d\n", ret
);
342 snd_soc_add_controls(codec
, jz_codec_controls
,
343 ARRAY_SIZE(jz_codec_controls
));
345 snd_soc_dapm_new_controls(codec
, jz_codec_dapm_widgets
,
346 ARRAY_SIZE(jz_codec_dapm_widgets
));
348 snd_soc_dapm_add_routes(codec
, jz_codec_dapm_routes
,
349 ARRAY_SIZE(jz_codec_dapm_routes
));
351 snd_soc_dapm_new_widgets(codec
);
353 ret
= snd_soc_init_card(socdev
);
356 dev_err(&pdev
->dev
, "Failed to register card\n");
363 snd_soc_free_pcms(socdev
);
364 snd_soc_dapm_free(socdev
);
369 static int jz_codec_dev_remove(struct platform_device
*pdev
)
371 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
372 snd_soc_free_pcms(socdev
);
373 snd_soc_dapm_free(socdev
);
378 struct snd_soc_codec_device soc_codec_dev_jzcodec
= {
379 .probe
= jz_codec_dev_probe
,
380 .remove
= jz_codec_dev_remove
,
382 EXPORT_SYMBOL_GPL(soc_codec_dev_jzcodec
);
384 static int __devinit
jz_codec_probe(struct platform_device
*pdev
)
387 struct jz_codec
*jz_codec
;
388 struct snd_soc_codec
*codec
;
390 jz_codec
= kzalloc(sizeof(*jz_codec
), GFP_KERNEL
);
395 jz_codec
->mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
397 if (!jz_codec
->mem
) {
398 dev_err(&pdev
->dev
, "Failed to get mmio memory resource\n");
400 goto err_free_jz_codec
;
403 jz_codec
->mem
= request_mem_region(jz_codec
->mem
->start
,
404 resource_size(jz_codec
->mem
), pdev
->name
);
406 if (!jz_codec
->mem
) {
407 dev_err(&pdev
->dev
, "Failed to request mmio memory region\n");
409 goto err_free_jz_codec
;
412 jz_codec
->base
= ioremap(jz_codec
->mem
->start
, resource_size(jz_codec
->mem
));
414 if (!jz_codec
->base
) {
415 dev_err(&pdev
->dev
, "Failed to ioremap mmio memory\n");
417 goto err_release_mem_region
;
420 jz_codec_dai
.dev
= &pdev
->dev
;
422 codec
= &jz_codec
->codec
;
424 codec
->dev
= &pdev
->dev
;
425 codec
->name
= "jz-codec";
426 codec
->owner
= THIS_MODULE
;
428 codec
->read
= jz_codec_read
;
429 codec
->write
= jz_codec_write
;
430 codec
->set_bias_level
= jz_codec_set_bias_level
;
431 codec
->bias_level
= SND_SOC_BIAS_OFF
;
433 codec
->dai
= &jz_codec_dai
;
436 codec
->reg_cache
= jz_codec
->reg_cache
;
437 codec
->reg_cache_size
= 2;
439 codec
->private_data
= jz_codec
;
441 mutex_init(&codec
->mutex
);
442 INIT_LIST_HEAD(&codec
->dapm_widgets
);
443 INIT_LIST_HEAD(&codec
->dapm_paths
);
445 jz_codec_codec
= codec
;
447 snd_soc_update_bits(codec
, JZ_REG_CODEC_1
,
448 JZ_CODEC_1_SW2_ENABLE
, JZ_CODEC_1_SW2_ENABLE
);
451 platform_set_drvdata(pdev
, jz_codec
);
452 ret
= snd_soc_register_codec(codec
);
455 dev_err(&pdev
->dev
, "Failed to register codec\n");
459 ret
= snd_soc_register_dai(&jz_codec_dai
);
461 dev_err(&pdev
->dev
, "Failed to register codec dai\n");
462 goto err_unregister_codec
;
465 jz_codec_set_bias_level (codec
, SND_SOC_BIAS_STANDBY
);
468 err_unregister_codec
:
469 snd_soc_unregister_codec(codec
);
471 iounmap(jz_codec
->base
);
472 err_release_mem_region
:
473 release_mem_region(jz_codec
->mem
->start
, resource_size(jz_codec
->mem
));
480 static int __devexit
jz_codec_remove(struct platform_device
*pdev
)
482 struct jz_codec
*jz_codec
= platform_get_drvdata(pdev
);
484 snd_soc_unregister_dai(&jz_codec_dai
);
485 snd_soc_unregister_codec(&jz_codec
->codec
);
487 iounmap(jz_codec
->base
);
488 release_mem_region(jz_codec
->mem
->start
, resource_size(jz_codec
->mem
));
490 platform_set_drvdata(pdev
, NULL
);
496 static struct platform_driver jz_codec_driver
= {
497 .probe
= jz_codec_probe
,
498 .remove
= __devexit_p(jz_codec_remove
),
500 .name
= "jz4740-codec",
501 .owner
= THIS_MODULE
,
505 static int __init
jz_codec_init(void)
507 return platform_driver_register(&jz_codec_driver
);
509 module_init(jz_codec_init
);
511 static void __exit
jz_codec_exit(void)
513 platform_driver_unregister(&jz_codec_driver
);
515 module_exit(jz_codec_exit
);
517 MODULE_DESCRIPTION("JZ4720/JZ4740 SoC internal codec driver");
518 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
519 MODULE_LICENSE("GPL v2");
520 MODULE_ALIAS("platform:jz-codec");