Date:2010-04-24 12:32:57 (10 years 4 months ago)
Author:Lars C.
Commit:e2038993b3efb2125ecfbfed6a3f55b562975bb8
Message:Add jz4740 codec driver

Files: sound/soc/codecs/Kconfig (1 diff)
sound/soc/codecs/Makefile (2 diffs)
sound/soc/codecs/jzcodec.c (1 diff)
sound/soc/codecs/jzcodec.h (1 diff)

Change Details

sound/soc/codecs/Kconfig
260260config SND_SOC_WM9713
261261    tristate
262262
263config SND_SOC_JZCODEC
264    tristate "JZ4720/JZ4740 SoC internal codec"
265    depends on SND_SOC && SOC_JZ4740
266    help
267      Say Y if you want to use internal codec on Ingenic JZ4720/JZ4740 based
268      boards.
269
263270# Amp
264271config SND_SOC_MAX9877
265272    tristate
sound/soc/codecs/Makefile
5454snd-soc-wm9712-objs := wm9712.o
5555snd-soc-wm9713-objs := wm9713.o
5656snd-soc-wm-hubs-objs := wm_hubs.o
57snd-soc-jzcodec-objs := jzcodec.o
5758
5859# Amp
5960snd-soc-max9877-objs := max9877.o
...... 
116117obj-$(CONFIG_SND_SOC_WM9712) += snd-soc-wm9712.o
117118obj-$(CONFIG_SND_SOC_WM9713) += snd-soc-wm9713.o
118119obj-$(CONFIG_SND_SOC_WM_HUBS) += snd-soc-wm-hubs.o
120obj-$(CONFIG_SND_SOC_JZCODEC) += snd-soc-jzcodec.o
119121
120122# Amp
121123obj-$(CONFIG_SND_SOC_MAX9877) += snd-soc-max9877.o
sound/soc/codecs/jzcodec.c
1/*
2 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
3 *
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.
7 *
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.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
18
19#include <linux/delay.h>
20
21#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/initval.h>
25#include <sound/soc-dapm.h>
26#include <sound/soc.h>
27
28#define JZ_REG_CODEC_1 0x0
29#define JZ_REG_CODEC_2 0x1
30
31#define JZ_CODEC_1_LINE_ENABLE BIT(29)
32#define JZ_CODEC_1_MIC_ENABLE BIT(28)
33#define JZ_CODEC_1_SW1_ENABLE BIT(27)
34#define JZ_CODEC_1_ADC_ENABLE BIT(26)
35#define JZ_CODEC_1_SW2_ENABLE BIT(25)
36#define JZ_CODEC_1_DAC_ENABLE BIT(24)
37#define JZ_CODEC_1_VREF_DISABLE BIT(20)
38#define JZ_CODEC_1_VREF_AMP_DISABLE BIT(19)
39#define JZ_CODEC_1_VREF_PULL_DOWN BIT(18)
40#define JZ_CODEC_1_VREF_LOW_CURRENT BIT(17)
41#define JZ_CODEC_1_VREF_HIGH_CURRENT BIT(16)
42#define JZ_CODEC_1_HEADPHONE_DISABLE BIT(14)
43#define JZ_CODEC_1_HEADPHONE_AMP_CHANGE_ANY BIT(13)
44#define JZ_CODEC_1_HEADPHONE_CHANGE BIT(12)
45#define JZ_CODEC_1_HEADPHONE_PULL_DOWN_M BIT(11)
46#define JZ_CODEC_1_HEADPHONE_PULL_DOWN_R BIT(10)
47#define JZ_CODEC_1_HEADPHONE_POWER_DOWN_M BIT(9)
48#define JZ_CODEC_1_HEADPHONE_POWER_DOWN BIT(8)
49#define JZ_CODEC_1_SUSPEND BIT(1)
50#define JZ_CODEC_1_RESET BIT(0)
51
52#define JZ_CODEC_1_LINE_ENABLE_OFFSET 29
53#define JZ_CODEC_1_MIC_ENABLE_OFFSET 28
54#define JZ_CODEC_1_SW1_ENABLE_OFFSET 27
55#define JZ_CODEC_1_ADC_ENABLE_OFFSET 26
56#define JZ_CODEC_1_SW2_ENABLE_OFFSET 25
57#define JZ_CODEC_1_DAC_ENABLE_OFFSET 24
58#define JZ_CODEC_1_HEADPHONE_DISABLE_OFFSET 14
59#define JZ_CODEC_1_HEADPHONE_POWER_DOWN_OFFSET 8
60
61#define JZ_CODEC_2_INPUT_VOLUME_MASK 0x1f0000
62#define JZ_CODEC_2_SAMPLE_RATE_MASK 0x000f00
63#define JZ_CODEC_2_MIC_BOOST_GAIN_MASK 0x000030
64#define JZ_CODEC_2_HEADPHONE_VOLUME_MASK 0x000003
65
66#define JZ_CODEC_2_INPUT_VOLUME_OFFSET 16
67#define JZ_CODEC_2_SAMPLE_RATE_OFFSET 8
68#define JZ_CODEC_2_MIC_BOOST_GAIN_OFFSET 4
69#define JZ_CODEC_2_HEADPHONE_VOLUME_OFFSET 0
70
71struct jz_codec {
72    void __iomem *base;
73    struct resource *mem;
74
75    uint32_t reg_cache[2];
76    struct snd_soc_codec codec;
77};
78
79static inline struct jz_codec *codec_to_jz(struct snd_soc_codec *codec)
80{
81    return container_of(codec, struct jz_codec, codec);
82}
83
84static unsigned int jz_codec_read(struct snd_soc_codec *codec, unsigned int reg)
85{
86    struct jz_codec *jz_codec = codec_to_jz(codec);
87    return readl(jz_codec->base + (reg << 2));
88}
89
90static int jz_codec_write(struct snd_soc_codec *codec, unsigned int reg,
91unsigned int val)
92{
93    struct jz_codec *jz_codec = codec_to_jz(codec);
94    jz_codec->reg_cache[reg] = val;
95
96    writel(val, jz_codec->base + (reg << 2));
97    return 0;
98}
99
100static const struct snd_kcontrol_new jz_codec_controls[] = {
101    SOC_SINGLE("Master Playback Volume", JZ_REG_CODEC_2,
102            JZ_CODEC_2_HEADPHONE_VOLUME_OFFSET, 3, 0),
103    SOC_SINGLE("Capture Volume", JZ_REG_CODEC_2,
104            JZ_CODEC_2_INPUT_VOLUME_OFFSET, 31, 0),
105    SOC_SINGLE("Master Playback Switch", JZ_REG_CODEC_1,
106            JZ_CODEC_1_HEADPHONE_DISABLE_OFFSET, 1, 1),
107    SOC_SINGLE("Mic Capture Volume", JZ_REG_CODEC_2,
108            JZ_CODEC_2_MIC_BOOST_GAIN_OFFSET, 3, 0),
109};
110
111static const struct snd_kcontrol_new jz_codec_output_controls[] = {
112    SOC_DAPM_SINGLE("Bypass Switch", JZ_REG_CODEC_1,
113            JZ_CODEC_1_SW1_ENABLE_OFFSET, 1, 0),
114    SOC_DAPM_SINGLE("DAC Switch", JZ_REG_CODEC_1,
115            JZ_CODEC_1_SW2_ENABLE_OFFSET, 1, 0),
116};
117
118static const struct snd_kcontrol_new jz_codec_input_controls[] = {
119    SOC_DAPM_SINGLE("Line Capture Switch", JZ_REG_CODEC_1,
120            JZ_CODEC_1_LINE_ENABLE_OFFSET, 1, 0),
121    SOC_DAPM_SINGLE("Mic Capture Switch", JZ_REG_CODEC_1,
122            JZ_CODEC_1_MIC_ENABLE_OFFSET, 1, 0),
123};
124
125static const struct snd_soc_dapm_widget jz_codec_dapm_widgets[] = {
126    SND_SOC_DAPM_ADC("ADC", "Capture", JZ_REG_CODEC_1,
127            JZ_CODEC_1_ADC_ENABLE_OFFSET, 0),
128    SND_SOC_DAPM_DAC("DAC", "Playback", JZ_REG_CODEC_1,
129            JZ_CODEC_1_DAC_ENABLE_OFFSET, 0),
130
131    SND_SOC_DAPM_MIXER("Output Mixer", JZ_REG_CODEC_1,
132            JZ_CODEC_1_HEADPHONE_POWER_DOWN_OFFSET, 1,
133            jz_codec_output_controls,
134            ARRAY_SIZE(jz_codec_output_controls)),
135
136    SND_SOC_DAPM_MIXER_NAMED_CTL("Input Mixer", SND_SOC_NOPM, 0, 0,
137            jz_codec_input_controls,
138            ARRAY_SIZE(jz_codec_input_controls)),
139    SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
140
141    SND_SOC_DAPM_OUTPUT("LOUT"),
142    SND_SOC_DAPM_OUTPUT("ROUT"),
143
144    SND_SOC_DAPM_INPUT("MIC"),
145    SND_SOC_DAPM_INPUT("LIN"),
146    SND_SOC_DAPM_INPUT("RIN"),
147};
148
149static const struct snd_soc_dapm_route jz_codec_dapm_routes[] = {
150
151    {"Line Input", NULL, "LIN"},
152    {"Line Input", NULL, "RIN"},
153
154    {"Input Mixer", "Line Capture Switch", "Line Input"},
155    {"Input Mixer", "Mic Capture Switch", "MIC"},
156
157    {"ADC", NULL, "Input Mixer"},
158
159    {"Output Mixer", "Bypass Switch", "Input Mixer"},
160    {"Output Mixer", "DAC Switch", "DAC"},
161
162    {"LOUT", NULL, "Output Mixer"},
163    {"ROUT", NULL, "Output Mixer"},
164};
165
166static int jz_codec_hw_params(struct snd_pcm_substream *substream, struct
167snd_pcm_hw_params *params, struct snd_soc_dai *dai)
168{
169    uint32_t val;
170    struct snd_soc_pcm_runtime *rtd = substream->private_data;
171    struct snd_soc_device *socdev = rtd->socdev;
172    struct snd_soc_codec *codec = socdev->card->codec;
173
174    switch (params_format(params)) {
175    case SNDRV_PCM_FORMAT_S8:
176    case SNDRV_PCM_FORMAT_S16_LE:
177    case SNDRV_PCM_FORMAT_S18_3LE:
178        break;
179    default:
180        return -EINVAL;
181        break;
182    }
183
184    switch (params_rate(params)) {
185    case 8000:
186        val = 0;
187        break;
188    case 11025:
189        val = 1;
190        break;
191    case 12000:
192        val = 2;
193        break;
194    case 16000:
195        val = 3;
196        break;
197    case 22050:
198        val = 4;
199        break;
200    case 24000:
201        val = 5;
202        break;
203    case 32000:
204        val = 6;
205        break;
206    case 44100:
207        val = 7;
208        break;
209    case 48000:
210        val = 8;
211        break;
212    default:
213        return -EINVAL;
214    }
215
216    val <<= JZ_CODEC_2_SAMPLE_RATE_OFFSET;
217
218    snd_soc_update_bits(codec, JZ_REG_CODEC_2,
219                JZ_CODEC_2_SAMPLE_RATE_MASK, val);
220
221    return 0;
222}
223
224static int jz_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
225{
226    switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
227    case SND_SOC_DAIFMT_CBM_CFM:
228        break;
229    default:
230        return -EINVAL;
231    }
232
233    switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
234    case SND_SOC_DAIFMT_I2S:
235        break;
236    default:
237        return -EINVAL;
238    }
239
240    switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
241    case SND_SOC_DAIFMT_NB_NF:
242        break;
243    default:
244        return -EINVAL;
245    }
246
247    return 0;
248}
249
250static int jz_codec_set_sysclk(struct snd_soc_dai *codec_dai,
251        int clk_id, unsigned int freq, int dir)
252{
253    return 0;
254}
255
256
257static struct snd_soc_dai_ops jz_codec_dai_ops = {
258    .hw_params = jz_codec_hw_params,
259    .set_fmt = jz_codec_set_fmt,
260/* .set_clkdiv = jz_codec_set_clkdiv,*/
261    .set_sysclk = jz_codec_set_sysclk,
262};
263
264struct snd_soc_dai jz_codec_dai = {
265    .name = "jz-codec",
266    .playback = {
267        .stream_name = "Playback",
268        .channels_min = 2,
269        .channels_max = 2,
270        .rates = SNDRV_PCM_RATE_8000_44100,
271        .formats = SNDRV_PCM_FMTBIT_S16_LE,
272    },
273    .capture = {
274        .stream_name = "Capture",
275        .channels_min = 2,
276        .channels_max = 2,
277        .rates = SNDRV_PCM_RATE_8000_44100,
278        .formats = SNDRV_PCM_FMTBIT_S16_LE,
279    },
280    .ops = &jz_codec_dai_ops,
281    .symmetric_rates = 1,
282};
283EXPORT_SYMBOL_GPL(jz_codec_dai);
284
285static int jz_codec_set_bias_level(struct snd_soc_codec *codec,
286                enum snd_soc_bias_level level)
287{
288
289    if (codec->bias_level == SND_SOC_BIAS_OFF && level != SND_SOC_BIAS_OFF) {
290        snd_soc_update_bits(codec, JZ_REG_CODEC_1,
291            JZ_CODEC_1_RESET, JZ_CODEC_1_RESET);
292        udelay(2);
293
294        snd_soc_update_bits(codec, JZ_REG_CODEC_1,
295            JZ_CODEC_1_SUSPEND | JZ_CODEC_1_RESET, 0);
296    }
297    switch (level) {
298    case SND_SOC_BIAS_ON:
299        snd_soc_update_bits(codec, JZ_REG_CODEC_1,
300            JZ_CODEC_1_VREF_DISABLE |
301            JZ_CODEC_1_VREF_AMP_DISABLE |
302            JZ_CODEC_1_HEADPHONE_POWER_DOWN_M |
303            JZ_CODEC_1_VREF_LOW_CURRENT |
304            JZ_CODEC_1_VREF_HIGH_CURRENT,
305            0);
306        break;
307    case SND_SOC_BIAS_PREPARE:
308        snd_soc_update_bits(codec, JZ_REG_CODEC_1,
309            JZ_CODEC_1_VREF_LOW_CURRENT |
310            JZ_CODEC_1_VREF_HIGH_CURRENT,
311            JZ_CODEC_1_VREF_LOW_CURRENT |
312            JZ_CODEC_1_VREF_HIGH_CURRENT);
313        break;
314    case SND_SOC_BIAS_STANDBY:
315        snd_soc_update_bits(codec, JZ_REG_CODEC_1,
316            JZ_CODEC_1_VREF_DISABLE | JZ_CODEC_1_VREF_AMP_DISABLE,
317            JZ_CODEC_1_VREF_DISABLE | JZ_CODEC_1_VREF_AMP_DISABLE);
318        break;
319    case SND_SOC_BIAS_OFF:
320        snd_soc_update_bits(codec, JZ_REG_CODEC_1,
321            JZ_CODEC_1_SUSPEND, JZ_CODEC_1_SUSPEND);
322        break;
323    }
324    codec->bias_level = level;
325
326    return 0;
327}
328
329
330static struct snd_soc_codec *jz_codec_codec;
331
332static int jz_codec_dev_probe(struct platform_device *pdev)
333{
334    int ret;
335    struct snd_soc_device *socdev = platform_get_drvdata(pdev);
336    struct snd_soc_codec *codec = jz_codec_codec;
337
338    BUG_ON(!codec);
339
340    socdev->card->codec = codec;
341
342    ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
343    if (ret) {
344        dev_err(&pdev->dev, "Failed to create pcms: %d\n", ret);
345        return ret;
346    }
347
348    snd_soc_add_controls(codec, jz_codec_controls,
349    ARRAY_SIZE(jz_codec_controls));
350
351    snd_soc_dapm_new_controls(codec, jz_codec_dapm_widgets,
352    ARRAY_SIZE(jz_codec_dapm_widgets));
353
354    snd_soc_dapm_add_routes(codec, jz_codec_dapm_routes,
355    ARRAY_SIZE(jz_codec_dapm_routes));
356
357    snd_soc_dapm_new_widgets(codec);
358
359    return 0;
360}
361
362static int jz_codec_dev_remove(struct platform_device *pdev)
363{
364    struct snd_soc_device *socdev = platform_get_drvdata(pdev);
365    snd_soc_free_pcms(socdev);
366    snd_soc_dapm_free(socdev);
367
368    return 0;
369}
370
371struct snd_soc_codec_device soc_codec_dev_jzcodec = {
372    .probe = jz_codec_dev_probe,
373    .remove = jz_codec_dev_remove,
374};
375EXPORT_SYMBOL_GPL(soc_codec_dev_jzcodec);
376
377static int __devinit jz_codec_probe(struct platform_device *pdev)
378{
379    int ret;
380    struct jz_codec *jz_codec;
381    struct snd_soc_codec *codec;
382
383    jz_codec = kzalloc(sizeof(*jz_codec), GFP_KERNEL);
384
385    if (!jz_codec)
386        return -ENOMEM;
387
388    jz_codec->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
389
390    if (!jz_codec->mem) {
391        dev_err(&pdev->dev, "Failed to get mmio memory resource\n");
392        ret = -ENOENT;
393        goto err_free_jz_codec;
394    }
395
396    jz_codec->mem = request_mem_region(jz_codec->mem->start,
397                resource_size(jz_codec->mem), pdev->name);
398
399    if (!jz_codec->mem) {
400        dev_err(&pdev->dev, "Failed to request mmio memory region\n");
401        ret = -EBUSY;
402        goto err_free_jz_codec;
403    }
404
405    jz_codec->base = ioremap(jz_codec->mem->start, resource_size(jz_codec->mem));
406
407    if (!jz_codec->base) {
408        dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
409        ret = -EBUSY;
410        goto err_release_mem_region;
411    }
412
413    jz_codec_dai.dev = &pdev->dev;
414
415    codec = &jz_codec->codec;
416
417    codec->dev = &pdev->dev;
418    codec->name = "jz-codec";
419    codec->owner = THIS_MODULE;
420
421    codec->read = jz_codec_read;
422    codec->write = jz_codec_write;
423    codec->set_bias_level = jz_codec_set_bias_level;
424    codec->bias_level = SND_SOC_BIAS_OFF;
425
426    codec->dai = &jz_codec_dai;
427    codec->num_dai = 1;
428
429    codec->reg_cache = jz_codec->reg_cache;
430    codec->reg_cache_size = 2;
431
432    codec->private_data = jz_codec;
433
434    mutex_init(&codec->mutex);
435    INIT_LIST_HEAD(&codec->dapm_widgets);
436    INIT_LIST_HEAD(&codec->dapm_paths);
437
438    jz_codec_codec = codec;
439
440    snd_soc_update_bits(codec, JZ_REG_CODEC_1,
441                JZ_CODEC_1_SW2_ENABLE, JZ_CODEC_1_SW2_ENABLE);
442
443
444    platform_set_drvdata(pdev, jz_codec);
445    ret = snd_soc_register_codec(codec);
446
447    if (ret) {
448        dev_err(&pdev->dev, "Failed to register codec\n");
449        goto err_iounmap;
450    }
451
452    ret = snd_soc_register_dai(&jz_codec_dai);
453    if (ret) {
454        dev_err(&pdev->dev, "Failed to register codec dai\n");
455        goto err_unregister_codec;
456    }
457
458    jz_codec_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
459
460    return 0;
461err_unregister_codec:
462    snd_soc_unregister_codec(codec);
463err_iounmap:
464    iounmap(jz_codec->base);
465err_release_mem_region:
466    release_mem_region(jz_codec->mem->start, resource_size(jz_codec->mem));
467err_free_jz_codec:
468    kfree(jz_codec);
469
470    return ret;
471}
472
473static int __devexit jz_codec_remove(struct platform_device *pdev)
474{
475    struct jz_codec *jz_codec = platform_get_drvdata(pdev);
476
477    snd_soc_unregister_dai(&jz_codec_dai);
478    snd_soc_unregister_codec(&jz_codec->codec);
479
480    iounmap(jz_codec->base);
481    release_mem_region(jz_codec->mem->start, resource_size(jz_codec->mem));
482
483    platform_set_drvdata(pdev, NULL);
484    kfree(jz_codec);
485
486    return 0;
487}
488
489static struct platform_driver jz_codec_driver = {
490    .probe = jz_codec_probe,
491    .remove = __devexit_p(jz_codec_remove),
492    .driver = {
493        .name = "jz4740-codec",
494        .owner = THIS_MODULE,
495    },
496};
497
498static int __init jz_codec_init(void)
499{
500    return platform_driver_register(&jz_codec_driver);
501}
502module_init(jz_codec_init);
503
504static void __exit jz_codec_exit(void)
505{
506    platform_driver_unregister(&jz_codec_driver);
507}
508module_exit(jz_codec_exit);
509
510MODULE_DESCRIPTION("JZ4720/JZ4740 SoC internal codec driver");
511MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
512MODULE_LICENSE("GPL v2");
513MODULE_ALIAS("platform:jz-codec");
sound/soc/codecs/jzcodec.h
1/*
2 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
3 *
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.
7 *
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.
11 *
12 */
13
14#ifndef _ICODEC_H
15#define _ICODEC_H
16
17#define JZCODEC_SYSCLK 0
18
19extern struct snd_soc_dai jz_codec_dai;
20extern struct snd_soc_codec_device soc_codec_dev_jzcodec;
21
22#endif

Archive Download the corresponding diff file



interactive