blob: 96f7e8523ee8498ea63673f074cfd0389a2c440d [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>
Mark Brownb83a3132011-05-11 19:59:58 +020019
Mark Brownfb2736b2011-07-24 21:30:55 +010020#define CREATE_TRACE_POINTS
21#include <trace/events/regmap.h>
22
Mark Brown93de9122011-07-20 22:35:37 +010023#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020024
Mark Brown1044c182012-07-06 14:10:23 +010025/*
26 * Sometimes for failures during very early init the trace
27 * infrastructure isn't available early enough to be used. For this
28 * sort of problem defining LOG_DEVICE will add printks for basic
29 * register I/O on a specific device.
30 */
31#undef LOG_DEVICE
32
33static int _regmap_update_bits(struct regmap *map, unsigned int reg,
34 unsigned int mask, unsigned int val,
35 bool *change);
36
Davide Ciminaghi76aad392012-11-20 15:20:30 +010037bool regmap_reg_in_ranges(unsigned int reg,
38 const struct regmap_range *ranges,
39 unsigned int nranges)
40{
41 const struct regmap_range *r;
42 int i;
43
44 for (i = 0, r = ranges; i < nranges; i++, r++)
45 if (regmap_reg_in_range(reg, r))
46 return true;
47 return false;
48}
49EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
50
51static bool _regmap_check_range_table(struct regmap *map,
52 unsigned int reg,
53 const struct regmap_access_table *table)
54{
55 /* Check "no ranges" first */
56 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
57 return false;
58
59 /* In case zero "yes ranges" are supplied, any reg is OK */
60 if (!table->n_yes_ranges)
61 return true;
62
63 return regmap_reg_in_ranges(reg, table->yes_ranges,
64 table->n_yes_ranges);
65}
66
Mark Brown8de2f082011-08-10 17:14:41 +090067bool regmap_writeable(struct regmap *map, unsigned int reg)
68{
69 if (map->max_register && reg > map->max_register)
70 return false;
71
72 if (map->writeable_reg)
73 return map->writeable_reg(map->dev, reg);
74
Davide Ciminaghi76aad392012-11-20 15:20:30 +010075 if (map->wr_table)
76 return _regmap_check_range_table(map, reg, map->wr_table);
77
Mark Brown8de2f082011-08-10 17:14:41 +090078 return true;
79}
80
81bool regmap_readable(struct regmap *map, unsigned int reg)
82{
83 if (map->max_register && reg > map->max_register)
84 return false;
85
Wolfram Sang4191f192012-01-30 15:08:16 +010086 if (map->format.format_write)
87 return false;
88
Mark Brown8de2f082011-08-10 17:14:41 +090089 if (map->readable_reg)
90 return map->readable_reg(map->dev, reg);
91
Davide Ciminaghi76aad392012-11-20 15:20:30 +010092 if (map->rd_table)
93 return _regmap_check_range_table(map, reg, map->rd_table);
94
Mark Brown8de2f082011-08-10 17:14:41 +090095 return true;
96}
97
98bool regmap_volatile(struct regmap *map, unsigned int reg)
99{
Wolfram Sang4191f192012-01-30 15:08:16 +0100100 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +0900101 return false;
102
103 if (map->volatile_reg)
104 return map->volatile_reg(map->dev, reg);
105
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100106 if (map->volatile_table)
107 return _regmap_check_range_table(map, reg, map->volatile_table);
108
Mark Brown8de2f082011-08-10 17:14:41 +0900109 return true;
110}
111
112bool regmap_precious(struct regmap *map, unsigned int reg)
113{
Wolfram Sang4191f192012-01-30 15:08:16 +0100114 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +0900115 return false;
116
117 if (map->precious_reg)
118 return map->precious_reg(map->dev, reg);
119
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100120 if (map->precious_table)
121 return _regmap_check_range_table(map, reg, map->precious_table);
122
Mark Brown8de2f082011-08-10 17:14:41 +0900123 return false;
124}
125
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100126static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
127 unsigned int num)
128{
129 unsigned int i;
130
131 for (i = 0; i < num; i++)
132 if (!regmap_volatile(map, reg + i))
133 return false;
134
135 return true;
136}
137
Wolfram Sang9aa50752012-01-27 16:10:22 +0100138static void regmap_format_2_6_write(struct regmap *map,
139 unsigned int reg, unsigned int val)
140{
141 u8 *out = map->work_buf;
142
143 *out = (reg << 6) | val;
144}
145
Mark Brownb83a3132011-05-11 19:59:58 +0200146static void regmap_format_4_12_write(struct regmap *map,
147 unsigned int reg, unsigned int val)
148{
149 __be16 *out = map->work_buf;
150 *out = cpu_to_be16((reg << 12) | val);
151}
152
153static void regmap_format_7_9_write(struct regmap *map,
154 unsigned int reg, unsigned int val)
155{
156 __be16 *out = map->work_buf;
157 *out = cpu_to_be16((reg << 9) | val);
158}
159
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100160static void regmap_format_10_14_write(struct regmap *map,
161 unsigned int reg, unsigned int val)
162{
163 u8 *out = map->work_buf;
164
165 out[2] = val;
166 out[1] = (val >> 8) | (reg << 6);
167 out[0] = reg >> 2;
168}
169
Marc Reillyd939fb92012-03-16 12:11:43 +1100170static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200171{
172 u8 *b = buf;
173
Marc Reillyd939fb92012-03-16 12:11:43 +1100174 b[0] = val << shift;
Mark Brownb83a3132011-05-11 19:59:58 +0200175}
176
Stephen Warren141eba22012-05-24 10:47:26 -0600177static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200178{
179 __be16 *b = buf;
180
Marc Reillyd939fb92012-03-16 12:11:43 +1100181 b[0] = cpu_to_be16(val << shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200182}
183
Stephen Warren141eba22012-05-24 10:47:26 -0600184static void regmap_format_16_native(void *buf, unsigned int val,
185 unsigned int shift)
186{
187 *(u16 *)buf = val << shift;
188}
189
Marc Reillyd939fb92012-03-16 12:11:43 +1100190static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
Marc Reillyea279fc2012-03-16 12:11:42 +1100191{
192 u8 *b = buf;
193
Marc Reillyd939fb92012-03-16 12:11:43 +1100194 val <<= shift;
195
Marc Reillyea279fc2012-03-16 12:11:42 +1100196 b[0] = val >> 16;
197 b[1] = val >> 8;
198 b[2] = val;
199}
200
Stephen Warren141eba22012-05-24 10:47:26 -0600201static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
Mark Brown7d5e5252012-02-17 15:58:25 -0800202{
203 __be32 *b = buf;
204
Marc Reillyd939fb92012-03-16 12:11:43 +1100205 b[0] = cpu_to_be32(val << shift);
Mark Brown7d5e5252012-02-17 15:58:25 -0800206}
207
Stephen Warren141eba22012-05-24 10:47:26 -0600208static void regmap_format_32_native(void *buf, unsigned int val,
209 unsigned int shift)
210{
211 *(u32 *)buf = val << shift;
212}
213
Mark Brownb83a3132011-05-11 19:59:58 +0200214static unsigned int regmap_parse_8(void *buf)
215{
216 u8 *b = buf;
217
218 return b[0];
219}
220
Stephen Warren141eba22012-05-24 10:47:26 -0600221static unsigned int regmap_parse_16_be(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200222{
223 __be16 *b = buf;
224
225 b[0] = be16_to_cpu(b[0]);
226
227 return b[0];
228}
229
Stephen Warren141eba22012-05-24 10:47:26 -0600230static unsigned int regmap_parse_16_native(void *buf)
231{
232 return *(u16 *)buf;
233}
234
Marc Reillyea279fc2012-03-16 12:11:42 +1100235static unsigned int regmap_parse_24(void *buf)
236{
237 u8 *b = buf;
238 unsigned int ret = b[2];
239 ret |= ((unsigned int)b[1]) << 8;
240 ret |= ((unsigned int)b[0]) << 16;
241
242 return ret;
243}
244
Stephen Warren141eba22012-05-24 10:47:26 -0600245static unsigned int regmap_parse_32_be(void *buf)
Mark Brown7d5e5252012-02-17 15:58:25 -0800246{
247 __be32 *b = buf;
248
249 b[0] = be32_to_cpu(b[0]);
250
251 return b[0];
252}
253
Stephen Warren141eba22012-05-24 10:47:26 -0600254static unsigned int regmap_parse_32_native(void *buf)
255{
256 return *(u32 *)buf;
257}
258
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200259static void regmap_lock_mutex(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600260{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200261 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600262 mutex_lock(&map->mutex);
263}
264
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200265static void regmap_unlock_mutex(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600266{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200267 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600268 mutex_unlock(&map->mutex);
269}
270
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200271static void regmap_lock_spinlock(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600272{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200273 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600274 spin_lock(&map->spinlock);
275}
276
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200277static void regmap_unlock_spinlock(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600278{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200279 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600280 spin_unlock(&map->spinlock);
281}
282
Mark Brown72b39f62012-05-08 17:44:40 +0100283static void dev_get_regmap_release(struct device *dev, void *res)
284{
285 /*
286 * We don't actually have anything to do here; the goal here
287 * is not to manage the regmap but to provide a simple way to
288 * get the regmap back given a struct device.
289 */
290}
291
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100292static bool _regmap_range_add(struct regmap *map,
293 struct regmap_range_node *data)
294{
295 struct rb_root *root = &map->range_tree;
296 struct rb_node **new = &(root->rb_node), *parent = NULL;
297
298 while (*new) {
299 struct regmap_range_node *this =
300 container_of(*new, struct regmap_range_node, node);
301
302 parent = *new;
303 if (data->range_max < this->range_min)
304 new = &((*new)->rb_left);
305 else if (data->range_min > this->range_max)
306 new = &((*new)->rb_right);
307 else
308 return false;
309 }
310
311 rb_link_node(&data->node, parent, new);
312 rb_insert_color(&data->node, root);
313
314 return true;
315}
316
317static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
318 unsigned int reg)
319{
320 struct rb_node *node = map->range_tree.rb_node;
321
322 while (node) {
323 struct regmap_range_node *this =
324 container_of(node, struct regmap_range_node, node);
325
326 if (reg < this->range_min)
327 node = node->rb_left;
328 else if (reg > this->range_max)
329 node = node->rb_right;
330 else
331 return this;
332 }
333
334 return NULL;
335}
336
337static void regmap_range_exit(struct regmap *map)
338{
339 struct rb_node *next;
340 struct regmap_range_node *range_node;
341
342 next = rb_first(&map->range_tree);
343 while (next) {
344 range_node = rb_entry(next, struct regmap_range_node, node);
345 next = rb_next(&range_node->node);
346 rb_erase(&range_node->node, &map->range_tree);
347 kfree(range_node);
348 }
349
350 kfree(map->selector_work_buf);
351}
352
Mark Brownb83a3132011-05-11 19:59:58 +0200353/**
354 * regmap_init(): Initialise register map
355 *
356 * @dev: Device that will be interacted with
357 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600358 * @bus_context: Data passed to bus-specific callbacks
Mark Brownb83a3132011-05-11 19:59:58 +0200359 * @config: Configuration for register map
360 *
361 * The return value will be an ERR_PTR() on error or a valid pointer to
362 * a struct regmap. This function should generally not be called
363 * directly, it should be called by bus-specific init functions.
364 */
365struct regmap *regmap_init(struct device *dev,
366 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600367 void *bus_context,
Mark Brownb83a3132011-05-11 19:59:58 +0200368 const struct regmap_config *config)
369{
Mark Brown72b39f62012-05-08 17:44:40 +0100370 struct regmap *map, **m;
Mark Brownb83a3132011-05-11 19:59:58 +0200371 int ret = -EINVAL;
Stephen Warren141eba22012-05-24 10:47:26 -0600372 enum regmap_endian reg_endian, val_endian;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100373 int i, j;
Mark Brownb83a3132011-05-11 19:59:58 +0200374
375 if (!bus || !config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100376 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200377
378 map = kzalloc(sizeof(*map), GFP_KERNEL);
379 if (map == NULL) {
380 ret = -ENOMEM;
381 goto err;
382 }
383
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200384 if (config->lock && config->unlock) {
385 map->lock = config->lock;
386 map->unlock = config->unlock;
387 map->lock_arg = config->lock_arg;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600388 } else {
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200389 if (bus->fast_io) {
390 spin_lock_init(&map->spinlock);
391 map->lock = regmap_lock_spinlock;
392 map->unlock = regmap_unlock_spinlock;
393 } else {
394 mutex_init(&map->mutex);
395 map->lock = regmap_lock_mutex;
396 map->unlock = regmap_unlock_mutex;
397 }
398 map->lock_arg = map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600399 }
Wolfram Sangc212acc2012-01-28 02:16:41 +0100400 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000401 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100402 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Fabio Estevam5494a982012-05-31 21:10:30 -0300403 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
404 config->val_bits + config->pad_bits, 8);
Marc Reillyd939fb92012-03-16 12:11:43 +1100405 map->reg_shift = config->pad_bits % 8;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600406 if (config->reg_stride)
407 map->reg_stride = config->reg_stride;
408 else
409 map->reg_stride = 1;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100410 map->use_single_rw = config->use_single_rw;
Mark Brownb83a3132011-05-11 19:59:58 +0200411 map->dev = dev;
412 map->bus = bus;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600413 map->bus_context = bus_context;
Mark Brown2e2ae662011-07-20 22:33:39 +0100414 map->max_register = config->max_register;
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100415 map->wr_table = config->wr_table;
416 map->rd_table = config->rd_table;
417 map->volatile_table = config->volatile_table;
418 map->precious_table = config->precious_table;
Mark Brown2e2ae662011-07-20 22:33:39 +0100419 map->writeable_reg = config->writeable_reg;
420 map->readable_reg = config->readable_reg;
421 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900422 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100423 map->cache_type = config->cache_type;
Mark Brown72b39f62012-05-08 17:44:40 +0100424 map->name = config->name;
Mark Brownb83a3132011-05-11 19:59:58 +0200425
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200426 if (config->read_flag_mask || config->write_flag_mask) {
427 map->read_flag_mask = config->read_flag_mask;
428 map->write_flag_mask = config->write_flag_mask;
429 } else {
430 map->read_flag_mask = bus->read_flag_mask;
431 }
432
Stephen Warren141eba22012-05-24 10:47:26 -0600433 reg_endian = config->reg_format_endian;
434 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
435 reg_endian = bus->reg_format_endian_default;
436 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
437 reg_endian = REGMAP_ENDIAN_BIG;
438
439 val_endian = config->val_format_endian;
440 if (val_endian == REGMAP_ENDIAN_DEFAULT)
441 val_endian = bus->val_format_endian_default;
442 if (val_endian == REGMAP_ENDIAN_DEFAULT)
443 val_endian = REGMAP_ENDIAN_BIG;
444
Marc Reillyd939fb92012-03-16 12:11:43 +1100445 switch (config->reg_bits + map->reg_shift) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100446 case 2:
447 switch (config->val_bits) {
448 case 6:
449 map->format.format_write = regmap_format_2_6_write;
450 break;
451 default:
452 goto err_map;
453 }
454 break;
455
Mark Brownb83a3132011-05-11 19:59:58 +0200456 case 4:
457 switch (config->val_bits) {
458 case 12:
459 map->format.format_write = regmap_format_4_12_write;
460 break;
461 default:
462 goto err_map;
463 }
464 break;
465
466 case 7:
467 switch (config->val_bits) {
468 case 9:
469 map->format.format_write = regmap_format_7_9_write;
470 break;
471 default:
472 goto err_map;
473 }
474 break;
475
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100476 case 10:
477 switch (config->val_bits) {
478 case 14:
479 map->format.format_write = regmap_format_10_14_write;
480 break;
481 default:
482 goto err_map;
483 }
484 break;
485
Mark Brownb83a3132011-05-11 19:59:58 +0200486 case 8:
487 map->format.format_reg = regmap_format_8;
488 break;
489
490 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600491 switch (reg_endian) {
492 case REGMAP_ENDIAN_BIG:
493 map->format.format_reg = regmap_format_16_be;
494 break;
495 case REGMAP_ENDIAN_NATIVE:
496 map->format.format_reg = regmap_format_16_native;
497 break;
498 default:
499 goto err_map;
500 }
Mark Brownb83a3132011-05-11 19:59:58 +0200501 break;
502
Mark Brown7d5e5252012-02-17 15:58:25 -0800503 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600504 switch (reg_endian) {
505 case REGMAP_ENDIAN_BIG:
506 map->format.format_reg = regmap_format_32_be;
507 break;
508 case REGMAP_ENDIAN_NATIVE:
509 map->format.format_reg = regmap_format_32_native;
510 break;
511 default:
512 goto err_map;
513 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800514 break;
515
Mark Brownb83a3132011-05-11 19:59:58 +0200516 default:
517 goto err_map;
518 }
519
520 switch (config->val_bits) {
521 case 8:
522 map->format.format_val = regmap_format_8;
523 map->format.parse_val = regmap_parse_8;
524 break;
525 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600526 switch (val_endian) {
527 case REGMAP_ENDIAN_BIG:
528 map->format.format_val = regmap_format_16_be;
529 map->format.parse_val = regmap_parse_16_be;
530 break;
531 case REGMAP_ENDIAN_NATIVE:
532 map->format.format_val = regmap_format_16_native;
533 map->format.parse_val = regmap_parse_16_native;
534 break;
535 default:
536 goto err_map;
537 }
Mark Brownb83a3132011-05-11 19:59:58 +0200538 break;
Marc Reillyea279fc2012-03-16 12:11:42 +1100539 case 24:
Stephen Warren141eba22012-05-24 10:47:26 -0600540 if (val_endian != REGMAP_ENDIAN_BIG)
541 goto err_map;
Marc Reillyea279fc2012-03-16 12:11:42 +1100542 map->format.format_val = regmap_format_24;
543 map->format.parse_val = regmap_parse_24;
544 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800545 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600546 switch (val_endian) {
547 case REGMAP_ENDIAN_BIG:
548 map->format.format_val = regmap_format_32_be;
549 map->format.parse_val = regmap_parse_32_be;
550 break;
551 case REGMAP_ENDIAN_NATIVE:
552 map->format.format_val = regmap_format_32_native;
553 map->format.parse_val = regmap_parse_32_native;
554 break;
555 default:
556 goto err_map;
557 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800558 break;
Mark Brownb83a3132011-05-11 19:59:58 +0200559 }
560
Stephen Warren141eba22012-05-24 10:47:26 -0600561 if (map->format.format_write) {
562 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
563 (val_endian != REGMAP_ENDIAN_BIG))
564 goto err_map;
Mark Brown7a647612012-04-30 23:26:32 +0100565 map->use_single_rw = true;
Stephen Warren141eba22012-05-24 10:47:26 -0600566 }
Mark Brown7a647612012-04-30 23:26:32 +0100567
Mark Brownb83a3132011-05-11 19:59:58 +0200568 if (!map->format.format_write &&
569 !(map->format.format_reg && map->format.format_val))
570 goto err_map;
571
Mark Brown82159ba2012-01-18 10:52:25 +0000572 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200573 if (map->work_buf == NULL) {
574 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700575 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200576 }
577
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100578 map->range_tree = RB_ROOT;
Mark Browne3549cd2012-10-02 20:17:15 +0100579 for (i = 0; i < config->num_ranges; i++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100580 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
581 struct regmap_range_node *new;
582
583 /* Sanity check */
Mark Brown061adc02012-10-03 12:17:51 +0100584 if (range_cfg->range_max < range_cfg->range_min) {
585 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
586 range_cfg->range_max, range_cfg->range_min);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100587 goto err_range;
Mark Brown061adc02012-10-03 12:17:51 +0100588 }
589
590 if (range_cfg->range_max > map->max_register) {
591 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
592 range_cfg->range_max, map->max_register);
593 goto err_range;
594 }
595
596 if (range_cfg->selector_reg > map->max_register) {
597 dev_err(map->dev,
598 "Invalid range %d: selector out of map\n", i);
599 goto err_range;
600 }
601
602 if (range_cfg->window_len == 0) {
603 dev_err(map->dev, "Invalid range %d: window_len 0\n",
604 i);
605 goto err_range;
606 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100607
608 /* Make sure, that this register range has no selector
609 or data window within its boundary */
Mark Browne3549cd2012-10-02 20:17:15 +0100610 for (j = 0; j < config->num_ranges; j++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100611 unsigned sel_reg = config->ranges[j].selector_reg;
612 unsigned win_min = config->ranges[j].window_start;
613 unsigned win_max = win_min +
614 config->ranges[j].window_len - 1;
615
616 if (range_cfg->range_min <= sel_reg &&
617 sel_reg <= range_cfg->range_max) {
Mark Brown061adc02012-10-03 12:17:51 +0100618 dev_err(map->dev,
619 "Range %d: selector for %d in window\n",
620 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100621 goto err_range;
622 }
623
624 if (!(win_max < range_cfg->range_min ||
625 win_min > range_cfg->range_max)) {
Mark Brown061adc02012-10-03 12:17:51 +0100626 dev_err(map->dev,
627 "Range %d: window for %d in window\n",
628 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100629 goto err_range;
630 }
631 }
632
633 new = kzalloc(sizeof(*new), GFP_KERNEL);
634 if (new == NULL) {
635 ret = -ENOMEM;
636 goto err_range;
637 }
638
Mark Brown4b020b32012-10-03 13:13:16 +0100639 new->map = map;
Mark Brownd058bb42012-10-03 12:40:47 +0100640 new->name = range_cfg->name;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100641 new->range_min = range_cfg->range_min;
642 new->range_max = range_cfg->range_max;
643 new->selector_reg = range_cfg->selector_reg;
644 new->selector_mask = range_cfg->selector_mask;
645 new->selector_shift = range_cfg->selector_shift;
646 new->window_start = range_cfg->window_start;
647 new->window_len = range_cfg->window_len;
648
649 if (_regmap_range_add(map, new) == false) {
Mark Brown061adc02012-10-03 12:17:51 +0100650 dev_err(map->dev, "Failed to add range %d\n", i);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100651 kfree(new);
652 goto err_range;
653 }
654
655 if (map->selector_work_buf == NULL) {
656 map->selector_work_buf =
657 kzalloc(map->format.buf_size, GFP_KERNEL);
658 if (map->selector_work_buf == NULL) {
659 ret = -ENOMEM;
660 goto err_range;
661 }
662 }
663 }
Mark Brown052d2cd2011-11-21 19:05:13 +0000664
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100665 ret = regcache_init(map, config);
Mark Brown0ff3e622012-10-04 17:39:13 +0100666 if (ret != 0)
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100667 goto err_range;
668
669 regmap_debugfs_init(map, config->name);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100670
Mark Brown72b39f62012-05-08 17:44:40 +0100671 /* Add a devres resource for dev_get_regmap() */
672 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
673 if (!m) {
674 ret = -ENOMEM;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100675 goto err_debugfs;
Mark Brown72b39f62012-05-08 17:44:40 +0100676 }
677 *m = map;
678 devres_add(dev, m);
679
Mark Brownb83a3132011-05-11 19:59:58 +0200680 return map;
681
Stephen Warrenbfaa25f2012-05-23 16:30:53 -0600682err_debugfs:
683 regmap_debugfs_exit(map);
Mark Brown72b39f62012-05-08 17:44:40 +0100684 regcache_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100685err_range:
686 regmap_range_exit(map);
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100687 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200688err_map:
689 kfree(map);
690err:
691 return ERR_PTR(ret);
692}
693EXPORT_SYMBOL_GPL(regmap_init);
694
Mark Brownc0eb4672012-01-30 19:56:52 +0000695static void devm_regmap_release(struct device *dev, void *res)
696{
697 regmap_exit(*(struct regmap **)res);
698}
699
700/**
701 * devm_regmap_init(): Initialise managed register map
702 *
703 * @dev: Device that will be interacted with
704 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600705 * @bus_context: Data passed to bus-specific callbacks
Mark Brownc0eb4672012-01-30 19:56:52 +0000706 * @config: Configuration for register map
707 *
708 * The return value will be an ERR_PTR() on error or a valid pointer
709 * to a struct regmap. This function should generally not be called
710 * directly, it should be called by bus-specific init functions. The
711 * map will be automatically freed by the device management code.
712 */
713struct regmap *devm_regmap_init(struct device *dev,
714 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600715 void *bus_context,
Mark Brownc0eb4672012-01-30 19:56:52 +0000716 const struct regmap_config *config)
717{
718 struct regmap **ptr, *regmap;
719
720 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
721 if (!ptr)
722 return ERR_PTR(-ENOMEM);
723
Stephen Warren0135bbc2012-04-04 15:48:30 -0600724 regmap = regmap_init(dev, bus, bus_context, config);
Mark Brownc0eb4672012-01-30 19:56:52 +0000725 if (!IS_ERR(regmap)) {
726 *ptr = regmap;
727 devres_add(dev, ptr);
728 } else {
729 devres_free(ptr);
730 }
731
732 return regmap;
733}
734EXPORT_SYMBOL_GPL(devm_regmap_init);
735
Mark Brownb83a3132011-05-11 19:59:58 +0200736/**
Mark Brownbf315172011-12-03 17:06:20 +0000737 * regmap_reinit_cache(): Reinitialise the current register cache
738 *
739 * @map: Register map to operate on.
740 * @config: New configuration. Only the cache data will be used.
741 *
742 * Discard any existing register cache for the map and initialize a
743 * new cache. This can be used to restore the cache to defaults or to
744 * update the cache configuration to reflect runtime discovery of the
745 * hardware.
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100746 *
747 * No explicit locking is done here, the user needs to ensure that
748 * this function will not race with other calls to regmap.
Mark Brownbf315172011-12-03 17:06:20 +0000749 */
750int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
751{
Mark Brownbf315172011-12-03 17:06:20 +0000752 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000753 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000754
755 map->max_register = config->max_register;
756 map->writeable_reg = config->writeable_reg;
757 map->readable_reg = config->readable_reg;
758 map->volatile_reg = config->volatile_reg;
759 map->precious_reg = config->precious_reg;
760 map->cache_type = config->cache_type;
761
Stephen Warrend3c242e2012-04-04 15:48:29 -0600762 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +0000763
Mark Brown421e8d22012-01-20 13:39:37 +0000764 map->cache_bypass = false;
765 map->cache_only = false;
766
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100767 return regcache_init(map, config);
Mark Brownbf315172011-12-03 17:06:20 +0000768}
Mark Brown752a6a52012-05-14 10:00:12 +0100769EXPORT_SYMBOL_GPL(regmap_reinit_cache);
Mark Brownbf315172011-12-03 17:06:20 +0000770
771/**
Mark Brownb83a3132011-05-11 19:59:58 +0200772 * regmap_exit(): Free a previously allocated register map
773 */
774void regmap_exit(struct regmap *map)
775{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100776 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100777 regmap_debugfs_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100778 regmap_range_exit(map);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600779 if (map->bus->free_context)
780 map->bus->free_context(map->bus_context);
Mark Brownb83a3132011-05-11 19:59:58 +0200781 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200782 kfree(map);
783}
784EXPORT_SYMBOL_GPL(regmap_exit);
785
Mark Brown72b39f62012-05-08 17:44:40 +0100786static int dev_get_regmap_match(struct device *dev, void *res, void *data)
787{
788 struct regmap **r = res;
789 if (!r || !*r) {
790 WARN_ON(!r || !*r);
791 return 0;
792 }
793
794 /* If the user didn't specify a name match any */
795 if (data)
796 return (*r)->name == data;
797 else
798 return 1;
799}
800
801/**
802 * dev_get_regmap(): Obtain the regmap (if any) for a device
803 *
804 * @dev: Device to retrieve the map for
805 * @name: Optional name for the register map, usually NULL.
806 *
807 * Returns the regmap for the device if one is present, or NULL. If
808 * name is specified then it must match the name specified when
809 * registering the device, if it is NULL then the first regmap found
810 * will be used. Devices with multiple register maps are very rare,
811 * generic code should normally not need to specify a name.
812 */
813struct regmap *dev_get_regmap(struct device *dev, const char *name)
814{
815 struct regmap **r = devres_find(dev, dev_get_regmap_release,
816 dev_get_regmap_match, (void *)name);
817
818 if (!r)
819 return NULL;
820 return *r;
821}
822EXPORT_SYMBOL_GPL(dev_get_regmap);
823
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100824static int _regmap_select_page(struct regmap *map, unsigned int *reg,
Mark Brown98bc7df2012-10-04 17:31:11 +0100825 struct regmap_range_node *range,
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100826 unsigned int val_num)
827{
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100828 void *orig_work_buf;
829 unsigned int win_offset;
830 unsigned int win_page;
831 bool page_chg;
832 int ret;
833
Mark Brown98bc7df2012-10-04 17:31:11 +0100834 win_offset = (*reg - range->range_min) % range->window_len;
835 win_page = (*reg - range->range_min) / range->window_len;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100836
Mark Brown98bc7df2012-10-04 17:31:11 +0100837 if (val_num > 1) {
838 /* Bulk write shouldn't cross range boundary */
839 if (*reg + val_num - 1 > range->range_max)
840 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100841
Mark Brown98bc7df2012-10-04 17:31:11 +0100842 /* ... or single page boundary */
843 if (val_num > range->window_len - win_offset)
844 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100845 }
846
Mark Brown98bc7df2012-10-04 17:31:11 +0100847 /* It is possible to have selector register inside data window.
848 In that case, selector register is located on every page and
849 it needs no page switching, when accessed alone. */
850 if (val_num > 1 ||
851 range->window_start + win_offset != range->selector_reg) {
852 /* Use separate work_buf during page switching */
853 orig_work_buf = map->work_buf;
854 map->work_buf = map->selector_work_buf;
855
856 ret = _regmap_update_bits(map, range->selector_reg,
857 range->selector_mask,
858 win_page << range->selector_shift,
859 &page_chg);
860
861 map->work_buf = orig_work_buf;
862
Mark Brown0ff3e622012-10-04 17:39:13 +0100863 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +0100864 return ret;
865 }
866
867 *reg = range->window_start + win_offset;
868
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100869 return 0;
870}
871
Mark Brownb83a3132011-05-11 19:59:58 +0200872static int _regmap_raw_write(struct regmap *map, unsigned int reg,
873 const void *val, size_t val_len)
874{
Mark Brown98bc7df2012-10-04 17:31:11 +0100875 struct regmap_range_node *range;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200876 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200877 void *buf;
878 int ret = -ENOTSUPP;
879 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100880 int i;
881
882 /* Check for unwritable registers before we start */
883 if (map->writeable_reg)
884 for (i = 0; i < val_len / map->format.val_bytes; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -0600885 if (!map->writeable_reg(map->dev,
886 reg + (i * map->reg_stride)))
Mark Brown73304782011-07-24 11:46:20 +0100887 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200888
Laxman Dewanganc9157192012-02-10 21:30:27 +0530889 if (!map->cache_bypass && map->format.parse_val) {
890 unsigned int ival;
891 int val_bytes = map->format.val_bytes;
892 for (i = 0; i < val_len / val_bytes; i++) {
893 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
894 ival = map->format.parse_val(map->work_buf);
Stephen Warrenf01ee602012-04-09 13:40:24 -0600895 ret = regcache_write(map, reg + (i * map->reg_stride),
896 ival);
Laxman Dewanganc9157192012-02-10 21:30:27 +0530897 if (ret) {
898 dev_err(map->dev,
Mark Brown6d04b8a2012-10-26 19:05:32 +0100899 "Error in caching of register: %x ret: %d\n",
Laxman Dewanganc9157192012-02-10 21:30:27 +0530900 reg + i, ret);
901 return ret;
902 }
903 }
904 if (map->cache_only) {
905 map->cache_dirty = true;
906 return 0;
907 }
908 }
909
Mark Brown98bc7df2012-10-04 17:31:11 +0100910 range = _regmap_range_lookup(map, reg);
911 if (range) {
Mark Brown8a2ceac2012-10-04 18:20:18 +0100912 int val_num = val_len / map->format.val_bytes;
913 int win_offset = (reg - range->range_min) % range->window_len;
914 int win_residue = range->window_len - win_offset;
915
916 /* If the write goes beyond the end of the window split it */
917 while (val_num > win_residue) {
Fabio Estevam1a61cfe2012-10-25 14:07:18 -0200918 dev_dbg(map->dev, "Writing window %d/%zu\n",
Mark Brown8a2ceac2012-10-04 18:20:18 +0100919 win_residue, val_len / map->format.val_bytes);
920 ret = _regmap_raw_write(map, reg, val, win_residue *
921 map->format.val_bytes);
922 if (ret != 0)
923 return ret;
924
925 reg += win_residue;
926 val_num -= win_residue;
927 val += win_residue * map->format.val_bytes;
928 val_len -= win_residue * map->format.val_bytes;
929
930 win_offset = (reg - range->range_min) %
931 range->window_len;
932 win_residue = range->window_len - win_offset;
933 }
934
935 ret = _regmap_select_page(map, &reg, range, val_num);
Mark Brown0ff3e622012-10-04 17:39:13 +0100936 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +0100937 return ret;
938 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100939
Marc Reillyd939fb92012-03-16 12:11:43 +1100940 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200941
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200942 u8[0] |= map->write_flag_mask;
943
Mark Brownfb2736b2011-07-24 21:30:55 +0100944 trace_regmap_hw_write_start(map->dev, reg,
945 val_len / map->format.val_bytes);
946
Mark Brown2547e202011-07-20 21:47:22 +0100947 /* If we're doing a single register write we can probably just
948 * send the work_buf directly, otherwise try to do a gather
949 * write.
950 */
Mark Brown82159ba2012-01-18 10:52:25 +0000951 if (val == (map->work_buf + map->format.pad_bytes +
952 map->format.reg_bytes))
Stephen Warren0135bbc2012-04-04 15:48:30 -0600953 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000954 map->format.reg_bytes +
955 map->format.pad_bytes +
956 val_len);
Mark Brown2547e202011-07-20 21:47:22 +0100957 else if (map->bus->gather_write)
Stephen Warren0135bbc2012-04-04 15:48:30 -0600958 ret = map->bus->gather_write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000959 map->format.reg_bytes +
960 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200961 val, val_len);
962
Mark Brown2547e202011-07-20 21:47:22 +0100963 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200964 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +0000965 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
966 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200967 if (!buf)
968 return -ENOMEM;
969
970 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +0000971 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
972 val, val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600973 ret = map->bus->write(map->bus_context, buf, len);
Mark Brownb83a3132011-05-11 19:59:58 +0200974
975 kfree(buf);
976 }
977
Mark Brownfb2736b2011-07-24 21:30:55 +0100978 trace_regmap_hw_write_done(map->dev, reg,
979 val_len / map->format.val_bytes);
980
Mark Brownb83a3132011-05-11 19:59:58 +0200981 return ret;
982}
983
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100984int _regmap_write(struct regmap *map, unsigned int reg,
985 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200986{
Mark Brown98bc7df2012-10-04 17:31:11 +0100987 struct regmap_range_node *range;
Mark Brownfb2736b2011-07-24 21:30:55 +0100988 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200989 BUG_ON(!map->format.format_write && !map->format.format_val);
990
Laxman Dewanganc9157192012-02-10 21:30:27 +0530991 if (!map->cache_bypass && map->format.format_write) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100992 ret = regcache_write(map, reg, val);
993 if (ret != 0)
994 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200995 if (map->cache_only) {
996 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100997 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200998 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100999 }
1000
Mark Brown1044c182012-07-06 14:10:23 +01001001#ifdef LOG_DEVICE
1002 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1003 dev_info(map->dev, "%x <= %x\n", reg, val);
1004#endif
1005
Mark Brownfb2736b2011-07-24 21:30:55 +01001006 trace_regmap_reg_write(map->dev, reg, val);
1007
Mark Brownb83a3132011-05-11 19:59:58 +02001008 if (map->format.format_write) {
Mark Brown98bc7df2012-10-04 17:31:11 +01001009 range = _regmap_range_lookup(map, reg);
1010 if (range) {
1011 ret = _regmap_select_page(map, &reg, range, 1);
Mark Brown0ff3e622012-10-04 17:39:13 +01001012 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001013 return ret;
1014 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001015
Mark Brownb83a3132011-05-11 19:59:58 +02001016 map->format.format_write(map, reg, val);
1017
Mark Brownfb2736b2011-07-24 21:30:55 +01001018 trace_regmap_hw_write_start(map->dev, reg, 1);
1019
Stephen Warren0135bbc2012-04-04 15:48:30 -06001020 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brownfb2736b2011-07-24 21:30:55 +01001021 map->format.buf_size);
1022
1023 trace_regmap_hw_write_done(map->dev, reg, 1);
1024
1025 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001026 } else {
Mark Brown82159ba2012-01-18 10:52:25 +00001027 map->format.format_val(map->work_buf + map->format.reg_bytes
Marc Reillyd939fb92012-03-16 12:11:43 +11001028 + map->format.pad_bytes, val, 0);
Mark Brownb83a3132011-05-11 19:59:58 +02001029 return _regmap_raw_write(map, reg,
Mark Brown82159ba2012-01-18 10:52:25 +00001030 map->work_buf +
1031 map->format.reg_bytes +
1032 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +02001033 map->format.val_bytes);
1034 }
1035}
1036
1037/**
1038 * regmap_write(): Write a value to a single register
1039 *
1040 * @map: Register map to write to
1041 * @reg: Register to write to
1042 * @val: Value to be written
1043 *
1044 * A value of zero will be returned on success, a negative errno will
1045 * be returned in error cases.
1046 */
1047int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1048{
1049 int ret;
1050
Stephen Warrenf01ee602012-04-09 13:40:24 -06001051 if (reg % map->reg_stride)
1052 return -EINVAL;
1053
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001054 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001055
1056 ret = _regmap_write(map, reg, val);
1057
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001058 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001059
1060 return ret;
1061}
1062EXPORT_SYMBOL_GPL(regmap_write);
1063
1064/**
1065 * regmap_raw_write(): Write raw values to one or more registers
1066 *
1067 * @map: Register map to write to
1068 * @reg: Initial register to write to
1069 * @val: Block of data to be written, laid out for direct transmission to the
1070 * device
1071 * @val_len: Length of data pointed to by val.
1072 *
1073 * This function is intended to be used for things like firmware
1074 * download where a large block of data needs to be transferred to the
1075 * device. No formatting will be done on the data provided.
1076 *
1077 * A value of zero will be returned on success, a negative errno will
1078 * be returned in error cases.
1079 */
1080int regmap_raw_write(struct regmap *map, unsigned int reg,
1081 const void *val, size_t val_len)
1082{
1083 int ret;
1084
Stephen Warren851960b2012-04-06 15:16:03 -06001085 if (val_len % map->format.val_bytes)
1086 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001087 if (reg % map->reg_stride)
1088 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001089
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001090 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001091
1092 ret = _regmap_raw_write(map, reg, val, val_len);
1093
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001094 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001095
1096 return ret;
1097}
1098EXPORT_SYMBOL_GPL(regmap_raw_write);
1099
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301100/*
1101 * regmap_bulk_write(): Write multiple registers to the device
1102 *
1103 * @map: Register map to write to
1104 * @reg: First register to be write from
1105 * @val: Block of data to be written, in native register size for device
1106 * @val_count: Number of registers to write
1107 *
1108 * This function is intended to be used for writing a large block of
1109 * data to be device either in single transfer or multiple transfer.
1110 *
1111 * A value of zero will be returned on success, a negative errno will
1112 * be returned in error cases.
1113 */
1114int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1115 size_t val_count)
1116{
1117 int ret = 0, i;
1118 size_t val_bytes = map->format.val_bytes;
1119 void *wval;
1120
1121 if (!map->format.parse_val)
1122 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001123 if (reg % map->reg_stride)
1124 return -EINVAL;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301125
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001126 map->lock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301127
1128 /* No formatting is require if val_byte is 1 */
1129 if (val_bytes == 1) {
1130 wval = (void *)val;
1131 } else {
1132 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1133 if (!wval) {
1134 ret = -ENOMEM;
1135 dev_err(map->dev, "Error in memory allocation\n");
1136 goto out;
1137 }
1138 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1139 map->format.parse_val(wval + i);
1140 }
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001141 /*
1142 * Some devices does not support bulk write, for
1143 * them we have a series of single write operations.
1144 */
1145 if (map->use_single_rw) {
1146 for (i = 0; i < val_count; i++) {
1147 ret = regmap_raw_write(map,
1148 reg + (i * map->reg_stride),
1149 val + (i * val_bytes),
1150 val_bytes);
1151 if (ret != 0)
1152 return ret;
1153 }
1154 } else {
1155 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1156 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301157
1158 if (val_bytes != 1)
1159 kfree(wval);
1160
1161out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001162 map->unlock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301163 return ret;
1164}
1165EXPORT_SYMBOL_GPL(regmap_bulk_write);
1166
Mark Brownb83a3132011-05-11 19:59:58 +02001167static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1168 unsigned int val_len)
1169{
Mark Brown98bc7df2012-10-04 17:31:11 +01001170 struct regmap_range_node *range;
Mark Brownb83a3132011-05-11 19:59:58 +02001171 u8 *u8 = map->work_buf;
1172 int ret;
1173
Mark Brown98bc7df2012-10-04 17:31:11 +01001174 range = _regmap_range_lookup(map, reg);
1175 if (range) {
1176 ret = _regmap_select_page(map, &reg, range,
1177 val_len / map->format.val_bytes);
Mark Brown0ff3e622012-10-04 17:39:13 +01001178 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001179 return ret;
1180 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001181
Marc Reillyd939fb92012-03-16 12:11:43 +11001182 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001183
1184 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001185 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +02001186 * register addresss; since it's always the high bits for all
1187 * current formats we can do this here rather than in
1188 * formatting. This may break if we get interesting formats.
1189 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001190 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001191
Mark Brownfb2736b2011-07-24 21:30:55 +01001192 trace_regmap_hw_read_start(map->dev, reg,
1193 val_len / map->format.val_bytes);
1194
Stephen Warren0135bbc2012-04-04 15:48:30 -06001195 ret = map->bus->read(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001196 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +01001197 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001198
Mark Brownfb2736b2011-07-24 21:30:55 +01001199 trace_regmap_hw_read_done(map->dev, reg,
1200 val_len / map->format.val_bytes);
1201
1202 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001203}
1204
1205static int _regmap_read(struct regmap *map, unsigned int reg,
1206 unsigned int *val)
1207{
1208 int ret;
1209
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001210 if (!map->cache_bypass) {
1211 ret = regcache_read(map, reg, val);
1212 if (ret == 0)
1213 return 0;
1214 }
1215
Lars-Peter Clausen19254412011-11-16 16:28:19 +01001216 if (!map->format.parse_val)
1217 return -EINVAL;
1218
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001219 if (map->cache_only)
1220 return -EBUSY;
1221
Mark Brownb83a3132011-05-11 19:59:58 +02001222 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +01001223 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +02001224 *val = map->format.parse_val(map->work_buf);
Mark Brown1044c182012-07-06 14:10:23 +01001225
1226#ifdef LOG_DEVICE
1227 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1228 dev_info(map->dev, "%x => %x\n", reg, *val);
1229#endif
1230
Mark Brownfb2736b2011-07-24 21:30:55 +01001231 trace_regmap_reg_read(map->dev, reg, *val);
1232 }
Mark Brownb83a3132011-05-11 19:59:58 +02001233
Mark Brownf2985362012-04-30 21:25:05 +01001234 if (ret == 0 && !map->cache_bypass)
1235 regcache_write(map, reg, *val);
1236
Mark Brownb83a3132011-05-11 19:59:58 +02001237 return ret;
1238}
1239
1240/**
1241 * regmap_read(): Read a value from a single register
1242 *
1243 * @map: Register map to write to
1244 * @reg: Register to be read from
1245 * @val: Pointer to store read value
1246 *
1247 * A value of zero will be returned on success, a negative errno will
1248 * be returned in error cases.
1249 */
1250int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1251{
1252 int ret;
1253
Stephen Warrenf01ee602012-04-09 13:40:24 -06001254 if (reg % map->reg_stride)
1255 return -EINVAL;
1256
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001257 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001258
1259 ret = _regmap_read(map, reg, val);
1260
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001261 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001262
1263 return ret;
1264}
1265EXPORT_SYMBOL_GPL(regmap_read);
1266
1267/**
1268 * regmap_raw_read(): Read raw data from the device
1269 *
1270 * @map: Register map to write to
1271 * @reg: First register to be read from
1272 * @val: Pointer to store read value
1273 * @val_len: Size of data to read
1274 *
1275 * A value of zero will be returned on success, a negative errno will
1276 * be returned in error cases.
1277 */
1278int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1279 size_t val_len)
1280{
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001281 size_t val_bytes = map->format.val_bytes;
1282 size_t val_count = val_len / val_bytes;
1283 unsigned int v;
1284 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +01001285
Stephen Warren851960b2012-04-06 15:16:03 -06001286 if (val_len % map->format.val_bytes)
1287 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001288 if (reg % map->reg_stride)
1289 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001290
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001291 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001292
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001293 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1294 map->cache_type == REGCACHE_NONE) {
1295 /* Physical block read if there's no cache involved */
1296 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001297
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001298 } else {
1299 /* Otherwise go word by word for the cache; should be low
1300 * cost as we expect to hit the cache.
1301 */
1302 for (i = 0; i < val_count; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -06001303 ret = _regmap_read(map, reg + (i * map->reg_stride),
1304 &v);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001305 if (ret != 0)
1306 goto out;
1307
Marc Reillyd939fb92012-03-16 12:11:43 +11001308 map->format.format_val(val + (i * val_bytes), v, 0);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001309 }
1310 }
1311
1312 out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001313 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001314
1315 return ret;
1316}
1317EXPORT_SYMBOL_GPL(regmap_raw_read);
1318
1319/**
1320 * regmap_bulk_read(): Read multiple registers from the device
1321 *
1322 * @map: Register map to write to
1323 * @reg: First register to be read from
1324 * @val: Pointer to store read value, in native register size for device
1325 * @val_count: Number of registers to read
1326 *
1327 * A value of zero will be returned on success, a negative errno will
1328 * be returned in error cases.
1329 */
1330int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1331 size_t val_count)
1332{
1333 int ret, i;
1334 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +01001335 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001336
Mark Brownb83a3132011-05-11 19:59:58 +02001337 if (!map->format.parse_val)
1338 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001339 if (reg % map->reg_stride)
1340 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001341
Mark Brownde2d8082011-10-10 13:24:52 +01001342 if (vol || map->cache_type == REGCACHE_NONE) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001343 /*
1344 * Some devices does not support bulk read, for
1345 * them we have a series of single read operations.
1346 */
1347 if (map->use_single_rw) {
1348 for (i = 0; i < val_count; i++) {
1349 ret = regmap_raw_read(map,
1350 reg + (i * map->reg_stride),
1351 val + (i * val_bytes),
1352 val_bytes);
1353 if (ret != 0)
1354 return ret;
1355 }
1356 } else {
1357 ret = regmap_raw_read(map, reg, val,
1358 val_bytes * val_count);
1359 if (ret != 0)
1360 return ret;
1361 }
Mark Brownde2d8082011-10-10 13:24:52 +01001362
1363 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1364 map->format.parse_val(val + i);
1365 } else {
1366 for (i = 0; i < val_count; i++) {
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301367 unsigned int ival;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001368 ret = regmap_read(map, reg + (i * map->reg_stride),
Mark Brown25061d22012-05-12 13:06:08 +01001369 &ival);
Mark Brownde2d8082011-10-10 13:24:52 +01001370 if (ret != 0)
1371 return ret;
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301372 memcpy(val + (i * val_bytes), &ival, val_bytes);
Mark Brownde2d8082011-10-10 13:24:52 +01001373 }
1374 }
Mark Brownb83a3132011-05-11 19:59:58 +02001375
1376 return 0;
1377}
1378EXPORT_SYMBOL_GPL(regmap_bulk_read);
1379
Mark Brown018690d2011-11-29 20:10:36 +00001380static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1381 unsigned int mask, unsigned int val,
1382 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +02001383{
1384 int ret;
Mark Brownd91e8db2011-11-18 16:03:50 +00001385 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +02001386
Mark Brownd91e8db2011-11-18 16:03:50 +00001387 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +02001388 if (ret != 0)
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001389 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001390
Mark Brownd91e8db2011-11-18 16:03:50 +00001391 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001392 tmp |= val & mask;
1393
Mark Brown018690d2011-11-29 20:10:36 +00001394 if (tmp != orig) {
Mark Brownd91e8db2011-11-18 16:03:50 +00001395 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +00001396 *change = true;
1397 } else {
1398 *change = false;
1399 }
Mark Brownb83a3132011-05-11 19:59:58 +02001400
Mark Brownb83a3132011-05-11 19:59:58 +02001401 return ret;
1402}
Mark Brown018690d2011-11-29 20:10:36 +00001403
1404/**
1405 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1406 *
1407 * @map: Register map to update
1408 * @reg: Register to update
1409 * @mask: Bitmask to change
1410 * @val: New value for bitmask
1411 *
1412 * Returns zero for success, a negative number on error.
1413 */
1414int regmap_update_bits(struct regmap *map, unsigned int reg,
1415 unsigned int mask, unsigned int val)
1416{
1417 bool change;
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001418 int ret;
1419
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001420 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001421 ret = _regmap_update_bits(map, reg, mask, val, &change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001422 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001423
1424 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00001425}
Mark Brownb83a3132011-05-11 19:59:58 +02001426EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +01001427
Mark Brown018690d2011-11-29 20:10:36 +00001428/**
1429 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1430 * register map and report if updated
1431 *
1432 * @map: Register map to update
1433 * @reg: Register to update
1434 * @mask: Bitmask to change
1435 * @val: New value for bitmask
1436 * @change: Boolean indicating if a write was done
1437 *
1438 * Returns zero for success, a negative number on error.
1439 */
1440int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1441 unsigned int mask, unsigned int val,
1442 bool *change)
1443{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001444 int ret;
1445
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001446 map->lock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001447 ret = _regmap_update_bits(map, reg, mask, val, change);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001448 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001449 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00001450}
1451EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1452
Mark Brown22f0d902012-01-21 12:01:14 +00001453/**
1454 * regmap_register_patch: Register and apply register updates to be applied
1455 * on device initialistion
1456 *
1457 * @map: Register map to apply updates to.
1458 * @regs: Values to update.
1459 * @num_regs: Number of entries in regs.
1460 *
1461 * Register a set of register updates to be applied to the device
1462 * whenever the device registers are synchronised with the cache and
1463 * apply them immediately. Typically this is used to apply
1464 * corrections to be applied to the device defaults on startup, such
1465 * as the updates some vendors provide to undocumented registers.
1466 */
1467int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1468 int num_regs)
1469{
1470 int i, ret;
1471 bool bypass;
1472
1473 /* If needed the implementation can be extended to support this */
1474 if (map->patch)
1475 return -EBUSY;
1476
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001477 map->lock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00001478
1479 bypass = map->cache_bypass;
1480
1481 map->cache_bypass = true;
1482
1483 /* Write out first; it's useful to apply even if we fail later. */
1484 for (i = 0; i < num_regs; i++) {
1485 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1486 if (ret != 0) {
1487 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1488 regs[i].reg, regs[i].def, ret);
1489 goto out;
1490 }
1491 }
1492
Axel Lin2a14d7d92012-02-10 19:29:55 +08001493 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
Mark Brown22f0d902012-01-21 12:01:14 +00001494 if (map->patch != NULL) {
1495 memcpy(map->patch, regs,
1496 num_regs * sizeof(struct reg_default));
1497 map->patch_regs = num_regs;
1498 } else {
1499 ret = -ENOMEM;
1500 }
1501
1502out:
1503 map->cache_bypass = bypass;
1504
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001505 map->unlock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00001506
1507 return ret;
1508}
1509EXPORT_SYMBOL_GPL(regmap_register_patch);
1510
Mark Browneae4b512012-03-14 13:15:03 +00001511/*
Mark Browna6539c32012-02-17 14:20:14 -08001512 * regmap_get_val_bytes(): Report the size of a register value
1513 *
1514 * Report the size of a register value, mainly intended to for use by
1515 * generic infrastructure built on top of regmap.
1516 */
1517int regmap_get_val_bytes(struct regmap *map)
1518{
1519 if (map->format.format_write)
1520 return -EINVAL;
1521
1522 return map->format.val_bytes;
1523}
1524EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1525
Mark Brown31244e32011-07-20 22:56:53 +01001526static int __init regmap_initcall(void)
1527{
1528 regmap_debugfs_initcall();
1529
1530 return 0;
1531}
1532postcore_initcall(regmap_initcall);