ASoC: tlv320aic26: Use snd_soc_update_bits()
[deliverable/linux.git] / sound / soc / codecs / tlv320aic26.c
CommitLineData
d8e3bb73
GL
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>
5a0e3ad6 16#include <linux/slab.h>
d8e3bb73
GL
17#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20#include <sound/soc.h>
d8e3bb73
GL
21#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;
f8f11795 32 struct snd_soc_codec *codec;
d8e3bb73
GL
33 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{
d4a8ca24 49 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
d8e3bb73
GL
50 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
d8e3bb73
GL
77static int aic26_reg_write(struct snd_soc_codec *codec, unsigned int reg,
78 unsigned int value)
79{
d4a8ca24 80 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
d8e3bb73
GL
81 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
4a11bc2f
MB
109static 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
d8e3bb73
GL
125/* ---------------------------------------------------------------------
126 * Digital Audio Interface Operations
127 */
128static int aic26_hw_params(struct snd_pcm_substream *substream,
dee89c4d
MB
129 struct snd_pcm_hw_params *params,
130 struct snd_soc_dai *dai)
d8e3bb73 131{
e6968a17 132 struct snd_soc_codec *codec = dai->codec;
d4a8ca24 133 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
d8e3bb73
GL
134 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
2aba76f0
MW
166 /**
167 * Configure PLL
168 * fsref = (mclk * PLLM) / 2048
169 * where PLLM = J.DDDD (DDDD register ranges from 0 to 9999, decimal)
170 */
d8e3bb73 171 pval = 1;
2aba76f0
MW
172 /* 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);
d8e3bb73
GL
178 qval = 0;
179 reg = 0x8000 | qval << 11 | pval << 8 | jval << 2;
12201398 180 snd_soc_write(codec, AIC26_REG_PLL_PROG1, reg);
d8e3bb73 181 reg = dval << 2;
12201398 182 snd_soc_write(codec, AIC26_REG_PLL_PROG2, reg);
d8e3bb73
GL
183
184 /* Audio Control 3 (master mode, fsref rate) */
d8e3bb73 185 if (aic26->master)
5b0959d4 186 reg = 0x0800;
d8e3bb73 187 if (fsref == 48000)
5b0959d4
MB
188 reg = 0x2000;
189 snd_soc_update_bits(codec, AIC26_REG_AUDIO_CTRL3, 0xf800, reg);
d8e3bb73
GL
190
191 /* Audio Control 1 (FSref divisor) */
5b0959d4
MB
192 reg = wlen | aic26->datfm | (divisor << 3) | divisor;
193 snd_soc_update_bits(codec, AIC26_REG_AUDIO_CTRL1, 0xfff, reg);
d8e3bb73
GL
194
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;
d4a8ca24 204 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
5b0959d4 205 u16 reg;
d8e3bb73
GL
206
207 dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n",
208 dai, mute);
209
210 if (mute)
5b0959d4 211 reg = 0x8080;
d8e3bb73 212 else
5b0959d4
MB
213 reg = 0;
214 snd_soc_update_bits(codec, AIC26_REG_DAC_GAIN, 0x8000, reg);
d8e3bb73
GL
215
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;
d4a8ca24 223 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
d8e3bb73
GL
224
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;
d4a8ca24 240 struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
d8e3bb73
GL
241
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
85e7652d 276static const struct snd_soc_dai_ops aic26_dai_ops = {
6335d055
EM
277 .hw_params = aic26_hw_params,
278 .digital_mute = aic26_mute,
279 .set_sysclk = aic26_set_sysclk,
280 .set_fmt = aic26_set_fmt,
281};
282
f0fba2ad
LG
283static struct snd_soc_dai_driver aic26_dai = {
284 .name = "tlv320aic26-hifi",
d8e3bb73
GL
285 .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 },
6335d055 299 .ops = &aic26_dai_ops,
d8e3bb73 300};
d8e3bb73
GL
301
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
d8e3bb73
GL
322/* ---------------------------------------------------------------------
323 * 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
5b0959d4 332 val = snd_soc_read(aic26->codec, AIC26_REG_AUDIO_CTRL2);
d8e3bb73
GL
333 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);
d8e3bb73 346
5b0959d4
MB
347 snd_soc_update_bits(aic26->codec, AIC26_REG_AUDIO_CTRL2,
348 0x8000, 0x800);
d8e3bb73
GL
349
350 return count;
351}
352
9cce39a1 353static DEVICE_ATTR(keyclick, 0644, aic26_keyclick_show, aic26_keyclick_set);
d8e3bb73
GL
354
355/* ---------------------------------------------------------------------
f0fba2ad 356 * SoC CODEC portion of driver: probe and release routines
d8e3bb73 357 */
f0fba2ad 358static int aic26_probe(struct snd_soc_codec *codec)
d8e3bb73 359{
f8f11795 360 struct aic26 *aic26 = dev_get_drvdata(codec->dev);
5b0959d4 361 int ret, reg;
d8e3bb73 362
f8f11795
LPC
363 aic26->codec = codec;
364
d8e3bb73 365 /* Reset the codec to power on defaults */
12201398 366 snd_soc_write(codec, AIC26_REG_RESET, 0xBB00);
d8e3bb73
GL
367
368 /* Power up CODEC */
12201398 369 snd_soc_write(codec, AIC26_REG_POWER_CTRL, 0);
d8e3bb73
GL
370
371 /* Audio Control 3 (master mode, fsref rate) */
12201398 372 reg = snd_soc_read(codec, AIC26_REG_AUDIO_CTRL3);
d8e3bb73
GL
373 reg &= ~0xf800;
374 reg |= 0x0800; /* set master mode */
12201398 375 snd_soc_write(codec, AIC26_REG_AUDIO_CTRL3, reg);
d8e3bb73 376
d8e3bb73
GL
377 /* Register the sysfs files for debugging */
378 /* Create SysFS files */
f0fba2ad 379 ret = device_create_file(codec->dev, &dev_attr_keyclick);
64089b84 380 if (ret)
f0fba2ad 381 dev_info(codec->dev, "error creating sysfs files\n");
d8e3bb73 382
d8e3bb73
GL
383 return 0;
384}
385
f0fba2ad
LG
386static struct snd_soc_codec_driver aic26_soc_codec_dev = {
387 .probe = aic26_probe,
388 .read = aic26_reg_read,
389 .write = aic26_reg_write,
806955dd
MB
390 .controls = aic26_snd_controls,
391 .num_controls = ARRAY_SIZE(aic26_snd_controls),
4a11bc2f
MB
392 .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),
f0fba2ad
LG
396};
397
398/* ---------------------------------------------------------------------
399 * SPI device portion of driver: probe and release routines and SPI
400 * driver registration.
401 */
402static int aic26_spi_probe(struct spi_device *spi)
d8e3bb73 403{
f0fba2ad
LG
404 struct aic26 *aic26;
405 int ret;
d8e3bb73 406
f0fba2ad
LG
407 dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n");
408
409 /* Allocate driver data */
a8163023 410 aic26 = devm_kzalloc(&spi->dev, sizeof *aic26, GFP_KERNEL);
f0fba2ad
LG
411 if (!aic26)
412 return -ENOMEM;
d8e3bb73 413
f0fba2ad
LG
414 /* Initialize the driver data */
415 aic26->spi = spi;
416 dev_set_drvdata(&spi->dev, aic26);
417 aic26->master = 1;
418
419 ret = snd_soc_register_codec(&spi->dev,
420 &aic26_soc_codec_dev, &aic26_dai, 1);
f0fba2ad 421 return ret;
f0fba2ad
LG
422}
423
424static int aic26_spi_remove(struct spi_device *spi)
425{
426 snd_soc_unregister_codec(&spi->dev);
d8e3bb73
GL
427 return 0;
428}
429
430static struct spi_driver aic26_spi = {
431 .driver = {
f0fba2ad 432 .name = "tlv320aic26-codec",
d8e3bb73
GL
433 .owner = THIS_MODULE,
434 },
435 .probe = aic26_spi_probe,
436 .remove = aic26_spi_remove,
437};
438
9bb280a2 439module_spi_driver(aic26_spi);
This page took 0.232993 seconds and 5 git commands to generate.