tlv320aic26.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Texas Instruments TLV320AIC26 low power audio CODEC
  4. * ALSA SoC CODEC driver
  5. *
  6. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/pm.h>
  13. #include <linux/device.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/slab.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include <sound/initval.h>
  22. #include "tlv320aic26.h"
  23. MODULE_DESCRIPTION("ASoC TLV320AIC26 codec driver");
  24. MODULE_AUTHOR("Grant Likely <[email protected]>");
  25. MODULE_LICENSE("GPL");
  26. /* AIC26 driver private data */
  27. struct aic26 {
  28. struct spi_device *spi;
  29. struct regmap *regmap;
  30. struct snd_soc_component *component;
  31. int clock_provider;
  32. int datfm;
  33. int mclk;
  34. /* Keyclick parameters */
  35. int keyclick_amplitude;
  36. int keyclick_freq;
  37. int keyclick_len;
  38. };
  39. static const struct snd_soc_dapm_widget tlv320aic26_dapm_widgets[] = {
  40. SND_SOC_DAPM_INPUT("MICIN"),
  41. SND_SOC_DAPM_INPUT("AUX"),
  42. SND_SOC_DAPM_OUTPUT("HPL"),
  43. SND_SOC_DAPM_OUTPUT("HPR"),
  44. };
  45. static const struct snd_soc_dapm_route tlv320aic26_dapm_routes[] = {
  46. { "Capture", NULL, "MICIN" },
  47. { "Capture", NULL, "AUX" },
  48. { "HPL", NULL, "Playback" },
  49. { "HPR", NULL, "Playback" },
  50. };
  51. /* ---------------------------------------------------------------------
  52. * Digital Audio Interface Operations
  53. */
  54. static int aic26_hw_params(struct snd_pcm_substream *substream,
  55. struct snd_pcm_hw_params *params,
  56. struct snd_soc_dai *dai)
  57. {
  58. struct snd_soc_component *component = dai->component;
  59. struct aic26 *aic26 = snd_soc_component_get_drvdata(component);
  60. int fsref, divisor, wlen, pval, jval, dval, qval;
  61. u16 reg;
  62. dev_dbg(&aic26->spi->dev, "aic26_hw_params(substream=%p, params=%p)\n",
  63. substream, params);
  64. dev_dbg(&aic26->spi->dev, "rate=%i width=%d\n", params_rate(params),
  65. params_width(params));
  66. switch (params_rate(params)) {
  67. case 8000: fsref = 48000; divisor = AIC26_DIV_6; break;
  68. case 11025: fsref = 44100; divisor = AIC26_DIV_4; break;
  69. case 12000: fsref = 48000; divisor = AIC26_DIV_4; break;
  70. case 16000: fsref = 48000; divisor = AIC26_DIV_3; break;
  71. case 22050: fsref = 44100; divisor = AIC26_DIV_2; break;
  72. case 24000: fsref = 48000; divisor = AIC26_DIV_2; break;
  73. case 32000: fsref = 48000; divisor = AIC26_DIV_1_5; break;
  74. case 44100: fsref = 44100; divisor = AIC26_DIV_1; break;
  75. case 48000: fsref = 48000; divisor = AIC26_DIV_1; break;
  76. default:
  77. dev_dbg(&aic26->spi->dev, "bad rate\n"); return -EINVAL;
  78. }
  79. /* select data word length */
  80. switch (params_width(params)) {
  81. case 8: wlen = AIC26_WLEN_16; break;
  82. case 16: wlen = AIC26_WLEN_16; break;
  83. case 24: wlen = AIC26_WLEN_24; break;
  84. case 32: wlen = AIC26_WLEN_32; break;
  85. default:
  86. dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL;
  87. }
  88. /**
  89. * Configure PLL
  90. * fsref = (mclk * PLLM) / 2048
  91. * where PLLM = J.DDDD (DDDD register ranges from 0 to 9999, decimal)
  92. */
  93. pval = 1;
  94. /* compute J portion of multiplier */
  95. jval = fsref / (aic26->mclk / 2048);
  96. /* compute fractional DDDD component of multiplier */
  97. dval = fsref - (jval * (aic26->mclk / 2048));
  98. dval = (10000 * dval) / (aic26->mclk / 2048);
  99. dev_dbg(&aic26->spi->dev, "Setting PLLM to %d.%04d\n", jval, dval);
  100. qval = 0;
  101. reg = 0x8000 | qval << 11 | pval << 8 | jval << 2;
  102. snd_soc_component_write(component, AIC26_REG_PLL_PROG1, reg);
  103. reg = dval << 2;
  104. snd_soc_component_write(component, AIC26_REG_PLL_PROG2, reg);
  105. /* Audio Control 3 (clock provider mode, fsref rate) */
  106. if (aic26->clock_provider)
  107. reg = 0x0800;
  108. if (fsref == 48000)
  109. reg = 0x2000;
  110. snd_soc_component_update_bits(component, AIC26_REG_AUDIO_CTRL3, 0xf800, reg);
  111. /* Audio Control 1 (FSref divisor) */
  112. reg = wlen | aic26->datfm | (divisor << 3) | divisor;
  113. snd_soc_component_update_bits(component, AIC26_REG_AUDIO_CTRL1, 0xfff, reg);
  114. return 0;
  115. }
  116. /*
  117. * aic26_mute - Mute control to reduce noise when changing audio format
  118. */
  119. static int aic26_mute(struct snd_soc_dai *dai, int mute, int direction)
  120. {
  121. struct snd_soc_component *component = dai->component;
  122. struct aic26 *aic26 = snd_soc_component_get_drvdata(component);
  123. u16 reg;
  124. dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n",
  125. dai, mute);
  126. if (mute)
  127. reg = 0x8080;
  128. else
  129. reg = 0;
  130. snd_soc_component_update_bits(component, AIC26_REG_DAC_GAIN, 0x8000, reg);
  131. return 0;
  132. }
  133. static int aic26_set_sysclk(struct snd_soc_dai *codec_dai,
  134. int clk_id, unsigned int freq, int dir)
  135. {
  136. struct snd_soc_component *component = codec_dai->component;
  137. struct aic26 *aic26 = snd_soc_component_get_drvdata(component);
  138. dev_dbg(&aic26->spi->dev, "aic26_set_sysclk(dai=%p, clk_id==%i,"
  139. " freq=%i, dir=%i)\n",
  140. codec_dai, clk_id, freq, dir);
  141. /* MCLK needs to fall between 2MHz and 50 MHz */
  142. if ((freq < 2000000) || (freq > 50000000))
  143. return -EINVAL;
  144. aic26->mclk = freq;
  145. return 0;
  146. }
  147. static int aic26_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  148. {
  149. struct snd_soc_component *component = codec_dai->component;
  150. struct aic26 *aic26 = snd_soc_component_get_drvdata(component);
  151. dev_dbg(&aic26->spi->dev, "aic26_set_fmt(dai=%p, fmt==%i)\n",
  152. codec_dai, fmt);
  153. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  154. case SND_SOC_DAIFMT_CBP_CFP: aic26->clock_provider = 1; break;
  155. case SND_SOC_DAIFMT_CBC_CFC: aic26->clock_provider = 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. .mute_stream = aic26_mute,
  182. .set_sysclk = aic26_set_sysclk,
  183. .set_fmt = aic26_set_fmt,
  184. .no_capture_mute = 1,
  185. };
  186. static struct snd_soc_dai_driver aic26_dai = {
  187. .name = "tlv320aic26-hifi",
  188. .playback = {
  189. .stream_name = "Playback",
  190. .channels_min = 2,
  191. .channels_max = 2,
  192. .rates = AIC26_RATES,
  193. .formats = AIC26_FORMATS,
  194. },
  195. .capture = {
  196. .stream_name = "Capture",
  197. .channels_min = 2,
  198. .channels_max = 2,
  199. .rates = AIC26_RATES,
  200. .formats = AIC26_FORMATS,
  201. },
  202. .ops = &aic26_dai_ops,
  203. };
  204. /* ---------------------------------------------------------------------
  205. * ALSA controls
  206. */
  207. static const char *aic26_capture_src_text[] = {"Mic", "Aux"};
  208. static SOC_ENUM_SINGLE_DECL(aic26_capture_src_enum,
  209. AIC26_REG_AUDIO_CTRL1, 12,
  210. aic26_capture_src_text);
  211. static const struct snd_kcontrol_new aic26_snd_controls[] = {
  212. /* Output */
  213. SOC_DOUBLE("PCM Playback Volume", AIC26_REG_DAC_GAIN, 8, 0, 0x7f, 1),
  214. SOC_DOUBLE("PCM Playback Switch", AIC26_REG_DAC_GAIN, 15, 7, 1, 1),
  215. SOC_SINGLE("PCM Capture Volume", AIC26_REG_ADC_GAIN, 8, 0x7f, 0),
  216. SOC_SINGLE("PCM Capture Mute", AIC26_REG_ADC_GAIN, 15, 1, 1),
  217. SOC_SINGLE("Keyclick activate", AIC26_REG_AUDIO_CTRL2, 15, 0x1, 0),
  218. SOC_SINGLE("Keyclick amplitude", AIC26_REG_AUDIO_CTRL2, 12, 0x7, 0),
  219. SOC_SINGLE("Keyclick frequency", AIC26_REG_AUDIO_CTRL2, 8, 0x7, 0),
  220. SOC_SINGLE("Keyclick period", AIC26_REG_AUDIO_CTRL2, 4, 0xf, 0),
  221. SOC_ENUM("Capture Source", aic26_capture_src_enum),
  222. };
  223. /* ---------------------------------------------------------------------
  224. * SPI device portion of driver: sysfs files for debugging
  225. */
  226. static ssize_t keyclick_show(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct aic26 *aic26 = dev_get_drvdata(dev);
  230. int val, amp, freq, len;
  231. val = snd_soc_component_read(aic26->component, AIC26_REG_AUDIO_CTRL2);
  232. amp = (val >> 12) & 0x7;
  233. freq = (125 << ((val >> 8) & 0x7)) >> 1;
  234. len = 2 * (1 + ((val >> 4) & 0xf));
  235. return sysfs_emit(buf, "amp=%x freq=%iHz len=%iclks\n", amp, freq, len);
  236. }
  237. /* Any write to the keyclick attribute will trigger the keyclick event */
  238. static ssize_t keyclick_store(struct device *dev,
  239. struct device_attribute *attr,
  240. const char *buf, size_t count)
  241. {
  242. struct aic26 *aic26 = dev_get_drvdata(dev);
  243. snd_soc_component_update_bits(aic26->component, AIC26_REG_AUDIO_CTRL2,
  244. 0x8000, 0x800);
  245. return count;
  246. }
  247. static DEVICE_ATTR_RW(keyclick);
  248. /* ---------------------------------------------------------------------
  249. * SoC CODEC portion of driver: probe and release routines
  250. */
  251. static int aic26_probe(struct snd_soc_component *component)
  252. {
  253. struct aic26 *aic26 = dev_get_drvdata(component->dev);
  254. int ret, reg;
  255. aic26->component = component;
  256. /* Reset the codec to power on defaults */
  257. snd_soc_component_write(component, AIC26_REG_RESET, 0xBB00);
  258. /* Power up CODEC */
  259. snd_soc_component_write(component, AIC26_REG_POWER_CTRL, 0);
  260. /* Audio Control 3 (master mode, fsref rate) */
  261. reg = snd_soc_component_read(component, AIC26_REG_AUDIO_CTRL3);
  262. reg &= ~0xf800;
  263. reg |= 0x0800; /* set master mode */
  264. snd_soc_component_write(component, AIC26_REG_AUDIO_CTRL3, reg);
  265. /* Register the sysfs files for debugging */
  266. /* Create SysFS files */
  267. ret = device_create_file(component->dev, &dev_attr_keyclick);
  268. if (ret)
  269. dev_info(component->dev, "error creating sysfs files\n");
  270. return 0;
  271. }
  272. static const struct snd_soc_component_driver aic26_soc_component_dev = {
  273. .probe = aic26_probe,
  274. .controls = aic26_snd_controls,
  275. .num_controls = ARRAY_SIZE(aic26_snd_controls),
  276. .dapm_widgets = tlv320aic26_dapm_widgets,
  277. .num_dapm_widgets = ARRAY_SIZE(tlv320aic26_dapm_widgets),
  278. .dapm_routes = tlv320aic26_dapm_routes,
  279. .num_dapm_routes = ARRAY_SIZE(tlv320aic26_dapm_routes),
  280. .idle_bias_on = 1,
  281. .use_pmdown_time = 1,
  282. .endianness = 1,
  283. };
  284. static const struct regmap_config aic26_regmap = {
  285. .reg_bits = 16,
  286. .val_bits = 16,
  287. };
  288. /* ---------------------------------------------------------------------
  289. * SPI device portion of driver: probe and release routines and SPI
  290. * driver registration.
  291. */
  292. static int aic26_spi_probe(struct spi_device *spi)
  293. {
  294. struct aic26 *aic26;
  295. int ret;
  296. dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n");
  297. /* Allocate driver data */
  298. aic26 = devm_kzalloc(&spi->dev, sizeof *aic26, GFP_KERNEL);
  299. if (!aic26)
  300. return -ENOMEM;
  301. aic26->regmap = devm_regmap_init_spi(spi, &aic26_regmap);
  302. if (IS_ERR(aic26->regmap))
  303. return PTR_ERR(aic26->regmap);
  304. /* Initialize the driver data */
  305. aic26->spi = spi;
  306. dev_set_drvdata(&spi->dev, aic26);
  307. aic26->clock_provider = 1;
  308. ret = devm_snd_soc_register_component(&spi->dev,
  309. &aic26_soc_component_dev, &aic26_dai, 1);
  310. return ret;
  311. }
  312. static struct spi_driver aic26_spi = {
  313. .driver = {
  314. .name = "tlv320aic26-codec",
  315. },
  316. .probe = aic26_spi_probe,
  317. };
  318. module_spi_driver(aic26_spi);