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