regcache.c 11 KB

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