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