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
[0] = (reg
<< 1) | ((value
>> 8) & 0x0001);
139 data
[1] = value
& 0x00ff;
141 return do_hw_write(codec
, reg
, value
, data
, 2);
144 #if defined(CONFIG_SPI_MASTER)
145 static int snd_soc_7_9_spi_write(void *control_data
, const char *data
,
153 return do_spi_write(control_data
, msg
, len
);
156 #define snd_soc_7_9_spi_write NULL
159 static int snd_soc_8_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
166 data
[1] = value
& 0xff;
168 return do_hw_write(codec
, reg
, value
, data
, 2);
171 static unsigned int snd_soc_8_8_read(struct snd_soc_codec
*codec
,
174 return do_hw_read(codec
, reg
);
177 #if defined(CONFIG_SPI_MASTER)
178 static int snd_soc_8_8_spi_write(void *control_data
, const char *data
,
186 return do_spi_write(control_data
, msg
, len
);
189 #define snd_soc_8_8_spi_write NULL
192 static int snd_soc_8_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
198 data
[1] = (value
>> 8) & 0xff;
199 data
[2] = value
& 0xff;
201 return do_hw_write(codec
, reg
, value
, data
, 3);
204 static unsigned int snd_soc_8_16_read(struct snd_soc_codec
*codec
,
207 return do_hw_read(codec
, reg
);
210 #if defined(CONFIG_SPI_MASTER)
211 static int snd_soc_8_16_spi_write(void *control_data
, const char *data
,
220 return do_spi_write(control_data
, msg
, len
);
223 #define snd_soc_8_16_spi_write NULL
226 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
227 static unsigned int do_i2c_read(struct snd_soc_codec
*codec
,
228 void *reg
, int reglen
,
229 void *data
, int datalen
)
231 struct i2c_msg xfer
[2];
233 struct i2c_client
*client
= codec
->control_data
;
236 xfer
[0].addr
= client
->addr
;
238 xfer
[0].len
= reglen
;
242 xfer
[1].addr
= client
->addr
;
243 xfer
[1].flags
= I2C_M_RD
;
244 xfer
[1].len
= datalen
;
247 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
257 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
258 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec
*codec
,
265 ret
= do_i2c_read(codec
, ®
, 1, &data
, 1);
271 #define snd_soc_8_8_read_i2c NULL
274 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
275 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec
*codec
,
282 ret
= do_i2c_read(codec
, ®
, 1, &data
, 2);
285 return (data
>> 8) | ((data
& 0xff) << 8);
288 #define snd_soc_8_16_read_i2c NULL
291 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
292 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec
*codec
,
299 ret
= do_i2c_read(codec
, ®
, 2, &data
, 1);
305 #define snd_soc_16_8_read_i2c NULL
308 static unsigned int snd_soc_16_8_read(struct snd_soc_codec
*codec
,
311 return do_hw_read(codec
, reg
);
314 static int snd_soc_16_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
319 data
[0] = (reg
>> 8) & 0xff;
320 data
[1] = reg
& 0xff;
324 return do_hw_write(codec
, reg
, value
, data
, 3);
327 #if defined(CONFIG_SPI_MASTER)
328 static int snd_soc_16_8_spi_write(void *control_data
, const char *data
,
337 return do_spi_write(control_data
, msg
, len
);
340 #define snd_soc_16_8_spi_write NULL
343 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
344 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec
*codec
,
347 u16 reg
= cpu_to_be16(r
);
351 ret
= do_i2c_read(codec
, ®
, 2, &data
, 2);
354 return be16_to_cpu(data
);
357 #define snd_soc_16_16_read_i2c NULL
360 static unsigned int snd_soc_16_16_read(struct snd_soc_codec
*codec
,
363 return do_hw_read(codec
, reg
);
366 static int snd_soc_16_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
371 data
[0] = (reg
>> 8) & 0xff;
372 data
[1] = reg
& 0xff;
373 data
[2] = (value
>> 8) & 0xff;
374 data
[3] = value
& 0xff;
376 return do_hw_write(codec
, reg
, value
, data
, 4);
379 #if defined(CONFIG_SPI_MASTER)
380 static int snd_soc_16_16_spi_write(void *control_data
, const char *data
,
390 return do_spi_write(control_data
, msg
, len
);
393 #define snd_soc_16_16_spi_write NULL
396 /* Primitive bulk write support for soc-cache. The data pointed to by
397 * `data' needs to already be in the form the hardware expects
398 * including any leading register specific data. Any data written
399 * through this function will not go through the cache as it only
400 * handles writing to volatile or out of bounds registers.
402 static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec
*codec
, unsigned int reg
,
403 const void *data
, size_t len
)
407 /* Ensure that the base register is volatile. Subsequently
408 * any other register that is touched by this routine should be
409 * volatile as well to ensure that we don't get out of sync with
412 if (!snd_soc_codec_volatile_register(codec
, reg
)
413 && reg
< codec
->driver
->reg_cache_size
)
416 switch (codec
->control_type
) {
417 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
419 ret
= i2c_master_send(codec
->control_data
, data
, len
);
422 #if defined(CONFIG_SPI_MASTER)
424 ret
= do_spi_write(codec
->control_data
, data
, len
);
442 int (*write
)(struct snd_soc_codec
*codec
, unsigned int, unsigned int);
443 int (*spi_write
)(void *, const char *, int);
444 unsigned int (*read
)(struct snd_soc_codec
*, unsigned int);
445 unsigned int (*i2c_read
)(struct snd_soc_codec
*, unsigned int);
448 .addr_bits
= 4, .data_bits
= 12,
449 .write
= snd_soc_4_12_write
, .read
= snd_soc_4_12_read
,
450 .spi_write
= snd_soc_4_12_spi_write
,
453 .addr_bits
= 7, .data_bits
= 9,
454 .write
= snd_soc_7_9_write
, .read
= snd_soc_7_9_read
,
455 .spi_write
= snd_soc_7_9_spi_write
,
458 .addr_bits
= 8, .data_bits
= 8,
459 .write
= snd_soc_8_8_write
, .read
= snd_soc_8_8_read
,
460 .i2c_read
= snd_soc_8_8_read_i2c
,
461 .spi_write
= snd_soc_8_8_spi_write
,
464 .addr_bits
= 8, .data_bits
= 16,
465 .write
= snd_soc_8_16_write
, .read
= snd_soc_8_16_read
,
466 .i2c_read
= snd_soc_8_16_read_i2c
,
467 .spi_write
= snd_soc_8_16_spi_write
,
470 .addr_bits
= 16, .data_bits
= 8,
471 .write
= snd_soc_16_8_write
, .read
= snd_soc_16_8_read
,
472 .i2c_read
= snd_soc_16_8_read_i2c
,
473 .spi_write
= snd_soc_16_8_spi_write
,
476 .addr_bits
= 16, .data_bits
= 16,
477 .write
= snd_soc_16_16_write
, .read
= snd_soc_16_16_read
,
478 .i2c_read
= snd_soc_16_16_read_i2c
,
479 .spi_write
= snd_soc_16_16_spi_write
,
484 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
486 * @codec: CODEC to configure.
487 * @addr_bits: Number of bits of register address data.
488 * @data_bits: Number of bits of data per register.
489 * @control: Control bus used.
491 * Register formats are frequently shared between many I2C and SPI
492 * devices. In order to promote code reuse the ASoC core provides
493 * some standard implementations of CODEC read and write operations
494 * which can be set up using this function.
496 * The caller is responsible for allocating and initialising the
499 * Note that at present this code cannot be used by CODECs with
500 * volatile registers.
502 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
503 int addr_bits
, int data_bits
,
504 enum snd_soc_control_type control
)
508 for (i
= 0; i
< ARRAY_SIZE(io_types
); i
++)
509 if (io_types
[i
].addr_bits
== addr_bits
&&
510 io_types
[i
].data_bits
== data_bits
)
512 if (i
== ARRAY_SIZE(io_types
)) {
514 "No I/O functions for %d bit address %d bit data\n",
515 addr_bits
, data_bits
);
519 codec
->write
= io_types
[i
].write
;
520 codec
->read
= io_types
[i
].read
;
521 codec
->bulk_write_raw
= snd_soc_hw_bulk_write_raw
;
528 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
529 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
531 if (io_types
[i
].i2c_read
)
532 codec
->hw_read
= io_types
[i
].i2c_read
;
534 codec
->control_data
= container_of(codec
->dev
,
540 if (io_types
[i
].spi_write
)
541 codec
->hw_write
= io_types
[i
].spi_write
;
543 codec
->control_data
= container_of(codec
->dev
,
551 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);
553 static bool snd_soc_set_cache_val(void *base
, unsigned int idx
,
554 unsigned int val
, unsigned int word_size
)
559 if (cache
[idx
] == val
)
566 if (cache
[idx
] == val
)
577 static unsigned int snd_soc_get_cache_val(const void *base
, unsigned int idx
,
578 unsigned int word_size
)
582 const u8
*cache
= base
;
586 const u16
*cache
= base
;
596 struct snd_soc_rbtree_node
{
601 } __attribute__ ((packed
));
603 struct snd_soc_rbtree_ctx
{
607 static struct snd_soc_rbtree_node
*snd_soc_rbtree_lookup(
608 struct rb_root
*root
, unsigned int reg
)
610 struct rb_node
*node
;
611 struct snd_soc_rbtree_node
*rbnode
;
613 node
= root
->rb_node
;
615 rbnode
= container_of(node
, struct snd_soc_rbtree_node
, node
);
616 if (rbnode
->reg
< reg
)
617 node
= node
->rb_left
;
618 else if (rbnode
->reg
> reg
)
619 node
= node
->rb_right
;
627 static int snd_soc_rbtree_insert(struct rb_root
*root
,
628 struct snd_soc_rbtree_node
*rbnode
)
630 struct rb_node
**new, *parent
;
631 struct snd_soc_rbtree_node
*rbnode_tmp
;
634 new = &root
->rb_node
;
636 rbnode_tmp
= container_of(*new, struct snd_soc_rbtree_node
,
639 if (rbnode_tmp
->reg
< rbnode
->reg
)
640 new = &((*new)->rb_left
);
641 else if (rbnode_tmp
->reg
> rbnode
->reg
)
642 new = &((*new)->rb_right
);
647 /* insert the node into the rbtree */
648 rb_link_node(&rbnode
->node
, parent
, new);
649 rb_insert_color(&rbnode
->node
, root
);
654 static int snd_soc_rbtree_cache_sync(struct snd_soc_codec
*codec
)
656 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
657 struct rb_node
*node
;
658 struct snd_soc_rbtree_node
*rbnode
;
662 rbtree_ctx
= codec
->reg_cache
;
663 for (node
= rb_first(&rbtree_ctx
->root
); node
; node
= rb_next(node
)) {
664 rbnode
= rb_entry(node
, struct snd_soc_rbtree_node
, node
);
665 if (rbnode
->value
== rbnode
->defval
)
667 WARN_ON(codec
->writable_register
&&
668 codec
->writable_register(codec
, rbnode
->reg
));
669 ret
= snd_soc_cache_read(codec
, rbnode
->reg
, &val
);
672 codec
->cache_bypass
= 1;
673 ret
= snd_soc_write(codec
, rbnode
->reg
, val
);
674 codec
->cache_bypass
= 0;
677 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
684 static int snd_soc_rbtree_cache_write(struct snd_soc_codec
*codec
,
685 unsigned int reg
, unsigned int value
)
687 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
688 struct snd_soc_rbtree_node
*rbnode
;
690 rbtree_ctx
= codec
->reg_cache
;
691 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
693 if (rbnode
->value
== value
)
695 rbnode
->value
= value
;
697 /* bail out early, no need to create the rbnode yet */
701 * for uninitialized registers whose value is changed
702 * from the default zero, create an rbnode and insert
705 rbnode
= kzalloc(sizeof *rbnode
, GFP_KERNEL
);
709 rbnode
->value
= value
;
710 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbnode
);
716 static int snd_soc_rbtree_cache_read(struct snd_soc_codec
*codec
,
717 unsigned int reg
, unsigned int *value
)
719 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
720 struct snd_soc_rbtree_node
*rbnode
;
722 rbtree_ctx
= codec
->reg_cache
;
723 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
725 *value
= rbnode
->value
;
727 /* uninitialized registers default to 0 */
734 static int snd_soc_rbtree_cache_exit(struct snd_soc_codec
*codec
)
736 struct rb_node
*next
;
737 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
738 struct snd_soc_rbtree_node
*rbtree_node
;
740 /* if we've already been called then just return */
741 rbtree_ctx
= codec
->reg_cache
;
745 /* free up the rbtree */
746 next
= rb_first(&rbtree_ctx
->root
);
748 rbtree_node
= rb_entry(next
, struct snd_soc_rbtree_node
, node
);
749 next
= rb_next(&rbtree_node
->node
);
750 rb_erase(&rbtree_node
->node
, &rbtree_ctx
->root
);
754 /* release the resources */
755 kfree(codec
->reg_cache
);
756 codec
->reg_cache
= NULL
;
761 static int snd_soc_rbtree_cache_init(struct snd_soc_codec
*codec
)
763 struct snd_soc_rbtree_node
*rbtree_node
;
764 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
766 unsigned int word_size
;
770 codec
->reg_cache
= kmalloc(sizeof *rbtree_ctx
, GFP_KERNEL
);
771 if (!codec
->reg_cache
)
774 rbtree_ctx
= codec
->reg_cache
;
775 rbtree_ctx
->root
= RB_ROOT
;
777 if (!codec
->reg_def_copy
)
781 * populate the rbtree with the initialized registers. All other
782 * registers will be inserted when they are first modified.
784 word_size
= codec
->driver
->reg_word_size
;
785 for (i
= 0; i
< codec
->driver
->reg_cache_size
; ++i
) {
786 val
= snd_soc_get_cache_val(codec
->reg_def_copy
, i
, word_size
);
789 rbtree_node
= kzalloc(sizeof *rbtree_node
, GFP_KERNEL
);
792 snd_soc_cache_exit(codec
);
795 rbtree_node
->reg
= i
;
796 rbtree_node
->value
= val
;
797 rbtree_node
->defval
= val
;
798 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbtree_node
);
804 #ifdef CONFIG_SND_SOC_CACHE_LZO
805 struct snd_soc_lzo_ctx
{
811 size_t decompressed_size
;
812 unsigned long *sync_bmp
;
816 #define LZO_BLOCK_NUM 8
817 static int snd_soc_lzo_block_count(void)
819 return LZO_BLOCK_NUM
;
822 static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx
*lzo_ctx
)
824 lzo_ctx
->wmem
= kmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
830 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx
*lzo_ctx
)
832 size_t compress_size
;
835 ret
= lzo1x_1_compress(lzo_ctx
->src
, lzo_ctx
->src_len
,
836 lzo_ctx
->dst
, &compress_size
, lzo_ctx
->wmem
);
837 if (ret
!= LZO_E_OK
|| compress_size
> lzo_ctx
->dst_len
)
839 lzo_ctx
->dst_len
= compress_size
;
843 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx
*lzo_ctx
)
848 dst_len
= lzo_ctx
->dst_len
;
849 ret
= lzo1x_decompress_safe(lzo_ctx
->src
, lzo_ctx
->src_len
,
850 lzo_ctx
->dst
, &dst_len
);
851 if (ret
!= LZO_E_OK
|| dst_len
!= lzo_ctx
->dst_len
)
856 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec
*codec
,
857 struct snd_soc_lzo_ctx
*lzo_ctx
)
861 lzo_ctx
->dst_len
= lzo1x_worst_compress(PAGE_SIZE
);
862 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
864 lzo_ctx
->dst_len
= 0;
868 ret
= snd_soc_lzo_compress(lzo_ctx
);
874 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec
*codec
,
875 struct snd_soc_lzo_ctx
*lzo_ctx
)
879 lzo_ctx
->dst_len
= lzo_ctx
->decompressed_size
;
880 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
882 lzo_ctx
->dst_len
= 0;
886 ret
= snd_soc_lzo_decompress(lzo_ctx
);
892 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec
*codec
,
895 const struct snd_soc_codec_driver
*codec_drv
;
897 codec_drv
= codec
->driver
;
898 return (reg
* codec_drv
->reg_word_size
) /
899 DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
902 static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec
*codec
,
905 const struct snd_soc_codec_driver
*codec_drv
;
907 codec_drv
= codec
->driver
;
908 return reg
% (DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count()) /
909 codec_drv
->reg_word_size
);
912 static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec
*codec
)
914 const struct snd_soc_codec_driver
*codec_drv
;
916 codec_drv
= codec
->driver
;
917 return DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
920 static int snd_soc_lzo_cache_sync(struct snd_soc_codec
*codec
)
922 struct snd_soc_lzo_ctx
**lzo_blocks
;
927 lzo_blocks
= codec
->reg_cache
;
928 for_each_set_bit(i
, lzo_blocks
[0]->sync_bmp
, lzo_blocks
[0]->sync_bmp_nbits
) {
929 WARN_ON(codec
->writable_register
&&
930 codec
->writable_register(codec
, i
));
931 ret
= snd_soc_cache_read(codec
, i
, &val
);
934 codec
->cache_bypass
= 1;
935 ret
= snd_soc_write(codec
, i
, val
);
936 codec
->cache_bypass
= 0;
939 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
946 static int snd_soc_lzo_cache_write(struct snd_soc_codec
*codec
,
947 unsigned int reg
, unsigned int value
)
949 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
950 int ret
, blkindex
, blkpos
;
951 size_t blksize
, tmp_dst_len
;
954 /* index of the compressed lzo block */
955 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
956 /* register index within the decompressed block */
957 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
958 /* size of the compressed block */
959 blksize
= snd_soc_lzo_get_blksize(codec
);
960 lzo_blocks
= codec
->reg_cache
;
961 lzo_block
= lzo_blocks
[blkindex
];
963 /* save the pointer and length of the compressed block */
964 tmp_dst
= lzo_block
->dst
;
965 tmp_dst_len
= lzo_block
->dst_len
;
967 /* prepare the source to be the compressed block */
968 lzo_block
->src
= lzo_block
->dst
;
969 lzo_block
->src_len
= lzo_block
->dst_len
;
971 /* decompress the block */
972 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
974 kfree(lzo_block
->dst
);
978 /* write the new value to the cache */
979 if (snd_soc_set_cache_val(lzo_block
->dst
, blkpos
, value
,
980 codec
->driver
->reg_word_size
)) {
981 kfree(lzo_block
->dst
);
985 /* prepare the source to be the decompressed block */
986 lzo_block
->src
= lzo_block
->dst
;
987 lzo_block
->src_len
= lzo_block
->dst_len
;
989 /* compress the block */
990 ret
= snd_soc_lzo_compress_cache_block(codec
, lzo_block
);
992 kfree(lzo_block
->dst
);
993 kfree(lzo_block
->src
);
997 /* set the bit so we know we have to sync this register */
998 set_bit(reg
, lzo_block
->sync_bmp
);
1000 kfree(lzo_block
->src
);
1003 lzo_block
->dst
= tmp_dst
;
1004 lzo_block
->dst_len
= tmp_dst_len
;
1008 static int snd_soc_lzo_cache_read(struct snd_soc_codec
*codec
,
1009 unsigned int reg
, unsigned int *value
)
1011 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
1012 int ret
, blkindex
, blkpos
;
1013 size_t blksize
, tmp_dst_len
;
1017 /* index of the compressed lzo block */
1018 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
1019 /* register index within the decompressed block */
1020 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
1021 /* size of the compressed block */
1022 blksize
= snd_soc_lzo_get_blksize(codec
);
1023 lzo_blocks
= codec
->reg_cache
;
1024 lzo_block
= lzo_blocks
[blkindex
];
1026 /* save the pointer and length of the compressed block */
1027 tmp_dst
= lzo_block
->dst
;
1028 tmp_dst_len
= lzo_block
->dst_len
;
1030 /* prepare the source to be the compressed block */
1031 lzo_block
->src
= lzo_block
->dst
;
1032 lzo_block
->src_len
= lzo_block
->dst_len
;
1034 /* decompress the block */
1035 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
1037 /* fetch the value from the cache */
1038 *value
= snd_soc_get_cache_val(lzo_block
->dst
, blkpos
,
1039 codec
->driver
->reg_word_size
);
1041 kfree(lzo_block
->dst
);
1042 /* restore the pointer and length of the compressed block */
1043 lzo_block
->dst
= tmp_dst
;
1044 lzo_block
->dst_len
= tmp_dst_len
;
1048 static int snd_soc_lzo_cache_exit(struct snd_soc_codec
*codec
)
1050 struct snd_soc_lzo_ctx
**lzo_blocks
;
1053 lzo_blocks
= codec
->reg_cache
;
1057 blkcount
= snd_soc_lzo_block_count();
1059 * the pointer to the bitmap used for syncing the cache
1060 * is shared amongst all lzo_blocks. Ensure it is freed
1064 kfree(lzo_blocks
[0]->sync_bmp
);
1065 for (i
= 0; i
< blkcount
; ++i
) {
1066 if (lzo_blocks
[i
]) {
1067 kfree(lzo_blocks
[i
]->wmem
);
1068 kfree(lzo_blocks
[i
]->dst
);
1070 /* each lzo_block is a pointer returned by kmalloc or NULL */
1071 kfree(lzo_blocks
[i
]);
1074 codec
->reg_cache
= NULL
;
1078 static int snd_soc_lzo_cache_init(struct snd_soc_codec
*codec
)
1080 struct snd_soc_lzo_ctx
**lzo_blocks
;
1082 const struct snd_soc_codec_driver
*codec_drv
;
1083 int ret
, tofree
, i
, blksize
, blkcount
;
1084 const char *p
, *end
;
1085 unsigned long *sync_bmp
;
1088 codec_drv
= codec
->driver
;
1091 * If we have not been given a default register cache
1092 * then allocate a dummy zero-ed out region, compress it
1093 * and remember to free it afterwards.
1096 if (!codec
->reg_def_copy
)
1099 if (!codec
->reg_def_copy
) {
1100 codec
->reg_def_copy
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1101 if (!codec
->reg_def_copy
)
1105 blkcount
= snd_soc_lzo_block_count();
1106 codec
->reg_cache
= kzalloc(blkcount
* sizeof *lzo_blocks
,
1108 if (!codec
->reg_cache
) {
1112 lzo_blocks
= codec
->reg_cache
;
1115 * allocate a bitmap to be used when syncing the cache with
1116 * the hardware. Each time a register is modified, the corresponding
1117 * bit is set in the bitmap, so we know that we have to sync
1120 bmp_size
= codec_drv
->reg_cache_size
;
1121 sync_bmp
= kmalloc(BITS_TO_LONGS(bmp_size
) * sizeof(long),
1127 bitmap_zero(sync_bmp
, bmp_size
);
1129 /* allocate the lzo blocks and initialize them */
1130 for (i
= 0; i
< blkcount
; ++i
) {
1131 lzo_blocks
[i
] = kzalloc(sizeof **lzo_blocks
,
1133 if (!lzo_blocks
[i
]) {
1138 lzo_blocks
[i
]->sync_bmp
= sync_bmp
;
1139 lzo_blocks
[i
]->sync_bmp_nbits
= bmp_size
;
1140 /* alloc the working space for the compressed block */
1141 ret
= snd_soc_lzo_prepare(lzo_blocks
[i
]);
1146 blksize
= snd_soc_lzo_get_blksize(codec
);
1147 p
= codec
->reg_def_copy
;
1148 end
= codec
->reg_def_copy
+ codec
->reg_size
;
1149 /* compress the register map and fill the lzo blocks */
1150 for (i
= 0; i
< blkcount
; ++i
, p
+= blksize
) {
1151 lzo_blocks
[i
]->src
= p
;
1152 if (p
+ blksize
> end
)
1153 lzo_blocks
[i
]->src_len
= end
- p
;
1155 lzo_blocks
[i
]->src_len
= blksize
;
1156 ret
= snd_soc_lzo_compress_cache_block(codec
,
1160 lzo_blocks
[i
]->decompressed_size
=
1161 lzo_blocks
[i
]->src_len
;
1165 kfree(codec
->reg_def_copy
);
1166 codec
->reg_def_copy
= NULL
;
1170 snd_soc_cache_exit(codec
);
1173 kfree(codec
->reg_def_copy
);
1174 codec
->reg_def_copy
= NULL
;
1180 static int snd_soc_flat_cache_sync(struct snd_soc_codec
*codec
)
1184 const struct snd_soc_codec_driver
*codec_drv
;
1187 codec_drv
= codec
->driver
;
1188 for (i
= 0; i
< codec_drv
->reg_cache_size
; ++i
) {
1189 WARN_ON(codec
->writable_register
&&
1190 codec
->writable_register(codec
, i
));
1191 ret
= snd_soc_cache_read(codec
, i
, &val
);
1194 if (codec
->reg_def_copy
)
1195 if (snd_soc_get_cache_val(codec
->reg_def_copy
,
1196 i
, codec_drv
->reg_word_size
) == val
)
1198 ret
= snd_soc_write(codec
, i
, val
);
1201 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
1207 static int snd_soc_flat_cache_write(struct snd_soc_codec
*codec
,
1208 unsigned int reg
, unsigned int value
)
1210 snd_soc_set_cache_val(codec
->reg_cache
, reg
, value
,
1211 codec
->driver
->reg_word_size
);
1215 static int snd_soc_flat_cache_read(struct snd_soc_codec
*codec
,
1216 unsigned int reg
, unsigned int *value
)
1218 *value
= snd_soc_get_cache_val(codec
->reg_cache
, reg
,
1219 codec
->driver
->reg_word_size
);
1223 static int snd_soc_flat_cache_exit(struct snd_soc_codec
*codec
)
1225 if (!codec
->reg_cache
)
1227 kfree(codec
->reg_cache
);
1228 codec
->reg_cache
= NULL
;
1232 static int snd_soc_flat_cache_init(struct snd_soc_codec
*codec
)
1234 const struct snd_soc_codec_driver
*codec_drv
;
1236 codec_drv
= codec
->driver
;
1238 if (codec
->reg_def_copy
)
1239 codec
->reg_cache
= kmemdup(codec
->reg_def_copy
,
1240 codec
->reg_size
, GFP_KERNEL
);
1242 codec
->reg_cache
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1243 if (!codec
->reg_cache
)
1249 /* an array of all supported compression types */
1250 static const struct snd_soc_cache_ops cache_types
[] = {
1251 /* Flat *must* be the first entry for fallback */
1253 .id
= SND_SOC_FLAT_COMPRESSION
,
1255 .init
= snd_soc_flat_cache_init
,
1256 .exit
= snd_soc_flat_cache_exit
,
1257 .read
= snd_soc_flat_cache_read
,
1258 .write
= snd_soc_flat_cache_write
,
1259 .sync
= snd_soc_flat_cache_sync
1261 #ifdef CONFIG_SND_SOC_CACHE_LZO
1263 .id
= SND_SOC_LZO_COMPRESSION
,
1265 .init
= snd_soc_lzo_cache_init
,
1266 .exit
= snd_soc_lzo_cache_exit
,
1267 .read
= snd_soc_lzo_cache_read
,
1268 .write
= snd_soc_lzo_cache_write
,
1269 .sync
= snd_soc_lzo_cache_sync
1273 .id
= SND_SOC_RBTREE_COMPRESSION
,
1275 .init
= snd_soc_rbtree_cache_init
,
1276 .exit
= snd_soc_rbtree_cache_exit
,
1277 .read
= snd_soc_rbtree_cache_read
,
1278 .write
= snd_soc_rbtree_cache_write
,
1279 .sync
= snd_soc_rbtree_cache_sync
1283 int snd_soc_cache_init(struct snd_soc_codec
*codec
)
1287 for (i
= 0; i
< ARRAY_SIZE(cache_types
); ++i
)
1288 if (cache_types
[i
].id
== codec
->compress_type
)
1291 /* Fall back to flat compression */
1292 if (i
== ARRAY_SIZE(cache_types
)) {
1293 dev_warn(codec
->dev
, "Could not match compress type: %d\n",
1294 codec
->compress_type
);
1298 mutex_init(&codec
->cache_rw_mutex
);
1299 codec
->cache_ops
= &cache_types
[i
];
1301 if (codec
->cache_ops
->init
) {
1302 if (codec
->cache_ops
->name
)
1303 dev_dbg(codec
->dev
, "Initializing %s cache for %s codec\n",
1304 codec
->cache_ops
->name
, codec
->name
);
1305 return codec
->cache_ops
->init(codec
);
1311 * NOTE: keep in mind that this function might be called
1314 int snd_soc_cache_exit(struct snd_soc_codec
*codec
)
1316 if (codec
->cache_ops
&& codec
->cache_ops
->exit
) {
1317 if (codec
->cache_ops
->name
)
1318 dev_dbg(codec
->dev
, "Destroying %s cache for %s codec\n",
1319 codec
->cache_ops
->name
, codec
->name
);
1320 return codec
->cache_ops
->exit(codec
);
1326 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1328 * @codec: CODEC to configure.
1329 * @reg: The register index.
1330 * @value: The value to be returned.
1332 int snd_soc_cache_read(struct snd_soc_codec
*codec
,
1333 unsigned int reg
, unsigned int *value
)
1337 mutex_lock(&codec
->cache_rw_mutex
);
1339 if (value
&& codec
->cache_ops
&& codec
->cache_ops
->read
) {
1340 ret
= codec
->cache_ops
->read(codec
, reg
, value
);
1341 mutex_unlock(&codec
->cache_rw_mutex
);
1345 mutex_unlock(&codec
->cache_rw_mutex
);
1348 EXPORT_SYMBOL_GPL(snd_soc_cache_read
);
1351 * snd_soc_cache_write: Set the value of a given register in the cache.
1353 * @codec: CODEC to configure.
1354 * @reg: The register index.
1355 * @value: The new register value.
1357 int snd_soc_cache_write(struct snd_soc_codec
*codec
,
1358 unsigned int reg
, unsigned int value
)
1362 mutex_lock(&codec
->cache_rw_mutex
);
1364 if (codec
->cache_ops
&& codec
->cache_ops
->write
) {
1365 ret
= codec
->cache_ops
->write(codec
, reg
, value
);
1366 mutex_unlock(&codec
->cache_rw_mutex
);
1370 mutex_unlock(&codec
->cache_rw_mutex
);
1373 EXPORT_SYMBOL_GPL(snd_soc_cache_write
);
1376 * snd_soc_cache_sync: Sync the register cache with the hardware.
1378 * @codec: CODEC to configure.
1380 * Any registers that should not be synced should be marked as
1381 * volatile. In general drivers can choose not to use the provided
1382 * syncing functionality if they so require.
1384 int snd_soc_cache_sync(struct snd_soc_codec
*codec
)
1389 if (!codec
->cache_sync
) {
1393 if (!codec
->cache_ops
|| !codec
->cache_ops
->sync
)
1396 if (codec
->cache_ops
->name
)
1397 name
= codec
->cache_ops
->name
;
1401 if (codec
->cache_ops
->name
)
1402 dev_dbg(codec
->dev
, "Syncing %s cache for %s codec\n",
1403 codec
->cache_ops
->name
, codec
->name
);
1404 trace_snd_soc_cache_sync(codec
, name
, "start");
1405 ret
= codec
->cache_ops
->sync(codec
);
1407 codec
->cache_sync
= 0;
1408 trace_snd_soc_cache_sync(codec
, name
, "end");
1411 EXPORT_SYMBOL_GPL(snd_soc_cache_sync
);
1413 static int snd_soc_get_reg_access_index(struct snd_soc_codec
*codec
,
1416 const struct snd_soc_codec_driver
*codec_drv
;
1417 unsigned int min
, max
, index
;
1419 codec_drv
= codec
->driver
;
1421 max
= codec_drv
->reg_access_size
- 1;
1423 index
= (min
+ max
) / 2;
1424 if (codec_drv
->reg_access_default
[index
].reg
== reg
)
1426 if (codec_drv
->reg_access_default
[index
].reg
< reg
)
1430 } while (min
<= max
);
1434 int snd_soc_default_volatile_register(struct snd_soc_codec
*codec
,
1439 if (reg
>= codec
->driver
->reg_cache_size
)
1441 index
= snd_soc_get_reg_access_index(codec
, reg
);
1444 return codec
->driver
->reg_access_default
[index
].vol
;
1446 EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register
);
1448 int snd_soc_default_readable_register(struct snd_soc_codec
*codec
,
1453 if (reg
>= codec
->driver
->reg_cache_size
)
1455 index
= snd_soc_get_reg_access_index(codec
, reg
);
1458 return codec
->driver
->reg_access_default
[index
].read
;
1460 EXPORT_SYMBOL_GPL(snd_soc_default_readable_register
);
1462 int snd_soc_default_writable_register(struct snd_soc_codec
*codec
,
1467 if (reg
>= codec
->driver
->reg_cache_size
)
1469 index
= snd_soc_get_reg_access_index(codec
, reg
);
1472 return codec
->driver
->reg_access_default
[index
].write
;
1474 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register
);