wm8728.c 11 KB

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