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