ce5aaf18772d3ce3ea996fa48413132f04903807
[openwrt/svn-archive/archive.git] / target / linux / xburst / files-2.6.32 / 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/delay.h>
18
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>
25
26 #define JZ_REG_CODEC_1 0x0
27 #define JZ_REG_CODEC_2 0x1
28
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)
49
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
58
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
63
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
68
69 struct jz_codec {
70 void __iomem *base;
71 struct resource *mem;
72
73 uint32_t reg_cache[2];
74 struct snd_soc_codec codec;
75 };
76
77 inline static struct jz_codec *codec_to_jz(struct snd_soc_codec *codec)
78 {
79 return container_of(codec, struct jz_codec, codec);
80 }
81
82 static unsigned int jz_codec_read(struct snd_soc_codec *codec, unsigned int reg)
83 {
84 struct jz_codec *jz_codec = codec_to_jz(codec);
85 return readl(jz_codec->base + (reg << 2));
86 }
87
88 static int jz_codec_write(struct snd_soc_codec *codec, unsigned int reg,
89 unsigned int val)
90 {
91 struct jz_codec *jz_codec = codec_to_jz(codec);
92 jz_codec->reg_cache[reg] = val;
93
94 writel(val, jz_codec->base + (reg << 2));
95 return 0;
96 }
97
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),
107 };
108
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),
114 };
115
116 static const struct snd_kcontrol_new jz_codec_input_controls[] =
117 {
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),
122 };
123
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),
129
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)),
134
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),
139
140 SND_SOC_DAPM_OUTPUT("LOUT"),
141 SND_SOC_DAPM_OUTPUT("ROUT"),
142
143 SND_SOC_DAPM_INPUT("MIC"),
144 SND_SOC_DAPM_INPUT("LIN"),
145 SND_SOC_DAPM_INPUT("RIN"),
146 };
147
148 static const struct snd_soc_dapm_route jz_codec_dapm_routes[] = {
149
150 {"Line Input", NULL, "LIN"},
151 {"Line Input", NULL, "RIN"},
152
153 {"Input Mixer", "Line Capture Switch", "Line Input"},
154 {"Input Mixer", "Mic Capture Switch", "MIC"},
155
156 {"ADC", NULL, "Input Mixer"},
157
158 {"Output Mixer", "Bypass Switch", "Input Mixer"},
159 {"Output Mixer", "DAC Switch", "DAC"},
160
161 {"LOUT", NULL, "Output Mixer"},
162 {"ROUT", NULL, "Output Mixer"},
163 };
164
165 static int jz_codec_hw_params(struct snd_pcm_substream *substream, struct
166 snd_pcm_hw_params *params, struct snd_soc_dai *dai)
167 {
168 uint32_t val;
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;
172
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:
177 break;
178 default:
179 return -EINVAL;
180 break;
181 }
182
183 switch (params_rate(params)) {
184 case 8000:
185 val = 0;
186 break;
187 case 11025:
188 val = 1;
189 break;
190 case 12000:
191 val = 2;
192 break;
193 case 16000:
194 val = 3;
195 break;
196 case 22050:
197 val = 4;
198 break;
199 case 24000:
200 val = 5;
201 break;
202 case 32000:
203 val = 6;
204 break;
205 case 44100:
206 val = 7;
207 break;
208 case 48000:
209 val = 8;
210 break;
211 default:
212 return -EINVAL;
213 }
214
215 val <<= JZ_CODEC_2_SAMPLE_RATE_OFFSET;
216
217 snd_soc_update_bits(codec, JZ_REG_CODEC_2,
218 JZ_CODEC_2_SAMPLE_RATE_MASK, val);
219
220 return 0;
221 }
222
223 static int jz_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
224 {
225 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
226 case SND_SOC_DAIFMT_CBM_CFM:
227 break;
228 default:
229 return -EINVAL;
230 }
231
232 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
233 case SND_SOC_DAIFMT_I2S:
234 break;
235 default:
236 return -EINVAL;
237 }
238
239 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
240 case SND_SOC_DAIFMT_NB_NF:
241 break;
242 default:
243 return -EINVAL;
244 }
245
246 return 0;
247 }
248
249 static int jz_codec_set_sysclk(struct snd_soc_dai *codec_dai,
250 int clk_id, unsigned int freq, int dir)
251 {
252 return 0;
253 }
254
255
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,
261 };
262
263 struct snd_soc_dai jz_codec_dai = {
264 .name = "jz-codec",
265 .playback = {
266 .stream_name = "Playback",
267 .channels_min = 2,
268 .channels_max = 2,
269 .rates = SNDRV_PCM_RATE_8000_44100,
270 .formats = SNDRV_PCM_FORMAT_S18_3LE,
271 },
272 .capture = {
273 .stream_name = "Capture",
274 .channels_min = 2,
275 .channels_max = 2,
276 .rates = SNDRV_PCM_RATE_8000_44100,
277 .formats = SNDRV_PCM_FORMAT_S16_LE,
278 },
279 .ops = &jz_codec_dai_ops,
280 .symmetric_rates = 1,
281 };
282 EXPORT_SYMBOL_GPL(jz_codec_dai);
283
284 static int jz_codec_set_bias_level(struct snd_soc_codec *codec,
285 enum snd_soc_bias_level level)
286 {
287
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);
291 udelay(2);
292
293 snd_soc_update_bits(codec, JZ_REG_CODEC_1,
294 JZ_CODEC_1_SUSPEND | JZ_CODEC_1_RESET, 0);
295 }
296 switch (level) {
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,
302 0);
303 break;
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);
308 break;
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);
313 break;
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);
317 break;
318 }
319 codec->bias_level = level;
320
321 return 0;
322 }
323
324
325 static struct snd_soc_codec *jz_codec_codec;
326
327 static int jz_codec_dev_probe(struct platform_device *pdev)
328 {
329 int ret;
330 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
331 struct snd_soc_codec *codec = jz_codec_codec;
332
333 BUG_ON(!codec);
334
335 socdev->card->codec = codec;
336
337 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
338 if (ret) {
339 dev_err(&pdev->dev, "Failed to create pcms: %d\n", ret);
340 goto err;
341 }
342 snd_soc_add_controls(codec, jz_codec_controls,
343 ARRAY_SIZE(jz_codec_controls));
344
345 snd_soc_dapm_new_controls(codec, jz_codec_dapm_widgets,
346 ARRAY_SIZE(jz_codec_dapm_widgets));
347
348 snd_soc_dapm_add_routes(codec, jz_codec_dapm_routes,
349 ARRAY_SIZE(jz_codec_dapm_routes));
350
351 snd_soc_dapm_new_widgets(codec);
352
353 ret = snd_soc_init_card(socdev);
354
355 if (ret) {
356 dev_err(&pdev->dev, "Failed to register card\n");
357 goto err;
358 }
359
360 return 0;
361
362 err:
363 snd_soc_free_pcms(socdev);
364 snd_soc_dapm_free(socdev);
365
366 return ret;
367 }
368
369 static int jz_codec_dev_remove(struct platform_device *pdev)
370 {
371 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
372 snd_soc_free_pcms(socdev);
373 snd_soc_dapm_free(socdev);
374
375 return 0;
376 }
377
378 struct snd_soc_codec_device soc_codec_dev_jzcodec = {
379 .probe = jz_codec_dev_probe,
380 .remove = jz_codec_dev_remove,
381 };
382 EXPORT_SYMBOL_GPL(soc_codec_dev_jzcodec);
383
384 static int __devinit jz_codec_probe(struct platform_device *pdev)
385 {
386 int ret;
387 struct jz_codec *jz_codec;
388 struct snd_soc_codec *codec;
389
390 jz_codec = kzalloc(sizeof(*jz_codec), GFP_KERNEL);
391
392 if (!jz_codec)
393 return -ENOMEM;
394
395 jz_codec->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
396
397 if (!jz_codec->mem) {
398 dev_err(&pdev->dev, "Failed to get mmio memory resource\n");
399 ret = -ENOENT;
400 goto err_free_jz_codec;
401 }
402
403 jz_codec->mem = request_mem_region(jz_codec->mem->start,
404 resource_size(jz_codec->mem), pdev->name);
405
406 if (!jz_codec->mem) {
407 dev_err(&pdev->dev, "Failed to request mmio memory region\n");
408 ret = -EBUSY;
409 goto err_free_jz_codec;
410 }
411
412 jz_codec->base = ioremap(jz_codec->mem->start, resource_size(jz_codec->mem));
413
414 if (!jz_codec->base) {
415 dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
416 ret = -EBUSY;
417 goto err_release_mem_region;
418 }
419
420 jz_codec_dai.dev = &pdev->dev;
421
422 codec = &jz_codec->codec;
423
424 codec->dev = &pdev->dev;
425 codec->name = "jz-codec";
426 codec->owner = THIS_MODULE;
427
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;
432
433 codec->dai = &jz_codec_dai;
434 codec->num_dai = 1;
435
436 codec->reg_cache = jz_codec->reg_cache;
437 codec->reg_cache_size = 2;
438
439 codec->private_data = jz_codec;
440
441 mutex_init(&codec->mutex);
442 INIT_LIST_HEAD(&codec->dapm_widgets);
443 INIT_LIST_HEAD(&codec->dapm_paths);
444
445 jz_codec_codec = codec;
446
447 snd_soc_update_bits(codec, JZ_REG_CODEC_1,
448 JZ_CODEC_1_SW2_ENABLE, JZ_CODEC_1_SW2_ENABLE);
449
450
451 platform_set_drvdata(pdev, jz_codec);
452 ret = snd_soc_register_codec(codec);
453
454 if (ret) {
455 dev_err(&pdev->dev, "Failed to register codec\n");
456 goto err_iounmap;
457 }
458
459 ret = snd_soc_register_dai(&jz_codec_dai);
460 if (ret) {
461 dev_err(&pdev->dev, "Failed to register codec dai\n");
462 goto err_unregister_codec;
463 }
464
465 jz_codec_set_bias_level (codec, SND_SOC_BIAS_STANDBY);
466
467 return 0;
468 err_unregister_codec:
469 snd_soc_unregister_codec(codec);
470 err_iounmap:
471 iounmap(jz_codec->base);
472 err_release_mem_region:
473 release_mem_region(jz_codec->mem->start, resource_size(jz_codec->mem));
474 err_free_jz_codec:
475 kfree(jz_codec);
476
477 return ret;
478 }
479
480 static int __devexit jz_codec_remove(struct platform_device *pdev)
481 {
482 struct jz_codec *jz_codec = platform_get_drvdata(pdev);
483
484 snd_soc_unregister_dai(&jz_codec_dai);
485 snd_soc_unregister_codec(&jz_codec->codec);
486
487 iounmap(jz_codec->base);
488 release_mem_region(jz_codec->mem->start, resource_size(jz_codec->mem));
489
490 platform_set_drvdata(pdev, NULL);
491 kfree(jz_codec);
492
493 return 0;
494 }
495
496 static struct platform_driver jz_codec_driver = {
497 .probe = jz_codec_probe,
498 .remove = __devexit_p(jz_codec_remove),
499 .driver = {
500 .name = "jz4740-codec",
501 .owner = THIS_MODULE,
502 },
503 };
504
505 static int __init jz_codec_init(void)
506 {
507 return platform_driver_register(&jz_codec_driver);
508 }
509 module_init(jz_codec_init);
510
511 static void __exit jz_codec_exit(void)
512 {
513 platform_driver_unregister(&jz_codec_driver);
514 }
515 module_exit(jz_codec_exit);
516
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");