blob: 1ebff9f12b4e924666bd830c754ae63fdcb2c113 [file] [log] [blame]
Mark Brown17a52fd2009-07-05 17:24:50 +01001/*
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
Mark Brown7084a422009-07-10 22:24:27 +010014#include <linux/i2c.h>
Mark Brown27ded042009-07-10 23:28:16 +010015#include <linux/spi/spi.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010016#include <sound/soc.h>
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +000017#include <linux/lzo.h>
18#include <linux/bitmap.h>
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +000019#include <linux/rbtree.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010020
Barry Song63b62ab2010-01-27 11:46:17 +080021static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
22 unsigned int reg)
23{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000024 int ret;
25 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010026
27 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +000028 snd_soc_codec_volatile_register(codec, reg) ||
29 codec->cache_bypass) {
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010030 if (codec->cache_only)
31 return -1;
32
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +000033 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010034 return codec->hw_read(codec, reg);
35 }
36
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000037 ret = snd_soc_cache_read(codec, reg, &val);
38 if (ret < 0)
39 return -1;
40 return val;
Barry Song63b62ab2010-01-27 11:46:17 +080041}
42
43static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
44 unsigned int value)
45{
Barry Song63b62ab2010-01-27 11:46:17 +080046 u8 data[2];
47 int ret;
48
Barry Song63b62ab2010-01-27 11:46:17 +080049 data[0] = (reg << 4) | ((value >> 8) & 0x000f);
50 data[1] = value & 0x00ff;
51
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010052 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +000053 reg < codec->driver->reg_cache_size &&
54 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000055 ret = snd_soc_cache_write(codec, reg, value);
56 if (ret < 0)
57 return -1;
58 }
Mark Brown8c961bc2010-02-01 18:46:10 +000059
Mark Browna3032b42010-02-01 18:48:03 +000060 if (codec->cache_only) {
61 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +000062 return 0;
Mark Browna3032b42010-02-01 18:48:03 +000063 }
Mark Brown8c961bc2010-02-01 18:46:10 +000064
Barry Song63b62ab2010-01-27 11:46:17 +080065 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);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +000090 memset(&t, 0, sizeof t);
Barry Song63b62ab2010-01-27 11:46:17 +080091
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
Mark Brown17a52fd2009-07-05 17:24:50 +0100104static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
105 unsigned int reg)
106{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000107 int ret;
108 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100109
110 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000111 snd_soc_codec_volatile_register(codec, reg) ||
112 codec->cache_bypass) {
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100113 if (codec->cache_only)
114 return -1;
115
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000116 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100117 return codec->hw_read(codec, reg);
118 }
119
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000120 ret = snd_soc_cache_read(codec, reg, &val);
121 if (ret < 0)
122 return -1;
123 return val;
Mark Brown17a52fd2009-07-05 17:24:50 +0100124}
125
126static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
127 unsigned int value)
128{
Mark Brown17a52fd2009-07-05 17:24:50 +0100129 u8 data[2];
130 int ret;
131
Mark Brown17a52fd2009-07-05 17:24:50 +0100132 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
133 data[1] = value & 0x00ff;
134
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100135 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000136 reg < codec->driver->reg_cache_size &&
137 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000138 ret = snd_soc_cache_write(codec, reg, value);
139 if (ret < 0)
140 return -1;
141 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000142
Mark Browna3032b42010-02-01 18:48:03 +0000143 if (codec->cache_only) {
144 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000145 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000146 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000147
Mark Brown17a52fd2009-07-05 17:24:50 +0100148 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
Mark Brown27ded042009-07-10 23:28:16 +0100157#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);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000173 memset(&t, 0, sizeof t);
Mark Brown27ded042009-07-10 23:28:16 +0100174
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
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900187static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
188 unsigned int value)
189{
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900190 u8 data[2];
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000191 int ret;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900192
Barry Songf4bee1b2010-03-18 16:17:01 +0800193 reg &= 0xff;
194 data[0] = reg;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900195 data[1] = value & 0xff;
196
Dimitris Papastamos005d65f2010-09-22 16:16:06 +0100197 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000198 reg < codec->driver->reg_cache_size &&
199 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000200 ret = snd_soc_cache_write(codec, reg, value);
201 if (ret < 0)
202 return -1;
203 }
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900204
Mark Browna3032b42010-02-01 18:48:03 +0000205 if (codec->cache_only) {
206 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000207 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000208 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000209
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900210 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{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000219 int ret;
220 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100221
Barry Songf4bee1b2010-03-18 16:17:01 +0800222 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100223 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000224 snd_soc_codec_volatile_register(codec, reg) ||
225 codec->cache_bypass) {
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100226 if (codec->cache_only)
227 return -1;
228
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000229 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100230 return codec->hw_read(codec, reg);
231 }
232
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000233 ret = snd_soc_cache_read(codec, reg, &val);
234 if (ret < 0)
235 return -1;
236 return val;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900237}
238
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100239#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);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000255 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100256
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
Mark Brownafa2f102009-07-10 23:11:24 +0100269static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
270 unsigned int value)
271{
Mark Brownafa2f102009-07-10 23:11:24 +0100272 u8 data[3];
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000273 int ret;
Mark Brownafa2f102009-07-10 23:11:24 +0100274
275 data[0] = reg;
276 data[1] = (value >> 8) & 0xff;
277 data[2] = value & 0xff;
278
Takashi Iwai3e13f652010-09-23 07:40:16 +0200279 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000280 reg < codec->driver->reg_cache_size &&
281 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000282 ret = snd_soc_cache_write(codec, reg, value);
283 if (ret < 0)
284 return -1;
285 }
Mark Brownafa2f102009-07-10 23:11:24 +0100286
Mark Browna3032b42010-02-01 18:48:03 +0000287 if (codec->cache_only) {
288 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000289 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000290 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000291
Mark Brownafa2f102009-07-10 23:11:24 +0100292 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{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000301 int ret;
302 unsigned int val;
Mark Brownafa2f102009-07-10 23:11:24 +0100303
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000304 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000305 snd_soc_codec_volatile_register(codec, reg) ||
306 codec->cache_bypass) {
Mark Brown8c961bc2010-02-01 18:46:10 +0000307 if (codec->cache_only)
Dimitris Papastamos391d8a02010-09-21 17:04:07 +0100308 return -1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000309
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000310 BUG_ON(!codec->hw_read);
Mark Brownafa2f102009-07-10 23:11:24 +0100311 return codec->hw_read(codec, reg);
Mark Brown8c961bc2010-02-01 18:46:10 +0000312 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000313
314 ret = snd_soc_cache_read(codec, reg, &val);
315 if (ret < 0)
316 return -1;
317 return val;
Mark Brownafa2f102009-07-10 23:11:24 +0100318}
319
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100320#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);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000337 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100338
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
Randy Dunlap17244c22009-08-10 16:04:39 -0700351#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800352static 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
385#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brownafa2f102009-07-10 23:11:24 +0100386static 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
Mark Brown17a52fd2009-07-05 17:24:50 +0100418
Barry Song994dc422010-01-27 11:46:18 +0800419#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{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000456 int ret;
457 unsigned int val;
Barry Song994dc422010-01-27 11:46:18 +0800458
459 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100460 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000461 snd_soc_codec_volatile_register(codec, reg) ||
462 codec->cache_bypass) {
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100463 if (codec->cache_only)
464 return -1;
465
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000466 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100467 return codec->hw_read(codec, reg);
468 }
469
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000470 ret = snd_soc_cache_read(codec, reg, &val);
471 if (ret < 0)
472 return -1;
473 return val;
Barry Song994dc422010-01-27 11:46:18 +0800474}
475
476static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
477 unsigned int value)
478{
Barry Song994dc422010-01-27 11:46:18 +0800479 u8 data[3];
480 int ret;
481
Barry Song994dc422010-01-27 11:46:18 +0800482 data[0] = (reg >> 8) & 0xff;
483 data[1] = reg & 0xff;
484 data[2] = value;
485
486 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100487 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000488 reg < codec->driver->reg_cache_size &&
489 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000490 ret = snd_soc_cache_write(codec, reg, value);
491 if (ret < 0)
492 return -1;
493 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000494
Mark Browna3032b42010-02-01 18:48:03 +0000495 if (codec->cache_only) {
496 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000497 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000498 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000499
Barry Song994dc422010-01-27 11:46:18 +0800500 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);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000526 memset(&t, 0, sizeof t);
Barry Song994dc422010-01-27 11:46:18 +0800527
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
Mark Brownbc6552f2010-03-05 16:27:15 +0000540#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{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000577 int ret;
578 unsigned int val;
Mark Brownbc6552f2010-03-05 16:27:15 +0000579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000581 snd_soc_codec_volatile_register(codec, reg) ||
582 codec->cache_bypass) {
Mark Brownbc6552f2010-03-05 16:27:15 +0000583 if (codec->cache_only)
Dimitris Papastamos391d8a02010-09-21 17:04:07 +0100584 return -1;
Mark Brownbc6552f2010-03-05 16:27:15 +0000585
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000586 BUG_ON(!codec->hw_read);
Mark Brownbc6552f2010-03-05 16:27:15 +0000587 return codec->hw_read(codec, reg);
588 }
589
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000590 ret = snd_soc_cache_read(codec, reg, &val);
591 if (ret < 0)
592 return -1;
593
594 return val;
Mark Brownbc6552f2010-03-05 16:27:15 +0000595}
596
597static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
598 unsigned int value)
599{
Mark Brownbc6552f2010-03-05 16:27:15 +0000600 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
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100608 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000609 reg < codec->driver->reg_cache_size &&
610 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000611 ret = snd_soc_cache_write(codec, reg, value);
612 if (ret < 0)
613 return -1;
614 }
Mark Brownbc6552f2010-03-05 16:27:15 +0000615
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}
Barry Song994dc422010-01-27 11:46:18 +0800629
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100630#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);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000648 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100649
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
Mark Brown17a52fd2009-07-05 17:24:50 +0100662static struct {
663 int addr_bits;
664 int data_bits;
Mark Brownafa2f102009-07-10 23:11:24 +0100665 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
Mark Brown27ded042009-07-10 23:28:16 +0100666 int (*spi_write)(void *, const char *, int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100667 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
Mark Brownafa2f102009-07-10 23:11:24 +0100668 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100669} io_types[] = {
Mark Brownd62ab352009-09-21 04:21:47 -0700670 {
Barry Song63b62ab2010-01-27 11:46:17 +0800671 .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 },
675 {
Mark Brownd62ab352009-09-21 04:21:47 -0700676 .addr_bits = 7, .data_bits = 9,
677 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
Barry Song8998c892009-12-31 10:30:34 +0800678 .spi_write = snd_soc_7_9_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700679 },
680 {
681 .addr_bits = 8, .data_bits = 8,
682 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800683 .i2c_read = snd_soc_8_8_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100684 .spi_write = snd_soc_8_8_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700685 },
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,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100690 .spi_write = snd_soc_8_16_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700691 },
Barry Song994dc422010-01-27 11:46:18 +0800692 {
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 },
Mark Brownbc6552f2010-03-05 16:27:15 +0000698 {
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,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100702 .spi_write = snd_soc_16_16_spi_write,
Mark Brownbc6552f2010-03-05 16:27:15 +0000703 },
Mark Brown17a52fd2009-07-05 17:24:50 +0100704};
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.
Mark Brown7084a422009-07-10 22:24:27 +0100713 * @control: Control bus used.
Mark Brown17a52fd2009-07-05 17:24:50 +0100714 *
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,
Mark Brown7084a422009-07-10 22:24:27 +0100727 int addr_bits, int data_bits,
728 enum snd_soc_control_type control)
Mark Brown17a52fd2009-07-05 17:24:50 +0100729{
730 int i;
731
Mark Brown17a52fd2009-07-05 17:24:50 +0100732 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
Mark Brownc3acec22010-12-02 16:15:29 +0000743 codec->write = io_types[i].write;
744 codec->read = io_types[i].read;
Mark Brown17a52fd2009-07-05 17:24:50 +0100745
Mark Brown7084a422009-07-10 22:24:27 +0100746 switch (control) {
747 case SND_SOC_CUSTOM:
748 break;
749
750 case SND_SOC_I2C:
Randy Dunlap17244c22009-08-10 16:04:39 -0700751#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brown7084a422009-07-10 22:24:27 +0100752 codec->hw_write = (hw_write_t)i2c_master_send;
753#endif
Mark Brownafa2f102009-07-10 23:11:24 +0100754 if (io_types[i].i2c_read)
755 codec->hw_read = io_types[i].i2c_read;
Mark Browna6d14342010-08-12 10:59:15 +0100756
757 codec->control_data = container_of(codec->dev,
758 struct i2c_client,
759 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100760 break;
761
762 case SND_SOC_SPI:
Mark Brown27ded042009-07-10 23:28:16 +0100763 if (io_types[i].spi_write)
764 codec->hw_write = io_types[i].spi_write;
Mark Browna6d14342010-08-12 10:59:15 +0100765
766 codec->control_data = container_of(codec->dev,
767 struct spi_device,
768 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100769 break;
770 }
771
Mark Brown17a52fd2009-07-05 17:24:50 +0100772 return 0;
773}
774EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000775
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000776static 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
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000819struct 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
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000850static 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;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000883 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000884
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;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000890 ret = snd_soc_cache_read(codec, rbnode->reg, &val);
891 if (ret)
892 return ret;
893 ret = snd_soc_write(codec, rbnode->reg, val);
894 if (ret)
895 return ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000896 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
897 rbnode->reg, val);
898 }
899
900 return 0;
901}
902
903static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
904 unsigned int reg, unsigned int value)
905{
906 struct snd_soc_rbtree_ctx *rbtree_ctx;
907 struct snd_soc_rbtree_node *rbnode;
908
909 rbtree_ctx = codec->reg_cache;
910 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
911 if (rbnode) {
912 if (rbnode->value == value)
913 return 0;
914 rbnode->value = value;
915 } else {
916 /* bail out early, no need to create the rbnode yet */
917 if (!value)
918 return 0;
919 /*
920 * for uninitialized registers whose value is changed
921 * from the default zero, create an rbnode and insert
922 * it into the tree.
923 */
924 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
925 if (!rbnode)
926 return -ENOMEM;
927 rbnode->reg = reg;
928 rbnode->value = value;
929 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
930 }
931
932 return 0;
933}
934
935static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
936 unsigned int reg, unsigned int *value)
937{
938 struct snd_soc_rbtree_ctx *rbtree_ctx;
939 struct snd_soc_rbtree_node *rbnode;
940
941 rbtree_ctx = codec->reg_cache;
942 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
943 if (rbnode) {
944 *value = rbnode->value;
945 } else {
946 /* uninitialized registers default to 0 */
947 *value = 0;
948 }
949
950 return 0;
951}
952
953static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
954{
955 struct rb_node *next;
956 struct snd_soc_rbtree_ctx *rbtree_ctx;
957 struct snd_soc_rbtree_node *rbtree_node;
958
959 /* if we've already been called then just return */
960 rbtree_ctx = codec->reg_cache;
961 if (!rbtree_ctx)
962 return 0;
963
964 /* free up the rbtree */
965 next = rb_first(&rbtree_ctx->root);
966 while (next) {
967 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
968 next = rb_next(&rbtree_node->node);
969 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
970 kfree(rbtree_node);
971 }
972
973 /* release the resources */
974 kfree(codec->reg_cache);
975 codec->reg_cache = NULL;
976
977 return 0;
978}
979
980static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
981{
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000982 struct snd_soc_rbtree_node *rbtree_node;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000983 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000984 unsigned int val;
985 unsigned int word_size;
986 int i;
987 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000988
989 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
990 if (!codec->reg_cache)
991 return -ENOMEM;
992
993 rbtree_ctx = codec->reg_cache;
994 rbtree_ctx->root = RB_ROOT;
995
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +0000996 if (!codec->reg_def_copy)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000997 return 0;
998
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000999 /*
1000 * populate the rbtree with the initialized registers. All other
1001 * registers will be inserted when they are first modified.
1002 */
1003 word_size = codec->driver->reg_word_size;
1004 for (i = 0; i < codec->driver->reg_cache_size; ++i) {
1005 val = snd_soc_get_cache_val(codec->reg_def_copy, i, word_size);
1006 if (!val)
1007 continue;
1008 rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL);
1009 if (!rbtree_node) {
1010 ret = -ENOMEM;
1011 snd_soc_cache_exit(codec);
1012 break;
1013 }
1014 rbtree_node->reg = i;
1015 rbtree_node->value = val;
1016 rbtree_node->defval = val;
1017 snd_soc_rbtree_insert(&rbtree_ctx->root, rbtree_node);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001018 }
1019
1020 return 0;
1021}
1022
Mark Brown68d44ee2010-12-21 17:19:56 +00001023#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001024struct snd_soc_lzo_ctx {
1025 void *wmem;
1026 void *dst;
1027 const void *src;
1028 size_t src_len;
1029 size_t dst_len;
1030 size_t decompressed_size;
1031 unsigned long *sync_bmp;
1032 int sync_bmp_nbits;
1033};
1034
1035#define LZO_BLOCK_NUM 8
1036static int snd_soc_lzo_block_count(void)
1037{
1038 return LZO_BLOCK_NUM;
1039}
1040
1041static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
1042{
1043 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
1044 if (!lzo_ctx->wmem)
1045 return -ENOMEM;
1046 return 0;
1047}
1048
1049static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
1050{
1051 size_t compress_size;
1052 int ret;
1053
1054 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
1055 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
1056 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
1057 return -EINVAL;
1058 lzo_ctx->dst_len = compress_size;
1059 return 0;
1060}
1061
1062static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
1063{
1064 size_t dst_len;
1065 int ret;
1066
1067 dst_len = lzo_ctx->dst_len;
1068 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
1069 lzo_ctx->dst, &dst_len);
1070 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
1071 return -EINVAL;
1072 return 0;
1073}
1074
1075static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
1076 struct snd_soc_lzo_ctx *lzo_ctx)
1077{
1078 int ret;
1079
1080 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
1081 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1082 if (!lzo_ctx->dst) {
1083 lzo_ctx->dst_len = 0;
1084 return -ENOMEM;
1085 }
1086
1087 ret = snd_soc_lzo_compress(lzo_ctx);
1088 if (ret < 0)
1089 return ret;
1090 return 0;
1091}
1092
1093static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
1094 struct snd_soc_lzo_ctx *lzo_ctx)
1095{
1096 int ret;
1097
1098 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
1099 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1100 if (!lzo_ctx->dst) {
1101 lzo_ctx->dst_len = 0;
1102 return -ENOMEM;
1103 }
1104
1105 ret = snd_soc_lzo_decompress(lzo_ctx);
1106 if (ret < 0)
1107 return ret;
1108 return 0;
1109}
1110
1111static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
1112 unsigned int reg)
1113{
Mark Brown001ae4c2010-12-02 16:21:08 +00001114 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001115
1116 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001117 return (reg * codec_drv->reg_word_size) /
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001118 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001119}
1120
1121static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
1122 unsigned int reg)
1123{
Mark Brown001ae4c2010-12-02 16:21:08 +00001124 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001125
1126 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001127 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001128 codec_drv->reg_word_size);
1129}
1130
1131static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
1132{
Mark Brown001ae4c2010-12-02 16:21:08 +00001133 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001134
1135 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001136 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001137}
1138
1139static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
1140{
1141 struct snd_soc_lzo_ctx **lzo_blocks;
1142 unsigned int val;
1143 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001144 int ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001145
1146 lzo_blocks = codec->reg_cache;
1147 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001148 ret = snd_soc_cache_read(codec, i, &val);
1149 if (ret)
1150 return ret;
1151 ret = snd_soc_write(codec, i, val);
1152 if (ret)
1153 return ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001154 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1155 i, val);
1156 }
1157
1158 return 0;
1159}
1160
1161static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
1162 unsigned int reg, unsigned int value)
1163{
1164 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1165 int ret, blkindex, blkpos;
1166 size_t blksize, tmp_dst_len;
1167 void *tmp_dst;
1168
1169 /* index of the compressed lzo block */
1170 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1171 /* register index within the decompressed block */
1172 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1173 /* size of the compressed block */
1174 blksize = snd_soc_lzo_get_blksize(codec);
1175 lzo_blocks = codec->reg_cache;
1176 lzo_block = lzo_blocks[blkindex];
1177
1178 /* save the pointer and length of the compressed block */
1179 tmp_dst = lzo_block->dst;
1180 tmp_dst_len = lzo_block->dst_len;
1181
1182 /* prepare the source to be the compressed block */
1183 lzo_block->src = lzo_block->dst;
1184 lzo_block->src_len = lzo_block->dst_len;
1185
1186 /* decompress the block */
1187 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1188 if (ret < 0) {
1189 kfree(lzo_block->dst);
1190 goto out;
1191 }
1192
1193 /* write the new value to the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001194 if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
1195 codec->driver->reg_word_size)) {
1196 kfree(lzo_block->dst);
1197 goto out;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001198 }
1199
1200 /* prepare the source to be the decompressed block */
1201 lzo_block->src = lzo_block->dst;
1202 lzo_block->src_len = lzo_block->dst_len;
1203
1204 /* compress the block */
1205 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1206 if (ret < 0) {
1207 kfree(lzo_block->dst);
1208 kfree(lzo_block->src);
1209 goto out;
1210 }
1211
1212 /* set the bit so we know we have to sync this register */
1213 set_bit(reg, lzo_block->sync_bmp);
1214 kfree(tmp_dst);
1215 kfree(lzo_block->src);
1216 return 0;
1217out:
1218 lzo_block->dst = tmp_dst;
1219 lzo_block->dst_len = tmp_dst_len;
1220 return ret;
1221}
1222
1223static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1224 unsigned int reg, unsigned int *value)
1225{
1226 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1227 int ret, blkindex, blkpos;
1228 size_t blksize, tmp_dst_len;
1229 void *tmp_dst;
1230
1231 *value = 0;
1232 /* index of the compressed lzo block */
1233 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1234 /* register index within the decompressed block */
1235 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1236 /* size of the compressed block */
1237 blksize = snd_soc_lzo_get_blksize(codec);
1238 lzo_blocks = codec->reg_cache;
1239 lzo_block = lzo_blocks[blkindex];
1240
1241 /* save the pointer and length of the compressed block */
1242 tmp_dst = lzo_block->dst;
1243 tmp_dst_len = lzo_block->dst_len;
1244
1245 /* prepare the source to be the compressed block */
1246 lzo_block->src = lzo_block->dst;
1247 lzo_block->src_len = lzo_block->dst_len;
1248
1249 /* decompress the block */
1250 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001251 if (ret >= 0)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001252 /* fetch the value from the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001253 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1254 codec->driver->reg_word_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001255
1256 kfree(lzo_block->dst);
1257 /* restore the pointer and length of the compressed block */
1258 lzo_block->dst = tmp_dst;
1259 lzo_block->dst_len = tmp_dst_len;
1260 return 0;
1261}
1262
1263static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1264{
1265 struct snd_soc_lzo_ctx **lzo_blocks;
1266 int i, blkcount;
1267
1268 lzo_blocks = codec->reg_cache;
1269 if (!lzo_blocks)
1270 return 0;
1271
1272 blkcount = snd_soc_lzo_block_count();
1273 /*
1274 * the pointer to the bitmap used for syncing the cache
1275 * is shared amongst all lzo_blocks. Ensure it is freed
1276 * only once.
1277 */
1278 if (lzo_blocks[0])
1279 kfree(lzo_blocks[0]->sync_bmp);
1280 for (i = 0; i < blkcount; ++i) {
1281 if (lzo_blocks[i]) {
1282 kfree(lzo_blocks[i]->wmem);
1283 kfree(lzo_blocks[i]->dst);
1284 }
1285 /* each lzo_block is a pointer returned by kmalloc or NULL */
1286 kfree(lzo_blocks[i]);
1287 }
1288 kfree(lzo_blocks);
1289 codec->reg_cache = NULL;
1290 return 0;
1291}
1292
1293static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1294{
1295 struct snd_soc_lzo_ctx **lzo_blocks;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001296 size_t bmp_size;
Mark Brown001ae4c2010-12-02 16:21:08 +00001297 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001298 int ret, tofree, i, blksize, blkcount;
1299 const char *p, *end;
1300 unsigned long *sync_bmp;
1301
1302 ret = 0;
1303 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001304
1305 /*
1306 * If we have not been given a default register cache
1307 * then allocate a dummy zero-ed out region, compress it
1308 * and remember to free it afterwards.
1309 */
1310 tofree = 0;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001311 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001312 tofree = 1;
1313
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001314 if (!codec->reg_def_copy) {
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001315 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001316 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001317 return -ENOMEM;
1318 }
1319
1320 blkcount = snd_soc_lzo_block_count();
1321 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1322 GFP_KERNEL);
1323 if (!codec->reg_cache) {
1324 ret = -ENOMEM;
1325 goto err_tofree;
1326 }
1327 lzo_blocks = codec->reg_cache;
1328
1329 /*
1330 * allocate a bitmap to be used when syncing the cache with
1331 * the hardware. Each time a register is modified, the corresponding
1332 * bit is set in the bitmap, so we know that we have to sync
1333 * that register.
1334 */
1335 bmp_size = codec_drv->reg_cache_size;
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +00001336 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001337 GFP_KERNEL);
1338 if (!sync_bmp) {
1339 ret = -ENOMEM;
1340 goto err;
1341 }
Dimitris Papastamos09c74a92010-11-29 11:43:33 +00001342 bitmap_zero(sync_bmp, bmp_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001343
1344 /* allocate the lzo blocks and initialize them */
1345 for (i = 0; i < blkcount; ++i) {
1346 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1347 GFP_KERNEL);
1348 if (!lzo_blocks[i]) {
1349 kfree(sync_bmp);
1350 ret = -ENOMEM;
1351 goto err;
1352 }
1353 lzo_blocks[i]->sync_bmp = sync_bmp;
Dimitris Papastamos04f8fd12011-01-11 11:24:02 +00001354 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001355 /* alloc the working space for the compressed block */
1356 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1357 if (ret < 0)
1358 goto err;
1359 }
1360
1361 blksize = snd_soc_lzo_get_blksize(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001362 p = codec->reg_def_copy;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001363 end = codec->reg_def_copy + codec->reg_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001364 /* compress the register map and fill the lzo blocks */
1365 for (i = 0; i < blkcount; ++i, p += blksize) {
1366 lzo_blocks[i]->src = p;
1367 if (p + blksize > end)
1368 lzo_blocks[i]->src_len = end - p;
1369 else
1370 lzo_blocks[i]->src_len = blksize;
1371 ret = snd_soc_lzo_compress_cache_block(codec,
1372 lzo_blocks[i]);
1373 if (ret < 0)
1374 goto err;
1375 lzo_blocks[i]->decompressed_size =
1376 lzo_blocks[i]->src_len;
1377 }
1378
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001379 if (tofree) {
1380 kfree(codec->reg_def_copy);
1381 codec->reg_def_copy = NULL;
1382 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001383 return 0;
1384err:
1385 snd_soc_cache_exit(codec);
1386err_tofree:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001387 if (tofree) {
1388 kfree(codec->reg_def_copy);
1389 codec->reg_def_copy = NULL;
1390 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001391 return ret;
1392}
Mark Brown68d44ee2010-12-21 17:19:56 +00001393#endif
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001394
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001395static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1396{
1397 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001398 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +00001399 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001400 unsigned int val;
1401
1402 codec_drv = codec->driver;
1403 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001404 ret = snd_soc_cache_read(codec, i, &val);
1405 if (ret)
1406 return ret;
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001407 if (codec->reg_def_copy)
1408 if (snd_soc_get_cache_val(codec->reg_def_copy,
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001409 i, codec_drv->reg_word_size) == val)
1410 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001411 ret = snd_soc_write(codec, i, val);
1412 if (ret)
1413 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001414 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1415 i, val);
1416 }
1417 return 0;
1418}
1419
1420static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1421 unsigned int reg, unsigned int value)
1422{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001423 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1424 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001425 return 0;
1426}
1427
1428static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1429 unsigned int reg, unsigned int *value)
1430{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001431 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1432 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001433 return 0;
1434}
1435
1436static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1437{
1438 if (!codec->reg_cache)
1439 return 0;
1440 kfree(codec->reg_cache);
1441 codec->reg_cache = NULL;
1442 return 0;
1443}
1444
1445static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1446{
Mark Brown001ae4c2010-12-02 16:21:08 +00001447 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001448
1449 codec_drv = codec->driver;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001450
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001451 if (codec->reg_def_copy)
1452 codec->reg_cache = kmemdup(codec->reg_def_copy,
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001453 codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001454 else
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001455 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001456 if (!codec->reg_cache)
1457 return -ENOMEM;
1458
1459 return 0;
1460}
1461
1462/* an array of all supported compression types */
1463static const struct snd_soc_cache_ops cache_types[] = {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001464 /* Flat *must* be the first entry for fallback */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001465 {
Dimitris Papastamosdf0701b2010-11-29 10:54:28 +00001466 .id = SND_SOC_FLAT_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001467 .name = "flat",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001468 .init = snd_soc_flat_cache_init,
1469 .exit = snd_soc_flat_cache_exit,
1470 .read = snd_soc_flat_cache_read,
1471 .write = snd_soc_flat_cache_write,
1472 .sync = snd_soc_flat_cache_sync
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001473 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001474#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001475 {
1476 .id = SND_SOC_LZO_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001477 .name = "LZO",
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001478 .init = snd_soc_lzo_cache_init,
1479 .exit = snd_soc_lzo_cache_exit,
1480 .read = snd_soc_lzo_cache_read,
1481 .write = snd_soc_lzo_cache_write,
1482 .sync = snd_soc_lzo_cache_sync
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001483 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001484#endif
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001485 {
1486 .id = SND_SOC_RBTREE_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001487 .name = "rbtree",
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001488 .init = snd_soc_rbtree_cache_init,
1489 .exit = snd_soc_rbtree_cache_exit,
1490 .read = snd_soc_rbtree_cache_read,
1491 .write = snd_soc_rbtree_cache_write,
1492 .sync = snd_soc_rbtree_cache_sync
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001493 }
1494};
1495
1496int snd_soc_cache_init(struct snd_soc_codec *codec)
1497{
1498 int i;
1499
1500 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00001501 if (cache_types[i].id == codec->compress_type)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001502 break;
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001503
1504 /* Fall back to flat compression */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001505 if (i == ARRAY_SIZE(cache_types)) {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001506 dev_warn(codec->dev, "Could not match compress type: %d\n",
1507 codec->compress_type);
1508 i = 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001509 }
1510
1511 mutex_init(&codec->cache_rw_mutex);
1512 codec->cache_ops = &cache_types[i];
1513
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001514 if (codec->cache_ops->init) {
1515 if (codec->cache_ops->name)
1516 dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1517 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001518 return codec->cache_ops->init(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001519 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001520 return -EINVAL;
1521}
1522
1523/*
1524 * NOTE: keep in mind that this function might be called
1525 * multiple times.
1526 */
1527int snd_soc_cache_exit(struct snd_soc_codec *codec)
1528{
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001529 if (codec->cache_ops && codec->cache_ops->exit) {
1530 if (codec->cache_ops->name)
1531 dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1532 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001533 return codec->cache_ops->exit(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001534 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001535 return -EINVAL;
1536}
1537
1538/**
1539 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1540 *
1541 * @codec: CODEC to configure.
1542 * @reg: The register index.
1543 * @value: The value to be returned.
1544 */
1545int snd_soc_cache_read(struct snd_soc_codec *codec,
1546 unsigned int reg, unsigned int *value)
1547{
1548 int ret;
1549
1550 mutex_lock(&codec->cache_rw_mutex);
1551
1552 if (value && codec->cache_ops && codec->cache_ops->read) {
1553 ret = codec->cache_ops->read(codec, reg, value);
1554 mutex_unlock(&codec->cache_rw_mutex);
1555 return ret;
1556 }
1557
1558 mutex_unlock(&codec->cache_rw_mutex);
1559 return -EINVAL;
1560}
1561EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1562
1563/**
1564 * snd_soc_cache_write: Set the value of a given register in the cache.
1565 *
1566 * @codec: CODEC to configure.
1567 * @reg: The register index.
1568 * @value: The new register value.
1569 */
1570int snd_soc_cache_write(struct snd_soc_codec *codec,
1571 unsigned int reg, unsigned int value)
1572{
1573 int ret;
1574
1575 mutex_lock(&codec->cache_rw_mutex);
1576
1577 if (codec->cache_ops && codec->cache_ops->write) {
1578 ret = codec->cache_ops->write(codec, reg, value);
1579 mutex_unlock(&codec->cache_rw_mutex);
1580 return ret;
1581 }
1582
1583 mutex_unlock(&codec->cache_rw_mutex);
1584 return -EINVAL;
1585}
1586EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1587
1588/**
1589 * snd_soc_cache_sync: Sync the register cache with the hardware.
1590 *
1591 * @codec: CODEC to configure.
1592 *
1593 * Any registers that should not be synced should be marked as
1594 * volatile. In general drivers can choose not to use the provided
1595 * syncing functionality if they so require.
1596 */
1597int snd_soc_cache_sync(struct snd_soc_codec *codec)
1598{
1599 int ret;
1600
1601 if (!codec->cache_sync) {
1602 return 0;
1603 }
1604
1605 if (codec->cache_ops && codec->cache_ops->sync) {
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001606 if (codec->cache_ops->name)
1607 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1608 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001609 ret = codec->cache_ops->sync(codec);
1610 if (!ret)
1611 codec->cache_sync = 0;
1612 return ret;
1613 }
1614
1615 return -EINVAL;
1616}
1617EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
Dimitris Papastamos066d16c2011-01-13 12:20:36 +00001618
1619static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1620 unsigned int reg)
1621{
1622 const struct snd_soc_codec_driver *codec_drv;
1623 unsigned int min, max, index;
1624
1625 codec_drv = codec->driver;
1626 min = 0;
1627 max = codec_drv->reg_access_size - 1;
1628 do {
1629 index = (min + max) / 2;
1630 if (codec_drv->reg_access_default[index].reg == reg)
1631 return index;
1632 if (codec_drv->reg_access_default[index].reg < reg)
1633 min = index + 1;
1634 else
1635 max = index;
1636 } while (min <= max);
1637 return -1;
1638}
1639
1640int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1641 unsigned int reg)
1642{
1643 int index;
1644
1645 if (reg >= codec->driver->reg_cache_size)
1646 return 1;
1647 index = snd_soc_get_reg_access_index(codec, reg);
1648 if (index < 0)
1649 return 0;
1650 return codec->driver->reg_access_default[index].vol;
1651}
1652EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1653
1654int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1655 unsigned int reg)
1656{
1657 int index;
1658
1659 if (reg >= codec->driver->reg_cache_size)
1660 return 1;
1661 index = snd_soc_get_reg_access_index(codec, reg);
1662 if (index < 0)
1663 return 0;
1664 return codec->driver->reg_access_default[index].read;
1665}
1666EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);