wm8711.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/initval.h>
  27. #include "wm8711.h"
  28. static struct snd_soc_codec *wm8711_codec;
  29. /* codec private data */
  30. struct wm8711_priv {
  31. struct snd_soc_codec codec;
  32. u16 reg_cache[WM8711_CACHEREGNUM];
  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 struct snd_kcontrol_new wm8711_snd_controls[] = {
  47. SOC_DOUBLE_R("Master Playback Volume", WM8711_LOUT1V, WM8711_ROUT1V,
  48. 0, 127, 0),
  49. SOC_DOUBLE_R("Master Playback ZC Switch", WM8711_LOUT1V, WM8711_ROUT1V,
  50. 7, 1, 0),
  51. };
  52. /* add non dapm controls */
  53. static int wm8711_add_controls(struct snd_soc_codec *codec)
  54. {
  55. int err, i;
  56. for (i = 0; i < ARRAY_SIZE(wm8711_snd_controls); i++) {
  57. err = snd_ctl_add(codec->card,
  58. snd_soc_cnew(&wm8711_snd_controls[i], codec,
  59. NULL));
  60. if (err < 0)
  61. return err;
  62. }
  63. return 0;
  64. }
  65. /* Output Mixer */
  66. static const struct snd_kcontrol_new wm8711_output_mixer_controls[] = {
  67. SOC_DAPM_SINGLE("Line Bypass Switch", WM8711_APANA, 3, 1, 0),
  68. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8711_APANA, 4, 1, 0),
  69. };
  70. static const struct snd_soc_dapm_widget wm8711_dapm_widgets[] = {
  71. SND_SOC_DAPM_MIXER("Output Mixer", WM8711_PWR, 4, 1,
  72. &wm8711_output_mixer_controls[0],
  73. ARRAY_SIZE(wm8711_output_mixer_controls)),
  74. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8711_PWR, 3, 1),
  75. SND_SOC_DAPM_OUTPUT("LOUT"),
  76. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  77. SND_SOC_DAPM_OUTPUT("ROUT"),
  78. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  79. };
  80. static const struct snd_soc_dapm_route intercon[] = {
  81. /* output mixer */
  82. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  83. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  84. /* outputs */
  85. {"RHPOUT", NULL, "Output Mixer"},
  86. {"ROUT", NULL, "Output Mixer"},
  87. {"LHPOUT", NULL, "Output Mixer"},
  88. {"LOUT", NULL, "Output Mixer"},
  89. };
  90. static int wm8711_add_widgets(struct snd_soc_codec *codec)
  91. {
  92. snd_soc_dapm_new_controls(codec, wm8711_dapm_widgets,
  93. ARRAY_SIZE(wm8711_dapm_widgets));
  94. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  95. snd_soc_dapm_new_widgets(codec);
  96. return 0;
  97. }
  98. struct _coeff_div {
  99. u32 mclk;
  100. u32 rate;
  101. u16 fs;
  102. u8 sr:4;
  103. u8 bosr:1;
  104. u8 usb:1;
  105. };
  106. /* codec mclk clock divider coefficients */
  107. static const struct _coeff_div coeff_div[] = {
  108. /* 48k */
  109. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  110. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  111. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  112. /* 32k */
  113. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  114. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  115. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  116. /* 8k */
  117. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  118. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  119. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  120. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  121. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  122. /* 96k */
  123. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  124. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  125. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  126. /* 44.1k */
  127. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  128. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  129. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  130. /* 88.2k */
  131. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  132. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  133. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  134. };
  135. static inline int get_coeff(int mclk, int rate)
  136. {
  137. int i;
  138. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  139. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  140. return i;
  141. }
  142. return 0;
  143. }
  144. static int wm8711_hw_params(struct snd_pcm_substream *substream,
  145. struct snd_pcm_hw_params *params,
  146. struct snd_soc_dai *dai)
  147. {
  148. struct snd_soc_codec *codec = dai->codec;
  149. struct wm8711_priv *wm8711 = codec->private_data;
  150. u16 iface = snd_soc_read(codec, WM8711_IFACE) & 0xfffc;
  151. int i = get_coeff(wm8711->sysclk, params_rate(params));
  152. u16 srate = (coeff_div[i].sr << 2) |
  153. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  154. snd_soc_write(codec, WM8711_SRATE, srate);
  155. /* bit size */
  156. switch (params_format(params)) {
  157. case SNDRV_PCM_FORMAT_S16_LE:
  158. break;
  159. case SNDRV_PCM_FORMAT_S20_3LE:
  160. iface |= 0x0004;
  161. break;
  162. case SNDRV_PCM_FORMAT_S24_LE:
  163. iface |= 0x0008;
  164. break;
  165. }
  166. snd_soc_write(codec, WM8711_IFACE, iface);
  167. return 0;
  168. }
  169. static int wm8711_pcm_prepare(struct snd_pcm_substream *substream,
  170. struct snd_soc_dai *dai)
  171. {
  172. struct snd_soc_codec *codec = dai->codec;
  173. /* set active */
  174. snd_soc_write(codec, WM8711_ACTIVE, 0x0001);
  175. return 0;
  176. }
  177. static void wm8711_shutdown(struct snd_pcm_substream *substream,
  178. struct snd_soc_dai *dai)
  179. {
  180. struct snd_soc_codec *codec = dai->codec;
  181. /* deactivate */
  182. if (!codec->active) {
  183. udelay(50);
  184. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  185. }
  186. }
  187. static int wm8711_mute(struct snd_soc_dai *dai, int mute)
  188. {
  189. struct snd_soc_codec *codec = dai->codec;
  190. u16 mute_reg = snd_soc_read(codec, WM8711_APDIGI) & 0xfff7;
  191. if (mute)
  192. snd_soc_write(codec, WM8711_APDIGI, mute_reg | 0x8);
  193. else
  194. snd_soc_write(codec, WM8711_APDIGI, mute_reg);
  195. return 0;
  196. }
  197. static int wm8711_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  198. int clk_id, unsigned int freq, int dir)
  199. {
  200. struct snd_soc_codec *codec = codec_dai->codec;
  201. struct wm8711_priv *wm8711 = codec->private_data;
  202. switch (freq) {
  203. case 11289600:
  204. case 12000000:
  205. case 12288000:
  206. case 16934400:
  207. case 18432000:
  208. wm8711->sysclk = freq;
  209. return 0;
  210. }
  211. return -EINVAL;
  212. }
  213. static int wm8711_set_dai_fmt(struct snd_soc_dai *codec_dai,
  214. unsigned int fmt)
  215. {
  216. struct snd_soc_codec *codec = codec_dai->codec;
  217. u16 iface = 0;
  218. /* set master/slave audio interface */
  219. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  220. case SND_SOC_DAIFMT_CBM_CFM:
  221. iface |= 0x0040;
  222. break;
  223. case SND_SOC_DAIFMT_CBS_CFS:
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. /* interface format */
  229. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  230. case SND_SOC_DAIFMT_I2S:
  231. iface |= 0x0002;
  232. break;
  233. case SND_SOC_DAIFMT_RIGHT_J:
  234. break;
  235. case SND_SOC_DAIFMT_LEFT_J:
  236. iface |= 0x0001;
  237. break;
  238. case SND_SOC_DAIFMT_DSP_A:
  239. iface |= 0x0003;
  240. break;
  241. case SND_SOC_DAIFMT_DSP_B:
  242. iface |= 0x0013;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. /* clock inversion */
  248. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  249. case SND_SOC_DAIFMT_NB_NF:
  250. break;
  251. case SND_SOC_DAIFMT_IB_IF:
  252. iface |= 0x0090;
  253. break;
  254. case SND_SOC_DAIFMT_IB_NF:
  255. iface |= 0x0080;
  256. break;
  257. case SND_SOC_DAIFMT_NB_IF:
  258. iface |= 0x0010;
  259. break;
  260. default:
  261. return -EINVAL;
  262. }
  263. /* set iface */
  264. snd_soc_write(codec, WM8711_IFACE, iface);
  265. return 0;
  266. }
  267. static int wm8711_set_bias_level(struct snd_soc_codec *codec,
  268. enum snd_soc_bias_level level)
  269. {
  270. u16 reg = snd_soc_read(codec, WM8711_PWR) & 0xff7f;
  271. switch (level) {
  272. case SND_SOC_BIAS_ON:
  273. snd_soc_write(codec, WM8711_PWR, reg);
  274. break;
  275. case SND_SOC_BIAS_PREPARE:
  276. break;
  277. case SND_SOC_BIAS_STANDBY:
  278. snd_soc_write(codec, WM8711_PWR, reg | 0x0040);
  279. break;
  280. case SND_SOC_BIAS_OFF:
  281. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  282. snd_soc_write(codec, WM8711_PWR, 0xffff);
  283. break;
  284. }
  285. codec->bias_level = level;
  286. return 0;
  287. }
  288. #define WM8711_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  289. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  290. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  291. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  292. SNDRV_PCM_RATE_96000)
  293. #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  294. SNDRV_PCM_FMTBIT_S24_LE)
  295. static struct snd_soc_dai_ops wm8711_ops = {
  296. .prepare = wm8711_pcm_prepare,
  297. .hw_params = wm8711_hw_params,
  298. .shutdown = wm8711_shutdown,
  299. .digital_mute = wm8711_mute,
  300. .set_sysclk = wm8711_set_dai_sysclk,
  301. .set_fmt = wm8711_set_dai_fmt,
  302. };
  303. struct snd_soc_dai wm8711_dai = {
  304. .name = "WM8711",
  305. .playback = {
  306. .stream_name = "Playback",
  307. .channels_min = 1,
  308. .channels_max = 2,
  309. .rates = WM8711_RATES,
  310. .formats = WM8711_FORMATS,},
  311. .ops = &wm8711_ops,
  312. };
  313. EXPORT_SYMBOL_GPL(wm8711_dai);
  314. static int wm8711_suspend(struct platform_device *pdev, pm_message_t state)
  315. {
  316. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  317. struct snd_soc_codec *codec = socdev->card->codec;
  318. snd_soc_write(codec, WM8711_ACTIVE, 0x0);
  319. wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF);
  320. return 0;
  321. }
  322. static int wm8711_resume(struct platform_device *pdev)
  323. {
  324. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  325. struct snd_soc_codec *codec = socdev->card->codec;
  326. int i;
  327. u8 data[2];
  328. u16 *cache = codec->reg_cache;
  329. /* Sync reg_cache with the hardware */
  330. for (i = 0; i < ARRAY_SIZE(wm8711_reg); i++) {
  331. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  332. data[1] = cache[i] & 0x00ff;
  333. codec->hw_write(codec->control_data, data, 2);
  334. }
  335. wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  336. wm8711_set_bias_level(codec, codec->suspend_bias_level);
  337. return 0;
  338. }
  339. static int wm8711_probe(struct platform_device *pdev)
  340. {
  341. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  342. struct snd_soc_codec *codec;
  343. int ret = 0;
  344. if (wm8711_codec == NULL) {
  345. dev_err(&pdev->dev, "Codec device not registered\n");
  346. return -ENODEV;
  347. }
  348. socdev->card->codec = wm8711_codec;
  349. codec = wm8711_codec;
  350. /* register pcms */
  351. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  352. if (ret < 0) {
  353. dev_err(codec->dev, "failed to create pcms: %d\n", ret);
  354. goto pcm_err;
  355. }
  356. snd_soc_add_controls(codec, wm8711_snd_controls,
  357. ARRAY_SIZE(wm8711_snd_controls));
  358. wm8711_add_widgets(codec);
  359. ret = snd_soc_init_card(socdev);
  360. if (ret < 0) {
  361. dev_err(codec->dev, "failed to register card: %d\n", ret);
  362. goto card_err;
  363. }
  364. return ret;
  365. card_err:
  366. snd_soc_free_pcms(socdev);
  367. snd_soc_dapm_free(socdev);
  368. pcm_err:
  369. return ret;
  370. }
  371. /* power down chip */
  372. static int wm8711_remove(struct platform_device *pdev)
  373. {
  374. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  375. snd_soc_free_pcms(socdev);
  376. snd_soc_dapm_free(socdev);
  377. return 0;
  378. }
  379. struct snd_soc_codec_device soc_codec_dev_wm8711 = {
  380. .probe = wm8711_probe,
  381. .remove = wm8711_remove,
  382. .suspend = wm8711_suspend,
  383. .resume = wm8711_resume,
  384. };
  385. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8711);
  386. static int wm8711_register(struct wm8711_priv *wm8711,
  387. enum snd_soc_control_type control)
  388. {
  389. int ret;
  390. struct snd_soc_codec *codec = &wm8711->codec;
  391. u16 reg;
  392. if (wm8711_codec) {
  393. dev_err(codec->dev, "Another WM8711 is registered\n");
  394. return -EINVAL;
  395. }
  396. mutex_init(&codec->mutex);
  397. INIT_LIST_HEAD(&codec->dapm_widgets);
  398. INIT_LIST_HEAD(&codec->dapm_paths);
  399. codec->private_data = wm8711;
  400. codec->name = "WM8711";
  401. codec->owner = THIS_MODULE;
  402. codec->bias_level = SND_SOC_BIAS_OFF;
  403. codec->set_bias_level = wm8711_set_bias_level;
  404. codec->dai = &wm8711_dai;
  405. codec->num_dai = 1;
  406. codec->reg_cache_size = WM8711_CACHEREGNUM;
  407. codec->reg_cache = &wm8711->reg_cache;
  408. memcpy(codec->reg_cache, wm8711_reg, sizeof(wm8711_reg));
  409. ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
  410. if (ret < 0) {
  411. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  412. goto err;
  413. }
  414. ret = wm8711_reset(codec);
  415. if (ret < 0) {
  416. dev_err(codec->dev, "Failed to issue reset\n");
  417. goto err;
  418. }
  419. wm8711_dai.dev = codec->dev;
  420. wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  421. /* Latch the update bits */
  422. reg = snd_soc_read(codec, WM8711_LOUT1V);
  423. snd_soc_write(codec, WM8711_LOUT1V, reg | 0x0100);
  424. reg = snd_soc_read(codec, WM8711_ROUT1V);
  425. snd_soc_write(codec, WM8711_ROUT1V, reg | 0x0100);
  426. wm8711_codec = codec;
  427. ret = snd_soc_register_codec(codec);
  428. if (ret != 0) {
  429. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  430. goto err;
  431. }
  432. ret = snd_soc_register_dai(&wm8711_dai);
  433. if (ret != 0) {
  434. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  435. goto err_codec;
  436. }
  437. return 0;
  438. err_codec:
  439. snd_soc_unregister_codec(codec);
  440. err:
  441. kfree(wm8711);
  442. return ret;
  443. }
  444. static void wm8711_unregister(struct wm8711_priv *wm8711)
  445. {
  446. wm8711_set_bias_level(&wm8711->codec, SND_SOC_BIAS_OFF);
  447. snd_soc_unregister_dai(&wm8711_dai);
  448. snd_soc_unregister_codec(&wm8711->codec);
  449. kfree(wm8711);
  450. wm8711_codec = NULL;
  451. }
  452. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  453. static __devinit int wm8711_i2c_probe(struct i2c_client *i2c,
  454. const struct i2c_device_id *id)
  455. {
  456. struct wm8711_priv *wm8711;
  457. struct snd_soc_codec *codec;
  458. wm8711 = kzalloc(sizeof(struct wm8711_priv), GFP_KERNEL);
  459. if (wm8711 == NULL)
  460. return -ENOMEM;
  461. codec = &wm8711->codec;
  462. codec->hw_write = (hw_write_t)i2c_master_send;
  463. i2c_set_clientdata(i2c, wm8711);
  464. codec->control_data = i2c;
  465. codec->dev = &i2c->dev;
  466. return wm8711_register(wm8711, SND_SOC_I2C);
  467. }
  468. static __devexit int wm8711_i2c_remove(struct i2c_client *client)
  469. {
  470. struct wm8711_priv *wm8711 = i2c_get_clientdata(client);
  471. wm8711_unregister(wm8711);
  472. return 0;
  473. }
  474. static const struct i2c_device_id wm8711_i2c_id[] = {
  475. { "wm8711", 0 },
  476. { }
  477. };
  478. MODULE_DEVICE_TABLE(i2c, wm8711_i2c_id);
  479. static struct i2c_driver wm8711_i2c_driver = {
  480. .driver = {
  481. .name = "WM8711 I2C Codec",
  482. .owner = THIS_MODULE,
  483. },
  484. .probe = wm8711_i2c_probe,
  485. .remove = __devexit_p(wm8711_i2c_remove),
  486. .id_table = wm8711_i2c_id,
  487. };
  488. #endif
  489. static int __init wm8711_modinit(void)
  490. {
  491. int ret;
  492. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  493. ret = i2c_add_driver(&wm8711_i2c_driver);
  494. if (ret != 0) {
  495. printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n",
  496. ret);
  497. }
  498. #endif
  499. return 0;
  500. }
  501. module_init(wm8711_modinit);
  502. static void __exit wm8711_exit(void)
  503. {
  504. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  505. i2c_del_driver(&wm8711_i2c_driver);
  506. #endif
  507. }
  508. module_exit(wm8711_exit);
  509. MODULE_DESCRIPTION("ASoC WM8711 driver");
  510. MODULE_AUTHOR("Mike Arthur");
  511. MODULE_LICENSE("GPL");