tlv320aic26.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 regmap *regmap;
  29. struct snd_soc_codec *codec;
  30. int master;
  31. int datfm;
  32. int mclk;
  33. /* Keyclick parameters */
  34. int keyclick_amplitude;
  35. int keyclick_freq;
  36. int keyclick_len;
  37. };
  38. static const struct snd_soc_dapm_widget tlv320aic26_dapm_widgets[] = {
  39. SND_SOC_DAPM_INPUT("MICIN"),
  40. SND_SOC_DAPM_INPUT("AUX"),
  41. SND_SOC_DAPM_OUTPUT("HPL"),
  42. SND_SOC_DAPM_OUTPUT("HPR"),
  43. };
  44. static const struct snd_soc_dapm_route tlv320aic26_dapm_routes[] = {
  45. { "Capture", NULL, "MICIN" },
  46. { "Capture", NULL, "AUX" },
  47. { "HPL", NULL, "Playback" },
  48. { "HPR", NULL, "Playback" },
  49. };
  50. /* ---------------------------------------------------------------------
  51. * Digital Audio Interface Operations
  52. */
  53. static int aic26_hw_params(struct snd_pcm_substream *substream,
  54. struct snd_pcm_hw_params *params,
  55. struct snd_soc_dai *dai)
  56. {
  57. struct snd_soc_codec *codec = dai->codec;
  58. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  59. int fsref, divisor, wlen, pval, jval, dval, qval;
  60. u16 reg;
  61. dev_dbg(&aic26->spi->dev, "aic26_hw_params(substream=%p, params=%p)\n",
  62. substream, params);
  63. dev_dbg(&aic26->spi->dev, "rate=%i width=%d\n", params_rate(params),
  64. params_width(params));
  65. switch (params_rate(params)) {
  66. case 8000: fsref = 48000; divisor = AIC26_DIV_6; break;
  67. case 11025: fsref = 44100; divisor = AIC26_DIV_4; break;
  68. case 12000: fsref = 48000; divisor = AIC26_DIV_4; break;
  69. case 16000: fsref = 48000; divisor = AIC26_DIV_3; break;
  70. case 22050: fsref = 44100; divisor = AIC26_DIV_2; break;
  71. case 24000: fsref = 48000; divisor = AIC26_DIV_2; break;
  72. case 32000: fsref = 48000; divisor = AIC26_DIV_1_5; break;
  73. case 44100: fsref = 44100; divisor = AIC26_DIV_1; break;
  74. case 48000: fsref = 48000; divisor = AIC26_DIV_1; break;
  75. default:
  76. dev_dbg(&aic26->spi->dev, "bad rate\n"); return -EINVAL;
  77. }
  78. /* select data word length */
  79. switch (params_width(params)) {
  80. case 8: wlen = AIC26_WLEN_16; break;
  81. case 16: wlen = AIC26_WLEN_16; break;
  82. case 24: wlen = AIC26_WLEN_24; break;
  83. case 32: wlen = AIC26_WLEN_32; break;
  84. default:
  85. dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
  86. }
  87. /**
  88. * Configure PLL
  89. * fsref = (mclk * PLLM) / 2048
  90. * where PLLM = J.DDDD (DDDD register ranges from 0 to 9999, decimal)
  91. */
  92. pval = 1;
  93. /* compute J portion of multiplier */
  94. jval = fsref / (aic26->mclk / 2048);
  95. /* compute fractional DDDD component of multiplier */
  96. dval = fsref - (jval * (aic26->mclk / 2048));
  97. dval = (10000 * dval) / (aic26->mclk / 2048);
  98. dev_dbg(&aic26->spi->dev, "Setting PLLM to %d.%04d\n", jval, dval);
  99. qval = 0;
  100. reg = 0x8000 | qval << 11 | pval << 8 | jval << 2;
  101. snd_soc_write(codec, AIC26_REG_PLL_PROG1, reg);
  102. reg = dval << 2;
  103. snd_soc_write(codec, AIC26_REG_PLL_PROG2, reg);
  104. /* Audio Control 3 (master mode, fsref rate) */
  105. if (aic26->master)
  106. reg = 0x0800;
  107. if (fsref == 48000)
  108. reg = 0x2000;
  109. snd_soc_update_bits(codec, AIC26_REG_AUDIO_CTRL3, 0xf800, reg);
  110. /* Audio Control 1 (FSref divisor) */
  111. reg = wlen | aic26->datfm | (divisor << 3) | divisor;
  112. snd_soc_update_bits(codec, AIC26_REG_AUDIO_CTRL1, 0xfff, reg);
  113. return 0;
  114. }
  115. /**
  116. * aic26_mute - Mute control to reduce noise when changing audio format
  117. */
  118. static int aic26_mute(struct snd_soc_dai *dai, int mute)
  119. {
  120. struct snd_soc_codec *codec = dai->codec;
  121. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  122. u16 reg;
  123. dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n",
  124. dai, mute);
  125. if (mute)
  126. reg = 0x8080;
  127. else
  128. reg = 0;
  129. snd_soc_update_bits(codec, AIC26_REG_DAC_GAIN, 0x8000, reg);
  130. return 0;
  131. }
  132. static int aic26_set_sysclk(struct snd_soc_dai *codec_dai,
  133. int clk_id, unsigned int freq, int dir)
  134. {
  135. struct snd_soc_codec *codec = codec_dai->codec;
  136. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  137. dev_dbg(&aic26->spi->dev, "aic26_set_sysclk(dai=%p, clk_id==%i,"
  138. " freq=%i, dir=%i)\n",
  139. codec_dai, clk_id, freq, dir);
  140. /* MCLK needs to fall between 2MHz and 50 MHz */
  141. if ((freq < 2000000) || (freq > 50000000))
  142. return -EINVAL;
  143. aic26->mclk = freq;
  144. return 0;
  145. }
  146. static int aic26_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  147. {
  148. struct snd_soc_codec *codec = codec_dai->codec;
  149. struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec);
  150. dev_dbg(&aic26->spi->dev, "aic26_set_fmt(dai=%p, fmt==%i)\n",
  151. codec_dai, fmt);
  152. /* set master/slave audio interface */
  153. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  154. case SND_SOC_DAIFMT_CBM_CFM: aic26->master = 1; break;
  155. case SND_SOC_DAIFMT_CBS_CFS: aic26->master = 0; break;
  156. default:
  157. dev_dbg(&aic26->spi->dev, "bad master\n"); return -EINVAL;
  158. }
  159. /* interface format */
  160. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  161. case SND_SOC_DAIFMT_I2S: aic26->datfm = AIC26_DATFM_I2S; break;
  162. case SND_SOC_DAIFMT_DSP_A: aic26->datfm = AIC26_DATFM_DSP; break;
  163. case SND_SOC_DAIFMT_RIGHT_J: aic26->datfm = AIC26_DATFM_RIGHTJ; break;
  164. case SND_SOC_DAIFMT_LEFT_J: aic26->datfm = AIC26_DATFM_LEFTJ; break;
  165. default:
  166. dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
  167. }
  168. return 0;
  169. }
  170. /* ---------------------------------------------------------------------
  171. * Digital Audio Interface Definition
  172. */
  173. #define AIC26_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  174. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  175. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  176. SNDRV_PCM_RATE_48000)
  177. #define AIC26_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |\
  178. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
  179. static const struct snd_soc_dai_ops aic26_dai_ops = {
  180. .hw_params = aic26_hw_params,
  181. .digital_mute = aic26_mute,
  182. .set_sysclk = aic26_set_sysclk,
  183. .set_fmt = aic26_set_fmt,
  184. };
  185. static struct snd_soc_dai_driver aic26_dai = {
  186. .name = "tlv320aic26-hifi",
  187. .playback = {
  188. .stream_name = "Playback",
  189. .channels_min = 2,
  190. .channels_max = 2,
  191. .rates = AIC26_RATES,
  192. .formats = AIC26_FORMATS,
  193. },
  194. .capture = {
  195. .stream_name = "Capture",
  196. .channels_min = 2,
  197. .channels_max = 2,
  198. .rates = AIC26_RATES,
  199. .formats = AIC26_FORMATS,
  200. },
  201. .ops = &aic26_dai_ops,
  202. };
  203. /* ---------------------------------------------------------------------
  204. * ALSA controls
  205. */
  206. static const char *aic26_capture_src_text[] = {"Mic", "Aux"};
  207. static SOC_ENUM_SINGLE_DECL(aic26_capture_src_enum,
  208. AIC26_REG_AUDIO_CTRL1, 12,
  209. aic26_capture_src_text);
  210. static const struct snd_kcontrol_new aic26_snd_controls[] = {
  211. /* Output */
  212. SOC_DOUBLE("PCM Playback Volume", AIC26_REG_DAC_GAIN, 8, 0, 0x7f, 1),
  213. SOC_DOUBLE("PCM Playback Switch", AIC26_REG_DAC_GAIN, 15, 7, 1, 1),
  214. SOC_SINGLE("PCM Capture Volume", AIC26_REG_ADC_GAIN, 8, 0x7f, 0),
  215. SOC_SINGLE("PCM Capture Mute", AIC26_REG_ADC_GAIN, 15, 1, 1),
  216. SOC_SINGLE("Keyclick activate", AIC26_REG_AUDIO_CTRL2, 15, 0x1, 0),
  217. SOC_SINGLE("Keyclick amplitude", AIC26_REG_AUDIO_CTRL2, 12, 0x7, 0),
  218. SOC_SINGLE("Keyclick frequency", AIC26_REG_AUDIO_CTRL2, 8, 0x7, 0),
  219. SOC_SINGLE("Keyclick period", AIC26_REG_AUDIO_CTRL2, 4, 0xf, 0),
  220. SOC_ENUM("Capture Source", aic26_capture_src_enum),
  221. };
  222. /* ---------------------------------------------------------------------
  223. * SPI device portion of driver: sysfs files for debugging
  224. */
  225. static ssize_t aic26_keyclick_show(struct device *dev,
  226. struct device_attribute *attr, char *buf)
  227. {
  228. struct aic26 *aic26 = dev_get_drvdata(dev);
  229. int val, amp, freq, len;
  230. val = snd_soc_read(aic26->codec, AIC26_REG_AUDIO_CTRL2);
  231. amp = (val >> 12) & 0x7;
  232. freq = (125 << ((val >> 8) & 0x7)) >> 1;
  233. len = 2 * (1 + ((val >> 4) & 0xf));
  234. return sprintf(buf, "amp=%x freq=%iHz len=%iclks\n", amp, freq, len);
  235. }
  236. /* Any write to the keyclick attribute will trigger the keyclick event */
  237. static ssize_t aic26_keyclick_set(struct device *dev,
  238. struct device_attribute *attr,
  239. const char *buf, size_t count)
  240. {
  241. struct aic26 *aic26 = dev_get_drvdata(dev);
  242. snd_soc_update_bits(aic26->codec, AIC26_REG_AUDIO_CTRL2,
  243. 0x8000, 0x800);
  244. return count;
  245. }
  246. static DEVICE_ATTR(keyclick, 0644, aic26_keyclick_show, aic26_keyclick_set);
  247. /* ---------------------------------------------------------------------
  248. * SoC CODEC portion of driver: probe and release routines
  249. */
  250. static int aic26_probe(struct snd_soc_codec *codec)
  251. {
  252. struct aic26 *aic26 = dev_get_drvdata(codec->dev);
  253. int ret, reg;
  254. aic26->codec = codec;
  255. /* Reset the codec to power on defaults */
  256. snd_soc_write(codec, AIC26_REG_RESET, 0xBB00);
  257. /* Power up CODEC */
  258. snd_soc_write(codec, AIC26_REG_POWER_CTRL, 0);
  259. /* Audio Control 3 (master mode, fsref rate) */
  260. reg = snd_soc_read(codec, AIC26_REG_AUDIO_CTRL3);
  261. reg &= ~0xf800;
  262. reg |= 0x0800; /* set master mode */
  263. snd_soc_write(codec, AIC26_REG_AUDIO_CTRL3, reg);
  264. /* Register the sysfs files for debugging */
  265. /* Create SysFS files */
  266. ret = device_create_file(codec->dev, &dev_attr_keyclick);
  267. if (ret)
  268. dev_info(codec->dev, "error creating sysfs files\n");
  269. return 0;
  270. }
  271. static struct snd_soc_codec_driver aic26_soc_codec_dev = {
  272. .probe = aic26_probe,
  273. .controls = aic26_snd_controls,
  274. .num_controls = ARRAY_SIZE(aic26_snd_controls),
  275. .dapm_widgets = tlv320aic26_dapm_widgets,
  276. .num_dapm_widgets = ARRAY_SIZE(tlv320aic26_dapm_widgets),
  277. .dapm_routes = tlv320aic26_dapm_routes,
  278. .num_dapm_routes = ARRAY_SIZE(tlv320aic26_dapm_routes),
  279. };
  280. static const struct regmap_config aic26_regmap = {
  281. .reg_bits = 16,
  282. .val_bits = 16,
  283. };
  284. /* ---------------------------------------------------------------------
  285. * SPI device portion of driver: probe and release routines and SPI
  286. * driver registration.
  287. */
  288. static int aic26_spi_probe(struct spi_device *spi)
  289. {
  290. struct aic26 *aic26;
  291. int ret;
  292. dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n");
  293. /* Allocate driver data */
  294. aic26 = devm_kzalloc(&spi->dev, sizeof *aic26, GFP_KERNEL);
  295. if (!aic26)
  296. return -ENOMEM;
  297. aic26->regmap = devm_regmap_init_spi(spi, &aic26_regmap);
  298. if (IS_ERR(aic26->regmap))
  299. return PTR_ERR(aic26->regmap);
  300. /* Initialize the driver data */
  301. aic26->spi = spi;
  302. dev_set_drvdata(&spi->dev, aic26);
  303. aic26->master = 1;
  304. ret = snd_soc_register_codec(&spi->dev,
  305. &aic26_soc_codec_dev, &aic26_dai, 1);
  306. return ret;
  307. }
  308. static int aic26_spi_remove(struct spi_device *spi)
  309. {
  310. snd_soc_unregister_codec(&spi->dev);
  311. return 0;
  312. }
  313. static struct spi_driver aic26_spi = {
  314. .driver = {
  315. .name = "tlv320aic26-codec",
  316. },
  317. .probe = aic26_spi_probe,
  318. .remove = aic26_spi_remove,
  319. };
  320. module_spi_driver(aic26_spi);