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