regcache.c 9.7 KB

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