wm8711.c 12 KB

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