ad193x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * AD193X Audio Codec driver supporting AD1936/7/8/9
  3. *
  4. * Copyright 2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/slab.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/initval.h>
  19. #include <sound/soc.h>
  20. #include <sound/tlv.h>
  21. #include "ad193x.h"
  22. /* codec private data */
  23. struct ad193x_priv {
  24. struct regmap *regmap;
  25. int sysclk;
  26. };
  27. /*
  28. * AD193X volume/mute/de-emphasis etc. controls
  29. */
  30. static const char * const ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
  31. static const struct soc_enum ad193x_deemp_enum =
  32. SOC_ENUM_SINGLE(AD193X_DAC_CTRL2, 1, 4, ad193x_deemp);
  33. static const DECLARE_TLV_DB_MINMAX(adau193x_tlv, -9563, 0);
  34. static const struct snd_kcontrol_new ad193x_snd_controls[] = {
  35. /* DAC volume control */
  36. SOC_DOUBLE_R_TLV("DAC1 Volume", AD193X_DAC_L1_VOL,
  37. AD193X_DAC_R1_VOL, 0, 0xFF, 1, adau193x_tlv),
  38. SOC_DOUBLE_R_TLV("DAC2 Volume", AD193X_DAC_L2_VOL,
  39. AD193X_DAC_R2_VOL, 0, 0xFF, 1, adau193x_tlv),
  40. SOC_DOUBLE_R_TLV("DAC3 Volume", AD193X_DAC_L3_VOL,
  41. AD193X_DAC_R3_VOL, 0, 0xFF, 1, adau193x_tlv),
  42. SOC_DOUBLE_R_TLV("DAC4 Volume", AD193X_DAC_L4_VOL,
  43. AD193X_DAC_R4_VOL, 0, 0xFF, 1, adau193x_tlv),
  44. /* ADC switch control */
  45. SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE,
  46. AD193X_ADCR1_MUTE, 1, 1),
  47. SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE,
  48. AD193X_ADCR2_MUTE, 1, 1),
  49. /* DAC switch control */
  50. SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE,
  51. AD193X_DACR1_MUTE, 1, 1),
  52. SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE,
  53. AD193X_DACR2_MUTE, 1, 1),
  54. SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE,
  55. AD193X_DACR3_MUTE, 1, 1),
  56. SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE,
  57. AD193X_DACR4_MUTE, 1, 1),
  58. /* ADC high-pass filter */
  59. SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0,
  60. AD193X_ADC_HIGHPASS_FILTER, 1, 0),
  61. /* DAC de-emphasis */
  62. SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum),
  63. };
  64. static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = {
  65. SND_SOC_DAPM_DAC("DAC", "Playback", AD193X_DAC_CTRL0, 0, 1),
  66. SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
  67. SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0),
  68. SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0),
  69. SND_SOC_DAPM_SUPPLY("SYSCLK", AD193X_PLL_CLK_CTRL0, 7, 0, NULL, 0),
  70. SND_SOC_DAPM_OUTPUT("DAC1OUT"),
  71. SND_SOC_DAPM_OUTPUT("DAC2OUT"),
  72. SND_SOC_DAPM_OUTPUT("DAC3OUT"),
  73. SND_SOC_DAPM_OUTPUT("DAC4OUT"),
  74. SND_SOC_DAPM_INPUT("ADC1IN"),
  75. SND_SOC_DAPM_INPUT("ADC2IN"),
  76. };
  77. static const struct snd_soc_dapm_route audio_paths[] = {
  78. { "DAC", NULL, "SYSCLK" },
  79. { "ADC", NULL, "SYSCLK" },
  80. { "DAC", NULL, "ADC_PWR" },
  81. { "ADC", NULL, "ADC_PWR" },
  82. { "DAC1OUT", NULL, "DAC" },
  83. { "DAC2OUT", NULL, "DAC" },
  84. { "DAC3OUT", NULL, "DAC" },
  85. { "DAC4OUT", NULL, "DAC" },
  86. { "ADC", NULL, "ADC1IN" },
  87. { "ADC", NULL, "ADC2IN" },
  88. { "SYSCLK", NULL, "PLL_PWR" },
  89. };
  90. /*
  91. * DAI ops entries
  92. */
  93. static int ad193x_mute(struct snd_soc_dai *dai, int mute)
  94. {
  95. struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(dai->codec);
  96. if (mute)
  97. regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
  98. AD193X_DAC_MASTER_MUTE,
  99. AD193X_DAC_MASTER_MUTE);
  100. else
  101. regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
  102. AD193X_DAC_MASTER_MUTE, 0);
  103. return 0;
  104. }
  105. static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  106. unsigned int rx_mask, int slots, int width)
  107. {
  108. struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(dai->codec);
  109. unsigned int channels;
  110. switch (slots) {
  111. case 2:
  112. channels = AD193X_2_CHANNELS;
  113. break;
  114. case 4:
  115. channels = AD193X_4_CHANNELS;
  116. break;
  117. case 8:
  118. channels = AD193X_8_CHANNELS;
  119. break;
  120. case 16:
  121. channels = AD193X_16_CHANNELS;
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
  127. AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT);
  128. regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
  129. AD193X_ADC_CHAN_MASK, channels << AD193X_ADC_CHAN_SHFT);
  130. return 0;
  131. }
  132. static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
  133. unsigned int fmt)
  134. {
  135. struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec_dai->codec);
  136. unsigned int adc_serfmt = 0;
  137. unsigned int adc_fmt = 0;
  138. unsigned int dac_fmt = 0;
  139. /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
  140. * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
  141. */
  142. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  143. case SND_SOC_DAIFMT_I2S:
  144. adc_serfmt |= AD193X_ADC_SERFMT_TDM;
  145. break;
  146. case SND_SOC_DAIFMT_DSP_A:
  147. adc_serfmt |= AD193X_ADC_SERFMT_AUX;
  148. break;
  149. default:
  150. return -EINVAL;
  151. }
  152. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  153. case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
  154. break;
  155. case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
  156. adc_fmt |= AD193X_ADC_LEFT_HIGH;
  157. dac_fmt |= AD193X_DAC_LEFT_HIGH;
  158. break;
  159. case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
  160. adc_fmt |= AD193X_ADC_BCLK_INV;
  161. dac_fmt |= AD193X_DAC_BCLK_INV;
  162. break;
  163. case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
  164. adc_fmt |= AD193X_ADC_LEFT_HIGH;
  165. adc_fmt |= AD193X_ADC_BCLK_INV;
  166. dac_fmt |= AD193X_DAC_LEFT_HIGH;
  167. dac_fmt |= AD193X_DAC_BCLK_INV;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  173. case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
  174. adc_fmt |= AD193X_ADC_LCR_MASTER;
  175. adc_fmt |= AD193X_ADC_BCLK_MASTER;
  176. dac_fmt |= AD193X_DAC_LCR_MASTER;
  177. dac_fmt |= AD193X_DAC_BCLK_MASTER;
  178. break;
  179. case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
  180. adc_fmt |= AD193X_ADC_LCR_MASTER;
  181. dac_fmt |= AD193X_DAC_LCR_MASTER;
  182. break;
  183. case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
  184. adc_fmt |= AD193X_ADC_BCLK_MASTER;
  185. dac_fmt |= AD193X_DAC_BCLK_MASTER;
  186. break;
  187. case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
  188. break;
  189. default:
  190. return -EINVAL;
  191. }
  192. regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
  193. AD193X_ADC_SERFMT_MASK, adc_serfmt);
  194. regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
  195. AD193X_ADC_FMT_MASK, adc_fmt);
  196. regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
  197. AD193X_DAC_FMT_MASK, dac_fmt);
  198. return 0;
  199. }
  200. static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  201. int clk_id, unsigned int freq, int dir)
  202. {
  203. struct snd_soc_codec *codec = codec_dai->codec;
  204. struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
  205. switch (freq) {
  206. case 12288000:
  207. case 18432000:
  208. case 24576000:
  209. case 36864000:
  210. ad193x->sysclk = freq;
  211. return 0;
  212. }
  213. return -EINVAL;
  214. }
  215. static int ad193x_hw_params(struct snd_pcm_substream *substream,
  216. struct snd_pcm_hw_params *params,
  217. struct snd_soc_dai *dai)
  218. {
  219. int word_len = 0, master_rate = 0;
  220. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  221. struct snd_soc_codec *codec = rtd->codec;
  222. struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
  223. /* bit size */
  224. switch (params_format(params)) {
  225. case SNDRV_PCM_FORMAT_S16_LE:
  226. word_len = 3;
  227. break;
  228. case SNDRV_PCM_FORMAT_S20_3LE:
  229. word_len = 1;
  230. break;
  231. case SNDRV_PCM_FORMAT_S24_LE:
  232. case SNDRV_PCM_FORMAT_S32_LE:
  233. word_len = 0;
  234. break;
  235. }
  236. switch (ad193x->sysclk) {
  237. case 12288000:
  238. master_rate = AD193X_PLL_INPUT_256;
  239. break;
  240. case 18432000:
  241. master_rate = AD193X_PLL_INPUT_384;
  242. break;
  243. case 24576000:
  244. master_rate = AD193X_PLL_INPUT_512;
  245. break;
  246. case 36864000:
  247. master_rate = AD193X_PLL_INPUT_768;
  248. break;
  249. }
  250. regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0,
  251. AD193X_PLL_INPUT_MASK, master_rate);
  252. regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
  253. AD193X_DAC_WORD_LEN_MASK,
  254. word_len << AD193X_DAC_WORD_LEN_SHFT);
  255. regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
  256. AD193X_ADC_WORD_LEN_MASK, word_len);
  257. return 0;
  258. }
  259. static const struct snd_soc_dai_ops ad193x_dai_ops = {
  260. .hw_params = ad193x_hw_params,
  261. .digital_mute = ad193x_mute,
  262. .set_tdm_slot = ad193x_set_tdm_slot,
  263. .set_sysclk = ad193x_set_dai_sysclk,
  264. .set_fmt = ad193x_set_dai_fmt,
  265. };
  266. /* codec DAI instance */
  267. static struct snd_soc_dai_driver ad193x_dai = {
  268. .name = "ad193x-hifi",
  269. .playback = {
  270. .stream_name = "Playback",
  271. .channels_min = 2,
  272. .channels_max = 8,
  273. .rates = SNDRV_PCM_RATE_48000,
  274. .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
  275. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
  276. },
  277. .capture = {
  278. .stream_name = "Capture",
  279. .channels_min = 2,
  280. .channels_max = 4,
  281. .rates = SNDRV_PCM_RATE_48000,
  282. .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
  283. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
  284. },
  285. .ops = &ad193x_dai_ops,
  286. };
  287. static int ad193x_probe(struct snd_soc_codec *codec)
  288. {
  289. struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
  290. int ret;
  291. codec->control_data = ad193x->regmap;
  292. ret = snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
  293. if (ret < 0) {
  294. dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
  295. return ret;
  296. }
  297. /* default setting for ad193x */
  298. /* unmute dac channels */
  299. regmap_write(ad193x->regmap, AD193X_DAC_CHNL_MUTE, 0x0);
  300. /* de-emphasis: 48kHz, powedown dac */
  301. regmap_write(ad193x->regmap, AD193X_DAC_CTRL2, 0x1A);
  302. /* powerdown dac, dac in tdm mode */
  303. regmap_write(ad193x->regmap, AD193X_DAC_CTRL0, 0x41);
  304. /* high-pass filter enable */
  305. regmap_write(ad193x->regmap, AD193X_ADC_CTRL0, 0x3);
  306. /* sata delay=1, adc aux mode */
  307. regmap_write(ad193x->regmap, AD193X_ADC_CTRL1, 0x43);
  308. /* pll input: mclki/xi */
  309. regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
  310. regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL1, 0x04);
  311. return ret;
  312. }
  313. static struct snd_soc_codec_driver soc_codec_dev_ad193x = {
  314. .probe = ad193x_probe,
  315. .controls = ad193x_snd_controls,
  316. .num_controls = ARRAY_SIZE(ad193x_snd_controls),
  317. .dapm_widgets = ad193x_dapm_widgets,
  318. .num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets),
  319. .dapm_routes = audio_paths,
  320. .num_dapm_routes = ARRAY_SIZE(audio_paths),
  321. };
  322. static bool adau193x_reg_volatile(struct device *dev, unsigned int reg)
  323. {
  324. return false;
  325. }
  326. #if defined(CONFIG_SPI_MASTER)
  327. static const struct regmap_config ad193x_spi_regmap_config = {
  328. .val_bits = 8,
  329. .reg_bits = 16,
  330. .read_flag_mask = 0x09,
  331. .write_flag_mask = 0x08,
  332. .max_register = AD193X_NUM_REGS - 1,
  333. .volatile_reg = adau193x_reg_volatile,
  334. };
  335. static int __devinit ad193x_spi_probe(struct spi_device *spi)
  336. {
  337. struct ad193x_priv *ad193x;
  338. int ret;
  339. ad193x = devm_kzalloc(&spi->dev, sizeof(struct ad193x_priv),
  340. GFP_KERNEL);
  341. if (ad193x == NULL)
  342. return -ENOMEM;
  343. ad193x->regmap = regmap_init_spi(spi, &ad193x_spi_regmap_config);
  344. if (IS_ERR(ad193x->regmap)) {
  345. ret = PTR_ERR(ad193x->regmap);
  346. goto err_out;
  347. }
  348. spi_set_drvdata(spi, ad193x);
  349. ret = snd_soc_register_codec(&spi->dev,
  350. &soc_codec_dev_ad193x, &ad193x_dai, 1);
  351. if (ret < 0)
  352. goto err_regmap_exit;
  353. return 0;
  354. err_regmap_exit:
  355. regmap_exit(ad193x->regmap);
  356. err_out:
  357. return ret;
  358. }
  359. static int __devexit ad193x_spi_remove(struct spi_device *spi)
  360. {
  361. struct ad193x_priv *ad193x = spi_get_drvdata(spi);
  362. snd_soc_unregister_codec(&spi->dev);
  363. regmap_exit(ad193x->regmap);
  364. return 0;
  365. }
  366. static struct spi_driver ad193x_spi_driver = {
  367. .driver = {
  368. .name = "ad193x",
  369. .owner = THIS_MODULE,
  370. },
  371. .probe = ad193x_spi_probe,
  372. .remove = __devexit_p(ad193x_spi_remove),
  373. };
  374. #endif
  375. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  376. static const struct regmap_config ad193x_i2c_regmap_config = {
  377. .val_bits = 8,
  378. .reg_bits = 8,
  379. .max_register = AD193X_NUM_REGS - 1,
  380. .volatile_reg = adau193x_reg_volatile,
  381. };
  382. static const struct i2c_device_id ad193x_id[] = {
  383. { "ad1936", 0 },
  384. { "ad1937", 0 },
  385. { }
  386. };
  387. MODULE_DEVICE_TABLE(i2c, ad193x_id);
  388. static int __devinit ad193x_i2c_probe(struct i2c_client *client,
  389. const struct i2c_device_id *id)
  390. {
  391. struct ad193x_priv *ad193x;
  392. int ret;
  393. ad193x = devm_kzalloc(&client->dev, sizeof(struct ad193x_priv),
  394. GFP_KERNEL);
  395. if (ad193x == NULL)
  396. return -ENOMEM;
  397. ad193x->regmap = regmap_init_i2c(client, &ad193x_i2c_regmap_config);
  398. if (IS_ERR(ad193x->regmap)) {
  399. ret = PTR_ERR(ad193x->regmap);
  400. goto err_out;
  401. }
  402. i2c_set_clientdata(client, ad193x);
  403. ret = snd_soc_register_codec(&client->dev,
  404. &soc_codec_dev_ad193x, &ad193x_dai, 1);
  405. if (ret < 0)
  406. goto err_regmap_exit;
  407. return 0;
  408. err_regmap_exit:
  409. regmap_exit(ad193x->regmap);
  410. err_out:
  411. return ret;
  412. }
  413. static int __devexit ad193x_i2c_remove(struct i2c_client *client)
  414. {
  415. struct ad193x_priv *ad193x = i2c_get_clientdata(client);
  416. snd_soc_unregister_codec(&client->dev);
  417. regmap_exit(ad193x->regmap);
  418. return 0;
  419. }
  420. static struct i2c_driver ad193x_i2c_driver = {
  421. .driver = {
  422. .name = "ad193x",
  423. },
  424. .probe = ad193x_i2c_probe,
  425. .remove = __devexit_p(ad193x_i2c_remove),
  426. .id_table = ad193x_id,
  427. };
  428. #endif
  429. static int __init ad193x_modinit(void)
  430. {
  431. int ret;
  432. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  433. ret = i2c_add_driver(&ad193x_i2c_driver);
  434. if (ret != 0) {
  435. printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n",
  436. ret);
  437. }
  438. #endif
  439. #if defined(CONFIG_SPI_MASTER)
  440. ret = spi_register_driver(&ad193x_spi_driver);
  441. if (ret != 0) {
  442. printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n",
  443. ret);
  444. }
  445. #endif
  446. return ret;
  447. }
  448. module_init(ad193x_modinit);
  449. static void __exit ad193x_modexit(void)
  450. {
  451. #if defined(CONFIG_SPI_MASTER)
  452. spi_unregister_driver(&ad193x_spi_driver);
  453. #endif
  454. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  455. i2c_del_driver(&ad193x_i2c_driver);
  456. #endif
  457. }
  458. module_exit(ad193x_modexit);
  459. MODULE_DESCRIPTION("ASoC ad193x driver");
  460. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  461. MODULE_LICENSE("GPL");