blob: d072074cc90837d03762d156c131e80b0e156f1f [file] [log] [blame]
Mark Brown70771482014-10-28 22:15:31 +00001/*
2 * soc-ops.c -- Generic ASoC operations
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 */
18
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/pm.h>
24#include <linux/bitops.h>
25#include <linux/ctype.h>
26#include <linux/slab.h>
27#include <sound/core.h>
28#include <sound/jack.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
32#include <sound/soc-dpcm.h>
33#include <sound/initval.h>
34
35/**
36 * snd_soc_info_enum_double - enumerated double mixer info callback
37 * @kcontrol: mixer control
38 * @uinfo: control element information
39 *
40 * Callback to provide information about a double enumerated
41 * mixer control.
42 *
43 * Returns 0 for success.
44 */
45int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
46 struct snd_ctl_elem_info *uinfo)
47{
48 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
49
50 return snd_ctl_enum_info(uinfo, e->shift_l == e->shift_r ? 1 : 2,
51 e->items, e->texts);
52}
53EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
54
55/**
56 * snd_soc_get_enum_double - enumerated double mixer get callback
57 * @kcontrol: mixer control
58 * @ucontrol: control element information
59 *
60 * Callback to get the value of a double enumerated mixer.
61 *
62 * Returns 0 for success.
63 */
64int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
65 struct snd_ctl_elem_value *ucontrol)
66{
67 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
68 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
69 unsigned int val, item;
70 unsigned int reg_val;
71 int ret;
72
73 ret = snd_soc_component_read(component, e->reg, &reg_val);
74 if (ret)
75 return ret;
76 val = (reg_val >> e->shift_l) & e->mask;
77 item = snd_soc_enum_val_to_item(e, val);
78 ucontrol->value.enumerated.item[0] = item;
79 if (e->shift_l != e->shift_r) {
Jaswinder Jassal189f06c2016-08-29 16:06:58 +010080 val = (reg_val >> e->shift_r) & e->mask;
Mark Brown70771482014-10-28 22:15:31 +000081 item = snd_soc_enum_val_to_item(e, val);
82 ucontrol->value.enumerated.item[1] = item;
83 }
84
85 return 0;
86}
87EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
88
89/**
90 * snd_soc_put_enum_double - enumerated double mixer put callback
91 * @kcontrol: mixer control
92 * @ucontrol: control element information
93 *
94 * Callback to set the value of a double enumerated mixer.
95 *
96 * Returns 0 for success.
97 */
98int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
99 struct snd_ctl_elem_value *ucontrol)
100{
101 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
102 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
103 unsigned int *item = ucontrol->value.enumerated.item;
104 unsigned int val;
105 unsigned int mask;
106
107 if (item[0] >= e->items)
108 return -EINVAL;
109 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
110 mask = e->mask << e->shift_l;
111 if (e->shift_l != e->shift_r) {
112 if (item[1] >= e->items)
113 return -EINVAL;
114 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
115 mask |= e->mask << e->shift_r;
116 }
117
118 return snd_soc_component_update_bits(component, e->reg, mask, val);
119}
120EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
121
122/**
123 * snd_soc_read_signed - Read a codec register and interprete as signed value
124 * @component: component
125 * @reg: Register to read
126 * @mask: Mask to use after shifting the register value
127 * @shift: Right shift of register value
128 * @sign_bit: Bit that describes if a number is negative or not.
129 * @signed_val: Pointer to where the read value should be stored
130 *
131 * This functions reads a codec register. The register value is shifted right
132 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
133 * the given registervalue into a signed integer if sign_bit is non-zero.
134 *
135 * Returns 0 on sucess, otherwise an error value
136 */
137static int snd_soc_read_signed(struct snd_soc_component *component,
138 unsigned int reg, unsigned int mask, unsigned int shift,
139 unsigned int sign_bit, int *signed_val)
140{
141 int ret;
142 unsigned int val;
143
144 ret = snd_soc_component_read(component, reg, &val);
145 if (ret < 0)
146 return ret;
147
148 val = (val >> shift) & mask;
149
150 if (!sign_bit) {
151 *signed_val = val;
152 return 0;
153 }
154
155 /* non-negative number */
156 if (!(val & BIT(sign_bit))) {
157 *signed_val = val;
158 return 0;
159 }
160
161 ret = val;
162
163 /*
164 * The register most probably does not contain a full-sized int.
165 * Instead we have an arbitrary number of bits in a signed
166 * representation which has to be translated into a full-sized int.
167 * This is done by filling up all bits above the sign-bit.
168 */
169 ret |= ~((int)(BIT(sign_bit) - 1));
170
171 *signed_val = ret;
172
173 return 0;
174}
175
176/**
177 * snd_soc_info_volsw - single mixer info callback
178 * @kcontrol: mixer control
179 * @uinfo: control element information
180 *
181 * Callback to provide information about a single mixer control, or a double
182 * mixer control that spans 2 registers.
183 *
184 * Returns 0 for success.
185 */
186int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_info *uinfo)
188{
189 struct soc_mixer_control *mc =
190 (struct soc_mixer_control *)kcontrol->private_value;
191 int platform_max;
192
193 if (!mc->platform_max)
194 mc->platform_max = mc->max;
195 platform_max = mc->platform_max;
196
197 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
198 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
199 else
200 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
201
202 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
203 uinfo->value.integer.min = 0;
Sudheer Papothi65c7d022016-01-29 01:37:55 +0530204 if (mc->min < 0 && (uinfo->type == SNDRV_CTL_ELEM_TYPE_INTEGER))
205 uinfo->value.integer.max = platform_max - mc->min;
206 else
207 uinfo->value.integer.max = platform_max;
Mark Brown70771482014-10-28 22:15:31 +0000208 return 0;
209}
210EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
211
212/**
Charles Keepax34198712015-10-14 13:31:24 +0100213 * snd_soc_info_volsw_sx - Mixer info callback for SX TLV controls
214 * @kcontrol: mixer control
215 * @uinfo: control element information
216 *
217 * Callback to provide information about a single mixer control, or a double
218 * mixer control that spans 2 registers of the SX TLV type. SX TLV controls
219 * have a range that represents both positive and negative values either side
220 * of zero but without a sign bit.
221 *
222 * Returns 0 for success.
223 */
224int snd_soc_info_volsw_sx(struct snd_kcontrol *kcontrol,
225 struct snd_ctl_elem_info *uinfo)
226{
227 struct soc_mixer_control *mc =
228 (struct soc_mixer_control *)kcontrol->private_value;
229
230 snd_soc_info_volsw(kcontrol, uinfo);
231 /* Max represents the number of levels in an SX control not the
232 * maximum value, so add the minimum value back on
233 */
234 uinfo->value.integer.max += mc->min;
235
236 return 0;
237}
238EXPORT_SYMBOL_GPL(snd_soc_info_volsw_sx);
239
240/**
Mark Brown70771482014-10-28 22:15:31 +0000241 * snd_soc_get_volsw - single mixer get callback
242 * @kcontrol: mixer control
243 * @ucontrol: control element information
244 *
245 * Callback to get the value of a single mixer control, or a double mixer
246 * control that spans 2 registers.
247 *
248 * Returns 0 for success.
249 */
250int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
251 struct snd_ctl_elem_value *ucontrol)
252{
253 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
254 struct soc_mixer_control *mc =
255 (struct soc_mixer_control *)kcontrol->private_value;
256 unsigned int reg = mc->reg;
257 unsigned int reg2 = mc->rreg;
258 unsigned int shift = mc->shift;
259 unsigned int rshift = mc->rshift;
260 int max = mc->max;
261 int min = mc->min;
262 int sign_bit = mc->sign_bit;
263 unsigned int mask = (1 << fls(max)) - 1;
264 unsigned int invert = mc->invert;
265 int val;
266 int ret;
267
268 if (sign_bit)
269 mask = BIT(sign_bit + 1) - 1;
270
271 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
272 if (ret)
273 return ret;
274
275 ucontrol->value.integer.value[0] = val - min;
276 if (invert)
277 ucontrol->value.integer.value[0] =
278 max - ucontrol->value.integer.value[0];
279
280 if (snd_soc_volsw_is_stereo(mc)) {
281 if (reg == reg2)
282 ret = snd_soc_read_signed(component, reg, mask, rshift,
283 sign_bit, &val);
284 else
285 ret = snd_soc_read_signed(component, reg2, mask, shift,
286 sign_bit, &val);
287 if (ret)
288 return ret;
289
290 ucontrol->value.integer.value[1] = val - min;
291 if (invert)
292 ucontrol->value.integer.value[1] =
293 max - ucontrol->value.integer.value[1];
294 }
295
296 return 0;
297}
298EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
299
300/**
301 * snd_soc_put_volsw - single mixer put callback
302 * @kcontrol: mixer control
303 * @ucontrol: control element information
304 *
305 * Callback to set the value of a single mixer control, or a double mixer
306 * control that spans 2 registers.
307 *
308 * Returns 0 for success.
309 */
310int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
311 struct snd_ctl_elem_value *ucontrol)
312{
313 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
314 struct soc_mixer_control *mc =
315 (struct soc_mixer_control *)kcontrol->private_value;
316 unsigned int reg = mc->reg;
317 unsigned int reg2 = mc->rreg;
318 unsigned int shift = mc->shift;
319 unsigned int rshift = mc->rshift;
320 int max = mc->max;
321 int min = mc->min;
322 unsigned int sign_bit = mc->sign_bit;
323 unsigned int mask = (1 << fls(max)) - 1;
324 unsigned int invert = mc->invert;
325 int err;
326 bool type_2r = false;
327 unsigned int val2 = 0;
328 unsigned int val, val_mask;
329
330 if (sign_bit)
331 mask = BIT(sign_bit + 1) - 1;
332
333 val = ((ucontrol->value.integer.value[0] + min) & mask);
334 if (invert)
335 val = max - val;
336 val_mask = mask << shift;
337 val = val << shift;
338 if (snd_soc_volsw_is_stereo(mc)) {
339 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
340 if (invert)
341 val2 = max - val2;
342 if (reg == reg2) {
343 val_mask |= mask << rshift;
344 val |= val2 << rshift;
345 } else {
346 val2 = val2 << shift;
347 type_2r = true;
348 }
349 }
350 err = snd_soc_component_update_bits(component, reg, val_mask, val);
351 if (err < 0)
352 return err;
353
354 if (type_2r)
355 err = snd_soc_component_update_bits(component, reg2, val_mask,
356 val2);
357
358 return err;
359}
360EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
361
362/**
363 * snd_soc_get_volsw_sx - single mixer get callback
364 * @kcontrol: mixer control
365 * @ucontrol: control element information
366 *
367 * Callback to get the value of a single mixer control, or a double mixer
368 * control that spans 2 registers.
369 *
370 * Returns 0 for success.
371 */
372int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
373 struct snd_ctl_elem_value *ucontrol)
374{
375 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
376 struct soc_mixer_control *mc =
377 (struct soc_mixer_control *)kcontrol->private_value;
378 unsigned int reg = mc->reg;
379 unsigned int reg2 = mc->rreg;
380 unsigned int shift = mc->shift;
381 unsigned int rshift = mc->rshift;
382 int max = mc->max;
383 int min = mc->min;
384 int mask = (1 << (fls(min + max) - 1)) - 1;
385 unsigned int val;
386 int ret;
387
388 ret = snd_soc_component_read(component, reg, &val);
389 if (ret < 0)
390 return ret;
391
392 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
393
394 if (snd_soc_volsw_is_stereo(mc)) {
395 ret = snd_soc_component_read(component, reg2, &val);
396 if (ret < 0)
397 return ret;
398
399 val = ((val >> rshift) - min) & mask;
400 ucontrol->value.integer.value[1] = val;
401 }
402
403 return 0;
404}
405EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
406
407/**
408 * snd_soc_put_volsw_sx - double mixer set callback
409 * @kcontrol: mixer control
Randy Dunlap9a11ef7f2015-11-23 17:37:54 -0800410 * @ucontrol: control element information
Mark Brown70771482014-10-28 22:15:31 +0000411 *
412 * Callback to set the value of a double mixer control that spans 2 registers.
413 *
414 * Returns 0 for success.
415 */
416int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
417 struct snd_ctl_elem_value *ucontrol)
418{
419 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
420 struct soc_mixer_control *mc =
421 (struct soc_mixer_control *)kcontrol->private_value;
422
423 unsigned int reg = mc->reg;
424 unsigned int reg2 = mc->rreg;
425 unsigned int shift = mc->shift;
426 unsigned int rshift = mc->rshift;
427 int max = mc->max;
428 int min = mc->min;
429 int mask = (1 << (fls(min + max) - 1)) - 1;
430 int err = 0;
431 unsigned int val, val_mask, val2 = 0;
432
433 val_mask = mask << shift;
434 val = (ucontrol->value.integer.value[0] + min) & mask;
435 val = val << shift;
436
437 err = snd_soc_component_update_bits(component, reg, val_mask, val);
438 if (err < 0)
439 return err;
440
441 if (snd_soc_volsw_is_stereo(mc)) {
442 val_mask = mask << rshift;
443 val2 = (ucontrol->value.integer.value[1] + min) & mask;
444 val2 = val2 << rshift;
445
446 err = snd_soc_component_update_bits(component, reg2, val_mask,
447 val2);
448 }
449 return err;
450}
451EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
452
453/**
454 * snd_soc_info_volsw_range - single mixer info callback with range.
455 * @kcontrol: mixer control
456 * @uinfo: control element information
457 *
458 * Callback to provide information, within a range, about a single
459 * mixer control.
460 *
461 * returns 0 for success.
462 */
463int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
464 struct snd_ctl_elem_info *uinfo)
465{
466 struct soc_mixer_control *mc =
467 (struct soc_mixer_control *)kcontrol->private_value;
468 int platform_max;
469 int min = mc->min;
470
471 if (!mc->platform_max)
472 mc->platform_max = mc->max;
473 platform_max = mc->platform_max;
474
475 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
476 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
477 uinfo->value.integer.min = 0;
478 uinfo->value.integer.max = platform_max - min;
479
480 return 0;
481}
482EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
483
484/**
485 * snd_soc_put_volsw_range - single mixer put value callback with range.
486 * @kcontrol: mixer control
487 * @ucontrol: control element information
488 *
489 * Callback to set the value, within a range, for a single mixer control.
490 *
491 * Returns 0 for success.
492 */
493int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
494 struct snd_ctl_elem_value *ucontrol)
495{
496 struct soc_mixer_control *mc =
497 (struct soc_mixer_control *)kcontrol->private_value;
498 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
499 unsigned int reg = mc->reg;
500 unsigned int rreg = mc->rreg;
501 unsigned int shift = mc->shift;
502 int min = mc->min;
503 int max = mc->max;
504 unsigned int mask = (1 << fls(max)) - 1;
505 unsigned int invert = mc->invert;
506 unsigned int val, val_mask;
507 int ret;
508
509 if (invert)
510 val = (max - ucontrol->value.integer.value[0]) & mask;
511 else
512 val = ((ucontrol->value.integer.value[0] + min) & mask);
513 val_mask = mask << shift;
514 val = val << shift;
515
516 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
517 if (ret < 0)
518 return ret;
519
520 if (snd_soc_volsw_is_stereo(mc)) {
521 if (invert)
522 val = (max - ucontrol->value.integer.value[1]) & mask;
523 else
524 val = ((ucontrol->value.integer.value[1] + min) & mask);
525 val_mask = mask << shift;
526 val = val << shift;
527
528 ret = snd_soc_component_update_bits(component, rreg, val_mask,
529 val);
530 }
531
532 return ret;
533}
534EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
535
536/**
537 * snd_soc_get_volsw_range - single mixer get callback with range
538 * @kcontrol: mixer control
539 * @ucontrol: control element information
540 *
541 * Callback to get the value, within a range, of a single mixer control.
542 *
543 * Returns 0 for success.
544 */
545int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
546 struct snd_ctl_elem_value *ucontrol)
547{
548 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
549 struct soc_mixer_control *mc =
550 (struct soc_mixer_control *)kcontrol->private_value;
551 unsigned int reg = mc->reg;
552 unsigned int rreg = mc->rreg;
553 unsigned int shift = mc->shift;
554 int min = mc->min;
555 int max = mc->max;
556 unsigned int mask = (1 << fls(max)) - 1;
557 unsigned int invert = mc->invert;
558 unsigned int val;
559 int ret;
560
561 ret = snd_soc_component_read(component, reg, &val);
562 if (ret)
563 return ret;
564
565 ucontrol->value.integer.value[0] = (val >> shift) & mask;
566 if (invert)
567 ucontrol->value.integer.value[0] =
568 max - ucontrol->value.integer.value[0];
569 else
570 ucontrol->value.integer.value[0] =
571 ucontrol->value.integer.value[0] - min;
572
573 if (snd_soc_volsw_is_stereo(mc)) {
574 ret = snd_soc_component_read(component, rreg, &val);
575 if (ret)
576 return ret;
577
578 ucontrol->value.integer.value[1] = (val >> shift) & mask;
579 if (invert)
580 ucontrol->value.integer.value[1] =
581 max - ucontrol->value.integer.value[1];
582 else
583 ucontrol->value.integer.value[1] =
584 ucontrol->value.integer.value[1] - min;
585 }
586
587 return 0;
588}
589EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
590
591/**
592 * snd_soc_limit_volume - Set new limit to an existing volume control.
593 *
Lars-Peter Clausen26d9ca32015-10-18 17:04:33 +0200594 * @card: where to look for the control
Mark Brown70771482014-10-28 22:15:31 +0000595 * @name: Name of the control
596 * @max: new maximum limit
597 *
598 * Return 0 for success, else error.
599 */
Lars-Peter Clausen26d9ca32015-10-18 17:04:33 +0200600int snd_soc_limit_volume(struct snd_soc_card *card,
Mark Brown70771482014-10-28 22:15:31 +0000601 const char *name, int max)
602{
Lars-Peter Clausen26d9ca32015-10-18 17:04:33 +0200603 struct snd_card *snd_card = card->snd_card;
Mark Brown70771482014-10-28 22:15:31 +0000604 struct snd_kcontrol *kctl;
605 struct soc_mixer_control *mc;
606 int found = 0;
607 int ret = -EINVAL;
608
609 /* Sanity check for name and max */
610 if (unlikely(!name || max <= 0))
611 return -EINVAL;
612
Lars-Peter Clausen26d9ca32015-10-18 17:04:33 +0200613 list_for_each_entry(kctl, &snd_card->controls, list) {
Mark Brown70771482014-10-28 22:15:31 +0000614 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
615 found = 1;
616 break;
617 }
618 }
619 if (found) {
620 mc = (struct soc_mixer_control *)kctl->private_value;
621 if (max <= mc->max) {
622 mc->platform_max = max;
623 ret = 0;
624 }
625 }
626 return ret;
627}
628EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
629
630int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
631 struct snd_ctl_elem_info *uinfo)
632{
633 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
634 struct soc_bytes *params = (void *)kcontrol->private_value;
635
636 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
637 uinfo->count = params->num_regs * component->val_bytes;
638
639 return 0;
640}
641EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
642
643int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
644 struct snd_ctl_elem_value *ucontrol)
645{
646 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
647 struct soc_bytes *params = (void *)kcontrol->private_value;
648 int ret;
649
650 if (component->regmap)
651 ret = regmap_raw_read(component->regmap, params->base,
652 ucontrol->value.bytes.data,
653 params->num_regs * component->val_bytes);
654 else
655 ret = -EINVAL;
656
657 /* Hide any masked bytes to ensure consistent data reporting */
658 if (ret == 0 && params->mask) {
659 switch (component->val_bytes) {
660 case 1:
661 ucontrol->value.bytes.data[0] &= ~params->mask;
662 break;
663 case 2:
664 ((u16 *)(&ucontrol->value.bytes.data))[0]
665 &= cpu_to_be16(~params->mask);
666 break;
667 case 4:
668 ((u32 *)(&ucontrol->value.bytes.data))[0]
669 &= cpu_to_be32(~params->mask);
670 break;
671 default:
672 return -EINVAL;
673 }
674 }
675
676 return ret;
677}
678EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
679
680int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
681 struct snd_ctl_elem_value *ucontrol)
682{
683 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
684 struct soc_bytes *params = (void *)kcontrol->private_value;
685 int ret, len;
686 unsigned int val, mask;
687 void *data;
688
689 if (!component->regmap || !params->num_regs)
690 return -EINVAL;
691
692 len = params->num_regs * component->val_bytes;
693
694 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
695 if (!data)
696 return -ENOMEM;
697
698 /*
699 * If we've got a mask then we need to preserve the register
700 * bits. We shouldn't modify the incoming data so take a
701 * copy.
702 */
703 if (params->mask) {
704 ret = regmap_read(component->regmap, params->base, &val);
705 if (ret != 0)
706 goto out;
707
708 val &= params->mask;
709
710 switch (component->val_bytes) {
711 case 1:
712 ((u8 *)data)[0] &= ~params->mask;
713 ((u8 *)data)[0] |= val;
714 break;
715 case 2:
716 mask = ~params->mask;
717 ret = regmap_parse_val(component->regmap,
718 &mask, &mask);
719 if (ret != 0)
720 goto out;
721
722 ((u16 *)data)[0] &= mask;
723
724 ret = regmap_parse_val(component->regmap,
725 &val, &val);
726 if (ret != 0)
727 goto out;
728
729 ((u16 *)data)[0] |= val;
730 break;
731 case 4:
732 mask = ~params->mask;
733 ret = regmap_parse_val(component->regmap,
734 &mask, &mask);
735 if (ret != 0)
736 goto out;
737
738 ((u32 *)data)[0] &= mask;
739
740 ret = regmap_parse_val(component->regmap,
741 &val, &val);
742 if (ret != 0)
743 goto out;
744
745 ((u32 *)data)[0] |= val;
746 break;
747 default:
748 ret = -EINVAL;
749 goto out;
750 }
751 }
752
753 ret = regmap_raw_write(component->regmap, params->base,
754 data, len);
755
756out:
757 kfree(data);
758
759 return ret;
760}
761EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
762
763int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
764 struct snd_ctl_elem_info *ucontrol)
765{
766 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
767
768 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
769 ucontrol->count = params->max;
770
771 return 0;
772}
773EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
774
775int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
776 unsigned int size, unsigned int __user *tlv)
777{
778 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
779 unsigned int count = size < params->max ? size : params->max;
780 int ret = -ENXIO;
781
782 switch (op_flag) {
783 case SNDRV_CTL_TLV_OP_READ:
784 if (params->get)
Mythri P Ka1e5e7e92015-11-09 23:20:00 +0530785 ret = params->get(kcontrol, tlv, count);
Mark Brown70771482014-10-28 22:15:31 +0000786 break;
787 case SNDRV_CTL_TLV_OP_WRITE:
788 if (params->put)
Mythri P Ka1e5e7e92015-11-09 23:20:00 +0530789 ret = params->put(kcontrol, tlv, count);
Mark Brown70771482014-10-28 22:15:31 +0000790 break;
791 }
792 return ret;
793}
794EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
795
796/**
797 * snd_soc_info_xr_sx - signed multi register info callback
798 * @kcontrol: mreg control
799 * @uinfo: control element information
800 *
801 * Callback to provide information of a control that can
802 * span multiple codec registers which together
803 * forms a single signed value in a MSB/LSB manner.
804 *
805 * Returns 0 for success.
806 */
807int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
808 struct snd_ctl_elem_info *uinfo)
809{
810 struct soc_mreg_control *mc =
811 (struct soc_mreg_control *)kcontrol->private_value;
812 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
813 uinfo->count = 1;
814 uinfo->value.integer.min = mc->min;
815 uinfo->value.integer.max = mc->max;
816
817 return 0;
818}
819EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
820
821/**
822 * snd_soc_get_xr_sx - signed multi register get callback
823 * @kcontrol: mreg control
824 * @ucontrol: control element information
825 *
826 * Callback to get the value of a control that can span
827 * multiple codec registers which together forms a single
828 * signed value in a MSB/LSB manner. The control supports
829 * specifying total no of bits used to allow for bitfields
830 * across the multiple codec registers.
831 *
832 * Returns 0 for success.
833 */
834int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
835 struct snd_ctl_elem_value *ucontrol)
836{
837 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
838 struct soc_mreg_control *mc =
839 (struct soc_mreg_control *)kcontrol->private_value;
840 unsigned int regbase = mc->regbase;
841 unsigned int regcount = mc->regcount;
842 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
843 unsigned int regwmask = (1<<regwshift)-1;
844 unsigned int invert = mc->invert;
845 unsigned long mask = (1UL<<mc->nbits)-1;
846 long min = mc->min;
847 long max = mc->max;
848 long val = 0;
849 unsigned int regval;
850 unsigned int i;
851 int ret;
852
853 for (i = 0; i < regcount; i++) {
854 ret = snd_soc_component_read(component, regbase+i, &regval);
855 if (ret)
856 return ret;
857 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
858 }
859 val &= mask;
860 if (min < 0 && val > max)
861 val |= ~mask;
862 if (invert)
863 val = max - val;
864 ucontrol->value.integer.value[0] = val;
865
866 return 0;
867}
868EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
869
870/**
871 * snd_soc_put_xr_sx - signed multi register get callback
872 * @kcontrol: mreg control
873 * @ucontrol: control element information
874 *
875 * Callback to set the value of a control that can span
876 * multiple codec registers which together forms a single
877 * signed value in a MSB/LSB manner. The control supports
878 * specifying total no of bits used to allow for bitfields
879 * across the multiple codec registers.
880 *
881 * Returns 0 for success.
882 */
883int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
884 struct snd_ctl_elem_value *ucontrol)
885{
886 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
887 struct soc_mreg_control *mc =
888 (struct soc_mreg_control *)kcontrol->private_value;
889 unsigned int regbase = mc->regbase;
890 unsigned int regcount = mc->regcount;
891 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
892 unsigned int regwmask = (1<<regwshift)-1;
893 unsigned int invert = mc->invert;
894 unsigned long mask = (1UL<<mc->nbits)-1;
895 long max = mc->max;
896 long val = ucontrol->value.integer.value[0];
897 unsigned int i, regval, regmask;
898 int err;
899
900 if (invert)
901 val = max - val;
902 val &= mask;
903 for (i = 0; i < regcount; i++) {
904 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
905 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
906 err = snd_soc_component_update_bits(component, regbase+i,
907 regmask, regval);
908 if (err < 0)
909 return err;
910 }
911
912 return 0;
913}
914EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
915
916/**
917 * snd_soc_get_strobe - strobe get callback
918 * @kcontrol: mixer control
919 * @ucontrol: control element information
920 *
921 * Callback get the value of a strobe mixer control.
922 *
923 * Returns 0 for success.
924 */
925int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
926 struct snd_ctl_elem_value *ucontrol)
927{
928 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
929 struct soc_mixer_control *mc =
930 (struct soc_mixer_control *)kcontrol->private_value;
931 unsigned int reg = mc->reg;
932 unsigned int shift = mc->shift;
933 unsigned int mask = 1 << shift;
934 unsigned int invert = mc->invert != 0;
935 unsigned int val;
936 int ret;
937
938 ret = snd_soc_component_read(component, reg, &val);
939 if (ret)
940 return ret;
941
942 val &= mask;
943
944 if (shift != 0 && val != 0)
945 val = val >> shift;
946 ucontrol->value.enumerated.item[0] = val ^ invert;
947
948 return 0;
949}
950EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
951
952/**
953 * snd_soc_put_strobe - strobe put callback
954 * @kcontrol: mixer control
955 * @ucontrol: control element information
956 *
957 * Callback strobe a register bit to high then low (or the inverse)
958 * in one pass of a single mixer enum control.
959 *
960 * Returns 1 for success.
961 */
962int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
963 struct snd_ctl_elem_value *ucontrol)
964{
965 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
966 struct soc_mixer_control *mc =
967 (struct soc_mixer_control *)kcontrol->private_value;
968 unsigned int reg = mc->reg;
969 unsigned int shift = mc->shift;
970 unsigned int mask = 1 << shift;
971 unsigned int invert = mc->invert != 0;
972 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
973 unsigned int val1 = (strobe ^ invert) ? mask : 0;
974 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
975 int err;
976
977 err = snd_soc_component_update_bits(component, reg, mask, val1);
978 if (err < 0)
979 return err;
980
981 return snd_soc_component_update_bits(component, reg, mask, val2);
982}
983EXPORT_SYMBOL_GPL(snd_soc_put_strobe);