regcache.c 9.0 KB

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