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