regmap.h 36 KB

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