tlv320aic26.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. {
  113. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  114. struct snd_soc_device *socdev = rtd->socdev;
  115. struct snd_soc_codec *codec = socdev->codec;
  116. struct aic26 *aic26 = codec->private_data;
  117. int fsref, divisor, wlen, pval, jval, dval, qval;
  118. u16 reg;
  119. dev_dbg(&aic26->spi->dev, "aic26_hw_params(substream=%p, params=%p)\n",
  120. substream, params);
  121. dev_dbg(&aic26->spi->dev, "rate=%i format=%i\n", params_rate(params),
  122. params_format(params));
  123. switch (params_rate(params)) {
  124. case 8000: fsref = 48000; divisor = AIC26_DIV_6; break;
  125. case 11025: fsref = 44100; divisor = AIC26_DIV_4; break;
  126. case 12000: fsref = 48000; divisor = AIC26_DIV_4; break;
  127. case 16000: fsref = 48000; divisor = AIC26_DIV_3; break;
  128. case 22050: fsref = 44100; divisor = AIC26_DIV_2; break;
  129. case 24000: fsref = 48000; divisor = AIC26_DIV_2; break;
  130. case 32000: fsref = 48000; divisor = AIC26_DIV_1_5; break;
  131. case 44100: fsref = 44100; divisor = AIC26_DIV_1; break;
  132. case 48000: fsref = 48000; divisor = AIC26_DIV_1; break;
  133. default:
  134. dev_dbg(&aic26->spi->dev, "bad rate\n"); return -EINVAL;
  135. }
  136. /* select data word length */
  137. switch (params_format(params)) {
  138. case SNDRV_PCM_FORMAT_S8: wlen = AIC26_WLEN_16; break;
  139. case SNDRV_PCM_FORMAT_S16_BE: wlen = AIC26_WLEN_16; break;
  140. case SNDRV_PCM_FORMAT_S24_BE: wlen = AIC26_WLEN_24; break;
  141. case SNDRV_PCM_FORMAT_S32_BE: wlen = AIC26_WLEN_32; break;
  142. default:
  143. dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
  144. }
  145. /* Configure PLL */
  146. pval = 1;
  147. jval = (fsref == 44100) ? 7 : 8;
  148. dval = (fsref == 44100) ? 5264 : 1920;
  149. qval = 0;
  150. reg = 0x8000 | qval << 11 | pval << 8 | jval << 2;
  151. aic26_reg_write(codec, AIC26_REG_PLL_PROG1, reg);
  152. reg = dval << 2;
  153. aic26_reg_write(codec, AIC26_REG_PLL_PROG2, reg);
  154. /* Audio Control 3 (master mode, fsref rate) */
  155. reg = aic26_reg_read_cache(codec, AIC26_REG_AUDIO_CTRL3);
  156. reg &= ~0xf800;
  157. if (aic26->master)
  158. reg |= 0x0800;
  159. if (fsref == 48000)
  160. reg |= 0x2000;
  161. aic26_reg_write(codec, AIC26_REG_AUDIO_CTRL3, reg);
  162. /* Audio Control 1 (FSref divisor) */
  163. reg = aic26_reg_read_cache(codec, AIC26_REG_AUDIO_CTRL1);
  164. reg &= ~0x0fff;
  165. reg |= wlen | aic26->datfm | (divisor << 3) | divisor;
  166. aic26_reg_write(codec, AIC26_REG_AUDIO_CTRL1, reg);
  167. return 0;
  168. }
  169. /**
  170. * aic26_mute - Mute control to reduce noise when changing audio format
  171. */
  172. static int aic26_mute(struct snd_soc_dai *dai, int mute)
  173. {
  174. struct snd_soc_codec *codec = dai->codec;
  175. struct aic26 *aic26 = codec->private_data;
  176. u16 reg = aic26_reg_read_cache(codec, AIC26_REG_DAC_GAIN);
  177. dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n",
  178. dai, mute);
  179. if (mute)
  180. reg |= 0x8080;
  181. else
  182. reg &= ~0x8080;
  183. aic26_reg_write(codec, AIC26_REG_DAC_GAIN, reg);
  184. return 0;
  185. }
  186. static int aic26_set_sysclk(struct snd_soc_dai *codec_dai,
  187. int clk_id, unsigned int freq, int dir)
  188. {
  189. struct snd_soc_codec *codec = codec_dai->codec;
  190. struct aic26 *aic26 = codec->private_data;
  191. dev_dbg(&aic26->spi->dev, "aic26_set_sysclk(dai=%p, clk_id==%i,"
  192. " freq=%i, dir=%i)\n",
  193. codec_dai, clk_id, freq, dir);
  194. /* MCLK needs to fall between 2MHz and 50 MHz */
  195. if ((freq < 2000000) || (freq > 50000000))
  196. return -EINVAL;
  197. aic26->mclk = freq;
  198. return 0;
  199. }
  200. static int aic26_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  201. {
  202. struct snd_soc_codec *codec = codec_dai->codec;
  203. struct aic26 *aic26 = codec->private_data;
  204. dev_dbg(&aic26->spi->dev, "aic26_set_fmt(dai=%p, fmt==%i)\n",
  205. codec_dai, fmt);
  206. /* set master/slave audio interface */
  207. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  208. case SND_SOC_DAIFMT_CBM_CFM: aic26->master = 1; break;
  209. case SND_SOC_DAIFMT_CBS_CFS: aic26->master = 0; break;
  210. default:
  211. dev_dbg(&aic26->spi->dev, "bad master\n"); return -EINVAL;
  212. }
  213. /* interface format */
  214. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  215. case SND_SOC_DAIFMT_I2S: aic26->datfm = AIC26_DATFM_I2S; break;
  216. case SND_SOC_DAIFMT_DSP_A: aic26->datfm = AIC26_DATFM_DSP; break;
  217. case SND_SOC_DAIFMT_RIGHT_J: aic26->datfm = AIC26_DATFM_RIGHTJ; break;
  218. case SND_SOC_DAIFMT_LEFT_J: aic26->datfm = AIC26_DATFM_LEFTJ; break;
  219. default:
  220. dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
  221. }
  222. return 0;
  223. }
  224. /* ---------------------------------------------------------------------
  225. * Digital Audio Interface Definition
  226. */
  227. #define AIC26_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  228. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  229. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  230. SNDRV_PCM_RATE_48000)
  231. #define AIC26_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |\
  232. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
  233. struct snd_soc_dai aic26_dai = {
  234. .name = "tlv320aic26",
  235. .playback = {
  236. .stream_name = "Playback",
  237. .channels_min = 2,
  238. .channels_max = 2,
  239. .rates = AIC26_RATES,
  240. .formats = AIC26_FORMATS,
  241. },
  242. .capture = {
  243. .stream_name = "Capture",
  244. .channels_min = 2,
  245. .channels_max = 2,
  246. .rates = AIC26_RATES,
  247. .formats = AIC26_FORMATS,
  248. },
  249. .ops = {
  250. .hw_params = aic26_hw_params,
  251. },
  252. .dai_ops = {
  253. .digital_mute = aic26_mute,
  254. .set_sysclk = aic26_set_sysclk,
  255. .set_fmt = aic26_set_fmt,
  256. },
  257. };
  258. EXPORT_SYMBOL_GPL(aic26_dai);
  259. /* ---------------------------------------------------------------------
  260. * ALSA controls
  261. */
  262. static const char *aic26_capture_src_text[] = {"Mic", "Aux"};
  263. static const struct soc_enum aic26_capture_src_enum =
  264. SOC_ENUM_SINGLE(AIC26_REG_AUDIO_CTRL1, 12, 2, aic26_capture_src_text);
  265. static const struct snd_kcontrol_new aic26_snd_controls[] = {
  266. /* Output */
  267. SOC_DOUBLE("PCM Playback Volume", AIC26_REG_DAC_GAIN, 8, 0, 0x7f, 1),
  268. SOC_DOUBLE("PCM Playback Switch", AIC26_REG_DAC_GAIN, 15, 7, 1, 1),
  269. SOC_SINGLE("PCM Capture Volume", AIC26_REG_ADC_GAIN, 8, 0x7f, 0),
  270. SOC_SINGLE("PCM Capture Mute", AIC26_REG_ADC_GAIN, 15, 1, 1),
  271. SOC_SINGLE("Keyclick activate", AIC26_REG_AUDIO_CTRL2, 15, 0x1, 0),
  272. SOC_SINGLE("Keyclick amplitude", AIC26_REG_AUDIO_CTRL2, 12, 0x7, 0),
  273. SOC_SINGLE("Keyclick frequency", AIC26_REG_AUDIO_CTRL2, 8, 0x7, 0),
  274. SOC_SINGLE("Keyclick period", AIC26_REG_AUDIO_CTRL2, 4, 0xf, 0),
  275. SOC_ENUM("Capture Source", aic26_capture_src_enum),
  276. };
  277. /* ---------------------------------------------------------------------
  278. * SoC CODEC portion of driver: probe and release routines
  279. */
  280. static int aic26_probe(struct platform_device *pdev)
  281. {
  282. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  283. struct snd_soc_codec *codec;
  284. struct snd_kcontrol *kcontrol;
  285. struct aic26 *aic26;
  286. int i, 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->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. for (i = 0; i < ARRAY_SIZE(aic26_snd_controls); i++) {
  310. kcontrol = snd_soc_cnew(&aic26_snd_controls[i], codec, NULL);
  311. err = snd_ctl_add(codec->card, kcontrol);
  312. WARN_ON(err < 0);
  313. }
  314. /* CODEC is setup, we can register the card now */
  315. dev_dbg(&pdev->dev, "Registering card\n");
  316. ret = snd_soc_register_card(socdev);
  317. if (ret < 0) {
  318. dev_err(&pdev->dev, "aic26: failed to register card\n");
  319. goto card_err;
  320. }
  321. return 0;
  322. card_err:
  323. snd_soc_free_pcms(socdev);
  324. return ret;
  325. }
  326. static int aic26_remove(struct platform_device *pdev)
  327. {
  328. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  329. snd_soc_free_pcms(socdev);
  330. return 0;
  331. }
  332. struct snd_soc_codec_device aic26_soc_codec_dev = {
  333. .probe = aic26_probe,
  334. .remove = aic26_remove,
  335. };
  336. EXPORT_SYMBOL_GPL(aic26_soc_codec_dev);
  337. /* ---------------------------------------------------------------------
  338. * SPI device portion of driver: sysfs files for debugging
  339. */
  340. static ssize_t aic26_keyclick_show(struct device *dev,
  341. struct device_attribute *attr, char *buf)
  342. {
  343. struct aic26 *aic26 = dev_get_drvdata(dev);
  344. int val, amp, freq, len;
  345. val = aic26_reg_read_cache(&aic26->codec, AIC26_REG_AUDIO_CTRL2);
  346. amp = (val >> 12) & 0x7;
  347. freq = (125 << ((val >> 8) & 0x7)) >> 1;
  348. len = 2 * (1 + ((val >> 4) & 0xf));
  349. return sprintf(buf, "amp=%x freq=%iHz len=%iclks\n", amp, freq, len);
  350. }
  351. /* Any write to the keyclick attribute will trigger the keyclick event */
  352. static ssize_t aic26_keyclick_set(struct device *dev,
  353. struct device_attribute *attr,
  354. const char *buf, size_t count)
  355. {
  356. struct aic26 *aic26 = dev_get_drvdata(dev);
  357. int val;
  358. val = aic26_reg_read_cache(&aic26->codec, AIC26_REG_AUDIO_CTRL2);
  359. val |= 0x8000;
  360. aic26_reg_write(&aic26->codec, AIC26_REG_AUDIO_CTRL2, val);
  361. return count;
  362. }
  363. static DEVICE_ATTR(keyclick, 0644, aic26_keyclick_show, aic26_keyclick_set);
  364. /* ---------------------------------------------------------------------
  365. * SPI device portion of driver: probe and release routines and SPI
  366. * driver registration.
  367. */
  368. static int aic26_spi_probe(struct spi_device *spi)
  369. {
  370. struct aic26 *aic26;
  371. int rc, i, reg;
  372. dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n");
  373. /* Allocate driver data */
  374. aic26 = kzalloc(sizeof *aic26, GFP_KERNEL);
  375. if (!aic26)
  376. return -ENOMEM;
  377. /* Initialize the driver data */
  378. aic26->spi = spi;
  379. dev_set_drvdata(&spi->dev, aic26);
  380. /* Setup what we can in the codec structure so that the register
  381. * access functions will work as expected. More will be filled
  382. * out when it is probed by the SoC CODEC part of this driver */
  383. aic26->codec.private_data = aic26;
  384. aic26->codec.name = "aic26";
  385. aic26->codec.owner = THIS_MODULE;
  386. aic26->codec.dai = &aic26_dai;
  387. aic26->codec.num_dai = 1;
  388. aic26->codec.read = aic26_reg_read;
  389. aic26->codec.write = aic26_reg_write;
  390. aic26->master = 1;
  391. mutex_init(&aic26->codec.mutex);
  392. INIT_LIST_HEAD(&aic26->codec.dapm_widgets);
  393. INIT_LIST_HEAD(&aic26->codec.dapm_paths);
  394. aic26->codec.reg_cache_size = AIC26_NUM_REGS;
  395. aic26->codec.reg_cache = aic26->reg_cache;
  396. /* Reset the codec to power on defaults */
  397. aic26_reg_write(&aic26->codec, AIC26_REG_RESET, 0xBB00);
  398. /* Power up CODEC */
  399. aic26_reg_write(&aic26->codec, AIC26_REG_POWER_CTRL, 0);
  400. /* Audio Control 3 (master mode, fsref rate) */
  401. reg = aic26_reg_read(&aic26->codec, AIC26_REG_AUDIO_CTRL3);
  402. reg &= ~0xf800;
  403. reg |= 0x0800; /* set master mode */
  404. aic26_reg_write(&aic26->codec, AIC26_REG_AUDIO_CTRL3, reg);
  405. /* Fill register cache */
  406. for (i = 0; i < ARRAY_SIZE(aic26->reg_cache); i++)
  407. aic26_reg_read(&aic26->codec, i);
  408. /* Register the sysfs files for debugging */
  409. /* Create SysFS files */
  410. rc = device_create_file(&spi->dev, &dev_attr_keyclick);
  411. if (rc)
  412. dev_info(&spi->dev, "error creating sysfs files\n");
  413. #if defined(CONFIG_SND_SOC_OF_SIMPLE)
  414. /* Tell the of_soc helper about this codec */
  415. of_snd_soc_register_codec(&aic26_soc_codec_dev, aic26, &aic26_dai,
  416. spi->dev.archdata.of_node);
  417. #endif
  418. dev_dbg(&spi->dev, "SPI device initialized\n");
  419. return 0;
  420. }
  421. static int aic26_spi_remove(struct spi_device *spi)
  422. {
  423. struct aic26 *aic26 = dev_get_drvdata(&spi->dev);
  424. kfree(aic26);
  425. return 0;
  426. }
  427. static struct spi_driver aic26_spi = {
  428. .driver = {
  429. .name = "tlv320aic26",
  430. .owner = THIS_MODULE,
  431. },
  432. .probe = aic26_spi_probe,
  433. .remove = aic26_spi_remove,
  434. };
  435. static int __init aic26_init(void)
  436. {
  437. return spi_register_driver(&aic26_spi);
  438. }
  439. module_init(aic26_init);
  440. static void __exit aic26_exit(void)
  441. {
  442. spi_unregister_driver(&aic26_spi);
  443. }
  444. module_exit(aic26_exit);