blob: 71282e12fd294bc386de954385d128fd590c780c [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>
Krystian Garbaciak6863ca62012-06-15 11:23:56 +010018#include <linux/rbtree.h>
Stephen Warren30b2a552013-02-02 22:50:14 -070019#include <linux/sched.h>
Mark Brownb83a3132011-05-11 19:59:58 +020020
Mark Brownfb2736b2011-07-24 21:30:55 +010021#define CREATE_TRACE_POINTS
22#include <trace/events/regmap.h>
23
Mark Brown93de9122011-07-20 22:35:37 +010024#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020025
Mark Brown1044c182012-07-06 14:10:23 +010026/*
27 * Sometimes for failures during very early init the trace
28 * infrastructure isn't available early enough to be used. For this
29 * sort of problem defining LOG_DEVICE will add printks for basic
30 * register I/O on a specific device.
31 */
32#undef LOG_DEVICE
33
34static int _regmap_update_bits(struct regmap *map, unsigned int reg,
35 unsigned int mask, unsigned int val,
36 bool *change);
37
Andrey Smirnovad278402013-01-12 12:54:12 -080038static int _regmap_bus_read(void *context, unsigned int reg,
39 unsigned int *val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -080040static int _regmap_bus_formatted_write(void *context, unsigned int reg,
41 unsigned int val);
42static int _regmap_bus_raw_write(void *context, unsigned int reg,
43 unsigned int val);
Andrey Smirnovad278402013-01-12 12:54:12 -080044
Davide Ciminaghi76aad392012-11-20 15:20:30 +010045bool regmap_reg_in_ranges(unsigned int reg,
46 const struct regmap_range *ranges,
47 unsigned int nranges)
48{
49 const struct regmap_range *r;
50 int i;
51
52 for (i = 0, r = ranges; i < nranges; i++, r++)
53 if (regmap_reg_in_range(reg, r))
54 return true;
55 return false;
56}
57EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
58
Mark Brown154881e2013-05-08 13:55:23 +010059bool regmap_check_range_table(struct regmap *map, unsigned int reg,
60 const struct regmap_access_table *table)
Davide Ciminaghi76aad392012-11-20 15:20:30 +010061{
62 /* Check "no ranges" first */
63 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
64 return false;
65
66 /* In case zero "yes ranges" are supplied, any reg is OK */
67 if (!table->n_yes_ranges)
68 return true;
69
70 return regmap_reg_in_ranges(reg, table->yes_ranges,
71 table->n_yes_ranges);
72}
Mark Brown154881e2013-05-08 13:55:23 +010073EXPORT_SYMBOL_GPL(regmap_check_range_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +010074
Mark Brown8de2f082011-08-10 17:14:41 +090075bool regmap_writeable(struct regmap *map, unsigned int reg)
76{
77 if (map->max_register && reg > map->max_register)
78 return false;
79
80 if (map->writeable_reg)
81 return map->writeable_reg(map->dev, reg);
82
Davide Ciminaghi76aad392012-11-20 15:20:30 +010083 if (map->wr_table)
Mark Brown154881e2013-05-08 13:55:23 +010084 return regmap_check_range_table(map, reg, map->wr_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +010085
Mark Brown8de2f082011-08-10 17:14:41 +090086 return true;
87}
88
89bool regmap_readable(struct regmap *map, unsigned int reg)
90{
91 if (map->max_register && reg > map->max_register)
92 return false;
93
Wolfram Sang4191f192012-01-30 15:08:16 +010094 if (map->format.format_write)
95 return false;
96
Mark Brown8de2f082011-08-10 17:14:41 +090097 if (map->readable_reg)
98 return map->readable_reg(map->dev, reg);
99
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100100 if (map->rd_table)
Mark Brown154881e2013-05-08 13:55:23 +0100101 return regmap_check_range_table(map, reg, map->rd_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100102
Mark Brown8de2f082011-08-10 17:14:41 +0900103 return true;
104}
105
106bool regmap_volatile(struct regmap *map, unsigned int reg)
107{
Wolfram Sang4191f192012-01-30 15:08:16 +0100108 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +0900109 return false;
110
111 if (map->volatile_reg)
112 return map->volatile_reg(map->dev, reg);
113
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100114 if (map->volatile_table)
Mark Brown154881e2013-05-08 13:55:23 +0100115 return regmap_check_range_table(map, reg, map->volatile_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100116
Mark Brownb92be6f2013-06-03 17:24:08 +0100117 if (map->cache_ops)
118 return false;
119 else
120 return true;
Mark Brown8de2f082011-08-10 17:14:41 +0900121}
122
123bool regmap_precious(struct regmap *map, unsigned int reg)
124{
Wolfram Sang4191f192012-01-30 15:08:16 +0100125 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +0900126 return false;
127
128 if (map->precious_reg)
129 return map->precious_reg(map->dev, reg);
130
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100131 if (map->precious_table)
Mark Brown154881e2013-05-08 13:55:23 +0100132 return regmap_check_range_table(map, reg, map->precious_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100133
Mark Brown8de2f082011-08-10 17:14:41 +0900134 return false;
135}
136
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100137static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
Paul Bollea8f28cf2012-10-08 22:06:30 +0200138 size_t num)
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100139{
140 unsigned int i;
141
142 for (i = 0; i < num; i++)
143 if (!regmap_volatile(map, reg + i))
144 return false;
145
146 return true;
147}
148
Wolfram Sang9aa50752012-01-27 16:10:22 +0100149static void regmap_format_2_6_write(struct regmap *map,
150 unsigned int reg, unsigned int val)
151{
152 u8 *out = map->work_buf;
153
154 *out = (reg << 6) | val;
155}
156
Mark Brownb83a3132011-05-11 19:59:58 +0200157static void regmap_format_4_12_write(struct regmap *map,
158 unsigned int reg, unsigned int val)
159{
160 __be16 *out = map->work_buf;
161 *out = cpu_to_be16((reg << 12) | val);
162}
163
164static void regmap_format_7_9_write(struct regmap *map,
165 unsigned int reg, unsigned int val)
166{
167 __be16 *out = map->work_buf;
168 *out = cpu_to_be16((reg << 9) | val);
169}
170
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100171static void regmap_format_10_14_write(struct regmap *map,
172 unsigned int reg, unsigned int val)
173{
174 u8 *out = map->work_buf;
175
176 out[2] = val;
177 out[1] = (val >> 8) | (reg << 6);
178 out[0] = reg >> 2;
179}
180
Marc Reillyd939fb92012-03-16 12:11:43 +1100181static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200182{
183 u8 *b = buf;
184
Marc Reillyd939fb92012-03-16 12:11:43 +1100185 b[0] = val << shift;
Mark Brownb83a3132011-05-11 19:59:58 +0200186}
187
Stephen Warren141eba22012-05-24 10:47:26 -0600188static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200189{
190 __be16 *b = buf;
191
Marc Reillyd939fb92012-03-16 12:11:43 +1100192 b[0] = cpu_to_be16(val << shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200193}
194
Stephen Warren141eba22012-05-24 10:47:26 -0600195static void regmap_format_16_native(void *buf, unsigned int val,
196 unsigned int shift)
197{
198 *(u16 *)buf = val << shift;
199}
200
Marc Reillyd939fb92012-03-16 12:11:43 +1100201static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
Marc Reillyea279fc2012-03-16 12:11:42 +1100202{
203 u8 *b = buf;
204
Marc Reillyd939fb92012-03-16 12:11:43 +1100205 val <<= shift;
206
Marc Reillyea279fc2012-03-16 12:11:42 +1100207 b[0] = val >> 16;
208 b[1] = val >> 8;
209 b[2] = val;
210}
211
Stephen Warren141eba22012-05-24 10:47:26 -0600212static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
Mark Brown7d5e5252012-02-17 15:58:25 -0800213{
214 __be32 *b = buf;
215
Marc Reillyd939fb92012-03-16 12:11:43 +1100216 b[0] = cpu_to_be32(val << shift);
Mark Brown7d5e5252012-02-17 15:58:25 -0800217}
218
Stephen Warren141eba22012-05-24 10:47:26 -0600219static void regmap_format_32_native(void *buf, unsigned int val,
220 unsigned int shift)
221{
222 *(u32 *)buf = val << shift;
223}
224
Mark Brown8a819ff2013-03-04 09:04:51 +0800225static void regmap_parse_inplace_noop(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200226{
Mark Brown8a819ff2013-03-04 09:04:51 +0800227}
228
229static unsigned int regmap_parse_8(const void *buf)
230{
231 const u8 *b = buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200232
233 return b[0];
234}
235
Mark Brown8a819ff2013-03-04 09:04:51 +0800236static unsigned int regmap_parse_16_be(const void *buf)
237{
238 const __be16 *b = buf;
239
240 return be16_to_cpu(b[0]);
241}
242
243static void regmap_parse_16_be_inplace(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200244{
245 __be16 *b = buf;
246
247 b[0] = be16_to_cpu(b[0]);
Mark Brownb83a3132011-05-11 19:59:58 +0200248}
249
Mark Brown8a819ff2013-03-04 09:04:51 +0800250static unsigned int regmap_parse_16_native(const void *buf)
Stephen Warren141eba22012-05-24 10:47:26 -0600251{
252 return *(u16 *)buf;
253}
254
Mark Brown8a819ff2013-03-04 09:04:51 +0800255static unsigned int regmap_parse_24(const void *buf)
Marc Reillyea279fc2012-03-16 12:11:42 +1100256{
Mark Brown8a819ff2013-03-04 09:04:51 +0800257 const u8 *b = buf;
Marc Reillyea279fc2012-03-16 12:11:42 +1100258 unsigned int ret = b[2];
259 ret |= ((unsigned int)b[1]) << 8;
260 ret |= ((unsigned int)b[0]) << 16;
261
262 return ret;
263}
264
Mark Brown8a819ff2013-03-04 09:04:51 +0800265static unsigned int regmap_parse_32_be(const void *buf)
266{
267 const __be32 *b = buf;
268
269 return be32_to_cpu(b[0]);
270}
271
272static void regmap_parse_32_be_inplace(void *buf)
Mark Brown7d5e5252012-02-17 15:58:25 -0800273{
274 __be32 *b = buf;
275
276 b[0] = be32_to_cpu(b[0]);
Mark Brown7d5e5252012-02-17 15:58:25 -0800277}
278
Mark Brown8a819ff2013-03-04 09:04:51 +0800279static unsigned int regmap_parse_32_native(const void *buf)
Stephen Warren141eba22012-05-24 10:47:26 -0600280{
281 return *(u32 *)buf;
282}
283
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200284static void regmap_lock_mutex(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600285{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200286 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600287 mutex_lock(&map->mutex);
288}
289
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200290static void regmap_unlock_mutex(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600291{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200292 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600293 mutex_unlock(&map->mutex);
294}
295
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200296static void regmap_lock_spinlock(void *__map)
Fabio Estevamb4519c72013-07-16 02:10:11 -0300297__acquires(&map->spinlock)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600298{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200299 struct regmap *map = __map;
Lars-Peter Clausen92ab1aa2013-05-24 10:29:22 +0200300 unsigned long flags;
301
302 spin_lock_irqsave(&map->spinlock, flags);
303 map->spinlock_flags = flags;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600304}
305
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200306static void regmap_unlock_spinlock(void *__map)
Fabio Estevamb4519c72013-07-16 02:10:11 -0300307__releases(&map->spinlock)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600308{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200309 struct regmap *map = __map;
Lars-Peter Clausen92ab1aa2013-05-24 10:29:22 +0200310 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600311}
312
Mark Brown72b39f62012-05-08 17:44:40 +0100313static void dev_get_regmap_release(struct device *dev, void *res)
314{
315 /*
316 * We don't actually have anything to do here; the goal here
317 * is not to manage the regmap but to provide a simple way to
318 * get the regmap back given a struct device.
319 */
320}
321
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100322static bool _regmap_range_add(struct regmap *map,
323 struct regmap_range_node *data)
324{
325 struct rb_root *root = &map->range_tree;
326 struct rb_node **new = &(root->rb_node), *parent = NULL;
327
328 while (*new) {
329 struct regmap_range_node *this =
330 container_of(*new, struct regmap_range_node, node);
331
332 parent = *new;
333 if (data->range_max < this->range_min)
334 new = &((*new)->rb_left);
335 else if (data->range_min > this->range_max)
336 new = &((*new)->rb_right);
337 else
338 return false;
339 }
340
341 rb_link_node(&data->node, parent, new);
342 rb_insert_color(&data->node, root);
343
344 return true;
345}
346
347static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
348 unsigned int reg)
349{
350 struct rb_node *node = map->range_tree.rb_node;
351
352 while (node) {
353 struct regmap_range_node *this =
354 container_of(node, struct regmap_range_node, node);
355
356 if (reg < this->range_min)
357 node = node->rb_left;
358 else if (reg > this->range_max)
359 node = node->rb_right;
360 else
361 return this;
362 }
363
364 return NULL;
365}
366
367static void regmap_range_exit(struct regmap *map)
368{
369 struct rb_node *next;
370 struct regmap_range_node *range_node;
371
372 next = rb_first(&map->range_tree);
373 while (next) {
374 range_node = rb_entry(next, struct regmap_range_node, node);
375 next = rb_next(&range_node->node);
376 rb_erase(&range_node->node, &map->range_tree);
377 kfree(range_node);
378 }
379
380 kfree(map->selector_work_buf);
381}
382
Mark Brownb83a3132011-05-11 19:59:58 +0200383/**
384 * regmap_init(): Initialise register map
385 *
386 * @dev: Device that will be interacted with
387 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600388 * @bus_context: Data passed to bus-specific callbacks
Mark Brownb83a3132011-05-11 19:59:58 +0200389 * @config: Configuration for register map
390 *
391 * The return value will be an ERR_PTR() on error or a valid pointer to
392 * a struct regmap. This function should generally not be called
393 * directly, it should be called by bus-specific init functions.
394 */
395struct regmap *regmap_init(struct device *dev,
396 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600397 void *bus_context,
Mark Brownb83a3132011-05-11 19:59:58 +0200398 const struct regmap_config *config)
399{
Mark Brown72b39f62012-05-08 17:44:40 +0100400 struct regmap *map, **m;
Mark Brownb83a3132011-05-11 19:59:58 +0200401 int ret = -EINVAL;
Stephen Warren141eba22012-05-24 10:47:26 -0600402 enum regmap_endian reg_endian, val_endian;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100403 int i, j;
Mark Brownb83a3132011-05-11 19:59:58 +0200404
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800405 if (!config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100406 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200407
408 map = kzalloc(sizeof(*map), GFP_KERNEL);
409 if (map == NULL) {
410 ret = -ENOMEM;
411 goto err;
412 }
413
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200414 if (config->lock && config->unlock) {
415 map->lock = config->lock;
416 map->unlock = config->unlock;
417 map->lock_arg = config->lock_arg;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600418 } else {
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800419 if ((bus && bus->fast_io) ||
420 config->fast_io) {
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200421 spin_lock_init(&map->spinlock);
422 map->lock = regmap_lock_spinlock;
423 map->unlock = regmap_unlock_spinlock;
424 } else {
425 mutex_init(&map->mutex);
426 map->lock = regmap_lock_mutex;
427 map->unlock = regmap_unlock_mutex;
428 }
429 map->lock_arg = map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600430 }
Wolfram Sangc212acc2012-01-28 02:16:41 +0100431 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000432 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100433 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Fabio Estevam5494a982012-05-31 21:10:30 -0300434 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
435 config->val_bits + config->pad_bits, 8);
Marc Reillyd939fb92012-03-16 12:11:43 +1100436 map->reg_shift = config->pad_bits % 8;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600437 if (config->reg_stride)
438 map->reg_stride = config->reg_stride;
439 else
440 map->reg_stride = 1;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100441 map->use_single_rw = config->use_single_rw;
Mark Brownb83a3132011-05-11 19:59:58 +0200442 map->dev = dev;
443 map->bus = bus;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600444 map->bus_context = bus_context;
Mark Brown2e2ae662011-07-20 22:33:39 +0100445 map->max_register = config->max_register;
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100446 map->wr_table = config->wr_table;
447 map->rd_table = config->rd_table;
448 map->volatile_table = config->volatile_table;
449 map->precious_table = config->precious_table;
Mark Brown2e2ae662011-07-20 22:33:39 +0100450 map->writeable_reg = config->writeable_reg;
451 map->readable_reg = config->readable_reg;
452 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900453 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100454 map->cache_type = config->cache_type;
Mark Brown72b39f62012-05-08 17:44:40 +0100455 map->name = config->name;
Mark Brownb83a3132011-05-11 19:59:58 +0200456
Mark Brown0d509f22013-01-27 22:07:38 +0800457 spin_lock_init(&map->async_lock);
458 INIT_LIST_HEAD(&map->async_list);
Mark Brown7e09a972013-10-07 23:00:24 +0100459 INIT_LIST_HEAD(&map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +0800460 init_waitqueue_head(&map->async_waitq);
461
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200462 if (config->read_flag_mask || config->write_flag_mask) {
463 map->read_flag_mask = config->read_flag_mask;
464 map->write_flag_mask = config->write_flag_mask;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800465 } else if (bus) {
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200466 map->read_flag_mask = bus->read_flag_mask;
467 }
468
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800469 if (!bus) {
470 map->reg_read = config->reg_read;
471 map->reg_write = config->reg_write;
472
473 map->defer_caching = false;
474 goto skip_format_initialization;
475 } else {
476 map->reg_read = _regmap_bus_read;
477 }
Andrey Smirnovad278402013-01-12 12:54:12 -0800478
Stephen Warren141eba22012-05-24 10:47:26 -0600479 reg_endian = config->reg_format_endian;
480 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
481 reg_endian = bus->reg_format_endian_default;
482 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
483 reg_endian = REGMAP_ENDIAN_BIG;
484
485 val_endian = config->val_format_endian;
486 if (val_endian == REGMAP_ENDIAN_DEFAULT)
487 val_endian = bus->val_format_endian_default;
488 if (val_endian == REGMAP_ENDIAN_DEFAULT)
489 val_endian = REGMAP_ENDIAN_BIG;
490
Marc Reillyd939fb92012-03-16 12:11:43 +1100491 switch (config->reg_bits + map->reg_shift) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100492 case 2:
493 switch (config->val_bits) {
494 case 6:
495 map->format.format_write = regmap_format_2_6_write;
496 break;
497 default:
498 goto err_map;
499 }
500 break;
501
Mark Brownb83a3132011-05-11 19:59:58 +0200502 case 4:
503 switch (config->val_bits) {
504 case 12:
505 map->format.format_write = regmap_format_4_12_write;
506 break;
507 default:
508 goto err_map;
509 }
510 break;
511
512 case 7:
513 switch (config->val_bits) {
514 case 9:
515 map->format.format_write = regmap_format_7_9_write;
516 break;
517 default:
518 goto err_map;
519 }
520 break;
521
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100522 case 10:
523 switch (config->val_bits) {
524 case 14:
525 map->format.format_write = regmap_format_10_14_write;
526 break;
527 default:
528 goto err_map;
529 }
530 break;
531
Mark Brownb83a3132011-05-11 19:59:58 +0200532 case 8:
533 map->format.format_reg = regmap_format_8;
534 break;
535
536 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600537 switch (reg_endian) {
538 case REGMAP_ENDIAN_BIG:
539 map->format.format_reg = regmap_format_16_be;
540 break;
541 case REGMAP_ENDIAN_NATIVE:
542 map->format.format_reg = regmap_format_16_native;
543 break;
544 default:
545 goto err_map;
546 }
Mark Brownb83a3132011-05-11 19:59:58 +0200547 break;
548
Lars-Peter Clausen237019e2013-01-10 17:06:10 +0100549 case 24:
550 if (reg_endian != REGMAP_ENDIAN_BIG)
551 goto err_map;
552 map->format.format_reg = regmap_format_24;
553 break;
554
Mark Brown7d5e5252012-02-17 15:58:25 -0800555 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600556 switch (reg_endian) {
557 case REGMAP_ENDIAN_BIG:
558 map->format.format_reg = regmap_format_32_be;
559 break;
560 case REGMAP_ENDIAN_NATIVE:
561 map->format.format_reg = regmap_format_32_native;
562 break;
563 default:
564 goto err_map;
565 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800566 break;
567
Mark Brownb83a3132011-05-11 19:59:58 +0200568 default:
569 goto err_map;
570 }
571
Mark Brown8a819ff2013-03-04 09:04:51 +0800572 if (val_endian == REGMAP_ENDIAN_NATIVE)
573 map->format.parse_inplace = regmap_parse_inplace_noop;
574
Mark Brownb83a3132011-05-11 19:59:58 +0200575 switch (config->val_bits) {
576 case 8:
577 map->format.format_val = regmap_format_8;
578 map->format.parse_val = regmap_parse_8;
Mark Brown8a819ff2013-03-04 09:04:51 +0800579 map->format.parse_inplace = regmap_parse_inplace_noop;
Mark Brownb83a3132011-05-11 19:59:58 +0200580 break;
581 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600582 switch (val_endian) {
583 case REGMAP_ENDIAN_BIG:
584 map->format.format_val = regmap_format_16_be;
585 map->format.parse_val = regmap_parse_16_be;
Mark Brown8a819ff2013-03-04 09:04:51 +0800586 map->format.parse_inplace = regmap_parse_16_be_inplace;
Stephen Warren141eba22012-05-24 10:47:26 -0600587 break;
588 case REGMAP_ENDIAN_NATIVE:
589 map->format.format_val = regmap_format_16_native;
590 map->format.parse_val = regmap_parse_16_native;
591 break;
592 default:
593 goto err_map;
594 }
Mark Brownb83a3132011-05-11 19:59:58 +0200595 break;
Marc Reillyea279fc2012-03-16 12:11:42 +1100596 case 24:
Stephen Warren141eba22012-05-24 10:47:26 -0600597 if (val_endian != REGMAP_ENDIAN_BIG)
598 goto err_map;
Marc Reillyea279fc2012-03-16 12:11:42 +1100599 map->format.format_val = regmap_format_24;
600 map->format.parse_val = regmap_parse_24;
601 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800602 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600603 switch (val_endian) {
604 case REGMAP_ENDIAN_BIG:
605 map->format.format_val = regmap_format_32_be;
606 map->format.parse_val = regmap_parse_32_be;
Mark Brown8a819ff2013-03-04 09:04:51 +0800607 map->format.parse_inplace = regmap_parse_32_be_inplace;
Stephen Warren141eba22012-05-24 10:47:26 -0600608 break;
609 case REGMAP_ENDIAN_NATIVE:
610 map->format.format_val = regmap_format_32_native;
611 map->format.parse_val = regmap_parse_32_native;
612 break;
613 default:
614 goto err_map;
615 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800616 break;
Mark Brownb83a3132011-05-11 19:59:58 +0200617 }
618
Stephen Warren141eba22012-05-24 10:47:26 -0600619 if (map->format.format_write) {
620 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
621 (val_endian != REGMAP_ENDIAN_BIG))
622 goto err_map;
Mark Brown7a647612012-04-30 23:26:32 +0100623 map->use_single_rw = true;
Stephen Warren141eba22012-05-24 10:47:26 -0600624 }
Mark Brown7a647612012-04-30 23:26:32 +0100625
Mark Brownb83a3132011-05-11 19:59:58 +0200626 if (!map->format.format_write &&
627 !(map->format.format_reg && map->format.format_val))
628 goto err_map;
629
Mark Brown82159ba2012-01-18 10:52:25 +0000630 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200631 if (map->work_buf == NULL) {
632 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700633 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200634 }
635
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800636 if (map->format.format_write) {
637 map->defer_caching = false;
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800638 map->reg_write = _regmap_bus_formatted_write;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800639 } else if (map->format.format_val) {
640 map->defer_caching = true;
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800641 map->reg_write = _regmap_bus_raw_write;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800642 }
643
644skip_format_initialization:
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800645
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100646 map->range_tree = RB_ROOT;
Mark Browne3549cd2012-10-02 20:17:15 +0100647 for (i = 0; i < config->num_ranges; i++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100648 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
649 struct regmap_range_node *new;
650
651 /* Sanity check */
Mark Brown061adc02012-10-03 12:17:51 +0100652 if (range_cfg->range_max < range_cfg->range_min) {
653 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
654 range_cfg->range_max, range_cfg->range_min);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100655 goto err_range;
Mark Brown061adc02012-10-03 12:17:51 +0100656 }
657
658 if (range_cfg->range_max > map->max_register) {
659 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
660 range_cfg->range_max, map->max_register);
661 goto err_range;
662 }
663
664 if (range_cfg->selector_reg > map->max_register) {
665 dev_err(map->dev,
666 "Invalid range %d: selector out of map\n", i);
667 goto err_range;
668 }
669
670 if (range_cfg->window_len == 0) {
671 dev_err(map->dev, "Invalid range %d: window_len 0\n",
672 i);
673 goto err_range;
674 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100675
676 /* Make sure, that this register range has no selector
677 or data window within its boundary */
Mark Browne3549cd2012-10-02 20:17:15 +0100678 for (j = 0; j < config->num_ranges; j++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100679 unsigned sel_reg = config->ranges[j].selector_reg;
680 unsigned win_min = config->ranges[j].window_start;
681 unsigned win_max = win_min +
682 config->ranges[j].window_len - 1;
683
Philipp Zabelf161d222013-07-23 12:16:02 +0200684 /* Allow data window inside its own virtual range */
685 if (j == i)
686 continue;
687
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100688 if (range_cfg->range_min <= sel_reg &&
689 sel_reg <= range_cfg->range_max) {
Mark Brown061adc02012-10-03 12:17:51 +0100690 dev_err(map->dev,
691 "Range %d: selector for %d in window\n",
692 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100693 goto err_range;
694 }
695
696 if (!(win_max < range_cfg->range_min ||
697 win_min > range_cfg->range_max)) {
Mark Brown061adc02012-10-03 12:17:51 +0100698 dev_err(map->dev,
699 "Range %d: window for %d in window\n",
700 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100701 goto err_range;
702 }
703 }
704
705 new = kzalloc(sizeof(*new), GFP_KERNEL);
706 if (new == NULL) {
707 ret = -ENOMEM;
708 goto err_range;
709 }
710
Mark Brown4b020b32012-10-03 13:13:16 +0100711 new->map = map;
Mark Brownd058bb42012-10-03 12:40:47 +0100712 new->name = range_cfg->name;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100713 new->range_min = range_cfg->range_min;
714 new->range_max = range_cfg->range_max;
715 new->selector_reg = range_cfg->selector_reg;
716 new->selector_mask = range_cfg->selector_mask;
717 new->selector_shift = range_cfg->selector_shift;
718 new->window_start = range_cfg->window_start;
719 new->window_len = range_cfg->window_len;
720
721 if (_regmap_range_add(map, new) == false) {
Mark Brown061adc02012-10-03 12:17:51 +0100722 dev_err(map->dev, "Failed to add range %d\n", i);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100723 kfree(new);
724 goto err_range;
725 }
726
727 if (map->selector_work_buf == NULL) {
728 map->selector_work_buf =
729 kzalloc(map->format.buf_size, GFP_KERNEL);
730 if (map->selector_work_buf == NULL) {
731 ret = -ENOMEM;
732 goto err_range;
733 }
734 }
735 }
Mark Brown052d2cd2011-11-21 19:05:13 +0000736
Dimitris Papastamosc6432ea2013-03-11 17:27:02 +0000737 regmap_debugfs_init(map, config->name);
738
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100739 ret = regcache_init(map, config);
Mark Brown0ff3e622012-10-04 17:39:13 +0100740 if (ret != 0)
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100741 goto err_range;
742
Mark Brown72b39f62012-05-08 17:44:40 +0100743 /* Add a devres resource for dev_get_regmap() */
744 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
745 if (!m) {
746 ret = -ENOMEM;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100747 goto err_debugfs;
Mark Brown72b39f62012-05-08 17:44:40 +0100748 }
749 *m = map;
750 devres_add(dev, m);
751
Mark Brownb83a3132011-05-11 19:59:58 +0200752 return map;
753
Stephen Warrenbfaa25f2012-05-23 16:30:53 -0600754err_debugfs:
755 regmap_debugfs_exit(map);
Mark Brown72b39f62012-05-08 17:44:40 +0100756 regcache_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100757err_range:
758 regmap_range_exit(map);
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100759 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200760err_map:
761 kfree(map);
762err:
763 return ERR_PTR(ret);
764}
765EXPORT_SYMBOL_GPL(regmap_init);
766
Mark Brownc0eb4672012-01-30 19:56:52 +0000767static void devm_regmap_release(struct device *dev, void *res)
768{
769 regmap_exit(*(struct regmap **)res);
770}
771
772/**
773 * devm_regmap_init(): Initialise managed register map
774 *
775 * @dev: Device that will be interacted with
776 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600777 * @bus_context: Data passed to bus-specific callbacks
Mark Brownc0eb4672012-01-30 19:56:52 +0000778 * @config: Configuration for register map
779 *
780 * The return value will be an ERR_PTR() on error or a valid pointer
781 * to a struct regmap. This function should generally not be called
782 * directly, it should be called by bus-specific init functions. The
783 * map will be automatically freed by the device management code.
784 */
785struct regmap *devm_regmap_init(struct device *dev,
786 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600787 void *bus_context,
Mark Brownc0eb4672012-01-30 19:56:52 +0000788 const struct regmap_config *config)
789{
790 struct regmap **ptr, *regmap;
791
792 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
793 if (!ptr)
794 return ERR_PTR(-ENOMEM);
795
Stephen Warren0135bbc2012-04-04 15:48:30 -0600796 regmap = regmap_init(dev, bus, bus_context, config);
Mark Brownc0eb4672012-01-30 19:56:52 +0000797 if (!IS_ERR(regmap)) {
798 *ptr = regmap;
799 devres_add(dev, ptr);
800 } else {
801 devres_free(ptr);
802 }
803
804 return regmap;
805}
806EXPORT_SYMBOL_GPL(devm_regmap_init);
807
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100808static void regmap_field_init(struct regmap_field *rm_field,
809 struct regmap *regmap, struct reg_field reg_field)
810{
811 int field_bits = reg_field.msb - reg_field.lsb + 1;
812 rm_field->regmap = regmap;
813 rm_field->reg = reg_field.reg;
814 rm_field->shift = reg_field.lsb;
815 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
816}
817
818/**
819 * devm_regmap_field_alloc(): Allocate and initialise a register field
820 * in a register map.
821 *
822 * @dev: Device that will be interacted with
823 * @regmap: regmap bank in which this register field is located.
824 * @reg_field: Register field with in the bank.
825 *
826 * The return value will be an ERR_PTR() on error or a valid pointer
827 * to a struct regmap_field. The regmap_field will be automatically freed
828 * by the device management code.
829 */
830struct regmap_field *devm_regmap_field_alloc(struct device *dev,
831 struct regmap *regmap, struct reg_field reg_field)
832{
833 struct regmap_field *rm_field = devm_kzalloc(dev,
834 sizeof(*rm_field), GFP_KERNEL);
835 if (!rm_field)
836 return ERR_PTR(-ENOMEM);
837
838 regmap_field_init(rm_field, regmap, reg_field);
839
840 return rm_field;
841
842}
843EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
844
845/**
846 * devm_regmap_field_free(): Free register field allocated using
847 * devm_regmap_field_alloc. Usally drivers need not call this function,
848 * as the memory allocated via devm will be freed as per device-driver
849 * life-cyle.
850 *
851 * @dev: Device that will be interacted with
852 * @field: regmap field which should be freed.
853 */
854void devm_regmap_field_free(struct device *dev,
855 struct regmap_field *field)
856{
857 devm_kfree(dev, field);
858}
859EXPORT_SYMBOL_GPL(devm_regmap_field_free);
860
861/**
862 * regmap_field_alloc(): Allocate and initialise a register field
863 * in a register map.
864 *
865 * @regmap: regmap bank in which this register field is located.
866 * @reg_field: Register field with in the bank.
867 *
868 * The return value will be an ERR_PTR() on error or a valid pointer
869 * to a struct regmap_field. The regmap_field should be freed by the
870 * user once its finished working with it using regmap_field_free().
871 */
872struct regmap_field *regmap_field_alloc(struct regmap *regmap,
873 struct reg_field reg_field)
874{
875 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
876
877 if (!rm_field)
878 return ERR_PTR(-ENOMEM);
879
880 regmap_field_init(rm_field, regmap, reg_field);
881
882 return rm_field;
883}
884EXPORT_SYMBOL_GPL(regmap_field_alloc);
885
886/**
887 * regmap_field_free(): Free register field allocated using regmap_field_alloc
888 *
889 * @field: regmap field which should be freed.
890 */
891void regmap_field_free(struct regmap_field *field)
892{
893 kfree(field);
894}
895EXPORT_SYMBOL_GPL(regmap_field_free);
896
Mark Brownb83a3132011-05-11 19:59:58 +0200897/**
Mark Brownbf315172011-12-03 17:06:20 +0000898 * regmap_reinit_cache(): Reinitialise the current register cache
899 *
900 * @map: Register map to operate on.
901 * @config: New configuration. Only the cache data will be used.
902 *
903 * Discard any existing register cache for the map and initialize a
904 * new cache. This can be used to restore the cache to defaults or to
905 * update the cache configuration to reflect runtime discovery of the
906 * hardware.
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100907 *
908 * No explicit locking is done here, the user needs to ensure that
909 * this function will not race with other calls to regmap.
Mark Brownbf315172011-12-03 17:06:20 +0000910 */
911int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
912{
Mark Brownbf315172011-12-03 17:06:20 +0000913 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000914 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000915
916 map->max_register = config->max_register;
917 map->writeable_reg = config->writeable_reg;
918 map->readable_reg = config->readable_reg;
919 map->volatile_reg = config->volatile_reg;
920 map->precious_reg = config->precious_reg;
921 map->cache_type = config->cache_type;
922
Stephen Warrend3c242e2012-04-04 15:48:29 -0600923 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +0000924
Mark Brown421e8d22012-01-20 13:39:37 +0000925 map->cache_bypass = false;
926 map->cache_only = false;
927
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100928 return regcache_init(map, config);
Mark Brownbf315172011-12-03 17:06:20 +0000929}
Mark Brown752a6a52012-05-14 10:00:12 +0100930EXPORT_SYMBOL_GPL(regmap_reinit_cache);
Mark Brownbf315172011-12-03 17:06:20 +0000931
932/**
Mark Brownb83a3132011-05-11 19:59:58 +0200933 * regmap_exit(): Free a previously allocated register map
934 */
935void regmap_exit(struct regmap *map)
936{
Mark Brown7e09a972013-10-07 23:00:24 +0100937 struct regmap_async *async;
938
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100939 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100940 regmap_debugfs_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100941 regmap_range_exit(map);
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800942 if (map->bus && map->bus->free_context)
Stephen Warren0135bbc2012-04-04 15:48:30 -0600943 map->bus->free_context(map->bus_context);
Mark Brownb83a3132011-05-11 19:59:58 +0200944 kfree(map->work_buf);
Mark Brown7e09a972013-10-07 23:00:24 +0100945 while (!list_empty(&map->async_free)) {
946 async = list_first_entry_or_null(&map->async_free,
947 struct regmap_async,
948 list);
949 list_del(&async->list);
950 kfree(async->work_buf);
951 kfree(async);
952 }
Mark Brownb83a3132011-05-11 19:59:58 +0200953 kfree(map);
954}
955EXPORT_SYMBOL_GPL(regmap_exit);
956
Mark Brown72b39f62012-05-08 17:44:40 +0100957static int dev_get_regmap_match(struct device *dev, void *res, void *data)
958{
959 struct regmap **r = res;
960 if (!r || !*r) {
961 WARN_ON(!r || !*r);
962 return 0;
963 }
964
965 /* If the user didn't specify a name match any */
966 if (data)
967 return (*r)->name == data;
968 else
969 return 1;
970}
971
972/**
973 * dev_get_regmap(): Obtain the regmap (if any) for a device
974 *
975 * @dev: Device to retrieve the map for
976 * @name: Optional name for the register map, usually NULL.
977 *
978 * Returns the regmap for the device if one is present, or NULL. If
979 * name is specified then it must match the name specified when
980 * registering the device, if it is NULL then the first regmap found
981 * will be used. Devices with multiple register maps are very rare,
982 * generic code should normally not need to specify a name.
983 */
984struct regmap *dev_get_regmap(struct device *dev, const char *name)
985{
986 struct regmap **r = devres_find(dev, dev_get_regmap_release,
987 dev_get_regmap_match, (void *)name);
988
989 if (!r)
990 return NULL;
991 return *r;
992}
993EXPORT_SYMBOL_GPL(dev_get_regmap);
994
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100995static int _regmap_select_page(struct regmap *map, unsigned int *reg,
Mark Brown98bc7df2012-10-04 17:31:11 +0100996 struct regmap_range_node *range,
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100997 unsigned int val_num)
998{
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100999 void *orig_work_buf;
1000 unsigned int win_offset;
1001 unsigned int win_page;
1002 bool page_chg;
1003 int ret;
1004
Mark Brown98bc7df2012-10-04 17:31:11 +01001005 win_offset = (*reg - range->range_min) % range->window_len;
1006 win_page = (*reg - range->range_min) / range->window_len;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001007
Mark Brown98bc7df2012-10-04 17:31:11 +01001008 if (val_num > 1) {
1009 /* Bulk write shouldn't cross range boundary */
1010 if (*reg + val_num - 1 > range->range_max)
1011 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001012
Mark Brown98bc7df2012-10-04 17:31:11 +01001013 /* ... or single page boundary */
1014 if (val_num > range->window_len - win_offset)
1015 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001016 }
1017
Mark Brown98bc7df2012-10-04 17:31:11 +01001018 /* It is possible to have selector register inside data window.
1019 In that case, selector register is located on every page and
1020 it needs no page switching, when accessed alone. */
1021 if (val_num > 1 ||
1022 range->window_start + win_offset != range->selector_reg) {
1023 /* Use separate work_buf during page switching */
1024 orig_work_buf = map->work_buf;
1025 map->work_buf = map->selector_work_buf;
1026
1027 ret = _regmap_update_bits(map, range->selector_reg,
1028 range->selector_mask,
1029 win_page << range->selector_shift,
1030 &page_chg);
1031
1032 map->work_buf = orig_work_buf;
1033
Mark Brown0ff3e622012-10-04 17:39:13 +01001034 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001035 return ret;
1036 }
1037
1038 *reg = range->window_start + win_offset;
1039
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001040 return 0;
1041}
1042
Mark Brown584de322013-03-13 19:19:34 +00001043int _regmap_raw_write(struct regmap *map, unsigned int reg,
Mark Brown0a819802013-10-09 12:28:52 +01001044 const void *val, size_t val_len)
Mark Brownb83a3132011-05-11 19:59:58 +02001045{
Mark Brown98bc7df2012-10-04 17:31:11 +01001046 struct regmap_range_node *range;
Mark Brown0d509f22013-01-27 22:07:38 +08001047 unsigned long flags;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001048 u8 *u8 = map->work_buf;
Mark Brown0d509f22013-01-27 22:07:38 +08001049 void *work_val = map->work_buf + map->format.reg_bytes +
1050 map->format.pad_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +02001051 void *buf;
1052 int ret = -ENOTSUPP;
1053 size_t len;
Mark Brown73304782011-07-24 11:46:20 +01001054 int i;
1055
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001056 WARN_ON(!map->bus);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001057
Mark Brown73304782011-07-24 11:46:20 +01001058 /* Check for unwritable registers before we start */
1059 if (map->writeable_reg)
1060 for (i = 0; i < val_len / map->format.val_bytes; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -06001061 if (!map->writeable_reg(map->dev,
1062 reg + (i * map->reg_stride)))
Mark Brown73304782011-07-24 11:46:20 +01001063 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001064
Laxman Dewanganc9157192012-02-10 21:30:27 +05301065 if (!map->cache_bypass && map->format.parse_val) {
1066 unsigned int ival;
1067 int val_bytes = map->format.val_bytes;
1068 for (i = 0; i < val_len / val_bytes; i++) {
Stephen Warren5a08d152013-03-20 17:02:02 -06001069 ival = map->format.parse_val(val + (i * val_bytes));
Stephen Warrenf01ee602012-04-09 13:40:24 -06001070 ret = regcache_write(map, reg + (i * map->reg_stride),
1071 ival);
Laxman Dewanganc9157192012-02-10 21:30:27 +05301072 if (ret) {
1073 dev_err(map->dev,
Mark Brown6d04b8a2012-10-26 19:05:32 +01001074 "Error in caching of register: %x ret: %d\n",
Laxman Dewanganc9157192012-02-10 21:30:27 +05301075 reg + i, ret);
1076 return ret;
1077 }
1078 }
1079 if (map->cache_only) {
1080 map->cache_dirty = true;
1081 return 0;
1082 }
1083 }
1084
Mark Brown98bc7df2012-10-04 17:31:11 +01001085 range = _regmap_range_lookup(map, reg);
1086 if (range) {
Mark Brown8a2ceac2012-10-04 18:20:18 +01001087 int val_num = val_len / map->format.val_bytes;
1088 int win_offset = (reg - range->range_min) % range->window_len;
1089 int win_residue = range->window_len - win_offset;
1090
1091 /* If the write goes beyond the end of the window split it */
1092 while (val_num > win_residue) {
Fabio Estevam1a61cfe2012-10-25 14:07:18 -02001093 dev_dbg(map->dev, "Writing window %d/%zu\n",
Mark Brown8a2ceac2012-10-04 18:20:18 +01001094 win_residue, val_len / map->format.val_bytes);
1095 ret = _regmap_raw_write(map, reg, val, win_residue *
Mark Brown0a819802013-10-09 12:28:52 +01001096 map->format.val_bytes);
Mark Brown8a2ceac2012-10-04 18:20:18 +01001097 if (ret != 0)
1098 return ret;
1099
1100 reg += win_residue;
1101 val_num -= win_residue;
1102 val += win_residue * map->format.val_bytes;
1103 val_len -= win_residue * map->format.val_bytes;
1104
1105 win_offset = (reg - range->range_min) %
1106 range->window_len;
1107 win_residue = range->window_len - win_offset;
1108 }
1109
1110 ret = _regmap_select_page(map, &reg, range, val_num);
Mark Brown0ff3e622012-10-04 17:39:13 +01001111 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001112 return ret;
1113 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001114
Marc Reillyd939fb92012-03-16 12:11:43 +11001115 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001116
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001117 u8[0] |= map->write_flag_mask;
1118
Mark Brown651e0132013-10-08 18:37:36 +01001119 /*
1120 * Essentially all I/O mechanisms will be faster with a single
1121 * buffer to write. Since register syncs often generate raw
1122 * writes of single registers optimise that case.
1123 */
1124 if (val != work_val && val_len == map->format.val_bytes) {
1125 memcpy(work_val, val, map->format.val_bytes);
1126 val = work_val;
1127 }
1128
Mark Brown0a819802013-10-09 12:28:52 +01001129 if (map->async && map->bus->async_write) {
Mark Brown7e09a972013-10-07 23:00:24 +01001130 struct regmap_async *async;
Mark Brown0d509f22013-01-27 22:07:38 +08001131
Mark Brownfe7d4cc2013-02-21 19:05:48 +00001132 trace_regmap_async_write_start(map->dev, reg, val_len);
1133
Mark Brown7e09a972013-10-07 23:00:24 +01001134 spin_lock_irqsave(&map->async_lock, flags);
1135 async = list_first_entry_or_null(&map->async_free,
1136 struct regmap_async,
1137 list);
1138 if (async)
1139 list_del(&async->list);
1140 spin_unlock_irqrestore(&map->async_lock, flags);
1141
1142 if (!async) {
1143 async = map->bus->async_alloc();
1144 if (!async)
1145 return -ENOMEM;
1146
1147 async->work_buf = kzalloc(map->format.buf_size,
1148 GFP_KERNEL | GFP_DMA);
1149 if (!async->work_buf) {
1150 kfree(async);
1151 return -ENOMEM;
1152 }
Mark Brown0d509f22013-01-27 22:07:38 +08001153 }
1154
Mark Brown0d509f22013-01-27 22:07:38 +08001155 async->map = map;
1156
1157 /* If the caller supplied the value we can use it safely. */
1158 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1159 map->format.reg_bytes + map->format.val_bytes);
1160 if (val == work_val)
1161 val = async->work_buf + map->format.pad_bytes +
1162 map->format.reg_bytes;
1163
1164 spin_lock_irqsave(&map->async_lock, flags);
1165 list_add_tail(&async->list, &map->async_list);
1166 spin_unlock_irqrestore(&map->async_lock, flags);
1167
1168 ret = map->bus->async_write(map->bus_context, async->work_buf,
1169 map->format.reg_bytes +
1170 map->format.pad_bytes,
1171 val, val_len, async);
1172
1173 if (ret != 0) {
1174 dev_err(map->dev, "Failed to schedule write: %d\n",
1175 ret);
1176
1177 spin_lock_irqsave(&map->async_lock, flags);
Mark Brown7e09a972013-10-07 23:00:24 +01001178 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08001179 spin_unlock_irqrestore(&map->async_lock, flags);
Mark Brown0d509f22013-01-27 22:07:38 +08001180 }
Mark Brownf951b652013-03-27 13:08:44 +00001181
1182 return ret;
Mark Brown0d509f22013-01-27 22:07:38 +08001183 }
1184
Mark Brownfb2736b2011-07-24 21:30:55 +01001185 trace_regmap_hw_write_start(map->dev, reg,
1186 val_len / map->format.val_bytes);
1187
Mark Brown2547e202011-07-20 21:47:22 +01001188 /* If we're doing a single register write we can probably just
1189 * send the work_buf directly, otherwise try to do a gather
1190 * write.
1191 */
Mark Brown0d509f22013-01-27 22:07:38 +08001192 if (val == work_val)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001193 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001194 map->format.reg_bytes +
1195 map->format.pad_bytes +
1196 val_len);
Mark Brown2547e202011-07-20 21:47:22 +01001197 else if (map->bus->gather_write)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001198 ret = map->bus->gather_write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001199 map->format.reg_bytes +
1200 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +02001201 val, val_len);
1202
Mark Brown2547e202011-07-20 21:47:22 +01001203 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +02001204 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +00001205 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1206 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +02001207 if (!buf)
1208 return -ENOMEM;
1209
1210 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +00001211 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1212 val, val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -06001213 ret = map->bus->write(map->bus_context, buf, len);
Mark Brownb83a3132011-05-11 19:59:58 +02001214
1215 kfree(buf);
1216 }
1217
Mark Brownfb2736b2011-07-24 21:30:55 +01001218 trace_regmap_hw_write_done(map->dev, reg,
1219 val_len / map->format.val_bytes);
1220
Mark Brownb83a3132011-05-11 19:59:58 +02001221 return ret;
1222}
1223
Mark Brown221ad7f2013-03-26 21:24:20 +00001224/**
1225 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1226 *
1227 * @map: Map to check.
1228 */
1229bool regmap_can_raw_write(struct regmap *map)
1230{
1231 return map->bus && map->format.format_val && map->format.format_reg;
1232}
1233EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1234
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001235static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1236 unsigned int val)
1237{
1238 int ret;
1239 struct regmap_range_node *range;
1240 struct regmap *map = context;
1241
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001242 WARN_ON(!map->bus || !map->format.format_write);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001243
1244 range = _regmap_range_lookup(map, reg);
1245 if (range) {
1246 ret = _regmap_select_page(map, &reg, range, 1);
1247 if (ret != 0)
1248 return ret;
1249 }
1250
1251 map->format.format_write(map, reg, val);
1252
1253 trace_regmap_hw_write_start(map->dev, reg, 1);
1254
1255 ret = map->bus->write(map->bus_context, map->work_buf,
1256 map->format.buf_size);
1257
1258 trace_regmap_hw_write_done(map->dev, reg, 1);
1259
1260 return ret;
1261}
1262
1263static int _regmap_bus_raw_write(void *context, unsigned int reg,
1264 unsigned int val)
1265{
1266 struct regmap *map = context;
1267
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001268 WARN_ON(!map->bus || !map->format.format_val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001269
1270 map->format.format_val(map->work_buf + map->format.reg_bytes
1271 + map->format.pad_bytes, val, 0);
1272 return _regmap_raw_write(map, reg,
1273 map->work_buf +
1274 map->format.reg_bytes +
1275 map->format.pad_bytes,
Mark Brown0a819802013-10-09 12:28:52 +01001276 map->format.val_bytes);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001277}
1278
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001279static inline void *_regmap_map_get_context(struct regmap *map)
1280{
1281 return (map->bus) ? map : map->bus_context;
1282}
1283
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +01001284int _regmap_write(struct regmap *map, unsigned int reg,
1285 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +02001286{
Mark Brownfb2736b2011-07-24 21:30:55 +01001287 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001288 void *context = _regmap_map_get_context(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001289
Ionut Nicu515f2262013-08-09 12:09:20 +02001290 if (!regmap_writeable(map, reg))
1291 return -EIO;
1292
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001293 if (!map->cache_bypass && !map->defer_caching) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001294 ret = regcache_write(map, reg, val);
1295 if (ret != 0)
1296 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001297 if (map->cache_only) {
1298 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001299 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001300 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001301 }
1302
Mark Brown1044c182012-07-06 14:10:23 +01001303#ifdef LOG_DEVICE
1304 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1305 dev_info(map->dev, "%x <= %x\n", reg, val);
1306#endif
1307
Mark Brownfb2736b2011-07-24 21:30:55 +01001308 trace_regmap_reg_write(map->dev, reg, val);
1309
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001310 return map->reg_write(context, reg, val);
Mark Brownb83a3132011-05-11 19:59:58 +02001311}
1312
1313/**
1314 * regmap_write(): Write a value to a single register
1315 *
1316 * @map: Register map to write to
1317 * @reg: Register to write to
1318 * @val: Value to be written
1319 *
1320 * A value of zero will be returned on success, a negative errno will
1321 * be returned in error cases.
1322 */
1323int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1324{
1325 int ret;
1326
Stephen Warrenf01ee602012-04-09 13:40:24 -06001327 if (reg % map->reg_stride)
1328 return -EINVAL;
1329
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001330 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001331
1332 ret = _regmap_write(map, reg, val);
1333
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001334 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001335
1336 return ret;
1337}
1338EXPORT_SYMBOL_GPL(regmap_write);
1339
1340/**
Mark Brown915f4412013-10-09 13:30:10 +01001341 * regmap_write_async(): Write a value to a single register asynchronously
1342 *
1343 * @map: Register map to write to
1344 * @reg: Register to write to
1345 * @val: Value to be written
1346 *
1347 * A value of zero will be returned on success, a negative errno will
1348 * be returned in error cases.
1349 */
1350int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1351{
1352 int ret;
1353
1354 if (reg % map->reg_stride)
1355 return -EINVAL;
1356
1357 map->lock(map->lock_arg);
1358
1359 map->async = true;
1360
1361 ret = _regmap_write(map, reg, val);
1362
1363 map->async = false;
1364
1365 map->unlock(map->lock_arg);
1366
1367 return ret;
1368}
1369EXPORT_SYMBOL_GPL(regmap_write_async);
1370
1371/**
Mark Brownb83a3132011-05-11 19:59:58 +02001372 * regmap_raw_write(): Write raw values to one or more registers
1373 *
1374 * @map: Register map to write to
1375 * @reg: Initial register to write to
1376 * @val: Block of data to be written, laid out for direct transmission to the
1377 * device
1378 * @val_len: Length of data pointed to by val.
1379 *
1380 * This function is intended to be used for things like firmware
1381 * download where a large block of data needs to be transferred to the
1382 * device. No formatting will be done on the data provided.
1383 *
1384 * A value of zero will be returned on success, a negative errno will
1385 * be returned in error cases.
1386 */
1387int regmap_raw_write(struct regmap *map, unsigned int reg,
1388 const void *val, size_t val_len)
1389{
1390 int ret;
1391
Mark Brown221ad7f2013-03-26 21:24:20 +00001392 if (!regmap_can_raw_write(map))
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001393 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001394 if (val_len % map->format.val_bytes)
1395 return -EINVAL;
1396
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001397 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001398
Mark Brown0a819802013-10-09 12:28:52 +01001399 ret = _regmap_raw_write(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001400
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001401 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001402
1403 return ret;
1404}
1405EXPORT_SYMBOL_GPL(regmap_raw_write);
1406
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001407/**
1408 * regmap_field_write(): Write a value to a single register field
1409 *
1410 * @field: Register field to write to
1411 * @val: Value to be written
1412 *
1413 * A value of zero will be returned on success, a negative errno will
1414 * be returned in error cases.
1415 */
1416int regmap_field_write(struct regmap_field *field, unsigned int val)
1417{
1418 return regmap_update_bits(field->regmap, field->reg,
1419 field->mask, val << field->shift);
1420}
1421EXPORT_SYMBOL_GPL(regmap_field_write);
1422
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301423/*
1424 * regmap_bulk_write(): Write multiple registers to the device
1425 *
1426 * @map: Register map to write to
1427 * @reg: First register to be write from
1428 * @val: Block of data to be written, in native register size for device
1429 * @val_count: Number of registers to write
1430 *
1431 * This function is intended to be used for writing a large block of
Nestor Ovroy31b35e9e2013-01-18 16:51:03 +01001432 * data to the device either in single transfer or multiple transfer.
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301433 *
1434 * A value of zero will be returned on success, a negative errno will
1435 * be returned in error cases.
1436 */
1437int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1438 size_t val_count)
1439{
1440 int ret = 0, i;
1441 size_t val_bytes = map->format.val_bytes;
1442 void *wval;
1443
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001444 if (!map->bus)
1445 return -EINVAL;
Mark Brown8a819ff2013-03-04 09:04:51 +08001446 if (!map->format.parse_inplace)
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301447 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001448 if (reg % map->reg_stride)
1449 return -EINVAL;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301450
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001451 map->lock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301452
1453 /* No formatting is require if val_byte is 1 */
1454 if (val_bytes == 1) {
1455 wval = (void *)val;
1456 } else {
1457 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1458 if (!wval) {
1459 ret = -ENOMEM;
1460 dev_err(map->dev, "Error in memory allocation\n");
1461 goto out;
1462 }
1463 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08001464 map->format.parse_inplace(wval + i);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301465 }
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001466 /*
1467 * Some devices does not support bulk write, for
1468 * them we have a series of single write operations.
1469 */
1470 if (map->use_single_rw) {
1471 for (i = 0; i < val_count; i++) {
1472 ret = regmap_raw_write(map,
Mark Brown0d509f22013-01-27 22:07:38 +08001473 reg + (i * map->reg_stride),
1474 val + (i * val_bytes),
1475 val_bytes);
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001476 if (ret != 0)
1477 return ret;
1478 }
1479 } else {
Mark Brown0a819802013-10-09 12:28:52 +01001480 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001481 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301482
1483 if (val_bytes != 1)
1484 kfree(wval);
1485
1486out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001487 map->unlock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301488 return ret;
1489}
1490EXPORT_SYMBOL_GPL(regmap_bulk_write);
1491
Mark Brown0d509f22013-01-27 22:07:38 +08001492/**
1493 * regmap_raw_write_async(): Write raw values to one or more registers
1494 * asynchronously
1495 *
1496 * @map: Register map to write to
1497 * @reg: Initial register to write to
1498 * @val: Block of data to be written, laid out for direct transmission to the
1499 * device. Must be valid until regmap_async_complete() is called.
1500 * @val_len: Length of data pointed to by val.
1501 *
1502 * This function is intended to be used for things like firmware
1503 * download where a large block of data needs to be transferred to the
1504 * device. No formatting will be done on the data provided.
1505 *
1506 * If supported by the underlying bus the write will be scheduled
1507 * asynchronously, helping maximise I/O speed on higher speed buses
1508 * like SPI. regmap_async_complete() can be called to ensure that all
1509 * asynchrnous writes have been completed.
1510 *
1511 * A value of zero will be returned on success, a negative errno will
1512 * be returned in error cases.
1513 */
1514int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1515 const void *val, size_t val_len)
1516{
1517 int ret;
1518
1519 if (val_len % map->format.val_bytes)
1520 return -EINVAL;
1521 if (reg % map->reg_stride)
1522 return -EINVAL;
1523
1524 map->lock(map->lock_arg);
1525
Mark Brown0a819802013-10-09 12:28:52 +01001526 map->async = true;
1527
1528 ret = _regmap_raw_write(map, reg, val, val_len);
1529
1530 map->async = false;
Mark Brown0d509f22013-01-27 22:07:38 +08001531
1532 map->unlock(map->lock_arg);
1533
1534 return ret;
1535}
1536EXPORT_SYMBOL_GPL(regmap_raw_write_async);
1537
Mark Brownb83a3132011-05-11 19:59:58 +02001538static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1539 unsigned int val_len)
1540{
Mark Brown98bc7df2012-10-04 17:31:11 +01001541 struct regmap_range_node *range;
Mark Brownb83a3132011-05-11 19:59:58 +02001542 u8 *u8 = map->work_buf;
1543 int ret;
1544
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001545 WARN_ON(!map->bus);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001546
Mark Brown98bc7df2012-10-04 17:31:11 +01001547 range = _regmap_range_lookup(map, reg);
1548 if (range) {
1549 ret = _regmap_select_page(map, &reg, range,
1550 val_len / map->format.val_bytes);
Mark Brown0ff3e622012-10-04 17:39:13 +01001551 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001552 return ret;
1553 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001554
Marc Reillyd939fb92012-03-16 12:11:43 +11001555 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001556
1557 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001558 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +02001559 * register addresss; since it's always the high bits for all
1560 * current formats we can do this here rather than in
1561 * formatting. This may break if we get interesting formats.
1562 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001563 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001564
Mark Brownfb2736b2011-07-24 21:30:55 +01001565 trace_regmap_hw_read_start(map->dev, reg,
1566 val_len / map->format.val_bytes);
1567
Stephen Warren0135bbc2012-04-04 15:48:30 -06001568 ret = map->bus->read(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001569 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +01001570 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001571
Mark Brownfb2736b2011-07-24 21:30:55 +01001572 trace_regmap_hw_read_done(map->dev, reg,
1573 val_len / map->format.val_bytes);
1574
1575 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001576}
1577
Andrey Smirnovad278402013-01-12 12:54:12 -08001578static int _regmap_bus_read(void *context, unsigned int reg,
1579 unsigned int *val)
1580{
1581 int ret;
1582 struct regmap *map = context;
1583
1584 if (!map->format.parse_val)
1585 return -EINVAL;
1586
1587 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
1588 if (ret == 0)
1589 *val = map->format.parse_val(map->work_buf);
1590
1591 return ret;
1592}
1593
Mark Brownb83a3132011-05-11 19:59:58 +02001594static int _regmap_read(struct regmap *map, unsigned int reg,
1595 unsigned int *val)
1596{
1597 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001598 void *context = _regmap_map_get_context(map);
1599
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001600 WARN_ON(!map->reg_read);
Mark Brownb83a3132011-05-11 19:59:58 +02001601
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001602 if (!map->cache_bypass) {
1603 ret = regcache_read(map, reg, val);
1604 if (ret == 0)
1605 return 0;
1606 }
1607
1608 if (map->cache_only)
1609 return -EBUSY;
1610
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001611 ret = map->reg_read(context, reg, val);
Mark Brownfb2736b2011-07-24 21:30:55 +01001612 if (ret == 0) {
Mark Brown1044c182012-07-06 14:10:23 +01001613#ifdef LOG_DEVICE
1614 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1615 dev_info(map->dev, "%x => %x\n", reg, *val);
1616#endif
1617
Mark Brownfb2736b2011-07-24 21:30:55 +01001618 trace_regmap_reg_read(map->dev, reg, *val);
Mark Brownb83a3132011-05-11 19:59:58 +02001619
Andrey Smirnovad278402013-01-12 12:54:12 -08001620 if (!map->cache_bypass)
1621 regcache_write(map, reg, *val);
1622 }
Mark Brownf2985362012-04-30 21:25:05 +01001623
Mark Brownb83a3132011-05-11 19:59:58 +02001624 return ret;
1625}
1626
1627/**
1628 * regmap_read(): Read a value from a single register
1629 *
1630 * @map: Register map to write to
1631 * @reg: Register to be read from
1632 * @val: Pointer to store read value
1633 *
1634 * A value of zero will be returned on success, a negative errno will
1635 * be returned in error cases.
1636 */
1637int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1638{
1639 int ret;
1640
Stephen Warrenf01ee602012-04-09 13:40:24 -06001641 if (reg % map->reg_stride)
1642 return -EINVAL;
1643
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001644 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001645
1646 ret = _regmap_read(map, reg, val);
1647
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001648 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001649
1650 return ret;
1651}
1652EXPORT_SYMBOL_GPL(regmap_read);
1653
1654/**
1655 * regmap_raw_read(): Read raw data from the device
1656 *
1657 * @map: Register map to write to
1658 * @reg: First register to be read from
1659 * @val: Pointer to store read value
1660 * @val_len: Size of data to read
1661 *
1662 * A value of zero will be returned on success, a negative errno will
1663 * be returned in error cases.
1664 */
1665int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1666 size_t val_len)
1667{
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001668 size_t val_bytes = map->format.val_bytes;
1669 size_t val_count = val_len / val_bytes;
1670 unsigned int v;
1671 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +01001672
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001673 if (!map->bus)
1674 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001675 if (val_len % map->format.val_bytes)
1676 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001677 if (reg % map->reg_stride)
1678 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001679
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001680 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001681
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001682 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1683 map->cache_type == REGCACHE_NONE) {
1684 /* Physical block read if there's no cache involved */
1685 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001686
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001687 } else {
1688 /* Otherwise go word by word for the cache; should be low
1689 * cost as we expect to hit the cache.
1690 */
1691 for (i = 0; i < val_count; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -06001692 ret = _regmap_read(map, reg + (i * map->reg_stride),
1693 &v);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001694 if (ret != 0)
1695 goto out;
1696
Marc Reillyd939fb92012-03-16 12:11:43 +11001697 map->format.format_val(val + (i * val_bytes), v, 0);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001698 }
1699 }
1700
1701 out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001702 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001703
1704 return ret;
1705}
1706EXPORT_SYMBOL_GPL(regmap_raw_read);
1707
1708/**
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001709 * regmap_field_read(): Read a value to a single register field
1710 *
1711 * @field: Register field to read from
1712 * @val: Pointer to store read value
1713 *
1714 * A value of zero will be returned on success, a negative errno will
1715 * be returned in error cases.
1716 */
1717int regmap_field_read(struct regmap_field *field, unsigned int *val)
1718{
1719 int ret;
1720 unsigned int reg_val;
1721 ret = regmap_read(field->regmap, field->reg, &reg_val);
1722 if (ret != 0)
1723 return ret;
1724
1725 reg_val &= field->mask;
1726 reg_val >>= field->shift;
1727 *val = reg_val;
1728
1729 return ret;
1730}
1731EXPORT_SYMBOL_GPL(regmap_field_read);
1732
1733/**
Mark Brownb83a3132011-05-11 19:59:58 +02001734 * regmap_bulk_read(): Read multiple registers from the device
1735 *
1736 * @map: Register map to write to
1737 * @reg: First register to be read from
1738 * @val: Pointer to store read value, in native register size for device
1739 * @val_count: Number of registers to read
1740 *
1741 * A value of zero will be returned on success, a negative errno will
1742 * be returned in error cases.
1743 */
1744int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1745 size_t val_count)
1746{
1747 int ret, i;
1748 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +01001749 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001750
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001751 if (!map->bus)
1752 return -EINVAL;
Mark Brown8a819ff2013-03-04 09:04:51 +08001753 if (!map->format.parse_inplace)
Mark Brownb83a3132011-05-11 19:59:58 +02001754 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001755 if (reg % map->reg_stride)
1756 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001757
Mark Brownde2d8082011-10-10 13:24:52 +01001758 if (vol || map->cache_type == REGCACHE_NONE) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001759 /*
1760 * Some devices does not support bulk read, for
1761 * them we have a series of single read operations.
1762 */
1763 if (map->use_single_rw) {
1764 for (i = 0; i < val_count; i++) {
1765 ret = regmap_raw_read(map,
1766 reg + (i * map->reg_stride),
1767 val + (i * val_bytes),
1768 val_bytes);
1769 if (ret != 0)
1770 return ret;
1771 }
1772 } else {
1773 ret = regmap_raw_read(map, reg, val,
1774 val_bytes * val_count);
1775 if (ret != 0)
1776 return ret;
1777 }
Mark Brownde2d8082011-10-10 13:24:52 +01001778
1779 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08001780 map->format.parse_inplace(val + i);
Mark Brownde2d8082011-10-10 13:24:52 +01001781 } else {
1782 for (i = 0; i < val_count; i++) {
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301783 unsigned int ival;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001784 ret = regmap_read(map, reg + (i * map->reg_stride),
Mark Brown25061d22012-05-12 13:06:08 +01001785 &ival);
Mark Brownde2d8082011-10-10 13:24:52 +01001786 if (ret != 0)
1787 return ret;
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301788 memcpy(val + (i * val_bytes), &ival, val_bytes);
Mark Brownde2d8082011-10-10 13:24:52 +01001789 }
1790 }
Mark Brownb83a3132011-05-11 19:59:58 +02001791
1792 return 0;
1793}
1794EXPORT_SYMBOL_GPL(regmap_bulk_read);
1795
Mark Brown018690d2011-11-29 20:10:36 +00001796static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1797 unsigned int mask, unsigned int val,
1798 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +02001799{
1800 int ret;
Mark Brownd91e8db22011-11-18 16:03:50 +00001801 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +02001802
Mark Brownd91e8db22011-11-18 16:03:50 +00001803 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +02001804 if (ret != 0)
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001805 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001806
Mark Brownd91e8db22011-11-18 16:03:50 +00001807 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001808 tmp |= val & mask;
1809
Mark Brown018690d2011-11-29 20:10:36 +00001810 if (tmp != orig) {
Mark Brownd91e8db22011-11-18 16:03:50 +00001811 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +00001812 *change = true;
1813 } else {
1814 *change = false;
1815 }
Mark Brownb83a3132011-05-11 19:59:58 +02001816
Mark Brownb83a3132011-05-11 19:59:58 +02001817 return ret;
1818}
Mark Brown018690d2011-11-29 20:10:36 +00001819
1820/**
1821 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1822 *
1823 * @map: Register map to update
1824 * @reg: Register to update
1825 * @mask: Bitmask to change
1826 * @val: New value for bitmask
1827 *
1828 * Returns zero for success, a negative number on error.
1829 */
1830int regmap_update_bits(struct regmap *map, unsigned int reg,
1831 unsigned int mask, unsigned int val)
1832{
1833 bool change;
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001834 int ret;
1835
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001836 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001837 ret = _regmap_update_bits(map, reg, mask, val, &change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001838 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001839
1840 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00001841}
Mark Brownb83a3132011-05-11 19:59:58 +02001842EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +01001843
Mark Brown018690d2011-11-29 20:10:36 +00001844/**
Mark Brown915f4412013-10-09 13:30:10 +01001845 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
1846 * map asynchronously
1847 *
1848 * @map: Register map to update
1849 * @reg: Register to update
1850 * @mask: Bitmask to change
1851 * @val: New value for bitmask
1852 *
1853 * With most buses the read must be done synchronously so this is most
1854 * useful for devices with a cache which do not need to interact with
1855 * the hardware to determine the current register value.
1856 *
1857 * Returns zero for success, a negative number on error.
1858 */
1859int regmap_update_bits_async(struct regmap *map, unsigned int reg,
1860 unsigned int mask, unsigned int val)
1861{
1862 bool change;
1863 int ret;
1864
1865 map->lock(map->lock_arg);
1866
1867 map->async = true;
1868
1869 ret = _regmap_update_bits(map, reg, mask, val, &change);
1870
1871 map->async = false;
1872
1873 map->unlock(map->lock_arg);
1874
1875 return ret;
1876}
1877EXPORT_SYMBOL_GPL(regmap_update_bits_async);
1878
1879/**
Mark Brown018690d2011-11-29 20:10:36 +00001880 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1881 * register map and report if updated
1882 *
1883 * @map: Register map to update
1884 * @reg: Register to update
1885 * @mask: Bitmask to change
1886 * @val: New value for bitmask
1887 * @change: Boolean indicating if a write was done
1888 *
1889 * Returns zero for success, a negative number on error.
1890 */
1891int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1892 unsigned int mask, unsigned int val,
1893 bool *change)
1894{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001895 int ret;
1896
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001897 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001898 ret = _regmap_update_bits(map, reg, mask, val, change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001899 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001900 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00001901}
1902EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1903
Mark Brown915f4412013-10-09 13:30:10 +01001904/**
1905 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
1906 * register map asynchronously and report if
1907 * updated
1908 *
1909 * @map: Register map to update
1910 * @reg: Register to update
1911 * @mask: Bitmask to change
1912 * @val: New value for bitmask
1913 * @change: Boolean indicating if a write was done
1914 *
1915 * With most buses the read must be done synchronously so this is most
1916 * useful for devices with a cache which do not need to interact with
1917 * the hardware to determine the current register value.
1918 *
1919 * Returns zero for success, a negative number on error.
1920 */
1921int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
1922 unsigned int mask, unsigned int val,
1923 bool *change)
1924{
1925 int ret;
1926
1927 map->lock(map->lock_arg);
1928
1929 map->async = true;
1930
1931 ret = _regmap_update_bits(map, reg, mask, val, change);
1932
1933 map->async = false;
1934
1935 map->unlock(map->lock_arg);
1936
1937 return ret;
1938}
1939EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
1940
Mark Brown0d509f22013-01-27 22:07:38 +08001941void regmap_async_complete_cb(struct regmap_async *async, int ret)
1942{
1943 struct regmap *map = async->map;
1944 bool wake;
1945
Mark Brownfe7d4cc2013-02-21 19:05:48 +00001946 trace_regmap_async_io_complete(map->dev);
1947
Mark Brown0d509f22013-01-27 22:07:38 +08001948 spin_lock(&map->async_lock);
Mark Brown7e09a972013-10-07 23:00:24 +01001949 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08001950 wake = list_empty(&map->async_list);
1951
1952 if (ret != 0)
1953 map->async_ret = ret;
1954
1955 spin_unlock(&map->async_lock);
1956
Mark Brown0d509f22013-01-27 22:07:38 +08001957 if (wake)
1958 wake_up(&map->async_waitq);
1959}
Axel Linf804fb52013-02-03 00:14:13 +08001960EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
Mark Brown0d509f22013-01-27 22:07:38 +08001961
1962static int regmap_async_is_done(struct regmap *map)
1963{
1964 unsigned long flags;
1965 int ret;
1966
1967 spin_lock_irqsave(&map->async_lock, flags);
1968 ret = list_empty(&map->async_list);
1969 spin_unlock_irqrestore(&map->async_lock, flags);
1970
1971 return ret;
1972}
1973
1974/**
1975 * regmap_async_complete: Ensure all asynchronous I/O has completed.
1976 *
1977 * @map: Map to operate on.
1978 *
1979 * Blocks until any pending asynchronous I/O has completed. Returns
1980 * an error code for any failed I/O operations.
1981 */
1982int regmap_async_complete(struct regmap *map)
1983{
1984 unsigned long flags;
1985 int ret;
1986
1987 /* Nothing to do with no async support */
Daniel Mackf2e055e2013-07-04 13:11:03 +02001988 if (!map->bus || !map->bus->async_write)
Mark Brown0d509f22013-01-27 22:07:38 +08001989 return 0;
1990
Mark Brownfe7d4cc2013-02-21 19:05:48 +00001991 trace_regmap_async_complete_start(map->dev);
1992
Mark Brown0d509f22013-01-27 22:07:38 +08001993 wait_event(map->async_waitq, regmap_async_is_done(map));
1994
1995 spin_lock_irqsave(&map->async_lock, flags);
1996 ret = map->async_ret;
1997 map->async_ret = 0;
1998 spin_unlock_irqrestore(&map->async_lock, flags);
1999
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002000 trace_regmap_async_complete_done(map->dev);
2001
Mark Brown0d509f22013-01-27 22:07:38 +08002002 return ret;
2003}
Mark Brownf88948e2013-02-05 13:53:26 +00002004EXPORT_SYMBOL_GPL(regmap_async_complete);
Mark Brown0d509f22013-01-27 22:07:38 +08002005
Mark Brown22f0d902012-01-21 12:01:14 +00002006/**
2007 * regmap_register_patch: Register and apply register updates to be applied
2008 * on device initialistion
2009 *
2010 * @map: Register map to apply updates to.
2011 * @regs: Values to update.
2012 * @num_regs: Number of entries in regs.
2013 *
2014 * Register a set of register updates to be applied to the device
2015 * whenever the device registers are synchronised with the cache and
2016 * apply them immediately. Typically this is used to apply
2017 * corrections to be applied to the device defaults on startup, such
2018 * as the updates some vendors provide to undocumented registers.
2019 */
2020int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2021 int num_regs)
2022{
Mark Brownaab13eb2013-07-11 12:41:44 +01002023 struct reg_default *p;
Mark Brown22f0d902012-01-21 12:01:14 +00002024 int i, ret;
2025 bool bypass;
2026
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002027 map->lock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00002028
2029 bypass = map->cache_bypass;
2030
2031 map->cache_bypass = true;
Mark Brown1a25f262013-10-10 20:55:03 +01002032 map->async = true;
Mark Brown22f0d902012-01-21 12:01:14 +00002033
2034 /* Write out first; it's useful to apply even if we fail later. */
2035 for (i = 0; i < num_regs; i++) {
2036 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2037 if (ret != 0) {
2038 dev_err(map->dev, "Failed to write %x = %x: %d\n",
2039 regs[i].reg, regs[i].def, ret);
2040 goto out;
2041 }
2042 }
2043
Mark Brownaab13eb2013-07-11 12:41:44 +01002044 p = krealloc(map->patch,
2045 sizeof(struct reg_default) * (map->patch_regs + num_regs),
2046 GFP_KERNEL);
2047 if (p) {
2048 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2049 map->patch = p;
2050 map->patch_regs += num_regs;
Mark Brown22f0d902012-01-21 12:01:14 +00002051 } else {
2052 ret = -ENOMEM;
2053 }
2054
2055out:
Mark Brown1a25f262013-10-10 20:55:03 +01002056 map->async = false;
Mark Brown22f0d902012-01-21 12:01:14 +00002057 map->cache_bypass = bypass;
2058
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002059 map->unlock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00002060
Mark Brown1a25f262013-10-10 20:55:03 +01002061 regmap_async_complete(map);
2062
Mark Brown22f0d902012-01-21 12:01:14 +00002063 return ret;
2064}
2065EXPORT_SYMBOL_GPL(regmap_register_patch);
2066
Mark Browneae4b512012-03-14 13:15:03 +00002067/*
Mark Browna6539c32012-02-17 14:20:14 -08002068 * regmap_get_val_bytes(): Report the size of a register value
2069 *
2070 * Report the size of a register value, mainly intended to for use by
2071 * generic infrastructure built on top of regmap.
2072 */
2073int regmap_get_val_bytes(struct regmap *map)
2074{
2075 if (map->format.format_write)
2076 return -EINVAL;
2077
2078 return map->format.val_bytes;
2079}
2080EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2081
Mark Brown31244e32011-07-20 22:56:53 +01002082static int __init regmap_initcall(void)
2083{
2084 regmap_debugfs_initcall();
2085
2086 return 0;
2087}
2088postcore_initcall(regmap_initcall);