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