tlv320aic26.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/initval.h>
  21. #include "tlv320aic26.h"
  22. MODULE_DESCRIPTION("ASoC TLV320AIC26 codec driver");
  23. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  24. MODULE_LICENSE("GPL");
  25. /* AIC26 driver private data */
  26. struct aic26 {
  27. struct spi_device *spi;
  28. struct snd_soc_codec *codec;
  29. int master;
  30. int datfm;
  31. int mclk;
  32. /* Keyclick parameters */
  33. int keyclick_amplitude;
  34. int keyclick_freq;
  35. int keyclick_len;
  36. };
  37. /* ---------------------------------------------------------------------
  38. * Register access routines
  39. */
  40. static unsigned int aic26_reg_read(struct snd_soc_codec *codec,
  41. unsigned int reg)
  42. {
  43. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  44. u16 *cache = codec->reg_cache;
  45. u16 cmd, value;
  46. u8 buffer[2];
  47. int rc;
  48. if (reg >= AIC26_NUM_REGS) {
  49. WARN_ON_ONCE(1);
  50. return 0;
  51. }
  52. /* Do SPI transfer; first 16bits are command; remaining is
  53. * register contents */
  54. cmd = AIC26_READ_COMMAND_WORD(reg);
  55. buffer[0] = (cmd >> 8) & 0xff;
  56. buffer[1] = cmd & 0xff;
  57. rc = spi_write_then_read(aic26->spi, buffer, 2, buffer, 2);
  58. if (rc) {
  59. dev_err(&aic26->spi->dev, "AIC26 reg read error\n");
  60. return -EIO;
  61. }
  62. value = (buffer[0] << 8) | buffer[1];
  63. /* Update the cache before returning with the value */
  64. cache[reg] = value;
  65. return value;
  66. }
  67. static int aic26_reg_write(struct snd_soc_codec *codec, unsigned int reg,
  68. unsigned int value)
  69. {
  70. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  71. u16 *cache = codec->reg_cache;
  72. u16 cmd;
  73. u8 buffer[4];
  74. int rc;
  75. if (reg >= AIC26_NUM_REGS) {
  76. WARN_ON_ONCE(1);
  77. return -EINVAL;
  78. }
  79. /* Do SPI transfer; first 16bits are command; remaining is data
  80. * to write into register */
  81. cmd = AIC26_WRITE_COMMAND_WORD(reg);
  82. buffer[0] = (cmd >> 8) & 0xff;
  83. buffer[1] = cmd & 0xff;
  84. buffer[2] = value >> 8;
  85. buffer[3] = value;
  86. rc = spi_write(aic26->spi, buffer, 4);
  87. if (rc) {
  88. dev_err(&aic26->spi->dev, "AIC26 reg read error\n");
  89. return -EIO;
  90. }
  91. /* update cache before returning */
  92. cache[reg] = value;
  93. return 0;
  94. }
  95. static const struct snd_soc_dapm_widget tlv320aic26_dapm_widgets[] = {
  96. SND_SOC_DAPM_INPUT("MICIN"),
  97. SND_SOC_DAPM_INPUT("AUX"),
  98. SND_SOC_DAPM_OUTPUT("HPL"),
  99. SND_SOC_DAPM_OUTPUT("HPR"),
  100. };
  101. static const struct snd_soc_dapm_route tlv320aic26_dapm_routes[] = {
  102. { "Capture", NULL, "MICIN" },
  103. { "Capture", NULL, "AUX" },
  104. { "HPL", NULL, "Playback" },
  105. { "HPR", NULL, "Playback" },
  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_codec *codec = dai->codec;
  115. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  116. int fsref, divisor, wlen, pval, jval, dval, qval;
  117. u16 reg;
  118. dev_dbg(&aic26->spi->dev, "aic26_hw_params(substream=%p, params=%p)\n",
  119. substream, params);
  120. dev_dbg(&aic26->spi->dev, "rate=%i format=%i\n", params_rate(params),
  121. params_format(params));
  122. switch (params_rate(params)) {
  123. case 8000: fsref = 48000; divisor = AIC26_DIV_6; break;
  124. case 11025: fsref = 44100; divisor = AIC26_DIV_4; break;
  125. case 12000: fsref = 48000; divisor = AIC26_DIV_4; break;
  126. case 16000: fsref = 48000; divisor = AIC26_DIV_3; break;
  127. case 22050: fsref = 44100; divisor = AIC26_DIV_2; break;
  128. case 24000: fsref = 48000; divisor = AIC26_DIV_2; break;
  129. case 32000: fsref = 48000; divisor = AIC26_DIV_1_5; break;
  130. case 44100: fsref = 44100; divisor = AIC26_DIV_1; break;
  131. case 48000: fsref = 48000; divisor = AIC26_DIV_1; break;
  132. default:
  133. dev_dbg(&aic26->spi->dev, "bad rate\n"); return -EINVAL;
  134. }
  135. /* select data word length */
  136. switch (params_format(params)) {
  137. case SNDRV_PCM_FORMAT_S8: wlen = AIC26_WLEN_16; break;
  138. case SNDRV_PCM_FORMAT_S16_BE: wlen = AIC26_WLEN_16; break;
  139. case SNDRV_PCM_FORMAT_S24_BE: wlen = AIC26_WLEN_24; break;
  140. case SNDRV_PCM_FORMAT_S32_BE: wlen = AIC26_WLEN_32; break;
  141. default:
  142. dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
  143. }
  144. /**
  145. * Configure PLL
  146. * fsref = (mclk * PLLM) / 2048
  147. * where PLLM = J.DDDD (DDDD register ranges from 0 to 9999, decimal)
  148. */
  149. pval = 1;
  150. /* compute J portion of multiplier */
  151. jval = fsref / (aic26->mclk / 2048);
  152. /* compute fractional DDDD component of multiplier */
  153. dval = fsref - (jval * (aic26->mclk / 2048));
  154. dval = (10000 * dval) / (aic26->mclk / 2048);
  155. dev_dbg(&aic26->spi->dev, "Setting PLLM to %d.%04d\n", jval, dval);
  156. qval = 0;
  157. reg = 0x8000 | qval << 11 | pval << 8 | jval << 2;
  158. snd_soc_write(codec, AIC26_REG_PLL_PROG1, reg);
  159. reg = dval << 2;
  160. snd_soc_write(codec, AIC26_REG_PLL_PROG2, reg);
  161. /* Audio Control 3 (master mode, fsref rate) */
  162. if (aic26->master)
  163. reg = 0x0800;
  164. if (fsref == 48000)
  165. reg = 0x2000;
  166. snd_soc_update_bits(codec, AIC26_REG_AUDIO_CTRL3, 0xf800, reg);
  167. /* Audio Control 1 (FSref divisor) */
  168. reg = wlen | aic26->datfm | (divisor << 3) | divisor;
  169. snd_soc_update_bits(codec, AIC26_REG_AUDIO_CTRL1, 0xfff, reg);
  170. return 0;
  171. }
  172. /**
  173. * aic26_mute - Mute control to reduce noise when changing audio format
  174. */
  175. static int aic26_mute(struct snd_soc_dai *dai, int mute)
  176. {
  177. struct snd_soc_codec *codec = dai->codec;
  178. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  179. u16 reg;
  180. dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n",
  181. dai, mute);
  182. if (mute)
  183. reg = 0x8080;
  184. else
  185. reg = 0;
  186. snd_soc_update_bits(codec, AIC26_REG_DAC_GAIN, 0x8000, reg);
  187. return 0;
  188. }
  189. static int aic26_set_sysclk(struct snd_soc_dai *codec_dai,
  190. int clk_id, unsigned int freq, int dir)
  191. {
  192. struct snd_soc_codec *codec = codec_dai->codec;
  193. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  194. dev_dbg(&aic26->spi->dev, "aic26_set_sysclk(dai=%p, clk_id==%i,"
  195. " freq=%i, dir=%i)\n",
  196. codec_dai, clk_id, freq, dir);
  197. /* MCLK needs to fall between 2MHz and 50 MHz */
  198. if ((freq < 2000000) || (freq > 50000000))
  199. return -EINVAL;
  200. aic26->mclk = freq;
  201. return 0;
  202. }
  203. static int aic26_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  204. {
  205. struct snd_soc_codec *codec = codec_dai->codec;
  206. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  207. dev_dbg(&aic26->spi->dev, "aic26_set_fmt(dai=%p, fmt==%i)\n",
  208. codec_dai, fmt);
  209. /* set master/slave audio interface */
  210. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  211. case SND_SOC_DAIFMT_CBM_CFM: aic26->master = 1; break;
  212. case SND_SOC_DAIFMT_CBS_CFS: aic26->master = 0; break;
  213. default:
  214. dev_dbg(&aic26->spi->dev, "bad master\n"); return -EINVAL;
  215. }
  216. /* interface format */
  217. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  218. case SND_SOC_DAIFMT_I2S: aic26->datfm = AIC26_DATFM_I2S; break;
  219. case SND_SOC_DAIFMT_DSP_A: aic26->datfm = AIC26_DATFM_DSP; break;
  220. case SND_SOC_DAIFMT_RIGHT_J: aic26->datfm = AIC26_DATFM_RIGHTJ; break;
  221. case SND_SOC_DAIFMT_LEFT_J: aic26->datfm = AIC26_DATFM_LEFTJ; break;
  222. default:
  223. dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
  224. }
  225. return 0;
  226. }
  227. /* ---------------------------------------------------------------------
  228. * Digital Audio Interface Definition
  229. */
  230. #define AIC26_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  231. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  232. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  233. SNDRV_PCM_RATE_48000)
  234. #define AIC26_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |\
  235. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
  236. static const struct snd_soc_dai_ops aic26_dai_ops = {
  237. .hw_params = aic26_hw_params,
  238. .digital_mute = aic26_mute,
  239. .set_sysclk = aic26_set_sysclk,
  240. .set_fmt = aic26_set_fmt,
  241. };
  242. static struct snd_soc_dai_driver aic26_dai = {
  243. .name = "tlv320aic26-hifi",
  244. .playback = {
  245. .stream_name = "Playback",
  246. .channels_min = 2,
  247. .channels_max = 2,
  248. .rates = AIC26_RATES,
  249. .formats = AIC26_FORMATS,
  250. },
  251. .capture = {
  252. .stream_name = "Capture",
  253. .channels_min = 2,
  254. .channels_max = 2,
  255. .rates = AIC26_RATES,
  256. .formats = AIC26_FORMATS,
  257. },
  258. .ops = &aic26_dai_ops,
  259. };
  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. * SPI device portion of driver: sysfs files for debugging
  280. */
  281. static ssize_t aic26_keyclick_show(struct device *dev,
  282. struct device_attribute *attr, char *buf)
  283. {
  284. struct aic26 *aic26 = dev_get_drvdata(dev);
  285. int val, amp, freq, len;
  286. val = snd_soc_read(aic26->codec, AIC26_REG_AUDIO_CTRL2);
  287. amp = (val >> 12) & 0x7;
  288. freq = (125 << ((val >> 8) & 0x7)) >> 1;
  289. len = 2 * (1 + ((val >> 4) & 0xf));
  290. return sprintf(buf, "amp=%x freq=%iHz len=%iclks\n", amp, freq, len);
  291. }
  292. /* Any write to the keyclick attribute will trigger the keyclick event */
  293. static ssize_t aic26_keyclick_set(struct device *dev,
  294. struct device_attribute *attr,
  295. const char *buf, size_t count)
  296. {
  297. struct aic26 *aic26 = dev_get_drvdata(dev);
  298. snd_soc_update_bits(aic26->codec, AIC26_REG_AUDIO_CTRL2,
  299. 0x8000, 0x800);
  300. return count;
  301. }
  302. static DEVICE_ATTR(keyclick, 0644, aic26_keyclick_show, aic26_keyclick_set);
  303. /* ---------------------------------------------------------------------
  304. * SoC CODEC portion of driver: probe and release routines
  305. */
  306. static int aic26_probe(struct snd_soc_codec *codec)
  307. {
  308. struct aic26 *aic26 = dev_get_drvdata(codec->dev);
  309. int ret, reg;
  310. aic26->codec = codec;
  311. /* Reset the codec to power on defaults */
  312. snd_soc_write(codec, AIC26_REG_RESET, 0xBB00);
  313. /* Power up CODEC */
  314. snd_soc_write(codec, AIC26_REG_POWER_CTRL, 0);
  315. /* Audio Control 3 (master mode, fsref rate) */
  316. reg = snd_soc_read(codec, AIC26_REG_AUDIO_CTRL3);
  317. reg &= ~0xf800;
  318. reg |= 0x0800; /* set master mode */
  319. snd_soc_write(codec, AIC26_REG_AUDIO_CTRL3, reg);
  320. /* Register the sysfs files for debugging */
  321. /* Create SysFS files */
  322. ret = device_create_file(codec->dev, &dev_attr_keyclick);
  323. if (ret)
  324. dev_info(codec->dev, "error creating sysfs files\n");
  325. return 0;
  326. }
  327. static struct snd_soc_codec_driver aic26_soc_codec_dev = {
  328. .probe = aic26_probe,
  329. .read = aic26_reg_read,
  330. .write = aic26_reg_write,
  331. .controls = aic26_snd_controls,
  332. .num_controls = ARRAY_SIZE(aic26_snd_controls),
  333. .dapm_widgets = tlv320aic26_dapm_widgets,
  334. .num_dapm_widgets = ARRAY_SIZE(tlv320aic26_dapm_widgets),
  335. .dapm_routes = tlv320aic26_dapm_routes,
  336. .num_dapm_routes = ARRAY_SIZE(tlv320aic26_dapm_routes),
  337. };
  338. /* ---------------------------------------------------------------------
  339. * SPI device portion of driver: probe and release routines and SPI
  340. * driver registration.
  341. */
  342. static int aic26_spi_probe(struct spi_device *spi)
  343. {
  344. struct aic26 *aic26;
  345. int ret;
  346. dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n");
  347. /* Allocate driver data */
  348. aic26 = devm_kzalloc(&spi->dev, sizeof *aic26, GFP_KERNEL);
  349. if (!aic26)
  350. return -ENOMEM;
  351. /* Initialize the driver data */
  352. aic26->spi = spi;
  353. dev_set_drvdata(&spi->dev, aic26);
  354. aic26->master = 1;
  355. ret = snd_soc_register_codec(&spi->dev,
  356. &aic26_soc_codec_dev, &aic26_dai, 1);
  357. return ret;
  358. }
  359. static int aic26_spi_remove(struct spi_device *spi)
  360. {
  361. snd_soc_unregister_codec(&spi->dev);
  362. return 0;
  363. }
  364. static struct spi_driver aic26_spi = {
  365. .driver = {
  366. .name = "tlv320aic26-codec",
  367. .owner = THIS_MODULE,
  368. },
  369. .probe = aic26_spi_probe,
  370. .remove = aic26_spi_remove,
  371. };
  372. module_spi_driver(aic26_spi);