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> |
Xiubo Li | d647c19 | 2014-07-15 12:23:02 +0800 | [diff] [blame] | 18 | #include <linux/of.h> |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 19 | #include <linux/rbtree.h> |
Stephen Warren | 30b2a55 | 2013-02-02 22:50:14 -0700 | [diff] [blame] | 20 | #include <linux/sched.h> |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 21 | #include <linux/delay.h> |
Xiubo Li | ca747be | 2016-01-04 18:00:33 +0800 | [diff] [blame^] | 22 | #include <linux/log2.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 23 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 24 | #define CREATE_TRACE_POINTS |
Steven Rostedt | f58078d | 2015-03-19 17:50:47 -0400 | [diff] [blame] | 25 | #include "trace.h" |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 26 | |
Mark Brown | 93de912 | 2011-07-20 22:35:37 +0100 | [diff] [blame] | 27 | #include "internal.h" |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 28 | |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 29 | /* |
| 30 | * Sometimes for failures during very early init the trace |
| 31 | * infrastructure isn't available early enough to be used. For this |
| 32 | * sort of problem defining LOG_DEVICE will add printks for basic |
| 33 | * register I/O on a specific device. |
| 34 | */ |
| 35 | #undef LOG_DEVICE |
| 36 | |
| 37 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 38 | unsigned int mask, unsigned int val, |
Kuninori Morimoto | 7ff0589 | 2015-06-16 08:52:22 +0000 | [diff] [blame] | 39 | bool *change, bool force_write); |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 40 | |
Boris BREZILLON | 3ac1703 | 2014-04-17 11:40:11 +0200 | [diff] [blame] | 41 | static int _regmap_bus_reg_read(void *context, unsigned int reg, |
| 42 | unsigned int *val); |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 43 | static int _regmap_bus_read(void *context, unsigned int reg, |
| 44 | unsigned int *val); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 45 | static int _regmap_bus_formatted_write(void *context, unsigned int reg, |
| 46 | unsigned int val); |
Boris BREZILLON | 3ac1703 | 2014-04-17 11:40:11 +0200 | [diff] [blame] | 47 | static int _regmap_bus_reg_write(void *context, unsigned int reg, |
| 48 | unsigned int val); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 49 | static int _regmap_bus_raw_write(void *context, unsigned int reg, |
| 50 | unsigned int val); |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 51 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 52 | bool regmap_reg_in_ranges(unsigned int reg, |
| 53 | const struct regmap_range *ranges, |
| 54 | unsigned int nranges) |
| 55 | { |
| 56 | const struct regmap_range *r; |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0, r = ranges; i < nranges; i++, r++) |
| 60 | if (regmap_reg_in_range(reg, r)) |
| 61 | return true; |
| 62 | return false; |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(regmap_reg_in_ranges); |
| 65 | |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 66 | bool regmap_check_range_table(struct regmap *map, unsigned int reg, |
| 67 | const struct regmap_access_table *table) |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 68 | { |
| 69 | /* Check "no ranges" first */ |
| 70 | if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges)) |
| 71 | return false; |
| 72 | |
| 73 | /* In case zero "yes ranges" are supplied, any reg is OK */ |
| 74 | if (!table->n_yes_ranges) |
| 75 | return true; |
| 76 | |
| 77 | return regmap_reg_in_ranges(reg, table->yes_ranges, |
| 78 | table->n_yes_ranges); |
| 79 | } |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 80 | EXPORT_SYMBOL_GPL(regmap_check_range_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 81 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 82 | bool regmap_writeable(struct regmap *map, unsigned int reg) |
| 83 | { |
| 84 | if (map->max_register && reg > map->max_register) |
| 85 | return false; |
| 86 | |
| 87 | if (map->writeable_reg) |
| 88 | return map->writeable_reg(map->dev, reg); |
| 89 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 90 | if (map->wr_table) |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 91 | return regmap_check_range_table(map, reg, map->wr_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 92 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool regmap_readable(struct regmap *map, unsigned int reg) |
| 97 | { |
Lars-Peter Clausen | 04dc91c | 2015-07-13 12:26:44 +0200 | [diff] [blame] | 98 | if (!map->reg_read) |
| 99 | return false; |
| 100 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 101 | if (map->max_register && reg > map->max_register) |
| 102 | return false; |
| 103 | |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 104 | if (map->format.format_write) |
| 105 | return false; |
| 106 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 107 | if (map->readable_reg) |
| 108 | return map->readable_reg(map->dev, reg); |
| 109 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 110 | if (map->rd_table) |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 111 | return regmap_check_range_table(map, reg, map->rd_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 112 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool regmap_volatile(struct regmap *map, unsigned int reg) |
| 117 | { |
Mark Brown | 5844a8b | 2014-08-26 12:12:17 +0100 | [diff] [blame] | 118 | if (!map->format.format_write && !regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 119 | return false; |
| 120 | |
| 121 | if (map->volatile_reg) |
| 122 | return map->volatile_reg(map->dev, reg); |
| 123 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 124 | if (map->volatile_table) |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 125 | return regmap_check_range_table(map, reg, map->volatile_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 126 | |
Mark Brown | b92be6f | 2013-06-03 17:24:08 +0100 | [diff] [blame] | 127 | if (map->cache_ops) |
| 128 | return false; |
| 129 | else |
| 130 | return true; |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | bool regmap_precious(struct regmap *map, unsigned int reg) |
| 134 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 135 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 136 | return false; |
| 137 | |
| 138 | if (map->precious_reg) |
| 139 | return map->precious_reg(map->dev, reg); |
| 140 | |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 141 | if (map->precious_table) |
Mark Brown | 154881e | 2013-05-08 13:55:23 +0100 | [diff] [blame] | 142 | return regmap_check_range_table(map, reg, map->precious_table); |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 143 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 147 | static bool regmap_volatile_range(struct regmap *map, unsigned int reg, |
Paul Bolle | a8f28cf | 2012-10-08 22:06:30 +0200 | [diff] [blame] | 148 | size_t num) |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 149 | { |
| 150 | unsigned int i; |
| 151 | |
| 152 | for (i = 0; i < num; i++) |
| 153 | if (!regmap_volatile(map, reg + i)) |
| 154 | return false; |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 159 | static void regmap_format_2_6_write(struct regmap *map, |
| 160 | unsigned int reg, unsigned int val) |
| 161 | { |
| 162 | u8 *out = map->work_buf; |
| 163 | |
| 164 | *out = (reg << 6) | val; |
| 165 | } |
| 166 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 167 | static void regmap_format_4_12_write(struct regmap *map, |
| 168 | unsigned int reg, unsigned int val) |
| 169 | { |
| 170 | __be16 *out = map->work_buf; |
| 171 | *out = cpu_to_be16((reg << 12) | val); |
| 172 | } |
| 173 | |
| 174 | static void regmap_format_7_9_write(struct regmap *map, |
| 175 | unsigned int reg, unsigned int val) |
| 176 | { |
| 177 | __be16 *out = map->work_buf; |
| 178 | *out = cpu_to_be16((reg << 9) | val); |
| 179 | } |
| 180 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 181 | static void regmap_format_10_14_write(struct regmap *map, |
| 182 | unsigned int reg, unsigned int val) |
| 183 | { |
| 184 | u8 *out = map->work_buf; |
| 185 | |
| 186 | out[2] = val; |
| 187 | out[1] = (val >> 8) | (reg << 6); |
| 188 | out[0] = reg >> 2; |
| 189 | } |
| 190 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 191 | 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] | 192 | { |
| 193 | u8 *b = buf; |
| 194 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 195 | b[0] = val << shift; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 198 | 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] | 199 | { |
| 200 | __be16 *b = buf; |
| 201 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 202 | b[0] = cpu_to_be16(val << shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Xiubo Li | 4aa8c06 | 2014-04-02 18:09:07 +0800 | [diff] [blame] | 205 | static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift) |
| 206 | { |
| 207 | __le16 *b = buf; |
| 208 | |
| 209 | b[0] = cpu_to_le16(val << shift); |
| 210 | } |
| 211 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 212 | static void regmap_format_16_native(void *buf, unsigned int val, |
| 213 | unsigned int shift) |
| 214 | { |
| 215 | *(u16 *)buf = val << shift; |
| 216 | } |
| 217 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 218 | 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] | 219 | { |
| 220 | u8 *b = buf; |
| 221 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 222 | val <<= shift; |
| 223 | |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 224 | b[0] = val >> 16; |
| 225 | b[1] = val >> 8; |
| 226 | b[2] = val; |
| 227 | } |
| 228 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 229 | 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] | 230 | { |
| 231 | __be32 *b = buf; |
| 232 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 233 | b[0] = cpu_to_be32(val << shift); |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Xiubo Li | 4aa8c06 | 2014-04-02 18:09:07 +0800 | [diff] [blame] | 236 | static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift) |
| 237 | { |
| 238 | __le32 *b = buf; |
| 239 | |
| 240 | b[0] = cpu_to_le32(val << shift); |
| 241 | } |
| 242 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 243 | static void regmap_format_32_native(void *buf, unsigned int val, |
| 244 | unsigned int shift) |
| 245 | { |
| 246 | *(u32 *)buf = val << shift; |
| 247 | } |
| 248 | |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 249 | #ifdef CONFIG_64BIT |
| 250 | static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift) |
| 251 | { |
| 252 | __be64 *b = buf; |
| 253 | |
Dan Carpenter | 01c377b | 2015-12-12 15:59:14 +0300 | [diff] [blame] | 254 | b[0] = cpu_to_be64((u64)val << shift); |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift) |
| 258 | { |
| 259 | __le64 *b = buf; |
| 260 | |
Dan Carpenter | 01c377b | 2015-12-12 15:59:14 +0300 | [diff] [blame] | 261 | b[0] = cpu_to_le64((u64)val << shift); |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static void regmap_format_64_native(void *buf, unsigned int val, |
| 265 | unsigned int shift) |
| 266 | { |
Dan Carpenter | 01c377b | 2015-12-12 15:59:14 +0300 | [diff] [blame] | 267 | *(u64 *)buf = (u64)val << shift; |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 268 | } |
| 269 | #endif |
| 270 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 271 | static void regmap_parse_inplace_noop(void *buf) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 272 | { |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static unsigned int regmap_parse_8(const void *buf) |
| 276 | { |
| 277 | const u8 *b = buf; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 278 | |
| 279 | return b[0]; |
| 280 | } |
| 281 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 282 | static unsigned int regmap_parse_16_be(const void *buf) |
| 283 | { |
| 284 | const __be16 *b = buf; |
| 285 | |
| 286 | return be16_to_cpu(b[0]); |
| 287 | } |
| 288 | |
Xiubo Li | 4aa8c06 | 2014-04-02 18:09:07 +0800 | [diff] [blame] | 289 | static unsigned int regmap_parse_16_le(const void *buf) |
| 290 | { |
| 291 | const __le16 *b = buf; |
| 292 | |
| 293 | return le16_to_cpu(b[0]); |
| 294 | } |
| 295 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 296 | static void regmap_parse_16_be_inplace(void *buf) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 297 | { |
| 298 | __be16 *b = buf; |
| 299 | |
| 300 | b[0] = be16_to_cpu(b[0]); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Xiubo Li | 4aa8c06 | 2014-04-02 18:09:07 +0800 | [diff] [blame] | 303 | static void regmap_parse_16_le_inplace(void *buf) |
| 304 | { |
| 305 | __le16 *b = buf; |
| 306 | |
| 307 | b[0] = le16_to_cpu(b[0]); |
| 308 | } |
| 309 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 310 | static unsigned int regmap_parse_16_native(const void *buf) |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 311 | { |
| 312 | return *(u16 *)buf; |
| 313 | } |
| 314 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 315 | static unsigned int regmap_parse_24(const void *buf) |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 316 | { |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 317 | const u8 *b = buf; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 318 | unsigned int ret = b[2]; |
| 319 | ret |= ((unsigned int)b[1]) << 8; |
| 320 | ret |= ((unsigned int)b[0]) << 16; |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 325 | static unsigned int regmap_parse_32_be(const void *buf) |
| 326 | { |
| 327 | const __be32 *b = buf; |
| 328 | |
| 329 | return be32_to_cpu(b[0]); |
| 330 | } |
| 331 | |
Xiubo Li | 4aa8c06 | 2014-04-02 18:09:07 +0800 | [diff] [blame] | 332 | static unsigned int regmap_parse_32_le(const void *buf) |
| 333 | { |
| 334 | const __le32 *b = buf; |
| 335 | |
| 336 | return le32_to_cpu(b[0]); |
| 337 | } |
| 338 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 339 | static void regmap_parse_32_be_inplace(void *buf) |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 340 | { |
| 341 | __be32 *b = buf; |
| 342 | |
| 343 | b[0] = be32_to_cpu(b[0]); |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Xiubo Li | 4aa8c06 | 2014-04-02 18:09:07 +0800 | [diff] [blame] | 346 | static void regmap_parse_32_le_inplace(void *buf) |
| 347 | { |
| 348 | __le32 *b = buf; |
| 349 | |
| 350 | b[0] = le32_to_cpu(b[0]); |
| 351 | } |
| 352 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 353 | static unsigned int regmap_parse_32_native(const void *buf) |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 354 | { |
| 355 | return *(u32 *)buf; |
| 356 | } |
| 357 | |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 358 | #ifdef CONFIG_64BIT |
| 359 | static unsigned int regmap_parse_64_be(const void *buf) |
| 360 | { |
| 361 | const __be64 *b = buf; |
| 362 | |
| 363 | return be64_to_cpu(b[0]); |
| 364 | } |
| 365 | |
| 366 | static unsigned int regmap_parse_64_le(const void *buf) |
| 367 | { |
| 368 | const __le64 *b = buf; |
| 369 | |
| 370 | return le64_to_cpu(b[0]); |
| 371 | } |
| 372 | |
| 373 | static void regmap_parse_64_be_inplace(void *buf) |
| 374 | { |
| 375 | __be64 *b = buf; |
| 376 | |
| 377 | b[0] = be64_to_cpu(b[0]); |
| 378 | } |
| 379 | |
| 380 | static void regmap_parse_64_le_inplace(void *buf) |
| 381 | { |
| 382 | __le64 *b = buf; |
| 383 | |
| 384 | b[0] = le64_to_cpu(b[0]); |
| 385 | } |
| 386 | |
| 387 | static unsigned int regmap_parse_64_native(const void *buf) |
| 388 | { |
| 389 | return *(u64 *)buf; |
| 390 | } |
| 391 | #endif |
| 392 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 393 | static void regmap_lock_mutex(void *__map) |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 394 | { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 395 | struct regmap *map = __map; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 396 | mutex_lock(&map->mutex); |
| 397 | } |
| 398 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 399 | static void regmap_unlock_mutex(void *__map) |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 400 | { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 401 | struct regmap *map = __map; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 402 | mutex_unlock(&map->mutex); |
| 403 | } |
| 404 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 405 | static void regmap_lock_spinlock(void *__map) |
Fabio Estevam | b4519c7 | 2013-07-16 02:10:11 -0300 | [diff] [blame] | 406 | __acquires(&map->spinlock) |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 407 | { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 408 | struct regmap *map = __map; |
Lars-Peter Clausen | 92ab1aa | 2013-05-24 10:29:22 +0200 | [diff] [blame] | 409 | unsigned long flags; |
| 410 | |
| 411 | spin_lock_irqsave(&map->spinlock, flags); |
| 412 | map->spinlock_flags = flags; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 413 | } |
| 414 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 415 | static void regmap_unlock_spinlock(void *__map) |
Fabio Estevam | b4519c7 | 2013-07-16 02:10:11 -0300 | [diff] [blame] | 416 | __releases(&map->spinlock) |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 417 | { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 418 | struct regmap *map = __map; |
Lars-Peter Clausen | 92ab1aa | 2013-05-24 10:29:22 +0200 | [diff] [blame] | 419 | spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags); |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 420 | } |
| 421 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 422 | static void dev_get_regmap_release(struct device *dev, void *res) |
| 423 | { |
| 424 | /* |
| 425 | * We don't actually have anything to do here; the goal here |
| 426 | * is not to manage the regmap but to provide a simple way to |
| 427 | * get the regmap back given a struct device. |
| 428 | */ |
| 429 | } |
| 430 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 431 | static bool _regmap_range_add(struct regmap *map, |
| 432 | struct regmap_range_node *data) |
| 433 | { |
| 434 | struct rb_root *root = &map->range_tree; |
| 435 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 436 | |
| 437 | while (*new) { |
| 438 | struct regmap_range_node *this = |
| 439 | container_of(*new, struct regmap_range_node, node); |
| 440 | |
| 441 | parent = *new; |
| 442 | if (data->range_max < this->range_min) |
| 443 | new = &((*new)->rb_left); |
| 444 | else if (data->range_min > this->range_max) |
| 445 | new = &((*new)->rb_right); |
| 446 | else |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | rb_link_node(&data->node, parent, new); |
| 451 | rb_insert_color(&data->node, root); |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | static struct regmap_range_node *_regmap_range_lookup(struct regmap *map, |
| 457 | unsigned int reg) |
| 458 | { |
| 459 | struct rb_node *node = map->range_tree.rb_node; |
| 460 | |
| 461 | while (node) { |
| 462 | struct regmap_range_node *this = |
| 463 | container_of(node, struct regmap_range_node, node); |
| 464 | |
| 465 | if (reg < this->range_min) |
| 466 | node = node->rb_left; |
| 467 | else if (reg > this->range_max) |
| 468 | node = node->rb_right; |
| 469 | else |
| 470 | return this; |
| 471 | } |
| 472 | |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | static void regmap_range_exit(struct regmap *map) |
| 477 | { |
| 478 | struct rb_node *next; |
| 479 | struct regmap_range_node *range_node; |
| 480 | |
| 481 | next = rb_first(&map->range_tree); |
| 482 | while (next) { |
| 483 | range_node = rb_entry(next, struct regmap_range_node, node); |
| 484 | next = rb_next(&range_node->node); |
| 485 | rb_erase(&range_node->node, &map->range_tree); |
| 486 | kfree(range_node); |
| 487 | } |
| 488 | |
| 489 | kfree(map->selector_work_buf); |
| 490 | } |
| 491 | |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame] | 492 | int regmap_attach_dev(struct device *dev, struct regmap *map, |
| 493 | const struct regmap_config *config) |
| 494 | { |
| 495 | struct regmap **m; |
| 496 | |
| 497 | map->dev = dev; |
| 498 | |
| 499 | regmap_debugfs_init(map, config->name); |
| 500 | |
| 501 | /* Add a devres resource for dev_get_regmap() */ |
| 502 | m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL); |
| 503 | if (!m) { |
| 504 | regmap_debugfs_exit(map); |
| 505 | return -ENOMEM; |
| 506 | } |
| 507 | *m = map; |
| 508 | devres_add(dev, m); |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | EXPORT_SYMBOL_GPL(regmap_attach_dev); |
| 513 | |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 514 | static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus, |
| 515 | const struct regmap_config *config) |
Xiubo Li | d647c19 | 2014-07-15 12:23:02 +0800 | [diff] [blame] | 516 | { |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 517 | enum regmap_endian endian; |
Xiubo Li | d647c19 | 2014-07-15 12:23:02 +0800 | [diff] [blame] | 518 | |
Stephen Warren | 45e1a27 | 2014-08-19 10:49:07 -0600 | [diff] [blame] | 519 | /* Retrieve the endianness specification from the regmap config */ |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 520 | endian = config->reg_format_endian; |
Xiubo Li | d647c19 | 2014-07-15 12:23:02 +0800 | [diff] [blame] | 521 | |
Stephen Warren | 45e1a27 | 2014-08-19 10:49:07 -0600 | [diff] [blame] | 522 | /* If the regmap config specified a non-default value, use that */ |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 523 | if (endian != REGMAP_ENDIAN_DEFAULT) |
| 524 | return endian; |
Stephen Warren | 45e1a27 | 2014-08-19 10:49:07 -0600 | [diff] [blame] | 525 | |
| 526 | /* Retrieve the endianness specification from the bus config */ |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 527 | if (bus && bus->reg_format_endian_default) |
| 528 | endian = bus->reg_format_endian_default; |
Xiubo Li | d647c19 | 2014-07-15 12:23:02 +0800 | [diff] [blame] | 529 | |
Stephen Warren | 45e1a27 | 2014-08-19 10:49:07 -0600 | [diff] [blame] | 530 | /* If the bus specified a non-default value, use that */ |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 531 | if (endian != REGMAP_ENDIAN_DEFAULT) |
| 532 | return endian; |
Stephen Warren | 45e1a27 | 2014-08-19 10:49:07 -0600 | [diff] [blame] | 533 | |
| 534 | /* Use this if no other value was found */ |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 535 | return REGMAP_ENDIAN_BIG; |
| 536 | } |
Stephen Warren | 45e1a27 | 2014-08-19 10:49:07 -0600 | [diff] [blame] | 537 | |
Guenter Roeck | 3c174d2 | 2015-02-03 10:01:18 -0800 | [diff] [blame] | 538 | enum regmap_endian regmap_get_val_endian(struct device *dev, |
| 539 | const struct regmap_bus *bus, |
| 540 | const struct regmap_config *config) |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 541 | { |
Pankaj Dubey | 6e64b6c | 2014-09-18 15:12:20 +0530 | [diff] [blame] | 542 | struct device_node *np; |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 543 | enum regmap_endian endian; |
| 544 | |
| 545 | /* Retrieve the endianness specification from the regmap config */ |
| 546 | endian = config->val_format_endian; |
| 547 | |
| 548 | /* If the regmap config specified a non-default value, use that */ |
| 549 | if (endian != REGMAP_ENDIAN_DEFAULT) |
| 550 | return endian; |
| 551 | |
Pankaj Dubey | 6e64b6c | 2014-09-18 15:12:20 +0530 | [diff] [blame] | 552 | /* If the dev and dev->of_node exist try to get endianness from DT */ |
| 553 | if (dev && dev->of_node) { |
| 554 | np = dev->of_node; |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 555 | |
Pankaj Dubey | 6e64b6c | 2014-09-18 15:12:20 +0530 | [diff] [blame] | 556 | /* Parse the device's DT node for an endianness specification */ |
| 557 | if (of_property_read_bool(np, "big-endian")) |
| 558 | endian = REGMAP_ENDIAN_BIG; |
| 559 | else if (of_property_read_bool(np, "little-endian")) |
| 560 | endian = REGMAP_ENDIAN_LITTLE; |
| 561 | |
| 562 | /* If the endianness was specified in DT, use that */ |
| 563 | if (endian != REGMAP_ENDIAN_DEFAULT) |
| 564 | return endian; |
| 565 | } |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 566 | |
| 567 | /* Retrieve the endianness specification from the bus config */ |
| 568 | if (bus && bus->val_format_endian_default) |
| 569 | endian = bus->val_format_endian_default; |
| 570 | |
| 571 | /* If the bus specified a non-default value, use that */ |
| 572 | if (endian != REGMAP_ENDIAN_DEFAULT) |
| 573 | return endian; |
| 574 | |
| 575 | /* Use this if no other value was found */ |
| 576 | return REGMAP_ENDIAN_BIG; |
Xiubo Li | d647c19 | 2014-07-15 12:23:02 +0800 | [diff] [blame] | 577 | } |
Guenter Roeck | 3c174d2 | 2015-02-03 10:01:18 -0800 | [diff] [blame] | 578 | EXPORT_SYMBOL_GPL(regmap_get_val_endian); |
Xiubo Li | d647c19 | 2014-07-15 12:23:02 +0800 | [diff] [blame] | 579 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 580 | struct regmap *__regmap_init(struct device *dev, |
| 581 | const struct regmap_bus *bus, |
| 582 | void *bus_context, |
| 583 | const struct regmap_config *config, |
| 584 | struct lock_class_key *lock_key, |
| 585 | const char *lock_name) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 586 | { |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame] | 587 | struct regmap *map; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 588 | int ret = -EINVAL; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 589 | enum regmap_endian reg_endian, val_endian; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 590 | int i, j; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 591 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 592 | if (!config) |
Lars-Peter Clausen | abbb18f | 2011-11-14 10:40:15 +0100 | [diff] [blame] | 593 | goto err; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 594 | |
| 595 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 596 | if (map == NULL) { |
| 597 | ret = -ENOMEM; |
| 598 | goto err; |
| 599 | } |
| 600 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 601 | if (config->lock && config->unlock) { |
| 602 | map->lock = config->lock; |
| 603 | map->unlock = config->unlock; |
| 604 | map->lock_arg = config->lock_arg; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 605 | } else { |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 606 | if ((bus && bus->fast_io) || |
| 607 | config->fast_io) { |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 608 | spin_lock_init(&map->spinlock); |
| 609 | map->lock = regmap_lock_spinlock; |
| 610 | map->unlock = regmap_unlock_spinlock; |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 611 | lockdep_set_class_and_name(&map->spinlock, |
| 612 | lock_key, lock_name); |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 613 | } else { |
| 614 | mutex_init(&map->mutex); |
| 615 | map->lock = regmap_lock_mutex; |
| 616 | map->unlock = regmap_unlock_mutex; |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 617 | lockdep_set_class_and_name(&map->mutex, |
| 618 | lock_key, lock_name); |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 619 | } |
| 620 | map->lock_arg = map; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 621 | } |
Stephen Boyd | b4a21fc | 2015-09-11 16:37:05 -0700 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | * When we write in fast-paths with regmap_bulk_write() don't allocate |
| 625 | * scratch buffers with sleeping allocations. |
| 626 | */ |
| 627 | if ((bus && bus->fast_io) || config->fast_io) |
| 628 | map->alloc_flags = GFP_ATOMIC; |
| 629 | else |
| 630 | map->alloc_flags = GFP_KERNEL; |
| 631 | |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 632 | map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 633 | map->format.pad_bytes = config->pad_bits / 8; |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 634 | map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); |
Fabio Estevam | 5494a98 | 2012-05-31 21:10:30 -0300 | [diff] [blame] | 635 | map->format.buf_size = DIV_ROUND_UP(config->reg_bits + |
| 636 | config->val_bits + config->pad_bits, 8); |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 637 | map->reg_shift = config->pad_bits % 8; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 638 | if (config->reg_stride) |
| 639 | map->reg_stride = config->reg_stride; |
| 640 | else |
| 641 | map->reg_stride = 1; |
Xiubo Li | ca747be | 2016-01-04 18:00:33 +0800 | [diff] [blame^] | 642 | if (is_power_of_2(map->reg_stride)) |
| 643 | map->reg_stride_order = ilog2(map->reg_stride); |
| 644 | else |
| 645 | map->reg_stride_order = -1; |
Markus Pargmann | 67921a1 | 2015-08-21 10:26:42 +0200 | [diff] [blame] | 646 | map->use_single_read = config->use_single_rw || !bus || !bus->read; |
| 647 | map->use_single_write = config->use_single_rw || !bus || !bus->write; |
Markus Pargmann | 9c9f7f6 | 2015-08-21 10:26:43 +0200 | [diff] [blame] | 648 | map->can_multi_write = config->can_multi_write && bus && bus->write; |
Sergey SENOZHATSKY | 17649c9 | 2015-08-31 18:54:58 +0900 | [diff] [blame] | 649 | if (bus) { |
| 650 | map->max_raw_read = bus->max_raw_read; |
| 651 | map->max_raw_write = bus->max_raw_write; |
| 652 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 653 | map->dev = dev; |
| 654 | map->bus = bus; |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 655 | map->bus_context = bus_context; |
Mark Brown | 2e2ae66 | 2011-07-20 22:33:39 +0100 | [diff] [blame] | 656 | map->max_register = config->max_register; |
Davide Ciminaghi | 76aad39 | 2012-11-20 15:20:30 +0100 | [diff] [blame] | 657 | map->wr_table = config->wr_table; |
| 658 | map->rd_table = config->rd_table; |
| 659 | map->volatile_table = config->volatile_table; |
| 660 | map->precious_table = config->precious_table; |
Mark Brown | 2e2ae66 | 2011-07-20 22:33:39 +0100 | [diff] [blame] | 661 | map->writeable_reg = config->writeable_reg; |
| 662 | map->readable_reg = config->readable_reg; |
| 663 | map->volatile_reg = config->volatile_reg; |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 664 | map->precious_reg = config->precious_reg; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 665 | map->cache_type = config->cache_type; |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 666 | map->name = config->name; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 667 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 668 | spin_lock_init(&map->async_lock); |
| 669 | INIT_LIST_HEAD(&map->async_list); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 670 | INIT_LIST_HEAD(&map->async_free); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 671 | init_waitqueue_head(&map->async_waitq); |
| 672 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 673 | if (config->read_flag_mask || config->write_flag_mask) { |
| 674 | map->read_flag_mask = config->read_flag_mask; |
| 675 | map->write_flag_mask = config->write_flag_mask; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 676 | } else if (bus) { |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 677 | map->read_flag_mask = bus->read_flag_mask; |
| 678 | } |
| 679 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 680 | if (!bus) { |
| 681 | map->reg_read = config->reg_read; |
| 682 | map->reg_write = config->reg_write; |
| 683 | |
| 684 | map->defer_caching = false; |
| 685 | goto skip_format_initialization; |
Boris BREZILLON | 3ac1703 | 2014-04-17 11:40:11 +0200 | [diff] [blame] | 686 | } else if (!bus->read || !bus->write) { |
| 687 | map->reg_read = _regmap_bus_reg_read; |
| 688 | map->reg_write = _regmap_bus_reg_write; |
| 689 | |
| 690 | map->defer_caching = false; |
| 691 | goto skip_format_initialization; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 692 | } else { |
| 693 | map->reg_read = _regmap_bus_read; |
Jon Ringle | 77792b1 | 2015-10-01 12:38:07 -0400 | [diff] [blame] | 694 | map->reg_update_bits = bus->reg_update_bits; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 695 | } |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 696 | |
Geert Uytterhoeven | cf673fb | 2014-08-27 16:36:03 +0200 | [diff] [blame] | 697 | reg_endian = regmap_get_reg_endian(bus, config); |
| 698 | val_endian = regmap_get_val_endian(dev, bus, config); |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 699 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 700 | switch (config->reg_bits + map->reg_shift) { |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 701 | case 2: |
| 702 | switch (config->val_bits) { |
| 703 | case 6: |
| 704 | map->format.format_write = regmap_format_2_6_write; |
| 705 | break; |
| 706 | default: |
| 707 | goto err_map; |
| 708 | } |
| 709 | break; |
| 710 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 711 | case 4: |
| 712 | switch (config->val_bits) { |
| 713 | case 12: |
| 714 | map->format.format_write = regmap_format_4_12_write; |
| 715 | break; |
| 716 | default: |
| 717 | goto err_map; |
| 718 | } |
| 719 | break; |
| 720 | |
| 721 | case 7: |
| 722 | switch (config->val_bits) { |
| 723 | case 9: |
| 724 | map->format.format_write = regmap_format_7_9_write; |
| 725 | break; |
| 726 | default: |
| 727 | goto err_map; |
| 728 | } |
| 729 | break; |
| 730 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 731 | case 10: |
| 732 | switch (config->val_bits) { |
| 733 | case 14: |
| 734 | map->format.format_write = regmap_format_10_14_write; |
| 735 | break; |
| 736 | default: |
| 737 | goto err_map; |
| 738 | } |
| 739 | break; |
| 740 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 741 | case 8: |
| 742 | map->format.format_reg = regmap_format_8; |
| 743 | break; |
| 744 | |
| 745 | case 16: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 746 | switch (reg_endian) { |
| 747 | case REGMAP_ENDIAN_BIG: |
| 748 | map->format.format_reg = regmap_format_16_be; |
| 749 | break; |
| 750 | case REGMAP_ENDIAN_NATIVE: |
| 751 | map->format.format_reg = regmap_format_16_native; |
| 752 | break; |
| 753 | default: |
| 754 | goto err_map; |
| 755 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 756 | break; |
| 757 | |
Lars-Peter Clausen | 237019e | 2013-01-10 17:06:10 +0100 | [diff] [blame] | 758 | case 24: |
| 759 | if (reg_endian != REGMAP_ENDIAN_BIG) |
| 760 | goto err_map; |
| 761 | map->format.format_reg = regmap_format_24; |
| 762 | break; |
| 763 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 764 | case 32: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 765 | switch (reg_endian) { |
| 766 | case REGMAP_ENDIAN_BIG: |
| 767 | map->format.format_reg = regmap_format_32_be; |
| 768 | break; |
| 769 | case REGMAP_ENDIAN_NATIVE: |
| 770 | map->format.format_reg = regmap_format_32_native; |
| 771 | break; |
| 772 | default: |
| 773 | goto err_map; |
| 774 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 775 | break; |
| 776 | |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 777 | #ifdef CONFIG_64BIT |
| 778 | case 64: |
| 779 | switch (reg_endian) { |
| 780 | case REGMAP_ENDIAN_BIG: |
| 781 | map->format.format_reg = regmap_format_64_be; |
| 782 | break; |
| 783 | case REGMAP_ENDIAN_NATIVE: |
| 784 | map->format.format_reg = regmap_format_64_native; |
| 785 | break; |
| 786 | default: |
| 787 | goto err_map; |
| 788 | } |
| 789 | break; |
| 790 | #endif |
| 791 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 792 | default: |
| 793 | goto err_map; |
| 794 | } |
| 795 | |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 796 | if (val_endian == REGMAP_ENDIAN_NATIVE) |
| 797 | map->format.parse_inplace = regmap_parse_inplace_noop; |
| 798 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 799 | switch (config->val_bits) { |
| 800 | case 8: |
| 801 | map->format.format_val = regmap_format_8; |
| 802 | map->format.parse_val = regmap_parse_8; |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 803 | map->format.parse_inplace = regmap_parse_inplace_noop; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 804 | break; |
| 805 | case 16: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 806 | switch (val_endian) { |
| 807 | case REGMAP_ENDIAN_BIG: |
| 808 | map->format.format_val = regmap_format_16_be; |
| 809 | map->format.parse_val = regmap_parse_16_be; |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 810 | map->format.parse_inplace = regmap_parse_16_be_inplace; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 811 | break; |
Xiubo Li | 4aa8c06 | 2014-04-02 18:09:07 +0800 | [diff] [blame] | 812 | case REGMAP_ENDIAN_LITTLE: |
| 813 | map->format.format_val = regmap_format_16_le; |
| 814 | map->format.parse_val = regmap_parse_16_le; |
| 815 | map->format.parse_inplace = regmap_parse_16_le_inplace; |
| 816 | break; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 817 | case REGMAP_ENDIAN_NATIVE: |
| 818 | map->format.format_val = regmap_format_16_native; |
| 819 | map->format.parse_val = regmap_parse_16_native; |
| 820 | break; |
| 821 | default: |
| 822 | goto err_map; |
| 823 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 824 | break; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 825 | case 24: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 826 | if (val_endian != REGMAP_ENDIAN_BIG) |
| 827 | goto err_map; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 828 | map->format.format_val = regmap_format_24; |
| 829 | map->format.parse_val = regmap_parse_24; |
| 830 | break; |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 831 | case 32: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 832 | switch (val_endian) { |
| 833 | case REGMAP_ENDIAN_BIG: |
| 834 | map->format.format_val = regmap_format_32_be; |
| 835 | map->format.parse_val = regmap_parse_32_be; |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 836 | map->format.parse_inplace = regmap_parse_32_be_inplace; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 837 | break; |
Xiubo Li | 4aa8c06 | 2014-04-02 18:09:07 +0800 | [diff] [blame] | 838 | case REGMAP_ENDIAN_LITTLE: |
| 839 | map->format.format_val = regmap_format_32_le; |
| 840 | map->format.parse_val = regmap_parse_32_le; |
| 841 | map->format.parse_inplace = regmap_parse_32_le_inplace; |
| 842 | break; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 843 | case REGMAP_ENDIAN_NATIVE: |
| 844 | map->format.format_val = regmap_format_32_native; |
| 845 | map->format.parse_val = regmap_parse_32_native; |
| 846 | break; |
| 847 | default: |
| 848 | goto err_map; |
| 849 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 850 | break; |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 851 | #ifdef CONFIG_64BIT |
Dan Carpenter | 782035e | 2015-12-12 15:59:43 +0300 | [diff] [blame] | 852 | case 64: |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 853 | switch (val_endian) { |
| 854 | case REGMAP_ENDIAN_BIG: |
| 855 | map->format.format_val = regmap_format_64_be; |
| 856 | map->format.parse_val = regmap_parse_64_be; |
| 857 | map->format.parse_inplace = regmap_parse_64_be_inplace; |
| 858 | break; |
| 859 | case REGMAP_ENDIAN_LITTLE: |
| 860 | map->format.format_val = regmap_format_64_le; |
| 861 | map->format.parse_val = regmap_parse_64_le; |
| 862 | map->format.parse_inplace = regmap_parse_64_le_inplace; |
| 863 | break; |
| 864 | case REGMAP_ENDIAN_NATIVE: |
| 865 | map->format.format_val = regmap_format_64_native; |
| 866 | map->format.parse_val = regmap_parse_64_native; |
| 867 | break; |
| 868 | default: |
| 869 | goto err_map; |
| 870 | } |
| 871 | break; |
| 872 | #endif |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 873 | } |
| 874 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 875 | if (map->format.format_write) { |
| 876 | if ((reg_endian != REGMAP_ENDIAN_BIG) || |
| 877 | (val_endian != REGMAP_ENDIAN_BIG)) |
| 878 | goto err_map; |
Markus Pargmann | 67921a1 | 2015-08-21 10:26:42 +0200 | [diff] [blame] | 879 | map->use_single_write = true; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 880 | } |
Mark Brown | 7a64761 | 2012-04-30 23:26:32 +0100 | [diff] [blame] | 881 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 882 | if (!map->format.format_write && |
| 883 | !(map->format.format_reg && map->format.format_val)) |
| 884 | goto err_map; |
| 885 | |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 886 | map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 887 | if (map->work_buf == NULL) { |
| 888 | ret = -ENOMEM; |
Mark Brown | 5204f5e | 2011-09-05 08:07:47 -0700 | [diff] [blame] | 889 | goto err_map; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 892 | if (map->format.format_write) { |
| 893 | map->defer_caching = false; |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 894 | map->reg_write = _regmap_bus_formatted_write; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 895 | } else if (map->format.format_val) { |
| 896 | map->defer_caching = true; |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 897 | map->reg_write = _regmap_bus_raw_write; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | skip_format_initialization: |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 901 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 902 | map->range_tree = RB_ROOT; |
Mark Brown | e3549cd | 2012-10-02 20:17:15 +0100 | [diff] [blame] | 903 | for (i = 0; i < config->num_ranges; i++) { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 904 | const struct regmap_range_cfg *range_cfg = &config->ranges[i]; |
| 905 | struct regmap_range_node *new; |
| 906 | |
| 907 | /* Sanity check */ |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 908 | if (range_cfg->range_max < range_cfg->range_min) { |
| 909 | dev_err(map->dev, "Invalid range %d: %d < %d\n", i, |
| 910 | range_cfg->range_max, range_cfg->range_min); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 911 | goto err_range; |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | if (range_cfg->range_max > map->max_register) { |
| 915 | dev_err(map->dev, "Invalid range %d: %d > %d\n", i, |
| 916 | range_cfg->range_max, map->max_register); |
| 917 | goto err_range; |
| 918 | } |
| 919 | |
| 920 | if (range_cfg->selector_reg > map->max_register) { |
| 921 | dev_err(map->dev, |
| 922 | "Invalid range %d: selector out of map\n", i); |
| 923 | goto err_range; |
| 924 | } |
| 925 | |
| 926 | if (range_cfg->window_len == 0) { |
| 927 | dev_err(map->dev, "Invalid range %d: window_len 0\n", |
| 928 | i); |
| 929 | goto err_range; |
| 930 | } |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 931 | |
| 932 | /* Make sure, that this register range has no selector |
| 933 | or data window within its boundary */ |
Mark Brown | e3549cd | 2012-10-02 20:17:15 +0100 | [diff] [blame] | 934 | for (j = 0; j < config->num_ranges; j++) { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 935 | unsigned sel_reg = config->ranges[j].selector_reg; |
| 936 | unsigned win_min = config->ranges[j].window_start; |
| 937 | unsigned win_max = win_min + |
| 938 | config->ranges[j].window_len - 1; |
| 939 | |
Philipp Zabel | f161d22 | 2013-07-23 12:16:02 +0200 | [diff] [blame] | 940 | /* Allow data window inside its own virtual range */ |
| 941 | if (j == i) |
| 942 | continue; |
| 943 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 944 | if (range_cfg->range_min <= sel_reg && |
| 945 | sel_reg <= range_cfg->range_max) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 946 | dev_err(map->dev, |
| 947 | "Range %d: selector for %d in window\n", |
| 948 | i, j); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 949 | goto err_range; |
| 950 | } |
| 951 | |
| 952 | if (!(win_max < range_cfg->range_min || |
| 953 | win_min > range_cfg->range_max)) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 954 | dev_err(map->dev, |
| 955 | "Range %d: window for %d in window\n", |
| 956 | i, j); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 957 | goto err_range; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 962 | if (new == NULL) { |
| 963 | ret = -ENOMEM; |
| 964 | goto err_range; |
| 965 | } |
| 966 | |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 967 | new->map = map; |
Mark Brown | d058bb4 | 2012-10-03 12:40:47 +0100 | [diff] [blame] | 968 | new->name = range_cfg->name; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 969 | new->range_min = range_cfg->range_min; |
| 970 | new->range_max = range_cfg->range_max; |
| 971 | new->selector_reg = range_cfg->selector_reg; |
| 972 | new->selector_mask = range_cfg->selector_mask; |
| 973 | new->selector_shift = range_cfg->selector_shift; |
| 974 | new->window_start = range_cfg->window_start; |
| 975 | new->window_len = range_cfg->window_len; |
| 976 | |
Nenghua Cao | 53e87f8 | 2014-02-21 16:05:45 +0800 | [diff] [blame] | 977 | if (!_regmap_range_add(map, new)) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame] | 978 | dev_err(map->dev, "Failed to add range %d\n", i); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 979 | kfree(new); |
| 980 | goto err_range; |
| 981 | } |
| 982 | |
| 983 | if (map->selector_work_buf == NULL) { |
| 984 | map->selector_work_buf = |
| 985 | kzalloc(map->format.buf_size, GFP_KERNEL); |
| 986 | if (map->selector_work_buf == NULL) { |
| 987 | ret = -ENOMEM; |
| 988 | goto err_range; |
| 989 | } |
| 990 | } |
| 991 | } |
Mark Brown | 052d2cd | 2011-11-21 19:05:13 +0000 | [diff] [blame] | 992 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 993 | ret = regcache_init(map, config); |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 994 | if (ret != 0) |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 995 | goto err_range; |
| 996 | |
Daeseok Youn | a7a037c | 2014-04-01 19:46:43 +0900 | [diff] [blame] | 997 | if (dev) { |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame] | 998 | ret = regmap_attach_dev(dev, map, config); |
| 999 | if (ret != 0) |
| 1000 | goto err_regcache; |
Daeseok Youn | a7a037c | 2014-04-01 19:46:43 +0900 | [diff] [blame] | 1001 | } |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 1002 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1003 | return map; |
| 1004 | |
Michal Simek | 6cfec04 | 2014-02-10 16:22:33 +0100 | [diff] [blame] | 1005 | err_regcache: |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 1006 | regcache_exit(map); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1007 | err_range: |
| 1008 | regmap_range_exit(map); |
Lars-Peter Clausen | 58072cb | 2011-11-10 18:15:15 +0100 | [diff] [blame] | 1009 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1010 | err_map: |
| 1011 | kfree(map); |
| 1012 | err: |
| 1013 | return ERR_PTR(ret); |
| 1014 | } |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 1015 | EXPORT_SYMBOL_GPL(__regmap_init); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1016 | |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 1017 | static void devm_regmap_release(struct device *dev, void *res) |
| 1018 | { |
| 1019 | regmap_exit(*(struct regmap **)res); |
| 1020 | } |
| 1021 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 1022 | struct regmap *__devm_regmap_init(struct device *dev, |
| 1023 | const struct regmap_bus *bus, |
| 1024 | void *bus_context, |
| 1025 | const struct regmap_config *config, |
| 1026 | struct lock_class_key *lock_key, |
| 1027 | const char *lock_name) |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 1028 | { |
| 1029 | struct regmap **ptr, *regmap; |
| 1030 | |
| 1031 | ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); |
| 1032 | if (!ptr) |
| 1033 | return ERR_PTR(-ENOMEM); |
| 1034 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 1035 | regmap = __regmap_init(dev, bus, bus_context, config, |
| 1036 | lock_key, lock_name); |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 1037 | if (!IS_ERR(regmap)) { |
| 1038 | *ptr = regmap; |
| 1039 | devres_add(dev, ptr); |
| 1040 | } else { |
| 1041 | devres_free(ptr); |
| 1042 | } |
| 1043 | |
| 1044 | return regmap; |
| 1045 | } |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 1046 | EXPORT_SYMBOL_GPL(__devm_regmap_init); |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 1047 | |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 1048 | static void regmap_field_init(struct regmap_field *rm_field, |
| 1049 | struct regmap *regmap, struct reg_field reg_field) |
| 1050 | { |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 1051 | rm_field->regmap = regmap; |
| 1052 | rm_field->reg = reg_field.reg; |
| 1053 | rm_field->shift = reg_field.lsb; |
Maxime Coquelin | 921cc29 | 2015-06-16 13:53:19 +0200 | [diff] [blame] | 1054 | rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb); |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 1055 | rm_field->id_size = reg_field.id_size; |
| 1056 | rm_field->id_offset = reg_field.id_offset; |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * devm_regmap_field_alloc(): Allocate and initialise a register field |
| 1061 | * in a register map. |
| 1062 | * |
| 1063 | * @dev: Device that will be interacted with |
| 1064 | * @regmap: regmap bank in which this register field is located. |
| 1065 | * @reg_field: Register field with in the bank. |
| 1066 | * |
| 1067 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 1068 | * to a struct regmap_field. The regmap_field will be automatically freed |
| 1069 | * by the device management code. |
| 1070 | */ |
| 1071 | struct regmap_field *devm_regmap_field_alloc(struct device *dev, |
| 1072 | struct regmap *regmap, struct reg_field reg_field) |
| 1073 | { |
| 1074 | struct regmap_field *rm_field = devm_kzalloc(dev, |
| 1075 | sizeof(*rm_field), GFP_KERNEL); |
| 1076 | if (!rm_field) |
| 1077 | return ERR_PTR(-ENOMEM); |
| 1078 | |
| 1079 | regmap_field_init(rm_field, regmap, reg_field); |
| 1080 | |
| 1081 | return rm_field; |
| 1082 | |
| 1083 | } |
| 1084 | EXPORT_SYMBOL_GPL(devm_regmap_field_alloc); |
| 1085 | |
| 1086 | /** |
| 1087 | * devm_regmap_field_free(): Free register field allocated using |
| 1088 | * devm_regmap_field_alloc. Usally drivers need not call this function, |
| 1089 | * as the memory allocated via devm will be freed as per device-driver |
| 1090 | * life-cyle. |
| 1091 | * |
| 1092 | * @dev: Device that will be interacted with |
| 1093 | * @field: regmap field which should be freed. |
| 1094 | */ |
| 1095 | void devm_regmap_field_free(struct device *dev, |
| 1096 | struct regmap_field *field) |
| 1097 | { |
| 1098 | devm_kfree(dev, field); |
| 1099 | } |
| 1100 | EXPORT_SYMBOL_GPL(devm_regmap_field_free); |
| 1101 | |
| 1102 | /** |
| 1103 | * regmap_field_alloc(): Allocate and initialise a register field |
| 1104 | * in a register map. |
| 1105 | * |
| 1106 | * @regmap: regmap bank in which this register field is located. |
| 1107 | * @reg_field: Register field with in the bank. |
| 1108 | * |
| 1109 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 1110 | * to a struct regmap_field. The regmap_field should be freed by the |
| 1111 | * user once its finished working with it using regmap_field_free(). |
| 1112 | */ |
| 1113 | struct regmap_field *regmap_field_alloc(struct regmap *regmap, |
| 1114 | struct reg_field reg_field) |
| 1115 | { |
| 1116 | struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL); |
| 1117 | |
| 1118 | if (!rm_field) |
| 1119 | return ERR_PTR(-ENOMEM); |
| 1120 | |
| 1121 | regmap_field_init(rm_field, regmap, reg_field); |
| 1122 | |
| 1123 | return rm_field; |
| 1124 | } |
| 1125 | EXPORT_SYMBOL_GPL(regmap_field_alloc); |
| 1126 | |
| 1127 | /** |
| 1128 | * regmap_field_free(): Free register field allocated using regmap_field_alloc |
| 1129 | * |
| 1130 | * @field: regmap field which should be freed. |
| 1131 | */ |
| 1132 | void regmap_field_free(struct regmap_field *field) |
| 1133 | { |
| 1134 | kfree(field); |
| 1135 | } |
| 1136 | EXPORT_SYMBOL_GPL(regmap_field_free); |
| 1137 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1138 | /** |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 1139 | * regmap_reinit_cache(): Reinitialise the current register cache |
| 1140 | * |
| 1141 | * @map: Register map to operate on. |
| 1142 | * @config: New configuration. Only the cache data will be used. |
| 1143 | * |
| 1144 | * Discard any existing register cache for the map and initialize a |
| 1145 | * new cache. This can be used to restore the cache to defaults or to |
| 1146 | * update the cache configuration to reflect runtime discovery of the |
| 1147 | * hardware. |
Dimitris Papastamos | 4d87951 | 2012-07-27 14:54:15 +0100 | [diff] [blame] | 1148 | * |
| 1149 | * No explicit locking is done here, the user needs to ensure that |
| 1150 | * this function will not race with other calls to regmap. |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 1151 | */ |
| 1152 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) |
| 1153 | { |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 1154 | regcache_exit(map); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 1155 | regmap_debugfs_exit(map); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 1156 | |
| 1157 | map->max_register = config->max_register; |
| 1158 | map->writeable_reg = config->writeable_reg; |
| 1159 | map->readable_reg = config->readable_reg; |
| 1160 | map->volatile_reg = config->volatile_reg; |
| 1161 | map->precious_reg = config->precious_reg; |
| 1162 | map->cache_type = config->cache_type; |
| 1163 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 1164 | regmap_debugfs_init(map, config->name); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 1165 | |
Mark Brown | 421e8d2 | 2012-01-20 13:39:37 +0000 | [diff] [blame] | 1166 | map->cache_bypass = false; |
| 1167 | map->cache_only = false; |
| 1168 | |
Dimitris Papastamos | 4d87951 | 2012-07-27 14:54:15 +0100 | [diff] [blame] | 1169 | return regcache_init(map, config); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 1170 | } |
Mark Brown | 752a6a5 | 2012-05-14 10:00:12 +0100 | [diff] [blame] | 1171 | EXPORT_SYMBOL_GPL(regmap_reinit_cache); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 1172 | |
| 1173 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1174 | * regmap_exit(): Free a previously allocated register map |
| 1175 | */ |
| 1176 | void regmap_exit(struct regmap *map) |
| 1177 | { |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1178 | struct regmap_async *async; |
| 1179 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1180 | regcache_exit(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 1181 | regmap_debugfs_exit(map); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1182 | regmap_range_exit(map); |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1183 | if (map->bus && map->bus->free_context) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1184 | map->bus->free_context(map->bus_context); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1185 | kfree(map->work_buf); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1186 | while (!list_empty(&map->async_free)) { |
| 1187 | async = list_first_entry_or_null(&map->async_free, |
| 1188 | struct regmap_async, |
| 1189 | list); |
| 1190 | list_del(&async->list); |
| 1191 | kfree(async->work_buf); |
| 1192 | kfree(async); |
| 1193 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1194 | kfree(map); |
| 1195 | } |
| 1196 | EXPORT_SYMBOL_GPL(regmap_exit); |
| 1197 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 1198 | static int dev_get_regmap_match(struct device *dev, void *res, void *data) |
| 1199 | { |
| 1200 | struct regmap **r = res; |
| 1201 | if (!r || !*r) { |
| 1202 | WARN_ON(!r || !*r); |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | /* If the user didn't specify a name match any */ |
| 1207 | if (data) |
| 1208 | return (*r)->name == data; |
| 1209 | else |
| 1210 | return 1; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * dev_get_regmap(): Obtain the regmap (if any) for a device |
| 1215 | * |
| 1216 | * @dev: Device to retrieve the map for |
| 1217 | * @name: Optional name for the register map, usually NULL. |
| 1218 | * |
| 1219 | * Returns the regmap for the device if one is present, or NULL. If |
| 1220 | * name is specified then it must match the name specified when |
| 1221 | * registering the device, if it is NULL then the first regmap found |
| 1222 | * will be used. Devices with multiple register maps are very rare, |
| 1223 | * generic code should normally not need to specify a name. |
| 1224 | */ |
| 1225 | struct regmap *dev_get_regmap(struct device *dev, const char *name) |
| 1226 | { |
| 1227 | struct regmap **r = devres_find(dev, dev_get_regmap_release, |
| 1228 | dev_get_regmap_match, (void *)name); |
| 1229 | |
| 1230 | if (!r) |
| 1231 | return NULL; |
| 1232 | return *r; |
| 1233 | } |
| 1234 | EXPORT_SYMBOL_GPL(dev_get_regmap); |
| 1235 | |
Tuomas Tynkkynen | 8d7d397 | 2014-07-21 18:38:47 +0300 | [diff] [blame] | 1236 | /** |
| 1237 | * regmap_get_device(): Obtain the device from a regmap |
| 1238 | * |
| 1239 | * @map: Register map to operate on. |
| 1240 | * |
| 1241 | * Returns the underlying device that the regmap has been created for. |
| 1242 | */ |
| 1243 | struct device *regmap_get_device(struct regmap *map) |
| 1244 | { |
| 1245 | return map->dev; |
| 1246 | } |
Mark Brown | fa2fbe4 | 2014-07-25 18:30:31 +0100 | [diff] [blame] | 1247 | EXPORT_SYMBOL_GPL(regmap_get_device); |
Tuomas Tynkkynen | 8d7d397 | 2014-07-21 18:38:47 +0300 | [diff] [blame] | 1248 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1249 | static int _regmap_select_page(struct regmap *map, unsigned int *reg, |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1250 | struct regmap_range_node *range, |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1251 | unsigned int val_num) |
| 1252 | { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1253 | void *orig_work_buf; |
| 1254 | unsigned int win_offset; |
| 1255 | unsigned int win_page; |
| 1256 | bool page_chg; |
| 1257 | int ret; |
| 1258 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1259 | win_offset = (*reg - range->range_min) % range->window_len; |
| 1260 | win_page = (*reg - range->range_min) / range->window_len; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1261 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1262 | if (val_num > 1) { |
| 1263 | /* Bulk write shouldn't cross range boundary */ |
| 1264 | if (*reg + val_num - 1 > range->range_max) |
| 1265 | return -EINVAL; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1266 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1267 | /* ... or single page boundary */ |
| 1268 | if (val_num > range->window_len - win_offset) |
| 1269 | return -EINVAL; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1270 | } |
| 1271 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1272 | /* It is possible to have selector register inside data window. |
| 1273 | In that case, selector register is located on every page and |
| 1274 | it needs no page switching, when accessed alone. */ |
| 1275 | if (val_num > 1 || |
| 1276 | range->window_start + win_offset != range->selector_reg) { |
| 1277 | /* Use separate work_buf during page switching */ |
| 1278 | orig_work_buf = map->work_buf; |
| 1279 | map->work_buf = map->selector_work_buf; |
| 1280 | |
| 1281 | ret = _regmap_update_bits(map, range->selector_reg, |
| 1282 | range->selector_mask, |
| 1283 | win_page << range->selector_shift, |
Kuninori Morimoto | 7ff0589 | 2015-06-16 08:52:22 +0000 | [diff] [blame] | 1284 | &page_chg, false); |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1285 | |
| 1286 | map->work_buf = orig_work_buf; |
| 1287 | |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 1288 | if (ret != 0) |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | *reg = range->window_start + win_offset; |
| 1293 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1294 | return 0; |
| 1295 | } |
| 1296 | |
Mark Brown | 584de32 | 2013-03-13 19:19:34 +0000 | [diff] [blame] | 1297 | int _regmap_raw_write(struct regmap *map, unsigned int reg, |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1298 | const void *val, size_t val_len) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1299 | { |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1300 | struct regmap_range_node *range; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1301 | unsigned long flags; |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1302 | u8 *u8 = map->work_buf; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1303 | void *work_val = map->work_buf + map->format.reg_bytes + |
| 1304 | map->format.pad_bytes; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1305 | void *buf; |
| 1306 | int ret = -ENOTSUPP; |
| 1307 | size_t len; |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1308 | int i; |
| 1309 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1310 | WARN_ON(!map->bus); |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1311 | |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1312 | /* Check for unwritable registers before we start */ |
| 1313 | if (map->writeable_reg) |
| 1314 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1315 | if (!map->writeable_reg(map->dev, |
Xiubo Li | ca747be | 2016-01-04 18:00:33 +0800 | [diff] [blame^] | 1316 | reg + regmap_get_offset(map, i))) |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 1317 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1318 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1319 | if (!map->cache_bypass && map->format.parse_val) { |
| 1320 | unsigned int ival; |
| 1321 | int val_bytes = map->format.val_bytes; |
| 1322 | for (i = 0; i < val_len / val_bytes; i++) { |
Stephen Warren | 5a08d15 | 2013-03-20 17:02:02 -0600 | [diff] [blame] | 1323 | ival = map->format.parse_val(val + (i * val_bytes)); |
Xiubo Li | ca747be | 2016-01-04 18:00:33 +0800 | [diff] [blame^] | 1324 | ret = regcache_write(map, |
| 1325 | reg + regmap_get_offset(map, i), |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1326 | ival); |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1327 | if (ret) { |
| 1328 | dev_err(map->dev, |
Mark Brown | 6d04b8a | 2012-10-26 19:05:32 +0100 | [diff] [blame] | 1329 | "Error in caching of register: %x ret: %d\n", |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 1330 | reg + i, ret); |
| 1331 | return ret; |
| 1332 | } |
| 1333 | } |
| 1334 | if (map->cache_only) { |
| 1335 | map->cache_dirty = true; |
| 1336 | return 0; |
| 1337 | } |
| 1338 | } |
| 1339 | |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1340 | range = _regmap_range_lookup(map, reg); |
| 1341 | if (range) { |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1342 | int val_num = val_len / map->format.val_bytes; |
| 1343 | int win_offset = (reg - range->range_min) % range->window_len; |
| 1344 | int win_residue = range->window_len - win_offset; |
| 1345 | |
| 1346 | /* If the write goes beyond the end of the window split it */ |
| 1347 | while (val_num > win_residue) { |
Fabio Estevam | 1a61cfe | 2012-10-25 14:07:18 -0200 | [diff] [blame] | 1348 | dev_dbg(map->dev, "Writing window %d/%zu\n", |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1349 | win_residue, val_len / map->format.val_bytes); |
| 1350 | ret = _regmap_raw_write(map, reg, val, win_residue * |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1351 | map->format.val_bytes); |
Mark Brown | 8a2ceac | 2012-10-04 18:20:18 +0100 | [diff] [blame] | 1352 | if (ret != 0) |
| 1353 | return ret; |
| 1354 | |
| 1355 | reg += win_residue; |
| 1356 | val_num -= win_residue; |
| 1357 | val += win_residue * map->format.val_bytes; |
| 1358 | val_len -= win_residue * map->format.val_bytes; |
| 1359 | |
| 1360 | win_offset = (reg - range->range_min) % |
| 1361 | range->window_len; |
| 1362 | win_residue = range->window_len - win_offset; |
| 1363 | } |
| 1364 | |
| 1365 | ret = _regmap_select_page(map, ®, range, val_num); |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 1366 | if (ret != 0) |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 1367 | return ret; |
| 1368 | } |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1369 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1370 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1371 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1372 | u8[0] |= map->write_flag_mask; |
| 1373 | |
Mark Brown | 651e013 | 2013-10-08 18:37:36 +0100 | [diff] [blame] | 1374 | /* |
| 1375 | * Essentially all I/O mechanisms will be faster with a single |
| 1376 | * buffer to write. Since register syncs often generate raw |
| 1377 | * writes of single registers optimise that case. |
| 1378 | */ |
| 1379 | if (val != work_val && val_len == map->format.val_bytes) { |
| 1380 | memcpy(work_val, val, map->format.val_bytes); |
| 1381 | val = work_val; |
| 1382 | } |
| 1383 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1384 | if (map->async && map->bus->async_write) { |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1385 | struct regmap_async *async; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1386 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 1387 | trace_regmap_async_write_start(map, reg, val_len); |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 1388 | |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1389 | spin_lock_irqsave(&map->async_lock, flags); |
| 1390 | async = list_first_entry_or_null(&map->async_free, |
| 1391 | struct regmap_async, |
| 1392 | list); |
| 1393 | if (async) |
| 1394 | list_del(&async->list); |
| 1395 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 1396 | |
| 1397 | if (!async) { |
| 1398 | async = map->bus->async_alloc(); |
| 1399 | if (!async) |
| 1400 | return -ENOMEM; |
| 1401 | |
| 1402 | async->work_buf = kzalloc(map->format.buf_size, |
| 1403 | GFP_KERNEL | GFP_DMA); |
| 1404 | if (!async->work_buf) { |
| 1405 | kfree(async); |
| 1406 | return -ENOMEM; |
| 1407 | } |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1408 | } |
| 1409 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1410 | async->map = map; |
| 1411 | |
| 1412 | /* If the caller supplied the value we can use it safely. */ |
| 1413 | memcpy(async->work_buf, map->work_buf, map->format.pad_bytes + |
| 1414 | map->format.reg_bytes + map->format.val_bytes); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1415 | |
| 1416 | spin_lock_irqsave(&map->async_lock, flags); |
| 1417 | list_add_tail(&async->list, &map->async_list); |
| 1418 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 1419 | |
Mark Brown | 04c50cc | 2013-10-10 22:38:29 +0100 | [diff] [blame] | 1420 | if (val != work_val) |
| 1421 | ret = map->bus->async_write(map->bus_context, |
| 1422 | async->work_buf, |
| 1423 | map->format.reg_bytes + |
| 1424 | map->format.pad_bytes, |
| 1425 | val, val_len, async); |
| 1426 | else |
| 1427 | ret = map->bus->async_write(map->bus_context, |
| 1428 | async->work_buf, |
| 1429 | map->format.reg_bytes + |
| 1430 | map->format.pad_bytes + |
| 1431 | val_len, NULL, 0, async); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1432 | |
| 1433 | if (ret != 0) { |
| 1434 | dev_err(map->dev, "Failed to schedule write: %d\n", |
| 1435 | ret); |
| 1436 | |
| 1437 | spin_lock_irqsave(&map->async_lock, flags); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 1438 | list_move(&async->list, &map->async_free); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1439 | spin_unlock_irqrestore(&map->async_lock, flags); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1440 | } |
Mark Brown | f951b65 | 2013-03-27 13:08:44 +0000 | [diff] [blame] | 1441 | |
| 1442 | return ret; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1443 | } |
| 1444 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 1445 | trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1446 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1447 | /* If we're doing a single register write we can probably just |
| 1448 | * send the work_buf directly, otherwise try to do a gather |
| 1449 | * write. |
| 1450 | */ |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 1451 | if (val == work_val) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1452 | ret = map->bus->write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1453 | map->format.reg_bytes + |
| 1454 | map->format.pad_bytes + |
| 1455 | val_len); |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1456 | else if (map->bus->gather_write) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1457 | ret = map->bus->gather_write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1458 | map->format.reg_bytes + |
| 1459 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1460 | val, val_len); |
| 1461 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 1462 | /* If that didn't work fall back on linearising by hand. */ |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1463 | if (ret == -ENOTSUPP) { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1464 | len = map->format.reg_bytes + map->format.pad_bytes + val_len; |
| 1465 | buf = kzalloc(len, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1466 | if (!buf) |
| 1467 | return -ENOMEM; |
| 1468 | |
| 1469 | memcpy(buf, map->work_buf, map->format.reg_bytes); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1470 | memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, |
| 1471 | val, val_len); |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1472 | ret = map->bus->write(map->bus_context, buf, len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1473 | |
| 1474 | kfree(buf); |
| 1475 | } |
| 1476 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 1477 | trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1478 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1479 | return ret; |
| 1480 | } |
| 1481 | |
Mark Brown | 221ad7f | 2013-03-26 21:24:20 +0000 | [diff] [blame] | 1482 | /** |
| 1483 | * regmap_can_raw_write - Test if regmap_raw_write() is supported |
| 1484 | * |
| 1485 | * @map: Map to check. |
| 1486 | */ |
| 1487 | bool regmap_can_raw_write(struct regmap *map) |
| 1488 | { |
Markus Pargmann | 07ea400 | 2015-08-12 12:12:33 +0200 | [diff] [blame] | 1489 | return map->bus && map->bus->write && map->format.format_val && |
| 1490 | map->format.format_reg; |
Mark Brown | 221ad7f | 2013-03-26 21:24:20 +0000 | [diff] [blame] | 1491 | } |
| 1492 | EXPORT_SYMBOL_GPL(regmap_can_raw_write); |
| 1493 | |
Markus Pargmann | f50c9eb | 2015-08-30 09:33:54 +0200 | [diff] [blame] | 1494 | /** |
| 1495 | * regmap_get_raw_read_max - Get the maximum size we can read |
| 1496 | * |
| 1497 | * @map: Map to check. |
| 1498 | */ |
| 1499 | size_t regmap_get_raw_read_max(struct regmap *map) |
| 1500 | { |
| 1501 | return map->max_raw_read; |
| 1502 | } |
| 1503 | EXPORT_SYMBOL_GPL(regmap_get_raw_read_max); |
| 1504 | |
| 1505 | /** |
| 1506 | * regmap_get_raw_write_max - Get the maximum size we can read |
| 1507 | * |
| 1508 | * @map: Map to check. |
| 1509 | */ |
| 1510 | size_t regmap_get_raw_write_max(struct regmap *map) |
| 1511 | { |
| 1512 | return map->max_raw_write; |
| 1513 | } |
| 1514 | EXPORT_SYMBOL_GPL(regmap_get_raw_write_max); |
| 1515 | |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1516 | static int _regmap_bus_formatted_write(void *context, unsigned int reg, |
| 1517 | unsigned int val) |
| 1518 | { |
| 1519 | int ret; |
| 1520 | struct regmap_range_node *range; |
| 1521 | struct regmap *map = context; |
| 1522 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1523 | WARN_ON(!map->bus || !map->format.format_write); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1524 | |
| 1525 | range = _regmap_range_lookup(map, reg); |
| 1526 | if (range) { |
| 1527 | ret = _regmap_select_page(map, ®, range, 1); |
| 1528 | if (ret != 0) |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
| 1532 | map->format.format_write(map, reg, val); |
| 1533 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 1534 | trace_regmap_hw_write_start(map, reg, 1); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1535 | |
| 1536 | ret = map->bus->write(map->bus_context, map->work_buf, |
| 1537 | map->format.buf_size); |
| 1538 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 1539 | trace_regmap_hw_write_done(map, reg, 1); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1540 | |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
Boris BREZILLON | 3ac1703 | 2014-04-17 11:40:11 +0200 | [diff] [blame] | 1544 | static int _regmap_bus_reg_write(void *context, unsigned int reg, |
| 1545 | unsigned int val) |
| 1546 | { |
| 1547 | struct regmap *map = context; |
| 1548 | |
| 1549 | return map->bus->reg_write(map->bus_context, reg, val); |
| 1550 | } |
| 1551 | |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1552 | static int _regmap_bus_raw_write(void *context, unsigned int reg, |
| 1553 | unsigned int val) |
| 1554 | { |
| 1555 | struct regmap *map = context; |
| 1556 | |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 1557 | WARN_ON(!map->bus || !map->format.format_val); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1558 | |
| 1559 | map->format.format_val(map->work_buf + map->format.reg_bytes |
| 1560 | + map->format.pad_bytes, val, 0); |
| 1561 | return _regmap_raw_write(map, reg, |
| 1562 | map->work_buf + |
| 1563 | map->format.reg_bytes + |
| 1564 | map->format.pad_bytes, |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1565 | map->format.val_bytes); |
Andrey Smirnov | 07c320d | 2013-01-12 12:54:13 -0800 | [diff] [blame] | 1566 | } |
| 1567 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1568 | static inline void *_regmap_map_get_context(struct regmap *map) |
| 1569 | { |
| 1570 | return (map->bus) ? map : map->bus_context; |
| 1571 | } |
| 1572 | |
Dimitris Papastamos | 4d2dc09 | 2011-09-29 10:39:07 +0100 | [diff] [blame] | 1573 | int _regmap_write(struct regmap *map, unsigned int reg, |
| 1574 | unsigned int val) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1575 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1576 | int ret; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1577 | void *context = _regmap_map_get_context(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1578 | |
Ionut Nicu | 515f226 | 2013-08-09 12:09:20 +0200 | [diff] [blame] | 1579 | if (!regmap_writeable(map, reg)) |
| 1580 | return -EIO; |
| 1581 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1582 | if (!map->cache_bypass && !map->defer_caching) { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1583 | ret = regcache_write(map, reg, val); |
| 1584 | if (ret != 0) |
| 1585 | return ret; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 1586 | if (map->cache_only) { |
| 1587 | map->cache_dirty = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1588 | return 0; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 1589 | } |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1590 | } |
| 1591 | |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 1592 | #ifdef LOG_DEVICE |
Pankaj Dubey | 5336be8 | 2014-09-27 09:47:55 +0530 | [diff] [blame] | 1593 | if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0) |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 1594 | dev_info(map->dev, "%x <= %x\n", reg, val); |
| 1595 | #endif |
| 1596 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 1597 | trace_regmap_reg_write(map, reg, val); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1598 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1599 | return map->reg_write(context, reg, val); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | /** |
| 1603 | * regmap_write(): Write a value to a single register |
| 1604 | * |
| 1605 | * @map: Register map to write to |
| 1606 | * @reg: Register to write to |
| 1607 | * @val: Value to be written |
| 1608 | * |
| 1609 | * A value of zero will be returned on success, a negative errno will |
| 1610 | * be returned in error cases. |
| 1611 | */ |
| 1612 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) |
| 1613 | { |
| 1614 | int ret; |
| 1615 | |
Xiubo Li | fcac023 | 2015-12-16 17:45:32 +0800 | [diff] [blame] | 1616 | if (!IS_ALIGNED(reg, map->reg_stride)) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1617 | return -EINVAL; |
| 1618 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1619 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1620 | |
| 1621 | ret = _regmap_write(map, reg, val); |
| 1622 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1623 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1624 | |
| 1625 | return ret; |
| 1626 | } |
| 1627 | EXPORT_SYMBOL_GPL(regmap_write); |
| 1628 | |
| 1629 | /** |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 1630 | * regmap_write_async(): Write a value to a single register asynchronously |
| 1631 | * |
| 1632 | * @map: Register map to write to |
| 1633 | * @reg: Register to write to |
| 1634 | * @val: Value to be written |
| 1635 | * |
| 1636 | * A value of zero will be returned on success, a negative errno will |
| 1637 | * be returned in error cases. |
| 1638 | */ |
| 1639 | int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val) |
| 1640 | { |
| 1641 | int ret; |
| 1642 | |
Xiubo Li | fcac023 | 2015-12-16 17:45:32 +0800 | [diff] [blame] | 1643 | if (!IS_ALIGNED(reg, map->reg_stride)) |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 1644 | return -EINVAL; |
| 1645 | |
| 1646 | map->lock(map->lock_arg); |
| 1647 | |
| 1648 | map->async = true; |
| 1649 | |
| 1650 | ret = _regmap_write(map, reg, val); |
| 1651 | |
| 1652 | map->async = false; |
| 1653 | |
| 1654 | map->unlock(map->lock_arg); |
| 1655 | |
| 1656 | return ret; |
| 1657 | } |
| 1658 | EXPORT_SYMBOL_GPL(regmap_write_async); |
| 1659 | |
| 1660 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1661 | * regmap_raw_write(): Write raw values to one or more registers |
| 1662 | * |
| 1663 | * @map: Register map to write to |
| 1664 | * @reg: Initial register to write to |
| 1665 | * @val: Block of data to be written, laid out for direct transmission to the |
| 1666 | * device |
| 1667 | * @val_len: Length of data pointed to by val. |
| 1668 | * |
| 1669 | * This function is intended to be used for things like firmware |
| 1670 | * download where a large block of data needs to be transferred to the |
| 1671 | * device. No formatting will be done on the data provided. |
| 1672 | * |
| 1673 | * A value of zero will be returned on success, a negative errno will |
| 1674 | * be returned in error cases. |
| 1675 | */ |
| 1676 | int regmap_raw_write(struct regmap *map, unsigned int reg, |
| 1677 | const void *val, size_t val_len) |
| 1678 | { |
| 1679 | int ret; |
| 1680 | |
Mark Brown | 221ad7f | 2013-03-26 21:24:20 +0000 | [diff] [blame] | 1681 | if (!regmap_can_raw_write(map)) |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 1682 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1683 | if (val_len % map->format.val_bytes) |
| 1684 | return -EINVAL; |
Markus Pargmann | c335931 | 2015-08-30 09:33:55 +0200 | [diff] [blame] | 1685 | if (map->max_raw_write && map->max_raw_write > val_len) |
| 1686 | return -E2BIG; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1687 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1688 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1689 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1690 | ret = _regmap_raw_write(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1691 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 1692 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1693 | |
| 1694 | return ret; |
| 1695 | } |
| 1696 | EXPORT_SYMBOL_GPL(regmap_raw_write); |
| 1697 | |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 1698 | /** |
| 1699 | * regmap_field_write(): Write a value to a single register field |
| 1700 | * |
| 1701 | * @field: Register field to write to |
| 1702 | * @val: Value to be written |
| 1703 | * |
| 1704 | * A value of zero will be returned on success, a negative errno will |
| 1705 | * be returned in error cases. |
| 1706 | */ |
| 1707 | int regmap_field_write(struct regmap_field *field, unsigned int val) |
| 1708 | { |
| 1709 | return regmap_update_bits(field->regmap, field->reg, |
| 1710 | field->mask, val << field->shift); |
| 1711 | } |
| 1712 | EXPORT_SYMBOL_GPL(regmap_field_write); |
| 1713 | |
Kuninori Morimoto | fdf2002 | 2013-09-01 20:24:50 -0700 | [diff] [blame] | 1714 | /** |
| 1715 | * regmap_field_update_bits(): Perform a read/modify/write cycle |
| 1716 | * on the register field |
| 1717 | * |
| 1718 | * @field: Register field to write to |
| 1719 | * @mask: Bitmask to change |
| 1720 | * @val: Value to be written |
| 1721 | * |
| 1722 | * A value of zero will be returned on success, a negative errno will |
| 1723 | * be returned in error cases. |
| 1724 | */ |
| 1725 | int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val) |
| 1726 | { |
| 1727 | mask = (mask << field->shift) & field->mask; |
| 1728 | |
| 1729 | return regmap_update_bits(field->regmap, field->reg, |
| 1730 | mask, val << field->shift); |
| 1731 | } |
| 1732 | EXPORT_SYMBOL_GPL(regmap_field_update_bits); |
| 1733 | |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 1734 | /** |
| 1735 | * regmap_fields_write(): Write a value to a single register field with port ID |
| 1736 | * |
| 1737 | * @field: Register field to write to |
| 1738 | * @id: port ID |
| 1739 | * @val: Value to be written |
| 1740 | * |
| 1741 | * A value of zero will be returned on success, a negative errno will |
| 1742 | * be returned in error cases. |
| 1743 | */ |
| 1744 | int regmap_fields_write(struct regmap_field *field, unsigned int id, |
| 1745 | unsigned int val) |
| 1746 | { |
| 1747 | if (id >= field->id_size) |
| 1748 | return -EINVAL; |
| 1749 | |
| 1750 | return regmap_update_bits(field->regmap, |
| 1751 | field->reg + (field->id_offset * id), |
| 1752 | field->mask, val << field->shift); |
| 1753 | } |
| 1754 | EXPORT_SYMBOL_GPL(regmap_fields_write); |
| 1755 | |
Kuninori Morimoto | e874e6c | 2015-06-16 08:52:55 +0000 | [diff] [blame] | 1756 | int regmap_fields_force_write(struct regmap_field *field, unsigned int id, |
| 1757 | unsigned int val) |
| 1758 | { |
| 1759 | if (id >= field->id_size) |
| 1760 | return -EINVAL; |
| 1761 | |
| 1762 | return regmap_write_bits(field->regmap, |
| 1763 | field->reg + (field->id_offset * id), |
| 1764 | field->mask, val << field->shift); |
| 1765 | } |
| 1766 | EXPORT_SYMBOL_GPL(regmap_fields_force_write); |
| 1767 | |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 1768 | /** |
| 1769 | * regmap_fields_update_bits(): Perform a read/modify/write cycle |
| 1770 | * on the register field |
| 1771 | * |
| 1772 | * @field: Register field to write to |
| 1773 | * @id: port ID |
| 1774 | * @mask: Bitmask to change |
| 1775 | * @val: Value to be written |
| 1776 | * |
| 1777 | * A value of zero will be returned on success, a negative errno will |
| 1778 | * be returned in error cases. |
| 1779 | */ |
| 1780 | int regmap_fields_update_bits(struct regmap_field *field, unsigned int id, |
| 1781 | unsigned int mask, unsigned int val) |
| 1782 | { |
| 1783 | if (id >= field->id_size) |
| 1784 | return -EINVAL; |
| 1785 | |
| 1786 | mask = (mask << field->shift) & field->mask; |
| 1787 | |
| 1788 | return regmap_update_bits(field->regmap, |
| 1789 | field->reg + (field->id_offset * id), |
| 1790 | mask, val << field->shift); |
| 1791 | } |
| 1792 | EXPORT_SYMBOL_GPL(regmap_fields_update_bits); |
| 1793 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1794 | /* |
| 1795 | * regmap_bulk_write(): Write multiple registers to the device |
| 1796 | * |
| 1797 | * @map: Register map to write to |
| 1798 | * @reg: First register to be write from |
| 1799 | * @val: Block of data to be written, in native register size for device |
| 1800 | * @val_count: Number of registers to write |
| 1801 | * |
| 1802 | * 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] | 1803 | * data to the device either in single transfer or multiple transfer. |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1804 | * |
| 1805 | * A value of zero will be returned on success, a negative errno will |
| 1806 | * be returned in error cases. |
| 1807 | */ |
| 1808 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, |
| 1809 | size_t val_count) |
| 1810 | { |
| 1811 | int ret = 0, i; |
| 1812 | size_t val_bytes = map->format.val_bytes; |
Markus Pargmann | adaac45 | 2015-08-30 09:33:53 +0200 | [diff] [blame] | 1813 | size_t total_size = val_bytes * val_count; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1814 | |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1815 | if (map->bus && !map->format.parse_inplace) |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1816 | return -EINVAL; |
Xiubo Li | fcac023 | 2015-12-16 17:45:32 +0800 | [diff] [blame] | 1817 | if (!IS_ALIGNED(reg, map->reg_stride)) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1818 | return -EINVAL; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1819 | |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1820 | /* |
| 1821 | * Some devices don't support bulk write, for |
Markus Pargmann | c594b7f | 2015-08-21 10:26:41 +0200 | [diff] [blame] | 1822 | * them we have a series of single write operations in the first two if |
| 1823 | * blocks. |
| 1824 | * |
| 1825 | * The first if block is used for memory mapped io. It does not allow |
| 1826 | * val_bytes of 3 for example. |
| 1827 | * The second one is used for busses which do not have this limitation |
| 1828 | * and can write arbitrary value lengths. |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1829 | */ |
Markus Pargmann | c594b7f | 2015-08-21 10:26:41 +0200 | [diff] [blame] | 1830 | if (!map->bus) { |
Takashi Iwai | 4999e96 | 2014-03-18 12:58:33 +0100 | [diff] [blame] | 1831 | map->lock(map->lock_arg); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1832 | for (i = 0; i < val_count; i++) { |
| 1833 | unsigned int ival; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1834 | |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1835 | switch (val_bytes) { |
| 1836 | case 1: |
| 1837 | ival = *(u8 *)(val + (i * val_bytes)); |
| 1838 | break; |
| 1839 | case 2: |
| 1840 | ival = *(u16 *)(val + (i * val_bytes)); |
| 1841 | break; |
| 1842 | case 4: |
| 1843 | ival = *(u32 *)(val + (i * val_bytes)); |
| 1844 | break; |
| 1845 | #ifdef CONFIG_64BIT |
| 1846 | case 8: |
| 1847 | ival = *(u64 *)(val + (i * val_bytes)); |
| 1848 | break; |
| 1849 | #endif |
| 1850 | default: |
| 1851 | ret = -EINVAL; |
| 1852 | goto out; |
| 1853 | } |
| 1854 | |
Xiubo Li | ca747be | 2016-01-04 18:00:33 +0800 | [diff] [blame^] | 1855 | ret = _regmap_write(map, |
| 1856 | reg + regmap_get_offset(map, i), |
| 1857 | ival); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1858 | if (ret != 0) |
| 1859 | goto out; |
| 1860 | } |
Takashi Iwai | 4999e96 | 2014-03-18 12:58:33 +0100 | [diff] [blame] | 1861 | out: |
| 1862 | map->unlock(map->lock_arg); |
Markus Pargmann | adaac45 | 2015-08-30 09:33:53 +0200 | [diff] [blame] | 1863 | } else if (map->use_single_write || |
| 1864 | (map->max_raw_write && map->max_raw_write < total_size)) { |
| 1865 | int chunk_stride = map->reg_stride; |
| 1866 | size_t chunk_size = val_bytes; |
| 1867 | size_t chunk_count = val_count; |
| 1868 | |
| 1869 | if (!map->use_single_write) { |
| 1870 | chunk_size = map->max_raw_write; |
| 1871 | if (chunk_size % val_bytes) |
| 1872 | chunk_size -= chunk_size % val_bytes; |
| 1873 | chunk_count = total_size / chunk_size; |
| 1874 | chunk_stride *= chunk_size / val_bytes; |
| 1875 | } |
| 1876 | |
Markus Pargmann | c594b7f | 2015-08-21 10:26:41 +0200 | [diff] [blame] | 1877 | map->lock(map->lock_arg); |
Markus Pargmann | adaac45 | 2015-08-30 09:33:53 +0200 | [diff] [blame] | 1878 | /* Write as many bytes as possible with chunk_size */ |
| 1879 | for (i = 0; i < chunk_count; i++) { |
Markus Pargmann | c594b7f | 2015-08-21 10:26:41 +0200 | [diff] [blame] | 1880 | ret = _regmap_raw_write(map, |
Markus Pargmann | adaac45 | 2015-08-30 09:33:53 +0200 | [diff] [blame] | 1881 | reg + (i * chunk_stride), |
| 1882 | val + (i * chunk_size), |
| 1883 | chunk_size); |
Markus Pargmann | c594b7f | 2015-08-21 10:26:41 +0200 | [diff] [blame] | 1884 | if (ret) |
| 1885 | break; |
| 1886 | } |
Markus Pargmann | adaac45 | 2015-08-30 09:33:53 +0200 | [diff] [blame] | 1887 | |
| 1888 | /* Write remaining bytes */ |
| 1889 | if (!ret && chunk_size * i < total_size) { |
| 1890 | ret = _regmap_raw_write(map, reg + (i * chunk_stride), |
| 1891 | val + (i * chunk_size), |
| 1892 | total_size - i * chunk_size); |
| 1893 | } |
Markus Pargmann | c594b7f | 2015-08-21 10:26:41 +0200 | [diff] [blame] | 1894 | map->unlock(map->lock_arg); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1895 | } else { |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1896 | void *wval; |
| 1897 | |
Xiubo Li | d6b41cb | 2014-09-28 17:09:54 +0800 | [diff] [blame] | 1898 | if (!val_count) |
| 1899 | return -EINVAL; |
| 1900 | |
Stephen Boyd | b4a21fc | 2015-09-11 16:37:05 -0700 | [diff] [blame] | 1901 | wval = kmemdup(val, val_count * val_bytes, map->alloc_flags); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1902 | if (!wval) { |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1903 | dev_err(map->dev, "Error in memory allocation\n"); |
Takashi Iwai | 4999e96 | 2014-03-18 12:58:33 +0100 | [diff] [blame] | 1904 | return -ENOMEM; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1905 | } |
| 1906 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 1907 | map->format.parse_inplace(wval + i); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1908 | |
Takashi Iwai | 4999e96 | 2014-03-18 12:58:33 +0100 | [diff] [blame] | 1909 | map->lock(map->lock_arg); |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 1910 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); |
Takashi Iwai | 4999e96 | 2014-03-18 12:58:33 +0100 | [diff] [blame] | 1911 | map->unlock(map->lock_arg); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1912 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1913 | kfree(wval); |
Stephen Boyd | f429836 | 2013-12-26 13:52:04 -0800 | [diff] [blame] | 1914 | } |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1915 | return ret; |
| 1916 | } |
| 1917 | EXPORT_SYMBOL_GPL(regmap_bulk_write); |
| 1918 | |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 1919 | /* |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1920 | * _regmap_raw_multi_reg_write() |
| 1921 | * |
| 1922 | * the (register,newvalue) pairs in regs have not been formatted, but |
| 1923 | * they are all in the same page and have been changed to being page |
Xiubo Li | b486afb | 2015-08-12 15:02:19 +0800 | [diff] [blame] | 1924 | * relative. The page register has been written if that was necessary. |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1925 | */ |
| 1926 | static int _regmap_raw_multi_reg_write(struct regmap *map, |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 1927 | const struct reg_sequence *regs, |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1928 | size_t num_regs) |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 1929 | { |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1930 | int ret; |
| 1931 | void *buf; |
| 1932 | int i; |
| 1933 | u8 *u8; |
| 1934 | size_t val_bytes = map->format.val_bytes; |
| 1935 | size_t reg_bytes = map->format.reg_bytes; |
| 1936 | size_t pad_bytes = map->format.pad_bytes; |
| 1937 | size_t pair_size = reg_bytes + pad_bytes + val_bytes; |
| 1938 | size_t len = pair_size * num_regs; |
| 1939 | |
Xiubo Li | f5727cd | 2014-04-30 17:31:08 +0800 | [diff] [blame] | 1940 | if (!len) |
| 1941 | return -EINVAL; |
| 1942 | |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1943 | buf = kzalloc(len, GFP_KERNEL); |
| 1944 | if (!buf) |
| 1945 | return -ENOMEM; |
| 1946 | |
| 1947 | /* We have to linearise by hand. */ |
| 1948 | |
| 1949 | u8 = buf; |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 1950 | |
| 1951 | for (i = 0; i < num_regs; i++) { |
Markus Pargmann | 2f9b660 | 2015-08-12 12:12:28 +0200 | [diff] [blame] | 1952 | unsigned int reg = regs[i].reg; |
| 1953 | unsigned int val = regs[i].def; |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 1954 | trace_regmap_hw_write_start(map, reg, 1); |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1955 | map->format.format_reg(u8, reg, map->reg_shift); |
| 1956 | u8 += reg_bytes + pad_bytes; |
| 1957 | map->format.format_val(u8, val, 0); |
| 1958 | u8 += val_bytes; |
| 1959 | } |
| 1960 | u8 = buf; |
| 1961 | *u8 |= map->write_flag_mask; |
| 1962 | |
| 1963 | ret = map->bus->write(map->bus_context, buf, len); |
| 1964 | |
| 1965 | kfree(buf); |
| 1966 | |
| 1967 | for (i = 0; i < num_regs; i++) { |
| 1968 | int reg = regs[i].reg; |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 1969 | trace_regmap_hw_write_done(map, reg, 1); |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1970 | } |
| 1971 | return ret; |
| 1972 | } |
| 1973 | |
| 1974 | static unsigned int _regmap_register_page(struct regmap *map, |
| 1975 | unsigned int reg, |
| 1976 | struct regmap_range_node *range) |
| 1977 | { |
| 1978 | unsigned int win_page = (reg - range->range_min) / range->window_len; |
| 1979 | |
| 1980 | return win_page; |
| 1981 | } |
| 1982 | |
| 1983 | static int _regmap_range_multi_paged_reg_write(struct regmap *map, |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 1984 | struct reg_sequence *regs, |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1985 | size_t num_regs) |
| 1986 | { |
| 1987 | int ret; |
| 1988 | int i, n; |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 1989 | struct reg_sequence *base; |
Geert Uytterhoeven | b48d139 | 2014-04-22 12:47:29 +0200 | [diff] [blame] | 1990 | unsigned int this_page = 0; |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 1991 | unsigned int page_change = 0; |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1992 | /* |
| 1993 | * the set of registers are not neccessarily in order, but |
| 1994 | * since the order of write must be preserved this algorithm |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 1995 | * chops the set each time the page changes. This also applies |
| 1996 | * if there is a delay required at any point in the sequence. |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 1997 | */ |
| 1998 | base = regs; |
| 1999 | for (i = 0, n = 0; i < num_regs; i++, n++) { |
| 2000 | unsigned int reg = regs[i].reg; |
| 2001 | struct regmap_range_node *range; |
| 2002 | |
| 2003 | range = _regmap_range_lookup(map, reg); |
| 2004 | if (range) { |
| 2005 | unsigned int win_page = _regmap_register_page(map, reg, |
| 2006 | range); |
| 2007 | |
| 2008 | if (i == 0) |
| 2009 | this_page = win_page; |
| 2010 | if (win_page != this_page) { |
| 2011 | this_page = win_page; |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 2012 | page_change = 1; |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | /* If we have both a page change and a delay make sure to |
| 2017 | * write the regs and apply the delay before we change the |
| 2018 | * page. |
| 2019 | */ |
| 2020 | |
| 2021 | if (page_change || regs[i].delay_us) { |
| 2022 | |
| 2023 | /* For situations where the first write requires |
| 2024 | * a delay we need to make sure we don't call |
| 2025 | * raw_multi_reg_write with n=0 |
| 2026 | * This can't occur with page breaks as we |
| 2027 | * never write on the first iteration |
| 2028 | */ |
| 2029 | if (regs[i].delay_us && i == 0) |
| 2030 | n = 1; |
| 2031 | |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2032 | ret = _regmap_raw_multi_reg_write(map, base, n); |
| 2033 | if (ret != 0) |
| 2034 | return ret; |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 2035 | |
| 2036 | if (regs[i].delay_us) |
| 2037 | udelay(regs[i].delay_us); |
| 2038 | |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2039 | base += n; |
| 2040 | n = 0; |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 2041 | |
| 2042 | if (page_change) { |
| 2043 | ret = _regmap_select_page(map, |
| 2044 | &base[n].reg, |
| 2045 | range, 1); |
| 2046 | if (ret != 0) |
| 2047 | return ret; |
| 2048 | |
| 2049 | page_change = 0; |
| 2050 | } |
| 2051 | |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2052 | } |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 2053 | |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2054 | } |
| 2055 | if (n > 0) |
| 2056 | return _regmap_raw_multi_reg_write(map, base, n); |
| 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | static int _regmap_multi_reg_write(struct regmap *map, |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 2061 | const struct reg_sequence *regs, |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2062 | size_t num_regs) |
| 2063 | { |
| 2064 | int i; |
| 2065 | int ret; |
| 2066 | |
| 2067 | if (!map->can_multi_write) { |
| 2068 | for (i = 0; i < num_regs; i++) { |
| 2069 | ret = _regmap_write(map, regs[i].reg, regs[i].def); |
| 2070 | if (ret != 0) |
| 2071 | return ret; |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 2072 | |
| 2073 | if (regs[i].delay_us) |
| 2074 | udelay(regs[i].delay_us); |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2075 | } |
| 2076 | return 0; |
| 2077 | } |
| 2078 | |
| 2079 | if (!map->format.parse_inplace) |
| 2080 | return -EINVAL; |
| 2081 | |
| 2082 | if (map->writeable_reg) |
| 2083 | for (i = 0; i < num_regs; i++) { |
| 2084 | int reg = regs[i].reg; |
| 2085 | if (!map->writeable_reg(map->dev, reg)) |
| 2086 | return -EINVAL; |
Xiubo Li | fcac023 | 2015-12-16 17:45:32 +0800 | [diff] [blame] | 2087 | if (!IS_ALIGNED(reg, map->reg_stride)) |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2088 | return -EINVAL; |
| 2089 | } |
| 2090 | |
| 2091 | if (!map->cache_bypass) { |
| 2092 | for (i = 0; i < num_regs; i++) { |
| 2093 | unsigned int val = regs[i].def; |
| 2094 | unsigned int reg = regs[i].reg; |
| 2095 | ret = regcache_write(map, reg, val); |
| 2096 | if (ret) { |
| 2097 | dev_err(map->dev, |
| 2098 | "Error in caching of register: %x ret: %d\n", |
| 2099 | reg, ret); |
| 2100 | return ret; |
| 2101 | } |
| 2102 | } |
| 2103 | if (map->cache_only) { |
| 2104 | map->cache_dirty = true; |
| 2105 | return 0; |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2109 | WARN_ON(!map->bus); |
| 2110 | |
| 2111 | for (i = 0; i < num_regs; i++) { |
| 2112 | unsigned int reg = regs[i].reg; |
| 2113 | struct regmap_range_node *range; |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 2114 | |
| 2115 | /* Coalesce all the writes between a page break or a delay |
| 2116 | * in a sequence |
| 2117 | */ |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2118 | range = _regmap_range_lookup(map, reg); |
Nariman Poushin | 2de9d60 | 2015-07-16 16:36:22 +0100 | [diff] [blame] | 2119 | if (range || regs[i].delay_us) { |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 2120 | size_t len = sizeof(struct reg_sequence)*num_regs; |
| 2121 | struct reg_sequence *base = kmemdup(regs, len, |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2122 | GFP_KERNEL); |
| 2123 | if (!base) |
| 2124 | return -ENOMEM; |
| 2125 | ret = _regmap_range_multi_paged_reg_write(map, base, |
| 2126 | num_regs); |
| 2127 | kfree(base); |
| 2128 | |
| 2129 | return ret; |
| 2130 | } |
| 2131 | } |
| 2132 | return _regmap_raw_multi_reg_write(map, regs, num_regs); |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 2135 | /* |
| 2136 | * regmap_multi_reg_write(): Write multiple registers to the device |
| 2137 | * |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2138 | * where the set of register,value pairs are supplied in any order, |
| 2139 | * possibly not all in a single range. |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 2140 | * |
| 2141 | * @map: Register map to write to |
| 2142 | * @regs: Array of structures containing register,value to be written |
| 2143 | * @num_regs: Number of registers to write |
| 2144 | * |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2145 | * The 'normal' block write mode will send ultimately send data on the |
| 2146 | * target bus as R,V1,V2,V3,..,Vn where successively higer registers are |
| 2147 | * addressed. However, this alternative block multi write mode will send |
| 2148 | * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device |
| 2149 | * must of course support the mode. |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 2150 | * |
Opensource [Anthony Olech] | e894c3f | 2014-03-04 13:54:02 +0000 | [diff] [blame] | 2151 | * A value of zero will be returned on success, a negative errno will be |
| 2152 | * returned in error cases. |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 2153 | */ |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 2154 | int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs, |
Charles Keepax | f7e2cec | 2014-02-25 13:45:49 +0000 | [diff] [blame] | 2155 | int num_regs) |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 2156 | { |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2157 | int ret; |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 2158 | |
| 2159 | map->lock(map->lock_arg); |
| 2160 | |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2161 | ret = _regmap_multi_reg_write(map, regs, num_regs); |
| 2162 | |
Anthony Olech | e33fabd | 2013-10-11 15:31:11 +0100 | [diff] [blame] | 2163 | map->unlock(map->lock_arg); |
| 2164 | |
| 2165 | return ret; |
| 2166 | } |
| 2167 | EXPORT_SYMBOL_GPL(regmap_multi_reg_write); |
| 2168 | |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2169 | /* |
| 2170 | * regmap_multi_reg_write_bypassed(): Write multiple registers to the |
| 2171 | * device but not the cache |
| 2172 | * |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2173 | * where the set of register are supplied in any order |
| 2174 | * |
| 2175 | * @map: Register map to write to |
| 2176 | * @regs: Array of structures containing register,value to be written |
| 2177 | * @num_regs: Number of registers to write |
| 2178 | * |
| 2179 | * This function is intended to be used for writing a large block of data |
| 2180 | * atomically to the device in single transfer for those I2C client devices |
| 2181 | * that implement this alternative block write mode. |
| 2182 | * |
| 2183 | * A value of zero will be returned on success, a negative errno will |
| 2184 | * be returned in error cases. |
| 2185 | */ |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2186 | int regmap_multi_reg_write_bypassed(struct regmap *map, |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 2187 | const struct reg_sequence *regs, |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2188 | int num_regs) |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2189 | { |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2190 | int ret; |
| 2191 | bool bypass; |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2192 | |
| 2193 | map->lock(map->lock_arg); |
| 2194 | |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2195 | bypass = map->cache_bypass; |
| 2196 | map->cache_bypass = true; |
| 2197 | |
| 2198 | ret = _regmap_multi_reg_write(map, regs, num_regs); |
| 2199 | |
| 2200 | map->cache_bypass = bypass; |
| 2201 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 2202 | map->unlock(map->lock_arg); |
| 2203 | |
| 2204 | return ret; |
| 2205 | } |
Charles Keepax | 1d5b40b | 2014-02-25 13:45:50 +0000 | [diff] [blame] | 2206 | EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2207 | |
| 2208 | /** |
| 2209 | * regmap_raw_write_async(): Write raw values to one or more registers |
| 2210 | * asynchronously |
| 2211 | * |
| 2212 | * @map: Register map to write to |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2213 | * @reg: Initial register to write to |
| 2214 | * @val: Block of data to be written, laid out for direct transmission to the |
| 2215 | * device. Must be valid until regmap_async_complete() is called. |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 2216 | * @val_len: Length of data pointed to by val. |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2217 | * |
| 2218 | * This function is intended to be used for things like firmware |
| 2219 | * download where a large block of data needs to be transferred to the |
Mark Brown | f1b5c5c | 2013-03-13 19:18:13 +0000 | [diff] [blame] | 2220 | * device. No formatting will be done on the data provided. |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 2221 | * |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 2222 | * If supported by the underlying bus the write will be scheduled |
| 2223 | * asynchronously, helping maximise I/O speed on higher speed buses |
| 2224 | * like SPI. regmap_async_complete() can be called to ensure that all |
| 2225 | * asynchrnous writes have been completed. |
Mark Brown | 0ff3e62 | 2012-10-04 17:39:13 +0100 | [diff] [blame] | 2226 | * |
Mark Brown | 98bc7df | 2012-10-04 17:31:11 +0100 | [diff] [blame] | 2227 | * A value of zero will be returned on success, a negative errno will |
| 2228 | * be returned in error cases. |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2229 | */ |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 2230 | int regmap_raw_write_async(struct regmap *map, unsigned int reg, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2231 | const void *val, size_t val_len) |
| 2232 | { |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 2233 | int ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2234 | |
| 2235 | if (val_len % map->format.val_bytes) |
| 2236 | return -EINVAL; |
Xiubo Li | fcac023 | 2015-12-16 17:45:32 +0800 | [diff] [blame] | 2237 | if (!IS_ALIGNED(reg, map->reg_stride)) |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 2238 | return -EINVAL; |
| 2239 | |
| 2240 | map->lock(map->lock_arg); |
| 2241 | |
Lars-Peter Clausen | 1925441 | 2011-11-16 16:28:19 +0100 | [diff] [blame] | 2242 | map->async = true; |
| 2243 | |
| 2244 | ret = _regmap_raw_write(map, reg, val, val_len); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 2245 | |
| 2246 | map->async = false; |
| 2247 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2248 | map->unlock(map->lock_arg); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 2249 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2250 | return ret; |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 2251 | } |
| 2252 | EXPORT_SYMBOL_GPL(regmap_raw_write_async); |
| 2253 | |
| 2254 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 2255 | unsigned int val_len) |
| 2256 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 2257 | struct regmap_range_node *range; |
| 2258 | u8 *u8 = map->work_buf; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2259 | int ret; |
Mark Brown | f298536 | 2012-04-30 21:25:05 +0100 | [diff] [blame] | 2260 | |
| 2261 | WARN_ON(!map->bus); |
| 2262 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2263 | range = _regmap_range_lookup(map, reg); |
| 2264 | if (range) { |
| 2265 | ret = _regmap_select_page(map, ®, range, |
| 2266 | val_len / map->format.val_bytes); |
| 2267 | if (ret != 0) |
| 2268 | return ret; |
| 2269 | } |
| 2270 | |
| 2271 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
| 2272 | |
| 2273 | /* |
| 2274 | * Some buses or devices flag reads by setting the high bits in the |
Xiubo Li | b486afb | 2015-08-12 15:02:19 +0800 | [diff] [blame] | 2275 | * register address; since it's always the high bits for all |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2276 | * current formats we can do this here rather than in |
| 2277 | * formatting. This may break if we get interesting formats. |
| 2278 | */ |
| 2279 | u8[0] |= map->read_flag_mask; |
| 2280 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 2281 | trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2282 | |
| 2283 | ret = map->bus->read(map->bus_context, map->work_buf, |
| 2284 | map->format.reg_bytes + map->format.pad_bytes, |
| 2285 | val, val_len); |
| 2286 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 2287 | trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2288 | |
| 2289 | return ret; |
| 2290 | } |
| 2291 | |
Boris BREZILLON | 3ac1703 | 2014-04-17 11:40:11 +0200 | [diff] [blame] | 2292 | static int _regmap_bus_reg_read(void *context, unsigned int reg, |
| 2293 | unsigned int *val) |
| 2294 | { |
| 2295 | struct regmap *map = context; |
| 2296 | |
| 2297 | return map->bus->reg_read(map->bus_context, reg, val); |
| 2298 | } |
| 2299 | |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 2300 | static int _regmap_bus_read(void *context, unsigned int reg, |
| 2301 | unsigned int *val) |
| 2302 | { |
| 2303 | int ret; |
| 2304 | struct regmap *map = context; |
| 2305 | |
| 2306 | if (!map->format.parse_val) |
| 2307 | return -EINVAL; |
| 2308 | |
| 2309 | ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); |
| 2310 | if (ret == 0) |
| 2311 | *val = map->format.parse_val(map->work_buf); |
| 2312 | |
| 2313 | return ret; |
| 2314 | } |
| 2315 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2316 | static int _regmap_read(struct regmap *map, unsigned int reg, |
| 2317 | unsigned int *val) |
| 2318 | { |
| 2319 | int ret; |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 2320 | void *context = _regmap_map_get_context(map); |
| 2321 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2322 | if (!map->cache_bypass) { |
| 2323 | ret = regcache_read(map, reg, val); |
| 2324 | if (ret == 0) |
| 2325 | return 0; |
| 2326 | } |
| 2327 | |
| 2328 | if (map->cache_only) |
| 2329 | return -EBUSY; |
| 2330 | |
Michal Simek | d4807ad | 2014-02-10 12:59:46 +0100 | [diff] [blame] | 2331 | if (!regmap_readable(map, reg)) |
| 2332 | return -EIO; |
| 2333 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 2334 | ret = map->reg_read(context, reg, val); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2335 | if (ret == 0) { |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2336 | #ifdef LOG_DEVICE |
Pankaj Dubey | 5336be8 | 2014-09-27 09:47:55 +0530 | [diff] [blame] | 2337 | if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2338 | dev_info(map->dev, "%x => %x\n", reg, *val); |
| 2339 | #endif |
| 2340 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 2341 | trace_regmap_reg_read(map, reg, *val); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2342 | |
Andrey Smirnov | ad27840 | 2013-01-12 12:54:12 -0800 | [diff] [blame] | 2343 | if (!map->cache_bypass) |
| 2344 | regcache_write(map, reg, *val); |
| 2345 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2346 | |
| 2347 | return ret; |
| 2348 | } |
| 2349 | |
| 2350 | /** |
| 2351 | * regmap_read(): Read a value from a single register |
| 2352 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 2353 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2354 | * @reg: Register to be read from |
| 2355 | * @val: Pointer to store read value |
| 2356 | * |
| 2357 | * A value of zero will be returned on success, a negative errno will |
| 2358 | * be returned in error cases. |
| 2359 | */ |
| 2360 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) |
| 2361 | { |
| 2362 | int ret; |
| 2363 | |
Xiubo Li | fcac023 | 2015-12-16 17:45:32 +0800 | [diff] [blame] | 2364 | if (!IS_ALIGNED(reg, map->reg_stride)) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 2365 | return -EINVAL; |
| 2366 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2367 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2368 | |
| 2369 | ret = _regmap_read(map, reg, val); |
| 2370 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2371 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2372 | |
| 2373 | return ret; |
| 2374 | } |
| 2375 | EXPORT_SYMBOL_GPL(regmap_read); |
| 2376 | |
| 2377 | /** |
| 2378 | * regmap_raw_read(): Read raw data from the device |
| 2379 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 2380 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2381 | * @reg: First register to be read from |
| 2382 | * @val: Pointer to store read value |
| 2383 | * @val_len: Size of data to read |
| 2384 | * |
| 2385 | * A value of zero will be returned on success, a negative errno will |
| 2386 | * be returned in error cases. |
| 2387 | */ |
| 2388 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 2389 | size_t val_len) |
| 2390 | { |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 2391 | size_t val_bytes = map->format.val_bytes; |
| 2392 | size_t val_count = val_len / val_bytes; |
| 2393 | unsigned int v; |
| 2394 | int ret, i; |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 2395 | |
Andrey Smirnov | d2a5884 | 2013-01-27 10:49:05 -0800 | [diff] [blame] | 2396 | if (!map->bus) |
| 2397 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 2398 | if (val_len % map->format.val_bytes) |
| 2399 | return -EINVAL; |
Xiubo Li | fcac023 | 2015-12-16 17:45:32 +0800 | [diff] [blame] | 2400 | if (!IS_ALIGNED(reg, map->reg_stride)) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 2401 | return -EINVAL; |
Mark Brown | fa3eec7 | 2015-07-01 23:51:43 +0100 | [diff] [blame] | 2402 | if (val_count == 0) |
| 2403 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 2404 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2405 | map->lock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2406 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 2407 | if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || |
| 2408 | map->cache_type == REGCACHE_NONE) { |
Markus Pargmann | 9a16ea9 | 2015-08-20 11:12:35 +0200 | [diff] [blame] | 2409 | if (!map->bus->read) { |
| 2410 | ret = -ENOTSUPP; |
| 2411 | goto out; |
| 2412 | } |
Markus Pargmann | c335931 | 2015-08-30 09:33:55 +0200 | [diff] [blame] | 2413 | if (map->max_raw_read && map->max_raw_read < val_len) { |
| 2414 | ret = -E2BIG; |
| 2415 | goto out; |
| 2416 | } |
Markus Pargmann | 9a16ea9 | 2015-08-20 11:12:35 +0200 | [diff] [blame] | 2417 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 2418 | /* Physical block read if there's no cache involved */ |
| 2419 | ret = _regmap_raw_read(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2420 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 2421 | } else { |
| 2422 | /* Otherwise go word by word for the cache; should be low |
| 2423 | * cost as we expect to hit the cache. |
| 2424 | */ |
| 2425 | for (i = 0; i < val_count; i++) { |
Xiubo Li | ca747be | 2016-01-04 18:00:33 +0800 | [diff] [blame^] | 2426 | ret = _regmap_read(map, reg + regmap_get_offset(map, i), |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 2427 | &v); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 2428 | if (ret != 0) |
| 2429 | goto out; |
| 2430 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 2431 | map->format.format_val(val + (i * val_bytes), v, 0); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | out: |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2436 | map->unlock(map->lock_arg); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2437 | |
| 2438 | return ret; |
| 2439 | } |
| 2440 | EXPORT_SYMBOL_GPL(regmap_raw_read); |
| 2441 | |
| 2442 | /** |
Srinivas Kandagatla | 6725228 | 2013-06-11 13:18:15 +0100 | [diff] [blame] | 2443 | * regmap_field_read(): Read a value to a single register field |
| 2444 | * |
| 2445 | * @field: Register field to read from |
| 2446 | * @val: Pointer to store read value |
| 2447 | * |
| 2448 | * A value of zero will be returned on success, a negative errno will |
| 2449 | * be returned in error cases. |
| 2450 | */ |
| 2451 | int regmap_field_read(struct regmap_field *field, unsigned int *val) |
| 2452 | { |
| 2453 | int ret; |
| 2454 | unsigned int reg_val; |
| 2455 | ret = regmap_read(field->regmap, field->reg, ®_val); |
| 2456 | if (ret != 0) |
| 2457 | return ret; |
| 2458 | |
| 2459 | reg_val &= field->mask; |
| 2460 | reg_val >>= field->shift; |
| 2461 | *val = reg_val; |
| 2462 | |
| 2463 | return ret; |
| 2464 | } |
| 2465 | EXPORT_SYMBOL_GPL(regmap_field_read); |
| 2466 | |
| 2467 | /** |
Kuninori Morimoto | a010237 | 2013-09-01 20:30:50 -0700 | [diff] [blame] | 2468 | * regmap_fields_read(): Read a value to a single register field with port ID |
| 2469 | * |
| 2470 | * @field: Register field to read from |
| 2471 | * @id: port ID |
| 2472 | * @val: Pointer to store read value |
| 2473 | * |
| 2474 | * A value of zero will be returned on success, a negative errno will |
| 2475 | * be returned in error cases. |
| 2476 | */ |
| 2477 | int regmap_fields_read(struct regmap_field *field, unsigned int id, |
| 2478 | unsigned int *val) |
| 2479 | { |
| 2480 | int ret; |
| 2481 | unsigned int reg_val; |
| 2482 | |
| 2483 | if (id >= field->id_size) |
| 2484 | return -EINVAL; |
| 2485 | |
| 2486 | ret = regmap_read(field->regmap, |
| 2487 | field->reg + (field->id_offset * id), |
| 2488 | ®_val); |
| 2489 | if (ret != 0) |
| 2490 | return ret; |
| 2491 | |
| 2492 | reg_val &= field->mask; |
| 2493 | reg_val >>= field->shift; |
| 2494 | *val = reg_val; |
| 2495 | |
| 2496 | return ret; |
| 2497 | } |
| 2498 | EXPORT_SYMBOL_GPL(regmap_fields_read); |
| 2499 | |
| 2500 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2501 | * regmap_bulk_read(): Read multiple registers from the device |
| 2502 | * |
Gerhard Sittig | 0093380 | 2013-11-11 10:42:36 +0100 | [diff] [blame] | 2503 | * @map: Register map to read from |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2504 | * @reg: First register to be read from |
| 2505 | * @val: Pointer to store read value, in native register size for device |
| 2506 | * @val_count: Number of registers to read |
| 2507 | * |
| 2508 | * A value of zero will be returned on success, a negative errno will |
| 2509 | * be returned in error cases. |
| 2510 | */ |
| 2511 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, |
| 2512 | size_t val_count) |
| 2513 | { |
| 2514 | int ret, i; |
| 2515 | size_t val_bytes = map->format.val_bytes; |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 2516 | bool vol = regmap_volatile_range(map, reg, val_count); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 2517 | |
Xiubo Li | fcac023 | 2015-12-16 17:45:32 +0800 | [diff] [blame] | 2518 | if (!IS_ALIGNED(reg, map->reg_stride)) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 2519 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2520 | |
Stephen Boyd | 3b58ee1 | 2013-12-13 09:14:07 -0800 | [diff] [blame] | 2521 | if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) { |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 2522 | /* |
| 2523 | * Some devices does not support bulk read, for |
| 2524 | * them we have a series of single read operations. |
| 2525 | */ |
Markus Pargmann | adaac45 | 2015-08-30 09:33:53 +0200 | [diff] [blame] | 2526 | size_t total_size = val_bytes * val_count; |
| 2527 | |
| 2528 | if (!map->use_single_read && |
| 2529 | (!map->max_raw_read || map->max_raw_read > total_size)) { |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 2530 | ret = regmap_raw_read(map, reg, val, |
| 2531 | val_bytes * val_count); |
| 2532 | if (ret != 0) |
| 2533 | return ret; |
Markus Pargmann | adaac45 | 2015-08-30 09:33:53 +0200 | [diff] [blame] | 2534 | } else { |
| 2535 | /* |
| 2536 | * Some devices do not support bulk read or do not |
| 2537 | * support large bulk reads, for them we have a series |
| 2538 | * of read operations. |
| 2539 | */ |
| 2540 | int chunk_stride = map->reg_stride; |
| 2541 | size_t chunk_size = val_bytes; |
| 2542 | size_t chunk_count = val_count; |
| 2543 | |
| 2544 | if (!map->use_single_read) { |
| 2545 | chunk_size = map->max_raw_read; |
| 2546 | if (chunk_size % val_bytes) |
| 2547 | chunk_size -= chunk_size % val_bytes; |
| 2548 | chunk_count = total_size / chunk_size; |
| 2549 | chunk_stride *= chunk_size / val_bytes; |
| 2550 | } |
| 2551 | |
| 2552 | /* Read bytes that fit into a multiple of chunk_size */ |
| 2553 | for (i = 0; i < chunk_count; i++) { |
| 2554 | ret = regmap_raw_read(map, |
| 2555 | reg + (i * chunk_stride), |
| 2556 | val + (i * chunk_size), |
| 2557 | chunk_size); |
| 2558 | if (ret != 0) |
| 2559 | return ret; |
| 2560 | } |
| 2561 | |
| 2562 | /* Read remaining bytes */ |
| 2563 | if (chunk_size * i < total_size) { |
| 2564 | ret = regmap_raw_read(map, |
| 2565 | reg + (i * chunk_stride), |
| 2566 | val + (i * chunk_size), |
| 2567 | total_size - i * chunk_size); |
| 2568 | if (ret != 0) |
| 2569 | return ret; |
| 2570 | } |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 2571 | } |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 2572 | |
| 2573 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
Mark Brown | 8a819ff | 2013-03-04 09:04:51 +0800 | [diff] [blame] | 2574 | map->format.parse_inplace(val + i); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 2575 | } else { |
| 2576 | for (i = 0; i < val_count; i++) { |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 2577 | unsigned int ival; |
Xiubo Li | ca747be | 2016-01-04 18:00:33 +0800 | [diff] [blame^] | 2578 | ret = regmap_read(map, reg + regmap_get_offset(map, i), |
Mark Brown | 25061d2 | 2012-05-12 13:06:08 +0100 | [diff] [blame] | 2579 | &ival); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 2580 | if (ret != 0) |
| 2581 | return ret; |
Mark Brown | d5b98eb | 2015-08-28 20:04:53 +0100 | [diff] [blame] | 2582 | |
| 2583 | if (map->format.format_val) { |
| 2584 | map->format.format_val(val + (i * val_bytes), ival, 0); |
| 2585 | } else { |
| 2586 | /* Devices providing read and write |
| 2587 | * operations can use the bulk I/O |
| 2588 | * functions if they define a val_bytes, |
| 2589 | * we assume that the values are native |
| 2590 | * endian. |
| 2591 | */ |
Xiubo Li | 19c0478 | 2015-12-09 17:11:53 +0800 | [diff] [blame] | 2592 | #ifdef CONFIG_64BIT |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 2593 | u64 *u64 = val; |
Xiubo Li | 19c0478 | 2015-12-09 17:11:53 +0800 | [diff] [blame] | 2594 | #endif |
Mark Brown | d5b98eb | 2015-08-28 20:04:53 +0100 | [diff] [blame] | 2595 | u32 *u32 = val; |
| 2596 | u16 *u16 = val; |
| 2597 | u8 *u8 = val; |
| 2598 | |
| 2599 | switch (map->format.val_bytes) { |
Xiubo Li | afcc00b | 2015-12-03 17:31:52 +0800 | [diff] [blame] | 2600 | #ifdef CONFIG_64BIT |
| 2601 | case 8: |
| 2602 | u64[i] = ival; |
| 2603 | break; |
| 2604 | #endif |
Mark Brown | d5b98eb | 2015-08-28 20:04:53 +0100 | [diff] [blame] | 2605 | case 4: |
| 2606 | u32[i] = ival; |
| 2607 | break; |
| 2608 | case 2: |
| 2609 | u16[i] = ival; |
| 2610 | break; |
| 2611 | case 1: |
| 2612 | u8[i] = ival; |
| 2613 | break; |
| 2614 | default: |
| 2615 | return -EINVAL; |
| 2616 | } |
| 2617 | } |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 2618 | } |
| 2619 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2620 | |
| 2621 | return 0; |
| 2622 | } |
| 2623 | EXPORT_SYMBOL_GPL(regmap_bulk_read); |
| 2624 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2625 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 2626 | unsigned int mask, unsigned int val, |
Kuninori Morimoto | 7ff0589 | 2015-06-16 08:52:22 +0000 | [diff] [blame] | 2627 | bool *change, bool force_write) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2628 | { |
| 2629 | int ret; |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 2630 | unsigned int tmp, orig; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2631 | |
Jon Ringle | 77792b1 | 2015-10-01 12:38:07 -0400 | [diff] [blame] | 2632 | if (change) |
| 2633 | *change = false; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2634 | |
Jon Ringle | 77792b1 | 2015-10-01 12:38:07 -0400 | [diff] [blame] | 2635 | if (regmap_volatile(map, reg) && map->reg_update_bits) { |
| 2636 | ret = map->reg_update_bits(map->bus_context, reg, mask, val); |
| 2637 | if (ret == 0 && change) |
Xiubo Li | e2f74dc | 2014-02-20 08:50:10 +0800 | [diff] [blame] | 2638 | *change = true; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2639 | } else { |
Jon Ringle | 77792b1 | 2015-10-01 12:38:07 -0400 | [diff] [blame] | 2640 | ret = _regmap_read(map, reg, &orig); |
| 2641 | if (ret != 0) |
| 2642 | return ret; |
| 2643 | |
| 2644 | tmp = orig & ~mask; |
| 2645 | tmp |= val & mask; |
| 2646 | |
| 2647 | if (force_write || (tmp != orig)) { |
| 2648 | ret = _regmap_write(map, reg, tmp); |
| 2649 | if (ret == 0 && change) |
| 2650 | *change = true; |
| 2651 | } |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2652 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2653 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2654 | return ret; |
| 2655 | } |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2656 | |
| 2657 | /** |
| 2658 | * regmap_update_bits: Perform a read/modify/write cycle on the register map |
| 2659 | * |
| 2660 | * @map: Register map to update |
| 2661 | * @reg: Register to update |
| 2662 | * @mask: Bitmask to change |
| 2663 | * @val: New value for bitmask |
| 2664 | * |
| 2665 | * Returns zero for success, a negative number on error. |
| 2666 | */ |
| 2667 | int regmap_update_bits(struct regmap *map, unsigned int reg, |
| 2668 | unsigned int mask, unsigned int val) |
| 2669 | { |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2670 | int ret; |
| 2671 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2672 | map->lock(map->lock_arg); |
Kuninori Morimoto | 7ff0589 | 2015-06-16 08:52:22 +0000 | [diff] [blame] | 2673 | ret = _regmap_update_bits(map, reg, mask, val, NULL, false); |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2674 | map->unlock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2675 | |
| 2676 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2677 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 2678 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 2679 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2680 | /** |
Kuninori Morimoto | fd4b7286c | 2015-06-16 08:52:39 +0000 | [diff] [blame] | 2681 | * regmap_write_bits: Perform a read/modify/write cycle on the register map |
| 2682 | * |
| 2683 | * @map: Register map to update |
| 2684 | * @reg: Register to update |
| 2685 | * @mask: Bitmask to change |
| 2686 | * @val: New value for bitmask |
| 2687 | * |
| 2688 | * Returns zero for success, a negative number on error. |
| 2689 | */ |
| 2690 | int regmap_write_bits(struct regmap *map, unsigned int reg, |
| 2691 | unsigned int mask, unsigned int val) |
| 2692 | { |
| 2693 | int ret; |
| 2694 | |
| 2695 | map->lock(map->lock_arg); |
| 2696 | ret = _regmap_update_bits(map, reg, mask, val, NULL, true); |
| 2697 | map->unlock(map->lock_arg); |
| 2698 | |
| 2699 | return ret; |
| 2700 | } |
| 2701 | EXPORT_SYMBOL_GPL(regmap_write_bits); |
| 2702 | |
| 2703 | /** |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2704 | * regmap_update_bits_async: Perform a read/modify/write cycle on the register |
| 2705 | * map asynchronously |
| 2706 | * |
| 2707 | * @map: Register map to update |
| 2708 | * @reg: Register to update |
| 2709 | * @mask: Bitmask to change |
| 2710 | * @val: New value for bitmask |
| 2711 | * |
| 2712 | * With most buses the read must be done synchronously so this is most |
| 2713 | * useful for devices with a cache which do not need to interact with |
| 2714 | * the hardware to determine the current register value. |
| 2715 | * |
| 2716 | * Returns zero for success, a negative number on error. |
| 2717 | */ |
| 2718 | int regmap_update_bits_async(struct regmap *map, unsigned int reg, |
| 2719 | unsigned int mask, unsigned int val) |
| 2720 | { |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2721 | int ret; |
| 2722 | |
| 2723 | map->lock(map->lock_arg); |
| 2724 | |
| 2725 | map->async = true; |
| 2726 | |
Kuninori Morimoto | 7ff0589 | 2015-06-16 08:52:22 +0000 | [diff] [blame] | 2727 | ret = _regmap_update_bits(map, reg, mask, val, NULL, false); |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2728 | |
| 2729 | map->async = false; |
| 2730 | |
| 2731 | map->unlock(map->lock_arg); |
| 2732 | |
| 2733 | return ret; |
| 2734 | } |
| 2735 | EXPORT_SYMBOL_GPL(regmap_update_bits_async); |
| 2736 | |
| 2737 | /** |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2738 | * regmap_update_bits_check: Perform a read/modify/write cycle on the |
| 2739 | * register map and report if updated |
| 2740 | * |
| 2741 | * @map: Register map to update |
| 2742 | * @reg: Register to update |
| 2743 | * @mask: Bitmask to change |
| 2744 | * @val: New value for bitmask |
| 2745 | * @change: Boolean indicating if a write was done |
| 2746 | * |
| 2747 | * Returns zero for success, a negative number on error. |
| 2748 | */ |
| 2749 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, |
| 2750 | unsigned int mask, unsigned int val, |
| 2751 | bool *change) |
| 2752 | { |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2753 | int ret; |
| 2754 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2755 | map->lock(map->lock_arg); |
Kuninori Morimoto | 7ff0589 | 2015-06-16 08:52:22 +0000 | [diff] [blame] | 2756 | ret = _regmap_update_bits(map, reg, mask, val, change, false); |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2757 | map->unlock(map->lock_arg); |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 2758 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 2759 | } |
| 2760 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); |
| 2761 | |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2762 | /** |
| 2763 | * regmap_update_bits_check_async: Perform a read/modify/write cycle on the |
| 2764 | * register map asynchronously and report if |
| 2765 | * updated |
| 2766 | * |
| 2767 | * @map: Register map to update |
| 2768 | * @reg: Register to update |
| 2769 | * @mask: Bitmask to change |
| 2770 | * @val: New value for bitmask |
| 2771 | * @change: Boolean indicating if a write was done |
| 2772 | * |
| 2773 | * With most buses the read must be done synchronously so this is most |
| 2774 | * useful for devices with a cache which do not need to interact with |
| 2775 | * the hardware to determine the current register value. |
| 2776 | * |
| 2777 | * Returns zero for success, a negative number on error. |
| 2778 | */ |
| 2779 | int regmap_update_bits_check_async(struct regmap *map, unsigned int reg, |
| 2780 | unsigned int mask, unsigned int val, |
| 2781 | bool *change) |
| 2782 | { |
| 2783 | int ret; |
| 2784 | |
| 2785 | map->lock(map->lock_arg); |
| 2786 | |
| 2787 | map->async = true; |
| 2788 | |
Kuninori Morimoto | 7ff0589 | 2015-06-16 08:52:22 +0000 | [diff] [blame] | 2789 | ret = _regmap_update_bits(map, reg, mask, val, change, false); |
Mark Brown | 915f441 | 2013-10-09 13:30:10 +0100 | [diff] [blame] | 2790 | |
| 2791 | map->async = false; |
| 2792 | |
| 2793 | map->unlock(map->lock_arg); |
| 2794 | |
| 2795 | return ret; |
| 2796 | } |
| 2797 | EXPORT_SYMBOL_GPL(regmap_update_bits_check_async); |
| 2798 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2799 | void regmap_async_complete_cb(struct regmap_async *async, int ret) |
| 2800 | { |
| 2801 | struct regmap *map = async->map; |
| 2802 | bool wake; |
| 2803 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 2804 | trace_regmap_async_io_complete(map); |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2805 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2806 | spin_lock(&map->async_lock); |
Mark Brown | 7e09a97 | 2013-10-07 23:00:24 +0100 | [diff] [blame] | 2807 | list_move(&async->list, &map->async_free); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2808 | wake = list_empty(&map->async_list); |
| 2809 | |
| 2810 | if (ret != 0) |
| 2811 | map->async_ret = ret; |
| 2812 | |
| 2813 | spin_unlock(&map->async_lock); |
| 2814 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2815 | if (wake) |
| 2816 | wake_up(&map->async_waitq); |
| 2817 | } |
Axel Lin | f804fb5 | 2013-02-03 00:14:13 +0800 | [diff] [blame] | 2818 | EXPORT_SYMBOL_GPL(regmap_async_complete_cb); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2819 | |
| 2820 | static int regmap_async_is_done(struct regmap *map) |
| 2821 | { |
| 2822 | unsigned long flags; |
| 2823 | int ret; |
| 2824 | |
| 2825 | spin_lock_irqsave(&map->async_lock, flags); |
| 2826 | ret = list_empty(&map->async_list); |
| 2827 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 2828 | |
| 2829 | return ret; |
| 2830 | } |
| 2831 | |
| 2832 | /** |
| 2833 | * regmap_async_complete: Ensure all asynchronous I/O has completed. |
| 2834 | * |
| 2835 | * @map: Map to operate on. |
| 2836 | * |
| 2837 | * Blocks until any pending asynchronous I/O has completed. Returns |
| 2838 | * an error code for any failed I/O operations. |
| 2839 | */ |
| 2840 | int regmap_async_complete(struct regmap *map) |
| 2841 | { |
| 2842 | unsigned long flags; |
| 2843 | int ret; |
| 2844 | |
| 2845 | /* Nothing to do with no async support */ |
Daniel Mack | f2e055e | 2013-07-04 13:11:03 +0200 | [diff] [blame] | 2846 | if (!map->bus || !map->bus->async_write) |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2847 | return 0; |
| 2848 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 2849 | trace_regmap_async_complete_start(map); |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2850 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2851 | wait_event(map->async_waitq, regmap_async_is_done(map)); |
| 2852 | |
| 2853 | spin_lock_irqsave(&map->async_lock, flags); |
| 2854 | ret = map->async_ret; |
| 2855 | map->async_ret = 0; |
| 2856 | spin_unlock_irqrestore(&map->async_lock, flags); |
| 2857 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 2858 | trace_regmap_async_complete_done(map); |
Mark Brown | fe7d4cc | 2013-02-21 19:05:48 +0000 | [diff] [blame] | 2859 | |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2860 | return ret; |
| 2861 | } |
Mark Brown | f88948e | 2013-02-05 13:53:26 +0000 | [diff] [blame] | 2862 | EXPORT_SYMBOL_GPL(regmap_async_complete); |
Mark Brown | 0d509f2 | 2013-01-27 22:07:38 +0800 | [diff] [blame] | 2863 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2864 | /** |
| 2865 | * regmap_register_patch: Register and apply register updates to be applied |
| 2866 | * on device initialistion |
| 2867 | * |
| 2868 | * @map: Register map to apply updates to. |
| 2869 | * @regs: Values to update. |
| 2870 | * @num_regs: Number of entries in regs. |
| 2871 | * |
| 2872 | * Register a set of register updates to be applied to the device |
| 2873 | * whenever the device registers are synchronised with the cache and |
| 2874 | * apply them immediately. Typically this is used to apply |
| 2875 | * corrections to be applied to the device defaults on startup, such |
| 2876 | * as the updates some vendors provide to undocumented registers. |
Mark Brown | 56fb1c7 | 2014-03-18 10:53:26 +0000 | [diff] [blame] | 2877 | * |
| 2878 | * The caller must ensure that this function cannot be called |
| 2879 | * concurrently with either itself or regcache_sync(). |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2880 | */ |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 2881 | int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs, |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2882 | int num_regs) |
| 2883 | { |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 2884 | struct reg_sequence *p; |
Charles Keepax | 6bf1310 | 2014-02-25 13:45:51 +0000 | [diff] [blame] | 2885 | int ret; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2886 | bool bypass; |
| 2887 | |
Cai Zhiyong | bd60e38 | 2013-11-18 20:21:49 +0800 | [diff] [blame] | 2888 | if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n", |
| 2889 | num_regs)) |
| 2890 | return 0; |
| 2891 | |
Mark Brown | aab13eb | 2013-07-11 12:41:44 +0100 | [diff] [blame] | 2892 | p = krealloc(map->patch, |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 2893 | sizeof(struct reg_sequence) * (map->patch_regs + num_regs), |
Mark Brown | aab13eb | 2013-07-11 12:41:44 +0100 | [diff] [blame] | 2894 | GFP_KERNEL); |
| 2895 | if (p) { |
| 2896 | memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs)); |
| 2897 | map->patch = p; |
| 2898 | map->patch_regs += num_regs; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2899 | } else { |
Mark Brown | 56fb1c7 | 2014-03-18 10:53:26 +0000 | [diff] [blame] | 2900 | return -ENOMEM; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2903 | map->lock(map->lock_arg); |
| 2904 | |
| 2905 | bypass = map->cache_bypass; |
| 2906 | |
| 2907 | map->cache_bypass = true; |
| 2908 | map->async = true; |
| 2909 | |
Charles Keepax | 6bf1310 | 2014-02-25 13:45:51 +0000 | [diff] [blame] | 2910 | ret = _regmap_multi_reg_write(map, regs, num_regs); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2911 | |
Mark Brown | 1a25f26 | 2013-10-10 20:55:03 +0100 | [diff] [blame] | 2912 | map->async = false; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2913 | map->cache_bypass = bypass; |
| 2914 | |
Davide Ciminaghi | 0d4529c | 2012-10-16 15:56:59 +0200 | [diff] [blame] | 2915 | map->unlock(map->lock_arg); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2916 | |
Mark Brown | 1a25f26 | 2013-10-10 20:55:03 +0100 | [diff] [blame] | 2917 | regmap_async_complete(map); |
| 2918 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 2919 | return ret; |
| 2920 | } |
| 2921 | EXPORT_SYMBOL_GPL(regmap_register_patch); |
| 2922 | |
Mark Brown | eae4b51 | 2012-03-14 13:15:03 +0000 | [diff] [blame] | 2923 | /* |
Mark Brown | a6539c3 | 2012-02-17 14:20:14 -0800 | [diff] [blame] | 2924 | * regmap_get_val_bytes(): Report the size of a register value |
| 2925 | * |
| 2926 | * Report the size of a register value, mainly intended to for use by |
| 2927 | * generic infrastructure built on top of regmap. |
| 2928 | */ |
| 2929 | int regmap_get_val_bytes(struct regmap *map) |
| 2930 | { |
| 2931 | if (map->format.format_write) |
| 2932 | return -EINVAL; |
| 2933 | |
| 2934 | return map->format.val_bytes; |
| 2935 | } |
| 2936 | EXPORT_SYMBOL_GPL(regmap_get_val_bytes); |
| 2937 | |
Srinivas Kandagatla | 668abc7 | 2015-05-21 17:42:43 +0100 | [diff] [blame] | 2938 | /** |
| 2939 | * regmap_get_max_register(): Report the max register value |
| 2940 | * |
| 2941 | * Report the max register value, mainly intended to for use by |
| 2942 | * generic infrastructure built on top of regmap. |
| 2943 | */ |
| 2944 | int regmap_get_max_register(struct regmap *map) |
| 2945 | { |
| 2946 | return map->max_register ? map->max_register : -EINVAL; |
| 2947 | } |
| 2948 | EXPORT_SYMBOL_GPL(regmap_get_max_register); |
| 2949 | |
Srinivas Kandagatla | a2f776c | 2015-05-21 17:42:54 +0100 | [diff] [blame] | 2950 | /** |
| 2951 | * regmap_get_reg_stride(): Report the register address stride |
| 2952 | * |
| 2953 | * Report the register address stride, mainly intended to for use by |
| 2954 | * generic infrastructure built on top of regmap. |
| 2955 | */ |
| 2956 | int regmap_get_reg_stride(struct regmap *map) |
| 2957 | { |
| 2958 | return map->reg_stride; |
| 2959 | } |
| 2960 | EXPORT_SYMBOL_GPL(regmap_get_reg_stride); |
| 2961 | |
Nenghua Cao | 13ff50c | 2014-02-19 18:44:13 +0800 | [diff] [blame] | 2962 | int regmap_parse_val(struct regmap *map, const void *buf, |
| 2963 | unsigned int *val) |
| 2964 | { |
| 2965 | if (!map->format.parse_val) |
| 2966 | return -EINVAL; |
| 2967 | |
| 2968 | *val = map->format.parse_val(buf); |
| 2969 | |
| 2970 | return 0; |
| 2971 | } |
| 2972 | EXPORT_SYMBOL_GPL(regmap_parse_val); |
| 2973 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 2974 | static int __init regmap_initcall(void) |
| 2975 | { |
| 2976 | regmap_debugfs_initcall(); |
| 2977 | |
| 2978 | return 0; |
| 2979 | } |
| 2980 | postcore_initcall(regmap_initcall); |