blob: 4d8244750f233fe1c90c0387fe6f68c8cfc80052 [file] [log] [blame]
Grant Likelyd8e3bb72008-07-29 11:42:31 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Grant Likelyd8e3bb72008-07-29 11:42:31 +010017#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20#include <sound/soc.h>
Grant Likelyd8e3bb72008-07-29 11:42:31 +010021#include <sound/initval.h>
22
23#include "tlv320aic26.h"
24
25MODULE_DESCRIPTION("ASoC TLV320AIC26 codec driver");
26MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
27MODULE_LICENSE("GPL");
28
29/* AIC26 driver private data */
30struct aic26 {
31 struct spi_device *spi;
Lars-Peter Clausenf8f11792013-08-06 13:39:29 +020032 struct snd_soc_codec *codec;
Grant Likelyd8e3bb72008-07-29 11:42:31 +010033 int master;
34 int datfm;
35 int mclk;
36
37 /* Keyclick parameters */
38 int keyclick_amplitude;
39 int keyclick_freq;
40 int keyclick_len;
41};
42
43/* ---------------------------------------------------------------------
44 * Register access routines
45 */
46static unsigned int aic26_reg_read(struct snd_soc_codec *codec,
47 unsigned int reg)
48{
Takashi Iwaid4a8ca22010-04-20 08:20:31 +020049 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
Grant Likelyd8e3bb72008-07-29 11:42:31 +010050 u16 *cache = codec->reg_cache;
51 u16 cmd, value;
52 u8 buffer[2];
53 int rc;
54
55 if (reg >= AIC26_NUM_REGS) {
56 WARN_ON_ONCE(1);
57 return 0;
58 }
59
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);
66 if (rc) {
67 dev_err(&aic26->spi->dev, "AIC26 reg read error\n");
68 return -EIO;
69 }
70 value = (buffer[0] << 8) | buffer[1];
71
72 /* Update the cache before returning with the value */
73 cache[reg] = value;
74 return value;
75}
76
Grant Likelyd8e3bb72008-07-29 11:42:31 +010077static int aic26_reg_write(struct snd_soc_codec *codec, unsigned int reg,
78 unsigned int value)
79{
Takashi Iwaid4a8ca22010-04-20 08:20:31 +020080 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
Grant Likelyd8e3bb72008-07-29 11:42:31 +010081 u16 *cache = codec->reg_cache;
82 u16 cmd;
83 u8 buffer[4];
84 int rc;
85
86 if (reg >= AIC26_NUM_REGS) {
87 WARN_ON_ONCE(1);
88 return -EINVAL;
89 }
90
91 /* Do SPI transfer; first 16bits are command; remaining is data
92 * to write into register */
93 cmd = AIC26_WRITE_COMMAND_WORD(reg);
94 buffer[0] = (cmd >> 8) & 0xff;
95 buffer[1] = cmd & 0xff;
96 buffer[2] = value >> 8;
97 buffer[3] = value;
98 rc = spi_write(aic26->spi, buffer, 4);
99 if (rc) {
100 dev_err(&aic26->spi->dev, "AIC26 reg read error\n");
101 return -EIO;
102 }
103
104 /* update cache before returning */
105 cache[reg] = value;
106 return 0;
107}
108
Mark Brown4a11bc22013-08-16 12:24:47 +0100109static const struct snd_soc_dapm_widget tlv320aic26_dapm_widgets[] = {
110SND_SOC_DAPM_INPUT("MICIN"),
111SND_SOC_DAPM_INPUT("AUX"),
112
113SND_SOC_DAPM_OUTPUT("HPL"),
114SND_SOC_DAPM_OUTPUT("HPR"),
115};
116
117static const struct snd_soc_dapm_route tlv320aic26_dapm_routes[] = {
118 { "Capture", NULL, "MICIN" },
119 { "Capture", NULL, "AUX" },
120
121 { "HPL", NULL, "Playback" },
122 { "HPR", NULL, "Playback" },
123};
124
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100125/* ---------------------------------------------------------------------
126 * Digital Audio Interface Operations
127 */
128static int aic26_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000129 struct snd_pcm_hw_params *params,
130 struct snd_soc_dai *dai)
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100131{
Mark Browne6968a12012-04-04 15:58:16 +0100132 struct snd_soc_codec *codec = dai->codec;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +0200133 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100134 int fsref, divisor, wlen, pval, jval, dval, qval;
135 u16 reg;
136
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));
141
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;
154 }
155
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;
164 }
165
Michael Williamson2aba76f2011-05-20 10:26:06 -0400166 /**
167 * Configure PLL
168 * fsref = (mclk * PLLM) / 2048
169 * where PLLM = J.DDDD (DDDD register ranges from 0 to 9999, decimal)
170 */
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100171 pval = 1;
Michael Williamson2aba76f2011-05-20 10:26:06 -0400172 /* compute J portion of multiplier */
173 jval = fsref / (aic26->mclk / 2048);
174 /* compute fractional DDDD component of multiplier */
175 dval = fsref - (jval * (aic26->mclk / 2048));
176 dval = (10000 * dval) / (aic26->mclk / 2048);
177 dev_dbg(&aic26->spi->dev, "Setting PLLM to %d.%04d\n", jval, dval);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100178 qval = 0;
179 reg = 0x8000 | qval << 11 | pval << 8 | jval << 2;
Mark Brown12201392013-08-16 12:13:50 +0100180 snd_soc_write(codec, AIC26_REG_PLL_PROG1, reg);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100181 reg = dval << 2;
Mark Brown12201392013-08-16 12:13:50 +0100182 snd_soc_write(codec, AIC26_REG_PLL_PROG2, reg);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100183
184 /* Audio Control 3 (master mode, fsref rate) */
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100185 if (aic26->master)
Mark Brown5b0959d2013-09-25 13:14:41 +0100186 reg = 0x0800;
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100187 if (fsref == 48000)
Mark Brown5b0959d2013-09-25 13:14:41 +0100188 reg = 0x2000;
189 snd_soc_update_bits(codec, AIC26_REG_AUDIO_CTRL3, 0xf800, reg);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100190
191 /* Audio Control 1 (FSref divisor) */
Mark Brown5b0959d2013-09-25 13:14:41 +0100192 reg = wlen | aic26->datfm | (divisor << 3) | divisor;
193 snd_soc_update_bits(codec, AIC26_REG_AUDIO_CTRL1, 0xfff, reg);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100194
195 return 0;
196}
197
198/**
199 * aic26_mute - Mute control to reduce noise when changing audio format
200 */
201static int aic26_mute(struct snd_soc_dai *dai, int mute)
202{
203 struct snd_soc_codec *codec = dai->codec;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +0200204 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
Mark Brown5b0959d2013-09-25 13:14:41 +0100205 u16 reg;
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100206
207 dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n",
208 dai, mute);
209
210 if (mute)
Mark Brown5b0959d2013-09-25 13:14:41 +0100211 reg = 0x8080;
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100212 else
Mark Brown5b0959d2013-09-25 13:14:41 +0100213 reg = 0;
214 snd_soc_update_bits(codec, AIC26_REG_DAC_GAIN, 0x8000, reg);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100215
216 return 0;
217}
218
219static int aic26_set_sysclk(struct snd_soc_dai *codec_dai,
220 int clk_id, unsigned int freq, int dir)
221{
222 struct snd_soc_codec *codec = codec_dai->codec;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +0200223 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100224
225 dev_dbg(&aic26->spi->dev, "aic26_set_sysclk(dai=%p, clk_id==%i,"
226 " freq=%i, dir=%i)\n",
227 codec_dai, clk_id, freq, dir);
228
229 /* MCLK needs to fall between 2MHz and 50 MHz */
230 if ((freq < 2000000) || (freq > 50000000))
231 return -EINVAL;
232
233 aic26->mclk = freq;
234 return 0;
235}
236
237static int aic26_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
238{
239 struct snd_soc_codec *codec = codec_dai->codec;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +0200240 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100241
242 dev_dbg(&aic26->spi->dev, "aic26_set_fmt(dai=%p, fmt==%i)\n",
243 codec_dai, fmt);
244
245 /* set master/slave audio interface */
246 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
247 case SND_SOC_DAIFMT_CBM_CFM: aic26->master = 1; break;
248 case SND_SOC_DAIFMT_CBS_CFS: aic26->master = 0; break;
249 default:
250 dev_dbg(&aic26->spi->dev, "bad master\n"); return -EINVAL;
251 }
252
253 /* interface format */
254 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
255 case SND_SOC_DAIFMT_I2S: aic26->datfm = AIC26_DATFM_I2S; break;
256 case SND_SOC_DAIFMT_DSP_A: aic26->datfm = AIC26_DATFM_DSP; break;
257 case SND_SOC_DAIFMT_RIGHT_J: aic26->datfm = AIC26_DATFM_RIGHTJ; break;
258 case SND_SOC_DAIFMT_LEFT_J: aic26->datfm = AIC26_DATFM_LEFTJ; break;
259 default:
260 dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
261 }
262
263 return 0;
264}
265
266/* ---------------------------------------------------------------------
267 * Digital Audio Interface Definition
268 */
269#define AIC26_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
270 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
271 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
272 SNDRV_PCM_RATE_48000)
273#define AIC26_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |\
274 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
275
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100276static const struct snd_soc_dai_ops aic26_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800277 .hw_params = aic26_hw_params,
278 .digital_mute = aic26_mute,
279 .set_sysclk = aic26_set_sysclk,
280 .set_fmt = aic26_set_fmt,
281};
282
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000283static struct snd_soc_dai_driver aic26_dai = {
284 .name = "tlv320aic26-hifi",
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100285 .playback = {
286 .stream_name = "Playback",
287 .channels_min = 2,
288 .channels_max = 2,
289 .rates = AIC26_RATES,
290 .formats = AIC26_FORMATS,
291 },
292 .capture = {
293 .stream_name = "Capture",
294 .channels_min = 2,
295 .channels_max = 2,
296 .rates = AIC26_RATES,
297 .formats = AIC26_FORMATS,
298 },
Eric Miao6335d052009-03-03 09:41:00 +0800299 .ops = &aic26_dai_ops,
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100300};
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100301
302/* ---------------------------------------------------------------------
303 * ALSA controls
304 */
305static const char *aic26_capture_src_text[] = {"Mic", "Aux"};
306static const struct soc_enum aic26_capture_src_enum =
307 SOC_ENUM_SINGLE(AIC26_REG_AUDIO_CTRL1, 12, 2, aic26_capture_src_text);
308
309static const struct snd_kcontrol_new aic26_snd_controls[] = {
310 /* Output */
311 SOC_DOUBLE("PCM Playback Volume", AIC26_REG_DAC_GAIN, 8, 0, 0x7f, 1),
312 SOC_DOUBLE("PCM Playback Switch", AIC26_REG_DAC_GAIN, 15, 7, 1, 1),
313 SOC_SINGLE("PCM Capture Volume", AIC26_REG_ADC_GAIN, 8, 0x7f, 0),
314 SOC_SINGLE("PCM Capture Mute", AIC26_REG_ADC_GAIN, 15, 1, 1),
315 SOC_SINGLE("Keyclick activate", AIC26_REG_AUDIO_CTRL2, 15, 0x1, 0),
316 SOC_SINGLE("Keyclick amplitude", AIC26_REG_AUDIO_CTRL2, 12, 0x7, 0),
317 SOC_SINGLE("Keyclick frequency", AIC26_REG_AUDIO_CTRL2, 8, 0x7, 0),
318 SOC_SINGLE("Keyclick period", AIC26_REG_AUDIO_CTRL2, 4, 0xf, 0),
319 SOC_ENUM("Capture Source", aic26_capture_src_enum),
320};
321
322/* ---------------------------------------------------------------------
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100323 * SPI device portion of driver: sysfs files for debugging
324 */
325
326static ssize_t aic26_keyclick_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329 struct aic26 *aic26 = dev_get_drvdata(dev);
330 int val, amp, freq, len;
331
Mark Brown5b0959d2013-09-25 13:14:41 +0100332 val = snd_soc_read(aic26->codec, AIC26_REG_AUDIO_CTRL2);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100333 amp = (val >> 12) & 0x7;
334 freq = (125 << ((val >> 8) & 0x7)) >> 1;
335 len = 2 * (1 + ((val >> 4) & 0xf));
336
337 return sprintf(buf, "amp=%x freq=%iHz len=%iclks\n", amp, freq, len);
338}
339
340/* Any write to the keyclick attribute will trigger the keyclick event */
341static ssize_t aic26_keyclick_set(struct device *dev,
342 struct device_attribute *attr,
343 const char *buf, size_t count)
344{
345 struct aic26 *aic26 = dev_get_drvdata(dev);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100346
Mark Brown5b0959d2013-09-25 13:14:41 +0100347 snd_soc_update_bits(aic26->codec, AIC26_REG_AUDIO_CTRL2,
348 0x8000, 0x800);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100349
350 return count;
351}
352
Mark Brown9cce39a2008-07-29 11:42:33 +0100353static DEVICE_ATTR(keyclick, 0644, aic26_keyclick_show, aic26_keyclick_set);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100354
355/* ---------------------------------------------------------------------
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000356 * SoC CODEC portion of driver: probe and release routines
357 */
358static int aic26_probe(struct snd_soc_codec *codec)
359{
Lars-Peter Clausenf8f11792013-08-06 13:39:29 +0200360 struct aic26 *aic26 = dev_get_drvdata(codec->dev);
Mark Brown5b0959d2013-09-25 13:14:41 +0100361 int ret, reg;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000362
Lars-Peter Clausenf8f11792013-08-06 13:39:29 +0200363 aic26->codec = codec;
364
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000365 /* Reset the codec to power on defaults */
Mark Brown12201392013-08-16 12:13:50 +0100366 snd_soc_write(codec, AIC26_REG_RESET, 0xBB00);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000367
368 /* Power up CODEC */
Mark Brown12201392013-08-16 12:13:50 +0100369 snd_soc_write(codec, AIC26_REG_POWER_CTRL, 0);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000370
371 /* Audio Control 3 (master mode, fsref rate) */
Mark Brown12201392013-08-16 12:13:50 +0100372 reg = snd_soc_read(codec, AIC26_REG_AUDIO_CTRL3);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000373 reg &= ~0xf800;
374 reg |= 0x0800; /* set master mode */
Mark Brown12201392013-08-16 12:13:50 +0100375 snd_soc_write(codec, AIC26_REG_AUDIO_CTRL3, reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000376
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000377 /* Register the sysfs files for debugging */
378 /* Create SysFS files */
379 ret = device_create_file(codec->dev, &dev_attr_keyclick);
380 if (ret)
381 dev_info(codec->dev, "error creating sysfs files\n");
382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000383 return 0;
384}
385
386static struct snd_soc_codec_driver aic26_soc_codec_dev = {
387 .probe = aic26_probe,
388 .read = aic26_reg_read,
389 .write = aic26_reg_write,
Mark Brown806955d2013-09-25 13:10:33 +0100390 .controls = aic26_snd_controls,
391 .num_controls = ARRAY_SIZE(aic26_snd_controls),
Mark Brown4a11bc22013-08-16 12:24:47 +0100392 .dapm_widgets = tlv320aic26_dapm_widgets,
393 .num_dapm_widgets = ARRAY_SIZE(tlv320aic26_dapm_widgets),
394 .dapm_routes = tlv320aic26_dapm_routes,
395 .num_dapm_routes = ARRAY_SIZE(tlv320aic26_dapm_routes),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000396};
397
398/* ---------------------------------------------------------------------
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100399 * SPI device portion of driver: probe and release routines and SPI
400 * driver registration.
401 */
402static int aic26_spi_probe(struct spi_device *spi)
403{
404 struct aic26 *aic26;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000405 int ret;
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100406
407 dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n");
408
409 /* Allocate driver data */
Axel Lina8163022011-12-29 12:08:36 +0800410 aic26 = devm_kzalloc(&spi->dev, sizeof *aic26, GFP_KERNEL);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100411 if (!aic26)
412 return -ENOMEM;
413
414 /* Initialize the driver data */
415 aic26->spi = spi;
416 dev_set_drvdata(&spi->dev, aic26);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100417 aic26->master = 1;
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100418
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000419 ret = snd_soc_register_codec(&spi->dev,
420 &aic26_soc_codec_dev, &aic26_dai, 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000421 return ret;
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100422}
423
424static int aic26_spi_remove(struct spi_device *spi)
425{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000426 snd_soc_unregister_codec(&spi->dev);
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100427 return 0;
428}
429
430static struct spi_driver aic26_spi = {
431 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000432 .name = "tlv320aic26-codec",
Grant Likelyd8e3bb72008-07-29 11:42:31 +0100433 .owner = THIS_MODULE,
434 },
435 .probe = aic26_spi_probe,
436 .remove = aic26_spi_remove,
437};
438
Sachin Kamat9bb280a2012-08-27 17:00:26 +0530439module_spi_driver(aic26_spi);