Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Register map access API |
| 3 | * |
| 4 | * Copyright 2011 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 |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
Stephen Warren | f5d6eba | 2012-03-09 13:17:28 -0700 | [diff] [blame] | 13 | #include <linux/device.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 14 | #include <linux/slab.h> |
Paul Gortmaker | 19694b5 | 2012-02-28 19:28:02 -0500 | [diff] [blame] | 15 | #include <linux/export.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 16 | #include <linux/mutex.h> |
| 17 | #include <linux/err.h> |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 18 | #include <linux/rbtree.h> |
Stephen Warren | 30b2a55 | 2013-02-02 22:50:14 -0700 | [diff] [blame] | 19 | #include <linux/sched.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 20 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 21 | #define CREATE_TRACE_POINTS |
| 22 | #include <trace/events/regmap.h> |
| 23 | |
Mark Brown | 93de912 | 2011-07-20 22:35:37 +0100 | [diff] [blame] | 24 | #include "internal.h" |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 25 | |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 26 | /* |
| 27 | * Sometimes for failures during very early init the trace |
| 28 | * infrastructure isn't available early enough to be used. For this |
| 29 | * sort of problem defining LOG_DEVICE will add printks for basic |
| 30 | * register I/O on a specific device. |
| 31 | */ |
| 32 | #undef LOG_DEVICE |
| 33 | |
| 34 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 35 | unsigned int mask, unsigned int val, |
| 36 | bool *change); |
| 37 | |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 38 | static int _regmap_bus_read(void *context, unsigned int reg, |
| 39 | unsigned int *val); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 40 | static int _regmap_bus_formatted_write(void *context, unsigned int reg, |
| 41 | unsigned int val); |
| 42 | static int _regmap_bus_raw_write(void *context, unsigned int reg, |
| 43 | unsigned int val); |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 44 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 45 | bool regmap_reg_in_ranges(unsigned int reg, |
| 46 | const struct regmap_range *ranges, |
| 47 | unsigned int nranges) |
| 48 | { |
| 49 | const struct regmap_range *r; |
| 50 | int i; |
| 51 | |
| 52 | for (i = 0, r = ranges; i < nranges; i++, r++) |
| 53 | if (regmap_reg_in_range(reg, r)) |
| 54 | return true; |
| 55 | return false; |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(regmap_reg_in_ranges); |
| 58 | |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 59 | bool regmap_check_range_table(struct regmap *map, unsigned int reg, |
| 60 | const struct regmap_access_table *table) |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 61 | { |
| 62 | /* Check "no ranges" first */ |
| 63 | if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges)) |
| 64 | return false; |
| 65 | |
| 66 | /* In case zero "yes ranges" are supplied, any reg is OK */ |
| 67 | if (!table->n_yes_ranges) |
| 68 | return true; |
| 69 | |
| 70 | return regmap_reg_in_ranges(reg, table->yes_ranges, |
| 71 | table->n_yes_ranges); |
| 72 | } |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(regmap_check_range_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 74 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 75 | bool regmap_writeable(struct regmap *map, unsigned int reg) |
| 76 | { |
| 77 | if (map->max_register && reg > map->max_register) |
| 78 | return false; |
| 79 | |
| 80 | if (map->writeable_reg) |
| 81 | return map->writeable_reg(map->dev, reg); |
| 82 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 83 | if (map->wr_table) |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 84 | return regmap_check_range_table(map, reg, map->wr_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 85 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool regmap_readable(struct regmap *map, unsigned int reg) |
| 90 | { |
| 91 | if (map->max_register && reg > map->max_register) |
| 92 | return false; |
| 93 | |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 94 | if (map->format.format_write) |
| 95 | return false; |
| 96 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 97 | if (map->readable_reg) |
| 98 | return map->readable_reg(map->dev, reg); |
| 99 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 100 | if (map->rd_table) |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 101 | return regmap_check_range_table(map, reg, map->rd_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 102 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool regmap_volatile(struct regmap *map, unsigned int reg) |
| 107 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 108 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 109 | return false; |
| 110 | |
| 111 | if (map->volatile_reg) |
| 112 | return map->volatile_reg(map->dev, reg); |
| 113 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 114 | if (map->volatile_table) |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 115 | return regmap_check_range_table(map, reg, map->volatile_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 116 | |
Mark Brown | b92be6f | 2013-06-03 17:24:08 +0100 | [diff] [blame] | 117 | if (map->cache_ops) |
| 118 | return false; |
| 119 | else |
| 120 | return true; |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool regmap_precious(struct regmap *map, unsigned int reg) |
| 124 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 125 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 126 | return false; |
| 127 | |
| 128 | if (map->precious_reg) |
| 129 | return map->precious_reg(map->dev, reg); |
| 130 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 131 | if (map->precious_table) |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 132 | return regmap_check_range_table(map, reg, map->precious_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 133 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 137 | static bool regmap_volatile_range(struct regmap *map, unsigned int reg, |
Paul Bolle | a8f28cf | 2012-10-08 22:06:30 +0200 | [diff] [blame] | 138 | size_t num) |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 139 | { |
| 140 | unsigned int i; |
| 141 | |
| 142 | for (i = 0; i < num; i++) |
| 143 | if (!regmap_volatile(map, reg + i)) |
| 144 | return false; |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 149 | static void regmap_format_2_6_write(struct regmap *map, |
| 150 | unsigned int reg, unsigned int val) |
| 151 | { |
| 152 | u8 *out = map->work_buf; |
| 153 | |
| 154 | *out = (reg << 6) | val; |
| 155 | } |
| 156 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 157 | static void regmap_format_4_12_write(struct regmap *map, |
| 158 | unsigned int reg, unsigned int val) |
| 159 | { |
| 160 | __be16 *out = map->work_buf; |
| 161 | *out = cpu_to_be16((reg << 12) | val); |
| 162 | } |
| 163 | |
| 164 | static void regmap_format_7_9_write(struct regmap *map, |
| 165 | unsigned int reg, unsigned int val) |
| 166 | { |
| 167 | __be16 *out = map->work_buf; |
| 168 | *out = cpu_to_be16((reg << 9) | val); |
| 169 | } |
| 170 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 171 | static void regmap_format_10_14_write(struct regmap *map, |
| 172 | unsigned int reg, unsigned int val) |
| 173 | { |
| 174 | u8 *out = map->work_buf; |
| 175 | |
| 176 | out[2] = val; |
| 177 | out[1] = (val >> 8) | (reg << 6); |
| 178 | out[0] = reg >> 2; |
| 179 | } |
| 180 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 181 | static void regmap_format_8(void *buf, unsigned int val, unsigned int shift) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 182 | { |
| 183 | u8 *b = buf; |
| 184 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 185 | b[0] = val << shift; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 188 | static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 189 | { |
| 190 | __be16 *b = buf; |
| 191 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 192 | b[0] = cpu_to_be16(val << shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 195 | static void regmap_format_16_native(void *buf, unsigned int val, |
| 196 | unsigned int shift) |
| 197 | { |
| 198 | *(u16 *)buf = val << shift; |
| 199 | } |
| 200 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 201 | static void regmap_format_24(void *buf, unsigned int val, unsigned int shift) |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 202 | { |
| 203 | u8 *b = buf; |
| 204 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 205 | val <<= shift; |
| 206 | |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 207 | b[0] = val >> 16; |
| 208 | b[1] = val >> 8; |
| 209 | b[2] = val; |
| 210 | } |
| 211 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 212 | static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift) |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 213 | { |
| 214 | __be32 *b = buf; |
| 215 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 216 | b[0] = cpu_to_be32(val << shift); |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 219 | static void regmap_format_32_native(void *buf, unsigned int val, |
| 220 | unsigned int shift) |
| 221 | { |
| 222 | *(u32 *)buf = val << shift; |
| 223 | } |
| 224 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 225 | static void regmap_parse_inplace_noop(void *buf) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 226 | { |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static unsigned int regmap_parse_8(const void *buf) |
| 230 | { |
| 231 | const u8 *b = buf; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 232 | |
| 233 | return b[0]; |
| 234 | } |
| 235 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 236 | static unsigned int regmap_parse_16_be(const void *buf) |
| 237 | { |
| 238 | const __be16 *b = buf; |
| 239 | |
| 240 | return be16_to_cpu(b[0]); |
| 241 | } |
| 242 | |
| 243 | static void regmap_parse_16_be_inplace(void *buf) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 244 | { |
| 245 | __be16 *b = buf; |
| 246 | |
| 247 | b[0] = be16_to_cpu(b[0]); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 250 | static unsigned int regmap_parse_16_native(const void *buf) |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 251 | { |
| 252 | return *(u16 *)buf; |
| 253 | } |
| 254 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 255 | static unsigned int regmap_parse_24(const void *buf) |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 256 | { |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 257 | const u8 *b = buf; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 258 | unsigned int ret = b[2]; |
| 259 | ret |= ((unsigned int)b[1]) << 8; |
| 260 | ret |= ((unsigned int)b[0]) << 16; |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 265 | static unsigned int regmap_parse_32_be(const void *buf) |
| 266 | { |
| 267 | const __be32 *b = buf; |
| 268 | |
| 269 | return be32_to_cpu(b[0]); |
| 270 | } |
| 271 | |
| 272 | static void regmap_parse_32_be_inplace(void *buf) |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 273 | { |
| 274 | __be32 *b = buf; |
| 275 | |
| 276 | b[0] = be32_to_cpu(b[0]); |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 279 | static unsigned int regmap_parse_32_native(const void *buf) |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 280 | { |
| 281 | return *(u32 *)buf; |
| 282 | } |
| 283 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 284 | static void regmap_lock_mutex(void *__map) |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 285 | { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 286 | struct regmap *map = __map; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 287 | mutex_lock(&map->mutex); |
| 288 | } |
| 289 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 290 | static void regmap_unlock_mutex(void *__map) |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 291 | { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 292 | struct regmap *map = __map; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 293 | mutex_unlock(&map->mutex); |
| 294 | } |
| 295 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 296 | static void regmap_lock_spinlock(void *__map) |
Fabio Estevam | b4519c7 | 2013-07-16 02:10:11 -0300 | [diff] [blame] | 297 | __acquires(&map->spinlock) |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 298 | { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 299 | struct regmap *map = __map; |
Lars-Peter Clausen | 92ab1aa | 2013-05-24 10:29:22 +0200 | [diff] [blame] | 300 | unsigned long flags; |
| 301 | |
| 302 | spin_lock_irqsave(&map->spinlock, flags); |
| 303 | map->spinlock_flags = flags; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 304 | } |
| 305 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 306 | static void regmap_unlock_spinlock(void *__map) |
Fabio Estevam | b4519c7 | 2013-07-16 02:10:11 -0300 | [diff] [blame] | 307 | __releases(&map->spinlock) |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 308 | { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 309 | struct regmap *map = __map; |
Lars-Peter Clausen | 92ab1aa | 2013-05-24 10:29:22 +0200 | [diff] [blame] | 310 | spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags); |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 311 | } |
| 312 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 313 | static void dev_get_regmap_release(struct device *dev, void *res) |
| 314 | { |
| 315 | /* |
| 316 | * We don't actually have anything to do here; the goal here |
| 317 | * is not to manage the regmap but to provide a simple way to |
| 318 | * get the regmap back given a struct device. |
| 319 | */ |
| 320 | } |
| 321 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 322 | static bool _regmap_range_add(struct regmap *map, |
| 323 | struct regmap_range_node *data) |
| 324 | { |
| 325 | struct rb_root *root = &map->range_tree; |
| 326 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 327 | |
| 328 | while (*new) { |
| 329 | struct regmap_range_node *this = |
| 330 | container_of(*new, struct regmap_range_node, node); |
| 331 | |
| 332 | parent = *new; |
| 333 | if (data->range_max < this->range_min) |
| 334 | new = &((*new)->rb_left); |
| 335 | else if (data->range_min > this->range_max) |
| 336 | new = &((*new)->rb_right); |
| 337 | else |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | rb_link_node(&data->node, parent, new); |
| 342 | rb_insert_color(&data->node, root); |
| 343 | |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | static struct regmap_range_node *_regmap_range_lookup(struct regmap *map, |
| 348 | unsigned int reg) |
| 349 | { |
| 350 | struct rb_node *node = map->range_tree.rb_node; |
| 351 | |
| 352 | while (node) { |
| 353 | struct regmap_range_node *this = |
| 354 | container_of(node, struct regmap_range_node, node); |
| 355 | |
| 356 | if (reg < this->range_min) |
| 357 | node = node->rb_left; |
| 358 | else if (reg > this->range_max) |
| 359 | node = node->rb_right; |
| 360 | else |
| 361 | return this; |
| 362 | } |
| 363 | |
| 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | static void regmap_range_exit(struct regmap *map) |
| 368 | { |
| 369 | struct rb_node *next; |
| 370 | struct regmap_range_node *range_node; |
| 371 | |
| 372 | next = rb_first(&map->range_tree); |
| 373 | while (next) { |
| 374 | range_node = rb_entry(next, struct regmap_range_node, node); |
| 375 | next = rb_next(&range_node->node); |
| 376 | rb_erase(&range_node->node, &map->range_tree); |
| 377 | kfree(range_node); |
| 378 | } |
| 379 | |
| 380 | kfree(map->selector_work_buf); |
| 381 | } |
| 382 | |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame^] | 383 | int regmap_attach_dev(struct device *dev, struct regmap *map, |
| 384 | const struct regmap_config *config) |
| 385 | { |
| 386 | struct regmap **m; |
| 387 | |
| 388 | map->dev = dev; |
| 389 | |
| 390 | regmap_debugfs_init(map, config->name); |
| 391 | |
| 392 | /* Add a devres resource for dev_get_regmap() */ |
| 393 | m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL); |
| 394 | if (!m) { |
| 395 | regmap_debugfs_exit(map); |
| 396 | return -ENOMEM; |
| 397 | } |
| 398 | *m = map; |
| 399 | devres_add(dev, m); |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | EXPORT_SYMBOL_GPL(regmap_attach_dev); |
| 404 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 405 | /** |
| 406 | * regmap_init(): Initialise register map |
| 407 | * |
| 408 | * @dev: Device that will be interacted with |
| 409 | * @bus: Bus-specific callbacks to use with device |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 410 | * @bus_context: Data passed to bus-specific callbacks |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 411 | * @config: Configuration for register map |
| 412 | * |
| 413 | * The return value will be an ERR_PTR() on error or a valid pointer to |
| 414 | * a struct regmap. This function should generally not be called |
| 415 | * directly, it should be called by bus-specific init functions. |
| 416 | */ |
| 417 | struct regmap *regmap_init(struct device *dev, |
| 418 | const struct regmap_bus *bus, |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 419 | void *bus_context, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 420 | const struct regmap_config *config) |
| 421 | { |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame^] | 422 | struct regmap *map; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 423 | int ret = -EINVAL; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 424 | enum regmap_endian reg_endian, val_endian; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 425 | int i, j; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 426 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 427 | if (!config) |
Lars-Peter Clausen | abbb18f | 2011-11-14 10:40:15 +0100 | [diff] [blame] | 428 | goto err; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 429 | |
| 430 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 431 | if (map == NULL) { |
| 432 | ret = -ENOMEM; |
| 433 | goto err; |
| 434 | } |
| 435 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 436 | if (config->lock && config->unlock) { |
| 437 | map->lock = config->lock; |
| 438 | map->unlock = config->unlock; |
| 439 | map->lock_arg = config->lock_arg; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 440 | } else { |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 441 | if ((bus && bus->fast_io) || |
| 442 | config->fast_io) { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 443 | spin_lock_init(&map->spinlock); |
| 444 | map->lock = regmap_lock_spinlock; |
| 445 | map->unlock = regmap_unlock_spinlock; |
| 446 | } else { |
| 447 | mutex_init(&map->mutex); |
| 448 | map->lock = regmap_lock_mutex; |
| 449 | map->unlock = regmap_unlock_mutex; |
| 450 | } |
| 451 | map->lock_arg = map; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 452 | } |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 453 | map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 454 | map->format.pad_bytes = config->pad_bits / 8; |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 455 | map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); |
Fabio Estevam | 5494a98 | 2012-05-31 21:10:30 -0300 | [diff] [blame] | 456 | map->format.buf_size = DIV_ROUND_UP(config->reg_bits + |
| 457 | config->val_bits + config->pad_bits, 8); |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 458 | map->reg_shift = config->pad_bits % 8; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 459 | if (config->reg_stride) |
| 460 | map->reg_stride = config->reg_stride; |
| 461 | else |
| 462 | map->reg_stride = 1; |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 463 | map->use_single_rw = config->use_single_rw; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 464 | map->dev = dev; |
| 465 | map->bus = bus; |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 466 | map->bus_context = bus_context; |
Mark Brown | 2e2ae66 | 2011-07-20 22:33:39 +0100 | [diff] [blame] | 467 | map->max_register = config->max_register; |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 468 | map->wr_table = config->wr_table; |
| 469 | map->rd_table = config->rd_table; |
| 470 | map->volatile_table = config->volatile_table; |
| 471 | map->precious_table = config->precious_table; |
Mark Brown | 2e2ae66 | 2011-07-20 22:33:39 +0100 | [diff] [blame] | 472 | map->writeable_reg = config->writeable_reg; |
| 473 | map->readable_reg = config->readable_reg; |
| 474 | map->volatile_reg = config->volatile_reg; |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 475 | map->precious_reg = config->precious_reg; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 476 | map->cache_type = config->cache_type; |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 477 | map->name = config->name; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 478 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 479 | spin_lock_init(&map->async_lock); |
| 480 | INIT_LIST_HEAD(&map->async_list); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 481 | INIT_LIST_HEAD(&map->async_free); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 482 | init_waitqueue_head(&map->async_waitq); |
| 483 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 484 | if (config->read_flag_mask || config->write_flag_mask) { |
| 485 | map->read_flag_mask = config->read_flag_mask; |
| 486 | map->write_flag_mask = config->write_flag_mask; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 487 | } else if (bus) { |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 488 | map->read_flag_mask = bus->read_flag_mask; |
| 489 | } |
| 490 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 491 | if (!bus) { |
| 492 | map->reg_read = config->reg_read; |
| 493 | map->reg_write = config->reg_write; |
| 494 | |
| 495 | map->defer_caching = false; |
| 496 | goto skip_format_initialization; |
| 497 | } else { |
| 498 | map->reg_read = _regmap_bus_read; |
| 499 | } |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 500 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 501 | reg_endian = config->reg_format_endian; |
| 502 | if (reg_endian == REGMAP_ENDIAN_DEFAULT) |
| 503 | reg_endian = bus->reg_format_endian_default; |
| 504 | if (reg_endian == REGMAP_ENDIAN_DEFAULT) |
| 505 | reg_endian = REGMAP_ENDIAN_BIG; |
| 506 | |
| 507 | val_endian = config->val_format_endian; |
| 508 | if (val_endian == REGMAP_ENDIAN_DEFAULT) |
| 509 | val_endian = bus->val_format_endian_default; |
| 510 | if (val_endian == REGMAP_ENDIAN_DEFAULT) |
| 511 | val_endian = REGMAP_ENDIAN_BIG; |
| 512 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 513 | switch (config->reg_bits + map->reg_shift) { |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 514 | case 2: |
| 515 | switch (config->val_bits) { |
| 516 | case 6: |
| 517 | map->format.format_write = regmap_format_2_6_write; |
| 518 | break; |
| 519 | default: |
| 520 | goto err_map; |
| 521 | } |
| 522 | break; |
| 523 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 524 | case 4: |
| 525 | switch (config->val_bits) { |
| 526 | case 12: |
| 527 | map->format.format_write = regmap_format_4_12_write; |
| 528 | break; |
| 529 | default: |
| 530 | goto err_map; |
| 531 | } |
| 532 | break; |
| 533 | |
| 534 | case 7: |
| 535 | switch (config->val_bits) { |
| 536 | case 9: |
| 537 | map->format.format_write = regmap_format_7_9_write; |
| 538 | break; |
| 539 | default: |
| 540 | goto err_map; |
| 541 | } |
| 542 | break; |
| 543 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 544 | case 10: |
| 545 | switch (config->val_bits) { |
| 546 | case 14: |
| 547 | map->format.format_write = regmap_format_10_14_write; |
| 548 | break; |
| 549 | default: |
| 550 | goto err_map; |
| 551 | } |
| 552 | break; |
| 553 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 554 | case 8: |
| 555 | map->format.format_reg = regmap_format_8; |
| 556 | break; |
| 557 | |
| 558 | case 16: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 559 | switch (reg_endian) { |
| 560 | case REGMAP_ENDIAN_BIG: |
| 561 | map->format.format_reg = regmap_format_16_be; |
| 562 | break; |
| 563 | case REGMAP_ENDIAN_NATIVE: |
| 564 | map->format.format_reg = regmap_format_16_native; |
| 565 | break; |
| 566 | default: |
| 567 | goto err_map; |
| 568 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 569 | break; |
| 570 | |
Lars-Peter Clausen | 237019e | 2013-01-10 17:06:10 +0100 | [diff] [blame] | 571 | case 24: |
| 572 | if (reg_endian != REGMAP_ENDIAN_BIG) |
| 573 | goto err_map; |
| 574 | map->format.format_reg = regmap_format_24; |
| 575 | break; |
| 576 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 577 | case 32: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 578 | switch (reg_endian) { |
| 579 | case REGMAP_ENDIAN_BIG: |
| 580 | map->format.format_reg = regmap_format_32_be; |
| 581 | break; |
| 582 | case REGMAP_ENDIAN_NATIVE: |
| 583 | map->format.format_reg = regmap_format_32_native; |
| 584 | break; |
| 585 | default: |
| 586 | goto err_map; |
| 587 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 588 | break; |
| 589 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 590 | default: |
| 591 | goto err_map; |
| 592 | } |
| 593 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 594 | if (val_endian == REGMAP_ENDIAN_NATIVE) |
| 595 | map->format.parse_inplace = regmap_parse_inplace_noop; |
| 596 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 597 | switch (config->val_bits) { |
| 598 | case 8: |
| 599 | map->format.format_val = regmap_format_8; |
| 600 | map->format.parse_val = regmap_parse_8; |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 601 | map->format.parse_inplace = regmap_parse_inplace_noop; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 602 | break; |
| 603 | case 16: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 604 | switch (val_endian) { |
| 605 | case REGMAP_ENDIAN_BIG: |
| 606 | map->format.format_val = regmap_format_16_be; |
| 607 | map->format.parse_val = regmap_parse_16_be; |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 608 | map->format.parse_inplace = regmap_parse_16_be_inplace; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 609 | break; |
| 610 | case REGMAP_ENDIAN_NATIVE: |
| 611 | map->format.format_val = regmap_format_16_native; |
| 612 | map->format.parse_val = regmap_parse_16_native; |
| 613 | break; |
| 614 | default: |
| 615 | goto err_map; |
| 616 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 617 | break; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 618 | case 24: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 619 | if (val_endian != REGMAP_ENDIAN_BIG) |
| 620 | goto err_map; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 621 | map->format.format_val = regmap_format_24; |
| 622 | map->format.parse_val = regmap_parse_24; |
| 623 | break; |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 624 | case 32: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 625 | switch (val_endian) { |
| 626 | case REGMAP_ENDIAN_BIG: |
| 627 | map->format.format_val = regmap_format_32_be; |
| 628 | map->format.parse_val = regmap_parse_32_be; |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 629 | map->format.parse_inplace = regmap_parse_32_be_inplace; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 630 | break; |
| 631 | case REGMAP_ENDIAN_NATIVE: |
| 632 | map->format.format_val = regmap_format_32_native; |
| 633 | map->format.parse_val = regmap_parse_32_native; |
| 634 | break; |
| 635 | default: |
| 636 | goto err_map; |
| 637 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 638 | break; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 641 | if (map->format.format_write) { |
| 642 | if ((reg_endian != REGMAP_ENDIAN_BIG) || |
| 643 | (val_endian != REGMAP_ENDIAN_BIG)) |
| 644 | goto err_map; |
Mark Brown | 7a64761 | 2012-04-30 23:26:32 +0100 | [diff] [blame] | 645 | map->use_single_rw = true; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 646 | } |
Mark Brown | 7a64761 | 2012-04-30 23:26:32 +0100 | [diff] [blame] | 647 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 648 | if (!map->format.format_write && |
| 649 | !(map->format.format_reg && map->format.format_val)) |
| 650 | goto err_map; |
| 651 | |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 652 | map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 653 | if (map->work_buf == NULL) { |
| 654 | ret = -ENOMEM; |
Mark Brown | 5204f5e | 2011-09-05 08:07:47 -0700 | [diff] [blame] | 655 | goto err_map; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 656 | } |
| 657 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 658 | if (map->format.format_write) { |
| 659 | map->defer_caching = false; |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 660 | map->reg_write = _regmap_bus_formatted_write; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 661 | } else if (map->format.format_val) { |
| 662 | map->defer_caching = true; |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 663 | map->reg_write = _regmap_bus_raw_write; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | skip_format_initialization: |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 667 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 668 | map->range_tree = RB_ROOT; |
Mark Brown | e3549cd | 2012-10-02 20:17:15 +0100 | [diff] [blame] | 669 | for (i = 0; i < config->num_ranges; i++) { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 670 | const struct regmap_range_cfg *range_cfg = &config->ranges[i]; |
| 671 | struct regmap_range_node *new; |
| 672 | |
| 673 | /* Sanity check */ |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 674 | if (range_cfg->range_max < range_cfg->range_min) { |
| 675 | dev_err(map->dev, "Invalid range %d: %d < %d\n", i, |
| 676 | range_cfg->range_max, range_cfg->range_min); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 677 | goto err_range; |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | if (range_cfg->range_max > map->max_register) { |
| 681 | dev_err(map->dev, "Invalid range %d: %d > %d\n", i, |
| 682 | range_cfg->range_max, map->max_register); |
| 683 | goto err_range; |
| 684 | } |
| 685 | |
| 686 | if (range_cfg->selector_reg > map->max_register) { |
| 687 | dev_err(map->dev, |
| 688 | "Invalid range %d: selector out of map\n", i); |
| 689 | goto err_range; |
| 690 | } |
| 691 | |
| 692 | if (range_cfg->window_len == 0) { |
| 693 | dev_err(map->dev, "Invalid range %d: window_len 0\n", |
| 694 | i); |
| 695 | goto err_range; |
| 696 | } |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 697 | |
| 698 | /* Make sure, that this register range has no selector |
| 699 | or data window within its boundary */ |
Mark Brown | e3549cd | 2012-10-02 20:17:15 +0100 | [diff] [blame] | 700 | for (j = 0; j < config->num_ranges; j++) { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 701 | unsigned sel_reg = config->ranges[j].selector_reg; |
| 702 | unsigned win_min = config->ranges[j].window_start; |
| 703 | unsigned win_max = win_min + |
| 704 | config->ranges[j].window_len - 1; |
| 705 | |
Philipp Zabel | f161d22 | 2013-07-23 12:16:02 +0200 | [diff] [blame] | 706 | /* Allow data window inside its own virtual range */ |
| 707 | if (j == i) |
| 708 | continue; |
| 709 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 710 | if (range_cfg->range_min <= sel_reg && |
| 711 | sel_reg <= range_cfg->range_max) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 712 | dev_err(map->dev, |
| 713 | "Range %d: selector for %d in window\n", |
| 714 | i, j); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 715 | goto err_range; |
| 716 | } |
| 717 | |
| 718 | if (!(win_max < range_cfg->range_min || |
| 719 | win_min > range_cfg->range_max)) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 720 | dev_err(map->dev, |
| 721 | "Range %d: window for %d in window\n", |
| 722 | i, j); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 723 | goto err_range; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 728 | if (new == NULL) { |
| 729 | ret = -ENOMEM; |
| 730 | goto err_range; |
| 731 | } |
| 732 | |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 733 | new->map = map; |
Mark Brown | d058bb4 | 2012-10-03 12:40:47 +0100 | [diff] [blame] | 734 | new->name = range_cfg->name; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 735 | new->range_min = range_cfg->range_min; |
| 736 | new->range_max = range_cfg->range_max; |
| 737 | new->selector_reg = range_cfg->selector_reg; |
| 738 | new->selector_mask = range_cfg->selector_mask; |
| 739 | new->selector_shift = range_cfg->selector_shift; |
| 740 | new->window_start = range_cfg->window_start; |
| 741 | new->window_len = range_cfg->window_len; |
| 742 | |
| 743 | if (_regmap_range_add(map, new) == false) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 744 | dev_err(map->dev, "Failed to add range %d\n", i); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 745 | kfree(new); |
| 746 | goto err_range; |
| 747 | } |
| 748 | |
| 749 | if (map->selector_work_buf == NULL) { |
| 750 | map->selector_work_buf = |
| 751 | kzalloc(map->format.buf_size, GFP_KERNEL); |
| 752 | if (map->selector_work_buf == NULL) { |
| 753 | ret = -ENOMEM; |
| 754 | goto err_range; |
| 755 | } |
| 756 | } |
| 757 | } |
Mark Brown | 052d2cd | 2011-11-21 19:05:13 +0000 | [diff] [blame] | 758 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 759 | ret = regcache_init(map, config); |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 760 | if (ret != 0) |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 761 | goto err_range; |
| 762 | |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame^] | 763 | if (dev) |
| 764 | ret = regmap_attach_dev(dev, map, config); |
| 765 | if (ret != 0) |
| 766 | goto err_regcache; |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 767 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 768 | return map; |
| 769 | |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame^] | 770 | err_regcache: |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 771 | regcache_exit(map); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 772 | err_range: |
| 773 | regmap_range_exit(map); |
Lars-Peter Clausen | 58072cb | 2011-11-10 18:15:15 +0100 | [diff] [blame] | 774 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 775 | err_map: |
| 776 | kfree(map); |
| 777 | err: |
| 778 | return ERR_PTR(ret); |
| 779 | } |
| 780 | EXPORT_SYMBOL_GPL(regmap_init); |
| 781 | |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 782 | static void devm_regmap_release(struct device *dev, void *res) |
| 783 | { |
| 784 | regmap_exit(*(struct regmap **)res); |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * devm_regmap_init(): Initialise managed register map |
| 789 | * |
| 790 | * @dev: Device that will be interacted with |
| 791 | * @bus: Bus-specific callbacks to use with device |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 792 | * @bus_context: Data passed to bus-specific callbacks |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 793 | * @config: Configuration for register map |
| 794 | * |
| 795 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 796 | * to a struct regmap. This function should generally not be called |
| 797 | * directly, it should be called by bus-specific init functions. The |
| 798 | * map will be automatically freed by the device management code. |
| 799 | */ |
| 800 | struct regmap *devm_regmap_init(struct device *dev, |
| 801 | const struct regmap_bus *bus, |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 802 | void *bus_context, |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 803 | const struct regmap_config *config) |
| 804 | { |
| 805 | struct regmap **ptr, *regmap; |
| 806 | |
| 807 | ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); |
| 808 | if (!ptr) |
| 809 | return ERR_PTR(-ENOMEM); |
| 810 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 811 | regmap = regmap_init(dev, bus, bus_context, config); |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 812 | if (!IS_ERR(regmap)) { |
| 813 | *ptr = regmap; |
| 814 | devres_add(dev, ptr); |
| 815 | } else { |
| 816 | devres_free(ptr); |
| 817 | } |
| 818 | |
| 819 | return regmap; |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(devm_regmap_init); |
| 822 | |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 823 | static void regmap_field_init(struct regmap_field *rm_field, |
| 824 | struct regmap *regmap, struct reg_field reg_field) |
| 825 | { |
| 826 | int field_bits = reg_field.msb - reg_field.lsb + 1; |
| 827 | rm_field->regmap = regmap; |
| 828 | rm_field->reg = reg_field.reg; |
| 829 | rm_field->shift = reg_field.lsb; |
| 830 | rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb); |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 831 | rm_field->id_size = reg_field.id_size; |
| 832 | rm_field->id_offset = reg_field.id_offset; |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /** |
| 836 | * devm_regmap_field_alloc(): Allocate and initialise a register field |
| 837 | * in a register map. |
| 838 | * |
| 839 | * @dev: Device that will be interacted with |
| 840 | * @regmap: regmap bank in which this register field is located. |
| 841 | * @reg_field: Register field with in the bank. |
| 842 | * |
| 843 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 844 | * to a struct regmap_field. The regmap_field will be automatically freed |
| 845 | * by the device management code. |
| 846 | */ |
| 847 | struct regmap_field *devm_regmap_field_alloc(struct device *dev, |
| 848 | struct regmap *regmap, struct reg_field reg_field) |
| 849 | { |
| 850 | struct regmap_field *rm_field = devm_kzalloc(dev, |
| 851 | sizeof(*rm_field), GFP_KERNEL); |
| 852 | if (!rm_field) |
| 853 | return ERR_PTR(-ENOMEM); |
| 854 | |
| 855 | regmap_field_init(rm_field, regmap, reg_field); |
| 856 | |
| 857 | return rm_field; |
| 858 | |
| 859 | } |
| 860 | EXPORT_SYMBOL_GPL(devm_regmap_field_alloc); |
| 861 | |
| 862 | /** |
| 863 | * devm_regmap_field_free(): Free register field allocated using |
| 864 | * devm_regmap_field_alloc. Usally drivers need not call this function, |
| 865 | * as the memory allocated via devm will be freed as per device-driver |
| 866 | * life-cyle. |
| 867 | * |
| 868 | * @dev: Device that will be interacted with |
| 869 | * @field: regmap field which should be freed. |
| 870 | */ |
| 871 | void devm_regmap_field_free(struct device *dev, |
| 872 | struct regmap_field *field) |
| 873 | { |
| 874 | devm_kfree(dev, field); |
| 875 | } |
| 876 | EXPORT_SYMBOL_GPL(devm_regmap_field_free); |
| 877 | |
| 878 | /** |
| 879 | * regmap_field_alloc(): Allocate and initialise a register field |
| 880 | * in a register map. |
| 881 | * |
| 882 | * @regmap: regmap bank in which this register field is located. |
| 883 | * @reg_field: Register field with in the bank. |
| 884 | * |
| 885 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 886 | * to a struct regmap_field. The regmap_field should be freed by the |
| 887 | * user once its finished working with it using regmap_field_free(). |
| 888 | */ |
| 889 | struct regmap_field *regmap_field_alloc(struct regmap *regmap, |
| 890 | struct reg_field reg_field) |
| 891 | { |
| 892 | struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL); |
| 893 | |
| 894 | if (!rm_field) |
| 895 | return ERR_PTR(-ENOMEM); |
| 896 | |
| 897 | regmap_field_init(rm_field, regmap, reg_field); |
| 898 | |
| 899 | return rm_field; |
| 900 | } |
| 901 | EXPORT_SYMBOL_GPL(regmap_field_alloc); |
| 902 | |
| 903 | /** |
| 904 | * regmap_field_free(): Free register field allocated using regmap_field_alloc |
| 905 | * |
| 906 | * @field: regmap field which should be freed. |
| 907 | */ |
| 908 | void regmap_field_free(struct regmap_field *field) |
| 909 | { |
| 910 | kfree(field); |
| 911 | } |
| 912 | EXPORT_SYMBOL_GPL(regmap_field_free); |
| 913 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 914 | /** |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 915 | * regmap_reinit_cache(): Reinitialise the current register cache |
| 916 | * |
| 917 | * @map: Register map to operate on. |
| 918 | * @config: New configuration. Only the cache data will be used. |
| 919 | * |
| 920 | * Discard any existing register cache for the map and initialize a |
| 921 | * new cache. This can be used to restore the cache to defaults or to |
| 922 | * update the cache configuration to reflect runtime discovery of the |
| 923 | * hardware. |
Dimitris Papastamos | 4d87951 | 2012-07-27 14:54:15 +0100 | [diff] [blame] | 924 | * |
| 925 | * No explicit locking is done here, the user needs to ensure that |
| 926 | * this function will not race with other calls to regmap. |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 927 | */ |
| 928 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) |
| 929 | { |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 930 | regcache_exit(map); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 931 | regmap_debugfs_exit(map); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 932 | |
| 933 | map->max_register = config->max_register; |
| 934 | map->writeable_reg = config->writeable_reg; |
| 935 | map->readable_reg = config->readable_reg; |
| 936 | map->volatile_reg = config->volatile_reg; |
| 937 | map->precious_reg = config->precious_reg; |
| 938 | map->cache_type = config->cache_type; |
| 939 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 940 | regmap_debugfs_init(map, config->name); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 941 | |
Mark Brown | 421e8d2 | 2012-01-20 13:39:37 +0000 | [diff] [blame] | 942 | map->cache_bypass = false; |
| 943 | map->cache_only = false; |
| 944 | |
Dimitris Papastamos | 4d87951 | 2012-07-27 14:54:15 +0100 | [diff] [blame] | 945 | return regcache_init(map, config); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 946 | } |
Mark Brown | 752a6a5 | 2012-05-14 10:00:12 +0100 | [diff] [blame] | 947 | EXPORT_SYMBOL_GPL(regmap_reinit_cache); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 948 | |
| 949 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 950 | * regmap_exit(): Free a previously allocated register map |
| 951 | */ |
| 952 | void regmap_exit(struct regmap *map) |
| 953 | { |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 954 | struct regmap_async *async; |
| 955 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 956 | regcache_exit(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 957 | regmap_debugfs_exit(map); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 958 | regmap_range_exit(map); |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 959 | if (map->bus && map->bus->free_context) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 960 | map->bus->free_context(map->bus_context); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 961 | kfree(map->work_buf); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 962 | while (!list_empty(&map->async_free)) { |
| 963 | async = list_first_entry_or_null(&map->async_free, |
| 964 | struct regmap_async, |
| 965 | list); |
| 966 | list_del(&async->list); |
| 967 | kfree(async->work_buf); |
| 968 | kfree(async); |
| 969 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 970 | kfree(map); |
| 971 | } |
| 972 | EXPORT_SYMBOL_GPL(regmap_exit); |
| 973 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 974 | static int dev_get_regmap_match(struct device *dev, void *res, void *data) |
| 975 | { |
| 976 | struct regmap **r = res; |
| 977 | if (!r || !*r) { |
| 978 | WARN_ON(!r || !*r); |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | /* If the user didn't specify a name match any */ |
| 983 | if (data) |
| 984 | return (*r)->name == data; |
| 985 | else |
| 986 | return 1; |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * dev_get_regmap(): Obtain the regmap (if any) for a device |
| 991 | * |
| 992 | * @dev: Device to retrieve the map for |
| 993 | * @name: Optional name for the register map, usually NULL. |
| 994 | * |
| 995 | * Returns the regmap for the device if one is present, or NULL. If |
| 996 | * name is specified then it must match the name specified when |
| 997 | * registering the device, if it is NULL then the first regmap found |
| 998 | * will be used. Devices with multiple register maps are very rare, |
| 999 | * generic code should normally not need to specify a name. |
| 1000 | */ |
| 1001 | struct regmap *dev_get_regmap(struct device *dev, const char *name) |
| 1002 | { |
| 1003 | struct regmap **r = devres_find(dev, dev_get_regmap_release, |
| 1004 | dev_get_regmap_match, (void *)name); |
| 1005 | |
| 1006 | if (!r) |
| 1007 | return NULL; |
| 1008 | return *r; |
| 1009 | } |
| 1010 | EXPORT_SYMBOL_GPL(dev_get_regmap); |
| 1011 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1012 | static int _regmap_select_page(struct regmap *map, unsigned int *reg, |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1013 | struct regmap_range_node *range, |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1014 | unsigned int val_num) |
| 1015 | { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1016 | void *orig_work_buf; |
| 1017 | unsigned int win_offset; |
| 1018 | unsigned int win_page; |
| 1019 | bool page_chg; |
| 1020 | int ret; |
| 1021 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1022 | win_offset = (*reg - range->range_min) % range->window_len; |
| 1023 | win_page = (*reg - range->range_min) / range->window_len; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1024 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1025 | if (val_num > 1) { |
| 1026 | /* Bulk write shouldn't cross range boundary */ |
| 1027 | if (*reg + val_num - 1 > range->range_max) |
| 1028 | return -EINVAL; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1029 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1030 | /* ... or single page boundary */ |
| 1031 | if (val_num > range->window_len - win_offset) |
| 1032 | return -EINVAL; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1035 | /* It is possible to have selector register inside data window. |
| 1036 | In that case, selector register is located on every page and |
| 1037 | it needs no page switching, when accessed alone. */ |
| 1038 | if (val_num > 1 || |
| 1039 | range->window_start + win_offset != range->selector_reg) { |
| 1040 | /* Use separate work_buf during page switching */ |
| 1041 | orig_work_buf = map->work_buf; |
| 1042 | map->work_buf = map->selector_work_buf; |
| 1043 | |
| 1044 | ret = _regmap_update_bits(map, range->selector_reg, |
| 1045 | range->selector_mask, |
| 1046 | win_page << range->selector_shift, |
| 1047 | &page_chg); |
| 1048 | |
| 1049 | map->work_buf = orig_work_buf; |
| 1050 | |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 1051 | if (ret != 0) |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | *reg = range->window_start + win_offset; |
| 1056 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1057 | return 0; |
| 1058 | } |
| 1059 | |
Mark Brown | 584de32 | 2013-03-13 19:19:34 +0000 | [diff] [blame] | 1060 | int _regmap_raw_write(struct regmap *map, unsigned int reg, |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1061 | const void *val, size_t val_len) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1062 | { |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1063 | struct regmap_range_node *range; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1064 | unsigned long flags; |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1065 | u8 *u8 = map->work_buf; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1066 | void *work_val = map->work_buf + map->format.reg_bytes + |
| 1067 | map->format.pad_bytes; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1068 | void *buf; |
| 1069 | int ret = -ENOTSUPP; |
| 1070 | size_t len; |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1071 | int i; |
| 1072 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1073 | WARN_ON(!map->bus); |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1074 | |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1075 | /* Check for unwritable registers before we start */ |
| 1076 | if (map->writeable_reg) |
| 1077 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1078 | if (!map->writeable_reg(map->dev, |
| 1079 | reg + (i * map->reg_stride))) |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1080 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1081 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1082 | if (!map->cache_bypass && map->format.parse_val) { |
| 1083 | unsigned int ival; |
| 1084 | int val_bytes = map->format.val_bytes; |
| 1085 | for (i = 0; i < val_len / val_bytes; i++) { |
Stephen Warren | 5a08d15 | 2013-03-20 17:02:02 -0600 | [diff] [blame] | 1086 | ival = map->format.parse_val(val + (i * val_bytes)); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1087 | ret = regcache_write(map, reg + (i * map->reg_stride), |
| 1088 | ival); |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1089 | if (ret) { |
| 1090 | dev_err(map->dev, |
Mark Brown | 6d04b8a | 2012-10-26 19:05:32 +0100 | [diff] [blame] | 1091 | "Error in caching of register: %x ret: %d\n", |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1092 | reg + i, ret); |
| 1093 | return ret; |
| 1094 | } |
| 1095 | } |
| 1096 | if (map->cache_only) { |
| 1097 | map->cache_dirty = true; |
| 1098 | return 0; |
| 1099 | } |
| 1100 | } |
| 1101 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1102 | range = _regmap_range_lookup(map, reg); |
| 1103 | if (range) { |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1104 | int val_num = val_len / map->format.val_bytes; |
| 1105 | int win_offset = (reg - range->range_min) % range->window_len; |
| 1106 | int win_residue = range->window_len - win_offset; |
| 1107 | |
| 1108 | /* If the write goes beyond the end of the window split it */ |
| 1109 | while (val_num > win_residue) { |
Fabio Estevam | 1a61cfe | 2012-10-25 14:07:18 -0200 | [diff] [blame] | 1110 | dev_dbg(map->dev, "Writing window %d/%zu\n", |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1111 | win_residue, val_len / map->format.val_bytes); |
| 1112 | ret = _regmap_raw_write(map, reg, val, win_residue * |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1113 | map->format.val_bytes); |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1114 | if (ret != 0) |
| 1115 | return ret; |
| 1116 | |
| 1117 | reg += win_residue; |
| 1118 | val_num -= win_residue; |
| 1119 | val += win_residue * map->format.val_bytes; |
| 1120 | val_len -= win_residue * map->format.val_bytes; |
| 1121 | |
| 1122 | win_offset = (reg - range->range_min) % |
| 1123 | range->window_len; |
| 1124 | win_residue = range->window_len - win_offset; |
| 1125 | } |
| 1126 | |
| 1127 | ret = _regmap_select_page(map, ®, range, val_num); |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 1128 | if (ret != 0) |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1129 | return ret; |
| 1130 | } |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1131 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1132 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1133 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1134 | u8[0] |= map->write_flag_mask; |
| 1135 | |
Mark Brown | 651e013 | 2013-10-08 18:37:36 +0100 | [diff] [blame] | 1136 | /* |
| 1137 | * Essentially all I/O mechanisms will be faster with a single |
| 1138 | * buffer to write. Since register syncs often generate raw |
| 1139 | * writes of single registers optimise that case. |
| 1140 | */ |
| 1141 | if (val != work_val && val_len == map->format.val_bytes) { |
| 1142 | memcpy(work_val, val, map->format.val_bytes); |
| 1143 | val = work_val; |
| 1144 | } |
| 1145 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1146 | if (map->async && map->bus->async_write) { |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1147 | struct regmap_async *async; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1148 | |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 1149 | trace_regmap_async_write_start(map->dev, reg, val_len); |
| 1150 | |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1151 | spin_lock_irqsave(&map->async_lock, flags); |
| 1152 | async = list_first_entry_or_null(&map->async_free, |
| 1153 | struct regmap_async, |
| 1154 | list); |
| 1155 | if (async) |
| 1156 | list_del(&async->list); |
| 1157 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 1158 | |
| 1159 | if (!async) { |
| 1160 | async = map->bus->async_alloc(); |
| 1161 | if (!async) |
| 1162 | return -ENOMEM; |
| 1163 | |
| 1164 | async->work_buf = kzalloc(map->format.buf_size, |
| 1165 | GFP_KERNEL | GFP_DMA); |
| 1166 | if (!async->work_buf) { |
| 1167 | kfree(async); |
| 1168 | return -ENOMEM; |
| 1169 | } |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1172 | async->map = map; |
| 1173 | |
| 1174 | /* If the caller supplied the value we can use it safely. */ |
| 1175 | memcpy(async->work_buf, map->work_buf, map->format.pad_bytes + |
| 1176 | map->format.reg_bytes + map->format.val_bytes); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1177 | |
| 1178 | spin_lock_irqsave(&map->async_lock, flags); |
| 1179 | list_add_tail(&async->list, &map->async_list); |
| 1180 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 1181 | |
Mark Brown | 04c50cc | 2013-10-10 22:38:29 +0100 | [diff] [blame] | 1182 | if (val != work_val) |
| 1183 | ret = map->bus->async_write(map->bus_context, |
| 1184 | async->work_buf, |
| 1185 | map->format.reg_bytes + |
| 1186 | map->format.pad_bytes, |
| 1187 | val, val_len, async); |
| 1188 | else |
| 1189 | ret = map->bus->async_write(map->bus_context, |
| 1190 | async->work_buf, |
| 1191 | map->format.reg_bytes + |
| 1192 | map->format.pad_bytes + |
| 1193 | val_len, NULL, 0, async); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1194 | |
| 1195 | if (ret != 0) { |
| 1196 | dev_err(map->dev, "Failed to schedule write: %d\n", |
| 1197 | ret); |
| 1198 | |
| 1199 | spin_lock_irqsave(&map->async_lock, flags); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1200 | list_move(&async->list, &map->async_free); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1201 | spin_unlock_irqrestore(&map->async_lock, flags); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1202 | } |
Mark Brown | f951b65 | 2013-03-27 13:08:44 +0000 | [diff] [blame] | 1203 | |
| 1204 | return ret; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1205 | } |
| 1206 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1207 | trace_regmap_hw_write_start(map->dev, reg, |
| 1208 | val_len / map->format.val_bytes); |
| 1209 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1210 | /* If we're doing a single register write we can probably just |
| 1211 | * send the work_buf directly, otherwise try to do a gather |
| 1212 | * write. |
| 1213 | */ |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1214 | if (val == work_val) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1215 | ret = map->bus->write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1216 | map->format.reg_bytes + |
| 1217 | map->format.pad_bytes + |
| 1218 | val_len); |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1219 | else if (map->bus->gather_write) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1220 | ret = map->bus->gather_write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1221 | map->format.reg_bytes + |
| 1222 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1223 | val, val_len); |
| 1224 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1225 | /* If that didn't work fall back on linearising by hand. */ |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1226 | if (ret == -ENOTSUPP) { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1227 | len = map->format.reg_bytes + map->format.pad_bytes + val_len; |
| 1228 | buf = kzalloc(len, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1229 | if (!buf) |
| 1230 | return -ENOMEM; |
| 1231 | |
| 1232 | memcpy(buf, map->work_buf, map->format.reg_bytes); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1233 | memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, |
| 1234 | val, val_len); |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1235 | ret = map->bus->write(map->bus_context, buf, len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1236 | |
| 1237 | kfree(buf); |
| 1238 | } |
| 1239 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1240 | trace_regmap_hw_write_done(map->dev, reg, |
| 1241 | val_len / map->format.val_bytes); |
| 1242 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1243 | return ret; |
| 1244 | } |
| 1245 | |
Mark Brown | 221ad7f | 2013-03-26 21:24:20 +0000 | [diff] [blame] | 1246 | /** |
| 1247 | * regmap_can_raw_write - Test if regmap_raw_write() is supported |
| 1248 | * |
| 1249 | * @map: Map to check. |
| 1250 | */ |
| 1251 | bool regmap_can_raw_write(struct regmap *map) |
| 1252 | { |
| 1253 | return map->bus && map->format.format_val && map->format.format_reg; |
| 1254 | } |
| 1255 | EXPORT_SYMBOL_GPL(regmap_can_raw_write); |
| 1256 | |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1257 | static int _regmap_bus_formatted_write(void *context, unsigned int reg, |
| 1258 | unsigned int val) |
| 1259 | { |
| 1260 | int ret; |
| 1261 | struct regmap_range_node *range; |
| 1262 | struct regmap *map = context; |
| 1263 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1264 | WARN_ON(!map->bus || !map->format.format_write); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1265 | |
| 1266 | range = _regmap_range_lookup(map, reg); |
| 1267 | if (range) { |
| 1268 | ret = _regmap_select_page(map, ®, range, 1); |
| 1269 | if (ret != 0) |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
| 1273 | map->format.format_write(map, reg, val); |
| 1274 | |
| 1275 | trace_regmap_hw_write_start(map->dev, reg, 1); |
| 1276 | |
| 1277 | ret = map->bus->write(map->bus_context, map->work_buf, |
| 1278 | map->format.buf_size); |
| 1279 | |
| 1280 | trace_regmap_hw_write_done(map->dev, reg, 1); |
| 1281 | |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
| 1285 | static int _regmap_bus_raw_write(void *context, unsigned int reg, |
| 1286 | unsigned int val) |
| 1287 | { |
| 1288 | struct regmap *map = context; |
| 1289 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1290 | WARN_ON(!map->bus || !map->format.format_val); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1291 | |
| 1292 | map->format.format_val(map->work_buf + map->format.reg_bytes |
| 1293 | + map->format.pad_bytes, val, 0); |
| 1294 | return _regmap_raw_write(map, reg, |
| 1295 | map->work_buf + |
| 1296 | map->format.reg_bytes + |
| 1297 | map->format.pad_bytes, |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1298 | map->format.val_bytes); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1301 | static inline void *_regmap_map_get_context(struct regmap *map) |
| 1302 | { |
| 1303 | return (map->bus) ? map : map->bus_context; |
| 1304 | } |
| 1305 | |
Dimitris Papastamos | 4d2dc09 | 2011-09-29 10:39:07 +0100 | [diff] [blame] | 1306 | int _regmap_write(struct regmap *map, unsigned int reg, |
| 1307 | unsigned int val) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1308 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1309 | int ret; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1310 | void *context = _regmap_map_get_context(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1311 | |
Ionut Nicu | 515f226 | 2013-08-09 12:09:20 +0200 | [diff] [blame] | 1312 | if (!regmap_writeable(map, reg)) |
| 1313 | return -EIO; |
| 1314 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1315 | if (!map->cache_bypass && !map->defer_caching) { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1316 | ret = regcache_write(map, reg, val); |
| 1317 | if (ret != 0) |
| 1318 | return ret; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 1319 | if (map->cache_only) { |
| 1320 | map->cache_dirty = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1321 | return 0; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 1322 | } |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1323 | } |
| 1324 | |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 1325 | #ifdef LOG_DEVICE |
| 1326 | if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0) |
| 1327 | dev_info(map->dev, "%x <= %x\n", reg, val); |
| 1328 | #endif |
| 1329 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1330 | trace_regmap_reg_write(map->dev, reg, val); |
| 1331 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1332 | return map->reg_write(context, reg, val); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * regmap_write(): Write a value to a single register |
| 1337 | * |
| 1338 | * @map: Register map to write to |
| 1339 | * @reg: Register to write to |
| 1340 | * @val: Value to be written |
| 1341 | * |
| 1342 | * A value of zero will be returned on success, a negative errno will |
| 1343 | * be returned in error cases. |
| 1344 | */ |
| 1345 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) |
| 1346 | { |
| 1347 | int ret; |
| 1348 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1349 | if (reg % map->reg_stride) |
| 1350 | return -EINVAL; |
| 1351 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1352 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1353 | |
| 1354 | ret = _regmap_write(map, reg, val); |
| 1355 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1356 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1357 | |
| 1358 | return ret; |
| 1359 | } |
| 1360 | EXPORT_SYMBOL_GPL(regmap_write); |
| 1361 | |
| 1362 | /** |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 1363 | * regmap_write_async(): Write a value to a single register asynchronously |
| 1364 | * |
| 1365 | * @map: Register map to write to |
| 1366 | * @reg: Register to write to |
| 1367 | * @val: Value to be written |
| 1368 | * |
| 1369 | * A value of zero will be returned on success, a negative errno will |
| 1370 | * be returned in error cases. |
| 1371 | */ |
| 1372 | int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val) |
| 1373 | { |
| 1374 | int ret; |
| 1375 | |
| 1376 | if (reg % map->reg_stride) |
| 1377 | return -EINVAL; |
| 1378 | |
| 1379 | map->lock(map->lock_arg); |
| 1380 | |
| 1381 | map->async = true; |
| 1382 | |
| 1383 | ret = _regmap_write(map, reg, val); |
| 1384 | |
| 1385 | map->async = false; |
| 1386 | |
| 1387 | map->unlock(map->lock_arg); |
| 1388 | |
| 1389 | return ret; |
| 1390 | } |
| 1391 | EXPORT_SYMBOL_GPL(regmap_write_async); |
| 1392 | |
| 1393 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1394 | * regmap_raw_write(): Write raw values to one or more registers |
| 1395 | * |
| 1396 | * @map: Register map to write to |
| 1397 | * @reg: Initial register to write to |
| 1398 | * @val: Block of data to be written, laid out for direct transmission to the |
| 1399 | * device |
| 1400 | * @val_len: Length of data pointed to by val. |
| 1401 | * |
| 1402 | * This function is intended to be used for things like firmware |
| 1403 | * download where a large block of data needs to be transferred to the |
| 1404 | * device. No formatting will be done on the data provided. |
| 1405 | * |
| 1406 | * A value of zero will be returned on success, a negative errno will |
| 1407 | * be returned in error cases. |
| 1408 | */ |
| 1409 | int regmap_raw_write(struct regmap *map, unsigned int reg, |
| 1410 | const void *val, size_t val_len) |
| 1411 | { |
| 1412 | int ret; |
| 1413 | |
Mark Brown | 221ad7f | 2013-03-26 21:24:20 +0000 | [diff] [blame] | 1414 | if (!regmap_can_raw_write(map)) |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1415 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1416 | if (val_len % map->format.val_bytes) |
| 1417 | return -EINVAL; |
| 1418 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1419 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1420 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1421 | ret = _regmap_raw_write(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1422 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1423 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1424 | |
| 1425 | return ret; |
| 1426 | } |
| 1427 | EXPORT_SYMBOL_GPL(regmap_raw_write); |
| 1428 | |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 1429 | /** |
| 1430 | * regmap_field_write(): Write a value to a single register field |
| 1431 | * |
| 1432 | * @field: Register field to write to |
| 1433 | * @val: Value to be written |
| 1434 | * |
| 1435 | * A value of zero will be returned on success, a negative errno will |
| 1436 | * be returned in error cases. |
| 1437 | */ |
| 1438 | int regmap_field_write(struct regmap_field *field, unsigned int val) |
| 1439 | { |
| 1440 | return regmap_update_bits(field->regmap, field->reg, |
| 1441 | field->mask, val << field->shift); |
| 1442 | } |
| 1443 | EXPORT_SYMBOL_GPL(regmap_field_write); |
| 1444 | |
Kuninori Morimoto | fdf2002 | 2013-09-01 20:24:50 -0700 | [diff] [blame] | 1445 | /** |
| 1446 | * regmap_field_update_bits(): Perform a read/modify/write cycle |
| 1447 | * on the register field |
| 1448 | * |
| 1449 | * @field: Register field to write to |
| 1450 | * @mask: Bitmask to change |
| 1451 | * @val: Value to be written |
| 1452 | * |
| 1453 | * A value of zero will be returned on success, a negative errno will |
| 1454 | * be returned in error cases. |
| 1455 | */ |
| 1456 | int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val) |
| 1457 | { |
| 1458 | mask = (mask << field->shift) & field->mask; |
| 1459 | |
| 1460 | return regmap_update_bits(field->regmap, field->reg, |
| 1461 | mask, val << field->shift); |
| 1462 | } |
| 1463 | EXPORT_SYMBOL_GPL(regmap_field_update_bits); |
| 1464 | |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 1465 | /** |
| 1466 | * regmap_fields_write(): Write a value to a single register field with port ID |
| 1467 | * |
| 1468 | * @field: Register field to write to |
| 1469 | * @id: port ID |
| 1470 | * @val: Value to be written |
| 1471 | * |
| 1472 | * A value of zero will be returned on success, a negative errno will |
| 1473 | * be returned in error cases. |
| 1474 | */ |
| 1475 | int regmap_fields_write(struct regmap_field *field, unsigned int id, |
| 1476 | unsigned int val) |
| 1477 | { |
| 1478 | if (id >= field->id_size) |
| 1479 | return -EINVAL; |
| 1480 | |
| 1481 | return regmap_update_bits(field->regmap, |
| 1482 | field->reg + (field->id_offset * id), |
| 1483 | field->mask, val << field->shift); |
| 1484 | } |
| 1485 | EXPORT_SYMBOL_GPL(regmap_fields_write); |
| 1486 | |
| 1487 | /** |
| 1488 | * regmap_fields_update_bits(): Perform a read/modify/write cycle |
| 1489 | * on the register field |
| 1490 | * |
| 1491 | * @field: Register field to write to |
| 1492 | * @id: port ID |
| 1493 | * @mask: Bitmask to change |
| 1494 | * @val: Value to be written |
| 1495 | * |
| 1496 | * A value of zero will be returned on success, a negative errno will |
| 1497 | * be returned in error cases. |
| 1498 | */ |
| 1499 | int regmap_fields_update_bits(struct regmap_field *field, unsigned int id, |
| 1500 | unsigned int mask, unsigned int val) |
| 1501 | { |
| 1502 | if (id >= field->id_size) |
| 1503 | return -EINVAL; |
| 1504 | |
| 1505 | mask = (mask << field->shift) & field->mask; |
| 1506 | |
| 1507 | return regmap_update_bits(field->regmap, |
| 1508 | field->reg + (field->id_offset * id), |
| 1509 | mask, val << field->shift); |
| 1510 | } |
| 1511 | EXPORT_SYMBOL_GPL(regmap_fields_update_bits); |
| 1512 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1513 | /* |
| 1514 | * regmap_bulk_write(): Write multiple registers to the device |
| 1515 | * |
| 1516 | * @map: Register map to write to |
| 1517 | * @reg: First register to be write from |
| 1518 | * @val: Block of data to be written, in native register size for device |
| 1519 | * @val_count: Number of registers to write |
| 1520 | * |
| 1521 | * This function is intended to be used for writing a large block of |
Nestor Ovroy | 31b35e9e | 2013-01-18 16:51:03 +0100 | [diff] [blame] | 1522 | * data to the device either in single transfer or multiple transfer. |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1523 | * |
| 1524 | * A value of zero will be returned on success, a negative errno will |
| 1525 | * be returned in error cases. |
| 1526 | */ |
| 1527 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, |
| 1528 | size_t val_count) |
| 1529 | { |
| 1530 | int ret = 0, i; |
| 1531 | size_t val_bytes = map->format.val_bytes; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1532 | |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1533 | if (map->bus && !map->format.parse_inplace) |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1534 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1535 | if (reg % map->reg_stride) |
| 1536 | return -EINVAL; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1537 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1538 | map->lock(map->lock_arg); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1539 | /* |
| 1540 | * Some devices don't support bulk write, for |
| 1541 | * them we have a series of single write operations. |
| 1542 | */ |
| 1543 | if (!map->bus || map->use_single_rw) { |
| 1544 | for (i = 0; i < val_count; i++) { |
| 1545 | unsigned int ival; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1546 | |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1547 | switch (val_bytes) { |
| 1548 | case 1: |
| 1549 | ival = *(u8 *)(val + (i * val_bytes)); |
| 1550 | break; |
| 1551 | case 2: |
| 1552 | ival = *(u16 *)(val + (i * val_bytes)); |
| 1553 | break; |
| 1554 | case 4: |
| 1555 | ival = *(u32 *)(val + (i * val_bytes)); |
| 1556 | break; |
| 1557 | #ifdef CONFIG_64BIT |
| 1558 | case 8: |
| 1559 | ival = *(u64 *)(val + (i * val_bytes)); |
| 1560 | break; |
| 1561 | #endif |
| 1562 | default: |
| 1563 | ret = -EINVAL; |
| 1564 | goto out; |
| 1565 | } |
| 1566 | |
| 1567 | ret = _regmap_write(map, reg + (i * map->reg_stride), |
| 1568 | ival); |
| 1569 | if (ret != 0) |
| 1570 | goto out; |
| 1571 | } |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1572 | } else { |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1573 | void *wval; |
| 1574 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1575 | wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); |
| 1576 | if (!wval) { |
| 1577 | ret = -ENOMEM; |
| 1578 | dev_err(map->dev, "Error in memory allocation\n"); |
| 1579 | goto out; |
| 1580 | } |
| 1581 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 1582 | map->format.parse_inplace(wval + i); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1583 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1584 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1585 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1586 | kfree(wval); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1587 | } |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1588 | out: |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1589 | map->unlock(map->lock_arg); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1590 | return ret; |
| 1591 | } |
| 1592 | EXPORT_SYMBOL_GPL(regmap_bulk_write); |
| 1593 | |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 1594 | /* |
| 1595 | * regmap_multi_reg_write(): Write multiple registers to the device |
| 1596 | * |
| 1597 | * where the set of register are supplied in any order |
| 1598 | * |
| 1599 | * @map: Register map to write to |
| 1600 | * @regs: Array of structures containing register,value to be written |
| 1601 | * @num_regs: Number of registers to write |
| 1602 | * |
| 1603 | * This function is intended to be used for writing a large block of data |
| 1604 | * atomically to the device in single transfer for those I2C client devices |
| 1605 | * that implement this alternative block write mode. |
| 1606 | * |
| 1607 | * A value of zero will be returned on success, a negative errno will |
| 1608 | * be returned in error cases. |
| 1609 | */ |
| 1610 | int regmap_multi_reg_write(struct regmap *map, struct reg_default *regs, |
| 1611 | int num_regs) |
| 1612 | { |
| 1613 | int ret = 0, i; |
| 1614 | |
| 1615 | for (i = 0; i < num_regs; i++) { |
| 1616 | int reg = regs[i].reg; |
| 1617 | if (reg % map->reg_stride) |
| 1618 | return -EINVAL; |
| 1619 | } |
| 1620 | |
| 1621 | map->lock(map->lock_arg); |
| 1622 | |
| 1623 | for (i = 0; i < num_regs; i++) { |
| 1624 | ret = _regmap_write(map, regs[i].reg, regs[i].def); |
| 1625 | if (ret != 0) |
| 1626 | goto out; |
| 1627 | } |
| 1628 | out: |
| 1629 | map->unlock(map->lock_arg); |
| 1630 | |
| 1631 | return ret; |
| 1632 | } |
| 1633 | EXPORT_SYMBOL_GPL(regmap_multi_reg_write); |
| 1634 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1635 | /** |
| 1636 | * regmap_raw_write_async(): Write raw values to one or more registers |
| 1637 | * asynchronously |
| 1638 | * |
| 1639 | * @map: Register map to write to |
| 1640 | * @reg: Initial register to write to |
| 1641 | * @val: Block of data to be written, laid out for direct transmission to the |
| 1642 | * device. Must be valid until regmap_async_complete() is called. |
| 1643 | * @val_len: Length of data pointed to by val. |
| 1644 | * |
| 1645 | * This function is intended to be used for things like firmware |
| 1646 | * download where a large block of data needs to be transferred to the |
| 1647 | * device. No formatting will be done on the data provided. |
| 1648 | * |
| 1649 | * If supported by the underlying bus the write will be scheduled |
| 1650 | * asynchronously, helping maximise I/O speed on higher speed buses |
| 1651 | * like SPI. regmap_async_complete() can be called to ensure that all |
| 1652 | * asynchrnous writes have been completed. |
| 1653 | * |
| 1654 | * A value of zero will be returned on success, a negative errno will |
| 1655 | * be returned in error cases. |
| 1656 | */ |
| 1657 | int regmap_raw_write_async(struct regmap *map, unsigned int reg, |
| 1658 | const void *val, size_t val_len) |
| 1659 | { |
| 1660 | int ret; |
| 1661 | |
| 1662 | if (val_len % map->format.val_bytes) |
| 1663 | return -EINVAL; |
| 1664 | if (reg % map->reg_stride) |
| 1665 | return -EINVAL; |
| 1666 | |
| 1667 | map->lock(map->lock_arg); |
| 1668 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1669 | map->async = true; |
| 1670 | |
| 1671 | ret = _regmap_raw_write(map, reg, val, val_len); |
| 1672 | |
| 1673 | map->async = false; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1674 | |
| 1675 | map->unlock(map->lock_arg); |
| 1676 | |
| 1677 | return ret; |
| 1678 | } |
| 1679 | EXPORT_SYMBOL_GPL(regmap_raw_write_async); |
| 1680 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1681 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 1682 | unsigned int val_len) |
| 1683 | { |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1684 | struct regmap_range_node *range; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1685 | u8 *u8 = map->work_buf; |
| 1686 | int ret; |
| 1687 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1688 | WARN_ON(!map->bus); |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1689 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1690 | range = _regmap_range_lookup(map, reg); |
| 1691 | if (range) { |
| 1692 | ret = _regmap_select_page(map, ®, range, |
| 1693 | val_len / map->format.val_bytes); |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 1694 | if (ret != 0) |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1695 | return ret; |
| 1696 | } |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1697 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1698 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1701 | * Some buses or devices flag reads by setting the high bits in the |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1702 | * register addresss; since it's always the high bits for all |
| 1703 | * current formats we can do this here rather than in |
| 1704 | * formatting. This may break if we get interesting formats. |
| 1705 | */ |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1706 | u8[0] |= map->read_flag_mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1707 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1708 | trace_regmap_hw_read_start(map->dev, reg, |
| 1709 | val_len / map->format.val_bytes); |
| 1710 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1711 | ret = map->bus->read(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1712 | map->format.reg_bytes + map->format.pad_bytes, |
Mark Brown | 40c5cc2 | 2011-07-24 22:39:12 +0100 | [diff] [blame] | 1713 | val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1714 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1715 | trace_regmap_hw_read_done(map->dev, reg, |
| 1716 | val_len / map->format.val_bytes); |
| 1717 | |
| 1718 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1719 | } |
| 1720 | |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 1721 | static int _regmap_bus_read(void *context, unsigned int reg, |
| 1722 | unsigned int *val) |
| 1723 | { |
| 1724 | int ret; |
| 1725 | struct regmap *map = context; |
| 1726 | |
| 1727 | if (!map->format.parse_val) |
| 1728 | return -EINVAL; |
| 1729 | |
| 1730 | ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); |
| 1731 | if (ret == 0) |
| 1732 | *val = map->format.parse_val(map->work_buf); |
| 1733 | |
| 1734 | return ret; |
| 1735 | } |
| 1736 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1737 | static int _regmap_read(struct regmap *map, unsigned int reg, |
| 1738 | unsigned int *val) |
| 1739 | { |
| 1740 | int ret; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1741 | void *context = _regmap_map_get_context(map); |
| 1742 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1743 | WARN_ON(!map->reg_read); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1744 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1745 | if (!map->cache_bypass) { |
| 1746 | ret = regcache_read(map, reg, val); |
| 1747 | if (ret == 0) |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | if (map->cache_only) |
| 1752 | return -EBUSY; |
| 1753 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1754 | ret = map->reg_read(context, reg, val); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1755 | if (ret == 0) { |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 1756 | #ifdef LOG_DEVICE |
| 1757 | if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0) |
| 1758 | dev_info(map->dev, "%x => %x\n", reg, *val); |
| 1759 | #endif |
| 1760 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1761 | trace_regmap_reg_read(map->dev, reg, *val); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1762 | |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 1763 | if (!map->cache_bypass) |
| 1764 | regcache_write(map, reg, *val); |
| 1765 | } |
Mark Brown | f298536 | 2012-04-30 21:25:05 +0100 | [diff] [blame] | 1766 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1767 | return ret; |
| 1768 | } |
| 1769 | |
| 1770 | /** |
| 1771 | * regmap_read(): Read a value from a single register |
| 1772 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 1773 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1774 | * @reg: Register to be read from |
| 1775 | * @val: Pointer to store read value |
| 1776 | * |
| 1777 | * A value of zero will be returned on success, a negative errno will |
| 1778 | * be returned in error cases. |
| 1779 | */ |
| 1780 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) |
| 1781 | { |
| 1782 | int ret; |
| 1783 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1784 | if (reg % map->reg_stride) |
| 1785 | return -EINVAL; |
| 1786 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1787 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1788 | |
| 1789 | ret = _regmap_read(map, reg, val); |
| 1790 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1791 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1792 | |
| 1793 | return ret; |
| 1794 | } |
| 1795 | EXPORT_SYMBOL_GPL(regmap_read); |
| 1796 | |
| 1797 | /** |
| 1798 | * regmap_raw_read(): Read raw data from the device |
| 1799 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 1800 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1801 | * @reg: First register to be read from |
| 1802 | * @val: Pointer to store read value |
| 1803 | * @val_len: Size of data to read |
| 1804 | * |
| 1805 | * A value of zero will be returned on success, a negative errno will |
| 1806 | * be returned in error cases. |
| 1807 | */ |
| 1808 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 1809 | size_t val_len) |
| 1810 | { |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1811 | size_t val_bytes = map->format.val_bytes; |
| 1812 | size_t val_count = val_len / val_bytes; |
| 1813 | unsigned int v; |
| 1814 | int ret, i; |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 1815 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1816 | if (!map->bus) |
| 1817 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1818 | if (val_len % map->format.val_bytes) |
| 1819 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1820 | if (reg % map->reg_stride) |
| 1821 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1822 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1823 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1824 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1825 | if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || |
| 1826 | map->cache_type == REGCACHE_NONE) { |
| 1827 | /* Physical block read if there's no cache involved */ |
| 1828 | ret = _regmap_raw_read(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1829 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1830 | } else { |
| 1831 | /* Otherwise go word by word for the cache; should be low |
| 1832 | * cost as we expect to hit the cache. |
| 1833 | */ |
| 1834 | for (i = 0; i < val_count; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1835 | ret = _regmap_read(map, reg + (i * map->reg_stride), |
| 1836 | &v); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1837 | if (ret != 0) |
| 1838 | goto out; |
| 1839 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1840 | map->format.format_val(val + (i * val_bytes), v, 0); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | out: |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1845 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1846 | |
| 1847 | return ret; |
| 1848 | } |
| 1849 | EXPORT_SYMBOL_GPL(regmap_raw_read); |
| 1850 | |
| 1851 | /** |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 1852 | * regmap_field_read(): Read a value to a single register field |
| 1853 | * |
| 1854 | * @field: Register field to read from |
| 1855 | * @val: Pointer to store read value |
| 1856 | * |
| 1857 | * A value of zero will be returned on success, a negative errno will |
| 1858 | * be returned in error cases. |
| 1859 | */ |
| 1860 | int regmap_field_read(struct regmap_field *field, unsigned int *val) |
| 1861 | { |
| 1862 | int ret; |
| 1863 | unsigned int reg_val; |
| 1864 | ret = regmap_read(field->regmap, field->reg, ®_val); |
| 1865 | if (ret != 0) |
| 1866 | return ret; |
| 1867 | |
| 1868 | reg_val &= field->mask; |
| 1869 | reg_val >>= field->shift; |
| 1870 | *val = reg_val; |
| 1871 | |
| 1872 | return ret; |
| 1873 | } |
| 1874 | EXPORT_SYMBOL_GPL(regmap_field_read); |
| 1875 | |
| 1876 | /** |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 1877 | * regmap_fields_read(): Read a value to a single register field with port ID |
| 1878 | * |
| 1879 | * @field: Register field to read from |
| 1880 | * @id: port ID |
| 1881 | * @val: Pointer to store read value |
| 1882 | * |
| 1883 | * A value of zero will be returned on success, a negative errno will |
| 1884 | * be returned in error cases. |
| 1885 | */ |
| 1886 | int regmap_fields_read(struct regmap_field *field, unsigned int id, |
| 1887 | unsigned int *val) |
| 1888 | { |
| 1889 | int ret; |
| 1890 | unsigned int reg_val; |
| 1891 | |
| 1892 | if (id >= field->id_size) |
| 1893 | return -EINVAL; |
| 1894 | |
| 1895 | ret = regmap_read(field->regmap, |
| 1896 | field->reg + (field->id_offset * id), |
| 1897 | ®_val); |
| 1898 | if (ret != 0) |
| 1899 | return ret; |
| 1900 | |
| 1901 | reg_val &= field->mask; |
| 1902 | reg_val >>= field->shift; |
| 1903 | *val = reg_val; |
| 1904 | |
| 1905 | return ret; |
| 1906 | } |
| 1907 | EXPORT_SYMBOL_GPL(regmap_fields_read); |
| 1908 | |
| 1909 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1910 | * regmap_bulk_read(): Read multiple registers from the device |
| 1911 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 1912 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1913 | * @reg: First register to be read from |
| 1914 | * @val: Pointer to store read value, in native register size for device |
| 1915 | * @val_count: Number of registers to read |
| 1916 | * |
| 1917 | * A value of zero will be returned on success, a negative errno will |
| 1918 | * be returned in error cases. |
| 1919 | */ |
| 1920 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, |
| 1921 | size_t val_count) |
| 1922 | { |
| 1923 | int ret, i; |
| 1924 | size_t val_bytes = map->format.val_bytes; |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 1925 | bool vol = regmap_volatile_range(map, reg, val_count); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1926 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1927 | if (reg % map->reg_stride) |
| 1928 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1929 | |
Stephen Boyd | 3b58ee1 | 2013-12-13 09:14:07 -0800 | [diff] [blame] | 1930 | if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) { |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 1931 | /* |
| 1932 | * Some devices does not support bulk read, for |
| 1933 | * them we have a series of single read operations. |
| 1934 | */ |
| 1935 | if (map->use_single_rw) { |
| 1936 | for (i = 0; i < val_count; i++) { |
| 1937 | ret = regmap_raw_read(map, |
| 1938 | reg + (i * map->reg_stride), |
| 1939 | val + (i * val_bytes), |
| 1940 | val_bytes); |
| 1941 | if (ret != 0) |
| 1942 | return ret; |
| 1943 | } |
| 1944 | } else { |
| 1945 | ret = regmap_raw_read(map, reg, val, |
| 1946 | val_bytes * val_count); |
| 1947 | if (ret != 0) |
| 1948 | return ret; |
| 1949 | } |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1950 | |
| 1951 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 1952 | map->format.parse_inplace(val + i); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1953 | } else { |
| 1954 | for (i = 0; i < val_count; i++) { |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 1955 | unsigned int ival; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1956 | ret = regmap_read(map, reg + (i * map->reg_stride), |
Mark Brown | 25061d2 | 2012-05-12 13:06:08 +0100 | [diff] [blame] | 1957 | &ival); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1958 | if (ret != 0) |
| 1959 | return ret; |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 1960 | memcpy(val + (i * val_bytes), &ival, val_bytes); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1961 | } |
| 1962 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1963 | |
| 1964 | return 0; |
| 1965 | } |
| 1966 | EXPORT_SYMBOL_GPL(regmap_bulk_read); |
| 1967 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1968 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 1969 | unsigned int mask, unsigned int val, |
| 1970 | bool *change) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1971 | { |
| 1972 | int ret; |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1973 | unsigned int tmp, orig; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1974 | |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1975 | ret = _regmap_read(map, reg, &orig); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1976 | if (ret != 0) |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1977 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1978 | |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1979 | tmp = orig & ~mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1980 | tmp |= val & mask; |
| 1981 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1982 | if (tmp != orig) { |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1983 | ret = _regmap_write(map, reg, tmp); |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1984 | *change = true; |
| 1985 | } else { |
| 1986 | *change = false; |
| 1987 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1988 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1989 | return ret; |
| 1990 | } |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1991 | |
| 1992 | /** |
| 1993 | * regmap_update_bits: Perform a read/modify/write cycle on the register map |
| 1994 | * |
| 1995 | * @map: Register map to update |
| 1996 | * @reg: Register to update |
| 1997 | * @mask: Bitmask to change |
| 1998 | * @val: New value for bitmask |
| 1999 | * |
| 2000 | * Returns zero for success, a negative number on error. |
| 2001 | */ |
| 2002 | int regmap_update_bits(struct regmap *map, unsigned int reg, |
| 2003 | unsigned int mask, unsigned int val) |
| 2004 | { |
| 2005 | bool change; |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2006 | int ret; |
| 2007 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2008 | map->lock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2009 | ret = _regmap_update_bits(map, reg, mask, val, &change); |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2010 | map->unlock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2011 | |
| 2012 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2013 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2014 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 2015 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2016 | /** |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2017 | * regmap_update_bits_async: Perform a read/modify/write cycle on the register |
| 2018 | * map asynchronously |
| 2019 | * |
| 2020 | * @map: Register map to update |
| 2021 | * @reg: Register to update |
| 2022 | * @mask: Bitmask to change |
| 2023 | * @val: New value for bitmask |
| 2024 | * |
| 2025 | * With most buses the read must be done synchronously so this is most |
| 2026 | * useful for devices with a cache which do not need to interact with |
| 2027 | * the hardware to determine the current register value. |
| 2028 | * |
| 2029 | * Returns zero for success, a negative number on error. |
| 2030 | */ |
| 2031 | int regmap_update_bits_async(struct regmap *map, unsigned int reg, |
| 2032 | unsigned int mask, unsigned int val) |
| 2033 | { |
| 2034 | bool change; |
| 2035 | int ret; |
| 2036 | |
| 2037 | map->lock(map->lock_arg); |
| 2038 | |
| 2039 | map->async = true; |
| 2040 | |
| 2041 | ret = _regmap_update_bits(map, reg, mask, val, &change); |
| 2042 | |
| 2043 | map->async = false; |
| 2044 | |
| 2045 | map->unlock(map->lock_arg); |
| 2046 | |
| 2047 | return ret; |
| 2048 | } |
| 2049 | EXPORT_SYMBOL_GPL(regmap_update_bits_async); |
| 2050 | |
| 2051 | /** |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2052 | * regmap_update_bits_check: Perform a read/modify/write cycle on the |
| 2053 | * register map and report if updated |
| 2054 | * |
| 2055 | * @map: Register map to update |
| 2056 | * @reg: Register to update |
| 2057 | * @mask: Bitmask to change |
| 2058 | * @val: New value for bitmask |
| 2059 | * @change: Boolean indicating if a write was done |
| 2060 | * |
| 2061 | * Returns zero for success, a negative number on error. |
| 2062 | */ |
| 2063 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, |
| 2064 | unsigned int mask, unsigned int val, |
| 2065 | bool *change) |
| 2066 | { |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2067 | int ret; |
| 2068 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2069 | map->lock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2070 | ret = _regmap_update_bits(map, reg, mask, val, change); |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2071 | map->unlock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2072 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2073 | } |
| 2074 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); |
| 2075 | |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2076 | /** |
| 2077 | * regmap_update_bits_check_async: Perform a read/modify/write cycle on the |
| 2078 | * register map asynchronously and report if |
| 2079 | * updated |
| 2080 | * |
| 2081 | * @map: Register map to update |
| 2082 | * @reg: Register to update |
| 2083 | * @mask: Bitmask to change |
| 2084 | * @val: New value for bitmask |
| 2085 | * @change: Boolean indicating if a write was done |
| 2086 | * |
| 2087 | * With most buses the read must be done synchronously so this is most |
| 2088 | * useful for devices with a cache which do not need to interact with |
| 2089 | * the hardware to determine the current register value. |
| 2090 | * |
| 2091 | * Returns zero for success, a negative number on error. |
| 2092 | */ |
| 2093 | int regmap_update_bits_check_async(struct regmap *map, unsigned int reg, |
| 2094 | unsigned int mask, unsigned int val, |
| 2095 | bool *change) |
| 2096 | { |
| 2097 | int ret; |
| 2098 | |
| 2099 | map->lock(map->lock_arg); |
| 2100 | |
| 2101 | map->async = true; |
| 2102 | |
| 2103 | ret = _regmap_update_bits(map, reg, mask, val, change); |
| 2104 | |
| 2105 | map->async = false; |
| 2106 | |
| 2107 | map->unlock(map->lock_arg); |
| 2108 | |
| 2109 | return ret; |
| 2110 | } |
| 2111 | EXPORT_SYMBOL_GPL(regmap_update_bits_check_async); |
| 2112 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2113 | void regmap_async_complete_cb(struct regmap_async *async, int ret) |
| 2114 | { |
| 2115 | struct regmap *map = async->map; |
| 2116 | bool wake; |
| 2117 | |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2118 | trace_regmap_async_io_complete(map->dev); |
| 2119 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2120 | spin_lock(&map->async_lock); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 2121 | list_move(&async->list, &map->async_free); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2122 | wake = list_empty(&map->async_list); |
| 2123 | |
| 2124 | if (ret != 0) |
| 2125 | map->async_ret = ret; |
| 2126 | |
| 2127 | spin_unlock(&map->async_lock); |
| 2128 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2129 | if (wake) |
| 2130 | wake_up(&map->async_waitq); |
| 2131 | } |
Axel Lin | f804fb5 | 2013-02-03 00:14:13 +0800 | [diff] [blame] | 2132 | EXPORT_SYMBOL_GPL(regmap_async_complete_cb); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2133 | |
| 2134 | static int regmap_async_is_done(struct regmap *map) |
| 2135 | { |
| 2136 | unsigned long flags; |
| 2137 | int ret; |
| 2138 | |
| 2139 | spin_lock_irqsave(&map->async_lock, flags); |
| 2140 | ret = list_empty(&map->async_list); |
| 2141 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 2142 | |
| 2143 | return ret; |
| 2144 | } |
| 2145 | |
| 2146 | /** |
| 2147 | * regmap_async_complete: Ensure all asynchronous I/O has completed. |
| 2148 | * |
| 2149 | * @map: Map to operate on. |
| 2150 | * |
| 2151 | * Blocks until any pending asynchronous I/O has completed. Returns |
| 2152 | * an error code for any failed I/O operations. |
| 2153 | */ |
| 2154 | int regmap_async_complete(struct regmap *map) |
| 2155 | { |
| 2156 | unsigned long flags; |
| 2157 | int ret; |
| 2158 | |
| 2159 | /* Nothing to do with no async support */ |
Daniel Mack | f2e055e | 2013-07-04 13:11:03 +0200 | [diff] [blame] | 2160 | if (!map->bus || !map->bus->async_write) |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2161 | return 0; |
| 2162 | |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2163 | trace_regmap_async_complete_start(map->dev); |
| 2164 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2165 | wait_event(map->async_waitq, regmap_async_is_done(map)); |
| 2166 | |
| 2167 | spin_lock_irqsave(&map->async_lock, flags); |
| 2168 | ret = map->async_ret; |
| 2169 | map->async_ret = 0; |
| 2170 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 2171 | |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2172 | trace_regmap_async_complete_done(map->dev); |
| 2173 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2174 | return ret; |
| 2175 | } |
Mark Brown | f88948e | 2013-02-05 13:53:26 +0000 | [diff] [blame] | 2176 | EXPORT_SYMBOL_GPL(regmap_async_complete); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2177 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2178 | /** |
| 2179 | * regmap_register_patch: Register and apply register updates to be applied |
| 2180 | * on device initialistion |
| 2181 | * |
| 2182 | * @map: Register map to apply updates to. |
| 2183 | * @regs: Values to update. |
| 2184 | * @num_regs: Number of entries in regs. |
| 2185 | * |
| 2186 | * Register a set of register updates to be applied to the device |
| 2187 | * whenever the device registers are synchronised with the cache and |
| 2188 | * apply them immediately. Typically this is used to apply |
| 2189 | * corrections to be applied to the device defaults on startup, such |
| 2190 | * as the updates some vendors provide to undocumented registers. |
| 2191 | */ |
| 2192 | int regmap_register_patch(struct regmap *map, const struct reg_default *regs, |
| 2193 | int num_regs) |
| 2194 | { |
Mark Brown | aab13eb | 2013-07-11 12:41:44 +0100 | [diff] [blame] | 2195 | struct reg_default *p; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2196 | int i, ret; |
| 2197 | bool bypass; |
| 2198 | |
Cai Zhiyong | bd60e38 | 2013-11-18 20:21:49 +0800 | [diff] [blame] | 2199 | if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n", |
| 2200 | num_regs)) |
| 2201 | return 0; |
| 2202 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2203 | map->lock(map->lock_arg); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2204 | |
| 2205 | bypass = map->cache_bypass; |
| 2206 | |
| 2207 | map->cache_bypass = true; |
Mark Brown | 1a25f26 | 2013-10-10 20:55:03 +0100 | [diff] [blame] | 2208 | map->async = true; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2209 | |
| 2210 | /* Write out first; it's useful to apply even if we fail later. */ |
| 2211 | for (i = 0; i < num_regs; i++) { |
| 2212 | ret = _regmap_write(map, regs[i].reg, regs[i].def); |
| 2213 | if (ret != 0) { |
| 2214 | dev_err(map->dev, "Failed to write %x = %x: %d\n", |
| 2215 | regs[i].reg, regs[i].def, ret); |
| 2216 | goto out; |
| 2217 | } |
| 2218 | } |
| 2219 | |
Mark Brown | aab13eb | 2013-07-11 12:41:44 +0100 | [diff] [blame] | 2220 | p = krealloc(map->patch, |
| 2221 | sizeof(struct reg_default) * (map->patch_regs + num_regs), |
| 2222 | GFP_KERNEL); |
| 2223 | if (p) { |
| 2224 | memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs)); |
| 2225 | map->patch = p; |
| 2226 | map->patch_regs += num_regs; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2227 | } else { |
| 2228 | ret = -ENOMEM; |
| 2229 | } |
| 2230 | |
| 2231 | out: |
Mark Brown | 1a25f26 | 2013-10-10 20:55:03 +0100 | [diff] [blame] | 2232 | map->async = false; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2233 | map->cache_bypass = bypass; |
| 2234 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2235 | map->unlock(map->lock_arg); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2236 | |
Mark Brown | 1a25f26 | 2013-10-10 20:55:03 +0100 | [diff] [blame] | 2237 | regmap_async_complete(map); |
| 2238 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2239 | return ret; |
| 2240 | } |
| 2241 | EXPORT_SYMBOL_GPL(regmap_register_patch); |
| 2242 | |
Mark Brown | eae4b51 | 2012-03-14 13:15:03 +0000 | [diff] [blame] | 2243 | /* |
Mark Brown | a6539c3 | 2012-02-17 14:20:14 -0800 | [diff] [blame] | 2244 | * regmap_get_val_bytes(): Report the size of a register value |
| 2245 | * |
| 2246 | * Report the size of a register value, mainly intended to for use by |
| 2247 | * generic infrastructure built on top of regmap. |
| 2248 | */ |
| 2249 | int regmap_get_val_bytes(struct regmap *map) |
| 2250 | { |
| 2251 | if (map->format.format_write) |
| 2252 | return -EINVAL; |
| 2253 | |
| 2254 | return map->format.val_bytes; |
| 2255 | } |
| 2256 | EXPORT_SYMBOL_GPL(regmap_get_val_bytes); |
| 2257 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 2258 | static int __init regmap_initcall(void) |
| 2259 | { |
| 2260 | regmap_debugfs_initcall(); |
| 2261 | |
| 2262 | return 0; |
| 2263 | } |
| 2264 | postcore_initcall(regmap_initcall); |