File Index
Files and directories
Sign in
FILE

soc-ops.c

File preview with syntax highlighting.

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