]> Pileus Git - ~andy/linux/blob - drivers/base/regmap/regmap.c
Merge branch 'topic/misc' into for-linus
[~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/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
151         if (config->read_flag_mask || config->write_flag_mask) {
152                 map->read_flag_mask = config->read_flag_mask;
153                 map->write_flag_mask = config->write_flag_mask;
154         } else {
155                 map->read_flag_mask = bus->read_flag_mask;
156         }
157
158         switch (config->reg_bits) {
159         case 4:
160                 switch (config->val_bits) {
161                 case 12:
162                         map->format.format_write = regmap_format_4_12_write;
163                         break;
164                 default:
165                         goto err_map;
166                 }
167                 break;
168
169         case 7:
170                 switch (config->val_bits) {
171                 case 9:
172                         map->format.format_write = regmap_format_7_9_write;
173                         break;
174                 default:
175                         goto err_map;
176                 }
177                 break;
178
179         case 8:
180                 map->format.format_reg = regmap_format_8;
181                 break;
182
183         case 16:
184                 map->format.format_reg = regmap_format_16;
185                 break;
186
187         default:
188                 goto err_map;
189         }
190
191         switch (config->val_bits) {
192         case 8:
193                 map->format.format_val = regmap_format_8;
194                 map->format.parse_val = regmap_parse_8;
195                 break;
196         case 16:
197                 map->format.format_val = regmap_format_16;
198                 map->format.parse_val = regmap_parse_16;
199                 break;
200         }
201
202         if (!map->format.format_write &&
203             !(map->format.format_reg && map->format.format_val))
204                 goto err_map;
205
206         map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
207         if (map->work_buf == NULL) {
208                 ret = -ENOMEM;
209                 goto err_map;
210         }
211
212         ret = regcache_init(map, config);
213         if (ret < 0)
214                 goto err_map;
215
216         regmap_debugfs_init(map);
217
218         return map;
219
220 err_map:
221         kfree(map);
222 err:
223         return ERR_PTR(ret);
224 }
225 EXPORT_SYMBOL_GPL(regmap_init);
226
227 /**
228  * regmap_reinit_cache(): Reinitialise the current register cache
229  *
230  * @map: Register map to operate on.
231  * @config: New configuration.  Only the cache data will be used.
232  *
233  * Discard any existing register cache for the map and initialize a
234  * new cache.  This can be used to restore the cache to defaults or to
235  * update the cache configuration to reflect runtime discovery of the
236  * hardware.
237  */
238 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
239 {
240         int ret;
241
242         mutex_lock(&map->lock);
243
244         regcache_exit(map);
245
246         map->max_register = config->max_register;
247         map->writeable_reg = config->writeable_reg;
248         map->readable_reg = config->readable_reg;
249         map->volatile_reg = config->volatile_reg;
250         map->precious_reg = config->precious_reg;
251         map->cache_type = config->cache_type;
252
253         ret = regcache_init(map, config);
254
255         mutex_unlock(&map->lock);
256
257         return ret;
258 }
259
260 /**
261  * regmap_exit(): Free a previously allocated register map
262  */
263 void regmap_exit(struct regmap *map)
264 {
265         regcache_exit(map);
266         regmap_debugfs_exit(map);
267         kfree(map->work_buf);
268         kfree(map);
269 }
270 EXPORT_SYMBOL_GPL(regmap_exit);
271
272 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
273                              const void *val, size_t val_len)
274 {
275         u8 *u8 = map->work_buf;
276         void *buf;
277         int ret = -ENOTSUPP;
278         size_t len;
279         int i;
280
281         /* Check for unwritable registers before we start */
282         if (map->writeable_reg)
283                 for (i = 0; i < val_len / map->format.val_bytes; i++)
284                         if (!map->writeable_reg(map->dev, reg + i))
285                                 return -EINVAL;
286
287         map->format.format_reg(map->work_buf, reg);
288
289         u8[0] |= map->write_flag_mask;
290
291         trace_regmap_hw_write_start(map->dev, reg,
292                                     val_len / map->format.val_bytes);
293
294         /* If we're doing a single register write we can probably just
295          * send the work_buf directly, otherwise try to do a gather
296          * write.
297          */
298         if (val == map->work_buf + map->format.reg_bytes)
299                 ret = map->bus->write(map->dev, map->work_buf,
300                                       map->format.reg_bytes + val_len);
301         else if (map->bus->gather_write)
302                 ret = map->bus->gather_write(map->dev, map->work_buf,
303                                              map->format.reg_bytes,
304                                              val, val_len);
305
306         /* If that didn't work fall back on linearising by hand. */
307         if (ret == -ENOTSUPP) {
308                 len = map->format.reg_bytes + val_len;
309                 buf = kmalloc(len, GFP_KERNEL);
310                 if (!buf)
311                         return -ENOMEM;
312
313                 memcpy(buf, map->work_buf, map->format.reg_bytes);
314                 memcpy(buf + map->format.reg_bytes, val, val_len);
315                 ret = map->bus->write(map->dev, buf, len);
316
317                 kfree(buf);
318         }
319
320         trace_regmap_hw_write_done(map->dev, reg,
321                                    val_len / map->format.val_bytes);
322
323         return ret;
324 }
325
326 int _regmap_write(struct regmap *map, unsigned int reg,
327                   unsigned int val)
328 {
329         int ret;
330         BUG_ON(!map->format.format_write && !map->format.format_val);
331
332         if (!map->cache_bypass) {
333                 ret = regcache_write(map, reg, val);
334                 if (ret != 0)
335                         return ret;
336                 if (map->cache_only) {
337                         map->cache_dirty = true;
338                         return 0;
339                 }
340         }
341
342         trace_regmap_reg_write(map->dev, reg, val);
343
344         if (map->format.format_write) {
345                 map->format.format_write(map, reg, val);
346
347                 trace_regmap_hw_write_start(map->dev, reg, 1);
348
349                 ret = map->bus->write(map->dev, map->work_buf,
350                                       map->format.buf_size);
351
352                 trace_regmap_hw_write_done(map->dev, reg, 1);
353
354                 return ret;
355         } else {
356                 map->format.format_val(map->work_buf + map->format.reg_bytes,
357                                        val);
358                 return _regmap_raw_write(map, reg,
359                                          map->work_buf + map->format.reg_bytes,
360                                          map->format.val_bytes);
361         }
362 }
363
364 /**
365  * regmap_write(): Write a value to a single register
366  *
367  * @map: Register map to write to
368  * @reg: Register to write to
369  * @val: Value to be written
370  *
371  * A value of zero will be returned on success, a negative errno will
372  * be returned in error cases.
373  */
374 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
375 {
376         int ret;
377
378         mutex_lock(&map->lock);
379
380         ret = _regmap_write(map, reg, val);
381
382         mutex_unlock(&map->lock);
383
384         return ret;
385 }
386 EXPORT_SYMBOL_GPL(regmap_write);
387
388 /**
389  * regmap_raw_write(): Write raw values to one or more registers
390  *
391  * @map: Register map to write to
392  * @reg: Initial register to write to
393  * @val: Block of data to be written, laid out for direct transmission to the
394  *       device
395  * @val_len: Length of data pointed to by val.
396  *
397  * This function is intended to be used for things like firmware
398  * download where a large block of data needs to be transferred to the
399  * device.  No formatting will be done on the data provided.
400  *
401  * A value of zero will be returned on success, a negative errno will
402  * be returned in error cases.
403  */
404 int regmap_raw_write(struct regmap *map, unsigned int reg,
405                      const void *val, size_t val_len)
406 {
407         int ret;
408
409         WARN_ON(map->cache_type != REGCACHE_NONE);
410
411         mutex_lock(&map->lock);
412
413         ret = _regmap_raw_write(map, reg, val, val_len);
414
415         mutex_unlock(&map->lock);
416
417         return ret;
418 }
419 EXPORT_SYMBOL_GPL(regmap_raw_write);
420
421 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
422                             unsigned int val_len)
423 {
424         u8 *u8 = map->work_buf;
425         int ret;
426
427         map->format.format_reg(map->work_buf, reg);
428
429         /*
430          * Some buses or devices flag reads by setting the high bits in the
431          * register addresss; since it's always the high bits for all
432          * current formats we can do this here rather than in
433          * formatting.  This may break if we get interesting formats.
434          */
435         u8[0] |= map->read_flag_mask;
436
437         trace_regmap_hw_read_start(map->dev, reg,
438                                    val_len / map->format.val_bytes);
439
440         ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
441                              val, val_len);
442
443         trace_regmap_hw_read_done(map->dev, reg,
444                                   val_len / map->format.val_bytes);
445
446         return ret;
447 }
448
449 static int _regmap_read(struct regmap *map, unsigned int reg,
450                         unsigned int *val)
451 {
452         int ret;
453
454         if (!map->format.parse_val)
455                 return -EINVAL;
456
457         if (!map->cache_bypass) {
458                 ret = regcache_read(map, reg, val);
459                 if (ret == 0)
460                         return 0;
461         }
462
463         if (map->cache_only)
464                 return -EBUSY;
465
466         ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
467         if (ret == 0) {
468                 *val = map->format.parse_val(map->work_buf);
469                 trace_regmap_reg_read(map->dev, reg, *val);
470         }
471
472         return ret;
473 }
474
475 /**
476  * regmap_read(): Read a value from a single register
477  *
478  * @map: Register map to write to
479  * @reg: Register to be read from
480  * @val: Pointer to store read value
481  *
482  * A value of zero will be returned on success, a negative errno will
483  * be returned in error cases.
484  */
485 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
486 {
487         int ret;
488
489         mutex_lock(&map->lock);
490
491         ret = _regmap_read(map, reg, val);
492
493         mutex_unlock(&map->lock);
494
495         return ret;
496 }
497 EXPORT_SYMBOL_GPL(regmap_read);
498
499 /**
500  * regmap_raw_read(): Read raw data from the device
501  *
502  * @map: Register map to write to
503  * @reg: First register to be read from
504  * @val: Pointer to store read value
505  * @val_len: Size of data to read
506  *
507  * A value of zero will be returned on success, a negative errno will
508  * be returned in error cases.
509  */
510 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
511                     size_t val_len)
512 {
513         int ret;
514         int i;
515         bool vol = true;
516
517         for (i = 0; i < val_len / map->format.val_bytes; i++)
518                 if (!regmap_volatile(map, reg + i))
519                         vol = false;
520
521         WARN_ON(!vol && map->cache_type != REGCACHE_NONE);
522
523         mutex_lock(&map->lock);
524
525         ret = _regmap_raw_read(map, reg, val, val_len);
526
527         mutex_unlock(&map->lock);
528
529         return ret;
530 }
531 EXPORT_SYMBOL_GPL(regmap_raw_read);
532
533 /**
534  * regmap_bulk_read(): Read multiple registers from the device
535  *
536  * @map: Register map to write to
537  * @reg: First register to be read from
538  * @val: Pointer to store read value, in native register size for device
539  * @val_count: Number of registers to read
540  *
541  * A value of zero will be returned on success, a negative errno will
542  * be returned in error cases.
543  */
544 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
545                      size_t val_count)
546 {
547         int ret, i;
548         size_t val_bytes = map->format.val_bytes;
549         bool vol = true;
550
551         if (!map->format.parse_val)
552                 return -EINVAL;
553
554         /* Is this a block of volatile registers? */
555         for (i = 0; i < val_count; i++)
556                 if (!regmap_volatile(map, reg + i))
557                         vol = false;
558
559         if (vol || map->cache_type == REGCACHE_NONE) {
560                 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
561                 if (ret != 0)
562                         return ret;
563
564                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
565                         map->format.parse_val(val + i);
566         } else {
567                 for (i = 0; i < val_count; i++) {
568                         ret = regmap_read(map, reg + i, val + (i * val_bytes));
569                         if (ret != 0)
570                                 return ret;
571                 }
572         }
573
574         return 0;
575 }
576 EXPORT_SYMBOL_GPL(regmap_bulk_read);
577
578 /**
579  * remap_update_bits: Perform a read/modify/write cycle on the register map
580  *
581  * @map: Register map to update
582  * @reg: Register to update
583  * @mask: Bitmask to change
584  * @val: New value for bitmask
585  *
586  * Returns zero for success, a negative number on error.
587  */
588 int regmap_update_bits(struct regmap *map, unsigned int reg,
589                        unsigned int mask, unsigned int val)
590 {
591         int ret;
592         unsigned int tmp;
593
594         mutex_lock(&map->lock);
595
596         ret = _regmap_read(map, reg, &tmp);
597         if (ret != 0)
598                 goto out;
599
600         tmp &= ~mask;
601         tmp |= val & mask;
602
603         ret = _regmap_write(map, reg, tmp);
604
605 out:
606         mutex_unlock(&map->lock);
607
608         return ret;
609 }
610 EXPORT_SYMBOL_GPL(regmap_update_bits);
611
612 static int __init regmap_initcall(void)
613 {
614         regmap_debugfs_initcall();
615
616         return 0;
617 }
618 postcore_initcall(regmap_initcall);