regmap.h 36 KB

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