xref: /linux/sound/hda/codecs/side-codecs/tas2781_hda_i2c.c (revision e6b9dce0aeeb91dfc0974ab87f02454e24566182)
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 
tas2781_get_i2c_res(struct acpi_resource * ares,void * data)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 
tas2781_read_acpi(struct tasdevice_priv * p,const char * hid)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 
tas2781_hda_playback_hook(struct device * dev,int action)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 
tas2781_amp_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
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 	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 
tas2781_force_fwload_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_force_fwload_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2563_save_calibration(struct tas2781_hda * h)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 	cd->cali_dat_sz_per_dev = TAS2563_CAL_DATA_SIZE * TASDEV_CALIB_N;
319 
320 	/* extra byte for each device is the device number */
321 	cd->total_sz = (cd->cali_dat_sz_per_dev + 1) * p->ndev;
322 	data = cd->data = devm_kzalloc(p->dev, cd->total_sz,
323 		GFP_KERNEL);
324 	if (!data)
325 		return -ENOMEM;
326 
327 	for (i = 0; i < p->ndev; ++i) {
328 		data[offset] = i;
329 		offset++;
330 		for (j = 0; j < TASDEV_CALIB_N; ++j) {
331 			/* EFI name for calibration started with 1, not 0 */
332 			ret = snprintf(var8, sizeof(var8), vars[j], i + 1);
333 			if (ret < 0 || ret >= sizeof(var8) - 1) {
334 				dev_err(p->dev, "%s: Read %s failed\n",
335 					__func__, var8);
336 				return -EINVAL;
337 			}
338 			/*
339 			 * Our variable names are ASCII by construction, but
340 			 * EFI names are wide chars.  Convert and zero-pad.
341 			 */
342 			memset(efi_name, 0, sizeof(efi_name));
343 			for (k = 0; k < sizeof(var8) && var8[k]; k++)
344 				efi_name[k] = var8[k];
345 			status = efi.get_variable(efi_name,
346 				&efi_guid, &attr, &max_size,
347 				&data[offset]);
348 			if (status != EFI_SUCCESS ||
349 				max_size != TAS2563_CAL_DATA_SIZE) {
350 				dev_warn(p->dev,
351 					"Dev %d: Caldat[%d] read failed %ld\n",
352 					i, j, status);
353 				return -EINVAL;
354 			}
355 			bedata = cpu_to_be32(*(uint32_t *)&data[offset]);
356 			memcpy(&data[offset], &bedata, sizeof(bedata));
357 			offset += TAS2563_CAL_DATA_SIZE;
358 		}
359 	}
360 
361 	if (cd->total_sz != offset) {
362 		dev_err(p->dev, "%s: tot_size(%lu) and offset(%u) dismatch\n",
363 			__func__, cd->total_sz, offset);
364 		return -EINVAL;
365 	}
366 
367 	r->r0_reg = TAS2563_CAL_R0;
368 	r->invr0_reg = TAS2563_CAL_INVR0;
369 	r->r0_low_reg = TAS2563_CAL_R0_LOW;
370 	r->pow_reg = TAS2563_CAL_POWER;
371 	r->tlimit_reg = TAS2563_CAL_TLIM;
372 
373 	/*
374 	 * TAS2781_FMWLIB supports two solutions of calibrated data. One is
375 	 * from the driver itself: driver reads the calibrated files directly
376 	 * during probe; The other from user space: during init of audio hal,
377 	 * the audio hal will pass the calibrated data via kcontrol interface.
378 	 * Driver will store this data in "struct calidata" for use. For hda
379 	 * device, calibrated data are usunally saved into UEFI. So Hda side
380 	 * codec driver use the mixture of these two solutions, driver reads
381 	 * the data from UEFI, then store this data in "struct calidata" for
382 	 * use.
383 	 */
384 	p->is_user_space_calidata = true;
385 
386 	return 0;
387 }
388 
tas2781_hda_remove_controls(struct tas2781_hda * tas_hda)389 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
390 {
391 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
392 	struct hda_codec *codec = tas_hda->priv->codec;
393 
394 	snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
395 	snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
396 
397 	for (int i = ARRAY_SIZE(hda_priv->snd_ctls) - 1; i >= 0; i--)
398 		snd_ctl_remove(codec->card, hda_priv->snd_ctls[i]);
399 
400 	snd_ctl_remove(codec->card, tas_hda->prof_ctl);
401 }
402 
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)403 static void tasdev_add_kcontrols(struct tasdevice_priv *tas_priv,
404 	struct snd_kcontrol **ctls, struct hda_codec *codec,
405 	const struct snd_kcontrol_new *tas_snd_ctrls, int num_ctls)
406 {
407 	int i, ret;
408 
409 	for (i = 0; i < num_ctls; i++) {
410 		ctls[i] = snd_ctl_new1(
411 			&tas_snd_ctrls[i], tas_priv);
412 		ret = snd_ctl_add(codec->card, ctls[i]);
413 		if (ret) {
414 			dev_err(tas_priv->dev,
415 				"Failed to add KControl %s = %d\n",
416 				tas_snd_ctrls[i].name, ret);
417 			break;
418 		}
419 	}
420 }
421 
tasdevice_dspfw_init(void * context)422 static void tasdevice_dspfw_init(void *context)
423 {
424 	struct tasdevice_priv *tas_priv = context;
425 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
426 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
427 	struct hda_codec *codec = tas_priv->codec;
428 	int ret, spk_id;
429 
430 	tasdevice_dsp_remove(tas_priv);
431 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
432 	if (tas_priv->speaker_id != NULL) {
433 		// Speaker id need to be checked for ASUS only.
434 		spk_id = gpiod_get_value(tas_priv->speaker_id);
435 		if (spk_id < 0) {
436 			// Speaker id is not valid, use default.
437 			dev_dbg(tas_priv->dev, "Wrong spk_id = %d\n", spk_id);
438 			spk_id = 0;
439 		}
440 		snprintf(tas_priv->coef_binaryname,
441 			  sizeof(tas_priv->coef_binaryname),
442 			  "TAS2XXX%04X%d.bin",
443 			  lower_16_bits(codec->core.subsystem_id),
444 			  spk_id);
445 	} else {
446 		snprintf(tas_priv->coef_binaryname,
447 			  sizeof(tas_priv->coef_binaryname),
448 			  "TAS2XXX%04X.bin",
449 			  lower_16_bits(codec->core.subsystem_id));
450 	}
451 	ret = tasdevice_dsp_parser(tas_priv);
452 	if (ret) {
453 		dev_err(tas_priv->dev, "dspfw load %s error\n",
454 			tas_priv->coef_binaryname);
455 		tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
456 		return;
457 	}
458 	tasdev_add_kcontrols(tas_priv, &tas_hda->dsp_prog_ctl, codec,
459 			     &tasdevice_dsp_prog_ctrl, 1);
460 	tasdev_add_kcontrols(tas_priv, &tas_hda->dsp_conf_ctl, codec,
461 			     &tasdevice_dsp_conf_ctrl, 1);
462 
463 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
464 	tasdevice_prmg_load(tas_priv, 0);
465 	if (tas_priv->fmw->nr_programs > 0)
466 		tas_priv->cur_prog = 0;
467 	if (tas_priv->fmw->nr_configurations > 0)
468 		tas_priv->cur_conf = 0;
469 
470 	/* If calibrated data occurs error, dsp will still works with default
471 	 * calibrated data inside algo.
472 	 */
473 	hda_priv->save_calibration(tas_hda);
474 }
475 
tasdev_fw_ready(const struct firmware * fmw,void * context)476 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
477 {
478 	struct tasdevice_priv *tas_priv = context;
479 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
480 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
481 	struct hda_codec *codec = tas_priv->codec;
482 	int ret;
483 
484 	pm_runtime_get_sync(tas_priv->dev);
485 	mutex_lock(&tas_priv->codec_lock);
486 
487 	ret = tasdevice_rca_parser(tas_priv, fmw);
488 	if (ret)
489 		goto out;
490 
491 	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
492 	tasdev_add_kcontrols(tas_priv, &tas_hda->prof_ctl, codec,
493 		&tasdevice_prof_ctrl, 1);
494 
495 	switch (hda_priv->hda_chip_id) {
496 	case HDA_TAS2770:
497 		tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
498 				     &tas2770_snd_controls[0],
499 				     ARRAY_SIZE(tas2770_snd_controls));
500 		break;
501 	case HDA_TAS2781:
502 		tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
503 				     &tas2781_snd_controls[0],
504 				     ARRAY_SIZE(tas2781_snd_controls));
505 		tasdevice_dspfw_init(context);
506 		break;
507 	case HDA_TAS2563:
508 		tasdevice_dspfw_init(context);
509 		break;
510 	default:
511 		break;
512 	}
513 
514 out:
515 	mutex_unlock(&tas_hda->priv->codec_lock);
516 	release_firmware(fmw);
517 	pm_runtime_put_autosuspend(tas_hda->dev);
518 }
519 
tas2781_hda_bind(struct device * dev,struct device * master,void * master_data)520 static int tas2781_hda_bind(struct device *dev, struct device *master,
521 	void *master_data)
522 {
523 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
524 	struct hda_component_parent *parent = master_data;
525 	struct hda_component *comp;
526 	struct hda_codec *codec;
527 	unsigned int subid;
528 	int ret;
529 
530 	comp = hda_component_from_index(parent, tas_hda->priv->index);
531 	if (!comp)
532 		return -EINVAL;
533 
534 	if (comp->dev)
535 		return -EBUSY;
536 
537 	codec = parent->codec;
538 	subid = codec->core.subsystem_id >> 16;
539 
540 	switch (subid) {
541 	case 0x1028:
542 		tas_hda->catlog_id = DELL;
543 		break;
544 	default:
545 		tas_hda->catlog_id = LENOVO;
546 		break;
547 	}
548 
549 	pm_runtime_get_sync(dev);
550 
551 	comp->dev = dev;
552 
553 	strscpy(comp->name, dev_name(dev), sizeof(comp->name));
554 
555 	ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
556 	if (!ret)
557 		comp->playback_hook = tas2781_hda_playback_hook;
558 
559 	pm_runtime_put_autosuspend(dev);
560 
561 	return ret;
562 }
563 
tas2781_hda_unbind(struct device * dev,struct device * master,void * master_data)564 static void tas2781_hda_unbind(struct device *dev,
565 	struct device *master, void *master_data)
566 {
567 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
568 	struct hda_component_parent *parent = master_data;
569 	struct hda_component *comp;
570 
571 	comp = hda_component_from_index(parent, tas_hda->priv->index);
572 	if (comp && (comp->dev == dev)) {
573 		comp->dev = NULL;
574 		memset(comp->name, 0, sizeof(comp->name));
575 		comp->playback_hook = NULL;
576 	}
577 
578 	tas2781_hda_remove_controls(tas_hda);
579 
580 	tasdevice_config_info_remove(tas_hda->priv);
581 	tasdevice_dsp_remove(tas_hda->priv);
582 
583 	tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
584 }
585 
586 static const struct component_ops tas2781_hda_comp_ops = {
587 	.bind = tas2781_hda_bind,
588 	.unbind = tas2781_hda_unbind,
589 };
590 
tas2781_hda_i2c_probe(struct i2c_client * clt)591 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
592 {
593 	struct tas2781_hda_i2c_priv *hda_priv;
594 	struct tas2781_hda *tas_hda;
595 	const char *device_name;
596 	int ret;
597 
598 	tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
599 	if (!tas_hda)
600 		return -ENOMEM;
601 
602 	hda_priv = devm_kzalloc(&clt->dev, sizeof(*hda_priv), GFP_KERNEL);
603 	if (!hda_priv)
604 		return -ENOMEM;
605 
606 	tas_hda->hda_priv = hda_priv;
607 
608 	dev_set_drvdata(&clt->dev, tas_hda);
609 	tas_hda->dev = &clt->dev;
610 
611 	tas_hda->priv = tasdevice_kzalloc(clt);
612 	if (!tas_hda->priv)
613 		return -ENOMEM;
614 
615 	if (strstr(dev_name(&clt->dev), "TIAS2781")) {
616 		/*
617 		 * TAS2781, integrated on-chip DSP with
618 		 * global I2C address supported.
619 		 */
620 		device_name = "TIAS2781";
621 		hda_priv->hda_chip_id = HDA_TAS2781;
622 		hda_priv->save_calibration = tas2781_save_calibration;
623 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
624 	} else if (strstarts(dev_name(&clt->dev), "i2c-TXNW2770")) {
625 		/*
626 		 * TAS2770, has no on-chip DSP, so no calibration data
627 		 * required; has no global I2C address supported.
628 		 */
629 		device_name = "TXNW2770";
630 		hda_priv->hda_chip_id = HDA_TAS2770;
631 	} else if (strstarts(dev_name(&clt->dev),
632 			     "i2c-TXNW2781:00-tas2781-hda.0")) {
633 		device_name = "TXNW2781";
634 		hda_priv->save_calibration = tas2781_save_calibration;
635 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
636 	} else if (strstr(dev_name(&clt->dev), "INT8866")) {
637 		/*
638 		 * TAS2563, integrated on-chip DSP with
639 		 * global I2C address supported.
640 		 */
641 		device_name = "INT8866";
642 		hda_priv->hda_chip_id = HDA_TAS2563;
643 		hda_priv->save_calibration = tas2563_save_calibration;
644 		tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
645 	} else {
646 		return -ENODEV;
647 	}
648 
649 	tas_hda->priv->irq = clt->irq;
650 	ret = tas2781_read_acpi(tas_hda->priv, device_name);
651 	if (ret)
652 		return dev_err_probe(tas_hda->dev, ret,
653 			"Platform not supported\n");
654 
655 	ret = tasdevice_init(tas_hda->priv);
656 	if (ret)
657 		goto err;
658 
659 	pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
660 	pm_runtime_use_autosuspend(tas_hda->dev);
661 	pm_runtime_mark_last_busy(tas_hda->dev);
662 	pm_runtime_set_active(tas_hda->dev);
663 	pm_runtime_enable(tas_hda->dev);
664 
665 	tasdevice_reset(tas_hda->priv);
666 
667 	ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
668 	if (ret) {
669 		dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
670 		pm_runtime_disable(tas_hda->dev);
671 	}
672 
673 err:
674 	if (ret)
675 		tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
676 	return ret;
677 }
678 
tas2781_hda_i2c_remove(struct i2c_client * clt)679 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
680 {
681 	tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
682 }
683 
tas2781_runtime_suspend(struct device * dev)684 static int tas2781_runtime_suspend(struct device *dev)
685 {
686 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
687 
688 	dev_dbg(tas_hda->dev, "Runtime Suspend\n");
689 
690 	mutex_lock(&tas_hda->priv->codec_lock);
691 
692 	/* The driver powers up the amplifiers at module load time.
693 	 * Stop the playback if it's unused.
694 	 */
695 	if (tas_hda->priv->playback_started) {
696 		tasdevice_tuning_switch(tas_hda->priv, 1);
697 		tas_hda->priv->playback_started = false;
698 	}
699 
700 	mutex_unlock(&tas_hda->priv->codec_lock);
701 
702 	return 0;
703 }
704 
tas2781_runtime_resume(struct device * dev)705 static int tas2781_runtime_resume(struct device *dev)
706 {
707 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
708 
709 	dev_dbg(tas_hda->dev, "Runtime Resume\n");
710 
711 	mutex_lock(&tas_hda->priv->codec_lock);
712 
713 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
714 
715 	mutex_unlock(&tas_hda->priv->codec_lock);
716 
717 	return 0;
718 }
719 
tas2781_system_suspend(struct device * dev)720 static int tas2781_system_suspend(struct device *dev)
721 {
722 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
723 
724 	dev_dbg(tas_hda->priv->dev, "System Suspend\n");
725 
726 	mutex_lock(&tas_hda->priv->codec_lock);
727 
728 	/* Shutdown chip before system suspend */
729 	if (tas_hda->priv->playback_started)
730 		tasdevice_tuning_switch(tas_hda->priv, 1);
731 
732 	mutex_unlock(&tas_hda->priv->codec_lock);
733 
734 	/*
735 	 * Reset GPIO may be shared, so cannot reset here.
736 	 * However beyond this point, amps may be powered down.
737 	 */
738 	return 0;
739 }
740 
tas2781_system_resume(struct device * dev)741 static int tas2781_system_resume(struct device *dev)
742 {
743 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
744 	int i;
745 
746 	dev_dbg(tas_hda->priv->dev, "System Resume\n");
747 
748 	mutex_lock(&tas_hda->priv->codec_lock);
749 
750 	for (i = 0; i < tas_hda->priv->ndev; i++) {
751 		tas_hda->priv->tasdevice[i].cur_book = -1;
752 		tas_hda->priv->tasdevice[i].cur_prog = -1;
753 		tas_hda->priv->tasdevice[i].cur_conf = -1;
754 	}
755 	tasdevice_reset(tas_hda->priv);
756 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
757 
758 	if (tas_hda->priv->playback_started)
759 		tasdevice_tuning_switch(tas_hda->priv, 0);
760 
761 	mutex_unlock(&tas_hda->priv->codec_lock);
762 
763 	return 0;
764 }
765 
766 static const struct dev_pm_ops tas2781_hda_pm_ops = {
767 	RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
768 	SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
769 };
770 
771 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
772 	{ "tas2781-hda" },
773 	{}
774 };
775 
776 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
777 	{"INT8866", 0 },
778 	{"TIAS2781", 0 },
779 	{"TXNW2770", 0 },
780 	{"TXNW2781", 0 },
781 	{}
782 };
783 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
784 
785 static struct i2c_driver tas2781_hda_i2c_driver = {
786 	.driver = {
787 		.name		= "tas2781-hda",
788 		.acpi_match_table = tas2781_acpi_hda_match,
789 		.pm		= &tas2781_hda_pm_ops,
790 	},
791 	.id_table	= tas2781_hda_i2c_id,
792 	.probe		= tas2781_hda_i2c_probe,
793 	.remove		= tas2781_hda_i2c_remove,
794 };
795 module_i2c_driver(tas2781_hda_i2c_driver);
796 
797 MODULE_DESCRIPTION("TAS2781 HDA Driver");
798 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
799 MODULE_LICENSE("GPL");
800 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
801 MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");
802