regcache.c 18 KB

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