Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 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 | |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 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 |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 22 | |
Lars-Peter Clausen | 2f3abe6 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 23 | struct adis; |
| 24 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 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; |
Lars-Peter Clausen | af8a412 | 2016-04-15 16:59:38 +0200 | [diff] [blame] | 44 | bool self_test_no_autoclear; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 45 | unsigned int startup_delay; |
| 46 | |
| 47 | const char * const *status_error_msgs; |
| 48 | unsigned int status_error_mask; |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 49 | |
Lars-Peter Clausen | 2f3abe6 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 50 | int (*enable_irq)(struct adis *adis, bool enable); |
| 51 | |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 52 | bool has_paging; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct 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 Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 64 | unsigned int current_page; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 65 | void *buffer; |
| 66 | |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 67 | uint8_t tx[10] ____cacheline_aligned; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 68 | uint8_t rx[4]; |
| 69 | }; |
| 70 | |
| 71 | int adis_init(struct adis *adis, struct iio_dev *indio_dev, |
| 72 | struct spi_device *spi, const struct adis_data *data); |
| 73 | int adis_reset(struct adis *adis); |
| 74 | |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 75 | int adis_write_reg(struct adis *adis, unsigned int reg, |
| 76 | unsigned int val, unsigned int size); |
| 77 | int 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 | */ |
| 86 | static 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 | */ |
| 98 | static 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 | */ |
| 110 | static 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 | */ |
| 122 | static 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 | */ |
| 140 | static 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 Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 151 | |
| 152 | int adis_enable_irq(struct adis *adis, bool enable); |
| 153 | int adis_check_status(struct adis *adis); |
| 154 | |
| 155 | int adis_initial_startup(struct adis *adis); |
| 156 | |
| 157 | int adis_single_conversion(struct iio_dev *indio_dev, |
| 158 | const struct iio_chan_spec *chan, unsigned int error_mask, |
| 159 | int *val); |
| 160 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 161 | #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 162 | .type = IIO_VOLTAGE, \ |
| 163 | .indexed = 1, \ |
| 164 | .channel = (chan), \ |
| 165 | .extend_name = name, \ |
Jonathan Cameron | b841f8a | 2013-02-27 19:35:55 +0000 | [diff] [blame] | 166 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 167 | BIT(IIO_CHAN_INFO_SCALE), \ |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 168 | .info_mask_shared_by_all = info_all, \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 169 | .address = (addr), \ |
| 170 | .scan_index = (si), \ |
| 171 | .scan_type = { \ |
| 172 | .sign = 'u', \ |
| 173 | .realbits = (bits), \ |
| 174 | .storagebits = 16, \ |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 175 | .endianness = IIO_BE, \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 176 | }, \ |
| 177 | } |
| 178 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 179 | #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \ |
| 180 | ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 181 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 182 | #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \ |
| 183 | ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 184 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 185 | #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 186 | .type = IIO_TEMP, \ |
| 187 | .indexed = 1, \ |
| 188 | .channel = 0, \ |
Jonathan Cameron | b841f8a | 2013-02-27 19:35:55 +0000 | [diff] [blame] | 189 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 190 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 191 | BIT(IIO_CHAN_INFO_OFFSET), \ |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 192 | .info_mask_shared_by_all = info_all, \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 193 | .address = (addr), \ |
| 194 | .scan_index = (si), \ |
| 195 | .scan_type = { \ |
| 196 | .sign = 'u', \ |
| 197 | .realbits = (bits), \ |
| 198 | .storagebits = 16, \ |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 199 | .endianness = IIO_BE, \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 200 | }, \ |
| 201 | } |
| 202 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 203 | #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 204 | .type = (_type), \ |
| 205 | .modified = 1, \ |
| 206 | .channel2 = IIO_MOD_ ## mod, \ |
Jonathan Cameron | b841f8a | 2013-02-27 19:35:55 +0000 | [diff] [blame] | 207 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 208 | info_sep, \ |
| 209 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 210 | .info_mask_shared_by_all = info_all, \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 211 | .address = (addr), \ |
| 212 | .scan_index = (si), \ |
| 213 | .scan_type = { \ |
| 214 | .sign = 's', \ |
| 215 | .realbits = (bits), \ |
| 216 | .storagebits = 16, \ |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 217 | .endianness = IIO_BE, \ |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 218 | }, \ |
| 219 | } |
| 220 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 221 | #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 Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 223 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 224 | #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 Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 226 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 227 | #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 Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 229 | |
Jonathan Cameron | 82695ef | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 230 | #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 Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 232 | |
| 233 | #ifdef CONFIG_IIO_ADIS_LIB_BUFFER |
| 234 | |
| 235 | int adis_setup_buffer_and_trigger(struct adis *adis, |
| 236 | struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *)); |
| 237 | void adis_cleanup_buffer_and_trigger(struct adis *adis, |
| 238 | struct iio_dev *indio_dev); |
| 239 | |
| 240 | int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev); |
| 241 | void adis_remove_trigger(struct adis *adis); |
| 242 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 243 | int adis_update_scan_mode(struct iio_dev *indio_dev, |
| 244 | const unsigned long *scan_mask); |
| 245 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 246 | #else /* CONFIG_IIO_BUFFER */ |
| 247 | |
| 248 | static 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 | |
| 254 | static inline void adis_cleanup_buffer_and_trigger(struct adis *adis, |
| 255 | struct iio_dev *indio_dev) |
| 256 | { |
| 257 | } |
| 258 | |
| 259 | static inline int adis_probe_trigger(struct adis *adis, |
| 260 | struct iio_dev *indio_dev) |
| 261 | { |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static inline void adis_remove_trigger(struct adis *adis) |
| 266 | { |
| 267 | } |
| 268 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 269 | #define adis_update_scan_mode NULL |
| 270 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 271 | #endif /* CONFIG_IIO_BUFFER */ |
| 272 | |
Lars-Peter Clausen | 78026a6 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 273 | #ifdef CONFIG_DEBUG_FS |
| 274 | |
| 275 | int 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 Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 284 | #endif |