blob: 1cf721421bec25cfc8477730ba6994cf06378f62 [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>
18
Mark Brownfb2736b2011-07-24 21:30:55 +010019#define CREATE_TRACE_POINTS
20#include <trace/events/regmap.h>
21
Mark Brown93de9122011-07-20 22:35:37 +010022#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020023
Mark Brown8de2f082011-08-10 17:14:41 +090024bool regmap_writeable(struct regmap *map, unsigned int reg)
25{
26 if (map->max_register && reg > map->max_register)
27 return false;
28
29 if (map->writeable_reg)
30 return map->writeable_reg(map->dev, reg);
31
32 return true;
33}
34
35bool regmap_readable(struct regmap *map, unsigned int reg)
36{
37 if (map->max_register && reg > map->max_register)
38 return false;
39
Wolfram Sang4191f192012-01-30 15:08:16 +010040 if (map->format.format_write)
41 return false;
42
Mark Brown8de2f082011-08-10 17:14:41 +090043 if (map->readable_reg)
44 return map->readable_reg(map->dev, reg);
45
46 return true;
47}
48
49bool regmap_volatile(struct regmap *map, unsigned int reg)
50{
Wolfram Sang4191f192012-01-30 15:08:16 +010051 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +090052 return false;
53
54 if (map->volatile_reg)
55 return map->volatile_reg(map->dev, reg);
56
57 return true;
58}
59
60bool regmap_precious(struct regmap *map, unsigned int reg)
61{
Wolfram Sang4191f192012-01-30 15:08:16 +010062 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +090063 return false;
64
65 if (map->precious_reg)
66 return map->precious_reg(map->dev, reg);
67
68 return false;
69}
70
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +010071static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
72 unsigned int num)
73{
74 unsigned int i;
75
76 for (i = 0; i < num; i++)
77 if (!regmap_volatile(map, reg + i))
78 return false;
79
80 return true;
81}
82
Wolfram Sang9aa50752012-01-27 16:10:22 +010083static void regmap_format_2_6_write(struct regmap *map,
84 unsigned int reg, unsigned int val)
85{
86 u8 *out = map->work_buf;
87
88 *out = (reg << 6) | val;
89}
90
Mark Brownb83a3132011-05-11 19:59:58 +020091static void regmap_format_4_12_write(struct regmap *map,
92 unsigned int reg, unsigned int val)
93{
94 __be16 *out = map->work_buf;
95 *out = cpu_to_be16((reg << 12) | val);
96}
97
98static void regmap_format_7_9_write(struct regmap *map,
99 unsigned int reg, unsigned int val)
100{
101 __be16 *out = map->work_buf;
102 *out = cpu_to_be16((reg << 9) | val);
103}
104
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100105static void regmap_format_10_14_write(struct regmap *map,
106 unsigned int reg, unsigned int val)
107{
108 u8 *out = map->work_buf;
109
110 out[2] = val;
111 out[1] = (val >> 8) | (reg << 6);
112 out[0] = reg >> 2;
113}
114
Marc Reillyd939fb92012-03-16 12:11:43 +1100115static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200116{
117 u8 *b = buf;
118
Marc Reillyd939fb92012-03-16 12:11:43 +1100119 b[0] = val << shift;
Mark Brownb83a3132011-05-11 19:59:58 +0200120}
121
Stephen Warren141eba22012-05-24 10:47:26 -0600122static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200123{
124 __be16 *b = buf;
125
Marc Reillyd939fb92012-03-16 12:11:43 +1100126 b[0] = cpu_to_be16(val << shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200127}
128
Stephen Warren141eba22012-05-24 10:47:26 -0600129static void regmap_format_16_native(void *buf, unsigned int val,
130 unsigned int shift)
131{
132 *(u16 *)buf = val << shift;
133}
134
Marc Reillyd939fb92012-03-16 12:11:43 +1100135static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
Marc Reillyea279fc2012-03-16 12:11:42 +1100136{
137 u8 *b = buf;
138
Marc Reillyd939fb92012-03-16 12:11:43 +1100139 val <<= shift;
140
Marc Reillyea279fc2012-03-16 12:11:42 +1100141 b[0] = val >> 16;
142 b[1] = val >> 8;
143 b[2] = val;
144}
145
Stephen Warren141eba22012-05-24 10:47:26 -0600146static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
Mark Brown7d5e5252012-02-17 15:58:25 -0800147{
148 __be32 *b = buf;
149
Marc Reillyd939fb92012-03-16 12:11:43 +1100150 b[0] = cpu_to_be32(val << shift);
Mark Brown7d5e5252012-02-17 15:58:25 -0800151}
152
Stephen Warren141eba22012-05-24 10:47:26 -0600153static void regmap_format_32_native(void *buf, unsigned int val,
154 unsigned int shift)
155{
156 *(u32 *)buf = val << shift;
157}
158
Mark Brownb83a3132011-05-11 19:59:58 +0200159static unsigned int regmap_parse_8(void *buf)
160{
161 u8 *b = buf;
162
163 return b[0];
164}
165
Stephen Warren141eba22012-05-24 10:47:26 -0600166static unsigned int regmap_parse_16_be(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200167{
168 __be16 *b = buf;
169
170 b[0] = be16_to_cpu(b[0]);
171
172 return b[0];
173}
174
Stephen Warren141eba22012-05-24 10:47:26 -0600175static unsigned int regmap_parse_16_native(void *buf)
176{
177 return *(u16 *)buf;
178}
179
Marc Reillyea279fc2012-03-16 12:11:42 +1100180static unsigned int regmap_parse_24(void *buf)
181{
182 u8 *b = buf;
183 unsigned int ret = b[2];
184 ret |= ((unsigned int)b[1]) << 8;
185 ret |= ((unsigned int)b[0]) << 16;
186
187 return ret;
188}
189
Stephen Warren141eba22012-05-24 10:47:26 -0600190static unsigned int regmap_parse_32_be(void *buf)
Mark Brown7d5e5252012-02-17 15:58:25 -0800191{
192 __be32 *b = buf;
193
194 b[0] = be32_to_cpu(b[0]);
195
196 return b[0];
197}
198
Stephen Warren141eba22012-05-24 10:47:26 -0600199static unsigned int regmap_parse_32_native(void *buf)
200{
201 return *(u32 *)buf;
202}
203
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600204static void regmap_lock_mutex(struct regmap *map)
205{
206 mutex_lock(&map->mutex);
207}
208
209static void regmap_unlock_mutex(struct regmap *map)
210{
211 mutex_unlock(&map->mutex);
212}
213
214static void regmap_lock_spinlock(struct regmap *map)
215{
216 spin_lock(&map->spinlock);
217}
218
219static void regmap_unlock_spinlock(struct regmap *map)
220{
221 spin_unlock(&map->spinlock);
222}
223
Mark Brown72b39f62012-05-08 17:44:40 +0100224static void dev_get_regmap_release(struct device *dev, void *res)
225{
226 /*
227 * We don't actually have anything to do here; the goal here
228 * is not to manage the regmap but to provide a simple way to
229 * get the regmap back given a struct device.
230 */
231}
232
Mark Brownb83a3132011-05-11 19:59:58 +0200233/**
234 * regmap_init(): Initialise register map
235 *
236 * @dev: Device that will be interacted with
237 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600238 * @bus_context: Data passed to bus-specific callbacks
Mark Brownb83a3132011-05-11 19:59:58 +0200239 * @config: Configuration for register map
240 *
241 * The return value will be an ERR_PTR() on error or a valid pointer to
242 * a struct regmap. This function should generally not be called
243 * directly, it should be called by bus-specific init functions.
244 */
245struct regmap *regmap_init(struct device *dev,
246 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600247 void *bus_context,
Mark Brownb83a3132011-05-11 19:59:58 +0200248 const struct regmap_config *config)
249{
Mark Brown72b39f62012-05-08 17:44:40 +0100250 struct regmap *map, **m;
Mark Brownb83a3132011-05-11 19:59:58 +0200251 int ret = -EINVAL;
Stephen Warren141eba22012-05-24 10:47:26 -0600252 enum regmap_endian reg_endian, val_endian;
Mark Brownb83a3132011-05-11 19:59:58 +0200253
254 if (!bus || !config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100255 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200256
257 map = kzalloc(sizeof(*map), GFP_KERNEL);
258 if (map == NULL) {
259 ret = -ENOMEM;
260 goto err;
261 }
262
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600263 if (bus->fast_io) {
264 spin_lock_init(&map->spinlock);
265 map->lock = regmap_lock_spinlock;
266 map->unlock = regmap_unlock_spinlock;
267 } else {
268 mutex_init(&map->mutex);
269 map->lock = regmap_lock_mutex;
270 map->unlock = regmap_unlock_mutex;
271 }
Mark Brownb83a3132011-05-11 19:59:58 +0200272 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100273 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000274 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100275 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000276 map->format.buf_size += map->format.pad_bytes;
Marc Reillyd939fb92012-03-16 12:11:43 +1100277 map->reg_shift = config->pad_bits % 8;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600278 if (config->reg_stride)
279 map->reg_stride = config->reg_stride;
280 else
281 map->reg_stride = 1;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100282 map->use_single_rw = config->use_single_rw;
Mark Brownb83a3132011-05-11 19:59:58 +0200283 map->dev = dev;
284 map->bus = bus;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600285 map->bus_context = bus_context;
Mark Brown2e2ae662011-07-20 22:33:39 +0100286 map->max_register = config->max_register;
287 map->writeable_reg = config->writeable_reg;
288 map->readable_reg = config->readable_reg;
289 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900290 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100291 map->cache_type = config->cache_type;
Mark Brown72b39f62012-05-08 17:44:40 +0100292 map->name = config->name;
Mark Brownb83a3132011-05-11 19:59:58 +0200293
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200294 if (config->read_flag_mask || config->write_flag_mask) {
295 map->read_flag_mask = config->read_flag_mask;
296 map->write_flag_mask = config->write_flag_mask;
297 } else {
298 map->read_flag_mask = bus->read_flag_mask;
299 }
300
Stephen Warren141eba22012-05-24 10:47:26 -0600301 reg_endian = config->reg_format_endian;
302 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
303 reg_endian = bus->reg_format_endian_default;
304 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
305 reg_endian = REGMAP_ENDIAN_BIG;
306
307 val_endian = config->val_format_endian;
308 if (val_endian == REGMAP_ENDIAN_DEFAULT)
309 val_endian = bus->val_format_endian_default;
310 if (val_endian == REGMAP_ENDIAN_DEFAULT)
311 val_endian = REGMAP_ENDIAN_BIG;
312
Marc Reillyd939fb92012-03-16 12:11:43 +1100313 switch (config->reg_bits + map->reg_shift) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100314 case 2:
315 switch (config->val_bits) {
316 case 6:
317 map->format.format_write = regmap_format_2_6_write;
318 break;
319 default:
320 goto err_map;
321 }
322 break;
323
Mark Brownb83a3132011-05-11 19:59:58 +0200324 case 4:
325 switch (config->val_bits) {
326 case 12:
327 map->format.format_write = regmap_format_4_12_write;
328 break;
329 default:
330 goto err_map;
331 }
332 break;
333
334 case 7:
335 switch (config->val_bits) {
336 case 9:
337 map->format.format_write = regmap_format_7_9_write;
338 break;
339 default:
340 goto err_map;
341 }
342 break;
343
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100344 case 10:
345 switch (config->val_bits) {
346 case 14:
347 map->format.format_write = regmap_format_10_14_write;
348 break;
349 default:
350 goto err_map;
351 }
352 break;
353
Mark Brownb83a3132011-05-11 19:59:58 +0200354 case 8:
355 map->format.format_reg = regmap_format_8;
356 break;
357
358 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600359 switch (reg_endian) {
360 case REGMAP_ENDIAN_BIG:
361 map->format.format_reg = regmap_format_16_be;
362 break;
363 case REGMAP_ENDIAN_NATIVE:
364 map->format.format_reg = regmap_format_16_native;
365 break;
366 default:
367 goto err_map;
368 }
Mark Brownb83a3132011-05-11 19:59:58 +0200369 break;
370
Mark Brown7d5e5252012-02-17 15:58:25 -0800371 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600372 switch (reg_endian) {
373 case REGMAP_ENDIAN_BIG:
374 map->format.format_reg = regmap_format_32_be;
375 break;
376 case REGMAP_ENDIAN_NATIVE:
377 map->format.format_reg = regmap_format_32_native;
378 break;
379 default:
380 goto err_map;
381 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800382 break;
383
Mark Brownb83a3132011-05-11 19:59:58 +0200384 default:
385 goto err_map;
386 }
387
388 switch (config->val_bits) {
389 case 8:
390 map->format.format_val = regmap_format_8;
391 map->format.parse_val = regmap_parse_8;
392 break;
393 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600394 switch (val_endian) {
395 case REGMAP_ENDIAN_BIG:
396 map->format.format_val = regmap_format_16_be;
397 map->format.parse_val = regmap_parse_16_be;
398 break;
399 case REGMAP_ENDIAN_NATIVE:
400 map->format.format_val = regmap_format_16_native;
401 map->format.parse_val = regmap_parse_16_native;
402 break;
403 default:
404 goto err_map;
405 }
Mark Brownb83a3132011-05-11 19:59:58 +0200406 break;
Marc Reillyea279fc2012-03-16 12:11:42 +1100407 case 24:
Stephen Warren141eba22012-05-24 10:47:26 -0600408 if (val_endian != REGMAP_ENDIAN_BIG)
409 goto err_map;
Marc Reillyea279fc2012-03-16 12:11:42 +1100410 map->format.format_val = regmap_format_24;
411 map->format.parse_val = regmap_parse_24;
412 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800413 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600414 switch (val_endian) {
415 case REGMAP_ENDIAN_BIG:
416 map->format.format_val = regmap_format_32_be;
417 map->format.parse_val = regmap_parse_32_be;
418 break;
419 case REGMAP_ENDIAN_NATIVE:
420 map->format.format_val = regmap_format_32_native;
421 map->format.parse_val = regmap_parse_32_native;
422 break;
423 default:
424 goto err_map;
425 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800426 break;
Mark Brownb83a3132011-05-11 19:59:58 +0200427 }
428
Stephen Warren141eba22012-05-24 10:47:26 -0600429 if (map->format.format_write) {
430 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
431 (val_endian != REGMAP_ENDIAN_BIG))
432 goto err_map;
Mark Brown7a647612012-04-30 23:26:32 +0100433 map->use_single_rw = true;
Stephen Warren141eba22012-05-24 10:47:26 -0600434 }
Mark Brown7a647612012-04-30 23:26:32 +0100435
Mark Brownb83a3132011-05-11 19:59:58 +0200436 if (!map->format.format_write &&
437 !(map->format.format_reg && map->format.format_val))
438 goto err_map;
439
Mark Brown82159ba2012-01-18 10:52:25 +0000440 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200441 if (map->work_buf == NULL) {
442 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700443 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200444 }
445
Stephen Warrend3c242e2012-04-04 15:48:29 -0600446 regmap_debugfs_init(map, config->name);
Mark Brown052d2cd2011-11-21 19:05:13 +0000447
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100448 ret = regcache_init(map, config);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100449 if (ret < 0)
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100450 goto err_free_workbuf;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100451
Mark Brown72b39f62012-05-08 17:44:40 +0100452 /* Add a devres resource for dev_get_regmap() */
453 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
454 if (!m) {
455 ret = -ENOMEM;
456 goto err_cache;
457 }
458 *m = map;
459 devres_add(dev, m);
460
Mark Brownb83a3132011-05-11 19:59:58 +0200461 return map;
462
Mark Brown72b39f62012-05-08 17:44:40 +0100463err_cache:
464 regcache_exit(map);
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100465err_free_workbuf:
466 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200467err_map:
468 kfree(map);
469err:
470 return ERR_PTR(ret);
471}
472EXPORT_SYMBOL_GPL(regmap_init);
473
Mark Brownc0eb4672012-01-30 19:56:52 +0000474static void devm_regmap_release(struct device *dev, void *res)
475{
476 regmap_exit(*(struct regmap **)res);
477}
478
479/**
480 * devm_regmap_init(): Initialise managed register map
481 *
482 * @dev: Device that will be interacted with
483 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600484 * @bus_context: Data passed to bus-specific callbacks
Mark Brownc0eb4672012-01-30 19:56:52 +0000485 * @config: Configuration for register map
486 *
487 * The return value will be an ERR_PTR() on error or a valid pointer
488 * to a struct regmap. This function should generally not be called
489 * directly, it should be called by bus-specific init functions. The
490 * map will be automatically freed by the device management code.
491 */
492struct regmap *devm_regmap_init(struct device *dev,
493 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600494 void *bus_context,
Mark Brownc0eb4672012-01-30 19:56:52 +0000495 const struct regmap_config *config)
496{
497 struct regmap **ptr, *regmap;
498
499 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
500 if (!ptr)
501 return ERR_PTR(-ENOMEM);
502
Stephen Warren0135bbc2012-04-04 15:48:30 -0600503 regmap = regmap_init(dev, bus, bus_context, config);
Mark Brownc0eb4672012-01-30 19:56:52 +0000504 if (!IS_ERR(regmap)) {
505 *ptr = regmap;
506 devres_add(dev, ptr);
507 } else {
508 devres_free(ptr);
509 }
510
511 return regmap;
512}
513EXPORT_SYMBOL_GPL(devm_regmap_init);
514
Mark Brownb83a3132011-05-11 19:59:58 +0200515/**
Mark Brownbf315172011-12-03 17:06:20 +0000516 * regmap_reinit_cache(): Reinitialise the current register cache
517 *
518 * @map: Register map to operate on.
519 * @config: New configuration. Only the cache data will be used.
520 *
521 * Discard any existing register cache for the map and initialize a
522 * new cache. This can be used to restore the cache to defaults or to
523 * update the cache configuration to reflect runtime discovery of the
524 * hardware.
525 */
526int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
527{
528 int ret;
529
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600530 map->lock(map);
Mark Brownbf315172011-12-03 17:06:20 +0000531
532 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000533 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000534
535 map->max_register = config->max_register;
536 map->writeable_reg = config->writeable_reg;
537 map->readable_reg = config->readable_reg;
538 map->volatile_reg = config->volatile_reg;
539 map->precious_reg = config->precious_reg;
540 map->cache_type = config->cache_type;
541
Stephen Warrend3c242e2012-04-04 15:48:29 -0600542 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +0000543
Mark Brown421e8d22012-01-20 13:39:37 +0000544 map->cache_bypass = false;
545 map->cache_only = false;
546
Mark Brownbf315172011-12-03 17:06:20 +0000547 ret = regcache_init(map, config);
548
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600549 map->unlock(map);
Mark Brownbf315172011-12-03 17:06:20 +0000550
551 return ret;
552}
553
554/**
Mark Brownb83a3132011-05-11 19:59:58 +0200555 * regmap_exit(): Free a previously allocated register map
556 */
557void regmap_exit(struct regmap *map)
558{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100559 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100560 regmap_debugfs_exit(map);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600561 if (map->bus->free_context)
562 map->bus->free_context(map->bus_context);
Mark Brownb83a3132011-05-11 19:59:58 +0200563 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200564 kfree(map);
565}
566EXPORT_SYMBOL_GPL(regmap_exit);
567
Mark Brown72b39f62012-05-08 17:44:40 +0100568static int dev_get_regmap_match(struct device *dev, void *res, void *data)
569{
570 struct regmap **r = res;
571 if (!r || !*r) {
572 WARN_ON(!r || !*r);
573 return 0;
574 }
575
576 /* If the user didn't specify a name match any */
577 if (data)
578 return (*r)->name == data;
579 else
580 return 1;
581}
582
583/**
584 * dev_get_regmap(): Obtain the regmap (if any) for a device
585 *
586 * @dev: Device to retrieve the map for
587 * @name: Optional name for the register map, usually NULL.
588 *
589 * Returns the regmap for the device if one is present, or NULL. If
590 * name is specified then it must match the name specified when
591 * registering the device, if it is NULL then the first regmap found
592 * will be used. Devices with multiple register maps are very rare,
593 * generic code should normally not need to specify a name.
594 */
595struct regmap *dev_get_regmap(struct device *dev, const char *name)
596{
597 struct regmap **r = devres_find(dev, dev_get_regmap_release,
598 dev_get_regmap_match, (void *)name);
599
600 if (!r)
601 return NULL;
602 return *r;
603}
604EXPORT_SYMBOL_GPL(dev_get_regmap);
605
Mark Brownb83a3132011-05-11 19:59:58 +0200606static int _regmap_raw_write(struct regmap *map, unsigned int reg,
607 const void *val, size_t val_len)
608{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200609 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200610 void *buf;
611 int ret = -ENOTSUPP;
612 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100613 int i;
614
615 /* Check for unwritable registers before we start */
616 if (map->writeable_reg)
617 for (i = 0; i < val_len / map->format.val_bytes; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -0600618 if (!map->writeable_reg(map->dev,
619 reg + (i * map->reg_stride)))
Mark Brown73304782011-07-24 11:46:20 +0100620 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200621
Laxman Dewanganc9157192012-02-10 21:30:27 +0530622 if (!map->cache_bypass && map->format.parse_val) {
623 unsigned int ival;
624 int val_bytes = map->format.val_bytes;
625 for (i = 0; i < val_len / val_bytes; i++) {
626 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
627 ival = map->format.parse_val(map->work_buf);
Stephen Warrenf01ee602012-04-09 13:40:24 -0600628 ret = regcache_write(map, reg + (i * map->reg_stride),
629 ival);
Laxman Dewanganc9157192012-02-10 21:30:27 +0530630 if (ret) {
631 dev_err(map->dev,
632 "Error in caching of register: %u ret: %d\n",
633 reg + i, ret);
634 return ret;
635 }
636 }
637 if (map->cache_only) {
638 map->cache_dirty = true;
639 return 0;
640 }
641 }
642
Marc Reillyd939fb92012-03-16 12:11:43 +1100643 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200644
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200645 u8[0] |= map->write_flag_mask;
646
Mark Brownfb2736b2011-07-24 21:30:55 +0100647 trace_regmap_hw_write_start(map->dev, reg,
648 val_len / map->format.val_bytes);
649
Mark Brown2547e202011-07-20 21:47:22 +0100650 /* If we're doing a single register write we can probably just
651 * send the work_buf directly, otherwise try to do a gather
652 * write.
653 */
Mark Brown82159ba2012-01-18 10:52:25 +0000654 if (val == (map->work_buf + map->format.pad_bytes +
655 map->format.reg_bytes))
Stephen Warren0135bbc2012-04-04 15:48:30 -0600656 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000657 map->format.reg_bytes +
658 map->format.pad_bytes +
659 val_len);
Mark Brown2547e202011-07-20 21:47:22 +0100660 else if (map->bus->gather_write)
Stephen Warren0135bbc2012-04-04 15:48:30 -0600661 ret = map->bus->gather_write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000662 map->format.reg_bytes +
663 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200664 val, val_len);
665
Mark Brown2547e202011-07-20 21:47:22 +0100666 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200667 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +0000668 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
669 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200670 if (!buf)
671 return -ENOMEM;
672
673 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +0000674 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
675 val, val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600676 ret = map->bus->write(map->bus_context, buf, len);
Mark Brownb83a3132011-05-11 19:59:58 +0200677
678 kfree(buf);
679 }
680
Mark Brownfb2736b2011-07-24 21:30:55 +0100681 trace_regmap_hw_write_done(map->dev, reg,
682 val_len / map->format.val_bytes);
683
Mark Brownb83a3132011-05-11 19:59:58 +0200684 return ret;
685}
686
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100687int _regmap_write(struct regmap *map, unsigned int reg,
688 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200689{
Mark Brownfb2736b2011-07-24 21:30:55 +0100690 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200691 BUG_ON(!map->format.format_write && !map->format.format_val);
692
Laxman Dewanganc9157192012-02-10 21:30:27 +0530693 if (!map->cache_bypass && map->format.format_write) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100694 ret = regcache_write(map, reg, val);
695 if (ret != 0)
696 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200697 if (map->cache_only) {
698 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100699 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200700 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100701 }
702
Mark Brownfb2736b2011-07-24 21:30:55 +0100703 trace_regmap_reg_write(map->dev, reg, val);
704
Mark Brownb83a3132011-05-11 19:59:58 +0200705 if (map->format.format_write) {
706 map->format.format_write(map, reg, val);
707
Mark Brownfb2736b2011-07-24 21:30:55 +0100708 trace_regmap_hw_write_start(map->dev, reg, 1);
709
Stephen Warren0135bbc2012-04-04 15:48:30 -0600710 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brownfb2736b2011-07-24 21:30:55 +0100711 map->format.buf_size);
712
713 trace_regmap_hw_write_done(map->dev, reg, 1);
714
715 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200716 } else {
Mark Brown82159ba2012-01-18 10:52:25 +0000717 map->format.format_val(map->work_buf + map->format.reg_bytes
Marc Reillyd939fb92012-03-16 12:11:43 +1100718 + map->format.pad_bytes, val, 0);
Mark Brownb83a3132011-05-11 19:59:58 +0200719 return _regmap_raw_write(map, reg,
Mark Brown82159ba2012-01-18 10:52:25 +0000720 map->work_buf +
721 map->format.reg_bytes +
722 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200723 map->format.val_bytes);
724 }
725}
726
727/**
728 * regmap_write(): Write a value to a single register
729 *
730 * @map: Register map to write to
731 * @reg: Register to write to
732 * @val: Value to be written
733 *
734 * A value of zero will be returned on success, a negative errno will
735 * be returned in error cases.
736 */
737int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
738{
739 int ret;
740
Stephen Warrenf01ee602012-04-09 13:40:24 -0600741 if (reg % map->reg_stride)
742 return -EINVAL;
743
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600744 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200745
746 ret = _regmap_write(map, reg, val);
747
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600748 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200749
750 return ret;
751}
752EXPORT_SYMBOL_GPL(regmap_write);
753
754/**
755 * regmap_raw_write(): Write raw values to one or more registers
756 *
757 * @map: Register map to write to
758 * @reg: Initial register to write to
759 * @val: Block of data to be written, laid out for direct transmission to the
760 * device
761 * @val_len: Length of data pointed to by val.
762 *
763 * This function is intended to be used for things like firmware
764 * download where a large block of data needs to be transferred to the
765 * device. No formatting will be done on the data provided.
766 *
767 * A value of zero will be returned on success, a negative errno will
768 * be returned in error cases.
769 */
770int regmap_raw_write(struct regmap *map, unsigned int reg,
771 const void *val, size_t val_len)
772{
773 int ret;
774
Stephen Warren851960b2012-04-06 15:16:03 -0600775 if (val_len % map->format.val_bytes)
776 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600777 if (reg % map->reg_stride)
778 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -0600779
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600780 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200781
782 ret = _regmap_raw_write(map, reg, val, val_len);
783
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600784 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200785
786 return ret;
787}
788EXPORT_SYMBOL_GPL(regmap_raw_write);
789
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530790/*
791 * regmap_bulk_write(): Write multiple registers to the device
792 *
793 * @map: Register map to write to
794 * @reg: First register to be write from
795 * @val: Block of data to be written, in native register size for device
796 * @val_count: Number of registers to write
797 *
798 * This function is intended to be used for writing a large block of
799 * data to be device either in single transfer or multiple transfer.
800 *
801 * A value of zero will be returned on success, a negative errno will
802 * be returned in error cases.
803 */
804int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
805 size_t val_count)
806{
807 int ret = 0, i;
808 size_t val_bytes = map->format.val_bytes;
809 void *wval;
810
811 if (!map->format.parse_val)
812 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600813 if (reg % map->reg_stride)
814 return -EINVAL;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530815
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600816 map->lock(map);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530817
818 /* No formatting is require if val_byte is 1 */
819 if (val_bytes == 1) {
820 wval = (void *)val;
821 } else {
822 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
823 if (!wval) {
824 ret = -ENOMEM;
825 dev_err(map->dev, "Error in memory allocation\n");
826 goto out;
827 }
828 for (i = 0; i < val_count * val_bytes; i += val_bytes)
829 map->format.parse_val(wval + i);
830 }
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100831 /*
832 * Some devices does not support bulk write, for
833 * them we have a series of single write operations.
834 */
835 if (map->use_single_rw) {
836 for (i = 0; i < val_count; i++) {
837 ret = regmap_raw_write(map,
838 reg + (i * map->reg_stride),
839 val + (i * val_bytes),
840 val_bytes);
841 if (ret != 0)
842 return ret;
843 }
844 } else {
845 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
846 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530847
848 if (val_bytes != 1)
849 kfree(wval);
850
851out:
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600852 map->unlock(map);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530853 return ret;
854}
855EXPORT_SYMBOL_GPL(regmap_bulk_write);
856
Mark Brownb83a3132011-05-11 19:59:58 +0200857static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
858 unsigned int val_len)
859{
860 u8 *u8 = map->work_buf;
861 int ret;
862
Marc Reillyd939fb92012-03-16 12:11:43 +1100863 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200864
865 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200866 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +0200867 * register addresss; since it's always the high bits for all
868 * current formats we can do this here rather than in
869 * formatting. This may break if we get interesting formats.
870 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200871 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200872
Mark Brownfb2736b2011-07-24 21:30:55 +0100873 trace_regmap_hw_read_start(map->dev, reg,
874 val_len / map->format.val_bytes);
875
Stephen Warren0135bbc2012-04-04 15:48:30 -0600876 ret = map->bus->read(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000877 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +0100878 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200879
Mark Brownfb2736b2011-07-24 21:30:55 +0100880 trace_regmap_hw_read_done(map->dev, reg,
881 val_len / map->format.val_bytes);
882
883 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200884}
885
886static int _regmap_read(struct regmap *map, unsigned int reg,
887 unsigned int *val)
888{
889 int ret;
890
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100891 if (!map->cache_bypass) {
892 ret = regcache_read(map, reg, val);
893 if (ret == 0)
894 return 0;
895 }
896
Lars-Peter Clausen19254412011-11-16 16:28:19 +0100897 if (!map->format.parse_val)
898 return -EINVAL;
899
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100900 if (map->cache_only)
901 return -EBUSY;
902
Mark Brownb83a3132011-05-11 19:59:58 +0200903 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +0100904 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +0200905 *val = map->format.parse_val(map->work_buf);
Mark Brownfb2736b2011-07-24 21:30:55 +0100906 trace_regmap_reg_read(map->dev, reg, *val);
907 }
Mark Brownb83a3132011-05-11 19:59:58 +0200908
Mark Brownf2985362012-04-30 21:25:05 +0100909 if (ret == 0 && !map->cache_bypass)
910 regcache_write(map, reg, *val);
911
Mark Brownb83a3132011-05-11 19:59:58 +0200912 return ret;
913}
914
915/**
916 * regmap_read(): Read a value from a single register
917 *
918 * @map: Register map to write to
919 * @reg: Register to be read from
920 * @val: Pointer to store read value
921 *
922 * A value of zero will be returned on success, a negative errno will
923 * be returned in error cases.
924 */
925int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
926{
927 int ret;
928
Stephen Warrenf01ee602012-04-09 13:40:24 -0600929 if (reg % map->reg_stride)
930 return -EINVAL;
931
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600932 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200933
934 ret = _regmap_read(map, reg, val);
935
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600936 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200937
938 return ret;
939}
940EXPORT_SYMBOL_GPL(regmap_read);
941
942/**
943 * regmap_raw_read(): Read raw data from the device
944 *
945 * @map: Register map to write to
946 * @reg: First register to be read from
947 * @val: Pointer to store read value
948 * @val_len: Size of data to read
949 *
950 * A value of zero will be returned on success, a negative errno will
951 * be returned in error cases.
952 */
953int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
954 size_t val_len)
955{
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000956 size_t val_bytes = map->format.val_bytes;
957 size_t val_count = val_len / val_bytes;
958 unsigned int v;
959 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +0100960
Stephen Warren851960b2012-04-06 15:16:03 -0600961 if (val_len % map->format.val_bytes)
962 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600963 if (reg % map->reg_stride)
964 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -0600965
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600966 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200967
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000968 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
969 map->cache_type == REGCACHE_NONE) {
970 /* Physical block read if there's no cache involved */
971 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200972
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000973 } else {
974 /* Otherwise go word by word for the cache; should be low
975 * cost as we expect to hit the cache.
976 */
977 for (i = 0; i < val_count; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600978 ret = _regmap_read(map, reg + (i * map->reg_stride),
979 &v);
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000980 if (ret != 0)
981 goto out;
982
Marc Reillyd939fb92012-03-16 12:11:43 +1100983 map->format.format_val(val + (i * val_bytes), v, 0);
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000984 }
985 }
986
987 out:
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600988 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200989
990 return ret;
991}
992EXPORT_SYMBOL_GPL(regmap_raw_read);
993
994/**
995 * regmap_bulk_read(): Read multiple registers from the device
996 *
997 * @map: Register map to write to
998 * @reg: First register to be read from
999 * @val: Pointer to store read value, in native register size for device
1000 * @val_count: Number of registers to read
1001 *
1002 * A value of zero will be returned on success, a negative errno will
1003 * be returned in error cases.
1004 */
1005int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1006 size_t val_count)
1007{
1008 int ret, i;
1009 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +01001010 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001011
Mark Brownb83a3132011-05-11 19:59:58 +02001012 if (!map->format.parse_val)
1013 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001014 if (reg % map->reg_stride)
1015 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001016
Mark Brownde2d8082011-10-10 13:24:52 +01001017 if (vol || map->cache_type == REGCACHE_NONE) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001018 /*
1019 * Some devices does not support bulk read, for
1020 * them we have a series of single read operations.
1021 */
1022 if (map->use_single_rw) {
1023 for (i = 0; i < val_count; i++) {
1024 ret = regmap_raw_read(map,
1025 reg + (i * map->reg_stride),
1026 val + (i * val_bytes),
1027 val_bytes);
1028 if (ret != 0)
1029 return ret;
1030 }
1031 } else {
1032 ret = regmap_raw_read(map, reg, val,
1033 val_bytes * val_count);
1034 if (ret != 0)
1035 return ret;
1036 }
Mark Brownde2d8082011-10-10 13:24:52 +01001037
1038 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1039 map->format.parse_val(val + i);
1040 } else {
1041 for (i = 0; i < val_count; i++) {
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301042 unsigned int ival;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001043 ret = regmap_read(map, reg + (i * map->reg_stride),
Mark Brown25061d22012-05-12 13:06:08 +01001044 &ival);
Mark Brownde2d8082011-10-10 13:24:52 +01001045 if (ret != 0)
1046 return ret;
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301047 memcpy(val + (i * val_bytes), &ival, val_bytes);
Mark Brownde2d8082011-10-10 13:24:52 +01001048 }
1049 }
Mark Brownb83a3132011-05-11 19:59:58 +02001050
1051 return 0;
1052}
1053EXPORT_SYMBOL_GPL(regmap_bulk_read);
1054
Mark Brown018690d2011-11-29 20:10:36 +00001055static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1056 unsigned int mask, unsigned int val,
1057 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +02001058{
1059 int ret;
Mark Brownd91e8db22011-11-18 16:03:50 +00001060 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +02001061
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001062 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001063
Mark Brownd91e8db22011-11-18 16:03:50 +00001064 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +02001065 if (ret != 0)
1066 goto out;
1067
Mark Brownd91e8db22011-11-18 16:03:50 +00001068 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001069 tmp |= val & mask;
1070
Mark Brown018690d2011-11-29 20:10:36 +00001071 if (tmp != orig) {
Mark Brownd91e8db22011-11-18 16:03:50 +00001072 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +00001073 *change = true;
1074 } else {
1075 *change = false;
1076 }
Mark Brownb83a3132011-05-11 19:59:58 +02001077
1078out:
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001079 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001080
1081 return ret;
1082}
Mark Brown018690d2011-11-29 20:10:36 +00001083
1084/**
1085 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1086 *
1087 * @map: Register map to update
1088 * @reg: Register to update
1089 * @mask: Bitmask to change
1090 * @val: New value for bitmask
1091 *
1092 * Returns zero for success, a negative number on error.
1093 */
1094int regmap_update_bits(struct regmap *map, unsigned int reg,
1095 unsigned int mask, unsigned int val)
1096{
1097 bool change;
1098 return _regmap_update_bits(map, reg, mask, val, &change);
1099}
Mark Brownb83a3132011-05-11 19:59:58 +02001100EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +01001101
Mark Brown018690d2011-11-29 20:10:36 +00001102/**
1103 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1104 * register map and report if updated
1105 *
1106 * @map: Register map to update
1107 * @reg: Register to update
1108 * @mask: Bitmask to change
1109 * @val: New value for bitmask
1110 * @change: Boolean indicating if a write was done
1111 *
1112 * Returns zero for success, a negative number on error.
1113 */
1114int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1115 unsigned int mask, unsigned int val,
1116 bool *change)
1117{
1118 return _regmap_update_bits(map, reg, mask, val, change);
1119}
1120EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1121
Mark Brown22f0d902012-01-21 12:01:14 +00001122/**
1123 * regmap_register_patch: Register and apply register updates to be applied
1124 * on device initialistion
1125 *
1126 * @map: Register map to apply updates to.
1127 * @regs: Values to update.
1128 * @num_regs: Number of entries in regs.
1129 *
1130 * Register a set of register updates to be applied to the device
1131 * whenever the device registers are synchronised with the cache and
1132 * apply them immediately. Typically this is used to apply
1133 * corrections to be applied to the device defaults on startup, such
1134 * as the updates some vendors provide to undocumented registers.
1135 */
1136int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1137 int num_regs)
1138{
1139 int i, ret;
1140 bool bypass;
1141
1142 /* If needed the implementation can be extended to support this */
1143 if (map->patch)
1144 return -EBUSY;
1145
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001146 map->lock(map);
Mark Brown22f0d902012-01-21 12:01:14 +00001147
1148 bypass = map->cache_bypass;
1149
1150 map->cache_bypass = true;
1151
1152 /* Write out first; it's useful to apply even if we fail later. */
1153 for (i = 0; i < num_regs; i++) {
1154 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1155 if (ret != 0) {
1156 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1157 regs[i].reg, regs[i].def, ret);
1158 goto out;
1159 }
1160 }
1161
Axel Lin2a14d7d92012-02-10 19:29:55 +08001162 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
Mark Brown22f0d902012-01-21 12:01:14 +00001163 if (map->patch != NULL) {
1164 memcpy(map->patch, regs,
1165 num_regs * sizeof(struct reg_default));
1166 map->patch_regs = num_regs;
1167 } else {
1168 ret = -ENOMEM;
1169 }
1170
1171out:
1172 map->cache_bypass = bypass;
1173
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001174 map->unlock(map);
Mark Brown22f0d902012-01-21 12:01:14 +00001175
1176 return ret;
1177}
1178EXPORT_SYMBOL_GPL(regmap_register_patch);
1179
Mark Browneae4b512012-03-14 13:15:03 +00001180/*
Mark Browna6539c32012-02-17 14:20:14 -08001181 * regmap_get_val_bytes(): Report the size of a register value
1182 *
1183 * Report the size of a register value, mainly intended to for use by
1184 * generic infrastructure built on top of regmap.
1185 */
1186int regmap_get_val_bytes(struct regmap *map)
1187{
1188 if (map->format.format_write)
1189 return -EINVAL;
1190
1191 return map->format.val_bytes;
1192}
1193EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1194
Mark Brown31244e32011-07-20 22:56:53 +01001195static int __init regmap_initcall(void)
1196{
1197 regmap_debugfs_initcall();
1198
1199 return 0;
1200}
1201postcore_initcall(regmap_initcall);