regcache.c 10 KB

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