wm8711.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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) & 0xfffc;
  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 = 0;
  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. snd_soc_write(codec, WM8711_PWR, reg | 0x0040);
  260. break;
  261. case SND_SOC_BIAS_OFF:
  262. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  263. snd_soc_write(codec, WM8711_PWR, 0xffff);
  264. break;
  265. }
  266. codec->dapm.bias_level = level;
  267. return 0;
  268. }
  269. #define WM8711_RATES SNDRV_PCM_RATE_8000_96000
  270. #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  271. SNDRV_PCM_FMTBIT_S24_LE)
  272. static struct snd_soc_dai_ops wm8711_ops = {
  273. .prepare = wm8711_pcm_prepare,
  274. .hw_params = wm8711_hw_params,
  275. .shutdown = wm8711_shutdown,
  276. .digital_mute = wm8711_mute,
  277. .set_sysclk = wm8711_set_dai_sysclk,
  278. .set_fmt = wm8711_set_dai_fmt,
  279. };
  280. static struct snd_soc_dai_driver wm8711_dai = {
  281. .name = "wm8711-hifi",
  282. .playback = {
  283. .stream_name = "Playback",
  284. .channels_min = 1,
  285. .channels_max = 2,
  286. .rates = WM8711_RATES,
  287. .formats = WM8711_FORMATS,
  288. },
  289. .ops = &wm8711_ops,
  290. };
  291. static int wm8711_suspend(struct snd_soc_codec *codec, pm_message_t state)
  292. {
  293. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  294. wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF);
  295. return 0;
  296. }
  297. static int wm8711_resume(struct snd_soc_codec *codec)
  298. {
  299. int i;
  300. u8 data[2];
  301. u16 *cache = codec->reg_cache;
  302. /* Sync reg_cache with the hardware */
  303. for (i = 0; i < ARRAY_SIZE(wm8711_reg); i++) {
  304. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  305. data[1] = cache[i] & 0x00ff;
  306. codec->hw_write(codec->control_data, data, 2);
  307. }
  308. wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  309. return 0;
  310. }
  311. static int wm8711_probe(struct snd_soc_codec *codec)
  312. {
  313. struct wm8711_priv *wm8711 = snd_soc_codec_get_drvdata(codec);
  314. int ret;
  315. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8711->bus_type);
  316. if (ret < 0) {
  317. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  318. return ret;
  319. }
  320. ret = wm8711_reset(codec);
  321. if (ret < 0) {
  322. dev_err(codec->dev, "Failed to issue reset\n");
  323. return ret;
  324. }
  325. wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  326. /* Latch the update bits */
  327. snd_soc_update_bits(codec, WM8711_LOUT1V, 0x0100, 0x0100);
  328. snd_soc_update_bits(codec, WM8711_ROUT1V, 0x0100, 0x0100);
  329. snd_soc_add_controls(codec, wm8711_snd_controls,
  330. ARRAY_SIZE(wm8711_snd_controls));
  331. return ret;
  332. }
  333. /* power down chip */
  334. static int wm8711_remove(struct snd_soc_codec *codec)
  335. {
  336. wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF);
  337. return 0;
  338. }
  339. static struct snd_soc_codec_driver soc_codec_dev_wm8711 = {
  340. .probe = wm8711_probe,
  341. .remove = wm8711_remove,
  342. .suspend = wm8711_suspend,
  343. .resume = wm8711_resume,
  344. .set_bias_level = wm8711_set_bias_level,
  345. .reg_cache_size = ARRAY_SIZE(wm8711_reg),
  346. .reg_word_size = sizeof(u16),
  347. .reg_cache_default = wm8711_reg,
  348. .dapm_widgets = wm8711_dapm_widgets,
  349. .num_dapm_widgets = ARRAY_SIZE(wm8711_dapm_widgets),
  350. .dapm_routes = wm8711_intercon,
  351. .num_dapm_routes = ARRAY_SIZE(wm8711_intercon),
  352. };
  353. static const struct of_device_id wm8711_of_match[] = {
  354. { .compatible = "wlf,wm8711", },
  355. { }
  356. };
  357. MODULE_DEVICE_TABLE(of, wm8711_of_match);
  358. #if defined(CONFIG_SPI_MASTER)
  359. static int __devinit wm8711_spi_probe(struct spi_device *spi)
  360. {
  361. struct wm8711_priv *wm8711;
  362. int ret;
  363. wm8711 = kzalloc(sizeof(struct wm8711_priv), GFP_KERNEL);
  364. if (wm8711 == NULL)
  365. return -ENOMEM;
  366. spi_set_drvdata(spi, wm8711);
  367. wm8711->bus_type = SND_SOC_SPI;
  368. ret = snd_soc_register_codec(&spi->dev,
  369. &soc_codec_dev_wm8711, &wm8711_dai, 1);
  370. if (ret < 0)
  371. kfree(wm8711);
  372. return ret;
  373. }
  374. static int __devexit wm8711_spi_remove(struct spi_device *spi)
  375. {
  376. snd_soc_unregister_codec(&spi->dev);
  377. kfree(spi_get_drvdata(spi));
  378. return 0;
  379. }
  380. static struct spi_driver wm8711_spi_driver = {
  381. .driver = {
  382. .name = "wm8711",
  383. .owner = THIS_MODULE,
  384. .of_match_table = wm8711_of_match,
  385. },
  386. .probe = wm8711_spi_probe,
  387. .remove = __devexit_p(wm8711_spi_remove),
  388. };
  389. #endif /* CONFIG_SPI_MASTER */
  390. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  391. static __devinit int wm8711_i2c_probe(struct i2c_client *client,
  392. const struct i2c_device_id *id)
  393. {
  394. struct wm8711_priv *wm8711;
  395. int ret;
  396. wm8711 = kzalloc(sizeof(struct wm8711_priv), GFP_KERNEL);
  397. if (wm8711 == NULL)
  398. return -ENOMEM;
  399. i2c_set_clientdata(client, wm8711);
  400. wm8711->bus_type = SND_SOC_I2C;
  401. ret = snd_soc_register_codec(&client->dev,
  402. &soc_codec_dev_wm8711, &wm8711_dai, 1);
  403. if (ret < 0)
  404. kfree(wm8711);
  405. return ret;
  406. }
  407. static __devexit int wm8711_i2c_remove(struct i2c_client *client)
  408. {
  409. snd_soc_unregister_codec(&client->dev);
  410. kfree(i2c_get_clientdata(client));
  411. return 0;
  412. }
  413. static const struct i2c_device_id wm8711_i2c_id[] = {
  414. { "wm8711", 0 },
  415. { }
  416. };
  417. MODULE_DEVICE_TABLE(i2c, wm8711_i2c_id);
  418. static struct i2c_driver wm8711_i2c_driver = {
  419. .driver = {
  420. .name = "wm8711",
  421. .owner = THIS_MODULE,
  422. .of_match_table = wm8711_of_match,
  423. },
  424. .probe = wm8711_i2c_probe,
  425. .remove = __devexit_p(wm8711_i2c_remove),
  426. .id_table = wm8711_i2c_id,
  427. };
  428. #endif
  429. static int __init wm8711_modinit(void)
  430. {
  431. int ret;
  432. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  433. ret = i2c_add_driver(&wm8711_i2c_driver);
  434. if (ret != 0) {
  435. printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n",
  436. ret);
  437. }
  438. #endif
  439. #if defined(CONFIG_SPI_MASTER)
  440. ret = spi_register_driver(&wm8711_spi_driver);
  441. if (ret != 0) {
  442. printk(KERN_ERR "Failed to register WM8711 SPI driver: %d\n",
  443. ret);
  444. }
  445. #endif
  446. return 0;
  447. }
  448. module_init(wm8711_modinit);
  449. static void __exit wm8711_exit(void)
  450. {
  451. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  452. i2c_del_driver(&wm8711_i2c_driver);
  453. #endif
  454. #if defined(CONFIG_SPI_MASTER)
  455. spi_unregister_driver(&wm8711_spi_driver);
  456. #endif
  457. }
  458. module_exit(wm8711_exit);
  459. MODULE_DESCRIPTION("ASoC WM8711 driver");
  460. MODULE_AUTHOR("Mike Arthur");
  461. MODULE_LICENSE("GPL");