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 459 dev_dbg(cs35l56->base.dev, "%s: %#x\n", __func__, fmt); 460 461 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 462 case SND_SOC_DAIFMT_CBC_CFC: 463 break; 464 default: 465 dev_err(cs35l56->base.dev, "Unsupported clock source mode\n"); 466 return -EINVAL; 467 } 468 469 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 470 case SND_SOC_DAIFMT_DSP_A: 471 val = CS35L56_ASP_FMT_DSP_A << CS35L56_ASP_FMT_SHIFT; 472 cs35l56->tdm_mode = true; 473 break; 474 case SND_SOC_DAIFMT_I2S: 475 val = CS35L56_ASP_FMT_I2S << CS35L56_ASP_FMT_SHIFT; 476 cs35l56->tdm_mode = false; 477 break; 478 default: 479 dev_err(cs35l56->base.dev, "Unsupported DAI format\n"); 480 return -EINVAL; 481 } 482 483 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 484 case SND_SOC_DAIFMT_NB_IF: 485 val |= CS35L56_ASP_FSYNC_INV_MASK; 486 break; 487 case SND_SOC_DAIFMT_IB_NF: 488 val |= CS35L56_ASP_BCLK_INV_MASK; 489 break; 490 case SND_SOC_DAIFMT_IB_IF: 491 val |= CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK; 492 break; 493 case SND_SOC_DAIFMT_NB_NF: 494 break; 495 default: 496 dev_err(cs35l56->base.dev, "Invalid clock invert\n"); 497 return -EINVAL; 498 } 499 500 regmap_update_bits(cs35l56->base.regmap, 501 CS35L56_ASP1_CONTROL2, 502 CS35L56_ASP_FMT_MASK | 503 CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK, 504 val); 505 506 /* Hi-Z DOUT in unused slots and when all TX are disabled */ 507 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3, 508 CS35L56_ASP1_DOUT_HIZ_CTRL_MASK, 509 CS35L56_ASP_UNUSED_HIZ_OFF_HIZ); 510 511 return 0; 512 } 513 514 static unsigned int cs35l56_make_tdm_config_word(unsigned int reg_val, unsigned long mask) 515 { 516 unsigned int channel_shift; 517 int bit_num; 518 519 /* Enable consecutive TX1..TXn for each of the slots set in mask */ 520 channel_shift = 0; 521 for_each_set_bit(bit_num, &mask, 32) { 522 reg_val &= ~(0x3f << channel_shift); 523 reg_val |= bit_num << channel_shift; 524 channel_shift += 8; 525 } 526 527 return reg_val; 528 } 529 530 static int cs35l56_asp_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, 531 unsigned int rx_mask, int slots, int slot_width) 532 { 533 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component); 534 535 if ((slots == 0) || (slot_width == 0)) { 536 dev_dbg(cs35l56->base.dev, "tdm config cleared\n"); 537 cs35l56->asp_slot_width = 0; 538 cs35l56->asp_slot_count = 0; 539 return 0; 540 } 541 542 if (slot_width > (CS35L56_ASP_RX_WIDTH_MASK >> CS35L56_ASP_RX_WIDTH_SHIFT)) { 543 dev_err(cs35l56->base.dev, "tdm invalid slot width %d\n", slot_width); 544 return -EINVAL; 545 } 546 547 /* More than 32 slots would give an unsupportable BCLK frequency */ 548 if (slots > 32) { 549 dev_err(cs35l56->base.dev, "tdm invalid slot count %d\n", slots); 550 return -EINVAL; 551 } 552 553 cs35l56->asp_slot_width = (u8)slot_width; 554 cs35l56->asp_slot_count = (u8)slots; 555 556 // Note: rx/tx is from point of view of the CPU end 557 if (tx_mask == 0) 558 tx_mask = 0x3; // ASPRX1/RX2 in slots 0 and 1 559 560 if (rx_mask == 0) 561 rx_mask = 0xf; // ASPTX1..TX4 in slots 0..3 562 563 /* Default unused slots to 63 */ 564 regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL1, 565 cs35l56_make_tdm_config_word(0x3f3f3f3f, rx_mask)); 566 regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL5, 567 cs35l56_make_tdm_config_word(0x3f3f3f, tx_mask)); 568 569 dev_dbg(cs35l56->base.dev, "tdm slot width: %u count: %u tx_mask: %#x rx_mask: %#x\n", 570 cs35l56->asp_slot_width, cs35l56->asp_slot_count, tx_mask, rx_mask); 571 572 return 0; 573 } 574 575 static int cs35l56_asp_dai_hw_params(struct snd_pcm_substream *substream, 576 struct snd_pcm_hw_params *params, 577 struct snd_soc_dai *dai) 578 { 579 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component); 580 unsigned int rate = params_rate(params); 581 u8 asp_width, asp_wl; 582 583 asp_wl = params_width(params); 584 if (cs35l56->asp_slot_width) 585 asp_width = cs35l56->asp_slot_width; 586 else 587 asp_width = asp_wl; 588 589 dev_dbg(cs35l56->base.dev, "%s: wl=%d, width=%d, rate=%d", 590 __func__, asp_wl, asp_width, rate); 591 592 if (!cs35l56->sysclk_set) { 593 unsigned int slots = cs35l56->asp_slot_count; 594 unsigned int bclk_freq; 595 int freq_id; 596 597 if (slots == 0) { 598 slots = params_channels(params); 599 600 /* I2S always has an even number of slots */ 601 if (!cs35l56->tdm_mode) 602 slots = round_up(slots, 2); 603 } 604 605 bclk_freq = asp_width * slots * rate; 606 freq_id = cs35l56_get_bclk_freq_id(bclk_freq); 607 if (freq_id < 0) { 608 dev_err(cs35l56->base.dev, "%s: Invalid BCLK %u\n", __func__, bclk_freq); 609 return -EINVAL; 610 } 611 612 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1, 613 CS35L56_ASP_BCLK_FREQ_MASK, 614 freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT); 615 } 616 617 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 618 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2, 619 CS35L56_ASP_RX_WIDTH_MASK, asp_width << 620 CS35L56_ASP_RX_WIDTH_SHIFT); 621 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL5, 622 CS35L56_ASP_RX_WL_MASK, asp_wl); 623 } else { 624 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2, 625 CS35L56_ASP_TX_WIDTH_MASK, asp_width << 626 CS35L56_ASP_TX_WIDTH_SHIFT); 627 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL1, 628 CS35L56_ASP_TX_WL_MASK, asp_wl); 629 } 630 631 return 0; 632 } 633 634 static int cs35l56_asp_dai_set_sysclk(struct snd_soc_dai *dai, 635 int clk_id, unsigned int freq, int dir) 636 { 637 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component); 638 int freq_id; 639 640 if (freq == 0) { 641 cs35l56->sysclk_set = false; 642 return 0; 643 } 644 645 freq_id = cs35l56_get_bclk_freq_id(freq); 646 if (freq_id < 0) 647 return freq_id; 648 649 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1, 650 CS35L56_ASP_BCLK_FREQ_MASK, 651 freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT); 652 cs35l56->sysclk_set = true; 653 654 return 0; 655 } 656 657 static const struct snd_soc_dai_ops cs35l56_ops = { 658 .set_fmt = cs35l56_asp_dai_set_fmt, 659 .set_tdm_slot = cs35l56_asp_dai_set_tdm_slot, 660 .hw_params = cs35l56_asp_dai_hw_params, 661 .set_sysclk = cs35l56_asp_dai_set_sysclk, 662 }; 663 664 static void cs35l56_sdw_dai_shutdown(struct snd_pcm_substream *substream, 665 struct snd_soc_dai *dai) 666 { 667 snd_soc_dai_set_dma_data(dai, substream, NULL); 668 } 669 670 static int cs35l56_sdw_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, 671 unsigned int rx_mask, int slots, int slot_width) 672 { 673 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component); 674 675 /* rx/tx are from point of view of the CPU end so opposite to our rx/tx */ 676 cs35l56->rx_mask = tx_mask; 677 cs35l56->tx_mask = rx_mask; 678 679 return 0; 680 } 681 682 static int cs35l56_sdw_dai_hw_params(struct snd_pcm_substream *substream, 683 struct snd_pcm_hw_params *params, 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 struct sdw_stream_config sconfig; 689 struct sdw_port_config pconfig; 690 int ret; 691 692 dev_dbg(cs35l56->base.dev, "%s: rate %d\n", __func__, params_rate(params)); 693 694 if (!cs35l56->base.init_done) 695 return -ENODEV; 696 697 if (!sdw_stream) 698 return -EINVAL; 699 700 memset(&sconfig, 0, sizeof(sconfig)); 701 memset(&pconfig, 0, sizeof(pconfig)); 702 703 sconfig.frame_rate = params_rate(params); 704 sconfig.bps = snd_pcm_format_width(params_format(params)); 705 706 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 707 sconfig.direction = SDW_DATA_DIR_RX; 708 pconfig.num = CS35L56_SDW1_PLAYBACK_PORT; 709 pconfig.ch_mask = cs35l56->rx_mask; 710 } else { 711 sconfig.direction = SDW_DATA_DIR_TX; 712 pconfig.num = CS35L56_SDW1_CAPTURE_PORT; 713 pconfig.ch_mask = cs35l56->tx_mask; 714 } 715 716 if (pconfig.ch_mask == 0) { 717 sconfig.ch_count = params_channels(params); 718 pconfig.ch_mask = GENMASK(sconfig.ch_count - 1, 0); 719 } else { 720 sconfig.ch_count = hweight32(pconfig.ch_mask); 721 } 722 723 ret = sdw_stream_add_slave(cs35l56->sdw_peripheral, &sconfig, &pconfig, 724 1, sdw_stream); 725 if (ret) { 726 dev_err(dai->dev, "Failed to add sdw stream: %d\n", ret); 727 return ret; 728 } 729 730 return 0; 731 } 732 733 static int cs35l56_sdw_dai_hw_free(struct snd_pcm_substream *substream, 734 struct snd_soc_dai *dai) 735 { 736 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component); 737 struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream); 738 739 if (!cs35l56->sdw_peripheral) 740 return -EINVAL; 741 742 sdw_stream_remove_slave(cs35l56->sdw_peripheral, sdw_stream); 743 744 return 0; 745 } 746 747 static int cs35l56_sdw_dai_set_stream(struct snd_soc_dai *dai, 748 void *sdw_stream, int direction) 749 { 750 snd_soc_dai_dma_data_set(dai, direction, sdw_stream); 751 752 return 0; 753 } 754 755 static const struct snd_soc_dai_ops cs35l56_sdw_dai_ops = { 756 .set_tdm_slot = cs35l56_sdw_dai_set_tdm_slot, 757 .shutdown = cs35l56_sdw_dai_shutdown, 758 .hw_params = cs35l56_sdw_dai_hw_params, 759 .hw_free = cs35l56_sdw_dai_hw_free, 760 .set_stream = cs35l56_sdw_dai_set_stream, 761 }; 762 763 static struct snd_soc_dai_driver cs35l56_dai[] = { 764 { 765 .name = "cs35l56-asp1", 766 .id = 0, 767 .playback = { 768 .stream_name = "ASP1 Playback", 769 .channels_min = 1, 770 .channels_max = 2, 771 .rates = CS35L56_RATES, 772 .formats = CS35L56_RX_FORMATS, 773 }, 774 .capture = { 775 .stream_name = "ASP1 Capture", 776 .channels_min = 1, 777 .channels_max = 4, 778 .rates = CS35L56_RATES, 779 .formats = CS35L56_TX_FORMATS, 780 }, 781 .ops = &cs35l56_ops, 782 .symmetric_rate = 1, 783 .symmetric_sample_bits = 1, 784 }, 785 { 786 .name = "cs35l56-sdw1", 787 .id = 1, 788 .playback = { 789 .stream_name = "SDW1 Playback", 790 .channels_min = 1, 791 .channels_max = 2, 792 .rates = CS35L56_RATES, 793 .formats = CS35L56_RX_FORMATS, 794 }, 795 .capture = { 796 .stream_name = "SDW1 Capture", 797 .channels_min = 1, 798 .channels_max = 4, 799 .rates = CS35L56_RATES, 800 .formats = CS35L56_TX_FORMATS, 801 }, 802 .symmetric_rate = 1, 803 .ops = &cs35l56_sdw_dai_ops, 804 } 805 }; 806 807 static int cs35l56_write_cal(struct cs35l56_private *cs35l56) 808 { 809 int ret; 810 811 if (cs35l56->base.secured || !cs35l56->base.cal_data_valid) 812 return -ENODATA; 813 814 ret = wm_adsp_run(&cs35l56->dsp); 815 if (ret) 816 return ret; 817 818 ret = cs_amp_write_cal_coeffs(&cs35l56->dsp.cs_dsp, 819 &cs35l56_calibration_controls, 820 &cs35l56->base.cal_data); 821 822 wm_adsp_stop(&cs35l56->dsp); 823 824 if (ret == 0) 825 dev_info(cs35l56->base.dev, "Calibration applied\n"); 826 827 return ret; 828 } 829 830 static void cs35l56_reinit_patch(struct cs35l56_private *cs35l56) 831 { 832 int ret; 833 834 /* Use wm_adsp to load and apply the firmware patch and coefficient files */ 835 ret = wm_adsp_power_up(&cs35l56->dsp, true); 836 if (ret) { 837 dev_dbg(cs35l56->base.dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret); 838 return; 839 } 840 841 cs35l56_write_cal(cs35l56); 842 843 /* Always REINIT after applying patch or coefficients */ 844 cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT); 845 } 846 847 static void cs35l56_patch(struct cs35l56_private *cs35l56, bool firmware_missing) 848 { 849 int ret; 850 851 /* 852 * Disable SoundWire interrupts to prevent race with IRQ work. 853 * Setting sdw_irq_no_unmask prevents the handler re-enabling 854 * the SoundWire interrupt. 855 */ 856 if (cs35l56->sdw_peripheral) { 857 cs35l56->sdw_irq_no_unmask = true; 858 flush_work(&cs35l56->sdw_irq_work); 859 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1, 0); 860 sdw_read_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1); 861 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1, 0xFF); 862 flush_work(&cs35l56->sdw_irq_work); 863 } 864 865 ret = cs35l56_firmware_shutdown(&cs35l56->base); 866 if (ret) 867 goto err; 868 869 /* 870 * Use wm_adsp to load and apply the firmware patch and coefficient files, 871 * but only if firmware is missing. If firmware is already patched just 872 * power-up wm_adsp without downloading firmware. 873 */ 874 ret = wm_adsp_power_up(&cs35l56->dsp, !!firmware_missing); 875 if (ret) { 876 dev_dbg(cs35l56->base.dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret); 877 goto err; 878 } 879 880 mutex_lock(&cs35l56->base.irq_lock); 881 882 reinit_completion(&cs35l56->init_completion); 883 884 cs35l56->soft_resetting = true; 885 cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral); 886 887 if (cs35l56->sdw_peripheral) { 888 /* 889 * The system-reset causes the CS35L56 to detach from the bus. 890 * Wait for the manager to re-enumerate the CS35L56 and 891 * cs35l56_init() to run again. 892 */ 893 if (!wait_for_completion_timeout(&cs35l56->init_completion, 894 msecs_to_jiffies(5000))) { 895 dev_err(cs35l56->base.dev, "%s: init_completion timed out (SDW)\n", 896 __func__); 897 goto err_unlock; 898 } 899 } else if (cs35l56_init(cs35l56)) { 900 goto err_unlock; 901 } 902 903 regmap_clear_bits(cs35l56->base.regmap, CS35L56_PROTECTION_STATUS, 904 CS35L56_FIRMWARE_MISSING); 905 cs35l56->base.fw_patched = true; 906 907 if (cs35l56_write_cal(cs35l56) == 0) 908 cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT); 909 910 err_unlock: 911 mutex_unlock(&cs35l56->base.irq_lock); 912 err: 913 /* Re-enable SoundWire interrupts */ 914 if (cs35l56->sdw_peripheral) { 915 cs35l56->sdw_irq_no_unmask = false; 916 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1, 917 CS35L56_SDW_INT_MASK_CODEC_IRQ); 918 } 919 } 920 921 static void cs35l56_dsp_work(struct work_struct *work) 922 { 923 struct cs35l56_private *cs35l56 = container_of(work, 924 struct cs35l56_private, 925 dsp_work); 926 unsigned int firmware_version; 927 bool firmware_missing; 928 int ret; 929 930 if (!cs35l56->base.init_done) 931 return; 932 933 pm_runtime_get_sync(cs35l56->base.dev); 934 935 ret = cs35l56_read_prot_status(&cs35l56->base, &firmware_missing, &firmware_version); 936 if (ret) 937 goto err; 938 939 /* Populate fw file qualifier with the revision and security state */ 940 kfree(cs35l56->dsp.fwf_name); 941 if (firmware_missing) { 942 cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL, "%02x-dsp1", cs35l56->base.rev); 943 } else { 944 /* Firmware files must match the running firmware version */ 945 cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL, 946 "%02x%s-%06x-dsp1", 947 cs35l56->base.rev, 948 cs35l56->base.secured ? "-s" : "", 949 firmware_version); 950 } 951 952 if (!cs35l56->dsp.fwf_name) 953 goto err; 954 955 dev_dbg(cs35l56->base.dev, "DSP fwf name: '%s' system name: '%s'\n", 956 cs35l56->dsp.fwf_name, cs35l56->dsp.system_name); 957 958 /* 959 * The firmware cannot be patched if it is already running from 960 * patch RAM. In this case the firmware files are versioned to 961 * match the running firmware version and will only contain 962 * tunings. We do not need to shutdown the firmware to apply 963 * tunings so can use the lower cost reinit sequence instead. 964 */ 965 if (!firmware_missing) 966 cs35l56_reinit_patch(cs35l56); 967 else 968 cs35l56_patch(cs35l56, firmware_missing); 969 970 err: 971 pm_runtime_mark_last_busy(cs35l56->base.dev); 972 pm_runtime_put_autosuspend(cs35l56->base.dev); 973 } 974 975 static int cs35l56_component_probe(struct snd_soc_component *component) 976 { 977 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component); 978 struct dentry *debugfs_root = component->debugfs_root; 979 unsigned short vendor, device; 980 981 BUILD_BUG_ON(ARRAY_SIZE(cs35l56_tx_input_texts) != ARRAY_SIZE(cs35l56_tx_input_values)); 982 983 if (!cs35l56->dsp.system_name && 984 (snd_soc_card_get_pci_ssid(component->card, &vendor, &device) == 0)) { 985 /* Append a speaker qualifier if there is a speaker ID */ 986 if (cs35l56->speaker_id >= 0) { 987 cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev, 988 GFP_KERNEL, 989 "%04x%04x-spkid%d", 990 vendor, device, 991 cs35l56->speaker_id); 992 } else { 993 cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev, 994 GFP_KERNEL, 995 "%04x%04x", 996 vendor, device); 997 } 998 if (!cs35l56->dsp.system_name) 999 return -ENOMEM; 1000 } 1001 1002 if (!wait_for_completion_timeout(&cs35l56->init_completion, 1003 msecs_to_jiffies(5000))) { 1004 dev_err(cs35l56->base.dev, "%s: init_completion timed out\n", __func__); 1005 return -ENODEV; 1006 } 1007 1008 cs35l56->dsp.part = kasprintf(GFP_KERNEL, "cs35l%02x", cs35l56->base.type); 1009 if (!cs35l56->dsp.part) 1010 return -ENOMEM; 1011 1012 cs35l56->component = component; 1013 wm_adsp2_component_probe(&cs35l56->dsp, component); 1014 1015 debugfs_create_bool("init_done", 0444, debugfs_root, &cs35l56->base.init_done); 1016 debugfs_create_bool("can_hibernate", 0444, debugfs_root, &cs35l56->base.can_hibernate); 1017 debugfs_create_bool("fw_patched", 0444, debugfs_root, &cs35l56->base.fw_patched); 1018 1019 /* 1020 * The widgets for the ASP1TX mixer can't be initialized 1021 * until the firmware has been downloaded and rebooted. 1022 */ 1023 regcache_drop_region(cs35l56->base.regmap, CS35L56_ASP1TX1_INPUT, CS35L56_ASP1TX4_INPUT); 1024 cs35l56->asp1_mixer_widgets_initialized = false; 1025 1026 queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work); 1027 1028 return 0; 1029 } 1030 1031 static void cs35l56_component_remove(struct snd_soc_component *component) 1032 { 1033 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component); 1034 1035 cancel_work_sync(&cs35l56->dsp_work); 1036 1037 if (cs35l56->dsp.cs_dsp.booted) 1038 wm_adsp_power_down(&cs35l56->dsp); 1039 1040 wm_adsp2_component_remove(&cs35l56->dsp, component); 1041 1042 kfree(cs35l56->dsp.part); 1043 cs35l56->dsp.part = NULL; 1044 1045 kfree(cs35l56->dsp.fwf_name); 1046 cs35l56->dsp.fwf_name = NULL; 1047 1048 cs35l56->component = NULL; 1049 } 1050 1051 static int cs35l56_set_bias_level(struct snd_soc_component *component, 1052 enum snd_soc_bias_level level) 1053 { 1054 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component); 1055 1056 switch (level) { 1057 case SND_SOC_BIAS_STANDBY: 1058 /* 1059 * Wait for patching to complete when transitioning from 1060 * BIAS_OFF to BIAS_STANDBY 1061 */ 1062 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) 1063 cs35l56_wait_dsp_ready(cs35l56); 1064 1065 break; 1066 default: 1067 break; 1068 } 1069 1070 return 0; 1071 } 1072 1073 static const struct snd_soc_component_driver soc_component_dev_cs35l56 = { 1074 .probe = cs35l56_component_probe, 1075 .remove = cs35l56_component_remove, 1076 1077 .dapm_widgets = cs35l56_dapm_widgets, 1078 .num_dapm_widgets = ARRAY_SIZE(cs35l56_dapm_widgets), 1079 .dapm_routes = cs35l56_audio_map, 1080 .num_dapm_routes = ARRAY_SIZE(cs35l56_audio_map), 1081 .controls = cs35l56_controls, 1082 .num_controls = ARRAY_SIZE(cs35l56_controls), 1083 1084 .set_bias_level = cs35l56_set_bias_level, 1085 1086 .suspend_bias_off = 1, /* see cs35l56_system_resume() */ 1087 }; 1088 1089 static int __maybe_unused cs35l56_runtime_suspend_i2c_spi(struct device *dev) 1090 { 1091 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev); 1092 1093 return cs35l56_runtime_suspend_common(&cs35l56->base); 1094 } 1095 1096 static int __maybe_unused cs35l56_runtime_resume_i2c_spi(struct device *dev) 1097 { 1098 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev); 1099 1100 return cs35l56_runtime_resume_common(&cs35l56->base, false); 1101 } 1102 1103 int cs35l56_system_suspend(struct device *dev) 1104 { 1105 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev); 1106 1107 dev_dbg(dev, "system_suspend\n"); 1108 1109 if (cs35l56->component) 1110 flush_work(&cs35l56->dsp_work); 1111 1112 /* 1113 * The interrupt line is normally shared, but after we start suspending 1114 * we can't check if our device is the source of an interrupt, and can't 1115 * clear it. Prevent this race by temporarily disabling the parent irq 1116 * until we reach _no_irq. 1117 */ 1118 if (cs35l56->base.irq) 1119 disable_irq(cs35l56->base.irq); 1120 1121 return pm_runtime_force_suspend(dev); 1122 } 1123 EXPORT_SYMBOL_GPL(cs35l56_system_suspend); 1124 1125 int cs35l56_system_suspend_late(struct device *dev) 1126 { 1127 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev); 1128 1129 dev_dbg(dev, "system_suspend_late\n"); 1130 1131 /* 1132 * Assert RESET before removing supplies. 1133 * RESET is usually shared by all amps so it must not be asserted until 1134 * all driver instances have done their suspend() stage. 1135 */ 1136 if (cs35l56->base.reset_gpio) { 1137 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1138 cs35l56_wait_min_reset_pulse(); 1139 } 1140 1141 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies); 1142 1143 return 0; 1144 } 1145 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_late); 1146 1147 int cs35l56_system_suspend_no_irq(struct device *dev) 1148 { 1149 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev); 1150 1151 dev_dbg(dev, "system_suspend_no_irq\n"); 1152 1153 /* Handlers are now disabled so the parent IRQ can safely be re-enabled. */ 1154 if (cs35l56->base.irq) 1155 enable_irq(cs35l56->base.irq); 1156 1157 return 0; 1158 } 1159 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_no_irq); 1160 1161 int cs35l56_system_resume_no_irq(struct device *dev) 1162 { 1163 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev); 1164 1165 dev_dbg(dev, "system_resume_no_irq\n"); 1166 1167 /* 1168 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause 1169 * spurious interrupts, and the interrupt line is normally shared. 1170 * We can't check if our device is the source of an interrupt, and can't 1171 * clear it, until it has fully resumed. Prevent this race by temporarily 1172 * disabling the parent irq until we complete resume(). 1173 */ 1174 if (cs35l56->base.irq) 1175 disable_irq(cs35l56->base.irq); 1176 1177 return 0; 1178 } 1179 EXPORT_SYMBOL_GPL(cs35l56_system_resume_no_irq); 1180 1181 int cs35l56_system_resume_early(struct device *dev) 1182 { 1183 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev); 1184 int ret; 1185 1186 dev_dbg(dev, "system_resume_early\n"); 1187 1188 /* Ensure a spec-compliant RESET pulse. */ 1189 if (cs35l56->base.reset_gpio) { 1190 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1191 cs35l56_wait_min_reset_pulse(); 1192 } 1193 1194 /* Enable supplies before releasing RESET. */ 1195 ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies); 1196 if (ret) { 1197 dev_err(dev, "system_resume_early failed to enable supplies: %d\n", ret); 1198 return ret; 1199 } 1200 1201 /* Release shared RESET before drivers start resume(). */ 1202 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1); 1203 1204 return 0; 1205 } 1206 EXPORT_SYMBOL_GPL(cs35l56_system_resume_early); 1207 1208 int cs35l56_system_resume(struct device *dev) 1209 { 1210 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev); 1211 int ret; 1212 1213 dev_dbg(dev, "system_resume\n"); 1214 1215 /* 1216 * We might have done a hard reset or the CS35L56 was power-cycled 1217 * so wait for control port to be ready. 1218 */ 1219 cs35l56_wait_control_port_ready(); 1220 1221 /* Undo pm_runtime_force_suspend() before re-enabling the irq */ 1222 ret = pm_runtime_force_resume(dev); 1223 if (cs35l56->base.irq) 1224 enable_irq(cs35l56->base.irq); 1225 1226 if (ret) 1227 return ret; 1228 1229 /* Firmware won't have been loaded if the component hasn't probed */ 1230 if (!cs35l56->component) 1231 return 0; 1232 1233 ret = cs35l56_is_fw_reload_needed(&cs35l56->base); 1234 dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret); 1235 if (ret < 1) 1236 return ret; 1237 1238 cs35l56->base.fw_patched = false; 1239 wm_adsp_power_down(&cs35l56->dsp); 1240 queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work); 1241 1242 /* 1243 * suspend_bias_off ensures we are now in BIAS_OFF so there will be 1244 * a BIAS_OFF->BIAS_STANDBY transition to complete dsp patching. 1245 */ 1246 1247 return 0; 1248 } 1249 EXPORT_SYMBOL_GPL(cs35l56_system_resume); 1250 1251 static int cs35l56_dsp_init(struct cs35l56_private *cs35l56) 1252 { 1253 struct wm_adsp *dsp; 1254 int ret; 1255 1256 cs35l56->dsp_wq = create_singlethread_workqueue("cs35l56-dsp"); 1257 if (!cs35l56->dsp_wq) 1258 return -ENOMEM; 1259 1260 INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work); 1261 1262 dsp = &cs35l56->dsp; 1263 cs35l56_init_cs_dsp(&cs35l56->base, &dsp->cs_dsp); 1264 1265 /* 1266 * dsp->part is filled in later as it is based on the DEVID. In a 1267 * SoundWire system that cannot be read until enumeration has occurred 1268 * and the device has attached. 1269 */ 1270 dsp->fw = 12; 1271 dsp->wmfw_optional = true; 1272 1273 dev_dbg(cs35l56->base.dev, "DSP system name: '%s'\n", dsp->system_name); 1274 1275 ret = wm_halo_init(dsp); 1276 if (ret != 0) { 1277 dev_err(cs35l56->base.dev, "wm_halo_init failed\n"); 1278 return ret; 1279 } 1280 1281 return 0; 1282 } 1283 1284 static int cs35l56_get_firmware_uid(struct cs35l56_private *cs35l56) 1285 { 1286 struct device *dev = cs35l56->base.dev; 1287 const char *prop; 1288 int ret; 1289 1290 ret = device_property_read_string(dev, "cirrus,firmware-uid", &prop); 1291 /* If bad sw node property, return 0 and fallback to legacy firmware path */ 1292 if (ret < 0) 1293 return 0; 1294 1295 /* Append a speaker qualifier if there is a speaker ID */ 1296 if (cs35l56->speaker_id >= 0) 1297 cs35l56->dsp.system_name = devm_kasprintf(dev, GFP_KERNEL, "%s-spkid%d", 1298 prop, cs35l56->speaker_id); 1299 else 1300 cs35l56->dsp.system_name = devm_kstrdup(dev, prop, GFP_KERNEL); 1301 1302 if (cs35l56->dsp.system_name == NULL) 1303 return -ENOMEM; 1304 1305 dev_dbg(dev, "Firmware UID: %s\n", cs35l56->dsp.system_name); 1306 1307 return 0; 1308 } 1309 1310 /* 1311 * Some SoundWire laptops have a spk-id-gpios property but it points to 1312 * the wrong ACPI Device node so can't be used to get the GPIO. Try to 1313 * find the SDCA node containing the GpioIo resource and add a GPIO 1314 * mapping to it. 1315 */ 1316 static const struct acpi_gpio_params cs35l56_af01_first_gpio = { 0, 0, false }; 1317 static const struct acpi_gpio_mapping cs35l56_af01_spkid_gpios_mapping[] = { 1318 { "spk-id-gpios", &cs35l56_af01_first_gpio, 1 }, 1319 { } 1320 }; 1321 1322 static void cs35l56_acpi_dev_release_driver_gpios(void *adev) 1323 { 1324 acpi_dev_remove_driver_gpios(adev); 1325 } 1326 1327 static int cs35l56_try_get_broken_sdca_spkid_gpio(struct cs35l56_private *cs35l56) 1328 { 1329 struct fwnode_handle *af01_fwnode; 1330 const union acpi_object *obj; 1331 struct gpio_desc *desc; 1332 int ret; 1333 1334 /* Find the SDCA node containing the GpioIo */ 1335 af01_fwnode = device_get_named_child_node(cs35l56->base.dev, "AF01"); 1336 if (!af01_fwnode) { 1337 dev_dbg(cs35l56->base.dev, "No AF01 node\n"); 1338 return -ENOENT; 1339 } 1340 1341 ret = acpi_dev_get_property(ACPI_COMPANION(cs35l56->base.dev), 1342 "spk-id-gpios", ACPI_TYPE_PACKAGE, &obj); 1343 if (ret) { 1344 dev_dbg(cs35l56->base.dev, "Could not get spk-id-gpios package: %d\n", ret); 1345 return -ENOENT; 1346 } 1347 1348 /* The broken properties we can handle are a 4-element package (one GPIO) */ 1349 if (obj->package.count != 4) { 1350 dev_warn(cs35l56->base.dev, "Unexpected spk-id element count %d\n", 1351 obj->package.count); 1352 return -ENOENT; 1353 } 1354 1355 /* Add a GPIO mapping if it doesn't already have one */ 1356 if (!fwnode_property_present(af01_fwnode, "spk-id-gpios")) { 1357 struct acpi_device *adev = to_acpi_device_node(af01_fwnode); 1358 1359 /* 1360 * Can't use devm_acpi_dev_add_driver_gpios() because the 1361 * mapping isn't being added to the node pointed to by 1362 * ACPI_COMPANION(). 1363 */ 1364 ret = acpi_dev_add_driver_gpios(adev, cs35l56_af01_spkid_gpios_mapping); 1365 if (ret) { 1366 return dev_err_probe(cs35l56->base.dev, ret, 1367 "Failed to add gpio mapping to AF01\n"); 1368 } 1369 1370 ret = devm_add_action_or_reset(cs35l56->base.dev, 1371 cs35l56_acpi_dev_release_driver_gpios, 1372 adev); 1373 if (ret) 1374 return ret; 1375 1376 dev_dbg(cs35l56->base.dev, "Added spk-id-gpios mapping to AF01\n"); 1377 } 1378 1379 desc = fwnode_gpiod_get_index(af01_fwnode, "spk-id", 0, GPIOD_IN, NULL); 1380 if (IS_ERR(desc)) { 1381 ret = PTR_ERR(desc); 1382 return dev_err_probe(cs35l56->base.dev, ret, "Get GPIO from AF01 failed\n"); 1383 } 1384 1385 ret = gpiod_get_value_cansleep(desc); 1386 gpiod_put(desc); 1387 1388 if (ret < 0) { 1389 dev_err_probe(cs35l56->base.dev, ret, "Error reading spk-id GPIO\n"); 1390 return ret; 1391 } 1392 1393 dev_info(cs35l56->base.dev, "Got spk-id from AF01\n"); 1394 1395 return ret; 1396 } 1397 1398 int cs35l56_common_probe(struct cs35l56_private *cs35l56) 1399 { 1400 int ret; 1401 1402 init_completion(&cs35l56->init_completion); 1403 mutex_init(&cs35l56->base.irq_lock); 1404 cs35l56->base.cal_index = -1; 1405 cs35l56->speaker_id = -ENOENT; 1406 1407 dev_set_drvdata(cs35l56->base.dev, cs35l56); 1408 1409 cs35l56_fill_supply_names(cs35l56->supplies); 1410 ret = devm_regulator_bulk_get(cs35l56->base.dev, ARRAY_SIZE(cs35l56->supplies), 1411 cs35l56->supplies); 1412 if (ret != 0) 1413 return dev_err_probe(cs35l56->base.dev, ret, "Failed to request supplies\n"); 1414 1415 /* Reset could be controlled by the BIOS or shared by multiple amps */ 1416 cs35l56->base.reset_gpio = devm_gpiod_get_optional(cs35l56->base.dev, "reset", 1417 GPIOD_OUT_LOW); 1418 if (IS_ERR(cs35l56->base.reset_gpio)) { 1419 ret = PTR_ERR(cs35l56->base.reset_gpio); 1420 /* 1421 * If RESET is shared the first amp to probe will grab the reset 1422 * line and reset all the amps 1423 */ 1424 if (ret != -EBUSY) 1425 return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n"); 1426 1427 dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n"); 1428 cs35l56->base.reset_gpio = NULL; 1429 } 1430 1431 ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies); 1432 if (ret != 0) 1433 return dev_err_probe(cs35l56->base.dev, ret, "Failed to enable supplies\n"); 1434 1435 if (cs35l56->base.reset_gpio) { 1436 /* ACPI can override GPIOD_OUT_LOW flag so force it to start low */ 1437 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1438 cs35l56_wait_min_reset_pulse(); 1439 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1); 1440 } 1441 1442 ret = cs35l56_get_speaker_id(&cs35l56->base); 1443 if (ACPI_COMPANION(cs35l56->base.dev) && cs35l56->sdw_peripheral && (ret == -ENOENT)) 1444 ret = cs35l56_try_get_broken_sdca_spkid_gpio(cs35l56); 1445 1446 if ((ret < 0) && (ret != -ENOENT)) 1447 goto err; 1448 1449 cs35l56->speaker_id = ret; 1450 1451 ret = cs35l56_get_firmware_uid(cs35l56); 1452 if (ret != 0) 1453 goto err; 1454 1455 ret = cs35l56_dsp_init(cs35l56); 1456 if (ret < 0) { 1457 dev_err_probe(cs35l56->base.dev, ret, "DSP init failed\n"); 1458 goto err; 1459 } 1460 1461 ret = devm_snd_soc_register_component(cs35l56->base.dev, 1462 &soc_component_dev_cs35l56, 1463 cs35l56_dai, ARRAY_SIZE(cs35l56_dai)); 1464 if (ret < 0) { 1465 dev_err_probe(cs35l56->base.dev, ret, "Register codec failed\n"); 1466 goto err; 1467 } 1468 1469 return 0; 1470 1471 err: 1472 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1473 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies); 1474 1475 return ret; 1476 } 1477 EXPORT_SYMBOL_NS_GPL(cs35l56_common_probe, SND_SOC_CS35L56_CORE); 1478 1479 int cs35l56_init(struct cs35l56_private *cs35l56) 1480 { 1481 int ret; 1482 1483 /* 1484 * Check whether the actions associated with soft reset or one time 1485 * init need to be performed. 1486 */ 1487 if (cs35l56->soft_resetting) 1488 goto post_soft_reset; 1489 1490 if (cs35l56->base.init_done) 1491 return 0; 1492 1493 pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 100); 1494 pm_runtime_use_autosuspend(cs35l56->base.dev); 1495 pm_runtime_set_active(cs35l56->base.dev); 1496 pm_runtime_enable(cs35l56->base.dev); 1497 1498 ret = cs35l56_hw_init(&cs35l56->base); 1499 if (ret < 0) 1500 return ret; 1501 1502 ret = cs35l56_set_patch(&cs35l56->base); 1503 if (ret) 1504 return ret; 1505 1506 ret = cs35l56_get_calibration(&cs35l56->base); 1507 if (ret) 1508 return ret; 1509 1510 if (!cs35l56->base.reset_gpio) { 1511 dev_dbg(cs35l56->base.dev, "No reset gpio: using soft reset\n"); 1512 cs35l56->soft_resetting = true; 1513 cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral); 1514 if (cs35l56->sdw_peripheral) { 1515 /* Keep alive while we wait for re-enumeration */ 1516 pm_runtime_get_noresume(cs35l56->base.dev); 1517 return 0; 1518 } 1519 } 1520 1521 post_soft_reset: 1522 if (cs35l56->soft_resetting) { 1523 cs35l56->soft_resetting = false; 1524 1525 /* Done re-enumerating after one-time init so release the keep-alive */ 1526 if (cs35l56->sdw_peripheral && !cs35l56->base.init_done) 1527 pm_runtime_put_noidle(cs35l56->base.dev); 1528 1529 regcache_mark_dirty(cs35l56->base.regmap); 1530 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base); 1531 if (ret) 1532 return ret; 1533 1534 dev_dbg(cs35l56->base.dev, "Firmware rebooted after soft reset\n"); 1535 } 1536 1537 /* Disable auto-hibernate so that runtime_pm has control */ 1538 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE); 1539 if (ret) 1540 return ret; 1541 1542 /* Registers could be dirty after soft reset or SoundWire enumeration */ 1543 regcache_sync(cs35l56->base.regmap); 1544 1545 /* Set ASP1 DOUT to high-impedance when it is not transmitting audio data. */ 1546 ret = regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3, 1547 CS35L56_ASP1_DOUT_HIZ_CTRL_MASK); 1548 if (ret) 1549 return dev_err_probe(cs35l56->base.dev, ret, "Failed to write ASP1_CONTROL3\n"); 1550 1551 cs35l56->base.init_done = true; 1552 complete(&cs35l56->init_completion); 1553 1554 return 0; 1555 } 1556 EXPORT_SYMBOL_NS_GPL(cs35l56_init, SND_SOC_CS35L56_CORE); 1557 1558 void cs35l56_remove(struct cs35l56_private *cs35l56) 1559 { 1560 cs35l56->base.init_done = false; 1561 1562 /* 1563 * WAKE IRQs unmask if CS35L56 hibernates so free the handler to 1564 * prevent it racing with remove(). 1565 */ 1566 if (cs35l56->base.irq) 1567 devm_free_irq(cs35l56->base.dev, cs35l56->base.irq, &cs35l56->base); 1568 1569 flush_workqueue(cs35l56->dsp_wq); 1570 destroy_workqueue(cs35l56->dsp_wq); 1571 1572 pm_runtime_dont_use_autosuspend(cs35l56->base.dev); 1573 pm_runtime_suspend(cs35l56->base.dev); 1574 pm_runtime_disable(cs35l56->base.dev); 1575 1576 regcache_cache_only(cs35l56->base.regmap, true); 1577 1578 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1579 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies); 1580 } 1581 EXPORT_SYMBOL_NS_GPL(cs35l56_remove, SND_SOC_CS35L56_CORE); 1582 1583 #if IS_ENABLED(CONFIG_SND_SOC_CS35L56_I2C) || IS_ENABLED(CONFIG_SND_SOC_CS35L56_SPI) 1584 EXPORT_NS_GPL_DEV_PM_OPS(cs35l56_pm_ops_i2c_spi, SND_SOC_CS35L56_CORE) = { 1585 SET_RUNTIME_PM_OPS(cs35l56_runtime_suspend_i2c_spi, cs35l56_runtime_resume_i2c_spi, NULL) 1586 SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend, cs35l56_system_resume) 1587 LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_late, cs35l56_system_resume_early) 1588 NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_no_irq, cs35l56_system_resume_no_irq) 1589 }; 1590 #endif 1591 1592 MODULE_DESCRIPTION("ASoC CS35L56 driver"); 1593 MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED); 1594 MODULE_IMPORT_NS(SND_SOC_CS_AMP_LIB); 1595 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>"); 1596 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>"); 1597 MODULE_LICENSE("GPL"); 1598