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