regcache.c 11 KB

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