blob: 839cc82bd176ebabbef528e22f9d8673df25f981 [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
Mark Brownc0eb4672012-01-30 19:56:52 +0000261static void devm_regmap_release(struct device *dev, void *res)
262{
263 regmap_exit(*(struct regmap **)res);
264}
265
266/**
267 * devm_regmap_init(): Initialise managed register map
268 *
269 * @dev: Device that will be interacted with
270 * @bus: Bus-specific callbacks to use with device
271 * @config: Configuration for register map
272 *
273 * The return value will be an ERR_PTR() on error or a valid pointer
274 * to a struct regmap. This function should generally not be called
275 * directly, it should be called by bus-specific init functions. The
276 * map will be automatically freed by the device management code.
277 */
278struct regmap *devm_regmap_init(struct device *dev,
279 const struct regmap_bus *bus,
280 const struct regmap_config *config)
281{
282 struct regmap **ptr, *regmap;
283
284 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
285 if (!ptr)
286 return ERR_PTR(-ENOMEM);
287
288 regmap = regmap_init(dev, bus, config);
289 if (!IS_ERR(regmap)) {
290 *ptr = regmap;
291 devres_add(dev, ptr);
292 } else {
293 devres_free(ptr);
294 }
295
296 return regmap;
297}
298EXPORT_SYMBOL_GPL(devm_regmap_init);
299
Mark Brownb83a3132011-05-11 19:59:58 +0200300/**
Mark Brownbf315172011-12-03 17:06:20 +0000301 * regmap_reinit_cache(): Reinitialise the current register cache
302 *
303 * @map: Register map to operate on.
304 * @config: New configuration. Only the cache data will be used.
305 *
306 * Discard any existing register cache for the map and initialize a
307 * new cache. This can be used to restore the cache to defaults or to
308 * update the cache configuration to reflect runtime discovery of the
309 * hardware.
310 */
311int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
312{
313 int ret;
314
315 mutex_lock(&map->lock);
316
317 regcache_exit(map);
318
319 map->max_register = config->max_register;
320 map->writeable_reg = config->writeable_reg;
321 map->readable_reg = config->readable_reg;
322 map->volatile_reg = config->volatile_reg;
323 map->precious_reg = config->precious_reg;
324 map->cache_type = config->cache_type;
325
326 ret = regcache_init(map, config);
327
328 mutex_unlock(&map->lock);
329
330 return ret;
331}
332
333/**
Mark Brownb83a3132011-05-11 19:59:58 +0200334 * regmap_exit(): Free a previously allocated register map
335 */
336void regmap_exit(struct regmap *map)
337{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100338 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100339 regmap_debugfs_exit(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200340 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200341 kfree(map);
342}
343EXPORT_SYMBOL_GPL(regmap_exit);
344
345static int _regmap_raw_write(struct regmap *map, unsigned int reg,
346 const void *val, size_t val_len)
347{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200348 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200349 void *buf;
350 int ret = -ENOTSUPP;
351 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100352 int i;
353
354 /* Check for unwritable registers before we start */
355 if (map->writeable_reg)
356 for (i = 0; i < val_len / map->format.val_bytes; i++)
357 if (!map->writeable_reg(map->dev, reg + i))
358 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200359
360 map->format.format_reg(map->work_buf, reg);
361
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200362 u8[0] |= map->write_flag_mask;
363
Mark Brownfb2736b2011-07-24 21:30:55 +0100364 trace_regmap_hw_write_start(map->dev, reg,
365 val_len / map->format.val_bytes);
366
Mark Brown2547e202011-07-20 21:47:22 +0100367 /* If we're doing a single register write we can probably just
368 * send the work_buf directly, otherwise try to do a gather
369 * write.
370 */
371 if (val == map->work_buf + map->format.reg_bytes)
372 ret = map->bus->write(map->dev, map->work_buf,
373 map->format.reg_bytes + val_len);
374 else if (map->bus->gather_write)
Mark Brownb83a3132011-05-11 19:59:58 +0200375 ret = map->bus->gather_write(map->dev, map->work_buf,
376 map->format.reg_bytes,
377 val, val_len);
378
Mark Brown2547e202011-07-20 21:47:22 +0100379 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200380 if (ret == -ENOTSUPP) {
381 len = map->format.reg_bytes + val_len;
382 buf = kmalloc(len, GFP_KERNEL);
383 if (!buf)
384 return -ENOMEM;
385
386 memcpy(buf, map->work_buf, map->format.reg_bytes);
387 memcpy(buf + map->format.reg_bytes, val, val_len);
388 ret = map->bus->write(map->dev, buf, len);
389
390 kfree(buf);
391 }
392
Mark Brownfb2736b2011-07-24 21:30:55 +0100393 trace_regmap_hw_write_done(map->dev, reg,
394 val_len / map->format.val_bytes);
395
Mark Brownb83a3132011-05-11 19:59:58 +0200396 return ret;
397}
398
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100399int _regmap_write(struct regmap *map, unsigned int reg,
400 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200401{
Mark Brownfb2736b2011-07-24 21:30:55 +0100402 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200403 BUG_ON(!map->format.format_write && !map->format.format_val);
404
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100405 if (!map->cache_bypass) {
406 ret = regcache_write(map, reg, val);
407 if (ret != 0)
408 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200409 if (map->cache_only) {
410 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100411 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200412 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100413 }
414
Mark Brownfb2736b2011-07-24 21:30:55 +0100415 trace_regmap_reg_write(map->dev, reg, val);
416
Mark Brownb83a3132011-05-11 19:59:58 +0200417 if (map->format.format_write) {
418 map->format.format_write(map, reg, val);
419
Mark Brownfb2736b2011-07-24 21:30:55 +0100420 trace_regmap_hw_write_start(map->dev, reg, 1);
421
422 ret = map->bus->write(map->dev, map->work_buf,
423 map->format.buf_size);
424
425 trace_regmap_hw_write_done(map->dev, reg, 1);
426
427 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200428 } else {
429 map->format.format_val(map->work_buf + map->format.reg_bytes,
430 val);
431 return _regmap_raw_write(map, reg,
432 map->work_buf + map->format.reg_bytes,
433 map->format.val_bytes);
434 }
435}
436
437/**
438 * regmap_write(): Write a value to a single register
439 *
440 * @map: Register map to write to
441 * @reg: Register to write to
442 * @val: Value to be written
443 *
444 * A value of zero will be returned on success, a negative errno will
445 * be returned in error cases.
446 */
447int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
448{
449 int ret;
450
451 mutex_lock(&map->lock);
452
453 ret = _regmap_write(map, reg, val);
454
455 mutex_unlock(&map->lock);
456
457 return ret;
458}
459EXPORT_SYMBOL_GPL(regmap_write);
460
461/**
462 * regmap_raw_write(): Write raw values to one or more registers
463 *
464 * @map: Register map to write to
465 * @reg: Initial register to write to
466 * @val: Block of data to be written, laid out for direct transmission to the
467 * device
468 * @val_len: Length of data pointed to by val.
469 *
470 * This function is intended to be used for things like firmware
471 * download where a large block of data needs to be transferred to the
472 * device. No formatting will be done on the data provided.
473 *
474 * A value of zero will be returned on success, a negative errno will
475 * be returned in error cases.
476 */
477int regmap_raw_write(struct regmap *map, unsigned int reg,
478 const void *val, size_t val_len)
479{
Lars-Peter Clausenc48a9d72011-11-08 18:37:26 +0100480 size_t val_count = val_len / map->format.val_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200481 int ret;
482
Lars-Peter Clausenc48a9d72011-11-08 18:37:26 +0100483 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
484 map->cache_type != REGCACHE_NONE);
Mark Brown04e016a2011-10-09 13:35:43 +0100485
Mark Brownb83a3132011-05-11 19:59:58 +0200486 mutex_lock(&map->lock);
487
488 ret = _regmap_raw_write(map, reg, val, val_len);
489
490 mutex_unlock(&map->lock);
491
492 return ret;
493}
494EXPORT_SYMBOL_GPL(regmap_raw_write);
495
496static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
497 unsigned int val_len)
498{
499 u8 *u8 = map->work_buf;
500 int ret;
501
502 map->format.format_reg(map->work_buf, reg);
503
504 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200505 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +0200506 * register addresss; since it's always the high bits for all
507 * current formats we can do this here rather than in
508 * formatting. This may break if we get interesting formats.
509 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200510 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200511
Mark Brownfb2736b2011-07-24 21:30:55 +0100512 trace_regmap_hw_read_start(map->dev, reg,
513 val_len / map->format.val_bytes);
514
Mark Brownb83a3132011-05-11 19:59:58 +0200515 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +0100516 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200517
Mark Brownfb2736b2011-07-24 21:30:55 +0100518 trace_regmap_hw_read_done(map->dev, reg,
519 val_len / map->format.val_bytes);
520
521 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200522}
523
524static int _regmap_read(struct regmap *map, unsigned int reg,
525 unsigned int *val)
526{
527 int ret;
528
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100529 if (!map->cache_bypass) {
530 ret = regcache_read(map, reg, val);
531 if (ret == 0)
532 return 0;
533 }
534
Lars-Peter Clausen19254412011-11-16 16:28:19 +0100535 if (!map->format.parse_val)
536 return -EINVAL;
537
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100538 if (map->cache_only)
539 return -EBUSY;
540
Mark Brownb83a3132011-05-11 19:59:58 +0200541 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +0100542 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +0200543 *val = map->format.parse_val(map->work_buf);
Mark Brownfb2736b2011-07-24 21:30:55 +0100544 trace_regmap_reg_read(map->dev, reg, *val);
545 }
Mark Brownb83a3132011-05-11 19:59:58 +0200546
547 return ret;
548}
549
550/**
551 * regmap_read(): Read a value from a single register
552 *
553 * @map: Register map to write to
554 * @reg: Register to be read from
555 * @val: Pointer to store read value
556 *
557 * A value of zero will be returned on success, a negative errno will
558 * be returned in error cases.
559 */
560int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
561{
562 int ret;
563
564 mutex_lock(&map->lock);
565
566 ret = _regmap_read(map, reg, val);
567
568 mutex_unlock(&map->lock);
569
570 return ret;
571}
572EXPORT_SYMBOL_GPL(regmap_read);
573
574/**
575 * regmap_raw_read(): Read raw data from the device
576 *
577 * @map: Register map to write to
578 * @reg: First register to be read from
579 * @val: Pointer to store read value
580 * @val_len: Size of data to read
581 *
582 * A value of zero will be returned on success, a negative errno will
583 * be returned in error cases.
584 */
585int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
586 size_t val_len)
587{
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100588 size_t val_count = val_len / map->format.val_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200589 int ret;
590
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100591 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
592 map->cache_type != REGCACHE_NONE);
Mark Brown04e016a2011-10-09 13:35:43 +0100593
Mark Brownb83a3132011-05-11 19:59:58 +0200594 mutex_lock(&map->lock);
595
596 ret = _regmap_raw_read(map, reg, val, val_len);
597
598 mutex_unlock(&map->lock);
599
600 return ret;
601}
602EXPORT_SYMBOL_GPL(regmap_raw_read);
603
604/**
605 * regmap_bulk_read(): Read multiple registers 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, in native register size for device
610 * @val_count: Number of registers 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_bulk_read(struct regmap *map, unsigned int reg, void *val,
616 size_t val_count)
617{
618 int ret, i;
619 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100620 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100621
Mark Brownb83a3132011-05-11 19:59:58 +0200622 if (!map->format.parse_val)
623 return -EINVAL;
624
Mark Brownde2d8082011-10-10 13:24:52 +0100625 if (vol || map->cache_type == REGCACHE_NONE) {
626 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
627 if (ret != 0)
628 return ret;
629
630 for (i = 0; i < val_count * val_bytes; i += val_bytes)
631 map->format.parse_val(val + i);
632 } else {
633 for (i = 0; i < val_count; i++) {
634 ret = regmap_read(map, reg + i, val + (i * val_bytes));
635 if (ret != 0)
636 return ret;
637 }
638 }
Mark Brownb83a3132011-05-11 19:59:58 +0200639
640 return 0;
641}
642EXPORT_SYMBOL_GPL(regmap_bulk_read);
643
Mark Brown018690d2011-11-29 20:10:36 +0000644static int _regmap_update_bits(struct regmap *map, unsigned int reg,
645 unsigned int mask, unsigned int val,
646 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +0200647{
648 int ret;
Mark Brownd91e8db2011-11-18 16:03:50 +0000649 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +0200650
651 mutex_lock(&map->lock);
652
Mark Brownd91e8db2011-11-18 16:03:50 +0000653 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +0200654 if (ret != 0)
655 goto out;
656
Mark Brownd91e8db2011-11-18 16:03:50 +0000657 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200658 tmp |= val & mask;
659
Mark Brown018690d2011-11-29 20:10:36 +0000660 if (tmp != orig) {
Mark Brownd91e8db2011-11-18 16:03:50 +0000661 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +0000662 *change = true;
663 } else {
664 *change = false;
665 }
Mark Brownb83a3132011-05-11 19:59:58 +0200666
667out:
668 mutex_unlock(&map->lock);
669
670 return ret;
671}
Mark Brown018690d2011-11-29 20:10:36 +0000672
673/**
674 * regmap_update_bits: Perform a read/modify/write cycle on the register map
675 *
676 * @map: Register map to update
677 * @reg: Register to update
678 * @mask: Bitmask to change
679 * @val: New value for bitmask
680 *
681 * Returns zero for success, a negative number on error.
682 */
683int regmap_update_bits(struct regmap *map, unsigned int reg,
684 unsigned int mask, unsigned int val)
685{
686 bool change;
687 return _regmap_update_bits(map, reg, mask, val, &change);
688}
Mark Brownb83a3132011-05-11 19:59:58 +0200689EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +0100690
Mark Brown018690d2011-11-29 20:10:36 +0000691/**
692 * regmap_update_bits_check: Perform a read/modify/write cycle on the
693 * register map and report if updated
694 *
695 * @map: Register map to update
696 * @reg: Register to update
697 * @mask: Bitmask to change
698 * @val: New value for bitmask
699 * @change: Boolean indicating if a write was done
700 *
701 * Returns zero for success, a negative number on error.
702 */
703int regmap_update_bits_check(struct regmap *map, unsigned int reg,
704 unsigned int mask, unsigned int val,
705 bool *change)
706{
707 return _regmap_update_bits(map, reg, mask, val, change);
708}
709EXPORT_SYMBOL_GPL(regmap_update_bits_check);
710
Mark Brown31244e32011-07-20 22:56:53 +0100711static int __init regmap_initcall(void)
712{
713 regmap_debugfs_initcall();
714
715 return 0;
716}
717postcore_initcall(regmap_initcall);