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;
150 switch (config->reg_bits) {
152 switch (config->val_bits) {
154 map->format.format_write = regmap_format_4_12_write;
162 switch (config->val_bits) {
164 map->format.format_write = regmap_format_7_9_write;
172 map->format.format_reg = regmap_format_8;
176 map->format.format_reg = regmap_format_16;
183 switch (config->val_bits) {
185 map->format.format_val = regmap_format_8;
186 map->format.parse_val = regmap_parse_8;
189 map->format.format_val = regmap_format_16;
190 map->format.parse_val = regmap_parse_16;
194 if (!map->format.format_write &&
195 !(map->format.format_reg && map->format.format_val))
198 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
199 if (map->work_buf == NULL) {
204 regmap_debugfs_init(map);
213 EXPORT_SYMBOL_GPL(regmap_init);
216 * regmap_exit(): Free a previously allocated register map
218 void regmap_exit(struct regmap *map)
220 regmap_debugfs_exit(map);
221 kfree(map->work_buf);
224 EXPORT_SYMBOL_GPL(regmap_exit);
226 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
227 const void *val, size_t val_len)
234 /* Check for unwritable registers before we start */
235 if (map->writeable_reg)
236 for (i = 0; i < val_len / map->format.val_bytes; i++)
237 if (!map->writeable_reg(map->dev, reg + i))
240 map->format.format_reg(map->work_buf, reg);
242 trace_regmap_hw_write_start(map->dev, reg,
243 val_len / map->format.val_bytes);
245 /* If we're doing a single register write we can probably just
246 * send the work_buf directly, otherwise try to do a gather
249 if (val == map->work_buf + map->format.reg_bytes)
250 ret = map->bus->write(map->dev, map->work_buf,
251 map->format.reg_bytes + val_len);
252 else if (map->bus->gather_write)
253 ret = map->bus->gather_write(map->dev, map->work_buf,
254 map->format.reg_bytes,
257 /* If that didn't work fall back on linearising by hand. */
258 if (ret == -ENOTSUPP) {
259 len = map->format.reg_bytes + val_len;
260 buf = kmalloc(len, GFP_KERNEL);
264 memcpy(buf, map->work_buf, map->format.reg_bytes);
265 memcpy(buf + map->format.reg_bytes, val, val_len);
266 ret = map->bus->write(map->dev, buf, len);
271 trace_regmap_hw_write_done(map->dev, reg,
272 val_len / map->format.val_bytes);
277 static int _regmap_write(struct regmap *map, unsigned int reg,
281 BUG_ON(!map->format.format_write && !map->format.format_val);
283 trace_regmap_reg_write(map->dev, reg, val);
285 if (map->format.format_write) {
286 map->format.format_write(map, reg, val);
288 trace_regmap_hw_write_start(map->dev, reg, 1);
290 ret = map->bus->write(map->dev, map->work_buf,
291 map->format.buf_size);
293 trace_regmap_hw_write_done(map->dev, reg, 1);
297 map->format.format_val(map->work_buf + map->format.reg_bytes,
299 return _regmap_raw_write(map, reg,
300 map->work_buf + map->format.reg_bytes,
301 map->format.val_bytes);
306 * regmap_write(): Write a value to a single register
308 * @map: Register map to write to
309 * @reg: Register to write to
310 * @val: Value to be written
312 * A value of zero will be returned on success, a negative errno will
313 * be returned in error cases.
315 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
319 mutex_lock(&map->lock);
321 ret = _regmap_write(map, reg, val);
323 mutex_unlock(&map->lock);
327 EXPORT_SYMBOL_GPL(regmap_write);
330 * regmap_raw_write(): Write raw values to one or more registers
332 * @map: Register map to write to
333 * @reg: Initial register to write to
334 * @val: Block of data to be written, laid out for direct transmission to the
336 * @val_len: Length of data pointed to by val.
338 * This function is intended to be used for things like firmware
339 * download where a large block of data needs to be transferred to the
340 * device. No formatting will be done on the data provided.
342 * A value of zero will be returned on success, a negative errno will
343 * be returned in error cases.
345 int regmap_raw_write(struct regmap *map, unsigned int reg,
346 const void *val, size_t val_len)
350 mutex_lock(&map->lock);
352 ret = _regmap_raw_write(map, reg, val, val_len);
354 mutex_unlock(&map->lock);
358 EXPORT_SYMBOL_GPL(regmap_raw_write);
360 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
361 unsigned int val_len)
363 u8 *u8 = map->work_buf;
366 map->format.format_reg(map->work_buf, reg);
369 * Some buses flag reads by setting the high bits in the
370 * register addresss; since it's always the high bits for all
371 * current formats we can do this here rather than in
372 * formatting. This may break if we get interesting formats.
374 if (map->bus->read_flag_mask)
375 u8[0] |= map->bus->read_flag_mask;
377 trace_regmap_hw_read_start(map->dev, reg,
378 val_len / map->format.val_bytes);
380 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
383 trace_regmap_hw_read_done(map->dev, reg,
384 val_len / map->format.val_bytes);
389 static int _regmap_read(struct regmap *map, unsigned int reg,
394 if (!map->format.parse_val)
397 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
399 *val = map->format.parse_val(map->work_buf);
400 trace_regmap_reg_read(map->dev, reg, *val);
407 * regmap_read(): Read a value from a single register
409 * @map: Register map to write to
410 * @reg: Register to be read from
411 * @val: Pointer to store read value
413 * A value of zero will be returned on success, a negative errno will
414 * be returned in error cases.
416 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
420 mutex_lock(&map->lock);
422 ret = _regmap_read(map, reg, val);
424 mutex_unlock(&map->lock);
428 EXPORT_SYMBOL_GPL(regmap_read);
431 * regmap_raw_read(): Read raw data from the device
433 * @map: Register map to write to
434 * @reg: First register to be read from
435 * @val: Pointer to store read value
436 * @val_len: Size of data to read
438 * A value of zero will be returned on success, a negative errno will
439 * be returned in error cases.
441 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
446 mutex_lock(&map->lock);
448 ret = _regmap_raw_read(map, reg, val, val_len);
450 mutex_unlock(&map->lock);
454 EXPORT_SYMBOL_GPL(regmap_raw_read);
457 * regmap_bulk_read(): Read multiple registers from the device
459 * @map: Register map to write to
460 * @reg: First register to be read from
461 * @val: Pointer to store read value, in native register size for device
462 * @val_count: Number of registers to read
464 * A value of zero will be returned on success, a negative errno will
465 * be returned in error cases.
467 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
471 size_t val_bytes = map->format.val_bytes;
473 if (!map->format.parse_val)
476 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
480 for (i = 0; i < val_count * val_bytes; i += val_bytes)
481 map->format.parse_val(val + i);
485 EXPORT_SYMBOL_GPL(regmap_bulk_read);
488 * remap_update_bits: Perform a read/modify/write cycle on the register map
490 * @map: Register map to update
491 * @reg: Register to update
492 * @mask: Bitmask to change
493 * @val: New value for bitmask
495 * Returns zero for success, a negative number on error.
497 int regmap_update_bits(struct regmap *map, unsigned int reg,
498 unsigned int mask, unsigned int val)
503 mutex_lock(&map->lock);
505 ret = _regmap_read(map, reg, &tmp);
512 ret = _regmap_write(map, reg, tmp);
515 mutex_unlock(&map->lock);
519 EXPORT_SYMBOL_GPL(regmap_update_bits);
521 static int __init regmap_initcall(void)
523 regmap_debugfs_initcall();
527 postcore_initcall(regmap_initcall);