xref: /linux/sound/pci/hda/tas2781_hda_i2c.c (revision 6a34dfa15d6edf7e78b8118d862d2db0889cf669)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA I2C driver
4 //
5 // Copyright 2023 - 2024 Texas Instruments, Inc.
6 //
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
8 // Current maintainer: Baojun Xu <baojun.xu@ti.com>
9 
10 #include <linux/unaligned.h>
11 #include <linux/acpi.h>
12 #include <linux/crc8.h>
13 #include <linux/crc32.h>
14 #include <linux/efi.h>
15 #include <linux/firmware.h>
16 #include <linux/i2c.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/pci_ids.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <sound/hda_codec.h>
23 #include <sound/soc.h>
24 #include <sound/tas2781.h>
25 #include <sound/tlv.h>
26 #include <sound/tas2781-tlv.h>
27 
28 #include "hda_local.h"
29 #include "hda_auto_parser.h"
30 #include "hda_component.h"
31 #include "hda_jack.h"
32 #include "hda_generic.h"
33 
34 #define TASDEVICE_SPEAKER_CALIBRATION_SIZE	20
35 
36 /* No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD
37  * Define two controls, one is Volume control callbacks, the other is
38  * flag setting control callbacks.
39  */
40 
41 /* Volume control callbacks for tas2781 */
42 #define ACARD_SINGLE_RANGE_EXT_TLV(xname, xreg, xshift, xmin, xmax, xinvert, \
43 	xhandler_get, xhandler_put, tlv_array) \
44 {	.iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = (xname),\
45 	.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
46 		 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
47 	.tlv.p = (tlv_array), \
48 	.info = snd_soc_info_volsw_range, \
49 	.get = xhandler_get, .put = xhandler_put, \
50 	.private_value = (unsigned long)&(struct soc_mixer_control) \
51 		{.reg = xreg, .rreg = xreg, .shift = xshift, \
52 		 .rshift = xshift, .min = xmin, .max = xmax, \
53 		 .invert = xinvert} }
54 
55 /* Flag control callbacks for tas2781 */
56 #define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
57 {	.iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, \
58 	.info = snd_ctl_boolean_mono_info, \
59 	.get = xhandler_get, .put = xhandler_put, \
60 	.private_value = xdata }
61 
62 enum calib_data {
63 	R0_VAL = 0,
64 	INV_R0,
65 	R0LOW,
66 	POWER,
67 	TLIM,
68 	CALIB_MAX
69 };
70 
71 #define TAS2563_MAX_CHANNELS	4
72 
73 #define TAS2563_CAL_POWER	TASDEVICE_REG(0, 0x0d, 0x3c)
74 #define TAS2563_CAL_R0		TASDEVICE_REG(0, 0x0f, 0x34)
75 #define TAS2563_CAL_INVR0	TASDEVICE_REG(0, 0x0f, 0x40)
76 #define TAS2563_CAL_R0_LOW	TASDEVICE_REG(0, 0x0f, 0x48)
77 #define TAS2563_CAL_TLIM	TASDEVICE_REG(0, 0x10, 0x14)
78 #define TAS2563_CAL_N		5
79 #define TAS2563_CAL_DATA_SIZE	4
80 #define TAS2563_CAL_CH_SIZE	20
81 #define TAS2563_CAL_ARRAY_SIZE	80
82 
83 static unsigned int cal_regs[TAS2563_CAL_N] = {
84 	TAS2563_CAL_POWER, TAS2563_CAL_R0, TAS2563_CAL_INVR0,
85 	TAS2563_CAL_R0_LOW, TAS2563_CAL_TLIM,
86 };
87 
88 
89 struct tas2781_hda {
90 	struct device *dev;
91 	struct tasdevice_priv *priv;
92 	struct snd_kcontrol *dsp_prog_ctl;
93 	struct snd_kcontrol *dsp_conf_ctl;
94 	struct snd_kcontrol *prof_ctl;
95 	struct snd_kcontrol *snd_ctls[2];
96 };
97 
98 static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
99 {
100 	struct tasdevice_priv *tas_priv = data;
101 	struct acpi_resource_i2c_serialbus *sb;
102 
103 	if (i2c_acpi_get_i2c_resource(ares, &sb)) {
104 		if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS &&
105 			sb->slave_address != tas_priv->global_addr) {
106 			tas_priv->tasdevice[tas_priv->ndev].dev_addr =
107 				(unsigned int)sb->slave_address;
108 			tas_priv->ndev++;
109 		}
110 	}
111 	return 1;
112 }
113 
114 static const struct acpi_gpio_params speakerid_gpios = { 0, 0, false };
115 
116 static const struct acpi_gpio_mapping tas2781_speaker_id_gpios[] = {
117 	{ "speakerid-gpios", &speakerid_gpios, 1 },
118 	{ }
119 };
120 
121 static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
122 {
123 	struct acpi_device *adev;
124 	struct device *physdev;
125 	LIST_HEAD(resources);
126 	const char *sub;
127 	uint32_t subid;
128 	int ret;
129 
130 	adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
131 	if (!adev) {
132 		dev_err(p->dev,
133 			"Failed to find an ACPI device for %s\n", hid);
134 		return -ENODEV;
135 	}
136 
137 	physdev = get_device(acpi_get_first_physical_node(adev));
138 	ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
139 	if (ret < 0) {
140 		dev_err(p->dev, "Failed to get ACPI resource.\n");
141 		goto err;
142 	}
143 	sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
144 	if (IS_ERR(sub)) {
145 		dev_err(p->dev, "Failed to get SUBSYS ID.\n");
146 		goto err;
147 	}
148 	/* Speaker id was needed for ASUS projects. */
149 	ret = kstrtou32(sub, 16, &subid);
150 	if (!ret && upper_16_bits(subid) == PCI_VENDOR_ID_ASUSTEK) {
151 		ret = devm_acpi_dev_add_driver_gpios(p->dev,
152 			tas2781_speaker_id_gpios);
153 		if (ret < 0)
154 			dev_err(p->dev, "Failed to add driver gpio %d.\n",
155 				ret);
156 		p->speaker_id = devm_gpiod_get(p->dev, "speakerid", GPIOD_IN);
157 		if (IS_ERR(p->speaker_id)) {
158 			dev_err(p->dev, "Failed to get Speaker id.\n");
159 			ret = PTR_ERR(p->speaker_id);
160 			goto err;
161 		}
162 	} else {
163 		p->speaker_id = NULL;
164 	}
165 
166 	acpi_dev_free_resource_list(&resources);
167 	strscpy(p->dev_name, hid, sizeof(p->dev_name));
168 	put_device(physdev);
169 	acpi_dev_put(adev);
170 
171 	return 0;
172 
173 err:
174 	dev_err(p->dev, "read acpi error, ret: %d\n", ret);
175 	put_device(physdev);
176 	acpi_dev_put(adev);
177 
178 	return ret;
179 }
180 
181 static void tas2781_hda_playback_hook(struct device *dev, int action)
182 {
183 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
184 
185 	dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
186 	switch (action) {
187 	case HDA_GEN_PCM_ACT_OPEN:
188 		pm_runtime_get_sync(dev);
189 		mutex_lock(&tas_hda->priv->codec_lock);
190 		tasdevice_tuning_switch(tas_hda->priv, 0);
191 		tas_hda->priv->playback_started = true;
192 		mutex_unlock(&tas_hda->priv->codec_lock);
193 		break;
194 	case HDA_GEN_PCM_ACT_CLOSE:
195 		mutex_lock(&tas_hda->priv->codec_lock);
196 		tasdevice_tuning_switch(tas_hda->priv, 1);
197 		tas_hda->priv->playback_started = false;
198 		mutex_unlock(&tas_hda->priv->codec_lock);
199 
200 		pm_runtime_mark_last_busy(dev);
201 		pm_runtime_put_autosuspend(dev);
202 		break;
203 	default:
204 		break;
205 	}
206 }
207 
208 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
209 			struct snd_ctl_elem_info *uinfo)
210 {
211 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
212 
213 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
214 	uinfo->count = 1;
215 	uinfo->value.integer.min = 0;
216 	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
217 
218 	return 0;
219 }
220 
221 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
222 			struct snd_ctl_elem_value *ucontrol)
223 {
224 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
225 
226 	mutex_lock(&tas_priv->codec_lock);
227 
228 	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
229 
230 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
231 		__func__, kcontrol->id.name, tas_priv->rcabin.profile_cfg_id);
232 
233 	mutex_unlock(&tas_priv->codec_lock);
234 
235 	return 0;
236 }
237 
238 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
239 		struct snd_ctl_elem_value *ucontrol)
240 {
241 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
242 	int nr_profile = ucontrol->value.integer.value[0];
243 	int max = tas_priv->rcabin.ncfgs - 1;
244 	int val, ret = 0;
245 
246 	val = clamp(nr_profile, 0, max);
247 
248 	mutex_lock(&tas_priv->codec_lock);
249 
250 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
251 		__func__, kcontrol->id.name,
252 		tas_priv->rcabin.profile_cfg_id, val);
253 
254 	if (tas_priv->rcabin.profile_cfg_id != val) {
255 		tas_priv->rcabin.profile_cfg_id = val;
256 		ret = 1;
257 	}
258 
259 	mutex_unlock(&tas_priv->codec_lock);
260 
261 	return ret;
262 }
263 
264 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
265 			struct snd_ctl_elem_info *uinfo)
266 {
267 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
268 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
269 
270 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
271 	uinfo->count = 1;
272 	uinfo->value.integer.min = 0;
273 	uinfo->value.integer.max = tas_fw->nr_programs - 1;
274 
275 	return 0;
276 }
277 
278 static int tasdevice_info_config(struct snd_kcontrol *kcontrol,
279 	struct snd_ctl_elem_info *uinfo)
280 {
281 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
282 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
283 
284 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
285 	uinfo->count = 1;
286 	uinfo->value.integer.min = 0;
287 	uinfo->value.integer.max = tas_fw->nr_configurations - 1;
288 
289 	return 0;
290 }
291 
292 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
293 	struct snd_ctl_elem_value *ucontrol)
294 {
295 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
296 
297 	mutex_lock(&tas_priv->codec_lock);
298 
299 	ucontrol->value.integer.value[0] = tas_priv->cur_prog;
300 
301 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
302 		__func__, kcontrol->id.name, tas_priv->cur_prog);
303 
304 	mutex_unlock(&tas_priv->codec_lock);
305 
306 	return 0;
307 }
308 
309 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
310 	struct snd_ctl_elem_value *ucontrol)
311 {
312 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
313 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
314 	int nr_program = ucontrol->value.integer.value[0];
315 	int max = tas_fw->nr_programs - 1;
316 	int val, ret = 0;
317 
318 	val = clamp(nr_program, 0, max);
319 
320 	mutex_lock(&tas_priv->codec_lock);
321 
322 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
323 		__func__, kcontrol->id.name, tas_priv->cur_prog, val);
324 
325 	if (tas_priv->cur_prog != val) {
326 		tas_priv->cur_prog = val;
327 		ret = 1;
328 	}
329 
330 	mutex_unlock(&tas_priv->codec_lock);
331 
332 	return ret;
333 }
334 
335 static int tasdevice_config_get(struct snd_kcontrol *kcontrol,
336 	struct snd_ctl_elem_value *ucontrol)
337 {
338 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
339 
340 	mutex_lock(&tas_priv->codec_lock);
341 
342 	ucontrol->value.integer.value[0] = tas_priv->cur_conf;
343 
344 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
345 		__func__, kcontrol->id.name, tas_priv->cur_conf);
346 
347 	mutex_unlock(&tas_priv->codec_lock);
348 
349 	return 0;
350 }
351 
352 static int tasdevice_config_put(struct snd_kcontrol *kcontrol,
353 	struct snd_ctl_elem_value *ucontrol)
354 {
355 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
356 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
357 	int nr_config = ucontrol->value.integer.value[0];
358 	int max = tas_fw->nr_configurations - 1;
359 	int val, ret = 0;
360 
361 	val = clamp(nr_config, 0, max);
362 
363 	mutex_lock(&tas_priv->codec_lock);
364 
365 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
366 		__func__, kcontrol->id.name, tas_priv->cur_conf, val);
367 
368 	if (tas_priv->cur_conf != val) {
369 		tas_priv->cur_conf = val;
370 		ret = 1;
371 	}
372 
373 	mutex_unlock(&tas_priv->codec_lock);
374 
375 	return ret;
376 }
377 
378 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
379 	struct snd_ctl_elem_value *ucontrol)
380 {
381 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
382 	struct soc_mixer_control *mc =
383 		(struct soc_mixer_control *)kcontrol->private_value;
384 	int ret;
385 
386 	mutex_lock(&tas_priv->codec_lock);
387 
388 	ret = tasdevice_amp_getvol(tas_priv, ucontrol, mc);
389 
390 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %ld\n",
391 		__func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
392 
393 	mutex_unlock(&tas_priv->codec_lock);
394 
395 	return ret;
396 }
397 
398 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
399 	struct snd_ctl_elem_value *ucontrol)
400 {
401 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
402 	struct soc_mixer_control *mc =
403 		(struct soc_mixer_control *)kcontrol->private_value;
404 	int ret;
405 
406 	mutex_lock(&tas_priv->codec_lock);
407 
408 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: -> %ld\n",
409 		__func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
410 
411 	/* The check of the given value is in tasdevice_amp_putvol. */
412 	ret = tasdevice_amp_putvol(tas_priv, ucontrol, mc);
413 
414 	mutex_unlock(&tas_priv->codec_lock);
415 
416 	return ret;
417 }
418 
419 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
420 	struct snd_ctl_elem_value *ucontrol)
421 {
422 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
423 
424 	mutex_lock(&tas_priv->codec_lock);
425 
426 	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
427 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
428 		__func__, kcontrol->id.name, tas_priv->force_fwload_status);
429 
430 	mutex_unlock(&tas_priv->codec_lock);
431 
432 	return 0;
433 }
434 
435 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
436 	struct snd_ctl_elem_value *ucontrol)
437 {
438 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
439 	bool change, val = (bool)ucontrol->value.integer.value[0];
440 
441 	mutex_lock(&tas_priv->codec_lock);
442 
443 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
444 		__func__, kcontrol->id.name,
445 		tas_priv->force_fwload_status, val);
446 
447 	if (tas_priv->force_fwload_status == val)
448 		change = false;
449 	else {
450 		change = true;
451 		tas_priv->force_fwload_status = val;
452 	}
453 
454 	mutex_unlock(&tas_priv->codec_lock);
455 
456 	return change;
457 }
458 
459 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
460 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
461 		1, 0, 20, 0, tas2781_amp_getvol,
462 		tas2781_amp_putvol, amp_vol_tlv),
463 	ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
464 		tas2781_force_fwload_get, tas2781_force_fwload_put),
465 };
466 
467 static const struct snd_kcontrol_new tas2781_prof_ctrl = {
468 	.name = "Speaker Profile Id",
469 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
470 	.info = tasdevice_info_profile,
471 	.get = tasdevice_get_profile_id,
472 	.put = tasdevice_set_profile_id,
473 };
474 
475 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = {
476 	.name = "Speaker Program Id",
477 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
478 	.info = tasdevice_info_programs,
479 	.get = tasdevice_program_get,
480 	.put = tasdevice_program_put,
481 };
482 
483 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = {
484 	.name = "Speaker Config Id",
485 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
486 	.info = tasdevice_info_config,
487 	.get = tasdevice_config_get,
488 	.put = tasdevice_config_put,
489 };
490 
491 static void tas2563_apply_calib(struct tasdevice_priv *tas_priv)
492 {
493 	int offset = 0;
494 	__be32 data;
495 	int ret;
496 
497 	for (int i = 0; i < tas_priv->ndev; i++) {
498 		for (int j = 0; j < TAS2563_CAL_N; ++j) {
499 			data = cpu_to_be32(
500 				*(uint32_t *)&tas_priv->cali_data.data[offset]);
501 			ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j],
502 				(unsigned char *)&data, TAS2563_CAL_DATA_SIZE);
503 			if (ret)
504 				dev_err(tas_priv->dev,
505 					"Error writing calib regs\n");
506 			offset += TAS2563_CAL_DATA_SIZE;
507 		}
508 	}
509 }
510 
511 static int tas2563_save_calibration(struct tasdevice_priv *tas_priv)
512 {
513 	static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
514 		0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
515 
516 	static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = {
517 		{ L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" },
518 		{ L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" },
519 		{ L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" },
520 		{ L"Power_4", L"R0_4", L"InvR0_4", L"R0_Low_4", L"TLim_4" },
521 	};
522 
523 	unsigned long max_size = TAS2563_CAL_DATA_SIZE;
524 	unsigned int offset = 0;
525 	efi_status_t status;
526 	unsigned int attr;
527 
528 	tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
529 			TAS2563_CAL_ARRAY_SIZE, GFP_KERNEL);
530 	if (!tas_priv->cali_data.data)
531 		return -ENOMEM;
532 
533 	for (int i = 0; i < tas_priv->ndev; ++i) {
534 		for (int j = 0; j < TAS2563_CAL_N; ++j) {
535 			status = efi.get_variable(efi_vars[i][j],
536 				&efi_guid, &attr, &max_size,
537 				&tas_priv->cali_data.data[offset]);
538 			if (status != EFI_SUCCESS ||
539 				max_size != TAS2563_CAL_DATA_SIZE) {
540 				dev_warn(tas_priv->dev,
541 				"Calibration data read failed %ld\n", status);
542 				return -EINVAL;
543 			}
544 			offset += TAS2563_CAL_DATA_SIZE;
545 		}
546 	}
547 
548 	tas_priv->cali_data.total_sz = offset;
549 	tasdevice_apply_calibration(tas_priv);
550 
551 	return 0;
552 }
553 
554 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv)
555 {
556 	static const unsigned char page_array[CALIB_MAX] = {
557 		0x17, 0x18, 0x18, 0x13, 0x18,
558 	};
559 	static const unsigned char rgno_array[CALIB_MAX] = {
560 		0x74, 0x0c, 0x14, 0x70, 0x7c,
561 	};
562 	int offset = 0;
563 	int i, j, rc;
564 	__be32 data;
565 
566 	for (i = 0; i < tas_priv->ndev; i++) {
567 		for (j = 0; j < CALIB_MAX; j++) {
568 			data = cpu_to_be32(
569 				*(uint32_t *)&tas_priv->cali_data.data[offset]);
570 			rc = tasdevice_dev_bulk_write(tas_priv, i,
571 				TASDEVICE_REG(0, page_array[j], rgno_array[j]),
572 				(unsigned char *)&data, 4);
573 			if (rc < 0)
574 				dev_err(tas_priv->dev,
575 					"chn %d calib %d bulk_wr err = %d\n",
576 					i, j, rc);
577 			offset += 4;
578 		}
579 	}
580 }
581 
582 /* Update the calibration data, including speaker impedance, f0, etc, into algo.
583  * Calibrate data is done by manufacturer in the factory. These data are used
584  * by Algo for calculating the speaker temperature, speaker membrane excursion
585  * and f0 in real time during playback.
586  */
587 static int tas2781_save_calibration(struct tasdevice_priv *tas_priv)
588 {
589 	efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d,
590 		0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
591 	static efi_char16_t efi_name[] = L"CALI_DATA";
592 	struct tm *tm = &tas_priv->tm;
593 	unsigned int attr, crc;
594 	unsigned int *tmp_val;
595 	efi_status_t status;
596 
597 	/* Lenovo devices */
598 	if (tas_priv->catlog_id == LENOVO)
599 		efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09,
600 			0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
601 
602 	tas_priv->cali_data.total_sz = 0;
603 	/* Get real size of UEFI variable */
604 	status = efi.get_variable(efi_name, &efi_guid, &attr,
605 		&tas_priv->cali_data.total_sz, tas_priv->cali_data.data);
606 	if (status == EFI_BUFFER_TOO_SMALL) {
607 		/* Allocate data buffer of data_size bytes */
608 		tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
609 			tas_priv->cali_data.total_sz, GFP_KERNEL);
610 		if (!tas_priv->cali_data.data)
611 			return -ENOMEM;
612 		/* Get variable contents into buffer */
613 		status = efi.get_variable(efi_name, &efi_guid, &attr,
614 			&tas_priv->cali_data.total_sz,
615 			tas_priv->cali_data.data);
616 	}
617 	if (status != EFI_SUCCESS)
618 		return -EINVAL;
619 
620 	tmp_val = (unsigned int *)tas_priv->cali_data.data;
621 
622 	crc = crc32(~0, tas_priv->cali_data.data, 84) ^ ~0;
623 	dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n",
624 		crc, tmp_val[21]);
625 
626 	if (crc == tmp_val[21]) {
627 		time64_to_tm(tmp_val[20], 0, tm);
628 		dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n",
629 			tm->tm_year, tm->tm_mon, tm->tm_mday,
630 			tm->tm_hour, tm->tm_min, tm->tm_sec);
631 		tasdevice_apply_calibration(tas_priv);
632 	} else
633 		tas_priv->cali_data.total_sz = 0;
634 
635 	return 0;
636 }
637 
638 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
639 {
640 	struct hda_codec *codec = tas_hda->priv->codec;
641 
642 	snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
643 	snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
644 
645 	for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
646 		snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
647 
648 	snd_ctl_remove(codec->card, tas_hda->prof_ctl);
649 }
650 
651 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
652 {
653 	struct tasdevice_priv *tas_priv = context;
654 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
655 	struct hda_codec *codec = tas_priv->codec;
656 	int i, ret, spk_id;
657 
658 	pm_runtime_get_sync(tas_priv->dev);
659 	mutex_lock(&tas_priv->codec_lock);
660 
661 	ret = tasdevice_rca_parser(tas_priv, fmw);
662 	if (ret)
663 		goto out;
664 
665 	tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv);
666 	ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
667 	if (ret) {
668 		dev_err(tas_priv->dev,
669 			"Failed to add KControl %s = %d\n",
670 			tas2781_prof_ctrl.name, ret);
671 		goto out;
672 	}
673 
674 	for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) {
675 		tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i],
676 			tas_priv);
677 		ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
678 		if (ret) {
679 			dev_err(tas_priv->dev,
680 				"Failed to add KControl %s = %d\n",
681 				tas2781_snd_controls[i].name, ret);
682 			goto out;
683 		}
684 	}
685 
686 	tasdevice_dsp_remove(tas_priv);
687 
688 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
689 	if (tas_priv->speaker_id != NULL) {
690 		// Speaker id need to be checked for ASUS only.
691 		spk_id = gpiod_get_value(tas_priv->speaker_id);
692 		if (spk_id < 0) {
693 			// Speaker id is not valid, use default.
694 			dev_dbg(tas_priv->dev, "Wrong spk_id = %d\n", spk_id);
695 			spk_id = 0;
696 		}
697 		snprintf(tas_priv->coef_binaryname,
698 			  sizeof(tas_priv->coef_binaryname),
699 			  "TAS2XXX%04X%d.bin",
700 			  lower_16_bits(codec->core.subsystem_id),
701 			  spk_id);
702 	} else {
703 		snprintf(tas_priv->coef_binaryname,
704 			  sizeof(tas_priv->coef_binaryname),
705 			  "TAS2XXX%04X.bin",
706 			  lower_16_bits(codec->core.subsystem_id));
707 	}
708 	ret = tasdevice_dsp_parser(tas_priv);
709 	if (ret) {
710 		dev_err(tas_priv->dev, "dspfw load %s error\n",
711 			tas_priv->coef_binaryname);
712 		tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
713 		goto out;
714 	}
715 
716 	tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl,
717 		tas_priv);
718 	ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
719 	if (ret) {
720 		dev_err(tas_priv->dev,
721 			"Failed to add KControl %s = %d\n",
722 			tas2781_dsp_prog_ctrl.name, ret);
723 		goto out;
724 	}
725 
726 	tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl,
727 		tas_priv);
728 	ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
729 	if (ret) {
730 		dev_err(tas_priv->dev,
731 			"Failed to add KControl %s = %d\n",
732 			tas2781_dsp_conf_ctrl.name, ret);
733 		goto out;
734 	}
735 
736 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
737 	tasdevice_prmg_load(tas_priv, 0);
738 	if (tas_priv->fmw->nr_programs > 0)
739 		tas_priv->cur_prog = 0;
740 	if (tas_priv->fmw->nr_configurations > 0)
741 		tas_priv->cur_conf = 0;
742 
743 	/* If calibrated data occurs error, dsp will still works with default
744 	 * calibrated data inside algo.
745 	 */
746 	tasdevice_save_calibration(tas_priv);
747 
748 	tasdevice_tuning_switch(tas_hda->priv, 0);
749 	tas_hda->priv->playback_started = true;
750 
751 out:
752 	mutex_unlock(&tas_hda->priv->codec_lock);
753 	if (fmw)
754 		release_firmware(fmw);
755 	pm_runtime_mark_last_busy(tas_hda->dev);
756 	pm_runtime_put_autosuspend(tas_hda->dev);
757 }
758 
759 static int tas2781_hda_bind(struct device *dev, struct device *master,
760 	void *master_data)
761 {
762 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
763 	struct hda_component_parent *parent = master_data;
764 	struct hda_component *comp;
765 	struct hda_codec *codec;
766 	unsigned int subid;
767 	int ret;
768 
769 	comp = hda_component_from_index(parent, tas_hda->priv->index);
770 	if (!comp)
771 		return -EINVAL;
772 
773 	if (comp->dev)
774 		return -EBUSY;
775 
776 	codec = parent->codec;
777 	subid = codec->core.subsystem_id >> 16;
778 
779 	switch (subid) {
780 	case 0x17aa:
781 		tas_hda->priv->catlog_id = LENOVO;
782 		break;
783 	default:
784 		tas_hda->priv->catlog_id = OTHERS;
785 		break;
786 	}
787 
788 	pm_runtime_get_sync(dev);
789 
790 	comp->dev = dev;
791 
792 	strscpy(comp->name, dev_name(dev), sizeof(comp->name));
793 
794 	ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
795 	if (!ret)
796 		comp->playback_hook = tas2781_hda_playback_hook;
797 
798 	pm_runtime_mark_last_busy(dev);
799 	pm_runtime_put_autosuspend(dev);
800 
801 	return ret;
802 }
803 
804 static void tas2781_hda_unbind(struct device *dev,
805 	struct device *master, void *master_data)
806 {
807 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
808 	struct hda_component_parent *parent = master_data;
809 	struct hda_component *comp;
810 
811 	comp = hda_component_from_index(parent, tas_hda->priv->index);
812 	if (comp && (comp->dev == dev)) {
813 		comp->dev = NULL;
814 		memset(comp->name, 0, sizeof(comp->name));
815 		comp->playback_hook = NULL;
816 	}
817 
818 	tas2781_hda_remove_controls(tas_hda);
819 
820 	tasdevice_config_info_remove(tas_hda->priv);
821 	tasdevice_dsp_remove(tas_hda->priv);
822 
823 	tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
824 }
825 
826 static const struct component_ops tas2781_hda_comp_ops = {
827 	.bind = tas2781_hda_bind,
828 	.unbind = tas2781_hda_unbind,
829 };
830 
831 static void tas2781_hda_remove(struct device *dev)
832 {
833 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
834 
835 	component_del(tas_hda->dev, &tas2781_hda_comp_ops);
836 
837 	pm_runtime_get_sync(tas_hda->dev);
838 	pm_runtime_disable(tas_hda->dev);
839 
840 	pm_runtime_put_noidle(tas_hda->dev);
841 
842 	tasdevice_remove(tas_hda->priv);
843 }
844 
845 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
846 {
847 	struct tas2781_hda *tas_hda;
848 	const char *device_name;
849 	int ret;
850 
851 
852 	tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
853 	if (!tas_hda)
854 		return -ENOMEM;
855 
856 	dev_set_drvdata(&clt->dev, tas_hda);
857 	tas_hda->dev = &clt->dev;
858 
859 	tas_hda->priv = tasdevice_kzalloc(clt);
860 	if (!tas_hda->priv)
861 		return -ENOMEM;
862 
863 	if (strstr(dev_name(&clt->dev), "TIAS2781")) {
864 		device_name = "TIAS2781";
865 		tas_hda->priv->save_calibration = tas2781_save_calibration;
866 		tas_hda->priv->apply_calibration = tas2781_apply_calib;
867 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
868 	} else if (strstr(dev_name(&clt->dev), "INT8866")) {
869 		device_name = "INT8866";
870 		tas_hda->priv->save_calibration = tas2563_save_calibration;
871 		tas_hda->priv->apply_calibration = tas2563_apply_calib;
872 		tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
873 	} else
874 		return -ENODEV;
875 
876 	tas_hda->priv->irq = clt->irq;
877 	ret = tas2781_read_acpi(tas_hda->priv, device_name);
878 	if (ret)
879 		return dev_err_probe(tas_hda->dev, ret,
880 			"Platform not supported\n");
881 
882 	ret = tasdevice_init(tas_hda->priv);
883 	if (ret)
884 		goto err;
885 
886 	pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
887 	pm_runtime_use_autosuspend(tas_hda->dev);
888 	pm_runtime_mark_last_busy(tas_hda->dev);
889 	pm_runtime_set_active(tas_hda->dev);
890 	pm_runtime_enable(tas_hda->dev);
891 
892 	tasdevice_reset(tas_hda->priv);
893 
894 	ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
895 	if (ret) {
896 		dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
897 		pm_runtime_disable(tas_hda->dev);
898 	}
899 
900 err:
901 	if (ret)
902 		tas2781_hda_remove(&clt->dev);
903 	return ret;
904 }
905 
906 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
907 {
908 	tas2781_hda_remove(&clt->dev);
909 }
910 
911 static int tas2781_runtime_suspend(struct device *dev)
912 {
913 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
914 
915 	dev_dbg(tas_hda->dev, "Runtime Suspend\n");
916 
917 	mutex_lock(&tas_hda->priv->codec_lock);
918 
919 	/* The driver powers up the amplifiers at module load time.
920 	 * Stop the playback if it's unused.
921 	 */
922 	if (tas_hda->priv->playback_started) {
923 		tasdevice_tuning_switch(tas_hda->priv, 1);
924 		tas_hda->priv->playback_started = false;
925 	}
926 
927 	mutex_unlock(&tas_hda->priv->codec_lock);
928 
929 	return 0;
930 }
931 
932 static int tas2781_runtime_resume(struct device *dev)
933 {
934 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
935 
936 	dev_dbg(tas_hda->dev, "Runtime Resume\n");
937 
938 	mutex_lock(&tas_hda->priv->codec_lock);
939 
940 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
941 
942 	/* If calibrated data occurs error, dsp will still works with default
943 	 * calibrated data inside algo.
944 	 */
945 	tasdevice_apply_calibration(tas_hda->priv);
946 
947 	mutex_unlock(&tas_hda->priv->codec_lock);
948 
949 	return 0;
950 }
951 
952 static int tas2781_system_suspend(struct device *dev)
953 {
954 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
955 
956 	dev_dbg(tas_hda->priv->dev, "System Suspend\n");
957 
958 	mutex_lock(&tas_hda->priv->codec_lock);
959 
960 	/* Shutdown chip before system suspend */
961 	if (tas_hda->priv->playback_started)
962 		tasdevice_tuning_switch(tas_hda->priv, 1);
963 
964 	mutex_unlock(&tas_hda->priv->codec_lock);
965 
966 	/*
967 	 * Reset GPIO may be shared, so cannot reset here.
968 	 * However beyond this point, amps may be powered down.
969 	 */
970 	return 0;
971 }
972 
973 static int tas2781_system_resume(struct device *dev)
974 {
975 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
976 	int i;
977 
978 	dev_dbg(tas_hda->priv->dev, "System Resume\n");
979 
980 	mutex_lock(&tas_hda->priv->codec_lock);
981 
982 	for (i = 0; i < tas_hda->priv->ndev; i++) {
983 		tas_hda->priv->tasdevice[i].cur_book = -1;
984 		tas_hda->priv->tasdevice[i].cur_prog = -1;
985 		tas_hda->priv->tasdevice[i].cur_conf = -1;
986 	}
987 	tasdevice_reset(tas_hda->priv);
988 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
989 
990 	/* If calibrated data occurs error, dsp will still work with default
991 	 * calibrated data inside algo.
992 	 */
993 	tasdevice_apply_calibration(tas_hda->priv);
994 
995 	if (tas_hda->priv->playback_started)
996 		tasdevice_tuning_switch(tas_hda->priv, 0);
997 
998 	mutex_unlock(&tas_hda->priv->codec_lock);
999 
1000 	return 0;
1001 }
1002 
1003 static const struct dev_pm_ops tas2781_hda_pm_ops = {
1004 	RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
1005 	SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
1006 };
1007 
1008 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
1009 	{ "tas2781-hda" },
1010 	{}
1011 };
1012 
1013 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
1014 	{"TIAS2781", 0 },
1015 	{"INT8866", 0 },
1016 	{}
1017 };
1018 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
1019 
1020 static struct i2c_driver tas2781_hda_i2c_driver = {
1021 	.driver = {
1022 		.name		= "tas2781-hda",
1023 		.acpi_match_table = tas2781_acpi_hda_match,
1024 		.pm		= &tas2781_hda_pm_ops,
1025 	},
1026 	.id_table	= tas2781_hda_i2c_id,
1027 	.probe		= tas2781_hda_i2c_probe,
1028 	.remove		= tas2781_hda_i2c_remove,
1029 };
1030 module_i2c_driver(tas2781_hda_i2c_driver);
1031 
1032 MODULE_DESCRIPTION("TAS2781 HDA Driver");
1033 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
1034 MODULE_LICENSE("GPL");
1035 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);
1036