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