regcache.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Register cache access API
  4. //
  5. // Copyright 2011 Wolfson Microelectronics plc
  6. //
  7. // Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
  8. #include <linux/bsearch.h>
  9. #include <linux/device.h>
  10. #include <linux/export.h>
  11. #include <linux/slab.h>
  12. #include <linux/sort.h>
  13. #include "trace.h"
  14. #include "internal.h"
  15. static const struct regcache_ops *cache_types[] = {
  16. &regcache_flat_sparse_ops,
  17. &regcache_rbtree_ops,
  18. &regcache_maple_ops,
  19. &regcache_flat_ops,
  20. };
  21. static int regcache_defaults_cmp(const void *a, const void *b)
  22. {
  23. const struct reg_default *x = a;
  24. const struct reg_default *y = b;
  25. if (x->reg > y->reg)
  26. return 1;
  27. else if (x->reg < y->reg)
  28. return -1;
  29. else
  30. return 0;
  31. }
  32. void regcache_sort_defaults(struct reg_default *defaults, unsigned int ndefaults)
  33. {
  34. sort(defaults, ndefaults, sizeof(*defaults),
  35. regcache_defaults_cmp, NULL);
  36. }
  37. EXPORT_SYMBOL_GPL(regcache_sort_defaults);
  38. static int regcache_hw_init(struct regmap *map)
  39. {
  40. int i, j;
  41. int ret;
  42. int count;
  43. unsigned int reg, val;
  44. void *tmp_buf;
  45. if (!map->num_reg_defaults_raw)
  46. return -EINVAL;
  47. /* calculate the size of reg_defaults */
  48. for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
  49. if (regmap_readable(map, i * map->reg_stride) &&
  50. !regmap_volatile(map, i * map->reg_stride))
  51. count++;
  52. /* all registers are unreadable or volatile, so just bypass */
  53. if (!count) {
  54. map->cache_bypass = true;
  55. return 0;
  56. }
  57. map->num_reg_defaults = count;
  58. map->reg_defaults = kmalloc_objs(struct reg_default, count);
  59. if (!map->reg_defaults)
  60. return -ENOMEM;
  61. if (!map->reg_defaults_raw) {
  62. bool cache_bypass = map->cache_bypass;
  63. dev_dbg(map->dev, "No cache defaults, reading back from HW\n");
  64. /* Bypass the cache access till data read from HW */
  65. map->cache_bypass = true;
  66. tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
  67. if (!tmp_buf) {
  68. ret = -ENOMEM;
  69. goto err_free;
  70. }
  71. ret = regmap_raw_read(map, 0, tmp_buf,
  72. map->cache_size_raw);
  73. map->cache_bypass = cache_bypass;
  74. if (ret == 0) {
  75. map->reg_defaults_raw = tmp_buf;
  76. map->cache_free = true;
  77. } else {
  78. kfree(tmp_buf);
  79. }
  80. }
  81. /* fill the reg_defaults */
  82. for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
  83. reg = i * map->reg_stride;
  84. if (!regmap_readable(map, reg))
  85. continue;
  86. if (regmap_volatile(map, reg))
  87. continue;
  88. if (map->reg_defaults_raw) {
  89. val = regcache_get_val(map, map->reg_defaults_raw, i);
  90. } else {
  91. bool cache_bypass = map->cache_bypass;
  92. map->cache_bypass = true;
  93. ret = regmap_read(map, reg, &val);
  94. map->cache_bypass = cache_bypass;
  95. if (ret != 0) {
  96. dev_err(map->dev, "Failed to read %d: %d\n",
  97. reg, ret);
  98. goto err_free;
  99. }
  100. }
  101. map->reg_defaults[j].reg = reg;
  102. map->reg_defaults[j].def = val;
  103. j++;
  104. }
  105. return 0;
  106. err_free:
  107. kfree(map->reg_defaults);
  108. return ret;
  109. }
  110. int regcache_init(struct regmap *map, const struct regmap_config *config)
  111. {
  112. int ret;
  113. int i;
  114. void *tmp_buf;
  115. if (map->cache_type == REGCACHE_NONE) {
  116. if (config->reg_defaults || config->num_reg_defaults_raw)
  117. dev_warn(map->dev,
  118. "No cache used with register defaults set!\n");
  119. map->cache_bypass = true;
  120. return 0;
  121. }
  122. if (config->reg_defaults && !config->num_reg_defaults) {
  123. dev_err(map->dev,
  124. "Register defaults are set without the number!\n");
  125. return -EINVAL;
  126. }
  127. if (config->num_reg_defaults && !config->reg_defaults) {
  128. dev_err(map->dev,
  129. "Register defaults number are set without the reg!\n");
  130. return -EINVAL;
  131. }
  132. for (i = 0; i < config->num_reg_defaults; i++)
  133. if (config->reg_defaults[i].reg % map->reg_stride)
  134. return -EINVAL;
  135. for (i = 0; i < ARRAY_SIZE(cache_types); i++)
  136. if (cache_types[i]->type == map->cache_type)
  137. break;
  138. if (i == ARRAY_SIZE(cache_types)) {
  139. dev_err(map->dev, "Could not match cache type: %d\n",
  140. map->cache_type);
  141. return -EINVAL;
  142. }
  143. map->num_reg_defaults = config->num_reg_defaults;
  144. map->num_reg_defaults_raw = config->num_reg_defaults_raw;
  145. map->reg_defaults_raw = config->reg_defaults_raw;
  146. map->cache_word_size = BITS_TO_BYTES(config->val_bits);
  147. map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
  148. map->cache = NULL;
  149. map->cache_ops = cache_types[i];
  150. if (!map->cache_ops->read ||
  151. !map->cache_ops->write ||
  152. !map->cache_ops->name)
  153. return -EINVAL;
  154. /* We still need to ensure that the reg_defaults
  155. * won't vanish from under us. We'll need to make
  156. * a copy of it.
  157. */
  158. if (config->reg_defaults) {
  159. tmp_buf = kmemdup_array(config->reg_defaults, map->num_reg_defaults,
  160. sizeof(*map->reg_defaults), GFP_KERNEL);
  161. if (!tmp_buf)
  162. return -ENOMEM;
  163. map->reg_defaults = tmp_buf;
  164. } else if (map->num_reg_defaults_raw) {
  165. /* Some devices such as PMICs don't have cache defaults,
  166. * we cope with this by reading back the HW registers and
  167. * crafting the cache defaults by hand.
  168. */
  169. ret = regcache_hw_init(map);
  170. if (ret < 0)
  171. return ret;
  172. if (map->cache_bypass)
  173. return 0;
  174. }
  175. if (!map->max_register_is_set && map->num_reg_defaults_raw) {
  176. map->max_register = (map->num_reg_defaults_raw - 1) * map->reg_stride;
  177. map->max_register_is_set = true;
  178. }
  179. if (map->cache_ops->init) {
  180. dev_dbg(map->dev, "Initializing %s cache\n",
  181. map->cache_ops->name);
  182. map->lock(map->lock_arg);
  183. ret = map->cache_ops->init(map);
  184. map->unlock(map->lock_arg);
  185. if (ret)
  186. goto err_free;
  187. }
  188. if (map->cache_ops->populate &&
  189. (map->num_reg_defaults || map->reg_default_cb)) {
  190. dev_dbg(map->dev, "Populating %s cache\n", map->cache_ops->name);
  191. map->lock(map->lock_arg);
  192. ret = map->cache_ops->populate(map);
  193. map->unlock(map->lock_arg);
  194. if (ret)
  195. goto err_exit;
  196. }
  197. return 0;
  198. err_exit:
  199. if (map->cache_ops->exit) {
  200. dev_dbg(map->dev, "Destroying %s cache\n", map->cache_ops->name);
  201. map->lock(map->lock_arg);
  202. ret = map->cache_ops->exit(map);
  203. map->unlock(map->lock_arg);
  204. }
  205. err_free:
  206. kfree(map->reg_defaults);
  207. if (map->cache_free)
  208. kfree(map->reg_defaults_raw);
  209. return ret;
  210. }
  211. void regcache_exit(struct regmap *map)
  212. {
  213. if (map->cache_type == REGCACHE_NONE)
  214. return;
  215. BUG_ON(!map->cache_ops);
  216. kfree(map->reg_defaults);
  217. if (map->cache_free)
  218. kfree(map->reg_defaults_raw);
  219. if (map->cache_ops->exit) {
  220. dev_dbg(map->dev, "Destroying %s cache\n",
  221. map->cache_ops->name);
  222. map->lock(map->lock_arg);
  223. map->cache_ops->exit(map);
  224. map->unlock(map->lock_arg);
  225. }
  226. }
  227. /**
  228. * regcache_read - Fetch the value of a given register from the cache.
  229. *
  230. * @map: map to configure.
  231. * @reg: The register index.
  232. * @value: The value to be returned.
  233. *
  234. * Return a negative value on failure, 0 on success.
  235. */
  236. int regcache_read(struct regmap *map,
  237. unsigned int reg, unsigned int *value)
  238. {
  239. int ret;
  240. if (map->cache_type == REGCACHE_NONE)
  241. return -EINVAL;
  242. BUG_ON(!map->cache_ops);
  243. if (!regmap_volatile(map, reg)) {
  244. ret = map->cache_ops->read(map, reg, value);
  245. if (ret == 0)
  246. trace_regmap_reg_read_cache(map, reg, *value);
  247. return ret;
  248. }
  249. return -EINVAL;
  250. }
  251. /**
  252. * regcache_write - Set the value of a given register in the cache.
  253. *
  254. * @map: map to configure.
  255. * @reg: The register index.
  256. * @value: The new register value.
  257. *
  258. * Return a negative value on failure, 0 on success.
  259. */
  260. int regcache_write(struct regmap *map,
  261. unsigned int reg, unsigned int value)
  262. {
  263. if (map->cache_type == REGCACHE_NONE)
  264. return 0;
  265. BUG_ON(!map->cache_ops);
  266. if (!regmap_volatile(map, reg))
  267. return map->cache_ops->write(map, reg, value);
  268. return 0;
  269. }
  270. bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
  271. unsigned int val)
  272. {
  273. int ret;
  274. if (!regmap_writeable(map, reg))
  275. return false;
  276. /* If we don't know the chip just got reset, then sync everything. */
  277. if (!map->no_sync_defaults)
  278. return true;
  279. /* Is this the hardware default? If so skip. */
  280. ret = regcache_lookup_reg(map, reg);
  281. if (ret >= 0 && val == map->reg_defaults[ret].def)
  282. return false;
  283. return true;
  284. }
  285. static int regcache_default_sync(struct regmap *map, unsigned int min,
  286. unsigned int max)
  287. {
  288. unsigned int reg;
  289. for (reg = min; reg <= max; reg += map->reg_stride) {
  290. unsigned int val;
  291. int ret;
  292. if (regmap_volatile(map, reg) ||
  293. !regmap_writeable(map, reg))
  294. continue;
  295. ret = regcache_read(map, reg, &val);
  296. if (ret == -ENOENT)
  297. continue;
  298. if (ret)
  299. return ret;
  300. if (!regcache_reg_needs_sync(map, reg, val))
  301. continue;
  302. map->cache_bypass = true;
  303. ret = _regmap_write(map, reg, val);
  304. map->cache_bypass = false;
  305. if (ret) {
  306. dev_err(map->dev, "Unable to sync register %#x. %d\n",
  307. reg, ret);
  308. return ret;
  309. }
  310. dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
  311. }
  312. return 0;
  313. }
  314. static int rbtree_all(const void *key, const struct rb_node *node)
  315. {
  316. return 0;
  317. }
  318. /**
  319. * regcache_sync - Sync the register cache with the hardware.
  320. *
  321. * @map: map to configure.
  322. *
  323. * Any registers that should not be synced should be marked as
  324. * volatile. In general drivers can choose not to use the provided
  325. * syncing functionality if they so require.
  326. *
  327. * Return a negative value on failure, 0 on success.
  328. */
  329. int regcache_sync(struct regmap *map)
  330. {
  331. int ret = 0;
  332. unsigned int i;
  333. const char *name;
  334. bool bypass;
  335. struct rb_node *node;
  336. if (WARN_ON(map->cache_type == REGCACHE_NONE))
  337. return -EINVAL;
  338. BUG_ON(!map->cache_ops);
  339. map->lock(map->lock_arg);
  340. /* Remember the initial bypass state */
  341. bypass = map->cache_bypass;
  342. dev_dbg(map->dev, "Syncing %s cache\n",
  343. map->cache_ops->name);
  344. name = map->cache_ops->name;
  345. trace_regcache_sync(map, name, "start");
  346. if (!map->cache_dirty)
  347. goto out;
  348. /* Apply any patch first */
  349. map->cache_bypass = true;
  350. for (i = 0; i < map->patch_regs; i++) {
  351. ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
  352. if (ret != 0) {
  353. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  354. map->patch[i].reg, map->patch[i].def, ret);
  355. goto out;
  356. }
  357. }
  358. map->cache_bypass = false;
  359. if (map->cache_ops->sync)
  360. ret = map->cache_ops->sync(map, 0, map->max_register);
  361. else
  362. ret = regcache_default_sync(map, 0, map->max_register);
  363. if (ret == 0)
  364. map->cache_dirty = false;
  365. out:
  366. /* Restore the bypass state */
  367. map->cache_bypass = bypass;
  368. map->no_sync_defaults = false;
  369. /*
  370. * If we did any paging with cache bypassed and a cached
  371. * paging register then the register and cache state might
  372. * have gone out of sync, force writes of all the paging
  373. * registers.
  374. */
  375. rb_for_each(node, NULL, &map->range_tree, rbtree_all) {
  376. struct regmap_range_node *this =
  377. rb_entry(node, struct regmap_range_node, node);
  378. /* If there's nothing in the cache there's nothing to sync */
  379. if (regcache_read(map, this->selector_reg, &i) != 0)
  380. continue;
  381. ret = _regmap_write(map, this->selector_reg, i);
  382. if (ret != 0) {
  383. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  384. this->selector_reg, i, ret);
  385. break;
  386. }
  387. }
  388. map->unlock(map->lock_arg);
  389. regmap_async_complete(map);
  390. trace_regcache_sync(map, name, "stop");
  391. return ret;
  392. }
  393. EXPORT_SYMBOL_GPL(regcache_sync);
  394. /**
  395. * regcache_sync_region - Sync part of the register cache with the hardware.
  396. *
  397. * @map: map to sync.
  398. * @min: first register to sync
  399. * @max: last register to sync
  400. *
  401. * Write all non-default register values in the specified region to
  402. * the hardware.
  403. *
  404. * Return a negative value on failure, 0 on success.
  405. */
  406. int regcache_sync_region(struct regmap *map, unsigned int min,
  407. unsigned int max)
  408. {
  409. int ret = 0;
  410. const char *name;
  411. bool bypass;
  412. if (WARN_ON(map->cache_type == REGCACHE_NONE))
  413. return -EINVAL;
  414. BUG_ON(!map->cache_ops);
  415. map->lock(map->lock_arg);
  416. /* Remember the initial bypass state */
  417. bypass = map->cache_bypass;
  418. name = map->cache_ops->name;
  419. dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
  420. trace_regcache_sync(map, name, "start region");
  421. if (!map->cache_dirty)
  422. goto out;
  423. map->async = true;
  424. if (map->cache_ops->sync)
  425. ret = map->cache_ops->sync(map, min, max);
  426. else
  427. ret = regcache_default_sync(map, min, max);
  428. out:
  429. /* Restore the bypass state */
  430. map->cache_bypass = bypass;
  431. map->async = false;
  432. map->no_sync_defaults = false;
  433. map->unlock(map->lock_arg);
  434. regmap_async_complete(map);
  435. trace_regcache_sync(map, name, "stop region");
  436. return ret;
  437. }
  438. EXPORT_SYMBOL_GPL(regcache_sync_region);
  439. /**
  440. * regcache_drop_region - Discard part of the register cache
  441. *
  442. * @map: map to operate on
  443. * @min: first register to discard
  444. * @max: last register to discard
  445. *
  446. * Discard part of the register cache.
  447. *
  448. * Return a negative value on failure, 0 on success.
  449. */
  450. int regcache_drop_region(struct regmap *map, unsigned int min,
  451. unsigned int max)
  452. {
  453. int ret = 0;
  454. if (!map->cache_ops || !map->cache_ops->drop)
  455. return -EINVAL;
  456. map->lock(map->lock_arg);
  457. trace_regcache_drop_region(map, min, max);
  458. ret = map->cache_ops->drop(map, min, max);
  459. map->unlock(map->lock_arg);
  460. return ret;
  461. }
  462. EXPORT_SYMBOL_GPL(regcache_drop_region);
  463. /**
  464. * regcache_cache_only - Put a register map into cache only mode
  465. *
  466. * @map: map to configure
  467. * @enable: flag if changes should be written to the hardware
  468. *
  469. * When a register map is marked as cache only writes to the register
  470. * map API will only update the register cache, they will not cause
  471. * any hardware changes. This is useful for allowing portions of
  472. * drivers to act as though the device were functioning as normal when
  473. * it is disabled for power saving reasons.
  474. */
  475. void regcache_cache_only(struct regmap *map, bool enable)
  476. {
  477. map->lock(map->lock_arg);
  478. WARN_ON(map->cache_type != REGCACHE_NONE &&
  479. map->cache_bypass && enable);
  480. map->cache_only = enable;
  481. trace_regmap_cache_only(map, enable);
  482. map->unlock(map->lock_arg);
  483. }
  484. EXPORT_SYMBOL_GPL(regcache_cache_only);
  485. /**
  486. * regcache_mark_dirty - Indicate that HW registers were reset to default values
  487. *
  488. * @map: map to mark
  489. *
  490. * Inform regcache that the device has been powered down or reset, so that
  491. * on resume, regcache_sync() knows to write out all non-default values
  492. * stored in the cache.
  493. *
  494. * If this function is not called, regcache_sync() will assume that
  495. * the hardware state still matches the cache state, modulo any writes that
  496. * happened when cache_only was true.
  497. */
  498. void regcache_mark_dirty(struct regmap *map)
  499. {
  500. map->lock(map->lock_arg);
  501. map->cache_dirty = true;
  502. map->no_sync_defaults = true;
  503. map->unlock(map->lock_arg);
  504. }
  505. EXPORT_SYMBOL_GPL(regcache_mark_dirty);
  506. /**
  507. * regcache_cache_bypass - Put a register map into cache bypass mode
  508. *
  509. * @map: map to configure
  510. * @enable: flag if changes should not be written to the cache
  511. *
  512. * When a register map is marked with the cache bypass option, writes
  513. * to the register map API will only update the hardware and not
  514. * the cache directly. This is useful when syncing the cache back to
  515. * the hardware.
  516. */
  517. void regcache_cache_bypass(struct regmap *map, bool enable)
  518. {
  519. map->lock(map->lock_arg);
  520. WARN_ON(map->cache_only && enable);
  521. map->cache_bypass = enable;
  522. trace_regmap_cache_bypass(map, enable);
  523. map->unlock(map->lock_arg);
  524. }
  525. EXPORT_SYMBOL_GPL(regcache_cache_bypass);
  526. /**
  527. * regcache_reg_cached - Check if a register is cached
  528. *
  529. * @map: map to check
  530. * @reg: register to check
  531. *
  532. * Reports if a register is cached.
  533. */
  534. bool regcache_reg_cached(struct regmap *map, unsigned int reg)
  535. {
  536. unsigned int val;
  537. int ret;
  538. map->lock(map->lock_arg);
  539. ret = regcache_read(map, reg, &val);
  540. map->unlock(map->lock_arg);
  541. return ret == 0;
  542. }
  543. EXPORT_SYMBOL_GPL(regcache_reg_cached);
  544. void regcache_set_val(struct regmap *map, void *base, unsigned int idx,
  545. unsigned int val)
  546. {
  547. /* Use device native format if possible */
  548. if (map->format.format_val) {
  549. map->format.format_val(base + (map->cache_word_size * idx),
  550. val, 0);
  551. return;
  552. }
  553. switch (map->cache_word_size) {
  554. case 1: {
  555. u8 *cache = base;
  556. cache[idx] = val;
  557. break;
  558. }
  559. case 2: {
  560. u16 *cache = base;
  561. cache[idx] = val;
  562. break;
  563. }
  564. case 4: {
  565. u32 *cache = base;
  566. cache[idx] = val;
  567. break;
  568. }
  569. default:
  570. BUG();
  571. }
  572. }
  573. unsigned int regcache_get_val(struct regmap *map, const void *base,
  574. unsigned int idx)
  575. {
  576. if (!base)
  577. return -EINVAL;
  578. /* Use device native format if possible */
  579. if (map->format.parse_val)
  580. return map->format.parse_val(regcache_get_val_addr(map, base,
  581. idx));
  582. switch (map->cache_word_size) {
  583. case 1: {
  584. const u8 *cache = base;
  585. return cache[idx];
  586. }
  587. case 2: {
  588. const u16 *cache = base;
  589. return cache[idx];
  590. }
  591. case 4: {
  592. const u32 *cache = base;
  593. return cache[idx];
  594. }
  595. default:
  596. BUG();
  597. }
  598. /* unreachable */
  599. return -1;
  600. }
  601. static int regcache_default_cmp(const void *a, const void *b)
  602. {
  603. const struct reg_default *_a = a;
  604. const struct reg_default *_b = b;
  605. return _a->reg - _b->reg;
  606. }
  607. int regcache_lookup_reg(struct regmap *map, unsigned int reg)
  608. {
  609. struct reg_default key;
  610. struct reg_default *r;
  611. key.reg = reg;
  612. key.def = 0;
  613. r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
  614. sizeof(struct reg_default), regcache_default_cmp);
  615. if (r)
  616. return r - map->reg_defaults;
  617. else
  618. return -ENOENT;
  619. }
  620. static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
  621. {
  622. if (!cache_present)
  623. return true;
  624. return test_bit(idx, cache_present);
  625. }
  626. int regcache_sync_val(struct regmap *map, unsigned int reg, unsigned int val)
  627. {
  628. int ret;
  629. if (!regcache_reg_needs_sync(map, reg, val))
  630. return 0;
  631. map->cache_bypass = true;
  632. ret = _regmap_write(map, reg, val);
  633. map->cache_bypass = false;
  634. if (ret != 0) {
  635. dev_err(map->dev, "Unable to sync register %#x. %d\n",
  636. reg, ret);
  637. return ret;
  638. }
  639. dev_dbg(map->dev, "Synced register %#x, value %#x\n",
  640. reg, val);
  641. return 0;
  642. }
  643. static int regcache_sync_block_single(struct regmap *map, void *block,
  644. unsigned long *cache_present,
  645. unsigned int block_base,
  646. unsigned int start, unsigned int end)
  647. {
  648. unsigned int i, regtmp, val;
  649. int ret;
  650. for (i = start; i < end; i++) {
  651. regtmp = block_base + (i * map->reg_stride);
  652. if (!regcache_reg_present(cache_present, i) ||
  653. !regmap_writeable(map, regtmp))
  654. continue;
  655. val = regcache_get_val(map, block, i);
  656. ret = regcache_sync_val(map, regtmp, val);
  657. if (ret != 0)
  658. return ret;
  659. }
  660. return 0;
  661. }
  662. static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
  663. unsigned int base, unsigned int cur)
  664. {
  665. size_t val_bytes = map->format.val_bytes;
  666. int ret, count;
  667. if (*data == NULL)
  668. return 0;
  669. count = (cur - base) / map->reg_stride;
  670. dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
  671. count * val_bytes, count, base, cur - map->reg_stride);
  672. map->cache_bypass = true;
  673. ret = _regmap_raw_write(map, base, *data, count * val_bytes, false);
  674. if (ret)
  675. dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
  676. base, cur - map->reg_stride, ret);
  677. map->cache_bypass = false;
  678. *data = NULL;
  679. return ret;
  680. }
  681. static int regcache_sync_block_raw(struct regmap *map, void *block,
  682. unsigned long *cache_present,
  683. unsigned int block_base, unsigned int start,
  684. unsigned int end)
  685. {
  686. unsigned int i, val;
  687. unsigned int regtmp = 0;
  688. unsigned int base = 0;
  689. const void *data = NULL;
  690. int ret;
  691. for (i = start; i < end; i++) {
  692. regtmp = block_base + (i * map->reg_stride);
  693. if (!regcache_reg_present(cache_present, i) ||
  694. !regmap_writeable(map, regtmp)) {
  695. ret = regcache_sync_block_raw_flush(map, &data,
  696. base, regtmp);
  697. if (ret != 0)
  698. return ret;
  699. continue;
  700. }
  701. val = regcache_get_val(map, block, i);
  702. if (!regcache_reg_needs_sync(map, regtmp, val)) {
  703. ret = regcache_sync_block_raw_flush(map, &data,
  704. base, regtmp);
  705. if (ret != 0)
  706. return ret;
  707. continue;
  708. }
  709. if (!data) {
  710. data = regcache_get_val_addr(map, block, i);
  711. base = regtmp;
  712. }
  713. }
  714. return regcache_sync_block_raw_flush(map, &data, base, regtmp +
  715. map->reg_stride);
  716. }
  717. int regcache_sync_block(struct regmap *map, void *block,
  718. unsigned long *cache_present,
  719. unsigned int block_base, unsigned int start,
  720. unsigned int end)
  721. {
  722. if (regmap_can_raw_write(map) && !map->use_single_write)
  723. return regcache_sync_block_raw(map, block, cache_present,
  724. block_base, start, end);
  725. else
  726. return regcache_sync_block_single(map, block, cache_present,
  727. block_base, start, end);
  728. }