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