blob: 9f6737413a6e646a3aaef10cb02a7f11be50f0e1 [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
Dimitris Papastamosc358e642011-01-21 15:29:02 +000021#include <trace/events/asoc.h>
22
Barry Song63b62ab2010-01-27 11:46:17 +080023static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
24 unsigned int reg)
25{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000026 int ret;
27 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010028
29 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +000030 snd_soc_codec_volatile_register(codec, reg) ||
31 codec->cache_bypass) {
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010032 if (codec->cache_only)
33 return -1;
34
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +000035 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010036 return codec->hw_read(codec, reg);
37 }
38
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000039 ret = snd_soc_cache_read(codec, reg, &val);
40 if (ret < 0)
41 return -1;
42 return val;
Barry Song63b62ab2010-01-27 11:46:17 +080043}
44
45static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
46 unsigned int value)
47{
Barry Song63b62ab2010-01-27 11:46:17 +080048 u8 data[2];
49 int ret;
50
Barry Song63b62ab2010-01-27 11:46:17 +080051 data[0] = (reg << 4) | ((value >> 8) & 0x000f);
52 data[1] = value & 0x00ff;
53
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010054 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +000055 reg < codec->driver->reg_cache_size &&
56 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000057 ret = snd_soc_cache_write(codec, reg, value);
58 if (ret < 0)
59 return -1;
60 }
Mark Brown8c961bc2010-02-01 18:46:10 +000061
Mark Browna3032b42010-02-01 18:48:03 +000062 if (codec->cache_only) {
63 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +000064 return 0;
Mark Browna3032b42010-02-01 18:48:03 +000065 }
Mark Brown8c961bc2010-02-01 18:46:10 +000066
Barry Song63b62ab2010-01-27 11:46:17 +080067 ret = codec->hw_write(codec->control_data, data, 2);
68 if (ret == 2)
69 return 0;
70 if (ret < 0)
71 return ret;
72 else
73 return -EIO;
74}
75
76#if defined(CONFIG_SPI_MASTER)
77static int snd_soc_4_12_spi_write(void *control_data, const char *data,
78 int len)
79{
80 struct spi_device *spi = control_data;
81 struct spi_transfer t;
82 struct spi_message m;
83 u8 msg[2];
84
85 if (len <= 0)
86 return 0;
87
88 msg[0] = data[1];
89 msg[1] = data[0];
90
91 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +000092 memset(&t, 0, sizeof t);
Barry Song63b62ab2010-01-27 11:46:17 +080093
94 t.tx_buf = &msg[0];
95 t.len = len;
96
97 spi_message_add_tail(&t, &m);
98 spi_sync(spi, &m);
99
100 return len;
101}
102#else
103#define snd_soc_4_12_spi_write NULL
104#endif
105
Mark Brown17a52fd2009-07-05 17:24:50 +0100106static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
107 unsigned int reg)
108{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000109 int ret;
110 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100111
112 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000113 snd_soc_codec_volatile_register(codec, reg) ||
114 codec->cache_bypass) {
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100115 if (codec->cache_only)
116 return -1;
117
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000118 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100119 return codec->hw_read(codec, reg);
120 }
121
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000122 ret = snd_soc_cache_read(codec, reg, &val);
123 if (ret < 0)
124 return -1;
125 return val;
Mark Brown17a52fd2009-07-05 17:24:50 +0100126}
127
128static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
129 unsigned int value)
130{
Mark Brown17a52fd2009-07-05 17:24:50 +0100131 u8 data[2];
132 int ret;
133
Mark Brown17a52fd2009-07-05 17:24:50 +0100134 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
135 data[1] = value & 0x00ff;
136
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100137 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000138 reg < codec->driver->reg_cache_size &&
139 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000140 ret = snd_soc_cache_write(codec, reg, value);
141 if (ret < 0)
142 return -1;
143 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000144
Mark Browna3032b42010-02-01 18:48:03 +0000145 if (codec->cache_only) {
146 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000147 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000148 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000149
Mark Brown17a52fd2009-07-05 17:24:50 +0100150 ret = codec->hw_write(codec->control_data, data, 2);
151 if (ret == 2)
152 return 0;
153 if (ret < 0)
154 return ret;
155 else
156 return -EIO;
157}
158
Mark Brown27ded042009-07-10 23:28:16 +0100159#if defined(CONFIG_SPI_MASTER)
160static int snd_soc_7_9_spi_write(void *control_data, const char *data,
161 int len)
162{
163 struct spi_device *spi = control_data;
164 struct spi_transfer t;
165 struct spi_message m;
166 u8 msg[2];
167
168 if (len <= 0)
169 return 0;
170
171 msg[0] = data[0];
172 msg[1] = data[1];
173
174 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000175 memset(&t, 0, sizeof t);
Mark Brown27ded042009-07-10 23:28:16 +0100176
177 t.tx_buf = &msg[0];
178 t.len = len;
179
180 spi_message_add_tail(&t, &m);
181 spi_sync(spi, &m);
182
183 return len;
184}
185#else
186#define snd_soc_7_9_spi_write NULL
187#endif
188
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900189static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
190 unsigned int value)
191{
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900192 u8 data[2];
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000193 int ret;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900194
Barry Songf4bee1b2010-03-18 16:17:01 +0800195 reg &= 0xff;
196 data[0] = reg;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900197 data[1] = value & 0xff;
198
Dimitris Papastamos005d65f2010-09-22 16:16:06 +0100199 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000200 reg < codec->driver->reg_cache_size &&
201 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000202 ret = snd_soc_cache_write(codec, reg, value);
203 if (ret < 0)
204 return -1;
205 }
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900206
Mark Browna3032b42010-02-01 18:48:03 +0000207 if (codec->cache_only) {
208 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000209 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000210 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000211
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900212 if (codec->hw_write(codec->control_data, data, 2) == 2)
213 return 0;
214 else
215 return -EIO;
216}
217
218static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
219 unsigned int reg)
220{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000221 int ret;
222 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100223
Barry Songf4bee1b2010-03-18 16:17:01 +0800224 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100225 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000226 snd_soc_codec_volatile_register(codec, reg) ||
227 codec->cache_bypass) {
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100228 if (codec->cache_only)
229 return -1;
230
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000231 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100232 return codec->hw_read(codec, reg);
233 }
234
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000235 ret = snd_soc_cache_read(codec, reg, &val);
236 if (ret < 0)
237 return -1;
238 return val;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900239}
240
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100241#if defined(CONFIG_SPI_MASTER)
242static int snd_soc_8_8_spi_write(void *control_data, const char *data,
243 int len)
244{
245 struct spi_device *spi = control_data;
246 struct spi_transfer t;
247 struct spi_message m;
248 u8 msg[2];
249
250 if (len <= 0)
251 return 0;
252
253 msg[0] = data[0];
254 msg[1] = data[1];
255
256 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000257 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100258
259 t.tx_buf = &msg[0];
260 t.len = len;
261
262 spi_message_add_tail(&t, &m);
263 spi_sync(spi, &m);
264
265 return len;
266}
267#else
268#define snd_soc_8_8_spi_write NULL
269#endif
270
Mark Brownafa2f102009-07-10 23:11:24 +0100271static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
272 unsigned int value)
273{
Mark Brownafa2f102009-07-10 23:11:24 +0100274 u8 data[3];
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000275 int ret;
Mark Brownafa2f102009-07-10 23:11:24 +0100276
277 data[0] = reg;
278 data[1] = (value >> 8) & 0xff;
279 data[2] = value & 0xff;
280
Takashi Iwai3e13f652010-09-23 07:40:16 +0200281 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000282 reg < codec->driver->reg_cache_size &&
283 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000284 ret = snd_soc_cache_write(codec, reg, value);
285 if (ret < 0)
286 return -1;
287 }
Mark Brownafa2f102009-07-10 23:11:24 +0100288
Mark Browna3032b42010-02-01 18:48:03 +0000289 if (codec->cache_only) {
290 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000291 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000292 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000293
Mark Brownafa2f102009-07-10 23:11:24 +0100294 if (codec->hw_write(codec->control_data, data, 3) == 3)
295 return 0;
296 else
297 return -EIO;
298}
299
300static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
301 unsigned int reg)
302{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000303 int ret;
304 unsigned int val;
Mark Brownafa2f102009-07-10 23:11:24 +0100305
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000306 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000307 snd_soc_codec_volatile_register(codec, reg) ||
308 codec->cache_bypass) {
Mark Brown8c961bc2010-02-01 18:46:10 +0000309 if (codec->cache_only)
Dimitris Papastamos391d8a02010-09-21 17:04:07 +0100310 return -1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000311
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000312 BUG_ON(!codec->hw_read);
Mark Brownafa2f102009-07-10 23:11:24 +0100313 return codec->hw_read(codec, reg);
Mark Brown8c961bc2010-02-01 18:46:10 +0000314 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000315
316 ret = snd_soc_cache_read(codec, reg, &val);
317 if (ret < 0)
318 return -1;
319 return val;
Mark Brownafa2f102009-07-10 23:11:24 +0100320}
321
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100322#if defined(CONFIG_SPI_MASTER)
323static int snd_soc_8_16_spi_write(void *control_data, const char *data,
324 int len)
325{
326 struct spi_device *spi = control_data;
327 struct spi_transfer t;
328 struct spi_message m;
329 u8 msg[3];
330
331 if (len <= 0)
332 return 0;
333
334 msg[0] = data[0];
335 msg[1] = data[1];
336 msg[2] = data[2];
337
338 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000339 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100340
341 t.tx_buf = &msg[0];
342 t.len = len;
343
344 spi_message_add_tail(&t, &m);
345 spi_sync(spi, &m);
346
347 return len;
348}
349#else
350#define snd_soc_8_16_spi_write NULL
351#endif
352
Randy Dunlap17244c22009-08-10 16:04:39 -0700353#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000354static unsigned int do_i2c_read(struct snd_soc_codec *codec,
355 void *reg, int reglen,
356 void *data, int datalen)
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800357{
358 struct i2c_msg xfer[2];
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800359 int ret;
360 struct i2c_client *client = codec->control_data;
361
362 /* Write register */
363 xfer[0].addr = client->addr;
364 xfer[0].flags = 0;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000365 xfer[0].len = reglen;
366 xfer[0].buf = reg;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800367
368 /* Read data */
369 xfer[1].addr = client->addr;
370 xfer[1].flags = I2C_M_RD;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000371 xfer[1].len = datalen;
372 xfer[1].buf = data;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800373
374 ret = i2c_transfer(client->adapter, xfer, 2);
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000375 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
376 if (ret == 2)
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800377 return 0;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000378 else if (ret < 0)
379 return ret;
380 else
381 return -EIO;
382}
383#endif
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800384
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000385#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
386static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
387 unsigned int r)
388{
389 u8 reg = r;
390 u8 data;
391 int ret;
392
393 ret = do_i2c_read(codec, &reg, 1, &data, 1);
394 if (ret < 0)
395 return 0;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800396 return data;
397}
398#else
399#define snd_soc_8_8_read_i2c NULL
400#endif
401
402#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brownafa2f102009-07-10 23:11:24 +0100403static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
404 unsigned int r)
405{
Mark Brownafa2f102009-07-10 23:11:24 +0100406 u8 reg = r;
407 u16 data;
408 int ret;
Mark Brownafa2f102009-07-10 23:11:24 +0100409
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000410 ret = do_i2c_read(codec, &reg, 1, &data, 2);
411 if (ret < 0)
Mark Brownafa2f102009-07-10 23:11:24 +0100412 return 0;
Mark Brownafa2f102009-07-10 23:11:24 +0100413 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{
Barry Song994dc422010-01-27 11:46:18 +0800423 u16 reg = r;
424 u8 data;
425 int ret;
Barry Song994dc422010-01-27 11:46:18 +0800426
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000427 ret = do_i2c_read(codec, &reg, 2, &data, 1);
428 if (ret < 0)
Barry Song994dc422010-01-27 11:46:18 +0800429 return 0;
Barry Song994dc422010-01-27 11:46:18 +0800430 return data;
431}
432#else
433#define snd_soc_16_8_read_i2c NULL
434#endif
435
436static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
437 unsigned int reg)
438{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000439 int ret;
440 unsigned int val;
Barry Song994dc422010-01-27 11:46:18 +0800441
442 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100443 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000444 snd_soc_codec_volatile_register(codec, reg) ||
445 codec->cache_bypass) {
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100446 if (codec->cache_only)
447 return -1;
448
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000449 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100450 return codec->hw_read(codec, reg);
451 }
452
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000453 ret = snd_soc_cache_read(codec, reg, &val);
454 if (ret < 0)
455 return -1;
456 return val;
Barry Song994dc422010-01-27 11:46:18 +0800457}
458
459static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
460 unsigned int value)
461{
Barry Song994dc422010-01-27 11:46:18 +0800462 u8 data[3];
463 int ret;
464
Barry Song994dc422010-01-27 11:46:18 +0800465 data[0] = (reg >> 8) & 0xff;
466 data[1] = reg & 0xff;
467 data[2] = value;
468
469 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100470 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000471 reg < codec->driver->reg_cache_size &&
472 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000473 ret = snd_soc_cache_write(codec, reg, value);
474 if (ret < 0)
475 return -1;
476 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000477
Mark Browna3032b42010-02-01 18:48:03 +0000478 if (codec->cache_only) {
479 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000480 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000481 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000482
Barry Song994dc422010-01-27 11:46:18 +0800483 ret = codec->hw_write(codec->control_data, data, 3);
484 if (ret == 3)
485 return 0;
486 if (ret < 0)
487 return ret;
488 else
489 return -EIO;
490}
491
492#if defined(CONFIG_SPI_MASTER)
493static int snd_soc_16_8_spi_write(void *control_data, const char *data,
494 int len)
495{
496 struct spi_device *spi = control_data;
497 struct spi_transfer t;
498 struct spi_message m;
499 u8 msg[3];
500
501 if (len <= 0)
502 return 0;
503
504 msg[0] = data[0];
505 msg[1] = data[1];
506 msg[2] = data[2];
507
508 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000509 memset(&t, 0, sizeof t);
Barry Song994dc422010-01-27 11:46:18 +0800510
511 t.tx_buf = &msg[0];
512 t.len = len;
513
514 spi_message_add_tail(&t, &m);
515 spi_sync(spi, &m);
516
517 return len;
518}
519#else
520#define snd_soc_16_8_spi_write NULL
521#endif
522
Mark Brownbc6552f2010-03-05 16:27:15 +0000523#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
524static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
525 unsigned int r)
526{
Mark Brownbc6552f2010-03-05 16:27:15 +0000527 u16 reg = cpu_to_be16(r);
528 u16 data;
529 int ret;
Mark Brownbc6552f2010-03-05 16:27:15 +0000530
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000531 ret = do_i2c_read(codec, &reg, 2, &data, 2);
532 if (ret < 0)
Mark Brownbc6552f2010-03-05 16:27:15 +0000533 return 0;
Mark Brownbc6552f2010-03-05 16:27:15 +0000534 return be16_to_cpu(data);
535}
536#else
537#define snd_soc_16_16_read_i2c NULL
538#endif
539
540static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
541 unsigned int reg)
542{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000543 int ret;
544 unsigned int val;
Mark Brownbc6552f2010-03-05 16:27:15 +0000545
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000547 snd_soc_codec_volatile_register(codec, reg) ||
548 codec->cache_bypass) {
Mark Brownbc6552f2010-03-05 16:27:15 +0000549 if (codec->cache_only)
Dimitris Papastamos391d8a02010-09-21 17:04:07 +0100550 return -1;
Mark Brownbc6552f2010-03-05 16:27:15 +0000551
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000552 BUG_ON(!codec->hw_read);
Mark Brownbc6552f2010-03-05 16:27:15 +0000553 return codec->hw_read(codec, reg);
554 }
555
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000556 ret = snd_soc_cache_read(codec, reg, &val);
557 if (ret < 0)
558 return -1;
559
560 return val;
Mark Brownbc6552f2010-03-05 16:27:15 +0000561}
562
563static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
564 unsigned int value)
565{
Mark Brownbc6552f2010-03-05 16:27:15 +0000566 u8 data[4];
567 int ret;
568
569 data[0] = (reg >> 8) & 0xff;
570 data[1] = reg & 0xff;
571 data[2] = (value >> 8) & 0xff;
572 data[3] = value & 0xff;
573
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100574 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamosdad8e7a2011-01-19 14:53:36 +0000575 reg < codec->driver->reg_cache_size &&
576 !codec->cache_bypass) {
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000577 ret = snd_soc_cache_write(codec, reg, value);
578 if (ret < 0)
579 return -1;
580 }
Mark Brownbc6552f2010-03-05 16:27:15 +0000581
582 if (codec->cache_only) {
583 codec->cache_sync = 1;
584 return 0;
585 }
586
587 ret = codec->hw_write(codec->control_data, data, 4);
588 if (ret == 4)
589 return 0;
590 if (ret < 0)
591 return ret;
592 else
593 return -EIO;
594}
Barry Song994dc422010-01-27 11:46:18 +0800595
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100596#if defined(CONFIG_SPI_MASTER)
597static int snd_soc_16_16_spi_write(void *control_data, const char *data,
598 int len)
599{
600 struct spi_device *spi = control_data;
601 struct spi_transfer t;
602 struct spi_message m;
603 u8 msg[4];
604
605 if (len <= 0)
606 return 0;
607
608 msg[0] = data[0];
609 msg[1] = data[1];
610 msg[2] = data[2];
611 msg[3] = data[3];
612
613 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000614 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100615
616 t.tx_buf = &msg[0];
617 t.len = len;
618
619 spi_message_add_tail(&t, &m);
620 spi_sync(spi, &m);
621
622 return len;
623}
624#else
625#define snd_soc_16_16_spi_write NULL
626#endif
627
Mark Brown17a52fd2009-07-05 17:24:50 +0100628static struct {
629 int addr_bits;
630 int data_bits;
Mark Brownafa2f102009-07-10 23:11:24 +0100631 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
Mark Brown27ded042009-07-10 23:28:16 +0100632 int (*spi_write)(void *, const char *, int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100633 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
Mark Brownafa2f102009-07-10 23:11:24 +0100634 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100635} io_types[] = {
Mark Brownd62ab352009-09-21 04:21:47 -0700636 {
Barry Song63b62ab2010-01-27 11:46:17 +0800637 .addr_bits = 4, .data_bits = 12,
638 .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
639 .spi_write = snd_soc_4_12_spi_write,
640 },
641 {
Mark Brownd62ab352009-09-21 04:21:47 -0700642 .addr_bits = 7, .data_bits = 9,
643 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
Barry Song8998c892009-12-31 10:30:34 +0800644 .spi_write = snd_soc_7_9_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700645 },
646 {
647 .addr_bits = 8, .data_bits = 8,
648 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800649 .i2c_read = snd_soc_8_8_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100650 .spi_write = snd_soc_8_8_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700651 },
652 {
653 .addr_bits = 8, .data_bits = 16,
654 .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
655 .i2c_read = snd_soc_8_16_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100656 .spi_write = snd_soc_8_16_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700657 },
Barry Song994dc422010-01-27 11:46:18 +0800658 {
659 .addr_bits = 16, .data_bits = 8,
660 .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
661 .i2c_read = snd_soc_16_8_read_i2c,
662 .spi_write = snd_soc_16_8_spi_write,
663 },
Mark Brownbc6552f2010-03-05 16:27:15 +0000664 {
665 .addr_bits = 16, .data_bits = 16,
666 .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
667 .i2c_read = snd_soc_16_16_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100668 .spi_write = snd_soc_16_16_spi_write,
Mark Brownbc6552f2010-03-05 16:27:15 +0000669 },
Mark Brown17a52fd2009-07-05 17:24:50 +0100670};
671
672/**
673 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
674 *
675 * @codec: CODEC to configure.
676 * @type: Type of cache.
677 * @addr_bits: Number of bits of register address data.
678 * @data_bits: Number of bits of data per register.
Mark Brown7084a422009-07-10 22:24:27 +0100679 * @control: Control bus used.
Mark Brown17a52fd2009-07-05 17:24:50 +0100680 *
681 * Register formats are frequently shared between many I2C and SPI
682 * devices. In order to promote code reuse the ASoC core provides
683 * some standard implementations of CODEC read and write operations
684 * which can be set up using this function.
685 *
686 * The caller is responsible for allocating and initialising the
687 * actual cache.
688 *
689 * Note that at present this code cannot be used by CODECs with
690 * volatile registers.
691 */
692int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Mark Brown7084a422009-07-10 22:24:27 +0100693 int addr_bits, int data_bits,
694 enum snd_soc_control_type control)
Mark Brown17a52fd2009-07-05 17:24:50 +0100695{
696 int i;
697
Mark Brown17a52fd2009-07-05 17:24:50 +0100698 for (i = 0; i < ARRAY_SIZE(io_types); i++)
699 if (io_types[i].addr_bits == addr_bits &&
700 io_types[i].data_bits == data_bits)
701 break;
702 if (i == ARRAY_SIZE(io_types)) {
703 printk(KERN_ERR
704 "No I/O functions for %d bit address %d bit data\n",
705 addr_bits, data_bits);
706 return -EINVAL;
707 }
708
Mark Brownc3acec22010-12-02 16:15:29 +0000709 codec->write = io_types[i].write;
710 codec->read = io_types[i].read;
Mark Brown17a52fd2009-07-05 17:24:50 +0100711
Mark Brown7084a422009-07-10 22:24:27 +0100712 switch (control) {
713 case SND_SOC_CUSTOM:
714 break;
715
716 case SND_SOC_I2C:
Randy Dunlap17244c22009-08-10 16:04:39 -0700717#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brown7084a422009-07-10 22:24:27 +0100718 codec->hw_write = (hw_write_t)i2c_master_send;
719#endif
Mark Brownafa2f102009-07-10 23:11:24 +0100720 if (io_types[i].i2c_read)
721 codec->hw_read = io_types[i].i2c_read;
Mark Browna6d14342010-08-12 10:59:15 +0100722
723 codec->control_data = container_of(codec->dev,
724 struct i2c_client,
725 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100726 break;
727
728 case SND_SOC_SPI:
Mark Brown27ded042009-07-10 23:28:16 +0100729 if (io_types[i].spi_write)
730 codec->hw_write = io_types[i].spi_write;
Mark Browna6d14342010-08-12 10:59:15 +0100731
732 codec->control_data = container_of(codec->dev,
733 struct spi_device,
734 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100735 break;
736 }
737
Mark Brown17a52fd2009-07-05 17:24:50 +0100738 return 0;
739}
740EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000741
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000742static bool snd_soc_set_cache_val(void *base, unsigned int idx,
743 unsigned int val, unsigned int word_size)
744{
745 switch (word_size) {
746 case 1: {
747 u8 *cache = base;
748 if (cache[idx] == val)
749 return true;
750 cache[idx] = val;
751 break;
752 }
753 case 2: {
754 u16 *cache = base;
755 if (cache[idx] == val)
756 return true;
757 cache[idx] = val;
758 break;
759 }
760 default:
761 BUG();
762 }
763 return false;
764}
765
766static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
767 unsigned int word_size)
768{
769 switch (word_size) {
770 case 1: {
771 const u8 *cache = base;
772 return cache[idx];
773 }
774 case 2: {
775 const u16 *cache = base;
776 return cache[idx];
777 }
778 default:
779 BUG();
780 }
781 /* unreachable */
782 return -1;
783}
784
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000785struct snd_soc_rbtree_node {
786 struct rb_node node;
787 unsigned int reg;
788 unsigned int value;
789 unsigned int defval;
790} __attribute__ ((packed));
791
792struct snd_soc_rbtree_ctx {
793 struct rb_root root;
794};
795
796static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
797 struct rb_root *root, unsigned int reg)
798{
799 struct rb_node *node;
800 struct snd_soc_rbtree_node *rbnode;
801
802 node = root->rb_node;
803 while (node) {
804 rbnode = container_of(node, struct snd_soc_rbtree_node, node);
805 if (rbnode->reg < reg)
806 node = node->rb_left;
807 else if (rbnode->reg > reg)
808 node = node->rb_right;
809 else
810 return rbnode;
811 }
812
813 return NULL;
814}
815
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000816static int snd_soc_rbtree_insert(struct rb_root *root,
817 struct snd_soc_rbtree_node *rbnode)
818{
819 struct rb_node **new, *parent;
820 struct snd_soc_rbtree_node *rbnode_tmp;
821
822 parent = NULL;
823 new = &root->rb_node;
824 while (*new) {
825 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
826 node);
827 parent = *new;
828 if (rbnode_tmp->reg < rbnode->reg)
829 new = &((*new)->rb_left);
830 else if (rbnode_tmp->reg > rbnode->reg)
831 new = &((*new)->rb_right);
832 else
833 return 0;
834 }
835
836 /* insert the node into the rbtree */
837 rb_link_node(&rbnode->node, parent, new);
838 rb_insert_color(&rbnode->node, root);
839
840 return 1;
841}
842
843static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
844{
845 struct snd_soc_rbtree_ctx *rbtree_ctx;
846 struct rb_node *node;
847 struct snd_soc_rbtree_node *rbnode;
848 unsigned int val;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000849 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000850
851 rbtree_ctx = codec->reg_cache;
852 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
853 rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
854 if (rbnode->value == rbnode->defval)
855 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000856 ret = snd_soc_cache_read(codec, rbnode->reg, &val);
857 if (ret)
858 return ret;
Dimitris Papastamos99780072011-01-19 14:53:37 +0000859 codec->cache_bypass = 1;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000860 ret = snd_soc_write(codec, rbnode->reg, val);
Dimitris Papastamos99780072011-01-19 14:53:37 +0000861 codec->cache_bypass = 0;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000862 if (ret)
863 return ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000864 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
865 rbnode->reg, val);
866 }
867
868 return 0;
869}
870
871static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
872 unsigned int reg, unsigned int value)
873{
874 struct snd_soc_rbtree_ctx *rbtree_ctx;
875 struct snd_soc_rbtree_node *rbnode;
876
877 rbtree_ctx = codec->reg_cache;
878 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
879 if (rbnode) {
880 if (rbnode->value == value)
881 return 0;
882 rbnode->value = value;
883 } else {
884 /* bail out early, no need to create the rbnode yet */
885 if (!value)
886 return 0;
887 /*
888 * for uninitialized registers whose value is changed
889 * from the default zero, create an rbnode and insert
890 * it into the tree.
891 */
892 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
893 if (!rbnode)
894 return -ENOMEM;
895 rbnode->reg = reg;
896 rbnode->value = value;
897 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
898 }
899
900 return 0;
901}
902
903static int snd_soc_rbtree_cache_read(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 *value = rbnode->value;
913 } else {
914 /* uninitialized registers default to 0 */
915 *value = 0;
916 }
917
918 return 0;
919}
920
921static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
922{
923 struct rb_node *next;
924 struct snd_soc_rbtree_ctx *rbtree_ctx;
925 struct snd_soc_rbtree_node *rbtree_node;
926
927 /* if we've already been called then just return */
928 rbtree_ctx = codec->reg_cache;
929 if (!rbtree_ctx)
930 return 0;
931
932 /* free up the rbtree */
933 next = rb_first(&rbtree_ctx->root);
934 while (next) {
935 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
936 next = rb_next(&rbtree_node->node);
937 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
938 kfree(rbtree_node);
939 }
940
941 /* release the resources */
942 kfree(codec->reg_cache);
943 codec->reg_cache = NULL;
944
945 return 0;
946}
947
948static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
949{
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000950 struct snd_soc_rbtree_node *rbtree_node;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000951 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000952 unsigned int val;
953 unsigned int word_size;
954 int i;
955 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000956
957 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
958 if (!codec->reg_cache)
959 return -ENOMEM;
960
961 rbtree_ctx = codec->reg_cache;
962 rbtree_ctx->root = RB_ROOT;
963
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +0000964 if (!codec->reg_def_copy)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000965 return 0;
966
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000967 /*
968 * populate the rbtree with the initialized registers. All other
969 * registers will be inserted when they are first modified.
970 */
971 word_size = codec->driver->reg_word_size;
972 for (i = 0; i < codec->driver->reg_cache_size; ++i) {
973 val = snd_soc_get_cache_val(codec->reg_def_copy, i, word_size);
974 if (!val)
975 continue;
976 rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL);
977 if (!rbtree_node) {
978 ret = -ENOMEM;
979 snd_soc_cache_exit(codec);
980 break;
981 }
982 rbtree_node->reg = i;
983 rbtree_node->value = val;
984 rbtree_node->defval = val;
985 snd_soc_rbtree_insert(&rbtree_ctx->root, rbtree_node);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000986 }
987
988 return 0;
989}
990
Mark Brown68d44ee2010-12-21 17:19:56 +0000991#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000992struct snd_soc_lzo_ctx {
993 void *wmem;
994 void *dst;
995 const void *src;
996 size_t src_len;
997 size_t dst_len;
998 size_t decompressed_size;
999 unsigned long *sync_bmp;
1000 int sync_bmp_nbits;
1001};
1002
1003#define LZO_BLOCK_NUM 8
1004static int snd_soc_lzo_block_count(void)
1005{
1006 return LZO_BLOCK_NUM;
1007}
1008
1009static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
1010{
1011 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
1012 if (!lzo_ctx->wmem)
1013 return -ENOMEM;
1014 return 0;
1015}
1016
1017static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
1018{
1019 size_t compress_size;
1020 int ret;
1021
1022 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
1023 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
1024 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
1025 return -EINVAL;
1026 lzo_ctx->dst_len = compress_size;
1027 return 0;
1028}
1029
1030static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
1031{
1032 size_t dst_len;
1033 int ret;
1034
1035 dst_len = lzo_ctx->dst_len;
1036 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
1037 lzo_ctx->dst, &dst_len);
1038 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
1039 return -EINVAL;
1040 return 0;
1041}
1042
1043static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
1044 struct snd_soc_lzo_ctx *lzo_ctx)
1045{
1046 int ret;
1047
1048 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
1049 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1050 if (!lzo_ctx->dst) {
1051 lzo_ctx->dst_len = 0;
1052 return -ENOMEM;
1053 }
1054
1055 ret = snd_soc_lzo_compress(lzo_ctx);
1056 if (ret < 0)
1057 return ret;
1058 return 0;
1059}
1060
1061static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
1062 struct snd_soc_lzo_ctx *lzo_ctx)
1063{
1064 int ret;
1065
1066 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
1067 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1068 if (!lzo_ctx->dst) {
1069 lzo_ctx->dst_len = 0;
1070 return -ENOMEM;
1071 }
1072
1073 ret = snd_soc_lzo_decompress(lzo_ctx);
1074 if (ret < 0)
1075 return ret;
1076 return 0;
1077}
1078
1079static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
1080 unsigned int reg)
1081{
Mark Brown001ae4c2010-12-02 16:21:08 +00001082 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001083
1084 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001085 return (reg * codec_drv->reg_word_size) /
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001086 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001087}
1088
1089static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
1090 unsigned int reg)
1091{
Mark Brown001ae4c2010-12-02 16:21:08 +00001092 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001093
1094 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001095 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001096 codec_drv->reg_word_size);
1097}
1098
1099static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
1100{
Mark Brown001ae4c2010-12-02 16:21:08 +00001101 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001102
1103 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001104 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001105}
1106
1107static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
1108{
1109 struct snd_soc_lzo_ctx **lzo_blocks;
1110 unsigned int val;
1111 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001112 int ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001113
1114 lzo_blocks = codec->reg_cache;
1115 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001116 ret = snd_soc_cache_read(codec, i, &val);
1117 if (ret)
1118 return ret;
Dimitris Papastamos99780072011-01-19 14:53:37 +00001119 codec->cache_bypass = 1;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001120 ret = snd_soc_write(codec, i, val);
Dimitris Papastamos99780072011-01-19 14:53:37 +00001121 codec->cache_bypass = 0;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001122 if (ret)
1123 return ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001124 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1125 i, val);
1126 }
1127
1128 return 0;
1129}
1130
1131static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
1132 unsigned int reg, unsigned int value)
1133{
1134 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1135 int ret, blkindex, blkpos;
1136 size_t blksize, tmp_dst_len;
1137 void *tmp_dst;
1138
1139 /* index of the compressed lzo block */
1140 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1141 /* register index within the decompressed block */
1142 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1143 /* size of the compressed block */
1144 blksize = snd_soc_lzo_get_blksize(codec);
1145 lzo_blocks = codec->reg_cache;
1146 lzo_block = lzo_blocks[blkindex];
1147
1148 /* save the pointer and length of the compressed block */
1149 tmp_dst = lzo_block->dst;
1150 tmp_dst_len = lzo_block->dst_len;
1151
1152 /* prepare the source to be the compressed block */
1153 lzo_block->src = lzo_block->dst;
1154 lzo_block->src_len = lzo_block->dst_len;
1155
1156 /* decompress the block */
1157 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1158 if (ret < 0) {
1159 kfree(lzo_block->dst);
1160 goto out;
1161 }
1162
1163 /* write the new value to the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001164 if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
1165 codec->driver->reg_word_size)) {
1166 kfree(lzo_block->dst);
1167 goto out;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001168 }
1169
1170 /* prepare the source to be the decompressed block */
1171 lzo_block->src = lzo_block->dst;
1172 lzo_block->src_len = lzo_block->dst_len;
1173
1174 /* compress the block */
1175 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1176 if (ret < 0) {
1177 kfree(lzo_block->dst);
1178 kfree(lzo_block->src);
1179 goto out;
1180 }
1181
1182 /* set the bit so we know we have to sync this register */
1183 set_bit(reg, lzo_block->sync_bmp);
1184 kfree(tmp_dst);
1185 kfree(lzo_block->src);
1186 return 0;
1187out:
1188 lzo_block->dst = tmp_dst;
1189 lzo_block->dst_len = tmp_dst_len;
1190 return ret;
1191}
1192
1193static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1194 unsigned int reg, unsigned int *value)
1195{
1196 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1197 int ret, blkindex, blkpos;
1198 size_t blksize, tmp_dst_len;
1199 void *tmp_dst;
1200
1201 *value = 0;
1202 /* index of the compressed lzo block */
1203 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1204 /* register index within the decompressed block */
1205 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1206 /* size of the compressed block */
1207 blksize = snd_soc_lzo_get_blksize(codec);
1208 lzo_blocks = codec->reg_cache;
1209 lzo_block = lzo_blocks[blkindex];
1210
1211 /* save the pointer and length of the compressed block */
1212 tmp_dst = lzo_block->dst;
1213 tmp_dst_len = lzo_block->dst_len;
1214
1215 /* prepare the source to be the compressed block */
1216 lzo_block->src = lzo_block->dst;
1217 lzo_block->src_len = lzo_block->dst_len;
1218
1219 /* decompress the block */
1220 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001221 if (ret >= 0)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001222 /* fetch the value from the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001223 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1224 codec->driver->reg_word_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001225
1226 kfree(lzo_block->dst);
1227 /* restore the pointer and length of the compressed block */
1228 lzo_block->dst = tmp_dst;
1229 lzo_block->dst_len = tmp_dst_len;
1230 return 0;
1231}
1232
1233static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1234{
1235 struct snd_soc_lzo_ctx **lzo_blocks;
1236 int i, blkcount;
1237
1238 lzo_blocks = codec->reg_cache;
1239 if (!lzo_blocks)
1240 return 0;
1241
1242 blkcount = snd_soc_lzo_block_count();
1243 /*
1244 * the pointer to the bitmap used for syncing the cache
1245 * is shared amongst all lzo_blocks. Ensure it is freed
1246 * only once.
1247 */
1248 if (lzo_blocks[0])
1249 kfree(lzo_blocks[0]->sync_bmp);
1250 for (i = 0; i < blkcount; ++i) {
1251 if (lzo_blocks[i]) {
1252 kfree(lzo_blocks[i]->wmem);
1253 kfree(lzo_blocks[i]->dst);
1254 }
1255 /* each lzo_block is a pointer returned by kmalloc or NULL */
1256 kfree(lzo_blocks[i]);
1257 }
1258 kfree(lzo_blocks);
1259 codec->reg_cache = NULL;
1260 return 0;
1261}
1262
1263static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1264{
1265 struct snd_soc_lzo_ctx **lzo_blocks;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001266 size_t bmp_size;
Mark Brown001ae4c2010-12-02 16:21:08 +00001267 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001268 int ret, tofree, i, blksize, blkcount;
1269 const char *p, *end;
1270 unsigned long *sync_bmp;
1271
1272 ret = 0;
1273 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001274
1275 /*
1276 * If we have not been given a default register cache
1277 * then allocate a dummy zero-ed out region, compress it
1278 * and remember to free it afterwards.
1279 */
1280 tofree = 0;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001281 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001282 tofree = 1;
1283
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001284 if (!codec->reg_def_copy) {
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001285 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001286 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001287 return -ENOMEM;
1288 }
1289
1290 blkcount = snd_soc_lzo_block_count();
1291 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1292 GFP_KERNEL);
1293 if (!codec->reg_cache) {
1294 ret = -ENOMEM;
1295 goto err_tofree;
1296 }
1297 lzo_blocks = codec->reg_cache;
1298
1299 /*
1300 * allocate a bitmap to be used when syncing the cache with
1301 * the hardware. Each time a register is modified, the corresponding
1302 * bit is set in the bitmap, so we know that we have to sync
1303 * that register.
1304 */
1305 bmp_size = codec_drv->reg_cache_size;
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +00001306 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001307 GFP_KERNEL);
1308 if (!sync_bmp) {
1309 ret = -ENOMEM;
1310 goto err;
1311 }
Dimitris Papastamos09c74a92010-11-29 11:43:33 +00001312 bitmap_zero(sync_bmp, bmp_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001313
1314 /* allocate the lzo blocks and initialize them */
1315 for (i = 0; i < blkcount; ++i) {
1316 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1317 GFP_KERNEL);
1318 if (!lzo_blocks[i]) {
1319 kfree(sync_bmp);
1320 ret = -ENOMEM;
1321 goto err;
1322 }
1323 lzo_blocks[i]->sync_bmp = sync_bmp;
Dimitris Papastamos04f8fd12011-01-11 11:24:02 +00001324 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001325 /* alloc the working space for the compressed block */
1326 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1327 if (ret < 0)
1328 goto err;
1329 }
1330
1331 blksize = snd_soc_lzo_get_blksize(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001332 p = codec->reg_def_copy;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001333 end = codec->reg_def_copy + codec->reg_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001334 /* compress the register map and fill the lzo blocks */
1335 for (i = 0; i < blkcount; ++i, p += blksize) {
1336 lzo_blocks[i]->src = p;
1337 if (p + blksize > end)
1338 lzo_blocks[i]->src_len = end - p;
1339 else
1340 lzo_blocks[i]->src_len = blksize;
1341 ret = snd_soc_lzo_compress_cache_block(codec,
1342 lzo_blocks[i]);
1343 if (ret < 0)
1344 goto err;
1345 lzo_blocks[i]->decompressed_size =
1346 lzo_blocks[i]->src_len;
1347 }
1348
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001349 if (tofree) {
1350 kfree(codec->reg_def_copy);
1351 codec->reg_def_copy = NULL;
1352 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001353 return 0;
1354err:
1355 snd_soc_cache_exit(codec);
1356err_tofree:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001357 if (tofree) {
1358 kfree(codec->reg_def_copy);
1359 codec->reg_def_copy = NULL;
1360 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001361 return ret;
1362}
Mark Brown68d44ee2010-12-21 17:19:56 +00001363#endif
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001364
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001365static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1366{
1367 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001368 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +00001369 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001370 unsigned int val;
1371
1372 codec_drv = codec->driver;
1373 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001374 ret = snd_soc_cache_read(codec, i, &val);
1375 if (ret)
1376 return ret;
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001377 if (codec->reg_def_copy)
1378 if (snd_soc_get_cache_val(codec->reg_def_copy,
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001379 i, codec_drv->reg_word_size) == val)
1380 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001381 ret = snd_soc_write(codec, i, val);
1382 if (ret)
1383 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001384 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1385 i, val);
1386 }
1387 return 0;
1388}
1389
1390static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1391 unsigned int reg, unsigned int value)
1392{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001393 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1394 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001395 return 0;
1396}
1397
1398static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1399 unsigned int reg, unsigned int *value)
1400{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001401 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1402 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001403 return 0;
1404}
1405
1406static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1407{
1408 if (!codec->reg_cache)
1409 return 0;
1410 kfree(codec->reg_cache);
1411 codec->reg_cache = NULL;
1412 return 0;
1413}
1414
1415static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1416{
Mark Brown001ae4c2010-12-02 16:21:08 +00001417 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001418
1419 codec_drv = codec->driver;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001420
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001421 if (codec->reg_def_copy)
1422 codec->reg_cache = kmemdup(codec->reg_def_copy,
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001423 codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001424 else
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001425 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001426 if (!codec->reg_cache)
1427 return -ENOMEM;
1428
1429 return 0;
1430}
1431
1432/* an array of all supported compression types */
1433static const struct snd_soc_cache_ops cache_types[] = {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001434 /* Flat *must* be the first entry for fallback */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001435 {
Dimitris Papastamosdf0701b2010-11-29 10:54:28 +00001436 .id = SND_SOC_FLAT_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001437 .name = "flat",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001438 .init = snd_soc_flat_cache_init,
1439 .exit = snd_soc_flat_cache_exit,
1440 .read = snd_soc_flat_cache_read,
1441 .write = snd_soc_flat_cache_write,
1442 .sync = snd_soc_flat_cache_sync
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001443 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001444#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001445 {
1446 .id = SND_SOC_LZO_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001447 .name = "LZO",
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001448 .init = snd_soc_lzo_cache_init,
1449 .exit = snd_soc_lzo_cache_exit,
1450 .read = snd_soc_lzo_cache_read,
1451 .write = snd_soc_lzo_cache_write,
1452 .sync = snd_soc_lzo_cache_sync
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001453 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001454#endif
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001455 {
1456 .id = SND_SOC_RBTREE_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001457 .name = "rbtree",
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001458 .init = snd_soc_rbtree_cache_init,
1459 .exit = snd_soc_rbtree_cache_exit,
1460 .read = snd_soc_rbtree_cache_read,
1461 .write = snd_soc_rbtree_cache_write,
1462 .sync = snd_soc_rbtree_cache_sync
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001463 }
1464};
1465
1466int snd_soc_cache_init(struct snd_soc_codec *codec)
1467{
1468 int i;
1469
1470 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00001471 if (cache_types[i].id == codec->compress_type)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001472 break;
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001473
1474 /* Fall back to flat compression */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001475 if (i == ARRAY_SIZE(cache_types)) {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001476 dev_warn(codec->dev, "Could not match compress type: %d\n",
1477 codec->compress_type);
1478 i = 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001479 }
1480
1481 mutex_init(&codec->cache_rw_mutex);
1482 codec->cache_ops = &cache_types[i];
1483
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001484 if (codec->cache_ops->init) {
1485 if (codec->cache_ops->name)
1486 dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1487 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001488 return codec->cache_ops->init(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001489 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001490 return -EINVAL;
1491}
1492
1493/*
1494 * NOTE: keep in mind that this function might be called
1495 * multiple times.
1496 */
1497int snd_soc_cache_exit(struct snd_soc_codec *codec)
1498{
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001499 if (codec->cache_ops && codec->cache_ops->exit) {
1500 if (codec->cache_ops->name)
1501 dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1502 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001503 return codec->cache_ops->exit(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001504 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001505 return -EINVAL;
1506}
1507
1508/**
1509 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1510 *
1511 * @codec: CODEC to configure.
1512 * @reg: The register index.
1513 * @value: The value to be returned.
1514 */
1515int snd_soc_cache_read(struct snd_soc_codec *codec,
1516 unsigned int reg, unsigned int *value)
1517{
1518 int ret;
1519
1520 mutex_lock(&codec->cache_rw_mutex);
1521
1522 if (value && codec->cache_ops && codec->cache_ops->read) {
1523 ret = codec->cache_ops->read(codec, reg, value);
1524 mutex_unlock(&codec->cache_rw_mutex);
1525 return ret;
1526 }
1527
1528 mutex_unlock(&codec->cache_rw_mutex);
1529 return -EINVAL;
1530}
1531EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1532
1533/**
1534 * snd_soc_cache_write: Set the value of a given register in the cache.
1535 *
1536 * @codec: CODEC to configure.
1537 * @reg: The register index.
1538 * @value: The new register value.
1539 */
1540int snd_soc_cache_write(struct snd_soc_codec *codec,
1541 unsigned int reg, unsigned int value)
1542{
1543 int ret;
1544
1545 mutex_lock(&codec->cache_rw_mutex);
1546
1547 if (codec->cache_ops && codec->cache_ops->write) {
1548 ret = codec->cache_ops->write(codec, reg, value);
1549 mutex_unlock(&codec->cache_rw_mutex);
1550 return ret;
1551 }
1552
1553 mutex_unlock(&codec->cache_rw_mutex);
1554 return -EINVAL;
1555}
1556EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1557
1558/**
1559 * snd_soc_cache_sync: Sync the register cache with the hardware.
1560 *
1561 * @codec: CODEC to configure.
1562 *
1563 * Any registers that should not be synced should be marked as
1564 * volatile. In general drivers can choose not to use the provided
1565 * syncing functionality if they so require.
1566 */
1567int snd_soc_cache_sync(struct snd_soc_codec *codec)
1568{
1569 int ret;
Dimitris Papastamosc358e642011-01-21 15:29:02 +00001570 const char *name;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001571
1572 if (!codec->cache_sync) {
1573 return 0;
1574 }
1575
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001576 if (!codec->cache_ops || !codec->cache_ops->sync)
1577 return -EINVAL;
1578
Dimitris Papastamosc358e642011-01-21 15:29:02 +00001579 if (codec->cache_ops->name)
1580 name = codec->cache_ops->name;
1581 else
1582 name = "unknown";
1583
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001584 if (codec->cache_ops->name)
1585 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1586 codec->cache_ops->name, codec->name);
1587 trace_snd_soc_cache_sync(codec, name, "start");
1588 ret = codec->cache_ops->sync(codec);
1589 if (!ret)
1590 codec->cache_sync = 0;
1591 trace_snd_soc_cache_sync(codec, name, "end");
1592 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001593}
1594EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
Dimitris Papastamos066d16c2011-01-13 12:20:36 +00001595
1596static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1597 unsigned int reg)
1598{
1599 const struct snd_soc_codec_driver *codec_drv;
1600 unsigned int min, max, index;
1601
1602 codec_drv = codec->driver;
1603 min = 0;
1604 max = codec_drv->reg_access_size - 1;
1605 do {
1606 index = (min + max) / 2;
1607 if (codec_drv->reg_access_default[index].reg == reg)
1608 return index;
1609 if (codec_drv->reg_access_default[index].reg < reg)
1610 min = index + 1;
1611 else
1612 max = index;
1613 } while (min <= max);
1614 return -1;
1615}
1616
1617int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1618 unsigned int reg)
1619{
1620 int index;
1621
1622 if (reg >= codec->driver->reg_cache_size)
1623 return 1;
1624 index = snd_soc_get_reg_access_index(codec, reg);
1625 if (index < 0)
1626 return 0;
1627 return codec->driver->reg_access_default[index].vol;
1628}
1629EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1630
1631int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1632 unsigned int reg)
1633{
1634 int index;
1635
1636 if (reg >= codec->driver->reg_cache_size)
1637 return 1;
1638 index = snd_soc_get_reg_access_index(codec, reg);
1639 if (index < 0)
1640 return 0;
1641 return codec->driver->reg_access_default[index].read;
1642}
1643EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);