blob: 360da7d18a3d011e630bc0fff1790485150ca78b [file] [log] [blame]
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +00001/*
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
Lars-Peter Clausen484a0bf2012-11-20 13:36:00 +000017#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
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +000022
Lars-Peter Clausen2f3abe62012-11-20 13:36:00 +000023struct adis;
24
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +000025/**
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 */
35struct 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;
Lars-Peter Clausenaf8a4122016-04-15 16:59:38 +020044 bool self_test_no_autoclear;
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +000045 unsigned int startup_delay;
46
47 const char * const *status_error_msgs;
48 unsigned int status_error_mask;
Lars-Peter Clausen484a0bf2012-11-20 13:36:00 +000049
Lars-Peter Clausen2f3abe62012-11-20 13:36:00 +000050 int (*enable_irq)(struct adis *adis, bool enable);
51
Lars-Peter Clausen484a0bf2012-11-20 13:36:00 +000052 bool has_paging;
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +000053};
54
55struct adis {
56 struct spi_device *spi;
57 struct iio_trigger *trig;
58
59 const struct adis_data *data;
60
61 struct mutex txrx_lock;
62 struct spi_message msg;
63 struct spi_transfer *xfer;
Lars-Peter Clausen484a0bf2012-11-20 13:36:00 +000064 unsigned int current_page;
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +000065 void *buffer;
66
Lars-Peter Clausen57a12282012-11-20 13:36:00 +000067 uint8_t tx[10] ____cacheline_aligned;
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +000068 uint8_t rx[4];
69};
70
71int adis_init(struct adis *adis, struct iio_dev *indio_dev,
72 struct spi_device *spi, const struct adis_data *data);
73int adis_reset(struct adis *adis);
74
Lars-Peter Clausen57a12282012-11-20 13:36:00 +000075int adis_write_reg(struct adis *adis, unsigned int reg,
76 unsigned int val, unsigned int size);
77int adis_read_reg(struct adis *adis, unsigned int reg,
78 unsigned int *val, unsigned int size);
79
80/**
81 * adis_write_reg_8() - Write single byte to a register
82 * @adis: The adis device
83 * @reg: The address of the register to be written
84 * @value: The value to write
85 */
86static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
87 uint8_t val)
88{
89 return adis_write_reg(adis, reg, val, 1);
90}
91
92/**
93 * adis_write_reg_16() - Write 2 bytes to a pair of registers
94 * @adis: The adis device
95 * @reg: The address of the lower of the two registers
96 * @value: Value to be written
97 */
98static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
99 uint16_t val)
100{
101 return adis_write_reg(adis, reg, val, 2);
102}
103
104/**
105 * adis_write_reg_32() - write 4 bytes to four registers
106 * @adis: The adis device
107 * @reg: The address of the lower of the four register
108 * @value: Value to be written
109 */
110static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
111 uint32_t val)
112{
113 return adis_write_reg(adis, reg, val, 4);
114}
115
116/**
117 * adis_read_reg_16() - read 2 bytes from a 16-bit register
118 * @adis: The adis device
119 * @reg: The address of the lower of the two registers
120 * @val: The value read back from the device
121 */
122static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
123 uint16_t *val)
124{
125 unsigned int tmp;
126 int ret;
127
128 ret = adis_read_reg(adis, reg, &tmp, 2);
129 *val = tmp;
130
131 return ret;
132}
133
134/**
135 * adis_read_reg_32() - read 4 bytes from a 32-bit register
136 * @adis: The adis device
137 * @reg: The address of the lower of the two registers
138 * @val: The value read back from the device
139 */
140static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
141 uint32_t *val)
142{
143 unsigned int tmp;
144 int ret;
145
146 ret = adis_read_reg(adis, reg, &tmp, 4);
147 *val = tmp;
148
149 return ret;
150}
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000151
152int adis_enable_irq(struct adis *adis, bool enable);
153int adis_check_status(struct adis *adis);
154
155int adis_initial_startup(struct adis *adis);
156
157int adis_single_conversion(struct iio_dev *indio_dev,
158 const struct iio_chan_spec *chan, unsigned int error_mask,
159 int *val);
160
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100161#define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000162 .type = IIO_VOLTAGE, \
163 .indexed = 1, \
164 .channel = (chan), \
165 .extend_name = name, \
Jonathan Cameronb841f8a2013-02-27 19:35:55 +0000166 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
167 BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100168 .info_mask_shared_by_all = info_all, \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000169 .address = (addr), \
170 .scan_index = (si), \
171 .scan_type = { \
172 .sign = 'u', \
173 .realbits = (bits), \
174 .storagebits = 16, \
Lars-Peter Clausenaacff892012-11-13 13:28:00 +0000175 .endianness = IIO_BE, \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000176 }, \
177}
178
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100179#define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
180 ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000181
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100182#define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
183 ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000184
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100185#define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000186 .type = IIO_TEMP, \
187 .indexed = 1, \
188 .channel = 0, \
Jonathan Cameronb841f8a2013-02-27 19:35:55 +0000189 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
190 BIT(IIO_CHAN_INFO_SCALE) | \
191 BIT(IIO_CHAN_INFO_OFFSET), \
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100192 .info_mask_shared_by_all = info_all, \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000193 .address = (addr), \
194 .scan_index = (si), \
195 .scan_type = { \
196 .sign = 'u', \
197 .realbits = (bits), \
198 .storagebits = 16, \
Lars-Peter Clausenaacff892012-11-13 13:28:00 +0000199 .endianness = IIO_BE, \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000200 }, \
201}
202
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100203#define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000204 .type = (_type), \
205 .modified = 1, \
206 .channel2 = IIO_MOD_ ## mod, \
Jonathan Cameronb841f8a2013-02-27 19:35:55 +0000207 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
208 info_sep, \
209 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100210 .info_mask_shared_by_all = info_all, \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000211 .address = (addr), \
212 .scan_index = (si), \
213 .scan_type = { \
214 .sign = 's', \
215 .realbits = (bits), \
216 .storagebits = 16, \
Lars-Peter Clausenaacff892012-11-13 13:28:00 +0000217 .endianness = IIO_BE, \
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000218 }, \
219}
220
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100221#define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
222 ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000223
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100224#define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \
225 ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000226
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100227#define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
228 ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000229
Jonathan Cameron82695ef2014-06-22 20:59:00 +0100230#define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
231 ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000232
233#ifdef CONFIG_IIO_ADIS_LIB_BUFFER
234
235int adis_setup_buffer_and_trigger(struct adis *adis,
236 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
237void adis_cleanup_buffer_and_trigger(struct adis *adis,
238 struct iio_dev *indio_dev);
239
240int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
241void adis_remove_trigger(struct adis *adis);
242
Lars-Peter Clausenaacff892012-11-13 13:28:00 +0000243int adis_update_scan_mode(struct iio_dev *indio_dev,
244 const unsigned long *scan_mask);
245
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000246#else /* CONFIG_IIO_BUFFER */
247
248static inline int adis_setup_buffer_and_trigger(struct adis *adis,
249 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
250{
251 return 0;
252}
253
254static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
255 struct iio_dev *indio_dev)
256{
257}
258
259static inline int adis_probe_trigger(struct adis *adis,
260 struct iio_dev *indio_dev)
261{
262 return 0;
263}
264
265static inline void adis_remove_trigger(struct adis *adis)
266{
267}
268
Lars-Peter Clausenaacff892012-11-13 13:28:00 +0000269#define adis_update_scan_mode NULL
270
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000271#endif /* CONFIG_IIO_BUFFER */
272
Lars-Peter Clausen78026a62012-11-20 13:36:00 +0000273#ifdef CONFIG_DEBUG_FS
274
275int adis_debugfs_reg_access(struct iio_dev *indio_dev,
276 unsigned int reg, unsigned int writeval, unsigned int *readval);
277
278#else
279
280#define adis_debugfs_reg_access NULL
281
282#endif
283
Lars-Peter Clausenccd2b522012-11-13 13:28:00 +0000284#endif