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