blob: b2e333f5a38899f5b3afbe8e2cf5de9fc5b375ea [file] [log] [blame]
Mark Brown17a52fd2009-07-05 17:24:50 +01001/*
2 * soc-cache.c -- ASoC register cache helpers
3 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
Mark Brown7084a422009-07-10 22:24:27 +010014#include <linux/i2c.h>
Mark Brown27ded042009-07-10 23:28:16 +010015#include <linux/spi/spi.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010016#include <sound/soc.h>
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +000017#include <linux/lzo.h>
18#include <linux/bitmap.h>
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +000019#include <linux/rbtree.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010020
Barry Song63b62ab2010-01-27 11:46:17 +080021static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec,
22 unsigned int reg)
23{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000024 int ret;
25 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010026
27 if (reg >= codec->driver->reg_cache_size ||
28 snd_soc_codec_volatile_register(codec, reg)) {
29 if (codec->cache_only)
30 return -1;
31
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +000032 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010033 return codec->hw_read(codec, reg);
34 }
35
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000036 ret = snd_soc_cache_read(codec, reg, &val);
37 if (ret < 0)
38 return -1;
39 return val;
Barry Song63b62ab2010-01-27 11:46:17 +080040}
41
42static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg,
43 unsigned int value)
44{
Barry Song63b62ab2010-01-27 11:46:17 +080045 u8 data[2];
46 int ret;
47
Barry Song63b62ab2010-01-27 11:46:17 +080048 data[0] = (reg << 4) | ((value >> 8) & 0x000f);
49 data[1] = value & 0x00ff;
50
Dimitris Papastamosdb49c142010-09-22 13:25:47 +010051 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000052 reg < codec->driver->reg_cache_size) {
53 ret = snd_soc_cache_write(codec, reg, value);
54 if (ret < 0)
55 return -1;
56 }
Mark Brown8c961bc2010-02-01 18:46:10 +000057
Mark Browna3032b42010-02-01 18:48:03 +000058 if (codec->cache_only) {
59 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +000060 return 0;
Mark Browna3032b42010-02-01 18:48:03 +000061 }
Mark Brown8c961bc2010-02-01 18:46:10 +000062
Barry Song63b62ab2010-01-27 11:46:17 +080063 ret = codec->hw_write(codec->control_data, data, 2);
64 if (ret == 2)
65 return 0;
66 if (ret < 0)
67 return ret;
68 else
69 return -EIO;
70}
71
72#if defined(CONFIG_SPI_MASTER)
73static int snd_soc_4_12_spi_write(void *control_data, const char *data,
74 int len)
75{
76 struct spi_device *spi = control_data;
77 struct spi_transfer t;
78 struct spi_message m;
79 u8 msg[2];
80
81 if (len <= 0)
82 return 0;
83
84 msg[0] = data[1];
85 msg[1] = data[0];
86
87 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +000088 memset(&t, 0, sizeof t);
Barry Song63b62ab2010-01-27 11:46:17 +080089
90 t.tx_buf = &msg[0];
91 t.len = len;
92
93 spi_message_add_tail(&t, &m);
94 spi_sync(spi, &m);
95
96 return len;
97}
98#else
99#define snd_soc_4_12_spi_write NULL
100#endif
101
Mark Brown17a52fd2009-07-05 17:24:50 +0100102static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
103 unsigned int reg)
104{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000105 int ret;
106 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100107
108 if (reg >= codec->driver->reg_cache_size ||
109 snd_soc_codec_volatile_register(codec, reg)) {
110 if (codec->cache_only)
111 return -1;
112
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000113 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100114 return codec->hw_read(codec, reg);
115 }
116
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000117 ret = snd_soc_cache_read(codec, reg, &val);
118 if (ret < 0)
119 return -1;
120 return val;
Mark Brown17a52fd2009-07-05 17:24:50 +0100121}
122
123static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
124 unsigned int value)
125{
Mark Brown17a52fd2009-07-05 17:24:50 +0100126 u8 data[2];
127 int ret;
128
Mark Brown17a52fd2009-07-05 17:24:50 +0100129 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
130 data[1] = value & 0x00ff;
131
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100132 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000133 reg < codec->driver->reg_cache_size) {
134 ret = snd_soc_cache_write(codec, reg, value);
135 if (ret < 0)
136 return -1;
137 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000138
Mark Browna3032b42010-02-01 18:48:03 +0000139 if (codec->cache_only) {
140 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000141 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000142 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000143
Mark Brown17a52fd2009-07-05 17:24:50 +0100144 ret = codec->hw_write(codec->control_data, data, 2);
145 if (ret == 2)
146 return 0;
147 if (ret < 0)
148 return ret;
149 else
150 return -EIO;
151}
152
Mark Brown27ded042009-07-10 23:28:16 +0100153#if defined(CONFIG_SPI_MASTER)
154static int snd_soc_7_9_spi_write(void *control_data, const char *data,
155 int len)
156{
157 struct spi_device *spi = control_data;
158 struct spi_transfer t;
159 struct spi_message m;
160 u8 msg[2];
161
162 if (len <= 0)
163 return 0;
164
165 msg[0] = data[0];
166 msg[1] = data[1];
167
168 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000169 memset(&t, 0, sizeof t);
Mark Brown27ded042009-07-10 23:28:16 +0100170
171 t.tx_buf = &msg[0];
172 t.len = len;
173
174 spi_message_add_tail(&t, &m);
175 spi_sync(spi, &m);
176
177 return len;
178}
179#else
180#define snd_soc_7_9_spi_write NULL
181#endif
182
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900183static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg,
184 unsigned int value)
185{
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900186 u8 data[2];
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000187 int ret;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900188
Barry Songf4bee1b2010-03-18 16:17:01 +0800189 reg &= 0xff;
190 data[0] = reg;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900191 data[1] = value & 0xff;
192
Dimitris Papastamos005d65f2010-09-22 16:16:06 +0100193 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000194 reg < codec->driver->reg_cache_size) {
195 ret = snd_soc_cache_write(codec, reg, value);
196 if (ret < 0)
197 return -1;
198 }
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900199
Mark Browna3032b42010-02-01 18:48:03 +0000200 if (codec->cache_only) {
201 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000202 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000203 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000204
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900205 if (codec->hw_write(codec->control_data, data, 2) == 2)
206 return 0;
207 else
208 return -EIO;
209}
210
211static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec,
212 unsigned int reg)
213{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000214 int ret;
215 unsigned int val;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100216
Barry Songf4bee1b2010-03-18 16:17:01 +0800217 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100218 if (reg >= codec->driver->reg_cache_size ||
219 snd_soc_codec_volatile_register(codec, reg)) {
220 if (codec->cache_only)
221 return -1;
222
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000223 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100224 return codec->hw_read(codec, reg);
225 }
226
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000227 ret = snd_soc_cache_read(codec, reg, &val);
228 if (ret < 0)
229 return -1;
230 return val;
Joonyoung Shim341c9b82009-09-07 12:04:37 +0900231}
232
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100233#if defined(CONFIG_SPI_MASTER)
234static int snd_soc_8_8_spi_write(void *control_data, const char *data,
235 int len)
236{
237 struct spi_device *spi = control_data;
238 struct spi_transfer t;
239 struct spi_message m;
240 u8 msg[2];
241
242 if (len <= 0)
243 return 0;
244
245 msg[0] = data[0];
246 msg[1] = data[1];
247
248 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000249 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100250
251 t.tx_buf = &msg[0];
252 t.len = len;
253
254 spi_message_add_tail(&t, &m);
255 spi_sync(spi, &m);
256
257 return len;
258}
259#else
260#define snd_soc_8_8_spi_write NULL
261#endif
262
Mark Brownafa2f102009-07-10 23:11:24 +0100263static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg,
264 unsigned int value)
265{
Mark Brownafa2f102009-07-10 23:11:24 +0100266 u8 data[3];
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000267 int ret;
Mark Brownafa2f102009-07-10 23:11:24 +0100268
269 data[0] = reg;
270 data[1] = (value >> 8) & 0xff;
271 data[2] = value & 0xff;
272
Takashi Iwai3e13f652010-09-23 07:40:16 +0200273 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000274 reg < codec->driver->reg_cache_size) {
275 ret = snd_soc_cache_write(codec, reg, value);
276 if (ret < 0)
277 return -1;
278 }
Mark Brownafa2f102009-07-10 23:11:24 +0100279
Mark Browna3032b42010-02-01 18:48:03 +0000280 if (codec->cache_only) {
281 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000282 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000283 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000284
Mark Brownafa2f102009-07-10 23:11:24 +0100285 if (codec->hw_write(codec->control_data, data, 3) == 3)
286 return 0;
287 else
288 return -EIO;
289}
290
291static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec,
292 unsigned int reg)
293{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000294 int ret;
295 unsigned int val;
Mark Brownafa2f102009-07-10 23:11:24 +0100296
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000297 if (reg >= codec->driver->reg_cache_size ||
Mark Brown8c961bc2010-02-01 18:46:10 +0000298 snd_soc_codec_volatile_register(codec, reg)) {
299 if (codec->cache_only)
Dimitris Papastamos391d8a02010-09-21 17:04:07 +0100300 return -1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000301
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000302 BUG_ON(!codec->hw_read);
Mark Brownafa2f102009-07-10 23:11:24 +0100303 return codec->hw_read(codec, reg);
Mark Brown8c961bc2010-02-01 18:46:10 +0000304 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000305
306 ret = snd_soc_cache_read(codec, reg, &val);
307 if (ret < 0)
308 return -1;
309 return val;
Mark Brownafa2f102009-07-10 23:11:24 +0100310}
311
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100312#if defined(CONFIG_SPI_MASTER)
313static int snd_soc_8_16_spi_write(void *control_data, const char *data,
314 int len)
315{
316 struct spi_device *spi = control_data;
317 struct spi_transfer t;
318 struct spi_message m;
319 u8 msg[3];
320
321 if (len <= 0)
322 return 0;
323
324 msg[0] = data[0];
325 msg[1] = data[1];
326 msg[2] = data[2];
327
328 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000329 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100330
331 t.tx_buf = &msg[0];
332 t.len = len;
333
334 spi_message_add_tail(&t, &m);
335 spi_sync(spi, &m);
336
337 return len;
338}
339#else
340#define snd_soc_8_16_spi_write NULL
341#endif
342
Randy Dunlap17244c22009-08-10 16:04:39 -0700343#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800344static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec,
345 unsigned int r)
346{
347 struct i2c_msg xfer[2];
348 u8 reg = r;
349 u8 data;
350 int ret;
351 struct i2c_client *client = codec->control_data;
352
353 /* Write register */
354 xfer[0].addr = client->addr;
355 xfer[0].flags = 0;
356 xfer[0].len = 1;
357 xfer[0].buf = &reg;
358
359 /* Read data */
360 xfer[1].addr = client->addr;
361 xfer[1].flags = I2C_M_RD;
362 xfer[1].len = 1;
363 xfer[1].buf = &data;
364
365 ret = i2c_transfer(client->adapter, xfer, 2);
366 if (ret != 2) {
367 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
368 return 0;
369 }
370
371 return data;
372}
373#else
374#define snd_soc_8_8_read_i2c NULL
375#endif
376
377#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brownafa2f102009-07-10 23:11:24 +0100378static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec,
379 unsigned int r)
380{
381 struct i2c_msg xfer[2];
382 u8 reg = r;
383 u16 data;
384 int ret;
385 struct i2c_client *client = codec->control_data;
386
387 /* Write register */
388 xfer[0].addr = client->addr;
389 xfer[0].flags = 0;
390 xfer[0].len = 1;
391 xfer[0].buf = &reg;
392
393 /* Read data */
394 xfer[1].addr = client->addr;
395 xfer[1].flags = I2C_M_RD;
396 xfer[1].len = 2;
397 xfer[1].buf = (u8 *)&data;
398
399 ret = i2c_transfer(client->adapter, xfer, 2);
400 if (ret != 2) {
401 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
402 return 0;
403 }
404
405 return (data >> 8) | ((data & 0xff) << 8);
406}
407#else
408#define snd_soc_8_16_read_i2c NULL
409#endif
Mark Brown17a52fd2009-07-05 17:24:50 +0100410
Barry Song994dc422010-01-27 11:46:18 +0800411#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
412static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec,
413 unsigned int r)
414{
415 struct i2c_msg xfer[2];
416 u16 reg = r;
417 u8 data;
418 int ret;
419 struct i2c_client *client = codec->control_data;
420
421 /* Write register */
422 xfer[0].addr = client->addr;
423 xfer[0].flags = 0;
424 xfer[0].len = 2;
425 xfer[0].buf = (u8 *)&reg;
426
427 /* Read data */
428 xfer[1].addr = client->addr;
429 xfer[1].flags = I2C_M_RD;
430 xfer[1].len = 1;
431 xfer[1].buf = &data;
432
433 ret = i2c_transfer(client->adapter, xfer, 2);
434 if (ret != 2) {
435 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
436 return 0;
437 }
438
439 return data;
440}
441#else
442#define snd_soc_16_8_read_i2c NULL
443#endif
444
445static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec,
446 unsigned int reg)
447{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000448 int ret;
449 unsigned int val;
Barry Song994dc422010-01-27 11:46:18 +0800450
451 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100452 if (reg >= codec->driver->reg_cache_size ||
453 snd_soc_codec_volatile_register(codec, reg)) {
454 if (codec->cache_only)
455 return -1;
456
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000457 BUG_ON(!codec->hw_read);
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100458 return codec->hw_read(codec, reg);
459 }
460
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000461 ret = snd_soc_cache_read(codec, reg, &val);
462 if (ret < 0)
463 return -1;
464 return val;
Barry Song994dc422010-01-27 11:46:18 +0800465}
466
467static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg,
468 unsigned int value)
469{
Barry Song994dc422010-01-27 11:46:18 +0800470 u8 data[3];
471 int ret;
472
Barry Song994dc422010-01-27 11:46:18 +0800473 data[0] = (reg >> 8) & 0xff;
474 data[1] = reg & 0xff;
475 data[2] = value;
476
477 reg &= 0xff;
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100478 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000479 reg < codec->driver->reg_cache_size) {
480 ret = snd_soc_cache_write(codec, reg, value);
481 if (ret < 0)
482 return -1;
483 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000484
Mark Browna3032b42010-02-01 18:48:03 +0000485 if (codec->cache_only) {
486 codec->cache_sync = 1;
Mark Brown8c961bc2010-02-01 18:46:10 +0000487 return 0;
Mark Browna3032b42010-02-01 18:48:03 +0000488 }
Mark Brown8c961bc2010-02-01 18:46:10 +0000489
Barry Song994dc422010-01-27 11:46:18 +0800490 ret = codec->hw_write(codec->control_data, data, 3);
491 if (ret == 3)
492 return 0;
493 if (ret < 0)
494 return ret;
495 else
496 return -EIO;
497}
498
499#if defined(CONFIG_SPI_MASTER)
500static int snd_soc_16_8_spi_write(void *control_data, const char *data,
501 int len)
502{
503 struct spi_device *spi = control_data;
504 struct spi_transfer t;
505 struct spi_message m;
506 u8 msg[3];
507
508 if (len <= 0)
509 return 0;
510
511 msg[0] = data[0];
512 msg[1] = data[1];
513 msg[2] = data[2];
514
515 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000516 memset(&t, 0, sizeof t);
Barry Song994dc422010-01-27 11:46:18 +0800517
518 t.tx_buf = &msg[0];
519 t.len = len;
520
521 spi_message_add_tail(&t, &m);
522 spi_sync(spi, &m);
523
524 return len;
525}
526#else
527#define snd_soc_16_8_spi_write NULL
528#endif
529
Mark Brownbc6552f2010-03-05 16:27:15 +0000530#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
531static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec,
532 unsigned int r)
533{
534 struct i2c_msg xfer[2];
535 u16 reg = cpu_to_be16(r);
536 u16 data;
537 int ret;
538 struct i2c_client *client = codec->control_data;
539
540 /* Write register */
541 xfer[0].addr = client->addr;
542 xfer[0].flags = 0;
543 xfer[0].len = 2;
544 xfer[0].buf = (u8 *)&reg;
545
546 /* Read data */
547 xfer[1].addr = client->addr;
548 xfer[1].flags = I2C_M_RD;
549 xfer[1].len = 2;
550 xfer[1].buf = (u8 *)&data;
551
552 ret = i2c_transfer(client->adapter, xfer, 2);
553 if (ret != 2) {
554 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
555 return 0;
556 }
557
558 return be16_to_cpu(data);
559}
560#else
561#define snd_soc_16_16_read_i2c NULL
562#endif
563
564static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec,
565 unsigned int reg)
566{
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000567 int ret;
568 unsigned int val;
Mark Brownbc6552f2010-03-05 16:27:15 +0000569
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 if (reg >= codec->driver->reg_cache_size ||
Mark Brownbc6552f2010-03-05 16:27:15 +0000571 snd_soc_codec_volatile_register(codec, reg)) {
572 if (codec->cache_only)
Dimitris Papastamos391d8a02010-09-21 17:04:07 +0100573 return -1;
Mark Brownbc6552f2010-03-05 16:27:15 +0000574
Dimitris Papastamos5aaa0622010-11-08 15:37:07 +0000575 BUG_ON(!codec->hw_read);
Mark Brownbc6552f2010-03-05 16:27:15 +0000576 return codec->hw_read(codec, reg);
577 }
578
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000579 ret = snd_soc_cache_read(codec, reg, &val);
580 if (ret < 0)
581 return -1;
582
583 return val;
Mark Brownbc6552f2010-03-05 16:27:15 +0000584}
585
586static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg,
587 unsigned int value)
588{
Mark Brownbc6552f2010-03-05 16:27:15 +0000589 u8 data[4];
590 int ret;
591
592 data[0] = (reg >> 8) & 0xff;
593 data[1] = reg & 0xff;
594 data[2] = (value >> 8) & 0xff;
595 data[3] = value & 0xff;
596
Dimitris Papastamosdb49c142010-09-22 13:25:47 +0100597 if (!snd_soc_codec_volatile_register(codec, reg) &&
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000598 reg < codec->driver->reg_cache_size) {
599 ret = snd_soc_cache_write(codec, reg, value);
600 if (ret < 0)
601 return -1;
602 }
Mark Brownbc6552f2010-03-05 16:27:15 +0000603
604 if (codec->cache_only) {
605 codec->cache_sync = 1;
606 return 0;
607 }
608
609 ret = codec->hw_write(codec->control_data, data, 4);
610 if (ret == 4)
611 return 0;
612 if (ret < 0)
613 return ret;
614 else
615 return -EIO;
616}
Barry Song994dc422010-01-27 11:46:18 +0800617
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100618#if defined(CONFIG_SPI_MASTER)
619static int snd_soc_16_16_spi_write(void *control_data, const char *data,
620 int len)
621{
622 struct spi_device *spi = control_data;
623 struct spi_transfer t;
624 struct spi_message m;
625 u8 msg[4];
626
627 if (len <= 0)
628 return 0;
629
630 msg[0] = data[0];
631 msg[1] = data[1];
632 msg[2] = data[2];
633 msg[3] = data[3];
634
635 spi_message_init(&m);
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +0000636 memset(&t, 0, sizeof t);
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100637
638 t.tx_buf = &msg[0];
639 t.len = len;
640
641 spi_message_add_tail(&t, &m);
642 spi_sync(spi, &m);
643
644 return len;
645}
646#else
647#define snd_soc_16_16_spi_write NULL
648#endif
649
Mark Brown17a52fd2009-07-05 17:24:50 +0100650static struct {
651 int addr_bits;
652 int data_bits;
Mark Brownafa2f102009-07-10 23:11:24 +0100653 int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int);
Mark Brown27ded042009-07-10 23:28:16 +0100654 int (*spi_write)(void *, const char *, int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100655 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
Mark Brownafa2f102009-07-10 23:11:24 +0100656 unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int);
Mark Brown17a52fd2009-07-05 17:24:50 +0100657} io_types[] = {
Mark Brownd62ab352009-09-21 04:21:47 -0700658 {
Barry Song63b62ab2010-01-27 11:46:17 +0800659 .addr_bits = 4, .data_bits = 12,
660 .write = snd_soc_4_12_write, .read = snd_soc_4_12_read,
661 .spi_write = snd_soc_4_12_spi_write,
662 },
663 {
Mark Brownd62ab352009-09-21 04:21:47 -0700664 .addr_bits = 7, .data_bits = 9,
665 .write = snd_soc_7_9_write, .read = snd_soc_7_9_read,
Barry Song8998c892009-12-31 10:30:34 +0800666 .spi_write = snd_soc_7_9_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700667 },
668 {
669 .addr_bits = 8, .data_bits = 8,
670 .write = snd_soc_8_8_write, .read = snd_soc_8_8_read,
Cliff Cai85dfcdf2010-03-18 16:17:00 +0800671 .i2c_read = snd_soc_8_8_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100672 .spi_write = snd_soc_8_8_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700673 },
674 {
675 .addr_bits = 8, .data_bits = 16,
676 .write = snd_soc_8_16_write, .read = snd_soc_8_16_read,
677 .i2c_read = snd_soc_8_16_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100678 .spi_write = snd_soc_8_16_spi_write,
Mark Brownd62ab352009-09-21 04:21:47 -0700679 },
Barry Song994dc422010-01-27 11:46:18 +0800680 {
681 .addr_bits = 16, .data_bits = 8,
682 .write = snd_soc_16_8_write, .read = snd_soc_16_8_read,
683 .i2c_read = snd_soc_16_8_read_i2c,
684 .spi_write = snd_soc_16_8_spi_write,
685 },
Mark Brownbc6552f2010-03-05 16:27:15 +0000686 {
687 .addr_bits = 16, .data_bits = 16,
688 .write = snd_soc_16_16_write, .read = snd_soc_16_16_read,
689 .i2c_read = snd_soc_16_16_read_i2c,
Dimitris Papastamosf479fd92010-10-04 11:25:13 +0100690 .spi_write = snd_soc_16_16_spi_write,
Mark Brownbc6552f2010-03-05 16:27:15 +0000691 },
Mark Brown17a52fd2009-07-05 17:24:50 +0100692};
693
694/**
695 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
696 *
697 * @codec: CODEC to configure.
698 * @type: Type of cache.
699 * @addr_bits: Number of bits of register address data.
700 * @data_bits: Number of bits of data per register.
Mark Brown7084a422009-07-10 22:24:27 +0100701 * @control: Control bus used.
Mark Brown17a52fd2009-07-05 17:24:50 +0100702 *
703 * Register formats are frequently shared between many I2C and SPI
704 * devices. In order to promote code reuse the ASoC core provides
705 * some standard implementations of CODEC read and write operations
706 * which can be set up using this function.
707 *
708 * The caller is responsible for allocating and initialising the
709 * actual cache.
710 *
711 * Note that at present this code cannot be used by CODECs with
712 * volatile registers.
713 */
714int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
Mark Brown7084a422009-07-10 22:24:27 +0100715 int addr_bits, int data_bits,
716 enum snd_soc_control_type control)
Mark Brown17a52fd2009-07-05 17:24:50 +0100717{
718 int i;
719
Mark Brown17a52fd2009-07-05 17:24:50 +0100720 for (i = 0; i < ARRAY_SIZE(io_types); i++)
721 if (io_types[i].addr_bits == addr_bits &&
722 io_types[i].data_bits == data_bits)
723 break;
724 if (i == ARRAY_SIZE(io_types)) {
725 printk(KERN_ERR
726 "No I/O functions for %d bit address %d bit data\n",
727 addr_bits, data_bits);
728 return -EINVAL;
729 }
730
Mark Brownc3acec22010-12-02 16:15:29 +0000731 codec->write = io_types[i].write;
732 codec->read = io_types[i].read;
Mark Brown17a52fd2009-07-05 17:24:50 +0100733
Mark Brown7084a422009-07-10 22:24:27 +0100734 switch (control) {
735 case SND_SOC_CUSTOM:
736 break;
737
738 case SND_SOC_I2C:
Randy Dunlap17244c22009-08-10 16:04:39 -0700739#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Mark Brown7084a422009-07-10 22:24:27 +0100740 codec->hw_write = (hw_write_t)i2c_master_send;
741#endif
Mark Brownafa2f102009-07-10 23:11:24 +0100742 if (io_types[i].i2c_read)
743 codec->hw_read = io_types[i].i2c_read;
Mark Browna6d14342010-08-12 10:59:15 +0100744
745 codec->control_data = container_of(codec->dev,
746 struct i2c_client,
747 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100748 break;
749
750 case SND_SOC_SPI:
Mark Brown27ded042009-07-10 23:28:16 +0100751 if (io_types[i].spi_write)
752 codec->hw_write = io_types[i].spi_write;
Mark Browna6d14342010-08-12 10:59:15 +0100753
754 codec->control_data = container_of(codec->dev,
755 struct spi_device,
756 dev);
Mark Brown7084a422009-07-10 22:24:27 +0100757 break;
758 }
759
Mark Brown17a52fd2009-07-05 17:24:50 +0100760 return 0;
761}
762EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000763
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000764static bool snd_soc_set_cache_val(void *base, unsigned int idx,
765 unsigned int val, unsigned int word_size)
766{
767 switch (word_size) {
768 case 1: {
769 u8 *cache = base;
770 if (cache[idx] == val)
771 return true;
772 cache[idx] = val;
773 break;
774 }
775 case 2: {
776 u16 *cache = base;
777 if (cache[idx] == val)
778 return true;
779 cache[idx] = val;
780 break;
781 }
782 default:
783 BUG();
784 }
785 return false;
786}
787
788static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
789 unsigned int word_size)
790{
791 switch (word_size) {
792 case 1: {
793 const u8 *cache = base;
794 return cache[idx];
795 }
796 case 2: {
797 const u16 *cache = base;
798 return cache[idx];
799 }
800 default:
801 BUG();
802 }
803 /* unreachable */
804 return -1;
805}
806
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000807struct snd_soc_rbtree_node {
808 struct rb_node node;
809 unsigned int reg;
810 unsigned int value;
811 unsigned int defval;
812} __attribute__ ((packed));
813
814struct snd_soc_rbtree_ctx {
815 struct rb_root root;
816};
817
818static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
819 struct rb_root *root, unsigned int reg)
820{
821 struct rb_node *node;
822 struct snd_soc_rbtree_node *rbnode;
823
824 node = root->rb_node;
825 while (node) {
826 rbnode = container_of(node, struct snd_soc_rbtree_node, node);
827 if (rbnode->reg < reg)
828 node = node->rb_left;
829 else if (rbnode->reg > reg)
830 node = node->rb_right;
831 else
832 return rbnode;
833 }
834
835 return NULL;
836}
837
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000838static int snd_soc_rbtree_insert(struct rb_root *root,
839 struct snd_soc_rbtree_node *rbnode)
840{
841 struct rb_node **new, *parent;
842 struct snd_soc_rbtree_node *rbnode_tmp;
843
844 parent = NULL;
845 new = &root->rb_node;
846 while (*new) {
847 rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
848 node);
849 parent = *new;
850 if (rbnode_tmp->reg < rbnode->reg)
851 new = &((*new)->rb_left);
852 else if (rbnode_tmp->reg > rbnode->reg)
853 new = &((*new)->rb_right);
854 else
855 return 0;
856 }
857
858 /* insert the node into the rbtree */
859 rb_link_node(&rbnode->node, parent, new);
860 rb_insert_color(&rbnode->node, root);
861
862 return 1;
863}
864
865static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
866{
867 struct snd_soc_rbtree_ctx *rbtree_ctx;
868 struct rb_node *node;
869 struct snd_soc_rbtree_node *rbnode;
870 unsigned int val;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000871 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000872
873 rbtree_ctx = codec->reg_cache;
874 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
875 rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
876 if (rbnode->value == rbnode->defval)
877 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000878 ret = snd_soc_cache_read(codec, rbnode->reg, &val);
879 if (ret)
880 return ret;
881 ret = snd_soc_write(codec, rbnode->reg, val);
882 if (ret)
883 return ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000884 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
885 rbnode->reg, val);
886 }
887
888 return 0;
889}
890
891static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
892 unsigned int reg, unsigned int value)
893{
894 struct snd_soc_rbtree_ctx *rbtree_ctx;
895 struct snd_soc_rbtree_node *rbnode;
896
897 rbtree_ctx = codec->reg_cache;
898 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
899 if (rbnode) {
900 if (rbnode->value == value)
901 return 0;
902 rbnode->value = value;
903 } else {
904 /* bail out early, no need to create the rbnode yet */
905 if (!value)
906 return 0;
907 /*
908 * for uninitialized registers whose value is changed
909 * from the default zero, create an rbnode and insert
910 * it into the tree.
911 */
912 rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
913 if (!rbnode)
914 return -ENOMEM;
915 rbnode->reg = reg;
916 rbnode->value = value;
917 snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
918 }
919
920 return 0;
921}
922
923static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
924 unsigned int reg, unsigned int *value)
925{
926 struct snd_soc_rbtree_ctx *rbtree_ctx;
927 struct snd_soc_rbtree_node *rbnode;
928
929 rbtree_ctx = codec->reg_cache;
930 rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
931 if (rbnode) {
932 *value = rbnode->value;
933 } else {
934 /* uninitialized registers default to 0 */
935 *value = 0;
936 }
937
938 return 0;
939}
940
941static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
942{
943 struct rb_node *next;
944 struct snd_soc_rbtree_ctx *rbtree_ctx;
945 struct snd_soc_rbtree_node *rbtree_node;
946
947 /* if we've already been called then just return */
948 rbtree_ctx = codec->reg_cache;
949 if (!rbtree_ctx)
950 return 0;
951
952 /* free up the rbtree */
953 next = rb_first(&rbtree_ctx->root);
954 while (next) {
955 rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
956 next = rb_next(&rbtree_node->node);
957 rb_erase(&rbtree_node->node, &rbtree_ctx->root);
958 kfree(rbtree_node);
959 }
960
961 /* release the resources */
962 kfree(codec->reg_cache);
963 codec->reg_cache = NULL;
964
965 return 0;
966}
967
968static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
969{
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000970 struct snd_soc_rbtree_node *rbtree_node;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000971 struct snd_soc_rbtree_ctx *rbtree_ctx;
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000972 unsigned int val;
973 unsigned int word_size;
974 int i;
975 int ret;
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000976
977 codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
978 if (!codec->reg_cache)
979 return -ENOMEM;
980
981 rbtree_ctx = codec->reg_cache;
982 rbtree_ctx->root = RB_ROOT;
983
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +0000984 if (!codec->reg_def_copy)
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +0000985 return 0;
986
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000987 /*
988 * populate the rbtree with the initialized registers. All other
989 * registers will be inserted when they are first modified.
990 */
991 word_size = codec->driver->reg_word_size;
992 for (i = 0; i < codec->driver->reg_cache_size; ++i) {
993 val = snd_soc_get_cache_val(codec->reg_def_copy, i, word_size);
994 if (!val)
995 continue;
996 rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL);
997 if (!rbtree_node) {
998 ret = -ENOMEM;
999 snd_soc_cache_exit(codec);
1000 break;
1001 }
1002 rbtree_node->reg = i;
1003 rbtree_node->value = val;
1004 rbtree_node->defval = val;
1005 snd_soc_rbtree_insert(&rbtree_ctx->root, rbtree_node);
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001006 }
1007
1008 return 0;
1009}
1010
Mark Brown68d44ee2010-12-21 17:19:56 +00001011#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001012struct snd_soc_lzo_ctx {
1013 void *wmem;
1014 void *dst;
1015 const void *src;
1016 size_t src_len;
1017 size_t dst_len;
1018 size_t decompressed_size;
1019 unsigned long *sync_bmp;
1020 int sync_bmp_nbits;
1021};
1022
1023#define LZO_BLOCK_NUM 8
1024static int snd_soc_lzo_block_count(void)
1025{
1026 return LZO_BLOCK_NUM;
1027}
1028
1029static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
1030{
1031 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
1032 if (!lzo_ctx->wmem)
1033 return -ENOMEM;
1034 return 0;
1035}
1036
1037static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
1038{
1039 size_t compress_size;
1040 int ret;
1041
1042 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
1043 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
1044 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
1045 return -EINVAL;
1046 lzo_ctx->dst_len = compress_size;
1047 return 0;
1048}
1049
1050static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
1051{
1052 size_t dst_len;
1053 int ret;
1054
1055 dst_len = lzo_ctx->dst_len;
1056 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
1057 lzo_ctx->dst, &dst_len);
1058 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
1059 return -EINVAL;
1060 return 0;
1061}
1062
1063static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
1064 struct snd_soc_lzo_ctx *lzo_ctx)
1065{
1066 int ret;
1067
1068 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
1069 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1070 if (!lzo_ctx->dst) {
1071 lzo_ctx->dst_len = 0;
1072 return -ENOMEM;
1073 }
1074
1075 ret = snd_soc_lzo_compress(lzo_ctx);
1076 if (ret < 0)
1077 return ret;
1078 return 0;
1079}
1080
1081static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
1082 struct snd_soc_lzo_ctx *lzo_ctx)
1083{
1084 int ret;
1085
1086 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
1087 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
1088 if (!lzo_ctx->dst) {
1089 lzo_ctx->dst_len = 0;
1090 return -ENOMEM;
1091 }
1092
1093 ret = snd_soc_lzo_decompress(lzo_ctx);
1094 if (ret < 0)
1095 return ret;
1096 return 0;
1097}
1098
1099static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
1100 unsigned int reg)
1101{
Mark Brown001ae4c2010-12-02 16:21:08 +00001102 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001103
1104 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001105 return (reg * codec_drv->reg_word_size) /
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001106 DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001107}
1108
1109static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
1110 unsigned int reg)
1111{
Mark Brown001ae4c2010-12-02 16:21:08 +00001112 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001113
1114 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001115 return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001116 codec_drv->reg_word_size);
1117}
1118
1119static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
1120{
Mark Brown001ae4c2010-12-02 16:21:08 +00001121 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001122
1123 codec_drv = codec->driver;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001124 return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001125}
1126
1127static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
1128{
1129 struct snd_soc_lzo_ctx **lzo_blocks;
1130 unsigned int val;
1131 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001132 int ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001133
1134 lzo_blocks = codec->reg_cache;
1135 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001136 ret = snd_soc_cache_read(codec, i, &val);
1137 if (ret)
1138 return ret;
1139 ret = snd_soc_write(codec, i, val);
1140 if (ret)
1141 return ret;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001142 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1143 i, val);
1144 }
1145
1146 return 0;
1147}
1148
1149static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
1150 unsigned int reg, unsigned int value)
1151{
1152 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1153 int ret, blkindex, blkpos;
1154 size_t blksize, tmp_dst_len;
1155 void *tmp_dst;
1156
1157 /* index of the compressed lzo block */
1158 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1159 /* register index within the decompressed block */
1160 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1161 /* size of the compressed block */
1162 blksize = snd_soc_lzo_get_blksize(codec);
1163 lzo_blocks = codec->reg_cache;
1164 lzo_block = lzo_blocks[blkindex];
1165
1166 /* save the pointer and length of the compressed block */
1167 tmp_dst = lzo_block->dst;
1168 tmp_dst_len = lzo_block->dst_len;
1169
1170 /* prepare the source to be the compressed block */
1171 lzo_block->src = lzo_block->dst;
1172 lzo_block->src_len = lzo_block->dst_len;
1173
1174 /* decompress the block */
1175 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
1176 if (ret < 0) {
1177 kfree(lzo_block->dst);
1178 goto out;
1179 }
1180
1181 /* write the new value to the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001182 if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
1183 codec->driver->reg_word_size)) {
1184 kfree(lzo_block->dst);
1185 goto out;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001186 }
1187
1188 /* prepare the source to be the decompressed block */
1189 lzo_block->src = lzo_block->dst;
1190 lzo_block->src_len = lzo_block->dst_len;
1191
1192 /* compress the block */
1193 ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
1194 if (ret < 0) {
1195 kfree(lzo_block->dst);
1196 kfree(lzo_block->src);
1197 goto out;
1198 }
1199
1200 /* set the bit so we know we have to sync this register */
1201 set_bit(reg, lzo_block->sync_bmp);
1202 kfree(tmp_dst);
1203 kfree(lzo_block->src);
1204 return 0;
1205out:
1206 lzo_block->dst = tmp_dst;
1207 lzo_block->dst_len = tmp_dst_len;
1208 return ret;
1209}
1210
1211static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
1212 unsigned int reg, unsigned int *value)
1213{
1214 struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
1215 int ret, blkindex, blkpos;
1216 size_t blksize, tmp_dst_len;
1217 void *tmp_dst;
1218
1219 *value = 0;
1220 /* index of the compressed lzo block */
1221 blkindex = snd_soc_lzo_get_blkindex(codec, reg);
1222 /* register index within the decompressed block */
1223 blkpos = snd_soc_lzo_get_blkpos(codec, reg);
1224 /* size of the compressed block */
1225 blksize = snd_soc_lzo_get_blksize(codec);
1226 lzo_blocks = codec->reg_cache;
1227 lzo_block = lzo_blocks[blkindex];
1228
1229 /* save the pointer and length of the compressed block */
1230 tmp_dst = lzo_block->dst;
1231 tmp_dst_len = lzo_block->dst_len;
1232
1233 /* prepare the source to be the compressed block */
1234 lzo_block->src = lzo_block->dst;
1235 lzo_block->src_len = lzo_block->dst_len;
1236
1237 /* decompress the block */
1238 ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001239 if (ret >= 0)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001240 /* fetch the value from the cache */
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001241 *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
1242 codec->driver->reg_word_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001243
1244 kfree(lzo_block->dst);
1245 /* restore the pointer and length of the compressed block */
1246 lzo_block->dst = tmp_dst;
1247 lzo_block->dst_len = tmp_dst_len;
1248 return 0;
1249}
1250
1251static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
1252{
1253 struct snd_soc_lzo_ctx **lzo_blocks;
1254 int i, blkcount;
1255
1256 lzo_blocks = codec->reg_cache;
1257 if (!lzo_blocks)
1258 return 0;
1259
1260 blkcount = snd_soc_lzo_block_count();
1261 /*
1262 * the pointer to the bitmap used for syncing the cache
1263 * is shared amongst all lzo_blocks. Ensure it is freed
1264 * only once.
1265 */
1266 if (lzo_blocks[0])
1267 kfree(lzo_blocks[0]->sync_bmp);
1268 for (i = 0; i < blkcount; ++i) {
1269 if (lzo_blocks[i]) {
1270 kfree(lzo_blocks[i]->wmem);
1271 kfree(lzo_blocks[i]->dst);
1272 }
1273 /* each lzo_block is a pointer returned by kmalloc or NULL */
1274 kfree(lzo_blocks[i]);
1275 }
1276 kfree(lzo_blocks);
1277 codec->reg_cache = NULL;
1278 return 0;
1279}
1280
1281static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
1282{
1283 struct snd_soc_lzo_ctx **lzo_blocks;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001284 size_t bmp_size;
Mark Brown001ae4c2010-12-02 16:21:08 +00001285 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001286 int ret, tofree, i, blksize, blkcount;
1287 const char *p, *end;
1288 unsigned long *sync_bmp;
1289
1290 ret = 0;
1291 codec_drv = codec->driver;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001292
1293 /*
1294 * If we have not been given a default register cache
1295 * then allocate a dummy zero-ed out region, compress it
1296 * and remember to free it afterwards.
1297 */
1298 tofree = 0;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001299 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001300 tofree = 1;
1301
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001302 if (!codec->reg_def_copy) {
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001303 codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001304 if (!codec->reg_def_copy)
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001305 return -ENOMEM;
1306 }
1307
1308 blkcount = snd_soc_lzo_block_count();
1309 codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
1310 GFP_KERNEL);
1311 if (!codec->reg_cache) {
1312 ret = -ENOMEM;
1313 goto err_tofree;
1314 }
1315 lzo_blocks = codec->reg_cache;
1316
1317 /*
1318 * allocate a bitmap to be used when syncing the cache with
1319 * the hardware. Each time a register is modified, the corresponding
1320 * bit is set in the bitmap, so we know that we have to sync
1321 * that register.
1322 */
1323 bmp_size = codec_drv->reg_cache_size;
Dimitris Papastamos465d7fc2010-12-14 15:15:36 +00001324 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001325 GFP_KERNEL);
1326 if (!sync_bmp) {
1327 ret = -ENOMEM;
1328 goto err;
1329 }
Dimitris Papastamos09c74a92010-11-29 11:43:33 +00001330 bitmap_zero(sync_bmp, bmp_size);
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001331
1332 /* allocate the lzo blocks and initialize them */
1333 for (i = 0; i < blkcount; ++i) {
1334 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
1335 GFP_KERNEL);
1336 if (!lzo_blocks[i]) {
1337 kfree(sync_bmp);
1338 ret = -ENOMEM;
1339 goto err;
1340 }
1341 lzo_blocks[i]->sync_bmp = sync_bmp;
1342 lzo_blocks[i]->sync_bmp_nbits = reg_size;
1343 /* alloc the working space for the compressed block */
1344 ret = snd_soc_lzo_prepare(lzo_blocks[i]);
1345 if (ret < 0)
1346 goto err;
1347 }
1348
1349 blksize = snd_soc_lzo_get_blksize(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001350 p = codec->reg_def_copy;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001351 end = codec->reg_def_copy + codec->reg_size;
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001352 /* compress the register map and fill the lzo blocks */
1353 for (i = 0; i < blkcount; ++i, p += blksize) {
1354 lzo_blocks[i]->src = p;
1355 if (p + blksize > end)
1356 lzo_blocks[i]->src_len = end - p;
1357 else
1358 lzo_blocks[i]->src_len = blksize;
1359 ret = snd_soc_lzo_compress_cache_block(codec,
1360 lzo_blocks[i]);
1361 if (ret < 0)
1362 goto err;
1363 lzo_blocks[i]->decompressed_size =
1364 lzo_blocks[i]->src_len;
1365 }
1366
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001367 if (tofree) {
1368 kfree(codec->reg_def_copy);
1369 codec->reg_def_copy = NULL;
1370 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001371 return 0;
1372err:
1373 snd_soc_cache_exit(codec);
1374err_tofree:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00001375 if (tofree) {
1376 kfree(codec->reg_def_copy);
1377 codec->reg_def_copy = NULL;
1378 }
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001379 return ret;
1380}
Mark Brown68d44ee2010-12-21 17:19:56 +00001381#endif
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001382
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001383static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
1384{
1385 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001386 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +00001387 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001388 unsigned int val;
1389
1390 codec_drv = codec->driver;
1391 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001392 ret = snd_soc_cache_read(codec, i, &val);
1393 if (ret)
1394 return ret;
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001395 if (codec->reg_def_copy)
1396 if (snd_soc_get_cache_val(codec->reg_def_copy,
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001397 i, codec_drv->reg_word_size) == val)
1398 continue;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +00001399 ret = snd_soc_write(codec, i, val);
1400 if (ret)
1401 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001402 dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
1403 i, val);
1404 }
1405 return 0;
1406}
1407
1408static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
1409 unsigned int reg, unsigned int value)
1410{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001411 snd_soc_set_cache_val(codec->reg_cache, reg, value,
1412 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001413 return 0;
1414}
1415
1416static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
1417 unsigned int reg, unsigned int *value)
1418{
Dimitris Papastamos1321e882011-01-11 11:29:49 +00001419 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
1420 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001421 return 0;
1422}
1423
1424static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
1425{
1426 if (!codec->reg_cache)
1427 return 0;
1428 kfree(codec->reg_cache);
1429 codec->reg_cache = NULL;
1430 return 0;
1431}
1432
1433static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
1434{
Mark Brown001ae4c2010-12-02 16:21:08 +00001435 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001436
1437 codec_drv = codec->driver;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001438
Dimitris Papastamosd779fce2011-01-12 10:22:28 +00001439 if (codec->reg_def_copy)
1440 codec->reg_cache = kmemdup(codec->reg_def_copy,
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001441 codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001442 else
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00001443 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001444 if (!codec->reg_cache)
1445 return -ENOMEM;
1446
1447 return 0;
1448}
1449
1450/* an array of all supported compression types */
1451static const struct snd_soc_cache_ops cache_types[] = {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001452 /* Flat *must* be the first entry for fallback */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001453 {
Dimitris Papastamosdf0701b2010-11-29 10:54:28 +00001454 .id = SND_SOC_FLAT_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001455 .name = "flat",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001456 .init = snd_soc_flat_cache_init,
1457 .exit = snd_soc_flat_cache_exit,
1458 .read = snd_soc_flat_cache_read,
1459 .write = snd_soc_flat_cache_write,
1460 .sync = snd_soc_flat_cache_sync
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001461 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001462#ifdef CONFIG_SND_SOC_CACHE_LZO
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001463 {
1464 .id = SND_SOC_LZO_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001465 .name = "LZO",
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +00001466 .init = snd_soc_lzo_cache_init,
1467 .exit = snd_soc_lzo_cache_exit,
1468 .read = snd_soc_lzo_cache_read,
1469 .write = snd_soc_lzo_cache_write,
1470 .sync = snd_soc_lzo_cache_sync
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001471 },
Mark Brown68d44ee2010-12-21 17:19:56 +00001472#endif
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001473 {
1474 .id = SND_SOC_RBTREE_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001475 .name = "rbtree",
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +00001476 .init = snd_soc_rbtree_cache_init,
1477 .exit = snd_soc_rbtree_cache_exit,
1478 .read = snd_soc_rbtree_cache_read,
1479 .write = snd_soc_rbtree_cache_write,
1480 .sync = snd_soc_rbtree_cache_sync
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001481 }
1482};
1483
1484int snd_soc_cache_init(struct snd_soc_codec *codec)
1485{
1486 int i;
1487
1488 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00001489 if (cache_types[i].id == codec->compress_type)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001490 break;
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001491
1492 /* Fall back to flat compression */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001493 if (i == ARRAY_SIZE(cache_types)) {
Mark Brownbe4fcdd2010-12-21 17:09:48 +00001494 dev_warn(codec->dev, "Could not match compress type: %d\n",
1495 codec->compress_type);
1496 i = 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001497 }
1498
1499 mutex_init(&codec->cache_rw_mutex);
1500 codec->cache_ops = &cache_types[i];
1501
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001502 if (codec->cache_ops->init) {
1503 if (codec->cache_ops->name)
1504 dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
1505 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001506 return codec->cache_ops->init(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001507 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001508 return -EINVAL;
1509}
1510
1511/*
1512 * NOTE: keep in mind that this function might be called
1513 * multiple times.
1514 */
1515int snd_soc_cache_exit(struct snd_soc_codec *codec)
1516{
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001517 if (codec->cache_ops && codec->cache_ops->exit) {
1518 if (codec->cache_ops->name)
1519 dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
1520 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001521 return codec->cache_ops->exit(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001522 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001523 return -EINVAL;
1524}
1525
1526/**
1527 * snd_soc_cache_read: Fetch the value of a given register from the cache.
1528 *
1529 * @codec: CODEC to configure.
1530 * @reg: The register index.
1531 * @value: The value to be returned.
1532 */
1533int snd_soc_cache_read(struct snd_soc_codec *codec,
1534 unsigned int reg, unsigned int *value)
1535{
1536 int ret;
1537
1538 mutex_lock(&codec->cache_rw_mutex);
1539
1540 if (value && codec->cache_ops && codec->cache_ops->read) {
1541 ret = codec->cache_ops->read(codec, reg, value);
1542 mutex_unlock(&codec->cache_rw_mutex);
1543 return ret;
1544 }
1545
1546 mutex_unlock(&codec->cache_rw_mutex);
1547 return -EINVAL;
1548}
1549EXPORT_SYMBOL_GPL(snd_soc_cache_read);
1550
1551/**
1552 * snd_soc_cache_write: Set the value of a given register in the cache.
1553 *
1554 * @codec: CODEC to configure.
1555 * @reg: The register index.
1556 * @value: The new register value.
1557 */
1558int snd_soc_cache_write(struct snd_soc_codec *codec,
1559 unsigned int reg, unsigned int value)
1560{
1561 int ret;
1562
1563 mutex_lock(&codec->cache_rw_mutex);
1564
1565 if (codec->cache_ops && codec->cache_ops->write) {
1566 ret = codec->cache_ops->write(codec, reg, value);
1567 mutex_unlock(&codec->cache_rw_mutex);
1568 return ret;
1569 }
1570
1571 mutex_unlock(&codec->cache_rw_mutex);
1572 return -EINVAL;
1573}
1574EXPORT_SYMBOL_GPL(snd_soc_cache_write);
1575
1576/**
1577 * snd_soc_cache_sync: Sync the register cache with the hardware.
1578 *
1579 * @codec: CODEC to configure.
1580 *
1581 * Any registers that should not be synced should be marked as
1582 * volatile. In general drivers can choose not to use the provided
1583 * syncing functionality if they so require.
1584 */
1585int snd_soc_cache_sync(struct snd_soc_codec *codec)
1586{
1587 int ret;
1588
1589 if (!codec->cache_sync) {
1590 return 0;
1591 }
1592
1593 if (codec->cache_ops && codec->cache_ops->sync) {
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +00001594 if (codec->cache_ops->name)
1595 dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
1596 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00001597 ret = codec->cache_ops->sync(codec);
1598 if (!ret)
1599 codec->cache_sync = 0;
1600 return ret;
1601 }
1602
1603 return -EINVAL;
1604}
1605EXPORT_SYMBOL_GPL(snd_soc_cache_sync);