blob: 15eddc1353bae3a272f6bbae881abe97981c0a6b [file] [log] [blame]
Mark Brownb83a3132011-05-11 19:59:58 +02001#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 <broonie@opensource.wolfsonmicro.com>
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
Mark Brownb83a3132011-05-11 19:59:58 +020016#include <linux/list.h>
Krystian Garbaciak6863ca62012-06-15 11:23:56 +010017#include <linux/rbtree.h>
Masahiro Yamadaadf08d42016-10-13 19:07:54 +090018#include <linux/delay.h>
Mateusz Krawczuk49ccc142013-08-06 18:34:40 +020019#include <linux/err.h>
Kevin Hilman3f0fa9a2013-08-14 16:05:02 -070020#include <linux/bug.h>
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +080021#include <linux/lockdep.h>
Mark Brownb83a3132011-05-11 19:59:58 +020022
Paul Gortmakerde477252011-05-26 13:46:22 -040023struct module;
Paul Gortmaker313162d2012-01-30 11:46:54 -050024struct device;
Mark Brown9943fa32011-06-20 19:02:29 +010025struct i2c_client;
Mark Brown90f790d2012-08-20 21:45:05 +010026struct irq_domain;
Mark Browna676f082011-05-12 11:42:10 +020027struct spi_device;
Josh Cartwrighta01779f2013-10-28 13:12:35 -050028struct spmi_device;
Mark Brownb83d2ff2012-03-11 11:49:17 +000029struct regmap;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +010030struct regmap_range_cfg;
Srinivas Kandagatla67252282013-06-11 13:18:15 +010031struct regmap_field;
Mark Brown22853222014-11-18 19:45:51 +010032struct snd_ac97;
Mark Brown9943fa32011-06-20 19:02:29 +010033
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010034/* An enum of all the supported cache types */
35enum regcache_type {
36 REGCACHE_NONE,
Dimitris Papastamos28644c802011-09-19 14:34:02 +010037 REGCACHE_RBTREE,
Mark Brown2ac902c2012-12-19 14:51:55 +000038 REGCACHE_COMPRESSED,
39 REGCACHE_FLAT,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010040};
41
Mark Browndd898b22011-07-20 22:28:58 +010042/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +000043 * struct reg_default - Default value for a register.
Mark Brownbd20eb52011-08-19 18:09:38 +090044 *
45 * @reg: Register address.
46 * @def: Register default value.
Charles Keepax2cf8e2d2017-01-12 11:17:39 +000047 *
48 * We use an array of structs rather than a simple array as many modern devices
49 * have very sparse register maps.
Mark Brownbd20eb52011-08-19 18:09:38 +090050 */
51struct reg_default {
52 unsigned int reg;
53 unsigned int def;
54};
55
Nariman Poushin8019ff62015-07-16 16:36:21 +010056/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +000057 * struct reg_sequence - An individual write from a sequence of writes.
Nariman Poushin8019ff62015-07-16 16:36:21 +010058 *
59 * @reg: Register address.
60 * @def: Register value.
Nariman Poushin2de9d602015-07-16 16:36:22 +010061 * @delay_us: Delay to be applied after the register write in microseconds
Charles Keepax2cf8e2d2017-01-12 11:17:39 +000062 *
63 * Register/value pairs for sequences of writes with an optional delay in
64 * microseconds to be applied after each write.
Nariman Poushin8019ff62015-07-16 16:36:21 +010065 */
66struct reg_sequence {
67 unsigned int reg;
68 unsigned int def;
Nariman Poushin2de9d602015-07-16 16:36:22 +010069 unsigned int delay_us;
Nariman Poushin8019ff62015-07-16 16:36:21 +010070};
71
Kuninori Morimotoca7a9442016-02-15 05:22:42 +000072#define regmap_update_bits(map, reg, mask, val) \
73 regmap_update_bits_base(map, reg, mask, val, NULL, false, false)
Kuninori Morimoto30ed9cb2016-02-15 05:23:01 +000074#define regmap_update_bits_async(map, reg, mask, val)\
75 regmap_update_bits_base(map, reg, mask, val, NULL, true, false)
Kuninori Morimoto98c2dc42016-02-15 05:23:17 +000076#define regmap_update_bits_check(map, reg, mask, val, change)\
77 regmap_update_bits_base(map, reg, mask, val, change, false, false)
Kuninori Morimoto89d8d4b2016-02-15 05:23:37 +000078#define regmap_update_bits_check_async(map, reg, mask, val, change)\
79 regmap_update_bits_base(map, reg, mask, val, change, true, false)
Kuninori Morimotoca7a9442016-02-15 05:22:42 +000080
Kuninori Morimotob8219572016-03-03 00:48:30 +000081#define regmap_write_bits(map, reg, mask, val) \
82 regmap_update_bits_base(map, reg, mask, val, NULL, false, true)
83
Kuninori Morimoto36741242016-02-15 05:24:15 +000084#define regmap_field_write(field, val) \
85 regmap_field_update_bits_base(field, ~0, val, NULL, false, false)
Kuninori Morimoto489061b2016-02-15 05:25:54 +000086#define regmap_field_force_write(field, val) \
87 regmap_field_update_bits_base(field, ~0, val, NULL, false, true)
Kuninori Morimoto721ed642016-02-15 05:24:33 +000088#define regmap_field_update_bits(field, mask, val)\
89 regmap_field_update_bits_base(field, mask, val, NULL, false, false)
Kuninori Morimoto489061b2016-02-15 05:25:54 +000090#define regmap_field_force_update_bits(field, mask, val) \
91 regmap_field_update_bits_base(field, mask, val, NULL, false, true)
Kuninori Morimoto36741242016-02-15 05:24:15 +000092
Kuninori Morimotobbf2c462016-02-15 05:25:15 +000093#define regmap_fields_write(field, id, val) \
94 regmap_fields_update_bits_base(field, id, ~0, val, NULL, false, false)
Kuninori Morimotoe6ef2432016-02-15 05:26:14 +000095#define regmap_fields_force_write(field, id, val) \
96 regmap_fields_update_bits_base(field, id, ~0, val, NULL, false, true)
Kuninori Morimoto48138602016-02-15 05:25:32 +000097#define regmap_fields_update_bits(field, id, mask, val)\
98 regmap_fields_update_bits_base(field, id, mask, val, NULL, false, false)
Kuninori Morimotoe6ef2432016-02-15 05:26:14 +000099#define regmap_fields_force_update_bits(field, id, mask, val) \
100 regmap_fields_update_bits_base(field, id, mask, val, NULL, false, true)
Kuninori Morimotobbf2c462016-02-15 05:25:15 +0000101
Philipp Zabel08188ba2016-07-06 16:19:41 +0200102/**
103 * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000104 *
Philipp Zabel08188ba2016-07-06 16:19:41 +0200105 * @map: Regmap to read from
106 * @addr: Address to poll
107 * @val: Unsigned integer variable to read the value into
108 * @cond: Break condition (usually involving @val)
109 * @sleep_us: Maximum time to sleep between reads in us (0
110 * tight-loops). Should be less than ~20ms since usleep_range
111 * is used (see Documentation/timers/timers-howto.txt).
112 * @timeout_us: Timeout in us, 0 means never timeout
113 *
114 * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
115 * error return value in case of a error read. In the two former cases,
116 * the last read value at @addr is stored in @val. Must not be called
117 * from atomic context if sleep_us or timeout_us are used.
118 *
119 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
120 */
121#define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_us) \
122({ \
Arnd Bergmann1b0c22e2017-10-13 14:12:45 +0200123 u64 __timeout_us = (timeout_us); \
124 unsigned long __sleep_us = (sleep_us); \
125 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
Ramesh Shanmugasundaram780b1352017-07-03 12:04:21 +0100126 int __ret; \
Arnd Bergmann1b0c22e2017-10-13 14:12:45 +0200127 might_sleep_if(__sleep_us); \
Philipp Zabel08188ba2016-07-06 16:19:41 +0200128 for (;;) { \
Ramesh Shanmugasundaram780b1352017-07-03 12:04:21 +0100129 __ret = regmap_read((map), (addr), &(val)); \
130 if (__ret) \
Philipp Zabel08188ba2016-07-06 16:19:41 +0200131 break; \
132 if (cond) \
133 break; \
Arnd Bergmann1b0c22e2017-10-13 14:12:45 +0200134 if ((__timeout_us) && \
Ramesh Shanmugasundaram780b1352017-07-03 12:04:21 +0100135 ktime_compare(ktime_get(), __timeout) > 0) { \
136 __ret = regmap_read((map), (addr), &(val)); \
Philipp Zabel08188ba2016-07-06 16:19:41 +0200137 break; \
138 } \
Arnd Bergmann1b0c22e2017-10-13 14:12:45 +0200139 if (__sleep_us) \
140 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
Philipp Zabel08188ba2016-07-06 16:19:41 +0200141 } \
Ramesh Shanmugasundaram780b1352017-07-03 12:04:21 +0100142 __ret ?: ((cond) ? 0 : -ETIMEDOUT); \
Philipp Zabel08188ba2016-07-06 16:19:41 +0200143})
144
Chen-Yu Tsai667063a2017-09-29 16:23:01 +0800145/**
146 * regmap_field_read_poll_timeout - Poll until a condition is met or timeout
147 *
148 * @field: Regmap field to read from
149 * @val: Unsigned integer variable to read the value into
150 * @cond: Break condition (usually involving @val)
151 * @sleep_us: Maximum time to sleep between reads in us (0
152 * tight-loops). Should be less than ~20ms since usleep_range
153 * is used (see Documentation/timers/timers-howto.txt).
154 * @timeout_us: Timeout in us, 0 means never timeout
155 *
156 * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_field_read
157 * error return value in case of a error read. In the two former cases,
158 * the last read value at @addr is stored in @val. Must not be called
159 * from atomic context if sleep_us or timeout_us are used.
160 *
161 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
162 */
163#define regmap_field_read_poll_timeout(field, val, cond, sleep_us, timeout_us) \
164({ \
Arnd Bergmann1b0c22e2017-10-13 14:12:45 +0200165 u64 __timeout_us = (timeout_us); \
166 unsigned long __sleep_us = (sleep_us); \
167 ktime_t timeout = ktime_add_us(ktime_get(), __timeout_us); \
Chen-Yu Tsai667063a2017-09-29 16:23:01 +0800168 int pollret; \
Arnd Bergmann1b0c22e2017-10-13 14:12:45 +0200169 might_sleep_if(__sleep_us); \
Chen-Yu Tsai667063a2017-09-29 16:23:01 +0800170 for (;;) { \
171 pollret = regmap_field_read((field), &(val)); \
172 if (pollret) \
173 break; \
174 if (cond) \
175 break; \
Arnd Bergmann1b0c22e2017-10-13 14:12:45 +0200176 if (__timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
Chen-Yu Tsai667063a2017-09-29 16:23:01 +0800177 pollret = regmap_field_read((field), &(val)); \
178 break; \
179 } \
Arnd Bergmann1b0c22e2017-10-13 14:12:45 +0200180 if (__sleep_us) \
181 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
Chen-Yu Tsai667063a2017-09-29 16:23:01 +0800182 } \
183 pollret ?: ((cond) ? 0 : -ETIMEDOUT); \
184})
185
Mark Brownb83d2ff2012-03-11 11:49:17 +0000186#ifdef CONFIG_REGMAP
187
Stephen Warren141eba22012-05-24 10:47:26 -0600188enum regmap_endian {
189 /* Unspecified -> 0 -> Backwards compatible default */
190 REGMAP_ENDIAN_DEFAULT = 0,
191 REGMAP_ENDIAN_BIG,
192 REGMAP_ENDIAN_LITTLE,
193 REGMAP_ENDIAN_NATIVE,
194};
195
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100196/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000197 * struct regmap_range - A register range, used for access related checks
198 * (readable/writeable/volatile/precious checks)
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100199 *
200 * @range_min: address of first register
201 * @range_max: address of last register
202 */
203struct regmap_range {
204 unsigned int range_min;
205 unsigned int range_max;
206};
207
Laxman Dewangan6112fe62013-09-20 18:00:10 +0530208#define regmap_reg_range(low, high) { .range_min = low, .range_max = high, }
209
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000210/**
211 * struct regmap_access_table - A table of register ranges for access checks
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100212 *
213 * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
214 * @n_yes_ranges: size of the above array
215 * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
216 * @n_no_ranges: size of the above array
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000217 *
218 * A table of ranges including some yes ranges and some no ranges.
219 * If a register belongs to a no_range, the corresponding check function
220 * will return false. If a register belongs to a yes range, the corresponding
221 * check function will return true. "no_ranges" are searched first.
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100222 */
223struct regmap_access_table {
224 const struct regmap_range *yes_ranges;
225 unsigned int n_yes_ranges;
226 const struct regmap_range *no_ranges;
227 unsigned int n_no_ranges;
228};
229
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200230typedef void (*regmap_lock)(void *);
231typedef void (*regmap_unlock)(void *);
232
Mark Brownbd20eb52011-08-19 18:09:38 +0900233/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000234 * struct regmap_config - Configuration for the register map of a device.
Mark Browndd898b22011-07-20 22:28:58 +0100235 *
Stephen Warrend3c242e2012-04-04 15:48:29 -0600236 * @name: Optional name of the regmap. Useful when a device has multiple
237 * register regions.
238 *
Mark Browndd898b22011-07-20 22:28:58 +0100239 * @reg_bits: Number of bits in a register address, mandatory.
Stephen Warrenf01ee602012-04-09 13:40:24 -0600240 * @reg_stride: The register address stride. Valid register addresses are a
241 * multiple of this value. If set to 0, a value of 1 will be
242 * used.
Mark Brown82159ba2012-01-18 10:52:25 +0000243 * @pad_bits: Number of bits of padding between register and value.
Mark Browndd898b22011-07-20 22:28:58 +0100244 * @val_bits: Number of bits in a register value, mandatory.
Mark Brown2e2ae662011-07-20 22:33:39 +0100245 *
Mark Brown3566cc92011-08-09 10:23:22 +0900246 * @writeable_reg: Optional callback returning true if the register
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100247 * can be written to. If this field is NULL but wr_table
248 * (see below) is not, the check is performed on such table
249 * (a register is writeable if it belongs to one of the ranges
250 * specified by wr_table).
Mark Brown3566cc92011-08-09 10:23:22 +0900251 * @readable_reg: Optional callback returning true if the register
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100252 * can be read from. If this field is NULL but rd_table
253 * (see below) is not, the check is performed on such table
254 * (a register is readable if it belongs to one of the ranges
255 * specified by rd_table).
Mark Brown3566cc92011-08-09 10:23:22 +0900256 * @volatile_reg: Optional callback returning true if the register
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100257 * value can't be cached. If this field is NULL but
258 * volatile_table (see below) is not, the check is performed on
259 * such table (a register is volatile if it belongs to one of
260 * the ranges specified by volatile_table).
Laszlo Pappbdc39642014-01-09 14:13:41 +0000261 * @precious_reg: Optional callback returning true if the register
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100262 * should not be read outside of a call from the driver
Laszlo Pappbdc39642014-01-09 14:13:41 +0000263 * (e.g., a clear on read interrupt status register). If this
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100264 * field is NULL but precious_table (see below) is not, the
265 * check is performed on such table (a register is precious if
266 * it belongs to one of the ranges specified by precious_table).
267 * @lock: Optional lock callback (overrides regmap's default lock
268 * function, based on spinlock or mutex).
269 * @unlock: As above for unlocking.
270 * @lock_arg: this field is passed as the only argument of lock/unlock
271 * functions (ignored in case regular lock/unlock functions
272 * are not overridden).
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800273 * @reg_read: Optional callback that if filled will be used to perform
274 * all the reads from the registers. Should only be provided for
Laszlo Pappbdc39642014-01-09 14:13:41 +0000275 * devices whose read operation cannot be represented as a simple
276 * read operation on a bus such as SPI, I2C, etc. Most of the
277 * devices do not need this.
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800278 * @reg_write: Same as above for writing.
279 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
280 * to perform locking. This field is ignored if custom lock/unlock
281 * functions are used (see fields lock/unlock of struct regmap_config).
282 * This field is a duplicate of a similar file in
283 * 'struct regmap_bus' and serves exact same purpose.
284 * Use it only for "no-bus" cases.
Stefan Agnerb429fab2016-01-19 17:14:39 -0800285 * @max_register: Optional, specifies the maximum valid register address.
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100286 * @wr_table: Optional, points to a struct regmap_access_table specifying
287 * valid ranges for write access.
288 * @rd_table: As above, for read access.
289 * @volatile_table: As above, for volatile registers.
290 * @precious_table: As above, for precious registers.
Mark Brownbd20eb52011-08-19 18:09:38 +0900291 * @reg_defaults: Power on reset values for registers (for use with
292 * register cache support).
293 * @num_reg_defaults: Number of elements in reg_defaults.
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200294 *
Tony Lindgrenf50e38c2016-09-15 13:56:10 -0700295 * @read_flag_mask: Mask to be set in the top bytes of the register when doing
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200296 * a read.
Tony Lindgrenf50e38c2016-09-15 13:56:10 -0700297 * @write_flag_mask: Mask to be set in the top bytes of the register when doing
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200298 * a write. If both read_flag_mask and write_flag_mask are
299 * empty the regmap_bus default masks are used.
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100300 * @use_single_rw: If set, converts the bulk read and write operations into
301 * a series of single read and write operations. This is useful
302 * for device that does not support bulk read and write.
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +0000303 * @can_multi_write: If set, the device supports the multi write mode of bulk
304 * write operations, if clear multi write requests will be
305 * split into individual write operations
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100306 *
307 * @cache_type: The actual cache type.
308 * @reg_defaults_raw: Power on reset values for registers (for use with
309 * register cache support).
310 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
Stephen Warren141eba22012-05-24 10:47:26 -0600311 * @reg_format_endian: Endianness for formatted register addresses. If this is
312 * DEFAULT, the @reg_format_endian_default value from the
313 * regmap bus is used.
314 * @val_format_endian: Endianness for formatted register values. If this is
315 * DEFAULT, the @reg_format_endian_default value from the
316 * regmap bus is used.
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100317 *
318 * @ranges: Array of configuration entries for virtual address ranges.
319 * @num_ranges: Number of range configuration entries.
Baolin Wang8698b932017-11-01 10:11:55 +0800320 * @hwlock_id: Specify the hardware spinlock id.
321 * @hwlock_mode: The hardware spinlock mode, should be HWLOCK_IRQSTATE,
322 * HWLOCK_IRQ or 0.
Mark Browndd898b22011-07-20 22:28:58 +0100323 */
Mark Brownb83a3132011-05-11 19:59:58 +0200324struct regmap_config {
Stephen Warrend3c242e2012-04-04 15:48:29 -0600325 const char *name;
326
Mark Brownb83a3132011-05-11 19:59:58 +0200327 int reg_bits;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600328 int reg_stride;
Mark Brown82159ba2012-01-18 10:52:25 +0000329 int pad_bits;
Mark Brownb83a3132011-05-11 19:59:58 +0200330 int val_bits;
Mark Brown2e2ae662011-07-20 22:33:39 +0100331
Mark Brown2e2ae662011-07-20 22:33:39 +0100332 bool (*writeable_reg)(struct device *dev, unsigned int reg);
333 bool (*readable_reg)(struct device *dev, unsigned int reg);
334 bool (*volatile_reg)(struct device *dev, unsigned int reg);
Mark Brown18694882011-08-08 15:40:22 +0900335 bool (*precious_reg)(struct device *dev, unsigned int reg);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200336 regmap_lock lock;
337 regmap_unlock unlock;
338 void *lock_arg;
Mark Brownbd20eb52011-08-19 18:09:38 +0900339
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800340 int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
341 int (*reg_write)(void *context, unsigned int reg, unsigned int val);
342
343 bool fast_io;
344
Mark Brownbd20eb52011-08-19 18:09:38 +0900345 unsigned int max_register;
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100346 const struct regmap_access_table *wr_table;
347 const struct regmap_access_table *rd_table;
348 const struct regmap_access_table *volatile_table;
349 const struct regmap_access_table *precious_table;
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100350 const struct reg_default *reg_defaults;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100351 unsigned int num_reg_defaults;
352 enum regcache_type cache_type;
353 const void *reg_defaults_raw;
354 unsigned int num_reg_defaults_raw;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200355
Tony Lindgrenf50e38c2016-09-15 13:56:10 -0700356 unsigned long read_flag_mask;
357 unsigned long write_flag_mask;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100358
359 bool use_single_rw;
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +0000360 bool can_multi_write;
Stephen Warren141eba22012-05-24 10:47:26 -0600361
362 enum regmap_endian reg_format_endian;
363 enum regmap_endian val_format_endian;
Mark Brown38e23192012-07-22 19:26:07 +0100364
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100365 const struct regmap_range_cfg *ranges;
Mark Browne3549cd2012-10-02 20:17:15 +0100366 unsigned int num_ranges;
Baolin Wang8698b932017-11-01 10:11:55 +0800367
368 unsigned int hwlock_id;
369 unsigned int hwlock_mode;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100370};
371
372/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000373 * struct regmap_range_cfg - Configuration for indirectly accessed or paged
374 * registers.
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100375 *
Mark Brownd058bb42012-10-03 12:40:47 +0100376 * @name: Descriptive name for diagnostics
377 *
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100378 * @range_min: Address of the lowest register address in virtual range.
379 * @range_max: Address of the highest register in virtual range.
380 *
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000381 * @selector_reg: Register with selector field.
382 * @selector_mask: Bit shift for selector value.
383 * @selector_shift: Bit mask for selector value.
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100384 *
385 * @window_start: Address of first (lowest) register in data window.
386 * @window_len: Number of registers in data window.
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000387 *
388 * Registers, mapped to this virtual range, are accessed in two steps:
389 * 1. page selector register update;
390 * 2. access through data window registers.
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100391 */
392struct regmap_range_cfg {
Mark Brownd058bb42012-10-03 12:40:47 +0100393 const char *name;
394
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100395 /* Registers of virtual address range */
396 unsigned int range_min;
397 unsigned int range_max;
398
399 /* Page selector for indirect addressing */
400 unsigned int selector_reg;
401 unsigned int selector_mask;
402 int selector_shift;
403
404 /* Data window (per each page) */
405 unsigned int window_start;
406 unsigned int window_len;
Mark Brownb83a3132011-05-11 19:59:58 +0200407};
408
Mark Brown0d509f22013-01-27 22:07:38 +0800409struct regmap_async;
410
Stephen Warren0135bbc2012-04-04 15:48:30 -0600411typedef int (*regmap_hw_write)(void *context, const void *data,
Mark Brownb83a3132011-05-11 19:59:58 +0200412 size_t count);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600413typedef int (*regmap_hw_gather_write)(void *context,
Mark Brownb83a3132011-05-11 19:59:58 +0200414 const void *reg, size_t reg_len,
415 const void *val, size_t val_len);
Mark Brown0d509f22013-01-27 22:07:38 +0800416typedef int (*regmap_hw_async_write)(void *context,
417 const void *reg, size_t reg_len,
418 const void *val, size_t val_len,
419 struct regmap_async *async);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600420typedef int (*regmap_hw_read)(void *context,
Mark Brownb83a3132011-05-11 19:59:58 +0200421 const void *reg_buf, size_t reg_size,
422 void *val_buf, size_t val_size);
Boris BREZILLON3ac17032014-04-17 11:40:11 +0200423typedef int (*regmap_hw_reg_read)(void *context, unsigned int reg,
424 unsigned int *val);
425typedef int (*regmap_hw_reg_write)(void *context, unsigned int reg,
426 unsigned int val);
Jon Ringle77792b12015-10-01 12:38:07 -0400427typedef int (*regmap_hw_reg_update_bits)(void *context, unsigned int reg,
428 unsigned int mask, unsigned int val);
Mark Brown0d509f22013-01-27 22:07:38 +0800429typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600430typedef void (*regmap_hw_free_context)(void *context);
Mark Brownb83a3132011-05-11 19:59:58 +0200431
432/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000433 * struct regmap_bus - Description of a hardware bus for the register map
434 * infrastructure.
Mark Brownb83a3132011-05-11 19:59:58 +0200435 *
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600436 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200437 * to perform locking. This field is ignored if custom lock/unlock
438 * functions are used (see fields lock/unlock of
439 * struct regmap_config).
Mark Brownb83a3132011-05-11 19:59:58 +0200440 * @write: Write operation.
441 * @gather_write: Write operation with split register/value, return -ENOTSUPP
442 * if not implemented on a given device.
Mark Brown0d509f22013-01-27 22:07:38 +0800443 * @async_write: Write operation which completes asynchronously, optional and
444 * must serialise with respect to non-async I/O.
Markus Pargmannc5f58f22015-08-21 10:26:40 +0200445 * @reg_write: Write a single register value to the given register address. This
446 * write operation has to complete when returning from the function.
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000447 * @reg_update_bits: Update bits operation to be used against volatile
448 * registers, intended for devices supporting some mechanism
449 * for setting clearing bits without having to
450 * read/modify/write.
Mark Brownb83a3132011-05-11 19:59:58 +0200451 * @read: Read operation. Data is returned in the buffer used to transmit
452 * data.
Markus Pargmannc5f58f22015-08-21 10:26:40 +0200453 * @reg_read: Read a single register value from a given register address.
454 * @free_context: Free context.
Mark Brown0d509f22013-01-27 22:07:38 +0800455 * @async_alloc: Allocate a regmap_async() structure.
Mark Brownb83a3132011-05-11 19:59:58 +0200456 * @read_flag_mask: Mask to be set in the top byte of the register when doing
457 * a read.
Stephen Warren141eba22012-05-24 10:47:26 -0600458 * @reg_format_endian_default: Default endianness for formatted register
459 * addresses. Used when the regmap_config specifies DEFAULT. If this is
460 * DEFAULT, BIG is assumed.
461 * @val_format_endian_default: Default endianness for formatted register
462 * values. Used when the regmap_config specifies DEFAULT. If this is
463 * DEFAULT, BIG is assumed.
Markus Pargmannadaac452015-08-30 09:33:53 +0200464 * @max_raw_read: Max raw read size that can be used on the bus.
465 * @max_raw_write: Max raw write size that can be used on the bus.
Mark Brownb83a3132011-05-11 19:59:58 +0200466 */
467struct regmap_bus {
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600468 bool fast_io;
Mark Brownb83a3132011-05-11 19:59:58 +0200469 regmap_hw_write write;
470 regmap_hw_gather_write gather_write;
Mark Brown0d509f22013-01-27 22:07:38 +0800471 regmap_hw_async_write async_write;
Boris BREZILLON3ac17032014-04-17 11:40:11 +0200472 regmap_hw_reg_write reg_write;
Jon Ringle77792b12015-10-01 12:38:07 -0400473 regmap_hw_reg_update_bits reg_update_bits;
Mark Brownb83a3132011-05-11 19:59:58 +0200474 regmap_hw_read read;
Boris BREZILLON3ac17032014-04-17 11:40:11 +0200475 regmap_hw_reg_read reg_read;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600476 regmap_hw_free_context free_context;
Mark Brown0d509f22013-01-27 22:07:38 +0800477 regmap_hw_async_alloc async_alloc;
Mark Brownb83a3132011-05-11 19:59:58 +0200478 u8 read_flag_mask;
Stephen Warren141eba22012-05-24 10:47:26 -0600479 enum regmap_endian reg_format_endian_default;
480 enum regmap_endian val_format_endian_default;
Markus Pargmannadaac452015-08-30 09:33:53 +0200481 size_t max_raw_read;
482 size_t max_raw_write;
Mark Brownb83a3132011-05-11 19:59:58 +0200483};
484
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800485/*
486 * __regmap_init functions.
487 *
488 * These functions take a lock key and name parameter, and should not be called
489 * directly. Instead, use the regmap_init macros that generate a key and name
490 * for each call.
491 */
492struct regmap *__regmap_init(struct device *dev,
493 const struct regmap_bus *bus,
494 void *bus_context,
495 const struct regmap_config *config,
496 struct lock_class_key *lock_key,
497 const char *lock_name);
498struct regmap *__regmap_init_i2c(struct i2c_client *i2c,
499 const struct regmap_config *config,
500 struct lock_class_key *lock_key,
501 const char *lock_name);
502struct regmap *__regmap_init_spi(struct spi_device *dev,
503 const struct regmap_config *config,
504 struct lock_class_key *lock_key,
505 const char *lock_name);
506struct regmap *__regmap_init_spmi_base(struct spmi_device *dev,
507 const struct regmap_config *config,
508 struct lock_class_key *lock_key,
509 const char *lock_name);
510struct regmap *__regmap_init_spmi_ext(struct spmi_device *dev,
511 const struct regmap_config *config,
512 struct lock_class_key *lock_key,
513 const char *lock_name);
Alex A. Mihaylovcc5d0db2017-06-02 10:06:27 +0300514struct regmap *__regmap_init_w1(struct device *w1_dev,
515 const struct regmap_config *config,
516 struct lock_class_key *lock_key,
517 const char *lock_name);
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800518struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
519 void __iomem *regs,
520 const struct regmap_config *config,
521 struct lock_class_key *lock_key,
522 const char *lock_name);
523struct regmap *__regmap_init_ac97(struct snd_ac97 *ac97,
524 const struct regmap_config *config,
525 struct lock_class_key *lock_key,
526 const char *lock_name);
527
528struct regmap *__devm_regmap_init(struct device *dev,
529 const struct regmap_bus *bus,
530 void *bus_context,
531 const struct regmap_config *config,
532 struct lock_class_key *lock_key,
533 const char *lock_name);
534struct regmap *__devm_regmap_init_i2c(struct i2c_client *i2c,
535 const struct regmap_config *config,
536 struct lock_class_key *lock_key,
537 const char *lock_name);
538struct regmap *__devm_regmap_init_spi(struct spi_device *dev,
539 const struct regmap_config *config,
540 struct lock_class_key *lock_key,
541 const char *lock_name);
542struct regmap *__devm_regmap_init_spmi_base(struct spmi_device *dev,
543 const struct regmap_config *config,
544 struct lock_class_key *lock_key,
545 const char *lock_name);
546struct regmap *__devm_regmap_init_spmi_ext(struct spmi_device *dev,
547 const struct regmap_config *config,
548 struct lock_class_key *lock_key,
549 const char *lock_name);
Alex A. Mihaylovcc5d0db2017-06-02 10:06:27 +0300550struct regmap *__devm_regmap_init_w1(struct device *w1_dev,
551 const struct regmap_config *config,
552 struct lock_class_key *lock_key,
553 const char *lock_name);
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800554struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
555 const char *clk_id,
556 void __iomem *regs,
557 const struct regmap_config *config,
558 struct lock_class_key *lock_key,
559 const char *lock_name);
560struct regmap *__devm_regmap_init_ac97(struct snd_ac97 *ac97,
561 const struct regmap_config *config,
562 struct lock_class_key *lock_key,
563 const char *lock_name);
564
565/*
566 * Wrapper for regmap_init macros to include a unique lockdep key and name
567 * for each call. No-op if CONFIG_LOCKDEP is not set.
568 *
569 * @fn: Real function to call (in the form __[*_]regmap_init[_*])
570 * @name: Config variable name (#config in the calling macro)
571 **/
572#ifdef CONFIG_LOCKDEP
573#define __regmap_lockdep_wrapper(fn, name, ...) \
574( \
575 ({ \
576 static struct lock_class_key _key; \
577 fn(__VA_ARGS__, &_key, \
578 KBUILD_BASENAME ":" \
579 __stringify(__LINE__) ":" \
580 "(" name ")->lock"); \
581 }) \
582)
583#else
584#define __regmap_lockdep_wrapper(fn, name, ...) fn(__VA_ARGS__, NULL, NULL)
585#endif
586
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800587/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000588 * regmap_init() - Initialise register map
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800589 *
590 * @dev: Device that will be interacted with
591 * @bus: Bus-specific callbacks to use with device
592 * @bus_context: Data passed to bus-specific callbacks
593 * @config: Configuration for register map
594 *
595 * The return value will be an ERR_PTR() on error or a valid pointer to
596 * a struct regmap. This function should generally not be called
597 * directly, it should be called by bus-specific init functions.
598 */
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800599#define regmap_init(dev, bus, bus_context, config) \
600 __regmap_lockdep_wrapper(__regmap_init, #config, \
601 dev, bus, bus_context, config)
Michal Simek6cfec042014-02-10 16:22:33 +0100602int regmap_attach_dev(struct device *dev, struct regmap *map,
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800603 const struct regmap_config *config);
Mark Browna676f082011-05-12 11:42:10 +0200604
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800605/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000606 * regmap_init_i2c() - Initialise register map
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800607 *
608 * @i2c: Device that will be interacted with
609 * @config: Configuration for register map
610 *
611 * The return value will be an ERR_PTR() on error or a valid pointer to
612 * a struct regmap.
613 */
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800614#define regmap_init_i2c(i2c, config) \
615 __regmap_lockdep_wrapper(__regmap_init_i2c, #config, \
616 i2c, config)
Mark Brown22853222014-11-18 19:45:51 +0100617
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800618/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000619 * regmap_init_spi() - Initialise register map
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800620 *
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000621 * @dev: Device that will be interacted with
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800622 * @config: Configuration for register map
623 *
624 * The return value will be an ERR_PTR() on error or a valid pointer to
625 * a struct regmap.
626 */
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800627#define regmap_init_spi(dev, config) \
628 __regmap_lockdep_wrapper(__regmap_init_spi, #config, \
629 dev, config)
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800630
631/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000632 * regmap_init_spmi_base() - Create regmap for the Base register space
633 *
634 * @dev: SPMI device that will be interacted with
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800635 * @config: Configuration for register map
636 *
637 * The return value will be an ERR_PTR() on error or a valid pointer to
638 * a struct regmap.
639 */
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800640#define regmap_init_spmi_base(dev, config) \
641 __regmap_lockdep_wrapper(__regmap_init_spmi_base, #config, \
642 dev, config)
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800643
644/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000645 * regmap_init_spmi_ext() - Create regmap for Ext register space
646 *
647 * @dev: Device that will be interacted with
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800648 * @config: Configuration for register map
649 *
650 * The return value will be an ERR_PTR() on error or a valid pointer to
651 * a struct regmap.
652 */
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800653#define regmap_init_spmi_ext(dev, config) \
654 __regmap_lockdep_wrapper(__regmap_init_spmi_ext, #config, \
655 dev, config)
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800656
657/**
Alex A. Mihaylovcc5d0db2017-06-02 10:06:27 +0300658 * regmap_init_w1() - Initialise register map
659 *
660 * @w1_dev: Device that will be interacted with
661 * @config: Configuration for register map
662 *
663 * The return value will be an ERR_PTR() on error or a valid pointer to
664 * a struct regmap.
665 */
666#define regmap_init_w1(w1_dev, config) \
667 __regmap_lockdep_wrapper(__regmap_init_w1, #config, \
668 w1_dev, config)
669
670/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000671 * regmap_init_mmio_clk() - Initialise register map with register clock
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800672 *
673 * @dev: Device that will be interacted with
674 * @clk_id: register clock consumer ID
675 * @regs: Pointer to memory-mapped IO region
676 * @config: Configuration for register map
677 *
678 * The return value will be an ERR_PTR() on error or a valid pointer to
679 * a struct regmap.
680 */
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800681#define regmap_init_mmio_clk(dev, clk_id, regs, config) \
682 __regmap_lockdep_wrapper(__regmap_init_mmio_clk, #config, \
683 dev, clk_id, regs, config)
Philipp Zabel878ec672013-02-14 17:39:08 +0100684
685/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000686 * regmap_init_mmio() - Initialise register map
Philipp Zabel878ec672013-02-14 17:39:08 +0100687 *
688 * @dev: Device that will be interacted with
689 * @regs: Pointer to memory-mapped IO region
690 * @config: Configuration for register map
691 *
692 * The return value will be an ERR_PTR() on error or a valid pointer to
693 * a struct regmap.
694 */
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800695#define regmap_init_mmio(dev, regs, config) \
696 regmap_init_mmio_clk(dev, NULL, regs, config)
Philipp Zabel878ec672013-02-14 17:39:08 +0100697
698/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000699 * regmap_init_ac97() - Initialise AC'97 register map
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800700 *
701 * @ac97: Device that will be interacted with
702 * @config: Configuration for register map
703 *
704 * The return value will be an ERR_PTR() on error or a valid pointer to
705 * a struct regmap.
706 */
707#define regmap_init_ac97(ac97, config) \
708 __regmap_lockdep_wrapper(__regmap_init_ac97, #config, \
709 ac97, config)
710bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
711
712/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000713 * devm_regmap_init() - Initialise managed register map
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800714 *
715 * @dev: Device that will be interacted with
716 * @bus: Bus-specific callbacks to use with device
717 * @bus_context: Data passed to bus-specific callbacks
718 * @config: Configuration for register map
719 *
720 * The return value will be an ERR_PTR() on error or a valid pointer
721 * to a struct regmap. This function should generally not be called
722 * directly, it should be called by bus-specific init functions. The
723 * map will be automatically freed by the device management code.
724 */
725#define devm_regmap_init(dev, bus, bus_context, config) \
726 __regmap_lockdep_wrapper(__devm_regmap_init, #config, \
727 dev, bus, bus_context, config)
728
729/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000730 * devm_regmap_init_i2c() - Initialise managed register map
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800731 *
732 * @i2c: Device that will be interacted with
733 * @config: Configuration for register map
734 *
735 * The return value will be an ERR_PTR() on error or a valid pointer
736 * to a struct regmap. The regmap will be automatically freed by the
737 * device management code.
738 */
739#define devm_regmap_init_i2c(i2c, config) \
740 __regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config, \
741 i2c, config)
742
743/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000744 * devm_regmap_init_spi() - Initialise register map
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800745 *
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000746 * @dev: Device that will be interacted with
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800747 * @config: Configuration for register map
748 *
749 * The return value will be an ERR_PTR() on error or a valid pointer
750 * to a struct regmap. The map will be automatically freed by the
751 * device management code.
752 */
753#define devm_regmap_init_spi(dev, config) \
754 __regmap_lockdep_wrapper(__devm_regmap_init_spi, #config, \
755 dev, config)
756
757/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000758 * devm_regmap_init_spmi_base() - Create managed regmap for Base register space
759 *
760 * @dev: SPMI device that will be interacted with
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800761 * @config: Configuration for register map
762 *
763 * The return value will be an ERR_PTR() on error or a valid pointer
764 * to a struct regmap. The regmap will be automatically freed by the
765 * device management code.
766 */
767#define devm_regmap_init_spmi_base(dev, config) \
768 __regmap_lockdep_wrapper(__devm_regmap_init_spmi_base, #config, \
769 dev, config)
770
771/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000772 * devm_regmap_init_spmi_ext() - Create managed regmap for Ext register space
773 *
774 * @dev: SPMI device that will be interacted with
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800775 * @config: Configuration for register map
776 *
777 * The return value will be an ERR_PTR() on error or a valid pointer
778 * to a struct regmap. The regmap will be automatically freed by the
779 * device management code.
780 */
781#define devm_regmap_init_spmi_ext(dev, config) \
782 __regmap_lockdep_wrapper(__devm_regmap_init_spmi_ext, #config, \
783 dev, config)
784
785/**
Alex A. Mihaylovcc5d0db2017-06-02 10:06:27 +0300786 * devm_regmap_init_w1() - Initialise managed register map
787 *
788 * @w1_dev: Device that will be interacted with
789 * @config: Configuration for register map
790 *
791 * The return value will be an ERR_PTR() on error or a valid pointer
792 * to a struct regmap. The regmap will be automatically freed by the
793 * device management code.
794 */
795#define devm_regmap_init_w1(w1_dev, config) \
796 __regmap_lockdep_wrapper(__devm_regmap_init_w1, #config, \
797 w1_dev, config)
798/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000799 * devm_regmap_init_mmio_clk() - Initialise managed register map with clock
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800800 *
801 * @dev: Device that will be interacted with
802 * @clk_id: register clock consumer ID
803 * @regs: Pointer to memory-mapped IO region
804 * @config: Configuration for register map
805 *
806 * The return value will be an ERR_PTR() on error or a valid pointer
807 * to a struct regmap. The regmap will be automatically freed by the
808 * device management code.
809 */
810#define devm_regmap_init_mmio_clk(dev, clk_id, regs, config) \
811 __regmap_lockdep_wrapper(__devm_regmap_init_mmio_clk, #config, \
812 dev, clk_id, regs, config)
Philipp Zabel878ec672013-02-14 17:39:08 +0100813
814/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000815 * devm_regmap_init_mmio() - Initialise managed register map
Philipp Zabel878ec672013-02-14 17:39:08 +0100816 *
817 * @dev: Device that will be interacted with
818 * @regs: Pointer to memory-mapped IO region
819 * @config: Configuration for register map
820 *
821 * The return value will be an ERR_PTR() on error or a valid pointer
822 * to a struct regmap. The regmap will be automatically freed by the
823 * device management code.
824 */
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800825#define devm_regmap_init_mmio(dev, regs, config) \
826 devm_regmap_init_mmio_clk(dev, NULL, regs, config)
Mark Brownc0eb4672012-01-30 19:56:52 +0000827
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800828/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000829 * devm_regmap_init_ac97() - Initialise AC'97 register map
Nicolas Boichat1ed81112015-08-11 18:04:21 +0800830 *
831 * @ac97: 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. The regmap will be automatically freed by the
836 * device management code.
837 */
838#define devm_regmap_init_ac97(ac97, config) \
839 __regmap_lockdep_wrapper(__devm_regmap_init_ac97, #config, \
840 ac97, config)
Mark Brownb83a3132011-05-11 19:59:58 +0200841
842void regmap_exit(struct regmap *map);
843int regmap_reinit_cache(struct regmap *map,
844 const struct regmap_config *config);
Mark Brown72b39f62012-05-08 17:44:40 +0100845struct regmap *dev_get_regmap(struct device *dev, const char *name);
Tuomas Tynkkynen8d7d3972014-07-21 18:38:47 +0300846struct device *regmap_get_device(struct regmap *map);
Mark Brownb83a3132011-05-11 19:59:58 +0200847int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
Mark Brown915f4412013-10-09 13:30:10 +0100848int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val);
Mark Brownb83a3132011-05-11 19:59:58 +0200849int regmap_raw_write(struct regmap *map, unsigned int reg,
850 const void *val, size_t val_len);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530851int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
852 size_t val_count);
Nariman Poushin8019ff62015-07-16 16:36:21 +0100853int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
Anthony Oleche33fabd2013-10-11 15:31:11 +0100854 int num_regs);
Charles Keepax1d5b40b2014-02-25 13:45:50 +0000855int regmap_multi_reg_write_bypassed(struct regmap *map,
Nariman Poushin8019ff62015-07-16 16:36:21 +0100856 const struct reg_sequence *regs,
Charles Keepax1d5b40b2014-02-25 13:45:50 +0000857 int num_regs);
Mark Brown0d509f22013-01-27 22:07:38 +0800858int regmap_raw_write_async(struct regmap *map, unsigned int reg,
859 const void *val, size_t val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200860int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
861int regmap_raw_read(struct regmap *map, unsigned int reg,
862 void *val, size_t val_len);
863int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
864 size_t val_count);
Kuninori Morimoto91d31b92016-02-15 05:22:18 +0000865int regmap_update_bits_base(struct regmap *map, unsigned int reg,
866 unsigned int mask, unsigned int val,
867 bool *change, bool async, bool force);
Mark Browna6539c32012-02-17 14:20:14 -0800868int regmap_get_val_bytes(struct regmap *map);
Srinivas Kandagatla668abc72015-05-21 17:42:43 +0100869int regmap_get_max_register(struct regmap *map);
Srinivas Kandagatlaa2f776c2015-05-21 17:42:54 +0100870int regmap_get_reg_stride(struct regmap *map);
Mark Brown0d509f22013-01-27 22:07:38 +0800871int regmap_async_complete(struct regmap *map);
Mark Brown221ad7f2013-03-26 21:24:20 +0000872bool regmap_can_raw_write(struct regmap *map);
Markus Pargmannf50c9eb2015-08-30 09:33:54 +0200873size_t regmap_get_raw_read_max(struct regmap *map);
874size_t regmap_get_raw_write_max(struct regmap *map);
Mark Brownb83a3132011-05-11 19:59:58 +0200875
Mark Brown39a58432011-09-19 18:21:49 +0100876int regcache_sync(struct regmap *map);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000877int regcache_sync_region(struct regmap *map, unsigned int min,
878 unsigned int max);
Mark Brown697e85b2013-05-08 13:55:22 +0100879int regcache_drop_region(struct regmap *map, unsigned int min,
880 unsigned int max);
Mark Brown92afb282011-09-19 18:22:14 +0100881void regcache_cache_only(struct regmap *map, bool enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100882void regcache_cache_bypass(struct regmap *map, bool enable);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200883void regcache_mark_dirty(struct regmap *map);
Mark Brown92afb282011-09-19 18:22:14 +0100884
Mark Brown154881e2013-05-08 13:55:23 +0100885bool regmap_check_range_table(struct regmap *map, unsigned int reg,
886 const struct regmap_access_table *table);
887
Nariman Poushin8019ff62015-07-16 16:36:21 +0100888int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
Mark Brown22f0d902012-01-21 12:01:14 +0000889 int num_regs);
Nenghua Cao13ff50c2014-02-19 18:44:13 +0800890int regmap_parse_val(struct regmap *map, const void *buf,
891 unsigned int *val);
Mark Brown22f0d902012-01-21 12:01:14 +0000892
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100893static inline bool regmap_reg_in_range(unsigned int reg,
894 const struct regmap_range *range)
895{
896 return reg >= range->range_min && reg <= range->range_max;
897}
898
899bool regmap_reg_in_ranges(unsigned int reg,
900 const struct regmap_range *ranges,
901 unsigned int nranges);
902
Mark Brownf8beab22011-10-28 23:50:49 +0200903/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000904 * struct reg_field - Description of an register field
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100905 *
906 * @reg: Offset of the register within the regmap bank
907 * @lsb: lsb of the register field.
Bintian Wangf27b37f2015-01-27 20:50:29 +0800908 * @msb: msb of the register field.
Kuninori Morimotoa0102372013-09-01 20:30:50 -0700909 * @id_size: port size if it has some ports
910 * @id_offset: address offset for each ports
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100911 */
912struct reg_field {
913 unsigned int reg;
914 unsigned int lsb;
915 unsigned int msb;
Kuninori Morimotoa0102372013-09-01 20:30:50 -0700916 unsigned int id_size;
917 unsigned int id_offset;
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100918};
919
920#define REG_FIELD(_reg, _lsb, _msb) { \
921 .reg = _reg, \
922 .lsb = _lsb, \
923 .msb = _msb, \
924 }
925
926struct regmap_field *regmap_field_alloc(struct regmap *regmap,
927 struct reg_field reg_field);
928void regmap_field_free(struct regmap_field *field);
929
930struct regmap_field *devm_regmap_field_alloc(struct device *dev,
931 struct regmap *regmap, struct reg_field reg_field);
932void devm_regmap_field_free(struct device *dev, struct regmap_field *field);
933
934int regmap_field_read(struct regmap_field *field, unsigned int *val);
Kuninori Morimoto28972ea2016-02-15 05:23:55 +0000935int regmap_field_update_bits_base(struct regmap_field *field,
936 unsigned int mask, unsigned int val,
937 bool *change, bool async, bool force);
Kuninori Morimotoa0102372013-09-01 20:30:50 -0700938int regmap_fields_read(struct regmap_field *field, unsigned int id,
939 unsigned int *val);
Kuninori Morimotoe126ede2016-02-15 05:24:51 +0000940int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
941 unsigned int mask, unsigned int val,
942 bool *change, bool async, bool force);
Srinivas Kandagatla67252282013-06-11 13:18:15 +0100943
944/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000945 * struct regmap_irq - Description of an IRQ for the generic regmap irq_chip.
Mark Brownf8beab22011-10-28 23:50:49 +0200946 *
947 * @reg_offset: Offset of the status/mask register within the bank
948 * @mask: Mask used to flag/control the register.
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530949 * @type_reg_offset: Offset register for the irq type setting.
950 * @type_rising_mask: Mask bit to configure RISING type irq.
951 * @type_falling_mask: Mask bit to configure FALLING type irq.
Mark Brownf8beab22011-10-28 23:50:49 +0200952 */
953struct regmap_irq {
954 unsigned int reg_offset;
955 unsigned int mask;
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530956 unsigned int type_reg_offset;
957 unsigned int type_rising_mask;
958 unsigned int type_falling_mask;
Mark Brownf8beab22011-10-28 23:50:49 +0200959};
960
Qipeng Zhab4fe8ba2015-09-15 00:39:17 +0800961#define REGMAP_IRQ_REG(_irq, _off, _mask) \
962 [_irq] = { .reg_offset = (_off), .mask = (_mask) }
963
Mark Brownf8beab22011-10-28 23:50:49 +0200964/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000965 * struct regmap_irq_chip - Description of a generic regmap irq_chip.
Mark Brownf8beab22011-10-28 23:50:49 +0200966 *
967 * @name: Descriptive name for IRQ controller.
968 *
969 * @status_base: Base status register address.
970 * @mask_base: Base mask register address.
Michael Grzeschika71411d2017-06-23 14:35:09 +0200971 * @mask_writeonly: Base mask register is write only.
Guo Zeng7b7d1962015-09-17 05:23:20 +0000972 * @unmask_base: Base unmask register address. for chips who have
973 * separate mask and unmask registers
Alexander Shiyand3233432013-12-15 13:36:51 +0400974 * @ack_base: Base ack address. If zero then the chip is clear on read.
975 * Using zero value is possible with @use_ack bit.
Mark Browna43fd502012-06-05 14:34:03 +0100976 * @wake_base: Base address for wake enables. If zero unsupported.
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530977 * @type_base: Base address for irq type. If zero unsupported.
Graeme Gregory022f926a2012-05-14 22:40:43 +0900978 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
Philipp Zabel2753e6f2013-07-22 17:15:52 +0200979 * @init_ack_masked: Ack all masked interrupts once during initalization.
Philipp Zabel68622bd2013-07-24 10:26:48 +0200980 * @mask_invert: Inverted mask register: cleared bits are masked out.
Alexander Shiyand3233432013-12-15 13:36:51 +0400981 * @use_ack: Use @ack register even if it is zero.
Guo Zenga650fdd2015-09-17 05:23:21 +0000982 * @ack_invert: Inverted ack register: cleared bits for ack.
Philipp Zabel68622bd2013-07-24 10:26:48 +0200983 * @wake_invert: Inverted wake register: cleared bits are wake enabled.
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530984 * @type_invert: Invert the type flags.
Mark Brown0c00c502012-07-24 15:41:19 +0100985 * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
Mark Brownf8beab22011-10-28 23:50:49 +0200986 *
987 * @num_regs: Number of registers in each control bank.
988 * @irqs: Descriptors for individual IRQs. Interrupt numbers are
989 * assigned based on the index in the array of the interrupt.
990 * @num_irqs: Number of descriptors.
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530991 * @num_type_reg: Number of type registers.
992 * @type_reg_stride: Stride to use for chips where type registers are not
993 * contiguous.
Laxman Dewanganccc12562016-05-20 20:40:26 +0530994 * @handle_pre_irq: Driver specific callback to handle interrupt from device
995 * before regmap_irq_handler process the interrupts.
996 * @handle_post_irq: Driver specific callback to handle interrupt from device
997 * after handling the interrupts in regmap_irq_handler().
998 * @irq_drv_data: Driver specific IRQ data which is passed as parameter when
999 * driver specific pre/post interrupt handler is called.
Charles Keepax2cf8e2d2017-01-12 11:17:39 +00001000 *
1001 * This is not intended to handle every possible interrupt controller, but
1002 * it should handle a substantial proportion of those that are found in the
1003 * wild.
Mark Brownf8beab22011-10-28 23:50:49 +02001004 */
1005struct regmap_irq_chip {
1006 const char *name;
1007
1008 unsigned int status_base;
1009 unsigned int mask_base;
Guo Zeng7b7d1962015-09-17 05:23:20 +00001010 unsigned int unmask_base;
Mark Brownf8beab22011-10-28 23:50:49 +02001011 unsigned int ack_base;
Mark Browna43fd502012-06-05 14:34:03 +01001012 unsigned int wake_base;
Laxman Dewangan7a78479f2015-12-22 18:25:26 +05301013 unsigned int type_base;
Graeme Gregory022f926a2012-05-14 22:40:43 +09001014 unsigned int irq_reg_stride;
Michael Grzeschika71411d2017-06-23 14:35:09 +02001015 bool mask_writeonly:1;
Philipp Zabelf484f7a2013-07-24 10:26:42 +02001016 bool init_ack_masked:1;
1017 bool mask_invert:1;
Alexander Shiyand3233432013-12-15 13:36:51 +04001018 bool use_ack:1;
Guo Zenga650fdd2015-09-17 05:23:21 +00001019 bool ack_invert:1;
Philipp Zabelf484f7a2013-07-24 10:26:42 +02001020 bool wake_invert:1;
1021 bool runtime_pm:1;
Laxman Dewangan7a78479f2015-12-22 18:25:26 +05301022 bool type_invert:1;
Mark Brownf8beab22011-10-28 23:50:49 +02001023
1024 int num_regs;
1025
1026 const struct regmap_irq *irqs;
1027 int num_irqs;
Laxman Dewangan7a78479f2015-12-22 18:25:26 +05301028
1029 int num_type_reg;
1030 unsigned int type_reg_stride;
Laxman Dewanganccc12562016-05-20 20:40:26 +05301031
1032 int (*handle_pre_irq)(void *irq_drv_data);
1033 int (*handle_post_irq)(void *irq_drv_data);
1034 void *irq_drv_data;
Mark Brownf8beab22011-10-28 23:50:49 +02001035};
1036
1037struct regmap_irq_chip_data;
1038
1039int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
Mark Brownb026ddb2012-05-31 21:01:46 +01001040 int irq_base, const struct regmap_irq_chip *chip,
Mark Brownf8beab22011-10-28 23:50:49 +02001041 struct regmap_irq_chip_data **data);
1042void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
Laxman Dewangan045b9842016-02-10 14:29:50 +05301043
1044int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
1045 int irq_flags, int irq_base,
1046 const struct regmap_irq_chip *chip,
1047 struct regmap_irq_chip_data **data);
1048void devm_regmap_del_irq_chip(struct device *dev, int irq,
1049 struct regmap_irq_chip_data *data);
1050
Mark Brown209a6002011-12-05 16:10:15 +00001051int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
Mark Brown4af8be62012-05-13 10:59:56 +01001052int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
Mark Brown90f790d2012-08-20 21:45:05 +01001053struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
Mark Brownb83a3132011-05-11 19:59:58 +02001054
Mark Brown9cde5fc2012-02-17 14:49:51 -08001055#else
1056
1057/*
1058 * These stubs should only ever be called by generic code which has
1059 * regmap based facilities, if they ever get called at runtime
1060 * something is going wrong and something probably needs to select
1061 * REGMAP.
1062 */
1063
1064static inline int regmap_write(struct regmap *map, unsigned int reg,
1065 unsigned int val)
1066{
1067 WARN_ONCE(1, "regmap API is disabled");
1068 return -EINVAL;
1069}
1070
Mark Brown915f4412013-10-09 13:30:10 +01001071static inline int regmap_write_async(struct regmap *map, unsigned int reg,
1072 unsigned int val)
1073{
1074 WARN_ONCE(1, "regmap API is disabled");
1075 return -EINVAL;
1076}
1077
Mark Brown9cde5fc2012-02-17 14:49:51 -08001078static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
1079 const void *val, size_t val_len)
1080{
1081 WARN_ONCE(1, "regmap API is disabled");
1082 return -EINVAL;
1083}
1084
Mark Brown0d509f22013-01-27 22:07:38 +08001085static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1086 const void *val, size_t val_len)
1087{
1088 WARN_ONCE(1, "regmap API is disabled");
1089 return -EINVAL;
1090}
1091
Mark Brown9cde5fc2012-02-17 14:49:51 -08001092static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
1093 const void *val, size_t val_count)
1094{
1095 WARN_ONCE(1, "regmap API is disabled");
1096 return -EINVAL;
1097}
1098
1099static inline int regmap_read(struct regmap *map, unsigned int reg,
1100 unsigned int *val)
1101{
1102 WARN_ONCE(1, "regmap API is disabled");
1103 return -EINVAL;
1104}
1105
1106static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
1107 void *val, size_t val_len)
1108{
1109 WARN_ONCE(1, "regmap API is disabled");
1110 return -EINVAL;
1111}
1112
1113static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
1114 void *val, size_t val_count)
1115{
1116 WARN_ONCE(1, "regmap API is disabled");
1117 return -EINVAL;
1118}
1119
Kuninori Morimoto91d31b92016-02-15 05:22:18 +00001120static inline int regmap_update_bits_base(struct regmap *map, unsigned int reg,
1121 unsigned int mask, unsigned int val,
1122 bool *change, bool async, bool force)
Mark Brown9cde5fc2012-02-17 14:49:51 -08001123{
1124 WARN_ONCE(1, "regmap API is disabled");
1125 return -EINVAL;
1126}
1127
Kuninori Morimoto28972ea2016-02-15 05:23:55 +00001128static inline int regmap_field_update_bits_base(struct regmap_field *field,
1129 unsigned int mask, unsigned int val,
1130 bool *change, bool async, bool force)
Kuninori Morimotofd4b7286c2015-06-16 08:52:39 +00001131{
1132 WARN_ONCE(1, "regmap API is disabled");
1133 return -EINVAL;
1134}
1135
Kuninori Morimotoe126ede2016-02-15 05:24:51 +00001136static inline int regmap_fields_update_bits_base(struct regmap_field *field,
1137 unsigned int id,
1138 unsigned int mask, unsigned int val,
1139 bool *change, bool async, bool force)
Mark Brown915f4412013-10-09 13:30:10 +01001140{
1141 WARN_ONCE(1, "regmap API is disabled");
1142 return -EINVAL;
1143}
1144
Mark Brown9cde5fc2012-02-17 14:49:51 -08001145static inline int regmap_get_val_bytes(struct regmap *map)
1146{
1147 WARN_ONCE(1, "regmap API is disabled");
1148 return -EINVAL;
1149}
1150
Srinivas Kandagatla668abc72015-05-21 17:42:43 +01001151static inline int regmap_get_max_register(struct regmap *map)
1152{
1153 WARN_ONCE(1, "regmap API is disabled");
1154 return -EINVAL;
1155}
1156
Srinivas Kandagatlaa2f776c2015-05-21 17:42:54 +01001157static inline int regmap_get_reg_stride(struct regmap *map)
1158{
1159 WARN_ONCE(1, "regmap API is disabled");
1160 return -EINVAL;
1161}
1162
Mark Brown9cde5fc2012-02-17 14:49:51 -08001163static inline int regcache_sync(struct regmap *map)
1164{
1165 WARN_ONCE(1, "regmap API is disabled");
1166 return -EINVAL;
1167}
1168
Mark Browna313f9f2012-02-23 19:49:43 +00001169static inline int regcache_sync_region(struct regmap *map, unsigned int min,
1170 unsigned int max)
1171{
1172 WARN_ONCE(1, "regmap API is disabled");
1173 return -EINVAL;
1174}
1175
Mark Brown697e85b2013-05-08 13:55:22 +01001176static inline int regcache_drop_region(struct regmap *map, unsigned int min,
1177 unsigned int max)
1178{
1179 WARN_ONCE(1, "regmap API is disabled");
1180 return -EINVAL;
1181}
1182
Mark Brown9cde5fc2012-02-17 14:49:51 -08001183static inline void regcache_cache_only(struct regmap *map, bool enable)
1184{
1185 WARN_ONCE(1, "regmap API is disabled");
1186}
1187
1188static inline void regcache_cache_bypass(struct regmap *map, bool enable)
1189{
1190 WARN_ONCE(1, "regmap API is disabled");
1191}
1192
1193static inline void regcache_mark_dirty(struct regmap *map)
1194{
1195 WARN_ONCE(1, "regmap API is disabled");
1196}
1197
Mark Brown0d509f22013-01-27 22:07:38 +08001198static inline void regmap_async_complete(struct regmap *map)
1199{
1200 WARN_ONCE(1, "regmap API is disabled");
1201}
1202
Mark Brown9cde5fc2012-02-17 14:49:51 -08001203static inline int regmap_register_patch(struct regmap *map,
Daniel Wagnera6baa3d2015-11-30 16:20:15 +01001204 const struct reg_sequence *regs,
Mark Brown9cde5fc2012-02-17 14:49:51 -08001205 int num_regs)
1206{
1207 WARN_ONCE(1, "regmap API is disabled");
1208 return -EINVAL;
1209}
1210
Nenghua Cao13ff50c2014-02-19 18:44:13 +08001211static inline int regmap_parse_val(struct regmap *map, const void *buf,
1212 unsigned int *val)
1213{
1214 WARN_ONCE(1, "regmap API is disabled");
1215 return -EINVAL;
1216}
1217
Mark Brown72b39f62012-05-08 17:44:40 +01001218static inline struct regmap *dev_get_regmap(struct device *dev,
1219 const char *name)
1220{
Mark Brown72b39f62012-05-08 17:44:40 +01001221 return NULL;
1222}
1223
Tuomas Tynkkynen8d7d3972014-07-21 18:38:47 +03001224static inline struct device *regmap_get_device(struct regmap *map)
1225{
1226 WARN_ONCE(1, "regmap API is disabled");
Mark Brown1d33dc62014-07-25 19:01:53 +01001227 return NULL;
Tuomas Tynkkynen8d7d3972014-07-21 18:38:47 +03001228}
1229
Mark Brown9cde5fc2012-02-17 14:49:51 -08001230#endif
1231
Mark Brownb83a3132011-05-11 19:59:58 +02001232#endif