- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.git] / sound / soc / codecs / tlv320aic26.c
1 /*
2  * Texas Instruments TLV320AIC26 low power audio CODEC
3  * ALSA SoC CODEC driver
4  *
5  * Copyright (C) 2008 Secret Lab Technologies Ltd.
6  */
7
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/init.h>
11 #include <linux/delay.h>
12 #include <linux/pm.h>
13 #include <linux/device.h>
14 #include <linux/sysfs.h>
15 #include <linux/spi/spi.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/soc-dapm.h>
21 #include <sound/soc-of-simple.h>
22 #include <sound/initval.h>
23
24 #include "tlv320aic26.h"
25
26 MODULE_DESCRIPTION("ASoC TLV320AIC26 codec driver");
27 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
28 MODULE_LICENSE("GPL");
29
30 /* AIC26 driver private data */
31 struct aic26 {
32         struct spi_device *spi;
33         struct snd_soc_codec codec;
34         u16 reg_cache[AIC26_NUM_REGS];  /* shadow registers */
35         int master;
36         int datfm;
37         int mclk;
38
39         /* Keyclick parameters */
40         int keyclick_amplitude;
41         int keyclick_freq;
42         int keyclick_len;
43 };
44
45 /* ---------------------------------------------------------------------
46  * Register access routines
47  */
48 static unsigned int aic26_reg_read(struct snd_soc_codec *codec,
49                                    unsigned int reg)
50 {
51         struct aic26 *aic26 = codec->private_data;
52         u16 *cache = codec->reg_cache;
53         u16 cmd, value;
54         u8 buffer[2];
55         int rc;
56
57         if (reg >= AIC26_NUM_REGS) {
58                 WARN_ON_ONCE(1);
59                 return 0;
60         }
61
62         /* Do SPI transfer; first 16bits are command; remaining is
63          * register contents */
64         cmd = AIC26_READ_COMMAND_WORD(reg);
65         buffer[0] = (cmd >> 8) & 0xff;
66         buffer[1] = cmd & 0xff;
67         rc = spi_write_then_read(aic26->spi, buffer, 2, buffer, 2);
68         if (rc) {
69                 dev_err(&aic26->spi->dev, "AIC26 reg read error\n");
70                 return -EIO;
71         }
72         value = (buffer[0] << 8) | buffer[1];
73
74         /* Update the cache before returning with the value */
75         cache[reg] = value;
76         return value;
77 }
78
79 static unsigned int aic26_reg_read_cache(struct snd_soc_codec *codec,
80                                          unsigned int reg)
81 {
82         u16 *cache = codec->reg_cache;
83
84         if (reg >= AIC26_NUM_REGS) {
85                 WARN_ON_ONCE(1);
86                 return 0;
87         }
88
89         return cache[reg];
90 }
91
92 static int aic26_reg_write(struct snd_soc_codec *codec, unsigned int reg,
93                            unsigned int value)
94 {
95         struct aic26 *aic26 = codec->private_data;
96         u16 *cache = codec->reg_cache;
97         u16 cmd;
98         u8 buffer[4];
99         int rc;
100
101         if (reg >= AIC26_NUM_REGS) {
102                 WARN_ON_ONCE(1);
103                 return -EINVAL;
104         }
105
106         /* Do SPI transfer; first 16bits are command; remaining is data
107          * to write into register */
108         cmd = AIC26_WRITE_COMMAND_WORD(reg);
109         buffer[0] = (cmd >> 8) & 0xff;
110         buffer[1] = cmd & 0xff;
111         buffer[2] = value >> 8;
112         buffer[3] = value;
113         rc = spi_write(aic26->spi, buffer, 4);
114         if (rc) {
115                 dev_err(&aic26->spi->dev, "AIC26 reg read error\n");
116                 return -EIO;
117         }
118
119         /* update cache before returning */
120         cache[reg] = value;
121         return 0;
122 }
123
124 /* ---------------------------------------------------------------------
125  * Digital Audio Interface Operations
126  */
127 static int aic26_hw_params(struct snd_pcm_substream *substream,
128                            struct snd_pcm_hw_params *params,
129                            struct snd_soc_dai *dai)
130 {
131         struct snd_soc_pcm_runtime *rtd = substream->private_data;
132         struct snd_soc_device *socdev = rtd->socdev;
133         struct snd_soc_codec *codec = socdev->card->codec;
134         struct aic26 *aic26 = codec->private_data;
135         int fsref, divisor, wlen, pval, jval, dval, qval;
136         u16 reg;
137
138         dev_dbg(&aic26->spi->dev, "aic26_hw_params(substream=%p, params=%p)\n",
139                 substream, params);
140         dev_dbg(&aic26->spi->dev, "rate=%i format=%i\n", params_rate(params),
141                 params_format(params));
142
143         switch (params_rate(params)) {
144         case 8000:  fsref = 48000; divisor = AIC26_DIV_6; break;
145         case 11025: fsref = 44100; divisor = AIC26_DIV_4; break;
146         case 12000: fsref = 48000; divisor = AIC26_DIV_4; break;
147         case 16000: fsref = 48000; divisor = AIC26_DIV_3; break;
148         case 22050: fsref = 44100; divisor = AIC26_DIV_2; break;
149         case 24000: fsref = 48000; divisor = AIC26_DIV_2; break;
150         case 32000: fsref = 48000; divisor = AIC26_DIV_1_5; break;
151         case 44100: fsref = 44100; divisor = AIC26_DIV_1; break;
152         case 48000: fsref = 48000; divisor = AIC26_DIV_1; break;
153         default:
154                 dev_dbg(&aic26->spi->dev, "bad rate\n"); return -EINVAL;
155         }
156
157         /* select data word length */
158         switch (params_format(params)) {
159         case SNDRV_PCM_FORMAT_S8:     wlen = AIC26_WLEN_16; break;
160         case SNDRV_PCM_FORMAT_S16_BE: wlen = AIC26_WLEN_16; break;
161         case SNDRV_PCM_FORMAT_S24_BE: wlen = AIC26_WLEN_24; break;
162         case SNDRV_PCM_FORMAT_S32_BE: wlen = AIC26_WLEN_32; break;
163         default:
164                 dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
165         }
166
167         /* Configure PLL */
168         pval = 1;
169         jval = (fsref == 44100) ? 7 : 8;
170         dval = (fsref == 44100) ? 5264 : 1920;
171         qval = 0;
172         reg = 0x8000 | qval << 11 | pval << 8 | jval << 2;
173         aic26_reg_write(codec, AIC26_REG_PLL_PROG1, reg);
174         reg = dval << 2;
175         aic26_reg_write(codec, AIC26_REG_PLL_PROG2, reg);
176
177         /* Audio Control 3 (master mode, fsref rate) */
178         reg = aic26_reg_read_cache(codec, AIC26_REG_AUDIO_CTRL3);
179         reg &= ~0xf800;
180         if (aic26->master)
181                 reg |= 0x0800;
182         if (fsref == 48000)
183                 reg |= 0x2000;
184         aic26_reg_write(codec, AIC26_REG_AUDIO_CTRL3, reg);
185
186         /* Audio Control 1 (FSref divisor) */
187         reg = aic26_reg_read_cache(codec, AIC26_REG_AUDIO_CTRL1);
188         reg &= ~0x0fff;
189         reg |= wlen | aic26->datfm | (divisor << 3) | divisor;
190         aic26_reg_write(codec, AIC26_REG_AUDIO_CTRL1, reg);
191
192         return 0;
193 }
194
195 /**
196  * aic26_mute - Mute control to reduce noise when changing audio format
197  */
198 static int aic26_mute(struct snd_soc_dai *dai, int mute)
199 {
200         struct snd_soc_codec *codec = dai->codec;
201         struct aic26 *aic26 = codec->private_data;
202         u16 reg = aic26_reg_read_cache(codec, AIC26_REG_DAC_GAIN);
203
204         dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n",
205                 dai, mute);
206
207         if (mute)
208                 reg |= 0x8080;
209         else
210                 reg &= ~0x8080;
211         aic26_reg_write(codec, AIC26_REG_DAC_GAIN, reg);
212
213         return 0;
214 }
215
216 static int aic26_set_sysclk(struct snd_soc_dai *codec_dai,
217                             int clk_id, unsigned int freq, int dir)
218 {
219         struct snd_soc_codec *codec = codec_dai->codec;
220         struct aic26 *aic26 = codec->private_data;
221
222         dev_dbg(&aic26->spi->dev, "aic26_set_sysclk(dai=%p, clk_id==%i,"
223                 " freq=%i, dir=%i)\n",
224                 codec_dai, clk_id, freq, dir);
225
226         /* MCLK needs to fall between 2MHz and 50 MHz */
227         if ((freq < 2000000) || (freq > 50000000))
228                 return -EINVAL;
229
230         aic26->mclk = freq;
231         return 0;
232 }
233
234 static int aic26_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
235 {
236         struct snd_soc_codec *codec = codec_dai->codec;
237         struct aic26 *aic26 = codec->private_data;
238
239         dev_dbg(&aic26->spi->dev, "aic26_set_fmt(dai=%p, fmt==%i)\n",
240                 codec_dai, fmt);
241
242         /* set master/slave audio interface */
243         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
244         case SND_SOC_DAIFMT_CBM_CFM: aic26->master = 1; break;
245         case SND_SOC_DAIFMT_CBS_CFS: aic26->master = 0; break;
246         default:
247                 dev_dbg(&aic26->spi->dev, "bad master\n"); return -EINVAL;
248         }
249
250         /* interface format */
251         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
252         case SND_SOC_DAIFMT_I2S:     aic26->datfm = AIC26_DATFM_I2S; break;
253         case SND_SOC_DAIFMT_DSP_A:   aic26->datfm = AIC26_DATFM_DSP; break;
254         case SND_SOC_DAIFMT_RIGHT_J: aic26->datfm = AIC26_DATFM_RIGHTJ; break;
255         case SND_SOC_DAIFMT_LEFT_J:  aic26->datfm = AIC26_DATFM_LEFTJ; break;
256         default:
257                 dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
258         }
259
260         return 0;
261 }
262
263 /* ---------------------------------------------------------------------
264  * Digital Audio Interface Definition
265  */
266 #define AIC26_RATES     (SNDRV_PCM_RATE_8000  | SNDRV_PCM_RATE_11025 |\
267                          SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
268                          SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
269                          SNDRV_PCM_RATE_48000)
270 #define AIC26_FORMATS   (SNDRV_PCM_FMTBIT_S8     | SNDRV_PCM_FMTBIT_S16_BE |\
271                          SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
272
273 static struct snd_soc_dai_ops aic26_dai_ops = {
274         .hw_params      = aic26_hw_params,
275         .digital_mute   = aic26_mute,
276         .set_sysclk     = aic26_set_sysclk,
277         .set_fmt        = aic26_set_fmt,
278 };
279
280 struct snd_soc_dai aic26_dai = {
281         .name = "tlv320aic26",
282         .playback = {
283                 .stream_name = "Playback",
284                 .channels_min = 2,
285                 .channels_max = 2,
286                 .rates = AIC26_RATES,
287                 .formats = AIC26_FORMATS,
288         },
289         .capture = {
290                 .stream_name = "Capture",
291                 .channels_min = 2,
292                 .channels_max = 2,
293                 .rates = AIC26_RATES,
294                 .formats = AIC26_FORMATS,
295         },
296         .ops = &aic26_dai_ops,
297 };
298 EXPORT_SYMBOL_GPL(aic26_dai);
299
300 /* ---------------------------------------------------------------------
301  * ALSA controls
302  */
303 static const char *aic26_capture_src_text[] = {"Mic", "Aux"};
304 static const struct soc_enum aic26_capture_src_enum =
305         SOC_ENUM_SINGLE(AIC26_REG_AUDIO_CTRL1, 12, 2, aic26_capture_src_text);
306
307 static const struct snd_kcontrol_new aic26_snd_controls[] = {
308         /* Output */
309         SOC_DOUBLE("PCM Playback Volume", AIC26_REG_DAC_GAIN, 8, 0, 0x7f, 1),
310         SOC_DOUBLE("PCM Playback Switch", AIC26_REG_DAC_GAIN, 15, 7, 1, 1),
311         SOC_SINGLE("PCM Capture Volume", AIC26_REG_ADC_GAIN, 8, 0x7f, 0),
312         SOC_SINGLE("PCM Capture Mute", AIC26_REG_ADC_GAIN, 15, 1, 1),
313         SOC_SINGLE("Keyclick activate", AIC26_REG_AUDIO_CTRL2, 15, 0x1, 0),
314         SOC_SINGLE("Keyclick amplitude", AIC26_REG_AUDIO_CTRL2, 12, 0x7, 0),
315         SOC_SINGLE("Keyclick frequency", AIC26_REG_AUDIO_CTRL2, 8, 0x7, 0),
316         SOC_SINGLE("Keyclick period", AIC26_REG_AUDIO_CTRL2, 4, 0xf, 0),
317         SOC_ENUM("Capture Source", aic26_capture_src_enum),
318 };
319
320 /* ---------------------------------------------------------------------
321  * SoC CODEC portion of driver: probe and release routines
322  */
323 static int aic26_probe(struct platform_device *pdev)
324 {
325         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
326         struct snd_soc_codec *codec;
327         struct aic26 *aic26;
328         int ret, err;
329
330         dev_info(&pdev->dev, "Probing AIC26 SoC CODEC driver\n");
331         dev_dbg(&pdev->dev, "socdev=%p\n", socdev);
332         dev_dbg(&pdev->dev, "codec_data=%p\n", socdev->codec_data);
333
334         /* Fetch the relevant aic26 private data here (it's already been
335          * stored in the .codec pointer) */
336         aic26 = socdev->codec_data;
337         if (aic26 == NULL) {
338                 dev_err(&pdev->dev, "aic26: missing codec pointer\n");
339                 return -ENODEV;
340         }
341         codec = &aic26->codec;
342         socdev->card->codec = codec;
343
344         dev_dbg(&pdev->dev, "Registering PCMs, dev=%p, socdev->dev=%p\n",
345                 &pdev->dev, socdev->dev);
346         /* register pcms */
347         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
348         if (ret < 0) {
349                 dev_err(&pdev->dev, "aic26: failed to create pcms\n");
350                 return -ENODEV;
351         }
352
353         /* register controls */
354         dev_dbg(&pdev->dev, "Registering controls\n");
355         err = snd_soc_add_controls(codec, aic26_snd_controls,
356                         ARRAY_SIZE(aic26_snd_controls));
357         WARN_ON(err < 0);
358
359         return 0;
360 }
361
362 static int aic26_remove(struct platform_device *pdev)
363 {
364         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
365         snd_soc_free_pcms(socdev);
366         return 0;
367 }
368
369 struct snd_soc_codec_device aic26_soc_codec_dev = {
370         .probe = aic26_probe,
371         .remove = aic26_remove,
372 };
373 EXPORT_SYMBOL_GPL(aic26_soc_codec_dev);
374
375 /* ---------------------------------------------------------------------
376  * SPI device portion of driver: sysfs files for debugging
377  */
378
379 static ssize_t aic26_keyclick_show(struct device *dev,
380                                    struct device_attribute *attr, char *buf)
381 {
382         struct aic26 *aic26 = dev_get_drvdata(dev);
383         int val, amp, freq, len;
384
385         val = aic26_reg_read_cache(&aic26->codec, AIC26_REG_AUDIO_CTRL2);
386         amp = (val >> 12) & 0x7;
387         freq = (125 << ((val >> 8) & 0x7)) >> 1;
388         len = 2 * (1 + ((val >> 4) & 0xf));
389
390         return sprintf(buf, "amp=%x freq=%iHz len=%iclks\n", amp, freq, len);
391 }
392
393 /* Any write to the keyclick attribute will trigger the keyclick event */
394 static ssize_t aic26_keyclick_set(struct device *dev,
395                                   struct device_attribute *attr,
396                                   const char *buf, size_t count)
397 {
398         struct aic26 *aic26 = dev_get_drvdata(dev);
399         int val;
400
401         val = aic26_reg_read_cache(&aic26->codec, AIC26_REG_AUDIO_CTRL2);
402         val |= 0x8000;
403         aic26_reg_write(&aic26->codec, AIC26_REG_AUDIO_CTRL2, val);
404
405         return count;
406 }
407
408 static DEVICE_ATTR(keyclick, 0644, aic26_keyclick_show, aic26_keyclick_set);
409
410 /* ---------------------------------------------------------------------
411  * SPI device portion of driver: probe and release routines and SPI
412  *                               driver registration.
413  */
414 static int aic26_spi_probe(struct spi_device *spi)
415 {
416         struct aic26 *aic26;
417         int ret, i, reg;
418
419         dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n");
420
421         /* Allocate driver data */
422         aic26 = kzalloc(sizeof *aic26, GFP_KERNEL);
423         if (!aic26)
424                 return -ENOMEM;
425
426         /* Initialize the driver data */
427         aic26->spi = spi;
428         dev_set_drvdata(&spi->dev, aic26);
429
430         /* Setup what we can in the codec structure so that the register
431          * access functions will work as expected.  More will be filled
432          * out when it is probed by the SoC CODEC part of this driver */
433         aic26->codec.private_data = aic26;
434         aic26->codec.name = "aic26";
435         aic26->codec.owner = THIS_MODULE;
436         aic26->codec.dai = &aic26_dai;
437         aic26->codec.num_dai = 1;
438         aic26->codec.read = aic26_reg_read;
439         aic26->codec.write = aic26_reg_write;
440         aic26->master = 1;
441         mutex_init(&aic26->codec.mutex);
442         INIT_LIST_HEAD(&aic26->codec.dapm_widgets);
443         INIT_LIST_HEAD(&aic26->codec.dapm_paths);
444         aic26->codec.reg_cache_size = AIC26_NUM_REGS;
445         aic26->codec.reg_cache = aic26->reg_cache;
446
447         aic26_dai.dev = &spi->dev;
448         ret = snd_soc_register_dai(&aic26_dai);
449         if (ret != 0) {
450                 dev_err(&spi->dev, "Failed to register DAI: %d\n", ret);
451                 kfree(aic26);
452                 return ret;
453         }
454
455         /* Reset the codec to power on defaults */
456         aic26_reg_write(&aic26->codec, AIC26_REG_RESET, 0xBB00);
457
458         /* Power up CODEC */
459         aic26_reg_write(&aic26->codec, AIC26_REG_POWER_CTRL, 0);
460
461         /* Audio Control 3 (master mode, fsref rate) */
462         reg = aic26_reg_read(&aic26->codec, AIC26_REG_AUDIO_CTRL3);
463         reg &= ~0xf800;
464         reg |= 0x0800; /* set master mode */
465         aic26_reg_write(&aic26->codec, AIC26_REG_AUDIO_CTRL3, reg);
466
467         /* Fill register cache */
468         for (i = 0; i < ARRAY_SIZE(aic26->reg_cache); i++)
469                 aic26_reg_read(&aic26->codec, i);
470
471         /* Register the sysfs files for debugging */
472         /* Create SysFS files */
473         ret = device_create_file(&spi->dev, &dev_attr_keyclick);
474         if (ret)
475                 dev_info(&spi->dev, "error creating sysfs files\n");
476
477 #if defined(CONFIG_SND_SOC_OF_SIMPLE)
478         /* Tell the of_soc helper about this codec */
479         of_snd_soc_register_codec(&aic26_soc_codec_dev, aic26, &aic26_dai,
480                                   spi->dev.archdata.of_node);
481 #endif
482
483         dev_dbg(&spi->dev, "SPI device initialized\n");
484         return 0;
485 }
486
487 static int aic26_spi_remove(struct spi_device *spi)
488 {
489         struct aic26 *aic26 = dev_get_drvdata(&spi->dev);
490
491         snd_soc_unregister_dai(&aic26_dai);
492         kfree(aic26);
493
494         return 0;
495 }
496
497 static struct spi_driver aic26_spi = {
498         .driver = {
499                 .name = "tlv320aic26",
500                 .owner = THIS_MODULE,
501         },
502         .probe = aic26_spi_probe,
503         .remove = aic26_spi_remove,
504 };
505
506 static int __init aic26_init(void)
507 {
508         return spi_register_driver(&aic26_spi);
509 }
510 module_init(aic26_init);
511
512 static void __exit aic26_exit(void)
513 {
514         spi_unregister_driver(&aic26_spi);
515 }
516 module_exit(aic26_exit);