Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Staging: IIO: VTI sca3000 series accelerometer driver (spi)

Example of how a device with a hardware ring buffer is
handled within IIO.

Changes since V2:
* Moved to new registration functions giving much cleaner
interface.

Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Jonathan Cameron and committed by
Greg Kroah-Hartman
574fb258 7026ea4b

+2171
+8
drivers/staging/iio/accel/Kconfig
··· 17 17 Say yes here to build SPI support for the ST microelectronics 18 18 accelerometer. The driver supplies direct access via sysfs files 19 19 and an event interface via a character device. 20 + 21 + config SCA3000 22 + depends on IIO_RING_BUFFER 23 + depends on SPI 24 + tristate "VTI SCA3000 series accelerometers" 25 + help 26 + Say yes here to build support for the VTI SCA3000 series of SPI 27 + accelerometers. These devices use a hardware ring buffer.
+3
drivers/staging/iio/accel/Makefile
··· 5 5 6 6 lis3l02dq-y := lis3l02dq_core.o 7 7 obj-$(CONFIG_LIS3L02DQ) += lis3l02dq.o 8 + 9 + sca3000-y := sca3000_core.o sca3000_ring.o 10 + obj-$(CONFIG_SCA3000) += sca3000.o
+298
drivers/staging/iio/accel/sca3000.h
··· 1 + /* 2 + * sca3000.c -- support VTI sca3000 series accelerometers 3 + * via SPI 4 + * 5 + * Copyright (c) 2007 Jonathan Cameron <jic23@cam.ac.uk> 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 + #define SCA3000_WRITE_REG(a) (((a) << 2) | 0x02) 42 + #define SCA3000_READ_REG(a) ((a) << 2) 43 + 44 + #define SCA3000_REG_ADDR_REVID 0x00 45 + #define SCA3000_REVID_MAJOR_MASK 0xf0 46 + #define SCA3000_REVID_MINOR_MASK 0x0f 47 + 48 + #define SCA3000_REG_ADDR_STATUS 0x02 49 + #define SCA3000_LOCKED 0x20 50 + #define SCA3000_EEPROM_CS_ERROR 0x02 51 + #define SCA3000_SPI_FRAME_ERROR 0x01 52 + 53 + /* All reads done using register decrement so no need to directly access LSBs */ 54 + #define SCA3000_REG_ADDR_X_MSB 0x05 55 + #define SCA3000_REG_ADDR_Y_MSB 0x07 56 + #define SCA3000_REG_ADDR_Z_MSB 0x09 57 + 58 + #define SCA3000_REG_ADDR_RING_OUT 0x0f 59 + 60 + /* Temp read untested - the e05 doesn't have the sensor */ 61 + #define SCA3000_REG_ADDR_TEMP_MSB 0x13 62 + 63 + #define SCA3000_REG_ADDR_MODE 0x14 64 + #define SCA3000_MODE_PROT_MASK 0x28 65 + 66 + #define SCA3000_RING_BUF_ENABLE 0x80 67 + #define SCA3000_RING_BUF_8BIT 0x40 68 + /* Free fall detection triggers an interrupt if the acceleration 69 + * is below a threshold for equivalent of 25cm drop 70 + */ 71 + #define SCA3000_FREE_FALL_DETECT 0x10 72 + #define SCA3000_MEAS_MODE_NORMAL 0x00 73 + #define SCA3000_MEAS_MODE_OP_1 0x01 74 + #define SCA3000_MEAS_MODE_OP_2 0x02 75 + 76 + /* In motion detection mode the accelerations are band pass filtered 77 + * (aprox 1 - 25Hz) and then a programmable theshold used to trigger 78 + * and interrupt. 79 + */ 80 + #define SCA3000_MEAS_MODE_MOT_DET 0x03 81 + 82 + #define SCA3000_REG_ADDR_BUF_COUNT 0x15 83 + 84 + #define SCA3000_REG_ADDR_INT_STATUS 0x16 85 + 86 + #define SCA3000_INT_STATUS_THREE_QUARTERS 0x80 87 + #define SCA3000_INT_STATUS_HALF 0x40 88 + 89 + #define SCA3000_INT_STATUS_FREE_FALL 0x08 90 + #define SCA3000_INT_STATUS_Y_TRIGGER 0x04 91 + #define SCA3000_INT_STATUS_X_TRIGGER 0x02 92 + #define SCA3000_INT_STATUS_Z_TRIGGER 0x01 93 + 94 + /* Used to allow accesss to multiplexed registers */ 95 + #define SCA3000_REG_ADDR_CTRL_SEL 0x18 96 + /* Only available for SCA3000-D03 and SCA3000-D01 */ 97 + #define SCA3000_REG_CTRL_SEL_I2C_DISABLE 0x01 98 + #define SCA3000_REG_CTRL_SEL_MD_CTRL 0x02 99 + #define SCA3000_REG_CTRL_SEL_MD_Y_TH 0x03 100 + #define SCA3000_REG_CTRL_SEL_MD_X_TH 0x04 101 + #define SCA3000_REG_CTRL_SEL_MD_Z_TH 0x05 102 + /* BE VERY CAREFUL WITH THIS, IF 3 BITS ARE NOT SET the device 103 + will not function */ 104 + #define SCA3000_REG_CTRL_SEL_OUT_CTRL 0x0B 105 + #define SCA3000_OUT_CTRL_PROT_MASK 0xE0 106 + #define SCA3000_OUT_CTRL_BUF_X_EN 0x10 107 + #define SCA3000_OUT_CTRL_BUF_Y_EN 0x08 108 + #define SCA3000_OUT_CTRL_BUF_Z_EN 0x04 109 + #define SCA3000_OUT_CTRL_BUF_DIV_4 0x02 110 + #define SCA3000_OUT_CTRL_BUF_DIV_2 0x01 111 + 112 + /* Control which motion detector interrupts are on. 113 + * For now only OR combinations are supported.x 114 + */ 115 + #define SCA3000_MD_CTRL_PROT_MASK 0xC0 116 + #define SCA3000_MD_CTRL_OR_Y 0x01 117 + #define SCA3000_MD_CTRL_OR_X 0x02 118 + #define SCA3000_MD_CTRL_OR_Z 0x04 119 + /* Currently unsupported */ 120 + #define SCA3000_MD_CTRL_AND_Y 0x08 121 + #define SCA3000_MD_CTRL_AND_X 0x10 122 + #define SAC3000_MD_CTRL_AND_Z 0x20 123 + 124 + /* Some control registers of complex access methods requiring this register to 125 + * be used to remove a lock. 126 + */ 127 + #define SCA3000_REG_ADDR_UNLOCK 0x1e 128 + 129 + #define SCA3000_REG_ADDR_INT_MASK 0x21 130 + #define SCA3000_INT_MASK_PROT_MASK 0x1C 131 + 132 + #define SCA3000_INT_MASK_RING_THREE_QUARTER 0x80 133 + #define SCA3000_INT_MASK_RING_HALF 0x40 134 + 135 + #define SCA3000_INT_MASK_ALL_INTS 0x02 136 + #define SCA3000_INT_MASK_ACTIVE_HIGH 0x01 137 + #define SCA3000_INT_MASK_ACTIVE_LOW 0x00 138 + 139 + /* Values of mulipexed registers (write to ctrl_data after select) */ 140 + #define SCA3000_REG_ADDR_CTRL_DATA 0x22 141 + 142 + /* Measurment modes available on some sca3000 series chips. Code assumes others 143 + * may become available in the future. 144 + * 145 + * Bypass - Bypass the low-pass filter in the signal channel so as to increase 146 + * signal bandwidth. 147 + * 148 + * Narrow - Narrow low-pass filtering of the signal channel and half output 149 + * data rate by decimation. 150 + * 151 + * Wide - Widen low-pass filtering of signal channel to increase bandwidth 152 + */ 153 + #define SCA3000_OP_MODE_BYPASS 0x01 154 + #define SCA3000_OP_MODE_NARROW 0x02 155 + #define SCA3000_OP_MODE_WIDE 0x04 156 + #define SCA3000_MAX_TX 6 157 + #define SCA3000_MAX_RX 2 158 + 159 + /** 160 + * struct sca3000_state - device instance state information 161 + * @us: the associated spi device 162 + * @info: chip variant information 163 + * @indio_dev: device information used by the IIO core 164 + * @interrupt_handler_ws: event interrupt handler for all events 165 + * @last_timestamp: the timestamp of the last event 166 + * @mo_det_use_count: reference counter for the motion detection unit 167 + * @lock: lock used to protect elements of sca3000_state 168 + * and the underlying device state. 169 + * @bpse: number of bits per scan element 170 + * @tx: dma-able transmit buffer 171 + * @rx: dma-able receive buffer 172 + **/ 173 + struct sca3000_state { 174 + struct spi_device *us; 175 + const struct sca3000_chip_info *info; 176 + struct iio_dev *indio_dev; 177 + struct work_struct interrupt_handler_ws; 178 + s64 last_timestamp; 179 + int mo_det_use_count; 180 + struct mutex lock; 181 + int bpse; 182 + u8 *tx; 183 + /* not used during a ring buffer read */ 184 + u8 *rx; 185 + }; 186 + 187 + /** 188 + * struct sca3000_chip_info - model dependant parameters 189 + * @name: model identification 190 + * @temp_output: some devices have temperature sensors. 191 + * @measurement_mode_freq: normal mode sampling frequency 192 + * @option_mode_1: first optional mode. Not all models have one 193 + * @option_mode_1_freq: option mode 1 sampling frequency 194 + * @option_mode_2: second optional mode. Not all chips have one 195 + * @option_mode_2_freq: option mode 2 sampling frequency 196 + * 197 + * This structure is used to hold information about the functionality of a given 198 + * sca3000 variant. 199 + **/ 200 + struct sca3000_chip_info { 201 + const char *name; 202 + bool temp_output; 203 + int measurement_mode_freq; 204 + int option_mode_1; 205 + int option_mode_1_freq; 206 + int option_mode_2; 207 + int option_mode_2_freq; 208 + }; 209 + 210 + /** 211 + * sca3000_read_data() read a series of values from the device 212 + * @dev: device 213 + * @reg_address_high: start address (decremented read) 214 + * @rx: pointer where recieved data is placed. Callee 215 + * responsible for freeing this. 216 + * @len: number of bytes to read 217 + * 218 + * The main lock must be held. 219 + **/ 220 + int sca3000_read_data(struct sca3000_state *st, 221 + u8 reg_address_high, 222 + u8 **rx_p, 223 + int len); 224 + 225 + /** 226 + * sca3000_write_reg() write a single register 227 + * @address: address of register on chip 228 + * @val: value to be written to register 229 + * 230 + * The main lock must be held. 231 + **/ 232 + int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val); 233 + 234 + /* Conversion function for use with the ring buffer when in 11bit mode */ 235 + static inline int sca3000_11bit_convert(uint8_t msb, uint8_t lsb) 236 + { 237 + int16_t val; 238 + 239 + val = ((lsb >> 3) & 0x1C) | (msb << 5); 240 + val |= (val & (1 << 12)) ? 0xE000 : 0; 241 + 242 + return val; 243 + }; 244 + 245 + static inline int sca3000_13bit_convert(uint8_t msb, uint8_t lsb) 246 + { 247 + s16 val; 248 + 249 + val = ((lsb >> 3) & 0x1F) | (msb << 5); 250 + /* sign fill */ 251 + val |= (val & (1 << 12)) ? 0xE000 : 0; 252 + 253 + return val; 254 + }; 255 + 256 + 257 + #ifdef CONFIG_IIO_RING_BUFFER 258 + /** 259 + * sca3000_register_ring_funcs() setup the ring state change functions 260 + **/ 261 + void sca3000_register_ring_funcs(struct iio_dev *indio_dev); 262 + 263 + /** 264 + * sca3000_configure_ring() - allocate and configure ring buffer 265 + * @indio_dev: iio-core device whose ring is to be configured 266 + * 267 + * The hardware ring buffer needs far fewer ring buffer functions than 268 + * a software one as a lot of things are handled automatically. 269 + * This function also tells the iio core that our device supports a 270 + * hardware ring buffer mode. 271 + **/ 272 + int sca3000_configure_ring(struct iio_dev *indio_dev); 273 + 274 + /** 275 + * sca3000_unconfigure_ring() - deallocate the ring buffer 276 + * @indio_dev: iio-core device whose ring we are freeing 277 + **/ 278 + void sca3000_unconfigure_ring(struct iio_dev *indio_dev); 279 + 280 + /** 281 + * sca3000_ring_int_process() handles ring related event pushing and escalation 282 + * @val: the event code 283 + **/ 284 + void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring); 285 + 286 + #else 287 + static inline void sca3000_register_ring_funcs(struct iio_dev *indio_dev) {}; 288 + 289 + static inline 290 + int sca3000_register_ring_access_and_init(struct iio_dev *indio_dev) 291 + { 292 + return 0; 293 + }; 294 + 295 + static inline void sca3000_ring_int_process(u8 val, void *ring) {}; 296 + 297 + #endif 298 +
+1509
drivers/staging/iio/accel/sca3000_core.c
··· 1 + /* 2 + * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI 3 + * 4 + * This program is free software; you can redistribute it and/or modify it 5 + * under the terms of the GNU General Public License version 2 as published by 6 + * the Free Software Foundation. 7 + * 8 + * Copyright (c) 2009 Jonathan Cameron <jic23@cam.ac.uk> 9 + * 10 + * See industrialio/accels/sca3000.h for comments. 11 + */ 12 + 13 + #include <linux/interrupt.h> 14 + #include <linux/gpio.h> 15 + #include <linux/fs.h> 16 + #include <linux/device.h> 17 + #include <linux/kernel.h> 18 + #include <linux/spi/spi.h> 19 + #include <linux/sysfs.h> 20 + #include "../iio.h" 21 + #include "../sysfs.h" 22 + #include "../ring_generic.h" 23 + 24 + #include "accel.h" 25 + #include "sca3000.h" 26 + 27 + enum sca3000_variant { 28 + d01, 29 + d03, 30 + e02, 31 + e04, 32 + e05, 33 + l01, 34 + }; 35 + 36 + /* Note where option modes are not defined, the chip simply does not 37 + * support any. 38 + * Other chips in the sca3000 series use i2c and are not included here. 39 + * 40 + * Some of these devices are only listed in the family data sheet and 41 + * do not actually appear to be available. 42 + */ 43 + static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = { 44 + { 45 + .name = "sca3000-d01", 46 + .temp_output = true, 47 + .measurement_mode_freq = 250, 48 + .option_mode_1 = SCA3000_OP_MODE_BYPASS, 49 + .option_mode_1_freq = 250, 50 + }, { 51 + /* No data sheet available - may be the same as the 3100-d03?*/ 52 + .name = "sca3000-d03", 53 + .temp_output = true, 54 + }, { 55 + .name = "sca3000-e02", 56 + .measurement_mode_freq = 125, 57 + .option_mode_1 = SCA3000_OP_MODE_NARROW, 58 + .option_mode_1_freq = 63, 59 + }, { 60 + .name = "sca3000-e04", 61 + .measurement_mode_freq = 100, 62 + .option_mode_1 = SCA3000_OP_MODE_NARROW, 63 + .option_mode_1_freq = 50, 64 + .option_mode_2 = SCA3000_OP_MODE_WIDE, 65 + .option_mode_2_freq = 400, 66 + }, { 67 + .name = "sca3000-e05", 68 + .measurement_mode_freq = 200, 69 + .option_mode_1 = SCA3000_OP_MODE_NARROW, 70 + .option_mode_1_freq = 50, 71 + .option_mode_2 = SCA3000_OP_MODE_WIDE, 72 + .option_mode_2_freq = 400, 73 + }, { 74 + /* No data sheet available. 75 + * Frequencies are unknown. 76 + */ 77 + .name = "sca3000-l01", 78 + .temp_output = true, 79 + .option_mode_1 = SCA3000_OP_MODE_BYPASS, 80 + }, 81 + }; 82 + 83 + 84 + int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val) 85 + { 86 + struct spi_transfer xfer = { 87 + .bits_per_word = 8, 88 + .len = 2, 89 + .cs_change = 1, 90 + .tx_buf = st->tx, 91 + }; 92 + struct spi_message msg; 93 + 94 + st->tx[0] = SCA3000_WRITE_REG(address); 95 + st->tx[1] = val; 96 + spi_message_init(&msg); 97 + spi_message_add_tail(&xfer, &msg); 98 + 99 + return spi_sync(st->us, &msg); 100 + } 101 + 102 + int sca3000_read_data(struct sca3000_state *st, 103 + uint8_t reg_address_high, 104 + u8 **rx_p, 105 + int len) 106 + { 107 + int ret; 108 + struct spi_message msg; 109 + struct spi_transfer xfer = { 110 + .bits_per_word = 8, 111 + .len = len + 1, 112 + .cs_change = 1, 113 + .tx_buf = st->tx, 114 + }; 115 + 116 + *rx_p = kmalloc(len + 1, GFP_KERNEL); 117 + if (*rx_p == NULL) { 118 + ret = -ENOMEM; 119 + goto error_ret; 120 + } 121 + xfer.rx_buf = *rx_p; 122 + st->tx[0] = SCA3000_READ_REG(reg_address_high); 123 + spi_message_init(&msg); 124 + spi_message_add_tail(&xfer, &msg); 125 + 126 + ret = spi_sync(st->us, &msg); 127 + 128 + if (ret) { 129 + dev_err(get_device(&st->us->dev), "problem reading register"); 130 + goto error_free_rx; 131 + } 132 + 133 + return 0; 134 + error_free_rx: 135 + kfree(*rx_p); 136 + error_ret: 137 + return ret; 138 + 139 + } 140 + /** 141 + * sca3000_reg_lock_on() test if the ctrl register lock is on 142 + * 143 + * Lock must be held. 144 + **/ 145 + static int sca3000_reg_lock_on(struct sca3000_state *st) 146 + { 147 + u8 *rx; 148 + int ret; 149 + 150 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1); 151 + 152 + if (ret < 0) 153 + return ret; 154 + ret = !(rx[1] & SCA3000_LOCKED); 155 + kfree(rx); 156 + 157 + return ret; 158 + } 159 + 160 + /** 161 + * __sca3000_unlock_reg_lock() unlock the control registers 162 + * 163 + * Note the device does not appear to support doing this in a single transfer. 164 + * This should only ever be used as part of ctrl reg read. 165 + * Lock must be held before calling this 166 + **/ 167 + static int __sca3000_unlock_reg_lock(struct sca3000_state *st) 168 + { 169 + struct spi_message msg; 170 + struct spi_transfer xfer[3] = { 171 + { 172 + .bits_per_word = 8, 173 + .len = 2, 174 + .cs_change = 1, 175 + .tx_buf = st->tx, 176 + }, { 177 + .bits_per_word = 8, 178 + .len = 2, 179 + .cs_change = 1, 180 + .tx_buf = st->tx + 2, 181 + }, { 182 + .bits_per_word = 8, 183 + .len = 2, 184 + .cs_change = 1, 185 + .tx_buf = st->tx + 4, 186 + }, 187 + }; 188 + st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK); 189 + st->tx[1] = 0x00; 190 + st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK); 191 + st->tx[3] = 0x50; 192 + st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK); 193 + st->tx[5] = 0xA0; 194 + spi_message_init(&msg); 195 + spi_message_add_tail(&xfer[0], &msg); 196 + spi_message_add_tail(&xfer[1], &msg); 197 + spi_message_add_tail(&xfer[2], &msg); 198 + 199 + return spi_sync(st->us, &msg); 200 + } 201 + 202 + /** 203 + * sca3000_write_ctrl_reg() write to a lock protect ctrl register 204 + * @sel: selects which registers we wish to write to 205 + * @val: the value to be written 206 + * 207 + * Certain control registers are protected against overwriting by the lock 208 + * register and use a shared write address. This function allows writing of 209 + * these registers. 210 + * Lock must be held. 211 + **/ 212 + static int sca3000_write_ctrl_reg(struct sca3000_state *st, 213 + uint8_t sel, 214 + uint8_t val) 215 + { 216 + 217 + int ret; 218 + 219 + ret = sca3000_reg_lock_on(st); 220 + if (ret < 0) 221 + goto error_ret; 222 + if (ret) { 223 + ret = __sca3000_unlock_reg_lock(st); 224 + if (ret) 225 + goto error_ret; 226 + } 227 + 228 + /* Set the control select register */ 229 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel); 230 + if (ret) 231 + goto error_ret; 232 + 233 + /* Write the actual value into the register */ 234 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val); 235 + 236 + error_ret: 237 + return ret; 238 + } 239 + 240 + /* Crucial that lock is called before calling this */ 241 + /** 242 + * sca3000_read_ctrl_reg() read from lock protected control register. 243 + * 244 + * Lock must be held. 245 + **/ 246 + static int sca3000_read_ctrl_reg(struct sca3000_state *st, 247 + u8 ctrl_reg, 248 + u8 **rx_p) 249 + { 250 + int ret; 251 + 252 + ret = sca3000_reg_lock_on(st); 253 + if (ret < 0) 254 + goto error_ret; 255 + if (ret) { 256 + ret = __sca3000_unlock_reg_lock(st); 257 + if (ret) 258 + goto error_ret; 259 + } 260 + /* Set the control select register */ 261 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg); 262 + if (ret) 263 + goto error_ret; 264 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_CTRL_DATA, rx_p, 1); 265 + 266 + error_ret: 267 + return ret; 268 + } 269 + 270 + #ifdef SCA3000_DEBUG 271 + /** 272 + * sca3000_check_status() check the status register 273 + * 274 + * Only used for debugging purposes 275 + **/ 276 + static int sca3000_check_status(struct device *dev) 277 + { 278 + u8 *rx; 279 + int ret; 280 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 281 + struct sca3000_state *st = indio_dev->dev_data; 282 + 283 + mutex_lock(&st->lock); 284 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1); 285 + if (ret < 0) 286 + goto error_ret; 287 + if (rx[1] & SCA3000_EEPROM_CS_ERROR) 288 + dev_err(dev, "eeprom error \n"); 289 + if (rx[1] & SCA3000_SPI_FRAME_ERROR) 290 + dev_err(dev, "Previous SPI Frame was corrupt\n"); 291 + kfree(rx); 292 + 293 + error_ret: 294 + mutex_unlock(&st->lock); 295 + return ret; 296 + } 297 + #endif /* SCA3000_DEBUG */ 298 + 299 + /** 300 + * sca3000_read_13bit_signed() sysfs interface to read 13 bit signed registers 301 + * 302 + * These are described as signed 12 bit on the data sheet, which appears 303 + * to be a conventional 2's complement 13 bit. 304 + **/ 305 + static ssize_t sca3000_read_13bit_signed(struct device *dev, 306 + struct device_attribute *attr, 307 + char *buf) 308 + { 309 + int len = 0, ret; 310 + int val; 311 + struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 312 + u8 *rx; 313 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 314 + struct sca3000_state *st = indio_dev->dev_data; 315 + 316 + mutex_lock(&st->lock); 317 + ret = sca3000_read_data(st, this_attr->address, &rx, 2); 318 + if (ret < 0) 319 + goto error_ret; 320 + val = sca3000_13bit_convert(rx[1], rx[2]); 321 + len += sprintf(buf + len, "%d\n", val); 322 + kfree(rx); 323 + error_ret: 324 + mutex_unlock(&st->lock); 325 + 326 + return ret ? ret : len; 327 + } 328 + 329 + 330 + static ssize_t sca3000_show_name(struct device *dev, 331 + struct device_attribute *attr, 332 + char *buf) 333 + { 334 + struct iio_dev *dev_info = dev_get_drvdata(dev); 335 + struct sca3000_state *st = dev_info->dev_data; 336 + return sprintf(buf, "%s\n", st->info->name); 337 + } 338 + /** 339 + * sca3000_show_reg() - sysfs interface to read the chip revision number 340 + **/ 341 + static ssize_t sca3000_show_rev(struct device *dev, 342 + struct device_attribute *attr, 343 + char *buf) 344 + { 345 + int len = 0, ret; 346 + struct iio_dev *dev_info = dev_get_drvdata(dev); 347 + struct sca3000_state *st = dev_info->dev_data; 348 + 349 + u8 *rx; 350 + 351 + mutex_lock(&st->lock); 352 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_REVID, &rx, 1); 353 + if (ret < 0) 354 + goto error_ret; 355 + len += sprintf(buf + len, 356 + "major=%d, minor=%d\n", 357 + rx[1] & SCA3000_REVID_MAJOR_MASK, 358 + rx[1] & SCA3000_REVID_MINOR_MASK); 359 + kfree(rx); 360 + 361 + error_ret: 362 + mutex_unlock(&st->lock); 363 + 364 + return ret ? ret : len; 365 + } 366 + 367 + /** 368 + * sca3000_show_available_measurement_modes() display available modes 369 + * 370 + * This is all read from chip specific data in the driver. Not all 371 + * of the sca3000 series support modes other than normal. 372 + **/ 373 + static ssize_t 374 + sca3000_show_available_measurement_modes(struct device *dev, 375 + struct device_attribute *attr, 376 + char *buf) 377 + { 378 + struct iio_dev *dev_info = dev_get_drvdata(dev); 379 + struct sca3000_state *st = dev_info->dev_data; 380 + int len = 0; 381 + 382 + len += sprintf(buf + len, "0 - normal mode"); 383 + switch (st->info->option_mode_1) { 384 + case SCA3000_OP_MODE_NARROW: 385 + len += sprintf(buf + len, ", 1 - narrow mode"); 386 + break; 387 + case SCA3000_OP_MODE_BYPASS: 388 + len += sprintf(buf + len, ", 1 - bypass mode"); 389 + break; 390 + }; 391 + switch (st->info->option_mode_2) { 392 + case SCA3000_OP_MODE_WIDE: 393 + len += sprintf(buf + len, ", 2 - wide mode"); 394 + break; 395 + } 396 + /* always supported */ 397 + len += sprintf(buf + len, " 3 - motion detection \n"); 398 + 399 + return len; 400 + } 401 + 402 + /** 403 + * sca3000_show_measurmenet_mode() sysfs read of current mode 404 + **/ 405 + static ssize_t 406 + sca3000_show_measurement_mode(struct device *dev, 407 + struct device_attribute *attr, 408 + char *buf) 409 + { 410 + struct iio_dev *dev_info = dev_get_drvdata(dev); 411 + struct sca3000_state *st = dev_info->dev_data; 412 + int len = 0, ret; 413 + u8 *rx; 414 + 415 + mutex_lock(&st->lock); 416 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 417 + if (ret) 418 + goto error_ret; 419 + /* mask bottom 2 bits - only ones that are relevant */ 420 + rx[1] &= 0x03; 421 + switch (rx[1]) { 422 + case SCA3000_MEAS_MODE_NORMAL: 423 + len += sprintf(buf + len, "0 - normal mode\n"); 424 + break; 425 + case SCA3000_MEAS_MODE_MOT_DET: 426 + len += sprintf(buf + len, "3 - motion detection\n"); 427 + break; 428 + case SCA3000_MEAS_MODE_OP_1: 429 + switch (st->info->option_mode_1) { 430 + case SCA3000_OP_MODE_NARROW: 431 + len += sprintf(buf + len, "1 - narrow mode\n"); 432 + break; 433 + case SCA3000_OP_MODE_BYPASS: 434 + len += sprintf(buf + len, "1 - bypass mode\n"); 435 + break; 436 + }; 437 + break; 438 + case SCA3000_MEAS_MODE_OP_2: 439 + switch (st->info->option_mode_2) { 440 + case SCA3000_OP_MODE_WIDE: 441 + len += sprintf(buf + len, "2 - wide mode\n"); 442 + break; 443 + } 444 + break; 445 + }; 446 + 447 + error_ret: 448 + mutex_unlock(&st->lock); 449 + 450 + return ret ? ret : len; 451 + } 452 + 453 + /** 454 + * sca3000_store_measurement_mode() set the current mode 455 + **/ 456 + static ssize_t 457 + sca3000_store_measurement_mode(struct device *dev, 458 + struct device_attribute *attr, 459 + const char *buf, 460 + size_t len) 461 + { 462 + struct iio_dev *dev_info = dev_get_drvdata(dev); 463 + struct sca3000_state *st = dev_info->dev_data; 464 + int ret; 465 + u8 *rx; 466 + int mask = 0x03; 467 + long val; 468 + 469 + mutex_lock(&st->lock); 470 + ret = strict_strtol(buf, 10, &val); 471 + if (ret) 472 + goto error_ret; 473 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 474 + if (ret) 475 + goto error_ret; 476 + rx[1] &= ~mask; 477 + rx[1] |= (val & mask); 478 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, rx[1]); 479 + if (ret) 480 + goto error_free_rx; 481 + mutex_unlock(&st->lock); 482 + 483 + return len; 484 + 485 + error_free_rx: 486 + kfree(rx); 487 + error_ret: 488 + mutex_unlock(&st->lock); 489 + 490 + return ret; 491 + } 492 + 493 + 494 + /* Not even vaguely standard attributes so defined here rather than 495 + * in the relevant IIO core headers 496 + */ 497 + static IIO_DEVICE_ATTR(available_measurement_modes, S_IRUGO, 498 + sca3000_show_available_measurement_modes, 499 + NULL, 0); 500 + 501 + static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR, 502 + sca3000_show_measurement_mode, 503 + sca3000_store_measurement_mode, 504 + 0); 505 + 506 + /* More standard attributes */ 507 + 508 + static IIO_DEV_ATTR_NAME(sca3000_show_name); 509 + static IIO_DEV_ATTR_REV(sca3000_show_rev); 510 + 511 + static IIO_DEV_ATTR_ACCEL_X(sca3000_read_13bit_signed, 512 + SCA3000_REG_ADDR_X_MSB); 513 + static IIO_DEV_ATTR_ACCEL_Y(sca3000_read_13bit_signed, 514 + SCA3000_REG_ADDR_Y_MSB); 515 + static IIO_DEV_ATTR_ACCEL_Z(sca3000_read_13bit_signed, 516 + SCA3000_REG_ADDR_Z_MSB); 517 + 518 + 519 + /** 520 + * sca3000_read_av_freq() sysfs function to get available frequencies 521 + * 522 + * The later modes are only relevant to the ring buffer - and depend on current 523 + * mode. Note that data sheet gives rather wide tolerances for these so integer 524 + * division will give good enough answer and not all chips have them specified 525 + * at all. 526 + **/ 527 + static ssize_t sca3000_read_av_freq(struct device *dev, 528 + struct device_attribute *attr, 529 + char *buf) 530 + { 531 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 532 + struct sca3000_state *st = indio_dev->dev_data; 533 + int len = 0, ret; 534 + u8 *rx; 535 + mutex_lock(&st->lock); 536 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 537 + mutex_unlock(&st->lock); 538 + if (ret) 539 + goto error_ret; 540 + rx[1] &= 0x03; 541 + switch (rx[1]) { 542 + case SCA3000_MEAS_MODE_NORMAL: 543 + len += sprintf(buf + len, "%d %d %d\n", 544 + st->info->measurement_mode_freq, 545 + st->info->measurement_mode_freq/2, 546 + st->info->measurement_mode_freq/4); 547 + break; 548 + case SCA3000_MEAS_MODE_OP_1: 549 + len += sprintf(buf + len, "%d %d %d\n", 550 + st->info->option_mode_1_freq, 551 + st->info->option_mode_1_freq/2, 552 + st->info->option_mode_1_freq/4); 553 + break; 554 + case SCA3000_MEAS_MODE_OP_2: 555 + len += sprintf(buf + len, "%d %d %d\n", 556 + st->info->option_mode_2_freq, 557 + st->info->option_mode_2_freq/2, 558 + st->info->option_mode_2_freq/4); 559 + break; 560 + }; 561 + kfree(rx); 562 + return len; 563 + error_ret: 564 + return ret; 565 + } 566 + /** 567 + * __sca3000_get_base_frequency() obtain mode specific base frequency 568 + * 569 + * lock must be held 570 + **/ 571 + static inline int __sca3000_get_base_freq(struct sca3000_state *st, 572 + const struct sca3000_chip_info *info, 573 + int *base_freq) 574 + { 575 + int ret; 576 + u8 *rx; 577 + 578 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 579 + if (ret) 580 + goto error_ret; 581 + switch (0x03 & rx[1]) { 582 + case SCA3000_MEAS_MODE_NORMAL: 583 + *base_freq = info->measurement_mode_freq; 584 + break; 585 + case SCA3000_MEAS_MODE_OP_1: 586 + *base_freq = info->option_mode_1_freq; 587 + break; 588 + case SCA3000_MEAS_MODE_OP_2: 589 + *base_freq = info->option_mode_2_freq; 590 + break; 591 + }; 592 + kfree(rx); 593 + error_ret: 594 + return ret; 595 + } 596 + 597 + /** 598 + * sca3000_read_frequency() sysfs interface to get the current frequency 599 + **/ 600 + static ssize_t sca3000_read_frequency(struct device *dev, 601 + struct device_attribute *attr, 602 + char *buf) 603 + { 604 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 605 + struct sca3000_state *st = indio_dev->dev_data; 606 + int ret, len = 0, base_freq = 0; 607 + u8 *rx; 608 + mutex_lock(&st->lock); 609 + ret = __sca3000_get_base_freq(st, st->info, &base_freq); 610 + if (ret) 611 + goto error_ret_mut; 612 + ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx); 613 + mutex_unlock(&st->lock); 614 + if (ret) 615 + goto error_ret; 616 + if (base_freq > 0) 617 + switch (rx[1]&0x03) { 618 + case 0x00: 619 + case 0x03: 620 + len = sprintf(buf, "%d\n", base_freq); 621 + break; 622 + case 0x01: 623 + len = sprintf(buf, "%d\n", base_freq/2); 624 + break; 625 + case 0x02: 626 + len = sprintf(buf, "%d\n", base_freq/4); 627 + break; 628 + }; 629 + kfree(rx); 630 + return len; 631 + error_ret_mut: 632 + mutex_unlock(&st->lock); 633 + error_ret: 634 + return ret; 635 + } 636 + 637 + /** 638 + * sca3000_set_frequency() sysfs interface to set the current frequency 639 + **/ 640 + static ssize_t sca3000_set_frequency(struct device *dev, 641 + struct device_attribute *attr, 642 + const char *buf, 643 + size_t len) 644 + { 645 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 646 + struct sca3000_state *st = indio_dev->dev_data; 647 + int ret, base_freq = 0; 648 + u8 *rx; 649 + long val; 650 + 651 + ret = strict_strtol(buf, 10, &val); 652 + if (ret) 653 + return ret; 654 + 655 + mutex_lock(&st->lock); 656 + /* What mode are we in? */ 657 + ret = __sca3000_get_base_freq(st, st->info, &base_freq); 658 + if (ret) 659 + goto error_free_lock; 660 + 661 + ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx); 662 + if (ret) 663 + goto error_free_lock; 664 + /* clear the bits */ 665 + rx[1] &= ~0x03; 666 + 667 + if (val == base_freq/2) { 668 + rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_2; 669 + } else if (val == base_freq/4) { 670 + rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_4; 671 + } else if (val != base_freq) { 672 + ret = -EINVAL; 673 + goto error_free_lock; 674 + } 675 + ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, rx[1]); 676 + error_free_lock: 677 + mutex_unlock(&st->lock); 678 + 679 + return ret ? ret : len; 680 + } 681 + 682 + /* Should only really be registered if ring buffer support is compiled in. 683 + * Does no harm however and doing it right would add a fair bit of complexity 684 + */ 685 + static IIO_DEV_ATTR_AVAIL_SAMP_FREQ(sca3000_read_av_freq); 686 + 687 + static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, 688 + sca3000_read_frequency, 689 + sca3000_set_frequency); 690 + 691 + 692 + /** 693 + * sca3000_read_temp() sysfs interface to get the temperature when available 694 + * 695 + * The alignment of data in here is downright odd. See data sheet. 696 + * Converting this into a meaningful value is left to inline functions in 697 + * userspace part of header. 698 + **/ 699 + static ssize_t sca3000_read_temp(struct device *dev, 700 + struct device_attribute *attr, 701 + char *buf) 702 + { 703 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 704 + struct sca3000_state *st = indio_dev->dev_data; 705 + int len = 0, ret; 706 + int val; 707 + u8 *rx; 708 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_TEMP_MSB, &rx, 2); 709 + if (ret < 0) 710 + goto error_ret; 711 + val = ((rx[1]&0x3F) << 3) | ((rx[2] & 0xE0) >> 5); 712 + len += sprintf(buf + len, "%d\n", val); 713 + kfree(rx); 714 + 715 + return len; 716 + 717 + error_ret: 718 + return ret; 719 + } 720 + static IIO_DEV_ATTR_TEMP(sca3000_read_temp); 721 + 722 + /** 723 + * sca3000_show_thresh() sysfs query of a theshold 724 + **/ 725 + static ssize_t sca3000_show_thresh(struct device *dev, 726 + struct device_attribute *attr, 727 + char *buf) 728 + { 729 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 730 + struct sca3000_state *st = indio_dev->dev_data; 731 + struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 732 + int len = 0, ret; 733 + u8 *rx; 734 + 735 + mutex_lock(&st->lock); 736 + ret = sca3000_read_ctrl_reg(st, 737 + this_attr->address, 738 + &rx); 739 + mutex_unlock(&st->lock); 740 + if (ret) 741 + return ret; 742 + len += sprintf(buf + len, "%d\n", rx[1]); 743 + kfree(rx); 744 + 745 + return len; 746 + } 747 + 748 + /** 749 + * sca3000_write_thresh() sysfs control of threshold 750 + **/ 751 + static ssize_t sca3000_write_thresh(struct device *dev, 752 + struct device_attribute *attr, 753 + const char *buf, 754 + size_t len) 755 + { 756 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 757 + struct sca3000_state *st = indio_dev->dev_data; 758 + struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 759 + int ret; 760 + long val; 761 + 762 + ret = strict_strtol(buf, 10, &val); 763 + if (ret) 764 + return ret; 765 + mutex_lock(&st->lock); 766 + ret = sca3000_write_ctrl_reg(st, this_attr->address, val); 767 + mutex_unlock(&st->lock); 768 + 769 + return ret ? ret : len; 770 + } 771 + 772 + static IIO_DEV_ATTR_ACCEL_THRESH_X(S_IRUGO | S_IWUSR, 773 + sca3000_show_thresh, 774 + sca3000_write_thresh, 775 + SCA3000_REG_CTRL_SEL_MD_X_TH); 776 + static IIO_DEV_ATTR_ACCEL_THRESH_Y(S_IRUGO | S_IWUSR, 777 + sca3000_show_thresh, 778 + sca3000_write_thresh, 779 + SCA3000_REG_CTRL_SEL_MD_Y_TH); 780 + static IIO_DEV_ATTR_ACCEL_THRESH_Z(S_IRUGO | S_IWUSR, 781 + sca3000_show_thresh, 782 + sca3000_write_thresh, 783 + SCA3000_REG_CTRL_SEL_MD_Z_TH); 784 + 785 + static struct attribute *sca3000_attributes[] = { 786 + &iio_dev_attr_name.dev_attr.attr, 787 + &iio_dev_attr_revision.dev_attr.attr, 788 + &iio_dev_attr_accel_x.dev_attr.attr, 789 + &iio_dev_attr_accel_y.dev_attr.attr, 790 + &iio_dev_attr_accel_z.dev_attr.attr, 791 + &iio_dev_attr_thresh_accel_x.dev_attr.attr, 792 + &iio_dev_attr_thresh_accel_y.dev_attr.attr, 793 + &iio_dev_attr_thresh_accel_z.dev_attr.attr, 794 + &iio_dev_attr_available_measurement_modes.dev_attr.attr, 795 + &iio_dev_attr_measurement_mode.dev_attr.attr, 796 + &iio_dev_attr_available_sampling_frequency.dev_attr.attr, 797 + &iio_dev_attr_sampling_frequency.dev_attr.attr, 798 + NULL, 799 + }; 800 + 801 + static struct attribute *sca3000_attributes_with_temp[] = { 802 + &iio_dev_attr_name.dev_attr.attr, 803 + &iio_dev_attr_revision.dev_attr.attr, 804 + &iio_dev_attr_accel_x.dev_attr.attr, 805 + &iio_dev_attr_accel_y.dev_attr.attr, 806 + &iio_dev_attr_accel_z.dev_attr.attr, 807 + &iio_dev_attr_thresh_accel_x.dev_attr.attr, 808 + &iio_dev_attr_thresh_accel_y.dev_attr.attr, 809 + &iio_dev_attr_thresh_accel_z.dev_attr.attr, 810 + &iio_dev_attr_available_measurement_modes.dev_attr.attr, 811 + &iio_dev_attr_measurement_mode.dev_attr.attr, 812 + &iio_dev_attr_available_sampling_frequency.dev_attr.attr, 813 + &iio_dev_attr_sampling_frequency.dev_attr.attr, 814 + /* Only present if temp sensor is */ 815 + &iio_dev_attr_temp.dev_attr.attr, 816 + NULL, 817 + }; 818 + 819 + static const struct attribute_group sca3000_attribute_group = { 820 + .attrs = sca3000_attributes, 821 + }; 822 + 823 + static const struct attribute_group sca3000_attribute_group_with_temp = { 824 + .attrs = sca3000_attributes_with_temp, 825 + }; 826 + 827 + /* RING RELATED interrupt handler */ 828 + /* depending on event, push to the ring buffer event chrdev or the event one */ 829 + 830 + /** 831 + * sca3000_interrupt_handler_bh() - handling ring and non ring events 832 + * 833 + * This function is complicated by the fact that the devices can signify ring 834 + * and non ring events via the same interrupt line and they can only 835 + * be distinguished via a read of the relevant status register. 836 + **/ 837 + static void sca3000_interrupt_handler_bh(struct work_struct *work_s) 838 + { 839 + struct sca3000_state *st 840 + = container_of(work_s, struct sca3000_state, 841 + interrupt_handler_ws); 842 + u8 *rx; 843 + int ret; 844 + 845 + /* Could lead if badly timed to an extra read of status reg, 846 + * but ensures no interrupt is missed. 847 + */ 848 + enable_irq(st->us->irq); 849 + mutex_lock(&st->lock); 850 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS, 851 + &rx, 1); 852 + mutex_unlock(&st->lock); 853 + if (ret) 854 + goto done; 855 + 856 + sca3000_ring_int_process(rx[1], st->indio_dev->ring); 857 + 858 + if (rx[1] & SCA3000_INT_STATUS_FREE_FALL) 859 + iio_push_event(st->indio_dev, 0, 860 + IIO_EVENT_CODE_FREE_FALL, 861 + st->last_timestamp); 862 + 863 + if (rx[1] & SCA3000_INT_STATUS_Y_TRIGGER) 864 + iio_push_event(st->indio_dev, 0, 865 + IIO_EVENT_CODE_ACCEL_Y_HIGH, 866 + st->last_timestamp); 867 + 868 + if (rx[1] & SCA3000_INT_STATUS_X_TRIGGER) 869 + iio_push_event(st->indio_dev, 0, 870 + IIO_EVENT_CODE_ACCEL_X_HIGH, 871 + st->last_timestamp); 872 + 873 + if (rx[1] & SCA3000_INT_STATUS_Z_TRIGGER) 874 + iio_push_event(st->indio_dev, 0, 875 + IIO_EVENT_CODE_ACCEL_Z_HIGH, 876 + st->last_timestamp); 877 + 878 + done: 879 + kfree(rx); 880 + return; 881 + } 882 + 883 + /** 884 + * sca3000_handler_th() handles all interrupt events from device 885 + * 886 + * These devices deploy unified interrupt status registers meaning 887 + * all interrupts must be handled together 888 + **/ 889 + static int sca3000_handler_th(struct iio_dev *dev_info, 890 + int index, 891 + s64 timestamp, 892 + int no_test) 893 + { 894 + struct sca3000_state *st = dev_info->dev_data; 895 + 896 + st->last_timestamp = timestamp; 897 + schedule_work(&st->interrupt_handler_ws); 898 + 899 + return 0; 900 + } 901 + 902 + /** 903 + * sca3000_query_mo_det() is motion detection enabled for this axis 904 + * 905 + * First queries if motion detection is enabled and then if this axis is 906 + * on. 907 + **/ 908 + static ssize_t sca3000_query_mo_det(struct device *dev, 909 + struct device_attribute *attr, 910 + char *buf) 911 + { 912 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 913 + struct sca3000_state *st = indio_dev->dev_data; 914 + struct iio_event_attr *this_attr = to_iio_event_attr(attr); 915 + int ret, len = 0; 916 + u8 *rx; 917 + u8 protect_mask = 0x03; 918 + 919 + /* read current value of mode register */ 920 + mutex_lock(&st->lock); 921 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 922 + if (ret) 923 + goto error_ret; 924 + 925 + if ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET) 926 + len += sprintf(buf + len, "0\n"); 927 + else { 928 + kfree(rx); 929 + ret = sca3000_read_ctrl_reg(st, 930 + SCA3000_REG_CTRL_SEL_MD_CTRL, 931 + &rx); 932 + if (ret) 933 + goto error_ret; 934 + /* only supporting logical or's for now */ 935 + len += sprintf(buf + len, "%d\n", 936 + (rx[1] & this_attr->mask) ? 1 : 0); 937 + } 938 + kfree(rx); 939 + error_ret: 940 + mutex_unlock(&st->lock); 941 + 942 + return ret ? ret : len; 943 + } 944 + /** 945 + * sca3000_query_free_fall_mode() is free fall mode enabled 946 + **/ 947 + static ssize_t sca3000_query_free_fall_mode(struct device *dev, 948 + struct device_attribute *attr, 949 + char *buf) 950 + { 951 + int ret, len; 952 + u8 *rx; 953 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 954 + struct sca3000_state *st = indio_dev->dev_data; 955 + 956 + mutex_lock(&st->lock); 957 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 958 + mutex_unlock(&st->lock); 959 + if (ret) 960 + return ret; 961 + len = sprintf(buf, "%d\n", 962 + !!(rx[1] & SCA3000_FREE_FALL_DETECT)); 963 + kfree(rx); 964 + 965 + return len; 966 + } 967 + /** 968 + * sca3000_query_ring_int() is the hardware ring status interrupt enabled 969 + **/ 970 + static ssize_t sca3000_query_ring_int(struct device *dev, 971 + struct device_attribute *attr, 972 + char *buf) 973 + { 974 + struct iio_event_attr *this_attr = to_iio_event_attr(attr); 975 + int ret, len; 976 + u8 *rx; 977 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 978 + struct sca3000_state *st = indio_dev->dev_data; 979 + mutex_lock(&st->lock); 980 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1); 981 + mutex_unlock(&st->lock); 982 + if (ret) 983 + return ret; 984 + len = sprintf(buf, "%d\n", (rx[1] & this_attr->mask) ? 1 : 0); 985 + kfree(rx); 986 + 987 + return len; 988 + } 989 + /** 990 + * sca3000_set_ring_int() set state of ring status interrupt 991 + **/ 992 + static ssize_t sca3000_set_ring_int(struct device *dev, 993 + struct device_attribute *attr, 994 + const char *buf, 995 + size_t len) 996 + { 997 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 998 + struct sca3000_state *st = indio_dev->dev_data; 999 + struct iio_event_attr *this_attr = to_iio_event_attr(attr); 1000 + 1001 + long val; 1002 + int ret; 1003 + u8 *rx; 1004 + 1005 + mutex_lock(&st->lock); 1006 + ret = strict_strtol(buf, 10, &val); 1007 + if (ret) 1008 + goto error_ret; 1009 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1); 1010 + if (ret) 1011 + goto error_ret; 1012 + if (val) 1013 + ret = sca3000_write_reg(st, 1014 + SCA3000_REG_ADDR_INT_MASK, 1015 + rx[1] | this_attr->mask); 1016 + else 1017 + ret = sca3000_write_reg(st, 1018 + SCA3000_REG_ADDR_INT_MASK, 1019 + rx[1] & ~this_attr->mask); 1020 + kfree(rx); 1021 + error_ret: 1022 + mutex_unlock(&st->lock); 1023 + 1024 + return ret ? ret : len; 1025 + } 1026 + 1027 + /** 1028 + * sca3000_set_free_fall_mode() simple on off control for free fall int 1029 + * 1030 + * In these chips the free fall detector should send an interrupt if 1031 + * the device falls more than 25cm. This has not been tested due 1032 + * to fragile wiring. 1033 + **/ 1034 + 1035 + static ssize_t sca3000_set_free_fall_mode(struct device *dev, 1036 + struct device_attribute *attr, 1037 + const char *buf, 1038 + size_t len) 1039 + { 1040 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 1041 + struct sca3000_state *st = indio_dev->dev_data; 1042 + long val; 1043 + int ret; 1044 + u8 *rx; 1045 + u8 protect_mask = SCA3000_FREE_FALL_DETECT; 1046 + 1047 + mutex_lock(&st->lock); 1048 + ret = strict_strtol(buf, 10, &val); 1049 + if (ret) 1050 + goto error_ret; 1051 + 1052 + /* read current value of mode register */ 1053 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 1054 + if (ret) 1055 + goto error_ret; 1056 + 1057 + /*if off and should be on*/ 1058 + if (val && !(rx[1] & protect_mask)) 1059 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1060 + (rx[1] | SCA3000_FREE_FALL_DETECT)); 1061 + /* if on and should be off */ 1062 + else if (!val && (rx[1]&protect_mask)) 1063 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1064 + (rx[1] & ~protect_mask)); 1065 + 1066 + kfree(rx); 1067 + error_ret: 1068 + mutex_unlock(&st->lock); 1069 + 1070 + return ret ? ret : len; 1071 + } 1072 + 1073 + /** 1074 + * sca3000_set_mo_det() simple on off control for motion detector 1075 + * 1076 + * This is a per axis control, but enabling any will result in the 1077 + * motion detector unit being enabled. 1078 + * N.B. enabling motion detector stops normal data acquisition. 1079 + * There is a complexity in knowing which mode to return to when 1080 + * this mode is disabled. Currently normal mode is assumed. 1081 + **/ 1082 + static ssize_t sca3000_set_mo_det(struct device *dev, 1083 + struct device_attribute *attr, 1084 + const char *buf, 1085 + size_t len) 1086 + { 1087 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 1088 + struct sca3000_state *st = indio_dev->dev_data; 1089 + struct iio_event_attr *this_attr = to_iio_event_attr(attr); 1090 + long val; 1091 + int ret; 1092 + u8 *rx; 1093 + u8 protect_mask = 0x03; 1094 + ret = strict_strtol(buf, 10, &val); 1095 + if (ret) 1096 + return ret; 1097 + 1098 + mutex_lock(&st->lock); 1099 + /* First read the motion detector config to find out if 1100 + * this axis is on*/ 1101 + ret = sca3000_read_ctrl_reg(st, 1102 + SCA3000_REG_CTRL_SEL_MD_CTRL, 1103 + &rx); 1104 + if (ret) 1105 + goto exit_point; 1106 + /* Off and should be on */ 1107 + if (val && !(rx[1] & this_attr->mask)) { 1108 + ret = sca3000_write_ctrl_reg(st, 1109 + SCA3000_REG_CTRL_SEL_MD_CTRL, 1110 + rx[1] | this_attr->mask); 1111 + if (ret) 1112 + goto exit_point_free_rx; 1113 + st->mo_det_use_count++; 1114 + } else if (!val && (rx[1]&this_attr->mask)) { 1115 + ret = sca3000_write_ctrl_reg(st, 1116 + SCA3000_REG_CTRL_SEL_MD_CTRL, 1117 + rx[1] & ~(this_attr->mask)); 1118 + if (ret) 1119 + goto exit_point_free_rx; 1120 + st->mo_det_use_count--; 1121 + } else /* relies on clean state for device on boot */ 1122 + goto exit_point_free_rx; 1123 + kfree(rx); 1124 + /* read current value of mode register */ 1125 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 1126 + if (ret) 1127 + goto exit_point; 1128 + /*if off and should be on*/ 1129 + if ((st->mo_det_use_count) 1130 + && ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET)) 1131 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1132 + (rx[1] & ~protect_mask) 1133 + | SCA3000_MEAS_MODE_MOT_DET); 1134 + /* if on and should be off */ 1135 + else if (!(st->mo_det_use_count) 1136 + && ((rx[1]&protect_mask) == SCA3000_MEAS_MODE_MOT_DET)) 1137 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1138 + (rx[1] & ~protect_mask)); 1139 + exit_point_free_rx: 1140 + kfree(rx); 1141 + exit_point: 1142 + mutex_unlock(&st->lock); 1143 + 1144 + return ret ? ret : len; 1145 + } 1146 + 1147 + /* Shared event handler for all events as single event status register */ 1148 + IIO_EVENT_SH(all, &sca3000_handler_th); 1149 + 1150 + /* Free fall detector related event attribute */ 1151 + IIO_EVENT_ATTR_FREE_FALL_DETECT_SH(iio_event_all, 1152 + sca3000_query_free_fall_mode, 1153 + sca3000_set_free_fall_mode, 1154 + 0) 1155 + 1156 + /* Motion detector related event attributes */ 1157 + IIO_EVENT_ATTR_ACCEL_X_HIGH_SH(iio_event_all, 1158 + sca3000_query_mo_det, 1159 + sca3000_set_mo_det, 1160 + SCA3000_MD_CTRL_OR_X); 1161 + 1162 + IIO_EVENT_ATTR_ACCEL_Y_HIGH_SH(iio_event_all, 1163 + sca3000_query_mo_det, 1164 + sca3000_set_mo_det, 1165 + SCA3000_MD_CTRL_OR_Y); 1166 + 1167 + IIO_EVENT_ATTR_ACCEL_Z_HIGH_SH(iio_event_all, 1168 + sca3000_query_mo_det, 1169 + sca3000_set_mo_det, 1170 + SCA3000_MD_CTRL_OR_Z); 1171 + 1172 + /* Hardware ring buffer related event attributes */ 1173 + IIO_EVENT_ATTR_RING_50_FULL_SH(iio_event_all, 1174 + sca3000_query_ring_int, 1175 + sca3000_set_ring_int, 1176 + SCA3000_INT_MASK_RING_HALF); 1177 + 1178 + IIO_EVENT_ATTR_RING_75_FULL_SH(iio_event_all, 1179 + sca3000_query_ring_int, 1180 + sca3000_set_ring_int, 1181 + SCA3000_INT_MASK_RING_THREE_QUARTER); 1182 + 1183 + static struct attribute *sca3000_event_attributes[] = { 1184 + &iio_event_attr_free_fall.dev_attr.attr, 1185 + &iio_event_attr_accel_x_high.dev_attr.attr, 1186 + &iio_event_attr_accel_y_high.dev_attr.attr, 1187 + &iio_event_attr_accel_z_high.dev_attr.attr, 1188 + &iio_event_attr_ring_50_full.dev_attr.attr, 1189 + &iio_event_attr_ring_75_full.dev_attr.attr, 1190 + NULL, 1191 + }; 1192 + 1193 + static struct attribute_group sca3000_event_attribute_group = { 1194 + .attrs = sca3000_event_attributes, 1195 + }; 1196 + 1197 + /** 1198 + * sca3000_clean_setup() get the device into a predictable state 1199 + * 1200 + * Devices use flash memory to store many of the register values 1201 + * and hence can come up in somewhat unpredictable states. 1202 + * Hence reset everything on driver load. 1203 + **/ 1204 + static int sca3000_clean_setup(struct sca3000_state *st) 1205 + { 1206 + int ret; 1207 + u8 *rx; 1208 + 1209 + mutex_lock(&st->lock); 1210 + /* Ensure all interrupts have been acknowledged */ 1211 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS, &rx, 1); 1212 + if (ret) 1213 + goto error_ret; 1214 + kfree(rx); 1215 + 1216 + /* Turn off all motion detection channels */ 1217 + ret = sca3000_read_ctrl_reg(st, 1218 + SCA3000_REG_CTRL_SEL_MD_CTRL, 1219 + &rx); 1220 + if (ret) 1221 + goto error_ret; 1222 + ret = sca3000_write_ctrl_reg(st, 1223 + SCA3000_REG_CTRL_SEL_MD_CTRL, 1224 + rx[1] & SCA3000_MD_CTRL_PROT_MASK); 1225 + kfree(rx); 1226 + if (ret) 1227 + goto error_ret; 1228 + 1229 + /* Disable ring buffer */ 1230 + sca3000_read_ctrl_reg(st, 1231 + SCA3000_REG_CTRL_SEL_OUT_CTRL, 1232 + &rx); 1233 + /* Frequency of ring buffer sampling deliberately restricted to make 1234 + * debugging easier - add control of this later */ 1235 + ret = sca3000_write_ctrl_reg(st, 1236 + SCA3000_REG_CTRL_SEL_OUT_CTRL, 1237 + (rx[1] & SCA3000_OUT_CTRL_PROT_MASK) 1238 + | SCA3000_OUT_CTRL_BUF_X_EN 1239 + | SCA3000_OUT_CTRL_BUF_Y_EN 1240 + | SCA3000_OUT_CTRL_BUF_Z_EN 1241 + | SCA3000_OUT_CTRL_BUF_DIV_4); 1242 + kfree(rx); 1243 + 1244 + if (ret) 1245 + goto error_ret; 1246 + /* Enable interrupts, relevant to mode and set up as active low */ 1247 + ret = sca3000_read_data(st, 1248 + SCA3000_REG_ADDR_INT_MASK, 1249 + &rx, 1); 1250 + if (ret) 1251 + goto error_ret; 1252 + ret = sca3000_write_reg(st, 1253 + SCA3000_REG_ADDR_INT_MASK, 1254 + (rx[1] & SCA3000_INT_MASK_PROT_MASK) 1255 + | SCA3000_INT_MASK_ACTIVE_LOW); 1256 + kfree(rx); 1257 + if (ret) 1258 + goto error_ret; 1259 + /* Select normal measurement mode, free fall off, ring off */ 1260 + /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5 1261 + * as that occurs in one of the example on the datasheet */ 1262 + ret = sca3000_read_data(st, 1263 + SCA3000_REG_ADDR_MODE, 1264 + &rx, 1); 1265 + if (ret) 1266 + goto error_ret; 1267 + ret = sca3000_write_reg(st, 1268 + SCA3000_REG_ADDR_MODE, 1269 + (rx[1] & SCA3000_MODE_PROT_MASK)); 1270 + kfree(rx); 1271 + st->bpse = 11; 1272 + 1273 + error_ret: 1274 + mutex_unlock(&st->lock); 1275 + return ret; 1276 + } 1277 + 1278 + static int __devinit __sca3000_probe(struct spi_device *spi, 1279 + enum sca3000_variant variant) 1280 + { 1281 + int ret, regdone = 0; 1282 + struct sca3000_state *st; 1283 + 1284 + st = kzalloc(sizeof(struct sca3000_state), GFP_KERNEL); 1285 + if (st == NULL) { 1286 + ret = -ENOMEM; 1287 + goto error_ret; 1288 + } 1289 + spi_set_drvdata(spi, st); 1290 + 1291 + st->tx = kmalloc(sizeof(*st->tx)*6, GFP_KERNEL); 1292 + if (st->tx == NULL) { 1293 + ret = -ENOMEM; 1294 + goto error_clear_st; 1295 + } 1296 + st->rx = kmalloc(sizeof(*st->rx)*3, GFP_KERNEL); 1297 + if (st->rx == NULL) { 1298 + ret = -ENOMEM; 1299 + goto error_free_tx; 1300 + } 1301 + st->us = spi; 1302 + mutex_init(&st->lock); 1303 + st->info = &sca3000_spi_chip_info_tbl[variant]; 1304 + 1305 + st->indio_dev = iio_allocate_device(); 1306 + if (st->indio_dev == NULL) { 1307 + ret = -ENOMEM; 1308 + goto error_free_rx; 1309 + } 1310 + 1311 + st->indio_dev->dev.parent = &spi->dev; 1312 + st->indio_dev->num_interrupt_lines = 1; 1313 + st->indio_dev->event_attrs = &sca3000_event_attribute_group; 1314 + if (st->info->temp_output) 1315 + st->indio_dev->attrs = &sca3000_attribute_group_with_temp; 1316 + else 1317 + st->indio_dev->attrs = &sca3000_attribute_group; 1318 + st->indio_dev->dev_data = (void *)(st); 1319 + st->indio_dev->modes = INDIO_DIRECT_MODE; 1320 + 1321 + sca3000_configure_ring(st->indio_dev); 1322 + 1323 + ret = iio_device_register(st->indio_dev); 1324 + if (ret < 0) 1325 + goto error_free_dev; 1326 + regdone = 1; 1327 + ret = iio_ring_buffer_register(st->indio_dev->ring); 1328 + if (ret < 0) 1329 + goto error_unregister_dev; 1330 + if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) { 1331 + INIT_WORK(&st->interrupt_handler_ws, 1332 + sca3000_interrupt_handler_bh); 1333 + ret = iio_register_interrupt_line(spi->irq, 1334 + st->indio_dev, 1335 + 0, 1336 + IRQF_TRIGGER_FALLING, 1337 + "sca3000"); 1338 + if (ret) 1339 + goto error_unregister_ring; 1340 + /* RFC 1341 + * Probably a common situation. All interrupts need an ack 1342 + * and there is only one handler so the complicated list system 1343 + * is overkill. At very least a simpler registration method 1344 + * might be worthwhile. 1345 + */ 1346 + iio_add_event_to_list(iio_event_attr_accel_z_high.listel, 1347 + &st->indio_dev 1348 + ->interrupts[0]->ev_list); 1349 + } 1350 + sca3000_register_ring_funcs(st->indio_dev); 1351 + ret = sca3000_clean_setup(st); 1352 + if (ret) 1353 + goto error_unregister_interrupt_line; 1354 + return 0; 1355 + 1356 + error_unregister_interrupt_line: 1357 + if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) 1358 + iio_unregister_interrupt_line(st->indio_dev, 0); 1359 + error_unregister_ring: 1360 + iio_ring_buffer_unregister(st->indio_dev->ring); 1361 + error_unregister_dev: 1362 + error_free_dev: 1363 + if (regdone) 1364 + iio_device_unregister(st->indio_dev); 1365 + else 1366 + iio_free_device(st->indio_dev); 1367 + error_free_rx: 1368 + kfree(st->rx); 1369 + error_free_tx: 1370 + kfree(st->tx); 1371 + error_clear_st: 1372 + kfree(st); 1373 + error_ret: 1374 + return ret; 1375 + } 1376 + 1377 + static int sca3000_stop_all_interrupts(struct sca3000_state *st) 1378 + { 1379 + int ret; 1380 + u8 *rx; 1381 + 1382 + mutex_lock(&st->lock); 1383 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1); 1384 + if (ret) 1385 + goto error_ret; 1386 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK, 1387 + (rx[1] & ~(SCA3000_INT_MASK_RING_THREE_QUARTER 1388 + | SCA3000_INT_MASK_RING_HALF 1389 + | SCA3000_INT_MASK_ALL_INTS))); 1390 + error_ret: 1391 + kfree(rx); 1392 + return ret; 1393 + 1394 + } 1395 + 1396 + static int sca3000_remove(struct spi_device *spi) 1397 + { 1398 + struct sca3000_state *st = spi_get_drvdata(spi); 1399 + struct iio_dev *indio_dev = st->indio_dev; 1400 + int ret; 1401 + /* Must ensure no interrupts can be generated after this!*/ 1402 + ret = sca3000_stop_all_interrupts(st); 1403 + if (ret) 1404 + return ret; 1405 + if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) 1406 + iio_unregister_interrupt_line(indio_dev, 0); 1407 + iio_ring_buffer_unregister(indio_dev->ring); 1408 + sca3000_unconfigure_ring(indio_dev); 1409 + iio_device_unregister(indio_dev); 1410 + 1411 + kfree(st->tx); 1412 + kfree(st->rx); 1413 + kfree(st); 1414 + 1415 + return 0; 1416 + } 1417 + 1418 + /* These macros save on an awful lot of repeated code */ 1419 + #define SCA3000_VARIANT_PROBE(_name) \ 1420 + static int __devinit \ 1421 + sca3000_##_name##_probe(struct spi_device *spi) \ 1422 + { \ 1423 + return __sca3000_probe(spi, _name); \ 1424 + } 1425 + 1426 + #define SCA3000_VARIANT_SPI_DRIVER(_name) \ 1427 + struct spi_driver sca3000_##_name##_driver = { \ 1428 + .driver = { \ 1429 + .name = "sca3000_" #_name, \ 1430 + .owner = THIS_MODULE, \ 1431 + }, \ 1432 + .probe = sca3000_##_name##_probe, \ 1433 + .remove = __devexit_p(sca3000_remove), \ 1434 + } 1435 + 1436 + SCA3000_VARIANT_PROBE(d01); 1437 + static SCA3000_VARIANT_SPI_DRIVER(d01); 1438 + 1439 + SCA3000_VARIANT_PROBE(d03); 1440 + static SCA3000_VARIANT_SPI_DRIVER(d03); 1441 + 1442 + SCA3000_VARIANT_PROBE(e02); 1443 + static SCA3000_VARIANT_SPI_DRIVER(e02); 1444 + 1445 + SCA3000_VARIANT_PROBE(e04); 1446 + static SCA3000_VARIANT_SPI_DRIVER(e04); 1447 + 1448 + SCA3000_VARIANT_PROBE(e05); 1449 + static SCA3000_VARIANT_SPI_DRIVER(e05); 1450 + 1451 + SCA3000_VARIANT_PROBE(l01); 1452 + static SCA3000_VARIANT_SPI_DRIVER(l01); 1453 + 1454 + static __init int sca3000_init(void) 1455 + { 1456 + int ret; 1457 + 1458 + ret = spi_register_driver(&sca3000_d01_driver); 1459 + if (ret) 1460 + goto error_ret; 1461 + ret = spi_register_driver(&sca3000_d03_driver); 1462 + if (ret) 1463 + goto error_unreg_d01; 1464 + ret = spi_register_driver(&sca3000_e02_driver); 1465 + if (ret) 1466 + goto error_unreg_d03; 1467 + ret = spi_register_driver(&sca3000_e04_driver); 1468 + if (ret) 1469 + goto error_unreg_e02; 1470 + ret = spi_register_driver(&sca3000_e05_driver); 1471 + if (ret) 1472 + goto error_unreg_e04; 1473 + ret = spi_register_driver(&sca3000_l01_driver); 1474 + if (ret) 1475 + goto error_unreg_e05; 1476 + 1477 + return 0; 1478 + 1479 + error_unreg_e05: 1480 + spi_unregister_driver(&sca3000_e05_driver); 1481 + error_unreg_e04: 1482 + spi_unregister_driver(&sca3000_e04_driver); 1483 + error_unreg_e02: 1484 + spi_unregister_driver(&sca3000_e02_driver); 1485 + error_unreg_d03: 1486 + spi_unregister_driver(&sca3000_d03_driver); 1487 + error_unreg_d01: 1488 + spi_unregister_driver(&sca3000_d01_driver); 1489 + error_ret: 1490 + 1491 + return ret; 1492 + } 1493 + 1494 + static __exit void sca3000_exit(void) 1495 + { 1496 + spi_unregister_driver(&sca3000_l01_driver); 1497 + spi_unregister_driver(&sca3000_e05_driver); 1498 + spi_unregister_driver(&sca3000_e04_driver); 1499 + spi_unregister_driver(&sca3000_e02_driver); 1500 + spi_unregister_driver(&sca3000_d03_driver); 1501 + spi_unregister_driver(&sca3000_d01_driver); 1502 + } 1503 + 1504 + module_init(sca3000_init); 1505 + module_exit(sca3000_exit); 1506 + 1507 + MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>"); 1508 + MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver"); 1509 + MODULE_LICENSE("GPL v2");
+331
drivers/staging/iio/accel/sca3000_ring.c
··· 1 + /* 2 + * sca3000_ring.c -- support VTI sca3000 series accelerometers via SPI 3 + * 4 + * This program is free software; you can redistribute it and/or modify it 5 + * under the terms of the GNU General Public License version 2 as published by 6 + * the Free Software Foundation. 7 + * 8 + * Copyright (c) 2009 Jonathan Cameron <jic23@cam.ac.uk> 9 + * 10 + */ 11 + 12 + #include <linux/interrupt.h> 13 + #include <linux/gpio.h> 14 + #include <linux/fs.h> 15 + #include <linux/device.h> 16 + #include <linux/kernel.h> 17 + #include <linux/spi/spi.h> 18 + #include <linux/sysfs.h> 19 + 20 + #include "../iio.h" 21 + #include "../sysfs.h" 22 + #include "../ring_generic.h" 23 + #include "../ring_hw.h" 24 + #include "accel.h" 25 + #include "sca3000.h" 26 + 27 + /* RFC / future work 28 + * 29 + * The internal ring buffer doesn't actually change what it holds depending 30 + * on which signals are enabled etc, merely whether you can read them. 31 + * As such the scan mode selection is somewhat different than for a software 32 + * ring buffer and changing it actually covers any data already in the buffer. 33 + * Currently scan elements aren't configured so it doesn't matter. 34 + */ 35 + 36 + /** 37 + * sca3000_rip_hw_rb() - main ring access function, pulls data from ring 38 + * @r: the ring 39 + * @count: number of samples to try and pull 40 + * @data: output the actual samples pulled from the hw ring 41 + * @dead_offset: cheating a bit here: Set to 1 so as to allow for the 42 + * leading byte used in bus comms. 43 + * 44 + * Currently does not provide timestamps. As the hardware doesn't add them they 45 + * can only be inferred aproximately from ring buffer events such as 50% full 46 + * and knowledge of when buffer was last emptied. This is left to userspace. 47 + **/ 48 + static int sca3000_rip_hw_rb(struct iio_ring_buffer *r, 49 + size_t count, u8 **data, int *dead_offset) 50 + { 51 + struct iio_hw_ring_buffer *hw_ring = iio_to_hw_ring_buf(r); 52 + struct iio_dev *indio_dev = hw_ring->private; 53 + struct sca3000_state *st = indio_dev->dev_data; 54 + u8 *rx; 55 + int ret, num_available, num_read = 0; 56 + int bytes_per_sample = 1; 57 + 58 + if (st->bpse == 11) 59 + bytes_per_sample = 2; 60 + 61 + mutex_lock(&st->lock); 62 + /* Check how much data is available: 63 + * RFC: Implement an ioctl to not bother checking whether there 64 + * is enough data in the ring? Afterall, if we are responding 65 + * to an interrupt we have a minimum content guaranteed so it 66 + * seems slight silly to waste time checking it is there. 67 + */ 68 + ret = sca3000_read_data(st, 69 + SCA3000_REG_ADDR_BUF_COUNT, 70 + &rx, 1); 71 + if (ret) 72 + goto error_ret; 73 + else 74 + num_available = rx[1]; 75 + /* num_available is the total number of samples available 76 + * i.e. number of time points * number of channels. 77 + */ 78 + kfree(rx); 79 + if (count > num_available * bytes_per_sample) 80 + num_read = num_available*bytes_per_sample; 81 + else 82 + num_read = count - (count % (bytes_per_sample)); 83 + 84 + /* Avoid the read request byte */ 85 + *dead_offset = 1; 86 + ret = sca3000_read_data(st, 87 + SCA3000_REG_ADDR_RING_OUT, 88 + data, num_read); 89 + error_ret: 90 + mutex_unlock(&st->lock); 91 + 92 + return ret ? ret : num_read; 93 + } 94 + 95 + /* This is only valid with all 3 elements enabled */ 96 + static int sca3000_ring_get_length(struct iio_ring_buffer *r) 97 + { 98 + return 64; 99 + } 100 + 101 + /* only valid if resolution is kept at 11bits */ 102 + static int sca3000_ring_get_bpd(struct iio_ring_buffer *r) 103 + { 104 + return 6; 105 + } 106 + static void sca3000_ring_release(struct device *dev) 107 + { 108 + struct iio_ring_buffer *r = to_iio_ring_buffer(dev); 109 + kfree(iio_to_hw_ring_buf(r)); 110 + } 111 + 112 + static IIO_RING_ENABLE_ATTR; 113 + static IIO_RING_BPS_ATTR; 114 + static IIO_RING_LENGTH_ATTR; 115 + 116 + /** 117 + * sca3000_show_ring_bpse() -sysfs function to query bits per sample from ring 118 + * @dev: ring buffer device 119 + * @attr: this device attribute 120 + * @buf: buffer to write to 121 + **/ 122 + static ssize_t sca3000_show_ring_bpse(struct device *dev, 123 + struct device_attribute *attr, 124 + char *buf) 125 + { 126 + int len = 0, ret; 127 + u8 *rx; 128 + struct iio_ring_buffer *r = dev_get_drvdata(dev); 129 + struct sca3000_state *st = r->indio_dev->dev_data; 130 + 131 + mutex_lock(&st->lock); 132 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 133 + if (ret) 134 + goto error_ret; 135 + len = sprintf(buf, "%d\n", (rx[1] & SCA3000_RING_BUF_8BIT) ? 8 : 11); 136 + kfree(rx); 137 + error_ret: 138 + mutex_unlock(&st->lock); 139 + 140 + return ret ? ret : len; 141 + } 142 + 143 + /** 144 + * sca3000_store_ring_bpse() - bits per scan element 145 + * @dev: ring buffer device 146 + * @attr: attribute called from 147 + * @buf: input from userspace 148 + * @len: length of input 149 + **/ 150 + static ssize_t sca3000_store_ring_bpse(struct device *dev, 151 + struct device_attribute *attr, 152 + const char *buf, 153 + size_t len) 154 + { 155 + struct iio_ring_buffer *r = dev_get_drvdata(dev); 156 + struct sca3000_state *st = r->indio_dev->dev_data; 157 + int ret; 158 + u8 *rx; 159 + long val; 160 + ret = strict_strtol(buf, 10, &val); 161 + if (ret) 162 + return ret; 163 + 164 + mutex_lock(&st->lock); 165 + 166 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 167 + if (!ret) 168 + switch (val) { 169 + case 8: 170 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 171 + rx[1] | SCA3000_RING_BUF_8BIT); 172 + st->bpse = 8; 173 + break; 174 + case 11: 175 + ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 176 + rx[1] & ~SCA3000_RING_BUF_8BIT); 177 + st->bpse = 11; 178 + break; 179 + default: 180 + ret = -EINVAL; 181 + break; 182 + } 183 + mutex_unlock(&st->lock); 184 + 185 + return ret ? ret : len; 186 + } 187 + 188 + static IIO_CONST_ATTR(bpse_available, "8 11"); 189 + 190 + static IIO_DEV_ATTR_BPSE(S_IRUGO | S_IWUSR, 191 + sca3000_show_ring_bpse, 192 + sca3000_store_ring_bpse); 193 + 194 + /* 195 + * Ring buffer attributes 196 + * This device is a bit unusual in that the sampling frequency and bpse 197 + * only apply to the ring buffer. At all times full rate and accuracy 198 + * is available via direct reading from registers. 199 + */ 200 + static struct attribute *iio_ring_attributes[] = { 201 + &dev_attr_length.attr, 202 + &dev_attr_bps.attr, 203 + &dev_attr_ring_enable.attr, 204 + &iio_dev_attr_bpse.dev_attr.attr, 205 + &iio_const_attr_bpse_available.dev_attr.attr, 206 + NULL, 207 + }; 208 + 209 + static struct attribute_group sca3000_ring_attr = { 210 + .attrs = iio_ring_attributes, 211 + }; 212 + 213 + static struct attribute_group *sca3000_ring_attr_groups[] = { 214 + &sca3000_ring_attr, 215 + NULL 216 + }; 217 + 218 + static struct device_type sca3000_ring_type = { 219 + .release = sca3000_ring_release, 220 + .groups = sca3000_ring_attr_groups, 221 + }; 222 + 223 + static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev) 224 + { 225 + struct iio_ring_buffer *buf; 226 + struct iio_hw_ring_buffer *ring; 227 + 228 + ring = kzalloc(sizeof *ring, GFP_KERNEL); 229 + if (!ring) 230 + return 0; 231 + ring->private = indio_dev; 232 + buf = &ring->buf; 233 + iio_ring_buffer_init(buf, indio_dev); 234 + buf->dev.type = &sca3000_ring_type; 235 + device_initialize(&buf->dev); 236 + buf->dev.parent = &indio_dev->dev; 237 + dev_set_drvdata(&buf->dev, (void *)buf); 238 + 239 + return buf; 240 + } 241 + 242 + static inline void sca3000_rb_free(struct iio_ring_buffer *r) 243 + { 244 + if (r) 245 + iio_put_ring_buffer(r); 246 + } 247 + 248 + int sca3000_configure_ring(struct iio_dev *indio_dev) 249 + { 250 + indio_dev->ring = sca3000_rb_allocate(indio_dev); 251 + if (indio_dev->ring == NULL) 252 + return -ENOMEM; 253 + indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER; 254 + 255 + indio_dev->ring->access.rip_lots = &sca3000_rip_hw_rb; 256 + indio_dev->ring->access.get_length = &sca3000_ring_get_length; 257 + indio_dev->ring->access.get_bpd = &sca3000_ring_get_bpd; 258 + 259 + return 0; 260 + } 261 + 262 + void sca3000_unconfigure_ring(struct iio_dev *indio_dev) 263 + { 264 + sca3000_rb_free(indio_dev->ring); 265 + } 266 + 267 + static inline 268 + int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state) 269 + { 270 + struct sca3000_state *st = indio_dev->dev_data; 271 + int ret; 272 + u8 *rx; 273 + 274 + mutex_lock(&st->lock); 275 + ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); 276 + if (ret) 277 + goto error_ret; 278 + if (state) { 279 + printk(KERN_INFO "supposedly enabling ring buffer\n"); 280 + ret = sca3000_write_reg(st, 281 + SCA3000_REG_ADDR_MODE, 282 + (rx[1] | SCA3000_RING_BUF_ENABLE)); 283 + } else 284 + ret = sca3000_write_reg(st, 285 + SCA3000_REG_ADDR_MODE, 286 + (rx[1] & ~SCA3000_RING_BUF_ENABLE)); 287 + kfree(rx); 288 + error_ret: 289 + mutex_unlock(&st->lock); 290 + 291 + return ret; 292 + } 293 + /** 294 + * sca3000_hw_ring_preenable() hw ring buffer preenable function 295 + * 296 + * Very simple enable function as the chip will allows normal reads 297 + * during ring buffer operation so as long as it is indeed running 298 + * before we notify the core, the precise ordering does not matter. 299 + **/ 300 + static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev) 301 + { 302 + return __sca3000_hw_ring_state_set(indio_dev, 1); 303 + } 304 + 305 + static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev) 306 + { 307 + return __sca3000_hw_ring_state_set(indio_dev, 0); 308 + } 309 + 310 + void sca3000_register_ring_funcs(struct iio_dev *indio_dev) 311 + { 312 + indio_dev->ring->preenable = &sca3000_hw_ring_preenable; 313 + indio_dev->ring->postdisable = &sca3000_hw_ring_postdisable; 314 + } 315 + 316 + /** 317 + * sca3000_ring_int_process() ring specific interrupt handling. 318 + * 319 + * This is only split from the main interrupt handler so as to 320 + * reduce the amount of code if the ring buffer is not enabled. 321 + **/ 322 + void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring) 323 + { 324 + if (val & SCA3000_INT_STATUS_THREE_QUARTERS) 325 + iio_push_or_escallate_ring_event(ring, 326 + IIO_EVENT_CODE_RING_75_FULL, 327 + 0); 328 + else if (val & SCA3000_INT_STATUS_HALF) 329 + iio_push_ring_event(ring, 330 + IIO_EVENT_CODE_RING_50_FULL, 0); 331 + }
+22
drivers/staging/iio/ring_hw.h
··· 1 + /* 2 + * ring_hw.h - common functionality for iio hardware ring buffers 3 + * 4 + * This program is free software; you can redistribute it and/or modify it 5 + * under the terms of the GNU General Public License version 2 as published by 6 + * the Free Software Foundation. 7 + * 8 + * Copyright (c) 2009 Jonathan Cameron <jic23@cam.ac.uk> 9 + * 10 + */ 11 + 12 + /** 13 + * struct iio_hw_ring_buffer- hardware ring buffer 14 + * @buf: generic ring buffer elements 15 + * @private: device specific data 16 + */ 17 + struct iio_hw_ring_buffer { 18 + struct iio_ring_buffer buf; 19 + void *private; 20 + }; 21 + 22 + #define iio_to_hw_ring_buf(r) container_of(r, struct iio_hw_ring_buffer, buf)