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/bitmap.h> |
Dimitris Papastamos | a7f387d | 2010-11-11 10:04:59 +0000 | [diff] [blame] | 18 | #include <linux/rbtree.h> |
Paul Gortmaker | d81a6d7 | 2011-09-22 09:34:58 -0400 | [diff] [blame] | 19 | #include <linux/export.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 | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 23 | static bool snd_soc_set_cache_val(void *base, unsigned int idx, |
| 24 | unsigned int val, unsigned int word_size) |
| 25 | { |
| 26 | switch (word_size) { |
| 27 | case 1: { |
| 28 | u8 *cache = base; |
| 29 | if (cache[idx] == val) |
| 30 | return true; |
| 31 | cache[idx] = val; |
| 32 | break; |
| 33 | } |
| 34 | case 2: { |
| 35 | u16 *cache = base; |
| 36 | if (cache[idx] == val) |
| 37 | return true; |
| 38 | cache[idx] = val; |
| 39 | break; |
| 40 | } |
| 41 | default: |
| 42 | BUG(); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx, |
| 48 | unsigned int word_size) |
| 49 | { |
Mark Brown | fd137e2 | 2011-06-06 11:26:15 +0100 | [diff] [blame] | 50 | if (!base) |
| 51 | return -1; |
| 52 | |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 53 | switch (word_size) { |
| 54 | case 1: { |
| 55 | const u8 *cache = base; |
| 56 | return cache[idx]; |
| 57 | } |
| 58 | case 2: { |
| 59 | const u16 *cache = base; |
| 60 | return cache[idx]; |
| 61 | } |
| 62 | default: |
| 63 | BUG(); |
| 64 | } |
| 65 | /* unreachable */ |
| 66 | return -1; |
| 67 | } |
| 68 | |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 69 | static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec) |
| 70 | { |
| 71 | int i; |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 72 | int ret; |
Mark Brown | 001ae4c | 2010-12-02 16:21:08 +0000 | [diff] [blame] | 73 | const struct snd_soc_codec_driver *codec_drv; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 74 | unsigned int val; |
| 75 | |
| 76 | codec_drv = codec->driver; |
| 77 | for (i = 0; i < codec_drv->reg_cache_size; ++i) { |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 78 | ret = snd_soc_cache_read(codec, i, &val); |
| 79 | if (ret) |
| 80 | return ret; |
Dimitris Papastamos | d779fce | 2011-01-12 10:22:28 +0000 | [diff] [blame] | 81 | if (codec->reg_def_copy) |
| 82 | if (snd_soc_get_cache_val(codec->reg_def_copy, |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 83 | i, codec_drv->reg_word_size) == val) |
| 84 | continue; |
Lars-Peter Clausen | 6c5b756 | 2011-08-27 18:24:13 +0200 | [diff] [blame] | 85 | |
| 86 | WARN_ON(!snd_soc_codec_writable_register(codec, i)); |
| 87 | |
Dimitris Papastamos | 7a33d4c | 2010-11-29 10:24:54 +0000 | [diff] [blame] | 88 | ret = snd_soc_write(codec, i, val); |
| 89 | if (ret) |
| 90 | return ret; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 91 | dev_dbg(codec->dev, "Synced register %#x, value = %#x\n", |
| 92 | i, val); |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int snd_soc_flat_cache_write(struct snd_soc_codec *codec, |
| 98 | unsigned int reg, unsigned int value) |
| 99 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 100 | snd_soc_set_cache_val(codec->reg_cache, reg, value, |
| 101 | codec->driver->reg_word_size); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int snd_soc_flat_cache_read(struct snd_soc_codec *codec, |
| 106 | unsigned int reg, unsigned int *value) |
| 107 | { |
Dimitris Papastamos | 1321e88 | 2011-01-11 11:29:49 +0000 | [diff] [blame] | 108 | *value = snd_soc_get_cache_val(codec->reg_cache, reg, |
| 109 | codec->driver->reg_word_size); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec) |
| 114 | { |
| 115 | if (!codec->reg_cache) |
| 116 | return 0; |
| 117 | kfree(codec->reg_cache); |
| 118 | codec->reg_cache = NULL; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int snd_soc_flat_cache_init(struct snd_soc_codec *codec) |
| 123 | { |
Dimitris Papastamos | d779fce | 2011-01-12 10:22:28 +0000 | [diff] [blame] | 124 | if (codec->reg_def_copy) |
| 125 | codec->reg_cache = kmemdup(codec->reg_def_copy, |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 126 | codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 127 | else |
Dimitris Papastamos | aea170a | 2011-01-12 10:38:58 +0000 | [diff] [blame] | 128 | codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 129 | if (!codec->reg_cache) |
| 130 | return -ENOMEM; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* an array of all supported compression types */ |
| 136 | static const struct snd_soc_cache_ops cache_types[] = { |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 137 | /* Flat *must* be the first entry for fallback */ |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 138 | { |
Dimitris Papastamos | df0701b | 2010-11-29 10:54:28 +0000 | [diff] [blame] | 139 | .id = SND_SOC_FLAT_COMPRESSION, |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 140 | .name = "flat", |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 141 | .init = snd_soc_flat_cache_init, |
| 142 | .exit = snd_soc_flat_cache_exit, |
| 143 | .read = snd_soc_flat_cache_read, |
| 144 | .write = snd_soc_flat_cache_write, |
| 145 | .sync = snd_soc_flat_cache_sync |
Dimitris Papastamos | cc28fb8 | 2010-11-11 10:04:58 +0000 | [diff] [blame] | 146 | }, |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | int snd_soc_cache_init(struct snd_soc_codec *codec) |
| 150 | { |
| 151 | int i; |
| 152 | |
| 153 | for (i = 0; i < ARRAY_SIZE(cache_types); ++i) |
Dimitris Papastamos | 23bbce3 | 2010-12-02 14:53:01 +0000 | [diff] [blame] | 154 | if (cache_types[i].id == codec->compress_type) |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 155 | break; |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 156 | |
| 157 | /* Fall back to flat compression */ |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 158 | if (i == ARRAY_SIZE(cache_types)) { |
Mark Brown | be4fcdd | 2010-12-21 17:09:48 +0000 | [diff] [blame] | 159 | dev_warn(codec->dev, "Could not match compress type: %d\n", |
| 160 | codec->compress_type); |
| 161 | i = 0; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | mutex_init(&codec->cache_rw_mutex); |
| 165 | codec->cache_ops = &cache_types[i]; |
| 166 | |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 167 | if (codec->cache_ops->init) { |
| 168 | if (codec->cache_ops->name) |
| 169 | dev_dbg(codec->dev, "Initializing %s cache for %s codec\n", |
| 170 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 171 | return codec->cache_ops->init(codec); |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 172 | } |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 173 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
| 177 | * NOTE: keep in mind that this function might be called |
| 178 | * multiple times. |
| 179 | */ |
| 180 | int snd_soc_cache_exit(struct snd_soc_codec *codec) |
| 181 | { |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 182 | if (codec->cache_ops && codec->cache_ops->exit) { |
| 183 | if (codec->cache_ops->name) |
| 184 | dev_dbg(codec->dev, "Destroying %s cache for %s codec\n", |
| 185 | codec->cache_ops->name, codec->name); |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 186 | return codec->cache_ops->exit(codec); |
Dimitris Papastamos | 0d735ea | 2010-12-06 09:51:57 +0000 | [diff] [blame] | 187 | } |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 188 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * snd_soc_cache_read: Fetch the value of a given register from the cache. |
| 193 | * |
| 194 | * @codec: CODEC to configure. |
| 195 | * @reg: The register index. |
| 196 | * @value: The value to be returned. |
| 197 | */ |
| 198 | int snd_soc_cache_read(struct snd_soc_codec *codec, |
| 199 | unsigned int reg, unsigned int *value) |
| 200 | { |
| 201 | int ret; |
| 202 | |
| 203 | mutex_lock(&codec->cache_rw_mutex); |
| 204 | |
| 205 | if (value && codec->cache_ops && codec->cache_ops->read) { |
| 206 | ret = codec->cache_ops->read(codec, reg, value); |
| 207 | mutex_unlock(&codec->cache_rw_mutex); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | mutex_unlock(&codec->cache_rw_mutex); |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 212 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 213 | } |
| 214 | EXPORT_SYMBOL_GPL(snd_soc_cache_read); |
| 215 | |
| 216 | /** |
| 217 | * snd_soc_cache_write: Set the value of a given register in the cache. |
| 218 | * |
| 219 | * @codec: CODEC to configure. |
| 220 | * @reg: The register index. |
| 221 | * @value: The new register value. |
| 222 | */ |
| 223 | int snd_soc_cache_write(struct snd_soc_codec *codec, |
| 224 | unsigned int reg, unsigned int value) |
| 225 | { |
| 226 | int ret; |
| 227 | |
| 228 | mutex_lock(&codec->cache_rw_mutex); |
| 229 | |
| 230 | if (codec->cache_ops && codec->cache_ops->write) { |
| 231 | ret = codec->cache_ops->write(codec, reg, value); |
| 232 | mutex_unlock(&codec->cache_rw_mutex); |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | mutex_unlock(&codec->cache_rw_mutex); |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 237 | return -ENOSYS; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 238 | } |
| 239 | EXPORT_SYMBOL_GPL(snd_soc_cache_write); |
| 240 | |
| 241 | /** |
| 242 | * snd_soc_cache_sync: Sync the register cache with the hardware. |
| 243 | * |
| 244 | * @codec: CODEC to configure. |
| 245 | * |
| 246 | * Any registers that should not be synced should be marked as |
| 247 | * volatile. In general drivers can choose not to use the provided |
| 248 | * syncing functionality if they so require. |
| 249 | */ |
| 250 | int snd_soc_cache_sync(struct snd_soc_codec *codec) |
| 251 | { |
| 252 | int ret; |
Dimitris Papastamos | c358e64 | 2011-01-21 15:29:02 +0000 | [diff] [blame] | 253 | const char *name; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 254 | |
| 255 | if (!codec->cache_sync) { |
| 256 | return 0; |
| 257 | } |
| 258 | |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 259 | if (!codec->cache_ops || !codec->cache_ops->sync) |
Dimitris Papastamos | acd6145 | 2011-03-22 10:48:49 +0000 | [diff] [blame] | 260 | return -ENOSYS; |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 261 | |
Dimitris Papastamos | c358e64 | 2011-01-21 15:29:02 +0000 | [diff] [blame] | 262 | if (codec->cache_ops->name) |
| 263 | name = codec->cache_ops->name; |
| 264 | else |
| 265 | name = "unknown"; |
| 266 | |
Dan Carpenter | 46fdaa3 | 2011-02-07 22:01:41 +0300 | [diff] [blame] | 267 | if (codec->cache_ops->name) |
| 268 | dev_dbg(codec->dev, "Syncing %s cache for %s codec\n", |
| 269 | codec->cache_ops->name, codec->name); |
| 270 | trace_snd_soc_cache_sync(codec, name, "start"); |
| 271 | ret = codec->cache_ops->sync(codec); |
| 272 | if (!ret) |
| 273 | codec->cache_sync = 0; |
| 274 | trace_snd_soc_cache_sync(codec, name, "end"); |
| 275 | return ret; |
Dimitris Papastamos | 7a30a3d | 2010-11-11 10:04:57 +0000 | [diff] [blame] | 276 | } |
| 277 | EXPORT_SYMBOL_GPL(snd_soc_cache_sync); |
Dimitris Papastamos | 066d16c | 2011-01-13 12:20:36 +0000 | [diff] [blame] | 278 | |
| 279 | static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec, |
| 280 | unsigned int reg) |
| 281 | { |
| 282 | const struct snd_soc_codec_driver *codec_drv; |
| 283 | unsigned int min, max, index; |
| 284 | |
| 285 | codec_drv = codec->driver; |
| 286 | min = 0; |
| 287 | max = codec_drv->reg_access_size - 1; |
| 288 | do { |
| 289 | index = (min + max) / 2; |
| 290 | if (codec_drv->reg_access_default[index].reg == reg) |
| 291 | return index; |
| 292 | if (codec_drv->reg_access_default[index].reg < reg) |
| 293 | min = index + 1; |
| 294 | else |
| 295 | max = index; |
| 296 | } while (min <= max); |
| 297 | return -1; |
| 298 | } |
| 299 | |
| 300 | int snd_soc_default_volatile_register(struct snd_soc_codec *codec, |
| 301 | unsigned int reg) |
| 302 | { |
| 303 | int index; |
| 304 | |
| 305 | if (reg >= codec->driver->reg_cache_size) |
| 306 | return 1; |
| 307 | index = snd_soc_get_reg_access_index(codec, reg); |
| 308 | if (index < 0) |
| 309 | return 0; |
| 310 | return codec->driver->reg_access_default[index].vol; |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register); |
| 313 | |
| 314 | int snd_soc_default_readable_register(struct snd_soc_codec *codec, |
| 315 | unsigned int reg) |
| 316 | { |
| 317 | int index; |
| 318 | |
| 319 | if (reg >= codec->driver->reg_cache_size) |
| 320 | return 1; |
| 321 | index = snd_soc_get_reg_access_index(codec, reg); |
| 322 | if (index < 0) |
| 323 | return 0; |
| 324 | return codec->driver->reg_access_default[index].read; |
| 325 | } |
| 326 | EXPORT_SYMBOL_GPL(snd_soc_default_readable_register); |
Dimitris Papastamos | 8020454 | 2011-03-24 13:45:17 +0000 | [diff] [blame] | 327 | |
| 328 | int snd_soc_default_writable_register(struct snd_soc_codec *codec, |
| 329 | unsigned int reg) |
| 330 | { |
| 331 | int index; |
| 332 | |
| 333 | if (reg >= codec->driver->reg_cache_size) |
| 334 | return 1; |
| 335 | index = snd_soc_get_reg_access_index(codec, reg); |
| 336 | if (index < 0) |
| 337 | return 0; |
| 338 | return codec->driver->reg_access_default[index].write; |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(snd_soc_default_writable_register); |