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