regcache.c 19 KB

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