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 do_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 unsigned int snd_soc_4_12_read(struct snd_soc_codec
*codec
,
88 return do_hw_read(codec
, reg
);
91 static int snd_soc_4_12_write(struct snd_soc_codec
*codec
, unsigned int reg
,
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
,
104 return do_hw_read(codec
, reg
);
107 static int snd_soc_7_9_write(struct snd_soc_codec
*codec
, unsigned int reg
,
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
,
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
,
132 return do_hw_read(codec
, reg
);
135 static int snd_soc_8_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
139 u16 val
= cpu_to_be16(value
);
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
,
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];
160 struct i2c_client
*client
= codec
->control_data
;
163 xfer
[0].addr
= client
->addr
;
165 xfer
[0].len
= reglen
;
169 xfer
[1].addr
= client
->addr
;
170 xfer
[1].flags
= I2C_M_RD
;
171 xfer
[1].len
= datalen
;
174 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
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
,
192 ret
= do_i2c_read(codec
, ®
, 1, &data
, 1);
198 #define snd_soc_8_8_read_i2c NULL
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
,
209 ret
= do_i2c_read(codec
, ®
, 1, &data
, 2);
212 return (data
>> 8) | ((data
& 0xff) << 8);
215 #define snd_soc_8_16_read_i2c NULL
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
,
226 ret
= do_i2c_read(codec
, ®
, 2, &data
, 1);
232 #define snd_soc_16_8_read_i2c NULL
235 static unsigned int snd_soc_16_8_read(struct snd_soc_codec
*codec
,
238 return do_hw_read(codec
, reg
);
241 static int snd_soc_16_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
245 u16 rval
= cpu_to_be16(reg
);
247 memcpy(data
, &rval
, sizeof(rval
));
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
,
257 u16 reg
= cpu_to_be16(r
);
261 ret
= do_i2c_read(codec
, ®
, 2, &data
, 2);
264 return be16_to_cpu(data
);
267 #define snd_soc_16_16_read_i2c NULL
270 static unsigned int snd_soc_16_16_read(struct snd_soc_codec
*codec
,
273 return do_hw_read(codec
, reg
);
276 static int snd_soc_16_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
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
)
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
303 if (!codec
->cache_bypass
304 && !snd_soc_codec_volatile_register(codec
, reg
)
305 && reg
< codec
->driver
->reg_cache_size
)
308 switch (codec
->control_type
) {
309 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
311 ret
= i2c_master_send(codec
->control_data
, data
, len
);
314 #if defined(CONFIG_SPI_MASTER)
316 ret
= spi_write(codec
->control_data
, data
, len
);
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);
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
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
)
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
)
397 if (i
== ARRAY_SIZE(io_types
)) {
399 "No I/O functions for %d bit address %d bit data\n",
400 addr_bits
, data_bits
);
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
;
413 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
414 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
416 if (io_types
[i
].i2c_read
)
417 codec
->hw_read
= io_types
[i
].i2c_read
;
419 codec
->control_data
= container_of(codec
->dev
,
425 #ifdef CONFIG_SPI_MASTER
426 codec
->hw_write
= do_spi_write
;
429 codec
->control_data
= container_of(codec
->dev
,
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
)
445 if (cache
[idx
] == val
)
452 if (cache
[idx
] == val
)
463 static unsigned int snd_soc_get_cache_val(const void *base
, unsigned int idx
,
464 unsigned int word_size
)
471 const u8
*cache
= base
;
475 const u16
*cache
= base
;
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
{
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
)
511 switch (rbnode
->word_size
) {
513 u8
*p
= rbnode
->block
;
518 u16
*p
= rbnode
->block
;
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
) {
534 u8
*p
= rbnode
->block
;
539 u16
*p
= rbnode
->block
;
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
;
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
)
562 else if (reg
> top_reg
)
563 node
= node
->rb_right
;
564 else if (reg
< base_reg
)
565 node
= node
->rb_left
;
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
;
580 new = &root
->rb_node
;
582 rbnode_tmp
= container_of(*new, struct snd_soc_rbtree_node
,
584 /* base and top registers of the current rbnode */
585 snd_soc_rbtree_get_base_top_reg(rbnode_tmp
, &base_reg_tmp
,
587 /* base register of the rbnode to be added */
588 base_reg
= rbnode
->base_reg
;
590 /* if this register has already been inserted, just return */
591 if (base_reg
>= base_reg_tmp
&&
592 base_reg
<= top_reg_tmp
)
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
);
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
;
613 unsigned int val
, def
;
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
,
630 codec
->cache_bypass
= 1;
631 ret
= snd_soc_write(codec
, regtmp
, val
);
632 codec
->cache_bypass
= 0;
635 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
643 static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node
*rbnode
,
644 unsigned int pos
, unsigned int reg
,
649 blk
= krealloc(rbnode
->block
,
650 (rbnode
->blklen
+ 1) * rbnode
->word_size
, GFP_KERNEL
);
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 */
663 rbnode
->base_reg
= reg
;
665 snd_soc_rbtree_set_register(rbnode
, pos
, value
);
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
;
676 unsigned int reg_tmp
;
677 unsigned int base_reg
, top_reg
;
682 rbtree_ctx
= codec
->reg_cache
;
683 /* look up the required register in the cached rbnode */
684 rbnode
= rbtree_ctx
->cached_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
);
692 snd_soc_rbtree_set_register(rbnode
, reg_tmp
, value
);
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
);
701 reg_tmp
= reg
- rbnode
->base_reg
;
702 val
= snd_soc_rbtree_get_register(rbnode
, reg_tmp
);
705 snd_soc_rbtree_set_register(rbnode
, reg_tmp
, value
);
706 rbtree_ctx
->cached_rbnode
= rbnode
;
708 /* bail out early, no need to create the rbnode yet */
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)
719 /* decide where in the block to place our register */
720 if (reg_tmp
+ 1 == reg
)
724 ret
= snd_soc_rbtree_insert_to_block(rbnode_tmp
, pos
,
728 rbtree_ctx
->cached_rbnode
= rbnode_tmp
;
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
);
741 rbnode
->base_reg
= reg
;
742 rbnode
->word_size
= codec
->driver
->reg_word_size
;
743 rbnode
->block
= kmalloc(rbnode
->blklen
* rbnode
->word_size
,
745 if (!rbnode
->block
) {
749 snd_soc_rbtree_set_register(rbnode
, 0, value
);
750 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbnode
);
751 rbtree_ctx
->cached_rbnode
= rbnode
;
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
;
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
);
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
);
781 reg_tmp
= reg
- rbnode
->base_reg
;
782 *value
= snd_soc_rbtree_get_register(rbnode
, reg_tmp
);
783 rbtree_ctx
->cached_rbnode
= rbnode
;
785 /* uninitialized registers default to 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
;
803 /* free up the rbtree */
804 next
= rb_first(&rbtree_ctx
->root
);
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
);
813 /* release the resources */
814 kfree(codec
->reg_cache
);
815 codec
->reg_cache
= NULL
;
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
;
828 codec
->reg_cache
= kmalloc(sizeof *rbtree_ctx
, GFP_KERNEL
);
829 if (!codec
->reg_cache
)
832 rbtree_ctx
= codec
->reg_cache
;
833 rbtree_ctx
->root
= RB_ROOT
;
834 rbtree_ctx
->cached_rbnode
= NULL
;
836 if (!codec
->reg_def_copy
)
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
,
845 ret
= snd_soc_rbtree_cache_write(codec
, i
, val
);
853 snd_soc_cache_exit(codec
);
857 #ifdef CONFIG_SND_SOC_CACHE_LZO
858 struct snd_soc_lzo_ctx
{
864 size_t decompressed_size
;
865 unsigned long *sync_bmp
;
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
);
883 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx
*lzo_ctx
)
885 size_t compress_size
;
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
)
892 lzo_ctx
->dst_len
= compress_size
;
896 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx
*lzo_ctx
)
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
)
909 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec
*codec
,
910 struct snd_soc_lzo_ctx
*lzo_ctx
)
914 lzo_ctx
->dst_len
= lzo1x_worst_compress(PAGE_SIZE
);
915 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
917 lzo_ctx
->dst_len
= 0;
921 ret
= snd_soc_lzo_compress(lzo_ctx
);
927 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec
*codec
,
928 struct snd_soc_lzo_ctx
*lzo_ctx
)
932 lzo_ctx
->dst_len
= lzo_ctx
->decompressed_size
;
933 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
935 lzo_ctx
->dst_len
= 0;
939 ret
= snd_soc_lzo_decompress(lzo_ctx
);
945 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec
*codec
,
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
,
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
;
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
);
987 codec
->cache_bypass
= 1;
988 ret
= snd_soc_write(codec
, i
, val
);
989 codec
->cache_bypass
= 0;
992 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
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
;
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
);
1027 kfree(lzo_block
->dst
);
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
);
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
);
1045 kfree(lzo_block
->dst
);
1046 kfree(lzo_block
->src
);
1050 /* set the bit so we know we have to sync this register */
1051 set_bit(reg
, lzo_block
->sync_bmp
);
1053 kfree(lzo_block
->src
);
1056 lzo_block
->dst
= tmp_dst
;
1057 lzo_block
->dst_len
= tmp_dst_len
;
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
;
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
);
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
;
1101 static int snd_soc_lzo_cache_exit(struct snd_soc_codec
*codec
)
1103 struct snd_soc_lzo_ctx
**lzo_blocks
;
1106 lzo_blocks
= codec
->reg_cache
;
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
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
]);
1127 codec
->reg_cache
= NULL
;
1131 static int snd_soc_lzo_cache_init(struct snd_soc_codec
*codec
)
1133 struct snd_soc_lzo_ctx
**lzo_blocks
;
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
;
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.
1149 if (!codec
->reg_def_copy
)
1152 if (!codec
->reg_def_copy
) {
1153 codec
->reg_def_copy
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1154 if (!codec
->reg_def_copy
)
1158 blkcount
= snd_soc_lzo_block_count();
1159 codec
->reg_cache
= kzalloc(blkcount
* sizeof *lzo_blocks
,
1161 if (!codec
->reg_cache
) {
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
1173 bmp_size
= codec_drv
->reg_cache_size
;
1174 sync_bmp
= kmalloc(BITS_TO_LONGS(bmp_size
) * sizeof(long),
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
,
1186 if (!lzo_blocks
[i
]) {
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
]);
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
;
1208 lzo_blocks
[i
]->src_len
= blksize
;
1209 ret
= snd_soc_lzo_compress_cache_block(codec
,
1213 lzo_blocks
[i
]->decompressed_size
=
1214 lzo_blocks
[i
]->src_len
;
1218 kfree(codec
->reg_def_copy
);
1219 codec
->reg_def_copy
= NULL
;
1223 snd_soc_cache_exit(codec
);
1226 kfree(codec
->reg_def_copy
);
1227 codec
->reg_def_copy
= NULL
;
1233 static int snd_soc_flat_cache_sync(struct snd_soc_codec
*codec
)
1237 const struct snd_soc_codec_driver
*codec_drv
;
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
);
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
)
1251 ret
= snd_soc_write(codec
, i
, val
);
1254 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
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
);
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
);
1276 static int snd_soc_flat_cache_exit(struct snd_soc_codec
*codec
)
1278 if (!codec
->reg_cache
)
1280 kfree(codec
->reg_cache
);
1281 codec
->reg_cache
= NULL
;
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
);
1295 codec
->reg_cache
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1296 if (!codec
->reg_cache
)
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
,
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
,
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
1326 .id
= SND_SOC_RBTREE_COMPRESSION
,
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
)
1340 for (i
= 0; i
< ARRAY_SIZE(cache_types
); ++i
)
1341 if (cache_types
[i
].id
== codec
->compress_type
)
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
);
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
);
1364 * NOTE: keep in mind that this function might be called
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
);
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
)
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
);
1398 mutex_unlock(&codec
->cache_rw_mutex
);
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
)
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
);
1423 mutex_unlock(&codec
->cache_rw_mutex
);
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
)
1442 if (!codec
->cache_sync
) {
1446 if (!codec
->cache_ops
|| !codec
->cache_ops
->sync
)
1449 if (codec
->cache_ops
->name
)
1450 name
= codec
->cache_ops
->name
;
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
);
1460 codec
->cache_sync
= 0;
1461 trace_snd_soc_cache_sync(codec
, name
, "end");
1464 EXPORT_SYMBOL_GPL(snd_soc_cache_sync
);
1466 static int snd_soc_get_reg_access_index(struct snd_soc_codec
*codec
,
1469 const struct snd_soc_codec_driver
*codec_drv
;
1470 unsigned int min
, max
, index
;
1472 codec_drv
= codec
->driver
;
1474 max
= codec_drv
->reg_access_size
- 1;
1476 index
= (min
+ max
) / 2;
1477 if (codec_drv
->reg_access_default
[index
].reg
== reg
)
1479 if (codec_drv
->reg_access_default
[index
].reg
< reg
)
1483 } while (min
<= max
);
1487 int snd_soc_default_volatile_register(struct snd_soc_codec
*codec
,
1492 if (reg
>= codec
->driver
->reg_cache_size
)
1494 index
= snd_soc_get_reg_access_index(codec
, reg
);
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
,
1506 if (reg
>= codec
->driver
->reg_cache_size
)
1508 index
= snd_soc_get_reg_access_index(codec
, reg
);
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
,
1520 if (reg
>= codec
->driver
->reg_cache_size
)
1522 index
= snd_soc_get_reg_access_index(codec
, reg
);
1525 return codec
->driver
->reg_access_default
[index
].write
;
1527 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register
);