wm8728.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/regmap.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/slab.h>
  22. #include <linux/of_device.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/initval.h>
  28. #include <sound/tlv.h>
  29. #include "wm8728.h"
  30. /*
  31. * We can't read the WM8728 register space so we cache them instead.
  32. * Note that the defaults here aren't the physical defaults, we latch
  33. * the volume update bits, mute the output and enable infinite zero
  34. * detect.
  35. */
  36. static const struct reg_default wm8728_reg_defaults[] = {
  37. { 0, 0x1ff },
  38. { 1, 0x1ff },
  39. { 2, 0x001 },
  40. { 3, 0x100 },
  41. };
  42. /* codec private data */
  43. struct wm8728_priv {
  44. struct regmap *regmap;
  45. };
  46. static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
  47. static const struct snd_kcontrol_new wm8728_snd_controls[] = {
  48. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
  49. 0, 255, 0, wm8728_tlv),
  50. SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
  51. };
  52. /*
  53. * DAPM controls.
  54. */
  55. static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
  56. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  57. SND_SOC_DAPM_OUTPUT("VOUTL"),
  58. SND_SOC_DAPM_OUTPUT("VOUTR"),
  59. };
  60. static const struct snd_soc_dapm_route wm8728_intercon[] = {
  61. {"VOUTL", NULL, "DAC"},
  62. {"VOUTR", NULL, "DAC"},
  63. };
  64. static int wm8728_mute(struct snd_soc_dai *dai, int mute)
  65. {
  66. struct snd_soc_codec *codec = dai->codec;
  67. u16 mute_reg = snd_soc_read(codec, WM8728_DACCTL);
  68. if (mute)
  69. snd_soc_write(codec, WM8728_DACCTL, mute_reg | 1);
  70. else
  71. snd_soc_write(codec, WM8728_DACCTL, mute_reg & ~1);
  72. return 0;
  73. }
  74. static int wm8728_hw_params(struct snd_pcm_substream *substream,
  75. struct snd_pcm_hw_params *params,
  76. struct snd_soc_dai *dai)
  77. {
  78. struct snd_soc_codec *codec = dai->codec;
  79. u16 dac = snd_soc_read(codec, WM8728_DACCTL);
  80. dac &= ~0x18;
  81. switch (params_width(params)) {
  82. case 16:
  83. break;
  84. case 20:
  85. dac |= 0x10;
  86. break;
  87. case 24:
  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. struct wm8728_priv *wm8728 = snd_soc_codec_get_drvdata(codec);
  143. u16 reg;
  144. switch (level) {
  145. case SND_SOC_BIAS_ON:
  146. case SND_SOC_BIAS_PREPARE:
  147. case SND_SOC_BIAS_STANDBY:
  148. if (snd_soc_codec_get_bias_level(codec) == 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. regcache_sync(wm8728->regmap);
  154. }
  155. break;
  156. case SND_SOC_BIAS_OFF:
  157. reg = snd_soc_read(codec, WM8728_DACCTL);
  158. snd_soc_write(codec, WM8728_DACCTL, reg | 0x4);
  159. break;
  160. }
  161. return 0;
  162. }
  163. #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
  164. #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  165. SNDRV_PCM_FMTBIT_S24_LE)
  166. static const struct snd_soc_dai_ops wm8728_dai_ops = {
  167. .hw_params = wm8728_hw_params,
  168. .digital_mute = wm8728_mute,
  169. .set_fmt = wm8728_set_dai_fmt,
  170. };
  171. static struct snd_soc_dai_driver wm8728_dai = {
  172. .name = "wm8728-hifi",
  173. .playback = {
  174. .stream_name = "Playback",
  175. .channels_min = 2,
  176. .channels_max = 2,
  177. .rates = WM8728_RATES,
  178. .formats = WM8728_FORMATS,
  179. },
  180. .ops = &wm8728_dai_ops,
  181. };
  182. static const struct snd_soc_codec_driver soc_codec_dev_wm8728 = {
  183. .set_bias_level = wm8728_set_bias_level,
  184. .suspend_bias_off = true,
  185. .component_driver = {
  186. .controls = wm8728_snd_controls,
  187. .num_controls = ARRAY_SIZE(wm8728_snd_controls),
  188. .dapm_widgets = wm8728_dapm_widgets,
  189. .num_dapm_widgets = ARRAY_SIZE(wm8728_dapm_widgets),
  190. .dapm_routes = wm8728_intercon,
  191. .num_dapm_routes = ARRAY_SIZE(wm8728_intercon),
  192. },
  193. };
  194. static const struct of_device_id wm8728_of_match[] = {
  195. { .compatible = "wlf,wm8728", },
  196. { }
  197. };
  198. MODULE_DEVICE_TABLE(of, wm8728_of_match);
  199. static const struct regmap_config wm8728_regmap = {
  200. .reg_bits = 7,
  201. .val_bits = 9,
  202. .max_register = WM8728_IFCTL,
  203. .reg_defaults = wm8728_reg_defaults,
  204. .num_reg_defaults = ARRAY_SIZE(wm8728_reg_defaults),
  205. .cache_type = REGCACHE_RBTREE,
  206. };
  207. #if defined(CONFIG_SPI_MASTER)
  208. static int wm8728_spi_probe(struct spi_device *spi)
  209. {
  210. struct wm8728_priv *wm8728;
  211. int ret;
  212. wm8728 = devm_kzalloc(&spi->dev, sizeof(struct wm8728_priv),
  213. GFP_KERNEL);
  214. if (wm8728 == NULL)
  215. return -ENOMEM;
  216. wm8728->regmap = devm_regmap_init_spi(spi, &wm8728_regmap);
  217. if (IS_ERR(wm8728->regmap))
  218. return PTR_ERR(wm8728->regmap);
  219. spi_set_drvdata(spi, wm8728);
  220. ret = snd_soc_register_codec(&spi->dev,
  221. &soc_codec_dev_wm8728, &wm8728_dai, 1);
  222. return ret;
  223. }
  224. static int wm8728_spi_remove(struct spi_device *spi)
  225. {
  226. snd_soc_unregister_codec(&spi->dev);
  227. return 0;
  228. }
  229. static struct spi_driver wm8728_spi_driver = {
  230. .driver = {
  231. .name = "wm8728",
  232. .of_match_table = wm8728_of_match,
  233. },
  234. .probe = wm8728_spi_probe,
  235. .remove = wm8728_spi_remove,
  236. };
  237. #endif /* CONFIG_SPI_MASTER */
  238. #if IS_ENABLED(CONFIG_I2C)
  239. static int wm8728_i2c_probe(struct i2c_client *i2c,
  240. const struct i2c_device_id *id)
  241. {
  242. struct wm8728_priv *wm8728;
  243. int ret;
  244. wm8728 = devm_kzalloc(&i2c->dev, sizeof(struct wm8728_priv),
  245. GFP_KERNEL);
  246. if (wm8728 == NULL)
  247. return -ENOMEM;
  248. wm8728->regmap = devm_regmap_init_i2c(i2c, &wm8728_regmap);
  249. if (IS_ERR(wm8728->regmap))
  250. return PTR_ERR(wm8728->regmap);
  251. i2c_set_clientdata(i2c, wm8728);
  252. ret = snd_soc_register_codec(&i2c->dev,
  253. &soc_codec_dev_wm8728, &wm8728_dai, 1);
  254. return ret;
  255. }
  256. static int wm8728_i2c_remove(struct i2c_client *client)
  257. {
  258. snd_soc_unregister_codec(&client->dev);
  259. return 0;
  260. }
  261. static const struct i2c_device_id wm8728_i2c_id[] = {
  262. { "wm8728", 0 },
  263. { }
  264. };
  265. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  266. static struct i2c_driver wm8728_i2c_driver = {
  267. .driver = {
  268. .name = "wm8728",
  269. .of_match_table = wm8728_of_match,
  270. },
  271. .probe = wm8728_i2c_probe,
  272. .remove = wm8728_i2c_remove,
  273. .id_table = wm8728_i2c_id,
  274. };
  275. #endif
  276. static int __init wm8728_modinit(void)
  277. {
  278. int ret = 0;
  279. #if IS_ENABLED(CONFIG_I2C)
  280. ret = i2c_add_driver(&wm8728_i2c_driver);
  281. if (ret != 0) {
  282. printk(KERN_ERR "Failed to register wm8728 I2C driver: %d\n",
  283. ret);
  284. }
  285. #endif
  286. #if defined(CONFIG_SPI_MASTER)
  287. ret = spi_register_driver(&wm8728_spi_driver);
  288. if (ret != 0) {
  289. printk(KERN_ERR "Failed to register wm8728 SPI driver: %d\n",
  290. ret);
  291. }
  292. #endif
  293. return ret;
  294. }
  295. module_init(wm8728_modinit);
  296. static void __exit wm8728_exit(void)
  297. {
  298. #if IS_ENABLED(CONFIG_I2C)
  299. i2c_del_driver(&wm8728_i2c_driver);
  300. #endif
  301. #if defined(CONFIG_SPI_MASTER)
  302. spi_unregister_driver(&wm8728_spi_driver);
  303. #endif
  304. }
  305. module_exit(wm8728_exit);
  306. MODULE_DESCRIPTION("ASoC WM8728 driver");
  307. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  308. MODULE_LICENSE("GPL");