max98373.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2022 Intel Corporation. All rights reserved.
  4. //
  5. // Authors: Cezary Rojewski <[email protected]>
  6. // Amadeusz Slawinski <[email protected]>
  7. //
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <sound/pcm_params.h>
  11. #include <sound/soc.h>
  12. #include <sound/soc-acpi.h>
  13. #include <sound/soc-dapm.h>
  14. #define MAX98373_DEV0_NAME "i2c-MX98373:00"
  15. #define MAX98373_DEV1_NAME "i2c-MX98373:01"
  16. #define MAX98373_CODEC_NAME "max98373-aif1"
  17. static struct snd_soc_codec_conf card_codec_conf[] = {
  18. {
  19. .dlc = COMP_CODEC_CONF(MAX98373_DEV0_NAME),
  20. .name_prefix = "Right",
  21. },
  22. {
  23. .dlc = COMP_CODEC_CONF(MAX98373_DEV1_NAME),
  24. .name_prefix = "Left",
  25. },
  26. };
  27. static const struct snd_kcontrol_new card_controls[] = {
  28. SOC_DAPM_PIN_SWITCH("Left Spk"),
  29. SOC_DAPM_PIN_SWITCH("Right Spk"),
  30. };
  31. static const struct snd_soc_dapm_widget card_widgets[] = {
  32. SND_SOC_DAPM_SPK("Left Spk", NULL),
  33. SND_SOC_DAPM_SPK("Right Spk", NULL),
  34. };
  35. static const struct snd_soc_dapm_route card_base_routes[] = {
  36. { "Left Spk", NULL, "Left BE_OUT" },
  37. { "Right Spk", NULL, "Right BE_OUT" },
  38. };
  39. static int
  40. avs_max98373_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
  41. {
  42. struct snd_interval *rate, *channels;
  43. struct snd_mask *fmt;
  44. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  45. channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  46. fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  47. /* The ADSP will covert the FE rate to 48k, stereo */
  48. rate->min = rate->max = 48000;
  49. channels->min = channels->max = 2;
  50. /* set SSP0 to 16 bit */
  51. snd_mask_none(fmt);
  52. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
  53. return 0;
  54. }
  55. static int avs_max98373_hw_params(struct snd_pcm_substream *substream,
  56. struct snd_pcm_hw_params *params)
  57. {
  58. struct snd_soc_pcm_runtime *runtime = asoc_substream_to_rtd(substream);
  59. struct snd_soc_dai *codec_dai;
  60. int ret, i;
  61. for_each_rtd_codec_dais(runtime, i, codec_dai) {
  62. if (!strcmp(codec_dai->component->name, MAX98373_DEV0_NAME)) {
  63. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
  64. if (ret < 0) {
  65. dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
  66. return ret;
  67. }
  68. }
  69. if (!strcmp(codec_dai->component->name, MAX98373_DEV1_NAME)) {
  70. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
  71. if (ret < 0) {
  72. dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
  73. return ret;
  74. }
  75. }
  76. }
  77. return 0;
  78. }
  79. static const struct snd_soc_ops avs_max98373_ops = {
  80. .hw_params = avs_max98373_hw_params,
  81. };
  82. static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
  83. struct snd_soc_dai_link **dai_link)
  84. {
  85. struct snd_soc_dai_link_component *platform;
  86. struct snd_soc_dai_link *dl;
  87. dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
  88. platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
  89. if (!dl || !platform)
  90. return -ENOMEM;
  91. platform->name = platform_name;
  92. dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
  93. dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
  94. dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
  95. if (!dl->name || !dl->cpus || !dl->codecs)
  96. return -ENOMEM;
  97. dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
  98. dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV0_NAME);
  99. dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
  100. dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV1_NAME);
  101. dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
  102. if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
  103. !dl->codecs[1].name || !dl->codecs[1].dai_name)
  104. return -ENOMEM;
  105. dl->num_cpus = 1;
  106. dl->num_codecs = 2;
  107. dl->platforms = platform;
  108. dl->num_platforms = 1;
  109. dl->id = 0;
  110. dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
  111. dl->be_hw_params_fixup = avs_max98373_be_fixup;
  112. dl->nonatomic = 1;
  113. dl->no_pcm = 1;
  114. dl->dpcm_capture = 1;
  115. dl->dpcm_playback = 1;
  116. dl->ignore_pmdown_time = 1;
  117. dl->ops = &avs_max98373_ops;
  118. *dai_link = dl;
  119. return 0;
  120. }
  121. static int avs_create_dapm_routes(struct device *dev, int ssp_port,
  122. struct snd_soc_dapm_route **routes, int *num_routes)
  123. {
  124. struct snd_soc_dapm_route *dr;
  125. const int num_base = ARRAY_SIZE(card_base_routes);
  126. const int num_dr = num_base + 2;
  127. int idx;
  128. dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
  129. if (!dr)
  130. return -ENOMEM;
  131. memcpy(dr, card_base_routes, num_base * sizeof(*dr));
  132. idx = num_base;
  133. dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Left HiFi Playback");
  134. dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
  135. if (!dr[idx].sink || !dr[idx].source)
  136. return -ENOMEM;
  137. idx++;
  138. dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Right HiFi Playback");
  139. dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
  140. if (!dr[idx].sink || !dr[idx].source)
  141. return -ENOMEM;
  142. *routes = dr;
  143. *num_routes = num_dr;
  144. return 0;
  145. }
  146. static int avs_max98373_probe(struct platform_device *pdev)
  147. {
  148. struct snd_soc_dapm_route *routes;
  149. struct snd_soc_dai_link *dai_link;
  150. struct snd_soc_acpi_mach *mach;
  151. struct snd_soc_card *card;
  152. struct device *dev = &pdev->dev;
  153. const char *pname;
  154. int num_routes, ssp_port, ret;
  155. mach = dev_get_platdata(dev);
  156. pname = mach->mach_params.platform;
  157. ssp_port = __ffs(mach->mach_params.i2s_link_mask);
  158. ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
  159. if (ret) {
  160. dev_err(dev, "Failed to create dai link: %d", ret);
  161. return ret;
  162. }
  163. ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
  164. if (ret) {
  165. dev_err(dev, "Failed to create dapm routes: %d", ret);
  166. return ret;
  167. }
  168. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  169. if (!card)
  170. return -ENOMEM;
  171. card->name = "avs_max98373";
  172. card->dev = dev;
  173. card->owner = THIS_MODULE;
  174. card->dai_link = dai_link;
  175. card->num_links = 1;
  176. card->codec_conf = card_codec_conf;
  177. card->num_configs = ARRAY_SIZE(card_codec_conf);
  178. card->controls = card_controls;
  179. card->num_controls = ARRAY_SIZE(card_controls);
  180. card->dapm_widgets = card_widgets;
  181. card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
  182. card->dapm_routes = routes;
  183. card->num_dapm_routes = num_routes;
  184. card->fully_routed = true;
  185. ret = snd_soc_fixup_dai_links_platform_name(card, pname);
  186. if (ret)
  187. return ret;
  188. return devm_snd_soc_register_card(dev, card);
  189. }
  190. static struct platform_driver avs_max98373_driver = {
  191. .probe = avs_max98373_probe,
  192. .driver = {
  193. .name = "avs_max98373",
  194. .pm = &snd_soc_pm_ops,
  195. },
  196. };
  197. module_platform_driver(avs_max98373_driver)
  198. MODULE_LICENSE("GPL");
  199. MODULE_ALIAS("platform:avs_max98373");