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