ad1836.c 10 KB

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