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 static unsigned int snd_soc_4_12_read(struct snd_soc_codec
*codec
,
29 if (reg
>= codec
->driver
->reg_cache_size
||
30 snd_soc_codec_volatile_register(codec
, reg
) ||
31 codec
->cache_bypass
) {
32 if (codec
->cache_only
)
35 BUG_ON(!codec
->hw_read
);
36 return codec
->hw_read(codec
, reg
);
39 ret
= snd_soc_cache_read(codec
, reg
, &val
);
45 static int snd_soc_4_12_write(struct snd_soc_codec
*codec
, unsigned int reg
,
51 data
[0] = (reg
<< 4) | ((value
>> 8) & 0x000f);
52 data
[1] = value
& 0x00ff;
54 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
55 reg
< codec
->driver
->reg_cache_size
&&
56 !codec
->cache_bypass
) {
57 ret
= snd_soc_cache_write(codec
, reg
, value
);
62 if (codec
->cache_only
) {
63 codec
->cache_sync
= 1;
67 ret
= codec
->hw_write(codec
->control_data
, data
, 2);
76 #if defined(CONFIG_SPI_MASTER)
77 static int snd_soc_4_12_spi_write(void *control_data
, const char *data
,
80 struct spi_device
*spi
= control_data
;
81 struct spi_transfer t
;
92 memset(&t
, 0, sizeof t
);
97 spi_message_add_tail(&t
, &m
);
103 #define snd_soc_4_12_spi_write NULL
106 static unsigned int snd_soc_7_9_read(struct snd_soc_codec
*codec
,
112 if (reg
>= codec
->driver
->reg_cache_size
||
113 snd_soc_codec_volatile_register(codec
, reg
) ||
114 codec
->cache_bypass
) {
115 if (codec
->cache_only
)
118 BUG_ON(!codec
->hw_read
);
119 return codec
->hw_read(codec
, reg
);
122 ret
= snd_soc_cache_read(codec
, reg
, &val
);
128 static int snd_soc_7_9_write(struct snd_soc_codec
*codec
, unsigned int reg
,
134 data
[0] = (reg
<< 1) | ((value
>> 8) & 0x0001);
135 data
[1] = value
& 0x00ff;
137 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
138 reg
< codec
->driver
->reg_cache_size
&&
139 !codec
->cache_bypass
) {
140 ret
= snd_soc_cache_write(codec
, reg
, value
);
145 if (codec
->cache_only
) {
146 codec
->cache_sync
= 1;
150 ret
= codec
->hw_write(codec
->control_data
, data
, 2);
159 #if defined(CONFIG_SPI_MASTER)
160 static int snd_soc_7_9_spi_write(void *control_data
, const char *data
,
163 struct spi_device
*spi
= control_data
;
164 struct spi_transfer t
;
165 struct spi_message m
;
174 spi_message_init(&m
);
175 memset(&t
, 0, sizeof t
);
180 spi_message_add_tail(&t
, &m
);
186 #define snd_soc_7_9_spi_write NULL
189 static int snd_soc_8_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
197 data
[1] = value
& 0xff;
199 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
200 reg
< codec
->driver
->reg_cache_size
&&
201 !codec
->cache_bypass
) {
202 ret
= snd_soc_cache_write(codec
, reg
, value
);
207 if (codec
->cache_only
) {
208 codec
->cache_sync
= 1;
212 if (codec
->hw_write(codec
->control_data
, data
, 2) == 2)
218 static unsigned int snd_soc_8_8_read(struct snd_soc_codec
*codec
,
225 if (reg
>= codec
->driver
->reg_cache_size
||
226 snd_soc_codec_volatile_register(codec
, reg
) ||
227 codec
->cache_bypass
) {
228 if (codec
->cache_only
)
231 BUG_ON(!codec
->hw_read
);
232 return codec
->hw_read(codec
, reg
);
235 ret
= snd_soc_cache_read(codec
, reg
, &val
);
241 #if defined(CONFIG_SPI_MASTER)
242 static int snd_soc_8_8_spi_write(void *control_data
, const char *data
,
245 struct spi_device
*spi
= control_data
;
246 struct spi_transfer t
;
247 struct spi_message m
;
256 spi_message_init(&m
);
257 memset(&t
, 0, sizeof t
);
262 spi_message_add_tail(&t
, &m
);
268 #define snd_soc_8_8_spi_write NULL
271 static int snd_soc_8_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
278 data
[1] = (value
>> 8) & 0xff;
279 data
[2] = value
& 0xff;
281 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
282 reg
< codec
->driver
->reg_cache_size
&&
283 !codec
->cache_bypass
) {
284 ret
= snd_soc_cache_write(codec
, reg
, value
);
289 if (codec
->cache_only
) {
290 codec
->cache_sync
= 1;
294 if (codec
->hw_write(codec
->control_data
, data
, 3) == 3)
300 static unsigned int snd_soc_8_16_read(struct snd_soc_codec
*codec
,
306 if (reg
>= codec
->driver
->reg_cache_size
||
307 snd_soc_codec_volatile_register(codec
, reg
) ||
308 codec
->cache_bypass
) {
309 if (codec
->cache_only
)
312 BUG_ON(!codec
->hw_read
);
313 return codec
->hw_read(codec
, reg
);
316 ret
= snd_soc_cache_read(codec
, reg
, &val
);
322 #if defined(CONFIG_SPI_MASTER)
323 static int snd_soc_8_16_spi_write(void *control_data
, const char *data
,
326 struct spi_device
*spi
= control_data
;
327 struct spi_transfer t
;
328 struct spi_message m
;
338 spi_message_init(&m
);
339 memset(&t
, 0, sizeof t
);
344 spi_message_add_tail(&t
, &m
);
350 #define snd_soc_8_16_spi_write NULL
353 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
354 static unsigned int do_i2c_read(struct snd_soc_codec
*codec
,
355 void *reg
, int reglen
,
356 void *data
, int datalen
)
358 struct i2c_msg xfer
[2];
360 struct i2c_client
*client
= codec
->control_data
;
363 xfer
[0].addr
= client
->addr
;
365 xfer
[0].len
= reglen
;
369 xfer
[1].addr
= client
->addr
;
370 xfer
[1].flags
= I2C_M_RD
;
371 xfer
[1].len
= datalen
;
374 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
375 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
385 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
386 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec
*codec
,
393 ret
= do_i2c_read(codec
, ®
, 1, &data
, 1);
399 #define snd_soc_8_8_read_i2c NULL
402 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
403 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec
*codec
,
410 ret
= do_i2c_read(codec
, ®
, 1, &data
, 2);
413 return (data
>> 8) | ((data
& 0xff) << 8);
416 #define snd_soc_8_16_read_i2c NULL
419 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
420 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec
*codec
,
427 ret
= do_i2c_read(codec
, ®
, 2, &data
, 1);
433 #define snd_soc_16_8_read_i2c NULL
436 static unsigned int snd_soc_16_8_read(struct snd_soc_codec
*codec
,
443 if (reg
>= codec
->driver
->reg_cache_size
||
444 snd_soc_codec_volatile_register(codec
, reg
) ||
445 codec
->cache_bypass
) {
446 if (codec
->cache_only
)
449 BUG_ON(!codec
->hw_read
);
450 return codec
->hw_read(codec
, reg
);
453 ret
= snd_soc_cache_read(codec
, reg
, &val
);
459 static int snd_soc_16_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
465 data
[0] = (reg
>> 8) & 0xff;
466 data
[1] = reg
& 0xff;
470 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
471 reg
< codec
->driver
->reg_cache_size
&&
472 !codec
->cache_bypass
) {
473 ret
= snd_soc_cache_write(codec
, reg
, value
);
478 if (codec
->cache_only
) {
479 codec
->cache_sync
= 1;
483 ret
= codec
->hw_write(codec
->control_data
, data
, 3);
492 #if defined(CONFIG_SPI_MASTER)
493 static int snd_soc_16_8_spi_write(void *control_data
, const char *data
,
496 struct spi_device
*spi
= control_data
;
497 struct spi_transfer t
;
498 struct spi_message m
;
508 spi_message_init(&m
);
509 memset(&t
, 0, sizeof t
);
514 spi_message_add_tail(&t
, &m
);
520 #define snd_soc_16_8_spi_write NULL
523 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
524 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec
*codec
,
527 u16 reg
= cpu_to_be16(r
);
531 ret
= do_i2c_read(codec
, ®
, 2, &data
, 2);
534 return be16_to_cpu(data
);
537 #define snd_soc_16_16_read_i2c NULL
540 static unsigned int snd_soc_16_16_read(struct snd_soc_codec
*codec
,
546 if (reg
>= codec
->driver
->reg_cache_size
||
547 snd_soc_codec_volatile_register(codec
, reg
) ||
548 codec
->cache_bypass
) {
549 if (codec
->cache_only
)
552 BUG_ON(!codec
->hw_read
);
553 return codec
->hw_read(codec
, reg
);
556 ret
= snd_soc_cache_read(codec
, reg
, &val
);
563 static int snd_soc_16_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
569 data
[0] = (reg
>> 8) & 0xff;
570 data
[1] = reg
& 0xff;
571 data
[2] = (value
>> 8) & 0xff;
572 data
[3] = value
& 0xff;
574 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
575 reg
< codec
->driver
->reg_cache_size
&&
576 !codec
->cache_bypass
) {
577 ret
= snd_soc_cache_write(codec
, reg
, value
);
582 if (codec
->cache_only
) {
583 codec
->cache_sync
= 1;
587 ret
= codec
->hw_write(codec
->control_data
, data
, 4);
596 #if defined(CONFIG_SPI_MASTER)
597 static int snd_soc_16_16_spi_write(void *control_data
, const char *data
,
600 struct spi_device
*spi
= control_data
;
601 struct spi_transfer t
;
602 struct spi_message m
;
613 spi_message_init(&m
);
614 memset(&t
, 0, sizeof t
);
619 spi_message_add_tail(&t
, &m
);
625 #define snd_soc_16_16_spi_write NULL
628 /* Primitive bulk write support for soc-cache. The data pointed to by `data' needs
629 * to already be in the form the hardware expects including any leading register specific
630 * data. Any data written through this function will not go through the cache as it
631 * only handles writing to volatile or out of bounds registers.
633 static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec
*codec
, unsigned int reg
,
634 const void *data
, size_t len
)
638 /* Ensure that the base register is volatile. Subsequently
639 * any other register that is touched by this routine should be
640 * volatile as well to ensure that we don't get out of sync with
643 if (!snd_soc_codec_volatile_register(codec
, reg
)
644 && reg
< codec
->driver
->reg_cache_size
)
647 switch (codec
->control_type
) {
649 ret
= i2c_master_send(codec
->control_data
, data
, len
);
652 ret
= do_spi_write(codec
->control_data
, data
, len
);
669 int (*write
)(struct snd_soc_codec
*codec
, unsigned int, unsigned int);
670 int (*spi_write
)(void *, const char *, int);
671 unsigned int (*read
)(struct snd_soc_codec
*, unsigned int);
672 unsigned int (*i2c_read
)(struct snd_soc_codec
*, unsigned int);
675 .addr_bits
= 4, .data_bits
= 12,
676 .write
= snd_soc_4_12_write
, .read
= snd_soc_4_12_read
,
677 .spi_write
= snd_soc_4_12_spi_write
,
680 .addr_bits
= 7, .data_bits
= 9,
681 .write
= snd_soc_7_9_write
, .read
= snd_soc_7_9_read
,
682 .spi_write
= snd_soc_7_9_spi_write
,
685 .addr_bits
= 8, .data_bits
= 8,
686 .write
= snd_soc_8_8_write
, .read
= snd_soc_8_8_read
,
687 .i2c_read
= snd_soc_8_8_read_i2c
,
688 .spi_write
= snd_soc_8_8_spi_write
,
691 .addr_bits
= 8, .data_bits
= 16,
692 .write
= snd_soc_8_16_write
, .read
= snd_soc_8_16_read
,
693 .i2c_read
= snd_soc_8_16_read_i2c
,
694 .spi_write
= snd_soc_8_16_spi_write
,
697 .addr_bits
= 16, .data_bits
= 8,
698 .write
= snd_soc_16_8_write
, .read
= snd_soc_16_8_read
,
699 .i2c_read
= snd_soc_16_8_read_i2c
,
700 .spi_write
= snd_soc_16_8_spi_write
,
703 .addr_bits
= 16, .data_bits
= 16,
704 .write
= snd_soc_16_16_write
, .read
= snd_soc_16_16_read
,
705 .i2c_read
= snd_soc_16_16_read_i2c
,
706 .spi_write
= snd_soc_16_16_spi_write
,
711 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
713 * @codec: CODEC to configure.
714 * @type: Type of cache.
715 * @addr_bits: Number of bits of register address data.
716 * @data_bits: Number of bits of data per register.
717 * @control: Control bus used.
719 * Register formats are frequently shared between many I2C and SPI
720 * devices. In order to promote code reuse the ASoC core provides
721 * some standard implementations of CODEC read and write operations
722 * which can be set up using this function.
724 * The caller is responsible for allocating and initialising the
727 * Note that at present this code cannot be used by CODECs with
728 * volatile registers.
730 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
731 int addr_bits
, int data_bits
,
732 enum snd_soc_control_type control
)
736 for (i
= 0; i
< ARRAY_SIZE(io_types
); i
++)
737 if (io_types
[i
].addr_bits
== addr_bits
&&
738 io_types
[i
].data_bits
== data_bits
)
740 if (i
== ARRAY_SIZE(io_types
)) {
742 "No I/O functions for %d bit address %d bit data\n",
743 addr_bits
, data_bits
);
747 codec
->write
= io_types
[i
].write
;
748 codec
->read
= io_types
[i
].read
;
749 codec
->bulk_write_raw
= snd_soc_hw_bulk_write_raw
;
756 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
757 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
759 if (io_types
[i
].i2c_read
)
760 codec
->hw_read
= io_types
[i
].i2c_read
;
762 codec
->control_data
= container_of(codec
->dev
,
768 if (io_types
[i
].spi_write
)
769 codec
->hw_write
= io_types
[i
].spi_write
;
771 codec
->control_data
= container_of(codec
->dev
,
779 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);
781 static bool snd_soc_set_cache_val(void *base
, unsigned int idx
,
782 unsigned int val
, unsigned int word_size
)
787 if (cache
[idx
] == val
)
794 if (cache
[idx
] == val
)
805 static unsigned int snd_soc_get_cache_val(const void *base
, unsigned int idx
,
806 unsigned int word_size
)
810 const u8
*cache
= base
;
814 const u16
*cache
= base
;
824 struct snd_soc_rbtree_node
{
829 } __attribute__ ((packed
));
831 struct snd_soc_rbtree_ctx
{
835 static struct snd_soc_rbtree_node
*snd_soc_rbtree_lookup(
836 struct rb_root
*root
, unsigned int reg
)
838 struct rb_node
*node
;
839 struct snd_soc_rbtree_node
*rbnode
;
841 node
= root
->rb_node
;
843 rbnode
= container_of(node
, struct snd_soc_rbtree_node
, node
);
844 if (rbnode
->reg
< reg
)
845 node
= node
->rb_left
;
846 else if (rbnode
->reg
> reg
)
847 node
= node
->rb_right
;
855 static int snd_soc_rbtree_insert(struct rb_root
*root
,
856 struct snd_soc_rbtree_node
*rbnode
)
858 struct rb_node
**new, *parent
;
859 struct snd_soc_rbtree_node
*rbnode_tmp
;
862 new = &root
->rb_node
;
864 rbnode_tmp
= container_of(*new, struct snd_soc_rbtree_node
,
867 if (rbnode_tmp
->reg
< rbnode
->reg
)
868 new = &((*new)->rb_left
);
869 else if (rbnode_tmp
->reg
> rbnode
->reg
)
870 new = &((*new)->rb_right
);
875 /* insert the node into the rbtree */
876 rb_link_node(&rbnode
->node
, parent
, new);
877 rb_insert_color(&rbnode
->node
, root
);
882 static int snd_soc_rbtree_cache_sync(struct snd_soc_codec
*codec
)
884 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
885 struct rb_node
*node
;
886 struct snd_soc_rbtree_node
*rbnode
;
890 rbtree_ctx
= codec
->reg_cache
;
891 for (node
= rb_first(&rbtree_ctx
->root
); node
; node
= rb_next(node
)) {
892 rbnode
= rb_entry(node
, struct snd_soc_rbtree_node
, node
);
893 if (rbnode
->value
== rbnode
->defval
)
895 ret
= snd_soc_cache_read(codec
, rbnode
->reg
, &val
);
898 codec
->cache_bypass
= 1;
899 ret
= snd_soc_write(codec
, rbnode
->reg
, val
);
900 codec
->cache_bypass
= 0;
903 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
910 static int snd_soc_rbtree_cache_write(struct snd_soc_codec
*codec
,
911 unsigned int reg
, unsigned int value
)
913 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
914 struct snd_soc_rbtree_node
*rbnode
;
916 rbtree_ctx
= codec
->reg_cache
;
917 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
919 if (rbnode
->value
== value
)
921 rbnode
->value
= value
;
923 /* bail out early, no need to create the rbnode yet */
927 * for uninitialized registers whose value is changed
928 * from the default zero, create an rbnode and insert
931 rbnode
= kzalloc(sizeof *rbnode
, GFP_KERNEL
);
935 rbnode
->value
= value
;
936 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbnode
);
942 static int snd_soc_rbtree_cache_read(struct snd_soc_codec
*codec
,
943 unsigned int reg
, unsigned int *value
)
945 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
946 struct snd_soc_rbtree_node
*rbnode
;
948 rbtree_ctx
= codec
->reg_cache
;
949 rbnode
= snd_soc_rbtree_lookup(&rbtree_ctx
->root
, reg
);
951 *value
= rbnode
->value
;
953 /* uninitialized registers default to 0 */
960 static int snd_soc_rbtree_cache_exit(struct snd_soc_codec
*codec
)
962 struct rb_node
*next
;
963 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
964 struct snd_soc_rbtree_node
*rbtree_node
;
966 /* if we've already been called then just return */
967 rbtree_ctx
= codec
->reg_cache
;
971 /* free up the rbtree */
972 next
= rb_first(&rbtree_ctx
->root
);
974 rbtree_node
= rb_entry(next
, struct snd_soc_rbtree_node
, node
);
975 next
= rb_next(&rbtree_node
->node
);
976 rb_erase(&rbtree_node
->node
, &rbtree_ctx
->root
);
980 /* release the resources */
981 kfree(codec
->reg_cache
);
982 codec
->reg_cache
= NULL
;
987 static int snd_soc_rbtree_cache_init(struct snd_soc_codec
*codec
)
989 struct snd_soc_rbtree_node
*rbtree_node
;
990 struct snd_soc_rbtree_ctx
*rbtree_ctx
;
992 unsigned int word_size
;
996 codec
->reg_cache
= kmalloc(sizeof *rbtree_ctx
, GFP_KERNEL
);
997 if (!codec
->reg_cache
)
1000 rbtree_ctx
= codec
->reg_cache
;
1001 rbtree_ctx
->root
= RB_ROOT
;
1003 if (!codec
->reg_def_copy
)
1007 * populate the rbtree with the initialized registers. All other
1008 * registers will be inserted when they are first modified.
1010 word_size
= codec
->driver
->reg_word_size
;
1011 for (i
= 0; i
< codec
->driver
->reg_cache_size
; ++i
) {
1012 val
= snd_soc_get_cache_val(codec
->reg_def_copy
, i
, word_size
);
1015 rbtree_node
= kzalloc(sizeof *rbtree_node
, GFP_KERNEL
);
1018 snd_soc_cache_exit(codec
);
1021 rbtree_node
->reg
= i
;
1022 rbtree_node
->value
= val
;
1023 rbtree_node
->defval
= val
;
1024 snd_soc_rbtree_insert(&rbtree_ctx
->root
, rbtree_node
);
1030 #ifdef CONFIG_SND_SOC_CACHE_LZO
1031 struct snd_soc_lzo_ctx
{
1037 size_t decompressed_size
;
1038 unsigned long *sync_bmp
;
1042 #define LZO_BLOCK_NUM 8
1043 static int snd_soc_lzo_block_count(void)
1045 return LZO_BLOCK_NUM
;
1048 static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx
*lzo_ctx
)
1050 lzo_ctx
->wmem
= kmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
1056 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx
*lzo_ctx
)
1058 size_t compress_size
;
1061 ret
= lzo1x_1_compress(lzo_ctx
->src
, lzo_ctx
->src_len
,
1062 lzo_ctx
->dst
, &compress_size
, lzo_ctx
->wmem
);
1063 if (ret
!= LZO_E_OK
|| compress_size
> lzo_ctx
->dst_len
)
1065 lzo_ctx
->dst_len
= compress_size
;
1069 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx
*lzo_ctx
)
1074 dst_len
= lzo_ctx
->dst_len
;
1075 ret
= lzo1x_decompress_safe(lzo_ctx
->src
, lzo_ctx
->src_len
,
1076 lzo_ctx
->dst
, &dst_len
);
1077 if (ret
!= LZO_E_OK
|| dst_len
!= lzo_ctx
->dst_len
)
1082 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec
*codec
,
1083 struct snd_soc_lzo_ctx
*lzo_ctx
)
1087 lzo_ctx
->dst_len
= lzo1x_worst_compress(PAGE_SIZE
);
1088 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
1089 if (!lzo_ctx
->dst
) {
1090 lzo_ctx
->dst_len
= 0;
1094 ret
= snd_soc_lzo_compress(lzo_ctx
);
1100 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec
*codec
,
1101 struct snd_soc_lzo_ctx
*lzo_ctx
)
1105 lzo_ctx
->dst_len
= lzo_ctx
->decompressed_size
;
1106 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
1107 if (!lzo_ctx
->dst
) {
1108 lzo_ctx
->dst_len
= 0;
1112 ret
= snd_soc_lzo_decompress(lzo_ctx
);
1118 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec
*codec
,
1121 const struct snd_soc_codec_driver
*codec_drv
;
1123 codec_drv
= codec
->driver
;
1124 return (reg
* codec_drv
->reg_word_size
) /
1125 DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
1128 static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec
*codec
,
1131 const struct snd_soc_codec_driver
*codec_drv
;
1133 codec_drv
= codec
->driver
;
1134 return reg
% (DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count()) /
1135 codec_drv
->reg_word_size
);
1138 static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec
*codec
)
1140 const struct snd_soc_codec_driver
*codec_drv
;
1142 codec_drv
= codec
->driver
;
1143 return DIV_ROUND_UP(codec
->reg_size
, snd_soc_lzo_block_count());
1146 static int snd_soc_lzo_cache_sync(struct snd_soc_codec
*codec
)
1148 struct snd_soc_lzo_ctx
**lzo_blocks
;
1153 lzo_blocks
= codec
->reg_cache
;
1154 for_each_set_bit(i
, lzo_blocks
[0]->sync_bmp
, lzo_blocks
[0]->sync_bmp_nbits
) {
1155 ret
= snd_soc_cache_read(codec
, i
, &val
);
1158 codec
->cache_bypass
= 1;
1159 ret
= snd_soc_write(codec
, i
, val
);
1160 codec
->cache_bypass
= 0;
1163 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
1170 static int snd_soc_lzo_cache_write(struct snd_soc_codec
*codec
,
1171 unsigned int reg
, unsigned int value
)
1173 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
1174 int ret
, blkindex
, blkpos
;
1175 size_t blksize
, tmp_dst_len
;
1178 /* index of the compressed lzo block */
1179 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
1180 /* register index within the decompressed block */
1181 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
1182 /* size of the compressed block */
1183 blksize
= snd_soc_lzo_get_blksize(codec
);
1184 lzo_blocks
= codec
->reg_cache
;
1185 lzo_block
= lzo_blocks
[blkindex
];
1187 /* save the pointer and length of the compressed block */
1188 tmp_dst
= lzo_block
->dst
;
1189 tmp_dst_len
= lzo_block
->dst_len
;
1191 /* prepare the source to be the compressed block */
1192 lzo_block
->src
= lzo_block
->dst
;
1193 lzo_block
->src_len
= lzo_block
->dst_len
;
1195 /* decompress the block */
1196 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
1198 kfree(lzo_block
->dst
);
1202 /* write the new value to the cache */
1203 if (snd_soc_set_cache_val(lzo_block
->dst
, blkpos
, value
,
1204 codec
->driver
->reg_word_size
)) {
1205 kfree(lzo_block
->dst
);
1209 /* prepare the source to be the decompressed block */
1210 lzo_block
->src
= lzo_block
->dst
;
1211 lzo_block
->src_len
= lzo_block
->dst_len
;
1213 /* compress the block */
1214 ret
= snd_soc_lzo_compress_cache_block(codec
, lzo_block
);
1216 kfree(lzo_block
->dst
);
1217 kfree(lzo_block
->src
);
1221 /* set the bit so we know we have to sync this register */
1222 set_bit(reg
, lzo_block
->sync_bmp
);
1224 kfree(lzo_block
->src
);
1227 lzo_block
->dst
= tmp_dst
;
1228 lzo_block
->dst_len
= tmp_dst_len
;
1232 static int snd_soc_lzo_cache_read(struct snd_soc_codec
*codec
,
1233 unsigned int reg
, unsigned int *value
)
1235 struct snd_soc_lzo_ctx
*lzo_block
, **lzo_blocks
;
1236 int ret
, blkindex
, blkpos
;
1237 size_t blksize
, tmp_dst_len
;
1241 /* index of the compressed lzo block */
1242 blkindex
= snd_soc_lzo_get_blkindex(codec
, reg
);
1243 /* register index within the decompressed block */
1244 blkpos
= snd_soc_lzo_get_blkpos(codec
, reg
);
1245 /* size of the compressed block */
1246 blksize
= snd_soc_lzo_get_blksize(codec
);
1247 lzo_blocks
= codec
->reg_cache
;
1248 lzo_block
= lzo_blocks
[blkindex
];
1250 /* save the pointer and length of the compressed block */
1251 tmp_dst
= lzo_block
->dst
;
1252 tmp_dst_len
= lzo_block
->dst_len
;
1254 /* prepare the source to be the compressed block */
1255 lzo_block
->src
= lzo_block
->dst
;
1256 lzo_block
->src_len
= lzo_block
->dst_len
;
1258 /* decompress the block */
1259 ret
= snd_soc_lzo_decompress_cache_block(codec
, lzo_block
);
1261 /* fetch the value from the cache */
1262 *value
= snd_soc_get_cache_val(lzo_block
->dst
, blkpos
,
1263 codec
->driver
->reg_word_size
);
1265 kfree(lzo_block
->dst
);
1266 /* restore the pointer and length of the compressed block */
1267 lzo_block
->dst
= tmp_dst
;
1268 lzo_block
->dst_len
= tmp_dst_len
;
1272 static int snd_soc_lzo_cache_exit(struct snd_soc_codec
*codec
)
1274 struct snd_soc_lzo_ctx
**lzo_blocks
;
1277 lzo_blocks
= codec
->reg_cache
;
1281 blkcount
= snd_soc_lzo_block_count();
1283 * the pointer to the bitmap used for syncing the cache
1284 * is shared amongst all lzo_blocks. Ensure it is freed
1288 kfree(lzo_blocks
[0]->sync_bmp
);
1289 for (i
= 0; i
< blkcount
; ++i
) {
1290 if (lzo_blocks
[i
]) {
1291 kfree(lzo_blocks
[i
]->wmem
);
1292 kfree(lzo_blocks
[i
]->dst
);
1294 /* each lzo_block is a pointer returned by kmalloc or NULL */
1295 kfree(lzo_blocks
[i
]);
1298 codec
->reg_cache
= NULL
;
1302 static int snd_soc_lzo_cache_init(struct snd_soc_codec
*codec
)
1304 struct snd_soc_lzo_ctx
**lzo_blocks
;
1306 const struct snd_soc_codec_driver
*codec_drv
;
1307 int ret
, tofree
, i
, blksize
, blkcount
;
1308 const char *p
, *end
;
1309 unsigned long *sync_bmp
;
1312 codec_drv
= codec
->driver
;
1315 * If we have not been given a default register cache
1316 * then allocate a dummy zero-ed out region, compress it
1317 * and remember to free it afterwards.
1320 if (!codec
->reg_def_copy
)
1323 if (!codec
->reg_def_copy
) {
1324 codec
->reg_def_copy
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1325 if (!codec
->reg_def_copy
)
1329 blkcount
= snd_soc_lzo_block_count();
1330 codec
->reg_cache
= kzalloc(blkcount
* sizeof *lzo_blocks
,
1332 if (!codec
->reg_cache
) {
1336 lzo_blocks
= codec
->reg_cache
;
1339 * allocate a bitmap to be used when syncing the cache with
1340 * the hardware. Each time a register is modified, the corresponding
1341 * bit is set in the bitmap, so we know that we have to sync
1344 bmp_size
= codec_drv
->reg_cache_size
;
1345 sync_bmp
= kmalloc(BITS_TO_LONGS(bmp_size
) * sizeof(long),
1351 bitmap_zero(sync_bmp
, bmp_size
);
1353 /* allocate the lzo blocks and initialize them */
1354 for (i
= 0; i
< blkcount
; ++i
) {
1355 lzo_blocks
[i
] = kzalloc(sizeof **lzo_blocks
,
1357 if (!lzo_blocks
[i
]) {
1362 lzo_blocks
[i
]->sync_bmp
= sync_bmp
;
1363 lzo_blocks
[i
]->sync_bmp_nbits
= bmp_size
;
1364 /* alloc the working space for the compressed block */
1365 ret
= snd_soc_lzo_prepare(lzo_blocks
[i
]);
1370 blksize
= snd_soc_lzo_get_blksize(codec
);
1371 p
= codec
->reg_def_copy
;
1372 end
= codec
->reg_def_copy
+ codec
->reg_size
;
1373 /* compress the register map and fill the lzo blocks */
1374 for (i
= 0; i
< blkcount
; ++i
, p
+= blksize
) {
1375 lzo_blocks
[i
]->src
= p
;
1376 if (p
+ blksize
> end
)
1377 lzo_blocks
[i
]->src_len
= end
- p
;
1379 lzo_blocks
[i
]->src_len
= blksize
;
1380 ret
= snd_soc_lzo_compress_cache_block(codec
,
1384 lzo_blocks
[i
]->decompressed_size
=
1385 lzo_blocks
[i
]->src_len
;
1389 kfree(codec
->reg_def_copy
);
1390 codec
->reg_def_copy
= NULL
;
1394 snd_soc_cache_exit(codec
);
1397 kfree(codec
->reg_def_copy
);
1398 codec
->reg_def_copy
= NULL
;
1404 static int snd_soc_flat_cache_sync(struct snd_soc_codec
*codec
)
1408 const struct snd_soc_codec_driver
*codec_drv
;
1411 codec_drv
= codec
->driver
;
1412 for (i
= 0; i
< codec_drv
->reg_cache_size
; ++i
) {
1413 ret
= snd_soc_cache_read(codec
, i
, &val
);
1416 if (codec
->reg_def_copy
)
1417 if (snd_soc_get_cache_val(codec
->reg_def_copy
,
1418 i
, codec_drv
->reg_word_size
) == val
)
1420 ret
= snd_soc_write(codec
, i
, val
);
1423 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
1429 static int snd_soc_flat_cache_write(struct snd_soc_codec
*codec
,
1430 unsigned int reg
, unsigned int value
)
1432 snd_soc_set_cache_val(codec
->reg_cache
, reg
, value
,
1433 codec
->driver
->reg_word_size
);
1437 static int snd_soc_flat_cache_read(struct snd_soc_codec
*codec
,
1438 unsigned int reg
, unsigned int *value
)
1440 *value
= snd_soc_get_cache_val(codec
->reg_cache
, reg
,
1441 codec
->driver
->reg_word_size
);
1445 static int snd_soc_flat_cache_exit(struct snd_soc_codec
*codec
)
1447 if (!codec
->reg_cache
)
1449 kfree(codec
->reg_cache
);
1450 codec
->reg_cache
= NULL
;
1454 static int snd_soc_flat_cache_init(struct snd_soc_codec
*codec
)
1456 const struct snd_soc_codec_driver
*codec_drv
;
1458 codec_drv
= codec
->driver
;
1460 if (codec
->reg_def_copy
)
1461 codec
->reg_cache
= kmemdup(codec
->reg_def_copy
,
1462 codec
->reg_size
, GFP_KERNEL
);
1464 codec
->reg_cache
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
1465 if (!codec
->reg_cache
)
1471 /* an array of all supported compression types */
1472 static const struct snd_soc_cache_ops cache_types
[] = {
1473 /* Flat *must* be the first entry for fallback */
1475 .id
= SND_SOC_FLAT_COMPRESSION
,
1477 .init
= snd_soc_flat_cache_init
,
1478 .exit
= snd_soc_flat_cache_exit
,
1479 .read
= snd_soc_flat_cache_read
,
1480 .write
= snd_soc_flat_cache_write
,
1481 .sync
= snd_soc_flat_cache_sync
1483 #ifdef CONFIG_SND_SOC_CACHE_LZO
1485 .id
= SND_SOC_LZO_COMPRESSION
,
1487 .init
= snd_soc_lzo_cache_init
,
1488 .exit
= snd_soc_lzo_cache_exit
,
1489 .read
= snd_soc_lzo_cache_read
,
1490 .write
= snd_soc_lzo_cache_write
,
1491 .sync
= snd_soc_lzo_cache_sync
1495 .id
= SND_SOC_RBTREE_COMPRESSION
,
1497 .init
= snd_soc_rbtree_cache_init
,
1498 .exit
= snd_soc_rbtree_cache_exit
,
1499 .read
= snd_soc_rbtree_cache_read
,
1500 .write
= snd_soc_rbtree_cache_write
,
1501 .sync
= snd_soc_rbtree_cache_sync
1505 int snd_soc_cache_init(struct snd_soc_codec
*codec
)
1509 for (i
= 0; i
< ARRAY_SIZE(cache_types
); ++i
)
1510 if (cache_types
[i
].id
== codec
->compress_type
)
1513 /* Fall back to flat compression */
1514 if (i
== ARRAY_SIZE(cache_types
)) {
1515 dev_warn(codec
->dev
, "Could not match compress type: %d\n",
1516 codec
->compress_type
);
1520 mutex_init(&codec
->cache_rw_mutex
);
1521 codec
->cache_ops
= &cache_types
[i
];
1523 if (codec
->cache_ops
->init
) {
1524 if (codec
->cache_ops
->name
)
1525 dev_dbg(codec
->dev
, "Initializing %s cache for %s codec\n",
1526 codec
->cache_ops
->name
, codec
->name
);
1527 return codec
->cache_ops
->init(codec
);
1533 * NOTE: keep in mind that this function might be called
1536 int snd_soc_cache_exit(struct snd_soc_codec
*codec
)
1538 if (codec
->cache_ops
&& codec
->cache_ops
->exit
) {
1539 if (codec
->cache_ops
->name
)
1540 dev_dbg(codec
->dev
, "Destroying %s cache for %s codec\n",
1541 codec
->cache_ops
->name
, codec
->name
);
1542 return codec
->cache_ops
->exit(codec
);
1548 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1550 * @codec: CODEC to configure.
1551 * @reg: The register index.
1552 * @value: The value to be returned.
1554 int snd_soc_cache_read(struct snd_soc_codec
*codec
,
1555 unsigned int reg
, unsigned int *value
)
1559 mutex_lock(&codec
->cache_rw_mutex
);
1561 if (value
&& codec
->cache_ops
&& codec
->cache_ops
->read
) {
1562 ret
= codec
->cache_ops
->read(codec
, reg
, value
);
1563 mutex_unlock(&codec
->cache_rw_mutex
);
1567 mutex_unlock(&codec
->cache_rw_mutex
);
1570 EXPORT_SYMBOL_GPL(snd_soc_cache_read
);
1573 * snd_soc_cache_write: Set the value of a given register in the cache.
1575 * @codec: CODEC to configure.
1576 * @reg: The register index.
1577 * @value: The new register value.
1579 int snd_soc_cache_write(struct snd_soc_codec
*codec
,
1580 unsigned int reg
, unsigned int value
)
1584 mutex_lock(&codec
->cache_rw_mutex
);
1586 if (codec
->cache_ops
&& codec
->cache_ops
->write
) {
1587 ret
= codec
->cache_ops
->write(codec
, reg
, value
);
1588 mutex_unlock(&codec
->cache_rw_mutex
);
1592 mutex_unlock(&codec
->cache_rw_mutex
);
1595 EXPORT_SYMBOL_GPL(snd_soc_cache_write
);
1598 * snd_soc_cache_sync: Sync the register cache with the hardware.
1600 * @codec: CODEC to configure.
1602 * Any registers that should not be synced should be marked as
1603 * volatile. In general drivers can choose not to use the provided
1604 * syncing functionality if they so require.
1606 int snd_soc_cache_sync(struct snd_soc_codec
*codec
)
1611 if (!codec
->cache_sync
) {
1615 if (!codec
->cache_ops
|| !codec
->cache_ops
->sync
)
1618 if (codec
->cache_ops
->name
)
1619 name
= codec
->cache_ops
->name
;
1623 if (codec
->cache_ops
->name
)
1624 dev_dbg(codec
->dev
, "Syncing %s cache for %s codec\n",
1625 codec
->cache_ops
->name
, codec
->name
);
1626 trace_snd_soc_cache_sync(codec
, name
, "start");
1627 ret
= codec
->cache_ops
->sync(codec
);
1629 codec
->cache_sync
= 0;
1630 trace_snd_soc_cache_sync(codec
, name
, "end");
1633 EXPORT_SYMBOL_GPL(snd_soc_cache_sync
);
1635 static int snd_soc_get_reg_access_index(struct snd_soc_codec
*codec
,
1638 const struct snd_soc_codec_driver
*codec_drv
;
1639 unsigned int min
, max
, index
;
1641 codec_drv
= codec
->driver
;
1643 max
= codec_drv
->reg_access_size
- 1;
1645 index
= (min
+ max
) / 2;
1646 if (codec_drv
->reg_access_default
[index
].reg
== reg
)
1648 if (codec_drv
->reg_access_default
[index
].reg
< reg
)
1652 } while (min
<= max
);
1656 int snd_soc_default_volatile_register(struct snd_soc_codec
*codec
,
1661 if (reg
>= codec
->driver
->reg_cache_size
)
1663 index
= snd_soc_get_reg_access_index(codec
, reg
);
1666 return codec
->driver
->reg_access_default
[index
].vol
;
1668 EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register
);
1670 int snd_soc_default_readable_register(struct snd_soc_codec
*codec
,
1675 if (reg
>= codec
->driver
->reg_cache_size
)
1677 index
= snd_soc_get_reg_access_index(codec
, reg
);
1680 return codec
->driver
->reg_access_default
[index
].read
;
1682 EXPORT_SYMBOL_GPL(snd_soc_default_readable_register
);