wm8741.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * wm8741.c -- WM8741 ALSA SoC Audio driver
  3. *
  4. * Copyright 2010 Wolfson Microelectronics plc
  5. *
  6. * Author: Ian Lartey <ian@opensource.wolfsonmicro.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/slab.h>
  22. #include <linux/of_device.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/initval.h>
  28. #include <sound/tlv.h>
  29. #include "wm8741.h"
  30. #define WM8741_NUM_SUPPLIES 2
  31. static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
  32. "AVDD",
  33. "DVDD",
  34. };
  35. #define WM8741_NUM_RATES 6
  36. /* codec private data */
  37. struct wm8741_priv {
  38. enum snd_soc_control_type control_type;
  39. struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
  40. unsigned int sysclk;
  41. struct snd_pcm_hw_constraint_list *sysclk_constraints;
  42. };
  43. static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = {
  44. 0x0000, /* R0 - DACLLSB Attenuation */
  45. 0x0000, /* R1 - DACLMSB Attenuation */
  46. 0x0000, /* R2 - DACRLSB Attenuation */
  47. 0x0000, /* R3 - DACRMSB Attenuation */
  48. 0x0000, /* R4 - Volume Control */
  49. 0x000A, /* R5 - Format Control */
  50. 0x0000, /* R6 - Filter Control */
  51. 0x0000, /* R7 - Mode Control 1 */
  52. 0x0002, /* R8 - Mode Control 2 */
  53. 0x0000, /* R9 - Reset */
  54. 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
  55. };
  56. static int wm8741_reset(struct snd_soc_codec *codec)
  57. {
  58. return snd_soc_write(codec, WM8741_RESET, 0);
  59. }
  60. static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
  61. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
  62. static const struct snd_kcontrol_new wm8741_snd_controls[] = {
  63. SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
  64. WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
  65. SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
  66. WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
  67. };
  68. static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
  69. SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
  70. SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
  71. SND_SOC_DAPM_OUTPUT("VOUTLP"),
  72. SND_SOC_DAPM_OUTPUT("VOUTLN"),
  73. SND_SOC_DAPM_OUTPUT("VOUTRP"),
  74. SND_SOC_DAPM_OUTPUT("VOUTRN"),
  75. };
  76. static const struct snd_soc_dapm_route wm8741_dapm_routes[] = {
  77. { "VOUTLP", NULL, "DACL" },
  78. { "VOUTLN", NULL, "DACL" },
  79. { "VOUTRP", NULL, "DACR" },
  80. { "VOUTRN", NULL, "DACR" },
  81. };
  82. static struct {
  83. int value;
  84. int ratio;
  85. } lrclk_ratios[WM8741_NUM_RATES] = {
  86. { 1, 128 },
  87. { 2, 192 },
  88. { 3, 256 },
  89. { 4, 384 },
  90. { 5, 512 },
  91. { 6, 768 },
  92. };
  93. static unsigned int rates_11289[] = {
  94. 44100, 88200,
  95. };
  96. static struct snd_pcm_hw_constraint_list constraints_11289 = {
  97. .count = ARRAY_SIZE(rates_11289),
  98. .list = rates_11289,
  99. };
  100. static unsigned int rates_12288[] = {
  101. 32000, 48000, 96000,
  102. };
  103. static struct snd_pcm_hw_constraint_list constraints_12288 = {
  104. .count = ARRAY_SIZE(rates_12288),
  105. .list = rates_12288,
  106. };
  107. static unsigned int rates_16384[] = {
  108. 32000,
  109. };
  110. static struct snd_pcm_hw_constraint_list constraints_16384 = {
  111. .count = ARRAY_SIZE(rates_16384),
  112. .list = rates_16384,
  113. };
  114. static unsigned int rates_16934[] = {
  115. 44100, 88200,
  116. };
  117. static struct snd_pcm_hw_constraint_list constraints_16934 = {
  118. .count = ARRAY_SIZE(rates_16934),
  119. .list = rates_16934,
  120. };
  121. static unsigned int rates_18432[] = {
  122. 48000, 96000,
  123. };
  124. static struct snd_pcm_hw_constraint_list constraints_18432 = {
  125. .count = ARRAY_SIZE(rates_18432),
  126. .list = rates_18432,
  127. };
  128. static unsigned int rates_22579[] = {
  129. 44100, 88200, 176400
  130. };
  131. static struct snd_pcm_hw_constraint_list constraints_22579 = {
  132. .count = ARRAY_SIZE(rates_22579),
  133. .list = rates_22579,
  134. };
  135. static unsigned int rates_24576[] = {
  136. 32000, 48000, 96000, 192000
  137. };
  138. static struct snd_pcm_hw_constraint_list constraints_24576 = {
  139. .count = ARRAY_SIZE(rates_24576),
  140. .list = rates_24576,
  141. };
  142. static unsigned int rates_36864[] = {
  143. 48000, 96000, 192000
  144. };
  145. static struct snd_pcm_hw_constraint_list constraints_36864 = {
  146. .count = ARRAY_SIZE(rates_36864),
  147. .list = rates_36864,
  148. };
  149. static int wm8741_startup(struct snd_pcm_substream *substream,
  150. struct snd_soc_dai *dai)
  151. {
  152. struct snd_soc_codec *codec = dai->codec;
  153. struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
  154. /* The set of sample rates that can be supported depends on the
  155. * MCLK supplied to the CODEC - enforce this.
  156. */
  157. if (!wm8741->sysclk) {
  158. dev_err(codec->dev,
  159. "No MCLK configured, call set_sysclk() on init\n");
  160. return -EINVAL;
  161. }
  162. snd_pcm_hw_constraint_list(substream->runtime, 0,
  163. SNDRV_PCM_HW_PARAM_RATE,
  164. wm8741->sysclk_constraints);
  165. return 0;
  166. }
  167. static int wm8741_hw_params(struct snd_pcm_substream *substream,
  168. struct snd_pcm_hw_params *params,
  169. struct snd_soc_dai *dai)
  170. {
  171. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  172. struct snd_soc_codec *codec = rtd->codec;
  173. struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
  174. u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
  175. int i;
  176. /* Find a supported LRCLK ratio */
  177. for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
  178. if (wm8741->sysclk / params_rate(params) ==
  179. lrclk_ratios[i].ratio)
  180. break;
  181. }
  182. /* Should never happen, should be handled by constraints */
  183. if (i == ARRAY_SIZE(lrclk_ratios)) {
  184. dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
  185. wm8741->sysclk / params_rate(params));
  186. return -EINVAL;
  187. }
  188. /* bit size */
  189. switch (params_format(params)) {
  190. case SNDRV_PCM_FORMAT_S16_LE:
  191. break;
  192. case SNDRV_PCM_FORMAT_S20_3LE:
  193. iface |= 0x0001;
  194. break;
  195. case SNDRV_PCM_FORMAT_S24_LE:
  196. iface |= 0x0002;
  197. break;
  198. case SNDRV_PCM_FORMAT_S32_LE:
  199. iface |= 0x0003;
  200. break;
  201. default:
  202. dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
  203. params_format(params));
  204. return -EINVAL;
  205. }
  206. dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d",
  207. params_format(params));
  208. snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
  209. return 0;
  210. }
  211. static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  212. int clk_id, unsigned int freq, int dir)
  213. {
  214. struct snd_soc_codec *codec = codec_dai->codec;
  215. struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
  216. dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
  217. switch (freq) {
  218. case 11289600:
  219. wm8741->sysclk_constraints = &constraints_11289;
  220. wm8741->sysclk = freq;
  221. return 0;
  222. case 12288000:
  223. wm8741->sysclk_constraints = &constraints_12288;
  224. wm8741->sysclk = freq;
  225. return 0;
  226. case 16384000:
  227. wm8741->sysclk_constraints = &constraints_16384;
  228. wm8741->sysclk = freq;
  229. return 0;
  230. case 16934400:
  231. wm8741->sysclk_constraints = &constraints_16934;
  232. wm8741->sysclk = freq;
  233. return 0;
  234. case 18432000:
  235. wm8741->sysclk_constraints = &constraints_18432;
  236. wm8741->sysclk = freq;
  237. return 0;
  238. case 22579200:
  239. case 33868800:
  240. wm8741->sysclk_constraints = &constraints_22579;
  241. wm8741->sysclk = freq;
  242. return 0;
  243. case 24576000:
  244. wm8741->sysclk_constraints = &constraints_24576;
  245. wm8741->sysclk = freq;
  246. return 0;
  247. case 36864000:
  248. wm8741->sysclk_constraints = &constraints_36864;
  249. wm8741->sysclk = freq;
  250. return 0;
  251. }
  252. return -EINVAL;
  253. }
  254. static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
  255. unsigned int fmt)
  256. {
  257. struct snd_soc_codec *codec = codec_dai->codec;
  258. u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3;
  259. /* check master/slave audio interface */
  260. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  261. case SND_SOC_DAIFMT_CBS_CFS:
  262. break;
  263. default:
  264. return -EINVAL;
  265. }
  266. /* interface format */
  267. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  268. case SND_SOC_DAIFMT_I2S:
  269. iface |= 0x0008;
  270. break;
  271. case SND_SOC_DAIFMT_RIGHT_J:
  272. break;
  273. case SND_SOC_DAIFMT_LEFT_J:
  274. iface |= 0x0004;
  275. break;
  276. case SND_SOC_DAIFMT_DSP_A:
  277. iface |= 0x000C;
  278. break;
  279. case SND_SOC_DAIFMT_DSP_B:
  280. iface |= 0x001C;
  281. break;
  282. default:
  283. return -EINVAL;
  284. }
  285. /* clock inversion */
  286. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  287. case SND_SOC_DAIFMT_NB_NF:
  288. break;
  289. case SND_SOC_DAIFMT_IB_IF:
  290. iface |= 0x0010;
  291. break;
  292. case SND_SOC_DAIFMT_IB_NF:
  293. iface |= 0x0020;
  294. break;
  295. case SND_SOC_DAIFMT_NB_IF:
  296. iface |= 0x0030;
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
  302. fmt & SND_SOC_DAIFMT_FORMAT_MASK,
  303. ((fmt & SND_SOC_DAIFMT_INV_MASK)));
  304. snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
  305. return 0;
  306. }
  307. #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  308. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
  309. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
  310. SNDRV_PCM_RATE_192000)
  311. #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  312. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  313. static const struct snd_soc_dai_ops wm8741_dai_ops = {
  314. .startup = wm8741_startup,
  315. .hw_params = wm8741_hw_params,
  316. .set_sysclk = wm8741_set_dai_sysclk,
  317. .set_fmt = wm8741_set_dai_fmt,
  318. };
  319. static struct snd_soc_dai_driver wm8741_dai = {
  320. .name = "wm8741",
  321. .playback = {
  322. .stream_name = "Playback",
  323. .channels_min = 2, /* Mono modes not yet supported */
  324. .channels_max = 2,
  325. .rates = WM8741_RATES,
  326. .formats = WM8741_FORMATS,
  327. },
  328. .ops = &wm8741_dai_ops,
  329. };
  330. #ifdef CONFIG_PM
  331. static int wm8741_resume(struct snd_soc_codec *codec)
  332. {
  333. snd_soc_cache_sync(codec);
  334. return 0;
  335. }
  336. #else
  337. #define wm8741_suspend NULL
  338. #define wm8741_resume NULL
  339. #endif
  340. static int wm8741_probe(struct snd_soc_codec *codec)
  341. {
  342. struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
  343. int ret = 0;
  344. int i;
  345. for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
  346. wm8741->supplies[i].supply = wm8741_supply_names[i];
  347. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8741->supplies),
  348. wm8741->supplies);
  349. if (ret != 0) {
  350. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  351. goto err;
  352. }
  353. ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
  354. wm8741->supplies);
  355. if (ret != 0) {
  356. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  357. goto err_get;
  358. }
  359. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8741->control_type);
  360. if (ret != 0) {
  361. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  362. goto err_enable;
  363. }
  364. ret = wm8741_reset(codec);
  365. if (ret < 0) {
  366. dev_err(codec->dev, "Failed to issue reset\n");
  367. goto err_enable;
  368. }
  369. /* Change some default settings - latch VU */
  370. snd_soc_update_bits(codec, WM8741_DACLLSB_ATTENUATION,
  371. WM8741_UPDATELL, WM8741_UPDATELL);
  372. snd_soc_update_bits(codec, WM8741_DACLMSB_ATTENUATION,
  373. WM8741_UPDATELM, WM8741_UPDATELM);
  374. snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION,
  375. WM8741_UPDATERL, WM8741_UPDATERL);
  376. snd_soc_update_bits(codec, WM8741_DACRMSB_ATTENUATION,
  377. WM8741_UPDATERM, WM8741_UPDATERM);
  378. dev_dbg(codec->dev, "Successful registration\n");
  379. return ret;
  380. err_enable:
  381. regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
  382. err_get:
  383. regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
  384. err:
  385. return ret;
  386. }
  387. static int wm8741_remove(struct snd_soc_codec *codec)
  388. {
  389. struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
  390. regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
  391. regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
  392. return 0;
  393. }
  394. static struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
  395. .probe = wm8741_probe,
  396. .remove = wm8741_remove,
  397. .resume = wm8741_resume,
  398. .reg_cache_size = ARRAY_SIZE(wm8741_reg_defaults),
  399. .reg_word_size = sizeof(u16),
  400. .reg_cache_default = wm8741_reg_defaults,
  401. .controls = wm8741_snd_controls,
  402. .num_controls = ARRAY_SIZE(wm8741_snd_controls),
  403. .dapm_widgets = wm8741_dapm_widgets,
  404. .num_dapm_widgets = ARRAY_SIZE(wm8741_dapm_widgets),
  405. .dapm_routes = wm8741_dapm_routes,
  406. .num_dapm_routes = ARRAY_SIZE(wm8741_dapm_routes),
  407. };
  408. static const struct of_device_id wm8741_of_match[] = {
  409. { .compatible = "wlf,wm8741", },
  410. { }
  411. };
  412. MODULE_DEVICE_TABLE(of, wm8741_of_match);
  413. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  414. static int wm8741_i2c_probe(struct i2c_client *i2c,
  415. const struct i2c_device_id *id)
  416. {
  417. struct wm8741_priv *wm8741;
  418. int ret;
  419. wm8741 = devm_kzalloc(&i2c->dev, sizeof(struct wm8741_priv),
  420. GFP_KERNEL);
  421. if (wm8741 == NULL)
  422. return -ENOMEM;
  423. i2c_set_clientdata(i2c, wm8741);
  424. wm8741->control_type = SND_SOC_I2C;
  425. ret = snd_soc_register_codec(&i2c->dev,
  426. &soc_codec_dev_wm8741, &wm8741_dai, 1);
  427. return ret;
  428. }
  429. static int wm8741_i2c_remove(struct i2c_client *client)
  430. {
  431. snd_soc_unregister_codec(&client->dev);
  432. return 0;
  433. }
  434. static const struct i2c_device_id wm8741_i2c_id[] = {
  435. { "wm8741", 0 },
  436. { }
  437. };
  438. MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
  439. static struct i2c_driver wm8741_i2c_driver = {
  440. .driver = {
  441. .name = "wm8741",
  442. .owner = THIS_MODULE,
  443. .of_match_table = wm8741_of_match,
  444. },
  445. .probe = wm8741_i2c_probe,
  446. .remove = wm8741_i2c_remove,
  447. .id_table = wm8741_i2c_id,
  448. };
  449. #endif
  450. #if defined(CONFIG_SPI_MASTER)
  451. static int __devinit wm8741_spi_probe(struct spi_device *spi)
  452. {
  453. struct wm8741_priv *wm8741;
  454. int ret;
  455. wm8741 = devm_kzalloc(&spi->dev, sizeof(struct wm8741_priv),
  456. GFP_KERNEL);
  457. if (wm8741 == NULL)
  458. return -ENOMEM;
  459. wm8741->control_type = SND_SOC_SPI;
  460. spi_set_drvdata(spi, wm8741);
  461. ret = snd_soc_register_codec(&spi->dev,
  462. &soc_codec_dev_wm8741, &wm8741_dai, 1);
  463. return ret;
  464. }
  465. static int __devexit wm8741_spi_remove(struct spi_device *spi)
  466. {
  467. snd_soc_unregister_codec(&spi->dev);
  468. return 0;
  469. }
  470. static struct spi_driver wm8741_spi_driver = {
  471. .driver = {
  472. .name = "wm8741",
  473. .owner = THIS_MODULE,
  474. .of_match_table = wm8741_of_match,
  475. },
  476. .probe = wm8741_spi_probe,
  477. .remove = __devexit_p(wm8741_spi_remove),
  478. };
  479. #endif /* CONFIG_SPI_MASTER */
  480. static int __init wm8741_modinit(void)
  481. {
  482. int ret = 0;
  483. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  484. ret = i2c_add_driver(&wm8741_i2c_driver);
  485. if (ret != 0)
  486. pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
  487. #endif
  488. #if defined(CONFIG_SPI_MASTER)
  489. ret = spi_register_driver(&wm8741_spi_driver);
  490. if (ret != 0) {
  491. printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n",
  492. ret);
  493. }
  494. #endif
  495. return ret;
  496. }
  497. module_init(wm8741_modinit);
  498. static void __exit wm8741_exit(void)
  499. {
  500. #if defined(CONFIG_SPI_MASTER)
  501. spi_unregister_driver(&wm8741_spi_driver);
  502. #endif
  503. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  504. i2c_del_driver(&wm8741_i2c_driver);
  505. #endif
  506. }
  507. module_exit(wm8741_exit);
  508. MODULE_DESCRIPTION("ASoC WM8741 driver");
  509. MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
  510. MODULE_LICENSE("GPL");