1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef __LINUX_REGMAP_H
3 #define __LINUX_REGMAP_H
4
5 /*
6 * Register map access API
7 *
8 * Copyright 2011 Wolfson Microelectronics plc
9 *
10 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
11 */
12
13 #include <linux/list.h>
14 #include <linux/rbtree.h>
15 #include <linux/ktime.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/bug.h>
19 #include <linux/lockdep.h>
20 #include <linux/iopoll.h>
21 #include <linux/fwnode.h>
22 #include <linux/android_kabi.h>
23
24 struct module;
25 struct clk;
26 struct device;
27 struct device_node;
28 struct i2c_client;
29 struct i3c_device;
30 struct irq_domain;
31 struct slim_device;
32 struct spi_device;
33 struct spmi_device;
34 struct regmap;
35 struct regmap_range_cfg;
36 struct regmap_field;
37 struct snd_ac97;
38 struct sdw_slave;
39
40 /* An enum of all the supported cache types */
41 enum regcache_type {
42 REGCACHE_NONE,
43 REGCACHE_RBTREE,
44 REGCACHE_COMPRESSED,
45 REGCACHE_FLAT,
46 };
47
48 /**
49 * struct reg_default - Default value for a register.
50 *
51 * @reg: Register address.
52 * @def: Register default value.
53 *
54 * We use an array of structs rather than a simple array as many modern devices
55 * have very sparse register maps.
56 */
57 struct reg_default {
58 unsigned int reg;
59 unsigned int def;
60 };
61
62 /**
63 * struct reg_sequence - An individual write from a sequence of writes.
64 *
65 * @reg: Register address.
66 * @def: Register value.
67 * @delay_us: Delay to be applied after the register write in microseconds
68 *
69 * Register/value pairs for sequences of writes with an optional delay in
70 * microseconds to be applied after each write.
71 */
72 struct reg_sequence {
73 unsigned int reg;
74 unsigned int def;
75 unsigned int delay_us;
76 };
77
78 #define REG_SEQ(_reg, _def, _delay_us) { \
79 .reg = _reg, \
80 .def = _def, \
81 .delay_us = _delay_us, \
82 }
83 #define REG_SEQ0(_reg, _def) REG_SEQ(_reg, _def, 0)
84
85 /**
86 * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
87 *
88 * @map: Regmap to read from
89 * @addr: Address to poll
90 * @val: Unsigned integer variable to read the value into
91 * @cond: Break condition (usually involving @val)
92 * @sleep_us: Maximum time to sleep between reads in us (0
93 * tight-loops). Should be less than ~20ms since usleep_range
94 * is used (see Documentation/timers/timers-howto.rst).
95 * @timeout_us: Timeout in us, 0 means never timeout
96 *
97 * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
98 * error return value in case of a error read. In the two former cases,
99 * the last read value at @addr is stored in @val. Must not be called
100 * from atomic context if sleep_us or timeout_us are used.
101 *
102 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
103 */
104 #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_us) \
105 ({ \
106 int __ret, __tmp; \
107 __tmp = read_poll_timeout(regmap_read, __ret, __ret || (cond), \
108 sleep_us, timeout_us, false, (map), (addr), &(val)); \
109 __ret ?: __tmp; \
110 })
111
112 /**
113 * regmap_read_poll_timeout_atomic - Poll until a condition is met or a timeout occurs
114 *
115 * @map: Regmap to read from
116 * @addr: Address to poll
117 * @val: Unsigned integer variable to read the value into
118 * @cond: Break condition (usually involving @val)
119 * @delay_us: Time to udelay between reads in us (0 tight-loops).
120 * Should be less than ~10us since udelay is used
121 * (see Documentation/timers/timers-howto.rst).
122 * @timeout_us: Timeout in us, 0 means never timeout
123 *
124 * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
125 * error return value in case of a error read. In the two former cases,
126 * the last read value at @addr is stored in @val.
127 *
128 * This is modelled after the readx_poll_timeout_atomic macros in linux/iopoll.h.
129 *
130 * Note: In general regmap cannot be used in atomic context. If you want to use
131 * this macro then first setup your regmap for atomic use (flat or no cache
132 * and MMIO regmap).
133 */
134 #define regmap_read_poll_timeout_atomic(map, addr, val, cond, delay_us, timeout_us) \
135 ({ \
136 u64 __timeout_us = (timeout_us); \
137 unsigned long __delay_us = (delay_us); \
138 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
139 int __ret; \
140 for (;;) { \
141 __ret = regmap_read((map), (addr), &(val)); \
142 if (__ret) \
143 break; \
144 if (cond) \
145 break; \
146 if ((__timeout_us) && \
147 ktime_compare(ktime_get(), __timeout) > 0) { \
148 __ret = regmap_read((map), (addr), &(val)); \
149 break; \
150 } \
151 if (__delay_us) \
152 udelay(__delay_us); \
153 } \
154 __ret ?: ((cond) ? 0 : -ETIMEDOUT); \
155 })
156
157 /**
158 * regmap_field_read_poll_timeout - Poll until a condition is met or timeout
159 *
160 * @field: Regmap field to read from
161 * @val: Unsigned integer variable to read the value into
162 * @cond: Break condition (usually involving @val)
163 * @sleep_us: Maximum time to sleep between reads in us (0
164 * tight-loops). Should be less than ~20ms since usleep_range
165 * is used (see Documentation/timers/timers-howto.rst).
166 * @timeout_us: Timeout in us, 0 means never timeout
167 *
168 * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_field_read
169 * error return value in case of a error read. In the two former cases,
170 * the last read value at @addr is stored in @val. Must not be called
171 * from atomic context if sleep_us or timeout_us are used.
172 *
173 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
174 */
175 #define regmap_field_read_poll_timeout(field, val, cond, sleep_us, timeout_us) \
176 ({ \
177 int __ret, __tmp; \
178 __tmp = read_poll_timeout(regmap_field_read, __ret, __ret || (cond), \
179 sleep_us, timeout_us, false, (field), &(val)); \
180 __ret ?: __tmp; \
181 })
182
183 #ifdef CONFIG_REGMAP
184
185 enum regmap_endian {
186 /* Unspecified -> 0 -> Backwards compatible default */
187 REGMAP_ENDIAN_DEFAULT = 0,
188 REGMAP_ENDIAN_BIG,
189 REGMAP_ENDIAN_LITTLE,
190 REGMAP_ENDIAN_NATIVE,
191 };
192
193 /**
194 * struct regmap_range - A register range, used for access related checks
195 * (readable/writeable/volatile/precious checks)
196 *
197 * @range_min: address of first register
198 * @range_max: address of last register
199 */
200 struct regmap_range {
201 unsigned int range_min;
202 unsigned int range_max;
203 };
204
205 #define regmap_reg_range(low, high) { .range_min = low, .range_max = high, }
206
207 /**
208 * struct regmap_access_table - A table of register ranges for access checks
209 *
210 * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
211 * @n_yes_ranges: size of the above array
212 * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
213 * @n_no_ranges: size of the above array
214 *
215 * A table of ranges including some yes ranges and some no ranges.
216 * If a register belongs to a no_range, the corresponding check function
217 * will return false. If a register belongs to a yes range, the corresponding
218 * check function will return true. "no_ranges" are searched first.
219 */
220 struct regmap_access_table {
221 const struct regmap_range *yes_ranges;
222 unsigned int n_yes_ranges;
223 const struct regmap_range *no_ranges;
224 unsigned int n_no_ranges;
225 };
226
227 typedef void (*regmap_lock)(void *);
228 typedef void (*regmap_unlock)(void *);
229
230 /**
231 * struct regmap_config - Configuration for the register map of a device.
232 *
233 * @name: Optional name of the regmap. Useful when a device has multiple
234 * register regions.
235 *
236 * @reg_bits: Number of bits in a register address, mandatory.
237 * @reg_stride: The register address stride. Valid register addresses are a
238 * multiple of this value. If set to 0, a value of 1 will be
239 * used.
240 * @pad_bits: Number of bits of padding between register and value.
241 * @val_bits: Number of bits in a register value, mandatory.
242 *
243 * @writeable_reg: Optional callback returning true if the register
244 * can be written to. If this field is NULL but wr_table
245 * (see below) is not, the check is performed on such table
246 * (a register is writeable if it belongs to one of the ranges
247 * specified by wr_table).
248 * @readable_reg: Optional callback returning true if the register
249 * can be read from. If this field is NULL but rd_table
250 * (see below) is not, the check is performed on such table
251 * (a register is readable if it belongs to one of the ranges
252 * specified by rd_table).
253 * @volatile_reg: Optional callback returning true if the register
254 * value can't be cached. If this field is NULL but
255 * volatile_table (see below) is not, the check is performed on
256 * such table (a register is volatile if it belongs to one of
257 * the ranges specified by volatile_table).
258 * @precious_reg: Optional callback returning true if the register
259 * should not be read outside of a call from the driver
260 * (e.g., a clear on read interrupt status register). If this
261 * field is NULL but precious_table (see below) is not, the
262 * check is performed on such table (a register is precious if
263 * it belongs to one of the ranges specified by precious_table).
264 * @writeable_noinc_reg: Optional callback returning true if the register
265 * supports multiple write operations without incrementing
266 * the register number. If this field is NULL but
267 * wr_noinc_table (see below) is not, the check is
268 * performed on such table (a register is no increment
269 * writeable if it belongs to one of the ranges specified
270 * by wr_noinc_table).
271 * @readable_noinc_reg: Optional callback returning true if the register
272 * supports multiple read operations without incrementing
273 * the register number. If this field is NULL but
274 * rd_noinc_table (see below) is not, the check is
275 * performed on such table (a register is no increment
276 * readable if it belongs to one of the ranges specified
277 * by rd_noinc_table).
278 * @disable_locking: This regmap is either protected by external means or
279 * is guaranteed not to be accessed from multiple threads.
280 * Don't use any locking mechanisms.
281 * @lock: Optional lock callback (overrides regmap's default lock
282 * function, based on spinlock or mutex).
283 * @unlock: As above for unlocking.
284 * @lock_arg: this field is passed as the only argument of lock/unlock
285 * functions (ignored in case regular lock/unlock functions
286 * are not overridden).
287 * @reg_read: Optional callback that if filled will be used to perform
288 * all the reads from the registers. Should only be provided for
289 * devices whose read operation cannot be represented as a simple
290 * read operation on a bus such as SPI, I2C, etc. Most of the
291 * devices do not need this.
292 * @reg_write: Same as above for writing.
293 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
294 * to perform locking. This field is ignored if custom lock/unlock
295 * functions are used (see fields lock/unlock of struct regmap_config).
296 * This field is a duplicate of a similar file in
297 * 'struct regmap_bus' and serves exact same purpose.
298 * Use it only for "no-bus" cases.
299 * @max_register: Optional, specifies the maximum valid register address.
300 * @wr_table: Optional, points to a struct regmap_access_table specifying
301 * valid ranges for write access.
302 * @rd_table: As above, for read access.
303 * @volatile_table: As above, for volatile registers.
304 * @precious_table: As above, for precious registers.
305 * @wr_noinc_table: As above, for no increment writeable registers.
306 * @rd_noinc_table: As above, for no increment readable registers.
307 * @reg_defaults: Power on reset values for registers (for use with
308 * register cache support).
309 * @num_reg_defaults: Number of elements in reg_defaults.
310 *
311 * @read_flag_mask: Mask to be set in the top bytes of the register when doing
312 * a read.
313 * @write_flag_mask: Mask to be set in the top bytes of the register when doing
314 * a write. If both read_flag_mask and write_flag_mask are
315 * empty and zero_flag_mask is not set the regmap_bus default
316 * masks are used.
317 * @zero_flag_mask: If set, read_flag_mask and write_flag_mask are used even
318 * if they are both empty.
319 * @use_single_read: If set, converts the bulk read operation into a series of
320 * single read operations. This is useful for a device that
321 * does not support bulk read.
322 * @use_single_write: If set, converts the bulk write operation into a series of
323 * single write operations. This is useful for a device that
324 * does not support bulk write.
325 * @can_multi_write: If set, the device supports the multi write mode of bulk
326 * write operations, if clear multi write requests will be
327 * split into individual write operations
328 *
329 * @cache_type: The actual cache type.
330 * @reg_defaults_raw: Power on reset values for registers (for use with
331 * register cache support).
332 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
333 * @reg_format_endian: Endianness for formatted register addresses. If this is
334 * DEFAULT, the @reg_format_endian_default value from the
335 * regmap bus is used.
336 * @val_format_endian: Endianness for formatted register values. If this is
337 * DEFAULT, the @reg_format_endian_default value from the
338 * regmap bus is used.
339 *
340 * @ranges: Array of configuration entries for virtual address ranges.
341 * @num_ranges: Number of range configuration entries.
342 * @use_hwlock: Indicate if a hardware spinlock should be used.
343 * @hwlock_id: Specify the hardware spinlock id.
344 * @hwlock_mode: The hardware spinlock mode, should be HWLOCK_IRQSTATE,
345 * HWLOCK_IRQ or 0.
346 * @can_sleep: Optional, specifies whether regmap operations can sleep.
347 */
348 struct regmap_config {
349 const char *name;
350
351 int reg_bits;
352 int reg_stride;
353 int pad_bits;
354 int val_bits;
355
356 bool (*writeable_reg)(struct device *dev, unsigned int reg);
357 bool (*readable_reg)(struct device *dev, unsigned int reg);
358 bool (*volatile_reg)(struct device *dev, unsigned int reg);
359 bool (*precious_reg)(struct device *dev, unsigned int reg);
360 bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg);
361 bool (*readable_noinc_reg)(struct device *dev, unsigned int reg);
362
363 bool disable_locking;
364 regmap_lock lock;
365 regmap_unlock unlock;
366 void *lock_arg;
367
368 int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
369 int (*reg_write)(void *context, unsigned int reg, unsigned int val);
370
371 bool fast_io;
372
373 unsigned int max_register;
374 const struct regmap_access_table *wr_table;
375 const struct regmap_access_table *rd_table;
376 const struct regmap_access_table *volatile_table;
377 const struct regmap_access_table *precious_table;
378 const struct regmap_access_table *wr_noinc_table;
379 const struct regmap_access_table *rd_noinc_table;
380 const struct reg_default *reg_defaults;
381 unsigned int num_reg_defaults;
382 enum regcache_type cache_type;
383 const void *reg_defaults_raw;
384 unsigned int num_reg_defaults_raw;
385
386 unsigned long read_flag_mask;
387 unsigned long write_flag_mask;
388 bool zero_flag_mask;
389
390 bool use_single_read;
391 bool use_single_write;
392 bool can_multi_write;
393
394 enum regmap_endian reg_format_endian;
395 enum regmap_endian val_format_endian;
396
397 const struct regmap_range_cfg *ranges;
398 unsigned int num_ranges;
399
400 bool use_hwlock;
401 unsigned int hwlock_id;
402 unsigned int hwlock_mode;
403
404 bool can_sleep;
405
406 ANDROID_KABI_RESERVE(1);
407 };
408
409 /**
410 * struct regmap_range_cfg - Configuration for indirectly accessed or paged
411 * registers.
412 *
413 * @name: Descriptive name for diagnostics
414 *
415 * @range_min: Address of the lowest register address in virtual range.
416 * @range_max: Address of the highest register in virtual range.
417 *
418 * @selector_reg: Register with selector field.
419 * @selector_mask: Bit mask for selector value.
420 * @selector_shift: Bit shift for selector value.
421 *
422 * @window_start: Address of first (lowest) register in data window.
423 * @window_len: Number of registers in data window.
424 *
425 * Registers, mapped to this virtual range, are accessed in two steps:
426 * 1. page selector register update;
427 * 2. access through data window registers.
428 */
429 struct regmap_range_cfg {
430 const char *name;
431
432 /* Registers of virtual address range */
433 unsigned int range_min;
434 unsigned int range_max;
435
436 /* Page selector for indirect addressing */
437 unsigned int selector_reg;
438 unsigned int selector_mask;
439 int selector_shift;
440
441 /* Data window (per each page) */
442 unsigned int window_start;
443 unsigned int window_len;
444
445 ANDROID_KABI_RESERVE(1);
446 };
447
448 struct regmap_async;
449
450 typedef int (*regmap_hw_write)(void *context, const void *data,
451 size_t count);
452 typedef int (*regmap_hw_gather_write)(void *context,
453 const void *reg, size_t reg_len,
454 const void *val, size_t val_len);
455 typedef int (*regmap_hw_async_write)(void *context,
456 const void *reg, size_t reg_len,
457 const void *val, size_t val_len,
458 struct regmap_async *async);
459 typedef int (*regmap_hw_read)(void *context,
460 const void *reg_buf, size_t reg_size,
461 void *val_buf, size_t val_size);
462 typedef int (*regmap_hw_reg_read)(void *context, unsigned int reg,
463 unsigned int *val);
464 typedef int (*regmap_hw_reg_write)(void *context, unsigned int reg,
465 unsigned int val);
466 typedef int (*regmap_hw_reg_update_bits)(void *context, unsigned int reg,
467 unsigned int mask, unsigned int val);
468 typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
469 typedef void (*regmap_hw_free_context)(void *context);
470
471 /**
472 * struct regmap_bus - Description of a hardware bus for the register map
473 * infrastructure.
474 *
475 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
476 * to perform locking. This field is ignored if custom lock/unlock
477 * functions are used (see fields lock/unlock of
478 * struct regmap_config).
479 * @write: Write operation.
480 * @gather_write: Write operation with split register/value, return -ENOTSUPP
481 * if not implemented on a given device.
482 * @async_write: Write operation which completes asynchronously, optional and
483 * must serialise with respect to non-async I/O.
484 * @reg_write: Write a single register value to the given register address. This
485 * write operation has to complete when returning from the function.
486 * @reg_update_bits: Update bits operation to be used against volatile
487 * registers, intended for devices supporting some mechanism
488 * for setting clearing bits without having to
489 * read/modify/write.
490 * @read: Read operation. Data is returned in the buffer used to transmit
491 * data.
492 * @reg_read: Read a single register value from a given register address.
493 * @free_context: Free context.
494 * @async_alloc: Allocate a regmap_async() structure.
495 * @read_flag_mask: Mask to be set in the top byte of the register when doing
496 * a read.
497 * @reg_format_endian_default: Default endianness for formatted register
498 * addresses. Used when the regmap_config specifies DEFAULT. If this is
499 * DEFAULT, BIG is assumed.
500 * @val_format_endian_default: Default endianness for formatted register
501 * values. Used when the regmap_config specifies DEFAULT. If this is
502 * DEFAULT, BIG is assumed.
503 * @max_raw_read: Max raw read size that can be used on the bus.
504 * @max_raw_write: Max raw write size that can be used on the bus.
505 */
506 struct regmap_bus {
507 bool fast_io;
508 regmap_hw_write write;
509 regmap_hw_gather_write gather_write;
510 regmap_hw_async_write async_write;
511 regmap_hw_reg_write reg_write;
512 regmap_hw_reg_update_bits reg_update_bits;
513 regmap_hw_read read;
514 regmap_hw_reg_read reg_read;
515 regmap_hw_free_context free_context;
516 regmap_hw_async_alloc async_alloc;
517 u8 read_flag_mask;
518 enum regmap_endian reg_format_endian_default;
519 enum regmap_endian val_format_endian_default;
520 size_t max_raw_read;
521 size_t max_raw_write;
522
523 ANDROID_KABI_RESERVE(1);
524 };
525
526 /*
527 * __regmap_init functions.
528 *
529 * These functions take a lock key and name parameter, and should not be called
530 * directly. Instead, use the regmap_init macros that generate a key and name
531 * for each call.
532 */
533 struct regmap *__regmap_init(struct device *dev,
534 const struct regmap_bus *bus,
535 void *bus_context,
536 const struct regmap_config *config,
537 struct lock_class_key *lock_key,
538 const char *lock_name);
539 struct regmap *__regmap_init_i2c(struct i2c_client *i2c,
540 const struct regmap_config *config,
541 struct lock_class_key *lock_key,
542 const char *lock_name);
543 struct regmap *__regmap_init_sccb(struct i2c_client *i2c,
544 const struct regmap_config *config,
545 struct lock_class_key *lock_key,
546 const char *lock_name);
547 struct regmap *__regmap_init_slimbus(struct slim_device *slimbus,
548 const struct regmap_config *config,
549 struct lock_class_key *lock_key,
550 const char *lock_name);
551 struct regmap *__regmap_init_spi(struct spi_device *dev,
552 const struct regmap_config *config,
553 struct lock_class_key *lock_key,
554 const char *lock_name);
555 struct regmap *__regmap_init_spmi_base(struct spmi_device *dev,
556 const struct regmap_config *config,
557 struct lock_class_key *lock_key,
558 const char *lock_name);
559 struct regmap *__regmap_init_spmi_ext(struct spmi_device *dev,
560 const struct regmap_config *config,
561 struct lock_class_key *lock_key,
562 const char *lock_name);
563 struct regmap *__regmap_init_w1(struct device *w1_dev,
564 const struct regmap_config *config,
565 struct lock_class_key *lock_key,
566 const char *lock_name);
567 struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
568 void __iomem *regs,
569 const struct regmap_config *config,
570 struct lock_class_key *lock_key,
571 const char *lock_name);
572 struct regmap *__regmap_init_ac97(struct snd_ac97 *ac97,
573 const struct regmap_config *config,
574 struct lock_class_key *lock_key,
575 const char *lock_name);
576 struct regmap *__regmap_init_sdw(struct sdw_slave *sdw,
577 const struct regmap_config *config,
578 struct lock_class_key *lock_key,
579 const char *lock_name);
580 struct regmap *__regmap_init_spi_avmm(struct spi_device *spi,
581 const struct regmap_config *config,
582 struct lock_class_key *lock_key,
583 const char *lock_name);
584
585 struct regmap *__devm_regmap_init(struct device *dev,
586 const struct regmap_bus *bus,
587 void *bus_context,
588 const struct regmap_config *config,
589 struct lock_class_key *lock_key,
590 const char *lock_name);
591 struct regmap *__devm_regmap_init_i2c(struct i2c_client *i2c,
592 const struct regmap_config *config,
593 struct lock_class_key *lock_key,
594 const char *lock_name);
595 struct regmap *__devm_regmap_init_sccb(struct i2c_client *i2c,
596 const struct regmap_config *config,
597 struct lock_class_key *lock_key,
598 const char *lock_name);
599 struct regmap *__devm_regmap_init_spi(struct spi_device *dev,
600 const struct regmap_config *config,
601 struct lock_class_key *lock_key,
602 const char *lock_name);
603 struct regmap *__devm_regmap_init_spmi_base(struct spmi_device *dev,
604 const struct regmap_config *config,
605 struct lock_class_key *lock_key,
606 const char *lock_name);
607 struct regmap *__devm_regmap_init_spmi_ext(struct spmi_device *dev,
608 const struct regmap_config *config,
609 struct lock_class_key *lock_key,
610 const char *lock_name);
611 struct regmap *__devm_regmap_init_w1(struct device *w1_dev,
612 const struct regmap_config *config,
613 struct lock_class_key *lock_key,
614 const char *lock_name);
615 struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
616 const char *clk_id,
617 void __iomem *regs,
618 const struct regmap_config *config,
619 struct lock_class_key *lock_key,
620 const char *lock_name);
621 struct regmap *__devm_regmap_init_ac97(struct snd_ac97 *ac97,
622 const struct regmap_config *config,
623 struct lock_class_key *lock_key,
624 const char *lock_name);
625 struct regmap *__devm_regmap_init_sdw(struct sdw_slave *sdw,
626 const struct regmap_config *config,
627 struct lock_class_key *lock_key,
628 const char *lock_name);
629 struct regmap *__devm_regmap_init_slimbus(struct slim_device *slimbus,
630 const struct regmap_config *config,
631 struct lock_class_key *lock_key,
632 const char *lock_name);
633 struct regmap *__devm_regmap_init_i3c(struct i3c_device *i3c,
634 const struct regmap_config *config,
635 struct lock_class_key *lock_key,
636 const char *lock_name);
637 struct regmap *__devm_regmap_init_spi_avmm(struct spi_device *spi,
638 const struct regmap_config *config,
639 struct lock_class_key *lock_key,
640 const char *lock_name);
641 /*
642 * Wrapper for regmap_init macros to include a unique lockdep key and name
643 * for each call. No-op if CONFIG_LOCKDEP is not set.
644 *
645 * @fn: Real function to call (in the form __[*_]regmap_init[_*])
646 * @name: Config variable name (#config in the calling macro)
647 **/
648 #ifdef CONFIG_LOCKDEP
649 #define __regmap_lockdep_wrapper(fn, name, ...) \
650 ( \
651 ({ \
652 static struct lock_class_key _key; \
653 fn(__VA_ARGS__, &_key, \
654 KBUILD_BASENAME ":" \
655 __stringify(__LINE__) ":" \
656 "(" name ")->lock"); \
657 }) \
658 )
659 #else
660 #define __regmap_lockdep_wrapper(fn, name, ...) fn(__VA_ARGS__, NULL, NULL)
661 #endif
662
663 /**
664 * regmap_init() - Initialise register map
665 *
666 * @dev: Device that will be interacted with
667 * @bus: Bus-specific callbacks to use with device
668 * @bus_context: Data passed to bus-specific callbacks
669 * @config: Configuration for register map
670 *
671 * The return value will be an ERR_PTR() on error or a valid pointer to
672 * a struct regmap. This function should generally not be called
673 * directly, it should be called by bus-specific init functions.
674 */
675 #define regmap_init(dev, bus, bus_context, config) \
676 __regmap_lockdep_wrapper(__regmap_init, #config, \
677 dev, bus, bus_context, config)
678 int regmap_attach_dev(struct device *dev, struct regmap *map,
679 const struct regmap_config *config);
680
681 /**
682 * regmap_init_i2c() - Initialise register map
683 *
684 * @i2c: Device that will be interacted with
685 * @config: Configuration for register map
686 *
687 * The return value will be an ERR_PTR() on error or a valid pointer to
688 * a struct regmap.
689 */
690 #define regmap_init_i2c(i2c, config) \
691 __regmap_lockdep_wrapper(__regmap_init_i2c, #config, \
692 i2c, config)
693
694 /**
695 * regmap_init_sccb() - Initialise register map
696 *
697 * @i2c: Device that will be interacted with
698 * @config: Configuration for register map
699 *
700 * The return value will be an ERR_PTR() on error or a valid pointer to
701 * a struct regmap.
702 */
703 #define regmap_init_sccb(i2c, config) \
704 __regmap_lockdep_wrapper(__regmap_init_sccb, #config, \
705 i2c, config)
706
707 /**
708 * regmap_init_slimbus() - Initialise register map
709 *
710 * @slimbus: Device that will be interacted with
711 * @config: Configuration for register map
712 *
713 * The return value will be an ERR_PTR() on error or a valid pointer to
714 * a struct regmap.
715 */
716 #define regmap_init_slimbus(slimbus, config) \
717 __regmap_lockdep_wrapper(__regmap_init_slimbus, #config, \
718 slimbus, config)
719
720 /**
721 * regmap_init_spi() - Initialise register map
722 *
723 * @dev: Device that will be interacted with
724 * @config: Configuration for register map
725 *
726 * The return value will be an ERR_PTR() on error or a valid pointer to
727 * a struct regmap.
728 */
729 #define regmap_init_spi(dev, config) \
730 __regmap_lockdep_wrapper(__regmap_init_spi, #config, \
731 dev, config)
732
733 /**
734 * regmap_init_spmi_base() - Create regmap for the Base register space
735 *
736 * @dev: SPMI device that will be interacted with
737 * @config: Configuration for register map
738 *
739 * The return value will be an ERR_PTR() on error or a valid pointer to
740 * a struct regmap.
741 */
742 #define regmap_init_spmi_base(dev, config) \
743 __regmap_lockdep_wrapper(__regmap_init_spmi_base, #config, \
744 dev, config)
745
746 /**
747 * regmap_init_spmi_ext() - Create regmap for Ext register space
748 *
749 * @dev: Device that will be interacted with
750 * @config: Configuration for register map
751 *
752 * The return value will be an ERR_PTR() on error or a valid pointer to
753 * a struct regmap.
754 */
755 #define regmap_init_spmi_ext(dev, config) \
756 __regmap_lockdep_wrapper(__regmap_init_spmi_ext, #config, \
757 dev, config)
758
759 /**
760 * regmap_init_w1() - Initialise register map
761 *
762 * @w1_dev: Device that will be interacted with
763 * @config: Configuration for register map
764 *
765 * The return value will be an ERR_PTR() on error or a valid pointer to
766 * a struct regmap.
767 */
768 #define regmap_init_w1(w1_dev, config) \
769 __regmap_lockdep_wrapper(__regmap_init_w1, #config, \
770 w1_dev, config)
771
772 /**
773 * regmap_init_mmio_clk() - Initialise register map with register clock
774 *
775 * @dev: Device that will be interacted with
776 * @clk_id: register clock consumer ID
777 * @regs: Pointer to memory-mapped IO region
778 * @config: Configuration for register map
779 *
780 * The return value will be an ERR_PTR() on error or a valid pointer to
781 * a struct regmap.
782 */
783 #define regmap_init_mmio_clk(dev, clk_id, regs, config) \
784 __regmap_lockdep_wrapper(__regmap_init_mmio_clk, #config, \
785 dev, clk_id, regs, config)
786
787 /**
788 * regmap_init_mmio() - Initialise register map
789 *
790 * @dev: Device that will be interacted with
791 * @regs: Pointer to memory-mapped IO region
792 * @config: Configuration for register map
793 *
794 * The return value will be an ERR_PTR() on error or a valid pointer to
795 * a struct regmap.
796 */
797 #define regmap_init_mmio(dev, regs, config) \
798 regmap_init_mmio_clk(dev, NULL, regs, config)
799
800 /**
801 * regmap_init_ac97() - Initialise AC'97 register map
802 *
803 * @ac97: Device that will be interacted with
804 * @config: Configuration for register map
805 *
806 * The return value will be an ERR_PTR() on error or a valid pointer to
807 * a struct regmap.
808 */
809 #define regmap_init_ac97(ac97, config) \
810 __regmap_lockdep_wrapper(__regmap_init_ac97, #config, \
811 ac97, config)
812 bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
813
814 /**
815 * regmap_init_sdw() - Initialise register map
816 *
817 * @sdw: Device that will be interacted with
818 * @config: Configuration for register map
819 *
820 * The return value will be an ERR_PTR() on error or a valid pointer to
821 * a struct regmap.
822 */
823 #define regmap_init_sdw(sdw, config) \
824 __regmap_lockdep_wrapper(__regmap_init_sdw, #config, \
825 sdw, config)
826
827 /**
828 * regmap_init_spi_avmm() - Initialize register map for Intel SPI Slave
829 * to AVMM Bus Bridge
830 *
831 * @spi: Device that will be interacted with
832 * @config: Configuration for register map
833 *
834 * The return value will be an ERR_PTR() on error or a valid pointer
835 * to a struct regmap.
836 */
837 #define regmap_init_spi_avmm(spi, config) \
838 __regmap_lockdep_wrapper(__regmap_init_spi_avmm, #config, \
839 spi, config)
840
841 /**
842 * devm_regmap_init() - Initialise managed register map
843 *
844 * @dev: Device that will be interacted with
845 * @bus: Bus-specific callbacks to use with device
846 * @bus_context: Data passed to bus-specific callbacks
847 * @config: Configuration for register map
848 *
849 * The return value will be an ERR_PTR() on error or a valid pointer
850 * to a struct regmap. This function should generally not be called
851 * directly, it should be called by bus-specific init functions. The
852 * map will be automatically freed by the device management code.
853 */
854 #define devm_regmap_init(dev, bus, bus_context, config) \
855 __regmap_lockdep_wrapper(__devm_regmap_init, #config, \
856 dev, bus, bus_context, config)
857
858 /**
859 * devm_regmap_init_i2c() - Initialise managed register map
860 *
861 * @i2c: Device that will be interacted with
862 * @config: Configuration for register map
863 *
864 * The return value will be an ERR_PTR() on error or a valid pointer
865 * to a struct regmap. The regmap will be automatically freed by the
866 * device management code.
867 */
868 #define devm_regmap_init_i2c(i2c, config) \
869 __regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config, \
870 i2c, config)
871
872 /**
873 * devm_regmap_init_sccb() - Initialise managed register map
874 *
875 * @i2c: Device that will be interacted with
876 * @config: Configuration for register map
877 *
878 * The return value will be an ERR_PTR() on error or a valid pointer
879 * to a struct regmap. The regmap will be automatically freed by the
880 * device management code.
881 */
882 #define devm_regmap_init_sccb(i2c, config) \
883 __regmap_lockdep_wrapper(__devm_regmap_init_sccb, #config, \
884 i2c, config)
885
886 /**
887 * devm_regmap_init_spi() - Initialise register map
888 *
889 * @dev: Device that will be interacted with
890 * @config: Configuration for register map
891 *
892 * The return value will be an ERR_PTR() on error or a valid pointer
893 * to a struct regmap. The map will be automatically freed by the
894 * device management code.
895 */
896 #define devm_regmap_init_spi(dev, config) \
897 __regmap_lockdep_wrapper(__devm_regmap_init_spi, #config, \
898 dev, config)
899
900 /**
901 * devm_regmap_init_spmi_base() - Create managed regmap for Base register space
902 *
903 * @dev: SPMI device that will be interacted with
904 * @config: Configuration for register map
905 *
906 * The return value will be an ERR_PTR() on error or a valid pointer
907 * to a struct regmap. The regmap will be automatically freed by the
908 * device management code.
909 */
910 #define devm_regmap_init_spmi_base(dev, config) \
911 __regmap_lockdep_wrapper(__devm_regmap_init_spmi_base, #config, \
912 dev, config)
913
914 /**
915 * devm_regmap_init_spmi_ext() - Create managed regmap for Ext register space
916 *
917 * @dev: SPMI device that will be interacted with
918 * @config: Configuration for register map
919 *
920 * The return value will be an ERR_PTR() on error or a valid pointer
921 * to a struct regmap. The regmap will be automatically freed by the
922 * device management code.
923 */
924 #define devm_regmap_init_spmi_ext(dev, config) \
925 __regmap_lockdep_wrapper(__devm_regmap_init_spmi_ext, #config, \
926 dev, config)
927
928 /**
929 * devm_regmap_init_w1() - Initialise managed register map
930 *
931 * @w1_dev: Device that will be interacted with
932 * @config: Configuration for register map
933 *
934 * The return value will be an ERR_PTR() on error or a valid pointer
935 * to a struct regmap. The regmap will be automatically freed by the
936 * device management code.
937 */
938 #define devm_regmap_init_w1(w1_dev, config) \
939 __regmap_lockdep_wrapper(__devm_regmap_init_w1, #config, \
940 w1_dev, config)
941 /**
942 * devm_regmap_init_mmio_clk() - Initialise managed register map with clock
943 *
944 * @dev: Device that will be interacted with
945 * @clk_id: register clock consumer ID
946 * @regs: Pointer to memory-mapped IO region
947 * @config: Configuration for register map
948 *
949 * The return value will be an ERR_PTR() on error or a valid pointer
950 * to a struct regmap. The regmap will be automatically freed by the
951 * device management code.
952 */
953 #define devm_regmap_init_mmio_clk(dev, clk_id, regs, config) \
954 __regmap_lockdep_wrapper(__devm_regmap_init_mmio_clk, #config, \
955 dev, clk_id, regs, config)
956
957 /**
958 * devm_regmap_init_mmio() - Initialise managed register map
959 *
960 * @dev: Device that will be interacted with
961 * @regs: Pointer to memory-mapped IO region
962 * @config: Configuration for register map
963 *
964 * The return value will be an ERR_PTR() on error or a valid pointer
965 * to a struct regmap. The regmap will be automatically freed by the
966 * device management code.
967 */
968 #define devm_regmap_init_mmio(dev, regs, config) \
969 devm_regmap_init_mmio_clk(dev, NULL, regs, config)
970
971 /**
972 * devm_regmap_init_ac97() - Initialise AC'97 register map
973 *
974 * @ac97: Device that will be interacted with
975 * @config: Configuration for register map
976 *
977 * The return value will be an ERR_PTR() on error or a valid pointer
978 * to a struct regmap. The regmap will be automatically freed by the
979 * device management code.
980 */
981 #define devm_regmap_init_ac97(ac97, config) \
982 __regmap_lockdep_wrapper(__devm_regmap_init_ac97, #config, \
983 ac97, config)
984
985 /**
986 * devm_regmap_init_sdw() - Initialise managed register map
987 *
988 * @sdw: Device that will be interacted with
989 * @config: Configuration for register map
990 *
991 * The return value will be an ERR_PTR() on error or a valid pointer
992 * to a struct regmap. The regmap will be automatically freed by the
993 * device management code.
994 */
995 #define devm_regmap_init_sdw(sdw, config) \
996 __regmap_lockdep_wrapper(__devm_regmap_init_sdw, #config, \
997 sdw, config)
998
999 /**
1000 * devm_regmap_init_slimbus() - Initialise managed register map
1001 *
1002 * @slimbus: Device that will be interacted with
1003 * @config: Configuration for register map
1004 *
1005 * The return value will be an ERR_PTR() on error or a valid pointer
1006 * to a struct regmap. The regmap will be automatically freed by the
1007 * device management code.
1008 */
1009 #define devm_regmap_init_slimbus(slimbus, config) \
1010 __regmap_lockdep_wrapper(__devm_regmap_init_slimbus, #config, \
1011 slimbus, config)
1012
1013 /**
1014 * devm_regmap_init_i3c() - Initialise managed register map
1015 *
1016 * @i3c: Device that will be interacted with
1017 * @config: Configuration for register map
1018 *
1019 * The return value will be an ERR_PTR() on error or a valid pointer
1020 * to a struct regmap. The regmap will be automatically freed by the
1021 * device management code.
1022 */
1023 #define devm_regmap_init_i3c(i3c, config) \
1024 __regmap_lockdep_wrapper(__devm_regmap_init_i3c, #config, \
1025 i3c, config)
1026
1027 /**
1028 * devm_regmap_init_spi_avmm() - Initialize register map for Intel SPI Slave
1029 * to AVMM Bus Bridge
1030 *
1031 * @spi: Device that will be interacted with
1032 * @config: Configuration for register map
1033 *
1034 * The return value will be an ERR_PTR() on error or a valid pointer
1035 * to a struct regmap. The map will be automatically freed by the
1036 * device management code.
1037 */
1038 #define devm_regmap_init_spi_avmm(spi, config) \
1039 __regmap_lockdep_wrapper(__devm_regmap_init_spi_avmm, #config, \
1040 spi, config)
1041
1042 int regmap_mmio_attach_clk(struct regmap *map, struct clk *clk);
1043 void regmap_mmio_detach_clk(struct regmap *map);
1044 void regmap_exit(struct regmap *map);
1045 int regmap_reinit_cache(struct regmap *map,
1046 const struct regmap_config *config);
1047 struct regmap *dev_get_regmap(struct device *dev, const char *name);
1048 struct device *regmap_get_device(struct regmap *map);
1049 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
1050 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val);
1051 int regmap_raw_write(struct regmap *map, unsigned int reg,
1052 const void *val, size_t val_len);
1053 int regmap_noinc_write(struct regmap *map, unsigned int reg,
1054 const void *val, size_t val_len);
1055 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1056 size_t val_count);
1057 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
1058 int num_regs);
1059 int regmap_multi_reg_write_bypassed(struct regmap *map,
1060 const struct reg_sequence *regs,
1061 int num_regs);
1062 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1063 const void *val, size_t val_len);
1064 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
1065 int regmap_raw_read(struct regmap *map, unsigned int reg,
1066 void *val, size_t val_len);
1067 int regmap_noinc_read(struct regmap *map, unsigned int reg,
1068 void *val, size_t val_len);
1069 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1070 size_t val_count);
1071 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
1072 unsigned int mask, unsigned int val,
1073 bool *change, bool async, bool force);
1074
regmap_update_bits(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val)1075 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
1076 unsigned int mask, unsigned int val)
1077 {
1078 return regmap_update_bits_base(map, reg, mask, val, NULL, false, false);
1079 }
1080
regmap_update_bits_async(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val)1081 static inline int regmap_update_bits_async(struct regmap *map, unsigned int reg,
1082 unsigned int mask, unsigned int val)
1083 {
1084 return regmap_update_bits_base(map, reg, mask, val, NULL, true, false);
1085 }
1086
regmap_update_bits_check(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val,bool * change)1087 static inline int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1088 unsigned int mask, unsigned int val,
1089 bool *change)
1090 {
1091 return regmap_update_bits_base(map, reg, mask, val,
1092 change, false, false);
1093 }
1094
1095 static inline int
regmap_update_bits_check_async(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val,bool * change)1096 regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
1097 unsigned int mask, unsigned int val,
1098 bool *change)
1099 {
1100 return regmap_update_bits_base(map, reg, mask, val,
1101 change, true, false);
1102 }
1103
regmap_write_bits(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val)1104 static inline int regmap_write_bits(struct regmap *map, unsigned int reg,
1105 unsigned int mask, unsigned int val)
1106 {
1107 return regmap_update_bits_base(map, reg, mask, val, NULL, false, true);
1108 }
1109
1110 int regmap_get_val_bytes(struct regmap *map);
1111 int regmap_get_max_register(struct regmap *map);
1112 int regmap_get_reg_stride(struct regmap *map);
1113 int regmap_async_complete(struct regmap *map);
1114 bool regmap_can_raw_write(struct regmap *map);
1115 size_t regmap_get_raw_read_max(struct regmap *map);
1116 size_t regmap_get_raw_write_max(struct regmap *map);
1117
1118 int regcache_sync(struct regmap *map);
1119 int regcache_sync_region(struct regmap *map, unsigned int min,
1120 unsigned int max);
1121 int regcache_drop_region(struct regmap *map, unsigned int min,
1122 unsigned int max);
1123 void regcache_cache_only(struct regmap *map, bool enable);
1124 void regcache_cache_bypass(struct regmap *map, bool enable);
1125 void regcache_mark_dirty(struct regmap *map);
1126
1127 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
1128 const struct regmap_access_table *table);
1129
1130 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
1131 int num_regs);
1132 int regmap_parse_val(struct regmap *map, const void *buf,
1133 unsigned int *val);
1134
regmap_reg_in_range(unsigned int reg,const struct regmap_range * range)1135 static inline bool regmap_reg_in_range(unsigned int reg,
1136 const struct regmap_range *range)
1137 {
1138 return reg >= range->range_min && reg <= range->range_max;
1139 }
1140
1141 bool regmap_reg_in_ranges(unsigned int reg,
1142 const struct regmap_range *ranges,
1143 unsigned int nranges);
1144
regmap_set_bits(struct regmap * map,unsigned int reg,unsigned int bits)1145 static inline int regmap_set_bits(struct regmap *map,
1146 unsigned int reg, unsigned int bits)
1147 {
1148 return regmap_update_bits_base(map, reg, bits, bits,
1149 NULL, false, false);
1150 }
1151
regmap_clear_bits(struct regmap * map,unsigned int reg,unsigned int bits)1152 static inline int regmap_clear_bits(struct regmap *map,
1153 unsigned int reg, unsigned int bits)
1154 {
1155 return regmap_update_bits_base(map, reg, bits, 0, NULL, false, false);
1156 }
1157
1158 int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits);
1159
1160 /**
1161 * struct reg_field - Description of an register field
1162 *
1163 * @reg: Offset of the register within the regmap bank
1164 * @lsb: lsb of the register field.
1165 * @msb: msb of the register field.
1166 * @id_size: port size if it has some ports
1167 * @id_offset: address offset for each ports
1168 */
1169 struct reg_field {
1170 unsigned int reg;
1171 unsigned int lsb;
1172 unsigned int msb;
1173 unsigned int id_size;
1174 unsigned int id_offset;
1175 };
1176
1177 #define REG_FIELD(_reg, _lsb, _msb) { \
1178 .reg = _reg, \
1179 .lsb = _lsb, \
1180 .msb = _msb, \
1181 }
1182
1183 #define REG_FIELD_ID(_reg, _lsb, _msb, _size, _offset) { \
1184 .reg = _reg, \
1185 .lsb = _lsb, \
1186 .msb = _msb, \
1187 .id_size = _size, \
1188 .id_offset = _offset, \
1189 }
1190
1191 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1192 struct reg_field reg_field);
1193 void regmap_field_free(struct regmap_field *field);
1194
1195 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1196 struct regmap *regmap, struct reg_field reg_field);
1197 void devm_regmap_field_free(struct device *dev, struct regmap_field *field);
1198
1199 int regmap_field_bulk_alloc(struct regmap *regmap,
1200 struct regmap_field **rm_field,
1201 struct reg_field *reg_field,
1202 int num_fields);
1203 void regmap_field_bulk_free(struct regmap_field *field);
1204 int devm_regmap_field_bulk_alloc(struct device *dev, struct regmap *regmap,
1205 struct regmap_field **field,
1206 struct reg_field *reg_field, int num_fields);
1207 void devm_regmap_field_bulk_free(struct device *dev,
1208 struct regmap_field *field);
1209
1210 int regmap_field_read(struct regmap_field *field, unsigned int *val);
1211 int regmap_field_update_bits_base(struct regmap_field *field,
1212 unsigned int mask, unsigned int val,
1213 bool *change, bool async, bool force);
1214 int regmap_fields_read(struct regmap_field *field, unsigned int id,
1215 unsigned int *val);
1216 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
1217 unsigned int mask, unsigned int val,
1218 bool *change, bool async, bool force);
1219
regmap_field_write(struct regmap_field * field,unsigned int val)1220 static inline int regmap_field_write(struct regmap_field *field,
1221 unsigned int val)
1222 {
1223 return regmap_field_update_bits_base(field, ~0, val,
1224 NULL, false, false);
1225 }
1226
regmap_field_force_write(struct regmap_field * field,unsigned int val)1227 static inline int regmap_field_force_write(struct regmap_field *field,
1228 unsigned int val)
1229 {
1230 return regmap_field_update_bits_base(field, ~0, val, NULL, false, true);
1231 }
1232
regmap_field_update_bits(struct regmap_field * field,unsigned int mask,unsigned int val)1233 static inline int regmap_field_update_bits(struct regmap_field *field,
1234 unsigned int mask, unsigned int val)
1235 {
1236 return regmap_field_update_bits_base(field, mask, val,
1237 NULL, false, false);
1238 }
1239
1240 static inline int
regmap_field_force_update_bits(struct regmap_field * field,unsigned int mask,unsigned int val)1241 regmap_field_force_update_bits(struct regmap_field *field,
1242 unsigned int mask, unsigned int val)
1243 {
1244 return regmap_field_update_bits_base(field, mask, val,
1245 NULL, false, true);
1246 }
1247
regmap_fields_write(struct regmap_field * field,unsigned int id,unsigned int val)1248 static inline int regmap_fields_write(struct regmap_field *field,
1249 unsigned int id, unsigned int val)
1250 {
1251 return regmap_fields_update_bits_base(field, id, ~0, val,
1252 NULL, false, false);
1253 }
1254
regmap_fields_force_write(struct regmap_field * field,unsigned int id,unsigned int val)1255 static inline int regmap_fields_force_write(struct regmap_field *field,
1256 unsigned int id, unsigned int val)
1257 {
1258 return regmap_fields_update_bits_base(field, id, ~0, val,
1259 NULL, false, true);
1260 }
1261
1262 static inline int
regmap_fields_update_bits(struct regmap_field * field,unsigned int id,unsigned int mask,unsigned int val)1263 regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1264 unsigned int mask, unsigned int val)
1265 {
1266 return regmap_fields_update_bits_base(field, id, mask, val,
1267 NULL, false, false);
1268 }
1269
1270 static inline int
regmap_fields_force_update_bits(struct regmap_field * field,unsigned int id,unsigned int mask,unsigned int val)1271 regmap_fields_force_update_bits(struct regmap_field *field, unsigned int id,
1272 unsigned int mask, unsigned int val)
1273 {
1274 return regmap_fields_update_bits_base(field, id, mask, val,
1275 NULL, false, true);
1276 }
1277
1278 /**
1279 * struct regmap_irq_type - IRQ type definitions.
1280 *
1281 * @type_reg_offset: Offset register for the irq type setting.
1282 * @type_rising_val: Register value to configure RISING type irq.
1283 * @type_falling_val: Register value to configure FALLING type irq.
1284 * @type_level_low_val: Register value to configure LEVEL_LOW type irq.
1285 * @type_level_high_val: Register value to configure LEVEL_HIGH type irq.
1286 * @types_supported: logical OR of IRQ_TYPE_* flags indicating supported types.
1287 */
1288 struct regmap_irq_type {
1289 unsigned int type_reg_offset;
1290 unsigned int type_reg_mask;
1291 unsigned int type_rising_val;
1292 unsigned int type_falling_val;
1293 unsigned int type_level_low_val;
1294 unsigned int type_level_high_val;
1295 unsigned int types_supported;
1296 };
1297
1298 /**
1299 * struct regmap_irq - Description of an IRQ for the generic regmap irq_chip.
1300 *
1301 * @reg_offset: Offset of the status/mask register within the bank
1302 * @mask: Mask used to flag/control the register.
1303 * @type: IRQ trigger type setting details if supported.
1304 */
1305 struct regmap_irq {
1306 unsigned int reg_offset;
1307 unsigned int mask;
1308 struct regmap_irq_type type;
1309 };
1310
1311 #define REGMAP_IRQ_REG(_irq, _off, _mask) \
1312 [_irq] = { .reg_offset = (_off), .mask = (_mask) }
1313
1314 #define REGMAP_IRQ_REG_LINE(_id, _reg_bits) \
1315 [_id] = { \
1316 .mask = BIT((_id) % (_reg_bits)), \
1317 .reg_offset = (_id) / (_reg_bits), \
1318 }
1319
1320 #define REGMAP_IRQ_MAIN_REG_OFFSET(arr) \
1321 { .num_regs = ARRAY_SIZE((arr)), .offset = &(arr)[0] }
1322
1323 struct regmap_irq_sub_irq_map {
1324 unsigned int num_regs;
1325 unsigned int *offset;
1326 };
1327
1328 /**
1329 * struct regmap_irq_chip - Description of a generic regmap irq_chip.
1330 *
1331 * @name: Descriptive name for IRQ controller.
1332 *
1333 * @main_status: Base main status register address. For chips which have
1334 * interrupts arranged in separate sub-irq blocks with own IRQ
1335 * registers and which have a main IRQ registers indicating
1336 * sub-irq blocks with unhandled interrupts. For such chips fill
1337 * sub-irq register information in status_base, mask_base and
1338 * ack_base.
1339 * @num_main_status_bits: Should be given to chips where number of meaningfull
1340 * main status bits differs from num_regs.
1341 * @sub_reg_offsets: arrays of mappings from main register bits to sub irq
1342 * registers. First item in array describes the registers
1343 * for first main status bit. Second array for second bit etc.
1344 * Offset is given as sub register status offset to
1345 * status_base. Should contain num_regs arrays.
1346 * Can be provided for chips with more complex mapping than
1347 * 1.st bit to 1.st sub-reg, 2.nd bit to 2.nd sub-reg, ...
1348 * @num_main_regs: Number of 'main status' irq registers for chips which have
1349 * main_status set.
1350 *
1351 * @status_base: Base status register address.
1352 * @mask_base: Base mask register address.
1353 * @mask_writeonly: Base mask register is write only.
1354 * @unmask_base: Base unmask register address. for chips who have
1355 * separate mask and unmask registers
1356 * @ack_base: Base ack address. If zero then the chip is clear on read.
1357 * Using zero value is possible with @use_ack bit.
1358 * @wake_base: Base address for wake enables. If zero unsupported.
1359 * @type_base: Base address for irq type. If zero unsupported.
1360 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
1361 * @init_ack_masked: Ack all masked interrupts once during initalization.
1362 * @mask_invert: Inverted mask register: cleared bits are masked out.
1363 * @use_ack: Use @ack register even if it is zero.
1364 * @ack_invert: Inverted ack register: cleared bits for ack.
1365 * @clear_ack: Use this to set 1 and 0 or vice-versa to clear interrupts.
1366 * @wake_invert: Inverted wake register: cleared bits are wake enabled.
1367 * @type_invert: Invert the type flags.
1368 * @type_in_mask: Use the mask registers for controlling irq type. For
1369 * interrupts defining type_rising/falling_mask use mask_base
1370 * for edge configuration and never update bits in type_base.
1371 * @clear_on_unmask: For chips with interrupts cleared on read: read the status
1372 * registers before unmasking interrupts to clear any bits
1373 * set when they were masked.
1374 * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
1375 *
1376 * @num_regs: Number of registers in each control bank.
1377 * @irqs: Descriptors for individual IRQs. Interrupt numbers are
1378 * assigned based on the index in the array of the interrupt.
1379 * @num_irqs: Number of descriptors.
1380 * @num_type_reg: Number of type registers.
1381 * @type_reg_stride: Stride to use for chips where type registers are not
1382 * contiguous.
1383 * @handle_pre_irq: Driver specific callback to handle interrupt from device
1384 * before regmap_irq_handler process the interrupts.
1385 * @handle_post_irq: Driver specific callback to handle interrupt from device
1386 * after handling the interrupts in regmap_irq_handler().
1387 * @irq_drv_data: Driver specific IRQ data which is passed as parameter when
1388 * driver specific pre/post interrupt handler is called.
1389 *
1390 * This is not intended to handle every possible interrupt controller, but
1391 * it should handle a substantial proportion of those that are found in the
1392 * wild.
1393 */
1394 struct regmap_irq_chip {
1395 const char *name;
1396
1397 unsigned int main_status;
1398 unsigned int num_main_status_bits;
1399 struct regmap_irq_sub_irq_map *sub_reg_offsets;
1400 int num_main_regs;
1401
1402 unsigned int status_base;
1403 unsigned int mask_base;
1404 unsigned int unmask_base;
1405 unsigned int ack_base;
1406 unsigned int wake_base;
1407 unsigned int type_base;
1408 unsigned int irq_reg_stride;
1409 bool mask_writeonly:1;
1410 bool init_ack_masked:1;
1411 bool mask_invert:1;
1412 bool use_ack:1;
1413 bool ack_invert:1;
1414 bool clear_ack:1;
1415 bool wake_invert:1;
1416 bool runtime_pm:1;
1417 bool type_invert:1;
1418 bool type_in_mask:1;
1419 bool clear_on_unmask:1;
1420
1421 int num_regs;
1422
1423 const struct regmap_irq *irqs;
1424 int num_irqs;
1425
1426 int num_type_reg;
1427 unsigned int type_reg_stride;
1428
1429 int (*handle_pre_irq)(void *irq_drv_data);
1430 int (*handle_post_irq)(void *irq_drv_data);
1431 void *irq_drv_data;
1432 };
1433
1434 struct regmap_irq_chip_data;
1435
1436 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
1437 int irq_base, const struct regmap_irq_chip *chip,
1438 struct regmap_irq_chip_data **data);
1439 int regmap_add_irq_chip_fwnode(struct fwnode_handle *fwnode,
1440 struct regmap *map, int irq,
1441 int irq_flags, int irq_base,
1442 const struct regmap_irq_chip *chip,
1443 struct regmap_irq_chip_data **data);
1444 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
1445
1446 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
1447 int irq_flags, int irq_base,
1448 const struct regmap_irq_chip *chip,
1449 struct regmap_irq_chip_data **data);
1450 int devm_regmap_add_irq_chip_fwnode(struct device *dev,
1451 struct fwnode_handle *fwnode,
1452 struct regmap *map, int irq,
1453 int irq_flags, int irq_base,
1454 const struct regmap_irq_chip *chip,
1455 struct regmap_irq_chip_data **data);
1456 void devm_regmap_del_irq_chip(struct device *dev, int irq,
1457 struct regmap_irq_chip_data *data);
1458
1459 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
1460 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
1461 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
1462
1463 #else
1464
1465 /*
1466 * These stubs should only ever be called by generic code which has
1467 * regmap based facilities, if they ever get called at runtime
1468 * something is going wrong and something probably needs to select
1469 * REGMAP.
1470 */
1471
regmap_write(struct regmap * map,unsigned int reg,unsigned int val)1472 static inline int regmap_write(struct regmap *map, unsigned int reg,
1473 unsigned int val)
1474 {
1475 WARN_ONCE(1, "regmap API is disabled");
1476 return -EINVAL;
1477 }
1478
regmap_write_async(struct regmap * map,unsigned int reg,unsigned int val)1479 static inline int regmap_write_async(struct regmap *map, unsigned int reg,
1480 unsigned int val)
1481 {
1482 WARN_ONCE(1, "regmap API is disabled");
1483 return -EINVAL;
1484 }
1485
regmap_raw_write(struct regmap * map,unsigned int reg,const void * val,size_t val_len)1486 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
1487 const void *val, size_t val_len)
1488 {
1489 WARN_ONCE(1, "regmap API is disabled");
1490 return -EINVAL;
1491 }
1492
regmap_raw_write_async(struct regmap * map,unsigned int reg,const void * val,size_t val_len)1493 static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1494 const void *val, size_t val_len)
1495 {
1496 WARN_ONCE(1, "regmap API is disabled");
1497 return -EINVAL;
1498 }
1499
regmap_noinc_write(struct regmap * map,unsigned int reg,const void * val,size_t val_len)1500 static inline int regmap_noinc_write(struct regmap *map, unsigned int reg,
1501 const void *val, size_t val_len)
1502 {
1503 WARN_ONCE(1, "regmap API is disabled");
1504 return -EINVAL;
1505 }
1506
regmap_bulk_write(struct regmap * map,unsigned int reg,const void * val,size_t val_count)1507 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
1508 const void *val, size_t val_count)
1509 {
1510 WARN_ONCE(1, "regmap API is disabled");
1511 return -EINVAL;
1512 }
1513
regmap_read(struct regmap * map,unsigned int reg,unsigned int * val)1514 static inline int regmap_read(struct regmap *map, unsigned int reg,
1515 unsigned int *val)
1516 {
1517 WARN_ONCE(1, "regmap API is disabled");
1518 return -EINVAL;
1519 }
1520
regmap_raw_read(struct regmap * map,unsigned int reg,void * val,size_t val_len)1521 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
1522 void *val, size_t val_len)
1523 {
1524 WARN_ONCE(1, "regmap API is disabled");
1525 return -EINVAL;
1526 }
1527
regmap_noinc_read(struct regmap * map,unsigned int reg,void * val,size_t val_len)1528 static inline int regmap_noinc_read(struct regmap *map, unsigned int reg,
1529 void *val, size_t val_len)
1530 {
1531 WARN_ONCE(1, "regmap API is disabled");
1532 return -EINVAL;
1533 }
1534
regmap_bulk_read(struct regmap * map,unsigned int reg,void * val,size_t val_count)1535 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
1536 void *val, size_t val_count)
1537 {
1538 WARN_ONCE(1, "regmap API is disabled");
1539 return -EINVAL;
1540 }
1541
regmap_update_bits_base(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val,bool * change,bool async,bool force)1542 static inline int regmap_update_bits_base(struct regmap *map, unsigned int reg,
1543 unsigned int mask, unsigned int val,
1544 bool *change, bool async, bool force)
1545 {
1546 WARN_ONCE(1, "regmap API is disabled");
1547 return -EINVAL;
1548 }
1549
regmap_set_bits(struct regmap * map,unsigned int reg,unsigned int bits)1550 static inline int regmap_set_bits(struct regmap *map,
1551 unsigned int reg, unsigned int bits)
1552 {
1553 WARN_ONCE(1, "regmap API is disabled");
1554 return -EINVAL;
1555 }
1556
regmap_clear_bits(struct regmap * map,unsigned int reg,unsigned int bits)1557 static inline int regmap_clear_bits(struct regmap *map,
1558 unsigned int reg, unsigned int bits)
1559 {
1560 WARN_ONCE(1, "regmap API is disabled");
1561 return -EINVAL;
1562 }
1563
regmap_test_bits(struct regmap * map,unsigned int reg,unsigned int bits)1564 static inline int regmap_test_bits(struct regmap *map,
1565 unsigned int reg, unsigned int bits)
1566 {
1567 WARN_ONCE(1, "regmap API is disabled");
1568 return -EINVAL;
1569 }
1570
regmap_field_update_bits_base(struct regmap_field * field,unsigned int mask,unsigned int val,bool * change,bool async,bool force)1571 static inline int regmap_field_update_bits_base(struct regmap_field *field,
1572 unsigned int mask, unsigned int val,
1573 bool *change, bool async, bool force)
1574 {
1575 WARN_ONCE(1, "regmap API is disabled");
1576 return -EINVAL;
1577 }
1578
regmap_fields_update_bits_base(struct regmap_field * field,unsigned int id,unsigned int mask,unsigned int val,bool * change,bool async,bool force)1579 static inline int regmap_fields_update_bits_base(struct regmap_field *field,
1580 unsigned int id,
1581 unsigned int mask, unsigned int val,
1582 bool *change, bool async, bool force)
1583 {
1584 WARN_ONCE(1, "regmap API is disabled");
1585 return -EINVAL;
1586 }
1587
regmap_update_bits(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val)1588 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
1589 unsigned int mask, unsigned int val)
1590 {
1591 WARN_ONCE(1, "regmap API is disabled");
1592 return -EINVAL;
1593 }
1594
regmap_update_bits_async(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val)1595 static inline int regmap_update_bits_async(struct regmap *map, unsigned int reg,
1596 unsigned int mask, unsigned int val)
1597 {
1598 WARN_ONCE(1, "regmap API is disabled");
1599 return -EINVAL;
1600 }
1601
regmap_update_bits_check(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val,bool * change)1602 static inline int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1603 unsigned int mask, unsigned int val,
1604 bool *change)
1605 {
1606 WARN_ONCE(1, "regmap API is disabled");
1607 return -EINVAL;
1608 }
1609
1610 static inline int
regmap_update_bits_check_async(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val,bool * change)1611 regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
1612 unsigned int mask, unsigned int val,
1613 bool *change)
1614 {
1615 WARN_ONCE(1, "regmap API is disabled");
1616 return -EINVAL;
1617 }
1618
regmap_write_bits(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val)1619 static inline int regmap_write_bits(struct regmap *map, unsigned int reg,
1620 unsigned int mask, unsigned int val)
1621 {
1622 WARN_ONCE(1, "regmap API is disabled");
1623 return -EINVAL;
1624 }
1625
regmap_field_write(struct regmap_field * field,unsigned int val)1626 static inline int regmap_field_write(struct regmap_field *field,
1627 unsigned int val)
1628 {
1629 WARN_ONCE(1, "regmap API is disabled");
1630 return -EINVAL;
1631 }
1632
regmap_field_force_write(struct regmap_field * field,unsigned int val)1633 static inline int regmap_field_force_write(struct regmap_field *field,
1634 unsigned int val)
1635 {
1636 WARN_ONCE(1, "regmap API is disabled");
1637 return -EINVAL;
1638 }
1639
regmap_field_update_bits(struct regmap_field * field,unsigned int mask,unsigned int val)1640 static inline int regmap_field_update_bits(struct regmap_field *field,
1641 unsigned int mask, unsigned int val)
1642 {
1643 WARN_ONCE(1, "regmap API is disabled");
1644 return -EINVAL;
1645 }
1646
1647 static inline int
regmap_field_force_update_bits(struct regmap_field * field,unsigned int mask,unsigned int val)1648 regmap_field_force_update_bits(struct regmap_field *field,
1649 unsigned int mask, unsigned int val)
1650 {
1651 WARN_ONCE(1, "regmap API is disabled");
1652 return -EINVAL;
1653 }
1654
regmap_fields_write(struct regmap_field * field,unsigned int id,unsigned int val)1655 static inline int regmap_fields_write(struct regmap_field *field,
1656 unsigned int id, unsigned int val)
1657 {
1658 WARN_ONCE(1, "regmap API is disabled");
1659 return -EINVAL;
1660 }
1661
regmap_fields_force_write(struct regmap_field * field,unsigned int id,unsigned int val)1662 static inline int regmap_fields_force_write(struct regmap_field *field,
1663 unsigned int id, unsigned int val)
1664 {
1665 WARN_ONCE(1, "regmap API is disabled");
1666 return -EINVAL;
1667 }
1668
1669 static inline int
regmap_fields_update_bits(struct regmap_field * field,unsigned int id,unsigned int mask,unsigned int val)1670 regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1671 unsigned int mask, unsigned int val)
1672 {
1673 WARN_ONCE(1, "regmap API is disabled");
1674 return -EINVAL;
1675 }
1676
1677 static inline int
regmap_fields_force_update_bits(struct regmap_field * field,unsigned int id,unsigned int mask,unsigned int val)1678 regmap_fields_force_update_bits(struct regmap_field *field, unsigned int id,
1679 unsigned int mask, unsigned int val)
1680 {
1681 WARN_ONCE(1, "regmap API is disabled");
1682 return -EINVAL;
1683 }
1684
regmap_get_val_bytes(struct regmap * map)1685 static inline int regmap_get_val_bytes(struct regmap *map)
1686 {
1687 WARN_ONCE(1, "regmap API is disabled");
1688 return -EINVAL;
1689 }
1690
regmap_get_max_register(struct regmap * map)1691 static inline int regmap_get_max_register(struct regmap *map)
1692 {
1693 WARN_ONCE(1, "regmap API is disabled");
1694 return -EINVAL;
1695 }
1696
regmap_get_reg_stride(struct regmap * map)1697 static inline int regmap_get_reg_stride(struct regmap *map)
1698 {
1699 WARN_ONCE(1, "regmap API is disabled");
1700 return -EINVAL;
1701 }
1702
regcache_sync(struct regmap * map)1703 static inline int regcache_sync(struct regmap *map)
1704 {
1705 WARN_ONCE(1, "regmap API is disabled");
1706 return -EINVAL;
1707 }
1708
regcache_sync_region(struct regmap * map,unsigned int min,unsigned int max)1709 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
1710 unsigned int max)
1711 {
1712 WARN_ONCE(1, "regmap API is disabled");
1713 return -EINVAL;
1714 }
1715
regcache_drop_region(struct regmap * map,unsigned int min,unsigned int max)1716 static inline int regcache_drop_region(struct regmap *map, unsigned int min,
1717 unsigned int max)
1718 {
1719 WARN_ONCE(1, "regmap API is disabled");
1720 return -EINVAL;
1721 }
1722
regcache_cache_only(struct regmap * map,bool enable)1723 static inline void regcache_cache_only(struct regmap *map, bool enable)
1724 {
1725 WARN_ONCE(1, "regmap API is disabled");
1726 }
1727
regcache_cache_bypass(struct regmap * map,bool enable)1728 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
1729 {
1730 WARN_ONCE(1, "regmap API is disabled");
1731 }
1732
regcache_mark_dirty(struct regmap * map)1733 static inline void regcache_mark_dirty(struct regmap *map)
1734 {
1735 WARN_ONCE(1, "regmap API is disabled");
1736 }
1737
regmap_async_complete(struct regmap * map)1738 static inline void regmap_async_complete(struct regmap *map)
1739 {
1740 WARN_ONCE(1, "regmap API is disabled");
1741 }
1742
regmap_register_patch(struct regmap * map,const struct reg_sequence * regs,int num_regs)1743 static inline int regmap_register_patch(struct regmap *map,
1744 const struct reg_sequence *regs,
1745 int num_regs)
1746 {
1747 WARN_ONCE(1, "regmap API is disabled");
1748 return -EINVAL;
1749 }
1750
regmap_parse_val(struct regmap * map,const void * buf,unsigned int * val)1751 static inline int regmap_parse_val(struct regmap *map, const void *buf,
1752 unsigned int *val)
1753 {
1754 WARN_ONCE(1, "regmap API is disabled");
1755 return -EINVAL;
1756 }
1757
dev_get_regmap(struct device * dev,const char * name)1758 static inline struct regmap *dev_get_regmap(struct device *dev,
1759 const char *name)
1760 {
1761 return NULL;
1762 }
1763
regmap_get_device(struct regmap * map)1764 static inline struct device *regmap_get_device(struct regmap *map)
1765 {
1766 WARN_ONCE(1, "regmap API is disabled");
1767 return NULL;
1768 }
1769
1770 #endif
1771
1772 #endif
1773