adau1701.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Driver for ADAU1701 SigmaDSP processor
  3. *
  4. * Copyright 2011 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. * based on an inital version by Cliff Cai <cliff.cai@analog.com>
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/i2c.h>
  13. #include <linux/delay.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/soc.h>
  19. #include "sigmadsp.h"
  20. #include "adau1701.h"
  21. #define ADAU1701_DSPCTRL 0x1c
  22. #define ADAU1701_SEROCTL 0x1e
  23. #define ADAU1701_SERICTL 0x1f
  24. #define ADAU1701_AUXNPOW 0x22
  25. #define ADAU1701_OSCIPOW 0x26
  26. #define ADAU1701_DACSET 0x27
  27. #define ADAU1701_NUM_REGS 0x28
  28. #define ADAU1701_DSPCTRL_CR (1 << 2)
  29. #define ADAU1701_DSPCTRL_DAM (1 << 3)
  30. #define ADAU1701_DSPCTRL_ADM (1 << 4)
  31. #define ADAU1701_DSPCTRL_SR_48 0x00
  32. #define ADAU1701_DSPCTRL_SR_96 0x01
  33. #define ADAU1701_DSPCTRL_SR_192 0x02
  34. #define ADAU1701_DSPCTRL_SR_MASK 0x03
  35. #define ADAU1701_SEROCTL_INV_LRCLK 0x2000
  36. #define ADAU1701_SEROCTL_INV_BCLK 0x1000
  37. #define ADAU1701_SEROCTL_MASTER 0x0800
  38. #define ADAU1701_SEROCTL_OBF16 0x0000
  39. #define ADAU1701_SEROCTL_OBF8 0x0200
  40. #define ADAU1701_SEROCTL_OBF4 0x0400
  41. #define ADAU1701_SEROCTL_OBF2 0x0600
  42. #define ADAU1701_SEROCTL_OBF_MASK 0x0600
  43. #define ADAU1701_SEROCTL_OLF1024 0x0000
  44. #define ADAU1701_SEROCTL_OLF512 0x0080
  45. #define ADAU1701_SEROCTL_OLF256 0x0100
  46. #define ADAU1701_SEROCTL_OLF_MASK 0x0180
  47. #define ADAU1701_SEROCTL_MSB_DEALY1 0x0000
  48. #define ADAU1701_SEROCTL_MSB_DEALY0 0x0004
  49. #define ADAU1701_SEROCTL_MSB_DEALY8 0x0008
  50. #define ADAU1701_SEROCTL_MSB_DEALY12 0x000c
  51. #define ADAU1701_SEROCTL_MSB_DEALY16 0x0010
  52. #define ADAU1701_SEROCTL_MSB_DEALY_MASK 0x001c
  53. #define ADAU1701_SEROCTL_WORD_LEN_24 0x0000
  54. #define ADAU1701_SEROCTL_WORD_LEN_20 0x0001
  55. #define ADAU1701_SEROCTL_WORD_LEN_16 0x0002
  56. #define ADAU1701_SEROCTL_WORD_LEN_MASK 0x0003
  57. #define ADAU1701_AUXNPOW_VBPD 0x40
  58. #define ADAU1701_AUXNPOW_VRPD 0x20
  59. #define ADAU1701_SERICTL_I2S 0
  60. #define ADAU1701_SERICTL_LEFTJ 1
  61. #define ADAU1701_SERICTL_TDM 2
  62. #define ADAU1701_SERICTL_RIGHTJ_24 3
  63. #define ADAU1701_SERICTL_RIGHTJ_20 4
  64. #define ADAU1701_SERICTL_RIGHTJ_18 5
  65. #define ADAU1701_SERICTL_RIGHTJ_16 6
  66. #define ADAU1701_SERICTL_MODE_MASK 7
  67. #define ADAU1701_SERICTL_INV_BCLK BIT(3)
  68. #define ADAU1701_SERICTL_INV_LRCLK BIT(4)
  69. #define ADAU1701_OSCIPOW_OPD 0x04
  70. #define ADAU1701_DACSET_DACINIT 1
  71. #define ADAU1701_FIRMWARE "adau1701.bin"
  72. struct adau1701 {
  73. unsigned int dai_fmt;
  74. };
  75. static const struct snd_kcontrol_new adau1701_controls[] = {
  76. SOC_SINGLE("Master Capture Switch", ADAU1701_DSPCTRL, 4, 1, 0),
  77. };
  78. static const struct snd_soc_dapm_widget adau1701_dapm_widgets[] = {
  79. SND_SOC_DAPM_DAC("DAC0", "Playback", ADAU1701_AUXNPOW, 3, 1),
  80. SND_SOC_DAPM_DAC("DAC1", "Playback", ADAU1701_AUXNPOW, 2, 1),
  81. SND_SOC_DAPM_DAC("DAC2", "Playback", ADAU1701_AUXNPOW, 1, 1),
  82. SND_SOC_DAPM_DAC("DAC3", "Playback", ADAU1701_AUXNPOW, 0, 1),
  83. SND_SOC_DAPM_ADC("ADC", "Capture", ADAU1701_AUXNPOW, 7, 1),
  84. SND_SOC_DAPM_OUTPUT("OUT0"),
  85. SND_SOC_DAPM_OUTPUT("OUT1"),
  86. SND_SOC_DAPM_OUTPUT("OUT2"),
  87. SND_SOC_DAPM_OUTPUT("OUT3"),
  88. SND_SOC_DAPM_INPUT("IN0"),
  89. SND_SOC_DAPM_INPUT("IN1"),
  90. };
  91. static const struct snd_soc_dapm_route adau1701_dapm_routes[] = {
  92. { "OUT0", NULL, "DAC0" },
  93. { "OUT1", NULL, "DAC1" },
  94. { "OUT2", NULL, "DAC2" },
  95. { "OUT3", NULL, "DAC3" },
  96. { "ADC", NULL, "IN0" },
  97. { "ADC", NULL, "IN1" },
  98. };
  99. static unsigned int adau1701_register_size(struct snd_soc_codec *codec,
  100. unsigned int reg)
  101. {
  102. switch (reg) {
  103. case ADAU1701_DSPCTRL:
  104. case ADAU1701_SEROCTL:
  105. case ADAU1701_AUXNPOW:
  106. case ADAU1701_OSCIPOW:
  107. case ADAU1701_DACSET:
  108. return 2;
  109. case ADAU1701_SERICTL:
  110. return 1;
  111. }
  112. dev_err(codec->dev, "Unsupported register address: %d\n", reg);
  113. return 0;
  114. }
  115. static int adau1701_write(struct snd_soc_codec *codec, unsigned int reg,
  116. unsigned int value)
  117. {
  118. unsigned int i;
  119. unsigned int size;
  120. uint8_t buf[4];
  121. int ret;
  122. size = adau1701_register_size(codec, reg);
  123. if (size == 0)
  124. return -EINVAL;
  125. snd_soc_cache_write(codec, reg, value);
  126. buf[0] = 0x08;
  127. buf[1] = reg;
  128. for (i = size + 1; i >= 2; --i) {
  129. buf[i] = value;
  130. value >>= 8;
  131. }
  132. ret = i2c_master_send(to_i2c_client(codec->dev), buf, size + 2);
  133. if (ret == size + 2)
  134. return 0;
  135. else if (ret < 0)
  136. return ret;
  137. else
  138. return -EIO;
  139. }
  140. static unsigned int adau1701_read(struct snd_soc_codec *codec, unsigned int reg)
  141. {
  142. unsigned int value;
  143. unsigned int ret;
  144. ret = snd_soc_cache_read(codec, reg, &value);
  145. if (ret)
  146. return ret;
  147. return value;
  148. }
  149. static int adau1701_load_firmware(struct snd_soc_codec *codec)
  150. {
  151. return process_sigma_firmware(codec->control_data, ADAU1701_FIRMWARE);
  152. }
  153. static int adau1701_set_capture_pcm_format(struct snd_soc_codec *codec,
  154. snd_pcm_format_t format)
  155. {
  156. struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
  157. unsigned int mask = ADAU1701_SEROCTL_WORD_LEN_MASK;
  158. unsigned int val;
  159. switch (format) {
  160. case SNDRV_PCM_FORMAT_S16_LE:
  161. val = ADAU1701_SEROCTL_WORD_LEN_16;
  162. break;
  163. case SNDRV_PCM_FORMAT_S20_3LE:
  164. val = ADAU1701_SEROCTL_WORD_LEN_20;
  165. break;
  166. case SNDRV_PCM_FORMAT_S24_LE:
  167. val = ADAU1701_SEROCTL_WORD_LEN_24;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. if (adau1701->dai_fmt == SND_SOC_DAIFMT_RIGHT_J) {
  173. switch (format) {
  174. case SNDRV_PCM_FORMAT_S16_LE:
  175. val |= ADAU1701_SEROCTL_MSB_DEALY16;
  176. break;
  177. case SNDRV_PCM_FORMAT_S20_3LE:
  178. val |= ADAU1701_SEROCTL_MSB_DEALY12;
  179. break;
  180. case SNDRV_PCM_FORMAT_S24_LE:
  181. val |= ADAU1701_SEROCTL_MSB_DEALY8;
  182. break;
  183. }
  184. mask |= ADAU1701_SEROCTL_MSB_DEALY_MASK;
  185. }
  186. snd_soc_update_bits(codec, ADAU1701_SEROCTL, mask, val);
  187. return 0;
  188. }
  189. static int adau1701_set_playback_pcm_format(struct snd_soc_codec *codec,
  190. snd_pcm_format_t format)
  191. {
  192. struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
  193. unsigned int val;
  194. if (adau1701->dai_fmt != SND_SOC_DAIFMT_RIGHT_J)
  195. return 0;
  196. switch (format) {
  197. case SNDRV_PCM_FORMAT_S16_LE:
  198. val = ADAU1701_SERICTL_RIGHTJ_16;
  199. break;
  200. case SNDRV_PCM_FORMAT_S20_3LE:
  201. val = ADAU1701_SERICTL_RIGHTJ_20;
  202. break;
  203. case SNDRV_PCM_FORMAT_S24_LE:
  204. val = ADAU1701_SERICTL_RIGHTJ_24;
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. snd_soc_update_bits(codec, ADAU1701_SERICTL,
  210. ADAU1701_SERICTL_MODE_MASK, val);
  211. return 0;
  212. }
  213. static int adau1701_hw_params(struct snd_pcm_substream *substream,
  214. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  215. {
  216. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  217. struct snd_soc_codec *codec = rtd->codec;
  218. snd_pcm_format_t format;
  219. unsigned int val;
  220. switch (params_rate(params)) {
  221. case 192000:
  222. val = ADAU1701_DSPCTRL_SR_192;
  223. break;
  224. case 96000:
  225. val = ADAU1701_DSPCTRL_SR_96;
  226. break;
  227. case 48000:
  228. val = ADAU1701_DSPCTRL_SR_48;
  229. break;
  230. default:
  231. return -EINVAL;
  232. }
  233. snd_soc_update_bits(codec, ADAU1701_DSPCTRL,
  234. ADAU1701_DSPCTRL_SR_MASK, val);
  235. format = params_format(params);
  236. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  237. return adau1701_set_playback_pcm_format(codec, format);
  238. else
  239. return adau1701_set_capture_pcm_format(codec, format);
  240. }
  241. static int adau1701_set_dai_fmt(struct snd_soc_dai *codec_dai,
  242. unsigned int fmt)
  243. {
  244. struct snd_soc_codec *codec = codec_dai->codec;
  245. struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
  246. unsigned int serictl = 0x00, seroctl = 0x00;
  247. bool invert_lrclk;
  248. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  249. case SND_SOC_DAIFMT_CBM_CFM:
  250. /* master, 64-bits per sample, 1 frame per sample */
  251. seroctl |= ADAU1701_SEROCTL_MASTER | ADAU1701_SEROCTL_OBF16
  252. | ADAU1701_SEROCTL_OLF1024;
  253. break;
  254. case SND_SOC_DAIFMT_CBS_CFS:
  255. break;
  256. default:
  257. return -EINVAL;
  258. }
  259. /* clock inversion */
  260. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  261. case SND_SOC_DAIFMT_NB_NF:
  262. invert_lrclk = false;
  263. break;
  264. case SND_SOC_DAIFMT_NB_IF:
  265. invert_lrclk = true;
  266. break;
  267. case SND_SOC_DAIFMT_IB_NF:
  268. invert_lrclk = false;
  269. serictl |= ADAU1701_SERICTL_INV_BCLK;
  270. seroctl |= ADAU1701_SEROCTL_INV_BCLK;
  271. break;
  272. case SND_SOC_DAIFMT_IB_IF:
  273. invert_lrclk = true;
  274. serictl |= ADAU1701_SERICTL_INV_BCLK;
  275. seroctl |= ADAU1701_SEROCTL_INV_BCLK;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  281. case SND_SOC_DAIFMT_I2S:
  282. break;
  283. case SND_SOC_DAIFMT_LEFT_J:
  284. serictl |= ADAU1701_SERICTL_LEFTJ;
  285. seroctl |= ADAU1701_SEROCTL_MSB_DEALY0;
  286. invert_lrclk = !invert_lrclk;
  287. break;
  288. case SND_SOC_DAIFMT_RIGHT_J:
  289. serictl |= ADAU1701_SERICTL_RIGHTJ_24;
  290. seroctl |= ADAU1701_SEROCTL_MSB_DEALY8;
  291. invert_lrclk = !invert_lrclk;
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. if (invert_lrclk) {
  297. seroctl |= ADAU1701_SEROCTL_INV_LRCLK;
  298. serictl |= ADAU1701_SERICTL_INV_LRCLK;
  299. }
  300. adau1701->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  301. snd_soc_write(codec, ADAU1701_SERICTL, serictl);
  302. snd_soc_update_bits(codec, ADAU1701_SEROCTL,
  303. ~ADAU1701_SEROCTL_WORD_LEN_MASK, seroctl);
  304. return 0;
  305. }
  306. static int adau1701_set_bias_level(struct snd_soc_codec *codec,
  307. enum snd_soc_bias_level level)
  308. {
  309. unsigned int mask = ADAU1701_AUXNPOW_VBPD | ADAU1701_AUXNPOW_VRPD;
  310. switch (level) {
  311. case SND_SOC_BIAS_ON:
  312. break;
  313. case SND_SOC_BIAS_PREPARE:
  314. break;
  315. case SND_SOC_BIAS_STANDBY:
  316. /* Enable VREF and VREF buffer */
  317. snd_soc_update_bits(codec, ADAU1701_AUXNPOW, mask, 0x00);
  318. break;
  319. case SND_SOC_BIAS_OFF:
  320. /* Disable VREF and VREF buffer */
  321. snd_soc_update_bits(codec, ADAU1701_AUXNPOW, mask, mask);
  322. break;
  323. }
  324. codec->dapm.bias_level = level;
  325. return 0;
  326. }
  327. static int adau1701_digital_mute(struct snd_soc_dai *dai, int mute)
  328. {
  329. struct snd_soc_codec *codec = dai->codec;
  330. unsigned int mask = ADAU1701_DSPCTRL_DAM;
  331. unsigned int val;
  332. if (mute)
  333. val = 0;
  334. else
  335. val = mask;
  336. snd_soc_update_bits(codec, ADAU1701_DSPCTRL, mask, val);
  337. return 0;
  338. }
  339. static int adau1701_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  340. int source, unsigned int freq, int dir)
  341. {
  342. unsigned int val;
  343. switch (clk_id) {
  344. case ADAU1701_CLK_SRC_OSC:
  345. val = 0x0;
  346. break;
  347. case ADAU1701_CLK_SRC_MCLK:
  348. val = ADAU1701_OSCIPOW_OPD;
  349. break;
  350. default:
  351. return -EINVAL;
  352. }
  353. snd_soc_update_bits(codec, ADAU1701_OSCIPOW, ADAU1701_OSCIPOW_OPD, val);
  354. return 0;
  355. }
  356. #define ADAU1701_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
  357. SNDRV_PCM_RATE_192000)
  358. #define ADAU1701_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  359. SNDRV_PCM_FMTBIT_S24_LE)
  360. static const struct snd_soc_dai_ops adau1701_dai_ops = {
  361. .set_fmt = adau1701_set_dai_fmt,
  362. .hw_params = adau1701_hw_params,
  363. .digital_mute = adau1701_digital_mute,
  364. };
  365. static struct snd_soc_dai_driver adau1701_dai = {
  366. .name = "adau1701",
  367. .playback = {
  368. .stream_name = "Playback",
  369. .channels_min = 2,
  370. .channels_max = 8,
  371. .rates = ADAU1701_RATES,
  372. .formats = ADAU1701_FORMATS,
  373. },
  374. .capture = {
  375. .stream_name = "Capture",
  376. .channels_min = 2,
  377. .channels_max = 8,
  378. .rates = ADAU1701_RATES,
  379. .formats = ADAU1701_FORMATS,
  380. },
  381. .ops = &adau1701_dai_ops,
  382. .symmetric_rates = 1,
  383. };
  384. static int adau1701_probe(struct snd_soc_codec *codec)
  385. {
  386. int ret;
  387. codec->control_data = to_i2c_client(codec->dev);
  388. ret = adau1701_load_firmware(codec);
  389. if (ret)
  390. dev_warn(codec->dev, "Failed to load firmware\n");
  391. snd_soc_write(codec, ADAU1701_DACSET, ADAU1701_DACSET_DACINIT);
  392. snd_soc_write(codec, ADAU1701_DSPCTRL, ADAU1701_DSPCTRL_CR);
  393. return 0;
  394. }
  395. static struct snd_soc_codec_driver adau1701_codec_drv = {
  396. .probe = adau1701_probe,
  397. .set_bias_level = adau1701_set_bias_level,
  398. .idle_bias_off = true,
  399. .reg_cache_size = ADAU1701_NUM_REGS,
  400. .reg_word_size = sizeof(u16),
  401. .controls = adau1701_controls,
  402. .num_controls = ARRAY_SIZE(adau1701_controls),
  403. .dapm_widgets = adau1701_dapm_widgets,
  404. .num_dapm_widgets = ARRAY_SIZE(adau1701_dapm_widgets),
  405. .dapm_routes = adau1701_dapm_routes,
  406. .num_dapm_routes = ARRAY_SIZE(adau1701_dapm_routes),
  407. .write = adau1701_write,
  408. .read = adau1701_read,
  409. .set_sysclk = adau1701_set_sysclk,
  410. };
  411. static __devinit int adau1701_i2c_probe(struct i2c_client *client,
  412. const struct i2c_device_id *id)
  413. {
  414. struct adau1701 *adau1701;
  415. int ret;
  416. adau1701 = devm_kzalloc(&client->dev, sizeof(*adau1701), GFP_KERNEL);
  417. if (!adau1701)
  418. return -ENOMEM;
  419. i2c_set_clientdata(client, adau1701);
  420. ret = snd_soc_register_codec(&client->dev, &adau1701_codec_drv,
  421. &adau1701_dai, 1);
  422. return ret;
  423. }
  424. static __devexit int adau1701_i2c_remove(struct i2c_client *client)
  425. {
  426. snd_soc_unregister_codec(&client->dev);
  427. return 0;
  428. }
  429. static const struct i2c_device_id adau1701_i2c_id[] = {
  430. { "adau1701", 0 },
  431. { }
  432. };
  433. MODULE_DEVICE_TABLE(i2c, adau1701_i2c_id);
  434. static struct i2c_driver adau1701_i2c_driver = {
  435. .driver = {
  436. .name = "adau1701",
  437. .owner = THIS_MODULE,
  438. },
  439. .probe = adau1701_i2c_probe,
  440. .remove = __devexit_p(adau1701_i2c_remove),
  441. .id_table = adau1701_i2c_id,
  442. };
  443. static int __init adau1701_init(void)
  444. {
  445. return i2c_add_driver(&adau1701_i2c_driver);
  446. }
  447. module_init(adau1701_init);
  448. static void __exit adau1701_exit(void)
  449. {
  450. i2c_del_driver(&adau1701_i2c_driver);
  451. }
  452. module_exit(adau1701_exit);
  453. MODULE_DESCRIPTION("ASoC ADAU1701 SigmaDSP driver");
  454. MODULE_AUTHOR("Cliff Cai <cliff.cai@analog.com>");
  455. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  456. MODULE_LICENSE("GPL");