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 | |
Daeseok Youn | a7a037c | 2014-04-01 19:46:43 +0900 | [diff] [blame^] | 763 | if (dev) { |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame] | 764 | ret = regmap_attach_dev(dev, map, config); |
| 765 | if (ret != 0) |
| 766 | goto err_regcache; |
Daeseok Youn | a7a037c | 2014-04-01 19:46:43 +0900 | [diff] [blame^] | 767 | } |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 768 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 769 | return map; |
| 770 | |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame] | 771 | err_regcache: |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 772 | regcache_exit(map); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 773 | err_range: |
| 774 | regmap_range_exit(map); |
Lars-Peter Clausen | 58072cb | 2011-11-10 18:15:15 +0100 | [diff] [blame] | 775 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 776 | err_map: |
| 777 | kfree(map); |
| 778 | err: |
| 779 | return ERR_PTR(ret); |
| 780 | } |
| 781 | EXPORT_SYMBOL_GPL(regmap_init); |
| 782 | |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 783 | static void devm_regmap_release(struct device *dev, void *res) |
| 784 | { |
| 785 | regmap_exit(*(struct regmap **)res); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * devm_regmap_init(): Initialise managed register map |
| 790 | * |
| 791 | * @dev: Device that will be interacted with |
| 792 | * @bus: Bus-specific callbacks to use with device |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 793 | * @bus_context: Data passed to bus-specific callbacks |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 794 | * @config: Configuration for register map |
| 795 | * |
| 796 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 797 | * to a struct regmap. This function should generally not be called |
| 798 | * directly, it should be called by bus-specific init functions. The |
| 799 | * map will be automatically freed by the device management code. |
| 800 | */ |
| 801 | struct regmap *devm_regmap_init(struct device *dev, |
| 802 | const struct regmap_bus *bus, |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 803 | void *bus_context, |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 804 | const struct regmap_config *config) |
| 805 | { |
| 806 | struct regmap **ptr, *regmap; |
| 807 | |
| 808 | ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); |
| 809 | if (!ptr) |
| 810 | return ERR_PTR(-ENOMEM); |
| 811 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 812 | regmap = regmap_init(dev, bus, bus_context, config); |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 813 | if (!IS_ERR(regmap)) { |
| 814 | *ptr = regmap; |
| 815 | devres_add(dev, ptr); |
| 816 | } else { |
| 817 | devres_free(ptr); |
| 818 | } |
| 819 | |
| 820 | return regmap; |
| 821 | } |
| 822 | EXPORT_SYMBOL_GPL(devm_regmap_init); |
| 823 | |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 824 | static void regmap_field_init(struct regmap_field *rm_field, |
| 825 | struct regmap *regmap, struct reg_field reg_field) |
| 826 | { |
| 827 | int field_bits = reg_field.msb - reg_field.lsb + 1; |
| 828 | rm_field->regmap = regmap; |
| 829 | rm_field->reg = reg_field.reg; |
| 830 | rm_field->shift = reg_field.lsb; |
| 831 | rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb); |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 832 | rm_field->id_size = reg_field.id_size; |
| 833 | rm_field->id_offset = reg_field.id_offset; |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
| 837 | * devm_regmap_field_alloc(): Allocate and initialise a register field |
| 838 | * in a register map. |
| 839 | * |
| 840 | * @dev: Device that will be interacted with |
| 841 | * @regmap: regmap bank in which this register field is located. |
| 842 | * @reg_field: Register field with in the bank. |
| 843 | * |
| 844 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 845 | * to a struct regmap_field. The regmap_field will be automatically freed |
| 846 | * by the device management code. |
| 847 | */ |
| 848 | struct regmap_field *devm_regmap_field_alloc(struct device *dev, |
| 849 | struct regmap *regmap, struct reg_field reg_field) |
| 850 | { |
| 851 | struct regmap_field *rm_field = devm_kzalloc(dev, |
| 852 | sizeof(*rm_field), GFP_KERNEL); |
| 853 | if (!rm_field) |
| 854 | return ERR_PTR(-ENOMEM); |
| 855 | |
| 856 | regmap_field_init(rm_field, regmap, reg_field); |
| 857 | |
| 858 | return rm_field; |
| 859 | |
| 860 | } |
| 861 | EXPORT_SYMBOL_GPL(devm_regmap_field_alloc); |
| 862 | |
| 863 | /** |
| 864 | * devm_regmap_field_free(): Free register field allocated using |
| 865 | * devm_regmap_field_alloc. Usally drivers need not call this function, |
| 866 | * as the memory allocated via devm will be freed as per device-driver |
| 867 | * life-cyle. |
| 868 | * |
| 869 | * @dev: Device that will be interacted with |
| 870 | * @field: regmap field which should be freed. |
| 871 | */ |
| 872 | void devm_regmap_field_free(struct device *dev, |
| 873 | struct regmap_field *field) |
| 874 | { |
| 875 | devm_kfree(dev, field); |
| 876 | } |
| 877 | EXPORT_SYMBOL_GPL(devm_regmap_field_free); |
| 878 | |
| 879 | /** |
| 880 | * regmap_field_alloc(): Allocate and initialise a register field |
| 881 | * in a register map. |
| 882 | * |
| 883 | * @regmap: regmap bank in which this register field is located. |
| 884 | * @reg_field: Register field with in the bank. |
| 885 | * |
| 886 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 887 | * to a struct regmap_field. The regmap_field should be freed by the |
| 888 | * user once its finished working with it using regmap_field_free(). |
| 889 | */ |
| 890 | struct regmap_field *regmap_field_alloc(struct regmap *regmap, |
| 891 | struct reg_field reg_field) |
| 892 | { |
| 893 | struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL); |
| 894 | |
| 895 | if (!rm_field) |
| 896 | return ERR_PTR(-ENOMEM); |
| 897 | |
| 898 | regmap_field_init(rm_field, regmap, reg_field); |
| 899 | |
| 900 | return rm_field; |
| 901 | } |
| 902 | EXPORT_SYMBOL_GPL(regmap_field_alloc); |
| 903 | |
| 904 | /** |
| 905 | * regmap_field_free(): Free register field allocated using regmap_field_alloc |
| 906 | * |
| 907 | * @field: regmap field which should be freed. |
| 908 | */ |
| 909 | void regmap_field_free(struct regmap_field *field) |
| 910 | { |
| 911 | kfree(field); |
| 912 | } |
| 913 | EXPORT_SYMBOL_GPL(regmap_field_free); |
| 914 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 915 | /** |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 916 | * regmap_reinit_cache(): Reinitialise the current register cache |
| 917 | * |
| 918 | * @map: Register map to operate on. |
| 919 | * @config: New configuration. Only the cache data will be used. |
| 920 | * |
| 921 | * Discard any existing register cache for the map and initialize a |
| 922 | * new cache. This can be used to restore the cache to defaults or to |
| 923 | * update the cache configuration to reflect runtime discovery of the |
| 924 | * hardware. |
Dimitris Papastamos | 4d87951 | 2012-07-27 14:54:15 +0100 | [diff] [blame] | 925 | * |
| 926 | * No explicit locking is done here, the user needs to ensure that |
| 927 | * this function will not race with other calls to regmap. |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 928 | */ |
| 929 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) |
| 930 | { |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 931 | regcache_exit(map); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 932 | regmap_debugfs_exit(map); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 933 | |
| 934 | map->max_register = config->max_register; |
| 935 | map->writeable_reg = config->writeable_reg; |
| 936 | map->readable_reg = config->readable_reg; |
| 937 | map->volatile_reg = config->volatile_reg; |
| 938 | map->precious_reg = config->precious_reg; |
| 939 | map->cache_type = config->cache_type; |
| 940 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 941 | regmap_debugfs_init(map, config->name); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 942 | |
Mark Brown | 421e8d2 | 2012-01-20 13:39:37 +0000 | [diff] [blame] | 943 | map->cache_bypass = false; |
| 944 | map->cache_only = false; |
| 945 | |
Dimitris Papastamos | 4d87951 | 2012-07-27 14:54:15 +0100 | [diff] [blame] | 946 | return regcache_init(map, config); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 947 | } |
Mark Brown | 752a6a5 | 2012-05-14 10:00:12 +0100 | [diff] [blame] | 948 | EXPORT_SYMBOL_GPL(regmap_reinit_cache); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 949 | |
| 950 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 951 | * regmap_exit(): Free a previously allocated register map |
| 952 | */ |
| 953 | void regmap_exit(struct regmap *map) |
| 954 | { |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 955 | struct regmap_async *async; |
| 956 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 957 | regcache_exit(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 958 | regmap_debugfs_exit(map); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 959 | regmap_range_exit(map); |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 960 | if (map->bus && map->bus->free_context) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 961 | map->bus->free_context(map->bus_context); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 962 | kfree(map->work_buf); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 963 | while (!list_empty(&map->async_free)) { |
| 964 | async = list_first_entry_or_null(&map->async_free, |
| 965 | struct regmap_async, |
| 966 | list); |
| 967 | list_del(&async->list); |
| 968 | kfree(async->work_buf); |
| 969 | kfree(async); |
| 970 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 971 | kfree(map); |
| 972 | } |
| 973 | EXPORT_SYMBOL_GPL(regmap_exit); |
| 974 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 975 | static int dev_get_regmap_match(struct device *dev, void *res, void *data) |
| 976 | { |
| 977 | struct regmap **r = res; |
| 978 | if (!r || !*r) { |
| 979 | WARN_ON(!r || !*r); |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | /* If the user didn't specify a name match any */ |
| 984 | if (data) |
| 985 | return (*r)->name == data; |
| 986 | else |
| 987 | return 1; |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * dev_get_regmap(): Obtain the regmap (if any) for a device |
| 992 | * |
| 993 | * @dev: Device to retrieve the map for |
| 994 | * @name: Optional name for the register map, usually NULL. |
| 995 | * |
| 996 | * Returns the regmap for the device if one is present, or NULL. If |
| 997 | * name is specified then it must match the name specified when |
| 998 | * registering the device, if it is NULL then the first regmap found |
| 999 | * will be used. Devices with multiple register maps are very rare, |
| 1000 | * generic code should normally not need to specify a name. |
| 1001 | */ |
| 1002 | struct regmap *dev_get_regmap(struct device *dev, const char *name) |
| 1003 | { |
| 1004 | struct regmap **r = devres_find(dev, dev_get_regmap_release, |
| 1005 | dev_get_regmap_match, (void *)name); |
| 1006 | |
| 1007 | if (!r) |
| 1008 | return NULL; |
| 1009 | return *r; |
| 1010 | } |
| 1011 | EXPORT_SYMBOL_GPL(dev_get_regmap); |
| 1012 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1013 | static int _regmap_select_page(struct regmap *map, unsigned int *reg, |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1014 | struct regmap_range_node *range, |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1015 | unsigned int val_num) |
| 1016 | { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1017 | void *orig_work_buf; |
| 1018 | unsigned int win_offset; |
| 1019 | unsigned int win_page; |
| 1020 | bool page_chg; |
| 1021 | int ret; |
| 1022 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1023 | win_offset = (*reg - range->range_min) % range->window_len; |
| 1024 | win_page = (*reg - range->range_min) / range->window_len; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1025 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1026 | if (val_num > 1) { |
| 1027 | /* Bulk write shouldn't cross range boundary */ |
| 1028 | if (*reg + val_num - 1 > range->range_max) |
| 1029 | return -EINVAL; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1030 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1031 | /* ... or single page boundary */ |
| 1032 | if (val_num > range->window_len - win_offset) |
| 1033 | return -EINVAL; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1036 | /* It is possible to have selector register inside data window. |
| 1037 | In that case, selector register is located on every page and |
| 1038 | it needs no page switching, when accessed alone. */ |
| 1039 | if (val_num > 1 || |
| 1040 | range->window_start + win_offset != range->selector_reg) { |
| 1041 | /* Use separate work_buf during page switching */ |
| 1042 | orig_work_buf = map->work_buf; |
| 1043 | map->work_buf = map->selector_work_buf; |
| 1044 | |
| 1045 | ret = _regmap_update_bits(map, range->selector_reg, |
| 1046 | range->selector_mask, |
| 1047 | win_page << range->selector_shift, |
| 1048 | &page_chg); |
| 1049 | |
| 1050 | map->work_buf = orig_work_buf; |
| 1051 | |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 1052 | if (ret != 0) |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1053 | return ret; |
| 1054 | } |
| 1055 | |
| 1056 | *reg = range->window_start + win_offset; |
| 1057 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1058 | return 0; |
| 1059 | } |
| 1060 | |
Mark Brown | 584de32 | 2013-03-13 19:19:34 +0000 | [diff] [blame] | 1061 | int _regmap_raw_write(struct regmap *map, unsigned int reg, |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1062 | const void *val, size_t val_len) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1063 | { |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1064 | struct regmap_range_node *range; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1065 | unsigned long flags; |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1066 | u8 *u8 = map->work_buf; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1067 | void *work_val = map->work_buf + map->format.reg_bytes + |
| 1068 | map->format.pad_bytes; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1069 | void *buf; |
| 1070 | int ret = -ENOTSUPP; |
| 1071 | size_t len; |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1072 | int i; |
| 1073 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1074 | WARN_ON(!map->bus); |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1075 | |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1076 | /* Check for unwritable registers before we start */ |
| 1077 | if (map->writeable_reg) |
| 1078 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1079 | if (!map->writeable_reg(map->dev, |
| 1080 | reg + (i * map->reg_stride))) |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1081 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1082 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1083 | if (!map->cache_bypass && map->format.parse_val) { |
| 1084 | unsigned int ival; |
| 1085 | int val_bytes = map->format.val_bytes; |
| 1086 | for (i = 0; i < val_len / val_bytes; i++) { |
Stephen Warren | 5a08d15 | 2013-03-20 17:02:02 -0600 | [diff] [blame] | 1087 | ival = map->format.parse_val(val + (i * val_bytes)); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1088 | ret = regcache_write(map, reg + (i * map->reg_stride), |
| 1089 | ival); |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1090 | if (ret) { |
| 1091 | dev_err(map->dev, |
Mark Brown | 6d04b8a | 2012-10-26 19:05:32 +0100 | [diff] [blame] | 1092 | "Error in caching of register: %x ret: %d\n", |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1093 | reg + i, ret); |
| 1094 | return ret; |
| 1095 | } |
| 1096 | } |
| 1097 | if (map->cache_only) { |
| 1098 | map->cache_dirty = true; |
| 1099 | return 0; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1103 | range = _regmap_range_lookup(map, reg); |
| 1104 | if (range) { |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1105 | int val_num = val_len / map->format.val_bytes; |
| 1106 | int win_offset = (reg - range->range_min) % range->window_len; |
| 1107 | int win_residue = range->window_len - win_offset; |
| 1108 | |
| 1109 | /* If the write goes beyond the end of the window split it */ |
| 1110 | while (val_num > win_residue) { |
Fabio Estevam | 1a61cfe | 2012-10-25 14:07:18 -0200 | [diff] [blame] | 1111 | dev_dbg(map->dev, "Writing window %d/%zu\n", |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1112 | win_residue, val_len / map->format.val_bytes); |
| 1113 | ret = _regmap_raw_write(map, reg, val, win_residue * |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1114 | map->format.val_bytes); |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1115 | if (ret != 0) |
| 1116 | return ret; |
| 1117 | |
| 1118 | reg += win_residue; |
| 1119 | val_num -= win_residue; |
| 1120 | val += win_residue * map->format.val_bytes; |
| 1121 | val_len -= win_residue * map->format.val_bytes; |
| 1122 | |
| 1123 | win_offset = (reg - range->range_min) % |
| 1124 | range->window_len; |
| 1125 | win_residue = range->window_len - win_offset; |
| 1126 | } |
| 1127 | |
| 1128 | ret = _regmap_select_page(map, ®, range, val_num); |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 1129 | if (ret != 0) |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1130 | return ret; |
| 1131 | } |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1132 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1133 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1134 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1135 | u8[0] |= map->write_flag_mask; |
| 1136 | |
Mark Brown | 651e013 | 2013-10-08 18:37:36 +0100 | [diff] [blame] | 1137 | /* |
| 1138 | * Essentially all I/O mechanisms will be faster with a single |
| 1139 | * buffer to write. Since register syncs often generate raw |
| 1140 | * writes of single registers optimise that case. |
| 1141 | */ |
| 1142 | if (val != work_val && val_len == map->format.val_bytes) { |
| 1143 | memcpy(work_val, val, map->format.val_bytes); |
| 1144 | val = work_val; |
| 1145 | } |
| 1146 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1147 | if (map->async && map->bus->async_write) { |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1148 | struct regmap_async *async; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1149 | |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 1150 | trace_regmap_async_write_start(map->dev, reg, val_len); |
| 1151 | |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1152 | spin_lock_irqsave(&map->async_lock, flags); |
| 1153 | async = list_first_entry_or_null(&map->async_free, |
| 1154 | struct regmap_async, |
| 1155 | list); |
| 1156 | if (async) |
| 1157 | list_del(&async->list); |
| 1158 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 1159 | |
| 1160 | if (!async) { |
| 1161 | async = map->bus->async_alloc(); |
| 1162 | if (!async) |
| 1163 | return -ENOMEM; |
| 1164 | |
| 1165 | async->work_buf = kzalloc(map->format.buf_size, |
| 1166 | GFP_KERNEL | GFP_DMA); |
| 1167 | if (!async->work_buf) { |
| 1168 | kfree(async); |
| 1169 | return -ENOMEM; |
| 1170 | } |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1171 | } |
| 1172 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1173 | async->map = map; |
| 1174 | |
| 1175 | /* If the caller supplied the value we can use it safely. */ |
| 1176 | memcpy(async->work_buf, map->work_buf, map->format.pad_bytes + |
| 1177 | map->format.reg_bytes + map->format.val_bytes); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1178 | |
| 1179 | spin_lock_irqsave(&map->async_lock, flags); |
| 1180 | list_add_tail(&async->list, &map->async_list); |
| 1181 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 1182 | |
Mark Brown | 04c50cc | 2013-10-10 22:38:29 +0100 | [diff] [blame] | 1183 | if (val != work_val) |
| 1184 | ret = map->bus->async_write(map->bus_context, |
| 1185 | async->work_buf, |
| 1186 | map->format.reg_bytes + |
| 1187 | map->format.pad_bytes, |
| 1188 | val, val_len, async); |
| 1189 | else |
| 1190 | ret = map->bus->async_write(map->bus_context, |
| 1191 | async->work_buf, |
| 1192 | map->format.reg_bytes + |
| 1193 | map->format.pad_bytes + |
| 1194 | val_len, NULL, 0, async); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1195 | |
| 1196 | if (ret != 0) { |
| 1197 | dev_err(map->dev, "Failed to schedule write: %d\n", |
| 1198 | ret); |
| 1199 | |
| 1200 | spin_lock_irqsave(&map->async_lock, flags); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1201 | list_move(&async->list, &map->async_free); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1202 | spin_unlock_irqrestore(&map->async_lock, flags); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1203 | } |
Mark Brown | f951b65 | 2013-03-27 13:08:44 +0000 | [diff] [blame] | 1204 | |
| 1205 | return ret; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1206 | } |
| 1207 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1208 | trace_regmap_hw_write_start(map->dev, reg, |
| 1209 | val_len / map->format.val_bytes); |
| 1210 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1211 | /* If we're doing a single register write we can probably just |
| 1212 | * send the work_buf directly, otherwise try to do a gather |
| 1213 | * write. |
| 1214 | */ |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1215 | if (val == work_val) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1216 | ret = map->bus->write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1217 | map->format.reg_bytes + |
| 1218 | map->format.pad_bytes + |
| 1219 | val_len); |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1220 | else if (map->bus->gather_write) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1221 | ret = map->bus->gather_write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1222 | map->format.reg_bytes + |
| 1223 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1224 | val, val_len); |
| 1225 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1226 | /* If that didn't work fall back on linearising by hand. */ |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1227 | if (ret == -ENOTSUPP) { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1228 | len = map->format.reg_bytes + map->format.pad_bytes + val_len; |
| 1229 | buf = kzalloc(len, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1230 | if (!buf) |
| 1231 | return -ENOMEM; |
| 1232 | |
| 1233 | memcpy(buf, map->work_buf, map->format.reg_bytes); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1234 | memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, |
| 1235 | val, val_len); |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1236 | ret = map->bus->write(map->bus_context, buf, len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1237 | |
| 1238 | kfree(buf); |
| 1239 | } |
| 1240 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1241 | trace_regmap_hw_write_done(map->dev, reg, |
| 1242 | val_len / map->format.val_bytes); |
| 1243 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1244 | return ret; |
| 1245 | } |
| 1246 | |
Mark Brown | 221ad7f | 2013-03-26 21:24:20 +0000 | [diff] [blame] | 1247 | /** |
| 1248 | * regmap_can_raw_write - Test if regmap_raw_write() is supported |
| 1249 | * |
| 1250 | * @map: Map to check. |
| 1251 | */ |
| 1252 | bool regmap_can_raw_write(struct regmap *map) |
| 1253 | { |
| 1254 | return map->bus && map->format.format_val && map->format.format_reg; |
| 1255 | } |
| 1256 | EXPORT_SYMBOL_GPL(regmap_can_raw_write); |
| 1257 | |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1258 | static int _regmap_bus_formatted_write(void *context, unsigned int reg, |
| 1259 | unsigned int val) |
| 1260 | { |
| 1261 | int ret; |
| 1262 | struct regmap_range_node *range; |
| 1263 | struct regmap *map = context; |
| 1264 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1265 | WARN_ON(!map->bus || !map->format.format_write); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1266 | |
| 1267 | range = _regmap_range_lookup(map, reg); |
| 1268 | if (range) { |
| 1269 | ret = _regmap_select_page(map, ®, range, 1); |
| 1270 | if (ret != 0) |
| 1271 | return ret; |
| 1272 | } |
| 1273 | |
| 1274 | map->format.format_write(map, reg, val); |
| 1275 | |
| 1276 | trace_regmap_hw_write_start(map->dev, reg, 1); |
| 1277 | |
| 1278 | ret = map->bus->write(map->bus_context, map->work_buf, |
| 1279 | map->format.buf_size); |
| 1280 | |
| 1281 | trace_regmap_hw_write_done(map->dev, reg, 1); |
| 1282 | |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
| 1286 | static int _regmap_bus_raw_write(void *context, unsigned int reg, |
| 1287 | unsigned int val) |
| 1288 | { |
| 1289 | struct regmap *map = context; |
| 1290 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1291 | WARN_ON(!map->bus || !map->format.format_val); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1292 | |
| 1293 | map->format.format_val(map->work_buf + map->format.reg_bytes |
| 1294 | + map->format.pad_bytes, val, 0); |
| 1295 | return _regmap_raw_write(map, reg, |
| 1296 | map->work_buf + |
| 1297 | map->format.reg_bytes + |
| 1298 | map->format.pad_bytes, |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1299 | map->format.val_bytes); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1302 | static inline void *_regmap_map_get_context(struct regmap *map) |
| 1303 | { |
| 1304 | return (map->bus) ? map : map->bus_context; |
| 1305 | } |
| 1306 | |
Dimitris Papastamos | 4d2dc09 | 2011-09-29 10:39:07 +0100 | [diff] [blame] | 1307 | int _regmap_write(struct regmap *map, unsigned int reg, |
| 1308 | unsigned int val) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1309 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1310 | int ret; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1311 | void *context = _regmap_map_get_context(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1312 | |
Ionut Nicu | 515f226 | 2013-08-09 12:09:20 +0200 | [diff] [blame] | 1313 | if (!regmap_writeable(map, reg)) |
| 1314 | return -EIO; |
| 1315 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1316 | if (!map->cache_bypass && !map->defer_caching) { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1317 | ret = regcache_write(map, reg, val); |
| 1318 | if (ret != 0) |
| 1319 | return ret; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 1320 | if (map->cache_only) { |
| 1321 | map->cache_dirty = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1322 | return 0; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 1323 | } |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 1326 | #ifdef LOG_DEVICE |
| 1327 | if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0) |
| 1328 | dev_info(map->dev, "%x <= %x\n", reg, val); |
| 1329 | #endif |
| 1330 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1331 | trace_regmap_reg_write(map->dev, reg, val); |
| 1332 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1333 | return map->reg_write(context, reg, val); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /** |
| 1337 | * regmap_write(): Write a value to a single register |
| 1338 | * |
| 1339 | * @map: Register map to write to |
| 1340 | * @reg: Register to write to |
| 1341 | * @val: Value to be written |
| 1342 | * |
| 1343 | * A value of zero will be returned on success, a negative errno will |
| 1344 | * be returned in error cases. |
| 1345 | */ |
| 1346 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) |
| 1347 | { |
| 1348 | int ret; |
| 1349 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1350 | if (reg % map->reg_stride) |
| 1351 | return -EINVAL; |
| 1352 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1353 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1354 | |
| 1355 | ret = _regmap_write(map, reg, val); |
| 1356 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1357 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1358 | |
| 1359 | return ret; |
| 1360 | } |
| 1361 | EXPORT_SYMBOL_GPL(regmap_write); |
| 1362 | |
| 1363 | /** |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 1364 | * regmap_write_async(): Write a value to a single register asynchronously |
| 1365 | * |
| 1366 | * @map: Register map to write to |
| 1367 | * @reg: Register to write to |
| 1368 | * @val: Value to be written |
| 1369 | * |
| 1370 | * A value of zero will be returned on success, a negative errno will |
| 1371 | * be returned in error cases. |
| 1372 | */ |
| 1373 | int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val) |
| 1374 | { |
| 1375 | int ret; |
| 1376 | |
| 1377 | if (reg % map->reg_stride) |
| 1378 | return -EINVAL; |
| 1379 | |
| 1380 | map->lock(map->lock_arg); |
| 1381 | |
| 1382 | map->async = true; |
| 1383 | |
| 1384 | ret = _regmap_write(map, reg, val); |
| 1385 | |
| 1386 | map->async = false; |
| 1387 | |
| 1388 | map->unlock(map->lock_arg); |
| 1389 | |
| 1390 | return ret; |
| 1391 | } |
| 1392 | EXPORT_SYMBOL_GPL(regmap_write_async); |
| 1393 | |
| 1394 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1395 | * regmap_raw_write(): Write raw values to one or more registers |
| 1396 | * |
| 1397 | * @map: Register map to write to |
| 1398 | * @reg: Initial register to write to |
| 1399 | * @val: Block of data to be written, laid out for direct transmission to the |
| 1400 | * device |
| 1401 | * @val_len: Length of data pointed to by val. |
| 1402 | * |
| 1403 | * This function is intended to be used for things like firmware |
| 1404 | * download where a large block of data needs to be transferred to the |
| 1405 | * device. No formatting will be done on the data provided. |
| 1406 | * |
| 1407 | * A value of zero will be returned on success, a negative errno will |
| 1408 | * be returned in error cases. |
| 1409 | */ |
| 1410 | int regmap_raw_write(struct regmap *map, unsigned int reg, |
| 1411 | const void *val, size_t val_len) |
| 1412 | { |
| 1413 | int ret; |
| 1414 | |
Mark Brown | 221ad7f | 2013-03-26 21:24:20 +0000 | [diff] [blame] | 1415 | if (!regmap_can_raw_write(map)) |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1416 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1417 | if (val_len % map->format.val_bytes) |
| 1418 | return -EINVAL; |
| 1419 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1420 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1421 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1422 | ret = _regmap_raw_write(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1423 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1424 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1425 | |
| 1426 | return ret; |
| 1427 | } |
| 1428 | EXPORT_SYMBOL_GPL(regmap_raw_write); |
| 1429 | |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 1430 | /** |
| 1431 | * regmap_field_write(): Write a value to a single register field |
| 1432 | * |
| 1433 | * @field: Register field to write to |
| 1434 | * @val: Value to be written |
| 1435 | * |
| 1436 | * A value of zero will be returned on success, a negative errno will |
| 1437 | * be returned in error cases. |
| 1438 | */ |
| 1439 | int regmap_field_write(struct regmap_field *field, unsigned int val) |
| 1440 | { |
| 1441 | return regmap_update_bits(field->regmap, field->reg, |
| 1442 | field->mask, val << field->shift); |
| 1443 | } |
| 1444 | EXPORT_SYMBOL_GPL(regmap_field_write); |
| 1445 | |
Kuninori Morimoto | fdf2002 | 2013-09-01 20:24:50 -0700 | [diff] [blame] | 1446 | /** |
| 1447 | * regmap_field_update_bits(): Perform a read/modify/write cycle |
| 1448 | * on the register field |
| 1449 | * |
| 1450 | * @field: Register field to write to |
| 1451 | * @mask: Bitmask to change |
| 1452 | * @val: Value to be written |
| 1453 | * |
| 1454 | * A value of zero will be returned on success, a negative errno will |
| 1455 | * be returned in error cases. |
| 1456 | */ |
| 1457 | int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val) |
| 1458 | { |
| 1459 | mask = (mask << field->shift) & field->mask; |
| 1460 | |
| 1461 | return regmap_update_bits(field->regmap, field->reg, |
| 1462 | mask, val << field->shift); |
| 1463 | } |
| 1464 | EXPORT_SYMBOL_GPL(regmap_field_update_bits); |
| 1465 | |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 1466 | /** |
| 1467 | * regmap_fields_write(): Write a value to a single register field with port ID |
| 1468 | * |
| 1469 | * @field: Register field to write to |
| 1470 | * @id: port ID |
| 1471 | * @val: Value to be written |
| 1472 | * |
| 1473 | * A value of zero will be returned on success, a negative errno will |
| 1474 | * be returned in error cases. |
| 1475 | */ |
| 1476 | int regmap_fields_write(struct regmap_field *field, unsigned int id, |
| 1477 | unsigned int val) |
| 1478 | { |
| 1479 | if (id >= field->id_size) |
| 1480 | return -EINVAL; |
| 1481 | |
| 1482 | return regmap_update_bits(field->regmap, |
| 1483 | field->reg + (field->id_offset * id), |
| 1484 | field->mask, val << field->shift); |
| 1485 | } |
| 1486 | EXPORT_SYMBOL_GPL(regmap_fields_write); |
| 1487 | |
| 1488 | /** |
| 1489 | * regmap_fields_update_bits(): Perform a read/modify/write cycle |
| 1490 | * on the register field |
| 1491 | * |
| 1492 | * @field: Register field to write to |
| 1493 | * @id: port ID |
| 1494 | * @mask: Bitmask to change |
| 1495 | * @val: Value to be written |
| 1496 | * |
| 1497 | * A value of zero will be returned on success, a negative errno will |
| 1498 | * be returned in error cases. |
| 1499 | */ |
| 1500 | int regmap_fields_update_bits(struct regmap_field *field, unsigned int id, |
| 1501 | unsigned int mask, unsigned int val) |
| 1502 | { |
| 1503 | if (id >= field->id_size) |
| 1504 | return -EINVAL; |
| 1505 | |
| 1506 | mask = (mask << field->shift) & field->mask; |
| 1507 | |
| 1508 | return regmap_update_bits(field->regmap, |
| 1509 | field->reg + (field->id_offset * id), |
| 1510 | mask, val << field->shift); |
| 1511 | } |
| 1512 | EXPORT_SYMBOL_GPL(regmap_fields_update_bits); |
| 1513 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1514 | /* |
| 1515 | * regmap_bulk_write(): Write multiple registers to the device |
| 1516 | * |
| 1517 | * @map: Register map to write to |
| 1518 | * @reg: First register to be write from |
| 1519 | * @val: Block of data to be written, in native register size for device |
| 1520 | * @val_count: Number of registers to write |
| 1521 | * |
| 1522 | * 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] | 1523 | * data to the device either in single transfer or multiple transfer. |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1524 | * |
| 1525 | * A value of zero will be returned on success, a negative errno will |
| 1526 | * be returned in error cases. |
| 1527 | */ |
| 1528 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, |
| 1529 | size_t val_count) |
| 1530 | { |
| 1531 | int ret = 0, i; |
| 1532 | size_t val_bytes = map->format.val_bytes; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1533 | |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1534 | if (map->bus && !map->format.parse_inplace) |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1535 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1536 | if (reg % map->reg_stride) |
| 1537 | return -EINVAL; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1538 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1539 | map->lock(map->lock_arg); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1540 | /* |
| 1541 | * Some devices don't support bulk write, for |
| 1542 | * them we have a series of single write operations. |
| 1543 | */ |
| 1544 | if (!map->bus || map->use_single_rw) { |
| 1545 | for (i = 0; i < val_count; i++) { |
| 1546 | unsigned int ival; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1547 | |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1548 | switch (val_bytes) { |
| 1549 | case 1: |
| 1550 | ival = *(u8 *)(val + (i * val_bytes)); |
| 1551 | break; |
| 1552 | case 2: |
| 1553 | ival = *(u16 *)(val + (i * val_bytes)); |
| 1554 | break; |
| 1555 | case 4: |
| 1556 | ival = *(u32 *)(val + (i * val_bytes)); |
| 1557 | break; |
| 1558 | #ifdef CONFIG_64BIT |
| 1559 | case 8: |
| 1560 | ival = *(u64 *)(val + (i * val_bytes)); |
| 1561 | break; |
| 1562 | #endif |
| 1563 | default: |
| 1564 | ret = -EINVAL; |
| 1565 | goto out; |
| 1566 | } |
| 1567 | |
| 1568 | ret = _regmap_write(map, reg + (i * map->reg_stride), |
| 1569 | ival); |
| 1570 | if (ret != 0) |
| 1571 | goto out; |
| 1572 | } |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1573 | } else { |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1574 | void *wval; |
| 1575 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1576 | wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); |
| 1577 | if (!wval) { |
| 1578 | ret = -ENOMEM; |
| 1579 | dev_err(map->dev, "Error in memory allocation\n"); |
| 1580 | goto out; |
| 1581 | } |
| 1582 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 1583 | map->format.parse_inplace(wval + i); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1584 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1585 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1586 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1587 | kfree(wval); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1588 | } |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1589 | out: |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1590 | map->unlock(map->lock_arg); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1591 | return ret; |
| 1592 | } |
| 1593 | EXPORT_SYMBOL_GPL(regmap_bulk_write); |
| 1594 | |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 1595 | /* |
| 1596 | * regmap_multi_reg_write(): Write multiple registers to the device |
| 1597 | * |
| 1598 | * where the set of register are supplied in any order |
| 1599 | * |
| 1600 | * @map: Register map to write to |
| 1601 | * @regs: Array of structures containing register,value to be written |
| 1602 | * @num_regs: Number of registers to write |
| 1603 | * |
| 1604 | * This function is intended to be used for writing a large block of data |
| 1605 | * atomically to the device in single transfer for those I2C client devices |
| 1606 | * that implement this alternative block write mode. |
| 1607 | * |
| 1608 | * A value of zero will be returned on success, a negative errno will |
| 1609 | * be returned in error cases. |
| 1610 | */ |
| 1611 | int regmap_multi_reg_write(struct regmap *map, struct reg_default *regs, |
| 1612 | int num_regs) |
| 1613 | { |
| 1614 | int ret = 0, i; |
| 1615 | |
| 1616 | for (i = 0; i < num_regs; i++) { |
| 1617 | int reg = regs[i].reg; |
| 1618 | if (reg % map->reg_stride) |
| 1619 | return -EINVAL; |
| 1620 | } |
| 1621 | |
| 1622 | map->lock(map->lock_arg); |
| 1623 | |
| 1624 | for (i = 0; i < num_regs; i++) { |
| 1625 | ret = _regmap_write(map, regs[i].reg, regs[i].def); |
| 1626 | if (ret != 0) |
| 1627 | goto out; |
| 1628 | } |
| 1629 | out: |
| 1630 | map->unlock(map->lock_arg); |
| 1631 | |
| 1632 | return ret; |
| 1633 | } |
| 1634 | EXPORT_SYMBOL_GPL(regmap_multi_reg_write); |
| 1635 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1636 | /** |
| 1637 | * regmap_raw_write_async(): Write raw values to one or more registers |
| 1638 | * asynchronously |
| 1639 | * |
| 1640 | * @map: Register map to write to |
| 1641 | * @reg: Initial register to write to |
| 1642 | * @val: Block of data to be written, laid out for direct transmission to the |
| 1643 | * device. Must be valid until regmap_async_complete() is called. |
| 1644 | * @val_len: Length of data pointed to by val. |
| 1645 | * |
| 1646 | * This function is intended to be used for things like firmware |
| 1647 | * download where a large block of data needs to be transferred to the |
| 1648 | * device. No formatting will be done on the data provided. |
| 1649 | * |
| 1650 | * If supported by the underlying bus the write will be scheduled |
| 1651 | * asynchronously, helping maximise I/O speed on higher speed buses |
| 1652 | * like SPI. regmap_async_complete() can be called to ensure that all |
| 1653 | * asynchrnous writes have been completed. |
| 1654 | * |
| 1655 | * A value of zero will be returned on success, a negative errno will |
| 1656 | * be returned in error cases. |
| 1657 | */ |
| 1658 | int regmap_raw_write_async(struct regmap *map, unsigned int reg, |
| 1659 | const void *val, size_t val_len) |
| 1660 | { |
| 1661 | int ret; |
| 1662 | |
| 1663 | if (val_len % map->format.val_bytes) |
| 1664 | return -EINVAL; |
| 1665 | if (reg % map->reg_stride) |
| 1666 | return -EINVAL; |
| 1667 | |
| 1668 | map->lock(map->lock_arg); |
| 1669 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1670 | map->async = true; |
| 1671 | |
| 1672 | ret = _regmap_raw_write(map, reg, val, val_len); |
| 1673 | |
| 1674 | map->async = false; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1675 | |
| 1676 | map->unlock(map->lock_arg); |
| 1677 | |
| 1678 | return ret; |
| 1679 | } |
| 1680 | EXPORT_SYMBOL_GPL(regmap_raw_write_async); |
| 1681 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1682 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 1683 | unsigned int val_len) |
| 1684 | { |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1685 | struct regmap_range_node *range; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1686 | u8 *u8 = map->work_buf; |
| 1687 | int ret; |
| 1688 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1689 | WARN_ON(!map->bus); |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1690 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1691 | range = _regmap_range_lookup(map, reg); |
| 1692 | if (range) { |
| 1693 | ret = _regmap_select_page(map, ®, range, |
| 1694 | val_len / map->format.val_bytes); |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 1695 | if (ret != 0) |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1696 | return ret; |
| 1697 | } |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1698 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1699 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1700 | |
| 1701 | /* |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1702 | * 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] | 1703 | * register addresss; since it's always the high bits for all |
| 1704 | * current formats we can do this here rather than in |
| 1705 | * formatting. This may break if we get interesting formats. |
| 1706 | */ |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1707 | u8[0] |= map->read_flag_mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1708 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1709 | trace_regmap_hw_read_start(map->dev, reg, |
| 1710 | val_len / map->format.val_bytes); |
| 1711 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1712 | ret = map->bus->read(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1713 | map->format.reg_bytes + map->format.pad_bytes, |
Mark Brown | 40c5cc2 | 2011-07-24 22:39:12 +0100 | [diff] [blame] | 1714 | val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1715 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1716 | trace_regmap_hw_read_done(map->dev, reg, |
| 1717 | val_len / map->format.val_bytes); |
| 1718 | |
| 1719 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1720 | } |
| 1721 | |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 1722 | static int _regmap_bus_read(void *context, unsigned int reg, |
| 1723 | unsigned int *val) |
| 1724 | { |
| 1725 | int ret; |
| 1726 | struct regmap *map = context; |
| 1727 | |
| 1728 | if (!map->format.parse_val) |
| 1729 | return -EINVAL; |
| 1730 | |
| 1731 | ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); |
| 1732 | if (ret == 0) |
| 1733 | *val = map->format.parse_val(map->work_buf); |
| 1734 | |
| 1735 | return ret; |
| 1736 | } |
| 1737 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1738 | static int _regmap_read(struct regmap *map, unsigned int reg, |
| 1739 | unsigned int *val) |
| 1740 | { |
| 1741 | int ret; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1742 | void *context = _regmap_map_get_context(map); |
| 1743 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1744 | WARN_ON(!map->reg_read); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1745 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1746 | if (!map->cache_bypass) { |
| 1747 | ret = regcache_read(map, reg, val); |
| 1748 | if (ret == 0) |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | if (map->cache_only) |
| 1753 | return -EBUSY; |
| 1754 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1755 | ret = map->reg_read(context, reg, val); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1756 | if (ret == 0) { |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 1757 | #ifdef LOG_DEVICE |
| 1758 | if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0) |
| 1759 | dev_info(map->dev, "%x => %x\n", reg, *val); |
| 1760 | #endif |
| 1761 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1762 | trace_regmap_reg_read(map->dev, reg, *val); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1763 | |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 1764 | if (!map->cache_bypass) |
| 1765 | regcache_write(map, reg, *val); |
| 1766 | } |
Mark Brown | f298536 | 2012-04-30 21:25:05 +0100 | [diff] [blame] | 1767 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1768 | return ret; |
| 1769 | } |
| 1770 | |
| 1771 | /** |
| 1772 | * regmap_read(): Read a value from a single register |
| 1773 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 1774 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1775 | * @reg: Register to be read from |
| 1776 | * @val: Pointer to store read value |
| 1777 | * |
| 1778 | * A value of zero will be returned on success, a negative errno will |
| 1779 | * be returned in error cases. |
| 1780 | */ |
| 1781 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) |
| 1782 | { |
| 1783 | int ret; |
| 1784 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1785 | if (reg % map->reg_stride) |
| 1786 | return -EINVAL; |
| 1787 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1788 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1789 | |
| 1790 | ret = _regmap_read(map, reg, val); |
| 1791 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1792 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1793 | |
| 1794 | return ret; |
| 1795 | } |
| 1796 | EXPORT_SYMBOL_GPL(regmap_read); |
| 1797 | |
| 1798 | /** |
| 1799 | * regmap_raw_read(): Read raw data from the device |
| 1800 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 1801 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1802 | * @reg: First register to be read from |
| 1803 | * @val: Pointer to store read value |
| 1804 | * @val_len: Size of data to read |
| 1805 | * |
| 1806 | * A value of zero will be returned on success, a negative errno will |
| 1807 | * be returned in error cases. |
| 1808 | */ |
| 1809 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 1810 | size_t val_len) |
| 1811 | { |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1812 | size_t val_bytes = map->format.val_bytes; |
| 1813 | size_t val_count = val_len / val_bytes; |
| 1814 | unsigned int v; |
| 1815 | int ret, i; |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 1816 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1817 | if (!map->bus) |
| 1818 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1819 | if (val_len % map->format.val_bytes) |
| 1820 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1821 | if (reg % map->reg_stride) |
| 1822 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1823 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1824 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1825 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1826 | if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || |
| 1827 | map->cache_type == REGCACHE_NONE) { |
| 1828 | /* Physical block read if there's no cache involved */ |
| 1829 | ret = _regmap_raw_read(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1830 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1831 | } else { |
| 1832 | /* Otherwise go word by word for the cache; should be low |
| 1833 | * cost as we expect to hit the cache. |
| 1834 | */ |
| 1835 | for (i = 0; i < val_count; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1836 | ret = _regmap_read(map, reg + (i * map->reg_stride), |
| 1837 | &v); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1838 | if (ret != 0) |
| 1839 | goto out; |
| 1840 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1841 | map->format.format_val(val + (i * val_bytes), v, 0); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | out: |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1846 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1847 | |
| 1848 | return ret; |
| 1849 | } |
| 1850 | EXPORT_SYMBOL_GPL(regmap_raw_read); |
| 1851 | |
| 1852 | /** |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 1853 | * regmap_field_read(): Read a value to a single register field |
| 1854 | * |
| 1855 | * @field: Register field to read from |
| 1856 | * @val: Pointer to store read value |
| 1857 | * |
| 1858 | * A value of zero will be returned on success, a negative errno will |
| 1859 | * be returned in error cases. |
| 1860 | */ |
| 1861 | int regmap_field_read(struct regmap_field *field, unsigned int *val) |
| 1862 | { |
| 1863 | int ret; |
| 1864 | unsigned int reg_val; |
| 1865 | ret = regmap_read(field->regmap, field->reg, ®_val); |
| 1866 | if (ret != 0) |
| 1867 | return ret; |
| 1868 | |
| 1869 | reg_val &= field->mask; |
| 1870 | reg_val >>= field->shift; |
| 1871 | *val = reg_val; |
| 1872 | |
| 1873 | return ret; |
| 1874 | } |
| 1875 | EXPORT_SYMBOL_GPL(regmap_field_read); |
| 1876 | |
| 1877 | /** |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 1878 | * regmap_fields_read(): Read a value to a single register field with port ID |
| 1879 | * |
| 1880 | * @field: Register field to read from |
| 1881 | * @id: port ID |
| 1882 | * @val: Pointer to store read value |
| 1883 | * |
| 1884 | * A value of zero will be returned on success, a negative errno will |
| 1885 | * be returned in error cases. |
| 1886 | */ |
| 1887 | int regmap_fields_read(struct regmap_field *field, unsigned int id, |
| 1888 | unsigned int *val) |
| 1889 | { |
| 1890 | int ret; |
| 1891 | unsigned int reg_val; |
| 1892 | |
| 1893 | if (id >= field->id_size) |
| 1894 | return -EINVAL; |
| 1895 | |
| 1896 | ret = regmap_read(field->regmap, |
| 1897 | field->reg + (field->id_offset * id), |
| 1898 | ®_val); |
| 1899 | if (ret != 0) |
| 1900 | return ret; |
| 1901 | |
| 1902 | reg_val &= field->mask; |
| 1903 | reg_val >>= field->shift; |
| 1904 | *val = reg_val; |
| 1905 | |
| 1906 | return ret; |
| 1907 | } |
| 1908 | EXPORT_SYMBOL_GPL(regmap_fields_read); |
| 1909 | |
| 1910 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1911 | * regmap_bulk_read(): Read multiple registers from the device |
| 1912 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 1913 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1914 | * @reg: First register to be read from |
| 1915 | * @val: Pointer to store read value, in native register size for device |
| 1916 | * @val_count: Number of registers to read |
| 1917 | * |
| 1918 | * A value of zero will be returned on success, a negative errno will |
| 1919 | * be returned in error cases. |
| 1920 | */ |
| 1921 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, |
| 1922 | size_t val_count) |
| 1923 | { |
| 1924 | int ret, i; |
| 1925 | size_t val_bytes = map->format.val_bytes; |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 1926 | bool vol = regmap_volatile_range(map, reg, val_count); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1927 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1928 | if (reg % map->reg_stride) |
| 1929 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1930 | |
Stephen Boyd | 3b58ee1 | 2013-12-13 09:14:07 -0800 | [diff] [blame] | 1931 | 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] | 1932 | /* |
| 1933 | * Some devices does not support bulk read, for |
| 1934 | * them we have a series of single read operations. |
| 1935 | */ |
| 1936 | if (map->use_single_rw) { |
| 1937 | for (i = 0; i < val_count; i++) { |
| 1938 | ret = regmap_raw_read(map, |
| 1939 | reg + (i * map->reg_stride), |
| 1940 | val + (i * val_bytes), |
| 1941 | val_bytes); |
| 1942 | if (ret != 0) |
| 1943 | return ret; |
| 1944 | } |
| 1945 | } else { |
| 1946 | ret = regmap_raw_read(map, reg, val, |
| 1947 | val_bytes * val_count); |
| 1948 | if (ret != 0) |
| 1949 | return ret; |
| 1950 | } |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1951 | |
| 1952 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 1953 | map->format.parse_inplace(val + i); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1954 | } else { |
| 1955 | for (i = 0; i < val_count; i++) { |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 1956 | unsigned int ival; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1957 | ret = regmap_read(map, reg + (i * map->reg_stride), |
Mark Brown | 25061d2 | 2012-05-12 13:06:08 +0100 | [diff] [blame] | 1958 | &ival); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1959 | if (ret != 0) |
| 1960 | return ret; |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 1961 | memcpy(val + (i * val_bytes), &ival, val_bytes); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1962 | } |
| 1963 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1964 | |
| 1965 | return 0; |
| 1966 | } |
| 1967 | EXPORT_SYMBOL_GPL(regmap_bulk_read); |
| 1968 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1969 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 1970 | unsigned int mask, unsigned int val, |
| 1971 | bool *change) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1972 | { |
| 1973 | int ret; |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1974 | unsigned int tmp, orig; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1975 | |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1976 | ret = _regmap_read(map, reg, &orig); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1977 | if (ret != 0) |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1978 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1979 | |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1980 | tmp = orig & ~mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1981 | tmp |= val & mask; |
| 1982 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1983 | if (tmp != orig) { |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1984 | ret = _regmap_write(map, reg, tmp); |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1985 | *change = true; |
| 1986 | } else { |
| 1987 | *change = false; |
| 1988 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1989 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1990 | return ret; |
| 1991 | } |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1992 | |
| 1993 | /** |
| 1994 | * regmap_update_bits: Perform a read/modify/write cycle on the register map |
| 1995 | * |
| 1996 | * @map: Register map to update |
| 1997 | * @reg: Register to update |
| 1998 | * @mask: Bitmask to change |
| 1999 | * @val: New value for bitmask |
| 2000 | * |
| 2001 | * Returns zero for success, a negative number on error. |
| 2002 | */ |
| 2003 | int regmap_update_bits(struct regmap *map, unsigned int reg, |
| 2004 | unsigned int mask, unsigned int val) |
| 2005 | { |
| 2006 | bool change; |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2007 | int ret; |
| 2008 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2009 | map->lock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2010 | ret = _regmap_update_bits(map, reg, mask, val, &change); |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2011 | map->unlock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2012 | |
| 2013 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2014 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2015 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 2016 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2017 | /** |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2018 | * regmap_update_bits_async: Perform a read/modify/write cycle on the register |
| 2019 | * map asynchronously |
| 2020 | * |
| 2021 | * @map: Register map to update |
| 2022 | * @reg: Register to update |
| 2023 | * @mask: Bitmask to change |
| 2024 | * @val: New value for bitmask |
| 2025 | * |
| 2026 | * With most buses the read must be done synchronously so this is most |
| 2027 | * useful for devices with a cache which do not need to interact with |
| 2028 | * the hardware to determine the current register value. |
| 2029 | * |
| 2030 | * Returns zero for success, a negative number on error. |
| 2031 | */ |
| 2032 | int regmap_update_bits_async(struct regmap *map, unsigned int reg, |
| 2033 | unsigned int mask, unsigned int val) |
| 2034 | { |
| 2035 | bool change; |
| 2036 | int ret; |
| 2037 | |
| 2038 | map->lock(map->lock_arg); |
| 2039 | |
| 2040 | map->async = true; |
| 2041 | |
| 2042 | ret = _regmap_update_bits(map, reg, mask, val, &change); |
| 2043 | |
| 2044 | map->async = false; |
| 2045 | |
| 2046 | map->unlock(map->lock_arg); |
| 2047 | |
| 2048 | return ret; |
| 2049 | } |
| 2050 | EXPORT_SYMBOL_GPL(regmap_update_bits_async); |
| 2051 | |
| 2052 | /** |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2053 | * regmap_update_bits_check: Perform a read/modify/write cycle on the |
| 2054 | * register map and report if updated |
| 2055 | * |
| 2056 | * @map: Register map to update |
| 2057 | * @reg: Register to update |
| 2058 | * @mask: Bitmask to change |
| 2059 | * @val: New value for bitmask |
| 2060 | * @change: Boolean indicating if a write was done |
| 2061 | * |
| 2062 | * Returns zero for success, a negative number on error. |
| 2063 | */ |
| 2064 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, |
| 2065 | unsigned int mask, unsigned int val, |
| 2066 | bool *change) |
| 2067 | { |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2068 | int ret; |
| 2069 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2070 | map->lock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2071 | ret = _regmap_update_bits(map, reg, mask, val, change); |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2072 | map->unlock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2073 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2074 | } |
| 2075 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); |
| 2076 | |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2077 | /** |
| 2078 | * regmap_update_bits_check_async: Perform a read/modify/write cycle on the |
| 2079 | * register map asynchronously and report if |
| 2080 | * updated |
| 2081 | * |
| 2082 | * @map: Register map to update |
| 2083 | * @reg: Register to update |
| 2084 | * @mask: Bitmask to change |
| 2085 | * @val: New value for bitmask |
| 2086 | * @change: Boolean indicating if a write was done |
| 2087 | * |
| 2088 | * With most buses the read must be done synchronously so this is most |
| 2089 | * useful for devices with a cache which do not need to interact with |
| 2090 | * the hardware to determine the current register value. |
| 2091 | * |
| 2092 | * Returns zero for success, a negative number on error. |
| 2093 | */ |
| 2094 | int regmap_update_bits_check_async(struct regmap *map, unsigned int reg, |
| 2095 | unsigned int mask, unsigned int val, |
| 2096 | bool *change) |
| 2097 | { |
| 2098 | int ret; |
| 2099 | |
| 2100 | map->lock(map->lock_arg); |
| 2101 | |
| 2102 | map->async = true; |
| 2103 | |
| 2104 | ret = _regmap_update_bits(map, reg, mask, val, change); |
| 2105 | |
| 2106 | map->async = false; |
| 2107 | |
| 2108 | map->unlock(map->lock_arg); |
| 2109 | |
| 2110 | return ret; |
| 2111 | } |
| 2112 | EXPORT_SYMBOL_GPL(regmap_update_bits_check_async); |
| 2113 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2114 | void regmap_async_complete_cb(struct regmap_async *async, int ret) |
| 2115 | { |
| 2116 | struct regmap *map = async->map; |
| 2117 | bool wake; |
| 2118 | |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2119 | trace_regmap_async_io_complete(map->dev); |
| 2120 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2121 | spin_lock(&map->async_lock); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 2122 | list_move(&async->list, &map->async_free); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2123 | wake = list_empty(&map->async_list); |
| 2124 | |
| 2125 | if (ret != 0) |
| 2126 | map->async_ret = ret; |
| 2127 | |
| 2128 | spin_unlock(&map->async_lock); |
| 2129 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2130 | if (wake) |
| 2131 | wake_up(&map->async_waitq); |
| 2132 | } |
Axel Lin | f804fb5 | 2013-02-03 00:14:13 +0800 | [diff] [blame] | 2133 | EXPORT_SYMBOL_GPL(regmap_async_complete_cb); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2134 | |
| 2135 | static int regmap_async_is_done(struct regmap *map) |
| 2136 | { |
| 2137 | unsigned long flags; |
| 2138 | int ret; |
| 2139 | |
| 2140 | spin_lock_irqsave(&map->async_lock, flags); |
| 2141 | ret = list_empty(&map->async_list); |
| 2142 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 2143 | |
| 2144 | return ret; |
| 2145 | } |
| 2146 | |
| 2147 | /** |
| 2148 | * regmap_async_complete: Ensure all asynchronous I/O has completed. |
| 2149 | * |
| 2150 | * @map: Map to operate on. |
| 2151 | * |
| 2152 | * Blocks until any pending asynchronous I/O has completed. Returns |
| 2153 | * an error code for any failed I/O operations. |
| 2154 | */ |
| 2155 | int regmap_async_complete(struct regmap *map) |
| 2156 | { |
| 2157 | unsigned long flags; |
| 2158 | int ret; |
| 2159 | |
| 2160 | /* Nothing to do with no async support */ |
Daniel Mack | f2e055e | 2013-07-04 13:11:03 +0200 | [diff] [blame] | 2161 | if (!map->bus || !map->bus->async_write) |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2162 | return 0; |
| 2163 | |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2164 | trace_regmap_async_complete_start(map->dev); |
| 2165 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2166 | wait_event(map->async_waitq, regmap_async_is_done(map)); |
| 2167 | |
| 2168 | spin_lock_irqsave(&map->async_lock, flags); |
| 2169 | ret = map->async_ret; |
| 2170 | map->async_ret = 0; |
| 2171 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 2172 | |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2173 | trace_regmap_async_complete_done(map->dev); |
| 2174 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2175 | return ret; |
| 2176 | } |
Mark Brown | f88948e | 2013-02-05 13:53:26 +0000 | [diff] [blame] | 2177 | EXPORT_SYMBOL_GPL(regmap_async_complete); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2178 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2179 | /** |
| 2180 | * regmap_register_patch: Register and apply register updates to be applied |
| 2181 | * on device initialistion |
| 2182 | * |
| 2183 | * @map: Register map to apply updates to. |
| 2184 | * @regs: Values to update. |
| 2185 | * @num_regs: Number of entries in regs. |
| 2186 | * |
| 2187 | * Register a set of register updates to be applied to the device |
| 2188 | * whenever the device registers are synchronised with the cache and |
| 2189 | * apply them immediately. Typically this is used to apply |
| 2190 | * corrections to be applied to the device defaults on startup, such |
| 2191 | * as the updates some vendors provide to undocumented registers. |
| 2192 | */ |
| 2193 | int regmap_register_patch(struct regmap *map, const struct reg_default *regs, |
| 2194 | int num_regs) |
| 2195 | { |
Mark Brown | aab13eb | 2013-07-11 12:41:44 +0100 | [diff] [blame] | 2196 | struct reg_default *p; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2197 | int i, ret; |
| 2198 | bool bypass; |
| 2199 | |
Cai Zhiyong | bd60e38 | 2013-11-18 20:21:49 +0800 | [diff] [blame] | 2200 | if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n", |
| 2201 | num_regs)) |
| 2202 | return 0; |
| 2203 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2204 | map->lock(map->lock_arg); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2205 | |
| 2206 | bypass = map->cache_bypass; |
| 2207 | |
| 2208 | map->cache_bypass = true; |
Mark Brown | 1a25f26 | 2013-10-10 20:55:03 +0100 | [diff] [blame] | 2209 | map->async = true; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2210 | |
| 2211 | /* Write out first; it's useful to apply even if we fail later. */ |
| 2212 | for (i = 0; i < num_regs; i++) { |
| 2213 | ret = _regmap_write(map, regs[i].reg, regs[i].def); |
| 2214 | if (ret != 0) { |
| 2215 | dev_err(map->dev, "Failed to write %x = %x: %d\n", |
| 2216 | regs[i].reg, regs[i].def, ret); |
| 2217 | goto out; |
| 2218 | } |
| 2219 | } |
| 2220 | |
Mark Brown | aab13eb | 2013-07-11 12:41:44 +0100 | [diff] [blame] | 2221 | p = krealloc(map->patch, |
| 2222 | sizeof(struct reg_default) * (map->patch_regs + num_regs), |
| 2223 | GFP_KERNEL); |
| 2224 | if (p) { |
| 2225 | memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs)); |
| 2226 | map->patch = p; |
| 2227 | map->patch_regs += num_regs; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2228 | } else { |
| 2229 | ret = -ENOMEM; |
| 2230 | } |
| 2231 | |
| 2232 | out: |
Mark Brown | 1a25f26 | 2013-10-10 20:55:03 +0100 | [diff] [blame] | 2233 | map->async = false; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2234 | map->cache_bypass = bypass; |
| 2235 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2236 | map->unlock(map->lock_arg); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2237 | |
Mark Brown | 1a25f26 | 2013-10-10 20:55:03 +0100 | [diff] [blame] | 2238 | regmap_async_complete(map); |
| 2239 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2240 | return ret; |
| 2241 | } |
| 2242 | EXPORT_SYMBOL_GPL(regmap_register_patch); |
| 2243 | |
Mark Brown | eae4b51 | 2012-03-14 13:15:03 +0000 | [diff] [blame] | 2244 | /* |
Mark Brown | a6539c3 | 2012-02-17 14:20:14 -0800 | [diff] [blame] | 2245 | * regmap_get_val_bytes(): Report the size of a register value |
| 2246 | * |
| 2247 | * Report the size of a register value, mainly intended to for use by |
| 2248 | * generic infrastructure built on top of regmap. |
| 2249 | */ |
| 2250 | int regmap_get_val_bytes(struct regmap *map) |
| 2251 | { |
| 2252 | if (map->format.format_write) |
| 2253 | return -EINVAL; |
| 2254 | |
| 2255 | return map->format.val_bytes; |
| 2256 | } |
| 2257 | EXPORT_SYMBOL_GPL(regmap_get_val_bytes); |
| 2258 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 2259 | static int __init regmap_initcall(void) |
| 2260 | { |
| 2261 | regmap_debugfs_initcall(); |
| 2262 | |
| 2263 | return 0; |
| 2264 | } |
| 2265 | postcore_initcall(regmap_initcall); |