xref: /linux/sound/pci/hda/tas2781_hda_i2c.c (revision a9e6060bb2a6cae6d43a98ec0794844ad01273d3)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA I2C driver
4 //
5 // Copyright 2023 - 2025 Texas Instruments, Inc.
6 //
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
8 // Current maintainer: Baojun Xu <baojun.xu@ti.com>
9 
10 #include <linux/unaligned.h>
11 #include <linux/acpi.h>
12 #include <linux/crc8.h>
13 #include <linux/crc32.h>
14 #include <linux/efi.h>
15 #include <linux/firmware.h>
16 #include <linux/i2c.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/pci_ids.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <sound/hda_codec.h>
23 #include <sound/soc.h>
24 #include <sound/tas2781.h>
25 #include <sound/tas2781-comlib-i2c.h>
26 #include <sound/tlv.h>
27 #include <sound/tas2781-tlv.h>
28 
29 #include "hda_local.h"
30 #include "hda_auto_parser.h"
31 #include "hda_component.h"
32 #include "hda_jack.h"
33 #include "hda_generic.h"
34 #include "tas2781_hda.h"
35 
36 #define TAS2563_CAL_VAR_NAME_MAX	16
37 #define TAS2563_CAL_ARRAY_SIZE		80
38 #define TAS2563_CAL_DATA_SIZE		4
39 #define TAS2563_MAX_CHANNELS		4
40 #define TAS2563_CAL_CH_SIZE		20
41 
42 #define TAS2563_CAL_R0_LOW		TASDEVICE_REG(0, 0x0f, 0x48)
43 #define TAS2563_CAL_POWER		TASDEVICE_REG(0, 0x0d, 0x3c)
44 #define TAS2563_CAL_INVR0		TASDEVICE_REG(0, 0x0f, 0x40)
45 #define TAS2563_CAL_TLIM		TASDEVICE_REG(0, 0x10, 0x14)
46 #define TAS2563_CAL_R0			TASDEVICE_REG(0, 0x0f, 0x34)
47 
48 struct tas2781_hda_i2c_priv {
49 	struct snd_kcontrol *snd_ctls[2];
50 	int (*save_calibration)(struct tas2781_hda *h);
51 };
52 
tas2781_get_i2c_res(struct acpi_resource * ares,void * data)53 static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
54 {
55 	struct tasdevice_priv *tas_priv = data;
56 	struct acpi_resource_i2c_serialbus *sb;
57 
58 	if (i2c_acpi_get_i2c_resource(ares, &sb)) {
59 		if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS &&
60 			sb->slave_address != tas_priv->global_addr) {
61 			tas_priv->tasdevice[tas_priv->ndev].dev_addr =
62 				(unsigned int)sb->slave_address;
63 			tas_priv->ndev++;
64 		}
65 	}
66 	return 1;
67 }
68 
69 static const struct acpi_gpio_params speakerid_gpios = { 0, 0, false };
70 
71 static const struct acpi_gpio_mapping tas2781_speaker_id_gpios[] = {
72 	{ "speakerid-gpios", &speakerid_gpios, 1 },
73 	{ }
74 };
75 
tas2781_read_acpi(struct tasdevice_priv * p,const char * hid)76 static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
77 {
78 	struct acpi_device *adev;
79 	struct device *physdev;
80 	LIST_HEAD(resources);
81 	const char *sub;
82 	uint32_t subid;
83 	int ret;
84 
85 	adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
86 	if (!adev) {
87 		dev_err(p->dev,
88 			"Failed to find an ACPI device for %s\n", hid);
89 		return -ENODEV;
90 	}
91 
92 	physdev = get_device(acpi_get_first_physical_node(adev));
93 	ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
94 	if (ret < 0) {
95 		dev_err(p->dev, "Failed to get ACPI resource.\n");
96 		goto err;
97 	}
98 	sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
99 	if (IS_ERR(sub)) {
100 		/* No subsys id in older tas2563 projects. */
101 		if (!strncmp(hid, "INT8866", sizeof("INT8866")))
102 			goto end_2563;
103 		dev_err(p->dev, "Failed to get SUBSYS ID.\n");
104 		ret = PTR_ERR(sub);
105 		goto err;
106 	}
107 	/* Speaker id was needed for ASUS projects. */
108 	ret = kstrtou32(sub, 16, &subid);
109 	if (!ret && upper_16_bits(subid) == PCI_VENDOR_ID_ASUSTEK) {
110 		ret = devm_acpi_dev_add_driver_gpios(p->dev,
111 			tas2781_speaker_id_gpios);
112 		if (ret < 0)
113 			dev_err(p->dev, "Failed to add driver gpio %d.\n",
114 				ret);
115 		p->speaker_id = devm_gpiod_get(p->dev, "speakerid", GPIOD_IN);
116 		if (IS_ERR(p->speaker_id)) {
117 			dev_err(p->dev, "Failed to get Speaker id.\n");
118 			ret = PTR_ERR(p->speaker_id);
119 			goto err;
120 		}
121 	} else {
122 		p->speaker_id = NULL;
123 	}
124 
125 end_2563:
126 	acpi_dev_free_resource_list(&resources);
127 	strscpy(p->dev_name, hid, sizeof(p->dev_name));
128 	put_device(physdev);
129 	acpi_dev_put(adev);
130 
131 	return 0;
132 
133 err:
134 	dev_err(p->dev, "read acpi error, ret: %d\n", ret);
135 	put_device(physdev);
136 	acpi_dev_put(adev);
137 
138 	return ret;
139 }
140 
tas2781_hda_playback_hook(struct device * dev,int action)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 		break;
165 	}
166 }
167 
tas2781_amp_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)168 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
169 	struct snd_ctl_elem_value *ucontrol)
170 {
171 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
172 	struct soc_mixer_control *mc =
173 		(struct soc_mixer_control *)kcontrol->private_value;
174 	int ret;
175 
176 	mutex_lock(&tas_priv->codec_lock);
177 
178 	ret = tasdevice_amp_getvol(tas_priv, ucontrol, mc);
179 
180 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %ld\n",
181 		__func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
182 
183 	mutex_unlock(&tas_priv->codec_lock);
184 
185 	return ret;
186 }
187 
tas2781_amp_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)188 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
189 	struct snd_ctl_elem_value *ucontrol)
190 {
191 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
192 	struct soc_mixer_control *mc =
193 		(struct soc_mixer_control *)kcontrol->private_value;
194 	int ret;
195 
196 	mutex_lock(&tas_priv->codec_lock);
197 
198 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: -> %ld\n",
199 		__func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
200 
201 	/* The check of the given value is in tasdevice_amp_putvol. */
202 	ret = tasdevice_amp_putvol(tas_priv, ucontrol, mc);
203 
204 	mutex_unlock(&tas_priv->codec_lock);
205 
206 	return ret;
207 }
208 
tas2781_force_fwload_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)209 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
210 	struct snd_ctl_elem_value *ucontrol)
211 {
212 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
213 
214 	mutex_lock(&tas_priv->codec_lock);
215 
216 	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
217 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
218 		__func__, kcontrol->id.name, tas_priv->force_fwload_status);
219 
220 	mutex_unlock(&tas_priv->codec_lock);
221 
222 	return 0;
223 }
224 
tas2781_force_fwload_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)225 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
226 	struct snd_ctl_elem_value *ucontrol)
227 {
228 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
229 	bool change, val = (bool)ucontrol->value.integer.value[0];
230 
231 	mutex_lock(&tas_priv->codec_lock);
232 
233 	dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
234 		__func__, kcontrol->id.name,
235 		tas_priv->force_fwload_status, val);
236 
237 	if (tas_priv->force_fwload_status == val)
238 		change = false;
239 	else {
240 		change = true;
241 		tas_priv->force_fwload_status = val;
242 	}
243 
244 	mutex_unlock(&tas_priv->codec_lock);
245 
246 	return change;
247 }
248 
249 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
250 	ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
251 		1, 0, 20, 0, tas2781_amp_getvol,
252 		tas2781_amp_putvol, amp_vol_tlv),
253 	ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
254 		tas2781_force_fwload_get, tas2781_force_fwload_put),
255 };
256 
257 static const struct snd_kcontrol_new tas2781_prof_ctrl = {
258 	.name = "Speaker Profile Id",
259 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
260 	.info = tasdevice_info_profile,
261 	.get = tasdevice_get_profile_id,
262 	.put = tasdevice_set_profile_id,
263 };
264 
265 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = {
266 	.name = "Speaker Program Id",
267 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
268 	.info = tasdevice_info_programs,
269 	.get = tasdevice_program_get,
270 	.put = tasdevice_program_put,
271 };
272 
273 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = {
274 	.name = "Speaker Config Id",
275 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
276 	.info = tasdevice_info_config,
277 	.get = tasdevice_config_get,
278 	.put = tasdevice_config_put,
279 };
280 
tas2563_save_calibration(struct tas2781_hda * h)281 static int tas2563_save_calibration(struct tas2781_hda *h)
282 {
283 	efi_guid_t efi_guid = tasdev_fct_efi_guid[LENOVO];
284 	char *vars[TASDEV_CALIB_N] = {
285 		"R0_%d", "InvR0_%d", "R0_Low_%d", "Power_%d", "TLim_%d"
286 	};
287 	efi_char16_t efi_name[TAS2563_CAL_VAR_NAME_MAX];
288 	unsigned long max_size = TAS2563_CAL_DATA_SIZE;
289 	unsigned char var8[TAS2563_CAL_VAR_NAME_MAX];
290 	struct tasdevice_priv *p = h->hda_priv;
291 	struct calidata *cd = &p->cali_data;
292 	struct cali_reg *r = &cd->cali_reg_array;
293 	unsigned int offset = 0;
294 	unsigned char *data;
295 	efi_status_t status;
296 	unsigned int attr;
297 	int ret, i, j, k;
298 
299 	cd->cali_dat_sz_per_dev = TAS2563_CAL_DATA_SIZE * TASDEV_CALIB_N;
300 
301 	/* extra byte for each device is the device number */
302 	cd->total_sz = (cd->cali_dat_sz_per_dev + 1) * p->ndev;
303 	data = cd->data = devm_kzalloc(p->dev, cd->total_sz,
304 		GFP_KERNEL);
305 	if (!data)
306 		return -ENOMEM;
307 
308 	for (i = 0; i < p->ndev; ++i) {
309 		data[offset] = i;
310 		offset++;
311 		for (j = 0; j < TASDEV_CALIB_N; ++j) {
312 			ret = snprintf(var8, sizeof(var8), vars[j], i);
313 
314 			if (ret < 0 || ret >= sizeof(var8) - 1) {
315 				dev_err(p->dev, "%s: Read %s failed\n",
316 					__func__, var8);
317 				return -EINVAL;
318 			}
319 			/*
320 			 * Our variable names are ASCII by construction, but
321 			 * EFI names are wide chars.  Convert and zero-pad.
322 			 */
323 			memset(efi_name, 0, sizeof(efi_name));
324 			for (k = 0; k < sizeof(var8) && var8[k]; k++)
325 				efi_name[k] = var8[k];
326 			status = efi.get_variable(efi_name,
327 				&efi_guid, &attr, &max_size,
328 				&data[offset]);
329 			if (status != EFI_SUCCESS ||
330 				max_size != TAS2563_CAL_DATA_SIZE) {
331 				dev_warn(p->dev,
332 					"Dev %d: Caldat[%d] read failed %ld\n",
333 					i, j, status);
334 				return -EINVAL;
335 			}
336 			offset += TAS2563_CAL_DATA_SIZE;
337 		}
338 	}
339 
340 	if (cd->total_sz != offset) {
341 		dev_err(p->dev, "%s: tot_size(%lu) and offset(%u) dismatch\n",
342 			__func__, cd->total_sz, offset);
343 		return -EINVAL;
344 	}
345 
346 	r->r0_reg = TAS2563_CAL_R0;
347 	r->invr0_reg = TAS2563_CAL_INVR0;
348 	r->r0_low_reg = TAS2563_CAL_R0_LOW;
349 	r->pow_reg = TAS2563_CAL_POWER;
350 	r->tlimit_reg = TAS2563_CAL_TLIM;
351 
352 	/*
353 	 * TAS2781_FMWLIB supports two solutions of calibrated data. One is
354 	 * from the driver itself: driver reads the calibrated files directly
355 	 * during probe; The other from user space: during init of audio hal,
356 	 * the audio hal will pass the calibrated data via kcontrol interface.
357 	 * Driver will store this data in "struct calidata" for use. For hda
358 	 * device, calibrated data are usunally saved into UEFI. So Hda side
359 	 * codec driver use the mixture of these two solutions, driver reads
360 	 * the data from UEFI, then store this data in "struct calidata" for
361 	 * use.
362 	 */
363 	p->is_user_space_calidata = true;
364 
365 	return 0;
366 }
367 
tas2781_hda_remove_controls(struct tas2781_hda * tas_hda)368 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
369 {
370 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
371 	struct hda_codec *codec = tas_hda->priv->codec;
372 
373 	snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
374 	snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
375 
376 	for (int i = ARRAY_SIZE(hda_priv->snd_ctls) - 1; i >= 0; i--)
377 		snd_ctl_remove(codec->card, hda_priv->snd_ctls[i]);
378 
379 	snd_ctl_remove(codec->card, tas_hda->prof_ctl);
380 }
381 
tasdev_fw_ready(const struct firmware * fmw,void * context)382 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
383 {
384 	struct tasdevice_priv *tas_priv = context;
385 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
386 	struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
387 	struct hda_codec *codec = tas_priv->codec;
388 	int i, ret, spk_id;
389 
390 	pm_runtime_get_sync(tas_priv->dev);
391 	mutex_lock(&tas_priv->codec_lock);
392 
393 	ret = tasdevice_rca_parser(tas_priv, fmw);
394 	if (ret)
395 		goto out;
396 
397 	tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv);
398 	ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
399 	if (ret) {
400 		dev_err(tas_priv->dev,
401 			"Failed to add KControl %s = %d\n",
402 			tas2781_prof_ctrl.name, ret);
403 		goto out;
404 	}
405 
406 	for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) {
407 		hda_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i],
408 			tas_priv);
409 		ret = snd_ctl_add(codec->card, hda_priv->snd_ctls[i]);
410 		if (ret) {
411 			dev_err(tas_priv->dev,
412 				"Failed to add KControl %s = %d\n",
413 				tas2781_snd_controls[i].name, ret);
414 			goto out;
415 		}
416 	}
417 
418 	tasdevice_dsp_remove(tas_priv);
419 
420 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
421 	if (tas_priv->speaker_id != NULL) {
422 		// Speaker id need to be checked for ASUS only.
423 		spk_id = gpiod_get_value(tas_priv->speaker_id);
424 		if (spk_id < 0) {
425 			// Speaker id is not valid, use default.
426 			dev_dbg(tas_priv->dev, "Wrong spk_id = %d\n", spk_id);
427 			spk_id = 0;
428 		}
429 		snprintf(tas_priv->coef_binaryname,
430 			  sizeof(tas_priv->coef_binaryname),
431 			  "TAS2XXX%04X%d.bin",
432 			  lower_16_bits(codec->core.subsystem_id),
433 			  spk_id);
434 	} else {
435 		snprintf(tas_priv->coef_binaryname,
436 			  sizeof(tas_priv->coef_binaryname),
437 			  "TAS2XXX%04X.bin",
438 			  lower_16_bits(codec->core.subsystem_id));
439 	}
440 	ret = tasdevice_dsp_parser(tas_priv);
441 	if (ret) {
442 		dev_err(tas_priv->dev, "dspfw load %s error\n",
443 			tas_priv->coef_binaryname);
444 		tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
445 		goto out;
446 	}
447 
448 	tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl,
449 		tas_priv);
450 	ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
451 	if (ret) {
452 		dev_err(tas_priv->dev,
453 			"Failed to add KControl %s = %d\n",
454 			tas2781_dsp_prog_ctrl.name, ret);
455 		goto out;
456 	}
457 
458 	tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl,
459 		tas_priv);
460 	ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
461 	if (ret) {
462 		dev_err(tas_priv->dev,
463 			"Failed to add KControl %s = %d\n",
464 			tas2781_dsp_conf_ctrl.name, ret);
465 		goto out;
466 	}
467 
468 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
469 	tasdevice_prmg_load(tas_priv, 0);
470 	if (tas_priv->fmw->nr_programs > 0)
471 		tas_priv->cur_prog = 0;
472 	if (tas_priv->fmw->nr_configurations > 0)
473 		tas_priv->cur_conf = 0;
474 
475 	/* If calibrated data occurs error, dsp will still works with default
476 	 * calibrated data inside algo.
477 	 */
478 	hda_priv->save_calibration(tas_hda);
479 
480 	tasdevice_tuning_switch(tas_hda->priv, 0);
481 	tas_hda->priv->playback_started = true;
482 
483 out:
484 	mutex_unlock(&tas_hda->priv->codec_lock);
485 	release_firmware(fmw);
486 	pm_runtime_mark_last_busy(tas_hda->dev);
487 	pm_runtime_put_autosuspend(tas_hda->dev);
488 }
489 
tas2781_hda_bind(struct device * dev,struct device * master,void * master_data)490 static int tas2781_hda_bind(struct device *dev, struct device *master,
491 	void *master_data)
492 {
493 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
494 	struct hda_component_parent *parent = master_data;
495 	struct hda_component *comp;
496 	struct hda_codec *codec;
497 	unsigned int subid;
498 	int ret;
499 
500 	comp = hda_component_from_index(parent, tas_hda->priv->index);
501 	if (!comp)
502 		return -EINVAL;
503 
504 	if (comp->dev)
505 		return -EBUSY;
506 
507 	codec = parent->codec;
508 	subid = codec->core.subsystem_id >> 16;
509 
510 	switch (subid) {
511 	case 0x1028:
512 		tas_hda->catlog_id = DELL;
513 		break;
514 	default:
515 		tas_hda->catlog_id = LENOVO;
516 		break;
517 	}
518 
519 	pm_runtime_get_sync(dev);
520 
521 	comp->dev = dev;
522 
523 	strscpy(comp->name, dev_name(dev), sizeof(comp->name));
524 
525 	ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
526 	if (!ret)
527 		comp->playback_hook = tas2781_hda_playback_hook;
528 
529 	pm_runtime_mark_last_busy(dev);
530 	pm_runtime_put_autosuspend(dev);
531 
532 	return ret;
533 }
534 
tas2781_hda_unbind(struct device * dev,struct device * master,void * master_data)535 static void tas2781_hda_unbind(struct device *dev,
536 	struct device *master, void *master_data)
537 {
538 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
539 	struct hda_component_parent *parent = master_data;
540 	struct hda_component *comp;
541 
542 	comp = hda_component_from_index(parent, tas_hda->priv->index);
543 	if (comp && (comp->dev == dev)) {
544 		comp->dev = NULL;
545 		memset(comp->name, 0, sizeof(comp->name));
546 		comp->playback_hook = NULL;
547 	}
548 
549 	tas2781_hda_remove_controls(tas_hda);
550 
551 	tasdevice_config_info_remove(tas_hda->priv);
552 	tasdevice_dsp_remove(tas_hda->priv);
553 
554 	tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
555 }
556 
557 static const struct component_ops tas2781_hda_comp_ops = {
558 	.bind = tas2781_hda_bind,
559 	.unbind = tas2781_hda_unbind,
560 };
561 
tas2781_hda_i2c_probe(struct i2c_client * clt)562 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
563 {
564 	struct tas2781_hda_i2c_priv *hda_priv;
565 	struct tas2781_hda *tas_hda;
566 	const char *device_name;
567 	int ret;
568 
569 	tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
570 	if (!tas_hda)
571 		return -ENOMEM;
572 
573 	hda_priv = devm_kzalloc(&clt->dev, sizeof(*hda_priv), GFP_KERNEL);
574 	if (!hda_priv)
575 		return -ENOMEM;
576 
577 	tas_hda->hda_priv = hda_priv;
578 
579 	dev_set_drvdata(&clt->dev, tas_hda);
580 	tas_hda->dev = &clt->dev;
581 
582 	tas_hda->priv = tasdevice_kzalloc(clt);
583 	if (!tas_hda->priv)
584 		return -ENOMEM;
585 
586 	if (strstr(dev_name(&clt->dev), "TIAS2781")) {
587 		device_name = "TIAS2781";
588 		hda_priv->save_calibration = tas2781_save_calibration;
589 		tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
590 	} else if (strstr(dev_name(&clt->dev), "INT8866")) {
591 		device_name = "INT8866";
592 		hda_priv->save_calibration = tas2563_save_calibration;
593 		tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
594 	} else
595 		return -ENODEV;
596 
597 	tas_hda->priv->irq = clt->irq;
598 	ret = tas2781_read_acpi(tas_hda->priv, device_name);
599 	if (ret)
600 		return dev_err_probe(tas_hda->dev, ret,
601 			"Platform not supported\n");
602 
603 	ret = tasdevice_init(tas_hda->priv);
604 	if (ret)
605 		goto err;
606 
607 	pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
608 	pm_runtime_use_autosuspend(tas_hda->dev);
609 	pm_runtime_mark_last_busy(tas_hda->dev);
610 	pm_runtime_set_active(tas_hda->dev);
611 	pm_runtime_enable(tas_hda->dev);
612 
613 	tasdevice_reset(tas_hda->priv);
614 
615 	ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
616 	if (ret) {
617 		dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
618 		pm_runtime_disable(tas_hda->dev);
619 	}
620 
621 err:
622 	if (ret)
623 		tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
624 	return ret;
625 }
626 
tas2781_hda_i2c_remove(struct i2c_client * clt)627 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
628 {
629 	tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
630 }
631 
tas2781_runtime_suspend(struct device * dev)632 static int tas2781_runtime_suspend(struct device *dev)
633 {
634 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
635 
636 	dev_dbg(tas_hda->dev, "Runtime Suspend\n");
637 
638 	mutex_lock(&tas_hda->priv->codec_lock);
639 
640 	/* The driver powers up the amplifiers at module load time.
641 	 * Stop the playback if it's unused.
642 	 */
643 	if (tas_hda->priv->playback_started) {
644 		tasdevice_tuning_switch(tas_hda->priv, 1);
645 		tas_hda->priv->playback_started = false;
646 	}
647 
648 	mutex_unlock(&tas_hda->priv->codec_lock);
649 
650 	return 0;
651 }
652 
tas2781_runtime_resume(struct device * dev)653 static int tas2781_runtime_resume(struct device *dev)
654 {
655 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
656 
657 	dev_dbg(tas_hda->dev, "Runtime Resume\n");
658 
659 	mutex_lock(&tas_hda->priv->codec_lock);
660 
661 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
662 
663 	mutex_unlock(&tas_hda->priv->codec_lock);
664 
665 	return 0;
666 }
667 
tas2781_system_suspend(struct device * dev)668 static int tas2781_system_suspend(struct device *dev)
669 {
670 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
671 
672 	dev_dbg(tas_hda->priv->dev, "System Suspend\n");
673 
674 	mutex_lock(&tas_hda->priv->codec_lock);
675 
676 	/* Shutdown chip before system suspend */
677 	if (tas_hda->priv->playback_started)
678 		tasdevice_tuning_switch(tas_hda->priv, 1);
679 
680 	mutex_unlock(&tas_hda->priv->codec_lock);
681 
682 	/*
683 	 * Reset GPIO may be shared, so cannot reset here.
684 	 * However beyond this point, amps may be powered down.
685 	 */
686 	return 0;
687 }
688 
tas2781_system_resume(struct device * dev)689 static int tas2781_system_resume(struct device *dev)
690 {
691 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
692 	int i;
693 
694 	dev_dbg(tas_hda->priv->dev, "System Resume\n");
695 
696 	mutex_lock(&tas_hda->priv->codec_lock);
697 
698 	for (i = 0; i < tas_hda->priv->ndev; i++) {
699 		tas_hda->priv->tasdevice[i].cur_book = -1;
700 		tas_hda->priv->tasdevice[i].cur_prog = -1;
701 		tas_hda->priv->tasdevice[i].cur_conf = -1;
702 	}
703 	tasdevice_reset(tas_hda->priv);
704 	tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
705 
706 	if (tas_hda->priv->playback_started)
707 		tasdevice_tuning_switch(tas_hda->priv, 0);
708 
709 	mutex_unlock(&tas_hda->priv->codec_lock);
710 
711 	return 0;
712 }
713 
714 static const struct dev_pm_ops tas2781_hda_pm_ops = {
715 	RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
716 	SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
717 };
718 
719 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
720 	{ "tas2781-hda" },
721 	{}
722 };
723 
724 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
725 	{"TIAS2781", 0 },
726 	{"INT8866", 0 },
727 	{}
728 };
729 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
730 
731 static struct i2c_driver tas2781_hda_i2c_driver = {
732 	.driver = {
733 		.name		= "tas2781-hda",
734 		.acpi_match_table = tas2781_acpi_hda_match,
735 		.pm		= &tas2781_hda_pm_ops,
736 	},
737 	.id_table	= tas2781_hda_i2c_id,
738 	.probe		= tas2781_hda_i2c_probe,
739 	.remove		= tas2781_hda_i2c_remove,
740 };
741 module_i2c_driver(tas2781_hda_i2c_driver);
742 
743 MODULE_DESCRIPTION("TAS2781 HDA Driver");
744 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
745 MODULE_LICENSE("GPL");
746 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
747 MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");
748