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