V4L/DVB: IR: jvc-decoder needs BITREVERSE
[linux-2.6.git] / sound / soc / codecs / ak4642.c
blob7528a54102b5b2cdc83b554ae8d8cf85d9f30888
1 /*
2 * ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
7 * Based on wm8731.c by Richard Purdie
8 * Based on ak4535.c by Richard Purdie
9 * Based on wm8753.c by Liam Girdwood
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 /* ** CAUTION **
18 * This is very simple driver.
19 * It can use headphone output / stereo input only
21 * AK4642 is not tested.
22 * AK4643 is tested.
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/i2c.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <sound/core.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37 #include <sound/soc-dapm.h>
38 #include <sound/initval.h>
40 #include "ak4642.h"
42 #define AK4642_VERSION "0.0.1"
44 #define PW_MGMT1 0x00
45 #define PW_MGMT2 0x01
46 #define SG_SL1 0x02
47 #define SG_SL2 0x03
48 #define MD_CTL1 0x04
49 #define MD_CTL2 0x05
50 #define TIMER 0x06
51 #define ALC_CTL1 0x07
52 #define ALC_CTL2 0x08
53 #define L_IVC 0x09
54 #define L_DVC 0x0a
55 #define ALC_CTL3 0x0b
56 #define R_IVC 0x0c
57 #define R_DVC 0x0d
58 #define MD_CTL3 0x0e
59 #define MD_CTL4 0x0f
60 #define PW_MGMT3 0x10
61 #define DF_S 0x11
62 #define FIL3_0 0x12
63 #define FIL3_1 0x13
64 #define FIL3_2 0x14
65 #define FIL3_3 0x15
66 #define EQ_0 0x16
67 #define EQ_1 0x17
68 #define EQ_2 0x18
69 #define EQ_3 0x19
70 #define EQ_4 0x1a
71 #define EQ_5 0x1b
72 #define FIL1_0 0x1c
73 #define FIL1_1 0x1d
74 #define FIL1_2 0x1e
75 #define FIL1_3 0x1f
76 #define PW_MGMT4 0x20
77 #define MD_CTL5 0x21
78 #define LO_MS 0x22
79 #define HP_MS 0x23
80 #define SPK_MS 0x24
82 #define AK4642_CACHEREGNUM 0x25
84 /* PW_MGMT2 */
85 #define HPMTN (1 << 6)
86 #define PMHPL (1 << 5)
87 #define PMHPR (1 << 4)
88 #define MS (1 << 3) /* master/slave select */
89 #define MCKO (1 << 1)
90 #define PMPLL (1 << 0)
92 #define PMHP_MASK (PMHPL | PMHPR)
93 #define PMHP PMHP_MASK
95 /* MD_CTL1 */
96 #define PLL3 (1 << 7)
97 #define PLL2 (1 << 6)
98 #define PLL1 (1 << 5)
99 #define PLL0 (1 << 4)
100 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
102 #define BCKO_MASK (1 << 3)
103 #define BCKO_64 BCKO_MASK
105 /* MD_CTL2 */
106 #define FS0 (1 << 0)
107 #define FS1 (1 << 1)
108 #define FS2 (1 << 2)
109 #define FS3 (1 << 5)
110 #define FS_MASK (FS0 | FS1 | FS2 | FS3)
112 struct snd_soc_codec_device soc_codec_dev_ak4642;
114 /* codec private data */
115 struct ak4642_priv {
116 struct snd_soc_codec codec;
119 static struct snd_soc_codec *ak4642_codec;
122 * ak4642 register cache
124 static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
125 0x0000, 0x0000, 0x0001, 0x0000,
126 0x0002, 0x0000, 0x0000, 0x0000,
127 0x00e1, 0x00e1, 0x0018, 0x0000,
128 0x00e1, 0x0018, 0x0011, 0x0008,
129 0x0000, 0x0000, 0x0000, 0x0000,
130 0x0000, 0x0000, 0x0000, 0x0000,
131 0x0000, 0x0000, 0x0000, 0x0000,
132 0x0000, 0x0000, 0x0000, 0x0000,
133 0x0000, 0x0000, 0x0000, 0x0000,
134 0x0000,
138 * read ak4642 register cache
140 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
141 unsigned int reg)
143 u16 *cache = codec->reg_cache;
144 if (reg >= AK4642_CACHEREGNUM)
145 return -1;
146 return cache[reg];
150 * write ak4642 register cache
152 static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
153 u16 reg, unsigned int value)
155 u16 *cache = codec->reg_cache;
156 if (reg >= AK4642_CACHEREGNUM)
157 return;
159 cache[reg] = value;
163 * write to the AK4642 register space
165 static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
166 unsigned int value)
168 u8 data[2];
170 /* data is
171 * D15..D8 AK4642 register offset
172 * D7...D0 register data
174 data[0] = reg & 0xff;
175 data[1] = value & 0xff;
177 if (codec->hw_write(codec->control_data, data, 2) == 2) {
178 ak4642_write_reg_cache(codec, reg, value);
179 return 0;
180 } else
181 return -EIO;
184 static int ak4642_sync(struct snd_soc_codec *codec)
186 u16 *cache = codec->reg_cache;
187 int i, r = 0;
189 for (i = 0; i < AK4642_CACHEREGNUM; i++)
190 r |= ak4642_write(codec, i, cache[i]);
192 return r;
195 static int ak4642_dai_startup(struct snd_pcm_substream *substream,
196 struct snd_soc_dai *dai)
198 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
199 struct snd_soc_codec *codec = dai->codec;
201 if (is_play) {
203 * start headphone output
205 * PLL, Master Mode
206 * Audio I/F Format :MSB justified (ADC & DAC)
207 * Digital Volume: -8dB
208 * Bass Boost Level : Middle
210 * This operation came from example code of
211 * "ASAHI KASEI AK4642" (japanese) manual p97.
213 ak4642_write(codec, 0x0f, 0x09);
214 ak4642_write(codec, 0x0e, 0x19);
215 ak4642_write(codec, 0x09, 0x91);
216 ak4642_write(codec, 0x0c, 0x91);
217 ak4642_write(codec, 0x0a, 0x28);
218 ak4642_write(codec, 0x0d, 0x28);
219 ak4642_write(codec, 0x00, 0x64);
220 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
221 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
222 } else {
224 * start stereo input
226 * PLL Master Mode
227 * Audio I/F Format:MSB justified (ADC & DAC)
228 * Pre MIC AMP:+20dB
229 * MIC Power On
230 * ALC setting:Refer to Table 35
231 * ALC bit=“1”
233 * This operation came from example code of
234 * "ASAHI KASEI AK4642" (japanese) manual p94.
236 ak4642_write(codec, 0x02, 0x05);
237 ak4642_write(codec, 0x06, 0x3c);
238 ak4642_write(codec, 0x08, 0xe1);
239 ak4642_write(codec, 0x0b, 0x00);
240 ak4642_write(codec, 0x07, 0x21);
241 ak4642_write(codec, 0x00, 0x41);
242 ak4642_write(codec, 0x10, 0x01);
245 return 0;
248 static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
249 struct snd_soc_dai *dai)
251 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
252 struct snd_soc_codec *codec = dai->codec;
254 if (is_play) {
255 /* stop headphone output */
256 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
257 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
258 ak4642_write(codec, 0x00, 0x40);
259 ak4642_write(codec, 0x0e, 0x11);
260 ak4642_write(codec, 0x0f, 0x08);
261 } else {
262 /* stop stereo input */
263 ak4642_write(codec, 0x00, 0x40);
264 ak4642_write(codec, 0x10, 0x00);
265 ak4642_write(codec, 0x07, 0x01);
269 static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
270 int clk_id, unsigned int freq, int dir)
272 struct snd_soc_codec *codec = codec_dai->codec;
273 u8 pll;
275 switch (freq) {
276 case 11289600:
277 pll = PLL2;
278 break;
279 case 12288000:
280 pll = PLL2 | PLL0;
281 break;
282 case 12000000:
283 pll = PLL2 | PLL1;
284 break;
285 case 24000000:
286 pll = PLL2 | PLL1 | PLL0;
287 break;
288 case 13500000:
289 pll = PLL3 | PLL2;
290 break;
291 case 27000000:
292 pll = PLL3 | PLL2 | PLL0;
293 break;
294 default:
295 return -EINVAL;
297 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
299 return 0;
302 static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
304 struct snd_soc_codec *codec = dai->codec;
305 u8 data;
306 u8 bcko;
308 data = MCKO | PMPLL; /* use MCKO */
309 bcko = 0;
311 /* set master/slave audio interface */
312 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
313 case SND_SOC_DAIFMT_CBM_CFM:
314 data |= MS;
315 bcko = BCKO_64;
316 break;
317 case SND_SOC_DAIFMT_CBS_CFS:
318 break;
319 default:
320 return -EINVAL;
322 snd_soc_update_bits(codec, PW_MGMT2, MS, data);
323 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
325 return 0;
328 static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
329 struct snd_pcm_hw_params *params,
330 struct snd_soc_dai *dai)
332 struct snd_soc_codec *codec = dai->codec;
333 u8 rate;
335 switch (params_rate(params)) {
336 case 7350:
337 rate = FS2;
338 break;
339 case 8000:
340 rate = 0;
341 break;
342 case 11025:
343 rate = FS2 | FS0;
344 break;
345 case 12000:
346 rate = FS0;
347 break;
348 case 14700:
349 rate = FS2 | FS1;
350 break;
351 case 16000:
352 rate = FS1;
353 break;
354 case 22050:
355 rate = FS2 | FS1 | FS0;
356 break;
357 case 24000:
358 rate = FS1 | FS0;
359 break;
360 case 29400:
361 rate = FS3 | FS2 | FS1;
362 break;
363 case 32000:
364 rate = FS3 | FS1;
365 break;
366 case 44100:
367 rate = FS3 | FS2 | FS1 | FS0;
368 break;
369 case 48000:
370 rate = FS3 | FS1 | FS0;
371 break;
372 default:
373 return -EINVAL;
374 break;
376 snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
378 return 0;
381 static struct snd_soc_dai_ops ak4642_dai_ops = {
382 .startup = ak4642_dai_startup,
383 .shutdown = ak4642_dai_shutdown,
384 .set_sysclk = ak4642_dai_set_sysclk,
385 .set_fmt = ak4642_dai_set_fmt,
386 .hw_params = ak4642_dai_hw_params,
389 struct snd_soc_dai ak4642_dai = {
390 .name = "AK4642",
391 .playback = {
392 .stream_name = "Playback",
393 .channels_min = 1,
394 .channels_max = 2,
395 .rates = SNDRV_PCM_RATE_8000_48000,
396 .formats = SNDRV_PCM_FMTBIT_S16_LE },
397 .capture = {
398 .stream_name = "Capture",
399 .channels_min = 1,
400 .channels_max = 2,
401 .rates = SNDRV_PCM_RATE_8000_48000,
402 .formats = SNDRV_PCM_FMTBIT_S16_LE },
403 .ops = &ak4642_dai_ops,
404 .symmetric_rates = 1,
406 EXPORT_SYMBOL_GPL(ak4642_dai);
408 static int ak4642_resume(struct platform_device *pdev)
410 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
411 struct snd_soc_codec *codec = socdev->card->codec;
413 ak4642_sync(codec);
414 return 0;
418 * initialise the AK4642 driver
419 * register the mixer and dsp interfaces with the kernel
421 static int ak4642_init(struct ak4642_priv *ak4642)
423 struct snd_soc_codec *codec = &ak4642->codec;
424 int ret = 0;
426 if (ak4642_codec) {
427 dev_err(codec->dev, "Another ak4642 is registered\n");
428 return -EINVAL;
431 mutex_init(&codec->mutex);
432 INIT_LIST_HEAD(&codec->dapm_widgets);
433 INIT_LIST_HEAD(&codec->dapm_paths);
435 snd_soc_codec_set_drvdata(codec, ak4642);
436 codec->name = "AK4642";
437 codec->owner = THIS_MODULE;
438 codec->read = ak4642_read_reg_cache;
439 codec->write = ak4642_write;
440 codec->dai = &ak4642_dai;
441 codec->num_dai = 1;
442 codec->hw_write = (hw_write_t)i2c_master_send;
443 codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
444 codec->reg_cache = kmemdup(ak4642_reg,
445 sizeof(ak4642_reg), GFP_KERNEL);
447 if (!codec->reg_cache)
448 return -ENOMEM;
450 ak4642_dai.dev = codec->dev;
451 ak4642_codec = codec;
453 ret = snd_soc_register_codec(codec);
454 if (ret) {
455 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
456 goto reg_cache_err;
459 ret = snd_soc_register_dai(&ak4642_dai);
460 if (ret) {
461 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
462 snd_soc_unregister_codec(codec);
463 goto reg_cache_err;
466 return ret;
468 reg_cache_err:
469 kfree(codec->reg_cache);
470 codec->reg_cache = NULL;
472 return ret;
475 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
476 static int ak4642_i2c_probe(struct i2c_client *i2c,
477 const struct i2c_device_id *id)
479 struct ak4642_priv *ak4642;
480 struct snd_soc_codec *codec;
481 int ret;
483 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
484 if (!ak4642)
485 return -ENOMEM;
487 codec = &ak4642->codec;
488 codec->dev = &i2c->dev;
490 i2c_set_clientdata(i2c, ak4642);
491 codec->control_data = i2c;
493 ret = ak4642_init(ak4642);
494 if (ret < 0)
495 printk(KERN_ERR "failed to initialise AK4642\n");
497 return ret;
500 static int ak4642_i2c_remove(struct i2c_client *client)
502 struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
504 snd_soc_unregister_dai(&ak4642_dai);
505 snd_soc_unregister_codec(&ak4642->codec);
506 kfree(ak4642->codec.reg_cache);
507 kfree(ak4642);
508 ak4642_codec = NULL;
510 return 0;
513 static const struct i2c_device_id ak4642_i2c_id[] = {
514 { "ak4642", 0 },
515 { "ak4643", 0 },
518 MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
520 static struct i2c_driver ak4642_i2c_driver = {
521 .driver = {
522 .name = "AK4642 I2C Codec",
523 .owner = THIS_MODULE,
525 .probe = ak4642_i2c_probe,
526 .remove = ak4642_i2c_remove,
527 .id_table = ak4642_i2c_id,
530 #endif
532 static int ak4642_probe(struct platform_device *pdev)
534 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
535 int ret;
537 if (!ak4642_codec) {
538 dev_err(&pdev->dev, "Codec device not registered\n");
539 return -ENODEV;
542 socdev->card->codec = ak4642_codec;
544 /* register pcms */
545 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
546 if (ret < 0) {
547 printk(KERN_ERR "ak4642: failed to create pcms\n");
548 goto pcm_err;
551 dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
552 return ret;
554 pcm_err:
555 return ret;
559 /* power down chip */
560 static int ak4642_remove(struct platform_device *pdev)
562 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
564 snd_soc_free_pcms(socdev);
565 snd_soc_dapm_free(socdev);
567 return 0;
570 struct snd_soc_codec_device soc_codec_dev_ak4642 = {
571 .probe = ak4642_probe,
572 .remove = ak4642_remove,
573 .resume = ak4642_resume,
575 EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
577 static int __init ak4642_modinit(void)
579 int ret = 0;
580 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
581 ret = i2c_add_driver(&ak4642_i2c_driver);
582 #endif
583 return ret;
586 module_init(ak4642_modinit);
588 static void __exit ak4642_exit(void)
590 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
591 i2c_del_driver(&ak4642_i2c_driver);
592 #endif
595 module_exit(ak4642_exit);
597 MODULE_DESCRIPTION("Soc AK4642 driver");
598 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
599 MODULE_LICENSE("GPL");