blob: 2366b6299f072e95f748cb72cb93c86ef603c7f7 [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
13#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/err.h>
17
Mark Brownfb2736b2011-07-24 21:30:55 +010018#define CREATE_TRACE_POINTS
19#include <trace/events/regmap.h>
20
Mark Brown93de9122011-07-20 22:35:37 +010021#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020022
Mark Brown8de2f082011-08-10 17:14:41 +090023bool regmap_writeable(struct regmap *map, unsigned int reg)
24{
25 if (map->max_register && reg > map->max_register)
26 return false;
27
28 if (map->writeable_reg)
29 return map->writeable_reg(map->dev, reg);
30
31 return true;
32}
33
34bool regmap_readable(struct regmap *map, unsigned int reg)
35{
36 if (map->max_register && reg > map->max_register)
37 return false;
38
39 if (map->readable_reg)
40 return map->readable_reg(map->dev, reg);
41
42 return true;
43}
44
45bool regmap_volatile(struct regmap *map, unsigned int reg)
46{
47 if (map->max_register && reg > map->max_register)
48 return false;
49
50 if (map->volatile_reg)
51 return map->volatile_reg(map->dev, reg);
52
53 return true;
54}
55
56bool regmap_precious(struct regmap *map, unsigned int reg)
57{
58 if (map->max_register && reg > map->max_register)
59 return false;
60
61 if (map->precious_reg)
62 return map->precious_reg(map->dev, reg);
63
64 return false;
65}
66
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +010067static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
68 unsigned int num)
69{
70 unsigned int i;
71
72 for (i = 0; i < num; i++)
73 if (!regmap_volatile(map, reg + i))
74 return false;
75
76 return true;
77}
78
Mark Brownb83a3132011-05-11 19:59:58 +020079static void regmap_format_4_12_write(struct regmap *map,
80 unsigned int reg, unsigned int val)
81{
82 __be16 *out = map->work_buf;
83 *out = cpu_to_be16((reg << 12) | val);
84}
85
86static void regmap_format_7_9_write(struct regmap *map,
87 unsigned int reg, unsigned int val)
88{
89 __be16 *out = map->work_buf;
90 *out = cpu_to_be16((reg << 9) | val);
91}
92
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +010093static void regmap_format_10_14_write(struct regmap *map,
94 unsigned int reg, unsigned int val)
95{
96 u8 *out = map->work_buf;
97
98 out[2] = val;
99 out[1] = (val >> 8) | (reg << 6);
100 out[0] = reg >> 2;
101}
102
Mark Brownb83a3132011-05-11 19:59:58 +0200103static void regmap_format_8(void *buf, unsigned int val)
104{
105 u8 *b = buf;
106
107 b[0] = val;
108}
109
110static void regmap_format_16(void *buf, unsigned int val)
111{
112 __be16 *b = buf;
113
114 b[0] = cpu_to_be16(val);
115}
116
117static unsigned int regmap_parse_8(void *buf)
118{
119 u8 *b = buf;
120
121 return b[0];
122}
123
124static unsigned int regmap_parse_16(void *buf)
125{
126 __be16 *b = buf;
127
128 b[0] = be16_to_cpu(b[0]);
129
130 return b[0];
131}
132
133/**
134 * regmap_init(): Initialise register map
135 *
136 * @dev: Device that will be interacted with
137 * @bus: Bus-specific callbacks to use with device
138 * @config: Configuration for register map
139 *
140 * The return value will be an ERR_PTR() on error or a valid pointer to
141 * a struct regmap. This function should generally not be called
142 * directly, it should be called by bus-specific init functions.
143 */
144struct regmap *regmap_init(struct device *dev,
145 const struct regmap_bus *bus,
146 const struct regmap_config *config)
147{
148 struct regmap *map;
149 int ret = -EINVAL;
150
151 if (!bus || !config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100152 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200153
154 map = kzalloc(sizeof(*map), GFP_KERNEL);
155 if (map == NULL) {
156 ret = -ENOMEM;
157 goto err;
158 }
159
160 mutex_init(&map->lock);
161 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
162 map->format.reg_bytes = config->reg_bits / 8;
163 map->format.val_bytes = config->val_bits / 8;
164 map->dev = dev;
165 map->bus = bus;
Mark Brown2e2ae662011-07-20 22:33:39 +0100166 map->max_register = config->max_register;
167 map->writeable_reg = config->writeable_reg;
168 map->readable_reg = config->readable_reg;
169 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900170 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100171 map->cache_type = config->cache_type;
Mark Brownb83a3132011-05-11 19:59:58 +0200172
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200173 if (config->read_flag_mask || config->write_flag_mask) {
174 map->read_flag_mask = config->read_flag_mask;
175 map->write_flag_mask = config->write_flag_mask;
176 } else {
177 map->read_flag_mask = bus->read_flag_mask;
178 }
179
Mark Brownb83a3132011-05-11 19:59:58 +0200180 switch (config->reg_bits) {
181 case 4:
182 switch (config->val_bits) {
183 case 12:
184 map->format.format_write = regmap_format_4_12_write;
185 break;
186 default:
187 goto err_map;
188 }
189 break;
190
191 case 7:
192 switch (config->val_bits) {
193 case 9:
194 map->format.format_write = regmap_format_7_9_write;
195 break;
196 default:
197 goto err_map;
198 }
199 break;
200
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100201 case 10:
202 switch (config->val_bits) {
203 case 14:
204 map->format.format_write = regmap_format_10_14_write;
205 break;
206 default:
207 goto err_map;
208 }
209 break;
210
Mark Brownb83a3132011-05-11 19:59:58 +0200211 case 8:
212 map->format.format_reg = regmap_format_8;
213 break;
214
215 case 16:
216 map->format.format_reg = regmap_format_16;
217 break;
218
219 default:
220 goto err_map;
221 }
222
223 switch (config->val_bits) {
224 case 8:
225 map->format.format_val = regmap_format_8;
226 map->format.parse_val = regmap_parse_8;
227 break;
228 case 16:
229 map->format.format_val = regmap_format_16;
230 map->format.parse_val = regmap_parse_16;
231 break;
232 }
233
234 if (!map->format.format_write &&
235 !(map->format.format_reg && map->format.format_val))
236 goto err_map;
237
238 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
239 if (map->work_buf == NULL) {
240 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700241 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200242 }
243
Mark Brown052d2cd2011-11-21 19:05:13 +0000244 regmap_debugfs_init(map);
245
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100246 ret = regcache_init(map, config);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100247 if (ret < 0)
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100248 goto err_free_workbuf;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100249
Mark Brownb83a3132011-05-11 19:59:58 +0200250 return map;
251
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100252err_free_workbuf:
253 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200254err_map:
255 kfree(map);
256err:
257 return ERR_PTR(ret);
258}
259EXPORT_SYMBOL_GPL(regmap_init);
260
261/**
Mark Brownbf315172011-12-03 17:06:20 +0000262 * regmap_reinit_cache(): Reinitialise the current register cache
263 *
264 * @map: Register map to operate on.
265 * @config: New configuration. Only the cache data will be used.
266 *
267 * Discard any existing register cache for the map and initialize a
268 * new cache. This can be used to restore the cache to defaults or to
269 * update the cache configuration to reflect runtime discovery of the
270 * hardware.
271 */
272int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
273{
274 int ret;
275
276 mutex_lock(&map->lock);
277
278 regcache_exit(map);
279
280 map->max_register = config->max_register;
281 map->writeable_reg = config->writeable_reg;
282 map->readable_reg = config->readable_reg;
283 map->volatile_reg = config->volatile_reg;
284 map->precious_reg = config->precious_reg;
285 map->cache_type = config->cache_type;
286
Mark Brown421e8d22012-01-20 13:39:37 +0000287 map->cache_bypass = false;
288 map->cache_only = false;
289
Mark Brownbf315172011-12-03 17:06:20 +0000290 ret = regcache_init(map, config);
291
292 mutex_unlock(&map->lock);
293
294 return ret;
295}
296
297/**
Mark Brownb83a3132011-05-11 19:59:58 +0200298 * regmap_exit(): Free a previously allocated register map
299 */
300void regmap_exit(struct regmap *map)
301{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100302 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100303 regmap_debugfs_exit(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200304 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200305 kfree(map);
306}
307EXPORT_SYMBOL_GPL(regmap_exit);
308
309static int _regmap_raw_write(struct regmap *map, unsigned int reg,
310 const void *val, size_t val_len)
311{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200312 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200313 void *buf;
314 int ret = -ENOTSUPP;
315 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100316 int i;
317
318 /* Check for unwritable registers before we start */
319 if (map->writeable_reg)
320 for (i = 0; i < val_len / map->format.val_bytes; i++)
321 if (!map->writeable_reg(map->dev, reg + i))
322 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200323
Laxman Dewanganc9157192012-02-10 21:30:27 +0530324 if (!map->cache_bypass && map->format.parse_val) {
325 unsigned int ival;
326 int val_bytes = map->format.val_bytes;
327 for (i = 0; i < val_len / val_bytes; i++) {
328 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
329 ival = map->format.parse_val(map->work_buf);
330 ret = regcache_write(map, reg + i, ival);
331 if (ret) {
332 dev_err(map->dev,
333 "Error in caching of register: %u ret: %d\n",
334 reg + i, ret);
335 return ret;
336 }
337 }
338 if (map->cache_only) {
339 map->cache_dirty = true;
340 return 0;
341 }
342 }
343
Mark Brownb83a3132011-05-11 19:59:58 +0200344 map->format.format_reg(map->work_buf, reg);
345
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200346 u8[0] |= map->write_flag_mask;
347
Mark Brownfb2736b2011-07-24 21:30:55 +0100348 trace_regmap_hw_write_start(map->dev, reg,
349 val_len / map->format.val_bytes);
350
Mark Brown2547e202011-07-20 21:47:22 +0100351 /* If we're doing a single register write we can probably just
352 * send the work_buf directly, otherwise try to do a gather
353 * write.
354 */
355 if (val == map->work_buf + map->format.reg_bytes)
356 ret = map->bus->write(map->dev, map->work_buf,
357 map->format.reg_bytes + val_len);
358 else if (map->bus->gather_write)
Mark Brownb83a3132011-05-11 19:59:58 +0200359 ret = map->bus->gather_write(map->dev, map->work_buf,
360 map->format.reg_bytes,
361 val, val_len);
362
Mark Brown2547e202011-07-20 21:47:22 +0100363 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200364 if (ret == -ENOTSUPP) {
365 len = map->format.reg_bytes + val_len;
366 buf = kmalloc(len, GFP_KERNEL);
367 if (!buf)
368 return -ENOMEM;
369
370 memcpy(buf, map->work_buf, map->format.reg_bytes);
371 memcpy(buf + map->format.reg_bytes, val, val_len);
372 ret = map->bus->write(map->dev, buf, len);
373
374 kfree(buf);
375 }
376
Mark Brownfb2736b2011-07-24 21:30:55 +0100377 trace_regmap_hw_write_done(map->dev, reg,
378 val_len / map->format.val_bytes);
379
Mark Brownb83a3132011-05-11 19:59:58 +0200380 return ret;
381}
382
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100383int _regmap_write(struct regmap *map, unsigned int reg,
384 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200385{
Mark Brownfb2736b2011-07-24 21:30:55 +0100386 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200387 BUG_ON(!map->format.format_write && !map->format.format_val);
388
Laxman Dewanganc9157192012-02-10 21:30:27 +0530389 if (!map->cache_bypass && map->format.format_write) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100390 ret = regcache_write(map, reg, val);
391 if (ret != 0)
392 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200393 if (map->cache_only) {
394 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100395 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200396 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100397 }
398
Mark Brownfb2736b2011-07-24 21:30:55 +0100399 trace_regmap_reg_write(map->dev, reg, val);
400
Mark Brownb83a3132011-05-11 19:59:58 +0200401 if (map->format.format_write) {
402 map->format.format_write(map, reg, val);
403
Mark Brownfb2736b2011-07-24 21:30:55 +0100404 trace_regmap_hw_write_start(map->dev, reg, 1);
405
406 ret = map->bus->write(map->dev, map->work_buf,
407 map->format.buf_size);
408
409 trace_regmap_hw_write_done(map->dev, reg, 1);
410
411 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200412 } else {
413 map->format.format_val(map->work_buf + map->format.reg_bytes,
414 val);
415 return _regmap_raw_write(map, reg,
416 map->work_buf + map->format.reg_bytes,
417 map->format.val_bytes);
418 }
419}
420
421/**
422 * regmap_write(): Write a value to a single register
423 *
424 * @map: Register map to write to
425 * @reg: Register to write to
426 * @val: Value to be written
427 *
428 * A value of zero will be returned on success, a negative errno will
429 * be returned in error cases.
430 */
431int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
432{
433 int ret;
434
435 mutex_lock(&map->lock);
436
437 ret = _regmap_write(map, reg, val);
438
439 mutex_unlock(&map->lock);
440
441 return ret;
442}
443EXPORT_SYMBOL_GPL(regmap_write);
444
445/**
446 * regmap_raw_write(): Write raw values to one or more registers
447 *
448 * @map: Register map to write to
449 * @reg: Initial register to write to
450 * @val: Block of data to be written, laid out for direct transmission to the
451 * device
452 * @val_len: Length of data pointed to by val.
453 *
454 * This function is intended to be used for things like firmware
455 * download where a large block of data needs to be transferred to the
456 * device. No formatting will be done on the data provided.
457 *
458 * A value of zero will be returned on success, a negative errno will
459 * be returned in error cases.
460 */
461int regmap_raw_write(struct regmap *map, unsigned int reg,
462 const void *val, size_t val_len)
463{
464 int ret;
465
466 mutex_lock(&map->lock);
467
468 ret = _regmap_raw_write(map, reg, val, val_len);
469
470 mutex_unlock(&map->lock);
471
472 return ret;
473}
474EXPORT_SYMBOL_GPL(regmap_raw_write);
475
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530476/*
477 * regmap_bulk_write(): Write multiple registers to the device
478 *
479 * @map: Register map to write to
480 * @reg: First register to be write from
481 * @val: Block of data to be written, in native register size for device
482 * @val_count: Number of registers to write
483 *
484 * This function is intended to be used for writing a large block of
485 * data to be device either in single transfer or multiple transfer.
486 *
487 * A value of zero will be returned on success, a negative errno will
488 * be returned in error cases.
489 */
490int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
491 size_t val_count)
492{
493 int ret = 0, i;
494 size_t val_bytes = map->format.val_bytes;
495 void *wval;
496
497 if (!map->format.parse_val)
498 return -EINVAL;
499
500 mutex_lock(&map->lock);
501
502 /* No formatting is require if val_byte is 1 */
503 if (val_bytes == 1) {
504 wval = (void *)val;
505 } else {
506 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
507 if (!wval) {
508 ret = -ENOMEM;
509 dev_err(map->dev, "Error in memory allocation\n");
510 goto out;
511 }
512 for (i = 0; i < val_count * val_bytes; i += val_bytes)
513 map->format.parse_val(wval + i);
514 }
515 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
516
517 if (val_bytes != 1)
518 kfree(wval);
519
520out:
521 mutex_unlock(&map->lock);
522 return ret;
523}
524EXPORT_SYMBOL_GPL(regmap_bulk_write);
525
Mark Brownb83a3132011-05-11 19:59:58 +0200526static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
527 unsigned int val_len)
528{
529 u8 *u8 = map->work_buf;
530 int ret;
531
532 map->format.format_reg(map->work_buf, reg);
533
534 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200535 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +0200536 * register addresss; since it's always the high bits for all
537 * current formats we can do this here rather than in
538 * formatting. This may break if we get interesting formats.
539 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200540 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200541
Mark Brownfb2736b2011-07-24 21:30:55 +0100542 trace_regmap_hw_read_start(map->dev, reg,
543 val_len / map->format.val_bytes);
544
Mark Brownb83a3132011-05-11 19:59:58 +0200545 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +0100546 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200547
Mark Brownfb2736b2011-07-24 21:30:55 +0100548 trace_regmap_hw_read_done(map->dev, reg,
549 val_len / map->format.val_bytes);
550
551 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200552}
553
554static int _regmap_read(struct regmap *map, unsigned int reg,
555 unsigned int *val)
556{
557 int ret;
558
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100559 if (!map->cache_bypass) {
560 ret = regcache_read(map, reg, val);
561 if (ret == 0)
562 return 0;
563 }
564
Lars-Peter Clausen19254412011-11-16 16:28:19 +0100565 if (!map->format.parse_val)
566 return -EINVAL;
567
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100568 if (map->cache_only)
569 return -EBUSY;
570
Mark Brownb83a3132011-05-11 19:59:58 +0200571 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +0100572 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +0200573 *val = map->format.parse_val(map->work_buf);
Mark Brownfb2736b2011-07-24 21:30:55 +0100574 trace_regmap_reg_read(map->dev, reg, *val);
575 }
Mark Brownb83a3132011-05-11 19:59:58 +0200576
577 return ret;
578}
579
580/**
581 * regmap_read(): Read a value from a single register
582 *
583 * @map: Register map to write to
584 * @reg: Register to be read from
585 * @val: Pointer to store read value
586 *
587 * A value of zero will be returned on success, a negative errno will
588 * be returned in error cases.
589 */
590int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
591{
592 int ret;
593
594 mutex_lock(&map->lock);
595
596 ret = _regmap_read(map, reg, val);
597
598 mutex_unlock(&map->lock);
599
600 return ret;
601}
602EXPORT_SYMBOL_GPL(regmap_read);
603
604/**
605 * regmap_raw_read(): Read raw data from the device
606 *
607 * @map: Register map to write to
608 * @reg: First register to be read from
609 * @val: Pointer to store read value
610 * @val_len: Size of data to read
611 *
612 * A value of zero will be returned on success, a negative errno will
613 * be returned in error cases.
614 */
615int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
616 size_t val_len)
617{
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100618 size_t val_count = val_len / map->format.val_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200619 int ret;
620
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100621 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
622 map->cache_type != REGCACHE_NONE);
Mark Brown04e016a2011-10-09 13:35:43 +0100623
Mark Brownb83a3132011-05-11 19:59:58 +0200624 mutex_lock(&map->lock);
625
626 ret = _regmap_raw_read(map, reg, val, val_len);
627
628 mutex_unlock(&map->lock);
629
630 return ret;
631}
632EXPORT_SYMBOL_GPL(regmap_raw_read);
633
634/**
635 * regmap_bulk_read(): Read multiple registers from the device
636 *
637 * @map: Register map to write to
638 * @reg: First register to be read from
639 * @val: Pointer to store read value, in native register size for device
640 * @val_count: Number of registers to read
641 *
642 * A value of zero will be returned on success, a negative errno will
643 * be returned in error cases.
644 */
645int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
646 size_t val_count)
647{
648 int ret, i;
649 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100650 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100651
Mark Brownb83a3132011-05-11 19:59:58 +0200652 if (!map->format.parse_val)
653 return -EINVAL;
654
Mark Brownde2d8082011-10-10 13:24:52 +0100655 if (vol || map->cache_type == REGCACHE_NONE) {
656 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
657 if (ret != 0)
658 return ret;
659
660 for (i = 0; i < val_count * val_bytes; i += val_bytes)
661 map->format.parse_val(val + i);
662 } else {
663 for (i = 0; i < val_count; i++) {
664 ret = regmap_read(map, reg + i, val + (i * val_bytes));
665 if (ret != 0)
666 return ret;
667 }
668 }
Mark Brownb83a3132011-05-11 19:59:58 +0200669
670 return 0;
671}
672EXPORT_SYMBOL_GPL(regmap_bulk_read);
673
Mark Brown018690d2011-11-29 20:10:36 +0000674static int _regmap_update_bits(struct regmap *map, unsigned int reg,
675 unsigned int mask, unsigned int val,
676 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +0200677{
678 int ret;
Mark Brownd91e8db2011-11-18 16:03:50 +0000679 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +0200680
681 mutex_lock(&map->lock);
682
Mark Brownd91e8db2011-11-18 16:03:50 +0000683 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +0200684 if (ret != 0)
685 goto out;
686
Mark Brownd91e8db2011-11-18 16:03:50 +0000687 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200688 tmp |= val & mask;
689
Mark Brown018690d2011-11-29 20:10:36 +0000690 if (tmp != orig) {
Mark Brownd91e8db2011-11-18 16:03:50 +0000691 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +0000692 *change = true;
693 } else {
694 *change = false;
695 }
Mark Brownb83a3132011-05-11 19:59:58 +0200696
697out:
698 mutex_unlock(&map->lock);
699
700 return ret;
701}
Mark Brown018690d2011-11-29 20:10:36 +0000702
703/**
704 * regmap_update_bits: Perform a read/modify/write cycle on the register map
705 *
706 * @map: Register map to update
707 * @reg: Register to update
708 * @mask: Bitmask to change
709 * @val: New value for bitmask
710 *
711 * Returns zero for success, a negative number on error.
712 */
713int regmap_update_bits(struct regmap *map, unsigned int reg,
714 unsigned int mask, unsigned int val)
715{
716 bool change;
717 return _regmap_update_bits(map, reg, mask, val, &change);
718}
Mark Brownb83a3132011-05-11 19:59:58 +0200719EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +0100720
Mark Brown018690d2011-11-29 20:10:36 +0000721/**
722 * regmap_update_bits_check: Perform a read/modify/write cycle on the
723 * register map and report if updated
724 *
725 * @map: Register map to update
726 * @reg: Register to update
727 * @mask: Bitmask to change
728 * @val: New value for bitmask
729 * @change: Boolean indicating if a write was done
730 *
731 * Returns zero for success, a negative number on error.
732 */
733int regmap_update_bits_check(struct regmap *map, unsigned int reg,
734 unsigned int mask, unsigned int val,
735 bool *change)
736{
737 return _regmap_update_bits(map, reg, mask, val, change);
738}
739EXPORT_SYMBOL_GPL(regmap_update_bits_check);
740
Mark Brown31244e32011-07-20 22:56:53 +0100741static int __init regmap_initcall(void)
742{
743 regmap_debugfs_initcall();
744
745 return 0;
746}
747postcore_initcall(regmap_initcall);