[xburst] jzcodec: Fix supported formats.
[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_FMTBIT_S18_3LE |
271 SNDRV_PCM_FMTBIT_S16_LE |
272 SNDRV_PCM_FMTBIT_S8,
273 },
274 .capture = {
275 .stream_name = "Capture",
276 .channels_min = 2,
277 .channels_max = 2,
278 .rates = SNDRV_PCM_RATE_8000_44100,
279 .formats = SNDRV_PCM_FMTBIT_S16_LE |
280 SNDRV_PCM_FMTBIT_S8,
281 },
282 .ops = &jz_codec_dai_ops,
283 .symmetric_rates = 1,
284 };
285 EXPORT_SYMBOL_GPL(jz_codec_dai);
286
287 static int jz_codec_set_bias_level(struct snd_soc_codec *codec,
288 enum snd_soc_bias_level level)
289 {
290
291 if (codec->bias_level == SND_SOC_BIAS_OFF && level != SND_SOC_BIAS_OFF) {
292 snd_soc_update_bits(codec, JZ_REG_CODEC_1,
293 JZ_CODEC_1_RESET, JZ_CODEC_1_RESET);
294 udelay(2);
295
296 snd_soc_update_bits(codec, JZ_REG_CODEC_1,
297 JZ_CODEC_1_SUSPEND | JZ_CODEC_1_RESET, 0);
298 }
299 switch (level) {
300 case SND_SOC_BIAS_ON:
301 snd_soc_update_bits(codec, JZ_REG_CODEC_1,
302 JZ_CODEC_1_VREF_DISABLE | JZ_CODEC_1_VREF_AMP_DISABLE |
303 JZ_CODEC_1_HEADPHONE_POWER_DOWN_M |
304 JZ_CODEC_1_VREF_LOW_CURRENT | 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 | JZ_CODEC_1_VREF_HIGH_CURRENT,
310 JZ_CODEC_1_VREF_LOW_CURRENT | JZ_CODEC_1_VREF_HIGH_CURRENT);
311 break;
312 case SND_SOC_BIAS_STANDBY:
313 snd_soc_update_bits(codec, JZ_REG_CODEC_1,
314 JZ_CODEC_1_VREF_DISABLE | JZ_CODEC_1_VREF_AMP_DISABLE,
315 JZ_CODEC_1_VREF_DISABLE | JZ_CODEC_1_VREF_AMP_DISABLE);
316 break;
317 case SND_SOC_BIAS_OFF:
318 snd_soc_update_bits(codec, JZ_REG_CODEC_1,
319 JZ_CODEC_1_SUSPEND, JZ_CODEC_1_SUSPEND);
320 break;
321 }
322 codec->bias_level = level;
323
324 return 0;
325 }
326
327
328 static struct snd_soc_codec *jz_codec_codec;
329
330 static int jz_codec_dev_probe(struct platform_device *pdev)
331 {
332 int ret;
333 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
334 struct snd_soc_codec *codec = jz_codec_codec;
335
336 BUG_ON(!codec);
337
338 socdev->card->codec = codec;
339
340 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
341 if (ret) {
342 dev_err(&pdev->dev, "Failed to create pcms: %d\n", ret);
343 goto err;
344 }
345 snd_soc_add_controls(codec, jz_codec_controls,
346 ARRAY_SIZE(jz_codec_controls));
347
348 snd_soc_dapm_new_controls(codec, jz_codec_dapm_widgets,
349 ARRAY_SIZE(jz_codec_dapm_widgets));
350
351 snd_soc_dapm_add_routes(codec, jz_codec_dapm_routes,
352 ARRAY_SIZE(jz_codec_dapm_routes));
353
354 snd_soc_dapm_new_widgets(codec);
355
356 ret = snd_soc_init_card(socdev);
357
358 if (ret) {
359 dev_err(&pdev->dev, "Failed to register card\n");
360 goto err;
361 }
362
363 return 0;
364
365 err:
366 snd_soc_free_pcms(socdev);
367 snd_soc_dapm_free(socdev);
368
369 return ret;
370 }
371
372 static int jz_codec_dev_remove(struct platform_device *pdev)
373 {
374 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
375 snd_soc_free_pcms(socdev);
376 snd_soc_dapm_free(socdev);
377
378 return 0;
379 }
380
381 struct snd_soc_codec_device soc_codec_dev_jzcodec = {
382 .probe = jz_codec_dev_probe,
383 .remove = jz_codec_dev_remove,
384 };
385 EXPORT_SYMBOL_GPL(soc_codec_dev_jzcodec);
386
387 static int __devinit jz_codec_probe(struct platform_device *pdev)
388 {
389 int ret;
390 struct jz_codec *jz_codec;
391 struct snd_soc_codec *codec;
392
393 jz_codec = kzalloc(sizeof(*jz_codec), GFP_KERNEL);
394
395 if (!jz_codec)
396 return -ENOMEM;
397
398 jz_codec->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
399
400 if (!jz_codec->mem) {
401 dev_err(&pdev->dev, "Failed to get mmio memory resource\n");
402 ret = -ENOENT;
403 goto err_free_jz_codec;
404 }
405
406 jz_codec->mem = request_mem_region(jz_codec->mem->start,
407 resource_size(jz_codec->mem), pdev->name);
408
409 if (!jz_codec->mem) {
410 dev_err(&pdev->dev, "Failed to request mmio memory region\n");
411 ret = -EBUSY;
412 goto err_free_jz_codec;
413 }
414
415 jz_codec->base = ioremap(jz_codec->mem->start, resource_size(jz_codec->mem));
416
417 if (!jz_codec->base) {
418 dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
419 ret = -EBUSY;
420 goto err_release_mem_region;
421 }
422
423 jz_codec_dai.dev = &pdev->dev;
424
425 codec = &jz_codec->codec;
426
427 codec->dev = &pdev->dev;
428 codec->name = "jz-codec";
429 codec->owner = THIS_MODULE;
430
431 codec->read = jz_codec_read;
432 codec->write = jz_codec_write;
433 codec->set_bias_level = jz_codec_set_bias_level;
434 codec->bias_level = SND_SOC_BIAS_OFF;
435
436 codec->dai = &jz_codec_dai;
437 codec->num_dai = 1;
438
439 codec->reg_cache = jz_codec->reg_cache;
440 codec->reg_cache_size = 2;
441
442 codec->private_data = jz_codec;
443
444 mutex_init(&codec->mutex);
445 INIT_LIST_HEAD(&codec->dapm_widgets);
446 INIT_LIST_HEAD(&codec->dapm_paths);
447
448 jz_codec_codec = codec;
449
450 snd_soc_update_bits(codec, JZ_REG_CODEC_1,
451 JZ_CODEC_1_SW2_ENABLE, JZ_CODEC_1_SW2_ENABLE);
452
453
454 platform_set_drvdata(pdev, jz_codec);
455 ret = snd_soc_register_codec(codec);
456
457 if (ret) {
458 dev_err(&pdev->dev, "Failed to register codec\n");
459 goto err_iounmap;
460 }
461
462 ret = snd_soc_register_dai(&jz_codec_dai);
463 if (ret) {
464 dev_err(&pdev->dev, "Failed to register codec dai\n");
465 goto err_unregister_codec;
466 }
467
468 jz_codec_set_bias_level (codec, SND_SOC_BIAS_STANDBY);
469
470 return 0;
471 err_unregister_codec:
472 snd_soc_unregister_codec(codec);
473 err_iounmap:
474 iounmap(jz_codec->base);
475 err_release_mem_region:
476 release_mem_region(jz_codec->mem->start, resource_size(jz_codec->mem));
477 err_free_jz_codec:
478 kfree(jz_codec);
479
480 return ret;
481 }
482
483 static int __devexit jz_codec_remove(struct platform_device *pdev)
484 {
485 struct jz_codec *jz_codec = platform_get_drvdata(pdev);
486
487 snd_soc_unregister_dai(&jz_codec_dai);
488 snd_soc_unregister_codec(&jz_codec->codec);
489
490 iounmap(jz_codec->base);
491 release_mem_region(jz_codec->mem->start, resource_size(jz_codec->mem));
492
493 platform_set_drvdata(pdev, NULL);
494 kfree(jz_codec);
495
496 return 0;
497 }
498
499 static struct platform_driver jz_codec_driver = {
500 .probe = jz_codec_probe,
501 .remove = __devexit_p(jz_codec_remove),
502 .driver = {
503 .name = "jz4740-codec",
504 .owner = THIS_MODULE,
505 },
506 };
507
508 static int __init jz_codec_init(void)
509 {
510 return platform_driver_register(&jz_codec_driver);
511 }
512 module_init(jz_codec_init);
513
514 static void __exit jz_codec_exit(void)
515 {
516 platform_driver_unregister(&jz_codec_driver);
517 }
518 module_exit(jz_codec_exit);
519
520 MODULE_DESCRIPTION("JZ4720/JZ4740 SoC internal codec driver");
521 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
522 MODULE_LICENSE("GPL v2");
523 MODULE_ALIAS("platform:jz-codec");