ASoC: Suppress restore of default register values for rbtree cache sync
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / soc-cache.c
blob9a88a276a0abbae8d87bc192de71621988bf0266
1 /*
2 * soc-cache.c -- ASoC register cache helpers
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
21 #include <trace/events/asoc.h>
23 #ifdef CONFIG_SPI_MASTER
24 static int do_spi_write(void *control, const char *data, int len)
26 struct spi_device *spi = control;
27 int ret;
29 ret = spi_write(spi, data, len);
30 if (ret < 0)
31 return ret;
33 return len;
35 #endif
37 static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg,
38 unsigned int value, const void *data, int len)
40 int ret;
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;
50 if (codec->cache_only) {
51 codec->cache_sync = 1;
52 return 0;
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;
64 static unsigned int do_hw_read(struct snd_soc_codec *codec, unsigned int reg)
66 int ret;
67 unsigned int val;
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;
75 BUG_ON(!codec->hw_read);
76 return codec->hw_read(codec, reg);
79 ret = snd_soc_cache_read(codec, reg, &val);
80 if (ret < 0)
81 return -1;
82 return val;
85 static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
86 unsigned int reg)
88 return do_hw_read(codec, reg);
91 static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
92 unsigned int value)
94 u16 data;
96 data = cpu_to_be16((reg << 12) | (value & 0xffffff));
98 return do_hw_write(codec, reg, value, &data, 2);
101 static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
102 unsigned int reg)
104 return do_hw_read(codec, reg);
107 static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
108 unsigned int value)
110 u16 data;
112 data = cpu_to_be16((reg << 9) | (value & 0x1ff));
114 return do_hw_write(codec, reg, value, &data, 2);
117 static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
118 unsigned int value)
120 u8 data[2];
122 reg &= 0xff;
123 data[0] = reg;
124 data[1] = value & 0xff;
126 return do_hw_write(codec, reg, value, data, 2);
129 static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
130 unsigned int reg)
132 return do_hw_read(codec, reg);
135 static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
136 unsigned int value)
138 u8 data[3];
139 u16 val = cpu_to_be16(value);
141 data[0] = reg;
142 memcpy(&data[1], &val, sizeof(val));
144 return do_hw_write(codec, reg, value, data, 3);
147 static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
148 unsigned int reg)
150 return do_hw_read(codec, reg);
153 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
154 static unsigned int do_i2c_read(struct snd_soc_codec *codec,
155 void *reg, int reglen,
156 void *data, int datalen)
158 struct i2c_msg xfer[2];
159 int ret;
160 struct i2c_client *client = codec->control_data;
162 /* Write register */
163 xfer[0].addr = client->addr;
164 xfer[0].flags = 0;
165 xfer[0].len = reglen;
166 xfer[0].buf = reg;
168 /* Read data */
169 xfer[1].addr = client->addr;
170 xfer[1].flags = I2C_M_RD;
171 xfer[1].len = datalen;
172 xfer[1].buf = data;
174 ret = i2c_transfer(client->adapter, xfer, 2);
175 if (ret == 2)
176 return 0;
177 else if (ret < 0)
178 return ret;
179 else
180 return -EIO;
182 #endif
184 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
185 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
186 unsigned int r)
188 u8 reg = r;
189 u8 data;
190 int ret;
192 ret = do_i2c_read(codec, &reg, 1, &data, 1);
193 if (ret < 0)
194 return 0;
195 return data;
197 #else
198 #define snd_soc_8_8_read_i2c NULL
199 #endif
201 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
202 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
203 unsigned int r)
205 u8 reg = r;
206 u16 data;
207 int ret;
209 ret = do_i2c_read(codec, &reg, 1, &data, 2);
210 if (ret < 0)
211 return 0;
212 return (data >> 8) | ((data & 0xff) << 8);
214 #else
215 #define snd_soc_8_16_read_i2c NULL
216 #endif
218 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
219 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
220 unsigned int r)
222 u16 reg = r;
223 u8 data;
224 int ret;
226 ret = do_i2c_read(codec, &reg, 2, &data, 1);
227 if (ret < 0)
228 return 0;
229 return data;
231 #else
232 #define snd_soc_16_8_read_i2c NULL
233 #endif
235 static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
236 unsigned int reg)
238 return do_hw_read(codec, reg);
241 static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
242 unsigned int value)
244 u8 data[3];
245 u16 rval = cpu_to_be16(reg);
247 memcpy(data, &rval, sizeof(rval));
248 data[2] = value;
250 return do_hw_write(codec, reg, value, data, 3);
253 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
254 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
255 unsigned int r)
257 u16 reg = cpu_to_be16(r);
258 u16 data;
259 int ret;
261 ret = do_i2c_read(codec, &reg, 2, &data, 2);
262 if (ret < 0)
263 return 0;
264 return be16_to_cpu(data);
266 #else
267 #define snd_soc_16_16_read_i2c NULL
268 #endif
270 static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
271 unsigned int reg)
273 return do_hw_read(codec, reg);
276 static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
277 unsigned int value)
279 u16 data[2];
281 data[0] = cpu_to_be16(reg);
282 data[1] = cpu_to_be16(value);
284 return do_hw_write(codec, reg, value, data, sizeof(data));
287 /* Primitive bulk write support for soc-cache. The data pointed to by
288 * `data' needs to already be in the form the hardware expects
289 * including any leading register specific data. Any data written
290 * through this function will not go through the cache as it only
291 * handles writing to volatile or out of bounds registers.
293 static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg,
294 const void *data, size_t len)
296 int ret;
298 /* To ensure that we don't get out of sync with the cache, check
299 * whether the base register is volatile or if we've directly asked
300 * to bypass the cache. Out of bounds registers are considered
301 * volatile.
303 if (!codec->cache_bypass
304 && !snd_soc_codec_volatile_register(codec, reg)
305 && reg < codec->driver->reg_cache_size)
306 return -EINVAL;
308 switch (codec->control_type) {
309 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
310 case SND_SOC_I2C:
311 ret = i2c_master_send(codec->control_data, data, len);
312 break;
313 #endif
314 #if defined(CONFIG_SPI_MASTER)
315 case SND_SOC_SPI:
316 ret = spi_write(codec->control_data, data, len);
317 break;
318 #endif
319 default:
320 BUG();
323 if (ret == len)
324 return 0;
325 if (ret < 0)
326 return ret;
327 else
328 return -EIO;
331 static struct {
332 int addr_bits;
333 int data_bits;
334 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
335 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
336 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
337 } io_types[] = {
339 .addr_bits = 4, .data_bits = 12,
340 .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
343 .addr_bits = 7, .data_bits = 9,
344 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
347 .addr_bits = 8, .data_bits = 8,
348 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
349 .i2c_read = snd_soc_8_8_read_i2c,
352 .addr_bits = 8, .data_bits = 16,
353 .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
354 .i2c_read = snd_soc_8_16_read_i2c,
357 .addr_bits = 16, .data_bits = 8,
358 .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
359 .i2c_read = snd_soc_16_8_read_i2c,
362 .addr_bits = 16, .data_bits = 16,
363 .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
364 .i2c_read = snd_soc_16_16_read_i2c,
369 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
371 * @codec: CODEC to configure.
372 * @addr_bits: Number of bits of register address data.
373 * @data_bits: Number of bits of data per register.
374 * @control: Control bus used.
376 * Register formats are frequently shared between many I2C and SPI
377 * devices. In order to promote code reuse the ASoC core provides
378 * some standard implementations of CODEC read and write operations
379 * which can be set up using this function.
381 * The caller is responsible for allocating and initialising the
382 * actual cache.
384 * Note that at present this code cannot be used by CODECs with
385 * volatile registers.
387 int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
388 int addr_bits, int data_bits,
389 enum snd_soc_control_type control)
391 int i;
393 for (i = 0; i < ARRAY_SIZE(io_types); i++)
394 if (io_types[i].addr_bits == addr_bits &&
395 io_types[i].data_bits == data_bits)
396 break;
397 if (i == ARRAY_SIZE(io_types)) {
398 printk(KERN_ERR
399 "No I/O functions for %d bit address %d bit data\n",
400 addr_bits, data_bits);
401 return -EINVAL;
404 codec->write = io_types[i].write;
405 codec->read = io_types[i].read;
406 codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;
408 switch (control) {
409 case SND_SOC_CUSTOM:
410 break;
412 case SND_SOC_I2C:
413 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
414 codec->hw_write = (hw_write_t)i2c_master_send;
415 #endif
416 if (io_types[i].i2c_read)
417 codec->hw_read = io_types[i].i2c_read;
419 codec->control_data = container_of(codec->dev,
420 struct i2c_client,
421 dev);
422 break;
424 case SND_SOC_SPI:
425 #ifdef CONFIG_SPI_MASTER
426 codec->hw_write = do_spi_write;
427 #endif
429 codec->control_data = container_of(codec->dev,
430 struct spi_device,
431 dev);
432 break;
435 return 0;
437 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
439 static bool snd_soc_set_cache_val(void *base, unsigned int idx,
440 unsigned int val, unsigned int word_size)
442 switch (word_size) {
443 case 1: {
444 u8 *cache = base;
445 if (cache[idx] == val)
446 return true;
447 cache[idx] = val;
448 break;
450 case 2: {
451 u16 *cache = base;
452 if (cache[idx] == val)
453 return true;
454 cache[idx] = val;
455 break;
457 default:
458 BUG();
460 return false;
463 static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
464 unsigned int word_size)
466 if (!base)
467 return -1;
469 switch (word_size) {
470 case 1: {
471 const u8 *cache = base;
472 return cache[idx];
474 case 2: {
475 const u16 *cache = base;
476 return cache[idx];
478 default:
479 BUG();
481 /* unreachable */
482 return -1;
485 struct snd_soc_rbtree_node {
486 struct rb_node node; /* the actual rbtree node holding this block */
487 unsigned int base_reg; /* base register handled by this block */
488 unsigned int word_size; /* number of bytes needed to represent the register index */
489 void *block; /* block of adjacent registers */
490 unsigned int blklen; /* number of registers available in the block */
491 } __attribute__ ((packed));
493 struct snd_soc_rbtree_ctx {
494 struct rb_root root;
495 struct snd_soc_rbtree_node *cached_rbnode;
498 static inline void snd_soc_rbtree_get_base_top_reg(
499 struct snd_soc_rbtree_node *rbnode,
500 unsigned int *base, unsigned int *top)
502 *base = rbnode->base_reg;
503 *top = rbnode->base_reg + rbnode->blklen - 1;
506 static unsigned int snd_soc_rbtree_get_register(
507 struct snd_soc_rbtree_node *rbnode, unsigned int idx)
509 unsigned int val;
511 switch (rbnode->word_size) {
512 case 1: {
513 u8 *p = rbnode->block;
514 val = p[idx];
515 return val;
517 case 2: {
518 u16 *p = rbnode->block;
519 val = p[idx];
520 return val;
522 default:
523 BUG();
524 break;
526 return -1;
529 static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node *rbnode,
530 unsigned int idx, unsigned int val)
532 switch (rbnode->word_size) {
533 case 1: {
534 u8 *p = rbnode->block;
535 p[idx] = val;
536 break;
538 case 2: {
539 u16 *p = rbnode->block;
540 p[idx] = val;
541 break;
543 default:
544 BUG();
545 break;
549 static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
550 struct rb_root *root, unsigned int reg)
552 struct rb_node *node;
553 struct snd_soc_rbtree_node *rbnode;
554 unsigned int base_reg, top_reg;
556 node = root->rb_node;
557 while (node) {
558 rbnode = container_of(node, struct snd_soc_rbtree_node, node);
559 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
560 if (reg >= base_reg && reg <= top_reg)
561 return rbnode;
562 else if (reg > top_reg)
563 node = node->rb_right;
564 else if (reg < base_reg)
565 node = node->rb_left;
568 return NULL;
571 static int snd_soc_rbtree_insert(struct rb_root *root,
572 struct snd_soc_rbtree_node *rbnode)
574 struct rb_node **new, *parent;
575 struct snd_soc_rbtree_node *rbnode_tmp;
576 unsigned int base_reg_tmp, top_reg_tmp;
577 unsigned int base_reg;
579 parent = NULL;
580 new = &root->rb_node;
581 while (*new) {
582 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
583 node);
584 /* base and top registers of the current rbnode */
585 snd_soc_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp,
586 &top_reg_tmp);
587 /* base register of the rbnode to be added */
588 base_reg = rbnode->base_reg;
589 parent = *new;
590 /* if this register has already been inserted, just return */
591 if (base_reg >= base_reg_tmp &&
592 base_reg <= top_reg_tmp)
593 return 0;
594 else if (base_reg > top_reg_tmp)
595 new = &((*new)->rb_right);
596 else if (base_reg < base_reg_tmp)
597 new = &((*new)->rb_left);
600 /* insert the node into the rbtree */
601 rb_link_node(&rbnode->node, parent, new);
602 rb_insert_color(&rbnode->node, root);
604 return 1;
607 static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
609 struct snd_soc_rbtree_ctx *rbtree_ctx;
610 struct rb_node *node;
611 struct snd_soc_rbtree_node *rbnode;
612 unsigned int regtmp;
613 unsigned int val, def;
614 int ret;
615 int i;
617 rbtree_ctx = codec->reg_cache;
618 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
619 rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
620 for (i = 0; i < rbnode->blklen; ++i) {
621 regtmp = rbnode->base_reg + i;
622 WARN_ON(codec->writable_register &&
623 codec->writable_register(codec, regtmp));
624 val = snd_soc_rbtree_get_register(rbnode, i);
625 def = snd_soc_get_cache_val(codec->reg_def_copy, i,
626 rbnode->word_size);
627 if (val == def)
628 continue;
630 codec->cache_bypass = 1;
631 ret = snd_soc_write(codec, regtmp, val);
632 codec->cache_bypass = 0;
633 if (ret)
634 return ret;
635 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
636 regtmp, val);
640 return 0;
643 static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node *rbnode,
644 unsigned int pos, unsigned int reg,
645 unsigned int value)
647 u8 *blk;
649 blk = krealloc(rbnode->block,
650 (rbnode->blklen + 1) * rbnode->word_size, GFP_KERNEL);
651 if (!blk)
652 return -ENOMEM;
654 /* insert the register value in the correct place in the rbnode block */
655 memmove(blk + (pos + 1) * rbnode->word_size,
656 blk + pos * rbnode->word_size,
657 (rbnode->blklen - pos) * rbnode->word_size);
659 /* update the rbnode block, its size and the base register */
660 rbnode->block = blk;
661 rbnode->blklen++;
662 if (!pos)
663 rbnode->base_reg = reg;
665 snd_soc_rbtree_set_register(rbnode, pos, value);
666 return 0;
669 static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
670 unsigned int reg, unsigned int value)
672 struct snd_soc_rbtree_ctx *rbtree_ctx;
673 struct snd_soc_rbtree_node *rbnode, *rbnode_tmp;
674 struct rb_node *node;
675 unsigned int val;
676 unsigned int reg_tmp;
677 unsigned int base_reg, top_reg;
678 unsigned int pos;
679 int i;
680 int ret;
682 rbtree_ctx = codec->reg_cache;
683 /* look up the required register in the cached rbnode */
684 rbnode = rbtree_ctx->cached_rbnode;
685 if (rbnode) {
686 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
687 if (reg >= base_reg && reg <= top_reg) {
688 reg_tmp = reg - base_reg;
689 val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
690 if (val == value)
691 return 0;
692 snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
693 return 0;
696 /* if we can't locate it in the cached rbnode we'll have
697 * to traverse the rbtree looking for it.
699 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
700 if (rbnode) {
701 reg_tmp = reg - rbnode->base_reg;
702 val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
703 if (val == value)
704 return 0;
705 snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
706 rbtree_ctx->cached_rbnode = rbnode;
707 } else {
708 /* bail out early, no need to create the rbnode yet */
709 if (!value)
710 return 0;
711 /* look for an adjacent register to the one we are about to add */
712 for (node = rb_first(&rbtree_ctx->root); node;
713 node = rb_next(node)) {
714 rbnode_tmp = rb_entry(node, struct snd_soc_rbtree_node, node);
715 for (i = 0; i < rbnode_tmp->blklen; ++i) {
716 reg_tmp = rbnode_tmp->base_reg + i;
717 if (abs(reg_tmp - reg) != 1)
718 continue;
719 /* decide where in the block to place our register */
720 if (reg_tmp + 1 == reg)
721 pos = i + 1;
722 else
723 pos = i;
724 ret = snd_soc_rbtree_insert_to_block(rbnode_tmp, pos,
725 reg, value);
726 if (ret)
727 return ret;
728 rbtree_ctx->cached_rbnode = rbnode_tmp;
729 return 0;
732 /* we did not manage to find a place to insert it in an existing
733 * block so create a new rbnode with a single register in its block.
734 * This block will get populated further if any other adjacent
735 * registers get modified in the future.
737 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
738 if (!rbnode)
739 return -ENOMEM;
740 rbnode->blklen = 1;
741 rbnode->base_reg = reg;
742 rbnode->word_size = codec->driver->reg_word_size;
743 rbnode->block = kmalloc(rbnode->blklen * rbnode->word_size,
744 GFP_KERNEL);
745 if (!rbnode->block) {
746 kfree(rbnode);
747 return -ENOMEM;
749 snd_soc_rbtree_set_register(rbnode, 0, value);
750 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
751 rbtree_ctx->cached_rbnode = rbnode;
754 return 0;
757 static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
758 unsigned int reg, unsigned int *value)
760 struct snd_soc_rbtree_ctx *rbtree_ctx;
761 struct snd_soc_rbtree_node *rbnode;
762 unsigned int base_reg, top_reg;
763 unsigned int reg_tmp;
765 rbtree_ctx = codec->reg_cache;
766 /* look up the required register in the cached rbnode */
767 rbnode = rbtree_ctx->cached_rbnode;
768 if (rbnode) {
769 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
770 if (reg >= base_reg && reg <= top_reg) {
771 reg_tmp = reg - base_reg;
772 *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
773 return 0;
776 /* if we can't locate it in the cached rbnode we'll have
777 * to traverse the rbtree looking for it.
779 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
780 if (rbnode) {
781 reg_tmp = reg - rbnode->base_reg;
782 *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
783 rbtree_ctx->cached_rbnode = rbnode;
784 } else {
785 /* uninitialized registers default to 0 */
786 *value = 0;
789 return 0;
792 static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
794 struct rb_node *next;
795 struct snd_soc_rbtree_ctx *rbtree_ctx;
796 struct snd_soc_rbtree_node *rbtree_node;
798 /* if we've already been called then just return */
799 rbtree_ctx = codec->reg_cache;
800 if (!rbtree_ctx)
801 return 0;
803 /* free up the rbtree */
804 next = rb_first(&rbtree_ctx->root);
805 while (next) {
806 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
807 next = rb_next(&rbtree_node->node);
808 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
809 kfree(rbtree_node->block);
810 kfree(rbtree_node);
813 /* release the resources */
814 kfree(codec->reg_cache);
815 codec->reg_cache = NULL;
817 return 0;
820 static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
822 struct snd_soc_rbtree_ctx *rbtree_ctx;
823 unsigned int word_size;
824 unsigned int val;
825 int i;
826 int ret;
828 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
829 if (!codec->reg_cache)
830 return -ENOMEM;
832 rbtree_ctx = codec->reg_cache;
833 rbtree_ctx->root = RB_ROOT;
834 rbtree_ctx->cached_rbnode = NULL;
836 if (!codec->reg_def_copy)
837 return 0;
839 word_size = codec->driver->reg_word_size;
840 for (i = 0; i < codec->driver->reg_cache_size; ++i) {
841 val = snd_soc_get_cache_val(codec->reg_def_copy, i,
842 word_size);
843 if (!val)
844 continue;
845 ret = snd_soc_rbtree_cache_write(codec, i, val);
846 if (ret)
847 goto err;
850 return 0;
852 err:
853 snd_soc_cache_exit(codec);
854 return ret;
857 #ifdef CONFIG_SND_SOC_CACHE_LZO
858 struct snd_soc_lzo_ctx {
859 void *wmem;
860 void *dst;
861 const void *src;
862 size_t src_len;
863 size_t dst_len;
864 size_t decompressed_size;
865 unsigned long *sync_bmp;
866 int sync_bmp_nbits;
869 #define LZO_BLOCK_NUM 8
870 static int snd_soc_lzo_block_count(void)
872 return LZO_BLOCK_NUM;
875 static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
877 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
878 if (!lzo_ctx->wmem)
879 return -ENOMEM;
880 return 0;
883 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
885 size_t compress_size;
886 int ret;
888 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
889 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
890 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
891 return -EINVAL;
892 lzo_ctx->dst_len = compress_size;
893 return 0;
896 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
898 size_t dst_len;
899 int ret;
901 dst_len = lzo_ctx->dst_len;
902 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
903 lzo_ctx->dst, &dst_len);
904 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
905 return -EINVAL;
906 return 0;
909 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
910 struct snd_soc_lzo_ctx *lzo_ctx)
912 int ret;
914 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
915 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
916 if (!lzo_ctx->dst) {
917 lzo_ctx->dst_len = 0;
918 return -ENOMEM;
921 ret = snd_soc_lzo_compress(lzo_ctx);
922 if (ret < 0)
923 return ret;
924 return 0;
927 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
928 struct snd_soc_lzo_ctx *lzo_ctx)
930 int ret;
932 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
933 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
934 if (!lzo_ctx->dst) {
935 lzo_ctx->dst_len = 0;
936 return -ENOMEM;
939 ret = snd_soc_lzo_decompress(lzo_ctx);
940 if (ret < 0)
941 return ret;
942 return 0;
945 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
946 unsigned int reg)
948 const struct snd_soc_codec_driver *codec_drv;
950 codec_drv = codec->driver;
951 return (reg * codec_drv->reg_word_size) /
952 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
955 static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
956 unsigned int reg)
958 const struct snd_soc_codec_driver *codec_drv;
960 codec_drv = codec->driver;
961 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
962 codec_drv->reg_word_size);
965 static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
967 const struct snd_soc_codec_driver *codec_drv;
969 codec_drv = codec->driver;
970 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
973 static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
975 struct snd_soc_lzo_ctx **lzo_blocks;
976 unsigned int val;
977 int i;
978 int ret;
980 lzo_blocks = codec->reg_cache;
981 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
982 WARN_ON(codec->writable_register &&
983 codec->writable_register(codec, i));
984 ret = snd_soc_cache_read(codec, i, &val);
985 if (ret)
986 return ret;
987 codec->cache_bypass = 1;
988 ret = snd_soc_write(codec, i, val);
989 codec->cache_bypass = 0;
990 if (ret)
991 return ret;
992 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
993 i, val);
996 return 0;
999 static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
1000 unsigned int reg, unsigned int value)
1002 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1003 int ret, blkindex, blkpos;
1004 size_t blksize, tmp_dst_len;
1005 void *tmp_dst;
1007 /* index of the compressed lzo block */
1008 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1009 /* register index within the decompressed block */
1010 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1011 /* size of the compressed block */
1012 blksize = snd_soc_lzo_get_blksize(codec);
1013 lzo_blocks = codec->reg_cache;
1014 lzo_block = lzo_blocks[blkindex];
1016 /* save the pointer and length of the compressed block */
1017 tmp_dst = lzo_block->dst;
1018 tmp_dst_len = lzo_block->dst_len;
1020 /* prepare the source to be the compressed block */
1021 lzo_block->src = lzo_block->dst;
1022 lzo_block->src_len = lzo_block->dst_len;
1024 /* decompress the block */
1025 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1026 if (ret < 0) {
1027 kfree(lzo_block->dst);
1028 goto out;
1031 /* write the new value to the cache */
1032 if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
1033 codec->driver->reg_word_size)) {
1034 kfree(lzo_block->dst);
1035 goto out;
1038 /* prepare the source to be the decompressed block */
1039 lzo_block->src = lzo_block->dst;
1040 lzo_block->src_len = lzo_block->dst_len;
1042 /* compress the block */
1043 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1044 if (ret < 0) {
1045 kfree(lzo_block->dst);
1046 kfree(lzo_block->src);
1047 goto out;
1050 /* set the bit so we know we have to sync this register */
1051 set_bit(reg, lzo_block->sync_bmp);
1052 kfree(tmp_dst);
1053 kfree(lzo_block->src);
1054 return 0;
1055 out:
1056 lzo_block->dst = tmp_dst;
1057 lzo_block->dst_len = tmp_dst_len;
1058 return ret;
1061 static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1062 unsigned int reg, unsigned int *value)
1064 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1065 int ret, blkindex, blkpos;
1066 size_t blksize, tmp_dst_len;
1067 void *tmp_dst;
1069 *value = 0;
1070 /* index of the compressed lzo block */
1071 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1072 /* register index within the decompressed block */
1073 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1074 /* size of the compressed block */
1075 blksize = snd_soc_lzo_get_blksize(codec);
1076 lzo_blocks = codec->reg_cache;
1077 lzo_block = lzo_blocks[blkindex];
1079 /* save the pointer and length of the compressed block */
1080 tmp_dst = lzo_block->dst;
1081 tmp_dst_len = lzo_block->dst_len;
1083 /* prepare the source to be the compressed block */
1084 lzo_block->src = lzo_block->dst;
1085 lzo_block->src_len = lzo_block->dst_len;
1087 /* decompress the block */
1088 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1089 if (ret >= 0)
1090 /* fetch the value from the cache */
1091 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1092 codec->driver->reg_word_size);
1094 kfree(lzo_block->dst);
1095 /* restore the pointer and length of the compressed block */
1096 lzo_block->dst = tmp_dst;
1097 lzo_block->dst_len = tmp_dst_len;
1098 return 0;
1101 static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1103 struct snd_soc_lzo_ctx **lzo_blocks;
1104 int i, blkcount;
1106 lzo_blocks = codec->reg_cache;
1107 if (!lzo_blocks)
1108 return 0;
1110 blkcount = snd_soc_lzo_block_count();
1112 * the pointer to the bitmap used for syncing the cache
1113 * is shared amongst all lzo_blocks. Ensure it is freed
1114 * only once.
1116 if (lzo_blocks[0])
1117 kfree(lzo_blocks[0]->sync_bmp);
1118 for (i = 0; i < blkcount; ++i) {
1119 if (lzo_blocks[i]) {
1120 kfree(lzo_blocks[i]->wmem);
1121 kfree(lzo_blocks[i]->dst);
1123 /* each lzo_block is a pointer returned by kmalloc or NULL */
1124 kfree(lzo_blocks[i]);
1126 kfree(lzo_blocks);
1127 codec->reg_cache = NULL;
1128 return 0;
1131 static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1133 struct snd_soc_lzo_ctx **lzo_blocks;
1134 size_t bmp_size;
1135 const struct snd_soc_codec_driver *codec_drv;
1136 int ret, tofree, i, blksize, blkcount;
1137 const char *p, *end;
1138 unsigned long *sync_bmp;
1140 ret = 0;
1141 codec_drv = codec->driver;
1144 * If we have not been given a default register cache
1145 * then allocate a dummy zero-ed out region, compress it
1146 * and remember to free it afterwards.
1148 tofree = 0;
1149 if (!codec->reg_def_copy)
1150 tofree = 1;
1152 if (!codec->reg_def_copy) {
1153 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
1154 if (!codec->reg_def_copy)
1155 return -ENOMEM;
1158 blkcount = snd_soc_lzo_block_count();
1159 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1160 GFP_KERNEL);
1161 if (!codec->reg_cache) {
1162 ret = -ENOMEM;
1163 goto err_tofree;
1165 lzo_blocks = codec->reg_cache;
1168 * allocate a bitmap to be used when syncing the cache with
1169 * the hardware. Each time a register is modified, the corresponding
1170 * bit is set in the bitmap, so we know that we have to sync
1171 * that register.
1173 bmp_size = codec_drv->reg_cache_size;
1174 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
1175 GFP_KERNEL);
1176 if (!sync_bmp) {
1177 ret = -ENOMEM;
1178 goto err;
1180 bitmap_zero(sync_bmp, bmp_size);
1182 /* allocate the lzo blocks and initialize them */
1183 for (i = 0; i < blkcount; ++i) {
1184 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1185 GFP_KERNEL);
1186 if (!lzo_blocks[i]) {
1187 kfree(sync_bmp);
1188 ret = -ENOMEM;
1189 goto err;
1191 lzo_blocks[i]->sync_bmp = sync_bmp;
1192 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
1193 /* alloc the working space for the compressed block */
1194 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1195 if (ret < 0)
1196 goto err;
1199 blksize = snd_soc_lzo_get_blksize(codec);
1200 p = codec->reg_def_copy;
1201 end = codec->reg_def_copy + codec->reg_size;
1202 /* compress the register map and fill the lzo blocks */
1203 for (i = 0; i < blkcount; ++i, p += blksize) {
1204 lzo_blocks[i]->src = p;
1205 if (p + blksize > end)
1206 lzo_blocks[i]->src_len = end - p;
1207 else
1208 lzo_blocks[i]->src_len = blksize;
1209 ret = snd_soc_lzo_compress_cache_block(codec,
1210 lzo_blocks[i]);
1211 if (ret < 0)
1212 goto err;
1213 lzo_blocks[i]->decompressed_size =
1214 lzo_blocks[i]->src_len;
1217 if (tofree) {
1218 kfree(codec->reg_def_copy);
1219 codec->reg_def_copy = NULL;
1221 return 0;
1222 err:
1223 snd_soc_cache_exit(codec);
1224 err_tofree:
1225 if (tofree) {
1226 kfree(codec->reg_def_copy);
1227 codec->reg_def_copy = NULL;
1229 return ret;
1231 #endif
1233 static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1235 int i;
1236 int ret;
1237 const struct snd_soc_codec_driver *codec_drv;
1238 unsigned int val;
1240 codec_drv = codec->driver;
1241 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
1242 WARN_ON(codec->writable_register &&
1243 codec->writable_register(codec, i));
1244 ret = snd_soc_cache_read(codec, i, &val);
1245 if (ret)
1246 return ret;
1247 if (codec->reg_def_copy)
1248 if (snd_soc_get_cache_val(codec->reg_def_copy,
1249 i, codec_drv->reg_word_size) == val)
1250 continue;
1251 ret = snd_soc_write(codec, i, val);
1252 if (ret)
1253 return ret;
1254 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1255 i, val);
1257 return 0;
1260 static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1261 unsigned int reg, unsigned int value)
1263 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1264 codec->driver->reg_word_size);
1265 return 0;
1268 static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1269 unsigned int reg, unsigned int *value)
1271 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1272 codec->driver->reg_word_size);
1273 return 0;
1276 static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1278 if (!codec->reg_cache)
1279 return 0;
1280 kfree(codec->reg_cache);
1281 codec->reg_cache = NULL;
1282 return 0;
1285 static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1287 const struct snd_soc_codec_driver *codec_drv;
1289 codec_drv = codec->driver;
1291 if (codec->reg_def_copy)
1292 codec->reg_cache = kmemdup(codec->reg_def_copy,
1293 codec->reg_size, GFP_KERNEL);
1294 else
1295 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
1296 if (!codec->reg_cache)
1297 return -ENOMEM;
1299 return 0;
1302 /* an array of all supported compression types */
1303 static const struct snd_soc_cache_ops cache_types[] = {
1304 /* Flat *must* be the first entry for fallback */
1306 .id = SND_SOC_FLAT_COMPRESSION,
1307 .name = "flat",
1308 .init = snd_soc_flat_cache_init,
1309 .exit = snd_soc_flat_cache_exit,
1310 .read = snd_soc_flat_cache_read,
1311 .write = snd_soc_flat_cache_write,
1312 .sync = snd_soc_flat_cache_sync
1314 #ifdef CONFIG_SND_SOC_CACHE_LZO
1316 .id = SND_SOC_LZO_COMPRESSION,
1317 .name = "LZO",
1318 .init = snd_soc_lzo_cache_init,
1319 .exit = snd_soc_lzo_cache_exit,
1320 .read = snd_soc_lzo_cache_read,
1321 .write = snd_soc_lzo_cache_write,
1322 .sync = snd_soc_lzo_cache_sync
1324 #endif
1326 .id = SND_SOC_RBTREE_COMPRESSION,
1327 .name = "rbtree",
1328 .init = snd_soc_rbtree_cache_init,
1329 .exit = snd_soc_rbtree_cache_exit,
1330 .read = snd_soc_rbtree_cache_read,
1331 .write = snd_soc_rbtree_cache_write,
1332 .sync = snd_soc_rbtree_cache_sync
1336 int snd_soc_cache_init(struct snd_soc_codec *codec)
1338 int i;
1340 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
1341 if (cache_types[i].id == codec->compress_type)
1342 break;
1344 /* Fall back to flat compression */
1345 if (i == ARRAY_SIZE(cache_types)) {
1346 dev_warn(codec->dev, "Could not match compress type: %d\n",
1347 codec->compress_type);
1348 i = 0;
1351 mutex_init(&codec->cache_rw_mutex);
1352 codec->cache_ops = &cache_types[i];
1354 if (codec->cache_ops->init) {
1355 if (codec->cache_ops->name)
1356 dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1357 codec->cache_ops->name, codec->name);
1358 return codec->cache_ops->init(codec);
1360 return -ENOSYS;
1364 * NOTE: keep in mind that this function might be called
1365 * multiple times.
1367 int snd_soc_cache_exit(struct snd_soc_codec *codec)
1369 if (codec->cache_ops && codec->cache_ops->exit) {
1370 if (codec->cache_ops->name)
1371 dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1372 codec->cache_ops->name, codec->name);
1373 return codec->cache_ops->exit(codec);
1375 return -ENOSYS;
1379 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1381 * @codec: CODEC to configure.
1382 * @reg: The register index.
1383 * @value: The value to be returned.
1385 int snd_soc_cache_read(struct snd_soc_codec *codec,
1386 unsigned int reg, unsigned int *value)
1388 int ret;
1390 mutex_lock(&codec->cache_rw_mutex);
1392 if (value && codec->cache_ops && codec->cache_ops->read) {
1393 ret = codec->cache_ops->read(codec, reg, value);
1394 mutex_unlock(&codec->cache_rw_mutex);
1395 return ret;
1398 mutex_unlock(&codec->cache_rw_mutex);
1399 return -ENOSYS;
1401 EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1404 * snd_soc_cache_write: Set the value of a given register in the cache.
1406 * @codec: CODEC to configure.
1407 * @reg: The register index.
1408 * @value: The new register value.
1410 int snd_soc_cache_write(struct snd_soc_codec *codec,
1411 unsigned int reg, unsigned int value)
1413 int ret;
1415 mutex_lock(&codec->cache_rw_mutex);
1417 if (codec->cache_ops && codec->cache_ops->write) {
1418 ret = codec->cache_ops->write(codec, reg, value);
1419 mutex_unlock(&codec->cache_rw_mutex);
1420 return ret;
1423 mutex_unlock(&codec->cache_rw_mutex);
1424 return -ENOSYS;
1426 EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1429 * snd_soc_cache_sync: Sync the register cache with the hardware.
1431 * @codec: CODEC to configure.
1433 * Any registers that should not be synced should be marked as
1434 * volatile. In general drivers can choose not to use the provided
1435 * syncing functionality if they so require.
1437 int snd_soc_cache_sync(struct snd_soc_codec *codec)
1439 int ret;
1440 const char *name;
1442 if (!codec->cache_sync) {
1443 return 0;
1446 if (!codec->cache_ops || !codec->cache_ops->sync)
1447 return -ENOSYS;
1449 if (codec->cache_ops->name)
1450 name = codec->cache_ops->name;
1451 else
1452 name = "unknown";
1454 if (codec->cache_ops->name)
1455 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1456 codec->cache_ops->name, codec->name);
1457 trace_snd_soc_cache_sync(codec, name, "start");
1458 ret = codec->cache_ops->sync(codec);
1459 if (!ret)
1460 codec->cache_sync = 0;
1461 trace_snd_soc_cache_sync(codec, name, "end");
1462 return ret;
1464 EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
1466 static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1467 unsigned int reg)
1469 const struct snd_soc_codec_driver *codec_drv;
1470 unsigned int min, max, index;
1472 codec_drv = codec->driver;
1473 min = 0;
1474 max = codec_drv->reg_access_size - 1;
1475 do {
1476 index = (min + max) / 2;
1477 if (codec_drv->reg_access_default[index].reg == reg)
1478 return index;
1479 if (codec_drv->reg_access_default[index].reg < reg)
1480 min = index + 1;
1481 else
1482 max = index;
1483 } while (min <= max);
1484 return -1;
1487 int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1488 unsigned int reg)
1490 int index;
1492 if (reg >= codec->driver->reg_cache_size)
1493 return 1;
1494 index = snd_soc_get_reg_access_index(codec, reg);
1495 if (index < 0)
1496 return 0;
1497 return codec->driver->reg_access_default[index].vol;
1499 EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1501 int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1502 unsigned int reg)
1504 int index;
1506 if (reg >= codec->driver->reg_cache_size)
1507 return 1;
1508 index = snd_soc_get_reg_access_index(codec, reg);
1509 if (index < 0)
1510 return 0;
1511 return codec->driver->reg_access_default[index].read;
1513 EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
1515 int snd_soc_default_writable_register(struct snd_soc_codec *codec,
1516 unsigned int reg)
1518 int index;
1520 if (reg >= codec->driver->reg_cache_size)
1521 return 1;
1522 index = snd_soc_get_reg_access_index(codec, reg);
1523 if (index < 0)
1524 return 0;
1525 return codec->driver->reg_access_default[index].write;
1527 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);