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