blob: 63e40b5275a0893080a3c7e25c47bb32bb3cbde5 [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;
Xiaojun Sang0d873de2016-08-23 15:53:39 +0800204 if (uinfo->type == SNDRV_CTL_ELEM_TYPE_INTEGER)
Sudheer Papothi65c7d022016-01-29 01:37:55 +0530205 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{
Charles Keepax34198712015-10-14 13:31:24 +0100227 snd_soc_info_volsw(kcontrol, uinfo);
228 /* Max represents the number of levels in an SX control not the
Xiaojun Sang0d873de2016-08-23 15:53:39 +0800229 * maximum value.
230 * uinfo->value.integer.max is set to number of levels
231 * in snd_soc_info_volsw. No further adjustment is necessary.
Charles Keepax34198712015-10-14 13:31:24 +0100232 */
Charles Keepax34198712015-10-14 13:31:24 +0100233
234 return 0;
235}
236EXPORT_SYMBOL_GPL(snd_soc_info_volsw_sx);
237
238/**
Mark Brown70771482014-10-28 22:15:31 +0000239 * snd_soc_get_volsw - single mixer get callback
240 * @kcontrol: mixer control
241 * @ucontrol: control element information
242 *
243 * Callback to get the value of a single mixer control, or a double mixer
244 * control that spans 2 registers.
245 *
246 * Returns 0 for success.
247 */
248int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
249 struct snd_ctl_elem_value *ucontrol)
250{
251 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
252 struct soc_mixer_control *mc =
253 (struct soc_mixer_control *)kcontrol->private_value;
254 unsigned int reg = mc->reg;
255 unsigned int reg2 = mc->rreg;
256 unsigned int shift = mc->shift;
257 unsigned int rshift = mc->rshift;
258 int max = mc->max;
259 int min = mc->min;
260 int sign_bit = mc->sign_bit;
261 unsigned int mask = (1 << fls(max)) - 1;
262 unsigned int invert = mc->invert;
263 int val;
264 int ret;
265
266 if (sign_bit)
267 mask = BIT(sign_bit + 1) - 1;
268
269 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
270 if (ret)
271 return ret;
272
273 ucontrol->value.integer.value[0] = val - min;
274 if (invert)
275 ucontrol->value.integer.value[0] =
276 max - ucontrol->value.integer.value[0];
277
278 if (snd_soc_volsw_is_stereo(mc)) {
279 if (reg == reg2)
280 ret = snd_soc_read_signed(component, reg, mask, rshift,
281 sign_bit, &val);
282 else
283 ret = snd_soc_read_signed(component, reg2, mask, shift,
284 sign_bit, &val);
285 if (ret)
286 return ret;
287
288 ucontrol->value.integer.value[1] = val - min;
289 if (invert)
290 ucontrol->value.integer.value[1] =
291 max - ucontrol->value.integer.value[1];
292 }
293
294 return 0;
295}
296EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
297
298/**
299 * snd_soc_put_volsw - single mixer put callback
300 * @kcontrol: mixer control
301 * @ucontrol: control element information
302 *
303 * Callback to set the value of a single mixer control, or a double mixer
304 * control that spans 2 registers.
305 *
306 * Returns 0 for success.
307 */
308int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
309 struct snd_ctl_elem_value *ucontrol)
310{
311 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
312 struct soc_mixer_control *mc =
313 (struct soc_mixer_control *)kcontrol->private_value;
314 unsigned int reg = mc->reg;
315 unsigned int reg2 = mc->rreg;
316 unsigned int shift = mc->shift;
317 unsigned int rshift = mc->rshift;
318 int max = mc->max;
319 int min = mc->min;
320 unsigned int sign_bit = mc->sign_bit;
321 unsigned int mask = (1 << fls(max)) - 1;
322 unsigned int invert = mc->invert;
323 int err;
324 bool type_2r = false;
325 unsigned int val2 = 0;
326 unsigned int val, val_mask;
327
328 if (sign_bit)
329 mask = BIT(sign_bit + 1) - 1;
330
331 val = ((ucontrol->value.integer.value[0] + min) & mask);
332 if (invert)
333 val = max - val;
334 val_mask = mask << shift;
335 val = val << shift;
336 if (snd_soc_volsw_is_stereo(mc)) {
337 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
338 if (invert)
339 val2 = max - val2;
340 if (reg == reg2) {
341 val_mask |= mask << rshift;
342 val |= val2 << rshift;
343 } else {
344 val2 = val2 << shift;
345 type_2r = true;
346 }
347 }
348 err = snd_soc_component_update_bits(component, reg, val_mask, val);
349 if (err < 0)
350 return err;
351
352 if (type_2r)
353 err = snd_soc_component_update_bits(component, reg2, val_mask,
354 val2);
355
356 return err;
357}
358EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
359
360/**
361 * snd_soc_get_volsw_sx - single mixer get callback
362 * @kcontrol: mixer control
363 * @ucontrol: control element information
364 *
365 * Callback to get the value of a single mixer control, or a double mixer
366 * control that spans 2 registers.
367 *
368 * Returns 0 for success.
369 */
370int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
371 struct snd_ctl_elem_value *ucontrol)
372{
373 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
374 struct soc_mixer_control *mc =
375 (struct soc_mixer_control *)kcontrol->private_value;
376 unsigned int reg = mc->reg;
377 unsigned int reg2 = mc->rreg;
378 unsigned int shift = mc->shift;
379 unsigned int rshift = mc->rshift;
380 int max = mc->max;
381 int min = mc->min;
Rohit kumaraaa88fe2018-08-09 18:21:50 +0530382 unsigned int mask = (1U << (fls(min + max) - 1)) - 1;
Mark Brown70771482014-10-28 22:15:31 +0000383 unsigned int val;
384 int ret;
385
386 ret = snd_soc_component_read(component, reg, &val);
387 if (ret < 0)
388 return ret;
389
390 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
391
392 if (snd_soc_volsw_is_stereo(mc)) {
393 ret = snd_soc_component_read(component, reg2, &val);
394 if (ret < 0)
395 return ret;
396
397 val = ((val >> rshift) - min) & mask;
398 ucontrol->value.integer.value[1] = val;
399 }
400
401 return 0;
402}
403EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
404
405/**
406 * snd_soc_put_volsw_sx - double mixer set callback
407 * @kcontrol: mixer control
Randy Dunlap9a11ef7f2015-11-23 17:37:54 -0800408 * @ucontrol: control element information
Mark Brown70771482014-10-28 22:15:31 +0000409 *
410 * Callback to set the value of a double mixer control that spans 2 registers.
411 *
412 * Returns 0 for success.
413 */
414int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
415 struct snd_ctl_elem_value *ucontrol)
416{
417 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
418 struct soc_mixer_control *mc =
419 (struct soc_mixer_control *)kcontrol->private_value;
420
421 unsigned int reg = mc->reg;
422 unsigned int reg2 = mc->rreg;
423 unsigned int shift = mc->shift;
424 unsigned int rshift = mc->rshift;
425 int max = mc->max;
426 int min = mc->min;
Rohit kumaraaa88fe2018-08-09 18:21:50 +0530427 unsigned int mask = (1U << (fls(min + max) - 1)) - 1;
Mark Brown70771482014-10-28 22:15:31 +0000428 int err = 0;
429 unsigned int val, val_mask, val2 = 0;
430
431 val_mask = mask << shift;
432 val = (ucontrol->value.integer.value[0] + min) & mask;
433 val = val << shift;
434
435 err = snd_soc_component_update_bits(component, reg, val_mask, val);
436 if (err < 0)
437 return err;
438
439 if (snd_soc_volsw_is_stereo(mc)) {
440 val_mask = mask << rshift;
441 val2 = (ucontrol->value.integer.value[1] + min) & mask;
442 val2 = val2 << rshift;
443
444 err = snd_soc_component_update_bits(component, reg2, val_mask,
445 val2);
446 }
447 return err;
448}
449EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
450
451/**
452 * snd_soc_info_volsw_range - single mixer info callback with range.
453 * @kcontrol: mixer control
454 * @uinfo: control element information
455 *
456 * Callback to provide information, within a range, about a single
457 * mixer control.
458 *
459 * returns 0 for success.
460 */
461int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
462 struct snd_ctl_elem_info *uinfo)
463{
464 struct soc_mixer_control *mc =
465 (struct soc_mixer_control *)kcontrol->private_value;
466 int platform_max;
467 int min = mc->min;
468
469 if (!mc->platform_max)
470 mc->platform_max = mc->max;
471 platform_max = mc->platform_max;
472
473 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
474 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
475 uinfo->value.integer.min = 0;
476 uinfo->value.integer.max = platform_max - min;
477
478 return 0;
479}
480EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
481
482/**
483 * snd_soc_put_volsw_range - single mixer put value callback with range.
484 * @kcontrol: mixer control
485 * @ucontrol: control element information
486 *
487 * Callback to set the value, within a range, for a single mixer control.
488 *
489 * Returns 0 for success.
490 */
491int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
492 struct snd_ctl_elem_value *ucontrol)
493{
494 struct soc_mixer_control *mc =
495 (struct soc_mixer_control *)kcontrol->private_value;
496 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
497 unsigned int reg = mc->reg;
498 unsigned int rreg = mc->rreg;
499 unsigned int shift = mc->shift;
500 int min = mc->min;
501 int max = mc->max;
502 unsigned int mask = (1 << fls(max)) - 1;
503 unsigned int invert = mc->invert;
504 unsigned int val, val_mask;
505 int ret;
506
507 if (invert)
508 val = (max - ucontrol->value.integer.value[0]) & mask;
509 else
510 val = ((ucontrol->value.integer.value[0] + min) & mask);
511 val_mask = mask << shift;
512 val = val << shift;
513
514 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
515 if (ret < 0)
516 return ret;
517
518 if (snd_soc_volsw_is_stereo(mc)) {
519 if (invert)
520 val = (max - ucontrol->value.integer.value[1]) & mask;
521 else
522 val = ((ucontrol->value.integer.value[1] + min) & mask);
523 val_mask = mask << shift;
524 val = val << shift;
525
526 ret = snd_soc_component_update_bits(component, rreg, val_mask,
527 val);
528 }
529
530 return ret;
531}
532EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
533
534/**
535 * snd_soc_get_volsw_range - single mixer get callback with range
536 * @kcontrol: mixer control
537 * @ucontrol: control element information
538 *
539 * Callback to get the value, within a range, of a single mixer control.
540 *
541 * Returns 0 for success.
542 */
543int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
544 struct snd_ctl_elem_value *ucontrol)
545{
546 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
547 struct soc_mixer_control *mc =
548 (struct soc_mixer_control *)kcontrol->private_value;
549 unsigned int reg = mc->reg;
550 unsigned int rreg = mc->rreg;
551 unsigned int shift = mc->shift;
552 int min = mc->min;
553 int max = mc->max;
554 unsigned int mask = (1 << fls(max)) - 1;
555 unsigned int invert = mc->invert;
556 unsigned int val;
557 int ret;
558
559 ret = snd_soc_component_read(component, reg, &val);
560 if (ret)
561 return ret;
562
563 ucontrol->value.integer.value[0] = (val >> shift) & mask;
564 if (invert)
565 ucontrol->value.integer.value[0] =
566 max - ucontrol->value.integer.value[0];
567 else
568 ucontrol->value.integer.value[0] =
569 ucontrol->value.integer.value[0] - min;
570
571 if (snd_soc_volsw_is_stereo(mc)) {
572 ret = snd_soc_component_read(component, rreg, &val);
573 if (ret)
574 return ret;
575
576 ucontrol->value.integer.value[1] = (val >> shift) & mask;
577 if (invert)
578 ucontrol->value.integer.value[1] =
579 max - ucontrol->value.integer.value[1];
580 else
581 ucontrol->value.integer.value[1] =
582 ucontrol->value.integer.value[1] - min;
583 }
584
585 return 0;
586}
587EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
588
589/**
590 * snd_soc_limit_volume - Set new limit to an existing volume control.
591 *
Lars-Peter Clausen26d9ca32015-10-18 17:04:33 +0200592 * @card: where to look for the control
Mark Brown70771482014-10-28 22:15:31 +0000593 * @name: Name of the control
594 * @max: new maximum limit
595 *
596 * Return 0 for success, else error.
597 */
Lars-Peter Clausen26d9ca32015-10-18 17:04:33 +0200598int snd_soc_limit_volume(struct snd_soc_card *card,
Mark Brown70771482014-10-28 22:15:31 +0000599 const char *name, int max)
600{
Lars-Peter Clausen26d9ca32015-10-18 17:04:33 +0200601 struct snd_card *snd_card = card->snd_card;
Mark Brown70771482014-10-28 22:15:31 +0000602 struct snd_kcontrol *kctl;
603 struct soc_mixer_control *mc;
604 int found = 0;
605 int ret = -EINVAL;
606
607 /* Sanity check for name and max */
608 if (unlikely(!name || max <= 0))
609 return -EINVAL;
610
Lars-Peter Clausen26d9ca32015-10-18 17:04:33 +0200611 list_for_each_entry(kctl, &snd_card->controls, list) {
Mark Brown70771482014-10-28 22:15:31 +0000612 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
613 found = 1;
614 break;
615 }
616 }
617 if (found) {
618 mc = (struct soc_mixer_control *)kctl->private_value;
619 if (max <= mc->max) {
620 mc->platform_max = max;
621 ret = 0;
622 }
623 }
624 return ret;
625}
626EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
627
628int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
629 struct snd_ctl_elem_info *uinfo)
630{
631 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
632 struct soc_bytes *params = (void *)kcontrol->private_value;
633
634 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
635 uinfo->count = params->num_regs * component->val_bytes;
636
637 return 0;
638}
639EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
640
641int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
642 struct snd_ctl_elem_value *ucontrol)
643{
644 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
645 struct soc_bytes *params = (void *)kcontrol->private_value;
646 int ret;
647
648 if (component->regmap)
649 ret = regmap_raw_read(component->regmap, params->base,
650 ucontrol->value.bytes.data,
651 params->num_regs * component->val_bytes);
652 else
653 ret = -EINVAL;
654
655 /* Hide any masked bytes to ensure consistent data reporting */
656 if (ret == 0 && params->mask) {
657 switch (component->val_bytes) {
658 case 1:
659 ucontrol->value.bytes.data[0] &= ~params->mask;
660 break;
661 case 2:
662 ((u16 *)(&ucontrol->value.bytes.data))[0]
663 &= cpu_to_be16(~params->mask);
664 break;
665 case 4:
666 ((u32 *)(&ucontrol->value.bytes.data))[0]
667 &= cpu_to_be32(~params->mask);
668 break;
669 default:
670 return -EINVAL;
671 }
672 }
673
674 return ret;
675}
676EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
677
678int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
679 struct snd_ctl_elem_value *ucontrol)
680{
681 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
682 struct soc_bytes *params = (void *)kcontrol->private_value;
683 int ret, len;
684 unsigned int val, mask;
685 void *data;
686
687 if (!component->regmap || !params->num_regs)
688 return -EINVAL;
689
690 len = params->num_regs * component->val_bytes;
691
692 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
693 if (!data)
694 return -ENOMEM;
695
696 /*
697 * If we've got a mask then we need to preserve the register
698 * bits. We shouldn't modify the incoming data so take a
699 * copy.
700 */
701 if (params->mask) {
702 ret = regmap_read(component->regmap, params->base, &val);
703 if (ret != 0)
704 goto out;
705
706 val &= params->mask;
707
708 switch (component->val_bytes) {
709 case 1:
710 ((u8 *)data)[0] &= ~params->mask;
711 ((u8 *)data)[0] |= val;
712 break;
713 case 2:
714 mask = ~params->mask;
715 ret = regmap_parse_val(component->regmap,
716 &mask, &mask);
717 if (ret != 0)
718 goto out;
719
720 ((u16 *)data)[0] &= mask;
721
722 ret = regmap_parse_val(component->regmap,
723 &val, &val);
724 if (ret != 0)
725 goto out;
726
727 ((u16 *)data)[0] |= val;
728 break;
729 case 4:
730 mask = ~params->mask;
731 ret = regmap_parse_val(component->regmap,
732 &mask, &mask);
733 if (ret != 0)
734 goto out;
735
736 ((u32 *)data)[0] &= mask;
737
738 ret = regmap_parse_val(component->regmap,
739 &val, &val);
740 if (ret != 0)
741 goto out;
742
743 ((u32 *)data)[0] |= val;
744 break;
745 default:
746 ret = -EINVAL;
747 goto out;
748 }
749 }
750
751 ret = regmap_raw_write(component->regmap, params->base,
752 data, len);
753
754out:
755 kfree(data);
756
757 return ret;
758}
759EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
760
761int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
762 struct snd_ctl_elem_info *ucontrol)
763{
764 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
765
766 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
767 ucontrol->count = params->max;
768
769 return 0;
770}
771EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
772
773int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
774 unsigned int size, unsigned int __user *tlv)
775{
776 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
777 unsigned int count = size < params->max ? size : params->max;
778 int ret = -ENXIO;
779
780 switch (op_flag) {
781 case SNDRV_CTL_TLV_OP_READ:
782 if (params->get)
Mythri P Ka1e5e7e92015-11-09 23:20:00 +0530783 ret = params->get(kcontrol, tlv, count);
Mark Brown70771482014-10-28 22:15:31 +0000784 break;
785 case SNDRV_CTL_TLV_OP_WRITE:
786 if (params->put)
Mythri P Ka1e5e7e92015-11-09 23:20:00 +0530787 ret = params->put(kcontrol, tlv, count);
Mark Brown70771482014-10-28 22:15:31 +0000788 break;
789 }
790 return ret;
791}
792EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
793
794/**
795 * snd_soc_info_xr_sx - signed multi register info callback
796 * @kcontrol: mreg control
797 * @uinfo: control element information
798 *
799 * Callback to provide information of a control that can
800 * span multiple codec registers which together
801 * forms a single signed value in a MSB/LSB manner.
802 *
803 * Returns 0 for success.
804 */
805int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
806 struct snd_ctl_elem_info *uinfo)
807{
808 struct soc_mreg_control *mc =
809 (struct soc_mreg_control *)kcontrol->private_value;
810 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
811 uinfo->count = 1;
812 uinfo->value.integer.min = mc->min;
813 uinfo->value.integer.max = mc->max;
814
815 return 0;
816}
817EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
818
819/**
820 * snd_soc_get_xr_sx - signed multi register get callback
821 * @kcontrol: mreg control
822 * @ucontrol: control element information
823 *
824 * Callback to get the value of a control that can span
825 * multiple codec registers which together forms a single
826 * signed value in a MSB/LSB manner. The control supports
827 * specifying total no of bits used to allow for bitfields
828 * across the multiple codec registers.
829 *
830 * Returns 0 for success.
831 */
832int snd_soc_get_xr_sx(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_mreg_control *mc =
837 (struct soc_mreg_control *)kcontrol->private_value;
838 unsigned int regbase = mc->regbase;
839 unsigned int regcount = mc->regcount;
840 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
841 unsigned int regwmask = (1<<regwshift)-1;
842 unsigned int invert = mc->invert;
843 unsigned long mask = (1UL<<mc->nbits)-1;
844 long min = mc->min;
845 long max = mc->max;
846 long val = 0;
847 unsigned int regval;
848 unsigned int i;
849 int ret;
850
851 for (i = 0; i < regcount; i++) {
852 ret = snd_soc_component_read(component, regbase+i, &regval);
853 if (ret)
854 return ret;
855 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
856 }
857 val &= mask;
858 if (min < 0 && val > max)
859 val |= ~mask;
860 if (invert)
861 val = max - val;
862 ucontrol->value.integer.value[0] = val;
863
864 return 0;
865}
866EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
867
868/**
869 * snd_soc_put_xr_sx - signed multi register get callback
870 * @kcontrol: mreg control
871 * @ucontrol: control element information
872 *
873 * Callback to set the value of a control that can span
874 * multiple codec registers which together forms a single
875 * signed value in a MSB/LSB manner. The control supports
876 * specifying total no of bits used to allow for bitfields
877 * across the multiple codec registers.
878 *
879 * Returns 0 for success.
880 */
881int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
882 struct snd_ctl_elem_value *ucontrol)
883{
884 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
885 struct soc_mreg_control *mc =
886 (struct soc_mreg_control *)kcontrol->private_value;
887 unsigned int regbase = mc->regbase;
888 unsigned int regcount = mc->regcount;
889 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
890 unsigned int regwmask = (1<<regwshift)-1;
891 unsigned int invert = mc->invert;
892 unsigned long mask = (1UL<<mc->nbits)-1;
893 long max = mc->max;
894 long val = ucontrol->value.integer.value[0];
895 unsigned int i, regval, regmask;
896 int err;
897
898 if (invert)
899 val = max - val;
900 val &= mask;
901 for (i = 0; i < regcount; i++) {
902 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
903 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
904 err = snd_soc_component_update_bits(component, regbase+i,
905 regmask, regval);
906 if (err < 0)
907 return err;
908 }
909
910 return 0;
911}
912EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
913
914/**
915 * snd_soc_get_strobe - strobe get callback
916 * @kcontrol: mixer control
917 * @ucontrol: control element information
918 *
919 * Callback get the value of a strobe mixer control.
920 *
921 * Returns 0 for success.
922 */
923int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
924 struct snd_ctl_elem_value *ucontrol)
925{
926 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
927 struct soc_mixer_control *mc =
928 (struct soc_mixer_control *)kcontrol->private_value;
929 unsigned int reg = mc->reg;
930 unsigned int shift = mc->shift;
931 unsigned int mask = 1 << shift;
932 unsigned int invert = mc->invert != 0;
933 unsigned int val;
934 int ret;
935
936 ret = snd_soc_component_read(component, reg, &val);
937 if (ret)
938 return ret;
939
940 val &= mask;
941
942 if (shift != 0 && val != 0)
943 val = val >> shift;
944 ucontrol->value.enumerated.item[0] = val ^ invert;
945
946 return 0;
947}
948EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
949
950/**
951 * snd_soc_put_strobe - strobe put callback
952 * @kcontrol: mixer control
953 * @ucontrol: control element information
954 *
955 * Callback strobe a register bit to high then low (or the inverse)
956 * in one pass of a single mixer enum control.
957 *
958 * Returns 1 for success.
959 */
960int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
961 struct snd_ctl_elem_value *ucontrol)
962{
963 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
964 struct soc_mixer_control *mc =
965 (struct soc_mixer_control *)kcontrol->private_value;
966 unsigned int reg = mc->reg;
967 unsigned int shift = mc->shift;
968 unsigned int mask = 1 << shift;
969 unsigned int invert = mc->invert != 0;
970 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
971 unsigned int val1 = (strobe ^ invert) ? mask : 0;
972 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
973 int err;
974
975 err = snd_soc_component_update_bits(component, reg, mask, val1);
976 if (err < 0)
977 return err;
978
979 return snd_soc_component_update_bits(component, reg, mask, val2);
980}
981EXPORT_SYMBOL_GPL(snd_soc_put_strobe);