regmap.h 37 KB

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