Add linux-next specific files for 20110421
[linux-2.6/next.git] / sound / soc / soc-cache.c
bloba217db256700ed9998b6812849dd8d8634710538
1 /*
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,
25 int len)
27 struct spi_device *spi = control_data;
28 struct spi_transfer t;
29 struct spi_message m;
31 if (len <= 0)
32 return 0;
34 spi_message_init(&m);
35 memset(&t, 0, sizeof t);
37 t.tx_buf = msg;
38 t.len = len;
40 spi_message_add_tail(&t, &m);
41 spi_sync(spi, &m);
43 return len;
45 #endif
47 static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg,
48 unsigned int value, const void *data, int len)
50 int ret;
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);
56 if (ret < 0)
57 return -1;
60 if (codec->cache_only) {
61 codec->cache_sync = 1;
62 return 0;
65 ret = codec->hw_write(codec->control_data, data, len);
66 if (ret == len)
67 return 0;
68 if (ret < 0)
69 return ret;
70 else
71 return -EIO;
74 static unsigned int do_hw_read(struct snd_soc_codec *codec, unsigned int reg)
76 int ret;
77 unsigned int val;
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)
83 return -1;
85 BUG_ON(!codec->hw_read);
86 return codec->hw_read(codec, reg);
89 ret = snd_soc_cache_read(codec, reg, &val);
90 if (ret < 0)
91 return -1;
92 return val;
95 static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
96 unsigned int reg)
98 return do_hw_read(codec, reg);
101 static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
102 unsigned int value)
104 u8 data[2];
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,
114 int len)
116 u8 msg[2];
118 msg[0] = data[1];
119 msg[1] = data[0];
121 return do_spi_write(control_data, msg, len);
123 #else
124 #define snd_soc_4_12_spi_write NULL
125 #endif
127 static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
128 unsigned int reg)
130 return do_hw_read(codec, reg);
133 static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
134 unsigned int value)
136 u8 data[2];
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,
146 int len)
148 u8 msg[2];
150 msg[0] = data[0];
151 msg[1] = data[1];
153 return do_spi_write(control_data, msg, len);
155 #else
156 #define snd_soc_7_9_spi_write NULL
157 #endif
159 static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
160 unsigned int value)
162 u8 data[2];
164 reg &= 0xff;
165 data[0] = 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,
172 unsigned int reg)
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,
179 int len)
181 u8 msg[2];
183 msg[0] = data[0];
184 msg[1] = data[1];
186 return do_spi_write(control_data, msg, len);
188 #else
189 #define snd_soc_8_8_spi_write NULL
190 #endif
192 static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
193 unsigned int value)
195 u8 data[3];
197 data[0] = 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,
205 unsigned int reg)
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,
212 int len)
214 u8 msg[3];
216 msg[0] = data[0];
217 msg[1] = data[1];
218 msg[2] = data[2];
220 return do_spi_write(control_data, msg, len);
222 #else
223 #define snd_soc_8_16_spi_write NULL
224 #endif
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];
232 int ret;
233 struct i2c_client *client = codec->control_data;
235 /* Write register */
236 xfer[0].addr = client->addr;
237 xfer[0].flags = 0;
238 xfer[0].len = reglen;
239 xfer[0].buf = reg;
241 /* Read data */
242 xfer[1].addr = client->addr;
243 xfer[1].flags = I2C_M_RD;
244 xfer[1].len = datalen;
245 xfer[1].buf = data;
247 ret = i2c_transfer(client->adapter, xfer, 2);
248 if (ret == 2)
249 return 0;
250 else if (ret < 0)
251 return ret;
252 else
253 return -EIO;
255 #endif
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,
259 unsigned int r)
261 u8 reg = r;
262 u8 data;
263 int ret;
265 ret = do_i2c_read(codec, &reg, 1, &data, 1);
266 if (ret < 0)
267 return 0;
268 return data;
270 #else
271 #define snd_soc_8_8_read_i2c NULL
272 #endif
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,
276 unsigned int r)
278 u8 reg = r;
279 u16 data;
280 int ret;
282 ret = do_i2c_read(codec, &reg, 1, &data, 2);
283 if (ret < 0)
284 return 0;
285 return (data >> 8) | ((data & 0xff) << 8);
287 #else
288 #define snd_soc_8_16_read_i2c NULL
289 #endif
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,
293 unsigned int r)
295 u16 reg = r;
296 u8 data;
297 int ret;
299 ret = do_i2c_read(codec, &reg, 2, &data, 1);
300 if (ret < 0)
301 return 0;
302 return data;
304 #else
305 #define snd_soc_16_8_read_i2c NULL
306 #endif
308 static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
309 unsigned int reg)
311 return do_hw_read(codec, reg);
314 static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
315 unsigned int value)
317 u8 data[3];
319 data[0] = (reg >> 8) & 0xff;
320 data[1] = reg & 0xff;
321 data[2] = value;
322 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,
329 int len)
331 u8 msg[3];
333 msg[0] = data[0];
334 msg[1] = data[1];
335 msg[2] = data[2];
337 return do_spi_write(control_data, msg, len);
339 #else
340 #define snd_soc_16_8_spi_write NULL
341 #endif
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,
345 unsigned int r)
347 u16 reg = cpu_to_be16(r);
348 u16 data;
349 int ret;
351 ret = do_i2c_read(codec, &reg, 2, &data, 2);
352 if (ret < 0)
353 return 0;
354 return be16_to_cpu(data);
356 #else
357 #define snd_soc_16_16_read_i2c NULL
358 #endif
360 static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
361 unsigned int reg)
363 return do_hw_read(codec, reg);
366 static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
367 unsigned int value)
369 u8 data[4];
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,
381 int len)
383 u8 msg[4];
385 msg[0] = data[0];
386 msg[1] = data[1];
387 msg[2] = data[2];
388 msg[3] = data[3];
390 return do_spi_write(control_data, msg, len);
392 #else
393 #define snd_soc_16_16_spi_write NULL
394 #endif
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)
405 int ret;
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
410 * the cache.
412 if (!snd_soc_codec_volatile_register(codec, reg)
413 && reg < codec->driver->reg_cache_size)
414 return -EINVAL;
416 switch (codec->control_type) {
417 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
418 case SND_SOC_I2C:
419 ret = i2c_master_send(codec->control_data, data, len);
420 break;
421 #endif
422 #if defined(CONFIG_SPI_MASTER)
423 case SND_SOC_SPI:
424 ret = do_spi_write(codec->control_data, data, len);
425 break;
426 #endif
427 default:
428 BUG();
431 if (ret == len)
432 return 0;
433 if (ret < 0)
434 return ret;
435 else
436 return -EIO;
439 static struct {
440 int addr_bits;
441 int data_bits;
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);
446 } io_types[] = {
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
497 * actual cache.
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)
506 int i;
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)
511 break;
512 if (i == ARRAY_SIZE(io_types)) {
513 printk(KERN_ERR
514 "No I/O functions for %d bit address %d bit data\n",
515 addr_bits, data_bits);
516 return -EINVAL;
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;
523 switch (control) {
524 case SND_SOC_CUSTOM:
525 break;
527 case SND_SOC_I2C:
528 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
529 codec->hw_write = (hw_write_t)i2c_master_send;
530 #endif
531 if (io_types[i].i2c_read)
532 codec->hw_read = io_types[i].i2c_read;
534 codec->control_data = container_of(codec->dev,
535 struct i2c_client,
536 dev);
537 break;
539 case SND_SOC_SPI:
540 if (io_types[i].spi_write)
541 codec->hw_write = io_types[i].spi_write;
543 codec->control_data = container_of(codec->dev,
544 struct spi_device,
545 dev);
546 break;
549 return 0;
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)
556 switch (word_size) {
557 case 1: {
558 u8 *cache = base;
559 if (cache[idx] == val)
560 return true;
561 cache[idx] = val;
562 break;
564 case 2: {
565 u16 *cache = base;
566 if (cache[idx] == val)
567 return true;
568 cache[idx] = val;
569 break;
571 default:
572 BUG();
574 return false;
577 static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
578 unsigned int word_size)
580 switch (word_size) {
581 case 1: {
582 const u8 *cache = base;
583 return cache[idx];
585 case 2: {
586 const u16 *cache = base;
587 return cache[idx];
589 default:
590 BUG();
592 /* unreachable */
593 return -1;
596 struct snd_soc_rbtree_node {
597 struct rb_node node;
598 unsigned int reg;
599 unsigned int value;
600 unsigned int defval;
601 } __attribute__ ((packed));
603 struct snd_soc_rbtree_ctx {
604 struct rb_root root;
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;
614 while (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;
620 else
621 return rbnode;
624 return NULL;
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;
633 parent = NULL;
634 new = &root->rb_node;
635 while (*new) {
636 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
637 node);
638 parent = *new;
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);
643 else
644 return 0;
647 /* insert the node into the rbtree */
648 rb_link_node(&rbnode->node, parent, new);
649 rb_insert_color(&rbnode->node, root);
651 return 1;
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;
659 unsigned int val;
660 int ret;
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)
666 continue;
667 WARN_ON(codec->writable_register &&
668 codec->writable_register(codec, rbnode->reg));
669 ret = snd_soc_cache_read(codec, rbnode->reg, &val);
670 if (ret)
671 return ret;
672 codec->cache_bypass = 1;
673 ret = snd_soc_write(codec, rbnode->reg, val);
674 codec->cache_bypass = 0;
675 if (ret)
676 return ret;
677 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
678 rbnode->reg, val);
681 return 0;
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);
692 if (rbnode) {
693 if (rbnode->value == value)
694 return 0;
695 rbnode->value = value;
696 } else {
697 /* bail out early, no need to create the rbnode yet */
698 if (!value)
699 return 0;
701 * for uninitialized registers whose value is changed
702 * from the default zero, create an rbnode and insert
703 * it into the tree.
705 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
706 if (!rbnode)
707 return -ENOMEM;
708 rbnode->reg = reg;
709 rbnode->value = value;
710 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
713 return 0;
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);
724 if (rbnode) {
725 *value = rbnode->value;
726 } else {
727 /* uninitialized registers default to 0 */
728 *value = 0;
731 return 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;
742 if (!rbtree_ctx)
743 return 0;
745 /* free up the rbtree */
746 next = rb_first(&rbtree_ctx->root);
747 while (next) {
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);
751 kfree(rbtree_node);
754 /* release the resources */
755 kfree(codec->reg_cache);
756 codec->reg_cache = NULL;
758 return 0;
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;
765 unsigned int val;
766 unsigned int word_size;
767 int i;
768 int ret;
770 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
771 if (!codec->reg_cache)
772 return -ENOMEM;
774 rbtree_ctx = codec->reg_cache;
775 rbtree_ctx->root = RB_ROOT;
777 if (!codec->reg_def_copy)
778 return 0;
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);
787 if (!val)
788 continue;
789 rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL);
790 if (!rbtree_node) {
791 ret = -ENOMEM;
792 snd_soc_cache_exit(codec);
793 break;
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);
801 return 0;
804 #ifdef CONFIG_SND_SOC_CACHE_LZO
805 struct snd_soc_lzo_ctx {
806 void *wmem;
807 void *dst;
808 const void *src;
809 size_t src_len;
810 size_t dst_len;
811 size_t decompressed_size;
812 unsigned long *sync_bmp;
813 int sync_bmp_nbits;
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);
825 if (!lzo_ctx->wmem)
826 return -ENOMEM;
827 return 0;
830 static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
832 size_t compress_size;
833 int ret;
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)
838 return -EINVAL;
839 lzo_ctx->dst_len = compress_size;
840 return 0;
843 static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
845 size_t dst_len;
846 int ret;
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)
852 return -EINVAL;
853 return 0;
856 static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
857 struct snd_soc_lzo_ctx *lzo_ctx)
859 int ret;
861 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
862 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
863 if (!lzo_ctx->dst) {
864 lzo_ctx->dst_len = 0;
865 return -ENOMEM;
868 ret = snd_soc_lzo_compress(lzo_ctx);
869 if (ret < 0)
870 return ret;
871 return 0;
874 static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
875 struct snd_soc_lzo_ctx *lzo_ctx)
877 int ret;
879 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
880 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
881 if (!lzo_ctx->dst) {
882 lzo_ctx->dst_len = 0;
883 return -ENOMEM;
886 ret = snd_soc_lzo_decompress(lzo_ctx);
887 if (ret < 0)
888 return ret;
889 return 0;
892 static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
893 unsigned int reg)
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,
903 unsigned int reg)
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;
923 unsigned int val;
924 int i;
925 int ret;
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);
932 if (ret)
933 return ret;
934 codec->cache_bypass = 1;
935 ret = snd_soc_write(codec, i, val);
936 codec->cache_bypass = 0;
937 if (ret)
938 return ret;
939 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
940 i, val);
943 return 0;
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;
952 void *tmp_dst;
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);
973 if (ret < 0) {
974 kfree(lzo_block->dst);
975 goto out;
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);
982 goto out;
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);
991 if (ret < 0) {
992 kfree(lzo_block->dst);
993 kfree(lzo_block->src);
994 goto out;
997 /* set the bit so we know we have to sync this register */
998 set_bit(reg, lzo_block->sync_bmp);
999 kfree(tmp_dst);
1000 kfree(lzo_block->src);
1001 return 0;
1002 out:
1003 lzo_block->dst = tmp_dst;
1004 lzo_block->dst_len = tmp_dst_len;
1005 return ret;
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;
1014 void *tmp_dst;
1016 *value = 0;
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);
1036 if (ret >= 0)
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;
1045 return 0;
1048 static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1050 struct snd_soc_lzo_ctx **lzo_blocks;
1051 int i, blkcount;
1053 lzo_blocks = codec->reg_cache;
1054 if (!lzo_blocks)
1055 return 0;
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
1061 * only once.
1063 if (lzo_blocks[0])
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]);
1073 kfree(lzo_blocks);
1074 codec->reg_cache = NULL;
1075 return 0;
1078 static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1080 struct snd_soc_lzo_ctx **lzo_blocks;
1081 size_t bmp_size;
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;
1087 ret = 0;
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.
1095 tofree = 0;
1096 if (!codec->reg_def_copy)
1097 tofree = 1;
1099 if (!codec->reg_def_copy) {
1100 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
1101 if (!codec->reg_def_copy)
1102 return -ENOMEM;
1105 blkcount = snd_soc_lzo_block_count();
1106 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1107 GFP_KERNEL);
1108 if (!codec->reg_cache) {
1109 ret = -ENOMEM;
1110 goto err_tofree;
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
1118 * that register.
1120 bmp_size = codec_drv->reg_cache_size;
1121 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
1122 GFP_KERNEL);
1123 if (!sync_bmp) {
1124 ret = -ENOMEM;
1125 goto err;
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,
1132 GFP_KERNEL);
1133 if (!lzo_blocks[i]) {
1134 kfree(sync_bmp);
1135 ret = -ENOMEM;
1136 goto err;
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]);
1142 if (ret < 0)
1143 goto err;
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;
1154 else
1155 lzo_blocks[i]->src_len = blksize;
1156 ret = snd_soc_lzo_compress_cache_block(codec,
1157 lzo_blocks[i]);
1158 if (ret < 0)
1159 goto err;
1160 lzo_blocks[i]->decompressed_size =
1161 lzo_blocks[i]->src_len;
1164 if (tofree) {
1165 kfree(codec->reg_def_copy);
1166 codec->reg_def_copy = NULL;
1168 return 0;
1169 err:
1170 snd_soc_cache_exit(codec);
1171 err_tofree:
1172 if (tofree) {
1173 kfree(codec->reg_def_copy);
1174 codec->reg_def_copy = NULL;
1176 return ret;
1178 #endif
1180 static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1182 int i;
1183 int ret;
1184 const struct snd_soc_codec_driver *codec_drv;
1185 unsigned int val;
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);
1192 if (ret)
1193 return ret;
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)
1197 continue;
1198 ret = snd_soc_write(codec, i, val);
1199 if (ret)
1200 return ret;
1201 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1202 i, val);
1204 return 0;
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);
1212 return 0;
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);
1220 return 0;
1223 static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1225 if (!codec->reg_cache)
1226 return 0;
1227 kfree(codec->reg_cache);
1228 codec->reg_cache = NULL;
1229 return 0;
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);
1241 else
1242 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
1243 if (!codec->reg_cache)
1244 return -ENOMEM;
1246 return 0;
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,
1254 .name = "flat",
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,
1264 .name = "LZO",
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
1271 #endif
1273 .id = SND_SOC_RBTREE_COMPRESSION,
1274 .name = "rbtree",
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)
1285 int i;
1287 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
1288 if (cache_types[i].id == codec->compress_type)
1289 break;
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);
1295 i = 0;
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);
1307 return -ENOSYS;
1311 * NOTE: keep in mind that this function might be called
1312 * multiple times.
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);
1322 return -ENOSYS;
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)
1335 int ret;
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);
1342 return ret;
1345 mutex_unlock(&codec->cache_rw_mutex);
1346 return -ENOSYS;
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)
1360 int ret;
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);
1367 return ret;
1370 mutex_unlock(&codec->cache_rw_mutex);
1371 return -ENOSYS;
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)
1386 int ret;
1387 const char *name;
1389 if (!codec->cache_sync) {
1390 return 0;
1393 if (!codec->cache_ops || !codec->cache_ops->sync)
1394 return -ENOSYS;
1396 if (codec->cache_ops->name)
1397 name = codec->cache_ops->name;
1398 else
1399 name = "unknown";
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);
1406 if (!ret)
1407 codec->cache_sync = 0;
1408 trace_snd_soc_cache_sync(codec, name, "end");
1409 return ret;
1411 EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
1413 static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1414 unsigned int reg)
1416 const struct snd_soc_codec_driver *codec_drv;
1417 unsigned int min, max, index;
1419 codec_drv = codec->driver;
1420 min = 0;
1421 max = codec_drv->reg_access_size - 1;
1422 do {
1423 index = (min + max) / 2;
1424 if (codec_drv->reg_access_default[index].reg == reg)
1425 return index;
1426 if (codec_drv->reg_access_default[index].reg < reg)
1427 min = index + 1;
1428 else
1429 max = index;
1430 } while (min <= max);
1431 return -1;
1434 int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1435 unsigned int reg)
1437 int index;
1439 if (reg >= codec->driver->reg_cache_size)
1440 return 1;
1441 index = snd_soc_get_reg_access_index(codec, reg);
1442 if (index < 0)
1443 return 0;
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,
1449 unsigned int reg)
1451 int index;
1453 if (reg >= codec->driver->reg_cache_size)
1454 return 1;
1455 index = snd_soc_get_reg_access_index(codec, reg);
1456 if (index < 0)
1457 return 0;
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,
1463 unsigned int reg)
1465 int index;
1467 if (reg >= codec->driver->reg_cache_size)
1468 return 1;
1469 index = snd_soc_get_reg_access_index(codec, reg);
1470 if (index < 0)
1471 return 0;
1472 return codec->driver->reg_access_default[index].write;
1474 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);