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