blob: 8418b1fa1976e0efd2727aa6cca0156b2266ffe5 [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 Papastamos30539a182011-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 Papastamos30539a182011-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 Papastamos30539a182011-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 Papastamos30539a182011-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 Papastamos30539a182011-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 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
249 if (ret == 2)
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800250 return 0;
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000251 else if (ret < 0)
252 return ret;
253 else
254 return -EIO;
255}
256#endif
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800257
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000258#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
259static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100260 unsigned int r)
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000261{
262 u8 reg = r;
263 u8 data;
264 int ret;
265
266 ret = do_i2c_read(codec, &reg, 1, &data, 1);
267 if (ret < 0)
268 return 0;
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800269 return data;
270}
271#else
272#define snd_soc_8_8_read_i2c NULL
273#endif
274
275#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brownafa2f102009-07-10 23:11:24 +0100276static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
277 unsigned int r)
278{
Mark Brownafa2f102009-07-10 23:11:24 +0100279 u8 reg = r;
280 u16 data;
281 int ret;
Mark Brownafa2f102009-07-10 23:11:24 +0100282
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000283 ret = do_i2c_read(codec, &reg, 1, &data, 2);
284 if (ret < 0)
Mark Brownafa2f102009-07-10 23:11:24 +0100285 return 0;
Mark Brownafa2f102009-07-10 23:11:24 +0100286 return (data >> 8) | ((data & 0xff) << 8);
287}
288#else
289#define snd_soc_8_16_read_i2c NULL
290#endif
Mark Brown17a52fd2009-07-05 17:24:50 +0100291
Barry Song994dc422010-01-27 11:46:18 +0800292#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
293static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
294 unsigned int r)
295{
Barry Song994dc422010-01-27 11:46:18 +0800296 u16 reg = r;
297 u8 data;
298 int ret;
Barry Song994dc422010-01-27 11:46:18 +0800299
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000300 ret = do_i2c_read(codec, &reg, 2, &data, 1);
301 if (ret < 0)
Barry Song994dc422010-01-27 11:46:18 +0800302 return 0;
Barry Song994dc422010-01-27 11:46:18 +0800303 return data;
304}
305#else
306#define snd_soc_16_8_read_i2c NULL
307#endif
308
309static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100310 unsigned int reg)
Barry Song994dc422010-01-27 11:46:18 +0800311{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000312 return do_hw_read(codec, reg);
Barry Song994dc422010-01-27 11:46:18 +0800313}
314
315static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100316 unsigned int value)
Barry Song994dc422010-01-27 11:46:18 +0800317{
Barry Song994dc422010-01-27 11:46:18 +0800318 u8 data[3];
Barry Song994dc422010-01-27 11:46:18 +0800319
Barry Song994dc422010-01-27 11:46:18 +0800320 data[0] = (reg >> 8) & 0xff;
321 data[1] = reg & 0xff;
322 data[2] = value;
Barry Song994dc422010-01-27 11:46:18 +0800323 reg &= 0xff;
Mark Brown8c961bc2010-02-01 18:46:10 +0000324
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000325 return do_hw_write(codec, reg, value, data, 3);
Barry Song994dc422010-01-27 11:46:18 +0800326}
327
328#if defined(CONFIG_SPI_MASTER)
329static int snd_soc_16_8_spi_write(void *control_data, const char *data,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100330 int len)
Barry Song994dc422010-01-27 11:46:18 +0800331{
Barry Song994dc422010-01-27 11:46:18 +0800332 u8 msg[3];
333
Barry Song994dc422010-01-27 11:46:18 +0800334 msg[0] = data[0];
335 msg[1] = data[1];
336 msg[2] = data[2];
337
Dimitris Papastamos30539a182011-03-22 10:37:00 +0000338 return do_spi_write(control_data, msg, len);
Barry Song994dc422010-01-27 11:46:18 +0800339}
340#else
341#define snd_soc_16_8_spi_write NULL
342#endif
343
Mark Brownbc6552f2010-03-05 16:27:15 +0000344#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
345static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
346 unsigned int r)
347{
Mark Brownbc6552f2010-03-05 16:27:15 +0000348 u16 reg = cpu_to_be16(r);
349 u16 data;
350 int ret;
Mark Brownbc6552f2010-03-05 16:27:15 +0000351
Dimitris Papastamosf3594f52011-03-22 10:37:01 +0000352 ret = do_i2c_read(codec, &reg, 2, &data, 2);
353 if (ret < 0)
Mark Brownbc6552f2010-03-05 16:27:15 +0000354 return 0;
Mark Brownbc6552f2010-03-05 16:27:15 +0000355 return be16_to_cpu(data);
356}
357#else
358#define snd_soc_16_16_read_i2c NULL
359#endif
360
361static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
362 unsigned int reg)
363{
Dimitris Papastamosb8cbc192011-03-22 10:36:59 +0000364 return do_hw_read(codec, reg);
Mark Brownbc6552f2010-03-05 16:27:15 +0000365}
366
367static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
368 unsigned int value)
369{
Mark Brownbc6552f2010-03-05 16:27:15 +0000370 u8 data[4];
Mark Brownbc6552f2010-03-05 16:27:15 +0000371
372 data[0] = (reg >> 8) & 0xff;
373 data[1] = reg & 0xff;
374 data[2] = (value >> 8) & 0xff;
375 data[3] = value & 0xff;
376
Dimitris Papastamos26e99842011-03-22 10:36:58 +0000377 return do_hw_write(codec, reg, value, data, 4);
Mark Brownbc6552f2010-03-05 16:27:15 +0000378}
Barry Song994dc422010-01-27 11:46:18 +0800379
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100380#if defined(CONFIG_SPI_MASTER)
381static int snd_soc_16_16_spi_write(void *control_data, const char *data,
Dimitris Papastamosfbda1822011-03-28 11:39:14 +0100382 int len)
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100383{
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100384 u8 msg[4];
385
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100386 msg[0] = data[0];
387 msg[1] = data[1];
388 msg[2] = data[2];
389 msg[3] = data[3];
390
Dimitris Papastamos30539a182011-03-22 10:37:00 +0000391 return do_spi_write(control_data, msg, len);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100392}
393#else
394#define snd_soc_16_16_spi_write NULL
395#endif
396
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000397/* Primitive bulk write support for soc-cache. The data pointed to by `data' needs
398 * to already be in the form the hardware expects including any leading register specific
399 * data. Any data written through this function will not go through the cache as it
400 * only handles writing to volatile or out of bounds registers.
401 */
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
407 /* Ensure that the base register is volatile. Subsequently
408 * any other register that is touched by this routine should be
409 * volatile as well to ensure that we don't get out of sync with
410 * the cache.
411 */
412 if (!snd_soc_codec_volatile_register(codec, reg)
413 && reg < codec->driver->reg_cache_size)
414 return -EINVAL;
415
416 switch (codec->control_type) {
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900417#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000418 case SND_SOC_I2C:
419 ret = i2c_master_send(codec->control_data, data, len);
420 break;
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900421#endif
422#if defined(CONFIG_SPI_MASTER)
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000423 case SND_SOC_SPI:
424 ret = do_spi_write(codec->control_data, data, len);
425 break;
Seungwhan Youn898f8b02011-04-04 13:43:42 +0900426#endif
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +0000427 default:
428 BUG();
429 }
430
431 if (ret == len)
432 return 0;
433 if (ret < 0)
434 return ret;
435 else
436 return -EIO;
437}
438
Mark Brown17a52fd2009-07-05 17:24:50 +0100439static struct {
440 int addr_bits;
441 int data_bits;
Mark Brownafa2f102009-07-10 23:11:24 +0100442 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
Mark Brown27ded042009-07-10 23:28:16 +0100443 int (*spi_write)(void *, const char *, int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100444 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
Mark Brownafa2f102009-07-10 23:11:24 +0100445 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100446} io_types[] = {
Mark Brownd62ab352009-09-21 04:21:47 -0700447 {
Barry Song63b62ab2010-01-27 11:46:17 +0800448 .addr_bits = 4, .data_bits = 12,
449 .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
450 .spi_write = snd_soc_4_12_spi_write,
451 },
452 {
Mark Brownd62ab352009-09-21 04:21:47 -0700453 .addr_bits = 7, .data_bits = 9,
454 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
Barry Song8998c892009-12-31 10:30:34 +0800455 .spi_write = snd_soc_7_9_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700456 },
457 {
458 .addr_bits = 8, .data_bits = 8,
459 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800460 .i2c_read = snd_soc_8_8_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100461 .spi_write = snd_soc_8_8_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700462 },
463 {
464 .addr_bits = 8, .data_bits = 16,
465 .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
466 .i2c_read = snd_soc_8_16_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100467 .spi_write = snd_soc_8_16_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700468 },
Barry Song994dc422010-01-27 11:46:18 +0800469 {
470 .addr_bits = 16, .data_bits = 8,
471 .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
472 .i2c_read = snd_soc_16_8_read_i2c,
473 .spi_write = snd_soc_16_8_spi_write,
474 },
Mark Brownbc6552f2010-03-05 16:27:15 +0000475 {
476 .addr_bits = 16, .data_bits = 16,
477 .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
478 .i2c_read = snd_soc_16_16_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100479 .spi_write = snd_soc_16_16_spi_write,
Mark Brownbc6552f2010-03-05 16:27:15 +0000480 },
Mark Brown17a52fd2009-07-05 17:24:50 +0100481};
482
483/**
484 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
485 *
486 * @codec: CODEC to configure.
487 * @type: Type of cache.
488 * @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 {
598 struct rb_node node;
599 unsigned int reg;
600 unsigned int value;
601 unsigned int defval;
602} __attribute__ ((packed));
603
604struct snd_soc_rbtree_ctx {
605 struct rb_root root;
606};
607
608static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
609 struct rb_root *root, unsigned int reg)
610{
611 struct rb_node *node;
612 struct snd_soc_rbtree_node *rbnode;
613
614 node = root->rb_node;
615 while (node) {
616 rbnode = container_of(node, struct snd_soc_rbtree_node, node);
617 if (rbnode->reg < reg)
618 node = node->rb_left;
619 else if (rbnode->reg > reg)
620 node = node->rb_right;
621 else
622 return rbnode;
623 }
624
625 return NULL;
626}
627
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000628static int snd_soc_rbtree_insert(struct rb_root *root,
629 struct snd_soc_rbtree_node *rbnode)
630{
631 struct rb_node **new, *parent;
632 struct snd_soc_rbtree_node *rbnode_tmp;
633
634 parent = NULL;
635 new = &root->rb_node;
636 while (*new) {
637 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
638 node);
639 parent = *new;
640 if (rbnode_tmp->reg < rbnode->reg)
641 new = &((*new)->rb_left);
642 else if (rbnode_tmp->reg > rbnode->reg)
643 new = &((*new)->rb_right);
644 else
645 return 0;
646 }
647
648 /* insert the node into the rbtree */
649 rb_link_node(&rbnode->node, parent, new);
650 rb_insert_color(&rbnode->node, root);
651
652 return 1;
653}
654
655static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
656{
657 struct snd_soc_rbtree_ctx *rbtree_ctx;
658 struct rb_node *node;
659 struct snd_soc_rbtree_node *rbnode;
660 unsigned int val;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000661 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000662
663 rbtree_ctx = codec->reg_cache;
664 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
665 rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
666 if (rbnode->value == rbnode->defval)
667 continue;
Dimitris Papastamosf20eda52011-03-28 11:39:15 +0100668 WARN_ON(codec->writable_register &&
669 codec->writable_register(codec, rbnode->reg));
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000670 ret = snd_soc_cache_read(codec, rbnode->reg, &val);
671 if (ret)
672 return ret;
Dimitris Papastamos99780072011-01-19 14:53:37 +0000673 codec->cache_bypass = 1;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000674 ret = snd_soc_write(codec, rbnode->reg, val);
Dimitris Papastamos99780072011-01-19 14:53:37 +0000675 codec->cache_bypass = 0;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000676 if (ret)
677 return ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000678 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
679 rbnode->reg, val);
680 }
681
682 return 0;
683}
684
685static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
686 unsigned int reg, unsigned int value)
687{
688 struct snd_soc_rbtree_ctx *rbtree_ctx;
689 struct snd_soc_rbtree_node *rbnode;
690
691 rbtree_ctx = codec->reg_cache;
692 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
693 if (rbnode) {
694 if (rbnode->value == value)
695 return 0;
696 rbnode->value = value;
697 } else {
698 /* bail out early, no need to create the rbnode yet */
699 if (!value)
700 return 0;
701 /*
702 * for uninitialized registers whose value is changed
703 * from the default zero, create an rbnode and insert
704 * it into the tree.
705 */
706 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
707 if (!rbnode)
708 return -ENOMEM;
709 rbnode->reg = reg;
710 rbnode->value = value;
711 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
712 }
713
714 return 0;
715}
716
717static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
718 unsigned int reg, unsigned int *value)
719{
720 struct snd_soc_rbtree_ctx *rbtree_ctx;
721 struct snd_soc_rbtree_node *rbnode;
722
723 rbtree_ctx = codec->reg_cache;
724 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
725 if (rbnode) {
726 *value = rbnode->value;
727 } else {
728 /* uninitialized registers default to 0 */
729 *value = 0;
730 }
731
732 return 0;
733}
734
735static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
736{
737 struct rb_node *next;
738 struct snd_soc_rbtree_ctx *rbtree_ctx;
739 struct snd_soc_rbtree_node *rbtree_node;
740
741 /* if we've already been called then just return */
742 rbtree_ctx = codec->reg_cache;
743 if (!rbtree_ctx)
744 return 0;
745
746 /* free up the rbtree */
747 next = rb_first(&rbtree_ctx->root);
748 while (next) {
749 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
750 next = rb_next(&rbtree_node->node);
751 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
752 kfree(rbtree_node);
753 }
754
755 /* release the resources */
756 kfree(codec->reg_cache);
757 codec->reg_cache = NULL;
758
759 return 0;
760}
761
762static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
763{
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000764 struct snd_soc_rbtree_node *rbtree_node;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000765 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000766 unsigned int val;
767 unsigned int word_size;
768 int i;
769 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000770
771 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
772 if (!codec->reg_cache)
773 return -ENOMEM;
774
775 rbtree_ctx = codec->reg_cache;
776 rbtree_ctx->root = RB_ROOT;
777
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +0000778 if (!codec->reg_def_copy)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000779 return 0;
780
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000781 /*
782 * populate the rbtree with the initialized registers. All other
783 * registers will be inserted when they are first modified.
784 */
785 word_size = codec->driver->reg_word_size;
786 for (i = 0; i < codec->driver->reg_cache_size; ++i) {
787 val = snd_soc_get_cache_val(codec->reg_def_copy, i, word_size);
788 if (!val)
789 continue;
790 rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL);
791 if (!rbtree_node) {
792 ret = -ENOMEM;
793 snd_soc_cache_exit(codec);
794 break;
795 }
796 rbtree_node->reg = i;
797 rbtree_node->value = val;
798 rbtree_node->defval = val;
799 snd_soc_rbtree_insert(&rbtree_ctx->root, rbtree_node);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000800 }
801
802 return 0;
803}
804
Mark Brown68d44ee2010-12-21 17:19:56 +0000805#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000806struct snd_soc_lzo_ctx {
807 void *wmem;
808 void *dst;
809 const void *src;
810 size_t src_len;
811 size_t dst_len;
812 size_t decompressed_size;
813 unsigned long *sync_bmp;
814 int sync_bmp_nbits;
815};
816
817#define LZO_BLOCK_NUM 8
818static int snd_soc_lzo_block_count(void)
819{
820 return LZO_BLOCK_NUM;
821}
822
823static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
824{
825 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
826 if (!lzo_ctx->wmem)
827 return -ENOMEM;
828 return 0;
829}
830
831static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
832{
833 size_t compress_size;
834 int ret;
835
836 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
837 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
838 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
839 return -EINVAL;
840 lzo_ctx->dst_len = compress_size;
841 return 0;
842}
843
844static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
845{
846 size_t dst_len;
847 int ret;
848
849 dst_len = lzo_ctx->dst_len;
850 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
851 lzo_ctx->dst, &dst_len);
852 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
853 return -EINVAL;
854 return 0;
855}
856
857static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
858 struct snd_soc_lzo_ctx *lzo_ctx)
859{
860 int ret;
861
862 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
863 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
864 if (!lzo_ctx->dst) {
865 lzo_ctx->dst_len = 0;
866 return -ENOMEM;
867 }
868
869 ret = snd_soc_lzo_compress(lzo_ctx);
870 if (ret < 0)
871 return ret;
872 return 0;
873}
874
875static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
876 struct snd_soc_lzo_ctx *lzo_ctx)
877{
878 int ret;
879
880 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
881 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
882 if (!lzo_ctx->dst) {
883 lzo_ctx->dst_len = 0;
884 return -ENOMEM;
885 }
886
887 ret = snd_soc_lzo_decompress(lzo_ctx);
888 if (ret < 0)
889 return ret;
890 return 0;
891}
892
893static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
894 unsigned int reg)
895{
Mark Brown001ae4c2010-12-02 16:21:08 +0000896 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000897
898 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000899 return (reg * codec_drv->reg_word_size) /
Dimitris Papastamosaea170a2011-01-12 10:38:58 +0000900 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000901}
902
903static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
904 unsigned int reg)
905{
Mark Brown001ae4c2010-12-02 16:21:08 +0000906 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000907
908 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +0000909 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000910 codec_drv->reg_word_size);
911}
912
913static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
914{
Mark Brown001ae4c2010-12-02 16:21:08 +0000915 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000916
917 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +0000918 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000919}
920
921static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
922{
923 struct snd_soc_lzo_ctx **lzo_blocks;
924 unsigned int val;
925 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000926 int ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000927
928 lzo_blocks = codec->reg_cache;
929 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
Dimitris Papastamosf20eda52011-03-28 11:39:15 +0100930 WARN_ON(codec->writable_register &&
931 codec->writable_register(codec, i));
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000932 ret = snd_soc_cache_read(codec, i, &val);
933 if (ret)
934 return ret;
Dimitris Papastamos99780072011-01-19 14:53:37 +0000935 codec->cache_bypass = 1;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000936 ret = snd_soc_write(codec, i, val);
Dimitris Papastamos99780072011-01-19 14:53:37 +0000937 codec->cache_bypass = 0;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000938 if (ret)
939 return ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000940 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
941 i, val);
942 }
943
944 return 0;
945}
946
947static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
948 unsigned int reg, unsigned int value)
949{
950 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
951 int ret, blkindex, blkpos;
952 size_t blksize, tmp_dst_len;
953 void *tmp_dst;
954
955 /* index of the compressed lzo block */
956 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
957 /* register index within the decompressed block */
958 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
959 /* size of the compressed block */
960 blksize = snd_soc_lzo_get_blksize(codec);
961 lzo_blocks = codec->reg_cache;
962 lzo_block = lzo_blocks[blkindex];
963
964 /* save the pointer and length of the compressed block */
965 tmp_dst = lzo_block->dst;
966 tmp_dst_len = lzo_block->dst_len;
967
968 /* prepare the source to be the compressed block */
969 lzo_block->src = lzo_block->dst;
970 lzo_block->src_len = lzo_block->dst_len;
971
972 /* decompress the block */
973 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
974 if (ret < 0) {
975 kfree(lzo_block->dst);
976 goto out;
977 }
978
979 /* write the new value to the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000980 if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
981 codec->driver->reg_word_size)) {
982 kfree(lzo_block->dst);
983 goto out;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000984 }
985
986 /* prepare the source to be the decompressed block */
987 lzo_block->src = lzo_block->dst;
988 lzo_block->src_len = lzo_block->dst_len;
989
990 /* compress the block */
991 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
992 if (ret < 0) {
993 kfree(lzo_block->dst);
994 kfree(lzo_block->src);
995 goto out;
996 }
997
998 /* set the bit so we know we have to sync this register */
999 set_bit(reg, lzo_block->sync_bmp);
1000 kfree(tmp_dst);
1001 kfree(lzo_block->src);
1002 return 0;
1003out:
1004 lzo_block->dst = tmp_dst;
1005 lzo_block->dst_len = tmp_dst_len;
1006 return ret;
1007}
1008
1009static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1010 unsigned int reg, unsigned int *value)
1011{
1012 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1013 int ret, blkindex, blkpos;
1014 size_t blksize, tmp_dst_len;
1015 void *tmp_dst;
1016
1017 *value = 0;
1018 /* index of the compressed lzo block */
1019 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1020 /* register index within the decompressed block */
1021 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1022 /* size of the compressed block */
1023 blksize = snd_soc_lzo_get_blksize(codec);
1024 lzo_blocks = codec->reg_cache;
1025 lzo_block = lzo_blocks[blkindex];
1026
1027 /* save the pointer and length of the compressed block */
1028 tmp_dst = lzo_block->dst;
1029 tmp_dst_len = lzo_block->dst_len;
1030
1031 /* prepare the source to be the compressed block */
1032 lzo_block->src = lzo_block->dst;
1033 lzo_block->src_len = lzo_block->dst_len;
1034
1035 /* decompress the block */
1036 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001037 if (ret >= 0)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001038 /* fetch the value from the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001039 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1040 codec->driver->reg_word_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001041
1042 kfree(lzo_block->dst);
1043 /* restore the pointer and length of the compressed block */
1044 lzo_block->dst = tmp_dst;
1045 lzo_block->dst_len = tmp_dst_len;
1046 return 0;
1047}
1048
1049static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1050{
1051 struct snd_soc_lzo_ctx **lzo_blocks;
1052 int i, blkcount;
1053
1054 lzo_blocks = codec->reg_cache;
1055 if (!lzo_blocks)
1056 return 0;
1057
1058 blkcount = snd_soc_lzo_block_count();
1059 /*
1060 * the pointer to the bitmap used for syncing the cache
1061 * is shared amongst all lzo_blocks. Ensure it is freed
1062 * only once.
1063 */
1064 if (lzo_blocks[0])
1065 kfree(lzo_blocks[0]->sync_bmp);
1066 for (i = 0; i < blkcount; ++i) {
1067 if (lzo_blocks[i]) {
1068 kfree(lzo_blocks[i]->wmem);
1069 kfree(lzo_blocks[i]->dst);
1070 }
1071 /* each lzo_block is a pointer returned by kmalloc or NULL */
1072 kfree(lzo_blocks[i]);
1073 }
1074 kfree(lzo_blocks);
1075 codec->reg_cache = NULL;
1076 return 0;
1077}
1078
1079static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1080{
1081 struct snd_soc_lzo_ctx **lzo_blocks;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001082 size_t bmp_size;
Mark Brown001ae4c2010-12-02 16:21:08 +00001083 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001084 int ret, tofree, i, blksize, blkcount;
1085 const char *p, *end;
1086 unsigned long *sync_bmp;
1087
1088 ret = 0;
1089 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001090
1091 /*
1092 * If we have not been given a default register cache
1093 * then allocate a dummy zero-ed out region, compress it
1094 * and remember to free it afterwards.
1095 */
1096 tofree = 0;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001097 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001098 tofree = 1;
1099
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001100 if (!codec->reg_def_copy) {
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001101 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001102 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001103 return -ENOMEM;
1104 }
1105
1106 blkcount = snd_soc_lzo_block_count();
1107 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1108 GFP_KERNEL);
1109 if (!codec->reg_cache) {
1110 ret = -ENOMEM;
1111 goto err_tofree;
1112 }
1113 lzo_blocks = codec->reg_cache;
1114
1115 /*
1116 * allocate a bitmap to be used when syncing the cache with
1117 * the hardware. Each time a register is modified, the corresponding
1118 * bit is set in the bitmap, so we know that we have to sync
1119 * that register.
1120 */
1121 bmp_size = codec_drv->reg_cache_size;
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +00001122 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001123 GFP_KERNEL);
1124 if (!sync_bmp) {
1125 ret = -ENOMEM;
1126 goto err;
1127 }
Dimitris Papastamos09c74a92010-11-29 11:43:33 +00001128 bitmap_zero(sync_bmp, bmp_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001129
1130 /* allocate the lzo blocks and initialize them */
1131 for (i = 0; i < blkcount; ++i) {
1132 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1133 GFP_KERNEL);
1134 if (!lzo_blocks[i]) {
1135 kfree(sync_bmp);
1136 ret = -ENOMEM;
1137 goto err;
1138 }
1139 lzo_blocks[i]->sync_bmp = sync_bmp;
Dimitris Papastamos04f8fd12011-01-11 11:24:02 +00001140 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001141 /* alloc the working space for the compressed block */
1142 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1143 if (ret < 0)
1144 goto err;
1145 }
1146
1147 blksize = snd_soc_lzo_get_blksize(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001148 p = codec->reg_def_copy;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001149 end = codec->reg_def_copy + codec->reg_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001150 /* compress the register map and fill the lzo blocks */
1151 for (i = 0; i < blkcount; ++i, p += blksize) {
1152 lzo_blocks[i]->src = p;
1153 if (p + blksize > end)
1154 lzo_blocks[i]->src_len = end - p;
1155 else
1156 lzo_blocks[i]->src_len = blksize;
1157 ret = snd_soc_lzo_compress_cache_block(codec,
1158 lzo_blocks[i]);
1159 if (ret < 0)
1160 goto err;
1161 lzo_blocks[i]->decompressed_size =
1162 lzo_blocks[i]->src_len;
1163 }
1164
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001165 if (tofree) {
1166 kfree(codec->reg_def_copy);
1167 codec->reg_def_copy = NULL;
1168 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001169 return 0;
1170err:
1171 snd_soc_cache_exit(codec);
1172err_tofree:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001173 if (tofree) {
1174 kfree(codec->reg_def_copy);
1175 codec->reg_def_copy = NULL;
1176 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001177 return ret;
1178}
Mark Brown68d44ee2010-12-21 17:19:56 +00001179#endif
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001180
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001181static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1182{
1183 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001184 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +00001185 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001186 unsigned int val;
1187
1188 codec_drv = codec->driver;
1189 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamosf20eda52011-03-28 11:39:15 +01001190 WARN_ON(codec->writable_register &&
1191 codec->writable_register(codec, i));
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001192 ret = snd_soc_cache_read(codec, i, &val);
1193 if (ret)
1194 return ret;
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001195 if (codec->reg_def_copy)
1196 if (snd_soc_get_cache_val(codec->reg_def_copy,
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001197 i, codec_drv->reg_word_size) == val)
1198 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001199 ret = snd_soc_write(codec, i, val);
1200 if (ret)
1201 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001202 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1203 i, val);
1204 }
1205 return 0;
1206}
1207
1208static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1209 unsigned int reg, unsigned int value)
1210{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001211 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1212 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001213 return 0;
1214}
1215
1216static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1217 unsigned int reg, unsigned int *value)
1218{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001219 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1220 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001221 return 0;
1222}
1223
1224static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1225{
1226 if (!codec->reg_cache)
1227 return 0;
1228 kfree(codec->reg_cache);
1229 codec->reg_cache = NULL;
1230 return 0;
1231}
1232
1233static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1234{
Mark Brown001ae4c2010-12-02 16:21:08 +00001235 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001236
1237 codec_drv = codec->driver;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001238
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001239 if (codec->reg_def_copy)
1240 codec->reg_cache = kmemdup(codec->reg_def_copy,
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001241 codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001242 else
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001243 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001244 if (!codec->reg_cache)
1245 return -ENOMEM;
1246
1247 return 0;
1248}
1249
1250/* an array of all supported compression types */
1251static const struct snd_soc_cache_ops cache_types[] = {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001252 /* Flat *must* be the first entry for fallback */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001253 {
Dimitris Papastamosdf0701b2010-11-29 10:54:28 +00001254 .id = SND_SOC_FLAT_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001255 .name = "flat",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001256 .init = snd_soc_flat_cache_init,
1257 .exit = snd_soc_flat_cache_exit,
1258 .read = snd_soc_flat_cache_read,
1259 .write = snd_soc_flat_cache_write,
1260 .sync = snd_soc_flat_cache_sync
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001261 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001262#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001263 {
1264 .id = SND_SOC_LZO_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001265 .name = "LZO",
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001266 .init = snd_soc_lzo_cache_init,
1267 .exit = snd_soc_lzo_cache_exit,
1268 .read = snd_soc_lzo_cache_read,
1269 .write = snd_soc_lzo_cache_write,
1270 .sync = snd_soc_lzo_cache_sync
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001271 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001272#endif
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001273 {
1274 .id = SND_SOC_RBTREE_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001275 .name = "rbtree",
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001276 .init = snd_soc_rbtree_cache_init,
1277 .exit = snd_soc_rbtree_cache_exit,
1278 .read = snd_soc_rbtree_cache_read,
1279 .write = snd_soc_rbtree_cache_write,
1280 .sync = snd_soc_rbtree_cache_sync
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001281 }
1282};
1283
1284int snd_soc_cache_init(struct snd_soc_codec *codec)
1285{
1286 int i;
1287
1288 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00001289 if (cache_types[i].id == codec->compress_type)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001290 break;
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001291
1292 /* Fall back to flat compression */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001293 if (i == ARRAY_SIZE(cache_types)) {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001294 dev_warn(codec->dev, "Could not match compress type: %d\n",
1295 codec->compress_type);
1296 i = 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001297 }
1298
1299 mutex_init(&codec->cache_rw_mutex);
1300 codec->cache_ops = &cache_types[i];
1301
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001302 if (codec->cache_ops->init) {
1303 if (codec->cache_ops->name)
1304 dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1305 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001306 return codec->cache_ops->init(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001307 }
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001308 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001309}
1310
1311/*
1312 * NOTE: keep in mind that this function might be called
1313 * multiple times.
1314 */
1315int snd_soc_cache_exit(struct snd_soc_codec *codec)
1316{
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001317 if (codec->cache_ops && codec->cache_ops->exit) {
1318 if (codec->cache_ops->name)
1319 dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1320 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001321 return codec->cache_ops->exit(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001322 }
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001323 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001324}
1325
1326/**
1327 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1328 *
1329 * @codec: CODEC to configure.
1330 * @reg: The register index.
1331 * @value: The value to be returned.
1332 */
1333int snd_soc_cache_read(struct snd_soc_codec *codec,
1334 unsigned int reg, unsigned int *value)
1335{
1336 int ret;
1337
1338 mutex_lock(&codec->cache_rw_mutex);
1339
1340 if (value && codec->cache_ops && codec->cache_ops->read) {
1341 ret = codec->cache_ops->read(codec, reg, value);
1342 mutex_unlock(&codec->cache_rw_mutex);
1343 return ret;
1344 }
1345
1346 mutex_unlock(&codec->cache_rw_mutex);
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001347 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001348}
1349EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1350
1351/**
1352 * snd_soc_cache_write: Set the value of a given register in the cache.
1353 *
1354 * @codec: CODEC to configure.
1355 * @reg: The register index.
1356 * @value: The new register value.
1357 */
1358int snd_soc_cache_write(struct snd_soc_codec *codec,
1359 unsigned int reg, unsigned int value)
1360{
1361 int ret;
1362
1363 mutex_lock(&codec->cache_rw_mutex);
1364
1365 if (codec->cache_ops && codec->cache_ops->write) {
1366 ret = codec->cache_ops->write(codec, reg, value);
1367 mutex_unlock(&codec->cache_rw_mutex);
1368 return ret;
1369 }
1370
1371 mutex_unlock(&codec->cache_rw_mutex);
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001372 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001373}
1374EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1375
1376/**
1377 * snd_soc_cache_sync: Sync the register cache with the hardware.
1378 *
1379 * @codec: CODEC to configure.
1380 *
1381 * Any registers that should not be synced should be marked as
1382 * volatile. In general drivers can choose not to use the provided
1383 * syncing functionality if they so require.
1384 */
1385int snd_soc_cache_sync(struct snd_soc_codec *codec)
1386{
1387 int ret;
Dimitris Papastamosc358e642011-01-21 15:29:02 +00001388 const char *name;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001389
1390 if (!codec->cache_sync) {
1391 return 0;
1392 }
1393
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001394 if (!codec->cache_ops || !codec->cache_ops->sync)
Dimitris Papastamosacd61452011-03-22 10:48:49 +00001395 return -ENOSYS;
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001396
Dimitris Papastamosc358e642011-01-21 15:29:02 +00001397 if (codec->cache_ops->name)
1398 name = codec->cache_ops->name;
1399 else
1400 name = "unknown";
1401
Dan Carpenter46fdaa32011-02-07 22:01:41 +03001402 if (codec->cache_ops->name)
1403 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1404 codec->cache_ops->name, codec->name);
1405 trace_snd_soc_cache_sync(codec, name, "start");
1406 ret = codec->cache_ops->sync(codec);
1407 if (!ret)
1408 codec->cache_sync = 0;
1409 trace_snd_soc_cache_sync(codec, name, "end");
1410 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001411}
1412EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
Dimitris Papastamos066d16c2011-01-13 12:20:36 +00001413
1414static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
1415 unsigned int reg)
1416{
1417 const struct snd_soc_codec_driver *codec_drv;
1418 unsigned int min, max, index;
1419
1420 codec_drv = codec->driver;
1421 min = 0;
1422 max = codec_drv->reg_access_size - 1;
1423 do {
1424 index = (min + max) / 2;
1425 if (codec_drv->reg_access_default[index].reg == reg)
1426 return index;
1427 if (codec_drv->reg_access_default[index].reg < reg)
1428 min = index + 1;
1429 else
1430 max = index;
1431 } while (min <= max);
1432 return -1;
1433}
1434
1435int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
1436 unsigned int reg)
1437{
1438 int index;
1439
1440 if (reg >= codec->driver->reg_cache_size)
1441 return 1;
1442 index = snd_soc_get_reg_access_index(codec, reg);
1443 if (index < 0)
1444 return 0;
1445 return codec->driver->reg_access_default[index].vol;
1446}
1447EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
1448
1449int snd_soc_default_readable_register(struct snd_soc_codec *codec,
1450 unsigned int reg)
1451{
1452 int index;
1453
1454 if (reg >= codec->driver->reg_cache_size)
1455 return 1;
1456 index = snd_soc_get_reg_access_index(codec, reg);
1457 if (index < 0)
1458 return 0;
1459 return codec->driver->reg_access_default[index].read;
1460}
1461EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
Dimitris Papastamos80204542011-03-24 13:45:17 +00001462
1463int snd_soc_default_writable_register(struct snd_soc_codec *codec,
1464 unsigned int reg)
1465{
1466 int index;
1467
1468 if (reg >= codec->driver->reg_cache_size)
1469 return 1;
1470 index = snd_soc_get_reg_access_index(codec, reg);
1471 if (index < 0)
1472 return 0;
1473 return codec->driver->reg_access_default[index].write;
1474}
1475EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);