wm8728.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * wm8728.c -- WM8728 ALSA SoC Audio driver
  3. *
  4. * Copyright 2008 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/i2c.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spi/spi.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.h>
  25. #include <sound/initval.h>
  26. #include <sound/tlv.h>
  27. #include "wm8728.h"
  28. struct snd_soc_codec_device soc_codec_dev_wm8728;
  29. /*
  30. * We can't read the WM8728 register space so we cache them instead.
  31. * Note that the defaults here aren't the physical defaults, we latch
  32. * the volume update bits, mute the output and enable infinite zero
  33. * detect.
  34. */
  35. static const u16 wm8728_reg_defaults[] = {
  36. 0x1ff,
  37. 0x1ff,
  38. 0x001,
  39. 0x100,
  40. };
  41. static inline unsigned int wm8728_read_reg_cache(struct snd_soc_codec *codec,
  42. unsigned int reg)
  43. {
  44. u16 *cache = codec->reg_cache;
  45. BUG_ON(reg > ARRAY_SIZE(wm8728_reg_defaults));
  46. return cache[reg];
  47. }
  48. static inline void wm8728_write_reg_cache(struct snd_soc_codec *codec,
  49. u16 reg, unsigned int value)
  50. {
  51. u16 *cache = codec->reg_cache;
  52. BUG_ON(reg > ARRAY_SIZE(wm8728_reg_defaults));
  53. cache[reg] = value;
  54. }
  55. /*
  56. * write to the WM8728 register space
  57. */
  58. static int wm8728_write(struct snd_soc_codec *codec, unsigned int reg,
  59. unsigned int value)
  60. {
  61. u8 data[2];
  62. /* data is
  63. * D15..D9 WM8728 register offset
  64. * D8...D0 register data
  65. */
  66. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  67. data[1] = value & 0x00ff;
  68. wm8728_write_reg_cache(codec, reg, value);
  69. if (codec->hw_write(codec->control_data, data, 2) == 2)
  70. return 0;
  71. else
  72. return -EIO;
  73. }
  74. static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
  75. static const struct snd_kcontrol_new wm8728_snd_controls[] = {
  76. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
  77. 0, 255, 0, wm8728_tlv),
  78. SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
  79. };
  80. static int wm8728_add_controls(struct snd_soc_codec *codec)
  81. {
  82. int err, i;
  83. for (i = 0; i < ARRAY_SIZE(wm8728_snd_controls); i++) {
  84. err = snd_ctl_add(codec->card,
  85. snd_soc_cnew(&wm8728_snd_controls[i],
  86. codec, NULL));
  87. if (err < 0)
  88. return err;
  89. }
  90. return 0;
  91. }
  92. /*
  93. * DAPM controls.
  94. */
  95. static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
  96. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  97. SND_SOC_DAPM_OUTPUT("VOUTL"),
  98. SND_SOC_DAPM_OUTPUT("VOUTR"),
  99. };
  100. static const struct snd_soc_dapm_route intercon[] = {
  101. {"VOUTL", NULL, "DAC"},
  102. {"VOUTR", NULL, "DAC"},
  103. };
  104. static int wm8728_add_widgets(struct snd_soc_codec *codec)
  105. {
  106. snd_soc_dapm_new_controls(codec, wm8728_dapm_widgets,
  107. ARRAY_SIZE(wm8728_dapm_widgets));
  108. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  109. snd_soc_dapm_new_widgets(codec);
  110. return 0;
  111. }
  112. static int wm8728_mute(struct snd_soc_dai *dai, int mute)
  113. {
  114. struct snd_soc_codec *codec = dai->codec;
  115. u16 mute_reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  116. if (mute)
  117. wm8728_write(codec, WM8728_DACCTL, mute_reg | 1);
  118. else
  119. wm8728_write(codec, WM8728_DACCTL, mute_reg & ~1);
  120. return 0;
  121. }
  122. static int wm8728_hw_params(struct snd_pcm_substream *substream,
  123. struct snd_pcm_hw_params *params,
  124. struct snd_soc_dai *dai)
  125. {
  126. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  127. struct snd_soc_device *socdev = rtd->socdev;
  128. struct snd_soc_codec *codec = socdev->codec;
  129. u16 dac = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  130. dac &= ~0x18;
  131. switch (params_format(params)) {
  132. case SNDRV_PCM_FORMAT_S16_LE:
  133. break;
  134. case SNDRV_PCM_FORMAT_S20_3LE:
  135. dac |= 0x10;
  136. break;
  137. case SNDRV_PCM_FORMAT_S24_LE:
  138. dac |= 0x08;
  139. break;
  140. default:
  141. return -EINVAL;
  142. }
  143. wm8728_write(codec, WM8728_DACCTL, dac);
  144. return 0;
  145. }
  146. static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
  147. unsigned int fmt)
  148. {
  149. struct snd_soc_codec *codec = codec_dai->codec;
  150. u16 iface = wm8728_read_reg_cache(codec, WM8728_IFCTL);
  151. /* Currently only I2S is supported by the driver, though the
  152. * hardware is more flexible.
  153. */
  154. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  155. case SND_SOC_DAIFMT_I2S:
  156. iface |= 1;
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. /* The hardware only support full slave mode */
  162. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  163. case SND_SOC_DAIFMT_CBS_CFS:
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  169. case SND_SOC_DAIFMT_NB_NF:
  170. iface &= ~0x22;
  171. break;
  172. case SND_SOC_DAIFMT_IB_NF:
  173. iface |= 0x20;
  174. iface &= ~0x02;
  175. break;
  176. case SND_SOC_DAIFMT_NB_IF:
  177. iface |= 0x02;
  178. iface &= ~0x20;
  179. break;
  180. case SND_SOC_DAIFMT_IB_IF:
  181. iface |= 0x22;
  182. break;
  183. default:
  184. return -EINVAL;
  185. }
  186. wm8728_write(codec, WM8728_IFCTL, iface);
  187. return 0;
  188. }
  189. static int wm8728_set_bias_level(struct snd_soc_codec *codec,
  190. enum snd_soc_bias_level level)
  191. {
  192. u16 reg;
  193. int i;
  194. switch (level) {
  195. case SND_SOC_BIAS_ON:
  196. case SND_SOC_BIAS_PREPARE:
  197. case SND_SOC_BIAS_STANDBY:
  198. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  199. /* Power everything up... */
  200. reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  201. wm8728_write(codec, WM8728_DACCTL, reg & ~0x4);
  202. /* ..then sync in the register cache. */
  203. for (i = 0; i < ARRAY_SIZE(wm8728_reg_defaults); i++)
  204. wm8728_write(codec, i,
  205. wm8728_read_reg_cache(codec, i));
  206. }
  207. break;
  208. case SND_SOC_BIAS_OFF:
  209. reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  210. wm8728_write(codec, WM8728_DACCTL, reg | 0x4);
  211. break;
  212. }
  213. codec->bias_level = level;
  214. return 0;
  215. }
  216. #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
  217. #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  218. SNDRV_PCM_FMTBIT_S24_LE)
  219. struct snd_soc_dai wm8728_dai = {
  220. .name = "WM8728",
  221. .playback = {
  222. .stream_name = "Playback",
  223. .channels_min = 2,
  224. .channels_max = 2,
  225. .rates = WM8728_RATES,
  226. .formats = WM8728_FORMATS,
  227. },
  228. .ops = {
  229. .hw_params = wm8728_hw_params,
  230. .digital_mute = wm8728_mute,
  231. .set_fmt = wm8728_set_dai_fmt,
  232. }
  233. };
  234. EXPORT_SYMBOL_GPL(wm8728_dai);
  235. static int wm8728_suspend(struct platform_device *pdev, pm_message_t state)
  236. {
  237. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  238. struct snd_soc_codec *codec = socdev->codec;
  239. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  240. return 0;
  241. }
  242. static int wm8728_resume(struct platform_device *pdev)
  243. {
  244. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  245. struct snd_soc_codec *codec = socdev->codec;
  246. wm8728_set_bias_level(codec, codec->suspend_bias_level);
  247. return 0;
  248. }
  249. /*
  250. * initialise the WM8728 driver
  251. * register the mixer and dsp interfaces with the kernel
  252. */
  253. static int wm8728_init(struct snd_soc_device *socdev)
  254. {
  255. struct snd_soc_codec *codec = socdev->codec;
  256. int ret = 0;
  257. codec->name = "WM8728";
  258. codec->owner = THIS_MODULE;
  259. codec->read = wm8728_read_reg_cache;
  260. codec->write = wm8728_write;
  261. codec->set_bias_level = wm8728_set_bias_level;
  262. codec->dai = &wm8728_dai;
  263. codec->num_dai = 1;
  264. codec->bias_level = SND_SOC_BIAS_OFF;
  265. codec->reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults);
  266. codec->reg_cache = kmemdup(wm8728_reg_defaults,
  267. sizeof(wm8728_reg_defaults),
  268. GFP_KERNEL);
  269. if (codec->reg_cache == NULL)
  270. return -ENOMEM;
  271. /* register pcms */
  272. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  273. if (ret < 0) {
  274. printk(KERN_ERR "wm8728: failed to create pcms\n");
  275. goto pcm_err;
  276. }
  277. /* power on device */
  278. wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  279. wm8728_add_controls(codec);
  280. wm8728_add_widgets(codec);
  281. ret = snd_soc_init_card(socdev);
  282. if (ret < 0) {
  283. printk(KERN_ERR "wm8728: failed to register card\n");
  284. goto card_err;
  285. }
  286. return ret;
  287. card_err:
  288. snd_soc_free_pcms(socdev);
  289. snd_soc_dapm_free(socdev);
  290. pcm_err:
  291. kfree(codec->reg_cache);
  292. return ret;
  293. }
  294. static struct snd_soc_device *wm8728_socdev;
  295. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  296. /*
  297. * WM8728 2 wire address is determined by GPIO5
  298. * state during powerup.
  299. * low = 0x1a
  300. * high = 0x1b
  301. */
  302. static int wm8728_i2c_probe(struct i2c_client *i2c,
  303. const struct i2c_device_id *id)
  304. {
  305. struct snd_soc_device *socdev = wm8728_socdev;
  306. struct snd_soc_codec *codec = socdev->codec;
  307. int ret;
  308. i2c_set_clientdata(i2c, codec);
  309. codec->control_data = i2c;
  310. ret = wm8728_init(socdev);
  311. if (ret < 0)
  312. pr_err("failed to initialise WM8728\n");
  313. return ret;
  314. }
  315. static int wm8728_i2c_remove(struct i2c_client *client)
  316. {
  317. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  318. kfree(codec->reg_cache);
  319. return 0;
  320. }
  321. static const struct i2c_device_id wm8728_i2c_id[] = {
  322. { "wm8728", 0 },
  323. { }
  324. };
  325. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  326. static struct i2c_driver wm8728_i2c_driver = {
  327. .driver = {
  328. .name = "WM8728 I2C Codec",
  329. .owner = THIS_MODULE,
  330. },
  331. .probe = wm8728_i2c_probe,
  332. .remove = wm8728_i2c_remove,
  333. .id_table = wm8728_i2c_id,
  334. };
  335. static int wm8728_add_i2c_device(struct platform_device *pdev,
  336. const struct wm8728_setup_data *setup)
  337. {
  338. struct i2c_board_info info;
  339. struct i2c_adapter *adapter;
  340. struct i2c_client *client;
  341. int ret;
  342. ret = i2c_add_driver(&wm8728_i2c_driver);
  343. if (ret != 0) {
  344. dev_err(&pdev->dev, "can't add i2c driver\n");
  345. return ret;
  346. }
  347. memset(&info, 0, sizeof(struct i2c_board_info));
  348. info.addr = setup->i2c_address;
  349. strlcpy(info.type, "wm8728", I2C_NAME_SIZE);
  350. adapter = i2c_get_adapter(setup->i2c_bus);
  351. if (!adapter) {
  352. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  353. setup->i2c_bus);
  354. goto err_driver;
  355. }
  356. client = i2c_new_device(adapter, &info);
  357. i2c_put_adapter(adapter);
  358. if (!client) {
  359. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  360. (unsigned int)info.addr);
  361. goto err_driver;
  362. }
  363. return 0;
  364. err_driver:
  365. i2c_del_driver(&wm8728_i2c_driver);
  366. return -ENODEV;
  367. }
  368. #endif
  369. #if defined(CONFIG_SPI_MASTER)
  370. static int __devinit wm8728_spi_probe(struct spi_device *spi)
  371. {
  372. struct snd_soc_device *socdev = wm8728_socdev;
  373. struct snd_soc_codec *codec = socdev->codec;
  374. int ret;
  375. codec->control_data = spi;
  376. ret = wm8728_init(socdev);
  377. if (ret < 0)
  378. dev_err(&spi->dev, "failed to initialise WM8728\n");
  379. return ret;
  380. }
  381. static int __devexit wm8728_spi_remove(struct spi_device *spi)
  382. {
  383. return 0;
  384. }
  385. static struct spi_driver wm8728_spi_driver = {
  386. .driver = {
  387. .name = "wm8728",
  388. .bus = &spi_bus_type,
  389. .owner = THIS_MODULE,
  390. },
  391. .probe = wm8728_spi_probe,
  392. .remove = __devexit_p(wm8728_spi_remove),
  393. };
  394. static int wm8728_spi_write(struct spi_device *spi, const char *data, int len)
  395. {
  396. struct spi_transfer t;
  397. struct spi_message m;
  398. u8 msg[2];
  399. if (len <= 0)
  400. return 0;
  401. msg[0] = data[0];
  402. msg[1] = data[1];
  403. spi_message_init(&m);
  404. memset(&t, 0, (sizeof t));
  405. t.tx_buf = &msg[0];
  406. t.len = len;
  407. spi_message_add_tail(&t, &m);
  408. spi_sync(spi, &m);
  409. return len;
  410. }
  411. #endif /* CONFIG_SPI_MASTER */
  412. static int wm8728_probe(struct platform_device *pdev)
  413. {
  414. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  415. struct wm8728_setup_data *setup;
  416. struct snd_soc_codec *codec;
  417. int ret = 0;
  418. setup = socdev->codec_data;
  419. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  420. if (codec == NULL)
  421. return -ENOMEM;
  422. socdev->codec = codec;
  423. mutex_init(&codec->mutex);
  424. INIT_LIST_HEAD(&codec->dapm_widgets);
  425. INIT_LIST_HEAD(&codec->dapm_paths);
  426. wm8728_socdev = socdev;
  427. ret = -ENODEV;
  428. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  429. if (setup->i2c_address) {
  430. codec->hw_write = (hw_write_t)i2c_master_send;
  431. ret = wm8728_add_i2c_device(pdev, setup);
  432. }
  433. #endif
  434. #if defined(CONFIG_SPI_MASTER)
  435. if (setup->spi) {
  436. codec->hw_write = (hw_write_t)wm8728_spi_write;
  437. ret = spi_register_driver(&wm8728_spi_driver);
  438. if (ret != 0)
  439. printk(KERN_ERR "can't add spi driver");
  440. }
  441. #endif
  442. if (ret != 0)
  443. kfree(codec);
  444. return ret;
  445. }
  446. /* power down chip */
  447. static int wm8728_remove(struct platform_device *pdev)
  448. {
  449. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  450. struct snd_soc_codec *codec = socdev->codec;
  451. if (codec->control_data)
  452. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  453. snd_soc_free_pcms(socdev);
  454. snd_soc_dapm_free(socdev);
  455. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  456. i2c_unregister_device(codec->control_data);
  457. i2c_del_driver(&wm8728_i2c_driver);
  458. #endif
  459. #if defined(CONFIG_SPI_MASTER)
  460. spi_unregister_driver(&wm8728_spi_driver);
  461. #endif
  462. kfree(codec);
  463. return 0;
  464. }
  465. struct snd_soc_codec_device soc_codec_dev_wm8728 = {
  466. .probe = wm8728_probe,
  467. .remove = wm8728_remove,
  468. .suspend = wm8728_suspend,
  469. .resume = wm8728_resume,
  470. };
  471. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8728);
  472. static int __init wm8728_modinit(void)
  473. {
  474. return snd_soc_register_dai(&wm8728_dai);
  475. }
  476. module_init(wm8728_modinit);
  477. static void __exit wm8728_exit(void)
  478. {
  479. snd_soc_unregister_dai(&wm8728_dai);
  480. }
  481. module_exit(wm8728_exit);
  482. MODULE_DESCRIPTION("ASoC WM8728 driver");
  483. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  484. MODULE_LICENSE("GPL");