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