wm8711.c 12 KB

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