regcache.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. &regcache_indexed_ops,
  17. &regcache_rbtree_ops,
  18. &regcache_lzo_ops,
  19. };
  20. static int regcache_hw_init(struct regmap *map)
  21. {
  22. int i, j;
  23. int ret;
  24. int count;
  25. unsigned int val;
  26. void *tmp_buf;
  27. if (!map->num_reg_defaults_raw)
  28. return -EINVAL;
  29. if (!map->reg_defaults_raw) {
  30. dev_warn(map->dev, "No cache defaults, reading back from HW\n");
  31. tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
  32. if (!tmp_buf)
  33. return -EINVAL;
  34. ret = regmap_bulk_read(map, 0, tmp_buf,
  35. map->num_reg_defaults_raw);
  36. if (ret < 0) {
  37. kfree(tmp_buf);
  38. return ret;
  39. }
  40. map->reg_defaults_raw = tmp_buf;
  41. map->cache_free = 1;
  42. }
  43. /* calculate the size of reg_defaults */
  44. for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
  45. val = regcache_get_val(map->reg_defaults_raw,
  46. i, map->cache_word_size);
  47. if (!val)
  48. continue;
  49. count++;
  50. }
  51. map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
  52. GFP_KERNEL);
  53. if (!map->reg_defaults)
  54. return -ENOMEM;
  55. /* fill the reg_defaults */
  56. map->num_reg_defaults = count;
  57. for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
  58. val = regcache_get_val(map->reg_defaults_raw,
  59. i, map->cache_word_size);
  60. if (!val)
  61. continue;
  62. map->reg_defaults[j].reg = i;
  63. map->reg_defaults[j].def = val;
  64. j++;
  65. }
  66. return 0;
  67. }
  68. int regcache_init(struct regmap *map)
  69. {
  70. int ret;
  71. int i;
  72. void *tmp_buf;
  73. if (map->cache_type == REGCACHE_NONE) {
  74. map->cache_bypass = true;
  75. return 0;
  76. }
  77. for (i = 0; i < ARRAY_SIZE(cache_types); i++)
  78. if (cache_types[i]->type == map->cache_type)
  79. break;
  80. if (i == ARRAY_SIZE(cache_types)) {
  81. dev_err(map->dev, "Could not match compress type: %d\n",
  82. map->cache_type);
  83. return -EINVAL;
  84. }
  85. map->cache = NULL;
  86. map->cache_ops = cache_types[i];
  87. if (!map->cache_ops->read ||
  88. !map->cache_ops->write ||
  89. !map->cache_ops->name)
  90. return -EINVAL;
  91. /* We still need to ensure that the reg_defaults
  92. * won't vanish from under us. We'll need to make
  93. * a copy of it.
  94. */
  95. if (map->reg_defaults) {
  96. if (!map->num_reg_defaults)
  97. return -EINVAL;
  98. tmp_buf = kmemdup(map->reg_defaults, map->num_reg_defaults *
  99. sizeof(struct reg_default), GFP_KERNEL);
  100. if (!tmp_buf)
  101. return -ENOMEM;
  102. map->reg_defaults = tmp_buf;
  103. } else {
  104. /* Some devices such as PMICs don't have cache defaults,
  105. * we cope with this by reading back the HW registers and
  106. * crafting the cache defaults by hand.
  107. */
  108. ret = regcache_hw_init(map);
  109. if (ret < 0)
  110. return ret;
  111. }
  112. if (!map->max_register)
  113. map->max_register = map->num_reg_defaults_raw;
  114. if (map->cache_ops->init) {
  115. dev_dbg(map->dev, "Initializing %s cache\n",
  116. map->cache_ops->name);
  117. return map->cache_ops->init(map);
  118. }
  119. return 0;
  120. }
  121. void regcache_exit(struct regmap *map)
  122. {
  123. if (map->cache_type == REGCACHE_NONE)
  124. return;
  125. BUG_ON(!map->cache_ops);
  126. kfree(map->reg_defaults);
  127. if (map->cache_free)
  128. kfree(map->reg_defaults_raw);
  129. if (map->cache_ops->exit) {
  130. dev_dbg(map->dev, "Destroying %s cache\n",
  131. map->cache_ops->name);
  132. map->cache_ops->exit(map);
  133. }
  134. }
  135. /**
  136. * regcache_read: Fetch the value of a given register from the cache.
  137. *
  138. * @map: map to configure.
  139. * @reg: The register index.
  140. * @value: The value to be returned.
  141. *
  142. * Return a negative value on failure, 0 on success.
  143. */
  144. int regcache_read(struct regmap *map,
  145. unsigned int reg, unsigned int *value)
  146. {
  147. if (map->cache_type == REGCACHE_NONE)
  148. return -ENOSYS;
  149. BUG_ON(!map->cache_ops);
  150. if (!regmap_readable(map, reg))
  151. return -EIO;
  152. if (!regmap_volatile(map, reg))
  153. return map->cache_ops->read(map, reg, value);
  154. return -EINVAL;
  155. }
  156. EXPORT_SYMBOL_GPL(regcache_read);
  157. /**
  158. * regcache_write: Set the value of a given register in the cache.
  159. *
  160. * @map: map to configure.
  161. * @reg: The register index.
  162. * @value: The new register value.
  163. *
  164. * Return a negative value on failure, 0 on success.
  165. */
  166. int regcache_write(struct regmap *map,
  167. unsigned int reg, unsigned int value)
  168. {
  169. if (map->cache_type == REGCACHE_NONE)
  170. return 0;
  171. BUG_ON(!map->cache_ops);
  172. if (!regmap_writeable(map, reg))
  173. return -EIO;
  174. if (!regmap_volatile(map, reg))
  175. return map->cache_ops->write(map, reg, value);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_GPL(regcache_write);
  179. /**
  180. * regcache_sync: Sync the register cache with the hardware.
  181. *
  182. * @map: map to configure.
  183. *
  184. * Any registers that should not be synced should be marked as
  185. * volatile. In general drivers can choose not to use the provided
  186. * syncing functionality if they so require.
  187. *
  188. * Return a negative value on failure, 0 on success.
  189. */
  190. int regcache_sync(struct regmap *map)
  191. {
  192. int ret = 0;
  193. unsigned int val;
  194. unsigned int i;
  195. const char *name;
  196. unsigned int bypass;
  197. BUG_ON(!map->cache_ops);
  198. mutex_lock(&map->lock);
  199. /* Remember the initial bypass state */
  200. bypass = map->cache_bypass;
  201. dev_dbg(map->dev, "Syncing %s cache\n",
  202. map->cache_ops->name);
  203. name = map->cache_ops->name;
  204. trace_regcache_sync(map->dev, name, "start");
  205. if (map->cache_ops->sync) {
  206. ret = map->cache_ops->sync(map);
  207. } else {
  208. for (i = 0; i < map->num_reg_defaults; i++) {
  209. ret = regcache_read(map, i, &val);
  210. if (ret < 0)
  211. goto out;
  212. map->cache_bypass = 1;
  213. ret = _regmap_write(map, i, val);
  214. map->cache_bypass = 0;
  215. if (ret < 0)
  216. goto out;
  217. dev_dbg(map->dev, "Synced register %#x, value %#x\n",
  218. map->reg_defaults[i].reg,
  219. map->reg_defaults[i].def);
  220. }
  221. }
  222. out:
  223. trace_regcache_sync(map->dev, name, "stop");
  224. /* Restore the bypass state */
  225. map->cache_bypass = bypass;
  226. mutex_unlock(&map->lock);
  227. return ret;
  228. }
  229. EXPORT_SYMBOL_GPL(regcache_sync);
  230. /**
  231. * regcache_cache_only: Put a register map into cache only mode
  232. *
  233. * @map: map to configure
  234. * @cache_only: flag if changes should be written to the hardware
  235. *
  236. * When a register map is marked as cache only writes to the register
  237. * map API will only update the register cache, they will not cause
  238. * any hardware changes. This is useful for allowing portions of
  239. * drivers to act as though the device were functioning as normal when
  240. * it is disabled for power saving reasons.
  241. */
  242. void regcache_cache_only(struct regmap *map, bool enable)
  243. {
  244. mutex_lock(&map->lock);
  245. map->cache_only = enable;
  246. mutex_unlock(&map->lock);
  247. }
  248. EXPORT_SYMBOL_GPL(regcache_cache_only);
  249. /**
  250. * regcache_cache_bypass: Put a register map into cache bypass mode
  251. *
  252. * @map: map to configure
  253. * @cache_only: flag if changes should not be written to the hardware
  254. *
  255. * When a register map is marked with the cache bypass option, writes
  256. * to the register map API will only update the hardware and not the
  257. * the cache directly. This is useful when syncing the cache back to
  258. * the hardware.
  259. */
  260. void regcache_cache_bypass(struct regmap *map, bool enable)
  261. {
  262. mutex_lock(&map->lock);
  263. map->cache_bypass = enable;
  264. mutex_unlock(&map->lock);
  265. }
  266. EXPORT_SYMBOL_GPL(regcache_cache_bypass);
  267. bool regcache_set_val(void *base, unsigned int idx,
  268. unsigned int val, unsigned int word_size)
  269. {
  270. switch (word_size) {
  271. case 1: {
  272. u8 *cache = base;
  273. if (cache[idx] == val)
  274. return true;
  275. cache[idx] = val;
  276. break;
  277. }
  278. case 2: {
  279. u16 *cache = base;
  280. if (cache[idx] == val)
  281. return true;
  282. cache[idx] = val;
  283. break;
  284. }
  285. default:
  286. BUG();
  287. }
  288. /* unreachable */
  289. return false;
  290. }
  291. unsigned int regcache_get_val(const void *base, unsigned int idx,
  292. unsigned int word_size)
  293. {
  294. if (!base)
  295. return -EINVAL;
  296. switch (word_size) {
  297. case 1: {
  298. const u8 *cache = base;
  299. return cache[idx];
  300. }
  301. case 2: {
  302. const u16 *cache = base;
  303. return cache[idx];
  304. }
  305. default:
  306. BUG();
  307. }
  308. /* unreachable */
  309. return -1;
  310. }
  311. int regcache_lookup_reg(struct regmap *map, unsigned int reg)
  312. {
  313. unsigned int i;
  314. for (i = 0; i < map->num_reg_defaults; i++)
  315. if (map->reg_defaults[i].reg == reg)
  316. return i;
  317. return -1;
  318. }
  319. int regcache_insert_reg(struct regmap *map, unsigned int reg,
  320. unsigned int val)
  321. {
  322. void *tmp;
  323. tmp = krealloc(map->reg_defaults,
  324. (map->num_reg_defaults + 1) * sizeof(struct reg_default),
  325. GFP_KERNEL);
  326. if (!tmp)
  327. return -ENOMEM;
  328. map->reg_defaults = tmp;
  329. map->num_reg_defaults++;
  330. map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
  331. map->reg_defaults[map->num_reg_defaults - 1].def = val;
  332. return 0;
  333. }