regcache.c 16 KB

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