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 #include <linux/regmap.h>
22 struct regmap_format {
26 void (*format_write)(struct regmap *map,
27 unsigned int reg, unsigned int val);
28 void (*format_reg)(void *buf, unsigned int reg);
29 void (*format_val)(void *buf, unsigned int val);
30 unsigned int (*parse_val)(void *buf);
36 struct device *dev; /* Device we do I/O on */
37 void *work_buf; /* Scratch buffer used to format I/O */
38 struct regmap_format format; /* Buffer format */
39 const struct regmap_bus *bus;
41 unsigned int max_register;
42 bool (*writeable_reg)(struct device *dev, unsigned int reg);
43 bool (*readable_reg)(struct device *dev, unsigned int reg);
44 bool (*volatile_reg)(struct device *dev, unsigned int reg);
47 static void regmap_format_4_12_write(struct regmap *map,
48 unsigned int reg, unsigned int val)
50 __be16 *out = map->work_buf;
51 *out = cpu_to_be16((reg << 12) | val);
54 static void regmap_format_7_9_write(struct regmap *map,
55 unsigned int reg, unsigned int val)
57 __be16 *out = map->work_buf;
58 *out = cpu_to_be16((reg << 9) | val);
61 static void regmap_format_8(void *buf, unsigned int val)
68 static void regmap_format_16(void *buf, unsigned int val)
72 b[0] = cpu_to_be16(val);
75 static unsigned int regmap_parse_8(void *buf)
82 static unsigned int regmap_parse_16(void *buf)
86 b[0] = be16_to_cpu(b[0]);
92 * regmap_init(): Initialise register map
94 * @dev: Device that will be interacted with
95 * @bus: Bus-specific callbacks to use with device
96 * @config: Configuration for register map
98 * The return value will be an ERR_PTR() on error or a valid pointer to
99 * a struct regmap. This function should generally not be called
100 * directly, it should be called by bus-specific init functions.
102 struct regmap *regmap_init(struct device *dev,
103 const struct regmap_bus *bus,
104 const struct regmap_config *config)
112 map = kzalloc(sizeof(*map), GFP_KERNEL);
118 mutex_init(&map->lock);
119 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
120 map->format.reg_bytes = config->reg_bits / 8;
121 map->format.val_bytes = config->val_bits / 8;
124 map->max_register = config->max_register;
125 map->writeable_reg = config->writeable_reg;
126 map->readable_reg = config->readable_reg;
127 map->volatile_reg = config->volatile_reg;
129 switch (config->reg_bits) {
131 switch (config->val_bits) {
133 map->format.format_write = regmap_format_4_12_write;
141 switch (config->val_bits) {
143 map->format.format_write = regmap_format_7_9_write;
151 map->format.format_reg = regmap_format_8;
155 map->format.format_reg = regmap_format_16;
162 switch (config->val_bits) {
164 map->format.format_val = regmap_format_8;
165 map->format.parse_val = regmap_parse_8;
168 map->format.format_val = regmap_format_16;
169 map->format.parse_val = regmap_parse_16;
173 if (!map->format.format_write &&
174 !(map->format.format_reg && map->format.format_val))
177 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
178 if (map->work_buf == NULL) {
186 module_put(map->bus->owner);
192 EXPORT_SYMBOL_GPL(regmap_init);
195 * regmap_exit(): Free a previously allocated register map
197 void regmap_exit(struct regmap *map)
199 kfree(map->work_buf);
200 module_put(map->bus->owner);
203 EXPORT_SYMBOL_GPL(regmap_exit);
205 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
206 const void *val, size_t val_len)
212 map->format.format_reg(map->work_buf, reg);
214 /* If we're doing a single register write we can probably just
215 * send the work_buf directly, otherwise try to do a gather
218 if (val == map->work_buf + map->format.reg_bytes)
219 ret = map->bus->write(map->dev, map->work_buf,
220 map->format.reg_bytes + val_len);
221 else if (map->bus->gather_write)
222 ret = map->bus->gather_write(map->dev, map->work_buf,
223 map->format.reg_bytes,
226 /* If that didn't work fall back on linearising by hand. */
227 if (ret == -ENOTSUPP) {
228 len = map->format.reg_bytes + val_len;
229 buf = kmalloc(len, GFP_KERNEL);
233 memcpy(buf, map->work_buf, map->format.reg_bytes);
234 memcpy(buf + map->format.reg_bytes, val, val_len);
235 ret = map->bus->write(map->dev, buf, len);
243 static int _regmap_write(struct regmap *map, unsigned int reg,
246 BUG_ON(!map->format.format_write && !map->format.format_val);
248 if (map->format.format_write) {
249 map->format.format_write(map, reg, val);
251 return map->bus->write(map->dev, map->work_buf,
252 map->format.buf_size);
254 map->format.format_val(map->work_buf + map->format.reg_bytes,
256 return _regmap_raw_write(map, reg,
257 map->work_buf + map->format.reg_bytes,
258 map->format.val_bytes);
263 * regmap_write(): Write a value to a single register
265 * @map: Register map to write to
266 * @reg: Register to write to
267 * @val: Value to be written
269 * A value of zero will be returned on success, a negative errno will
270 * be returned in error cases.
272 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
276 mutex_lock(&map->lock);
278 ret = _regmap_write(map, reg, val);
280 mutex_unlock(&map->lock);
284 EXPORT_SYMBOL_GPL(regmap_write);
287 * regmap_raw_write(): Write raw values to one or more registers
289 * @map: Register map to write to
290 * @reg: Initial register to write to
291 * @val: Block of data to be written, laid out for direct transmission to the
293 * @val_len: Length of data pointed to by val.
295 * This function is intended to be used for things like firmware
296 * download where a large block of data needs to be transferred to the
297 * device. No formatting will be done on the data provided.
299 * A value of zero will be returned on success, a negative errno will
300 * be returned in error cases.
302 int regmap_raw_write(struct regmap *map, unsigned int reg,
303 const void *val, size_t val_len)
307 mutex_lock(&map->lock);
309 ret = _regmap_raw_write(map, reg, val, val_len);
311 mutex_unlock(&map->lock);
315 EXPORT_SYMBOL_GPL(regmap_raw_write);
317 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
318 unsigned int val_len)
320 u8 *u8 = map->work_buf;
323 map->format.format_reg(map->work_buf, reg);
326 * Some buses flag reads by setting the high bits in the
327 * register addresss; since it's always the high bits for all
328 * current formats we can do this here rather than in
329 * formatting. This may break if we get interesting formats.
331 if (map->bus->read_flag_mask)
332 u8[0] |= map->bus->read_flag_mask;
334 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
342 static int _regmap_read(struct regmap *map, unsigned int reg,
347 if (!map->format.parse_val)
350 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
352 *val = map->format.parse_val(map->work_buf);
358 * regmap_read(): Read a value from a single register
360 * @map: Register map to write to
361 * @reg: Register to be read from
362 * @val: Pointer to store read value
364 * A value of zero will be returned on success, a negative errno will
365 * be returned in error cases.
367 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
371 mutex_lock(&map->lock);
373 ret = _regmap_read(map, reg, val);
375 mutex_unlock(&map->lock);
379 EXPORT_SYMBOL_GPL(regmap_read);
382 * regmap_raw_read(): Read raw data from the device
384 * @map: Register map to write to
385 * @reg: First register to be read from
386 * @val: Pointer to store read value
387 * @val_len: Size of data to read
389 * A value of zero will be returned on success, a negative errno will
390 * be returned in error cases.
392 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
397 mutex_lock(&map->lock);
399 ret = _regmap_raw_read(map, reg, val, val_len);
401 mutex_unlock(&map->lock);
405 EXPORT_SYMBOL_GPL(regmap_raw_read);
408 * regmap_bulk_read(): Read multiple registers from the device
410 * @map: Register map to write to
411 * @reg: First register to be read from
412 * @val: Pointer to store read value, in native register size for device
413 * @val_count: Number of registers to read
415 * A value of zero will be returned on success, a negative errno will
416 * be returned in error cases.
418 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
422 size_t val_bytes = map->format.val_bytes;
424 if (!map->format.parse_val)
427 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
431 for (i = 0; i < val_count * val_bytes; i += val_bytes)
432 map->format.parse_val(val + i);
436 EXPORT_SYMBOL_GPL(regmap_bulk_read);
439 * remap_update_bits: Perform a read/modify/write cycle on the register map
441 * @map: Register map to update
442 * @reg: Register to update
443 * @mask: Bitmask to change
444 * @val: New value for bitmask
446 * Returns zero for success, a negative number on error.
448 int regmap_update_bits(struct regmap *map, unsigned int reg,
449 unsigned int mask, unsigned int val)
454 mutex_lock(&map->lock);
456 ret = _regmap_read(map, reg, &tmp);
463 ret = _regmap_write(map, reg, tmp);
466 mutex_unlock(&map->lock);
470 EXPORT_SYMBOL_GPL(regmap_update_bits);