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 static unsigned int snd_soc_4_12_read(struct snd_soc_codec
*codec
,
27 if (reg
>= codec
->driver
->reg_cache_size
||
28 snd_soc_codec_volatile_register(codec
, reg
)) {
29 if (codec
->cache_only
)
32 BUG_ON(!codec
->hw_read
);
33 return codec
->hw_read(codec
, reg
);
36 ret
= snd_soc_cache_read(codec
, reg
, &val
);
42 static int snd_soc_4_12_write(struct snd_soc_codec
*codec
, unsigned int reg
,
48 data
[0] = (reg
<< 4) | ((value
>> 8) & 0x000f);
49 data
[1] = value
& 0x00ff;
51 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
52 reg
< codec
->driver
->reg_cache_size
) {
53 ret
= snd_soc_cache_write(codec
, reg
, value
);
58 if (codec
->cache_only
) {
59 codec
->cache_sync
= 1;
63 ret
= codec
->hw_write(codec
->control_data
, data
, 2);
72 #if defined(CONFIG_SPI_MASTER)
73 static int snd_soc_4_12_spi_write(void *control_data
, const char *data
,
76 struct spi_device
*spi
= control_data
;
77 struct spi_transfer t
;
88 memset(&t
, 0, sizeof t
);
93 spi_message_add_tail(&t
, &m
);
99 #define snd_soc_4_12_spi_write NULL
102 static unsigned int snd_soc_7_9_read(struct snd_soc_codec
*codec
,
108 if (reg
>= codec
->driver
->reg_cache_size
||
109 snd_soc_codec_volatile_register(codec
, reg
)) {
110 if (codec
->cache_only
)
113 BUG_ON(!codec
->hw_read
);
114 return codec
->hw_read(codec
, reg
);
117 ret
= snd_soc_cache_read(codec
, reg
, &val
);
123 static int snd_soc_7_9_write(struct snd_soc_codec
*codec
, unsigned int reg
,
129 data
[0] = (reg
<< 1) | ((value
>> 8) & 0x0001);
130 data
[1] = value
& 0x00ff;
132 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
133 reg
< codec
->driver
->reg_cache_size
) {
134 ret
= snd_soc_cache_write(codec
, reg
, value
);
139 if (codec
->cache_only
) {
140 codec
->cache_sync
= 1;
144 ret
= codec
->hw_write(codec
->control_data
, data
, 2);
153 #if defined(CONFIG_SPI_MASTER)
154 static int snd_soc_7_9_spi_write(void *control_data
, const char *data
,
157 struct spi_device
*spi
= control_data
;
158 struct spi_transfer t
;
159 struct spi_message m
;
168 spi_message_init(&m
);
169 memset(&t
, 0, sizeof t
);
174 spi_message_add_tail(&t
, &m
);
180 #define snd_soc_7_9_spi_write NULL
183 static int snd_soc_8_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
191 data
[1] = value
& 0xff;
193 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
194 reg
< codec
->driver
->reg_cache_size
) {
195 ret
= snd_soc_cache_write(codec
, reg
, value
);
200 if (codec
->cache_only
) {
201 codec
->cache_sync
= 1;
205 if (codec
->hw_write(codec
->control_data
, data
, 2) == 2)
211 static unsigned int snd_soc_8_8_read(struct snd_soc_codec
*codec
,
218 if (reg
>= codec
->driver
->reg_cache_size
||
219 snd_soc_codec_volatile_register(codec
, reg
)) {
220 if (codec
->cache_only
)
223 BUG_ON(!codec
->hw_read
);
224 return codec
->hw_read(codec
, reg
);
227 ret
= snd_soc_cache_read(codec
, reg
, &val
);
233 #if defined(CONFIG_SPI_MASTER)
234 static int snd_soc_8_8_spi_write(void *control_data
, const char *data
,
237 struct spi_device
*spi
= control_data
;
238 struct spi_transfer t
;
239 struct spi_message m
;
248 spi_message_init(&m
);
249 memset(&t
, 0, sizeof t
);
254 spi_message_add_tail(&t
, &m
);
260 #define snd_soc_8_8_spi_write NULL
263 static int snd_soc_8_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
270 data
[1] = (value
>> 8) & 0xff;
271 data
[2] = value
& 0xff;
273 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
274 reg
< codec
->driver
->reg_cache_size
) {
275 ret
= snd_soc_cache_write(codec
, reg
, value
);
280 if (codec
->cache_only
) {
281 codec
->cache_sync
= 1;
285 if (codec
->hw_write(codec
->control_data
, data
, 3) == 3)
291 static unsigned int snd_soc_8_16_read(struct snd_soc_codec
*codec
,
297 if (reg
>= codec
->driver
->reg_cache_size
||
298 snd_soc_codec_volatile_register(codec
, reg
)) {
299 if (codec
->cache_only
)
302 BUG_ON(!codec
->hw_read
);
303 return codec
->hw_read(codec
, reg
);
306 ret
= snd_soc_cache_read(codec
, reg
, &val
);
312 #if defined(CONFIG_SPI_MASTER)
313 static int snd_soc_8_16_spi_write(void *control_data
, const char *data
,
316 struct spi_device
*spi
= control_data
;
317 struct spi_transfer t
;
318 struct spi_message m
;
328 spi_message_init(&m
);
329 memset(&t
, 0, sizeof t
);
334 spi_message_add_tail(&t
, &m
);
340 #define snd_soc_8_16_spi_write NULL
343 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
344 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec
*codec
,
347 struct i2c_msg xfer
[2];
351 struct i2c_client
*client
= codec
->control_data
;
354 xfer
[0].addr
= client
->addr
;
360 xfer
[1].addr
= client
->addr
;
361 xfer
[1].flags
= I2C_M_RD
;
365 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
367 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
374 #define snd_soc_8_8_read_i2c NULL
377 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
378 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec
*codec
,
381 struct i2c_msg xfer
[2];
385 struct i2c_client
*client
= codec
->control_data
;
388 xfer
[0].addr
= client
->addr
;
394 xfer
[1].addr
= client
->addr
;
395 xfer
[1].flags
= I2C_M_RD
;
397 xfer
[1].buf
= (u8
*)&data
;
399 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
401 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
405 return (data
>> 8) | ((data
& 0xff) << 8);
408 #define snd_soc_8_16_read_i2c NULL
411 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
412 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec
*codec
,
415 struct i2c_msg xfer
[2];
419 struct i2c_client
*client
= codec
->control_data
;
422 xfer
[0].addr
= client
->addr
;
425 xfer
[0].buf
= (u8
*)®
;
428 xfer
[1].addr
= client
->addr
;
429 xfer
[1].flags
= I2C_M_RD
;
433 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
435 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
442 #define snd_soc_16_8_read_i2c NULL
445 static unsigned int snd_soc_16_8_read(struct snd_soc_codec
*codec
,
452 if (reg
>= codec
->driver
->reg_cache_size
||
453 snd_soc_codec_volatile_register(codec
, reg
)) {
454 if (codec
->cache_only
)
457 BUG_ON(!codec
->hw_read
);
458 return codec
->hw_read(codec
, reg
);
461 ret
= snd_soc_cache_read(codec
, reg
, &val
);
467 static int snd_soc_16_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
473 data
[0] = (reg
>> 8) & 0xff;
474 data
[1] = reg
& 0xff;
478 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
479 reg
< codec
->driver
->reg_cache_size
) {
480 ret
= snd_soc_cache_write(codec
, reg
, value
);
485 if (codec
->cache_only
) {
486 codec
->cache_sync
= 1;
490 ret
= codec
->hw_write(codec
->control_data
, data
, 3);
499 #if defined(CONFIG_SPI_MASTER)
500 static int snd_soc_16_8_spi_write(void *control_data
, const char *data
,
503 struct spi_device
*spi
= control_data
;
504 struct spi_transfer t
;
505 struct spi_message m
;
515 spi_message_init(&m
);
516 memset(&t
, 0, sizeof t
);
521 spi_message_add_tail(&t
, &m
);
527 #define snd_soc_16_8_spi_write NULL
530 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
531 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec
*codec
,
534 struct i2c_msg xfer
[2];
535 u16 reg
= cpu_to_be16(r
);
538 struct i2c_client
*client
= codec
->control_data
;
541 xfer
[0].addr
= client
->addr
;
544 xfer
[0].buf
= (u8
*)®
;
547 xfer
[1].addr
= client
->addr
;
548 xfer
[1].flags
= I2C_M_RD
;
550 xfer
[1].buf
= (u8
*)&data
;
552 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
554 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
558 return be16_to_cpu(data
);
561 #define snd_soc_16_16_read_i2c NULL
564 static unsigned int snd_soc_16_16_read(struct snd_soc_codec
*codec
,
570 if (reg
>= codec
->driver
->reg_cache_size
||
571 snd_soc_codec_volatile_register(codec
, reg
)) {
572 if (codec
->cache_only
)
575 BUG_ON(!codec
->hw_read
);
576 return codec
->hw_read(codec
, reg
);
579 ret
= snd_soc_cache_read(codec
, reg
, &val
);
586 static int snd_soc_16_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
592 data
[0] = (reg
>> 8) & 0xff;
593 data
[1] = reg
& 0xff;
594 data
[2] = (value
>> 8) & 0xff;
595 data
[3] = value
& 0xff;
597 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
598 reg
< codec
->driver
->reg_cache_size
) {
599 ret
= snd_soc_cache_write(codec
, reg
, value
);
604 if (codec
->cache_only
) {
605 codec
->cache_sync
= 1;
609 ret
= codec
->hw_write(codec
->control_data
, data
, 4);
618 #if defined(CONFIG_SPI_MASTER)
619 static int snd_soc_16_16_spi_write(void *control_data
, const char *data
,
622 struct spi_device
*spi
= control_data
;
623 struct spi_transfer t
;
624 struct spi_message m
;
635 spi_message_init(&m
);
636 memset(&t
, 0, sizeof t
);
641 spi_message_add_tail(&t
, &m
);
647 #define snd_soc_16_16_spi_write NULL
653 int (*write
)(struct snd_soc_codec
*codec
, unsigned int, unsigned int);
654 int (*spi_write
)(void *, const char *, int);
655 unsigned int (*read
)(struct snd_soc_codec
*, unsigned int);
656 unsigned int (*i2c_read
)(struct snd_soc_codec
*, unsigned int);
659 .addr_bits
= 4, .data_bits
= 12,
660 .write
= snd_soc_4_12_write
, .read
= snd_soc_4_12_read
,
661 .spi_write
= snd_soc_4_12_spi_write
,
664 .addr_bits
= 7, .data_bits
= 9,
665 .write
= snd_soc_7_9_write
, .read
= snd_soc_7_9_read
,
666 .spi_write
= snd_soc_7_9_spi_write
,
669 .addr_bits
= 8, .data_bits
= 8,
670 .write
= snd_soc_8_8_write
, .read
= snd_soc_8_8_read
,
671 .i2c_read
= snd_soc_8_8_read_i2c
,
672 .spi_write
= snd_soc_8_8_spi_write
,
675 .addr_bits
= 8, .data_bits
= 16,
676 .write
= snd_soc_8_16_write
, .read
= snd_soc_8_16_read
,
677 .i2c_read
= snd_soc_8_16_read_i2c
,
678 .spi_write
= snd_soc_8_16_spi_write
,
681 .addr_bits
= 16, .data_bits
= 8,
682 .write
= snd_soc_16_8_write
, .read
= snd_soc_16_8_read
,
683 .i2c_read
= snd_soc_16_8_read_i2c
,
684 .spi_write
= snd_soc_16_8_spi_write
,
687 .addr_bits
= 16, .data_bits
= 16,
688 .write
= snd_soc_16_16_write
, .read
= snd_soc_16_16_read
,
689 .i2c_read
= snd_soc_16_16_read_i2c
,
690 .spi_write
= snd_soc_16_16_spi_write
,
695 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
697 * @codec: CODEC to configure.
698 * @type: Type of cache.
699 * @addr_bits: Number of bits of register address data.
700 * @data_bits: Number of bits of data per register.
701 * @control: Control bus used.
703 * Register formats are frequently shared between many I2C and SPI
704 * devices. In order to promote code reuse the ASoC core provides
705 * some standard implementations of CODEC read and write operations
706 * which can be set up using this function.
708 * The caller is responsible for allocating and initialising the
711 * Note that at present this code cannot be used by CODECs with
712 * volatile registers.
714 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
715 int addr_bits
, int data_bits
,
716 enum snd_soc_control_type control
)
720 for (i
= 0; i
< ARRAY_SIZE(io_types
); i
++)
721 if (io_types
[i
].addr_bits
== addr_bits
&&
722 io_types
[i
].data_bits
== data_bits
)
724 if (i
== ARRAY_SIZE(io_types
)) {
726 "No I/O functions for %d bit address %d bit data\n",
727 addr_bits
, data_bits
);
731 codec
->write
= io_types
[i
].write
;
732 codec
->read
= io_types
[i
].read
;
739 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
740 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
742 if (io_types
[i
].i2c_read
)
743 codec
->hw_read
= io_types
[i
].i2c_read
;
745 codec
->control_data
= container_of(codec
->dev
,
751 if (io_types
[i
].spi_write
)
752 codec
->hw_write
= io_types
[i
].spi_write
;
754 codec
->control_data
= container_of(codec
->dev
,
762 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);
764 static bool snd_soc_set_cache_val(void *base
, unsigned int idx
,
765 unsigned int val
, unsigned int word_size
)
770 if (cache
[idx
] == val
)
777 if (cache
[idx
] == val
)
788 static unsigned int snd_soc_get_cache_val(const void *base
, unsigned int idx
,
789 unsigned int word_size
)
793 const u8
*cache
= base
;
797 const u16
*cache
= base
;
807 struct snd_soc_rbtree_node
{
812 } __attribute__ ((packed
));
814 struct snd_soc_rbtree_ctx
{
818 static struct snd_soc_rbtree_node
*snd_soc_rbtree_lookup(
819 struct rb_root
*root
, unsigned int reg
)
821 struct rb_node
*node
;
822 struct snd_soc_rbtree_node
*rbnode
;
824 node
= root
->rb_node
;
826 rbnode
= container_of(node
, struct snd_soc_rbtree_node
, node
);
827 if (rbnode
->reg
< reg
)
828 node
= node
->rb_left
;
829 else if (rbnode
->reg
> reg
)
830 node
= node
->rb_right
;
838 static int snd_soc_rbtree_insert(struct rb_root
*root
,
839 struct snd_soc_rbtree_node
*rbnode
)
841 struct rb_node
**new, *parent
;
842 struct snd_soc_rbtree_node
*rbnode_tmp
;
845 new = &root
->rb_node
;
847 rbnode_tmp
= container_of(*new, struct snd_soc_rbtree_node
,
850 if (rbnode_tmp
->reg
< rbnode
->reg
)
851 new = &((*new)->rb_left
);
852 else if (rbnode_tmp
->reg
> rbnode
->reg
)
853 new = &((*new)->rb_right
);
858 /* insert the node into the rbtree */
859 rb_link_node(&rbnode
->node
, parent
, new);
860 rb_insert_color(&rbnode
->node
, root
);
865 static int snd_soc_rbtree_cache_sync(struct snd_soc_codec
*codec
)
867 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
868 struct rb_node
*node
;
869 struct snd_soc_rbtree_node
*rbnode
;
873 rbtree_ctx
= codec
->reg_cache
;
874 for (node
= rb_first(&rbtree_ctx
->root
); node
; node
= rb_next(node
)) {
875 rbnode
= rb_entry(node
, struct snd_soc_rbtree_node
, node
);
876 if (rbnode
->value
== rbnode
->defval
)
878 ret
= snd_soc_cache_read(codec
, rbnode
->reg
, &val
);
881 ret
= snd_soc_write(codec
, rbnode
->reg
, val
);
884 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
891 static int snd_soc_rbtree_cache_write(struct snd_soc_codec
*codec
,
892 unsigned int reg
, unsigned int value
)
894 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
895 struct snd_soc_rbtree_node
*rbnode
;
897 rbtree_ctx
= codec
->reg_cache
;
898 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
900 if (rbnode
->value
== value
)
902 rbnode
->value
= value
;
904 /* bail out early, no need to create the rbnode yet */
908 * for uninitialized registers whose value is changed
909 * from the default zero, create an rbnode and insert
912 rbnode
= kzalloc(sizeof *rbnode
, GFP_KERNEL
);
916 rbnode
->value
= value
;
917 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbnode
);
923 static int snd_soc_rbtree_cache_read(struct snd_soc_codec
*codec
,
924 unsigned int reg
, unsigned int *value
)
926 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
927 struct snd_soc_rbtree_node
*rbnode
;
929 rbtree_ctx
= codec
->reg_cache
;
930 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
932 *value
= rbnode
->value
;
934 /* uninitialized registers default to 0 */
941 static int snd_soc_rbtree_cache_exit(struct snd_soc_codec
*codec
)
943 struct rb_node
*next
;
944 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
945 struct snd_soc_rbtree_node
*rbtree_node
;
947 /* if we've already been called then just return */
948 rbtree_ctx
= codec
->reg_cache
;
952 /* free up the rbtree */
953 next
= rb_first(&rbtree_ctx
->root
);
955 rbtree_node
= rb_entry(next
, struct snd_soc_rbtree_node
, node
);
956 next
= rb_next(&rbtree_node
->node
);
957 rb_erase(&rbtree_node
->node
, &rbtree_ctx
->root
);
961 /* release the resources */
962 kfree(codec
->reg_cache
);
963 codec
->reg_cache
= NULL
;
968 static int snd_soc_rbtree_cache_init(struct snd_soc_codec
*codec
)
970 struct snd_soc_rbtree_node
*rbtree_node
;
971 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
973 unsigned int word_size
;
977 codec
->reg_cache
= kmalloc(sizeof *rbtree_ctx
, GFP_KERNEL
);
978 if (!codec
->reg_cache
)
981 rbtree_ctx
= codec
->reg_cache
;
982 rbtree_ctx
->root
= RB_ROOT
;
984 if (!codec
->reg_def_copy
)
988 * populate the rbtree with the initialized registers. All other
989 * registers will be inserted when they are first modified.
991 word_size
= codec
->driver
->reg_word_size
;
992 for (i
= 0; i
< codec
->driver
->reg_cache_size
; ++i
) {
993 val
= snd_soc_get_cache_val(codec
->reg_def_copy
, i
, word_size
);
996 rbtree_node
= kzalloc(sizeof *rbtree_node
, GFP_KERNEL
);
999 snd_soc_cache_exit(codec
);
1002 rbtree_node
->reg
= i
;
1003 rbtree_node
->value
= val
;
1004 rbtree_node
->defval
= val
;
1005 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbtree_node
);
1011 #ifdef CONFIG_SND_SOC_CACHE_LZO
1012 struct snd_soc_lzo_ctx
{
1018 size_t decompressed_size
;
1019 unsigned long *sync_bmp
;
1023 #define LZO_BLOCK_NUM 8
1024 static int snd_soc_lzo_block_count(void)
1026 return LZO_BLOCK_NUM
;
1029 static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx
*lzo_ctx
)
1031 lzo_ctx
->wmem
= kmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
1037 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx
*lzo_ctx
)
1039 size_t compress_size
;
1042 ret
= lzo1x_1_compress(lzo_ctx
->src
, lzo_ctx
->src_len
,
1043 lzo_ctx
->dst
, &compress_size
, lzo_ctx
->wmem
);
1044 if (ret
!= LZO_E_OK
|| compress_size
> lzo_ctx
->dst_len
)
1046 lzo_ctx
->dst_len
= compress_size
;
1050 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx
*lzo_ctx
)
1055 dst_len
= lzo_ctx
->dst_len
;
1056 ret
= lzo1x_decompress_safe(lzo_ctx
->src
, lzo_ctx
->src_len
,
1057 lzo_ctx
->dst
, &dst_len
);
1058 if (ret
!= LZO_E_OK
|| dst_len
!= lzo_ctx
->dst_len
)
1063 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec
*codec
,
1064 struct snd_soc_lzo_ctx
*lzo_ctx
)
1068 lzo_ctx
->dst_len
= lzo1x_worst_compress(PAGE_SIZE
);
1069 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
1070 if (!lzo_ctx
->dst
) {
1071 lzo_ctx
->dst_len
= 0;
1075 ret
= snd_soc_lzo_compress(lzo_ctx
);
1081 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec
*codec
,
1082 struct snd_soc_lzo_ctx
*lzo_ctx
)
1086 lzo_ctx
->dst_len
= lzo_ctx
->decompressed_size
;
1087 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
1088 if (!lzo_ctx
->dst
) {
1089 lzo_ctx
->dst_len
= 0;
1093 ret
= snd_soc_lzo_decompress(lzo_ctx
);
1099 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec
*codec
,
1102 const struct snd_soc_codec_driver
*codec_drv
;
1104 codec_drv
= codec
->driver
;
1105 return (reg
* codec_drv
->reg_word_size
) /
1106 DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
1109 static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec
*codec
,
1112 const struct snd_soc_codec_driver
*codec_drv
;
1114 codec_drv
= codec
->driver
;
1115 return reg
% (DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count()) /
1116 codec_drv
->reg_word_size
);
1119 static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec
*codec
)
1121 const struct snd_soc_codec_driver
*codec_drv
;
1123 codec_drv
= codec
->driver
;
1124 return DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
1127 static int snd_soc_lzo_cache_sync(struct snd_soc_codec
*codec
)
1129 struct snd_soc_lzo_ctx
**lzo_blocks
;
1134 lzo_blocks
= codec
->reg_cache
;
1135 for_each_set_bit(i
, lzo_blocks
[0]->sync_bmp
, lzo_blocks
[0]->sync_bmp_nbits
) {
1136 ret
= snd_soc_cache_read(codec
, i
, &val
);
1139 ret
= snd_soc_write(codec
, i
, val
);
1142 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
1149 static int snd_soc_lzo_cache_write(struct snd_soc_codec
*codec
,
1150 unsigned int reg
, unsigned int value
)
1152 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
1153 int ret
, blkindex
, blkpos
;
1154 size_t blksize
, tmp_dst_len
;
1157 /* index of the compressed lzo block */
1158 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
1159 /* register index within the decompressed block */
1160 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
1161 /* size of the compressed block */
1162 blksize
= snd_soc_lzo_get_blksize(codec
);
1163 lzo_blocks
= codec
->reg_cache
;
1164 lzo_block
= lzo_blocks
[blkindex
];
1166 /* save the pointer and length of the compressed block */
1167 tmp_dst
= lzo_block
->dst
;
1168 tmp_dst_len
= lzo_block
->dst_len
;
1170 /* prepare the source to be the compressed block */
1171 lzo_block
->src
= lzo_block
->dst
;
1172 lzo_block
->src_len
= lzo_block
->dst_len
;
1174 /* decompress the block */
1175 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
1177 kfree(lzo_block
->dst
);
1181 /* write the new value to the cache */
1182 if (snd_soc_set_cache_val(lzo_block
->dst
, blkpos
, value
,
1183 codec
->driver
->reg_word_size
)) {
1184 kfree(lzo_block
->dst
);
1188 /* prepare the source to be the decompressed block */
1189 lzo_block
->src
= lzo_block
->dst
;
1190 lzo_block
->src_len
= lzo_block
->dst_len
;
1192 /* compress the block */
1193 ret
= snd_soc_lzo_compress_cache_block(codec
, lzo_block
);
1195 kfree(lzo_block
->dst
);
1196 kfree(lzo_block
->src
);
1200 /* set the bit so we know we have to sync this register */
1201 set_bit(reg
, lzo_block
->sync_bmp
);
1203 kfree(lzo_block
->src
);
1206 lzo_block
->dst
= tmp_dst
;
1207 lzo_block
->dst_len
= tmp_dst_len
;
1211 static int snd_soc_lzo_cache_read(struct snd_soc_codec
*codec
,
1212 unsigned int reg
, unsigned int *value
)
1214 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
1215 int ret
, blkindex
, blkpos
;
1216 size_t blksize
, tmp_dst_len
;
1220 /* index of the compressed lzo block */
1221 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
1222 /* register index within the decompressed block */
1223 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
1224 /* size of the compressed block */
1225 blksize
= snd_soc_lzo_get_blksize(codec
);
1226 lzo_blocks
= codec
->reg_cache
;
1227 lzo_block
= lzo_blocks
[blkindex
];
1229 /* save the pointer and length of the compressed block */
1230 tmp_dst
= lzo_block
->dst
;
1231 tmp_dst_len
= lzo_block
->dst_len
;
1233 /* prepare the source to be the compressed block */
1234 lzo_block
->src
= lzo_block
->dst
;
1235 lzo_block
->src_len
= lzo_block
->dst_len
;
1237 /* decompress the block */
1238 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
1240 /* fetch the value from the cache */
1241 *value
= snd_soc_get_cache_val(lzo_block
->dst
, blkpos
,
1242 codec
->driver
->reg_word_size
);
1244 kfree(lzo_block
->dst
);
1245 /* restore the pointer and length of the compressed block */
1246 lzo_block
->dst
= tmp_dst
;
1247 lzo_block
->dst_len
= tmp_dst_len
;
1251 static int snd_soc_lzo_cache_exit(struct snd_soc_codec
*codec
)
1253 struct snd_soc_lzo_ctx
**lzo_blocks
;
1256 lzo_blocks
= codec
->reg_cache
;
1260 blkcount
= snd_soc_lzo_block_count();
1262 * the pointer to the bitmap used for syncing the cache
1263 * is shared amongst all lzo_blocks. Ensure it is freed
1267 kfree(lzo_blocks
[0]->sync_bmp
);
1268 for (i
= 0; i
< blkcount
; ++i
) {
1269 if (lzo_blocks
[i
]) {
1270 kfree(lzo_blocks
[i
]->wmem
);
1271 kfree(lzo_blocks
[i
]->dst
);
1273 /* each lzo_block is a pointer returned by kmalloc or NULL */
1274 kfree(lzo_blocks
[i
]);
1277 codec
->reg_cache
= NULL
;
1281 static int snd_soc_lzo_cache_init(struct snd_soc_codec
*codec
)
1283 struct snd_soc_lzo_ctx
**lzo_blocks
;
1285 const struct snd_soc_codec_driver
*codec_drv
;
1286 int ret
, tofree
, i
, blksize
, blkcount
;
1287 const char *p
, *end
;
1288 unsigned long *sync_bmp
;
1291 codec_drv
= codec
->driver
;
1294 * If we have not been given a default register cache
1295 * then allocate a dummy zero-ed out region, compress it
1296 * and remember to free it afterwards.
1299 if (!codec
->reg_def_copy
)
1302 if (!codec
->reg_def_copy
) {
1303 codec
->reg_def_copy
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1304 if (!codec
->reg_def_copy
)
1308 blkcount
= snd_soc_lzo_block_count();
1309 codec
->reg_cache
= kzalloc(blkcount
* sizeof *lzo_blocks
,
1311 if (!codec
->reg_cache
) {
1315 lzo_blocks
= codec
->reg_cache
;
1318 * allocate a bitmap to be used when syncing the cache with
1319 * the hardware. Each time a register is modified, the corresponding
1320 * bit is set in the bitmap, so we know that we have to sync
1323 bmp_size
= codec_drv
->reg_cache_size
;
1324 sync_bmp
= kmalloc(BITS_TO_LONGS(bmp_size
) * sizeof(long),
1330 bitmap_zero(sync_bmp
, bmp_size
);
1332 /* allocate the lzo blocks and initialize them */
1333 for (i
= 0; i
< blkcount
; ++i
) {
1334 lzo_blocks
[i
] = kzalloc(sizeof **lzo_blocks
,
1336 if (!lzo_blocks
[i
]) {
1341 lzo_blocks
[i
]->sync_bmp
= sync_bmp
;
1342 lzo_blocks
[i
]->sync_bmp_nbits
= reg_size
;
1343 /* alloc the working space for the compressed block */
1344 ret
= snd_soc_lzo_prepare(lzo_blocks
[i
]);
1349 blksize
= snd_soc_lzo_get_blksize(codec
);
1350 p
= codec
->reg_def_copy
;
1351 end
= codec
->reg_def_copy
+ codec
->reg_size
;
1352 /* compress the register map and fill the lzo blocks */
1353 for (i
= 0; i
< blkcount
; ++i
, p
+= blksize
) {
1354 lzo_blocks
[i
]->src
= p
;
1355 if (p
+ blksize
> end
)
1356 lzo_blocks
[i
]->src_len
= end
- p
;
1358 lzo_blocks
[i
]->src_len
= blksize
;
1359 ret
= snd_soc_lzo_compress_cache_block(codec
,
1363 lzo_blocks
[i
]->decompressed_size
=
1364 lzo_blocks
[i
]->src_len
;
1368 kfree(codec
->reg_def_copy
);
1369 codec
->reg_def_copy
= NULL
;
1373 snd_soc_cache_exit(codec
);
1376 kfree(codec
->reg_def_copy
);
1377 codec
->reg_def_copy
= NULL
;
1383 static int snd_soc_flat_cache_sync(struct snd_soc_codec
*codec
)
1387 const struct snd_soc_codec_driver
*codec_drv
;
1390 codec_drv
= codec
->driver
;
1391 for (i
= 0; i
< codec_drv
->reg_cache_size
; ++i
) {
1392 ret
= snd_soc_cache_read(codec
, i
, &val
);
1395 if (codec
->reg_def_copy
)
1396 if (snd_soc_get_cache_val(codec
->reg_def_copy
,
1397 i
, codec_drv
->reg_word_size
) == val
)
1399 ret
= snd_soc_write(codec
, i
, val
);
1402 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
1408 static int snd_soc_flat_cache_write(struct snd_soc_codec
*codec
,
1409 unsigned int reg
, unsigned int value
)
1411 snd_soc_set_cache_val(codec
->reg_cache
, reg
, value
,
1412 codec
->driver
->reg_word_size
);
1416 static int snd_soc_flat_cache_read(struct snd_soc_codec
*codec
,
1417 unsigned int reg
, unsigned int *value
)
1419 *value
= snd_soc_get_cache_val(codec
->reg_cache
, reg
,
1420 codec
->driver
->reg_word_size
);
1424 static int snd_soc_flat_cache_exit(struct snd_soc_codec
*codec
)
1426 if (!codec
->reg_cache
)
1428 kfree(codec
->reg_cache
);
1429 codec
->reg_cache
= NULL
;
1433 static int snd_soc_flat_cache_init(struct snd_soc_codec
*codec
)
1435 const struct snd_soc_codec_driver
*codec_drv
;
1437 codec_drv
= codec
->driver
;
1439 if (codec
->reg_def_copy
)
1440 codec
->reg_cache
= kmemdup(codec
->reg_def_copy
,
1441 codec
->reg_size
, GFP_KERNEL
);
1443 codec
->reg_cache
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1444 if (!codec
->reg_cache
)
1450 /* an array of all supported compression types */
1451 static const struct snd_soc_cache_ops cache_types
[] = {
1452 /* Flat *must* be the first entry for fallback */
1454 .id
= SND_SOC_FLAT_COMPRESSION
,
1456 .init
= snd_soc_flat_cache_init
,
1457 .exit
= snd_soc_flat_cache_exit
,
1458 .read
= snd_soc_flat_cache_read
,
1459 .write
= snd_soc_flat_cache_write
,
1460 .sync
= snd_soc_flat_cache_sync
1462 #ifdef CONFIG_SND_SOC_CACHE_LZO
1464 .id
= SND_SOC_LZO_COMPRESSION
,
1466 .init
= snd_soc_lzo_cache_init
,
1467 .exit
= snd_soc_lzo_cache_exit
,
1468 .read
= snd_soc_lzo_cache_read
,
1469 .write
= snd_soc_lzo_cache_write
,
1470 .sync
= snd_soc_lzo_cache_sync
1474 .id
= SND_SOC_RBTREE_COMPRESSION
,
1476 .init
= snd_soc_rbtree_cache_init
,
1477 .exit
= snd_soc_rbtree_cache_exit
,
1478 .read
= snd_soc_rbtree_cache_read
,
1479 .write
= snd_soc_rbtree_cache_write
,
1480 .sync
= snd_soc_rbtree_cache_sync
1484 int snd_soc_cache_init(struct snd_soc_codec
*codec
)
1488 for (i
= 0; i
< ARRAY_SIZE(cache_types
); ++i
)
1489 if (cache_types
[i
].id
== codec
->compress_type
)
1492 /* Fall back to flat compression */
1493 if (i
== ARRAY_SIZE(cache_types
)) {
1494 dev_warn(codec
->dev
, "Could not match compress type: %d\n",
1495 codec
->compress_type
);
1499 mutex_init(&codec
->cache_rw_mutex
);
1500 codec
->cache_ops
= &cache_types
[i
];
1502 if (codec
->cache_ops
->init
) {
1503 if (codec
->cache_ops
->name
)
1504 dev_dbg(codec
->dev
, "Initializing %s cache for %s codec\n",
1505 codec
->cache_ops
->name
, codec
->name
);
1506 return codec
->cache_ops
->init(codec
);
1512 * NOTE: keep in mind that this function might be called
1515 int snd_soc_cache_exit(struct snd_soc_codec
*codec
)
1517 if (codec
->cache_ops
&& codec
->cache_ops
->exit
) {
1518 if (codec
->cache_ops
->name
)
1519 dev_dbg(codec
->dev
, "Destroying %s cache for %s codec\n",
1520 codec
->cache_ops
->name
, codec
->name
);
1521 return codec
->cache_ops
->exit(codec
);
1527 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1529 * @codec: CODEC to configure.
1530 * @reg: The register index.
1531 * @value: The value to be returned.
1533 int snd_soc_cache_read(struct snd_soc_codec
*codec
,
1534 unsigned int reg
, unsigned int *value
)
1538 mutex_lock(&codec
->cache_rw_mutex
);
1540 if (value
&& codec
->cache_ops
&& codec
->cache_ops
->read
) {
1541 ret
= codec
->cache_ops
->read(codec
, reg
, value
);
1542 mutex_unlock(&codec
->cache_rw_mutex
);
1546 mutex_unlock(&codec
->cache_rw_mutex
);
1549 EXPORT_SYMBOL_GPL(snd_soc_cache_read
);
1552 * snd_soc_cache_write: Set the value of a given register in the cache.
1554 * @codec: CODEC to configure.
1555 * @reg: The register index.
1556 * @value: The new register value.
1558 int snd_soc_cache_write(struct snd_soc_codec
*codec
,
1559 unsigned int reg
, unsigned int value
)
1563 mutex_lock(&codec
->cache_rw_mutex
);
1565 if (codec
->cache_ops
&& codec
->cache_ops
->write
) {
1566 ret
= codec
->cache_ops
->write(codec
, reg
, value
);
1567 mutex_unlock(&codec
->cache_rw_mutex
);
1571 mutex_unlock(&codec
->cache_rw_mutex
);
1574 EXPORT_SYMBOL_GPL(snd_soc_cache_write
);
1577 * snd_soc_cache_sync: Sync the register cache with the hardware.
1579 * @codec: CODEC to configure.
1581 * Any registers that should not be synced should be marked as
1582 * volatile. In general drivers can choose not to use the provided
1583 * syncing functionality if they so require.
1585 int snd_soc_cache_sync(struct snd_soc_codec
*codec
)
1589 if (!codec
->cache_sync
) {
1593 if (codec
->cache_ops
&& codec
->cache_ops
->sync
) {
1594 if (codec
->cache_ops
->name
)
1595 dev_dbg(codec
->dev
, "Syncing %s cache for %s codec\n",
1596 codec
->cache_ops
->name
, codec
->name
);
1597 ret
= codec
->cache_ops
->sync(codec
);
1599 codec
->cache_sync
= 0;
1605 EXPORT_SYMBOL_GPL(snd_soc_cache_sync
);