powerpc/mm: Avoid calling arch_enter/leave_lazy_mmu() in set_ptes
[platform/kernel/linux-starfive.git] / sound / pci / hda / cs35l41_hda.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // CS35l41 ALSA HDA audio driver
4 //
5 // Copyright 2021 Cirrus Logic, Inc.
6 //
7 // Author: Lucas Tanure <tanureal@opensource.cirrus.com>
8
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <sound/hda_codec.h>
13 #include <sound/soc.h>
14 #include <linux/pm_runtime.h>
15 #include "hda_local.h"
16 #include "hda_auto_parser.h"
17 #include "hda_jack.h"
18 #include "hda_generic.h"
19 #include "hda_component.h"
20 #include "cs35l41_hda.h"
21 #include "hda_cs_dsp_ctl.h"
22 #include "cs35l41_hda_property.h"
23
24 #define CS35L41_FIRMWARE_ROOT "cirrus/"
25 #define CS35L41_PART "cs35l41"
26
27 #define HALO_STATE_DSP_CTL_NAME         "HALO_STATE"
28 #define HALO_STATE_DSP_CTL_TYPE         5
29 #define HALO_STATE_DSP_CTL_ALG          262308
30 #define CAL_R_DSP_CTL_NAME              "CAL_R"
31 #define CAL_STATUS_DSP_CTL_NAME         "CAL_STATUS"
32 #define CAL_CHECKSUM_DSP_CTL_NAME       "CAL_CHECKSUM"
33 #define CAL_AMBIENT_DSP_CTL_NAME        "CAL_AMBIENT"
34 #define CAL_DSP_CTL_TYPE                5
35 #define CAL_DSP_CTL_ALG                 205
36
37 static bool firmware_autostart = 1;
38 module_param(firmware_autostart, bool, 0444);
39 MODULE_PARM_DESC(firmware_autostart, "Allow automatic firmware download on boot"
40                              "(0=Disable, 1=Enable) (default=1); ");
41
42 static const struct reg_sequence cs35l41_hda_config[] = {
43         { CS35L41_PLL_CLK_CTRL,         0x00000430 }, // 3072000Hz, BCLK Input, PLL_REFCLK_EN = 1
44         { CS35L41_DSP_CLK_CTRL,         0x00000003 }, // DSP CLK EN
45         { CS35L41_GLOBAL_CLK_CTRL,      0x00000003 }, // GLOBAL_FS = 48 kHz
46         { CS35L41_SP_ENABLES,           0x00010000 }, // ASP_RX1_EN = 1
47         { CS35L41_SP_RATE_CTRL,         0x00000021 }, // ASP_BCLK_FREQ = 3.072 MHz
48         { CS35L41_SP_FORMAT,            0x20200200 }, // 32 bits RX/TX slots, I2S, clk consumer
49         { CS35L41_SP_HIZ_CTRL,          0x00000002 }, // Hi-Z unused
50         { CS35L41_SP_TX_WL,             0x00000018 }, // 24 cycles/slot
51         { CS35L41_SP_RX_WL,             0x00000018 }, // 24 cycles/slot
52         { CS35L41_DAC_PCM1_SRC,         0x00000008 }, // DACPCM1_SRC = ASPRX1
53         { CS35L41_ASP_TX1_SRC,          0x00000018 }, // ASPTX1 SRC = VMON
54         { CS35L41_ASP_TX2_SRC,          0x00000019 }, // ASPTX2 SRC = IMON
55         { CS35L41_ASP_TX3_SRC,          0x00000032 }, // ASPTX3 SRC = ERRVOL
56         { CS35L41_ASP_TX4_SRC,          0x00000033 }, // ASPTX4 SRC = CLASSH_TGT
57         { CS35L41_DSP1_RX1_SRC,         0x00000008 }, // DSP1RX1 SRC = ASPRX1
58         { CS35L41_DSP1_RX2_SRC,         0x00000009 }, // DSP1RX2 SRC = ASPRX2
59         { CS35L41_DSP1_RX3_SRC,         0x00000018 }, // DSP1RX3 SRC = VMON
60         { CS35L41_DSP1_RX4_SRC,         0x00000019 }, // DSP1RX4 SRC = IMON
61         { CS35L41_DSP1_RX5_SRC,         0x00000020 }, // DSP1RX5 SRC = ERRVOL
62 };
63
64 static const struct reg_sequence cs35l41_hda_config_dsp[] = {
65         { CS35L41_PLL_CLK_CTRL,         0x00000430 }, // 3072000Hz, BCLK Input, PLL_REFCLK_EN = 1
66         { CS35L41_DSP_CLK_CTRL,         0x00000003 }, // DSP CLK EN
67         { CS35L41_GLOBAL_CLK_CTRL,      0x00000003 }, // GLOBAL_FS = 48 kHz
68         { CS35L41_SP_ENABLES,           0x00010001 }, // ASP_RX1_EN = 1, ASP_TX1_EN = 1
69         { CS35L41_SP_RATE_CTRL,         0x00000021 }, // ASP_BCLK_FREQ = 3.072 MHz
70         { CS35L41_SP_FORMAT,            0x20200200 }, // 32 bits RX/TX slots, I2S, clk consumer
71         { CS35L41_SP_HIZ_CTRL,          0x00000003 }, // Hi-Z unused/disabled
72         { CS35L41_SP_TX_WL,             0x00000018 }, // 24 cycles/slot
73         { CS35L41_SP_RX_WL,             0x00000018 }, // 24 cycles/slot
74         { CS35L41_DAC_PCM1_SRC,         0x00000032 }, // DACPCM1_SRC = ERR_VOL
75         { CS35L41_ASP_TX1_SRC,          0x00000018 }, // ASPTX1 SRC = VMON
76         { CS35L41_ASP_TX2_SRC,          0x00000019 }, // ASPTX2 SRC = IMON
77         { CS35L41_ASP_TX3_SRC,          0x00000028 }, // ASPTX3 SRC = VPMON
78         { CS35L41_ASP_TX4_SRC,          0x00000029 }, // ASPTX4 SRC = VBSTMON
79         { CS35L41_DSP1_RX1_SRC,         0x00000008 }, // DSP1RX1 SRC = ASPRX1
80         { CS35L41_DSP1_RX2_SRC,         0x00000008 }, // DSP1RX2 SRC = ASPRX1
81         { CS35L41_DSP1_RX3_SRC,         0x00000018 }, // DSP1RX3 SRC = VMON
82         { CS35L41_DSP1_RX4_SRC,         0x00000019 }, // DSP1RX4 SRC = IMON
83         { CS35L41_DSP1_RX5_SRC,         0x00000029 }, // DSP1RX5 SRC = VBSTMON
84 };
85
86 static const struct reg_sequence cs35l41_hda_unmute[] = {
87         { CS35L41_AMP_DIG_VOL_CTRL,     0x00008000 }, // AMP_HPF_PCM_EN = 1, AMP_VOL_PCM  0.0 dB
88         { CS35L41_AMP_GAIN_CTRL,        0x00000084 }, // AMP_GAIN_PCM 4.5 dB
89 };
90
91 static const struct reg_sequence cs35l41_hda_unmute_dsp[] = {
92         { CS35L41_AMP_DIG_VOL_CTRL,     0x00008000 }, // AMP_HPF_PCM_EN = 1, AMP_VOL_PCM  0.0 dB
93         { CS35L41_AMP_GAIN_CTRL,        0x00000233 }, // AMP_GAIN_PCM = 17.5dB AMP_GAIN_PDM = 19.5dB
94 };
95
96 static const struct reg_sequence cs35l41_hda_mute[] = {
97         { CS35L41_AMP_GAIN_CTRL,        0x00000000 }, // AMP_GAIN_PCM 0.5 dB
98         { CS35L41_AMP_DIG_VOL_CTRL,     0x0000A678 }, // AMP_HPF_PCM_EN = 1, AMP_VOL_PCM Mute
99 };
100
101 static void cs35l41_add_controls(struct cs35l41_hda *cs35l41)
102 {
103         struct hda_cs_dsp_ctl_info info;
104
105         info.device_name = cs35l41->amp_name;
106         info.fw_type = cs35l41->firmware_type;
107         info.card = cs35l41->codec->card;
108
109         hda_cs_dsp_add_controls(&cs35l41->cs_dsp, &info);
110 }
111
112 static const struct cs_dsp_client_ops client_ops = {
113         .control_remove = hda_cs_dsp_control_remove,
114 };
115
116 static int cs35l41_request_firmware_file(struct cs35l41_hda *cs35l41,
117                                          const struct firmware **firmware, char **filename,
118                                          const char *dir, const char *ssid, const char *amp_name,
119                                          int spkid, const char *filetype)
120 {
121         const char * const dsp_name = cs35l41->cs_dsp.name;
122         char *s, c;
123         int ret = 0;
124
125         if (spkid > -1 && ssid && amp_name)
126                 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s-spkid%d-%s.%s", dir, CS35L41_PART,
127                                       dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
128                                       ssid, spkid, amp_name, filetype);
129         else if (spkid > -1 && ssid)
130                 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s-spkid%d.%s", dir, CS35L41_PART,
131                                       dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
132                                       ssid, spkid, filetype);
133         else if (ssid && amp_name)
134                 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s-%s.%s", dir, CS35L41_PART,
135                                       dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
136                                       ssid, amp_name, filetype);
137         else if (ssid)
138                 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s.%s", dir, CS35L41_PART,
139                                       dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
140                                       ssid, filetype);
141         else
142                 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s.%s", dir, CS35L41_PART,
143                                       dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
144                                       filetype);
145
146         if (*filename == NULL)
147                 return -ENOMEM;
148
149         /*
150          * Make sure that filename is lower-case and any non alpha-numeric
151          * characters except full stop and '/' are replaced with hyphens.
152          */
153         s = *filename;
154         while (*s) {
155                 c = *s;
156                 if (isalnum(c))
157                         *s = tolower(c);
158                 else if (c != '.' && c != '/')
159                         *s = '-';
160                 s++;
161         }
162
163         ret = firmware_request_nowarn(firmware, *filename, cs35l41->dev);
164         if (ret != 0) {
165                 dev_dbg(cs35l41->dev, "Failed to request '%s'\n", *filename);
166                 kfree(*filename);
167                 *filename = NULL;
168         }
169
170         return ret;
171 }
172
173 static int cs35l41_request_firmware_files_spkid(struct cs35l41_hda *cs35l41,
174                                                 const struct firmware **wmfw_firmware,
175                                                 char **wmfw_filename,
176                                                 const struct firmware **coeff_firmware,
177                                                 char **coeff_filename)
178 {
179         int ret;
180
181         /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.wmfw */
182         ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
183                                             CS35L41_FIRMWARE_ROOT,
184                                             cs35l41->acpi_subsystem_id, cs35l41->amp_name,
185                                             cs35l41->speaker_id, "wmfw");
186         if (!ret) {
187                 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
188                 return cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
189                                                      CS35L41_FIRMWARE_ROOT,
190                                                      cs35l41->acpi_subsystem_id, cs35l41->amp_name,
191                                                      cs35l41->speaker_id, "bin");
192         }
193
194         /* try cirrus/part-dspN-fwtype-sub<-ampname>.wmfw */
195         ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
196                                             CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
197                                             cs35l41->amp_name, -1, "wmfw");
198         if (!ret) {
199                 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
200                 return cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
201                                                      CS35L41_FIRMWARE_ROOT,
202                                                      cs35l41->acpi_subsystem_id, cs35l41->amp_name,
203                                                      cs35l41->speaker_id, "bin");
204         }
205
206         /* try cirrus/part-dspN-fwtype-sub<-spkidN>.wmfw */
207         ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
208                                             CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
209                                             NULL, cs35l41->speaker_id, "wmfw");
210         if (!ret) {
211                 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
212                 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
213                                                     CS35L41_FIRMWARE_ROOT,
214                                                     cs35l41->acpi_subsystem_id,
215                                                     cs35l41->amp_name, cs35l41->speaker_id, "bin");
216                 if (ret)
217                         /* try cirrus/part-dspN-fwtype-sub<-spkidN>.bin */
218                         return cs35l41_request_firmware_file(cs35l41, coeff_firmware,
219                                                              coeff_filename, CS35L41_FIRMWARE_ROOT,
220                                                              cs35l41->acpi_subsystem_id, NULL,
221                                                              cs35l41->speaker_id, "bin");
222         }
223
224         /* try cirrus/part-dspN-fwtype-sub.wmfw */
225         ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
226                                             CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
227                                             NULL, -1, "wmfw");
228         if (!ret) {
229                 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
230                 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
231                                                     CS35L41_FIRMWARE_ROOT,
232                                                     cs35l41->acpi_subsystem_id, cs35l41->amp_name,
233                                                     cs35l41->speaker_id, "bin");
234                 if (ret)
235                         /* try cirrus/part-dspN-fwtype-sub<-spkidN>.bin */
236                         return cs35l41_request_firmware_file(cs35l41, coeff_firmware,
237                                                              coeff_filename, CS35L41_FIRMWARE_ROOT,
238                                                              cs35l41->acpi_subsystem_id, NULL,
239                                                              cs35l41->speaker_id, "bin");
240         }
241
242         return ret;
243 }
244
245 static int cs35l41_request_firmware_files(struct cs35l41_hda *cs35l41,
246                                           const struct firmware **wmfw_firmware,
247                                           char **wmfw_filename,
248                                           const struct firmware **coeff_firmware,
249                                           char **coeff_filename)
250 {
251         int ret;
252
253         if (cs35l41->speaker_id > -1) {
254                 ret = cs35l41_request_firmware_files_spkid(cs35l41, wmfw_firmware, wmfw_filename,
255                                                            coeff_firmware, coeff_filename);
256                 goto out;
257
258         }
259
260         /* try cirrus/part-dspN-fwtype-sub<-ampname>.wmfw */
261         ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
262                                             CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
263                                             cs35l41->amp_name, -1, "wmfw");
264         if (!ret) {
265                 /* try cirrus/part-dspN-fwtype-sub<-ampname>.bin */
266                 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
267                                                     CS35L41_FIRMWARE_ROOT,
268                                                     cs35l41->acpi_subsystem_id, cs35l41->amp_name,
269                                                     -1, "bin");
270                 goto out;
271         }
272
273         /* try cirrus/part-dspN-fwtype-sub.wmfw */
274         ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
275                                             CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
276                                             NULL, -1, "wmfw");
277         if (!ret) {
278                 /* try cirrus/part-dspN-fwtype-sub<-ampname>.bin */
279                 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
280                                                     CS35L41_FIRMWARE_ROOT,
281                                                     cs35l41->acpi_subsystem_id,
282                                                     cs35l41->amp_name, -1, "bin");
283                 if (ret)
284                         /* try cirrus/part-dspN-fwtype-sub.bin */
285                         ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
286                                                             CS35L41_FIRMWARE_ROOT,
287                                                             cs35l41->acpi_subsystem_id, NULL, -1,
288                                                             "bin");
289         }
290
291 out:
292         if (!ret)
293                 return 0;
294
295         /* Handle fallback */
296         dev_warn(cs35l41->dev, "Falling back to default firmware.\n");
297
298         release_firmware(*wmfw_firmware);
299         kfree(*wmfw_filename);
300
301         /* fallback try cirrus/part-dspN-fwtype.wmfw */
302         ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
303                                             CS35L41_FIRMWARE_ROOT, NULL, NULL, -1, "wmfw");
304         if (!ret)
305                 /* fallback try cirrus/part-dspN-fwtype.bin */
306                 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
307                                                     CS35L41_FIRMWARE_ROOT, NULL, NULL, -1, "bin");
308
309         if (ret) {
310                 release_firmware(*wmfw_firmware);
311                 kfree(*wmfw_filename);
312                 dev_warn(cs35l41->dev, "Unable to find firmware and tuning\n");
313         }
314         return ret;
315 }
316
317 #if IS_ENABLED(CONFIG_EFI)
318 static int cs35l41_apply_calibration(struct cs35l41_hda *cs35l41, __be32 ambient, __be32 r0,
319                                      __be32 status, __be32 checksum)
320 {
321         int ret;
322
323         ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_AMBIENT_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
324                                    CAL_DSP_CTL_ALG, &ambient, 4);
325         if (ret) {
326                 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_AMBIENT_DSP_CTL_NAME,
327                         ret);
328                 return ret;
329         }
330         ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_R_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
331                                    CAL_DSP_CTL_ALG, &r0, 4);
332         if (ret) {
333                 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_R_DSP_CTL_NAME, ret);
334                 return ret;
335         }
336         ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_STATUS_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
337                                    CAL_DSP_CTL_ALG, &status, 4);
338         if (ret) {
339                 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_STATUS_DSP_CTL_NAME,
340                         ret);
341                 return ret;
342         }
343         ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_CHECKSUM_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
344                                    CAL_DSP_CTL_ALG, &checksum, 4);
345         if (ret) {
346                 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_CHECKSUM_DSP_CTL_NAME,
347                         ret);
348                 return ret;
349         }
350
351         return 0;
352 }
353
354 static int cs35l41_save_calibration(struct cs35l41_hda *cs35l41)
355 {
356         static efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 0x93, 0xfe,
357                                               0x5a, 0xa3, 0x5d, 0xb3);
358         static efi_char16_t efi_name[] = L"CirrusSmartAmpCalibrationData";
359         const struct cs35l41_amp_efi_data *efi_data;
360         const struct cs35l41_amp_cal_data *cl;
361         unsigned long data_size = 0;
362         efi_status_t status;
363         int ret = 0;
364         u8 *data = NULL;
365         u32 attr;
366
367         /* Get real size of UEFI variable */
368         status = efi.get_variable(efi_name, &efi_guid, &attr, &data_size, data);
369         if (status == EFI_BUFFER_TOO_SMALL) {
370                 ret = -ENODEV;
371                 /* Allocate data buffer of data_size bytes */
372                 data = vmalloc(data_size);
373                 if (!data)
374                         return -ENOMEM;
375                 /* Get variable contents into buffer */
376                 status = efi.get_variable(efi_name, &efi_guid, &attr, &data_size, data);
377                 if (status == EFI_SUCCESS) {
378                         efi_data = (struct cs35l41_amp_efi_data *)data;
379                         dev_dbg(cs35l41->dev, "Calibration: Size=%d, Amp Count=%d\n",
380                                 efi_data->size, efi_data->count);
381                         if (efi_data->count > cs35l41->index) {
382                                 cl = &efi_data->data[cs35l41->index];
383                                 dev_dbg(cs35l41->dev,
384                                         "Calibration: Ambient=%02x, Status=%02x, R0=%d\n",
385                                         cl->calAmbient, cl->calStatus, cl->calR);
386
387                                 /* Calibration can only be applied whilst the DSP is not running */
388                                 ret = cs35l41_apply_calibration(cs35l41,
389                                                                 cpu_to_be32(cl->calAmbient),
390                                                                 cpu_to_be32(cl->calR),
391                                                                 cpu_to_be32(cl->calStatus),
392                                                                 cpu_to_be32(cl->calR + 1));
393                         }
394                 }
395                 vfree(data);
396         }
397         return ret;
398 }
399 #else
400 static int cs35l41_save_calibration(struct cs35l41_hda *cs35l41)
401 {
402         dev_warn(cs35l41->dev, "Calibration not supported without EFI support.\n");
403         return 0;
404 }
405 #endif
406
407 static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
408 {
409         const struct firmware *coeff_firmware = NULL;
410         const struct firmware *wmfw_firmware = NULL;
411         struct cs_dsp *dsp = &cs35l41->cs_dsp;
412         char *coeff_filename = NULL;
413         char *wmfw_filename = NULL;
414         int ret;
415
416         if (!cs35l41->halo_initialized) {
417                 cs35l41_configure_cs_dsp(cs35l41->dev, cs35l41->regmap, dsp);
418                 dsp->client_ops = &client_ops;
419
420                 ret = cs_dsp_halo_init(&cs35l41->cs_dsp);
421                 if (ret)
422                         return ret;
423                 cs35l41->halo_initialized = true;
424         }
425
426         ret = cs35l41_request_firmware_files(cs35l41, &wmfw_firmware, &wmfw_filename,
427                                              &coeff_firmware, &coeff_filename);
428         if (ret < 0)
429                 return ret;
430
431         dev_dbg(cs35l41->dev, "Loading WMFW Firmware: %s\n", wmfw_filename);
432         if (coeff_filename)
433                 dev_dbg(cs35l41->dev, "Loading Coefficient File: %s\n", coeff_filename);
434         else
435                 dev_warn(cs35l41->dev, "No Coefficient File available.\n");
436
437         ret = cs_dsp_power_up(dsp, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename,
438                               hda_cs_dsp_fw_ids[cs35l41->firmware_type]);
439         if (ret)
440                 goto err_release;
441
442         cs35l41_add_controls(cs35l41);
443
444         ret = cs35l41_save_calibration(cs35l41);
445
446 err_release:
447         release_firmware(wmfw_firmware);
448         release_firmware(coeff_firmware);
449         kfree(wmfw_filename);
450         kfree(coeff_filename);
451
452         return ret;
453 }
454
455 static void cs35l41_shutdown_dsp(struct cs35l41_hda *cs35l41)
456 {
457         struct cs_dsp *dsp = &cs35l41->cs_dsp;
458
459         cs_dsp_stop(dsp);
460         cs_dsp_power_down(dsp);
461         cs35l41->firmware_running = false;
462         dev_dbg(cs35l41->dev, "Unloaded Firmware\n");
463 }
464
465 static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41)
466 {
467         struct cs_dsp *dsp = &cs35l41->cs_dsp;
468
469         cancel_work_sync(&cs35l41->fw_load_work);
470
471         mutex_lock(&cs35l41->fw_mutex);
472         cs35l41_shutdown_dsp(cs35l41);
473         cs_dsp_remove(dsp);
474         cs35l41->halo_initialized = false;
475         mutex_unlock(&cs35l41->fw_mutex);
476 }
477
478 /* Protection release cycle to get the speaker out of Safe-Mode */
479 static void cs35l41_error_release(struct device *dev, struct regmap *regmap, unsigned int mask)
480 {
481         regmap_write(regmap, CS35L41_PROTECT_REL_ERR_IGN, 0);
482         regmap_set_bits(regmap, CS35L41_PROTECT_REL_ERR_IGN, mask);
483         regmap_clear_bits(regmap, CS35L41_PROTECT_REL_ERR_IGN, mask);
484 }
485
486 /* Clear all errors to release safe mode. Global Enable must be cleared first. */
487 static void cs35l41_irq_release(struct cs35l41_hda *cs35l41)
488 {
489         cs35l41_error_release(cs35l41->dev, cs35l41->regmap, cs35l41->irq_errors);
490         cs35l41->irq_errors = 0;
491 }
492
493 static void cs35l41_hda_play_start(struct device *dev)
494 {
495         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
496         struct regmap *reg = cs35l41->regmap;
497
498         dev_dbg(dev, "Play (Start)\n");
499
500         if (cs35l41->playback_started) {
501                 dev_dbg(dev, "Playback already started.");
502                 return;
503         }
504
505         cs35l41->playback_started = true;
506
507         if (cs35l41->firmware_running) {
508                 regmap_multi_reg_write(reg, cs35l41_hda_config_dsp,
509                                        ARRAY_SIZE(cs35l41_hda_config_dsp));
510                 regmap_update_bits(reg, CS35L41_PWR_CTRL2,
511                                    CS35L41_VMON_EN_MASK | CS35L41_IMON_EN_MASK,
512                                    1 << CS35L41_VMON_EN_SHIFT | 1 << CS35L41_IMON_EN_SHIFT);
513                 cs35l41_set_cspl_mbox_cmd(cs35l41->dev, reg, CSPL_MBOX_CMD_RESUME);
514         } else {
515                 regmap_multi_reg_write(reg, cs35l41_hda_config, ARRAY_SIZE(cs35l41_hda_config));
516         }
517         regmap_update_bits(reg, CS35L41_PWR_CTRL2, CS35L41_AMP_EN_MASK, 1 << CS35L41_AMP_EN_SHIFT);
518         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
519                 regmap_write(reg, CS35L41_GPIO1_CTRL1, 0x00008001);
520
521 }
522
523 static void cs35l41_hda_play_done(struct device *dev)
524 {
525         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
526         struct regmap *reg = cs35l41->regmap;
527
528         dev_dbg(dev, "Play (Complete)\n");
529
530         cs35l41_global_enable(dev, reg, cs35l41->hw_cfg.bst_type, 1, NULL,
531                               cs35l41->firmware_running);
532         if (cs35l41->firmware_running) {
533                 regmap_multi_reg_write(reg, cs35l41_hda_unmute_dsp,
534                                        ARRAY_SIZE(cs35l41_hda_unmute_dsp));
535         } else {
536                 regmap_multi_reg_write(reg, cs35l41_hda_unmute,
537                                        ARRAY_SIZE(cs35l41_hda_unmute));
538         }
539 }
540
541 static void cs35l41_hda_pause_start(struct device *dev)
542 {
543         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
544         struct regmap *reg = cs35l41->regmap;
545
546         dev_dbg(dev, "Pause (Start)\n");
547
548         regmap_multi_reg_write(reg, cs35l41_hda_mute, ARRAY_SIZE(cs35l41_hda_mute));
549         cs35l41_global_enable(dev, reg, cs35l41->hw_cfg.bst_type, 0, NULL,
550                               cs35l41->firmware_running);
551 }
552
553 static void cs35l41_hda_pause_done(struct device *dev)
554 {
555         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
556         struct regmap *reg = cs35l41->regmap;
557
558         dev_dbg(dev, "Pause (Complete)\n");
559
560         regmap_update_bits(reg, CS35L41_PWR_CTRL2, CS35L41_AMP_EN_MASK, 0 << CS35L41_AMP_EN_SHIFT);
561         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
562                 regmap_write(reg, CS35L41_GPIO1_CTRL1, 0x00000001);
563         if (cs35l41->firmware_running) {
564                 cs35l41_set_cspl_mbox_cmd(dev, reg, CSPL_MBOX_CMD_PAUSE);
565                 regmap_update_bits(reg, CS35L41_PWR_CTRL2,
566                                    CS35L41_VMON_EN_MASK | CS35L41_IMON_EN_MASK,
567                                    0 << CS35L41_VMON_EN_SHIFT | 0 << CS35L41_IMON_EN_SHIFT);
568         }
569         cs35l41_irq_release(cs35l41);
570         cs35l41->playback_started = false;
571 }
572
573 static void cs35l41_hda_pre_playback_hook(struct device *dev, int action)
574 {
575         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
576
577         switch (action) {
578         case HDA_GEN_PCM_ACT_CLEANUP:
579                 mutex_lock(&cs35l41->fw_mutex);
580                 cs35l41_hda_pause_start(dev);
581                 mutex_unlock(&cs35l41->fw_mutex);
582                 break;
583         default:
584                 break;
585         }
586 }
587 static void cs35l41_hda_playback_hook(struct device *dev, int action)
588 {
589         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
590
591         switch (action) {
592         case HDA_GEN_PCM_ACT_OPEN:
593                 /*
594                  * All amps must be resumed before we can start playing back.
595                  * This ensures, for external boost, that all amps are in AMP_SAFE mode.
596                  * Do this in HDA_GEN_PCM_ACT_OPEN, since this is run prior to any of the
597                  * other actions.
598                  */
599                 pm_runtime_get_sync(dev);
600                 break;
601         case HDA_GEN_PCM_ACT_PREPARE:
602                 mutex_lock(&cs35l41->fw_mutex);
603                 cs35l41_hda_play_start(dev);
604                 mutex_unlock(&cs35l41->fw_mutex);
605                 break;
606         case HDA_GEN_PCM_ACT_CLEANUP:
607                 mutex_lock(&cs35l41->fw_mutex);
608                 cs35l41_hda_pause_done(dev);
609                 mutex_unlock(&cs35l41->fw_mutex);
610                 break;
611         case HDA_GEN_PCM_ACT_CLOSE:
612                 mutex_lock(&cs35l41->fw_mutex);
613                 if (!cs35l41->firmware_running && cs35l41->request_fw_load &&
614                     !cs35l41->fw_request_ongoing) {
615                         dev_info(dev, "Requesting Firmware Load after HDA_GEN_PCM_ACT_CLOSE\n");
616                         cs35l41->fw_request_ongoing = true;
617                         schedule_work(&cs35l41->fw_load_work);
618                 }
619                 mutex_unlock(&cs35l41->fw_mutex);
620
621                 /*
622                  * Playback must be finished for all amps before we start runtime suspend.
623                  * This ensures no amps are playing back when we start putting them to sleep.
624                  */
625                 pm_runtime_mark_last_busy(dev);
626                 pm_runtime_put_autosuspend(dev);
627                 break;
628         default:
629                 break;
630         }
631 }
632
633 static void cs35l41_hda_post_playback_hook(struct device *dev, int action)
634 {
635         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
636
637         switch (action) {
638         case HDA_GEN_PCM_ACT_PREPARE:
639                 mutex_lock(&cs35l41->fw_mutex);
640                 cs35l41_hda_play_done(dev);
641                 mutex_unlock(&cs35l41->fw_mutex);
642                 break;
643         default:
644                 break;
645         }
646 }
647
648 static int cs35l41_hda_channel_map(struct device *dev, unsigned int tx_num, unsigned int *tx_slot,
649                                     unsigned int rx_num, unsigned int *rx_slot)
650 {
651         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
652         static const char * const channel_name[] = { "L", "R" };
653
654         if (!cs35l41->amp_name) {
655                 if (*rx_slot >= ARRAY_SIZE(channel_name))
656                         return -EINVAL;
657
658                 cs35l41->amp_name = devm_kasprintf(cs35l41->dev, GFP_KERNEL, "%s%d",
659                                                    channel_name[*rx_slot], cs35l41->channel_index);
660                 if (!cs35l41->amp_name)
661                         return -ENOMEM;
662         }
663
664         return cs35l41_set_channels(cs35l41->dev, cs35l41->regmap, tx_num, tx_slot, rx_num,
665                                     rx_slot);
666 }
667
668 static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)
669 {
670         int ret = 0;
671
672         mutex_lock(&cs35l41->fw_mutex);
673         if (cs35l41->firmware_running) {
674
675                 regcache_cache_only(cs35l41->regmap, false);
676
677                 ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap);
678                 if (ret) {
679                         dev_warn(cs35l41->dev, "Unable to exit Hibernate.");
680                         goto err;
681                 }
682
683                 /* Test key needs to be unlocked to allow the OTP settings to re-apply */
684                 cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
685                 ret = regcache_sync(cs35l41->regmap);
686                 cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
687                 if (ret) {
688                         dev_err(cs35l41->dev, "Failed to restore register cache: %d\n", ret);
689                         goto err;
690                 }
691
692                 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
693                         cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, &cs35l41->hw_cfg);
694
695                 cs35l41_shutdown_dsp(cs35l41);
696                 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
697         }
698 err:
699         regcache_cache_only(cs35l41->regmap, true);
700         regcache_mark_dirty(cs35l41->regmap);
701
702         mutex_unlock(&cs35l41->fw_mutex);
703
704         return ret;
705 }
706
707 static int cs35l41_system_suspend_prep(struct device *dev)
708 {
709         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
710
711         dev_dbg(cs35l41->dev, "System Suspend Prepare\n");
712
713         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
714                 dev_err_once(cs35l41->dev, "System Suspend not supported\n");
715                 return 0; /* don't block the whole system suspend */
716         }
717
718         mutex_lock(&cs35l41->fw_mutex);
719         if (cs35l41->playback_started)
720                 cs35l41_hda_pause_start(dev);
721         mutex_unlock(&cs35l41->fw_mutex);
722
723         return 0;
724 }
725
726 static int cs35l41_system_suspend(struct device *dev)
727 {
728         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
729         int ret;
730
731         dev_dbg(cs35l41->dev, "System Suspend\n");
732
733         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
734                 dev_err_once(cs35l41->dev, "System Suspend not supported\n");
735                 return 0; /* don't block the whole system suspend */
736         }
737
738         mutex_lock(&cs35l41->fw_mutex);
739         if (cs35l41->playback_started)
740                 cs35l41_hda_pause_done(dev);
741         mutex_unlock(&cs35l41->fw_mutex);
742
743         ret = pm_runtime_force_suspend(dev);
744         if (ret) {
745                 dev_err(dev, "System Suspend Failed, unable to runtime suspend: %d\n", ret);
746                 return ret;
747         }
748
749         /* Shutdown DSP before system suspend */
750         ret = cs35l41_ready_for_reset(cs35l41);
751
752         if (ret)
753                 dev_err(dev, "System Suspend Failed, not ready for Reset: %d\n", ret);
754
755         /*
756          * Reset GPIO may be shared, so cannot reset here.
757          * However beyond this point, amps may be powered down.
758          */
759         return ret;
760 }
761
762 static int cs35l41_system_resume(struct device *dev)
763 {
764         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
765         int ret;
766
767         dev_dbg(cs35l41->dev, "System Resume\n");
768
769         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
770                 dev_err_once(cs35l41->dev, "System Resume not supported\n");
771                 return 0; /* don't block the whole system resume */
772         }
773
774         if (cs35l41->reset_gpio) {
775                 usleep_range(2000, 2100);
776                 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
777         }
778
779         usleep_range(2000, 2100);
780
781         ret = pm_runtime_force_resume(dev);
782         if (ret) {
783                 dev_err(dev, "System Resume Failed: Unable to runtime resume: %d\n", ret);
784                 return ret;
785         }
786
787         mutex_lock(&cs35l41->fw_mutex);
788
789         if (cs35l41->request_fw_load && !cs35l41->fw_request_ongoing) {
790                 cs35l41->fw_request_ongoing = true;
791                 schedule_work(&cs35l41->fw_load_work);
792         }
793         mutex_unlock(&cs35l41->fw_mutex);
794
795         return ret;
796 }
797
798 static int cs35l41_runtime_idle(struct device *dev)
799 {
800         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
801
802         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH)
803                 return -EBUSY; /* suspend not supported yet on this model */
804         return 0;
805 }
806
807 static int cs35l41_runtime_suspend(struct device *dev)
808 {
809         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
810         int ret = 0;
811
812         dev_dbg(cs35l41->dev, "Runtime Suspend\n");
813
814         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
815                 dev_dbg(cs35l41->dev, "Runtime Suspend not supported\n");
816                 return 0;
817         }
818
819         mutex_lock(&cs35l41->fw_mutex);
820
821         if (cs35l41->firmware_running) {
822                 ret = cs35l41_enter_hibernate(cs35l41->dev, cs35l41->regmap,
823                                               cs35l41->hw_cfg.bst_type);
824                 if (ret)
825                         goto err;
826         } else {
827                 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
828         }
829
830         regcache_cache_only(cs35l41->regmap, true);
831         regcache_mark_dirty(cs35l41->regmap);
832
833 err:
834         mutex_unlock(&cs35l41->fw_mutex);
835
836         return ret;
837 }
838
839 static int cs35l41_runtime_resume(struct device *dev)
840 {
841         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
842         int ret = 0;
843
844         dev_dbg(cs35l41->dev, "Runtime Resume\n");
845
846         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
847                 dev_dbg(cs35l41->dev, "Runtime Resume not supported\n");
848                 return 0;
849         }
850
851         mutex_lock(&cs35l41->fw_mutex);
852
853         regcache_cache_only(cs35l41->regmap, false);
854
855         if (cs35l41->firmware_running)  {
856                 ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap);
857                 if (ret) {
858                         dev_warn(cs35l41->dev, "Unable to exit Hibernate.");
859                         goto err;
860                 }
861         }
862
863         /* Test key needs to be unlocked to allow the OTP settings to re-apply */
864         cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
865         ret = regcache_sync(cs35l41->regmap);
866         cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
867         if (ret) {
868                 dev_err(cs35l41->dev, "Failed to restore register cache: %d\n", ret);
869                 goto err;
870         }
871
872         if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
873                 cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, &cs35l41->hw_cfg);
874
875 err:
876         mutex_unlock(&cs35l41->fw_mutex);
877
878         return ret;
879 }
880
881 static int cs35l41_smart_amp(struct cs35l41_hda *cs35l41)
882 {
883         __be32 halo_sts;
884         int ret;
885
886         ret = cs35l41_init_dsp(cs35l41);
887         if (ret) {
888                 dev_warn(cs35l41->dev, "Cannot Initialize Firmware. Error: %d\n", ret);
889                 goto clean_dsp;
890         }
891
892         ret = cs35l41_write_fs_errata(cs35l41->dev, cs35l41->regmap);
893         if (ret) {
894                 dev_err(cs35l41->dev, "Cannot Write FS Errata: %d\n", ret);
895                 goto clean_dsp;
896         }
897
898         ret = cs_dsp_run(&cs35l41->cs_dsp);
899         if (ret) {
900                 dev_err(cs35l41->dev, "Fail to start dsp: %d\n", ret);
901                 goto clean_dsp;
902         }
903
904         ret = read_poll_timeout(hda_cs_dsp_read_ctl, ret,
905                                 be32_to_cpu(halo_sts) == HALO_STATE_CODE_RUN,
906                                 1000, 15000, false, &cs35l41->cs_dsp, HALO_STATE_DSP_CTL_NAME,
907                                 HALO_STATE_DSP_CTL_TYPE, HALO_STATE_DSP_CTL_ALG,
908                                 &halo_sts, sizeof(halo_sts));
909
910         if (ret) {
911                 dev_err(cs35l41->dev, "Timeout waiting for HALO Core to start. State: %u\n",
912                          halo_sts);
913                 goto clean_dsp;
914         }
915
916         ret = cs35l41_set_cspl_mbox_cmd(cs35l41->dev, cs35l41->regmap, CSPL_MBOX_CMD_PAUSE);
917         if (ret) {
918                 dev_err(cs35l41->dev, "Error waiting for DSP to pause: %u\n", ret);
919                 goto clean_dsp;
920         }
921
922         cs35l41->firmware_running = true;
923
924         return 0;
925
926 clean_dsp:
927         cs35l41_shutdown_dsp(cs35l41);
928         return ret;
929 }
930
931 static void cs35l41_load_firmware(struct cs35l41_hda *cs35l41, bool load)
932 {
933         if (cs35l41->firmware_running && !load) {
934                 dev_dbg(cs35l41->dev, "Unloading Firmware\n");
935                 cs35l41_shutdown_dsp(cs35l41);
936         } else if (!cs35l41->firmware_running && load) {
937                 dev_dbg(cs35l41->dev, "Loading Firmware\n");
938                 cs35l41_smart_amp(cs35l41);
939         } else {
940                 dev_dbg(cs35l41->dev, "Unable to Load firmware.\n");
941         }
942 }
943
944 static int cs35l41_fw_load_ctl_get(struct snd_kcontrol *kcontrol,
945                                    struct snd_ctl_elem_value *ucontrol)
946 {
947         struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
948
949         ucontrol->value.integer.value[0] = cs35l41->request_fw_load;
950         return 0;
951 }
952
953 static void cs35l41_fw_load_work(struct work_struct *work)
954 {
955         struct cs35l41_hda *cs35l41 = container_of(work, struct cs35l41_hda, fw_load_work);
956
957         pm_runtime_get_sync(cs35l41->dev);
958
959         mutex_lock(&cs35l41->fw_mutex);
960
961         /* Recheck if playback is ongoing, mutex will block playback during firmware loading */
962         if (cs35l41->playback_started)
963                 dev_err(cs35l41->dev, "Cannot Load/Unload firmware during Playback. Retrying...\n");
964         else
965                 cs35l41_load_firmware(cs35l41, cs35l41->request_fw_load);
966
967         cs35l41->fw_request_ongoing = false;
968         mutex_unlock(&cs35l41->fw_mutex);
969
970         pm_runtime_mark_last_busy(cs35l41->dev);
971         pm_runtime_put_autosuspend(cs35l41->dev);
972 }
973
974 static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol,
975                                    struct snd_ctl_elem_value *ucontrol)
976 {
977         struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
978
979         if (cs35l41->request_fw_load == ucontrol->value.integer.value[0])
980                 return 0;
981
982         if (cs35l41->fw_request_ongoing) {
983                 dev_dbg(cs35l41->dev, "Existing request not complete\n");
984                 return -EBUSY;
985         }
986
987         /* Check if playback is ongoing when initial request is made */
988         if (cs35l41->playback_started) {
989                 dev_err(cs35l41->dev, "Cannot Load/Unload firmware during Playback\n");
990                 return -EBUSY;
991         }
992
993         cs35l41->fw_request_ongoing = true;
994         cs35l41->request_fw_load = ucontrol->value.integer.value[0];
995         schedule_work(&cs35l41->fw_load_work);
996
997         return 1;
998 }
999
1000 static int cs35l41_fw_type_ctl_get(struct snd_kcontrol *kcontrol,
1001                                    struct snd_ctl_elem_value *ucontrol)
1002 {
1003         struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
1004
1005         ucontrol->value.enumerated.item[0] = cs35l41->firmware_type;
1006
1007         return 0;
1008 }
1009
1010 static int cs35l41_fw_type_ctl_put(struct snd_kcontrol *kcontrol,
1011                                    struct snd_ctl_elem_value *ucontrol)
1012 {
1013         struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
1014
1015         if (ucontrol->value.enumerated.item[0] < HDA_CS_DSP_NUM_FW) {
1016                 if (cs35l41->firmware_type != ucontrol->value.enumerated.item[0]) {
1017                         cs35l41->firmware_type = ucontrol->value.enumerated.item[0];
1018                         return 1;
1019                 } else {
1020                         return 0;
1021                 }
1022         }
1023
1024         return -EINVAL;
1025 }
1026
1027 static int cs35l41_fw_type_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1028 {
1029         return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(hda_cs_dsp_fw_ids), hda_cs_dsp_fw_ids);
1030 }
1031
1032 static int cs35l41_create_controls(struct cs35l41_hda *cs35l41)
1033 {
1034         char fw_type_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1035         char fw_load_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1036         struct snd_kcontrol_new fw_type_ctl = {
1037                 .name = fw_type_ctl_name,
1038                 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1039                 .info = cs35l41_fw_type_ctl_info,
1040                 .get = cs35l41_fw_type_ctl_get,
1041                 .put = cs35l41_fw_type_ctl_put,
1042         };
1043         struct snd_kcontrol_new fw_load_ctl = {
1044                 .name = fw_load_ctl_name,
1045                 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1046                 .info = snd_ctl_boolean_mono_info,
1047                 .get = cs35l41_fw_load_ctl_get,
1048                 .put = cs35l41_fw_load_ctl_put,
1049         };
1050         int ret;
1051
1052         scnprintf(fw_type_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Type",
1053                   cs35l41->amp_name);
1054         scnprintf(fw_load_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Load",
1055                   cs35l41->amp_name);
1056
1057         ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&fw_type_ctl, cs35l41));
1058         if (ret) {
1059                 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", fw_type_ctl.name, ret);
1060                 return ret;
1061         }
1062
1063         dev_dbg(cs35l41->dev, "Added Control %s\n", fw_type_ctl.name);
1064
1065         ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&fw_load_ctl, cs35l41));
1066         if (ret) {
1067                 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", fw_load_ctl.name, ret);
1068                 return ret;
1069         }
1070
1071         dev_dbg(cs35l41->dev, "Added Control %s\n", fw_load_ctl.name);
1072
1073         return 0;
1074 }
1075
1076 static int cs35l41_hda_bind(struct device *dev, struct device *master, void *master_data)
1077 {
1078         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1079         struct hda_component *comps = master_data;
1080         unsigned int sleep_flags;
1081         int ret = 0;
1082
1083         if (!comps || cs35l41->index < 0 || cs35l41->index >= HDA_MAX_COMPONENTS)
1084                 return -EINVAL;
1085
1086         comps = &comps[cs35l41->index];
1087         if (comps->dev)
1088                 return -EBUSY;
1089
1090         pm_runtime_get_sync(dev);
1091
1092         mutex_lock(&cs35l41->fw_mutex);
1093
1094         comps->dev = dev;
1095         if (!cs35l41->acpi_subsystem_id)
1096                 cs35l41->acpi_subsystem_id = kasprintf(GFP_KERNEL, "%.8x",
1097                                                        comps->codec->core.subsystem_id);
1098         cs35l41->codec = comps->codec;
1099         strscpy(comps->name, dev_name(dev), sizeof(comps->name));
1100
1101         cs35l41->firmware_type = HDA_CS_DSP_FW_SPK_PROT;
1102
1103         if (firmware_autostart) {
1104                 dev_dbg(cs35l41->dev, "Firmware Autostart.\n");
1105                 cs35l41->request_fw_load = true;
1106                 if (cs35l41_smart_amp(cs35l41) < 0)
1107                         dev_warn(cs35l41->dev, "Cannot Run Firmware, reverting to dsp bypass...\n");
1108         } else {
1109                 dev_dbg(cs35l41->dev, "Firmware Autostart is disabled.\n");
1110         }
1111
1112         ret = cs35l41_create_controls(cs35l41);
1113
1114         comps->playback_hook = cs35l41_hda_playback_hook;
1115         comps->pre_playback_hook = cs35l41_hda_pre_playback_hook;
1116         comps->post_playback_hook = cs35l41_hda_post_playback_hook;
1117
1118         mutex_unlock(&cs35l41->fw_mutex);
1119
1120         sleep_flags = lock_system_sleep();
1121         if (!device_link_add(&comps->codec->core.dev, cs35l41->dev, DL_FLAG_STATELESS))
1122                 dev_warn(dev, "Unable to create device link\n");
1123         unlock_system_sleep(sleep_flags);
1124
1125         pm_runtime_mark_last_busy(dev);
1126         pm_runtime_put_autosuspend(dev);
1127
1128         dev_info(cs35l41->dev,
1129                  "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n",
1130                  cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type,
1131                  cs35l41->hw_cfg.gpio1.func == CS35l41_VSPK_SWITCH,
1132                  cs35l41->hw_cfg.spk_pos ? 'R' : 'L',
1133                  cs35l41->firmware_running, cs35l41->speaker_id);
1134
1135         return ret;
1136 }
1137
1138 static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *master_data)
1139 {
1140         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1141         struct hda_component *comps = master_data;
1142         unsigned int sleep_flags;
1143
1144         if (comps[cs35l41->index].dev == dev) {
1145                 memset(&comps[cs35l41->index], 0, sizeof(*comps));
1146                 sleep_flags = lock_system_sleep();
1147                 device_link_remove(&comps->codec->core.dev, cs35l41->dev);
1148                 unlock_system_sleep(sleep_flags);
1149         }
1150 }
1151
1152 static const struct component_ops cs35l41_hda_comp_ops = {
1153         .bind = cs35l41_hda_bind,
1154         .unbind = cs35l41_hda_unbind,
1155 };
1156
1157 static irqreturn_t cs35l41_bst_short_err(int irq, void *data)
1158 {
1159         struct cs35l41_hda *cs35l41 = data;
1160
1161         dev_crit_ratelimited(cs35l41->dev, "LBST Error\n");
1162         set_bit(CS35L41_BST_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1163
1164         return IRQ_HANDLED;
1165 }
1166
1167 static irqreturn_t cs35l41_bst_dcm_uvp_err(int irq, void *data)
1168 {
1169         struct cs35l41_hda *cs35l41 = data;
1170
1171         dev_crit_ratelimited(cs35l41->dev, "DCM VBST Under Voltage Error\n");
1172         set_bit(CS35L41_BST_UVP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1173
1174         return IRQ_HANDLED;
1175 }
1176
1177 static irqreturn_t cs35l41_bst_ovp_err(int irq, void *data)
1178 {
1179         struct cs35l41_hda *cs35l41 = data;
1180
1181         dev_crit_ratelimited(cs35l41->dev, "VBST Over Voltage error\n");
1182         set_bit(CS35L41_BST_OVP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1183
1184         return IRQ_HANDLED;
1185 }
1186
1187 static irqreturn_t cs35l41_temp_err(int irq, void *data)
1188 {
1189         struct cs35l41_hda *cs35l41 = data;
1190
1191         dev_crit_ratelimited(cs35l41->dev, "Over temperature error\n");
1192         set_bit(CS35L41_TEMP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1193
1194         return IRQ_HANDLED;
1195 }
1196
1197 static irqreturn_t cs35l41_temp_warn(int irq, void *data)
1198 {
1199         struct cs35l41_hda *cs35l41 = data;
1200
1201         dev_crit_ratelimited(cs35l41->dev, "Over temperature warning\n");
1202         set_bit(CS35L41_TEMP_WARN_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1203
1204         return IRQ_HANDLED;
1205 }
1206
1207 static irqreturn_t cs35l41_amp_short(int irq, void *data)
1208 {
1209         struct cs35l41_hda *cs35l41 = data;
1210
1211         dev_crit_ratelimited(cs35l41->dev, "Amp short error\n");
1212         set_bit(CS35L41_AMP_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1213
1214         return IRQ_HANDLED;
1215 }
1216
1217 static const struct cs35l41_irq cs35l41_irqs[] = {
1218         CS35L41_IRQ(BST_OVP_ERR, "Boost Overvoltage Error", cs35l41_bst_ovp_err),
1219         CS35L41_IRQ(BST_DCM_UVP_ERR, "Boost Undervoltage Error", cs35l41_bst_dcm_uvp_err),
1220         CS35L41_IRQ(BST_SHORT_ERR, "Boost Inductor Short Error", cs35l41_bst_short_err),
1221         CS35L41_IRQ(TEMP_WARN, "Temperature Warning", cs35l41_temp_warn),
1222         CS35L41_IRQ(TEMP_ERR, "Temperature Error", cs35l41_temp_err),
1223         CS35L41_IRQ(AMP_SHORT_ERR, "Amp Short", cs35l41_amp_short),
1224 };
1225
1226 static const struct regmap_irq cs35l41_reg_irqs[] = {
1227         CS35L41_REG_IRQ(IRQ1_STATUS1, BST_OVP_ERR),
1228         CS35L41_REG_IRQ(IRQ1_STATUS1, BST_DCM_UVP_ERR),
1229         CS35L41_REG_IRQ(IRQ1_STATUS1, BST_SHORT_ERR),
1230         CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_WARN),
1231         CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_ERR),
1232         CS35L41_REG_IRQ(IRQ1_STATUS1, AMP_SHORT_ERR),
1233 };
1234
1235 static struct regmap_irq_chip cs35l41_regmap_irq_chip = {
1236         .name = "cs35l41 IRQ1 Controller",
1237         .status_base = CS35L41_IRQ1_STATUS1,
1238         .mask_base = CS35L41_IRQ1_MASK1,
1239         .ack_base = CS35L41_IRQ1_STATUS1,
1240         .num_regs = 4,
1241         .irqs = cs35l41_reg_irqs,
1242         .num_irqs = ARRAY_SIZE(cs35l41_reg_irqs),
1243         .runtime_pm = true,
1244 };
1245
1246 static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41)
1247 {
1248         struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
1249         bool using_irq = false;
1250         int irq, irq_pol;
1251         int ret;
1252         int i;
1253
1254         if (!cs35l41->hw_cfg.valid)
1255                 return -EINVAL;
1256
1257         ret = cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, hw_cfg);
1258         if (ret)
1259                 return ret;
1260
1261         if (hw_cfg->gpio1.valid) {
1262                 switch (hw_cfg->gpio1.func) {
1263                 case CS35L41_NOT_USED:
1264                         break;
1265                 case CS35l41_VSPK_SWITCH:
1266                         hw_cfg->gpio1.func = CS35L41_GPIO1_GPIO;
1267                         hw_cfg->gpio1.out_en = true;
1268                         break;
1269                 case CS35l41_SYNC:
1270                         hw_cfg->gpio1.func = CS35L41_GPIO1_MDSYNC;
1271                         break;
1272                 default:
1273                         dev_err(cs35l41->dev, "Invalid function %d for GPIO1\n",
1274                                 hw_cfg->gpio1.func);
1275                         return -EINVAL;
1276                 }
1277         }
1278
1279         if (hw_cfg->gpio2.valid) {
1280                 switch (hw_cfg->gpio2.func) {
1281                 case CS35L41_NOT_USED:
1282                         break;
1283                 case CS35L41_INTERRUPT:
1284                         using_irq = true;
1285                         hw_cfg->gpio2.func = CS35L41_GPIO2_INT_OPEN_DRAIN;
1286                         break;
1287                 default:
1288                         dev_err(cs35l41->dev, "Invalid GPIO2 function %d\n", hw_cfg->gpio2.func);
1289                         return -EINVAL;
1290                 }
1291         }
1292
1293         irq_pol = cs35l41_gpio_config(cs35l41->regmap, hw_cfg);
1294
1295         if (cs35l41->irq && using_irq) {
1296                 ret = devm_regmap_add_irq_chip(cs35l41->dev, cs35l41->regmap, cs35l41->irq,
1297                                                IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1298                                                0, &cs35l41_regmap_irq_chip, &cs35l41->irq_data);
1299                 if (ret)
1300                         return ret;
1301
1302                 for (i = 0; i < ARRAY_SIZE(cs35l41_irqs); i++) {
1303                         irq = regmap_irq_get_virq(cs35l41->irq_data, cs35l41_irqs[i].irq);
1304                         if (irq < 0)
1305                                 return irq;
1306
1307                         ret = devm_request_threaded_irq(cs35l41->dev, irq, NULL,
1308                                                         cs35l41_irqs[i].handler,
1309                                                         IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1310                                                         cs35l41_irqs[i].name, cs35l41);
1311                         if (ret)
1312                                 return ret;
1313                 }
1314         }
1315
1316         return cs35l41_hda_channel_map(cs35l41->dev, 0, NULL, 1, &hw_cfg->spk_pos);
1317 }
1318
1319 int cs35l41_get_speaker_id(struct device *dev, int amp_index, int num_amps, int fixed_gpio_id)
1320 {
1321         struct gpio_desc *speaker_id_desc;
1322         int speaker_id = -ENODEV;
1323
1324         if (fixed_gpio_id >= 0) {
1325                 dev_dbg(dev, "Found Fixed Speaker ID GPIO (index = %d)\n", fixed_gpio_id);
1326                 speaker_id_desc = gpiod_get_index(dev, NULL, fixed_gpio_id, GPIOD_IN);
1327                 if (IS_ERR(speaker_id_desc)) {
1328                         speaker_id = PTR_ERR(speaker_id_desc);
1329                         return speaker_id;
1330                 }
1331                 speaker_id = gpiod_get_value_cansleep(speaker_id_desc);
1332                 gpiod_put(speaker_id_desc);
1333                 dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
1334         } else {
1335                 int base_index;
1336                 int gpios_per_amp;
1337                 int count;
1338                 int tmp;
1339                 int i;
1340
1341                 count = gpiod_count(dev, "spk-id");
1342                 if (count > 0) {
1343                         speaker_id = 0;
1344                         gpios_per_amp = count / num_amps;
1345                         base_index = gpios_per_amp * amp_index;
1346
1347                         if (count % num_amps)
1348                                 return -EINVAL;
1349
1350                         dev_dbg(dev, "Found %d Speaker ID GPIOs per Amp\n", gpios_per_amp);
1351
1352                         for (i = 0; i < gpios_per_amp; i++) {
1353                                 speaker_id_desc = gpiod_get_index(dev, "spk-id", i + base_index,
1354                                                                   GPIOD_IN);
1355                                 if (IS_ERR(speaker_id_desc)) {
1356                                         speaker_id = PTR_ERR(speaker_id_desc);
1357                                         break;
1358                                 }
1359                                 tmp = gpiod_get_value_cansleep(speaker_id_desc);
1360                                 gpiod_put(speaker_id_desc);
1361                                 if (tmp < 0) {
1362                                         speaker_id = tmp;
1363                                         break;
1364                                 }
1365                                 speaker_id |= tmp << i;
1366                         }
1367                         dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
1368                 }
1369         }
1370         return speaker_id;
1371 }
1372
1373 static int cs35l41_hda_read_acpi(struct cs35l41_hda *cs35l41, const char *hid, int id)
1374 {
1375         struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
1376         u32 values[HDA_MAX_COMPONENTS];
1377         struct acpi_device *adev;
1378         struct device *physdev;
1379         const char *sub;
1380         char *property;
1381         size_t nval;
1382         int i, ret;
1383
1384         adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1385         if (!adev) {
1386                 dev_err(cs35l41->dev, "Failed to find an ACPI device for %s\n", hid);
1387                 return -ENODEV;
1388         }
1389
1390         physdev = get_device(acpi_get_first_physical_node(adev));
1391         acpi_dev_put(adev);
1392
1393         sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
1394         if (IS_ERR(sub))
1395                 sub = NULL;
1396         cs35l41->acpi_subsystem_id = sub;
1397
1398         ret = cs35l41_add_dsd_properties(cs35l41, physdev, id, hid);
1399         if (!ret) {
1400                 dev_info(cs35l41->dev, "Using extra _DSD properties, bypassing _DSD in ACPI\n");
1401                 goto put_physdev;
1402         }
1403
1404         property = "cirrus,dev-index";
1405         ret = device_property_count_u32(physdev, property);
1406         if (ret <= 0)
1407                 goto err;
1408
1409         if (ret > ARRAY_SIZE(values)) {
1410                 ret = -EINVAL;
1411                 goto err;
1412         }
1413         nval = ret;
1414
1415         ret = device_property_read_u32_array(physdev, property, values, nval);
1416         if (ret)
1417                 goto err;
1418
1419         cs35l41->index = -1;
1420         for (i = 0; i < nval; i++) {
1421                 if (values[i] == id) {
1422                         cs35l41->index = i;
1423                         break;
1424                 }
1425         }
1426         if (cs35l41->index == -1) {
1427                 dev_err(cs35l41->dev, "No index found in %s\n", property);
1428                 ret = -ENODEV;
1429                 goto err;
1430         }
1431
1432         /* To use the same release code for all laptop variants we can't use devm_ version of
1433          * gpiod_get here, as CLSA010* don't have a fully functional bios with an _DSD node
1434          */
1435         cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(adev), "reset", cs35l41->index,
1436                                                      GPIOD_OUT_LOW, "cs35l41-reset");
1437
1438         property = "cirrus,speaker-position";
1439         ret = device_property_read_u32_array(physdev, property, values, nval);
1440         if (ret)
1441                 goto err;
1442         hw_cfg->spk_pos = values[cs35l41->index];
1443
1444         cs35l41->channel_index = 0;
1445         for (i = 0; i < cs35l41->index; i++)
1446                 if (values[i] == hw_cfg->spk_pos)
1447                         cs35l41->channel_index++;
1448
1449         property = "cirrus,gpio1-func";
1450         ret = device_property_read_u32_array(physdev, property, values, nval);
1451         if (ret)
1452                 goto err;
1453         hw_cfg->gpio1.func = values[cs35l41->index];
1454         hw_cfg->gpio1.valid = true;
1455
1456         property = "cirrus,gpio2-func";
1457         ret = device_property_read_u32_array(physdev, property, values, nval);
1458         if (ret)
1459                 goto err;
1460         hw_cfg->gpio2.func = values[cs35l41->index];
1461         hw_cfg->gpio2.valid = true;
1462
1463         property = "cirrus,boost-peak-milliamp";
1464         ret = device_property_read_u32_array(physdev, property, values, nval);
1465         if (ret == 0)
1466                 hw_cfg->bst_ipk = values[cs35l41->index];
1467         else
1468                 hw_cfg->bst_ipk = -1;
1469
1470         property = "cirrus,boost-ind-nanohenry";
1471         ret = device_property_read_u32_array(physdev, property, values, nval);
1472         if (ret == 0)
1473                 hw_cfg->bst_ind = values[cs35l41->index];
1474         else
1475                 hw_cfg->bst_ind = -1;
1476
1477         property = "cirrus,boost-cap-microfarad";
1478         ret = device_property_read_u32_array(physdev, property, values, nval);
1479         if (ret == 0)
1480                 hw_cfg->bst_cap = values[cs35l41->index];
1481         else
1482                 hw_cfg->bst_cap = -1;
1483
1484         cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, nval, -1);
1485
1486         if (hw_cfg->bst_ind > 0 || hw_cfg->bst_cap > 0 || hw_cfg->bst_ipk > 0)
1487                 hw_cfg->bst_type = CS35L41_INT_BOOST;
1488         else
1489                 hw_cfg->bst_type = CS35L41_EXT_BOOST;
1490
1491         hw_cfg->valid = true;
1492         put_device(physdev);
1493
1494         return 0;
1495
1496 err:
1497         dev_err(cs35l41->dev, "Failed property %s: %d\n", property, ret);
1498         hw_cfg->valid = false;
1499         hw_cfg->gpio1.valid = false;
1500         hw_cfg->gpio2.valid = false;
1501 put_physdev:
1502         put_device(physdev);
1503
1504         return ret;
1505 }
1506
1507 int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int irq,
1508                       struct regmap *regmap)
1509 {
1510         unsigned int int_sts, regid, reg_revid, mtl_revid, chipid, int_status;
1511         struct cs35l41_hda *cs35l41;
1512         int ret;
1513
1514         BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != ARRAY_SIZE(cs35l41_reg_irqs));
1515         BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != CS35L41_NUM_IRQ);
1516
1517         if (IS_ERR(regmap))
1518                 return PTR_ERR(regmap);
1519
1520         cs35l41 = devm_kzalloc(dev, sizeof(*cs35l41), GFP_KERNEL);
1521         if (!cs35l41)
1522                 return -ENOMEM;
1523
1524         cs35l41->dev = dev;
1525         cs35l41->irq = irq;
1526         cs35l41->regmap = regmap;
1527         dev_set_drvdata(dev, cs35l41);
1528
1529         ret = cs35l41_hda_read_acpi(cs35l41, device_name, id);
1530         if (ret)
1531                 return dev_err_probe(cs35l41->dev, ret, "Platform not supported\n");
1532
1533         if (IS_ERR(cs35l41->reset_gpio)) {
1534                 ret = PTR_ERR(cs35l41->reset_gpio);
1535                 cs35l41->reset_gpio = NULL;
1536                 if (ret == -EBUSY) {
1537                         dev_info(cs35l41->dev, "Reset line busy, assuming shared reset\n");
1538                 } else {
1539                         dev_err_probe(cs35l41->dev, ret, "Failed to get reset GPIO\n");
1540                         goto err;
1541                 }
1542         }
1543         if (cs35l41->reset_gpio) {
1544                 usleep_range(2000, 2100);
1545                 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
1546         }
1547
1548         usleep_range(2000, 2100);
1549
1550         ret = regmap_read_poll_timeout(cs35l41->regmap, CS35L41_IRQ1_STATUS4, int_status,
1551                                        int_status & CS35L41_OTP_BOOT_DONE, 1000, 100000);
1552         if (ret) {
1553                 dev_err(cs35l41->dev, "Failed waiting for OTP_BOOT_DONE: %d\n", ret);
1554                 goto err;
1555         }
1556
1557         ret = regmap_read(cs35l41->regmap, CS35L41_IRQ1_STATUS3, &int_sts);
1558         if (ret || (int_sts & CS35L41_OTP_BOOT_ERR)) {
1559                 dev_err(cs35l41->dev, "OTP Boot status %x error: %d\n",
1560                         int_sts & CS35L41_OTP_BOOT_ERR, ret);
1561                 ret = -EIO;
1562                 goto err;
1563         }
1564
1565         ret = regmap_read(cs35l41->regmap, CS35L41_DEVID, &regid);
1566         if (ret) {
1567                 dev_err(cs35l41->dev, "Get Device ID failed: %d\n", ret);
1568                 goto err;
1569         }
1570
1571         ret = regmap_read(cs35l41->regmap, CS35L41_REVID, &reg_revid);
1572         if (ret) {
1573                 dev_err(cs35l41->dev, "Get Revision ID failed: %d\n", ret);
1574                 goto err;
1575         }
1576
1577         mtl_revid = reg_revid & CS35L41_MTLREVID_MASK;
1578
1579         chipid = (mtl_revid % 2) ? CS35L41R_CHIP_ID : CS35L41_CHIP_ID;
1580         if (regid != chipid) {
1581                 dev_err(cs35l41->dev, "CS35L41 Device ID (%X). Expected ID %X\n", regid, chipid);
1582                 ret = -ENODEV;
1583                 goto err;
1584         }
1585
1586         ret = cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
1587         if (ret)
1588                 goto err;
1589
1590         ret = cs35l41_register_errata_patch(cs35l41->dev, cs35l41->regmap, reg_revid);
1591         if (ret)
1592                 goto err;
1593
1594         ret = cs35l41_otp_unpack(cs35l41->dev, cs35l41->regmap);
1595         if (ret) {
1596                 dev_err(cs35l41->dev, "OTP Unpack failed: %d\n", ret);
1597                 goto err;
1598         }
1599
1600         ret = cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
1601         if (ret)
1602                 goto err;
1603
1604         ret = regmap_multi_reg_write(cs35l41->regmap, cs35l41_hda_mute,
1605                                      ARRAY_SIZE(cs35l41_hda_mute));
1606         if (ret)
1607                 goto err;
1608
1609         INIT_WORK(&cs35l41->fw_load_work, cs35l41_fw_load_work);
1610         mutex_init(&cs35l41->fw_mutex);
1611
1612         pm_runtime_set_autosuspend_delay(cs35l41->dev, 3000);
1613         pm_runtime_use_autosuspend(cs35l41->dev);
1614         pm_runtime_mark_last_busy(cs35l41->dev);
1615         pm_runtime_set_active(cs35l41->dev);
1616         pm_runtime_get_noresume(cs35l41->dev);
1617         pm_runtime_enable(cs35l41->dev);
1618
1619         ret = cs35l41_hda_apply_properties(cs35l41);
1620         if (ret)
1621                 goto err_pm;
1622
1623         pm_runtime_put_autosuspend(cs35l41->dev);
1624
1625         ret = component_add(cs35l41->dev, &cs35l41_hda_comp_ops);
1626         if (ret) {
1627                 dev_err(cs35l41->dev, "Register component failed: %d\n", ret);
1628                 pm_runtime_disable(cs35l41->dev);
1629                 goto err;
1630         }
1631
1632         dev_info(cs35l41->dev, "Cirrus Logic CS35L41 (%x), Revision: %02X\n", regid, reg_revid);
1633
1634         return 0;
1635
1636 err_pm:
1637         pm_runtime_disable(cs35l41->dev);
1638         pm_runtime_put_noidle(cs35l41->dev);
1639
1640 err:
1641         if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type))
1642                 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1643         gpiod_put(cs35l41->reset_gpio);
1644         kfree(cs35l41->acpi_subsystem_id);
1645
1646         return ret;
1647 }
1648 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_probe, SND_HDA_SCODEC_CS35L41);
1649
1650 void cs35l41_hda_remove(struct device *dev)
1651 {
1652         struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1653
1654         pm_runtime_get_sync(cs35l41->dev);
1655         pm_runtime_disable(cs35l41->dev);
1656
1657         if (cs35l41->halo_initialized)
1658                 cs35l41_remove_dsp(cs35l41);
1659
1660         component_del(cs35l41->dev, &cs35l41_hda_comp_ops);
1661
1662         pm_runtime_put_noidle(cs35l41->dev);
1663
1664         if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type))
1665                 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1666         gpiod_put(cs35l41->reset_gpio);
1667         kfree(cs35l41->acpi_subsystem_id);
1668 }
1669 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_remove, SND_HDA_SCODEC_CS35L41);
1670
1671 const struct dev_pm_ops cs35l41_hda_pm_ops = {
1672         RUNTIME_PM_OPS(cs35l41_runtime_suspend, cs35l41_runtime_resume,
1673                        cs35l41_runtime_idle)
1674         .prepare = cs35l41_system_suspend_prep,
1675         SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend, cs35l41_system_resume)
1676 };
1677 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_pm_ops, SND_HDA_SCODEC_CS35L41);
1678
1679 MODULE_DESCRIPTION("CS35L41 HDA Driver");
1680 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS);
1681 MODULE_AUTHOR("Lucas Tanure, Cirrus Logic Inc, <tanureal@opensource.cirrus.com>");
1682 MODULE_LICENSE("GPL");
1683 MODULE_IMPORT_NS(FW_CS_DSP);