wm8728.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/initval.h>
  26. #include <sound/tlv.h>
  27. #include "wm8728.h"
  28. /*
  29. * We can't read the WM8728 register space so we cache them instead.
  30. * Note that the defaults here aren't the physical defaults, we latch
  31. * the volume update bits, mute the output and enable infinite zero
  32. * detect.
  33. */
  34. static const u16 wm8728_reg_defaults[] = {
  35. 0x1ff,
  36. 0x1ff,
  37. 0x001,
  38. 0x100,
  39. };
  40. /* codec private data */
  41. struct wm8728_priv {
  42. enum snd_soc_control_type control_type;
  43. };
  44. static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
  45. static const struct snd_kcontrol_new wm8728_snd_controls[] = {
  46. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
  47. 0, 255, 0, wm8728_tlv),
  48. SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
  49. };
  50. /*
  51. * DAPM controls.
  52. */
  53. static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
  54. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  55. SND_SOC_DAPM_OUTPUT("VOUTL"),
  56. SND_SOC_DAPM_OUTPUT("VOUTR"),
  57. };
  58. static const struct snd_soc_dapm_route wm8728_intercon[] = {
  59. {"VOUTL", NULL, "DAC"},
  60. {"VOUTR", NULL, "DAC"},
  61. };
  62. static int wm8728_mute(struct snd_soc_dai *dai, int mute)
  63. {
  64. struct snd_soc_codec *codec = dai->codec;
  65. u16 mute_reg = snd_soc_read(codec, WM8728_DACCTL);
  66. if (mute)
  67. snd_soc_write(codec, WM8728_DACCTL, mute_reg | 1);
  68. else
  69. snd_soc_write(codec, WM8728_DACCTL, mute_reg & ~1);
  70. return 0;
  71. }
  72. static int wm8728_hw_params(struct snd_pcm_substream *substream,
  73. struct snd_pcm_hw_params *params,
  74. struct snd_soc_dai *dai)
  75. {
  76. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  77. struct snd_soc_codec *codec = rtd->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 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, pm_message_t state)
  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. snd_soc_add_controls(codec, wm8728_snd_controls,
  207. ARRAY_SIZE(wm8728_snd_controls));
  208. return ret;
  209. }
  210. static int wm8728_remove(struct snd_soc_codec *codec)
  211. {
  212. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  213. return 0;
  214. }
  215. static struct snd_soc_codec_driver soc_codec_dev_wm8728 = {
  216. .probe = wm8728_probe,
  217. .remove = wm8728_remove,
  218. .suspend = wm8728_suspend,
  219. .resume = wm8728_resume,
  220. .set_bias_level = wm8728_set_bias_level,
  221. .reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults),
  222. .reg_word_size = sizeof(u16),
  223. .reg_cache_default = wm8728_reg_defaults,
  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. #if defined(CONFIG_SPI_MASTER)
  230. static int __devinit wm8728_spi_probe(struct spi_device *spi)
  231. {
  232. struct wm8728_priv *wm8728;
  233. int ret;
  234. wm8728 = kzalloc(sizeof(struct wm8728_priv), GFP_KERNEL);
  235. if (wm8728 == NULL)
  236. return -ENOMEM;
  237. wm8728->control_type = SND_SOC_SPI;
  238. spi_set_drvdata(spi, wm8728);
  239. ret = snd_soc_register_codec(&spi->dev,
  240. &soc_codec_dev_wm8728, &wm8728_dai, 1);
  241. if (ret < 0)
  242. kfree(wm8728);
  243. return ret;
  244. }
  245. static int __devexit wm8728_spi_remove(struct spi_device *spi)
  246. {
  247. snd_soc_unregister_codec(&spi->dev);
  248. kfree(spi_get_drvdata(spi));
  249. return 0;
  250. }
  251. static struct spi_driver wm8728_spi_driver = {
  252. .driver = {
  253. .name = "wm8728-codec",
  254. .owner = THIS_MODULE,
  255. },
  256. .probe = wm8728_spi_probe,
  257. .remove = __devexit_p(wm8728_spi_remove),
  258. };
  259. #endif /* CONFIG_SPI_MASTER */
  260. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  261. static __devinit int wm8728_i2c_probe(struct i2c_client *i2c,
  262. const struct i2c_device_id *id)
  263. {
  264. struct wm8728_priv *wm8728;
  265. int ret;
  266. wm8728 = kzalloc(sizeof(struct wm8728_priv), GFP_KERNEL);
  267. if (wm8728 == NULL)
  268. return -ENOMEM;
  269. i2c_set_clientdata(i2c, wm8728);
  270. wm8728->control_type = SND_SOC_I2C;
  271. ret = snd_soc_register_codec(&i2c->dev,
  272. &soc_codec_dev_wm8728, &wm8728_dai, 1);
  273. if (ret < 0)
  274. kfree(wm8728);
  275. return ret;
  276. }
  277. static __devexit int wm8728_i2c_remove(struct i2c_client *client)
  278. {
  279. snd_soc_unregister_codec(&client->dev);
  280. kfree(i2c_get_clientdata(client));
  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-codec",
  291. .owner = THIS_MODULE,
  292. },
  293. .probe = wm8728_i2c_probe,
  294. .remove = __devexit_p(wm8728_i2c_remove),
  295. .id_table = wm8728_i2c_id,
  296. };
  297. #endif
  298. static int __init wm8728_modinit(void)
  299. {
  300. int ret = 0;
  301. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  302. ret = i2c_add_driver(&wm8728_i2c_driver);
  303. if (ret != 0) {
  304. printk(KERN_ERR "Failed to register wm8728 I2C driver: %d\n",
  305. ret);
  306. }
  307. #endif
  308. #if defined(CONFIG_SPI_MASTER)
  309. ret = spi_register_driver(&wm8728_spi_driver);
  310. if (ret != 0) {
  311. printk(KERN_ERR "Failed to register wm8728 SPI driver: %d\n",
  312. ret);
  313. }
  314. #endif
  315. return ret;
  316. }
  317. module_init(wm8728_modinit);
  318. static void __exit wm8728_exit(void)
  319. {
  320. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  321. i2c_del_driver(&wm8728_i2c_driver);
  322. #endif
  323. #if defined(CONFIG_SPI_MASTER)
  324. spi_unregister_driver(&wm8728_spi_driver);
  325. #endif
  326. }
  327. module_exit(wm8728_exit);
  328. MODULE_DESCRIPTION("ASoC WM8728 driver");
  329. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  330. MODULE_LICENSE("GPL");