xref: /linux/sound/hda/codecs/side-codecs/tas2781_hda_i2c.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA I2C driver
4 //
5 // Copyright 2023 - 2025 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/tas2781-comlib-i2c.h>
26 #include <sound/tlv.h>
27 #include <sound/tas2770-tlv.h>
28 #include <sound/tas2781-tlv.h>
29 #include <sound/tas5825-tlv.h>
30 
31 #include "hda_local.h"
32 #include "hda_auto_parser.h"
33 #include "hda_component.h"
34 #include "hda_jack.h"
35 #include "../generic.h"
36 #include "tas2781_hda.h"
37 
38 #define TAS2563_CAL_VAR_NAME_MAX	16
39 #define TAS2563_CAL_ARRAY_SIZE		80
40 #define TAS2563_CAL_DATA_SIZE		4
41 #define TAS2563_MAX_CHANNELS		4
42 #define TAS2563_CAL_CH_SIZE		20
43 
44 #define TAS2563_CAL_R0_LOW		TASDEVICE_REG(0, 0x0f, 0x48)
45 #define TAS2563_CAL_POWER		TASDEVICE_REG(0, 0x0d, 0x3c)
46 #define TAS2563_CAL_INVR0		TASDEVICE_REG(0, 0x0f, 0x40)
47 #define TAS2563_CAL_TLIM		TASDEVICE_REG(0, 0x10, 0x14)
48 #define TAS2563_CAL_R0			TASDEVICE_REG(0, 0x0f, 0x34)
49 
50 enum device_chip_id {
51 	HDA_TAS2563,
52 	HDA_TAS2770,
53 	HDA_TAS2781,
54 	HDA_TAS5825,
55 	HDA_OTHERS
56 };
57 
58 struct tas2781_hda_i2c_priv {
59 	struct snd_kcontrol *snd_ctls[2];
60 	int (*save_calibration)(struct tas2781_hda *h);
61 
62 	int hda_chip_id;
63 };
64 
65 static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
66 {
67 	struct tasdevice_priv *tas_priv = data;
68 	struct acpi_resource_i2c_serialbus *sb;
69 
70 	if (i2c_acpi_get_i2c_resource(ares, &sb)) {
71 		if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS &&
72 			sb->slave_address != tas_priv->global_addr) {
73 			tas_priv->tasdevice[tas_priv->ndev].dev_addr =
74 				(unsigned int)sb->slave_address;
75 			tas_priv->ndev++;
76 		}
77 	}
78 	return 1;
79 }
80 
81 static const struct acpi_gpio_params speakerid_gpios = { 0, 0, false };
82 
83 static const struct acpi_gpio_mapping tas2781_speaker_id_gpios[] = {
84 	{ "speakerid-gpios", &speakerid_gpios, 1 },
85 	{ }
86 };
87 
88 static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
89 {
90 	struct acpi_device *adev;
91 	struct device *physdev;
92 	LIST_HEAD(resources);
93 	const char *sub;
94 	uint32_t subid;
95 	int ret;
96 
97 	adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
98 	if (!adev) {
99 		dev_err(p->dev,
100 			"Failed to find an ACPI device for %s\n", hid);
101 		return -ENODEV;
102 	}
103 
104 	physdev = get_device(acpi_get_first_physical_node(adev));
105 	ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
106 	if (ret < 0) {
107 		dev_err(p->dev, "Failed to get ACPI resource.\n");
108 		goto err;
109 	}
110 	sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
111 	if (IS_ERR(sub)) {
112 		/* No subsys id in older tas2563 projects. */
113 		if (!strncmp(hid, "INT8866", sizeof("INT8866")))
114 			goto end_2563;
115 		dev_err(p->dev, "Failed to get SUBSYS ID.\n");
116 		ret = PTR_ERR(sub);
117 		goto err;
118 	}
119 	/* Speaker id was needed for ASUS projects. */
120 	ret = kstrtou32(sub, 16, &subid);
121 	if (!ret && upper_16_bits(subid) == PCI_VENDOR_ID_ASUSTEK) {
122 		ret = devm_acpi_dev_add_driver_gpios(p->dev,
123 			tas2781_speaker_id_gpios);
124 		if (ret < 0)
125 			dev_err(p->dev, "Failed to add driver gpio %d.\n",
126 				ret);
127 		p->speaker_id = devm_gpiod_get(p->dev, "speakerid", GPIOD_IN);
128 		if (IS_ERR(p->speaker_id)) {
129 			dev_err(p->dev, "Failed to get Speaker id.\n");
130 			ret = PTR_ERR(p->speaker_id);
131 			goto err;
132 		}
133 	} else {
134 		p->speaker_id = NULL;
135 	}
136 
137 end_2563:
138 	acpi_dev_free_resource_list(&resources);
139 	strscpy(p->dev_name, hid, sizeof(p->dev_name));
140 	put_device(physdev);
141 	acpi_dev_put(adev);
142 
143 	return 0;
144 
145 err:
146 	dev_err(p->dev, "read acpi error, ret: %d\n", ret);
147 	put_device(physdev);
148 	acpi_dev_put(adev);
149 
150 	return ret;
151 }
152 
153 static void tas2781_hda_playback_hook(struct device *dev, int action)
154 {
155 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
156 
157 	dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
158 	switch (action) {
159 	case HDA_GEN_PCM_ACT_OPEN:
160 		pm_runtime_get_sync(dev);
161 		scoped_guard(mutex, &tas_hda->priv->codec_lock) {
162 			tasdevice_tuning_switch(tas_hda->priv, 0);
163 			tas_hda->priv->playback_started = true;
164 		}
165 		break;
166 	case HDA_GEN_PCM_ACT_CLOSE:
167 		scoped_guard(mutex, &tas_hda->priv->codec_lock) {
168 			tasdevice_tuning_switch(tas_hda->priv, 1);
169 			tas_hda->priv->playback_started = false;
170 		}
171 
172 		pm_runtime_put_autosuspend(dev);
173 		break;
174 	default:
175 		break;
176 	}
177 }
178 
179 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
180 	struct snd_ctl_elem_value *ucontrol)
181 {
182 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
183 	struct soc_mixer_control *mc =
184 		(struct soc_mixer_control *)kcontrol->private_value;
185 	int ret;
186 
187 	guard(mutex)(&tas_priv->codec_lock);
188 
189 	ret = tasdevice_amp_getvol(tas_priv, ucontrol, mc);
190 
191 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %ld\n",
192 		__func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
193 
194 	return ret;
195 }
196 
197 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
198 	struct snd_ctl_elem_value *ucontrol)
199 {
200 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
201 	struct soc_mixer_control *mc =
202 		(struct soc_mixer_control *)kcontrol->private_value;
203 
204 	guard(mutex)(&tas_priv->codec_lock);
205 
206 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: -> %ld\n",
207 		__func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
208 
209 	/* The check of the given value is in tasdevice_amp_putvol. */
210 	return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
211 }
212 
213 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
214 	struct snd_ctl_elem_value *ucontrol)
215 {
216 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
217 
218 	guard(mutex)(&tas_priv->codec_lock);
219 
220 	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
221 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
222 		__func__, kcontrol->id.name, tas_priv->force_fwload_status);
223 
224 	return 0;
225 }
226 
227 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
228 	struct snd_ctl_elem_value *ucontrol)
229 {
230 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
231 	bool change, val = (bool)ucontrol->value.integer.value[0];
232 
233 	guard(mutex)(&tas_priv->codec_lock);
234 
235 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
236 		__func__, kcontrol->id.name,
237 		tas_priv->force_fwload_status, val);
238 
239 	if (tas_priv->force_fwload_status == val)
240 		change = false;
241 	else {
242 		change = true;
243 		tas_priv->force_fwload_status = val;
244 	}
245 
246 	return change;
247 }
248 
249 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
250 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2770_AMP_LEVEL,
251 		0, 0, 20, 0, tas2781_amp_getvol,
252 		tas2781_amp_putvol, tas2770_amp_tlv),
253 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2770_DVC_LEVEL,
254 		0, 0, 200, 1, tas2781_amp_getvol,
255 		tas2781_amp_putvol, tas2770_dvc_tlv),
256 };
257 
258 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
259 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2781_AMP_LEVEL,
260 		1, 0, 20, 0, tas2781_amp_getvol,
261 		tas2781_amp_putvol, tas2781_amp_tlv),
262 	ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
263 		tas2781_force_fwload_get, tas2781_force_fwload_put),
264 };
265 
266 static const struct snd_kcontrol_new tas5825_snd_controls[] = {
267 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS5825_AMP_LEVEL,
268 		0, 0, 31, 1, tas2781_amp_getvol,
269 		tas2781_amp_putvol, tas5825_amp_tlv),
270 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS5825_DVC_LEVEL,
271 		0, 0, 254, 1, tas2781_amp_getvol,
272 		tas2781_amp_putvol, tas5825_dvc_tlv),
273 	ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
274 		tas2781_force_fwload_get, tas2781_force_fwload_put),
275 };
276 
277 static const struct snd_kcontrol_new tasdevice_prof_ctrl = {
278 	.name = "Speaker Profile Id",
279 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
280 	.info = tasdevice_info_profile,
281 	.get = tasdevice_get_profile_id,
282 	.put = tasdevice_set_profile_id,
283 };
284 
285 static const struct snd_kcontrol_new tasdevice_dsp_prog_ctrl = {
286 	.name = "Speaker Program Id",
287 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
288 	.info = tasdevice_info_programs,
289 	.get = tasdevice_program_get,
290 	.put = tasdevice_program_put,
291 };
292 
293 static const struct snd_kcontrol_new tasdevice_dsp_conf_ctrl = {
294 	.name = "Speaker Config Id",
295 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
296 	.info = tasdevice_info_config,
297 	.get = tasdevice_config_get,
298 	.put = tasdevice_config_put,
299 };
300 
301 static int tas2563_save_calibration(struct tas2781_hda *h)
302 {
303 	efi_guid_t efi_guid = tasdev_fct_efi_guid[LENOVO];
304 	char *vars[TASDEV_CALIB_N] = {
305 		"R0_%d", "R0_Low_%d", "InvR0_%d", "Power_%d", "TLim_%d"
306 	};
307 	efi_char16_t efi_name[TAS2563_CAL_VAR_NAME_MAX];
308 	unsigned long max_size = TAS2563_CAL_DATA_SIZE;
309 	unsigned char var8[TAS2563_CAL_VAR_NAME_MAX];
310 	struct tasdevice_priv *p = h->priv;
311 	struct calidata *cd = &p->cali_data;
312 	struct cali_reg *r = &cd->cali_reg_array;
313 	unsigned int offset = 0;
314 	unsigned char *data;
315 	__be32 bedata;
316 	efi_status_t status;
317 	unsigned int attr;
318 	int ret, i, j, k;
319 
320 	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
321 		dev_err(p->dev, "%s: NO EFI FOUND!\n", __func__);
322 		return -EINVAL;
323 	}
324 
325 	cd->cali_dat_sz_per_dev = TAS2563_CAL_DATA_SIZE * TASDEV_CALIB_N;
326 
327 	/* extra byte for each device is the device number */
328 	cd->total_sz = (cd->cali_dat_sz_per_dev + 1) * p->ndev;
329 	data = cd->data = devm_kzalloc(p->dev, cd->total_sz,
330 		GFP_KERNEL);
331 	if (!data)
332 		return -ENOMEM;
333 
334 	for (i = 0; i < p->ndev; ++i) {
335 		data[offset] = i;
336 		offset++;
337 		for (j = 0; j < TASDEV_CALIB_N; ++j) {
338 			/* EFI name for calibration started with 1, not 0 */
339 			ret = snprintf(var8, sizeof(var8), vars[j], i + 1);
340 			if (ret < 0 || ret >= sizeof(var8) - 1) {
341 				dev_err(p->dev, "%s: Read %s failed\n",
342 					__func__, var8);
343 				return -EINVAL;
344 			}
345 			/*
346 			 * Our variable names are ASCII by construction, but
347 			 * EFI names are wide chars.  Convert and zero-pad.
348 			 */
349 			memset(efi_name, 0, sizeof(efi_name));
350 			for (k = 0; k < sizeof(var8) && var8[k]; k++)
351 				efi_name[k] = var8[k];
352 			status = efi.get_variable(efi_name,
353 				&efi_guid, &attr, &max_size,
354 				&data[offset]);
355 			if (status != EFI_SUCCESS ||
356 				max_size != TAS2563_CAL_DATA_SIZE) {
357 				dev_warn(p->dev,
358 					"Dev %d: Caldat[%d] read failed %ld\n",
359 					i, j, status);
360 				return -EINVAL;
361 			}
362 			bedata = cpu_to_be32(*(uint32_t *)&data[offset]);
363 			memcpy(&data[offset], &bedata, sizeof(bedata));
364 			offset += TAS2563_CAL_DATA_SIZE;
365 		}
366 	}
367 
368 	if (cd->total_sz != offset) {
369 		dev_err(p->dev, "%s: tot_size(%lu) and offset(%u) mismatch\n",
370 			__func__, cd->total_sz, offset);
371 		return -EINVAL;
372 	}
373 
374 	r->r0_reg = TAS2563_CAL_R0;
375 	r->invr0_reg = TAS2563_CAL_INVR0;
376 	r->r0_low_reg = TAS2563_CAL_R0_LOW;
377 	r->pow_reg = TAS2563_CAL_POWER;
378 	r->tlimit_reg = TAS2563_CAL_TLIM;
379 
380 	/*
381 	 * TAS2781_FMWLIB supports two solutions of calibrated data. One is
382 	 * from the driver itself: driver reads the calibrated files directly
383 	 * during probe; The other from user space: during init of audio hal,
384 	 * the audio hal will pass the calibrated data via kcontrol interface.
385 	 * Driver will store this data in "struct calidata" for use. For hda
386 	 * device, calibrated data are usunally saved into UEFI. So Hda side
387 	 * codec driver use the mixture of these two solutions, driver reads
388 	 * the data from UEFI, then store this data in "struct calidata" for
389 	 * use.
390 	 */
391 	p->is_user_space_calidata = true;
392 
393 	return 0;
394 }
395 
396 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
397 {
398 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
399 	struct hda_codec *codec = tas_hda->priv->codec;
400 
401 	snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
402 	snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
403 
404 	for (int i = ARRAY_SIZE(hda_priv->snd_ctls) - 1; i >= 0; i--)
405 		snd_ctl_remove(codec->card, hda_priv->snd_ctls[i]);
406 
407 	snd_ctl_remove(codec->card, tas_hda->prof_ctl);
408 }
409 
410 static void tasdev_add_kcontrols(struct tasdevice_priv *tas_priv,
411 	struct snd_kcontrol **ctls, struct hda_codec *codec,
412 	const struct snd_kcontrol_new *tas_snd_ctrls, int num_ctls)
413 {
414 	int i, ret;
415 
416 	for (i = 0; i < num_ctls; i++) {
417 		ctls[i] = snd_ctl_new1(
418 			&tas_snd_ctrls[i], tas_priv);
419 		ret = snd_ctl_add(codec->card, ctls[i]);
420 		if (ret) {
421 			dev_err(tas_priv->dev,
422 				"Failed to add KControl %s = %d\n",
423 				tas_snd_ctrls[i].name, ret);
424 			break;
425 		}
426 	}
427 }
428 
429 static void tasdevice_dspfw_init(void *context)
430 {
431 	struct tasdevice_priv *tas_priv = context;
432 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
433 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
434 	struct hda_codec *codec = tas_priv->codec;
435 	int ret, spk_id;
436 
437 	tasdevice_dsp_remove(tas_priv);
438 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
439 	if (tas_priv->speaker_id != NULL) {
440 		// Speaker id need to be checked for ASUS only.
441 		spk_id = gpiod_get_value(tas_priv->speaker_id);
442 		if (spk_id < 0) {
443 			// Speaker id is not valid, use default.
444 			dev_dbg(tas_priv->dev, "Wrong spk_id = %d\n", spk_id);
445 			spk_id = 0;
446 		}
447 		snprintf(tas_priv->coef_binaryname,
448 			  sizeof(tas_priv->coef_binaryname),
449 			  "TAS2XXX%04X%d.bin",
450 			  lower_16_bits(codec->core.subsystem_id),
451 			  spk_id);
452 	} else {
453 		snprintf(tas_priv->coef_binaryname,
454 			  sizeof(tas_priv->coef_binaryname),
455 			  "TAS2XXX%04X.bin",
456 			  lower_16_bits(codec->core.subsystem_id));
457 	}
458 	ret = tasdevice_dsp_parser(tas_priv);
459 	if (ret) {
460 		dev_err(tas_priv->dev, "dspfw load %s error\n",
461 			tas_priv->coef_binaryname);
462 		tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
463 		return;
464 	}
465 	tasdev_add_kcontrols(tas_priv, &tas_hda->dsp_prog_ctl, codec,
466 			     &tasdevice_dsp_prog_ctrl, 1);
467 	tasdev_add_kcontrols(tas_priv, &tas_hda->dsp_conf_ctl, codec,
468 			     &tasdevice_dsp_conf_ctrl, 1);
469 
470 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
471 	tasdevice_prmg_load(tas_priv, 0);
472 	if (tas_priv->fmw->nr_programs > 0)
473 		tas_priv->cur_prog = 0;
474 	if (tas_priv->fmw->nr_configurations > 0)
475 		tas_priv->cur_conf = 0;
476 
477 	/* If calibrated data occurs error, dsp will still works with default
478 	 * calibrated data inside algo.
479 	 */
480 	hda_priv->save_calibration(tas_hda);
481 }
482 
483 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
484 {
485 	struct tasdevice_priv *tas_priv = context;
486 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
487 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
488 	struct hda_codec *codec = tas_priv->codec;
489 	int ret;
490 
491 	pm_runtime_get_sync(tas_priv->dev);
492 	mutex_lock(&tas_priv->codec_lock);
493 
494 	ret = tasdevice_rca_parser(tas_priv, fmw);
495 	if (ret)
496 		goto out;
497 
498 	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
499 	tasdev_add_kcontrols(tas_priv, &tas_hda->prof_ctl, codec,
500 		&tasdevice_prof_ctrl, 1);
501 
502 	switch (hda_priv->hda_chip_id) {
503 	case HDA_TAS2770:
504 		tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
505 				     &tas2770_snd_controls[0],
506 				     ARRAY_SIZE(tas2770_snd_controls));
507 		break;
508 	case HDA_TAS2781:
509 		tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
510 				     &tas2781_snd_controls[0],
511 				     ARRAY_SIZE(tas2781_snd_controls));
512 		tasdevice_dspfw_init(context);
513 		break;
514 	case HDA_TAS5825:
515 		tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
516 				     &tas5825_snd_controls[0],
517 				     ARRAY_SIZE(tas5825_snd_controls));
518 		tasdevice_dspfw_init(context);
519 		break;
520 	case HDA_TAS2563:
521 		tasdevice_dspfw_init(context);
522 		break;
523 	default:
524 		break;
525 	}
526 
527 out:
528 	mutex_unlock(&tas_hda->priv->codec_lock);
529 	release_firmware(fmw);
530 	pm_runtime_put_autosuspend(tas_hda->dev);
531 }
532 
533 static int tas2781_hda_bind(struct device *dev, struct device *master,
534 	void *master_data)
535 {
536 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
537 	struct hda_component_parent *parent = master_data;
538 	struct hda_component *comp;
539 	struct hda_codec *codec;
540 	unsigned int subid;
541 	int ret;
542 
543 	comp = hda_component_from_index(parent, tas_hda->priv->index);
544 	if (!comp)
545 		return -EINVAL;
546 
547 	if (comp->dev)
548 		return -EBUSY;
549 
550 	codec = parent->codec;
551 	subid = codec->core.subsystem_id >> 16;
552 
553 	switch (subid) {
554 	case 0x1028:
555 		tas_hda->catlog_id = DELL;
556 		break;
557 	default:
558 		tas_hda->catlog_id = LENOVO;
559 		break;
560 	}
561 
562 	pm_runtime_get_sync(dev);
563 
564 	comp->dev = dev;
565 
566 	strscpy(comp->name, dev_name(dev), sizeof(comp->name));
567 
568 	ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
569 	if (!ret)
570 		comp->playback_hook = tas2781_hda_playback_hook;
571 
572 	pm_runtime_put_autosuspend(dev);
573 
574 	return ret;
575 }
576 
577 static void tas2781_hda_unbind(struct device *dev,
578 	struct device *master, void *master_data)
579 {
580 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
581 	struct hda_component_parent *parent = master_data;
582 	struct hda_component *comp;
583 
584 	comp = hda_component_from_index(parent, tas_hda->priv->index);
585 	if (comp && (comp->dev == dev)) {
586 		comp->dev = NULL;
587 		memset(comp->name, 0, sizeof(comp->name));
588 		comp->playback_hook = NULL;
589 	}
590 
591 	tas2781_hda_remove_controls(tas_hda);
592 
593 	tasdevice_config_info_remove(tas_hda->priv);
594 	tasdevice_dsp_remove(tas_hda->priv);
595 
596 	tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
597 }
598 
599 static const struct component_ops tas2781_hda_comp_ops = {
600 	.bind = tas2781_hda_bind,
601 	.unbind = tas2781_hda_unbind,
602 };
603 
604 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
605 {
606 	struct tas2781_hda_i2c_priv *hda_priv;
607 	struct tas2781_hda *tas_hda;
608 	const char *device_name;
609 	int ret;
610 
611 	tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
612 	if (!tas_hda)
613 		return -ENOMEM;
614 
615 	hda_priv = devm_kzalloc(&clt->dev, sizeof(*hda_priv), GFP_KERNEL);
616 	if (!hda_priv)
617 		return -ENOMEM;
618 
619 	tas_hda->hda_priv = hda_priv;
620 
621 	dev_set_drvdata(&clt->dev, tas_hda);
622 	tas_hda->dev = &clt->dev;
623 
624 	tas_hda->priv = tasdevice_kzalloc(clt);
625 	if (!tas_hda->priv)
626 		return -ENOMEM;
627 
628 	if (strstr(dev_name(&clt->dev), "TIAS2781")) {
629 		/*
630 		 * TAS2781, integrated on-chip DSP with
631 		 * global I2C address supported.
632 		 */
633 		device_name = "TIAS2781";
634 		hda_priv->hda_chip_id = HDA_TAS2781;
635 		hda_priv->save_calibration = tas2781_save_calibration;
636 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
637 	} else if (strstarts(dev_name(&clt->dev), "i2c-TXNW2770")) {
638 		/*
639 		 * TAS2770, has no on-chip DSP, so no calibration data
640 		 * required; has no global I2C address supported.
641 		 */
642 		device_name = "TXNW2770";
643 		hda_priv->hda_chip_id = HDA_TAS2770;
644 	} else if (strstarts(dev_name(&clt->dev),
645 			     "i2c-TXNW2781:00-tas2781-hda.0")) {
646 		device_name = "TXNW2781";
647 		hda_priv->hda_chip_id = HDA_TAS2781;
648 		hda_priv->save_calibration = tas2781_save_calibration;
649 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
650 	} else if (strstr(dev_name(&clt->dev), "INT8866")) {
651 		/*
652 		 * TAS2563, integrated on-chip DSP with
653 		 * global I2C address supported.
654 		 */
655 		device_name = "INT8866";
656 		hda_priv->hda_chip_id = HDA_TAS2563;
657 		hda_priv->save_calibration = tas2563_save_calibration;
658 		tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
659 	} else if (strstarts(dev_name(&clt->dev), "i2c-TXNW5825")) {
660 		/*
661 		 * TAS5825, integrated on-chip DSP without
662 		 * global I2C address and calibration supported.
663 		 */
664 		device_name = "TXNW5825";
665 		hda_priv->hda_chip_id = HDA_TAS5825;
666 	} else {
667 		return -ENODEV;
668 	}
669 
670 	tas_hda->priv->irq = clt->irq;
671 	ret = tas2781_read_acpi(tas_hda->priv, device_name);
672 	if (ret)
673 		return dev_err_probe(tas_hda->dev, ret,
674 			"Platform not supported\n");
675 
676 	ret = tasdevice_init(tas_hda->priv);
677 	if (ret)
678 		goto err;
679 
680 	pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
681 	pm_runtime_use_autosuspend(tas_hda->dev);
682 	pm_runtime_mark_last_busy(tas_hda->dev);
683 	pm_runtime_set_active(tas_hda->dev);
684 	pm_runtime_enable(tas_hda->dev);
685 
686 	tasdevice_reset(tas_hda->priv);
687 
688 	ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
689 	if (ret) {
690 		dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
691 		pm_runtime_disable(tas_hda->dev);
692 	}
693 
694 err:
695 	if (ret)
696 		tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
697 	return ret;
698 }
699 
700 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
701 {
702 	tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
703 }
704 
705 static int tas2781_runtime_suspend(struct device *dev)
706 {
707 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
708 
709 	dev_dbg(tas_hda->dev, "Runtime Suspend\n");
710 
711 	guard(mutex)(&tas_hda->priv->codec_lock);
712 
713 	/* The driver powers up the amplifiers at module load time.
714 	 * Stop the playback if it's unused.
715 	 */
716 	if (tas_hda->priv->playback_started) {
717 		tasdevice_tuning_switch(tas_hda->priv, 1);
718 		tas_hda->priv->playback_started = false;
719 	}
720 
721 	return 0;
722 }
723 
724 static int tas2781_runtime_resume(struct device *dev)
725 {
726 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
727 
728 	dev_dbg(tas_hda->dev, "Runtime Resume\n");
729 
730 	guard(mutex)(&tas_hda->priv->codec_lock);
731 
732 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
733 
734 	return 0;
735 }
736 
737 static int tas2781_system_suspend(struct device *dev)
738 {
739 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
740 
741 	dev_dbg(tas_hda->priv->dev, "System Suspend\n");
742 
743 	guard(mutex)(&tas_hda->priv->codec_lock);
744 
745 	/* Shutdown chip before system suspend */
746 	if (tas_hda->priv->playback_started)
747 		tasdevice_tuning_switch(tas_hda->priv, 1);
748 
749 	/*
750 	 * Reset GPIO may be shared, so cannot reset here.
751 	 * However beyond this point, amps may be powered down.
752 	 */
753 	return 0;
754 }
755 
756 static int tas2781_system_resume(struct device *dev)
757 {
758 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
759 	int i;
760 
761 	dev_dbg(tas_hda->priv->dev, "System Resume\n");
762 
763 	guard(mutex)(&tas_hda->priv->codec_lock);
764 
765 	for (i = 0; i < tas_hda->priv->ndev; i++) {
766 		tas_hda->priv->tasdevice[i].cur_book = -1;
767 		tas_hda->priv->tasdevice[i].cur_prog = -1;
768 		tas_hda->priv->tasdevice[i].cur_conf = -1;
769 	}
770 	tasdevice_reset(tas_hda->priv);
771 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
772 
773 	if (tas_hda->priv->playback_started)
774 		tasdevice_tuning_switch(tas_hda->priv, 0);
775 
776 	return 0;
777 }
778 
779 static const struct dev_pm_ops tas2781_hda_pm_ops = {
780 	RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
781 	SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
782 };
783 
784 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
785 	{ "tas2781-hda" },
786 	{}
787 };
788 
789 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
790 	{"INT8866", 0 },
791 	{"TIAS2781", 0 },
792 	{"TXNW2770", 0 },
793 	{"TXNW2781", 0 },
794 	{"TXNW5825", 0 },
795 	{}
796 };
797 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
798 
799 static struct i2c_driver tas2781_hda_i2c_driver = {
800 	.driver = {
801 		.name		= "tas2781-hda",
802 		.acpi_match_table = tas2781_acpi_hda_match,
803 		.pm		= &tas2781_hda_pm_ops,
804 	},
805 	.id_table	= tas2781_hda_i2c_id,
806 	.probe		= tas2781_hda_i2c_probe,
807 	.remove		= tas2781_hda_i2c_remove,
808 };
809 module_i2c_driver(tas2781_hda_i2c_driver);
810 
811 MODULE_DESCRIPTION("TAS2781 HDA Driver");
812 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
813 MODULE_LICENSE("GPL");
814 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
815 MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");
816