xref: /linux/sound/soc/soc-ops.c (revision b20be2c77ce5341ded1a2d8aec119f6dca8ef1ad)
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 /**
114  * snd_soc_read_signed - Read a codec register and interpret as signed value
115  * @component: component
116  * @reg: Register to read
117  * @mask: Mask to use after shifting the register value
118  * @shift: Right shift of register value
119  * @sign_bit: Bit that describes if a number is negative or not.
120  * @signed_val: Pointer to where the read value should be stored
121  *
122  * This functions reads a codec register. The register value is shifted right
123  * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
124  * the given registervalue into a signed integer if sign_bit is non-zero.
125  */
126 static void snd_soc_read_signed(struct snd_soc_component *component,
127 	unsigned int reg, unsigned int mask, unsigned int shift,
128 	unsigned int sign_bit, int *signed_val)
129 {
130 	int ret;
131 	unsigned int val;
132 
133 	val = snd_soc_component_read(component, reg);
134 	val = (val >> shift) & mask;
135 
136 	if (!sign_bit) {
137 		*signed_val = val;
138 		return;
139 	}
140 
141 	/* non-negative number */
142 	if (!(val & BIT(sign_bit))) {
143 		*signed_val = val;
144 		return;
145 	}
146 
147 	ret = val;
148 
149 	/*
150 	 * The register most probably does not contain a full-sized int.
151 	 * Instead we have an arbitrary number of bits in a signed
152 	 * representation which has to be translated into a full-sized int.
153 	 * This is done by filling up all bits above the sign-bit.
154 	 */
155 	ret |= ~((int)(BIT(sign_bit) - 1));
156 
157 	*signed_val = ret;
158 }
159 
160 /**
161  * snd_soc_info_volsw - single mixer info callback
162  * @kcontrol: mixer control
163  * @uinfo: control element information
164  *
165  * Callback to provide information about a single mixer control, or a double
166  * mixer control that spans 2 registers.
167  *
168  * Returns 0 for success.
169  */
170 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
171 	struct snd_ctl_elem_info *uinfo)
172 {
173 	struct soc_mixer_control *mc =
174 		(struct soc_mixer_control *)kcontrol->private_value;
175 	const char *vol_string = NULL;
176 	int max;
177 
178 	max = uinfo->value.integer.max = mc->max - mc->min;
179 	if (mc->platform_max && mc->platform_max < max)
180 		max = mc->platform_max;
181 
182 	if (max == 1) {
183 		/* Even two value controls ending in Volume should always be integer */
184 		vol_string = strstr(kcontrol->id.name, " Volume");
185 		if (vol_string && !strcmp(vol_string, " Volume"))
186 			uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
187 		else
188 			uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
189 	} else {
190 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
191 	}
192 
193 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
194 	uinfo->value.integer.min = 0;
195 	uinfo->value.integer.max = max;
196 
197 	return 0;
198 }
199 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
200 
201 /**
202  * snd_soc_info_volsw_sx - Mixer info callback for SX TLV controls
203  * @kcontrol: mixer control
204  * @uinfo: control element information
205  *
206  * Callback to provide information about a single mixer control, or a double
207  * mixer control that spans 2 registers of the SX TLV type. SX TLV controls
208  * have a range that represents both positive and negative values either side
209  * of zero but without a sign bit. min is the minimum register value, max is
210  * the number of steps.
211  *
212  * Returns 0 for success.
213  */
214 int snd_soc_info_volsw_sx(struct snd_kcontrol *kcontrol,
215 			  struct snd_ctl_elem_info *uinfo)
216 {
217 	struct soc_mixer_control *mc =
218 		(struct soc_mixer_control *)kcontrol->private_value;
219 	int max;
220 
221 	if (mc->platform_max)
222 		max = mc->platform_max;
223 	else
224 		max = mc->max;
225 
226 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
227 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
228 	else
229 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
230 
231 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
232 	uinfo->value.integer.min = 0;
233 	uinfo->value.integer.max = max;
234 
235 	return 0;
236 }
237 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_sx);
238 
239 /**
240  * snd_soc_get_volsw - single mixer get callback
241  * @kcontrol: mixer control
242  * @ucontrol: control element information
243  *
244  * Callback to get the value of a single mixer control, or a double mixer
245  * control that spans 2 registers.
246  *
247  * Returns 0 for success.
248  */
249 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
250 	struct snd_ctl_elem_value *ucontrol)
251 {
252 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
253 	struct soc_mixer_control *mc =
254 		(struct soc_mixer_control *)kcontrol->private_value;
255 	unsigned int reg = mc->reg;
256 	unsigned int reg2 = mc->rreg;
257 	unsigned int shift = mc->shift;
258 	unsigned int rshift = mc->rshift;
259 	int max = mc->max;
260 	int min = mc->min;
261 	int sign_bit = mc->sign_bit;
262 	unsigned int mask = (1ULL << fls(max)) - 1;
263 	unsigned int invert = mc->invert;
264 	int val;
265 
266 	if (sign_bit)
267 		mask = BIT(sign_bit + 1) - 1;
268 
269 	snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
270 
271 	ucontrol->value.integer.value[0] = val - min;
272 	if (invert)
273 		ucontrol->value.integer.value[0] =
274 			max - ucontrol->value.integer.value[0];
275 
276 	if (snd_soc_volsw_is_stereo(mc)) {
277 		if (reg == reg2)
278 			snd_soc_read_signed(component, reg, mask, rshift, sign_bit, &val);
279 		else
280 			snd_soc_read_signed(component, reg2, mask, shift, sign_bit, &val);
281 
282 		ucontrol->value.integer.value[1] = val - min;
283 		if (invert)
284 			ucontrol->value.integer.value[1] =
285 				max - ucontrol->value.integer.value[1];
286 	}
287 
288 	return 0;
289 }
290 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
291 
292 /**
293  * snd_soc_put_volsw - single mixer put callback
294  * @kcontrol: mixer control
295  * @ucontrol: control element information
296  *
297  * Callback to set the value of a single mixer control, or a double mixer
298  * control that spans 2 registers.
299  *
300  * Returns 0 for success.
301  */
302 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
303 	struct snd_ctl_elem_value *ucontrol)
304 {
305 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
306 	struct soc_mixer_control *mc =
307 		(struct soc_mixer_control *)kcontrol->private_value;
308 	unsigned int reg = mc->reg;
309 	unsigned int reg2 = mc->rreg;
310 	unsigned int shift = mc->shift;
311 	unsigned int rshift = mc->rshift;
312 	int max = mc->max;
313 	int min = mc->min;
314 	unsigned int sign_bit = mc->sign_bit;
315 	unsigned int mask = (1 << fls(max)) - 1;
316 	unsigned int invert = mc->invert;
317 	int err, ret;
318 	bool type_2r = false;
319 	unsigned int val2 = 0;
320 	unsigned int val, val_mask;
321 
322 	if (sign_bit)
323 		mask = BIT(sign_bit + 1) - 1;
324 
325 	if (ucontrol->value.integer.value[0] < 0)
326 		return -EINVAL;
327 	val = ucontrol->value.integer.value[0];
328 	if (mc->platform_max && ((int)val + min) > mc->platform_max)
329 		return -EINVAL;
330 	if (val > max - min)
331 		return -EINVAL;
332 	val = (val + min) & mask;
333 	if (invert)
334 		val = max - val;
335 	val_mask = mask << shift;
336 	val = val << shift;
337 	if (snd_soc_volsw_is_stereo(mc)) {
338 		if (ucontrol->value.integer.value[1] < 0)
339 			return -EINVAL;
340 		val2 = ucontrol->value.integer.value[1];
341 		if (mc->platform_max && ((int)val2 + min) > mc->platform_max)
342 			return -EINVAL;
343 		if (val2 > max - min)
344 			return -EINVAL;
345 		val2 = (val2 + min) & mask;
346 		if (invert)
347 			val2 = max - val2;
348 		if (reg == reg2) {
349 			val_mask |= mask << rshift;
350 			val |= val2 << rshift;
351 		} else {
352 			val2 = val2 << shift;
353 			type_2r = true;
354 		}
355 	}
356 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
357 	if (err < 0)
358 		return err;
359 	ret = err;
360 
361 	if (type_2r) {
362 		err = snd_soc_component_update_bits(component, reg2, val_mask,
363 						    val2);
364 		/* Don't discard any error code or drop change flag */
365 		if (ret == 0 || err < 0) {
366 			ret = err;
367 		}
368 	}
369 
370 	return ret;
371 }
372 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
373 
374 /**
375  * snd_soc_get_volsw_sx - single mixer get callback
376  * @kcontrol: mixer control
377  * @ucontrol: control element information
378  *
379  * Callback to get the value of a single mixer control, or a double mixer
380  * control that spans 2 registers.
381  *
382  * Returns 0 for success.
383  */
384 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
385 		      struct snd_ctl_elem_value *ucontrol)
386 {
387 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
388 	struct soc_mixer_control *mc =
389 	    (struct soc_mixer_control *)kcontrol->private_value;
390 	unsigned int reg = mc->reg;
391 	unsigned int reg2 = mc->rreg;
392 	unsigned int shift = mc->shift;
393 	unsigned int rshift = mc->rshift;
394 	int max = mc->max;
395 	int min = mc->min;
396 	unsigned int mask = (1U << (fls(min + max) - 1)) - 1;
397 	unsigned int val;
398 
399 	val = snd_soc_component_read(component, reg);
400 	ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
401 
402 	if (snd_soc_volsw_is_stereo(mc)) {
403 		val = snd_soc_component_read(component, reg2);
404 		val = ((val >> rshift) - min) & mask;
405 		ucontrol->value.integer.value[1] = val;
406 	}
407 
408 	return 0;
409 }
410 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
411 
412 /**
413  * snd_soc_put_volsw_sx - double mixer set callback
414  * @kcontrol: mixer control
415  * @ucontrol: control element information
416  *
417  * Callback to set the value of a double mixer control that spans 2 registers.
418  *
419  * Returns 0 for success.
420  */
421 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
422 			 struct snd_ctl_elem_value *ucontrol)
423 {
424 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
425 	struct soc_mixer_control *mc =
426 	    (struct soc_mixer_control *)kcontrol->private_value;
427 
428 	unsigned int reg = mc->reg;
429 	unsigned int reg2 = mc->rreg;
430 	unsigned int shift = mc->shift;
431 	unsigned int rshift = mc->rshift;
432 	int max = mc->max;
433 	int min = mc->min;
434 	unsigned int mask = (1U << (fls(min + max) - 1)) - 1;
435 	int err = 0;
436 	int ret;
437 	unsigned int val, val_mask;
438 
439 	if (ucontrol->value.integer.value[0] < 0)
440 		return -EINVAL;
441 	val = ucontrol->value.integer.value[0];
442 	if (mc->platform_max && val > mc->platform_max)
443 		return -EINVAL;
444 	if (val > max)
445 		return -EINVAL;
446 	val_mask = mask << shift;
447 	val = (val + min) & mask;
448 	val = val << shift;
449 
450 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
451 	if (err < 0)
452 		return err;
453 	ret = err;
454 
455 	if (snd_soc_volsw_is_stereo(mc)) {
456 		unsigned int val2 = ucontrol->value.integer.value[1];
457 
458 		if (mc->platform_max && val2 > mc->platform_max)
459 			return -EINVAL;
460 		if (val2 > max)
461 			return -EINVAL;
462 
463 		val_mask = mask << rshift;
464 		val2 = (val2 + min) & mask;
465 		val2 = val2 << rshift;
466 
467 		err = snd_soc_component_update_bits(component, reg2, val_mask,
468 			val2);
469 
470 		/* Don't discard any error code or drop change flag */
471 		if (ret == 0 || err < 0) {
472 			ret = err;
473 		}
474 	}
475 	return ret;
476 }
477 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
478 
479 /**
480  * snd_soc_info_volsw_range - single mixer info callback with range.
481  * @kcontrol: mixer control
482  * @uinfo: control element information
483  *
484  * Callback to provide information, within a range, about a single
485  * mixer control.
486  *
487  * returns 0 for success.
488  */
489 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
490 	struct snd_ctl_elem_info *uinfo)
491 {
492 	struct soc_mixer_control *mc =
493 		(struct soc_mixer_control *)kcontrol->private_value;
494 	int platform_max;
495 	int min = mc->min;
496 
497 	if (!mc->platform_max)
498 		mc->platform_max = mc->max;
499 	platform_max = mc->platform_max;
500 
501 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
502 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
503 	uinfo->value.integer.min = 0;
504 	uinfo->value.integer.max = platform_max - min;
505 
506 	return 0;
507 }
508 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
509 
510 /**
511  * snd_soc_put_volsw_range - single mixer put value callback with range.
512  * @kcontrol: mixer control
513  * @ucontrol: control element information
514  *
515  * Callback to set the value, within a range, for a single mixer control.
516  *
517  * Returns 0 for success.
518  */
519 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
520 	struct snd_ctl_elem_value *ucontrol)
521 {
522 	struct soc_mixer_control *mc =
523 		(struct soc_mixer_control *)kcontrol->private_value;
524 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
525 	unsigned int reg = mc->reg;
526 	unsigned int rreg = mc->rreg;
527 	unsigned int shift = mc->shift;
528 	int min = mc->min;
529 	int max = mc->max;
530 	unsigned int mask = (1 << fls(max)) - 1;
531 	unsigned int invert = mc->invert;
532 	unsigned int val, val_mask;
533 	int err, ret, tmp;
534 
535 	tmp = ucontrol->value.integer.value[0];
536 	if (tmp < 0)
537 		return -EINVAL;
538 	if (mc->platform_max && tmp > mc->platform_max)
539 		return -EINVAL;
540 	if (tmp > mc->max - mc->min)
541 		return -EINVAL;
542 
543 	if (invert)
544 		val = (max - ucontrol->value.integer.value[0]) & mask;
545 	else
546 		val = ((ucontrol->value.integer.value[0] + min) & mask);
547 	val_mask = mask << shift;
548 	val = val << shift;
549 
550 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
551 	if (err < 0)
552 		return err;
553 	ret = err;
554 
555 	if (snd_soc_volsw_is_stereo(mc)) {
556 		tmp = ucontrol->value.integer.value[1];
557 		if (tmp < 0)
558 			return -EINVAL;
559 		if (mc->platform_max && tmp > mc->platform_max)
560 			return -EINVAL;
561 		if (tmp > mc->max - mc->min)
562 			return -EINVAL;
563 
564 		if (invert)
565 			val = (max - ucontrol->value.integer.value[1]) & mask;
566 		else
567 			val = ((ucontrol->value.integer.value[1] + min) & mask);
568 		val_mask = mask << shift;
569 		val = val << shift;
570 
571 		err = snd_soc_component_update_bits(component, rreg, val_mask,
572 			val);
573 		/* Don't discard any error code or drop change flag */
574 		if (ret == 0 || err < 0) {
575 			ret = err;
576 		}
577 	}
578 
579 	return ret;
580 }
581 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
582 
583 /**
584  * snd_soc_get_volsw_range - single mixer get callback with range
585  * @kcontrol: mixer control
586  * @ucontrol: control element information
587  *
588  * Callback to get the value, within a range, of a single mixer control.
589  *
590  * Returns 0 for success.
591  */
592 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
593 	struct snd_ctl_elem_value *ucontrol)
594 {
595 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
596 	struct soc_mixer_control *mc =
597 		(struct soc_mixer_control *)kcontrol->private_value;
598 	unsigned int reg = mc->reg;
599 	unsigned int rreg = mc->rreg;
600 	unsigned int shift = mc->shift;
601 	int min = mc->min;
602 	int max = mc->max;
603 	unsigned int mask = (1 << fls(max)) - 1;
604 	unsigned int invert = mc->invert;
605 	unsigned int val;
606 
607 	val = snd_soc_component_read(component, reg);
608 	ucontrol->value.integer.value[0] = (val >> shift) & mask;
609 	if (invert)
610 		ucontrol->value.integer.value[0] =
611 			max - ucontrol->value.integer.value[0];
612 	else
613 		ucontrol->value.integer.value[0] =
614 			ucontrol->value.integer.value[0] - min;
615 
616 	if (snd_soc_volsw_is_stereo(mc)) {
617 		val = snd_soc_component_read(component, rreg);
618 		ucontrol->value.integer.value[1] = (val >> shift) & mask;
619 		if (invert)
620 			ucontrol->value.integer.value[1] =
621 				max - ucontrol->value.integer.value[1];
622 		else
623 			ucontrol->value.integer.value[1] =
624 				ucontrol->value.integer.value[1] - min;
625 	}
626 
627 	return 0;
628 }
629 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
630 
631 static int snd_soc_clip_to_platform_max(struct snd_kcontrol *kctl)
632 {
633 	struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value;
634 	struct snd_ctl_elem_value uctl;
635 	int ret;
636 
637 	if (!mc->platform_max)
638 		return 0;
639 
640 	ret = kctl->get(kctl, &uctl);
641 	if (ret < 0)
642 		return ret;
643 
644 	if (uctl.value.integer.value[0] > mc->platform_max)
645 		uctl.value.integer.value[0] = mc->platform_max;
646 
647 	if (snd_soc_volsw_is_stereo(mc) &&
648 	    uctl.value.integer.value[1] > mc->platform_max)
649 		uctl.value.integer.value[1] = mc->platform_max;
650 
651 	ret = kctl->put(kctl, &uctl);
652 	if (ret < 0)
653 		return ret;
654 
655 	return 0;
656 }
657 
658 /**
659  * snd_soc_limit_volume - Set new limit to an existing volume control.
660  *
661  * @card: where to look for the control
662  * @name: Name of the control
663  * @max: new maximum limit
664  *
665  * Return 0 for success, else error.
666  */
667 int snd_soc_limit_volume(struct snd_soc_card *card,
668 	const char *name, int max)
669 {
670 	struct snd_kcontrol *kctl;
671 	int ret = -EINVAL;
672 
673 	/* Sanity check for name and max */
674 	if (unlikely(!name || max <= 0))
675 		return -EINVAL;
676 
677 	kctl = snd_soc_card_get_kcontrol(card, name);
678 	if (kctl) {
679 		struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value;
680 		if (max <= mc->max - mc->min) {
681 			mc->platform_max = max;
682 			ret = snd_soc_clip_to_platform_max(kctl);
683 		}
684 	}
685 	return ret;
686 }
687 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
688 
689 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
690 		       struct snd_ctl_elem_info *uinfo)
691 {
692 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
693 	struct soc_bytes *params = (void *)kcontrol->private_value;
694 
695 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
696 	uinfo->count = params->num_regs * component->val_bytes;
697 
698 	return 0;
699 }
700 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
701 
702 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
703 		      struct snd_ctl_elem_value *ucontrol)
704 {
705 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
706 	struct soc_bytes *params = (void *)kcontrol->private_value;
707 	int ret;
708 
709 	if (component->regmap)
710 		ret = regmap_raw_read(component->regmap, params->base,
711 				      ucontrol->value.bytes.data,
712 				      params->num_regs * component->val_bytes);
713 	else
714 		ret = -EINVAL;
715 
716 	/* Hide any masked bytes to ensure consistent data reporting */
717 	if (ret == 0 && params->mask) {
718 		switch (component->val_bytes) {
719 		case 1:
720 			ucontrol->value.bytes.data[0] &= ~params->mask;
721 			break;
722 		case 2:
723 			((u16 *)(&ucontrol->value.bytes.data))[0]
724 				&= cpu_to_be16(~params->mask);
725 			break;
726 		case 4:
727 			((u32 *)(&ucontrol->value.bytes.data))[0]
728 				&= cpu_to_be32(~params->mask);
729 			break;
730 		default:
731 			return -EINVAL;
732 		}
733 	}
734 
735 	return ret;
736 }
737 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
738 
739 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
740 		      struct snd_ctl_elem_value *ucontrol)
741 {
742 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
743 	struct soc_bytes *params = (void *)kcontrol->private_value;
744 	int ret, len;
745 	unsigned int val, mask;
746 
747 	if (!component->regmap || !params->num_regs)
748 		return -EINVAL;
749 
750 	len = params->num_regs * component->val_bytes;
751 
752 	void *data __free(kfree) = kmemdup(ucontrol->value.bytes.data, len,
753 					   GFP_KERNEL | GFP_DMA);
754 	if (!data)
755 		return -ENOMEM;
756 
757 	/*
758 	 * If we've got a mask then we need to preserve the register
759 	 * bits.  We shouldn't modify the incoming data so take a
760 	 * copy.
761 	 */
762 	if (params->mask) {
763 		ret = regmap_read(component->regmap, params->base, &val);
764 		if (ret != 0)
765 			return ret;
766 
767 		val &= params->mask;
768 
769 		switch (component->val_bytes) {
770 		case 1:
771 			((u8 *)data)[0] &= ~params->mask;
772 			((u8 *)data)[0] |= val;
773 			break;
774 		case 2:
775 			mask = ~params->mask;
776 			ret = regmap_parse_val(component->regmap,
777 							&mask, &mask);
778 			if (ret != 0)
779 				return ret;
780 
781 			((u16 *)data)[0] &= mask;
782 
783 			ret = regmap_parse_val(component->regmap,
784 							&val, &val);
785 			if (ret != 0)
786 				return ret;
787 
788 			((u16 *)data)[0] |= val;
789 			break;
790 		case 4:
791 			mask = ~params->mask;
792 			ret = regmap_parse_val(component->regmap,
793 							&mask, &mask);
794 			if (ret != 0)
795 				return ret;
796 
797 			((u32 *)data)[0] &= mask;
798 
799 			ret = regmap_parse_val(component->regmap,
800 							&val, &val);
801 			if (ret != 0)
802 				return ret;
803 
804 			((u32 *)data)[0] |= val;
805 			break;
806 		default:
807 			return -EINVAL;
808 		}
809 	}
810 
811 	return regmap_raw_write(component->regmap, params->base, data, len);
812 }
813 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
814 
815 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
816 			struct snd_ctl_elem_info *ucontrol)
817 {
818 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
819 
820 	ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
821 	ucontrol->count = params->max;
822 
823 	return 0;
824 }
825 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
826 
827 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
828 				unsigned int size, unsigned int __user *tlv)
829 {
830 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
831 	unsigned int count = size < params->max ? size : params->max;
832 	int ret = -ENXIO;
833 
834 	switch (op_flag) {
835 	case SNDRV_CTL_TLV_OP_READ:
836 		if (params->get)
837 			ret = params->get(kcontrol, tlv, count);
838 		break;
839 	case SNDRV_CTL_TLV_OP_WRITE:
840 		if (params->put)
841 			ret = params->put(kcontrol, tlv, count);
842 		break;
843 	}
844 	return ret;
845 }
846 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
847 
848 /**
849  * snd_soc_info_xr_sx - signed multi register info callback
850  * @kcontrol: mreg control
851  * @uinfo: control element information
852  *
853  * Callback to provide information of a control that can
854  * span multiple codec registers which together
855  * forms a single signed value in a MSB/LSB manner.
856  *
857  * Returns 0 for success.
858  */
859 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
860 	struct snd_ctl_elem_info *uinfo)
861 {
862 	struct soc_mreg_control *mc =
863 		(struct soc_mreg_control *)kcontrol->private_value;
864 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
865 	uinfo->count = 1;
866 	uinfo->value.integer.min = mc->min;
867 	uinfo->value.integer.max = mc->max;
868 
869 	return 0;
870 }
871 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
872 
873 /**
874  * snd_soc_get_xr_sx - signed multi register get callback
875  * @kcontrol: mreg control
876  * @ucontrol: control element information
877  *
878  * Callback to get the value of a control that can span
879  * multiple codec registers which together forms a single
880  * signed value in a MSB/LSB manner. The control supports
881  * specifying total no of bits used to allow for bitfields
882  * across the multiple codec registers.
883  *
884  * Returns 0 for success.
885  */
886 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
887 	struct snd_ctl_elem_value *ucontrol)
888 {
889 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
890 	struct soc_mreg_control *mc =
891 		(struct soc_mreg_control *)kcontrol->private_value;
892 	unsigned int regbase = mc->regbase;
893 	unsigned int regcount = mc->regcount;
894 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
895 	unsigned int regwmask = (1UL<<regwshift)-1;
896 	unsigned int invert = mc->invert;
897 	unsigned long mask = (1UL<<mc->nbits)-1;
898 	long min = mc->min;
899 	long max = mc->max;
900 	long val = 0;
901 	unsigned int i;
902 
903 	for (i = 0; i < regcount; i++) {
904 		unsigned int regval = snd_soc_component_read(component, regbase+i);
905 		val |= (regval & regwmask) << (regwshift*(regcount-i-1));
906 	}
907 	val &= mask;
908 	if (min < 0 && val > max)
909 		val |= ~mask;
910 	if (invert)
911 		val = max - val;
912 	ucontrol->value.integer.value[0] = val;
913 
914 	return 0;
915 }
916 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
917 
918 /**
919  * snd_soc_put_xr_sx - signed multi register get callback
920  * @kcontrol: mreg control
921  * @ucontrol: control element information
922  *
923  * Callback to set the value of a control that can span
924  * multiple codec registers which together forms a single
925  * signed value in a MSB/LSB manner. The control supports
926  * specifying total no of bits used to allow for bitfields
927  * across the multiple codec registers.
928  *
929  * Returns 0 for success.
930  */
931 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
932 	struct snd_ctl_elem_value *ucontrol)
933 {
934 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
935 	struct soc_mreg_control *mc =
936 		(struct soc_mreg_control *)kcontrol->private_value;
937 	unsigned int regbase = mc->regbase;
938 	unsigned int regcount = mc->regcount;
939 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
940 	unsigned int regwmask = (1UL<<regwshift)-1;
941 	unsigned int invert = mc->invert;
942 	unsigned long mask = (1UL<<mc->nbits)-1;
943 	long max = mc->max;
944 	long val = ucontrol->value.integer.value[0];
945 	int ret = 0;
946 	unsigned int i;
947 
948 	if (val < mc->min || val > mc->max)
949 		return -EINVAL;
950 	if (invert)
951 		val = max - val;
952 	val &= mask;
953 	for (i = 0; i < regcount; i++) {
954 		unsigned int regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
955 		unsigned int regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
956 		int err = snd_soc_component_update_bits(component, regbase+i,
957 							regmask, regval);
958 		if (err < 0)
959 			return err;
960 		if (err > 0)
961 			ret = err;
962 	}
963 
964 	return ret;
965 }
966 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
967 
968 /**
969  * snd_soc_get_strobe - strobe get callback
970  * @kcontrol: mixer control
971  * @ucontrol: control element information
972  *
973  * Callback get the value of a strobe mixer control.
974  *
975  * Returns 0 for success.
976  */
977 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
978 	struct snd_ctl_elem_value *ucontrol)
979 {
980 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
981 	struct soc_mixer_control *mc =
982 		(struct soc_mixer_control *)kcontrol->private_value;
983 	unsigned int reg = mc->reg;
984 	unsigned int shift = mc->shift;
985 	unsigned int mask = 1 << shift;
986 	unsigned int invert = mc->invert != 0;
987 	unsigned int val;
988 
989 	val = snd_soc_component_read(component, reg);
990 	val &= mask;
991 
992 	if (shift != 0 && val != 0)
993 		val = val >> shift;
994 	ucontrol->value.enumerated.item[0] = val ^ invert;
995 
996 	return 0;
997 }
998 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
999 
1000 /**
1001  * snd_soc_put_strobe - strobe put callback
1002  * @kcontrol: mixer control
1003  * @ucontrol: control element information
1004  *
1005  * Callback strobe a register bit to high then low (or the inverse)
1006  * in one pass of a single mixer enum control.
1007  *
1008  * Returns 1 for success.
1009  */
1010 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
1011 	struct snd_ctl_elem_value *ucontrol)
1012 {
1013 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1014 	struct soc_mixer_control *mc =
1015 		(struct soc_mixer_control *)kcontrol->private_value;
1016 	unsigned int reg = mc->reg;
1017 	unsigned int shift = mc->shift;
1018 	unsigned int mask = 1 << shift;
1019 	unsigned int invert = mc->invert != 0;
1020 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
1021 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
1022 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
1023 	int err;
1024 
1025 	err = snd_soc_component_update_bits(component, reg, mask, val1);
1026 	if (err < 0)
1027 		return err;
1028 
1029 	return snd_soc_component_update_bits(component, reg, mask, val2);
1030 }
1031 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
1032