ASoC: Factor out redundant read() functions
[pandora-kernel.git] / sound / soc / soc-cache.c
1 /*
2  * soc-cache.c  --  ASoC register cache helpers
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  */
13
14 #include <linux/i2c.h>
15 #include <linux/spi/spi.h>
16 #include <sound/soc.h>
17 #include <linux/lzo.h>
18 #include <linux/bitmap.h>
19 #include <linux/rbtree.h>
20
21 #include <trace/events/asoc.h>
22
23 #ifdef CONFIG_SPI_MASTER
24 static int do_spi_write(void *control, const char *data, int len)
25 {
26         struct spi_device *spi = control;
27         int ret;
28
29         ret = spi_write(spi, data, len);
30         if (ret < 0)
31                 return ret;
32
33         return len;
34 }
35 #endif
36
37 static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg,
38                        unsigned int value, const void *data, int len)
39 {
40         int ret;
41
42         if (!snd_soc_codec_volatile_register(codec, reg) &&
43             reg < codec->driver->reg_cache_size &&
44             !codec->cache_bypass) {
45                 ret = snd_soc_cache_write(codec, reg, value);
46                 if (ret < 0)
47                         return -1;
48         }
49
50         if (codec->cache_only) {
51                 codec->cache_sync = 1;
52                 return 0;
53         }
54
55         ret = codec->hw_write(codec->control_data, data, len);
56         if (ret == len)
57                 return 0;
58         if (ret < 0)
59                 return ret;
60         else
61                 return -EIO;
62 }
63
64 static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
65 {
66         int ret;
67         unsigned int val;
68
69         if (reg >= codec->driver->reg_cache_size ||
70             snd_soc_codec_volatile_register(codec, reg) ||
71             codec->cache_bypass) {
72                 if (codec->cache_only)
73                         return -1;
74
75                 BUG_ON(!codec->hw_read);
76                 return codec->hw_read(codec, reg);
77         }
78
79         ret = snd_soc_cache_read(codec, reg, &val);
80         if (ret < 0)
81                 return -1;
82         return val;
83 }
84
85 static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
86                               unsigned int value)
87 {
88         u16 data;
89
90         data = cpu_to_be16((reg << 12) | (value & 0xffffff));
91
92         return do_hw_write(codec, reg, value, &data, 2);
93 }
94
95 static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
96                              unsigned int value)
97 {
98         u16 data;
99
100         data = cpu_to_be16((reg << 9) | (value & 0x1ff));
101
102         return do_hw_write(codec, reg, value, &data, 2);
103 }
104
105 static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
106                              unsigned int value)
107 {
108         u8 data[2];
109
110         reg &= 0xff;
111         data[0] = reg;
112         data[1] = value & 0xff;
113
114         return do_hw_write(codec, reg, value, data, 2);
115 }
116
117 static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
118                               unsigned int value)
119 {
120         u8 data[3];
121         u16 val = cpu_to_be16(value);
122
123         data[0] = reg;
124         memcpy(&data[1], &val, sizeof(val));
125
126         return do_hw_write(codec, reg, value, data, 3);
127 }
128
129 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
130 static unsigned int do_i2c_read(struct snd_soc_codec *codec,
131                                 void *reg, int reglen,
132                                 void *data, int datalen)
133 {
134         struct i2c_msg xfer[2];
135         int ret;
136         struct i2c_client *client = codec->control_data;
137
138         /* Write register */
139         xfer[0].addr = client->addr;
140         xfer[0].flags = 0;
141         xfer[0].len = reglen;
142         xfer[0].buf = reg;
143
144         /* Read data */
145         xfer[1].addr = client->addr;
146         xfer[1].flags = I2C_M_RD;
147         xfer[1].len = datalen;
148         xfer[1].buf = data;
149
150         ret = i2c_transfer(client->adapter, xfer, 2);
151         if (ret == 2)
152                 return 0;
153         else if (ret < 0)
154                 return ret;
155         else
156                 return -EIO;
157 }
158 #endif
159
160 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
161 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
162                                          unsigned int r)
163 {
164         u8 reg = r;
165         u8 data;
166         int ret;
167
168         ret = do_i2c_read(codec, &reg, 1, &data, 1);
169         if (ret < 0)
170                 return 0;
171         return data;
172 }
173 #else
174 #define snd_soc_8_8_read_i2c NULL
175 #endif
176
177 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
178 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
179                                           unsigned int r)
180 {
181         u8 reg = r;
182         u16 data;
183         int ret;
184
185         ret = do_i2c_read(codec, &reg, 1, &data, 2);
186         if (ret < 0)
187                 return 0;
188         return (data >> 8) | ((data & 0xff) << 8);
189 }
190 #else
191 #define snd_soc_8_16_read_i2c NULL
192 #endif
193
194 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
195 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
196                                           unsigned int r)
197 {
198         u16 reg = r;
199         u8 data;
200         int ret;
201
202         ret = do_i2c_read(codec, &reg, 2, &data, 1);
203         if (ret < 0)
204                 return 0;
205         return data;
206 }
207 #else
208 #define snd_soc_16_8_read_i2c NULL
209 #endif
210
211 static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
212                               unsigned int value)
213 {
214         u8 data[3];
215         u16 rval = cpu_to_be16(reg);
216
217         memcpy(data, &rval, sizeof(rval));
218         data[2] = value;
219
220         return do_hw_write(codec, reg, value, data, 3);
221 }
222
223 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
224 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
225                                            unsigned int r)
226 {
227         u16 reg = cpu_to_be16(r);
228         u16 data;
229         int ret;
230
231         ret = do_i2c_read(codec, &reg, 2, &data, 2);
232         if (ret < 0)
233                 return 0;
234         return be16_to_cpu(data);
235 }
236 #else
237 #define snd_soc_16_16_read_i2c NULL
238 #endif
239
240 static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
241                                unsigned int value)
242 {
243         u16 data[2];
244
245         data[0] = cpu_to_be16(reg);
246         data[1] = cpu_to_be16(value);
247
248         return do_hw_write(codec, reg, value, data, sizeof(data));
249 }
250
251 /* Primitive bulk write support for soc-cache.  The data pointed to by
252  * `data' needs to already be in the form the hardware expects
253  * including any leading register specific data.  Any data written
254  * through this function will not go through the cache as it only
255  * handles writing to volatile or out of bounds registers.
256  */
257 static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg,
258                                      const void *data, size_t len)
259 {
260         int ret;
261
262         /* To ensure that we don't get out of sync with the cache, check
263          * whether the base register is volatile or if we've directly asked
264          * to bypass the cache.  Out of bounds registers are considered
265          * volatile.
266          */
267         if (!codec->cache_bypass
268             && !snd_soc_codec_volatile_register(codec, reg)
269             && reg < codec->driver->reg_cache_size)
270                 return -EINVAL;
271
272         switch (codec->control_type) {
273 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
274         case SND_SOC_I2C:
275                 ret = i2c_master_send(codec->control_data, data, len);
276                 break;
277 #endif
278 #if defined(CONFIG_SPI_MASTER)
279         case SND_SOC_SPI:
280                 ret = spi_write(codec->control_data, data, len);
281                 break;
282 #endif
283         default:
284                 BUG();
285         }
286
287         if (ret == len)
288                 return 0;
289         if (ret < 0)
290                 return ret;
291         else
292                 return -EIO;
293 }
294
295 static struct {
296         int addr_bits;
297         int data_bits;
298         int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
299         unsigned int (*read)(struct snd_soc_codec *, unsigned int);
300         unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
301 } io_types[] = {
302         {
303                 .addr_bits = 4, .data_bits = 12,
304                 .write = snd_soc_4_12_write,
305         },
306         {
307                 .addr_bits = 7, .data_bits = 9,
308                 .write = snd_soc_7_9_write,
309         },
310         {
311                 .addr_bits = 8, .data_bits = 8,
312                 .write = snd_soc_8_8_write,
313                 .i2c_read = snd_soc_8_8_read_i2c,
314         },
315         {
316                 .addr_bits = 8, .data_bits = 16,
317                 .write = snd_soc_8_16_write,
318                 .i2c_read = snd_soc_8_16_read_i2c,
319         },
320         {
321                 .addr_bits = 16, .data_bits = 8,
322                 .write = snd_soc_16_8_write,
323                 .i2c_read = snd_soc_16_8_read_i2c,
324         },
325         {
326                 .addr_bits = 16, .data_bits = 16,
327                 .write = snd_soc_16_16_write,
328                 .i2c_read = snd_soc_16_16_read_i2c,
329         },
330 };
331
332 /**
333  * snd_soc_codec_set_cache_io: Set up standard I/O functions.
334  *
335  * @codec: CODEC to configure.
336  * @addr_bits: Number of bits of register address data.
337  * @data_bits: Number of bits of data per register.
338  * @control: Control bus used.
339  *
340  * Register formats are frequently shared between many I2C and SPI
341  * devices.  In order to promote code reuse the ASoC core provides
342  * some standard implementations of CODEC read and write operations
343  * which can be set up using this function.
344  *
345  * The caller is responsible for allocating and initialising the
346  * actual cache.
347  *
348  * Note that at present this code cannot be used by CODECs with
349  * volatile registers.
350  */
351 int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
352                                int addr_bits, int data_bits,
353                                enum snd_soc_control_type control)
354 {
355         int i;
356
357         for (i = 0; i < ARRAY_SIZE(io_types); i++)
358                 if (io_types[i].addr_bits == addr_bits &&
359                     io_types[i].data_bits == data_bits)
360                         break;
361         if (i == ARRAY_SIZE(io_types)) {
362                 printk(KERN_ERR
363                        "No I/O functions for %d bit address %d bit data\n",
364                        addr_bits, data_bits);
365                 return -EINVAL;
366         }
367
368         codec->write = io_types[i].write;
369         codec->read = hw_read;
370         codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;
371
372         switch (control) {
373         case SND_SOC_CUSTOM:
374                 break;
375
376         case SND_SOC_I2C:
377 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
378                 codec->hw_write = (hw_write_t)i2c_master_send;
379 #endif
380                 if (io_types[i].i2c_read)
381                         codec->hw_read = io_types[i].i2c_read;
382
383                 codec->control_data = container_of(codec->dev,
384                                                    struct i2c_client,
385                                                    dev);
386                 break;
387
388         case SND_SOC_SPI:
389 #ifdef CONFIG_SPI_MASTER
390                 codec->hw_write = do_spi_write;
391 #endif
392
393                 codec->control_data = container_of(codec->dev,
394                                                    struct spi_device,
395                                                    dev);
396                 break;
397         }
398
399         return 0;
400 }
401 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
402
403 static bool snd_soc_set_cache_val(void *base, unsigned int idx,
404                                   unsigned int val, unsigned int word_size)
405 {
406         switch (word_size) {
407         case 1: {
408                 u8 *cache = base;
409                 if (cache[idx] == val)
410                         return true;
411                 cache[idx] = val;
412                 break;
413         }
414         case 2: {
415                 u16 *cache = base;
416                 if (cache[idx] == val)
417                         return true;
418                 cache[idx] = val;
419                 break;
420         }
421         default:
422                 BUG();
423         }
424         return false;
425 }
426
427 static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
428                 unsigned int word_size)
429 {
430         if (!base)
431                 return -1;
432
433         switch (word_size) {
434         case 1: {
435                 const u8 *cache = base;
436                 return cache[idx];
437         }
438         case 2: {
439                 const u16 *cache = base;
440                 return cache[idx];
441         }
442         default:
443                 BUG();
444         }
445         /* unreachable */
446         return -1;
447 }
448
449 struct snd_soc_rbtree_node {
450         struct rb_node node; /* the actual rbtree node holding this block */
451         unsigned int base_reg; /* base register handled by this block */
452         unsigned int word_size; /* number of bytes needed to represent the register index */
453         void *block; /* block of adjacent registers */
454         unsigned int blklen; /* number of registers available in the block */
455 } __attribute__ ((packed));
456
457 struct snd_soc_rbtree_ctx {
458         struct rb_root root;
459         struct snd_soc_rbtree_node *cached_rbnode;
460 };
461
462 static inline void snd_soc_rbtree_get_base_top_reg(
463         struct snd_soc_rbtree_node *rbnode,
464         unsigned int *base, unsigned int *top)
465 {
466         *base = rbnode->base_reg;
467         *top = rbnode->base_reg + rbnode->blklen - 1;
468 }
469
470 static unsigned int snd_soc_rbtree_get_register(
471         struct snd_soc_rbtree_node *rbnode, unsigned int idx)
472 {
473         unsigned int val;
474
475         switch (rbnode->word_size) {
476         case 1: {
477                 u8 *p = rbnode->block;
478                 val = p[idx];
479                 return val;
480         }
481         case 2: {
482                 u16 *p = rbnode->block;
483                 val = p[idx];
484                 return val;
485         }
486         default:
487                 BUG();
488                 break;
489         }
490         return -1;
491 }
492
493 static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node *rbnode,
494                                         unsigned int idx, unsigned int val)
495 {
496         switch (rbnode->word_size) {
497         case 1: {
498                 u8 *p = rbnode->block;
499                 p[idx] = val;
500                 break;
501         }
502         case 2: {
503                 u16 *p = rbnode->block;
504                 p[idx] = val;
505                 break;
506         }
507         default:
508                 BUG();
509                 break;
510         }
511 }
512
513 static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
514         struct rb_root *root, unsigned int reg)
515 {
516         struct rb_node *node;
517         struct snd_soc_rbtree_node *rbnode;
518         unsigned int base_reg, top_reg;
519
520         node = root->rb_node;
521         while (node) {
522                 rbnode = container_of(node, struct snd_soc_rbtree_node, node);
523                 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
524                 if (reg >= base_reg && reg <= top_reg)
525                         return rbnode;
526                 else if (reg > top_reg)
527                         node = node->rb_right;
528                 else if (reg < base_reg)
529                         node = node->rb_left;
530         }
531
532         return NULL;
533 }
534
535 static int snd_soc_rbtree_insert(struct rb_root *root,
536                                  struct snd_soc_rbtree_node *rbnode)
537 {
538         struct rb_node **new, *parent;
539         struct snd_soc_rbtree_node *rbnode_tmp;
540         unsigned int base_reg_tmp, top_reg_tmp;
541         unsigned int base_reg;
542
543         parent = NULL;
544         new = &root->rb_node;
545         while (*new) {
546                 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
547                                           node);
548                 /* base and top registers of the current rbnode */
549                 snd_soc_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp,
550                                                 &top_reg_tmp);
551                 /* base register of the rbnode to be added */
552                 base_reg = rbnode->base_reg;
553                 parent = *new;
554                 /* if this register has already been inserted, just return */
555                 if (base_reg >= base_reg_tmp &&
556                     base_reg <= top_reg_tmp)
557                         return 0;
558                 else if (base_reg > top_reg_tmp)
559                         new = &((*new)->rb_right);
560                 else if (base_reg < base_reg_tmp)
561                         new = &((*new)->rb_left);
562         }
563
564         /* insert the node into the rbtree */
565         rb_link_node(&rbnode->node, parent, new);
566         rb_insert_color(&rbnode->node, root);
567
568         return 1;
569 }
570
571 static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
572 {
573         struct snd_soc_rbtree_ctx *rbtree_ctx;
574         struct rb_node *node;
575         struct snd_soc_rbtree_node *rbnode;
576         unsigned int regtmp;
577         unsigned int val, def;
578         int ret;
579         int i;
580
581         rbtree_ctx = codec->reg_cache;
582         for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
583                 rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
584                 for (i = 0; i < rbnode->blklen; ++i) {
585                         regtmp = rbnode->base_reg + i;
586                         WARN_ON(codec->writable_register &&
587                                 codec->writable_register(codec, regtmp));
588                         val = snd_soc_rbtree_get_register(rbnode, i);
589                         def = snd_soc_get_cache_val(codec->reg_def_copy, i,
590                                                     rbnode->word_size);
591                         if (val == def)
592                                 continue;
593
594                         codec->cache_bypass = 1;
595                         ret = snd_soc_write(codec, regtmp, val);
596                         codec->cache_bypass = 0;
597                         if (ret)
598                                 return ret;
599                         dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
600                                 regtmp, val);
601                 }
602         }
603
604         return 0;
605 }
606
607 static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node *rbnode,
608                                           unsigned int pos, unsigned int reg,
609                                           unsigned int value)
610 {
611         u8 *blk;
612
613         blk = krealloc(rbnode->block,
614                        (rbnode->blklen + 1) * rbnode->word_size, GFP_KERNEL);
615         if (!blk)
616                 return -ENOMEM;
617
618         /* insert the register value in the correct place in the rbnode block */
619         memmove(blk + (pos + 1) * rbnode->word_size,
620                 blk + pos * rbnode->word_size,
621                 (rbnode->blklen - pos) * rbnode->word_size);
622
623         /* update the rbnode block, its size and the base register */
624         rbnode->block = blk;
625         rbnode->blklen++;
626         if (!pos)
627                 rbnode->base_reg = reg;
628
629         snd_soc_rbtree_set_register(rbnode, pos, value);
630         return 0;
631 }
632
633 static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
634                                       unsigned int reg, unsigned int value)
635 {
636         struct snd_soc_rbtree_ctx *rbtree_ctx;
637         struct snd_soc_rbtree_node *rbnode, *rbnode_tmp;
638         struct rb_node *node;
639         unsigned int val;
640         unsigned int reg_tmp;
641         unsigned int base_reg, top_reg;
642         unsigned int pos;
643         int i;
644         int ret;
645
646         rbtree_ctx = codec->reg_cache;
647         /* look up the required register in the cached rbnode */
648         rbnode = rbtree_ctx->cached_rbnode;
649         if (rbnode) {
650                 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
651                 if (reg >= base_reg && reg <= top_reg) {
652                         reg_tmp = reg - base_reg;
653                         val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
654                         if (val == value)
655                                 return 0;
656                         snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
657                         return 0;
658                 }
659         }
660         /* if we can't locate it in the cached rbnode we'll have
661          * to traverse the rbtree looking for it.
662          */
663         rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
664         if (rbnode) {
665                 reg_tmp = reg - rbnode->base_reg;
666                 val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
667                 if (val == value)
668                         return 0;
669                 snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
670                 rbtree_ctx->cached_rbnode = rbnode;
671         } else {
672                 /* bail out early, no need to create the rbnode yet */
673                 if (!value)
674                         return 0;
675                 /* look for an adjacent register to the one we are about to add */
676                 for (node = rb_first(&rbtree_ctx->root); node;
677                      node = rb_next(node)) {
678                         rbnode_tmp = rb_entry(node, struct snd_soc_rbtree_node, node);
679                         for (i = 0; i < rbnode_tmp->blklen; ++i) {
680                                 reg_tmp = rbnode_tmp->base_reg + i;
681                                 if (abs(reg_tmp - reg) != 1)
682                                         continue;
683                                 /* decide where in the block to place our register */
684                                 if (reg_tmp + 1 == reg)
685                                         pos = i + 1;
686                                 else
687                                         pos = i;
688                                 ret = snd_soc_rbtree_insert_to_block(rbnode_tmp, pos,
689                                                                      reg, value);
690                                 if (ret)
691                                         return ret;
692                                 rbtree_ctx->cached_rbnode = rbnode_tmp;
693                                 return 0;
694                         }
695                 }
696                 /* we did not manage to find a place to insert it in an existing
697                  * block so create a new rbnode with a single register in its block.
698                  * This block will get populated further if any other adjacent
699                  * registers get modified in the future.
700                  */
701                 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
702                 if (!rbnode)
703                         return -ENOMEM;
704                 rbnode->blklen = 1;
705                 rbnode->base_reg = reg;
706                 rbnode->word_size = codec->driver->reg_word_size;
707                 rbnode->block = kmalloc(rbnode->blklen * rbnode->word_size,
708                                         GFP_KERNEL);
709                 if (!rbnode->block) {
710                         kfree(rbnode);
711                         return -ENOMEM;
712                 }
713                 snd_soc_rbtree_set_register(rbnode, 0, value);
714                 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
715                 rbtree_ctx->cached_rbnode = rbnode;
716         }
717
718         return 0;
719 }
720
721 static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
722                                      unsigned int reg, unsigned int *value)
723 {
724         struct snd_soc_rbtree_ctx *rbtree_ctx;
725         struct snd_soc_rbtree_node *rbnode;
726         unsigned int base_reg, top_reg;
727         unsigned int reg_tmp;
728
729         rbtree_ctx = codec->reg_cache;
730         /* look up the required register in the cached rbnode */
731         rbnode = rbtree_ctx->cached_rbnode;
732         if (rbnode) {
733                 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
734                 if (reg >= base_reg && reg <= top_reg) {
735                         reg_tmp = reg - base_reg;
736                         *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
737                         return 0;
738                 }
739         }
740         /* if we can't locate it in the cached rbnode we'll have
741          * to traverse the rbtree looking for it.
742          */
743         rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
744         if (rbnode) {
745                 reg_tmp = reg - rbnode->base_reg;
746                 *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
747                 rbtree_ctx->cached_rbnode = rbnode;
748         } else {
749                 /* uninitialized registers default to 0 */
750                 *value = 0;
751         }
752
753         return 0;
754 }
755
756 static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
757 {
758         struct rb_node *next;
759         struct snd_soc_rbtree_ctx *rbtree_ctx;
760         struct snd_soc_rbtree_node *rbtree_node;
761
762         /* if we've already been called then just return */
763         rbtree_ctx = codec->reg_cache;
764         if (!rbtree_ctx)
765                 return 0;
766
767         /* free up the rbtree */
768         next = rb_first(&rbtree_ctx->root);
769         while (next) {
770                 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
771                 next = rb_next(&rbtree_node->node);
772                 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
773                 kfree(rbtree_node->block);
774                 kfree(rbtree_node);
775         }
776
777         /* release the resources */
778         kfree(codec->reg_cache);
779         codec->reg_cache = NULL;
780
781         return 0;
782 }
783
784 static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
785 {
786         struct snd_soc_rbtree_ctx *rbtree_ctx;
787         unsigned int word_size;
788         unsigned int val;
789         int i;
790         int ret;
791
792         codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
793         if (!codec->reg_cache)
794                 return -ENOMEM;
795
796         rbtree_ctx = codec->reg_cache;
797         rbtree_ctx->root = RB_ROOT;
798         rbtree_ctx->cached_rbnode = NULL;
799
800         if (!codec->reg_def_copy)
801                 return 0;
802
803         word_size = codec->driver->reg_word_size;
804         for (i = 0; i < codec->driver->reg_cache_size; ++i) {
805                 val = snd_soc_get_cache_val(codec->reg_def_copy, i,
806                                             word_size);
807                 if (!val)
808                         continue;
809                 ret = snd_soc_rbtree_cache_write(codec, i, val);
810                 if (ret)
811                         goto err;
812         }
813
814         return 0;
815
816 err:
817         snd_soc_cache_exit(codec);
818         return ret;
819 }
820
821 #ifdef CONFIG_SND_SOC_CACHE_LZO
822 struct snd_soc_lzo_ctx {
823         void *wmem;
824         void *dst;
825         const void *src;
826         size_t src_len;
827         size_t dst_len;
828         size_t decompressed_size;
829         unsigned long *sync_bmp;
830         int sync_bmp_nbits;
831 };
832
833 #define LZO_BLOCK_NUM 8
834 static int snd_soc_lzo_block_count(void)
835 {
836         return LZO_BLOCK_NUM;
837 }
838
839 static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
840 {
841         lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
842         if (!lzo_ctx->wmem)
843                 return -ENOMEM;
844         return 0;
845 }
846
847 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
848 {
849         size_t compress_size;
850         int ret;
851
852         ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
853                                lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
854         if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
855                 return -EINVAL;
856         lzo_ctx->dst_len = compress_size;
857         return 0;
858 }
859
860 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
861 {
862         size_t dst_len;
863         int ret;
864
865         dst_len = lzo_ctx->dst_len;
866         ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
867                                     lzo_ctx->dst, &dst_len);
868         if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
869                 return -EINVAL;
870         return 0;
871 }
872
873 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
874                 struct snd_soc_lzo_ctx *lzo_ctx)
875 {
876         int ret;
877
878         lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
879         lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
880         if (!lzo_ctx->dst) {
881                 lzo_ctx->dst_len = 0;
882                 return -ENOMEM;
883         }
884
885         ret = snd_soc_lzo_compress(lzo_ctx);
886         if (ret < 0)
887                 return ret;
888         return 0;
889 }
890
891 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
892                 struct snd_soc_lzo_ctx *lzo_ctx)
893 {
894         int ret;
895
896         lzo_ctx->dst_len = lzo_ctx->decompressed_size;
897         lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
898         if (!lzo_ctx->dst) {
899                 lzo_ctx->dst_len = 0;
900                 return -ENOMEM;
901         }
902
903         ret = snd_soc_lzo_decompress(lzo_ctx);
904         if (ret < 0)
905                 return ret;
906         return 0;
907 }
908
909 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
910                 unsigned int reg)
911 {
912         const struct snd_soc_codec_driver *codec_drv;
913
914         codec_drv = codec->driver;
915         return (reg * codec_drv->reg_word_size) /
916                DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
917 }
918
919 static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
920                 unsigned int reg)
921 {
922         const struct snd_soc_codec_driver *codec_drv;
923
924         codec_drv = codec->driver;
925         return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
926                       codec_drv->reg_word_size);
927 }
928
929 static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
930 {
931         const struct snd_soc_codec_driver *codec_drv;
932
933         codec_drv = codec->driver;
934         return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
935 }
936
937 static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
938 {
939         struct snd_soc_lzo_ctx **lzo_blocks;
940         unsigned int val;
941         int i;
942         int ret;
943
944         lzo_blocks = codec->reg_cache;
945         for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
946                 WARN_ON(codec->writable_register &&
947                         codec->writable_register(codec, i));
948                 ret = snd_soc_cache_read(codec, i, &val);
949                 if (ret)
950                         return ret;
951                 codec->cache_bypass = 1;
952                 ret = snd_soc_write(codec, i, val);
953                 codec->cache_bypass = 0;
954                 if (ret)
955                         return ret;
956                 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
957                         i, val);
958         }
959
960         return 0;
961 }
962
963 static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
964                                    unsigned int reg, unsigned int value)
965 {
966         struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
967         int ret, blkindex, blkpos;
968         size_t blksize, tmp_dst_len;
969         void *tmp_dst;
970
971         /* index of the compressed lzo block */
972         blkindex = snd_soc_lzo_get_blkindex(codec, reg);
973         /* register index within the decompressed block */
974         blkpos = snd_soc_lzo_get_blkpos(codec, reg);
975         /* size of the compressed block */
976         blksize = snd_soc_lzo_get_blksize(codec);
977         lzo_blocks = codec->reg_cache;
978         lzo_block = lzo_blocks[blkindex];
979
980         /* save the pointer and length of the compressed block */
981         tmp_dst = lzo_block->dst;
982         tmp_dst_len = lzo_block->dst_len;
983
984         /* prepare the source to be the compressed block */
985         lzo_block->src = lzo_block->dst;
986         lzo_block->src_len = lzo_block->dst_len;
987
988         /* decompress the block */
989         ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
990         if (ret < 0) {
991                 kfree(lzo_block->dst);
992                 goto out;
993         }
994
995         /* write the new value to the cache */
996         if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
997                                   codec->driver->reg_word_size)) {
998                 kfree(lzo_block->dst);
999                 goto out;
1000         }
1001
1002         /* prepare the source to be the decompressed block */
1003         lzo_block->src = lzo_block->dst;
1004         lzo_block->src_len = lzo_block->dst_len;
1005
1006         /* compress the block */
1007         ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1008         if (ret < 0) {
1009                 kfree(lzo_block->dst);
1010                 kfree(lzo_block->src);
1011                 goto out;
1012         }
1013
1014         /* set the bit so we know we have to sync this register */
1015         set_bit(reg, lzo_block->sync_bmp);
1016         kfree(tmp_dst);
1017         kfree(lzo_block->src);
1018         return 0;
1019 out:
1020         lzo_block->dst = tmp_dst;
1021         lzo_block->dst_len = tmp_dst_len;
1022         return ret;
1023 }
1024
1025 static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1026                                   unsigned int reg, unsigned int *value)
1027 {
1028         struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1029         int ret, blkindex, blkpos;
1030         size_t blksize, tmp_dst_len;
1031         void *tmp_dst;
1032
1033         *value = 0;
1034         /* index of the compressed lzo block */
1035         blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1036         /* register index within the decompressed block */
1037         blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1038         /* size of the compressed block */
1039         blksize = snd_soc_lzo_get_blksize(codec);
1040         lzo_blocks = codec->reg_cache;
1041         lzo_block = lzo_blocks[blkindex];
1042
1043         /* save the pointer and length of the compressed block */
1044         tmp_dst = lzo_block->dst;
1045         tmp_dst_len = lzo_block->dst_len;
1046
1047         /* prepare the source to be the compressed block */
1048         lzo_block->src = lzo_block->dst;
1049         lzo_block->src_len = lzo_block->dst_len;
1050
1051         /* decompress the block */
1052         ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1053         if (ret >= 0)
1054                 /* fetch the value from the cache */
1055                 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1056                                                codec->driver->reg_word_size);
1057
1058         kfree(lzo_block->dst);
1059         /* restore the pointer and length of the compressed block */
1060         lzo_block->dst = tmp_dst;
1061         lzo_block->dst_len = tmp_dst_len;
1062         return 0;
1063 }
1064
1065 static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1066 {
1067         struct snd_soc_lzo_ctx **lzo_blocks;
1068         int i, blkcount;
1069
1070         lzo_blocks = codec->reg_cache;
1071         if (!lzo_blocks)
1072                 return 0;
1073
1074         blkcount = snd_soc_lzo_block_count();
1075         /*
1076          * the pointer to the bitmap used for syncing the cache
1077          * is shared amongst all lzo_blocks.  Ensure it is freed
1078          * only once.
1079          */
1080         if (lzo_blocks[0])
1081                 kfree(lzo_blocks[0]->sync_bmp);
1082         for (i = 0; i < blkcount; ++i) {
1083                 if (lzo_blocks[i]) {
1084                         kfree(lzo_blocks[i]->wmem);
1085                         kfree(lzo_blocks[i]->dst);
1086                 }
1087                 /* each lzo_block is a pointer returned by kmalloc or NULL */
1088                 kfree(lzo_blocks[i]);
1089         }
1090         kfree(lzo_blocks);
1091         codec->reg_cache = NULL;
1092         return 0;
1093 }
1094
1095 static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1096 {
1097         struct snd_soc_lzo_ctx **lzo_blocks;
1098         size_t bmp_size;
1099         const struct snd_soc_codec_driver *codec_drv;
1100         int ret, tofree, i, blksize, blkcount;
1101         const char *p, *end;
1102         unsigned long *sync_bmp;
1103
1104         ret = 0;
1105         codec_drv = codec->driver;
1106
1107         /*
1108          * If we have not been given a default register cache
1109          * then allocate a dummy zero-ed out region, compress it
1110          * and remember to free it afterwards.
1111          */
1112         tofree = 0;
1113         if (!codec->reg_def_copy)
1114                 tofree = 1;
1115
1116         if (!codec->reg_def_copy) {
1117                 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
1118                 if (!codec->reg_def_copy)
1119                         return -ENOMEM;
1120         }
1121
1122         blkcount = snd_soc_lzo_block_count();
1123         codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1124                                    GFP_KERNEL);
1125         if (!codec->reg_cache) {
1126                 ret = -ENOMEM;
1127                 goto err_tofree;
1128         }
1129         lzo_blocks = codec->reg_cache;
1130
1131         /*
1132          * allocate a bitmap to be used when syncing the cache with
1133          * the hardware.  Each time a register is modified, the corresponding
1134          * bit is set in the bitmap, so we know that we have to sync
1135          * that register.
1136          */
1137         bmp_size = codec_drv->reg_cache_size;
1138         sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
1139                            GFP_KERNEL);
1140         if (!sync_bmp) {
1141                 ret = -ENOMEM;
1142                 goto err;
1143         }
1144         bitmap_zero(sync_bmp, bmp_size);
1145
1146         /* allocate the lzo blocks and initialize them */
1147         for (i = 0; i < blkcount; ++i) {
1148                 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1149                                         GFP_KERNEL);
1150                 if (!lzo_blocks[i]) {
1151                         kfree(sync_bmp);
1152                         ret = -ENOMEM;
1153                         goto err;
1154                 }
1155                 lzo_blocks[i]->sync_bmp = sync_bmp;
1156                 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
1157                 /* alloc the working space for the compressed block */
1158                 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1159                 if (ret < 0)
1160                         goto err;
1161         }
1162
1163         blksize = snd_soc_lzo_get_blksize(codec);
1164         p = codec->reg_def_copy;
1165         end = codec->reg_def_copy + codec->reg_size;
1166         /* compress the register map and fill the lzo blocks */
1167         for (i = 0; i < blkcount; ++i, p += blksize) {
1168                 lzo_blocks[i]->src = p;
1169                 if (p + blksize > end)
1170                         lzo_blocks[i]->src_len = end - p;
1171                 else
1172                         lzo_blocks[i]->src_len = blksize;
1173                 ret = snd_soc_lzo_compress_cache_block(codec,
1174                                                        lzo_blocks[i]);
1175                 if (ret < 0)
1176                         goto err;
1177                 lzo_blocks[i]->decompressed_size =
1178                         lzo_blocks[i]->src_len;
1179         }
1180
1181         if (tofree) {
1182                 kfree(codec->reg_def_copy);
1183                 codec->reg_def_copy = NULL;
1184         }
1185         return 0;
1186 err:
1187         snd_soc_cache_exit(codec);
1188 err_tofree:
1189         if (tofree) {
1190                 kfree(codec->reg_def_copy);
1191                 codec->reg_def_copy = NULL;
1192         }
1193         return ret;
1194 }
1195 #endif
1196
1197 static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1198 {
1199         int i;
1200         int ret;
1201         const struct snd_soc_codec_driver *codec_drv;
1202         unsigned int val;
1203
1204         codec_drv = codec->driver;
1205         for (i = 0; i < codec_drv->reg_cache_size; ++i) {
1206                 WARN_ON(codec->writable_register &&
1207                         codec->writable_register(codec, i));
1208                 ret = snd_soc_cache_read(codec, i, &val);
1209                 if (ret)
1210                         return ret;
1211                 if (codec->reg_def_copy)
1212                         if (snd_soc_get_cache_val(codec->reg_def_copy,
1213                                                   i, codec_drv->reg_word_size) == val)
1214                                 continue;
1215                 ret = snd_soc_write(codec, i, val);
1216                 if (ret)
1217                         return ret;
1218                 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1219                         i, val);
1220         }
1221         return 0;
1222 }
1223
1224 static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1225                                     unsigned int reg, unsigned int value)
1226 {
1227         snd_soc_set_cache_val(codec->reg_cache, reg, value,
1228                               codec->driver->reg_word_size);
1229         return 0;
1230 }
1231
1232 static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1233                                    unsigned int reg, unsigned int *value)
1234 {
1235         *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1236                                        codec->driver->reg_word_size);
1237         return 0;
1238 }
1239
1240 static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1241 {
1242         if (!codec->reg_cache)
1243                 return 0;
1244         kfree(codec->reg_cache);
1245         codec->reg_cache = NULL;
1246         return 0;
1247 }
1248
1249 static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1250 {
1251         const struct snd_soc_codec_driver *codec_drv;
1252
1253         codec_drv = codec->driver;
1254
1255         if (codec->reg_def_copy)
1256                 codec->reg_cache = kmemdup(codec->reg_def_copy,
1257                                            codec->reg_size, GFP_KERNEL);
1258         else
1259                 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
1260         if (!codec->reg_cache)
1261                 return -ENOMEM;
1262
1263         return 0;
1264 }
1265
1266 /* an array of all supported compression types */
1267 static const struct snd_soc_cache_ops cache_types[] = {
1268         /* Flat *must* be the first entry for fallback */
1269         {
1270                 .id = SND_SOC_FLAT_COMPRESSION,
1271                 .name = "flat",
1272                 .init = snd_soc_flat_cache_init,
1273                 .exit = snd_soc_flat_cache_exit,
1274                 .read = snd_soc_flat_cache_read,
1275                 .write = snd_soc_flat_cache_write,
1276                 .sync = snd_soc_flat_cache_sync
1277         },
1278 #ifdef CONFIG_SND_SOC_CACHE_LZO
1279         {
1280                 .id = SND_SOC_LZO_COMPRESSION,
1281                 .name = "LZO",
1282                 .init = snd_soc_lzo_cache_init,
1283                 .exit = snd_soc_lzo_cache_exit,
1284                 .read = snd_soc_lzo_cache_read,
1285                 .write = snd_soc_lzo_cache_write,
1286                 .sync = snd_soc_lzo_cache_sync
1287         },
1288 #endif
1289         {
1290                 .id = SND_SOC_RBTREE_COMPRESSION,
1291                 .name = "rbtree",
1292                 .init = snd_soc_rbtree_cache_init,
1293                 .exit = snd_soc_rbtree_cache_exit,
1294                 .read = snd_soc_rbtree_cache_read,
1295                 .write = snd_soc_rbtree_cache_write,
1296                 .sync = snd_soc_rbtree_cache_sync
1297         }
1298 };
1299
1300 int snd_soc_cache_init(struct snd_soc_codec *codec)
1301 {
1302         int i;
1303
1304         for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
1305                 if (cache_types[i].id == codec->compress_type)
1306                         break;
1307
1308         /* Fall back to flat compression */
1309         if (i == ARRAY_SIZE(cache_types)) {
1310                 dev_warn(codec->dev, "Could not match compress type: %d\n",
1311                          codec->compress_type);
1312                 i = 0;
1313         }
1314
1315         mutex_init(&codec->cache_rw_mutex);
1316         codec->cache_ops = &cache_types[i];
1317
1318         if (codec->cache_ops->init) {
1319                 if (codec->cache_ops->name)
1320                         dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1321                                 codec->cache_ops->name, codec->name);
1322                 return codec->cache_ops->init(codec);
1323         }
1324         return -ENOSYS;
1325 }
1326
1327 /*
1328  * NOTE: keep in mind that this function might be called
1329  * multiple times.
1330  */
1331 int snd_soc_cache_exit(struct snd_soc_codec *codec)
1332 {
1333         if (codec->cache_ops && codec->cache_ops->exit) {
1334                 if (codec->cache_ops->name)
1335                         dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1336                                 codec->cache_ops->name, codec->name);
1337                 return codec->cache_ops->exit(codec);
1338         }
1339         return -ENOSYS;
1340 }
1341
1342 /**
1343  * snd_soc_cache_read: Fetch the value of a given register from the cache.
1344  *
1345  * @codec: CODEC to configure.
1346  * @reg: The register index.
1347  * @value: The value to be returned.
1348  */
1349 int snd_soc_cache_read(struct snd_soc_codec *codec,
1350                        unsigned int reg, unsigned int *value)
1351 {
1352         int ret;
1353
1354         mutex_lock(&codec->cache_rw_mutex);
1355
1356         if (value && codec->cache_ops && codec->cache_ops->read) {
1357                 ret = codec->cache_ops->read(codec, reg, value);
1358                 mutex_unlock(&codec->cache_rw_mutex);
1359                 return ret;
1360         }
1361
1362         mutex_unlock(&codec->cache_rw_mutex);
1363         return -ENOSYS;
1364 }
1365 EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1366
1367 /**
1368  * snd_soc_cache_write: Set the value of a given register in the cache.
1369  *
1370  * @codec: CODEC to configure.
1371  * @reg: The register index.
1372  * @value: The new register value.
1373  */
1374 int snd_soc_cache_write(struct snd_soc_codec *codec,
1375                         unsigned int reg, unsigned int value)
1376 {
1377         int ret;
1378
1379         mutex_lock(&codec->cache_rw_mutex);
1380
1381         if (codec->cache_ops && codec->cache_ops->write) {
1382                 ret = codec->cache_ops->write(codec, reg, value);
1383                 mutex_unlock(&codec->cache_rw_mutex);
1384                 return ret;
1385         }
1386
1387         mutex_unlock(&codec->cache_rw_mutex);
1388         return -ENOSYS;
1389 }
1390 EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1391
1392 /**
1393  * snd_soc_cache_sync: Sync the register cache with the hardware.
1394  *
1395  * @codec: CODEC to configure.
1396  *
1397  * Any registers that should not be synced should be marked as
1398  * volatile.  In general drivers can choose not to use the provided
1399  * syncing functionality if they so require.
1400  */
1401 int snd_soc_cache_sync(struct snd_soc_codec *codec)
1402 {
1403         int ret;
1404         const char *name;
1405
1406         if (!codec->cache_sync) {
1407                 return 0;
1408         }
1409
1410         if (!codec->cache_ops || !codec->cache_ops->sync)
1411                 return -ENOSYS;
1412
1413         if (codec->cache_ops->name)
1414                 name = codec->cache_ops->name;
1415         else
1416                 name = "unknown";
1417
1418         if (codec->cache_ops->name)
1419                 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1420                         codec->cache_ops->name, codec->name);
1421         trace_snd_soc_cache_sync(codec, name, "start");
1422         ret = codec->cache_ops->sync(codec);
1423         if (!ret)
1424                 codec->cache_sync = 0;
1425         trace_snd_soc_cache_sync(codec, name, "end");
1426         return ret;
1427 }
1428 EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
1429
1430 static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1431                                         unsigned int reg)
1432 {
1433         const struct snd_soc_codec_driver *codec_drv;
1434         unsigned int min, max, index;
1435
1436         codec_drv = codec->driver;
1437         min = 0;
1438         max = codec_drv->reg_access_size - 1;
1439         do {
1440                 index = (min + max) / 2;
1441                 if (codec_drv->reg_access_default[index].reg == reg)
1442                         return index;
1443                 if (codec_drv->reg_access_default[index].reg < reg)
1444                         min = index + 1;
1445                 else
1446                         max = index;
1447         } while (min <= max);
1448         return -1;
1449 }
1450
1451 int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1452                                       unsigned int reg)
1453 {
1454         int index;
1455
1456         if (reg >= codec->driver->reg_cache_size)
1457                 return 1;
1458         index = snd_soc_get_reg_access_index(codec, reg);
1459         if (index < 0)
1460                 return 0;
1461         return codec->driver->reg_access_default[index].vol;
1462 }
1463 EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1464
1465 int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1466                                       unsigned int reg)
1467 {
1468         int index;
1469
1470         if (reg >= codec->driver->reg_cache_size)
1471                 return 1;
1472         index = snd_soc_get_reg_access_index(codec, reg);
1473         if (index < 0)
1474                 return 0;
1475         return codec->driver->reg_access_default[index].read;
1476 }
1477 EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
1478
1479 int snd_soc_default_writable_register(struct snd_soc_codec *codec,
1480                                       unsigned int reg)
1481 {
1482         int index;
1483
1484         if (reg >= codec->driver->reg_cache_size)
1485                 return 1;
1486         index = snd_soc_get_reg_access_index(codec, reg);
1487         if (index < 0)
1488                 return 0;
1489         return codec->driver->reg_access_default[index].write;
1490 }
1491 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);