regmap: Fix future missing prototype of devres_alloc() and friends
[pandora-kernel.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/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
18
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/regmap.h>
21
22 #include "internal.h"
23
24 bool 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
35 bool regmap_readable(struct regmap *map, unsigned int reg)
36 {
37         if (map->max_register && reg > map->max_register)
38                 return false;
39
40         if (map->format.format_write)
41                 return false;
42
43         if (map->readable_reg)
44                 return map->readable_reg(map->dev, reg);
45
46         return true;
47 }
48
49 bool regmap_volatile(struct regmap *map, unsigned int reg)
50 {
51         if (!regmap_readable(map, reg))
52                 return false;
53
54         if (map->volatile_reg)
55                 return map->volatile_reg(map->dev, reg);
56
57         return true;
58 }
59
60 bool regmap_precious(struct regmap *map, unsigned int reg)
61 {
62         if (!regmap_readable(map, reg))
63                 return false;
64
65         if (map->precious_reg)
66                 return map->precious_reg(map->dev, reg);
67
68         return false;
69 }
70
71 static 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
83 static 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
91 static 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
98 static 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
105 static 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
115 static void regmap_format_8(void *buf, unsigned int val)
116 {
117         u8 *b = buf;
118
119         b[0] = val;
120 }
121
122 static void regmap_format_16(void *buf, unsigned int val)
123 {
124         __be16 *b = buf;
125
126         b[0] = cpu_to_be16(val);
127 }
128
129 static void regmap_format_32(void *buf, unsigned int val)
130 {
131         __be32 *b = buf;
132
133         b[0] = cpu_to_be32(val);
134 }
135
136 static unsigned int regmap_parse_8(void *buf)
137 {
138         u8 *b = buf;
139
140         return b[0];
141 }
142
143 static unsigned int regmap_parse_16(void *buf)
144 {
145         __be16 *b = buf;
146
147         b[0] = be16_to_cpu(b[0]);
148
149         return b[0];
150 }
151
152 static unsigned int regmap_parse_32(void *buf)
153 {
154         __be32 *b = buf;
155
156         b[0] = be32_to_cpu(b[0]);
157
158         return b[0];
159 }
160
161 /**
162  * regmap_init(): Initialise register map
163  *
164  * @dev: Device that will be interacted with
165  * @bus: Bus-specific callbacks to use with device
166  * @config: Configuration for register map
167  *
168  * The return value will be an ERR_PTR() on error or a valid pointer to
169  * a struct regmap.  This function should generally not be called
170  * directly, it should be called by bus-specific init functions.
171  */
172 struct regmap *regmap_init(struct device *dev,
173                            const struct regmap_bus *bus,
174                            const struct regmap_config *config)
175 {
176         struct regmap *map;
177         int ret = -EINVAL;
178
179         if (!bus || !config)
180                 goto err;
181
182         map = kzalloc(sizeof(*map), GFP_KERNEL);
183         if (map == NULL) {
184                 ret = -ENOMEM;
185                 goto err;
186         }
187
188         mutex_init(&map->lock);
189         map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
190         map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
191         map->format.pad_bytes = config->pad_bits / 8;
192         map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
193         map->format.buf_size += map->format.pad_bytes;
194         map->dev = dev;
195         map->bus = bus;
196         map->max_register = config->max_register;
197         map->writeable_reg = config->writeable_reg;
198         map->readable_reg = config->readable_reg;
199         map->volatile_reg = config->volatile_reg;
200         map->precious_reg = config->precious_reg;
201         map->cache_type = config->cache_type;
202
203         if (config->read_flag_mask || config->write_flag_mask) {
204                 map->read_flag_mask = config->read_flag_mask;
205                 map->write_flag_mask = config->write_flag_mask;
206         } else {
207                 map->read_flag_mask = bus->read_flag_mask;
208         }
209
210         switch (config->reg_bits) {
211         case 2:
212                 switch (config->val_bits) {
213                 case 6:
214                         map->format.format_write = regmap_format_2_6_write;
215                         break;
216                 default:
217                         goto err_map;
218                 }
219                 break;
220
221         case 4:
222                 switch (config->val_bits) {
223                 case 12:
224                         map->format.format_write = regmap_format_4_12_write;
225                         break;
226                 default:
227                         goto err_map;
228                 }
229                 break;
230
231         case 7:
232                 switch (config->val_bits) {
233                 case 9:
234                         map->format.format_write = regmap_format_7_9_write;
235                         break;
236                 default:
237                         goto err_map;
238                 }
239                 break;
240
241         case 10:
242                 switch (config->val_bits) {
243                 case 14:
244                         map->format.format_write = regmap_format_10_14_write;
245                         break;
246                 default:
247                         goto err_map;
248                 }
249                 break;
250
251         case 8:
252                 map->format.format_reg = regmap_format_8;
253                 break;
254
255         case 16:
256                 map->format.format_reg = regmap_format_16;
257                 break;
258
259         case 32:
260                 map->format.format_reg = regmap_format_32;
261                 break;
262
263         default:
264                 goto err_map;
265         }
266
267         switch (config->val_bits) {
268         case 8:
269                 map->format.format_val = regmap_format_8;
270                 map->format.parse_val = regmap_parse_8;
271                 break;
272         case 16:
273                 map->format.format_val = regmap_format_16;
274                 map->format.parse_val = regmap_parse_16;
275                 break;
276         case 32:
277                 map->format.format_val = regmap_format_32;
278                 map->format.parse_val = regmap_parse_32;
279                 break;
280         }
281
282         if (!map->format.format_write &&
283             !(map->format.format_reg && map->format.format_val))
284                 goto err_map;
285
286         map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
287         if (map->work_buf == NULL) {
288                 ret = -ENOMEM;
289                 goto err_map;
290         }
291
292         regmap_debugfs_init(map);
293
294         ret = regcache_init(map, config);
295         if (ret < 0)
296                 goto err_free_workbuf;
297
298         return map;
299
300 err_free_workbuf:
301         kfree(map->work_buf);
302 err_map:
303         kfree(map);
304 err:
305         return ERR_PTR(ret);
306 }
307 EXPORT_SYMBOL_GPL(regmap_init);
308
309 /**
310  * regmap_reinit_cache(): Reinitialise the current register cache
311  *
312  * @map: Register map to operate on.
313  * @config: New configuration.  Only the cache data will be used.
314  *
315  * Discard any existing register cache for the map and initialize a
316  * new cache.  This can be used to restore the cache to defaults or to
317  * update the cache configuration to reflect runtime discovery of the
318  * hardware.
319  */
320 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
321 {
322         int ret;
323
324         mutex_lock(&map->lock);
325
326         regcache_exit(map);
327         regmap_debugfs_exit(map);
328
329         map->max_register = config->max_register;
330         map->writeable_reg = config->writeable_reg;
331         map->readable_reg = config->readable_reg;
332         map->volatile_reg = config->volatile_reg;
333         map->precious_reg = config->precious_reg;
334         map->cache_type = config->cache_type;
335
336         regmap_debugfs_init(map);
337
338         ret = regcache_init(map, config);
339
340         mutex_unlock(&map->lock);
341
342         return ret;
343 }
344
345 /**
346  * regmap_exit(): Free a previously allocated register map
347  */
348 void regmap_exit(struct regmap *map)
349 {
350         regcache_exit(map);
351         regmap_debugfs_exit(map);
352         kfree(map->work_buf);
353         kfree(map);
354 }
355 EXPORT_SYMBOL_GPL(regmap_exit);
356
357 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
358                              const void *val, size_t val_len)
359 {
360         u8 *u8 = map->work_buf;
361         void *buf;
362         int ret = -ENOTSUPP;
363         size_t len;
364         int i;
365
366         /* Check for unwritable registers before we start */
367         if (map->writeable_reg)
368                 for (i = 0; i < val_len / map->format.val_bytes; i++)
369                         if (!map->writeable_reg(map->dev, reg + i))
370                                 return -EINVAL;
371
372         map->format.format_reg(map->work_buf, reg);
373
374         u8[0] |= map->write_flag_mask;
375
376         trace_regmap_hw_write_start(map->dev, reg,
377                                     val_len / map->format.val_bytes);
378
379         /* If we're doing a single register write we can probably just
380          * send the work_buf directly, otherwise try to do a gather
381          * write.
382          */
383         if (val == (map->work_buf + map->format.pad_bytes +
384                     map->format.reg_bytes))
385                 ret = map->bus->write(map->dev, map->work_buf,
386                                       map->format.reg_bytes +
387                                       map->format.pad_bytes +
388                                       val_len);
389         else if (map->bus->gather_write)
390                 ret = map->bus->gather_write(map->dev, map->work_buf,
391                                              map->format.reg_bytes +
392                                              map->format.pad_bytes,
393                                              val, val_len);
394
395         /* If that didn't work fall back on linearising by hand. */
396         if (ret == -ENOTSUPP) {
397                 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
398                 buf = kzalloc(len, GFP_KERNEL);
399                 if (!buf)
400                         return -ENOMEM;
401
402                 memcpy(buf, map->work_buf, map->format.reg_bytes);
403                 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
404                        val, val_len);
405                 ret = map->bus->write(map->dev, buf, len);
406
407                 kfree(buf);
408         }
409
410         trace_regmap_hw_write_done(map->dev, reg,
411                                    val_len / map->format.val_bytes);
412
413         return ret;
414 }
415
416 int _regmap_write(struct regmap *map, unsigned int reg,
417                   unsigned int val)
418 {
419         int ret;
420         BUG_ON(!map->format.format_write && !map->format.format_val);
421
422         if (!map->cache_bypass) {
423                 ret = regcache_write(map, reg, val);
424                 if (ret != 0)
425                         return ret;
426                 if (map->cache_only) {
427                         map->cache_dirty = true;
428                         return 0;
429                 }
430         }
431
432         trace_regmap_reg_write(map->dev, reg, val);
433
434         if (map->format.format_write) {
435                 map->format.format_write(map, reg, val);
436
437                 trace_regmap_hw_write_start(map->dev, reg, 1);
438
439                 ret = map->bus->write(map->dev, map->work_buf,
440                                       map->format.buf_size);
441
442                 trace_regmap_hw_write_done(map->dev, reg, 1);
443
444                 return ret;
445         } else {
446                 map->format.format_val(map->work_buf + map->format.reg_bytes
447                                        + map->format.pad_bytes, val);
448                 return _regmap_raw_write(map, reg,
449                                          map->work_buf +
450                                          map->format.reg_bytes +
451                                          map->format.pad_bytes,
452                                          map->format.val_bytes);
453         }
454 }
455
456 /**
457  * regmap_write(): Write a value to a single register
458  *
459  * @map: Register map to write to
460  * @reg: Register to write to
461  * @val: Value to be written
462  *
463  * A value of zero will be returned on success, a negative errno will
464  * be returned in error cases.
465  */
466 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
467 {
468         int ret;
469
470         mutex_lock(&map->lock);
471
472         ret = _regmap_write(map, reg, val);
473
474         mutex_unlock(&map->lock);
475
476         return ret;
477 }
478 EXPORT_SYMBOL_GPL(regmap_write);
479
480 /**
481  * regmap_raw_write(): Write raw values to one or more registers
482  *
483  * @map: Register map to write to
484  * @reg: Initial register to write to
485  * @val: Block of data to be written, laid out for direct transmission to the
486  *       device
487  * @val_len: Length of data pointed to by val.
488  *
489  * This function is intended to be used for things like firmware
490  * download where a large block of data needs to be transferred to the
491  * device.  No formatting will be done on the data provided.
492  *
493  * A value of zero will be returned on success, a negative errno will
494  * be returned in error cases.
495  */
496 int regmap_raw_write(struct regmap *map, unsigned int reg,
497                      const void *val, size_t val_len)
498 {
499         size_t val_count = val_len / map->format.val_bytes;
500         int ret;
501
502         WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
503                 map->cache_type != REGCACHE_NONE);
504
505         mutex_lock(&map->lock);
506
507         ret = _regmap_raw_write(map, reg, val, val_len);
508
509         mutex_unlock(&map->lock);
510
511         return ret;
512 }
513 EXPORT_SYMBOL_GPL(regmap_raw_write);
514
515 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
516                             unsigned int val_len)
517 {
518         u8 *u8 = map->work_buf;
519         int ret;
520
521         map->format.format_reg(map->work_buf, reg);
522
523         /*
524          * Some buses or devices flag reads by setting the high bits in the
525          * register addresss; since it's always the high bits for all
526          * current formats we can do this here rather than in
527          * formatting.  This may break if we get interesting formats.
528          */
529         u8[0] |= map->read_flag_mask;
530
531         trace_regmap_hw_read_start(map->dev, reg,
532                                    val_len / map->format.val_bytes);
533
534         ret = map->bus->read(map->dev, map->work_buf,
535                              map->format.reg_bytes + map->format.pad_bytes,
536                              val, val_len);
537
538         trace_regmap_hw_read_done(map->dev, reg,
539                                   val_len / map->format.val_bytes);
540
541         return ret;
542 }
543
544 static int _regmap_read(struct regmap *map, unsigned int reg,
545                         unsigned int *val)
546 {
547         int ret;
548
549         if (!map->cache_bypass) {
550                 ret = regcache_read(map, reg, val);
551                 if (ret == 0)
552                         return 0;
553         }
554
555         if (!map->format.parse_val)
556                 return -EINVAL;
557
558         if (map->cache_only)
559                 return -EBUSY;
560
561         ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
562         if (ret == 0) {
563                 *val = map->format.parse_val(map->work_buf);
564                 trace_regmap_reg_read(map->dev, reg, *val);
565         }
566
567         return ret;
568 }
569
570 /**
571  * regmap_read(): Read a value from a single register
572  *
573  * @map: Register map to write to
574  * @reg: Register to be read from
575  * @val: Pointer to store read value
576  *
577  * A value of zero will be returned on success, a negative errno will
578  * be returned in error cases.
579  */
580 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
581 {
582         int ret;
583
584         mutex_lock(&map->lock);
585
586         ret = _regmap_read(map, reg, val);
587
588         mutex_unlock(&map->lock);
589
590         return ret;
591 }
592 EXPORT_SYMBOL_GPL(regmap_read);
593
594 /**
595  * regmap_raw_read(): Read raw data from the device
596  *
597  * @map: Register map to write to
598  * @reg: First register to be read from
599  * @val: Pointer to store read value
600  * @val_len: Size of data to read
601  *
602  * A value of zero will be returned on success, a negative errno will
603  * be returned in error cases.
604  */
605 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
606                     size_t val_len)
607 {
608         size_t val_bytes = map->format.val_bytes;
609         size_t val_count = val_len / val_bytes;
610         unsigned int v;
611         int ret, i;
612
613         mutex_lock(&map->lock);
614
615         if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
616             map->cache_type == REGCACHE_NONE) {
617                 /* Physical block read if there's no cache involved */
618                 ret = _regmap_raw_read(map, reg, val, val_len);
619
620         } else {
621                 /* Otherwise go word by word for the cache; should be low
622                  * cost as we expect to hit the cache.
623                  */
624                 for (i = 0; i < val_count; i++) {
625                         ret = _regmap_read(map, reg + i, &v);
626                         if (ret != 0)
627                                 goto out;
628
629                         map->format.format_val(val + (i * val_bytes), v);
630                 }
631         }
632
633  out:
634         mutex_unlock(&map->lock);
635
636         return ret;
637 }
638 EXPORT_SYMBOL_GPL(regmap_raw_read);
639
640 /**
641  * regmap_bulk_read(): Read multiple registers from the device
642  *
643  * @map: Register map to write to
644  * @reg: First register to be read from
645  * @val: Pointer to store read value, in native register size for device
646  * @val_count: Number of registers to read
647  *
648  * A value of zero will be returned on success, a negative errno will
649  * be returned in error cases.
650  */
651 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
652                      size_t val_count)
653 {
654         int ret, i;
655         size_t val_bytes = map->format.val_bytes;
656         bool vol = regmap_volatile_range(map, reg, val_count);
657
658         if (!map->format.parse_val)
659                 return -EINVAL;
660
661         if (vol || map->cache_type == REGCACHE_NONE) {
662                 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
663                 if (ret != 0)
664                         return ret;
665
666                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
667                         map->format.parse_val(val + i);
668         } else {
669                 for (i = 0; i < val_count; i++) {
670                         ret = regmap_read(map, reg + i, val + (i * val_bytes));
671                         if (ret != 0)
672                                 return ret;
673                 }
674         }
675
676         return 0;
677 }
678 EXPORT_SYMBOL_GPL(regmap_bulk_read);
679
680 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
681                                unsigned int mask, unsigned int val,
682                                bool *change)
683 {
684         int ret;
685         unsigned int tmp, orig;
686
687         mutex_lock(&map->lock);
688
689         ret = _regmap_read(map, reg, &orig);
690         if (ret != 0)
691                 goto out;
692
693         tmp = orig & ~mask;
694         tmp |= val & mask;
695
696         if (tmp != orig) {
697                 ret = _regmap_write(map, reg, tmp);
698                 *change = true;
699         } else {
700                 *change = false;
701         }
702
703 out:
704         mutex_unlock(&map->lock);
705
706         return ret;
707 }
708
709 /**
710  * regmap_update_bits: Perform a read/modify/write cycle on the register map
711  *
712  * @map: Register map to update
713  * @reg: Register to update
714  * @mask: Bitmask to change
715  * @val: New value for bitmask
716  *
717  * Returns zero for success, a negative number on error.
718  */
719 int regmap_update_bits(struct regmap *map, unsigned int reg,
720                        unsigned int mask, unsigned int val)
721 {
722         bool change;
723         return _regmap_update_bits(map, reg, mask, val, &change);
724 }
725 EXPORT_SYMBOL_GPL(regmap_update_bits);
726
727 /**
728  * regmap_update_bits_check: Perform a read/modify/write cycle on the
729  *                           register map and report if updated
730  *
731  * @map: Register map to update
732  * @reg: Register to update
733  * @mask: Bitmask to change
734  * @val: New value for bitmask
735  * @change: Boolean indicating if a write was done
736  *
737  * Returns zero for success, a negative number on error.
738  */
739 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
740                              unsigned int mask, unsigned int val,
741                              bool *change)
742 {
743         return _regmap_update_bits(map, reg, mask, val, change);
744 }
745 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
746
747 static int __init regmap_initcall(void)
748 {
749         regmap_debugfs_initcall();
750
751         return 0;
752 }
753 postcore_initcall(regmap_initcall);