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