ath5k: ath5k_eeprom_mode_from_channel() returns signed
[linux/fpc-iii.git] / sound / soc / codecs / tlv320aic26.c
blob6b7d71ec0004ddb660e9e962a64002dc430ecd2d
1 /*
2 * Texas Instruments TLV320AIC26 low power audio CODEC
3 * ALSA SoC CODEC driver
5 * Copyright (C) 2008 Secret Lab Technologies Ltd.
6 */
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 <linux/slab.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22 #include <sound/initval.h>
24 #include "tlv320aic26.h"
26 MODULE_DESCRIPTION("ASoC TLV320AIC26 codec driver");
27 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
28 MODULE_LICENSE("GPL");
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;
39 /* Keyclick parameters */
40 int keyclick_amplitude;
41 int keyclick_freq;
42 int keyclick_len;
45 /* ---------------------------------------------------------------------
46 * Register access routines
48 static unsigned int aic26_reg_read(struct snd_soc_codec *codec,
49 unsigned int reg)
51 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
52 u16 *cache = codec->reg_cache;
53 u16 cmd, value;
54 u8 buffer[2];
55 int rc;
57 if (reg >= AIC26_NUM_REGS) {
58 WARN_ON_ONCE(1);
59 return 0;
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;
72 value = (buffer[0] << 8) | buffer[1];
74 /* Update the cache before returning with the value */
75 cache[reg] = value;
76 return value;
79 static unsigned int aic26_reg_read_cache(struct snd_soc_codec *codec,
80 unsigned int reg)
82 u16 *cache = codec->reg_cache;
84 if (reg >= AIC26_NUM_REGS) {
85 WARN_ON_ONCE(1);
86 return 0;
89 return cache[reg];
92 static int aic26_reg_write(struct snd_soc_codec *codec, unsigned int reg,
93 unsigned int value)
95 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
96 u16 *cache = codec->reg_cache;
97 u16 cmd;
98 u8 buffer[4];
99 int rc;
101 if (reg >= AIC26_NUM_REGS) {
102 WARN_ON_ONCE(1);
103 return -EINVAL;
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;
119 /* update cache before returning */
120 cache[reg] = value;
121 return 0;
124 /* ---------------------------------------------------------------------
125 * Digital Audio Interface Operations
127 static int aic26_hw_params(struct snd_pcm_substream *substream,
128 struct snd_pcm_hw_params *params,
129 struct snd_soc_dai *dai)
131 struct snd_soc_pcm_runtime *rtd = substream->private_data;
132 struct snd_soc_codec *codec = rtd->codec;
133 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
134 int fsref, divisor, wlen, pval, jval, dval, qval;
135 u16 reg;
137 dev_dbg(&aic26->spi->dev, "aic26_hw_params(substream=%p, params=%p)\n",
138 substream, params);
139 dev_dbg(&aic26->spi->dev, "rate=%i format=%i\n", params_rate(params),
140 params_format(params));
142 switch (params_rate(params)) {
143 case 8000: fsref = 48000; divisor = AIC26_DIV_6; break;
144 case 11025: fsref = 44100; divisor = AIC26_DIV_4; break;
145 case 12000: fsref = 48000; divisor = AIC26_DIV_4; break;
146 case 16000: fsref = 48000; divisor = AIC26_DIV_3; break;
147 case 22050: fsref = 44100; divisor = AIC26_DIV_2; break;
148 case 24000: fsref = 48000; divisor = AIC26_DIV_2; break;
149 case 32000: fsref = 48000; divisor = AIC26_DIV_1_5; break;
150 case 44100: fsref = 44100; divisor = AIC26_DIV_1; break;
151 case 48000: fsref = 48000; divisor = AIC26_DIV_1; break;
152 default:
153 dev_dbg(&aic26->spi->dev, "bad rate\n"); return -EINVAL;
156 /* select data word length */
157 switch (params_format(params)) {
158 case SNDRV_PCM_FORMAT_S8: wlen = AIC26_WLEN_16; break;
159 case SNDRV_PCM_FORMAT_S16_BE: wlen = AIC26_WLEN_16; break;
160 case SNDRV_PCM_FORMAT_S24_BE: wlen = AIC26_WLEN_24; break;
161 case SNDRV_PCM_FORMAT_S32_BE: wlen = AIC26_WLEN_32; break;
162 default:
163 dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
166 /* Configure PLL */
167 pval = 1;
168 jval = (fsref == 44100) ? 7 : 8;
169 dval = (fsref == 44100) ? 5264 : 1920;
170 qval = 0;
171 reg = 0x8000 | qval << 11 | pval << 8 | jval << 2;
172 aic26_reg_write(codec, AIC26_REG_PLL_PROG1, reg);
173 reg = dval << 2;
174 aic26_reg_write(codec, AIC26_REG_PLL_PROG2, reg);
176 /* Audio Control 3 (master mode, fsref rate) */
177 reg = aic26_reg_read_cache(codec, AIC26_REG_AUDIO_CTRL3);
178 reg &= ~0xf800;
179 if (aic26->master)
180 reg |= 0x0800;
181 if (fsref == 48000)
182 reg |= 0x2000;
183 aic26_reg_write(codec, AIC26_REG_AUDIO_CTRL3, reg);
185 /* Audio Control 1 (FSref divisor) */
186 reg = aic26_reg_read_cache(codec, AIC26_REG_AUDIO_CTRL1);
187 reg &= ~0x0fff;
188 reg |= wlen | aic26->datfm | (divisor << 3) | divisor;
189 aic26_reg_write(codec, AIC26_REG_AUDIO_CTRL1, reg);
191 return 0;
195 * aic26_mute - Mute control to reduce noise when changing audio format
197 static int aic26_mute(struct snd_soc_dai *dai, int mute)
199 struct snd_soc_codec *codec = dai->codec;
200 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
201 u16 reg = aic26_reg_read_cache(codec, AIC26_REG_DAC_GAIN);
203 dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n",
204 dai, mute);
206 if (mute)
207 reg |= 0x8080;
208 else
209 reg &= ~0x8080;
210 aic26_reg_write(codec, AIC26_REG_DAC_GAIN, reg);
212 return 0;
215 static int aic26_set_sysclk(struct snd_soc_dai *codec_dai,
216 int clk_id, unsigned int freq, int dir)
218 struct snd_soc_codec *codec = codec_dai->codec;
219 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
221 dev_dbg(&aic26->spi->dev, "aic26_set_sysclk(dai=%p, clk_id==%i,"
222 " freq=%i, dir=%i)\n",
223 codec_dai, clk_id, freq, dir);
225 /* MCLK needs to fall between 2MHz and 50 MHz */
226 if ((freq < 2000000) || (freq > 50000000))
227 return -EINVAL;
229 aic26->mclk = freq;
230 return 0;
233 static int aic26_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
235 struct snd_soc_codec *codec = codec_dai->codec;
236 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
238 dev_dbg(&aic26->spi->dev, "aic26_set_fmt(dai=%p, fmt==%i)\n",
239 codec_dai, fmt);
241 /* set master/slave audio interface */
242 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
243 case SND_SOC_DAIFMT_CBM_CFM: aic26->master = 1; break;
244 case SND_SOC_DAIFMT_CBS_CFS: aic26->master = 0; break;
245 default:
246 dev_dbg(&aic26->spi->dev, "bad master\n"); return -EINVAL;
249 /* interface format */
250 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
251 case SND_SOC_DAIFMT_I2S: aic26->datfm = AIC26_DATFM_I2S; break;
252 case SND_SOC_DAIFMT_DSP_A: aic26->datfm = AIC26_DATFM_DSP; break;
253 case SND_SOC_DAIFMT_RIGHT_J: aic26->datfm = AIC26_DATFM_RIGHTJ; break;
254 case SND_SOC_DAIFMT_LEFT_J: aic26->datfm = AIC26_DATFM_LEFTJ; break;
255 default:
256 dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
259 return 0;
262 /* ---------------------------------------------------------------------
263 * Digital Audio Interface Definition
265 #define AIC26_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
266 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
267 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
268 SNDRV_PCM_RATE_48000)
269 #define AIC26_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |\
270 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
272 static struct snd_soc_dai_ops aic26_dai_ops = {
273 .hw_params = aic26_hw_params,
274 .digital_mute = aic26_mute,
275 .set_sysclk = aic26_set_sysclk,
276 .set_fmt = aic26_set_fmt,
279 static struct snd_soc_dai_driver aic26_dai = {
280 .name = "tlv320aic26-hifi",
281 .playback = {
282 .stream_name = "Playback",
283 .channels_min = 2,
284 .channels_max = 2,
285 .rates = AIC26_RATES,
286 .formats = AIC26_FORMATS,
288 .capture = {
289 .stream_name = "Capture",
290 .channels_min = 2,
291 .channels_max = 2,
292 .rates = AIC26_RATES,
293 .formats = AIC26_FORMATS,
295 .ops = &aic26_dai_ops,
298 /* ---------------------------------------------------------------------
299 * ALSA controls
301 static const char *aic26_capture_src_text[] = {"Mic", "Aux"};
302 static const struct soc_enum aic26_capture_src_enum =
303 SOC_ENUM_SINGLE(AIC26_REG_AUDIO_CTRL1, 12, 2, aic26_capture_src_text);
305 static const struct snd_kcontrol_new aic26_snd_controls[] = {
306 /* Output */
307 SOC_DOUBLE("PCM Playback Volume", AIC26_REG_DAC_GAIN, 8, 0, 0x7f, 1),
308 SOC_DOUBLE("PCM Playback Switch", AIC26_REG_DAC_GAIN, 15, 7, 1, 1),
309 SOC_SINGLE("PCM Capture Volume", AIC26_REG_ADC_GAIN, 8, 0x7f, 0),
310 SOC_SINGLE("PCM Capture Mute", AIC26_REG_ADC_GAIN, 15, 1, 1),
311 SOC_SINGLE("Keyclick activate", AIC26_REG_AUDIO_CTRL2, 15, 0x1, 0),
312 SOC_SINGLE("Keyclick amplitude", AIC26_REG_AUDIO_CTRL2, 12, 0x7, 0),
313 SOC_SINGLE("Keyclick frequency", AIC26_REG_AUDIO_CTRL2, 8, 0x7, 0),
314 SOC_SINGLE("Keyclick period", AIC26_REG_AUDIO_CTRL2, 4, 0xf, 0),
315 SOC_ENUM("Capture Source", aic26_capture_src_enum),
318 /* ---------------------------------------------------------------------
319 * SPI device portion of driver: sysfs files for debugging
322 static ssize_t aic26_keyclick_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 struct aic26 *aic26 = dev_get_drvdata(dev);
326 int val, amp, freq, len;
328 val = aic26_reg_read_cache(&aic26->codec, AIC26_REG_AUDIO_CTRL2);
329 amp = (val >> 12) & 0x7;
330 freq = (125 << ((val >> 8) & 0x7)) >> 1;
331 len = 2 * (1 + ((val >> 4) & 0xf));
333 return sprintf(buf, "amp=%x freq=%iHz len=%iclks\n", amp, freq, len);
336 /* Any write to the keyclick attribute will trigger the keyclick event */
337 static ssize_t aic26_keyclick_set(struct device *dev,
338 struct device_attribute *attr,
339 const char *buf, size_t count)
341 struct aic26 *aic26 = dev_get_drvdata(dev);
342 int val;
344 val = aic26_reg_read_cache(&aic26->codec, AIC26_REG_AUDIO_CTRL2);
345 val |= 0x8000;
346 aic26_reg_write(&aic26->codec, AIC26_REG_AUDIO_CTRL2, val);
348 return count;
351 static DEVICE_ATTR(keyclick, 0644, aic26_keyclick_show, aic26_keyclick_set);
353 /* ---------------------------------------------------------------------
354 * SoC CODEC portion of driver: probe and release routines
356 static int aic26_probe(struct snd_soc_codec *codec)
358 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
359 int ret, err, i, reg;
361 dev_info(codec->dev, "Probing AIC26 SoC CODEC driver\n");
363 /* Reset the codec to power on defaults */
364 aic26_reg_write(codec, AIC26_REG_RESET, 0xBB00);
366 /* Power up CODEC */
367 aic26_reg_write(codec, AIC26_REG_POWER_CTRL, 0);
369 /* Audio Control 3 (master mode, fsref rate) */
370 reg = aic26_reg_read(codec, AIC26_REG_AUDIO_CTRL3);
371 reg &= ~0xf800;
372 reg |= 0x0800; /* set master mode */
373 aic26_reg_write(codec, AIC26_REG_AUDIO_CTRL3, reg);
375 /* Fill register cache */
376 for (i = 0; i < ARRAY_SIZE(aic26->reg_cache); i++)
377 aic26_reg_read(codec, i);
379 /* Register the sysfs files for debugging */
380 /* Create SysFS files */
381 ret = device_create_file(codec->dev, &dev_attr_keyclick);
382 if (ret)
383 dev_info(codec->dev, "error creating sysfs files\n");
385 /* register controls */
386 dev_dbg(codec->dev, "Registering controls\n");
387 err = snd_soc_add_controls(codec, aic26_snd_controls,
388 ARRAY_SIZE(aic26_snd_controls));
389 WARN_ON(err < 0);
391 return 0;
394 static struct snd_soc_codec_driver aic26_soc_codec_dev = {
395 .probe = aic26_probe,
396 .read = aic26_reg_read,
397 .write = aic26_reg_write,
398 .reg_cache_size = AIC26_NUM_REGS,
399 .reg_word_size = sizeof(u16),
402 /* ---------------------------------------------------------------------
403 * SPI device portion of driver: probe and release routines and SPI
404 * driver registration.
406 static int aic26_spi_probe(struct spi_device *spi)
408 struct aic26 *aic26;
409 int ret;
411 dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n");
413 /* Allocate driver data */
414 aic26 = kzalloc(sizeof *aic26, GFP_KERNEL);
415 if (!aic26)
416 return -ENOMEM;
418 /* Initialize the driver data */
419 aic26->spi = spi;
420 dev_set_drvdata(&spi->dev, aic26);
421 aic26->master = 1;
423 ret = snd_soc_register_codec(&spi->dev,
424 &aic26_soc_codec_dev, &aic26_dai, 1);
425 if (ret < 0)
426 kfree(aic26);
427 return ret;
429 dev_dbg(&spi->dev, "SPI device initialized\n");
430 return 0;
433 static int aic26_spi_remove(struct spi_device *spi)
435 snd_soc_unregister_codec(&spi->dev);
436 kfree(spi_get_drvdata(spi));
437 return 0;
440 static struct spi_driver aic26_spi = {
441 .driver = {
442 .name = "tlv320aic26-codec",
443 .owner = THIS_MODULE,
445 .probe = aic26_spi_probe,
446 .remove = aic26_spi_remove,
449 static int __init aic26_init(void)
451 return spi_register_driver(&aic26_spi);
453 module_init(aic26_init);
455 static void __exit aic26_exit(void)
457 spi_unregister_driver(&aic26_spi);
459 module_exit(aic26_exit);