2 * Register map access API
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/regmap.h>
23 bool regmap_writeable(struct regmap *map, unsigned int reg)
25 if (map->max_register && reg > map->max_register)
28 if (map->writeable_reg)
29 return map->writeable_reg(map->dev, reg);
34 bool regmap_readable(struct regmap *map, unsigned int reg)
36 if (map->max_register && reg > map->max_register)
39 if (map->readable_reg)
40 return map->readable_reg(map->dev, reg);
45 bool regmap_volatile(struct regmap *map, unsigned int reg)
47 if (map->max_register && reg > map->max_register)
50 if (map->volatile_reg)
51 return map->volatile_reg(map->dev, reg);
56 bool regmap_precious(struct regmap *map, unsigned int reg)
58 if (map->max_register && reg > map->max_register)
61 if (map->precious_reg)
62 return map->precious_reg(map->dev, reg);
67 static void regmap_format_4_12_write(struct regmap *map,
68 unsigned int reg, unsigned int val)
70 __be16 *out = map->work_buf;
71 *out = cpu_to_be16((reg << 12) | val);
74 static void regmap_format_7_9_write(struct regmap *map,
75 unsigned int reg, unsigned int val)
77 __be16 *out = map->work_buf;
78 *out = cpu_to_be16((reg << 9) | val);
81 static void regmap_format_8(void *buf, unsigned int val)
88 static void regmap_format_16(void *buf, unsigned int val)
92 b[0] = cpu_to_be16(val);
95 static unsigned int regmap_parse_8(void *buf)
102 static unsigned int regmap_parse_16(void *buf)
106 b[0] = be16_to_cpu(b[0]);
112 * regmap_init(): Initialise register map
114 * @dev: Device that will be interacted with
115 * @bus: Bus-specific callbacks to use with device
116 * @config: Configuration for register map
118 * The return value will be an ERR_PTR() on error or a valid pointer to
119 * a struct regmap. This function should generally not be called
120 * directly, it should be called by bus-specific init functions.
122 struct regmap *regmap_init(struct device *dev,
123 const struct regmap_bus *bus,
124 const struct regmap_config *config)
132 map = kzalloc(sizeof(*map), GFP_KERNEL);
138 mutex_init(&map->lock);
139 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
140 map->format.reg_bytes = config->reg_bits / 8;
141 map->format.val_bytes = config->val_bits / 8;
144 map->max_register = config->max_register;
145 map->writeable_reg = config->writeable_reg;
146 map->readable_reg = config->readable_reg;
147 map->volatile_reg = config->volatile_reg;
148 map->precious_reg = config->precious_reg;
149 map->cache_type = config->cache_type;
151 if (config->read_flag_mask || config->write_flag_mask) {
152 map->read_flag_mask = config->read_flag_mask;
153 map->write_flag_mask = config->write_flag_mask;
155 map->read_flag_mask = bus->read_flag_mask;
158 switch (config->reg_bits) {
160 switch (config->val_bits) {
162 map->format.format_write = regmap_format_4_12_write;
170 switch (config->val_bits) {
172 map->format.format_write = regmap_format_7_9_write;
180 map->format.format_reg = regmap_format_8;
184 map->format.format_reg = regmap_format_16;
191 switch (config->val_bits) {
193 map->format.format_val = regmap_format_8;
194 map->format.parse_val = regmap_parse_8;
197 map->format.format_val = regmap_format_16;
198 map->format.parse_val = regmap_parse_16;
202 if (!map->format.format_write &&
203 !(map->format.format_reg && map->format.format_val))
206 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
207 if (map->work_buf == NULL) {
212 ret = regcache_init(map, config);
216 regmap_debugfs_init(map);
225 EXPORT_SYMBOL_GPL(regmap_init);
228 * regmap_reinit_cache(): Reinitialise the current register cache
230 * @map: Register map to operate on.
231 * @config: New configuration. Only the cache data will be used.
233 * Discard any existing register cache for the map and initialize a
234 * new cache. This can be used to restore the cache to defaults or to
235 * update the cache configuration to reflect runtime discovery of the
238 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
242 mutex_lock(&map->lock);
246 map->max_register = config->max_register;
247 map->writeable_reg = config->writeable_reg;
248 map->readable_reg = config->readable_reg;
249 map->volatile_reg = config->volatile_reg;
250 map->precious_reg = config->precious_reg;
251 map->cache_type = config->cache_type;
253 ret = regcache_init(map, config);
255 mutex_unlock(&map->lock);
261 * regmap_exit(): Free a previously allocated register map
263 void regmap_exit(struct regmap *map)
266 regmap_debugfs_exit(map);
267 kfree(map->work_buf);
270 EXPORT_SYMBOL_GPL(regmap_exit);
272 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
273 const void *val, size_t val_len)
275 u8 *u8 = map->work_buf;
281 /* Check for unwritable registers before we start */
282 if (map->writeable_reg)
283 for (i = 0; i < val_len / map->format.val_bytes; i++)
284 if (!map->writeable_reg(map->dev, reg + i))
287 map->format.format_reg(map->work_buf, reg);
289 u8[0] |= map->write_flag_mask;
291 trace_regmap_hw_write_start(map->dev, reg,
292 val_len / map->format.val_bytes);
294 /* If we're doing a single register write we can probably just
295 * send the work_buf directly, otherwise try to do a gather
298 if (val == map->work_buf + map->format.reg_bytes)
299 ret = map->bus->write(map->dev, map->work_buf,
300 map->format.reg_bytes + val_len);
301 else if (map->bus->gather_write)
302 ret = map->bus->gather_write(map->dev, map->work_buf,
303 map->format.reg_bytes,
306 /* If that didn't work fall back on linearising by hand. */
307 if (ret == -ENOTSUPP) {
308 len = map->format.reg_bytes + val_len;
309 buf = kmalloc(len, GFP_KERNEL);
313 memcpy(buf, map->work_buf, map->format.reg_bytes);
314 memcpy(buf + map->format.reg_bytes, val, val_len);
315 ret = map->bus->write(map->dev, buf, len);
320 trace_regmap_hw_write_done(map->dev, reg,
321 val_len / map->format.val_bytes);
326 int _regmap_write(struct regmap *map, unsigned int reg,
330 BUG_ON(!map->format.format_write && !map->format.format_val);
332 if (!map->cache_bypass) {
333 ret = regcache_write(map, reg, val);
336 if (map->cache_only) {
337 map->cache_dirty = true;
342 trace_regmap_reg_write(map->dev, reg, val);
344 if (map->format.format_write) {
345 map->format.format_write(map, reg, val);
347 trace_regmap_hw_write_start(map->dev, reg, 1);
349 ret = map->bus->write(map->dev, map->work_buf,
350 map->format.buf_size);
352 trace_regmap_hw_write_done(map->dev, reg, 1);
356 map->format.format_val(map->work_buf + map->format.reg_bytes,
358 return _regmap_raw_write(map, reg,
359 map->work_buf + map->format.reg_bytes,
360 map->format.val_bytes);
365 * regmap_write(): Write a value to a single register
367 * @map: Register map to write to
368 * @reg: Register to write to
369 * @val: Value to be written
371 * A value of zero will be returned on success, a negative errno will
372 * be returned in error cases.
374 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
378 mutex_lock(&map->lock);
380 ret = _regmap_write(map, reg, val);
382 mutex_unlock(&map->lock);
386 EXPORT_SYMBOL_GPL(regmap_write);
389 * regmap_raw_write(): Write raw values to one or more registers
391 * @map: Register map to write to
392 * @reg: Initial register to write to
393 * @val: Block of data to be written, laid out for direct transmission to the
395 * @val_len: Length of data pointed to by val.
397 * This function is intended to be used for things like firmware
398 * download where a large block of data needs to be transferred to the
399 * device. No formatting will be done on the data provided.
401 * A value of zero will be returned on success, a negative errno will
402 * be returned in error cases.
404 int regmap_raw_write(struct regmap *map, unsigned int reg,
405 const void *val, size_t val_len)
409 WARN_ON(map->cache_type != REGCACHE_NONE);
411 mutex_lock(&map->lock);
413 ret = _regmap_raw_write(map, reg, val, val_len);
415 mutex_unlock(&map->lock);
419 EXPORT_SYMBOL_GPL(regmap_raw_write);
421 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
422 unsigned int val_len)
424 u8 *u8 = map->work_buf;
427 map->format.format_reg(map->work_buf, reg);
430 * Some buses or devices flag reads by setting the high bits in the
431 * register addresss; since it's always the high bits for all
432 * current formats we can do this here rather than in
433 * formatting. This may break if we get interesting formats.
435 u8[0] |= map->read_flag_mask;
437 trace_regmap_hw_read_start(map->dev, reg,
438 val_len / map->format.val_bytes);
440 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
443 trace_regmap_hw_read_done(map->dev, reg,
444 val_len / map->format.val_bytes);
449 static int _regmap_read(struct regmap *map, unsigned int reg,
454 if (!map->format.parse_val)
457 if (!map->cache_bypass) {
458 ret = regcache_read(map, reg, val);
466 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
468 *val = map->format.parse_val(map->work_buf);
469 trace_regmap_reg_read(map->dev, reg, *val);
476 * regmap_read(): Read a value from a single register
478 * @map: Register map to write to
479 * @reg: Register to be read from
480 * @val: Pointer to store read value
482 * A value of zero will be returned on success, a negative errno will
483 * be returned in error cases.
485 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
489 mutex_lock(&map->lock);
491 ret = _regmap_read(map, reg, val);
493 mutex_unlock(&map->lock);
497 EXPORT_SYMBOL_GPL(regmap_read);
500 * regmap_raw_read(): Read raw data from the device
502 * @map: Register map to write to
503 * @reg: First register to be read from
504 * @val: Pointer to store read value
505 * @val_len: Size of data to read
507 * A value of zero will be returned on success, a negative errno will
508 * be returned in error cases.
510 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
517 for (i = 0; i < val_len / map->format.val_bytes; i++)
518 if (!regmap_volatile(map, reg + i))
521 WARN_ON(!vol && map->cache_type != REGCACHE_NONE);
523 mutex_lock(&map->lock);
525 ret = _regmap_raw_read(map, reg, val, val_len);
527 mutex_unlock(&map->lock);
531 EXPORT_SYMBOL_GPL(regmap_raw_read);
534 * regmap_bulk_read(): Read multiple registers from the device
536 * @map: Register map to write to
537 * @reg: First register to be read from
538 * @val: Pointer to store read value, in native register size for device
539 * @val_count: Number of registers to read
541 * A value of zero will be returned on success, a negative errno will
542 * be returned in error cases.
544 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
548 size_t val_bytes = map->format.val_bytes;
551 if (!map->format.parse_val)
554 /* Is this a block of volatile registers? */
555 for (i = 0; i < val_count; i++)
556 if (!regmap_volatile(map, reg + i))
559 if (vol || map->cache_type == REGCACHE_NONE) {
560 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
564 for (i = 0; i < val_count * val_bytes; i += val_bytes)
565 map->format.parse_val(val + i);
567 for (i = 0; i < val_count; i++) {
568 ret = regmap_read(map, reg + i, val + (i * val_bytes));
576 EXPORT_SYMBOL_GPL(regmap_bulk_read);
579 * remap_update_bits: Perform a read/modify/write cycle on the register map
581 * @map: Register map to update
582 * @reg: Register to update
583 * @mask: Bitmask to change
584 * @val: New value for bitmask
586 * Returns zero for success, a negative number on error.
588 int regmap_update_bits(struct regmap *map, unsigned int reg,
589 unsigned int mask, unsigned int val)
594 mutex_lock(&map->lock);
596 ret = _regmap_read(map, reg, &tmp);
603 ret = _regmap_write(map, reg, tmp);
606 mutex_unlock(&map->lock);
610 EXPORT_SYMBOL_GPL(regmap_update_bits);
612 static int __init regmap_initcall(void)
614 regmap_debugfs_initcall();
618 postcore_initcall(regmap_initcall);