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