| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* sca3000_core.c -- support VTI sca3000 series accelerometers via SPI |
| 4 |
* |
| 5 |
* Copyright (c) 2009 Jonathan Cameron <[email protected]> |
| 6 |
* |
| 7 |
* See industrialio/accels/sca3000.h for comments. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/interrupt.h> |
| 11 |
#include <linux/fs.h> |
| 12 |
#include <linux/device.h> |
| 13 |
#include <linux/slab.h> |
| 14 |
#include <linux/kernel.h> |
| 15 |
#include <linux/spi/spi.h> |
| 16 |
#include <linux/sysfs.h> |
| 17 |
#include <linux/module.h> |
| 18 |
#include <linux/uaccess.h> |
| 19 |
#include <linux/iio/iio.h> |
| 20 |
#include <linux/iio/sysfs.h> |
| 21 |
#include <linux/iio/events.h> |
| 22 |
#include <linux/iio/buffer.h> |
| 23 |
#include <linux/iio/kfifo_buf.h> |
| 24 |
|
| 25 |
#define SCA3000_WRITE_REG(a) (((a) << 2) | 0x02) |
| 26 |
#define SCA3000_READ_REG(a) ((a) << 2) |
| 27 |
|
| 28 |
#define SCA3000_REG_REVID_ADDR 0x00 |
| 29 |
#define SCA3000_REG_REVID_MAJOR_MASK GENMASK(8, 4) |
| 30 |
#define SCA3000_REG_REVID_MINOR_MASK GENMASK(3, 0) |
| 31 |
|
| 32 |
#define SCA3000_REG_STATUS_ADDR 0x02 |
| 33 |
#define SCA3000_LOCKED BIT(5) |
| 34 |
#define SCA3000_EEPROM_CS_ERROR BIT(1) |
| 35 |
#define SCA3000_SPI_FRAME_ERROR BIT(0) |
| 36 |
|
| 37 |
/* All reads done using register decrement so no need to directly access LSBs */ |
| 38 |
#define SCA3000_REG_X_MSB_ADDR 0x05 |
| 39 |
#define SCA3000_REG_Y_MSB_ADDR 0x07 |
| 40 |
#define SCA3000_REG_Z_MSB_ADDR 0x09 |
| 41 |
|
| 42 |
#define SCA3000_REG_RING_OUT_ADDR 0x0f |
| 43 |
|
| 44 |
/* Temp read untested - the e05 doesn't have the sensor */ |
| 45 |
#define SCA3000_REG_TEMP_MSB_ADDR 0x13 |
| 46 |
|
| 47 |
#define SCA3000_REG_MODE_ADDR 0x14 |
| 48 |
#define SCA3000_MODE_PROT_MASK 0x28 |
| 49 |
#define SCA3000_REG_MODE_RING_BUF_ENABLE BIT(7) |
| 50 |
#define SCA3000_REG_MODE_RING_BUF_8BIT BIT(6) |
| 51 |
|
| 52 |
/* |
| 53 |
* Free fall detection triggers an interrupt if the acceleration |
| 54 |
* is below a threshold for equivalent of 25cm drop |
| 55 |
*/ |
| 56 |
#define SCA3000_REG_MODE_FREE_FALL_DETECT BIT(4) |
| 57 |
#define SCA3000_REG_MODE_MEAS_MODE_NORMAL 0x00 |
| 58 |
#define SCA3000_REG_MODE_MEAS_MODE_OP_1 0x01 |
| 59 |
#define SCA3000_REG_MODE_MEAS_MODE_OP_2 0x02 |
| 60 |
|
| 61 |
/* |
| 62 |
* In motion detection mode the accelerations are band pass filtered |
| 63 |
* (approx 1 - 25Hz) and then a programmable threshold used to trigger |
| 64 |
* and interrupt. |
| 65 |
*/ |
| 66 |
#define SCA3000_REG_MODE_MEAS_MODE_MOT_DET 0x03 |
| 67 |
#define SCA3000_REG_MODE_MODE_MASK 0x03 |
| 68 |
|
| 69 |
#define SCA3000_REG_BUF_COUNT_ADDR 0x15 |
| 70 |
|
| 71 |
#define SCA3000_REG_INT_STATUS_ADDR 0x16 |
| 72 |
#define SCA3000_REG_INT_STATUS_THREE_QUARTERS BIT(7) |
| 73 |
#define SCA3000_REG_INT_STATUS_HALF BIT(6) |
| 74 |
|
| 75 |
#define SCA3000_INT_STATUS_FREE_FALL BIT(3) |
| 76 |
#define SCA3000_INT_STATUS_Y_TRIGGER BIT(2) |
| 77 |
#define SCA3000_INT_STATUS_X_TRIGGER BIT(1) |
| 78 |
#define SCA3000_INT_STATUS_Z_TRIGGER BIT(0) |
| 79 |
|
| 80 |
/* Used to allow access to multiplexed registers */ |
| 81 |
#define SCA3000_REG_CTRL_SEL_ADDR 0x18 |
| 82 |
/* Only available for SCA3000-D03 and SCA3000-D01 */ |
| 83 |
#define SCA3000_REG_CTRL_SEL_I2C_DISABLE 0x01 |
| 84 |
#define SCA3000_REG_CTRL_SEL_MD_CTRL 0x02 |
| 85 |
#define SCA3000_REG_CTRL_SEL_MD_Y_TH 0x03 |
| 86 |
#define SCA3000_REG_CTRL_SEL_MD_X_TH 0x04 |
| 87 |
#define SCA3000_REG_CTRL_SEL_MD_Z_TH 0x05 |
| 88 |
/* |
| 89 |
* BE VERY CAREFUL WITH THIS, IF 3 BITS ARE NOT SET the device |
| 90 |
* will not function |
| 91 |
*/ |
| 92 |
#define SCA3000_REG_CTRL_SEL_OUT_CTRL 0x0B |
| 93 |
|
| 94 |
#define SCA3000_REG_OUT_CTRL_PROT_MASK 0xE0 |
| 95 |
#define SCA3000_REG_OUT_CTRL_BUF_X_EN 0x10 |
| 96 |
#define SCA3000_REG_OUT_CTRL_BUF_Y_EN 0x08 |
| 97 |
#define SCA3000_REG_OUT_CTRL_BUF_Z_EN 0x04 |
| 98 |
#define SCA3000_REG_OUT_CTRL_BUF_DIV_MASK 0x03 |
| 99 |
#define SCA3000_REG_OUT_CTRL_BUF_DIV_4 0x02 |
| 100 |
#define SCA3000_REG_OUT_CTRL_BUF_DIV_2 0x01 |
| 101 |
|
| 102 |
|
| 103 |
/* |
| 104 |
* Control which motion detector interrupts are on. |
| 105 |
* For now only OR combinations are supported. |
| 106 |
*/ |
| 107 |
#define SCA3000_MD_CTRL_PROT_MASK 0xC0 |
| 108 |
#define SCA3000_MD_CTRL_OR_Y BIT(0) |
| 109 |
#define SCA3000_MD_CTRL_OR_X BIT(1) |
| 110 |
#define SCA3000_MD_CTRL_OR_Z BIT(2) |
| 111 |
/* Currently unsupported */ |
| 112 |
#define SCA3000_MD_CTRL_AND_Y BIT(3) |
| 113 |
#define SCA3000_MD_CTRL_AND_X BIT(4) |
| 114 |
#define SCA3000_MD_CTRL_AND_Z BIT(5) |
| 115 |
|
| 116 |
/* |
| 117 |
* Some control registers of complex access methods requiring this register to |
| 118 |
* be used to remove a lock. |
| 119 |
*/ |
| 120 |
#define SCA3000_REG_UNLOCK_ADDR 0x1e |
| 121 |
|
| 122 |
#define SCA3000_REG_INT_MASK_ADDR 0x21 |
| 123 |
#define SCA3000_REG_INT_MASK_PROT_MASK 0x1C |
| 124 |
|
| 125 |
#define SCA3000_REG_INT_MASK_RING_THREE_QUARTER BIT(7) |
| 126 |
#define SCA3000_REG_INT_MASK_RING_HALF BIT(6) |
| 127 |
|
| 128 |
#define SCA3000_REG_INT_MASK_ALL_INTS 0x02 |
| 129 |
#define SCA3000_REG_INT_MASK_ACTIVE_HIGH 0x01 |
| 130 |
#define SCA3000_REG_INT_MASK_ACTIVE_LOW 0x00 |
| 131 |
/* Values of multiplexed registers (write to ctrl_data after select) */ |
| 132 |
#define SCA3000_REG_CTRL_DATA_ADDR 0x22 |
| 133 |
|
| 134 |
/* |
| 135 |
* Measurement modes available on some sca3000 series chips. Code assumes others |
| 136 |
* may become available in the future. |
| 137 |
* |
| 138 |
* Bypass - Bypass the low-pass filter in the signal channel so as to increase |
| 139 |
* signal bandwidth. |
| 140 |
* |
| 141 |
* Narrow - Narrow low-pass filtering of the signal channel and half output |
| 142 |
* data rate by decimation. |
| 143 |
* |
| 144 |
* Wide - Widen low-pass filtering of signal channel to increase bandwidth |
| 145 |
*/ |
| 146 |
#define SCA3000_OP_MODE_BYPASS 0x01 |
| 147 |
#define SCA3000_OP_MODE_NARROW 0x02 |
| 148 |
#define SCA3000_OP_MODE_WIDE 0x04 |
| 149 |
#define SCA3000_MAX_TX 6 |
| 150 |
#define SCA3000_MAX_RX 2 |
| 151 |
|
| 152 |
/** |
| 153 |
* struct sca3000_state - device instance state information |
| 154 |
* @us: the associated spi device |
| 155 |
* @info: chip variant information |
| 156 |
* @mo_det_use_count: reference counter for the motion detection unit |
| 157 |
* @lock: lock used to protect elements of sca3000_state |
| 158 |
* and the underlying device state. |
| 159 |
* @tx: dma-able transmit buffer |
| 160 |
* @rx: dma-able receive buffer |
| 161 |
**/ |
| 162 |
struct sca3000_state { |
| 163 |
struct spi_device *us; |
| 164 |
const struct sca3000_chip_info *info; |
| 165 |
int mo_det_use_count; |
| 166 |
struct mutex lock; |
| 167 |
/* Can these share a cacheline ? */ |
| 168 |
u8 rx[384] __aligned(IIO_DMA_MINALIGN); |
| 169 |
u8 tx[6] __aligned(IIO_DMA_MINALIGN); |
| 170 |
}; |
| 171 |
|
| 172 |
/** |
| 173 |
* struct sca3000_chip_info - model dependent parameters |
| 174 |
* @scale: scale * 10^-6 |
| 175 |
* @temp_output: some devices have temperature sensors. |
| 176 |
* @measurement_mode_freq: normal mode sampling frequency |
| 177 |
* @measurement_mode_3db_freq: 3db cutoff frequency of the low pass filter for |
| 178 |
* the normal measurement mode. |
| 179 |
* @option_mode_1: first optional mode. Not all models have one |
| 180 |
* @option_mode_1_freq: option mode 1 sampling frequency |
| 181 |
* @option_mode_1_3db_freq: 3db cutoff frequency of the low pass filter for |
| 182 |
* the first option mode. |
| 183 |
* @option_mode_2: second optional mode. Not all chips have one |
| 184 |
* @option_mode_2_freq: option mode 2 sampling frequency |
| 185 |
* @option_mode_2_3db_freq: 3db cutoff frequency of the low pass filter for |
| 186 |
* the second option mode. |
| 187 |
* @mot_det_mult_xz: Bit wise multipliers to calculate the threshold |
| 188 |
* for motion detection in the x and z axis. |
| 189 |
* @mot_det_mult_y: Bit wise multipliers to calculate the threshold |
| 190 |
* for motion detection in the y axis. |
| 191 |
* |
| 192 |
* This structure is used to hold information about the functionality of a given |
| 193 |
* sca3000 variant. |
| 194 |
**/ |
| 195 |
struct sca3000_chip_info { |
| 196 |
unsigned int scale; |
| 197 |
bool temp_output; |
| 198 |
int measurement_mode_freq; |
| 199 |
int measurement_mode_3db_freq; |
| 200 |
int option_mode_1; |
| 201 |
int option_mode_1_freq; |
| 202 |
int option_mode_1_3db_freq; |
| 203 |
int option_mode_2; |
| 204 |
int option_mode_2_freq; |
| 205 |
int option_mode_2_3db_freq; |
| 206 |
int mot_det_mult_xz[6]; |
| 207 |
int mot_det_mult_y[7]; |
| 208 |
}; |
| 209 |
|
| 210 |
enum sca3000_variant { |
| 211 |
d01, |
| 212 |
e02, |
| 213 |
e04, |
| 214 |
e05, |
| 215 |
}; |
| 216 |
|
| 217 |
/* |
| 218 |
* Note where option modes are not defined, the chip simply does not |
| 219 |
* support any. |
| 220 |
* Other chips in the sca3000 series use i2c and are not included here. |
| 221 |
* |
| 222 |
* Some of these devices are only listed in the family data sheet and |
| 223 |
* do not actually appear to be available. |
| 224 |
*/ |
| 225 |
static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = { |
| 226 |
[d01] = { |
| 227 |
.scale = 7357, |
| 228 |
.temp_output = true, |
| 229 |
.measurement_mode_freq = 250, |
| 230 |
.measurement_mode_3db_freq = 45, |
| 231 |
.option_mode_1 = SCA3000_OP_MODE_BYPASS, |
| 232 |
.option_mode_1_freq = 250, |
| 233 |
.option_mode_1_3db_freq = 70, |
| 234 |
.mot_det_mult_xz = {50, 100, 200, 350, 650, 1300}, |
| 235 |
.mot_det_mult_y = {50, 100, 150, 250, 450, 850, 1750}, |
| 236 |
}, |
| 237 |
[e02] = { |
| 238 |
.scale = 9810, |
| 239 |
.measurement_mode_freq = 125, |
| 240 |
.measurement_mode_3db_freq = 40, |
| 241 |
.option_mode_1 = SCA3000_OP_MODE_NARROW, |
| 242 |
.option_mode_1_freq = 63, |
| 243 |
.option_mode_1_3db_freq = 11, |
| 244 |
.mot_det_mult_xz = {100, 150, 300, 550, 1050, 2050}, |
| 245 |
.mot_det_mult_y = {50, 100, 200, 350, 700, 1350, 2700}, |
| 246 |
}, |
| 247 |
[e04] = { |
| 248 |
.scale = 19620, |
| 249 |
.measurement_mode_freq = 100, |
| 250 |
.measurement_mode_3db_freq = 38, |
| 251 |
.option_mode_1 = SCA3000_OP_MODE_NARROW, |
| 252 |
.option_mode_1_freq = 50, |
| 253 |
.option_mode_1_3db_freq = 9, |
| 254 |
.option_mode_2 = SCA3000_OP_MODE_WIDE, |
| 255 |
.option_mode_2_freq = 400, |
| 256 |
.option_mode_2_3db_freq = 70, |
| 257 |
.mot_det_mult_xz = {200, 300, 600, 1100, 2100, 4100}, |
| 258 |
.mot_det_mult_y = {100, 200, 400, 7000, 1400, 2700, 54000}, |
| 259 |
}, |
| 260 |
[e05] = { |
| 261 |
.scale = 61313, |
| 262 |
.measurement_mode_freq = 200, |
| 263 |
.measurement_mode_3db_freq = 60, |
| 264 |
.option_mode_1 = SCA3000_OP_MODE_NARROW, |
| 265 |
.option_mode_1_freq = 50, |
| 266 |
.option_mode_1_3db_freq = 9, |
| 267 |
.option_mode_2 = SCA3000_OP_MODE_WIDE, |
| 268 |
.option_mode_2_freq = 400, |
| 269 |
.option_mode_2_3db_freq = 75, |
| 270 |
.mot_det_mult_xz = {600, 900, 1700, 3200, 6100, 11900}, |
| 271 |
.mot_det_mult_y = {300, 600, 1200, 2000, 4100, 7800, 15600}, |
| 272 |
}, |
| 273 |
}; |
| 274 |
|
| 275 |
static int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val) |
| 276 |
{ |
| 277 |
st->tx[0] = SCA3000_WRITE_REG(address); |
| 278 |
st->tx[1] = val; |
| 279 |
return spi_write(st->us, st->tx, 2); |
| 280 |
} |
| 281 |
|
| 282 |
static int sca3000_read_data_short(struct sca3000_state *st, |
| 283 |
u8 reg_address_high, |
| 284 |
int len) |
| 285 |
{ |
| 286 |
struct spi_transfer xfer[2] = { |
| 287 |
{ |
| 288 |
.len = 1, |
| 289 |
.tx_buf = st->tx, |
| 290 |
}, { |
| 291 |
.len = len, |
| 292 |
.rx_buf = st->rx, |
| 293 |
} |
| 294 |
}; |
| 295 |
st->tx[0] = SCA3000_READ_REG(reg_address_high); |
| 296 |
|
| 297 |
return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* sca3000_reg_lock_on() - test if the ctrl register lock is on |
| 302 |
* @st: Driver specific device instance data. |
| 303 |
* |
| 304 |
* Lock must be held. |
| 305 |
**/ |
| 306 |
static int sca3000_reg_lock_on(struct sca3000_state *st) |
| 307 |
{ |
| 308 |
int ret; |
| 309 |
|
| 310 |
ret = sca3000_read_data_short(st, SCA3000_REG_STATUS_ADDR, 1); |
| 311 |
if (ret < 0) |
| 312 |
return ret; |
| 313 |
|
| 314 |
return !(st->rx[0] & SCA3000_LOCKED); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* __sca3000_unlock_reg_lock() - unlock the control registers |
| 319 |
* @st: Driver specific device instance data. |
| 320 |
* |
| 321 |
* Note the device does not appear to support doing this in a single transfer. |
| 322 |
* This should only ever be used as part of ctrl reg read. |
| 323 |
* Lock must be held before calling this |
| 324 |
*/ |
| 325 |
static int __sca3000_unlock_reg_lock(struct sca3000_state *st) |
| 326 |
{ |
| 327 |
struct spi_transfer xfer[3] = { |
| 328 |
{ |
| 329 |
.len = 2, |
| 330 |
.cs_change = 1, |
| 331 |
.tx_buf = st->tx, |
| 332 |
}, { |
| 333 |
.len = 2, |
| 334 |
.cs_change = 1, |
| 335 |
.tx_buf = st->tx + 2, |
| 336 |
}, { |
| 337 |
.len = 2, |
| 338 |
.tx_buf = st->tx + 4, |
| 339 |
}, |
| 340 |
}; |
| 341 |
st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR); |
| 342 |
st->tx[1] = 0x00; |
| 343 |
st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR); |
| 344 |
st->tx[3] = 0x50; |
| 345 |
st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR); |
| 346 |
st->tx[5] = 0xA0; |
| 347 |
|
| 348 |
return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* sca3000_write_ctrl_reg() - write to a lock protect ctrl register |
| 353 |
* @st: Driver specific device instance data. |
| 354 |
* @sel: selects which registers we wish to write to |
| 355 |
* @val: the value to be written |
| 356 |
* |
| 357 |
* Certain control registers are protected against overwriting by the lock |
| 358 |
* register and use a shared write address. This function allows writing of |
| 359 |
* these registers. |
| 360 |
* Lock must be held. |
| 361 |
*/ |
| 362 |
static int sca3000_write_ctrl_reg(struct sca3000_state *st, |
| 363 |
u8 sel, |
| 364 |
uint8_t val) |
| 365 |
{ |
| 366 |
int ret; |
| 367 |
|
| 368 |
ret = sca3000_reg_lock_on(st); |
| 369 |
if (ret < 0) |
| 370 |
return ret; |
| 371 |
if (ret) { |
| 372 |
ret = __sca3000_unlock_reg_lock(st); |
| 373 |
if (ret) |
| 374 |
return ret; |
| 375 |
} |
| 376 |
|
| 377 |
/* Set the control select register */ |
| 378 |
ret = sca3000_write_reg(st, SCA3000_REG_CTRL_SEL_ADDR, sel); |
| 379 |
if (ret) |
| 380 |
return ret; |
| 381 |
|
| 382 |
/* Write the actual value into the register */ |
| 383 |
return sca3000_write_reg(st, SCA3000_REG_CTRL_DATA_ADDR, val); |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* sca3000_read_ctrl_reg() - read from lock protected control register. |
| 388 |
* @st: Driver specific device instance data. |
| 389 |
* @ctrl_reg: Which ctrl register do we want to read. |
| 390 |
* |
| 391 |
* Lock must be held. |
| 392 |
*/ |
| 393 |
static int sca3000_read_ctrl_reg(struct sca3000_state *st, |
| 394 |
u8 ctrl_reg) |
| 395 |
{ |
| 396 |
int ret; |
| 397 |
|
| 398 |
ret = sca3000_reg_lock_on(st); |
| 399 |
if (ret < 0) |
| 400 |
return ret; |
| 401 |
if (ret) { |
| 402 |
ret = __sca3000_unlock_reg_lock(st); |
| 403 |
if (ret) |
| 404 |
return ret; |
| 405 |
} |
| 406 |
/* Set the control select register */ |
| 407 |
ret = sca3000_write_reg(st, SCA3000_REG_CTRL_SEL_ADDR, ctrl_reg); |
| 408 |
if (ret) |
| 409 |
return ret; |
| 410 |
ret = sca3000_read_data_short(st, SCA3000_REG_CTRL_DATA_ADDR, 1); |
| 411 |
if (ret) |
| 412 |
return ret; |
| 413 |
return st->rx[0]; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* sca3000_print_rev() - sysfs interface to read the chip revision number |
| 418 |
* @indio_dev: Device instance specific generic IIO data. |
| 419 |
* Driver specific device instance data can be obtained via |
| 420 |
* iio_priv(indio_dev) |
| 421 |
*/ |
| 422 |
static int sca3000_print_rev(struct iio_dev *indio_dev) |
| 423 |
{ |
| 424 |
int ret; |
| 425 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 426 |
|
| 427 |
mutex_lock(&st->lock); |
| 428 |
ret = sca3000_read_data_short(st, SCA3000_REG_REVID_ADDR, 1); |
| 429 |
if (ret < 0) |
| 430 |
goto error_ret; |
| 431 |
dev_info(&indio_dev->dev, |
| 432 |
"sca3000 revision major=%lu, minor=%lu\n", |
| 433 |
st->rx[0] & SCA3000_REG_REVID_MAJOR_MASK, |
| 434 |
st->rx[0] & SCA3000_REG_REVID_MINOR_MASK); |
| 435 |
error_ret: |
| 436 |
mutex_unlock(&st->lock); |
| 437 |
|
| 438 |
return ret; |
| 439 |
} |
| 440 |
|
| 441 |
static ssize_t |
| 442 |
sca3000_show_available_3db_freqs(struct device *dev, |
| 443 |
struct device_attribute *attr, |
| 444 |
char *buf) |
| 445 |
{ |
| 446 |
struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 447 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 448 |
int len; |
| 449 |
|
| 450 |
len = sprintf(buf, "%d", st->info->measurement_mode_3db_freq); |
| 451 |
if (st->info->option_mode_1) |
| 452 |
len += sprintf(buf + len, " %d", |
| 453 |
st->info->option_mode_1_3db_freq); |
| 454 |
if (st->info->option_mode_2) |
| 455 |
len += sprintf(buf + len, " %d", |
| 456 |
st->info->option_mode_2_3db_freq); |
| 457 |
len += sprintf(buf + len, "\n"); |
| 458 |
|
| 459 |
return len; |
| 460 |
} |
| 461 |
|
| 462 |
static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available, |
| 463 |
S_IRUGO, sca3000_show_available_3db_freqs, |
| 464 |
NULL, 0); |
| 465 |
|
| 466 |
static const struct iio_event_spec sca3000_event = { |
| 467 |
.type = IIO_EV_TYPE_MAG, |
| 468 |
.dir = IIO_EV_DIR_RISING, |
| 469 |
.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), |
| 470 |
}; |
| 471 |
|
| 472 |
/* |
| 473 |
* Note the hack in the number of bits to pretend we have 2 more than |
| 474 |
* we do in the fifo. |
| 475 |
*/ |
| 476 |
#define SCA3000_CHAN(index, mod) \ |
| 477 |
{ \ |
| 478 |
.type = IIO_ACCEL, \ |
| 479 |
.modified = 1, \ |
| 480 |
.channel2 = mod, \ |
| 481 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 482 |
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |\ |
| 483 |
BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),\ |
| 484 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\ |
| 485 |
.address = index, \ |
| 486 |
.scan_index = index, \ |
| 487 |
.scan_type = { \ |
| 488 |
.sign = 's', \ |
| 489 |
.realbits = 13, \ |
| 490 |
.storagebits = 16, \ |
| 491 |
.shift = 3, \ |
| 492 |
.endianness = IIO_BE, \ |
| 493 |
}, \ |
| 494 |
.event_spec = &sca3000_event, \ |
| 495 |
.num_event_specs = 1, \ |
| 496 |
} |
| 497 |
|
| 498 |
static const struct iio_event_spec sca3000_freefall_event_spec = { |
| 499 |
.type = IIO_EV_TYPE_MAG, |
| 500 |
.dir = IIO_EV_DIR_FALLING, |
| 501 |
.mask_separate = BIT(IIO_EV_INFO_ENABLE) | |
| 502 |
BIT(IIO_EV_INFO_PERIOD), |
| 503 |
}; |
| 504 |
|
| 505 |
static const struct iio_chan_spec sca3000_channels[] = { |
| 506 |
SCA3000_CHAN(0, IIO_MOD_X), |
| 507 |
SCA3000_CHAN(1, IIO_MOD_Y), |
| 508 |
SCA3000_CHAN(2, IIO_MOD_Z), |
| 509 |
{ |
| 510 |
.type = IIO_ACCEL, |
| 511 |
.modified = 1, |
| 512 |
.channel2 = IIO_MOD_X_AND_Y_AND_Z, |
| 513 |
.scan_index = -1, /* Fake channel */ |
| 514 |
.event_spec = &sca3000_freefall_event_spec, |
| 515 |
.num_event_specs = 1, |
| 516 |
}, |
| 517 |
}; |
| 518 |
|
| 519 |
static const struct iio_chan_spec sca3000_channels_with_temp[] = { |
| 520 |
SCA3000_CHAN(0, IIO_MOD_X), |
| 521 |
SCA3000_CHAN(1, IIO_MOD_Y), |
| 522 |
SCA3000_CHAN(2, IIO_MOD_Z), |
| 523 |
{ |
| 524 |
.type = IIO_TEMP, |
| 525 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 526 |
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | |
| 527 |
BIT(IIO_CHAN_INFO_OFFSET), |
| 528 |
/* No buffer support */ |
| 529 |
.scan_index = -1, |
| 530 |
.scan_type = { |
| 531 |
.sign = 'u', |
| 532 |
.realbits = 9, |
| 533 |
.storagebits = 16, |
| 534 |
.shift = 5, |
| 535 |
.endianness = IIO_BE, |
| 536 |
}, |
| 537 |
}, |
| 538 |
{ |
| 539 |
.type = IIO_ACCEL, |
| 540 |
.modified = 1, |
| 541 |
.channel2 = IIO_MOD_X_AND_Y_AND_Z, |
| 542 |
.scan_index = -1, /* Fake channel */ |
| 543 |
.event_spec = &sca3000_freefall_event_spec, |
| 544 |
.num_event_specs = 1, |
| 545 |
}, |
| 546 |
}; |
| 547 |
|
| 548 |
static u8 sca3000_addresses[3][3] = { |
| 549 |
[0] = {SCA3000_REG_X_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_X_TH, |
| 550 |
SCA3000_MD_CTRL_OR_X}, |
| 551 |
[1] = {SCA3000_REG_Y_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_Y_TH, |
| 552 |
SCA3000_MD_CTRL_OR_Y}, |
| 553 |
[2] = {SCA3000_REG_Z_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_Z_TH, |
| 554 |
SCA3000_MD_CTRL_OR_Z}, |
| 555 |
}; |
| 556 |
|
| 557 |
/** |
| 558 |
* __sca3000_get_base_freq() - obtain mode specific base frequency |
| 559 |
* @st: Private driver specific device instance specific state. |
| 560 |
* @info: chip type specific information. |
| 561 |
* @base_freq: Base frequency for the current measurement mode. |
| 562 |
* |
| 563 |
* lock must be held |
| 564 |
*/ |
| 565 |
static inline int __sca3000_get_base_freq(struct sca3000_state *st, |
| 566 |
const struct sca3000_chip_info *info, |
| 567 |
int *base_freq) |
| 568 |
{ |
| 569 |
int ret; |
| 570 |
|
| 571 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 572 |
if (ret) |
| 573 |
return ret; |
| 574 |
|
| 575 |
switch (SCA3000_REG_MODE_MODE_MASK & st->rx[0]) { |
| 576 |
case SCA3000_REG_MODE_MEAS_MODE_NORMAL: |
| 577 |
*base_freq = info->measurement_mode_freq; |
| 578 |
break; |
| 579 |
case SCA3000_REG_MODE_MEAS_MODE_OP_1: |
| 580 |
*base_freq = info->option_mode_1_freq; |
| 581 |
break; |
| 582 |
case SCA3000_REG_MODE_MEAS_MODE_OP_2: |
| 583 |
*base_freq = info->option_mode_2_freq; |
| 584 |
break; |
| 585 |
default: |
| 586 |
ret = -EINVAL; |
| 587 |
} |
| 588 |
return ret; |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* sca3000_read_raw_samp_freq() - read_raw handler for IIO_CHAN_INFO_SAMP_FREQ |
| 593 |
* @st: Private driver specific device instance specific state. |
| 594 |
* @val: The frequency read back. |
| 595 |
* |
| 596 |
* lock must be held |
| 597 |
**/ |
| 598 |
static int sca3000_read_raw_samp_freq(struct sca3000_state *st, int *val) |
| 599 |
{ |
| 600 |
int ret; |
| 601 |
|
| 602 |
ret = __sca3000_get_base_freq(st, st->info, val); |
| 603 |
if (ret) |
| 604 |
return ret; |
| 605 |
|
| 606 |
ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); |
| 607 |
if (ret < 0) |
| 608 |
return ret; |
| 609 |
|
| 610 |
if (*val > 0) { |
| 611 |
ret &= SCA3000_REG_OUT_CTRL_BUF_DIV_MASK; |
| 612 |
switch (ret) { |
| 613 |
case SCA3000_REG_OUT_CTRL_BUF_DIV_2: |
| 614 |
*val /= 2; |
| 615 |
break; |
| 616 |
case SCA3000_REG_OUT_CTRL_BUF_DIV_4: |
| 617 |
*val /= 4; |
| 618 |
break; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
/** |
| 626 |
* sca3000_write_raw_samp_freq() - write_raw handler for IIO_CHAN_INFO_SAMP_FREQ |
| 627 |
* @st: Private driver specific device instance specific state. |
| 628 |
* @val: The frequency desired. |
| 629 |
* |
| 630 |
* lock must be held |
| 631 |
*/ |
| 632 |
static int sca3000_write_raw_samp_freq(struct sca3000_state *st, int val) |
| 633 |
{ |
| 634 |
int ret, base_freq, ctrlval; |
| 635 |
|
| 636 |
ret = __sca3000_get_base_freq(st, st->info, &base_freq); |
| 637 |
if (ret) |
| 638 |
return ret; |
| 639 |
|
| 640 |
ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); |
| 641 |
if (ret < 0) |
| 642 |
return ret; |
| 643 |
|
| 644 |
ctrlval = ret & ~SCA3000_REG_OUT_CTRL_BUF_DIV_MASK; |
| 645 |
|
| 646 |
if (val == base_freq / 2) |
| 647 |
ctrlval |= SCA3000_REG_OUT_CTRL_BUF_DIV_2; |
| 648 |
if (val == base_freq / 4) |
| 649 |
ctrlval |= SCA3000_REG_OUT_CTRL_BUF_DIV_4; |
| 650 |
else if (val != base_freq) |
| 651 |
return -EINVAL; |
| 652 |
|
| 653 |
return sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, |
| 654 |
ctrlval); |
| 655 |
} |
| 656 |
|
| 657 |
static int sca3000_read_3db_freq(struct sca3000_state *st, int *val) |
| 658 |
{ |
| 659 |
int ret; |
| 660 |
|
| 661 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 662 |
if (ret) |
| 663 |
return ret; |
| 664 |
|
| 665 |
/* mask bottom 2 bits - only ones that are relevant */ |
| 666 |
st->rx[0] &= SCA3000_REG_MODE_MODE_MASK; |
| 667 |
switch (st->rx[0]) { |
| 668 |
case SCA3000_REG_MODE_MEAS_MODE_NORMAL: |
| 669 |
*val = st->info->measurement_mode_3db_freq; |
| 670 |
return IIO_VAL_INT; |
| 671 |
case SCA3000_REG_MODE_MEAS_MODE_MOT_DET: |
| 672 |
return -EBUSY; |
| 673 |
case SCA3000_REG_MODE_MEAS_MODE_OP_1: |
| 674 |
*val = st->info->option_mode_1_3db_freq; |
| 675 |
return IIO_VAL_INT; |
| 676 |
case SCA3000_REG_MODE_MEAS_MODE_OP_2: |
| 677 |
*val = st->info->option_mode_2_3db_freq; |
| 678 |
return IIO_VAL_INT; |
| 679 |
default: |
| 680 |
return -EINVAL; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
static int sca3000_write_3db_freq(struct sca3000_state *st, int val) |
| 685 |
{ |
| 686 |
int ret; |
| 687 |
int mode; |
| 688 |
|
| 689 |
if (val == st->info->measurement_mode_3db_freq) |
| 690 |
mode = SCA3000_REG_MODE_MEAS_MODE_NORMAL; |
| 691 |
else if (st->info->option_mode_1 && |
| 692 |
(val == st->info->option_mode_1_3db_freq)) |
| 693 |
mode = SCA3000_REG_MODE_MEAS_MODE_OP_1; |
| 694 |
else if (st->info->option_mode_2 && |
| 695 |
(val == st->info->option_mode_2_3db_freq)) |
| 696 |
mode = SCA3000_REG_MODE_MEAS_MODE_OP_2; |
| 697 |
else |
| 698 |
return -EINVAL; |
| 699 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 700 |
if (ret) |
| 701 |
return ret; |
| 702 |
|
| 703 |
st->rx[0] &= ~SCA3000_REG_MODE_MODE_MASK; |
| 704 |
st->rx[0] |= (mode & SCA3000_REG_MODE_MODE_MASK); |
| 705 |
|
| 706 |
return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, st->rx[0]); |
| 707 |
} |
| 708 |
|
| 709 |
static int sca3000_read_raw(struct iio_dev *indio_dev, |
| 710 |
struct iio_chan_spec const *chan, |
| 711 |
int *val, |
| 712 |
int *val2, |
| 713 |
long mask) |
| 714 |
{ |
| 715 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 716 |
int ret; |
| 717 |
u8 address; |
| 718 |
|
| 719 |
switch (mask) { |
| 720 |
case IIO_CHAN_INFO_RAW: |
| 721 |
mutex_lock(&st->lock); |
| 722 |
if (chan->type == IIO_ACCEL) { |
| 723 |
if (st->mo_det_use_count) { |
| 724 |
mutex_unlock(&st->lock); |
| 725 |
return -EBUSY; |
| 726 |
} |
| 727 |
address = sca3000_addresses[chan->address][0]; |
| 728 |
ret = sca3000_read_data_short(st, address, 2); |
| 729 |
if (ret < 0) { |
| 730 |
mutex_unlock(&st->lock); |
| 731 |
return ret; |
| 732 |
} |
| 733 |
*val = sign_extend32(be16_to_cpup((__be16 *)st->rx) >> |
| 734 |
chan->scan_type.shift, |
| 735 |
chan->scan_type.realbits - 1); |
| 736 |
} else { |
| 737 |
/* get the temperature when available */ |
| 738 |
ret = sca3000_read_data_short(st, |
| 739 |
SCA3000_REG_TEMP_MSB_ADDR, |
| 740 |
2); |
| 741 |
if (ret < 0) { |
| 742 |
mutex_unlock(&st->lock); |
| 743 |
return ret; |
| 744 |
} |
| 745 |
*val = (be16_to_cpup((__be16 *)st->rx) >> |
| 746 |
chan->scan_type.shift) & |
| 747 |
GENMASK(chan->scan_type.realbits - 1, 0); |
| 748 |
} |
| 749 |
mutex_unlock(&st->lock); |
| 750 |
return IIO_VAL_INT; |
| 751 |
case IIO_CHAN_INFO_SCALE: |
| 752 |
*val = 0; |
| 753 |
if (chan->type == IIO_ACCEL) |
| 754 |
*val2 = st->info->scale; |
| 755 |
else /* temperature */ |
| 756 |
*val2 = 555556; |
| 757 |
return IIO_VAL_INT_PLUS_MICRO; |
| 758 |
case IIO_CHAN_INFO_OFFSET: |
| 759 |
*val = -214; |
| 760 |
*val2 = 600000; |
| 761 |
return IIO_VAL_INT_PLUS_MICRO; |
| 762 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 763 |
mutex_lock(&st->lock); |
| 764 |
ret = sca3000_read_raw_samp_freq(st, val); |
| 765 |
mutex_unlock(&st->lock); |
| 766 |
return ret ? ret : IIO_VAL_INT; |
| 767 |
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 768 |
mutex_lock(&st->lock); |
| 769 |
ret = sca3000_read_3db_freq(st, val); |
| 770 |
mutex_unlock(&st->lock); |
| 771 |
return ret; |
| 772 |
default: |
| 773 |
return -EINVAL; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
static int sca3000_write_raw(struct iio_dev *indio_dev, |
| 778 |
struct iio_chan_spec const *chan, |
| 779 |
int val, int val2, long mask) |
| 780 |
{ |
| 781 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 782 |
int ret; |
| 783 |
|
| 784 |
switch (mask) { |
| 785 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 786 |
if (val2) |
| 787 |
return -EINVAL; |
| 788 |
mutex_lock(&st->lock); |
| 789 |
ret = sca3000_write_raw_samp_freq(st, val); |
| 790 |
mutex_unlock(&st->lock); |
| 791 |
return ret; |
| 792 |
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 793 |
if (val2) |
| 794 |
return -EINVAL; |
| 795 |
mutex_lock(&st->lock); |
| 796 |
ret = sca3000_write_3db_freq(st, val); |
| 797 |
mutex_unlock(&st->lock); |
| 798 |
return ret; |
| 799 |
default: |
| 800 |
return -EINVAL; |
| 801 |
} |
| 802 |
|
| 803 |
return ret; |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
* sca3000_read_av_freq() - sysfs function to get available frequencies |
| 808 |
* @dev: Device structure for this device. |
| 809 |
* @attr: Description of the attribute. |
| 810 |
* @buf: Incoming string |
| 811 |
* |
| 812 |
* The later modes are only relevant to the ring buffer - and depend on current |
| 813 |
* mode. Note that data sheet gives rather wide tolerances for these so integer |
| 814 |
* division will give good enough answer and not all chips have them specified |
| 815 |
* at all. |
| 816 |
**/ |
| 817 |
static ssize_t sca3000_read_av_freq(struct device *dev, |
| 818 |
struct device_attribute *attr, |
| 819 |
char *buf) |
| 820 |
{ |
| 821 |
struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 822 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 823 |
int len = 0, ret, val; |
| 824 |
|
| 825 |
mutex_lock(&st->lock); |
| 826 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 827 |
val = st->rx[0]; |
| 828 |
mutex_unlock(&st->lock); |
| 829 |
if (ret) |
| 830 |
return ret; |
| 831 |
|
| 832 |
switch (val & SCA3000_REG_MODE_MODE_MASK) { |
| 833 |
case SCA3000_REG_MODE_MEAS_MODE_NORMAL: |
| 834 |
len += sprintf(buf + len, "%d %d %d\n", |
| 835 |
st->info->measurement_mode_freq, |
| 836 |
st->info->measurement_mode_freq / 2, |
| 837 |
st->info->measurement_mode_freq / 4); |
| 838 |
break; |
| 839 |
case SCA3000_REG_MODE_MEAS_MODE_OP_1: |
| 840 |
len += sprintf(buf + len, "%d %d %d\n", |
| 841 |
st->info->option_mode_1_freq, |
| 842 |
st->info->option_mode_1_freq / 2, |
| 843 |
st->info->option_mode_1_freq / 4); |
| 844 |
break; |
| 845 |
case SCA3000_REG_MODE_MEAS_MODE_OP_2: |
| 846 |
len += sprintf(buf + len, "%d %d %d\n", |
| 847 |
st->info->option_mode_2_freq, |
| 848 |
st->info->option_mode_2_freq / 2, |
| 849 |
st->info->option_mode_2_freq / 4); |
| 850 |
break; |
| 851 |
} |
| 852 |
return len; |
| 853 |
} |
| 854 |
|
| 855 |
/* |
| 856 |
* Should only really be registered if ring buffer support is compiled in. |
| 857 |
* Does no harm however and doing it right would add a fair bit of complexity |
| 858 |
*/ |
| 859 |
static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq); |
| 860 |
|
| 861 |
/* |
| 862 |
* sca3000_read_event_value() - query of a threshold or period |
| 863 |
*/ |
| 864 |
static int sca3000_read_event_value(struct iio_dev *indio_dev, |
| 865 |
const struct iio_chan_spec *chan, |
| 866 |
enum iio_event_type type, |
| 867 |
enum iio_event_direction dir, |
| 868 |
enum iio_event_info info, |
| 869 |
int *val, int *val2) |
| 870 |
{ |
| 871 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 872 |
long ret; |
| 873 |
int i; |
| 874 |
|
| 875 |
switch (info) { |
| 876 |
case IIO_EV_INFO_VALUE: |
| 877 |
mutex_lock(&st->lock); |
| 878 |
ret = sca3000_read_ctrl_reg(st, |
| 879 |
sca3000_addresses[chan->address][1]); |
| 880 |
mutex_unlock(&st->lock); |
| 881 |
if (ret < 0) |
| 882 |
return ret; |
| 883 |
*val = 0; |
| 884 |
if (chan->channel2 == IIO_MOD_Y) |
| 885 |
for_each_set_bit(i, &ret, |
| 886 |
ARRAY_SIZE(st->info->mot_det_mult_y)) |
| 887 |
*val += st->info->mot_det_mult_y[i]; |
| 888 |
else |
| 889 |
for_each_set_bit(i, &ret, |
| 890 |
ARRAY_SIZE(st->info->mot_det_mult_xz)) |
| 891 |
*val += st->info->mot_det_mult_xz[i]; |
| 892 |
|
| 893 |
return IIO_VAL_INT; |
| 894 |
case IIO_EV_INFO_PERIOD: |
| 895 |
*val = 0; |
| 896 |
*val2 = 226000; |
| 897 |
return IIO_VAL_INT_PLUS_MICRO; |
| 898 |
default: |
| 899 |
return -EINVAL; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
/** |
| 904 |
* sca3000_write_event_value() - control of threshold and period |
| 905 |
* @indio_dev: Device instance specific IIO information. |
| 906 |
* @chan: Description of the channel for which the event is being |
| 907 |
* configured. |
| 908 |
* @type: The type of event being configured, here magnitude rising |
| 909 |
* as everything else is read only. |
| 910 |
* @dir: Direction of the event (here rising) |
| 911 |
* @info: What information about the event are we configuring. |
| 912 |
* Here the threshold only. |
| 913 |
* @val: Integer part of the value being written.. |
| 914 |
* @val2: Non integer part of the value being written. Here always 0. |
| 915 |
*/ |
| 916 |
static int sca3000_write_event_value(struct iio_dev *indio_dev, |
| 917 |
const struct iio_chan_spec *chan, |
| 918 |
enum iio_event_type type, |
| 919 |
enum iio_event_direction dir, |
| 920 |
enum iio_event_info info, |
| 921 |
int val, int val2) |
| 922 |
{ |
| 923 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 924 |
int ret; |
| 925 |
int i; |
| 926 |
u8 nonlinear = 0; |
| 927 |
|
| 928 |
if (chan->channel2 == IIO_MOD_Y) { |
| 929 |
i = ARRAY_SIZE(st->info->mot_det_mult_y); |
| 930 |
while (i > 0) |
| 931 |
if (val >= st->info->mot_det_mult_y[--i]) { |
| 932 |
nonlinear |= (1 << i); |
| 933 |
val -= st->info->mot_det_mult_y[i]; |
| 934 |
} |
| 935 |
} else { |
| 936 |
i = ARRAY_SIZE(st->info->mot_det_mult_xz); |
| 937 |
while (i > 0) |
| 938 |
if (val >= st->info->mot_det_mult_xz[--i]) { |
| 939 |
nonlinear |= (1 << i); |
| 940 |
val -= st->info->mot_det_mult_xz[i]; |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
mutex_lock(&st->lock); |
| 945 |
ret = sca3000_write_ctrl_reg(st, |
| 946 |
sca3000_addresses[chan->address][1], |
| 947 |
nonlinear); |
| 948 |
mutex_unlock(&st->lock); |
| 949 |
|
| 950 |
return ret; |
| 951 |
} |
| 952 |
|
| 953 |
static struct attribute *sca3000_attributes[] = { |
| 954 |
&iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr, |
| 955 |
&iio_dev_attr_sampling_frequency_available.dev_attr.attr, |
| 956 |
NULL, |
| 957 |
}; |
| 958 |
|
| 959 |
static const struct attribute_group sca3000_attribute_group = { |
| 960 |
.attrs = sca3000_attributes, |
| 961 |
}; |
| 962 |
|
| 963 |
static int sca3000_read_data(struct sca3000_state *st, |
| 964 |
u8 reg_address_high, |
| 965 |
u8 *rx, |
| 966 |
int len) |
| 967 |
{ |
| 968 |
int ret; |
| 969 |
struct spi_transfer xfer[2] = { |
| 970 |
{ |
| 971 |
.len = 1, |
| 972 |
.tx_buf = st->tx, |
| 973 |
}, { |
| 974 |
.len = len, |
| 975 |
.rx_buf = rx, |
| 976 |
} |
| 977 |
}; |
| 978 |
|
| 979 |
st->tx[0] = SCA3000_READ_REG(reg_address_high); |
| 980 |
ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); |
| 981 |
if (ret) { |
| 982 |
dev_err(&st->us->dev, "problem reading register\n"); |
| 983 |
return ret; |
| 984 |
} |
| 985 |
|
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
/** |
| 990 |
* sca3000_ring_int_process() - ring specific interrupt handling. |
| 991 |
* @val: Value of the interrupt status register. |
| 992 |
* @indio_dev: Device instance specific IIO device structure. |
| 993 |
*/ |
| 994 |
static void sca3000_ring_int_process(u8 val, struct iio_dev *indio_dev) |
| 995 |
{ |
| 996 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 997 |
int ret, i, num_available; |
| 998 |
|
| 999 |
mutex_lock(&st->lock); |
| 1000 |
|
| 1001 |
if (val & SCA3000_REG_INT_STATUS_HALF) { |
| 1002 |
ret = sca3000_read_data_short(st, SCA3000_REG_BUF_COUNT_ADDR, |
| 1003 |
1); |
| 1004 |
if (ret) |
| 1005 |
goto error_ret; |
| 1006 |
num_available = st->rx[0]; |
| 1007 |
/* |
| 1008 |
* num_available is the total number of samples available |
| 1009 |
* i.e. number of time points * number of channels. |
| 1010 |
*/ |
| 1011 |
ret = sca3000_read_data(st, SCA3000_REG_RING_OUT_ADDR, st->rx, |
| 1012 |
num_available * 2); |
| 1013 |
if (ret) |
| 1014 |
goto error_ret; |
| 1015 |
for (i = 0; i < num_available / 3; i++) { |
| 1016 |
/* |
| 1017 |
* Dirty hack to cover for 11 bit in fifo, 13 bit |
| 1018 |
* direct reading. |
| 1019 |
* |
| 1020 |
* In theory the bottom two bits are undefined. |
| 1021 |
* In reality they appear to always be 0. |
| 1022 |
*/ |
| 1023 |
iio_push_to_buffers(indio_dev, st->rx + i * 3 * 2); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
error_ret: |
| 1027 |
mutex_unlock(&st->lock); |
| 1028 |
} |
| 1029 |
|
| 1030 |
/** |
| 1031 |
* sca3000_event_handler() - handling ring and non ring events |
| 1032 |
* @irq: The irq being handled. |
| 1033 |
* @private: struct iio_device pointer for the device. |
| 1034 |
* |
| 1035 |
* Ring related interrupt handler. Depending on event, push to |
| 1036 |
* the ring buffer event chrdev or the event one. |
| 1037 |
* |
| 1038 |
* This function is complicated by the fact that the devices can signify ring |
| 1039 |
* and non ring events via the same interrupt line and they can only |
| 1040 |
* be distinguished via a read of the relevant status register. |
| 1041 |
*/ |
| 1042 |
static irqreturn_t sca3000_event_handler(int irq, void *private) |
| 1043 |
{ |
| 1044 |
struct iio_dev *indio_dev = private; |
| 1045 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1046 |
int ret, val; |
| 1047 |
s64 last_timestamp = iio_get_time_ns(indio_dev); |
| 1048 |
|
| 1049 |
/* |
| 1050 |
* Could lead if badly timed to an extra read of status reg, |
| 1051 |
* but ensures no interrupt is missed. |
| 1052 |
*/ |
| 1053 |
mutex_lock(&st->lock); |
| 1054 |
ret = sca3000_read_data_short(st, SCA3000_REG_INT_STATUS_ADDR, 1); |
| 1055 |
val = st->rx[0]; |
| 1056 |
mutex_unlock(&st->lock); |
| 1057 |
if (ret) |
| 1058 |
goto done; |
| 1059 |
|
| 1060 |
sca3000_ring_int_process(val, indio_dev); |
| 1061 |
|
| 1062 |
if (val & SCA3000_INT_STATUS_FREE_FALL) |
| 1063 |
iio_push_event(indio_dev, |
| 1064 |
IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1065 |
0, |
| 1066 |
IIO_MOD_X_AND_Y_AND_Z, |
| 1067 |
IIO_EV_TYPE_MAG, |
| 1068 |
IIO_EV_DIR_FALLING), |
| 1069 |
last_timestamp); |
| 1070 |
|
| 1071 |
if (val & SCA3000_INT_STATUS_Y_TRIGGER) |
| 1072 |
iio_push_event(indio_dev, |
| 1073 |
IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1074 |
0, |
| 1075 |
IIO_MOD_Y, |
| 1076 |
IIO_EV_TYPE_MAG, |
| 1077 |
IIO_EV_DIR_RISING), |
| 1078 |
last_timestamp); |
| 1079 |
|
| 1080 |
if (val & SCA3000_INT_STATUS_X_TRIGGER) |
| 1081 |
iio_push_event(indio_dev, |
| 1082 |
IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1083 |
0, |
| 1084 |
IIO_MOD_X, |
| 1085 |
IIO_EV_TYPE_MAG, |
| 1086 |
IIO_EV_DIR_RISING), |
| 1087 |
last_timestamp); |
| 1088 |
|
| 1089 |
if (val & SCA3000_INT_STATUS_Z_TRIGGER) |
| 1090 |
iio_push_event(indio_dev, |
| 1091 |
IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1092 |
0, |
| 1093 |
IIO_MOD_Z, |
| 1094 |
IIO_EV_TYPE_MAG, |
| 1095 |
IIO_EV_DIR_RISING), |
| 1096 |
last_timestamp); |
| 1097 |
|
| 1098 |
done: |
| 1099 |
return IRQ_HANDLED; |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* |
| 1103 |
* sca3000_read_event_config() what events are enabled |
| 1104 |
*/ |
| 1105 |
static int sca3000_read_event_config(struct iio_dev *indio_dev, |
| 1106 |
const struct iio_chan_spec *chan, |
| 1107 |
enum iio_event_type type, |
| 1108 |
enum iio_event_direction dir) |
| 1109 |
{ |
| 1110 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1111 |
int ret; |
| 1112 |
/* read current value of mode register */ |
| 1113 |
mutex_lock(&st->lock); |
| 1114 |
|
| 1115 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 1116 |
if (ret) |
| 1117 |
goto error_ret; |
| 1118 |
|
| 1119 |
switch (chan->channel2) { |
| 1120 |
case IIO_MOD_X_AND_Y_AND_Z: |
| 1121 |
ret = !!(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT); |
| 1122 |
break; |
| 1123 |
case IIO_MOD_X: |
| 1124 |
case IIO_MOD_Y: |
| 1125 |
case IIO_MOD_Z: |
| 1126 |
/* |
| 1127 |
* Motion detection mode cannot run at the same time as |
| 1128 |
* acceleration data being read. |
| 1129 |
*/ |
| 1130 |
if ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) |
| 1131 |
!= SCA3000_REG_MODE_MEAS_MODE_MOT_DET) { |
| 1132 |
ret = 0; |
| 1133 |
} else { |
| 1134 |
ret = sca3000_read_ctrl_reg(st, |
| 1135 |
SCA3000_REG_CTRL_SEL_MD_CTRL); |
| 1136 |
if (ret < 0) |
| 1137 |
goto error_ret; |
| 1138 |
/* only supporting logical or's for now */ |
| 1139 |
ret = !!(ret & sca3000_addresses[chan->address][2]); |
| 1140 |
} |
| 1141 |
break; |
| 1142 |
default: |
| 1143 |
ret = -EINVAL; |
| 1144 |
} |
| 1145 |
|
| 1146 |
error_ret: |
| 1147 |
mutex_unlock(&st->lock); |
| 1148 |
|
| 1149 |
return ret; |
| 1150 |
} |
| 1151 |
|
| 1152 |
static int sca3000_freefall_set_state(struct iio_dev *indio_dev, bool state) |
| 1153 |
{ |
| 1154 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1155 |
int ret; |
| 1156 |
|
| 1157 |
/* read current value of mode register */ |
| 1158 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 1159 |
if (ret) |
| 1160 |
return ret; |
| 1161 |
|
| 1162 |
/* if off and should be on */ |
| 1163 |
if (state && !(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT)) |
| 1164 |
return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, |
| 1165 |
st->rx[0] | SCA3000_REG_MODE_FREE_FALL_DETECT); |
| 1166 |
/* if on and should be off */ |
| 1167 |
else if (!state && (st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT)) |
| 1168 |
return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, |
| 1169 |
st->rx[0] & ~SCA3000_REG_MODE_FREE_FALL_DETECT); |
| 1170 |
else |
| 1171 |
return 0; |
| 1172 |
} |
| 1173 |
|
| 1174 |
static int sca3000_motion_detect_set_state(struct iio_dev *indio_dev, int axis, |
| 1175 |
bool state) |
| 1176 |
{ |
| 1177 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1178 |
int ret, ctrlval; |
| 1179 |
|
| 1180 |
/* |
| 1181 |
* First read the motion detector config to find out if |
| 1182 |
* this axis is on |
| 1183 |
*/ |
| 1184 |
ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL); |
| 1185 |
if (ret < 0) |
| 1186 |
return ret; |
| 1187 |
ctrlval = ret; |
| 1188 |
/* if off and should be on */ |
| 1189 |
if (state && !(ctrlval & sca3000_addresses[axis][2])) { |
| 1190 |
ret = sca3000_write_ctrl_reg(st, |
| 1191 |
SCA3000_REG_CTRL_SEL_MD_CTRL, |
| 1192 |
ctrlval | |
| 1193 |
sca3000_addresses[axis][2]); |
| 1194 |
if (ret) |
| 1195 |
return ret; |
| 1196 |
st->mo_det_use_count++; |
| 1197 |
} else if (!state && (ctrlval & sca3000_addresses[axis][2])) { |
| 1198 |
ret = sca3000_write_ctrl_reg(st, |
| 1199 |
SCA3000_REG_CTRL_SEL_MD_CTRL, |
| 1200 |
ctrlval & |
| 1201 |
~(sca3000_addresses[axis][2])); |
| 1202 |
if (ret) |
| 1203 |
return ret; |
| 1204 |
st->mo_det_use_count--; |
| 1205 |
} |
| 1206 |
|
| 1207 |
/* read current value of mode register */ |
| 1208 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 1209 |
if (ret) |
| 1210 |
return ret; |
| 1211 |
/* if off and should be on */ |
| 1212 |
if ((st->mo_det_use_count) && |
| 1213 |
((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) |
| 1214 |
!= SCA3000_REG_MODE_MEAS_MODE_MOT_DET)) |
| 1215 |
return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, |
| 1216 |
(st->rx[0] & ~SCA3000_REG_MODE_MODE_MASK) |
| 1217 |
| SCA3000_REG_MODE_MEAS_MODE_MOT_DET); |
| 1218 |
/* if on and should be off */ |
| 1219 |
else if (!(st->mo_det_use_count) && |
| 1220 |
((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) |
| 1221 |
== SCA3000_REG_MODE_MEAS_MODE_MOT_DET)) |
| 1222 |
return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, |
| 1223 |
st->rx[0] & SCA3000_REG_MODE_MODE_MASK); |
| 1224 |
else |
| 1225 |
return 0; |
| 1226 |
} |
| 1227 |
|
| 1228 |
/** |
| 1229 |
* sca3000_write_event_config() - simple on off control for motion detector |
| 1230 |
* @indio_dev: IIO device instance specific structure. Data specific to this |
| 1231 |
* particular driver may be accessed via iio_priv(indio_dev). |
| 1232 |
* @chan: Description of the channel whose event we are configuring. |
| 1233 |
* @type: The type of event. |
| 1234 |
* @dir: The direction of the event. |
| 1235 |
* @state: Desired state of event being configured. |
| 1236 |
* |
| 1237 |
* This is a per axis control, but enabling any will result in the |
| 1238 |
* motion detector unit being enabled. |
| 1239 |
* N.B. enabling motion detector stops normal data acquisition. |
| 1240 |
* There is a complexity in knowing which mode to return to when |
| 1241 |
* this mode is disabled. Currently normal mode is assumed. |
| 1242 |
**/ |
| 1243 |
static int sca3000_write_event_config(struct iio_dev *indio_dev, |
| 1244 |
const struct iio_chan_spec *chan, |
| 1245 |
enum iio_event_type type, |
| 1246 |
enum iio_event_direction dir, |
| 1247 |
bool state) |
| 1248 |
{ |
| 1249 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1250 |
int ret; |
| 1251 |
|
| 1252 |
mutex_lock(&st->lock); |
| 1253 |
switch (chan->channel2) { |
| 1254 |
case IIO_MOD_X_AND_Y_AND_Z: |
| 1255 |
ret = sca3000_freefall_set_state(indio_dev, state); |
| 1256 |
break; |
| 1257 |
|
| 1258 |
case IIO_MOD_X: |
| 1259 |
case IIO_MOD_Y: |
| 1260 |
case IIO_MOD_Z: |
| 1261 |
ret = sca3000_motion_detect_set_state(indio_dev, |
| 1262 |
chan->address, |
| 1263 |
state); |
| 1264 |
break; |
| 1265 |
default: |
| 1266 |
ret = -EINVAL; |
| 1267 |
break; |
| 1268 |
} |
| 1269 |
mutex_unlock(&st->lock); |
| 1270 |
|
| 1271 |
return ret; |
| 1272 |
} |
| 1273 |
|
| 1274 |
static inline |
| 1275 |
int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state) |
| 1276 |
{ |
| 1277 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1278 |
int ret; |
| 1279 |
|
| 1280 |
mutex_lock(&st->lock); |
| 1281 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 1282 |
if (ret) |
| 1283 |
goto error_ret; |
| 1284 |
if (state) { |
| 1285 |
dev_info(&indio_dev->dev, "supposedly enabling ring buffer\n"); |
| 1286 |
ret = sca3000_write_reg(st, |
| 1287 |
SCA3000_REG_MODE_ADDR, |
| 1288 |
(st->rx[0] | SCA3000_REG_MODE_RING_BUF_ENABLE)); |
| 1289 |
} else |
| 1290 |
ret = sca3000_write_reg(st, |
| 1291 |
SCA3000_REG_MODE_ADDR, |
| 1292 |
(st->rx[0] & ~SCA3000_REG_MODE_RING_BUF_ENABLE)); |
| 1293 |
error_ret: |
| 1294 |
mutex_unlock(&st->lock); |
| 1295 |
|
| 1296 |
return ret; |
| 1297 |
} |
| 1298 |
|
| 1299 |
/** |
| 1300 |
* sca3000_hw_ring_preenable() - hw ring buffer preenable function |
| 1301 |
* @indio_dev: structure representing the IIO device. Device instance |
| 1302 |
* specific state can be accessed via iio_priv(indio_dev). |
| 1303 |
* |
| 1304 |
* Very simple enable function as the chip will allows normal reads |
| 1305 |
* during ring buffer operation so as long as it is indeed running |
| 1306 |
* before we notify the core, the precise ordering does not matter. |
| 1307 |
*/ |
| 1308 |
static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev) |
| 1309 |
{ |
| 1310 |
int ret; |
| 1311 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1312 |
|
| 1313 |
mutex_lock(&st->lock); |
| 1314 |
|
| 1315 |
/* Enable the 50% full interrupt */ |
| 1316 |
ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1); |
| 1317 |
if (ret) |
| 1318 |
goto error_unlock; |
| 1319 |
ret = sca3000_write_reg(st, |
| 1320 |
SCA3000_REG_INT_MASK_ADDR, |
| 1321 |
st->rx[0] | SCA3000_REG_INT_MASK_RING_HALF); |
| 1322 |
if (ret) |
| 1323 |
goto error_unlock; |
| 1324 |
|
| 1325 |
mutex_unlock(&st->lock); |
| 1326 |
|
| 1327 |
return __sca3000_hw_ring_state_set(indio_dev, 1); |
| 1328 |
|
| 1329 |
error_unlock: |
| 1330 |
mutex_unlock(&st->lock); |
| 1331 |
|
| 1332 |
return ret; |
| 1333 |
} |
| 1334 |
|
| 1335 |
static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev) |
| 1336 |
{ |
| 1337 |
int ret; |
| 1338 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1339 |
|
| 1340 |
ret = __sca3000_hw_ring_state_set(indio_dev, 0); |
| 1341 |
if (ret) |
| 1342 |
return ret; |
| 1343 |
|
| 1344 |
/* Disable the 50% full interrupt */ |
| 1345 |
mutex_lock(&st->lock); |
| 1346 |
|
| 1347 |
ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1); |
| 1348 |
if (ret) |
| 1349 |
goto unlock; |
| 1350 |
ret = sca3000_write_reg(st, |
| 1351 |
SCA3000_REG_INT_MASK_ADDR, |
| 1352 |
st->rx[0] & ~SCA3000_REG_INT_MASK_RING_HALF); |
| 1353 |
unlock: |
| 1354 |
mutex_unlock(&st->lock); |
| 1355 |
return ret; |
| 1356 |
} |
| 1357 |
|
| 1358 |
static const struct iio_buffer_setup_ops sca3000_ring_setup_ops = { |
| 1359 |
.preenable = &sca3000_hw_ring_preenable, |
| 1360 |
.postdisable = &sca3000_hw_ring_postdisable, |
| 1361 |
}; |
| 1362 |
|
| 1363 |
/** |
| 1364 |
* sca3000_clean_setup() - get the device into a predictable state |
| 1365 |
* @st: Device instance specific private data structure |
| 1366 |
* |
| 1367 |
* Devices use flash memory to store many of the register values |
| 1368 |
* and hence can come up in somewhat unpredictable states. |
| 1369 |
* Hence reset everything on driver load. |
| 1370 |
*/ |
| 1371 |
static int sca3000_clean_setup(struct sca3000_state *st) |
| 1372 |
{ |
| 1373 |
int ret; |
| 1374 |
|
| 1375 |
mutex_lock(&st->lock); |
| 1376 |
/* Ensure all interrupts have been acknowledged */ |
| 1377 |
ret = sca3000_read_data_short(st, SCA3000_REG_INT_STATUS_ADDR, 1); |
| 1378 |
if (ret) |
| 1379 |
goto error_ret; |
| 1380 |
|
| 1381 |
/* Turn off all motion detection channels */ |
| 1382 |
ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL); |
| 1383 |
if (ret < 0) |
| 1384 |
goto error_ret; |
| 1385 |
ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL, |
| 1386 |
ret & SCA3000_MD_CTRL_PROT_MASK); |
| 1387 |
if (ret) |
| 1388 |
goto error_ret; |
| 1389 |
|
| 1390 |
/* Disable ring buffer */ |
| 1391 |
ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); |
| 1392 |
if (ret < 0) |
| 1393 |
goto error_ret; |
| 1394 |
ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, |
| 1395 |
(ret & SCA3000_REG_OUT_CTRL_PROT_MASK) |
| 1396 |
| SCA3000_REG_OUT_CTRL_BUF_X_EN |
| 1397 |
| SCA3000_REG_OUT_CTRL_BUF_Y_EN |
| 1398 |
| SCA3000_REG_OUT_CTRL_BUF_Z_EN |
| 1399 |
| SCA3000_REG_OUT_CTRL_BUF_DIV_4); |
| 1400 |
if (ret) |
| 1401 |
goto error_ret; |
| 1402 |
/* Enable interrupts, relevant to mode and set up as active low */ |
| 1403 |
ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1); |
| 1404 |
if (ret) |
| 1405 |
goto error_ret; |
| 1406 |
ret = sca3000_write_reg(st, |
| 1407 |
SCA3000_REG_INT_MASK_ADDR, |
| 1408 |
(ret & SCA3000_REG_INT_MASK_PROT_MASK) |
| 1409 |
| SCA3000_REG_INT_MASK_ACTIVE_LOW); |
| 1410 |
if (ret) |
| 1411 |
goto error_ret; |
| 1412 |
/* |
| 1413 |
* Select normal measurement mode, free fall off, ring off |
| 1414 |
* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5 |
| 1415 |
* as that occurs in one of the example on the datasheet |
| 1416 |
*/ |
| 1417 |
ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1); |
| 1418 |
if (ret) |
| 1419 |
goto error_ret; |
| 1420 |
ret = sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, |
| 1421 |
(st->rx[0] & SCA3000_MODE_PROT_MASK)); |
| 1422 |
|
| 1423 |
error_ret: |
| 1424 |
mutex_unlock(&st->lock); |
| 1425 |
return ret; |
| 1426 |
} |
| 1427 |
|
| 1428 |
static const struct iio_info sca3000_info = { |
| 1429 |
.attrs = &sca3000_attribute_group, |
| 1430 |
.read_raw = &sca3000_read_raw, |
| 1431 |
.write_raw = &sca3000_write_raw, |
| 1432 |
.read_event_value = &sca3000_read_event_value, |
| 1433 |
.write_event_value = &sca3000_write_event_value, |
| 1434 |
.read_event_config = &sca3000_read_event_config, |
| 1435 |
.write_event_config = &sca3000_write_event_config, |
| 1436 |
}; |
| 1437 |
|
| 1438 |
static int sca3000_probe(struct spi_device *spi) |
| 1439 |
{ |
| 1440 |
int ret; |
| 1441 |
struct sca3000_state *st; |
| 1442 |
struct iio_dev *indio_dev; |
| 1443 |
|
| 1444 |
indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
| 1445 |
if (!indio_dev) |
| 1446 |
return -ENOMEM; |
| 1447 |
|
| 1448 |
st = iio_priv(indio_dev); |
| 1449 |
spi_set_drvdata(spi, indio_dev); |
| 1450 |
st->us = spi; |
| 1451 |
mutex_init(&st->lock); |
| 1452 |
st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi) |
| 1453 |
->driver_data]; |
| 1454 |
|
| 1455 |
indio_dev->name = spi_get_device_id(spi)->name; |
| 1456 |
indio_dev->info = &sca3000_info; |
| 1457 |
if (st->info->temp_output) { |
| 1458 |
indio_dev->channels = sca3000_channels_with_temp; |
| 1459 |
indio_dev->num_channels = |
| 1460 |
ARRAY_SIZE(sca3000_channels_with_temp); |
| 1461 |
} else { |
| 1462 |
indio_dev->channels = sca3000_channels; |
| 1463 |
indio_dev->num_channels = ARRAY_SIZE(sca3000_channels); |
| 1464 |
} |
| 1465 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 1466 |
|
| 1467 |
ret = devm_iio_kfifo_buffer_setup(&spi->dev, indio_dev, |
| 1468 |
&sca3000_ring_setup_ops); |
| 1469 |
if (ret) |
| 1470 |
return ret; |
| 1471 |
|
| 1472 |
if (spi->irq) { |
| 1473 |
ret = request_threaded_irq(spi->irq, |
| 1474 |
NULL, |
| 1475 |
&sca3000_event_handler, |
| 1476 |
IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 1477 |
"sca3000", |
| 1478 |
indio_dev); |
| 1479 |
if (ret) |
| 1480 |
return ret; |
| 1481 |
} |
| 1482 |
ret = sca3000_clean_setup(st); |
| 1483 |
if (ret) |
| 1484 |
goto error_free_irq; |
| 1485 |
|
| 1486 |
ret = sca3000_print_rev(indio_dev); |
| 1487 |
if (ret) |
| 1488 |
goto error_free_irq; |
| 1489 |
|
| 1490 |
ret = iio_device_register(indio_dev); |
| 1491 |
if (ret) |
| 1492 |
goto error_free_irq; |
| 1493 |
|
| 1494 |
return 0; |
| 1495 |
|
| 1496 |
error_free_irq: |
| 1497 |
if (spi->irq) |
| 1498 |
free_irq(spi->irq, indio_dev); |
| 1499 |
|
| 1500 |
return ret; |
| 1501 |
} |
| 1502 |
|
| 1503 |
static int sca3000_stop_all_interrupts(struct sca3000_state *st) |
| 1504 |
{ |
| 1505 |
int ret; |
| 1506 |
|
| 1507 |
mutex_lock(&st->lock); |
| 1508 |
ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1); |
| 1509 |
if (ret) |
| 1510 |
goto error_ret; |
| 1511 |
ret = sca3000_write_reg(st, SCA3000_REG_INT_MASK_ADDR, |
| 1512 |
(st->rx[0] & |
| 1513 |
~(SCA3000_REG_INT_MASK_RING_THREE_QUARTER | |
| 1514 |
SCA3000_REG_INT_MASK_RING_HALF | |
| 1515 |
SCA3000_REG_INT_MASK_ALL_INTS))); |
| 1516 |
error_ret: |
| 1517 |
mutex_unlock(&st->lock); |
| 1518 |
return ret; |
| 1519 |
} |
| 1520 |
|
| 1521 |
static void sca3000_remove(struct spi_device *spi) |
| 1522 |
{ |
| 1523 |
struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 1524 |
struct sca3000_state *st = iio_priv(indio_dev); |
| 1525 |
|
| 1526 |
iio_device_unregister(indio_dev); |
| 1527 |
|
| 1528 |
/* Must ensure no interrupts can be generated after this! */ |
| 1529 |
sca3000_stop_all_interrupts(st); |
| 1530 |
if (spi->irq) |
| 1531 |
free_irq(spi->irq, indio_dev); |
| 1532 |
} |
| 1533 |
|
| 1534 |
static const struct spi_device_id sca3000_id[] = { |
| 1535 |
{"sca3000_d01", d01}, |
| 1536 |
{"sca3000_e02", e02}, |
| 1537 |
{"sca3000_e04", e04}, |
| 1538 |
{"sca3000_e05", e05}, |
| 1539 |
{ } |
| 1540 |
}; |
| 1541 |
MODULE_DEVICE_TABLE(spi, sca3000_id); |
| 1542 |
|
| 1543 |
static struct spi_driver sca3000_driver = { |
| 1544 |
.driver = { |
| 1545 |
.name = "sca3000", |
| 1546 |
}, |
| 1547 |
.probe = sca3000_probe, |
| 1548 |
.remove = sca3000_remove, |
| 1549 |
.id_table = sca3000_id, |
| 1550 |
}; |
| 1551 |
module_spi_driver(sca3000_driver); |
| 1552 |
|
| 1553 |
MODULE_AUTHOR("Jonathan Cameron <[email protected]>"); |
| 1554 |
MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver"); |
| 1555 |
MODULE_LICENSE("GPL v2"); |