wm8728.c 8.9 KB

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