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