wm8523.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * wm8523.c -- WM8523 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@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/regulator/consumer.h>
  20. #include <linux/slab.h>
  21. #include <linux/of_device.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/initval.h>
  27. #include <sound/tlv.h>
  28. #include "wm8523.h"
  29. #define WM8523_NUM_SUPPLIES 2
  30. static const char *wm8523_supply_names[WM8523_NUM_SUPPLIES] = {
  31. "AVDD",
  32. "LINEVDD",
  33. };
  34. #define WM8523_NUM_RATES 7
  35. /* codec private data */
  36. struct wm8523_priv {
  37. enum snd_soc_control_type control_type;
  38. struct regulator_bulk_data supplies[WM8523_NUM_SUPPLIES];
  39. unsigned int sysclk;
  40. unsigned int rate_constraint_list[WM8523_NUM_RATES];
  41. struct snd_pcm_hw_constraint_list rate_constraint;
  42. };
  43. static const u16 wm8523_reg[WM8523_REGISTER_COUNT] = {
  44. 0x8523, /* R0 - DEVICE_ID */
  45. 0x0001, /* R1 - REVISION */
  46. 0x0000, /* R2 - PSCTRL1 */
  47. 0x1812, /* R3 - AIF_CTRL1 */
  48. 0x0000, /* R4 - AIF_CTRL2 */
  49. 0x0001, /* R5 - DAC_CTRL3 */
  50. 0x0190, /* R6 - DAC_GAINL */
  51. 0x0190, /* R7 - DAC_GAINR */
  52. 0x0000, /* R8 - ZERO_DETECT */
  53. };
  54. static int wm8523_volatile_register(struct snd_soc_codec *codec, unsigned int reg)
  55. {
  56. switch (reg) {
  57. case WM8523_DEVICE_ID:
  58. case WM8523_REVISION:
  59. return 1;
  60. default:
  61. return 0;
  62. }
  63. }
  64. static int wm8523_reset(struct snd_soc_codec *codec)
  65. {
  66. return snd_soc_write(codec, WM8523_DEVICE_ID, 0);
  67. }
  68. static const DECLARE_TLV_DB_SCALE(dac_tlv, -10000, 25, 0);
  69. static const char *wm8523_zd_count_text[] = {
  70. "1024",
  71. "2048",
  72. };
  73. static const struct soc_enum wm8523_zc_count =
  74. SOC_ENUM_SINGLE(WM8523_ZERO_DETECT, 0, 2, wm8523_zd_count_text);
  75. static const struct snd_kcontrol_new wm8523_controls[] = {
  76. SOC_DOUBLE_R_TLV("Playback Volume", WM8523_DAC_GAINL, WM8523_DAC_GAINR,
  77. 0, 448, 0, dac_tlv),
  78. SOC_SINGLE("ZC Switch", WM8523_DAC_CTRL3, 4, 1, 0),
  79. SOC_SINGLE("Playback Deemphasis Switch", WM8523_AIF_CTRL1, 8, 1, 0),
  80. SOC_DOUBLE("Playback Switch", WM8523_DAC_CTRL3, 2, 3, 1, 1),
  81. SOC_SINGLE("Volume Ramp Up Switch", WM8523_DAC_CTRL3, 1, 1, 0),
  82. SOC_SINGLE("Volume Ramp Down Switch", WM8523_DAC_CTRL3, 0, 1, 0),
  83. SOC_ENUM("Zero Detect Count", wm8523_zc_count),
  84. };
  85. static const struct snd_soc_dapm_widget wm8523_dapm_widgets[] = {
  86. SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
  87. SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
  88. SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
  89. };
  90. static const struct snd_soc_dapm_route wm8523_dapm_routes[] = {
  91. { "LINEVOUTL", NULL, "DAC" },
  92. { "LINEVOUTR", NULL, "DAC" },
  93. };
  94. static struct {
  95. int value;
  96. int ratio;
  97. } lrclk_ratios[WM8523_NUM_RATES] = {
  98. { 1, 128 },
  99. { 2, 192 },
  100. { 3, 256 },
  101. { 4, 384 },
  102. { 5, 512 },
  103. { 6, 768 },
  104. { 7, 1152 },
  105. };
  106. static int wm8523_startup(struct snd_pcm_substream *substream,
  107. struct snd_soc_dai *dai)
  108. {
  109. struct snd_soc_codec *codec = dai->codec;
  110. struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
  111. /* The set of sample rates that can be supported depends on the
  112. * MCLK supplied to the CODEC - enforce this.
  113. */
  114. if (!wm8523->sysclk) {
  115. dev_err(codec->dev,
  116. "No MCLK configured, call set_sysclk() on init\n");
  117. return -EINVAL;
  118. }
  119. snd_pcm_hw_constraint_list(substream->runtime, 0,
  120. SNDRV_PCM_HW_PARAM_RATE,
  121. &wm8523->rate_constraint);
  122. return 0;
  123. }
  124. static int wm8523_hw_params(struct snd_pcm_substream *substream,
  125. struct snd_pcm_hw_params *params,
  126. struct snd_soc_dai *dai)
  127. {
  128. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  129. struct snd_soc_codec *codec = rtd->codec;
  130. struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
  131. int i;
  132. u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
  133. u16 aifctrl2 = snd_soc_read(codec, WM8523_AIF_CTRL2);
  134. /* Find a supported LRCLK ratio */
  135. for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
  136. if (wm8523->sysclk / params_rate(params) ==
  137. lrclk_ratios[i].ratio)
  138. break;
  139. }
  140. /* Should never happen, should be handled by constraints */
  141. if (i == ARRAY_SIZE(lrclk_ratios)) {
  142. dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
  143. wm8523->sysclk / params_rate(params));
  144. return -EINVAL;
  145. }
  146. aifctrl2 &= ~WM8523_SR_MASK;
  147. aifctrl2 |= lrclk_ratios[i].value;
  148. aifctrl1 &= ~WM8523_WL_MASK;
  149. switch (params_format(params)) {
  150. case SNDRV_PCM_FORMAT_S16_LE:
  151. break;
  152. case SNDRV_PCM_FORMAT_S20_3LE:
  153. aifctrl1 |= 0x8;
  154. break;
  155. case SNDRV_PCM_FORMAT_S24_LE:
  156. aifctrl1 |= 0x10;
  157. break;
  158. case SNDRV_PCM_FORMAT_S32_LE:
  159. aifctrl1 |= 0x18;
  160. break;
  161. }
  162. snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
  163. snd_soc_write(codec, WM8523_AIF_CTRL2, aifctrl2);
  164. return 0;
  165. }
  166. static int wm8523_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  167. int clk_id, unsigned int freq, int dir)
  168. {
  169. struct snd_soc_codec *codec = codec_dai->codec;
  170. struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
  171. unsigned int val;
  172. int i;
  173. wm8523->sysclk = freq;
  174. wm8523->rate_constraint.count = 0;
  175. for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
  176. val = freq / lrclk_ratios[i].ratio;
  177. /* Check that it's a standard rate since core can't
  178. * cope with others and having the odd rates confuses
  179. * constraint matching.
  180. */
  181. switch (val) {
  182. case 8000:
  183. case 11025:
  184. case 16000:
  185. case 22050:
  186. case 32000:
  187. case 44100:
  188. case 48000:
  189. case 64000:
  190. case 88200:
  191. case 96000:
  192. case 176400:
  193. case 192000:
  194. dev_dbg(codec->dev, "Supported sample rate: %dHz\n",
  195. val);
  196. wm8523->rate_constraint_list[i] = val;
  197. wm8523->rate_constraint.count++;
  198. break;
  199. default:
  200. dev_dbg(codec->dev, "Skipping sample rate: %dHz\n",
  201. val);
  202. }
  203. }
  204. /* Need at least one supported rate... */
  205. if (wm8523->rate_constraint.count == 0)
  206. return -EINVAL;
  207. return 0;
  208. }
  209. static int wm8523_set_dai_fmt(struct snd_soc_dai *codec_dai,
  210. unsigned int fmt)
  211. {
  212. struct snd_soc_codec *codec = codec_dai->codec;
  213. u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
  214. aifctrl1 &= ~(WM8523_BCLK_INV_MASK | WM8523_LRCLK_INV_MASK |
  215. WM8523_FMT_MASK | WM8523_AIF_MSTR_MASK);
  216. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  217. case SND_SOC_DAIFMT_CBM_CFM:
  218. aifctrl1 |= WM8523_AIF_MSTR;
  219. break;
  220. case SND_SOC_DAIFMT_CBS_CFS:
  221. break;
  222. default:
  223. return -EINVAL;
  224. }
  225. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  226. case SND_SOC_DAIFMT_I2S:
  227. aifctrl1 |= 0x0002;
  228. break;
  229. case SND_SOC_DAIFMT_RIGHT_J:
  230. break;
  231. case SND_SOC_DAIFMT_LEFT_J:
  232. aifctrl1 |= 0x0001;
  233. break;
  234. case SND_SOC_DAIFMT_DSP_A:
  235. aifctrl1 |= 0x0003;
  236. break;
  237. case SND_SOC_DAIFMT_DSP_B:
  238. aifctrl1 |= 0x0023;
  239. break;
  240. default:
  241. return -EINVAL;
  242. }
  243. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  244. case SND_SOC_DAIFMT_NB_NF:
  245. break;
  246. case SND_SOC_DAIFMT_IB_IF:
  247. aifctrl1 |= WM8523_BCLK_INV | WM8523_LRCLK_INV;
  248. break;
  249. case SND_SOC_DAIFMT_IB_NF:
  250. aifctrl1 |= WM8523_BCLK_INV;
  251. break;
  252. case SND_SOC_DAIFMT_NB_IF:
  253. aifctrl1 |= WM8523_LRCLK_INV;
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
  259. return 0;
  260. }
  261. static int wm8523_set_bias_level(struct snd_soc_codec *codec,
  262. enum snd_soc_bias_level level)
  263. {
  264. struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
  265. u16 *reg_cache = codec->reg_cache;
  266. int ret, i;
  267. switch (level) {
  268. case SND_SOC_BIAS_ON:
  269. break;
  270. case SND_SOC_BIAS_PREPARE:
  271. /* Full power on */
  272. snd_soc_update_bits(codec, WM8523_PSCTRL1,
  273. WM8523_SYS_ENA_MASK, 3);
  274. break;
  275. case SND_SOC_BIAS_STANDBY:
  276. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  277. ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
  278. wm8523->supplies);
  279. if (ret != 0) {
  280. dev_err(codec->dev,
  281. "Failed to enable supplies: %d\n",
  282. ret);
  283. return ret;
  284. }
  285. /* Initial power up */
  286. snd_soc_update_bits(codec, WM8523_PSCTRL1,
  287. WM8523_SYS_ENA_MASK, 1);
  288. /* Sync back default/cached values */
  289. for (i = WM8523_AIF_CTRL1;
  290. i < WM8523_MAX_REGISTER; i++)
  291. snd_soc_write(codec, i, reg_cache[i]);
  292. msleep(100);
  293. }
  294. /* Power up to mute */
  295. snd_soc_update_bits(codec, WM8523_PSCTRL1,
  296. WM8523_SYS_ENA_MASK, 2);
  297. break;
  298. case SND_SOC_BIAS_OFF:
  299. /* The chip runs through the power down sequence for us. */
  300. snd_soc_update_bits(codec, WM8523_PSCTRL1,
  301. WM8523_SYS_ENA_MASK, 0);
  302. msleep(100);
  303. regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies),
  304. wm8523->supplies);
  305. break;
  306. }
  307. codec->dapm.bias_level = level;
  308. return 0;
  309. }
  310. #define WM8523_RATES SNDRV_PCM_RATE_8000_192000
  311. #define WM8523_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 wm8523_dai_ops = {
  314. .startup = wm8523_startup,
  315. .hw_params = wm8523_hw_params,
  316. .set_sysclk = wm8523_set_dai_sysclk,
  317. .set_fmt = wm8523_set_dai_fmt,
  318. };
  319. static struct snd_soc_dai_driver wm8523_dai = {
  320. .name = "wm8523-hifi",
  321. .playback = {
  322. .stream_name = "Playback",
  323. .channels_min = 2, /* Mono modes not yet supported */
  324. .channels_max = 2,
  325. .rates = WM8523_RATES,
  326. .formats = WM8523_FORMATS,
  327. },
  328. .ops = &wm8523_dai_ops,
  329. };
  330. #ifdef CONFIG_PM
  331. static int wm8523_suspend(struct snd_soc_codec *codec)
  332. {
  333. wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
  334. return 0;
  335. }
  336. static int wm8523_resume(struct snd_soc_codec *codec)
  337. {
  338. wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  339. return 0;
  340. }
  341. #else
  342. #define wm8523_suspend NULL
  343. #define wm8523_resume NULL
  344. #endif
  345. static int wm8523_probe(struct snd_soc_codec *codec)
  346. {
  347. struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
  348. int ret, i;
  349. wm8523->rate_constraint.list = &wm8523->rate_constraint_list[0];
  350. wm8523->rate_constraint.count =
  351. ARRAY_SIZE(wm8523->rate_constraint_list);
  352. ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8523->control_type);
  353. if (ret != 0) {
  354. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  355. return ret;
  356. }
  357. for (i = 0; i < ARRAY_SIZE(wm8523->supplies); i++)
  358. wm8523->supplies[i].supply = wm8523_supply_names[i];
  359. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8523->supplies),
  360. wm8523->supplies);
  361. if (ret != 0) {
  362. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  363. return ret;
  364. }
  365. ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
  366. wm8523->supplies);
  367. if (ret != 0) {
  368. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  369. goto err_get;
  370. }
  371. ret = snd_soc_read(codec, WM8523_DEVICE_ID);
  372. if (ret < 0) {
  373. dev_err(codec->dev, "Failed to read ID register\n");
  374. goto err_enable;
  375. }
  376. if (ret != wm8523_reg[WM8523_DEVICE_ID]) {
  377. dev_err(codec->dev, "Device is not a WM8523, ID is %x\n", ret);
  378. ret = -EINVAL;
  379. goto err_enable;
  380. }
  381. ret = snd_soc_read(codec, WM8523_REVISION);
  382. if (ret < 0) {
  383. dev_err(codec->dev, "Failed to read revision register\n");
  384. goto err_enable;
  385. }
  386. dev_info(codec->dev, "revision %c\n",
  387. (ret & WM8523_CHIP_REV_MASK) + 'A');
  388. ret = wm8523_reset(codec);
  389. if (ret < 0) {
  390. dev_err(codec->dev, "Failed to issue reset\n");
  391. goto err_enable;
  392. }
  393. /* Change some default settings - latch VU and enable ZC */
  394. snd_soc_update_bits(codec, WM8523_DAC_GAINR,
  395. WM8523_DACR_VU, WM8523_DACR_VU);
  396. snd_soc_update_bits(codec, WM8523_DAC_CTRL3, WM8523_ZC, WM8523_ZC);
  397. wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  398. /* Bias level configuration will have done an extra enable */
  399. regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
  400. return 0;
  401. err_enable:
  402. regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
  403. err_get:
  404. regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
  405. return ret;
  406. }
  407. static int wm8523_remove(struct snd_soc_codec *codec)
  408. {
  409. struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
  410. wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
  411. regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
  412. return 0;
  413. }
  414. static struct snd_soc_codec_driver soc_codec_dev_wm8523 = {
  415. .probe = wm8523_probe,
  416. .remove = wm8523_remove,
  417. .suspend = wm8523_suspend,
  418. .resume = wm8523_resume,
  419. .set_bias_level = wm8523_set_bias_level,
  420. .reg_cache_size = WM8523_REGISTER_COUNT,
  421. .reg_word_size = sizeof(u16),
  422. .reg_cache_default = wm8523_reg,
  423. .volatile_register = wm8523_volatile_register,
  424. .controls = wm8523_controls,
  425. .num_controls = ARRAY_SIZE(wm8523_controls),
  426. .dapm_widgets = wm8523_dapm_widgets,
  427. .num_dapm_widgets = ARRAY_SIZE(wm8523_dapm_widgets),
  428. .dapm_routes = wm8523_dapm_routes,
  429. .num_dapm_routes = ARRAY_SIZE(wm8523_dapm_routes),
  430. };
  431. static const struct of_device_id wm8523_of_match[] = {
  432. { .compatible = "wlf,wm8523" },
  433. { },
  434. };
  435. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  436. static __devinit int wm8523_i2c_probe(struct i2c_client *i2c,
  437. const struct i2c_device_id *id)
  438. {
  439. struct wm8523_priv *wm8523;
  440. int ret;
  441. wm8523 = kzalloc(sizeof(struct wm8523_priv), GFP_KERNEL);
  442. if (wm8523 == NULL)
  443. return -ENOMEM;
  444. i2c_set_clientdata(i2c, wm8523);
  445. wm8523->control_type = SND_SOC_I2C;
  446. ret = snd_soc_register_codec(&i2c->dev,
  447. &soc_codec_dev_wm8523, &wm8523_dai, 1);
  448. if (ret < 0)
  449. kfree(wm8523);
  450. return ret;
  451. }
  452. static __devexit int wm8523_i2c_remove(struct i2c_client *client)
  453. {
  454. snd_soc_unregister_codec(&client->dev);
  455. kfree(i2c_get_clientdata(client));
  456. return 0;
  457. }
  458. static const struct i2c_device_id wm8523_i2c_id[] = {
  459. { "wm8523", 0 },
  460. { }
  461. };
  462. MODULE_DEVICE_TABLE(i2c, wm8523_i2c_id);
  463. static struct i2c_driver wm8523_i2c_driver = {
  464. .driver = {
  465. .name = "wm8523",
  466. .owner = THIS_MODULE,
  467. .of_match_table = wm8523_of_match,
  468. },
  469. .probe = wm8523_i2c_probe,
  470. .remove = __devexit_p(wm8523_i2c_remove),
  471. .id_table = wm8523_i2c_id,
  472. };
  473. #endif
  474. static int __init wm8523_modinit(void)
  475. {
  476. int ret;
  477. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  478. ret = i2c_add_driver(&wm8523_i2c_driver);
  479. if (ret != 0) {
  480. printk(KERN_ERR "Failed to register WM8523 I2C driver: %d\n",
  481. ret);
  482. }
  483. #endif
  484. return 0;
  485. }
  486. module_init(wm8523_modinit);
  487. static void __exit wm8523_exit(void)
  488. {
  489. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  490. i2c_del_driver(&wm8523_i2c_driver);
  491. #endif
  492. }
  493. module_exit(wm8523_exit);
  494. MODULE_DESCRIPTION("ASoC WM8523 driver");
  495. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  496. MODULE_LICENSE("GPL");