blob: cad70c9640dc70e39aaa9c5f2d02a2224ba705fe [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
Mark Brownf7391fc2011-05-11 19:25:42 +020023#ifdef CONFIG_SPI_MASTER
24static int do_spi_write(void *control, const char *data, int len)
Dimitris Papastamos30539a12011-03-22 10:37:00 +000025{
Mark Brownf7391fc2011-05-11 19:25:42 +020026 struct spi_device *spi = control;
27 int ret;
Dimitris Papastamos30539a12011-03-22 10:37:00 +000028
Mark Brownf7391fc2011-05-11 19:25:42 +020029 ret = spi_write(spi, data, len);
30 if (ret < 0)
31 return ret;
Dimitris Papastamos30539a12011-03-22 10:37:00 +000032
33 return len;
34}
35#endif
36
Dimitris Papastamos26e99842011-03-22 10:36:58 +000037static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg,
38 unsigned int value, const void *data, int len)
39{
40 int ret;
41
42 if (!snd_soc_codec_volatile_register(codec, reg) &&
43 reg < codec->driver->reg_cache_size &&
44 !codec->cache_bypass) {
45 ret = snd_soc_cache_write(codec, reg, value);
46 if (ret < 0)
47 return -1;
48 }
49
50 if (codec->cache_only) {
51 codec->cache_sync = 1;
52 return 0;
53 }
54
55 ret = codec->hw_write(codec->control_data, data, len);
56 if (ret == len)
57 return 0;
58 if (ret < 0)
59 return ret;
60 else
61 return -EIO;
62}
63
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000064static unsigned int do_hw_read(struct snd_soc_codec *codec, unsigned int reg)
Barry Song63b62ab2010-01-27 11:46:17 +080065{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000066 int ret;
67 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010068
69 if (reg >= codec->driver->reg_cache_size ||
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000070 snd_soc_codec_volatile_register(codec, reg) ||
71 codec->cache_bypass) {
72 if (codec->cache_only)
73 return -1;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010074
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000075 BUG_ON(!codec->hw_read);
76 return codec->hw_read(codec, reg);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010077 }
78
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000079 ret = snd_soc_cache_read(codec, reg, &val);
80 if (ret < 0)
81 return -1;
82 return val;
Barry Song63b62ab2010-01-27 11:46:17 +080083}
84
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000085static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +010086 unsigned int reg)
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +000087{
88 return do_hw_read(codec, reg);
89}
90
Barry Song63b62ab2010-01-27 11:46:17 +080091static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +010092 unsigned int value)
Barry Song63b62ab2010-01-27 11:46:17 +080093{
Mark Brown063b7cc2011-05-10 23:55:21 +020094 u16 data;
Barry Song63b62ab2010-01-27 11:46:17 +080095
Mark Brown063b7cc2011-05-10 23:55:21 +020096 data = cpu_to_be16((reg << 12) | (value & 0xffffff));
Barry Song63b62ab2010-01-27 11:46:17 +080097
Mark Brown063b7cc2011-05-10 23:55:21 +020098 return do_hw_write(codec, reg, value, &data, 2);
Barry Song63b62ab2010-01-27 11:46:17 +080099}
100
Mark Brown17a52fd2009-07-05 17:24:50 +0100101static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
102 unsigned int reg)
103{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000104 return do_hw_read(codec, reg);
Mark Brown17a52fd2009-07-05 17:24:50 +0100105}
106
107static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
108 unsigned int value)
109{
Mark Brownf06f1362011-05-11 00:56:13 +0200110 u16 data;
Mark Brown17a52fd2009-07-05 17:24:50 +0100111
Mark Brownf06f1362011-05-11 00:56:13 +0200112 data = cpu_to_be16((reg << 9) | (value & 0x1ff));
Mark Brown17a52fd2009-07-05 17:24:50 +0100113
Mark Brownf06f1362011-05-11 00:56:13 +0200114 return do_hw_write(codec, reg, value, &data, 2);
Mark Brown17a52fd2009-07-05 17:24:50 +0100115}
116
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900117static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
118 unsigned int value)
119{
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900120 u8 data[2];
121
Barry Songf4bee1b2010-03-18 16:17:01 +0800122 reg &= 0xff;
123 data[0] = reg;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900124 data[1] = value & 0xff;
125
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000126 return do_hw_write(codec, reg, value, data, 2);
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900127}
128
129static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
130 unsigned int reg)
131{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000132 return do_hw_read(codec, reg);
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900133}
134
Mark Brownafa2f102009-07-10 23:11:24 +0100135static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
136 unsigned int value)
137{
Mark Brownafa2f102009-07-10 23:11:24 +0100138 u8 data[3];
Mark Brown94228bcf2011-05-11 11:18:00 +0200139 u16 val = cpu_to_be16(value);
Mark Brownafa2f102009-07-10 23:11:24 +0100140
141 data[0] = reg;
Mark Brown94228bcf2011-05-11 11:18:00 +0200142 memcpy(&data[1], &val, sizeof(val));
Mark Brownafa2f102009-07-10 23:11:24 +0100143
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000144 return do_hw_write(codec, reg, value, data, 3);
Mark Brownafa2f102009-07-10 23:11:24 +0100145}
146
147static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
148 unsigned int reg)
149{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000150 return do_hw_read(codec, reg);
Mark Brownafa2f102009-07-10 23:11:24 +0100151}
152
Randy Dunlap17244c22009-08-10 16:04:39 -0700153#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000154static unsigned int do_i2c_read(struct snd_soc_codec *codec,
155 void *reg, int reglen,
156 void *data, int datalen)
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800157{
158 struct i2c_msg xfer[2];
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800159 int ret;
160 struct i2c_client *client = codec->control_data;
161
162 /* Write register */
163 xfer[0].addr = client->addr;
164 xfer[0].flags = 0;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000165 xfer[0].len = reglen;
166 xfer[0].buf = reg;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800167
168 /* Read data */
169 xfer[1].addr = client->addr;
170 xfer[1].flags = I2C_M_RD;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000171 xfer[1].len = datalen;
172 xfer[1].buf = data;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800173
174 ret = i2c_transfer(client->adapter, xfer, 2);
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000175 if (ret == 2)
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800176 return 0;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000177 else if (ret < 0)
178 return ret;
179 else
180 return -EIO;
181}
182#endif
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800183
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000184#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
185static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100186 unsigned int r)
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000187{
188 u8 reg = r;
189 u8 data;
190 int ret;
191
192 ret = do_i2c_read(codec, &reg, 1, &data, 1);
193 if (ret < 0)
194 return 0;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800195 return data;
196}
197#else
198#define snd_soc_8_8_read_i2c NULL
199#endif
200
201#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brownafa2f102009-07-10 23:11:24 +0100202static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
203 unsigned int r)
204{
Mark Brownafa2f102009-07-10 23:11:24 +0100205 u8 reg = r;
206 u16 data;
207 int ret;
Mark Brownafa2f102009-07-10 23:11:24 +0100208
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000209 ret = do_i2c_read(codec, &reg, 1, &data, 2);
210 if (ret < 0)
Mark Brownafa2f102009-07-10 23:11:24 +0100211 return 0;
Mark Brownafa2f102009-07-10 23:11:24 +0100212 return (data >> 8) | ((data & 0xff) << 8);
213}
214#else
215#define snd_soc_8_16_read_i2c NULL
216#endif
Mark Brown17a52fd2009-07-05 17:24:50 +0100217
Barry Song994dc422010-01-27 11:46:18 +0800218#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
219static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
220 unsigned int r)
221{
Barry Song994dc422010-01-27 11:46:18 +0800222 u16 reg = r;
223 u8 data;
224 int ret;
Barry Song994dc422010-01-27 11:46:18 +0800225
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000226 ret = do_i2c_read(codec, &reg, 2, &data, 1);
227 if (ret < 0)
Barry Song994dc422010-01-27 11:46:18 +0800228 return 0;
Barry Song994dc422010-01-27 11:46:18 +0800229 return data;
230}
231#else
232#define snd_soc_16_8_read_i2c NULL
233#endif
234
235static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100236 unsigned int reg)
Barry Song994dc422010-01-27 11:46:18 +0800237{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000238 return do_hw_read(codec, reg);
Barry Song994dc422010-01-27 11:46:18 +0800239}
240
241static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100242 unsigned int value)
Barry Song994dc422010-01-27 11:46:18 +0800243{
Barry Song994dc422010-01-27 11:46:18 +0800244 u8 data[3];
Mark Brown2ac8b6f2011-05-11 15:15:56 +0200245 u16 rval = cpu_to_be16(reg);
Barry Song994dc422010-01-27 11:46:18 +0800246
Mark Brown2ac8b6f2011-05-11 15:15:56 +0200247 memcpy(data, &rval, sizeof(rval));
Barry Song994dc422010-01-27 11:46:18 +0800248 data[2] = value;
Mark Brown8c961bc2010-02-01 18:46:10 +0000249
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000250 return do_hw_write(codec, reg, value, data, 3);
Barry Song994dc422010-01-27 11:46:18 +0800251}
252
Mark Brownbc6552f2010-03-05 16:27:15 +0000253#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
254static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
255 unsigned int r)
256{
Mark Brownbc6552f2010-03-05 16:27:15 +0000257 u16 reg = cpu_to_be16(r);
258 u16 data;
259 int ret;
Mark Brownbc6552f2010-03-05 16:27:15 +0000260
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000261 ret = do_i2c_read(codec, &reg, 2, &data, 2);
262 if (ret < 0)
Mark Brownbc6552f2010-03-05 16:27:15 +0000263 return 0;
Mark Brownbc6552f2010-03-05 16:27:15 +0000264 return be16_to_cpu(data);
265}
266#else
267#define snd_soc_16_16_read_i2c NULL
268#endif
269
270static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
271 unsigned int reg)
272{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000273 return do_hw_read(codec, reg);
Mark Brownbc6552f2010-03-05 16:27:15 +0000274}
275
276static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
277 unsigned int value)
278{
Mark Brown60c655e2011-05-11 01:02:35 +0200279 u16 data[2];
Mark Brownbc6552f2010-03-05 16:27:15 +0000280
Mark Brown60c655e2011-05-11 01:02:35 +0200281 data[0] = cpu_to_be16(reg);
282 data[1] = cpu_to_be16(value);
Mark Brownbc6552f2010-03-05 16:27:15 +0000283
Mark Brown60c655e2011-05-11 01:02:35 +0200284 return do_hw_write(codec, reg, value, data, sizeof(data));
Mark Brownbc6552f2010-03-05 16:27:15 +0000285}
Barry Song994dc422010-01-27 11:46:18 +0800286
Mark Brown34bad692011-04-04 17:55:42 +0900287/* Primitive bulk write support for soc-cache. The data pointed to by
288 * `data' needs to already be in the form the hardware expects
289 * including any leading register specific data. Any data written
290 * through this function will not go through the cache as it only
291 * handles writing to volatile or out of bounds registers.
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000292 */
293static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg,
294 const void *data, size_t len)
295{
296 int ret;
297
Dimitris Papastamos64d27062011-05-05 14:18:11 +0100298 /* To ensure that we don't get out of sync with the cache, check
299 * whether the base register is volatile or if we've directly asked
300 * to bypass the cache. Out of bounds registers are considered
301 * volatile.
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000302 */
Dimitris Papastamos64d27062011-05-05 14:18:11 +0100303 if (!codec->cache_bypass
304 && !snd_soc_codec_volatile_register(codec, reg)
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000305 && reg < codec->driver->reg_cache_size)
306 return -EINVAL;
307
308 switch (codec->control_type) {
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900309#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000310 case SND_SOC_I2C:
311 ret = i2c_master_send(codec->control_data, data, len);
312 break;
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900313#endif
314#if defined(CONFIG_SPI_MASTER)
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000315 case SND_SOC_SPI:
Mark Brown6e28f972011-05-10 23:33:48 +0200316 ret = spi_write(codec->control_data, data, len);
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000317 break;
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900318#endif
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000319 default:
320 BUG();
321 }
322
323 if (ret == len)
324 return 0;
325 if (ret < 0)
326 return ret;
327 else
328 return -EIO;
329}
330
Mark Brown17a52fd2009-07-05 17:24:50 +0100331static struct {
332 int addr_bits;
333 int data_bits;
Mark Brownafa2f102009-07-10 23:11:24 +0100334 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100335 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
Mark Brownafa2f102009-07-10 23:11:24 +0100336 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100337} io_types[] = {
Mark Brownd62ab352009-09-21 04:21:47 -0700338 {
Barry Song63b62ab2010-01-27 11:46:17 +0800339 .addr_bits = 4, .data_bits = 12,
340 .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
Barry Song63b62ab2010-01-27 11:46:17 +0800341 },
342 {
Mark Brownd62ab352009-09-21 04:21:47 -0700343 .addr_bits = 7, .data_bits = 9,
344 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
Mark Brownd62ab352009-09-21 04:21:47 -0700345 },
346 {
347 .addr_bits = 8, .data_bits = 8,
348 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800349 .i2c_read = snd_soc_8_8_read_i2c,
Mark Brownd62ab352009-09-21 04:21:47 -0700350 },
351 {
352 .addr_bits = 8, .data_bits = 16,
353 .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
354 .i2c_read = snd_soc_8_16_read_i2c,
355 },
Barry Song994dc422010-01-27 11:46:18 +0800356 {
357 .addr_bits = 16, .data_bits = 8,
358 .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
359 .i2c_read = snd_soc_16_8_read_i2c,
Barry Song994dc422010-01-27 11:46:18 +0800360 },
Mark Brownbc6552f2010-03-05 16:27:15 +0000361 {
362 .addr_bits = 16, .data_bits = 16,
363 .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
364 .i2c_read = snd_soc_16_16_read_i2c,
365 },
Mark Brown17a52fd2009-07-05 17:24:50 +0100366};
367
368/**
369 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
370 *
371 * @codec: CODEC to configure.
Mark Brown17a52fd2009-07-05 17:24:50 +0100372 * @addr_bits: Number of bits of register address data.
373 * @data_bits: Number of bits of data per register.
Mark Brown7084a422009-07-10 22:24:27 +0100374 * @control: Control bus used.
Mark Brown17a52fd2009-07-05 17:24:50 +0100375 *
376 * Register formats are frequently shared between many I2C and SPI
377 * devices. In order to promote code reuse the ASoC core provides
378 * some standard implementations of CODEC read and write operations
379 * which can be set up using this function.
380 *
381 * The caller is responsible for allocating and initialising the
382 * actual cache.
383 *
384 * Note that at present this code cannot be used by CODECs with
385 * volatile registers.
386 */
387int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Mark Brown7084a422009-07-10 22:24:27 +0100388 int addr_bits, int data_bits,
389 enum snd_soc_control_type control)
Mark Brown17a52fd2009-07-05 17:24:50 +0100390{
391 int i;
392
Mark Brown17a52fd2009-07-05 17:24:50 +0100393 for (i = 0; i < ARRAY_SIZE(io_types); i++)
394 if (io_types[i].addr_bits == addr_bits &&
395 io_types[i].data_bits == data_bits)
396 break;
397 if (i == ARRAY_SIZE(io_types)) {
398 printk(KERN_ERR
399 "No I/O functions for %d bit address %d bit data\n",
400 addr_bits, data_bits);
401 return -EINVAL;
402 }
403
Mark Brownc3acec22010-12-02 16:15:29 +0000404 codec->write = io_types[i].write;
405 codec->read = io_types[i].read;
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000406 codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;
Mark Brown17a52fd2009-07-05 17:24:50 +0100407
Mark Brown7084a422009-07-10 22:24:27 +0100408 switch (control) {
409 case SND_SOC_CUSTOM:
410 break;
411
412 case SND_SOC_I2C:
Randy Dunlap17244c22009-08-10 16:04:39 -0700413#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brown7084a422009-07-10 22:24:27 +0100414 codec->hw_write = (hw_write_t)i2c_master_send;
415#endif
Mark Brownafa2f102009-07-10 23:11:24 +0100416 if (io_types[i].i2c_read)
417 codec->hw_read = io_types[i].i2c_read;
Mark Browna6d14342010-08-12 10:59:15 +0100418
419 codec->control_data = container_of(codec->dev,
420 struct i2c_client,
421 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100422 break;
423
424 case SND_SOC_SPI:
Mark Brown6e28f972011-05-10 23:33:48 +0200425#ifdef CONFIG_SPI_MASTER
Mark Brownf7391fc2011-05-11 19:25:42 +0200426 codec->hw_write = do_spi_write;
Mark Brown6e28f972011-05-10 23:33:48 +0200427#endif
Mark Browna6d14342010-08-12 10:59:15 +0100428
429 codec->control_data = container_of(codec->dev,
430 struct spi_device,
431 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100432 break;
433 }
434
Mark Brown17a52fd2009-07-05 17:24:50 +0100435 return 0;
436}
437EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000438
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000439static bool snd_soc_set_cache_val(void *base, unsigned int idx,
440 unsigned int val, unsigned int word_size)
441{
442 switch (word_size) {
443 case 1: {
444 u8 *cache = base;
445 if (cache[idx] == val)
446 return true;
447 cache[idx] = val;
448 break;
449 }
450 case 2: {
451 u16 *cache = base;
452 if (cache[idx] == val)
453 return true;
454 cache[idx] = val;
455 break;
456 }
457 default:
458 BUG();
459 }
460 return false;
461}
462
463static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
464 unsigned int word_size)
465{
Mark Brownfd137e22011-06-06 11:26:15 +0100466 if (!base)
467 return -1;
468
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000469 switch (word_size) {
470 case 1: {
471 const u8 *cache = base;
472 return cache[idx];
473 }
474 case 2: {
475 const u16 *cache = base;
476 return cache[idx];
477 }
478 default:
479 BUG();
480 }
481 /* unreachable */
482 return -1;
483}
484
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000485struct snd_soc_rbtree_node {
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100486 struct rb_node node; /* the actual rbtree node holding this block */
487 unsigned int base_reg; /* base register handled by this block */
488 unsigned int word_size; /* number of bytes needed to represent the register index */
489 void *block; /* block of adjacent registers */
490 unsigned int blklen; /* number of registers available in the block */
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000491} __attribute__ ((packed));
492
493struct snd_soc_rbtree_ctx {
494 struct rb_root root;
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100495 struct snd_soc_rbtree_node *cached_rbnode;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000496};
497
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100498static inline void snd_soc_rbtree_get_base_top_reg(
499 struct snd_soc_rbtree_node *rbnode,
500 unsigned int *base, unsigned int *top)
501{
502 *base = rbnode->base_reg;
503 *top = rbnode->base_reg + rbnode->blklen - 1;
504}
505
506static unsigned int snd_soc_rbtree_get_register(
507 struct snd_soc_rbtree_node *rbnode, unsigned int idx)
508{
509 unsigned int val;
510
511 switch (rbnode->word_size) {
512 case 1: {
513 u8 *p = rbnode->block;
514 val = p[idx];
515 return val;
516 }
517 case 2: {
518 u16 *p = rbnode->block;
519 val = p[idx];
520 return val;
521 }
522 default:
523 BUG();
524 break;
525 }
526 return -1;
527}
528
529static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node *rbnode,
530 unsigned int idx, unsigned int val)
531{
532 switch (rbnode->word_size) {
533 case 1: {
534 u8 *p = rbnode->block;
535 p[idx] = val;
536 break;
537 }
538 case 2: {
539 u16 *p = rbnode->block;
540 p[idx] = val;
541 break;
542 }
543 default:
544 BUG();
545 break;
546 }
547}
548
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000549static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
550 struct rb_root *root, unsigned int reg)
551{
552 struct rb_node *node;
553 struct snd_soc_rbtree_node *rbnode;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100554 unsigned int base_reg, top_reg;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000555
556 node = root->rb_node;
557 while (node) {
558 rbnode = container_of(node, struct snd_soc_rbtree_node, node);
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100559 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
560 if (reg >= base_reg && reg <= top_reg)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000561 return rbnode;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100562 else if (reg > top_reg)
563 node = node->rb_right;
564 else if (reg < base_reg)
565 node = node->rb_left;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000566 }
567
568 return NULL;
569}
570
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000571static int snd_soc_rbtree_insert(struct rb_root *root,
572 struct snd_soc_rbtree_node *rbnode)
573{
574 struct rb_node **new, *parent;
575 struct snd_soc_rbtree_node *rbnode_tmp;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100576 unsigned int base_reg_tmp, top_reg_tmp;
577 unsigned int base_reg;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000578
579 parent = NULL;
580 new = &root->rb_node;
581 while (*new) {
582 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
583 node);
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100584 /* base and top registers of the current rbnode */
585 snd_soc_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp,
586 &top_reg_tmp);
587 /* base register of the rbnode to be added */
588 base_reg = rbnode->base_reg;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000589 parent = *new;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100590 /* if this register has already been inserted, just return */
591 if (base_reg >= base_reg_tmp &&
592 base_reg <= top_reg_tmp)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000593 return 0;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100594 else if (base_reg > top_reg_tmp)
595 new = &((*new)->rb_right);
596 else if (base_reg < base_reg_tmp)
597 new = &((*new)->rb_left);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000598 }
599
600 /* insert the node into the rbtree */
601 rb_link_node(&rbnode->node, parent, new);
602 rb_insert_color(&rbnode->node, root);
603
604 return 1;
605}
606
607static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
608{
609 struct snd_soc_rbtree_ctx *rbtree_ctx;
610 struct rb_node *node;
611 struct snd_soc_rbtree_node *rbnode;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100612 unsigned int regtmp;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000613 unsigned int val;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000614 int ret;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100615 int i;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000616
617 rbtree_ctx = codec->reg_cache;
618 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
619 rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100620 for (i = 0; i < rbnode->blklen; ++i) {
621 regtmp = rbnode->base_reg + i;
622 WARN_ON(codec->writable_register &&
623 codec->writable_register(codec, regtmp));
624 val = snd_soc_rbtree_get_register(rbnode, i);
625 codec->cache_bypass = 1;
626 ret = snd_soc_write(codec, regtmp, val);
627 codec->cache_bypass = 0;
628 if (ret)
629 return ret;
630 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
631 regtmp, val);
632 }
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000633 }
634
635 return 0;
636}
637
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100638static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node *rbnode,
639 unsigned int pos, unsigned int reg,
640 unsigned int value)
641{
642 u8 *blk;
643
644 blk = krealloc(rbnode->block,
645 (rbnode->blklen + 1) * rbnode->word_size, GFP_KERNEL);
646 if (!blk)
647 return -ENOMEM;
648
649 /* insert the register value in the correct place in the rbnode block */
650 memmove(blk + (pos + 1) * rbnode->word_size,
651 blk + pos * rbnode->word_size,
652 (rbnode->blklen - pos) * rbnode->word_size);
653
654 /* update the rbnode block, its size and the base register */
655 rbnode->block = blk;
656 rbnode->blklen++;
657 if (!pos)
658 rbnode->base_reg = reg;
659
660 snd_soc_rbtree_set_register(rbnode, pos, value);
661 return 0;
662}
663
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000664static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
665 unsigned int reg, unsigned int value)
666{
667 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100668 struct snd_soc_rbtree_node *rbnode, *rbnode_tmp;
669 struct rb_node *node;
670 unsigned int val;
671 unsigned int reg_tmp;
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100672 unsigned int base_reg, top_reg;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100673 unsigned int pos;
674 int i;
675 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000676
677 rbtree_ctx = codec->reg_cache;
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100678 /* look up the required register in the cached rbnode */
679 rbnode = rbtree_ctx->cached_rbnode;
680 if (rbnode) {
681 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
682 if (reg >= base_reg && reg <= top_reg) {
683 reg_tmp = reg - base_reg;
684 val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
685 if (val == value)
686 return 0;
687 snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
688 return 0;
689 }
690 }
691 /* if we can't locate it in the cached rbnode we'll have
692 * to traverse the rbtree looking for it.
693 */
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000694 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
695 if (rbnode) {
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100696 reg_tmp = reg - rbnode->base_reg;
697 val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
698 if (val == value)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000699 return 0;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100700 snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100701 rbtree_ctx->cached_rbnode = rbnode;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000702 } else {
703 /* bail out early, no need to create the rbnode yet */
704 if (!value)
705 return 0;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100706 /* look for an adjacent register to the one we are about to add */
707 for (node = rb_first(&rbtree_ctx->root); node;
708 node = rb_next(node)) {
709 rbnode_tmp = rb_entry(node, struct snd_soc_rbtree_node, node);
710 for (i = 0; i < rbnode_tmp->blklen; ++i) {
711 reg_tmp = rbnode_tmp->base_reg + i;
712 if (abs(reg_tmp - reg) != 1)
713 continue;
714 /* decide where in the block to place our register */
715 if (reg_tmp + 1 == reg)
716 pos = i + 1;
717 else
718 pos = i;
719 ret = snd_soc_rbtree_insert_to_block(rbnode_tmp, pos,
720 reg, value);
721 if (ret)
722 return ret;
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100723 rbtree_ctx->cached_rbnode = rbnode_tmp;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100724 return 0;
725 }
726 }
727 /* we did not manage to find a place to insert it in an existing
728 * block so create a new rbnode with a single register in its block.
729 * This block will get populated further if any other adjacent
730 * registers get modified in the future.
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000731 */
732 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
733 if (!rbnode)
734 return -ENOMEM;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100735 rbnode->blklen = 1;
736 rbnode->base_reg = reg;
737 rbnode->word_size = codec->driver->reg_word_size;
738 rbnode->block = kmalloc(rbnode->blklen * rbnode->word_size,
739 GFP_KERNEL);
740 if (!rbnode->block) {
741 kfree(rbnode);
742 return -ENOMEM;
743 }
744 snd_soc_rbtree_set_register(rbnode, 0, value);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000745 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100746 rbtree_ctx->cached_rbnode = rbnode;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000747 }
748
749 return 0;
750}
751
752static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
753 unsigned int reg, unsigned int *value)
754{
755 struct snd_soc_rbtree_ctx *rbtree_ctx;
756 struct snd_soc_rbtree_node *rbnode;
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100757 unsigned int base_reg, top_reg;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100758 unsigned int reg_tmp;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000759
760 rbtree_ctx = codec->reg_cache;
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100761 /* look up the required register in the cached rbnode */
762 rbnode = rbtree_ctx->cached_rbnode;
763 if (rbnode) {
764 snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
765 if (reg >= base_reg && reg <= top_reg) {
766 reg_tmp = reg - base_reg;
767 *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
768 return 0;
769 }
770 }
771 /* if we can't locate it in the cached rbnode we'll have
772 * to traverse the rbtree looking for it.
773 */
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000774 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
775 if (rbnode) {
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100776 reg_tmp = reg - rbnode->base_reg;
777 *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100778 rbtree_ctx->cached_rbnode = rbnode;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000779 } else {
780 /* uninitialized registers default to 0 */
781 *value = 0;
782 }
783
784 return 0;
785}
786
787static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
788{
789 struct rb_node *next;
790 struct snd_soc_rbtree_ctx *rbtree_ctx;
791 struct snd_soc_rbtree_node *rbtree_node;
792
793 /* if we've already been called then just return */
794 rbtree_ctx = codec->reg_cache;
795 if (!rbtree_ctx)
796 return 0;
797
798 /* free up the rbtree */
799 next = rb_first(&rbtree_ctx->root);
800 while (next) {
801 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
802 next = rb_next(&rbtree_node->node);
803 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100804 kfree(rbtree_node->block);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000805 kfree(rbtree_node);
806 }
807
808 /* release the resources */
809 kfree(codec->reg_cache);
810 codec->reg_cache = NULL;
811
812 return 0;
813}
814
815static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
816{
817 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000818 unsigned int word_size;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100819 unsigned int val;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000820 int i;
821 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000822
823 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
824 if (!codec->reg_cache)
825 return -ENOMEM;
826
827 rbtree_ctx = codec->reg_cache;
828 rbtree_ctx->root = RB_ROOT;
Dimitris Papastamos7e146b52011-05-19 13:45:30 +0100829 rbtree_ctx->cached_rbnode = NULL;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000830
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +0000831 if (!codec->reg_def_copy)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000832 return 0;
833
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000834 word_size = codec->driver->reg_word_size;
835 for (i = 0; i < codec->driver->reg_cache_size; ++i) {
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100836 val = snd_soc_get_cache_val(codec->reg_def_copy, i,
837 word_size);
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000838 if (!val)
839 continue;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100840 ret = snd_soc_rbtree_cache_write(codec, i, val);
841 if (ret)
842 goto err;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000843 }
844
845 return 0;
Dimitris Papastamos0944cc32011-05-19 13:45:29 +0100846
847err:
848 snd_soc_cache_exit(codec);
849 return ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000850}
851
Mark Brown68d44ee2010-12-21 17:19:56 +0000852#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000853struct snd_soc_lzo_ctx {
854 void *wmem;
855 void *dst;
856 const void *src;
857 size_t src_len;
858 size_t dst_len;
859 size_t decompressed_size;
860 unsigned long *sync_bmp;
861 int sync_bmp_nbits;
862};
863
864#define LZO_BLOCK_NUM 8
865static int snd_soc_lzo_block_count(void)
866{
867 return LZO_BLOCK_NUM;
868}
869
870static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
871{
872 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
873 if (!lzo_ctx->wmem)
874 return -ENOMEM;
875 return 0;
876}
877
878static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
879{
880 size_t compress_size;
881 int ret;
882
883 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
884 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
885 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
886 return -EINVAL;
887 lzo_ctx->dst_len = compress_size;
888 return 0;
889}
890
891static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
892{
893 size_t dst_len;
894 int ret;
895
896 dst_len = lzo_ctx->dst_len;
897 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
898 lzo_ctx->dst, &dst_len);
899 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
900 return -EINVAL;
901 return 0;
902}
903
904static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
905 struct snd_soc_lzo_ctx *lzo_ctx)
906{
907 int ret;
908
909 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
910 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
911 if (!lzo_ctx->dst) {
912 lzo_ctx->dst_len = 0;
913 return -ENOMEM;
914 }
915
916 ret = snd_soc_lzo_compress(lzo_ctx);
917 if (ret < 0)
918 return ret;
919 return 0;
920}
921
922static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
923 struct snd_soc_lzo_ctx *lzo_ctx)
924{
925 int ret;
926
927 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
928 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
929 if (!lzo_ctx->dst) {
930 lzo_ctx->dst_len = 0;
931 return -ENOMEM;
932 }
933
934 ret = snd_soc_lzo_decompress(lzo_ctx);
935 if (ret < 0)
936 return ret;
937 return 0;
938}
939
940static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
941 unsigned int reg)
942{
Mark Brown001ae4c2010-12-02 16:21:08 +0000943 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000944
945 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000946 return (reg * codec_drv->reg_word_size) /
Dimitris Papastamosaea170a2011-01-12 10:38:58 +0000947 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000948}
949
950static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
951 unsigned int reg)
952{
Mark Brown001ae4c2010-12-02 16:21:08 +0000953 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000954
955 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +0000956 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000957 codec_drv->reg_word_size);
958}
959
960static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
961{
Mark Brown001ae4c2010-12-02 16:21:08 +0000962 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000963
964 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +0000965 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000966}
967
968static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
969{
970 struct snd_soc_lzo_ctx **lzo_blocks;
971 unsigned int val;
972 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000973 int ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000974
975 lzo_blocks = codec->reg_cache;
976 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
Dimitris Papastamosf20eda52011-03-28 11:39:15 +0100977 WARN_ON(codec->writable_register &&
978 codec->writable_register(codec, i));
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000979 ret = snd_soc_cache_read(codec, i, &val);
980 if (ret)
981 return ret;
Dimitris Papastamos99780072011-01-19 14:53:37 +0000982 codec->cache_bypass = 1;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000983 ret = snd_soc_write(codec, i, val);
Dimitris Papastamos99780072011-01-19 14:53:37 +0000984 codec->cache_bypass = 0;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000985 if (ret)
986 return ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000987 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
988 i, val);
989 }
990
991 return 0;
992}
993
994static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
995 unsigned int reg, unsigned int value)
996{
997 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
998 int ret, blkindex, blkpos;
999 size_t blksize, tmp_dst_len;
1000 void *tmp_dst;
1001
1002 /* index of the compressed lzo block */
1003 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1004 /* register index within the decompressed block */
1005 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1006 /* size of the compressed block */
1007 blksize = snd_soc_lzo_get_blksize(codec);
1008 lzo_blocks = codec->reg_cache;
1009 lzo_block = lzo_blocks[blkindex];
1010
1011 /* save the pointer and length of the compressed block */
1012 tmp_dst = lzo_block->dst;
1013 tmp_dst_len = lzo_block->dst_len;
1014
1015 /* prepare the source to be the compressed block */
1016 lzo_block->src = lzo_block->dst;
1017 lzo_block->src_len = lzo_block->dst_len;
1018
1019 /* decompress the block */
1020 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1021 if (ret < 0) {
1022 kfree(lzo_block->dst);
1023 goto out;
1024 }
1025
1026 /* write the new value to the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001027 if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
1028 codec->driver->reg_word_size)) {
1029 kfree(lzo_block->dst);
1030 goto out;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001031 }
1032
1033 /* prepare the source to be the decompressed block */
1034 lzo_block->src = lzo_block->dst;
1035 lzo_block->src_len = lzo_block->dst_len;
1036
1037 /* compress the block */
1038 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1039 if (ret < 0) {
1040 kfree(lzo_block->dst);
1041 kfree(lzo_block->src);
1042 goto out;
1043 }
1044
1045 /* set the bit so we know we have to sync this register */
1046 set_bit(reg, lzo_block->sync_bmp);
1047 kfree(tmp_dst);
1048 kfree(lzo_block->src);
1049 return 0;
1050out:
1051 lzo_block->dst = tmp_dst;
1052 lzo_block->dst_len = tmp_dst_len;
1053 return ret;
1054}
1055
1056static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1057 unsigned int reg, unsigned int *value)
1058{
1059 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1060 int ret, blkindex, blkpos;
1061 size_t blksize, tmp_dst_len;
1062 void *tmp_dst;
1063
1064 *value = 0;
1065 /* index of the compressed lzo block */
1066 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1067 /* register index within the decompressed block */
1068 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1069 /* size of the compressed block */
1070 blksize = snd_soc_lzo_get_blksize(codec);
1071 lzo_blocks = codec->reg_cache;
1072 lzo_block = lzo_blocks[blkindex];
1073
1074 /* save the pointer and length of the compressed block */
1075 tmp_dst = lzo_block->dst;
1076 tmp_dst_len = lzo_block->dst_len;
1077
1078 /* prepare the source to be the compressed block */
1079 lzo_block->src = lzo_block->dst;
1080 lzo_block->src_len = lzo_block->dst_len;
1081
1082 /* decompress the block */
1083 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001084 if (ret >= 0)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001085 /* fetch the value from the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001086 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1087 codec->driver->reg_word_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001088
1089 kfree(lzo_block->dst);
1090 /* restore the pointer and length of the compressed block */
1091 lzo_block->dst = tmp_dst;
1092 lzo_block->dst_len = tmp_dst_len;
1093 return 0;
1094}
1095
1096static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1097{
1098 struct snd_soc_lzo_ctx **lzo_blocks;
1099 int i, blkcount;
1100
1101 lzo_blocks = codec->reg_cache;
1102 if (!lzo_blocks)
1103 return 0;
1104
1105 blkcount = snd_soc_lzo_block_count();
1106 /*
1107 * the pointer to the bitmap used for syncing the cache
1108 * is shared amongst all lzo_blocks. Ensure it is freed
1109 * only once.
1110 */
1111 if (lzo_blocks[0])
1112 kfree(lzo_blocks[0]->sync_bmp);
1113 for (i = 0; i < blkcount; ++i) {
1114 if (lzo_blocks[i]) {
1115 kfree(lzo_blocks[i]->wmem);
1116 kfree(lzo_blocks[i]->dst);
1117 }
1118 /* each lzo_block is a pointer returned by kmalloc or NULL */
1119 kfree(lzo_blocks[i]);
1120 }
1121 kfree(lzo_blocks);
1122 codec->reg_cache = NULL;
1123 return 0;
1124}
1125
1126static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1127{
1128 struct snd_soc_lzo_ctx **lzo_blocks;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001129 size_t bmp_size;
Mark Brown001ae4c2010-12-02 16:21:08 +00001130 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001131 int ret, tofree, i, blksize, blkcount;
1132 const char *p, *end;
1133 unsigned long *sync_bmp;
1134
1135 ret = 0;
1136 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001137
1138 /*
1139 * If we have not been given a default register cache
1140 * then allocate a dummy zero-ed out region, compress it
1141 * and remember to free it afterwards.
1142 */
1143 tofree = 0;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001144 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001145 tofree = 1;
1146
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001147 if (!codec->reg_def_copy) {
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001148 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001149 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001150 return -ENOMEM;
1151 }
1152
1153 blkcount = snd_soc_lzo_block_count();
1154 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1155 GFP_KERNEL);
1156 if (!codec->reg_cache) {
1157 ret = -ENOMEM;
1158 goto err_tofree;
1159 }
1160 lzo_blocks = codec->reg_cache;
1161
1162 /*
1163 * allocate a bitmap to be used when syncing the cache with
1164 * the hardware. Each time a register is modified, the corresponding
1165 * bit is set in the bitmap, so we know that we have to sync
1166 * that register.
1167 */
1168 bmp_size = codec_drv->reg_cache_size;
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +00001169 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001170 GFP_KERNEL);
1171 if (!sync_bmp) {
1172 ret = -ENOMEM;
1173 goto err;
1174 }
Dimitris Papastamos09c74a92010-11-29 11:43:33 +00001175 bitmap_zero(sync_bmp, bmp_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001176
1177 /* allocate the lzo blocks and initialize them */
1178 for (i = 0; i < blkcount; ++i) {
1179 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1180 GFP_KERNEL);
1181 if (!lzo_blocks[i]) {
1182 kfree(sync_bmp);
1183 ret = -ENOMEM;
1184 goto err;
1185 }
1186 lzo_blocks[i]->sync_bmp = sync_bmp;
Dimitris Papastamos04f8fd12011-01-11 11:24:02 +00001187 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001188 /* alloc the working space for the compressed block */
1189 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1190 if (ret < 0)
1191 goto err;
1192 }
1193
1194 blksize = snd_soc_lzo_get_blksize(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001195 p = codec->reg_def_copy;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001196 end = codec->reg_def_copy + codec->reg_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001197 /* compress the register map and fill the lzo blocks */
1198 for (i = 0; i < blkcount; ++i, p += blksize) {
1199 lzo_blocks[i]->src = p;
1200 if (p + blksize > end)
1201 lzo_blocks[i]->src_len = end - p;
1202 else
1203 lzo_blocks[i]->src_len = blksize;
1204 ret = snd_soc_lzo_compress_cache_block(codec,
1205 lzo_blocks[i]);
1206 if (ret < 0)
1207 goto err;
1208 lzo_blocks[i]->decompressed_size =
1209 lzo_blocks[i]->src_len;
1210 }
1211
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001212 if (tofree) {
1213 kfree(codec->reg_def_copy);
1214 codec->reg_def_copy = NULL;
1215 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001216 return 0;
1217err:
1218 snd_soc_cache_exit(codec);
1219err_tofree:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001220 if (tofree) {
1221 kfree(codec->reg_def_copy);
1222 codec->reg_def_copy = NULL;
1223 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001224 return ret;
1225}
Mark Brown68d44ee2010-12-21 17:19:56 +00001226#endif
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001227
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001228static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1229{
1230 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001231 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +00001232 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001233 unsigned int val;
1234
1235 codec_drv = codec->driver;
1236 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamosf20eda52011-03-28 11:39:15 +01001237 WARN_ON(codec->writable_register &&
1238 codec->writable_register(codec, i));
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001239 ret = snd_soc_cache_read(codec, i, &val);
1240 if (ret)
1241 return ret;
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001242 if (codec->reg_def_copy)
1243 if (snd_soc_get_cache_val(codec->reg_def_copy,
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001244 i, codec_drv->reg_word_size) == val)
1245 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001246 ret = snd_soc_write(codec, i, val);
1247 if (ret)
1248 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001249 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1250 i, val);
1251 }
1252 return 0;
1253}
1254
1255static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1256 unsigned int reg, unsigned int value)
1257{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001258 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1259 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001260 return 0;
1261}
1262
1263static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1264 unsigned int reg, unsigned int *value)
1265{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001266 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1267 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001268 return 0;
1269}
1270
1271static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1272{
1273 if (!codec->reg_cache)
1274 return 0;
1275 kfree(codec->reg_cache);
1276 codec->reg_cache = NULL;
1277 return 0;
1278}
1279
1280static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1281{
Mark Brown001ae4c2010-12-02 16:21:08 +00001282 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001283
1284 codec_drv = codec->driver;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001285
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001286 if (codec->reg_def_copy)
1287 codec->reg_cache = kmemdup(codec->reg_def_copy,
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001288 codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001289 else
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001290 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001291 if (!codec->reg_cache)
1292 return -ENOMEM;
1293
1294 return 0;
1295}
1296
1297/* an array of all supported compression types */
1298static const struct snd_soc_cache_ops cache_types[] = {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001299 /* Flat *must* be the first entry for fallback */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001300 {
Dimitris Papastamosdf0701b2010-11-29 10:54:28 +00001301 .id = SND_SOC_FLAT_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001302 .name = "flat",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001303 .init = snd_soc_flat_cache_init,
1304 .exit = snd_soc_flat_cache_exit,
1305 .read = snd_soc_flat_cache_read,
1306 .write = snd_soc_flat_cache_write,
1307 .sync = snd_soc_flat_cache_sync
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001308 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001309#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001310 {
1311 .id = SND_SOC_LZO_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001312 .name = "LZO",
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001313 .init = snd_soc_lzo_cache_init,
1314 .exit = snd_soc_lzo_cache_exit,
1315 .read = snd_soc_lzo_cache_read,
1316 .write = snd_soc_lzo_cache_write,
1317 .sync = snd_soc_lzo_cache_sync
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001318 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001319#endif
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001320 {
1321 .id = SND_SOC_RBTREE_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001322 .name = "rbtree",
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001323 .init = snd_soc_rbtree_cache_init,
1324 .exit = snd_soc_rbtree_cache_exit,
1325 .read = snd_soc_rbtree_cache_read,
1326 .write = snd_soc_rbtree_cache_write,
1327 .sync = snd_soc_rbtree_cache_sync
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001328 }
1329};
1330
1331int snd_soc_cache_init(struct snd_soc_codec *codec)
1332{
1333 int i;
1334
1335 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00001336 if (cache_types[i].id == codec->compress_type)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001337 break;
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001338
1339 /* Fall back to flat compression */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001340 if (i == ARRAY_SIZE(cache_types)) {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001341 dev_warn(codec->dev, "Could not match compress type: %d\n",
1342 codec->compress_type);
1343 i = 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001344 }
1345
1346 mutex_init(&codec->cache_rw_mutex);
1347 codec->cache_ops = &cache_types[i];
1348
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001349 if (codec->cache_ops->init) {
1350 if (codec->cache_ops->name)
1351 dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1352 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001353 return codec->cache_ops->init(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001354 }
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001355 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001356}
1357
1358/*
1359 * NOTE: keep in mind that this function might be called
1360 * multiple times.
1361 */
1362int snd_soc_cache_exit(struct snd_soc_codec *codec)
1363{
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001364 if (codec->cache_ops && codec->cache_ops->exit) {
1365 if (codec->cache_ops->name)
1366 dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1367 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001368 return codec->cache_ops->exit(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001369 }
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001370 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001371}
1372
1373/**
1374 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1375 *
1376 * @codec: CODEC to configure.
1377 * @reg: The register index.
1378 * @value: The value to be returned.
1379 */
1380int snd_soc_cache_read(struct snd_soc_codec *codec,
1381 unsigned int reg, unsigned int *value)
1382{
1383 int ret;
1384
1385 mutex_lock(&codec->cache_rw_mutex);
1386
1387 if (value && codec->cache_ops && codec->cache_ops->read) {
1388 ret = codec->cache_ops->read(codec, reg, value);
1389 mutex_unlock(&codec->cache_rw_mutex);
1390 return ret;
1391 }
1392
1393 mutex_unlock(&codec->cache_rw_mutex);
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001394 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001395}
1396EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1397
1398/**
1399 * snd_soc_cache_write: Set the value of a given register in the cache.
1400 *
1401 * @codec: CODEC to configure.
1402 * @reg: The register index.
1403 * @value: The new register value.
1404 */
1405int snd_soc_cache_write(struct snd_soc_codec *codec,
1406 unsigned int reg, unsigned int value)
1407{
1408 int ret;
1409
1410 mutex_lock(&codec->cache_rw_mutex);
1411
1412 if (codec->cache_ops && codec->cache_ops->write) {
1413 ret = codec->cache_ops->write(codec, reg, value);
1414 mutex_unlock(&codec->cache_rw_mutex);
1415 return ret;
1416 }
1417
1418 mutex_unlock(&codec->cache_rw_mutex);
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001419 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001420}
1421EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1422
1423/**
1424 * snd_soc_cache_sync: Sync the register cache with the hardware.
1425 *
1426 * @codec: CODEC to configure.
1427 *
1428 * Any registers that should not be synced should be marked as
1429 * volatile. In general drivers can choose not to use the provided
1430 * syncing functionality if they so require.
1431 */
1432int snd_soc_cache_sync(struct snd_soc_codec *codec)
1433{
1434 int ret;
Dimitris Papastamosc358e642011-01-21 15:29:02 +00001435 const char *name;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001436
1437 if (!codec->cache_sync) {
1438 return 0;
1439 }
1440
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001441 if (!codec->cache_ops || !codec->cache_ops->sync)
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001442 return -ENOSYS;
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001443
Dimitris Papastamosc358e642011-01-21 15:29:02 +00001444 if (codec->cache_ops->name)
1445 name = codec->cache_ops->name;
1446 else
1447 name = "unknown";
1448
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001449 if (codec->cache_ops->name)
1450 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1451 codec->cache_ops->name, codec->name);
1452 trace_snd_soc_cache_sync(codec, name, "start");
1453 ret = codec->cache_ops->sync(codec);
1454 if (!ret)
1455 codec->cache_sync = 0;
1456 trace_snd_soc_cache_sync(codec, name, "end");
1457 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001458}
1459EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
Dimitris Papastamos066d16c2011-01-13 12:20:36 +00001460
1461static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1462 unsigned int reg)
1463{
1464 const struct snd_soc_codec_driver *codec_drv;
1465 unsigned int min, max, index;
1466
1467 codec_drv = codec->driver;
1468 min = 0;
1469 max = codec_drv->reg_access_size - 1;
1470 do {
1471 index = (min + max) / 2;
1472 if (codec_drv->reg_access_default[index].reg == reg)
1473 return index;
1474 if (codec_drv->reg_access_default[index].reg < reg)
1475 min = index + 1;
1476 else
1477 max = index;
1478 } while (min <= max);
1479 return -1;
1480}
1481
1482int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1483 unsigned int reg)
1484{
1485 int index;
1486
1487 if (reg >= codec->driver->reg_cache_size)
1488 return 1;
1489 index = snd_soc_get_reg_access_index(codec, reg);
1490 if (index < 0)
1491 return 0;
1492 return codec->driver->reg_access_default[index].vol;
1493}
1494EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1495
1496int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1497 unsigned int reg)
1498{
1499 int index;
1500
1501 if (reg >= codec->driver->reg_cache_size)
1502 return 1;
1503 index = snd_soc_get_reg_access_index(codec, reg);
1504 if (index < 0)
1505 return 0;
1506 return codec->driver->reg_access_default[index].read;
1507}
1508EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
Dimitris Papastamos80204542011-03-24 13:45:17 +00001509
1510int snd_soc_default_writable_register(struct snd_soc_codec *codec,
1511 unsigned int reg)
1512{
1513 int index;
1514
1515 if (reg >= codec->driver->reg_cache_size)
1516 return 1;
1517 index = snd_soc_get_reg_access_index(codec, reg);
1518 if (index < 0)
1519 return 0;
1520 return codec->driver->reg_access_default[index].write;
1521}
1522EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);