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;
150 map->reg_defaults = config->reg_defaults;
151 map->num_reg_defaults = config->num_reg_defaults;
152 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
153 map->reg_defaults_raw = config->reg_defaults_raw;
154 map->cache_size_raw = (config->val_bits / 8) * config->num_reg_defaults_raw;
155 map->cache_word_size = config->val_bits / 8;
157 if (config->read_flag_mask || config->write_flag_mask) {
158 map->read_flag_mask = config->read_flag_mask;
159 map->write_flag_mask = config->write_flag_mask;
161 map->read_flag_mask = bus->read_flag_mask;
164 switch (config->reg_bits) {
166 switch (config->val_bits) {
168 map->format.format_write = regmap_format_4_12_write;
176 switch (config->val_bits) {
178 map->format.format_write = regmap_format_7_9_write;
186 map->format.format_reg = regmap_format_8;
190 map->format.format_reg = regmap_format_16;
197 switch (config->val_bits) {
199 map->format.format_val = regmap_format_8;
200 map->format.parse_val = regmap_parse_8;
203 map->format.format_val = regmap_format_16;
204 map->format.parse_val = regmap_parse_16;
208 if (!map->format.format_write &&
209 !(map->format.format_reg && map->format.format_val))
212 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
213 if (map->work_buf == NULL) {
218 ret = regcache_init(map);
222 regmap_debugfs_init(map);
231 EXPORT_SYMBOL_GPL(regmap_init);
234 * regmap_reinit_cache(): Reinitialise the current register cache
236 * @map: Register map to operate on.
237 * @config: New configuration. Only the cache data will be used.
239 * Discard any existing register cache for the map and initialize a
240 * new cache. This can be used to restore the cache to defaults or to
241 * update the cache configuration to reflect runtime discovery of the
244 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
248 mutex_lock(&map->lock);
252 map->max_register = config->max_register;
253 map->writeable_reg = config->writeable_reg;
254 map->readable_reg = config->readable_reg;
255 map->volatile_reg = config->volatile_reg;
256 map->precious_reg = config->precious_reg;
257 map->cache_type = config->cache_type;
259 ret = regcache_init(map, config);
261 mutex_unlock(&map->lock);
267 * regmap_exit(): Free a previously allocated register map
269 void regmap_exit(struct regmap *map)
272 regmap_debugfs_exit(map);
273 kfree(map->work_buf);
276 EXPORT_SYMBOL_GPL(regmap_exit);
278 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
279 const void *val, size_t val_len)
281 u8 *u8 = map->work_buf;
287 /* Check for unwritable registers before we start */
288 if (map->writeable_reg)
289 for (i = 0; i < val_len / map->format.val_bytes; i++)
290 if (!map->writeable_reg(map->dev, reg + i))
293 map->format.format_reg(map->work_buf, reg);
295 u8[0] |= map->write_flag_mask;
297 trace_regmap_hw_write_start(map->dev, reg,
298 val_len / map->format.val_bytes);
300 /* If we're doing a single register write we can probably just
301 * send the work_buf directly, otherwise try to do a gather
304 if (val == map->work_buf + map->format.reg_bytes)
305 ret = map->bus->write(map->dev, map->work_buf,
306 map->format.reg_bytes + val_len);
307 else if (map->bus->gather_write)
308 ret = map->bus->gather_write(map->dev, map->work_buf,
309 map->format.reg_bytes,
312 /* If that didn't work fall back on linearising by hand. */
313 if (ret == -ENOTSUPP) {
314 len = map->format.reg_bytes + val_len;
315 buf = kmalloc(len, GFP_KERNEL);
319 memcpy(buf, map->work_buf, map->format.reg_bytes);
320 memcpy(buf + map->format.reg_bytes, val, val_len);
321 ret = map->bus->write(map->dev, buf, len);
326 trace_regmap_hw_write_done(map->dev, reg,
327 val_len / map->format.val_bytes);
332 int _regmap_write(struct regmap *map, unsigned int reg,
336 BUG_ON(!map->format.format_write && !map->format.format_val);
338 if (!map->cache_bypass) {
339 ret = regcache_write(map, reg, val);
342 if (map->cache_only) {
343 map->cache_dirty = true;
348 trace_regmap_reg_write(map->dev, reg, val);
350 if (map->format.format_write) {
351 map->format.format_write(map, reg, val);
353 trace_regmap_hw_write_start(map->dev, reg, 1);
355 ret = map->bus->write(map->dev, map->work_buf,
356 map->format.buf_size);
358 trace_regmap_hw_write_done(map->dev, reg, 1);
362 map->format.format_val(map->work_buf + map->format.reg_bytes,
364 return _regmap_raw_write(map, reg,
365 map->work_buf + map->format.reg_bytes,
366 map->format.val_bytes);
371 * regmap_write(): Write a value to a single register
373 * @map: Register map to write to
374 * @reg: Register to write to
375 * @val: Value to be written
377 * A value of zero will be returned on success, a negative errno will
378 * be returned in error cases.
380 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
384 mutex_lock(&map->lock);
386 ret = _regmap_write(map, reg, val);
388 mutex_unlock(&map->lock);
392 EXPORT_SYMBOL_GPL(regmap_write);
395 * regmap_raw_write(): Write raw values to one or more registers
397 * @map: Register map to write to
398 * @reg: Initial register to write to
399 * @val: Block of data to be written, laid out for direct transmission to the
401 * @val_len: Length of data pointed to by val.
403 * This function is intended to be used for things like firmware
404 * download where a large block of data needs to be transferred to the
405 * device. No formatting will be done on the data provided.
407 * A value of zero will be returned on success, a negative errno will
408 * be returned in error cases.
410 int regmap_raw_write(struct regmap *map, unsigned int reg,
411 const void *val, size_t val_len)
415 WARN_ON(map->cache_type != REGCACHE_NONE);
417 mutex_lock(&map->lock);
419 ret = _regmap_raw_write(map, reg, val, val_len);
421 mutex_unlock(&map->lock);
425 EXPORT_SYMBOL_GPL(regmap_raw_write);
427 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
428 unsigned int val_len)
430 u8 *u8 = map->work_buf;
433 map->format.format_reg(map->work_buf, reg);
436 * Some buses or devices flag reads by setting the high bits in the
437 * register addresss; since it's always the high bits for all
438 * current formats we can do this here rather than in
439 * formatting. This may break if we get interesting formats.
441 u8[0] |= map->read_flag_mask;
443 trace_regmap_hw_read_start(map->dev, reg,
444 val_len / map->format.val_bytes);
446 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
449 trace_regmap_hw_read_done(map->dev, reg,
450 val_len / map->format.val_bytes);
455 static int _regmap_read(struct regmap *map, unsigned int reg,
460 if (!map->format.parse_val)
463 if (!map->cache_bypass) {
464 ret = regcache_read(map, reg, val);
472 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
474 *val = map->format.parse_val(map->work_buf);
475 trace_regmap_reg_read(map->dev, reg, *val);
482 * regmap_read(): Read a value from a single register
484 * @map: Register map to write to
485 * @reg: Register to be read from
486 * @val: Pointer to store read value
488 * A value of zero will be returned on success, a negative errno will
489 * be returned in error cases.
491 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
495 mutex_lock(&map->lock);
497 ret = _regmap_read(map, reg, val);
499 mutex_unlock(&map->lock);
503 EXPORT_SYMBOL_GPL(regmap_read);
506 * regmap_raw_read(): Read raw data from the device
508 * @map: Register map to write to
509 * @reg: First register to be read from
510 * @val: Pointer to store read value
511 * @val_len: Size of data to read
513 * A value of zero will be returned on success, a negative errno will
514 * be returned in error cases.
516 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
523 for (i = 0; i < val_len / map->format.val_bytes; i++)
524 if (!regmap_volatile(map, reg + i))
527 WARN_ON(!vol && map->cache_type != REGCACHE_NONE);
529 mutex_lock(&map->lock);
531 ret = _regmap_raw_read(map, reg, val, val_len);
533 mutex_unlock(&map->lock);
537 EXPORT_SYMBOL_GPL(regmap_raw_read);
540 * regmap_bulk_read(): Read multiple registers from the device
542 * @map: Register map to write to
543 * @reg: First register to be read from
544 * @val: Pointer to store read value, in native register size for device
545 * @val_count: Number of registers to read
547 * A value of zero will be returned on success, a negative errno will
548 * be returned in error cases.
550 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
554 size_t val_bytes = map->format.val_bytes;
557 if (!map->format.parse_val)
560 /* Is this a block of volatile registers? */
561 for (i = 0; i < val_count; i++)
562 if (!regmap_volatile(map, reg + i))
565 if (vol || map->cache_type == REGCACHE_NONE) {
566 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
570 for (i = 0; i < val_count * val_bytes; i += val_bytes)
571 map->format.parse_val(val + i);
573 for (i = 0; i < val_count; i++) {
574 ret = regmap_read(map, reg + i, val + (i * val_bytes));
582 EXPORT_SYMBOL_GPL(regmap_bulk_read);
585 * remap_update_bits: Perform a read/modify/write cycle on the register map
587 * @map: Register map to update
588 * @reg: Register to update
589 * @mask: Bitmask to change
590 * @val: New value for bitmask
592 * Returns zero for success, a negative number on error.
594 int regmap_update_bits(struct regmap *map, unsigned int reg,
595 unsigned int mask, unsigned int val)
600 mutex_lock(&map->lock);
602 ret = _regmap_read(map, reg, &tmp);
609 ret = _regmap_write(map, reg, tmp);
612 mutex_unlock(&map->lock);
616 EXPORT_SYMBOL_GPL(regmap_update_bits);
618 static int __init regmap_initcall(void)
620 regmap_debugfs_initcall();
624 postcore_initcall(regmap_initcall);