wm8741.c 13 KB

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