xref: /linux/sound/pci/hda/tas2781_hda_i2c.c (revision eb01fe7abbe2d0b38824d2a93fdb4cc3eaf2ccc1)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA I2C driver
4 //
5 // Copyright 2023 Texas Instruments, Inc.
6 //
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
8 
9 #include <linux/acpi.h>
10 #include <linux/crc8.h>
11 #include <linux/crc32.h>
12 #include <linux/efi.h>
13 #include <linux/firmware.h>
14 #include <linux/i2c.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <sound/hda_codec.h>
20 #include <sound/soc.h>
21 #include <sound/tas2781.h>
22 #include <sound/tlv.h>
23 #include <sound/tas2781-tlv.h>
24 
25 #include "hda_local.h"
26 #include "hda_auto_parser.h"
27 #include "hda_component.h"
28 #include "hda_jack.h"
29 #include "hda_generic.h"
30 
31 #define TASDEVICE_SPEAKER_CALIBRATION_SIZE	20
32 
33 /* No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD
34  * Define two controls, one is Volume control callbacks, the other is
35  * flag setting control callbacks.
36  */
37 
38 /* Volume control callbacks for tas2781 */
39 #define ACARD_SINGLE_RANGE_EXT_TLV(xname, xreg, xshift, xmin, xmax, xinvert, \
40 	xhandler_get, xhandler_put, tlv_array) \
41 {	.iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = (xname),\
42 	.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
43 		 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
44 	.tlv.p = (tlv_array), \
45 	.info = snd_soc_info_volsw_range, \
46 	.get = xhandler_get, .put = xhandler_put, \
47 	.private_value = (unsigned long)&(struct soc_mixer_control) \
48 		{.reg = xreg, .rreg = xreg, .shift = xshift, \
49 		 .rshift = xshift, .min = xmin, .max = xmax, \
50 		 .invert = xinvert} }
51 
52 /* Flag control callbacks for tas2781 */
53 #define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
54 {	.iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, \
55 	.info = snd_ctl_boolean_mono_info, \
56 	.get = xhandler_get, .put = xhandler_put, \
57 	.private_value = xdata }
58 
59 enum calib_data {
60 	R0_VAL = 0,
61 	INV_R0,
62 	R0LOW,
63 	POWER,
64 	TLIM,
65 	CALIB_MAX
66 };
67 
68 #define TAS2563_MAX_CHANNELS	4
69 
70 #define TAS2563_CAL_POWER	TASDEVICE_REG(0, 0x0d, 0x3c)
71 #define TAS2563_CAL_R0		TASDEVICE_REG(0, 0x0f, 0x34)
72 #define TAS2563_CAL_INVR0	TASDEVICE_REG(0, 0x0f, 0x40)
73 #define TAS2563_CAL_R0_LOW	TASDEVICE_REG(0, 0x0f, 0x48)
74 #define TAS2563_CAL_TLIM	TASDEVICE_REG(0, 0x10, 0x14)
75 #define TAS2563_CAL_N		5
76 #define TAS2563_CAL_DATA_SIZE	4
77 #define TAS2563_CAL_CH_SIZE	20
78 #define TAS2563_CAL_ARRAY_SIZE	80
79 
80 static unsigned int cal_regs[TAS2563_CAL_N] = {
81 	TAS2563_CAL_POWER, TAS2563_CAL_R0, TAS2563_CAL_INVR0,
82 	TAS2563_CAL_R0_LOW, TAS2563_CAL_TLIM,
83 };
84 
85 
86 struct tas2781_hda {
87 	struct device *dev;
88 	struct tasdevice_priv *priv;
89 	struct snd_kcontrol *dsp_prog_ctl;
90 	struct snd_kcontrol *dsp_conf_ctl;
91 	struct snd_kcontrol *prof_ctl;
92 	struct snd_kcontrol *snd_ctls[3];
93 };
94 
95 static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
96 {
97 	struct tasdevice_priv *tas_priv = data;
98 	struct acpi_resource_i2c_serialbus *sb;
99 
100 	if (i2c_acpi_get_i2c_resource(ares, &sb)) {
101 		if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS &&
102 			sb->slave_address != tas_priv->global_addr) {
103 			tas_priv->tasdevice[tas_priv->ndev].dev_addr =
104 				(unsigned int)sb->slave_address;
105 			tas_priv->ndev++;
106 		}
107 	}
108 	return 1;
109 }
110 
111 static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
112 {
113 	struct acpi_device *adev;
114 	LIST_HEAD(resources);
115 	int ret;
116 
117 	adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
118 	if (!adev) {
119 		dev_err(p->dev,
120 			"Failed to find an ACPI device for %s\n", hid);
121 		return -ENODEV;
122 	}
123 
124 	ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
125 	if (ret < 0)
126 		goto err;
127 
128 	acpi_dev_free_resource_list(&resources);
129 	strscpy(p->dev_name, hid, sizeof(p->dev_name));
130 	acpi_dev_put(adev);
131 
132 	return 0;
133 
134 err:
135 	dev_err(p->dev, "read acpi error, ret: %d\n", ret);
136 	acpi_dev_put(adev);
137 
138 	return ret;
139 }
140 
141 static void tas2781_hda_playback_hook(struct device *dev, int action)
142 {
143 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
144 
145 	dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
146 	switch (action) {
147 	case HDA_GEN_PCM_ACT_OPEN:
148 		pm_runtime_get_sync(dev);
149 		mutex_lock(&tas_hda->priv->codec_lock);
150 		tasdevice_tuning_switch(tas_hda->priv, 0);
151 		tas_hda->priv->playback_started = true;
152 		mutex_unlock(&tas_hda->priv->codec_lock);
153 		break;
154 	case HDA_GEN_PCM_ACT_CLOSE:
155 		mutex_lock(&tas_hda->priv->codec_lock);
156 		tasdevice_tuning_switch(tas_hda->priv, 1);
157 		tas_hda->priv->playback_started = false;
158 		mutex_unlock(&tas_hda->priv->codec_lock);
159 
160 		pm_runtime_mark_last_busy(dev);
161 		pm_runtime_put_autosuspend(dev);
162 		break;
163 	default:
164 		dev_dbg(tas_hda->dev, "Playback action not supported: %d\n",
165 			action);
166 		break;
167 	}
168 }
169 
170 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
171 			struct snd_ctl_elem_info *uinfo)
172 {
173 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
174 
175 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
176 	uinfo->count = 1;
177 	uinfo->value.integer.min = 0;
178 	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
179 
180 	return 0;
181 }
182 
183 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
184 			struct snd_ctl_elem_value *ucontrol)
185 {
186 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
187 
188 	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
189 
190 	return 0;
191 }
192 
193 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
194 		struct snd_ctl_elem_value *ucontrol)
195 {
196 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
197 	int nr_profile = ucontrol->value.integer.value[0];
198 	int max = tas_priv->rcabin.ncfgs - 1;
199 	int val, ret = 0;
200 
201 	val = clamp(nr_profile, 0, max);
202 
203 	if (tas_priv->rcabin.profile_cfg_id != val) {
204 		tas_priv->rcabin.profile_cfg_id = val;
205 		ret = 1;
206 	}
207 
208 	return ret;
209 }
210 
211 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
212 			struct snd_ctl_elem_info *uinfo)
213 {
214 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
215 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
216 
217 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
218 	uinfo->count = 1;
219 	uinfo->value.integer.min = 0;
220 	uinfo->value.integer.max = tas_fw->nr_programs - 1;
221 
222 	return 0;
223 }
224 
225 static int tasdevice_info_config(struct snd_kcontrol *kcontrol,
226 	struct snd_ctl_elem_info *uinfo)
227 {
228 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
229 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
230 
231 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
232 	uinfo->count = 1;
233 	uinfo->value.integer.min = 0;
234 	uinfo->value.integer.max = tas_fw->nr_configurations - 1;
235 
236 	return 0;
237 }
238 
239 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
240 	struct snd_ctl_elem_value *ucontrol)
241 {
242 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
243 
244 	ucontrol->value.integer.value[0] = tas_priv->cur_prog;
245 
246 	return 0;
247 }
248 
249 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
250 	struct snd_ctl_elem_value *ucontrol)
251 {
252 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
253 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
254 	int nr_program = ucontrol->value.integer.value[0];
255 	int max = tas_fw->nr_programs - 1;
256 	int val, ret = 0;
257 
258 	val = clamp(nr_program, 0, max);
259 
260 	if (tas_priv->cur_prog != val) {
261 		tas_priv->cur_prog = val;
262 		ret = 1;
263 	}
264 
265 	return ret;
266 }
267 
268 static int tasdevice_config_get(struct snd_kcontrol *kcontrol,
269 	struct snd_ctl_elem_value *ucontrol)
270 {
271 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
272 
273 	ucontrol->value.integer.value[0] = tas_priv->cur_conf;
274 
275 	return 0;
276 }
277 
278 static int tasdevice_config_put(struct snd_kcontrol *kcontrol,
279 	struct snd_ctl_elem_value *ucontrol)
280 {
281 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
282 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
283 	int nr_config = ucontrol->value.integer.value[0];
284 	int max = tas_fw->nr_configurations - 1;
285 	int val, ret = 0;
286 
287 	val = clamp(nr_config, 0, max);
288 
289 	if (tas_priv->cur_conf != val) {
290 		tas_priv->cur_conf = val;
291 		ret = 1;
292 	}
293 
294 	return ret;
295 }
296 
297 /*
298  * tas2781_digital_getvol - get the volum control
299  * @kcontrol: control pointer
300  * @ucontrol: User data
301  * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
302  * depends on internal regmap mechanism.
303  * tas2781 contains book and page two-level register map, especially
304  * book switching will set the register BXXP00R7F, after switching to the
305  * correct book, then leverage the mechanism for paging to access the
306  * register.
307  */
308 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
309 	struct snd_ctl_elem_value *ucontrol)
310 {
311 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
312 	struct soc_mixer_control *mc =
313 		(struct soc_mixer_control *)kcontrol->private_value;
314 
315 	return tasdevice_digital_getvol(tas_priv, ucontrol, mc);
316 }
317 
318 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
319 	struct snd_ctl_elem_value *ucontrol)
320 {
321 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
322 	struct soc_mixer_control *mc =
323 		(struct soc_mixer_control *)kcontrol->private_value;
324 
325 	return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
326 }
327 
328 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
329 	struct snd_ctl_elem_value *ucontrol)
330 {
331 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
332 	struct soc_mixer_control *mc =
333 		(struct soc_mixer_control *)kcontrol->private_value;
334 
335 	/* The check of the given value is in tasdevice_digital_putvol. */
336 	return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
337 }
338 
339 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
340 	struct snd_ctl_elem_value *ucontrol)
341 {
342 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
343 	struct soc_mixer_control *mc =
344 		(struct soc_mixer_control *)kcontrol->private_value;
345 
346 	/* The check of the given value is in tasdevice_amp_putvol. */
347 	return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
348 }
349 
350 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
351 	struct snd_ctl_elem_value *ucontrol)
352 {
353 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
354 
355 	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
356 	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
357 			tas_priv->force_fwload_status ? "ON" : "OFF");
358 
359 	return 0;
360 }
361 
362 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
363 	struct snd_ctl_elem_value *ucontrol)
364 {
365 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
366 	bool change, val = (bool)ucontrol->value.integer.value[0];
367 
368 	if (tas_priv->force_fwload_status == val)
369 		change = false;
370 	else {
371 		change = true;
372 		tas_priv->force_fwload_status = val;
373 	}
374 	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
375 		tas_priv->force_fwload_status ? "ON" : "OFF");
376 
377 	return change;
378 }
379 
380 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
381 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
382 		1, 0, 20, 0, tas2781_amp_getvol,
383 		tas2781_amp_putvol, amp_vol_tlv),
384 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,
385 		0, 0, 200, 1, tas2781_digital_getvol,
386 		tas2781_digital_putvol, dvc_tlv),
387 	ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
388 		tas2781_force_fwload_get, tas2781_force_fwload_put),
389 };
390 
391 static const struct snd_kcontrol_new tas2781_prof_ctrl = {
392 	.name = "Speaker Profile Id",
393 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
394 	.info = tasdevice_info_profile,
395 	.get = tasdevice_get_profile_id,
396 	.put = tasdevice_set_profile_id,
397 };
398 
399 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = {
400 	.name = "Speaker Program Id",
401 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
402 	.info = tasdevice_info_programs,
403 	.get = tasdevice_program_get,
404 	.put = tasdevice_program_put,
405 };
406 
407 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = {
408 	.name = "Speaker Config Id",
409 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
410 	.info = tasdevice_info_config,
411 	.get = tasdevice_config_get,
412 	.put = tasdevice_config_put,
413 };
414 
415 static void tas2563_apply_calib(struct tasdevice_priv *tas_priv)
416 {
417 	int offset = 0;
418 	__be32 data;
419 	int ret;
420 
421 	for (int i = 0; i < tas_priv->ndev; i++) {
422 		for (int j = 0; j < TAS2563_CAL_N; ++j) {
423 			data = cpu_to_be32(
424 				*(uint32_t *)&tas_priv->cali_data.data[offset]);
425 			ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j],
426 				(unsigned char *)&data, TAS2563_CAL_DATA_SIZE);
427 			if (ret)
428 				dev_err(tas_priv->dev,
429 					"Error writing calib regs\n");
430 			offset += TAS2563_CAL_DATA_SIZE;
431 		}
432 	}
433 }
434 
435 static int tas2563_save_calibration(struct tasdevice_priv *tas_priv)
436 {
437 	static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
438 		0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
439 
440 	static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = {
441 		{ L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" },
442 		{ L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" },
443 		{ L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" },
444 		{ L"Power_4", L"R0_4", L"InvR0_4", L"R0_Low_4", L"TLim_4" },
445 	};
446 
447 	unsigned long max_size = TAS2563_CAL_DATA_SIZE;
448 	unsigned int offset = 0;
449 	efi_status_t status;
450 	unsigned int attr;
451 
452 	tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
453 			TAS2563_CAL_ARRAY_SIZE, GFP_KERNEL);
454 	if (!tas_priv->cali_data.data)
455 		return -ENOMEM;
456 
457 	for (int i = 0; i < tas_priv->ndev; ++i) {
458 		for (int j = 0; j < TAS2563_CAL_N; ++j) {
459 			status = efi.get_variable(efi_vars[i][j],
460 				&efi_guid, &attr, &max_size,
461 				&tas_priv->cali_data.data[offset]);
462 			if (status != EFI_SUCCESS ||
463 				max_size != TAS2563_CAL_DATA_SIZE) {
464 				dev_warn(tas_priv->dev,
465 				"Calibration data read failed %ld\n", status);
466 				return -EINVAL;
467 			}
468 			offset += TAS2563_CAL_DATA_SIZE;
469 		}
470 	}
471 
472 	tas_priv->cali_data.total_sz = offset;
473 	tasdevice_apply_calibration(tas_priv);
474 
475 	return 0;
476 }
477 
478 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv)
479 {
480 	static const unsigned char page_array[CALIB_MAX] = {
481 		0x17, 0x18, 0x18, 0x0d, 0x18
482 	};
483 	static const unsigned char rgno_array[CALIB_MAX] = {
484 		0x74, 0x0c, 0x14, 0x3c, 0x7c
485 	};
486 	unsigned char *data;
487 	int i, j, rc;
488 
489 	for (i = 0; i < tas_priv->ndev; i++) {
490 		data = tas_priv->cali_data.data +
491 			i * TASDEVICE_SPEAKER_CALIBRATION_SIZE;
492 		for (j = 0; j < CALIB_MAX; j++) {
493 			rc = tasdevice_dev_bulk_write(tas_priv, i,
494 				TASDEVICE_REG(0, page_array[j], rgno_array[j]),
495 				&(data[4 * j]), 4);
496 			if (rc < 0)
497 				dev_err(tas_priv->dev,
498 					"chn %d calib %d bulk_wr err = %d\n",
499 					i, j, rc);
500 		}
501 	}
502 }
503 
504 /* Update the calibration data, including speaker impedance, f0, etc, into algo.
505  * Calibrate data is done by manufacturer in the factory. These data are used
506  * by Algo for calculating the speaker temperature, speaker membrane excursion
507  * and f0 in real time during playback.
508  */
509 static int tas2781_save_calibration(struct tasdevice_priv *tas_priv)
510 {
511 	efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d,
512 		0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
513 	static efi_char16_t efi_name[] = L"CALI_DATA";
514 	struct tm *tm = &tas_priv->tm;
515 	unsigned int attr, crc;
516 	unsigned int *tmp_val;
517 	efi_status_t status;
518 
519 	/* Lenovo devices */
520 	if (tas_priv->catlog_id == LENOVO)
521 		efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09,
522 			0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
523 
524 	tas_priv->cali_data.total_sz = 0;
525 	/* Get real size of UEFI variable */
526 	status = efi.get_variable(efi_name, &efi_guid, &attr,
527 		&tas_priv->cali_data.total_sz, tas_priv->cali_data.data);
528 	if (status == EFI_BUFFER_TOO_SMALL) {
529 		/* Allocate data buffer of data_size bytes */
530 		tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
531 			tas_priv->cali_data.total_sz, GFP_KERNEL);
532 		if (!tas_priv->cali_data.data)
533 			return -ENOMEM;
534 		/* Get variable contents into buffer */
535 		status = efi.get_variable(efi_name, &efi_guid, &attr,
536 			&tas_priv->cali_data.total_sz,
537 			tas_priv->cali_data.data);
538 	}
539 	if (status != EFI_SUCCESS)
540 		return -EINVAL;
541 
542 	tmp_val = (unsigned int *)tas_priv->cali_data.data;
543 
544 	crc = crc32(~0, tas_priv->cali_data.data, 84) ^ ~0;
545 	dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n",
546 		crc, tmp_val[21]);
547 
548 	if (crc == tmp_val[21]) {
549 		time64_to_tm(tmp_val[20], 0, tm);
550 		dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n",
551 			tm->tm_year, tm->tm_mon, tm->tm_mday,
552 			tm->tm_hour, tm->tm_min, tm->tm_sec);
553 		tasdevice_apply_calibration(tas_priv);
554 	} else
555 		tas_priv->cali_data.total_sz = 0;
556 
557 	return 0;
558 }
559 
560 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
561 {
562 	struct hda_codec *codec = tas_hda->priv->codec;
563 
564 	if (tas_hda->dsp_prog_ctl)
565 		snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
566 
567 	if (tas_hda->dsp_conf_ctl)
568 		snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
569 
570 	for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
571 		if (tas_hda->snd_ctls[i])
572 			snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
573 
574 	if (tas_hda->prof_ctl)
575 		snd_ctl_remove(codec->card, tas_hda->prof_ctl);
576 }
577 
578 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
579 {
580 	struct tasdevice_priv *tas_priv = context;
581 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
582 	struct hda_codec *codec = tas_priv->codec;
583 	int i, ret;
584 
585 	pm_runtime_get_sync(tas_priv->dev);
586 	mutex_lock(&tas_priv->codec_lock);
587 
588 	ret = tasdevice_rca_parser(tas_priv, fmw);
589 	if (ret)
590 		goto out;
591 
592 	tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv);
593 	ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
594 	if (ret) {
595 		dev_err(tas_priv->dev,
596 			"Failed to add KControl %s = %d\n",
597 			tas2781_prof_ctrl.name, ret);
598 		goto out;
599 	}
600 
601 	for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) {
602 		tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i],
603 			tas_priv);
604 		ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
605 		if (ret) {
606 			dev_err(tas_priv->dev,
607 				"Failed to add KControl %s = %d\n",
608 				tas2781_snd_controls[i].name, ret);
609 			goto out;
610 		}
611 	}
612 
613 	tasdevice_dsp_remove(tas_priv);
614 
615 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
616 	scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X.bin",
617 		codec->core.subsystem_id & 0xffff);
618 	ret = tasdevice_dsp_parser(tas_priv);
619 	if (ret) {
620 		dev_err(tas_priv->dev, "dspfw load %s error\n",
621 			tas_priv->coef_binaryname);
622 		tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
623 		goto out;
624 	}
625 
626 	tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl,
627 		tas_priv);
628 	ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
629 	if (ret) {
630 		dev_err(tas_priv->dev,
631 			"Failed to add KControl %s = %d\n",
632 			tas2781_dsp_prog_ctrl.name, ret);
633 		goto out;
634 	}
635 
636 	tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl,
637 		tas_priv);
638 	ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
639 	if (ret) {
640 		dev_err(tas_priv->dev,
641 			"Failed to add KControl %s = %d\n",
642 			tas2781_dsp_conf_ctrl.name, ret);
643 		goto out;
644 	}
645 
646 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
647 	tasdevice_prmg_load(tas_priv, 0);
648 	if (tas_priv->fmw->nr_programs > 0)
649 		tas_priv->cur_prog = 0;
650 	if (tas_priv->fmw->nr_configurations > 0)
651 		tas_priv->cur_conf = 0;
652 
653 	/* If calibrated data occurs error, dsp will still works with default
654 	 * calibrated data inside algo.
655 	 */
656 	tasdevice_save_calibration(tas_priv);
657 
658 	tasdevice_tuning_switch(tas_hda->priv, 0);
659 	tas_hda->priv->playback_started = true;
660 
661 out:
662 	mutex_unlock(&tas_hda->priv->codec_lock);
663 	if (fmw)
664 		release_firmware(fmw);
665 	pm_runtime_mark_last_busy(tas_hda->dev);
666 	pm_runtime_put_autosuspend(tas_hda->dev);
667 }
668 
669 static int tas2781_hda_bind(struct device *dev, struct device *master,
670 	void *master_data)
671 {
672 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
673 	struct hda_component *comps = master_data;
674 	struct hda_codec *codec;
675 	unsigned int subid;
676 	int ret;
677 
678 	if (!comps || tas_hda->priv->index < 0 ||
679 		tas_hda->priv->index >= HDA_MAX_COMPONENTS)
680 		return -EINVAL;
681 
682 	comps = &comps[tas_hda->priv->index];
683 	if (comps->dev)
684 		return -EBUSY;
685 
686 	codec = comps->codec;
687 	subid = codec->core.subsystem_id >> 16;
688 
689 	switch (subid) {
690 	case 0x17aa:
691 		tas_hda->priv->catlog_id = LENOVO;
692 		break;
693 	default:
694 		tas_hda->priv->catlog_id = OTHERS;
695 		break;
696 	}
697 
698 	pm_runtime_get_sync(dev);
699 
700 	comps->dev = dev;
701 
702 	strscpy(comps->name, dev_name(dev), sizeof(comps->name));
703 
704 	ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
705 	if (!ret)
706 		comps->playback_hook = tas2781_hda_playback_hook;
707 
708 	pm_runtime_mark_last_busy(dev);
709 	pm_runtime_put_autosuspend(dev);
710 
711 	return ret;
712 }
713 
714 static void tas2781_hda_unbind(struct device *dev,
715 	struct device *master, void *master_data)
716 {
717 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
718 	struct hda_component *comps = master_data;
719 	comps = &comps[tas_hda->priv->index];
720 
721 	if (comps->dev == dev) {
722 		comps->dev = NULL;
723 		memset(comps->name, 0, sizeof(comps->name));
724 		comps->playback_hook = NULL;
725 	}
726 
727 	tas2781_hda_remove_controls(tas_hda);
728 
729 	tasdevice_config_info_remove(tas_hda->priv);
730 	tasdevice_dsp_remove(tas_hda->priv);
731 
732 	tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
733 }
734 
735 static const struct component_ops tas2781_hda_comp_ops = {
736 	.bind = tas2781_hda_bind,
737 	.unbind = tas2781_hda_unbind,
738 };
739 
740 static void tas2781_hda_remove(struct device *dev)
741 {
742 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
743 
744 	pm_runtime_get_sync(tas_hda->dev);
745 	pm_runtime_disable(tas_hda->dev);
746 
747 	component_del(tas_hda->dev, &tas2781_hda_comp_ops);
748 
749 	pm_runtime_put_noidle(tas_hda->dev);
750 
751 	tasdevice_remove(tas_hda->priv);
752 }
753 
754 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
755 {
756 	struct tas2781_hda *tas_hda;
757 	const char *device_name;
758 	int ret;
759 
760 
761 	tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
762 	if (!tas_hda)
763 		return -ENOMEM;
764 
765 	dev_set_drvdata(&clt->dev, tas_hda);
766 	tas_hda->dev = &clt->dev;
767 
768 	tas_hda->priv = tasdevice_kzalloc(clt);
769 	if (!tas_hda->priv)
770 		return -ENOMEM;
771 
772 	if (strstr(dev_name(&clt->dev), "TIAS2781")) {
773 		device_name = "TIAS2781";
774 		tas_hda->priv->save_calibration = tas2781_save_calibration;
775 		tas_hda->priv->apply_calibration = tas2781_apply_calib;
776 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
777 	} else if (strstr(dev_name(&clt->dev), "INT8866")) {
778 		device_name = "INT8866";
779 		tas_hda->priv->save_calibration = tas2563_save_calibration;
780 		tas_hda->priv->apply_calibration = tas2563_apply_calib;
781 		tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
782 	} else
783 		return -ENODEV;
784 
785 	tas_hda->priv->irq_info.irq = clt->irq;
786 	ret = tas2781_read_acpi(tas_hda->priv, device_name);
787 	if (ret)
788 		return dev_err_probe(tas_hda->dev, ret,
789 			"Platform not supported\n");
790 
791 	ret = tasdevice_init(tas_hda->priv);
792 	if (ret)
793 		goto err;
794 
795 	pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
796 	pm_runtime_use_autosuspend(tas_hda->dev);
797 	pm_runtime_mark_last_busy(tas_hda->dev);
798 	pm_runtime_set_active(tas_hda->dev);
799 	pm_runtime_enable(tas_hda->dev);
800 
801 	tas2781_reset(tas_hda->priv);
802 
803 	ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
804 	if (ret) {
805 		dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
806 		pm_runtime_disable(tas_hda->dev);
807 	}
808 
809 err:
810 	if (ret)
811 		tas2781_hda_remove(&clt->dev);
812 	return ret;
813 }
814 
815 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
816 {
817 	tas2781_hda_remove(&clt->dev);
818 }
819 
820 static int tas2781_runtime_suspend(struct device *dev)
821 {
822 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
823 
824 	dev_dbg(tas_hda->dev, "Runtime Suspend\n");
825 
826 	mutex_lock(&tas_hda->priv->codec_lock);
827 
828 	/* The driver powers up the amplifiers at module load time.
829 	 * Stop the playback if it's unused.
830 	 */
831 	if (tas_hda->priv->playback_started) {
832 		tasdevice_tuning_switch(tas_hda->priv, 1);
833 		tas_hda->priv->playback_started = false;
834 	}
835 
836 	mutex_unlock(&tas_hda->priv->codec_lock);
837 
838 	return 0;
839 }
840 
841 static int tas2781_runtime_resume(struct device *dev)
842 {
843 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
844 
845 	dev_dbg(tas_hda->dev, "Runtime Resume\n");
846 
847 	mutex_lock(&tas_hda->priv->codec_lock);
848 
849 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
850 
851 	/* If calibrated data occurs error, dsp will still works with default
852 	 * calibrated data inside algo.
853 	 */
854 	tasdevice_apply_calibration(tas_hda->priv);
855 
856 	mutex_unlock(&tas_hda->priv->codec_lock);
857 
858 	return 0;
859 }
860 
861 static int tas2781_system_suspend(struct device *dev)
862 {
863 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
864 
865 	dev_dbg(tas_hda->priv->dev, "System Suspend\n");
866 
867 	mutex_lock(&tas_hda->priv->codec_lock);
868 
869 	/* Shutdown chip before system suspend */
870 	if (tas_hda->priv->playback_started)
871 		tasdevice_tuning_switch(tas_hda->priv, 1);
872 
873 	mutex_unlock(&tas_hda->priv->codec_lock);
874 
875 	/*
876 	 * Reset GPIO may be shared, so cannot reset here.
877 	 * However beyond this point, amps may be powered down.
878 	 */
879 	return 0;
880 }
881 
882 static int tas2781_system_resume(struct device *dev)
883 {
884 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
885 	int i;
886 
887 	dev_dbg(tas_hda->priv->dev, "System Resume\n");
888 
889 	mutex_lock(&tas_hda->priv->codec_lock);
890 
891 	for (i = 0; i < tas_hda->priv->ndev; i++) {
892 		tas_hda->priv->tasdevice[i].cur_book = -1;
893 		tas_hda->priv->tasdevice[i].cur_prog = -1;
894 		tas_hda->priv->tasdevice[i].cur_conf = -1;
895 	}
896 	tas2781_reset(tas_hda->priv);
897 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
898 
899 	/* If calibrated data occurs error, dsp will still work with default
900 	 * calibrated data inside algo.
901 	 */
902 	tasdevice_apply_calibration(tas_hda->priv);
903 
904 	if (tas_hda->priv->playback_started)
905 		tasdevice_tuning_switch(tas_hda->priv, 0);
906 
907 	mutex_unlock(&tas_hda->priv->codec_lock);
908 
909 	return 0;
910 }
911 
912 static const struct dev_pm_ops tas2781_hda_pm_ops = {
913 	RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
914 	SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
915 };
916 
917 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
918 	{ "tas2781-hda", 0 },
919 	{}
920 };
921 
922 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
923 	{"TIAS2781", 0 },
924 	{"INT8866", 0 },
925 	{}
926 };
927 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
928 
929 static struct i2c_driver tas2781_hda_i2c_driver = {
930 	.driver = {
931 		.name		= "tas2781-hda",
932 		.acpi_match_table = tas2781_acpi_hda_match,
933 		.pm		= &tas2781_hda_pm_ops,
934 	},
935 	.id_table	= tas2781_hda_i2c_id,
936 	.probe		= tas2781_hda_i2c_probe,
937 	.remove		= tas2781_hda_i2c_remove,
938 };
939 module_i2c_driver(tas2781_hda_i2c_driver);
940 
941 MODULE_DESCRIPTION("TAS2781 HDA Driver");
942 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
943 MODULE_LICENSE("GPL");
944 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);
945