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