blob: e4932a8f6c855f2ce4c4e392396e943c87946537 [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
Daeseok Youna7a037c2014-04-01 19:46:43 +0900763 if (dev) {
Michal Simek6cfec042014-02-10 16:22:33 +0100764 ret = regmap_attach_dev(dev, map, config);
765 if (ret != 0)
766 goto err_regcache;
Daeseok Youna7a037c2014-04-01 19:46:43 +0900767 }
Mark Brown72b39f62012-05-08 17:44:40 +0100768
Mark Brownb83a3132011-05-11 19:59:58 +0200769 return map;
770
Michal Simek6cfec042014-02-10 16:22:33 +0100771err_regcache:
Mark Brown72b39f62012-05-08 17:44:40 +0100772 regcache_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100773err_range:
774 regmap_range_exit(map);
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100775 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200776err_map:
777 kfree(map);
778err:
779 return ERR_PTR(ret);
780}
781EXPORT_SYMBOL_GPL(regmap_init);
782
Mark Brownc0eb4672012-01-30 19:56:52 +0000783static void devm_regmap_release(struct device *dev, void *res)
784{
785 regmap_exit(*(struct regmap **)res);
786}
787
788/**
789 * devm_regmap_init(): Initialise managed register map
790 *
791 * @dev: Device that will be interacted with
792 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600793 * @bus_context: Data passed to bus-specific callbacks
Mark Brownc0eb4672012-01-30 19:56:52 +0000794 * @config: Configuration for register map
795 *
796 * The return value will be an ERR_PTR() on error or a valid pointer
797 * to a struct regmap. This function should generally not be called
798 * directly, it should be called by bus-specific init functions. The
799 * map will be automatically freed by the device management code.
800 */
801struct regmap *devm_regmap_init(struct device *dev,
802 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600803 void *bus_context,
Mark Brownc0eb4672012-01-30 19:56:52 +0000804 const struct regmap_config *config)
805{
806 struct regmap **ptr, *regmap;
807
808 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
809 if (!ptr)
810 return ERR_PTR(-ENOMEM);
811
Stephen Warren0135bbc2012-04-04 15:48:30 -0600812 regmap = regmap_init(dev, bus, bus_context, config);
Mark Brownc0eb4672012-01-30 19:56:52 +0000813 if (!IS_ERR(regmap)) {
814 *ptr = regmap;
815 devres_add(dev, ptr);
816 } else {
817 devres_free(ptr);
818 }
819
820 return regmap;
821}
822EXPORT_SYMBOL_GPL(devm_regmap_init);
823
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100824static void regmap_field_init(struct regmap_field *rm_field,
825 struct regmap *regmap, struct reg_field reg_field)
826{
827 int field_bits = reg_field.msb - reg_field.lsb + 1;
828 rm_field->regmap = regmap;
829 rm_field->reg = reg_field.reg;
830 rm_field->shift = reg_field.lsb;
831 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
Kuninori Morimotoa0102372013-09-01 20:30:50 -0700832 rm_field->id_size = reg_field.id_size;
833 rm_field->id_offset = reg_field.id_offset;
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100834}
835
836/**
837 * devm_regmap_field_alloc(): Allocate and initialise a register field
838 * in a register map.
839 *
840 * @dev: Device that will be interacted with
841 * @regmap: regmap bank in which this register field is located.
842 * @reg_field: Register field with in the bank.
843 *
844 * The return value will be an ERR_PTR() on error or a valid pointer
845 * to a struct regmap_field. The regmap_field will be automatically freed
846 * by the device management code.
847 */
848struct regmap_field *devm_regmap_field_alloc(struct device *dev,
849 struct regmap *regmap, struct reg_field reg_field)
850{
851 struct regmap_field *rm_field = devm_kzalloc(dev,
852 sizeof(*rm_field), GFP_KERNEL);
853 if (!rm_field)
854 return ERR_PTR(-ENOMEM);
855
856 regmap_field_init(rm_field, regmap, reg_field);
857
858 return rm_field;
859
860}
861EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
862
863/**
864 * devm_regmap_field_free(): Free register field allocated using
865 * devm_regmap_field_alloc. Usally drivers need not call this function,
866 * as the memory allocated via devm will be freed as per device-driver
867 * life-cyle.
868 *
869 * @dev: Device that will be interacted with
870 * @field: regmap field which should be freed.
871 */
872void devm_regmap_field_free(struct device *dev,
873 struct regmap_field *field)
874{
875 devm_kfree(dev, field);
876}
877EXPORT_SYMBOL_GPL(devm_regmap_field_free);
878
879/**
880 * regmap_field_alloc(): Allocate and initialise a register field
881 * in a register map.
882 *
883 * @regmap: regmap bank in which this register field is located.
884 * @reg_field: Register field with in the bank.
885 *
886 * The return value will be an ERR_PTR() on error or a valid pointer
887 * to a struct regmap_field. The regmap_field should be freed by the
888 * user once its finished working with it using regmap_field_free().
889 */
890struct regmap_field *regmap_field_alloc(struct regmap *regmap,
891 struct reg_field reg_field)
892{
893 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
894
895 if (!rm_field)
896 return ERR_PTR(-ENOMEM);
897
898 regmap_field_init(rm_field, regmap, reg_field);
899
900 return rm_field;
901}
902EXPORT_SYMBOL_GPL(regmap_field_alloc);
903
904/**
905 * regmap_field_free(): Free register field allocated using regmap_field_alloc
906 *
907 * @field: regmap field which should be freed.
908 */
909void regmap_field_free(struct regmap_field *field)
910{
911 kfree(field);
912}
913EXPORT_SYMBOL_GPL(regmap_field_free);
914
Mark Brownb83a3132011-05-11 19:59:58 +0200915/**
Mark Brownbf315172011-12-03 17:06:20 +0000916 * regmap_reinit_cache(): Reinitialise the current register cache
917 *
918 * @map: Register map to operate on.
919 * @config: New configuration. Only the cache data will be used.
920 *
921 * Discard any existing register cache for the map and initialize a
922 * new cache. This can be used to restore the cache to defaults or to
923 * update the cache configuration to reflect runtime discovery of the
924 * hardware.
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100925 *
926 * No explicit locking is done here, the user needs to ensure that
927 * this function will not race with other calls to regmap.
Mark Brownbf315172011-12-03 17:06:20 +0000928 */
929int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
930{
Mark Brownbf315172011-12-03 17:06:20 +0000931 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000932 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000933
934 map->max_register = config->max_register;
935 map->writeable_reg = config->writeable_reg;
936 map->readable_reg = config->readable_reg;
937 map->volatile_reg = config->volatile_reg;
938 map->precious_reg = config->precious_reg;
939 map->cache_type = config->cache_type;
940
Stephen Warrend3c242e2012-04-04 15:48:29 -0600941 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +0000942
Mark Brown421e8d22012-01-20 13:39:37 +0000943 map->cache_bypass = false;
944 map->cache_only = false;
945
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100946 return regcache_init(map, config);
Mark Brownbf315172011-12-03 17:06:20 +0000947}
Mark Brown752a6a52012-05-14 10:00:12 +0100948EXPORT_SYMBOL_GPL(regmap_reinit_cache);
Mark Brownbf315172011-12-03 17:06:20 +0000949
950/**
Mark Brownb83a3132011-05-11 19:59:58 +0200951 * regmap_exit(): Free a previously allocated register map
952 */
953void regmap_exit(struct regmap *map)
954{
Mark Brown7e09a972013-10-07 23:00:24 +0100955 struct regmap_async *async;
956
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100957 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100958 regmap_debugfs_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100959 regmap_range_exit(map);
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800960 if (map->bus && map->bus->free_context)
Stephen Warren0135bbc2012-04-04 15:48:30 -0600961 map->bus->free_context(map->bus_context);
Mark Brownb83a3132011-05-11 19:59:58 +0200962 kfree(map->work_buf);
Mark Brown7e09a972013-10-07 23:00:24 +0100963 while (!list_empty(&map->async_free)) {
964 async = list_first_entry_or_null(&map->async_free,
965 struct regmap_async,
966 list);
967 list_del(&async->list);
968 kfree(async->work_buf);
969 kfree(async);
970 }
Mark Brownb83a3132011-05-11 19:59:58 +0200971 kfree(map);
972}
973EXPORT_SYMBOL_GPL(regmap_exit);
974
Mark Brown72b39f62012-05-08 17:44:40 +0100975static int dev_get_regmap_match(struct device *dev, void *res, void *data)
976{
977 struct regmap **r = res;
978 if (!r || !*r) {
979 WARN_ON(!r || !*r);
980 return 0;
981 }
982
983 /* If the user didn't specify a name match any */
984 if (data)
985 return (*r)->name == data;
986 else
987 return 1;
988}
989
990/**
991 * dev_get_regmap(): Obtain the regmap (if any) for a device
992 *
993 * @dev: Device to retrieve the map for
994 * @name: Optional name for the register map, usually NULL.
995 *
996 * Returns the regmap for the device if one is present, or NULL. If
997 * name is specified then it must match the name specified when
998 * registering the device, if it is NULL then the first regmap found
999 * will be used. Devices with multiple register maps are very rare,
1000 * generic code should normally not need to specify a name.
1001 */
1002struct regmap *dev_get_regmap(struct device *dev, const char *name)
1003{
1004 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1005 dev_get_regmap_match, (void *)name);
1006
1007 if (!r)
1008 return NULL;
1009 return *r;
1010}
1011EXPORT_SYMBOL_GPL(dev_get_regmap);
1012
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001013static int _regmap_select_page(struct regmap *map, unsigned int *reg,
Mark Brown98bc7df2012-10-04 17:31:11 +01001014 struct regmap_range_node *range,
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001015 unsigned int val_num)
1016{
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001017 void *orig_work_buf;
1018 unsigned int win_offset;
1019 unsigned int win_page;
1020 bool page_chg;
1021 int ret;
1022
Mark Brown98bc7df2012-10-04 17:31:11 +01001023 win_offset = (*reg - range->range_min) % range->window_len;
1024 win_page = (*reg - range->range_min) / range->window_len;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001025
Mark Brown98bc7df2012-10-04 17:31:11 +01001026 if (val_num > 1) {
1027 /* Bulk write shouldn't cross range boundary */
1028 if (*reg + val_num - 1 > range->range_max)
1029 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001030
Mark Brown98bc7df2012-10-04 17:31:11 +01001031 /* ... or single page boundary */
1032 if (val_num > range->window_len - win_offset)
1033 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001034 }
1035
Mark Brown98bc7df2012-10-04 17:31:11 +01001036 /* It is possible to have selector register inside data window.
1037 In that case, selector register is located on every page and
1038 it needs no page switching, when accessed alone. */
1039 if (val_num > 1 ||
1040 range->window_start + win_offset != range->selector_reg) {
1041 /* Use separate work_buf during page switching */
1042 orig_work_buf = map->work_buf;
1043 map->work_buf = map->selector_work_buf;
1044
1045 ret = _regmap_update_bits(map, range->selector_reg,
1046 range->selector_mask,
1047 win_page << range->selector_shift,
1048 &page_chg);
1049
1050 map->work_buf = orig_work_buf;
1051
Mark Brown0ff3e622012-10-04 17:39:13 +01001052 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001053 return ret;
1054 }
1055
1056 *reg = range->window_start + win_offset;
1057
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001058 return 0;
1059}
1060
Mark Brown584de322013-03-13 19:19:34 +00001061int _regmap_raw_write(struct regmap *map, unsigned int reg,
Mark Brown0a819802013-10-09 12:28:52 +01001062 const void *val, size_t val_len)
Mark Brownb83a3132011-05-11 19:59:58 +02001063{
Mark Brown98bc7df2012-10-04 17:31:11 +01001064 struct regmap_range_node *range;
Mark Brown0d509f22013-01-27 22:07:38 +08001065 unsigned long flags;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001066 u8 *u8 = map->work_buf;
Mark Brown0d509f22013-01-27 22:07:38 +08001067 void *work_val = map->work_buf + map->format.reg_bytes +
1068 map->format.pad_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +02001069 void *buf;
1070 int ret = -ENOTSUPP;
1071 size_t len;
Mark Brown73304782011-07-24 11:46:20 +01001072 int i;
1073
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001074 WARN_ON(!map->bus);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001075
Mark Brown73304782011-07-24 11:46:20 +01001076 /* Check for unwritable registers before we start */
1077 if (map->writeable_reg)
1078 for (i = 0; i < val_len / map->format.val_bytes; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -06001079 if (!map->writeable_reg(map->dev,
1080 reg + (i * map->reg_stride)))
Mark Brown73304782011-07-24 11:46:20 +01001081 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001082
Laxman Dewanganc9157192012-02-10 21:30:27 +05301083 if (!map->cache_bypass && map->format.parse_val) {
1084 unsigned int ival;
1085 int val_bytes = map->format.val_bytes;
1086 for (i = 0; i < val_len / val_bytes; i++) {
Stephen Warren5a08d152013-03-20 17:02:02 -06001087 ival = map->format.parse_val(val + (i * val_bytes));
Stephen Warrenf01ee602012-04-09 13:40:24 -06001088 ret = regcache_write(map, reg + (i * map->reg_stride),
1089 ival);
Laxman Dewanganc9157192012-02-10 21:30:27 +05301090 if (ret) {
1091 dev_err(map->dev,
Mark Brown6d04b8a2012-10-26 19:05:32 +01001092 "Error in caching of register: %x ret: %d\n",
Laxman Dewanganc9157192012-02-10 21:30:27 +05301093 reg + i, ret);
1094 return ret;
1095 }
1096 }
1097 if (map->cache_only) {
1098 map->cache_dirty = true;
1099 return 0;
1100 }
1101 }
1102
Mark Brown98bc7df2012-10-04 17:31:11 +01001103 range = _regmap_range_lookup(map, reg);
1104 if (range) {
Mark Brown8a2ceac2012-10-04 18:20:18 +01001105 int val_num = val_len / map->format.val_bytes;
1106 int win_offset = (reg - range->range_min) % range->window_len;
1107 int win_residue = range->window_len - win_offset;
1108
1109 /* If the write goes beyond the end of the window split it */
1110 while (val_num > win_residue) {
Fabio Estevam1a61cfe2012-10-25 14:07:18 -02001111 dev_dbg(map->dev, "Writing window %d/%zu\n",
Mark Brown8a2ceac2012-10-04 18:20:18 +01001112 win_residue, val_len / map->format.val_bytes);
1113 ret = _regmap_raw_write(map, reg, val, win_residue *
Mark Brown0a819802013-10-09 12:28:52 +01001114 map->format.val_bytes);
Mark Brown8a2ceac2012-10-04 18:20:18 +01001115 if (ret != 0)
1116 return ret;
1117
1118 reg += win_residue;
1119 val_num -= win_residue;
1120 val += win_residue * map->format.val_bytes;
1121 val_len -= win_residue * map->format.val_bytes;
1122
1123 win_offset = (reg - range->range_min) %
1124 range->window_len;
1125 win_residue = range->window_len - win_offset;
1126 }
1127
1128 ret = _regmap_select_page(map, &reg, range, val_num);
Mark Brown0ff3e622012-10-04 17:39:13 +01001129 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001130 return ret;
1131 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001132
Marc Reillyd939fb92012-03-16 12:11:43 +11001133 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001134
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001135 u8[0] |= map->write_flag_mask;
1136
Mark Brown651e0132013-10-08 18:37:36 +01001137 /*
1138 * Essentially all I/O mechanisms will be faster with a single
1139 * buffer to write. Since register syncs often generate raw
1140 * writes of single registers optimise that case.
1141 */
1142 if (val != work_val && val_len == map->format.val_bytes) {
1143 memcpy(work_val, val, map->format.val_bytes);
1144 val = work_val;
1145 }
1146
Mark Brown0a819802013-10-09 12:28:52 +01001147 if (map->async && map->bus->async_write) {
Mark Brown7e09a972013-10-07 23:00:24 +01001148 struct regmap_async *async;
Mark Brown0d509f22013-01-27 22:07:38 +08001149
Mark Brownfe7d4cc2013-02-21 19:05:48 +00001150 trace_regmap_async_write_start(map->dev, reg, val_len);
1151
Mark Brown7e09a972013-10-07 23:00:24 +01001152 spin_lock_irqsave(&map->async_lock, flags);
1153 async = list_first_entry_or_null(&map->async_free,
1154 struct regmap_async,
1155 list);
1156 if (async)
1157 list_del(&async->list);
1158 spin_unlock_irqrestore(&map->async_lock, flags);
1159
1160 if (!async) {
1161 async = map->bus->async_alloc();
1162 if (!async)
1163 return -ENOMEM;
1164
1165 async->work_buf = kzalloc(map->format.buf_size,
1166 GFP_KERNEL | GFP_DMA);
1167 if (!async->work_buf) {
1168 kfree(async);
1169 return -ENOMEM;
1170 }
Mark Brown0d509f22013-01-27 22:07:38 +08001171 }
1172
Mark Brown0d509f22013-01-27 22:07:38 +08001173 async->map = map;
1174
1175 /* If the caller supplied the value we can use it safely. */
1176 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1177 map->format.reg_bytes + map->format.val_bytes);
Mark Brown0d509f22013-01-27 22:07:38 +08001178
1179 spin_lock_irqsave(&map->async_lock, flags);
1180 list_add_tail(&async->list, &map->async_list);
1181 spin_unlock_irqrestore(&map->async_lock, flags);
1182
Mark Brown04c50cc2013-10-10 22:38:29 +01001183 if (val != work_val)
1184 ret = map->bus->async_write(map->bus_context,
1185 async->work_buf,
1186 map->format.reg_bytes +
1187 map->format.pad_bytes,
1188 val, val_len, async);
1189 else
1190 ret = map->bus->async_write(map->bus_context,
1191 async->work_buf,
1192 map->format.reg_bytes +
1193 map->format.pad_bytes +
1194 val_len, NULL, 0, async);
Mark Brown0d509f22013-01-27 22:07:38 +08001195
1196 if (ret != 0) {
1197 dev_err(map->dev, "Failed to schedule write: %d\n",
1198 ret);
1199
1200 spin_lock_irqsave(&map->async_lock, flags);
Mark Brown7e09a972013-10-07 23:00:24 +01001201 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08001202 spin_unlock_irqrestore(&map->async_lock, flags);
Mark Brown0d509f22013-01-27 22:07:38 +08001203 }
Mark Brownf951b652013-03-27 13:08:44 +00001204
1205 return ret;
Mark Brown0d509f22013-01-27 22:07:38 +08001206 }
1207
Mark Brownfb2736b2011-07-24 21:30:55 +01001208 trace_regmap_hw_write_start(map->dev, reg,
1209 val_len / map->format.val_bytes);
1210
Mark Brown2547e202011-07-20 21:47:22 +01001211 /* If we're doing a single register write we can probably just
1212 * send the work_buf directly, otherwise try to do a gather
1213 * write.
1214 */
Mark Brown0d509f22013-01-27 22:07:38 +08001215 if (val == work_val)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001216 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001217 map->format.reg_bytes +
1218 map->format.pad_bytes +
1219 val_len);
Mark Brown2547e202011-07-20 21:47:22 +01001220 else if (map->bus->gather_write)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001221 ret = map->bus->gather_write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001222 map->format.reg_bytes +
1223 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +02001224 val, val_len);
1225
Mark Brown2547e202011-07-20 21:47:22 +01001226 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +02001227 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +00001228 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1229 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +02001230 if (!buf)
1231 return -ENOMEM;
1232
1233 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +00001234 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1235 val, val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -06001236 ret = map->bus->write(map->bus_context, buf, len);
Mark Brownb83a3132011-05-11 19:59:58 +02001237
1238 kfree(buf);
1239 }
1240
Mark Brownfb2736b2011-07-24 21:30:55 +01001241 trace_regmap_hw_write_done(map->dev, reg,
1242 val_len / map->format.val_bytes);
1243
Mark Brownb83a3132011-05-11 19:59:58 +02001244 return ret;
1245}
1246
Mark Brown221ad7f2013-03-26 21:24:20 +00001247/**
1248 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1249 *
1250 * @map: Map to check.
1251 */
1252bool regmap_can_raw_write(struct regmap *map)
1253{
1254 return map->bus && map->format.format_val && map->format.format_reg;
1255}
1256EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1257
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001258static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1259 unsigned int val)
1260{
1261 int ret;
1262 struct regmap_range_node *range;
1263 struct regmap *map = context;
1264
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001265 WARN_ON(!map->bus || !map->format.format_write);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001266
1267 range = _regmap_range_lookup(map, reg);
1268 if (range) {
1269 ret = _regmap_select_page(map, &reg, range, 1);
1270 if (ret != 0)
1271 return ret;
1272 }
1273
1274 map->format.format_write(map, reg, val);
1275
1276 trace_regmap_hw_write_start(map->dev, reg, 1);
1277
1278 ret = map->bus->write(map->bus_context, map->work_buf,
1279 map->format.buf_size);
1280
1281 trace_regmap_hw_write_done(map->dev, reg, 1);
1282
1283 return ret;
1284}
1285
1286static int _regmap_bus_raw_write(void *context, unsigned int reg,
1287 unsigned int val)
1288{
1289 struct regmap *map = context;
1290
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001291 WARN_ON(!map->bus || !map->format.format_val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001292
1293 map->format.format_val(map->work_buf + map->format.reg_bytes
1294 + map->format.pad_bytes, val, 0);
1295 return _regmap_raw_write(map, reg,
1296 map->work_buf +
1297 map->format.reg_bytes +
1298 map->format.pad_bytes,
Mark Brown0a819802013-10-09 12:28:52 +01001299 map->format.val_bytes);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001300}
1301
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001302static inline void *_regmap_map_get_context(struct regmap *map)
1303{
1304 return (map->bus) ? map : map->bus_context;
1305}
1306
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +01001307int _regmap_write(struct regmap *map, unsigned int reg,
1308 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +02001309{
Mark Brownfb2736b2011-07-24 21:30:55 +01001310 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001311 void *context = _regmap_map_get_context(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001312
Ionut Nicu515f2262013-08-09 12:09:20 +02001313 if (!regmap_writeable(map, reg))
1314 return -EIO;
1315
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001316 if (!map->cache_bypass && !map->defer_caching) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001317 ret = regcache_write(map, reg, val);
1318 if (ret != 0)
1319 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001320 if (map->cache_only) {
1321 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001322 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001323 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001324 }
1325
Mark Brown1044c182012-07-06 14:10:23 +01001326#ifdef LOG_DEVICE
1327 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1328 dev_info(map->dev, "%x <= %x\n", reg, val);
1329#endif
1330
Mark Brownfb2736b2011-07-24 21:30:55 +01001331 trace_regmap_reg_write(map->dev, reg, val);
1332
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001333 return map->reg_write(context, reg, val);
Mark Brownb83a3132011-05-11 19:59:58 +02001334}
1335
1336/**
1337 * regmap_write(): Write a value to a single register
1338 *
1339 * @map: Register map to write to
1340 * @reg: Register to write to
1341 * @val: Value to be written
1342 *
1343 * A value of zero will be returned on success, a negative errno will
1344 * be returned in error cases.
1345 */
1346int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1347{
1348 int ret;
1349
Stephen Warrenf01ee602012-04-09 13:40:24 -06001350 if (reg % map->reg_stride)
1351 return -EINVAL;
1352
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001353 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001354
1355 ret = _regmap_write(map, reg, val);
1356
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001357 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001358
1359 return ret;
1360}
1361EXPORT_SYMBOL_GPL(regmap_write);
1362
1363/**
Mark Brown915f4412013-10-09 13:30:10 +01001364 * regmap_write_async(): Write a value to a single register asynchronously
1365 *
1366 * @map: Register map to write to
1367 * @reg: Register to write to
1368 * @val: Value to be written
1369 *
1370 * A value of zero will be returned on success, a negative errno will
1371 * be returned in error cases.
1372 */
1373int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1374{
1375 int ret;
1376
1377 if (reg % map->reg_stride)
1378 return -EINVAL;
1379
1380 map->lock(map->lock_arg);
1381
1382 map->async = true;
1383
1384 ret = _regmap_write(map, reg, val);
1385
1386 map->async = false;
1387
1388 map->unlock(map->lock_arg);
1389
1390 return ret;
1391}
1392EXPORT_SYMBOL_GPL(regmap_write_async);
1393
1394/**
Mark Brownb83a3132011-05-11 19:59:58 +02001395 * regmap_raw_write(): Write raw values to one or more registers
1396 *
1397 * @map: Register map to write to
1398 * @reg: Initial register to write to
1399 * @val: Block of data to be written, laid out for direct transmission to the
1400 * device
1401 * @val_len: Length of data pointed to by val.
1402 *
1403 * This function is intended to be used for things like firmware
1404 * download where a large block of data needs to be transferred to the
1405 * device. No formatting will be done on the data provided.
1406 *
1407 * A value of zero will be returned on success, a negative errno will
1408 * be returned in error cases.
1409 */
1410int regmap_raw_write(struct regmap *map, unsigned int reg,
1411 const void *val, size_t val_len)
1412{
1413 int ret;
1414
Mark Brown221ad7f2013-03-26 21:24:20 +00001415 if (!regmap_can_raw_write(map))
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001416 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001417 if (val_len % map->format.val_bytes)
1418 return -EINVAL;
1419
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001420 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001421
Mark Brown0a819802013-10-09 12:28:52 +01001422 ret = _regmap_raw_write(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001423
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001424 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001425
1426 return ret;
1427}
1428EXPORT_SYMBOL_GPL(regmap_raw_write);
1429
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001430/**
1431 * regmap_field_write(): Write a value to a single register field
1432 *
1433 * @field: Register field to write to
1434 * @val: Value to be written
1435 *
1436 * A value of zero will be returned on success, a negative errno will
1437 * be returned in error cases.
1438 */
1439int regmap_field_write(struct regmap_field *field, unsigned int val)
1440{
1441 return regmap_update_bits(field->regmap, field->reg,
1442 field->mask, val << field->shift);
1443}
1444EXPORT_SYMBOL_GPL(regmap_field_write);
1445
Kuninori Morimotofdf20022013-09-01 20:24:50 -07001446/**
1447 * regmap_field_update_bits(): Perform a read/modify/write cycle
1448 * on the register field
1449 *
1450 * @field: Register field to write to
1451 * @mask: Bitmask to change
1452 * @val: Value to be written
1453 *
1454 * A value of zero will be returned on success, a negative errno will
1455 * be returned in error cases.
1456 */
1457int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1458{
1459 mask = (mask << field->shift) & field->mask;
1460
1461 return regmap_update_bits(field->regmap, field->reg,
1462 mask, val << field->shift);
1463}
1464EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1465
Kuninori Morimotoa0102372013-09-01 20:30:50 -07001466/**
1467 * regmap_fields_write(): Write a value to a single register field with port ID
1468 *
1469 * @field: Register field to write to
1470 * @id: port ID
1471 * @val: Value to be written
1472 *
1473 * A value of zero will be returned on success, a negative errno will
1474 * be returned in error cases.
1475 */
1476int regmap_fields_write(struct regmap_field *field, unsigned int id,
1477 unsigned int val)
1478{
1479 if (id >= field->id_size)
1480 return -EINVAL;
1481
1482 return regmap_update_bits(field->regmap,
1483 field->reg + (field->id_offset * id),
1484 field->mask, val << field->shift);
1485}
1486EXPORT_SYMBOL_GPL(regmap_fields_write);
1487
1488/**
1489 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1490 * on the register field
1491 *
1492 * @field: Register field to write to
1493 * @id: port ID
1494 * @mask: Bitmask to change
1495 * @val: Value to be written
1496 *
1497 * A value of zero will be returned on success, a negative errno will
1498 * be returned in error cases.
1499 */
1500int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1501 unsigned int mask, unsigned int val)
1502{
1503 if (id >= field->id_size)
1504 return -EINVAL;
1505
1506 mask = (mask << field->shift) & field->mask;
1507
1508 return regmap_update_bits(field->regmap,
1509 field->reg + (field->id_offset * id),
1510 mask, val << field->shift);
1511}
1512EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1513
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301514/*
1515 * regmap_bulk_write(): Write multiple registers to the device
1516 *
1517 * @map: Register map to write to
1518 * @reg: First register to be write from
1519 * @val: Block of data to be written, in native register size for device
1520 * @val_count: Number of registers to write
1521 *
1522 * This function is intended to be used for writing a large block of
Nestor Ovroy31b35e9e2013-01-18 16:51:03 +01001523 * data to the device either in single transfer or multiple transfer.
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301524 *
1525 * A value of zero will be returned on success, a negative errno will
1526 * be returned in error cases.
1527 */
1528int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1529 size_t val_count)
1530{
1531 int ret = 0, i;
1532 size_t val_bytes = map->format.val_bytes;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301533
Stephen Boydf4298362013-12-26 13:52:04 -08001534 if (map->bus && !map->format.parse_inplace)
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301535 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001536 if (reg % map->reg_stride)
1537 return -EINVAL;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301538
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001539 map->lock(map->lock_arg);
Stephen Boydf4298362013-12-26 13:52:04 -08001540 /*
1541 * Some devices don't support bulk write, for
1542 * them we have a series of single write operations.
1543 */
1544 if (!map->bus || map->use_single_rw) {
1545 for (i = 0; i < val_count; i++) {
1546 unsigned int ival;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301547
Stephen Boydf4298362013-12-26 13:52:04 -08001548 switch (val_bytes) {
1549 case 1:
1550 ival = *(u8 *)(val + (i * val_bytes));
1551 break;
1552 case 2:
1553 ival = *(u16 *)(val + (i * val_bytes));
1554 break;
1555 case 4:
1556 ival = *(u32 *)(val + (i * val_bytes));
1557 break;
1558#ifdef CONFIG_64BIT
1559 case 8:
1560 ival = *(u64 *)(val + (i * val_bytes));
1561 break;
1562#endif
1563 default:
1564 ret = -EINVAL;
1565 goto out;
1566 }
1567
1568 ret = _regmap_write(map, reg + (i * map->reg_stride),
1569 ival);
1570 if (ret != 0)
1571 goto out;
1572 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301573 } else {
Stephen Boydf4298362013-12-26 13:52:04 -08001574 void *wval;
1575
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301576 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1577 if (!wval) {
1578 ret = -ENOMEM;
1579 dev_err(map->dev, "Error in memory allocation\n");
1580 goto out;
1581 }
1582 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08001583 map->format.parse_inplace(wval + i);
Stephen Boydf4298362013-12-26 13:52:04 -08001584
Mark Brown0a819802013-10-09 12:28:52 +01001585 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301586
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301587 kfree(wval);
Stephen Boydf4298362013-12-26 13:52:04 -08001588 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301589out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001590 map->unlock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301591 return ret;
1592}
1593EXPORT_SYMBOL_GPL(regmap_bulk_write);
1594
Anthony Oleche33fabd2013-10-11 15:31:11 +01001595/*
1596 * regmap_multi_reg_write(): Write multiple registers to the device
1597 *
1598 * where the set of register are supplied in any order
1599 *
1600 * @map: Register map to write to
1601 * @regs: Array of structures containing register,value to be written
1602 * @num_regs: Number of registers to write
1603 *
1604 * This function is intended to be used for writing a large block of data
1605 * atomically to the device in single transfer for those I2C client devices
1606 * that implement this alternative block write mode.
1607 *
1608 * A value of zero will be returned on success, a negative errno will
1609 * be returned in error cases.
1610 */
1611int regmap_multi_reg_write(struct regmap *map, struct reg_default *regs,
1612 int num_regs)
1613{
1614 int ret = 0, i;
1615
1616 for (i = 0; i < num_regs; i++) {
1617 int reg = regs[i].reg;
1618 if (reg % map->reg_stride)
1619 return -EINVAL;
1620 }
1621
1622 map->lock(map->lock_arg);
1623
1624 for (i = 0; i < num_regs; i++) {
1625 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1626 if (ret != 0)
1627 goto out;
1628 }
1629out:
1630 map->unlock(map->lock_arg);
1631
1632 return ret;
1633}
1634EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
1635
Mark Brown0d509f22013-01-27 22:07:38 +08001636/**
1637 * regmap_raw_write_async(): Write raw values to one or more registers
1638 * asynchronously
1639 *
1640 * @map: Register map to write to
1641 * @reg: Initial register to write to
1642 * @val: Block of data to be written, laid out for direct transmission to the
1643 * device. Must be valid until regmap_async_complete() is called.
1644 * @val_len: Length of data pointed to by val.
1645 *
1646 * This function is intended to be used for things like firmware
1647 * download where a large block of data needs to be transferred to the
1648 * device. No formatting will be done on the data provided.
1649 *
1650 * If supported by the underlying bus the write will be scheduled
1651 * asynchronously, helping maximise I/O speed on higher speed buses
1652 * like SPI. regmap_async_complete() can be called to ensure that all
1653 * asynchrnous writes have been completed.
1654 *
1655 * A value of zero will be returned on success, a negative errno will
1656 * be returned in error cases.
1657 */
1658int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1659 const void *val, size_t val_len)
1660{
1661 int ret;
1662
1663 if (val_len % map->format.val_bytes)
1664 return -EINVAL;
1665 if (reg % map->reg_stride)
1666 return -EINVAL;
1667
1668 map->lock(map->lock_arg);
1669
Mark Brown0a819802013-10-09 12:28:52 +01001670 map->async = true;
1671
1672 ret = _regmap_raw_write(map, reg, val, val_len);
1673
1674 map->async = false;
Mark Brown0d509f22013-01-27 22:07:38 +08001675
1676 map->unlock(map->lock_arg);
1677
1678 return ret;
1679}
1680EXPORT_SYMBOL_GPL(regmap_raw_write_async);
1681
Mark Brownb83a3132011-05-11 19:59:58 +02001682static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1683 unsigned int val_len)
1684{
Mark Brown98bc7df2012-10-04 17:31:11 +01001685 struct regmap_range_node *range;
Mark Brownb83a3132011-05-11 19:59:58 +02001686 u8 *u8 = map->work_buf;
1687 int ret;
1688
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001689 WARN_ON(!map->bus);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001690
Mark Brown98bc7df2012-10-04 17:31:11 +01001691 range = _regmap_range_lookup(map, reg);
1692 if (range) {
1693 ret = _regmap_select_page(map, &reg, range,
1694 val_len / map->format.val_bytes);
Mark Brown0ff3e622012-10-04 17:39:13 +01001695 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001696 return ret;
1697 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001698
Marc Reillyd939fb92012-03-16 12:11:43 +11001699 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001700
1701 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001702 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +02001703 * register addresss; since it's always the high bits for all
1704 * current formats we can do this here rather than in
1705 * formatting. This may break if we get interesting formats.
1706 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001707 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001708
Mark Brownfb2736b2011-07-24 21:30:55 +01001709 trace_regmap_hw_read_start(map->dev, reg,
1710 val_len / map->format.val_bytes);
1711
Stephen Warren0135bbc2012-04-04 15:48:30 -06001712 ret = map->bus->read(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001713 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +01001714 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001715
Mark Brownfb2736b2011-07-24 21:30:55 +01001716 trace_regmap_hw_read_done(map->dev, reg,
1717 val_len / map->format.val_bytes);
1718
1719 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001720}
1721
Andrey Smirnovad278402013-01-12 12:54:12 -08001722static int _regmap_bus_read(void *context, unsigned int reg,
1723 unsigned int *val)
1724{
1725 int ret;
1726 struct regmap *map = context;
1727
1728 if (!map->format.parse_val)
1729 return -EINVAL;
1730
1731 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
1732 if (ret == 0)
1733 *val = map->format.parse_val(map->work_buf);
1734
1735 return ret;
1736}
1737
Mark Brownb83a3132011-05-11 19:59:58 +02001738static int _regmap_read(struct regmap *map, unsigned int reg,
1739 unsigned int *val)
1740{
1741 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001742 void *context = _regmap_map_get_context(map);
1743
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001744 WARN_ON(!map->reg_read);
Mark Brownb83a3132011-05-11 19:59:58 +02001745
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001746 if (!map->cache_bypass) {
1747 ret = regcache_read(map, reg, val);
1748 if (ret == 0)
1749 return 0;
1750 }
1751
1752 if (map->cache_only)
1753 return -EBUSY;
1754
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001755 ret = map->reg_read(context, reg, val);
Mark Brownfb2736b2011-07-24 21:30:55 +01001756 if (ret == 0) {
Mark Brown1044c182012-07-06 14:10:23 +01001757#ifdef LOG_DEVICE
1758 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1759 dev_info(map->dev, "%x => %x\n", reg, *val);
1760#endif
1761
Mark Brownfb2736b2011-07-24 21:30:55 +01001762 trace_regmap_reg_read(map->dev, reg, *val);
Mark Brownb83a3132011-05-11 19:59:58 +02001763
Andrey Smirnovad278402013-01-12 12:54:12 -08001764 if (!map->cache_bypass)
1765 regcache_write(map, reg, *val);
1766 }
Mark Brownf2985362012-04-30 21:25:05 +01001767
Mark Brownb83a3132011-05-11 19:59:58 +02001768 return ret;
1769}
1770
1771/**
1772 * regmap_read(): Read a value from a single register
1773 *
Gerhard Sittig00933802013-11-11 10:42:36 +01001774 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02001775 * @reg: Register to be read from
1776 * @val: Pointer to store read value
1777 *
1778 * A value of zero will be returned on success, a negative errno will
1779 * be returned in error cases.
1780 */
1781int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1782{
1783 int ret;
1784
Stephen Warrenf01ee602012-04-09 13:40:24 -06001785 if (reg % map->reg_stride)
1786 return -EINVAL;
1787
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001788 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001789
1790 ret = _regmap_read(map, reg, val);
1791
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001792 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001793
1794 return ret;
1795}
1796EXPORT_SYMBOL_GPL(regmap_read);
1797
1798/**
1799 * regmap_raw_read(): Read raw data from the device
1800 *
Gerhard Sittig00933802013-11-11 10:42:36 +01001801 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02001802 * @reg: First register to be read from
1803 * @val: Pointer to store read value
1804 * @val_len: Size of data to read
1805 *
1806 * A value of zero will be returned on success, a negative errno will
1807 * be returned in error cases.
1808 */
1809int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1810 size_t val_len)
1811{
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001812 size_t val_bytes = map->format.val_bytes;
1813 size_t val_count = val_len / val_bytes;
1814 unsigned int v;
1815 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +01001816
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001817 if (!map->bus)
1818 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001819 if (val_len % map->format.val_bytes)
1820 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001821 if (reg % map->reg_stride)
1822 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001823
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001824 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001825
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001826 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1827 map->cache_type == REGCACHE_NONE) {
1828 /* Physical block read if there's no cache involved */
1829 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001830
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001831 } else {
1832 /* Otherwise go word by word for the cache; should be low
1833 * cost as we expect to hit the cache.
1834 */
1835 for (i = 0; i < val_count; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -06001836 ret = _regmap_read(map, reg + (i * map->reg_stride),
1837 &v);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001838 if (ret != 0)
1839 goto out;
1840
Marc Reillyd939fb92012-03-16 12:11:43 +11001841 map->format.format_val(val + (i * val_bytes), v, 0);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001842 }
1843 }
1844
1845 out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001846 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001847
1848 return ret;
1849}
1850EXPORT_SYMBOL_GPL(regmap_raw_read);
1851
1852/**
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001853 * regmap_field_read(): Read a value to a single register field
1854 *
1855 * @field: Register field to read from
1856 * @val: Pointer to store read value
1857 *
1858 * A value of zero will be returned on success, a negative errno will
1859 * be returned in error cases.
1860 */
1861int regmap_field_read(struct regmap_field *field, unsigned int *val)
1862{
1863 int ret;
1864 unsigned int reg_val;
1865 ret = regmap_read(field->regmap, field->reg, &reg_val);
1866 if (ret != 0)
1867 return ret;
1868
1869 reg_val &= field->mask;
1870 reg_val >>= field->shift;
1871 *val = reg_val;
1872
1873 return ret;
1874}
1875EXPORT_SYMBOL_GPL(regmap_field_read);
1876
1877/**
Kuninori Morimotoa0102372013-09-01 20:30:50 -07001878 * regmap_fields_read(): Read a value to a single register field with port ID
1879 *
1880 * @field: Register field to read from
1881 * @id: port ID
1882 * @val: Pointer to store read value
1883 *
1884 * A value of zero will be returned on success, a negative errno will
1885 * be returned in error cases.
1886 */
1887int regmap_fields_read(struct regmap_field *field, unsigned int id,
1888 unsigned int *val)
1889{
1890 int ret;
1891 unsigned int reg_val;
1892
1893 if (id >= field->id_size)
1894 return -EINVAL;
1895
1896 ret = regmap_read(field->regmap,
1897 field->reg + (field->id_offset * id),
1898 &reg_val);
1899 if (ret != 0)
1900 return ret;
1901
1902 reg_val &= field->mask;
1903 reg_val >>= field->shift;
1904 *val = reg_val;
1905
1906 return ret;
1907}
1908EXPORT_SYMBOL_GPL(regmap_fields_read);
1909
1910/**
Mark Brownb83a3132011-05-11 19:59:58 +02001911 * regmap_bulk_read(): Read multiple registers from the device
1912 *
Gerhard Sittig00933802013-11-11 10:42:36 +01001913 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02001914 * @reg: First register to be read from
1915 * @val: Pointer to store read value, in native register size for device
1916 * @val_count: Number of registers to read
1917 *
1918 * A value of zero will be returned on success, a negative errno will
1919 * be returned in error cases.
1920 */
1921int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1922 size_t val_count)
1923{
1924 int ret, i;
1925 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +01001926 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001927
Stephen Warrenf01ee602012-04-09 13:40:24 -06001928 if (reg % map->reg_stride)
1929 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001930
Stephen Boyd3b58ee12013-12-13 09:14:07 -08001931 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001932 /*
1933 * Some devices does not support bulk read, for
1934 * them we have a series of single read operations.
1935 */
1936 if (map->use_single_rw) {
1937 for (i = 0; i < val_count; i++) {
1938 ret = regmap_raw_read(map,
1939 reg + (i * map->reg_stride),
1940 val + (i * val_bytes),
1941 val_bytes);
1942 if (ret != 0)
1943 return ret;
1944 }
1945 } else {
1946 ret = regmap_raw_read(map, reg, val,
1947 val_bytes * val_count);
1948 if (ret != 0)
1949 return ret;
1950 }
Mark Brownde2d8082011-10-10 13:24:52 +01001951
1952 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08001953 map->format.parse_inplace(val + i);
Mark Brownde2d8082011-10-10 13:24:52 +01001954 } else {
1955 for (i = 0; i < val_count; i++) {
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301956 unsigned int ival;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001957 ret = regmap_read(map, reg + (i * map->reg_stride),
Mark Brown25061d22012-05-12 13:06:08 +01001958 &ival);
Mark Brownde2d8082011-10-10 13:24:52 +01001959 if (ret != 0)
1960 return ret;
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301961 memcpy(val + (i * val_bytes), &ival, val_bytes);
Mark Brownde2d8082011-10-10 13:24:52 +01001962 }
1963 }
Mark Brownb83a3132011-05-11 19:59:58 +02001964
1965 return 0;
1966}
1967EXPORT_SYMBOL_GPL(regmap_bulk_read);
1968
Mark Brown018690d2011-11-29 20:10:36 +00001969static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1970 unsigned int mask, unsigned int val,
1971 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +02001972{
1973 int ret;
Mark Brownd91e8db22011-11-18 16:03:50 +00001974 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +02001975
Mark Brownd91e8db22011-11-18 16:03:50 +00001976 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +02001977 if (ret != 0)
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001978 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001979
Mark Brownd91e8db22011-11-18 16:03:50 +00001980 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001981 tmp |= val & mask;
1982
Mark Brown018690d2011-11-29 20:10:36 +00001983 if (tmp != orig) {
Mark Brownd91e8db22011-11-18 16:03:50 +00001984 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +00001985 *change = true;
1986 } else {
1987 *change = false;
1988 }
Mark Brownb83a3132011-05-11 19:59:58 +02001989
Mark Brownb83a3132011-05-11 19:59:58 +02001990 return ret;
1991}
Mark Brown018690d2011-11-29 20:10:36 +00001992
1993/**
1994 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1995 *
1996 * @map: Register map to update
1997 * @reg: Register to update
1998 * @mask: Bitmask to change
1999 * @val: New value for bitmask
2000 *
2001 * Returns zero for success, a negative number on error.
2002 */
2003int regmap_update_bits(struct regmap *map, unsigned int reg,
2004 unsigned int mask, unsigned int val)
2005{
2006 bool change;
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002007 int ret;
2008
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002009 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002010 ret = _regmap_update_bits(map, reg, mask, val, &change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002011 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002012
2013 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00002014}
Mark Brownb83a3132011-05-11 19:59:58 +02002015EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +01002016
Mark Brown018690d2011-11-29 20:10:36 +00002017/**
Mark Brown915f4412013-10-09 13:30:10 +01002018 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2019 * map asynchronously
2020 *
2021 * @map: Register map to update
2022 * @reg: Register to update
2023 * @mask: Bitmask to change
2024 * @val: New value for bitmask
2025 *
2026 * With most buses the read must be done synchronously so this is most
2027 * useful for devices with a cache which do not need to interact with
2028 * the hardware to determine the current register value.
2029 *
2030 * Returns zero for success, a negative number on error.
2031 */
2032int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2033 unsigned int mask, unsigned int val)
2034{
2035 bool change;
2036 int ret;
2037
2038 map->lock(map->lock_arg);
2039
2040 map->async = true;
2041
2042 ret = _regmap_update_bits(map, reg, mask, val, &change);
2043
2044 map->async = false;
2045
2046 map->unlock(map->lock_arg);
2047
2048 return ret;
2049}
2050EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2051
2052/**
Mark Brown018690d2011-11-29 20:10:36 +00002053 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2054 * register map and report if updated
2055 *
2056 * @map: Register map to update
2057 * @reg: Register to update
2058 * @mask: Bitmask to change
2059 * @val: New value for bitmask
2060 * @change: Boolean indicating if a write was done
2061 *
2062 * Returns zero for success, a negative number on error.
2063 */
2064int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2065 unsigned int mask, unsigned int val,
2066 bool *change)
2067{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002068 int ret;
2069
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002070 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002071 ret = _regmap_update_bits(map, reg, mask, val, change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002072 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002073 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00002074}
2075EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2076
Mark Brown915f4412013-10-09 13:30:10 +01002077/**
2078 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2079 * register map asynchronously and report if
2080 * updated
2081 *
2082 * @map: Register map to update
2083 * @reg: Register to update
2084 * @mask: Bitmask to change
2085 * @val: New value for bitmask
2086 * @change: Boolean indicating if a write was done
2087 *
2088 * With most buses the read must be done synchronously so this is most
2089 * useful for devices with a cache which do not need to interact with
2090 * the hardware to determine the current register value.
2091 *
2092 * Returns zero for success, a negative number on error.
2093 */
2094int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2095 unsigned int mask, unsigned int val,
2096 bool *change)
2097{
2098 int ret;
2099
2100 map->lock(map->lock_arg);
2101
2102 map->async = true;
2103
2104 ret = _regmap_update_bits(map, reg, mask, val, change);
2105
2106 map->async = false;
2107
2108 map->unlock(map->lock_arg);
2109
2110 return ret;
2111}
2112EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2113
Mark Brown0d509f22013-01-27 22:07:38 +08002114void regmap_async_complete_cb(struct regmap_async *async, int ret)
2115{
2116 struct regmap *map = async->map;
2117 bool wake;
2118
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002119 trace_regmap_async_io_complete(map->dev);
2120
Mark Brown0d509f22013-01-27 22:07:38 +08002121 spin_lock(&map->async_lock);
Mark Brown7e09a972013-10-07 23:00:24 +01002122 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08002123 wake = list_empty(&map->async_list);
2124
2125 if (ret != 0)
2126 map->async_ret = ret;
2127
2128 spin_unlock(&map->async_lock);
2129
Mark Brown0d509f22013-01-27 22:07:38 +08002130 if (wake)
2131 wake_up(&map->async_waitq);
2132}
Axel Linf804fb52013-02-03 00:14:13 +08002133EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
Mark Brown0d509f22013-01-27 22:07:38 +08002134
2135static int regmap_async_is_done(struct regmap *map)
2136{
2137 unsigned long flags;
2138 int ret;
2139
2140 spin_lock_irqsave(&map->async_lock, flags);
2141 ret = list_empty(&map->async_list);
2142 spin_unlock_irqrestore(&map->async_lock, flags);
2143
2144 return ret;
2145}
2146
2147/**
2148 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2149 *
2150 * @map: Map to operate on.
2151 *
2152 * Blocks until any pending asynchronous I/O has completed. Returns
2153 * an error code for any failed I/O operations.
2154 */
2155int regmap_async_complete(struct regmap *map)
2156{
2157 unsigned long flags;
2158 int ret;
2159
2160 /* Nothing to do with no async support */
Daniel Mackf2e055e2013-07-04 13:11:03 +02002161 if (!map->bus || !map->bus->async_write)
Mark Brown0d509f22013-01-27 22:07:38 +08002162 return 0;
2163
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002164 trace_regmap_async_complete_start(map->dev);
2165
Mark Brown0d509f22013-01-27 22:07:38 +08002166 wait_event(map->async_waitq, regmap_async_is_done(map));
2167
2168 spin_lock_irqsave(&map->async_lock, flags);
2169 ret = map->async_ret;
2170 map->async_ret = 0;
2171 spin_unlock_irqrestore(&map->async_lock, flags);
2172
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002173 trace_regmap_async_complete_done(map->dev);
2174
Mark Brown0d509f22013-01-27 22:07:38 +08002175 return ret;
2176}
Mark Brownf88948e2013-02-05 13:53:26 +00002177EXPORT_SYMBOL_GPL(regmap_async_complete);
Mark Brown0d509f22013-01-27 22:07:38 +08002178
Mark Brown22f0d902012-01-21 12:01:14 +00002179/**
2180 * regmap_register_patch: Register and apply register updates to be applied
2181 * on device initialistion
2182 *
2183 * @map: Register map to apply updates to.
2184 * @regs: Values to update.
2185 * @num_regs: Number of entries in regs.
2186 *
2187 * Register a set of register updates to be applied to the device
2188 * whenever the device registers are synchronised with the cache and
2189 * apply them immediately. Typically this is used to apply
2190 * corrections to be applied to the device defaults on startup, such
2191 * as the updates some vendors provide to undocumented registers.
2192 */
2193int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2194 int num_regs)
2195{
Mark Brownaab13eb2013-07-11 12:41:44 +01002196 struct reg_default *p;
Mark Brown22f0d902012-01-21 12:01:14 +00002197 int i, ret;
2198 bool bypass;
2199
Cai Zhiyongbd60e382013-11-18 20:21:49 +08002200 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2201 num_regs))
2202 return 0;
2203
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002204 map->lock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00002205
2206 bypass = map->cache_bypass;
2207
2208 map->cache_bypass = true;
Mark Brown1a25f262013-10-10 20:55:03 +01002209 map->async = true;
Mark Brown22f0d902012-01-21 12:01:14 +00002210
2211 /* Write out first; it's useful to apply even if we fail later. */
2212 for (i = 0; i < num_regs; i++) {
2213 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2214 if (ret != 0) {
2215 dev_err(map->dev, "Failed to write %x = %x: %d\n",
2216 regs[i].reg, regs[i].def, ret);
2217 goto out;
2218 }
2219 }
2220
Mark Brownaab13eb2013-07-11 12:41:44 +01002221 p = krealloc(map->patch,
2222 sizeof(struct reg_default) * (map->patch_regs + num_regs),
2223 GFP_KERNEL);
2224 if (p) {
2225 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2226 map->patch = p;
2227 map->patch_regs += num_regs;
Mark Brown22f0d902012-01-21 12:01:14 +00002228 } else {
2229 ret = -ENOMEM;
2230 }
2231
2232out:
Mark Brown1a25f262013-10-10 20:55:03 +01002233 map->async = false;
Mark Brown22f0d902012-01-21 12:01:14 +00002234 map->cache_bypass = bypass;
2235
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002236 map->unlock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00002237
Mark Brown1a25f262013-10-10 20:55:03 +01002238 regmap_async_complete(map);
2239
Mark Brown22f0d902012-01-21 12:01:14 +00002240 return ret;
2241}
2242EXPORT_SYMBOL_GPL(regmap_register_patch);
2243
Mark Browneae4b512012-03-14 13:15:03 +00002244/*
Mark Browna6539c32012-02-17 14:20:14 -08002245 * regmap_get_val_bytes(): Report the size of a register value
2246 *
2247 * Report the size of a register value, mainly intended to for use by
2248 * generic infrastructure built on top of regmap.
2249 */
2250int regmap_get_val_bytes(struct regmap *map)
2251{
2252 if (map->format.format_write)
2253 return -EINVAL;
2254
2255 return map->format.val_bytes;
2256}
2257EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2258
Mark Brown31244e32011-07-20 22:56:53 +01002259static int __init regmap_initcall(void)
2260{
2261 regmap_debugfs_initcall();
2262
2263 return 0;
2264}
2265postcore_initcall(regmap_initcall);