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