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