blob: 055a9c3a3b12a375e6f98e83b016e8311d9a97a2 [file] [log] [blame]
Mark Brownb83a3132011-05-11 19:59:58 +02001/*
2 * Register map access API
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Stephen Warrenf5d6eba2012-03-09 13:17:28 -070013#include <linux/device.h>
Mark Brownb83a3132011-05-11 19:59:58 +020014#include <linux/slab.h>
Paul Gortmaker19694b52012-02-28 19:28:02 -050015#include <linux/export.h>
Mark Brownb83a3132011-05-11 19:59:58 +020016#include <linux/mutex.h>
17#include <linux/err.h>
Xiubo Lid647c192014-07-15 12:23:02 +080018#include <linux/of.h>
Krystian Garbaciak6863ca62012-06-15 11:23:56 +010019#include <linux/rbtree.h>
Stephen Warren30b2a552013-02-02 22:50:14 -070020#include <linux/sched.h>
Mark Brownb83a3132011-05-11 19:59:58 +020021
Mark Brownfb2736b2011-07-24 21:30:55 +010022#define CREATE_TRACE_POINTS
23#include <trace/events/regmap.h>
24
Mark Brown93de9122011-07-20 22:35:37 +010025#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020026
Mark Brown1044c182012-07-06 14:10:23 +010027/*
28 * Sometimes for failures during very early init the trace
29 * infrastructure isn't available early enough to be used. For this
30 * sort of problem defining LOG_DEVICE will add printks for basic
31 * register I/O on a specific device.
32 */
33#undef LOG_DEVICE
34
35static int _regmap_update_bits(struct regmap *map, unsigned int reg,
36 unsigned int mask, unsigned int val,
37 bool *change);
38
Boris BREZILLON3ac17032014-04-17 11:40:11 +020039static int _regmap_bus_reg_read(void *context, unsigned int reg,
40 unsigned int *val);
Andrey Smirnovad278402013-01-12 12:54:12 -080041static int _regmap_bus_read(void *context, unsigned int reg,
42 unsigned int *val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -080043static int _regmap_bus_formatted_write(void *context, unsigned int reg,
44 unsigned int val);
Boris BREZILLON3ac17032014-04-17 11:40:11 +020045static int _regmap_bus_reg_write(void *context, unsigned int reg,
46 unsigned int val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -080047static int _regmap_bus_raw_write(void *context, unsigned int reg,
48 unsigned int val);
Andrey Smirnovad278402013-01-12 12:54:12 -080049
Davide Ciminaghi76aad392012-11-20 15:20:30 +010050bool regmap_reg_in_ranges(unsigned int reg,
51 const struct regmap_range *ranges,
52 unsigned int nranges)
53{
54 const struct regmap_range *r;
55 int i;
56
57 for (i = 0, r = ranges; i < nranges; i++, r++)
58 if (regmap_reg_in_range(reg, r))
59 return true;
60 return false;
61}
62EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
63
Mark Brown154881e2013-05-08 13:55:23 +010064bool regmap_check_range_table(struct regmap *map, unsigned int reg,
65 const struct regmap_access_table *table)
Davide Ciminaghi76aad392012-11-20 15:20:30 +010066{
67 /* Check "no ranges" first */
68 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
69 return false;
70
71 /* In case zero "yes ranges" are supplied, any reg is OK */
72 if (!table->n_yes_ranges)
73 return true;
74
75 return regmap_reg_in_ranges(reg, table->yes_ranges,
76 table->n_yes_ranges);
77}
Mark Brown154881e2013-05-08 13:55:23 +010078EXPORT_SYMBOL_GPL(regmap_check_range_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +010079
Mark Brown8de2f082011-08-10 17:14:41 +090080bool regmap_writeable(struct regmap *map, unsigned int reg)
81{
82 if (map->max_register && reg > map->max_register)
83 return false;
84
85 if (map->writeable_reg)
86 return map->writeable_reg(map->dev, reg);
87
Davide Ciminaghi76aad392012-11-20 15:20:30 +010088 if (map->wr_table)
Mark Brown154881e2013-05-08 13:55:23 +010089 return regmap_check_range_table(map, reg, map->wr_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +010090
Mark Brown8de2f082011-08-10 17:14:41 +090091 return true;
92}
93
94bool regmap_readable(struct regmap *map, unsigned int reg)
95{
96 if (map->max_register && reg > map->max_register)
97 return false;
98
Wolfram Sang4191f192012-01-30 15:08:16 +010099 if (map->format.format_write)
100 return false;
101
Mark Brown8de2f082011-08-10 17:14:41 +0900102 if (map->readable_reg)
103 return map->readable_reg(map->dev, reg);
104
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100105 if (map->rd_table)
Mark Brown154881e2013-05-08 13:55:23 +0100106 return regmap_check_range_table(map, reg, map->rd_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100107
Mark Brown8de2f082011-08-10 17:14:41 +0900108 return true;
109}
110
111bool regmap_volatile(struct regmap *map, unsigned int reg)
112{
Wolfram Sang4191f192012-01-30 15:08:16 +0100113 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +0900114 return false;
115
116 if (map->volatile_reg)
117 return map->volatile_reg(map->dev, reg);
118
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100119 if (map->volatile_table)
Mark Brown154881e2013-05-08 13:55:23 +0100120 return regmap_check_range_table(map, reg, map->volatile_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100121
Mark Brownb92be6f2013-06-03 17:24:08 +0100122 if (map->cache_ops)
123 return false;
124 else
125 return true;
Mark Brown8de2f082011-08-10 17:14:41 +0900126}
127
128bool regmap_precious(struct regmap *map, unsigned int reg)
129{
Wolfram Sang4191f192012-01-30 15:08:16 +0100130 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +0900131 return false;
132
133 if (map->precious_reg)
134 return map->precious_reg(map->dev, reg);
135
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100136 if (map->precious_table)
Mark Brown154881e2013-05-08 13:55:23 +0100137 return regmap_check_range_table(map, reg, map->precious_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100138
Mark Brown8de2f082011-08-10 17:14:41 +0900139 return false;
140}
141
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100142static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
Paul Bollea8f28cf2012-10-08 22:06:30 +0200143 size_t num)
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100144{
145 unsigned int i;
146
147 for (i = 0; i < num; i++)
148 if (!regmap_volatile(map, reg + i))
149 return false;
150
151 return true;
152}
153
Wolfram Sang9aa50752012-01-27 16:10:22 +0100154static void regmap_format_2_6_write(struct regmap *map,
155 unsigned int reg, unsigned int val)
156{
157 u8 *out = map->work_buf;
158
159 *out = (reg << 6) | val;
160}
161
Mark Brownb83a3132011-05-11 19:59:58 +0200162static void regmap_format_4_12_write(struct regmap *map,
163 unsigned int reg, unsigned int val)
164{
165 __be16 *out = map->work_buf;
166 *out = cpu_to_be16((reg << 12) | val);
167}
168
169static void regmap_format_7_9_write(struct regmap *map,
170 unsigned int reg, unsigned int val)
171{
172 __be16 *out = map->work_buf;
173 *out = cpu_to_be16((reg << 9) | val);
174}
175
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100176static void regmap_format_10_14_write(struct regmap *map,
177 unsigned int reg, unsigned int val)
178{
179 u8 *out = map->work_buf;
180
181 out[2] = val;
182 out[1] = (val >> 8) | (reg << 6);
183 out[0] = reg >> 2;
184}
185
Marc Reillyd939fb92012-03-16 12:11:43 +1100186static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200187{
188 u8 *b = buf;
189
Marc Reillyd939fb92012-03-16 12:11:43 +1100190 b[0] = val << shift;
Mark Brownb83a3132011-05-11 19:59:58 +0200191}
192
Stephen Warren141eba22012-05-24 10:47:26 -0600193static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200194{
195 __be16 *b = buf;
196
Marc Reillyd939fb92012-03-16 12:11:43 +1100197 b[0] = cpu_to_be16(val << shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200198}
199
Xiubo Li4aa8c062014-04-02 18:09:07 +0800200static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
201{
202 __le16 *b = buf;
203
204 b[0] = cpu_to_le16(val << shift);
205}
206
Stephen Warren141eba22012-05-24 10:47:26 -0600207static void regmap_format_16_native(void *buf, unsigned int val,
208 unsigned int shift)
209{
210 *(u16 *)buf = val << shift;
211}
212
Marc Reillyd939fb92012-03-16 12:11:43 +1100213static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
Marc Reillyea279fc2012-03-16 12:11:42 +1100214{
215 u8 *b = buf;
216
Marc Reillyd939fb92012-03-16 12:11:43 +1100217 val <<= shift;
218
Marc Reillyea279fc2012-03-16 12:11:42 +1100219 b[0] = val >> 16;
220 b[1] = val >> 8;
221 b[2] = val;
222}
223
Stephen Warren141eba22012-05-24 10:47:26 -0600224static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
Mark Brown7d5e5252012-02-17 15:58:25 -0800225{
226 __be32 *b = buf;
227
Marc Reillyd939fb92012-03-16 12:11:43 +1100228 b[0] = cpu_to_be32(val << shift);
Mark Brown7d5e5252012-02-17 15:58:25 -0800229}
230
Xiubo Li4aa8c062014-04-02 18:09:07 +0800231static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
232{
233 __le32 *b = buf;
234
235 b[0] = cpu_to_le32(val << shift);
236}
237
Stephen Warren141eba22012-05-24 10:47:26 -0600238static void regmap_format_32_native(void *buf, unsigned int val,
239 unsigned int shift)
240{
241 *(u32 *)buf = val << shift;
242}
243
Mark Brown8a819ff2013-03-04 09:04:51 +0800244static void regmap_parse_inplace_noop(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200245{
Mark Brown8a819ff2013-03-04 09:04:51 +0800246}
247
248static unsigned int regmap_parse_8(const void *buf)
249{
250 const u8 *b = buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200251
252 return b[0];
253}
254
Mark Brown8a819ff2013-03-04 09:04:51 +0800255static unsigned int regmap_parse_16_be(const void *buf)
256{
257 const __be16 *b = buf;
258
259 return be16_to_cpu(b[0]);
260}
261
Xiubo Li4aa8c062014-04-02 18:09:07 +0800262static unsigned int regmap_parse_16_le(const void *buf)
263{
264 const __le16 *b = buf;
265
266 return le16_to_cpu(b[0]);
267}
268
Mark Brown8a819ff2013-03-04 09:04:51 +0800269static void regmap_parse_16_be_inplace(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200270{
271 __be16 *b = buf;
272
273 b[0] = be16_to_cpu(b[0]);
Mark Brownb83a3132011-05-11 19:59:58 +0200274}
275
Xiubo Li4aa8c062014-04-02 18:09:07 +0800276static void regmap_parse_16_le_inplace(void *buf)
277{
278 __le16 *b = buf;
279
280 b[0] = le16_to_cpu(b[0]);
281}
282
Mark Brown8a819ff2013-03-04 09:04:51 +0800283static unsigned int regmap_parse_16_native(const void *buf)
Stephen Warren141eba22012-05-24 10:47:26 -0600284{
285 return *(u16 *)buf;
286}
287
Mark Brown8a819ff2013-03-04 09:04:51 +0800288static unsigned int regmap_parse_24(const void *buf)
Marc Reillyea279fc2012-03-16 12:11:42 +1100289{
Mark Brown8a819ff2013-03-04 09:04:51 +0800290 const u8 *b = buf;
Marc Reillyea279fc2012-03-16 12:11:42 +1100291 unsigned int ret = b[2];
292 ret |= ((unsigned int)b[1]) << 8;
293 ret |= ((unsigned int)b[0]) << 16;
294
295 return ret;
296}
297
Mark Brown8a819ff2013-03-04 09:04:51 +0800298static unsigned int regmap_parse_32_be(const void *buf)
299{
300 const __be32 *b = buf;
301
302 return be32_to_cpu(b[0]);
303}
304
Xiubo Li4aa8c062014-04-02 18:09:07 +0800305static unsigned int regmap_parse_32_le(const void *buf)
306{
307 const __le32 *b = buf;
308
309 return le32_to_cpu(b[0]);
310}
311
Mark Brown8a819ff2013-03-04 09:04:51 +0800312static void regmap_parse_32_be_inplace(void *buf)
Mark Brown7d5e5252012-02-17 15:58:25 -0800313{
314 __be32 *b = buf;
315
316 b[0] = be32_to_cpu(b[0]);
Mark Brown7d5e5252012-02-17 15:58:25 -0800317}
318
Xiubo Li4aa8c062014-04-02 18:09:07 +0800319static void regmap_parse_32_le_inplace(void *buf)
320{
321 __le32 *b = buf;
322
323 b[0] = le32_to_cpu(b[0]);
324}
325
Mark Brown8a819ff2013-03-04 09:04:51 +0800326static unsigned int regmap_parse_32_native(const void *buf)
Stephen Warren141eba22012-05-24 10:47:26 -0600327{
328 return *(u32 *)buf;
329}
330
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200331static void regmap_lock_mutex(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600332{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200333 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600334 mutex_lock(&map->mutex);
335}
336
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200337static void regmap_unlock_mutex(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600338{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200339 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600340 mutex_unlock(&map->mutex);
341}
342
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200343static void regmap_lock_spinlock(void *__map)
Fabio Estevamb4519c72013-07-16 02:10:11 -0300344__acquires(&map->spinlock)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600345{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200346 struct regmap *map = __map;
Lars-Peter Clausen92ab1aa2013-05-24 10:29:22 +0200347 unsigned long flags;
348
349 spin_lock_irqsave(&map->spinlock, flags);
350 map->spinlock_flags = flags;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600351}
352
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200353static void regmap_unlock_spinlock(void *__map)
Fabio Estevamb4519c72013-07-16 02:10:11 -0300354__releases(&map->spinlock)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600355{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200356 struct regmap *map = __map;
Lars-Peter Clausen92ab1aa2013-05-24 10:29:22 +0200357 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600358}
359
Mark Brown72b39f62012-05-08 17:44:40 +0100360static void dev_get_regmap_release(struct device *dev, void *res)
361{
362 /*
363 * We don't actually have anything to do here; the goal here
364 * is not to manage the regmap but to provide a simple way to
365 * get the regmap back given a struct device.
366 */
367}
368
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100369static bool _regmap_range_add(struct regmap *map,
370 struct regmap_range_node *data)
371{
372 struct rb_root *root = &map->range_tree;
373 struct rb_node **new = &(root->rb_node), *parent = NULL;
374
375 while (*new) {
376 struct regmap_range_node *this =
377 container_of(*new, struct regmap_range_node, node);
378
379 parent = *new;
380 if (data->range_max < this->range_min)
381 new = &((*new)->rb_left);
382 else if (data->range_min > this->range_max)
383 new = &((*new)->rb_right);
384 else
385 return false;
386 }
387
388 rb_link_node(&data->node, parent, new);
389 rb_insert_color(&data->node, root);
390
391 return true;
392}
393
394static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
395 unsigned int reg)
396{
397 struct rb_node *node = map->range_tree.rb_node;
398
399 while (node) {
400 struct regmap_range_node *this =
401 container_of(node, struct regmap_range_node, node);
402
403 if (reg < this->range_min)
404 node = node->rb_left;
405 else if (reg > this->range_max)
406 node = node->rb_right;
407 else
408 return this;
409 }
410
411 return NULL;
412}
413
414static void regmap_range_exit(struct regmap *map)
415{
416 struct rb_node *next;
417 struct regmap_range_node *range_node;
418
419 next = rb_first(&map->range_tree);
420 while (next) {
421 range_node = rb_entry(next, struct regmap_range_node, node);
422 next = rb_next(&range_node->node);
423 rb_erase(&range_node->node, &map->range_tree);
424 kfree(range_node);
425 }
426
427 kfree(map->selector_work_buf);
428}
429
Michal Simek6cfec042014-02-10 16:22:33 +0100430int regmap_attach_dev(struct device *dev, struct regmap *map,
431 const struct regmap_config *config)
432{
433 struct regmap **m;
434
435 map->dev = dev;
436
437 regmap_debugfs_init(map, config->name);
438
439 /* Add a devres resource for dev_get_regmap() */
440 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
441 if (!m) {
442 regmap_debugfs_exit(map);
443 return -ENOMEM;
444 }
445 *m = map;
446 devres_add(dev, m);
447
448 return 0;
449}
450EXPORT_SYMBOL_GPL(regmap_attach_dev);
451
Xiubo Lid647c192014-07-15 12:23:02 +0800452enum regmap_endian_type {
453 REGMAP_ENDIAN_REG,
454 REGMAP_ENDIAN_VAL,
455};
456
457static int of_regmap_get_endian(struct device *dev,
458 const struct regmap_bus *bus,
459 const struct regmap_config *config,
460 enum regmap_endian_type type,
461 enum regmap_endian *endian)
462{
463 struct device_node *np = dev->of_node;
464
465 if (!endian || !config)
466 return -EINVAL;
467
468 /*
469 * Firstly, try to parse the endianness from driver's config,
470 * this is to be compatible with the none DT or the old drivers.
471 * From the driver's config the endianness value maybe:
472 * REGMAP_ENDIAN_BIG,
473 * REGMAP_ENDIAN_LITTLE,
474 * REGMAP_ENDIAN_NATIVE,
475 * REGMAP_ENDIAN_DEFAULT.
476 */
477 switch (type) {
478 case REGMAP_ENDIAN_REG:
479 *endian = config->reg_format_endian;
480 break;
481 case REGMAP_ENDIAN_VAL:
482 *endian = config->val_format_endian;
483 break;
484 default:
485 return -EINVAL;
486 }
487
488 /*
489 * If the endianness parsed from driver config is
490 * REGMAP_ENDIAN_DEFAULT, that means maybe we are using the DT
491 * node to specify the endianness information.
492 */
493 if (*endian != REGMAP_ENDIAN_DEFAULT)
494 return 0;
495
496 /*
497 * Secondly, try to parse the endianness from DT node if the
498 * driver config does not specify it.
499 * From the DT node the endianness value maybe:
500 * REGMAP_ENDIAN_BIG,
501 * REGMAP_ENDIAN_LITTLE,
Xiubo Lid647c192014-07-15 12:23:02 +0800502 */
503 switch (type) {
504 case REGMAP_ENDIAN_VAL:
505 if (of_property_read_bool(np, "big-endian"))
506 *endian = REGMAP_ENDIAN_BIG;
507 else if (of_property_read_bool(np, "little-endian"))
508 *endian = REGMAP_ENDIAN_LITTLE;
Javier Martinez Canillasba1b53f2014-08-18 15:09:02 +0200509
510 if (*endian != REGMAP_ENDIAN_DEFAULT)
511 return 0;
512
Xiubo Lid647c192014-07-15 12:23:02 +0800513 break;
514 case REGMAP_ENDIAN_REG:
515 break;
516 default:
517 return -EINVAL;
518 }
519
520 /*
Xiubo Lid647c192014-07-15 12:23:02 +0800521 * Finally, try to parse the endianness from regmap bus config
522 * if in device's DT node the endianness property is absent.
523 */
524 switch (type) {
525 case REGMAP_ENDIAN_REG:
526 if (bus && bus->reg_format_endian_default)
527 *endian = bus->reg_format_endian_default;
528 break;
529 case REGMAP_ENDIAN_VAL:
530 if (bus && bus->val_format_endian_default)
531 *endian = bus->val_format_endian_default;
532 break;
533 default:
534 return -EINVAL;
535 }
536
537 return 0;
538}
539
Mark Brownb83a3132011-05-11 19:59:58 +0200540/**
541 * regmap_init(): Initialise register map
542 *
543 * @dev: Device that will be interacted with
544 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600545 * @bus_context: Data passed to bus-specific callbacks
Mark Brownb83a3132011-05-11 19:59:58 +0200546 * @config: Configuration for register map
547 *
548 * The return value will be an ERR_PTR() on error or a valid pointer to
549 * a struct regmap. This function should generally not be called
550 * directly, it should be called by bus-specific init functions.
551 */
552struct regmap *regmap_init(struct device *dev,
553 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600554 void *bus_context,
Mark Brownb83a3132011-05-11 19:59:58 +0200555 const struct regmap_config *config)
556{
Michal Simek6cfec042014-02-10 16:22:33 +0100557 struct regmap *map;
Mark Brownb83a3132011-05-11 19:59:58 +0200558 int ret = -EINVAL;
Stephen Warren141eba22012-05-24 10:47:26 -0600559 enum regmap_endian reg_endian, val_endian;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100560 int i, j;
Mark Brownb83a3132011-05-11 19:59:58 +0200561
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800562 if (!config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100563 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200564
565 map = kzalloc(sizeof(*map), GFP_KERNEL);
566 if (map == NULL) {
567 ret = -ENOMEM;
568 goto err;
569 }
570
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200571 if (config->lock && config->unlock) {
572 map->lock = config->lock;
573 map->unlock = config->unlock;
574 map->lock_arg = config->lock_arg;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600575 } else {
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800576 if ((bus && bus->fast_io) ||
577 config->fast_io) {
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200578 spin_lock_init(&map->spinlock);
579 map->lock = regmap_lock_spinlock;
580 map->unlock = regmap_unlock_spinlock;
581 } else {
582 mutex_init(&map->mutex);
583 map->lock = regmap_lock_mutex;
584 map->unlock = regmap_unlock_mutex;
585 }
586 map->lock_arg = map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600587 }
Wolfram Sangc212acc2012-01-28 02:16:41 +0100588 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000589 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100590 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Fabio Estevam5494a982012-05-31 21:10:30 -0300591 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
592 config->val_bits + config->pad_bits, 8);
Marc Reillyd939fb92012-03-16 12:11:43 +1100593 map->reg_shift = config->pad_bits % 8;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600594 if (config->reg_stride)
595 map->reg_stride = config->reg_stride;
596 else
597 map->reg_stride = 1;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100598 map->use_single_rw = config->use_single_rw;
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +0000599 map->can_multi_write = config->can_multi_write;
Mark Brownb83a3132011-05-11 19:59:58 +0200600 map->dev = dev;
601 map->bus = bus;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600602 map->bus_context = bus_context;
Mark Brown2e2ae662011-07-20 22:33:39 +0100603 map->max_register = config->max_register;
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100604 map->wr_table = config->wr_table;
605 map->rd_table = config->rd_table;
606 map->volatile_table = config->volatile_table;
607 map->precious_table = config->precious_table;
Mark Brown2e2ae662011-07-20 22:33:39 +0100608 map->writeable_reg = config->writeable_reg;
609 map->readable_reg = config->readable_reg;
610 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900611 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100612 map->cache_type = config->cache_type;
Mark Brown72b39f62012-05-08 17:44:40 +0100613 map->name = config->name;
Mark Brownb83a3132011-05-11 19:59:58 +0200614
Mark Brown0d509f22013-01-27 22:07:38 +0800615 spin_lock_init(&map->async_lock);
616 INIT_LIST_HEAD(&map->async_list);
Mark Brown7e09a972013-10-07 23:00:24 +0100617 INIT_LIST_HEAD(&map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +0800618 init_waitqueue_head(&map->async_waitq);
619
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200620 if (config->read_flag_mask || config->write_flag_mask) {
621 map->read_flag_mask = config->read_flag_mask;
622 map->write_flag_mask = config->write_flag_mask;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800623 } else if (bus) {
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200624 map->read_flag_mask = bus->read_flag_mask;
625 }
626
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800627 if (!bus) {
628 map->reg_read = config->reg_read;
629 map->reg_write = config->reg_write;
630
631 map->defer_caching = false;
632 goto skip_format_initialization;
Boris BREZILLON3ac17032014-04-17 11:40:11 +0200633 } else if (!bus->read || !bus->write) {
634 map->reg_read = _regmap_bus_reg_read;
635 map->reg_write = _regmap_bus_reg_write;
636
637 map->defer_caching = false;
638 goto skip_format_initialization;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800639 } else {
640 map->reg_read = _regmap_bus_read;
641 }
Andrey Smirnovad278402013-01-12 12:54:12 -0800642
Xiubo Lid647c192014-07-15 12:23:02 +0800643 ret = of_regmap_get_endian(dev, bus, config, REGMAP_ENDIAN_REG,
644 &reg_endian);
645 if (ret)
646 return ERR_PTR(ret);
Stephen Warren141eba22012-05-24 10:47:26 -0600647
Xiubo Lid647c192014-07-15 12:23:02 +0800648 ret = of_regmap_get_endian(dev, bus, config, REGMAP_ENDIAN_VAL,
649 &val_endian);
650 if (ret)
651 return ERR_PTR(ret);
Stephen Warren141eba22012-05-24 10:47:26 -0600652
Marc Reillyd939fb92012-03-16 12:11:43 +1100653 switch (config->reg_bits + map->reg_shift) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100654 case 2:
655 switch (config->val_bits) {
656 case 6:
657 map->format.format_write = regmap_format_2_6_write;
658 break;
659 default:
660 goto err_map;
661 }
662 break;
663
Mark Brownb83a3132011-05-11 19:59:58 +0200664 case 4:
665 switch (config->val_bits) {
666 case 12:
667 map->format.format_write = regmap_format_4_12_write;
668 break;
669 default:
670 goto err_map;
671 }
672 break;
673
674 case 7:
675 switch (config->val_bits) {
676 case 9:
677 map->format.format_write = regmap_format_7_9_write;
678 break;
679 default:
680 goto err_map;
681 }
682 break;
683
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100684 case 10:
685 switch (config->val_bits) {
686 case 14:
687 map->format.format_write = regmap_format_10_14_write;
688 break;
689 default:
690 goto err_map;
691 }
692 break;
693
Mark Brownb83a3132011-05-11 19:59:58 +0200694 case 8:
695 map->format.format_reg = regmap_format_8;
696 break;
697
698 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600699 switch (reg_endian) {
700 case REGMAP_ENDIAN_BIG:
701 map->format.format_reg = regmap_format_16_be;
702 break;
703 case REGMAP_ENDIAN_NATIVE:
704 map->format.format_reg = regmap_format_16_native;
705 break;
706 default:
707 goto err_map;
708 }
Mark Brownb83a3132011-05-11 19:59:58 +0200709 break;
710
Lars-Peter Clausen237019e2013-01-10 17:06:10 +0100711 case 24:
712 if (reg_endian != REGMAP_ENDIAN_BIG)
713 goto err_map;
714 map->format.format_reg = regmap_format_24;
715 break;
716
Mark Brown7d5e5252012-02-17 15:58:25 -0800717 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600718 switch (reg_endian) {
719 case REGMAP_ENDIAN_BIG:
720 map->format.format_reg = regmap_format_32_be;
721 break;
722 case REGMAP_ENDIAN_NATIVE:
723 map->format.format_reg = regmap_format_32_native;
724 break;
725 default:
726 goto err_map;
727 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800728 break;
729
Mark Brownb83a3132011-05-11 19:59:58 +0200730 default:
731 goto err_map;
732 }
733
Mark Brown8a819ff2013-03-04 09:04:51 +0800734 if (val_endian == REGMAP_ENDIAN_NATIVE)
735 map->format.parse_inplace = regmap_parse_inplace_noop;
736
Mark Brownb83a3132011-05-11 19:59:58 +0200737 switch (config->val_bits) {
738 case 8:
739 map->format.format_val = regmap_format_8;
740 map->format.parse_val = regmap_parse_8;
Mark Brown8a819ff2013-03-04 09:04:51 +0800741 map->format.parse_inplace = regmap_parse_inplace_noop;
Mark Brownb83a3132011-05-11 19:59:58 +0200742 break;
743 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600744 switch (val_endian) {
745 case REGMAP_ENDIAN_BIG:
746 map->format.format_val = regmap_format_16_be;
747 map->format.parse_val = regmap_parse_16_be;
Mark Brown8a819ff2013-03-04 09:04:51 +0800748 map->format.parse_inplace = regmap_parse_16_be_inplace;
Stephen Warren141eba22012-05-24 10:47:26 -0600749 break;
Xiubo Li4aa8c062014-04-02 18:09:07 +0800750 case REGMAP_ENDIAN_LITTLE:
751 map->format.format_val = regmap_format_16_le;
752 map->format.parse_val = regmap_parse_16_le;
753 map->format.parse_inplace = regmap_parse_16_le_inplace;
754 break;
Stephen Warren141eba22012-05-24 10:47:26 -0600755 case REGMAP_ENDIAN_NATIVE:
756 map->format.format_val = regmap_format_16_native;
757 map->format.parse_val = regmap_parse_16_native;
758 break;
759 default:
760 goto err_map;
761 }
Mark Brownb83a3132011-05-11 19:59:58 +0200762 break;
Marc Reillyea279fc2012-03-16 12:11:42 +1100763 case 24:
Stephen Warren141eba22012-05-24 10:47:26 -0600764 if (val_endian != REGMAP_ENDIAN_BIG)
765 goto err_map;
Marc Reillyea279fc2012-03-16 12:11:42 +1100766 map->format.format_val = regmap_format_24;
767 map->format.parse_val = regmap_parse_24;
768 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800769 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600770 switch (val_endian) {
771 case REGMAP_ENDIAN_BIG:
772 map->format.format_val = regmap_format_32_be;
773 map->format.parse_val = regmap_parse_32_be;
Mark Brown8a819ff2013-03-04 09:04:51 +0800774 map->format.parse_inplace = regmap_parse_32_be_inplace;
Stephen Warren141eba22012-05-24 10:47:26 -0600775 break;
Xiubo Li4aa8c062014-04-02 18:09:07 +0800776 case REGMAP_ENDIAN_LITTLE:
777 map->format.format_val = regmap_format_32_le;
778 map->format.parse_val = regmap_parse_32_le;
779 map->format.parse_inplace = regmap_parse_32_le_inplace;
780 break;
Stephen Warren141eba22012-05-24 10:47:26 -0600781 case REGMAP_ENDIAN_NATIVE:
782 map->format.format_val = regmap_format_32_native;
783 map->format.parse_val = regmap_parse_32_native;
784 break;
785 default:
786 goto err_map;
787 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800788 break;
Mark Brownb83a3132011-05-11 19:59:58 +0200789 }
790
Stephen Warren141eba22012-05-24 10:47:26 -0600791 if (map->format.format_write) {
792 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
793 (val_endian != REGMAP_ENDIAN_BIG))
794 goto err_map;
Mark Brown7a647612012-04-30 23:26:32 +0100795 map->use_single_rw = true;
Stephen Warren141eba22012-05-24 10:47:26 -0600796 }
Mark Brown7a647612012-04-30 23:26:32 +0100797
Mark Brownb83a3132011-05-11 19:59:58 +0200798 if (!map->format.format_write &&
799 !(map->format.format_reg && map->format.format_val))
800 goto err_map;
801
Mark Brown82159ba2012-01-18 10:52:25 +0000802 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200803 if (map->work_buf == NULL) {
804 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700805 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200806 }
807
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800808 if (map->format.format_write) {
809 map->defer_caching = false;
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800810 map->reg_write = _regmap_bus_formatted_write;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800811 } else if (map->format.format_val) {
812 map->defer_caching = true;
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800813 map->reg_write = _regmap_bus_raw_write;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800814 }
815
816skip_format_initialization:
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800817
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100818 map->range_tree = RB_ROOT;
Mark Browne3549cd2012-10-02 20:17:15 +0100819 for (i = 0; i < config->num_ranges; i++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100820 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
821 struct regmap_range_node *new;
822
823 /* Sanity check */
Mark Brown061adc02012-10-03 12:17:51 +0100824 if (range_cfg->range_max < range_cfg->range_min) {
825 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
826 range_cfg->range_max, range_cfg->range_min);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100827 goto err_range;
Mark Brown061adc02012-10-03 12:17:51 +0100828 }
829
830 if (range_cfg->range_max > map->max_register) {
831 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
832 range_cfg->range_max, map->max_register);
833 goto err_range;
834 }
835
836 if (range_cfg->selector_reg > map->max_register) {
837 dev_err(map->dev,
838 "Invalid range %d: selector out of map\n", i);
839 goto err_range;
840 }
841
842 if (range_cfg->window_len == 0) {
843 dev_err(map->dev, "Invalid range %d: window_len 0\n",
844 i);
845 goto err_range;
846 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100847
848 /* Make sure, that this register range has no selector
849 or data window within its boundary */
Mark Browne3549cd2012-10-02 20:17:15 +0100850 for (j = 0; j < config->num_ranges; j++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100851 unsigned sel_reg = config->ranges[j].selector_reg;
852 unsigned win_min = config->ranges[j].window_start;
853 unsigned win_max = win_min +
854 config->ranges[j].window_len - 1;
855
Philipp Zabelf161d222013-07-23 12:16:02 +0200856 /* Allow data window inside its own virtual range */
857 if (j == i)
858 continue;
859
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100860 if (range_cfg->range_min <= sel_reg &&
861 sel_reg <= range_cfg->range_max) {
Mark Brown061adc02012-10-03 12:17:51 +0100862 dev_err(map->dev,
863 "Range %d: selector for %d in window\n",
864 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100865 goto err_range;
866 }
867
868 if (!(win_max < range_cfg->range_min ||
869 win_min > range_cfg->range_max)) {
Mark Brown061adc02012-10-03 12:17:51 +0100870 dev_err(map->dev,
871 "Range %d: window for %d in window\n",
872 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100873 goto err_range;
874 }
875 }
876
877 new = kzalloc(sizeof(*new), GFP_KERNEL);
878 if (new == NULL) {
879 ret = -ENOMEM;
880 goto err_range;
881 }
882
Mark Brown4b020b32012-10-03 13:13:16 +0100883 new->map = map;
Mark Brownd058bb42012-10-03 12:40:47 +0100884 new->name = range_cfg->name;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100885 new->range_min = range_cfg->range_min;
886 new->range_max = range_cfg->range_max;
887 new->selector_reg = range_cfg->selector_reg;
888 new->selector_mask = range_cfg->selector_mask;
889 new->selector_shift = range_cfg->selector_shift;
890 new->window_start = range_cfg->window_start;
891 new->window_len = range_cfg->window_len;
892
Nenghua Cao53e87f82014-02-21 16:05:45 +0800893 if (!_regmap_range_add(map, new)) {
Mark Brown061adc02012-10-03 12:17:51 +0100894 dev_err(map->dev, "Failed to add range %d\n", i);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100895 kfree(new);
896 goto err_range;
897 }
898
899 if (map->selector_work_buf == NULL) {
900 map->selector_work_buf =
901 kzalloc(map->format.buf_size, GFP_KERNEL);
902 if (map->selector_work_buf == NULL) {
903 ret = -ENOMEM;
904 goto err_range;
905 }
906 }
907 }
Mark Brown052d2cd2011-11-21 19:05:13 +0000908
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100909 ret = regcache_init(map, config);
Mark Brown0ff3e622012-10-04 17:39:13 +0100910 if (ret != 0)
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100911 goto err_range;
912
Daeseok Youna7a037c2014-04-01 19:46:43 +0900913 if (dev) {
Michal Simek6cfec042014-02-10 16:22:33 +0100914 ret = regmap_attach_dev(dev, map, config);
915 if (ret != 0)
916 goto err_regcache;
Daeseok Youna7a037c2014-04-01 19:46:43 +0900917 }
Mark Brown72b39f62012-05-08 17:44:40 +0100918
Mark Brownb83a3132011-05-11 19:59:58 +0200919 return map;
920
Michal Simek6cfec042014-02-10 16:22:33 +0100921err_regcache:
Mark Brown72b39f62012-05-08 17:44:40 +0100922 regcache_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100923err_range:
924 regmap_range_exit(map);
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100925 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200926err_map:
927 kfree(map);
928err:
929 return ERR_PTR(ret);
930}
931EXPORT_SYMBOL_GPL(regmap_init);
932
Mark Brownc0eb4672012-01-30 19:56:52 +0000933static void devm_regmap_release(struct device *dev, void *res)
934{
935 regmap_exit(*(struct regmap **)res);
936}
937
938/**
939 * devm_regmap_init(): Initialise managed register map
940 *
941 * @dev: Device that will be interacted with
942 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600943 * @bus_context: Data passed to bus-specific callbacks
Mark Brownc0eb4672012-01-30 19:56:52 +0000944 * @config: Configuration for register map
945 *
946 * The return value will be an ERR_PTR() on error or a valid pointer
947 * to a struct regmap. This function should generally not be called
948 * directly, it should be called by bus-specific init functions. The
949 * map will be automatically freed by the device management code.
950 */
951struct regmap *devm_regmap_init(struct device *dev,
952 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600953 void *bus_context,
Mark Brownc0eb4672012-01-30 19:56:52 +0000954 const struct regmap_config *config)
955{
956 struct regmap **ptr, *regmap;
957
958 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
959 if (!ptr)
960 return ERR_PTR(-ENOMEM);
961
Stephen Warren0135bbc2012-04-04 15:48:30 -0600962 regmap = regmap_init(dev, bus, bus_context, config);
Mark Brownc0eb4672012-01-30 19:56:52 +0000963 if (!IS_ERR(regmap)) {
964 *ptr = regmap;
965 devres_add(dev, ptr);
966 } else {
967 devres_free(ptr);
968 }
969
970 return regmap;
971}
972EXPORT_SYMBOL_GPL(devm_regmap_init);
973
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100974static void regmap_field_init(struct regmap_field *rm_field,
975 struct regmap *regmap, struct reg_field reg_field)
976{
977 int field_bits = reg_field.msb - reg_field.lsb + 1;
978 rm_field->regmap = regmap;
979 rm_field->reg = reg_field.reg;
980 rm_field->shift = reg_field.lsb;
981 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
Kuninori Morimotoa0102372013-09-01 20:30:50 -0700982 rm_field->id_size = reg_field.id_size;
983 rm_field->id_offset = reg_field.id_offset;
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100984}
985
986/**
987 * devm_regmap_field_alloc(): Allocate and initialise a register field
988 * in a register map.
989 *
990 * @dev: Device that will be interacted with
991 * @regmap: regmap bank in which this register field is located.
992 * @reg_field: Register field with in the bank.
993 *
994 * The return value will be an ERR_PTR() on error or a valid pointer
995 * to a struct regmap_field. The regmap_field will be automatically freed
996 * by the device management code.
997 */
998struct regmap_field *devm_regmap_field_alloc(struct device *dev,
999 struct regmap *regmap, struct reg_field reg_field)
1000{
1001 struct regmap_field *rm_field = devm_kzalloc(dev,
1002 sizeof(*rm_field), GFP_KERNEL);
1003 if (!rm_field)
1004 return ERR_PTR(-ENOMEM);
1005
1006 regmap_field_init(rm_field, regmap, reg_field);
1007
1008 return rm_field;
1009
1010}
1011EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1012
1013/**
1014 * devm_regmap_field_free(): Free register field allocated using
1015 * devm_regmap_field_alloc. Usally drivers need not call this function,
1016 * as the memory allocated via devm will be freed as per device-driver
1017 * life-cyle.
1018 *
1019 * @dev: Device that will be interacted with
1020 * @field: regmap field which should be freed.
1021 */
1022void devm_regmap_field_free(struct device *dev,
1023 struct regmap_field *field)
1024{
1025 devm_kfree(dev, field);
1026}
1027EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1028
1029/**
1030 * regmap_field_alloc(): Allocate and initialise a register field
1031 * in a register map.
1032 *
1033 * @regmap: regmap bank in which this register field is located.
1034 * @reg_field: Register field with in the bank.
1035 *
1036 * The return value will be an ERR_PTR() on error or a valid pointer
1037 * to a struct regmap_field. The regmap_field should be freed by the
1038 * user once its finished working with it using regmap_field_free().
1039 */
1040struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1041 struct reg_field reg_field)
1042{
1043 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1044
1045 if (!rm_field)
1046 return ERR_PTR(-ENOMEM);
1047
1048 regmap_field_init(rm_field, regmap, reg_field);
1049
1050 return rm_field;
1051}
1052EXPORT_SYMBOL_GPL(regmap_field_alloc);
1053
1054/**
1055 * regmap_field_free(): Free register field allocated using regmap_field_alloc
1056 *
1057 * @field: regmap field which should be freed.
1058 */
1059void regmap_field_free(struct regmap_field *field)
1060{
1061 kfree(field);
1062}
1063EXPORT_SYMBOL_GPL(regmap_field_free);
1064
Mark Brownb83a3132011-05-11 19:59:58 +02001065/**
Mark Brownbf315172011-12-03 17:06:20 +00001066 * regmap_reinit_cache(): Reinitialise the current register cache
1067 *
1068 * @map: Register map to operate on.
1069 * @config: New configuration. Only the cache data will be used.
1070 *
1071 * Discard any existing register cache for the map and initialize a
1072 * new cache. This can be used to restore the cache to defaults or to
1073 * update the cache configuration to reflect runtime discovery of the
1074 * hardware.
Dimitris Papastamos4d879512012-07-27 14:54:15 +01001075 *
1076 * No explicit locking is done here, the user needs to ensure that
1077 * this function will not race with other calls to regmap.
Mark Brownbf315172011-12-03 17:06:20 +00001078 */
1079int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1080{
Mark Brownbf315172011-12-03 17:06:20 +00001081 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +00001082 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +00001083
1084 map->max_register = config->max_register;
1085 map->writeable_reg = config->writeable_reg;
1086 map->readable_reg = config->readable_reg;
1087 map->volatile_reg = config->volatile_reg;
1088 map->precious_reg = config->precious_reg;
1089 map->cache_type = config->cache_type;
1090
Stephen Warrend3c242e2012-04-04 15:48:29 -06001091 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +00001092
Mark Brown421e8d22012-01-20 13:39:37 +00001093 map->cache_bypass = false;
1094 map->cache_only = false;
1095
Dimitris Papastamos4d879512012-07-27 14:54:15 +01001096 return regcache_init(map, config);
Mark Brownbf315172011-12-03 17:06:20 +00001097}
Mark Brown752a6a52012-05-14 10:00:12 +01001098EXPORT_SYMBOL_GPL(regmap_reinit_cache);
Mark Brownbf315172011-12-03 17:06:20 +00001099
1100/**
Mark Brownb83a3132011-05-11 19:59:58 +02001101 * regmap_exit(): Free a previously allocated register map
1102 */
1103void regmap_exit(struct regmap *map)
1104{
Mark Brown7e09a972013-10-07 23:00:24 +01001105 struct regmap_async *async;
1106
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001107 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +01001108 regmap_debugfs_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001109 regmap_range_exit(map);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001110 if (map->bus && map->bus->free_context)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001111 map->bus->free_context(map->bus_context);
Mark Brownb83a3132011-05-11 19:59:58 +02001112 kfree(map->work_buf);
Mark Brown7e09a972013-10-07 23:00:24 +01001113 while (!list_empty(&map->async_free)) {
1114 async = list_first_entry_or_null(&map->async_free,
1115 struct regmap_async,
1116 list);
1117 list_del(&async->list);
1118 kfree(async->work_buf);
1119 kfree(async);
1120 }
Mark Brownb83a3132011-05-11 19:59:58 +02001121 kfree(map);
1122}
1123EXPORT_SYMBOL_GPL(regmap_exit);
1124
Mark Brown72b39f62012-05-08 17:44:40 +01001125static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1126{
1127 struct regmap **r = res;
1128 if (!r || !*r) {
1129 WARN_ON(!r || !*r);
1130 return 0;
1131 }
1132
1133 /* If the user didn't specify a name match any */
1134 if (data)
1135 return (*r)->name == data;
1136 else
1137 return 1;
1138}
1139
1140/**
1141 * dev_get_regmap(): Obtain the regmap (if any) for a device
1142 *
1143 * @dev: Device to retrieve the map for
1144 * @name: Optional name for the register map, usually NULL.
1145 *
1146 * Returns the regmap for the device if one is present, or NULL. If
1147 * name is specified then it must match the name specified when
1148 * registering the device, if it is NULL then the first regmap found
1149 * will be used. Devices with multiple register maps are very rare,
1150 * generic code should normally not need to specify a name.
1151 */
1152struct regmap *dev_get_regmap(struct device *dev, const char *name)
1153{
1154 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1155 dev_get_regmap_match, (void *)name);
1156
1157 if (!r)
1158 return NULL;
1159 return *r;
1160}
1161EXPORT_SYMBOL_GPL(dev_get_regmap);
1162
Tuomas Tynkkynen8d7d3972014-07-21 18:38:47 +03001163/**
1164 * regmap_get_device(): Obtain the device from a regmap
1165 *
1166 * @map: Register map to operate on.
1167 *
1168 * Returns the underlying device that the regmap has been created for.
1169 */
1170struct device *regmap_get_device(struct regmap *map)
1171{
1172 return map->dev;
1173}
Mark Brownfa2fbe42014-07-25 18:30:31 +01001174EXPORT_SYMBOL_GPL(regmap_get_device);
Tuomas Tynkkynen8d7d3972014-07-21 18:38:47 +03001175
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001176static int _regmap_select_page(struct regmap *map, unsigned int *reg,
Mark Brown98bc7df2012-10-04 17:31:11 +01001177 struct regmap_range_node *range,
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001178 unsigned int val_num)
1179{
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001180 void *orig_work_buf;
1181 unsigned int win_offset;
1182 unsigned int win_page;
1183 bool page_chg;
1184 int ret;
1185
Mark Brown98bc7df2012-10-04 17:31:11 +01001186 win_offset = (*reg - range->range_min) % range->window_len;
1187 win_page = (*reg - range->range_min) / range->window_len;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001188
Mark Brown98bc7df2012-10-04 17:31:11 +01001189 if (val_num > 1) {
1190 /* Bulk write shouldn't cross range boundary */
1191 if (*reg + val_num - 1 > range->range_max)
1192 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001193
Mark Brown98bc7df2012-10-04 17:31:11 +01001194 /* ... or single page boundary */
1195 if (val_num > range->window_len - win_offset)
1196 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001197 }
1198
Mark Brown98bc7df2012-10-04 17:31:11 +01001199 /* It is possible to have selector register inside data window.
1200 In that case, selector register is located on every page and
1201 it needs no page switching, when accessed alone. */
1202 if (val_num > 1 ||
1203 range->window_start + win_offset != range->selector_reg) {
1204 /* Use separate work_buf during page switching */
1205 orig_work_buf = map->work_buf;
1206 map->work_buf = map->selector_work_buf;
1207
1208 ret = _regmap_update_bits(map, range->selector_reg,
1209 range->selector_mask,
1210 win_page << range->selector_shift,
1211 &page_chg);
1212
1213 map->work_buf = orig_work_buf;
1214
Mark Brown0ff3e622012-10-04 17:39:13 +01001215 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001216 return ret;
1217 }
1218
1219 *reg = range->window_start + win_offset;
1220
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001221 return 0;
1222}
1223
Mark Brown584de322013-03-13 19:19:34 +00001224int _regmap_raw_write(struct regmap *map, unsigned int reg,
Mark Brown0a819802013-10-09 12:28:52 +01001225 const void *val, size_t val_len)
Mark Brownb83a3132011-05-11 19:59:58 +02001226{
Mark Brown98bc7df2012-10-04 17:31:11 +01001227 struct regmap_range_node *range;
Mark Brown0d509f22013-01-27 22:07:38 +08001228 unsigned long flags;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001229 u8 *u8 = map->work_buf;
Mark Brown0d509f22013-01-27 22:07:38 +08001230 void *work_val = map->work_buf + map->format.reg_bytes +
1231 map->format.pad_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +02001232 void *buf;
1233 int ret = -ENOTSUPP;
1234 size_t len;
Mark Brown73304782011-07-24 11:46:20 +01001235 int i;
1236
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001237 WARN_ON(!map->bus);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001238
Mark Brown73304782011-07-24 11:46:20 +01001239 /* Check for unwritable registers before we start */
1240 if (map->writeable_reg)
1241 for (i = 0; i < val_len / map->format.val_bytes; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -06001242 if (!map->writeable_reg(map->dev,
1243 reg + (i * map->reg_stride)))
Mark Brown73304782011-07-24 11:46:20 +01001244 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001245
Laxman Dewanganc9157192012-02-10 21:30:27 +05301246 if (!map->cache_bypass && map->format.parse_val) {
1247 unsigned int ival;
1248 int val_bytes = map->format.val_bytes;
1249 for (i = 0; i < val_len / val_bytes; i++) {
Stephen Warren5a08d152013-03-20 17:02:02 -06001250 ival = map->format.parse_val(val + (i * val_bytes));
Stephen Warrenf01ee602012-04-09 13:40:24 -06001251 ret = regcache_write(map, reg + (i * map->reg_stride),
1252 ival);
Laxman Dewanganc9157192012-02-10 21:30:27 +05301253 if (ret) {
1254 dev_err(map->dev,
Mark Brown6d04b8a2012-10-26 19:05:32 +01001255 "Error in caching of register: %x ret: %d\n",
Laxman Dewanganc9157192012-02-10 21:30:27 +05301256 reg + i, ret);
1257 return ret;
1258 }
1259 }
1260 if (map->cache_only) {
1261 map->cache_dirty = true;
1262 return 0;
1263 }
1264 }
1265
Mark Brown98bc7df2012-10-04 17:31:11 +01001266 range = _regmap_range_lookup(map, reg);
1267 if (range) {
Mark Brown8a2ceac2012-10-04 18:20:18 +01001268 int val_num = val_len / map->format.val_bytes;
1269 int win_offset = (reg - range->range_min) % range->window_len;
1270 int win_residue = range->window_len - win_offset;
1271
1272 /* If the write goes beyond the end of the window split it */
1273 while (val_num > win_residue) {
Fabio Estevam1a61cfe2012-10-25 14:07:18 -02001274 dev_dbg(map->dev, "Writing window %d/%zu\n",
Mark Brown8a2ceac2012-10-04 18:20:18 +01001275 win_residue, val_len / map->format.val_bytes);
1276 ret = _regmap_raw_write(map, reg, val, win_residue *
Mark Brown0a819802013-10-09 12:28:52 +01001277 map->format.val_bytes);
Mark Brown8a2ceac2012-10-04 18:20:18 +01001278 if (ret != 0)
1279 return ret;
1280
1281 reg += win_residue;
1282 val_num -= win_residue;
1283 val += win_residue * map->format.val_bytes;
1284 val_len -= win_residue * map->format.val_bytes;
1285
1286 win_offset = (reg - range->range_min) %
1287 range->window_len;
1288 win_residue = range->window_len - win_offset;
1289 }
1290
1291 ret = _regmap_select_page(map, &reg, range, val_num);
Mark Brown0ff3e622012-10-04 17:39:13 +01001292 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001293 return ret;
1294 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001295
Marc Reillyd939fb92012-03-16 12:11:43 +11001296 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001297
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001298 u8[0] |= map->write_flag_mask;
1299
Mark Brown651e0132013-10-08 18:37:36 +01001300 /*
1301 * Essentially all I/O mechanisms will be faster with a single
1302 * buffer to write. Since register syncs often generate raw
1303 * writes of single registers optimise that case.
1304 */
1305 if (val != work_val && val_len == map->format.val_bytes) {
1306 memcpy(work_val, val, map->format.val_bytes);
1307 val = work_val;
1308 }
1309
Mark Brown0a819802013-10-09 12:28:52 +01001310 if (map->async && map->bus->async_write) {
Mark Brown7e09a972013-10-07 23:00:24 +01001311 struct regmap_async *async;
Mark Brown0d509f22013-01-27 22:07:38 +08001312
Mark Brownfe7d4cc2013-02-21 19:05:48 +00001313 trace_regmap_async_write_start(map->dev, reg, val_len);
1314
Mark Brown7e09a972013-10-07 23:00:24 +01001315 spin_lock_irqsave(&map->async_lock, flags);
1316 async = list_first_entry_or_null(&map->async_free,
1317 struct regmap_async,
1318 list);
1319 if (async)
1320 list_del(&async->list);
1321 spin_unlock_irqrestore(&map->async_lock, flags);
1322
1323 if (!async) {
1324 async = map->bus->async_alloc();
1325 if (!async)
1326 return -ENOMEM;
1327
1328 async->work_buf = kzalloc(map->format.buf_size,
1329 GFP_KERNEL | GFP_DMA);
1330 if (!async->work_buf) {
1331 kfree(async);
1332 return -ENOMEM;
1333 }
Mark Brown0d509f22013-01-27 22:07:38 +08001334 }
1335
Mark Brown0d509f22013-01-27 22:07:38 +08001336 async->map = map;
1337
1338 /* If the caller supplied the value we can use it safely. */
1339 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1340 map->format.reg_bytes + map->format.val_bytes);
Mark Brown0d509f22013-01-27 22:07:38 +08001341
1342 spin_lock_irqsave(&map->async_lock, flags);
1343 list_add_tail(&async->list, &map->async_list);
1344 spin_unlock_irqrestore(&map->async_lock, flags);
1345
Mark Brown04c50cc2013-10-10 22:38:29 +01001346 if (val != work_val)
1347 ret = map->bus->async_write(map->bus_context,
1348 async->work_buf,
1349 map->format.reg_bytes +
1350 map->format.pad_bytes,
1351 val, val_len, async);
1352 else
1353 ret = map->bus->async_write(map->bus_context,
1354 async->work_buf,
1355 map->format.reg_bytes +
1356 map->format.pad_bytes +
1357 val_len, NULL, 0, async);
Mark Brown0d509f22013-01-27 22:07:38 +08001358
1359 if (ret != 0) {
1360 dev_err(map->dev, "Failed to schedule write: %d\n",
1361 ret);
1362
1363 spin_lock_irqsave(&map->async_lock, flags);
Mark Brown7e09a972013-10-07 23:00:24 +01001364 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08001365 spin_unlock_irqrestore(&map->async_lock, flags);
Mark Brown0d509f22013-01-27 22:07:38 +08001366 }
Mark Brownf951b652013-03-27 13:08:44 +00001367
1368 return ret;
Mark Brown0d509f22013-01-27 22:07:38 +08001369 }
1370
Mark Brownfb2736b2011-07-24 21:30:55 +01001371 trace_regmap_hw_write_start(map->dev, reg,
1372 val_len / map->format.val_bytes);
1373
Mark Brown2547e202011-07-20 21:47:22 +01001374 /* If we're doing a single register write we can probably just
1375 * send the work_buf directly, otherwise try to do a gather
1376 * write.
1377 */
Mark Brown0d509f22013-01-27 22:07:38 +08001378 if (val == work_val)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001379 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001380 map->format.reg_bytes +
1381 map->format.pad_bytes +
1382 val_len);
Mark Brown2547e202011-07-20 21:47:22 +01001383 else if (map->bus->gather_write)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001384 ret = map->bus->gather_write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001385 map->format.reg_bytes +
1386 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +02001387 val, val_len);
1388
Mark Brown2547e202011-07-20 21:47:22 +01001389 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +02001390 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +00001391 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1392 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +02001393 if (!buf)
1394 return -ENOMEM;
1395
1396 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +00001397 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1398 val, val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -06001399 ret = map->bus->write(map->bus_context, buf, len);
Mark Brownb83a3132011-05-11 19:59:58 +02001400
1401 kfree(buf);
1402 }
1403
Mark Brownfb2736b2011-07-24 21:30:55 +01001404 trace_regmap_hw_write_done(map->dev, reg,
1405 val_len / map->format.val_bytes);
1406
Mark Brownb83a3132011-05-11 19:59:58 +02001407 return ret;
1408}
1409
Mark Brown221ad7f2013-03-26 21:24:20 +00001410/**
1411 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1412 *
1413 * @map: Map to check.
1414 */
1415bool regmap_can_raw_write(struct regmap *map)
1416{
1417 return map->bus && map->format.format_val && map->format.format_reg;
1418}
1419EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1420
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001421static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1422 unsigned int val)
1423{
1424 int ret;
1425 struct regmap_range_node *range;
1426 struct regmap *map = context;
1427
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001428 WARN_ON(!map->bus || !map->format.format_write);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001429
1430 range = _regmap_range_lookup(map, reg);
1431 if (range) {
1432 ret = _regmap_select_page(map, &reg, range, 1);
1433 if (ret != 0)
1434 return ret;
1435 }
1436
1437 map->format.format_write(map, reg, val);
1438
1439 trace_regmap_hw_write_start(map->dev, reg, 1);
1440
1441 ret = map->bus->write(map->bus_context, map->work_buf,
1442 map->format.buf_size);
1443
1444 trace_regmap_hw_write_done(map->dev, reg, 1);
1445
1446 return ret;
1447}
1448
Boris BREZILLON3ac17032014-04-17 11:40:11 +02001449static int _regmap_bus_reg_write(void *context, unsigned int reg,
1450 unsigned int val)
1451{
1452 struct regmap *map = context;
1453
1454 return map->bus->reg_write(map->bus_context, reg, val);
1455}
1456
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001457static int _regmap_bus_raw_write(void *context, unsigned int reg,
1458 unsigned int val)
1459{
1460 struct regmap *map = context;
1461
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001462 WARN_ON(!map->bus || !map->format.format_val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001463
1464 map->format.format_val(map->work_buf + map->format.reg_bytes
1465 + map->format.pad_bytes, val, 0);
1466 return _regmap_raw_write(map, reg,
1467 map->work_buf +
1468 map->format.reg_bytes +
1469 map->format.pad_bytes,
Mark Brown0a819802013-10-09 12:28:52 +01001470 map->format.val_bytes);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001471}
1472
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001473static inline void *_regmap_map_get_context(struct regmap *map)
1474{
1475 return (map->bus) ? map : map->bus_context;
1476}
1477
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +01001478int _regmap_write(struct regmap *map, unsigned int reg,
1479 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +02001480{
Mark Brownfb2736b2011-07-24 21:30:55 +01001481 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001482 void *context = _regmap_map_get_context(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001483
Ionut Nicu515f2262013-08-09 12:09:20 +02001484 if (!regmap_writeable(map, reg))
1485 return -EIO;
1486
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001487 if (!map->cache_bypass && !map->defer_caching) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001488 ret = regcache_write(map, reg, val);
1489 if (ret != 0)
1490 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001491 if (map->cache_only) {
1492 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001493 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001494 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001495 }
1496
Mark Brown1044c182012-07-06 14:10:23 +01001497#ifdef LOG_DEVICE
1498 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1499 dev_info(map->dev, "%x <= %x\n", reg, val);
1500#endif
1501
Mark Brownfb2736b2011-07-24 21:30:55 +01001502 trace_regmap_reg_write(map->dev, reg, val);
1503
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001504 return map->reg_write(context, reg, val);
Mark Brownb83a3132011-05-11 19:59:58 +02001505}
1506
1507/**
1508 * regmap_write(): Write a value to a single register
1509 *
1510 * @map: Register map to write to
1511 * @reg: Register to write to
1512 * @val: Value to be written
1513 *
1514 * A value of zero will be returned on success, a negative errno will
1515 * be returned in error cases.
1516 */
1517int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1518{
1519 int ret;
1520
Stephen Warrenf01ee602012-04-09 13:40:24 -06001521 if (reg % map->reg_stride)
1522 return -EINVAL;
1523
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001524 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001525
1526 ret = _regmap_write(map, reg, val);
1527
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001528 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001529
1530 return ret;
1531}
1532EXPORT_SYMBOL_GPL(regmap_write);
1533
1534/**
Mark Brown915f4412013-10-09 13:30:10 +01001535 * regmap_write_async(): Write a value to a single register asynchronously
1536 *
1537 * @map: Register map to write to
1538 * @reg: Register to write to
1539 * @val: Value to be written
1540 *
1541 * A value of zero will be returned on success, a negative errno will
1542 * be returned in error cases.
1543 */
1544int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1545{
1546 int ret;
1547
1548 if (reg % map->reg_stride)
1549 return -EINVAL;
1550
1551 map->lock(map->lock_arg);
1552
1553 map->async = true;
1554
1555 ret = _regmap_write(map, reg, val);
1556
1557 map->async = false;
1558
1559 map->unlock(map->lock_arg);
1560
1561 return ret;
1562}
1563EXPORT_SYMBOL_GPL(regmap_write_async);
1564
1565/**
Mark Brownb83a3132011-05-11 19:59:58 +02001566 * regmap_raw_write(): Write raw values to one or more registers
1567 *
1568 * @map: Register map to write to
1569 * @reg: Initial register to write to
1570 * @val: Block of data to be written, laid out for direct transmission to the
1571 * device
1572 * @val_len: Length of data pointed to by val.
1573 *
1574 * This function is intended to be used for things like firmware
1575 * download where a large block of data needs to be transferred to the
1576 * device. No formatting will be done on the data provided.
1577 *
1578 * A value of zero will be returned on success, a negative errno will
1579 * be returned in error cases.
1580 */
1581int regmap_raw_write(struct regmap *map, unsigned int reg,
1582 const void *val, size_t val_len)
1583{
1584 int ret;
1585
Mark Brown221ad7f2013-03-26 21:24:20 +00001586 if (!regmap_can_raw_write(map))
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001587 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001588 if (val_len % map->format.val_bytes)
1589 return -EINVAL;
1590
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001591 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001592
Mark Brown0a819802013-10-09 12:28:52 +01001593 ret = _regmap_raw_write(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001594
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001595 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001596
1597 return ret;
1598}
1599EXPORT_SYMBOL_GPL(regmap_raw_write);
1600
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001601/**
1602 * regmap_field_write(): Write a value to a single register field
1603 *
1604 * @field: Register field to write to
1605 * @val: Value to be written
1606 *
1607 * A value of zero will be returned on success, a negative errno will
1608 * be returned in error cases.
1609 */
1610int regmap_field_write(struct regmap_field *field, unsigned int val)
1611{
1612 return regmap_update_bits(field->regmap, field->reg,
1613 field->mask, val << field->shift);
1614}
1615EXPORT_SYMBOL_GPL(regmap_field_write);
1616
Kuninori Morimotofdf20022013-09-01 20:24:50 -07001617/**
1618 * regmap_field_update_bits(): Perform a read/modify/write cycle
1619 * on the register field
1620 *
1621 * @field: Register field to write to
1622 * @mask: Bitmask to change
1623 * @val: Value to be written
1624 *
1625 * A value of zero will be returned on success, a negative errno will
1626 * be returned in error cases.
1627 */
1628int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1629{
1630 mask = (mask << field->shift) & field->mask;
1631
1632 return regmap_update_bits(field->regmap, field->reg,
1633 mask, val << field->shift);
1634}
1635EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1636
Kuninori Morimotoa0102372013-09-01 20:30:50 -07001637/**
1638 * regmap_fields_write(): Write a value to a single register field with port ID
1639 *
1640 * @field: Register field to write to
1641 * @id: port ID
1642 * @val: Value to be written
1643 *
1644 * A value of zero will be returned on success, a negative errno will
1645 * be returned in error cases.
1646 */
1647int regmap_fields_write(struct regmap_field *field, unsigned int id,
1648 unsigned int val)
1649{
1650 if (id >= field->id_size)
1651 return -EINVAL;
1652
1653 return regmap_update_bits(field->regmap,
1654 field->reg + (field->id_offset * id),
1655 field->mask, val << field->shift);
1656}
1657EXPORT_SYMBOL_GPL(regmap_fields_write);
1658
1659/**
1660 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1661 * on the register field
1662 *
1663 * @field: Register field to write to
1664 * @id: port ID
1665 * @mask: Bitmask to change
1666 * @val: Value to be written
1667 *
1668 * A value of zero will be returned on success, a negative errno will
1669 * be returned in error cases.
1670 */
1671int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1672 unsigned int mask, unsigned int val)
1673{
1674 if (id >= field->id_size)
1675 return -EINVAL;
1676
1677 mask = (mask << field->shift) & field->mask;
1678
1679 return regmap_update_bits(field->regmap,
1680 field->reg + (field->id_offset * id),
1681 mask, val << field->shift);
1682}
1683EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1684
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301685/*
1686 * regmap_bulk_write(): Write multiple registers to the device
1687 *
1688 * @map: Register map to write to
1689 * @reg: First register to be write from
1690 * @val: Block of data to be written, in native register size for device
1691 * @val_count: Number of registers to write
1692 *
1693 * This function is intended to be used for writing a large block of
Nestor Ovroy31b35e9e2013-01-18 16:51:03 +01001694 * data to the device either in single transfer or multiple transfer.
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301695 *
1696 * A value of zero will be returned on success, a negative errno will
1697 * be returned in error cases.
1698 */
1699int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1700 size_t val_count)
1701{
1702 int ret = 0, i;
1703 size_t val_bytes = map->format.val_bytes;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301704
Stephen Boydf4298362013-12-26 13:52:04 -08001705 if (map->bus && !map->format.parse_inplace)
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301706 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001707 if (reg % map->reg_stride)
1708 return -EINVAL;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301709
Stephen Boydf4298362013-12-26 13:52:04 -08001710 /*
1711 * Some devices don't support bulk write, for
1712 * them we have a series of single write operations.
1713 */
1714 if (!map->bus || map->use_single_rw) {
Takashi Iwai4999e962014-03-18 12:58:33 +01001715 map->lock(map->lock_arg);
Stephen Boydf4298362013-12-26 13:52:04 -08001716 for (i = 0; i < val_count; i++) {
1717 unsigned int ival;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301718
Stephen Boydf4298362013-12-26 13:52:04 -08001719 switch (val_bytes) {
1720 case 1:
1721 ival = *(u8 *)(val + (i * val_bytes));
1722 break;
1723 case 2:
1724 ival = *(u16 *)(val + (i * val_bytes));
1725 break;
1726 case 4:
1727 ival = *(u32 *)(val + (i * val_bytes));
1728 break;
1729#ifdef CONFIG_64BIT
1730 case 8:
1731 ival = *(u64 *)(val + (i * val_bytes));
1732 break;
1733#endif
1734 default:
1735 ret = -EINVAL;
1736 goto out;
1737 }
1738
1739 ret = _regmap_write(map, reg + (i * map->reg_stride),
1740 ival);
1741 if (ret != 0)
1742 goto out;
1743 }
Takashi Iwai4999e962014-03-18 12:58:33 +01001744out:
1745 map->unlock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301746 } else {
Stephen Boydf4298362013-12-26 13:52:04 -08001747 void *wval;
1748
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301749 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1750 if (!wval) {
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301751 dev_err(map->dev, "Error in memory allocation\n");
Takashi Iwai4999e962014-03-18 12:58:33 +01001752 return -ENOMEM;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301753 }
1754 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08001755 map->format.parse_inplace(wval + i);
Stephen Boydf4298362013-12-26 13:52:04 -08001756
Takashi Iwai4999e962014-03-18 12:58:33 +01001757 map->lock(map->lock_arg);
Mark Brown0a819802013-10-09 12:28:52 +01001758 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
Takashi Iwai4999e962014-03-18 12:58:33 +01001759 map->unlock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301760
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301761 kfree(wval);
Stephen Boydf4298362013-12-26 13:52:04 -08001762 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301763 return ret;
1764}
1765EXPORT_SYMBOL_GPL(regmap_bulk_write);
1766
Anthony Oleche33fabd2013-10-11 15:31:11 +01001767/*
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001768 * _regmap_raw_multi_reg_write()
1769 *
1770 * the (register,newvalue) pairs in regs have not been formatted, but
1771 * they are all in the same page and have been changed to being page
1772 * relative. The page register has been written if that was neccessary.
1773 */
1774static int _regmap_raw_multi_reg_write(struct regmap *map,
1775 const struct reg_default *regs,
1776 size_t num_regs)
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001777{
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001778 int ret;
1779 void *buf;
1780 int i;
1781 u8 *u8;
1782 size_t val_bytes = map->format.val_bytes;
1783 size_t reg_bytes = map->format.reg_bytes;
1784 size_t pad_bytes = map->format.pad_bytes;
1785 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1786 size_t len = pair_size * num_regs;
1787
Xiubo Lif5727cd2014-04-30 17:31:08 +08001788 if (!len)
1789 return -EINVAL;
1790
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001791 buf = kzalloc(len, GFP_KERNEL);
1792 if (!buf)
1793 return -ENOMEM;
1794
1795 /* We have to linearise by hand. */
1796
1797 u8 = buf;
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001798
1799 for (i = 0; i < num_regs; i++) {
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001800 int reg = regs[i].reg;
1801 int val = regs[i].def;
1802 trace_regmap_hw_write_start(map->dev, reg, 1);
1803 map->format.format_reg(u8, reg, map->reg_shift);
1804 u8 += reg_bytes + pad_bytes;
1805 map->format.format_val(u8, val, 0);
1806 u8 += val_bytes;
1807 }
1808 u8 = buf;
1809 *u8 |= map->write_flag_mask;
1810
1811 ret = map->bus->write(map->bus_context, buf, len);
1812
1813 kfree(buf);
1814
1815 for (i = 0; i < num_regs; i++) {
1816 int reg = regs[i].reg;
1817 trace_regmap_hw_write_done(map->dev, reg, 1);
1818 }
1819 return ret;
1820}
1821
1822static unsigned int _regmap_register_page(struct regmap *map,
1823 unsigned int reg,
1824 struct regmap_range_node *range)
1825{
1826 unsigned int win_page = (reg - range->range_min) / range->window_len;
1827
1828 return win_page;
1829}
1830
1831static int _regmap_range_multi_paged_reg_write(struct regmap *map,
1832 struct reg_default *regs,
1833 size_t num_regs)
1834{
1835 int ret;
1836 int i, n;
1837 struct reg_default *base;
Geert Uytterhoevenb48d1392014-04-22 12:47:29 +02001838 unsigned int this_page = 0;
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001839 /*
1840 * the set of registers are not neccessarily in order, but
1841 * since the order of write must be preserved this algorithm
1842 * chops the set each time the page changes
1843 */
1844 base = regs;
1845 for (i = 0, n = 0; i < num_regs; i++, n++) {
1846 unsigned int reg = regs[i].reg;
1847 struct regmap_range_node *range;
1848
1849 range = _regmap_range_lookup(map, reg);
1850 if (range) {
1851 unsigned int win_page = _regmap_register_page(map, reg,
1852 range);
1853
1854 if (i == 0)
1855 this_page = win_page;
1856 if (win_page != this_page) {
1857 this_page = win_page;
1858 ret = _regmap_raw_multi_reg_write(map, base, n);
1859 if (ret != 0)
1860 return ret;
1861 base += n;
1862 n = 0;
1863 }
1864 ret = _regmap_select_page(map, &base[n].reg, range, 1);
1865 if (ret != 0)
1866 return ret;
1867 }
1868 }
1869 if (n > 0)
1870 return _regmap_raw_multi_reg_write(map, base, n);
1871 return 0;
1872}
1873
1874static int _regmap_multi_reg_write(struct regmap *map,
1875 const struct reg_default *regs,
1876 size_t num_regs)
1877{
1878 int i;
1879 int ret;
1880
1881 if (!map->can_multi_write) {
1882 for (i = 0; i < num_regs; i++) {
1883 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1884 if (ret != 0)
1885 return ret;
1886 }
1887 return 0;
1888 }
1889
1890 if (!map->format.parse_inplace)
1891 return -EINVAL;
1892
1893 if (map->writeable_reg)
1894 for (i = 0; i < num_regs; i++) {
1895 int reg = regs[i].reg;
1896 if (!map->writeable_reg(map->dev, reg))
1897 return -EINVAL;
1898 if (reg % map->reg_stride)
1899 return -EINVAL;
1900 }
1901
1902 if (!map->cache_bypass) {
1903 for (i = 0; i < num_regs; i++) {
1904 unsigned int val = regs[i].def;
1905 unsigned int reg = regs[i].reg;
1906 ret = regcache_write(map, reg, val);
1907 if (ret) {
1908 dev_err(map->dev,
1909 "Error in caching of register: %x ret: %d\n",
1910 reg, ret);
1911 return ret;
1912 }
1913 }
1914 if (map->cache_only) {
1915 map->cache_dirty = true;
1916 return 0;
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001917 }
1918 }
1919
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001920 WARN_ON(!map->bus);
1921
1922 for (i = 0; i < num_regs; i++) {
1923 unsigned int reg = regs[i].reg;
1924 struct regmap_range_node *range;
1925 range = _regmap_range_lookup(map, reg);
1926 if (range) {
1927 size_t len = sizeof(struct reg_default)*num_regs;
1928 struct reg_default *base = kmemdup(regs, len,
1929 GFP_KERNEL);
1930 if (!base)
1931 return -ENOMEM;
1932 ret = _regmap_range_multi_paged_reg_write(map, base,
1933 num_regs);
1934 kfree(base);
1935
1936 return ret;
1937 }
1938 }
1939 return _regmap_raw_multi_reg_write(map, regs, num_regs);
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001940}
1941
Anthony Oleche33fabd2013-10-11 15:31:11 +01001942/*
1943 * regmap_multi_reg_write(): Write multiple registers to the device
1944 *
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001945 * where the set of register,value pairs are supplied in any order,
1946 * possibly not all in a single range.
Anthony Oleche33fabd2013-10-11 15:31:11 +01001947 *
1948 * @map: Register map to write to
1949 * @regs: Array of structures containing register,value to be written
1950 * @num_regs: Number of registers to write
1951 *
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001952 * The 'normal' block write mode will send ultimately send data on the
1953 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
1954 * addressed. However, this alternative block multi write mode will send
1955 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
1956 * must of course support the mode.
Anthony Oleche33fabd2013-10-11 15:31:11 +01001957 *
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001958 * A value of zero will be returned on success, a negative errno will be
1959 * returned in error cases.
Anthony Oleche33fabd2013-10-11 15:31:11 +01001960 */
Charles Keepaxf7e2cec2014-02-25 13:45:49 +00001961int regmap_multi_reg_write(struct regmap *map, const struct reg_default *regs,
1962 int num_regs)
Anthony Oleche33fabd2013-10-11 15:31:11 +01001963{
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001964 int ret;
Anthony Oleche33fabd2013-10-11 15:31:11 +01001965
1966 map->lock(map->lock_arg);
1967
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001968 ret = _regmap_multi_reg_write(map, regs, num_regs);
1969
Anthony Oleche33fabd2013-10-11 15:31:11 +01001970 map->unlock(map->lock_arg);
1971
1972 return ret;
1973}
1974EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
1975
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001976/*
1977 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
1978 * device but not the cache
1979 *
Mark Brown0d509f22013-01-27 22:07:38 +08001980 * where the set of register are supplied in any order
1981 *
1982 * @map: Register map to write to
1983 * @regs: Array of structures containing register,value to be written
1984 * @num_regs: Number of registers to write
1985 *
1986 * This function is intended to be used for writing a large block of data
1987 * atomically to the device in single transfer for those I2C client devices
1988 * that implement this alternative block write mode.
1989 *
1990 * A value of zero will be returned on success, a negative errno will
1991 * be returned in error cases.
1992 */
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001993int regmap_multi_reg_write_bypassed(struct regmap *map,
1994 const struct reg_default *regs,
1995 int num_regs)
Mark Brown0d509f22013-01-27 22:07:38 +08001996{
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001997 int ret;
1998 bool bypass;
Mark Brown0d509f22013-01-27 22:07:38 +08001999
2000 map->lock(map->lock_arg);
2001
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002002 bypass = map->cache_bypass;
2003 map->cache_bypass = true;
2004
2005 ret = _regmap_multi_reg_write(map, regs, num_regs);
2006
2007 map->cache_bypass = bypass;
2008
Mark Brown0a819802013-10-09 12:28:52 +01002009 map->unlock(map->lock_arg);
2010
2011 return ret;
2012}
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002013EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
Mark Brown0d509f22013-01-27 22:07:38 +08002014
2015/**
2016 * regmap_raw_write_async(): Write raw values to one or more registers
2017 * asynchronously
2018 *
2019 * @map: Register map to write to
Mark Brownb83a3132011-05-11 19:59:58 +02002020 * @reg: Initial register to write to
2021 * @val: Block of data to be written, laid out for direct transmission to the
2022 * device. Must be valid until regmap_async_complete() is called.
Mark Brown98bc7df2012-10-04 17:31:11 +01002023 * @val_len: Length of data pointed to by val.
Mark Brownb83a3132011-05-11 19:59:58 +02002024 *
2025 * This function is intended to be used for things like firmware
2026 * download where a large block of data needs to be transferred to the
Mark Brownf1b5c5c2013-03-13 19:18:13 +00002027 * device. No formatting will be done on the data provided.
Andrey Smirnovd2a58842013-01-27 10:49:05 -08002028 *
Mark Brown98bc7df2012-10-04 17:31:11 +01002029 * If supported by the underlying bus the write will be scheduled
2030 * asynchronously, helping maximise I/O speed on higher speed buses
2031 * like SPI. regmap_async_complete() can be called to ensure that all
2032 * asynchrnous writes have been completed.
Mark Brown0ff3e622012-10-04 17:39:13 +01002033 *
Mark Brown98bc7df2012-10-04 17:31:11 +01002034 * A value of zero will be returned on success, a negative errno will
2035 * be returned in error cases.
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01002036 */
Marc Reillyd939fb92012-03-16 12:11:43 +11002037int regmap_raw_write_async(struct regmap *map, unsigned int reg,
Mark Brownb83a3132011-05-11 19:59:58 +02002038 const void *val, size_t val_len)
2039{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02002040 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +02002041
2042 if (val_len % map->format.val_bytes)
2043 return -EINVAL;
2044 if (reg % map->reg_stride)
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02002045 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02002046
Mark Brownfb2736b2011-07-24 21:30:55 +01002047 map->lock(map->lock_arg);
2048
2049 map->async = true;
Stephen Warren0135bbc2012-04-04 15:48:30 -06002050
Mark Brown82159ba2012-01-18 10:52:25 +00002051 ret = _regmap_raw_write(map, reg, val, val_len);
Mark Brown40c5cc22011-07-24 22:39:12 +01002052
Mark Brownb83a3132011-05-11 19:59:58 +02002053 map->async = false;
Mark Brownfb2736b2011-07-24 21:30:55 +01002054
2055 map->unlock(map->lock_arg);
2056
2057 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02002058}
2059EXPORT_SYMBOL_GPL(regmap_raw_write_async);
Andrey Smirnovad278402013-01-12 12:54:12 -08002060
2061static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2062 unsigned int val_len)
2063{
2064 struct regmap_range_node *range;
2065 u8 *u8 = map->work_buf;
2066 int ret;
2067
2068 WARN_ON(!map->bus);
2069
2070 range = _regmap_range_lookup(map, reg);
2071 if (range) {
2072 ret = _regmap_select_page(map, &reg, range,
2073 val_len / map->format.val_bytes);
2074 if (ret != 0)
2075 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02002076 }
2077
2078 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2079
Andrey Smirnovd2a58842013-01-27 10:49:05 -08002080 /*
2081 * Some buses or devices flag reads by setting the high bits in the
Mark Brownf1b5c5c2013-03-13 19:18:13 +00002082 * register addresss; since it's always the high bits for all
Mark Brownb83a3132011-05-11 19:59:58 +02002083 * current formats we can do this here rather than in
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01002084 * formatting. This may break if we get interesting formats.
2085 */
2086 u8[0] |= map->read_flag_mask;
2087
2088 trace_regmap_hw_read_start(map->dev, reg,
2089 val_len / map->format.val_bytes);
2090
2091 ret = map->bus->read(map->bus_context, map->work_buf,
2092 map->format.reg_bytes + map->format.pad_bytes,
Andrey Smirnovd2a58842013-01-27 10:49:05 -08002093 val, val_len);
Mark Brownfb2736b2011-07-24 21:30:55 +01002094
Mark Brown1044c182012-07-06 14:10:23 +01002095 trace_regmap_hw_read_done(map->dev, reg,
2096 val_len / map->format.val_bytes);
2097
2098 return ret;
2099}
Mark Brownfb2736b2011-07-24 21:30:55 +01002100
Boris BREZILLON3ac17032014-04-17 11:40:11 +02002101static int _regmap_bus_reg_read(void *context, unsigned int reg,
2102 unsigned int *val)
2103{
2104 struct regmap *map = context;
2105
2106 return map->bus->reg_read(map->bus_context, reg, val);
2107}
2108
Mark Brownb83a3132011-05-11 19:59:58 +02002109static int _regmap_bus_read(void *context, unsigned int reg,
2110 unsigned int *val)
2111{
2112 int ret;
2113 struct regmap *map = context;
2114
2115 if (!map->format.parse_val)
2116 return -EINVAL;
2117
2118 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2119 if (ret == 0)
2120 *val = map->format.parse_val(map->work_buf);
2121
2122 return ret;
2123}
2124
2125static int _regmap_read(struct regmap *map, unsigned int reg,
Mark Brownfb2736b2011-07-24 21:30:55 +01002126 unsigned int *val)
2127{
Mark Brownb83a3132011-05-11 19:59:58 +02002128 int ret;
Mark Brownf2985362012-04-30 21:25:05 +01002129 void *context = _regmap_map_get_context(map);
2130
2131 WARN_ON(!map->reg_read);
Mark Brownb83a3132011-05-11 19:59:58 +02002132
2133 if (!map->cache_bypass) {
2134 ret = regcache_read(map, reg, val);
2135 if (ret == 0)
2136 return 0;
2137 }
2138
2139 if (map->cache_only)
2140 return -EBUSY;
2141
Michal Simekd4807ad2014-02-10 12:59:46 +01002142 if (!regmap_readable(map, reg))
2143 return -EIO;
2144
Mark Brownb83a3132011-05-11 19:59:58 +02002145 ret = map->reg_read(context, reg, val);
2146 if (ret == 0) {
2147#ifdef LOG_DEVICE
2148 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2149 dev_info(map->dev, "%x => %x\n", reg, *val);
2150#endif
2151
Stephen Warrenf01ee602012-04-09 13:40:24 -06002152 trace_regmap_reg_read(map->dev, reg, *val);
Mark Brownb83a3132011-05-11 19:59:58 +02002153
Andrey Smirnovad278402013-01-12 12:54:12 -08002154 if (!map->cache_bypass)
2155 regcache_write(map, reg, *val);
2156 }
Mark Brownb83a3132011-05-11 19:59:58 +02002157
2158 return ret;
2159}
2160
2161/**
2162 * regmap_read(): Read a value from a single register
2163 *
Gerhard Sittig00933802013-11-11 10:42:36 +01002164 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02002165 * @reg: Register to be read from
2166 * @val: Pointer to store read value
2167 *
2168 * A value of zero will be returned on success, a negative errno will
2169 * be returned in error cases.
2170 */
2171int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2172{
2173 int ret;
2174
Stephen Warrenf01ee602012-04-09 13:40:24 -06002175 if (reg % map->reg_stride)
2176 return -EINVAL;
2177
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002178 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02002179
2180 ret = _regmap_read(map, reg, val);
2181
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002182 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02002183
2184 return ret;
2185}
2186EXPORT_SYMBOL_GPL(regmap_read);
2187
2188/**
2189 * regmap_raw_read(): Read raw data from the device
2190 *
Gerhard Sittig00933802013-11-11 10:42:36 +01002191 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02002192 * @reg: First register to be read from
2193 * @val: Pointer to store read value
2194 * @val_len: Size of data to read
2195 *
2196 * A value of zero will be returned on success, a negative errno will
2197 * be returned in error cases.
2198 */
2199int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2200 size_t val_len)
2201{
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002202 size_t val_bytes = map->format.val_bytes;
2203 size_t val_count = val_len / val_bytes;
2204 unsigned int v;
2205 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +01002206
Andrey Smirnovd2a58842013-01-27 10:49:05 -08002207 if (!map->bus)
2208 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06002209 if (val_len % map->format.val_bytes)
2210 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06002211 if (reg % map->reg_stride)
2212 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06002213
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002214 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02002215
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002216 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2217 map->cache_type == REGCACHE_NONE) {
2218 /* Physical block read if there's no cache involved */
2219 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02002220
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002221 } else {
2222 /* Otherwise go word by word for the cache; should be low
2223 * cost as we expect to hit the cache.
2224 */
2225 for (i = 0; i < val_count; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -06002226 ret = _regmap_read(map, reg + (i * map->reg_stride),
2227 &v);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002228 if (ret != 0)
2229 goto out;
2230
Marc Reillyd939fb92012-03-16 12:11:43 +11002231 map->format.format_val(val + (i * val_bytes), v, 0);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002232 }
2233 }
2234
2235 out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002236 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02002237
2238 return ret;
2239}
2240EXPORT_SYMBOL_GPL(regmap_raw_read);
2241
2242/**
Srinivas Kandagatla67252282013-06-11 13:18:15 +01002243 * regmap_field_read(): Read a value to a single register field
2244 *
2245 * @field: Register field to read from
2246 * @val: Pointer to store read value
2247 *
2248 * A value of zero will be returned on success, a negative errno will
2249 * be returned in error cases.
2250 */
2251int regmap_field_read(struct regmap_field *field, unsigned int *val)
2252{
2253 int ret;
2254 unsigned int reg_val;
2255 ret = regmap_read(field->regmap, field->reg, &reg_val);
2256 if (ret != 0)
2257 return ret;
2258
2259 reg_val &= field->mask;
2260 reg_val >>= field->shift;
2261 *val = reg_val;
2262
2263 return ret;
2264}
2265EXPORT_SYMBOL_GPL(regmap_field_read);
2266
2267/**
Kuninori Morimotoa0102372013-09-01 20:30:50 -07002268 * regmap_fields_read(): Read a value to a single register field with port ID
2269 *
2270 * @field: Register field to read from
2271 * @id: port ID
2272 * @val: Pointer to store read value
2273 *
2274 * A value of zero will be returned on success, a negative errno will
2275 * be returned in error cases.
2276 */
2277int regmap_fields_read(struct regmap_field *field, unsigned int id,
2278 unsigned int *val)
2279{
2280 int ret;
2281 unsigned int reg_val;
2282
2283 if (id >= field->id_size)
2284 return -EINVAL;
2285
2286 ret = regmap_read(field->regmap,
2287 field->reg + (field->id_offset * id),
2288 &reg_val);
2289 if (ret != 0)
2290 return ret;
2291
2292 reg_val &= field->mask;
2293 reg_val >>= field->shift;
2294 *val = reg_val;
2295
2296 return ret;
2297}
2298EXPORT_SYMBOL_GPL(regmap_fields_read);
2299
2300/**
Mark Brownb83a3132011-05-11 19:59:58 +02002301 * regmap_bulk_read(): Read multiple registers from the device
2302 *
Gerhard Sittig00933802013-11-11 10:42:36 +01002303 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02002304 * @reg: First register to be read from
2305 * @val: Pointer to store read value, in native register size for device
2306 * @val_count: Number of registers to read
2307 *
2308 * A value of zero will be returned on success, a negative errno will
2309 * be returned in error cases.
2310 */
2311int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2312 size_t val_count)
2313{
2314 int ret, i;
2315 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +01002316 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01002317
Stephen Warrenf01ee602012-04-09 13:40:24 -06002318 if (reg % map->reg_stride)
2319 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02002320
Stephen Boyd3b58ee12013-12-13 09:14:07 -08002321 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01002322 /*
2323 * Some devices does not support bulk read, for
2324 * them we have a series of single read operations.
2325 */
2326 if (map->use_single_rw) {
2327 for (i = 0; i < val_count; i++) {
2328 ret = regmap_raw_read(map,
2329 reg + (i * map->reg_stride),
2330 val + (i * val_bytes),
2331 val_bytes);
2332 if (ret != 0)
2333 return ret;
2334 }
2335 } else {
2336 ret = regmap_raw_read(map, reg, val,
2337 val_bytes * val_count);
2338 if (ret != 0)
2339 return ret;
2340 }
Mark Brownde2d8082011-10-10 13:24:52 +01002341
2342 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08002343 map->format.parse_inplace(val + i);
Mark Brownde2d8082011-10-10 13:24:52 +01002344 } else {
2345 for (i = 0; i < val_count; i++) {
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05302346 unsigned int ival;
Stephen Warrenf01ee602012-04-09 13:40:24 -06002347 ret = regmap_read(map, reg + (i * map->reg_stride),
Mark Brown25061d22012-05-12 13:06:08 +01002348 &ival);
Mark Brownde2d8082011-10-10 13:24:52 +01002349 if (ret != 0)
2350 return ret;
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05302351 memcpy(val + (i * val_bytes), &ival, val_bytes);
Mark Brownde2d8082011-10-10 13:24:52 +01002352 }
2353 }
Mark Brownb83a3132011-05-11 19:59:58 +02002354
2355 return 0;
2356}
2357EXPORT_SYMBOL_GPL(regmap_bulk_read);
2358
Mark Brown018690d2011-11-29 20:10:36 +00002359static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2360 unsigned int mask, unsigned int val,
2361 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +02002362{
2363 int ret;
Mark Brownd91e8db2011-11-18 16:03:50 +00002364 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +02002365
Mark Brownd91e8db2011-11-18 16:03:50 +00002366 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +02002367 if (ret != 0)
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002368 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02002369
Mark Brownd91e8db2011-11-18 16:03:50 +00002370 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +02002371 tmp |= val & mask;
2372
Mark Brown018690d2011-11-29 20:10:36 +00002373 if (tmp != orig) {
Mark Brownd91e8db2011-11-18 16:03:50 +00002374 ret = _regmap_write(map, reg, tmp);
Xiubo Lie2f74dc2014-02-20 08:50:10 +08002375 if (change)
2376 *change = true;
Mark Brown018690d2011-11-29 20:10:36 +00002377 } else {
Xiubo Lie2f74dc2014-02-20 08:50:10 +08002378 if (change)
2379 *change = false;
Mark Brown018690d2011-11-29 20:10:36 +00002380 }
Mark Brownb83a3132011-05-11 19:59:58 +02002381
Mark Brownb83a3132011-05-11 19:59:58 +02002382 return ret;
2383}
Mark Brown018690d2011-11-29 20:10:36 +00002384
2385/**
2386 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2387 *
2388 * @map: Register map to update
2389 * @reg: Register to update
2390 * @mask: Bitmask to change
2391 * @val: New value for bitmask
2392 *
2393 * Returns zero for success, a negative number on error.
2394 */
2395int regmap_update_bits(struct regmap *map, unsigned int reg,
2396 unsigned int mask, unsigned int val)
2397{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002398 int ret;
2399
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002400 map->lock(map->lock_arg);
Xiubo Lie2f74dc2014-02-20 08:50:10 +08002401 ret = _regmap_update_bits(map, reg, mask, val, NULL);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002402 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002403
2404 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00002405}
Mark Brownb83a3132011-05-11 19:59:58 +02002406EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +01002407
Mark Brown018690d2011-11-29 20:10:36 +00002408/**
Mark Brown915f4412013-10-09 13:30:10 +01002409 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2410 * map asynchronously
2411 *
2412 * @map: Register map to update
2413 * @reg: Register to update
2414 * @mask: Bitmask to change
2415 * @val: New value for bitmask
2416 *
2417 * With most buses the read must be done synchronously so this is most
2418 * useful for devices with a cache which do not need to interact with
2419 * the hardware to determine the current register value.
2420 *
2421 * Returns zero for success, a negative number on error.
2422 */
2423int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2424 unsigned int mask, unsigned int val)
2425{
Mark Brown915f4412013-10-09 13:30:10 +01002426 int ret;
2427
2428 map->lock(map->lock_arg);
2429
2430 map->async = true;
2431
Xiubo Lie2f74dc2014-02-20 08:50:10 +08002432 ret = _regmap_update_bits(map, reg, mask, val, NULL);
Mark Brown915f4412013-10-09 13:30:10 +01002433
2434 map->async = false;
2435
2436 map->unlock(map->lock_arg);
2437
2438 return ret;
2439}
2440EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2441
2442/**
Mark Brown018690d2011-11-29 20:10:36 +00002443 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2444 * register map and report if updated
2445 *
2446 * @map: Register map to update
2447 * @reg: Register to update
2448 * @mask: Bitmask to change
2449 * @val: New value for bitmask
2450 * @change: Boolean indicating if a write was done
2451 *
2452 * Returns zero for success, a negative number on error.
2453 */
2454int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2455 unsigned int mask, unsigned int val,
2456 bool *change)
2457{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002458 int ret;
2459
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002460 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002461 ret = _regmap_update_bits(map, reg, mask, val, change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002462 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002463 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00002464}
2465EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2466
Mark Brown915f4412013-10-09 13:30:10 +01002467/**
2468 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2469 * register map asynchronously and report if
2470 * updated
2471 *
2472 * @map: Register map to update
2473 * @reg: Register to update
2474 * @mask: Bitmask to change
2475 * @val: New value for bitmask
2476 * @change: Boolean indicating if a write was done
2477 *
2478 * With most buses the read must be done synchronously so this is most
2479 * useful for devices with a cache which do not need to interact with
2480 * the hardware to determine the current register value.
2481 *
2482 * Returns zero for success, a negative number on error.
2483 */
2484int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2485 unsigned int mask, unsigned int val,
2486 bool *change)
2487{
2488 int ret;
2489
2490 map->lock(map->lock_arg);
2491
2492 map->async = true;
2493
2494 ret = _regmap_update_bits(map, reg, mask, val, change);
2495
2496 map->async = false;
2497
2498 map->unlock(map->lock_arg);
2499
2500 return ret;
2501}
2502EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2503
Mark Brown0d509f22013-01-27 22:07:38 +08002504void regmap_async_complete_cb(struct regmap_async *async, int ret)
2505{
2506 struct regmap *map = async->map;
2507 bool wake;
2508
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002509 trace_regmap_async_io_complete(map->dev);
2510
Mark Brown0d509f22013-01-27 22:07:38 +08002511 spin_lock(&map->async_lock);
Mark Brown7e09a972013-10-07 23:00:24 +01002512 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08002513 wake = list_empty(&map->async_list);
2514
2515 if (ret != 0)
2516 map->async_ret = ret;
2517
2518 spin_unlock(&map->async_lock);
2519
Mark Brown0d509f22013-01-27 22:07:38 +08002520 if (wake)
2521 wake_up(&map->async_waitq);
2522}
Axel Linf804fb52013-02-03 00:14:13 +08002523EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
Mark Brown0d509f22013-01-27 22:07:38 +08002524
2525static int regmap_async_is_done(struct regmap *map)
2526{
2527 unsigned long flags;
2528 int ret;
2529
2530 spin_lock_irqsave(&map->async_lock, flags);
2531 ret = list_empty(&map->async_list);
2532 spin_unlock_irqrestore(&map->async_lock, flags);
2533
2534 return ret;
2535}
2536
2537/**
2538 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2539 *
2540 * @map: Map to operate on.
2541 *
2542 * Blocks until any pending asynchronous I/O has completed. Returns
2543 * an error code for any failed I/O operations.
2544 */
2545int regmap_async_complete(struct regmap *map)
2546{
2547 unsigned long flags;
2548 int ret;
2549
2550 /* Nothing to do with no async support */
Daniel Mackf2e055e2013-07-04 13:11:03 +02002551 if (!map->bus || !map->bus->async_write)
Mark Brown0d509f22013-01-27 22:07:38 +08002552 return 0;
2553
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002554 trace_regmap_async_complete_start(map->dev);
2555
Mark Brown0d509f22013-01-27 22:07:38 +08002556 wait_event(map->async_waitq, regmap_async_is_done(map));
2557
2558 spin_lock_irqsave(&map->async_lock, flags);
2559 ret = map->async_ret;
2560 map->async_ret = 0;
2561 spin_unlock_irqrestore(&map->async_lock, flags);
2562
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002563 trace_regmap_async_complete_done(map->dev);
2564
Mark Brown0d509f22013-01-27 22:07:38 +08002565 return ret;
2566}
Mark Brownf88948e2013-02-05 13:53:26 +00002567EXPORT_SYMBOL_GPL(regmap_async_complete);
Mark Brown0d509f22013-01-27 22:07:38 +08002568
Mark Brown22f0d902012-01-21 12:01:14 +00002569/**
2570 * regmap_register_patch: Register and apply register updates to be applied
2571 * on device initialistion
2572 *
2573 * @map: Register map to apply updates to.
2574 * @regs: Values to update.
2575 * @num_regs: Number of entries in regs.
2576 *
2577 * Register a set of register updates to be applied to the device
2578 * whenever the device registers are synchronised with the cache and
2579 * apply them immediately. Typically this is used to apply
2580 * corrections to be applied to the device defaults on startup, such
2581 * as the updates some vendors provide to undocumented registers.
Mark Brown56fb1c72014-03-18 10:53:26 +00002582 *
2583 * The caller must ensure that this function cannot be called
2584 * concurrently with either itself or regcache_sync().
Mark Brown22f0d902012-01-21 12:01:14 +00002585 */
2586int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2587 int num_regs)
2588{
Mark Brownaab13eb2013-07-11 12:41:44 +01002589 struct reg_default *p;
Charles Keepax6bf13102014-02-25 13:45:51 +00002590 int ret;
Mark Brown22f0d902012-01-21 12:01:14 +00002591 bool bypass;
2592
Cai Zhiyongbd60e382013-11-18 20:21:49 +08002593 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2594 num_regs))
2595 return 0;
2596
Mark Brownaab13eb2013-07-11 12:41:44 +01002597 p = krealloc(map->patch,
2598 sizeof(struct reg_default) * (map->patch_regs + num_regs),
2599 GFP_KERNEL);
2600 if (p) {
2601 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2602 map->patch = p;
2603 map->patch_regs += num_regs;
Mark Brown22f0d902012-01-21 12:01:14 +00002604 } else {
Mark Brown56fb1c72014-03-18 10:53:26 +00002605 return -ENOMEM;
Mark Brown22f0d902012-01-21 12:01:14 +00002606 }
2607
Mark Brown22f0d902012-01-21 12:01:14 +00002608 map->lock(map->lock_arg);
2609
2610 bypass = map->cache_bypass;
2611
2612 map->cache_bypass = true;
2613 map->async = true;
2614
Charles Keepax6bf13102014-02-25 13:45:51 +00002615 ret = _regmap_multi_reg_write(map, regs, num_regs);
2616 if (ret != 0)
2617 goto out;
Mark Brown22f0d902012-01-21 12:01:14 +00002618
Mark Brown22f0d902012-01-21 12:01:14 +00002619out:
Mark Brown1a25f262013-10-10 20:55:03 +01002620 map->async = false;
Mark Brown22f0d902012-01-21 12:01:14 +00002621 map->cache_bypass = bypass;
2622
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002623 map->unlock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00002624
Mark Brown1a25f262013-10-10 20:55:03 +01002625 regmap_async_complete(map);
2626
Mark Brown22f0d902012-01-21 12:01:14 +00002627 return ret;
2628}
2629EXPORT_SYMBOL_GPL(regmap_register_patch);
2630
Mark Browneae4b512012-03-14 13:15:03 +00002631/*
Mark Browna6539c32012-02-17 14:20:14 -08002632 * regmap_get_val_bytes(): Report the size of a register value
2633 *
2634 * Report the size of a register value, mainly intended to for use by
2635 * generic infrastructure built on top of regmap.
2636 */
2637int regmap_get_val_bytes(struct regmap *map)
2638{
2639 if (map->format.format_write)
2640 return -EINVAL;
2641
2642 return map->format.val_bytes;
2643}
2644EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2645
Nenghua Cao13ff50c2014-02-19 18:44:13 +08002646int regmap_parse_val(struct regmap *map, const void *buf,
2647 unsigned int *val)
2648{
2649 if (!map->format.parse_val)
2650 return -EINVAL;
2651
2652 *val = map->format.parse_val(buf);
2653
2654 return 0;
2655}
2656EXPORT_SYMBOL_GPL(regmap_parse_val);
2657
Mark Brown31244e32011-07-20 22:56:53 +01002658static int __init regmap_initcall(void)
2659{
2660 regmap_debugfs_initcall();
2661
2662 return 0;
2663}
2664postcore_initcall(regmap_initcall);