arm64: dts: qcom: sm8550: add TRNG node
[linux-modified.git] / sound / soc / intel / boards / sof_sdw_rt_amp.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2022 Intel Corporation
3
4 /*
5  *  sof_sdw_rt_amp - Helpers to handle RT1308/RT1316/RT1318 from generic machine driver
6  */
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 "sof_sdw_common.h"
18 #include "sof_sdw_amp_coeff_tables.h"
19 #include "../../codecs/rt1308.h"
20
21 #define CODEC_NAME_SIZE 7
22
23 /* choose a larger value to resolve compatibility issues */
24 #define RT_AMP_MAX_BQ_REG RT1316_MAX_BQ_REG
25
26 struct rt_amp_platform_data {
27         const unsigned char *bq_params;
28         const unsigned int bq_params_cnt;
29 };
30
31 static const struct rt_amp_platform_data dell_0a5d_platform_data = {
32         .bq_params = dell_0a5d_bq_params,
33         .bq_params_cnt = ARRAY_SIZE(dell_0a5d_bq_params),
34 };
35
36 static const struct rt_amp_platform_data dell_0b00_platform_data = {
37         .bq_params = dell_0b00_bq_params,
38         .bq_params_cnt = ARRAY_SIZE(dell_0b00_bq_params),
39 };
40
41 static const struct dmi_system_id dmi_platform_data[] = {
42         /* CometLake devices */
43         {
44                 .matches = {
45                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
46                         DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990")
47                 },
48                 .driver_data = (void *)&dell_0a5d_platform_data,
49         },
50         {
51                 .matches = {
52                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
53                         DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F")
54                 },
55                 .driver_data = (void *)&dell_0a5d_platform_data,
56         },
57         /* TigerLake devices */
58         {
59                 .matches = {
60                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
61                         DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5D")
62                 },
63                 .driver_data = (void *)&dell_0a5d_platform_data,
64         },
65         {
66                 .matches = {
67                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
68                         DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5E")
69                 },
70                 .driver_data = (void *)&dell_0a5d_platform_data,
71         },
72         /* AlderLake devices */
73         {
74                 .matches = {
75                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
76                         DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B00")
77                 },
78                 .driver_data = (void *)&dell_0b00_platform_data,
79         },
80         {
81                 .matches = {
82                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
83                         DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B01")
84                 },
85                 .driver_data = (void *)&dell_0b00_platform_data,
86         },
87         {
88                 .matches = {
89                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
90                         DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AFF")
91                 },
92                 .driver_data = (void *)&dell_0b00_platform_data,
93         },
94         {
95                 .matches = {
96                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
97                         DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AFE")
98                 },
99                 .driver_data = (void *)&dell_0b00_platform_data,
100         },
101         {},
102 };
103
104 static int rt_amp_add_device_props(struct device *sdw_dev)
105 {
106         struct property_entry props[3] = {};
107         struct fwnode_handle *fwnode;
108         const struct dmi_system_id *dmi_data;
109         const struct rt_amp_platform_data *pdata;
110         unsigned char params[RT_AMP_MAX_BQ_REG];
111         int ret;
112
113         dmi_data = dmi_first_match(dmi_platform_data);
114         if (!dmi_data)
115                 return 0;
116
117         pdata = dmi_data->driver_data;
118         memcpy(&params, pdata->bq_params, sizeof(unsigned char) * pdata->bq_params_cnt);
119
120         props[0] = PROPERTY_ENTRY_U8_ARRAY("realtek,bq-params", params);
121         props[1] = PROPERTY_ENTRY_U32("realtek,bq-params-cnt", pdata->bq_params_cnt);
122
123         fwnode = fwnode_create_software_node(props, NULL);
124         if (IS_ERR(fwnode))
125                 return PTR_ERR(fwnode);
126
127         ret = device_add_software_node(sdw_dev, to_software_node(fwnode));
128
129         fwnode_handle_put(fwnode);
130
131         return ret;
132 }
133
134 static const struct snd_kcontrol_new rt_amp_controls[] = {
135         SOC_DAPM_PIN_SWITCH("Speaker"),
136 };
137
138 static const struct snd_soc_dapm_widget rt_amp_widgets[] = {
139         SND_SOC_DAPM_SPK("Speaker", NULL),
140 };
141
142 /*
143  * dapm routes for rt1308/rt1316/rt1318 will be registered dynamically
144  * according to the number of rt1308/rt1316/rt1318 used. The first two
145  * entries will be registered for one codec case, and the last two entries
146  * are also registered if two 1308s/1316s/1318s are used.
147  */
148 static const struct snd_soc_dapm_route rt1308_map[] = {
149         { "Speaker", NULL, "rt1308-1 SPOL" },
150         { "Speaker", NULL, "rt1308-1 SPOR" },
151         { "Speaker", NULL, "rt1308-2 SPOL" },
152         { "Speaker", NULL, "rt1308-2 SPOR" },
153 };
154
155 static const struct snd_soc_dapm_route rt1316_map[] = {
156         { "Speaker", NULL, "rt1316-1 SPOL" },
157         { "Speaker", NULL, "rt1316-1 SPOR" },
158         { "Speaker", NULL, "rt1316-2 SPOL" },
159         { "Speaker", NULL, "rt1316-2 SPOR" },
160 };
161
162 static const struct snd_soc_dapm_route rt1318_map[] = {
163         { "Speaker", NULL, "rt1318-1 SPOL" },
164         { "Speaker", NULL, "rt1318-1 SPOR" },
165         { "Speaker", NULL, "rt1318-2 SPOL" },
166         { "Speaker", NULL, "rt1318-2 SPOR" },
167 };
168
169 static const struct snd_soc_dapm_route *get_codec_name_and_route(struct snd_soc_pcm_runtime *rtd,
170                                                                  char *codec_name)
171 {
172         const char *dai_name;
173
174         dai_name = rtd->dai_link->codecs->dai_name;
175
176         /* get the codec name */
177         snprintf(codec_name, CODEC_NAME_SIZE, "%s", dai_name);
178
179         /* choose the right codec's map  */
180         if (strcmp(codec_name, "rt1308") == 0)
181                 return rt1308_map;
182         else if (strcmp(codec_name, "rt1316") == 0)
183                 return rt1316_map;
184         else
185                 return rt1318_map;
186 }
187
188 static int first_spk_init(struct snd_soc_pcm_runtime *rtd)
189 {
190         struct snd_soc_card *card = rtd->card;
191         const struct snd_soc_dapm_route *rt_amp_map;
192         char codec_name[CODEC_NAME_SIZE];
193         int ret;
194
195         rt_amp_map = get_codec_name_and_route(rtd, codec_name);
196
197         card->components = devm_kasprintf(card->dev, GFP_KERNEL,
198                                           "%s spk:%s",
199                                           card->components, codec_name);
200         if (!card->components)
201                 return -ENOMEM;
202
203         ret = snd_soc_add_card_controls(card, rt_amp_controls,
204                                         ARRAY_SIZE(rt_amp_controls));
205         if (ret) {
206                 dev_err(card->dev, "%s controls addition failed: %d\n", codec_name, ret);
207                 return ret;
208         }
209
210         ret = snd_soc_dapm_new_controls(&card->dapm, rt_amp_widgets,
211                                         ARRAY_SIZE(rt_amp_widgets));
212         if (ret) {
213                 dev_err(card->dev, "%s widgets addition failed: %d\n", codec_name, ret);
214                 return ret;
215         }
216
217         ret = snd_soc_dapm_add_routes(&card->dapm, rt_amp_map, 2);
218         if (ret)
219                 dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
220
221         return ret;
222 }
223
224 static int second_spk_init(struct snd_soc_pcm_runtime *rtd)
225 {
226         struct snd_soc_card *card = rtd->card;
227         const struct snd_soc_dapm_route *rt_amp_map;
228         char codec_name[CODEC_NAME_SIZE];
229         int ret;
230
231         rt_amp_map = get_codec_name_and_route(rtd, codec_name);
232
233         ret = snd_soc_dapm_add_routes(&card->dapm, rt_amp_map + 2, 2);
234         if (ret)
235                 dev_err(rtd->dev, "failed to add second SPK map: %d\n", ret);
236
237         return ret;
238 }
239
240 static int all_spk_init(struct snd_soc_pcm_runtime *rtd)
241 {
242         int ret;
243
244         ret = first_spk_init(rtd);
245         if (ret)
246                 return ret;
247
248         return second_spk_init(rtd);
249 }
250
251 static int rt1308_i2s_hw_params(struct snd_pcm_substream *substream,
252                                 struct snd_pcm_hw_params *params)
253 {
254         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
255         struct snd_soc_card *card = rtd->card;
256         struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
257         int clk_id, clk_freq, pll_out;
258         int err;
259
260         clk_id = RT1308_PLL_S_MCLK;
261         clk_freq = 38400000;
262
263         pll_out = params_rate(params) * 512;
264
265         /* Set rt1308 pll */
266         err = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
267         if (err < 0) {
268                 dev_err(card->dev, "Failed to set RT1308 PLL: %d\n", err);
269                 return err;
270         }
271
272         /* Set rt1308 sysclk */
273         err = snd_soc_dai_set_sysclk(codec_dai, RT1308_FS_SYS_S_PLL, pll_out,
274                                      SND_SOC_CLOCK_IN);
275         if (err < 0) {
276                 dev_err(card->dev, "Failed to set RT1308 SYSCLK: %d\n", err);
277                 return err;
278         }
279
280         return 0;
281 }
282
283 /* machine stream operations */
284 struct snd_soc_ops sof_sdw_rt1308_i2s_ops = {
285         .hw_params = rt1308_i2s_hw_params,
286 };
287
288 int sof_sdw_rt_amp_exit(struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
289 {
290         struct mc_private *ctx = snd_soc_card_get_drvdata(card);
291
292         if (ctx->amp_dev1) {
293                 device_remove_software_node(ctx->amp_dev1);
294                 put_device(ctx->amp_dev1);
295         }
296
297         if (ctx->amp_dev2) {
298                 device_remove_software_node(ctx->amp_dev2);
299                 put_device(ctx->amp_dev2);
300         }
301
302         return 0;
303 }
304
305 int sof_sdw_rt_amp_init(struct snd_soc_card *card,
306                         const struct snd_soc_acpi_link_adr *link,
307                         struct snd_soc_dai_link *dai_links,
308                         struct sof_sdw_codec_info *info,
309                         bool playback)
310 {
311         struct mc_private *ctx = snd_soc_card_get_drvdata(card);
312         struct device *sdw_dev1, *sdw_dev2;
313         int ret;
314
315         /* Count amp number and do init on playback link only. */
316         if (!playback)
317                 return 0;
318
319         info->amp_num++;
320         if (info->amp_num == 1)
321                 dai_links->init = first_spk_init;
322
323         if (info->amp_num == 2) {
324                 sdw_dev1 = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[0].name);
325                 if (!sdw_dev1)
326                         return -EPROBE_DEFER;
327
328                 ret = rt_amp_add_device_props(sdw_dev1);
329                 if (ret < 0) {
330                         put_device(sdw_dev1);
331                         return ret;
332                 }
333                 ctx->amp_dev1 = sdw_dev1;
334
335                 sdw_dev2 = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[1].name);
336                 if (!sdw_dev2)
337                         return -EPROBE_DEFER;
338
339                 ret = rt_amp_add_device_props(sdw_dev2);
340                 if (ret < 0) {
341                         put_device(sdw_dev2);
342                         return ret;
343                 }
344                 ctx->amp_dev2 = sdw_dev2;
345
346                 /*
347                  * if two amps are in one dai link, the init function
348                  * in this dai link will be first set for the first speaker,
349                  * and it should be reset to initialize all speakers when
350                  * the second speaker is found.
351                  */
352                 if (dai_links->init)
353                         dai_links->init = all_spk_init;
354                 else
355                         dai_links->init = second_spk_init;
356         }
357
358         return 0;
359 }