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 } 465 466 return ret; 467 } 468 EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 469 470 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol, 471 struct snd_ctl_elem_info *uinfo) 472 { 473 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 474 struct soc_bytes *params = (void *)kcontrol->private_value; 475 int val_bytes = snd_soc_component_regmap_val_bytes(component); 476 477 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 478 uinfo->count = params->num_regs * val_bytes; 479 480 return 0; 481 } 482 EXPORT_SYMBOL_GPL(snd_soc_bytes_info); 483 484 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol, 485 struct snd_ctl_elem_value *ucontrol) 486 { 487 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 488 struct soc_bytes *params = (void *)kcontrol->private_value; 489 int val_bytes = snd_soc_component_regmap_val_bytes(component); 490 int ret; 491 492 if (component->regmap) 493 ret = regmap_raw_read(component->regmap, params->base, 494 ucontrol->value.bytes.data, 495 params->num_regs * val_bytes); 496 else 497 ret = -EINVAL; 498 499 /* Hide any masked bytes to ensure consistent data reporting */ 500 if (ret == 0 && params->mask) { 501 switch (val_bytes) { 502 case 1: 503 ucontrol->value.bytes.data[0] &= ~params->mask; 504 break; 505 case 2: 506 ((__be16 *)(&ucontrol->value.bytes.data))[0] 507 &= cpu_to_be16(~params->mask); 508 break; 509 case 4: 510 ((__be32 *)(&ucontrol->value.bytes.data))[0] 511 &= cpu_to_be32(~params->mask); 512 break; 513 default: 514 return -EINVAL; 515 } 516 } 517 518 return ret; 519 } 520 EXPORT_SYMBOL_GPL(snd_soc_bytes_get); 521 522 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, 523 struct snd_ctl_elem_value *ucontrol) 524 { 525 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 526 struct soc_bytes *params = (void *)kcontrol->private_value; 527 int val_bytes = snd_soc_component_regmap_val_bytes(component); 528 unsigned int val, mask; 529 int ret, len; 530 531 if (!component->regmap || !params->num_regs) 532 return -EINVAL; 533 534 len = params->num_regs * val_bytes; 535 536 void *data __free(kfree) = kmemdup(ucontrol->value.bytes.data, len, 537 GFP_KERNEL | GFP_DMA); 538 if (!data) 539 return -ENOMEM; 540 541 /* 542 * If we've got a mask then we need to preserve the register 543 * bits. We shouldn't modify the incoming data so take a 544 * copy. 545 */ 546 if (params->mask) { 547 ret = regmap_read(component->regmap, params->base, &val); 548 if (ret != 0) 549 return ret; 550 551 val &= params->mask; 552 553 switch (val_bytes) { 554 case 1: 555 ((u8 *)data)[0] &= ~params->mask; 556 ((u8 *)data)[0] |= val; 557 break; 558 case 2: 559 mask = ~params->mask; 560 ret = regmap_parse_val(component->regmap, &mask, &mask); 561 if (ret != 0) 562 return ret; 563 564 ((u16 *)data)[0] &= mask; 565 566 ret = regmap_parse_val(component->regmap, &val, &val); 567 if (ret != 0) 568 return ret; 569 570 ((u16 *)data)[0] |= val; 571 break; 572 case 4: 573 mask = ~params->mask; 574 ret = regmap_parse_val(component->regmap, &mask, &mask); 575 if (ret != 0) 576 return ret; 577 578 ((u32 *)data)[0] &= mask; 579 580 ret = regmap_parse_val(component->regmap, &val, &val); 581 if (ret != 0) 582 return ret; 583 584 ((u32 *)data)[0] |= val; 585 break; 586 default: 587 return -EINVAL; 588 } 589 } 590 591 return regmap_raw_write(component->regmap, params->base, data, len); 592 } 593 EXPORT_SYMBOL_GPL(snd_soc_bytes_put); 594 595 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol, 596 struct snd_ctl_elem_info *ucontrol) 597 { 598 struct soc_bytes_ext *params = (void *)kcontrol->private_value; 599 600 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES; 601 ucontrol->count = params->max; 602 603 return 0; 604 } 605 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext); 606 607 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag, 608 unsigned int size, unsigned int __user *tlv) 609 { 610 struct soc_bytes_ext *params = (void *)kcontrol->private_value; 611 unsigned int count = size < params->max ? size : params->max; 612 int ret = -ENXIO; 613 614 switch (op_flag) { 615 case SNDRV_CTL_TLV_OP_READ: 616 if (params->get) 617 ret = params->get(kcontrol, tlv, count); 618 break; 619 case SNDRV_CTL_TLV_OP_WRITE: 620 if (params->put) 621 ret = params->put(kcontrol, tlv, count); 622 break; 623 } 624 625 return ret; 626 } 627 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback); 628 629 /** 630 * snd_soc_info_xr_sx - signed multi register info callback 631 * @kcontrol: mreg control 632 * @uinfo: control element information 633 * 634 * Callback to provide information of a control that can span multiple 635 * codec registers which together forms a single signed value. Note 636 * that unlike the non-xr variant of sx controls these may or may not 637 * include the sign bit, depending on nbits, and there is no shift. 638 * 639 * Returns 0 for success. 640 */ 641 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol, 642 struct snd_ctl_elem_info *uinfo) 643 { 644 struct soc_mreg_control *mc = 645 (struct soc_mreg_control *)kcontrol->private_value; 646 647 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 648 uinfo->count = 1; 649 uinfo->value.integer.min = mc->min; 650 uinfo->value.integer.max = mc->max; 651 652 return 0; 653 } 654 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx); 655 656 /** 657 * snd_soc_get_xr_sx - signed multi register get callback 658 * @kcontrol: mreg control 659 * @ucontrol: control element information 660 * 661 * Callback to get the value of a control that can span multiple codec 662 * registers which together forms a single signed value. The control 663 * supports specifying total no of bits used to allow for bitfields 664 * across the multiple codec registers. Note that unlike the non-xr 665 * variant of sx controls these may or may not include the sign bit, 666 * depending on nbits, and there is no shift. 667 * 668 * Returns 0 for success. 669 */ 670 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol, 671 struct snd_ctl_elem_value *ucontrol) 672 { 673 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 674 struct soc_mreg_control *mc = 675 (struct soc_mreg_control *)kcontrol->private_value; 676 int val_bytes = snd_soc_component_regmap_val_bytes(component); 677 unsigned int regbase = mc->regbase; 678 unsigned int regcount = mc->regcount; 679 unsigned int regwshift = val_bytes * BITS_PER_BYTE; 680 unsigned int regwmask = GENMASK(regwshift - 1, 0); 681 unsigned long mask = GENMASK(mc->nbits - 1, 0); 682 long val = 0; 683 unsigned int i; 684 685 for (i = 0; i < regcount; i++) { 686 unsigned int regval = snd_soc_component_read(component, regbase + i); 687 688 val |= (regval & regwmask) << (regwshift * (regcount - i - 1)); 689 } 690 val &= mask; 691 if (mc->min < 0 && val > mc->max) 692 val |= ~mask; 693 if (mc->invert) 694 val = mc->max - val; 695 ucontrol->value.integer.value[0] = val; 696 697 return 0; 698 } 699 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx); 700 701 /** 702 * snd_soc_put_xr_sx - signed multi register get callback 703 * @kcontrol: mreg control 704 * @ucontrol: control element information 705 * 706 * Callback to set the value of a control that can span multiple codec 707 * registers which together forms a single signed value. The control 708 * supports specifying total no of bits used to allow for bitfields 709 * across the multiple codec registers. Note that unlike the non-xr 710 * variant of sx controls these may or may not include the sign bit, 711 * depending on nbits, and there is no shift. 712 * 713 * Returns 0 for success. 714 */ 715 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol, 716 struct snd_ctl_elem_value *ucontrol) 717 { 718 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 719 struct soc_mreg_control *mc = 720 (struct soc_mreg_control *)kcontrol->private_value; 721 int val_bytes = snd_soc_component_regmap_val_bytes(component); 722 unsigned int regbase = mc->regbase; 723 unsigned int regcount = mc->regcount; 724 unsigned int regwshift = val_bytes * BITS_PER_BYTE; 725 unsigned int regwmask = GENMASK(regwshift - 1, 0); 726 unsigned long mask = GENMASK(mc->nbits - 1, 0); 727 long val = ucontrol->value.integer.value[0]; 728 int ret = 0; 729 unsigned int i; 730 731 if (val < mc->min || val > mc->max) 732 return -EINVAL; 733 if (mc->invert) 734 val = mc->max - val; 735 val &= mask; 736 for (i = 0; i < regcount; i++) { 737 unsigned int regval = (val >> (regwshift * (regcount - i - 1))) & 738 regwmask; 739 unsigned int regmask = (mask >> (regwshift * (regcount - i - 1))) & 740 regwmask; 741 int err = snd_soc_component_update_bits(component, regbase + i, 742 regmask, regval); 743 744 if (err < 0) 745 return err; 746 if (err > 0) 747 ret = err; 748 } 749 750 return ret; 751 } 752 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx); 753 754 /** 755 * snd_soc_get_strobe - strobe get callback 756 * @kcontrol: mixer control 757 * @ucontrol: control element information 758 * 759 * Callback get the value of a strobe mixer control. 760 * 761 * Returns 0 for success. 762 */ 763 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol, 764 struct snd_ctl_elem_value *ucontrol) 765 { 766 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 767 struct soc_mixer_control *mc = 768 (struct soc_mixer_control *)kcontrol->private_value; 769 unsigned int invert = mc->invert != 0; 770 unsigned int mask = BIT(mc->shift); 771 unsigned int val; 772 773 val = snd_soc_component_read(component, mc->reg); 774 val &= mask; 775 776 if (mc->shift != 0 && val != 0) 777 val = val >> mc->shift; 778 779 ucontrol->value.enumerated.item[0] = val ^ invert; 780 781 return 0; 782 } 783 EXPORT_SYMBOL_GPL(snd_soc_get_strobe); 784 785 /** 786 * snd_soc_put_strobe - strobe put callback 787 * @kcontrol: mixer control 788 * @ucontrol: control element information 789 * 790 * Callback strobe a register bit to high then low (or the inverse) 791 * in one pass of a single mixer enum control. 792 * 793 * Returns 1 for success. 794 */ 795 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol, 796 struct snd_ctl_elem_value *ucontrol) 797 { 798 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 799 struct soc_mixer_control *mc = 800 (struct soc_mixer_control *)kcontrol->private_value; 801 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0; 802 unsigned int invert = mc->invert != 0; 803 unsigned int mask = BIT(mc->shift); 804 unsigned int val1 = (strobe ^ invert) ? mask : 0; 805 unsigned int val2 = (strobe ^ invert) ? 0 : mask; 806 int ret; 807 808 ret = snd_soc_component_update_bits(component, mc->reg, mask, val1); 809 if (ret < 0) 810 return ret; 811 812 return snd_soc_component_update_bits(component, mc->reg, mask, val2); 813 } 814 EXPORT_SYMBOL_GPL(snd_soc_put_strobe); 815