2 * Texas Instruments TLV320AIC26 low power audio CODEC
3 * ALSA SoC CODEC driver
5 * Copyright (C) 2008 Secret Lab Technologies Ltd.
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/init.h>
11 #include <linux/delay.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/initval.h>
23 #include "tlv320aic26.h"
25 MODULE_DESCRIPTION("ASoC TLV320AIC26 codec driver");
26 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
27 MODULE_LICENSE("GPL");
29 /* AIC26 driver private data */
31 struct spi_device
*spi
;
32 struct snd_soc_codec codec
;
37 /* Keyclick parameters */
38 int keyclick_amplitude
;
43 /* ---------------------------------------------------------------------
44 * Register access routines
46 static unsigned int aic26_reg_read(struct snd_soc_codec
*codec
,
49 struct aic26
*aic26
= snd_soc_codec_get_drvdata(codec
);
50 u16
*cache
= codec
->reg_cache
;
55 if (reg
>= AIC26_NUM_REGS
) {
60 /* Do SPI transfer; first 16bits are command; remaining is
61 * register contents */
62 cmd
= AIC26_READ_COMMAND_WORD(reg
);
63 buffer
[0] = (cmd
>> 8) & 0xff;
64 buffer
[1] = cmd
& 0xff;
65 rc
= spi_write_then_read(aic26
->spi
, buffer
, 2, buffer
, 2);
67 dev_err(&aic26
->spi
->dev
, "AIC26 reg read error\n");
70 value
= (buffer
[0] << 8) | buffer
[1];
72 /* Update the cache before returning with the value */
77 static unsigned int aic26_reg_read_cache(struct snd_soc_codec
*codec
,
80 u16
*cache
= codec
->reg_cache
;
82 if (reg
>= AIC26_NUM_REGS
) {
90 static int aic26_reg_write(struct snd_soc_codec
*codec
, unsigned int reg
,
93 struct aic26
*aic26
= snd_soc_codec_get_drvdata(codec
);
94 u16
*cache
= codec
->reg_cache
;
99 if (reg
>= AIC26_NUM_REGS
) {
104 /* Do SPI transfer; first 16bits are command; remaining is data
105 * to write into register */
106 cmd
= AIC26_WRITE_COMMAND_WORD(reg
);
107 buffer
[0] = (cmd
>> 8) & 0xff;
108 buffer
[1] = cmd
& 0xff;
109 buffer
[2] = value
>> 8;
111 rc
= spi_write(aic26
->spi
, buffer
, 4);
113 dev_err(&aic26
->spi
->dev
, "AIC26 reg read error\n");
117 /* update cache before returning */
122 /* ---------------------------------------------------------------------
123 * Digital Audio Interface Operations
125 static int aic26_hw_params(struct snd_pcm_substream
*substream
,
126 struct snd_pcm_hw_params
*params
,
127 struct snd_soc_dai
*dai
)
129 struct snd_soc_codec
*codec
= dai
->codec
;
130 struct aic26
*aic26
= snd_soc_codec_get_drvdata(codec
);
131 int fsref
, divisor
, wlen
, pval
, jval
, dval
, qval
;
134 dev_dbg(&aic26
->spi
->dev
, "aic26_hw_params(substream=%p, params=%p)\n",
136 dev_dbg(&aic26
->spi
->dev
, "rate=%i format=%i\n", params_rate(params
),
137 params_format(params
));
139 switch (params_rate(params
)) {
140 case 8000: fsref
= 48000; divisor
= AIC26_DIV_6
; break;
141 case 11025: fsref
= 44100; divisor
= AIC26_DIV_4
; break;
142 case 12000: fsref
= 48000; divisor
= AIC26_DIV_4
; break;
143 case 16000: fsref
= 48000; divisor
= AIC26_DIV_3
; break;
144 case 22050: fsref
= 44100; divisor
= AIC26_DIV_2
; break;
145 case 24000: fsref
= 48000; divisor
= AIC26_DIV_2
; break;
146 case 32000: fsref
= 48000; divisor
= AIC26_DIV_1_5
; break;
147 case 44100: fsref
= 44100; divisor
= AIC26_DIV_1
; break;
148 case 48000: fsref
= 48000; divisor
= AIC26_DIV_1
; break;
150 dev_dbg(&aic26
->spi
->dev
, "bad rate\n"); return -EINVAL
;
153 /* select data word length */
154 switch (params_format(params
)) {
155 case SNDRV_PCM_FORMAT_S8
: wlen
= AIC26_WLEN_16
; break;
156 case SNDRV_PCM_FORMAT_S16_BE
: wlen
= AIC26_WLEN_16
; break;
157 case SNDRV_PCM_FORMAT_S24_BE
: wlen
= AIC26_WLEN_24
; break;
158 case SNDRV_PCM_FORMAT_S32_BE
: wlen
= AIC26_WLEN_32
; break;
160 dev_dbg(&aic26
->spi
->dev
, "bad format\n"); return -EINVAL
;
165 * fsref = (mclk * PLLM) / 2048
166 * where PLLM = J.DDDD (DDDD register ranges from 0 to 9999, decimal)
169 /* compute J portion of multiplier */
170 jval
= fsref
/ (aic26
->mclk
/ 2048);
171 /* compute fractional DDDD component of multiplier */
172 dval
= fsref
- (jval
* (aic26
->mclk
/ 2048));
173 dval
= (10000 * dval
) / (aic26
->mclk
/ 2048);
174 dev_dbg(&aic26
->spi
->dev
, "Setting PLLM to %d.%04d\n", jval
, dval
);
176 reg
= 0x8000 | qval
<< 11 | pval
<< 8 | jval
<< 2;
177 aic26_reg_write(codec
, AIC26_REG_PLL_PROG1
, reg
);
179 aic26_reg_write(codec
, AIC26_REG_PLL_PROG2
, reg
);
181 /* Audio Control 3 (master mode, fsref rate) */
182 reg
= aic26_reg_read_cache(codec
, AIC26_REG_AUDIO_CTRL3
);
188 aic26_reg_write(codec
, AIC26_REG_AUDIO_CTRL3
, reg
);
190 /* Audio Control 1 (FSref divisor) */
191 reg
= aic26_reg_read_cache(codec
, AIC26_REG_AUDIO_CTRL1
);
193 reg
|= wlen
| aic26
->datfm
| (divisor
<< 3) | divisor
;
194 aic26_reg_write(codec
, AIC26_REG_AUDIO_CTRL1
, reg
);
200 * aic26_mute - Mute control to reduce noise when changing audio format
202 static int aic26_mute(struct snd_soc_dai
*dai
, int mute
)
204 struct snd_soc_codec
*codec
= dai
->codec
;
205 struct aic26
*aic26
= snd_soc_codec_get_drvdata(codec
);
206 u16 reg
= aic26_reg_read_cache(codec
, AIC26_REG_DAC_GAIN
);
208 dev_dbg(&aic26
->spi
->dev
, "aic26_mute(dai=%p, mute=%i)\n",
215 aic26_reg_write(codec
, AIC26_REG_DAC_GAIN
, reg
);
220 static int aic26_set_sysclk(struct snd_soc_dai
*codec_dai
,
221 int clk_id
, unsigned int freq
, int dir
)
223 struct snd_soc_codec
*codec
= codec_dai
->codec
;
224 struct aic26
*aic26
= snd_soc_codec_get_drvdata(codec
);
226 dev_dbg(&aic26
->spi
->dev
, "aic26_set_sysclk(dai=%p, clk_id==%i,"
227 " freq=%i, dir=%i)\n",
228 codec_dai
, clk_id
, freq
, dir
);
230 /* MCLK needs to fall between 2MHz and 50 MHz */
231 if ((freq
< 2000000) || (freq
> 50000000))
238 static int aic26_set_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
240 struct snd_soc_codec
*codec
= codec_dai
->codec
;
241 struct aic26
*aic26
= snd_soc_codec_get_drvdata(codec
);
243 dev_dbg(&aic26
->spi
->dev
, "aic26_set_fmt(dai=%p, fmt==%i)\n",
246 /* set master/slave audio interface */
247 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
248 case SND_SOC_DAIFMT_CBM_CFM
: aic26
->master
= 1; break;
249 case SND_SOC_DAIFMT_CBS_CFS
: aic26
->master
= 0; break;
251 dev_dbg(&aic26
->spi
->dev
, "bad master\n"); return -EINVAL
;
254 /* interface format */
255 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
256 case SND_SOC_DAIFMT_I2S
: aic26
->datfm
= AIC26_DATFM_I2S
; break;
257 case SND_SOC_DAIFMT_DSP_A
: aic26
->datfm
= AIC26_DATFM_DSP
; break;
258 case SND_SOC_DAIFMT_RIGHT_J
: aic26
->datfm
= AIC26_DATFM_RIGHTJ
; break;
259 case SND_SOC_DAIFMT_LEFT_J
: aic26
->datfm
= AIC26_DATFM_LEFTJ
; break;
261 dev_dbg(&aic26
->spi
->dev
, "bad format\n"); return -EINVAL
;
267 /* ---------------------------------------------------------------------
268 * Digital Audio Interface Definition
270 #define AIC26_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
271 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
272 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
273 SNDRV_PCM_RATE_48000)
274 #define AIC26_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |\
275 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
277 static const struct snd_soc_dai_ops aic26_dai_ops
= {
278 .hw_params
= aic26_hw_params
,
279 .digital_mute
= aic26_mute
,
280 .set_sysclk
= aic26_set_sysclk
,
281 .set_fmt
= aic26_set_fmt
,
284 static struct snd_soc_dai_driver aic26_dai
= {
285 .name
= "tlv320aic26-hifi",
287 .stream_name
= "Playback",
290 .rates
= AIC26_RATES
,
291 .formats
= AIC26_FORMATS
,
294 .stream_name
= "Capture",
297 .rates
= AIC26_RATES
,
298 .formats
= AIC26_FORMATS
,
300 .ops
= &aic26_dai_ops
,
303 /* ---------------------------------------------------------------------
306 static const char *aic26_capture_src_text
[] = {"Mic", "Aux"};
307 static const struct soc_enum aic26_capture_src_enum
=
308 SOC_ENUM_SINGLE(AIC26_REG_AUDIO_CTRL1
, 12, 2, aic26_capture_src_text
);
310 static const struct snd_kcontrol_new aic26_snd_controls
[] = {
312 SOC_DOUBLE("PCM Playback Volume", AIC26_REG_DAC_GAIN
, 8, 0, 0x7f, 1),
313 SOC_DOUBLE("PCM Playback Switch", AIC26_REG_DAC_GAIN
, 15, 7, 1, 1),
314 SOC_SINGLE("PCM Capture Volume", AIC26_REG_ADC_GAIN
, 8, 0x7f, 0),
315 SOC_SINGLE("PCM Capture Mute", AIC26_REG_ADC_GAIN
, 15, 1, 1),
316 SOC_SINGLE("Keyclick activate", AIC26_REG_AUDIO_CTRL2
, 15, 0x1, 0),
317 SOC_SINGLE("Keyclick amplitude", AIC26_REG_AUDIO_CTRL2
, 12, 0x7, 0),
318 SOC_SINGLE("Keyclick frequency", AIC26_REG_AUDIO_CTRL2
, 8, 0x7, 0),
319 SOC_SINGLE("Keyclick period", AIC26_REG_AUDIO_CTRL2
, 4, 0xf, 0),
320 SOC_ENUM("Capture Source", aic26_capture_src_enum
),
323 /* ---------------------------------------------------------------------
324 * SPI device portion of driver: sysfs files for debugging
327 static ssize_t
aic26_keyclick_show(struct device
*dev
,
328 struct device_attribute
*attr
, char *buf
)
330 struct aic26
*aic26
= dev_get_drvdata(dev
);
331 int val
, amp
, freq
, len
;
333 val
= aic26_reg_read_cache(&aic26
->codec
, AIC26_REG_AUDIO_CTRL2
);
334 amp
= (val
>> 12) & 0x7;
335 freq
= (125 << ((val
>> 8) & 0x7)) >> 1;
336 len
= 2 * (1 + ((val
>> 4) & 0xf));
338 return sprintf(buf
, "amp=%x freq=%iHz len=%iclks\n", amp
, freq
, len
);
341 /* Any write to the keyclick attribute will trigger the keyclick event */
342 static ssize_t
aic26_keyclick_set(struct device
*dev
,
343 struct device_attribute
*attr
,
344 const char *buf
, size_t count
)
346 struct aic26
*aic26
= dev_get_drvdata(dev
);
349 val
= aic26_reg_read_cache(&aic26
->codec
, AIC26_REG_AUDIO_CTRL2
);
351 aic26_reg_write(&aic26
->codec
, AIC26_REG_AUDIO_CTRL2
, val
);
356 static DEVICE_ATTR(keyclick
, 0644, aic26_keyclick_show
, aic26_keyclick_set
);
358 /* ---------------------------------------------------------------------
359 * SoC CODEC portion of driver: probe and release routines
361 static int aic26_probe(struct snd_soc_codec
*codec
)
363 int ret
, err
, i
, reg
;
365 dev_info(codec
->dev
, "Probing AIC26 SoC CODEC driver\n");
367 /* Reset the codec to power on defaults */
368 aic26_reg_write(codec
, AIC26_REG_RESET
, 0xBB00);
371 aic26_reg_write(codec
, AIC26_REG_POWER_CTRL
, 0);
373 /* Audio Control 3 (master mode, fsref rate) */
374 reg
= aic26_reg_read(codec
, AIC26_REG_AUDIO_CTRL3
);
376 reg
|= 0x0800; /* set master mode */
377 aic26_reg_write(codec
, AIC26_REG_AUDIO_CTRL3
, reg
);
379 /* Fill register cache */
380 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
++)
381 aic26_reg_read(codec
, i
);
383 /* Register the sysfs files for debugging */
384 /* Create SysFS files */
385 ret
= device_create_file(codec
->dev
, &dev_attr_keyclick
);
387 dev_info(codec
->dev
, "error creating sysfs files\n");
389 /* register controls */
390 dev_dbg(codec
->dev
, "Registering controls\n");
391 err
= snd_soc_add_codec_controls(codec
, aic26_snd_controls
,
392 ARRAY_SIZE(aic26_snd_controls
));
398 static struct snd_soc_codec_driver aic26_soc_codec_dev
= {
399 .probe
= aic26_probe
,
400 .read
= aic26_reg_read
,
401 .write
= aic26_reg_write
,
402 .reg_cache_size
= AIC26_NUM_REGS
,
403 .reg_word_size
= sizeof(u16
),
406 /* ---------------------------------------------------------------------
407 * SPI device portion of driver: probe and release routines and SPI
408 * driver registration.
410 static int aic26_spi_probe(struct spi_device
*spi
)
415 dev_dbg(&spi
->dev
, "probing tlv320aic26 spi device\n");
417 /* Allocate driver data */
418 aic26
= devm_kzalloc(&spi
->dev
, sizeof *aic26
, GFP_KERNEL
);
422 /* Initialize the driver data */
424 dev_set_drvdata(&spi
->dev
, aic26
);
427 ret
= snd_soc_register_codec(&spi
->dev
,
428 &aic26_soc_codec_dev
, &aic26_dai
, 1);
432 static int aic26_spi_remove(struct spi_device
*spi
)
434 snd_soc_unregister_codec(&spi
->dev
);
438 static struct spi_driver aic26_spi
= {
440 .name
= "tlv320aic26-codec",
441 .owner
= THIS_MODULE
,
443 .probe
= aic26_spi_probe
,
444 .remove
= aic26_spi_remove
,
447 module_spi_driver(aic26_spi
);