1 From 075550fa667fca846d1217f649438df5cb4f9529 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Fri, 28 May 2010 19:53:05 +0200
4 Subject: [PATCH] Add jz4740 codec driver
7 sound/soc/codecs/Kconfig | 7 +
8 sound/soc/codecs/Makefile | 2 +
9 sound/soc/codecs/jzcodec.c | 513 ++++++++++++++++++++++++++++++++++++++++++++
10 sound/soc/codecs/jzcodec.h | 22 ++
11 4 files changed, 544 insertions(+), 0 deletions(-)
12 create mode 100644 sound/soc/codecs/jzcodec.c
13 create mode 100644 sound/soc/codecs/jzcodec.h
15 diff --git a/sound/soc/codecs/Kconfig b/sound/soc/codecs/Kconfig
16 index 1743d56..b25598a 100644
17 --- a/sound/soc/codecs/Kconfig
18 +++ b/sound/soc/codecs/Kconfig
19 @@ -260,6 +260,13 @@ config SND_SOC_WM9712
23 +config SND_SOC_JZCODEC
24 + tristate "JZ4720/JZ4740 SoC internal codec"
25 + depends on SND_SOC && SOC_JZ4740
27 + Say Y if you want to use internal codec on Ingenic JZ4720/JZ4740 based
31 config SND_SOC_MAX9877
33 diff --git a/sound/soc/codecs/Makefile b/sound/soc/codecs/Makefile
34 index dd5ce6d..3f6ce05 100644
35 --- a/sound/soc/codecs/Makefile
36 +++ b/sound/soc/codecs/Makefile
37 @@ -54,6 +54,7 @@ snd-soc-wm9705-objs := wm9705.o
38 snd-soc-wm9712-objs := wm9712.o
39 snd-soc-wm9713-objs := wm9713.o
40 snd-soc-wm-hubs-objs := wm_hubs.o
41 +snd-soc-jzcodec-objs := jzcodec.o
44 snd-soc-max9877-objs := max9877.o
45 @@ -116,6 +117,7 @@ obj-$(CONFIG_SND_SOC_WM9705) += snd-soc-wm9705.o
46 obj-$(CONFIG_SND_SOC_WM9712) += snd-soc-wm9712.o
47 obj-$(CONFIG_SND_SOC_WM9713) += snd-soc-wm9713.o
48 obj-$(CONFIG_SND_SOC_WM_HUBS) += snd-soc-wm-hubs.o
49 +obj-$(CONFIG_SND_SOC_JZCODEC) += snd-soc-jzcodec.o
52 obj-$(CONFIG_SND_SOC_MAX9877) += snd-soc-max9877.o
53 diff --git a/sound/soc/codecs/jzcodec.c b/sound/soc/codecs/jzcodec.c
55 index 0000000..91720fa
57 +++ b/sound/soc/codecs/jzcodec.c
60 + * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
62 + * This program is free software; you can redistribute it and/or modify
63 + * it under the terms of the GNU General Public License version 2 as
64 + * published by the Free Software Foundation.
66 + * You should have received a copy of the GNU General Public License along
67 + * with this program; if not, write to the Free Software Foundation, Inc.,
68 + * 675 Mass Ave, Cambridge, MA 02139, USA.
72 +#include <linux/kernel.h>
73 +#include <linux/module.h>
74 +#include <linux/platform_device.h>
75 +#include <linux/slab.h>
77 +#include <linux/delay.h>
79 +#include <sound/core.h>
80 +#include <sound/pcm.h>
81 +#include <sound/pcm_params.h>
82 +#include <sound/initval.h>
83 +#include <sound/soc-dapm.h>
84 +#include <sound/soc.h>
86 +#define JZ_REG_CODEC_1 0x0
87 +#define JZ_REG_CODEC_2 0x1
89 +#define JZ_CODEC_1_LINE_ENABLE BIT(29)
90 +#define JZ_CODEC_1_MIC_ENABLE BIT(28)
91 +#define JZ_CODEC_1_SW1_ENABLE BIT(27)
92 +#define JZ_CODEC_1_ADC_ENABLE BIT(26)
93 +#define JZ_CODEC_1_SW2_ENABLE BIT(25)
94 +#define JZ_CODEC_1_DAC_ENABLE BIT(24)
95 +#define JZ_CODEC_1_VREF_DISABLE BIT(20)
96 +#define JZ_CODEC_1_VREF_AMP_DISABLE BIT(19)
97 +#define JZ_CODEC_1_VREF_PULL_DOWN BIT(18)
98 +#define JZ_CODEC_1_VREF_LOW_CURRENT BIT(17)
99 +#define JZ_CODEC_1_VREF_HIGH_CURRENT BIT(16)
100 +#define JZ_CODEC_1_HEADPHONE_DISABLE BIT(14)
101 +#define JZ_CODEC_1_HEADPHONE_AMP_CHANGE_ANY BIT(13)
102 +#define JZ_CODEC_1_HEADPHONE_CHANGE BIT(12)
103 +#define JZ_CODEC_1_HEADPHONE_PULL_DOWN_M BIT(11)
104 +#define JZ_CODEC_1_HEADPHONE_PULL_DOWN_R BIT(10)
105 +#define JZ_CODEC_1_HEADPHONE_POWER_DOWN_M BIT(9)
106 +#define JZ_CODEC_1_HEADPHONE_POWER_DOWN BIT(8)
107 +#define JZ_CODEC_1_SUSPEND BIT(1)
108 +#define JZ_CODEC_1_RESET BIT(0)
110 +#define JZ_CODEC_1_LINE_ENABLE_OFFSET 29
111 +#define JZ_CODEC_1_MIC_ENABLE_OFFSET 28
112 +#define JZ_CODEC_1_SW1_ENABLE_OFFSET 27
113 +#define JZ_CODEC_1_ADC_ENABLE_OFFSET 26
114 +#define JZ_CODEC_1_SW2_ENABLE_OFFSET 25
115 +#define JZ_CODEC_1_DAC_ENABLE_OFFSET 24
116 +#define JZ_CODEC_1_HEADPHONE_DISABLE_OFFSET 14
117 +#define JZ_CODEC_1_HEADPHONE_POWER_DOWN_OFFSET 8
119 +#define JZ_CODEC_2_INPUT_VOLUME_MASK 0x1f0000
120 +#define JZ_CODEC_2_SAMPLE_RATE_MASK 0x000f00
121 +#define JZ_CODEC_2_MIC_BOOST_GAIN_MASK 0x000030
122 +#define JZ_CODEC_2_HEADPHONE_VOLUME_MASK 0x000003
124 +#define JZ_CODEC_2_INPUT_VOLUME_OFFSET 16
125 +#define JZ_CODEC_2_SAMPLE_RATE_OFFSET 8
126 +#define JZ_CODEC_2_MIC_BOOST_GAIN_OFFSET 4
127 +#define JZ_CODEC_2_HEADPHONE_VOLUME_OFFSET 0
130 + void __iomem *base;
131 + struct resource *mem;
133 + uint32_t reg_cache[2];
134 + struct snd_soc_codec codec;
137 +static inline struct jz_codec *codec_to_jz(struct snd_soc_codec *codec)
139 + return container_of(codec, struct jz_codec, codec);
142 +static unsigned int jz_codec_read(struct snd_soc_codec *codec, unsigned int reg)
144 + struct jz_codec *jz_codec = codec_to_jz(codec);
145 + return readl(jz_codec->base + (reg << 2));
148 +static int jz_codec_write(struct snd_soc_codec *codec, unsigned int reg,
151 + struct jz_codec *jz_codec = codec_to_jz(codec);
152 + jz_codec->reg_cache[reg] = val;
154 + writel(val, jz_codec->base + (reg << 2));
158 +static const struct snd_kcontrol_new jz_codec_controls[] = {
159 + SOC_SINGLE("Master Playback Volume", JZ_REG_CODEC_2,
160 + JZ_CODEC_2_HEADPHONE_VOLUME_OFFSET, 3, 0),
161 + SOC_SINGLE("Capture Volume", JZ_REG_CODEC_2,
162 + JZ_CODEC_2_INPUT_VOLUME_OFFSET, 31, 0),
163 + SOC_SINGLE("Master Playback Switch", JZ_REG_CODEC_1,
164 + JZ_CODEC_1_HEADPHONE_DISABLE_OFFSET, 1, 1),
165 + SOC_SINGLE("Mic Capture Volume", JZ_REG_CODEC_2,
166 + JZ_CODEC_2_MIC_BOOST_GAIN_OFFSET, 3, 0),
169 +static const struct snd_kcontrol_new jz_codec_output_controls[] = {
170 + SOC_DAPM_SINGLE("Bypass Switch", JZ_REG_CODEC_1,
171 + JZ_CODEC_1_SW1_ENABLE_OFFSET, 1, 0),
172 + SOC_DAPM_SINGLE("DAC Switch", JZ_REG_CODEC_1,
173 + JZ_CODEC_1_SW2_ENABLE_OFFSET, 1, 0),
176 +static const struct snd_kcontrol_new jz_codec_input_controls[] = {
177 + SOC_DAPM_SINGLE("Line Capture Switch", JZ_REG_CODEC_1,
178 + JZ_CODEC_1_LINE_ENABLE_OFFSET, 1, 0),
179 + SOC_DAPM_SINGLE("Mic Capture Switch", JZ_REG_CODEC_1,
180 + JZ_CODEC_1_MIC_ENABLE_OFFSET, 1, 0),
183 +static const struct snd_soc_dapm_widget jz_codec_dapm_widgets[] = {
184 + SND_SOC_DAPM_ADC("ADC", "Capture", JZ_REG_CODEC_1,
185 + JZ_CODEC_1_ADC_ENABLE_OFFSET, 0),
186 + SND_SOC_DAPM_DAC("DAC", "Playback", JZ_REG_CODEC_1,
187 + JZ_CODEC_1_DAC_ENABLE_OFFSET, 0),
189 + SND_SOC_DAPM_MIXER("Output Mixer", JZ_REG_CODEC_1,
190 + JZ_CODEC_1_HEADPHONE_POWER_DOWN_OFFSET, 1,
191 + jz_codec_output_controls,
192 + ARRAY_SIZE(jz_codec_output_controls)),
194 + SND_SOC_DAPM_MIXER_NAMED_CTL("Input Mixer", SND_SOC_NOPM, 0, 0,
195 + jz_codec_input_controls,
196 + ARRAY_SIZE(jz_codec_input_controls)),
197 + SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
199 + SND_SOC_DAPM_OUTPUT("LOUT"),
200 + SND_SOC_DAPM_OUTPUT("ROUT"),
202 + SND_SOC_DAPM_INPUT("MIC"),
203 + SND_SOC_DAPM_INPUT("LIN"),
204 + SND_SOC_DAPM_INPUT("RIN"),
207 +static const struct snd_soc_dapm_route jz_codec_dapm_routes[] = {
209 + {"Line Input", NULL, "LIN"},
210 + {"Line Input", NULL, "RIN"},
212 + {"Input Mixer", "Line Capture Switch", "Line Input"},
213 + {"Input Mixer", "Mic Capture Switch", "MIC"},
215 + {"ADC", NULL, "Input Mixer"},
217 + {"Output Mixer", "Bypass Switch", "Input Mixer"},
218 + {"Output Mixer", "DAC Switch", "DAC"},
220 + {"LOUT", NULL, "Output Mixer"},
221 + {"ROUT", NULL, "Output Mixer"},
224 +static int jz_codec_hw_params(struct snd_pcm_substream *substream, struct
225 +snd_pcm_hw_params *params, struct snd_soc_dai *dai)
228 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
229 + struct snd_soc_device *socdev = rtd->socdev;
230 + struct snd_soc_codec *codec = socdev->card->codec;
232 + switch (params_format(params)) {
233 + case SNDRV_PCM_FORMAT_S8:
234 + case SNDRV_PCM_FORMAT_S16_LE:
235 + case SNDRV_PCM_FORMAT_S18_3LE:
242 + switch (params_rate(params)) {
274 + val <<= JZ_CODEC_2_SAMPLE_RATE_OFFSET;
276 + snd_soc_update_bits(codec, JZ_REG_CODEC_2,
277 + JZ_CODEC_2_SAMPLE_RATE_MASK, val);
282 +static int jz_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
284 + switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
285 + case SND_SOC_DAIFMT_CBM_CFM:
291 + switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
292 + case SND_SOC_DAIFMT_I2S:
298 + switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
299 + case SND_SOC_DAIFMT_NB_NF:
308 +static int jz_codec_set_sysclk(struct snd_soc_dai *codec_dai,
309 + int clk_id, unsigned int freq, int dir)
315 +static struct snd_soc_dai_ops jz_codec_dai_ops = {
316 + .hw_params = jz_codec_hw_params,
317 + .set_fmt = jz_codec_set_fmt,
318 +/* .set_clkdiv = jz_codec_set_clkdiv,*/
319 + .set_sysclk = jz_codec_set_sysclk,
322 +struct snd_soc_dai jz_codec_dai = {
323 + .name = "jz-codec",
325 + .stream_name = "Playback",
328 + .rates = SNDRV_PCM_RATE_8000_48000,
329 + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
332 + .stream_name = "Capture",
335 + .rates = SNDRV_PCM_RATE_8000_48000,
336 + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
338 + .ops = &jz_codec_dai_ops,
339 + .symmetric_rates = 1,
341 +EXPORT_SYMBOL_GPL(jz_codec_dai);
343 +static int jz_codec_set_bias_level(struct snd_soc_codec *codec,
344 + enum snd_soc_bias_level level)
347 + if (codec->bias_level == SND_SOC_BIAS_OFF && level != SND_SOC_BIAS_OFF) {
348 + snd_soc_update_bits(codec, JZ_REG_CODEC_1,
349 + JZ_CODEC_1_RESET, JZ_CODEC_1_RESET);
352 + snd_soc_update_bits(codec, JZ_REG_CODEC_1,
353 + JZ_CODEC_1_SUSPEND | JZ_CODEC_1_RESET, 0);
356 + case SND_SOC_BIAS_ON:
357 + snd_soc_update_bits(codec, JZ_REG_CODEC_1,
358 + JZ_CODEC_1_VREF_DISABLE |
359 + JZ_CODEC_1_VREF_AMP_DISABLE |
360 + JZ_CODEC_1_HEADPHONE_POWER_DOWN_M |
361 + JZ_CODEC_1_VREF_LOW_CURRENT |
362 + JZ_CODEC_1_VREF_HIGH_CURRENT,
365 + case SND_SOC_BIAS_PREPARE:
366 + snd_soc_update_bits(codec, JZ_REG_CODEC_1,
367 + JZ_CODEC_1_VREF_LOW_CURRENT |
368 + JZ_CODEC_1_VREF_HIGH_CURRENT,
369 + JZ_CODEC_1_VREF_LOW_CURRENT |
370 + JZ_CODEC_1_VREF_HIGH_CURRENT);
372 + case SND_SOC_BIAS_STANDBY:
373 + snd_soc_update_bits(codec, JZ_REG_CODEC_1,
374 + JZ_CODEC_1_VREF_DISABLE | JZ_CODEC_1_VREF_AMP_DISABLE,
375 + JZ_CODEC_1_VREF_DISABLE | JZ_CODEC_1_VREF_AMP_DISABLE);
377 + case SND_SOC_BIAS_OFF:
378 + snd_soc_update_bits(codec, JZ_REG_CODEC_1,
379 + JZ_CODEC_1_SUSPEND, JZ_CODEC_1_SUSPEND);
382 + codec->bias_level = level;
388 +static struct snd_soc_codec *jz_codec_codec;
390 +static int jz_codec_dev_probe(struct platform_device *pdev)
393 + struct snd_soc_device *socdev = platform_get_drvdata(pdev);
394 + struct snd_soc_codec *codec = jz_codec_codec;
398 + socdev->card->codec = codec;
400 + ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
402 + dev_err(&pdev->dev, "Failed to create pcms: %d\n", ret);
406 + snd_soc_add_controls(codec, jz_codec_controls,
407 + ARRAY_SIZE(jz_codec_controls));
409 + snd_soc_dapm_new_controls(codec, jz_codec_dapm_widgets,
410 + ARRAY_SIZE(jz_codec_dapm_widgets));
412 + snd_soc_dapm_add_routes(codec, jz_codec_dapm_routes,
413 + ARRAY_SIZE(jz_codec_dapm_routes));
415 + snd_soc_dapm_new_widgets(codec);
420 +static int jz_codec_dev_remove(struct platform_device *pdev)
422 + struct snd_soc_device *socdev = platform_get_drvdata(pdev);
423 + snd_soc_free_pcms(socdev);
424 + snd_soc_dapm_free(socdev);
429 +struct snd_soc_codec_device soc_codec_dev_jzcodec = {
430 + .probe = jz_codec_dev_probe,
431 + .remove = jz_codec_dev_remove,
433 +EXPORT_SYMBOL_GPL(soc_codec_dev_jzcodec);
435 +static int __devinit jz_codec_probe(struct platform_device *pdev)
438 + struct jz_codec *jz_codec;
439 + struct snd_soc_codec *codec;
441 + jz_codec = kzalloc(sizeof(*jz_codec), GFP_KERNEL);
446 + jz_codec->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
448 + if (!jz_codec->mem) {
449 + dev_err(&pdev->dev, "Failed to get mmio memory resource\n");
451 + goto err_free_jz_codec;
454 + jz_codec->mem = request_mem_region(jz_codec->mem->start,
455 + resource_size(jz_codec->mem), pdev->name);
457 + if (!jz_codec->mem) {
458 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
460 + goto err_free_jz_codec;
463 + jz_codec->base = ioremap(jz_codec->mem->start, resource_size(jz_codec->mem));
465 + if (!jz_codec->base) {
466 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
468 + goto err_release_mem_region;
471 + jz_codec_dai.dev = &pdev->dev;
473 + codec = &jz_codec->codec;
475 + codec->dev = &pdev->dev;
476 + codec->name = "jz-codec";
477 + codec->owner = THIS_MODULE;
479 + codec->read = jz_codec_read;
480 + codec->write = jz_codec_write;
481 + codec->set_bias_level = jz_codec_set_bias_level;
482 + codec->bias_level = SND_SOC_BIAS_OFF;
484 + codec->dai = &jz_codec_dai;
485 + codec->num_dai = 1;
487 + codec->reg_cache = jz_codec->reg_cache;
488 + codec->reg_cache_size = 2;
490 + codec->private_data = jz_codec;
492 + mutex_init(&codec->mutex);
493 + INIT_LIST_HEAD(&codec->dapm_widgets);
494 + INIT_LIST_HEAD(&codec->dapm_paths);
496 + jz_codec_codec = codec;
498 + snd_soc_update_bits(codec, JZ_REG_CODEC_1,
499 + JZ_CODEC_1_SW2_ENABLE, JZ_CODEC_1_SW2_ENABLE);
502 + platform_set_drvdata(pdev, jz_codec);
503 + ret = snd_soc_register_codec(codec);
506 + dev_err(&pdev->dev, "Failed to register codec\n");
510 + ret = snd_soc_register_dai(&jz_codec_dai);
512 + dev_err(&pdev->dev, "Failed to register codec dai\n");
513 + goto err_unregister_codec;
516 + jz_codec_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
519 +err_unregister_codec:
520 + snd_soc_unregister_codec(codec);
522 + iounmap(jz_codec->base);
523 +err_release_mem_region:
524 + release_mem_region(jz_codec->mem->start, resource_size(jz_codec->mem));
531 +static int __devexit jz_codec_remove(struct platform_device *pdev)
533 + struct jz_codec *jz_codec = platform_get_drvdata(pdev);
535 + snd_soc_unregister_dai(&jz_codec_dai);
536 + snd_soc_unregister_codec(&jz_codec->codec);
538 + iounmap(jz_codec->base);
539 + release_mem_region(jz_codec->mem->start, resource_size(jz_codec->mem));
541 + platform_set_drvdata(pdev, NULL);
547 +static struct platform_driver jz_codec_driver = {
548 + .probe = jz_codec_probe,
549 + .remove = __devexit_p(jz_codec_remove),
551 + .name = "jz4740-codec",
552 + .owner = THIS_MODULE,
556 +static int __init jz_codec_init(void)
558 + return platform_driver_register(&jz_codec_driver);
560 +module_init(jz_codec_init);
562 +static void __exit jz_codec_exit(void)
564 + platform_driver_unregister(&jz_codec_driver);
566 +module_exit(jz_codec_exit);
568 +MODULE_DESCRIPTION("JZ4720/JZ4740 SoC internal codec driver");
569 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
570 +MODULE_LICENSE("GPL v2");
571 +MODULE_ALIAS("platform:jz-codec");
572 diff --git a/sound/soc/codecs/jzcodec.h b/sound/soc/codecs/jzcodec.h
574 index 0000000..05c57b8
576 +++ b/sound/soc/codecs/jzcodec.h
579 + * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
581 + * This program is free software; you can redistribute it and/or modify
582 + * it under the terms of the GNU General Public License version 2 as
583 + * published by the Free Software Foundation.
585 + * You should have received a copy of the GNU General Public License along
586 + * with this program; if not, write to the Free Software Foundation, Inc.,
587 + * 675 Mass Ave, Cambridge, MA 02139, USA.
594 +#define JZCODEC_SYSCLK 0
596 +extern struct snd_soc_dai jz_codec_dai;
597 +extern struct snd_soc_codec_device soc_codec_dev_jzcodec;