blob: 78c9869615dfa02c5273ace6d9a1a948bbe8a77f [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
Dimitris Papastamos30539a12011-03-22 10:37:00 +000023#if defined(CONFIG_SPI_MASTER)
24static int do_spi_write(void *control_data, const void *msg,
25 int len)
26{
27 struct spi_device *spi = control_data;
28 struct spi_transfer t;
29 struct spi_message m;
30
31 if (len <= 0)
32 return 0;
33
34 spi_message_init(&m);
35 memset(&t, 0, sizeof t);
36
37 t.tx_buf = msg;
38 t.len = len;
39
40 spi_message_add_tail(&t, &m);
41 spi_sync(spi, &m);
42
43 return len;
44}
45#endif
46
Dimitris Papastamos26e99842011-03-22 10:36:58 +000047static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg,
48 unsigned int value, const void *data, int len)
49{
50 int ret;
51
52 if (!snd_soc_codec_volatile_register(codec, reg) &&
53 reg < codec->driver->reg_cache_size &&
54 !codec->cache_bypass) {
55 ret = snd_soc_cache_write(codec, reg, value);
56 if (ret < 0)
57 return -1;
58 }
59
60 if (codec->cache_only) {
61 codec->cache_sync = 1;
62 return 0;
63 }
64
65 ret = codec->hw_write(codec->control_data, data, len);
66 if (ret == len)
67 return 0;
68 if (ret < 0)
69 return ret;
70 else
71 return -EIO;
72}
73
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000074static unsigned int do_hw_read(struct snd_soc_codec *codec, unsigned int reg)
Barry Song63b62ab2010-01-27 11:46:17 +080075{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000076 int ret;
77 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010078
79 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000080 snd_soc_codec_volatile_register(codec, reg) ||
81 codec->cache_bypass) {
82 if (codec->cache_only)
83 return -1;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010084
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000085 BUG_ON(!codec->hw_read);
86 return codec->hw_read(codec, reg);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010087 }
88
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000089 ret = snd_soc_cache_read(codec, reg, &val);
90 if (ret < 0)
91 return -1;
92 return val;
Barry Song63b62ab2010-01-27 11:46:17 +080093}
94
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000095static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +010096 unsigned int reg)
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000097{
98 return do_hw_read(codec, reg);
99}
100
Barry Song63b62ab2010-01-27 11:46:17 +0800101static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100102 unsigned int value)
Barry Song63b62ab2010-01-27 11:46:17 +0800103{
Barry Song63b62ab2010-01-27 11:46:17 +0800104 u8 data[2];
Barry Song63b62ab2010-01-27 11:46:17 +0800105
Barry Song63b62ab2010-01-27 11:46:17 +0800106 data[0] = (reg << 4) | ((value >> 8) & 0x000f);
107 data[1] = value & 0x00ff;
108
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000109 return do_hw_write(codec, reg, value, data, 2);
Barry Song63b62ab2010-01-27 11:46:17 +0800110}
111
112#if defined(CONFIG_SPI_MASTER)
113static int snd_soc_4_12_spi_write(void *control_data, const char *data,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100114 int len)
Barry Song63b62ab2010-01-27 11:46:17 +0800115{
Barry Song63b62ab2010-01-27 11:46:17 +0800116 u8 msg[2];
117
Barry Song63b62ab2010-01-27 11:46:17 +0800118 msg[0] = data[1];
119 msg[1] = data[0];
120
Dimitris Papastamos30539a12011-03-22 10:37:00 +0000121 return do_spi_write(control_data, msg, len);
Barry Song63b62ab2010-01-27 11:46:17 +0800122}
123#else
124#define snd_soc_4_12_spi_write NULL
125#endif
126
Mark Brown17a52fd2009-07-05 17:24:50 +0100127static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
128 unsigned int reg)
129{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000130 return do_hw_read(codec, reg);
Mark Brown17a52fd2009-07-05 17:24:50 +0100131}
132
133static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
134 unsigned int value)
135{
Mark Brown17a52fd2009-07-05 17:24:50 +0100136 u8 data[2];
Mark Brown17a52fd2009-07-05 17:24:50 +0100137
Mark Brown17a52fd2009-07-05 17:24:50 +0100138 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
139 data[1] = value & 0x00ff;
140
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000141 return do_hw_write(codec, reg, value, data, 2);
Mark Brown17a52fd2009-07-05 17:24:50 +0100142}
143
Mark Brown27ded042009-07-10 23:28:16 +0100144#if defined(CONFIG_SPI_MASTER)
145static int snd_soc_7_9_spi_write(void *control_data, const char *data,
146 int len)
147{
Mark Brown27ded042009-07-10 23:28:16 +0100148 u8 msg[2];
149
Mark Brown27ded042009-07-10 23:28:16 +0100150 msg[0] = data[0];
151 msg[1] = data[1];
152
Dimitris Papastamos30539a12011-03-22 10:37:00 +0000153 return do_spi_write(control_data, msg, len);
Mark Brown27ded042009-07-10 23:28:16 +0100154}
155#else
156#define snd_soc_7_9_spi_write NULL
157#endif
158
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900159static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
160 unsigned int value)
161{
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900162 u8 data[2];
163
Barry Songf4bee1b2010-03-18 16:17:01 +0800164 reg &= 0xff;
165 data[0] = reg;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900166 data[1] = value & 0xff;
167
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000168 return do_hw_write(codec, reg, value, data, 2);
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900169}
170
171static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
172 unsigned int reg)
173{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000174 return do_hw_read(codec, reg);
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900175}
176
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100177#if defined(CONFIG_SPI_MASTER)
178static int snd_soc_8_8_spi_write(void *control_data, const char *data,
179 int len)
180{
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100181 u8 msg[2];
182
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100183 msg[0] = data[0];
184 msg[1] = data[1];
185
Dimitris Papastamos30539a12011-03-22 10:37:00 +0000186 return do_spi_write(control_data, msg, len);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100187}
188#else
189#define snd_soc_8_8_spi_write NULL
190#endif
191
Mark Brownafa2f102009-07-10 23:11:24 +0100192static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
193 unsigned int value)
194{
Mark Brownafa2f102009-07-10 23:11:24 +0100195 u8 data[3];
196
197 data[0] = reg;
198 data[1] = (value >> 8) & 0xff;
199 data[2] = value & 0xff;
200
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000201 return do_hw_write(codec, reg, value, data, 3);
Mark Brownafa2f102009-07-10 23:11:24 +0100202}
203
204static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
205 unsigned int reg)
206{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000207 return do_hw_read(codec, reg);
Mark Brownafa2f102009-07-10 23:11:24 +0100208}
209
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100210#if defined(CONFIG_SPI_MASTER)
211static int snd_soc_8_16_spi_write(void *control_data, const char *data,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100212 int len)
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100213{
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100214 u8 msg[3];
215
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100216 msg[0] = data[0];
217 msg[1] = data[1];
218 msg[2] = data[2];
219
Dimitris Papastamos30539a12011-03-22 10:37:00 +0000220 return do_spi_write(control_data, msg, len);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100221}
222#else
223#define snd_soc_8_16_spi_write NULL
224#endif
225
Randy Dunlap17244c22009-08-10 16:04:39 -0700226#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000227static unsigned int do_i2c_read(struct snd_soc_codec *codec,
228 void *reg, int reglen,
229 void *data, int datalen)
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800230{
231 struct i2c_msg xfer[2];
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800232 int ret;
233 struct i2c_client *client = codec->control_data;
234
235 /* Write register */
236 xfer[0].addr = client->addr;
237 xfer[0].flags = 0;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000238 xfer[0].len = reglen;
239 xfer[0].buf = reg;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800240
241 /* Read data */
242 xfer[1].addr = client->addr;
243 xfer[1].flags = I2C_M_RD;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000244 xfer[1].len = datalen;
245 xfer[1].buf = data;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800246
247 ret = i2c_transfer(client->adapter, xfer, 2);
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000248 if (ret == 2)
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800249 return 0;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000250 else if (ret < 0)
251 return ret;
252 else
253 return -EIO;
254}
255#endif
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800256
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000257#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
258static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100259 unsigned int r)
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000260{
261 u8 reg = r;
262 u8 data;
263 int ret;
264
265 ret = do_i2c_read(codec, &reg, 1, &data, 1);
266 if (ret < 0)
267 return 0;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800268 return data;
269}
270#else
271#define snd_soc_8_8_read_i2c NULL
272#endif
273
274#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brownafa2f102009-07-10 23:11:24 +0100275static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
276 unsigned int r)
277{
Mark Brownafa2f102009-07-10 23:11:24 +0100278 u8 reg = r;
279 u16 data;
280 int ret;
Mark Brownafa2f102009-07-10 23:11:24 +0100281
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000282 ret = do_i2c_read(codec, &reg, 1, &data, 2);
283 if (ret < 0)
Mark Brownafa2f102009-07-10 23:11:24 +0100284 return 0;
Mark Brownafa2f102009-07-10 23:11:24 +0100285 return (data >> 8) | ((data & 0xff) << 8);
286}
287#else
288#define snd_soc_8_16_read_i2c NULL
289#endif
Mark Brown17a52fd2009-07-05 17:24:50 +0100290
Barry Song994dc422010-01-27 11:46:18 +0800291#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
292static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
293 unsigned int r)
294{
Barry Song994dc422010-01-27 11:46:18 +0800295 u16 reg = r;
296 u8 data;
297 int ret;
Barry Song994dc422010-01-27 11:46:18 +0800298
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000299 ret = do_i2c_read(codec, &reg, 2, &data, 1);
300 if (ret < 0)
Barry Song994dc422010-01-27 11:46:18 +0800301 return 0;
Barry Song994dc422010-01-27 11:46:18 +0800302 return data;
303}
304#else
305#define snd_soc_16_8_read_i2c NULL
306#endif
307
308static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100309 unsigned int reg)
Barry Song994dc422010-01-27 11:46:18 +0800310{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000311 return do_hw_read(codec, reg);
Barry Song994dc422010-01-27 11:46:18 +0800312}
313
314static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100315 unsigned int value)
Barry Song994dc422010-01-27 11:46:18 +0800316{
Barry Song994dc422010-01-27 11:46:18 +0800317 u8 data[3];
Barry Song994dc422010-01-27 11:46:18 +0800318
Barry Song994dc422010-01-27 11:46:18 +0800319 data[0] = (reg >> 8) & 0xff;
320 data[1] = reg & 0xff;
321 data[2] = value;
Barry Song994dc422010-01-27 11:46:18 +0800322 reg &= 0xff;
Mark Brown8c961bc2010-02-01 18:46:10 +0000323
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000324 return do_hw_write(codec, reg, value, data, 3);
Barry Song994dc422010-01-27 11:46:18 +0800325}
326
327#if defined(CONFIG_SPI_MASTER)
328static int snd_soc_16_8_spi_write(void *control_data, const char *data,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100329 int len)
Barry Song994dc422010-01-27 11:46:18 +0800330{
Barry Song994dc422010-01-27 11:46:18 +0800331 u8 msg[3];
332
Barry Song994dc422010-01-27 11:46:18 +0800333 msg[0] = data[0];
334 msg[1] = data[1];
335 msg[2] = data[2];
336
Dimitris Papastamos30539a12011-03-22 10:37:00 +0000337 return do_spi_write(control_data, msg, len);
Barry Song994dc422010-01-27 11:46:18 +0800338}
339#else
340#define snd_soc_16_8_spi_write NULL
341#endif
342
Mark Brownbc6552f2010-03-05 16:27:15 +0000343#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
344static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
345 unsigned int r)
346{
Mark Brownbc6552f2010-03-05 16:27:15 +0000347 u16 reg = cpu_to_be16(r);
348 u16 data;
349 int ret;
Mark Brownbc6552f2010-03-05 16:27:15 +0000350
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000351 ret = do_i2c_read(codec, &reg, 2, &data, 2);
352 if (ret < 0)
Mark Brownbc6552f2010-03-05 16:27:15 +0000353 return 0;
Mark Brownbc6552f2010-03-05 16:27:15 +0000354 return be16_to_cpu(data);
355}
356#else
357#define snd_soc_16_16_read_i2c NULL
358#endif
359
360static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
361 unsigned int reg)
362{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000363 return do_hw_read(codec, reg);
Mark Brownbc6552f2010-03-05 16:27:15 +0000364}
365
366static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
367 unsigned int value)
368{
Mark Brownbc6552f2010-03-05 16:27:15 +0000369 u8 data[4];
Mark Brownbc6552f2010-03-05 16:27:15 +0000370
371 data[0] = (reg >> 8) & 0xff;
372 data[1] = reg & 0xff;
373 data[2] = (value >> 8) & 0xff;
374 data[3] = value & 0xff;
375
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000376 return do_hw_write(codec, reg, value, data, 4);
Mark Brownbc6552f2010-03-05 16:27:15 +0000377}
Barry Song994dc422010-01-27 11:46:18 +0800378
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100379#if defined(CONFIG_SPI_MASTER)
380static int snd_soc_16_16_spi_write(void *control_data, const char *data,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100381 int len)
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100382{
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100383 u8 msg[4];
384
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100385 msg[0] = data[0];
386 msg[1] = data[1];
387 msg[2] = data[2];
388 msg[3] = data[3];
389
Dimitris Papastamos30539a12011-03-22 10:37:00 +0000390 return do_spi_write(control_data, msg, len);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100391}
392#else
393#define snd_soc_16_16_spi_write NULL
394#endif
395
Mark Brown34bad692011-04-04 17:55:42 +0900396/* Primitive bulk write support for soc-cache. The data pointed to by
397 * `data' needs to already be in the form the hardware expects
398 * including any leading register specific data. Any data written
399 * through this function will not go through the cache as it only
400 * handles writing to volatile or out of bounds registers.
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000401 */
402static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg,
403 const void *data, size_t len)
404{
405 int ret;
406
Dimitris Papastamos64d27062011-05-05 14:18:11 +0100407 /* To ensure that we don't get out of sync with the cache, check
408 * whether the base register is volatile or if we've directly asked
409 * to bypass the cache. Out of bounds registers are considered
410 * volatile.
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000411 */
Dimitris Papastamos64d27062011-05-05 14:18:11 +0100412 if (!codec->cache_bypass
413 && !snd_soc_codec_volatile_register(codec, reg)
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000414 && reg < codec->driver->reg_cache_size)
415 return -EINVAL;
416
417 switch (codec->control_type) {
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900418#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000419 case SND_SOC_I2C:
420 ret = i2c_master_send(codec->control_data, data, len);
421 break;
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900422#endif
423#if defined(CONFIG_SPI_MASTER)
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000424 case SND_SOC_SPI:
425 ret = do_spi_write(codec->control_data, data, len);
426 break;
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900427#endif
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000428 default:
429 BUG();
430 }
431
432 if (ret == len)
433 return 0;
434 if (ret < 0)
435 return ret;
436 else
437 return -EIO;
438}
439
Mark Brown17a52fd2009-07-05 17:24:50 +0100440static struct {
441 int addr_bits;
442 int data_bits;
Mark Brownafa2f102009-07-10 23:11:24 +0100443 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
Mark Brown27ded042009-07-10 23:28:16 +0100444 int (*spi_write)(void *, const char *, int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100445 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
Mark Brownafa2f102009-07-10 23:11:24 +0100446 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100447} io_types[] = {
Mark Brownd62ab352009-09-21 04:21:47 -0700448 {
Barry Song63b62ab2010-01-27 11:46:17 +0800449 .addr_bits = 4, .data_bits = 12,
450 .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
451 .spi_write = snd_soc_4_12_spi_write,
452 },
453 {
Mark Brownd62ab352009-09-21 04:21:47 -0700454 .addr_bits = 7, .data_bits = 9,
455 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
Barry Song8998c892009-12-31 10:30:34 +0800456 .spi_write = snd_soc_7_9_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700457 },
458 {
459 .addr_bits = 8, .data_bits = 8,
460 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800461 .i2c_read = snd_soc_8_8_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100462 .spi_write = snd_soc_8_8_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700463 },
464 {
465 .addr_bits = 8, .data_bits = 16,
466 .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
467 .i2c_read = snd_soc_8_16_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100468 .spi_write = snd_soc_8_16_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700469 },
Barry Song994dc422010-01-27 11:46:18 +0800470 {
471 .addr_bits = 16, .data_bits = 8,
472 .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
473 .i2c_read = snd_soc_16_8_read_i2c,
474 .spi_write = snd_soc_16_8_spi_write,
475 },
Mark Brownbc6552f2010-03-05 16:27:15 +0000476 {
477 .addr_bits = 16, .data_bits = 16,
478 .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
479 .i2c_read = snd_soc_16_16_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100480 .spi_write = snd_soc_16_16_spi_write,
Mark Brownbc6552f2010-03-05 16:27:15 +0000481 },
Mark Brown17a52fd2009-07-05 17:24:50 +0100482};
483
484/**
485 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
486 *
487 * @codec: CODEC to configure.
Mark Brown17a52fd2009-07-05 17:24:50 +0100488 * @addr_bits: Number of bits of register address data.
489 * @data_bits: Number of bits of data per register.
Mark Brown7084a422009-07-10 22:24:27 +0100490 * @control: Control bus used.
Mark Brown17a52fd2009-07-05 17:24:50 +0100491 *
492 * Register formats are frequently shared between many I2C and SPI
493 * devices. In order to promote code reuse the ASoC core provides
494 * some standard implementations of CODEC read and write operations
495 * which can be set up using this function.
496 *
497 * The caller is responsible for allocating and initialising the
498 * actual cache.
499 *
500 * Note that at present this code cannot be used by CODECs with
501 * volatile registers.
502 */
503int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Mark Brown7084a422009-07-10 22:24:27 +0100504 int addr_bits, int data_bits,
505 enum snd_soc_control_type control)
Mark Brown17a52fd2009-07-05 17:24:50 +0100506{
507 int i;
508
Mark Brown17a52fd2009-07-05 17:24:50 +0100509 for (i = 0; i < ARRAY_SIZE(io_types); i++)
510 if (io_types[i].addr_bits == addr_bits &&
511 io_types[i].data_bits == data_bits)
512 break;
513 if (i == ARRAY_SIZE(io_types)) {
514 printk(KERN_ERR
515 "No I/O functions for %d bit address %d bit data\n",
516 addr_bits, data_bits);
517 return -EINVAL;
518 }
519
Mark Brownc3acec22010-12-02 16:15:29 +0000520 codec->write = io_types[i].write;
521 codec->read = io_types[i].read;
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000522 codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;
Mark Brown17a52fd2009-07-05 17:24:50 +0100523
Mark Brown7084a422009-07-10 22:24:27 +0100524 switch (control) {
525 case SND_SOC_CUSTOM:
526 break;
527
528 case SND_SOC_I2C:
Randy Dunlap17244c22009-08-10 16:04:39 -0700529#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brown7084a422009-07-10 22:24:27 +0100530 codec->hw_write = (hw_write_t)i2c_master_send;
531#endif
Mark Brownafa2f102009-07-10 23:11:24 +0100532 if (io_types[i].i2c_read)
533 codec->hw_read = io_types[i].i2c_read;
Mark Browna6d14342010-08-12 10:59:15 +0100534
535 codec->control_data = container_of(codec->dev,
536 struct i2c_client,
537 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100538 break;
539
540 case SND_SOC_SPI:
Mark Brown27ded042009-07-10 23:28:16 +0100541 if (io_types[i].spi_write)
542 codec->hw_write = io_types[i].spi_write;
Mark Browna6d14342010-08-12 10:59:15 +0100543
544 codec->control_data = container_of(codec->dev,
545 struct spi_device,
546 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100547 break;
548 }
549
Mark Brown17a52fd2009-07-05 17:24:50 +0100550 return 0;
551}
552EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000553
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000554static bool snd_soc_set_cache_val(void *base, unsigned int idx,
555 unsigned int val, unsigned int word_size)
556{
557 switch (word_size) {
558 case 1: {
559 u8 *cache = base;
560 if (cache[idx] == val)
561 return true;
562 cache[idx] = val;
563 break;
564 }
565 case 2: {
566 u16 *cache = base;
567 if (cache[idx] == val)
568 return true;
569 cache[idx] = val;
570 break;
571 }
572 default:
573 BUG();
574 }
575 return false;
576}
577
578static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
579 unsigned int word_size)
580{
581 switch (word_size) {
582 case 1: {
583 const u8 *cache = base;
584 return cache[idx];
585 }
586 case 2: {
587 const u16 *cache = base;
588 return cache[idx];
589 }
590 default:
591 BUG();
592 }
593 /* unreachable */
594 return -1;
595}
596
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000597struct snd_soc_rbtree_node {
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100598 struct rb_node node; /* the actual rbtree node holding this block */
599 unsigned int base_reg; /* base register handled by this block */
600 unsigned int word_size; /* number of bytes needed to represent the register index */
601 void *block; /* block of adjacent registers */
602 unsigned int blklen; /* number of registers available in the block */
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000603} __attribute__ ((packed));
604
605struct snd_soc_rbtree_ctx {
606 struct rb_root root;
607};
608
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100609static inline void snd_soc_rbtree_get_base_top_reg(
610 struct snd_soc_rbtree_node *rbnode,
611 unsigned int *base, unsigned int *top)
612{
613 *base = rbnode->base_reg;
614 *top = rbnode->base_reg + rbnode->blklen - 1;
615}
616
617static unsigned int snd_soc_rbtree_get_register(
618 struct snd_soc_rbtree_node *rbnode, unsigned int idx)
619{
620 unsigned int val;
621
622 switch (rbnode->word_size) {
623 case 1: {
624 u8 *p = rbnode->block;
625 val = p[idx];
626 return val;
627 }
628 case 2: {
629 u16 *p = rbnode->block;
630 val = p[idx];
631 return val;
632 }
633 default:
634 BUG();
635 break;
636 }
637 return -1;
638}
639
640static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node *rbnode,
641 unsigned int idx, unsigned int val)
642{
643 switch (rbnode->word_size) {
644 case 1: {
645 u8 *p = rbnode->block;
646 p[idx] = val;
647 break;
648 }
649 case 2: {
650 u16 *p = rbnode->block;
651 p[idx] = val;
652 break;
653 }
654 default:
655 BUG();
656 break;
657 }
658}
659
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000660static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
661 struct rb_root *root, unsigned int reg)
662{
663 struct rb_node *node;
664 struct snd_soc_rbtree_node *rbnode;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100665 unsigned int base_reg, top_reg;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000666
667 node = root->rb_node;
668 while (node) {
669 rbnode = container_of(node, struct snd_soc_rbtree_node, node);
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100670 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
671 if (reg >= base_reg && reg <= top_reg)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000672 return rbnode;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100673 else if (reg > top_reg)
674 node = node->rb_right;
675 else if (reg < base_reg)
676 node = node->rb_left;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000677 }
678
679 return NULL;
680}
681
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000682static int snd_soc_rbtree_insert(struct rb_root *root,
683 struct snd_soc_rbtree_node *rbnode)
684{
685 struct rb_node **new, *parent;
686 struct snd_soc_rbtree_node *rbnode_tmp;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100687 unsigned int base_reg_tmp, top_reg_tmp;
688 unsigned int base_reg;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000689
690 parent = NULL;
691 new = &root->rb_node;
692 while (*new) {
693 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
694 node);
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100695 /* base and top registers of the current rbnode */
696 snd_soc_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp,
697 &top_reg_tmp);
698 /* base register of the rbnode to be added */
699 base_reg = rbnode->base_reg;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000700 parent = *new;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100701 /* if this register has already been inserted, just return */
702 if (base_reg >= base_reg_tmp &&
703 base_reg <= top_reg_tmp)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000704 return 0;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100705 else if (base_reg > top_reg_tmp)
706 new = &((*new)->rb_right);
707 else if (base_reg < base_reg_tmp)
708 new = &((*new)->rb_left);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000709 }
710
711 /* insert the node into the rbtree */
712 rb_link_node(&rbnode->node, parent, new);
713 rb_insert_color(&rbnode->node, root);
714
715 return 1;
716}
717
718static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
719{
720 struct snd_soc_rbtree_ctx *rbtree_ctx;
721 struct rb_node *node;
722 struct snd_soc_rbtree_node *rbnode;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100723 unsigned int regtmp;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000724 unsigned int val;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000725 int ret;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100726 int i;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000727
728 rbtree_ctx = codec->reg_cache;
729 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
730 rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100731 for (i = 0; i < rbnode->blklen; ++i) {
732 regtmp = rbnode->base_reg + i;
733 WARN_ON(codec->writable_register &&
734 codec->writable_register(codec, regtmp));
735 val = snd_soc_rbtree_get_register(rbnode, i);
736 codec->cache_bypass = 1;
737 ret = snd_soc_write(codec, regtmp, val);
738 codec->cache_bypass = 0;
739 if (ret)
740 return ret;
741 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
742 regtmp, val);
743 }
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000744 }
745
746 return 0;
747}
748
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100749static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node *rbnode,
750 unsigned int pos, unsigned int reg,
751 unsigned int value)
752{
753 u8 *blk;
754
755 blk = krealloc(rbnode->block,
756 (rbnode->blklen + 1) * rbnode->word_size, GFP_KERNEL);
757 if (!blk)
758 return -ENOMEM;
759
760 /* insert the register value in the correct place in the rbnode block */
761 memmove(blk + (pos + 1) * rbnode->word_size,
762 blk + pos * rbnode->word_size,
763 (rbnode->blklen - pos) * rbnode->word_size);
764
765 /* update the rbnode block, its size and the base register */
766 rbnode->block = blk;
767 rbnode->blklen++;
768 if (!pos)
769 rbnode->base_reg = reg;
770
771 snd_soc_rbtree_set_register(rbnode, pos, value);
772 return 0;
773}
774
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000775static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
776 unsigned int reg, unsigned int value)
777{
778 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100779 struct snd_soc_rbtree_node *rbnode, *rbnode_tmp;
780 struct rb_node *node;
781 unsigned int val;
782 unsigned int reg_tmp;
783 unsigned int pos;
784 int i;
785 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000786
787 rbtree_ctx = codec->reg_cache;
788 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
789 if (rbnode) {
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100790 reg_tmp = reg - rbnode->base_reg;
791 val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
792 if (val == value)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000793 return 0;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100794 snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000795 } else {
796 /* bail out early, no need to create the rbnode yet */
797 if (!value)
798 return 0;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100799 /* look for an adjacent register to the one we are about to add */
800 for (node = rb_first(&rbtree_ctx->root); node;
801 node = rb_next(node)) {
802 rbnode_tmp = rb_entry(node, struct snd_soc_rbtree_node, node);
803 for (i = 0; i < rbnode_tmp->blklen; ++i) {
804 reg_tmp = rbnode_tmp->base_reg + i;
805 if (abs(reg_tmp - reg) != 1)
806 continue;
807 /* decide where in the block to place our register */
808 if (reg_tmp + 1 == reg)
809 pos = i + 1;
810 else
811 pos = i;
812 ret = snd_soc_rbtree_insert_to_block(rbnode_tmp, pos,
813 reg, value);
814 if (ret)
815 return ret;
816 return 0;
817 }
818 }
819 /* we did not manage to find a place to insert it in an existing
820 * block so create a new rbnode with a single register in its block.
821 * This block will get populated further if any other adjacent
822 * registers get modified in the future.
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000823 */
824 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
825 if (!rbnode)
826 return -ENOMEM;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100827 rbnode->blklen = 1;
828 rbnode->base_reg = reg;
829 rbnode->word_size = codec->driver->reg_word_size;
830 rbnode->block = kmalloc(rbnode->blklen * rbnode->word_size,
831 GFP_KERNEL);
832 if (!rbnode->block) {
833 kfree(rbnode);
834 return -ENOMEM;
835 }
836 snd_soc_rbtree_set_register(rbnode, 0, value);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000837 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
838 }
839
840 return 0;
841}
842
843static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
844 unsigned int reg, unsigned int *value)
845{
846 struct snd_soc_rbtree_ctx *rbtree_ctx;
847 struct snd_soc_rbtree_node *rbnode;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100848 unsigned int reg_tmp;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000849
850 rbtree_ctx = codec->reg_cache;
851 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
852 if (rbnode) {
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100853 reg_tmp = reg - rbnode->base_reg;
854 *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000855 } else {
856 /* uninitialized registers default to 0 */
857 *value = 0;
858 }
859
860 return 0;
861}
862
863static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
864{
865 struct rb_node *next;
866 struct snd_soc_rbtree_ctx *rbtree_ctx;
867 struct snd_soc_rbtree_node *rbtree_node;
868
869 /* if we've already been called then just return */
870 rbtree_ctx = codec->reg_cache;
871 if (!rbtree_ctx)
872 return 0;
873
874 /* free up the rbtree */
875 next = rb_first(&rbtree_ctx->root);
876 while (next) {
877 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
878 next = rb_next(&rbtree_node->node);
879 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100880 kfree(rbtree_node->block);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000881 kfree(rbtree_node);
882 }
883
884 /* release the resources */
885 kfree(codec->reg_cache);
886 codec->reg_cache = NULL;
887
888 return 0;
889}
890
891static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
892{
893 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000894 unsigned int word_size;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100895 unsigned int val;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000896 int i;
897 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000898
899 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
900 if (!codec->reg_cache)
901 return -ENOMEM;
902
903 rbtree_ctx = codec->reg_cache;
904 rbtree_ctx->root = RB_ROOT;
905
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +0000906 if (!codec->reg_def_copy)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000907 return 0;
908
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000909 word_size = codec->driver->reg_word_size;
910 for (i = 0; i < codec->driver->reg_cache_size; ++i) {
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100911 val = snd_soc_get_cache_val(codec->reg_def_copy, i,
912 word_size);
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000913 if (!val)
914 continue;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100915 ret = snd_soc_rbtree_cache_write(codec, i, val);
916 if (ret)
917 goto err;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000918 }
919
920 return 0;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100921
922err:
923 snd_soc_cache_exit(codec);
924 return ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000925}
926
Mark Brown68d44ee2010-12-21 17:19:56 +0000927#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000928struct snd_soc_lzo_ctx {
929 void *wmem;
930 void *dst;
931 const void *src;
932 size_t src_len;
933 size_t dst_len;
934 size_t decompressed_size;
935 unsigned long *sync_bmp;
936 int sync_bmp_nbits;
937};
938
939#define LZO_BLOCK_NUM 8
940static int snd_soc_lzo_block_count(void)
941{
942 return LZO_BLOCK_NUM;
943}
944
945static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
946{
947 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
948 if (!lzo_ctx->wmem)
949 return -ENOMEM;
950 return 0;
951}
952
953static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
954{
955 size_t compress_size;
956 int ret;
957
958 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
959 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
960 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
961 return -EINVAL;
962 lzo_ctx->dst_len = compress_size;
963 return 0;
964}
965
966static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
967{
968 size_t dst_len;
969 int ret;
970
971 dst_len = lzo_ctx->dst_len;
972 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
973 lzo_ctx->dst, &dst_len);
974 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
975 return -EINVAL;
976 return 0;
977}
978
979static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
980 struct snd_soc_lzo_ctx *lzo_ctx)
981{
982 int ret;
983
984 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
985 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
986 if (!lzo_ctx->dst) {
987 lzo_ctx->dst_len = 0;
988 return -ENOMEM;
989 }
990
991 ret = snd_soc_lzo_compress(lzo_ctx);
992 if (ret < 0)
993 return ret;
994 return 0;
995}
996
997static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
998 struct snd_soc_lzo_ctx *lzo_ctx)
999{
1000 int ret;
1001
1002 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
1003 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1004 if (!lzo_ctx->dst) {
1005 lzo_ctx->dst_len = 0;
1006 return -ENOMEM;
1007 }
1008
1009 ret = snd_soc_lzo_decompress(lzo_ctx);
1010 if (ret < 0)
1011 return ret;
1012 return 0;
1013}
1014
1015static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
1016 unsigned int reg)
1017{
Mark Brown001ae4c2010-12-02 16:21:08 +00001018 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001019
1020 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001021 return (reg * codec_drv->reg_word_size) /
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001022 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001023}
1024
1025static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
1026 unsigned int reg)
1027{
Mark Brown001ae4c2010-12-02 16:21:08 +00001028 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001029
1030 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001031 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001032 codec_drv->reg_word_size);
1033}
1034
1035static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
1036{
Mark Brown001ae4c2010-12-02 16:21:08 +00001037 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001038
1039 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001040 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001041}
1042
1043static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
1044{
1045 struct snd_soc_lzo_ctx **lzo_blocks;
1046 unsigned int val;
1047 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001048 int ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001049
1050 lzo_blocks = codec->reg_cache;
1051 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
Dimitris Papastamosf20eda52011-03-28 11:39:15 +01001052 WARN_ON(codec->writable_register &&
1053 codec->writable_register(codec, i));
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001054 ret = snd_soc_cache_read(codec, i, &val);
1055 if (ret)
1056 return ret;
Dimitris Papastamos99780072011-01-19 14:53:37 +00001057 codec->cache_bypass = 1;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001058 ret = snd_soc_write(codec, i, val);
Dimitris Papastamos99780072011-01-19 14:53:37 +00001059 codec->cache_bypass = 0;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001060 if (ret)
1061 return ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001062 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1063 i, val);
1064 }
1065
1066 return 0;
1067}
1068
1069static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
1070 unsigned int reg, unsigned int value)
1071{
1072 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1073 int ret, blkindex, blkpos;
1074 size_t blksize, tmp_dst_len;
1075 void *tmp_dst;
1076
1077 /* index of the compressed lzo block */
1078 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1079 /* register index within the decompressed block */
1080 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1081 /* size of the compressed block */
1082 blksize = snd_soc_lzo_get_blksize(codec);
1083 lzo_blocks = codec->reg_cache;
1084 lzo_block = lzo_blocks[blkindex];
1085
1086 /* save the pointer and length of the compressed block */
1087 tmp_dst = lzo_block->dst;
1088 tmp_dst_len = lzo_block->dst_len;
1089
1090 /* prepare the source to be the compressed block */
1091 lzo_block->src = lzo_block->dst;
1092 lzo_block->src_len = lzo_block->dst_len;
1093
1094 /* decompress the block */
1095 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1096 if (ret < 0) {
1097 kfree(lzo_block->dst);
1098 goto out;
1099 }
1100
1101 /* write the new value to the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001102 if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
1103 codec->driver->reg_word_size)) {
1104 kfree(lzo_block->dst);
1105 goto out;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001106 }
1107
1108 /* prepare the source to be the decompressed block */
1109 lzo_block->src = lzo_block->dst;
1110 lzo_block->src_len = lzo_block->dst_len;
1111
1112 /* compress the block */
1113 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1114 if (ret < 0) {
1115 kfree(lzo_block->dst);
1116 kfree(lzo_block->src);
1117 goto out;
1118 }
1119
1120 /* set the bit so we know we have to sync this register */
1121 set_bit(reg, lzo_block->sync_bmp);
1122 kfree(tmp_dst);
1123 kfree(lzo_block->src);
1124 return 0;
1125out:
1126 lzo_block->dst = tmp_dst;
1127 lzo_block->dst_len = tmp_dst_len;
1128 return ret;
1129}
1130
1131static int snd_soc_lzo_cache_read(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 *value = 0;
1140 /* index of the compressed lzo block */
1141 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1142 /* register index within the decompressed block */
1143 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1144 /* size of the compressed block */
1145 blksize = snd_soc_lzo_get_blksize(codec);
1146 lzo_blocks = codec->reg_cache;
1147 lzo_block = lzo_blocks[blkindex];
1148
1149 /* save the pointer and length of the compressed block */
1150 tmp_dst = lzo_block->dst;
1151 tmp_dst_len = lzo_block->dst_len;
1152
1153 /* prepare the source to be the compressed block */
1154 lzo_block->src = lzo_block->dst;
1155 lzo_block->src_len = lzo_block->dst_len;
1156
1157 /* decompress the block */
1158 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001159 if (ret >= 0)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001160 /* fetch the value from the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001161 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1162 codec->driver->reg_word_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001163
1164 kfree(lzo_block->dst);
1165 /* restore the pointer and length of the compressed block */
1166 lzo_block->dst = tmp_dst;
1167 lzo_block->dst_len = tmp_dst_len;
1168 return 0;
1169}
1170
1171static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1172{
1173 struct snd_soc_lzo_ctx **lzo_blocks;
1174 int i, blkcount;
1175
1176 lzo_blocks = codec->reg_cache;
1177 if (!lzo_blocks)
1178 return 0;
1179
1180 blkcount = snd_soc_lzo_block_count();
1181 /*
1182 * the pointer to the bitmap used for syncing the cache
1183 * is shared amongst all lzo_blocks. Ensure it is freed
1184 * only once.
1185 */
1186 if (lzo_blocks[0])
1187 kfree(lzo_blocks[0]->sync_bmp);
1188 for (i = 0; i < blkcount; ++i) {
1189 if (lzo_blocks[i]) {
1190 kfree(lzo_blocks[i]->wmem);
1191 kfree(lzo_blocks[i]->dst);
1192 }
1193 /* each lzo_block is a pointer returned by kmalloc or NULL */
1194 kfree(lzo_blocks[i]);
1195 }
1196 kfree(lzo_blocks);
1197 codec->reg_cache = NULL;
1198 return 0;
1199}
1200
1201static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1202{
1203 struct snd_soc_lzo_ctx **lzo_blocks;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001204 size_t bmp_size;
Mark Brown001ae4c2010-12-02 16:21:08 +00001205 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001206 int ret, tofree, i, blksize, blkcount;
1207 const char *p, *end;
1208 unsigned long *sync_bmp;
1209
1210 ret = 0;
1211 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001212
1213 /*
1214 * If we have not been given a default register cache
1215 * then allocate a dummy zero-ed out region, compress it
1216 * and remember to free it afterwards.
1217 */
1218 tofree = 0;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001219 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001220 tofree = 1;
1221
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001222 if (!codec->reg_def_copy) {
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001223 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001224 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001225 return -ENOMEM;
1226 }
1227
1228 blkcount = snd_soc_lzo_block_count();
1229 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1230 GFP_KERNEL);
1231 if (!codec->reg_cache) {
1232 ret = -ENOMEM;
1233 goto err_tofree;
1234 }
1235 lzo_blocks = codec->reg_cache;
1236
1237 /*
1238 * allocate a bitmap to be used when syncing the cache with
1239 * the hardware. Each time a register is modified, the corresponding
1240 * bit is set in the bitmap, so we know that we have to sync
1241 * that register.
1242 */
1243 bmp_size = codec_drv->reg_cache_size;
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +00001244 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001245 GFP_KERNEL);
1246 if (!sync_bmp) {
1247 ret = -ENOMEM;
1248 goto err;
1249 }
Dimitris Papastamos09c74a92010-11-29 11:43:33 +00001250 bitmap_zero(sync_bmp, bmp_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001251
1252 /* allocate the lzo blocks and initialize them */
1253 for (i = 0; i < blkcount; ++i) {
1254 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1255 GFP_KERNEL);
1256 if (!lzo_blocks[i]) {
1257 kfree(sync_bmp);
1258 ret = -ENOMEM;
1259 goto err;
1260 }
1261 lzo_blocks[i]->sync_bmp = sync_bmp;
Dimitris Papastamos04f8fd12011-01-11 11:24:02 +00001262 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001263 /* alloc the working space for the compressed block */
1264 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1265 if (ret < 0)
1266 goto err;
1267 }
1268
1269 blksize = snd_soc_lzo_get_blksize(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001270 p = codec->reg_def_copy;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001271 end = codec->reg_def_copy + codec->reg_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001272 /* compress the register map and fill the lzo blocks */
1273 for (i = 0; i < blkcount; ++i, p += blksize) {
1274 lzo_blocks[i]->src = p;
1275 if (p + blksize > end)
1276 lzo_blocks[i]->src_len = end - p;
1277 else
1278 lzo_blocks[i]->src_len = blksize;
1279 ret = snd_soc_lzo_compress_cache_block(codec,
1280 lzo_blocks[i]);
1281 if (ret < 0)
1282 goto err;
1283 lzo_blocks[i]->decompressed_size =
1284 lzo_blocks[i]->src_len;
1285 }
1286
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001287 if (tofree) {
1288 kfree(codec->reg_def_copy);
1289 codec->reg_def_copy = NULL;
1290 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001291 return 0;
1292err:
1293 snd_soc_cache_exit(codec);
1294err_tofree:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001295 if (tofree) {
1296 kfree(codec->reg_def_copy);
1297 codec->reg_def_copy = NULL;
1298 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001299 return ret;
1300}
Mark Brown68d44ee2010-12-21 17:19:56 +00001301#endif
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001302
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001303static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1304{
1305 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001306 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +00001307 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001308 unsigned int val;
1309
1310 codec_drv = codec->driver;
1311 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamosf20eda52011-03-28 11:39:15 +01001312 WARN_ON(codec->writable_register &&
1313 codec->writable_register(codec, i));
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001314 ret = snd_soc_cache_read(codec, i, &val);
1315 if (ret)
1316 return ret;
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001317 if (codec->reg_def_copy)
1318 if (snd_soc_get_cache_val(codec->reg_def_copy,
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001319 i, codec_drv->reg_word_size) == val)
1320 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001321 ret = snd_soc_write(codec, i, val);
1322 if (ret)
1323 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001324 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1325 i, val);
1326 }
1327 return 0;
1328}
1329
1330static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1331 unsigned int reg, unsigned int value)
1332{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001333 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1334 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001335 return 0;
1336}
1337
1338static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1339 unsigned int reg, unsigned int *value)
1340{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001341 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1342 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001343 return 0;
1344}
1345
1346static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1347{
1348 if (!codec->reg_cache)
1349 return 0;
1350 kfree(codec->reg_cache);
1351 codec->reg_cache = NULL;
1352 return 0;
1353}
1354
1355static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1356{
Mark Brown001ae4c2010-12-02 16:21:08 +00001357 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001358
1359 codec_drv = codec->driver;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001360
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001361 if (codec->reg_def_copy)
1362 codec->reg_cache = kmemdup(codec->reg_def_copy,
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001363 codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001364 else
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001365 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001366 if (!codec->reg_cache)
1367 return -ENOMEM;
1368
1369 return 0;
1370}
1371
1372/* an array of all supported compression types */
1373static const struct snd_soc_cache_ops cache_types[] = {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001374 /* Flat *must* be the first entry for fallback */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001375 {
Dimitris Papastamosdf0701b2010-11-29 10:54:28 +00001376 .id = SND_SOC_FLAT_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001377 .name = "flat",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001378 .init = snd_soc_flat_cache_init,
1379 .exit = snd_soc_flat_cache_exit,
1380 .read = snd_soc_flat_cache_read,
1381 .write = snd_soc_flat_cache_write,
1382 .sync = snd_soc_flat_cache_sync
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001383 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001384#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001385 {
1386 .id = SND_SOC_LZO_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001387 .name = "LZO",
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001388 .init = snd_soc_lzo_cache_init,
1389 .exit = snd_soc_lzo_cache_exit,
1390 .read = snd_soc_lzo_cache_read,
1391 .write = snd_soc_lzo_cache_write,
1392 .sync = snd_soc_lzo_cache_sync
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001393 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001394#endif
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001395 {
1396 .id = SND_SOC_RBTREE_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001397 .name = "rbtree",
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001398 .init = snd_soc_rbtree_cache_init,
1399 .exit = snd_soc_rbtree_cache_exit,
1400 .read = snd_soc_rbtree_cache_read,
1401 .write = snd_soc_rbtree_cache_write,
1402 .sync = snd_soc_rbtree_cache_sync
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001403 }
1404};
1405
1406int snd_soc_cache_init(struct snd_soc_codec *codec)
1407{
1408 int i;
1409
1410 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00001411 if (cache_types[i].id == codec->compress_type)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001412 break;
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001413
1414 /* Fall back to flat compression */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001415 if (i == ARRAY_SIZE(cache_types)) {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001416 dev_warn(codec->dev, "Could not match compress type: %d\n",
1417 codec->compress_type);
1418 i = 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001419 }
1420
1421 mutex_init(&codec->cache_rw_mutex);
1422 codec->cache_ops = &cache_types[i];
1423
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001424 if (codec->cache_ops->init) {
1425 if (codec->cache_ops->name)
1426 dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1427 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001428 return codec->cache_ops->init(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001429 }
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001430 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001431}
1432
1433/*
1434 * NOTE: keep in mind that this function might be called
1435 * multiple times.
1436 */
1437int snd_soc_cache_exit(struct snd_soc_codec *codec)
1438{
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001439 if (codec->cache_ops && codec->cache_ops->exit) {
1440 if (codec->cache_ops->name)
1441 dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1442 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001443 return codec->cache_ops->exit(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001444 }
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001445 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001446}
1447
1448/**
1449 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1450 *
1451 * @codec: CODEC to configure.
1452 * @reg: The register index.
1453 * @value: The value to be returned.
1454 */
1455int snd_soc_cache_read(struct snd_soc_codec *codec,
1456 unsigned int reg, unsigned int *value)
1457{
1458 int ret;
1459
1460 mutex_lock(&codec->cache_rw_mutex);
1461
1462 if (value && codec->cache_ops && codec->cache_ops->read) {
1463 ret = codec->cache_ops->read(codec, reg, value);
1464 mutex_unlock(&codec->cache_rw_mutex);
1465 return ret;
1466 }
1467
1468 mutex_unlock(&codec->cache_rw_mutex);
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001469 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001470}
1471EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1472
1473/**
1474 * snd_soc_cache_write: Set the value of a given register in the cache.
1475 *
1476 * @codec: CODEC to configure.
1477 * @reg: The register index.
1478 * @value: The new register value.
1479 */
1480int snd_soc_cache_write(struct snd_soc_codec *codec,
1481 unsigned int reg, unsigned int value)
1482{
1483 int ret;
1484
1485 mutex_lock(&codec->cache_rw_mutex);
1486
1487 if (codec->cache_ops && codec->cache_ops->write) {
1488 ret = codec->cache_ops->write(codec, reg, value);
1489 mutex_unlock(&codec->cache_rw_mutex);
1490 return ret;
1491 }
1492
1493 mutex_unlock(&codec->cache_rw_mutex);
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001494 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001495}
1496EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1497
1498/**
1499 * snd_soc_cache_sync: Sync the register cache with the hardware.
1500 *
1501 * @codec: CODEC to configure.
1502 *
1503 * Any registers that should not be synced should be marked as
1504 * volatile. In general drivers can choose not to use the provided
1505 * syncing functionality if they so require.
1506 */
1507int snd_soc_cache_sync(struct snd_soc_codec *codec)
1508{
1509 int ret;
Dimitris Papastamosc358e642011-01-21 15:29:02 +00001510 const char *name;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001511
1512 if (!codec->cache_sync) {
1513 return 0;
1514 }
1515
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001516 if (!codec->cache_ops || !codec->cache_ops->sync)
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001517 return -ENOSYS;
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001518
Dimitris Papastamosc358e642011-01-21 15:29:02 +00001519 if (codec->cache_ops->name)
1520 name = codec->cache_ops->name;
1521 else
1522 name = "unknown";
1523
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001524 if (codec->cache_ops->name)
1525 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1526 codec->cache_ops->name, codec->name);
1527 trace_snd_soc_cache_sync(codec, name, "start");
1528 ret = codec->cache_ops->sync(codec);
1529 if (!ret)
1530 codec->cache_sync = 0;
1531 trace_snd_soc_cache_sync(codec, name, "end");
1532 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001533}
1534EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
Dimitris Papastamos066d16c2011-01-13 12:20:36 +00001535
1536static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1537 unsigned int reg)
1538{
1539 const struct snd_soc_codec_driver *codec_drv;
1540 unsigned int min, max, index;
1541
1542 codec_drv = codec->driver;
1543 min = 0;
1544 max = codec_drv->reg_access_size - 1;
1545 do {
1546 index = (min + max) / 2;
1547 if (codec_drv->reg_access_default[index].reg == reg)
1548 return index;
1549 if (codec_drv->reg_access_default[index].reg < reg)
1550 min = index + 1;
1551 else
1552 max = index;
1553 } while (min <= max);
1554 return -1;
1555}
1556
1557int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1558 unsigned int reg)
1559{
1560 int index;
1561
1562 if (reg >= codec->driver->reg_cache_size)
1563 return 1;
1564 index = snd_soc_get_reg_access_index(codec, reg);
1565 if (index < 0)
1566 return 0;
1567 return codec->driver->reg_access_default[index].vol;
1568}
1569EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1570
1571int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1572 unsigned int reg)
1573{
1574 int index;
1575
1576 if (reg >= codec->driver->reg_cache_size)
1577 return 1;
1578 index = snd_soc_get_reg_access_index(codec, reg);
1579 if (index < 0)
1580 return 0;
1581 return codec->driver->reg_access_default[index].read;
1582}
1583EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
Dimitris Papastamos80204542011-03-24 13:45:17 +00001584
1585int snd_soc_default_writable_register(struct snd_soc_codec *codec,
1586 unsigned int reg)
1587{
1588 int index;
1589
1590 if (reg >= codec->driver->reg_cache_size)
1591 return 1;
1592 index = snd_soc_get_reg_access_index(codec, reg);
1593 if (index < 0)
1594 return 0;
1595 return codec->driver->reg_access_default[index].write;
1596}
1597EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);