uda1334.c 7.5 KB

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