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