regmap.h 37 KB

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