wm8776.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * wm8776.c -- WM8776 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * TODO: Input ALC/limiter support
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/of_device.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/slab.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/initval.h>
  28. #include <sound/tlv.h>
  29. #include "wm8776.h"
  30. enum wm8776_chip_type {
  31. WM8775 = 1,
  32. WM8776,
  33. };
  34. /* codec private data */
  35. struct wm8776_priv {
  36. enum snd_soc_control_type control_type;
  37. int sysclk[2];
  38. };
  39. static const u16 wm8776_reg[WM8776_CACHEREGNUM] = {
  40. 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */
  41. 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */
  42. 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */
  43. 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */
  44. 0xa6, 0x01, 0x01
  45. };
  46. static int wm8776_reset(struct snd_soc_codec *codec)
  47. {
  48. return snd_soc_write(codec, WM8776_RESET, 0);
  49. }
  50. static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
  51. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
  52. static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
  53. static const struct snd_kcontrol_new wm8776_snd_controls[] = {
  54. SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
  55. 0, 127, 0, hp_tlv),
  56. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
  57. 0, 255, 0, dac_tlv),
  58. SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
  59. SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
  60. SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
  61. 0, 255, 0, adc_tlv),
  62. SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
  63. SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
  64. SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
  65. };
  66. static const struct snd_kcontrol_new inmix_controls[] = {
  67. SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
  68. SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
  69. SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
  70. SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
  71. SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
  72. };
  73. static const struct snd_kcontrol_new outmix_controls[] = {
  74. SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
  75. SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
  76. SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
  77. };
  78. static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
  79. SND_SOC_DAPM_INPUT("AUX"),
  80. SND_SOC_DAPM_INPUT("AIN1"),
  81. SND_SOC_DAPM_INPUT("AIN2"),
  82. SND_SOC_DAPM_INPUT("AIN3"),
  83. SND_SOC_DAPM_INPUT("AIN4"),
  84. SND_SOC_DAPM_INPUT("AIN5"),
  85. SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
  86. inmix_controls, ARRAY_SIZE(inmix_controls)),
  87. SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
  88. SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
  89. SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
  90. outmix_controls, ARRAY_SIZE(outmix_controls)),
  91. SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
  92. SND_SOC_DAPM_OUTPUT("VOUT"),
  93. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  94. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  95. };
  96. static const struct snd_soc_dapm_route routes[] = {
  97. { "Input Mixer", "AIN1 Switch", "AIN1" },
  98. { "Input Mixer", "AIN2 Switch", "AIN2" },
  99. { "Input Mixer", "AIN3 Switch", "AIN3" },
  100. { "Input Mixer", "AIN4 Switch", "AIN4" },
  101. { "Input Mixer", "AIN5 Switch", "AIN5" },
  102. { "ADC", NULL, "Input Mixer" },
  103. { "Output Mixer", "DAC Switch", "DAC" },
  104. { "Output Mixer", "AUX Switch", "AUX" },
  105. { "Output Mixer", "Bypass Switch", "Input Mixer" },
  106. { "VOUT", NULL, "Output Mixer" },
  107. { "Headphone PGA", NULL, "Output Mixer" },
  108. { "HPOUTL", NULL, "Headphone PGA" },
  109. { "HPOUTR", NULL, "Headphone PGA" },
  110. };
  111. static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  112. {
  113. struct snd_soc_codec *codec = dai->codec;
  114. int reg, iface, master;
  115. switch (dai->driver->id) {
  116. case WM8776_DAI_DAC:
  117. reg = WM8776_DACIFCTRL;
  118. master = 0x80;
  119. break;
  120. case WM8776_DAI_ADC:
  121. reg = WM8776_ADCIFCTRL;
  122. master = 0x100;
  123. break;
  124. default:
  125. return -EINVAL;
  126. }
  127. iface = 0;
  128. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  129. case SND_SOC_DAIFMT_CBM_CFM:
  130. break;
  131. case SND_SOC_DAIFMT_CBS_CFS:
  132. master = 0;
  133. break;
  134. default:
  135. return -EINVAL;
  136. }
  137. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  138. case SND_SOC_DAIFMT_I2S:
  139. iface |= 0x0002;
  140. break;
  141. case SND_SOC_DAIFMT_RIGHT_J:
  142. break;
  143. case SND_SOC_DAIFMT_LEFT_J:
  144. iface |= 0x0001;
  145. break;
  146. default:
  147. return -EINVAL;
  148. }
  149. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  150. case SND_SOC_DAIFMT_NB_NF:
  151. break;
  152. case SND_SOC_DAIFMT_IB_IF:
  153. iface |= 0x00c;
  154. break;
  155. case SND_SOC_DAIFMT_IB_NF:
  156. iface |= 0x008;
  157. break;
  158. case SND_SOC_DAIFMT_NB_IF:
  159. iface |= 0x004;
  160. break;
  161. default:
  162. return -EINVAL;
  163. }
  164. /* Finally, write out the values */
  165. snd_soc_update_bits(codec, reg, 0xf, iface);
  166. snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master);
  167. return 0;
  168. }
  169. static int mclk_ratios[] = {
  170. 128,
  171. 192,
  172. 256,
  173. 384,
  174. 512,
  175. 768,
  176. };
  177. static int wm8776_hw_params(struct snd_pcm_substream *substream,
  178. struct snd_pcm_hw_params *params,
  179. struct snd_soc_dai *dai)
  180. {
  181. struct snd_soc_codec *codec = dai->codec;
  182. struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
  183. int iface_reg, iface;
  184. int ratio_shift, master;
  185. int i;
  186. switch (dai->driver->id) {
  187. case WM8776_DAI_DAC:
  188. iface_reg = WM8776_DACIFCTRL;
  189. master = 0x80;
  190. ratio_shift = 4;
  191. break;
  192. case WM8776_DAI_ADC:
  193. iface_reg = WM8776_ADCIFCTRL;
  194. master = 0x100;
  195. ratio_shift = 0;
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. /* Set word length */
  201. switch (snd_pcm_format_width(params_format(params))) {
  202. case 16:
  203. iface = 0;
  204. break;
  205. case 20:
  206. iface = 0x10;
  207. break;
  208. case 24:
  209. iface = 0x20;
  210. break;
  211. case 32:
  212. iface = 0x30;
  213. break;
  214. default:
  215. dev_err(codec->dev, "Unsupported sample size: %i\n",
  216. snd_pcm_format_width(params_format(params)));
  217. return -EINVAL;
  218. }
  219. /* Only need to set MCLK/LRCLK ratio if we're master */
  220. if (snd_soc_read(codec, WM8776_MSTRCTRL) & master) {
  221. for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
  222. if (wm8776->sysclk[dai->driver->id] / params_rate(params)
  223. == mclk_ratios[i])
  224. break;
  225. }
  226. if (i == ARRAY_SIZE(mclk_ratios)) {
  227. dev_err(codec->dev,
  228. "Unable to configure MCLK ratio %d/%d\n",
  229. wm8776->sysclk[dai->driver->id], params_rate(params));
  230. return -EINVAL;
  231. }
  232. dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
  233. snd_soc_update_bits(codec, WM8776_MSTRCTRL,
  234. 0x7 << ratio_shift, i << ratio_shift);
  235. } else {
  236. dev_dbg(codec->dev, "DAI in slave mode\n");
  237. }
  238. snd_soc_update_bits(codec, iface_reg, 0x30, iface);
  239. return 0;
  240. }
  241. static int wm8776_mute(struct snd_soc_dai *dai, int mute)
  242. {
  243. struct snd_soc_codec *codec = dai->codec;
  244. return snd_soc_write(codec, WM8776_DACMUTE, !!mute);
  245. }
  246. static int wm8776_set_sysclk(struct snd_soc_dai *dai,
  247. int clk_id, unsigned int freq, int dir)
  248. {
  249. struct snd_soc_codec *codec = dai->codec;
  250. struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
  251. BUG_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk));
  252. wm8776->sysclk[dai->driver->id] = freq;
  253. return 0;
  254. }
  255. static int wm8776_set_bias_level(struct snd_soc_codec *codec,
  256. enum snd_soc_bias_level level)
  257. {
  258. switch (level) {
  259. case SND_SOC_BIAS_ON:
  260. break;
  261. case SND_SOC_BIAS_PREPARE:
  262. break;
  263. case SND_SOC_BIAS_STANDBY:
  264. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  265. snd_soc_cache_sync(codec);
  266. /* Disable the global powerdown; DAPM does the rest */
  267. snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0);
  268. }
  269. break;
  270. case SND_SOC_BIAS_OFF:
  271. snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1);
  272. break;
  273. }
  274. codec->dapm.bias_level = level;
  275. return 0;
  276. }
  277. #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  278. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  279. static const struct snd_soc_dai_ops wm8776_dac_ops = {
  280. .digital_mute = wm8776_mute,
  281. .hw_params = wm8776_hw_params,
  282. .set_fmt = wm8776_set_fmt,
  283. .set_sysclk = wm8776_set_sysclk,
  284. };
  285. static const struct snd_soc_dai_ops wm8776_adc_ops = {
  286. .hw_params = wm8776_hw_params,
  287. .set_fmt = wm8776_set_fmt,
  288. .set_sysclk = wm8776_set_sysclk,
  289. };
  290. static struct snd_soc_dai_driver wm8776_dai[] = {
  291. {
  292. .name = "wm8776-hifi-playback",
  293. .id = WM8776_DAI_DAC,
  294. .playback = {
  295. .stream_name = "Playback",
  296. .channels_min = 2,
  297. .channels_max = 2,
  298. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  299. .rate_min = 32000,
  300. .rate_max = 192000,
  301. .formats = WM8776_FORMATS,
  302. },
  303. .ops = &wm8776_dac_ops,
  304. },
  305. {
  306. .name = "wm8776-hifi-capture",
  307. .id = WM8776_DAI_ADC,
  308. .capture = {
  309. .stream_name = "Capture",
  310. .channels_min = 2,
  311. .channels_max = 2,
  312. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  313. .rate_min = 32000,
  314. .rate_max = 96000,
  315. .formats = WM8776_FORMATS,
  316. },
  317. .ops = &wm8776_adc_ops,
  318. },
  319. };
  320. #ifdef CONFIG_PM
  321. static int wm8776_suspend(struct snd_soc_codec *codec)
  322. {
  323. wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
  324. return 0;
  325. }
  326. static int wm8776_resume(struct snd_soc_codec *codec)
  327. {
  328. wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  329. return 0;
  330. }
  331. #else
  332. #define wm8776_suspend NULL
  333. #define wm8776_resume NULL
  334. #endif
  335. static int wm8776_probe(struct snd_soc_codec *codec)
  336. {
  337. struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
  338. int ret = 0;
  339. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8776->control_type);
  340. if (ret < 0) {
  341. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  342. return ret;
  343. }
  344. ret = wm8776_reset(codec);
  345. if (ret < 0) {
  346. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  347. return ret;
  348. }
  349. wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  350. /* Latch the update bits; right channel only since we always
  351. * update both. */
  352. snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100);
  353. snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100);
  354. return ret;
  355. }
  356. /* power down chip */
  357. static int wm8776_remove(struct snd_soc_codec *codec)
  358. {
  359. wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
  360. return 0;
  361. }
  362. static struct snd_soc_codec_driver soc_codec_dev_wm8776 = {
  363. .probe = wm8776_probe,
  364. .remove = wm8776_remove,
  365. .suspend = wm8776_suspend,
  366. .resume = wm8776_resume,
  367. .set_bias_level = wm8776_set_bias_level,
  368. .reg_cache_size = ARRAY_SIZE(wm8776_reg),
  369. .reg_word_size = sizeof(u16),
  370. .reg_cache_default = wm8776_reg,
  371. .controls = wm8776_snd_controls,
  372. .num_controls = ARRAY_SIZE(wm8776_snd_controls),
  373. .dapm_widgets = wm8776_dapm_widgets,
  374. .num_dapm_widgets = ARRAY_SIZE(wm8776_dapm_widgets),
  375. .dapm_routes = routes,
  376. .num_dapm_routes = ARRAY_SIZE(routes),
  377. };
  378. static const struct of_device_id wm8776_of_match[] = {
  379. { .compatible = "wlf,wm8776", },
  380. { }
  381. };
  382. MODULE_DEVICE_TABLE(of, wm8776_of_match);
  383. #if defined(CONFIG_SPI_MASTER)
  384. static int __devinit wm8776_spi_probe(struct spi_device *spi)
  385. {
  386. struct wm8776_priv *wm8776;
  387. int ret;
  388. wm8776 = devm_kzalloc(&spi->dev, sizeof(struct wm8776_priv),
  389. GFP_KERNEL);
  390. if (wm8776 == NULL)
  391. return -ENOMEM;
  392. wm8776->control_type = SND_SOC_SPI;
  393. spi_set_drvdata(spi, wm8776);
  394. ret = snd_soc_register_codec(&spi->dev,
  395. &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
  396. return ret;
  397. }
  398. static int __devexit wm8776_spi_remove(struct spi_device *spi)
  399. {
  400. snd_soc_unregister_codec(&spi->dev);
  401. return 0;
  402. }
  403. static struct spi_driver wm8776_spi_driver = {
  404. .driver = {
  405. .name = "wm8776",
  406. .owner = THIS_MODULE,
  407. .of_match_table = wm8776_of_match,
  408. },
  409. .probe = wm8776_spi_probe,
  410. .remove = __devexit_p(wm8776_spi_remove),
  411. };
  412. #endif /* CONFIG_SPI_MASTER */
  413. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  414. static __devinit int wm8776_i2c_probe(struct i2c_client *i2c,
  415. const struct i2c_device_id *id)
  416. {
  417. struct wm8776_priv *wm8776;
  418. int ret;
  419. wm8776 = devm_kzalloc(&i2c->dev, sizeof(struct wm8776_priv),
  420. GFP_KERNEL);
  421. if (wm8776 == NULL)
  422. return -ENOMEM;
  423. i2c_set_clientdata(i2c, wm8776);
  424. wm8776->control_type = SND_SOC_I2C;
  425. ret = snd_soc_register_codec(&i2c->dev,
  426. &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
  427. return ret;
  428. }
  429. static __devexit int wm8776_i2c_remove(struct i2c_client *client)
  430. {
  431. snd_soc_unregister_codec(&client->dev);
  432. return 0;
  433. }
  434. static const struct i2c_device_id wm8776_i2c_id[] = {
  435. { "wm8775", WM8775 },
  436. { "wm8776", WM8776 },
  437. { }
  438. };
  439. MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
  440. static struct i2c_driver wm8776_i2c_driver = {
  441. .driver = {
  442. .name = "wm8776",
  443. .owner = THIS_MODULE,
  444. .of_match_table = wm8776_of_match,
  445. },
  446. .probe = wm8776_i2c_probe,
  447. .remove = __devexit_p(wm8776_i2c_remove),
  448. .id_table = wm8776_i2c_id,
  449. };
  450. #endif
  451. static int __init wm8776_modinit(void)
  452. {
  453. int ret = 0;
  454. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  455. ret = i2c_add_driver(&wm8776_i2c_driver);
  456. if (ret != 0) {
  457. printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
  458. ret);
  459. }
  460. #endif
  461. #if defined(CONFIG_SPI_MASTER)
  462. ret = spi_register_driver(&wm8776_spi_driver);
  463. if (ret != 0) {
  464. printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
  465. ret);
  466. }
  467. #endif
  468. return ret;
  469. }
  470. module_init(wm8776_modinit);
  471. static void __exit wm8776_exit(void)
  472. {
  473. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  474. i2c_del_driver(&wm8776_i2c_driver);
  475. #endif
  476. #if defined(CONFIG_SPI_MASTER)
  477. spi_unregister_driver(&wm8776_spi_driver);
  478. #endif
  479. }
  480. module_exit(wm8776_exit);
  481. MODULE_DESCRIPTION("ASoC WM8776 driver");
  482. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  483. MODULE_LICENSE("GPL");