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