wm8728.c 9.0 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 <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_pcm_runtime *rtd = substream->private_data;
  78. struct snd_soc_codec *codec = rtd->codec;
  79. u16 dac = snd_soc_read(codec, WM8728_DACCTL);
  80. dac &= ~0x18;
  81. switch (params_format(params)) {
  82. case SNDRV_PCM_FORMAT_S16_LE:
  83. break;
  84. case SNDRV_PCM_FORMAT_S20_3LE:
  85. dac |= 0x10;
  86. break;
  87. case SNDRV_PCM_FORMAT_S24_LE:
  88. dac |= 0x08;
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. snd_soc_write(codec, WM8728_DACCTL, dac);
  94. return 0;
  95. }
  96. static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
  97. unsigned int fmt)
  98. {
  99. struct snd_soc_codec *codec = codec_dai->codec;
  100. u16 iface = snd_soc_read(codec, WM8728_IFCTL);
  101. /* Currently only I2S is supported by the driver, though the
  102. * hardware is more flexible.
  103. */
  104. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  105. case SND_SOC_DAIFMT_I2S:
  106. iface |= 1;
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. /* The hardware only support full slave mode */
  112. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  113. case SND_SOC_DAIFMT_CBS_CFS:
  114. break;
  115. default:
  116. return -EINVAL;
  117. }
  118. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  119. case SND_SOC_DAIFMT_NB_NF:
  120. iface &= ~0x22;
  121. break;
  122. case SND_SOC_DAIFMT_IB_NF:
  123. iface |= 0x20;
  124. iface &= ~0x02;
  125. break;
  126. case SND_SOC_DAIFMT_NB_IF:
  127. iface |= 0x02;
  128. iface &= ~0x20;
  129. break;
  130. case SND_SOC_DAIFMT_IB_IF:
  131. iface |= 0x22;
  132. break;
  133. default:
  134. return -EINVAL;
  135. }
  136. snd_soc_write(codec, WM8728_IFCTL, iface);
  137. return 0;
  138. }
  139. static int wm8728_set_bias_level(struct snd_soc_codec *codec,
  140. enum snd_soc_bias_level level)
  141. {
  142. u16 reg;
  143. int i;
  144. switch (level) {
  145. case SND_SOC_BIAS_ON:
  146. case SND_SOC_BIAS_PREPARE:
  147. case SND_SOC_BIAS_STANDBY:
  148. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  149. /* Power everything up... */
  150. reg = snd_soc_read(codec, WM8728_DACCTL);
  151. snd_soc_write(codec, WM8728_DACCTL, reg & ~0x4);
  152. /* ..then sync in the register cache. */
  153. for (i = 0; i < ARRAY_SIZE(wm8728_reg_defaults); i++)
  154. snd_soc_write(codec, i,
  155. snd_soc_read(codec, i));
  156. }
  157. break;
  158. case SND_SOC_BIAS_OFF:
  159. reg = snd_soc_read(codec, WM8728_DACCTL);
  160. snd_soc_write(codec, WM8728_DACCTL, reg | 0x4);
  161. break;
  162. }
  163. codec->dapm.bias_level = level;
  164. return 0;
  165. }
  166. #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
  167. #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  168. SNDRV_PCM_FMTBIT_S24_LE)
  169. static const struct snd_soc_dai_ops wm8728_dai_ops = {
  170. .hw_params = wm8728_hw_params,
  171. .digital_mute = wm8728_mute,
  172. .set_fmt = wm8728_set_dai_fmt,
  173. };
  174. static struct snd_soc_dai_driver wm8728_dai = {
  175. .name = "wm8728-hifi",
  176. .playback = {
  177. .stream_name = "Playback",
  178. .channels_min = 2,
  179. .channels_max = 2,
  180. .rates = WM8728_RATES,
  181. .formats = WM8728_FORMATS,
  182. },
  183. .ops = &wm8728_dai_ops,
  184. };
  185. static int wm8728_suspend(struct snd_soc_codec *codec)
  186. {
  187. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  188. return 0;
  189. }
  190. static int wm8728_resume(struct snd_soc_codec *codec)
  191. {
  192. wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  193. return 0;
  194. }
  195. static int wm8728_probe(struct snd_soc_codec *codec)
  196. {
  197. struct wm8728_priv *wm8728 = snd_soc_codec_get_drvdata(codec);
  198. int ret;
  199. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8728->control_type);
  200. if (ret < 0) {
  201. printk(KERN_ERR "wm8728: failed to configure cache I/O: %d\n",
  202. ret);
  203. return ret;
  204. }
  205. /* power on device */
  206. wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  207. return ret;
  208. }
  209. static int wm8728_remove(struct snd_soc_codec *codec)
  210. {
  211. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  212. return 0;
  213. }
  214. static struct snd_soc_codec_driver soc_codec_dev_wm8728 = {
  215. .probe = wm8728_probe,
  216. .remove = wm8728_remove,
  217. .suspend = wm8728_suspend,
  218. .resume = wm8728_resume,
  219. .set_bias_level = wm8728_set_bias_level,
  220. .reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults),
  221. .reg_word_size = sizeof(u16),
  222. .reg_cache_default = wm8728_reg_defaults,
  223. .controls = wm8728_snd_controls,
  224. .num_controls = ARRAY_SIZE(wm8728_snd_controls),
  225. .dapm_widgets = wm8728_dapm_widgets,
  226. .num_dapm_widgets = ARRAY_SIZE(wm8728_dapm_widgets),
  227. .dapm_routes = wm8728_intercon,
  228. .num_dapm_routes = ARRAY_SIZE(wm8728_intercon),
  229. };
  230. static const struct of_device_id wm8728_of_match[] = {
  231. { .compatible = "wlf,wm8728", },
  232. { }
  233. };
  234. MODULE_DEVICE_TABLE(of, wm8728_of_match);
  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",
  260. .owner = THIS_MODULE,
  261. .of_match_table = wm8728_of_match,
  262. },
  263. .probe = wm8728_spi_probe,
  264. .remove = __devexit_p(wm8728_spi_remove),
  265. };
  266. #endif /* CONFIG_SPI_MASTER */
  267. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  268. static __devinit int wm8728_i2c_probe(struct i2c_client *i2c,
  269. const struct i2c_device_id *id)
  270. {
  271. struct wm8728_priv *wm8728;
  272. int ret;
  273. wm8728 = kzalloc(sizeof(struct wm8728_priv), GFP_KERNEL);
  274. if (wm8728 == NULL)
  275. return -ENOMEM;
  276. i2c_set_clientdata(i2c, wm8728);
  277. wm8728->control_type = SND_SOC_I2C;
  278. ret = snd_soc_register_codec(&i2c->dev,
  279. &soc_codec_dev_wm8728, &wm8728_dai, 1);
  280. if (ret < 0)
  281. kfree(wm8728);
  282. return ret;
  283. }
  284. static __devexit int wm8728_i2c_remove(struct i2c_client *client)
  285. {
  286. snd_soc_unregister_codec(&client->dev);
  287. kfree(i2c_get_clientdata(client));
  288. return 0;
  289. }
  290. static const struct i2c_device_id wm8728_i2c_id[] = {
  291. { "wm8728", 0 },
  292. { }
  293. };
  294. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  295. static struct i2c_driver wm8728_i2c_driver = {
  296. .driver = {
  297. .name = "wm8728",
  298. .owner = THIS_MODULE,
  299. .of_match_table = wm8728_of_match,
  300. },
  301. .probe = wm8728_i2c_probe,
  302. .remove = __devexit_p(wm8728_i2c_remove),
  303. .id_table = wm8728_i2c_id,
  304. };
  305. #endif
  306. static int __init wm8728_modinit(void)
  307. {
  308. int ret = 0;
  309. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  310. ret = i2c_add_driver(&wm8728_i2c_driver);
  311. if (ret != 0) {
  312. printk(KERN_ERR "Failed to register wm8728 I2C driver: %d\n",
  313. ret);
  314. }
  315. #endif
  316. #if defined(CONFIG_SPI_MASTER)
  317. ret = spi_register_driver(&wm8728_spi_driver);
  318. if (ret != 0) {
  319. printk(KERN_ERR "Failed to register wm8728 SPI driver: %d\n",
  320. ret);
  321. }
  322. #endif
  323. return ret;
  324. }
  325. module_init(wm8728_modinit);
  326. static void __exit wm8728_exit(void)
  327. {
  328. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  329. i2c_del_driver(&wm8728_i2c_driver);
  330. #endif
  331. #if defined(CONFIG_SPI_MASTER)
  332. spi_unregister_driver(&wm8728_spi_driver);
  333. #endif
  334. }
  335. module_exit(wm8728_exit);
  336. MODULE_DESCRIPTION("ASoC WM8728 driver");
  337. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  338. MODULE_LICENSE("GPL");