regmap: Add helper function for checking if a register range is volatile
[deliverable/linux.git] / drivers / base / regmap / regmap.c
1 /*
2 * Register map access API
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
11 */
12
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
17
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/regmap.h>
20
21 #include "internal.h"
22
23 bool regmap_writeable(struct regmap *map, unsigned int reg)
24 {
25 if (map->max_register && reg > map->max_register)
26 return false;
27
28 if (map->writeable_reg)
29 return map->writeable_reg(map->dev, reg);
30
31 return true;
32 }
33
34 bool regmap_readable(struct regmap *map, unsigned int reg)
35 {
36 if (map->max_register && reg > map->max_register)
37 return false;
38
39 if (map->readable_reg)
40 return map->readable_reg(map->dev, reg);
41
42 return true;
43 }
44
45 bool regmap_volatile(struct regmap *map, unsigned int reg)
46 {
47 if (map->max_register && reg > map->max_register)
48 return false;
49
50 if (map->volatile_reg)
51 return map->volatile_reg(map->dev, reg);
52
53 return true;
54 }
55
56 bool regmap_precious(struct regmap *map, unsigned int reg)
57 {
58 if (map->max_register && reg > map->max_register)
59 return false;
60
61 if (map->precious_reg)
62 return map->precious_reg(map->dev, reg);
63
64 return false;
65 }
66
67 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
68 unsigned int num)
69 {
70 unsigned int i;
71
72 for (i = 0; i < num; i++)
73 if (!regmap_volatile(map, reg + i))
74 return false;
75
76 return true;
77 }
78
79 static void regmap_format_4_12_write(struct regmap *map,
80 unsigned int reg, unsigned int val)
81 {
82 __be16 *out = map->work_buf;
83 *out = cpu_to_be16((reg << 12) | val);
84 }
85
86 static void regmap_format_7_9_write(struct regmap *map,
87 unsigned int reg, unsigned int val)
88 {
89 __be16 *out = map->work_buf;
90 *out = cpu_to_be16((reg << 9) | val);
91 }
92
93 static void regmap_format_8(void *buf, unsigned int val)
94 {
95 u8 *b = buf;
96
97 b[0] = val;
98 }
99
100 static void regmap_format_16(void *buf, unsigned int val)
101 {
102 __be16 *b = buf;
103
104 b[0] = cpu_to_be16(val);
105 }
106
107 static unsigned int regmap_parse_8(void *buf)
108 {
109 u8 *b = buf;
110
111 return b[0];
112 }
113
114 static unsigned int regmap_parse_16(void *buf)
115 {
116 __be16 *b = buf;
117
118 b[0] = be16_to_cpu(b[0]);
119
120 return b[0];
121 }
122
123 /**
124 * regmap_init(): Initialise register map
125 *
126 * @dev: Device that will be interacted with
127 * @bus: Bus-specific callbacks to use with device
128 * @config: Configuration for register map
129 *
130 * The return value will be an ERR_PTR() on error or a valid pointer to
131 * a struct regmap. This function should generally not be called
132 * directly, it should be called by bus-specific init functions.
133 */
134 struct regmap *regmap_init(struct device *dev,
135 const struct regmap_bus *bus,
136 const struct regmap_config *config)
137 {
138 struct regmap *map;
139 int ret = -EINVAL;
140
141 if (!bus || !config)
142 return NULL;
143
144 map = kzalloc(sizeof(*map), GFP_KERNEL);
145 if (map == NULL) {
146 ret = -ENOMEM;
147 goto err;
148 }
149
150 mutex_init(&map->lock);
151 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
152 map->format.reg_bytes = config->reg_bits / 8;
153 map->format.val_bytes = config->val_bits / 8;
154 map->dev = dev;
155 map->bus = bus;
156 map->max_register = config->max_register;
157 map->writeable_reg = config->writeable_reg;
158 map->readable_reg = config->readable_reg;
159 map->volatile_reg = config->volatile_reg;
160 map->precious_reg = config->precious_reg;
161 map->cache_type = config->cache_type;
162 map->reg_defaults = config->reg_defaults;
163 map->num_reg_defaults = config->num_reg_defaults;
164 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
165 map->reg_defaults_raw = config->reg_defaults_raw;
166 map->cache_size_raw = (config->val_bits / 8) * config->num_reg_defaults_raw;
167 map->cache_word_size = config->val_bits / 8;
168
169 if (config->read_flag_mask || config->write_flag_mask) {
170 map->read_flag_mask = config->read_flag_mask;
171 map->write_flag_mask = config->write_flag_mask;
172 } else {
173 map->read_flag_mask = bus->read_flag_mask;
174 }
175
176 switch (config->reg_bits) {
177 case 4:
178 switch (config->val_bits) {
179 case 12:
180 map->format.format_write = regmap_format_4_12_write;
181 break;
182 default:
183 goto err_map;
184 }
185 break;
186
187 case 7:
188 switch (config->val_bits) {
189 case 9:
190 map->format.format_write = regmap_format_7_9_write;
191 break;
192 default:
193 goto err_map;
194 }
195 break;
196
197 case 8:
198 map->format.format_reg = regmap_format_8;
199 break;
200
201 case 16:
202 map->format.format_reg = regmap_format_16;
203 break;
204
205 default:
206 goto err_map;
207 }
208
209 switch (config->val_bits) {
210 case 8:
211 map->format.format_val = regmap_format_8;
212 map->format.parse_val = regmap_parse_8;
213 break;
214 case 16:
215 map->format.format_val = regmap_format_16;
216 map->format.parse_val = regmap_parse_16;
217 break;
218 }
219
220 if (!map->format.format_write &&
221 !(map->format.format_reg && map->format.format_val))
222 goto err_map;
223
224 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
225 if (map->work_buf == NULL) {
226 ret = -ENOMEM;
227 goto err_map;
228 }
229
230 ret = regcache_init(map);
231 if (ret < 0)
232 goto err_map;
233
234 regmap_debugfs_init(map);
235
236 return map;
237
238 err_map:
239 kfree(map);
240 err:
241 return ERR_PTR(ret);
242 }
243 EXPORT_SYMBOL_GPL(regmap_init);
244
245 /**
246 * regmap_exit(): Free a previously allocated register map
247 */
248 void regmap_exit(struct regmap *map)
249 {
250 regcache_exit(map);
251 regmap_debugfs_exit(map);
252 kfree(map->work_buf);
253 kfree(map);
254 }
255 EXPORT_SYMBOL_GPL(regmap_exit);
256
257 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
258 const void *val, size_t val_len)
259 {
260 u8 *u8 = map->work_buf;
261 void *buf;
262 int ret = -ENOTSUPP;
263 size_t len;
264 int i;
265
266 /* Check for unwritable registers before we start */
267 if (map->writeable_reg)
268 for (i = 0; i < val_len / map->format.val_bytes; i++)
269 if (!map->writeable_reg(map->dev, reg + i))
270 return -EINVAL;
271
272 map->format.format_reg(map->work_buf, reg);
273
274 u8[0] |= map->write_flag_mask;
275
276 trace_regmap_hw_write_start(map->dev, reg,
277 val_len / map->format.val_bytes);
278
279 /* If we're doing a single register write we can probably just
280 * send the work_buf directly, otherwise try to do a gather
281 * write.
282 */
283 if (val == map->work_buf + map->format.reg_bytes)
284 ret = map->bus->write(map->dev, map->work_buf,
285 map->format.reg_bytes + val_len);
286 else if (map->bus->gather_write)
287 ret = map->bus->gather_write(map->dev, map->work_buf,
288 map->format.reg_bytes,
289 val, val_len);
290
291 /* If that didn't work fall back on linearising by hand. */
292 if (ret == -ENOTSUPP) {
293 len = map->format.reg_bytes + val_len;
294 buf = kmalloc(len, GFP_KERNEL);
295 if (!buf)
296 return -ENOMEM;
297
298 memcpy(buf, map->work_buf, map->format.reg_bytes);
299 memcpy(buf + map->format.reg_bytes, val, val_len);
300 ret = map->bus->write(map->dev, buf, len);
301
302 kfree(buf);
303 }
304
305 trace_regmap_hw_write_done(map->dev, reg,
306 val_len / map->format.val_bytes);
307
308 return ret;
309 }
310
311 int _regmap_write(struct regmap *map, unsigned int reg,
312 unsigned int val)
313 {
314 int ret;
315 BUG_ON(!map->format.format_write && !map->format.format_val);
316
317 if (!map->cache_bypass) {
318 ret = regcache_write(map, reg, val);
319 if (ret != 0)
320 return ret;
321 if (map->cache_only) {
322 map->cache_dirty = true;
323 return 0;
324 }
325 }
326
327 trace_regmap_reg_write(map->dev, reg, val);
328
329 if (map->format.format_write) {
330 map->format.format_write(map, reg, val);
331
332 trace_regmap_hw_write_start(map->dev, reg, 1);
333
334 ret = map->bus->write(map->dev, map->work_buf,
335 map->format.buf_size);
336
337 trace_regmap_hw_write_done(map->dev, reg, 1);
338
339 return ret;
340 } else {
341 map->format.format_val(map->work_buf + map->format.reg_bytes,
342 val);
343 return _regmap_raw_write(map, reg,
344 map->work_buf + map->format.reg_bytes,
345 map->format.val_bytes);
346 }
347 }
348
349 /**
350 * regmap_write(): Write a value to a single register
351 *
352 * @map: Register map to write to
353 * @reg: Register to write to
354 * @val: Value to be written
355 *
356 * A value of zero will be returned on success, a negative errno will
357 * be returned in error cases.
358 */
359 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
360 {
361 int ret;
362
363 mutex_lock(&map->lock);
364
365 ret = _regmap_write(map, reg, val);
366
367 mutex_unlock(&map->lock);
368
369 return ret;
370 }
371 EXPORT_SYMBOL_GPL(regmap_write);
372
373 /**
374 * regmap_raw_write(): Write raw values to one or more registers
375 *
376 * @map: Register map to write to
377 * @reg: Initial register to write to
378 * @val: Block of data to be written, laid out for direct transmission to the
379 * device
380 * @val_len: Length of data pointed to by val.
381 *
382 * This function is intended to be used for things like firmware
383 * download where a large block of data needs to be transferred to the
384 * device. No formatting will be done on the data provided.
385 *
386 * A value of zero will be returned on success, a negative errno will
387 * be returned in error cases.
388 */
389 int regmap_raw_write(struct regmap *map, unsigned int reg,
390 const void *val, size_t val_len)
391 {
392 int ret;
393
394 WARN_ON(map->cache_type != REGCACHE_NONE);
395
396 mutex_lock(&map->lock);
397
398 ret = _regmap_raw_write(map, reg, val, val_len);
399
400 mutex_unlock(&map->lock);
401
402 return ret;
403 }
404 EXPORT_SYMBOL_GPL(regmap_raw_write);
405
406 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
407 unsigned int val_len)
408 {
409 u8 *u8 = map->work_buf;
410 int ret;
411
412 map->format.format_reg(map->work_buf, reg);
413
414 /*
415 * Some buses or devices flag reads by setting the high bits in the
416 * register addresss; since it's always the high bits for all
417 * current formats we can do this here rather than in
418 * formatting. This may break if we get interesting formats.
419 */
420 u8[0] |= map->read_flag_mask;
421
422 trace_regmap_hw_read_start(map->dev, reg,
423 val_len / map->format.val_bytes);
424
425 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
426 val, val_len);
427
428 trace_regmap_hw_read_done(map->dev, reg,
429 val_len / map->format.val_bytes);
430
431 return ret;
432 }
433
434 static int _regmap_read(struct regmap *map, unsigned int reg,
435 unsigned int *val)
436 {
437 int ret;
438
439 if (!map->format.parse_val)
440 return -EINVAL;
441
442 if (!map->cache_bypass) {
443 ret = regcache_read(map, reg, val);
444 if (ret == 0)
445 return 0;
446 }
447
448 if (map->cache_only)
449 return -EBUSY;
450
451 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
452 if (ret == 0) {
453 *val = map->format.parse_val(map->work_buf);
454 trace_regmap_reg_read(map->dev, reg, *val);
455 }
456
457 return ret;
458 }
459
460 /**
461 * regmap_read(): Read a value from a single register
462 *
463 * @map: Register map to write to
464 * @reg: Register to be read from
465 * @val: Pointer to store read value
466 *
467 * A value of zero will be returned on success, a negative errno will
468 * be returned in error cases.
469 */
470 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
471 {
472 int ret;
473
474 mutex_lock(&map->lock);
475
476 ret = _regmap_read(map, reg, val);
477
478 mutex_unlock(&map->lock);
479
480 return ret;
481 }
482 EXPORT_SYMBOL_GPL(regmap_read);
483
484 /**
485 * regmap_raw_read(): Read raw data from the device
486 *
487 * @map: Register map to write to
488 * @reg: First register to be read from
489 * @val: Pointer to store read value
490 * @val_len: Size of data to read
491 *
492 * A value of zero will be returned on success, a negative errno will
493 * be returned in error cases.
494 */
495 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
496 size_t val_len)
497 {
498 size_t val_count = val_len / map->format.val_bytes;
499 int ret;
500
501 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
502 map->cache_type != REGCACHE_NONE);
503
504 mutex_lock(&map->lock);
505
506 ret = _regmap_raw_read(map, reg, val, val_len);
507
508 mutex_unlock(&map->lock);
509
510 return ret;
511 }
512 EXPORT_SYMBOL_GPL(regmap_raw_read);
513
514 /**
515 * regmap_bulk_read(): Read multiple registers from the device
516 *
517 * @map: Register map to write to
518 * @reg: First register to be read from
519 * @val: Pointer to store read value, in native register size for device
520 * @val_count: Number of registers to read
521 *
522 * A value of zero will be returned on success, a negative errno will
523 * be returned in error cases.
524 */
525 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
526 size_t val_count)
527 {
528 int ret, i;
529 size_t val_bytes = map->format.val_bytes;
530 bool vol = regmap_volatile_range(map, reg, val_count);
531
532 if (!map->format.parse_val)
533 return -EINVAL;
534
535 if (vol || map->cache_type == REGCACHE_NONE) {
536 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
537 if (ret != 0)
538 return ret;
539
540 for (i = 0; i < val_count * val_bytes; i += val_bytes)
541 map->format.parse_val(val + i);
542 } else {
543 for (i = 0; i < val_count; i++) {
544 ret = regmap_read(map, reg + i, val + (i * val_bytes));
545 if (ret != 0)
546 return ret;
547 }
548 }
549
550 return 0;
551 }
552 EXPORT_SYMBOL_GPL(regmap_bulk_read);
553
554 /**
555 * regmap_update_bits: Perform a read/modify/write cycle on the register map
556 *
557 * @map: Register map to update
558 * @reg: Register to update
559 * @mask: Bitmask to change
560 * @val: New value for bitmask
561 *
562 * Returns zero for success, a negative number on error.
563 */
564 int regmap_update_bits(struct regmap *map, unsigned int reg,
565 unsigned int mask, unsigned int val)
566 {
567 int ret;
568 unsigned int tmp;
569
570 mutex_lock(&map->lock);
571
572 ret = _regmap_read(map, reg, &tmp);
573 if (ret != 0)
574 goto out;
575
576 tmp &= ~mask;
577 tmp |= val & mask;
578
579 ret = _regmap_write(map, reg, tmp);
580
581 out:
582 mutex_unlock(&map->lock);
583
584 return ret;
585 }
586 EXPORT_SYMBOL_GPL(regmap_update_bits);
587
588 static int __init regmap_initcall(void)
589 {
590 regmap_debugfs_initcall();
591
592 return 0;
593 }
594 postcore_initcall(regmap_initcall);
This page took 0.120569 seconds and 5 git commands to generate.