PageRenderTime 136ms CodeModel.GetById 15ms app.highlight 78ms RepoModel.GetById 1ms app.codeStats 0ms

/sound/soc/codecs/tlv320aic26.c

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