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