regcache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 (regmap_volatile(map, i))
  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 (regmap_volatile(map, i))
  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. /**
  184. * regcache_write: Set the value of a given register in the cache.
  185. *
  186. * @map: map to configure.
  187. * @reg: The register index.
  188. * @value: The new register value.
  189. *
  190. * Return a negative value on failure, 0 on success.
  191. */
  192. int regcache_write(struct regmap *map,
  193. unsigned int reg, unsigned int value)
  194. {
  195. if (map->cache_type == REGCACHE_NONE)
  196. return 0;
  197. BUG_ON(!map->cache_ops);
  198. if (!regmap_writeable(map, reg))
  199. return -EIO;
  200. if (!regmap_volatile(map, reg))
  201. return map->cache_ops->write(map, reg, value);
  202. return 0;
  203. }
  204. /**
  205. * regcache_sync: Sync the register cache with the hardware.
  206. *
  207. * @map: map to configure.
  208. *
  209. * Any registers that should not be synced should be marked as
  210. * volatile. In general drivers can choose not to use the provided
  211. * syncing functionality if they so require.
  212. *
  213. * Return a negative value on failure, 0 on success.
  214. */
  215. int regcache_sync(struct regmap *map)
  216. {
  217. int ret = 0;
  218. unsigned int i;
  219. const char *name;
  220. unsigned int bypass;
  221. BUG_ON(!map->cache_ops || !map->cache_ops->sync);
  222. mutex_lock(&map->lock);
  223. /* Remember the initial bypass state */
  224. bypass = map->cache_bypass;
  225. dev_dbg(map->dev, "Syncing %s cache\n",
  226. map->cache_ops->name);
  227. name = map->cache_ops->name;
  228. trace_regcache_sync(map->dev, name, "start");
  229. if (!map->cache_dirty)
  230. goto out;
  231. /* Apply any patch first */
  232. map->cache_bypass = 1;
  233. for (i = 0; i < map->patch_regs; i++) {
  234. ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
  235. if (ret != 0) {
  236. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  237. map->patch[i].reg, map->patch[i].def, ret);
  238. goto out;
  239. }
  240. }
  241. map->cache_bypass = 0;
  242. ret = map->cache_ops->sync(map, 0, map->max_register);
  243. if (ret == 0)
  244. map->cache_dirty = false;
  245. out:
  246. trace_regcache_sync(map->dev, name, "stop");
  247. /* Restore the bypass state */
  248. map->cache_bypass = bypass;
  249. mutex_unlock(&map->lock);
  250. return ret;
  251. }
  252. EXPORT_SYMBOL_GPL(regcache_sync);
  253. /**
  254. * regcache_sync_region: Sync part of the register cache with the hardware.
  255. *
  256. * @map: map to sync.
  257. * @min: first register to sync
  258. * @max: last register to sync
  259. *
  260. * Write all non-default register values in the specified region to
  261. * the hardware.
  262. *
  263. * Return a negative value on failure, 0 on success.
  264. */
  265. int regcache_sync_region(struct regmap *map, unsigned int min,
  266. unsigned int max)
  267. {
  268. int ret = 0;
  269. const char *name;
  270. unsigned int bypass;
  271. BUG_ON(!map->cache_ops || !map->cache_ops->sync);
  272. mutex_lock(&map->lock);
  273. /* Remember the initial bypass state */
  274. bypass = map->cache_bypass;
  275. name = map->cache_ops->name;
  276. dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
  277. trace_regcache_sync(map->dev, name, "start region");
  278. if (!map->cache_dirty)
  279. goto out;
  280. ret = map->cache_ops->sync(map, min, max);
  281. out:
  282. trace_regcache_sync(map->dev, name, "stop region");
  283. /* Restore the bypass state */
  284. map->cache_bypass = bypass;
  285. mutex_unlock(&map->lock);
  286. return ret;
  287. }
  288. /**
  289. * regcache_cache_only: Put a register map into cache only mode
  290. *
  291. * @map: map to configure
  292. * @cache_only: flag if changes should be written to the hardware
  293. *
  294. * When a register map is marked as cache only writes to the register
  295. * map API will only update the register cache, they will not cause
  296. * any hardware changes. This is useful for allowing portions of
  297. * drivers to act as though the device were functioning as normal when
  298. * it is disabled for power saving reasons.
  299. */
  300. void regcache_cache_only(struct regmap *map, bool enable)
  301. {
  302. mutex_lock(&map->lock);
  303. WARN_ON(map->cache_bypass && enable);
  304. map->cache_only = enable;
  305. trace_regmap_cache_only(map->dev, enable);
  306. mutex_unlock(&map->lock);
  307. }
  308. EXPORT_SYMBOL_GPL(regcache_cache_only);
  309. /**
  310. * regcache_mark_dirty: Mark the register cache as dirty
  311. *
  312. * @map: map to mark
  313. *
  314. * Mark the register cache as dirty, for example due to the device
  315. * having been powered down for suspend. If the cache is not marked
  316. * as dirty then the cache sync will be suppressed.
  317. */
  318. void regcache_mark_dirty(struct regmap *map)
  319. {
  320. mutex_lock(&map->lock);
  321. map->cache_dirty = true;
  322. mutex_unlock(&map->lock);
  323. }
  324. EXPORT_SYMBOL_GPL(regcache_mark_dirty);
  325. /**
  326. * regcache_cache_bypass: Put a register map into cache bypass mode
  327. *
  328. * @map: map to configure
  329. * @cache_bypass: flag if changes should not be written to the hardware
  330. *
  331. * When a register map is marked with the cache bypass option, writes
  332. * to the register map API will only update the hardware and not the
  333. * the cache directly. This is useful when syncing the cache back to
  334. * the hardware.
  335. */
  336. void regcache_cache_bypass(struct regmap *map, bool enable)
  337. {
  338. mutex_lock(&map->lock);
  339. WARN_ON(map->cache_only && enable);
  340. map->cache_bypass = enable;
  341. trace_regmap_cache_bypass(map->dev, enable);
  342. mutex_unlock(&map->lock);
  343. }
  344. EXPORT_SYMBOL_GPL(regcache_cache_bypass);
  345. bool regcache_set_val(void *base, unsigned int idx,
  346. unsigned int val, unsigned int word_size)
  347. {
  348. switch (word_size) {
  349. case 1: {
  350. u8 *cache = base;
  351. if (cache[idx] == val)
  352. return true;
  353. cache[idx] = val;
  354. break;
  355. }
  356. case 2: {
  357. u16 *cache = base;
  358. if (cache[idx] == val)
  359. return true;
  360. cache[idx] = val;
  361. break;
  362. }
  363. case 4: {
  364. u32 *cache = base;
  365. if (cache[idx] == val)
  366. return true;
  367. cache[idx] = val;
  368. break;
  369. }
  370. default:
  371. BUG();
  372. }
  373. return false;
  374. }
  375. unsigned int regcache_get_val(const void *base, unsigned int idx,
  376. unsigned int word_size)
  377. {
  378. if (!base)
  379. return -EINVAL;
  380. switch (word_size) {
  381. case 1: {
  382. const u8 *cache = base;
  383. return cache[idx];
  384. }
  385. case 2: {
  386. const u16 *cache = base;
  387. return cache[idx];
  388. }
  389. case 4: {
  390. const u32 *cache = base;
  391. return cache[idx];
  392. }
  393. default:
  394. BUG();
  395. }
  396. /* unreachable */
  397. return -1;
  398. }
  399. static int regcache_default_cmp(const void *a, const void *b)
  400. {
  401. const struct reg_default *_a = a;
  402. const struct reg_default *_b = b;
  403. return _a->reg - _b->reg;
  404. }
  405. int regcache_lookup_reg(struct regmap *map, unsigned int reg)
  406. {
  407. struct reg_default key;
  408. struct reg_default *r;
  409. key.reg = reg;
  410. key.def = 0;
  411. r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
  412. sizeof(struct reg_default), regcache_default_cmp);
  413. if (r)
  414. return r - map->reg_defaults;
  415. else
  416. return -ENOENT;
  417. }