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