1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2025 Cirrus Logic, Inc. and 3 // Cirrus Logic International Semiconductor Ltd. 4 5 /* 6 * The MIPI SDCA specification is available for public downloads at 7 * https://www.mipi.org/mipi-sdca-v1-0-download 8 */ 9 10 #include <linux/auxiliary_bus.h> 11 #include <linux/cleanup.h> 12 #include <linux/minmax.h> 13 #include <linux/module.h> 14 #include <linux/pm.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/soundwire/sdw.h> 17 #include <linux/soundwire/sdw_registers.h> 18 #include <sound/pcm.h> 19 #include <sound/sdca_asoc.h> 20 #include <sound/sdca_fdl.h> 21 #include <sound/sdca_function.h> 22 #include <sound/sdca_interrupts.h> 23 #include <sound/sdca_jack.h> 24 #include <sound/sdca_regmap.h> 25 #include <sound/sdw.h> 26 #include <sound/soc-component.h> 27 #include <sound/soc-dai.h> 28 #include <sound/soc.h> 29 #include "sdca_class.h" 30 31 struct class_function_drv { 32 struct device *dev; 33 struct regmap *regmap; 34 struct sdca_class_drv *core; 35 36 struct sdca_function_data *function; 37 bool suspended; 38 }; 39 40 static void class_function_regmap_lock(void *data) 41 { 42 struct mutex *lock = data; 43 44 mutex_lock(lock); 45 } 46 47 static void class_function_regmap_unlock(void *data) 48 { 49 struct mutex *lock = data; 50 51 mutex_unlock(lock); 52 } 53 54 static bool class_function_regmap_writeable(struct device *dev, unsigned int reg) 55 { 56 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 57 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 58 59 return sdca_regmap_writeable(drv->function, reg); 60 } 61 62 static bool class_function_regmap_readable(struct device *dev, unsigned int reg) 63 { 64 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 65 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 66 67 return sdca_regmap_readable(drv->function, reg); 68 } 69 70 static bool class_function_regmap_volatile(struct device *dev, unsigned int reg) 71 { 72 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 73 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 74 75 return sdca_regmap_volatile(drv->function, reg); 76 } 77 78 static const struct regmap_config class_function_regmap_config = { 79 .name = "sdca", 80 .reg_bits = 32, 81 .val_bits = 32, 82 .reg_format_endian = REGMAP_ENDIAN_LITTLE, 83 .val_format_endian = REGMAP_ENDIAN_LITTLE, 84 85 .max_register = SDW_SDCA_MAX_REGISTER, 86 .readable_reg = class_function_regmap_readable, 87 .writeable_reg = class_function_regmap_writeable, 88 .volatile_reg = class_function_regmap_volatile, 89 90 .cache_type = REGCACHE_MAPLE, 91 92 .lock = class_function_regmap_lock, 93 .unlock = class_function_regmap_unlock, 94 }; 95 96 static int class_function_regmap_mbq_size(struct device *dev, unsigned int reg) 97 { 98 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 99 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 100 101 return sdca_regmap_mbq_size(drv->function, reg); 102 } 103 104 static bool class_function_regmap_deferrable(struct device *dev, unsigned int reg) 105 { 106 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 107 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 108 109 return sdca_regmap_deferrable(drv->function, reg); 110 } 111 112 static const struct regmap_sdw_mbq_cfg class_function_mbq_config = { 113 .mbq_size = class_function_regmap_mbq_size, 114 .deferrable = class_function_regmap_deferrable, 115 .retry_us = 1000, 116 .timeout_us = 10000, 117 }; 118 119 static int class_function_startup(struct snd_pcm_substream *substream, 120 struct snd_soc_dai *dai) 121 { 122 struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component); 123 124 return sdca_asoc_set_constraints(drv->dev, drv->regmap, drv->function, 125 substream, dai); 126 } 127 128 static int class_function_sdw_add_peripheral(struct snd_pcm_substream *substream, 129 struct snd_pcm_hw_params *params, 130 struct snd_soc_dai *dai) 131 { 132 struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component); 133 struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream); 134 struct sdw_slave *sdw = dev_to_sdw_dev(drv->dev->parent); 135 struct sdw_stream_config sconfig = {0}; 136 struct sdw_port_config pconfig = {0}; 137 int ret; 138 139 if (!sdw_stream) 140 return -EINVAL; 141 142 snd_sdw_params_to_config(substream, params, &sconfig, &pconfig); 143 144 /* 145 * FIXME: As also noted in sdca_asoc_get_port(), currently only 146 * a single unshared port is supported for each DAI. 147 */ 148 ret = sdca_asoc_get_port(drv->dev, drv->regmap, drv->function, dai); 149 if (ret < 0) 150 return ret; 151 152 pconfig.num = ret; 153 154 ret = sdw_stream_add_slave(sdw, &sconfig, &pconfig, 1, sdw_stream); 155 if (ret) { 156 dev_err(drv->dev, "failed to add sdw stream: %d\n", ret); 157 return ret; 158 } 159 160 return sdca_asoc_hw_params(drv->dev, drv->regmap, drv->function, 161 substream, params, dai); 162 } 163 164 static int class_function_sdw_remove_peripheral(struct snd_pcm_substream *substream, 165 struct snd_soc_dai *dai) 166 { 167 struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component); 168 struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream); 169 struct sdw_slave *sdw = dev_to_sdw_dev(drv->dev->parent); 170 171 if (!sdw_stream) 172 return -EINVAL; 173 174 return sdw_stream_remove_slave(sdw, sdw_stream); 175 } 176 177 static int class_function_sdw_set_stream(struct snd_soc_dai *dai, void *sdw_stream, 178 int direction) 179 { 180 snd_soc_dai_dma_data_set(dai, direction, sdw_stream); 181 182 return 0; 183 } 184 185 static const struct snd_soc_dai_ops class_function_sdw_ops = { 186 .startup = class_function_startup, 187 .shutdown = sdca_asoc_free_constraints, 188 .set_stream = class_function_sdw_set_stream, 189 .hw_params = class_function_sdw_add_peripheral, 190 .hw_free = class_function_sdw_remove_peripheral, 191 }; 192 193 static int class_function_component_probe(struct snd_soc_component *component) 194 { 195 struct class_function_drv *drv = snd_soc_component_get_drvdata(component); 196 struct sdca_class_drv *core = drv->core; 197 198 return sdca_irq_populate(drv->function, component, core->irq_info); 199 } 200 201 static void class_function_component_remove(struct snd_soc_component *component) 202 { 203 struct class_function_drv *drv = snd_soc_component_get_drvdata(component); 204 struct sdca_class_drv *core = drv->core; 205 206 sdca_irq_cleanup(component->dev, drv->function, core->irq_info); 207 } 208 209 static int class_function_set_jack(struct snd_soc_component *component, 210 struct snd_soc_jack *jack, void *d) 211 { 212 struct class_function_drv *drv = snd_soc_component_get_drvdata(component); 213 struct sdca_class_drv *core = drv->core; 214 215 return sdca_jack_set_jack(core->irq_info, jack); 216 } 217 218 static const struct snd_soc_component_driver class_function_component_drv = { 219 .probe = class_function_component_probe, 220 .remove = class_function_component_remove, 221 .endianness = 1, 222 }; 223 224 static int class_function_init_device(struct class_function_drv *drv, 225 unsigned int status) 226 { 227 int ret; 228 229 if (!(status & SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET)) { 230 dev_dbg(drv->dev, "reset function device\n"); 231 232 ret = sdca_reset_function(drv->dev, drv->function, drv->regmap); 233 if (ret) 234 return ret; 235 } 236 237 if (status & SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION) { 238 dev_dbg(drv->dev, "write initialisation\n"); 239 240 ret = sdca_regmap_write_init(drv->dev, drv->core->dev_regmap, 241 drv->function); 242 if (ret) 243 return ret; 244 } 245 246 return 0; 247 } 248 249 static int class_function_boot(struct class_function_drv *drv) 250 { 251 unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr, 252 SDCA_ENTITY_TYPE_ENTITY_0, 253 SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0); 254 unsigned int val; 255 int ret; 256 257 guard(mutex)(&drv->core->init_lock); 258 259 ret = regmap_read(drv->regmap, reg, &val); 260 if (ret < 0) { 261 dev_err(drv->dev, "failed to read function status: %d\n", ret); 262 return ret; 263 } 264 265 ret = class_function_init_device(drv, val); 266 if (ret) 267 return ret; 268 269 /* Start FDL process */ 270 ret = sdca_irq_populate_early(drv->dev, drv->regmap, drv->function, 271 drv->core->irq_info); 272 if (ret) 273 return ret; 274 275 ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info); 276 if (ret) 277 return ret; 278 279 ret = sdca_regmap_write_defaults(drv->dev, drv->regmap, drv->function); 280 if (ret) 281 return ret; 282 283 ret = regmap_write(drv->regmap, reg, 0xFF); 284 if (ret < 0) { 285 dev_err(drv->dev, "failed to clear function status: %d\n", ret); 286 return ret; 287 } 288 289 return 0; 290 } 291 292 static int class_function_probe(struct auxiliary_device *auxdev, 293 const struct auxiliary_device_id *aux_dev_id) 294 { 295 struct device *dev = &auxdev->dev; 296 struct sdca_class_drv *core = dev_get_drvdata(dev->parent); 297 struct sdca_device_data *data = &core->sdw->sdca_data; 298 struct sdca_function_desc *desc; 299 struct snd_soc_component_driver *cmp_drv; 300 struct snd_soc_dai_driver *dais; 301 struct class_function_drv *drv; 302 struct regmap_sdw_mbq_cfg *mbq_config; 303 struct regmap_config *config; 304 struct reg_default *defaults; 305 int ndefaults; 306 int num_dais; 307 int ret; 308 int i; 309 310 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL); 311 if (!drv) 312 return -ENOMEM; 313 314 cmp_drv = devm_kmemdup(dev, &class_function_component_drv, sizeof(*cmp_drv), 315 GFP_KERNEL); 316 if (!cmp_drv) 317 return -ENOMEM; 318 319 config = devm_kmemdup(dev, &class_function_regmap_config, sizeof(*config), 320 GFP_KERNEL); 321 if (!config) 322 return -ENOMEM; 323 324 mbq_config = devm_kmemdup(dev, &class_function_mbq_config, sizeof(*mbq_config), 325 GFP_KERNEL); 326 if (!mbq_config) 327 return -ENOMEM; 328 329 drv->dev = dev; 330 drv->core = core; 331 332 for (i = 0; i < data->num_functions; i++) { 333 desc = &data->function[i]; 334 335 if (desc->type == aux_dev_id->driver_data) 336 break; 337 } 338 if (i == core->sdw->sdca_data.num_functions) { 339 dev_err(dev, "failed to locate function\n"); 340 return -EINVAL; 341 } 342 343 drv->function = &core->functions[i]; 344 345 ret = sdca_parse_function(dev, core->sdw, desc, drv->function); 346 if (ret) 347 return ret; 348 349 ndefaults = sdca_regmap_count_constants(dev, drv->function); 350 if (ndefaults < 0) 351 return ndefaults; 352 353 defaults = devm_kcalloc(dev, ndefaults, sizeof(*defaults), GFP_KERNEL); 354 if (!defaults) 355 return -ENOMEM; 356 357 ret = sdca_regmap_populate_constants(dev, drv->function, defaults); 358 if (ret < 0) 359 return ret; 360 361 regcache_sort_defaults(defaults, ndefaults); 362 363 auxiliary_set_drvdata(auxdev, drv); 364 365 config->reg_defaults = defaults; 366 config->num_reg_defaults = ndefaults; 367 config->lock_arg = &core->regmap_lock; 368 369 if (drv->function->busy_max_delay) { 370 mbq_config->timeout_us = drv->function->busy_max_delay; 371 mbq_config->retry_us = umax(drv->function->busy_max_delay / 10, 372 mbq_config->retry_us); 373 } 374 375 drv->regmap = devm_regmap_init_sdw_mbq_cfg(dev, core->sdw, config, mbq_config); 376 if (IS_ERR(drv->regmap)) 377 return dev_err_probe(dev, PTR_ERR(drv->regmap), 378 "failed to create regmap"); 379 380 switch (desc->type) { 381 case SDCA_FUNCTION_TYPE_UAJ: 382 case SDCA_FUNCTION_TYPE_RJ: 383 cmp_drv->set_jack = class_function_set_jack; 384 break; 385 default: 386 break; 387 } 388 389 ret = sdca_asoc_populate_component(dev, drv->function, cmp_drv, 390 &dais, &num_dais, 391 &class_function_sdw_ops); 392 if (ret) 393 return ret; 394 395 dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE); 396 397 pm_runtime_set_autosuspend_delay(dev, 200); 398 pm_runtime_use_autosuspend(dev); 399 pm_runtime_set_active(dev); 400 pm_runtime_get_noresume(dev); 401 402 ret = devm_pm_runtime_enable(dev); 403 if (ret) 404 return ret; 405 406 ret = class_function_boot(drv); 407 if (ret) 408 return ret; 409 410 ret = devm_snd_soc_register_component(dev, cmp_drv, dais, num_dais); 411 if (ret) 412 return dev_err_probe(dev, ret, "failed to register component\n"); 413 414 pm_runtime_mark_last_busy(dev); 415 pm_runtime_put_autosuspend(dev); 416 417 return 0; 418 } 419 420 static void class_function_remove(struct auxiliary_device *auxdev) 421 { 422 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 423 424 sdca_irq_cleanup(drv->dev, drv->function, drv->core->irq_info); 425 } 426 427 static int class_function_runtime_suspend(struct device *dev) 428 { 429 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 430 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 431 432 /* 433 * Whilst the driver doesn't power the chip down here, going into 434 * runtime suspend means the driver can't be sure the bus won't 435 * power down which would prevent communication with the device. 436 */ 437 regcache_cache_only(drv->regmap, true); 438 439 return 0; 440 } 441 442 static int class_function_runtime_resume(struct device *dev) 443 { 444 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 445 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 446 int ret; 447 448 guard(mutex)(&drv->core->init_lock); 449 450 regcache_mark_dirty(drv->regmap); 451 regcache_cache_only(drv->regmap, false); 452 453 if (drv->suspended) { 454 unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr, 455 SDCA_ENTITY_TYPE_ENTITY_0, 456 SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0); 457 unsigned int val; 458 459 ret = regmap_read(drv->regmap, reg, &val); 460 if (ret < 0) { 461 dev_err(drv->dev, "failed to read function status: %d\n", ret); 462 goto err; 463 } 464 465 ret = class_function_init_device(drv, val); 466 if (ret) 467 goto err; 468 469 sdca_irq_enable_early(drv->function, drv->core->irq_info); 470 471 ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info); 472 if (ret) 473 goto err; 474 475 sdca_irq_enable(drv->function, drv->core->irq_info); 476 477 ret = regmap_write(drv->regmap, reg, 0xFF); 478 if (ret < 0) { 479 dev_err(drv->dev, "failed to clear function status: %d\n", ret); 480 goto err; 481 } 482 483 drv->suspended = false; 484 } 485 486 ret = regcache_sync(drv->regmap); 487 if (ret) { 488 dev_err(drv->dev, "failed to restore register cache: %d\n", ret); 489 goto err; 490 } 491 492 return 0; 493 494 err: 495 regcache_cache_only(drv->regmap, true); 496 497 return ret; 498 } 499 500 static int class_function_suspend(struct device *dev) 501 { 502 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 503 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 504 int ret; 505 506 drv->suspended = true; 507 508 /* Ensure runtime resume runs on resume */ 509 ret = pm_runtime_resume_and_get(dev); 510 if (ret) { 511 dev_err(dev, "failed to resume for suspend: %d\n", ret); 512 return ret; 513 } 514 515 sdca_irq_disable(drv->function, drv->core->irq_info); 516 517 ret = pm_runtime_force_suspend(dev); 518 if (ret) { 519 dev_err(dev, "failed to force suspend: %d\n", ret); 520 return ret; 521 } 522 523 pm_runtime_put_noidle(dev); 524 525 return 0; 526 } 527 528 static int class_function_resume(struct device *dev) 529 { 530 int ret; 531 532 ret = pm_runtime_force_resume(dev); 533 if (ret) { 534 dev_err(dev, "failed to force resume: %d\n", ret); 535 return ret; 536 } 537 538 return 0; 539 } 540 541 static const struct dev_pm_ops class_function_pm_ops = { 542 SYSTEM_SLEEP_PM_OPS(class_function_suspend, class_function_resume) 543 RUNTIME_PM_OPS(class_function_runtime_suspend, 544 class_function_runtime_resume, NULL) 545 }; 546 547 static const struct auxiliary_device_id class_function_id_table[] = { 548 { 549 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_AMP_NAME, 550 .driver_data = SDCA_FUNCTION_TYPE_SMART_AMP, 551 }, 552 { 553 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_MIC_NAME, 554 .driver_data = SDCA_FUNCTION_TYPE_SMART_MIC, 555 }, 556 { 557 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_UAJ_NAME, 558 .driver_data = SDCA_FUNCTION_TYPE_UAJ, 559 }, 560 { 561 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_HID_NAME, 562 .driver_data = SDCA_FUNCTION_TYPE_HID, 563 }, 564 { 565 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_RJ_NAME, 566 .driver_data = SDCA_FUNCTION_TYPE_RJ, 567 }, 568 {}, 569 }; 570 MODULE_DEVICE_TABLE(auxiliary, class_function_id_table); 571 572 static struct auxiliary_driver class_function_drv = { 573 .driver = { 574 .name = "sdca_function", 575 .pm = pm_ptr(&class_function_pm_ops), 576 }, 577 578 .probe = class_function_probe, 579 .remove = class_function_remove, 580 .id_table = class_function_id_table 581 }; 582 module_auxiliary_driver(class_function_drv); 583 584 MODULE_LICENSE("GPL"); 585 MODULE_DESCRIPTION("SDCA Class Function Driver"); 586 MODULE_IMPORT_NS("SND_SOC_SDCA"); 587