regcache.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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_present = NULL;
  106. map->cache_present_nbits = 0;
  107. map->cache = NULL;
  108. map->cache_ops = cache_types[i];
  109. if (!map->cache_ops->read ||
  110. !map->cache_ops->write ||
  111. !map->cache_ops->name)
  112. return -EINVAL;
  113. /* We still need to ensure that the reg_defaults
  114. * won't vanish from under us. We'll need to make
  115. * a copy of it.
  116. */
  117. if (config->reg_defaults) {
  118. if (!map->num_reg_defaults)
  119. return -EINVAL;
  120. tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
  121. sizeof(struct reg_default), GFP_KERNEL);
  122. if (!tmp_buf)
  123. return -ENOMEM;
  124. map->reg_defaults = tmp_buf;
  125. } else if (map->num_reg_defaults_raw) {
  126. /* Some devices such as PMICs don't have cache defaults,
  127. * we cope with this by reading back the HW registers and
  128. * crafting the cache defaults by hand.
  129. */
  130. ret = regcache_hw_init(map);
  131. if (ret < 0)
  132. return ret;
  133. }
  134. if (!map->max_register)
  135. map->max_register = map->num_reg_defaults_raw;
  136. if (map->cache_ops->init) {
  137. dev_dbg(map->dev, "Initializing %s cache\n",
  138. map->cache_ops->name);
  139. ret = map->cache_ops->init(map);
  140. if (ret)
  141. goto err_free;
  142. }
  143. return 0;
  144. err_free:
  145. kfree(map->reg_defaults);
  146. if (map->cache_free)
  147. kfree(map->reg_defaults_raw);
  148. return ret;
  149. }
  150. void regcache_exit(struct regmap *map)
  151. {
  152. if (map->cache_type == REGCACHE_NONE)
  153. return;
  154. BUG_ON(!map->cache_ops);
  155. kfree(map->cache_present);
  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->dev, 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_writeable(map, reg))
  205. return -EIO;
  206. if (!regmap_volatile(map, reg))
  207. return map->cache_ops->write(map, reg, value);
  208. return 0;
  209. }
  210. static int regcache_default_sync(struct regmap *map, unsigned int min,
  211. unsigned int max)
  212. {
  213. unsigned int reg;
  214. for (reg = min; reg <= max; reg++) {
  215. unsigned int val;
  216. int ret;
  217. if (regmap_volatile(map, reg))
  218. continue;
  219. ret = regcache_read(map, reg, &val);
  220. if (ret)
  221. return ret;
  222. /* Is this the hardware default? If so skip. */
  223. ret = regcache_lookup_reg(map, reg);
  224. if (ret >= 0 && val == map->reg_defaults[ret].def)
  225. continue;
  226. map->cache_bypass = 1;
  227. ret = _regmap_write(map, reg, val);
  228. map->cache_bypass = 0;
  229. if (ret)
  230. return ret;
  231. dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
  232. }
  233. return 0;
  234. }
  235. /**
  236. * regcache_sync: Sync the register cache with the hardware.
  237. *
  238. * @map: map to configure.
  239. *
  240. * Any registers that should not be synced should be marked as
  241. * volatile. In general drivers can choose not to use the provided
  242. * syncing functionality if they so require.
  243. *
  244. * Return a negative value on failure, 0 on success.
  245. */
  246. int regcache_sync(struct regmap *map)
  247. {
  248. int ret = 0;
  249. unsigned int i;
  250. const char *name;
  251. unsigned int bypass;
  252. BUG_ON(!map->cache_ops);
  253. map->lock(map->lock_arg);
  254. /* Remember the initial bypass state */
  255. bypass = map->cache_bypass;
  256. dev_dbg(map->dev, "Syncing %s cache\n",
  257. map->cache_ops->name);
  258. name = map->cache_ops->name;
  259. trace_regcache_sync(map->dev, name, "start");
  260. if (!map->cache_dirty)
  261. goto out;
  262. /* Apply any patch first */
  263. map->cache_bypass = 1;
  264. for (i = 0; i < map->patch_regs; i++) {
  265. if (map->patch[i].reg % map->reg_stride) {
  266. ret = -EINVAL;
  267. goto out;
  268. }
  269. ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
  270. if (ret != 0) {
  271. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  272. map->patch[i].reg, map->patch[i].def, ret);
  273. goto out;
  274. }
  275. }
  276. map->cache_bypass = 0;
  277. if (map->cache_ops->sync)
  278. ret = map->cache_ops->sync(map, 0, map->max_register);
  279. else
  280. ret = regcache_default_sync(map, 0, map->max_register);
  281. if (ret == 0)
  282. map->cache_dirty = false;
  283. out:
  284. trace_regcache_sync(map->dev, name, "stop");
  285. /* Restore the bypass state */
  286. map->cache_bypass = bypass;
  287. map->unlock(map->lock_arg);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL_GPL(regcache_sync);
  291. /**
  292. * regcache_sync_region: Sync part of the register cache with the hardware.
  293. *
  294. * @map: map to sync.
  295. * @min: first register to sync
  296. * @max: last register to sync
  297. *
  298. * Write all non-default register values in the specified region to
  299. * the hardware.
  300. *
  301. * Return a negative value on failure, 0 on success.
  302. */
  303. int regcache_sync_region(struct regmap *map, unsigned int min,
  304. unsigned int max)
  305. {
  306. int ret = 0;
  307. const char *name;
  308. unsigned int bypass;
  309. BUG_ON(!map->cache_ops);
  310. map->lock(map->lock_arg);
  311. /* Remember the initial bypass state */
  312. bypass = map->cache_bypass;
  313. name = map->cache_ops->name;
  314. dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
  315. trace_regcache_sync(map->dev, name, "start region");
  316. if (!map->cache_dirty)
  317. goto out;
  318. if (map->cache_ops->sync)
  319. ret = map->cache_ops->sync(map, min, max);
  320. else
  321. ret = regcache_default_sync(map, min, max);
  322. out:
  323. trace_regcache_sync(map->dev, name, "stop region");
  324. /* Restore the bypass state */
  325. map->cache_bypass = bypass;
  326. map->unlock(map->lock_arg);
  327. return ret;
  328. }
  329. EXPORT_SYMBOL_GPL(regcache_sync_region);
  330. /**
  331. * regcache_drop_region: Discard part of the register cache
  332. *
  333. * @map: map to operate on
  334. * @min: first register to discard
  335. * @max: last register to discard
  336. *
  337. * Discard part of the register cache.
  338. *
  339. * Return a negative value on failure, 0 on success.
  340. */
  341. int regcache_drop_region(struct regmap *map, unsigned int min,
  342. unsigned int max)
  343. {
  344. unsigned int reg;
  345. int ret = 0;
  346. if (!map->cache_present && !(map->cache_ops && map->cache_ops->drop))
  347. return -EINVAL;
  348. map->lock(map->lock_arg);
  349. trace_regcache_drop_region(map->dev, min, max);
  350. if (map->cache_present)
  351. for (reg = min; reg < max + 1; reg++)
  352. clear_bit(reg, map->cache_present);
  353. if (map->cache_ops && map->cache_ops->drop)
  354. ret = map->cache_ops->drop(map, min, max);
  355. map->unlock(map->lock_arg);
  356. return ret;
  357. }
  358. EXPORT_SYMBOL_GPL(regcache_drop_region);
  359. /**
  360. * regcache_cache_only: Put a register map into cache only mode
  361. *
  362. * @map: map to configure
  363. * @cache_only: flag if changes should be written to the hardware
  364. *
  365. * When a register map is marked as cache only writes to the register
  366. * map API will only update the register cache, they will not cause
  367. * any hardware changes. This is useful for allowing portions of
  368. * drivers to act as though the device were functioning as normal when
  369. * it is disabled for power saving reasons.
  370. */
  371. void regcache_cache_only(struct regmap *map, bool enable)
  372. {
  373. map->lock(map->lock_arg);
  374. WARN_ON(map->cache_bypass && enable);
  375. map->cache_only = enable;
  376. trace_regmap_cache_only(map->dev, enable);
  377. map->unlock(map->lock_arg);
  378. }
  379. EXPORT_SYMBOL_GPL(regcache_cache_only);
  380. /**
  381. * regcache_mark_dirty: Mark the register cache as dirty
  382. *
  383. * @map: map to mark
  384. *
  385. * Mark the register cache as dirty, for example due to the device
  386. * having been powered down for suspend. If the cache is not marked
  387. * as dirty then the cache sync will be suppressed.
  388. */
  389. void regcache_mark_dirty(struct regmap *map)
  390. {
  391. map->lock(map->lock_arg);
  392. map->cache_dirty = true;
  393. map->unlock(map->lock_arg);
  394. }
  395. EXPORT_SYMBOL_GPL(regcache_mark_dirty);
  396. /**
  397. * regcache_cache_bypass: Put a register map into cache bypass mode
  398. *
  399. * @map: map to configure
  400. * @cache_bypass: flag if changes should not be written to the hardware
  401. *
  402. * When a register map is marked with the cache bypass option, writes
  403. * to the register map API will only update the hardware and not the
  404. * the cache directly. This is useful when syncing the cache back to
  405. * the hardware.
  406. */
  407. void regcache_cache_bypass(struct regmap *map, bool enable)
  408. {
  409. map->lock(map->lock_arg);
  410. WARN_ON(map->cache_only && enable);
  411. map->cache_bypass = enable;
  412. trace_regmap_cache_bypass(map->dev, enable);
  413. map->unlock(map->lock_arg);
  414. }
  415. EXPORT_SYMBOL_GPL(regcache_cache_bypass);
  416. int regcache_set_reg_present(struct regmap *map, unsigned int reg)
  417. {
  418. unsigned long *cache_present;
  419. unsigned int cache_present_size;
  420. unsigned int nregs;
  421. int i;
  422. nregs = reg + 1;
  423. cache_present_size = BITS_TO_LONGS(nregs);
  424. cache_present_size *= sizeof(long);
  425. if (!map->cache_present) {
  426. cache_present = kmalloc(cache_present_size, GFP_KERNEL);
  427. if (!cache_present)
  428. return -ENOMEM;
  429. bitmap_zero(cache_present, nregs);
  430. map->cache_present = cache_present;
  431. map->cache_present_nbits = nregs;
  432. }
  433. if (nregs > map->cache_present_nbits) {
  434. cache_present = krealloc(map->cache_present,
  435. cache_present_size, GFP_KERNEL);
  436. if (!cache_present)
  437. return -ENOMEM;
  438. for (i = 0; i < nregs; i++)
  439. if (i >= map->cache_present_nbits)
  440. clear_bit(i, cache_present);
  441. map->cache_present = cache_present;
  442. map->cache_present_nbits = nregs;
  443. }
  444. set_bit(reg, map->cache_present);
  445. return 0;
  446. }
  447. bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
  448. unsigned int val)
  449. {
  450. if (regcache_get_val(map, base, idx) == val)
  451. return true;
  452. /* Use device native format if possible */
  453. if (map->format.format_val) {
  454. map->format.format_val(base + (map->cache_word_size * idx),
  455. val, 0);
  456. return false;
  457. }
  458. switch (map->cache_word_size) {
  459. case 1: {
  460. u8 *cache = base;
  461. cache[idx] = val;
  462. break;
  463. }
  464. case 2: {
  465. u16 *cache = base;
  466. cache[idx] = val;
  467. break;
  468. }
  469. case 4: {
  470. u32 *cache = base;
  471. cache[idx] = val;
  472. break;
  473. }
  474. default:
  475. BUG();
  476. }
  477. return false;
  478. }
  479. unsigned int regcache_get_val(struct regmap *map, const void *base,
  480. unsigned int idx)
  481. {
  482. if (!base)
  483. return -EINVAL;
  484. /* Use device native format if possible */
  485. if (map->format.parse_val)
  486. return map->format.parse_val(regcache_get_val_addr(map, base,
  487. idx));
  488. switch (map->cache_word_size) {
  489. case 1: {
  490. const u8 *cache = base;
  491. return cache[idx];
  492. }
  493. case 2: {
  494. const u16 *cache = base;
  495. return cache[idx];
  496. }
  497. case 4: {
  498. const u32 *cache = base;
  499. return cache[idx];
  500. }
  501. default:
  502. BUG();
  503. }
  504. /* unreachable */
  505. return -1;
  506. }
  507. static int regcache_default_cmp(const void *a, const void *b)
  508. {
  509. const struct reg_default *_a = a;
  510. const struct reg_default *_b = b;
  511. return _a->reg - _b->reg;
  512. }
  513. int regcache_lookup_reg(struct regmap *map, unsigned int reg)
  514. {
  515. struct reg_default key;
  516. struct reg_default *r;
  517. key.reg = reg;
  518. key.def = 0;
  519. r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
  520. sizeof(struct reg_default), regcache_default_cmp);
  521. if (r)
  522. return r - map->reg_defaults;
  523. else
  524. return -ENOENT;
  525. }
  526. static int regcache_sync_block_single(struct regmap *map, void *block,
  527. unsigned int block_base,
  528. unsigned int start, unsigned int end)
  529. {
  530. unsigned int i, regtmp, val;
  531. int ret;
  532. for (i = start; i < end; i++) {
  533. regtmp = block_base + (i * map->reg_stride);
  534. if (!regcache_reg_present(map, regtmp))
  535. continue;
  536. val = regcache_get_val(map, block, i);
  537. /* Is this the hardware default? If so skip. */
  538. ret = regcache_lookup_reg(map, regtmp);
  539. if (ret >= 0 && val == map->reg_defaults[ret].def)
  540. continue;
  541. map->cache_bypass = 1;
  542. ret = _regmap_write(map, regtmp, val);
  543. map->cache_bypass = 0;
  544. if (ret != 0)
  545. return ret;
  546. dev_dbg(map->dev, "Synced register %#x, value %#x\n",
  547. regtmp, val);
  548. }
  549. return 0;
  550. }
  551. static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
  552. unsigned int base, unsigned int cur)
  553. {
  554. size_t val_bytes = map->format.val_bytes;
  555. int ret, count;
  556. if (*data == NULL)
  557. return 0;
  558. count = cur - base;
  559. dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
  560. count * val_bytes, count, base, cur - 1);
  561. map->cache_bypass = 1;
  562. ret = _regmap_raw_write(map, base, *data, count * val_bytes,
  563. false);
  564. map->cache_bypass = 0;
  565. *data = NULL;
  566. return ret;
  567. }
  568. static int regcache_sync_block_raw(struct regmap *map, void *block,
  569. unsigned int block_base, unsigned int start,
  570. unsigned int end)
  571. {
  572. unsigned int i, val;
  573. unsigned int regtmp = 0;
  574. unsigned int base = 0;
  575. const void *data = NULL;
  576. int ret;
  577. for (i = start; i < end; i++) {
  578. regtmp = block_base + (i * map->reg_stride);
  579. if (!regcache_reg_present(map, regtmp)) {
  580. ret = regcache_sync_block_raw_flush(map, &data,
  581. base, regtmp);
  582. if (ret != 0)
  583. return ret;
  584. continue;
  585. }
  586. val = regcache_get_val(map, block, i);
  587. /* Is this the hardware default? If so skip. */
  588. ret = regcache_lookup_reg(map, regtmp);
  589. if (ret >= 0 && val == map->reg_defaults[ret].def) {
  590. ret = regcache_sync_block_raw_flush(map, &data,
  591. base, regtmp);
  592. if (ret != 0)
  593. return ret;
  594. continue;
  595. }
  596. if (!data) {
  597. data = regcache_get_val_addr(map, block, i);
  598. base = regtmp;
  599. }
  600. }
  601. return regcache_sync_block_raw_flush(map, &data, base, regtmp);
  602. }
  603. int regcache_sync_block(struct regmap *map, void *block,
  604. unsigned int block_base, unsigned int start,
  605. unsigned int end)
  606. {
  607. if (regmap_can_raw_write(map))
  608. return regcache_sync_block_raw(map, block, block_base,
  609. start, end);
  610. else
  611. return regcache_sync_block_single(map, block, block_base,
  612. start, end);
  613. }