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