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