1 /* 2 * Common library for ADIS16XXX devices 3 * 4 * Copyright 2012 Analog Devices Inc. 5 * Author: Lars-Peter Clausen <[email protected]> 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 */ 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 */ 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 */ 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 */ 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 */ 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, info_all, 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 .info_mask_shared_by_all = info_all, \ 168 .address = (addr), \ 169 .scan_index = (si), \ 170 .scan_type = { \ 171 .sign = 'u', \ 172 .realbits = (bits), \ 173 .storagebits = 16, \ 174 .endianness = IIO_BE, \ 175 }, \ 176 } 177 178 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \ 179 ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits) 180 181 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \ 182 ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits) 183 184 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \ 185 .type = IIO_TEMP, \ 186 .indexed = 1, \ 187 .channel = 0, \ 188 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 189 BIT(IIO_CHAN_INFO_SCALE) | \ 190 BIT(IIO_CHAN_INFO_OFFSET), \ 191 .info_mask_shared_by_all = info_all, \ 192 .address = (addr), \ 193 .scan_index = (si), \ 194 .scan_type = { \ 195 .sign = 'u', \ 196 .realbits = (bits), \ 197 .storagebits = 16, \ 198 .endianness = IIO_BE, \ 199 }, \ 200 } 201 202 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \ 203 .type = (_type), \ 204 .modified = 1, \ 205 .channel2 = IIO_MOD_ ## mod, \ 206 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 207 info_sep, \ 208 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 209 .info_mask_shared_by_all = info_all, \ 210 .address = (addr), \ 211 .scan_index = (si), \ 212 .scan_type = { \ 213 .sign = 's', \ 214 .realbits = (bits), \ 215 .storagebits = 16, \ 216 .endianness = IIO_BE, \ 217 }, \ 218 } 219 220 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \ 221 ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits) 222 223 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \ 224 ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits) 225 226 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \ 227 ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits) 228 229 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \ 230 ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits) 231 232 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER 233 234 int adis_setup_buffer_and_trigger(struct adis *adis, 235 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *)); 236 void adis_cleanup_buffer_and_trigger(struct adis *adis, 237 struct iio_dev *indio_dev); 238 239 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev); 240 void adis_remove_trigger(struct adis *adis); 241 242 int adis_update_scan_mode(struct iio_dev *indio_dev, 243 const unsigned long *scan_mask); 244 245 #else /* CONFIG_IIO_BUFFER */ 246 247 static inline int adis_setup_buffer_and_trigger(struct adis *adis, 248 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *)) 249 { 250 return 0; 251 } 252 253 static inline void adis_cleanup_buffer_and_trigger(struct adis *adis, 254 struct iio_dev *indio_dev) 255 { 256 } 257 258 static inline int adis_probe_trigger(struct adis *adis, 259 struct iio_dev *indio_dev) 260 { 261 return 0; 262 } 263 264 static inline void adis_remove_trigger(struct adis *adis) 265 { 266 } 267 268 #define adis_update_scan_mode NULL 269 270 #endif /* CONFIG_IIO_BUFFER */ 271 272 #ifdef CONFIG_DEBUG_FS 273 274 int adis_debugfs_reg_access(struct iio_dev *indio_dev, 275 unsigned int reg, unsigned int writeval, unsigned int *readval); 276 277 #else 278 279 #define adis_debugfs_reg_access NULL 280 281 #endif 282 283 #endif 284