File Index
Files and directories
Sign in
FILE

cs35l56.c

File preview with syntax highlighting.

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