regmap: mmio: Staticize regmap_mmio_gen_context()
[deliverable/linux.git] / drivers / base / regmap / regmap.c
CommitLineData
b83a313b
MB
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
f5d6eba7 13#include <linux/device.h>
b83a313b 14#include <linux/slab.h>
19694b5e 15#include <linux/export.h>
b83a313b
MB
16#include <linux/mutex.h>
17#include <linux/err.h>
18
fb2736bb
MB
19#define CREATE_TRACE_POINTS
20#include <trace/events/regmap.h>
21
93de9124 22#include "internal.h"
b83a313b 23
8de2f081
MB
24bool regmap_writeable(struct regmap *map, unsigned int reg)
25{
26 if (map->max_register && reg > map->max_register)
27 return false;
28
29 if (map->writeable_reg)
30 return map->writeable_reg(map->dev, reg);
31
32 return true;
33}
34
35bool regmap_readable(struct regmap *map, unsigned int reg)
36{
37 if (map->max_register && reg > map->max_register)
38 return false;
39
4191f197
WS
40 if (map->format.format_write)
41 return false;
42
8de2f081
MB
43 if (map->readable_reg)
44 return map->readable_reg(map->dev, reg);
45
46 return true;
47}
48
49bool regmap_volatile(struct regmap *map, unsigned int reg)
50{
4191f197 51 if (!regmap_readable(map, reg))
8de2f081
MB
52 return false;
53
54 if (map->volatile_reg)
55 return map->volatile_reg(map->dev, reg);
56
57 return true;
58}
59
60bool regmap_precious(struct regmap *map, unsigned int reg)
61{
4191f197 62 if (!regmap_readable(map, reg))
8de2f081
MB
63 return false;
64
65 if (map->precious_reg)
66 return map->precious_reg(map->dev, reg);
67
68 return false;
69}
70
82cd9965
LPC
71static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
72 unsigned int num)
73{
74 unsigned int i;
75
76 for (i = 0; i < num; i++)
77 if (!regmap_volatile(map, reg + i))
78 return false;
79
80 return true;
81}
82
9aa50750
WS
83static void regmap_format_2_6_write(struct regmap *map,
84 unsigned int reg, unsigned int val)
85{
86 u8 *out = map->work_buf;
87
88 *out = (reg << 6) | val;
89}
90
b83a313b
MB
91static void regmap_format_4_12_write(struct regmap *map,
92 unsigned int reg, unsigned int val)
93{
94 __be16 *out = map->work_buf;
95 *out = cpu_to_be16((reg << 12) | val);
96}
97
98static void regmap_format_7_9_write(struct regmap *map,
99 unsigned int reg, unsigned int val)
100{
101 __be16 *out = map->work_buf;
102 *out = cpu_to_be16((reg << 9) | val);
103}
104
7e5ec63e
LPC
105static void regmap_format_10_14_write(struct regmap *map,
106 unsigned int reg, unsigned int val)
107{
108 u8 *out = map->work_buf;
109
110 out[2] = val;
111 out[1] = (val >> 8) | (reg << 6);
112 out[0] = reg >> 2;
113}
114
d939fb9a 115static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
b83a313b
MB
116{
117 u8 *b = buf;
118
d939fb9a 119 b[0] = val << shift;
b83a313b
MB
120}
121
141eba2e 122static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
b83a313b
MB
123{
124 __be16 *b = buf;
125
d939fb9a 126 b[0] = cpu_to_be16(val << shift);
b83a313b
MB
127}
128
141eba2e
SW
129static void regmap_format_16_native(void *buf, unsigned int val,
130 unsigned int shift)
131{
132 *(u16 *)buf = val << shift;
133}
134
d939fb9a 135static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
ea279fc5
MR
136{
137 u8 *b = buf;
138
d939fb9a
MR
139 val <<= shift;
140
ea279fc5
MR
141 b[0] = val >> 16;
142 b[1] = val >> 8;
143 b[2] = val;
144}
145
141eba2e 146static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
7d5e525b
MB
147{
148 __be32 *b = buf;
149
d939fb9a 150 b[0] = cpu_to_be32(val << shift);
7d5e525b
MB
151}
152
141eba2e
SW
153static void regmap_format_32_native(void *buf, unsigned int val,
154 unsigned int shift)
155{
156 *(u32 *)buf = val << shift;
157}
158
b83a313b
MB
159static unsigned int regmap_parse_8(void *buf)
160{
161 u8 *b = buf;
162
163 return b[0];
164}
165
141eba2e 166static unsigned int regmap_parse_16_be(void *buf)
b83a313b
MB
167{
168 __be16 *b = buf;
169
170 b[0] = be16_to_cpu(b[0]);
171
172 return b[0];
173}
174
141eba2e
SW
175static unsigned int regmap_parse_16_native(void *buf)
176{
177 return *(u16 *)buf;
178}
179
ea279fc5
MR
180static unsigned int regmap_parse_24(void *buf)
181{
182 u8 *b = buf;
183 unsigned int ret = b[2];
184 ret |= ((unsigned int)b[1]) << 8;
185 ret |= ((unsigned int)b[0]) << 16;
186
187 return ret;
188}
189
141eba2e 190static unsigned int regmap_parse_32_be(void *buf)
7d5e525b
MB
191{
192 __be32 *b = buf;
193
194 b[0] = be32_to_cpu(b[0]);
195
196 return b[0];
197}
198
141eba2e
SW
199static unsigned int regmap_parse_32_native(void *buf)
200{
201 return *(u32 *)buf;
202}
203
bacdbe07
SW
204static void regmap_lock_mutex(struct regmap *map)
205{
206 mutex_lock(&map->mutex);
207}
208
209static void regmap_unlock_mutex(struct regmap *map)
210{
211 mutex_unlock(&map->mutex);
212}
213
214static void regmap_lock_spinlock(struct regmap *map)
215{
216 spin_lock(&map->spinlock);
217}
218
219static void regmap_unlock_spinlock(struct regmap *map)
220{
221 spin_unlock(&map->spinlock);
222}
223
72b39f6f
MB
224static void dev_get_regmap_release(struct device *dev, void *res)
225{
226 /*
227 * We don't actually have anything to do here; the goal here
228 * is not to manage the regmap but to provide a simple way to
229 * get the regmap back given a struct device.
230 */
231}
232
b83a313b
MB
233/**
234 * regmap_init(): Initialise register map
235 *
236 * @dev: Device that will be interacted with
237 * @bus: Bus-specific callbacks to use with device
0135bbcc 238 * @bus_context: Data passed to bus-specific callbacks
b83a313b
MB
239 * @config: Configuration for register map
240 *
241 * The return value will be an ERR_PTR() on error or a valid pointer to
242 * a struct regmap. This function should generally not be called
243 * directly, it should be called by bus-specific init functions.
244 */
245struct regmap *regmap_init(struct device *dev,
246 const struct regmap_bus *bus,
0135bbcc 247 void *bus_context,
b83a313b
MB
248 const struct regmap_config *config)
249{
72b39f6f 250 struct regmap *map, **m;
b83a313b 251 int ret = -EINVAL;
141eba2e 252 enum regmap_endian reg_endian, val_endian;
b83a313b
MB
253
254 if (!bus || !config)
abbb18fb 255 goto err;
b83a313b
MB
256
257 map = kzalloc(sizeof(*map), GFP_KERNEL);
258 if (map == NULL) {
259 ret = -ENOMEM;
260 goto err;
261 }
262
bacdbe07
SW
263 if (bus->fast_io) {
264 spin_lock_init(&map->spinlock);
265 map->lock = regmap_lock_spinlock;
266 map->unlock = regmap_unlock_spinlock;
267 } else {
268 mutex_init(&map->mutex);
269 map->lock = regmap_lock_mutex;
270 map->unlock = regmap_unlock_mutex;
271 }
b83a313b 272 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
c212accc 273 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
82159ba8 274 map->format.pad_bytes = config->pad_bits / 8;
c212accc 275 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
82159ba8 276 map->format.buf_size += map->format.pad_bytes;
d939fb9a 277 map->reg_shift = config->pad_bits % 8;
f01ee60f
SW
278 if (config->reg_stride)
279 map->reg_stride = config->reg_stride;
280 else
281 map->reg_stride = 1;
2e33caf1 282 map->use_single_rw = config->use_single_rw;
b83a313b
MB
283 map->dev = dev;
284 map->bus = bus;
0135bbcc 285 map->bus_context = bus_context;
2e2ae66d
MB
286 map->max_register = config->max_register;
287 map->writeable_reg = config->writeable_reg;
288 map->readable_reg = config->readable_reg;
289 map->volatile_reg = config->volatile_reg;
2efe1642 290 map->precious_reg = config->precious_reg;
5d1729e7 291 map->cache_type = config->cache_type;
72b39f6f 292 map->name = config->name;
b83a313b 293
6f306441
LPC
294 if (config->read_flag_mask || config->write_flag_mask) {
295 map->read_flag_mask = config->read_flag_mask;
296 map->write_flag_mask = config->write_flag_mask;
297 } else {
298 map->read_flag_mask = bus->read_flag_mask;
299 }
300
141eba2e
SW
301 reg_endian = config->reg_format_endian;
302 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
303 reg_endian = bus->reg_format_endian_default;
304 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
305 reg_endian = REGMAP_ENDIAN_BIG;
306
307 val_endian = config->val_format_endian;
308 if (val_endian == REGMAP_ENDIAN_DEFAULT)
309 val_endian = bus->val_format_endian_default;
310 if (val_endian == REGMAP_ENDIAN_DEFAULT)
311 val_endian = REGMAP_ENDIAN_BIG;
312
d939fb9a 313 switch (config->reg_bits + map->reg_shift) {
9aa50750
WS
314 case 2:
315 switch (config->val_bits) {
316 case 6:
317 map->format.format_write = regmap_format_2_6_write;
318 break;
319 default:
320 goto err_map;
321 }
322 break;
323
b83a313b
MB
324 case 4:
325 switch (config->val_bits) {
326 case 12:
327 map->format.format_write = regmap_format_4_12_write;
328 break;
329 default:
330 goto err_map;
331 }
332 break;
333
334 case 7:
335 switch (config->val_bits) {
336 case 9:
337 map->format.format_write = regmap_format_7_9_write;
338 break;
339 default:
340 goto err_map;
341 }
342 break;
343
7e5ec63e
LPC
344 case 10:
345 switch (config->val_bits) {
346 case 14:
347 map->format.format_write = regmap_format_10_14_write;
348 break;
349 default:
350 goto err_map;
351 }
352 break;
353
b83a313b
MB
354 case 8:
355 map->format.format_reg = regmap_format_8;
356 break;
357
358 case 16:
141eba2e
SW
359 switch (reg_endian) {
360 case REGMAP_ENDIAN_BIG:
361 map->format.format_reg = regmap_format_16_be;
362 break;
363 case REGMAP_ENDIAN_NATIVE:
364 map->format.format_reg = regmap_format_16_native;
365 break;
366 default:
367 goto err_map;
368 }
b83a313b
MB
369 break;
370
7d5e525b 371 case 32:
141eba2e
SW
372 switch (reg_endian) {
373 case REGMAP_ENDIAN_BIG:
374 map->format.format_reg = regmap_format_32_be;
375 break;
376 case REGMAP_ENDIAN_NATIVE:
377 map->format.format_reg = regmap_format_32_native;
378 break;
379 default:
380 goto err_map;
381 }
7d5e525b
MB
382 break;
383
b83a313b
MB
384 default:
385 goto err_map;
386 }
387
388 switch (config->val_bits) {
389 case 8:
390 map->format.format_val = regmap_format_8;
391 map->format.parse_val = regmap_parse_8;
392 break;
393 case 16:
141eba2e
SW
394 switch (val_endian) {
395 case REGMAP_ENDIAN_BIG:
396 map->format.format_val = regmap_format_16_be;
397 map->format.parse_val = regmap_parse_16_be;
398 break;
399 case REGMAP_ENDIAN_NATIVE:
400 map->format.format_val = regmap_format_16_native;
401 map->format.parse_val = regmap_parse_16_native;
402 break;
403 default:
404 goto err_map;
405 }
b83a313b 406 break;
ea279fc5 407 case 24:
141eba2e
SW
408 if (val_endian != REGMAP_ENDIAN_BIG)
409 goto err_map;
ea279fc5
MR
410 map->format.format_val = regmap_format_24;
411 map->format.parse_val = regmap_parse_24;
412 break;
7d5e525b 413 case 32:
141eba2e
SW
414 switch (val_endian) {
415 case REGMAP_ENDIAN_BIG:
416 map->format.format_val = regmap_format_32_be;
417 map->format.parse_val = regmap_parse_32_be;
418 break;
419 case REGMAP_ENDIAN_NATIVE:
420 map->format.format_val = regmap_format_32_native;
421 map->format.parse_val = regmap_parse_32_native;
422 break;
423 default:
424 goto err_map;
425 }
7d5e525b 426 break;
b83a313b
MB
427 }
428
141eba2e
SW
429 if (map->format.format_write) {
430 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
431 (val_endian != REGMAP_ENDIAN_BIG))
432 goto err_map;
7a647614 433 map->use_single_rw = true;
141eba2e 434 }
7a647614 435
b83a313b
MB
436 if (!map->format.format_write &&
437 !(map->format.format_reg && map->format.format_val))
438 goto err_map;
439
82159ba8 440 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
b83a313b
MB
441 if (map->work_buf == NULL) {
442 ret = -ENOMEM;
5204f5e3 443 goto err_map;
b83a313b
MB
444 }
445
d3c242e1 446 regmap_debugfs_init(map, config->name);
052d2cd1 447
e5e3b8ab 448 ret = regcache_init(map, config);
5d1729e7 449 if (ret < 0)
58072cbf 450 goto err_free_workbuf;
5d1729e7 451
72b39f6f
MB
452 /* Add a devres resource for dev_get_regmap() */
453 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
454 if (!m) {
455 ret = -ENOMEM;
456 goto err_cache;
457 }
458 *m = map;
459 devres_add(dev, m);
460
b83a313b
MB
461 return map;
462
72b39f6f
MB
463err_cache:
464 regcache_exit(map);
58072cbf
LPC
465err_free_workbuf:
466 kfree(map->work_buf);
b83a313b
MB
467err_map:
468 kfree(map);
469err:
470 return ERR_PTR(ret);
471}
472EXPORT_SYMBOL_GPL(regmap_init);
473
c0eb4676
MB
474static void devm_regmap_release(struct device *dev, void *res)
475{
476 regmap_exit(*(struct regmap **)res);
477}
478
479/**
480 * devm_regmap_init(): Initialise managed register map
481 *
482 * @dev: Device that will be interacted with
483 * @bus: Bus-specific callbacks to use with device
0135bbcc 484 * @bus_context: Data passed to bus-specific callbacks
c0eb4676
MB
485 * @config: Configuration for register map
486 *
487 * The return value will be an ERR_PTR() on error or a valid pointer
488 * to a struct regmap. This function should generally not be called
489 * directly, it should be called by bus-specific init functions. The
490 * map will be automatically freed by the device management code.
491 */
492struct regmap *devm_regmap_init(struct device *dev,
493 const struct regmap_bus *bus,
0135bbcc 494 void *bus_context,
c0eb4676
MB
495 const struct regmap_config *config)
496{
497 struct regmap **ptr, *regmap;
498
499 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
500 if (!ptr)
501 return ERR_PTR(-ENOMEM);
502
0135bbcc 503 regmap = regmap_init(dev, bus, bus_context, config);
c0eb4676
MB
504 if (!IS_ERR(regmap)) {
505 *ptr = regmap;
506 devres_add(dev, ptr);
507 } else {
508 devres_free(ptr);
509 }
510
511 return regmap;
512}
513EXPORT_SYMBOL_GPL(devm_regmap_init);
514
bf315173
MB
515/**
516 * regmap_reinit_cache(): Reinitialise the current register cache
517 *
518 * @map: Register map to operate on.
519 * @config: New configuration. Only the cache data will be used.
520 *
521 * Discard any existing register cache for the map and initialize a
522 * new cache. This can be used to restore the cache to defaults or to
523 * update the cache configuration to reflect runtime discovery of the
524 * hardware.
525 */
526int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
527{
528 int ret;
529
bacdbe07 530 map->lock(map);
bf315173
MB
531
532 regcache_exit(map);
a24f64a6 533 regmap_debugfs_exit(map);
bf315173
MB
534
535 map->max_register = config->max_register;
536 map->writeable_reg = config->writeable_reg;
537 map->readable_reg = config->readable_reg;
538 map->volatile_reg = config->volatile_reg;
539 map->precious_reg = config->precious_reg;
540 map->cache_type = config->cache_type;
541
d3c242e1 542 regmap_debugfs_init(map, config->name);
a24f64a6 543
421e8d2d
MB
544 map->cache_bypass = false;
545 map->cache_only = false;
546
bf315173
MB
547 ret = regcache_init(map, config);
548
bacdbe07 549 map->unlock(map);
bf315173
MB
550
551 return ret;
552}
553
b83a313b
MB
554/**
555 * regmap_exit(): Free a previously allocated register map
556 */
557void regmap_exit(struct regmap *map)
558{
5d1729e7 559 regcache_exit(map);
31244e39 560 regmap_debugfs_exit(map);
0135bbcc
SW
561 if (map->bus->free_context)
562 map->bus->free_context(map->bus_context);
b83a313b 563 kfree(map->work_buf);
b83a313b
MB
564 kfree(map);
565}
566EXPORT_SYMBOL_GPL(regmap_exit);
567
72b39f6f
MB
568static int dev_get_regmap_match(struct device *dev, void *res, void *data)
569{
570 struct regmap **r = res;
571 if (!r || !*r) {
572 WARN_ON(!r || !*r);
573 return 0;
574 }
575
576 /* If the user didn't specify a name match any */
577 if (data)
578 return (*r)->name == data;
579 else
580 return 1;
581}
582
583/**
584 * dev_get_regmap(): Obtain the regmap (if any) for a device
585 *
586 * @dev: Device to retrieve the map for
587 * @name: Optional name for the register map, usually NULL.
588 *
589 * Returns the regmap for the device if one is present, or NULL. If
590 * name is specified then it must match the name specified when
591 * registering the device, if it is NULL then the first regmap found
592 * will be used. Devices with multiple register maps are very rare,
593 * generic code should normally not need to specify a name.
594 */
595struct regmap *dev_get_regmap(struct device *dev, const char *name)
596{
597 struct regmap **r = devres_find(dev, dev_get_regmap_release,
598 dev_get_regmap_match, (void *)name);
599
600 if (!r)
601 return NULL;
602 return *r;
603}
604EXPORT_SYMBOL_GPL(dev_get_regmap);
605
b83a313b
MB
606static int _regmap_raw_write(struct regmap *map, unsigned int reg,
607 const void *val, size_t val_len)
608{
6f306441 609 u8 *u8 = map->work_buf;
b83a313b
MB
610 void *buf;
611 int ret = -ENOTSUPP;
612 size_t len;
73304781
MB
613 int i;
614
615 /* Check for unwritable registers before we start */
616 if (map->writeable_reg)
617 for (i = 0; i < val_len / map->format.val_bytes; i++)
f01ee60f
SW
618 if (!map->writeable_reg(map->dev,
619 reg + (i * map->reg_stride)))
73304781 620 return -EINVAL;
b83a313b 621
c9157198
LD
622 if (!map->cache_bypass && map->format.parse_val) {
623 unsigned int ival;
624 int val_bytes = map->format.val_bytes;
625 for (i = 0; i < val_len / val_bytes; i++) {
626 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
627 ival = map->format.parse_val(map->work_buf);
f01ee60f
SW
628 ret = regcache_write(map, reg + (i * map->reg_stride),
629 ival);
c9157198
LD
630 if (ret) {
631 dev_err(map->dev,
632 "Error in caching of register: %u ret: %d\n",
633 reg + i, ret);
634 return ret;
635 }
636 }
637 if (map->cache_only) {
638 map->cache_dirty = true;
639 return 0;
640 }
641 }
642
d939fb9a 643 map->format.format_reg(map->work_buf, reg, map->reg_shift);
b83a313b 644
6f306441
LPC
645 u8[0] |= map->write_flag_mask;
646
fb2736bb
MB
647 trace_regmap_hw_write_start(map->dev, reg,
648 val_len / map->format.val_bytes);
649
2547e201
MB
650 /* If we're doing a single register write we can probably just
651 * send the work_buf directly, otherwise try to do a gather
652 * write.
653 */
82159ba8
MB
654 if (val == (map->work_buf + map->format.pad_bytes +
655 map->format.reg_bytes))
0135bbcc 656 ret = map->bus->write(map->bus_context, map->work_buf,
82159ba8
MB
657 map->format.reg_bytes +
658 map->format.pad_bytes +
659 val_len);
2547e201 660 else if (map->bus->gather_write)
0135bbcc 661 ret = map->bus->gather_write(map->bus_context, map->work_buf,
82159ba8
MB
662 map->format.reg_bytes +
663 map->format.pad_bytes,
b83a313b
MB
664 val, val_len);
665
2547e201 666 /* If that didn't work fall back on linearising by hand. */
b83a313b 667 if (ret == -ENOTSUPP) {
82159ba8
MB
668 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
669 buf = kzalloc(len, GFP_KERNEL);
b83a313b
MB
670 if (!buf)
671 return -ENOMEM;
672
673 memcpy(buf, map->work_buf, map->format.reg_bytes);
82159ba8
MB
674 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
675 val, val_len);
0135bbcc 676 ret = map->bus->write(map->bus_context, buf, len);
b83a313b
MB
677
678 kfree(buf);
679 }
680
fb2736bb
MB
681 trace_regmap_hw_write_done(map->dev, reg,
682 val_len / map->format.val_bytes);
683
b83a313b
MB
684 return ret;
685}
686
4d2dc095
DP
687int _regmap_write(struct regmap *map, unsigned int reg,
688 unsigned int val)
b83a313b 689{
fb2736bb 690 int ret;
b83a313b
MB
691 BUG_ON(!map->format.format_write && !map->format.format_val);
692
c9157198 693 if (!map->cache_bypass && map->format.format_write) {
5d1729e7
DP
694 ret = regcache_write(map, reg, val);
695 if (ret != 0)
696 return ret;
8ae0d7e8
MB
697 if (map->cache_only) {
698 map->cache_dirty = true;
5d1729e7 699 return 0;
8ae0d7e8 700 }
5d1729e7
DP
701 }
702
fb2736bb
MB
703 trace_regmap_reg_write(map->dev, reg, val);
704
b83a313b
MB
705 if (map->format.format_write) {
706 map->format.format_write(map, reg, val);
707
fb2736bb
MB
708 trace_regmap_hw_write_start(map->dev, reg, 1);
709
0135bbcc 710 ret = map->bus->write(map->bus_context, map->work_buf,
fb2736bb
MB
711 map->format.buf_size);
712
713 trace_regmap_hw_write_done(map->dev, reg, 1);
714
715 return ret;
b83a313b 716 } else {
82159ba8 717 map->format.format_val(map->work_buf + map->format.reg_bytes
d939fb9a 718 + map->format.pad_bytes, val, 0);
b83a313b 719 return _regmap_raw_write(map, reg,
82159ba8
MB
720 map->work_buf +
721 map->format.reg_bytes +
722 map->format.pad_bytes,
b83a313b
MB
723 map->format.val_bytes);
724 }
725}
726
727/**
728 * regmap_write(): Write a value to a single register
729 *
730 * @map: Register map to write to
731 * @reg: Register to write to
732 * @val: Value to be written
733 *
734 * A value of zero will be returned on success, a negative errno will
735 * be returned in error cases.
736 */
737int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
738{
739 int ret;
740
f01ee60f
SW
741 if (reg % map->reg_stride)
742 return -EINVAL;
743
bacdbe07 744 map->lock(map);
b83a313b
MB
745
746 ret = _regmap_write(map, reg, val);
747
bacdbe07 748 map->unlock(map);
b83a313b
MB
749
750 return ret;
751}
752EXPORT_SYMBOL_GPL(regmap_write);
753
754/**
755 * regmap_raw_write(): Write raw values to one or more registers
756 *
757 * @map: Register map to write to
758 * @reg: Initial register to write to
759 * @val: Block of data to be written, laid out for direct transmission to the
760 * device
761 * @val_len: Length of data pointed to by val.
762 *
763 * This function is intended to be used for things like firmware
764 * download where a large block of data needs to be transferred to the
765 * device. No formatting will be done on the data provided.
766 *
767 * A value of zero will be returned on success, a negative errno will
768 * be returned in error cases.
769 */
770int regmap_raw_write(struct regmap *map, unsigned int reg,
771 const void *val, size_t val_len)
772{
773 int ret;
774
851960ba
SW
775 if (val_len % map->format.val_bytes)
776 return -EINVAL;
f01ee60f
SW
777 if (reg % map->reg_stride)
778 return -EINVAL;
851960ba 779
bacdbe07 780 map->lock(map);
b83a313b
MB
781
782 ret = _regmap_raw_write(map, reg, val, val_len);
783
bacdbe07 784 map->unlock(map);
b83a313b
MB
785
786 return ret;
787}
788EXPORT_SYMBOL_GPL(regmap_raw_write);
789
8eaeb219
LD
790/*
791 * regmap_bulk_write(): Write multiple registers to the device
792 *
793 * @map: Register map to write to
794 * @reg: First register to be write from
795 * @val: Block of data to be written, in native register size for device
796 * @val_count: Number of registers to write
797 *
798 * This function is intended to be used for writing a large block of
799 * data to be device either in single transfer or multiple transfer.
800 *
801 * A value of zero will be returned on success, a negative errno will
802 * be returned in error cases.
803 */
804int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
805 size_t val_count)
806{
807 int ret = 0, i;
808 size_t val_bytes = map->format.val_bytes;
809 void *wval;
810
811 if (!map->format.parse_val)
812 return -EINVAL;
f01ee60f
SW
813 if (reg % map->reg_stride)
814 return -EINVAL;
8eaeb219 815
bacdbe07 816 map->lock(map);
8eaeb219
LD
817
818 /* No formatting is require if val_byte is 1 */
819 if (val_bytes == 1) {
820 wval = (void *)val;
821 } else {
822 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
823 if (!wval) {
824 ret = -ENOMEM;
825 dev_err(map->dev, "Error in memory allocation\n");
826 goto out;
827 }
828 for (i = 0; i < val_count * val_bytes; i += val_bytes)
829 map->format.parse_val(wval + i);
830 }
2e33caf1
AJ
831 /*
832 * Some devices does not support bulk write, for
833 * them we have a series of single write operations.
834 */
835 if (map->use_single_rw) {
836 for (i = 0; i < val_count; i++) {
837 ret = regmap_raw_write(map,
838 reg + (i * map->reg_stride),
839 val + (i * val_bytes),
840 val_bytes);
841 if (ret != 0)
842 return ret;
843 }
844 } else {
845 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
846 }
8eaeb219
LD
847
848 if (val_bytes != 1)
849 kfree(wval);
850
851out:
bacdbe07 852 map->unlock(map);
8eaeb219
LD
853 return ret;
854}
855EXPORT_SYMBOL_GPL(regmap_bulk_write);
856
b83a313b
MB
857static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
858 unsigned int val_len)
859{
860 u8 *u8 = map->work_buf;
861 int ret;
862
d939fb9a 863 map->format.format_reg(map->work_buf, reg, map->reg_shift);
b83a313b
MB
864
865 /*
6f306441 866 * Some buses or devices flag reads by setting the high bits in the
b83a313b
MB
867 * register addresss; since it's always the high bits for all
868 * current formats we can do this here rather than in
869 * formatting. This may break if we get interesting formats.
870 */
6f306441 871 u8[0] |= map->read_flag_mask;
b83a313b 872
fb2736bb
MB
873 trace_regmap_hw_read_start(map->dev, reg,
874 val_len / map->format.val_bytes);
875
0135bbcc 876 ret = map->bus->read(map->bus_context, map->work_buf,
82159ba8 877 map->format.reg_bytes + map->format.pad_bytes,
40c5cc26 878 val, val_len);
b83a313b 879
fb2736bb
MB
880 trace_regmap_hw_read_done(map->dev, reg,
881 val_len / map->format.val_bytes);
882
883 return ret;
b83a313b
MB
884}
885
886static int _regmap_read(struct regmap *map, unsigned int reg,
887 unsigned int *val)
888{
889 int ret;
890
5d1729e7
DP
891 if (!map->cache_bypass) {
892 ret = regcache_read(map, reg, val);
893 if (ret == 0)
894 return 0;
895 }
896
19254411
LPC
897 if (!map->format.parse_val)
898 return -EINVAL;
899
5d1729e7
DP
900 if (map->cache_only)
901 return -EBUSY;
902
b83a313b 903 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
fb2736bb 904 if (ret == 0) {
b83a313b 905 *val = map->format.parse_val(map->work_buf);
fb2736bb
MB
906 trace_regmap_reg_read(map->dev, reg, *val);
907 }
b83a313b 908
f2985367
MB
909 if (ret == 0 && !map->cache_bypass)
910 regcache_write(map, reg, *val);
911
b83a313b
MB
912 return ret;
913}
914
915/**
916 * regmap_read(): Read a value from a single register
917 *
918 * @map: Register map to write to
919 * @reg: Register to be read from
920 * @val: Pointer to store read value
921 *
922 * A value of zero will be returned on success, a negative errno will
923 * be returned in error cases.
924 */
925int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
926{
927 int ret;
928
f01ee60f
SW
929 if (reg % map->reg_stride)
930 return -EINVAL;
931
bacdbe07 932 map->lock(map);
b83a313b
MB
933
934 ret = _regmap_read(map, reg, val);
935
bacdbe07 936 map->unlock(map);
b83a313b
MB
937
938 return ret;
939}
940EXPORT_SYMBOL_GPL(regmap_read);
941
942/**
943 * regmap_raw_read(): Read raw data from the device
944 *
945 * @map: Register map to write to
946 * @reg: First register to be read from
947 * @val: Pointer to store read value
948 * @val_len: Size of data to read
949 *
950 * A value of zero will be returned on success, a negative errno will
951 * be returned in error cases.
952 */
953int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
954 size_t val_len)
955{
b8fb5ab1
MB
956 size_t val_bytes = map->format.val_bytes;
957 size_t val_count = val_len / val_bytes;
958 unsigned int v;
959 int ret, i;
04e016ad 960
851960ba
SW
961 if (val_len % map->format.val_bytes)
962 return -EINVAL;
f01ee60f
SW
963 if (reg % map->reg_stride)
964 return -EINVAL;
851960ba 965
bacdbe07 966 map->lock(map);
b83a313b 967
b8fb5ab1
MB
968 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
969 map->cache_type == REGCACHE_NONE) {
970 /* Physical block read if there's no cache involved */
971 ret = _regmap_raw_read(map, reg, val, val_len);
972
973 } else {
974 /* Otherwise go word by word for the cache; should be low
975 * cost as we expect to hit the cache.
976 */
977 for (i = 0; i < val_count; i++) {
f01ee60f
SW
978 ret = _regmap_read(map, reg + (i * map->reg_stride),
979 &v);
b8fb5ab1
MB
980 if (ret != 0)
981 goto out;
982
d939fb9a 983 map->format.format_val(val + (i * val_bytes), v, 0);
b8fb5ab1
MB
984 }
985 }
b83a313b 986
b8fb5ab1 987 out:
bacdbe07 988 map->unlock(map);
b83a313b
MB
989
990 return ret;
991}
992EXPORT_SYMBOL_GPL(regmap_raw_read);
993
994/**
995 * regmap_bulk_read(): Read multiple registers from the device
996 *
997 * @map: Register map to write to
998 * @reg: First register to be read from
999 * @val: Pointer to store read value, in native register size for device
1000 * @val_count: Number of registers to read
1001 *
1002 * A value of zero will be returned on success, a negative errno will
1003 * be returned in error cases.
1004 */
1005int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1006 size_t val_count)
1007{
1008 int ret, i;
1009 size_t val_bytes = map->format.val_bytes;
82cd9965 1010 bool vol = regmap_volatile_range(map, reg, val_count);
5d1729e7 1011
b83a313b
MB
1012 if (!map->format.parse_val)
1013 return -EINVAL;
f01ee60f
SW
1014 if (reg % map->reg_stride)
1015 return -EINVAL;
b83a313b 1016
de2d808f 1017 if (vol || map->cache_type == REGCACHE_NONE) {
2e33caf1
AJ
1018 /*
1019 * Some devices does not support bulk read, for
1020 * them we have a series of single read operations.
1021 */
1022 if (map->use_single_rw) {
1023 for (i = 0; i < val_count; i++) {
1024 ret = regmap_raw_read(map,
1025 reg + (i * map->reg_stride),
1026 val + (i * val_bytes),
1027 val_bytes);
1028 if (ret != 0)
1029 return ret;
1030 }
1031 } else {
1032 ret = regmap_raw_read(map, reg, val,
1033 val_bytes * val_count);
1034 if (ret != 0)
1035 return ret;
1036 }
de2d808f
MB
1037
1038 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1039 map->format.parse_val(val + i);
1040 } else {
1041 for (i = 0; i < val_count; i++) {
6560ffd1 1042 unsigned int ival;
f01ee60f 1043 ret = regmap_read(map, reg + (i * map->reg_stride),
25061d28 1044 &ival);
de2d808f
MB
1045 if (ret != 0)
1046 return ret;
6560ffd1 1047 memcpy(val + (i * val_bytes), &ival, val_bytes);
de2d808f
MB
1048 }
1049 }
b83a313b
MB
1050
1051 return 0;
1052}
1053EXPORT_SYMBOL_GPL(regmap_bulk_read);
1054
018690d3
MB
1055static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1056 unsigned int mask, unsigned int val,
1057 bool *change)
b83a313b
MB
1058{
1059 int ret;
d91e8db2 1060 unsigned int tmp, orig;
b83a313b 1061
bacdbe07 1062 map->lock(map);
b83a313b 1063
d91e8db2 1064 ret = _regmap_read(map, reg, &orig);
b83a313b
MB
1065 if (ret != 0)
1066 goto out;
1067
d91e8db2 1068 tmp = orig & ~mask;
b83a313b
MB
1069 tmp |= val & mask;
1070
018690d3 1071 if (tmp != orig) {
d91e8db2 1072 ret = _regmap_write(map, reg, tmp);
018690d3
MB
1073 *change = true;
1074 } else {
1075 *change = false;
1076 }
b83a313b
MB
1077
1078out:
bacdbe07 1079 map->unlock(map);
b83a313b
MB
1080
1081 return ret;
1082}
018690d3
MB
1083
1084/**
1085 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1086 *
1087 * @map: Register map to update
1088 * @reg: Register to update
1089 * @mask: Bitmask to change
1090 * @val: New value for bitmask
1091 *
1092 * Returns zero for success, a negative number on error.
1093 */
1094int regmap_update_bits(struct regmap *map, unsigned int reg,
1095 unsigned int mask, unsigned int val)
1096{
1097 bool change;
1098 return _regmap_update_bits(map, reg, mask, val, &change);
1099}
b83a313b 1100EXPORT_SYMBOL_GPL(regmap_update_bits);
31244e39 1101
018690d3
MB
1102/**
1103 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1104 * register map and report if updated
1105 *
1106 * @map: Register map to update
1107 * @reg: Register to update
1108 * @mask: Bitmask to change
1109 * @val: New value for bitmask
1110 * @change: Boolean indicating if a write was done
1111 *
1112 * Returns zero for success, a negative number on error.
1113 */
1114int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1115 unsigned int mask, unsigned int val,
1116 bool *change)
1117{
1118 return _regmap_update_bits(map, reg, mask, val, change);
1119}
1120EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1121
22f0d90a
MB
1122/**
1123 * regmap_register_patch: Register and apply register updates to be applied
1124 * on device initialistion
1125 *
1126 * @map: Register map to apply updates to.
1127 * @regs: Values to update.
1128 * @num_regs: Number of entries in regs.
1129 *
1130 * Register a set of register updates to be applied to the device
1131 * whenever the device registers are synchronised with the cache and
1132 * apply them immediately. Typically this is used to apply
1133 * corrections to be applied to the device defaults on startup, such
1134 * as the updates some vendors provide to undocumented registers.
1135 */
1136int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1137 int num_regs)
1138{
1139 int i, ret;
1140 bool bypass;
1141
1142 /* If needed the implementation can be extended to support this */
1143 if (map->patch)
1144 return -EBUSY;
1145
bacdbe07 1146 map->lock(map);
22f0d90a
MB
1147
1148 bypass = map->cache_bypass;
1149
1150 map->cache_bypass = true;
1151
1152 /* Write out first; it's useful to apply even if we fail later. */
1153 for (i = 0; i < num_regs; i++) {
1154 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1155 if (ret != 0) {
1156 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1157 regs[i].reg, regs[i].def, ret);
1158 goto out;
1159 }
1160 }
1161
2a14d7d9 1162 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
22f0d90a
MB
1163 if (map->patch != NULL) {
1164 memcpy(map->patch, regs,
1165 num_regs * sizeof(struct reg_default));
1166 map->patch_regs = num_regs;
1167 } else {
1168 ret = -ENOMEM;
1169 }
1170
1171out:
1172 map->cache_bypass = bypass;
1173
bacdbe07 1174 map->unlock(map);
22f0d90a
MB
1175
1176 return ret;
1177}
1178EXPORT_SYMBOL_GPL(regmap_register_patch);
1179
eae4b51b 1180/*
a6539c32
MB
1181 * regmap_get_val_bytes(): Report the size of a register value
1182 *
1183 * Report the size of a register value, mainly intended to for use by
1184 * generic infrastructure built on top of regmap.
1185 */
1186int regmap_get_val_bytes(struct regmap *map)
1187{
1188 if (map->format.format_write)
1189 return -EINVAL;
1190
1191 return map->format.val_bytes;
1192}
1193EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1194
31244e39
MB
1195static int __init regmap_initcall(void)
1196{
1197 regmap_debugfs_initcall();
1198
1199 return 0;
1200}
1201postcore_initcall(regmap_initcall);
This page took 0.113919 seconds and 5 git commands to generate.