Merge tag 'tty-5.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[linux-2.6-microblaze.git] / include / linux / iio / imu / adis.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Common library for ADIS16XXX devices
4  *
5  * Copyright 2012 Analog Devices Inc.
6  *   Author: Lars-Peter Clausen <lars@metafoo.de>
7  */
8
9 #ifndef __IIO_ADIS_H__
10 #define __IIO_ADIS_H__
11
12 #include <linux/spi/spi.h>
13 #include <linux/interrupt.h>
14 #include <linux/iio/types.h>
15
16 #define ADIS_WRITE_REG(reg) ((0x80 | (reg)))
17 #define ADIS_READ_REG(reg) ((reg) & 0x7f)
18
19 #define ADIS_PAGE_SIZE 0x80
20 #define ADIS_REG_PAGE_ID 0x00
21
22 struct adis;
23 struct adis_burst;
24
25 /**
26  * struct adis_data - ADIS chip variant specific data
27  * @read_delay: SPI delay for read operations in us
28  * @write_delay: SPI delay for write operations in us
29  * @cs_change_delay: SPI delay between CS changes in us
30  * @glob_cmd_reg: Register address of the GLOB_CMD register
31  * @msc_ctrl_reg: Register address of the MSC_CTRL register
32  * @diag_stat_reg: Register address of the DIAG_STAT register
33  * @status_error_msgs: Array of error messgaes
34  * @status_error_mask:
35  */
36 struct adis_data {
37         unsigned int read_delay;
38         unsigned int write_delay;
39         unsigned int cs_change_delay;
40
41         unsigned int glob_cmd_reg;
42         unsigned int msc_ctrl_reg;
43         unsigned int diag_stat_reg;
44
45         unsigned int self_test_mask;
46         bool self_test_no_autoclear;
47         unsigned int startup_delay;
48
49         const char * const *status_error_msgs;
50         unsigned int status_error_mask;
51
52         int (*enable_irq)(struct adis *adis, bool enable);
53
54         bool has_paging;
55 };
56
57 struct adis {
58         struct spi_device       *spi;
59         struct iio_trigger      *trig;
60
61         const struct adis_data  *data;
62         struct adis_burst       *burst;
63
64         struct mutex            txrx_lock;
65         struct spi_message      msg;
66         struct spi_transfer     *xfer;
67         unsigned int            current_page;
68         void                    *buffer;
69
70         uint8_t                 tx[10] ____cacheline_aligned;
71         uint8_t                 rx[4];
72 };
73
74 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
75         struct spi_device *spi, const struct adis_data *data);
76 int adis_reset(struct adis *adis);
77
78 int adis_write_reg(struct adis *adis, unsigned int reg,
79         unsigned int val, unsigned int size);
80 int adis_read_reg(struct adis *adis, unsigned int reg,
81         unsigned int *val, unsigned int size);
82
83 /**
84  * adis_write_reg_8() - Write single byte to a register
85  * @adis: The adis device
86  * @reg: The address of the register to be written
87  * @value: The value to write
88  */
89 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
90         uint8_t val)
91 {
92         return adis_write_reg(adis, reg, val, 1);
93 }
94
95 /**
96  * adis_write_reg_16() - Write 2 bytes to a pair of registers
97  * @adis: The adis device
98  * @reg: The address of the lower of the two registers
99  * @value: Value to be written
100  */
101 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
102         uint16_t val)
103 {
104         return adis_write_reg(adis, reg, val, 2);
105 }
106
107 /**
108  * adis_write_reg_32() - write 4 bytes to four registers
109  * @adis: The adis device
110  * @reg: The address of the lower of the four register
111  * @value: Value to be written
112  */
113 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
114         uint32_t val)
115 {
116         return adis_write_reg(adis, reg, val, 4);
117 }
118
119 /**
120  * adis_read_reg_16() - read 2 bytes from a 16-bit register
121  * @adis: The adis device
122  * @reg: The address of the lower of the two registers
123  * @val: The value read back from the device
124  */
125 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
126         uint16_t *val)
127 {
128         unsigned int tmp;
129         int ret;
130
131         ret = adis_read_reg(adis, reg, &tmp, 2);
132         if (ret == 0)
133                 *val = tmp;
134
135         return ret;
136 }
137
138 /**
139  * adis_read_reg_32() - read 4 bytes from a 32-bit register
140  * @adis: The adis device
141  * @reg: The address of the lower of the two registers
142  * @val: The value read back from the device
143  */
144 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
145         uint32_t *val)
146 {
147         unsigned int tmp;
148         int ret;
149
150         ret = adis_read_reg(adis, reg, &tmp, 4);
151         if (ret == 0)
152                 *val = tmp;
153
154         return ret;
155 }
156
157 int adis_enable_irq(struct adis *adis, bool enable);
158 int adis_check_status(struct adis *adis);
159
160 int adis_initial_startup(struct adis *adis);
161
162 int adis_single_conversion(struct iio_dev *indio_dev,
163         const struct iio_chan_spec *chan, unsigned int error_mask,
164         int *val);
165
166 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
167         .type = IIO_VOLTAGE, \
168         .indexed = 1, \
169         .channel = (chan), \
170         .extend_name = name, \
171         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
172                 BIT(IIO_CHAN_INFO_SCALE), \
173         .info_mask_shared_by_all = info_all, \
174         .address = (addr), \
175         .scan_index = (si), \
176         .scan_type = { \
177                 .sign = 'u', \
178                 .realbits = (bits), \
179                 .storagebits = 16, \
180                 .endianness = IIO_BE, \
181         }, \
182 }
183
184 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
185         ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
186
187 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
188         ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
189
190 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
191         .type = IIO_TEMP, \
192         .indexed = 1, \
193         .channel = 0, \
194         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
195                 BIT(IIO_CHAN_INFO_SCALE) | \
196                 BIT(IIO_CHAN_INFO_OFFSET), \
197         .info_mask_shared_by_all = info_all, \
198         .address = (addr), \
199         .scan_index = (si), \
200         .scan_type = { \
201                 .sign = 'u', \
202                 .realbits = (bits), \
203                 .storagebits = 16, \
204                 .endianness = IIO_BE, \
205         }, \
206 }
207
208 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
209         .type = (_type), \
210         .modified = 1, \
211         .channel2 = IIO_MOD_ ## mod, \
212         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
213                  info_sep, \
214         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
215         .info_mask_shared_by_all = info_all, \
216         .address = (addr), \
217         .scan_index = (si), \
218         .scan_type = { \
219                 .sign = 's', \
220                 .realbits = (bits), \
221                 .storagebits = 16, \
222                 .endianness = IIO_BE, \
223         }, \
224 }
225
226 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
227         ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
228
229 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits)         \
230         ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
231
232 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
233         ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
234
235 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
236         ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
237
238 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
239
240 /**
241  * struct adis_burst - ADIS data for burst transfers
242  * @en                  burst mode enabled
243  * @reg_cmd             register command that triggers burst
244  * @extra_len           extra length to account in the SPI RX buffer
245  */
246 struct adis_burst {
247         bool            en;
248         unsigned int    reg_cmd;
249         unsigned int    extra_len;
250 };
251
252 int adis_setup_buffer_and_trigger(struct adis *adis,
253         struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
254 void adis_cleanup_buffer_and_trigger(struct adis *adis,
255         struct iio_dev *indio_dev);
256
257 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
258 void adis_remove_trigger(struct adis *adis);
259
260 int adis_update_scan_mode(struct iio_dev *indio_dev,
261         const unsigned long *scan_mask);
262
263 #else /* CONFIG_IIO_BUFFER */
264
265 static inline int adis_setup_buffer_and_trigger(struct adis *adis,
266         struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
267 {
268         return 0;
269 }
270
271 static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
272         struct iio_dev *indio_dev)
273 {
274 }
275
276 static inline int adis_probe_trigger(struct adis *adis,
277         struct iio_dev *indio_dev)
278 {
279         return 0;
280 }
281
282 static inline void adis_remove_trigger(struct adis *adis)
283 {
284 }
285
286 #define adis_update_scan_mode NULL
287
288 #endif /* CONFIG_IIO_BUFFER */
289
290 #ifdef CONFIG_DEBUG_FS
291
292 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
293         unsigned int reg, unsigned int writeval, unsigned int *readval);
294
295 #else
296
297 #define adis_debugfs_reg_access NULL
298
299 #endif
300
301 #endif