wm8711.c 12 KB

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