wm8728.c 8.9 KB

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