regcache.c 11 KB

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