1 // SPDX-License-Identifier: GPL-2.0-only
3 // HDA audio driver for Cirrus Logic CS35L56 smart amp
5 // Copyright (C) 2023 Cirrus Logic, Inc. and
6 // Cirrus Logic International Semiconductor Ltd.
9 #include <linux/acpi.h>
10 #include <linux/debugfs.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 #include <sound/core.h>
17 #include <sound/hda_codec.h>
18 #include <sound/tlv.h>
19 #include "cs35l56_hda.h"
20 #include "hda_component.h"
21 #include "hda_cs_dsp_ctl.h"
22 #include "hda_generic.h"
25 * The cs35l56_hda_dai_config[] reg sequence configures the device as
26 * ASP1_BCLK_FREQ = 3.072 MHz
27 * ASP1_RX_WIDTH = 32 cycles per slot, ASP1_TX_WIDTH = 32 cycles per slot, ASP1_FMT = I2S
28 * ASP1_DOUT_HIZ_CONTROL = Hi-Z during unused timeslots
29 * ASP1_RX_WL = 24 bits per sample
30 * ASP1_TX_WL = 24 bits per sample
31 * ASP1_RXn_EN 1..3 and ASP1_TXn_EN 1..4 disabled
33 static const struct reg_sequence cs35l56_hda_dai_config[] = {
34 { CS35L56_ASP1_CONTROL1, 0x00000021 },
35 { CS35L56_ASP1_CONTROL2, 0x20200200 },
36 { CS35L56_ASP1_CONTROL3, 0x00000003 },
37 { CS35L56_ASP1_DATA_CONTROL5, 0x00000018 },
38 { CS35L56_ASP1_DATA_CONTROL1, 0x00000018 },
39 { CS35L56_ASP1_ENABLES1, 0x00000000 },
42 static void cs35l56_hda_play(struct cs35l56_hda *cs35l56)
47 pm_runtime_get_sync(cs35l56->base.dev);
48 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PLAY);
50 /* Wait for firmware to enter PS0 power state */
51 ret = regmap_read_poll_timeout(cs35l56->base.regmap,
52 CS35L56_TRANSDUCER_ACTUAL_PS,
53 val, (val == CS35L56_PS0),
55 CS35L56_PS0_TIMEOUT_US);
57 dev_warn(cs35l56->base.dev, "PS0 wait failed: %d\n", ret);
59 regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_ENABLES1,
60 BIT(CS35L56_ASP_RX1_EN_SHIFT) | BIT(CS35L56_ASP_RX2_EN_SHIFT) |
61 cs35l56->asp_tx_mask);
62 cs35l56->playing = true;
65 static void cs35l56_hda_pause(struct cs35l56_hda *cs35l56)
67 cs35l56->playing = false;
68 cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PAUSE);
69 regmap_clear_bits(cs35l56->base.regmap, CS35L56_ASP1_ENABLES1,
70 BIT(CS35L56_ASP_RX1_EN_SHIFT) | BIT(CS35L56_ASP_RX2_EN_SHIFT) |
71 BIT(CS35L56_ASP_TX1_EN_SHIFT) | BIT(CS35L56_ASP_TX2_EN_SHIFT) |
72 BIT(CS35L56_ASP_TX3_EN_SHIFT) | BIT(CS35L56_ASP_TX4_EN_SHIFT));
74 pm_runtime_mark_last_busy(cs35l56->base.dev);
75 pm_runtime_put_autosuspend(cs35l56->base.dev);
78 static void cs35l56_hda_playback_hook(struct device *dev, int action)
80 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
82 dev_dbg(cs35l56->base.dev, "%s()%d: action: %d\n", __func__, __LINE__, action);
85 case HDA_GEN_PCM_ACT_PREPARE:
89 /* If we're suspended: flag that resume should start playback */
90 if (cs35l56->suspended) {
91 cs35l56->playing = true;
95 cs35l56_hda_play(cs35l56);
97 case HDA_GEN_PCM_ACT_CLEANUP:
98 if (!cs35l56->playing)
101 cs35l56_hda_pause(cs35l56);
108 static int __maybe_unused cs35l56_hda_runtime_suspend(struct device *dev)
110 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
112 if (cs35l56->cs_dsp.booted)
113 cs_dsp_stop(&cs35l56->cs_dsp);
115 return cs35l56_runtime_suspend_common(&cs35l56->base);
118 static int __maybe_unused cs35l56_hda_runtime_resume(struct device *dev)
120 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
123 ret = cs35l56_runtime_resume_common(&cs35l56->base, false);
127 if (cs35l56->cs_dsp.booted) {
128 ret = cs_dsp_run(&cs35l56->cs_dsp);
130 dev_dbg(cs35l56->base.dev, "%s: cs_dsp_run ret %d\n", __func__, ret);
138 cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_ALLOW_AUTO_HIBERNATE);
139 regmap_write(cs35l56->base.regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
140 CS35L56_MBOX_CMD_HIBERNATE_NOW);
142 regcache_cache_only(cs35l56->base.regmap, true);
147 static int cs35l56_hda_mixer_info(struct snd_kcontrol *kcontrol,
148 struct snd_ctl_elem_info *uinfo)
150 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
152 uinfo->value.enumerated.items = CS35L56_NUM_INPUT_SRC;
153 if (uinfo->value.enumerated.item >= CS35L56_NUM_INPUT_SRC)
154 uinfo->value.enumerated.item = CS35L56_NUM_INPUT_SRC - 1;
155 strscpy(uinfo->value.enumerated.name, cs35l56_tx_input_texts[uinfo->value.enumerated.item],
156 sizeof(uinfo->value.enumerated.name));
161 static int cs35l56_hda_mixer_get(struct snd_kcontrol *kcontrol,
162 struct snd_ctl_elem_value *ucontrol)
164 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
165 unsigned int reg_val;
168 regmap_read(cs35l56->base.regmap, kcontrol->private_value, ®_val);
169 reg_val &= CS35L56_ASP_TXn_SRC_MASK;
171 for (i = 0; i < CS35L56_NUM_INPUT_SRC; ++i) {
172 if (cs35l56_tx_input_values[i] == reg_val) {
173 ucontrol->value.enumerated.item[0] = i;
181 static int cs35l56_hda_mixer_put(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
184 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
185 unsigned int item = ucontrol->value.enumerated.item[0];
188 if (item >= CS35L56_NUM_INPUT_SRC)
191 regmap_update_bits_check(cs35l56->base.regmap, kcontrol->private_value,
192 CS35L56_INPUT_MASK, cs35l56_tx_input_values[item],
198 static int cs35l56_hda_posture_info(struct snd_kcontrol *kcontrol,
199 struct snd_ctl_elem_info *uinfo)
201 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
203 uinfo->value.integer.min = CS35L56_MAIN_POSTURE_MIN;
204 uinfo->value.integer.max = CS35L56_MAIN_POSTURE_MAX;
208 static int cs35l56_hda_posture_get(struct snd_kcontrol *kcontrol,
209 struct snd_ctl_elem_value *ucontrol)
211 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
215 ret = regmap_read(cs35l56->base.regmap, CS35L56_MAIN_POSTURE_NUMBER, &pos);
219 ucontrol->value.integer.value[0] = pos;
224 static int cs35l56_hda_posture_put(struct snd_kcontrol *kcontrol,
225 struct snd_ctl_elem_value *ucontrol)
227 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
228 unsigned long pos = ucontrol->value.integer.value[0];
232 if ((pos < CS35L56_MAIN_POSTURE_MIN) ||
233 (pos > CS35L56_MAIN_POSTURE_MAX))
236 ret = regmap_update_bits_check(cs35l56->base.regmap,
237 CS35L56_MAIN_POSTURE_NUMBER,
238 CS35L56_MAIN_POSTURE_MASK,
246 static const struct {
249 } cs35l56_hda_mixer_controls[] = {
250 { "ASP1 TX1 Source", CS35L56_ASP1TX1_INPUT },
251 { "ASP1 TX2 Source", CS35L56_ASP1TX2_INPUT },
252 { "ASP1 TX3 Source", CS35L56_ASP1TX3_INPUT },
253 { "ASP1 TX4 Source", CS35L56_ASP1TX4_INPUT },
256 static const DECLARE_TLV_DB_SCALE(cs35l56_hda_vol_tlv, -10000, 25, 0);
258 static int cs35l56_hda_vol_info(struct snd_kcontrol *kcontrol,
259 struct snd_ctl_elem_info *uinfo)
261 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
263 uinfo->value.integer.step = 1;
264 uinfo->value.integer.min = 0;
265 uinfo->value.integer.max = CS35L56_MAIN_RENDER_USER_VOLUME_MAX -
266 CS35L56_MAIN_RENDER_USER_VOLUME_MIN;
271 static int cs35l56_hda_vol_get(struct snd_kcontrol *kcontrol,
272 struct snd_ctl_elem_value *ucontrol)
274 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
275 unsigned int raw_vol;
279 ret = regmap_read(cs35l56->base.regmap, CS35L56_MAIN_RENDER_USER_VOLUME, &raw_vol);
284 vol = (s16)(raw_vol & 0xFFFF);
285 vol >>= CS35L56_MAIN_RENDER_USER_VOLUME_SHIFT;
287 if (vol & BIT(CS35L56_MAIN_RENDER_USER_VOLUME_SIGNBIT))
288 vol |= ~((int)(BIT(CS35L56_MAIN_RENDER_USER_VOLUME_SIGNBIT) - 1));
290 ucontrol->value.integer.value[0] = vol - CS35L56_MAIN_RENDER_USER_VOLUME_MIN;
295 static int cs35l56_hda_vol_put(struct snd_kcontrol *kcontrol,
296 struct snd_ctl_elem_value *ucontrol)
298 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
299 long vol = ucontrol->value.integer.value[0];
300 unsigned int raw_vol;
304 if ((vol < 0) || (vol > (CS35L56_MAIN_RENDER_USER_VOLUME_MAX -
305 CS35L56_MAIN_RENDER_USER_VOLUME_MIN)))
308 raw_vol = (vol + CS35L56_MAIN_RENDER_USER_VOLUME_MIN) <<
309 CS35L56_MAIN_RENDER_USER_VOLUME_SHIFT;
311 ret = regmap_update_bits_check(cs35l56->base.regmap,
312 CS35L56_MAIN_RENDER_USER_VOLUME,
313 CS35L56_MAIN_RENDER_USER_VOLUME_MASK,
321 static void cs35l56_hda_create_controls(struct cs35l56_hda *cs35l56)
323 struct snd_kcontrol_new ctl_template = {
324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
325 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
326 .info = cs35l56_hda_posture_info,
327 .get = cs35l56_hda_posture_get,
328 .put = cs35l56_hda_posture_put,
333 snprintf(name, sizeof(name), "%s Posture Number", cs35l56->amp_name);
334 ctl_template.name = name;
335 cs35l56->posture_ctl = snd_ctl_new1(&ctl_template, cs35l56);
336 if (snd_ctl_add(cs35l56->codec->card, cs35l56->posture_ctl))
337 dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n", ctl_template.name);
340 ctl_template.info = cs35l56_hda_mixer_info;
341 ctl_template.get = cs35l56_hda_mixer_get;
342 ctl_template.put = cs35l56_hda_mixer_put;
344 BUILD_BUG_ON(ARRAY_SIZE(cs35l56->mixer_ctl) != ARRAY_SIZE(cs35l56_hda_mixer_controls));
346 for (i = 0; i < ARRAY_SIZE(cs35l56_hda_mixer_controls); ++i) {
347 snprintf(name, sizeof(name), "%s %s", cs35l56->amp_name,
348 cs35l56_hda_mixer_controls[i].name);
349 ctl_template.private_value = cs35l56_hda_mixer_controls[i].reg;
350 cs35l56->mixer_ctl[i] = snd_ctl_new1(&ctl_template, cs35l56);
351 if (snd_ctl_add(cs35l56->codec->card, cs35l56->mixer_ctl[i])) {
352 dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n",
357 ctl_template.info = cs35l56_hda_vol_info;
358 ctl_template.get = cs35l56_hda_vol_get;
359 ctl_template.put = cs35l56_hda_vol_put;
360 ctl_template.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ);
361 ctl_template.tlv.p = cs35l56_hda_vol_tlv;
362 snprintf(name, sizeof(name), "%s Speaker Playback Volume", cs35l56->amp_name);
363 ctl_template.name = name;
364 cs35l56->volume_ctl = snd_ctl_new1(&ctl_template, cs35l56);
365 if (snd_ctl_add(cs35l56->codec->card, cs35l56->volume_ctl))
366 dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n", ctl_template.name);
369 static void cs35l56_hda_remove_controls(struct cs35l56_hda *cs35l56)
373 for (i = ARRAY_SIZE(cs35l56->mixer_ctl) - 1; i >= 0; i--)
374 snd_ctl_remove(cs35l56->codec->card, cs35l56->mixer_ctl[i]);
376 snd_ctl_remove(cs35l56->codec->card, cs35l56->posture_ctl);
377 snd_ctl_remove(cs35l56->codec->card, cs35l56->volume_ctl);
380 static const struct cs_dsp_client_ops cs35l56_hda_client_ops = {
381 .control_remove = hda_cs_dsp_control_remove,
384 static int cs35l56_hda_request_firmware_file(struct cs35l56_hda *cs35l56,
385 const struct firmware **firmware, char **filename,
386 const char *dir, const char *system_name,
387 const char *amp_name,
388 const char *filetype)
393 if (system_name && amp_name)
394 *filename = kasprintf(GFP_KERNEL, "%scs35l56%s-%02x-dsp1-misc-%s-%s.%s", dir,
395 cs35l56->base.secured ? "s" : "", cs35l56->base.rev,
396 system_name, amp_name, filetype);
397 else if (system_name)
398 *filename = kasprintf(GFP_KERNEL, "%scs35l56%s-%02x-dsp1-misc-%s.%s", dir,
399 cs35l56->base.secured ? "s" : "", cs35l56->base.rev,
400 system_name, filetype);
402 *filename = kasprintf(GFP_KERNEL, "%scs35l56%s-%02x-dsp1-misc.%s", dir,
403 cs35l56->base.secured ? "s" : "", cs35l56->base.rev,
410 * Make sure that filename is lower-case and any non alpha-numeric
411 * characters except full stop and forward slash are replaced with
419 else if (c != '.' && c != '/')
424 ret = firmware_request_nowarn(firmware, *filename, cs35l56->base.dev);
426 dev_dbg(cs35l56->base.dev, "Failed to request '%s'\n", *filename);
432 dev_dbg(cs35l56->base.dev, "Found '%s'\n", *filename);
437 static const char cirrus_dir[] = "cirrus/";
438 static void cs35l56_hda_request_firmware_files(struct cs35l56_hda *cs35l56,
439 const struct firmware **wmfw_firmware,
440 char **wmfw_filename,
441 const struct firmware **coeff_firmware,
442 char **coeff_filename)
444 const char *system_name = cs35l56->system_name;
445 const char *amp_name = cs35l56->amp_name;
448 if (system_name && amp_name) {
449 if (!cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename,
450 cirrus_dir, system_name, amp_name, "wmfw")) {
451 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
452 cirrus_dir, system_name, amp_name, "bin");
458 if (!cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename,
459 cirrus_dir, system_name, NULL, "wmfw")) {
461 cs35l56_hda_request_firmware_file(cs35l56,
462 coeff_firmware, coeff_filename,
463 cirrus_dir, system_name,
465 if (!*coeff_firmware)
466 cs35l56_hda_request_firmware_file(cs35l56,
467 coeff_firmware, coeff_filename,
468 cirrus_dir, system_name,
474 ret = cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename,
475 cirrus_dir, NULL, NULL, "wmfw");
477 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
478 cirrus_dir, NULL, NULL, "bin");
482 /* When a firmware file is not found must still search for the coeff files */
485 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
486 cirrus_dir, system_name, amp_name, "bin");
487 if (!*coeff_firmware)
488 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
489 cirrus_dir, system_name, NULL, "bin");
492 if (!*coeff_firmware)
493 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
494 cirrus_dir, NULL, NULL, "bin");
497 static void cs35l56_hda_release_firmware_files(const struct firmware *wmfw_firmware,
499 const struct firmware *coeff_firmware,
500 char *coeff_filename)
503 release_firmware(wmfw_firmware);
504 kfree(wmfw_filename);
507 release_firmware(coeff_firmware);
508 kfree(coeff_filename);
511 static void cs35l56_hda_add_dsp_controls(struct cs35l56_hda *cs35l56)
513 struct hda_cs_dsp_ctl_info info;
515 info.device_name = cs35l56->amp_name;
516 info.fw_type = HDA_CS_DSP_FW_MISC;
517 info.card = cs35l56->codec->card;
519 hda_cs_dsp_add_controls(&cs35l56->cs_dsp, &info);
522 static int cs35l56_hda_fw_load(struct cs35l56_hda *cs35l56)
524 const struct firmware *coeff_firmware = NULL;
525 const struct firmware *wmfw_firmware = NULL;
526 char *coeff_filename = NULL;
527 char *wmfw_filename = NULL;
530 cs35l56_hda_request_firmware_files(cs35l56, &wmfw_firmware, &wmfw_filename,
531 &coeff_firmware, &coeff_filename);
533 /* Nothing to do - no firmware files were found to download */
534 if (!wmfw_filename && !coeff_filename)
537 mutex_lock(&cs35l56->base.irq_lock);
538 pm_runtime_get_sync(cs35l56->base.dev);
541 * When the device is running in secure mode the firmware files can
542 * only contain insecure tunings and therefore we do not need to
543 * shutdown the firmware to apply them and can use the lower cost
544 * reinit sequence instead.
546 if (!cs35l56->base.secured) {
547 ret = cs35l56_firmware_shutdown(&cs35l56->base);
552 ret = cs_dsp_power_up(&cs35l56->cs_dsp, wmfw_firmware, wmfw_filename,
553 coeff_firmware, coeff_filename, "misc");
555 dev_dbg(cs35l56->base.dev, "%s: cs_dsp_power_up ret %d\n", __func__, ret);
560 dev_dbg(cs35l56->base.dev, "Loaded WMFW Firmware: %s\n", wmfw_filename);
563 dev_dbg(cs35l56->base.dev, "Loaded Coefficients: %s\n", coeff_filename);
565 ret = cs_dsp_run(&cs35l56->cs_dsp);
567 dev_dbg(cs35l56->base.dev, "%s: cs_dsp_run ret %d\n", __func__, ret);
571 if (cs35l56->base.secured) {
572 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
576 /* Reset the device and wait for it to boot */
577 cs35l56_system_reset(&cs35l56->base, false);
578 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
583 /* Disable auto-hibernate so that runtime_pm has control */
584 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
588 regcache_mark_dirty(cs35l56->base.regmap);
589 regcache_sync(cs35l56->base.regmap);
591 regmap_clear_bits(cs35l56->base.regmap, CS35L56_PROTECTION_STATUS,
592 CS35L56_FIRMWARE_MISSING);
593 cs35l56->base.fw_patched = true;
595 pm_runtime_put(cs35l56->base.dev);
596 mutex_unlock(&cs35l56->base.irq_lock);
598 cs35l56_hda_release_firmware_files(wmfw_firmware, wmfw_filename,
599 coeff_firmware, coeff_filename);
604 static int cs35l56_hda_bind(struct device *dev, struct device *master, void *master_data)
606 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
607 struct hda_component *comps = master_data;
610 if (!comps || cs35l56->index < 0 || cs35l56->index >= HDA_MAX_COMPONENTS)
613 comps = &comps[cs35l56->index];
618 cs35l56->codec = comps->codec;
619 strscpy(comps->name, dev_name(dev), sizeof(comps->name));
620 comps->playback_hook = cs35l56_hda_playback_hook;
622 ret = cs35l56_hda_fw_load(cs35l56);
626 cs35l56_hda_create_controls(cs35l56);
627 cs35l56_hda_add_dsp_controls(cs35l56);
629 #if IS_ENABLED(CONFIG_SND_DEBUG)
630 cs35l56->debugfs_root = debugfs_create_dir(dev_name(cs35l56->base.dev), sound_debugfs_root);
631 cs_dsp_init_debugfs(&cs35l56->cs_dsp, cs35l56->debugfs_root);
634 dev_dbg(cs35l56->base.dev, "Bound\n");
639 static void cs35l56_hda_unbind(struct device *dev, struct device *master, void *master_data)
641 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
642 struct hda_component *comps = master_data;
644 cs35l56_hda_remove_controls(cs35l56);
646 #if IS_ENABLED(CONFIG_SND_DEBUG)
647 cs_dsp_cleanup_debugfs(&cs35l56->cs_dsp);
648 debugfs_remove_recursive(cs35l56->debugfs_root);
651 cs_dsp_remove(&cs35l56->cs_dsp);
653 if (comps[cs35l56->index].dev == dev)
654 memset(&comps[cs35l56->index], 0, sizeof(*comps));
656 dev_dbg(cs35l56->base.dev, "Unbound\n");
659 static const struct component_ops cs35l56_hda_comp_ops = {
660 .bind = cs35l56_hda_bind,
661 .unbind = cs35l56_hda_unbind,
664 static int cs35l56_hda_system_suspend(struct device *dev)
666 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
668 if (cs35l56->playing)
669 cs35l56_hda_pause(cs35l56);
671 cs35l56->suspended = true;
674 * The interrupt line is normally shared, but after we start suspending
675 * we can't check if our device is the source of an interrupt, and can't
676 * clear it. Prevent this race by temporarily disabling the parent irq
677 * until we reach _no_irq.
679 if (cs35l56->base.irq)
680 disable_irq(cs35l56->base.irq);
682 return pm_runtime_force_suspend(dev);
685 static int cs35l56_hda_system_suspend_late(struct device *dev)
687 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
690 * RESET is usually shared by all amps so it must not be asserted until
691 * all driver instances have done their suspend() stage.
693 if (cs35l56->base.reset_gpio) {
694 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
695 cs35l56_wait_min_reset_pulse();
701 static int cs35l56_hda_system_suspend_no_irq(struct device *dev)
703 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
705 /* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
706 if (cs35l56->base.irq)
707 enable_irq(cs35l56->base.irq);
712 static int cs35l56_hda_system_resume_no_irq(struct device *dev)
714 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
717 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
718 * spurious interrupts, and the interrupt line is normally shared.
719 * We can't check if our device is the source of an interrupt, and can't
720 * clear it, until it has fully resumed. Prevent this race by temporarily
721 * disabling the parent irq until we complete resume().
723 if (cs35l56->base.irq)
724 disable_irq(cs35l56->base.irq);
729 static int cs35l56_hda_system_resume_early(struct device *dev)
731 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
733 /* Ensure a spec-compliant RESET pulse. */
734 if (cs35l56->base.reset_gpio) {
735 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
736 cs35l56_wait_min_reset_pulse();
738 /* Release shared RESET before drivers start resume(). */
739 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
740 cs35l56_wait_control_port_ready();
746 static int cs35l56_hda_system_resume(struct device *dev)
748 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
751 /* Undo pm_runtime_force_suspend() before re-enabling the irq */
752 ret = pm_runtime_force_resume(dev);
753 if (cs35l56->base.irq)
754 enable_irq(cs35l56->base.irq);
759 cs35l56->suspended = false;
761 ret = cs35l56_is_fw_reload_needed(&cs35l56->base);
762 dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret);
764 ret = cs35l56_hda_fw_load(cs35l56);
769 if (cs35l56->playing)
770 cs35l56_hda_play(cs35l56);
775 static int cs35l56_hda_read_acpi(struct cs35l56_hda *cs35l56, int id)
777 u32 values[HDA_MAX_COMPONENTS];
778 struct acpi_device *adev;
779 const char *property, *sub;
784 * ACPI_COMPANION isn't available when this driver was instantiated by
785 * the serial-multi-instantiate driver, so lookup the node by HID
787 if (!ACPI_COMPANION(cs35l56->base.dev)) {
788 adev = acpi_dev_get_first_match_dev("CSC3556", NULL, -1);
790 dev_err(cs35l56->base.dev, "Failed to find an ACPI device for %s\n",
791 dev_name(cs35l56->base.dev));
794 ACPI_COMPANION_SET(cs35l56->base.dev, adev);
797 property = "cirrus,dev-index";
798 ret = device_property_count_u32(cs35l56->base.dev, property);
802 if (ret > ARRAY_SIZE(values)) {
808 ret = device_property_read_u32_array(cs35l56->base.dev, property, values, nval);
813 for (i = 0; i < nval; i++) {
814 if (values[i] == id) {
819 if (cs35l56->index == -1) {
820 dev_err(cs35l56->base.dev, "No index found in %s\n", property);
825 sub = acpi_get_subsystem_id(ACPI_HANDLE(cs35l56->base.dev));
828 /* If no ACPI SUB, return 0 and fallback to legacy firmware path, otherwise fail */
829 if (PTR_ERR(sub) == -ENODATA)
835 cs35l56->system_name = sub;
837 cs35l56->base.reset_gpio = devm_gpiod_get_index_optional(cs35l56->base.dev,
841 if (IS_ERR(cs35l56->base.reset_gpio)) {
842 ret = PTR_ERR(cs35l56->base.reset_gpio);
845 * If RESET is shared the first amp to probe will grab the reset
846 * line and reset all the amps
849 return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n");
851 dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n");
852 cs35l56->base.reset_gpio = NULL;
858 dev_err(cs35l56->base.dev, "Failed property %s: %d\n", property, ret);
863 int cs35l56_hda_common_probe(struct cs35l56_hda *cs35l56, int id)
867 mutex_init(&cs35l56->base.irq_lock);
868 dev_set_drvdata(cs35l56->base.dev, cs35l56);
870 ret = cs35l56_hda_read_acpi(cs35l56, id);
872 dev_err_probe(cs35l56->base.dev, ret, "Platform not supported\n");
876 cs35l56->amp_name = devm_kasprintf(cs35l56->base.dev, GFP_KERNEL, "AMP%d",
878 if (!cs35l56->amp_name) {
883 cs35l56_init_cs_dsp(&cs35l56->base, &cs35l56->cs_dsp);
884 cs35l56->cs_dsp.client_ops = &cs35l56_hda_client_ops;
886 if (cs35l56->base.reset_gpio) {
887 dev_dbg(cs35l56->base.dev, "Hard reset\n");
890 * The GPIOD_OUT_LOW to *_gpiod_get_*() will be ignored if the
891 * ACPI defines a different default state. So explicitly set low.
893 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
894 cs35l56_wait_min_reset_pulse();
895 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
898 ret = cs35l56_hw_init(&cs35l56->base);
902 /* Reset the device and wait for it to boot */
903 cs35l56_system_reset(&cs35l56->base, false);
904 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
908 ret = cs35l56_set_patch(&cs35l56->base);
912 regcache_mark_dirty(cs35l56->base.regmap);
913 regcache_sync(cs35l56->base.regmap);
915 /* Disable auto-hibernate so that runtime_pm has control */
916 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
920 ret = cs_dsp_halo_init(&cs35l56->cs_dsp);
922 dev_err_probe(cs35l56->base.dev, ret, "cs_dsp_halo_init failed\n");
926 dev_dbg(cs35l56->base.dev, "DSP system name: '%s', amp name: '%s'\n",
927 cs35l56->system_name, cs35l56->amp_name);
929 regmap_multi_reg_write(cs35l56->base.regmap, cs35l56_hda_dai_config,
930 ARRAY_SIZE(cs35l56_hda_dai_config));
933 * By default only enable one ASP1TXn, where n=amplifier index,
934 * This prevents multiple amps trying to drive the same slot.
936 cs35l56->asp_tx_mask = BIT(cs35l56->index);
938 pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 3000);
939 pm_runtime_use_autosuspend(cs35l56->base.dev);
940 pm_runtime_set_active(cs35l56->base.dev);
941 pm_runtime_mark_last_busy(cs35l56->base.dev);
942 pm_runtime_enable(cs35l56->base.dev);
944 ret = component_add(cs35l56->base.dev, &cs35l56_hda_comp_ops);
946 dev_err(cs35l56->base.dev, "Register component failed: %d\n", ret);
950 cs35l56->base.init_done = true;
955 pm_runtime_disable(cs35l56->base.dev);
957 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
961 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_common_probe, SND_HDA_SCODEC_CS35L56);
963 void cs35l56_hda_remove(struct device *dev)
965 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
967 pm_runtime_get_sync(cs35l56->base.dev);
968 pm_runtime_disable(cs35l56->base.dev);
970 component_del(cs35l56->base.dev, &cs35l56_hda_comp_ops);
972 kfree(cs35l56->system_name);
973 pm_runtime_put_noidle(cs35l56->base.dev);
975 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
977 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_remove, SND_HDA_SCODEC_CS35L56);
979 const struct dev_pm_ops cs35l56_hda_pm_ops = {
980 SET_RUNTIME_PM_OPS(cs35l56_hda_runtime_suspend, cs35l56_hda_runtime_resume, NULL)
981 SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend, cs35l56_hda_system_resume)
982 LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend_late,
983 cs35l56_hda_system_resume_early)
984 NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend_no_irq,
985 cs35l56_hda_system_resume_no_irq)
987 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_pm_ops, SND_HDA_SCODEC_CS35L56);
989 MODULE_DESCRIPTION("CS35L56 HDA Driver");
990 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS);
991 MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED);
992 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
993 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
994 MODULE_LICENSE("GPL");
995 MODULE_IMPORT_NS(FW_CS_DSP);