regmap.h 35 KB

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