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