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