xref: /linux/sound/hda/codecs/side-codecs/tas2781_hda_i2c.c (revision fdfa38e95e1229ec2fb2f18540987c97f861d470)
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 
tas2781_get_i2c_res(struct acpi_resource * ares,void * data)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 
tas2781_read_acpi(struct tasdevice_priv * p,const char * hid)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 
tas2781_hda_playback_hook(struct device * dev,int action)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 
tas2781_amp_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_amp_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_force_fwload_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_force_fwload_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2563_save_calibration(struct tas2781_hda * h)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 
tas2781_hda_remove_controls(struct tas2781_hda * tas_hda)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 
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)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 
tasdevice_dspfw_init(void * context)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 	/* Init common setting for different audio profiles */
478 	if (tas_priv->rcabin.init_profile_id >= 0)
479 		tasdevice_select_cfg_blk(tas_priv,
480 			tas_priv->rcabin.init_profile_id,
481 			TASDEVICE_BIN_BLK_PRE_POWER_UP);
482 
483 	/* If calibrated data occurs error, dsp will still works with default
484 	 * calibrated data inside algo.
485 	 */
486 	hda_priv->save_calibration(tas_hda);
487 }
488 
tasdev_fw_ready(const struct firmware * fmw,void * context)489 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
490 {
491 	struct tasdevice_priv *tas_priv = context;
492 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
493 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
494 	struct hda_codec *codec = tas_priv->codec;
495 	int ret;
496 
497 	pm_runtime_get_sync(tas_priv->dev);
498 	mutex_lock(&tas_priv->codec_lock);
499 
500 	ret = tasdevice_rca_parser(tas_priv, fmw);
501 	if (ret)
502 		goto out;
503 
504 	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
505 	tasdev_add_kcontrols(tas_priv, &tas_hda->prof_ctl, codec,
506 		&tasdevice_prof_ctrl, 1);
507 
508 	switch (hda_priv->hda_chip_id) {
509 	case HDA_TAS2770:
510 		tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
511 				     &tas2770_snd_controls[0],
512 				     ARRAY_SIZE(tas2770_snd_controls));
513 		break;
514 	case HDA_TAS2781:
515 		tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
516 				     &tas2781_snd_controls[0],
517 				     ARRAY_SIZE(tas2781_snd_controls));
518 		tasdevice_dspfw_init(context);
519 		break;
520 	case HDA_TAS5825:
521 		tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
522 				     &tas5825_snd_controls[0],
523 				     ARRAY_SIZE(tas5825_snd_controls));
524 		tasdevice_dspfw_init(context);
525 		break;
526 	case HDA_TAS2563:
527 		tasdevice_dspfw_init(context);
528 		break;
529 	default:
530 		break;
531 	}
532 
533 out:
534 	mutex_unlock(&tas_hda->priv->codec_lock);
535 	release_firmware(fmw);
536 	pm_runtime_put_autosuspend(tas_hda->dev);
537 }
538 
tas2781_hda_bind(struct device * dev,struct device * master,void * master_data)539 static int tas2781_hda_bind(struct device *dev, struct device *master,
540 	void *master_data)
541 {
542 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
543 	struct hda_component_parent *parent = master_data;
544 	struct hda_component *comp;
545 	struct hda_codec *codec;
546 	unsigned int subid;
547 	int ret;
548 
549 	comp = hda_component_from_index(parent, tas_hda->priv->index);
550 	if (!comp)
551 		return -EINVAL;
552 
553 	if (comp->dev)
554 		return -EBUSY;
555 
556 	codec = parent->codec;
557 	subid = codec->core.subsystem_id >> 16;
558 
559 	switch (subid) {
560 	case 0x1028:
561 		tas_hda->catlog_id = DELL;
562 		break;
563 	default:
564 		tas_hda->catlog_id = LENOVO;
565 		break;
566 	}
567 
568 	pm_runtime_get_sync(dev);
569 
570 	comp->dev = dev;
571 
572 	strscpy(comp->name, dev_name(dev), sizeof(comp->name));
573 
574 	ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
575 	if (!ret)
576 		comp->playback_hook = tas2781_hda_playback_hook;
577 
578 	pm_runtime_put_autosuspend(dev);
579 
580 	return ret;
581 }
582 
tas2781_hda_unbind(struct device * dev,struct device * master,void * master_data)583 static void tas2781_hda_unbind(struct device *dev,
584 	struct device *master, void *master_data)
585 {
586 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
587 	struct hda_component_parent *parent = master_data;
588 	struct hda_component *comp;
589 
590 	comp = hda_component_from_index(parent, tas_hda->priv->index);
591 	if (comp && (comp->dev == dev)) {
592 		comp->dev = NULL;
593 		memset(comp->name, 0, sizeof(comp->name));
594 		comp->playback_hook = NULL;
595 	}
596 
597 	tas2781_hda_remove_controls(tas_hda);
598 
599 	tasdevice_config_info_remove(tas_hda->priv);
600 	tasdevice_dsp_remove(tas_hda->priv);
601 
602 	tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
603 }
604 
605 static const struct component_ops tas2781_hda_comp_ops = {
606 	.bind = tas2781_hda_bind,
607 	.unbind = tas2781_hda_unbind,
608 };
609 
tas2781_hda_i2c_probe(struct i2c_client * clt)610 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
611 {
612 	struct tas2781_hda_i2c_priv *hda_priv;
613 	struct tas2781_hda *tas_hda;
614 	const char *device_name;
615 	int ret;
616 
617 	tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
618 	if (!tas_hda)
619 		return -ENOMEM;
620 
621 	hda_priv = devm_kzalloc(&clt->dev, sizeof(*hda_priv), GFP_KERNEL);
622 	if (!hda_priv)
623 		return -ENOMEM;
624 
625 	tas_hda->hda_priv = hda_priv;
626 
627 	dev_set_drvdata(&clt->dev, tas_hda);
628 	tas_hda->dev = &clt->dev;
629 
630 	tas_hda->priv = tasdevice_kzalloc(clt);
631 	if (!tas_hda->priv)
632 		return -ENOMEM;
633 
634 	if (strstr(dev_name(&clt->dev), "TIAS2781")) {
635 		/*
636 		 * TAS2781, integrated on-chip DSP with
637 		 * global I2C address supported.
638 		 */
639 		device_name = "TIAS2781";
640 		hda_priv->hda_chip_id = HDA_TAS2781;
641 		hda_priv->save_calibration = tas2781_save_calibration;
642 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
643 	} else if (strstarts(dev_name(&clt->dev), "i2c-TXNW2770")) {
644 		/*
645 		 * TAS2770, has no on-chip DSP, so no calibration data
646 		 * required; has no global I2C address supported.
647 		 */
648 		device_name = "TXNW2770";
649 		hda_priv->hda_chip_id = HDA_TAS2770;
650 	} else if (strstarts(dev_name(&clt->dev),
651 			     "i2c-TXNW2781:00-tas2781-hda.0")) {
652 		device_name = "TXNW2781";
653 		hda_priv->hda_chip_id = HDA_TAS2781;
654 		hda_priv->save_calibration = tas2781_save_calibration;
655 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
656 	} else if (strstr(dev_name(&clt->dev), "INT8866")) {
657 		/*
658 		 * TAS2563, integrated on-chip DSP with
659 		 * global I2C address supported.
660 		 */
661 		device_name = "INT8866";
662 		hda_priv->hda_chip_id = HDA_TAS2563;
663 		hda_priv->save_calibration = tas2563_save_calibration;
664 		tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
665 	} else if (strstarts(dev_name(&clt->dev), "i2c-TXNW5825")) {
666 		/*
667 		 * TAS5825, integrated on-chip DSP without
668 		 * global I2C address and calibration supported.
669 		 */
670 		device_name = "TXNW5825";
671 		hda_priv->hda_chip_id = HDA_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);
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);
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);
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 	{ "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