wm8728.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /*
  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. /* codec private data */
  42. struct wm8728_priv {
  43. enum snd_soc_control_type control_type;
  44. };
  45. static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
  46. static const struct snd_kcontrol_new wm8728_snd_controls[] = {
  47. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
  48. 0, 255, 0, wm8728_tlv),
  49. SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
  50. };
  51. /*
  52. * DAPM controls.
  53. */
  54. static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
  55. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  56. SND_SOC_DAPM_OUTPUT("VOUTL"),
  57. SND_SOC_DAPM_OUTPUT("VOUTR"),
  58. };
  59. static const struct snd_soc_dapm_route intercon[] = {
  60. {"VOUTL", NULL, "DAC"},
  61. {"VOUTR", NULL, "DAC"},
  62. };
  63. static int wm8728_add_widgets(struct snd_soc_codec *codec)
  64. {
  65. struct snd_soc_dapm_context *dapm = &codec->dapm;
  66. snd_soc_dapm_new_controls(dapm, wm8728_dapm_widgets,
  67. ARRAY_SIZE(wm8728_dapm_widgets));
  68. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  69. return 0;
  70. }
  71. static int wm8728_mute(struct snd_soc_dai *dai, int mute)
  72. {
  73. struct snd_soc_codec *codec = dai->codec;
  74. u16 mute_reg = snd_soc_read(codec, WM8728_DACCTL);
  75. if (mute)
  76. snd_soc_write(codec, WM8728_DACCTL, mute_reg | 1);
  77. else
  78. snd_soc_write(codec, WM8728_DACCTL, mute_reg & ~1);
  79. return 0;
  80. }
  81. static int wm8728_hw_params(struct snd_pcm_substream *substream,
  82. struct snd_pcm_hw_params *params,
  83. struct snd_soc_dai *dai)
  84. {
  85. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  86. struct snd_soc_codec *codec = rtd->codec;
  87. u16 dac = snd_soc_read(codec, WM8728_DACCTL);
  88. dac &= ~0x18;
  89. switch (params_format(params)) {
  90. case SNDRV_PCM_FORMAT_S16_LE:
  91. break;
  92. case SNDRV_PCM_FORMAT_S20_3LE:
  93. dac |= 0x10;
  94. break;
  95. case SNDRV_PCM_FORMAT_S24_LE:
  96. dac |= 0x08;
  97. break;
  98. default:
  99. return -EINVAL;
  100. }
  101. snd_soc_write(codec, WM8728_DACCTL, dac);
  102. return 0;
  103. }
  104. static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
  105. unsigned int fmt)
  106. {
  107. struct snd_soc_codec *codec = codec_dai->codec;
  108. u16 iface = snd_soc_read(codec, WM8728_IFCTL);
  109. /* Currently only I2S is supported by the driver, though the
  110. * hardware is more flexible.
  111. */
  112. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  113. case SND_SOC_DAIFMT_I2S:
  114. iface |= 1;
  115. break;
  116. default:
  117. return -EINVAL;
  118. }
  119. /* The hardware only support full slave mode */
  120. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  121. case SND_SOC_DAIFMT_CBS_CFS:
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  127. case SND_SOC_DAIFMT_NB_NF:
  128. iface &= ~0x22;
  129. break;
  130. case SND_SOC_DAIFMT_IB_NF:
  131. iface |= 0x20;
  132. iface &= ~0x02;
  133. break;
  134. case SND_SOC_DAIFMT_NB_IF:
  135. iface |= 0x02;
  136. iface &= ~0x20;
  137. break;
  138. case SND_SOC_DAIFMT_IB_IF:
  139. iface |= 0x22;
  140. break;
  141. default:
  142. return -EINVAL;
  143. }
  144. snd_soc_write(codec, WM8728_IFCTL, iface);
  145. return 0;
  146. }
  147. static int wm8728_set_bias_level(struct snd_soc_codec *codec,
  148. enum snd_soc_bias_level level)
  149. {
  150. u16 reg;
  151. int i;
  152. switch (level) {
  153. case SND_SOC_BIAS_ON:
  154. case SND_SOC_BIAS_PREPARE:
  155. case SND_SOC_BIAS_STANDBY:
  156. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  157. /* Power everything up... */
  158. reg = snd_soc_read(codec, WM8728_DACCTL);
  159. snd_soc_write(codec, WM8728_DACCTL, reg & ~0x4);
  160. /* ..then sync in the register cache. */
  161. for (i = 0; i < ARRAY_SIZE(wm8728_reg_defaults); i++)
  162. snd_soc_write(codec, i,
  163. snd_soc_read(codec, i));
  164. }
  165. break;
  166. case SND_SOC_BIAS_OFF:
  167. reg = snd_soc_read(codec, WM8728_DACCTL);
  168. snd_soc_write(codec, WM8728_DACCTL, reg | 0x4);
  169. break;
  170. }
  171. codec->dapm.bias_level = level;
  172. return 0;
  173. }
  174. #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
  175. #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  176. SNDRV_PCM_FMTBIT_S24_LE)
  177. static struct snd_soc_dai_ops wm8728_dai_ops = {
  178. .hw_params = wm8728_hw_params,
  179. .digital_mute = wm8728_mute,
  180. .set_fmt = wm8728_set_dai_fmt,
  181. };
  182. static struct snd_soc_dai_driver wm8728_dai = {
  183. .name = "wm8728-hifi",
  184. .playback = {
  185. .stream_name = "Playback",
  186. .channels_min = 2,
  187. .channels_max = 2,
  188. .rates = WM8728_RATES,
  189. .formats = WM8728_FORMATS,
  190. },
  191. .ops = &wm8728_dai_ops,
  192. };
  193. static int wm8728_suspend(struct snd_soc_codec *codec, pm_message_t state)
  194. {
  195. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  196. return 0;
  197. }
  198. static int wm8728_resume(struct snd_soc_codec *codec)
  199. {
  200. wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  201. return 0;
  202. }
  203. static int wm8728_probe(struct snd_soc_codec *codec)
  204. {
  205. struct wm8728_priv *wm8728 = snd_soc_codec_get_drvdata(codec);
  206. int ret;
  207. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8728->control_type);
  208. if (ret < 0) {
  209. printk(KERN_ERR "wm8728: failed to configure cache I/O: %d\n",
  210. ret);
  211. return ret;
  212. }
  213. /* power on device */
  214. wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  215. snd_soc_add_controls(codec, wm8728_snd_controls,
  216. ARRAY_SIZE(wm8728_snd_controls));
  217. wm8728_add_widgets(codec);
  218. return ret;
  219. }
  220. static int wm8728_remove(struct snd_soc_codec *codec)
  221. {
  222. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  223. return 0;
  224. }
  225. static struct snd_soc_codec_driver soc_codec_dev_wm8728 = {
  226. .probe = wm8728_probe,
  227. .remove = wm8728_remove,
  228. .suspend = wm8728_suspend,
  229. .resume = wm8728_resume,
  230. .set_bias_level = wm8728_set_bias_level,
  231. .reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults),
  232. .reg_word_size = sizeof(u16),
  233. .reg_cache_default = wm8728_reg_defaults,
  234. };
  235. #if defined(CONFIG_SPI_MASTER)
  236. static int __devinit wm8728_spi_probe(struct spi_device *spi)
  237. {
  238. struct wm8728_priv *wm8728;
  239. int ret;
  240. wm8728 = kzalloc(sizeof(struct wm8728_priv), GFP_KERNEL);
  241. if (wm8728 == NULL)
  242. return -ENOMEM;
  243. wm8728->control_type = SND_SOC_SPI;
  244. spi_set_drvdata(spi, wm8728);
  245. ret = snd_soc_register_codec(&spi->dev,
  246. &soc_codec_dev_wm8728, &wm8728_dai, 1);
  247. if (ret < 0)
  248. kfree(wm8728);
  249. return ret;
  250. }
  251. static int __devexit wm8728_spi_remove(struct spi_device *spi)
  252. {
  253. snd_soc_unregister_codec(&spi->dev);
  254. kfree(spi_get_drvdata(spi));
  255. return 0;
  256. }
  257. static struct spi_driver wm8728_spi_driver = {
  258. .driver = {
  259. .name = "wm8728-codec",
  260. .owner = THIS_MODULE,
  261. },
  262. .probe = wm8728_spi_probe,
  263. .remove = __devexit_p(wm8728_spi_remove),
  264. };
  265. #endif /* CONFIG_SPI_MASTER */
  266. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  267. static __devinit int wm8728_i2c_probe(struct i2c_client *i2c,
  268. const struct i2c_device_id *id)
  269. {
  270. struct wm8728_priv *wm8728;
  271. int ret;
  272. wm8728 = kzalloc(sizeof(struct wm8728_priv), GFP_KERNEL);
  273. if (wm8728 == NULL)
  274. return -ENOMEM;
  275. i2c_set_clientdata(i2c, wm8728);
  276. wm8728->control_type = SND_SOC_I2C;
  277. ret = snd_soc_register_codec(&i2c->dev,
  278. &soc_codec_dev_wm8728, &wm8728_dai, 1);
  279. if (ret < 0)
  280. kfree(wm8728);
  281. return ret;
  282. }
  283. static __devexit int wm8728_i2c_remove(struct i2c_client *client)
  284. {
  285. snd_soc_unregister_codec(&client->dev);
  286. kfree(i2c_get_clientdata(client));
  287. return 0;
  288. }
  289. static const struct i2c_device_id wm8728_i2c_id[] = {
  290. { "wm8728", 0 },
  291. { }
  292. };
  293. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  294. static struct i2c_driver wm8728_i2c_driver = {
  295. .driver = {
  296. .name = "wm8728-codec",
  297. .owner = THIS_MODULE,
  298. },
  299. .probe = wm8728_i2c_probe,
  300. .remove = __devexit_p(wm8728_i2c_remove),
  301. .id_table = wm8728_i2c_id,
  302. };
  303. #endif
  304. static int __init wm8728_modinit(void)
  305. {
  306. int ret = 0;
  307. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  308. ret = i2c_add_driver(&wm8728_i2c_driver);
  309. if (ret != 0) {
  310. printk(KERN_ERR "Failed to register wm8728 I2C driver: %d\n",
  311. ret);
  312. }
  313. #endif
  314. #if defined(CONFIG_SPI_MASTER)
  315. ret = spi_register_driver(&wm8728_spi_driver);
  316. if (ret != 0) {
  317. printk(KERN_ERR "Failed to register wm8728 SPI driver: %d\n",
  318. ret);
  319. }
  320. #endif
  321. return ret;
  322. }
  323. module_init(wm8728_modinit);
  324. static void __exit wm8728_exit(void)
  325. {
  326. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  327. i2c_del_driver(&wm8728_i2c_driver);
  328. #endif
  329. #if defined(CONFIG_SPI_MASTER)
  330. spi_unregister_driver(&wm8728_spi_driver);
  331. #endif
  332. }
  333. module_exit(wm8728_exit);
  334. MODULE_DESCRIPTION("ASoC WM8728 driver");
  335. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  336. MODULE_LICENSE("GPL");