Merge tag 'staging-5.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[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_timeouts - ADIS chip variant timeouts
27  * @reset_ms - Wait time after rst pin goes inactive
28  * @sw_reset_ms - Wait time after sw reset command
29  * @self_test_ms - Wait time after self test command
30  */
31 struct adis_timeout {
32         u16 reset_ms;
33         u16 sw_reset_ms;
34         u16 self_test_ms;
35 };
36 /**
37  * struct adis_data - ADIS chip variant specific data
38  * @read_delay: SPI delay for read operations in us
39  * @write_delay: SPI delay for write operations in us
40  * @cs_change_delay: SPI delay between CS changes in us
41  * @glob_cmd_reg: Register address of the GLOB_CMD register
42  * @msc_ctrl_reg: Register address of the MSC_CTRL register
43  * @diag_stat_reg: Register address of the DIAG_STAT register
44  * @status_error_msgs: Array of error messgaes
45  * @status_error_mask:
46  * @timeouts: Chip specific delays
47  */
48 struct adis_data {
49         unsigned int read_delay;
50         unsigned int write_delay;
51         unsigned int cs_change_delay;
52
53         unsigned int glob_cmd_reg;
54         unsigned int msc_ctrl_reg;
55         unsigned int diag_stat_reg;
56
57         unsigned int self_test_mask;
58         bool self_test_no_autoclear;
59         const struct adis_timeout *timeouts;
60
61         const char * const *status_error_msgs;
62         unsigned int status_error_mask;
63
64         int (*enable_irq)(struct adis *adis, bool enable);
65
66         bool has_paging;
67 };
68
69 struct adis {
70         struct spi_device       *spi;
71         struct iio_trigger      *trig;
72
73         const struct adis_data  *data;
74         struct adis_burst       *burst;
75
76         struct mutex            state_lock;
77         struct spi_message      msg;
78         struct spi_transfer     *xfer;
79         unsigned int            current_page;
80         void                    *buffer;
81
82         uint8_t                 tx[10] ____cacheline_aligned;
83         uint8_t                 rx[4];
84 };
85
86 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
87         struct spi_device *spi, const struct adis_data *data);
88 int __adis_reset(struct adis *adis);
89
90 /**
91  * adis_reset() - Reset the device
92  * @adis: The adis device
93  *
94  * Returns 0 on success, a negative error code otherwise
95  */
96 static inline int adis_reset(struct adis *adis)
97 {
98         int ret;
99
100         mutex_lock(&adis->state_lock);
101         ret = __adis_reset(adis);
102         mutex_unlock(&adis->state_lock);
103
104         return ret;
105 }
106
107 int __adis_write_reg(struct adis *adis, unsigned int reg,
108         unsigned int val, unsigned int size);
109 int __adis_read_reg(struct adis *adis, unsigned int reg,
110         unsigned int *val, unsigned int size);
111
112 /**
113  * __adis_write_reg_8() - Write single byte to a register (unlocked)
114  * @adis: The adis device
115  * @reg: The address of the register to be written
116  * @value: The value to write
117  */
118 static inline int __adis_write_reg_8(struct adis *adis, unsigned int reg,
119         uint8_t val)
120 {
121         return __adis_write_reg(adis, reg, val, 1);
122 }
123
124 /**
125  * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
126  * @adis: The adis device
127  * @reg: The address of the lower of the two registers
128  * @value: Value to be written
129  */
130 static inline int __adis_write_reg_16(struct adis *adis, unsigned int reg,
131         uint16_t val)
132 {
133         return __adis_write_reg(adis, reg, val, 2);
134 }
135
136 /**
137  * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
138  * @adis: The adis device
139  * @reg: The address of the lower of the four register
140  * @value: Value to be written
141  */
142 static inline int __adis_write_reg_32(struct adis *adis, unsigned int reg,
143         uint32_t val)
144 {
145         return __adis_write_reg(adis, reg, val, 4);
146 }
147
148 /**
149  * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
150  * @adis: The adis device
151  * @reg: The address of the lower of the two registers
152  * @val: The value read back from the device
153  */
154 static inline int __adis_read_reg_16(struct adis *adis, unsigned int reg,
155         uint16_t *val)
156 {
157         unsigned int tmp;
158         int ret;
159
160         ret = __adis_read_reg(adis, reg, &tmp, 2);
161         if (ret == 0)
162                 *val = tmp;
163
164         return ret;
165 }
166
167 /**
168  * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
169  * @adis: The adis device
170  * @reg: The address of the lower of the two registers
171  * @val: The value read back from the device
172  */
173 static inline int __adis_read_reg_32(struct adis *adis, unsigned int reg,
174         uint32_t *val)
175 {
176         unsigned int tmp;
177         int ret;
178
179         ret = __adis_read_reg(adis, reg, &tmp, 4);
180         if (ret == 0)
181                 *val = tmp;
182
183         return ret;
184 }
185
186 /**
187  * adis_write_reg() - write N bytes to register
188  * @adis: The adis device
189  * @reg: The address of the lower of the two registers
190  * @value: The value to write to device (up to 4 bytes)
191  * @size: The size of the @value (in bytes)
192  */
193 static inline int adis_write_reg(struct adis *adis, unsigned int reg,
194         unsigned int val, unsigned int size)
195 {
196         int ret;
197
198         mutex_lock(&adis->state_lock);
199         ret = __adis_write_reg(adis, reg, val, size);
200         mutex_unlock(&adis->state_lock);
201
202         return ret;
203 }
204
205 /**
206  * adis_read_reg() - read N bytes from register
207  * @adis: The adis device
208  * @reg: The address of the lower of the two registers
209  * @val: The value read back from the device
210  * @size: The size of the @val buffer
211  */
212 static int adis_read_reg(struct adis *adis, unsigned int reg,
213         unsigned int *val, unsigned int size)
214 {
215         int ret;
216
217         mutex_lock(&adis->state_lock);
218         ret = __adis_read_reg(adis, reg, val, size);
219         mutex_unlock(&adis->state_lock);
220
221         return ret;
222 }
223
224 /**
225  * adis_write_reg_8() - Write single byte to a register
226  * @adis: The adis device
227  * @reg: The address of the register to be written
228  * @value: The value to write
229  */
230 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
231         uint8_t val)
232 {
233         return adis_write_reg(adis, reg, val, 1);
234 }
235
236 /**
237  * adis_write_reg_16() - Write 2 bytes to a pair of registers
238  * @adis: The adis device
239  * @reg: The address of the lower of the two registers
240  * @value: Value to be written
241  */
242 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
243         uint16_t val)
244 {
245         return adis_write_reg(adis, reg, val, 2);
246 }
247
248 /**
249  * adis_write_reg_32() - write 4 bytes to four registers
250  * @adis: The adis device
251  * @reg: The address of the lower of the four register
252  * @value: Value to be written
253  */
254 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
255         uint32_t val)
256 {
257         return adis_write_reg(adis, reg, val, 4);
258 }
259
260 /**
261  * adis_read_reg_16() - read 2 bytes from a 16-bit register
262  * @adis: The adis device
263  * @reg: The address of the lower of the two registers
264  * @val: The value read back from the device
265  */
266 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
267         uint16_t *val)
268 {
269         unsigned int tmp;
270         int ret;
271
272         ret = adis_read_reg(adis, reg, &tmp, 2);
273         if (ret == 0)
274                 *val = tmp;
275
276         return ret;
277 }
278
279 /**
280  * adis_read_reg_32() - read 4 bytes from a 32-bit register
281  * @adis: The adis device
282  * @reg: The address of the lower of the two registers
283  * @val: The value read back from the device
284  */
285 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
286         uint32_t *val)
287 {
288         unsigned int tmp;
289         int ret;
290
291         ret = adis_read_reg(adis, reg, &tmp, 4);
292         if (ret == 0)
293                 *val = tmp;
294
295         return ret;
296 }
297
298 int adis_enable_irq(struct adis *adis, bool enable);
299 int __adis_check_status(struct adis *adis);
300
301 static inline int adis_check_status(struct adis *adis)
302 {
303         int ret;
304
305         mutex_lock(&adis->state_lock);
306         ret = __adis_check_status(adis);
307         mutex_unlock(&adis->state_lock);
308
309         return ret;
310 }
311
312 int adis_initial_startup(struct adis *adis);
313
314 int adis_single_conversion(struct iio_dev *indio_dev,
315         const struct iio_chan_spec *chan, unsigned int error_mask,
316         int *val);
317
318 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
319         .type = IIO_VOLTAGE, \
320         .indexed = 1, \
321         .channel = (chan), \
322         .extend_name = name, \
323         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
324                 BIT(IIO_CHAN_INFO_SCALE), \
325         .info_mask_shared_by_all = info_all, \
326         .address = (addr), \
327         .scan_index = (si), \
328         .scan_type = { \
329                 .sign = 'u', \
330                 .realbits = (bits), \
331                 .storagebits = 16, \
332                 .endianness = IIO_BE, \
333         }, \
334 }
335
336 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
337         ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
338
339 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
340         ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
341
342 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
343         .type = IIO_TEMP, \
344         .indexed = 1, \
345         .channel = 0, \
346         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
347                 BIT(IIO_CHAN_INFO_SCALE) | \
348                 BIT(IIO_CHAN_INFO_OFFSET), \
349         .info_mask_shared_by_all = info_all, \
350         .address = (addr), \
351         .scan_index = (si), \
352         .scan_type = { \
353                 .sign = 'u', \
354                 .realbits = (bits), \
355                 .storagebits = 16, \
356                 .endianness = IIO_BE, \
357         }, \
358 }
359
360 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
361         .type = (_type), \
362         .modified = 1, \
363         .channel2 = IIO_MOD_ ## mod, \
364         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
365                  info_sep, \
366         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
367         .info_mask_shared_by_all = info_all, \
368         .address = (addr), \
369         .scan_index = (si), \
370         .scan_type = { \
371                 .sign = 's', \
372                 .realbits = (bits), \
373                 .storagebits = 16, \
374                 .endianness = IIO_BE, \
375         }, \
376 }
377
378 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
379         ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
380
381 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits)         \
382         ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
383
384 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
385         ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
386
387 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
388         ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
389
390 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
391
392 /**
393  * struct adis_burst - ADIS data for burst transfers
394  * @en                  burst mode enabled
395  * @reg_cmd             register command that triggers burst
396  * @extra_len           extra length to account in the SPI RX buffer
397  */
398 struct adis_burst {
399         bool            en;
400         unsigned int    reg_cmd;
401         unsigned int    extra_len;
402 };
403
404 int adis_setup_buffer_and_trigger(struct adis *adis,
405         struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
406 void adis_cleanup_buffer_and_trigger(struct adis *adis,
407         struct iio_dev *indio_dev);
408
409 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
410 void adis_remove_trigger(struct adis *adis);
411
412 int adis_update_scan_mode(struct iio_dev *indio_dev,
413         const unsigned long *scan_mask);
414
415 #else /* CONFIG_IIO_BUFFER */
416
417 static inline int adis_setup_buffer_and_trigger(struct adis *adis,
418         struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
419 {
420         return 0;
421 }
422
423 static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
424         struct iio_dev *indio_dev)
425 {
426 }
427
428 static inline int adis_probe_trigger(struct adis *adis,
429         struct iio_dev *indio_dev)
430 {
431         return 0;
432 }
433
434 static inline void adis_remove_trigger(struct adis *adis)
435 {
436 }
437
438 #define adis_update_scan_mode NULL
439
440 #endif /* CONFIG_IIO_BUFFER */
441
442 #ifdef CONFIG_DEBUG_FS
443
444 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
445         unsigned int reg, unsigned int writeval, unsigned int *readval);
446
447 #else
448
449 #define adis_debugfs_reg_access NULL
450
451 #endif
452
453 #endif