wm8728.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * wm8728.c -- WM8728 ALSA SoC Audio driver
  4. *
  5. * Copyright 2008 Wolfson Microelectronics plc
  6. *
  7. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/pm.h>
  14. #include <linux/i2c.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_device.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/initval.h>
  25. #include <sound/tlv.h>
  26. #include "wm8728.h"
  27. /*
  28. * We can't read the WM8728 register space so we cache them instead.
  29. * Note that the defaults here aren't the physical defaults, we latch
  30. * the volume update bits, mute the output and enable infinite zero
  31. * detect.
  32. */
  33. static const struct reg_default wm8728_reg_defaults[] = {
  34. { 0, 0x1ff },
  35. { 1, 0x1ff },
  36. { 2, 0x001 },
  37. { 3, 0x100 },
  38. };
  39. /* codec private data */
  40. struct wm8728_priv {
  41. struct regmap *regmap;
  42. };
  43. static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
  44. static const struct snd_kcontrol_new wm8728_snd_controls[] = {
  45. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
  46. 0, 255, 0, wm8728_tlv),
  47. SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
  48. };
  49. /*
  50. * DAPM controls.
  51. */
  52. static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
  53. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  54. SND_SOC_DAPM_OUTPUT("VOUTL"),
  55. SND_SOC_DAPM_OUTPUT("VOUTR"),
  56. };
  57. static const struct snd_soc_dapm_route wm8728_intercon[] = {
  58. {"VOUTL", NULL, "DAC"},
  59. {"VOUTR", NULL, "DAC"},
  60. };
  61. static int wm8728_mute(struct snd_soc_dai *dai, int mute, int direction)
  62. {
  63. struct snd_soc_component *component = dai->component;
  64. u16 mute_reg = snd_soc_component_read(component, WM8728_DACCTL);
  65. if (mute)
  66. snd_soc_component_write(component, WM8728_DACCTL, mute_reg | 1);
  67. else
  68. snd_soc_component_write(component, WM8728_DACCTL, mute_reg & ~1);
  69. return 0;
  70. }
  71. static int wm8728_hw_params(struct snd_pcm_substream *substream,
  72. struct snd_pcm_hw_params *params,
  73. struct snd_soc_dai *dai)
  74. {
  75. struct snd_soc_component *component = dai->component;
  76. u16 dac = snd_soc_component_read(component, WM8728_DACCTL);
  77. dac &= ~0x18;
  78. switch (params_width(params)) {
  79. case 16:
  80. break;
  81. case 20:
  82. dac |= 0x10;
  83. break;
  84. case 24:
  85. dac |= 0x08;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. snd_soc_component_write(component, WM8728_DACCTL, dac);
  91. return 0;
  92. }
  93. static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
  94. unsigned int fmt)
  95. {
  96. struct snd_soc_component *component = codec_dai->component;
  97. u16 iface = snd_soc_component_read(component, WM8728_IFCTL);
  98. /* Currently only I2S is supported by the driver, though the
  99. * hardware is more flexible.
  100. */
  101. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  102. case SND_SOC_DAIFMT_I2S:
  103. iface |= 1;
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. /* The hardware only support full slave mode */
  109. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  110. case SND_SOC_DAIFMT_CBS_CFS:
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  116. case SND_SOC_DAIFMT_NB_NF:
  117. iface &= ~0x22;
  118. break;
  119. case SND_SOC_DAIFMT_IB_NF:
  120. iface |= 0x20;
  121. iface &= ~0x02;
  122. break;
  123. case SND_SOC_DAIFMT_NB_IF:
  124. iface |= 0x02;
  125. iface &= ~0x20;
  126. break;
  127. case SND_SOC_DAIFMT_IB_IF:
  128. iface |= 0x22;
  129. break;
  130. default:
  131. return -EINVAL;
  132. }
  133. snd_soc_component_write(component, WM8728_IFCTL, iface);
  134. return 0;
  135. }
  136. static int wm8728_set_bias_level(struct snd_soc_component *component,
  137. enum snd_soc_bias_level level)
  138. {
  139. struct wm8728_priv *wm8728 = snd_soc_component_get_drvdata(component);
  140. u16 reg;
  141. switch (level) {
  142. case SND_SOC_BIAS_ON:
  143. case SND_SOC_BIAS_PREPARE:
  144. case SND_SOC_BIAS_STANDBY:
  145. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
  146. /* Power everything up... */
  147. reg = snd_soc_component_read(component, WM8728_DACCTL);
  148. snd_soc_component_write(component, WM8728_DACCTL, reg & ~0x4);
  149. /* ..then sync in the register cache. */
  150. regcache_sync(wm8728->regmap);
  151. }
  152. break;
  153. case SND_SOC_BIAS_OFF:
  154. reg = snd_soc_component_read(component, WM8728_DACCTL);
  155. snd_soc_component_write(component, WM8728_DACCTL, reg | 0x4);
  156. break;
  157. }
  158. return 0;
  159. }
  160. #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
  161. #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  162. SNDRV_PCM_FMTBIT_S24_LE)
  163. static const struct snd_soc_dai_ops wm8728_dai_ops = {
  164. .hw_params = wm8728_hw_params,
  165. .mute_stream = wm8728_mute,
  166. .set_fmt = wm8728_set_dai_fmt,
  167. .no_capture_mute = 1,
  168. };
  169. static struct snd_soc_dai_driver wm8728_dai = {
  170. .name = "wm8728-hifi",
  171. .playback = {
  172. .stream_name = "Playback",
  173. .channels_min = 2,
  174. .channels_max = 2,
  175. .rates = WM8728_RATES,
  176. .formats = WM8728_FORMATS,
  177. },
  178. .ops = &wm8728_dai_ops,
  179. };
  180. static const struct snd_soc_component_driver soc_component_dev_wm8728 = {
  181. .set_bias_level = wm8728_set_bias_level,
  182. .controls = wm8728_snd_controls,
  183. .num_controls = ARRAY_SIZE(wm8728_snd_controls),
  184. .dapm_widgets = wm8728_dapm_widgets,
  185. .num_dapm_widgets = ARRAY_SIZE(wm8728_dapm_widgets),
  186. .dapm_routes = wm8728_intercon,
  187. .num_dapm_routes = ARRAY_SIZE(wm8728_intercon),
  188. .suspend_bias_off = 1,
  189. .idle_bias_on = 1,
  190. .use_pmdown_time = 1,
  191. .endianness = 1,
  192. .non_legacy_dai_naming = 1,
  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 = devm_snd_soc_register_component(&spi->dev,
  221. &soc_component_dev_wm8728, &wm8728_dai, 1);
  222. return ret;
  223. }
  224. static struct spi_driver wm8728_spi_driver = {
  225. .driver = {
  226. .name = "wm8728",
  227. .of_match_table = wm8728_of_match,
  228. },
  229. .probe = wm8728_spi_probe,
  230. };
  231. #endif /* CONFIG_SPI_MASTER */
  232. #if IS_ENABLED(CONFIG_I2C)
  233. static int wm8728_i2c_probe(struct i2c_client *i2c,
  234. const struct i2c_device_id *id)
  235. {
  236. struct wm8728_priv *wm8728;
  237. int ret;
  238. wm8728 = devm_kzalloc(&i2c->dev, sizeof(struct wm8728_priv),
  239. GFP_KERNEL);
  240. if (wm8728 == NULL)
  241. return -ENOMEM;
  242. wm8728->regmap = devm_regmap_init_i2c(i2c, &wm8728_regmap);
  243. if (IS_ERR(wm8728->regmap))
  244. return PTR_ERR(wm8728->regmap);
  245. i2c_set_clientdata(i2c, wm8728);
  246. ret = devm_snd_soc_register_component(&i2c->dev,
  247. &soc_component_dev_wm8728, &wm8728_dai, 1);
  248. return ret;
  249. }
  250. static const struct i2c_device_id wm8728_i2c_id[] = {
  251. { "wm8728", 0 },
  252. { }
  253. };
  254. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  255. static struct i2c_driver wm8728_i2c_driver = {
  256. .driver = {
  257. .name = "wm8728",
  258. .of_match_table = wm8728_of_match,
  259. },
  260. .probe = wm8728_i2c_probe,
  261. .id_table = wm8728_i2c_id,
  262. };
  263. #endif
  264. static int __init wm8728_modinit(void)
  265. {
  266. int ret = 0;
  267. #if IS_ENABLED(CONFIG_I2C)
  268. ret = i2c_add_driver(&wm8728_i2c_driver);
  269. if (ret != 0) {
  270. printk(KERN_ERR "Failed to register wm8728 I2C driver: %d\n",
  271. ret);
  272. }
  273. #endif
  274. #if defined(CONFIG_SPI_MASTER)
  275. ret = spi_register_driver(&wm8728_spi_driver);
  276. if (ret != 0) {
  277. printk(KERN_ERR "Failed to register wm8728 SPI driver: %d\n",
  278. ret);
  279. }
  280. #endif
  281. return ret;
  282. }
  283. module_init(wm8728_modinit);
  284. static void __exit wm8728_exit(void)
  285. {
  286. #if IS_ENABLED(CONFIG_I2C)
  287. i2c_del_driver(&wm8728_i2c_driver);
  288. #endif
  289. #if defined(CONFIG_SPI_MASTER)
  290. spi_unregister_driver(&wm8728_spi_driver);
  291. #endif
  292. }
  293. module_exit(wm8728_exit);
  294. MODULE_DESCRIPTION("ASoC WM8728 driver");
  295. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  296. MODULE_LICENSE("GPL");