regcache.c 11 KB

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