USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / base / regmap / regmap.c
blobbb80853ff27a9d25e1003a25edec0d21cbe8c2ad
1 /*
2 * Register map access API
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/regmap.h>
22 #include "internal.h"
24 bool regmap_writeable(struct regmap *map, unsigned int reg)
26 if (map->max_register && reg > map->max_register)
27 return false;
29 if (map->writeable_reg)
30 return map->writeable_reg(map->dev, reg);
32 return true;
35 bool regmap_readable(struct regmap *map, unsigned int reg)
37 if (map->max_register && reg > map->max_register)
38 return false;
40 if (map->format.format_write)
41 return false;
43 if (map->readable_reg)
44 return map->readable_reg(map->dev, reg);
46 return true;
49 bool regmap_volatile(struct regmap *map, unsigned int reg)
51 if (!regmap_readable(map, reg))
52 return false;
54 if (map->volatile_reg)
55 return map->volatile_reg(map->dev, reg);
57 return true;
60 bool regmap_precious(struct regmap *map, unsigned int reg)
62 if (!regmap_readable(map, reg))
63 return false;
65 if (map->precious_reg)
66 return map->precious_reg(map->dev, reg);
68 return false;
71 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
72 unsigned int num)
74 unsigned int i;
76 for (i = 0; i < num; i++)
77 if (!regmap_volatile(map, reg + i))
78 return false;
80 return true;
83 static void regmap_format_2_6_write(struct regmap *map,
84 unsigned int reg, unsigned int val)
86 u8 *out = map->work_buf;
88 *out = (reg << 6) | val;
91 static void regmap_format_4_12_write(struct regmap *map,
92 unsigned int reg, unsigned int val)
94 __be16 *out = map->work_buf;
95 *out = cpu_to_be16((reg << 12) | val);
98 static void regmap_format_7_9_write(struct regmap *map,
99 unsigned int reg, unsigned int val)
101 __be16 *out = map->work_buf;
102 *out = cpu_to_be16((reg << 9) | val);
105 static void regmap_format_10_14_write(struct regmap *map,
106 unsigned int reg, unsigned int val)
108 u8 *out = map->work_buf;
110 out[2] = val;
111 out[1] = (val >> 8) | (reg << 6);
112 out[0] = reg >> 2;
115 static void regmap_format_8(void *buf, unsigned int val)
117 u8 *b = buf;
119 b[0] = val;
122 static void regmap_format_16(void *buf, unsigned int val)
124 __be16 *b = buf;
126 b[0] = cpu_to_be16(val);
129 static void regmap_format_32(void *buf, unsigned int val)
131 __be32 *b = buf;
133 b[0] = cpu_to_be32(val);
136 static unsigned int regmap_parse_8(void *buf)
138 u8 *b = buf;
140 return b[0];
143 static unsigned int regmap_parse_16(void *buf)
145 __be16 *b = buf;
147 b[0] = be16_to_cpu(b[0]);
149 return b[0];
152 static unsigned int regmap_parse_32(void *buf)
154 __be32 *b = buf;
156 b[0] = be32_to_cpu(b[0]);
158 return b[0];
162 * regmap_init(): Initialise register map
164 * @dev: Device that will be interacted with
165 * @bus: Bus-specific callbacks to use with device
166 * @config: Configuration for register map
168 * The return value will be an ERR_PTR() on error or a valid pointer to
169 * a struct regmap. This function should generally not be called
170 * directly, it should be called by bus-specific init functions.
172 struct regmap *regmap_init(struct device *dev,
173 const struct regmap_bus *bus,
174 const struct regmap_config *config)
176 struct regmap *map;
177 int ret = -EINVAL;
179 if (!bus || !config)
180 goto err;
182 map = kzalloc(sizeof(*map), GFP_KERNEL);
183 if (map == NULL) {
184 ret = -ENOMEM;
185 goto err;
188 mutex_init(&map->lock);
189 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
190 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
191 map->format.pad_bytes = config->pad_bits / 8;
192 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
193 map->format.buf_size += map->format.pad_bytes;
194 map->dev = dev;
195 map->bus = bus;
196 map->max_register = config->max_register;
197 map->writeable_reg = config->writeable_reg;
198 map->readable_reg = config->readable_reg;
199 map->volatile_reg = config->volatile_reg;
200 map->precious_reg = config->precious_reg;
201 map->cache_type = config->cache_type;
203 if (config->read_flag_mask || config->write_flag_mask) {
204 map->read_flag_mask = config->read_flag_mask;
205 map->write_flag_mask = config->write_flag_mask;
206 } else {
207 map->read_flag_mask = bus->read_flag_mask;
210 switch (config->reg_bits) {
211 case 2:
212 switch (config->val_bits) {
213 case 6:
214 map->format.format_write = regmap_format_2_6_write;
215 break;
216 default:
217 goto err_map;
219 break;
221 case 4:
222 switch (config->val_bits) {
223 case 12:
224 map->format.format_write = regmap_format_4_12_write;
225 break;
226 default:
227 goto err_map;
229 break;
231 case 7:
232 switch (config->val_bits) {
233 case 9:
234 map->format.format_write = regmap_format_7_9_write;
235 break;
236 default:
237 goto err_map;
239 break;
241 case 10:
242 switch (config->val_bits) {
243 case 14:
244 map->format.format_write = regmap_format_10_14_write;
245 break;
246 default:
247 goto err_map;
249 break;
251 case 8:
252 map->format.format_reg = regmap_format_8;
253 break;
255 case 16:
256 map->format.format_reg = regmap_format_16;
257 break;
259 case 32:
260 map->format.format_reg = regmap_format_32;
261 break;
263 default:
264 goto err_map;
267 switch (config->val_bits) {
268 case 8:
269 map->format.format_val = regmap_format_8;
270 map->format.parse_val = regmap_parse_8;
271 break;
272 case 16:
273 map->format.format_val = regmap_format_16;
274 map->format.parse_val = regmap_parse_16;
275 break;
276 case 32:
277 map->format.format_val = regmap_format_32;
278 map->format.parse_val = regmap_parse_32;
279 break;
282 if (!map->format.format_write &&
283 !(map->format.format_reg && map->format.format_val))
284 goto err_map;
286 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
287 if (map->work_buf == NULL) {
288 ret = -ENOMEM;
289 goto err_map;
292 regmap_debugfs_init(map);
294 ret = regcache_init(map, config);
295 if (ret < 0)
296 goto err_free_workbuf;
298 return map;
300 err_free_workbuf:
301 kfree(map->work_buf);
302 err_map:
303 kfree(map);
304 err:
305 return ERR_PTR(ret);
307 EXPORT_SYMBOL_GPL(regmap_init);
309 static void devm_regmap_release(struct device *dev, void *res)
311 regmap_exit(*(struct regmap **)res);
315 * devm_regmap_init(): Initialise managed register map
317 * @dev: Device that will be interacted with
318 * @bus: Bus-specific callbacks to use with device
319 * @config: Configuration for register map
321 * The return value will be an ERR_PTR() on error or a valid pointer
322 * to a struct regmap. This function should generally not be called
323 * directly, it should be called by bus-specific init functions. The
324 * map will be automatically freed by the device management code.
326 struct regmap *devm_regmap_init(struct device *dev,
327 const struct regmap_bus *bus,
328 const struct regmap_config *config)
330 struct regmap **ptr, *regmap;
332 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
333 if (!ptr)
334 return ERR_PTR(-ENOMEM);
336 regmap = regmap_init(dev, bus, config);
337 if (!IS_ERR(regmap)) {
338 *ptr = regmap;
339 devres_add(dev, ptr);
340 } else {
341 devres_free(ptr);
344 return regmap;
346 EXPORT_SYMBOL_GPL(devm_regmap_init);
349 * regmap_reinit_cache(): Reinitialise the current register cache
351 * @map: Register map to operate on.
352 * @config: New configuration. Only the cache data will be used.
354 * Discard any existing register cache for the map and initialize a
355 * new cache. This can be used to restore the cache to defaults or to
356 * update the cache configuration to reflect runtime discovery of the
357 * hardware.
359 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
361 int ret;
363 mutex_lock(&map->lock);
365 regcache_exit(map);
366 regmap_debugfs_exit(map);
368 map->max_register = config->max_register;
369 map->writeable_reg = config->writeable_reg;
370 map->readable_reg = config->readable_reg;
371 map->volatile_reg = config->volatile_reg;
372 map->precious_reg = config->precious_reg;
373 map->cache_type = config->cache_type;
375 regmap_debugfs_init(map);
377 map->cache_bypass = false;
378 map->cache_only = false;
380 ret = regcache_init(map, config);
382 mutex_unlock(&map->lock);
384 return ret;
388 * regmap_exit(): Free a previously allocated register map
390 void regmap_exit(struct regmap *map)
392 regcache_exit(map);
393 regmap_debugfs_exit(map);
394 kfree(map->work_buf);
395 kfree(map);
397 EXPORT_SYMBOL_GPL(regmap_exit);
399 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
400 const void *val, size_t val_len)
402 u8 *u8 = map->work_buf;
403 void *buf;
404 int ret = -ENOTSUPP;
405 size_t len;
406 int i;
408 /* Check for unwritable registers before we start */
409 if (map->writeable_reg)
410 for (i = 0; i < val_len / map->format.val_bytes; i++)
411 if (!map->writeable_reg(map->dev, reg + i))
412 return -EINVAL;
414 if (!map->cache_bypass && map->format.parse_val) {
415 unsigned int ival;
416 int val_bytes = map->format.val_bytes;
417 for (i = 0; i < val_len / val_bytes; i++) {
418 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
419 ival = map->format.parse_val(map->work_buf);
420 ret = regcache_write(map, reg + i, ival);
421 if (ret) {
422 dev_err(map->dev,
423 "Error in caching of register: %u ret: %d\n",
424 reg + i, ret);
425 return ret;
428 if (map->cache_only) {
429 map->cache_dirty = true;
430 return 0;
434 map->format.format_reg(map->work_buf, reg);
436 u8[0] |= map->write_flag_mask;
438 trace_regmap_hw_write_start(map->dev, reg,
439 val_len / map->format.val_bytes);
441 /* If we're doing a single register write we can probably just
442 * send the work_buf directly, otherwise try to do a gather
443 * write.
445 if (val == (map->work_buf + map->format.pad_bytes +
446 map->format.reg_bytes))
447 ret = map->bus->write(map->dev, map->work_buf,
448 map->format.reg_bytes +
449 map->format.pad_bytes +
450 val_len);
451 else if (map->bus->gather_write)
452 ret = map->bus->gather_write(map->dev, map->work_buf,
453 map->format.reg_bytes +
454 map->format.pad_bytes,
455 val, val_len);
457 /* If that didn't work fall back on linearising by hand. */
458 if (ret == -ENOTSUPP) {
459 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
460 buf = kzalloc(len, GFP_KERNEL);
461 if (!buf)
462 return -ENOMEM;
464 memcpy(buf, map->work_buf, map->format.reg_bytes);
465 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
466 val, val_len);
467 ret = map->bus->write(map->dev, buf, len);
469 kfree(buf);
472 trace_regmap_hw_write_done(map->dev, reg,
473 val_len / map->format.val_bytes);
475 return ret;
478 int _regmap_write(struct regmap *map, unsigned int reg,
479 unsigned int val)
481 int ret;
482 BUG_ON(!map->format.format_write && !map->format.format_val);
484 if (!map->cache_bypass && map->format.format_write) {
485 ret = regcache_write(map, reg, val);
486 if (ret != 0)
487 return ret;
488 if (map->cache_only) {
489 map->cache_dirty = true;
490 return 0;
494 trace_regmap_reg_write(map->dev, reg, val);
496 if (map->format.format_write) {
497 map->format.format_write(map, reg, val);
499 trace_regmap_hw_write_start(map->dev, reg, 1);
501 ret = map->bus->write(map->dev, map->work_buf,
502 map->format.buf_size);
504 trace_regmap_hw_write_done(map->dev, reg, 1);
506 return ret;
507 } else {
508 map->format.format_val(map->work_buf + map->format.reg_bytes
509 + map->format.pad_bytes, val);
510 return _regmap_raw_write(map, reg,
511 map->work_buf +
512 map->format.reg_bytes +
513 map->format.pad_bytes,
514 map->format.val_bytes);
519 * regmap_write(): Write a value to a single register
521 * @map: Register map to write to
522 * @reg: Register to write to
523 * @val: Value to be written
525 * A value of zero will be returned on success, a negative errno will
526 * be returned in error cases.
528 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
530 int ret;
532 mutex_lock(&map->lock);
534 ret = _regmap_write(map, reg, val);
536 mutex_unlock(&map->lock);
538 return ret;
540 EXPORT_SYMBOL_GPL(regmap_write);
543 * regmap_raw_write(): Write raw values to one or more registers
545 * @map: Register map to write to
546 * @reg: Initial register to write to
547 * @val: Block of data to be written, laid out for direct transmission to the
548 * device
549 * @val_len: Length of data pointed to by val.
551 * This function is intended to be used for things like firmware
552 * download where a large block of data needs to be transferred to the
553 * device. No formatting will be done on the data provided.
555 * A value of zero will be returned on success, a negative errno will
556 * be returned in error cases.
558 int regmap_raw_write(struct regmap *map, unsigned int reg,
559 const void *val, size_t val_len)
561 int ret;
563 mutex_lock(&map->lock);
565 ret = _regmap_raw_write(map, reg, val, val_len);
567 mutex_unlock(&map->lock);
569 return ret;
571 EXPORT_SYMBOL_GPL(regmap_raw_write);
574 * regmap_bulk_write(): Write multiple registers to the device
576 * @map: Register map to write to
577 * @reg: First register to be write from
578 * @val: Block of data to be written, in native register size for device
579 * @val_count: Number of registers to write
581 * This function is intended to be used for writing a large block of
582 * data to be device either in single transfer or multiple transfer.
584 * A value of zero will be returned on success, a negative errno will
585 * be returned in error cases.
587 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
588 size_t val_count)
590 int ret = 0, i;
591 size_t val_bytes = map->format.val_bytes;
592 void *wval;
594 if (!map->format.parse_val)
595 return -EINVAL;
597 mutex_lock(&map->lock);
599 /* No formatting is require if val_byte is 1 */
600 if (val_bytes == 1) {
601 wval = (void *)val;
602 } else {
603 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
604 if (!wval) {
605 ret = -ENOMEM;
606 dev_err(map->dev, "Error in memory allocation\n");
607 goto out;
609 for (i = 0; i < val_count * val_bytes; i += val_bytes)
610 map->format.parse_val(wval + i);
612 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
614 if (val_bytes != 1)
615 kfree(wval);
617 out:
618 mutex_unlock(&map->lock);
619 return ret;
621 EXPORT_SYMBOL_GPL(regmap_bulk_write);
623 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
624 unsigned int val_len)
626 u8 *u8 = map->work_buf;
627 int ret;
629 map->format.format_reg(map->work_buf, reg);
632 * Some buses or devices flag reads by setting the high bits in the
633 * register addresss; since it's always the high bits for all
634 * current formats we can do this here rather than in
635 * formatting. This may break if we get interesting formats.
637 u8[0] |= map->read_flag_mask;
639 trace_regmap_hw_read_start(map->dev, reg,
640 val_len / map->format.val_bytes);
642 ret = map->bus->read(map->dev, map->work_buf,
643 map->format.reg_bytes + map->format.pad_bytes,
644 val, val_len);
646 trace_regmap_hw_read_done(map->dev, reg,
647 val_len / map->format.val_bytes);
649 return ret;
652 static int _regmap_read(struct regmap *map, unsigned int reg,
653 unsigned int *val)
655 int ret;
657 if (!map->cache_bypass) {
658 ret = regcache_read(map, reg, val);
659 if (ret == 0)
660 return 0;
663 if (!map->format.parse_val)
664 return -EINVAL;
666 if (map->cache_only)
667 return -EBUSY;
669 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
670 if (ret == 0) {
671 *val = map->format.parse_val(map->work_buf);
672 trace_regmap_reg_read(map->dev, reg, *val);
675 return ret;
679 * regmap_read(): Read a value from a single register
681 * @map: Register map to write to
682 * @reg: Register to be read from
683 * @val: Pointer to store read value
685 * A value of zero will be returned on success, a negative errno will
686 * be returned in error cases.
688 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
690 int ret;
692 mutex_lock(&map->lock);
694 ret = _regmap_read(map, reg, val);
696 mutex_unlock(&map->lock);
698 return ret;
700 EXPORT_SYMBOL_GPL(regmap_read);
703 * regmap_raw_read(): Read raw data from the device
705 * @map: Register map to write to
706 * @reg: First register to be read from
707 * @val: Pointer to store read value
708 * @val_len: Size of data to read
710 * A value of zero will be returned on success, a negative errno will
711 * be returned in error cases.
713 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
714 size_t val_len)
716 size_t val_bytes = map->format.val_bytes;
717 size_t val_count = val_len / val_bytes;
718 unsigned int v;
719 int ret, i;
721 mutex_lock(&map->lock);
723 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
724 map->cache_type == REGCACHE_NONE) {
725 /* Physical block read if there's no cache involved */
726 ret = _regmap_raw_read(map, reg, val, val_len);
728 } else {
729 /* Otherwise go word by word for the cache; should be low
730 * cost as we expect to hit the cache.
732 for (i = 0; i < val_count; i++) {
733 ret = _regmap_read(map, reg + i, &v);
734 if (ret != 0)
735 goto out;
737 map->format.format_val(val + (i * val_bytes), v);
741 out:
742 mutex_unlock(&map->lock);
744 return ret;
746 EXPORT_SYMBOL_GPL(regmap_raw_read);
749 * regmap_bulk_read(): Read multiple registers from the device
751 * @map: Register map to write to
752 * @reg: First register to be read from
753 * @val: Pointer to store read value, in native register size for device
754 * @val_count: Number of registers to read
756 * A value of zero will be returned on success, a negative errno will
757 * be returned in error cases.
759 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
760 size_t val_count)
762 int ret, i;
763 size_t val_bytes = map->format.val_bytes;
764 bool vol = regmap_volatile_range(map, reg, val_count);
766 if (!map->format.parse_val)
767 return -EINVAL;
769 if (vol || map->cache_type == REGCACHE_NONE) {
770 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
771 if (ret != 0)
772 return ret;
774 for (i = 0; i < val_count * val_bytes; i += val_bytes)
775 map->format.parse_val(val + i);
776 } else {
777 for (i = 0; i < val_count; i++) {
778 unsigned int ival;
779 ret = regmap_read(map, reg + i, &ival);
780 if (ret != 0)
781 return ret;
782 memcpy(val + (i * val_bytes), &ival, val_bytes);
786 return 0;
788 EXPORT_SYMBOL_GPL(regmap_bulk_read);
790 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
791 unsigned int mask, unsigned int val,
792 bool *change)
794 int ret;
795 unsigned int tmp, orig;
797 mutex_lock(&map->lock);
799 ret = _regmap_read(map, reg, &orig);
800 if (ret != 0)
801 goto out;
803 tmp = orig & ~mask;
804 tmp |= val & mask;
806 if (tmp != orig) {
807 ret = _regmap_write(map, reg, tmp);
808 *change = true;
809 } else {
810 *change = false;
813 out:
814 mutex_unlock(&map->lock);
816 return ret;
820 * regmap_update_bits: Perform a read/modify/write cycle on the register map
822 * @map: Register map to update
823 * @reg: Register to update
824 * @mask: Bitmask to change
825 * @val: New value for bitmask
827 * Returns zero for success, a negative number on error.
829 int regmap_update_bits(struct regmap *map, unsigned int reg,
830 unsigned int mask, unsigned int val)
832 bool change;
833 return _regmap_update_bits(map, reg, mask, val, &change);
835 EXPORT_SYMBOL_GPL(regmap_update_bits);
838 * regmap_update_bits_check: Perform a read/modify/write cycle on the
839 * register map and report if updated
841 * @map: Register map to update
842 * @reg: Register to update
843 * @mask: Bitmask to change
844 * @val: New value for bitmask
845 * @change: Boolean indicating if a write was done
847 * Returns zero for success, a negative number on error.
849 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
850 unsigned int mask, unsigned int val,
851 bool *change)
853 return _regmap_update_bits(map, reg, mask, val, change);
855 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
858 * regmap_register_patch: Register and apply register updates to be applied
859 * on device initialistion
861 * @map: Register map to apply updates to.
862 * @regs: Values to update.
863 * @num_regs: Number of entries in regs.
865 * Register a set of register updates to be applied to the device
866 * whenever the device registers are synchronised with the cache and
867 * apply them immediately. Typically this is used to apply
868 * corrections to be applied to the device defaults on startup, such
869 * as the updates some vendors provide to undocumented registers.
871 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
872 int num_regs)
874 int i, ret;
875 bool bypass;
877 /* If needed the implementation can be extended to support this */
878 if (map->patch)
879 return -EBUSY;
881 mutex_lock(&map->lock);
883 bypass = map->cache_bypass;
885 map->cache_bypass = true;
887 /* Write out first; it's useful to apply even if we fail later. */
888 for (i = 0; i < num_regs; i++) {
889 ret = _regmap_write(map, regs[i].reg, regs[i].def);
890 if (ret != 0) {
891 dev_err(map->dev, "Failed to write %x = %x: %d\n",
892 regs[i].reg, regs[i].def, ret);
893 goto out;
897 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
898 if (map->patch != NULL) {
899 memcpy(map->patch, regs,
900 num_regs * sizeof(struct reg_default));
901 map->patch_regs = num_regs;
902 } else {
903 ret = -ENOMEM;
906 out:
907 map->cache_bypass = bypass;
909 mutex_unlock(&map->lock);
911 return ret;
913 EXPORT_SYMBOL_GPL(regmap_register_patch);
916 * regmap_get_val_bytes(): Report the size of a register value
918 * Report the size of a register value, mainly intended to for use by
919 * generic infrastructure built on top of regmap.
921 int regmap_get_val_bytes(struct regmap *map)
923 if (map->format.format_write)
924 return -EINVAL;
926 return map->format.val_bytes;
928 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
930 static int __init regmap_initcall(void)
932 regmap_debugfs_initcall();
934 return 0;
936 postcore_initcall(regmap_initcall);