wm8741.c 12 KB

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