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