Merge 5.5-rc6 into staging-next
[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            state_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 /**
79  * adis_reset() - Reset the device
80  * @adis: The adis device
81  *
82  * Returns 0 on success, a negative error code otherwise
83  */
84 static inline int adis_reset(struct adis *adis)
85 {
86         int ret;
87
88         mutex_lock(&adis->state_lock);
89         ret = __adis_reset(adis);
90         mutex_unlock(&adis->state_lock);
91
92         return ret;
93 }
94
95 int __adis_write_reg(struct adis *adis, unsigned int reg,
96         unsigned int val, unsigned int size);
97 int __adis_read_reg(struct adis *adis, unsigned int reg,
98         unsigned int *val, unsigned int size);
99
100 /**
101  * __adis_write_reg_8() - Write single byte to a register (unlocked)
102  * @adis: The adis device
103  * @reg: The address of the register to be written
104  * @value: The value to write
105  */
106 static inline int __adis_write_reg_8(struct adis *adis, unsigned int reg,
107         uint8_t val)
108 {
109         return __adis_write_reg(adis, reg, val, 1);
110 }
111
112 /**
113  * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
114  * @adis: The adis device
115  * @reg: The address of the lower of the two registers
116  * @value: Value to be written
117  */
118 static inline int __adis_write_reg_16(struct adis *adis, unsigned int reg,
119         uint16_t val)
120 {
121         return __adis_write_reg(adis, reg, val, 2);
122 }
123
124 /**
125  * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
126  * @adis: The adis device
127  * @reg: The address of the lower of the four register
128  * @value: Value to be written
129  */
130 static inline int __adis_write_reg_32(struct adis *adis, unsigned int reg,
131         uint32_t val)
132 {
133         return __adis_write_reg(adis, reg, val, 4);
134 }
135
136 /**
137  * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
138  * @adis: The adis device
139  * @reg: The address of the lower of the two registers
140  * @val: The value read back from the device
141  */
142 static inline int __adis_read_reg_16(struct adis *adis, unsigned int reg,
143         uint16_t *val)
144 {
145         unsigned int tmp;
146         int ret;
147
148         ret = __adis_read_reg(adis, reg, &tmp, 2);
149         if (ret == 0)
150                 *val = tmp;
151
152         return ret;
153 }
154
155 /**
156  * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
157  * @adis: The adis device
158  * @reg: The address of the lower of the two registers
159  * @val: The value read back from the device
160  */
161 static inline int __adis_read_reg_32(struct adis *adis, unsigned int reg,
162         uint32_t *val)
163 {
164         unsigned int tmp;
165         int ret;
166
167         ret = __adis_read_reg(adis, reg, &tmp, 4);
168         if (ret == 0)
169                 *val = tmp;
170
171         return ret;
172 }
173
174 /**
175  * adis_write_reg() - write N bytes to register
176  * @adis: The adis device
177  * @reg: The address of the lower of the two registers
178  * @value: The value to write to device (up to 4 bytes)
179  * @size: The size of the @value (in bytes)
180  */
181 static inline int adis_write_reg(struct adis *adis, unsigned int reg,
182         unsigned int val, unsigned int size)
183 {
184         int ret;
185
186         mutex_lock(&adis->state_lock);
187         ret = __adis_write_reg(adis, reg, val, size);
188         mutex_unlock(&adis->state_lock);
189
190         return ret;
191 }
192
193 /**
194  * adis_read_reg() - read N bytes from register
195  * @adis: The adis device
196  * @reg: The address of the lower of the two registers
197  * @val: The value read back from the device
198  * @size: The size of the @val buffer
199  */
200 static int adis_read_reg(struct adis *adis, unsigned int reg,
201         unsigned int *val, unsigned int size)
202 {
203         int ret;
204
205         mutex_lock(&adis->state_lock);
206         ret = __adis_read_reg(adis, reg, val, size);
207         mutex_unlock(&adis->state_lock);
208
209         return ret;
210 }
211
212 /**
213  * adis_write_reg_8() - Write single byte to a register
214  * @adis: The adis device
215  * @reg: The address of the register to be written
216  * @value: The value to write
217  */
218 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
219         uint8_t val)
220 {
221         return adis_write_reg(adis, reg, val, 1);
222 }
223
224 /**
225  * adis_write_reg_16() - Write 2 bytes to a pair of registers
226  * @adis: The adis device
227  * @reg: The address of the lower of the two registers
228  * @value: Value to be written
229  */
230 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
231         uint16_t val)
232 {
233         return adis_write_reg(adis, reg, val, 2);
234 }
235
236 /**
237  * adis_write_reg_32() - write 4 bytes to four registers
238  * @adis: The adis device
239  * @reg: The address of the lower of the four register
240  * @value: Value to be written
241  */
242 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
243         uint32_t val)
244 {
245         return adis_write_reg(adis, reg, val, 4);
246 }
247
248 /**
249  * adis_read_reg_16() - read 2 bytes from a 16-bit register
250  * @adis: The adis device
251  * @reg: The address of the lower of the two registers
252  * @val: The value read back from the device
253  */
254 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
255         uint16_t *val)
256 {
257         unsigned int tmp;
258         int ret;
259
260         ret = adis_read_reg(adis, reg, &tmp, 2);
261         if (ret == 0)
262                 *val = tmp;
263
264         return ret;
265 }
266
267 /**
268  * adis_read_reg_32() - read 4 bytes from a 32-bit register
269  * @adis: The adis device
270  * @reg: The address of the lower of the two registers
271  * @val: The value read back from the device
272  */
273 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
274         uint32_t *val)
275 {
276         unsigned int tmp;
277         int ret;
278
279         ret = adis_read_reg(adis, reg, &tmp, 4);
280         if (ret == 0)
281                 *val = tmp;
282
283         return ret;
284 }
285
286 int adis_enable_irq(struct adis *adis, bool enable);
287 int __adis_check_status(struct adis *adis);
288
289 static inline int adis_check_status(struct adis *adis)
290 {
291         int ret;
292
293         mutex_lock(&adis->state_lock);
294         ret = __adis_check_status(adis);
295         mutex_unlock(&adis->state_lock);
296
297         return ret;
298 }
299
300 int adis_initial_startup(struct adis *adis);
301
302 int adis_single_conversion(struct iio_dev *indio_dev,
303         const struct iio_chan_spec *chan, unsigned int error_mask,
304         int *val);
305
306 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
307         .type = IIO_VOLTAGE, \
308         .indexed = 1, \
309         .channel = (chan), \
310         .extend_name = name, \
311         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
312                 BIT(IIO_CHAN_INFO_SCALE), \
313         .info_mask_shared_by_all = info_all, \
314         .address = (addr), \
315         .scan_index = (si), \
316         .scan_type = { \
317                 .sign = 'u', \
318                 .realbits = (bits), \
319                 .storagebits = 16, \
320                 .endianness = IIO_BE, \
321         }, \
322 }
323
324 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
325         ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
326
327 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
328         ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
329
330 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
331         .type = IIO_TEMP, \
332         .indexed = 1, \
333         .channel = 0, \
334         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
335                 BIT(IIO_CHAN_INFO_SCALE) | \
336                 BIT(IIO_CHAN_INFO_OFFSET), \
337         .info_mask_shared_by_all = info_all, \
338         .address = (addr), \
339         .scan_index = (si), \
340         .scan_type = { \
341                 .sign = 'u', \
342                 .realbits = (bits), \
343                 .storagebits = 16, \
344                 .endianness = IIO_BE, \
345         }, \
346 }
347
348 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
349         .type = (_type), \
350         .modified = 1, \
351         .channel2 = IIO_MOD_ ## mod, \
352         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
353                  info_sep, \
354         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
355         .info_mask_shared_by_all = info_all, \
356         .address = (addr), \
357         .scan_index = (si), \
358         .scan_type = { \
359                 .sign = 's', \
360                 .realbits = (bits), \
361                 .storagebits = 16, \
362                 .endianness = IIO_BE, \
363         }, \
364 }
365
366 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
367         ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
368
369 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits)         \
370         ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
371
372 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
373         ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
374
375 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
376         ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
377
378 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
379
380 /**
381  * struct adis_burst - ADIS data for burst transfers
382  * @en                  burst mode enabled
383  * @reg_cmd             register command that triggers burst
384  * @extra_len           extra length to account in the SPI RX buffer
385  */
386 struct adis_burst {
387         bool            en;
388         unsigned int    reg_cmd;
389         unsigned int    extra_len;
390 };
391
392 int adis_setup_buffer_and_trigger(struct adis *adis,
393         struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
394 void adis_cleanup_buffer_and_trigger(struct adis *adis,
395         struct iio_dev *indio_dev);
396
397 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
398 void adis_remove_trigger(struct adis *adis);
399
400 int adis_update_scan_mode(struct iio_dev *indio_dev,
401         const unsigned long *scan_mask);
402
403 #else /* CONFIG_IIO_BUFFER */
404
405 static inline int adis_setup_buffer_and_trigger(struct adis *adis,
406         struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
407 {
408         return 0;
409 }
410
411 static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
412         struct iio_dev *indio_dev)
413 {
414 }
415
416 static inline int adis_probe_trigger(struct adis *adis,
417         struct iio_dev *indio_dev)
418 {
419         return 0;
420 }
421
422 static inline void adis_remove_trigger(struct adis *adis)
423 {
424 }
425
426 #define adis_update_scan_mode NULL
427
428 #endif /* CONFIG_IIO_BUFFER */
429
430 #ifdef CONFIG_DEBUG_FS
431
432 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
433         unsigned int reg, unsigned int writeval, unsigned int *readval);
434
435 #else
436
437 #define adis_debugfs_reg_access NULL
438
439 #endif
440
441 #endif