xref: /linux-6.15/include/linux/regmap.h (revision 63d23367)
1 #ifndef __LINUX_REGMAP_H
2 #define __LINUX_REGMAP_H
3 
4 /*
5  * Register map access API
6  *
7  * Copyright 2011 Wolfson Microelectronics plc
8  *
9  * Author: Mark Brown <[email protected]>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15 
16 #include <linux/list.h>
17 #include <linux/rbtree.h>
18 
19 struct module;
20 struct device;
21 struct i2c_client;
22 struct irq_domain;
23 struct spi_device;
24 struct regmap;
25 struct regmap_range_cfg;
26 
27 /* An enum of all the supported cache types */
28 enum regcache_type {
29 	REGCACHE_NONE,
30 	REGCACHE_RBTREE,
31 	REGCACHE_COMPRESSED
32 };
33 
34 /**
35  * Default value for a register.  We use an array of structs rather
36  * than a simple array as many modern devices have very sparse
37  * register maps.
38  *
39  * @reg: Register address.
40  * @def: Register default value.
41  */
42 struct reg_default {
43 	unsigned int reg;
44 	unsigned int def;
45 };
46 
47 #ifdef CONFIG_REGMAP
48 
49 enum regmap_endian {
50 	/* Unspecified -> 0 -> Backwards compatible default */
51 	REGMAP_ENDIAN_DEFAULT = 0,
52 	REGMAP_ENDIAN_BIG,
53 	REGMAP_ENDIAN_LITTLE,
54 	REGMAP_ENDIAN_NATIVE,
55 };
56 
57 /**
58  * A register range, used for access related checks
59  * (readable/writeable/volatile/precious checks)
60  *
61  * @range_min: address of first register
62  * @range_max: address of last register
63  */
64 struct regmap_range {
65 	unsigned int range_min;
66 	unsigned int range_max;
67 };
68 
69 /*
70  * A table of ranges including some yes ranges and some no ranges.
71  * If a register belongs to a no_range, the corresponding check function
72  * will return false. If a register belongs to a yes range, the corresponding
73  * check function will return true. "no_ranges" are searched first.
74  *
75  * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
76  * @n_yes_ranges: size of the above array
77  * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
78  * @n_no_ranges: size of the above array
79  */
80 struct regmap_access_table {
81 	const struct regmap_range *yes_ranges;
82 	unsigned int n_yes_ranges;
83 	const struct regmap_range *no_ranges;
84 	unsigned int n_no_ranges;
85 };
86 
87 typedef void (*regmap_lock)(void *);
88 typedef void (*regmap_unlock)(void *);
89 
90 /**
91  * Configuration for the register map of a device.
92  *
93  * @name: Optional name of the regmap. Useful when a device has multiple
94  *        register regions.
95  *
96  * @reg_bits: Number of bits in a register address, mandatory.
97  * @reg_stride: The register address stride. Valid register addresses are a
98  *              multiple of this value. If set to 0, a value of 1 will be
99  *              used.
100  * @pad_bits: Number of bits of padding between register and value.
101  * @val_bits: Number of bits in a register value, mandatory.
102  *
103  * @writeable_reg: Optional callback returning true if the register
104  *		   can be written to. If this field is NULL but wr_table
105  *		   (see below) is not, the check is performed on such table
106  *                 (a register is writeable if it belongs to one of the ranges
107  *                  specified by wr_table).
108  * @readable_reg: Optional callback returning true if the register
109  *		  can be read from. If this field is NULL but rd_table
110  *		   (see below) is not, the check is performed on such table
111  *                 (a register is readable if it belongs to one of the ranges
112  *                  specified by rd_table).
113  * @volatile_reg: Optional callback returning true if the register
114  *		  value can't be cached. If this field is NULL but
115  *		  volatile_table (see below) is not, the check is performed on
116  *                such table (a register is volatile if it belongs to one of
117  *                the ranges specified by volatile_table).
118  * @precious_reg: Optional callback returning true if the rgister
119  *		  should not be read outside of a call from the driver
120  *		  (eg, a clear on read interrupt status register). If this
121  *                field is NULL but precious_table (see below) is not, the
122  *                check is performed on such table (a register is precious if
123  *                it belongs to one of the ranges specified by precious_table).
124  * @lock:	  Optional lock callback (overrides regmap's default lock
125  *		  function, based on spinlock or mutex).
126  * @unlock:	  As above for unlocking.
127  * @lock_arg:	  this field is passed as the only argument of lock/unlock
128  *		  functions (ignored in case regular lock/unlock functions
129  *		  are not overridden).
130  *
131  * @max_register: Optional, specifies the maximum valid register index.
132  * @wr_table:     Optional, points to a struct regmap_access_table specifying
133  *                valid ranges for write access.
134  * @rd_table:     As above, for read access.
135  * @volatile_table: As above, for volatile registers.
136  * @precious_table: As above, for precious registers.
137  * @reg_defaults: Power on reset values for registers (for use with
138  *                register cache support).
139  * @num_reg_defaults: Number of elements in reg_defaults.
140  *
141  * @read_flag_mask: Mask to be set in the top byte of the register when doing
142  *                  a read.
143  * @write_flag_mask: Mask to be set in the top byte of the register when doing
144  *                   a write. If both read_flag_mask and write_flag_mask are
145  *                   empty the regmap_bus default masks are used.
146  * @use_single_rw: If set, converts the bulk read and write operations into
147  *		    a series of single read and write operations. This is useful
148  *		    for device that does not support bulk read and write.
149  *
150  * @cache_type: The actual cache type.
151  * @reg_defaults_raw: Power on reset values for registers (for use with
152  *                    register cache support).
153  * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
154  * @reg_format_endian: Endianness for formatted register addresses. If this is
155  *                     DEFAULT, the @reg_format_endian_default value from the
156  *                     regmap bus is used.
157  * @val_format_endian: Endianness for formatted register values. If this is
158  *                     DEFAULT, the @reg_format_endian_default value from the
159  *                     regmap bus is used.
160  *
161  * @ranges: Array of configuration entries for virtual address ranges.
162  * @num_ranges: Number of range configuration entries.
163  */
164 struct regmap_config {
165 	const char *name;
166 
167 	int reg_bits;
168 	int reg_stride;
169 	int pad_bits;
170 	int val_bits;
171 
172 	bool (*writeable_reg)(struct device *dev, unsigned int reg);
173 	bool (*readable_reg)(struct device *dev, unsigned int reg);
174 	bool (*volatile_reg)(struct device *dev, unsigned int reg);
175 	bool (*precious_reg)(struct device *dev, unsigned int reg);
176 	regmap_lock lock;
177 	regmap_unlock unlock;
178 	void *lock_arg;
179 
180 	unsigned int max_register;
181 	const struct regmap_access_table *wr_table;
182 	const struct regmap_access_table *rd_table;
183 	const struct regmap_access_table *volatile_table;
184 	const struct regmap_access_table *precious_table;
185 	const struct reg_default *reg_defaults;
186 	unsigned int num_reg_defaults;
187 	enum regcache_type cache_type;
188 	const void *reg_defaults_raw;
189 	unsigned int num_reg_defaults_raw;
190 
191 	u8 read_flag_mask;
192 	u8 write_flag_mask;
193 
194 	bool use_single_rw;
195 
196 	enum regmap_endian reg_format_endian;
197 	enum regmap_endian val_format_endian;
198 
199 	const struct regmap_range_cfg *ranges;
200 	unsigned int num_ranges;
201 };
202 
203 /**
204  * Configuration for indirectly accessed or paged registers.
205  * Registers, mapped to this virtual range, are accessed in two steps:
206  *     1. page selector register update;
207  *     2. access through data window registers.
208  *
209  * @name: Descriptive name for diagnostics
210  *
211  * @range_min: Address of the lowest register address in virtual range.
212  * @range_max: Address of the highest register in virtual range.
213  *
214  * @page_sel_reg: Register with selector field.
215  * @page_sel_mask: Bit shift for selector value.
216  * @page_sel_shift: Bit mask for selector value.
217  *
218  * @window_start: Address of first (lowest) register in data window.
219  * @window_len: Number of registers in data window.
220  */
221 struct regmap_range_cfg {
222 	const char *name;
223 
224 	/* Registers of virtual address range */
225 	unsigned int range_min;
226 	unsigned int range_max;
227 
228 	/* Page selector for indirect addressing */
229 	unsigned int selector_reg;
230 	unsigned int selector_mask;
231 	int selector_shift;
232 
233 	/* Data window (per each page) */
234 	unsigned int window_start;
235 	unsigned int window_len;
236 };
237 
238 typedef int (*regmap_hw_write)(void *context, const void *data,
239 			       size_t count);
240 typedef int (*regmap_hw_gather_write)(void *context,
241 				      const void *reg, size_t reg_len,
242 				      const void *val, size_t val_len);
243 typedef int (*regmap_hw_read)(void *context,
244 			      const void *reg_buf, size_t reg_size,
245 			      void *val_buf, size_t val_size);
246 typedef void (*regmap_hw_free_context)(void *context);
247 
248 /**
249  * Description of a hardware bus for the register map infrastructure.
250  *
251  * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
252  *	     to perform locking. This field is ignored if custom lock/unlock
253  *	     functions are used (see fields lock/unlock of
254  *	     struct regmap_config).
255  * @write: Write operation.
256  * @gather_write: Write operation with split register/value, return -ENOTSUPP
257  *                if not implemented  on a given device.
258  * @read: Read operation.  Data is returned in the buffer used to transmit
259  *         data.
260  * @read_flag_mask: Mask to be set in the top byte of the register when doing
261  *                  a read.
262  * @reg_format_endian_default: Default endianness for formatted register
263  *     addresses. Used when the regmap_config specifies DEFAULT. If this is
264  *     DEFAULT, BIG is assumed.
265  * @val_format_endian_default: Default endianness for formatted register
266  *     values. Used when the regmap_config specifies DEFAULT. If this is
267  *     DEFAULT, BIG is assumed.
268  */
269 struct regmap_bus {
270 	bool fast_io;
271 	regmap_hw_write write;
272 	regmap_hw_gather_write gather_write;
273 	regmap_hw_read read;
274 	regmap_hw_free_context free_context;
275 	u8 read_flag_mask;
276 	enum regmap_endian reg_format_endian_default;
277 	enum regmap_endian val_format_endian_default;
278 };
279 
280 struct regmap *regmap_init(struct device *dev,
281 			   const struct regmap_bus *bus,
282 			   void *bus_context,
283 			   const struct regmap_config *config);
284 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
285 			       const struct regmap_config *config);
286 struct regmap *regmap_init_spi(struct spi_device *dev,
287 			       const struct regmap_config *config);
288 struct regmap *regmap_init_mmio(struct device *dev,
289 				void __iomem *regs,
290 				const struct regmap_config *config);
291 
292 struct regmap *devm_regmap_init(struct device *dev,
293 				const struct regmap_bus *bus,
294 				void *bus_context,
295 				const struct regmap_config *config);
296 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
297 				    const struct regmap_config *config);
298 struct regmap *devm_regmap_init_spi(struct spi_device *dev,
299 				    const struct regmap_config *config);
300 struct regmap *devm_regmap_init_mmio(struct device *dev,
301 				     void __iomem *regs,
302 				     const struct regmap_config *config);
303 
304 void regmap_exit(struct regmap *map);
305 int regmap_reinit_cache(struct regmap *map,
306 			const struct regmap_config *config);
307 struct regmap *dev_get_regmap(struct device *dev, const char *name);
308 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
309 int regmap_raw_write(struct regmap *map, unsigned int reg,
310 		     const void *val, size_t val_len);
311 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
312 			size_t val_count);
313 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
314 int regmap_raw_read(struct regmap *map, unsigned int reg,
315 		    void *val, size_t val_len);
316 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
317 		     size_t val_count);
318 int regmap_update_bits(struct regmap *map, unsigned int reg,
319 		       unsigned int mask, unsigned int val);
320 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
321 			     unsigned int mask, unsigned int val,
322 			     bool *change);
323 int regmap_get_val_bytes(struct regmap *map);
324 
325 int regcache_sync(struct regmap *map);
326 int regcache_sync_region(struct regmap *map, unsigned int min,
327 			 unsigned int max);
328 void regcache_cache_only(struct regmap *map, bool enable);
329 void regcache_cache_bypass(struct regmap *map, bool enable);
330 void regcache_mark_dirty(struct regmap *map);
331 
332 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
333 			  int num_regs);
334 
335 static inline bool regmap_reg_in_range(unsigned int reg,
336 				       const struct regmap_range *range)
337 {
338 	return reg >= range->range_min && reg <= range->range_max;
339 }
340 
341 bool regmap_reg_in_ranges(unsigned int reg,
342 			  const struct regmap_range *ranges,
343 			  unsigned int nranges);
344 
345 /**
346  * Description of an IRQ for the generic regmap irq_chip.
347  *
348  * @reg_offset: Offset of the status/mask register within the bank
349  * @mask:       Mask used to flag/control the register.
350  */
351 struct regmap_irq {
352 	unsigned int reg_offset;
353 	unsigned int mask;
354 };
355 
356 /**
357  * Description of a generic regmap irq_chip.  This is not intended to
358  * handle every possible interrupt controller, but it should handle a
359  * substantial proportion of those that are found in the wild.
360  *
361  * @name:        Descriptive name for IRQ controller.
362  *
363  * @status_base: Base status register address.
364  * @mask_base:   Base mask register address.
365  * @ack_base:    Base ack address.  If zero then the chip is clear on read.
366  * @wake_base:   Base address for wake enables.  If zero unsupported.
367  * @irq_reg_stride:  Stride to use for chips where registers are not contiguous.
368  * @runtime_pm:  Hold a runtime PM lock on the device when accessing it.
369  *
370  * @num_regs:    Number of registers in each control bank.
371  * @irqs:        Descriptors for individual IRQs.  Interrupt numbers are
372  *               assigned based on the index in the array of the interrupt.
373  * @num_irqs:    Number of descriptors.
374  */
375 struct regmap_irq_chip {
376 	const char *name;
377 
378 	unsigned int status_base;
379 	unsigned int mask_base;
380 	unsigned int ack_base;
381 	unsigned int wake_base;
382 	unsigned int irq_reg_stride;
383 	unsigned int mask_invert;
384 	bool runtime_pm;
385 
386 	int num_regs;
387 
388 	const struct regmap_irq *irqs;
389 	int num_irqs;
390 };
391 
392 struct regmap_irq_chip_data;
393 
394 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
395 			int irq_base, const struct regmap_irq_chip *chip,
396 			struct regmap_irq_chip_data **data);
397 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
398 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
399 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
400 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
401 
402 #else
403 
404 /*
405  * These stubs should only ever be called by generic code which has
406  * regmap based facilities, if they ever get called at runtime
407  * something is going wrong and something probably needs to select
408  * REGMAP.
409  */
410 
411 static inline int regmap_write(struct regmap *map, unsigned int reg,
412 			       unsigned int val)
413 {
414 	WARN_ONCE(1, "regmap API is disabled");
415 	return -EINVAL;
416 }
417 
418 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
419 				   const void *val, size_t val_len)
420 {
421 	WARN_ONCE(1, "regmap API is disabled");
422 	return -EINVAL;
423 }
424 
425 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
426 				    const void *val, size_t val_count)
427 {
428 	WARN_ONCE(1, "regmap API is disabled");
429 	return -EINVAL;
430 }
431 
432 static inline int regmap_read(struct regmap *map, unsigned int reg,
433 			      unsigned int *val)
434 {
435 	WARN_ONCE(1, "regmap API is disabled");
436 	return -EINVAL;
437 }
438 
439 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
440 				  void *val, size_t val_len)
441 {
442 	WARN_ONCE(1, "regmap API is disabled");
443 	return -EINVAL;
444 }
445 
446 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
447 				   void *val, size_t val_count)
448 {
449 	WARN_ONCE(1, "regmap API is disabled");
450 	return -EINVAL;
451 }
452 
453 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
454 				     unsigned int mask, unsigned int val)
455 {
456 	WARN_ONCE(1, "regmap API is disabled");
457 	return -EINVAL;
458 }
459 
460 static inline int regmap_update_bits_check(struct regmap *map,
461 					   unsigned int reg,
462 					   unsigned int mask, unsigned int val,
463 					   bool *change)
464 {
465 	WARN_ONCE(1, "regmap API is disabled");
466 	return -EINVAL;
467 }
468 
469 static inline int regmap_get_val_bytes(struct regmap *map)
470 {
471 	WARN_ONCE(1, "regmap API is disabled");
472 	return -EINVAL;
473 }
474 
475 static inline int regcache_sync(struct regmap *map)
476 {
477 	WARN_ONCE(1, "regmap API is disabled");
478 	return -EINVAL;
479 }
480 
481 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
482 				       unsigned int max)
483 {
484 	WARN_ONCE(1, "regmap API is disabled");
485 	return -EINVAL;
486 }
487 
488 static inline void regcache_cache_only(struct regmap *map, bool enable)
489 {
490 	WARN_ONCE(1, "regmap API is disabled");
491 }
492 
493 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
494 {
495 	WARN_ONCE(1, "regmap API is disabled");
496 }
497 
498 static inline void regcache_mark_dirty(struct regmap *map)
499 {
500 	WARN_ONCE(1, "regmap API is disabled");
501 }
502 
503 static inline int regmap_register_patch(struct regmap *map,
504 					const struct reg_default *regs,
505 					int num_regs)
506 {
507 	WARN_ONCE(1, "regmap API is disabled");
508 	return -EINVAL;
509 }
510 
511 static inline struct regmap *dev_get_regmap(struct device *dev,
512 					    const char *name)
513 {
514 	return NULL;
515 }
516 
517 #endif
518 
519 #endif
520