wm8741.c 14 KB

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