wm8711.c 12 KB

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