wm8711.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * wm8711.c -- WM8711 ALSA SoC Audio driver
  3. *
  4. * Copyright 2006 Wolfson Microelectronics
  5. *
  6. * Author: Mike Arthur <Mike.Arthur@wolfsonmicro.com>
  7. *
  8. * Based on wm8731.c by Richard Purdie
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  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/regmap.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/slab.h>
  23. #include <linux/of_device.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/tlv.h>
  29. #include <sound/initval.h>
  30. #include "wm8711.h"
  31. /* codec private data */
  32. struct wm8711_priv {
  33. struct regmap *regmap;
  34. unsigned int sysclk;
  35. };
  36. /*
  37. * wm8711 register cache
  38. * We can't read the WM8711 register space when we are
  39. * using 2 wire for device control, so we cache them instead.
  40. * There is no point in caching the reset register
  41. */
  42. static const struct reg_default wm8711_reg_defaults[] = {
  43. { 0, 0x0079 }, { 1, 0x0079 }, { 2, 0x000a }, { 3, 0x0008 },
  44. { 4, 0x009f }, { 5, 0x000a }, { 6, 0x0000 }, { 7, 0x0000 },
  45. };
  46. static bool wm8711_volatile(struct device *dev, unsigned int reg)
  47. {
  48. switch (reg) {
  49. case WM8711_RESET:
  50. return true;
  51. default:
  52. return false;
  53. }
  54. }
  55. #define wm8711_reset(c) snd_soc_write(c, WM8711_RESET, 0)
  56. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  57. static const struct snd_kcontrol_new wm8711_snd_controls[] = {
  58. SOC_DOUBLE_R_TLV("Master Playback Volume", WM8711_LOUT1V, WM8711_ROUT1V,
  59. 0, 127, 0, out_tlv),
  60. SOC_DOUBLE_R("Master Playback ZC Switch", WM8711_LOUT1V, WM8711_ROUT1V,
  61. 7, 1, 0),
  62. };
  63. /* Output Mixer */
  64. static const struct snd_kcontrol_new wm8711_output_mixer_controls[] = {
  65. SOC_DAPM_SINGLE("Line Bypass Switch", WM8711_APANA, 3, 1, 0),
  66. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8711_APANA, 4, 1, 0),
  67. };
  68. static const struct snd_soc_dapm_widget wm8711_dapm_widgets[] = {
  69. SND_SOC_DAPM_MIXER("Output Mixer", WM8711_PWR, 4, 1,
  70. &wm8711_output_mixer_controls[0],
  71. ARRAY_SIZE(wm8711_output_mixer_controls)),
  72. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8711_PWR, 3, 1),
  73. SND_SOC_DAPM_OUTPUT("LOUT"),
  74. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  75. SND_SOC_DAPM_OUTPUT("ROUT"),
  76. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  77. };
  78. static const struct snd_soc_dapm_route wm8711_intercon[] = {
  79. /* output mixer */
  80. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  81. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  82. /* outputs */
  83. {"RHPOUT", NULL, "Output Mixer"},
  84. {"ROUT", NULL, "Output Mixer"},
  85. {"LHPOUT", NULL, "Output Mixer"},
  86. {"LOUT", NULL, "Output Mixer"},
  87. };
  88. struct _coeff_div {
  89. u32 mclk;
  90. u32 rate;
  91. u16 fs;
  92. u8 sr:4;
  93. u8 bosr:1;
  94. u8 usb:1;
  95. };
  96. /* codec mclk clock divider coefficients */
  97. static const struct _coeff_div coeff_div[] = {
  98. /* 48k */
  99. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  100. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  101. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  102. /* 32k */
  103. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  104. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  105. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  106. /* 8k */
  107. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  108. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  109. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  110. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  111. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  112. /* 96k */
  113. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  114. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  115. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  116. /* 44.1k */
  117. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  118. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  119. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  120. /* 88.2k */
  121. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  122. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  123. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  124. };
  125. static inline int get_coeff(int mclk, int rate)
  126. {
  127. int i;
  128. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  129. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  130. return i;
  131. }
  132. return 0;
  133. }
  134. static int wm8711_hw_params(struct snd_pcm_substream *substream,
  135. struct snd_pcm_hw_params *params,
  136. struct snd_soc_dai *dai)
  137. {
  138. struct snd_soc_codec *codec = dai->codec;
  139. struct wm8711_priv *wm8711 = snd_soc_codec_get_drvdata(codec);
  140. u16 iface = snd_soc_read(codec, WM8711_IFACE) & 0xfff3;
  141. int i = get_coeff(wm8711->sysclk, params_rate(params));
  142. u16 srate = (coeff_div[i].sr << 2) |
  143. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  144. snd_soc_write(codec, WM8711_SRATE, srate);
  145. /* bit size */
  146. switch (params_width(params)) {
  147. case 16:
  148. break;
  149. case 20:
  150. iface |= 0x0004;
  151. break;
  152. case 24:
  153. iface |= 0x0008;
  154. break;
  155. }
  156. snd_soc_write(codec, WM8711_IFACE, iface);
  157. return 0;
  158. }
  159. static int wm8711_pcm_prepare(struct snd_pcm_substream *substream,
  160. struct snd_soc_dai *dai)
  161. {
  162. struct snd_soc_codec *codec = dai->codec;
  163. /* set active */
  164. snd_soc_write(codec, WM8711_ACTIVE, 0x0001);
  165. return 0;
  166. }
  167. static void wm8711_shutdown(struct snd_pcm_substream *substream,
  168. struct snd_soc_dai *dai)
  169. {
  170. struct snd_soc_codec *codec = dai->codec;
  171. /* deactivate */
  172. if (!snd_soc_codec_is_active(codec)) {
  173. udelay(50);
  174. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  175. }
  176. }
  177. static int wm8711_mute(struct snd_soc_dai *dai, int mute)
  178. {
  179. struct snd_soc_codec *codec = dai->codec;
  180. u16 mute_reg = snd_soc_read(codec, WM8711_APDIGI) & 0xfff7;
  181. if (mute)
  182. snd_soc_write(codec, WM8711_APDIGI, mute_reg | 0x8);
  183. else
  184. snd_soc_write(codec, WM8711_APDIGI, mute_reg);
  185. return 0;
  186. }
  187. static int wm8711_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  188. int clk_id, unsigned int freq, int dir)
  189. {
  190. struct snd_soc_codec *codec = codec_dai->codec;
  191. struct wm8711_priv *wm8711 = snd_soc_codec_get_drvdata(codec);
  192. switch (freq) {
  193. case 11289600:
  194. case 12000000:
  195. case 12288000:
  196. case 16934400:
  197. case 18432000:
  198. wm8711->sysclk = freq;
  199. return 0;
  200. }
  201. return -EINVAL;
  202. }
  203. static int wm8711_set_dai_fmt(struct snd_soc_dai *codec_dai,
  204. unsigned int fmt)
  205. {
  206. struct snd_soc_codec *codec = codec_dai->codec;
  207. u16 iface = snd_soc_read(codec, WM8711_IFACE) & 0x000c;
  208. /* set master/slave audio interface */
  209. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  210. case SND_SOC_DAIFMT_CBM_CFM:
  211. iface |= 0x0040;
  212. break;
  213. case SND_SOC_DAIFMT_CBS_CFS:
  214. break;
  215. default:
  216. return -EINVAL;
  217. }
  218. /* interface format */
  219. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  220. case SND_SOC_DAIFMT_I2S:
  221. iface |= 0x0002;
  222. break;
  223. case SND_SOC_DAIFMT_RIGHT_J:
  224. break;
  225. case SND_SOC_DAIFMT_LEFT_J:
  226. iface |= 0x0001;
  227. break;
  228. case SND_SOC_DAIFMT_DSP_A:
  229. iface |= 0x0003;
  230. break;
  231. case SND_SOC_DAIFMT_DSP_B:
  232. iface |= 0x0013;
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. /* clock inversion */
  238. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  239. case SND_SOC_DAIFMT_NB_NF:
  240. break;
  241. case SND_SOC_DAIFMT_IB_IF:
  242. iface |= 0x0090;
  243. break;
  244. case SND_SOC_DAIFMT_IB_NF:
  245. iface |= 0x0080;
  246. break;
  247. case SND_SOC_DAIFMT_NB_IF:
  248. iface |= 0x0010;
  249. break;
  250. default:
  251. return -EINVAL;
  252. }
  253. /* set iface */
  254. snd_soc_write(codec, WM8711_IFACE, iface);
  255. return 0;
  256. }
  257. static int wm8711_set_bias_level(struct snd_soc_codec *codec,
  258. enum snd_soc_bias_level level)
  259. {
  260. struct wm8711_priv *wm8711 = snd_soc_codec_get_drvdata(codec);
  261. u16 reg = snd_soc_read(codec, WM8711_PWR) & 0xff7f;
  262. switch (level) {
  263. case SND_SOC_BIAS_ON:
  264. snd_soc_write(codec, WM8711_PWR, reg);
  265. break;
  266. case SND_SOC_BIAS_PREPARE:
  267. break;
  268. case SND_SOC_BIAS_STANDBY:
  269. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
  270. regcache_sync(wm8711->regmap);
  271. snd_soc_write(codec, WM8711_PWR, reg | 0x0040);
  272. break;
  273. case SND_SOC_BIAS_OFF:
  274. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  275. snd_soc_write(codec, WM8711_PWR, 0xffff);
  276. break;
  277. }
  278. return 0;
  279. }
  280. #define WM8711_RATES SNDRV_PCM_RATE_8000_96000
  281. #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  282. SNDRV_PCM_FMTBIT_S24_LE)
  283. static const struct snd_soc_dai_ops wm8711_ops = {
  284. .prepare = wm8711_pcm_prepare,
  285. .hw_params = wm8711_hw_params,
  286. .shutdown = wm8711_shutdown,
  287. .digital_mute = wm8711_mute,
  288. .set_sysclk = wm8711_set_dai_sysclk,
  289. .set_fmt = wm8711_set_dai_fmt,
  290. };
  291. static struct snd_soc_dai_driver wm8711_dai = {
  292. .name = "wm8711-hifi",
  293. .playback = {
  294. .stream_name = "Playback",
  295. .channels_min = 1,
  296. .channels_max = 2,
  297. .rates = WM8711_RATES,
  298. .formats = WM8711_FORMATS,
  299. },
  300. .ops = &wm8711_ops,
  301. };
  302. static int wm8711_probe(struct snd_soc_codec *codec)
  303. {
  304. int ret;
  305. ret = wm8711_reset(codec);
  306. if (ret < 0) {
  307. dev_err(codec->dev, "Failed to issue reset\n");
  308. return ret;
  309. }
  310. /* Latch the update bits */
  311. snd_soc_update_bits(codec, WM8711_LOUT1V, 0x0100, 0x0100);
  312. snd_soc_update_bits(codec, WM8711_ROUT1V, 0x0100, 0x0100);
  313. return ret;
  314. }
  315. static const struct snd_soc_codec_driver soc_codec_dev_wm8711 = {
  316. .probe = wm8711_probe,
  317. .set_bias_level = wm8711_set_bias_level,
  318. .suspend_bias_off = true,
  319. .component_driver = {
  320. .controls = wm8711_snd_controls,
  321. .num_controls = ARRAY_SIZE(wm8711_snd_controls),
  322. .dapm_widgets = wm8711_dapm_widgets,
  323. .num_dapm_widgets = ARRAY_SIZE(wm8711_dapm_widgets),
  324. .dapm_routes = wm8711_intercon,
  325. .num_dapm_routes = ARRAY_SIZE(wm8711_intercon),
  326. },
  327. };
  328. static const struct of_device_id wm8711_of_match[] = {
  329. { .compatible = "wlf,wm8711", },
  330. { }
  331. };
  332. MODULE_DEVICE_TABLE(of, wm8711_of_match);
  333. static const struct regmap_config wm8711_regmap = {
  334. .reg_bits = 7,
  335. .val_bits = 9,
  336. .max_register = WM8711_RESET,
  337. .reg_defaults = wm8711_reg_defaults,
  338. .num_reg_defaults = ARRAY_SIZE(wm8711_reg_defaults),
  339. .cache_type = REGCACHE_RBTREE,
  340. .volatile_reg = wm8711_volatile,
  341. };
  342. #if defined(CONFIG_SPI_MASTER)
  343. static int wm8711_spi_probe(struct spi_device *spi)
  344. {
  345. struct wm8711_priv *wm8711;
  346. int ret;
  347. wm8711 = devm_kzalloc(&spi->dev, sizeof(struct wm8711_priv),
  348. GFP_KERNEL);
  349. if (wm8711 == NULL)
  350. return -ENOMEM;
  351. wm8711->regmap = devm_regmap_init_spi(spi, &wm8711_regmap);
  352. if (IS_ERR(wm8711->regmap))
  353. return PTR_ERR(wm8711->regmap);
  354. spi_set_drvdata(spi, wm8711);
  355. ret = snd_soc_register_codec(&spi->dev,
  356. &soc_codec_dev_wm8711, &wm8711_dai, 1);
  357. return ret;
  358. }
  359. static int wm8711_spi_remove(struct spi_device *spi)
  360. {
  361. snd_soc_unregister_codec(&spi->dev);
  362. return 0;
  363. }
  364. static struct spi_driver wm8711_spi_driver = {
  365. .driver = {
  366. .name = "wm8711",
  367. .of_match_table = wm8711_of_match,
  368. },
  369. .probe = wm8711_spi_probe,
  370. .remove = wm8711_spi_remove,
  371. };
  372. #endif /* CONFIG_SPI_MASTER */
  373. #if IS_ENABLED(CONFIG_I2C)
  374. static int wm8711_i2c_probe(struct i2c_client *client,
  375. const struct i2c_device_id *id)
  376. {
  377. struct wm8711_priv *wm8711;
  378. int ret;
  379. wm8711 = devm_kzalloc(&client->dev, sizeof(struct wm8711_priv),
  380. GFP_KERNEL);
  381. if (wm8711 == NULL)
  382. return -ENOMEM;
  383. wm8711->regmap = devm_regmap_init_i2c(client, &wm8711_regmap);
  384. if (IS_ERR(wm8711->regmap))
  385. return PTR_ERR(wm8711->regmap);
  386. i2c_set_clientdata(client, wm8711);
  387. ret = snd_soc_register_codec(&client->dev,
  388. &soc_codec_dev_wm8711, &wm8711_dai, 1);
  389. return ret;
  390. }
  391. static int wm8711_i2c_remove(struct i2c_client *client)
  392. {
  393. snd_soc_unregister_codec(&client->dev);
  394. return 0;
  395. }
  396. static const struct i2c_device_id wm8711_i2c_id[] = {
  397. { "wm8711", 0 },
  398. { }
  399. };
  400. MODULE_DEVICE_TABLE(i2c, wm8711_i2c_id);
  401. static struct i2c_driver wm8711_i2c_driver = {
  402. .driver = {
  403. .name = "wm8711",
  404. .of_match_table = wm8711_of_match,
  405. },
  406. .probe = wm8711_i2c_probe,
  407. .remove = wm8711_i2c_remove,
  408. .id_table = wm8711_i2c_id,
  409. };
  410. #endif
  411. static int __init wm8711_modinit(void)
  412. {
  413. int ret;
  414. #if IS_ENABLED(CONFIG_I2C)
  415. ret = i2c_add_driver(&wm8711_i2c_driver);
  416. if (ret != 0) {
  417. printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n",
  418. ret);
  419. }
  420. #endif
  421. #if defined(CONFIG_SPI_MASTER)
  422. ret = spi_register_driver(&wm8711_spi_driver);
  423. if (ret != 0) {
  424. printk(KERN_ERR "Failed to register WM8711 SPI driver: %d\n",
  425. ret);
  426. }
  427. #endif
  428. return 0;
  429. }
  430. module_init(wm8711_modinit);
  431. static void __exit wm8711_exit(void)
  432. {
  433. #if IS_ENABLED(CONFIG_I2C)
  434. i2c_del_driver(&wm8711_i2c_driver);
  435. #endif
  436. #if defined(CONFIG_SPI_MASTER)
  437. spi_unregister_driver(&wm8711_spi_driver);
  438. #endif
  439. }
  440. module_exit(wm8711_exit);
  441. MODULE_DESCRIPTION("ASoC WM8711 driver");
  442. MODULE_AUTHOR("Mike Arthur");
  443. MODULE_LICENSE("GPL");