wm8524.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * wm8524.c -- WM8524 ALSA SoC Audio driver
  4. *
  5. * Copyright 2009 Wolfson Microelectronics plc
  6. * Copyright 2017 NXP
  7. *
  8. * Based on WM8523 ALSA SoC Audio driver written by Mark Brown
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/of_device.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include <sound/initval.h>
  22. #define WM8524_NUM_RATES 7
  23. /* codec private data */
  24. struct wm8524_priv {
  25. struct gpio_desc *mute;
  26. unsigned int sysclk;
  27. unsigned int rate_constraint_list[WM8524_NUM_RATES];
  28. struct snd_pcm_hw_constraint_list rate_constraint;
  29. };
  30. static const struct snd_soc_dapm_widget wm8524_dapm_widgets[] = {
  31. SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
  32. SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
  33. SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
  34. };
  35. static const struct snd_soc_dapm_route wm8524_dapm_routes[] = {
  36. { "LINEVOUTL", NULL, "DAC" },
  37. { "LINEVOUTR", NULL, "DAC" },
  38. };
  39. static const struct {
  40. int value;
  41. int ratio;
  42. } lrclk_ratios[WM8524_NUM_RATES] = {
  43. { 1, 128 },
  44. { 2, 192 },
  45. { 3, 256 },
  46. { 4, 384 },
  47. { 5, 512 },
  48. { 6, 768 },
  49. { 7, 1152 },
  50. };
  51. static int wm8524_startup(struct snd_pcm_substream *substream,
  52. struct snd_soc_dai *dai)
  53. {
  54. struct snd_soc_component *component = dai->component;
  55. struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
  56. /* The set of sample rates that can be supported depends on the
  57. * MCLK supplied to the CODEC - enforce this.
  58. */
  59. if (!wm8524->sysclk) {
  60. dev_err(component->dev,
  61. "No MCLK configured, call set_sysclk() on init\n");
  62. return -EINVAL;
  63. }
  64. snd_pcm_hw_constraint_list(substream->runtime, 0,
  65. SNDRV_PCM_HW_PARAM_RATE,
  66. &wm8524->rate_constraint);
  67. gpiod_set_value_cansleep(wm8524->mute, 1);
  68. return 0;
  69. }
  70. static void wm8524_shutdown(struct snd_pcm_substream *substream,
  71. struct snd_soc_dai *dai)
  72. {
  73. struct snd_soc_component *component = dai->component;
  74. struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
  75. gpiod_set_value_cansleep(wm8524->mute, 0);
  76. }
  77. static int wm8524_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  78. int clk_id, unsigned int freq, int dir)
  79. {
  80. struct snd_soc_component *component = codec_dai->component;
  81. struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
  82. unsigned int val;
  83. int i, j = 0;
  84. wm8524->sysclk = freq;
  85. wm8524->rate_constraint.count = 0;
  86. for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
  87. val = freq / lrclk_ratios[i].ratio;
  88. /* Check that it's a standard rate since core can't
  89. * cope with others and having the odd rates confuses
  90. * constraint matching.
  91. */
  92. switch (val) {
  93. case 8000:
  94. case 32000:
  95. case 44100:
  96. case 48000:
  97. case 88200:
  98. case 96000:
  99. case 176400:
  100. case 192000:
  101. dev_dbg(component->dev, "Supported sample rate: %dHz\n",
  102. val);
  103. wm8524->rate_constraint_list[j++] = val;
  104. wm8524->rate_constraint.count++;
  105. break;
  106. default:
  107. dev_dbg(component->dev, "Skipping sample rate: %dHz\n",
  108. val);
  109. }
  110. }
  111. /* Need at least one supported rate... */
  112. if (wm8524->rate_constraint.count == 0)
  113. return -EINVAL;
  114. return 0;
  115. }
  116. static int wm8524_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  117. {
  118. fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK |
  119. SND_SOC_DAIFMT_MASTER_MASK);
  120. if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  121. SND_SOC_DAIFMT_CBS_CFS)) {
  122. dev_err(codec_dai->dev, "Invalid DAI format\n");
  123. return -EINVAL;
  124. }
  125. return 0;
  126. }
  127. static int wm8524_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
  128. {
  129. struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(dai->component);
  130. if (wm8524->mute)
  131. gpiod_set_value_cansleep(wm8524->mute, mute);
  132. return 0;
  133. }
  134. #define WM8524_RATES SNDRV_PCM_RATE_8000_192000
  135. #define WM8524_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  136. SNDRV_PCM_FMTBIT_S24_LE |\
  137. SNDRV_PCM_FMTBIT_S32_LE)
  138. static const struct snd_soc_dai_ops wm8524_dai_ops = {
  139. .startup = wm8524_startup,
  140. .shutdown = wm8524_shutdown,
  141. .set_sysclk = wm8524_set_dai_sysclk,
  142. .set_fmt = wm8524_set_fmt,
  143. .mute_stream = wm8524_mute_stream,
  144. };
  145. static struct snd_soc_dai_driver wm8524_dai = {
  146. .name = "wm8524-hifi",
  147. .playback = {
  148. .stream_name = "Playback",
  149. .channels_min = 2,
  150. .channels_max = 2,
  151. .rates = WM8524_RATES,
  152. .formats = WM8524_FORMATS,
  153. },
  154. .ops = &wm8524_dai_ops,
  155. };
  156. static int wm8524_probe(struct snd_soc_component *component)
  157. {
  158. struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
  159. wm8524->rate_constraint.list = &wm8524->rate_constraint_list[0];
  160. wm8524->rate_constraint.count =
  161. ARRAY_SIZE(wm8524->rate_constraint_list);
  162. return 0;
  163. }
  164. static const struct snd_soc_component_driver soc_component_dev_wm8524 = {
  165. .probe = wm8524_probe,
  166. .dapm_widgets = wm8524_dapm_widgets,
  167. .num_dapm_widgets = ARRAY_SIZE(wm8524_dapm_widgets),
  168. .dapm_routes = wm8524_dapm_routes,
  169. .num_dapm_routes = ARRAY_SIZE(wm8524_dapm_routes),
  170. .idle_bias_on = 1,
  171. .use_pmdown_time = 1,
  172. .endianness = 1,
  173. .non_legacy_dai_naming = 1,
  174. };
  175. static const struct of_device_id wm8524_of_match[] = {
  176. { .compatible = "wlf,wm8524" },
  177. { /* sentinel*/ }
  178. };
  179. MODULE_DEVICE_TABLE(of, wm8524_of_match);
  180. static int wm8524_codec_probe(struct platform_device *pdev)
  181. {
  182. struct wm8524_priv *wm8524;
  183. int ret;
  184. wm8524 = devm_kzalloc(&pdev->dev, sizeof(struct wm8524_priv),
  185. GFP_KERNEL);
  186. if (wm8524 == NULL)
  187. return -ENOMEM;
  188. platform_set_drvdata(pdev, wm8524);
  189. wm8524->mute = devm_gpiod_get(&pdev->dev, "wlf,mute", GPIOD_OUT_LOW);
  190. if (IS_ERR(wm8524->mute)) {
  191. ret = PTR_ERR(wm8524->mute);
  192. dev_err(&pdev->dev, "Failed to get mute line: %d\n", ret);
  193. return ret;
  194. }
  195. ret = devm_snd_soc_register_component(&pdev->dev,
  196. &soc_component_dev_wm8524, &wm8524_dai, 1);
  197. if (ret < 0)
  198. dev_err(&pdev->dev, "Failed to register component: %d\n", ret);
  199. return ret;
  200. }
  201. static struct platform_driver wm8524_codec_driver = {
  202. .probe = wm8524_codec_probe,
  203. .driver = {
  204. .name = "wm8524-codec",
  205. .of_match_table = wm8524_of_match,
  206. },
  207. };
  208. module_platform_driver(wm8524_codec_driver);
  209. MODULE_DESCRIPTION("ASoC WM8524 driver");
  210. MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
  211. MODULE_ALIAS("platform:wm8524-codec");
  212. MODULE_LICENSE("GPL");