regcache.c 9.0 KB

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