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/bits.h> 11 #include <linux/bitmap.h> 12 #include <linux/build_bug.h> 13 #include <linux/delay.h> 14 #include <linux/dev_printk.h> 15 #include <linux/device.h> 16 #include <linux/minmax.h> 17 #include <linux/module.h> 18 #include <linux/overflow.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/regmap.h> 21 #include <linux/soundwire/sdw_registers.h> 22 #include <linux/string_helpers.h> 23 #include <linux/types.h> 24 #include <sound/control.h> 25 #include <sound/pcm.h> 26 #include <sound/pcm_params.h> 27 #include <sound/sdca.h> 28 #include <sound/sdca_asoc.h> 29 #include <sound/sdca_function.h> 30 #include <sound/soc.h> 31 #include <sound/soc-component.h> 32 #include <sound/soc-dai.h> 33 #include <sound/soc-dapm.h> 34 #include <sound/tlv.h> 35 36 static bool exported_control(struct sdca_entity *entity, struct sdca_control *control) 37 { 38 switch (SDCA_CTL_TYPE(entity->type, control->sel)) { 39 case SDCA_CTL_TYPE_S(GE, DETECTED_MODE): 40 return true; 41 default: 42 break; 43 } 44 45 return control->layers & (SDCA_ACCESS_LAYER_USER | 46 SDCA_ACCESS_LAYER_APPLICATION); 47 } 48 49 static bool readonly_control(struct sdca_control *control) 50 { 51 return control->has_fixed || control->mode == SDCA_ACCESS_MODE_RO; 52 } 53 54 static int ge_count_routes(struct sdca_entity *entity) 55 { 56 int count = 0; 57 int i, j; 58 59 for (i = 0; i < entity->ge.num_modes; i++) { 60 struct sdca_ge_mode *mode = &entity->ge.modes[i]; 61 62 for (j = 0; j < mode->num_controls; j++) { 63 struct sdca_ge_control *affected = &mode->controls[j]; 64 65 if (affected->sel != SDCA_CTL_SU_SELECTOR || affected->val) 66 count++; 67 } 68 } 69 70 return count; 71 } 72 73 /** 74 * sdca_asoc_count_component - count the various component parts 75 * @dev: Pointer to the device against which allocations will be done. 76 * @function: Pointer to the Function information. 77 * @num_widgets: Output integer pointer, will be filled with the 78 * required number of DAPM widgets for the Function. 79 * @num_routes: Output integer pointer, will be filled with the 80 * required number of DAPM routes for the Function. 81 * @num_controls: Output integer pointer, will be filled with the 82 * required number of ALSA controls for the Function. 83 * @num_dais: Output integer pointer, will be filled with the 84 * required number of ASoC DAIs for the Function. 85 * 86 * This function counts various things within the SDCA Function such 87 * that the calling driver can allocate appropriate space before 88 * calling the appropriate population functions. 89 * 90 * Return: Returns zero on success, and a negative error code on failure. 91 */ 92 int sdca_asoc_count_component(struct device *dev, struct sdca_function_data *function, 93 int *num_widgets, int *num_routes, int *num_controls, 94 int *num_dais) 95 { 96 struct sdca_control *control; 97 int i, j; 98 99 *num_widgets = function->num_entities - 1; 100 *num_routes = 0; 101 *num_controls = 0; 102 *num_dais = 0; 103 104 for (i = 0; i < function->num_entities - 1; i++) { 105 struct sdca_entity *entity = &function->entities[i]; 106 bool skip_primary_routes = false; 107 108 /* Add supply/DAI widget connections */ 109 switch (entity->type) { 110 case SDCA_ENTITY_TYPE_IT: 111 case SDCA_ENTITY_TYPE_OT: 112 *num_routes += !!entity->iot.clock; 113 *num_routes += !!entity->iot.is_dataport; 114 *num_controls += !entity->iot.is_dataport; 115 *num_dais += !!entity->iot.is_dataport; 116 break; 117 case SDCA_ENTITY_TYPE_PDE: 118 *num_routes += entity->pde.num_managed; 119 break; 120 case SDCA_ENTITY_TYPE_GE: 121 *num_routes += ge_count_routes(entity); 122 skip_primary_routes = true; 123 break; 124 case SDCA_ENTITY_TYPE_SU: 125 control = sdca_selector_find_control(dev, entity, SDCA_CTL_SU_SELECTOR); 126 if (!control) 127 return -EINVAL; 128 129 skip_primary_routes = (control->layers == SDCA_ACCESS_LAYER_DEVICE); 130 break; 131 default: 132 break; 133 } 134 135 if (entity->group) 136 (*num_routes)++; 137 138 /* Add primary entity connections from DisCo */ 139 if (!skip_primary_routes) 140 *num_routes += entity->num_sources; 141 142 for (j = 0; j < entity->num_controls; j++) { 143 if (exported_control(entity, &entity->controls[j])) 144 (*num_controls)++; 145 } 146 } 147 148 return 0; 149 } 150 EXPORT_SYMBOL_NS(sdca_asoc_count_component, "SND_SOC_SDCA"); 151 152 static int ge_put_enum_double(struct snd_kcontrol *kcontrol, 153 struct snd_ctl_elem_value *ucontrol) 154 { 155 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol); 156 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm); 157 struct device *dev = component->dev; 158 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 159 unsigned int *item = ucontrol->value.enumerated.item; 160 unsigned int reg = e->reg; 161 int ret; 162 163 reg &= ~SDW_SDCA_CTL_CSEL(0x3F); 164 reg |= SDW_SDCA_CTL_CSEL(SDCA_CTL_GE_DETECTED_MODE); 165 166 ret = pm_runtime_resume_and_get(dev); 167 if (ret < 0) { 168 dev_err(dev, "failed to resume writing %s: %d\n", 169 kcontrol->id.name, ret); 170 return ret; 171 } 172 173 ret = snd_soc_component_read(component, reg); 174 pm_runtime_put(dev); 175 if (ret < 0) 176 return ret; 177 else if (ret <= SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS) 178 return -EBUSY; 179 180 ret = snd_soc_enum_item_to_val(e, item[0]); 181 if (ret <= SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS) 182 return -EINVAL; 183 184 return snd_soc_dapm_put_enum_double(kcontrol, ucontrol); 185 } 186 187 static int entity_early_parse_ge(struct device *dev, 188 struct sdca_function_data *function, 189 struct sdca_entity *entity) 190 { 191 struct sdca_control_range *range; 192 struct sdca_control *control; 193 struct snd_kcontrol_new *kctl; 194 struct soc_enum *soc_enum; 195 const char *control_name; 196 unsigned int *values; 197 const char **texts; 198 int i; 199 200 control = sdca_selector_find_control(dev, entity, SDCA_CTL_GE_SELECTED_MODE); 201 if (!control) 202 return -EINVAL; 203 204 if (control->layers != SDCA_ACCESS_LAYER_CLASS) 205 dev_warn(dev, "%s: unexpected access layer: %x\n", 206 entity->label, control->layers); 207 208 range = sdca_control_find_range(dev, entity, control, SDCA_SELECTED_MODE_NCOLS, 0); 209 if (!range) 210 return -EINVAL; 211 212 control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", 213 entity->label, control->label); 214 if (!control_name) 215 return -ENOMEM; 216 217 kctl = devm_kzalloc(dev, sizeof(*kctl), GFP_KERNEL); 218 if (!kctl) 219 return -ENOMEM; 220 221 soc_enum = devm_kzalloc(dev, sizeof(*soc_enum), GFP_KERNEL); 222 if (!soc_enum) 223 return -ENOMEM; 224 225 texts = devm_kcalloc(dev, range->rows + 3, sizeof(*texts), GFP_KERNEL); 226 if (!texts) 227 return -ENOMEM; 228 229 values = devm_kcalloc(dev, range->rows + 3, sizeof(*values), GFP_KERNEL); 230 if (!values) 231 return -ENOMEM; 232 233 texts[0] = "Jack Unplugged"; 234 texts[1] = "Jack Unknown"; 235 texts[2] = "Detection in Progress"; 236 values[0] = SDCA_DETECTED_MODE_JACK_UNPLUGGED; 237 values[1] = SDCA_DETECTED_MODE_JACK_UNKNOWN; 238 values[2] = SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS; 239 for (i = 0; i < range->rows; i++) { 240 enum sdca_terminal_type type; 241 242 type = sdca_range(range, SDCA_SELECTED_MODE_TERM_TYPE, i); 243 244 values[i + 3] = sdca_range(range, SDCA_SELECTED_MODE_INDEX, i); 245 texts[i + 3] = sdca_find_terminal_name(type); 246 if (!texts[i + 3]) { 247 dev_err(dev, "%s: unrecognised terminal type: %#x\n", 248 entity->label, type); 249 return -EINVAL; 250 } 251 } 252 253 soc_enum->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0); 254 soc_enum->items = range->rows + 3; 255 soc_enum->mask = roundup_pow_of_two(soc_enum->items) - 1; 256 soc_enum->texts = texts; 257 soc_enum->values = values; 258 259 kctl->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 260 kctl->name = control_name; 261 kctl->info = snd_soc_info_enum_double; 262 kctl->get = snd_soc_dapm_get_enum_double; 263 kctl->put = ge_put_enum_double; 264 kctl->private_value = (unsigned long)soc_enum; 265 266 entity->ge.kctl = kctl; 267 268 return 0; 269 } 270 271 static void add_route(struct snd_soc_dapm_route **route, const char *sink, 272 const char *control, const char *source) 273 { 274 (*route)->sink = sink; 275 (*route)->control = control; 276 (*route)->source = source; 277 (*route)++; 278 } 279 280 static int entity_parse_simple(struct device *dev, 281 struct sdca_function_data *function, 282 struct sdca_entity *entity, 283 struct snd_soc_dapm_widget **widget, 284 struct snd_soc_dapm_route **route, 285 enum snd_soc_dapm_type id) 286 { 287 int i; 288 289 (*widget)->id = id; 290 (*widget)++; 291 292 for (i = 0; i < entity->num_sources; i++) 293 add_route(route, entity->label, NULL, entity->sources[i]->label); 294 295 return 0; 296 } 297 298 static int entity_parse_it(struct device *dev, 299 struct sdca_function_data *function, 300 struct sdca_entity *entity, 301 struct snd_soc_dapm_widget **widget, 302 struct snd_soc_dapm_route **route) 303 { 304 int i; 305 306 if (entity->iot.is_dataport) { 307 const char *aif_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", 308 entity->label, "Playback"); 309 if (!aif_name) 310 return -ENOMEM; 311 312 (*widget)->id = snd_soc_dapm_aif_in; 313 314 add_route(route, entity->label, NULL, aif_name); 315 } else { 316 (*widget)->id = snd_soc_dapm_mic; 317 } 318 319 if (entity->iot.clock) 320 add_route(route, entity->label, NULL, entity->iot.clock->label); 321 322 for (i = 0; i < entity->num_sources; i++) 323 add_route(route, entity->label, NULL, entity->sources[i]->label); 324 325 (*widget)++; 326 327 return 0; 328 } 329 330 static int entity_parse_ot(struct device *dev, 331 struct sdca_function_data *function, 332 struct sdca_entity *entity, 333 struct snd_soc_dapm_widget **widget, 334 struct snd_soc_dapm_route **route) 335 { 336 int i; 337 338 if (entity->iot.is_dataport) { 339 const char *aif_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", 340 entity->label, "Capture"); 341 if (!aif_name) 342 return -ENOMEM; 343 344 (*widget)->id = snd_soc_dapm_aif_out; 345 346 add_route(route, aif_name, NULL, entity->label); 347 } else { 348 (*widget)->id = snd_soc_dapm_spk; 349 } 350 351 if (entity->iot.clock) 352 add_route(route, entity->label, NULL, entity->iot.clock->label); 353 354 for (i = 0; i < entity->num_sources; i++) 355 add_route(route, entity->label, NULL, entity->sources[i]->label); 356 357 (*widget)++; 358 359 return 0; 360 } 361 362 static int entity_pde_event(struct snd_soc_dapm_widget *widget, 363 struct snd_kcontrol *kctl, int event) 364 { 365 struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm); 366 struct sdca_entity *entity = widget->priv; 367 static const int polls = 100; 368 unsigned int reg, val; 369 int from, to, i; 370 int poll_us; 371 int ret; 372 373 if (!component) 374 return -EIO; 375 376 switch (event) { 377 case SND_SOC_DAPM_POST_PMD: 378 from = widget->on_val; 379 to = widget->off_val; 380 break; 381 case SND_SOC_DAPM_POST_PMU: 382 from = widget->off_val; 383 to = widget->on_val; 384 break; 385 default: 386 return 0; 387 } 388 389 for (i = 0; i < entity->pde.num_max_delay; i++) { 390 struct sdca_pde_delay *delay = &entity->pde.max_delay[i]; 391 392 if (delay->from_ps == from && delay->to_ps == to) { 393 poll_us = delay->us / polls; 394 break; 395 } 396 } 397 398 reg = SDW_SDCA_CTL(SDW_SDCA_CTL_FUNC(widget->reg), 399 SDW_SDCA_CTL_ENT(widget->reg), 400 SDCA_CTL_PDE_ACTUAL_PS, 0); 401 402 for (i = 0; i < polls; i++) { 403 if (i) 404 fsleep(poll_us); 405 406 ret = regmap_read(component->regmap, reg, &val); 407 if (ret) 408 return ret; 409 else if (val == to) 410 return 0; 411 } 412 413 dev_err(component->dev, "%s: power transition failed: %x\n", 414 entity->label, val); 415 return -ETIMEDOUT; 416 } 417 418 static int entity_parse_pde(struct device *dev, 419 struct sdca_function_data *function, 420 struct sdca_entity *entity, 421 struct snd_soc_dapm_widget **widget, 422 struct snd_soc_dapm_route **route) 423 { 424 unsigned int target = (1 << SDCA_PDE_PS0) | (1 << SDCA_PDE_PS3); 425 struct sdca_control_range *range; 426 struct sdca_control *control; 427 unsigned int mask = 0; 428 int i; 429 430 control = sdca_selector_find_control(dev, entity, SDCA_CTL_PDE_REQUESTED_PS); 431 if (!control) 432 return -EINVAL; 433 434 /* Power should only be controlled by the driver */ 435 if (control->layers != SDCA_ACCESS_LAYER_CLASS) 436 dev_warn(dev, "%s: unexpected access layer: %x\n", 437 entity->label, control->layers); 438 439 range = sdca_control_find_range(dev, entity, control, SDCA_REQUESTED_PS_NCOLS, 0); 440 if (!range) 441 return -EINVAL; 442 443 for (i = 0; i < range->rows; i++) 444 mask |= 1 << sdca_range(range, SDCA_REQUESTED_PS_STATE, i); 445 446 if ((mask & target) != target) { 447 dev_err(dev, "%s: power control missing states\n", entity->label); 448 return -EINVAL; 449 } 450 451 (*widget)->id = snd_soc_dapm_supply; 452 (*widget)->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0); 453 (*widget)->mask = GENMASK(control->nbits - 1, 0); 454 (*widget)->on_val = SDCA_PDE_PS0; 455 (*widget)->off_val = SDCA_PDE_PS3; 456 (*widget)->event_flags = SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD; 457 (*widget)->event = entity_pde_event; 458 (*widget)->priv = entity; 459 (*widget)++; 460 461 for (i = 0; i < entity->pde.num_managed; i++) 462 add_route(route, entity->pde.managed[i]->label, NULL, entity->label); 463 464 for (i = 0; i < entity->num_sources; i++) 465 add_route(route, entity->label, NULL, entity->sources[i]->label); 466 467 return 0; 468 } 469 470 /* Device selector units are controlled through a group entity */ 471 static int entity_parse_su_device(struct device *dev, 472 struct sdca_function_data *function, 473 struct sdca_entity *entity, 474 struct snd_soc_dapm_widget **widget, 475 struct snd_soc_dapm_route **route) 476 { 477 struct sdca_control_range *range; 478 int i, j; 479 480 if (!entity->group) { 481 dev_err(dev, "%s: device selector unit missing group\n", entity->label); 482 return -EINVAL; 483 } 484 485 range = sdca_selector_find_range(dev, entity->group, SDCA_CTL_GE_SELECTED_MODE, 486 SDCA_SELECTED_MODE_NCOLS, 0); 487 if (!range) 488 return -EINVAL; 489 490 (*widget)->id = snd_soc_dapm_mux_named_ctl; 491 (*widget)->kcontrol_news = entity->group->ge.kctl; 492 (*widget)->num_kcontrols = 1; 493 (*widget)++; 494 495 for (i = 0; i < entity->group->ge.num_modes; i++) { 496 struct sdca_ge_mode *mode = &entity->group->ge.modes[i]; 497 498 for (j = 0; j < mode->num_controls; j++) { 499 struct sdca_ge_control *affected = &mode->controls[j]; 500 int term; 501 502 if (affected->id != entity->id || 503 affected->sel != SDCA_CTL_SU_SELECTOR || 504 !affected->val) 505 continue; 506 507 if (affected->val - 1 >= entity->num_sources) { 508 dev_err(dev, "%s: bad control value: %#x\n", 509 entity->label, affected->val); 510 return -EINVAL; 511 } 512 513 term = sdca_range_search(range, SDCA_SELECTED_MODE_INDEX, 514 mode->val, SDCA_SELECTED_MODE_TERM_TYPE); 515 if (!term) { 516 dev_err(dev, "%s: mode not found: %#x\n", 517 entity->label, mode->val); 518 return -EINVAL; 519 } 520 521 add_route(route, entity->label, sdca_find_terminal_name(term), 522 entity->sources[affected->val - 1]->label); 523 } 524 } 525 526 return 0; 527 } 528 529 /* Class selector units will be exported as an ALSA control */ 530 static int entity_parse_su_class(struct device *dev, 531 struct sdca_function_data *function, 532 struct sdca_entity *entity, 533 struct sdca_control *control, 534 struct snd_soc_dapm_widget **widget, 535 struct snd_soc_dapm_route **route) 536 { 537 struct snd_kcontrol_new *kctl; 538 struct soc_enum *soc_enum; 539 const char **texts; 540 int i; 541 542 kctl = devm_kzalloc(dev, sizeof(*kctl), GFP_KERNEL); 543 if (!kctl) 544 return -ENOMEM; 545 546 soc_enum = devm_kzalloc(dev, sizeof(*soc_enum), GFP_KERNEL); 547 if (!soc_enum) 548 return -ENOMEM; 549 550 texts = devm_kcalloc(dev, entity->num_sources + 1, sizeof(*texts), GFP_KERNEL); 551 if (!texts) 552 return -ENOMEM; 553 554 texts[0] = "No Signal"; 555 for (i = 0; i < entity->num_sources; i++) 556 texts[i + 1] = entity->sources[i]->label; 557 558 soc_enum->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0); 559 soc_enum->items = entity->num_sources + 1; 560 soc_enum->mask = roundup_pow_of_two(soc_enum->items) - 1; 561 soc_enum->texts = texts; 562 563 kctl->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 564 kctl->name = "Route"; 565 kctl->info = snd_soc_info_enum_double; 566 kctl->get = snd_soc_dapm_get_enum_double; 567 kctl->put = snd_soc_dapm_put_enum_double; 568 kctl->private_value = (unsigned long)soc_enum; 569 570 (*widget)->id = snd_soc_dapm_mux; 571 (*widget)->kcontrol_news = kctl; 572 (*widget)->num_kcontrols = 1; 573 (*widget)++; 574 575 for (i = 0; i < entity->num_sources; i++) 576 add_route(route, entity->label, texts[i + 1], entity->sources[i]->label); 577 578 return 0; 579 } 580 581 static int entity_parse_su(struct device *dev, 582 struct sdca_function_data *function, 583 struct sdca_entity *entity, 584 struct snd_soc_dapm_widget **widget, 585 struct snd_soc_dapm_route **route) 586 { 587 struct sdca_control *control; 588 589 if (!entity->num_sources) { 590 dev_err(dev, "%s: selector with no inputs\n", entity->label); 591 return -EINVAL; 592 } 593 594 control = sdca_selector_find_control(dev, entity, SDCA_CTL_SU_SELECTOR); 595 if (!control) 596 return -EINVAL; 597 598 if (control->layers == SDCA_ACCESS_LAYER_DEVICE) 599 return entity_parse_su_device(dev, function, entity, widget, route); 600 601 if (control->layers != SDCA_ACCESS_LAYER_CLASS) 602 dev_warn(dev, "%s: unexpected access layer: %x\n", 603 entity->label, control->layers); 604 605 return entity_parse_su_class(dev, function, entity, control, widget, route); 606 } 607 608 static int entity_parse_mu(struct device *dev, 609 struct sdca_function_data *function, 610 struct sdca_entity *entity, 611 struct snd_soc_dapm_widget **widget, 612 struct snd_soc_dapm_route **route) 613 { 614 struct sdca_control *control; 615 struct snd_kcontrol_new *kctl; 616 int i; 617 618 if (!entity->num_sources) { 619 dev_err(dev, "%s: selector 1 or more inputs\n", entity->label); 620 return -EINVAL; 621 } 622 623 control = sdca_selector_find_control(dev, entity, SDCA_CTL_MU_MIXER); 624 if (!control) 625 return -EINVAL; 626 627 /* MU control should be through DAPM */ 628 if (control->layers != SDCA_ACCESS_LAYER_CLASS) 629 dev_warn(dev, "%s: unexpected access layer: %x\n", 630 entity->label, control->layers); 631 632 kctl = devm_kcalloc(dev, entity->num_sources, sizeof(*kctl), GFP_KERNEL); 633 if (!kctl) 634 return -ENOMEM; 635 636 for (i = 0; i < entity->num_sources; i++) { 637 const char *control_name; 638 struct soc_mixer_control *mc; 639 640 control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %d", 641 control->label, i + 1); 642 if (!control_name) 643 return -ENOMEM; 644 645 mc = devm_kzalloc(dev, sizeof(*mc), GFP_KERNEL); 646 if (!mc) 647 return -ENOMEM; 648 649 mc->reg = SND_SOC_NOPM; 650 mc->rreg = SND_SOC_NOPM; 651 mc->invert = 1; // Ensure default is connected 652 mc->min = 0; 653 mc->max = 1; 654 655 kctl[i].name = control_name; 656 kctl[i].private_value = (unsigned long)mc; 657 kctl[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 658 kctl[i].info = snd_soc_info_volsw; 659 kctl[i].get = snd_soc_dapm_get_volsw; 660 kctl[i].put = snd_soc_dapm_put_volsw; 661 } 662 663 (*widget)->id = snd_soc_dapm_mixer; 664 (*widget)->kcontrol_news = kctl; 665 (*widget)->num_kcontrols = entity->num_sources; 666 (*widget)++; 667 668 for (i = 0; i < entity->num_sources; i++) 669 add_route(route, entity->label, kctl[i].name, entity->sources[i]->label); 670 671 return 0; 672 } 673 674 static int entity_cs_event(struct snd_soc_dapm_widget *widget, 675 struct snd_kcontrol *kctl, int event) 676 { 677 struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm); 678 struct sdca_entity *entity = widget->priv; 679 680 if (!component) 681 return -EIO; 682 683 if (entity->cs.max_delay) 684 fsleep(entity->cs.max_delay); 685 686 return 0; 687 } 688 689 static int entity_parse_cs(struct device *dev, 690 struct sdca_function_data *function, 691 struct sdca_entity *entity, 692 struct snd_soc_dapm_widget **widget, 693 struct snd_soc_dapm_route **route) 694 { 695 int i; 696 697 (*widget)->id = snd_soc_dapm_supply; 698 (*widget)->subseq = 1; /* Ensure these run after PDEs */ 699 (*widget)->event_flags = SND_SOC_DAPM_POST_PMU; 700 (*widget)->event = entity_cs_event; 701 (*widget)->priv = entity; 702 (*widget)++; 703 704 for (i = 0; i < entity->num_sources; i++) 705 add_route(route, entity->label, NULL, entity->sources[i]->label); 706 707 return 0; 708 } 709 710 /** 711 * sdca_asoc_populate_dapm - fill in arrays of DAPM widgets and routes 712 * @dev: Pointer to the device against which allocations will be done. 713 * @function: Pointer to the Function information. 714 * @widget: Array of DAPM widgets to be populated. 715 * @route: Array of DAPM routes to be populated. 716 * 717 * This function populates arrays of DAPM widgets and routes from the 718 * DisCo information for a particular SDCA Function. Typically, 719 * snd_soc_asoc_count_component will be used to allocate appropriately 720 * sized arrays before calling this function. 721 * 722 * Return: Returns zero on success, and a negative error code on failure. 723 */ 724 int sdca_asoc_populate_dapm(struct device *dev, struct sdca_function_data *function, 725 struct snd_soc_dapm_widget *widget, 726 struct snd_soc_dapm_route *route) 727 { 728 int ret; 729 int i; 730 731 for (i = 0; i < function->num_entities - 1; i++) { 732 struct sdca_entity *entity = &function->entities[i]; 733 734 /* 735 * Some entities need to add controls "early" as they are 736 * referenced by other entities. 737 */ 738 switch (entity->type) { 739 case SDCA_ENTITY_TYPE_GE: 740 ret = entity_early_parse_ge(dev, function, entity); 741 if (ret) 742 return ret; 743 break; 744 default: 745 break; 746 } 747 } 748 749 for (i = 0; i < function->num_entities - 1; i++) { 750 struct sdca_entity *entity = &function->entities[i]; 751 752 widget->name = entity->label; 753 widget->reg = SND_SOC_NOPM; 754 755 switch (entity->type) { 756 case SDCA_ENTITY_TYPE_IT: 757 ret = entity_parse_it(dev, function, entity, &widget, &route); 758 break; 759 case SDCA_ENTITY_TYPE_OT: 760 ret = entity_parse_ot(dev, function, entity, &widget, &route); 761 break; 762 case SDCA_ENTITY_TYPE_PDE: 763 ret = entity_parse_pde(dev, function, entity, &widget, &route); 764 break; 765 case SDCA_ENTITY_TYPE_SU: 766 ret = entity_parse_su(dev, function, entity, &widget, &route); 767 break; 768 case SDCA_ENTITY_TYPE_MU: 769 ret = entity_parse_mu(dev, function, entity, &widget, &route); 770 break; 771 case SDCA_ENTITY_TYPE_CS: 772 ret = entity_parse_cs(dev, function, entity, &widget, &route); 773 break; 774 case SDCA_ENTITY_TYPE_CX: 775 /* 776 * FIXME: For now we will just treat these as a supply, 777 * meaning all options are enabled. 778 */ 779 dev_warn(dev, "%s: clock selectors not fully supported yet\n", 780 entity->label); 781 ret = entity_parse_simple(dev, function, entity, &widget, 782 &route, snd_soc_dapm_supply); 783 break; 784 case SDCA_ENTITY_TYPE_TG: 785 ret = entity_parse_simple(dev, function, entity, &widget, 786 &route, snd_soc_dapm_siggen); 787 break; 788 case SDCA_ENTITY_TYPE_GE: 789 ret = entity_parse_simple(dev, function, entity, &widget, 790 &route, snd_soc_dapm_supply); 791 break; 792 default: 793 ret = entity_parse_simple(dev, function, entity, &widget, 794 &route, snd_soc_dapm_pga); 795 break; 796 } 797 if (ret) 798 return ret; 799 800 if (entity->group) 801 add_route(&route, entity->label, NULL, entity->group->label); 802 } 803 804 return 0; 805 } 806 EXPORT_SYMBOL_NS(sdca_asoc_populate_dapm, "SND_SOC_SDCA"); 807 808 static int q78_write(struct snd_soc_component *component, 809 struct soc_mixer_control *mc, 810 unsigned int reg, const int val) 811 { 812 unsigned int mask = GENMASK(mc->sign_bit, 0); 813 unsigned int reg_val; 814 815 if (val < 0 || val > mc->max - mc->min) 816 return -EINVAL; 817 818 reg_val = (val + mc->min) * mc->shift; 819 820 return snd_soc_component_update_bits(component, reg, mask, reg_val); 821 } 822 823 int sdca_asoc_q78_put_volsw(struct snd_kcontrol *kcontrol, 824 struct snd_ctl_elem_value *ucontrol) 825 { 826 struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value; 827 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 828 int ret; 829 830 ret = q78_write(component, mc, mc->reg, ucontrol->value.integer.value[0]); 831 if (ret < 0) 832 return ret; 833 834 if (snd_soc_volsw_is_stereo(mc)) { 835 int err; /* Don't drop change flag */ 836 837 err = q78_write(component, mc, mc->rreg, ucontrol->value.integer.value[1]); 838 if (err) 839 return err; 840 } 841 842 return ret; 843 } 844 EXPORT_SYMBOL_NS(sdca_asoc_q78_put_volsw, "SND_SOC_SDCA"); 845 846 static int q78_read(struct snd_soc_component *component, 847 struct soc_mixer_control *mc, unsigned int reg) 848 { 849 unsigned int reg_val; 850 int val; 851 852 reg_val = snd_soc_component_read(component, reg); 853 854 val = (sign_extend32(reg_val, mc->sign_bit) / (int)mc->shift) - mc->min; 855 856 return val & GENMASK(mc->sign_bit, 0); 857 } 858 859 int sdca_asoc_q78_get_volsw(struct snd_kcontrol *kcontrol, 860 struct snd_ctl_elem_value *ucontrol) 861 { 862 struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value; 863 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 864 865 ucontrol->value.integer.value[0] = q78_read(component, mc, mc->reg); 866 867 if (snd_soc_volsw_is_stereo(mc)) 868 ucontrol->value.integer.value[1] = q78_read(component, mc, mc->rreg); 869 870 return 0; 871 } 872 EXPORT_SYMBOL_NS(sdca_asoc_q78_get_volsw, "SND_SOC_SDCA"); 873 874 static int control_limit_kctl(struct device *dev, 875 struct sdca_entity *entity, 876 struct sdca_control *control, 877 struct snd_kcontrol_new *kctl) 878 { 879 struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value; 880 struct sdca_control_range *range; 881 int min, max, step; 882 unsigned int *tlv; 883 884 if (control->type != SDCA_CTL_DATATYPE_Q7P8DB) 885 return 0; 886 887 /* 888 * FIXME: For now only handle the simple case of a single linear range 889 */ 890 range = sdca_control_find_range(dev, entity, control, SDCA_VOLUME_LINEAR_NCOLS, 1); 891 if (!range) 892 return -EINVAL; 893 894 min = sdca_range(range, SDCA_VOLUME_LINEAR_MIN, 0); 895 max = sdca_range(range, SDCA_VOLUME_LINEAR_MAX, 0); 896 step = sdca_range(range, SDCA_VOLUME_LINEAR_STEP, 0); 897 898 min = sign_extend32(min, control->nbits - 1); 899 max = sign_extend32(max, control->nbits - 1); 900 901 tlv = devm_kcalloc(dev, 4, sizeof(*tlv), GFP_KERNEL); 902 if (!tlv) 903 return -ENOMEM; 904 905 tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX; 906 tlv[1] = 2 * sizeof(*tlv); 907 tlv[2] = (min * 100) >> 8; 908 tlv[3] = (max * 100) >> 8; 909 910 mc->min = min / step; 911 mc->max = max / step; 912 mc->shift = step; 913 mc->sign_bit = 15; 914 915 kctl->tlv.p = tlv; 916 kctl->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ; 917 kctl->get = sdca_asoc_q78_get_volsw; 918 kctl->put = sdca_asoc_q78_put_volsw; 919 920 return 0; 921 } 922 923 static int volatile_get_volsw(struct snd_kcontrol *kcontrol, 924 struct snd_ctl_elem_value *ucontrol) 925 { 926 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 927 struct device *dev = component->dev; 928 int ret; 929 930 ret = pm_runtime_resume_and_get(dev); 931 if (ret < 0) { 932 dev_err(dev, "failed to resume reading %s: %d\n", 933 kcontrol->id.name, ret); 934 return ret; 935 } 936 937 ret = snd_soc_get_volsw(kcontrol, ucontrol); 938 939 pm_runtime_put(dev); 940 941 return ret; 942 } 943 944 static int volatile_put_volsw(struct snd_kcontrol *kcontrol, 945 struct snd_ctl_elem_value *ucontrol) 946 { 947 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 948 struct device *dev = component->dev; 949 int ret; 950 951 ret = pm_runtime_resume_and_get(dev); 952 if (ret < 0) { 953 dev_err(dev, "failed to resume writing %s: %d\n", 954 kcontrol->id.name, ret); 955 return ret; 956 } 957 958 ret = snd_soc_put_volsw(kcontrol, ucontrol); 959 960 pm_runtime_put(dev); 961 962 return ret; 963 } 964 965 static int populate_control(struct device *dev, 966 struct sdca_function_data *function, 967 struct sdca_entity *entity, 968 struct sdca_control *control, 969 struct snd_kcontrol_new **kctl) 970 { 971 const char *control_suffix = ""; 972 const char *control_name; 973 struct soc_mixer_control *mc; 974 int index = 0; 975 int ret; 976 int cn; 977 978 if (!exported_control(entity, control)) 979 return 0; 980 981 if (control->type == SDCA_CTL_DATATYPE_ONEBIT) 982 control_suffix = " Switch"; 983 984 control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s%s", entity->label, 985 control->label, control_suffix); 986 if (!control_name) 987 return -ENOMEM; 988 989 mc = devm_kzalloc(dev, sizeof(*mc), GFP_KERNEL); 990 if (!mc) 991 return -ENOMEM; 992 993 for_each_set_bit(cn, (unsigned long *)&control->cn_list, 994 BITS_PER_TYPE(control->cn_list)) { 995 switch (index++) { 996 case 0: 997 mc->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, 998 control->sel, cn); 999 mc->rreg = mc->reg; 1000 break; 1001 case 1: 1002 mc->rreg = SDW_SDCA_CTL(function->desc->adr, entity->id, 1003 control->sel, cn); 1004 break; 1005 default: 1006 dev_err(dev, "%s: %s: only mono/stereo controls supported\n", 1007 entity->label, control->label); 1008 return -EINVAL; 1009 } 1010 } 1011 1012 mc->min = 0; 1013 mc->max = clamp((0x1ull << control->nbits) - 1, 0, type_max(mc->max)); 1014 1015 if (SDCA_CTL_TYPE(entity->type, control->sel) == SDCA_CTL_TYPE_S(FU, MUTE)) 1016 mc->invert = true; 1017 1018 (*kctl)->name = control_name; 1019 (*kctl)->private_value = (unsigned long)mc; 1020 (*kctl)->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1021 (*kctl)->info = snd_soc_info_volsw; 1022 if (control->is_volatile) { 1023 (*kctl)->get = volatile_get_volsw; 1024 (*kctl)->put = volatile_put_volsw; 1025 } else { 1026 (*kctl)->get = snd_soc_get_volsw; 1027 (*kctl)->put = snd_soc_put_volsw; 1028 } 1029 1030 if (readonly_control(control)) 1031 (*kctl)->access = SNDRV_CTL_ELEM_ACCESS_READ; 1032 else 1033 (*kctl)->access = SNDRV_CTL_ELEM_ACCESS_READWRITE; 1034 1035 ret = control_limit_kctl(dev, entity, control, *kctl); 1036 if (ret) 1037 return ret; 1038 1039 (*kctl)++; 1040 1041 return 0; 1042 } 1043 1044 static int populate_pin_switch(struct device *dev, 1045 struct sdca_entity *entity, 1046 struct snd_kcontrol_new **kctl) 1047 { 1048 const char *control_name; 1049 1050 control_name = devm_kasprintf(dev, GFP_KERNEL, "%s Switch", entity->label); 1051 if (!control_name) 1052 return -ENOMEM; 1053 1054 (*kctl)->name = control_name; 1055 (*kctl)->private_value = (unsigned long)entity->label; 1056 (*kctl)->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1057 (*kctl)->info = snd_soc_dapm_info_pin_switch; 1058 (*kctl)->get = snd_soc_dapm_get_component_pin_switch; 1059 (*kctl)->put = snd_soc_dapm_put_component_pin_switch; 1060 (*kctl)++; 1061 1062 return 0; 1063 } 1064 1065 /** 1066 * sdca_asoc_populate_controls - fill in an array of ALSA controls for a Function 1067 * @dev: Pointer to the device against which allocations will be done. 1068 * @function: Pointer to the Function information. 1069 * @kctl: Array of ALSA controls to be populated. 1070 * 1071 * This function populates an array of ALSA controls from the DisCo 1072 * information for a particular SDCA Function. Typically, 1073 * snd_soc_asoc_count_component will be used to allocate an 1074 * appropriately sized array before calling this function. 1075 * 1076 * Return: Returns zero on success, and a negative error code on failure. 1077 */ 1078 int sdca_asoc_populate_controls(struct device *dev, 1079 struct sdca_function_data *function, 1080 struct snd_kcontrol_new *kctl) 1081 { 1082 int i, j; 1083 int ret; 1084 1085 for (i = 0; i < function->num_entities; i++) { 1086 struct sdca_entity *entity = &function->entities[i]; 1087 1088 switch (entity->type) { 1089 case SDCA_ENTITY_TYPE_IT: 1090 case SDCA_ENTITY_TYPE_OT: 1091 if (!entity->iot.is_dataport) { 1092 ret = populate_pin_switch(dev, entity, &kctl); 1093 if (ret) 1094 return ret; 1095 } 1096 break; 1097 default: 1098 break; 1099 } 1100 1101 for (j = 0; j < entity->num_controls; j++) { 1102 ret = populate_control(dev, function, entity, 1103 &entity->controls[j], &kctl); 1104 if (ret) 1105 return ret; 1106 } 1107 } 1108 1109 return 0; 1110 } 1111 EXPORT_SYMBOL_NS(sdca_asoc_populate_controls, "SND_SOC_SDCA"); 1112 1113 static unsigned int rate_find_mask(unsigned int rate) 1114 { 1115 switch (rate) { 1116 case 0: 1117 return SNDRV_PCM_RATE_8000_768000; 1118 case 5512: 1119 return SNDRV_PCM_RATE_5512; 1120 case 8000: 1121 return SNDRV_PCM_RATE_8000; 1122 case 11025: 1123 return SNDRV_PCM_RATE_11025; 1124 case 16000: 1125 return SNDRV_PCM_RATE_16000; 1126 case 22050: 1127 return SNDRV_PCM_RATE_22050; 1128 case 32000: 1129 return SNDRV_PCM_RATE_32000; 1130 case 44100: 1131 return SNDRV_PCM_RATE_44100; 1132 case 48000: 1133 return SNDRV_PCM_RATE_48000; 1134 case 64000: 1135 return SNDRV_PCM_RATE_64000; 1136 case 88200: 1137 return SNDRV_PCM_RATE_88200; 1138 case 96000: 1139 return SNDRV_PCM_RATE_96000; 1140 case 176400: 1141 return SNDRV_PCM_RATE_176400; 1142 case 192000: 1143 return SNDRV_PCM_RATE_192000; 1144 case 352800: 1145 return SNDRV_PCM_RATE_352800; 1146 case 384000: 1147 return SNDRV_PCM_RATE_384000; 1148 case 705600: 1149 return SNDRV_PCM_RATE_705600; 1150 case 768000: 1151 return SNDRV_PCM_RATE_768000; 1152 case 12000: 1153 return SNDRV_PCM_RATE_12000; 1154 case 24000: 1155 return SNDRV_PCM_RATE_24000; 1156 case 128000: 1157 return SNDRV_PCM_RATE_128000; 1158 default: 1159 return 0; 1160 } 1161 } 1162 1163 static u64 width_find_mask(unsigned int bits) 1164 { 1165 switch (bits) { 1166 case 0: 1167 return SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | 1168 SNDRV_PCM_FMTBIT_S20_LE | SNDRV_PCM_FMTBIT_S24_LE | 1169 SNDRV_PCM_FMTBIT_S32_LE; 1170 case 8: 1171 return SNDRV_PCM_FMTBIT_S8; 1172 case 16: 1173 return SNDRV_PCM_FMTBIT_S16_LE; 1174 case 20: 1175 return SNDRV_PCM_FMTBIT_S20_LE; 1176 case 24: 1177 return SNDRV_PCM_FMTBIT_S24_LE; 1178 case 32: 1179 return SNDRV_PCM_FMTBIT_S32_LE; 1180 default: 1181 return 0; 1182 } 1183 } 1184 1185 static int populate_rate_format(struct device *dev, 1186 struct sdca_function_data *function, 1187 struct sdca_entity *entity, 1188 struct snd_soc_pcm_stream *stream) 1189 { 1190 struct sdca_control_range *range; 1191 unsigned int sample_rate, sample_width; 1192 unsigned int clock_rates = 0; 1193 unsigned int rates = 0; 1194 u64 formats = 0; 1195 int sel, i; 1196 1197 switch (entity->type) { 1198 case SDCA_ENTITY_TYPE_IT: 1199 sel = SDCA_CTL_IT_USAGE; 1200 break; 1201 case SDCA_ENTITY_TYPE_OT: 1202 sel = SDCA_CTL_OT_USAGE; 1203 break; 1204 default: 1205 dev_err(dev, "%s: entity type has no usage control\n", 1206 entity->label); 1207 return -EINVAL; 1208 } 1209 1210 if (entity->iot.clock) { 1211 range = sdca_selector_find_range(dev, entity->iot.clock, 1212 SDCA_CTL_CS_SAMPLERATEINDEX, 1213 SDCA_SAMPLERATEINDEX_NCOLS, 0); 1214 if (!range) 1215 return -EINVAL; 1216 1217 for (i = 0; i < range->rows; i++) { 1218 sample_rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i); 1219 clock_rates |= rate_find_mask(sample_rate); 1220 } 1221 } else { 1222 clock_rates = UINT_MAX; 1223 } 1224 1225 range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0); 1226 if (!range) 1227 return -EINVAL; 1228 1229 for (i = 0; i < range->rows; i++) { 1230 sample_rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i); 1231 sample_rate = rate_find_mask(sample_rate); 1232 1233 if (sample_rate & clock_rates) { 1234 rates |= sample_rate; 1235 1236 sample_width = sdca_range(range, SDCA_USAGE_SAMPLE_WIDTH, i); 1237 formats |= width_find_mask(sample_width); 1238 } 1239 } 1240 1241 stream->formats = formats; 1242 stream->rates = rates; 1243 1244 return 0; 1245 } 1246 1247 /** 1248 * sdca_asoc_populate_dais - fill in an array of DAI drivers for a Function 1249 * @dev: Pointer to the device against which allocations will be done. 1250 * @function: Pointer to the Function information. 1251 * @dais: Array of DAI drivers to be populated. 1252 * @ops: DAI ops to be attached to each of the created DAI drivers. 1253 * 1254 * This function populates an array of ASoC DAI drivers from the DisCo 1255 * information for a particular SDCA Function. Typically, 1256 * snd_soc_asoc_count_component will be used to allocate an 1257 * appropriately sized array before calling this function. 1258 * 1259 * Return: Returns zero on success, and a negative error code on failure. 1260 */ 1261 int sdca_asoc_populate_dais(struct device *dev, struct sdca_function_data *function, 1262 struct snd_soc_dai_driver *dais, 1263 const struct snd_soc_dai_ops *ops) 1264 { 1265 int i, j; 1266 int ret; 1267 1268 for (i = 0, j = 0; i < function->num_entities - 1; i++) { 1269 struct sdca_entity *entity = &function->entities[i]; 1270 struct snd_soc_pcm_stream *stream; 1271 const char *stream_suffix; 1272 1273 switch (entity->type) { 1274 case SDCA_ENTITY_TYPE_IT: 1275 stream = &dais[j].playback; 1276 stream_suffix = "Playback"; 1277 break; 1278 case SDCA_ENTITY_TYPE_OT: 1279 stream = &dais[j].capture; 1280 stream_suffix = "Capture"; 1281 break; 1282 default: 1283 continue; 1284 } 1285 1286 /* Can't check earlier as only terminals have an iot member. */ 1287 if (!entity->iot.is_dataport) 1288 continue; 1289 1290 stream->stream_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", 1291 entity->label, stream_suffix); 1292 if (!stream->stream_name) 1293 return -ENOMEM; 1294 /* Channels will be further limited by constraints */ 1295 stream->channels_min = 1; 1296 stream->channels_max = SDCA_MAX_CHANNEL_COUNT; 1297 1298 ret = populate_rate_format(dev, function, entity, stream); 1299 if (ret) 1300 return ret; 1301 1302 dais[j].id = i; 1303 dais[j].name = entity->label; 1304 dais[j].ops = ops; 1305 j++; 1306 } 1307 1308 return 0; 1309 } 1310 EXPORT_SYMBOL_NS(sdca_asoc_populate_dais, "SND_SOC_SDCA"); 1311 1312 /** 1313 * sdca_asoc_populate_component - fill in a component driver for a Function 1314 * @dev: Pointer to the device against which allocations will be done. 1315 * @function: Pointer to the Function information. 1316 * @component_drv: Pointer to the component driver to be populated. 1317 * @dai_drv: Pointer to the DAI driver array to be allocated and populated. 1318 * @num_dai_drv: Pointer to integer that will be populated with the number of 1319 * DAI drivers. 1320 * @ops: DAI ops pointer that will be used for each DAI driver. 1321 * 1322 * This function populates a snd_soc_component_driver structure based 1323 * on the DisCo information for a particular SDCA Function. It does 1324 * all allocation internally. 1325 * 1326 * Return: Returns zero on success, and a negative error code on failure. 1327 */ 1328 int sdca_asoc_populate_component(struct device *dev, 1329 struct sdca_function_data *function, 1330 struct snd_soc_component_driver *component_drv, 1331 struct snd_soc_dai_driver **dai_drv, int *num_dai_drv, 1332 const struct snd_soc_dai_ops *ops) 1333 { 1334 struct snd_soc_dapm_widget *widgets; 1335 struct snd_soc_dapm_route *routes; 1336 struct snd_kcontrol_new *controls; 1337 struct snd_soc_dai_driver *dais; 1338 int num_widgets, num_routes, num_controls, num_dais; 1339 int ret; 1340 1341 ret = sdca_asoc_count_component(dev, function, &num_widgets, &num_routes, 1342 &num_controls, &num_dais); 1343 if (ret) 1344 return ret; 1345 1346 widgets = devm_kcalloc(dev, num_widgets, sizeof(*widgets), GFP_KERNEL); 1347 if (!widgets) 1348 return -ENOMEM; 1349 1350 routes = devm_kcalloc(dev, num_routes, sizeof(*routes), GFP_KERNEL); 1351 if (!routes) 1352 return -ENOMEM; 1353 1354 controls = devm_kcalloc(dev, num_controls, sizeof(*controls), GFP_KERNEL); 1355 if (!controls) 1356 return -ENOMEM; 1357 1358 dais = devm_kcalloc(dev, num_dais, sizeof(*dais), GFP_KERNEL); 1359 if (!dais) 1360 return -ENOMEM; 1361 1362 ret = sdca_asoc_populate_dapm(dev, function, widgets, routes); 1363 if (ret) 1364 return ret; 1365 1366 ret = sdca_asoc_populate_controls(dev, function, controls); 1367 if (ret) 1368 return ret; 1369 1370 ret = sdca_asoc_populate_dais(dev, function, dais, ops); 1371 if (ret) 1372 return ret; 1373 1374 component_drv->dapm_widgets = widgets; 1375 component_drv->num_dapm_widgets = num_widgets; 1376 component_drv->dapm_routes = routes; 1377 component_drv->num_dapm_routes = num_routes; 1378 component_drv->controls = controls; 1379 component_drv->num_controls = num_controls; 1380 1381 *dai_drv = dais; 1382 *num_dai_drv = num_dais; 1383 1384 return 0; 1385 } 1386 EXPORT_SYMBOL_NS(sdca_asoc_populate_component, "SND_SOC_SDCA"); 1387 1388 /** 1389 * sdca_asoc_set_constraints - constrain channels available on a DAI 1390 * @dev: Pointer to the device, used for error messages. 1391 * @regmap: Pointer to the Function register map. 1392 * @function: Pointer to the Function information. 1393 * @substream: Pointer to the PCM substream. 1394 * @dai: Pointer to the ASoC DAI. 1395 * 1396 * Typically called from startup(). 1397 * 1398 * Return: Returns zero on success, and a negative error code on failure. 1399 */ 1400 int sdca_asoc_set_constraints(struct device *dev, struct regmap *regmap, 1401 struct sdca_function_data *function, 1402 struct snd_pcm_substream *substream, 1403 struct snd_soc_dai *dai) 1404 { 1405 static const unsigned int channel_list[] = { 1406 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1407 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 1408 }; 1409 struct sdca_entity *entity = &function->entities[dai->id]; 1410 struct snd_pcm_hw_constraint_list *constraint; 1411 struct sdca_control_range *range; 1412 struct sdca_control *control; 1413 unsigned int channel_mask = 0; 1414 int i, ret; 1415 1416 static_assert(ARRAY_SIZE(channel_list) == SDCA_MAX_CHANNEL_COUNT); 1417 static_assert(sizeof(channel_mask) * BITS_PER_BYTE >= SDCA_MAX_CHANNEL_COUNT); 1418 1419 if (entity->type != SDCA_ENTITY_TYPE_IT) 1420 return 0; 1421 1422 control = sdca_selector_find_control(dev, entity, SDCA_CTL_IT_CLUSTERINDEX); 1423 if (!control) 1424 return -EINVAL; 1425 1426 range = sdca_control_find_range(dev, entity, control, SDCA_CLUSTER_NCOLS, 0); 1427 if (!range) 1428 return -EINVAL; 1429 1430 for (i = 0; i < range->rows; i++) { 1431 int clusterid = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i); 1432 struct sdca_cluster *cluster; 1433 1434 cluster = sdca_id_find_cluster(dev, function, clusterid); 1435 if (!cluster) 1436 return -ENODEV; 1437 1438 channel_mask |= (1 << (cluster->num_channels - 1)); 1439 } 1440 1441 dev_dbg(dev, "%s: set channel constraint mask: %#x\n", 1442 entity->label, channel_mask); 1443 1444 constraint = kzalloc_obj(*constraint); 1445 if (!constraint) 1446 return -ENOMEM; 1447 1448 constraint->count = ARRAY_SIZE(channel_list); 1449 constraint->list = channel_list; 1450 constraint->mask = channel_mask; 1451 1452 ret = snd_pcm_hw_constraint_list(substream->runtime, 0, 1453 SNDRV_PCM_HW_PARAM_CHANNELS, 1454 constraint); 1455 if (ret) { 1456 dev_err(dev, "%s: failed to add constraint: %d\n", entity->label, ret); 1457 kfree(constraint); 1458 return ret; 1459 } 1460 1461 dai->priv = constraint; 1462 1463 return 0; 1464 } 1465 EXPORT_SYMBOL_NS(sdca_asoc_set_constraints, "SND_SOC_SDCA"); 1466 1467 /** 1468 * sdca_asoc_free_constraints - free constraint allocations 1469 * @substream: Pointer to the PCM substream. 1470 * @dai: Pointer to the ASoC DAI. 1471 * 1472 * Typically called from shutdown(). 1473 */ 1474 void sdca_asoc_free_constraints(struct snd_pcm_substream *substream, 1475 struct snd_soc_dai *dai) 1476 { 1477 struct snd_pcm_hw_constraint_list *constraint = dai->priv; 1478 1479 kfree(constraint); 1480 } 1481 EXPORT_SYMBOL_NS(sdca_asoc_free_constraints, "SND_SOC_SDCA"); 1482 1483 /** 1484 * sdca_asoc_get_port - return SoundWire port for a DAI 1485 * @dev: Pointer to the device, used for error messages. 1486 * @regmap: Pointer to the Function register map. 1487 * @function: Pointer to the Function information. 1488 * @dai: Pointer to the ASoC DAI. 1489 * 1490 * Typically called from hw_params(). 1491 * 1492 * Return: Returns a positive port number on success, and a negative error 1493 * code on failure. 1494 */ 1495 int sdca_asoc_get_port(struct device *dev, struct regmap *regmap, 1496 struct sdca_function_data *function, 1497 struct snd_soc_dai *dai) 1498 { 1499 struct sdca_entity *entity = &function->entities[dai->id]; 1500 struct sdca_control_range *range; 1501 unsigned int reg, val; 1502 int sel = -EINVAL; 1503 int i, ret; 1504 1505 switch (entity->type) { 1506 case SDCA_ENTITY_TYPE_IT: 1507 sel = SDCA_CTL_IT_DATAPORT_SELECTOR; 1508 break; 1509 case SDCA_ENTITY_TYPE_OT: 1510 sel = SDCA_CTL_OT_DATAPORT_SELECTOR; 1511 break; 1512 default: 1513 break; 1514 } 1515 1516 if (sel < 0 || !entity->iot.is_dataport) { 1517 dev_err(dev, "%s: port number only available for dataports\n", 1518 entity->label); 1519 return -EINVAL; 1520 } 1521 1522 range = sdca_selector_find_range(dev, entity, sel, SDCA_DATAPORT_SELECTOR_NCOLS, 1523 SDCA_DATAPORT_SELECTOR_NROWS); 1524 if (!range) 1525 return -EINVAL; 1526 1527 reg = SDW_SDCA_CTL(function->desc->adr, entity->id, sel, 0); 1528 1529 ret = regmap_read(regmap, reg, &val); 1530 if (ret) { 1531 dev_err(dev, "%s: failed to read dataport selector: %d\n", 1532 entity->label, ret); 1533 return ret; 1534 } 1535 1536 for (i = 0; i < range->rows; i++) { 1537 static const u8 port_mask = 0xF; 1538 1539 sel = sdca_range(range, val & port_mask, i); 1540 1541 /* 1542 * FIXME: Currently only a single dataport is supported, so 1543 * return the first one found, technically up to 4 dataports 1544 * could be linked, but this is not yet supported. 1545 */ 1546 if (sel != 0xFF) 1547 return sel; 1548 1549 val >>= hweight8(port_mask); 1550 } 1551 1552 dev_err(dev, "%s: no dataport found\n", entity->label); 1553 return -ENODEV; 1554 } 1555 EXPORT_SYMBOL_NS(sdca_asoc_get_port, "SND_SOC_SDCA"); 1556 1557 static int set_cluster(struct device *dev, struct regmap *regmap, 1558 struct sdca_function_data *function, 1559 struct sdca_entity *entity, unsigned int channels) 1560 { 1561 int sel = SDCA_CTL_IT_CLUSTERINDEX; 1562 struct sdca_control_range *range; 1563 int i, ret; 1564 1565 range = sdca_selector_find_range(dev, entity, sel, SDCA_CLUSTER_NCOLS, 0); 1566 if (!range) 1567 return -EINVAL; 1568 1569 for (i = 0; i < range->rows; i++) { 1570 int cluster_id = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i); 1571 struct sdca_cluster *cluster; 1572 1573 cluster = sdca_id_find_cluster(dev, function, cluster_id); 1574 if (!cluster) 1575 return -ENODEV; 1576 1577 if (cluster->num_channels == channels) { 1578 int index = sdca_range(range, SDCA_CLUSTER_BYTEINDEX, i); 1579 unsigned int reg = SDW_SDCA_CTL(function->desc->adr, 1580 entity->id, sel, 0); 1581 1582 ret = regmap_update_bits(regmap, reg, 0xFF, index); 1583 if (ret) { 1584 dev_err(dev, "%s: failed to write cluster index: %d\n", 1585 entity->label, ret); 1586 return ret; 1587 } 1588 1589 dev_dbg(dev, "%s: set cluster to %d (%d channels)\n", 1590 entity->label, index, channels); 1591 1592 return 0; 1593 } 1594 } 1595 1596 dev_err(dev, "%s: no cluster for %d channels\n", entity->label, channels); 1597 return -EINVAL; 1598 } 1599 1600 static int set_clock(struct device *dev, struct regmap *regmap, 1601 struct sdca_function_data *function, 1602 struct sdca_entity *entity, int target_rate) 1603 { 1604 int sel = SDCA_CTL_CS_SAMPLERATEINDEX; 1605 struct sdca_control_range *range; 1606 int i, ret; 1607 1608 range = sdca_selector_find_range(dev, entity, sel, SDCA_SAMPLERATEINDEX_NCOLS, 0); 1609 if (!range) 1610 return -EINVAL; 1611 1612 for (i = 0; i < range->rows; i++) { 1613 unsigned int rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i); 1614 1615 if (rate == target_rate) { 1616 unsigned int index = sdca_range(range, 1617 SDCA_SAMPLERATEINDEX_INDEX, 1618 i); 1619 unsigned int reg = SDW_SDCA_CTL(function->desc->adr, 1620 entity->id, sel, 0); 1621 1622 ret = regmap_update_bits(regmap, reg, 0xFF, index); 1623 if (ret) { 1624 dev_err(dev, "%s: failed to write clock rate: %d\n", 1625 entity->label, ret); 1626 return ret; 1627 } 1628 1629 dev_dbg(dev, "%s: set clock rate to %d (%dHz)\n", 1630 entity->label, index, rate); 1631 1632 return 0; 1633 } 1634 } 1635 1636 dev_err(dev, "%s: no clock rate for %dHz\n", entity->label, target_rate); 1637 return -EINVAL; 1638 } 1639 1640 static int set_usage(struct device *dev, struct regmap *regmap, 1641 struct sdca_function_data *function, 1642 struct sdca_entity *entity, int sel, 1643 int target_rate, int target_width) 1644 { 1645 struct sdca_control_range *range; 1646 int i, ret; 1647 1648 range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0); 1649 if (!range) 1650 return -EINVAL; 1651 1652 for (i = 0; i < range->rows; i++) { 1653 unsigned int rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i); 1654 unsigned int width = sdca_range(range, SDCA_USAGE_SAMPLE_WIDTH, i); 1655 1656 if ((!rate || rate == target_rate) && (!width || width == target_width)) { 1657 unsigned int usage = sdca_range(range, SDCA_USAGE_NUMBER, i); 1658 unsigned int reg = SDW_SDCA_CTL(function->desc->adr, 1659 entity->id, sel, 0); 1660 1661 ret = regmap_update_bits(regmap, reg, 0xFF, usage); 1662 if (ret) { 1663 dev_err(dev, "%s: failed to write usage: %d\n", 1664 entity->label, ret); 1665 return ret; 1666 } 1667 1668 dev_dbg(dev, "%s: set usage to %#x (%dHz, %d bits)\n", 1669 entity->label, usage, target_rate, target_width); 1670 1671 return 0; 1672 } 1673 } 1674 1675 dev_err(dev, "%s: no usage for %dHz, %dbits\n", 1676 entity->label, target_rate, target_width); 1677 return -EINVAL; 1678 } 1679 1680 /** 1681 * sdca_asoc_hw_params - set SDCA channels, sample rate and bit depth 1682 * @dev: Pointer to the device, used for error messages. 1683 * @regmap: Pointer to the Function register map. 1684 * @function: Pointer to the Function information. 1685 * @substream: Pointer to the PCM substream. 1686 * @params: Pointer to the hardware parameters. 1687 * @dai: Pointer to the ASoC DAI. 1688 * 1689 * Typically called from hw_params(). 1690 * 1691 * Return: Returns zero on success, and a negative error code on failure. 1692 */ 1693 int sdca_asoc_hw_params(struct device *dev, struct regmap *regmap, 1694 struct sdca_function_data *function, 1695 struct snd_pcm_substream *substream, 1696 struct snd_pcm_hw_params *params, 1697 struct snd_soc_dai *dai) 1698 { 1699 struct sdca_entity *entity = &function->entities[dai->id]; 1700 int channels = params_channels(params); 1701 int width = params_width(params); 1702 int rate = params_rate(params); 1703 int usage_sel; 1704 int ret; 1705 1706 switch (entity->type) { 1707 case SDCA_ENTITY_TYPE_IT: 1708 ret = set_cluster(dev, regmap, function, entity, channels); 1709 if (ret) 1710 return ret; 1711 1712 usage_sel = SDCA_CTL_IT_USAGE; 1713 break; 1714 case SDCA_ENTITY_TYPE_OT: 1715 usage_sel = SDCA_CTL_OT_USAGE; 1716 break; 1717 default: 1718 dev_err(dev, "%s: hw_params on non-terminal entity\n", entity->label); 1719 return -EINVAL; 1720 } 1721 1722 if (entity->iot.clock) { 1723 ret = set_clock(dev, regmap, function, entity->iot.clock, rate); 1724 if (ret) 1725 return ret; 1726 } 1727 1728 ret = set_usage(dev, regmap, function, entity, usage_sel, rate, width); 1729 if (ret) 1730 return ret; 1731 1732 return 0; 1733 } 1734 EXPORT_SYMBOL_NS(sdca_asoc_hw_params, "SND_SOC_SDCA"); 1735