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