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