Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 1 | /* |
| 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 Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 14 | #include <linux/i2c.h> |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 15 | #include <linux/spi/spi.h> |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 16 | #include <sound/soc.h> |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 17 | #include <linux/lzo.h> |
| 18 | #include <linux/bitmap.h> |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 19 | #include <linux/rbtree.h> |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 20 | |
Dimitris Papastamos | c358e64 | 2011-01-21 15:29:02 +0000 | [diff] [blame] | 21 | #include <trace/events/asoc.h> |
| 22 | |
Dimitris Papastamos | 30539a1 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 23 | #if defined(CONFIG_SPI_MASTER) |
| 24 | static 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 Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 47 | static 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 Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 74 | static unsigned int do_hw_read(struct snd_soc_codec *codec, unsigned int reg) |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 75 | { |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 76 | int ret; |
| 77 | unsigned int val; |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 78 | |
| 79 | if (reg >= codec->driver->reg_cache_size || |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 80 | snd_soc_codec_volatile_register(codec, reg) || |
| 81 | codec->cache_bypass) { |
| 82 | if (codec->cache_only) |
| 83 | return -1; |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 84 | |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 85 | BUG_ON(!codec->hw_read); |
| 86 | return codec->hw_read(codec, reg); |
Dimitris Papastamos | db49c14 | 2010-09-22 13:25:47 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 89 | ret = snd_soc_cache_read(codec, reg, &val); |
| 90 | if (ret < 0) |
| 91 | return -1; |
| 92 | return val; |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 95 | static unsigned int snd_soc_4_12_read(struct snd_soc_codec *codec, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 96 | unsigned int reg) |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 97 | { |
| 98 | return do_hw_read(codec, reg); |
| 99 | } |
| 100 | |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 101 | static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 102 | unsigned int value) |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 103 | { |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 104 | u8 data[2]; |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 105 | |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 106 | data[0] = (reg << 4) | ((value >> 8) & 0x000f); |
| 107 | data[1] = value & 0x00ff; |
| 108 | |
Dimitris Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 109 | return do_hw_write(codec, reg, value, data, 2); |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | #if defined(CONFIG_SPI_MASTER) |
| 113 | static int snd_soc_4_12_spi_write(void *control_data, const char *data, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 114 | int len) |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 115 | { |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 116 | u8 msg[2]; |
| 117 | |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 118 | msg[0] = data[1]; |
| 119 | msg[1] = data[0]; |
| 120 | |
Dimitris Papastamos | 30539a1 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 121 | return do_spi_write(control_data, msg, len); |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 122 | } |
| 123 | #else |
| 124 | #define snd_soc_4_12_spi_write NULL |
| 125 | #endif |
| 126 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 127 | static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec, |
| 128 | unsigned int reg) |
| 129 | { |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 130 | return do_hw_read(codec, reg); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg, |
| 134 | unsigned int value) |
| 135 | { |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 136 | u8 data[2]; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 137 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 138 | data[0] = (reg << 1) | ((value >> 8) & 0x0001); |
| 139 | data[1] = value & 0x00ff; |
| 140 | |
Dimitris Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 141 | return do_hw_write(codec, reg, value, data, 2); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 144 | #if defined(CONFIG_SPI_MASTER) |
| 145 | static int snd_soc_7_9_spi_write(void *control_data, const char *data, |
| 146 | int len) |
| 147 | { |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 148 | u8 msg[2]; |
| 149 | |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 150 | msg[0] = data[0]; |
| 151 | msg[1] = data[1]; |
| 152 | |
Dimitris Papastamos | 30539a1 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 153 | return do_spi_write(control_data, msg, len); |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 154 | } |
| 155 | #else |
| 156 | #define snd_soc_7_9_spi_write NULL |
| 157 | #endif |
| 158 | |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 159 | static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg, |
| 160 | unsigned int value) |
| 161 | { |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 162 | u8 data[2]; |
| 163 | |
Barry Song | f4bee1b | 2010-03-18 16:17:01 +0800 | [diff] [blame] | 164 | reg &= 0xff; |
| 165 | data[0] = reg; |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 166 | data[1] = value & 0xff; |
| 167 | |
Dimitris Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 168 | return do_hw_write(codec, reg, value, data, 2); |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static unsigned int snd_soc_8_8_read(struct snd_soc_codec *codec, |
| 172 | unsigned int reg) |
| 173 | { |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 174 | return do_hw_read(codec, reg); |
Joonyoung Shim | 341c9b8 | 2009-09-07 12:04:37 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 177 | #if defined(CONFIG_SPI_MASTER) |
| 178 | static int snd_soc_8_8_spi_write(void *control_data, const char *data, |
| 179 | int len) |
| 180 | { |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 181 | u8 msg[2]; |
| 182 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 183 | msg[0] = data[0]; |
| 184 | msg[1] = data[1]; |
| 185 | |
Dimitris Papastamos | 30539a1 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 186 | return do_spi_write(control_data, msg, len); |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 187 | } |
| 188 | #else |
| 189 | #define snd_soc_8_8_spi_write NULL |
| 190 | #endif |
| 191 | |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 192 | static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg, |
| 193 | unsigned int value) |
| 194 | { |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 195 | u8 data[3]; |
| 196 | |
| 197 | data[0] = reg; |
| 198 | data[1] = (value >> 8) & 0xff; |
| 199 | data[2] = value & 0xff; |
| 200 | |
Dimitris Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 201 | return do_hw_write(codec, reg, value, data, 3); |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static unsigned int snd_soc_8_16_read(struct snd_soc_codec *codec, |
| 205 | unsigned int reg) |
| 206 | { |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 207 | return do_hw_read(codec, reg); |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 210 | #if defined(CONFIG_SPI_MASTER) |
| 211 | static int snd_soc_8_16_spi_write(void *control_data, const char *data, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 212 | int len) |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 213 | { |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 214 | u8 msg[3]; |
| 215 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 216 | msg[0] = data[0]; |
| 217 | msg[1] = data[1]; |
| 218 | msg[2] = data[2]; |
| 219 | |
Dimitris Papastamos | 30539a1 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 220 | return do_spi_write(control_data, msg, len); |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 221 | } |
| 222 | #else |
| 223 | #define snd_soc_8_16_spi_write NULL |
| 224 | #endif |
| 225 | |
Randy Dunlap | 17244c2 | 2009-08-10 16:04:39 -0700 | [diff] [blame] | 226 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 227 | static unsigned int do_i2c_read(struct snd_soc_codec *codec, |
| 228 | void *reg, int reglen, |
| 229 | void *data, int datalen) |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 230 | { |
| 231 | struct i2c_msg xfer[2]; |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 232 | 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 Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 238 | xfer[0].len = reglen; |
| 239 | xfer[0].buf = reg; |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 240 | |
| 241 | /* Read data */ |
| 242 | xfer[1].addr = client->addr; |
| 243 | xfer[1].flags = I2C_M_RD; |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 244 | xfer[1].len = datalen; |
| 245 | xfer[1].buf = data; |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 246 | |
| 247 | ret = i2c_transfer(client->adapter, xfer, 2); |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 248 | if (ret == 2) |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 249 | return 0; |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 250 | else if (ret < 0) |
| 251 | return ret; |
| 252 | else |
| 253 | return -EIO; |
| 254 | } |
| 255 | #endif |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 256 | |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 257 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
| 258 | static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 259 | unsigned int r) |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 260 | { |
| 261 | u8 reg = r; |
| 262 | u8 data; |
| 263 | int ret; |
| 264 | |
| 265 | ret = do_i2c_read(codec, ®, 1, &data, 1); |
| 266 | if (ret < 0) |
| 267 | return 0; |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 268 | return data; |
| 269 | } |
| 270 | #else |
| 271 | #define snd_soc_8_8_read_i2c NULL |
| 272 | #endif |
| 273 | |
| 274 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 275 | static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec, |
| 276 | unsigned int r) |
| 277 | { |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 278 | u8 reg = r; |
| 279 | u16 data; |
| 280 | int ret; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 281 | |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 282 | ret = do_i2c_read(codec, ®, 1, &data, 2); |
| 283 | if (ret < 0) |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 284 | return 0; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 285 | return (data >> 8) | ((data & 0xff) << 8); |
| 286 | } |
| 287 | #else |
| 288 | #define snd_soc_8_16_read_i2c NULL |
| 289 | #endif |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 290 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 291 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
| 292 | static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec, |
| 293 | unsigned int r) |
| 294 | { |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 295 | u16 reg = r; |
| 296 | u8 data; |
| 297 | int ret; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 298 | |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 299 | ret = do_i2c_read(codec, ®, 2, &data, 1); |
| 300 | if (ret < 0) |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 301 | return 0; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 302 | return data; |
| 303 | } |
| 304 | #else |
| 305 | #define snd_soc_16_8_read_i2c NULL |
| 306 | #endif |
| 307 | |
| 308 | static unsigned int snd_soc_16_8_read(struct snd_soc_codec *codec, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 309 | unsigned int reg) |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 310 | { |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 311 | return do_hw_read(codec, reg); |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 315 | unsigned int value) |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 316 | { |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 317 | u8 data[3]; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 318 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 319 | data[0] = (reg >> 8) & 0xff; |
| 320 | data[1] = reg & 0xff; |
| 321 | data[2] = value; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 322 | reg &= 0xff; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 323 | |
Dimitris Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 324 | return do_hw_write(codec, reg, value, data, 3); |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | #if defined(CONFIG_SPI_MASTER) |
| 328 | static int snd_soc_16_8_spi_write(void *control_data, const char *data, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 329 | int len) |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 330 | { |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 331 | u8 msg[3]; |
| 332 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 333 | msg[0] = data[0]; |
| 334 | msg[1] = data[1]; |
| 335 | msg[2] = data[2]; |
| 336 | |
Dimitris Papastamos | 30539a1 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 337 | return do_spi_write(control_data, msg, len); |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 338 | } |
| 339 | #else |
| 340 | #define snd_soc_16_8_spi_write NULL |
| 341 | #endif |
| 342 | |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 343 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
| 344 | static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec, |
| 345 | unsigned int r) |
| 346 | { |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 347 | u16 reg = cpu_to_be16(r); |
| 348 | u16 data; |
| 349 | int ret; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 350 | |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 351 | ret = do_i2c_read(codec, ®, 2, &data, 2); |
| 352 | if (ret < 0) |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 353 | return 0; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 354 | return be16_to_cpu(data); |
| 355 | } |
| 356 | #else |
| 357 | #define snd_soc_16_16_read_i2c NULL |
| 358 | #endif |
| 359 | |
| 360 | static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec, |
| 361 | unsigned int reg) |
| 362 | { |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 363 | return do_hw_read(codec, reg); |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg, |
| 367 | unsigned int value) |
| 368 | { |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 369 | u8 data[4]; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 370 | |
| 371 | data[0] = (reg >> 8) & 0xff; |
| 372 | data[1] = reg & 0xff; |
| 373 | data[2] = (value >> 8) & 0xff; |
| 374 | data[3] = value & 0xff; |
| 375 | |
Dimitris Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 376 | return do_hw_write(codec, reg, value, data, 4); |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 377 | } |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 378 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 379 | #if defined(CONFIG_SPI_MASTER) |
| 380 | static int snd_soc_16_16_spi_write(void *control_data, const char *data, |
Dimitris Papastamos | fbda182 | 2011-03-28 11:39:14 +0100 | [diff] [blame] | 381 | int len) |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 382 | { |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 383 | u8 msg[4]; |
| 384 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 385 | msg[0] = data[0]; |
| 386 | msg[1] = data[1]; |
| 387 | msg[2] = data[2]; |
| 388 | msg[3] = data[3]; |
| 389 | |
Dimitris Papastamos | 30539a1 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 390 | return do_spi_write(control_data, msg, len); |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 391 | } |
| 392 | #else |
| 393 | #define snd_soc_16_16_spi_write NULL |
| 394 | #endif |
| 395 | |
Mark Brown | 34bad69 | 2011-04-04 17:55:42 +0900 | [diff] [blame] | 396 | /* Primitive bulk write support for soc-cache. The data pointed to by |
| 397 | * `data' needs to already be in the form the hardware expects |
| 398 | * including any leading register specific data. Any data written |
| 399 | * through this function will not go through the cache as it only |
| 400 | * handles writing to volatile or out of bounds registers. |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 401 | */ |
| 402 | static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg, |
| 403 | const void *data, size_t len) |
| 404 | { |
| 405 | int ret; |
| 406 | |
Dimitris Papastamos | 64d2706 | 2011-05-05 14:18:11 +0100 | [diff] [blame] | 407 | /* To ensure that we don't get out of sync with the cache, check |
| 408 | * whether the base register is volatile or if we've directly asked |
| 409 | * to bypass the cache. Out of bounds registers are considered |
| 410 | * volatile. |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 411 | */ |
Dimitris Papastamos | 64d2706 | 2011-05-05 14:18:11 +0100 | [diff] [blame] | 412 | if (!codec->cache_bypass |
| 413 | && !snd_soc_codec_volatile_register(codec, reg) |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 414 | && reg < codec->driver->reg_cache_size) |
| 415 | return -EINVAL; |
| 416 | |
| 417 | switch (codec->control_type) { |
Seungwhan Youn | 898f8b0 | 2011-04-04 13:43:42 +0900 | [diff] [blame] | 418 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 419 | case SND_SOC_I2C: |
| 420 | ret = i2c_master_send(codec->control_data, data, len); |
| 421 | break; |
Seungwhan Youn | 898f8b0 | 2011-04-04 13:43:42 +0900 | [diff] [blame] | 422 | #endif |
| 423 | #if defined(CONFIG_SPI_MASTER) |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 424 | case SND_SOC_SPI: |
| 425 | ret = do_spi_write(codec->control_data, data, len); |
| 426 | break; |
Seungwhan Youn | 898f8b0 | 2011-04-04 13:43:42 +0900 | [diff] [blame] | 427 | #endif |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 428 | default: |
| 429 | BUG(); |
| 430 | } |
| 431 | |
| 432 | if (ret == len) |
| 433 | return 0; |
| 434 | if (ret < 0) |
| 435 | return ret; |
| 436 | else |
| 437 | return -EIO; |
| 438 | } |
| 439 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 440 | static struct { |
| 441 | int addr_bits; |
| 442 | int data_bits; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 443 | int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int); |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 444 | int (*spi_write)(void *, const char *, int); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 445 | unsigned int (*read)(struct snd_soc_codec *, unsigned int); |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 446 | unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 447 | } io_types[] = { |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 448 | { |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 449 | .addr_bits = 4, .data_bits = 12, |
| 450 | .write = snd_soc_4_12_write, .read = snd_soc_4_12_read, |
| 451 | .spi_write = snd_soc_4_12_spi_write, |
| 452 | }, |
| 453 | { |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 454 | .addr_bits = 7, .data_bits = 9, |
| 455 | .write = snd_soc_7_9_write, .read = snd_soc_7_9_read, |
Barry Song | 8998c89 | 2009-12-31 10:30:34 +0800 | [diff] [blame] | 456 | .spi_write = snd_soc_7_9_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 457 | }, |
| 458 | { |
| 459 | .addr_bits = 8, .data_bits = 8, |
| 460 | .write = snd_soc_8_8_write, .read = snd_soc_8_8_read, |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 461 | .i2c_read = snd_soc_8_8_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 462 | .spi_write = snd_soc_8_8_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 463 | }, |
| 464 | { |
| 465 | .addr_bits = 8, .data_bits = 16, |
| 466 | .write = snd_soc_8_16_write, .read = snd_soc_8_16_read, |
| 467 | .i2c_read = snd_soc_8_16_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 468 | .spi_write = snd_soc_8_16_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 469 | }, |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 470 | { |
| 471 | .addr_bits = 16, .data_bits = 8, |
| 472 | .write = snd_soc_16_8_write, .read = snd_soc_16_8_read, |
| 473 | .i2c_read = snd_soc_16_8_read_i2c, |
| 474 | .spi_write = snd_soc_16_8_spi_write, |
| 475 | }, |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 476 | { |
| 477 | .addr_bits = 16, .data_bits = 16, |
| 478 | .write = snd_soc_16_16_write, .read = snd_soc_16_16_read, |
| 479 | .i2c_read = snd_soc_16_16_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 480 | .spi_write = snd_soc_16_16_spi_write, |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 481 | }, |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | /** |
| 485 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
| 486 | * |
| 487 | * @codec: CODEC to configure. |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 488 | * @addr_bits: Number of bits of register address data. |
| 489 | * @data_bits: Number of bits of data per register. |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 490 | * @control: Control bus used. |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 491 | * |
| 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 | */ |
| 503 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 504 | int addr_bits, int data_bits, |
| 505 | enum snd_soc_control_type control) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 506 | { |
| 507 | int i; |
| 508 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 509 | 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 Brown | c3acec2 | 2010-12-02 16:15:29 +0000 | [diff] [blame] | 520 | codec->write = io_types[i].write; |
| 521 | codec->read = io_types[i].read; |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 522 | codec->bulk_write_raw = snd_soc_hw_bulk_write_raw; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 523 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 524 | switch (control) { |
| 525 | case SND_SOC_CUSTOM: |
| 526 | break; |
| 527 | |
| 528 | case SND_SOC_I2C: |
Randy Dunlap | 17244c2 | 2009-08-10 16:04:39 -0700 | [diff] [blame] | 529 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 530 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 531 | #endif |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 532 | if (io_types[i].i2c_read) |
| 533 | codec->hw_read = io_types[i].i2c_read; |
Mark Brown | a6d1434 | 2010-08-12 10:59:15 +0100 | [diff] [blame] | 534 | |
| 535 | codec->control_data = container_of(codec->dev, |
| 536 | struct i2c_client, |
| 537 | dev); |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 538 | break; |
| 539 | |
| 540 | case SND_SOC_SPI: |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 541 | if (io_types[i].spi_write) |
| 542 | codec->hw_write = io_types[i].spi_write; |
Mark Brown | a6d1434 | 2010-08-12 10:59:15 +0100 | [diff] [blame] | 543 | |
| 544 | codec->control_data = container_of(codec->dev, |
| 545 | struct spi_device, |
| 546 | dev); |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 553 | |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 554 | static 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 | |
| 578 | static 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 Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 597 | struct snd_soc_rbtree_node { |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 598 | struct rb_node node; /* the actual rbtree node holding this block */ |
| 599 | unsigned int base_reg; /* base register handled by this block */ |
| 600 | unsigned int word_size; /* number of bytes needed to represent the register index */ |
| 601 | void *block; /* block of adjacent registers */ |
| 602 | unsigned int blklen; /* number of registers available in the block */ |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 603 | } __attribute__ ((packed)); |
| 604 | |
| 605 | struct snd_soc_rbtree_ctx { |
| 606 | struct rb_root root; |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 607 | struct snd_soc_rbtree_node *cached_rbnode; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 608 | }; |
| 609 | |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 610 | static inline void snd_soc_rbtree_get_base_top_reg( |
| 611 | struct snd_soc_rbtree_node *rbnode, |
| 612 | unsigned int *base, unsigned int *top) |
| 613 | { |
| 614 | *base = rbnode->base_reg; |
| 615 | *top = rbnode->base_reg + rbnode->blklen - 1; |
| 616 | } |
| 617 | |
| 618 | static unsigned int snd_soc_rbtree_get_register( |
| 619 | struct snd_soc_rbtree_node *rbnode, unsigned int idx) |
| 620 | { |
| 621 | unsigned int val; |
| 622 | |
| 623 | switch (rbnode->word_size) { |
| 624 | case 1: { |
| 625 | u8 *p = rbnode->block; |
| 626 | val = p[idx]; |
| 627 | return val; |
| 628 | } |
| 629 | case 2: { |
| 630 | u16 *p = rbnode->block; |
| 631 | val = p[idx]; |
| 632 | return val; |
| 633 | } |
| 634 | default: |
| 635 | BUG(); |
| 636 | break; |
| 637 | } |
| 638 | return -1; |
| 639 | } |
| 640 | |
| 641 | static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node *rbnode, |
| 642 | unsigned int idx, unsigned int val) |
| 643 | { |
| 644 | switch (rbnode->word_size) { |
| 645 | case 1: { |
| 646 | u8 *p = rbnode->block; |
| 647 | p[idx] = val; |
| 648 | break; |
| 649 | } |
| 650 | case 2: { |
| 651 | u16 *p = rbnode->block; |
| 652 | p[idx] = val; |
| 653 | break; |
| 654 | } |
| 655 | default: |
| 656 | BUG(); |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 661 | static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup( |
| 662 | struct rb_root *root, unsigned int reg) |
| 663 | { |
| 664 | struct rb_node *node; |
| 665 | struct snd_soc_rbtree_node *rbnode; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 666 | unsigned int base_reg, top_reg; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 667 | |
| 668 | node = root->rb_node; |
| 669 | while (node) { |
| 670 | rbnode = container_of(node, struct snd_soc_rbtree_node, node); |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 671 | snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg); |
| 672 | if (reg >= base_reg && reg <= top_reg) |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 673 | return rbnode; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 674 | else if (reg > top_reg) |
| 675 | node = node->rb_right; |
| 676 | else if (reg < base_reg) |
| 677 | node = node->rb_left; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | return NULL; |
| 681 | } |
| 682 | |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 683 | static int snd_soc_rbtree_insert(struct rb_root *root, |
| 684 | struct snd_soc_rbtree_node *rbnode) |
| 685 | { |
| 686 | struct rb_node **new, *parent; |
| 687 | struct snd_soc_rbtree_node *rbnode_tmp; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 688 | unsigned int base_reg_tmp, top_reg_tmp; |
| 689 | unsigned int base_reg; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 690 | |
| 691 | parent = NULL; |
| 692 | new = &root->rb_node; |
| 693 | while (*new) { |
| 694 | rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node, |
| 695 | node); |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 696 | /* base and top registers of the current rbnode */ |
| 697 | snd_soc_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp, |
| 698 | &top_reg_tmp); |
| 699 | /* base register of the rbnode to be added */ |
| 700 | base_reg = rbnode->base_reg; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 701 | parent = *new; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 702 | /* if this register has already been inserted, just return */ |
| 703 | if (base_reg >= base_reg_tmp && |
| 704 | base_reg <= top_reg_tmp) |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 705 | return 0; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 706 | else if (base_reg > top_reg_tmp) |
| 707 | new = &((*new)->rb_right); |
| 708 | else if (base_reg < base_reg_tmp) |
| 709 | new = &((*new)->rb_left); |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* insert the node into the rbtree */ |
| 713 | rb_link_node(&rbnode->node, parent, new); |
| 714 | rb_insert_color(&rbnode->node, root); |
| 715 | |
| 716 | return 1; |
| 717 | } |
| 718 | |
| 719 | static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec) |
| 720 | { |
| 721 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 722 | struct rb_node *node; |
| 723 | struct snd_soc_rbtree_node *rbnode; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 724 | unsigned int regtmp; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 725 | unsigned int val; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 726 | int ret; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 727 | int i; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 728 | |
| 729 | rbtree_ctx = codec->reg_cache; |
| 730 | for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) { |
| 731 | rbnode = rb_entry(node, struct snd_soc_rbtree_node, node); |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 732 | for (i = 0; i < rbnode->blklen; ++i) { |
| 733 | regtmp = rbnode->base_reg + i; |
| 734 | WARN_ON(codec->writable_register && |
| 735 | codec->writable_register(codec, regtmp)); |
| 736 | val = snd_soc_rbtree_get_register(rbnode, i); |
| 737 | codec->cache_bypass = 1; |
| 738 | ret = snd_soc_write(codec, regtmp, val); |
| 739 | codec->cache_bypass = 0; |
| 740 | if (ret) |
| 741 | return ret; |
| 742 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 743 | regtmp, val); |
| 744 | } |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 750 | static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node *rbnode, |
| 751 | unsigned int pos, unsigned int reg, |
| 752 | unsigned int value) |
| 753 | { |
| 754 | u8 *blk; |
| 755 | |
| 756 | blk = krealloc(rbnode->block, |
| 757 | (rbnode->blklen + 1) * rbnode->word_size, GFP_KERNEL); |
| 758 | if (!blk) |
| 759 | return -ENOMEM; |
| 760 | |
| 761 | /* insert the register value in the correct place in the rbnode block */ |
| 762 | memmove(blk + (pos + 1) * rbnode->word_size, |
| 763 | blk + pos * rbnode->word_size, |
| 764 | (rbnode->blklen - pos) * rbnode->word_size); |
| 765 | |
| 766 | /* update the rbnode block, its size and the base register */ |
| 767 | rbnode->block = blk; |
| 768 | rbnode->blklen++; |
| 769 | if (!pos) |
| 770 | rbnode->base_reg = reg; |
| 771 | |
| 772 | snd_soc_rbtree_set_register(rbnode, pos, value); |
| 773 | return 0; |
| 774 | } |
| 775 | |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 776 | static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec, |
| 777 | unsigned int reg, unsigned int value) |
| 778 | { |
| 779 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 780 | struct snd_soc_rbtree_node *rbnode, *rbnode_tmp; |
| 781 | struct rb_node *node; |
| 782 | unsigned int val; |
| 783 | unsigned int reg_tmp; |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 784 | unsigned int base_reg, top_reg; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 785 | unsigned int pos; |
| 786 | int i; |
| 787 | int ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 788 | |
| 789 | rbtree_ctx = codec->reg_cache; |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 790 | /* look up the required register in the cached rbnode */ |
| 791 | rbnode = rbtree_ctx->cached_rbnode; |
| 792 | if (rbnode) { |
| 793 | snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg); |
| 794 | if (reg >= base_reg && reg <= top_reg) { |
| 795 | reg_tmp = reg - base_reg; |
| 796 | val = snd_soc_rbtree_get_register(rbnode, reg_tmp); |
| 797 | if (val == value) |
| 798 | return 0; |
| 799 | snd_soc_rbtree_set_register(rbnode, reg_tmp, value); |
| 800 | return 0; |
| 801 | } |
| 802 | } |
| 803 | /* if we can't locate it in the cached rbnode we'll have |
| 804 | * to traverse the rbtree looking for it. |
| 805 | */ |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 806 | rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg); |
| 807 | if (rbnode) { |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 808 | reg_tmp = reg - rbnode->base_reg; |
| 809 | val = snd_soc_rbtree_get_register(rbnode, reg_tmp); |
| 810 | if (val == value) |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 811 | return 0; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 812 | snd_soc_rbtree_set_register(rbnode, reg_tmp, value); |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 813 | rbtree_ctx->cached_rbnode = rbnode; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 814 | } else { |
| 815 | /* bail out early, no need to create the rbnode yet */ |
| 816 | if (!value) |
| 817 | return 0; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 818 | /* look for an adjacent register to the one we are about to add */ |
| 819 | for (node = rb_first(&rbtree_ctx->root); node; |
| 820 | node = rb_next(node)) { |
| 821 | rbnode_tmp = rb_entry(node, struct snd_soc_rbtree_node, node); |
| 822 | for (i = 0; i < rbnode_tmp->blklen; ++i) { |
| 823 | reg_tmp = rbnode_tmp->base_reg + i; |
| 824 | if (abs(reg_tmp - reg) != 1) |
| 825 | continue; |
| 826 | /* decide where in the block to place our register */ |
| 827 | if (reg_tmp + 1 == reg) |
| 828 | pos = i + 1; |
| 829 | else |
| 830 | pos = i; |
| 831 | ret = snd_soc_rbtree_insert_to_block(rbnode_tmp, pos, |
| 832 | reg, value); |
| 833 | if (ret) |
| 834 | return ret; |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 835 | rbtree_ctx->cached_rbnode = rbnode_tmp; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 836 | return 0; |
| 837 | } |
| 838 | } |
| 839 | /* we did not manage to find a place to insert it in an existing |
| 840 | * block so create a new rbnode with a single register in its block. |
| 841 | * This block will get populated further if any other adjacent |
| 842 | * registers get modified in the future. |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 843 | */ |
| 844 | rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL); |
| 845 | if (!rbnode) |
| 846 | return -ENOMEM; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 847 | rbnode->blklen = 1; |
| 848 | rbnode->base_reg = reg; |
| 849 | rbnode->word_size = codec->driver->reg_word_size; |
| 850 | rbnode->block = kmalloc(rbnode->blklen * rbnode->word_size, |
| 851 | GFP_KERNEL); |
| 852 | if (!rbnode->block) { |
| 853 | kfree(rbnode); |
| 854 | return -ENOMEM; |
| 855 | } |
| 856 | snd_soc_rbtree_set_register(rbnode, 0, value); |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 857 | snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode); |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 858 | rbtree_ctx->cached_rbnode = rbnode; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec, |
| 865 | unsigned int reg, unsigned int *value) |
| 866 | { |
| 867 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 868 | struct snd_soc_rbtree_node *rbnode; |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 869 | unsigned int base_reg, top_reg; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 870 | unsigned int reg_tmp; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 871 | |
| 872 | rbtree_ctx = codec->reg_cache; |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 873 | /* look up the required register in the cached rbnode */ |
| 874 | rbnode = rbtree_ctx->cached_rbnode; |
| 875 | if (rbnode) { |
| 876 | snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg); |
| 877 | if (reg >= base_reg && reg <= top_reg) { |
| 878 | reg_tmp = reg - base_reg; |
| 879 | *value = snd_soc_rbtree_get_register(rbnode, reg_tmp); |
| 880 | return 0; |
| 881 | } |
| 882 | } |
| 883 | /* if we can't locate it in the cached rbnode we'll have |
| 884 | * to traverse the rbtree looking for it. |
| 885 | */ |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 886 | rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg); |
| 887 | if (rbnode) { |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 888 | reg_tmp = reg - rbnode->base_reg; |
| 889 | *value = snd_soc_rbtree_get_register(rbnode, reg_tmp); |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 890 | rbtree_ctx->cached_rbnode = rbnode; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 891 | } else { |
| 892 | /* uninitialized registers default to 0 */ |
| 893 | *value = 0; |
| 894 | } |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec) |
| 900 | { |
| 901 | struct rb_node *next; |
| 902 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 903 | struct snd_soc_rbtree_node *rbtree_node; |
| 904 | |
| 905 | /* if we've already been called then just return */ |
| 906 | rbtree_ctx = codec->reg_cache; |
| 907 | if (!rbtree_ctx) |
| 908 | return 0; |
| 909 | |
| 910 | /* free up the rbtree */ |
| 911 | next = rb_first(&rbtree_ctx->root); |
| 912 | while (next) { |
| 913 | rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node); |
| 914 | next = rb_next(&rbtree_node->node); |
| 915 | rb_erase(&rbtree_node->node, &rbtree_ctx->root); |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 916 | kfree(rbtree_node->block); |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 917 | kfree(rbtree_node); |
| 918 | } |
| 919 | |
| 920 | /* release the resources */ |
| 921 | kfree(codec->reg_cache); |
| 922 | codec->reg_cache = NULL; |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec) |
| 928 | { |
| 929 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 930 | unsigned int word_size; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 931 | unsigned int val; |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 932 | int i; |
| 933 | int ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 934 | |
| 935 | codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL); |
| 936 | if (!codec->reg_cache) |
| 937 | return -ENOMEM; |
| 938 | |
| 939 | rbtree_ctx = codec->reg_cache; |
| 940 | rbtree_ctx->root = RB_ROOT; |
Dimitris Papastamos | 7e146b5 | 2011-05-19 13:45:30 +0100 | [diff] [blame^] | 941 | rbtree_ctx->cached_rbnode = NULL; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 942 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 943 | if (!codec->reg_def_copy) |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 944 | return 0; |
| 945 | |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 946 | word_size = codec->driver->reg_word_size; |
| 947 | for (i = 0; i < codec->driver->reg_cache_size; ++i) { |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 948 | val = snd_soc_get_cache_val(codec->reg_def_copy, i, |
| 949 | word_size); |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 950 | if (!val) |
| 951 | continue; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 952 | ret = snd_soc_rbtree_cache_write(codec, i, val); |
| 953 | if (ret) |
| 954 | goto err; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | return 0; |
Dimitris Papastamos | 0944cc3 | 2011-05-19 13:45:29 +0100 | [diff] [blame] | 958 | |
| 959 | err: |
| 960 | snd_soc_cache_exit(codec); |
| 961 | return ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 964 | #ifdef CONFIG_SND_SOC_CACHE_LZO |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 965 | struct snd_soc_lzo_ctx { |
| 966 | void *wmem; |
| 967 | void *dst; |
| 968 | const void *src; |
| 969 | size_t src_len; |
| 970 | size_t dst_len; |
| 971 | size_t decompressed_size; |
| 972 | unsigned long *sync_bmp; |
| 973 | int sync_bmp_nbits; |
| 974 | }; |
| 975 | |
| 976 | #define LZO_BLOCK_NUM 8 |
| 977 | static int snd_soc_lzo_block_count(void) |
| 978 | { |
| 979 | return LZO_BLOCK_NUM; |
| 980 | } |
| 981 | |
| 982 | static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx) |
| 983 | { |
| 984 | lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 985 | if (!lzo_ctx->wmem) |
| 986 | return -ENOMEM; |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx) |
| 991 | { |
| 992 | size_t compress_size; |
| 993 | int ret; |
| 994 | |
| 995 | ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len, |
| 996 | lzo_ctx->dst, &compress_size, lzo_ctx->wmem); |
| 997 | if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len) |
| 998 | return -EINVAL; |
| 999 | lzo_ctx->dst_len = compress_size; |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx) |
| 1004 | { |
| 1005 | size_t dst_len; |
| 1006 | int ret; |
| 1007 | |
| 1008 | dst_len = lzo_ctx->dst_len; |
| 1009 | ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len, |
| 1010 | lzo_ctx->dst, &dst_len); |
| 1011 | if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len) |
| 1012 | return -EINVAL; |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec, |
| 1017 | struct snd_soc_lzo_ctx *lzo_ctx) |
| 1018 | { |
| 1019 | int ret; |
| 1020 | |
| 1021 | lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE); |
| 1022 | lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL); |
| 1023 | if (!lzo_ctx->dst) { |
| 1024 | lzo_ctx->dst_len = 0; |
| 1025 | return -ENOMEM; |
| 1026 | } |
| 1027 | |
| 1028 | ret = snd_soc_lzo_compress(lzo_ctx); |
| 1029 | if (ret < 0) |
| 1030 | return ret; |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec, |
| 1035 | struct snd_soc_lzo_ctx *lzo_ctx) |
| 1036 | { |
| 1037 | int ret; |
| 1038 | |
| 1039 | lzo_ctx->dst_len = lzo_ctx->decompressed_size; |
| 1040 | lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL); |
| 1041 | if (!lzo_ctx->dst) { |
| 1042 | lzo_ctx->dst_len = 0; |
| 1043 | return -ENOMEM; |
| 1044 | } |
| 1045 | |
| 1046 | ret = snd_soc_lzo_decompress(lzo_ctx); |
| 1047 | if (ret < 0) |
| 1048 | return ret; |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec, |
| 1053 | unsigned int reg) |
| 1054 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1055 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1056 | |
| 1057 | codec_drv = codec->driver; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1058 | return (reg * codec_drv->reg_word_size) / |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1059 | DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec, |
| 1063 | unsigned int reg) |
| 1064 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1065 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1066 | |
| 1067 | codec_drv = codec->driver; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1068 | return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) / |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1069 | codec_drv->reg_word_size); |
| 1070 | } |
| 1071 | |
| 1072 | static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec) |
| 1073 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1074 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1075 | |
| 1076 | codec_drv = codec->driver; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1077 | return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec) |
| 1081 | { |
| 1082 | struct snd_soc_lzo_ctx **lzo_blocks; |
| 1083 | unsigned int val; |
| 1084 | int i; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1085 | int ret; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1086 | |
| 1087 | lzo_blocks = codec->reg_cache; |
| 1088 | for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) { |
Dimitris Papastamos | f20eda5 | 2011-03-28 11:39:15 +0100 | [diff] [blame] | 1089 | WARN_ON(codec->writable_register && |
| 1090 | codec->writable_register(codec, i)); |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1091 | ret = snd_soc_cache_read(codec, i, &val); |
| 1092 | if (ret) |
| 1093 | return ret; |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame] | 1094 | codec->cache_bypass = 1; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1095 | ret = snd_soc_write(codec, i, val); |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame] | 1096 | codec->cache_bypass = 0; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1097 | if (ret) |
| 1098 | return ret; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1099 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 1100 | i, val); |
| 1101 | } |
| 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec, |
| 1107 | unsigned int reg, unsigned int value) |
| 1108 | { |
| 1109 | struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks; |
| 1110 | int ret, blkindex, blkpos; |
| 1111 | size_t blksize, tmp_dst_len; |
| 1112 | void *tmp_dst; |
| 1113 | |
| 1114 | /* index of the compressed lzo block */ |
| 1115 | blkindex = snd_soc_lzo_get_blkindex(codec, reg); |
| 1116 | /* register index within the decompressed block */ |
| 1117 | blkpos = snd_soc_lzo_get_blkpos(codec, reg); |
| 1118 | /* size of the compressed block */ |
| 1119 | blksize = snd_soc_lzo_get_blksize(codec); |
| 1120 | lzo_blocks = codec->reg_cache; |
| 1121 | lzo_block = lzo_blocks[blkindex]; |
| 1122 | |
| 1123 | /* save the pointer and length of the compressed block */ |
| 1124 | tmp_dst = lzo_block->dst; |
| 1125 | tmp_dst_len = lzo_block->dst_len; |
| 1126 | |
| 1127 | /* prepare the source to be the compressed block */ |
| 1128 | lzo_block->src = lzo_block->dst; |
| 1129 | lzo_block->src_len = lzo_block->dst_len; |
| 1130 | |
| 1131 | /* decompress the block */ |
| 1132 | ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block); |
| 1133 | if (ret < 0) { |
| 1134 | kfree(lzo_block->dst); |
| 1135 | goto out; |
| 1136 | } |
| 1137 | |
| 1138 | /* write the new value to the cache */ |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1139 | if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value, |
| 1140 | codec->driver->reg_word_size)) { |
| 1141 | kfree(lzo_block->dst); |
| 1142 | goto out; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /* prepare the source to be the decompressed block */ |
| 1146 | lzo_block->src = lzo_block->dst; |
| 1147 | lzo_block->src_len = lzo_block->dst_len; |
| 1148 | |
| 1149 | /* compress the block */ |
| 1150 | ret = snd_soc_lzo_compress_cache_block(codec, lzo_block); |
| 1151 | if (ret < 0) { |
| 1152 | kfree(lzo_block->dst); |
| 1153 | kfree(lzo_block->src); |
| 1154 | goto out; |
| 1155 | } |
| 1156 | |
| 1157 | /* set the bit so we know we have to sync this register */ |
| 1158 | set_bit(reg, lzo_block->sync_bmp); |
| 1159 | kfree(tmp_dst); |
| 1160 | kfree(lzo_block->src); |
| 1161 | return 0; |
| 1162 | out: |
| 1163 | lzo_block->dst = tmp_dst; |
| 1164 | lzo_block->dst_len = tmp_dst_len; |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec, |
| 1169 | unsigned int reg, unsigned int *value) |
| 1170 | { |
| 1171 | struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks; |
| 1172 | int ret, blkindex, blkpos; |
| 1173 | size_t blksize, tmp_dst_len; |
| 1174 | void *tmp_dst; |
| 1175 | |
| 1176 | *value = 0; |
| 1177 | /* index of the compressed lzo block */ |
| 1178 | blkindex = snd_soc_lzo_get_blkindex(codec, reg); |
| 1179 | /* register index within the decompressed block */ |
| 1180 | blkpos = snd_soc_lzo_get_blkpos(codec, reg); |
| 1181 | /* size of the compressed block */ |
| 1182 | blksize = snd_soc_lzo_get_blksize(codec); |
| 1183 | lzo_blocks = codec->reg_cache; |
| 1184 | lzo_block = lzo_blocks[blkindex]; |
| 1185 | |
| 1186 | /* save the pointer and length of the compressed block */ |
| 1187 | tmp_dst = lzo_block->dst; |
| 1188 | tmp_dst_len = lzo_block->dst_len; |
| 1189 | |
| 1190 | /* prepare the source to be the compressed block */ |
| 1191 | lzo_block->src = lzo_block->dst; |
| 1192 | lzo_block->src_len = lzo_block->dst_len; |
| 1193 | |
| 1194 | /* decompress the block */ |
| 1195 | ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block); |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1196 | if (ret >= 0) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1197 | /* fetch the value from the cache */ |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1198 | *value = snd_soc_get_cache_val(lzo_block->dst, blkpos, |
| 1199 | codec->driver->reg_word_size); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1200 | |
| 1201 | kfree(lzo_block->dst); |
| 1202 | /* restore the pointer and length of the compressed block */ |
| 1203 | lzo_block->dst = tmp_dst; |
| 1204 | lzo_block->dst_len = tmp_dst_len; |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec) |
| 1209 | { |
| 1210 | struct snd_soc_lzo_ctx **lzo_blocks; |
| 1211 | int i, blkcount; |
| 1212 | |
| 1213 | lzo_blocks = codec->reg_cache; |
| 1214 | if (!lzo_blocks) |
| 1215 | return 0; |
| 1216 | |
| 1217 | blkcount = snd_soc_lzo_block_count(); |
| 1218 | /* |
| 1219 | * the pointer to the bitmap used for syncing the cache |
| 1220 | * is shared amongst all lzo_blocks. Ensure it is freed |
| 1221 | * only once. |
| 1222 | */ |
| 1223 | if (lzo_blocks[0]) |
| 1224 | kfree(lzo_blocks[0]->sync_bmp); |
| 1225 | for (i = 0; i < blkcount; ++i) { |
| 1226 | if (lzo_blocks[i]) { |
| 1227 | kfree(lzo_blocks[i]->wmem); |
| 1228 | kfree(lzo_blocks[i]->dst); |
| 1229 | } |
| 1230 | /* each lzo_block is a pointer returned by kmalloc or NULL */ |
| 1231 | kfree(lzo_blocks[i]); |
| 1232 | } |
| 1233 | kfree(lzo_blocks); |
| 1234 | codec->reg_cache = NULL; |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec) |
| 1239 | { |
| 1240 | struct snd_soc_lzo_ctx **lzo_blocks; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1241 | size_t bmp_size; |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1242 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1243 | int ret, tofree, i, blksize, blkcount; |
| 1244 | const char *p, *end; |
| 1245 | unsigned long *sync_bmp; |
| 1246 | |
| 1247 | ret = 0; |
| 1248 | codec_drv = codec->driver; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1249 | |
| 1250 | /* |
| 1251 | * If we have not been given a default register cache |
| 1252 | * then allocate a dummy zero-ed out region, compress it |
| 1253 | * and remember to free it afterwards. |
| 1254 | */ |
| 1255 | tofree = 0; |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1256 | if (!codec->reg_def_copy) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1257 | tofree = 1; |
| 1258 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1259 | if (!codec->reg_def_copy) { |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1260 | codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1261 | if (!codec->reg_def_copy) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1262 | return -ENOMEM; |
| 1263 | } |
| 1264 | |
| 1265 | blkcount = snd_soc_lzo_block_count(); |
| 1266 | codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks, |
| 1267 | GFP_KERNEL); |
| 1268 | if (!codec->reg_cache) { |
| 1269 | ret = -ENOMEM; |
| 1270 | goto err_tofree; |
| 1271 | } |
| 1272 | lzo_blocks = codec->reg_cache; |
| 1273 | |
| 1274 | /* |
| 1275 | * allocate a bitmap to be used when syncing the cache with |
| 1276 | * the hardware. Each time a register is modified, the corresponding |
| 1277 | * bit is set in the bitmap, so we know that we have to sync |
| 1278 | * that register. |
| 1279 | */ |
| 1280 | bmp_size = codec_drv->reg_cache_size; |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 1281 | sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long), |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1282 | GFP_KERNEL); |
| 1283 | if (!sync_bmp) { |
| 1284 | ret = -ENOMEM; |
| 1285 | goto err; |
| 1286 | } |
Dimitris Papastamos | 09c74a9 | 2010-11-29 11:43:33 +0000 | [diff] [blame] | 1287 | bitmap_zero(sync_bmp, bmp_size); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1288 | |
| 1289 | /* allocate the lzo blocks and initialize them */ |
| 1290 | for (i = 0; i < blkcount; ++i) { |
| 1291 | lzo_blocks[i] = kzalloc(sizeof **lzo_blocks, |
| 1292 | GFP_KERNEL); |
| 1293 | if (!lzo_blocks[i]) { |
| 1294 | kfree(sync_bmp); |
| 1295 | ret = -ENOMEM; |
| 1296 | goto err; |
| 1297 | } |
| 1298 | lzo_blocks[i]->sync_bmp = sync_bmp; |
Dimitris Papastamos | 04f8fd1 | 2011-01-11 11:24:02 +0000 | [diff] [blame] | 1299 | lzo_blocks[i]->sync_bmp_nbits = bmp_size; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1300 | /* alloc the working space for the compressed block */ |
| 1301 | ret = snd_soc_lzo_prepare(lzo_blocks[i]); |
| 1302 | if (ret < 0) |
| 1303 | goto err; |
| 1304 | } |
| 1305 | |
| 1306 | blksize = snd_soc_lzo_get_blksize(codec); |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1307 | p = codec->reg_def_copy; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1308 | end = codec->reg_def_copy + codec->reg_size; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1309 | /* compress the register map and fill the lzo blocks */ |
| 1310 | for (i = 0; i < blkcount; ++i, p += blksize) { |
| 1311 | lzo_blocks[i]->src = p; |
| 1312 | if (p + blksize > end) |
| 1313 | lzo_blocks[i]->src_len = end - p; |
| 1314 | else |
| 1315 | lzo_blocks[i]->src_len = blksize; |
| 1316 | ret = snd_soc_lzo_compress_cache_block(codec, |
| 1317 | lzo_blocks[i]); |
| 1318 | if (ret < 0) |
| 1319 | goto err; |
| 1320 | lzo_blocks[i]->decompressed_size = |
| 1321 | lzo_blocks[i]->src_len; |
| 1322 | } |
| 1323 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1324 | if (tofree) { |
| 1325 | kfree(codec->reg_def_copy); |
| 1326 | codec->reg_def_copy = NULL; |
| 1327 | } |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1328 | return 0; |
| 1329 | err: |
| 1330 | snd_soc_cache_exit(codec); |
| 1331 | err_tofree: |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1332 | if (tofree) { |
| 1333 | kfree(codec->reg_def_copy); |
| 1334 | codec->reg_def_copy = NULL; |
| 1335 | } |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1336 | return ret; |
| 1337 | } |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1338 | #endif |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1339 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1340 | static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec) |
| 1341 | { |
| 1342 | int i; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1343 | int ret; |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1344 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1345 | unsigned int val; |
| 1346 | |
| 1347 | codec_drv = codec->driver; |
| 1348 | for (i = 0; i < codec_drv->reg_cache_size; ++i) { |
Dimitris Papastamos | f20eda5 | 2011-03-28 11:39:15 +0100 | [diff] [blame] | 1349 | WARN_ON(codec->writable_register && |
| 1350 | codec->writable_register(codec, i)); |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1351 | ret = snd_soc_cache_read(codec, i, &val); |
| 1352 | if (ret) |
| 1353 | return ret; |
Dimitris Papastamos | d779fce | 2011-01-12 10:22:28 +0000 | [diff] [blame] | 1354 | if (codec->reg_def_copy) |
| 1355 | if (snd_soc_get_cache_val(codec->reg_def_copy, |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1356 | i, codec_drv->reg_word_size) == val) |
| 1357 | continue; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1358 | ret = snd_soc_write(codec, i, val); |
| 1359 | if (ret) |
| 1360 | return ret; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1361 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 1362 | i, val); |
| 1363 | } |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | static int snd_soc_flat_cache_write(struct snd_soc_codec *codec, |
| 1368 | unsigned int reg, unsigned int value) |
| 1369 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1370 | snd_soc_set_cache_val(codec->reg_cache, reg, value, |
| 1371 | codec->driver->reg_word_size); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | static int snd_soc_flat_cache_read(struct snd_soc_codec *codec, |
| 1376 | unsigned int reg, unsigned int *value) |
| 1377 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1378 | *value = snd_soc_get_cache_val(codec->reg_cache, reg, |
| 1379 | codec->driver->reg_word_size); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | |
| 1383 | static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec) |
| 1384 | { |
| 1385 | if (!codec->reg_cache) |
| 1386 | return 0; |
| 1387 | kfree(codec->reg_cache); |
| 1388 | codec->reg_cache = NULL; |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | static int snd_soc_flat_cache_init(struct snd_soc_codec *codec) |
| 1393 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1394 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1395 | |
| 1396 | codec_drv = codec->driver; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1397 | |
Dimitris Papastamos | d779fce | 2011-01-12 10:22:28 +0000 | [diff] [blame] | 1398 | if (codec->reg_def_copy) |
| 1399 | codec->reg_cache = kmemdup(codec->reg_def_copy, |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1400 | codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1401 | else |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1402 | codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1403 | if (!codec->reg_cache) |
| 1404 | return -ENOMEM; |
| 1405 | |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | /* an array of all supported compression types */ |
| 1410 | static const struct snd_soc_cache_ops cache_types[] = { |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1411 | /* Flat *must* be the first entry for fallback */ |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1412 | { |
Dimitris Papastamos | df0701b | 2010-11-29 10:54:28 +0000 | [diff] [blame] | 1413 | .id = SND_SOC_FLAT_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1414 | .name = "flat", |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1415 | .init = snd_soc_flat_cache_init, |
| 1416 | .exit = snd_soc_flat_cache_exit, |
| 1417 | .read = snd_soc_flat_cache_read, |
| 1418 | .write = snd_soc_flat_cache_write, |
| 1419 | .sync = snd_soc_flat_cache_sync |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1420 | }, |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1421 | #ifdef CONFIG_SND_SOC_CACHE_LZO |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1422 | { |
| 1423 | .id = SND_SOC_LZO_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1424 | .name = "LZO", |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1425 | .init = snd_soc_lzo_cache_init, |
| 1426 | .exit = snd_soc_lzo_cache_exit, |
| 1427 | .read = snd_soc_lzo_cache_read, |
| 1428 | .write = snd_soc_lzo_cache_write, |
| 1429 | .sync = snd_soc_lzo_cache_sync |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1430 | }, |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1431 | #endif |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1432 | { |
| 1433 | .id = SND_SOC_RBTREE_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1434 | .name = "rbtree", |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1435 | .init = snd_soc_rbtree_cache_init, |
| 1436 | .exit = snd_soc_rbtree_cache_exit, |
| 1437 | .read = snd_soc_rbtree_cache_read, |
| 1438 | .write = snd_soc_rbtree_cache_write, |
| 1439 | .sync = snd_soc_rbtree_cache_sync |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1440 | } |
| 1441 | }; |
| 1442 | |
| 1443 | int snd_soc_cache_init(struct snd_soc_codec *codec) |
| 1444 | { |
| 1445 | int i; |
| 1446 | |
| 1447 | for (i = 0; i < ARRAY_SIZE(cache_types); ++i) |
Dimitris Papastamos | 23bbce3 | 2010-12-02 14:53:01 +0000 | [diff] [blame] | 1448 | if (cache_types[i].id == codec->compress_type) |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1449 | break; |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1450 | |
| 1451 | /* Fall back to flat compression */ |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1452 | if (i == ARRAY_SIZE(cache_types)) { |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1453 | dev_warn(codec->dev, "Could not match compress type: %d\n", |
| 1454 | codec->compress_type); |
| 1455 | i = 0; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | mutex_init(&codec->cache_rw_mutex); |
| 1459 | codec->cache_ops = &cache_types[i]; |
| 1460 | |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1461 | if (codec->cache_ops->init) { |
| 1462 | if (codec->cache_ops->name) |
| 1463 | dev_dbg(codec->dev, "Initializing %s cache for %s codec\n", |
| 1464 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1465 | return codec->cache_ops->init(codec); |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1466 | } |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1467 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /* |
| 1471 | * NOTE: keep in mind that this function might be called |
| 1472 | * multiple times. |
| 1473 | */ |
| 1474 | int snd_soc_cache_exit(struct snd_soc_codec *codec) |
| 1475 | { |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1476 | if (codec->cache_ops && codec->cache_ops->exit) { |
| 1477 | if (codec->cache_ops->name) |
| 1478 | dev_dbg(codec->dev, "Destroying %s cache for %s codec\n", |
| 1479 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1480 | return codec->cache_ops->exit(codec); |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1481 | } |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1482 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | /** |
| 1486 | * snd_soc_cache_read: Fetch the value of a given register from the cache. |
| 1487 | * |
| 1488 | * @codec: CODEC to configure. |
| 1489 | * @reg: The register index. |
| 1490 | * @value: The value to be returned. |
| 1491 | */ |
| 1492 | int snd_soc_cache_read(struct snd_soc_codec *codec, |
| 1493 | unsigned int reg, unsigned int *value) |
| 1494 | { |
| 1495 | int ret; |
| 1496 | |
| 1497 | mutex_lock(&codec->cache_rw_mutex); |
| 1498 | |
| 1499 | if (value && codec->cache_ops && codec->cache_ops->read) { |
| 1500 | ret = codec->cache_ops->read(codec, reg, value); |
| 1501 | mutex_unlock(&codec->cache_rw_mutex); |
| 1502 | return ret; |
| 1503 | } |
| 1504 | |
| 1505 | mutex_unlock(&codec->cache_rw_mutex); |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1506 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1507 | } |
| 1508 | EXPORT_SYMBOL_GPL(snd_soc_cache_read); |
| 1509 | |
| 1510 | /** |
| 1511 | * snd_soc_cache_write: Set the value of a given register in the cache. |
| 1512 | * |
| 1513 | * @codec: CODEC to configure. |
| 1514 | * @reg: The register index. |
| 1515 | * @value: The new register value. |
| 1516 | */ |
| 1517 | int snd_soc_cache_write(struct snd_soc_codec *codec, |
| 1518 | unsigned int reg, unsigned int value) |
| 1519 | { |
| 1520 | int ret; |
| 1521 | |
| 1522 | mutex_lock(&codec->cache_rw_mutex); |
| 1523 | |
| 1524 | if (codec->cache_ops && codec->cache_ops->write) { |
| 1525 | ret = codec->cache_ops->write(codec, reg, value); |
| 1526 | mutex_unlock(&codec->cache_rw_mutex); |
| 1527 | return ret; |
| 1528 | } |
| 1529 | |
| 1530 | mutex_unlock(&codec->cache_rw_mutex); |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1531 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1532 | } |
| 1533 | EXPORT_SYMBOL_GPL(snd_soc_cache_write); |
| 1534 | |
| 1535 | /** |
| 1536 | * snd_soc_cache_sync: Sync the register cache with the hardware. |
| 1537 | * |
| 1538 | * @codec: CODEC to configure. |
| 1539 | * |
| 1540 | * Any registers that should not be synced should be marked as |
| 1541 | * volatile. In general drivers can choose not to use the provided |
| 1542 | * syncing functionality if they so require. |
| 1543 | */ |
| 1544 | int snd_soc_cache_sync(struct snd_soc_codec *codec) |
| 1545 | { |
| 1546 | int ret; |
Dimitris Papastamos | c358e64 | 2011-01-21 15:29:02 +0000 | [diff] [blame] | 1547 | const char *name; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1548 | |
| 1549 | if (!codec->cache_sync) { |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 1553 | if (!codec->cache_ops || !codec->cache_ops->sync) |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1554 | return -ENOSYS; |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 1555 | |
Dimitris Papastamos | c358e64 | 2011-01-21 15:29:02 +0000 | [diff] [blame] | 1556 | if (codec->cache_ops->name) |
| 1557 | name = codec->cache_ops->name; |
| 1558 | else |
| 1559 | name = "unknown"; |
| 1560 | |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 1561 | if (codec->cache_ops->name) |
| 1562 | dev_dbg(codec->dev, "Syncing %s cache for %s codec\n", |
| 1563 | codec->cache_ops->name, codec->name); |
| 1564 | trace_snd_soc_cache_sync(codec, name, "start"); |
| 1565 | ret = codec->cache_ops->sync(codec); |
| 1566 | if (!ret) |
| 1567 | codec->cache_sync = 0; |
| 1568 | trace_snd_soc_cache_sync(codec, name, "end"); |
| 1569 | return ret; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1570 | } |
| 1571 | EXPORT_SYMBOL_GPL(snd_soc_cache_sync); |
Dimitris Papastamos | 066d16c | 2011-01-13 12:20:36 +0000 | [diff] [blame] | 1572 | |
| 1573 | static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec, |
| 1574 | unsigned int reg) |
| 1575 | { |
| 1576 | const struct snd_soc_codec_driver *codec_drv; |
| 1577 | unsigned int min, max, index; |
| 1578 | |
| 1579 | codec_drv = codec->driver; |
| 1580 | min = 0; |
| 1581 | max = codec_drv->reg_access_size - 1; |
| 1582 | do { |
| 1583 | index = (min + max) / 2; |
| 1584 | if (codec_drv->reg_access_default[index].reg == reg) |
| 1585 | return index; |
| 1586 | if (codec_drv->reg_access_default[index].reg < reg) |
| 1587 | min = index + 1; |
| 1588 | else |
| 1589 | max = index; |
| 1590 | } while (min <= max); |
| 1591 | return -1; |
| 1592 | } |
| 1593 | |
| 1594 | int snd_soc_default_volatile_register(struct snd_soc_codec *codec, |
| 1595 | unsigned int reg) |
| 1596 | { |
| 1597 | int index; |
| 1598 | |
| 1599 | if (reg >= codec->driver->reg_cache_size) |
| 1600 | return 1; |
| 1601 | index = snd_soc_get_reg_access_index(codec, reg); |
| 1602 | if (index < 0) |
| 1603 | return 0; |
| 1604 | return codec->driver->reg_access_default[index].vol; |
| 1605 | } |
| 1606 | EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register); |
| 1607 | |
| 1608 | int snd_soc_default_readable_register(struct snd_soc_codec *codec, |
| 1609 | unsigned int reg) |
| 1610 | { |
| 1611 | int index; |
| 1612 | |
| 1613 | if (reg >= codec->driver->reg_cache_size) |
| 1614 | return 1; |
| 1615 | index = snd_soc_get_reg_access_index(codec, reg); |
| 1616 | if (index < 0) |
| 1617 | return 0; |
| 1618 | return codec->driver->reg_access_default[index].read; |
| 1619 | } |
| 1620 | EXPORT_SYMBOL_GPL(snd_soc_default_readable_register); |
Dimitris Papastamos | 8020454 | 2011-03-24 13:45:17 +0000 | [diff] [blame] | 1621 | |
| 1622 | int snd_soc_default_writable_register(struct snd_soc_codec *codec, |
| 1623 | unsigned int reg) |
| 1624 | { |
| 1625 | int index; |
| 1626 | |
| 1627 | if (reg >= codec->driver->reg_cache_size) |
| 1628 | return 1; |
| 1629 | index = snd_soc_get_reg_access_index(codec, reg); |
| 1630 | if (index < 0) |
| 1631 | return 0; |
| 1632 | return codec->driver->reg_access_default[index].write; |
| 1633 | } |
| 1634 | EXPORT_SYMBOL_GPL(snd_soc_default_writable_register); |