wm8728.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. snd_soc_dapm_new_widgets(codec);
  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. ret = snd_soc_init_card(socdev);
  244. if (ret < 0) {
  245. printk(KERN_ERR "wm8728: failed to register card\n");
  246. goto card_err;
  247. }
  248. return ret;
  249. card_err:
  250. snd_soc_free_pcms(socdev);
  251. snd_soc_dapm_free(socdev);
  252. err:
  253. kfree(codec->reg_cache);
  254. return ret;
  255. }
  256. static struct snd_soc_device *wm8728_socdev;
  257. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  258. /*
  259. * WM8728 2 wire address is determined by GPIO5
  260. * state during powerup.
  261. * low = 0x1a
  262. * high = 0x1b
  263. */
  264. static int wm8728_i2c_probe(struct i2c_client *i2c,
  265. const struct i2c_device_id *id)
  266. {
  267. struct snd_soc_device *socdev = wm8728_socdev;
  268. struct snd_soc_codec *codec = socdev->card->codec;
  269. int ret;
  270. i2c_set_clientdata(i2c, codec);
  271. codec->control_data = i2c;
  272. ret = wm8728_init(socdev, SND_SOC_I2C);
  273. if (ret < 0)
  274. pr_err("failed to initialise WM8728\n");
  275. return ret;
  276. }
  277. static int wm8728_i2c_remove(struct i2c_client *client)
  278. {
  279. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  280. kfree(codec->reg_cache);
  281. return 0;
  282. }
  283. static const struct i2c_device_id wm8728_i2c_id[] = {
  284. { "wm8728", 0 },
  285. { }
  286. };
  287. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  288. static struct i2c_driver wm8728_i2c_driver = {
  289. .driver = {
  290. .name = "WM8728 I2C Codec",
  291. .owner = THIS_MODULE,
  292. },
  293. .probe = wm8728_i2c_probe,
  294. .remove = wm8728_i2c_remove,
  295. .id_table = wm8728_i2c_id,
  296. };
  297. static int wm8728_add_i2c_device(struct platform_device *pdev,
  298. const struct wm8728_setup_data *setup)
  299. {
  300. struct i2c_board_info info;
  301. struct i2c_adapter *adapter;
  302. struct i2c_client *client;
  303. int ret;
  304. ret = i2c_add_driver(&wm8728_i2c_driver);
  305. if (ret != 0) {
  306. dev_err(&pdev->dev, "can't add i2c driver\n");
  307. return ret;
  308. }
  309. memset(&info, 0, sizeof(struct i2c_board_info));
  310. info.addr = setup->i2c_address;
  311. strlcpy(info.type, "wm8728", I2C_NAME_SIZE);
  312. adapter = i2c_get_adapter(setup->i2c_bus);
  313. if (!adapter) {
  314. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  315. setup->i2c_bus);
  316. goto err_driver;
  317. }
  318. client = i2c_new_device(adapter, &info);
  319. i2c_put_adapter(adapter);
  320. if (!client) {
  321. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  322. (unsigned int)info.addr);
  323. goto err_driver;
  324. }
  325. return 0;
  326. err_driver:
  327. i2c_del_driver(&wm8728_i2c_driver);
  328. return -ENODEV;
  329. }
  330. #endif
  331. #if defined(CONFIG_SPI_MASTER)
  332. static int __devinit wm8728_spi_probe(struct spi_device *spi)
  333. {
  334. struct snd_soc_device *socdev = wm8728_socdev;
  335. struct snd_soc_codec *codec = socdev->card->codec;
  336. int ret;
  337. codec->control_data = spi;
  338. ret = wm8728_init(socdev, SND_SOC_SPI);
  339. if (ret < 0)
  340. dev_err(&spi->dev, "failed to initialise WM8728\n");
  341. return ret;
  342. }
  343. static int __devexit wm8728_spi_remove(struct spi_device *spi)
  344. {
  345. return 0;
  346. }
  347. static struct spi_driver wm8728_spi_driver = {
  348. .driver = {
  349. .name = "wm8728",
  350. .bus = &spi_bus_type,
  351. .owner = THIS_MODULE,
  352. },
  353. .probe = wm8728_spi_probe,
  354. .remove = __devexit_p(wm8728_spi_remove),
  355. };
  356. #endif /* CONFIG_SPI_MASTER */
  357. static int wm8728_probe(struct platform_device *pdev)
  358. {
  359. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  360. struct wm8728_setup_data *setup;
  361. struct snd_soc_codec *codec;
  362. int ret = 0;
  363. setup = socdev->codec_data;
  364. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  365. if (codec == NULL)
  366. return -ENOMEM;
  367. socdev->card->codec = codec;
  368. mutex_init(&codec->mutex);
  369. INIT_LIST_HEAD(&codec->dapm_widgets);
  370. INIT_LIST_HEAD(&codec->dapm_paths);
  371. wm8728_socdev = socdev;
  372. ret = -ENODEV;
  373. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  374. if (setup->i2c_address) {
  375. ret = wm8728_add_i2c_device(pdev, setup);
  376. }
  377. #endif
  378. #if defined(CONFIG_SPI_MASTER)
  379. if (setup->spi) {
  380. ret = spi_register_driver(&wm8728_spi_driver);
  381. if (ret != 0)
  382. printk(KERN_ERR "can't add spi driver");
  383. }
  384. #endif
  385. if (ret != 0)
  386. kfree(codec);
  387. return ret;
  388. }
  389. /* power down chip */
  390. static int wm8728_remove(struct platform_device *pdev)
  391. {
  392. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  393. struct snd_soc_codec *codec = socdev->card->codec;
  394. if (codec->control_data)
  395. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  396. snd_soc_free_pcms(socdev);
  397. snd_soc_dapm_free(socdev);
  398. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  399. i2c_unregister_device(codec->control_data);
  400. i2c_del_driver(&wm8728_i2c_driver);
  401. #endif
  402. #if defined(CONFIG_SPI_MASTER)
  403. spi_unregister_driver(&wm8728_spi_driver);
  404. #endif
  405. kfree(codec);
  406. return 0;
  407. }
  408. struct snd_soc_codec_device soc_codec_dev_wm8728 = {
  409. .probe = wm8728_probe,
  410. .remove = wm8728_remove,
  411. .suspend = wm8728_suspend,
  412. .resume = wm8728_resume,
  413. };
  414. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8728);
  415. static int __init wm8728_modinit(void)
  416. {
  417. return snd_soc_register_dai(&wm8728_dai);
  418. }
  419. module_init(wm8728_modinit);
  420. static void __exit wm8728_exit(void)
  421. {
  422. snd_soc_unregister_dai(&wm8728_dai);
  423. }
  424. module_exit(wm8728_exit);
  425. MODULE_DESCRIPTION("ASoC WM8728 driver");
  426. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  427. MODULE_LICENSE("GPL");