regcache.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Register cache access API
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc
  5. *
  6. * Author: Dimitris Papastamos <dp@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. #include <linux/slab.h>
  13. #include <trace/events/regmap.h>
  14. #include "internal.h"
  15. static const struct regcache_ops *cache_types[] = {
  16. };
  17. static int regcache_hw_init(struct regmap *map)
  18. {
  19. int i, j;
  20. int ret;
  21. int count;
  22. unsigned int val;
  23. void *tmp_buf;
  24. if (!map->num_reg_defaults_raw)
  25. return -EINVAL;
  26. if (!map->reg_defaults_raw) {
  27. dev_warn(map->dev, "No cache defaults, reading back from HW\n");
  28. tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
  29. if (!tmp_buf)
  30. return -EINVAL;
  31. ret = regmap_bulk_read(map, 0, tmp_buf,
  32. map->num_reg_defaults_raw);
  33. if (ret < 0) {
  34. kfree(tmp_buf);
  35. return ret;
  36. }
  37. map->reg_defaults_raw = tmp_buf;
  38. map->cache_free = 1;
  39. }
  40. /* calculate the size of reg_defaults */
  41. for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
  42. val = regcache_get_val(map->reg_defaults_raw,
  43. i, map->cache_word_size);
  44. if (!val)
  45. continue;
  46. count++;
  47. }
  48. map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
  49. GFP_KERNEL);
  50. if (!map->reg_defaults)
  51. return -ENOMEM;
  52. /* fill the reg_defaults */
  53. map->num_reg_defaults = count;
  54. for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
  55. val = regcache_get_val(map->reg_defaults_raw,
  56. i, map->cache_word_size);
  57. if (!val)
  58. continue;
  59. map->reg_defaults[j].reg = i;
  60. map->reg_defaults[j].def = val;
  61. j++;
  62. }
  63. return 0;
  64. }
  65. int regcache_init(struct regmap *map)
  66. {
  67. int ret;
  68. int i;
  69. void *tmp_buf;
  70. if (map->cache_type == REGCACHE_NONE)
  71. return 0;
  72. for (i = 0; i < ARRAY_SIZE(cache_types); i++)
  73. if (cache_types[i]->type == map->cache_type)
  74. break;
  75. if (i == ARRAY_SIZE(cache_types)) {
  76. dev_err(map->dev, "Could not match compress type: %d\n",
  77. map->cache_type);
  78. return -EINVAL;
  79. }
  80. map->cache = NULL;
  81. map->cache_ops = cache_types[i];
  82. if (!map->cache_ops->read ||
  83. !map->cache_ops->write ||
  84. !map->cache_ops->name)
  85. return -EINVAL;
  86. /* We still need to ensure that the reg_defaults
  87. * won't vanish from under us. We'll need to make
  88. * a copy of it.
  89. */
  90. if (map->reg_defaults) {
  91. if (!map->num_reg_defaults)
  92. return -EINVAL;
  93. tmp_buf = kmemdup(map->reg_defaults, map->num_reg_defaults *
  94. sizeof(struct reg_default), GFP_KERNEL);
  95. if (!tmp_buf)
  96. return -ENOMEM;
  97. map->reg_defaults = tmp_buf;
  98. } else {
  99. /* Some devices such as PMIC's don't have cache defaults,
  100. * we cope with this by reading back the HW registers and
  101. * crafting the cache defaults by hand.
  102. */
  103. ret = regcache_hw_init(map);
  104. if (ret < 0)
  105. return ret;
  106. }
  107. if (!map->max_register)
  108. map->max_register = map->num_reg_defaults_raw;
  109. if (map->cache_ops->init) {
  110. dev_dbg(map->dev, "Initializing %s cache\n",
  111. map->cache_ops->name);
  112. return map->cache_ops->init(map);
  113. }
  114. return 0;
  115. }
  116. void regcache_exit(struct regmap *map)
  117. {
  118. if (map->cache_type == REGCACHE_NONE)
  119. return;
  120. BUG_ON(!map->cache_ops);
  121. kfree(map->reg_defaults);
  122. if (map->cache_free)
  123. kfree(map->reg_defaults_raw);
  124. if (map->cache_ops->exit) {
  125. dev_dbg(map->dev, "Destroying %s cache\n",
  126. map->cache_ops->name);
  127. map->cache_ops->exit(map);
  128. }
  129. }
  130. /**
  131. * regcache_read: Fetch the value of a given register from the cache.
  132. *
  133. * @map: map to configure.
  134. * @reg: The register index.
  135. * @value: The value to be returned.
  136. *
  137. * Return a negative value on failure, 0 on success.
  138. */
  139. int regcache_read(struct regmap *map,
  140. unsigned int reg, unsigned int *value)
  141. {
  142. if (map->cache_type == REGCACHE_NONE)
  143. return -ENOSYS;
  144. BUG_ON(!map->cache_ops);
  145. if (!regmap_readable(map, reg))
  146. return -EIO;
  147. if (!regmap_volatile(map, reg))
  148. return map->cache_ops->read(map, reg, value);
  149. return -EINVAL;
  150. }
  151. EXPORT_SYMBOL_GPL(regcache_read);
  152. /**
  153. * regcache_write: Set the value of a given register in the cache.
  154. *
  155. * @map: map to configure.
  156. * @reg: The register index.
  157. * @value: The new register value.
  158. *
  159. * Return a negative value on failure, 0 on success.
  160. */
  161. int regcache_write(struct regmap *map,
  162. unsigned int reg, unsigned int value)
  163. {
  164. if (map->cache_type == REGCACHE_NONE)
  165. return 0;
  166. BUG_ON(!map->cache_ops);
  167. if (!regmap_writeable(map, reg))
  168. return -EIO;
  169. if (!regmap_volatile(map, reg))
  170. return map->cache_ops->write(map, reg, value);
  171. return 0;
  172. }
  173. EXPORT_SYMBOL_GPL(regcache_write);
  174. /**
  175. * regcache_sync: Sync the register cache with the hardware.
  176. *
  177. * @map: map to configure.
  178. *
  179. * Any registers that should not be synced should be marked as
  180. * volatile. In general drivers can choose not to use the provided
  181. * syncing functionality if they so require.
  182. *
  183. * Return a negative value on failure, 0 on success.
  184. */
  185. int regcache_sync(struct regmap *map)
  186. {
  187. BUG_ON(!map->cache_ops);
  188. if (map->cache_ops->sync) {
  189. dev_dbg(map->dev, "Syncing %s cache\n",
  190. map->cache_ops->name);
  191. return map->cache_ops->sync(map);
  192. }
  193. return 0;
  194. }
  195. EXPORT_SYMBOL_GPL(regcache_sync);
  196. bool regcache_set_val(void *base, unsigned int idx,
  197. unsigned int val, unsigned int word_size)
  198. {
  199. switch (word_size) {
  200. case 1: {
  201. u8 *cache = base;
  202. if (cache[idx] == val)
  203. return true;
  204. cache[idx] = val;
  205. break;
  206. }
  207. case 2: {
  208. u16 *cache = base;
  209. if (cache[idx] == val)
  210. return true;
  211. cache[idx] = val;
  212. break;
  213. }
  214. default:
  215. BUG();
  216. }
  217. /* unreachable */
  218. return false;
  219. }
  220. unsigned int regcache_get_val(const void *base, unsigned int idx,
  221. unsigned int word_size)
  222. {
  223. if (!base)
  224. return -EINVAL;
  225. switch (word_size) {
  226. case 1: {
  227. const u8 *cache = base;
  228. return cache[idx];
  229. }
  230. case 2: {
  231. const u16 *cache = base;
  232. return cache[idx];
  233. }
  234. default:
  235. BUG();
  236. }
  237. /* unreachable */
  238. return -1;
  239. }
  240. int regcache_lookup_reg(struct regmap *map, unsigned int reg)
  241. {
  242. unsigned int i;
  243. for (i = 0; i < map->num_reg_defaults; i++)
  244. if (map->reg_defaults[i].reg == reg)
  245. return i;
  246. return -1;
  247. }
  248. int regcache_insert_reg(struct regmap *map, unsigned int reg,
  249. unsigned int val)
  250. {
  251. void *tmp;
  252. tmp = krealloc(map->reg_defaults,
  253. (map->num_reg_defaults + 1) * sizeof(struct reg_default),
  254. GFP_KERNEL);
  255. if (!tmp)
  256. return -ENOMEM;
  257. map->reg_defaults = tmp;
  258. map->num_reg_defaults++;
  259. map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
  260. map->reg_defaults[map->num_reg_defaults - 1].def = val;
  261. return 0;
  262. }