wm8711.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * wm8711.c -- WM8711 ALSA SoC Audio driver
  3. *
  4. * Copyright 2006 Wolfson Microelectronics
  5. *
  6. * Author: Mike Arthur <linux@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 <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 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. static int wm8711_add_widgets(struct snd_soc_codec *codec)
  79. {
  80. struct snd_soc_dapm_context *dapm = &codec->dapm;
  81. snd_soc_dapm_new_controls(dapm, wm8711_dapm_widgets,
  82. ARRAY_SIZE(wm8711_dapm_widgets));
  83. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  84. return 0;
  85. }
  86. struct _coeff_div {
  87. u32 mclk;
  88. u32 rate;
  89. u16 fs;
  90. u8 sr:4;
  91. u8 bosr:1;
  92. u8 usb:1;
  93. };
  94. /* codec mclk clock divider coefficients */
  95. static const struct _coeff_div coeff_div[] = {
  96. /* 48k */
  97. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  98. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  99. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  100. /* 32k */
  101. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  102. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  103. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  104. /* 8k */
  105. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  106. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  107. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  108. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  109. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  110. /* 96k */
  111. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  112. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  113. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  114. /* 44.1k */
  115. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  116. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  117. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  118. /* 88.2k */
  119. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  120. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  121. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  122. };
  123. static inline int get_coeff(int mclk, int rate)
  124. {
  125. int i;
  126. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  127. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  128. return i;
  129. }
  130. return 0;
  131. }
  132. static int wm8711_hw_params(struct snd_pcm_substream *substream,
  133. struct snd_pcm_hw_params *params,
  134. struct snd_soc_dai *dai)
  135. {
  136. struct snd_soc_codec *codec = dai->codec;
  137. struct wm8711_priv *wm8711 = snd_soc_codec_get_drvdata(codec);
  138. u16 iface = snd_soc_read(codec, WM8711_IFACE) & 0xfffc;
  139. int i = get_coeff(wm8711->sysclk, params_rate(params));
  140. u16 srate = (coeff_div[i].sr << 2) |
  141. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  142. snd_soc_write(codec, WM8711_SRATE, srate);
  143. /* bit size */
  144. switch (params_format(params)) {
  145. case SNDRV_PCM_FORMAT_S16_LE:
  146. break;
  147. case SNDRV_PCM_FORMAT_S20_3LE:
  148. iface |= 0x0004;
  149. break;
  150. case SNDRV_PCM_FORMAT_S24_LE:
  151. iface |= 0x0008;
  152. break;
  153. }
  154. snd_soc_write(codec, WM8711_IFACE, iface);
  155. return 0;
  156. }
  157. static int wm8711_pcm_prepare(struct snd_pcm_substream *substream,
  158. struct snd_soc_dai *dai)
  159. {
  160. struct snd_soc_codec *codec = dai->codec;
  161. /* set active */
  162. snd_soc_write(codec, WM8711_ACTIVE, 0x0001);
  163. return 0;
  164. }
  165. static void wm8711_shutdown(struct snd_pcm_substream *substream,
  166. struct snd_soc_dai *dai)
  167. {
  168. struct snd_soc_codec *codec = dai->codec;
  169. /* deactivate */
  170. if (!codec->active) {
  171. udelay(50);
  172. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  173. }
  174. }
  175. static int wm8711_mute(struct snd_soc_dai *dai, int mute)
  176. {
  177. struct snd_soc_codec *codec = dai->codec;
  178. u16 mute_reg = snd_soc_read(codec, WM8711_APDIGI) & 0xfff7;
  179. if (mute)
  180. snd_soc_write(codec, WM8711_APDIGI, mute_reg | 0x8);
  181. else
  182. snd_soc_write(codec, WM8711_APDIGI, mute_reg);
  183. return 0;
  184. }
  185. static int wm8711_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  186. int clk_id, unsigned int freq, int dir)
  187. {
  188. struct snd_soc_codec *codec = codec_dai->codec;
  189. struct wm8711_priv *wm8711 = snd_soc_codec_get_drvdata(codec);
  190. switch (freq) {
  191. case 11289600:
  192. case 12000000:
  193. case 12288000:
  194. case 16934400:
  195. case 18432000:
  196. wm8711->sysclk = freq;
  197. return 0;
  198. }
  199. return -EINVAL;
  200. }
  201. static int wm8711_set_dai_fmt(struct snd_soc_dai *codec_dai,
  202. unsigned int fmt)
  203. {
  204. struct snd_soc_codec *codec = codec_dai->codec;
  205. u16 iface = 0;
  206. /* set master/slave audio interface */
  207. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  208. case SND_SOC_DAIFMT_CBM_CFM:
  209. iface |= 0x0040;
  210. break;
  211. case SND_SOC_DAIFMT_CBS_CFS:
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. /* interface format */
  217. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  218. case SND_SOC_DAIFMT_I2S:
  219. iface |= 0x0002;
  220. break;
  221. case SND_SOC_DAIFMT_RIGHT_J:
  222. break;
  223. case SND_SOC_DAIFMT_LEFT_J:
  224. iface |= 0x0001;
  225. break;
  226. case SND_SOC_DAIFMT_DSP_A:
  227. iface |= 0x0003;
  228. break;
  229. case SND_SOC_DAIFMT_DSP_B:
  230. iface |= 0x0013;
  231. break;
  232. default:
  233. return -EINVAL;
  234. }
  235. /* clock inversion */
  236. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  237. case SND_SOC_DAIFMT_NB_NF:
  238. break;
  239. case SND_SOC_DAIFMT_IB_IF:
  240. iface |= 0x0090;
  241. break;
  242. case SND_SOC_DAIFMT_IB_NF:
  243. iface |= 0x0080;
  244. break;
  245. case SND_SOC_DAIFMT_NB_IF:
  246. iface |= 0x0010;
  247. break;
  248. default:
  249. return -EINVAL;
  250. }
  251. /* set iface */
  252. snd_soc_write(codec, WM8711_IFACE, iface);
  253. return 0;
  254. }
  255. static int wm8711_set_bias_level(struct snd_soc_codec *codec,
  256. enum snd_soc_bias_level level)
  257. {
  258. u16 reg = snd_soc_read(codec, WM8711_PWR) & 0xff7f;
  259. switch (level) {
  260. case SND_SOC_BIAS_ON:
  261. snd_soc_write(codec, WM8711_PWR, reg);
  262. break;
  263. case SND_SOC_BIAS_PREPARE:
  264. break;
  265. case SND_SOC_BIAS_STANDBY:
  266. snd_soc_write(codec, WM8711_PWR, reg | 0x0040);
  267. break;
  268. case SND_SOC_BIAS_OFF:
  269. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  270. snd_soc_write(codec, WM8711_PWR, 0xffff);
  271. break;
  272. }
  273. codec->dapm.bias_level = level;
  274. return 0;
  275. }
  276. #define WM8711_RATES SNDRV_PCM_RATE_8000_96000
  277. #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  278. SNDRV_PCM_FMTBIT_S24_LE)
  279. static struct snd_soc_dai_ops wm8711_ops = {
  280. .prepare = wm8711_pcm_prepare,
  281. .hw_params = wm8711_hw_params,
  282. .shutdown = wm8711_shutdown,
  283. .digital_mute = wm8711_mute,
  284. .set_sysclk = wm8711_set_dai_sysclk,
  285. .set_fmt = wm8711_set_dai_fmt,
  286. };
  287. static struct snd_soc_dai_driver wm8711_dai = {
  288. .name = "wm8711-hifi",
  289. .playback = {
  290. .stream_name = "Playback",
  291. .channels_min = 1,
  292. .channels_max = 2,
  293. .rates = WM8711_RATES,
  294. .formats = WM8711_FORMATS,
  295. },
  296. .ops = &wm8711_ops,
  297. };
  298. static int wm8711_suspend(struct snd_soc_codec *codec, pm_message_t state)
  299. {
  300. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  301. wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF);
  302. return 0;
  303. }
  304. static int wm8711_resume(struct snd_soc_codec *codec)
  305. {
  306. int i;
  307. u8 data[2];
  308. u16 *cache = codec->reg_cache;
  309. /* Sync reg_cache with the hardware */
  310. for (i = 0; i < ARRAY_SIZE(wm8711_reg); i++) {
  311. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  312. data[1] = cache[i] & 0x00ff;
  313. codec->hw_write(codec->control_data, data, 2);
  314. }
  315. wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  316. return 0;
  317. }
  318. static int wm8711_probe(struct snd_soc_codec *codec)
  319. {
  320. struct wm8711_priv *wm8711 = snd_soc_codec_get_drvdata(codec);
  321. int ret, reg;
  322. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8711->bus_type);
  323. if (ret < 0) {
  324. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  325. return ret;
  326. }
  327. ret = wm8711_reset(codec);
  328. if (ret < 0) {
  329. dev_err(codec->dev, "Failed to issue reset\n");
  330. return ret;
  331. }
  332. wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  333. /* Latch the update bits */
  334. reg = snd_soc_read(codec, WM8711_LOUT1V);
  335. snd_soc_write(codec, WM8711_LOUT1V, reg | 0x0100);
  336. reg = snd_soc_read(codec, WM8711_ROUT1V);
  337. snd_soc_write(codec, WM8711_ROUT1V, reg | 0x0100);
  338. snd_soc_add_controls(codec, wm8711_snd_controls,
  339. ARRAY_SIZE(wm8711_snd_controls));
  340. wm8711_add_widgets(codec);
  341. return ret;
  342. }
  343. /* power down chip */
  344. static int wm8711_remove(struct snd_soc_codec *codec)
  345. {
  346. wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF);
  347. return 0;
  348. }
  349. static struct snd_soc_codec_driver soc_codec_dev_wm8711 = {
  350. .probe = wm8711_probe,
  351. .remove = wm8711_remove,
  352. .suspend = wm8711_suspend,
  353. .resume = wm8711_resume,
  354. .set_bias_level = wm8711_set_bias_level,
  355. .reg_cache_size = ARRAY_SIZE(wm8711_reg),
  356. .reg_word_size = sizeof(u16),
  357. .reg_cache_default = wm8711_reg,
  358. };
  359. #if defined(CONFIG_SPI_MASTER)
  360. static int __devinit wm8711_spi_probe(struct spi_device *spi)
  361. {
  362. struct wm8711_priv *wm8711;
  363. int ret;
  364. wm8711 = kzalloc(sizeof(struct wm8711_priv), GFP_KERNEL);
  365. if (wm8711 == NULL)
  366. return -ENOMEM;
  367. spi_set_drvdata(spi, wm8711);
  368. wm8711->bus_type = SND_SOC_SPI;
  369. ret = snd_soc_register_codec(&spi->dev,
  370. &soc_codec_dev_wm8711, &wm8711_dai, 1);
  371. if (ret < 0)
  372. kfree(wm8711);
  373. return ret;
  374. }
  375. static int __devexit wm8711_spi_remove(struct spi_device *spi)
  376. {
  377. snd_soc_unregister_codec(&spi->dev);
  378. kfree(spi_get_drvdata(spi));
  379. return 0;
  380. }
  381. static struct spi_driver wm8711_spi_driver = {
  382. .driver = {
  383. .name = "wm8711-codec",
  384. .owner = THIS_MODULE,
  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-codec",
  421. .owner = THIS_MODULE,
  422. },
  423. .probe = wm8711_i2c_probe,
  424. .remove = __devexit_p(wm8711_i2c_remove),
  425. .id_table = wm8711_i2c_id,
  426. };
  427. #endif
  428. static int __init wm8711_modinit(void)
  429. {
  430. int ret;
  431. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  432. ret = i2c_add_driver(&wm8711_i2c_driver);
  433. if (ret != 0) {
  434. printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n",
  435. ret);
  436. }
  437. #endif
  438. #if defined(CONFIG_SPI_MASTER)
  439. ret = spi_register_driver(&wm8711_spi_driver);
  440. if (ret != 0) {
  441. printk(KERN_ERR "Failed to register WM8711 SPI driver: %d\n",
  442. ret);
  443. }
  444. #endif
  445. return 0;
  446. }
  447. module_init(wm8711_modinit);
  448. static void __exit wm8711_exit(void)
  449. {
  450. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  451. i2c_del_driver(&wm8711_i2c_driver);
  452. #endif
  453. #if defined(CONFIG_SPI_MASTER)
  454. spi_unregister_driver(&wm8711_spi_driver);
  455. #endif
  456. }
  457. module_exit(wm8711_exit);
  458. MODULE_DESCRIPTION("ASoC WM8711 driver");
  459. MODULE_AUTHOR("Mike Arthur");
  460. MODULE_LICENSE("GPL");