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