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