ad1836.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * File: sound/soc/codecs/ad1836.c
  3. * Author: Barry Song <Barry.Song@analog.com>
  4. *
  5. * Created: Aug 04 2009
  6. * Description: Driver for AD1836 sound chip
  7. *
  8. * Modified:
  9. * Copyright 2009 Analog Devices Inc.
  10. *
  11. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/device.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/initval.h>
  27. #include <sound/soc.h>
  28. #include <sound/tlv.h>
  29. #include <linux/spi/spi.h>
  30. #include "ad1836.h"
  31. /* codec private data */
  32. struct ad1836_priv {
  33. enum snd_soc_control_type control_type;
  34. void *control_data;
  35. };
  36. /*
  37. * AD1836 volume/mute/de-emphasis etc. controls
  38. */
  39. static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"};
  40. static const struct soc_enum ad1836_deemp_enum =
  41. SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp);
  42. static const struct snd_kcontrol_new ad1836_snd_controls[] = {
  43. /* DAC volume control */
  44. SOC_DOUBLE_R("DAC1 Volume", AD1836_DAC_L1_VOL,
  45. AD1836_DAC_R1_VOL, 0, 0x3FF, 0),
  46. SOC_DOUBLE_R("DAC2 Volume", AD1836_DAC_L2_VOL,
  47. AD1836_DAC_R2_VOL, 0, 0x3FF, 0),
  48. SOC_DOUBLE_R("DAC3 Volume", AD1836_DAC_L3_VOL,
  49. AD1836_DAC_R3_VOL, 0, 0x3FF, 0),
  50. /* ADC switch control */
  51. SOC_DOUBLE("ADC1 Switch", AD1836_ADC_CTRL2, AD1836_ADCL1_MUTE,
  52. AD1836_ADCR1_MUTE, 1, 1),
  53. SOC_DOUBLE("ADC2 Switch", AD1836_ADC_CTRL2, AD1836_ADCL2_MUTE,
  54. AD1836_ADCR2_MUTE, 1, 1),
  55. /* DAC switch control */
  56. SOC_DOUBLE("DAC1 Switch", AD1836_DAC_CTRL2, AD1836_DACL1_MUTE,
  57. AD1836_DACR1_MUTE, 1, 1),
  58. SOC_DOUBLE("DAC2 Switch", AD1836_DAC_CTRL2, AD1836_DACL2_MUTE,
  59. AD1836_DACR2_MUTE, 1, 1),
  60. SOC_DOUBLE("DAC3 Switch", AD1836_DAC_CTRL2, AD1836_DACL3_MUTE,
  61. AD1836_DACR3_MUTE, 1, 1),
  62. /* ADC high-pass filter */
  63. SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1,
  64. AD1836_ADC_HIGHPASS_FILTER, 1, 0),
  65. /* DAC de-emphasis */
  66. SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum),
  67. };
  68. static const struct snd_soc_dapm_widget ad1836_dapm_widgets[] = {
  69. SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1,
  70. AD1836_DAC_POWERDOWN, 1),
  71. SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
  72. SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1,
  73. AD1836_ADC_POWERDOWN, 1, NULL, 0),
  74. SND_SOC_DAPM_OUTPUT("DAC1OUT"),
  75. SND_SOC_DAPM_OUTPUT("DAC2OUT"),
  76. SND_SOC_DAPM_OUTPUT("DAC3OUT"),
  77. SND_SOC_DAPM_INPUT("ADC1IN"),
  78. SND_SOC_DAPM_INPUT("ADC2IN"),
  79. };
  80. static const struct snd_soc_dapm_route audio_paths[] = {
  81. { "DAC", NULL, "ADC_PWR" },
  82. { "ADC", NULL, "ADC_PWR" },
  83. { "DAC1OUT", "DAC1 Switch", "DAC" },
  84. { "DAC2OUT", "DAC2 Switch", "DAC" },
  85. { "DAC3OUT", "DAC3 Switch", "DAC" },
  86. { "ADC", "ADC1 Switch", "ADC1IN" },
  87. { "ADC", "ADC2 Switch", "ADC2IN" },
  88. };
  89. /*
  90. * DAI ops entries
  91. */
  92. static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai,
  93. unsigned int fmt)
  94. {
  95. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  96. /* at present, we support adc aux mode to interface with
  97. * blackfin sport tdm mode
  98. */
  99. case SND_SOC_DAIFMT_DSP_A:
  100. break;
  101. default:
  102. return -EINVAL;
  103. }
  104. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  105. case SND_SOC_DAIFMT_IB_IF:
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  111. /* ALCLK,ABCLK are both output, AD1836 can only be master */
  112. case SND_SOC_DAIFMT_CBM_CFM:
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. return 0;
  118. }
  119. static int ad1836_hw_params(struct snd_pcm_substream *substream,
  120. struct snd_pcm_hw_params *params,
  121. struct snd_soc_dai *dai)
  122. {
  123. int word_len = 0;
  124. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  125. struct snd_soc_codec *codec = rtd->codec;
  126. /* bit size */
  127. switch (params_format(params)) {
  128. case SNDRV_PCM_FORMAT_S16_LE:
  129. word_len = AD1836_WORD_LEN_16;
  130. break;
  131. case SNDRV_PCM_FORMAT_S20_3LE:
  132. word_len = AD1836_WORD_LEN_20;
  133. break;
  134. case SNDRV_PCM_FORMAT_S24_LE:
  135. case SNDRV_PCM_FORMAT_S32_LE:
  136. word_len = AD1836_WORD_LEN_24;
  137. break;
  138. }
  139. snd_soc_update_bits(codec, AD1836_DAC_CTRL1, AD1836_DAC_WORD_LEN_MASK,
  140. word_len << AD1836_DAC_WORD_LEN_OFFSET);
  141. snd_soc_update_bits(codec, AD1836_ADC_CTRL2, AD1836_ADC_WORD_LEN_MASK,
  142. word_len << AD1836_ADC_WORD_OFFSET);
  143. return 0;
  144. }
  145. #ifdef CONFIG_PM
  146. static int ad1836_soc_suspend(struct snd_soc_codec *codec,
  147. pm_message_t state)
  148. {
  149. /* reset clock control mode */
  150. u16 adc_ctrl2 = snd_soc_read(codec, AD1836_ADC_CTRL2);
  151. adc_ctrl2 &= ~AD1836_ADC_SERFMT_MASK;
  152. return snd_soc_write(codec, AD1836_ADC_CTRL2, adc_ctrl2);
  153. }
  154. static int ad1836_soc_resume(struct snd_soc_codec *codec)
  155. {
  156. /* restore clock control mode */
  157. u16 adc_ctrl2 = snd_soc_read(codec, AD1836_ADC_CTRL2);
  158. adc_ctrl2 |= AD1836_ADC_AUX;
  159. return snd_soc_write(codec, AD1836_ADC_CTRL2, adc_ctrl2);
  160. }
  161. #else
  162. #define ad1836_soc_suspend NULL
  163. #define ad1836_soc_resume NULL
  164. #endif
  165. static struct snd_soc_dai_ops ad1836_dai_ops = {
  166. .hw_params = ad1836_hw_params,
  167. .set_fmt = ad1836_set_dai_fmt,
  168. };
  169. /* codec DAI instance */
  170. static struct snd_soc_dai_driver ad1836_dai = {
  171. .name = "ad1836-hifi",
  172. .playback = {
  173. .stream_name = "Playback",
  174. .channels_min = 2,
  175. .channels_max = 6,
  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 = 4,
  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 int ad1836_probe(struct snd_soc_codec *codec)
  191. {
  192. struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec);
  193. struct snd_soc_dapm_context *dapm = &codec->dapm;
  194. int ret = 0;
  195. codec->control_data = ad1836->control_data;
  196. ret = snd_soc_codec_set_cache_io(codec, 4, 12, SND_SOC_SPI);
  197. if (ret < 0) {
  198. dev_err(codec->dev, "failed to set cache I/O: %d\n",
  199. ret);
  200. return ret;
  201. }
  202. /* default setting for ad1836 */
  203. /* de-emphasis: 48kHz, power-on dac */
  204. snd_soc_write(codec, AD1836_DAC_CTRL1, 0x300);
  205. /* unmute dac channels */
  206. snd_soc_write(codec, AD1836_DAC_CTRL2, 0x0);
  207. /* high-pass filter enable, power-on adc */
  208. snd_soc_write(codec, AD1836_ADC_CTRL1, 0x100);
  209. /* unmute adc channles, adc aux mode */
  210. snd_soc_write(codec, AD1836_ADC_CTRL2, 0x180);
  211. /* left/right diff:PGA/MUX */
  212. snd_soc_write(codec, AD1836_ADC_CTRL3, 0x3A);
  213. /* volume */
  214. snd_soc_write(codec, AD1836_DAC_L1_VOL, 0x3FF);
  215. snd_soc_write(codec, AD1836_DAC_R1_VOL, 0x3FF);
  216. snd_soc_write(codec, AD1836_DAC_L2_VOL, 0x3FF);
  217. snd_soc_write(codec, AD1836_DAC_R2_VOL, 0x3FF);
  218. snd_soc_write(codec, AD1836_DAC_L3_VOL, 0x3FF);
  219. snd_soc_write(codec, AD1836_DAC_R3_VOL, 0x3FF);
  220. snd_soc_add_controls(codec, ad1836_snd_controls,
  221. ARRAY_SIZE(ad1836_snd_controls));
  222. snd_soc_dapm_new_controls(dapm, ad1836_dapm_widgets,
  223. ARRAY_SIZE(ad1836_dapm_widgets));
  224. snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
  225. return ret;
  226. }
  227. /* power down chip */
  228. static int ad1836_remove(struct snd_soc_codec *codec)
  229. {
  230. /* reset clock control mode */
  231. u16 adc_ctrl2 = snd_soc_read(codec, AD1836_ADC_CTRL2);
  232. adc_ctrl2 &= ~AD1836_ADC_SERFMT_MASK;
  233. return snd_soc_write(codec, AD1836_ADC_CTRL2, adc_ctrl2);
  234. }
  235. static struct snd_soc_codec_driver soc_codec_dev_ad1836 = {
  236. .probe = ad1836_probe,
  237. .remove = ad1836_remove,
  238. .suspend = ad1836_soc_suspend,
  239. .resume = ad1836_soc_resume,
  240. .reg_cache_size = AD1836_NUM_REGS,
  241. .reg_word_size = sizeof(u16),
  242. };
  243. static int __devinit ad1836_spi_probe(struct spi_device *spi)
  244. {
  245. struct ad1836_priv *ad1836;
  246. int ret;
  247. ad1836 = kzalloc(sizeof(struct ad1836_priv), GFP_KERNEL);
  248. if (ad1836 == NULL)
  249. return -ENOMEM;
  250. spi_set_drvdata(spi, ad1836);
  251. ad1836->control_data = spi;
  252. ad1836->control_type = SND_SOC_SPI;
  253. ret = snd_soc_register_codec(&spi->dev,
  254. &soc_codec_dev_ad1836, &ad1836_dai, 1);
  255. if (ret < 0)
  256. kfree(ad1836);
  257. return ret;
  258. }
  259. static int __devexit ad1836_spi_remove(struct spi_device *spi)
  260. {
  261. snd_soc_unregister_codec(&spi->dev);
  262. kfree(spi_get_drvdata(spi));
  263. return 0;
  264. }
  265. static struct spi_driver ad1836_spi_driver = {
  266. .driver = {
  267. .name = "ad1836-codec",
  268. .owner = THIS_MODULE,
  269. },
  270. .probe = ad1836_spi_probe,
  271. .remove = __devexit_p(ad1836_spi_remove),
  272. };
  273. static int __init ad1836_init(void)
  274. {
  275. int ret;
  276. ret = spi_register_driver(&ad1836_spi_driver);
  277. if (ret != 0) {
  278. printk(KERN_ERR "Failed to register ad1836 SPI driver: %d\n",
  279. ret);
  280. }
  281. return ret;
  282. }
  283. module_init(ad1836_init);
  284. static void __exit ad1836_exit(void)
  285. {
  286. spi_unregister_driver(&ad1836_spi_driver);
  287. }
  288. module_exit(ad1836_exit);
  289. MODULE_DESCRIPTION("ASoC ad1836 driver");
  290. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  291. MODULE_LICENSE("GPL");