regmap.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. #ifndef __LINUX_REGMAP_H
  2. #define __LINUX_REGMAP_H
  3. /*
  4. * Register map access API
  5. *
  6. * Copyright 2011 Wolfson Microelectronics plc
  7. *
  8. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/list.h>
  15. #include <linux/rbtree.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/bug.h>
  19. #include <linux/lockdep.h>
  20. struct module;
  21. struct device;
  22. struct i2c_client;
  23. struct irq_domain;
  24. struct spi_device;
  25. struct spmi_device;
  26. struct regmap;
  27. struct regmap_range_cfg;
  28. struct regmap_field;
  29. struct snd_ac97;
  30. /* An enum of all the supported cache types */
  31. enum regcache_type {
  32. REGCACHE_NONE,
  33. REGCACHE_RBTREE,
  34. REGCACHE_COMPRESSED,
  35. REGCACHE_FLAT,
  36. };
  37. /**
  38. * Default value for a register. We use an array of structs rather
  39. * than a simple array as many modern devices have very sparse
  40. * register maps.
  41. *
  42. * @reg: Register address.
  43. * @def: Register default value.
  44. */
  45. struct reg_default {
  46. unsigned int reg;
  47. unsigned int def;
  48. };
  49. /**
  50. * Register/value pairs for sequences of writes with an optional delay in
  51. * microseconds to be applied after each write.
  52. *
  53. * @reg: Register address.
  54. * @def: Register value.
  55. * @delay_us: Delay to be applied after the register write in microseconds
  56. */
  57. struct reg_sequence {
  58. unsigned int reg;
  59. unsigned int def;
  60. unsigned int delay_us;
  61. };
  62. #define regmap_update_bits(map, reg, mask, val) \
  63. regmap_update_bits_base(map, reg, mask, val, NULL, false, false)
  64. #define regmap_update_bits_async(map, reg, mask, val)\
  65. regmap_update_bits_base(map, reg, mask, val, NULL, true, false)
  66. #define regmap_update_bits_check(map, reg, mask, val, change)\
  67. regmap_update_bits_base(map, reg, mask, val, change, false, false)
  68. #define regmap_update_bits_check_async(map, reg, mask, val, change)\
  69. regmap_update_bits_base(map, reg, mask, val, change, true, false)
  70. #define regmap_write_bits(map, reg, mask, val) \
  71. regmap_update_bits_base(map, reg, mask, val, NULL, false, true)
  72. #define regmap_field_write(field, val) \
  73. regmap_field_update_bits_base(field, ~0, val, NULL, false, false)
  74. #define regmap_field_force_write(field, val) \
  75. regmap_field_update_bits_base(field, ~0, val, NULL, false, true)
  76. #define regmap_field_update_bits(field, mask, val)\
  77. regmap_field_update_bits_base(field, mask, val, NULL, false, false)
  78. #define regmap_field_force_update_bits(field, mask, val) \
  79. regmap_field_update_bits_base(field, mask, val, NULL, false, true)
  80. #define regmap_fields_write(field, id, val) \
  81. regmap_fields_update_bits_base(field, id, ~0, val, NULL, false, false)
  82. #define regmap_fields_force_write(field, id, val) \
  83. regmap_fields_update_bits_base(field, id, ~0, val, NULL, false, true)
  84. #define regmap_fields_update_bits(field, id, mask, val)\
  85. regmap_fields_update_bits_base(field, id, mask, val, NULL, false, false)
  86. #define regmap_fields_force_update_bits(field, id, mask, val) \
  87. regmap_fields_update_bits_base(field, id, mask, val, NULL, false, true)
  88. /**
  89. * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
  90. * @map: Regmap to read from
  91. * @addr: Address to poll
  92. * @val: Unsigned integer variable to read the value into
  93. * @cond: Break condition (usually involving @val)
  94. * @sleep_us: Maximum time to sleep between reads in us (0
  95. * tight-loops). Should be less than ~20ms since usleep_range
  96. * is used (see Documentation/timers/timers-howto.txt).
  97. * @timeout_us: Timeout in us, 0 means never timeout
  98. *
  99. * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
  100. * error return value in case of a error read. In the two former cases,
  101. * the last read value at @addr is stored in @val. Must not be called
  102. * from atomic context if sleep_us or timeout_us are used.
  103. *
  104. * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
  105. */
  106. #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_us) \
  107. ({ \
  108. ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \
  109. int pollret; \
  110. might_sleep_if(sleep_us); \
  111. for (;;) { \
  112. pollret = regmap_read((map), (addr), &(val)); \
  113. if (pollret) \
  114. break; \
  115. if (cond) \
  116. break; \
  117. if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
  118. pollret = regmap_read((map), (addr), &(val)); \
  119. break; \
  120. } \
  121. if (sleep_us) \
  122. usleep_range((sleep_us >> 2) + 1, sleep_us); \
  123. } \
  124. pollret ?: ((cond) ? 0 : -ETIMEDOUT); \
  125. })
  126. #ifdef CONFIG_REGMAP
  127. enum regmap_endian {
  128. /* Unspecified -> 0 -> Backwards compatible default */
  129. REGMAP_ENDIAN_DEFAULT = 0,
  130. REGMAP_ENDIAN_BIG,
  131. REGMAP_ENDIAN_LITTLE,
  132. REGMAP_ENDIAN_NATIVE,
  133. };
  134. /**
  135. * A register range, used for access related checks
  136. * (readable/writeable/volatile/precious checks)
  137. *
  138. * @range_min: address of first register
  139. * @range_max: address of last register
  140. */
  141. struct regmap_range {
  142. unsigned int range_min;
  143. unsigned int range_max;
  144. };
  145. #define regmap_reg_range(low, high) { .range_min = low, .range_max = high, }
  146. /*
  147. * A table of ranges including some yes ranges and some no ranges.
  148. * If a register belongs to a no_range, the corresponding check function
  149. * will return false. If a register belongs to a yes range, the corresponding
  150. * check function will return true. "no_ranges" are searched first.
  151. *
  152. * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
  153. * @n_yes_ranges: size of the above array
  154. * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
  155. * @n_no_ranges: size of the above array
  156. */
  157. struct regmap_access_table {
  158. const struct regmap_range *yes_ranges;
  159. unsigned int n_yes_ranges;
  160. const struct regmap_range *no_ranges;
  161. unsigned int n_no_ranges;
  162. };
  163. typedef void (*regmap_lock)(void *);
  164. typedef void (*regmap_unlock)(void *);
  165. /**
  166. * Configuration for the register map of a device.
  167. *
  168. * @name: Optional name of the regmap. Useful when a device has multiple
  169. * register regions.
  170. *
  171. * @reg_bits: Number of bits in a register address, mandatory.
  172. * @reg_stride: The register address stride. Valid register addresses are a
  173. * multiple of this value. If set to 0, a value of 1 will be
  174. * used.
  175. * @pad_bits: Number of bits of padding between register and value.
  176. * @val_bits: Number of bits in a register value, mandatory.
  177. *
  178. * @writeable_reg: Optional callback returning true if the register
  179. * can be written to. If this field is NULL but wr_table
  180. * (see below) is not, the check is performed on such table
  181. * (a register is writeable if it belongs to one of the ranges
  182. * specified by wr_table).
  183. * @readable_reg: Optional callback returning true if the register
  184. * can be read from. If this field is NULL but rd_table
  185. * (see below) is not, the check is performed on such table
  186. * (a register is readable if it belongs to one of the ranges
  187. * specified by rd_table).
  188. * @volatile_reg: Optional callback returning true if the register
  189. * value can't be cached. If this field is NULL but
  190. * volatile_table (see below) is not, the check is performed on
  191. * such table (a register is volatile if it belongs to one of
  192. * the ranges specified by volatile_table).
  193. * @precious_reg: Optional callback returning true if the register
  194. * should not be read outside of a call from the driver
  195. * (e.g., a clear on read interrupt status register). If this
  196. * field is NULL but precious_table (see below) is not, the
  197. * check is performed on such table (a register is precious if
  198. * it belongs to one of the ranges specified by precious_table).
  199. * @lock: Optional lock callback (overrides regmap's default lock
  200. * function, based on spinlock or mutex).
  201. * @unlock: As above for unlocking.
  202. * @lock_arg: this field is passed as the only argument of lock/unlock
  203. * functions (ignored in case regular lock/unlock functions
  204. * are not overridden).
  205. * @reg_read: Optional callback that if filled will be used to perform
  206. * all the reads from the registers. Should only be provided for
  207. * devices whose read operation cannot be represented as a simple
  208. * read operation on a bus such as SPI, I2C, etc. Most of the
  209. * devices do not need this.
  210. * @reg_write: Same as above for writing.
  211. * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
  212. * to perform locking. This field is ignored if custom lock/unlock
  213. * functions are used (see fields lock/unlock of struct regmap_config).
  214. * This field is a duplicate of a similar file in
  215. * 'struct regmap_bus' and serves exact same purpose.
  216. * Use it only for "no-bus" cases.
  217. * @max_register: Optional, specifies the maximum valid register address.
  218. * @wr_table: Optional, points to a struct regmap_access_table specifying
  219. * valid ranges for write access.
  220. * @rd_table: As above, for read access.
  221. * @volatile_table: As above, for volatile registers.
  222. * @precious_table: As above, for precious registers.
  223. * @reg_defaults: Power on reset values for registers (for use with
  224. * register cache support).
  225. * @num_reg_defaults: Number of elements in reg_defaults.
  226. *
  227. * @read_flag_mask: Mask to be set in the top bytes of the register when doing
  228. * a read.
  229. * @write_flag_mask: Mask to be set in the top bytes of the register when doing
  230. * a write. If both read_flag_mask and write_flag_mask are
  231. * empty the regmap_bus default masks are used.
  232. * @use_single_rw: If set, converts the bulk read and write operations into
  233. * a series of single read and write operations. This is useful
  234. * for device that does not support bulk read and write.
  235. * @can_multi_write: If set, the device supports the multi write mode of bulk
  236. * write operations, if clear multi write requests will be
  237. * split into individual write operations
  238. *
  239. * @cache_type: The actual cache type.
  240. * @reg_defaults_raw: Power on reset values for registers (for use with
  241. * register cache support).
  242. * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
  243. * @reg_format_endian: Endianness for formatted register addresses. If this is
  244. * DEFAULT, the @reg_format_endian_default value from the
  245. * regmap bus is used.
  246. * @val_format_endian: Endianness for formatted register values. If this is
  247. * DEFAULT, the @reg_format_endian_default value from the
  248. * regmap bus is used.
  249. *
  250. * @ranges: Array of configuration entries for virtual address ranges.
  251. * @num_ranges: Number of range configuration entries.
  252. */
  253. struct regmap_config {
  254. const char *name;
  255. int reg_bits;
  256. int reg_stride;
  257. int pad_bits;
  258. int val_bits;
  259. bool (*writeable_reg)(struct device *dev, unsigned int reg);
  260. bool (*readable_reg)(struct device *dev, unsigned int reg);
  261. bool (*volatile_reg)(struct device *dev, unsigned int reg);
  262. bool (*precious_reg)(struct device *dev, unsigned int reg);
  263. regmap_lock lock;
  264. regmap_unlock unlock;
  265. void *lock_arg;
  266. int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
  267. int (*reg_write)(void *context, unsigned int reg, unsigned int val);
  268. bool fast_io;
  269. unsigned int max_register;
  270. const struct regmap_access_table *wr_table;
  271. const struct regmap_access_table *rd_table;
  272. const struct regmap_access_table *volatile_table;
  273. const struct regmap_access_table *precious_table;
  274. const struct reg_default *reg_defaults;
  275. unsigned int num_reg_defaults;
  276. enum regcache_type cache_type;
  277. const void *reg_defaults_raw;
  278. unsigned int num_reg_defaults_raw;
  279. unsigned long read_flag_mask;
  280. unsigned long write_flag_mask;
  281. bool use_single_rw;
  282. bool can_multi_write;
  283. enum regmap_endian reg_format_endian;
  284. enum regmap_endian val_format_endian;
  285. const struct regmap_range_cfg *ranges;
  286. unsigned int num_ranges;
  287. };
  288. /**
  289. * Configuration for indirectly accessed or paged registers.
  290. * Registers, mapped to this virtual range, are accessed in two steps:
  291. * 1. page selector register update;
  292. * 2. access through data window registers.
  293. *
  294. * @name: Descriptive name for diagnostics
  295. *
  296. * @range_min: Address of the lowest register address in virtual range.
  297. * @range_max: Address of the highest register in virtual range.
  298. *
  299. * @page_sel_reg: Register with selector field.
  300. * @page_sel_mask: Bit shift for selector value.
  301. * @page_sel_shift: Bit mask for selector value.
  302. *
  303. * @window_start: Address of first (lowest) register in data window.
  304. * @window_len: Number of registers in data window.
  305. */
  306. struct regmap_range_cfg {
  307. const char *name;
  308. /* Registers of virtual address range */
  309. unsigned int range_min;
  310. unsigned int range_max;
  311. /* Page selector for indirect addressing */
  312. unsigned int selector_reg;
  313. unsigned int selector_mask;
  314. int selector_shift;
  315. /* Data window (per each page) */
  316. unsigned int window_start;
  317. unsigned int window_len;
  318. };
  319. struct regmap_async;
  320. typedef int (*regmap_hw_write)(void *context, const void *data,
  321. size_t count);
  322. typedef int (*regmap_hw_gather_write)(void *context,
  323. const void *reg, size_t reg_len,
  324. const void *val, size_t val_len);
  325. typedef int (*regmap_hw_async_write)(void *context,
  326. const void *reg, size_t reg_len,
  327. const void *val, size_t val_len,
  328. struct regmap_async *async);
  329. typedef int (*regmap_hw_read)(void *context,
  330. const void *reg_buf, size_t reg_size,
  331. void *val_buf, size_t val_size);
  332. typedef int (*regmap_hw_reg_read)(void *context, unsigned int reg,
  333. unsigned int *val);
  334. typedef int (*regmap_hw_reg_write)(void *context, unsigned int reg,
  335. unsigned int val);
  336. typedef int (*regmap_hw_reg_update_bits)(void *context, unsigned int reg,
  337. unsigned int mask, unsigned int val);
  338. typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
  339. typedef void (*regmap_hw_free_context)(void *context);
  340. /**
  341. * Description of a hardware bus for the register map infrastructure.
  342. *
  343. * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
  344. * to perform locking. This field is ignored if custom lock/unlock
  345. * functions are used (see fields lock/unlock of
  346. * struct regmap_config).
  347. * @write: Write operation.
  348. * @gather_write: Write operation with split register/value, return -ENOTSUPP
  349. * if not implemented on a given device.
  350. * @async_write: Write operation which completes asynchronously, optional and
  351. * must serialise with respect to non-async I/O.
  352. * @reg_write: Write a single register value to the given register address. This
  353. * write operation has to complete when returning from the function.
  354. * @read: Read operation. Data is returned in the buffer used to transmit
  355. * data.
  356. * @reg_read: Read a single register value from a given register address.
  357. * @free_context: Free context.
  358. * @async_alloc: Allocate a regmap_async() structure.
  359. * @read_flag_mask: Mask to be set in the top byte of the register when doing
  360. * a read.
  361. * @reg_format_endian_default: Default endianness for formatted register
  362. * addresses. Used when the regmap_config specifies DEFAULT. If this is
  363. * DEFAULT, BIG is assumed.
  364. * @val_format_endian_default: Default endianness for formatted register
  365. * values. Used when the regmap_config specifies DEFAULT. If this is
  366. * DEFAULT, BIG is assumed.
  367. * @max_raw_read: Max raw read size that can be used on the bus.
  368. * @max_raw_write: Max raw write size that can be used on the bus.
  369. */
  370. struct regmap_bus {
  371. bool fast_io;
  372. regmap_hw_write write;
  373. regmap_hw_gather_write gather_write;
  374. regmap_hw_async_write async_write;
  375. regmap_hw_reg_write reg_write;
  376. regmap_hw_reg_update_bits reg_update_bits;
  377. regmap_hw_read read;
  378. regmap_hw_reg_read reg_read;
  379. regmap_hw_free_context free_context;
  380. regmap_hw_async_alloc async_alloc;
  381. u8 read_flag_mask;
  382. enum regmap_endian reg_format_endian_default;
  383. enum regmap_endian val_format_endian_default;
  384. size_t max_raw_read;
  385. size_t max_raw_write;
  386. };
  387. /*
  388. * __regmap_init functions.
  389. *
  390. * These functions take a lock key and name parameter, and should not be called
  391. * directly. Instead, use the regmap_init macros that generate a key and name
  392. * for each call.
  393. */
  394. struct regmap *__regmap_init(struct device *dev,
  395. const struct regmap_bus *bus,
  396. void *bus_context,
  397. const struct regmap_config *config,
  398. struct lock_class_key *lock_key,
  399. const char *lock_name);
  400. struct regmap *__regmap_init_i2c(struct i2c_client *i2c,
  401. const struct regmap_config *config,
  402. struct lock_class_key *lock_key,
  403. const char *lock_name);
  404. struct regmap *__regmap_init_spi(struct spi_device *dev,
  405. const struct regmap_config *config,
  406. struct lock_class_key *lock_key,
  407. const char *lock_name);
  408. struct regmap *__regmap_init_spmi_base(struct spmi_device *dev,
  409. const struct regmap_config *config,
  410. struct lock_class_key *lock_key,
  411. const char *lock_name);
  412. struct regmap *__regmap_init_spmi_ext(struct spmi_device *dev,
  413. const struct regmap_config *config,
  414. struct lock_class_key *lock_key,
  415. const char *lock_name);
  416. struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
  417. void __iomem *regs,
  418. const struct regmap_config *config,
  419. struct lock_class_key *lock_key,
  420. const char *lock_name);
  421. struct regmap *__regmap_init_ac97(struct snd_ac97 *ac97,
  422. const struct regmap_config *config,
  423. struct lock_class_key *lock_key,
  424. const char *lock_name);
  425. struct regmap *__devm_regmap_init(struct device *dev,
  426. const struct regmap_bus *bus,
  427. void *bus_context,
  428. const struct regmap_config *config,
  429. struct lock_class_key *lock_key,
  430. const char *lock_name);
  431. struct regmap *__devm_regmap_init_i2c(struct i2c_client *i2c,
  432. const struct regmap_config *config,
  433. struct lock_class_key *lock_key,
  434. const char *lock_name);
  435. struct regmap *__devm_regmap_init_spi(struct spi_device *dev,
  436. const struct regmap_config *config,
  437. struct lock_class_key *lock_key,
  438. const char *lock_name);
  439. struct regmap *__devm_regmap_init_spmi_base(struct spmi_device *dev,
  440. const struct regmap_config *config,
  441. struct lock_class_key *lock_key,
  442. const char *lock_name);
  443. struct regmap *__devm_regmap_init_spmi_ext(struct spmi_device *dev,
  444. const struct regmap_config *config,
  445. struct lock_class_key *lock_key,
  446. const char *lock_name);
  447. struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
  448. const char *clk_id,
  449. void __iomem *regs,
  450. const struct regmap_config *config,
  451. struct lock_class_key *lock_key,
  452. const char *lock_name);
  453. struct regmap *__devm_regmap_init_ac97(struct snd_ac97 *ac97,
  454. const struct regmap_config *config,
  455. struct lock_class_key *lock_key,
  456. const char *lock_name);
  457. /*
  458. * Wrapper for regmap_init macros to include a unique lockdep key and name
  459. * for each call. No-op if CONFIG_LOCKDEP is not set.
  460. *
  461. * @fn: Real function to call (in the form __[*_]regmap_init[_*])
  462. * @name: Config variable name (#config in the calling macro)
  463. **/
  464. #ifdef CONFIG_LOCKDEP
  465. #define __regmap_lockdep_wrapper(fn, name, ...) \
  466. ( \
  467. ({ \
  468. static struct lock_class_key _key; \
  469. fn(__VA_ARGS__, &_key, \
  470. KBUILD_BASENAME ":" \
  471. __stringify(__LINE__) ":" \
  472. "(" name ")->lock"); \
  473. }) \
  474. )
  475. #else
  476. #define __regmap_lockdep_wrapper(fn, name, ...) fn(__VA_ARGS__, NULL, NULL)
  477. #endif
  478. /**
  479. * regmap_init(): Initialise register map
  480. *
  481. * @dev: Device that will be interacted with
  482. * @bus: Bus-specific callbacks to use with device
  483. * @bus_context: Data passed to bus-specific callbacks
  484. * @config: Configuration for register map
  485. *
  486. * The return value will be an ERR_PTR() on error or a valid pointer to
  487. * a struct regmap. This function should generally not be called
  488. * directly, it should be called by bus-specific init functions.
  489. */
  490. #define regmap_init(dev, bus, bus_context, config) \
  491. __regmap_lockdep_wrapper(__regmap_init, #config, \
  492. dev, bus, bus_context, config)
  493. int regmap_attach_dev(struct device *dev, struct regmap *map,
  494. const struct regmap_config *config);
  495. /**
  496. * regmap_init_i2c(): Initialise register map
  497. *
  498. * @i2c: Device that will be interacted with
  499. * @config: Configuration for register map
  500. *
  501. * The return value will be an ERR_PTR() on error or a valid pointer to
  502. * a struct regmap.
  503. */
  504. #define regmap_init_i2c(i2c, config) \
  505. __regmap_lockdep_wrapper(__regmap_init_i2c, #config, \
  506. i2c, config)
  507. /**
  508. * regmap_init_spi(): Initialise register map
  509. *
  510. * @spi: Device that will be interacted with
  511. * @config: Configuration for register map
  512. *
  513. * The return value will be an ERR_PTR() on error or a valid pointer to
  514. * a struct regmap.
  515. */
  516. #define regmap_init_spi(dev, config) \
  517. __regmap_lockdep_wrapper(__regmap_init_spi, #config, \
  518. dev, config)
  519. /**
  520. * regmap_init_spmi_base(): Create regmap for the Base register space
  521. * @sdev: SPMI device that will be interacted with
  522. * @config: Configuration for register map
  523. *
  524. * The return value will be an ERR_PTR() on error or a valid pointer to
  525. * a struct regmap.
  526. */
  527. #define regmap_init_spmi_base(dev, config) \
  528. __regmap_lockdep_wrapper(__regmap_init_spmi_base, #config, \
  529. dev, config)
  530. /**
  531. * regmap_init_spmi_ext(): Create regmap for Ext register space
  532. * @sdev: Device that will be interacted with
  533. * @config: Configuration for register map
  534. *
  535. * The return value will be an ERR_PTR() on error or a valid pointer to
  536. * a struct regmap.
  537. */
  538. #define regmap_init_spmi_ext(dev, config) \
  539. __regmap_lockdep_wrapper(__regmap_init_spmi_ext, #config, \
  540. dev, config)
  541. /**
  542. * regmap_init_mmio_clk(): Initialise register map with register clock
  543. *
  544. * @dev: Device that will be interacted with
  545. * @clk_id: register clock consumer ID
  546. * @regs: Pointer to memory-mapped IO region
  547. * @config: Configuration for register map
  548. *
  549. * The return value will be an ERR_PTR() on error or a valid pointer to
  550. * a struct regmap.
  551. */
  552. #define regmap_init_mmio_clk(dev, clk_id, regs, config) \
  553. __regmap_lockdep_wrapper(__regmap_init_mmio_clk, #config, \
  554. dev, clk_id, regs, config)
  555. /**
  556. * regmap_init_mmio(): Initialise register map
  557. *
  558. * @dev: Device that will be interacted with
  559. * @regs: Pointer to memory-mapped IO region
  560. * @config: Configuration for register map
  561. *
  562. * The return value will be an ERR_PTR() on error or a valid pointer to
  563. * a struct regmap.
  564. */
  565. #define regmap_init_mmio(dev, regs, config) \
  566. regmap_init_mmio_clk(dev, NULL, regs, config)
  567. /**
  568. * regmap_init_ac97(): Initialise AC'97 register map
  569. *
  570. * @ac97: Device that will be interacted with
  571. * @config: Configuration for register map
  572. *
  573. * The return value will be an ERR_PTR() on error or a valid pointer to
  574. * a struct regmap.
  575. */
  576. #define regmap_init_ac97(ac97, config) \
  577. __regmap_lockdep_wrapper(__regmap_init_ac97, #config, \
  578. ac97, config)
  579. bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
  580. /**
  581. * devm_regmap_init(): Initialise managed register map
  582. *
  583. * @dev: Device that will be interacted with
  584. * @bus: Bus-specific callbacks to use with device
  585. * @bus_context: Data passed to bus-specific callbacks
  586. * @config: Configuration for register map
  587. *
  588. * The return value will be an ERR_PTR() on error or a valid pointer
  589. * to a struct regmap. This function should generally not be called
  590. * directly, it should be called by bus-specific init functions. The
  591. * map will be automatically freed by the device management code.
  592. */
  593. #define devm_regmap_init(dev, bus, bus_context, config) \
  594. __regmap_lockdep_wrapper(__devm_regmap_init, #config, \
  595. dev, bus, bus_context, config)
  596. /**
  597. * devm_regmap_init_i2c(): Initialise managed register map
  598. *
  599. * @i2c: Device that will be interacted with
  600. * @config: Configuration for register map
  601. *
  602. * The return value will be an ERR_PTR() on error or a valid pointer
  603. * to a struct regmap. The regmap will be automatically freed by the
  604. * device management code.
  605. */
  606. #define devm_regmap_init_i2c(i2c, config) \
  607. __regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config, \
  608. i2c, config)
  609. /**
  610. * devm_regmap_init_spi(): Initialise register map
  611. *
  612. * @spi: Device that will be interacted with
  613. * @config: Configuration for register map
  614. *
  615. * The return value will be an ERR_PTR() on error or a valid pointer
  616. * to a struct regmap. The map will be automatically freed by the
  617. * device management code.
  618. */
  619. #define devm_regmap_init_spi(dev, config) \
  620. __regmap_lockdep_wrapper(__devm_regmap_init_spi, #config, \
  621. dev, config)
  622. /**
  623. * devm_regmap_init_spmi_base(): Create managed regmap for Base register space
  624. * @sdev: SPMI device that will be interacted with
  625. * @config: Configuration for register map
  626. *
  627. * The return value will be an ERR_PTR() on error or a valid pointer
  628. * to a struct regmap. The regmap will be automatically freed by the
  629. * device management code.
  630. */
  631. #define devm_regmap_init_spmi_base(dev, config) \
  632. __regmap_lockdep_wrapper(__devm_regmap_init_spmi_base, #config, \
  633. dev, config)
  634. /**
  635. * devm_regmap_init_spmi_ext(): Create managed regmap for Ext register space
  636. * @sdev: SPMI device that will be interacted with
  637. * @config: Configuration for register map
  638. *
  639. * The return value will be an ERR_PTR() on error or a valid pointer
  640. * to a struct regmap. The regmap will be automatically freed by the
  641. * device management code.
  642. */
  643. #define devm_regmap_init_spmi_ext(dev, config) \
  644. __regmap_lockdep_wrapper(__devm_regmap_init_spmi_ext, #config, \
  645. dev, config)
  646. /**
  647. * devm_regmap_init_mmio_clk(): Initialise managed register map with clock
  648. *
  649. * @dev: Device that will be interacted with
  650. * @clk_id: register clock consumer ID
  651. * @regs: Pointer to memory-mapped IO region
  652. * @config: Configuration for register map
  653. *
  654. * The return value will be an ERR_PTR() on error or a valid pointer
  655. * to a struct regmap. The regmap will be automatically freed by the
  656. * device management code.
  657. */
  658. #define devm_regmap_init_mmio_clk(dev, clk_id, regs, config) \
  659. __regmap_lockdep_wrapper(__devm_regmap_init_mmio_clk, #config, \
  660. dev, clk_id, regs, config)
  661. /**
  662. * devm_regmap_init_mmio(): Initialise managed register map
  663. *
  664. * @dev: Device that will be interacted with
  665. * @regs: Pointer to memory-mapped IO region
  666. * @config: Configuration for register map
  667. *
  668. * The return value will be an ERR_PTR() on error or a valid pointer
  669. * to a struct regmap. The regmap will be automatically freed by the
  670. * device management code.
  671. */
  672. #define devm_regmap_init_mmio(dev, regs, config) \
  673. devm_regmap_init_mmio_clk(dev, NULL, regs, config)
  674. /**
  675. * devm_regmap_init_ac97(): Initialise AC'97 register map
  676. *
  677. * @ac97: Device that will be interacted with
  678. * @config: Configuration for register map
  679. *
  680. * The return value will be an ERR_PTR() on error or a valid pointer
  681. * to a struct regmap. The regmap will be automatically freed by the
  682. * device management code.
  683. */
  684. #define devm_regmap_init_ac97(ac97, config) \
  685. __regmap_lockdep_wrapper(__devm_regmap_init_ac97, #config, \
  686. ac97, config)
  687. void regmap_exit(struct regmap *map);
  688. int regmap_reinit_cache(struct regmap *map,
  689. const struct regmap_config *config);
  690. struct regmap *dev_get_regmap(struct device *dev, const char *name);
  691. struct device *regmap_get_device(struct regmap *map);
  692. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
  693. int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val);
  694. int regmap_raw_write(struct regmap *map, unsigned int reg,
  695. const void *val, size_t val_len);
  696. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  697. size_t val_count);
  698. int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
  699. int num_regs);
  700. int regmap_multi_reg_write_bypassed(struct regmap *map,
  701. const struct reg_sequence *regs,
  702. int num_regs);
  703. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  704. const void *val, size_t val_len);
  705. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
  706. int regmap_raw_read(struct regmap *map, unsigned int reg,
  707. void *val, size_t val_len);
  708. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  709. size_t val_count);
  710. int regmap_update_bits_base(struct regmap *map, unsigned int reg,
  711. unsigned int mask, unsigned int val,
  712. bool *change, bool async, bool force);
  713. int regmap_get_val_bytes(struct regmap *map);
  714. int regmap_get_max_register(struct regmap *map);
  715. int regmap_get_reg_stride(struct regmap *map);
  716. int regmap_async_complete(struct regmap *map);
  717. bool regmap_can_raw_write(struct regmap *map);
  718. size_t regmap_get_raw_read_max(struct regmap *map);
  719. size_t regmap_get_raw_write_max(struct regmap *map);
  720. int regcache_sync(struct regmap *map);
  721. int regcache_sync_region(struct regmap *map, unsigned int min,
  722. unsigned int max);
  723. int regcache_drop_region(struct regmap *map, unsigned int min,
  724. unsigned int max);
  725. void regcache_cache_only(struct regmap *map, bool enable);
  726. void regcache_cache_bypass(struct regmap *map, bool enable);
  727. void regcache_mark_dirty(struct regmap *map);
  728. bool regmap_check_range_table(struct regmap *map, unsigned int reg,
  729. const struct regmap_access_table *table);
  730. int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
  731. int num_regs);
  732. int regmap_parse_val(struct regmap *map, const void *buf,
  733. unsigned int *val);
  734. static inline bool regmap_reg_in_range(unsigned int reg,
  735. const struct regmap_range *range)
  736. {
  737. return reg >= range->range_min && reg <= range->range_max;
  738. }
  739. bool regmap_reg_in_ranges(unsigned int reg,
  740. const struct regmap_range *ranges,
  741. unsigned int nranges);
  742. /**
  743. * Description of an register field
  744. *
  745. * @reg: Offset of the register within the regmap bank
  746. * @lsb: lsb of the register field.
  747. * @msb: msb of the register field.
  748. * @id_size: port size if it has some ports
  749. * @id_offset: address offset for each ports
  750. */
  751. struct reg_field {
  752. unsigned int reg;
  753. unsigned int lsb;
  754. unsigned int msb;
  755. unsigned int id_size;
  756. unsigned int id_offset;
  757. };
  758. #define REG_FIELD(_reg, _lsb, _msb) { \
  759. .reg = _reg, \
  760. .lsb = _lsb, \
  761. .msb = _msb, \
  762. }
  763. struct regmap_field *regmap_field_alloc(struct regmap *regmap,
  764. struct reg_field reg_field);
  765. void regmap_field_free(struct regmap_field *field);
  766. struct regmap_field *devm_regmap_field_alloc(struct device *dev,
  767. struct regmap *regmap, struct reg_field reg_field);
  768. void devm_regmap_field_free(struct device *dev, struct regmap_field *field);
  769. int regmap_field_read(struct regmap_field *field, unsigned int *val);
  770. int regmap_field_update_bits_base(struct regmap_field *field,
  771. unsigned int mask, unsigned int val,
  772. bool *change, bool async, bool force);
  773. int regmap_fields_read(struct regmap_field *field, unsigned int id,
  774. unsigned int *val);
  775. int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
  776. unsigned int mask, unsigned int val,
  777. bool *change, bool async, bool force);
  778. /**
  779. * Description of an IRQ for the generic regmap irq_chip.
  780. *
  781. * @reg_offset: Offset of the status/mask register within the bank
  782. * @mask: Mask used to flag/control the register.
  783. * @type_reg_offset: Offset register for the irq type setting.
  784. * @type_rising_mask: Mask bit to configure RISING type irq.
  785. * @type_falling_mask: Mask bit to configure FALLING type irq.
  786. */
  787. struct regmap_irq {
  788. unsigned int reg_offset;
  789. unsigned int mask;
  790. unsigned int type_reg_offset;
  791. unsigned int type_rising_mask;
  792. unsigned int type_falling_mask;
  793. };
  794. #define REGMAP_IRQ_REG(_irq, _off, _mask) \
  795. [_irq] = { .reg_offset = (_off), .mask = (_mask) }
  796. /**
  797. * Description of a generic regmap irq_chip. This is not intended to
  798. * handle every possible interrupt controller, but it should handle a
  799. * substantial proportion of those that are found in the wild.
  800. *
  801. * @name: Descriptive name for IRQ controller.
  802. *
  803. * @status_base: Base status register address.
  804. * @mask_base: Base mask register address.
  805. * @unmask_base: Base unmask register address. for chips who have
  806. * separate mask and unmask registers
  807. * @ack_base: Base ack address. If zero then the chip is clear on read.
  808. * Using zero value is possible with @use_ack bit.
  809. * @wake_base: Base address for wake enables. If zero unsupported.
  810. * @type_base: Base address for irq type. If zero unsupported.
  811. * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
  812. * @init_ack_masked: Ack all masked interrupts once during initalization.
  813. * @mask_invert: Inverted mask register: cleared bits are masked out.
  814. * @use_ack: Use @ack register even if it is zero.
  815. * @ack_invert: Inverted ack register: cleared bits for ack.
  816. * @wake_invert: Inverted wake register: cleared bits are wake enabled.
  817. * @type_invert: Invert the type flags.
  818. * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
  819. *
  820. * @num_regs: Number of registers in each control bank.
  821. * @irqs: Descriptors for individual IRQs. Interrupt numbers are
  822. * assigned based on the index in the array of the interrupt.
  823. * @num_irqs: Number of descriptors.
  824. * @num_type_reg: Number of type registers.
  825. * @type_reg_stride: Stride to use for chips where type registers are not
  826. * contiguous.
  827. * @handle_pre_irq: Driver specific callback to handle interrupt from device
  828. * before regmap_irq_handler process the interrupts.
  829. * @handle_post_irq: Driver specific callback to handle interrupt from device
  830. * after handling the interrupts in regmap_irq_handler().
  831. * @irq_drv_data: Driver specific IRQ data which is passed as parameter when
  832. * driver specific pre/post interrupt handler is called.
  833. */
  834. struct regmap_irq_chip {
  835. const char *name;
  836. unsigned int status_base;
  837. unsigned int mask_base;
  838. unsigned int unmask_base;
  839. unsigned int ack_base;
  840. unsigned int wake_base;
  841. unsigned int type_base;
  842. unsigned int irq_reg_stride;
  843. bool init_ack_masked:1;
  844. bool mask_invert:1;
  845. bool use_ack:1;
  846. bool ack_invert:1;
  847. bool wake_invert:1;
  848. bool runtime_pm:1;
  849. bool type_invert:1;
  850. int num_regs;
  851. const struct regmap_irq *irqs;
  852. int num_irqs;
  853. int num_type_reg;
  854. unsigned int type_reg_stride;
  855. int (*handle_pre_irq)(void *irq_drv_data);
  856. int (*handle_post_irq)(void *irq_drv_data);
  857. void *irq_drv_data;
  858. };
  859. struct regmap_irq_chip_data;
  860. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  861. int irq_base, const struct regmap_irq_chip *chip,
  862. struct regmap_irq_chip_data **data);
  863. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
  864. int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
  865. int irq_flags, int irq_base,
  866. const struct regmap_irq_chip *chip,
  867. struct regmap_irq_chip_data **data);
  868. void devm_regmap_del_irq_chip(struct device *dev, int irq,
  869. struct regmap_irq_chip_data *data);
  870. int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
  871. int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
  872. struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
  873. #else
  874. /*
  875. * These stubs should only ever be called by generic code which has
  876. * regmap based facilities, if they ever get called at runtime
  877. * something is going wrong and something probably needs to select
  878. * REGMAP.
  879. */
  880. static inline int regmap_write(struct regmap *map, unsigned int reg,
  881. unsigned int val)
  882. {
  883. WARN_ONCE(1, "regmap API is disabled");
  884. return -EINVAL;
  885. }
  886. static inline int regmap_write_async(struct regmap *map, unsigned int reg,
  887. unsigned int val)
  888. {
  889. WARN_ONCE(1, "regmap API is disabled");
  890. return -EINVAL;
  891. }
  892. static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
  893. const void *val, size_t val_len)
  894. {
  895. WARN_ONCE(1, "regmap API is disabled");
  896. return -EINVAL;
  897. }
  898. static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  899. const void *val, size_t val_len)
  900. {
  901. WARN_ONCE(1, "regmap API is disabled");
  902. return -EINVAL;
  903. }
  904. static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
  905. const void *val, size_t val_count)
  906. {
  907. WARN_ONCE(1, "regmap API is disabled");
  908. return -EINVAL;
  909. }
  910. static inline int regmap_read(struct regmap *map, unsigned int reg,
  911. unsigned int *val)
  912. {
  913. WARN_ONCE(1, "regmap API is disabled");
  914. return -EINVAL;
  915. }
  916. static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
  917. void *val, size_t val_len)
  918. {
  919. WARN_ONCE(1, "regmap API is disabled");
  920. return -EINVAL;
  921. }
  922. static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
  923. void *val, size_t val_count)
  924. {
  925. WARN_ONCE(1, "regmap API is disabled");
  926. return -EINVAL;
  927. }
  928. static inline int regmap_update_bits_base(struct regmap *map, unsigned int reg,
  929. unsigned int mask, unsigned int val,
  930. bool *change, bool async, bool force)
  931. {
  932. WARN_ONCE(1, "regmap API is disabled");
  933. return -EINVAL;
  934. }
  935. static inline int regmap_field_update_bits_base(struct regmap_field *field,
  936. unsigned int mask, unsigned int val,
  937. bool *change, bool async, bool force)
  938. {
  939. WARN_ONCE(1, "regmap API is disabled");
  940. return -EINVAL;
  941. }
  942. static inline int regmap_fields_update_bits_base(struct regmap_field *field,
  943. unsigned int id,
  944. unsigned int mask, unsigned int val,
  945. bool *change, bool async, bool force)
  946. {
  947. WARN_ONCE(1, "regmap API is disabled");
  948. return -EINVAL;
  949. }
  950. static inline int regmap_get_val_bytes(struct regmap *map)
  951. {
  952. WARN_ONCE(1, "regmap API is disabled");
  953. return -EINVAL;
  954. }
  955. static inline int regmap_get_max_register(struct regmap *map)
  956. {
  957. WARN_ONCE(1, "regmap API is disabled");
  958. return -EINVAL;
  959. }
  960. static inline int regmap_get_reg_stride(struct regmap *map)
  961. {
  962. WARN_ONCE(1, "regmap API is disabled");
  963. return -EINVAL;
  964. }
  965. static inline int regcache_sync(struct regmap *map)
  966. {
  967. WARN_ONCE(1, "regmap API is disabled");
  968. return -EINVAL;
  969. }
  970. static inline int regcache_sync_region(struct regmap *map, unsigned int min,
  971. unsigned int max)
  972. {
  973. WARN_ONCE(1, "regmap API is disabled");
  974. return -EINVAL;
  975. }
  976. static inline int regcache_drop_region(struct regmap *map, unsigned int min,
  977. unsigned int max)
  978. {
  979. WARN_ONCE(1, "regmap API is disabled");
  980. return -EINVAL;
  981. }
  982. static inline void regcache_cache_only(struct regmap *map, bool enable)
  983. {
  984. WARN_ONCE(1, "regmap API is disabled");
  985. }
  986. static inline void regcache_cache_bypass(struct regmap *map, bool enable)
  987. {
  988. WARN_ONCE(1, "regmap API is disabled");
  989. }
  990. static inline void regcache_mark_dirty(struct regmap *map)
  991. {
  992. WARN_ONCE(1, "regmap API is disabled");
  993. }
  994. static inline void regmap_async_complete(struct regmap *map)
  995. {
  996. WARN_ONCE(1, "regmap API is disabled");
  997. }
  998. static inline int regmap_register_patch(struct regmap *map,
  999. const struct reg_sequence *regs,
  1000. int num_regs)
  1001. {
  1002. WARN_ONCE(1, "regmap API is disabled");
  1003. return -EINVAL;
  1004. }
  1005. static inline int regmap_parse_val(struct regmap *map, const void *buf,
  1006. unsigned int *val)
  1007. {
  1008. WARN_ONCE(1, "regmap API is disabled");
  1009. return -EINVAL;
  1010. }
  1011. static inline struct regmap *dev_get_regmap(struct device *dev,
  1012. const char *name)
  1013. {
  1014. return NULL;
  1015. }
  1016. static inline struct device *regmap_get_device(struct regmap *map)
  1017. {
  1018. WARN_ONCE(1, "regmap API is disabled");
  1019. return NULL;
  1020. }
  1021. #endif
  1022. #endif