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