tlv320aic26.c 15 KB

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