regcache.c 17 KB

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