ad1836.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Audio Codec driver supporting:
  3. * AD1835A, AD1836, AD1837A, AD1838A, AD1839A
  4. *
  5. * Copyright 2009-2011 Analog Devices Inc.
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/device.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/initval.h>
  18. #include <sound/soc.h>
  19. #include <sound/tlv.h>
  20. #include <linux/spi/spi.h>
  21. #include "ad1836.h"
  22. enum ad1836_type {
  23. AD1835,
  24. AD1836,
  25. AD1838,
  26. };
  27. /* codec private data */
  28. struct ad1836_priv {
  29. enum ad1836_type type;
  30. };
  31. /*
  32. * AD1836 volume/mute/de-emphasis etc. controls
  33. */
  34. static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"};
  35. static const struct soc_enum ad1836_deemp_enum =
  36. SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp);
  37. #define AD1836_DAC_VOLUME(x) \
  38. SOC_DOUBLE_R("DAC" #x " Playback Volume", AD1836_DAC_L_VOL(x), \
  39. AD1836_DAC_R_VOL(x), 0, 0x3FF, 0)
  40. #define AD1836_DAC_SWITCH(x) \
  41. SOC_DOUBLE("DAC" #x " Playback Switch", AD1836_DAC_CTRL2, \
  42. AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
  43. #define AD1836_ADC_SWITCH(x) \
  44. SOC_DOUBLE("ADC" #x " Capture Switch", AD1836_ADC_CTRL2, \
  45. AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
  46. static const struct snd_kcontrol_new ad183x_dac_controls[] = {
  47. AD1836_DAC_VOLUME(1),
  48. AD1836_DAC_SWITCH(1),
  49. AD1836_DAC_VOLUME(2),
  50. AD1836_DAC_SWITCH(2),
  51. AD1836_DAC_VOLUME(3),
  52. AD1836_DAC_SWITCH(3),
  53. AD1836_DAC_VOLUME(4),
  54. AD1836_DAC_SWITCH(4),
  55. };
  56. static const struct snd_soc_dapm_widget ad183x_dac_dapm_widgets[] = {
  57. SND_SOC_DAPM_OUTPUT("DAC1OUT"),
  58. SND_SOC_DAPM_OUTPUT("DAC2OUT"),
  59. SND_SOC_DAPM_OUTPUT("DAC3OUT"),
  60. SND_SOC_DAPM_OUTPUT("DAC4OUT"),
  61. };
  62. static const struct snd_soc_dapm_route ad183x_dac_routes[] = {
  63. { "DAC1OUT", NULL, "DAC" },
  64. { "DAC2OUT", NULL, "DAC" },
  65. { "DAC3OUT", NULL, "DAC" },
  66. { "DAC4OUT", NULL, "DAC" },
  67. };
  68. static const struct snd_kcontrol_new ad183x_adc_controls[] = {
  69. AD1836_ADC_SWITCH(1),
  70. AD1836_ADC_SWITCH(2),
  71. AD1836_ADC_SWITCH(3),
  72. };
  73. static const struct snd_soc_dapm_widget ad183x_adc_dapm_widgets[] = {
  74. SND_SOC_DAPM_INPUT("ADC1IN"),
  75. SND_SOC_DAPM_INPUT("ADC2IN"),
  76. };
  77. static const struct snd_soc_dapm_route ad183x_adc_routes[] = {
  78. { "ADC", NULL, "ADC1IN" },
  79. { "ADC", NULL, "ADC2IN" },
  80. };
  81. static const struct snd_kcontrol_new ad183x_controls[] = {
  82. /* ADC high-pass filter */
  83. SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1,
  84. AD1836_ADC_HIGHPASS_FILTER, 1, 0),
  85. /* DAC de-emphasis */
  86. SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum),
  87. };
  88. static const struct snd_soc_dapm_widget ad183x_dapm_widgets[] = {
  89. SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1,
  90. AD1836_DAC_POWERDOWN, 1),
  91. SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
  92. SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1,
  93. AD1836_ADC_POWERDOWN, 1, NULL, 0),
  94. };
  95. static const struct snd_soc_dapm_route ad183x_dapm_routes[] = {
  96. { "DAC", NULL, "ADC_PWR" },
  97. { "ADC", NULL, "ADC_PWR" },
  98. };
  99. static const DECLARE_TLV_DB_SCALE(ad1836_in_tlv, 0, 300, 0);
  100. static const struct snd_kcontrol_new ad1836_controls[] = {
  101. SOC_DOUBLE_TLV("ADC2 Capture Volume", AD1836_ADC_CTRL1, 3, 0, 4, 0,
  102. ad1836_in_tlv),
  103. };
  104. /*
  105. * DAI ops entries
  106. */
  107. static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai,
  108. unsigned int fmt)
  109. {
  110. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  111. /* at present, we support adc aux mode to interface with
  112. * blackfin sport tdm mode
  113. */
  114. case SND_SOC_DAIFMT_DSP_A:
  115. break;
  116. default:
  117. return -EINVAL;
  118. }
  119. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  120. case SND_SOC_DAIFMT_IB_IF:
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  126. /* ALCLK,ABCLK are both output, AD1836 can only be master */
  127. case SND_SOC_DAIFMT_CBM_CFM:
  128. break;
  129. default:
  130. return -EINVAL;
  131. }
  132. return 0;
  133. }
  134. static int ad1836_hw_params(struct snd_pcm_substream *substream,
  135. struct snd_pcm_hw_params *params,
  136. struct snd_soc_dai *dai)
  137. {
  138. int word_len = 0;
  139. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  140. struct snd_soc_codec *codec = rtd->codec;
  141. /* bit size */
  142. switch (params_format(params)) {
  143. case SNDRV_PCM_FORMAT_S16_LE:
  144. word_len = AD1836_WORD_LEN_16;
  145. break;
  146. case SNDRV_PCM_FORMAT_S20_3LE:
  147. word_len = AD1836_WORD_LEN_20;
  148. break;
  149. case SNDRV_PCM_FORMAT_S24_LE:
  150. case SNDRV_PCM_FORMAT_S32_LE:
  151. word_len = AD1836_WORD_LEN_24;
  152. break;
  153. }
  154. snd_soc_update_bits(codec, AD1836_DAC_CTRL1, AD1836_DAC_WORD_LEN_MASK,
  155. word_len << AD1836_DAC_WORD_LEN_OFFSET);
  156. snd_soc_update_bits(codec, AD1836_ADC_CTRL2, AD1836_ADC_WORD_LEN_MASK,
  157. word_len << AD1836_ADC_WORD_OFFSET);
  158. return 0;
  159. }
  160. static const struct snd_soc_dai_ops ad1836_dai_ops = {
  161. .hw_params = ad1836_hw_params,
  162. .set_fmt = ad1836_set_dai_fmt,
  163. };
  164. #define AD183X_DAI(_name, num_dacs, num_adcs) \
  165. { \
  166. .name = _name "-hifi", \
  167. .playback = { \
  168. .stream_name = "Playback", \
  169. .channels_min = 2, \
  170. .channels_max = (num_dacs) * 2, \
  171. .rates = SNDRV_PCM_RATE_48000, \
  172. .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
  173. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
  174. }, \
  175. .capture = { \
  176. .stream_name = "Capture", \
  177. .channels_min = 2, \
  178. .channels_max = (num_adcs) * 2, \
  179. .rates = SNDRV_PCM_RATE_48000, \
  180. .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
  181. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
  182. }, \
  183. .ops = &ad1836_dai_ops, \
  184. }
  185. static struct snd_soc_dai_driver ad183x_dais[] = {
  186. [AD1835] = AD183X_DAI("ad1835", 4, 1),
  187. [AD1836] = AD183X_DAI("ad1836", 3, 2),
  188. [AD1838] = AD183X_DAI("ad1838", 3, 1),
  189. };
  190. #ifdef CONFIG_PM
  191. static int ad1836_suspend(struct snd_soc_codec *codec)
  192. {
  193. /* reset clock control mode */
  194. return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
  195. AD1836_ADC_SERFMT_MASK, 0);
  196. }
  197. static int ad1836_resume(struct snd_soc_codec *codec)
  198. {
  199. /* restore clock control mode */
  200. return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
  201. AD1836_ADC_SERFMT_MASK, AD1836_ADC_AUX);
  202. }
  203. #else
  204. #define ad1836_suspend NULL
  205. #define ad1836_resume NULL
  206. #endif
  207. static int ad1836_probe(struct snd_soc_codec *codec)
  208. {
  209. struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec);
  210. struct snd_soc_dapm_context *dapm = &codec->dapm;
  211. int num_dacs, num_adcs;
  212. int ret = 0;
  213. int i;
  214. num_dacs = ad183x_dais[ad1836->type].playback.channels_max / 2;
  215. num_adcs = ad183x_dais[ad1836->type].capture.channels_max / 2;
  216. ret = snd_soc_codec_set_cache_io(codec, 4, 12, SND_SOC_SPI);
  217. if (ret < 0) {
  218. dev_err(codec->dev, "failed to set cache I/O: %d\n",
  219. ret);
  220. return ret;
  221. }
  222. /* default setting for ad1836 */
  223. /* de-emphasis: 48kHz, power-on dac */
  224. snd_soc_write(codec, AD1836_DAC_CTRL1, 0x300);
  225. /* unmute dac channels */
  226. snd_soc_write(codec, AD1836_DAC_CTRL2, 0x0);
  227. /* high-pass filter enable, power-on adc */
  228. snd_soc_write(codec, AD1836_ADC_CTRL1, 0x100);
  229. /* unmute adc channles, adc aux mode */
  230. snd_soc_write(codec, AD1836_ADC_CTRL2, 0x180);
  231. /* volume */
  232. for (i = 1; i <= num_dacs; ++i) {
  233. snd_soc_write(codec, AD1836_DAC_L_VOL(i), 0x3FF);
  234. snd_soc_write(codec, AD1836_DAC_R_VOL(i), 0x3FF);
  235. }
  236. if (ad1836->type == AD1836) {
  237. /* left/right diff:PGA/MUX */
  238. snd_soc_write(codec, AD1836_ADC_CTRL3, 0x3A);
  239. ret = snd_soc_add_codec_controls(codec, ad1836_controls,
  240. ARRAY_SIZE(ad1836_controls));
  241. if (ret)
  242. return ret;
  243. } else {
  244. snd_soc_write(codec, AD1836_ADC_CTRL3, 0x00);
  245. }
  246. ret = snd_soc_add_codec_controls(codec, ad183x_dac_controls, num_dacs * 2);
  247. if (ret)
  248. return ret;
  249. ret = snd_soc_add_codec_controls(codec, ad183x_adc_controls, num_adcs);
  250. if (ret)
  251. return ret;
  252. ret = snd_soc_dapm_new_controls(dapm, ad183x_dac_dapm_widgets, num_dacs);
  253. if (ret)
  254. return ret;
  255. ret = snd_soc_dapm_new_controls(dapm, ad183x_adc_dapm_widgets, num_adcs);
  256. if (ret)
  257. return ret;
  258. ret = snd_soc_dapm_add_routes(dapm, ad183x_dac_routes, num_dacs);
  259. if (ret)
  260. return ret;
  261. ret = snd_soc_dapm_add_routes(dapm, ad183x_adc_routes, num_adcs);
  262. if (ret)
  263. return ret;
  264. return ret;
  265. }
  266. /* power down chip */
  267. static int ad1836_remove(struct snd_soc_codec *codec)
  268. {
  269. /* reset clock control mode */
  270. return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
  271. AD1836_ADC_SERFMT_MASK, 0);
  272. }
  273. static struct snd_soc_codec_driver soc_codec_dev_ad1836 = {
  274. .probe = ad1836_probe,
  275. .remove = ad1836_remove,
  276. .suspend = ad1836_suspend,
  277. .resume = ad1836_resume,
  278. .reg_cache_size = AD1836_NUM_REGS,
  279. .reg_word_size = sizeof(u16),
  280. .controls = ad183x_controls,
  281. .num_controls = ARRAY_SIZE(ad183x_controls),
  282. .dapm_widgets = ad183x_dapm_widgets,
  283. .num_dapm_widgets = ARRAY_SIZE(ad183x_dapm_widgets),
  284. .dapm_routes = ad183x_dapm_routes,
  285. .num_dapm_routes = ARRAY_SIZE(ad183x_dapm_routes),
  286. };
  287. static int __devinit ad1836_spi_probe(struct spi_device *spi)
  288. {
  289. struct ad1836_priv *ad1836;
  290. int ret;
  291. ad1836 = devm_kzalloc(&spi->dev, sizeof(struct ad1836_priv),
  292. GFP_KERNEL);
  293. if (ad1836 == NULL)
  294. return -ENOMEM;
  295. ad1836->type = spi_get_device_id(spi)->driver_data;
  296. spi_set_drvdata(spi, ad1836);
  297. ret = snd_soc_register_codec(&spi->dev,
  298. &soc_codec_dev_ad1836, &ad183x_dais[ad1836->type], 1);
  299. return ret;
  300. }
  301. static int __devexit ad1836_spi_remove(struct spi_device *spi)
  302. {
  303. snd_soc_unregister_codec(&spi->dev);
  304. return 0;
  305. }
  306. static const struct spi_device_id ad1836_ids[] = {
  307. { "ad1835", AD1835 },
  308. { "ad1836", AD1836 },
  309. { "ad1837", AD1835 },
  310. { "ad1838", AD1838 },
  311. { "ad1839", AD1838 },
  312. { },
  313. };
  314. MODULE_DEVICE_TABLE(spi, ad1836_ids);
  315. static struct spi_driver ad1836_spi_driver = {
  316. .driver = {
  317. .name = "ad1836",
  318. .owner = THIS_MODULE,
  319. },
  320. .probe = ad1836_spi_probe,
  321. .remove = __devexit_p(ad1836_spi_remove),
  322. .id_table = ad1836_ids,
  323. };
  324. static int __init ad1836_init(void)
  325. {
  326. return spi_register_driver(&ad1836_spi_driver);
  327. }
  328. module_init(ad1836_init);
  329. static void __exit ad1836_exit(void)
  330. {
  331. spi_unregister_driver(&ad1836_spi_driver);
  332. }
  333. module_exit(ad1836_exit);
  334. MODULE_DESCRIPTION("ASoC ad1836 driver");
  335. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  336. MODULE_LICENSE("GPL");