wm8728.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
  42. static const struct snd_kcontrol_new wm8728_snd_controls[] = {
  43. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
  44. 0, 255, 0, wm8728_tlv),
  45. SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
  46. };
  47. /*
  48. * DAPM controls.
  49. */
  50. static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
  51. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  52. SND_SOC_DAPM_OUTPUT("VOUTL"),
  53. SND_SOC_DAPM_OUTPUT("VOUTR"),
  54. };
  55. static const struct snd_soc_dapm_route intercon[] = {
  56. {"VOUTL", NULL, "DAC"},
  57. {"VOUTR", NULL, "DAC"},
  58. };
  59. static int wm8728_add_widgets(struct snd_soc_codec *codec)
  60. {
  61. snd_soc_dapm_new_controls(codec, wm8728_dapm_widgets,
  62. ARRAY_SIZE(wm8728_dapm_widgets));
  63. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  64. return 0;
  65. }
  66. static int wm8728_mute(struct snd_soc_dai *dai, int mute)
  67. {
  68. struct snd_soc_codec *codec = dai->codec;
  69. u16 mute_reg = snd_soc_read(codec, WM8728_DACCTL);
  70. if (mute)
  71. snd_soc_write(codec, WM8728_DACCTL, mute_reg | 1);
  72. else
  73. snd_soc_write(codec, WM8728_DACCTL, mute_reg & ~1);
  74. return 0;
  75. }
  76. static int wm8728_hw_params(struct snd_pcm_substream *substream,
  77. struct snd_pcm_hw_params *params,
  78. struct snd_soc_dai *dai)
  79. {
  80. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  81. struct snd_soc_device *socdev = rtd->socdev;
  82. struct snd_soc_codec *codec = socdev->card->codec;
  83. u16 dac = snd_soc_read(codec, WM8728_DACCTL);
  84. dac &= ~0x18;
  85. switch (params_format(params)) {
  86. case SNDRV_PCM_FORMAT_S16_LE:
  87. break;
  88. case SNDRV_PCM_FORMAT_S20_3LE:
  89. dac |= 0x10;
  90. break;
  91. case SNDRV_PCM_FORMAT_S24_LE:
  92. dac |= 0x08;
  93. break;
  94. default:
  95. return -EINVAL;
  96. }
  97. snd_soc_write(codec, WM8728_DACCTL, dac);
  98. return 0;
  99. }
  100. static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
  101. unsigned int fmt)
  102. {
  103. struct snd_soc_codec *codec = codec_dai->codec;
  104. u16 iface = snd_soc_read(codec, WM8728_IFCTL);
  105. /* Currently only I2S is supported by the driver, though the
  106. * hardware is more flexible.
  107. */
  108. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  109. case SND_SOC_DAIFMT_I2S:
  110. iface |= 1;
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. /* The hardware only support full slave mode */
  116. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  117. case SND_SOC_DAIFMT_CBS_CFS:
  118. break;
  119. default:
  120. return -EINVAL;
  121. }
  122. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  123. case SND_SOC_DAIFMT_NB_NF:
  124. iface &= ~0x22;
  125. break;
  126. case SND_SOC_DAIFMT_IB_NF:
  127. iface |= 0x20;
  128. iface &= ~0x02;
  129. break;
  130. case SND_SOC_DAIFMT_NB_IF:
  131. iface |= 0x02;
  132. iface &= ~0x20;
  133. break;
  134. case SND_SOC_DAIFMT_IB_IF:
  135. iface |= 0x22;
  136. break;
  137. default:
  138. return -EINVAL;
  139. }
  140. snd_soc_write(codec, WM8728_IFCTL, iface);
  141. return 0;
  142. }
  143. static int wm8728_set_bias_level(struct snd_soc_codec *codec,
  144. enum snd_soc_bias_level level)
  145. {
  146. u16 reg;
  147. int i;
  148. switch (level) {
  149. case SND_SOC_BIAS_ON:
  150. case SND_SOC_BIAS_PREPARE:
  151. case SND_SOC_BIAS_STANDBY:
  152. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  153. /* Power everything up... */
  154. reg = snd_soc_read(codec, WM8728_DACCTL);
  155. snd_soc_write(codec, WM8728_DACCTL, reg & ~0x4);
  156. /* ..then sync in the register cache. */
  157. for (i = 0; i < ARRAY_SIZE(wm8728_reg_defaults); i++)
  158. snd_soc_write(codec, i,
  159. snd_soc_read(codec, i));
  160. }
  161. break;
  162. case SND_SOC_BIAS_OFF:
  163. reg = snd_soc_read(codec, WM8728_DACCTL);
  164. snd_soc_write(codec, WM8728_DACCTL, reg | 0x4);
  165. break;
  166. }
  167. codec->bias_level = level;
  168. return 0;
  169. }
  170. #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
  171. #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  172. SNDRV_PCM_FMTBIT_S24_LE)
  173. static struct snd_soc_dai_ops wm8728_dai_ops = {
  174. .hw_params = wm8728_hw_params,
  175. .digital_mute = wm8728_mute,
  176. .set_fmt = wm8728_set_dai_fmt,
  177. };
  178. struct snd_soc_dai wm8728_dai = {
  179. .name = "WM8728",
  180. .playback = {
  181. .stream_name = "Playback",
  182. .channels_min = 2,
  183. .channels_max = 2,
  184. .rates = WM8728_RATES,
  185. .formats = WM8728_FORMATS,
  186. },
  187. .ops = &wm8728_dai_ops,
  188. };
  189. EXPORT_SYMBOL_GPL(wm8728_dai);
  190. static int wm8728_suspend(struct platform_device *pdev, pm_message_t state)
  191. {
  192. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  193. struct snd_soc_codec *codec = socdev->card->codec;
  194. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  195. return 0;
  196. }
  197. static int wm8728_resume(struct platform_device *pdev)
  198. {
  199. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  200. struct snd_soc_codec *codec = socdev->card->codec;
  201. wm8728_set_bias_level(codec, codec->suspend_bias_level);
  202. return 0;
  203. }
  204. /*
  205. * initialise the WM8728 driver
  206. * register the mixer and dsp interfaces with the kernel
  207. */
  208. static int wm8728_init(struct snd_soc_device *socdev,
  209. enum snd_soc_control_type control)
  210. {
  211. struct snd_soc_codec *codec = socdev->card->codec;
  212. int ret = 0;
  213. codec->name = "WM8728";
  214. codec->owner = THIS_MODULE;
  215. codec->set_bias_level = wm8728_set_bias_level;
  216. codec->dai = &wm8728_dai;
  217. codec->num_dai = 1;
  218. codec->bias_level = SND_SOC_BIAS_OFF;
  219. codec->reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults);
  220. codec->reg_cache = kmemdup(wm8728_reg_defaults,
  221. sizeof(wm8728_reg_defaults),
  222. GFP_KERNEL);
  223. if (codec->reg_cache == NULL)
  224. return -ENOMEM;
  225. ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
  226. if (ret < 0) {
  227. printk(KERN_ERR "wm8728: failed to configure cache I/O: %d\n",
  228. ret);
  229. goto err;
  230. }
  231. /* register pcms */
  232. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  233. if (ret < 0) {
  234. printk(KERN_ERR "wm8728: failed to create pcms\n");
  235. goto err;
  236. }
  237. /* power on device */
  238. wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  239. snd_soc_add_controls(codec, wm8728_snd_controls,
  240. ARRAY_SIZE(wm8728_snd_controls));
  241. wm8728_add_widgets(codec);
  242. return ret;
  243. err:
  244. kfree(codec->reg_cache);
  245. return ret;
  246. }
  247. static struct snd_soc_device *wm8728_socdev;
  248. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  249. /*
  250. * WM8728 2 wire address is determined by GPIO5
  251. * state during powerup.
  252. * low = 0x1a
  253. * high = 0x1b
  254. */
  255. static int wm8728_i2c_probe(struct i2c_client *i2c,
  256. const struct i2c_device_id *id)
  257. {
  258. struct snd_soc_device *socdev = wm8728_socdev;
  259. struct snd_soc_codec *codec = socdev->card->codec;
  260. int ret;
  261. i2c_set_clientdata(i2c, codec);
  262. codec->control_data = i2c;
  263. ret = wm8728_init(socdev, SND_SOC_I2C);
  264. if (ret < 0)
  265. pr_err("failed to initialise WM8728\n");
  266. return ret;
  267. }
  268. static int wm8728_i2c_remove(struct i2c_client *client)
  269. {
  270. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  271. kfree(codec->reg_cache);
  272. return 0;
  273. }
  274. static const struct i2c_device_id wm8728_i2c_id[] = {
  275. { "wm8728", 0 },
  276. { }
  277. };
  278. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  279. static struct i2c_driver wm8728_i2c_driver = {
  280. .driver = {
  281. .name = "WM8728 I2C Codec",
  282. .owner = THIS_MODULE,
  283. },
  284. .probe = wm8728_i2c_probe,
  285. .remove = wm8728_i2c_remove,
  286. .id_table = wm8728_i2c_id,
  287. };
  288. static int wm8728_add_i2c_device(struct platform_device *pdev,
  289. const struct wm8728_setup_data *setup)
  290. {
  291. struct i2c_board_info info;
  292. struct i2c_adapter *adapter;
  293. struct i2c_client *client;
  294. int ret;
  295. ret = i2c_add_driver(&wm8728_i2c_driver);
  296. if (ret != 0) {
  297. dev_err(&pdev->dev, "can't add i2c driver\n");
  298. return ret;
  299. }
  300. memset(&info, 0, sizeof(struct i2c_board_info));
  301. info.addr = setup->i2c_address;
  302. strlcpy(info.type, "wm8728", I2C_NAME_SIZE);
  303. adapter = i2c_get_adapter(setup->i2c_bus);
  304. if (!adapter) {
  305. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  306. setup->i2c_bus);
  307. goto err_driver;
  308. }
  309. client = i2c_new_device(adapter, &info);
  310. i2c_put_adapter(adapter);
  311. if (!client) {
  312. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  313. (unsigned int)info.addr);
  314. goto err_driver;
  315. }
  316. return 0;
  317. err_driver:
  318. i2c_del_driver(&wm8728_i2c_driver);
  319. return -ENODEV;
  320. }
  321. #endif
  322. #if defined(CONFIG_SPI_MASTER)
  323. static int __devinit wm8728_spi_probe(struct spi_device *spi)
  324. {
  325. struct snd_soc_device *socdev = wm8728_socdev;
  326. struct snd_soc_codec *codec = socdev->card->codec;
  327. int ret;
  328. codec->control_data = spi;
  329. ret = wm8728_init(socdev, SND_SOC_SPI);
  330. if (ret < 0)
  331. dev_err(&spi->dev, "failed to initialise WM8728\n");
  332. return ret;
  333. }
  334. static int __devexit wm8728_spi_remove(struct spi_device *spi)
  335. {
  336. return 0;
  337. }
  338. static struct spi_driver wm8728_spi_driver = {
  339. .driver = {
  340. .name = "wm8728",
  341. .bus = &spi_bus_type,
  342. .owner = THIS_MODULE,
  343. },
  344. .probe = wm8728_spi_probe,
  345. .remove = __devexit_p(wm8728_spi_remove),
  346. };
  347. #endif /* CONFIG_SPI_MASTER */
  348. static int wm8728_probe(struct platform_device *pdev)
  349. {
  350. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  351. struct wm8728_setup_data *setup;
  352. struct snd_soc_codec *codec;
  353. int ret = 0;
  354. setup = socdev->codec_data;
  355. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  356. if (codec == NULL)
  357. return -ENOMEM;
  358. socdev->card->codec = codec;
  359. mutex_init(&codec->mutex);
  360. INIT_LIST_HEAD(&codec->dapm_widgets);
  361. INIT_LIST_HEAD(&codec->dapm_paths);
  362. wm8728_socdev = socdev;
  363. ret = -ENODEV;
  364. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  365. if (setup->i2c_address) {
  366. ret = wm8728_add_i2c_device(pdev, setup);
  367. }
  368. #endif
  369. #if defined(CONFIG_SPI_MASTER)
  370. if (setup->spi) {
  371. ret = spi_register_driver(&wm8728_spi_driver);
  372. if (ret != 0)
  373. printk(KERN_ERR "can't add spi driver");
  374. }
  375. #endif
  376. if (ret != 0)
  377. kfree(codec);
  378. return ret;
  379. }
  380. /* power down chip */
  381. static int wm8728_remove(struct platform_device *pdev)
  382. {
  383. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  384. struct snd_soc_codec *codec = socdev->card->codec;
  385. if (codec->control_data)
  386. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  387. snd_soc_free_pcms(socdev);
  388. snd_soc_dapm_free(socdev);
  389. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  390. i2c_unregister_device(codec->control_data);
  391. i2c_del_driver(&wm8728_i2c_driver);
  392. #endif
  393. #if defined(CONFIG_SPI_MASTER)
  394. spi_unregister_driver(&wm8728_spi_driver);
  395. #endif
  396. kfree(codec);
  397. return 0;
  398. }
  399. struct snd_soc_codec_device soc_codec_dev_wm8728 = {
  400. .probe = wm8728_probe,
  401. .remove = wm8728_remove,
  402. .suspend = wm8728_suspend,
  403. .resume = wm8728_resume,
  404. };
  405. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8728);
  406. static int __init wm8728_modinit(void)
  407. {
  408. return snd_soc_register_dai(&wm8728_dai);
  409. }
  410. module_init(wm8728_modinit);
  411. static void __exit wm8728_exit(void)
  412. {
  413. snd_soc_unregister_dai(&wm8728_dai);
  414. }
  415. module_exit(wm8728_exit);
  416. MODULE_DESCRIPTION("ASoC WM8728 driver");
  417. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  418. MODULE_LICENSE("GPL");