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/bitmap.h> 11 #include <linux/bits.h> 12 #include <linux/cleanup.h> 13 #include <linux/device.h> 14 #include <linux/dev_printk.h> 15 #include <linux/interrupt.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/regmap.h> 18 #include <linux/soundwire/sdw.h> 19 #include <linux/soundwire/sdw_registers.h> 20 #include <sound/sdca.h> 21 #include <sound/sdca_fdl.h> 22 #include <sound/sdca_function.h> 23 #include <sound/sdca_hid.h> 24 #include <sound/sdca_interrupts.h> 25 #include <sound/sdca_jack.h> 26 #include <sound/sdca_ump.h> 27 #include <sound/soc-component.h> 28 #include <sound/soc.h> 29 30 #define IRQ_SDCA(number) REGMAP_IRQ_REG(number, ((number) / BITS_PER_BYTE), \ 31 SDW_SCP_SDCA_INTMASK_SDCA_##number) 32 33 static const struct regmap_irq regmap_irqs[SDCA_MAX_INTERRUPTS] = { 34 IRQ_SDCA(0), 35 IRQ_SDCA(1), 36 IRQ_SDCA(2), 37 IRQ_SDCA(3), 38 IRQ_SDCA(4), 39 IRQ_SDCA(5), 40 IRQ_SDCA(6), 41 IRQ_SDCA(7), 42 IRQ_SDCA(8), 43 IRQ_SDCA(9), 44 IRQ_SDCA(10), 45 IRQ_SDCA(11), 46 IRQ_SDCA(12), 47 IRQ_SDCA(13), 48 IRQ_SDCA(14), 49 IRQ_SDCA(15), 50 IRQ_SDCA(16), 51 IRQ_SDCA(17), 52 IRQ_SDCA(18), 53 IRQ_SDCA(19), 54 IRQ_SDCA(20), 55 IRQ_SDCA(21), 56 IRQ_SDCA(22), 57 IRQ_SDCA(23), 58 IRQ_SDCA(24), 59 IRQ_SDCA(25), 60 IRQ_SDCA(26), 61 IRQ_SDCA(27), 62 IRQ_SDCA(28), 63 IRQ_SDCA(29), 64 IRQ_SDCA(30), 65 }; 66 67 static const struct regmap_irq_chip sdca_irq_chip = { 68 .name = "sdca_irq", 69 70 .status_base = SDW_SCP_SDCA_INT1, 71 .unmask_base = SDW_SCP_SDCA_INTMASK1, 72 .ack_base = SDW_SCP_SDCA_INT1, 73 .num_regs = 4, 74 75 .irqs = regmap_irqs, 76 .num_irqs = SDCA_MAX_INTERRUPTS, 77 78 .runtime_pm = true, 79 }; 80 81 static irqreturn_t base_handler(int irq, void *data) 82 { 83 struct sdca_interrupt *interrupt = data; 84 struct device *dev = interrupt->dev; 85 86 dev_info(dev, "%s irq without full handling\n", interrupt->name); 87 88 return IRQ_HANDLED; 89 } 90 91 static irqreturn_t function_status_handler(int irq, void *data) 92 { 93 struct sdca_interrupt *interrupt = data; 94 struct device *dev = interrupt->dev; 95 irqreturn_t irqret = IRQ_NONE; 96 unsigned int reg, val; 97 unsigned long status; 98 unsigned int mask; 99 int ret; 100 101 ret = pm_runtime_get_sync(dev); 102 if (ret < 0) { 103 dev_err(dev, "failed to resume for function status: %d\n", ret); 104 goto error; 105 } 106 107 reg = SDW_SDCA_CTL(interrupt->function->desc->adr, interrupt->entity->id, 108 interrupt->control->sel, 0); 109 110 ret = regmap_read(interrupt->function_regmap, reg, &val); 111 if (ret < 0) { 112 dev_err(dev, "failed to read function status: %d\n", ret); 113 goto error; 114 } 115 116 dev_dbg(dev, "function status: %#x\n", val); 117 118 status = val; 119 for_each_set_bit(mask, &status, BITS_PER_BYTE) { 120 mask = 1 << mask; 121 122 switch (mask) { 123 case SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION: 124 //FIXME: Add init writes 125 break; 126 case SDCA_CTL_ENTITY_0_FUNCTION_FAULT: 127 dev_err(dev, "function fault\n"); 128 break; 129 case SDCA_CTL_ENTITY_0_UMP_SEQUENCE_FAULT: 130 dev_err(dev, "ump sequence fault\n"); 131 break; 132 case SDCA_CTL_ENTITY_0_FUNCTION_BUSY: 133 dev_info(dev, "unexpected function busy\n"); 134 break; 135 case SDCA_CTL_ENTITY_0_DEVICE_NEWLY_ATTACHED: 136 case SDCA_CTL_ENTITY_0_INTS_DISABLED_ABNORMALLY: 137 case SDCA_CTL_ENTITY_0_STREAMING_STOPPED_ABNORMALLY: 138 case SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET: 139 break; 140 } 141 } 142 143 ret = regmap_write(interrupt->function_regmap, reg, val); 144 if (ret < 0) { 145 dev_err(dev, "failed to clear function status: %d\n", ret); 146 goto error; 147 } 148 149 irqret = IRQ_HANDLED; 150 error: 151 pm_runtime_put(dev); 152 return irqret; 153 } 154 155 static irqreturn_t detected_mode_handler(int irq, void *data) 156 { 157 struct sdca_interrupt *interrupt = data; 158 struct device *dev = interrupt->dev; 159 irqreturn_t irqret = IRQ_NONE; 160 int ret; 161 162 ret = pm_runtime_get_sync(dev); 163 if (ret < 0) { 164 dev_err(dev, "failed to resume for detected mode: %d\n", ret); 165 goto error; 166 } 167 168 ret = sdca_jack_process(interrupt); 169 if (ret) 170 goto error; 171 172 irqret = IRQ_HANDLED; 173 error: 174 pm_runtime_put(dev); 175 return irqret; 176 } 177 178 static irqreturn_t hid_handler(int irq, void *data) 179 { 180 struct sdca_interrupt *interrupt = data; 181 struct device *dev = interrupt->dev; 182 irqreturn_t irqret = IRQ_NONE; 183 int ret; 184 185 ret = pm_runtime_get_sync(dev); 186 if (ret < 0) { 187 dev_err(dev, "failed to resume for hid: %d\n", ret); 188 goto error; 189 } 190 191 ret = sdca_hid_process_report(interrupt); 192 if (ret) 193 goto error; 194 195 irqret = IRQ_HANDLED; 196 error: 197 pm_runtime_put(dev); 198 return irqret; 199 } 200 201 static irqreturn_t fdl_owner_handler(int irq, void *data) 202 { 203 struct sdca_interrupt *interrupt = data; 204 struct device *dev = interrupt->dev; 205 irqreturn_t irqret = IRQ_NONE; 206 int ret; 207 208 ret = pm_runtime_get_sync(dev); 209 if (ret < 0) { 210 dev_err(dev, "failed to resume for fdl: %d\n", ret); 211 goto error; 212 } 213 214 ret = sdca_fdl_process(interrupt); 215 if (ret) 216 goto error; 217 218 irqret = IRQ_HANDLED; 219 error: 220 pm_runtime_put(dev); 221 return irqret; 222 } 223 224 static int sdca_irq_request_locked(struct device *dev, 225 struct sdca_interrupt_info *info, 226 int sdca_irq, const char *name, 227 irq_handler_t handler, void *data) 228 { 229 int irq; 230 int ret; 231 232 irq = regmap_irq_get_virq(info->irq_data, sdca_irq); 233 if (irq < 0) 234 return irq; 235 236 ret = devm_request_threaded_irq(dev, irq, NULL, handler, 237 IRQF_ONESHOT, name, data); 238 if (ret) 239 return ret; 240 241 info->irqs[sdca_irq].irq = irq; 242 243 dev_dbg(dev, "requested irq %d for %s\n", irq, name); 244 245 return 0; 246 } 247 248 /** 249 * sdca_request_irq - request an individual SDCA interrupt 250 * @dev: Pointer to the struct device against which things should be allocated. 251 * @interrupt_info: Pointer to the interrupt information structure. 252 * @sdca_irq: SDCA interrupt position. 253 * @name: Name to be given to the IRQ. 254 * @handler: A callback thread function to be called for the IRQ. 255 * @data: Private data pointer that will be passed to the handler. 256 * 257 * Typically this is handled internally by sdca_irq_populate, however if 258 * a device requires custom IRQ handling this can be called manually before 259 * calling sdca_irq_populate, which will then skip that IRQ whilst processing. 260 * 261 * Return: Zero on success, and a negative error code on failure. 262 */ 263 int sdca_irq_request(struct device *dev, struct sdca_interrupt_info *info, 264 int sdca_irq, const char *name, irq_handler_t handler, 265 void *data) 266 { 267 int ret; 268 269 if (sdca_irq < 0 || sdca_irq >= SDCA_MAX_INTERRUPTS) { 270 dev_err(dev, "bad irq request: %d\n", sdca_irq); 271 return -EINVAL; 272 } 273 274 guard(mutex)(&info->irq_lock); 275 276 ret = sdca_irq_request_locked(dev, info, sdca_irq, name, handler, data); 277 if (ret) { 278 dev_err(dev, "failed to request irq %s: %d\n", name, ret); 279 return ret; 280 } 281 282 return 0; 283 } 284 EXPORT_SYMBOL_NS_GPL(sdca_irq_request, "SND_SOC_SDCA"); 285 286 /** 287 * sdca_irq_data_populate - Populate common interrupt data 288 * @dev: Pointer to the Function device. 289 * @regmap: Pointer to the Function regmap. 290 * @component: Pointer to the ASoC component for the Function. 291 * @function: Pointer to the SDCA Function. 292 * @entity: Pointer to the SDCA Entity. 293 * @control: Pointer to the SDCA Control. 294 * @interrupt: Pointer to the SDCA interrupt for this IRQ. 295 * 296 * Return: Zero on success, and a negative error code on failure. 297 */ 298 int sdca_irq_data_populate(struct device *dev, struct regmap *regmap, 299 struct snd_soc_component *component, 300 struct sdca_function_data *function, 301 struct sdca_entity *entity, 302 struct sdca_control *control, 303 struct sdca_interrupt *interrupt) 304 { 305 const char *name; 306 307 if (!dev && component) 308 dev = component->dev; 309 if (!dev) 310 return -ENODEV; 311 312 name = devm_kasprintf(dev, GFP_KERNEL, "%s %s %s", function->desc->name, 313 entity->label, control->label); 314 if (!name) 315 return -ENOMEM; 316 317 interrupt->name = name; 318 interrupt->dev = dev; 319 if (!regmap && component) 320 interrupt->function_regmap = component->regmap; 321 else 322 interrupt->function_regmap = regmap; 323 interrupt->component = component; 324 interrupt->function = function; 325 interrupt->entity = entity; 326 interrupt->control = control; 327 328 return 0; 329 } 330 EXPORT_SYMBOL_NS_GPL(sdca_irq_data_populate, "SND_SOC_SDCA"); 331 332 static struct sdca_interrupt *get_interrupt_data(struct device *dev, int irq, 333 struct sdca_interrupt_info *info) 334 { 335 if (irq == SDCA_NO_INTERRUPT) { 336 return NULL; 337 } else if (irq < 0 || irq >= SDCA_MAX_INTERRUPTS) { 338 dev_err(dev, "bad irq position: %d\n", irq); 339 return ERR_PTR(-EINVAL); 340 } 341 342 if (info->irqs[irq].irq) { 343 dev_dbg(dev, "skipping irq %d, already requested\n", irq); 344 return NULL; 345 } 346 347 return &info->irqs[irq]; 348 } 349 350 /** 351 * sdca_irq_populate_early - process pre-audio card IRQ registrations 352 * @dev: Device pointer for SDCA Function. 353 * @regmap: Regmap pointer for the SDCA Function. 354 * @function: Pointer to the SDCA Function. 355 * @info: Pointer to the SDCA interrupt info for this device. 356 * 357 * This is intended to be used as part of the Function boot process. It 358 * can be called before the soundcard is registered (ie. doesn't depend 359 * on component) and will register the FDL interrupts. 360 * 361 * Return: Zero on success, and a negative error code on failure. 362 */ 363 int sdca_irq_populate_early(struct device *dev, struct regmap *regmap, 364 struct sdca_function_data *function, 365 struct sdca_interrupt_info *info) 366 { 367 int i, j; 368 369 guard(mutex)(&info->irq_lock); 370 371 for (i = 0; i < function->num_entities; i++) { 372 struct sdca_entity *entity = &function->entities[i]; 373 374 for (j = 0; j < entity->num_controls; j++) { 375 struct sdca_control *control = &entity->controls[j]; 376 int irq = control->interrupt_position; 377 struct sdca_interrupt *interrupt; 378 int ret; 379 380 interrupt = get_interrupt_data(dev, irq, info); 381 if (IS_ERR(interrupt)) 382 return PTR_ERR(interrupt); 383 else if (!interrupt) 384 continue; 385 386 switch (SDCA_CTL_TYPE(entity->type, control->sel)) { 387 case SDCA_CTL_TYPE_S(XU, FDL_CURRENTOWNER): 388 ret = sdca_irq_data_populate(dev, regmap, NULL, 389 function, entity, 390 control, interrupt); 391 if (ret) 392 return ret; 393 394 ret = sdca_fdl_alloc_state(interrupt); 395 if (ret) 396 return ret; 397 398 ret = sdca_irq_request_locked(dev, info, irq, 399 interrupt->name, 400 fdl_owner_handler, 401 interrupt); 402 if (ret) { 403 dev_err(dev, "failed to request irq %s: %d\n", 404 interrupt->name, ret); 405 return ret; 406 } 407 break; 408 default: 409 break; 410 } 411 } 412 } 413 414 return 0; 415 } 416 EXPORT_SYMBOL_NS_GPL(sdca_irq_populate_early, "SND_SOC_SDCA"); 417 418 /** 419 * sdca_irq_populate - Request all the individual IRQs for an SDCA Function 420 * @function: Pointer to the SDCA Function. 421 * @component: Pointer to the ASoC component for the Function. 422 * @info: Pointer to the SDCA interrupt info for this device. 423 * 424 * Typically this would be called from the driver for a single SDCA Function. 425 * 426 * Return: Zero on success, and a negative error code on failure. 427 */ 428 int sdca_irq_populate(struct sdca_function_data *function, 429 struct snd_soc_component *component, 430 struct sdca_interrupt_info *info) 431 { 432 struct device *dev = component->dev; 433 int i, j; 434 435 guard(mutex)(&info->irq_lock); 436 437 for (i = 0; i < function->num_entities; i++) { 438 struct sdca_entity *entity = &function->entities[i]; 439 440 for (j = 0; j < entity->num_controls; j++) { 441 struct sdca_control *control = &entity->controls[j]; 442 int irq = control->interrupt_position; 443 struct sdca_interrupt *interrupt; 444 irq_handler_t handler; 445 int ret; 446 447 interrupt = get_interrupt_data(dev, irq, info); 448 if (IS_ERR(interrupt)) 449 return PTR_ERR(interrupt); 450 else if (!interrupt) 451 continue; 452 453 ret = sdca_irq_data_populate(dev, NULL, component, 454 function, entity, control, 455 interrupt); 456 if (ret) 457 return ret; 458 459 handler = base_handler; 460 461 switch (SDCA_CTL_TYPE(entity->type, control->sel)) { 462 case SDCA_CTL_TYPE_S(ENTITY_0, FUNCTION_STATUS): 463 handler = function_status_handler; 464 break; 465 case SDCA_CTL_TYPE_S(GE, DETECTED_MODE): 466 ret = sdca_jack_alloc_state(interrupt); 467 if (ret) 468 return ret; 469 470 handler = detected_mode_handler; 471 break; 472 case SDCA_CTL_TYPE_S(XU, FDL_CURRENTOWNER): 473 ret = sdca_fdl_alloc_state(interrupt); 474 if (ret) 475 return ret; 476 477 handler = fdl_owner_handler; 478 break; 479 case SDCA_CTL_TYPE_S(HIDE, HIDTX_CURRENTOWNER): 480 handler = hid_handler; 481 break; 482 default: 483 break; 484 } 485 486 ret = sdca_irq_request_locked(dev, info, irq, interrupt->name, 487 handler, interrupt); 488 if (ret) { 489 dev_err(dev, "failed to request irq %s: %d\n", 490 interrupt->name, ret); 491 return ret; 492 } 493 } 494 } 495 496 return 0; 497 } 498 EXPORT_SYMBOL_NS_GPL(sdca_irq_populate, "SND_SOC_SDCA"); 499 500 /** 501 * sdca_irq_allocate - allocate an SDCA interrupt structure for a device 502 * @sdev: Device pointer against which things should be allocated. 503 * @regmap: regmap to be used for accessing the SDCA IRQ registers. 504 * @irq: The interrupt number. 505 * 506 * Typically this would be called from the top level driver for the whole 507 * SDCA device, as only a single instance is required across all Functions 508 * on the device. 509 * 510 * Return: A pointer to the allocated sdca_interrupt_info struct, or an 511 * error code. 512 */ 513 struct sdca_interrupt_info *sdca_irq_allocate(struct device *sdev, 514 struct regmap *regmap, int irq) 515 { 516 struct sdca_interrupt_info *info; 517 int ret, i; 518 519 info = devm_kzalloc(sdev, sizeof(*info), GFP_KERNEL); 520 if (!info) 521 return ERR_PTR(-ENOMEM); 522 523 info->irq_chip = sdca_irq_chip; 524 525 for (i = 0; i < ARRAY_SIZE(info->irqs); i++) 526 info->irqs[i].device_regmap = regmap; 527 528 ret = devm_mutex_init(sdev, &info->irq_lock); 529 if (ret) 530 return ERR_PTR(ret); 531 532 ret = devm_regmap_add_irq_chip(sdev, regmap, irq, IRQF_ONESHOT, 0, 533 &info->irq_chip, &info->irq_data); 534 if (ret) { 535 dev_err(sdev, "failed to register irq chip: %d\n", ret); 536 return ERR_PTR(ret); 537 } 538 539 dev_dbg(sdev, "registered on irq %d\n", irq); 540 541 return info; 542 } 543 EXPORT_SYMBOL_NS_GPL(sdca_irq_allocate, "SND_SOC_SDCA"); 544