1/* 2 * sca3000.c -- support VTI sca3000 series accelerometers 3 * via SPI 4 * 5 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org> 6 * 7 * Partly based upon tle62x0.c 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 * 13 * Initial mode is direct measurement. 14 * 15 * Untested things 16 * 17 * Temperature reading (the e05 I'm testing with doesn't have a sensor) 18 * 19 * Free fall detection mode - supported but untested as I'm not droping my 20 * dubious wire rig far enough to test it. 21 * 22 * Unsupported as yet 23 * 24 * Time stamping of data from ring. Various ideas on how to do this but none 25 * are remotely simple. Suggestions welcome. 26 * 27 * Individual enabling disabling of channels going into ring buffer 28 * 29 * Overflow handling (this is signaled for all but 8 bit ring buffer mode.) 30 * 31 * Motion detector using AND combinations of signals. 32 * 33 * Note: Be very careful about not touching an register bytes marked 34 * as reserved on the data sheet. They really mean it as changing convents of 35 * some will cause the device to lock up. 36 * 37 * Known issues - on rare occasions the interrupts lock up. Not sure why as yet. 38 * Can probably alleviate this by reading the interrupt register on start, but 39 * that is really just brushing the problem under the carpet. 40 */ 41#ifndef _SCA3000 42#define _SCA3000 43 44#define SCA3000_WRITE_REG(a) (((a) << 2) | 0x02) 45#define SCA3000_READ_REG(a) ((a) << 2) 46 47#define SCA3000_REG_ADDR_REVID 0x00 48#define SCA3000_REVID_MAJOR_MASK 0xf0 49#define SCA3000_REVID_MINOR_MASK 0x0f 50 51#define SCA3000_REG_ADDR_STATUS 0x02 52#define SCA3000_LOCKED 0x20 53#define SCA3000_EEPROM_CS_ERROR 0x02 54#define SCA3000_SPI_FRAME_ERROR 0x01 55 56/* All reads done using register decrement so no need to directly access LSBs */ 57#define SCA3000_REG_ADDR_X_MSB 0x05 58#define SCA3000_REG_ADDR_Y_MSB 0x07 59#define SCA3000_REG_ADDR_Z_MSB 0x09 60 61#define SCA3000_REG_ADDR_RING_OUT 0x0f 62 63/* Temp read untested - the e05 doesn't have the sensor */ 64#define SCA3000_REG_ADDR_TEMP_MSB 0x13 65 66#define SCA3000_REG_ADDR_MODE 0x14 67#define SCA3000_MODE_PROT_MASK 0x28 68 69#define SCA3000_RING_BUF_ENABLE 0x80 70#define SCA3000_RING_BUF_8BIT 0x40 71/* 72 * Free fall detection triggers an interrupt if the acceleration 73 * is below a threshold for equivalent of 25cm drop 74 */ 75#define SCA3000_FREE_FALL_DETECT 0x10 76#define SCA3000_MEAS_MODE_NORMAL 0x00 77#define SCA3000_MEAS_MODE_OP_1 0x01 78#define SCA3000_MEAS_MODE_OP_2 0x02 79 80/* 81 * In motion detection mode the accelerations are band pass filtered 82 * (approx 1 - 25Hz) and then a programmable threshold used to trigger 83 * and interrupt. 84 */ 85#define SCA3000_MEAS_MODE_MOT_DET 0x03 86 87#define SCA3000_REG_ADDR_BUF_COUNT 0x15 88 89#define SCA3000_REG_ADDR_INT_STATUS 0x16 90 91#define SCA3000_INT_STATUS_THREE_QUARTERS 0x80 92#define SCA3000_INT_STATUS_HALF 0x40 93 94#define SCA3000_INT_STATUS_FREE_FALL 0x08 95#define SCA3000_INT_STATUS_Y_TRIGGER 0x04 96#define SCA3000_INT_STATUS_X_TRIGGER 0x02 97#define SCA3000_INT_STATUS_Z_TRIGGER 0x01 98 99/* Used to allow access to multiplexed registers */ 100#define SCA3000_REG_ADDR_CTRL_SEL 0x18 101/* Only available for SCA3000-D03 and SCA3000-D01 */ 102#define SCA3000_REG_CTRL_SEL_I2C_DISABLE 0x01 103#define SCA3000_REG_CTRL_SEL_MD_CTRL 0x02 104#define SCA3000_REG_CTRL_SEL_MD_Y_TH 0x03 105#define SCA3000_REG_CTRL_SEL_MD_X_TH 0x04 106#define SCA3000_REG_CTRL_SEL_MD_Z_TH 0x05 107/* 108 * BE VERY CAREFUL WITH THIS, IF 3 BITS ARE NOT SET the device 109 * will not function 110 */ 111#define SCA3000_REG_CTRL_SEL_OUT_CTRL 0x0B 112#define SCA3000_OUT_CTRL_PROT_MASK 0xE0 113#define SCA3000_OUT_CTRL_BUF_X_EN 0x10 114#define SCA3000_OUT_CTRL_BUF_Y_EN 0x08 115#define SCA3000_OUT_CTRL_BUF_Z_EN 0x04 116#define SCA3000_OUT_CTRL_BUF_DIV_4 0x02 117#define SCA3000_OUT_CTRL_BUF_DIV_2 0x01 118 119/* 120 * Control which motion detector interrupts are on. 121 * For now only OR combinations are supported. 122 */ 123#define SCA3000_MD_CTRL_PROT_MASK 0xC0 124#define SCA3000_MD_CTRL_OR_Y 0x01 125#define SCA3000_MD_CTRL_OR_X 0x02 126#define SCA3000_MD_CTRL_OR_Z 0x04 127/* Currently unsupported */ 128#define SCA3000_MD_CTRL_AND_Y 0x08 129#define SCA3000_MD_CTRL_AND_X 0x10 130#define SAC3000_MD_CTRL_AND_Z 0x20 131 132/* 133 * Some control registers of complex access methods requiring this register to 134 * be used to remove a lock. 135 */ 136#define SCA3000_REG_ADDR_UNLOCK 0x1e 137 138#define SCA3000_REG_ADDR_INT_MASK 0x21 139#define SCA3000_INT_MASK_PROT_MASK 0x1C 140 141#define SCA3000_INT_MASK_RING_THREE_QUARTER 0x80 142#define SCA3000_INT_MASK_RING_HALF 0x40 143 144#define SCA3000_INT_MASK_ALL_INTS 0x02 145#define SCA3000_INT_MASK_ACTIVE_HIGH 0x01 146#define SCA3000_INT_MASK_ACTIVE_LOW 0x00 147 148/* Values of multiplexed registers (write to ctrl_data after select) */ 149#define SCA3000_REG_ADDR_CTRL_DATA 0x22 150 151/* 152 * Measurement modes available on some sca3000 series chips. Code assumes others 153 * may become available in the future. 154 * 155 * Bypass - Bypass the low-pass filter in the signal channel so as to increase 156 * signal bandwidth. 157 * 158 * Narrow - Narrow low-pass filtering of the signal channel and half output 159 * data rate by decimation. 160 * 161 * Wide - Widen low-pass filtering of signal channel to increase bandwidth 162 */ 163#define SCA3000_OP_MODE_BYPASS 0x01 164#define SCA3000_OP_MODE_NARROW 0x02 165#define SCA3000_OP_MODE_WIDE 0x04 166#define SCA3000_MAX_TX 6 167#define SCA3000_MAX_RX 2 168 169/** 170 * struct sca3000_state - device instance state information 171 * @us: the associated spi device 172 * @info: chip variant information 173 * @interrupt_handler_ws: event interrupt handler for all events 174 * @last_timestamp: the timestamp of the last event 175 * @mo_det_use_count: reference counter for the motion detection unit 176 * @lock: lock used to protect elements of sca3000_state 177 * and the underlying device state. 178 * @bpse: number of bits per scan element 179 * @tx: dma-able transmit buffer 180 * @rx: dma-able receive buffer 181 **/ 182struct sca3000_state { 183 struct spi_device *us; 184 const struct sca3000_chip_info *info; 185 struct work_struct interrupt_handler_ws; 186 s64 last_timestamp; 187 int mo_det_use_count; 188 struct mutex lock; 189 int bpse; 190 /* Can these share a cacheline ? */ 191 u8 rx[2] ____cacheline_aligned; 192 u8 tx[6] ____cacheline_aligned; 193}; 194 195/** 196 * struct sca3000_chip_info - model dependent parameters 197 * @scale: scale * 10^-6 198 * @temp_output: some devices have temperature sensors. 199 * @measurement_mode_freq: normal mode sampling frequency 200 * @option_mode_1: first optional mode. Not all models have one 201 * @option_mode_1_freq: option mode 1 sampling frequency 202 * @option_mode_2: second optional mode. Not all chips have one 203 * @option_mode_2_freq: option mode 2 sampling frequency 204 * 205 * This structure is used to hold information about the functionality of a given 206 * sca3000 variant. 207 **/ 208struct sca3000_chip_info { 209 unsigned int scale; 210 bool temp_output; 211 int measurement_mode_freq; 212 int option_mode_1; 213 int option_mode_1_freq; 214 int option_mode_2; 215 int option_mode_2_freq; 216 int mot_det_mult_xz[6]; 217 int mot_det_mult_y[7]; 218}; 219 220int sca3000_read_data_short(struct sca3000_state *st, 221 u8 reg_address_high, 222 int len); 223 224/** 225 * sca3000_write_reg() write a single register 226 * @address: address of register on chip 227 * @val: value to be written to register 228 * 229 * The main lock must be held. 230 **/ 231int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val); 232 233#ifdef CONFIG_IIO_BUFFER 234/** 235 * sca3000_register_ring_funcs() setup the ring state change functions 236 **/ 237void sca3000_register_ring_funcs(struct iio_dev *indio_dev); 238 239/** 240 * sca3000_configure_ring() - allocate and configure ring buffer 241 * @indio_dev: iio-core device whose ring is to be configured 242 * 243 * The hardware ring buffer needs far fewer ring buffer functions than 244 * a software one as a lot of things are handled automatically. 245 * This function also tells the iio core that our device supports a 246 * hardware ring buffer mode. 247 **/ 248int sca3000_configure_ring(struct iio_dev *indio_dev); 249 250/** 251 * sca3000_unconfigure_ring() - deallocate the ring buffer 252 * @indio_dev: iio-core device whose ring we are freeing 253 **/ 254void sca3000_unconfigure_ring(struct iio_dev *indio_dev); 255 256/** 257 * sca3000_ring_int_process() handles ring related event pushing and escalation 258 * @val: the event code 259 **/ 260void sca3000_ring_int_process(u8 val, struct iio_buffer *ring); 261 262#else 263static inline void sca3000_register_ring_funcs(struct iio_dev *indio_dev) 264{ 265} 266 267static inline 268int sca3000_register_ring_access_and_init(struct iio_dev *indio_dev) 269{ 270 return 0; 271} 272 273static inline void sca3000_ring_int_process(u8 val, void *ring) 274{ 275} 276 277#endif 278#endif /* _SCA3000 */ 279