regmap.h 36 KB

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