1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // soc-ops.c -- Generic ASoC operations 4 // 5 // Copyright 2005 Wolfson Microelectronics PLC. 6 // Copyright 2005 Openedhand Ltd. 7 // Copyright (C) 2010 Slimlogic Ltd. 8 // Copyright (C) 2010 Texas Instruments Inc. 9 // 10 // Author: Liam Girdwood <lrg@slimlogic.co.uk> 11 // with code, comments and ideas from :- 12 // Richard Purdie <richard@openedhand.com> 13 14 #include <linux/cleanup.h> 15 #include <linux/module.h> 16 #include <linux/moduleparam.h> 17 #include <linux/init.h> 18 #include <linux/pm.h> 19 #include <linux/bitops.h> 20 #include <linux/ctype.h> 21 #include <linux/slab.h> 22 #include <sound/core.h> 23 #include <sound/jack.h> 24 #include <sound/pcm.h> 25 #include <sound/pcm_params.h> 26 #include <sound/soc.h> 27 #include <sound/initval.h> 28 29 /** 30 * snd_soc_info_enum_double - enumerated double mixer info callback 31 * @kcontrol: mixer control 32 * @uinfo: control element information 33 * 34 * Callback to provide information about a double enumerated 35 * mixer control. 36 * 37 * Returns 0 for success. 38 */ 39 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 40 struct snd_ctl_elem_info *uinfo) 41 { 42 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 43 44 return snd_ctl_enum_info(uinfo, e->shift_l == e->shift_r ? 1 : 2, 45 e->items, e->texts); 46 } 47 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 48 49 /** 50 * snd_soc_get_enum_double - enumerated double mixer get callback 51 * @kcontrol: mixer control 52 * @ucontrol: control element information 53 * 54 * Callback to get the value of a double enumerated mixer. 55 * 56 * Returns 0 for success. 57 */ 58 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 59 struct snd_ctl_elem_value *ucontrol) 60 { 61 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 62 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 63 unsigned int val, item; 64 unsigned int reg_val; 65 66 reg_val = snd_soc_component_read(component, e->reg); 67 val = (reg_val >> e->shift_l) & e->mask; 68 item = snd_soc_enum_val_to_item(e, val); 69 ucontrol->value.enumerated.item[0] = item; 70 if (e->shift_l != e->shift_r) { 71 val = (reg_val >> e->shift_r) & e->mask; 72 item = snd_soc_enum_val_to_item(e, val); 73 ucontrol->value.enumerated.item[1] = item; 74 } 75 76 return 0; 77 } 78 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 79 80 /** 81 * snd_soc_put_enum_double - enumerated double mixer put callback 82 * @kcontrol: mixer control 83 * @ucontrol: control element information 84 * 85 * Callback to set the value of a double enumerated mixer. 86 * 87 * Returns 0 for success. 88 */ 89 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 90 struct snd_ctl_elem_value *ucontrol) 91 { 92 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 93 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 94 unsigned int *item = ucontrol->value.enumerated.item; 95 unsigned int val; 96 unsigned int mask; 97 98 if (item[0] >= e->items) 99 return -EINVAL; 100 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 101 mask = e->mask << e->shift_l; 102 if (e->shift_l != e->shift_r) { 103 if (item[1] >= e->items) 104 return -EINVAL; 105 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r; 106 mask |= e->mask << e->shift_r; 107 } 108 109 return snd_soc_component_update_bits(component, e->reg, mask, val); 110 } 111 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 112 113 static int soc_mixer_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_val, 114 unsigned int mask, unsigned int shift, int max, 115 bool sx) 116 { 117 int val = (reg_val >> shift) & mask; 118 119 if (mc->sign_bit) 120 val = sign_extend32(val, mc->sign_bit); 121 122 if (sx) { 123 val -= mc->min; // SX controls intentionally can overflow here 124 val = min_t(unsigned int, val & mask, max); 125 } else { 126 val = clamp(val, mc->min, mc->max); 127 val -= mc->min; 128 } 129 130 if (mc->invert) 131 val = max - val; 132 133 return val; 134 } 135 136 static unsigned int soc_mixer_ctl_to_reg(struct soc_mixer_control *mc, int val, 137 unsigned int mask, unsigned int shift, 138 int max) 139 { 140 unsigned int reg_val; 141 142 if (mc->invert) 143 val = max - val; 144 145 reg_val = val + mc->min; 146 147 return (reg_val & mask) << shift; 148 } 149 150 static int soc_mixer_valid_ctl(struct soc_mixer_control *mc, long val, int max) 151 { 152 if (val < 0) 153 return -EINVAL; 154 155 if (mc->platform_max && val > mc->platform_max) 156 return -EINVAL; 157 158 if (val > max) 159 return -EINVAL; 160 161 return 0; 162 } 163 164 static int soc_mixer_mask(struct soc_mixer_control *mc) 165 { 166 if (mc->sign_bit) 167 return GENMASK(mc->sign_bit, 0); 168 else 169 return GENMASK(fls(mc->max) - 1, 0); 170 } 171 172 static int soc_mixer_sx_mask(struct soc_mixer_control *mc) 173 { 174 // min + max will take us 1-bit over the size of the mask 175 return GENMASK(fls(mc->min + mc->max) - 2, 0); 176 } 177 178 static int soc_info_volsw(struct snd_kcontrol *kcontrol, 179 struct snd_ctl_elem_info *uinfo, 180 struct soc_mixer_control *mc, int max) 181 { 182 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 183 184 if (max == 1) { 185 /* Even two value controls ending in Volume should be integer */ 186 const char *vol_string = strstr(kcontrol->id.name, " Volume"); 187 188 if (!vol_string || strcmp(vol_string, " Volume")) 189 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 190 } 191 192 if (mc->platform_max && mc->platform_max < max) 193 max = mc->platform_max; 194 195 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1; 196 uinfo->value.integer.min = 0; 197 uinfo->value.integer.max = max; 198 199 return 0; 200 } 201 202 static int soc_put_volsw(struct snd_kcontrol *kcontrol, 203 struct snd_ctl_elem_value *ucontrol, 204 struct soc_mixer_control *mc, int mask, int max) 205 { 206 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 207 unsigned int val1, val_mask; 208 unsigned int val2 = 0; 209 bool double_r = false; 210 int ret; 211 212 ret = soc_mixer_valid_ctl(mc, ucontrol->value.integer.value[0], max); 213 if (ret) 214 return ret; 215 216 val1 = soc_mixer_ctl_to_reg(mc, ucontrol->value.integer.value[0], 217 mask, mc->shift, max); 218 val_mask = mask << mc->shift; 219 220 if (snd_soc_volsw_is_stereo(mc)) { 221 ret = soc_mixer_valid_ctl(mc, ucontrol->value.integer.value[1], max); 222 if (ret) 223 return ret; 224 225 if (mc->reg == mc->rreg) { 226 val1 |= soc_mixer_ctl_to_reg(mc, 227 ucontrol->value.integer.value[1], 228 mask, mc->rshift, max); 229 val_mask |= mask << mc->rshift; 230 } else { 231 val2 = soc_mixer_ctl_to_reg(mc, 232 ucontrol->value.integer.value[1], 233 mask, mc->shift, max); 234 double_r = true; 235 } 236 } 237 238 ret = snd_soc_component_update_bits(component, mc->reg, val_mask, val1); 239 if (ret < 0) 240 return ret; 241 242 if (double_r) { 243 int err = snd_soc_component_update_bits(component, mc->rreg, 244 val_mask, val2); 245 /* Don't drop change flag */ 246 if (err) 247 return err; 248 } 249 250 return ret; 251 } 252 253 static int soc_get_volsw(struct snd_kcontrol *kcontrol, 254 struct snd_ctl_elem_value *ucontrol, 255 struct soc_mixer_control *mc, int mask, int max, bool sx) 256 { 257 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 258 unsigned int reg_val; 259 int val; 260 261 reg_val = snd_soc_component_read(component, mc->reg); 262 val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max, sx); 263 264 ucontrol->value.integer.value[0] = val; 265 266 if (snd_soc_volsw_is_stereo(mc)) { 267 if (mc->reg == mc->rreg) { 268 val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->rshift, max, sx); 269 } else { 270 reg_val = snd_soc_component_read(component, mc->rreg); 271 val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max, sx); 272 } 273 274 ucontrol->value.integer.value[1] = val; 275 } 276 277 return 0; 278 } 279 280 /** 281 * snd_soc_info_volsw - single mixer info callback with range. 282 * @kcontrol: mixer control 283 * @uinfo: control element information 284 * 285 * Callback to provide information, with a range, about a single mixer control, 286 * or a double mixer control that spans 2 registers. 287 * 288 * Returns 0 for success. 289 */ 290 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 291 struct snd_ctl_elem_info *uinfo) 292 { 293 struct soc_mixer_control *mc = 294 (struct soc_mixer_control *)kcontrol->private_value; 295 296 return soc_info_volsw(kcontrol, uinfo, mc, mc->max - mc->min); 297 } 298 EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 299 300 /** 301 * snd_soc_info_volsw_sx - Mixer info callback for SX TLV controls 302 * @kcontrol: mixer control 303 * @uinfo: control element information 304 * 305 * Callback to provide information about a single mixer control, or a double 306 * mixer control that spans 2 registers of the SX TLV type. SX TLV controls 307 * have a range that represents both positive and negative values either side 308 * of zero but without a sign bit. min is the minimum register value, max is 309 * the number of steps. 310 * 311 * Returns 0 for success. 312 */ 313 int snd_soc_info_volsw_sx(struct snd_kcontrol *kcontrol, 314 struct snd_ctl_elem_info *uinfo) 315 { 316 struct soc_mixer_control *mc = 317 (struct soc_mixer_control *)kcontrol->private_value; 318 319 return soc_info_volsw(kcontrol, uinfo, mc, mc->max); 320 } 321 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_sx); 322 323 /** 324 * snd_soc_get_volsw - single mixer get callback with range 325 * @kcontrol: mixer control 326 * @ucontrol: control element information 327 * 328 * Callback to get the value, within a range, of a single mixer control, or a 329 * double mixer control that spans 2 registers. 330 * 331 * Returns 0 for success. 332 */ 333 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 334 struct snd_ctl_elem_value *ucontrol) 335 { 336 struct soc_mixer_control *mc = 337 (struct soc_mixer_control *)kcontrol->private_value; 338 unsigned int mask = soc_mixer_mask(mc); 339 340 return soc_get_volsw(kcontrol, ucontrol, mc, mask, mc->max - mc->min, false); 341 } 342 EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 343 344 /** 345 * snd_soc_put_volsw - single mixer put callback with range 346 * @kcontrol: mixer control 347 * @ucontrol: control element information 348 * 349 * Callback to set the value , within a range, of a single mixer control, or 350 * a double mixer control that spans 2 registers. 351 * 352 * Returns 0 for success. 353 */ 354 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 355 struct snd_ctl_elem_value *ucontrol) 356 { 357 struct soc_mixer_control *mc = 358 (struct soc_mixer_control *)kcontrol->private_value; 359 unsigned int mask = soc_mixer_mask(mc); 360 361 return soc_put_volsw(kcontrol, ucontrol, mc, mask, mc->max - mc->min); 362 } 363 EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 364 365 /** 366 * snd_soc_get_volsw_sx - single mixer get callback 367 * @kcontrol: mixer control 368 * @ucontrol: control element information 369 * 370 * Callback to get the value of a single mixer control, or a double mixer 371 * control that spans 2 registers. 372 * 373 * Returns 0 for success. 374 */ 375 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol, 376 struct snd_ctl_elem_value *ucontrol) 377 { 378 struct soc_mixer_control *mc = 379 (struct soc_mixer_control *)kcontrol->private_value; 380 unsigned int mask = soc_mixer_sx_mask(mc); 381 382 return soc_get_volsw(kcontrol, ucontrol, mc, mask, mc->max, true); 383 } 384 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx); 385 386 /** 387 * snd_soc_put_volsw_sx - double mixer set callback 388 * @kcontrol: mixer control 389 * @ucontrol: control element information 390 * 391 * Callback to set the value of a double mixer control that spans 2 registers. 392 * 393 * Returns 0 for success. 394 */ 395 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol, 396 struct snd_ctl_elem_value *ucontrol) 397 { 398 struct soc_mixer_control *mc = 399 (struct soc_mixer_control *)kcontrol->private_value; 400 unsigned int mask = soc_mixer_sx_mask(mc); 401 402 return soc_put_volsw(kcontrol, ucontrol, mc, mask, mc->max); 403 } 404 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx); 405 406 static int snd_soc_clip_to_platform_max(struct snd_kcontrol *kctl) 407 { 408 struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value; 409 struct snd_ctl_elem_value *uctl; 410 int ret; 411 412 if (!mc->platform_max) 413 return 0; 414 415 uctl = kzalloc_obj(*uctl); 416 if (!uctl) 417 return -ENOMEM; 418 419 ret = kctl->get(kctl, uctl); 420 if (ret < 0) 421 goto out; 422 423 if (uctl->value.integer.value[0] > mc->platform_max) 424 uctl->value.integer.value[0] = mc->platform_max; 425 426 if (snd_soc_volsw_is_stereo(mc) && 427 uctl->value.integer.value[1] > mc->platform_max) 428 uctl->value.integer.value[1] = mc->platform_max; 429 430 ret = kctl->put(kctl, uctl); 431 432 out: 433 kfree(uctl); 434 return ret; 435 } 436 437 /** 438 * snd_soc_limit_volume - Set new limit to an existing volume control. 439 * 440 * @card: where to look for the control 441 * @name: Name of the control 442 * @max: new maximum limit 443 * 444 * Return 0 for success, else error. 445 */ 446 int snd_soc_limit_volume(struct snd_soc_card *card, const char *name, int max) 447 { 448 struct snd_kcontrol *kctl; 449 int ret = -EINVAL; 450 451 /* Sanity check for name and max */ 452 if (unlikely(!name || max <= 0)) 453 return -EINVAL; 454 455 kctl = snd_soc_card_get_kcontrol(card, name); 456 if (kctl) { 457 struct soc_mixer_control *mc = 458 (struct soc_mixer_control *)kctl->private_value; 459 460 if (max <= mc->max - mc->min) { 461 mc->platform_max = max; 462 ret = snd_soc_clip_to_platform_max(kctl); 463 } 464 } else { 465 /* Some cards blindly add limits for multiple variants. */ 466 dev_dbg(card->dev, "Volume limit for unknown control '%s'\n", 467 name); 468 } 469 470 return ret; 471 } 472 EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 473 474 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol, 475 struct snd_ctl_elem_info *uinfo) 476 { 477 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 478 struct soc_bytes *params = (void *)kcontrol->private_value; 479 int val_bytes = snd_soc_component_regmap_val_bytes(component); 480 481 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 482 uinfo->count = params->num_regs * val_bytes; 483 484 return 0; 485 } 486 EXPORT_SYMBOL_GPL(snd_soc_bytes_info); 487 488 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol, 489 struct snd_ctl_elem_value *ucontrol) 490 { 491 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 492 struct soc_bytes *params = (void *)kcontrol->private_value; 493 int val_bytes = snd_soc_component_regmap_val_bytes(component); 494 int ret; 495 496 if (component->regmap) 497 ret = regmap_raw_read(component->regmap, params->base, 498 ucontrol->value.bytes.data, 499 params->num_regs * val_bytes); 500 else 501 ret = -EINVAL; 502 503 /* Hide any masked bytes to ensure consistent data reporting */ 504 if (ret == 0 && params->mask) { 505 switch (val_bytes) { 506 case 1: 507 ucontrol->value.bytes.data[0] &= ~params->mask; 508 break; 509 case 2: 510 ((__be16 *)(&ucontrol->value.bytes.data))[0] 511 &= cpu_to_be16(~params->mask); 512 break; 513 case 4: 514 ((__be32 *)(&ucontrol->value.bytes.data))[0] 515 &= cpu_to_be32(~params->mask); 516 break; 517 default: 518 return -EINVAL; 519 } 520 } 521 522 return ret; 523 } 524 EXPORT_SYMBOL_GPL(snd_soc_bytes_get); 525 526 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, 527 struct snd_ctl_elem_value *ucontrol) 528 { 529 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 530 struct soc_bytes *params = (void *)kcontrol->private_value; 531 int val_bytes = snd_soc_component_regmap_val_bytes(component); 532 unsigned int val, mask; 533 int ret, len; 534 535 if (!component->regmap || !params->num_regs) 536 return -EINVAL; 537 538 len = params->num_regs * val_bytes; 539 540 void *data __free(kfree) = kmemdup(ucontrol->value.bytes.data, len, 541 GFP_KERNEL | GFP_DMA); 542 if (!data) 543 return -ENOMEM; 544 545 /* 546 * If we've got a mask then we need to preserve the register 547 * bits. We shouldn't modify the incoming data so take a 548 * copy. 549 */ 550 if (params->mask) { 551 ret = regmap_read(component->regmap, params->base, &val); 552 if (ret != 0) 553 return ret; 554 555 val &= params->mask; 556 557 switch (val_bytes) { 558 case 1: 559 ((u8 *)data)[0] &= ~params->mask; 560 ((u8 *)data)[0] |= val; 561 break; 562 case 2: 563 mask = ~params->mask; 564 ret = regmap_parse_val(component->regmap, &mask, &mask); 565 if (ret != 0) 566 return ret; 567 568 ((u16 *)data)[0] &= mask; 569 570 ret = regmap_parse_val(component->regmap, &val, &val); 571 if (ret != 0) 572 return ret; 573 574 ((u16 *)data)[0] |= val; 575 break; 576 case 4: 577 mask = ~params->mask; 578 ret = regmap_parse_val(component->regmap, &mask, &mask); 579 if (ret != 0) 580 return ret; 581 582 ((u32 *)data)[0] &= mask; 583 584 ret = regmap_parse_val(component->regmap, &val, &val); 585 if (ret != 0) 586 return ret; 587 588 ((u32 *)data)[0] |= val; 589 break; 590 default: 591 return -EINVAL; 592 } 593 } 594 595 return regmap_raw_write(component->regmap, params->base, data, len); 596 } 597 EXPORT_SYMBOL_GPL(snd_soc_bytes_put); 598 599 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol, 600 struct snd_ctl_elem_info *ucontrol) 601 { 602 struct soc_bytes_ext *params = (void *)kcontrol->private_value; 603 604 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES; 605 ucontrol->count = params->max; 606 607 return 0; 608 } 609 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext); 610 611 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag, 612 unsigned int size, unsigned int __user *tlv) 613 { 614 struct soc_bytes_ext *params = (void *)kcontrol->private_value; 615 unsigned int count = size < params->max ? size : params->max; 616 int ret = -ENXIO; 617 618 switch (op_flag) { 619 case SNDRV_CTL_TLV_OP_READ: 620 if (params->get) 621 ret = params->get(kcontrol, tlv, count); 622 break; 623 case SNDRV_CTL_TLV_OP_WRITE: 624 if (params->put) 625 ret = params->put(kcontrol, tlv, count); 626 break; 627 } 628 629 return ret; 630 } 631 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback); 632 633 /** 634 * snd_soc_info_xr_sx - signed multi register info callback 635 * @kcontrol: mreg control 636 * @uinfo: control element information 637 * 638 * Callback to provide information of a control that can span multiple 639 * codec registers which together forms a single signed value. Note 640 * that unlike the non-xr variant of sx controls these may or may not 641 * include the sign bit, depending on nbits, and there is no shift. 642 * 643 * Returns 0 for success. 644 */ 645 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol, 646 struct snd_ctl_elem_info *uinfo) 647 { 648 struct soc_mreg_control *mc = 649 (struct soc_mreg_control *)kcontrol->private_value; 650 651 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 652 uinfo->count = 1; 653 uinfo->value.integer.min = mc->min; 654 uinfo->value.integer.max = mc->max; 655 656 return 0; 657 } 658 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx); 659 660 /** 661 * snd_soc_get_xr_sx - signed multi register get callback 662 * @kcontrol: mreg control 663 * @ucontrol: control element information 664 * 665 * Callback to get the value of a control that can span multiple codec 666 * registers which together forms a single signed value. The control 667 * supports specifying total no of bits used to allow for bitfields 668 * across the multiple codec registers. Note that unlike the non-xr 669 * variant of sx controls these may or may not include the sign bit, 670 * depending on nbits, and there is no shift. 671 * 672 * Returns 0 for success. 673 */ 674 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol, 675 struct snd_ctl_elem_value *ucontrol) 676 { 677 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 678 struct soc_mreg_control *mc = 679 (struct soc_mreg_control *)kcontrol->private_value; 680 int val_bytes = snd_soc_component_regmap_val_bytes(component); 681 unsigned int regbase = mc->regbase; 682 unsigned int regcount = mc->regcount; 683 unsigned int regwshift = val_bytes * BITS_PER_BYTE; 684 unsigned int regwmask = GENMASK(regwshift - 1, 0); 685 unsigned long mask = GENMASK(mc->nbits - 1, 0); 686 long val = 0; 687 unsigned int i; 688 689 for (i = 0; i < regcount; i++) { 690 unsigned int regval = snd_soc_component_read(component, regbase + i); 691 692 val |= (regval & regwmask) << (regwshift * (regcount - i - 1)); 693 } 694 val &= mask; 695 if (mc->min < 0 && val > mc->max) 696 val |= ~mask; 697 if (mc->invert) 698 val = mc->max - val; 699 ucontrol->value.integer.value[0] = val; 700 701 return 0; 702 } 703 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx); 704 705 /** 706 * snd_soc_put_xr_sx - signed multi register get callback 707 * @kcontrol: mreg control 708 * @ucontrol: control element information 709 * 710 * Callback to set the value of a control that can span multiple codec 711 * registers which together forms a single signed value. The control 712 * supports specifying total no of bits used to allow for bitfields 713 * across the multiple codec registers. Note that unlike the non-xr 714 * variant of sx controls these may or may not include the sign bit, 715 * depending on nbits, and there is no shift. 716 * 717 * Returns 0 for success. 718 */ 719 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol, 720 struct snd_ctl_elem_value *ucontrol) 721 { 722 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 723 struct soc_mreg_control *mc = 724 (struct soc_mreg_control *)kcontrol->private_value; 725 int val_bytes = snd_soc_component_regmap_val_bytes(component); 726 unsigned int regbase = mc->regbase; 727 unsigned int regcount = mc->regcount; 728 unsigned int regwshift = val_bytes * BITS_PER_BYTE; 729 unsigned int regwmask = GENMASK(regwshift - 1, 0); 730 unsigned long mask = GENMASK(mc->nbits - 1, 0); 731 long val = ucontrol->value.integer.value[0]; 732 int ret = 0; 733 unsigned int i; 734 735 if (val < mc->min || val > mc->max) 736 return -EINVAL; 737 if (mc->invert) 738 val = mc->max - val; 739 val &= mask; 740 for (i = 0; i < regcount; i++) { 741 unsigned int regval = (val >> (regwshift * (regcount - i - 1))) & 742 regwmask; 743 unsigned int regmask = (mask >> (regwshift * (regcount - i - 1))) & 744 regwmask; 745 int err = snd_soc_component_update_bits(component, regbase + i, 746 regmask, regval); 747 748 if (err < 0) 749 return err; 750 if (err > 0) 751 ret = err; 752 } 753 754 return ret; 755 } 756 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx); 757 758 /** 759 * snd_soc_get_strobe - strobe get callback 760 * @kcontrol: mixer control 761 * @ucontrol: control element information 762 * 763 * Callback get the value of a strobe mixer control. 764 * 765 * Returns 0 for success. 766 */ 767 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol, 768 struct snd_ctl_elem_value *ucontrol) 769 { 770 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 771 struct soc_mixer_control *mc = 772 (struct soc_mixer_control *)kcontrol->private_value; 773 unsigned int invert = mc->invert != 0; 774 unsigned int mask = BIT(mc->shift); 775 unsigned int val; 776 777 val = snd_soc_component_read(component, mc->reg); 778 val &= mask; 779 780 if (mc->shift != 0 && val != 0) 781 val = val >> mc->shift; 782 783 ucontrol->value.enumerated.item[0] = val ^ invert; 784 785 return 0; 786 } 787 EXPORT_SYMBOL_GPL(snd_soc_get_strobe); 788 789 /** 790 * snd_soc_put_strobe - strobe put callback 791 * @kcontrol: mixer control 792 * @ucontrol: control element information 793 * 794 * Callback strobe a register bit to high then low (or the inverse) 795 * in one pass of a single mixer enum control. 796 * 797 * Returns 1 for success. 798 */ 799 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol, 800 struct snd_ctl_elem_value *ucontrol) 801 { 802 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 803 struct soc_mixer_control *mc = 804 (struct soc_mixer_control *)kcontrol->private_value; 805 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0; 806 unsigned int invert = mc->invert != 0; 807 unsigned int mask = BIT(mc->shift); 808 unsigned int val1 = (strobe ^ invert) ? mask : 0; 809 unsigned int val2 = (strobe ^ invert) ? 0 : mask; 810 int ret; 811 812 ret = snd_soc_component_update_bits(component, mc->reg, mask, val1); 813 if (ret < 0) 814 return ret; 815 816 return snd_soc_component_update_bits(component, mc->reg, mask, val2); 817 } 818 EXPORT_SYMBOL_GPL(snd_soc_put_strobe); 819