blob: f834839637915499fb87f57f2df039797796ae12 [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;
Dimitris Papastamos99780072011-01-19 14:53:37 +0000893 codec->cache_bypass = 1;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000894 ret = snd_soc_write(codec, rbnode->reg, val);
Dimitris Papastamos99780072011-01-19 14:53:37 +0000895 codec->cache_bypass = 0;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000896 if (ret)
897 return ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000898 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{
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000984 struct snd_soc_rbtree_node *rbtree_node;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000985 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000986 unsigned int val;
987 unsigned int word_size;
988 int i;
989 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000990
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
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +0000998 if (!codec->reg_def_copy)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000999 return 0;
1000
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001001 /*
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);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001020 }
1021
1022 return 0;
1023}
1024
Mark Brown68d44ee2010-12-21 17:19:56 +00001025#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001026struct 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{
Mark Brown001ae4c2010-12-02 16:21:08 +00001116 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001117
1118 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001119 return (reg * codec_drv->reg_word_size) /
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001120 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001121}
1122
1123static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
1124 unsigned int reg)
1125{
Mark Brown001ae4c2010-12-02 16:21:08 +00001126 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001127
1128 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001129 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001130 codec_drv->reg_word_size);
1131}
1132
1133static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
1134{
Mark Brown001ae4c2010-12-02 16:21:08 +00001135 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001136
1137 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001138 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001139}
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;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001146 int ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001147
1148 lzo_blocks = codec->reg_cache;
1149 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001150 ret = snd_soc_cache_read(codec, i, &val);
1151 if (ret)
1152 return ret;
Dimitris Papastamos99780072011-01-19 14:53:37 +00001153 codec->cache_bypass = 1;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001154 ret = snd_soc_write(codec, i, val);
Dimitris Papastamos99780072011-01-19 14:53:37 +00001155 codec->cache_bypass = 0;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001156 if (ret)
1157 return ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001158 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 */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001198 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;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001202 }
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);
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001255 if (ret >= 0)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001256 /* fetch the value from the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001257 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1258 codec->driver->reg_word_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001259
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;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001300 size_t bmp_size;
Mark Brown001ae4c2010-12-02 16:21:08 +00001301 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001302 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;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001308
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;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001315 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001316 tofree = 1;
1317
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001318 if (!codec->reg_def_copy) {
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001319 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001320 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001321 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;
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +00001340 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001341 GFP_KERNEL);
1342 if (!sync_bmp) {
1343 ret = -ENOMEM;
1344 goto err;
1345 }
Dimitris Papastamos09c74a92010-11-29 11:43:33 +00001346 bitmap_zero(sync_bmp, bmp_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001347
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;
Dimitris Papastamos04f8fd12011-01-11 11:24:02 +00001358 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001359 /* 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);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001366 p = codec->reg_def_copy;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001367 end = codec->reg_def_copy + codec->reg_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001368 /* 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
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001383 if (tofree) {
1384 kfree(codec->reg_def_copy);
1385 codec->reg_def_copy = NULL;
1386 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001387 return 0;
1388err:
1389 snd_soc_cache_exit(codec);
1390err_tofree:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001391 if (tofree) {
1392 kfree(codec->reg_def_copy);
1393 codec->reg_def_copy = NULL;
1394 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001395 return ret;
1396}
Mark Brown68d44ee2010-12-21 17:19:56 +00001397#endif
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001398
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001399static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1400{
1401 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001402 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +00001403 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001404 unsigned int val;
1405
1406 codec_drv = codec->driver;
1407 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001408 ret = snd_soc_cache_read(codec, i, &val);
1409 if (ret)
1410 return ret;
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001411 if (codec->reg_def_copy)
1412 if (snd_soc_get_cache_val(codec->reg_def_copy,
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001413 i, codec_drv->reg_word_size) == val)
1414 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001415 ret = snd_soc_write(codec, i, val);
1416 if (ret)
1417 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001418 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{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001427 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1428 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001429 return 0;
1430}
1431
1432static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1433 unsigned int reg, unsigned int *value)
1434{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001435 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1436 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001437 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{
Mark Brown001ae4c2010-12-02 16:21:08 +00001451 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001452
1453 codec_drv = codec->driver;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001454
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001455 if (codec->reg_def_copy)
1456 codec->reg_cache = kmemdup(codec->reg_def_copy,
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001457 codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001458 else
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001459 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001460 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[] = {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001468 /* Flat *must* be the first entry for fallback */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001469 {
Dimitris Papastamosdf0701b2010-11-29 10:54:28 +00001470 .id = SND_SOC_FLAT_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001471 .name = "flat",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001472 .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
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001477 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001478#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001479 {
1480 .id = SND_SOC_LZO_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001481 .name = "LZO",
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001482 .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
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001487 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001488#endif
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001489 {
1490 .id = SND_SOC_RBTREE_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001491 .name = "rbtree",
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001492 .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
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001497 }
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)
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00001505 if (cache_types[i].id == codec->compress_type)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001506 break;
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001507
1508 /* Fall back to flat compression */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001509 if (i == ARRAY_SIZE(cache_types)) {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001510 dev_warn(codec->dev, "Could not match compress type: %d\n",
1511 codec->compress_type);
1512 i = 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001513 }
1514
1515 mutex_init(&codec->cache_rw_mutex);
1516 codec->cache_ops = &cache_types[i];
1517
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001518 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);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001522 return codec->cache_ops->init(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001523 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001524 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{
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001533 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);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001537 return codec->cache_ops->exit(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001538 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001539 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) {
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001610 if (codec->cache_ops->name)
1611 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1612 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001613 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);
Dimitris Papastamos066d16c2011-01-13 12:20:36 +00001622
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);