Merge tag 'soc-drivers-6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / sound / soc / codecs / cs35l56.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Driver for Cirrus Logic CS35L56 smart amp
4 //
5 // Copyright (C) 2023 Cirrus Logic, Inc. and
6 //                    Cirrus Logic International Semiconductor Ltd.
7
8 #include <linux/acpi.h>
9 #include <linux/completion.h>
10 #include <linux/debugfs.h>
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/math.h>
16 #include <linux/module.h>
17 #include <linux/pm.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/soundwire/sdw.h>
24 #include <linux/types.h>
25 #include <linux/workqueue.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/tlv.h>
31
32 #include "wm_adsp.h"
33 #include "cs35l56.h"
34
35 static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
36                              struct snd_kcontrol *kcontrol, int event);
37
38 static void cs35l56_wait_dsp_ready(struct cs35l56_private *cs35l56)
39 {
40         /* Wait for patching to complete */
41         flush_work(&cs35l56->dsp_work);
42 }
43
44 static int cs35l56_dspwait_get_volsw(struct snd_kcontrol *kcontrol,
45                                      struct snd_ctl_elem_value *ucontrol)
46 {
47         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
48         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
49
50         cs35l56_wait_dsp_ready(cs35l56);
51         return snd_soc_get_volsw(kcontrol, ucontrol);
52 }
53
54 static int cs35l56_dspwait_put_volsw(struct snd_kcontrol *kcontrol,
55                                      struct snd_ctl_elem_value *ucontrol)
56 {
57         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
58         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
59
60         cs35l56_wait_dsp_ready(cs35l56);
61         return snd_soc_put_volsw(kcontrol, ucontrol);
62 }
63
64 static const unsigned short cs35l56_asp1_mixer_regs[] = {
65         CS35L56_ASP1TX1_INPUT, CS35L56_ASP1TX2_INPUT,
66         CS35L56_ASP1TX3_INPUT, CS35L56_ASP1TX4_INPUT,
67 };
68
69 static const char * const cs35l56_asp1_mux_control_names[] = {
70         "ASP1 TX1 Source", "ASP1 TX2 Source", "ASP1 TX3 Source", "ASP1 TX4 Source"
71 };
72
73 static int cs35l56_sync_asp1_mixer_widgets_with_firmware(struct cs35l56_private *cs35l56)
74 {
75         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cs35l56->component);
76         const char *prefix = cs35l56->component->name_prefix;
77         char full_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
78         const char *name;
79         struct snd_kcontrol *kcontrol;
80         struct soc_enum *e;
81         unsigned int val[4];
82         int i, item, ret;
83
84         if (cs35l56->asp1_mixer_widgets_initialized)
85                 return 0;
86
87         /*
88          * Resume so we can read the registers from silicon if the regmap
89          * cache has not yet been populated.
90          */
91         ret = pm_runtime_resume_and_get(cs35l56->base.dev);
92         if (ret < 0)
93                 return ret;
94
95         /* Wait for firmware download and reboot */
96         cs35l56_wait_dsp_ready(cs35l56);
97
98         ret = regmap_bulk_read(cs35l56->base.regmap, CS35L56_ASP1TX1_INPUT,
99                                val, ARRAY_SIZE(val));
100
101         pm_runtime_mark_last_busy(cs35l56->base.dev);
102         pm_runtime_put_autosuspend(cs35l56->base.dev);
103
104         if (ret) {
105                 dev_err(cs35l56->base.dev, "Failed to read ASP1 mixer regs: %d\n", ret);
106                 return ret;
107         }
108
109         for (i = 0; i < ARRAY_SIZE(cs35l56_asp1_mux_control_names); ++i) {
110                 name = cs35l56_asp1_mux_control_names[i];
111
112                 if (prefix) {
113                         snprintf(full_name, sizeof(full_name), "%s %s", prefix, name);
114                         name = full_name;
115                 }
116
117                 kcontrol = snd_soc_card_get_kcontrol_locked(dapm->card, name);
118                 if (!kcontrol) {
119                         dev_warn(cs35l56->base.dev, "Could not find control %s\n", name);
120                         continue;
121                 }
122
123                 e = (struct soc_enum *)kcontrol->private_value;
124                 item = snd_soc_enum_val_to_item(e, val[i] & CS35L56_ASP_TXn_SRC_MASK);
125                 snd_soc_dapm_mux_update_power(dapm, kcontrol, item, e, NULL);
126         }
127
128         cs35l56->asp1_mixer_widgets_initialized = true;
129
130         return 0;
131 }
132
133 static int cs35l56_dspwait_asp1tx_get(struct snd_kcontrol *kcontrol,
134                                       struct snd_ctl_elem_value *ucontrol)
135 {
136         struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
137         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
138         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
139         int index = e->shift_l;
140         unsigned int addr, val;
141         int ret;
142
143         ret = cs35l56_sync_asp1_mixer_widgets_with_firmware(cs35l56);
144         if (ret)
145                 return ret;
146
147         addr = cs35l56_asp1_mixer_regs[index];
148         ret = regmap_read(cs35l56->base.regmap, addr, &val);
149         if (ret)
150                 return ret;
151
152         val &= CS35L56_ASP_TXn_SRC_MASK;
153         ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
154
155         return 0;
156 }
157
158 static int cs35l56_dspwait_asp1tx_put(struct snd_kcontrol *kcontrol,
159                                       struct snd_ctl_elem_value *ucontrol)
160 {
161         struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
162         struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
163         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
164         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
165         int item = ucontrol->value.enumerated.item[0];
166         int index = e->shift_l;
167         unsigned int addr, val;
168         bool changed;
169         int ret;
170
171         ret = cs35l56_sync_asp1_mixer_widgets_with_firmware(cs35l56);
172         if (ret)
173                 return ret;
174
175         addr = cs35l56_asp1_mixer_regs[index];
176         val = snd_soc_enum_item_to_val(e, item);
177
178         ret = regmap_update_bits_check(cs35l56->base.regmap, addr,
179                                        CS35L56_ASP_TXn_SRC_MASK, val, &changed);
180         if (ret)
181                 return ret;
182
183         if (changed)
184                 snd_soc_dapm_mux_update_power(dapm, kcontrol, item, e, NULL);
185
186         return changed;
187 }
188
189 static DECLARE_TLV_DB_SCALE(vol_tlv, -10000, 25, 0);
190
191 static const struct snd_kcontrol_new cs35l56_controls[] = {
192         SOC_SINGLE_EXT("Speaker Switch",
193                        CS35L56_MAIN_RENDER_USER_MUTE, 0, 1, 1,
194                        cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
195         SOC_SINGLE_S_EXT_TLV("Speaker Volume",
196                              CS35L56_MAIN_RENDER_USER_VOLUME,
197                              6, -400, 400, 9, 0,
198                              cs35l56_dspwait_get_volsw,
199                              cs35l56_dspwait_put_volsw,
200                              vol_tlv),
201         SOC_SINGLE_EXT("Posture Number", CS35L56_MAIN_POSTURE_NUMBER,
202                        0, 255, 0,
203                        cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
204 };
205
206 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx1_enum,
207                                   SND_SOC_NOPM,
208                                   0, 0,
209                                   cs35l56_tx_input_texts,
210                                   cs35l56_tx_input_values);
211
212 static const struct snd_kcontrol_new asp1_tx1_mux =
213         SOC_DAPM_ENUM_EXT("ASP1TX1 SRC", cs35l56_asp1tx1_enum,
214                           cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
215
216 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx2_enum,
217                                   SND_SOC_NOPM,
218                                   1, 0,
219                                   cs35l56_tx_input_texts,
220                                   cs35l56_tx_input_values);
221
222 static const struct snd_kcontrol_new asp1_tx2_mux =
223         SOC_DAPM_ENUM_EXT("ASP1TX2 SRC", cs35l56_asp1tx2_enum,
224                           cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
225
226 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx3_enum,
227                                   SND_SOC_NOPM,
228                                   2, 0,
229                                   cs35l56_tx_input_texts,
230                                   cs35l56_tx_input_values);
231
232 static const struct snd_kcontrol_new asp1_tx3_mux =
233         SOC_DAPM_ENUM_EXT("ASP1TX3 SRC", cs35l56_asp1tx3_enum,
234                           cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
235
236 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx4_enum,
237                                   SND_SOC_NOPM,
238                                   3, 0,
239                                   cs35l56_tx_input_texts,
240                                   cs35l56_tx_input_values);
241
242 static const struct snd_kcontrol_new asp1_tx4_mux =
243         SOC_DAPM_ENUM_EXT("ASP1TX4 SRC", cs35l56_asp1tx4_enum,
244                           cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
245
246 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx1_enum,
247                                 CS35L56_SWIRE_DP3_CH1_INPUT,
248                                 0, CS35L56_SWIRETXn_SRC_MASK,
249                                 cs35l56_tx_input_texts,
250                                 cs35l56_tx_input_values);
251
252 static const struct snd_kcontrol_new sdw1_tx1_mux =
253         SOC_DAPM_ENUM("SDW1TX1 SRC", cs35l56_sdw1tx1_enum);
254
255 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx2_enum,
256                                 CS35L56_SWIRE_DP3_CH2_INPUT,
257                                 0, CS35L56_SWIRETXn_SRC_MASK,
258                                 cs35l56_tx_input_texts,
259                                 cs35l56_tx_input_values);
260
261 static const struct snd_kcontrol_new sdw1_tx2_mux =
262         SOC_DAPM_ENUM("SDW1TX2 SRC", cs35l56_sdw1tx2_enum);
263
264 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx3_enum,
265                                 CS35L56_SWIRE_DP3_CH3_INPUT,
266                                 0, CS35L56_SWIRETXn_SRC_MASK,
267                                 cs35l56_tx_input_texts,
268                                 cs35l56_tx_input_values);
269
270 static const struct snd_kcontrol_new sdw1_tx3_mux =
271         SOC_DAPM_ENUM("SDW1TX3 SRC", cs35l56_sdw1tx3_enum);
272
273 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx4_enum,
274                                 CS35L56_SWIRE_DP3_CH4_INPUT,
275                                 0, CS35L56_SWIRETXn_SRC_MASK,
276                                 cs35l56_tx_input_texts,
277                                 cs35l56_tx_input_values);
278
279 static const struct snd_kcontrol_new sdw1_tx4_mux =
280         SOC_DAPM_ENUM("SDW1TX4 SRC", cs35l56_sdw1tx4_enum);
281
282 static int cs35l56_asp1_cfg_event(struct snd_soc_dapm_widget *w,
283                                   struct snd_kcontrol *kcontrol, int event)
284 {
285         struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
286         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
287
288         switch (event) {
289         case SND_SOC_DAPM_PRE_PMU:
290                 /* Override register values set by firmware boot */
291                 return cs35l56_force_sync_asp1_registers_from_cache(&cs35l56->base);
292         default:
293                 return 0;
294         }
295 }
296
297 static int cs35l56_play_event(struct snd_soc_dapm_widget *w,
298                               struct snd_kcontrol *kcontrol, int event)
299 {
300         struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
301         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
302         unsigned int val;
303         int ret;
304
305         dev_dbg(cs35l56->base.dev, "play: %d\n", event);
306
307         switch (event) {
308         case SND_SOC_DAPM_PRE_PMU:
309                 /* Don't wait for ACK, we check in POST_PMU that it completed */
310                 return regmap_write(cs35l56->base.regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
311                                     CS35L56_MBOX_CMD_AUDIO_PLAY);
312         case SND_SOC_DAPM_POST_PMU:
313                 /* Wait for firmware to enter PS0 power state */
314                 ret = regmap_read_poll_timeout(cs35l56->base.regmap,
315                                                CS35L56_TRANSDUCER_ACTUAL_PS,
316                                                val, (val == CS35L56_PS0),
317                                                CS35L56_PS0_POLL_US,
318                                                CS35L56_PS0_TIMEOUT_US);
319                 if (ret)
320                         dev_err(cs35l56->base.dev, "PS0 wait failed: %d\n", ret);
321                 return ret;
322         case SND_SOC_DAPM_POST_PMD:
323                 return cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PAUSE);
324         default:
325                 return 0;
326         }
327 }
328
329 static const struct snd_soc_dapm_widget cs35l56_dapm_widgets[] = {
330         SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_B", 0, 0),
331         SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_AMP", 0, 0),
332
333         SND_SOC_DAPM_SUPPLY("ASP1 CFG", SND_SOC_NOPM, 0, 0, cs35l56_asp1_cfg_event,
334                             SND_SOC_DAPM_PRE_PMU),
335
336         SND_SOC_DAPM_SUPPLY("PLAY", SND_SOC_NOPM, 0, 0, cs35l56_play_event,
337                             SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
338
339         SND_SOC_DAPM_OUT_DRV("AMP", SND_SOC_NOPM, 0, 0, NULL, 0),
340         SND_SOC_DAPM_OUTPUT("SPK"),
341
342         SND_SOC_DAPM_PGA_E("DSP1", SND_SOC_NOPM, 0, 0, NULL, 0, cs35l56_dsp_event,
343                            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
344
345         SND_SOC_DAPM_AIF_IN("ASP1RX1", NULL, 0, CS35L56_ASP1_ENABLES1,
346                             CS35L56_ASP_RX1_EN_SHIFT, 0),
347         SND_SOC_DAPM_AIF_IN("ASP1RX2", NULL, 1, CS35L56_ASP1_ENABLES1,
348                             CS35L56_ASP_RX2_EN_SHIFT, 0),
349         SND_SOC_DAPM_AIF_OUT("ASP1TX1", NULL, 0, CS35L56_ASP1_ENABLES1,
350                              CS35L56_ASP_TX1_EN_SHIFT, 0),
351         SND_SOC_DAPM_AIF_OUT("ASP1TX2", NULL, 1, CS35L56_ASP1_ENABLES1,
352                              CS35L56_ASP_TX2_EN_SHIFT, 0),
353         SND_SOC_DAPM_AIF_OUT("ASP1TX3", NULL, 2, CS35L56_ASP1_ENABLES1,
354                              CS35L56_ASP_TX3_EN_SHIFT, 0),
355         SND_SOC_DAPM_AIF_OUT("ASP1TX4", NULL, 3, CS35L56_ASP1_ENABLES1,
356                              CS35L56_ASP_TX4_EN_SHIFT, 0),
357
358         SND_SOC_DAPM_MUX("ASP1 TX1 Source", SND_SOC_NOPM, 0, 0, &asp1_tx1_mux),
359         SND_SOC_DAPM_MUX("ASP1 TX2 Source", SND_SOC_NOPM, 0, 0, &asp1_tx2_mux),
360         SND_SOC_DAPM_MUX("ASP1 TX3 Source", SND_SOC_NOPM, 0, 0, &asp1_tx3_mux),
361         SND_SOC_DAPM_MUX("ASP1 TX4 Source", SND_SOC_NOPM, 0, 0, &asp1_tx4_mux),
362
363         SND_SOC_DAPM_MUX("SDW1 TX1 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx1_mux),
364         SND_SOC_DAPM_MUX("SDW1 TX2 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx2_mux),
365         SND_SOC_DAPM_MUX("SDW1 TX3 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx3_mux),
366         SND_SOC_DAPM_MUX("SDW1 TX4 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx4_mux),
367
368         SND_SOC_DAPM_SIGGEN("VMON ADC"),
369         SND_SOC_DAPM_SIGGEN("IMON ADC"),
370         SND_SOC_DAPM_SIGGEN("ERRVOL ADC"),
371         SND_SOC_DAPM_SIGGEN("CLASSH ADC"),
372         SND_SOC_DAPM_SIGGEN("VDDBMON ADC"),
373         SND_SOC_DAPM_SIGGEN("VBSTMON ADC"),
374         SND_SOC_DAPM_SIGGEN("TEMPMON ADC"),
375 };
376
377 #define CS35L56_SRC_ROUTE(name) \
378         { name" Source", "ASP1RX1", "ASP1RX1" }, \
379         { name" Source", "ASP1RX2", "ASP1RX2" }, \
380         { name" Source", "VMON", "VMON ADC" }, \
381         { name" Source", "IMON", "IMON ADC" }, \
382         { name" Source", "ERRVOL", "ERRVOL ADC" },   \
383         { name" Source", "CLASSH", "CLASSH ADC" },   \
384         { name" Source", "VDDBMON", "VDDBMON ADC" }, \
385         { name" Source", "VBSTMON", "VBSTMON ADC" }, \
386         { name" Source", "DSP1TX1", "DSP1" }, \
387         { name" Source", "DSP1TX2", "DSP1" }, \
388         { name" Source", "DSP1TX3", "DSP1" }, \
389         { name" Source", "DSP1TX4", "DSP1" }, \
390         { name" Source", "DSP1TX5", "DSP1" }, \
391         { name" Source", "DSP1TX6", "DSP1" }, \
392         { name" Source", "DSP1TX7", "DSP1" }, \
393         { name" Source", "DSP1TX8", "DSP1" }, \
394         { name" Source", "TEMPMON", "TEMPMON ADC" }, \
395         { name" Source", "INTERPOLATOR", "AMP" }, \
396         { name" Source", "SDW1RX1", "SDW1 Playback" }, \
397         { name" Source", "SDW1RX2", "SDW1 Playback" },
398
399 static const struct snd_soc_dapm_route cs35l56_audio_map[] = {
400         { "AMP", NULL, "VDD_B" },
401         { "AMP", NULL, "VDD_AMP" },
402
403         { "ASP1 Playback", NULL, "ASP1 CFG" },
404         { "ASP1 Capture", NULL, "ASP1 CFG" },
405
406         { "ASP1 Playback", NULL, "PLAY" },
407         { "SDW1 Playback", NULL, "PLAY" },
408
409         { "ASP1RX1", NULL, "ASP1 Playback" },
410         { "ASP1RX2", NULL, "ASP1 Playback" },
411         { "DSP1", NULL, "ASP1RX1" },
412         { "DSP1", NULL, "ASP1RX2" },
413         { "DSP1", NULL, "SDW1 Playback" },
414         { "AMP", NULL, "DSP1" },
415         { "SPK", NULL, "AMP" },
416
417         CS35L56_SRC_ROUTE("ASP1 TX1")
418         CS35L56_SRC_ROUTE("ASP1 TX2")
419         CS35L56_SRC_ROUTE("ASP1 TX3")
420         CS35L56_SRC_ROUTE("ASP1 TX4")
421
422         { "ASP1TX1", NULL, "ASP1 TX1 Source" },
423         { "ASP1TX2", NULL, "ASP1 TX2 Source" },
424         { "ASP1TX3", NULL, "ASP1 TX3 Source" },
425         { "ASP1TX4", NULL, "ASP1 TX4 Source" },
426         { "ASP1 Capture", NULL, "ASP1TX1" },
427         { "ASP1 Capture", NULL, "ASP1TX2" },
428         { "ASP1 Capture", NULL, "ASP1TX3" },
429         { "ASP1 Capture", NULL, "ASP1TX4" },
430
431         CS35L56_SRC_ROUTE("SDW1 TX1")
432         CS35L56_SRC_ROUTE("SDW1 TX2")
433         CS35L56_SRC_ROUTE("SDW1 TX3")
434         CS35L56_SRC_ROUTE("SDW1 TX4")
435         { "SDW1 Capture", NULL, "SDW1 TX1 Source" },
436         { "SDW1 Capture", NULL, "SDW1 TX2 Source" },
437         { "SDW1 Capture", NULL, "SDW1 TX3 Source" },
438         { "SDW1 Capture", NULL, "SDW1 TX4 Source" },
439 };
440
441 static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
442                              struct snd_kcontrol *kcontrol, int event)
443 {
444         struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
445         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
446
447         dev_dbg(cs35l56->base.dev, "%s: %d\n", __func__, event);
448
449         return wm_adsp_event(w, kcontrol, event);
450 }
451
452 static int cs35l56_asp_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
453 {
454         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(codec_dai->component);
455         unsigned int val;
456
457         dev_dbg(cs35l56->base.dev, "%s: %#x\n", __func__, fmt);
458
459         switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
460         case SND_SOC_DAIFMT_CBC_CFC:
461                 break;
462         default:
463                 dev_err(cs35l56->base.dev, "Unsupported clock source mode\n");
464                 return -EINVAL;
465         }
466
467         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
468         case SND_SOC_DAIFMT_DSP_A:
469                 val = CS35L56_ASP_FMT_DSP_A << CS35L56_ASP_FMT_SHIFT;
470                 cs35l56->tdm_mode = true;
471                 break;
472         case SND_SOC_DAIFMT_I2S:
473                 val = CS35L56_ASP_FMT_I2S << CS35L56_ASP_FMT_SHIFT;
474                 cs35l56->tdm_mode = false;
475                 break;
476         default:
477                 dev_err(cs35l56->base.dev, "Unsupported DAI format\n");
478                 return -EINVAL;
479         }
480
481         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
482         case SND_SOC_DAIFMT_NB_IF:
483                 val |= CS35L56_ASP_FSYNC_INV_MASK;
484                 break;
485         case SND_SOC_DAIFMT_IB_NF:
486                 val |= CS35L56_ASP_BCLK_INV_MASK;
487                 break;
488         case SND_SOC_DAIFMT_IB_IF:
489                 val |= CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK;
490                 break;
491         case SND_SOC_DAIFMT_NB_NF:
492                 break;
493         default:
494                 dev_err(cs35l56->base.dev, "Invalid clock invert\n");
495                 return -EINVAL;
496         }
497
498         regmap_update_bits(cs35l56->base.regmap,
499                            CS35L56_ASP1_CONTROL2,
500                            CS35L56_ASP_FMT_MASK |
501                            CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK,
502                            val);
503
504         /* Hi-Z DOUT in unused slots and when all TX are disabled */
505         regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
506                            CS35L56_ASP1_DOUT_HIZ_CTRL_MASK,
507                            CS35L56_ASP_UNUSED_HIZ_OFF_HIZ);
508
509         return 0;
510 }
511
512 static unsigned int cs35l56_make_tdm_config_word(unsigned int reg_val, unsigned long mask)
513 {
514         unsigned int channel_shift;
515         int bit_num;
516
517         /* Enable consecutive TX1..TXn for each of the slots set in mask */
518         channel_shift = 0;
519         for_each_set_bit(bit_num, &mask, 32) {
520                 reg_val &= ~(0x3f << channel_shift);
521                 reg_val |= bit_num << channel_shift;
522                 channel_shift += 8;
523         }
524
525         return reg_val;
526 }
527
528 static int cs35l56_asp_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
529                                         unsigned int rx_mask, int slots, int slot_width)
530 {
531         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
532
533         if ((slots == 0) || (slot_width == 0)) {
534                 dev_dbg(cs35l56->base.dev, "tdm config cleared\n");
535                 cs35l56->asp_slot_width = 0;
536                 cs35l56->asp_slot_count = 0;
537                 return 0;
538         }
539
540         if (slot_width > (CS35L56_ASP_RX_WIDTH_MASK >> CS35L56_ASP_RX_WIDTH_SHIFT)) {
541                 dev_err(cs35l56->base.dev, "tdm invalid slot width %d\n", slot_width);
542                 return -EINVAL;
543         }
544
545         /* More than 32 slots would give an unsupportable BCLK frequency */
546         if (slots > 32) {
547                 dev_err(cs35l56->base.dev, "tdm invalid slot count %d\n", slots);
548                 return -EINVAL;
549         }
550
551         cs35l56->asp_slot_width = (u8)slot_width;
552         cs35l56->asp_slot_count = (u8)slots;
553
554         // Note: rx/tx is from point of view of the CPU end
555         if (tx_mask == 0)
556                 tx_mask = 0x3;  // ASPRX1/RX2 in slots 0 and 1
557
558         if (rx_mask == 0)
559                 rx_mask = 0xf;  // ASPTX1..TX4 in slots 0..3
560
561         /* Default unused slots to 63 */
562         regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL1,
563                      cs35l56_make_tdm_config_word(0x3f3f3f3f, rx_mask));
564         regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL5,
565                      cs35l56_make_tdm_config_word(0x3f3f3f, tx_mask));
566
567         dev_dbg(cs35l56->base.dev, "tdm slot width: %u count: %u tx_mask: %#x rx_mask: %#x\n",
568                 cs35l56->asp_slot_width, cs35l56->asp_slot_count, tx_mask, rx_mask);
569
570         return 0;
571 }
572
573 static int cs35l56_asp_dai_hw_params(struct snd_pcm_substream *substream,
574                                      struct snd_pcm_hw_params *params,
575                                      struct snd_soc_dai *dai)
576 {
577         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
578         unsigned int rate = params_rate(params);
579         u8 asp_width, asp_wl;
580
581         asp_wl = params_width(params);
582         if (cs35l56->asp_slot_width)
583                 asp_width = cs35l56->asp_slot_width;
584         else
585                 asp_width = asp_wl;
586
587         dev_dbg(cs35l56->base.dev, "%s: wl=%d, width=%d, rate=%d",
588                 __func__, asp_wl, asp_width, rate);
589
590         if (!cs35l56->sysclk_set) {
591                 unsigned int slots = cs35l56->asp_slot_count;
592                 unsigned int bclk_freq;
593                 int freq_id;
594
595                 if (slots == 0) {
596                         slots = params_channels(params);
597
598                         /* I2S always has an even number of slots */
599                         if (!cs35l56->tdm_mode)
600                                 slots = round_up(slots, 2);
601                 }
602
603                 bclk_freq = asp_width * slots * rate;
604                 freq_id = cs35l56_get_bclk_freq_id(bclk_freq);
605                 if (freq_id < 0) {
606                         dev_err(cs35l56->base.dev, "%s: Invalid BCLK %u\n", __func__, bclk_freq);
607                         return -EINVAL;
608                 }
609
610                 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1,
611                                    CS35L56_ASP_BCLK_FREQ_MASK,
612                                    freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
613         }
614
615         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
616                 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2,
617                                    CS35L56_ASP_RX_WIDTH_MASK, asp_width <<
618                                    CS35L56_ASP_RX_WIDTH_SHIFT);
619                 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL5,
620                                    CS35L56_ASP_RX_WL_MASK, asp_wl);
621         } else {
622                 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2,
623                                    CS35L56_ASP_TX_WIDTH_MASK, asp_width <<
624                                    CS35L56_ASP_TX_WIDTH_SHIFT);
625                 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL1,
626                                    CS35L56_ASP_TX_WL_MASK, asp_wl);
627         }
628
629         return 0;
630 }
631
632 static int cs35l56_asp_dai_set_sysclk(struct snd_soc_dai *dai,
633                                       int clk_id, unsigned int freq, int dir)
634 {
635         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
636         int freq_id;
637
638         if (freq == 0) {
639                 cs35l56->sysclk_set = false;
640                 return 0;
641         }
642
643         freq_id = cs35l56_get_bclk_freq_id(freq);
644         if (freq_id < 0)
645                 return freq_id;
646
647         regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1,
648                            CS35L56_ASP_BCLK_FREQ_MASK,
649                            freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
650         cs35l56->sysclk_set = true;
651
652         return 0;
653 }
654
655 static const struct snd_soc_dai_ops cs35l56_ops = {
656         .set_fmt = cs35l56_asp_dai_set_fmt,
657         .set_tdm_slot = cs35l56_asp_dai_set_tdm_slot,
658         .hw_params = cs35l56_asp_dai_hw_params,
659         .set_sysclk = cs35l56_asp_dai_set_sysclk,
660 };
661
662 static void cs35l56_sdw_dai_shutdown(struct snd_pcm_substream *substream,
663                                      struct snd_soc_dai *dai)
664 {
665         snd_soc_dai_set_dma_data(dai, substream, NULL);
666 }
667
668 static int cs35l56_sdw_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
669                                         unsigned int rx_mask, int slots, int slot_width)
670 {
671         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
672
673         /* rx/tx are from point of view of the CPU end so opposite to our rx/tx */
674         cs35l56->rx_mask = tx_mask;
675         cs35l56->tx_mask = rx_mask;
676
677         return 0;
678 }
679
680 static int cs35l56_sdw_dai_hw_params(struct snd_pcm_substream *substream,
681                                      struct snd_pcm_hw_params *params,
682                                      struct snd_soc_dai *dai)
683 {
684         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
685         struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
686         struct sdw_stream_config sconfig;
687         struct sdw_port_config pconfig;
688         int ret;
689
690         dev_dbg(cs35l56->base.dev, "%s: rate %d\n", __func__, params_rate(params));
691
692         if (!cs35l56->base.init_done)
693                 return -ENODEV;
694
695         if (!sdw_stream)
696                 return -EINVAL;
697
698         memset(&sconfig, 0, sizeof(sconfig));
699         memset(&pconfig, 0, sizeof(pconfig));
700
701         sconfig.frame_rate = params_rate(params);
702         sconfig.bps = snd_pcm_format_width(params_format(params));
703
704         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
705                 sconfig.direction = SDW_DATA_DIR_RX;
706                 pconfig.num = CS35L56_SDW1_PLAYBACK_PORT;
707                 pconfig.ch_mask = cs35l56->rx_mask;
708         } else {
709                 sconfig.direction = SDW_DATA_DIR_TX;
710                 pconfig.num = CS35L56_SDW1_CAPTURE_PORT;
711                 pconfig.ch_mask = cs35l56->tx_mask;
712         }
713
714         if (pconfig.ch_mask == 0) {
715                 sconfig.ch_count = params_channels(params);
716                 pconfig.ch_mask = GENMASK(sconfig.ch_count - 1, 0);
717         } else {
718                 sconfig.ch_count = hweight32(pconfig.ch_mask);
719         }
720
721         ret = sdw_stream_add_slave(cs35l56->sdw_peripheral, &sconfig, &pconfig,
722                                    1, sdw_stream);
723         if (ret) {
724                 dev_err(dai->dev, "Failed to add sdw stream: %d\n", ret);
725                 return ret;
726         }
727
728         return 0;
729 }
730
731 static int cs35l56_sdw_dai_hw_free(struct snd_pcm_substream *substream,
732                                    struct snd_soc_dai *dai)
733 {
734         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
735         struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
736
737         if (!cs35l56->sdw_peripheral)
738                 return -EINVAL;
739
740         sdw_stream_remove_slave(cs35l56->sdw_peripheral, sdw_stream);
741
742         return 0;
743 }
744
745 static int cs35l56_sdw_dai_set_stream(struct snd_soc_dai *dai,
746                                       void *sdw_stream, int direction)
747 {
748         snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
749
750         return 0;
751 }
752
753 static const struct snd_soc_dai_ops cs35l56_sdw_dai_ops = {
754         .set_tdm_slot = cs35l56_sdw_dai_set_tdm_slot,
755         .shutdown = cs35l56_sdw_dai_shutdown,
756         .hw_params = cs35l56_sdw_dai_hw_params,
757         .hw_free = cs35l56_sdw_dai_hw_free,
758         .set_stream = cs35l56_sdw_dai_set_stream,
759 };
760
761 static struct snd_soc_dai_driver cs35l56_dai[] = {
762         {
763                 .name = "cs35l56-asp1",
764                 .id = 0,
765                 .playback = {
766                         .stream_name = "ASP1 Playback",
767                         .channels_min = 1,
768                         .channels_max = 2,
769                         .rates = CS35L56_RATES,
770                         .formats = CS35L56_RX_FORMATS,
771                 },
772                 .capture = {
773                         .stream_name = "ASP1 Capture",
774                         .channels_min = 1,
775                         .channels_max = 4,
776                         .rates = CS35L56_RATES,
777                         .formats = CS35L56_TX_FORMATS,
778                 },
779                 .ops = &cs35l56_ops,
780                 .symmetric_rate = 1,
781                 .symmetric_sample_bits = 1,
782         },
783         {
784                 .name = "cs35l56-sdw1",
785                 .id = 1,
786                 .playback = {
787                         .stream_name = "SDW1 Playback",
788                         .channels_min = 1,
789                         .channels_max = 2,
790                         .rates = CS35L56_RATES,
791                         .formats = CS35L56_RX_FORMATS,
792                 },
793                 .capture = {
794                         .stream_name = "SDW1 Capture",
795                         .channels_min = 1,
796                         .channels_max = 4,
797                         .rates = CS35L56_RATES,
798                         .formats = CS35L56_TX_FORMATS,
799                 },
800                 .symmetric_rate = 1,
801                 .ops = &cs35l56_sdw_dai_ops,
802         }
803 };
804
805 static void cs35l56_reinit_patch(struct cs35l56_private *cs35l56)
806 {
807         int ret;
808
809         /* Use wm_adsp to load and apply the firmware patch and coefficient files */
810         ret = wm_adsp_power_up(&cs35l56->dsp, true);
811         if (ret)
812                 dev_dbg(cs35l56->base.dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret);
813         else
814                 cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
815 }
816
817 static void cs35l56_patch(struct cs35l56_private *cs35l56, bool firmware_missing)
818 {
819         int ret;
820
821         /*
822          * Disable SoundWire interrupts to prevent race with IRQ work.
823          * Setting sdw_irq_no_unmask prevents the handler re-enabling
824          * the SoundWire interrupt.
825          */
826         if (cs35l56->sdw_peripheral) {
827                 cs35l56->sdw_irq_no_unmask = true;
828                 flush_work(&cs35l56->sdw_irq_work);
829                 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1, 0);
830                 sdw_read_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1);
831                 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1, 0xFF);
832                 flush_work(&cs35l56->sdw_irq_work);
833         }
834
835         ret = cs35l56_firmware_shutdown(&cs35l56->base);
836         if (ret)
837                 goto err;
838
839         /*
840          * Use wm_adsp to load and apply the firmware patch and coefficient files,
841          * but only if firmware is missing. If firmware is already patched just
842          * power-up wm_adsp without downloading firmware.
843          */
844         ret = wm_adsp_power_up(&cs35l56->dsp, !!firmware_missing);
845         if (ret) {
846                 dev_dbg(cs35l56->base.dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret);
847                 goto err;
848         }
849
850         mutex_lock(&cs35l56->base.irq_lock);
851
852         reinit_completion(&cs35l56->init_completion);
853
854         cs35l56->soft_resetting = true;
855         cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
856
857         if (cs35l56->sdw_peripheral) {
858                 /*
859                  * The system-reset causes the CS35L56 to detach from the bus.
860                  * Wait for the manager to re-enumerate the CS35L56 and
861                  * cs35l56_init() to run again.
862                  */
863                 if (!wait_for_completion_timeout(&cs35l56->init_completion,
864                                                  msecs_to_jiffies(5000))) {
865                         dev_err(cs35l56->base.dev, "%s: init_completion timed out (SDW)\n",
866                                 __func__);
867                         goto err_unlock;
868                 }
869         } else if (cs35l56_init(cs35l56)) {
870                 goto err_unlock;
871         }
872
873         regmap_clear_bits(cs35l56->base.regmap, CS35L56_PROTECTION_STATUS,
874                           CS35L56_FIRMWARE_MISSING);
875         cs35l56->base.fw_patched = true;
876
877 err_unlock:
878         mutex_unlock(&cs35l56->base.irq_lock);
879 err:
880         /* Re-enable SoundWire interrupts */
881         if (cs35l56->sdw_peripheral) {
882                 cs35l56->sdw_irq_no_unmask = false;
883                 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1,
884                                 CS35L56_SDW_INT_MASK_CODEC_IRQ);
885         }
886 }
887
888 static void cs35l56_dsp_work(struct work_struct *work)
889 {
890         struct cs35l56_private *cs35l56 = container_of(work,
891                                                        struct cs35l56_private,
892                                                        dsp_work);
893         unsigned int firmware_version;
894         bool firmware_missing;
895         int ret;
896
897         if (!cs35l56->base.init_done)
898                 return;
899
900         pm_runtime_get_sync(cs35l56->base.dev);
901
902         ret = cs35l56_read_prot_status(&cs35l56->base, &firmware_missing, &firmware_version);
903         if (ret)
904                 goto err;
905
906         /* Populate fw file qualifier with the revision and security state */
907         kfree(cs35l56->dsp.fwf_name);
908         if (firmware_missing) {
909                 cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL, "%02x-dsp1", cs35l56->base.rev);
910         } else {
911                 /* Firmware files must match the running firmware version */
912                 cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL,
913                                                   "%02x%s-%06x-dsp1",
914                                                   cs35l56->base.rev,
915                                                   cs35l56->base.secured ? "-s" : "",
916                                                   firmware_version);
917         }
918
919         if (!cs35l56->dsp.fwf_name)
920                 goto err;
921
922         dev_dbg(cs35l56->base.dev, "DSP fwf name: '%s' system name: '%s'\n",
923                 cs35l56->dsp.fwf_name, cs35l56->dsp.system_name);
924
925         /*
926          * The firmware cannot be patched if it is already running from
927          * patch RAM. In this case the firmware files are versioned to
928          * match the running firmware version and will only contain
929          * tunings. We do not need to shutdown the firmware to apply
930          * tunings so can use the lower cost reinit sequence instead.
931          */
932         if (!firmware_missing)
933                 cs35l56_reinit_patch(cs35l56);
934         else
935                 cs35l56_patch(cs35l56, firmware_missing);
936
937 err:
938         pm_runtime_mark_last_busy(cs35l56->base.dev);
939         pm_runtime_put_autosuspend(cs35l56->base.dev);
940 }
941
942 static int cs35l56_component_probe(struct snd_soc_component *component)
943 {
944         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
945         struct dentry *debugfs_root = component->debugfs_root;
946         unsigned short vendor, device;
947
948         BUILD_BUG_ON(ARRAY_SIZE(cs35l56_tx_input_texts) != ARRAY_SIZE(cs35l56_tx_input_values));
949
950         if (!cs35l56->dsp.system_name &&
951             (snd_soc_card_get_pci_ssid(component->card, &vendor, &device) == 0)) {
952                 /* Append a speaker qualifier if there is a speaker ID */
953                 if (cs35l56->speaker_id >= 0) {
954                         cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
955                                                                   GFP_KERNEL,
956                                                                   "%04x%04x-spkid%d",
957                                                                   vendor, device,
958                                                                   cs35l56->speaker_id);
959                 } else {
960                         cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
961                                                                   GFP_KERNEL,
962                                                                   "%04x%04x",
963                                                                   vendor, device);
964                 }
965                 if (!cs35l56->dsp.system_name)
966                         return -ENOMEM;
967         }
968
969         if (!wait_for_completion_timeout(&cs35l56->init_completion,
970                                          msecs_to_jiffies(5000))) {
971                 dev_err(cs35l56->base.dev, "%s: init_completion timed out\n", __func__);
972                 return -ENODEV;
973         }
974
975         cs35l56->component = component;
976         wm_adsp2_component_probe(&cs35l56->dsp, component);
977
978         debugfs_create_bool("init_done", 0444, debugfs_root, &cs35l56->base.init_done);
979         debugfs_create_bool("can_hibernate", 0444, debugfs_root, &cs35l56->base.can_hibernate);
980         debugfs_create_bool("fw_patched", 0444, debugfs_root, &cs35l56->base.fw_patched);
981
982         /*
983          * The widgets for the ASP1TX mixer can't be initialized
984          * until the firmware has been downloaded and rebooted.
985          */
986         regcache_drop_region(cs35l56->base.regmap, CS35L56_ASP1TX1_INPUT, CS35L56_ASP1TX4_INPUT);
987         cs35l56->asp1_mixer_widgets_initialized = false;
988
989         queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
990
991         return 0;
992 }
993
994 static void cs35l56_component_remove(struct snd_soc_component *component)
995 {
996         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
997
998         cancel_work_sync(&cs35l56->dsp_work);
999
1000         if (cs35l56->dsp.cs_dsp.booted)
1001                 wm_adsp_power_down(&cs35l56->dsp);
1002
1003         wm_adsp2_component_remove(&cs35l56->dsp, component);
1004
1005         kfree(cs35l56->dsp.fwf_name);
1006         cs35l56->dsp.fwf_name = NULL;
1007
1008         cs35l56->component = NULL;
1009 }
1010
1011 static int cs35l56_set_bias_level(struct snd_soc_component *component,
1012                                   enum snd_soc_bias_level level)
1013 {
1014         struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1015
1016         switch (level) {
1017         case SND_SOC_BIAS_STANDBY:
1018                 /*
1019                  * Wait for patching to complete when transitioning from
1020                  * BIAS_OFF to BIAS_STANDBY
1021                  */
1022                 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
1023                         cs35l56_wait_dsp_ready(cs35l56);
1024
1025                 break;
1026         default:
1027                 break;
1028         }
1029
1030         return 0;
1031 }
1032
1033 static const struct snd_soc_component_driver soc_component_dev_cs35l56 = {
1034         .probe = cs35l56_component_probe,
1035         .remove = cs35l56_component_remove,
1036
1037         .dapm_widgets = cs35l56_dapm_widgets,
1038         .num_dapm_widgets = ARRAY_SIZE(cs35l56_dapm_widgets),
1039         .dapm_routes = cs35l56_audio_map,
1040         .num_dapm_routes = ARRAY_SIZE(cs35l56_audio_map),
1041         .controls = cs35l56_controls,
1042         .num_controls = ARRAY_SIZE(cs35l56_controls),
1043
1044         .set_bias_level = cs35l56_set_bias_level,
1045
1046         .suspend_bias_off = 1, /* see cs35l56_system_resume() */
1047 };
1048
1049 static int __maybe_unused cs35l56_runtime_suspend_i2c_spi(struct device *dev)
1050 {
1051         struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1052
1053         return cs35l56_runtime_suspend_common(&cs35l56->base);
1054 }
1055
1056 static int __maybe_unused cs35l56_runtime_resume_i2c_spi(struct device *dev)
1057 {
1058         struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1059
1060         return cs35l56_runtime_resume_common(&cs35l56->base, false);
1061 }
1062
1063 int cs35l56_system_suspend(struct device *dev)
1064 {
1065         struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1066
1067         dev_dbg(dev, "system_suspend\n");
1068
1069         if (cs35l56->component)
1070                 flush_work(&cs35l56->dsp_work);
1071
1072         /*
1073          * The interrupt line is normally shared, but after we start suspending
1074          * we can't check if our device is the source of an interrupt, and can't
1075          * clear it. Prevent this race by temporarily disabling the parent irq
1076          * until we reach _no_irq.
1077          */
1078         if (cs35l56->base.irq)
1079                 disable_irq(cs35l56->base.irq);
1080
1081         return pm_runtime_force_suspend(dev);
1082 }
1083 EXPORT_SYMBOL_GPL(cs35l56_system_suspend);
1084
1085 int cs35l56_system_suspend_late(struct device *dev)
1086 {
1087         struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1088
1089         dev_dbg(dev, "system_suspend_late\n");
1090
1091         /*
1092          * Assert RESET before removing supplies.
1093          * RESET is usually shared by all amps so it must not be asserted until
1094          * all driver instances have done their suspend() stage.
1095          */
1096         if (cs35l56->base.reset_gpio) {
1097                 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1098                 cs35l56_wait_min_reset_pulse();
1099         }
1100
1101         regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1102
1103         return 0;
1104 }
1105 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_late);
1106
1107 int cs35l56_system_suspend_no_irq(struct device *dev)
1108 {
1109         struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1110
1111         dev_dbg(dev, "system_suspend_no_irq\n");
1112
1113         /* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
1114         if (cs35l56->base.irq)
1115                 enable_irq(cs35l56->base.irq);
1116
1117         return 0;
1118 }
1119 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_no_irq);
1120
1121 int cs35l56_system_resume_no_irq(struct device *dev)
1122 {
1123         struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1124
1125         dev_dbg(dev, "system_resume_no_irq\n");
1126
1127         /*
1128          * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
1129          * spurious interrupts, and the interrupt line is normally shared.
1130          * We can't check if our device is the source of an interrupt, and can't
1131          * clear it, until it has fully resumed. Prevent this race by temporarily
1132          * disabling the parent irq until we complete resume().
1133          */
1134         if (cs35l56->base.irq)
1135                 disable_irq(cs35l56->base.irq);
1136
1137         return 0;
1138 }
1139 EXPORT_SYMBOL_GPL(cs35l56_system_resume_no_irq);
1140
1141 int cs35l56_system_resume_early(struct device *dev)
1142 {
1143         struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1144         int ret;
1145
1146         dev_dbg(dev, "system_resume_early\n");
1147
1148         /* Ensure a spec-compliant RESET pulse. */
1149         if (cs35l56->base.reset_gpio) {
1150                 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1151                 cs35l56_wait_min_reset_pulse();
1152         }
1153
1154         /* Enable supplies before releasing RESET. */
1155         ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1156         if (ret) {
1157                 dev_err(dev, "system_resume_early failed to enable supplies: %d\n", ret);
1158                 return ret;
1159         }
1160
1161         /* Release shared RESET before drivers start resume(). */
1162         gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1163
1164         return 0;
1165 }
1166 EXPORT_SYMBOL_GPL(cs35l56_system_resume_early);
1167
1168 int cs35l56_system_resume(struct device *dev)
1169 {
1170         struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1171         int ret;
1172
1173         dev_dbg(dev, "system_resume\n");
1174
1175         /*
1176          * We might have done a hard reset or the CS35L56 was power-cycled
1177          * so wait for control port to be ready.
1178          */
1179         cs35l56_wait_control_port_ready();
1180
1181         /* Undo pm_runtime_force_suspend() before re-enabling the irq */
1182         ret = pm_runtime_force_resume(dev);
1183         if (cs35l56->base.irq)
1184                 enable_irq(cs35l56->base.irq);
1185
1186         if (ret)
1187                 return ret;
1188
1189         /* Firmware won't have been loaded if the component hasn't probed */
1190         if (!cs35l56->component)
1191                 return 0;
1192
1193         ret = cs35l56_is_fw_reload_needed(&cs35l56->base);
1194         dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret);
1195         if (ret < 1)
1196                 return ret;
1197
1198         cs35l56->base.fw_patched = false;
1199         wm_adsp_power_down(&cs35l56->dsp);
1200         queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1201
1202         /*
1203          * suspend_bias_off ensures we are now in BIAS_OFF so there will be
1204          * a BIAS_OFF->BIAS_STANDBY transition to complete dsp patching.
1205          */
1206
1207         return 0;
1208 }
1209 EXPORT_SYMBOL_GPL(cs35l56_system_resume);
1210
1211 static int cs35l56_dsp_init(struct cs35l56_private *cs35l56)
1212 {
1213         struct wm_adsp *dsp;
1214         int ret;
1215
1216         cs35l56->dsp_wq = create_singlethread_workqueue("cs35l56-dsp");
1217         if (!cs35l56->dsp_wq)
1218                 return -ENOMEM;
1219
1220         INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work);
1221
1222         dsp = &cs35l56->dsp;
1223         cs35l56_init_cs_dsp(&cs35l56->base, &dsp->cs_dsp);
1224         dsp->part = "cs35l56";
1225         dsp->fw = 12;
1226         dsp->wmfw_optional = true;
1227
1228         dev_dbg(cs35l56->base.dev, "DSP system name: '%s'\n", dsp->system_name);
1229
1230         ret = wm_halo_init(dsp);
1231         if (ret != 0) {
1232                 dev_err(cs35l56->base.dev, "wm_halo_init failed\n");
1233                 return ret;
1234         }
1235
1236         return 0;
1237 }
1238
1239 static int cs35l56_get_firmware_uid(struct cs35l56_private *cs35l56)
1240 {
1241         struct device *dev = cs35l56->base.dev;
1242         const char *prop;
1243         int ret;
1244
1245         ret = device_property_read_string(dev, "cirrus,firmware-uid", &prop);
1246         /* If bad sw node property, return 0 and fallback to legacy firmware path */
1247         if (ret < 0)
1248                 return 0;
1249
1250         /* Append a speaker qualifier if there is a speaker ID */
1251         if (cs35l56->speaker_id >= 0)
1252                 cs35l56->dsp.system_name = devm_kasprintf(dev, GFP_KERNEL, "%s-spkid%d",
1253                                                           prop, cs35l56->speaker_id);
1254         else
1255                 cs35l56->dsp.system_name = devm_kstrdup(dev, prop, GFP_KERNEL);
1256
1257         if (cs35l56->dsp.system_name == NULL)
1258                 return -ENOMEM;
1259
1260         dev_dbg(dev, "Firmware UID: %s\n", cs35l56->dsp.system_name);
1261
1262         return 0;
1263 }
1264
1265 /*
1266  * Some SoundWire laptops have a spk-id-gpios property but it points to
1267  * the wrong ACPI Device node so can't be used to get the GPIO. Try to
1268  * find the SDCA node containing the GpioIo resource and add a GPIO
1269  * mapping to it.
1270  */
1271 static const struct acpi_gpio_params cs35l56_af01_first_gpio = { 0, 0, false };
1272 static const struct acpi_gpio_mapping cs35l56_af01_spkid_gpios_mapping[] = {
1273         { "spk-id-gpios", &cs35l56_af01_first_gpio, 1 },
1274         { }
1275 };
1276
1277 static void cs35l56_acpi_dev_release_driver_gpios(void *adev)
1278 {
1279         acpi_dev_remove_driver_gpios(adev);
1280 }
1281
1282 static int cs35l56_try_get_broken_sdca_spkid_gpio(struct cs35l56_private *cs35l56)
1283 {
1284         struct fwnode_handle *af01_fwnode;
1285         const union acpi_object *obj;
1286         struct gpio_desc *desc;
1287         int ret;
1288
1289         /* Find the SDCA node containing the GpioIo */
1290         af01_fwnode = device_get_named_child_node(cs35l56->base.dev, "AF01");
1291         if (!af01_fwnode) {
1292                 dev_dbg(cs35l56->base.dev, "No AF01 node\n");
1293                 return -ENOENT;
1294         }
1295
1296         ret = acpi_dev_get_property(ACPI_COMPANION(cs35l56->base.dev),
1297                                     "spk-id-gpios", ACPI_TYPE_PACKAGE, &obj);
1298         if (ret) {
1299                 dev_dbg(cs35l56->base.dev, "Could not get spk-id-gpios package: %d\n", ret);
1300                 return -ENOENT;
1301         }
1302
1303         /* The broken properties we can handle are a 4-element package (one GPIO) */
1304         if (obj->package.count != 4) {
1305                 dev_warn(cs35l56->base.dev, "Unexpected spk-id element count %d\n",
1306                          obj->package.count);
1307                 return -ENOENT;
1308         }
1309
1310         /* Add a GPIO mapping if it doesn't already have one */
1311         if (!fwnode_property_present(af01_fwnode, "spk-id-gpios")) {
1312                 struct acpi_device *adev = to_acpi_device_node(af01_fwnode);
1313
1314                 /*
1315                  * Can't use devm_acpi_dev_add_driver_gpios() because the
1316                  * mapping isn't being added to the node pointed to by
1317                  * ACPI_COMPANION().
1318                  */
1319                 ret = acpi_dev_add_driver_gpios(adev, cs35l56_af01_spkid_gpios_mapping);
1320                 if (ret) {
1321                         return dev_err_probe(cs35l56->base.dev, ret,
1322                                              "Failed to add gpio mapping to AF01\n");
1323                 }
1324
1325                 ret = devm_add_action_or_reset(cs35l56->base.dev,
1326                                                cs35l56_acpi_dev_release_driver_gpios,
1327                                                adev);
1328                 if (ret)
1329                         return ret;
1330
1331                 dev_dbg(cs35l56->base.dev, "Added spk-id-gpios mapping to AF01\n");
1332         }
1333
1334         desc = fwnode_gpiod_get_index(af01_fwnode, "spk-id", 0, GPIOD_IN, NULL);
1335         if (IS_ERR(desc)) {
1336                 ret = PTR_ERR(desc);
1337                 return dev_err_probe(cs35l56->base.dev, ret, "Get GPIO from AF01 failed\n");
1338         }
1339
1340         ret = gpiod_get_value_cansleep(desc);
1341         gpiod_put(desc);
1342
1343         if (ret < 0) {
1344                 dev_err_probe(cs35l56->base.dev, ret, "Error reading spk-id GPIO\n");
1345                 return ret;
1346                 }
1347
1348         dev_info(cs35l56->base.dev, "Got spk-id from AF01\n");
1349
1350         return ret;
1351 }
1352
1353 int cs35l56_common_probe(struct cs35l56_private *cs35l56)
1354 {
1355         int ret;
1356
1357         init_completion(&cs35l56->init_completion);
1358         mutex_init(&cs35l56->base.irq_lock);
1359         cs35l56->speaker_id = -ENOENT;
1360
1361         dev_set_drvdata(cs35l56->base.dev, cs35l56);
1362
1363         cs35l56_fill_supply_names(cs35l56->supplies);
1364         ret = devm_regulator_bulk_get(cs35l56->base.dev, ARRAY_SIZE(cs35l56->supplies),
1365                                       cs35l56->supplies);
1366         if (ret != 0)
1367                 return dev_err_probe(cs35l56->base.dev, ret, "Failed to request supplies\n");
1368
1369         /* Reset could be controlled by the BIOS or shared by multiple amps */
1370         cs35l56->base.reset_gpio = devm_gpiod_get_optional(cs35l56->base.dev, "reset",
1371                                                            GPIOD_OUT_LOW);
1372         if (IS_ERR(cs35l56->base.reset_gpio)) {
1373                 ret = PTR_ERR(cs35l56->base.reset_gpio);
1374                 /*
1375                  * If RESET is shared the first amp to probe will grab the reset
1376                  * line and reset all the amps
1377                  */
1378                 if (ret != -EBUSY)
1379                         return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n");
1380
1381                 dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n");
1382                 cs35l56->base.reset_gpio = NULL;
1383         }
1384
1385         ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1386         if (ret != 0)
1387                 return dev_err_probe(cs35l56->base.dev, ret, "Failed to enable supplies\n");
1388
1389         if (cs35l56->base.reset_gpio) {
1390                 /* ACPI can override GPIOD_OUT_LOW flag so force it to start low */
1391                 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1392                 cs35l56_wait_min_reset_pulse();
1393                 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1394         }
1395
1396         ret = cs35l56_get_speaker_id(&cs35l56->base);
1397         if (ACPI_COMPANION(cs35l56->base.dev) && cs35l56->sdw_peripheral && (ret == -ENOENT))
1398                 ret = cs35l56_try_get_broken_sdca_spkid_gpio(cs35l56);
1399
1400         if ((ret < 0) && (ret != -ENOENT))
1401                 goto err;
1402
1403         cs35l56->speaker_id = ret;
1404
1405         ret = cs35l56_get_firmware_uid(cs35l56);
1406         if (ret != 0)
1407                 goto err;
1408
1409         ret = cs35l56_dsp_init(cs35l56);
1410         if (ret < 0) {
1411                 dev_err_probe(cs35l56->base.dev, ret, "DSP init failed\n");
1412                 goto err;
1413         }
1414
1415         ret = devm_snd_soc_register_component(cs35l56->base.dev,
1416                                               &soc_component_dev_cs35l56,
1417                                               cs35l56_dai, ARRAY_SIZE(cs35l56_dai));
1418         if (ret < 0) {
1419                 dev_err_probe(cs35l56->base.dev, ret, "Register codec failed\n");
1420                 goto err;
1421         }
1422
1423         return 0;
1424
1425 err:
1426         gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1427         regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1428
1429         return ret;
1430 }
1431 EXPORT_SYMBOL_NS_GPL(cs35l56_common_probe, SND_SOC_CS35L56_CORE);
1432
1433 int cs35l56_init(struct cs35l56_private *cs35l56)
1434 {
1435         int ret;
1436
1437         /*
1438          * Check whether the actions associated with soft reset or one time
1439          * init need to be performed.
1440          */
1441         if (cs35l56->soft_resetting)
1442                 goto post_soft_reset;
1443
1444         if (cs35l56->base.init_done)
1445                 return 0;
1446
1447         pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 100);
1448         pm_runtime_use_autosuspend(cs35l56->base.dev);
1449         pm_runtime_set_active(cs35l56->base.dev);
1450         pm_runtime_enable(cs35l56->base.dev);
1451
1452         ret = cs35l56_hw_init(&cs35l56->base);
1453         if (ret < 0)
1454                 return ret;
1455
1456         ret = cs35l56_set_patch(&cs35l56->base);
1457         if (ret)
1458                 return ret;
1459
1460         if (!cs35l56->base.reset_gpio) {
1461                 dev_dbg(cs35l56->base.dev, "No reset gpio: using soft reset\n");
1462                 cs35l56->soft_resetting = true;
1463                 cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
1464                 if (cs35l56->sdw_peripheral) {
1465                         /* Keep alive while we wait for re-enumeration */
1466                         pm_runtime_get_noresume(cs35l56->base.dev);
1467                         return 0;
1468                 }
1469         }
1470
1471 post_soft_reset:
1472         if (cs35l56->soft_resetting) {
1473                 cs35l56->soft_resetting = false;
1474
1475                 /* Done re-enumerating after one-time init so release the keep-alive */
1476                 if (cs35l56->sdw_peripheral && !cs35l56->base.init_done)
1477                         pm_runtime_put_noidle(cs35l56->base.dev);
1478
1479                 regcache_mark_dirty(cs35l56->base.regmap);
1480                 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
1481                 if (ret)
1482                         return ret;
1483
1484                 dev_dbg(cs35l56->base.dev, "Firmware rebooted after soft reset\n");
1485         }
1486
1487         /* Disable auto-hibernate so that runtime_pm has control */
1488         ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
1489         if (ret)
1490                 return ret;
1491
1492         /* Registers could be dirty after soft reset or SoundWire enumeration */
1493         regcache_sync(cs35l56->base.regmap);
1494
1495         /* Set ASP1 DOUT to high-impedance when it is not transmitting audio data. */
1496         ret = regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
1497                               CS35L56_ASP1_DOUT_HIZ_CTRL_MASK);
1498         if (ret)
1499                 return dev_err_probe(cs35l56->base.dev, ret, "Failed to write ASP1_CONTROL3\n");
1500
1501         cs35l56->base.init_done = true;
1502         complete(&cs35l56->init_completion);
1503
1504         return 0;
1505 }
1506 EXPORT_SYMBOL_NS_GPL(cs35l56_init, SND_SOC_CS35L56_CORE);
1507
1508 void cs35l56_remove(struct cs35l56_private *cs35l56)
1509 {
1510         cs35l56->base.init_done = false;
1511
1512         /*
1513          * WAKE IRQs unmask if CS35L56 hibernates so free the handler to
1514          * prevent it racing with remove().
1515          */
1516         if (cs35l56->base.irq)
1517                 devm_free_irq(cs35l56->base.dev, cs35l56->base.irq, &cs35l56->base);
1518
1519         flush_workqueue(cs35l56->dsp_wq);
1520         destroy_workqueue(cs35l56->dsp_wq);
1521
1522         pm_runtime_dont_use_autosuspend(cs35l56->base.dev);
1523         pm_runtime_suspend(cs35l56->base.dev);
1524         pm_runtime_disable(cs35l56->base.dev);
1525
1526         regcache_cache_only(cs35l56->base.regmap, true);
1527
1528         gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1529         regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1530 }
1531 EXPORT_SYMBOL_NS_GPL(cs35l56_remove, SND_SOC_CS35L56_CORE);
1532
1533 #if IS_ENABLED(CONFIG_SND_SOC_CS35L56_I2C) || IS_ENABLED(CONFIG_SND_SOC_CS35L56_SPI)
1534 EXPORT_NS_GPL_DEV_PM_OPS(cs35l56_pm_ops_i2c_spi, SND_SOC_CS35L56_CORE) = {
1535         SET_RUNTIME_PM_OPS(cs35l56_runtime_suspend_i2c_spi, cs35l56_runtime_resume_i2c_spi, NULL)
1536         SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend, cs35l56_system_resume)
1537         LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_late, cs35l56_system_resume_early)
1538         NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_no_irq, cs35l56_system_resume_no_irq)
1539 };
1540 #endif
1541
1542 MODULE_DESCRIPTION("ASoC CS35L56 driver");
1543 MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED);
1544 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
1545 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
1546 MODULE_LICENSE("GPL");