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;
29 ret = spi_write(spi, data, len);
37 static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg,
38 unsigned int value, const void *data, int len)
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);
50 if (codec->cache_only) {
51 codec->cache_sync = 1;
55 ret = codec->hw_write(codec->control_data, data, len);
64 static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
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)
75 BUG_ON(!codec->hw_read);
76 return codec->hw_read(codec, reg);
79 ret = snd_soc_cache_read(codec, reg, &val);
85 static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
90 data = cpu_to_be16((reg << 12) | (value & 0xffffff));
92 return do_hw_write(codec, reg, value, &data, 2);
95 static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
100 data = cpu_to_be16((reg << 9) | (value & 0x1ff));
102 return do_hw_write(codec, reg, value, &data, 2);
105 static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
112 data[1] = value & 0xff;
114 return do_hw_write(codec, reg, value, data, 2);
117 static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
121 u16 val = cpu_to_be16(value);
124 memcpy(&data[1], &val, sizeof(val));
126 return do_hw_write(codec, reg, value, data, 3);
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)
134 struct i2c_msg xfer[2];
136 struct i2c_client *client = codec->control_data;
139 xfer[0].addr = client->addr;
141 xfer[0].len = reglen;
145 xfer[1].addr = client->addr;
146 xfer[1].flags = I2C_M_RD;
147 xfer[1].len = datalen;
150 ret = i2c_transfer(client->adapter, xfer, 2);
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,
168 ret = do_i2c_read(codec, ®, 1, &data, 1);
174 #define snd_soc_8_8_read_i2c NULL
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,
185 ret = do_i2c_read(codec, ®, 1, &data, 2);
188 return (data >> 8) | ((data & 0xff) << 8);
191 #define snd_soc_8_16_read_i2c NULL
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,
202 ret = do_i2c_read(codec, ®, 2, &data, 1);
208 #define snd_soc_16_8_read_i2c NULL
211 static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
215 u16 rval = cpu_to_be16(reg);
217 memcpy(data, &rval, sizeof(rval));
220 return do_hw_write(codec, reg, value, data, 3);
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,
227 u16 reg = cpu_to_be16(r);
231 ret = do_i2c_read(codec, ®, 2, &data, 2);
234 return be16_to_cpu(data);
237 #define snd_soc_16_16_read_i2c NULL
240 static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
245 data[0] = cpu_to_be16(reg);
246 data[1] = cpu_to_be16(value);
248 return do_hw_write(codec, reg, value, data, sizeof(data));
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.
257 static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg,
258 const void *data, size_t len)
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
267 if (!codec->cache_bypass
268 && !snd_soc_codec_volatile_register(codec, reg)
269 && reg < codec->driver->reg_cache_size)
272 switch (codec->control_type) {
273 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
275 ret = i2c_master_send(codec->control_data, data, len);
278 #if defined(CONFIG_SPI_MASTER)
280 ret = spi_write(codec->control_data, data, len);
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);
303 .addr_bits = 4, .data_bits = 12,
304 .write = snd_soc_4_12_write,
307 .addr_bits = 7, .data_bits = 9,
308 .write = snd_soc_7_9_write,
311 .addr_bits = 8, .data_bits = 8,
312 .write = snd_soc_8_8_write,
313 .i2c_read = snd_soc_8_8_read_i2c,
316 .addr_bits = 8, .data_bits = 16,
317 .write = snd_soc_8_16_write,
318 .i2c_read = snd_soc_8_16_read_i2c,
321 .addr_bits = 16, .data_bits = 8,
322 .write = snd_soc_16_8_write,
323 .i2c_read = snd_soc_16_8_read_i2c,
326 .addr_bits = 16, .data_bits = 16,
327 .write = snd_soc_16_16_write,
328 .i2c_read = snd_soc_16_16_read_i2c,
333 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
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.
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.
345 * The caller is responsible for allocating and initialising the
348 * Note that at present this code cannot be used by CODECs with
349 * volatile registers.
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)
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)
361 if (i == ARRAY_SIZE(io_types)) {
363 "No I/O functions for %d bit address %d bit data\n",
364 addr_bits, data_bits);
368 codec->write = io_types[i].write;
369 codec->read = hw_read;
370 codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;
377 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
378 codec->hw_write = (hw_write_t)i2c_master_send;
380 if (io_types[i].i2c_read)
381 codec->hw_read = io_types[i].i2c_read;
383 codec->control_data = container_of(codec->dev,
389 #ifdef CONFIG_SPI_MASTER
390 codec->hw_write = do_spi_write;
393 codec->control_data = container_of(codec->dev,
401 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
403 static bool snd_soc_set_cache_val(void *base, unsigned int idx,
404 unsigned int val, unsigned int word_size)
409 if (cache[idx] == val)
416 if (cache[idx] == val)
427 static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
428 unsigned int word_size)
435 const u8 *cache = base;
439 const u16 *cache = base;
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));
457 struct snd_soc_rbtree_ctx {
459 struct snd_soc_rbtree_node *cached_rbnode;
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)
466 *base = rbnode->base_reg;
467 *top = rbnode->base_reg + rbnode->blklen - 1;
470 static unsigned int snd_soc_rbtree_get_register(
471 struct snd_soc_rbtree_node *rbnode, unsigned int idx)
475 switch (rbnode->word_size) {
477 u8 *p = rbnode->block;
482 u16 *p = rbnode->block;
493 static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node *rbnode,
494 unsigned int idx, unsigned int val)
496 switch (rbnode->word_size) {
498 u8 *p = rbnode->block;
503 u16 *p = rbnode->block;
513 static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
514 struct rb_root *root, unsigned int reg)
516 struct rb_node *node;
517 struct snd_soc_rbtree_node *rbnode;
518 unsigned int base_reg, top_reg;
520 node = root->rb_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)
526 else if (reg > top_reg)
527 node = node->rb_right;
528 else if (reg < base_reg)
529 node = node->rb_left;
535 static int snd_soc_rbtree_insert(struct rb_root *root,
536 struct snd_soc_rbtree_node *rbnode)
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;
544 new = &root->rb_node;
546 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
548 /* base and top registers of the current rbnode */
549 snd_soc_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp,
551 /* base register of the rbnode to be added */
552 base_reg = rbnode->base_reg;
554 /* if this register has already been inserted, just return */
555 if (base_reg >= base_reg_tmp &&
556 base_reg <= top_reg_tmp)
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);
564 /* insert the node into the rbtree */
565 rb_link_node(&rbnode->node, parent, new);
566 rb_insert_color(&rbnode->node, root);
571 static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
573 struct snd_soc_rbtree_ctx *rbtree_ctx;
574 struct rb_node *node;
575 struct snd_soc_rbtree_node *rbnode;
577 unsigned int val, def;
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,
594 codec->cache_bypass = 1;
595 ret = snd_soc_write(codec, regtmp, val);
596 codec->cache_bypass = 0;
599 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
607 static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node *rbnode,
608 unsigned int pos, unsigned int reg,
613 blk = krealloc(rbnode->block,
614 (rbnode->blklen + 1) * rbnode->word_size, GFP_KERNEL);
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);
623 /* update the rbnode block, its size and the base register */
627 rbnode->base_reg = reg;
629 snd_soc_rbtree_set_register(rbnode, pos, value);
633 static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
634 unsigned int reg, unsigned int value)
636 struct snd_soc_rbtree_ctx *rbtree_ctx;
637 struct snd_soc_rbtree_node *rbnode, *rbnode_tmp;
638 struct rb_node *node;
640 unsigned int reg_tmp;
641 unsigned int base_reg, top_reg;
646 rbtree_ctx = codec->reg_cache;
647 /* look up the required register in the cached rbnode */
648 rbnode = rbtree_ctx->cached_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);
656 snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
660 /* if we can't locate it in the cached rbnode we'll have
661 * to traverse the rbtree looking for it.
663 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
665 reg_tmp = reg - rbnode->base_reg;
666 val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
669 snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
670 rbtree_ctx->cached_rbnode = rbnode;
672 /* bail out early, no need to create the rbnode yet */
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)
683 /* decide where in the block to place our register */
684 if (reg_tmp + 1 == reg)
688 ret = snd_soc_rbtree_insert_to_block(rbnode_tmp, pos,
692 rbtree_ctx->cached_rbnode = rbnode_tmp;
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.
701 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
705 rbnode->base_reg = reg;
706 rbnode->word_size = codec->driver->reg_word_size;
707 rbnode->block = kmalloc(rbnode->blklen * rbnode->word_size,
709 if (!rbnode->block) {
713 snd_soc_rbtree_set_register(rbnode, 0, value);
714 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
715 rbtree_ctx->cached_rbnode = rbnode;
721 static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
722 unsigned int reg, unsigned int *value)
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;
729 rbtree_ctx = codec->reg_cache;
730 /* look up the required register in the cached rbnode */
731 rbnode = rbtree_ctx->cached_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);
740 /* if we can't locate it in the cached rbnode we'll have
741 * to traverse the rbtree looking for it.
743 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
745 reg_tmp = reg - rbnode->base_reg;
746 *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
747 rbtree_ctx->cached_rbnode = rbnode;
749 /* uninitialized registers default to 0 */
756 static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
758 struct rb_node *next;
759 struct snd_soc_rbtree_ctx *rbtree_ctx;
760 struct snd_soc_rbtree_node *rbtree_node;
762 /* if we've already been called then just return */
763 rbtree_ctx = codec->reg_cache;
767 /* free up the rbtree */
768 next = rb_first(&rbtree_ctx->root);
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);
777 /* release the resources */
778 kfree(codec->reg_cache);
779 codec->reg_cache = NULL;
784 static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
786 struct snd_soc_rbtree_ctx *rbtree_ctx;
787 unsigned int word_size;
792 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
793 if (!codec->reg_cache)
796 rbtree_ctx = codec->reg_cache;
797 rbtree_ctx->root = RB_ROOT;
798 rbtree_ctx->cached_rbnode = NULL;
800 if (!codec->reg_def_copy)
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,
809 ret = snd_soc_rbtree_cache_write(codec, i, val);
817 snd_soc_cache_exit(codec);
821 #ifdef CONFIG_SND_SOC_CACHE_LZO
822 struct snd_soc_lzo_ctx {
828 size_t decompressed_size;
829 unsigned long *sync_bmp;
833 #define LZO_BLOCK_NUM 8
834 static int snd_soc_lzo_block_count(void)
836 return LZO_BLOCK_NUM;
839 static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
841 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
847 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
849 size_t compress_size;
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)
856 lzo_ctx->dst_len = compress_size;
860 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
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)
873 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
874 struct snd_soc_lzo_ctx *lzo_ctx)
878 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
879 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
881 lzo_ctx->dst_len = 0;
885 ret = snd_soc_lzo_compress(lzo_ctx);
891 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
892 struct snd_soc_lzo_ctx *lzo_ctx)
896 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
897 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
899 lzo_ctx->dst_len = 0;
903 ret = snd_soc_lzo_decompress(lzo_ctx);
909 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
912 const struct snd_soc_codec_driver *codec_drv;
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());
919 static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
922 const struct snd_soc_codec_driver *codec_drv;
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);
929 static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
931 const struct snd_soc_codec_driver *codec_drv;
933 codec_drv = codec->driver;
934 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
937 static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
939 struct snd_soc_lzo_ctx **lzo_blocks;
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);
951 codec->cache_bypass = 1;
952 ret = snd_soc_write(codec, i, val);
953 codec->cache_bypass = 0;
956 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
963 static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
964 unsigned int reg, unsigned int value)
966 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
967 int ret, blkindex, blkpos;
968 size_t blksize, tmp_dst_len;
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];
980 /* save the pointer and length of the compressed block */
981 tmp_dst = lzo_block->dst;
982 tmp_dst_len = lzo_block->dst_len;
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;
988 /* decompress the block */
989 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
991 kfree(lzo_block->dst);
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);
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;
1006 /* compress the block */
1007 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1009 kfree(lzo_block->dst);
1010 kfree(lzo_block->src);
1014 /* set the bit so we know we have to sync this register */
1015 set_bit(reg, lzo_block->sync_bmp);
1017 kfree(lzo_block->src);
1020 lzo_block->dst = tmp_dst;
1021 lzo_block->dst_len = tmp_dst_len;
1025 static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1026 unsigned int reg, unsigned int *value)
1028 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1029 int ret, blkindex, blkpos;
1030 size_t blksize, tmp_dst_len;
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];
1043 /* save the pointer and length of the compressed block */
1044 tmp_dst = lzo_block->dst;
1045 tmp_dst_len = lzo_block->dst_len;
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;
1051 /* decompress the block */
1052 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1054 /* fetch the value from the cache */
1055 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1056 codec->driver->reg_word_size);
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;
1065 static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1067 struct snd_soc_lzo_ctx **lzo_blocks;
1070 lzo_blocks = codec->reg_cache;
1074 blkcount = snd_soc_lzo_block_count();
1076 * the pointer to the bitmap used for syncing the cache
1077 * is shared amongst all lzo_blocks. Ensure it is freed
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);
1087 /* each lzo_block is a pointer returned by kmalloc or NULL */
1088 kfree(lzo_blocks[i]);
1091 codec->reg_cache = NULL;
1095 static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1097 struct snd_soc_lzo_ctx **lzo_blocks;
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;
1105 codec_drv = codec->driver;
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.
1113 if (!codec->reg_def_copy)
1116 if (!codec->reg_def_copy) {
1117 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
1118 if (!codec->reg_def_copy)
1122 blkcount = snd_soc_lzo_block_count();
1123 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1125 if (!codec->reg_cache) {
1129 lzo_blocks = codec->reg_cache;
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
1137 bmp_size = codec_drv->reg_cache_size;
1138 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
1144 bitmap_zero(sync_bmp, bmp_size);
1146 /* allocate the lzo blocks and initialize them */
1147 for (i = 0; i < blkcount; ++i) {
1148 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1150 if (!lzo_blocks[i]) {
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]);
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;
1172 lzo_blocks[i]->src_len = blksize;
1173 ret = snd_soc_lzo_compress_cache_block(codec,
1177 lzo_blocks[i]->decompressed_size =
1178 lzo_blocks[i]->src_len;
1182 kfree(codec->reg_def_copy);
1183 codec->reg_def_copy = NULL;
1187 snd_soc_cache_exit(codec);
1190 kfree(codec->reg_def_copy);
1191 codec->reg_def_copy = NULL;
1197 static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1201 const struct snd_soc_codec_driver *codec_drv;
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);
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)
1215 ret = snd_soc_write(codec, i, val);
1218 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1224 static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1225 unsigned int reg, unsigned int value)
1227 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1228 codec->driver->reg_word_size);
1232 static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1233 unsigned int reg, unsigned int *value)
1235 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1236 codec->driver->reg_word_size);
1240 static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1242 if (!codec->reg_cache)
1244 kfree(codec->reg_cache);
1245 codec->reg_cache = NULL;
1249 static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1251 const struct snd_soc_codec_driver *codec_drv;
1253 codec_drv = codec->driver;
1255 if (codec->reg_def_copy)
1256 codec->reg_cache = kmemdup(codec->reg_def_copy,
1257 codec->reg_size, GFP_KERNEL);
1259 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
1260 if (!codec->reg_cache)
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 */
1270 .id = SND_SOC_FLAT_COMPRESSION,
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
1278 #ifdef CONFIG_SND_SOC_CACHE_LZO
1280 .id = SND_SOC_LZO_COMPRESSION,
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
1290 .id = SND_SOC_RBTREE_COMPRESSION,
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
1300 int snd_soc_cache_init(struct snd_soc_codec *codec)
1304 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
1305 if (cache_types[i].id == codec->compress_type)
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);
1315 mutex_init(&codec->cache_rw_mutex);
1316 codec->cache_ops = &cache_types[i];
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);
1328 * NOTE: keep in mind that this function might be called
1331 int snd_soc_cache_exit(struct snd_soc_codec *codec)
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);
1343 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1345 * @codec: CODEC to configure.
1346 * @reg: The register index.
1347 * @value: The value to be returned.
1349 int snd_soc_cache_read(struct snd_soc_codec *codec,
1350 unsigned int reg, unsigned int *value)
1354 mutex_lock(&codec->cache_rw_mutex);
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);
1362 mutex_unlock(&codec->cache_rw_mutex);
1365 EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1368 * snd_soc_cache_write: Set the value of a given register in the cache.
1370 * @codec: CODEC to configure.
1371 * @reg: The register index.
1372 * @value: The new register value.
1374 int snd_soc_cache_write(struct snd_soc_codec *codec,
1375 unsigned int reg, unsigned int value)
1379 mutex_lock(&codec->cache_rw_mutex);
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);
1387 mutex_unlock(&codec->cache_rw_mutex);
1390 EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1393 * snd_soc_cache_sync: Sync the register cache with the hardware.
1395 * @codec: CODEC to configure.
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.
1401 int snd_soc_cache_sync(struct snd_soc_codec *codec)
1406 if (!codec->cache_sync) {
1410 if (!codec->cache_ops || !codec->cache_ops->sync)
1413 if (codec->cache_ops->name)
1414 name = codec->cache_ops->name;
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);
1424 codec->cache_sync = 0;
1425 trace_snd_soc_cache_sync(codec, name, "end");
1428 EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
1430 static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1433 const struct snd_soc_codec_driver *codec_drv;
1434 unsigned int min, max, index;
1436 codec_drv = codec->driver;
1438 max = codec_drv->reg_access_size - 1;
1440 index = (min + max) / 2;
1441 if (codec_drv->reg_access_default[index].reg == reg)
1443 if (codec_drv->reg_access_default[index].reg < reg)
1447 } while (min <= max);
1451 int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1456 if (reg >= codec->driver->reg_cache_size)
1458 index = snd_soc_get_reg_access_index(codec, reg);
1461 return codec->driver->reg_access_default[index].vol;
1463 EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1465 int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1470 if (reg >= codec->driver->reg_cache_size)
1472 index = snd_soc_get_reg_access_index(codec, reg);
1475 return codec->driver->reg_access_default[index].read;
1477 EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
1479 int snd_soc_default_writable_register(struct snd_soc_codec *codec,
1484 if (reg >= codec->driver->reg_cache_size)
1486 index = snd_soc_get_reg_access_index(codec, reg);
1489 return codec->driver->reg_access_default[index].write;
1491 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);