rust: upgrade to Rust 1.76.0
[sfrench/cifs-2.6.git] / sound / pci / hda / tas2781_hda_i2c.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA I2C driver
4 //
5 // Copyright 2023 Texas Instruments, Inc.
6 //
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
8
9 #include <linux/acpi.h>
10 #include <linux/crc8.h>
11 #include <linux/crc32.h>
12 #include <linux/efi.h>
13 #include <linux/firmware.h>
14 #include <linux/i2c.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <sound/hda_codec.h>
20 #include <sound/soc.h>
21 #include <sound/tas2781.h>
22 #include <sound/tlv.h>
23 #include <sound/tas2781-tlv.h>
24
25 #include "hda_local.h"
26 #include "hda_auto_parser.h"
27 #include "hda_component.h"
28 #include "hda_jack.h"
29 #include "hda_generic.h"
30
31 #define TASDEVICE_SPEAKER_CALIBRATION_SIZE      20
32
33 /* No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD
34  * Define two controls, one is Volume control callbacks, the other is
35  * flag setting control callbacks.
36  */
37
38 /* Volume control callbacks for tas2781 */
39 #define ACARD_SINGLE_RANGE_EXT_TLV(xname, xreg, xshift, xmin, xmax, xinvert, \
40         xhandler_get, xhandler_put, tlv_array) \
41 {       .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = (xname),\
42         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
43                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
44         .tlv.p = (tlv_array), \
45         .info = snd_soc_info_volsw_range, \
46         .get = xhandler_get, .put = xhandler_put, \
47         .private_value = (unsigned long)&(struct soc_mixer_control) \
48                 {.reg = xreg, .rreg = xreg, .shift = xshift, \
49                  .rshift = xshift, .min = xmin, .max = xmax, \
50                  .invert = xinvert} }
51
52 /* Flag control callbacks for tas2781 */
53 #define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
54 {       .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, \
55         .info = snd_ctl_boolean_mono_info, \
56         .get = xhandler_get, .put = xhandler_put, \
57         .private_value = xdata }
58
59 enum calib_data {
60         R0_VAL = 0,
61         INV_R0,
62         R0LOW,
63         POWER,
64         TLIM,
65         CALIB_MAX
66 };
67
68 #define TAS2563_MAX_CHANNELS    4
69
70 #define TAS2563_CAL_POWER       TASDEVICE_REG(0, 0x0d, 0x3c)
71 #define TAS2563_CAL_R0          TASDEVICE_REG(0, 0x0f, 0x34)
72 #define TAS2563_CAL_INVR0       TASDEVICE_REG(0, 0x0f, 0x40)
73 #define TAS2563_CAL_R0_LOW      TASDEVICE_REG(0, 0x0f, 0x48)
74 #define TAS2563_CAL_TLIM        TASDEVICE_REG(0, 0x10, 0x14)
75 #define TAS2563_CAL_N           5
76 #define TAS2563_CAL_DATA_SIZE   4
77 #define TAS2563_CAL_CH_SIZE     20
78 #define TAS2563_CAL_ARRAY_SIZE  80
79
80 static unsigned int cal_regs[TAS2563_CAL_N] = {
81         TAS2563_CAL_POWER, TAS2563_CAL_R0, TAS2563_CAL_INVR0,
82         TAS2563_CAL_R0_LOW, TAS2563_CAL_TLIM,
83 };
84
85
86 struct tas2781_hda {
87         struct device *dev;
88         struct tasdevice_priv *priv;
89         struct snd_kcontrol *dsp_prog_ctl;
90         struct snd_kcontrol *dsp_conf_ctl;
91         struct snd_kcontrol *prof_ctl;
92         struct snd_kcontrol *snd_ctls[3];
93 };
94
95 static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
96 {
97         struct tasdevice_priv *tas_priv = data;
98         struct acpi_resource_i2c_serialbus *sb;
99
100         if (i2c_acpi_get_i2c_resource(ares, &sb)) {
101                 if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS &&
102                         sb->slave_address != tas_priv->global_addr) {
103                         tas_priv->tasdevice[tas_priv->ndev].dev_addr =
104                                 (unsigned int)sb->slave_address;
105                         tas_priv->ndev++;
106                 }
107         }
108         return 1;
109 }
110
111 static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
112 {
113         struct acpi_device *adev;
114         struct device *physdev;
115         LIST_HEAD(resources);
116         const char *sub;
117         int ret;
118
119         adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
120         if (!adev) {
121                 dev_err(p->dev,
122                         "Failed to find an ACPI device for %s\n", hid);
123                 return -ENODEV;
124         }
125
126         ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
127         if (ret < 0)
128                 goto err;
129
130         acpi_dev_free_resource_list(&resources);
131         strscpy(p->dev_name, hid, sizeof(p->dev_name));
132         physdev = get_device(acpi_get_first_physical_node(adev));
133         acpi_dev_put(adev);
134
135         /* No side-effect to the playback even if subsystem_id is NULL*/
136         sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
137         if (IS_ERR(sub))
138                 sub = NULL;
139
140         p->acpi_subsystem_id = sub;
141
142         put_device(physdev);
143
144         return 0;
145
146 err:
147         dev_err(p->dev, "read acpi error, ret: %d\n", ret);
148         acpi_dev_put(adev);
149
150         return ret;
151 }
152
153 static void tas2781_hda_playback_hook(struct device *dev, int action)
154 {
155         struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
156
157         dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
158         switch (action) {
159         case HDA_GEN_PCM_ACT_OPEN:
160                 pm_runtime_get_sync(dev);
161                 mutex_lock(&tas_hda->priv->codec_lock);
162                 tasdevice_tuning_switch(tas_hda->priv, 0);
163                 mutex_unlock(&tas_hda->priv->codec_lock);
164                 break;
165         case HDA_GEN_PCM_ACT_CLOSE:
166                 mutex_lock(&tas_hda->priv->codec_lock);
167                 tasdevice_tuning_switch(tas_hda->priv, 1);
168                 mutex_unlock(&tas_hda->priv->codec_lock);
169
170                 pm_runtime_mark_last_busy(dev);
171                 pm_runtime_put_autosuspend(dev);
172                 break;
173         default:
174                 dev_dbg(tas_hda->dev, "Playback action not supported: %d\n",
175                         action);
176                 break;
177         }
178 }
179
180 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
181                         struct snd_ctl_elem_info *uinfo)
182 {
183         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
184
185         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
186         uinfo->count = 1;
187         uinfo->value.integer.min = 0;
188         uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
189
190         return 0;
191 }
192
193 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
194                         struct snd_ctl_elem_value *ucontrol)
195 {
196         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
197
198         ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
199
200         return 0;
201 }
202
203 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
204                 struct snd_ctl_elem_value *ucontrol)
205 {
206         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
207         int nr_profile = ucontrol->value.integer.value[0];
208         int max = tas_priv->rcabin.ncfgs - 1;
209         int val, ret = 0;
210
211         val = clamp(nr_profile, 0, max);
212
213         if (tas_priv->rcabin.profile_cfg_id != val) {
214                 tas_priv->rcabin.profile_cfg_id = val;
215                 ret = 1;
216         }
217
218         return ret;
219 }
220
221 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
222                         struct snd_ctl_elem_info *uinfo)
223 {
224         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
225         struct tasdevice_fw *tas_fw = tas_priv->fmw;
226
227         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
228         uinfo->count = 1;
229         uinfo->value.integer.min = 0;
230         uinfo->value.integer.max = tas_fw->nr_programs - 1;
231
232         return 0;
233 }
234
235 static int tasdevice_info_config(struct snd_kcontrol *kcontrol,
236         struct snd_ctl_elem_info *uinfo)
237 {
238         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
239         struct tasdevice_fw *tas_fw = tas_priv->fmw;
240
241         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
242         uinfo->count = 1;
243         uinfo->value.integer.min = 0;
244         uinfo->value.integer.max = tas_fw->nr_configurations - 1;
245
246         return 0;
247 }
248
249 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
250         struct snd_ctl_elem_value *ucontrol)
251 {
252         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
253
254         ucontrol->value.integer.value[0] = tas_priv->cur_prog;
255
256         return 0;
257 }
258
259 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
260         struct snd_ctl_elem_value *ucontrol)
261 {
262         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
263         struct tasdevice_fw *tas_fw = tas_priv->fmw;
264         int nr_program = ucontrol->value.integer.value[0];
265         int max = tas_fw->nr_programs - 1;
266         int val, ret = 0;
267
268         val = clamp(nr_program, 0, max);
269
270         if (tas_priv->cur_prog != val) {
271                 tas_priv->cur_prog = val;
272                 ret = 1;
273         }
274
275         return ret;
276 }
277
278 static int tasdevice_config_get(struct snd_kcontrol *kcontrol,
279         struct snd_ctl_elem_value *ucontrol)
280 {
281         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
282
283         ucontrol->value.integer.value[0] = tas_priv->cur_conf;
284
285         return 0;
286 }
287
288 static int tasdevice_config_put(struct snd_kcontrol *kcontrol,
289         struct snd_ctl_elem_value *ucontrol)
290 {
291         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
292         struct tasdevice_fw *tas_fw = tas_priv->fmw;
293         int nr_config = ucontrol->value.integer.value[0];
294         int max = tas_fw->nr_configurations - 1;
295         int val, ret = 0;
296
297         val = clamp(nr_config, 0, max);
298
299         if (tas_priv->cur_conf != val) {
300                 tas_priv->cur_conf = val;
301                 ret = 1;
302         }
303
304         return ret;
305 }
306
307 /*
308  * tas2781_digital_getvol - get the volum control
309  * @kcontrol: control pointer
310  * @ucontrol: User data
311  * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
312  * depends on internal regmap mechanism.
313  * tas2781 contains book and page two-level register map, especially
314  * book switching will set the register BXXP00R7F, after switching to the
315  * correct book, then leverage the mechanism for paging to access the
316  * register.
317  */
318 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
319         struct snd_ctl_elem_value *ucontrol)
320 {
321         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
322         struct soc_mixer_control *mc =
323                 (struct soc_mixer_control *)kcontrol->private_value;
324
325         return tasdevice_digital_getvol(tas_priv, ucontrol, mc);
326 }
327
328 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
329         struct snd_ctl_elem_value *ucontrol)
330 {
331         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
332         struct soc_mixer_control *mc =
333                 (struct soc_mixer_control *)kcontrol->private_value;
334
335         return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
336 }
337
338 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
339         struct snd_ctl_elem_value *ucontrol)
340 {
341         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
342         struct soc_mixer_control *mc =
343                 (struct soc_mixer_control *)kcontrol->private_value;
344
345         /* The check of the given value is in tasdevice_digital_putvol. */
346         return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
347 }
348
349 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
350         struct snd_ctl_elem_value *ucontrol)
351 {
352         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
353         struct soc_mixer_control *mc =
354                 (struct soc_mixer_control *)kcontrol->private_value;
355
356         /* The check of the given value is in tasdevice_amp_putvol. */
357         return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
358 }
359
360 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
361         struct snd_ctl_elem_value *ucontrol)
362 {
363         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
364
365         ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
366         dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
367                         tas_priv->force_fwload_status ? "ON" : "OFF");
368
369         return 0;
370 }
371
372 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
373         struct snd_ctl_elem_value *ucontrol)
374 {
375         struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
376         bool change, val = (bool)ucontrol->value.integer.value[0];
377
378         if (tas_priv->force_fwload_status == val)
379                 change = false;
380         else {
381                 change = true;
382                 tas_priv->force_fwload_status = val;
383         }
384         dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
385                 tas_priv->force_fwload_status ? "ON" : "OFF");
386
387         return change;
388 }
389
390 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
391         ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
392                 1, 0, 20, 0, tas2781_amp_getvol,
393                 tas2781_amp_putvol, amp_vol_tlv),
394         ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,
395                 0, 0, 200, 1, tas2781_digital_getvol,
396                 tas2781_digital_putvol, dvc_tlv),
397         ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
398                 tas2781_force_fwload_get, tas2781_force_fwload_put),
399 };
400
401 static const struct snd_kcontrol_new tas2781_prof_ctrl = {
402         .name = "Speaker Profile Id",
403         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
404         .info = tasdevice_info_profile,
405         .get = tasdevice_get_profile_id,
406         .put = tasdevice_set_profile_id,
407 };
408
409 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = {
410         .name = "Speaker Program Id",
411         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
412         .info = tasdevice_info_programs,
413         .get = tasdevice_program_get,
414         .put = tasdevice_program_put,
415 };
416
417 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = {
418         .name = "Speaker Config Id",
419         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
420         .info = tasdevice_info_config,
421         .get = tasdevice_config_get,
422         .put = tasdevice_config_put,
423 };
424
425 static void tas2563_apply_calib(struct tasdevice_priv *tas_priv)
426 {
427         int offset = 0;
428         __be32 data;
429         int ret;
430
431         for (int i = 0; i < tas_priv->ndev; i++) {
432                 for (int j = 0; j < TAS2563_CAL_N; ++j) {
433                         data = cpu_to_be32(
434                                 *(uint32_t *)&tas_priv->cali_data.data[offset]);
435                         ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j],
436                                 (unsigned char *)&data, TAS2563_CAL_DATA_SIZE);
437                         if (ret)
438                                 dev_err(tas_priv->dev,
439                                         "Error writing calib regs\n");
440                         offset += TAS2563_CAL_DATA_SIZE;
441                 }
442         }
443 }
444
445 static int tas2563_save_calibration(struct tasdevice_priv *tas_priv)
446 {
447         static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
448                 0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
449
450         static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = {
451                 { L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" },
452                 { L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" },
453                 { L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" },
454                 { L"Power_4", L"R0_4", L"InvR0_4", L"R0_Low_4", L"TLim_4" },
455         };
456
457         unsigned long max_size = TAS2563_CAL_DATA_SIZE;
458         unsigned int offset = 0;
459         efi_status_t status;
460         unsigned int attr;
461
462         tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
463                         TAS2563_CAL_ARRAY_SIZE, GFP_KERNEL);
464         if (!tas_priv->cali_data.data)
465                 return -ENOMEM;
466
467         for (int i = 0; i < tas_priv->ndev; ++i) {
468                 for (int j = 0; j < TAS2563_CAL_N; ++j) {
469                         status = efi.get_variable(efi_vars[i][j],
470                                 &efi_guid, &attr, &max_size,
471                                 &tas_priv->cali_data.data[offset]);
472                         if (status != EFI_SUCCESS ||
473                                 max_size != TAS2563_CAL_DATA_SIZE) {
474                                 dev_warn(tas_priv->dev,
475                                 "Calibration data read failed %ld\n", status);
476                                 return -EINVAL;
477                         }
478                         offset += TAS2563_CAL_DATA_SIZE;
479                 }
480         }
481
482         tas_priv->cali_data.total_sz = offset;
483         tasdevice_apply_calibration(tas_priv);
484
485         return 0;
486 }
487
488 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv)
489 {
490         static const unsigned char page_array[CALIB_MAX] = {
491                 0x17, 0x18, 0x18, 0x0d, 0x18
492         };
493         static const unsigned char rgno_array[CALIB_MAX] = {
494                 0x74, 0x0c, 0x14, 0x3c, 0x7c
495         };
496         unsigned char *data;
497         int i, j, rc;
498
499         for (i = 0; i < tas_priv->ndev; i++) {
500                 data = tas_priv->cali_data.data +
501                         i * TASDEVICE_SPEAKER_CALIBRATION_SIZE;
502                 for (j = 0; j < CALIB_MAX; j++) {
503                         rc = tasdevice_dev_bulk_write(tas_priv, i,
504                                 TASDEVICE_REG(0, page_array[j], rgno_array[j]),
505                                 &(data[4 * j]), 4);
506                         if (rc < 0)
507                                 dev_err(tas_priv->dev,
508                                         "chn %d calib %d bulk_wr err = %d\n",
509                                         i, j, rc);
510                 }
511         }
512 }
513
514 /* Update the calibration data, including speaker impedance, f0, etc, into algo.
515  * Calibrate data is done by manufacturer in the factory. These data are used
516  * by Algo for calculating the speaker temperature, speaker membrane excursion
517  * and f0 in real time during playback.
518  */
519 static int tas2781_save_calibration(struct tasdevice_priv *tas_priv)
520 {
521         efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d,
522                 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
523         static efi_char16_t efi_name[] = L"CALI_DATA";
524         struct tm *tm = &tas_priv->tm;
525         unsigned int attr, crc;
526         unsigned int *tmp_val;
527         efi_status_t status;
528
529         /* Lenovo devices */
530         if (tas_priv->catlog_id == LENOVO)
531                 efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09,
532                         0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
533
534         tas_priv->cali_data.total_sz = 0;
535         /* Get real size of UEFI variable */
536         status = efi.get_variable(efi_name, &efi_guid, &attr,
537                 &tas_priv->cali_data.total_sz, tas_priv->cali_data.data);
538         if (status == EFI_BUFFER_TOO_SMALL) {
539                 /* Allocate data buffer of data_size bytes */
540                 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
541                         tas_priv->cali_data.total_sz, GFP_KERNEL);
542                 if (!tas_priv->cali_data.data)
543                         return -ENOMEM;
544                 /* Get variable contents into buffer */
545                 status = efi.get_variable(efi_name, &efi_guid, &attr,
546                         &tas_priv->cali_data.total_sz,
547                         tas_priv->cali_data.data);
548         }
549         if (status != EFI_SUCCESS)
550                 return -EINVAL;
551
552         tmp_val = (unsigned int *)tas_priv->cali_data.data;
553
554         crc = crc32(~0, tas_priv->cali_data.data, 84) ^ ~0;
555         dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n",
556                 crc, tmp_val[21]);
557
558         if (crc == tmp_val[21]) {
559                 time64_to_tm(tmp_val[20], 0, tm);
560                 dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n",
561                         tm->tm_year, tm->tm_mon, tm->tm_mday,
562                         tm->tm_hour, tm->tm_min, tm->tm_sec);
563                 tasdevice_apply_calibration(tas_priv);
564         } else
565                 tas_priv->cali_data.total_sz = 0;
566
567         return 0;
568 }
569
570 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
571 {
572         struct hda_codec *codec = tas_hda->priv->codec;
573
574         if (tas_hda->dsp_prog_ctl)
575                 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
576
577         if (tas_hda->dsp_conf_ctl)
578                 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
579
580         for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
581                 if (tas_hda->snd_ctls[i])
582                         snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
583
584         if (tas_hda->prof_ctl)
585                 snd_ctl_remove(codec->card, tas_hda->prof_ctl);
586 }
587
588 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
589 {
590         struct tasdevice_priv *tas_priv = context;
591         struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
592         struct hda_codec *codec = tas_priv->codec;
593         int i, ret;
594
595         pm_runtime_get_sync(tas_priv->dev);
596         mutex_lock(&tas_priv->codec_lock);
597
598         ret = tasdevice_rca_parser(tas_priv, fmw);
599         if (ret)
600                 goto out;
601
602         tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv);
603         ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
604         if (ret) {
605                 dev_err(tas_priv->dev,
606                         "Failed to add KControl %s = %d\n",
607                         tas2781_prof_ctrl.name, ret);
608                 goto out;
609         }
610
611         for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) {
612                 tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i],
613                         tas_priv);
614                 ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
615                 if (ret) {
616                         dev_err(tas_priv->dev,
617                                 "Failed to add KControl %s = %d\n",
618                                 tas2781_snd_controls[i].name, ret);
619                         goto out;
620                 }
621         }
622
623         tasdevice_dsp_remove(tas_priv);
624
625         tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
626         scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X.bin",
627                 codec->core.subsystem_id & 0xffff);
628         ret = tasdevice_dsp_parser(tas_priv);
629         if (ret) {
630                 dev_err(tas_priv->dev, "dspfw load %s error\n",
631                         tas_priv->coef_binaryname);
632                 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
633                 goto out;
634         }
635
636         tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl,
637                 tas_priv);
638         ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
639         if (ret) {
640                 dev_err(tas_priv->dev,
641                         "Failed to add KControl %s = %d\n",
642                         tas2781_dsp_prog_ctrl.name, ret);
643                 goto out;
644         }
645
646         tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl,
647                 tas_priv);
648         ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
649         if (ret) {
650                 dev_err(tas_priv->dev,
651                         "Failed to add KControl %s = %d\n",
652                         tas2781_dsp_conf_ctrl.name, ret);
653                 goto out;
654         }
655
656         tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
657         tasdevice_prmg_load(tas_priv, 0);
658         if (tas_priv->fmw->nr_programs > 0)
659                 tas_priv->cur_prog = 0;
660         if (tas_priv->fmw->nr_configurations > 0)
661                 tas_priv->cur_conf = 0;
662
663         /* If calibrated data occurs error, dsp will still works with default
664          * calibrated data inside algo.
665          */
666         tasdevice_save_calibration(tas_priv);
667
668         tasdevice_tuning_switch(tas_hda->priv, 0);
669
670 out:
671         mutex_unlock(&tas_hda->priv->codec_lock);
672         if (fmw)
673                 release_firmware(fmw);
674         pm_runtime_mark_last_busy(tas_hda->dev);
675         pm_runtime_put_autosuspend(tas_hda->dev);
676 }
677
678 static int tas2781_hda_bind(struct device *dev, struct device *master,
679         void *master_data)
680 {
681         struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
682         struct hda_component *comps = master_data;
683         struct hda_codec *codec;
684         unsigned int subid;
685         int ret;
686
687         if (!comps || tas_hda->priv->index < 0 ||
688                 tas_hda->priv->index >= HDA_MAX_COMPONENTS)
689                 return -EINVAL;
690
691         comps = &comps[tas_hda->priv->index];
692         if (comps->dev)
693                 return -EBUSY;
694
695         codec = comps->codec;
696         subid = codec->core.subsystem_id >> 16;
697
698         switch (subid) {
699         case 0x17aa:
700                 tas_hda->priv->catlog_id = LENOVO;
701                 break;
702         default:
703                 tas_hda->priv->catlog_id = OTHERS;
704                 break;
705         }
706
707         pm_runtime_get_sync(dev);
708
709         comps->dev = dev;
710
711         strscpy(comps->name, dev_name(dev), sizeof(comps->name));
712
713         ret = tascodec_init(tas_hda->priv, codec, tasdev_fw_ready);
714         if (!ret)
715                 comps->playback_hook = tas2781_hda_playback_hook;
716
717         pm_runtime_mark_last_busy(dev);
718         pm_runtime_put_autosuspend(dev);
719
720         return ret;
721 }
722
723 static void tas2781_hda_unbind(struct device *dev,
724         struct device *master, void *master_data)
725 {
726         struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
727         struct hda_component *comps = master_data;
728         comps = &comps[tas_hda->priv->index];
729
730         if (comps->dev == dev) {
731                 comps->dev = NULL;
732                 memset(comps->name, 0, sizeof(comps->name));
733                 comps->playback_hook = NULL;
734         }
735
736         tas2781_hda_remove_controls(tas_hda);
737
738         tasdevice_config_info_remove(tas_hda->priv);
739         tasdevice_dsp_remove(tas_hda->priv);
740
741         tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
742 }
743
744 static const struct component_ops tas2781_hda_comp_ops = {
745         .bind = tas2781_hda_bind,
746         .unbind = tas2781_hda_unbind,
747 };
748
749 static void tas2781_hda_remove(struct device *dev)
750 {
751         struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
752
753         pm_runtime_get_sync(tas_hda->dev);
754         pm_runtime_disable(tas_hda->dev);
755
756         component_del(tas_hda->dev, &tas2781_hda_comp_ops);
757
758         pm_runtime_put_noidle(tas_hda->dev);
759
760         tasdevice_remove(tas_hda->priv);
761 }
762
763 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
764 {
765         struct tas2781_hda *tas_hda;
766         const char *device_name;
767         int ret;
768
769
770         tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
771         if (!tas_hda)
772                 return -ENOMEM;
773
774         dev_set_drvdata(&clt->dev, tas_hda);
775         tas_hda->dev = &clt->dev;
776
777         tas_hda->priv = tasdevice_kzalloc(clt);
778         if (!tas_hda->priv)
779                 return -ENOMEM;
780
781         if (strstr(dev_name(&clt->dev), "TIAS2781")) {
782                 device_name = "TIAS2781";
783                 tas_hda->priv->save_calibration = tas2781_save_calibration;
784                 tas_hda->priv->apply_calibration = tas2781_apply_calib;
785                 tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
786         } else if (strstr(dev_name(&clt->dev), "INT8866")) {
787                 device_name = "INT8866";
788                 tas_hda->priv->save_calibration = tas2563_save_calibration;
789                 tas_hda->priv->apply_calibration = tas2563_apply_calib;
790                 tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
791         } else
792                 return -ENODEV;
793
794         tas_hda->priv->irq_info.irq = clt->irq;
795         ret = tas2781_read_acpi(tas_hda->priv, device_name);
796         if (ret)
797                 return dev_err_probe(tas_hda->dev, ret,
798                         "Platform not supported\n");
799
800         ret = tasdevice_init(tas_hda->priv);
801         if (ret)
802                 goto err;
803
804         pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
805         pm_runtime_use_autosuspend(tas_hda->dev);
806         pm_runtime_mark_last_busy(tas_hda->dev);
807         pm_runtime_set_active(tas_hda->dev);
808         pm_runtime_get_noresume(tas_hda->dev);
809         pm_runtime_enable(tas_hda->dev);
810
811         pm_runtime_put_autosuspend(tas_hda->dev);
812
813         tas2781_reset(tas_hda->priv);
814
815         ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
816         if (ret) {
817                 dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
818                 pm_runtime_disable(tas_hda->dev);
819         }
820
821 err:
822         if (ret)
823                 tas2781_hda_remove(&clt->dev);
824         return ret;
825 }
826
827 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
828 {
829         tas2781_hda_remove(&clt->dev);
830 }
831
832 static int tas2781_runtime_suspend(struct device *dev)
833 {
834         struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
835         int i;
836
837         dev_dbg(tas_hda->dev, "Runtime Suspend\n");
838
839         mutex_lock(&tas_hda->priv->codec_lock);
840
841         if (tas_hda->priv->playback_started) {
842                 tasdevice_tuning_switch(tas_hda->priv, 1);
843                 tas_hda->priv->playback_started = false;
844         }
845
846         for (i = 0; i < tas_hda->priv->ndev; i++) {
847                 tas_hda->priv->tasdevice[i].cur_book = -1;
848                 tas_hda->priv->tasdevice[i].cur_prog = -1;
849                 tas_hda->priv->tasdevice[i].cur_conf = -1;
850         }
851
852         mutex_unlock(&tas_hda->priv->codec_lock);
853
854         return 0;
855 }
856
857 static int tas2781_runtime_resume(struct device *dev)
858 {
859         struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
860
861         dev_dbg(tas_hda->dev, "Runtime Resume\n");
862
863         mutex_lock(&tas_hda->priv->codec_lock);
864
865         tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
866
867         /* If calibrated data occurs error, dsp will still works with default
868          * calibrated data inside algo.
869          */
870         tasdevice_apply_calibration(tas_hda->priv);
871
872         mutex_unlock(&tas_hda->priv->codec_lock);
873
874         return 0;
875 }
876
877 static int tas2781_system_suspend(struct device *dev)
878 {
879         struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
880         int ret;
881
882         dev_dbg(tas_hda->priv->dev, "System Suspend\n");
883
884         ret = pm_runtime_force_suspend(dev);
885         if (ret)
886                 return ret;
887
888         /* Shutdown chip before system suspend */
889         tasdevice_tuning_switch(tas_hda->priv, 1);
890
891         /*
892          * Reset GPIO may be shared, so cannot reset here.
893          * However beyond this point, amps may be powered down.
894          */
895         return 0;
896 }
897
898 static int tas2781_system_resume(struct device *dev)
899 {
900         struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
901         int i, ret;
902
903         dev_info(tas_hda->priv->dev, "System Resume\n");
904
905         ret = pm_runtime_force_resume(dev);
906         if (ret)
907                 return ret;
908
909         mutex_lock(&tas_hda->priv->codec_lock);
910
911         for (i = 0; i < tas_hda->priv->ndev; i++) {
912                 tas_hda->priv->tasdevice[i].cur_book = -1;
913                 tas_hda->priv->tasdevice[i].cur_prog = -1;
914                 tas_hda->priv->tasdevice[i].cur_conf = -1;
915         }
916         tas2781_reset(tas_hda->priv);
917         tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
918
919         /* If calibrated data occurs error, dsp will still work with default
920          * calibrated data inside algo.
921          */
922         tasdevice_apply_calibration(tas_hda->priv);
923         mutex_unlock(&tas_hda->priv->codec_lock);
924
925         return 0;
926 }
927
928 static const struct dev_pm_ops tas2781_hda_pm_ops = {
929         RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
930         SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
931 };
932
933 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
934         { "tas2781-hda", 0 },
935         {}
936 };
937
938 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
939         {"TIAS2781", 0 },
940         {"INT8866", 0 },
941         {}
942 };
943 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
944
945 static struct i2c_driver tas2781_hda_i2c_driver = {
946         .driver = {
947                 .name           = "tas2781-hda",
948                 .acpi_match_table = tas2781_acpi_hda_match,
949                 .pm             = &tas2781_hda_pm_ops,
950         },
951         .id_table       = tas2781_hda_i2c_id,
952         .probe          = tas2781_hda_i2c_probe,
953         .remove         = tas2781_hda_i2c_remove,
954 };
955 module_i2c_driver(tas2781_hda_i2c_driver);
956
957 MODULE_DESCRIPTION("TAS2781 HDA Driver");
958 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
959 MODULE_LICENSE("GPL");
960 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);