wm8776.c 13 KB

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