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
[0] = (reg
<< 1) | ((value
>> 8) & 0x0001);
113 data
[1] = value
& 0x00ff;
115 return do_hw_write(codec
, reg
, value
, data
, 2);
118 static int snd_soc_8_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
125 data
[1] = value
& 0xff;
127 return do_hw_write(codec
, reg
, value
, data
, 2);
130 static unsigned int snd_soc_8_8_read(struct snd_soc_codec
*codec
,
133 return do_hw_read(codec
, reg
);
136 static int snd_soc_8_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
142 data
[1] = (value
>> 8) & 0xff;
143 data
[2] = value
& 0xff;
145 return do_hw_write(codec
, reg
, value
, data
, 3);
148 static unsigned int snd_soc_8_16_read(struct snd_soc_codec
*codec
,
151 return do_hw_read(codec
, reg
);
154 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
155 static unsigned int do_i2c_read(struct snd_soc_codec
*codec
,
156 void *reg
, int reglen
,
157 void *data
, int datalen
)
159 struct i2c_msg xfer
[2];
161 struct i2c_client
*client
= codec
->control_data
;
164 xfer
[0].addr
= client
->addr
;
166 xfer
[0].len
= reglen
;
170 xfer
[1].addr
= client
->addr
;
171 xfer
[1].flags
= I2C_M_RD
;
172 xfer
[1].len
= datalen
;
175 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
185 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
186 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec
*codec
,
193 ret
= do_i2c_read(codec
, ®
, 1, &data
, 1);
199 #define snd_soc_8_8_read_i2c NULL
202 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
203 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec
*codec
,
210 ret
= do_i2c_read(codec
, ®
, 1, &data
, 2);
213 return (data
>> 8) | ((data
& 0xff) << 8);
216 #define snd_soc_8_16_read_i2c NULL
219 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
220 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec
*codec
,
227 ret
= do_i2c_read(codec
, ®
, 2, &data
, 1);
233 #define snd_soc_16_8_read_i2c NULL
236 static unsigned int snd_soc_16_8_read(struct snd_soc_codec
*codec
,
239 return do_hw_read(codec
, reg
);
242 static int snd_soc_16_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
247 data
[0] = (reg
>> 8) & 0xff;
248 data
[1] = reg
& 0xff;
251 return do_hw_write(codec
, reg
, value
, data
, 3);
254 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
255 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec
*codec
,
258 u16 reg
= cpu_to_be16(r
);
262 ret
= do_i2c_read(codec
, ®
, 2, &data
, 2);
265 return be16_to_cpu(data
);
268 #define snd_soc_16_16_read_i2c NULL
271 static unsigned int snd_soc_16_16_read(struct snd_soc_codec
*codec
,
274 return do_hw_read(codec
, reg
);
277 static int snd_soc_16_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
282 data
[0] = (reg
>> 8) & 0xff;
283 data
[1] = reg
& 0xff;
284 data
[2] = (value
>> 8) & 0xff;
285 data
[3] = value
& 0xff;
287 return do_hw_write(codec
, reg
, value
, data
, 4);
290 /* Primitive bulk write support for soc-cache. The data pointed to by
291 * `data' needs to already be in the form the hardware expects
292 * including any leading register specific data. Any data written
293 * through this function will not go through the cache as it only
294 * handles writing to volatile or out of bounds registers.
296 static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec
*codec
, unsigned int reg
,
297 const void *data
, size_t len
)
301 /* To ensure that we don't get out of sync with the cache, check
302 * whether the base register is volatile or if we've directly asked
303 * to bypass the cache. Out of bounds registers are considered
306 if (!codec
->cache_bypass
307 && !snd_soc_codec_volatile_register(codec
, reg
)
308 && reg
< codec
->driver
->reg_cache_size
)
311 switch (codec
->control_type
) {
312 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
314 ret
= i2c_master_send(codec
->control_data
, data
, len
);
317 #if defined(CONFIG_SPI_MASTER)
319 ret
= spi_write(codec
->control_data
, data
, len
);
337 int (*write
)(struct snd_soc_codec
*codec
, unsigned int, unsigned int);
338 unsigned int (*read
)(struct snd_soc_codec
*, unsigned int);
339 unsigned int (*i2c_read
)(struct snd_soc_codec
*, unsigned int);
342 .addr_bits
= 4, .data_bits
= 12,
343 .write
= snd_soc_4_12_write
, .read
= snd_soc_4_12_read
,
346 .addr_bits
= 7, .data_bits
= 9,
347 .write
= snd_soc_7_9_write
, .read
= snd_soc_7_9_read
,
350 .addr_bits
= 8, .data_bits
= 8,
351 .write
= snd_soc_8_8_write
, .read
= snd_soc_8_8_read
,
352 .i2c_read
= snd_soc_8_8_read_i2c
,
355 .addr_bits
= 8, .data_bits
= 16,
356 .write
= snd_soc_8_16_write
, .read
= snd_soc_8_16_read
,
357 .i2c_read
= snd_soc_8_16_read_i2c
,
360 .addr_bits
= 16, .data_bits
= 8,
361 .write
= snd_soc_16_8_write
, .read
= snd_soc_16_8_read
,
362 .i2c_read
= snd_soc_16_8_read_i2c
,
365 .addr_bits
= 16, .data_bits
= 16,
366 .write
= snd_soc_16_16_write
, .read
= snd_soc_16_16_read
,
367 .i2c_read
= snd_soc_16_16_read_i2c
,
372 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
374 * @codec: CODEC to configure.
375 * @addr_bits: Number of bits of register address data.
376 * @data_bits: Number of bits of data per register.
377 * @control: Control bus used.
379 * Register formats are frequently shared between many I2C and SPI
380 * devices. In order to promote code reuse the ASoC core provides
381 * some standard implementations of CODEC read and write operations
382 * which can be set up using this function.
384 * The caller is responsible for allocating and initialising the
387 * Note that at present this code cannot be used by CODECs with
388 * volatile registers.
390 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
391 int addr_bits
, int data_bits
,
392 enum snd_soc_control_type control
)
396 for (i
= 0; i
< ARRAY_SIZE(io_types
); i
++)
397 if (io_types
[i
].addr_bits
== addr_bits
&&
398 io_types
[i
].data_bits
== data_bits
)
400 if (i
== ARRAY_SIZE(io_types
)) {
402 "No I/O functions for %d bit address %d bit data\n",
403 addr_bits
, data_bits
);
407 codec
->write
= io_types
[i
].write
;
408 codec
->read
= io_types
[i
].read
;
409 codec
->bulk_write_raw
= snd_soc_hw_bulk_write_raw
;
416 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
417 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
419 if (io_types
[i
].i2c_read
)
420 codec
->hw_read
= io_types
[i
].i2c_read
;
422 codec
->control_data
= container_of(codec
->dev
,
428 #ifdef CONFIG_SPI_MASTER
429 codec
->hw_write
= do_spi_write
;
432 codec
->control_data
= container_of(codec
->dev
,
440 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);
442 static bool snd_soc_set_cache_val(void *base
, unsigned int idx
,
443 unsigned int val
, unsigned int word_size
)
448 if (cache
[idx
] == val
)
455 if (cache
[idx
] == val
)
466 static unsigned int snd_soc_get_cache_val(const void *base
, unsigned int idx
,
467 unsigned int word_size
)
471 const u8
*cache
= base
;
475 const u16
*cache
= base
;
485 struct snd_soc_rbtree_node
{
490 } __attribute__ ((packed
));
492 struct snd_soc_rbtree_ctx
{
496 static struct snd_soc_rbtree_node
*snd_soc_rbtree_lookup(
497 struct rb_root
*root
, unsigned int reg
)
499 struct rb_node
*node
;
500 struct snd_soc_rbtree_node
*rbnode
;
502 node
= root
->rb_node
;
504 rbnode
= container_of(node
, struct snd_soc_rbtree_node
, node
);
505 if (rbnode
->reg
< reg
)
506 node
= node
->rb_left
;
507 else if (rbnode
->reg
> reg
)
508 node
= node
->rb_right
;
516 static int snd_soc_rbtree_insert(struct rb_root
*root
,
517 struct snd_soc_rbtree_node
*rbnode
)
519 struct rb_node
**new, *parent
;
520 struct snd_soc_rbtree_node
*rbnode_tmp
;
523 new = &root
->rb_node
;
525 rbnode_tmp
= container_of(*new, struct snd_soc_rbtree_node
,
528 if (rbnode_tmp
->reg
< rbnode
->reg
)
529 new = &((*new)->rb_left
);
530 else if (rbnode_tmp
->reg
> rbnode
->reg
)
531 new = &((*new)->rb_right
);
536 /* insert the node into the rbtree */
537 rb_link_node(&rbnode
->node
, parent
, new);
538 rb_insert_color(&rbnode
->node
, root
);
543 static int snd_soc_rbtree_cache_sync(struct snd_soc_codec
*codec
)
545 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
546 struct rb_node
*node
;
547 struct snd_soc_rbtree_node
*rbnode
;
551 rbtree_ctx
= codec
->reg_cache
;
552 for (node
= rb_first(&rbtree_ctx
->root
); node
; node
= rb_next(node
)) {
553 rbnode
= rb_entry(node
, struct snd_soc_rbtree_node
, node
);
554 if (rbnode
->value
== rbnode
->defval
)
556 WARN_ON(codec
->writable_register
&&
557 codec
->writable_register(codec
, rbnode
->reg
));
558 ret
= snd_soc_cache_read(codec
, rbnode
->reg
, &val
);
561 codec
->cache_bypass
= 1;
562 ret
= snd_soc_write(codec
, rbnode
->reg
, val
);
563 codec
->cache_bypass
= 0;
566 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
573 static int snd_soc_rbtree_cache_write(struct snd_soc_codec
*codec
,
574 unsigned int reg
, unsigned int value
)
576 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
577 struct snd_soc_rbtree_node
*rbnode
;
579 rbtree_ctx
= codec
->reg_cache
;
580 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
582 if (rbnode
->value
== value
)
584 rbnode
->value
= value
;
586 /* bail out early, no need to create the rbnode yet */
590 * for uninitialized registers whose value is changed
591 * from the default zero, create an rbnode and insert
594 rbnode
= kzalloc(sizeof *rbnode
, GFP_KERNEL
);
598 rbnode
->value
= value
;
599 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbnode
);
605 static int snd_soc_rbtree_cache_read(struct snd_soc_codec
*codec
,
606 unsigned int reg
, unsigned int *value
)
608 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
609 struct snd_soc_rbtree_node
*rbnode
;
611 rbtree_ctx
= codec
->reg_cache
;
612 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
614 *value
= rbnode
->value
;
616 /* uninitialized registers default to 0 */
623 static int snd_soc_rbtree_cache_exit(struct snd_soc_codec
*codec
)
625 struct rb_node
*next
;
626 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
627 struct snd_soc_rbtree_node
*rbtree_node
;
629 /* if we've already been called then just return */
630 rbtree_ctx
= codec
->reg_cache
;
634 /* free up the rbtree */
635 next
= rb_first(&rbtree_ctx
->root
);
637 rbtree_node
= rb_entry(next
, struct snd_soc_rbtree_node
, node
);
638 next
= rb_next(&rbtree_node
->node
);
639 rb_erase(&rbtree_node
->node
, &rbtree_ctx
->root
);
643 /* release the resources */
644 kfree(codec
->reg_cache
);
645 codec
->reg_cache
= NULL
;
650 static int snd_soc_rbtree_cache_init(struct snd_soc_codec
*codec
)
652 struct snd_soc_rbtree_node
*rbtree_node
;
653 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
655 unsigned int word_size
;
659 codec
->reg_cache
= kmalloc(sizeof *rbtree_ctx
, GFP_KERNEL
);
660 if (!codec
->reg_cache
)
663 rbtree_ctx
= codec
->reg_cache
;
664 rbtree_ctx
->root
= RB_ROOT
;
666 if (!codec
->reg_def_copy
)
670 * populate the rbtree with the initialized registers. All other
671 * registers will be inserted when they are first modified.
673 word_size
= codec
->driver
->reg_word_size
;
674 for (i
= 0; i
< codec
->driver
->reg_cache_size
; ++i
) {
675 val
= snd_soc_get_cache_val(codec
->reg_def_copy
, i
, word_size
);
678 rbtree_node
= kzalloc(sizeof *rbtree_node
, GFP_KERNEL
);
681 snd_soc_cache_exit(codec
);
684 rbtree_node
->reg
= i
;
685 rbtree_node
->value
= val
;
686 rbtree_node
->defval
= val
;
687 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbtree_node
);
693 #ifdef CONFIG_SND_SOC_CACHE_LZO
694 struct snd_soc_lzo_ctx
{
700 size_t decompressed_size
;
701 unsigned long *sync_bmp
;
705 #define LZO_BLOCK_NUM 8
706 static int snd_soc_lzo_block_count(void)
708 return LZO_BLOCK_NUM
;
711 static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx
*lzo_ctx
)
713 lzo_ctx
->wmem
= kmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
719 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx
*lzo_ctx
)
721 size_t compress_size
;
724 ret
= lzo1x_1_compress(lzo_ctx
->src
, lzo_ctx
->src_len
,
725 lzo_ctx
->dst
, &compress_size
, lzo_ctx
->wmem
);
726 if (ret
!= LZO_E_OK
|| compress_size
> lzo_ctx
->dst_len
)
728 lzo_ctx
->dst_len
= compress_size
;
732 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx
*lzo_ctx
)
737 dst_len
= lzo_ctx
->dst_len
;
738 ret
= lzo1x_decompress_safe(lzo_ctx
->src
, lzo_ctx
->src_len
,
739 lzo_ctx
->dst
, &dst_len
);
740 if (ret
!= LZO_E_OK
|| dst_len
!= lzo_ctx
->dst_len
)
745 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec
*codec
,
746 struct snd_soc_lzo_ctx
*lzo_ctx
)
750 lzo_ctx
->dst_len
= lzo1x_worst_compress(PAGE_SIZE
);
751 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
753 lzo_ctx
->dst_len
= 0;
757 ret
= snd_soc_lzo_compress(lzo_ctx
);
763 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec
*codec
,
764 struct snd_soc_lzo_ctx
*lzo_ctx
)
768 lzo_ctx
->dst_len
= lzo_ctx
->decompressed_size
;
769 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
771 lzo_ctx
->dst_len
= 0;
775 ret
= snd_soc_lzo_decompress(lzo_ctx
);
781 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec
*codec
,
784 const struct snd_soc_codec_driver
*codec_drv
;
786 codec_drv
= codec
->driver
;
787 return (reg
* codec_drv
->reg_word_size
) /
788 DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
791 static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec
*codec
,
794 const struct snd_soc_codec_driver
*codec_drv
;
796 codec_drv
= codec
->driver
;
797 return reg
% (DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count()) /
798 codec_drv
->reg_word_size
);
801 static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec
*codec
)
803 const struct snd_soc_codec_driver
*codec_drv
;
805 codec_drv
= codec
->driver
;
806 return DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
809 static int snd_soc_lzo_cache_sync(struct snd_soc_codec
*codec
)
811 struct snd_soc_lzo_ctx
**lzo_blocks
;
816 lzo_blocks
= codec
->reg_cache
;
817 for_each_set_bit(i
, lzo_blocks
[0]->sync_bmp
, lzo_blocks
[0]->sync_bmp_nbits
) {
818 WARN_ON(codec
->writable_register
&&
819 codec
->writable_register(codec
, i
));
820 ret
= snd_soc_cache_read(codec
, i
, &val
);
823 codec
->cache_bypass
= 1;
824 ret
= snd_soc_write(codec
, i
, val
);
825 codec
->cache_bypass
= 0;
828 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
835 static int snd_soc_lzo_cache_write(struct snd_soc_codec
*codec
,
836 unsigned int reg
, unsigned int value
)
838 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
839 int ret
, blkindex
, blkpos
;
840 size_t blksize
, tmp_dst_len
;
843 /* index of the compressed lzo block */
844 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
845 /* register index within the decompressed block */
846 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
847 /* size of the compressed block */
848 blksize
= snd_soc_lzo_get_blksize(codec
);
849 lzo_blocks
= codec
->reg_cache
;
850 lzo_block
= lzo_blocks
[blkindex
];
852 /* save the pointer and length of the compressed block */
853 tmp_dst
= lzo_block
->dst
;
854 tmp_dst_len
= lzo_block
->dst_len
;
856 /* prepare the source to be the compressed block */
857 lzo_block
->src
= lzo_block
->dst
;
858 lzo_block
->src_len
= lzo_block
->dst_len
;
860 /* decompress the block */
861 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
863 kfree(lzo_block
->dst
);
867 /* write the new value to the cache */
868 if (snd_soc_set_cache_val(lzo_block
->dst
, blkpos
, value
,
869 codec
->driver
->reg_word_size
)) {
870 kfree(lzo_block
->dst
);
874 /* prepare the source to be the decompressed block */
875 lzo_block
->src
= lzo_block
->dst
;
876 lzo_block
->src_len
= lzo_block
->dst_len
;
878 /* compress the block */
879 ret
= snd_soc_lzo_compress_cache_block(codec
, lzo_block
);
881 kfree(lzo_block
->dst
);
882 kfree(lzo_block
->src
);
886 /* set the bit so we know we have to sync this register */
887 set_bit(reg
, lzo_block
->sync_bmp
);
889 kfree(lzo_block
->src
);
892 lzo_block
->dst
= tmp_dst
;
893 lzo_block
->dst_len
= tmp_dst_len
;
897 static int snd_soc_lzo_cache_read(struct snd_soc_codec
*codec
,
898 unsigned int reg
, unsigned int *value
)
900 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
901 int ret
, blkindex
, blkpos
;
902 size_t blksize
, tmp_dst_len
;
906 /* index of the compressed lzo block */
907 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
908 /* register index within the decompressed block */
909 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
910 /* size of the compressed block */
911 blksize
= snd_soc_lzo_get_blksize(codec
);
912 lzo_blocks
= codec
->reg_cache
;
913 lzo_block
= lzo_blocks
[blkindex
];
915 /* save the pointer and length of the compressed block */
916 tmp_dst
= lzo_block
->dst
;
917 tmp_dst_len
= lzo_block
->dst_len
;
919 /* prepare the source to be the compressed block */
920 lzo_block
->src
= lzo_block
->dst
;
921 lzo_block
->src_len
= lzo_block
->dst_len
;
923 /* decompress the block */
924 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
926 /* fetch the value from the cache */
927 *value
= snd_soc_get_cache_val(lzo_block
->dst
, blkpos
,
928 codec
->driver
->reg_word_size
);
930 kfree(lzo_block
->dst
);
931 /* restore the pointer and length of the compressed block */
932 lzo_block
->dst
= tmp_dst
;
933 lzo_block
->dst_len
= tmp_dst_len
;
937 static int snd_soc_lzo_cache_exit(struct snd_soc_codec
*codec
)
939 struct snd_soc_lzo_ctx
**lzo_blocks
;
942 lzo_blocks
= codec
->reg_cache
;
946 blkcount
= snd_soc_lzo_block_count();
948 * the pointer to the bitmap used for syncing the cache
949 * is shared amongst all lzo_blocks. Ensure it is freed
953 kfree(lzo_blocks
[0]->sync_bmp
);
954 for (i
= 0; i
< blkcount
; ++i
) {
956 kfree(lzo_blocks
[i
]->wmem
);
957 kfree(lzo_blocks
[i
]->dst
);
959 /* each lzo_block is a pointer returned by kmalloc or NULL */
960 kfree(lzo_blocks
[i
]);
963 codec
->reg_cache
= NULL
;
967 static int snd_soc_lzo_cache_init(struct snd_soc_codec
*codec
)
969 struct snd_soc_lzo_ctx
**lzo_blocks
;
971 const struct snd_soc_codec_driver
*codec_drv
;
972 int ret
, tofree
, i
, blksize
, blkcount
;
974 unsigned long *sync_bmp
;
977 codec_drv
= codec
->driver
;
980 * If we have not been given a default register cache
981 * then allocate a dummy zero-ed out region, compress it
982 * and remember to free it afterwards.
985 if (!codec
->reg_def_copy
)
988 if (!codec
->reg_def_copy
) {
989 codec
->reg_def_copy
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
990 if (!codec
->reg_def_copy
)
994 blkcount
= snd_soc_lzo_block_count();
995 codec
->reg_cache
= kzalloc(blkcount
* sizeof *lzo_blocks
,
997 if (!codec
->reg_cache
) {
1001 lzo_blocks
= codec
->reg_cache
;
1004 * allocate a bitmap to be used when syncing the cache with
1005 * the hardware. Each time a register is modified, the corresponding
1006 * bit is set in the bitmap, so we know that we have to sync
1009 bmp_size
= codec_drv
->reg_cache_size
;
1010 sync_bmp
= kmalloc(BITS_TO_LONGS(bmp_size
) * sizeof(long),
1016 bitmap_zero(sync_bmp
, bmp_size
);
1018 /* allocate the lzo blocks and initialize them */
1019 for (i
= 0; i
< blkcount
; ++i
) {
1020 lzo_blocks
[i
] = kzalloc(sizeof **lzo_blocks
,
1022 if (!lzo_blocks
[i
]) {
1027 lzo_blocks
[i
]->sync_bmp
= sync_bmp
;
1028 lzo_blocks
[i
]->sync_bmp_nbits
= bmp_size
;
1029 /* alloc the working space for the compressed block */
1030 ret
= snd_soc_lzo_prepare(lzo_blocks
[i
]);
1035 blksize
= snd_soc_lzo_get_blksize(codec
);
1036 p
= codec
->reg_def_copy
;
1037 end
= codec
->reg_def_copy
+ codec
->reg_size
;
1038 /* compress the register map and fill the lzo blocks */
1039 for (i
= 0; i
< blkcount
; ++i
, p
+= blksize
) {
1040 lzo_blocks
[i
]->src
= p
;
1041 if (p
+ blksize
> end
)
1042 lzo_blocks
[i
]->src_len
= end
- p
;
1044 lzo_blocks
[i
]->src_len
= blksize
;
1045 ret
= snd_soc_lzo_compress_cache_block(codec
,
1049 lzo_blocks
[i
]->decompressed_size
=
1050 lzo_blocks
[i
]->src_len
;
1054 kfree(codec
->reg_def_copy
);
1055 codec
->reg_def_copy
= NULL
;
1059 snd_soc_cache_exit(codec
);
1062 kfree(codec
->reg_def_copy
);
1063 codec
->reg_def_copy
= NULL
;
1069 static int snd_soc_flat_cache_sync(struct snd_soc_codec
*codec
)
1073 const struct snd_soc_codec_driver
*codec_drv
;
1076 codec_drv
= codec
->driver
;
1077 for (i
= 0; i
< codec_drv
->reg_cache_size
; ++i
) {
1078 WARN_ON(codec
->writable_register
&&
1079 codec
->writable_register(codec
, i
));
1080 ret
= snd_soc_cache_read(codec
, i
, &val
);
1083 if (codec
->reg_def_copy
)
1084 if (snd_soc_get_cache_val(codec
->reg_def_copy
,
1085 i
, codec_drv
->reg_word_size
) == val
)
1087 ret
= snd_soc_write(codec
, i
, val
);
1090 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
1096 static int snd_soc_flat_cache_write(struct snd_soc_codec
*codec
,
1097 unsigned int reg
, unsigned int value
)
1099 snd_soc_set_cache_val(codec
->reg_cache
, reg
, value
,
1100 codec
->driver
->reg_word_size
);
1104 static int snd_soc_flat_cache_read(struct snd_soc_codec
*codec
,
1105 unsigned int reg
, unsigned int *value
)
1107 *value
= snd_soc_get_cache_val(codec
->reg_cache
, reg
,
1108 codec
->driver
->reg_word_size
);
1112 static int snd_soc_flat_cache_exit(struct snd_soc_codec
*codec
)
1114 if (!codec
->reg_cache
)
1116 kfree(codec
->reg_cache
);
1117 codec
->reg_cache
= NULL
;
1121 static int snd_soc_flat_cache_init(struct snd_soc_codec
*codec
)
1123 const struct snd_soc_codec_driver
*codec_drv
;
1125 codec_drv
= codec
->driver
;
1127 if (codec
->reg_def_copy
)
1128 codec
->reg_cache
= kmemdup(codec
->reg_def_copy
,
1129 codec
->reg_size
, GFP_KERNEL
);
1131 codec
->reg_cache
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1132 if (!codec
->reg_cache
)
1138 /* an array of all supported compression types */
1139 static const struct snd_soc_cache_ops cache_types
[] = {
1140 /* Flat *must* be the first entry for fallback */
1142 .id
= SND_SOC_FLAT_COMPRESSION
,
1144 .init
= snd_soc_flat_cache_init
,
1145 .exit
= snd_soc_flat_cache_exit
,
1146 .read
= snd_soc_flat_cache_read
,
1147 .write
= snd_soc_flat_cache_write
,
1148 .sync
= snd_soc_flat_cache_sync
1150 #ifdef CONFIG_SND_SOC_CACHE_LZO
1152 .id
= SND_SOC_LZO_COMPRESSION
,
1154 .init
= snd_soc_lzo_cache_init
,
1155 .exit
= snd_soc_lzo_cache_exit
,
1156 .read
= snd_soc_lzo_cache_read
,
1157 .write
= snd_soc_lzo_cache_write
,
1158 .sync
= snd_soc_lzo_cache_sync
1162 .id
= SND_SOC_RBTREE_COMPRESSION
,
1164 .init
= snd_soc_rbtree_cache_init
,
1165 .exit
= snd_soc_rbtree_cache_exit
,
1166 .read
= snd_soc_rbtree_cache_read
,
1167 .write
= snd_soc_rbtree_cache_write
,
1168 .sync
= snd_soc_rbtree_cache_sync
1172 int snd_soc_cache_init(struct snd_soc_codec
*codec
)
1176 for (i
= 0; i
< ARRAY_SIZE(cache_types
); ++i
)
1177 if (cache_types
[i
].id
== codec
->compress_type
)
1180 /* Fall back to flat compression */
1181 if (i
== ARRAY_SIZE(cache_types
)) {
1182 dev_warn(codec
->dev
, "Could not match compress type: %d\n",
1183 codec
->compress_type
);
1187 mutex_init(&codec
->cache_rw_mutex
);
1188 codec
->cache_ops
= &cache_types
[i
];
1190 if (codec
->cache_ops
->init
) {
1191 if (codec
->cache_ops
->name
)
1192 dev_dbg(codec
->dev
, "Initializing %s cache for %s codec\n",
1193 codec
->cache_ops
->name
, codec
->name
);
1194 return codec
->cache_ops
->init(codec
);
1200 * NOTE: keep in mind that this function might be called
1203 int snd_soc_cache_exit(struct snd_soc_codec
*codec
)
1205 if (codec
->cache_ops
&& codec
->cache_ops
->exit
) {
1206 if (codec
->cache_ops
->name
)
1207 dev_dbg(codec
->dev
, "Destroying %s cache for %s codec\n",
1208 codec
->cache_ops
->name
, codec
->name
);
1209 return codec
->cache_ops
->exit(codec
);
1215 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1217 * @codec: CODEC to configure.
1218 * @reg: The register index.
1219 * @value: The value to be returned.
1221 int snd_soc_cache_read(struct snd_soc_codec
*codec
,
1222 unsigned int reg
, unsigned int *value
)
1226 mutex_lock(&codec
->cache_rw_mutex
);
1228 if (value
&& codec
->cache_ops
&& codec
->cache_ops
->read
) {
1229 ret
= codec
->cache_ops
->read(codec
, reg
, value
);
1230 mutex_unlock(&codec
->cache_rw_mutex
);
1234 mutex_unlock(&codec
->cache_rw_mutex
);
1237 EXPORT_SYMBOL_GPL(snd_soc_cache_read
);
1240 * snd_soc_cache_write: Set the value of a given register in the cache.
1242 * @codec: CODEC to configure.
1243 * @reg: The register index.
1244 * @value: The new register value.
1246 int snd_soc_cache_write(struct snd_soc_codec
*codec
,
1247 unsigned int reg
, unsigned int value
)
1251 mutex_lock(&codec
->cache_rw_mutex
);
1253 if (codec
->cache_ops
&& codec
->cache_ops
->write
) {
1254 ret
= codec
->cache_ops
->write(codec
, reg
, value
);
1255 mutex_unlock(&codec
->cache_rw_mutex
);
1259 mutex_unlock(&codec
->cache_rw_mutex
);
1262 EXPORT_SYMBOL_GPL(snd_soc_cache_write
);
1265 * snd_soc_cache_sync: Sync the register cache with the hardware.
1267 * @codec: CODEC to configure.
1269 * Any registers that should not be synced should be marked as
1270 * volatile. In general drivers can choose not to use the provided
1271 * syncing functionality if they so require.
1273 int snd_soc_cache_sync(struct snd_soc_codec
*codec
)
1278 if (!codec
->cache_sync
) {
1282 if (!codec
->cache_ops
|| !codec
->cache_ops
->sync
)
1285 if (codec
->cache_ops
->name
)
1286 name
= codec
->cache_ops
->name
;
1290 if (codec
->cache_ops
->name
)
1291 dev_dbg(codec
->dev
, "Syncing %s cache for %s codec\n",
1292 codec
->cache_ops
->name
, codec
->name
);
1293 trace_snd_soc_cache_sync(codec
, name
, "start");
1294 ret
= codec
->cache_ops
->sync(codec
);
1296 codec
->cache_sync
= 0;
1297 trace_snd_soc_cache_sync(codec
, name
, "end");
1300 EXPORT_SYMBOL_GPL(snd_soc_cache_sync
);
1302 static int snd_soc_get_reg_access_index(struct snd_soc_codec
*codec
,
1305 const struct snd_soc_codec_driver
*codec_drv
;
1306 unsigned int min
, max
, index
;
1308 codec_drv
= codec
->driver
;
1310 max
= codec_drv
->reg_access_size
- 1;
1312 index
= (min
+ max
) / 2;
1313 if (codec_drv
->reg_access_default
[index
].reg
== reg
)
1315 if (codec_drv
->reg_access_default
[index
].reg
< reg
)
1319 } while (min
<= max
);
1323 int snd_soc_default_volatile_register(struct snd_soc_codec
*codec
,
1328 if (reg
>= codec
->driver
->reg_cache_size
)
1330 index
= snd_soc_get_reg_access_index(codec
, reg
);
1333 return codec
->driver
->reg_access_default
[index
].vol
;
1335 EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register
);
1337 int snd_soc_default_readable_register(struct snd_soc_codec
*codec
,
1342 if (reg
>= codec
->driver
->reg_cache_size
)
1344 index
= snd_soc_get_reg_access_index(codec
, reg
);
1347 return codec
->driver
->reg_access_default
[index
].read
;
1349 EXPORT_SYMBOL_GPL(snd_soc_default_readable_register
);
1351 int snd_soc_default_writable_register(struct snd_soc_codec
*codec
,
1356 if (reg
>= codec
->driver
->reg_cache_size
)
1358 index
= snd_soc_get_reg_access_index(codec
, reg
);
1361 return codec
->driver
->reg_access_default
[index
].write
;
1363 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register
);