regmap: Add basic tracepoints
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / regmap / regmap.c
blob7d4dc11ad86cf9db6d1a5abbc4576ceb037d3cc2
1 /*
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>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/regmap.h>
23 struct regmap;
25 struct regmap_format {
26 size_t buf_size;
27 size_t reg_bytes;
28 size_t val_bytes;
29 void (*format_write)(struct regmap *map,
30 unsigned int reg, unsigned int val);
31 void (*format_reg)(void *buf, unsigned int reg);
32 void (*format_val)(void *buf, unsigned int val);
33 unsigned int (*parse_val)(void *buf);
36 struct regmap {
37 struct mutex lock;
39 struct device *dev; /* Device we do I/O on */
40 void *work_buf; /* Scratch buffer used to format I/O */
41 struct regmap_format format; /* Buffer format */
42 const struct regmap_bus *bus;
44 unsigned int max_register;
45 bool (*writeable_reg)(struct device *dev, unsigned int reg);
46 bool (*readable_reg)(struct device *dev, unsigned int reg);
47 bool (*volatile_reg)(struct device *dev, unsigned int reg);
50 static void regmap_format_4_12_write(struct regmap *map,
51 unsigned int reg, unsigned int val)
53 __be16 *out = map->work_buf;
54 *out = cpu_to_be16((reg << 12) | val);
57 static void regmap_format_7_9_write(struct regmap *map,
58 unsigned int reg, unsigned int val)
60 __be16 *out = map->work_buf;
61 *out = cpu_to_be16((reg << 9) | val);
64 static void regmap_format_8(void *buf, unsigned int val)
66 u8 *b = buf;
68 b[0] = val;
71 static void regmap_format_16(void *buf, unsigned int val)
73 __be16 *b = buf;
75 b[0] = cpu_to_be16(val);
78 static unsigned int regmap_parse_8(void *buf)
80 u8 *b = buf;
82 return b[0];
85 static unsigned int regmap_parse_16(void *buf)
87 __be16 *b = buf;
89 b[0] = be16_to_cpu(b[0]);
91 return b[0];
94 /**
95 * regmap_init(): Initialise register map
97 * @dev: Device that will be interacted with
98 * @bus: Bus-specific callbacks to use with device
99 * @config: Configuration for register map
101 * The return value will be an ERR_PTR() on error or a valid pointer to
102 * a struct regmap. This function should generally not be called
103 * directly, it should be called by bus-specific init functions.
105 struct regmap *regmap_init(struct device *dev,
106 const struct regmap_bus *bus,
107 const struct regmap_config *config)
109 struct regmap *map;
110 int ret = -EINVAL;
112 if (!bus || !config)
113 return NULL;
115 map = kzalloc(sizeof(*map), GFP_KERNEL);
116 if (map == NULL) {
117 ret = -ENOMEM;
118 goto err;
121 mutex_init(&map->lock);
122 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
123 map->format.reg_bytes = config->reg_bits / 8;
124 map->format.val_bytes = config->val_bits / 8;
125 map->dev = dev;
126 map->bus = bus;
127 map->max_register = config->max_register;
128 map->writeable_reg = config->writeable_reg;
129 map->readable_reg = config->readable_reg;
130 map->volatile_reg = config->volatile_reg;
132 switch (config->reg_bits) {
133 case 4:
134 switch (config->val_bits) {
135 case 12:
136 map->format.format_write = regmap_format_4_12_write;
137 break;
138 default:
139 goto err_map;
141 break;
143 case 7:
144 switch (config->val_bits) {
145 case 9:
146 map->format.format_write = regmap_format_7_9_write;
147 break;
148 default:
149 goto err_map;
151 break;
153 case 8:
154 map->format.format_reg = regmap_format_8;
155 break;
157 case 16:
158 map->format.format_reg = regmap_format_16;
159 break;
161 default:
162 goto err_map;
165 switch (config->val_bits) {
166 case 8:
167 map->format.format_val = regmap_format_8;
168 map->format.parse_val = regmap_parse_8;
169 break;
170 case 16:
171 map->format.format_val = regmap_format_16;
172 map->format.parse_val = regmap_parse_16;
173 break;
176 if (!map->format.format_write &&
177 !(map->format.format_reg && map->format.format_val))
178 goto err_map;
180 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
181 if (map->work_buf == NULL) {
182 ret = -ENOMEM;
183 goto err_bus;
186 return map;
188 err_bus:
189 module_put(map->bus->owner);
190 err_map:
191 kfree(map);
192 err:
193 return ERR_PTR(ret);
195 EXPORT_SYMBOL_GPL(regmap_init);
198 * regmap_exit(): Free a previously allocated register map
200 void regmap_exit(struct regmap *map)
202 kfree(map->work_buf);
203 module_put(map->bus->owner);
204 kfree(map);
206 EXPORT_SYMBOL_GPL(regmap_exit);
208 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
209 const void *val, size_t val_len)
211 void *buf;
212 int ret = -ENOTSUPP;
213 size_t len;
215 map->format.format_reg(map->work_buf, reg);
217 trace_regmap_hw_write_start(map->dev, reg,
218 val_len / map->format.val_bytes);
220 /* If we're doing a single register write we can probably just
221 * send the work_buf directly, otherwise try to do a gather
222 * write.
224 if (val == map->work_buf + map->format.reg_bytes)
225 ret = map->bus->write(map->dev, map->work_buf,
226 map->format.reg_bytes + val_len);
227 else if (map->bus->gather_write)
228 ret = map->bus->gather_write(map->dev, map->work_buf,
229 map->format.reg_bytes,
230 val, val_len);
232 /* If that didn't work fall back on linearising by hand. */
233 if (ret == -ENOTSUPP) {
234 len = map->format.reg_bytes + val_len;
235 buf = kmalloc(len, GFP_KERNEL);
236 if (!buf)
237 return -ENOMEM;
239 memcpy(buf, map->work_buf, map->format.reg_bytes);
240 memcpy(buf + map->format.reg_bytes, val, val_len);
241 ret = map->bus->write(map->dev, buf, len);
243 kfree(buf);
246 trace_regmap_hw_write_done(map->dev, reg,
247 val_len / map->format.val_bytes);
249 return ret;
252 static int _regmap_write(struct regmap *map, unsigned int reg,
253 unsigned int val)
255 int ret;
256 BUG_ON(!map->format.format_write && !map->format.format_val);
258 trace_regmap_reg_write(map->dev, reg, val);
260 if (map->format.format_write) {
261 map->format.format_write(map, reg, val);
263 trace_regmap_hw_write_start(map->dev, reg, 1);
265 ret = map->bus->write(map->dev, map->work_buf,
266 map->format.buf_size);
268 trace_regmap_hw_write_done(map->dev, reg, 1);
270 return ret;
271 } else {
272 map->format.format_val(map->work_buf + map->format.reg_bytes,
273 val);
274 return _regmap_raw_write(map, reg,
275 map->work_buf + map->format.reg_bytes,
276 map->format.val_bytes);
281 * regmap_write(): Write a value to a single register
283 * @map: Register map to write to
284 * @reg: Register to write to
285 * @val: Value to be written
287 * A value of zero will be returned on success, a negative errno will
288 * be returned in error cases.
290 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
292 int ret;
294 mutex_lock(&map->lock);
296 ret = _regmap_write(map, reg, val);
298 mutex_unlock(&map->lock);
300 return ret;
302 EXPORT_SYMBOL_GPL(regmap_write);
305 * regmap_raw_write(): Write raw values to one or more registers
307 * @map: Register map to write to
308 * @reg: Initial register to write to
309 * @val: Block of data to be written, laid out for direct transmission to the
310 * device
311 * @val_len: Length of data pointed to by val.
313 * This function is intended to be used for things like firmware
314 * download where a large block of data needs to be transferred to the
315 * device. No formatting will be done on the data provided.
317 * A value of zero will be returned on success, a negative errno will
318 * be returned in error cases.
320 int regmap_raw_write(struct regmap *map, unsigned int reg,
321 const void *val, size_t val_len)
323 int ret;
325 mutex_lock(&map->lock);
327 ret = _regmap_raw_write(map, reg, val, val_len);
329 mutex_unlock(&map->lock);
331 return ret;
333 EXPORT_SYMBOL_GPL(regmap_raw_write);
335 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
336 unsigned int val_len)
338 u8 *u8 = map->work_buf;
339 int ret;
341 map->format.format_reg(map->work_buf, reg);
344 * Some buses flag reads by setting the high bits in the
345 * register addresss; since it's always the high bits for all
346 * current formats we can do this here rather than in
347 * formatting. This may break if we get interesting formats.
349 if (map->bus->read_flag_mask)
350 u8[0] |= map->bus->read_flag_mask;
352 trace_regmap_hw_read_start(map->dev, reg,
353 val_len / map->format.val_bytes);
355 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
356 val, val_len);
358 trace_regmap_hw_read_done(map->dev, reg,
359 val_len / map->format.val_bytes);
361 return ret;
364 static int _regmap_read(struct regmap *map, unsigned int reg,
365 unsigned int *val)
367 int ret;
369 if (!map->format.parse_val)
370 return -EINVAL;
372 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
373 if (ret == 0) {
374 *val = map->format.parse_val(map->work_buf);
375 trace_regmap_reg_read(map->dev, reg, *val);
378 return ret;
382 * regmap_read(): Read a value from a single register
384 * @map: Register map to write to
385 * @reg: Register to be read from
386 * @val: Pointer to store read value
388 * A value of zero will be returned on success, a negative errno will
389 * be returned in error cases.
391 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
393 int ret;
395 mutex_lock(&map->lock);
397 ret = _regmap_read(map, reg, val);
399 mutex_unlock(&map->lock);
401 return ret;
403 EXPORT_SYMBOL_GPL(regmap_read);
406 * regmap_raw_read(): Read raw data from the device
408 * @map: Register map to write to
409 * @reg: First register to be read from
410 * @val: Pointer to store read value
411 * @val_len: Size of data to read
413 * A value of zero will be returned on success, a negative errno will
414 * be returned in error cases.
416 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
417 size_t val_len)
419 int ret;
421 mutex_lock(&map->lock);
423 ret = _regmap_raw_read(map, reg, val, val_len);
425 mutex_unlock(&map->lock);
427 return ret;
429 EXPORT_SYMBOL_GPL(regmap_raw_read);
432 * regmap_bulk_read(): Read multiple registers from the device
434 * @map: Register map to write to
435 * @reg: First register to be read from
436 * @val: Pointer to store read value, in native register size for device
437 * @val_count: Number of registers to read
439 * A value of zero will be returned on success, a negative errno will
440 * be returned in error cases.
442 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
443 size_t val_count)
445 int ret, i;
446 size_t val_bytes = map->format.val_bytes;
448 if (!map->format.parse_val)
449 return -EINVAL;
451 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
452 if (ret != 0)
453 return ret;
455 for (i = 0; i < val_count * val_bytes; i += val_bytes)
456 map->format.parse_val(val + i);
458 return 0;
460 EXPORT_SYMBOL_GPL(regmap_bulk_read);
463 * remap_update_bits: Perform a read/modify/write cycle on the register map
465 * @map: Register map to update
466 * @reg: Register to update
467 * @mask: Bitmask to change
468 * @val: New value for bitmask
470 * Returns zero for success, a negative number on error.
472 int regmap_update_bits(struct regmap *map, unsigned int reg,
473 unsigned int mask, unsigned int val)
475 int ret;
476 unsigned int tmp;
478 mutex_lock(&map->lock);
480 ret = _regmap_read(map, reg, &tmp);
481 if (ret != 0)
482 goto out;
484 tmp &= ~mask;
485 tmp |= val & mask;
487 ret = _regmap_write(map, reg, tmp);
489 out:
490 mutex_unlock(&map->lock);
492 return ret;
494 EXPORT_SYMBOL_GPL(regmap_update_bits);