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 #if defined(CONFIG_SPI_MASTER)
24 static int do_spi_write(void *control_data
, const void *msg
,
27 struct spi_device
*spi
= control_data
;
28 struct spi_transfer t
;
35 memset(&t
, 0, sizeof t
);
40 spi_message_add_tail(&t
, &m
);
47 static int do_hw_write(struct snd_soc_codec
*codec
, unsigned int reg
,
48 unsigned int value
, const void *data
, int len
)
52 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
53 reg
< codec
->driver
->reg_cache_size
&&
54 !codec
->cache_bypass
) {
55 ret
= snd_soc_cache_write(codec
, reg
, value
);
60 if (codec
->cache_only
) {
61 codec
->cache_sync
= 1;
65 ret
= codec
->hw_write(codec
->control_data
, data
, len
);
74 static unsigned int do_hw_read(struct snd_soc_codec
*codec
, unsigned int reg
)
79 if (reg
>= codec
->driver
->reg_cache_size
||
80 snd_soc_codec_volatile_register(codec
, reg
) ||
81 codec
->cache_bypass
) {
82 if (codec
->cache_only
)
85 BUG_ON(!codec
->hw_read
);
86 return codec
->hw_read(codec
, reg
);
89 ret
= snd_soc_cache_read(codec
, reg
, &val
);
95 static unsigned int snd_soc_4_12_read(struct snd_soc_codec
*codec
,
98 return do_hw_read(codec
, reg
);
101 static int snd_soc_4_12_write(struct snd_soc_codec
*codec
, unsigned int reg
,
106 data
[0] = (reg
<< 4) | ((value
>> 8) & 0x000f);
107 data
[1] = value
& 0x00ff;
109 return do_hw_write(codec
, reg
, value
, data
, 2);
112 #if defined(CONFIG_SPI_MASTER)
113 static int snd_soc_4_12_spi_write(void *control_data
, const char *data
,
121 return do_spi_write(control_data
, msg
, len
);
124 #define snd_soc_4_12_spi_write NULL
127 static unsigned int snd_soc_7_9_read(struct snd_soc_codec
*codec
,
130 return do_hw_read(codec
, reg
);
133 static int snd_soc_7_9_write(struct snd_soc_codec
*codec
, unsigned int reg
,
138 data
= cpu_to_be16((reg
<< 9) | (value
& 0x1ff));
140 return do_hw_write(codec
, reg
, value
, &data
, 2);
143 #if defined(CONFIG_SPI_MASTER)
144 static int snd_soc_7_9_spi_write(void *control_data
, const char *data
,
152 return do_spi_write(control_data
, msg
, len
);
155 #define snd_soc_7_9_spi_write NULL
158 static int snd_soc_8_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
165 data
[1] = value
& 0xff;
167 return do_hw_write(codec
, reg
, value
, data
, 2);
170 static unsigned int snd_soc_8_8_read(struct snd_soc_codec
*codec
,
173 return do_hw_read(codec
, reg
);
176 #if defined(CONFIG_SPI_MASTER)
177 static int snd_soc_8_8_spi_write(void *control_data
, const char *data
,
185 return do_spi_write(control_data
, msg
, len
);
188 #define snd_soc_8_8_spi_write NULL
191 static int snd_soc_8_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
195 u16 val
= cpu_to_be16(value
);
198 memcpy(&data
[1], &val
, sizeof(val
));
200 return do_hw_write(codec
, reg
, value
, data
, 3);
203 static unsigned int snd_soc_8_16_read(struct snd_soc_codec
*codec
,
206 return do_hw_read(codec
, reg
);
209 #if defined(CONFIG_SPI_MASTER)
210 static int snd_soc_8_16_spi_write(void *control_data
, const char *data
,
219 return do_spi_write(control_data
, msg
, len
);
222 #define snd_soc_8_16_spi_write NULL
225 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
226 static unsigned int do_i2c_read(struct snd_soc_codec
*codec
,
227 void *reg
, int reglen
,
228 void *data
, int datalen
)
230 struct i2c_msg xfer
[2];
232 struct i2c_client
*client
= codec
->control_data
;
235 xfer
[0].addr
= client
->addr
;
237 xfer
[0].len
= reglen
;
241 xfer
[1].addr
= client
->addr
;
242 xfer
[1].flags
= I2C_M_RD
;
243 xfer
[1].len
= datalen
;
246 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
256 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
257 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec
*codec
,
264 ret
= do_i2c_read(codec
, ®
, 1, &data
, 1);
270 #define snd_soc_8_8_read_i2c NULL
273 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
274 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec
*codec
,
281 ret
= do_i2c_read(codec
, ®
, 1, &data
, 2);
284 return (data
>> 8) | ((data
& 0xff) << 8);
287 #define snd_soc_8_16_read_i2c NULL
290 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
291 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec
*codec
,
298 ret
= do_i2c_read(codec
, ®
, 2, &data
, 1);
304 #define snd_soc_16_8_read_i2c NULL
307 static unsigned int snd_soc_16_8_read(struct snd_soc_codec
*codec
,
310 return do_hw_read(codec
, reg
);
313 static int snd_soc_16_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
318 data
[0] = (reg
>> 8) & 0xff;
319 data
[1] = reg
& 0xff;
323 return do_hw_write(codec
, reg
, value
, data
, 3);
326 #if defined(CONFIG_SPI_MASTER)
327 static int snd_soc_16_8_spi_write(void *control_data
, const char *data
,
336 return do_spi_write(control_data
, msg
, len
);
339 #define snd_soc_16_8_spi_write NULL
342 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
343 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec
*codec
,
346 u16 reg
= cpu_to_be16(r
);
350 ret
= do_i2c_read(codec
, ®
, 2, &data
, 2);
353 return be16_to_cpu(data
);
356 #define snd_soc_16_16_read_i2c NULL
359 static unsigned int snd_soc_16_16_read(struct snd_soc_codec
*codec
,
362 return do_hw_read(codec
, reg
);
365 static int snd_soc_16_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
370 data
[0] = cpu_to_be16(reg
);
371 data
[1] = cpu_to_be16(value
);
373 return do_hw_write(codec
, reg
, value
, data
, sizeof(data
));
376 #if defined(CONFIG_SPI_MASTER)
377 static int snd_soc_16_16_spi_write(void *control_data
, const char *data
,
387 return do_spi_write(control_data
, msg
, len
);
390 #define snd_soc_16_16_spi_write NULL
393 /* Primitive bulk write support for soc-cache. The data pointed to by
394 * `data' needs to already be in the form the hardware expects
395 * including any leading register specific data. Any data written
396 * through this function will not go through the cache as it only
397 * handles writing to volatile or out of bounds registers.
399 static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec
*codec
, unsigned int reg
,
400 const void *data
, size_t len
)
404 /* To ensure that we don't get out of sync with the cache, check
405 * whether the base register is volatile or if we've directly asked
406 * to bypass the cache. Out of bounds registers are considered
409 if (!codec
->cache_bypass
410 && !snd_soc_codec_volatile_register(codec
, reg
)
411 && reg
< codec
->driver
->reg_cache_size
)
414 switch (codec
->control_type
) {
415 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
417 ret
= i2c_master_send(codec
->control_data
, data
, len
);
420 #if defined(CONFIG_SPI_MASTER)
422 ret
= do_spi_write(codec
->control_data
, data
, len
);
440 int (*write
)(struct snd_soc_codec
*codec
, unsigned int, unsigned int);
441 int (*spi_write
)(void *, const char *, int);
442 unsigned int (*read
)(struct snd_soc_codec
*, unsigned int);
443 unsigned int (*i2c_read
)(struct snd_soc_codec
*, unsigned int);
446 .addr_bits
= 4, .data_bits
= 12,
447 .write
= snd_soc_4_12_write
, .read
= snd_soc_4_12_read
,
448 .spi_write
= snd_soc_4_12_spi_write
,
451 .addr_bits
= 7, .data_bits
= 9,
452 .write
= snd_soc_7_9_write
, .read
= snd_soc_7_9_read
,
453 .spi_write
= snd_soc_7_9_spi_write
,
456 .addr_bits
= 8, .data_bits
= 8,
457 .write
= snd_soc_8_8_write
, .read
= snd_soc_8_8_read
,
458 .i2c_read
= snd_soc_8_8_read_i2c
,
459 .spi_write
= snd_soc_8_8_spi_write
,
462 .addr_bits
= 8, .data_bits
= 16,
463 .write
= snd_soc_8_16_write
, .read
= snd_soc_8_16_read
,
464 .i2c_read
= snd_soc_8_16_read_i2c
,
465 .spi_write
= snd_soc_8_16_spi_write
,
468 .addr_bits
= 16, .data_bits
= 8,
469 .write
= snd_soc_16_8_write
, .read
= snd_soc_16_8_read
,
470 .i2c_read
= snd_soc_16_8_read_i2c
,
471 .spi_write
= snd_soc_16_8_spi_write
,
474 .addr_bits
= 16, .data_bits
= 16,
475 .write
= snd_soc_16_16_write
, .read
= snd_soc_16_16_read
,
476 .i2c_read
= snd_soc_16_16_read_i2c
,
477 .spi_write
= snd_soc_16_16_spi_write
,
482 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
484 * @codec: CODEC to configure.
485 * @addr_bits: Number of bits of register address data.
486 * @data_bits: Number of bits of data per register.
487 * @control: Control bus used.
489 * Register formats are frequently shared between many I2C and SPI
490 * devices. In order to promote code reuse the ASoC core provides
491 * some standard implementations of CODEC read and write operations
492 * which can be set up using this function.
494 * The caller is responsible for allocating and initialising the
497 * Note that at present this code cannot be used by CODECs with
498 * volatile registers.
500 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
501 int addr_bits
, int data_bits
,
502 enum snd_soc_control_type control
)
506 for (i
= 0; i
< ARRAY_SIZE(io_types
); i
++)
507 if (io_types
[i
].addr_bits
== addr_bits
&&
508 io_types
[i
].data_bits
== data_bits
)
510 if (i
== ARRAY_SIZE(io_types
)) {
512 "No I/O functions for %d bit address %d bit data\n",
513 addr_bits
, data_bits
);
517 codec
->write
= io_types
[i
].write
;
518 codec
->read
= io_types
[i
].read
;
519 codec
->bulk_write_raw
= snd_soc_hw_bulk_write_raw
;
526 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
527 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
529 if (io_types
[i
].i2c_read
)
530 codec
->hw_read
= io_types
[i
].i2c_read
;
532 codec
->control_data
= container_of(codec
->dev
,
538 if (io_types
[i
].spi_write
)
539 codec
->hw_write
= io_types
[i
].spi_write
;
541 codec
->control_data
= container_of(codec
->dev
,
549 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);
551 static bool snd_soc_set_cache_val(void *base
, unsigned int idx
,
552 unsigned int val
, unsigned int word_size
)
557 if (cache
[idx
] == val
)
564 if (cache
[idx
] == val
)
575 static unsigned int snd_soc_get_cache_val(const void *base
, unsigned int idx
,
576 unsigned int word_size
)
580 const u8
*cache
= base
;
584 const u16
*cache
= base
;
594 struct snd_soc_rbtree_node
{
595 struct rb_node node
; /* the actual rbtree node holding this block */
596 unsigned int base_reg
; /* base register handled by this block */
597 unsigned int word_size
; /* number of bytes needed to represent the register index */
598 void *block
; /* block of adjacent registers */
599 unsigned int blklen
; /* number of registers available in the block */
600 } __attribute__ ((packed
));
602 struct snd_soc_rbtree_ctx
{
604 struct snd_soc_rbtree_node
*cached_rbnode
;
607 static inline void snd_soc_rbtree_get_base_top_reg(
608 struct snd_soc_rbtree_node
*rbnode
,
609 unsigned int *base
, unsigned int *top
)
611 *base
= rbnode
->base_reg
;
612 *top
= rbnode
->base_reg
+ rbnode
->blklen
- 1;
615 static unsigned int snd_soc_rbtree_get_register(
616 struct snd_soc_rbtree_node
*rbnode
, unsigned int idx
)
620 switch (rbnode
->word_size
) {
622 u8
*p
= rbnode
->block
;
627 u16
*p
= rbnode
->block
;
638 static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node
*rbnode
,
639 unsigned int idx
, unsigned int val
)
641 switch (rbnode
->word_size
) {
643 u8
*p
= rbnode
->block
;
648 u16
*p
= rbnode
->block
;
658 static struct snd_soc_rbtree_node
*snd_soc_rbtree_lookup(
659 struct rb_root
*root
, unsigned int reg
)
661 struct rb_node
*node
;
662 struct snd_soc_rbtree_node
*rbnode
;
663 unsigned int base_reg
, top_reg
;
665 node
= root
->rb_node
;
667 rbnode
= container_of(node
, struct snd_soc_rbtree_node
, node
);
668 snd_soc_rbtree_get_base_top_reg(rbnode
, &base_reg
, &top_reg
);
669 if (reg
>= base_reg
&& reg
<= top_reg
)
671 else if (reg
> top_reg
)
672 node
= node
->rb_right
;
673 else if (reg
< base_reg
)
674 node
= node
->rb_left
;
680 static int snd_soc_rbtree_insert(struct rb_root
*root
,
681 struct snd_soc_rbtree_node
*rbnode
)
683 struct rb_node
**new, *parent
;
684 struct snd_soc_rbtree_node
*rbnode_tmp
;
685 unsigned int base_reg_tmp
, top_reg_tmp
;
686 unsigned int base_reg
;
689 new = &root
->rb_node
;
691 rbnode_tmp
= container_of(*new, struct snd_soc_rbtree_node
,
693 /* base and top registers of the current rbnode */
694 snd_soc_rbtree_get_base_top_reg(rbnode_tmp
, &base_reg_tmp
,
696 /* base register of the rbnode to be added */
697 base_reg
= rbnode
->base_reg
;
699 /* if this register has already been inserted, just return */
700 if (base_reg
>= base_reg_tmp
&&
701 base_reg
<= top_reg_tmp
)
703 else if (base_reg
> top_reg_tmp
)
704 new = &((*new)->rb_right
);
705 else if (base_reg
< base_reg_tmp
)
706 new = &((*new)->rb_left
);
709 /* insert the node into the rbtree */
710 rb_link_node(&rbnode
->node
, parent
, new);
711 rb_insert_color(&rbnode
->node
, root
);
716 static int snd_soc_rbtree_cache_sync(struct snd_soc_codec
*codec
)
718 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
719 struct rb_node
*node
;
720 struct snd_soc_rbtree_node
*rbnode
;
726 rbtree_ctx
= codec
->reg_cache
;
727 for (node
= rb_first(&rbtree_ctx
->root
); node
; node
= rb_next(node
)) {
728 rbnode
= rb_entry(node
, struct snd_soc_rbtree_node
, node
);
729 for (i
= 0; i
< rbnode
->blklen
; ++i
) {
730 regtmp
= rbnode
->base_reg
+ i
;
731 WARN_ON(codec
->writable_register
&&
732 codec
->writable_register(codec
, regtmp
));
733 val
= snd_soc_rbtree_get_register(rbnode
, i
);
734 codec
->cache_bypass
= 1;
735 ret
= snd_soc_write(codec
, regtmp
, val
);
736 codec
->cache_bypass
= 0;
739 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
747 static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node
*rbnode
,
748 unsigned int pos
, unsigned int reg
,
753 blk
= krealloc(rbnode
->block
,
754 (rbnode
->blklen
+ 1) * rbnode
->word_size
, GFP_KERNEL
);
758 /* insert the register value in the correct place in the rbnode block */
759 memmove(blk
+ (pos
+ 1) * rbnode
->word_size
,
760 blk
+ pos
* rbnode
->word_size
,
761 (rbnode
->blklen
- pos
) * rbnode
->word_size
);
763 /* update the rbnode block, its size and the base register */
767 rbnode
->base_reg
= reg
;
769 snd_soc_rbtree_set_register(rbnode
, pos
, value
);
773 static int snd_soc_rbtree_cache_write(struct snd_soc_codec
*codec
,
774 unsigned int reg
, unsigned int value
)
776 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
777 struct snd_soc_rbtree_node
*rbnode
, *rbnode_tmp
;
778 struct rb_node
*node
;
780 unsigned int reg_tmp
;
781 unsigned int base_reg
, top_reg
;
786 rbtree_ctx
= codec
->reg_cache
;
787 /* look up the required register in the cached rbnode */
788 rbnode
= rbtree_ctx
->cached_rbnode
;
790 snd_soc_rbtree_get_base_top_reg(rbnode
, &base_reg
, &top_reg
);
791 if (reg
>= base_reg
&& reg
<= top_reg
) {
792 reg_tmp
= reg
- base_reg
;
793 val
= snd_soc_rbtree_get_register(rbnode
, reg_tmp
);
796 snd_soc_rbtree_set_register(rbnode
, reg_tmp
, value
);
800 /* if we can't locate it in the cached rbnode we'll have
801 * to traverse the rbtree looking for it.
803 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
805 reg_tmp
= reg
- rbnode
->base_reg
;
806 val
= snd_soc_rbtree_get_register(rbnode
, reg_tmp
);
809 snd_soc_rbtree_set_register(rbnode
, reg_tmp
, value
);
810 rbtree_ctx
->cached_rbnode
= rbnode
;
812 /* bail out early, no need to create the rbnode yet */
815 /* look for an adjacent register to the one we are about to add */
816 for (node
= rb_first(&rbtree_ctx
->root
); node
;
817 node
= rb_next(node
)) {
818 rbnode_tmp
= rb_entry(node
, struct snd_soc_rbtree_node
, node
);
819 for (i
= 0; i
< rbnode_tmp
->blklen
; ++i
) {
820 reg_tmp
= rbnode_tmp
->base_reg
+ i
;
821 if (abs(reg_tmp
- reg
) != 1)
823 /* decide where in the block to place our register */
824 if (reg_tmp
+ 1 == reg
)
828 ret
= snd_soc_rbtree_insert_to_block(rbnode_tmp
, pos
,
832 rbtree_ctx
->cached_rbnode
= rbnode_tmp
;
836 /* we did not manage to find a place to insert it in an existing
837 * block so create a new rbnode with a single register in its block.
838 * This block will get populated further if any other adjacent
839 * registers get modified in the future.
841 rbnode
= kzalloc(sizeof *rbnode
, GFP_KERNEL
);
845 rbnode
->base_reg
= reg
;
846 rbnode
->word_size
= codec
->driver
->reg_word_size
;
847 rbnode
->block
= kmalloc(rbnode
->blklen
* rbnode
->word_size
,
849 if (!rbnode
->block
) {
853 snd_soc_rbtree_set_register(rbnode
, 0, value
);
854 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbnode
);
855 rbtree_ctx
->cached_rbnode
= rbnode
;
861 static int snd_soc_rbtree_cache_read(struct snd_soc_codec
*codec
,
862 unsigned int reg
, unsigned int *value
)
864 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
865 struct snd_soc_rbtree_node
*rbnode
;
866 unsigned int base_reg
, top_reg
;
867 unsigned int reg_tmp
;
869 rbtree_ctx
= codec
->reg_cache
;
870 /* look up the required register in the cached rbnode */
871 rbnode
= rbtree_ctx
->cached_rbnode
;
873 snd_soc_rbtree_get_base_top_reg(rbnode
, &base_reg
, &top_reg
);
874 if (reg
>= base_reg
&& reg
<= top_reg
) {
875 reg_tmp
= reg
- base_reg
;
876 *value
= snd_soc_rbtree_get_register(rbnode
, reg_tmp
);
880 /* if we can't locate it in the cached rbnode we'll have
881 * to traverse the rbtree looking for it.
883 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
885 reg_tmp
= reg
- rbnode
->base_reg
;
886 *value
= snd_soc_rbtree_get_register(rbnode
, reg_tmp
);
887 rbtree_ctx
->cached_rbnode
= rbnode
;
889 /* uninitialized registers default to 0 */
896 static int snd_soc_rbtree_cache_exit(struct snd_soc_codec
*codec
)
898 struct rb_node
*next
;
899 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
900 struct snd_soc_rbtree_node
*rbtree_node
;
902 /* if we've already been called then just return */
903 rbtree_ctx
= codec
->reg_cache
;
907 /* free up the rbtree */
908 next
= rb_first(&rbtree_ctx
->root
);
910 rbtree_node
= rb_entry(next
, struct snd_soc_rbtree_node
, node
);
911 next
= rb_next(&rbtree_node
->node
);
912 rb_erase(&rbtree_node
->node
, &rbtree_ctx
->root
);
913 kfree(rbtree_node
->block
);
917 /* release the resources */
918 kfree(codec
->reg_cache
);
919 codec
->reg_cache
= NULL
;
924 static int snd_soc_rbtree_cache_init(struct snd_soc_codec
*codec
)
926 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
927 unsigned int word_size
;
932 codec
->reg_cache
= kmalloc(sizeof *rbtree_ctx
, GFP_KERNEL
);
933 if (!codec
->reg_cache
)
936 rbtree_ctx
= codec
->reg_cache
;
937 rbtree_ctx
->root
= RB_ROOT
;
938 rbtree_ctx
->cached_rbnode
= NULL
;
940 if (!codec
->reg_def_copy
)
943 word_size
= codec
->driver
->reg_word_size
;
944 for (i
= 0; i
< codec
->driver
->reg_cache_size
; ++i
) {
945 val
= snd_soc_get_cache_val(codec
->reg_def_copy
, i
,
949 ret
= snd_soc_rbtree_cache_write(codec
, i
, val
);
957 snd_soc_cache_exit(codec
);
961 #ifdef CONFIG_SND_SOC_CACHE_LZO
962 struct snd_soc_lzo_ctx
{
968 size_t decompressed_size
;
969 unsigned long *sync_bmp
;
973 #define LZO_BLOCK_NUM 8
974 static int snd_soc_lzo_block_count(void)
976 return LZO_BLOCK_NUM
;
979 static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx
*lzo_ctx
)
981 lzo_ctx
->wmem
= kmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
987 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx
*lzo_ctx
)
989 size_t compress_size
;
992 ret
= lzo1x_1_compress(lzo_ctx
->src
, lzo_ctx
->src_len
,
993 lzo_ctx
->dst
, &compress_size
, lzo_ctx
->wmem
);
994 if (ret
!= LZO_E_OK
|| compress_size
> lzo_ctx
->dst_len
)
996 lzo_ctx
->dst_len
= compress_size
;
1000 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx
*lzo_ctx
)
1005 dst_len
= lzo_ctx
->dst_len
;
1006 ret
= lzo1x_decompress_safe(lzo_ctx
->src
, lzo_ctx
->src_len
,
1007 lzo_ctx
->dst
, &dst_len
);
1008 if (ret
!= LZO_E_OK
|| dst_len
!= lzo_ctx
->dst_len
)
1013 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec
*codec
,
1014 struct snd_soc_lzo_ctx
*lzo_ctx
)
1018 lzo_ctx
->dst_len
= lzo1x_worst_compress(PAGE_SIZE
);
1019 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
1020 if (!lzo_ctx
->dst
) {
1021 lzo_ctx
->dst_len
= 0;
1025 ret
= snd_soc_lzo_compress(lzo_ctx
);
1031 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec
*codec
,
1032 struct snd_soc_lzo_ctx
*lzo_ctx
)
1036 lzo_ctx
->dst_len
= lzo_ctx
->decompressed_size
;
1037 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
1038 if (!lzo_ctx
->dst
) {
1039 lzo_ctx
->dst_len
= 0;
1043 ret
= snd_soc_lzo_decompress(lzo_ctx
);
1049 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec
*codec
,
1052 const struct snd_soc_codec_driver
*codec_drv
;
1054 codec_drv
= codec
->driver
;
1055 return (reg
* codec_drv
->reg_word_size
) /
1056 DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
1059 static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec
*codec
,
1062 const struct snd_soc_codec_driver
*codec_drv
;
1064 codec_drv
= codec
->driver
;
1065 return reg
% (DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count()) /
1066 codec_drv
->reg_word_size
);
1069 static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec
*codec
)
1071 const struct snd_soc_codec_driver
*codec_drv
;
1073 codec_drv
= codec
->driver
;
1074 return DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
1077 static int snd_soc_lzo_cache_sync(struct snd_soc_codec
*codec
)
1079 struct snd_soc_lzo_ctx
**lzo_blocks
;
1084 lzo_blocks
= codec
->reg_cache
;
1085 for_each_set_bit(i
, lzo_blocks
[0]->sync_bmp
, lzo_blocks
[0]->sync_bmp_nbits
) {
1086 WARN_ON(codec
->writable_register
&&
1087 codec
->writable_register(codec
, i
));
1088 ret
= snd_soc_cache_read(codec
, i
, &val
);
1091 codec
->cache_bypass
= 1;
1092 ret
= snd_soc_write(codec
, i
, val
);
1093 codec
->cache_bypass
= 0;
1096 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
1103 static int snd_soc_lzo_cache_write(struct snd_soc_codec
*codec
,
1104 unsigned int reg
, unsigned int value
)
1106 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
1107 int ret
, blkindex
, blkpos
;
1108 size_t blksize
, tmp_dst_len
;
1111 /* index of the compressed lzo block */
1112 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
1113 /* register index within the decompressed block */
1114 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
1115 /* size of the compressed block */
1116 blksize
= snd_soc_lzo_get_blksize(codec
);
1117 lzo_blocks
= codec
->reg_cache
;
1118 lzo_block
= lzo_blocks
[blkindex
];
1120 /* save the pointer and length of the compressed block */
1121 tmp_dst
= lzo_block
->dst
;
1122 tmp_dst_len
= lzo_block
->dst_len
;
1124 /* prepare the source to be the compressed block */
1125 lzo_block
->src
= lzo_block
->dst
;
1126 lzo_block
->src_len
= lzo_block
->dst_len
;
1128 /* decompress the block */
1129 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
1131 kfree(lzo_block
->dst
);
1135 /* write the new value to the cache */
1136 if (snd_soc_set_cache_val(lzo_block
->dst
, blkpos
, value
,
1137 codec
->driver
->reg_word_size
)) {
1138 kfree(lzo_block
->dst
);
1142 /* prepare the source to be the decompressed block */
1143 lzo_block
->src
= lzo_block
->dst
;
1144 lzo_block
->src_len
= lzo_block
->dst_len
;
1146 /* compress the block */
1147 ret
= snd_soc_lzo_compress_cache_block(codec
, lzo_block
);
1149 kfree(lzo_block
->dst
);
1150 kfree(lzo_block
->src
);
1154 /* set the bit so we know we have to sync this register */
1155 set_bit(reg
, lzo_block
->sync_bmp
);
1157 kfree(lzo_block
->src
);
1160 lzo_block
->dst
= tmp_dst
;
1161 lzo_block
->dst_len
= tmp_dst_len
;
1165 static int snd_soc_lzo_cache_read(struct snd_soc_codec
*codec
,
1166 unsigned int reg
, unsigned int *value
)
1168 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
1169 int ret
, blkindex
, blkpos
;
1170 size_t blksize
, tmp_dst_len
;
1174 /* index of the compressed lzo block */
1175 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
1176 /* register index within the decompressed block */
1177 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
1178 /* size of the compressed block */
1179 blksize
= snd_soc_lzo_get_blksize(codec
);
1180 lzo_blocks
= codec
->reg_cache
;
1181 lzo_block
= lzo_blocks
[blkindex
];
1183 /* save the pointer and length of the compressed block */
1184 tmp_dst
= lzo_block
->dst
;
1185 tmp_dst_len
= lzo_block
->dst_len
;
1187 /* prepare the source to be the compressed block */
1188 lzo_block
->src
= lzo_block
->dst
;
1189 lzo_block
->src_len
= lzo_block
->dst_len
;
1191 /* decompress the block */
1192 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
1194 /* fetch the value from the cache */
1195 *value
= snd_soc_get_cache_val(lzo_block
->dst
, blkpos
,
1196 codec
->driver
->reg_word_size
);
1198 kfree(lzo_block
->dst
);
1199 /* restore the pointer and length of the compressed block */
1200 lzo_block
->dst
= tmp_dst
;
1201 lzo_block
->dst_len
= tmp_dst_len
;
1205 static int snd_soc_lzo_cache_exit(struct snd_soc_codec
*codec
)
1207 struct snd_soc_lzo_ctx
**lzo_blocks
;
1210 lzo_blocks
= codec
->reg_cache
;
1214 blkcount
= snd_soc_lzo_block_count();
1216 * the pointer to the bitmap used for syncing the cache
1217 * is shared amongst all lzo_blocks. Ensure it is freed
1221 kfree(lzo_blocks
[0]->sync_bmp
);
1222 for (i
= 0; i
< blkcount
; ++i
) {
1223 if (lzo_blocks
[i
]) {
1224 kfree(lzo_blocks
[i
]->wmem
);
1225 kfree(lzo_blocks
[i
]->dst
);
1227 /* each lzo_block is a pointer returned by kmalloc or NULL */
1228 kfree(lzo_blocks
[i
]);
1231 codec
->reg_cache
= NULL
;
1235 static int snd_soc_lzo_cache_init(struct snd_soc_codec
*codec
)
1237 struct snd_soc_lzo_ctx
**lzo_blocks
;
1239 const struct snd_soc_codec_driver
*codec_drv
;
1240 int ret
, tofree
, i
, blksize
, blkcount
;
1241 const char *p
, *end
;
1242 unsigned long *sync_bmp
;
1245 codec_drv
= codec
->driver
;
1248 * If we have not been given a default register cache
1249 * then allocate a dummy zero-ed out region, compress it
1250 * and remember to free it afterwards.
1253 if (!codec
->reg_def_copy
)
1256 if (!codec
->reg_def_copy
) {
1257 codec
->reg_def_copy
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1258 if (!codec
->reg_def_copy
)
1262 blkcount
= snd_soc_lzo_block_count();
1263 codec
->reg_cache
= kzalloc(blkcount
* sizeof *lzo_blocks
,
1265 if (!codec
->reg_cache
) {
1269 lzo_blocks
= codec
->reg_cache
;
1272 * allocate a bitmap to be used when syncing the cache with
1273 * the hardware. Each time a register is modified, the corresponding
1274 * bit is set in the bitmap, so we know that we have to sync
1277 bmp_size
= codec_drv
->reg_cache_size
;
1278 sync_bmp
= kmalloc(BITS_TO_LONGS(bmp_size
) * sizeof(long),
1284 bitmap_zero(sync_bmp
, bmp_size
);
1286 /* allocate the lzo blocks and initialize them */
1287 for (i
= 0; i
< blkcount
; ++i
) {
1288 lzo_blocks
[i
] = kzalloc(sizeof **lzo_blocks
,
1290 if (!lzo_blocks
[i
]) {
1295 lzo_blocks
[i
]->sync_bmp
= sync_bmp
;
1296 lzo_blocks
[i
]->sync_bmp_nbits
= bmp_size
;
1297 /* alloc the working space for the compressed block */
1298 ret
= snd_soc_lzo_prepare(lzo_blocks
[i
]);
1303 blksize
= snd_soc_lzo_get_blksize(codec
);
1304 p
= codec
->reg_def_copy
;
1305 end
= codec
->reg_def_copy
+ codec
->reg_size
;
1306 /* compress the register map and fill the lzo blocks */
1307 for (i
= 0; i
< blkcount
; ++i
, p
+= blksize
) {
1308 lzo_blocks
[i
]->src
= p
;
1309 if (p
+ blksize
> end
)
1310 lzo_blocks
[i
]->src_len
= end
- p
;
1312 lzo_blocks
[i
]->src_len
= blksize
;
1313 ret
= snd_soc_lzo_compress_cache_block(codec
,
1317 lzo_blocks
[i
]->decompressed_size
=
1318 lzo_blocks
[i
]->src_len
;
1322 kfree(codec
->reg_def_copy
);
1323 codec
->reg_def_copy
= NULL
;
1327 snd_soc_cache_exit(codec
);
1330 kfree(codec
->reg_def_copy
);
1331 codec
->reg_def_copy
= NULL
;
1337 static int snd_soc_flat_cache_sync(struct snd_soc_codec
*codec
)
1341 const struct snd_soc_codec_driver
*codec_drv
;
1344 codec_drv
= codec
->driver
;
1345 for (i
= 0; i
< codec_drv
->reg_cache_size
; ++i
) {
1346 WARN_ON(codec
->writable_register
&&
1347 codec
->writable_register(codec
, i
));
1348 ret
= snd_soc_cache_read(codec
, i
, &val
);
1351 if (codec
->reg_def_copy
)
1352 if (snd_soc_get_cache_val(codec
->reg_def_copy
,
1353 i
, codec_drv
->reg_word_size
) == val
)
1355 ret
= snd_soc_write(codec
, i
, val
);
1358 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
1364 static int snd_soc_flat_cache_write(struct snd_soc_codec
*codec
,
1365 unsigned int reg
, unsigned int value
)
1367 snd_soc_set_cache_val(codec
->reg_cache
, reg
, value
,
1368 codec
->driver
->reg_word_size
);
1372 static int snd_soc_flat_cache_read(struct snd_soc_codec
*codec
,
1373 unsigned int reg
, unsigned int *value
)
1375 *value
= snd_soc_get_cache_val(codec
->reg_cache
, reg
,
1376 codec
->driver
->reg_word_size
);
1380 static int snd_soc_flat_cache_exit(struct snd_soc_codec
*codec
)
1382 if (!codec
->reg_cache
)
1384 kfree(codec
->reg_cache
);
1385 codec
->reg_cache
= NULL
;
1389 static int snd_soc_flat_cache_init(struct snd_soc_codec
*codec
)
1391 const struct snd_soc_codec_driver
*codec_drv
;
1393 codec_drv
= codec
->driver
;
1395 if (codec
->reg_def_copy
)
1396 codec
->reg_cache
= kmemdup(codec
->reg_def_copy
,
1397 codec
->reg_size
, GFP_KERNEL
);
1399 codec
->reg_cache
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1400 if (!codec
->reg_cache
)
1406 /* an array of all supported compression types */
1407 static const struct snd_soc_cache_ops cache_types
[] = {
1408 /* Flat *must* be the first entry for fallback */
1410 .id
= SND_SOC_FLAT_COMPRESSION
,
1412 .init
= snd_soc_flat_cache_init
,
1413 .exit
= snd_soc_flat_cache_exit
,
1414 .read
= snd_soc_flat_cache_read
,
1415 .write
= snd_soc_flat_cache_write
,
1416 .sync
= snd_soc_flat_cache_sync
1418 #ifdef CONFIG_SND_SOC_CACHE_LZO
1420 .id
= SND_SOC_LZO_COMPRESSION
,
1422 .init
= snd_soc_lzo_cache_init
,
1423 .exit
= snd_soc_lzo_cache_exit
,
1424 .read
= snd_soc_lzo_cache_read
,
1425 .write
= snd_soc_lzo_cache_write
,
1426 .sync
= snd_soc_lzo_cache_sync
1430 .id
= SND_SOC_RBTREE_COMPRESSION
,
1432 .init
= snd_soc_rbtree_cache_init
,
1433 .exit
= snd_soc_rbtree_cache_exit
,
1434 .read
= snd_soc_rbtree_cache_read
,
1435 .write
= snd_soc_rbtree_cache_write
,
1436 .sync
= snd_soc_rbtree_cache_sync
1440 int snd_soc_cache_init(struct snd_soc_codec
*codec
)
1444 for (i
= 0; i
< ARRAY_SIZE(cache_types
); ++i
)
1445 if (cache_types
[i
].id
== codec
->compress_type
)
1448 /* Fall back to flat compression */
1449 if (i
== ARRAY_SIZE(cache_types
)) {
1450 dev_warn(codec
->dev
, "Could not match compress type: %d\n",
1451 codec
->compress_type
);
1455 mutex_init(&codec
->cache_rw_mutex
);
1456 codec
->cache_ops
= &cache_types
[i
];
1458 if (codec
->cache_ops
->init
) {
1459 if (codec
->cache_ops
->name
)
1460 dev_dbg(codec
->dev
, "Initializing %s cache for %s codec\n",
1461 codec
->cache_ops
->name
, codec
->name
);
1462 return codec
->cache_ops
->init(codec
);
1468 * NOTE: keep in mind that this function might be called
1471 int snd_soc_cache_exit(struct snd_soc_codec
*codec
)
1473 if (codec
->cache_ops
&& codec
->cache_ops
->exit
) {
1474 if (codec
->cache_ops
->name
)
1475 dev_dbg(codec
->dev
, "Destroying %s cache for %s codec\n",
1476 codec
->cache_ops
->name
, codec
->name
);
1477 return codec
->cache_ops
->exit(codec
);
1483 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1485 * @codec: CODEC to configure.
1486 * @reg: The register index.
1487 * @value: The value to be returned.
1489 int snd_soc_cache_read(struct snd_soc_codec
*codec
,
1490 unsigned int reg
, unsigned int *value
)
1494 mutex_lock(&codec
->cache_rw_mutex
);
1496 if (value
&& codec
->cache_ops
&& codec
->cache_ops
->read
) {
1497 ret
= codec
->cache_ops
->read(codec
, reg
, value
);
1498 mutex_unlock(&codec
->cache_rw_mutex
);
1502 mutex_unlock(&codec
->cache_rw_mutex
);
1505 EXPORT_SYMBOL_GPL(snd_soc_cache_read
);
1508 * snd_soc_cache_write: Set the value of a given register in the cache.
1510 * @codec: CODEC to configure.
1511 * @reg: The register index.
1512 * @value: The new register value.
1514 int snd_soc_cache_write(struct snd_soc_codec
*codec
,
1515 unsigned int reg
, unsigned int value
)
1519 mutex_lock(&codec
->cache_rw_mutex
);
1521 if (codec
->cache_ops
&& codec
->cache_ops
->write
) {
1522 ret
= codec
->cache_ops
->write(codec
, reg
, value
);
1523 mutex_unlock(&codec
->cache_rw_mutex
);
1527 mutex_unlock(&codec
->cache_rw_mutex
);
1530 EXPORT_SYMBOL_GPL(snd_soc_cache_write
);
1533 * snd_soc_cache_sync: Sync the register cache with the hardware.
1535 * @codec: CODEC to configure.
1537 * Any registers that should not be synced should be marked as
1538 * volatile. In general drivers can choose not to use the provided
1539 * syncing functionality if they so require.
1541 int snd_soc_cache_sync(struct snd_soc_codec
*codec
)
1546 if (!codec
->cache_sync
) {
1550 if (!codec
->cache_ops
|| !codec
->cache_ops
->sync
)
1553 if (codec
->cache_ops
->name
)
1554 name
= codec
->cache_ops
->name
;
1558 if (codec
->cache_ops
->name
)
1559 dev_dbg(codec
->dev
, "Syncing %s cache for %s codec\n",
1560 codec
->cache_ops
->name
, codec
->name
);
1561 trace_snd_soc_cache_sync(codec
, name
, "start");
1562 ret
= codec
->cache_ops
->sync(codec
);
1564 codec
->cache_sync
= 0;
1565 trace_snd_soc_cache_sync(codec
, name
, "end");
1568 EXPORT_SYMBOL_GPL(snd_soc_cache_sync
);
1570 static int snd_soc_get_reg_access_index(struct snd_soc_codec
*codec
,
1573 const struct snd_soc_codec_driver
*codec_drv
;
1574 unsigned int min
, max
, index
;
1576 codec_drv
= codec
->driver
;
1578 max
= codec_drv
->reg_access_size
- 1;
1580 index
= (min
+ max
) / 2;
1581 if (codec_drv
->reg_access_default
[index
].reg
== reg
)
1583 if (codec_drv
->reg_access_default
[index
].reg
< reg
)
1587 } while (min
<= max
);
1591 int snd_soc_default_volatile_register(struct snd_soc_codec
*codec
,
1596 if (reg
>= codec
->driver
->reg_cache_size
)
1598 index
= snd_soc_get_reg_access_index(codec
, reg
);
1601 return codec
->driver
->reg_access_default
[index
].vol
;
1603 EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register
);
1605 int snd_soc_default_readable_register(struct snd_soc_codec
*codec
,
1610 if (reg
>= codec
->driver
->reg_cache_size
)
1612 index
= snd_soc_get_reg_access_index(codec
, reg
);
1615 return codec
->driver
->reg_access_default
[index
].read
;
1617 EXPORT_SYMBOL_GPL(snd_soc_default_readable_register
);
1619 int snd_soc_default_writable_register(struct snd_soc_codec
*codec
,
1624 if (reg
>= codec
->driver
->reg_cache_size
)
1626 index
= snd_soc_get_reg_access_index(codec
, reg
);
1629 return codec
->driver
->reg_access_default
[index
].write
;
1631 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register
);