FILE
sdca_class_function.c
File preview with syntax highlighting.
linux-6.19.14/sound/soc/sdca/sdca_class_function.c
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 30struct 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 38static void class_function_regmap_lock(void *data) 39{ 40 struct mutex *lock = data; 41 42 mutex_lock(lock); 43} 44 45static void class_function_regmap_unlock(void *data) 46{ 47 struct mutex *lock = data; 48 49 mutex_unlock(lock); 50} 51 52static 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 60static 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 68static 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 76static 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 94static 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 102static 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 110static 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 117static 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 126static 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 162static 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 175static 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 183static 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 191static 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 199static void class_function_component_remove(struct snd_soc_component *component) 200{ 201 struct class_function_drv *drv = snd_soc_component_get_drvdata(component); 202 struct sdca_class_drv *core = drv->core; 203 204 sdca_irq_cleanup(component->dev, drv->function, core->irq_info); 205} 206 207static int class_function_set_jack(struct snd_soc_component *component, 208 struct snd_soc_jack *jack, void *d) 209{ 210 struct class_function_drv *drv = snd_soc_component_get_drvdata(component); 211 struct sdca_class_drv *core = drv->core; 212 213 return sdca_jack_set_jack(core->irq_info, jack); 214} 215 216static const struct snd_soc_component_driver class_function_component_drv = { 217 .probe = class_function_component_probe, 218 .remove = class_function_component_remove, 219 .endianness = 1, 220}; 221 222static int class_function_boot(struct class_function_drv *drv) 223{ 224 unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr, 225 SDCA_ENTITY_TYPE_ENTITY_0, 226 SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0); 227 unsigned int val; 228 int ret; 229 230 ret = regmap_read(drv->regmap, reg, &val); 231 if (ret < 0) { 232 dev_err(drv->dev, "failed to read function status: %d\n", ret); 233 return ret; 234 } 235 236 if (!(val & SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET)) { 237 dev_dbg(drv->dev, "reset function device\n"); 238 239 ret = sdca_reset_function(drv->dev, drv->function, drv->regmap); 240 if (ret) 241 return ret; 242 } 243 244 if (val & SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION) { 245 dev_dbg(drv->dev, "write initialisation\n"); 246 247 ret = sdca_regmap_write_init(drv->dev, drv->core->dev_regmap, 248 drv->function); 249 if (ret) 250 return ret; 251 252 ret = regmap_write(drv->regmap, reg, 253 SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION); 254 if (ret < 0) { 255 dev_err(drv->dev, 256 "failed to clear function init status: %d\n", 257 ret); 258 return ret; 259 } 260 } 261 262 /* Start FDL process */ 263 ret = sdca_irq_populate_early(drv->dev, drv->regmap, drv->function, 264 drv->core->irq_info); 265 if (ret) 266 return ret; 267 268 ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info); 269 if (ret) 270 return ret; 271 272 ret = sdca_regmap_write_defaults(drv->dev, drv->regmap, drv->function); 273 if (ret) 274 return ret; 275 276 ret = regmap_write(drv->regmap, reg, 0xFF); 277 if (ret < 0) { 278 dev_err(drv->dev, "failed to clear function status: %d\n", ret); 279 return ret; 280 } 281 282 return 0; 283} 284 285static int class_function_probe(struct auxiliary_device *auxdev, 286 const struct auxiliary_device_id *aux_dev_id) 287{ 288 struct device *dev = &auxdev->dev; 289 struct sdca_class_drv *core = dev_get_drvdata(dev->parent); 290 struct sdca_device_data *data = &core->sdw->sdca_data; 291 struct sdca_function_desc *desc; 292 struct snd_soc_component_driver *cmp_drv; 293 struct snd_soc_dai_driver *dais; 294 struct class_function_drv *drv; 295 struct regmap_sdw_mbq_cfg *mbq_config; 296 struct regmap_config *config; 297 struct reg_default *defaults; 298 int ndefaults; 299 int num_dais; 300 int ret; 301 int i; 302 303 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL); 304 if (!drv) 305 return -ENOMEM; 306 307 cmp_drv = devm_kmemdup(dev, &class_function_component_drv, sizeof(*cmp_drv), 308 GFP_KERNEL); 309 if (!cmp_drv) 310 return -ENOMEM; 311 312 config = devm_kmemdup(dev, &class_function_regmap_config, sizeof(*config), 313 GFP_KERNEL); 314 if (!config) 315 return -ENOMEM; 316 317 mbq_config = devm_kmemdup(dev, &class_function_mbq_config, sizeof(*mbq_config), 318 GFP_KERNEL); 319 if (!mbq_config) 320 return -ENOMEM; 321 322 drv->dev = dev; 323 drv->core = core; 324 325 for (i = 0; i < data->num_functions; i++) { 326 desc = &data->function[i]; 327 328 if (desc->type == aux_dev_id->driver_data) 329 break; 330 } 331 if (i == core->sdw->sdca_data.num_functions) { 332 dev_err(dev, "failed to locate function\n"); 333 return -EINVAL; 334 } 335 336 drv->function = &core->functions[i]; 337 338 ret = sdca_parse_function(dev, core->sdw, desc, drv->function); 339 if (ret) 340 return ret; 341 342 ndefaults = sdca_regmap_count_constants(dev, drv->function); 343 if (ndefaults < 0) 344 return ndefaults; 345 346 defaults = devm_kcalloc(dev, ndefaults, sizeof(*defaults), GFP_KERNEL); 347 if (!defaults) 348 return -ENOMEM; 349 350 ret = sdca_regmap_populate_constants(dev, drv->function, defaults); 351 if (ret < 0) 352 return ret; 353 354 regcache_sort_defaults(defaults, ndefaults); 355 356 auxiliary_set_drvdata(auxdev, drv); 357 358 config->reg_defaults = defaults; 359 config->num_reg_defaults = ndefaults; 360 config->lock_arg = &core->regmap_lock; 361 362 if (drv->function->busy_max_delay) { 363 mbq_config->timeout_us = drv->function->busy_max_delay; 364 mbq_config->retry_us = umax(drv->function->busy_max_delay / 10, 365 mbq_config->retry_us); 366 } 367 368 drv->regmap = devm_regmap_init_sdw_mbq_cfg(dev, core->sdw, config, mbq_config); 369 if (IS_ERR(drv->regmap)) 370 return dev_err_probe(dev, PTR_ERR(drv->regmap), 371 "failed to create regmap"); 372 373 if (desc->type == SDCA_FUNCTION_TYPE_UAJ) 374 cmp_drv->set_jack = class_function_set_jack; 375 376 ret = sdca_asoc_populate_component(dev, drv->function, cmp_drv, 377 &dais, &num_dais, 378 &class_function_sdw_ops); 379 if (ret) 380 return ret; 381 382 pm_runtime_set_autosuspend_delay(dev, 200); 383 pm_runtime_use_autosuspend(dev); 384 pm_runtime_set_active(dev); 385 pm_runtime_get_noresume(dev); 386 387 ret = devm_pm_runtime_enable(dev); 388 if (ret) 389 return ret; 390 391 ret = class_function_boot(drv); 392 if (ret) 393 return ret; 394 395 ret = devm_snd_soc_register_component(dev, cmp_drv, dais, num_dais); 396 if (ret) 397 return dev_err_probe(dev, ret, "failed to register component\n"); 398 399 pm_runtime_mark_last_busy(dev); 400 pm_runtime_put_autosuspend(dev); 401 402 return 0; 403} 404 405static void class_function_remove(struct auxiliary_device *auxdev) 406{ 407 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 408 409 sdca_irq_cleanup(drv->dev, drv->function, drv->core->irq_info); 410} 411 412static int class_function_runtime_suspend(struct device *dev) 413{ 414 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 415 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 416 417 /* 418 * Whilst the driver doesn't power the chip down here, going into 419 * runtime suspend means the driver can't be sure the bus won't 420 * power down which would prevent communication with the device. 421 */ 422 regcache_cache_only(drv->regmap, true); 423 424 return 0; 425} 426 427static int class_function_runtime_resume(struct device *dev) 428{ 429 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 430 struct class_function_drv *drv = auxiliary_get_drvdata(auxdev); 431 int ret; 432 433 regcache_mark_dirty(drv->regmap); 434 regcache_cache_only(drv->regmap, false); 435 436 ret = regcache_sync(drv->regmap); 437 if (ret) { 438 dev_err(drv->dev, "failed to restore register cache: %d\n", ret); 439 goto err; 440 } 441 442 return 0; 443 444err: 445 regcache_cache_only(drv->regmap, true); 446 447 return ret; 448} 449 450static const struct dev_pm_ops class_function_pm_ops = { 451 RUNTIME_PM_OPS(class_function_runtime_suspend, 452 class_function_runtime_resume, NULL) 453}; 454 455static const struct auxiliary_device_id class_function_id_table[] = { 456 { 457 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_AMP_NAME, 458 .driver_data = SDCA_FUNCTION_TYPE_SMART_AMP, 459 }, 460 { 461 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_MIC_NAME, 462 .driver_data = SDCA_FUNCTION_TYPE_SMART_MIC, 463 }, 464 { 465 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_UAJ_NAME, 466 .driver_data = SDCA_FUNCTION_TYPE_UAJ, 467 }, 468 { 469 .name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_HID_NAME, 470 .driver_data = SDCA_FUNCTION_TYPE_HID, 471 }, 472 {}, 473}; 474MODULE_DEVICE_TABLE(auxiliary, class_function_id_table); 475 476static struct auxiliary_driver class_function_drv = { 477 .driver = { 478 .name = "sdca_function", 479 .pm = pm_ptr(&class_function_pm_ops), 480 }, 481 482 .probe = class_function_probe, 483 .remove = class_function_remove, 484 .id_table = class_function_id_table 485}; 486module_auxiliary_driver(class_function_drv); 487 488MODULE_LICENSE("GPL"); 489MODULE_DESCRIPTION("SDCA Class Function Driver"); 490MODULE_IMPORT_NS("SND_SOC_SDCA");
~/
Read-only file preview.