Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // soc-ops.c  --  Generic ASoC operations
0004 //
0005 // Copyright 2005 Wolfson Microelectronics PLC.
0006 // Copyright 2005 Openedhand Ltd.
0007 // Copyright (C) 2010 Slimlogic Ltd.
0008 // Copyright (C) 2010 Texas Instruments Inc.
0009 //
0010 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
0011 //         with code, comments and ideas from :-
0012 //         Richard Purdie <richard@openedhand.com>
0013 
0014 #include <linux/module.h>
0015 #include <linux/moduleparam.h>
0016 #include <linux/init.h>
0017 #include <linux/pm.h>
0018 #include <linux/bitops.h>
0019 #include <linux/ctype.h>
0020 #include <linux/slab.h>
0021 #include <sound/core.h>
0022 #include <sound/jack.h>
0023 #include <sound/pcm.h>
0024 #include <sound/pcm_params.h>
0025 #include <sound/soc.h>
0026 #include <sound/soc-dpcm.h>
0027 #include <sound/initval.h>
0028 
0029 /**
0030  * snd_soc_info_enum_double - enumerated double mixer info callback
0031  * @kcontrol: mixer control
0032  * @uinfo: control element information
0033  *
0034  * Callback to provide information about a double enumerated
0035  * mixer control.
0036  *
0037  * Returns 0 for success.
0038  */
0039 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
0040     struct snd_ctl_elem_info *uinfo)
0041 {
0042     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0043 
0044     return snd_ctl_enum_info(uinfo, e->shift_l == e->shift_r ? 1 : 2,
0045                  e->items, e->texts);
0046 }
0047 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
0048 
0049 /**
0050  * snd_soc_get_enum_double - enumerated double mixer get callback
0051  * @kcontrol: mixer control
0052  * @ucontrol: control element information
0053  *
0054  * Callback to get the value of a double enumerated mixer.
0055  *
0056  * Returns 0 for success.
0057  */
0058 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
0059     struct snd_ctl_elem_value *ucontrol)
0060 {
0061     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0062     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0063     unsigned int val, item;
0064     unsigned int reg_val;
0065 
0066     reg_val = snd_soc_component_read(component, e->reg);
0067     val = (reg_val >> e->shift_l) & e->mask;
0068     item = snd_soc_enum_val_to_item(e, val);
0069     ucontrol->value.enumerated.item[0] = item;
0070     if (e->shift_l != e->shift_r) {
0071         val = (reg_val >> e->shift_r) & e->mask;
0072         item = snd_soc_enum_val_to_item(e, val);
0073         ucontrol->value.enumerated.item[1] = item;
0074     }
0075 
0076     return 0;
0077 }
0078 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
0079 
0080 /**
0081  * snd_soc_put_enum_double - enumerated double mixer put callback
0082  * @kcontrol: mixer control
0083  * @ucontrol: control element information
0084  *
0085  * Callback to set the value of a double enumerated mixer.
0086  *
0087  * Returns 0 for success.
0088  */
0089 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
0090     struct snd_ctl_elem_value *ucontrol)
0091 {
0092     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0093     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0094     unsigned int *item = ucontrol->value.enumerated.item;
0095     unsigned int val;
0096     unsigned int mask;
0097 
0098     if (item[0] >= e->items)
0099         return -EINVAL;
0100     val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
0101     mask = e->mask << e->shift_l;
0102     if (e->shift_l != e->shift_r) {
0103         if (item[1] >= e->items)
0104             return -EINVAL;
0105         val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
0106         mask |= e->mask << e->shift_r;
0107     }
0108 
0109     return snd_soc_component_update_bits(component, e->reg, mask, val);
0110 }
0111 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
0112 
0113 /**
0114  * snd_soc_read_signed - Read a codec register and interpret as signed value
0115  * @component: component
0116  * @reg: Register to read
0117  * @mask: Mask to use after shifting the register value
0118  * @shift: Right shift of register value
0119  * @sign_bit: Bit that describes if a number is negative or not.
0120  * @signed_val: Pointer to where the read value should be stored
0121  *
0122  * This functions reads a codec register. The register value is shifted right
0123  * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
0124  * the given registervalue into a signed integer if sign_bit is non-zero.
0125  *
0126  * Returns 0 on sucess, otherwise an error value
0127  */
0128 static int snd_soc_read_signed(struct snd_soc_component *component,
0129     unsigned int reg, unsigned int mask, unsigned int shift,
0130     unsigned int sign_bit, int *signed_val)
0131 {
0132     int ret;
0133     unsigned int val;
0134 
0135     val = snd_soc_component_read(component, reg);
0136     val = (val >> shift) & mask;
0137 
0138     if (!sign_bit) {
0139         *signed_val = val;
0140         return 0;
0141     }
0142 
0143     /* non-negative number */
0144     if (!(val & BIT(sign_bit))) {
0145         *signed_val = val;
0146         return 0;
0147     }
0148 
0149     ret = val;
0150 
0151     /*
0152      * The register most probably does not contain a full-sized int.
0153      * Instead we have an arbitrary number of bits in a signed
0154      * representation which has to be translated into a full-sized int.
0155      * This is done by filling up all bits above the sign-bit.
0156      */
0157     ret |= ~((int)(BIT(sign_bit) - 1));
0158 
0159     *signed_val = ret;
0160 
0161     return 0;
0162 }
0163 
0164 /**
0165  * snd_soc_info_volsw - single mixer info callback
0166  * @kcontrol: mixer control
0167  * @uinfo: control element information
0168  *
0169  * Callback to provide information about a single mixer control, or a double
0170  * mixer control that spans 2 registers.
0171  *
0172  * Returns 0 for success.
0173  */
0174 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
0175     struct snd_ctl_elem_info *uinfo)
0176 {
0177     struct soc_mixer_control *mc =
0178         (struct soc_mixer_control *)kcontrol->private_value;
0179     const char *vol_string = NULL;
0180     int max;
0181 
0182     max = uinfo->value.integer.max = mc->max - mc->min;
0183     if (mc->platform_max && mc->platform_max < max)
0184         max = mc->platform_max;
0185 
0186     if (max == 1) {
0187         /* Even two value controls ending in Volume should always be integer */
0188         vol_string = strstr(kcontrol->id.name, " Volume");
0189         if (vol_string && !strcmp(vol_string, " Volume"))
0190             uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0191         else
0192             uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
0193     } else {
0194         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0195     }
0196 
0197     uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
0198     uinfo->value.integer.min = 0;
0199     uinfo->value.integer.max = max;
0200 
0201     return 0;
0202 }
0203 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
0204 
0205 /**
0206  * snd_soc_info_volsw_sx - Mixer info callback for SX TLV controls
0207  * @kcontrol: mixer control
0208  * @uinfo: control element information
0209  *
0210  * Callback to provide information about a single mixer control, or a double
0211  * mixer control that spans 2 registers of the SX TLV type. SX TLV controls
0212  * have a range that represents both positive and negative values either side
0213  * of zero but without a sign bit. min is the minimum register value, max is
0214  * the number of steps.
0215  *
0216  * Returns 0 for success.
0217  */
0218 int snd_soc_info_volsw_sx(struct snd_kcontrol *kcontrol,
0219               struct snd_ctl_elem_info *uinfo)
0220 {
0221     struct soc_mixer_control *mc =
0222         (struct soc_mixer_control *)kcontrol->private_value;
0223     int max;
0224 
0225     if (mc->platform_max)
0226         max = mc->platform_max;
0227     else
0228         max = mc->max;
0229 
0230     if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
0231         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
0232     else
0233         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0234 
0235     uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
0236     uinfo->value.integer.min = 0;
0237     uinfo->value.integer.max = max;
0238 
0239     return 0;
0240 }
0241 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_sx);
0242 
0243 /**
0244  * snd_soc_get_volsw - single mixer get callback
0245  * @kcontrol: mixer control
0246  * @ucontrol: control element information
0247  *
0248  * Callback to get the value of a single mixer control, or a double mixer
0249  * control that spans 2 registers.
0250  *
0251  * Returns 0 for success.
0252  */
0253 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
0254     struct snd_ctl_elem_value *ucontrol)
0255 {
0256     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0257     struct soc_mixer_control *mc =
0258         (struct soc_mixer_control *)kcontrol->private_value;
0259     unsigned int reg = mc->reg;
0260     unsigned int reg2 = mc->rreg;
0261     unsigned int shift = mc->shift;
0262     unsigned int rshift = mc->rshift;
0263     int max = mc->max;
0264     int min = mc->min;
0265     int sign_bit = mc->sign_bit;
0266     unsigned int mask = (1 << fls(max)) - 1;
0267     unsigned int invert = mc->invert;
0268     int val;
0269     int ret;
0270 
0271     if (sign_bit)
0272         mask = BIT(sign_bit + 1) - 1;
0273 
0274     ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
0275     if (ret)
0276         return ret;
0277 
0278     ucontrol->value.integer.value[0] = val - min;
0279     if (invert)
0280         ucontrol->value.integer.value[0] =
0281             max - ucontrol->value.integer.value[0];
0282 
0283     if (snd_soc_volsw_is_stereo(mc)) {
0284         if (reg == reg2)
0285             ret = snd_soc_read_signed(component, reg, mask, rshift,
0286                 sign_bit, &val);
0287         else
0288             ret = snd_soc_read_signed(component, reg2, mask, shift,
0289                 sign_bit, &val);
0290         if (ret)
0291             return ret;
0292 
0293         ucontrol->value.integer.value[1] = val - min;
0294         if (invert)
0295             ucontrol->value.integer.value[1] =
0296                 max - ucontrol->value.integer.value[1];
0297     }
0298 
0299     return 0;
0300 }
0301 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
0302 
0303 /**
0304  * snd_soc_put_volsw - single mixer put callback
0305  * @kcontrol: mixer control
0306  * @ucontrol: control element information
0307  *
0308  * Callback to set the value of a single mixer control, or a double mixer
0309  * control that spans 2 registers.
0310  *
0311  * Returns 0 for success.
0312  */
0313 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
0314     struct snd_ctl_elem_value *ucontrol)
0315 {
0316     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0317     struct soc_mixer_control *mc =
0318         (struct soc_mixer_control *)kcontrol->private_value;
0319     unsigned int reg = mc->reg;
0320     unsigned int reg2 = mc->rreg;
0321     unsigned int shift = mc->shift;
0322     unsigned int rshift = mc->rshift;
0323     int max = mc->max;
0324     int min = mc->min;
0325     unsigned int sign_bit = mc->sign_bit;
0326     unsigned int mask = (1 << fls(max)) - 1;
0327     unsigned int invert = mc->invert;
0328     int err, ret;
0329     bool type_2r = false;
0330     unsigned int val2 = 0;
0331     unsigned int val, val_mask;
0332 
0333     if (sign_bit)
0334         mask = BIT(sign_bit + 1) - 1;
0335 
0336     if (ucontrol->value.integer.value[0] < 0)
0337         return -EINVAL;
0338     val = ucontrol->value.integer.value[0];
0339     if (mc->platform_max && ((int)val + min) > mc->platform_max)
0340         return -EINVAL;
0341     if (val > max - min)
0342         return -EINVAL;
0343     val = (val + min) & mask;
0344     if (invert)
0345         val = max - val;
0346     val_mask = mask << shift;
0347     val = val << shift;
0348     if (snd_soc_volsw_is_stereo(mc)) {
0349         if (ucontrol->value.integer.value[1] < 0)
0350             return -EINVAL;
0351         val2 = ucontrol->value.integer.value[1];
0352         if (mc->platform_max && ((int)val2 + min) > mc->platform_max)
0353             return -EINVAL;
0354         if (val2 > max - min)
0355             return -EINVAL;
0356         val2 = (val2 + min) & mask;
0357         if (invert)
0358             val2 = max - val2;
0359         if (reg == reg2) {
0360             val_mask |= mask << rshift;
0361             val |= val2 << rshift;
0362         } else {
0363             val2 = val2 << shift;
0364             type_2r = true;
0365         }
0366     }
0367     err = snd_soc_component_update_bits(component, reg, val_mask, val);
0368     if (err < 0)
0369         return err;
0370     ret = err;
0371 
0372     if (type_2r) {
0373         err = snd_soc_component_update_bits(component, reg2, val_mask,
0374                             val2);
0375         /* Don't discard any error code or drop change flag */
0376         if (ret == 0 || err < 0) {
0377             ret = err;
0378         }
0379     }
0380 
0381     return ret;
0382 }
0383 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
0384 
0385 /**
0386  * snd_soc_get_volsw_sx - single mixer get callback
0387  * @kcontrol: mixer control
0388  * @ucontrol: control element information
0389  *
0390  * Callback to get the value of a single mixer control, or a double mixer
0391  * control that spans 2 registers.
0392  *
0393  * Returns 0 for success.
0394  */
0395 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
0396               struct snd_ctl_elem_value *ucontrol)
0397 {
0398     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0399     struct soc_mixer_control *mc =
0400         (struct soc_mixer_control *)kcontrol->private_value;
0401     unsigned int reg = mc->reg;
0402     unsigned int reg2 = mc->rreg;
0403     unsigned int shift = mc->shift;
0404     unsigned int rshift = mc->rshift;
0405     int max = mc->max;
0406     int min = mc->min;
0407     unsigned int mask = (1U << (fls(min + max) - 1)) - 1;
0408     unsigned int val;
0409 
0410     val = snd_soc_component_read(component, reg);
0411     ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
0412 
0413     if (snd_soc_volsw_is_stereo(mc)) {
0414         val = snd_soc_component_read(component, reg2);
0415         val = ((val >> rshift) - min) & mask;
0416         ucontrol->value.integer.value[1] = val;
0417     }
0418 
0419     return 0;
0420 }
0421 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
0422 
0423 /**
0424  * snd_soc_put_volsw_sx - double mixer set callback
0425  * @kcontrol: mixer control
0426  * @ucontrol: control element information
0427  *
0428  * Callback to set the value of a double mixer control that spans 2 registers.
0429  *
0430  * Returns 0 for success.
0431  */
0432 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
0433              struct snd_ctl_elem_value *ucontrol)
0434 {
0435     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0436     struct soc_mixer_control *mc =
0437         (struct soc_mixer_control *)kcontrol->private_value;
0438 
0439     unsigned int reg = mc->reg;
0440     unsigned int reg2 = mc->rreg;
0441     unsigned int shift = mc->shift;
0442     unsigned int rshift = mc->rshift;
0443     int max = mc->max;
0444     int min = mc->min;
0445     unsigned int mask = (1U << (fls(min + max) - 1)) - 1;
0446     int err = 0;
0447     int ret;
0448     unsigned int val, val_mask;
0449 
0450     if (ucontrol->value.integer.value[0] < 0)
0451         return -EINVAL;
0452     val = ucontrol->value.integer.value[0];
0453     if (mc->platform_max && val > mc->platform_max)
0454         return -EINVAL;
0455     if (val > max - min)
0456         return -EINVAL;
0457     val_mask = mask << shift;
0458     val = (val + min) & mask;
0459     val = val << shift;
0460 
0461     err = snd_soc_component_update_bits(component, reg, val_mask, val);
0462     if (err < 0)
0463         return err;
0464     ret = err;
0465 
0466     if (snd_soc_volsw_is_stereo(mc)) {
0467         unsigned int val2;
0468 
0469         val_mask = mask << rshift;
0470         val2 = (ucontrol->value.integer.value[1] + min) & mask;
0471         val2 = val2 << rshift;
0472 
0473         err = snd_soc_component_update_bits(component, reg2, val_mask,
0474             val2);
0475 
0476         /* Don't discard any error code or drop change flag */
0477         if (ret == 0 || err < 0) {
0478             ret = err;
0479         }
0480     }
0481     return ret;
0482 }
0483 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
0484 
0485 /**
0486  * snd_soc_info_volsw_range - single mixer info callback with range.
0487  * @kcontrol: mixer control
0488  * @uinfo: control element information
0489  *
0490  * Callback to provide information, within a range, about a single
0491  * mixer control.
0492  *
0493  * returns 0 for success.
0494  */
0495 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
0496     struct snd_ctl_elem_info *uinfo)
0497 {
0498     struct soc_mixer_control *mc =
0499         (struct soc_mixer_control *)kcontrol->private_value;
0500     int platform_max;
0501     int min = mc->min;
0502 
0503     if (!mc->platform_max)
0504         mc->platform_max = mc->max;
0505     platform_max = mc->platform_max;
0506 
0507     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0508     uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
0509     uinfo->value.integer.min = 0;
0510     uinfo->value.integer.max = platform_max - min;
0511 
0512     return 0;
0513 }
0514 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
0515 
0516 /**
0517  * snd_soc_put_volsw_range - single mixer put value callback with range.
0518  * @kcontrol: mixer control
0519  * @ucontrol: control element information
0520  *
0521  * Callback to set the value, within a range, for a single mixer control.
0522  *
0523  * Returns 0 for success.
0524  */
0525 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
0526     struct snd_ctl_elem_value *ucontrol)
0527 {
0528     struct soc_mixer_control *mc =
0529         (struct soc_mixer_control *)kcontrol->private_value;
0530     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0531     unsigned int reg = mc->reg;
0532     unsigned int rreg = mc->rreg;
0533     unsigned int shift = mc->shift;
0534     int min = mc->min;
0535     int max = mc->max;
0536     unsigned int mask = (1 << fls(max)) - 1;
0537     unsigned int invert = mc->invert;
0538     unsigned int val, val_mask;
0539     int err, ret, tmp;
0540 
0541     tmp = ucontrol->value.integer.value[0];
0542     if (tmp < 0)
0543         return -EINVAL;
0544     if (mc->platform_max && tmp > mc->platform_max)
0545         return -EINVAL;
0546     if (tmp > mc->max - mc->min)
0547         return -EINVAL;
0548 
0549     if (invert)
0550         val = (max - ucontrol->value.integer.value[0]) & mask;
0551     else
0552         val = ((ucontrol->value.integer.value[0] + min) & mask);
0553     val_mask = mask << shift;
0554     val = val << shift;
0555 
0556     err = snd_soc_component_update_bits(component, reg, val_mask, val);
0557     if (err < 0)
0558         return err;
0559     ret = err;
0560 
0561     if (snd_soc_volsw_is_stereo(mc)) {
0562         tmp = ucontrol->value.integer.value[1];
0563         if (tmp < 0)
0564             return -EINVAL;
0565         if (mc->platform_max && tmp > mc->platform_max)
0566             return -EINVAL;
0567         if (tmp > mc->max - mc->min)
0568             return -EINVAL;
0569 
0570         if (invert)
0571             val = (max - ucontrol->value.integer.value[1]) & mask;
0572         else
0573             val = ((ucontrol->value.integer.value[1] + min) & mask);
0574         val_mask = mask << shift;
0575         val = val << shift;
0576 
0577         err = snd_soc_component_update_bits(component, rreg, val_mask,
0578             val);
0579         /* Don't discard any error code or drop change flag */
0580         if (ret == 0 || err < 0) {
0581             ret = err;
0582         }
0583     }
0584 
0585     return ret;
0586 }
0587 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
0588 
0589 /**
0590  * snd_soc_get_volsw_range - single mixer get callback with range
0591  * @kcontrol: mixer control
0592  * @ucontrol: control element information
0593  *
0594  * Callback to get the value, within a range, of a single mixer control.
0595  *
0596  * Returns 0 for success.
0597  */
0598 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
0599     struct snd_ctl_elem_value *ucontrol)
0600 {
0601     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0602     struct soc_mixer_control *mc =
0603         (struct soc_mixer_control *)kcontrol->private_value;
0604     unsigned int reg = mc->reg;
0605     unsigned int rreg = mc->rreg;
0606     unsigned int shift = mc->shift;
0607     int min = mc->min;
0608     int max = mc->max;
0609     unsigned int mask = (1 << fls(max)) - 1;
0610     unsigned int invert = mc->invert;
0611     unsigned int val;
0612 
0613     val = snd_soc_component_read(component, reg);
0614     ucontrol->value.integer.value[0] = (val >> shift) & mask;
0615     if (invert)
0616         ucontrol->value.integer.value[0] =
0617             max - ucontrol->value.integer.value[0];
0618     else
0619         ucontrol->value.integer.value[0] =
0620             ucontrol->value.integer.value[0] - min;
0621 
0622     if (snd_soc_volsw_is_stereo(mc)) {
0623         val = snd_soc_component_read(component, rreg);
0624         ucontrol->value.integer.value[1] = (val >> shift) & mask;
0625         if (invert)
0626             ucontrol->value.integer.value[1] =
0627                 max - ucontrol->value.integer.value[1];
0628         else
0629             ucontrol->value.integer.value[1] =
0630                 ucontrol->value.integer.value[1] - min;
0631     }
0632 
0633     return 0;
0634 }
0635 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
0636 
0637 /**
0638  * snd_soc_limit_volume - Set new limit to an existing volume control.
0639  *
0640  * @card: where to look for the control
0641  * @name: Name of the control
0642  * @max: new maximum limit
0643  *
0644  * Return 0 for success, else error.
0645  */
0646 int snd_soc_limit_volume(struct snd_soc_card *card,
0647     const char *name, int max)
0648 {
0649     struct snd_kcontrol *kctl;
0650     int ret = -EINVAL;
0651 
0652     /* Sanity check for name and max */
0653     if (unlikely(!name || max <= 0))
0654         return -EINVAL;
0655 
0656     kctl = snd_soc_card_get_kcontrol(card, name);
0657     if (kctl) {
0658         struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value;
0659         if (max <= mc->max) {
0660             mc->platform_max = max;
0661             ret = 0;
0662         }
0663     }
0664     return ret;
0665 }
0666 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
0667 
0668 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
0669                struct snd_ctl_elem_info *uinfo)
0670 {
0671     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0672     struct soc_bytes *params = (void *)kcontrol->private_value;
0673 
0674     uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
0675     uinfo->count = params->num_regs * component->val_bytes;
0676 
0677     return 0;
0678 }
0679 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
0680 
0681 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
0682               struct snd_ctl_elem_value *ucontrol)
0683 {
0684     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0685     struct soc_bytes *params = (void *)kcontrol->private_value;
0686     int ret;
0687 
0688     if (component->regmap)
0689         ret = regmap_raw_read(component->regmap, params->base,
0690                       ucontrol->value.bytes.data,
0691                       params->num_regs * component->val_bytes);
0692     else
0693         ret = -EINVAL;
0694 
0695     /* Hide any masked bytes to ensure consistent data reporting */
0696     if (ret == 0 && params->mask) {
0697         switch (component->val_bytes) {
0698         case 1:
0699             ucontrol->value.bytes.data[0] &= ~params->mask;
0700             break;
0701         case 2:
0702             ((u16 *)(&ucontrol->value.bytes.data))[0]
0703                 &= cpu_to_be16(~params->mask);
0704             break;
0705         case 4:
0706             ((u32 *)(&ucontrol->value.bytes.data))[0]
0707                 &= cpu_to_be32(~params->mask);
0708             break;
0709         default:
0710             return -EINVAL;
0711         }
0712     }
0713 
0714     return ret;
0715 }
0716 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
0717 
0718 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
0719               struct snd_ctl_elem_value *ucontrol)
0720 {
0721     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0722     struct soc_bytes *params = (void *)kcontrol->private_value;
0723     int ret, len;
0724     unsigned int val, mask;
0725     void *data;
0726 
0727     if (!component->regmap || !params->num_regs)
0728         return -EINVAL;
0729 
0730     len = params->num_regs * component->val_bytes;
0731 
0732     data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
0733     if (!data)
0734         return -ENOMEM;
0735 
0736     /*
0737      * If we've got a mask then we need to preserve the register
0738      * bits.  We shouldn't modify the incoming data so take a
0739      * copy.
0740      */
0741     if (params->mask) {
0742         ret = regmap_read(component->regmap, params->base, &val);
0743         if (ret != 0)
0744             goto out;
0745 
0746         val &= params->mask;
0747 
0748         switch (component->val_bytes) {
0749         case 1:
0750             ((u8 *)data)[0] &= ~params->mask;
0751             ((u8 *)data)[0] |= val;
0752             break;
0753         case 2:
0754             mask = ~params->mask;
0755             ret = regmap_parse_val(component->regmap,
0756                             &mask, &mask);
0757             if (ret != 0)
0758                 goto out;
0759 
0760             ((u16 *)data)[0] &= mask;
0761 
0762             ret = regmap_parse_val(component->regmap,
0763                             &val, &val);
0764             if (ret != 0)
0765                 goto out;
0766 
0767             ((u16 *)data)[0] |= val;
0768             break;
0769         case 4:
0770             mask = ~params->mask;
0771             ret = regmap_parse_val(component->regmap,
0772                             &mask, &mask);
0773             if (ret != 0)
0774                 goto out;
0775 
0776             ((u32 *)data)[0] &= mask;
0777 
0778             ret = regmap_parse_val(component->regmap,
0779                             &val, &val);
0780             if (ret != 0)
0781                 goto out;
0782 
0783             ((u32 *)data)[0] |= val;
0784             break;
0785         default:
0786             ret = -EINVAL;
0787             goto out;
0788         }
0789     }
0790 
0791     ret = regmap_raw_write(component->regmap, params->base,
0792                    data, len);
0793 
0794 out:
0795     kfree(data);
0796 
0797     return ret;
0798 }
0799 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
0800 
0801 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
0802             struct snd_ctl_elem_info *ucontrol)
0803 {
0804     struct soc_bytes_ext *params = (void *)kcontrol->private_value;
0805 
0806     ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
0807     ucontrol->count = params->max;
0808 
0809     return 0;
0810 }
0811 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
0812 
0813 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
0814                 unsigned int size, unsigned int __user *tlv)
0815 {
0816     struct soc_bytes_ext *params = (void *)kcontrol->private_value;
0817     unsigned int count = size < params->max ? size : params->max;
0818     int ret = -ENXIO;
0819 
0820     switch (op_flag) {
0821     case SNDRV_CTL_TLV_OP_READ:
0822         if (params->get)
0823             ret = params->get(kcontrol, tlv, count);
0824         break;
0825     case SNDRV_CTL_TLV_OP_WRITE:
0826         if (params->put)
0827             ret = params->put(kcontrol, tlv, count);
0828         break;
0829     }
0830     return ret;
0831 }
0832 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
0833 
0834 /**
0835  * snd_soc_info_xr_sx - signed multi register info callback
0836  * @kcontrol: mreg control
0837  * @uinfo: control element information
0838  *
0839  * Callback to provide information of a control that can
0840  * span multiple codec registers which together
0841  * forms a single signed value in a MSB/LSB manner.
0842  *
0843  * Returns 0 for success.
0844  */
0845 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
0846     struct snd_ctl_elem_info *uinfo)
0847 {
0848     struct soc_mreg_control *mc =
0849         (struct soc_mreg_control *)kcontrol->private_value;
0850     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0851     uinfo->count = 1;
0852     uinfo->value.integer.min = mc->min;
0853     uinfo->value.integer.max = mc->max;
0854 
0855     return 0;
0856 }
0857 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
0858 
0859 /**
0860  * snd_soc_get_xr_sx - signed multi register get callback
0861  * @kcontrol: mreg control
0862  * @ucontrol: control element information
0863  *
0864  * Callback to get the value of a control that can span
0865  * multiple codec registers which together forms a single
0866  * signed value in a MSB/LSB manner. The control supports
0867  * specifying total no of bits used to allow for bitfields
0868  * across the multiple codec registers.
0869  *
0870  * Returns 0 for success.
0871  */
0872 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
0873     struct snd_ctl_elem_value *ucontrol)
0874 {
0875     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0876     struct soc_mreg_control *mc =
0877         (struct soc_mreg_control *)kcontrol->private_value;
0878     unsigned int regbase = mc->regbase;
0879     unsigned int regcount = mc->regcount;
0880     unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
0881     unsigned int regwmask = (1UL<<regwshift)-1;
0882     unsigned int invert = mc->invert;
0883     unsigned long mask = (1UL<<mc->nbits)-1;
0884     long min = mc->min;
0885     long max = mc->max;
0886     long val = 0;
0887     unsigned int i;
0888 
0889     for (i = 0; i < regcount; i++) {
0890         unsigned int regval = snd_soc_component_read(component, regbase+i);
0891         val |= (regval & regwmask) << (regwshift*(regcount-i-1));
0892     }
0893     val &= mask;
0894     if (min < 0 && val > max)
0895         val |= ~mask;
0896     if (invert)
0897         val = max - val;
0898     ucontrol->value.integer.value[0] = val;
0899 
0900     return 0;
0901 }
0902 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
0903 
0904 /**
0905  * snd_soc_put_xr_sx - signed multi register get callback
0906  * @kcontrol: mreg control
0907  * @ucontrol: control element information
0908  *
0909  * Callback to set the value of a control that can span
0910  * multiple codec registers which together forms a single
0911  * signed value in a MSB/LSB manner. The control supports
0912  * specifying total no of bits used to allow for bitfields
0913  * across the multiple codec registers.
0914  *
0915  * Returns 0 for success.
0916  */
0917 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
0918     struct snd_ctl_elem_value *ucontrol)
0919 {
0920     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0921     struct soc_mreg_control *mc =
0922         (struct soc_mreg_control *)kcontrol->private_value;
0923     unsigned int regbase = mc->regbase;
0924     unsigned int regcount = mc->regcount;
0925     unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
0926     unsigned int regwmask = (1UL<<regwshift)-1;
0927     unsigned int invert = mc->invert;
0928     unsigned long mask = (1UL<<mc->nbits)-1;
0929     long max = mc->max;
0930     long val = ucontrol->value.integer.value[0];
0931     int ret = 0;
0932     unsigned int i;
0933 
0934     if (val < mc->min || val > mc->max)
0935         return -EINVAL;
0936     if (invert)
0937         val = max - val;
0938     val &= mask;
0939     for (i = 0; i < regcount; i++) {
0940         unsigned int regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
0941         unsigned int regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
0942         int err = snd_soc_component_update_bits(component, regbase+i,
0943                             regmask, regval);
0944         if (err < 0)
0945             return err;
0946         if (err > 0)
0947             ret = err;
0948     }
0949 
0950     return ret;
0951 }
0952 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
0953 
0954 /**
0955  * snd_soc_get_strobe - strobe get callback
0956  * @kcontrol: mixer control
0957  * @ucontrol: control element information
0958  *
0959  * Callback get the value of a strobe mixer control.
0960  *
0961  * Returns 0 for success.
0962  */
0963 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
0964     struct snd_ctl_elem_value *ucontrol)
0965 {
0966     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0967     struct soc_mixer_control *mc =
0968         (struct soc_mixer_control *)kcontrol->private_value;
0969     unsigned int reg = mc->reg;
0970     unsigned int shift = mc->shift;
0971     unsigned int mask = 1 << shift;
0972     unsigned int invert = mc->invert != 0;
0973     unsigned int val;
0974 
0975     val = snd_soc_component_read(component, reg);
0976     val &= mask;
0977 
0978     if (shift != 0 && val != 0)
0979         val = val >> shift;
0980     ucontrol->value.enumerated.item[0] = val ^ invert;
0981 
0982     return 0;
0983 }
0984 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
0985 
0986 /**
0987  * snd_soc_put_strobe - strobe put callback
0988  * @kcontrol: mixer control
0989  * @ucontrol: control element information
0990  *
0991  * Callback strobe a register bit to high then low (or the inverse)
0992  * in one pass of a single mixer enum control.
0993  *
0994  * Returns 1 for success.
0995  */
0996 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
0997     struct snd_ctl_elem_value *ucontrol)
0998 {
0999     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1000     struct soc_mixer_control *mc =
1001         (struct soc_mixer_control *)kcontrol->private_value;
1002     unsigned int reg = mc->reg;
1003     unsigned int shift = mc->shift;
1004     unsigned int mask = 1 << shift;
1005     unsigned int invert = mc->invert != 0;
1006     unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
1007     unsigned int val1 = (strobe ^ invert) ? mask : 0;
1008     unsigned int val2 = (strobe ^ invert) ? 0 : mask;
1009     int err;
1010 
1011     err = snd_soc_component_update_bits(component, reg, mask, val1);
1012     if (err < 0)
1013         return err;
1014 
1015     return snd_soc_component_update_bits(component, reg, mask, val2);
1016 }
1017 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);