ASoC: CS4271 codec support
[linux-block.git] / sound / soc / soc-cache.c
CommitLineData
17a52fd6
MB
1/*
2 * soc-cache.c -- ASoC register cache helpers
3 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
12 */
13
7084a42b 14#include <linux/i2c.h>
27ded041 15#include <linux/spi/spi.h>
17a52fd6 16#include <sound/soc.h>
cc28fb8e
DP
17#include <linux/lzo.h>
18#include <linux/bitmap.h>
a7f387d5 19#include <linux/rbtree.h>
17a52fd6 20
63b62ab0
BS
21static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
22 unsigned int reg)
23{
7a30a3db
DP
24 int ret;
25 unsigned int val;
db49c146
DP
26
27 if (reg >= codec->driver->reg_cache_size ||
dad8e7ae
DP
28 snd_soc_codec_volatile_register(codec, reg) ||
29 codec->cache_bypass) {
db49c146
DP
30 if (codec->cache_only)
31 return -1;
32
5aaa062c 33 BUG_ON(!codec->hw_read);
db49c146
DP
34 return codec->hw_read(codec, reg);
35 }
36
7a30a3db
DP
37 ret = snd_soc_cache_read(codec, reg, &val);
38 if (ret < 0)
39 return -1;
40 return val;
63b62ab0
BS
41}
42
43static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
44 unsigned int value)
45{
63b62ab0
BS
46 u8 data[2];
47 int ret;
48
63b62ab0
BS
49 data[0] = (reg << 4) | ((value >> 8) & 0x000f);
50 data[1] = value & 0x00ff;
51
db49c146 52 if (!snd_soc_codec_volatile_register(codec, reg) &&
dad8e7ae
DP
53 reg < codec->driver->reg_cache_size &&
54 !codec->cache_bypass) {
7a30a3db
DP
55 ret = snd_soc_cache_write(codec, reg, value);
56 if (ret < 0)
57 return -1;
58 }
8c961bcc 59
a3032b47
MB
60 if (codec->cache_only) {
61 codec->cache_sync = 1;
8c961bcc 62 return 0;
a3032b47 63 }
8c961bcc 64
63b62ab0
BS
65 ret = codec->hw_write(codec->control_data, data, 2);
66 if (ret == 2)
67 return 0;
68 if (ret < 0)
69 return ret;
70 else
71 return -EIO;
72}
73
74#if defined(CONFIG_SPI_MASTER)
75static int snd_soc_4_12_spi_write(void *control_data, const char *data,
76 int len)
77{
78 struct spi_device *spi = control_data;
79 struct spi_transfer t;
80 struct spi_message m;
81 u8 msg[2];
82
83 if (len <= 0)
84 return 0;
85
86 msg[0] = data[1];
87 msg[1] = data[0];
88
89 spi_message_init(&m);
465d7fcc 90 memset(&t, 0, sizeof t);
63b62ab0
BS
91
92 t.tx_buf = &msg[0];
93 t.len = len;
94
95 spi_message_add_tail(&t, &m);
96 spi_sync(spi, &m);
97
98 return len;
99}
100#else
101#define snd_soc_4_12_spi_write NULL
102#endif
103
17a52fd6
MB
104static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
105 unsigned int reg)
106{
7a30a3db
DP
107 int ret;
108 unsigned int val;
db49c146
DP
109
110 if (reg >= codec->driver->reg_cache_size ||
dad8e7ae
DP
111 snd_soc_codec_volatile_register(codec, reg) ||
112 codec->cache_bypass) {
db49c146
DP
113 if (codec->cache_only)
114 return -1;
115
5aaa062c 116 BUG_ON(!codec->hw_read);
db49c146
DP
117 return codec->hw_read(codec, reg);
118 }
119
7a30a3db
DP
120 ret = snd_soc_cache_read(codec, reg, &val);
121 if (ret < 0)
122 return -1;
123 return val;
17a52fd6
MB
124}
125
126static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
127 unsigned int value)
128{
17a52fd6
MB
129 u8 data[2];
130 int ret;
131
17a52fd6
MB
132 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
133 data[1] = value & 0x00ff;
134
db49c146 135 if (!snd_soc_codec_volatile_register(codec, reg) &&
dad8e7ae
DP
136 reg < codec->driver->reg_cache_size &&
137 !codec->cache_bypass) {
7a30a3db
DP
138 ret = snd_soc_cache_write(codec, reg, value);
139 if (ret < 0)
140 return -1;
141 }
8c961bcc 142
a3032b47
MB
143 if (codec->cache_only) {
144 codec->cache_sync = 1;
8c961bcc 145 return 0;
a3032b47 146 }
8c961bcc 147
17a52fd6
MB
148 ret = codec->hw_write(codec->control_data, data, 2);
149 if (ret == 2)
150 return 0;
151 if (ret < 0)
152 return ret;
153 else
154 return -EIO;
155}
156
27ded041
MB
157#if defined(CONFIG_SPI_MASTER)
158static int snd_soc_7_9_spi_write(void *control_data, const char *data,
159 int len)
160{
161 struct spi_device *spi = control_data;
162 struct spi_transfer t;
163 struct spi_message m;
164 u8 msg[2];
165
166 if (len <= 0)
167 return 0;
168
169 msg[0] = data[0];
170 msg[1] = data[1];
171
172 spi_message_init(&m);
465d7fcc 173 memset(&t, 0, sizeof t);
27ded041
MB
174
175 t.tx_buf = &msg[0];
176 t.len = len;
177
178 spi_message_add_tail(&t, &m);
179 spi_sync(spi, &m);
180
181 return len;
182}
183#else
184#define snd_soc_7_9_spi_write NULL
185#endif
186
341c9b84
JS
187static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
188 unsigned int value)
189{
341c9b84 190 u8 data[2];
7a30a3db 191 int ret;
341c9b84 192
f4bee1bb
BS
193 reg &= 0xff;
194 data[0] = reg;
341c9b84
JS
195 data[1] = value & 0xff;
196
005d65fb 197 if (!snd_soc_codec_volatile_register(codec, reg) &&
dad8e7ae
DP
198 reg < codec->driver->reg_cache_size &&
199 !codec->cache_bypass) {
7a30a3db
DP
200 ret = snd_soc_cache_write(codec, reg, value);
201 if (ret < 0)
202 return -1;
203 }
341c9b84 204
a3032b47
MB
205 if (codec->cache_only) {
206 codec->cache_sync = 1;
8c961bcc 207 return 0;
a3032b47 208 }
8c961bcc 209
341c9b84
JS
210 if (codec->hw_write(codec->control_data, data, 2) == 2)
211 return 0;
212 else
213 return -EIO;
214}
215
216static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
217 unsigned int reg)
218{
7a30a3db
DP
219 int ret;
220 unsigned int val;
db49c146 221
f4bee1bb 222 reg &= 0xff;
db49c146 223 if (reg >= codec->driver->reg_cache_size ||
dad8e7ae
DP
224 snd_soc_codec_volatile_register(codec, reg) ||
225 codec->cache_bypass) {
db49c146
DP
226 if (codec->cache_only)
227 return -1;
228
5aaa062c 229 BUG_ON(!codec->hw_read);
db49c146
DP
230 return codec->hw_read(codec, reg);
231 }
232
7a30a3db
DP
233 ret = snd_soc_cache_read(codec, reg, &val);
234 if (ret < 0)
235 return -1;
236 return val;
341c9b84
JS
237}
238
f479fd93
DP
239#if defined(CONFIG_SPI_MASTER)
240static int snd_soc_8_8_spi_write(void *control_data, const char *data,
241 int len)
242{
243 struct spi_device *spi = control_data;
244 struct spi_transfer t;
245 struct spi_message m;
246 u8 msg[2];
247
248 if (len <= 0)
249 return 0;
250
251 msg[0] = data[0];
252 msg[1] = data[1];
253
254 spi_message_init(&m);
465d7fcc 255 memset(&t, 0, sizeof t);
f479fd93
DP
256
257 t.tx_buf = &msg[0];
258 t.len = len;
259
260 spi_message_add_tail(&t, &m);
261 spi_sync(spi, &m);
262
263 return len;
264}
265#else
266#define snd_soc_8_8_spi_write NULL
267#endif
268
afa2f106
MB
269static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
270 unsigned int value)
271{
afa2f106 272 u8 data[3];
7a30a3db 273 int ret;
afa2f106
MB
274
275 data[0] = reg;
276 data[1] = (value >> 8) & 0xff;
277 data[2] = value & 0xff;
278
3e13f65e 279 if (!snd_soc_codec_volatile_register(codec, reg) &&
dad8e7ae
DP
280 reg < codec->driver->reg_cache_size &&
281 !codec->cache_bypass) {
7a30a3db
DP
282 ret = snd_soc_cache_write(codec, reg, value);
283 if (ret < 0)
284 return -1;
285 }
afa2f106 286
a3032b47
MB
287 if (codec->cache_only) {
288 codec->cache_sync = 1;
8c961bcc 289 return 0;
a3032b47 290 }
8c961bcc 291
afa2f106
MB
292 if (codec->hw_write(codec->control_data, data, 3) == 3)
293 return 0;
294 else
295 return -EIO;
296}
297
298static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
299 unsigned int reg)
300{
7a30a3db
DP
301 int ret;
302 unsigned int val;
afa2f106 303
f0fba2ad 304 if (reg >= codec->driver->reg_cache_size ||
dad8e7ae
DP
305 snd_soc_codec_volatile_register(codec, reg) ||
306 codec->cache_bypass) {
8c961bcc 307 if (codec->cache_only)
391d8a04 308 return -1;
8c961bcc 309
5aaa062c 310 BUG_ON(!codec->hw_read);
afa2f106 311 return codec->hw_read(codec, reg);
8c961bcc 312 }
7a30a3db
DP
313
314 ret = snd_soc_cache_read(codec, reg, &val);
315 if (ret < 0)
316 return -1;
317 return val;
afa2f106
MB
318}
319
f479fd93
DP
320#if defined(CONFIG_SPI_MASTER)
321static int snd_soc_8_16_spi_write(void *control_data, const char *data,
322 int len)
323{
324 struct spi_device *spi = control_data;
325 struct spi_transfer t;
326 struct spi_message m;
327 u8 msg[3];
328
329 if (len <= 0)
330 return 0;
331
332 msg[0] = data[0];
333 msg[1] = data[1];
334 msg[2] = data[2];
335
336 spi_message_init(&m);
465d7fcc 337 memset(&t, 0, sizeof t);
f479fd93
DP
338
339 t.tx_buf = &msg[0];
340 t.len = len;
341
342 spi_message_add_tail(&t, &m);
343 spi_sync(spi, &m);
344
345 return len;
346}
347#else
348#define snd_soc_8_16_spi_write NULL
349#endif
350
85dfcdff
CC
351#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
352static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
353 unsigned int r)
354{
355 struct i2c_msg xfer[2];
356 u8 reg = r;
357 u8 data;
358 int ret;
359 struct i2c_client *client = codec->control_data;
360
361 /* Write register */
362 xfer[0].addr = client->addr;
363 xfer[0].flags = 0;
364 xfer[0].len = 1;
365 xfer[0].buf = &reg;
366
367 /* Read data */
368 xfer[1].addr = client->addr;
369 xfer[1].flags = I2C_M_RD;
370 xfer[1].len = 1;
371 xfer[1].buf = &data;
372
373 ret = i2c_transfer(client->adapter, xfer, 2);
374 if (ret != 2) {
375 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
376 return 0;
377 }
378
379 return data;
380}
381#else
382#define snd_soc_8_8_read_i2c NULL
383#endif
384
17244c24 385#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
afa2f106
MB
386static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
387 unsigned int r)
388{
389 struct i2c_msg xfer[2];
390 u8 reg = r;
391 u16 data;
392 int ret;
393 struct i2c_client *client = codec->control_data;
394
395 /* Write register */
396 xfer[0].addr = client->addr;
397 xfer[0].flags = 0;
398 xfer[0].len = 1;
399 xfer[0].buf = &reg;
400
401 /* Read data */
402 xfer[1].addr = client->addr;
403 xfer[1].flags = I2C_M_RD;
404 xfer[1].len = 2;
405 xfer[1].buf = (u8 *)&data;
406
407 ret = i2c_transfer(client->adapter, xfer, 2);
408 if (ret != 2) {
409 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
410 return 0;
411 }
412
413 return (data >> 8) | ((data & 0xff) << 8);
414}
415#else
416#define snd_soc_8_16_read_i2c NULL
417#endif
17a52fd6 418
994dc424
BS
419#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
420static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
421 unsigned int r)
422{
423 struct i2c_msg xfer[2];
424 u16 reg = r;
425 u8 data;
426 int ret;
427 struct i2c_client *client = codec->control_data;
428
429 /* Write register */
430 xfer[0].addr = client->addr;
431 xfer[0].flags = 0;
432 xfer[0].len = 2;
433 xfer[0].buf = (u8 *)&reg;
434
435 /* Read data */
436 xfer[1].addr = client->addr;
437 xfer[1].flags = I2C_M_RD;
438 xfer[1].len = 1;
439 xfer[1].buf = &data;
440
441 ret = i2c_transfer(client->adapter, xfer, 2);
442 if (ret != 2) {
443 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
444 return 0;
445 }
446
447 return data;
448}
449#else
450#define snd_soc_16_8_read_i2c NULL
451#endif
452
453static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
454 unsigned int reg)
455{
7a30a3db
DP
456 int ret;
457 unsigned int val;
994dc424
BS
458
459 reg &= 0xff;
db49c146 460 if (reg >= codec->driver->reg_cache_size ||
dad8e7ae
DP
461 snd_soc_codec_volatile_register(codec, reg) ||
462 codec->cache_bypass) {
db49c146
DP
463 if (codec->cache_only)
464 return -1;
465
5aaa062c 466 BUG_ON(!codec->hw_read);
db49c146
DP
467 return codec->hw_read(codec, reg);
468 }
469
7a30a3db
DP
470 ret = snd_soc_cache_read(codec, reg, &val);
471 if (ret < 0)
472 return -1;
473 return val;
994dc424
BS
474}
475
476static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
477 unsigned int value)
478{
994dc424
BS
479 u8 data[3];
480 int ret;
481
994dc424
BS
482 data[0] = (reg >> 8) & 0xff;
483 data[1] = reg & 0xff;
484 data[2] = value;
485
486 reg &= 0xff;
db49c146 487 if (!snd_soc_codec_volatile_register(codec, reg) &&
dad8e7ae
DP
488 reg < codec->driver->reg_cache_size &&
489 !codec->cache_bypass) {
7a30a3db
DP
490 ret = snd_soc_cache_write(codec, reg, value);
491 if (ret < 0)
492 return -1;
493 }
8c961bcc 494
a3032b47
MB
495 if (codec->cache_only) {
496 codec->cache_sync = 1;
8c961bcc 497 return 0;
a3032b47 498 }
8c961bcc 499
994dc424
BS
500 ret = codec->hw_write(codec->control_data, data, 3);
501 if (ret == 3)
502 return 0;
503 if (ret < 0)
504 return ret;
505 else
506 return -EIO;
507}
508
509#if defined(CONFIG_SPI_MASTER)
510static int snd_soc_16_8_spi_write(void *control_data, const char *data,
511 int len)
512{
513 struct spi_device *spi = control_data;
514 struct spi_transfer t;
515 struct spi_message m;
516 u8 msg[3];
517
518 if (len <= 0)
519 return 0;
520
521 msg[0] = data[0];
522 msg[1] = data[1];
523 msg[2] = data[2];
524
525 spi_message_init(&m);
465d7fcc 526 memset(&t, 0, sizeof t);
994dc424
BS
527
528 t.tx_buf = &msg[0];
529 t.len = len;
530
531 spi_message_add_tail(&t, &m);
532 spi_sync(spi, &m);
533
534 return len;
535}
536#else
537#define snd_soc_16_8_spi_write NULL
538#endif
539
bc6552f4
MB
540#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
541static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
542 unsigned int r)
543{
544 struct i2c_msg xfer[2];
545 u16 reg = cpu_to_be16(r);
546 u16 data;
547 int ret;
548 struct i2c_client *client = codec->control_data;
549
550 /* Write register */
551 xfer[0].addr = client->addr;
552 xfer[0].flags = 0;
553 xfer[0].len = 2;
554 xfer[0].buf = (u8 *)&reg;
555
556 /* Read data */
557 xfer[1].addr = client->addr;
558 xfer[1].flags = I2C_M_RD;
559 xfer[1].len = 2;
560 xfer[1].buf = (u8 *)&data;
561
562 ret = i2c_transfer(client->adapter, xfer, 2);
563 if (ret != 2) {
564 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
565 return 0;
566 }
567
568 return be16_to_cpu(data);
569}
570#else
571#define snd_soc_16_16_read_i2c NULL
572#endif
573
574static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
575 unsigned int reg)
576{
7a30a3db
DP
577 int ret;
578 unsigned int val;
bc6552f4 579
f0fba2ad 580 if (reg >= codec->driver->reg_cache_size ||
dad8e7ae
DP
581 snd_soc_codec_volatile_register(codec, reg) ||
582 codec->cache_bypass) {
bc6552f4 583 if (codec->cache_only)
391d8a04 584 return -1;
bc6552f4 585
5aaa062c 586 BUG_ON(!codec->hw_read);
bc6552f4
MB
587 return codec->hw_read(codec, reg);
588 }
589
7a30a3db
DP
590 ret = snd_soc_cache_read(codec, reg, &val);
591 if (ret < 0)
592 return -1;
593
594 return val;
bc6552f4
MB
595}
596
597static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
598 unsigned int value)
599{
bc6552f4
MB
600 u8 data[4];
601 int ret;
602
603 data[0] = (reg >> 8) & 0xff;
604 data[1] = reg & 0xff;
605 data[2] = (value >> 8) & 0xff;
606 data[3] = value & 0xff;
607
db49c146 608 if (!snd_soc_codec_volatile_register(codec, reg) &&
dad8e7ae
DP
609 reg < codec->driver->reg_cache_size &&
610 !codec->cache_bypass) {
7a30a3db
DP
611 ret = snd_soc_cache_write(codec, reg, value);
612 if (ret < 0)
613 return -1;
614 }
bc6552f4
MB
615
616 if (codec->cache_only) {
617 codec->cache_sync = 1;
618 return 0;
619 }
620
621 ret = codec->hw_write(codec->control_data, data, 4);
622 if (ret == 4)
623 return 0;
624 if (ret < 0)
625 return ret;
626 else
627 return -EIO;
628}
994dc424 629
f479fd93
DP
630#if defined(CONFIG_SPI_MASTER)
631static int snd_soc_16_16_spi_write(void *control_data, const char *data,
632 int len)
633{
634 struct spi_device *spi = control_data;
635 struct spi_transfer t;
636 struct spi_message m;
637 u8 msg[4];
638
639 if (len <= 0)
640 return 0;
641
642 msg[0] = data[0];
643 msg[1] = data[1];
644 msg[2] = data[2];
645 msg[3] = data[3];
646
647 spi_message_init(&m);
465d7fcc 648 memset(&t, 0, sizeof t);
f479fd93
DP
649
650 t.tx_buf = &msg[0];
651 t.len = len;
652
653 spi_message_add_tail(&t, &m);
654 spi_sync(spi, &m);
655
656 return len;
657}
658#else
659#define snd_soc_16_16_spi_write NULL
660#endif
661
17a52fd6
MB
662static struct {
663 int addr_bits;
664 int data_bits;
afa2f106 665 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
27ded041 666 int (*spi_write)(void *, const char *, int);
17a52fd6 667 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
afa2f106 668 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
17a52fd6 669} io_types[] = {
63b62ab0
BS
670 {
671 .addr_bits = 4, .data_bits = 12,
672 .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
673 .spi_write = snd_soc_4_12_spi_write,
674 },
d62ab358
MB
675 {
676 .addr_bits = 7, .data_bits = 9,
677 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
8998c899 678 .spi_write = snd_soc_7_9_spi_write,
d62ab358
MB
679 },
680 {
681 .addr_bits = 8, .data_bits = 8,
682 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
85dfcdff 683 .i2c_read = snd_soc_8_8_read_i2c,
f479fd93 684 .spi_write = snd_soc_8_8_spi_write,
d62ab358
MB
685 },
686 {
687 .addr_bits = 8, .data_bits = 16,
688 .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
689 .i2c_read = snd_soc_8_16_read_i2c,
f479fd93 690 .spi_write = snd_soc_8_16_spi_write,
d62ab358 691 },
994dc424
BS
692 {
693 .addr_bits = 16, .data_bits = 8,
694 .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
695 .i2c_read = snd_soc_16_8_read_i2c,
696 .spi_write = snd_soc_16_8_spi_write,
697 },
bc6552f4
MB
698 {
699 .addr_bits = 16, .data_bits = 16,
700 .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
701 .i2c_read = snd_soc_16_16_read_i2c,
f479fd93 702 .spi_write = snd_soc_16_16_spi_write,
bc6552f4 703 },
17a52fd6
MB
704};
705
706/**
707 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
708 *
709 * @codec: CODEC to configure.
710 * @type: Type of cache.
711 * @addr_bits: Number of bits of register address data.
712 * @data_bits: Number of bits of data per register.
7084a42b 713 * @control: Control bus used.
17a52fd6
MB
714 *
715 * Register formats are frequently shared between many I2C and SPI
716 * devices. In order to promote code reuse the ASoC core provides
717 * some standard implementations of CODEC read and write operations
718 * which can be set up using this function.
719 *
720 * The caller is responsible for allocating and initialising the
721 * actual cache.
722 *
723 * Note that at present this code cannot be used by CODECs with
724 * volatile registers.
725 */
726int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
7084a42b
MB
727 int addr_bits, int data_bits,
728 enum snd_soc_control_type control)
17a52fd6
MB
729{
730 int i;
731
17a52fd6
MB
732 for (i = 0; i < ARRAY_SIZE(io_types); i++)
733 if (io_types[i].addr_bits == addr_bits &&
734 io_types[i].data_bits == data_bits)
735 break;
736 if (i == ARRAY_SIZE(io_types)) {
737 printk(KERN_ERR
738 "No I/O functions for %d bit address %d bit data\n",
739 addr_bits, data_bits);
740 return -EINVAL;
741 }
742
c3acec26
MB
743 codec->write = io_types[i].write;
744 codec->read = io_types[i].read;
17a52fd6 745
7084a42b
MB
746 switch (control) {
747 case SND_SOC_CUSTOM:
748 break;
749
750 case SND_SOC_I2C:
17244c24 751#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
7084a42b
MB
752 codec->hw_write = (hw_write_t)i2c_master_send;
753#endif
afa2f106
MB
754 if (io_types[i].i2c_read)
755 codec->hw_read = io_types[i].i2c_read;
a6d14342
MB
756
757 codec->control_data = container_of(codec->dev,
758 struct i2c_client,
759 dev);
7084a42b
MB
760 break;
761
762 case SND_SOC_SPI:
27ded041
MB
763 if (io_types[i].spi_write)
764 codec->hw_write = io_types[i].spi_write;
a6d14342
MB
765
766 codec->control_data = container_of(codec->dev,
767 struct spi_device,
768 dev);
7084a42b
MB
769 break;
770 }
771
17a52fd6
MB
772 return 0;
773}
774EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
7a30a3db 775
1321e883
DP
776static bool snd_soc_set_cache_val(void *base, unsigned int idx,
777 unsigned int val, unsigned int word_size)
778{
779 switch (word_size) {
780 case 1: {
781 u8 *cache = base;
782 if (cache[idx] == val)
783 return true;
784 cache[idx] = val;
785 break;
786 }
787 case 2: {
788 u16 *cache = base;
789 if (cache[idx] == val)
790 return true;
791 cache[idx] = val;
792 break;
793 }
794 default:
795 BUG();
796 }
797 return false;
798}
799
800static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
801 unsigned int word_size)
802{
803 switch (word_size) {
804 case 1: {
805 const u8 *cache = base;
806 return cache[idx];
807 }
808 case 2: {
809 const u16 *cache = base;
810 return cache[idx];
811 }
812 default:
813 BUG();
814 }
815 /* unreachable */
816 return -1;
817}
818
a7f387d5
DP
819struct snd_soc_rbtree_node {
820 struct rb_node node;
821 unsigned int reg;
822 unsigned int value;
823 unsigned int defval;
824} __attribute__ ((packed));
825
826struct snd_soc_rbtree_ctx {
827 struct rb_root root;
828};
829
830static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
831 struct rb_root *root, unsigned int reg)
832{
833 struct rb_node *node;
834 struct snd_soc_rbtree_node *rbnode;
835
836 node = root->rb_node;
837 while (node) {
838 rbnode = container_of(node, struct snd_soc_rbtree_node, node);
839 if (rbnode->reg < reg)
840 node = node->rb_left;
841 else if (rbnode->reg > reg)
842 node = node->rb_right;
843 else
844 return rbnode;
845 }
846
847 return NULL;
848}
849
a7f387d5
DP
850static int snd_soc_rbtree_insert(struct rb_root *root,
851 struct snd_soc_rbtree_node *rbnode)
852{
853 struct rb_node **new, *parent;
854 struct snd_soc_rbtree_node *rbnode_tmp;
855
856 parent = NULL;
857 new = &root->rb_node;
858 while (*new) {
859 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
860 node);
861 parent = *new;
862 if (rbnode_tmp->reg < rbnode->reg)
863 new = &((*new)->rb_left);
864 else if (rbnode_tmp->reg > rbnode->reg)
865 new = &((*new)->rb_right);
866 else
867 return 0;
868 }
869
870 /* insert the node into the rbtree */
871 rb_link_node(&rbnode->node, parent, new);
872 rb_insert_color(&rbnode->node, root);
873
874 return 1;
875}
876
877static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
878{
879 struct snd_soc_rbtree_ctx *rbtree_ctx;
880 struct rb_node *node;
881 struct snd_soc_rbtree_node *rbnode;
882 unsigned int val;
7a33d4ce 883 int ret;
a7f387d5
DP
884
885 rbtree_ctx = codec->reg_cache;
886 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
887 rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
888 if (rbnode->value == rbnode->defval)
889 continue;
7a33d4ce
DP
890 ret = snd_soc_cache_read(codec, rbnode->reg, &val);
891 if (ret)
892 return ret;
9978007b 893 codec->cache_bypass = 1;
7a33d4ce 894 ret = snd_soc_write(codec, rbnode->reg, val);
9978007b 895 codec->cache_bypass = 0;
7a33d4ce
DP
896 if (ret)
897 return ret;
a7f387d5
DP
898 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
899 rbnode->reg, val);
900 }
901
902 return 0;
903}
904
905static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
906 unsigned int reg, unsigned int value)
907{
908 struct snd_soc_rbtree_ctx *rbtree_ctx;
909 struct snd_soc_rbtree_node *rbnode;
910
911 rbtree_ctx = codec->reg_cache;
912 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
913 if (rbnode) {
914 if (rbnode->value == value)
915 return 0;
916 rbnode->value = value;
917 } else {
918 /* bail out early, no need to create the rbnode yet */
919 if (!value)
920 return 0;
921 /*
922 * for uninitialized registers whose value is changed
923 * from the default zero, create an rbnode and insert
924 * it into the tree.
925 */
926 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
927 if (!rbnode)
928 return -ENOMEM;
929 rbnode->reg = reg;
930 rbnode->value = value;
931 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
932 }
933
934 return 0;
935}
936
937static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
938 unsigned int reg, unsigned int *value)
939{
940 struct snd_soc_rbtree_ctx *rbtree_ctx;
941 struct snd_soc_rbtree_node *rbnode;
942
943 rbtree_ctx = codec->reg_cache;
944 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
945 if (rbnode) {
946 *value = rbnode->value;
947 } else {
948 /* uninitialized registers default to 0 */
949 *value = 0;
950 }
951
952 return 0;
953}
954
955static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
956{
957 struct rb_node *next;
958 struct snd_soc_rbtree_ctx *rbtree_ctx;
959 struct snd_soc_rbtree_node *rbtree_node;
960
961 /* if we've already been called then just return */
962 rbtree_ctx = codec->reg_cache;
963 if (!rbtree_ctx)
964 return 0;
965
966 /* free up the rbtree */
967 next = rb_first(&rbtree_ctx->root);
968 while (next) {
969 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
970 next = rb_next(&rbtree_node->node);
971 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
972 kfree(rbtree_node);
973 }
974
975 /* release the resources */
976 kfree(codec->reg_cache);
977 codec->reg_cache = NULL;
978
979 return 0;
980}
981
982static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
983{
1321e883 984 struct snd_soc_rbtree_node *rbtree_node;
a7f387d5 985 struct snd_soc_rbtree_ctx *rbtree_ctx;
1321e883
DP
986 unsigned int val;
987 unsigned int word_size;
988 int i;
989 int ret;
a7f387d5
DP
990
991 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
992 if (!codec->reg_cache)
993 return -ENOMEM;
994
995 rbtree_ctx = codec->reg_cache;
996 rbtree_ctx->root = RB_ROOT;
997
3335ddca 998 if (!codec->reg_def_copy)
a7f387d5
DP
999 return 0;
1000
1321e883
DP
1001 /*
1002 * populate the rbtree with the initialized registers. All other
1003 * registers will be inserted when they are first modified.
1004 */
1005 word_size = codec->driver->reg_word_size;
1006 for (i = 0; i < codec->driver->reg_cache_size; ++i) {
1007 val = snd_soc_get_cache_val(codec->reg_def_copy, i, word_size);
1008 if (!val)
1009 continue;
1010 rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL);
1011 if (!rbtree_node) {
1012 ret = -ENOMEM;
1013 snd_soc_cache_exit(codec);
1014 break;
1015 }
1016 rbtree_node->reg = i;
1017 rbtree_node->value = val;
1018 rbtree_node->defval = val;
1019 snd_soc_rbtree_insert(&rbtree_ctx->root, rbtree_node);
a7f387d5
DP
1020 }
1021
1022 return 0;
1023}
1024
68d44ee0 1025#ifdef CONFIG_SND_SOC_CACHE_LZO
cc28fb8e
DP
1026struct snd_soc_lzo_ctx {
1027 void *wmem;
1028 void *dst;
1029 const void *src;
1030 size_t src_len;
1031 size_t dst_len;
1032 size_t decompressed_size;
1033 unsigned long *sync_bmp;
1034 int sync_bmp_nbits;
1035};
1036
1037#define LZO_BLOCK_NUM 8
1038static int snd_soc_lzo_block_count(void)
1039{
1040 return LZO_BLOCK_NUM;
1041}
1042
1043static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
1044{
1045 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
1046 if (!lzo_ctx->wmem)
1047 return -ENOMEM;
1048 return 0;
1049}
1050
1051static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
1052{
1053 size_t compress_size;
1054 int ret;
1055
1056 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
1057 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
1058 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
1059 return -EINVAL;
1060 lzo_ctx->dst_len = compress_size;
1061 return 0;
1062}
1063
1064static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
1065{
1066 size_t dst_len;
1067 int ret;
1068
1069 dst_len = lzo_ctx->dst_len;
1070 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
1071 lzo_ctx->dst, &dst_len);
1072 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
1073 return -EINVAL;
1074 return 0;
1075}
1076
1077static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
1078 struct snd_soc_lzo_ctx *lzo_ctx)
1079{
1080 int ret;
1081
1082 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
1083 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1084 if (!lzo_ctx->dst) {
1085 lzo_ctx->dst_len = 0;
1086 return -ENOMEM;
1087 }
1088
1089 ret = snd_soc_lzo_compress(lzo_ctx);
1090 if (ret < 0)
1091 return ret;
1092 return 0;
1093}
1094
1095static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
1096 struct snd_soc_lzo_ctx *lzo_ctx)
1097{
1098 int ret;
1099
1100 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
1101 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1102 if (!lzo_ctx->dst) {
1103 lzo_ctx->dst_len = 0;
1104 return -ENOMEM;
1105 }
1106
1107 ret = snd_soc_lzo_decompress(lzo_ctx);
1108 if (ret < 0)
1109 return ret;
1110 return 0;
1111}
1112
1113static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
1114 unsigned int reg)
1115{
001ae4c0 1116 const struct snd_soc_codec_driver *codec_drv;
cc28fb8e
DP
1117
1118 codec_drv = codec->driver;
cc28fb8e 1119 return (reg * codec_drv->reg_word_size) /
aea170a0 1120 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
cc28fb8e
DP
1121}
1122
1123static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
1124 unsigned int reg)
1125{
001ae4c0 1126 const struct snd_soc_codec_driver *codec_drv;
cc28fb8e
DP
1127
1128 codec_drv = codec->driver;
aea170a0 1129 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
cc28fb8e
DP
1130 codec_drv->reg_word_size);
1131}
1132
1133static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
1134{
001ae4c0 1135 const struct snd_soc_codec_driver *codec_drv;
cc28fb8e
DP
1136
1137 codec_drv = codec->driver;
aea170a0 1138 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
cc28fb8e
DP
1139}
1140
1141static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
1142{
1143 struct snd_soc_lzo_ctx **lzo_blocks;
1144 unsigned int val;
1145 int i;
7a33d4ce 1146 int ret;
cc28fb8e
DP
1147
1148 lzo_blocks = codec->reg_cache;
1149 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
7a33d4ce
DP
1150 ret = snd_soc_cache_read(codec, i, &val);
1151 if (ret)
1152 return ret;
9978007b 1153 codec->cache_bypass = 1;
7a33d4ce 1154 ret = snd_soc_write(codec, i, val);
9978007b 1155 codec->cache_bypass = 0;
7a33d4ce
DP
1156 if (ret)
1157 return ret;
cc28fb8e
DP
1158 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1159 i, val);
1160 }
1161
1162 return 0;
1163}
1164
1165static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
1166 unsigned int reg, unsigned int value)
1167{
1168 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1169 int ret, blkindex, blkpos;
1170 size_t blksize, tmp_dst_len;
1171 void *tmp_dst;
1172
1173 /* index of the compressed lzo block */
1174 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1175 /* register index within the decompressed block */
1176 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1177 /* size of the compressed block */
1178 blksize = snd_soc_lzo_get_blksize(codec);
1179 lzo_blocks = codec->reg_cache;
1180 lzo_block = lzo_blocks[blkindex];
1181
1182 /* save the pointer and length of the compressed block */
1183 tmp_dst = lzo_block->dst;
1184 tmp_dst_len = lzo_block->dst_len;
1185
1186 /* prepare the source to be the compressed block */
1187 lzo_block->src = lzo_block->dst;
1188 lzo_block->src_len = lzo_block->dst_len;
1189
1190 /* decompress the block */
1191 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1192 if (ret < 0) {
1193 kfree(lzo_block->dst);
1194 goto out;
1195 }
1196
1197 /* write the new value to the cache */
1321e883
DP
1198 if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
1199 codec->driver->reg_word_size)) {
1200 kfree(lzo_block->dst);
1201 goto out;
cc28fb8e
DP
1202 }
1203
1204 /* prepare the source to be the decompressed block */
1205 lzo_block->src = lzo_block->dst;
1206 lzo_block->src_len = lzo_block->dst_len;
1207
1208 /* compress the block */
1209 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1210 if (ret < 0) {
1211 kfree(lzo_block->dst);
1212 kfree(lzo_block->src);
1213 goto out;
1214 }
1215
1216 /* set the bit so we know we have to sync this register */
1217 set_bit(reg, lzo_block->sync_bmp);
1218 kfree(tmp_dst);
1219 kfree(lzo_block->src);
1220 return 0;
1221out:
1222 lzo_block->dst = tmp_dst;
1223 lzo_block->dst_len = tmp_dst_len;
1224 return ret;
1225}
1226
1227static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1228 unsigned int reg, unsigned int *value)
1229{
1230 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1231 int ret, blkindex, blkpos;
1232 size_t blksize, tmp_dst_len;
1233 void *tmp_dst;
1234
1235 *value = 0;
1236 /* index of the compressed lzo block */
1237 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1238 /* register index within the decompressed block */
1239 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1240 /* size of the compressed block */
1241 blksize = snd_soc_lzo_get_blksize(codec);
1242 lzo_blocks = codec->reg_cache;
1243 lzo_block = lzo_blocks[blkindex];
1244
1245 /* save the pointer and length of the compressed block */
1246 tmp_dst = lzo_block->dst;
1247 tmp_dst_len = lzo_block->dst_len;
1248
1249 /* prepare the source to be the compressed block */
1250 lzo_block->src = lzo_block->dst;
1251 lzo_block->src_len = lzo_block->dst_len;
1252
1253 /* decompress the block */
1254 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1321e883 1255 if (ret >= 0)
cc28fb8e 1256 /* fetch the value from the cache */
1321e883
DP
1257 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1258 codec->driver->reg_word_size);
cc28fb8e
DP
1259
1260 kfree(lzo_block->dst);
1261 /* restore the pointer and length of the compressed block */
1262 lzo_block->dst = tmp_dst;
1263 lzo_block->dst_len = tmp_dst_len;
1264 return 0;
1265}
1266
1267static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1268{
1269 struct snd_soc_lzo_ctx **lzo_blocks;
1270 int i, blkcount;
1271
1272 lzo_blocks = codec->reg_cache;
1273 if (!lzo_blocks)
1274 return 0;
1275
1276 blkcount = snd_soc_lzo_block_count();
1277 /*
1278 * the pointer to the bitmap used for syncing the cache
1279 * is shared amongst all lzo_blocks. Ensure it is freed
1280 * only once.
1281 */
1282 if (lzo_blocks[0])
1283 kfree(lzo_blocks[0]->sync_bmp);
1284 for (i = 0; i < blkcount; ++i) {
1285 if (lzo_blocks[i]) {
1286 kfree(lzo_blocks[i]->wmem);
1287 kfree(lzo_blocks[i]->dst);
1288 }
1289 /* each lzo_block is a pointer returned by kmalloc or NULL */
1290 kfree(lzo_blocks[i]);
1291 }
1292 kfree(lzo_blocks);
1293 codec->reg_cache = NULL;
1294 return 0;
1295}
1296
1297static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1298{
1299 struct snd_soc_lzo_ctx **lzo_blocks;
aea170a0 1300 size_t bmp_size;
001ae4c0 1301 const struct snd_soc_codec_driver *codec_drv;
cc28fb8e
DP
1302 int ret, tofree, i, blksize, blkcount;
1303 const char *p, *end;
1304 unsigned long *sync_bmp;
1305
1306 ret = 0;
1307 codec_drv = codec->driver;
cc28fb8e
DP
1308
1309 /*
1310 * If we have not been given a default register cache
1311 * then allocate a dummy zero-ed out region, compress it
1312 * and remember to free it afterwards.
1313 */
1314 tofree = 0;
3335ddca 1315 if (!codec->reg_def_copy)
cc28fb8e
DP
1316 tofree = 1;
1317
3335ddca 1318 if (!codec->reg_def_copy) {
aea170a0 1319 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
3335ddca 1320 if (!codec->reg_def_copy)
cc28fb8e
DP
1321 return -ENOMEM;
1322 }
1323
1324 blkcount = snd_soc_lzo_block_count();
1325 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1326 GFP_KERNEL);
1327 if (!codec->reg_cache) {
1328 ret = -ENOMEM;
1329 goto err_tofree;
1330 }
1331 lzo_blocks = codec->reg_cache;
1332
1333 /*
1334 * allocate a bitmap to be used when syncing the cache with
1335 * the hardware. Each time a register is modified, the corresponding
1336 * bit is set in the bitmap, so we know that we have to sync
1337 * that register.
1338 */
1339 bmp_size = codec_drv->reg_cache_size;
465d7fcc 1340 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
cc28fb8e
DP
1341 GFP_KERNEL);
1342 if (!sync_bmp) {
1343 ret = -ENOMEM;
1344 goto err;
1345 }
09c74a9d 1346 bitmap_zero(sync_bmp, bmp_size);
cc28fb8e
DP
1347
1348 /* allocate the lzo blocks and initialize them */
1349 for (i = 0; i < blkcount; ++i) {
1350 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1351 GFP_KERNEL);
1352 if (!lzo_blocks[i]) {
1353 kfree(sync_bmp);
1354 ret = -ENOMEM;
1355 goto err;
1356 }
1357 lzo_blocks[i]->sync_bmp = sync_bmp;
04f8fd17 1358 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
cc28fb8e
DP
1359 /* alloc the working space for the compressed block */
1360 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1361 if (ret < 0)
1362 goto err;
1363 }
1364
1365 blksize = snd_soc_lzo_get_blksize(codec);
3335ddca 1366 p = codec->reg_def_copy;
aea170a0 1367 end = codec->reg_def_copy + codec->reg_size;
cc28fb8e
DP
1368 /* compress the register map and fill the lzo blocks */
1369 for (i = 0; i < blkcount; ++i, p += blksize) {
1370 lzo_blocks[i]->src = p;
1371 if (p + blksize > end)
1372 lzo_blocks[i]->src_len = end - p;
1373 else
1374 lzo_blocks[i]->src_len = blksize;
1375 ret = snd_soc_lzo_compress_cache_block(codec,
1376 lzo_blocks[i]);
1377 if (ret < 0)
1378 goto err;
1379 lzo_blocks[i]->decompressed_size =
1380 lzo_blocks[i]->src_len;
1381 }
1382
3335ddca
DP
1383 if (tofree) {
1384 kfree(codec->reg_def_copy);
1385 codec->reg_def_copy = NULL;
1386 }
cc28fb8e
DP
1387 return 0;
1388err:
1389 snd_soc_cache_exit(codec);
1390err_tofree:
3335ddca
DP
1391 if (tofree) {
1392 kfree(codec->reg_def_copy);
1393 codec->reg_def_copy = NULL;
1394 }
cc28fb8e
DP
1395 return ret;
1396}
68d44ee0 1397#endif
cc28fb8e 1398
7a30a3db
DP
1399static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1400{
1401 int i;
7a33d4ce 1402 int ret;
001ae4c0 1403 const struct snd_soc_codec_driver *codec_drv;
7a30a3db
DP
1404 unsigned int val;
1405
1406 codec_drv = codec->driver;
1407 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
7a33d4ce
DP
1408 ret = snd_soc_cache_read(codec, i, &val);
1409 if (ret)
1410 return ret;
d779fce5
DP
1411 if (codec->reg_def_copy)
1412 if (snd_soc_get_cache_val(codec->reg_def_copy,
1321e883
DP
1413 i, codec_drv->reg_word_size) == val)
1414 continue;
7a33d4ce
DP
1415 ret = snd_soc_write(codec, i, val);
1416 if (ret)
1417 return ret;
7a30a3db
DP
1418 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1419 i, val);
1420 }
1421 return 0;
1422}
1423
1424static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1425 unsigned int reg, unsigned int value)
1426{
1321e883
DP
1427 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1428 codec->driver->reg_word_size);
7a30a3db
DP
1429 return 0;
1430}
1431
1432static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1433 unsigned int reg, unsigned int *value)
1434{
1321e883
DP
1435 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1436 codec->driver->reg_word_size);
7a30a3db
DP
1437 return 0;
1438}
1439
1440static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1441{
1442 if (!codec->reg_cache)
1443 return 0;
1444 kfree(codec->reg_cache);
1445 codec->reg_cache = NULL;
1446 return 0;
1447}
1448
1449static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1450{
001ae4c0 1451 const struct snd_soc_codec_driver *codec_drv;
7a30a3db
DP
1452
1453 codec_drv = codec->driver;
7a30a3db 1454
d779fce5
DP
1455 if (codec->reg_def_copy)
1456 codec->reg_cache = kmemdup(codec->reg_def_copy,
aea170a0 1457 codec->reg_size, GFP_KERNEL);
7a30a3db 1458 else
aea170a0 1459 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
7a30a3db
DP
1460 if (!codec->reg_cache)
1461 return -ENOMEM;
1462
1463 return 0;
1464}
1465
1466/* an array of all supported compression types */
1467static const struct snd_soc_cache_ops cache_types[] = {
be4fcddd 1468 /* Flat *must* be the first entry for fallback */
7a30a3db 1469 {
df0701bb 1470 .id = SND_SOC_FLAT_COMPRESSION,
0d735eaa 1471 .name = "flat",
7a30a3db
DP
1472 .init = snd_soc_flat_cache_init,
1473 .exit = snd_soc_flat_cache_exit,
1474 .read = snd_soc_flat_cache_read,
1475 .write = snd_soc_flat_cache_write,
1476 .sync = snd_soc_flat_cache_sync
cc28fb8e 1477 },
68d44ee0 1478#ifdef CONFIG_SND_SOC_CACHE_LZO
cc28fb8e
DP
1479 {
1480 .id = SND_SOC_LZO_COMPRESSION,
0d735eaa 1481 .name = "LZO",
cc28fb8e
DP
1482 .init = snd_soc_lzo_cache_init,
1483 .exit = snd_soc_lzo_cache_exit,
1484 .read = snd_soc_lzo_cache_read,
1485 .write = snd_soc_lzo_cache_write,
1486 .sync = snd_soc_lzo_cache_sync
a7f387d5 1487 },
68d44ee0 1488#endif
a7f387d5
DP
1489 {
1490 .id = SND_SOC_RBTREE_COMPRESSION,
0d735eaa 1491 .name = "rbtree",
a7f387d5
DP
1492 .init = snd_soc_rbtree_cache_init,
1493 .exit = snd_soc_rbtree_cache_exit,
1494 .read = snd_soc_rbtree_cache_read,
1495 .write = snd_soc_rbtree_cache_write,
1496 .sync = snd_soc_rbtree_cache_sync
7a30a3db
DP
1497 }
1498};
1499
1500int snd_soc_cache_init(struct snd_soc_codec *codec)
1501{
1502 int i;
1503
1504 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
23bbce34 1505 if (cache_types[i].id == codec->compress_type)
7a30a3db 1506 break;
be4fcddd
MB
1507
1508 /* Fall back to flat compression */
7a30a3db 1509 if (i == ARRAY_SIZE(cache_types)) {
be4fcddd
MB
1510 dev_warn(codec->dev, "Could not match compress type: %d\n",
1511 codec->compress_type);
1512 i = 0;
7a30a3db
DP
1513 }
1514
1515 mutex_init(&codec->cache_rw_mutex);
1516 codec->cache_ops = &cache_types[i];
1517
0d735eaa
DP
1518 if (codec->cache_ops->init) {
1519 if (codec->cache_ops->name)
1520 dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1521 codec->cache_ops->name, codec->name);
7a30a3db 1522 return codec->cache_ops->init(codec);
0d735eaa 1523 }
7a30a3db
DP
1524 return -EINVAL;
1525}
1526
1527/*
1528 * NOTE: keep in mind that this function might be called
1529 * multiple times.
1530 */
1531int snd_soc_cache_exit(struct snd_soc_codec *codec)
1532{
0d735eaa
DP
1533 if (codec->cache_ops && codec->cache_ops->exit) {
1534 if (codec->cache_ops->name)
1535 dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1536 codec->cache_ops->name, codec->name);
7a30a3db 1537 return codec->cache_ops->exit(codec);
0d735eaa 1538 }
7a30a3db
DP
1539 return -EINVAL;
1540}
1541
1542/**
1543 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1544 *
1545 * @codec: CODEC to configure.
1546 * @reg: The register index.
1547 * @value: The value to be returned.
1548 */
1549int snd_soc_cache_read(struct snd_soc_codec *codec,
1550 unsigned int reg, unsigned int *value)
1551{
1552 int ret;
1553
1554 mutex_lock(&codec->cache_rw_mutex);
1555
1556 if (value && codec->cache_ops && codec->cache_ops->read) {
1557 ret = codec->cache_ops->read(codec, reg, value);
1558 mutex_unlock(&codec->cache_rw_mutex);
1559 return ret;
1560 }
1561
1562 mutex_unlock(&codec->cache_rw_mutex);
1563 return -EINVAL;
1564}
1565EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1566
1567/**
1568 * snd_soc_cache_write: Set the value of a given register in the cache.
1569 *
1570 * @codec: CODEC to configure.
1571 * @reg: The register index.
1572 * @value: The new register value.
1573 */
1574int snd_soc_cache_write(struct snd_soc_codec *codec,
1575 unsigned int reg, unsigned int value)
1576{
1577 int ret;
1578
1579 mutex_lock(&codec->cache_rw_mutex);
1580
1581 if (codec->cache_ops && codec->cache_ops->write) {
1582 ret = codec->cache_ops->write(codec, reg, value);
1583 mutex_unlock(&codec->cache_rw_mutex);
1584 return ret;
1585 }
1586
1587 mutex_unlock(&codec->cache_rw_mutex);
1588 return -EINVAL;
1589}
1590EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1591
1592/**
1593 * snd_soc_cache_sync: Sync the register cache with the hardware.
1594 *
1595 * @codec: CODEC to configure.
1596 *
1597 * Any registers that should not be synced should be marked as
1598 * volatile. In general drivers can choose not to use the provided
1599 * syncing functionality if they so require.
1600 */
1601int snd_soc_cache_sync(struct snd_soc_codec *codec)
1602{
1603 int ret;
1604
1605 if (!codec->cache_sync) {
1606 return 0;
1607 }
1608
1609 if (codec->cache_ops && codec->cache_ops->sync) {
0d735eaa
DP
1610 if (codec->cache_ops->name)
1611 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1612 codec->cache_ops->name, codec->name);
7a30a3db
DP
1613 ret = codec->cache_ops->sync(codec);
1614 if (!ret)
1615 codec->cache_sync = 0;
1616 return ret;
1617 }
1618
1619 return -EINVAL;
1620}
1621EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
066d16c3
DP
1622
1623static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1624 unsigned int reg)
1625{
1626 const struct snd_soc_codec_driver *codec_drv;
1627 unsigned int min, max, index;
1628
1629 codec_drv = codec->driver;
1630 min = 0;
1631 max = codec_drv->reg_access_size - 1;
1632 do {
1633 index = (min + max) / 2;
1634 if (codec_drv->reg_access_default[index].reg == reg)
1635 return index;
1636 if (codec_drv->reg_access_default[index].reg < reg)
1637 min = index + 1;
1638 else
1639 max = index;
1640 } while (min <= max);
1641 return -1;
1642}
1643
1644int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1645 unsigned int reg)
1646{
1647 int index;
1648
1649 if (reg >= codec->driver->reg_cache_size)
1650 return 1;
1651 index = snd_soc_get_reg_access_index(codec, reg);
1652 if (index < 0)
1653 return 0;
1654 return codec->driver->reg_access_default[index].vol;
1655}
1656EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1657
1658int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1659 unsigned int reg)
1660{
1661 int index;
1662
1663 if (reg >= codec->driver->reg_cache_size)
1664 return 1;
1665 index = snd_soc_get_reg_access_index(codec, reg);
1666 if (index < 0)
1667 return 0;
1668 return codec->driver->reg_access_default[index].read;
1669}
1670EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);