regmap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #ifndef __REGMAP_H
  7. #define __REGMAP_H
  8. /**
  9. * DOC: Overview
  10. *
  11. * Regmaps are an abstraction mechanism that allows device drivers to access
  12. * register maps irrespective of the underlying bus architecture. This entails
  13. * that for devices that support multiple busses (e.g. I2C and SPI for a GPIO
  14. * expander chip) only one driver has to be written. This driver will
  15. * instantiate a regmap with a backend depending on the bus the device is
  16. * attached to, and use the regmap API to access the register map through that
  17. * bus transparently.
  18. *
  19. * Read and write functions are supplied, which can read/write data of
  20. * arbitrary length from/to the regmap.
  21. *
  22. * The endianness of regmap accesses is selectable for each map through device
  23. * tree settings via the boolean "little-endian", "big-endian", and
  24. * "native-endian" properties.
  25. *
  26. * Furthermore, the register map described by a regmap can be split into
  27. * multiple disjoint areas called ranges. In this way, register maps with
  28. * "holes", i.e. areas of addressable memory that are not part of the register
  29. * map, can be accessed in a concise manner.
  30. *
  31. * Currently, only a bare "mem" backend for regmaps is supported, which
  32. * accesses the register map as regular IO-mapped memory.
  33. */
  34. /**
  35. * enum regmap_size_t - Access sizes for regmap reads and writes
  36. *
  37. * @REGMAP_SIZE_8: 8-bit read/write access size
  38. * @REGMAP_SIZE_16: 16-bit read/write access size
  39. * @REGMAP_SIZE_32: 32-bit read/write access size
  40. * @REGMAP_SIZE_64: 64-bit read/write access size
  41. */
  42. enum regmap_size_t {
  43. REGMAP_SIZE_8 = 1,
  44. REGMAP_SIZE_16 = 2,
  45. REGMAP_SIZE_32 = 4,
  46. REGMAP_SIZE_64 = 8,
  47. };
  48. /**
  49. * enum regmap_endianness_t - Endianness for regmap reads and writes
  50. *
  51. * @REGMAP_NATIVE_ENDIAN: Native endian read/write accesses
  52. * @REGMAP_LITTLE_ENDIAN: Little endian read/write accesses
  53. * @REGMAP_BIG_ENDIAN: Big endian read/write accesses
  54. */
  55. enum regmap_endianness_t {
  56. REGMAP_NATIVE_ENDIAN,
  57. REGMAP_LITTLE_ENDIAN,
  58. REGMAP_BIG_ENDIAN,
  59. };
  60. /**
  61. * struct regmap_range - a register map range
  62. *
  63. * @start: Start address
  64. * @size: Size in bytes
  65. */
  66. struct regmap_range {
  67. ulong start;
  68. ulong size;
  69. };
  70. /**
  71. * struct regmap - a way of accessing hardware/bus registers
  72. *
  73. * @range_count: Number of ranges available within the map
  74. * @ranges: Array of ranges
  75. */
  76. struct regmap {
  77. enum regmap_endianness_t endianness;
  78. int range_count;
  79. struct regmap_range ranges[0];
  80. };
  81. /*
  82. * Interface to provide access to registers either through a direct memory
  83. * bus or through a peripheral bus like I2C, SPI.
  84. */
  85. /**
  86. * regmap_write() - Write a 32-bit value to a regmap
  87. *
  88. * @map: Regmap to write to
  89. * @offset: Offset in the regmap to write to
  90. * @val: Data to write to the regmap at the specified offset
  91. *
  92. * Note that this function will only write values of 32 bit width to the
  93. * regmap; if the size of data to be read is different, the regmap_raw_write
  94. * function can be used.
  95. *
  96. * Return: 0 if OK, -ve on error
  97. */
  98. int regmap_write(struct regmap *map, uint offset, uint val);
  99. /**
  100. * regmap_read() - Read a 32-bit value from a regmap
  101. *
  102. * @map: Regmap to read from
  103. * @offset: Offset in the regmap to read from
  104. * @valp: Pointer to the buffer to receive the data read from the regmap
  105. * at the specified offset
  106. *
  107. * Note that this function will only read values of 32 bit width from the
  108. * regmap; if the size of data to be read is different, the regmap_raw_read
  109. * function can be used.
  110. *
  111. * Return: 0 if OK, -ve on error
  112. */
  113. int regmap_read(struct regmap *map, uint offset, uint *valp);
  114. /**
  115. * regmap_raw_write() - Write a value of specified length to a regmap
  116. *
  117. * @map: Regmap to write to
  118. * @offset: Offset in the regmap to write to
  119. * @val: Value to write to the regmap at the specified offset
  120. * @val_len: Length of the data to be written to the regmap
  121. *
  122. * Note that this function will, as opposed to regmap_write, write data of
  123. * arbitrary length to the regmap, and not just 32-bit values, and is thus a
  124. * generalized version of regmap_write.
  125. *
  126. * Return: 0 if OK, -ve on error
  127. */
  128. int regmap_raw_write(struct regmap *map, uint offset, const void *val,
  129. size_t val_len);
  130. /**
  131. * regmap_raw_read() - Read a value of specified length from a regmap
  132. *
  133. * @map: Regmap to read from
  134. * @offset: Offset in the regmap to read from
  135. * @valp: Pointer to the buffer to receive the data read from the regmap
  136. * at the specified offset
  137. * @val_len: Length of the data to be read from the regmap
  138. *
  139. * Note that this function will, as opposed to regmap_read, read data of
  140. * arbitrary length from the regmap, and not just 32-bit values, and is thus a
  141. * generalized version of regmap_read.
  142. *
  143. * Return: 0 if OK, -ve on error
  144. */
  145. int regmap_raw_read(struct regmap *map, uint offset, void *valp,
  146. size_t val_len);
  147. /**
  148. * regmap_raw_write_range() - Write a value of specified length to a range of a
  149. * regmap
  150. *
  151. * @map: Regmap to write to
  152. * @range_num: Number of the range in the regmap to write to
  153. * @offset: Offset in the regmap to write to
  154. * @val: Value to write to the regmap at the specified offset
  155. * @val_len: Length of the data to be written to the regmap
  156. *
  157. * Return: 0 if OK, -ve on error
  158. */
  159. int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
  160. const void *val, size_t val_len);
  161. /**
  162. * regmap_raw_read_range() - Read a value of specified length from a range of a
  163. * regmap
  164. *
  165. * @map: Regmap to read from
  166. * @range_num: Number of the range in the regmap to write to
  167. * @offset: Offset in the regmap to read from
  168. * @valp: Pointer to the buffer to receive the data read from the regmap
  169. * at the specified offset
  170. * @val_len: Length of the data to be read from the regmap
  171. *
  172. * Return: 0 if OK, -ve on error
  173. */
  174. int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
  175. void *valp, size_t val_len);
  176. /**
  177. * regmap_range_set() - Set a value in a regmap range described by a struct
  178. * @map: Regmap in which a value should be set
  179. * @range: Range of the regmap in which a value should be set
  180. * @type: Structure type that describes the memory layout of the regmap range
  181. * @member: Member of the describing structure that should be set in the regmap
  182. * range
  183. * @val: Value which should be written to the regmap range
  184. */
  185. #define regmap_range_set(map, range, type, member, val) \
  186. do { \
  187. typeof(((type *)0)->member) __tmp = val; \
  188. regmap_raw_write_range(map, range, offsetof(type, member), \
  189. &__tmp, sizeof(((type *)0)->member)); \
  190. } while (0)
  191. /**
  192. * regmap_set() - Set a value in a regmap described by a struct
  193. * @map: Regmap in which a value should be set
  194. * @type: Structure type that describes the memory layout of the regmap
  195. * @member: Member of the describing structure that should be set in the regmap
  196. * @val: Value which should be written to the regmap
  197. */
  198. #define regmap_set(map, type, member, val) \
  199. regmap_range_set(map, 0, type, member, val)
  200. /**
  201. * regmap_range_get() - Get a value from a regmap range described by a struct
  202. * @map: Regmap from which a value should be read
  203. * @range: Range of the regmap from which a value should be read
  204. * @type: Structure type that describes the memory layout of the regmap
  205. * range
  206. * @member: Member of the describing structure that should be read in the
  207. * regmap range
  208. * @valp: Variable that receives the value read from the regmap range
  209. */
  210. #define regmap_range_get(map, range, type, member, valp) \
  211. regmap_raw_read_range(map, range, offsetof(type, member), \
  212. (void *)valp, sizeof(((type *)0)->member))
  213. /**
  214. * regmap_get() - Get a value from a regmap described by a struct
  215. * @map: Regmap from which a value should be read
  216. * @type: Structure type that describes the memory layout of the regmap
  217. * range
  218. * @member: Member of the describing structure that should be read in the
  219. * regmap
  220. * @valp: Variable that receives the value read from the regmap
  221. */
  222. #define regmap_get(map, type, member, valp) \
  223. regmap_range_get(map, 0, type, member, valp)
  224. /**
  225. * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
  226. *
  227. * @map: Regmap to read from
  228. * @addr: Offset to poll
  229. * @val: Unsigned integer variable to read the value into
  230. * @cond: Break condition (usually involving @val)
  231. * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops).
  232. * @timeout_ms: Timeout in ms, 0 means never timeout
  233. * @test_add_time: Used for sandbox testing - amount of time to add after
  234. * starting the loop (0 if not testing)
  235. *
  236. * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
  237. * error return value in case of a error read. In the two former cases,
  238. * the last read value at @addr is stored in @val. Must not be called
  239. * from atomic context if sleep_us or timeout_us are used.
  240. *
  241. * This is modelled after the regmap_read_poll_timeout macros in linux but
  242. * with millisecond timeout.
  243. *
  244. * The _test version is for sandbox testing only. Do not use this in normal
  245. * code as it advances the timer.
  246. */
  247. #define regmap_read_poll_timeout_test(map, addr, val, cond, sleep_us, \
  248. timeout_ms, test_add_time) \
  249. ({ \
  250. unsigned long __start = get_timer(0); \
  251. int __ret; \
  252. for (;;) { \
  253. __ret = regmap_read((map), (addr), &(val)); \
  254. if (__ret) \
  255. break; \
  256. if (cond) \
  257. break; \
  258. if (IS_ENABLED(CONFIG_SANDBOX) && test_add_time) \
  259. timer_test_add_offset(test_add_time); \
  260. if ((timeout_ms) && get_timer(__start) > (timeout_ms)) { \
  261. __ret = regmap_read((map), (addr), &(val)); \
  262. break; \
  263. } \
  264. if ((sleep_us)) \
  265. udelay((sleep_us)); \
  266. } \
  267. __ret ?: ((cond) ? 0 : -ETIMEDOUT); \
  268. })
  269. #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_ms) \
  270. regmap_read_poll_timeout_test(map, addr, val, cond, sleep_us, \
  271. timeout_ms, 0) \
  272. /**
  273. * regmap_update_bits() - Perform a read/modify/write using a mask
  274. *
  275. * @map: The map returned by regmap_init_mem*()
  276. * @offset: Offset of the memory
  277. * @mask: Mask to apply to the read value
  278. * @val: Value to OR with the read value after masking. Note that any
  279. * bits set in @val which are not set in @mask are ignored
  280. * Return: 0 if OK, -ve on error
  281. */
  282. int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val);
  283. /**
  284. * regmap_init_mem() - Set up a new register map that uses memory access
  285. *
  286. * @node: Device node that uses this map
  287. * @mapp: Returns allocated map
  288. * Return: 0 if OK, -ve on error
  289. *
  290. * Use regmap_uninit() to free it.
  291. */
  292. int regmap_init_mem(ofnode node, struct regmap **mapp);
  293. /**
  294. * regmap_init_mem_platdata() - Set up a new memory register map for
  295. * of-platdata
  296. *
  297. * @dev: Device that uses this map
  298. * @reg: List of address, size pairs
  299. * @count: Number of pairs (e.g. 1 if the regmap has a single entry)
  300. * @mapp: Returns allocated map
  301. * Return: 0 if OK, -ve on error
  302. *
  303. * This creates a new regmap with a list of regions passed in, rather than
  304. * using the device tree. It only supports 32-bit machines.
  305. *
  306. * Use regmap_uninit() to free it.
  307. *
  308. */
  309. int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
  310. struct regmap **mapp);
  311. int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index);
  312. /**
  313. * regmap_get_range() - Obtain the base memory address of a regmap range
  314. *
  315. * @map: Regmap to query
  316. * @range_num: Range to look up
  317. * Return: Pointer to the range in question if OK, NULL on error
  318. */
  319. void *regmap_get_range(struct regmap *map, unsigned int range_num);
  320. /**
  321. * regmap_uninit() - free a previously inited regmap
  322. *
  323. * @map: Regmap to free
  324. * Return: 0 if OK, -ve on error
  325. */
  326. int regmap_uninit(struct regmap *map);
  327. #endif