tlv320aic26.c 14 KB

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