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