MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / drivers / base / regmap / regmap.c
blobbf441db1ee90af507ac3597e6a32dd1a3cbfec10
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 #define CREATE_TRACE_POINTS
19 #include <trace/events/regmap.h>
21 #include "internal.h"
23 bool regmap_writeable(struct regmap *map, unsigned int reg)
25 if (map->max_register && reg > map->max_register)
26 return false;
28 if (map->writeable_reg)
29 return map->writeable_reg(map->dev, reg);
31 return true;
34 bool regmap_readable(struct regmap *map, unsigned int reg)
36 if (map->max_register && reg > map->max_register)
37 return false;
39 if (map->readable_reg)
40 return map->readable_reg(map->dev, reg);
42 return true;
45 bool regmap_volatile(struct regmap *map, unsigned int reg)
47 if (map->max_register && reg > map->max_register)
48 return false;
50 if (map->volatile_reg)
51 return map->volatile_reg(map->dev, reg);
53 return true;
56 bool regmap_precious(struct regmap *map, unsigned int reg)
58 if (map->max_register && reg > map->max_register)
59 return false;
61 if (map->precious_reg)
62 return map->precious_reg(map->dev, reg);
64 return false;
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)
83 u8 *b = buf;
85 b[0] = val;
88 static void regmap_format_16(void *buf, unsigned int val)
90 __be16 *b = buf;
92 b[0] = cpu_to_be16(val);
95 static unsigned int regmap_parse_8(void *buf)
97 u8 *b = buf;
99 return b[0];
102 static unsigned int regmap_parse_16(void *buf)
104 __be16 *b = buf;
106 b[0] = be16_to_cpu(b[0]);
108 return 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)
126 struct regmap *map;
127 int ret = -EINVAL;
129 if (!bus || !config)
130 return NULL;
132 map = kzalloc(sizeof(*map), GFP_KERNEL);
133 if (map == NULL) {
134 ret = -ENOMEM;
135 goto err;
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;
142 map->dev = dev;
143 map->bus = bus;
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;
160 } else {
161 map->read_flag_mask = bus->read_flag_mask;
164 switch (config->reg_bits) {
165 case 4:
166 switch (config->val_bits) {
167 case 12:
168 map->format.format_write = regmap_format_4_12_write;
169 break;
170 default:
171 goto err_map;
173 break;
175 case 7:
176 switch (config->val_bits) {
177 case 9:
178 map->format.format_write = regmap_format_7_9_write;
179 break;
180 default:
181 goto err_map;
183 break;
185 case 8:
186 map->format.format_reg = regmap_format_8;
187 break;
189 case 16:
190 map->format.format_reg = regmap_format_16;
191 break;
193 default:
194 goto err_map;
197 switch (config->val_bits) {
198 case 8:
199 map->format.format_val = regmap_format_8;
200 map->format.parse_val = regmap_parse_8;
201 break;
202 case 16:
203 map->format.format_val = regmap_format_16;
204 map->format.parse_val = regmap_parse_16;
205 break;
208 if (!map->format.format_write &&
209 !(map->format.format_reg && map->format.format_val))
210 goto err_map;
212 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
213 if (map->work_buf == NULL) {
214 ret = -ENOMEM;
215 goto err_map;
218 ret = regcache_init(map);
219 if (ret < 0)
220 goto err_map;
222 regmap_debugfs_init(map);
224 return map;
226 err_map:
227 kfree(map);
228 err:
229 return ERR_PTR(ret);
231 EXPORT_SYMBOL_GPL(regmap_init);
234 * regmap_exit(): Free a previously allocated register map
236 void regmap_exit(struct regmap *map)
238 regcache_exit(map);
239 regmap_debugfs_exit(map);
240 kfree(map->work_buf);
241 kfree(map);
243 EXPORT_SYMBOL_GPL(regmap_exit);
245 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
246 const void *val, size_t val_len)
248 u8 *u8 = map->work_buf;
249 void *buf;
250 int ret = -ENOTSUPP;
251 size_t len;
252 int i;
254 /* Check for unwritable registers before we start */
255 if (map->writeable_reg)
256 for (i = 0; i < val_len / map->format.val_bytes; i++)
257 if (!map->writeable_reg(map->dev, reg + i))
258 return -EINVAL;
260 map->format.format_reg(map->work_buf, reg);
262 u8[0] |= map->write_flag_mask;
264 trace_regmap_hw_write_start(map->dev, reg,
265 val_len / map->format.val_bytes);
267 /* If we're doing a single register write we can probably just
268 * send the work_buf directly, otherwise try to do a gather
269 * write.
271 if (val == map->work_buf + map->format.reg_bytes)
272 ret = map->bus->write(map->dev, map->work_buf,
273 map->format.reg_bytes + val_len);
274 else if (map->bus->gather_write)
275 ret = map->bus->gather_write(map->dev, map->work_buf,
276 map->format.reg_bytes,
277 val, val_len);
279 /* If that didn't work fall back on linearising by hand. */
280 if (ret == -ENOTSUPP) {
281 len = map->format.reg_bytes + val_len;
282 buf = kmalloc(len, GFP_KERNEL);
283 if (!buf)
284 return -ENOMEM;
286 memcpy(buf, map->work_buf, map->format.reg_bytes);
287 memcpy(buf + map->format.reg_bytes, val, val_len);
288 ret = map->bus->write(map->dev, buf, len);
290 kfree(buf);
293 trace_regmap_hw_write_done(map->dev, reg,
294 val_len / map->format.val_bytes);
296 return ret;
299 int _regmap_write(struct regmap *map, unsigned int reg,
300 unsigned int val)
302 int ret;
303 BUG_ON(!map->format.format_write && !map->format.format_val);
305 if (!map->cache_bypass) {
306 ret = regcache_write(map, reg, val);
307 if (ret != 0)
308 return ret;
309 if (map->cache_only)
310 return 0;
313 trace_regmap_reg_write(map->dev, reg, val);
315 if (map->format.format_write) {
316 map->format.format_write(map, reg, val);
318 trace_regmap_hw_write_start(map->dev, reg, 1);
320 ret = map->bus->write(map->dev, map->work_buf,
321 map->format.buf_size);
323 trace_regmap_hw_write_done(map->dev, reg, 1);
325 return ret;
326 } else {
327 map->format.format_val(map->work_buf + map->format.reg_bytes,
328 val);
329 return _regmap_raw_write(map, reg,
330 map->work_buf + map->format.reg_bytes,
331 map->format.val_bytes);
336 * regmap_write(): Write a value to a single register
338 * @map: Register map to write to
339 * @reg: Register to write to
340 * @val: Value to be written
342 * A value of zero will be returned on success, a negative errno will
343 * be returned in error cases.
345 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
347 int ret;
349 mutex_lock(&map->lock);
351 ret = _regmap_write(map, reg, val);
353 mutex_unlock(&map->lock);
355 return ret;
357 EXPORT_SYMBOL_GPL(regmap_write);
360 * regmap_raw_write(): Write raw values to one or more registers
362 * @map: Register map to write to
363 * @reg: Initial register to write to
364 * @val: Block of data to be written, laid out for direct transmission to the
365 * device
366 * @val_len: Length of data pointed to by val.
368 * This function is intended to be used for things like firmware
369 * download where a large block of data needs to be transferred to the
370 * device. No formatting will be done on the data provided.
372 * A value of zero will be returned on success, a negative errno will
373 * be returned in error cases.
375 int regmap_raw_write(struct regmap *map, unsigned int reg,
376 const void *val, size_t val_len)
378 int ret;
380 WARN_ON(map->cache_type != REGCACHE_NONE);
382 mutex_lock(&map->lock);
384 ret = _regmap_raw_write(map, reg, val, val_len);
386 mutex_unlock(&map->lock);
388 return ret;
390 EXPORT_SYMBOL_GPL(regmap_raw_write);
392 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
393 unsigned int val_len)
395 u8 *u8 = map->work_buf;
396 int ret;
398 map->format.format_reg(map->work_buf, reg);
401 * Some buses or devices flag reads by setting the high bits in the
402 * register addresss; since it's always the high bits for all
403 * current formats we can do this here rather than in
404 * formatting. This may break if we get interesting formats.
406 u8[0] |= map->read_flag_mask;
408 trace_regmap_hw_read_start(map->dev, reg,
409 val_len / map->format.val_bytes);
411 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
412 val, val_len);
414 trace_regmap_hw_read_done(map->dev, reg,
415 val_len / map->format.val_bytes);
417 return ret;
420 static int _regmap_read(struct regmap *map, unsigned int reg,
421 unsigned int *val)
423 int ret;
425 if (!map->format.parse_val)
426 return -EINVAL;
428 if (!map->cache_bypass) {
429 ret = regcache_read(map, reg, val);
430 if (ret == 0)
431 return 0;
434 if (map->cache_only)
435 return -EBUSY;
437 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
438 if (ret == 0) {
439 *val = map->format.parse_val(map->work_buf);
440 trace_regmap_reg_read(map->dev, reg, *val);
443 return ret;
447 * regmap_read(): Read a value from a single register
449 * @map: Register map to write to
450 * @reg: Register to be read from
451 * @val: Pointer to store read value
453 * A value of zero will be returned on success, a negative errno will
454 * be returned in error cases.
456 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
458 int ret;
460 mutex_lock(&map->lock);
462 ret = _regmap_read(map, reg, val);
464 mutex_unlock(&map->lock);
466 return ret;
468 EXPORT_SYMBOL_GPL(regmap_read);
471 * regmap_raw_read(): Read raw data from the device
473 * @map: Register map to write to
474 * @reg: First register to be read from
475 * @val: Pointer to store read value
476 * @val_len: Size of data to read
478 * A value of zero will be returned on success, a negative errno will
479 * be returned in error cases.
481 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
482 size_t val_len)
484 int ret;
485 int i;
486 bool vol = true;
488 for (i = 0; i < val_len / map->format.val_bytes; i++)
489 if (!regmap_volatile(map, reg + i))
490 vol = false;
492 WARN_ON(!vol && map->cache_type != REGCACHE_NONE);
494 mutex_lock(&map->lock);
496 ret = _regmap_raw_read(map, reg, val, val_len);
498 mutex_unlock(&map->lock);
500 return ret;
502 EXPORT_SYMBOL_GPL(regmap_raw_read);
505 * regmap_bulk_read(): Read multiple registers from the device
507 * @map: Register map to write to
508 * @reg: First register to be read from
509 * @val: Pointer to store read value, in native register size for device
510 * @val_count: Number of registers to read
512 * A value of zero will be returned on success, a negative errno will
513 * be returned in error cases.
515 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
516 size_t val_count)
518 int ret, i;
519 size_t val_bytes = map->format.val_bytes;
520 bool vol = true;
522 if (!map->format.parse_val)
523 return -EINVAL;
525 /* Is this a block of volatile registers? */
526 for (i = 0; i < val_count; i++)
527 if (!regmap_volatile(map, reg + i))
528 vol = false;
530 if (vol || map->cache_type == REGCACHE_NONE) {
531 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
532 if (ret != 0)
533 return ret;
535 for (i = 0; i < val_count * val_bytes; i += val_bytes)
536 map->format.parse_val(val + i);
537 } else {
538 for (i = 0; i < val_count; i++) {
539 ret = regmap_read(map, reg + i, val + (i * val_bytes));
540 if (ret != 0)
541 return ret;
545 return 0;
547 EXPORT_SYMBOL_GPL(regmap_bulk_read);
550 * remap_update_bits: Perform a read/modify/write cycle on the register map
552 * @map: Register map to update
553 * @reg: Register to update
554 * @mask: Bitmask to change
555 * @val: New value for bitmask
557 * Returns zero for success, a negative number on error.
559 int regmap_update_bits(struct regmap *map, unsigned int reg,
560 unsigned int mask, unsigned int val)
562 int ret;
563 unsigned int tmp;
565 mutex_lock(&map->lock);
567 ret = _regmap_read(map, reg, &tmp);
568 if (ret != 0)
569 goto out;
571 tmp &= ~mask;
572 tmp |= val & mask;
574 ret = _regmap_write(map, reg, tmp);
576 out:
577 mutex_unlock(&map->lock);
579 return ret;
581 EXPORT_SYMBOL_GPL(regmap_update_bits);
583 static int __init regmap_initcall(void)
585 regmap_debugfs_initcall();
587 return 0;
589 postcore_initcall(regmap_initcall);