blob: b4475d87f4a0e8c64061913b8a6a57ca95a00547 [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
Wolfram Sang9aa50752012-01-27 16:10:22 +010079static void regmap_format_2_6_write(struct regmap *map,
80 unsigned int reg, unsigned int val)
81{
82 u8 *out = map->work_buf;
83
84 *out = (reg << 6) | val;
85}
86
Mark Brownb83a3132011-05-11 19:59:58 +020087static void regmap_format_4_12_write(struct regmap *map,
88 unsigned int reg, unsigned int val)
89{
90 __be16 *out = map->work_buf;
91 *out = cpu_to_be16((reg << 12) | val);
92}
93
94static void regmap_format_7_9_write(struct regmap *map,
95 unsigned int reg, unsigned int val)
96{
97 __be16 *out = map->work_buf;
98 *out = cpu_to_be16((reg << 9) | val);
99}
100
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100101static void regmap_format_10_14_write(struct regmap *map,
102 unsigned int reg, unsigned int val)
103{
104 u8 *out = map->work_buf;
105
106 out[2] = val;
107 out[1] = (val >> 8) | (reg << 6);
108 out[0] = reg >> 2;
109}
110
Mark Brownb83a3132011-05-11 19:59:58 +0200111static void regmap_format_8(void *buf, unsigned int val)
112{
113 u8 *b = buf;
114
115 b[0] = val;
116}
117
118static void regmap_format_16(void *buf, unsigned int val)
119{
120 __be16 *b = buf;
121
122 b[0] = cpu_to_be16(val);
123}
124
125static unsigned int regmap_parse_8(void *buf)
126{
127 u8 *b = buf;
128
129 return b[0];
130}
131
132static unsigned int regmap_parse_16(void *buf)
133{
134 __be16 *b = buf;
135
136 b[0] = be16_to_cpu(b[0]);
137
138 return b[0];
139}
140
141/**
142 * regmap_init(): Initialise register map
143 *
144 * @dev: Device that will be interacted with
145 * @bus: Bus-specific callbacks to use with device
146 * @config: Configuration for register map
147 *
148 * The return value will be an ERR_PTR() on error or a valid pointer to
149 * a struct regmap. This function should generally not be called
150 * directly, it should be called by bus-specific init functions.
151 */
152struct regmap *regmap_init(struct device *dev,
153 const struct regmap_bus *bus,
154 const struct regmap_config *config)
155{
156 struct regmap *map;
157 int ret = -EINVAL;
158
159 if (!bus || !config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100160 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200161
162 map = kzalloc(sizeof(*map), GFP_KERNEL);
163 if (map == NULL) {
164 ret = -ENOMEM;
165 goto err;
166 }
167
168 mutex_init(&map->lock);
169 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
170 map->format.reg_bytes = config->reg_bits / 8;
Mark Brown82159ba2012-01-18 10:52:25 +0000171 map->format.pad_bytes = config->pad_bits / 8;
Mark Brownb83a3132011-05-11 19:59:58 +0200172 map->format.val_bytes = config->val_bits / 8;
Mark Brown82159ba2012-01-18 10:52:25 +0000173 map->format.buf_size += map->format.pad_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200174 map->dev = dev;
175 map->bus = bus;
Mark Brown2e2ae662011-07-20 22:33:39 +0100176 map->max_register = config->max_register;
177 map->writeable_reg = config->writeable_reg;
178 map->readable_reg = config->readable_reg;
179 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900180 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100181 map->cache_type = config->cache_type;
Mark Brownb83a3132011-05-11 19:59:58 +0200182
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200183 if (config->read_flag_mask || config->write_flag_mask) {
184 map->read_flag_mask = config->read_flag_mask;
185 map->write_flag_mask = config->write_flag_mask;
186 } else {
187 map->read_flag_mask = bus->read_flag_mask;
188 }
189
Mark Brownb83a3132011-05-11 19:59:58 +0200190 switch (config->reg_bits) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100191 case 2:
192 switch (config->val_bits) {
193 case 6:
194 map->format.format_write = regmap_format_2_6_write;
195 break;
196 default:
197 goto err_map;
198 }
199 break;
200
Mark Brownb83a3132011-05-11 19:59:58 +0200201 case 4:
202 switch (config->val_bits) {
203 case 12:
204 map->format.format_write = regmap_format_4_12_write;
205 break;
206 default:
207 goto err_map;
208 }
209 break;
210
211 case 7:
212 switch (config->val_bits) {
213 case 9:
214 map->format.format_write = regmap_format_7_9_write;
215 break;
216 default:
217 goto err_map;
218 }
219 break;
220
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100221 case 10:
222 switch (config->val_bits) {
223 case 14:
224 map->format.format_write = regmap_format_10_14_write;
225 break;
226 default:
227 goto err_map;
228 }
229 break;
230
Mark Brownb83a3132011-05-11 19:59:58 +0200231 case 8:
232 map->format.format_reg = regmap_format_8;
233 break;
234
235 case 16:
236 map->format.format_reg = regmap_format_16;
237 break;
238
239 default:
240 goto err_map;
241 }
242
243 switch (config->val_bits) {
244 case 8:
245 map->format.format_val = regmap_format_8;
246 map->format.parse_val = regmap_parse_8;
247 break;
248 case 16:
249 map->format.format_val = regmap_format_16;
250 map->format.parse_val = regmap_parse_16;
251 break;
252 }
253
254 if (!map->format.format_write &&
255 !(map->format.format_reg && map->format.format_val))
256 goto err_map;
257
Mark Brown82159ba2012-01-18 10:52:25 +0000258 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200259 if (map->work_buf == NULL) {
260 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700261 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200262 }
263
Mark Brown052d2cd2011-11-21 19:05:13 +0000264 regmap_debugfs_init(map);
265
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100266 ret = regcache_init(map, config);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100267 if (ret < 0)
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100268 goto err_free_workbuf;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100269
Mark Brownb83a3132011-05-11 19:59:58 +0200270 return map;
271
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100272err_free_workbuf:
273 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200274err_map:
275 kfree(map);
276err:
277 return ERR_PTR(ret);
278}
279EXPORT_SYMBOL_GPL(regmap_init);
280
281/**
Mark Brownbf315172011-12-03 17:06:20 +0000282 * regmap_reinit_cache(): Reinitialise the current register cache
283 *
284 * @map: Register map to operate on.
285 * @config: New configuration. Only the cache data will be used.
286 *
287 * Discard any existing register cache for the map and initialize a
288 * new cache. This can be used to restore the cache to defaults or to
289 * update the cache configuration to reflect runtime discovery of the
290 * hardware.
291 */
292int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
293{
294 int ret;
295
296 mutex_lock(&map->lock);
297
298 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000299 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000300
301 map->max_register = config->max_register;
302 map->writeable_reg = config->writeable_reg;
303 map->readable_reg = config->readable_reg;
304 map->volatile_reg = config->volatile_reg;
305 map->precious_reg = config->precious_reg;
306 map->cache_type = config->cache_type;
307
Mark Browna24f64a2012-01-26 18:30:16 +0000308 regmap_debugfs_init(map);
309
Mark Brownbf315172011-12-03 17:06:20 +0000310 ret = regcache_init(map, config);
311
312 mutex_unlock(&map->lock);
313
314 return ret;
315}
316
317/**
Mark Brownb83a3132011-05-11 19:59:58 +0200318 * regmap_exit(): Free a previously allocated register map
319 */
320void regmap_exit(struct regmap *map)
321{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100322 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100323 regmap_debugfs_exit(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200324 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200325 kfree(map);
326}
327EXPORT_SYMBOL_GPL(regmap_exit);
328
329static int _regmap_raw_write(struct regmap *map, unsigned int reg,
330 const void *val, size_t val_len)
331{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200332 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200333 void *buf;
334 int ret = -ENOTSUPP;
335 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100336 int i;
337
338 /* Check for unwritable registers before we start */
339 if (map->writeable_reg)
340 for (i = 0; i < val_len / map->format.val_bytes; i++)
341 if (!map->writeable_reg(map->dev, reg + i))
342 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200343
344 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 */
Mark Brown82159ba2012-01-18 10:52:25 +0000355 if (val == (map->work_buf + map->format.pad_bytes +
356 map->format.reg_bytes))
Mark Brown2547e202011-07-20 21:47:22 +0100357 ret = map->bus->write(map->dev, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000358 map->format.reg_bytes +
359 map->format.pad_bytes +
360 val_len);
Mark Brown2547e202011-07-20 21:47:22 +0100361 else if (map->bus->gather_write)
Mark Brownb83a3132011-05-11 19:59:58 +0200362 ret = map->bus->gather_write(map->dev, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000363 map->format.reg_bytes +
364 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200365 val, val_len);
366
Mark Brown2547e202011-07-20 21:47:22 +0100367 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200368 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +0000369 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
370 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200371 if (!buf)
372 return -ENOMEM;
373
374 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +0000375 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
376 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200377 ret = map->bus->write(map->dev, buf, len);
378
379 kfree(buf);
380 }
381
Mark Brownfb2736b2011-07-24 21:30:55 +0100382 trace_regmap_hw_write_done(map->dev, reg,
383 val_len / map->format.val_bytes);
384
Mark Brownb83a3132011-05-11 19:59:58 +0200385 return ret;
386}
387
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100388int _regmap_write(struct regmap *map, unsigned int reg,
389 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200390{
Mark Brownfb2736b2011-07-24 21:30:55 +0100391 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200392 BUG_ON(!map->format.format_write && !map->format.format_val);
393
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100394 if (!map->cache_bypass) {
395 ret = regcache_write(map, reg, val);
396 if (ret != 0)
397 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200398 if (map->cache_only) {
399 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100400 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200401 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100402 }
403
Mark Brownfb2736b2011-07-24 21:30:55 +0100404 trace_regmap_reg_write(map->dev, reg, val);
405
Mark Brownb83a3132011-05-11 19:59:58 +0200406 if (map->format.format_write) {
407 map->format.format_write(map, reg, val);
408
Mark Brownfb2736b2011-07-24 21:30:55 +0100409 trace_regmap_hw_write_start(map->dev, reg, 1);
410
411 ret = map->bus->write(map->dev, map->work_buf,
412 map->format.buf_size);
413
414 trace_regmap_hw_write_done(map->dev, reg, 1);
415
416 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200417 } else {
Mark Brown82159ba2012-01-18 10:52:25 +0000418 map->format.format_val(map->work_buf + map->format.reg_bytes
419 + map->format.pad_bytes, val);
Mark Brownb83a3132011-05-11 19:59:58 +0200420 return _regmap_raw_write(map, reg,
Mark Brown82159ba2012-01-18 10:52:25 +0000421 map->work_buf +
422 map->format.reg_bytes +
423 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200424 map->format.val_bytes);
425 }
426}
427
428/**
429 * regmap_write(): Write a value to a single register
430 *
431 * @map: Register map to write to
432 * @reg: Register to write to
433 * @val: Value to be written
434 *
435 * A value of zero will be returned on success, a negative errno will
436 * be returned in error cases.
437 */
438int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
439{
440 int ret;
441
442 mutex_lock(&map->lock);
443
444 ret = _regmap_write(map, reg, val);
445
446 mutex_unlock(&map->lock);
447
448 return ret;
449}
450EXPORT_SYMBOL_GPL(regmap_write);
451
452/**
453 * regmap_raw_write(): Write raw values to one or more registers
454 *
455 * @map: Register map to write to
456 * @reg: Initial register to write to
457 * @val: Block of data to be written, laid out for direct transmission to the
458 * device
459 * @val_len: Length of data pointed to by val.
460 *
461 * This function is intended to be used for things like firmware
462 * download where a large block of data needs to be transferred to the
463 * device. No formatting will be done on the data provided.
464 *
465 * A value of zero will be returned on success, a negative errno will
466 * be returned in error cases.
467 */
468int regmap_raw_write(struct regmap *map, unsigned int reg,
469 const void *val, size_t val_len)
470{
Lars-Peter Clausenc48a9d72011-11-08 18:37:26 +0100471 size_t val_count = val_len / map->format.val_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200472 int ret;
473
Lars-Peter Clausenc48a9d72011-11-08 18:37:26 +0100474 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
475 map->cache_type != REGCACHE_NONE);
Mark Brown04e016a2011-10-09 13:35:43 +0100476
Mark Brownb83a3132011-05-11 19:59:58 +0200477 mutex_lock(&map->lock);
478
479 ret = _regmap_raw_write(map, reg, val, val_len);
480
481 mutex_unlock(&map->lock);
482
483 return ret;
484}
485EXPORT_SYMBOL_GPL(regmap_raw_write);
486
487static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
488 unsigned int val_len)
489{
490 u8 *u8 = map->work_buf;
491 int ret;
492
493 map->format.format_reg(map->work_buf, reg);
494
495 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200496 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +0200497 * register addresss; since it's always the high bits for all
498 * current formats we can do this here rather than in
499 * formatting. This may break if we get interesting formats.
500 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200501 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200502
Mark Brownfb2736b2011-07-24 21:30:55 +0100503 trace_regmap_hw_read_start(map->dev, reg,
504 val_len / map->format.val_bytes);
505
Mark Brown82159ba2012-01-18 10:52:25 +0000506 ret = map->bus->read(map->dev, map->work_buf,
507 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +0100508 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200509
Mark Brownfb2736b2011-07-24 21:30:55 +0100510 trace_regmap_hw_read_done(map->dev, reg,
511 val_len / map->format.val_bytes);
512
513 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200514}
515
516static int _regmap_read(struct regmap *map, unsigned int reg,
517 unsigned int *val)
518{
519 int ret;
520
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100521 if (!map->cache_bypass) {
522 ret = regcache_read(map, reg, val);
523 if (ret == 0)
524 return 0;
525 }
526
Lars-Peter Clausen19254412011-11-16 16:28:19 +0100527 if (!map->format.parse_val)
528 return -EINVAL;
529
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100530 if (map->cache_only)
531 return -EBUSY;
532
Mark Brownb83a3132011-05-11 19:59:58 +0200533 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +0100534 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +0200535 *val = map->format.parse_val(map->work_buf);
Mark Brownfb2736b2011-07-24 21:30:55 +0100536 trace_regmap_reg_read(map->dev, reg, *val);
537 }
Mark Brownb83a3132011-05-11 19:59:58 +0200538
539 return ret;
540}
541
542/**
543 * regmap_read(): Read a value from a single register
544 *
545 * @map: Register map to write to
546 * @reg: Register to be read from
547 * @val: Pointer to store read value
548 *
549 * A value of zero will be returned on success, a negative errno will
550 * be returned in error cases.
551 */
552int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
553{
554 int ret;
555
556 mutex_lock(&map->lock);
557
558 ret = _regmap_read(map, reg, val);
559
560 mutex_unlock(&map->lock);
561
562 return ret;
563}
564EXPORT_SYMBOL_GPL(regmap_read);
565
566/**
567 * regmap_raw_read(): Read raw data from the device
568 *
569 * @map: Register map to write to
570 * @reg: First register to be read from
571 * @val: Pointer to store read value
572 * @val_len: Size of data to read
573 *
574 * A value of zero will be returned on success, a negative errno will
575 * be returned in error cases.
576 */
577int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
578 size_t val_len)
579{
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100580 size_t val_count = val_len / map->format.val_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200581 int ret;
582
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100583 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
584 map->cache_type != REGCACHE_NONE);
Mark Brown04e016a2011-10-09 13:35:43 +0100585
Mark Brownb83a3132011-05-11 19:59:58 +0200586 mutex_lock(&map->lock);
587
588 ret = _regmap_raw_read(map, reg, val, val_len);
589
590 mutex_unlock(&map->lock);
591
592 return ret;
593}
594EXPORT_SYMBOL_GPL(regmap_raw_read);
595
596/**
597 * regmap_bulk_read(): Read multiple registers from the device
598 *
599 * @map: Register map to write to
600 * @reg: First register to be read from
601 * @val: Pointer to store read value, in native register size for device
602 * @val_count: Number of registers to read
603 *
604 * A value of zero will be returned on success, a negative errno will
605 * be returned in error cases.
606 */
607int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
608 size_t val_count)
609{
610 int ret, i;
611 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100612 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100613
Mark Brownb83a3132011-05-11 19:59:58 +0200614 if (!map->format.parse_val)
615 return -EINVAL;
616
Mark Brownde2d8082011-10-10 13:24:52 +0100617 if (vol || map->cache_type == REGCACHE_NONE) {
618 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
619 if (ret != 0)
620 return ret;
621
622 for (i = 0; i < val_count * val_bytes; i += val_bytes)
623 map->format.parse_val(val + i);
624 } else {
625 for (i = 0; i < val_count; i++) {
626 ret = regmap_read(map, reg + i, val + (i * val_bytes));
627 if (ret != 0)
628 return ret;
629 }
630 }
Mark Brownb83a3132011-05-11 19:59:58 +0200631
632 return 0;
633}
634EXPORT_SYMBOL_GPL(regmap_bulk_read);
635
Mark Brown018690d2011-11-29 20:10:36 +0000636static int _regmap_update_bits(struct regmap *map, unsigned int reg,
637 unsigned int mask, unsigned int val,
638 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +0200639{
640 int ret;
Mark Brownd91e8db2011-11-18 16:03:50 +0000641 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +0200642
643 mutex_lock(&map->lock);
644
Mark Brownd91e8db2011-11-18 16:03:50 +0000645 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +0200646 if (ret != 0)
647 goto out;
648
Mark Brownd91e8db2011-11-18 16:03:50 +0000649 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200650 tmp |= val & mask;
651
Mark Brown018690d2011-11-29 20:10:36 +0000652 if (tmp != orig) {
Mark Brownd91e8db2011-11-18 16:03:50 +0000653 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +0000654 *change = true;
655 } else {
656 *change = false;
657 }
Mark Brownb83a3132011-05-11 19:59:58 +0200658
659out:
660 mutex_unlock(&map->lock);
661
662 return ret;
663}
Mark Brown018690d2011-11-29 20:10:36 +0000664
665/**
666 * regmap_update_bits: Perform a read/modify/write cycle on the register map
667 *
668 * @map: Register map to update
669 * @reg: Register to update
670 * @mask: Bitmask to change
671 * @val: New value for bitmask
672 *
673 * Returns zero for success, a negative number on error.
674 */
675int regmap_update_bits(struct regmap *map, unsigned int reg,
676 unsigned int mask, unsigned int val)
677{
678 bool change;
679 return _regmap_update_bits(map, reg, mask, val, &change);
680}
Mark Brownb83a3132011-05-11 19:59:58 +0200681EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +0100682
Mark Brown018690d2011-11-29 20:10:36 +0000683/**
684 * regmap_update_bits_check: Perform a read/modify/write cycle on the
685 * register map and report if updated
686 *
687 * @map: Register map to update
688 * @reg: Register to update
689 * @mask: Bitmask to change
690 * @val: New value for bitmask
691 * @change: Boolean indicating if a write was done
692 *
693 * Returns zero for success, a negative number on error.
694 */
695int regmap_update_bits_check(struct regmap *map, unsigned int reg,
696 unsigned int mask, unsigned int val,
697 bool *change)
698{
699 return _regmap_update_bits(map, reg, mask, val, change);
700}
701EXPORT_SYMBOL_GPL(regmap_update_bits_check);
702
Mark Brown31244e32011-07-20 22:56:53 +0100703static int __init regmap_initcall(void)
704{
705 regmap_debugfs_initcall();
706
707 return 0;
708}
709postcore_initcall(regmap_initcall);