blob: 43065ceff90f9402f03e752910eeb44c597fa122 [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
Michal Simek6cfec042014-02-10 16:22:33 +0100383int regmap_attach_dev(struct device *dev, struct regmap *map,
384 const struct regmap_config *config)
385{
386 struct regmap **m;
387
388 map->dev = dev;
389
390 regmap_debugfs_init(map, config->name);
391
392 /* Add a devres resource for dev_get_regmap() */
393 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
394 if (!m) {
395 regmap_debugfs_exit(map);
396 return -ENOMEM;
397 }
398 *m = map;
399 devres_add(dev, m);
400
401 return 0;
402}
403EXPORT_SYMBOL_GPL(regmap_attach_dev);
404
Mark Brownb83a3132011-05-11 19:59:58 +0200405/**
406 * regmap_init(): Initialise register map
407 *
408 * @dev: Device that will be interacted with
409 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600410 * @bus_context: Data passed to bus-specific callbacks
Mark Brownb83a3132011-05-11 19:59:58 +0200411 * @config: Configuration for register map
412 *
413 * The return value will be an ERR_PTR() on error or a valid pointer to
414 * a struct regmap. This function should generally not be called
415 * directly, it should be called by bus-specific init functions.
416 */
417struct regmap *regmap_init(struct device *dev,
418 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600419 void *bus_context,
Mark Brownb83a3132011-05-11 19:59:58 +0200420 const struct regmap_config *config)
421{
Michal Simek6cfec042014-02-10 16:22:33 +0100422 struct regmap *map;
Mark Brownb83a3132011-05-11 19:59:58 +0200423 int ret = -EINVAL;
Stephen Warren141eba22012-05-24 10:47:26 -0600424 enum regmap_endian reg_endian, val_endian;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100425 int i, j;
Mark Brownb83a3132011-05-11 19:59:58 +0200426
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800427 if (!config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100428 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200429
430 map = kzalloc(sizeof(*map), GFP_KERNEL);
431 if (map == NULL) {
432 ret = -ENOMEM;
433 goto err;
434 }
435
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200436 if (config->lock && config->unlock) {
437 map->lock = config->lock;
438 map->unlock = config->unlock;
439 map->lock_arg = config->lock_arg;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600440 } else {
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800441 if ((bus && bus->fast_io) ||
442 config->fast_io) {
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200443 spin_lock_init(&map->spinlock);
444 map->lock = regmap_lock_spinlock;
445 map->unlock = regmap_unlock_spinlock;
446 } else {
447 mutex_init(&map->mutex);
448 map->lock = regmap_lock_mutex;
449 map->unlock = regmap_unlock_mutex;
450 }
451 map->lock_arg = map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600452 }
Wolfram Sangc212acc2012-01-28 02:16:41 +0100453 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000454 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100455 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Fabio Estevam5494a982012-05-31 21:10:30 -0300456 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
457 config->val_bits + config->pad_bits, 8);
Marc Reillyd939fb92012-03-16 12:11:43 +1100458 map->reg_shift = config->pad_bits % 8;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600459 if (config->reg_stride)
460 map->reg_stride = config->reg_stride;
461 else
462 map->reg_stride = 1;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100463 map->use_single_rw = config->use_single_rw;
Mark Brownb83a3132011-05-11 19:59:58 +0200464 map->dev = dev;
465 map->bus = bus;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600466 map->bus_context = bus_context;
Mark Brown2e2ae662011-07-20 22:33:39 +0100467 map->max_register = config->max_register;
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100468 map->wr_table = config->wr_table;
469 map->rd_table = config->rd_table;
470 map->volatile_table = config->volatile_table;
471 map->precious_table = config->precious_table;
Mark Brown2e2ae662011-07-20 22:33:39 +0100472 map->writeable_reg = config->writeable_reg;
473 map->readable_reg = config->readable_reg;
474 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900475 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100476 map->cache_type = config->cache_type;
Mark Brown72b39f62012-05-08 17:44:40 +0100477 map->name = config->name;
Mark Brownb83a3132011-05-11 19:59:58 +0200478
Mark Brown0d509f22013-01-27 22:07:38 +0800479 spin_lock_init(&map->async_lock);
480 INIT_LIST_HEAD(&map->async_list);
Mark Brown7e09a972013-10-07 23:00:24 +0100481 INIT_LIST_HEAD(&map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +0800482 init_waitqueue_head(&map->async_waitq);
483
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200484 if (config->read_flag_mask || config->write_flag_mask) {
485 map->read_flag_mask = config->read_flag_mask;
486 map->write_flag_mask = config->write_flag_mask;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800487 } else if (bus) {
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200488 map->read_flag_mask = bus->read_flag_mask;
489 }
490
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800491 if (!bus) {
492 map->reg_read = config->reg_read;
493 map->reg_write = config->reg_write;
494
495 map->defer_caching = false;
496 goto skip_format_initialization;
497 } else {
498 map->reg_read = _regmap_bus_read;
499 }
Andrey Smirnovad278402013-01-12 12:54:12 -0800500
Stephen Warren141eba22012-05-24 10:47:26 -0600501 reg_endian = config->reg_format_endian;
502 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
503 reg_endian = bus->reg_format_endian_default;
504 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
505 reg_endian = REGMAP_ENDIAN_BIG;
506
507 val_endian = config->val_format_endian;
508 if (val_endian == REGMAP_ENDIAN_DEFAULT)
509 val_endian = bus->val_format_endian_default;
510 if (val_endian == REGMAP_ENDIAN_DEFAULT)
511 val_endian = REGMAP_ENDIAN_BIG;
512
Marc Reillyd939fb92012-03-16 12:11:43 +1100513 switch (config->reg_bits + map->reg_shift) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100514 case 2:
515 switch (config->val_bits) {
516 case 6:
517 map->format.format_write = regmap_format_2_6_write;
518 break;
519 default:
520 goto err_map;
521 }
522 break;
523
Mark Brownb83a3132011-05-11 19:59:58 +0200524 case 4:
525 switch (config->val_bits) {
526 case 12:
527 map->format.format_write = regmap_format_4_12_write;
528 break;
529 default:
530 goto err_map;
531 }
532 break;
533
534 case 7:
535 switch (config->val_bits) {
536 case 9:
537 map->format.format_write = regmap_format_7_9_write;
538 break;
539 default:
540 goto err_map;
541 }
542 break;
543
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100544 case 10:
545 switch (config->val_bits) {
546 case 14:
547 map->format.format_write = regmap_format_10_14_write;
548 break;
549 default:
550 goto err_map;
551 }
552 break;
553
Mark Brownb83a3132011-05-11 19:59:58 +0200554 case 8:
555 map->format.format_reg = regmap_format_8;
556 break;
557
558 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600559 switch (reg_endian) {
560 case REGMAP_ENDIAN_BIG:
561 map->format.format_reg = regmap_format_16_be;
562 break;
563 case REGMAP_ENDIAN_NATIVE:
564 map->format.format_reg = regmap_format_16_native;
565 break;
566 default:
567 goto err_map;
568 }
Mark Brownb83a3132011-05-11 19:59:58 +0200569 break;
570
Lars-Peter Clausen237019e2013-01-10 17:06:10 +0100571 case 24:
572 if (reg_endian != REGMAP_ENDIAN_BIG)
573 goto err_map;
574 map->format.format_reg = regmap_format_24;
575 break;
576
Mark Brown7d5e5252012-02-17 15:58:25 -0800577 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600578 switch (reg_endian) {
579 case REGMAP_ENDIAN_BIG:
580 map->format.format_reg = regmap_format_32_be;
581 break;
582 case REGMAP_ENDIAN_NATIVE:
583 map->format.format_reg = regmap_format_32_native;
584 break;
585 default:
586 goto err_map;
587 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800588 break;
589
Mark Brownb83a3132011-05-11 19:59:58 +0200590 default:
591 goto err_map;
592 }
593
Mark Brown8a819ff2013-03-04 09:04:51 +0800594 if (val_endian == REGMAP_ENDIAN_NATIVE)
595 map->format.parse_inplace = regmap_parse_inplace_noop;
596
Mark Brownb83a3132011-05-11 19:59:58 +0200597 switch (config->val_bits) {
598 case 8:
599 map->format.format_val = regmap_format_8;
600 map->format.parse_val = regmap_parse_8;
Mark Brown8a819ff2013-03-04 09:04:51 +0800601 map->format.parse_inplace = regmap_parse_inplace_noop;
Mark Brownb83a3132011-05-11 19:59:58 +0200602 break;
603 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600604 switch (val_endian) {
605 case REGMAP_ENDIAN_BIG:
606 map->format.format_val = regmap_format_16_be;
607 map->format.parse_val = regmap_parse_16_be;
Mark Brown8a819ff2013-03-04 09:04:51 +0800608 map->format.parse_inplace = regmap_parse_16_be_inplace;
Stephen Warren141eba22012-05-24 10:47:26 -0600609 break;
610 case REGMAP_ENDIAN_NATIVE:
611 map->format.format_val = regmap_format_16_native;
612 map->format.parse_val = regmap_parse_16_native;
613 break;
614 default:
615 goto err_map;
616 }
Mark Brownb83a3132011-05-11 19:59:58 +0200617 break;
Marc Reillyea279fc2012-03-16 12:11:42 +1100618 case 24:
Stephen Warren141eba22012-05-24 10:47:26 -0600619 if (val_endian != REGMAP_ENDIAN_BIG)
620 goto err_map;
Marc Reillyea279fc2012-03-16 12:11:42 +1100621 map->format.format_val = regmap_format_24;
622 map->format.parse_val = regmap_parse_24;
623 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800624 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600625 switch (val_endian) {
626 case REGMAP_ENDIAN_BIG:
627 map->format.format_val = regmap_format_32_be;
628 map->format.parse_val = regmap_parse_32_be;
Mark Brown8a819ff2013-03-04 09:04:51 +0800629 map->format.parse_inplace = regmap_parse_32_be_inplace;
Stephen Warren141eba22012-05-24 10:47:26 -0600630 break;
631 case REGMAP_ENDIAN_NATIVE:
632 map->format.format_val = regmap_format_32_native;
633 map->format.parse_val = regmap_parse_32_native;
634 break;
635 default:
636 goto err_map;
637 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800638 break;
Mark Brownb83a3132011-05-11 19:59:58 +0200639 }
640
Stephen Warren141eba22012-05-24 10:47:26 -0600641 if (map->format.format_write) {
642 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
643 (val_endian != REGMAP_ENDIAN_BIG))
644 goto err_map;
Mark Brown7a647612012-04-30 23:26:32 +0100645 map->use_single_rw = true;
Stephen Warren141eba22012-05-24 10:47:26 -0600646 }
Mark Brown7a647612012-04-30 23:26:32 +0100647
Mark Brownb83a3132011-05-11 19:59:58 +0200648 if (!map->format.format_write &&
649 !(map->format.format_reg && map->format.format_val))
650 goto err_map;
651
Mark Brown82159ba2012-01-18 10:52:25 +0000652 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200653 if (map->work_buf == NULL) {
654 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700655 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200656 }
657
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800658 if (map->format.format_write) {
659 map->defer_caching = false;
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800660 map->reg_write = _regmap_bus_formatted_write;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800661 } else if (map->format.format_val) {
662 map->defer_caching = true;
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800663 map->reg_write = _regmap_bus_raw_write;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800664 }
665
666skip_format_initialization:
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800667
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100668 map->range_tree = RB_ROOT;
Mark Browne3549cd2012-10-02 20:17:15 +0100669 for (i = 0; i < config->num_ranges; i++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100670 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
671 struct regmap_range_node *new;
672
673 /* Sanity check */
Mark Brown061adc02012-10-03 12:17:51 +0100674 if (range_cfg->range_max < range_cfg->range_min) {
675 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
676 range_cfg->range_max, range_cfg->range_min);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100677 goto err_range;
Mark Brown061adc02012-10-03 12:17:51 +0100678 }
679
680 if (range_cfg->range_max > map->max_register) {
681 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
682 range_cfg->range_max, map->max_register);
683 goto err_range;
684 }
685
686 if (range_cfg->selector_reg > map->max_register) {
687 dev_err(map->dev,
688 "Invalid range %d: selector out of map\n", i);
689 goto err_range;
690 }
691
692 if (range_cfg->window_len == 0) {
693 dev_err(map->dev, "Invalid range %d: window_len 0\n",
694 i);
695 goto err_range;
696 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100697
698 /* Make sure, that this register range has no selector
699 or data window within its boundary */
Mark Browne3549cd2012-10-02 20:17:15 +0100700 for (j = 0; j < config->num_ranges; j++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100701 unsigned sel_reg = config->ranges[j].selector_reg;
702 unsigned win_min = config->ranges[j].window_start;
703 unsigned win_max = win_min +
704 config->ranges[j].window_len - 1;
705
Philipp Zabelf161d222013-07-23 12:16:02 +0200706 /* Allow data window inside its own virtual range */
707 if (j == i)
708 continue;
709
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100710 if (range_cfg->range_min <= sel_reg &&
711 sel_reg <= range_cfg->range_max) {
Mark Brown061adc02012-10-03 12:17:51 +0100712 dev_err(map->dev,
713 "Range %d: selector for %d in window\n",
714 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100715 goto err_range;
716 }
717
718 if (!(win_max < range_cfg->range_min ||
719 win_min > range_cfg->range_max)) {
Mark Brown061adc02012-10-03 12:17:51 +0100720 dev_err(map->dev,
721 "Range %d: window for %d in window\n",
722 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100723 goto err_range;
724 }
725 }
726
727 new = kzalloc(sizeof(*new), GFP_KERNEL);
728 if (new == NULL) {
729 ret = -ENOMEM;
730 goto err_range;
731 }
732
Mark Brown4b020b32012-10-03 13:13:16 +0100733 new->map = map;
Mark Brownd058bb42012-10-03 12:40:47 +0100734 new->name = range_cfg->name;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100735 new->range_min = range_cfg->range_min;
736 new->range_max = range_cfg->range_max;
737 new->selector_reg = range_cfg->selector_reg;
738 new->selector_mask = range_cfg->selector_mask;
739 new->selector_shift = range_cfg->selector_shift;
740 new->window_start = range_cfg->window_start;
741 new->window_len = range_cfg->window_len;
742
743 if (_regmap_range_add(map, new) == false) {
Mark Brown061adc02012-10-03 12:17:51 +0100744 dev_err(map->dev, "Failed to add range %d\n", i);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100745 kfree(new);
746 goto err_range;
747 }
748
749 if (map->selector_work_buf == NULL) {
750 map->selector_work_buf =
751 kzalloc(map->format.buf_size, GFP_KERNEL);
752 if (map->selector_work_buf == NULL) {
753 ret = -ENOMEM;
754 goto err_range;
755 }
756 }
757 }
Mark Brown052d2cd2011-11-21 19:05:13 +0000758
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100759 ret = regcache_init(map, config);
Mark Brown0ff3e622012-10-04 17:39:13 +0100760 if (ret != 0)
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100761 goto err_range;
762
Michal Simek6cfec042014-02-10 16:22:33 +0100763 if (dev)
764 ret = regmap_attach_dev(dev, map, config);
765 if (ret != 0)
766 goto err_regcache;
Mark Brown72b39f62012-05-08 17:44:40 +0100767
Mark Brownb83a3132011-05-11 19:59:58 +0200768 return map;
769
Michal Simek6cfec042014-02-10 16:22:33 +0100770err_regcache:
Mark Brown72b39f62012-05-08 17:44:40 +0100771 regcache_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100772err_range:
773 regmap_range_exit(map);
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100774 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200775err_map:
776 kfree(map);
777err:
778 return ERR_PTR(ret);
779}
780EXPORT_SYMBOL_GPL(regmap_init);
781
Mark Brownc0eb4672012-01-30 19:56:52 +0000782static void devm_regmap_release(struct device *dev, void *res)
783{
784 regmap_exit(*(struct regmap **)res);
785}
786
787/**
788 * devm_regmap_init(): Initialise managed register map
789 *
790 * @dev: Device that will be interacted with
791 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600792 * @bus_context: Data passed to bus-specific callbacks
Mark Brownc0eb4672012-01-30 19:56:52 +0000793 * @config: Configuration for register map
794 *
795 * The return value will be an ERR_PTR() on error or a valid pointer
796 * to a struct regmap. This function should generally not be called
797 * directly, it should be called by bus-specific init functions. The
798 * map will be automatically freed by the device management code.
799 */
800struct regmap *devm_regmap_init(struct device *dev,
801 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600802 void *bus_context,
Mark Brownc0eb4672012-01-30 19:56:52 +0000803 const struct regmap_config *config)
804{
805 struct regmap **ptr, *regmap;
806
807 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
808 if (!ptr)
809 return ERR_PTR(-ENOMEM);
810
Stephen Warren0135bbc2012-04-04 15:48:30 -0600811 regmap = regmap_init(dev, bus, bus_context, config);
Mark Brownc0eb4672012-01-30 19:56:52 +0000812 if (!IS_ERR(regmap)) {
813 *ptr = regmap;
814 devres_add(dev, ptr);
815 } else {
816 devres_free(ptr);
817 }
818
819 return regmap;
820}
821EXPORT_SYMBOL_GPL(devm_regmap_init);
822
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100823static void regmap_field_init(struct regmap_field *rm_field,
824 struct regmap *regmap, struct reg_field reg_field)
825{
826 int field_bits = reg_field.msb - reg_field.lsb + 1;
827 rm_field->regmap = regmap;
828 rm_field->reg = reg_field.reg;
829 rm_field->shift = reg_field.lsb;
830 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
Kuninori Morimotoa0102372013-09-01 20:30:50 -0700831 rm_field->id_size = reg_field.id_size;
832 rm_field->id_offset = reg_field.id_offset;
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100833}
834
835/**
836 * devm_regmap_field_alloc(): Allocate and initialise a register field
837 * in a register map.
838 *
839 * @dev: Device that will be interacted with
840 * @regmap: regmap bank in which this register field is located.
841 * @reg_field: Register field with in the bank.
842 *
843 * The return value will be an ERR_PTR() on error or a valid pointer
844 * to a struct regmap_field. The regmap_field will be automatically freed
845 * by the device management code.
846 */
847struct regmap_field *devm_regmap_field_alloc(struct device *dev,
848 struct regmap *regmap, struct reg_field reg_field)
849{
850 struct regmap_field *rm_field = devm_kzalloc(dev,
851 sizeof(*rm_field), GFP_KERNEL);
852 if (!rm_field)
853 return ERR_PTR(-ENOMEM);
854
855 regmap_field_init(rm_field, regmap, reg_field);
856
857 return rm_field;
858
859}
860EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
861
862/**
863 * devm_regmap_field_free(): Free register field allocated using
864 * devm_regmap_field_alloc. Usally drivers need not call this function,
865 * as the memory allocated via devm will be freed as per device-driver
866 * life-cyle.
867 *
868 * @dev: Device that will be interacted with
869 * @field: regmap field which should be freed.
870 */
871void devm_regmap_field_free(struct device *dev,
872 struct regmap_field *field)
873{
874 devm_kfree(dev, field);
875}
876EXPORT_SYMBOL_GPL(devm_regmap_field_free);
877
878/**
879 * regmap_field_alloc(): Allocate and initialise a register field
880 * in a register map.
881 *
882 * @regmap: regmap bank in which this register field is located.
883 * @reg_field: Register field with in the bank.
884 *
885 * The return value will be an ERR_PTR() on error or a valid pointer
886 * to a struct regmap_field. The regmap_field should be freed by the
887 * user once its finished working with it using regmap_field_free().
888 */
889struct regmap_field *regmap_field_alloc(struct regmap *regmap,
890 struct reg_field reg_field)
891{
892 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
893
894 if (!rm_field)
895 return ERR_PTR(-ENOMEM);
896
897 regmap_field_init(rm_field, regmap, reg_field);
898
899 return rm_field;
900}
901EXPORT_SYMBOL_GPL(regmap_field_alloc);
902
903/**
904 * regmap_field_free(): Free register field allocated using regmap_field_alloc
905 *
906 * @field: regmap field which should be freed.
907 */
908void regmap_field_free(struct regmap_field *field)
909{
910 kfree(field);
911}
912EXPORT_SYMBOL_GPL(regmap_field_free);
913
Mark Brownb83a3132011-05-11 19:59:58 +0200914/**
Mark Brownbf315172011-12-03 17:06:20 +0000915 * regmap_reinit_cache(): Reinitialise the current register cache
916 *
917 * @map: Register map to operate on.
918 * @config: New configuration. Only the cache data will be used.
919 *
920 * Discard any existing register cache for the map and initialize a
921 * new cache. This can be used to restore the cache to defaults or to
922 * update the cache configuration to reflect runtime discovery of the
923 * hardware.
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100924 *
925 * No explicit locking is done here, the user needs to ensure that
926 * this function will not race with other calls to regmap.
Mark Brownbf315172011-12-03 17:06:20 +0000927 */
928int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
929{
Mark Brownbf315172011-12-03 17:06:20 +0000930 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000931 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000932
933 map->max_register = config->max_register;
934 map->writeable_reg = config->writeable_reg;
935 map->readable_reg = config->readable_reg;
936 map->volatile_reg = config->volatile_reg;
937 map->precious_reg = config->precious_reg;
938 map->cache_type = config->cache_type;
939
Stephen Warrend3c242e2012-04-04 15:48:29 -0600940 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +0000941
Mark Brown421e8d22012-01-20 13:39:37 +0000942 map->cache_bypass = false;
943 map->cache_only = false;
944
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100945 return regcache_init(map, config);
Mark Brownbf315172011-12-03 17:06:20 +0000946}
Mark Brown752a6a52012-05-14 10:00:12 +0100947EXPORT_SYMBOL_GPL(regmap_reinit_cache);
Mark Brownbf315172011-12-03 17:06:20 +0000948
949/**
Mark Brownb83a3132011-05-11 19:59:58 +0200950 * regmap_exit(): Free a previously allocated register map
951 */
952void regmap_exit(struct regmap *map)
953{
Mark Brown7e09a972013-10-07 23:00:24 +0100954 struct regmap_async *async;
955
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100956 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100957 regmap_debugfs_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100958 regmap_range_exit(map);
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800959 if (map->bus && map->bus->free_context)
Stephen Warren0135bbc2012-04-04 15:48:30 -0600960 map->bus->free_context(map->bus_context);
Mark Brownb83a3132011-05-11 19:59:58 +0200961 kfree(map->work_buf);
Mark Brown7e09a972013-10-07 23:00:24 +0100962 while (!list_empty(&map->async_free)) {
963 async = list_first_entry_or_null(&map->async_free,
964 struct regmap_async,
965 list);
966 list_del(&async->list);
967 kfree(async->work_buf);
968 kfree(async);
969 }
Mark Brownb83a3132011-05-11 19:59:58 +0200970 kfree(map);
971}
972EXPORT_SYMBOL_GPL(regmap_exit);
973
Mark Brown72b39f62012-05-08 17:44:40 +0100974static int dev_get_regmap_match(struct device *dev, void *res, void *data)
975{
976 struct regmap **r = res;
977 if (!r || !*r) {
978 WARN_ON(!r || !*r);
979 return 0;
980 }
981
982 /* If the user didn't specify a name match any */
983 if (data)
984 return (*r)->name == data;
985 else
986 return 1;
987}
988
989/**
990 * dev_get_regmap(): Obtain the regmap (if any) for a device
991 *
992 * @dev: Device to retrieve the map for
993 * @name: Optional name for the register map, usually NULL.
994 *
995 * Returns the regmap for the device if one is present, or NULL. If
996 * name is specified then it must match the name specified when
997 * registering the device, if it is NULL then the first regmap found
998 * will be used. Devices with multiple register maps are very rare,
999 * generic code should normally not need to specify a name.
1000 */
1001struct regmap *dev_get_regmap(struct device *dev, const char *name)
1002{
1003 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1004 dev_get_regmap_match, (void *)name);
1005
1006 if (!r)
1007 return NULL;
1008 return *r;
1009}
1010EXPORT_SYMBOL_GPL(dev_get_regmap);
1011
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001012static int _regmap_select_page(struct regmap *map, unsigned int *reg,
Mark Brown98bc7df2012-10-04 17:31:11 +01001013 struct regmap_range_node *range,
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001014 unsigned int val_num)
1015{
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001016 void *orig_work_buf;
1017 unsigned int win_offset;
1018 unsigned int win_page;
1019 bool page_chg;
1020 int ret;
1021
Mark Brown98bc7df2012-10-04 17:31:11 +01001022 win_offset = (*reg - range->range_min) % range->window_len;
1023 win_page = (*reg - range->range_min) / range->window_len;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001024
Mark Brown98bc7df2012-10-04 17:31:11 +01001025 if (val_num > 1) {
1026 /* Bulk write shouldn't cross range boundary */
1027 if (*reg + val_num - 1 > range->range_max)
1028 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001029
Mark Brown98bc7df2012-10-04 17:31:11 +01001030 /* ... or single page boundary */
1031 if (val_num > range->window_len - win_offset)
1032 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001033 }
1034
Mark Brown98bc7df2012-10-04 17:31:11 +01001035 /* It is possible to have selector register inside data window.
1036 In that case, selector register is located on every page and
1037 it needs no page switching, when accessed alone. */
1038 if (val_num > 1 ||
1039 range->window_start + win_offset != range->selector_reg) {
1040 /* Use separate work_buf during page switching */
1041 orig_work_buf = map->work_buf;
1042 map->work_buf = map->selector_work_buf;
1043
1044 ret = _regmap_update_bits(map, range->selector_reg,
1045 range->selector_mask,
1046 win_page << range->selector_shift,
1047 &page_chg);
1048
1049 map->work_buf = orig_work_buf;
1050
Mark Brown0ff3e622012-10-04 17:39:13 +01001051 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001052 return ret;
1053 }
1054
1055 *reg = range->window_start + win_offset;
1056
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001057 return 0;
1058}
1059
Mark Brown584de322013-03-13 19:19:34 +00001060int _regmap_raw_write(struct regmap *map, unsigned int reg,
Mark Brown0a819802013-10-09 12:28:52 +01001061 const void *val, size_t val_len)
Mark Brownb83a3132011-05-11 19:59:58 +02001062{
Mark Brown98bc7df2012-10-04 17:31:11 +01001063 struct regmap_range_node *range;
Mark Brown0d509f22013-01-27 22:07:38 +08001064 unsigned long flags;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001065 u8 *u8 = map->work_buf;
Mark Brown0d509f22013-01-27 22:07:38 +08001066 void *work_val = map->work_buf + map->format.reg_bytes +
1067 map->format.pad_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +02001068 void *buf;
1069 int ret = -ENOTSUPP;
1070 size_t len;
Mark Brown73304782011-07-24 11:46:20 +01001071 int i;
1072
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001073 WARN_ON(!map->bus);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001074
Mark Brown73304782011-07-24 11:46:20 +01001075 /* Check for unwritable registers before we start */
1076 if (map->writeable_reg)
1077 for (i = 0; i < val_len / map->format.val_bytes; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -06001078 if (!map->writeable_reg(map->dev,
1079 reg + (i * map->reg_stride)))
Mark Brown73304782011-07-24 11:46:20 +01001080 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001081
Laxman Dewanganc9157192012-02-10 21:30:27 +05301082 if (!map->cache_bypass && map->format.parse_val) {
1083 unsigned int ival;
1084 int val_bytes = map->format.val_bytes;
1085 for (i = 0; i < val_len / val_bytes; i++) {
Stephen Warren5a08d152013-03-20 17:02:02 -06001086 ival = map->format.parse_val(val + (i * val_bytes));
Stephen Warrenf01ee602012-04-09 13:40:24 -06001087 ret = regcache_write(map, reg + (i * map->reg_stride),
1088 ival);
Laxman Dewanganc9157192012-02-10 21:30:27 +05301089 if (ret) {
1090 dev_err(map->dev,
Mark Brown6d04b8a2012-10-26 19:05:32 +01001091 "Error in caching of register: %x ret: %d\n",
Laxman Dewanganc9157192012-02-10 21:30:27 +05301092 reg + i, ret);
1093 return ret;
1094 }
1095 }
1096 if (map->cache_only) {
1097 map->cache_dirty = true;
1098 return 0;
1099 }
1100 }
1101
Mark Brown98bc7df2012-10-04 17:31:11 +01001102 range = _regmap_range_lookup(map, reg);
1103 if (range) {
Mark Brown8a2ceac2012-10-04 18:20:18 +01001104 int val_num = val_len / map->format.val_bytes;
1105 int win_offset = (reg - range->range_min) % range->window_len;
1106 int win_residue = range->window_len - win_offset;
1107
1108 /* If the write goes beyond the end of the window split it */
1109 while (val_num > win_residue) {
Fabio Estevam1a61cfe2012-10-25 14:07:18 -02001110 dev_dbg(map->dev, "Writing window %d/%zu\n",
Mark Brown8a2ceac2012-10-04 18:20:18 +01001111 win_residue, val_len / map->format.val_bytes);
1112 ret = _regmap_raw_write(map, reg, val, win_residue *
Mark Brown0a819802013-10-09 12:28:52 +01001113 map->format.val_bytes);
Mark Brown8a2ceac2012-10-04 18:20:18 +01001114 if (ret != 0)
1115 return ret;
1116
1117 reg += win_residue;
1118 val_num -= win_residue;
1119 val += win_residue * map->format.val_bytes;
1120 val_len -= win_residue * map->format.val_bytes;
1121
1122 win_offset = (reg - range->range_min) %
1123 range->window_len;
1124 win_residue = range->window_len - win_offset;
1125 }
1126
1127 ret = _regmap_select_page(map, &reg, range, val_num);
Mark Brown0ff3e622012-10-04 17:39:13 +01001128 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001129 return ret;
1130 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001131
Marc Reillyd939fb92012-03-16 12:11:43 +11001132 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001133
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001134 u8[0] |= map->write_flag_mask;
1135
Mark Brown651e0132013-10-08 18:37:36 +01001136 /*
1137 * Essentially all I/O mechanisms will be faster with a single
1138 * buffer to write. Since register syncs often generate raw
1139 * writes of single registers optimise that case.
1140 */
1141 if (val != work_val && val_len == map->format.val_bytes) {
1142 memcpy(work_val, val, map->format.val_bytes);
1143 val = work_val;
1144 }
1145
Mark Brown0a819802013-10-09 12:28:52 +01001146 if (map->async && map->bus->async_write) {
Mark Brown7e09a972013-10-07 23:00:24 +01001147 struct regmap_async *async;
Mark Brown0d509f22013-01-27 22:07:38 +08001148
Mark Brownfe7d4cc2013-02-21 19:05:48 +00001149 trace_regmap_async_write_start(map->dev, reg, val_len);
1150
Mark Brown7e09a972013-10-07 23:00:24 +01001151 spin_lock_irqsave(&map->async_lock, flags);
1152 async = list_first_entry_or_null(&map->async_free,
1153 struct regmap_async,
1154 list);
1155 if (async)
1156 list_del(&async->list);
1157 spin_unlock_irqrestore(&map->async_lock, flags);
1158
1159 if (!async) {
1160 async = map->bus->async_alloc();
1161 if (!async)
1162 return -ENOMEM;
1163
1164 async->work_buf = kzalloc(map->format.buf_size,
1165 GFP_KERNEL | GFP_DMA);
1166 if (!async->work_buf) {
1167 kfree(async);
1168 return -ENOMEM;
1169 }
Mark Brown0d509f22013-01-27 22:07:38 +08001170 }
1171
Mark Brown0d509f22013-01-27 22:07:38 +08001172 async->map = map;
1173
1174 /* If the caller supplied the value we can use it safely. */
1175 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1176 map->format.reg_bytes + map->format.val_bytes);
Mark Brown0d509f22013-01-27 22:07:38 +08001177
1178 spin_lock_irqsave(&map->async_lock, flags);
1179 list_add_tail(&async->list, &map->async_list);
1180 spin_unlock_irqrestore(&map->async_lock, flags);
1181
Mark Brown04c50cc2013-10-10 22:38:29 +01001182 if (val != work_val)
1183 ret = map->bus->async_write(map->bus_context,
1184 async->work_buf,
1185 map->format.reg_bytes +
1186 map->format.pad_bytes,
1187 val, val_len, async);
1188 else
1189 ret = map->bus->async_write(map->bus_context,
1190 async->work_buf,
1191 map->format.reg_bytes +
1192 map->format.pad_bytes +
1193 val_len, NULL, 0, async);
Mark Brown0d509f22013-01-27 22:07:38 +08001194
1195 if (ret != 0) {
1196 dev_err(map->dev, "Failed to schedule write: %d\n",
1197 ret);
1198
1199 spin_lock_irqsave(&map->async_lock, flags);
Mark Brown7e09a972013-10-07 23:00:24 +01001200 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08001201 spin_unlock_irqrestore(&map->async_lock, flags);
Mark Brown0d509f22013-01-27 22:07:38 +08001202 }
Mark Brownf951b652013-03-27 13:08:44 +00001203
1204 return ret;
Mark Brown0d509f22013-01-27 22:07:38 +08001205 }
1206
Mark Brownfb2736b2011-07-24 21:30:55 +01001207 trace_regmap_hw_write_start(map->dev, reg,
1208 val_len / map->format.val_bytes);
1209
Mark Brown2547e202011-07-20 21:47:22 +01001210 /* If we're doing a single register write we can probably just
1211 * send the work_buf directly, otherwise try to do a gather
1212 * write.
1213 */
Mark Brown0d509f22013-01-27 22:07:38 +08001214 if (val == work_val)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001215 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001216 map->format.reg_bytes +
1217 map->format.pad_bytes +
1218 val_len);
Mark Brown2547e202011-07-20 21:47:22 +01001219 else if (map->bus->gather_write)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001220 ret = map->bus->gather_write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001221 map->format.reg_bytes +
1222 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +02001223 val, val_len);
1224
Mark Brown2547e202011-07-20 21:47:22 +01001225 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +02001226 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +00001227 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1228 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +02001229 if (!buf)
1230 return -ENOMEM;
1231
1232 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +00001233 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1234 val, val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -06001235 ret = map->bus->write(map->bus_context, buf, len);
Mark Brownb83a3132011-05-11 19:59:58 +02001236
1237 kfree(buf);
1238 }
1239
Mark Brownfb2736b2011-07-24 21:30:55 +01001240 trace_regmap_hw_write_done(map->dev, reg,
1241 val_len / map->format.val_bytes);
1242
Mark Brownb83a3132011-05-11 19:59:58 +02001243 return ret;
1244}
1245
Mark Brown221ad7f2013-03-26 21:24:20 +00001246/**
1247 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1248 *
1249 * @map: Map to check.
1250 */
1251bool regmap_can_raw_write(struct regmap *map)
1252{
1253 return map->bus && map->format.format_val && map->format.format_reg;
1254}
1255EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1256
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001257static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1258 unsigned int val)
1259{
1260 int ret;
1261 struct regmap_range_node *range;
1262 struct regmap *map = context;
1263
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001264 WARN_ON(!map->bus || !map->format.format_write);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001265
1266 range = _regmap_range_lookup(map, reg);
1267 if (range) {
1268 ret = _regmap_select_page(map, &reg, range, 1);
1269 if (ret != 0)
1270 return ret;
1271 }
1272
1273 map->format.format_write(map, reg, val);
1274
1275 trace_regmap_hw_write_start(map->dev, reg, 1);
1276
1277 ret = map->bus->write(map->bus_context, map->work_buf,
1278 map->format.buf_size);
1279
1280 trace_regmap_hw_write_done(map->dev, reg, 1);
1281
1282 return ret;
1283}
1284
1285static int _regmap_bus_raw_write(void *context, unsigned int reg,
1286 unsigned int val)
1287{
1288 struct regmap *map = context;
1289
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001290 WARN_ON(!map->bus || !map->format.format_val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001291
1292 map->format.format_val(map->work_buf + map->format.reg_bytes
1293 + map->format.pad_bytes, val, 0);
1294 return _regmap_raw_write(map, reg,
1295 map->work_buf +
1296 map->format.reg_bytes +
1297 map->format.pad_bytes,
Mark Brown0a819802013-10-09 12:28:52 +01001298 map->format.val_bytes);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001299}
1300
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001301static inline void *_regmap_map_get_context(struct regmap *map)
1302{
1303 return (map->bus) ? map : map->bus_context;
1304}
1305
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +01001306int _regmap_write(struct regmap *map, unsigned int reg,
1307 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +02001308{
Mark Brownfb2736b2011-07-24 21:30:55 +01001309 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001310 void *context = _regmap_map_get_context(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001311
Ionut Nicu515f2262013-08-09 12:09:20 +02001312 if (!regmap_writeable(map, reg))
1313 return -EIO;
1314
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001315 if (!map->cache_bypass && !map->defer_caching) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001316 ret = regcache_write(map, reg, val);
1317 if (ret != 0)
1318 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001319 if (map->cache_only) {
1320 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001321 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001322 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001323 }
1324
Mark Brown1044c182012-07-06 14:10:23 +01001325#ifdef LOG_DEVICE
1326 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1327 dev_info(map->dev, "%x <= %x\n", reg, val);
1328#endif
1329
Mark Brownfb2736b2011-07-24 21:30:55 +01001330 trace_regmap_reg_write(map->dev, reg, val);
1331
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001332 return map->reg_write(context, reg, val);
Mark Brownb83a3132011-05-11 19:59:58 +02001333}
1334
1335/**
1336 * regmap_write(): Write a value to a single register
1337 *
1338 * @map: Register map to write to
1339 * @reg: Register to write to
1340 * @val: Value to be written
1341 *
1342 * A value of zero will be returned on success, a negative errno will
1343 * be returned in error cases.
1344 */
1345int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1346{
1347 int ret;
1348
Stephen Warrenf01ee602012-04-09 13:40:24 -06001349 if (reg % map->reg_stride)
1350 return -EINVAL;
1351
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001352 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001353
1354 ret = _regmap_write(map, reg, val);
1355
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001356 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001357
1358 return ret;
1359}
1360EXPORT_SYMBOL_GPL(regmap_write);
1361
1362/**
Mark Brown915f4412013-10-09 13:30:10 +01001363 * regmap_write_async(): Write a value to a single register asynchronously
1364 *
1365 * @map: Register map to write to
1366 * @reg: Register to write to
1367 * @val: Value to be written
1368 *
1369 * A value of zero will be returned on success, a negative errno will
1370 * be returned in error cases.
1371 */
1372int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1373{
1374 int ret;
1375
1376 if (reg % map->reg_stride)
1377 return -EINVAL;
1378
1379 map->lock(map->lock_arg);
1380
1381 map->async = true;
1382
1383 ret = _regmap_write(map, reg, val);
1384
1385 map->async = false;
1386
1387 map->unlock(map->lock_arg);
1388
1389 return ret;
1390}
1391EXPORT_SYMBOL_GPL(regmap_write_async);
1392
1393/**
Mark Brownb83a3132011-05-11 19:59:58 +02001394 * regmap_raw_write(): Write raw values to one or more registers
1395 *
1396 * @map: Register map to write to
1397 * @reg: Initial register to write to
1398 * @val: Block of data to be written, laid out for direct transmission to the
1399 * device
1400 * @val_len: Length of data pointed to by val.
1401 *
1402 * This function is intended to be used for things like firmware
1403 * download where a large block of data needs to be transferred to the
1404 * device. No formatting will be done on the data provided.
1405 *
1406 * A value of zero will be returned on success, a negative errno will
1407 * be returned in error cases.
1408 */
1409int regmap_raw_write(struct regmap *map, unsigned int reg,
1410 const void *val, size_t val_len)
1411{
1412 int ret;
1413
Mark Brown221ad7f2013-03-26 21:24:20 +00001414 if (!regmap_can_raw_write(map))
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001415 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001416 if (val_len % map->format.val_bytes)
1417 return -EINVAL;
1418
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001419 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001420
Mark Brown0a819802013-10-09 12:28:52 +01001421 ret = _regmap_raw_write(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001422
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001423 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001424
1425 return ret;
1426}
1427EXPORT_SYMBOL_GPL(regmap_raw_write);
1428
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001429/**
1430 * regmap_field_write(): Write a value to a single register field
1431 *
1432 * @field: Register field to write to
1433 * @val: Value to be written
1434 *
1435 * A value of zero will be returned on success, a negative errno will
1436 * be returned in error cases.
1437 */
1438int regmap_field_write(struct regmap_field *field, unsigned int val)
1439{
1440 return regmap_update_bits(field->regmap, field->reg,
1441 field->mask, val << field->shift);
1442}
1443EXPORT_SYMBOL_GPL(regmap_field_write);
1444
Kuninori Morimotofdf20022013-09-01 20:24:50 -07001445/**
1446 * regmap_field_update_bits(): Perform a read/modify/write cycle
1447 * on the register field
1448 *
1449 * @field: Register field to write to
1450 * @mask: Bitmask to change
1451 * @val: Value to be written
1452 *
1453 * A value of zero will be returned on success, a negative errno will
1454 * be returned in error cases.
1455 */
1456int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1457{
1458 mask = (mask << field->shift) & field->mask;
1459
1460 return regmap_update_bits(field->regmap, field->reg,
1461 mask, val << field->shift);
1462}
1463EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1464
Kuninori Morimotoa0102372013-09-01 20:30:50 -07001465/**
1466 * regmap_fields_write(): Write a value to a single register field with port ID
1467 *
1468 * @field: Register field to write to
1469 * @id: port ID
1470 * @val: Value to be written
1471 *
1472 * A value of zero will be returned on success, a negative errno will
1473 * be returned in error cases.
1474 */
1475int regmap_fields_write(struct regmap_field *field, unsigned int id,
1476 unsigned int val)
1477{
1478 if (id >= field->id_size)
1479 return -EINVAL;
1480
1481 return regmap_update_bits(field->regmap,
1482 field->reg + (field->id_offset * id),
1483 field->mask, val << field->shift);
1484}
1485EXPORT_SYMBOL_GPL(regmap_fields_write);
1486
1487/**
1488 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1489 * on the register field
1490 *
1491 * @field: Register field to write to
1492 * @id: port ID
1493 * @mask: Bitmask to change
1494 * @val: Value to be written
1495 *
1496 * A value of zero will be returned on success, a negative errno will
1497 * be returned in error cases.
1498 */
1499int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1500 unsigned int mask, unsigned int val)
1501{
1502 if (id >= field->id_size)
1503 return -EINVAL;
1504
1505 mask = (mask << field->shift) & field->mask;
1506
1507 return regmap_update_bits(field->regmap,
1508 field->reg + (field->id_offset * id),
1509 mask, val << field->shift);
1510}
1511EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1512
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301513/*
1514 * regmap_bulk_write(): Write multiple registers to the device
1515 *
1516 * @map: Register map to write to
1517 * @reg: First register to be write from
1518 * @val: Block of data to be written, in native register size for device
1519 * @val_count: Number of registers to write
1520 *
1521 * This function is intended to be used for writing a large block of
Nestor Ovroy31b35e9e2013-01-18 16:51:03 +01001522 * data to the device either in single transfer or multiple transfer.
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301523 *
1524 * A value of zero will be returned on success, a negative errno will
1525 * be returned in error cases.
1526 */
1527int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1528 size_t val_count)
1529{
1530 int ret = 0, i;
1531 size_t val_bytes = map->format.val_bytes;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301532
Stephen Boydf4298362013-12-26 13:52:04 -08001533 if (map->bus && !map->format.parse_inplace)
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301534 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001535 if (reg % map->reg_stride)
1536 return -EINVAL;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301537
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001538 map->lock(map->lock_arg);
Stephen Boydf4298362013-12-26 13:52:04 -08001539 /*
1540 * Some devices don't support bulk write, for
1541 * them we have a series of single write operations.
1542 */
1543 if (!map->bus || map->use_single_rw) {
1544 for (i = 0; i < val_count; i++) {
1545 unsigned int ival;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301546
Stephen Boydf4298362013-12-26 13:52:04 -08001547 switch (val_bytes) {
1548 case 1:
1549 ival = *(u8 *)(val + (i * val_bytes));
1550 break;
1551 case 2:
1552 ival = *(u16 *)(val + (i * val_bytes));
1553 break;
1554 case 4:
1555 ival = *(u32 *)(val + (i * val_bytes));
1556 break;
1557#ifdef CONFIG_64BIT
1558 case 8:
1559 ival = *(u64 *)(val + (i * val_bytes));
1560 break;
1561#endif
1562 default:
1563 ret = -EINVAL;
1564 goto out;
1565 }
1566
1567 ret = _regmap_write(map, reg + (i * map->reg_stride),
1568 ival);
1569 if (ret != 0)
1570 goto out;
1571 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301572 } else {
Stephen Boydf4298362013-12-26 13:52:04 -08001573 void *wval;
1574
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301575 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1576 if (!wval) {
1577 ret = -ENOMEM;
1578 dev_err(map->dev, "Error in memory allocation\n");
1579 goto out;
1580 }
1581 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08001582 map->format.parse_inplace(wval + i);
Stephen Boydf4298362013-12-26 13:52:04 -08001583
Mark Brown0a819802013-10-09 12:28:52 +01001584 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301585
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301586 kfree(wval);
Stephen Boydf4298362013-12-26 13:52:04 -08001587 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301588out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001589 map->unlock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301590 return ret;
1591}
1592EXPORT_SYMBOL_GPL(regmap_bulk_write);
1593
Anthony Oleche33fabd2013-10-11 15:31:11 +01001594/*
1595 * regmap_multi_reg_write(): Write multiple registers to the device
1596 *
1597 * where the set of register are supplied in any order
1598 *
1599 * @map: Register map to write to
1600 * @regs: Array of structures containing register,value to be written
1601 * @num_regs: Number of registers to write
1602 *
1603 * This function is intended to be used for writing a large block of data
1604 * atomically to the device in single transfer for those I2C client devices
1605 * that implement this alternative block write mode.
1606 *
1607 * A value of zero will be returned on success, a negative errno will
1608 * be returned in error cases.
1609 */
1610int regmap_multi_reg_write(struct regmap *map, struct reg_default *regs,
1611 int num_regs)
1612{
1613 int ret = 0, i;
1614
1615 for (i = 0; i < num_regs; i++) {
1616 int reg = regs[i].reg;
1617 if (reg % map->reg_stride)
1618 return -EINVAL;
1619 }
1620
1621 map->lock(map->lock_arg);
1622
1623 for (i = 0; i < num_regs; i++) {
1624 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1625 if (ret != 0)
1626 goto out;
1627 }
1628out:
1629 map->unlock(map->lock_arg);
1630
1631 return ret;
1632}
1633EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
1634
Mark Brown0d509f22013-01-27 22:07:38 +08001635/**
1636 * regmap_raw_write_async(): Write raw values to one or more registers
1637 * asynchronously
1638 *
1639 * @map: Register map to write to
1640 * @reg: Initial register to write to
1641 * @val: Block of data to be written, laid out for direct transmission to the
1642 * device. Must be valid until regmap_async_complete() is called.
1643 * @val_len: Length of data pointed to by val.
1644 *
1645 * This function is intended to be used for things like firmware
1646 * download where a large block of data needs to be transferred to the
1647 * device. No formatting will be done on the data provided.
1648 *
1649 * If supported by the underlying bus the write will be scheduled
1650 * asynchronously, helping maximise I/O speed on higher speed buses
1651 * like SPI. regmap_async_complete() can be called to ensure that all
1652 * asynchrnous writes have been completed.
1653 *
1654 * A value of zero will be returned on success, a negative errno will
1655 * be returned in error cases.
1656 */
1657int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1658 const void *val, size_t val_len)
1659{
1660 int ret;
1661
1662 if (val_len % map->format.val_bytes)
1663 return -EINVAL;
1664 if (reg % map->reg_stride)
1665 return -EINVAL;
1666
1667 map->lock(map->lock_arg);
1668
Mark Brown0a819802013-10-09 12:28:52 +01001669 map->async = true;
1670
1671 ret = _regmap_raw_write(map, reg, val, val_len);
1672
1673 map->async = false;
Mark Brown0d509f22013-01-27 22:07:38 +08001674
1675 map->unlock(map->lock_arg);
1676
1677 return ret;
1678}
1679EXPORT_SYMBOL_GPL(regmap_raw_write_async);
1680
Mark Brownb83a3132011-05-11 19:59:58 +02001681static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1682 unsigned int val_len)
1683{
Mark Brown98bc7df2012-10-04 17:31:11 +01001684 struct regmap_range_node *range;
Mark Brownb83a3132011-05-11 19:59:58 +02001685 u8 *u8 = map->work_buf;
1686 int ret;
1687
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001688 WARN_ON(!map->bus);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001689
Mark Brown98bc7df2012-10-04 17:31:11 +01001690 range = _regmap_range_lookup(map, reg);
1691 if (range) {
1692 ret = _regmap_select_page(map, &reg, range,
1693 val_len / map->format.val_bytes);
Mark Brown0ff3e622012-10-04 17:39:13 +01001694 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001695 return ret;
1696 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001697
Marc Reillyd939fb92012-03-16 12:11:43 +11001698 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001699
1700 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001701 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +02001702 * register addresss; since it's always the high bits for all
1703 * current formats we can do this here rather than in
1704 * formatting. This may break if we get interesting formats.
1705 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001706 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001707
Mark Brownfb2736b2011-07-24 21:30:55 +01001708 trace_regmap_hw_read_start(map->dev, reg,
1709 val_len / map->format.val_bytes);
1710
Stephen Warren0135bbc2012-04-04 15:48:30 -06001711 ret = map->bus->read(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001712 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +01001713 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001714
Mark Brownfb2736b2011-07-24 21:30:55 +01001715 trace_regmap_hw_read_done(map->dev, reg,
1716 val_len / map->format.val_bytes);
1717
1718 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001719}
1720
Andrey Smirnovad278402013-01-12 12:54:12 -08001721static int _regmap_bus_read(void *context, unsigned int reg,
1722 unsigned int *val)
1723{
1724 int ret;
1725 struct regmap *map = context;
1726
1727 if (!map->format.parse_val)
1728 return -EINVAL;
1729
1730 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
1731 if (ret == 0)
1732 *val = map->format.parse_val(map->work_buf);
1733
1734 return ret;
1735}
1736
Mark Brownb83a3132011-05-11 19:59:58 +02001737static int _regmap_read(struct regmap *map, unsigned int reg,
1738 unsigned int *val)
1739{
1740 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001741 void *context = _regmap_map_get_context(map);
1742
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001743 WARN_ON(!map->reg_read);
Mark Brownb83a3132011-05-11 19:59:58 +02001744
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001745 if (!map->cache_bypass) {
1746 ret = regcache_read(map, reg, val);
1747 if (ret == 0)
1748 return 0;
1749 }
1750
1751 if (map->cache_only)
1752 return -EBUSY;
1753
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001754 ret = map->reg_read(context, reg, val);
Mark Brownfb2736b2011-07-24 21:30:55 +01001755 if (ret == 0) {
Mark Brown1044c182012-07-06 14:10:23 +01001756#ifdef LOG_DEVICE
1757 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1758 dev_info(map->dev, "%x => %x\n", reg, *val);
1759#endif
1760
Mark Brownfb2736b2011-07-24 21:30:55 +01001761 trace_regmap_reg_read(map->dev, reg, *val);
Mark Brownb83a3132011-05-11 19:59:58 +02001762
Andrey Smirnovad278402013-01-12 12:54:12 -08001763 if (!map->cache_bypass)
1764 regcache_write(map, reg, *val);
1765 }
Mark Brownf2985362012-04-30 21:25:05 +01001766
Mark Brownb83a3132011-05-11 19:59:58 +02001767 return ret;
1768}
1769
1770/**
1771 * regmap_read(): Read a value from a single register
1772 *
Gerhard Sittig00933802013-11-11 10:42:36 +01001773 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02001774 * @reg: Register to be read from
1775 * @val: Pointer to store read value
1776 *
1777 * A value of zero will be returned on success, a negative errno will
1778 * be returned in error cases.
1779 */
1780int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1781{
1782 int ret;
1783
Stephen Warrenf01ee602012-04-09 13:40:24 -06001784 if (reg % map->reg_stride)
1785 return -EINVAL;
1786
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001787 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001788
1789 ret = _regmap_read(map, reg, val);
1790
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001791 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001792
1793 return ret;
1794}
1795EXPORT_SYMBOL_GPL(regmap_read);
1796
1797/**
1798 * regmap_raw_read(): Read raw data from the device
1799 *
Gerhard Sittig00933802013-11-11 10:42:36 +01001800 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02001801 * @reg: First register to be read from
1802 * @val: Pointer to store read value
1803 * @val_len: Size of data to read
1804 *
1805 * A value of zero will be returned on success, a negative errno will
1806 * be returned in error cases.
1807 */
1808int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1809 size_t val_len)
1810{
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001811 size_t val_bytes = map->format.val_bytes;
1812 size_t val_count = val_len / val_bytes;
1813 unsigned int v;
1814 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +01001815
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001816 if (!map->bus)
1817 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001818 if (val_len % map->format.val_bytes)
1819 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001820 if (reg % map->reg_stride)
1821 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001822
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001823 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001824
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001825 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1826 map->cache_type == REGCACHE_NONE) {
1827 /* Physical block read if there's no cache involved */
1828 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001829
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001830 } else {
1831 /* Otherwise go word by word for the cache; should be low
1832 * cost as we expect to hit the cache.
1833 */
1834 for (i = 0; i < val_count; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -06001835 ret = _regmap_read(map, reg + (i * map->reg_stride),
1836 &v);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001837 if (ret != 0)
1838 goto out;
1839
Marc Reillyd939fb92012-03-16 12:11:43 +11001840 map->format.format_val(val + (i * val_bytes), v, 0);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001841 }
1842 }
1843
1844 out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001845 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001846
1847 return ret;
1848}
1849EXPORT_SYMBOL_GPL(regmap_raw_read);
1850
1851/**
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001852 * regmap_field_read(): Read a value to a single register field
1853 *
1854 * @field: Register field to read from
1855 * @val: Pointer to store read value
1856 *
1857 * A value of zero will be returned on success, a negative errno will
1858 * be returned in error cases.
1859 */
1860int regmap_field_read(struct regmap_field *field, unsigned int *val)
1861{
1862 int ret;
1863 unsigned int reg_val;
1864 ret = regmap_read(field->regmap, field->reg, &reg_val);
1865 if (ret != 0)
1866 return ret;
1867
1868 reg_val &= field->mask;
1869 reg_val >>= field->shift;
1870 *val = reg_val;
1871
1872 return ret;
1873}
1874EXPORT_SYMBOL_GPL(regmap_field_read);
1875
1876/**
Kuninori Morimotoa0102372013-09-01 20:30:50 -07001877 * regmap_fields_read(): Read a value to a single register field with port ID
1878 *
1879 * @field: Register field to read from
1880 * @id: port ID
1881 * @val: Pointer to store read value
1882 *
1883 * A value of zero will be returned on success, a negative errno will
1884 * be returned in error cases.
1885 */
1886int regmap_fields_read(struct regmap_field *field, unsigned int id,
1887 unsigned int *val)
1888{
1889 int ret;
1890 unsigned int reg_val;
1891
1892 if (id >= field->id_size)
1893 return -EINVAL;
1894
1895 ret = regmap_read(field->regmap,
1896 field->reg + (field->id_offset * id),
1897 &reg_val);
1898 if (ret != 0)
1899 return ret;
1900
1901 reg_val &= field->mask;
1902 reg_val >>= field->shift;
1903 *val = reg_val;
1904
1905 return ret;
1906}
1907EXPORT_SYMBOL_GPL(regmap_fields_read);
1908
1909/**
Mark Brownb83a3132011-05-11 19:59:58 +02001910 * regmap_bulk_read(): Read multiple registers from the device
1911 *
Gerhard Sittig00933802013-11-11 10:42:36 +01001912 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02001913 * @reg: First register to be read from
1914 * @val: Pointer to store read value, in native register size for device
1915 * @val_count: Number of registers to read
1916 *
1917 * A value of zero will be returned on success, a negative errno will
1918 * be returned in error cases.
1919 */
1920int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1921 size_t val_count)
1922{
1923 int ret, i;
1924 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +01001925 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001926
Stephen Warrenf01ee602012-04-09 13:40:24 -06001927 if (reg % map->reg_stride)
1928 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001929
Stephen Boyd3b58ee12013-12-13 09:14:07 -08001930 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001931 /*
1932 * Some devices does not support bulk read, for
1933 * them we have a series of single read operations.
1934 */
1935 if (map->use_single_rw) {
1936 for (i = 0; i < val_count; i++) {
1937 ret = regmap_raw_read(map,
1938 reg + (i * map->reg_stride),
1939 val + (i * val_bytes),
1940 val_bytes);
1941 if (ret != 0)
1942 return ret;
1943 }
1944 } else {
1945 ret = regmap_raw_read(map, reg, val,
1946 val_bytes * val_count);
1947 if (ret != 0)
1948 return ret;
1949 }
Mark Brownde2d8082011-10-10 13:24:52 +01001950
1951 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08001952 map->format.parse_inplace(val + i);
Mark Brownde2d8082011-10-10 13:24:52 +01001953 } else {
1954 for (i = 0; i < val_count; i++) {
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301955 unsigned int ival;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001956 ret = regmap_read(map, reg + (i * map->reg_stride),
Mark Brown25061d22012-05-12 13:06:08 +01001957 &ival);
Mark Brownde2d8082011-10-10 13:24:52 +01001958 if (ret != 0)
1959 return ret;
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301960 memcpy(val + (i * val_bytes), &ival, val_bytes);
Mark Brownde2d8082011-10-10 13:24:52 +01001961 }
1962 }
Mark Brownb83a3132011-05-11 19:59:58 +02001963
1964 return 0;
1965}
1966EXPORT_SYMBOL_GPL(regmap_bulk_read);
1967
Mark Brown018690d2011-11-29 20:10:36 +00001968static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1969 unsigned int mask, unsigned int val,
1970 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +02001971{
1972 int ret;
Mark Brownd91e8db2011-11-18 16:03:50 +00001973 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +02001974
Mark Brownd91e8db2011-11-18 16:03:50 +00001975 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +02001976 if (ret != 0)
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001977 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001978
Mark Brownd91e8db2011-11-18 16:03:50 +00001979 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001980 tmp |= val & mask;
1981
Mark Brown018690d2011-11-29 20:10:36 +00001982 if (tmp != orig) {
Mark Brownd91e8db2011-11-18 16:03:50 +00001983 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +00001984 *change = true;
1985 } else {
1986 *change = false;
1987 }
Mark Brownb83a3132011-05-11 19:59:58 +02001988
Mark Brownb83a3132011-05-11 19:59:58 +02001989 return ret;
1990}
Mark Brown018690d2011-11-29 20:10:36 +00001991
1992/**
1993 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1994 *
1995 * @map: Register map to update
1996 * @reg: Register to update
1997 * @mask: Bitmask to change
1998 * @val: New value for bitmask
1999 *
2000 * Returns zero for success, a negative number on error.
2001 */
2002int regmap_update_bits(struct regmap *map, unsigned int reg,
2003 unsigned int mask, unsigned int val)
2004{
2005 bool change;
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002006 int ret;
2007
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002008 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002009 ret = _regmap_update_bits(map, reg, mask, val, &change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002010 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002011
2012 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00002013}
Mark Brownb83a3132011-05-11 19:59:58 +02002014EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +01002015
Mark Brown018690d2011-11-29 20:10:36 +00002016/**
Mark Brown915f4412013-10-09 13:30:10 +01002017 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2018 * map asynchronously
2019 *
2020 * @map: Register map to update
2021 * @reg: Register to update
2022 * @mask: Bitmask to change
2023 * @val: New value for bitmask
2024 *
2025 * With most buses the read must be done synchronously so this is most
2026 * useful for devices with a cache which do not need to interact with
2027 * the hardware to determine the current register value.
2028 *
2029 * Returns zero for success, a negative number on error.
2030 */
2031int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2032 unsigned int mask, unsigned int val)
2033{
2034 bool change;
2035 int ret;
2036
2037 map->lock(map->lock_arg);
2038
2039 map->async = true;
2040
2041 ret = _regmap_update_bits(map, reg, mask, val, &change);
2042
2043 map->async = false;
2044
2045 map->unlock(map->lock_arg);
2046
2047 return ret;
2048}
2049EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2050
2051/**
Mark Brown018690d2011-11-29 20:10:36 +00002052 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2053 * register map and report if updated
2054 *
2055 * @map: Register map to update
2056 * @reg: Register to update
2057 * @mask: Bitmask to change
2058 * @val: New value for bitmask
2059 * @change: Boolean indicating if a write was done
2060 *
2061 * Returns zero for success, a negative number on error.
2062 */
2063int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2064 unsigned int mask, unsigned int val,
2065 bool *change)
2066{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002067 int ret;
2068
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002069 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002070 ret = _regmap_update_bits(map, reg, mask, val, change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002071 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002072 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00002073}
2074EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2075
Mark Brown915f4412013-10-09 13:30:10 +01002076/**
2077 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2078 * register map asynchronously and report if
2079 * updated
2080 *
2081 * @map: Register map to update
2082 * @reg: Register to update
2083 * @mask: Bitmask to change
2084 * @val: New value for bitmask
2085 * @change: Boolean indicating if a write was done
2086 *
2087 * With most buses the read must be done synchronously so this is most
2088 * useful for devices with a cache which do not need to interact with
2089 * the hardware to determine the current register value.
2090 *
2091 * Returns zero for success, a negative number on error.
2092 */
2093int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2094 unsigned int mask, unsigned int val,
2095 bool *change)
2096{
2097 int ret;
2098
2099 map->lock(map->lock_arg);
2100
2101 map->async = true;
2102
2103 ret = _regmap_update_bits(map, reg, mask, val, change);
2104
2105 map->async = false;
2106
2107 map->unlock(map->lock_arg);
2108
2109 return ret;
2110}
2111EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2112
Mark Brown0d509f22013-01-27 22:07:38 +08002113void regmap_async_complete_cb(struct regmap_async *async, int ret)
2114{
2115 struct regmap *map = async->map;
2116 bool wake;
2117
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002118 trace_regmap_async_io_complete(map->dev);
2119
Mark Brown0d509f22013-01-27 22:07:38 +08002120 spin_lock(&map->async_lock);
Mark Brown7e09a972013-10-07 23:00:24 +01002121 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08002122 wake = list_empty(&map->async_list);
2123
2124 if (ret != 0)
2125 map->async_ret = ret;
2126
2127 spin_unlock(&map->async_lock);
2128
Mark Brown0d509f22013-01-27 22:07:38 +08002129 if (wake)
2130 wake_up(&map->async_waitq);
2131}
Axel Linf804fb52013-02-03 00:14:13 +08002132EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
Mark Brown0d509f22013-01-27 22:07:38 +08002133
2134static int regmap_async_is_done(struct regmap *map)
2135{
2136 unsigned long flags;
2137 int ret;
2138
2139 spin_lock_irqsave(&map->async_lock, flags);
2140 ret = list_empty(&map->async_list);
2141 spin_unlock_irqrestore(&map->async_lock, flags);
2142
2143 return ret;
2144}
2145
2146/**
2147 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2148 *
2149 * @map: Map to operate on.
2150 *
2151 * Blocks until any pending asynchronous I/O has completed. Returns
2152 * an error code for any failed I/O operations.
2153 */
2154int regmap_async_complete(struct regmap *map)
2155{
2156 unsigned long flags;
2157 int ret;
2158
2159 /* Nothing to do with no async support */
Daniel Mackf2e055e2013-07-04 13:11:03 +02002160 if (!map->bus || !map->bus->async_write)
Mark Brown0d509f22013-01-27 22:07:38 +08002161 return 0;
2162
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002163 trace_regmap_async_complete_start(map->dev);
2164
Mark Brown0d509f22013-01-27 22:07:38 +08002165 wait_event(map->async_waitq, regmap_async_is_done(map));
2166
2167 spin_lock_irqsave(&map->async_lock, flags);
2168 ret = map->async_ret;
2169 map->async_ret = 0;
2170 spin_unlock_irqrestore(&map->async_lock, flags);
2171
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002172 trace_regmap_async_complete_done(map->dev);
2173
Mark Brown0d509f22013-01-27 22:07:38 +08002174 return ret;
2175}
Mark Brownf88948e2013-02-05 13:53:26 +00002176EXPORT_SYMBOL_GPL(regmap_async_complete);
Mark Brown0d509f22013-01-27 22:07:38 +08002177
Mark Brown22f0d902012-01-21 12:01:14 +00002178/**
2179 * regmap_register_patch: Register and apply register updates to be applied
2180 * on device initialistion
2181 *
2182 * @map: Register map to apply updates to.
2183 * @regs: Values to update.
2184 * @num_regs: Number of entries in regs.
2185 *
2186 * Register a set of register updates to be applied to the device
2187 * whenever the device registers are synchronised with the cache and
2188 * apply them immediately. Typically this is used to apply
2189 * corrections to be applied to the device defaults on startup, such
2190 * as the updates some vendors provide to undocumented registers.
2191 */
2192int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2193 int num_regs)
2194{
Mark Brownaab13eb2013-07-11 12:41:44 +01002195 struct reg_default *p;
Mark Brown22f0d902012-01-21 12:01:14 +00002196 int i, ret;
2197 bool bypass;
2198
Cai Zhiyongbd60e382013-11-18 20:21:49 +08002199 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2200 num_regs))
2201 return 0;
2202
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002203 map->lock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00002204
2205 bypass = map->cache_bypass;
2206
2207 map->cache_bypass = true;
Mark Brown1a25f262013-10-10 20:55:03 +01002208 map->async = true;
Mark Brown22f0d902012-01-21 12:01:14 +00002209
2210 /* Write out first; it's useful to apply even if we fail later. */
2211 for (i = 0; i < num_regs; i++) {
2212 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2213 if (ret != 0) {
2214 dev_err(map->dev, "Failed to write %x = %x: %d\n",
2215 regs[i].reg, regs[i].def, ret);
2216 goto out;
2217 }
2218 }
2219
Mark Brownaab13eb2013-07-11 12:41:44 +01002220 p = krealloc(map->patch,
2221 sizeof(struct reg_default) * (map->patch_regs + num_regs),
2222 GFP_KERNEL);
2223 if (p) {
2224 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2225 map->patch = p;
2226 map->patch_regs += num_regs;
Mark Brown22f0d902012-01-21 12:01:14 +00002227 } else {
2228 ret = -ENOMEM;
2229 }
2230
2231out:
Mark Brown1a25f262013-10-10 20:55:03 +01002232 map->async = false;
Mark Brown22f0d902012-01-21 12:01:14 +00002233 map->cache_bypass = bypass;
2234
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002235 map->unlock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00002236
Mark Brown1a25f262013-10-10 20:55:03 +01002237 regmap_async_complete(map);
2238
Mark Brown22f0d902012-01-21 12:01:14 +00002239 return ret;
2240}
2241EXPORT_SYMBOL_GPL(regmap_register_patch);
2242
Mark Browneae4b512012-03-14 13:15:03 +00002243/*
Mark Browna6539c32012-02-17 14:20:14 -08002244 * regmap_get_val_bytes(): Report the size of a register value
2245 *
2246 * Report the size of a register value, mainly intended to for use by
2247 * generic infrastructure built on top of regmap.
2248 */
2249int regmap_get_val_bytes(struct regmap *map)
2250{
2251 if (map->format.format_write)
2252 return -EINVAL;
2253
2254 return map->format.val_bytes;
2255}
2256EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2257
Mark Brown31244e32011-07-20 22:56:53 +01002258static int __init regmap_initcall(void)
2259{
2260 regmap_debugfs_initcall();
2261
2262 return 0;
2263}
2264postcore_initcall(regmap_initcall);