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 | 30539a18 | 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 | 30539a18 | 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 | 30539a18 | 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 | 30539a18 | 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 | 30539a18 | 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; |
Mark Brown | 8c961bc | 2010-02-01 18:46:10 +0000 | [diff] [blame] | 322 | |
Dimitris Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 323 | return do_hw_write(codec, reg, value, data, 3); |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | #if defined(CONFIG_SPI_MASTER) |
| 327 | 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] | 328 | int len) |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 329 | { |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 330 | u8 msg[3]; |
| 331 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 332 | msg[0] = data[0]; |
| 333 | msg[1] = data[1]; |
| 334 | msg[2] = data[2]; |
| 335 | |
Dimitris Papastamos | 30539a18 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 336 | return do_spi_write(control_data, msg, len); |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 337 | } |
| 338 | #else |
| 339 | #define snd_soc_16_8_spi_write NULL |
| 340 | #endif |
| 341 | |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 342 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
| 343 | static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec, |
| 344 | unsigned int r) |
| 345 | { |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 346 | u16 reg = cpu_to_be16(r); |
| 347 | u16 data; |
| 348 | int ret; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 349 | |
Dimitris Papastamos | f3594f5 | 2011-03-22 10:37:01 +0000 | [diff] [blame] | 350 | ret = do_i2c_read(codec, ®, 2, &data, 2); |
| 351 | if (ret < 0) |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 352 | return 0; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 353 | return be16_to_cpu(data); |
| 354 | } |
| 355 | #else |
| 356 | #define snd_soc_16_16_read_i2c NULL |
| 357 | #endif |
| 358 | |
| 359 | static unsigned int snd_soc_16_16_read(struct snd_soc_codec *codec, |
| 360 | unsigned int reg) |
| 361 | { |
Dimitris Papastamos | b8cbc19 | 2011-03-22 10:36:59 +0000 | [diff] [blame] | 362 | return do_hw_read(codec, reg); |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg, |
| 366 | unsigned int value) |
| 367 | { |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 368 | u8 data[4]; |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 369 | |
| 370 | data[0] = (reg >> 8) & 0xff; |
| 371 | data[1] = reg & 0xff; |
| 372 | data[2] = (value >> 8) & 0xff; |
| 373 | data[3] = value & 0xff; |
| 374 | |
Dimitris Papastamos | 26e9984 | 2011-03-22 10:36:58 +0000 | [diff] [blame] | 375 | return do_hw_write(codec, reg, value, data, 4); |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 376 | } |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 377 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 378 | #if defined(CONFIG_SPI_MASTER) |
| 379 | 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] | 380 | int len) |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 381 | { |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 382 | u8 msg[4]; |
| 383 | |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 384 | msg[0] = data[0]; |
| 385 | msg[1] = data[1]; |
| 386 | msg[2] = data[2]; |
| 387 | msg[3] = data[3]; |
| 388 | |
Dimitris Papastamos | 30539a18 | 2011-03-22 10:37:00 +0000 | [diff] [blame] | 389 | return do_spi_write(control_data, msg, len); |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 390 | } |
| 391 | #else |
| 392 | #define snd_soc_16_16_spi_write NULL |
| 393 | #endif |
| 394 | |
Mark Brown | 34bad69 | 2011-04-04 17:55:42 +0900 | [diff] [blame] | 395 | /* Primitive bulk write support for soc-cache. The data pointed to by |
| 396 | * `data' needs to already be in the form the hardware expects |
| 397 | * including any leading register specific data. Any data written |
| 398 | * through this function will not go through the cache as it only |
| 399 | * handles writing to volatile or out of bounds registers. |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 400 | */ |
| 401 | static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg, |
| 402 | const void *data, size_t len) |
| 403 | { |
| 404 | int ret; |
| 405 | |
Dimitris Papastamos | 64d2706 | 2011-05-05 14:18:11 +0100 | [diff] [blame] | 406 | /* To ensure that we don't get out of sync with the cache, check |
| 407 | * whether the base register is volatile or if we've directly asked |
| 408 | * to bypass the cache. Out of bounds registers are considered |
| 409 | * volatile. |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 410 | */ |
Dimitris Papastamos | 64d2706 | 2011-05-05 14:18:11 +0100 | [diff] [blame] | 411 | if (!codec->cache_bypass |
| 412 | && !snd_soc_codec_volatile_register(codec, reg) |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 413 | && reg < codec->driver->reg_cache_size) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | switch (codec->control_type) { |
Seungwhan Youn | 898f8b0 | 2011-04-04 13:43:42 +0900 | [diff] [blame] | 417 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 418 | case SND_SOC_I2C: |
| 419 | ret = i2c_master_send(codec->control_data, data, len); |
| 420 | break; |
Seungwhan Youn | 898f8b0 | 2011-04-04 13:43:42 +0900 | [diff] [blame] | 421 | #endif |
| 422 | #if defined(CONFIG_SPI_MASTER) |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 423 | case SND_SOC_SPI: |
| 424 | ret = do_spi_write(codec->control_data, data, len); |
| 425 | break; |
Seungwhan Youn | 898f8b0 | 2011-04-04 13:43:42 +0900 | [diff] [blame] | 426 | #endif |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 427 | default: |
| 428 | BUG(); |
| 429 | } |
| 430 | |
| 431 | if (ret == len) |
| 432 | return 0; |
| 433 | if (ret < 0) |
| 434 | return ret; |
| 435 | else |
| 436 | return -EIO; |
| 437 | } |
| 438 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 439 | static struct { |
| 440 | int addr_bits; |
| 441 | int data_bits; |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 442 | int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int); |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 443 | int (*spi_write)(void *, const char *, int); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 444 | unsigned int (*read)(struct snd_soc_codec *, unsigned int); |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 445 | unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 446 | } io_types[] = { |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 447 | { |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 448 | .addr_bits = 4, .data_bits = 12, |
| 449 | .write = snd_soc_4_12_write, .read = snd_soc_4_12_read, |
| 450 | .spi_write = snd_soc_4_12_spi_write, |
| 451 | }, |
| 452 | { |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 453 | .addr_bits = 7, .data_bits = 9, |
| 454 | .write = snd_soc_7_9_write, .read = snd_soc_7_9_read, |
Barry Song | 8998c89 | 2009-12-31 10:30:34 +0800 | [diff] [blame] | 455 | .spi_write = snd_soc_7_9_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 456 | }, |
| 457 | { |
| 458 | .addr_bits = 8, .data_bits = 8, |
| 459 | .write = snd_soc_8_8_write, .read = snd_soc_8_8_read, |
Cliff Cai | 85dfcdf | 2010-03-18 16:17:00 +0800 | [diff] [blame] | 460 | .i2c_read = snd_soc_8_8_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 461 | .spi_write = snd_soc_8_8_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 462 | }, |
| 463 | { |
| 464 | .addr_bits = 8, .data_bits = 16, |
| 465 | .write = snd_soc_8_16_write, .read = snd_soc_8_16_read, |
| 466 | .i2c_read = snd_soc_8_16_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 467 | .spi_write = snd_soc_8_16_spi_write, |
Mark Brown | d62ab35 | 2009-09-21 04:21:47 -0700 | [diff] [blame] | 468 | }, |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 469 | { |
| 470 | .addr_bits = 16, .data_bits = 8, |
| 471 | .write = snd_soc_16_8_write, .read = snd_soc_16_8_read, |
| 472 | .i2c_read = snd_soc_16_8_read_i2c, |
| 473 | .spi_write = snd_soc_16_8_spi_write, |
| 474 | }, |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 475 | { |
| 476 | .addr_bits = 16, .data_bits = 16, |
| 477 | .write = snd_soc_16_16_write, .read = snd_soc_16_16_read, |
| 478 | .i2c_read = snd_soc_16_16_read_i2c, |
Dimitris Papastamos | f479fd9 | 2010-10-04 11:25:13 +0100 | [diff] [blame] | 479 | .spi_write = snd_soc_16_16_spi_write, |
Mark Brown | bc6552f | 2010-03-05 16:27:15 +0000 | [diff] [blame] | 480 | }, |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | /** |
| 484 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
| 485 | * |
| 486 | * @codec: CODEC to configure. |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 487 | * @addr_bits: Number of bits of register address data. |
| 488 | * @data_bits: Number of bits of data per register. |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 489 | * @control: Control bus used. |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 490 | * |
| 491 | * Register formats are frequently shared between many I2C and SPI |
| 492 | * devices. In order to promote code reuse the ASoC core provides |
| 493 | * some standard implementations of CODEC read and write operations |
| 494 | * which can be set up using this function. |
| 495 | * |
| 496 | * The caller is responsible for allocating and initialising the |
| 497 | * actual cache. |
| 498 | * |
| 499 | * Note that at present this code cannot be used by CODECs with |
| 500 | * volatile registers. |
| 501 | */ |
| 502 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 503 | int addr_bits, int data_bits, |
| 504 | enum snd_soc_control_type control) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 505 | { |
| 506 | int i; |
| 507 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 508 | for (i = 0; i < ARRAY_SIZE(io_types); i++) |
| 509 | if (io_types[i].addr_bits == addr_bits && |
| 510 | io_types[i].data_bits == data_bits) |
| 511 | break; |
| 512 | if (i == ARRAY_SIZE(io_types)) { |
| 513 | printk(KERN_ERR |
| 514 | "No I/O functions for %d bit address %d bit data\n", |
| 515 | addr_bits, data_bits); |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | |
Mark Brown | c3acec2 | 2010-12-02 16:15:29 +0000 | [diff] [blame] | 519 | codec->write = io_types[i].write; |
| 520 | codec->read = io_types[i].read; |
Dimitris Papastamos | 5fb609d | 2011-03-22 10:37:03 +0000 | [diff] [blame] | 521 | codec->bulk_write_raw = snd_soc_hw_bulk_write_raw; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 522 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 523 | switch (control) { |
| 524 | case SND_SOC_CUSTOM: |
| 525 | break; |
| 526 | |
| 527 | case SND_SOC_I2C: |
Randy Dunlap | 17244c2 | 2009-08-10 16:04:39 -0700 | [diff] [blame] | 528 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 529 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 530 | #endif |
Mark Brown | afa2f10 | 2009-07-10 23:11:24 +0100 | [diff] [blame] | 531 | if (io_types[i].i2c_read) |
| 532 | codec->hw_read = io_types[i].i2c_read; |
Mark Brown | a6d1434 | 2010-08-12 10:59:15 +0100 | [diff] [blame] | 533 | |
| 534 | codec->control_data = container_of(codec->dev, |
| 535 | struct i2c_client, |
| 536 | dev); |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 537 | break; |
| 538 | |
| 539 | case SND_SOC_SPI: |
Mark Brown | 27ded04 | 2009-07-10 23:28:16 +0100 | [diff] [blame] | 540 | if (io_types[i].spi_write) |
| 541 | codec->hw_write = io_types[i].spi_write; |
Mark Brown | a6d1434 | 2010-08-12 10:59:15 +0100 | [diff] [blame] | 542 | |
| 543 | codec->control_data = container_of(codec->dev, |
| 544 | struct spi_device, |
| 545 | dev); |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 552 | |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 553 | static bool snd_soc_set_cache_val(void *base, unsigned int idx, |
| 554 | unsigned int val, unsigned int word_size) |
| 555 | { |
| 556 | switch (word_size) { |
| 557 | case 1: { |
| 558 | u8 *cache = base; |
| 559 | if (cache[idx] == val) |
| 560 | return true; |
| 561 | cache[idx] = val; |
| 562 | break; |
| 563 | } |
| 564 | case 2: { |
| 565 | u16 *cache = base; |
| 566 | if (cache[idx] == val) |
| 567 | return true; |
| 568 | cache[idx] = val; |
| 569 | break; |
| 570 | } |
| 571 | default: |
| 572 | BUG(); |
| 573 | } |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx, |
| 578 | unsigned int word_size) |
| 579 | { |
| 580 | switch (word_size) { |
| 581 | case 1: { |
| 582 | const u8 *cache = base; |
| 583 | return cache[idx]; |
| 584 | } |
| 585 | case 2: { |
| 586 | const u16 *cache = base; |
| 587 | return cache[idx]; |
| 588 | } |
| 589 | default: |
| 590 | BUG(); |
| 591 | } |
| 592 | /* unreachable */ |
| 593 | return -1; |
| 594 | } |
| 595 | |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 596 | struct snd_soc_rbtree_node { |
| 597 | struct rb_node node; |
| 598 | unsigned int reg; |
| 599 | unsigned int value; |
| 600 | unsigned int defval; |
| 601 | } __attribute__ ((packed)); |
| 602 | |
| 603 | struct snd_soc_rbtree_ctx { |
| 604 | struct rb_root root; |
| 605 | }; |
| 606 | |
| 607 | static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup( |
| 608 | struct rb_root *root, unsigned int reg) |
| 609 | { |
| 610 | struct rb_node *node; |
| 611 | struct snd_soc_rbtree_node *rbnode; |
| 612 | |
| 613 | node = root->rb_node; |
| 614 | while (node) { |
| 615 | rbnode = container_of(node, struct snd_soc_rbtree_node, node); |
| 616 | if (rbnode->reg < reg) |
| 617 | node = node->rb_left; |
| 618 | else if (rbnode->reg > reg) |
| 619 | node = node->rb_right; |
| 620 | else |
| 621 | return rbnode; |
| 622 | } |
| 623 | |
| 624 | return NULL; |
| 625 | } |
| 626 | |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 627 | static int snd_soc_rbtree_insert(struct rb_root *root, |
| 628 | struct snd_soc_rbtree_node *rbnode) |
| 629 | { |
| 630 | struct rb_node **new, *parent; |
| 631 | struct snd_soc_rbtree_node *rbnode_tmp; |
| 632 | |
| 633 | parent = NULL; |
| 634 | new = &root->rb_node; |
| 635 | while (*new) { |
| 636 | rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node, |
| 637 | node); |
| 638 | parent = *new; |
| 639 | if (rbnode_tmp->reg < rbnode->reg) |
| 640 | new = &((*new)->rb_left); |
| 641 | else if (rbnode_tmp->reg > rbnode->reg) |
| 642 | new = &((*new)->rb_right); |
| 643 | else |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | /* insert the node into the rbtree */ |
| 648 | rb_link_node(&rbnode->node, parent, new); |
| 649 | rb_insert_color(&rbnode->node, root); |
| 650 | |
| 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec) |
| 655 | { |
| 656 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 657 | struct rb_node *node; |
| 658 | struct snd_soc_rbtree_node *rbnode; |
| 659 | unsigned int val; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 660 | int ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 661 | |
| 662 | rbtree_ctx = codec->reg_cache; |
| 663 | for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) { |
| 664 | rbnode = rb_entry(node, struct snd_soc_rbtree_node, node); |
| 665 | if (rbnode->value == rbnode->defval) |
| 666 | continue; |
Dimitris Papastamos | f20eda5 | 2011-03-28 11:39:15 +0100 | [diff] [blame] | 667 | WARN_ON(codec->writable_register && |
| 668 | codec->writable_register(codec, rbnode->reg)); |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 669 | ret = snd_soc_cache_read(codec, rbnode->reg, &val); |
| 670 | if (ret) |
| 671 | return ret; |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame] | 672 | codec->cache_bypass = 1; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 673 | ret = snd_soc_write(codec, rbnode->reg, val); |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame] | 674 | codec->cache_bypass = 0; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 675 | if (ret) |
| 676 | return ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 677 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 678 | rbnode->reg, val); |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec, |
| 685 | unsigned int reg, unsigned int value) |
| 686 | { |
| 687 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 688 | struct snd_soc_rbtree_node *rbnode; |
| 689 | |
| 690 | rbtree_ctx = codec->reg_cache; |
| 691 | rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg); |
| 692 | if (rbnode) { |
| 693 | if (rbnode->value == value) |
| 694 | return 0; |
| 695 | rbnode->value = value; |
| 696 | } else { |
| 697 | /* bail out early, no need to create the rbnode yet */ |
| 698 | if (!value) |
| 699 | return 0; |
| 700 | /* |
| 701 | * for uninitialized registers whose value is changed |
| 702 | * from the default zero, create an rbnode and insert |
| 703 | * it into the tree. |
| 704 | */ |
| 705 | rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL); |
| 706 | if (!rbnode) |
| 707 | return -ENOMEM; |
| 708 | rbnode->reg = reg; |
| 709 | rbnode->value = value; |
| 710 | snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode); |
| 711 | } |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec, |
| 717 | unsigned int reg, unsigned int *value) |
| 718 | { |
| 719 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 720 | struct snd_soc_rbtree_node *rbnode; |
| 721 | |
| 722 | rbtree_ctx = codec->reg_cache; |
| 723 | rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg); |
| 724 | if (rbnode) { |
| 725 | *value = rbnode->value; |
| 726 | } else { |
| 727 | /* uninitialized registers default to 0 */ |
| 728 | *value = 0; |
| 729 | } |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec) |
| 735 | { |
| 736 | struct rb_node *next; |
| 737 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
| 738 | struct snd_soc_rbtree_node *rbtree_node; |
| 739 | |
| 740 | /* if we've already been called then just return */ |
| 741 | rbtree_ctx = codec->reg_cache; |
| 742 | if (!rbtree_ctx) |
| 743 | return 0; |
| 744 | |
| 745 | /* free up the rbtree */ |
| 746 | next = rb_first(&rbtree_ctx->root); |
| 747 | while (next) { |
| 748 | rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node); |
| 749 | next = rb_next(&rbtree_node->node); |
| 750 | rb_erase(&rbtree_node->node, &rbtree_ctx->root); |
| 751 | kfree(rbtree_node); |
| 752 | } |
| 753 | |
| 754 | /* release the resources */ |
| 755 | kfree(codec->reg_cache); |
| 756 | codec->reg_cache = NULL; |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec) |
| 762 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 763 | struct snd_soc_rbtree_node *rbtree_node; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 764 | struct snd_soc_rbtree_ctx *rbtree_ctx; |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 765 | unsigned int val; |
| 766 | unsigned int word_size; |
| 767 | int i; |
| 768 | int ret; |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 769 | |
| 770 | codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL); |
| 771 | if (!codec->reg_cache) |
| 772 | return -ENOMEM; |
| 773 | |
| 774 | rbtree_ctx = codec->reg_cache; |
| 775 | rbtree_ctx->root = RB_ROOT; |
| 776 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 777 | if (!codec->reg_def_copy) |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 778 | return 0; |
| 779 | |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 780 | /* |
| 781 | * populate the rbtree with the initialized registers. All other |
| 782 | * registers will be inserted when they are first modified. |
| 783 | */ |
| 784 | word_size = codec->driver->reg_word_size; |
| 785 | for (i = 0; i < codec->driver->reg_cache_size; ++i) { |
| 786 | val = snd_soc_get_cache_val(codec->reg_def_copy, i, word_size); |
| 787 | if (!val) |
| 788 | continue; |
| 789 | rbtree_node = kzalloc(sizeof *rbtree_node, GFP_KERNEL); |
| 790 | if (!rbtree_node) { |
| 791 | ret = -ENOMEM; |
| 792 | snd_soc_cache_exit(codec); |
| 793 | break; |
| 794 | } |
| 795 | rbtree_node->reg = i; |
| 796 | rbtree_node->value = val; |
| 797 | rbtree_node->defval = val; |
| 798 | snd_soc_rbtree_insert(&rbtree_ctx->root, rbtree_node); |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 804 | #ifdef CONFIG_SND_SOC_CACHE_LZO |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 805 | struct snd_soc_lzo_ctx { |
| 806 | void *wmem; |
| 807 | void *dst; |
| 808 | const void *src; |
| 809 | size_t src_len; |
| 810 | size_t dst_len; |
| 811 | size_t decompressed_size; |
| 812 | unsigned long *sync_bmp; |
| 813 | int sync_bmp_nbits; |
| 814 | }; |
| 815 | |
| 816 | #define LZO_BLOCK_NUM 8 |
| 817 | static int snd_soc_lzo_block_count(void) |
| 818 | { |
| 819 | return LZO_BLOCK_NUM; |
| 820 | } |
| 821 | |
| 822 | static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx) |
| 823 | { |
| 824 | lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 825 | if (!lzo_ctx->wmem) |
| 826 | return -ENOMEM; |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx) |
| 831 | { |
| 832 | size_t compress_size; |
| 833 | int ret; |
| 834 | |
| 835 | ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len, |
| 836 | lzo_ctx->dst, &compress_size, lzo_ctx->wmem); |
| 837 | if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len) |
| 838 | return -EINVAL; |
| 839 | lzo_ctx->dst_len = compress_size; |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx) |
| 844 | { |
| 845 | size_t dst_len; |
| 846 | int ret; |
| 847 | |
| 848 | dst_len = lzo_ctx->dst_len; |
| 849 | ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len, |
| 850 | lzo_ctx->dst, &dst_len); |
| 851 | if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len) |
| 852 | return -EINVAL; |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec, |
| 857 | struct snd_soc_lzo_ctx *lzo_ctx) |
| 858 | { |
| 859 | int ret; |
| 860 | |
| 861 | lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE); |
| 862 | lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL); |
| 863 | if (!lzo_ctx->dst) { |
| 864 | lzo_ctx->dst_len = 0; |
| 865 | return -ENOMEM; |
| 866 | } |
| 867 | |
| 868 | ret = snd_soc_lzo_compress(lzo_ctx); |
| 869 | if (ret < 0) |
| 870 | return ret; |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec, |
| 875 | struct snd_soc_lzo_ctx *lzo_ctx) |
| 876 | { |
| 877 | int ret; |
| 878 | |
| 879 | lzo_ctx->dst_len = lzo_ctx->decompressed_size; |
| 880 | lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL); |
| 881 | if (!lzo_ctx->dst) { |
| 882 | lzo_ctx->dst_len = 0; |
| 883 | return -ENOMEM; |
| 884 | } |
| 885 | |
| 886 | ret = snd_soc_lzo_decompress(lzo_ctx); |
| 887 | if (ret < 0) |
| 888 | return ret; |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec, |
| 893 | unsigned int reg) |
| 894 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 895 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 896 | |
| 897 | codec_drv = codec->driver; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 898 | return (reg * codec_drv->reg_word_size) / |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 899 | DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec, |
| 903 | unsigned int reg) |
| 904 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 905 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 906 | |
| 907 | codec_drv = codec->driver; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 908 | 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] | 909 | codec_drv->reg_word_size); |
| 910 | } |
| 911 | |
| 912 | static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec) |
| 913 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 914 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 915 | |
| 916 | codec_drv = codec->driver; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 917 | return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec) |
| 921 | { |
| 922 | struct snd_soc_lzo_ctx **lzo_blocks; |
| 923 | unsigned int val; |
| 924 | int i; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 925 | int ret; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 926 | |
| 927 | lzo_blocks = codec->reg_cache; |
| 928 | 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] | 929 | WARN_ON(codec->writable_register && |
| 930 | codec->writable_register(codec, i)); |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 931 | ret = snd_soc_cache_read(codec, i, &val); |
| 932 | if (ret) |
| 933 | return ret; |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame] | 934 | codec->cache_bypass = 1; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 935 | ret = snd_soc_write(codec, i, val); |
Dimitris Papastamos | 9978007 | 2011-01-19 14:53:37 +0000 | [diff] [blame] | 936 | codec->cache_bypass = 0; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 937 | if (ret) |
| 938 | return ret; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 939 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 940 | i, val); |
| 941 | } |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec, |
| 947 | unsigned int reg, unsigned int value) |
| 948 | { |
| 949 | struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks; |
| 950 | int ret, blkindex, blkpos; |
| 951 | size_t blksize, tmp_dst_len; |
| 952 | void *tmp_dst; |
| 953 | |
| 954 | /* index of the compressed lzo block */ |
| 955 | blkindex = snd_soc_lzo_get_blkindex(codec, reg); |
| 956 | /* register index within the decompressed block */ |
| 957 | blkpos = snd_soc_lzo_get_blkpos(codec, reg); |
| 958 | /* size of the compressed block */ |
| 959 | blksize = snd_soc_lzo_get_blksize(codec); |
| 960 | lzo_blocks = codec->reg_cache; |
| 961 | lzo_block = lzo_blocks[blkindex]; |
| 962 | |
| 963 | /* save the pointer and length of the compressed block */ |
| 964 | tmp_dst = lzo_block->dst; |
| 965 | tmp_dst_len = lzo_block->dst_len; |
| 966 | |
| 967 | /* prepare the source to be the compressed block */ |
| 968 | lzo_block->src = lzo_block->dst; |
| 969 | lzo_block->src_len = lzo_block->dst_len; |
| 970 | |
| 971 | /* decompress the block */ |
| 972 | ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block); |
| 973 | if (ret < 0) { |
| 974 | kfree(lzo_block->dst); |
| 975 | goto out; |
| 976 | } |
| 977 | |
| 978 | /* write the new value to the cache */ |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 979 | if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value, |
| 980 | codec->driver->reg_word_size)) { |
| 981 | kfree(lzo_block->dst); |
| 982 | goto out; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /* prepare the source to be the decompressed block */ |
| 986 | lzo_block->src = lzo_block->dst; |
| 987 | lzo_block->src_len = lzo_block->dst_len; |
| 988 | |
| 989 | /* compress the block */ |
| 990 | ret = snd_soc_lzo_compress_cache_block(codec, lzo_block); |
| 991 | if (ret < 0) { |
| 992 | kfree(lzo_block->dst); |
| 993 | kfree(lzo_block->src); |
| 994 | goto out; |
| 995 | } |
| 996 | |
| 997 | /* set the bit so we know we have to sync this register */ |
| 998 | set_bit(reg, lzo_block->sync_bmp); |
| 999 | kfree(tmp_dst); |
| 1000 | kfree(lzo_block->src); |
| 1001 | return 0; |
| 1002 | out: |
| 1003 | lzo_block->dst = tmp_dst; |
| 1004 | lzo_block->dst_len = tmp_dst_len; |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec, |
| 1009 | unsigned int reg, unsigned int *value) |
| 1010 | { |
| 1011 | struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks; |
| 1012 | int ret, blkindex, blkpos; |
| 1013 | size_t blksize, tmp_dst_len; |
| 1014 | void *tmp_dst; |
| 1015 | |
| 1016 | *value = 0; |
| 1017 | /* index of the compressed lzo block */ |
| 1018 | blkindex = snd_soc_lzo_get_blkindex(codec, reg); |
| 1019 | /* register index within the decompressed block */ |
| 1020 | blkpos = snd_soc_lzo_get_blkpos(codec, reg); |
| 1021 | /* size of the compressed block */ |
| 1022 | blksize = snd_soc_lzo_get_blksize(codec); |
| 1023 | lzo_blocks = codec->reg_cache; |
| 1024 | lzo_block = lzo_blocks[blkindex]; |
| 1025 | |
| 1026 | /* save the pointer and length of the compressed block */ |
| 1027 | tmp_dst = lzo_block->dst; |
| 1028 | tmp_dst_len = lzo_block->dst_len; |
| 1029 | |
| 1030 | /* prepare the source to be the compressed block */ |
| 1031 | lzo_block->src = lzo_block->dst; |
| 1032 | lzo_block->src_len = lzo_block->dst_len; |
| 1033 | |
| 1034 | /* decompress the block */ |
| 1035 | ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block); |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1036 | if (ret >= 0) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1037 | /* fetch the value from the cache */ |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1038 | *value = snd_soc_get_cache_val(lzo_block->dst, blkpos, |
| 1039 | codec->driver->reg_word_size); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1040 | |
| 1041 | kfree(lzo_block->dst); |
| 1042 | /* restore the pointer and length of the compressed block */ |
| 1043 | lzo_block->dst = tmp_dst; |
| 1044 | lzo_block->dst_len = tmp_dst_len; |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec) |
| 1049 | { |
| 1050 | struct snd_soc_lzo_ctx **lzo_blocks; |
| 1051 | int i, blkcount; |
| 1052 | |
| 1053 | lzo_blocks = codec->reg_cache; |
| 1054 | if (!lzo_blocks) |
| 1055 | return 0; |
| 1056 | |
| 1057 | blkcount = snd_soc_lzo_block_count(); |
| 1058 | /* |
| 1059 | * the pointer to the bitmap used for syncing the cache |
| 1060 | * is shared amongst all lzo_blocks. Ensure it is freed |
| 1061 | * only once. |
| 1062 | */ |
| 1063 | if (lzo_blocks[0]) |
| 1064 | kfree(lzo_blocks[0]->sync_bmp); |
| 1065 | for (i = 0; i < blkcount; ++i) { |
| 1066 | if (lzo_blocks[i]) { |
| 1067 | kfree(lzo_blocks[i]->wmem); |
| 1068 | kfree(lzo_blocks[i]->dst); |
| 1069 | } |
| 1070 | /* each lzo_block is a pointer returned by kmalloc or NULL */ |
| 1071 | kfree(lzo_blocks[i]); |
| 1072 | } |
| 1073 | kfree(lzo_blocks); |
| 1074 | codec->reg_cache = NULL; |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec) |
| 1079 | { |
| 1080 | struct snd_soc_lzo_ctx **lzo_blocks; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1081 | size_t bmp_size; |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1082 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1083 | int ret, tofree, i, blksize, blkcount; |
| 1084 | const char *p, *end; |
| 1085 | unsigned long *sync_bmp; |
| 1086 | |
| 1087 | ret = 0; |
| 1088 | codec_drv = codec->driver; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* |
| 1091 | * If we have not been given a default register cache |
| 1092 | * then allocate a dummy zero-ed out region, compress it |
| 1093 | * and remember to free it afterwards. |
| 1094 | */ |
| 1095 | tofree = 0; |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1096 | if (!codec->reg_def_copy) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1097 | tofree = 1; |
| 1098 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1099 | if (!codec->reg_def_copy) { |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1100 | codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1101 | if (!codec->reg_def_copy) |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1102 | return -ENOMEM; |
| 1103 | } |
| 1104 | |
| 1105 | blkcount = snd_soc_lzo_block_count(); |
| 1106 | codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks, |
| 1107 | GFP_KERNEL); |
| 1108 | if (!codec->reg_cache) { |
| 1109 | ret = -ENOMEM; |
| 1110 | goto err_tofree; |
| 1111 | } |
| 1112 | lzo_blocks = codec->reg_cache; |
| 1113 | |
| 1114 | /* |
| 1115 | * allocate a bitmap to be used when syncing the cache with |
| 1116 | * the hardware. Each time a register is modified, the corresponding |
| 1117 | * bit is set in the bitmap, so we know that we have to sync |
| 1118 | * that register. |
| 1119 | */ |
| 1120 | bmp_size = codec_drv->reg_cache_size; |
Dimitris Papastamos | 465d7fc | 2010-12-14 15:15:36 +0000 | [diff] [blame] | 1121 | sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long), |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1122 | GFP_KERNEL); |
| 1123 | if (!sync_bmp) { |
| 1124 | ret = -ENOMEM; |
| 1125 | goto err; |
| 1126 | } |
Dimitris Papastamos | 09c74a9 | 2010-11-29 11:43:33 +0000 | [diff] [blame] | 1127 | bitmap_zero(sync_bmp, bmp_size); |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1128 | |
| 1129 | /* allocate the lzo blocks and initialize them */ |
| 1130 | for (i = 0; i < blkcount; ++i) { |
| 1131 | lzo_blocks[i] = kzalloc(sizeof **lzo_blocks, |
| 1132 | GFP_KERNEL); |
| 1133 | if (!lzo_blocks[i]) { |
| 1134 | kfree(sync_bmp); |
| 1135 | ret = -ENOMEM; |
| 1136 | goto err; |
| 1137 | } |
| 1138 | lzo_blocks[i]->sync_bmp = sync_bmp; |
Dimitris Papastamos | 04f8fd1 | 2011-01-11 11:24:02 +0000 | [diff] [blame] | 1139 | lzo_blocks[i]->sync_bmp_nbits = bmp_size; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1140 | /* alloc the working space for the compressed block */ |
| 1141 | ret = snd_soc_lzo_prepare(lzo_blocks[i]); |
| 1142 | if (ret < 0) |
| 1143 | goto err; |
| 1144 | } |
| 1145 | |
| 1146 | blksize = snd_soc_lzo_get_blksize(codec); |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1147 | p = codec->reg_def_copy; |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1148 | end = codec->reg_def_copy + codec->reg_size; |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1149 | /* compress the register map and fill the lzo blocks */ |
| 1150 | for (i = 0; i < blkcount; ++i, p += blksize) { |
| 1151 | lzo_blocks[i]->src = p; |
| 1152 | if (p + blksize > end) |
| 1153 | lzo_blocks[i]->src_len = end - p; |
| 1154 | else |
| 1155 | lzo_blocks[i]->src_len = blksize; |
| 1156 | ret = snd_soc_lzo_compress_cache_block(codec, |
| 1157 | lzo_blocks[i]); |
| 1158 | if (ret < 0) |
| 1159 | goto err; |
| 1160 | lzo_blocks[i]->decompressed_size = |
| 1161 | lzo_blocks[i]->src_len; |
| 1162 | } |
| 1163 | |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1164 | if (tofree) { |
| 1165 | kfree(codec->reg_def_copy); |
| 1166 | codec->reg_def_copy = NULL; |
| 1167 | } |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1168 | return 0; |
| 1169 | err: |
| 1170 | snd_soc_cache_exit(codec); |
| 1171 | err_tofree: |
Dimitris Papastamos | 3335ddc | 2010-12-02 16:11:05 +0000 | [diff] [blame] | 1172 | if (tofree) { |
| 1173 | kfree(codec->reg_def_copy); |
| 1174 | codec->reg_def_copy = NULL; |
| 1175 | } |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1176 | return ret; |
| 1177 | } |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1178 | #endif |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1179 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1180 | static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec) |
| 1181 | { |
| 1182 | int i; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1183 | int ret; |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1184 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1185 | unsigned int val; |
| 1186 | |
| 1187 | codec_drv = codec->driver; |
| 1188 | for (i = 0; i < codec_drv->reg_cache_size; ++i) { |
Dimitris Papastamos | f20eda5 | 2011-03-28 11:39:15 +0100 | [diff] [blame] | 1189 | WARN_ON(codec->writable_register && |
| 1190 | codec->writable_register(codec, i)); |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1191 | ret = snd_soc_cache_read(codec, i, &val); |
| 1192 | if (ret) |
| 1193 | return ret; |
Dimitris Papastamos | d779fce | 2011-01-12 10:22:28 +0000 | [diff] [blame] | 1194 | if (codec->reg_def_copy) |
| 1195 | if (snd_soc_get_cache_val(codec->reg_def_copy, |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1196 | i, codec_drv->reg_word_size) == val) |
| 1197 | continue; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 1198 | ret = snd_soc_write(codec, i, val); |
| 1199 | if (ret) |
| 1200 | return ret; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1201 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 1202 | i, val); |
| 1203 | } |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | static int snd_soc_flat_cache_write(struct snd_soc_codec *codec, |
| 1208 | unsigned int reg, unsigned int value) |
| 1209 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1210 | snd_soc_set_cache_val(codec->reg_cache, reg, value, |
| 1211 | codec->driver->reg_word_size); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static int snd_soc_flat_cache_read(struct snd_soc_codec *codec, |
| 1216 | unsigned int reg, unsigned int *value) |
| 1217 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 1218 | *value = snd_soc_get_cache_val(codec->reg_cache, reg, |
| 1219 | codec->driver->reg_word_size); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec) |
| 1224 | { |
| 1225 | if (!codec->reg_cache) |
| 1226 | return 0; |
| 1227 | kfree(codec->reg_cache); |
| 1228 | codec->reg_cache = NULL; |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static int snd_soc_flat_cache_init(struct snd_soc_codec *codec) |
| 1233 | { |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 1234 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1235 | |
| 1236 | codec_drv = codec->driver; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1237 | |
Dimitris Papastamos | d779fce | 2011-01-12 10:22:28 +0000 | [diff] [blame] | 1238 | if (codec->reg_def_copy) |
| 1239 | codec->reg_cache = kmemdup(codec->reg_def_copy, |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1240 | codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1241 | else |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 1242 | codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1243 | if (!codec->reg_cache) |
| 1244 | return -ENOMEM; |
| 1245 | |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | /* an array of all supported compression types */ |
| 1250 | static const struct snd_soc_cache_ops cache_types[] = { |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1251 | /* Flat *must* be the first entry for fallback */ |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1252 | { |
Dimitris Papastamos | df0701b | 2010-11-29 10:54:28 +0000 | [diff] [blame] | 1253 | .id = SND_SOC_FLAT_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1254 | .name = "flat", |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1255 | .init = snd_soc_flat_cache_init, |
| 1256 | .exit = snd_soc_flat_cache_exit, |
| 1257 | .read = snd_soc_flat_cache_read, |
| 1258 | .write = snd_soc_flat_cache_write, |
| 1259 | .sync = snd_soc_flat_cache_sync |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1260 | }, |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1261 | #ifdef CONFIG_SND_SOC_CACHE_LZO |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1262 | { |
| 1263 | .id = SND_SOC_LZO_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1264 | .name = "LZO", |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 1265 | .init = snd_soc_lzo_cache_init, |
| 1266 | .exit = snd_soc_lzo_cache_exit, |
| 1267 | .read = snd_soc_lzo_cache_read, |
| 1268 | .write = snd_soc_lzo_cache_write, |
| 1269 | .sync = snd_soc_lzo_cache_sync |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1270 | }, |
Mark Brown | 68d44ee | 2010-12-21 17:19:56 +0000 | [diff] [blame] | 1271 | #endif |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1272 | { |
| 1273 | .id = SND_SOC_RBTREE_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1274 | .name = "rbtree", |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 1275 | .init = snd_soc_rbtree_cache_init, |
| 1276 | .exit = snd_soc_rbtree_cache_exit, |
| 1277 | .read = snd_soc_rbtree_cache_read, |
| 1278 | .write = snd_soc_rbtree_cache_write, |
| 1279 | .sync = snd_soc_rbtree_cache_sync |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1280 | } |
| 1281 | }; |
| 1282 | |
| 1283 | int snd_soc_cache_init(struct snd_soc_codec *codec) |
| 1284 | { |
| 1285 | int i; |
| 1286 | |
| 1287 | for (i = 0; i < ARRAY_SIZE(cache_types); ++i) |
Dimitris Papastamos | 23bbce3 | 2010-12-02 14:53:01 +0000 | [diff] [blame] | 1288 | if (cache_types[i].id == codec->compress_type) |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1289 | break; |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1290 | |
| 1291 | /* Fall back to flat compression */ |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1292 | if (i == ARRAY_SIZE(cache_types)) { |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 1293 | dev_warn(codec->dev, "Could not match compress type: %d\n", |
| 1294 | codec->compress_type); |
| 1295 | i = 0; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | mutex_init(&codec->cache_rw_mutex); |
| 1299 | codec->cache_ops = &cache_types[i]; |
| 1300 | |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1301 | if (codec->cache_ops->init) { |
| 1302 | if (codec->cache_ops->name) |
| 1303 | dev_dbg(codec->dev, "Initializing %s cache for %s codec\n", |
| 1304 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1305 | return codec->cache_ops->init(codec); |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1306 | } |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1307 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | /* |
| 1311 | * NOTE: keep in mind that this function might be called |
| 1312 | * multiple times. |
| 1313 | */ |
| 1314 | int snd_soc_cache_exit(struct snd_soc_codec *codec) |
| 1315 | { |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1316 | if (codec->cache_ops && codec->cache_ops->exit) { |
| 1317 | if (codec->cache_ops->name) |
| 1318 | dev_dbg(codec->dev, "Destroying %s cache for %s codec\n", |
| 1319 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1320 | return codec->cache_ops->exit(codec); |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 1321 | } |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1322 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | /** |
| 1326 | * snd_soc_cache_read: Fetch the value of a given register from the cache. |
| 1327 | * |
| 1328 | * @codec: CODEC to configure. |
| 1329 | * @reg: The register index. |
| 1330 | * @value: The value to be returned. |
| 1331 | */ |
| 1332 | int snd_soc_cache_read(struct snd_soc_codec *codec, |
| 1333 | unsigned int reg, unsigned int *value) |
| 1334 | { |
| 1335 | int ret; |
| 1336 | |
| 1337 | mutex_lock(&codec->cache_rw_mutex); |
| 1338 | |
| 1339 | if (value && codec->cache_ops && codec->cache_ops->read) { |
| 1340 | ret = codec->cache_ops->read(codec, reg, value); |
| 1341 | mutex_unlock(&codec->cache_rw_mutex); |
| 1342 | return ret; |
| 1343 | } |
| 1344 | |
| 1345 | mutex_unlock(&codec->cache_rw_mutex); |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1346 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1347 | } |
| 1348 | EXPORT_SYMBOL_GPL(snd_soc_cache_read); |
| 1349 | |
| 1350 | /** |
| 1351 | * snd_soc_cache_write: Set the value of a given register in the cache. |
| 1352 | * |
| 1353 | * @codec: CODEC to configure. |
| 1354 | * @reg: The register index. |
| 1355 | * @value: The new register value. |
| 1356 | */ |
| 1357 | int snd_soc_cache_write(struct snd_soc_codec *codec, |
| 1358 | unsigned int reg, unsigned int value) |
| 1359 | { |
| 1360 | int ret; |
| 1361 | |
| 1362 | mutex_lock(&codec->cache_rw_mutex); |
| 1363 | |
| 1364 | if (codec->cache_ops && codec->cache_ops->write) { |
| 1365 | ret = codec->cache_ops->write(codec, reg, value); |
| 1366 | mutex_unlock(&codec->cache_rw_mutex); |
| 1367 | return ret; |
| 1368 | } |
| 1369 | |
| 1370 | mutex_unlock(&codec->cache_rw_mutex); |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1371 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1372 | } |
| 1373 | EXPORT_SYMBOL_GPL(snd_soc_cache_write); |
| 1374 | |
| 1375 | /** |
| 1376 | * snd_soc_cache_sync: Sync the register cache with the hardware. |
| 1377 | * |
| 1378 | * @codec: CODEC to configure. |
| 1379 | * |
| 1380 | * Any registers that should not be synced should be marked as |
| 1381 | * volatile. In general drivers can choose not to use the provided |
| 1382 | * syncing functionality if they so require. |
| 1383 | */ |
| 1384 | int snd_soc_cache_sync(struct snd_soc_codec *codec) |
| 1385 | { |
| 1386 | int ret; |
Dimitris Papastamos | c358e64 | 2011-01-21 15:29:02 +0000 | [diff] [blame] | 1387 | const char *name; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1388 | |
| 1389 | if (!codec->cache_sync) { |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 1393 | if (!codec->cache_ops || !codec->cache_ops->sync) |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 1394 | return -ENOSYS; |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 1395 | |
Dimitris Papastamos | c358e64 | 2011-01-21 15:29:02 +0000 | [diff] [blame] | 1396 | if (codec->cache_ops->name) |
| 1397 | name = codec->cache_ops->name; |
| 1398 | else |
| 1399 | name = "unknown"; |
| 1400 | |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 1401 | if (codec->cache_ops->name) |
| 1402 | dev_dbg(codec->dev, "Syncing %s cache for %s codec\n", |
| 1403 | codec->cache_ops->name, codec->name); |
| 1404 | trace_snd_soc_cache_sync(codec, name, "start"); |
| 1405 | ret = codec->cache_ops->sync(codec); |
| 1406 | if (!ret) |
| 1407 | codec->cache_sync = 0; |
| 1408 | trace_snd_soc_cache_sync(codec, name, "end"); |
| 1409 | return ret; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 1410 | } |
| 1411 | EXPORT_SYMBOL_GPL(snd_soc_cache_sync); |
Dimitris Papastamos | 066d16c | 2011-01-13 12:20:36 +0000 | [diff] [blame] | 1412 | |
| 1413 | static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec, |
| 1414 | unsigned int reg) |
| 1415 | { |
| 1416 | const struct snd_soc_codec_driver *codec_drv; |
| 1417 | unsigned int min, max, index; |
| 1418 | |
| 1419 | codec_drv = codec->driver; |
| 1420 | min = 0; |
| 1421 | max = codec_drv->reg_access_size - 1; |
| 1422 | do { |
| 1423 | index = (min + max) / 2; |
| 1424 | if (codec_drv->reg_access_default[index].reg == reg) |
| 1425 | return index; |
| 1426 | if (codec_drv->reg_access_default[index].reg < reg) |
| 1427 | min = index + 1; |
| 1428 | else |
| 1429 | max = index; |
| 1430 | } while (min <= max); |
| 1431 | return -1; |
| 1432 | } |
| 1433 | |
| 1434 | int snd_soc_default_volatile_register(struct snd_soc_codec *codec, |
| 1435 | unsigned int reg) |
| 1436 | { |
| 1437 | int index; |
| 1438 | |
| 1439 | if (reg >= codec->driver->reg_cache_size) |
| 1440 | return 1; |
| 1441 | index = snd_soc_get_reg_access_index(codec, reg); |
| 1442 | if (index < 0) |
| 1443 | return 0; |
| 1444 | return codec->driver->reg_access_default[index].vol; |
| 1445 | } |
| 1446 | EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register); |
| 1447 | |
| 1448 | int snd_soc_default_readable_register(struct snd_soc_codec *codec, |
| 1449 | unsigned int reg) |
| 1450 | { |
| 1451 | int index; |
| 1452 | |
| 1453 | if (reg >= codec->driver->reg_cache_size) |
| 1454 | return 1; |
| 1455 | index = snd_soc_get_reg_access_index(codec, reg); |
| 1456 | if (index < 0) |
| 1457 | return 0; |
| 1458 | return codec->driver->reg_access_default[index].read; |
| 1459 | } |
| 1460 | EXPORT_SYMBOL_GPL(snd_soc_default_readable_register); |
Dimitris Papastamos | 8020454 | 2011-03-24 13:45:17 +0000 | [diff] [blame] | 1461 | |
| 1462 | int snd_soc_default_writable_register(struct snd_soc_codec *codec, |
| 1463 | unsigned int reg) |
| 1464 | { |
| 1465 | int index; |
| 1466 | |
| 1467 | if (reg >= codec->driver->reg_cache_size) |
| 1468 | return 1; |
| 1469 | index = snd_soc_get_reg_access_index(codec, reg); |
| 1470 | if (index < 0) |
| 1471 | return 0; |
| 1472 | return codec->driver->reg_access_default[index].write; |
| 1473 | } |
| 1474 | EXPORT_SYMBOL_GPL(snd_soc_default_writable_register); |