wm8523.c 14 KB

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