at commit c03c1a14

1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA Shared Lib for I2C&SPI driver
4 //
5 // Copyright 2025 - 2026 Texas Instruments, Inc.
6 //
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
8
9 #include <linux/component.h>
10 #include <linux/crc8.h>
11 #include <linux/crc32.h>
12 #include <linux/efi.h>
13 #include <linux/firmware.h>
14 #include <linux/i2c.h>
15 #include <linux/pm_runtime.h>
16 #include <sound/soc.h>
17 #include <sound/tas2781.h>
18
19 #include "tas2781_hda.h"
20
21 #define CALIBRATION_DATA_AREA_NUM 2
22
23 const efi_guid_t tasdev_fct_efi_guid[] = {
24 /* DELL */
25 EFI_GUID(0xcc92382d, 0x6337, 0x41cb, 0xa8, 0x8b, 0x8e, 0xce, 0x74,
26 0x91, 0xea, 0x9f),
27 /* HP */
28 EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 0x93, 0xfe, 0x5a,
29 0xa3, 0x5d, 0xb3),
30 /* LENOVO & OTHERS */
31 EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09, 0x43, 0xa3, 0xf4,
32 0x31, 0x0a, 0x92),
33 };
34 EXPORT_SYMBOL_NS_GPL(tasdev_fct_efi_guid, "SND_HDA_SCODEC_TAS2781");
35
36 /*
37 * The order of calibrated-data writing function is a bit different from the
38 * order in UEFI. Here is the conversion to match the order of calibrated-data
39 * writing function.
40 */
41 static void cali_cnv(unsigned char *data, unsigned int base, int offset)
42 {
43 struct cali_reg reg_data;
44
45 memcpy(&reg_data, &data[base], sizeof(reg_data));
46 /* the data order has to be swapped between r0_low_reg and inv0_reg */
47 swap(reg_data.r0_low_reg, reg_data.invr0_reg);
48
49 cpu_to_be32_array((__force __be32 *)(data + offset + 1),
50 (u32 *)&reg_data, TASDEV_CALIB_N);
51 }
52
53 static void tas2781_apply_calib(struct tasdevice_priv *p)
54 {
55 struct calidata *cali_data = &p->cali_data;
56 struct cali_reg *r = &cali_data->cali_reg_array;
57 unsigned char *data = cali_data->data;
58 unsigned int *tmp_val = (unsigned int *)data;
59 unsigned int cali_reg[TASDEV_CALIB_N] = {
60 TASDEVICE_REG(0, 0x17, 0x74),
61 TASDEVICE_REG(0, 0x18, 0x0c),
62 TASDEVICE_REG(0, 0x18, 0x14),
63 TASDEVICE_REG(0, 0x13, 0x70),
64 TASDEVICE_REG(0, 0x18, 0x7c),
65 };
66 unsigned int crc, oft, node_num;
67 unsigned char *buf;
68 int i, j, k, l;
69
70 if (tmp_val[0] == 2781) {
71 /*
72 * New features were added in calibrated Data V3:
73 * 1. Added calibration registers address define in
74 * a node, marked as Device id == 0x80.
75 * New features were added in calibrated Data V2:
76 * 1. Added some the fields to store the link_id and
77 * uniqie_id for multi-link solutions
78 * 2. Support flexible number of devices instead of
79 * fixed one in V1.
80 * Layout of calibrated data V2 in UEFI(total 256 bytes):
81 * ChipID (2781, 4 bytes)
82 * Data-Group-Sum (4 bytes)
83 * TimeStamp of Calibration (4 bytes)
84 * for (i = 0; i < Data-Group-Sum; i++) {
85 * if (Data type != 0x80) (4 bytes)
86 * Calibrated Data of Device #i (20 bytes)
87 * else
88 * Calibration registers address (5*4 = 20 bytes)
89 * # V2: No reg addr in data grp section.
90 * # V3: Normally the last grp is the reg addr.
91 * }
92 * CRC (4 bytes)
93 * Reserved (the rest)
94 */
95 crc = crc32(~0, data, (3 + tmp_val[1] * 6) * 4) ^ ~0;
96
97 if (crc != tmp_val[3 + tmp_val[1] * 6]) {
98 cali_data->total_sz = 0;
99 dev_err(p->dev, "%s: CRC error\n", __func__);
100 return;
101 }
102 node_num = tmp_val[1];
103
104 for (j = 0, k = 0; j < node_num; j++) {
105 oft = j * 6 + 3;
106 if (tmp_val[oft] == TASDEV_UEFI_CALI_REG_ADDR_FLG) {
107 for (i = 0; i < TASDEV_CALIB_N; i++) {
108 buf = &data[(oft + i + 1) * 4];
109 cali_reg[i] = TASDEVICE_REG(buf[1],
110 buf[2], buf[3]);
111 }
112 } else {
113 l = j * (cali_data->cali_dat_sz_per_dev + 1);
114 if (k >= p->ndev || l > oft * 4) {
115 dev_err(p->dev, "%s: dev sum error\n",
116 __func__);
117 cali_data->total_sz = 0;
118 return;
119 }
120
121 data[l] = k;
122 oft++;
123 cali_cnv(data, 4 * oft, l);
124 k++;
125 }
126 }
127 } else {
128 /*
129 * Calibration data is in V1 format.
130 * struct cali_data {
131 * char cali_data[20];
132 * }
133 *
134 * struct {
135 * struct cali_data cali_data[4];
136 * int TimeStamp of Calibration (4 bytes)
137 * int CRC (4 bytes)
138 * } ueft;
139 */
140 crc = crc32(~0, data, 84) ^ ~0;
141 if (crc != tmp_val[21]) {
142 /*
143 * Some Lenovo BIOSes (e.g. Legion Pro 7 16IRX9H,
144 * SSID 17aa:38cd) store valid V1 calibration data
145 * but leave TimeStamp and CRC zeroed.
146 */
147 if (!tmp_val[20] && !tmp_val[21] &&
148 tmp_val[0] && tmp_val[1]) {
149 dev_warn(p->dev,
150 "%s: V1 data without CRC, accepting\n",
151 __func__);
152 } else {
153 cali_data->total_sz = 0;
154 dev_err(p->dev, "%s: V1 CRC error\n", __func__);
155 return;
156 }
157 }
158
159 for (j = p->ndev - 1; j >= 0; j--) {
160 l = j * (cali_data->cali_dat_sz_per_dev + 1);
161 cali_cnv(data, cali_data->cali_dat_sz_per_dev * j, l);
162 data[l] = j;
163 }
164 }
165
166 if (p->dspbin_typ == TASDEV_BASIC) {
167 r->r0_reg = cali_reg[0];
168 r->invr0_reg = cali_reg[1];
169 r->r0_low_reg = cali_reg[2];
170 r->pow_reg = cali_reg[3];
171 r->tlimit_reg = cali_reg[4];
172 }
173
174 cali_data->total_sz = p->ndev * (cali_data->cali_dat_sz_per_dev + 1);
175 }
176
177 /*
178 * Update the calibration data, including speaker impedance, f0, etc,
179 * into algo. Calibrate data is done by manufacturer in the factory.
180 * The data is used by Algo for calculating the speaker temperature,
181 * speaker membrane excursion and f0 in real time during playback.
182 * Calibration data format in EFI is V2, since 2024.
183 */
184 int tas2781_save_calibration(struct tas2781_hda *hda)
185 {
186 /*
187 * GUID was used for data access in BIOS, it was provided by board
188 * manufactory.
189 */
190 efi_guid_t efi_guid = tasdev_fct_efi_guid[LENOVO];
191 /*
192 * Some devices save the calibrated data into L"CALI_DATA",
193 * and others into L"SmartAmpCalibrationData".
194 */
195 static efi_char16_t *efi_name[CALIBRATION_DATA_AREA_NUM] = {
196 L"CALI_DATA",
197 L"SmartAmpCalibrationData",
198 };
199 struct tasdevice_priv *p = hda->priv;
200 struct calidata *cali_data = &p->cali_data;
201 unsigned long total_sz = 0;
202 unsigned int attr, size;
203 unsigned char *data;
204 efi_status_t status;
205 int i;
206
207 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
208 dev_err(p->dev, "%s: NO EFI FOUND!\n", __func__);
209 return -EINVAL;
210 }
211
212 if (hda->catlog_id < LENOVO)
213 efi_guid = tasdev_fct_efi_guid[hda->catlog_id];
214
215 cali_data->cali_dat_sz_per_dev = 20;
216 size = p->ndev * (cali_data->cali_dat_sz_per_dev + 1);
217 for (i = 0; i < CALIBRATION_DATA_AREA_NUM; i++) {
218 /* Get real size of UEFI variable */
219 status = efi.get_variable(efi_name[i], &efi_guid, &attr,
220 &total_sz, NULL);
221 cali_data->total_sz = total_sz > size ? total_sz : size;
222 if (status == EFI_BUFFER_TOO_SMALL) {
223 /* Allocate data buffer of data_size bytes */
224 data = cali_data->data = devm_kzalloc(p->dev,
225 cali_data->total_sz, GFP_KERNEL);
226 if (!data) {
227 status = -ENOMEM;
228 continue;
229 }
230 /*
231 * Set to an invalid value before the calibrated data
232 * is stored into it, for the default value is 0, which
233 * means the first device.
234 */
235 data[0] = 0xff;
236 /* Get variable contents into buffer */
237 status = efi.get_variable(efi_name[i], &efi_guid,
238 &attr, &cali_data->total_sz, data);
239 }
240 /* Check whether get the calibrated data */
241 if (status == EFI_SUCCESS)
242 break;
243 }
244
245 if (status != EFI_SUCCESS) {
246 cali_data->total_sz = 0;
247 return status;
248 }
249
250 tas2781_apply_calib(p);
251
252 return 0;
253 }
254 EXPORT_SYMBOL_NS_GPL(tas2781_save_calibration, "SND_HDA_SCODEC_TAS2781");
255
256 void tas2781_hda_remove(struct device *dev,
257 const struct component_ops *ops)
258 {
259 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
260
261 component_del(tas_hda->dev, ops);
262
263 pm_runtime_get_sync(tas_hda->dev);
264 pm_runtime_disable(tas_hda->dev);
265
266 pm_runtime_put_noidle(tas_hda->dev);
267
268 tasdevice_remove(tas_hda->priv);
269 }
270 EXPORT_SYMBOL_NS_GPL(tas2781_hda_remove, "SND_HDA_SCODEC_TAS2781");
271
272 int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_info *uinfo)
274 {
275 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
276
277 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
278 uinfo->count = 1;
279 uinfo->value.integer.min = 0;
280 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
281
282 return 0;
283 }
284 EXPORT_SYMBOL_NS_GPL(tasdevice_info_profile, "SND_HDA_SCODEC_TAS2781");
285
286 int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
287 struct snd_ctl_elem_info *uinfo)
288 {
289 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
290
291 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
292 uinfo->count = 1;
293 uinfo->value.integer.min = 0;
294 uinfo->value.integer.max = tas_priv->fmw->nr_programs - 1;
295
296 return 0;
297 }
298 EXPORT_SYMBOL_NS_GPL(tasdevice_info_programs, "SND_HDA_SCODEC_TAS2781");
299
300 int tasdevice_info_config(struct snd_kcontrol *kcontrol,
301 struct snd_ctl_elem_info *uinfo)
302 {
303 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
304 struct tasdevice_fw *tas_fw = tas_priv->fmw;
305
306 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
307 uinfo->count = 1;
308 uinfo->value.integer.min = 0;
309 uinfo->value.integer.max = tas_fw->nr_configurations - 1;
310
311 return 0;
312 }
313 EXPORT_SYMBOL_NS_GPL(tasdevice_info_config, "SND_HDA_SCODEC_TAS2781");
314
315 int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
316 struct snd_ctl_elem_value *ucontrol)
317 {
318 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
319
320 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
321
322 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,
323 kcontrol->id.name, tas_priv->rcabin.profile_cfg_id);
324
325 return 0;
326 }
327 EXPORT_SYMBOL_NS_GPL(tasdevice_get_profile_id, "SND_HDA_SCODEC_TAS2781");
328
329 int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
330 struct snd_ctl_elem_value *ucontrol)
331 {
332 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
333 int profile_id = ucontrol->value.integer.value[0];
334 int max = tas_priv->rcabin.ncfgs - 1;
335 int val, ret = 0;
336
337 val = clamp(profile_id, 0, max);
338
339 guard(mutex)(&tas_priv->codec_lock);
340
341 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,
342 kcontrol->id.name, tas_priv->rcabin.profile_cfg_id, val);
343
344 if (tas_priv->rcabin.profile_cfg_id != val) {
345 tas_priv->rcabin.profile_cfg_id = val;
346 ret = 1;
347 }
348
349 return ret;
350 }
351 EXPORT_SYMBOL_NS_GPL(tasdevice_set_profile_id, "SND_HDA_SCODEC_TAS2781");
352
353 int tasdevice_program_get(struct snd_kcontrol *kcontrol,
354 struct snd_ctl_elem_value *ucontrol)
355 {
356 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
357
358 ucontrol->value.integer.value[0] = tas_priv->cur_prog;
359
360 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,
361 kcontrol->id.name, tas_priv->cur_prog);
362
363 return 0;
364 }
365 EXPORT_SYMBOL_NS_GPL(tasdevice_program_get, "SND_HDA_SCODEC_TAS2781");
366
367 int tasdevice_program_put(struct snd_kcontrol *kcontrol,
368 struct snd_ctl_elem_value *ucontrol)
369 {
370 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
371 struct tasdevice_fw *tas_fw = tas_priv->fmw;
372 int nr_program = ucontrol->value.integer.value[0];
373 int max = tas_fw->nr_programs - 1;
374 int val, ret = 0;
375
376 val = clamp(nr_program, 0, max);
377
378 guard(mutex)(&tas_priv->codec_lock);
379
380 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,
381 kcontrol->id.name, tas_priv->cur_prog, val);
382
383 if (tas_priv->cur_prog != val) {
384 tas_priv->cur_prog = val;
385 ret = 1;
386 }
387
388 return ret;
389 }
390 EXPORT_SYMBOL_NS_GPL(tasdevice_program_put, "SND_HDA_SCODEC_TAS2781");
391
392 int tasdevice_config_get(struct snd_kcontrol *kcontrol,
393 struct snd_ctl_elem_value *ucontrol)
394 {
395 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
396
397 ucontrol->value.integer.value[0] = tas_priv->cur_conf;
398
399 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,
400 kcontrol->id.name, tas_priv->cur_conf);
401
402 return 0;
403 }
404 EXPORT_SYMBOL_NS_GPL(tasdevice_config_get, "SND_HDA_SCODEC_TAS2781");
405
406 int tasdevice_config_put(struct snd_kcontrol *kcontrol,
407 struct snd_ctl_elem_value *ucontrol)
408 {
409 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
410 struct tasdevice_fw *tas_fw = tas_priv->fmw;
411 int nr_config = ucontrol->value.integer.value[0];
412 int max = tas_fw->nr_configurations - 1;
413 int val, ret = 0;
414
415 val = clamp(nr_config, 0, max);
416
417 guard(mutex)(&tas_priv->codec_lock);
418
419 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,
420 kcontrol->id.name, tas_priv->cur_conf, val);
421
422 if (tas_priv->cur_conf != val) {
423 tas_priv->cur_conf = val;
424 ret = 1;
425 }
426
427 return ret;
428 }
429 EXPORT_SYMBOL_NS_GPL(tasdevice_config_put, "SND_HDA_SCODEC_TAS2781");
430
431 MODULE_DESCRIPTION("TAS2781 HDA Driver");
432 MODULE_LICENSE("GPL");
433 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");