soc_sdw_rt_amp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // This file incorporates work covered by the following copyright notice:
  3. // Copyright (c) 2022 Intel Corporation
  4. // Copyright (c) 2024 Advanced Micro Devices, Inc.
  5. /*
  6. * soc_sdw_rt_amp - Helpers to handle RT1308/RT1316/RT1318 from generic machine driver
  7. */
  8. #include <linux/device.h>
  9. #include <linux/errno.h>
  10. #include <sound/control.h>
  11. #include <sound/soc.h>
  12. #include <sound/soc-acpi.h>
  13. #include <sound/soc-dapm.h>
  14. #include <linux/soundwire/sdw.h>
  15. #include <linux/soundwire/sdw_type.h>
  16. #include <linux/dmi.h>
  17. #include <sound/soc_sdw_utils.h>
  18. #include "soc_sdw_rt_amp_coeff_tables.h"
  19. #include "../codecs/rt1308.h"
  20. #define CODEC_NAME_SIZE 7
  21. /* choose a larger value to resolve compatibility issues */
  22. #define RT_AMP_MAX_BQ_REG RT1316_MAX_BQ_REG
  23. struct rt_amp_platform_data {
  24. const unsigned char *bq_params;
  25. const unsigned int bq_params_cnt;
  26. };
  27. static const struct rt_amp_platform_data dell_0a5d_platform_data = {
  28. .bq_params = dell_0a5d_bq_params,
  29. .bq_params_cnt = ARRAY_SIZE(dell_0a5d_bq_params),
  30. };
  31. static const struct rt_amp_platform_data dell_0b00_platform_data = {
  32. .bq_params = dell_0b00_bq_params,
  33. .bq_params_cnt = ARRAY_SIZE(dell_0b00_bq_params),
  34. };
  35. static const struct dmi_system_id dmi_platform_data[] = {
  36. /* CometLake devices */
  37. {
  38. .matches = {
  39. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
  40. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990")
  41. },
  42. .driver_data = (void *)&dell_0a5d_platform_data,
  43. },
  44. {
  45. .matches = {
  46. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
  47. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F")
  48. },
  49. .driver_data = (void *)&dell_0a5d_platform_data,
  50. },
  51. /* TigerLake devices */
  52. {
  53. .matches = {
  54. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
  55. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5D")
  56. },
  57. .driver_data = (void *)&dell_0a5d_platform_data,
  58. },
  59. {
  60. .matches = {
  61. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
  62. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5E")
  63. },
  64. .driver_data = (void *)&dell_0a5d_platform_data,
  65. },
  66. /* AlderLake devices */
  67. {
  68. .matches = {
  69. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
  70. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B00")
  71. },
  72. .driver_data = (void *)&dell_0b00_platform_data,
  73. },
  74. {
  75. .matches = {
  76. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
  77. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B01")
  78. },
  79. .driver_data = (void *)&dell_0b00_platform_data,
  80. },
  81. {
  82. .matches = {
  83. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
  84. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AFF")
  85. },
  86. .driver_data = (void *)&dell_0b00_platform_data,
  87. },
  88. {
  89. .matches = {
  90. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
  91. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AFE")
  92. },
  93. .driver_data = (void *)&dell_0b00_platform_data,
  94. },
  95. {},
  96. };
  97. static int rt_amp_add_device_props(struct device *sdw_dev)
  98. {
  99. struct property_entry props[3] = {};
  100. struct fwnode_handle *fwnode;
  101. const struct dmi_system_id *dmi_data;
  102. const struct rt_amp_platform_data *pdata;
  103. unsigned char params[RT_AMP_MAX_BQ_REG];
  104. int ret;
  105. dmi_data = dmi_first_match(dmi_platform_data);
  106. if (!dmi_data)
  107. return 0;
  108. pdata = dmi_data->driver_data;
  109. memcpy(&params, pdata->bq_params, sizeof(unsigned char) * pdata->bq_params_cnt);
  110. props[0] = PROPERTY_ENTRY_U8_ARRAY("realtek,bq-params", params);
  111. props[1] = PROPERTY_ENTRY_U32("realtek,bq-params-cnt", pdata->bq_params_cnt);
  112. fwnode = fwnode_create_software_node(props, NULL);
  113. if (IS_ERR(fwnode))
  114. return PTR_ERR(fwnode);
  115. ret = device_add_software_node(sdw_dev, to_software_node(fwnode));
  116. fwnode_handle_put(fwnode);
  117. return ret;
  118. }
  119. /*
  120. * dapm routes for rt1308/rt1316/rt1318 will be registered dynamically
  121. * according to the number of rt1308/rt1316/rt1318 used. The first two
  122. * entries will be registered for one codec case, and the last two entries
  123. * are also registered if two 1308s/1316s/1318s are used.
  124. */
  125. static const struct snd_soc_dapm_route rt1308_map[] = {
  126. { "Speaker", NULL, "rt1308-1 SPOL" },
  127. { "Speaker", NULL, "rt1308-1 SPOR" },
  128. { "Speaker", NULL, "rt1308-2 SPOL" },
  129. { "Speaker", NULL, "rt1308-2 SPOR" },
  130. };
  131. static const struct snd_soc_dapm_route rt1316_map[] = {
  132. { "Speaker", NULL, "rt1316-1 SPOL" },
  133. { "Speaker", NULL, "rt1316-1 SPOR" },
  134. { "Speaker", NULL, "rt1316-2 SPOL" },
  135. { "Speaker", NULL, "rt1316-2 SPOR" },
  136. };
  137. static const struct snd_soc_dapm_route rt1318_map[] = {
  138. { "Speaker", NULL, "rt1318-1 SPOL" },
  139. { "Speaker", NULL, "rt1318-1 SPOR" },
  140. { "Speaker", NULL, "rt1318-2 SPOL" },
  141. { "Speaker", NULL, "rt1318-2 SPOR" },
  142. };
  143. static const struct snd_soc_dapm_route rt1320_map[] = {
  144. { "Speaker", NULL, "rt1320-1 SPOL" },
  145. { "Speaker", NULL, "rt1320-1 SPOR" },
  146. { "Speaker", NULL, "rt1320-2 SPOL" },
  147. { "Speaker", NULL, "rt1320-2 SPOR" },
  148. };
  149. static const struct snd_soc_dapm_route *get_codec_name_and_route(struct snd_soc_dai *dai,
  150. char *codec_name)
  151. {
  152. /* get the codec name */
  153. snprintf(codec_name, CODEC_NAME_SIZE, "%s", dai->name);
  154. /* choose the right codec's map */
  155. if (strcmp(codec_name, "rt1308") == 0)
  156. return rt1308_map;
  157. else if (strcmp(codec_name, "rt1316") == 0)
  158. return rt1316_map;
  159. else if (strcmp(codec_name, "rt1318") == 0)
  160. return rt1318_map;
  161. else
  162. return rt1320_map;
  163. }
  164. int asoc_sdw_rt_amp_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
  165. {
  166. struct snd_soc_card *card = rtd->card;
  167. const struct snd_soc_dapm_route *rt_amp_map;
  168. char codec_name[CODEC_NAME_SIZE];
  169. struct snd_soc_dai *codec_dai;
  170. int ret = -EINVAL;
  171. int i;
  172. rt_amp_map = get_codec_name_and_route(dai, codec_name);
  173. card->components = devm_kasprintf(card->dev, GFP_KERNEL,
  174. "%s spk:%s",
  175. card->components, codec_name);
  176. if (!card->components)
  177. return -ENOMEM;
  178. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  179. if (strstr(codec_dai->component->name_prefix, "-1"))
  180. ret = snd_soc_dapm_add_routes(&card->dapm, rt_amp_map, 2);
  181. else if (strstr(codec_dai->component->name_prefix, "-2"))
  182. ret = snd_soc_dapm_add_routes(&card->dapm, rt_amp_map + 2, 2);
  183. }
  184. return ret;
  185. }
  186. EXPORT_SYMBOL_NS(asoc_sdw_rt_amp_spk_rtd_init, SND_SOC_SDW_UTILS);
  187. static int rt1308_i2s_hw_params(struct snd_pcm_substream *substream,
  188. struct snd_pcm_hw_params *params)
  189. {
  190. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  191. struct snd_soc_card *card = rtd->card;
  192. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
  193. int clk_id, clk_freq, pll_out;
  194. int err;
  195. clk_id = RT1308_PLL_S_MCLK;
  196. clk_freq = 38400000;
  197. pll_out = params_rate(params) * 512;
  198. /* Set rt1308 pll */
  199. err = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
  200. if (err < 0) {
  201. dev_err(card->dev, "Failed to set RT1308 PLL: %d\n", err);
  202. return err;
  203. }
  204. /* Set rt1308 sysclk */
  205. err = snd_soc_dai_set_sysclk(codec_dai, RT1308_FS_SYS_S_PLL, pll_out,
  206. SND_SOC_CLOCK_IN);
  207. if (err < 0) {
  208. dev_err(card->dev, "Failed to set RT1308 SYSCLK: %d\n", err);
  209. return err;
  210. }
  211. return 0;
  212. }
  213. /* machine stream operations */
  214. const struct snd_soc_ops soc_sdw_rt1308_i2s_ops = {
  215. .hw_params = rt1308_i2s_hw_params,
  216. };
  217. EXPORT_SYMBOL_NS(soc_sdw_rt1308_i2s_ops, SND_SOC_SDW_UTILS);
  218. int asoc_sdw_rt_amp_exit(struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
  219. {
  220. struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);
  221. if (ctx->amp_dev1) {
  222. device_remove_software_node(ctx->amp_dev1);
  223. put_device(ctx->amp_dev1);
  224. }
  225. if (ctx->amp_dev2) {
  226. device_remove_software_node(ctx->amp_dev2);
  227. put_device(ctx->amp_dev2);
  228. }
  229. return 0;
  230. }
  231. EXPORT_SYMBOL_NS(asoc_sdw_rt_amp_exit, SND_SOC_SDW_UTILS);
  232. int asoc_sdw_rt_amp_init(struct snd_soc_card *card,
  233. struct snd_soc_dai_link *dai_links,
  234. struct asoc_sdw_codec_info *info,
  235. bool playback)
  236. {
  237. struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);
  238. struct device *sdw_dev1, *sdw_dev2;
  239. int ret;
  240. /* Count amp number and do init on playback link only. */
  241. if (!playback)
  242. return 0;
  243. info->amp_num++;
  244. if (info->amp_num == 2) {
  245. sdw_dev1 = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[0].name);
  246. if (!sdw_dev1)
  247. return -EPROBE_DEFER;
  248. ret = rt_amp_add_device_props(sdw_dev1);
  249. if (ret < 0) {
  250. put_device(sdw_dev1);
  251. return ret;
  252. }
  253. ctx->amp_dev1 = sdw_dev1;
  254. sdw_dev2 = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[1].name);
  255. if (!sdw_dev2)
  256. return -EPROBE_DEFER;
  257. ret = rt_amp_add_device_props(sdw_dev2);
  258. if (ret < 0) {
  259. put_device(sdw_dev2);
  260. return ret;
  261. }
  262. ctx->amp_dev2 = sdw_dev2;
  263. }
  264. return 0;
  265. }
  266. EXPORT_SYMBOL_NS(asoc_sdw_rt_amp_init, SND_SOC_SDW_UTILS);