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@kernel.org> 9 * 10 * See industrialio/accels/sca3000.h for comments. 11 */ 12 13#include <linux/interrupt.h> 14#include <linux/fs.h> 15#include <linux/device.h> 16#include <linux/slab.h> 17#include <linux/kernel.h> 18#include <linux/spi/spi.h> 19#include <linux/sysfs.h> 20#include <linux/module.h> 21#include <linux/iio/iio.h> 22#include <linux/iio/sysfs.h> 23#include <linux/iio/events.h> 24#include <linux/iio/buffer.h> 25 26#include "sca3000.h" 27 28enum sca3000_variant { 29 d01, 30 e02, 31 e04, 32 e05, 33}; 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 */ 43static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = { 44 [d01] = { 45 .scale = 7357, 46 .temp_output = true, 47 .measurement_mode_freq = 250, 48 .option_mode_1 = SCA3000_OP_MODE_BYPASS, 49 .option_mode_1_freq = 250, 50 .mot_det_mult_xz = {50, 100, 200, 350, 650, 1300}, 51 .mot_det_mult_y = {50, 100, 150, 250, 450, 850, 1750}, 52 }, 53 [e02] = { 54 .scale = 9810, 55 .measurement_mode_freq = 125, 56 .option_mode_1 = SCA3000_OP_MODE_NARROW, 57 .option_mode_1_freq = 63, 58 .mot_det_mult_xz = {100, 150, 300, 550, 1050, 2050}, 59 .mot_det_mult_y = {50, 100, 200, 350, 700, 1350, 2700}, 60 }, 61 [e04] = { 62 .scale = 19620, 63 .measurement_mode_freq = 100, 64 .option_mode_1 = SCA3000_OP_MODE_NARROW, 65 .option_mode_1_freq = 50, 66 .option_mode_2 = SCA3000_OP_MODE_WIDE, 67 .option_mode_2_freq = 400, 68 .mot_det_mult_xz = {200, 300, 600, 1100, 2100, 4100}, 69 .mot_det_mult_y = {100, 200, 400, 7000, 1400, 2700, 54000}, 70 }, 71 [e05] = { 72 .scale = 61313, 73 .measurement_mode_freq = 200, 74 .option_mode_1 = SCA3000_OP_MODE_NARROW, 75 .option_mode_1_freq = 50, 76 .option_mode_2 = SCA3000_OP_MODE_WIDE, 77 .option_mode_2_freq = 400, 78 .mot_det_mult_xz = {600, 900, 1700, 3200, 6100, 11900}, 79 .mot_det_mult_y = {300, 600, 1200, 2000, 4100, 7800, 15600}, 80 }, 81}; 82 83int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val) 84{ 85 st->tx[0] = SCA3000_WRITE_REG(address); 86 st->tx[1] = val; 87 return spi_write(st->us, st->tx, 2); 88} 89 90int sca3000_read_data_short(struct sca3000_state *st, 91 uint8_t reg_address_high, 92 int len) 93{ 94 struct spi_transfer xfer[2] = { 95 { 96 .len = 1, 97 .tx_buf = st->tx, 98 }, { 99 .len = len, 100 .rx_buf = st->rx, 101 } 102 }; 103 st->tx[0] = SCA3000_READ_REG(reg_address_high); 104 105 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); 106} 107 108/** 109 * sca3000_reg_lock_on() test if the ctrl register lock is on 110 * 111 * Lock must be held. 112 **/ 113static int sca3000_reg_lock_on(struct sca3000_state *st) 114{ 115 int ret; 116 117 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1); 118 if (ret < 0) 119 return ret; 120 121 return !(st->rx[0] & SCA3000_LOCKED); 122} 123 124/** 125 * __sca3000_unlock_reg_lock() unlock the control registers 126 * 127 * Note the device does not appear to support doing this in a single transfer. 128 * This should only ever be used as part of ctrl reg read. 129 * Lock must be held before calling this 130 **/ 131static int __sca3000_unlock_reg_lock(struct sca3000_state *st) 132{ 133 struct spi_transfer xfer[3] = { 134 { 135 .len = 2, 136 .cs_change = 1, 137 .tx_buf = st->tx, 138 }, { 139 .len = 2, 140 .cs_change = 1, 141 .tx_buf = st->tx + 2, 142 }, { 143 .len = 2, 144 .tx_buf = st->tx + 4, 145 }, 146 }; 147 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK); 148 st->tx[1] = 0x00; 149 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK); 150 st->tx[3] = 0x50; 151 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK); 152 st->tx[5] = 0xA0; 153 154 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); 155} 156 157/** 158 * sca3000_write_ctrl_reg() write to a lock protect ctrl register 159 * @sel: selects which registers we wish to write to 160 * @val: the value to be written 161 * 162 * Certain control registers are protected against overwriting by the lock 163 * register and use a shared write address. This function allows writing of 164 * these registers. 165 * Lock must be held. 166 **/ 167static int sca3000_write_ctrl_reg(struct sca3000_state *st, 168 uint8_t sel, 169 uint8_t val) 170{ 171 172 int ret; 173 174 ret = sca3000_reg_lock_on(st); 175 if (ret < 0) 176 goto error_ret; 177 if (ret) { 178 ret = __sca3000_unlock_reg_lock(st); 179 if (ret) 180 goto error_ret; 181 } 182 183 /* Set the control select register */ 184 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel); 185 if (ret) 186 goto error_ret; 187 188 /* Write the actual value into the register */ 189 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val); 190 191error_ret: 192 return ret; 193} 194 195/** 196 * sca3000_read_ctrl_reg() read from lock protected control register. 197 * 198 * Lock must be held. 199 **/ 200static int sca3000_read_ctrl_reg(struct sca3000_state *st, 201 u8 ctrl_reg) 202{ 203 int ret; 204 205 ret = sca3000_reg_lock_on(st); 206 if (ret < 0) 207 goto error_ret; 208 if (ret) { 209 ret = __sca3000_unlock_reg_lock(st); 210 if (ret) 211 goto error_ret; 212 } 213 /* Set the control select register */ 214 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg); 215 if (ret) 216 goto error_ret; 217 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_CTRL_DATA, 1); 218 if (ret) 219 goto error_ret; 220 else 221 return st->rx[0]; 222error_ret: 223 return ret; 224} 225 226#ifdef SCA3000_DEBUG 227/** 228 * sca3000_check_status() check the status register 229 * 230 * Only used for debugging purposes 231 **/ 232static int sca3000_check_status(struct device *dev) 233{ 234 int ret; 235 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 236 struct sca3000_state *st = iio_priv(indio_dev); 237 238 mutex_lock(&st->lock); 239 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1); 240 if (ret < 0) 241 goto error_ret; 242 if (st->rx[0] & SCA3000_EEPROM_CS_ERROR) 243 dev_err(dev, "eeprom error\n"); 244 if (st->rx[0] & SCA3000_SPI_FRAME_ERROR) 245 dev_err(dev, "Previous SPI Frame was corrupt\n"); 246 247error_ret: 248 mutex_unlock(&st->lock); 249 return ret; 250} 251#endif /* SCA3000_DEBUG */ 252 253/** 254 * sca3000_show_rev() - sysfs interface to read the chip revision number 255 **/ 256static ssize_t sca3000_show_rev(struct device *dev, 257 struct device_attribute *attr, 258 char *buf) 259{ 260 int len = 0, ret; 261 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 262 struct sca3000_state *st = iio_priv(indio_dev); 263 264 mutex_lock(&st->lock); 265 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_REVID, 1); 266 if (ret < 0) 267 goto error_ret; 268 len += sprintf(buf + len, 269 "major=%d, minor=%d\n", 270 st->rx[0] & SCA3000_REVID_MAJOR_MASK, 271 st->rx[0] & SCA3000_REVID_MINOR_MASK); 272error_ret: 273 mutex_unlock(&st->lock); 274 275 return ret ? ret : len; 276} 277 278/** 279 * sca3000_show_available_measurement_modes() display available modes 280 * 281 * This is all read from chip specific data in the driver. Not all 282 * of the sca3000 series support modes other than normal. 283 **/ 284static ssize_t 285sca3000_show_available_measurement_modes(struct device *dev, 286 struct device_attribute *attr, 287 char *buf) 288{ 289 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 290 struct sca3000_state *st = iio_priv(indio_dev); 291 int len = 0; 292 293 len += sprintf(buf + len, "0 - normal mode"); 294 switch (st->info->option_mode_1) { 295 case SCA3000_OP_MODE_NARROW: 296 len += sprintf(buf + len, ", 1 - narrow mode"); 297 break; 298 case SCA3000_OP_MODE_BYPASS: 299 len += sprintf(buf + len, ", 1 - bypass mode"); 300 break; 301 } 302 switch (st->info->option_mode_2) { 303 case SCA3000_OP_MODE_WIDE: 304 len += sprintf(buf + len, ", 2 - wide mode"); 305 break; 306 } 307 /* always supported */ 308 len += sprintf(buf + len, " 3 - motion detection\n"); 309 310 return len; 311} 312 313/** 314 * sca3000_show_measurement_mode() sysfs read of current mode 315 **/ 316static ssize_t 317sca3000_show_measurement_mode(struct device *dev, 318 struct device_attribute *attr, 319 char *buf) 320{ 321 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 322 struct sca3000_state *st = iio_priv(indio_dev); 323 int len = 0, ret; 324 325 mutex_lock(&st->lock); 326 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 327 if (ret) 328 goto error_ret; 329 /* mask bottom 2 bits - only ones that are relevant */ 330 st->rx[0] &= 0x03; 331 switch (st->rx[0]) { 332 case SCA3000_MEAS_MODE_NORMAL: 333 len += sprintf(buf + len, "0 - normal mode\n"); 334 break; 335 case SCA3000_MEAS_MODE_MOT_DET: 336 len += sprintf(buf + len, "3 - motion detection\n"); 337 break; 338 case SCA3000_MEAS_MODE_OP_1: 339 switch (st->info->option_mode_1) { 340 case SCA3000_OP_MODE_NARROW: 341 len += sprintf(buf + len, "1 - narrow mode\n"); 342 break; 343 case SCA3000_OP_MODE_BYPASS: 344 len += sprintf(buf + len, "1 - bypass mode\n"); 345 break; 346 } 347 break; 348 case SCA3000_MEAS_MODE_OP_2: 349 switch (st->info->option_mode_2) { 350 case SCA3000_OP_MODE_WIDE: 351 len += sprintf(buf + len, "2 - wide mode\n"); 352 break; 353 } 354 break; 355 } 356 357error_ret: 358 mutex_unlock(&st->lock); 359 360 return ret ? ret : len; 361} 362 363/** 364 * sca3000_store_measurement_mode() set the current mode 365 **/ 366static ssize_t 367sca3000_store_measurement_mode(struct device *dev, 368 struct device_attribute *attr, 369 const char *buf, 370 size_t len) 371{ 372 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 373 struct sca3000_state *st = iio_priv(indio_dev); 374 int ret; 375 u8 mask = 0x03; 376 u8 val; 377 378 mutex_lock(&st->lock); 379 ret = kstrtou8(buf, 10, &val); 380 if (ret) 381 goto error_ret; 382 if (val > 3) { 383 ret = -EINVAL; 384 goto error_ret; 385 } 386 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 387 if (ret) 388 goto error_ret; 389 st->rx[0] &= ~mask; 390 st->rx[0] |= (val & mask); 391 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, st->rx[0]); 392 if (ret) 393 goto error_ret; 394 mutex_unlock(&st->lock); 395 396 return len; 397 398error_ret: 399 mutex_unlock(&st->lock); 400 401 return ret; 402} 403 404 405/* 406 * Not even vaguely standard attributes so defined here rather than 407 * in the relevant IIO core headers 408 */ 409static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO, 410 sca3000_show_available_measurement_modes, 411 NULL, 0); 412 413static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR, 414 sca3000_show_measurement_mode, 415 sca3000_store_measurement_mode, 416 0); 417 418/* More standard attributes */ 419 420static IIO_DEVICE_ATTR(revision, S_IRUGO, sca3000_show_rev, NULL, 0); 421 422static const struct iio_event_spec sca3000_event = { 423 .type = IIO_EV_TYPE_MAG, 424 .dir = IIO_EV_DIR_RISING, 425 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), 426}; 427 428#define SCA3000_CHAN(index, mod) \ 429 { \ 430 .type = IIO_ACCEL, \ 431 .modified = 1, \ 432 .channel2 = mod, \ 433 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 434 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\ 435 .address = index, \ 436 .scan_index = index, \ 437 .scan_type = { \ 438 .sign = 's', \ 439 .realbits = 11, \ 440 .storagebits = 16, \ 441 .shift = 5, \ 442 }, \ 443 .event_spec = &sca3000_event, \ 444 .num_event_specs = 1, \ 445 } 446 447static const struct iio_chan_spec sca3000_channels[] = { 448 SCA3000_CHAN(0, IIO_MOD_X), 449 SCA3000_CHAN(1, IIO_MOD_Y), 450 SCA3000_CHAN(2, IIO_MOD_Z), 451}; 452 453static const struct iio_chan_spec sca3000_channels_with_temp[] = { 454 SCA3000_CHAN(0, IIO_MOD_X), 455 SCA3000_CHAN(1, IIO_MOD_Y), 456 SCA3000_CHAN(2, IIO_MOD_Z), 457 { 458 .type = IIO_TEMP, 459 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 460 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | 461 BIT(IIO_CHAN_INFO_OFFSET), 462 }, 463}; 464 465static u8 sca3000_addresses[3][3] = { 466 [0] = {SCA3000_REG_ADDR_X_MSB, SCA3000_REG_CTRL_SEL_MD_X_TH, 467 SCA3000_MD_CTRL_OR_X}, 468 [1] = {SCA3000_REG_ADDR_Y_MSB, SCA3000_REG_CTRL_SEL_MD_Y_TH, 469 SCA3000_MD_CTRL_OR_Y}, 470 [2] = {SCA3000_REG_ADDR_Z_MSB, SCA3000_REG_CTRL_SEL_MD_Z_TH, 471 SCA3000_MD_CTRL_OR_Z}, 472}; 473 474static int sca3000_read_raw(struct iio_dev *indio_dev, 475 struct iio_chan_spec const *chan, 476 int *val, 477 int *val2, 478 long mask) 479{ 480 struct sca3000_state *st = iio_priv(indio_dev); 481 int ret; 482 u8 address; 483 484 switch (mask) { 485 case IIO_CHAN_INFO_RAW: 486 mutex_lock(&st->lock); 487 if (chan->type == IIO_ACCEL) { 488 if (st->mo_det_use_count) { 489 mutex_unlock(&st->lock); 490 return -EBUSY; 491 } 492 address = sca3000_addresses[chan->address][0]; 493 ret = sca3000_read_data_short(st, address, 2); 494 if (ret < 0) { 495 mutex_unlock(&st->lock); 496 return ret; 497 } 498 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF; 499 *val = ((*val) << (sizeof(*val)*8 - 13)) >> 500 (sizeof(*val)*8 - 13); 501 } else { 502 /* get the temperature when available */ 503 ret = sca3000_read_data_short(st, 504 SCA3000_REG_ADDR_TEMP_MSB, 2); 505 if (ret < 0) { 506 mutex_unlock(&st->lock); 507 return ret; 508 } 509 *val = ((st->rx[0] & 0x3F) << 3) | 510 ((st->rx[1] & 0xE0) >> 5); 511 } 512 mutex_unlock(&st->lock); 513 return IIO_VAL_INT; 514 case IIO_CHAN_INFO_SCALE: 515 *val = 0; 516 if (chan->type == IIO_ACCEL) 517 *val2 = st->info->scale; 518 else /* temperature */ 519 *val2 = 555556; 520 return IIO_VAL_INT_PLUS_MICRO; 521 case IIO_CHAN_INFO_OFFSET: 522 *val = -214; 523 *val2 = 600000; 524 return IIO_VAL_INT_PLUS_MICRO; 525 default: 526 return -EINVAL; 527 } 528} 529 530/** 531 * sca3000_read_av_freq() sysfs function to get available frequencies 532 * 533 * The later modes are only relevant to the ring buffer - and depend on current 534 * mode. Note that data sheet gives rather wide tolerances for these so integer 535 * division will give good enough answer and not all chips have them specified 536 * at all. 537 **/ 538static ssize_t sca3000_read_av_freq(struct device *dev, 539 struct device_attribute *attr, 540 char *buf) 541{ 542 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 543 struct sca3000_state *st = iio_priv(indio_dev); 544 int len = 0, ret, val; 545 546 mutex_lock(&st->lock); 547 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 548 val = st->rx[0]; 549 mutex_unlock(&st->lock); 550 if (ret) 551 goto error_ret; 552 553 switch (val & 0x03) { 554 case SCA3000_MEAS_MODE_NORMAL: 555 len += sprintf(buf + len, "%d %d %d\n", 556 st->info->measurement_mode_freq, 557 st->info->measurement_mode_freq/2, 558 st->info->measurement_mode_freq/4); 559 break; 560 case SCA3000_MEAS_MODE_OP_1: 561 len += sprintf(buf + len, "%d %d %d\n", 562 st->info->option_mode_1_freq, 563 st->info->option_mode_1_freq/2, 564 st->info->option_mode_1_freq/4); 565 break; 566 case SCA3000_MEAS_MODE_OP_2: 567 len += sprintf(buf + len, "%d %d %d\n", 568 st->info->option_mode_2_freq, 569 st->info->option_mode_2_freq/2, 570 st->info->option_mode_2_freq/4); 571 break; 572 } 573 return len; 574error_ret: 575 return ret; 576} 577/** 578 * __sca3000_get_base_freq() obtain mode specific base frequency 579 * 580 * lock must be held 581 **/ 582static inline int __sca3000_get_base_freq(struct sca3000_state *st, 583 const struct sca3000_chip_info *info, 584 int *base_freq) 585{ 586 int ret; 587 588 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 589 if (ret) 590 goto error_ret; 591 switch (0x03 & st->rx[0]) { 592 case SCA3000_MEAS_MODE_NORMAL: 593 *base_freq = info->measurement_mode_freq; 594 break; 595 case SCA3000_MEAS_MODE_OP_1: 596 *base_freq = info->option_mode_1_freq; 597 break; 598 case SCA3000_MEAS_MODE_OP_2: 599 *base_freq = info->option_mode_2_freq; 600 break; 601 } 602error_ret: 603 return ret; 604} 605 606/** 607 * sca3000_read_frequency() sysfs interface to get the current frequency 608 **/ 609static ssize_t sca3000_read_frequency(struct device *dev, 610 struct device_attribute *attr, 611 char *buf) 612{ 613 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 614 struct sca3000_state *st = iio_priv(indio_dev); 615 int ret, len = 0, base_freq = 0, val; 616 617 mutex_lock(&st->lock); 618 ret = __sca3000_get_base_freq(st, st->info, &base_freq); 619 if (ret) 620 goto error_ret_mut; 621 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); 622 mutex_unlock(&st->lock); 623 if (ret) 624 goto error_ret; 625 val = ret; 626 if (base_freq > 0) 627 switch (val & 0x03) { 628 case 0x00: 629 case 0x03: 630 len = sprintf(buf, "%d\n", base_freq); 631 break; 632 case 0x01: 633 len = sprintf(buf, "%d\n", base_freq/2); 634 break; 635 case 0x02: 636 len = sprintf(buf, "%d\n", base_freq/4); 637 break; 638 } 639 640 return len; 641error_ret_mut: 642 mutex_unlock(&st->lock); 643error_ret: 644 return ret; 645} 646 647/** 648 * sca3000_set_frequency() sysfs interface to set the current frequency 649 **/ 650static ssize_t sca3000_set_frequency(struct device *dev, 651 struct device_attribute *attr, 652 const char *buf, 653 size_t len) 654{ 655 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 656 struct sca3000_state *st = iio_priv(indio_dev); 657 int ret, base_freq = 0; 658 int ctrlval; 659 int val; 660 661 ret = kstrtoint(buf, 10, &val); 662 if (ret) 663 return ret; 664 665 mutex_lock(&st->lock); 666 /* What mode are we in? */ 667 ret = __sca3000_get_base_freq(st, st->info, &base_freq); 668 if (ret) 669 goto error_free_lock; 670 671 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); 672 if (ret < 0) 673 goto error_free_lock; 674 ctrlval = ret; 675 /* clear the bits */ 676 ctrlval &= ~0x03; 677 678 if (val == base_freq/2) { 679 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2; 680 } else if (val == base_freq/4) { 681 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4; 682 } else if (val != base_freq) { 683 ret = -EINVAL; 684 goto error_free_lock; 685 } 686 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, 687 ctrlval); 688error_free_lock: 689 mutex_unlock(&st->lock); 690 691 return ret ? ret : len; 692} 693 694/* 695 * Should only really be registered if ring buffer support is compiled in. 696 * Does no harm however and doing it right would add a fair bit of complexity 697 */ 698static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq); 699 700static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, 701 sca3000_read_frequency, 702 sca3000_set_frequency); 703 704/** 705 * sca3000_read_thresh() - query of a threshold 706 **/ 707static int sca3000_read_thresh(struct iio_dev *indio_dev, 708 const struct iio_chan_spec *chan, 709 enum iio_event_type type, 710 enum iio_event_direction dir, 711 enum iio_event_info info, 712 int *val, int *val2) 713{ 714 int ret, i; 715 struct sca3000_state *st = iio_priv(indio_dev); 716 int num = chan->channel2; 717 718 mutex_lock(&st->lock); 719 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]); 720 mutex_unlock(&st->lock); 721 if (ret < 0) 722 return ret; 723 *val = 0; 724 if (num == 1) 725 for_each_set_bit(i, (unsigned long *)&ret, 726 ARRAY_SIZE(st->info->mot_det_mult_y)) 727 *val += st->info->mot_det_mult_y[i]; 728 else 729 for_each_set_bit(i, (unsigned long *)&ret, 730 ARRAY_SIZE(st->info->mot_det_mult_xz)) 731 *val += st->info->mot_det_mult_xz[i]; 732 733 return IIO_VAL_INT; 734} 735 736/** 737 * sca3000_write_thresh() control of threshold 738 **/ 739static int sca3000_write_thresh(struct iio_dev *indio_dev, 740 const struct iio_chan_spec *chan, 741 enum iio_event_type type, 742 enum iio_event_direction dir, 743 enum iio_event_info info, 744 int val, int val2) 745{ 746 struct sca3000_state *st = iio_priv(indio_dev); 747 int num = chan->channel2; 748 int ret; 749 int i; 750 u8 nonlinear = 0; 751 752 if (num == 1) { 753 i = ARRAY_SIZE(st->info->mot_det_mult_y); 754 while (i > 0) 755 if (val >= st->info->mot_det_mult_y[--i]) { 756 nonlinear |= (1 << i); 757 val -= st->info->mot_det_mult_y[i]; 758 } 759 } else { 760 i = ARRAY_SIZE(st->info->mot_det_mult_xz); 761 while (i > 0) 762 if (val >= st->info->mot_det_mult_xz[--i]) { 763 nonlinear |= (1 << i); 764 val -= st->info->mot_det_mult_xz[i]; 765 } 766 } 767 768 mutex_lock(&st->lock); 769 ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear); 770 mutex_unlock(&st->lock); 771 772 return ret; 773} 774 775static struct attribute *sca3000_attributes[] = { 776 &iio_dev_attr_revision.dev_attr.attr, 777 &iio_dev_attr_measurement_mode_available.dev_attr.attr, 778 &iio_dev_attr_measurement_mode.dev_attr.attr, 779 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 780 &iio_dev_attr_sampling_frequency.dev_attr.attr, 781 NULL, 782}; 783 784static const struct attribute_group sca3000_attribute_group = { 785 .attrs = sca3000_attributes, 786}; 787 788/** 789 * sca3000_event_handler() - handling ring and non ring events 790 * 791 * Ring related interrupt handler. Depending on event, push to 792 * the ring buffer event chrdev or the event one. 793 * 794 * This function is complicated by the fact that the devices can signify ring 795 * and non ring events via the same interrupt line and they can only 796 * be distinguished via a read of the relevant status register. 797 **/ 798static irqreturn_t sca3000_event_handler(int irq, void *private) 799{ 800 struct iio_dev *indio_dev = private; 801 struct sca3000_state *st = iio_priv(indio_dev); 802 int ret, val; 803 s64 last_timestamp = iio_get_time_ns(); 804 805 /* 806 * Could lead if badly timed to an extra read of status reg, 807 * but ensures no interrupt is missed. 808 */ 809 mutex_lock(&st->lock); 810 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1); 811 val = st->rx[0]; 812 mutex_unlock(&st->lock); 813 if (ret) 814 goto done; 815 816 sca3000_ring_int_process(val, indio_dev->buffer); 817 818 if (val & SCA3000_INT_STATUS_FREE_FALL) 819 iio_push_event(indio_dev, 820 IIO_MOD_EVENT_CODE(IIO_ACCEL, 821 0, 822 IIO_MOD_X_AND_Y_AND_Z, 823 IIO_EV_TYPE_MAG, 824 IIO_EV_DIR_FALLING), 825 last_timestamp); 826 827 if (val & SCA3000_INT_STATUS_Y_TRIGGER) 828 iio_push_event(indio_dev, 829 IIO_MOD_EVENT_CODE(IIO_ACCEL, 830 0, 831 IIO_MOD_Y, 832 IIO_EV_TYPE_MAG, 833 IIO_EV_DIR_RISING), 834 last_timestamp); 835 836 if (val & SCA3000_INT_STATUS_X_TRIGGER) 837 iio_push_event(indio_dev, 838 IIO_MOD_EVENT_CODE(IIO_ACCEL, 839 0, 840 IIO_MOD_X, 841 IIO_EV_TYPE_MAG, 842 IIO_EV_DIR_RISING), 843 last_timestamp); 844 845 if (val & SCA3000_INT_STATUS_Z_TRIGGER) 846 iio_push_event(indio_dev, 847 IIO_MOD_EVENT_CODE(IIO_ACCEL, 848 0, 849 IIO_MOD_Z, 850 IIO_EV_TYPE_MAG, 851 IIO_EV_DIR_RISING), 852 last_timestamp); 853 854done: 855 return IRQ_HANDLED; 856} 857 858/** 859 * sca3000_read_event_config() what events are enabled 860 **/ 861static int sca3000_read_event_config(struct iio_dev *indio_dev, 862 const struct iio_chan_spec *chan, 863 enum iio_event_type type, 864 enum iio_event_direction dir) 865{ 866 struct sca3000_state *st = iio_priv(indio_dev); 867 int ret; 868 u8 protect_mask = 0x03; 869 int num = chan->channel2; 870 871 /* read current value of mode register */ 872 mutex_lock(&st->lock); 873 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 874 if (ret) 875 goto error_ret; 876 877 if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET) 878 ret = 0; 879 else { 880 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL); 881 if (ret < 0) 882 goto error_ret; 883 /* only supporting logical or's for now */ 884 ret = !!(ret & sca3000_addresses[num][2]); 885 } 886error_ret: 887 mutex_unlock(&st->lock); 888 889 return ret; 890} 891/** 892 * sca3000_query_free_fall_mode() is free fall mode enabled 893 **/ 894static ssize_t sca3000_query_free_fall_mode(struct device *dev, 895 struct device_attribute *attr, 896 char *buf) 897{ 898 int ret, len; 899 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 900 struct sca3000_state *st = iio_priv(indio_dev); 901 int val; 902 903 mutex_lock(&st->lock); 904 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 905 val = st->rx[0]; 906 mutex_unlock(&st->lock); 907 if (ret < 0) 908 return ret; 909 len = sprintf(buf, "%d\n", 910 !!(val & SCA3000_FREE_FALL_DETECT)); 911 return len; 912} 913 914/** 915 * sca3000_set_free_fall_mode() simple on off control for free fall int 916 * 917 * In these chips the free fall detector should send an interrupt if 918 * the device falls more than 25cm. This has not been tested due 919 * to fragile wiring. 920 **/ 921static ssize_t sca3000_set_free_fall_mode(struct device *dev, 922 struct device_attribute *attr, 923 const char *buf, 924 size_t len) 925{ 926 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 927 struct sca3000_state *st = iio_priv(indio_dev); 928 u8 val; 929 int ret; 930 u8 protect_mask = SCA3000_FREE_FALL_DETECT; 931 932 mutex_lock(&st->lock); 933 ret = kstrtou8(buf, 10, &val); 934 if (ret) 935 goto error_ret; 936 937 /* read current value of mode register */ 938 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 939 if (ret) 940 goto error_ret; 941 942 /* if off and should be on */ 943 if (val && !(st->rx[0] & protect_mask)) 944 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 945 (st->rx[0] | SCA3000_FREE_FALL_DETECT)); 946 /* if on and should be off */ 947 else if (!val && (st->rx[0] & protect_mask)) 948 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 949 (st->rx[0] & ~protect_mask)); 950error_ret: 951 mutex_unlock(&st->lock); 952 953 return ret ? ret : len; 954} 955 956/** 957 * sca3000_write_event_config() simple on off control for motion detector 958 * 959 * This is a per axis control, but enabling any will result in the 960 * motion detector unit being enabled. 961 * N.B. enabling motion detector stops normal data acquisition. 962 * There is a complexity in knowing which mode to return to when 963 * this mode is disabled. Currently normal mode is assumed. 964 **/ 965static int sca3000_write_event_config(struct iio_dev *indio_dev, 966 const struct iio_chan_spec *chan, 967 enum iio_event_type type, 968 enum iio_event_direction dir, 969 int state) 970{ 971 struct sca3000_state *st = iio_priv(indio_dev); 972 int ret, ctrlval; 973 u8 protect_mask = 0x03; 974 int num = chan->channel2; 975 976 mutex_lock(&st->lock); 977 /* 978 * First read the motion detector config to find out if 979 * this axis is on 980 */ 981 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL); 982 if (ret < 0) 983 goto exit_point; 984 ctrlval = ret; 985 /* if off and should be on */ 986 if (state && !(ctrlval & sca3000_addresses[num][2])) { 987 ret = sca3000_write_ctrl_reg(st, 988 SCA3000_REG_CTRL_SEL_MD_CTRL, 989 ctrlval | 990 sca3000_addresses[num][2]); 991 if (ret) 992 goto exit_point; 993 st->mo_det_use_count++; 994 } else if (!state && (ctrlval & sca3000_addresses[num][2])) { 995 ret = sca3000_write_ctrl_reg(st, 996 SCA3000_REG_CTRL_SEL_MD_CTRL, 997 ctrlval & 998 ~(sca3000_addresses[num][2])); 999 if (ret) 1000 goto exit_point; 1001 st->mo_det_use_count--; 1002 } 1003 1004 /* read current value of mode register */ 1005 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 1006 if (ret) 1007 goto exit_point; 1008 /* if off and should be on */ 1009 if ((st->mo_det_use_count) 1010 && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)) 1011 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1012 (st->rx[0] & ~protect_mask) 1013 | SCA3000_MEAS_MODE_MOT_DET); 1014 /* if on and should be off */ 1015 else if (!(st->mo_det_use_count) 1016 && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET)) 1017 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1018 (st->rx[0] & ~protect_mask)); 1019exit_point: 1020 mutex_unlock(&st->lock); 1021 1022 return ret; 1023} 1024 1025/* Free fall detector related event attribute */ 1026static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en, 1027 in_accel_x&y&z_mag_falling_en, 1028 S_IRUGO | S_IWUSR, 1029 sca3000_query_free_fall_mode, 1030 sca3000_set_free_fall_mode, 1031 0); 1032 1033static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period, 1034 in_accel_x&y&z_mag_falling_period, 1035 "0.226"); 1036 1037static struct attribute *sca3000_event_attributes[] = { 1038 &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr, 1039 &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr, 1040 NULL, 1041}; 1042 1043static struct attribute_group sca3000_event_attribute_group = { 1044 .attrs = sca3000_event_attributes, 1045 .name = "events", 1046}; 1047 1048/** 1049 * sca3000_clean_setup() get the device into a predictable state 1050 * 1051 * Devices use flash memory to store many of the register values 1052 * and hence can come up in somewhat unpredictable states. 1053 * Hence reset everything on driver load. 1054 **/ 1055static int sca3000_clean_setup(struct sca3000_state *st) 1056{ 1057 int ret; 1058 1059 mutex_lock(&st->lock); 1060 /* Ensure all interrupts have been acknowledged */ 1061 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1); 1062 if (ret) 1063 goto error_ret; 1064 1065 /* Turn off all motion detection channels */ 1066 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL); 1067 if (ret < 0) 1068 goto error_ret; 1069 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL, 1070 ret & SCA3000_MD_CTRL_PROT_MASK); 1071 if (ret) 1072 goto error_ret; 1073 1074 /* Disable ring buffer */ 1075 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); 1076 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, 1077 (ret & SCA3000_OUT_CTRL_PROT_MASK) 1078 | SCA3000_OUT_CTRL_BUF_X_EN 1079 | SCA3000_OUT_CTRL_BUF_Y_EN 1080 | SCA3000_OUT_CTRL_BUF_Z_EN 1081 | SCA3000_OUT_CTRL_BUF_DIV_4); 1082 if (ret) 1083 goto error_ret; 1084 /* Enable interrupts, relevant to mode and set up as active low */ 1085 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1); 1086 if (ret) 1087 goto error_ret; 1088 ret = sca3000_write_reg(st, 1089 SCA3000_REG_ADDR_INT_MASK, 1090 (ret & SCA3000_INT_MASK_PROT_MASK) 1091 | SCA3000_INT_MASK_ACTIVE_LOW); 1092 if (ret) 1093 goto error_ret; 1094 /* 1095 * Select normal measurement mode, free fall off, ring off 1096 * Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5 1097 * as that occurs in one of the example on the datasheet 1098 */ 1099 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 1100 if (ret) 1101 goto error_ret; 1102 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1103 (st->rx[0] & SCA3000_MODE_PROT_MASK)); 1104 st->bpse = 11; 1105 1106error_ret: 1107 mutex_unlock(&st->lock); 1108 return ret; 1109} 1110 1111static const struct iio_info sca3000_info = { 1112 .attrs = &sca3000_attribute_group, 1113 .read_raw = &sca3000_read_raw, 1114 .event_attrs = &sca3000_event_attribute_group, 1115 .read_event_value = &sca3000_read_thresh, 1116 .write_event_value = &sca3000_write_thresh, 1117 .read_event_config = &sca3000_read_event_config, 1118 .write_event_config = &sca3000_write_event_config, 1119 .driver_module = THIS_MODULE, 1120}; 1121 1122static int sca3000_probe(struct spi_device *spi) 1123{ 1124 int ret; 1125 struct sca3000_state *st; 1126 struct iio_dev *indio_dev; 1127 1128 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 1129 if (!indio_dev) 1130 return -ENOMEM; 1131 1132 st = iio_priv(indio_dev); 1133 spi_set_drvdata(spi, indio_dev); 1134 st->us = spi; 1135 mutex_init(&st->lock); 1136 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi) 1137 ->driver_data]; 1138 1139 indio_dev->dev.parent = &spi->dev; 1140 indio_dev->name = spi_get_device_id(spi)->name; 1141 indio_dev->info = &sca3000_info; 1142 if (st->info->temp_output) { 1143 indio_dev->channels = sca3000_channels_with_temp; 1144 indio_dev->num_channels = 1145 ARRAY_SIZE(sca3000_channels_with_temp); 1146 } else { 1147 indio_dev->channels = sca3000_channels; 1148 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels); 1149 } 1150 indio_dev->modes = INDIO_DIRECT_MODE; 1151 1152 sca3000_configure_ring(indio_dev); 1153 ret = iio_device_register(indio_dev); 1154 if (ret < 0) 1155 return ret; 1156 1157 ret = iio_buffer_register(indio_dev, 1158 sca3000_channels, 1159 ARRAY_SIZE(sca3000_channels)); 1160 if (ret < 0) 1161 goto error_unregister_dev; 1162 if (indio_dev->buffer) { 1163 iio_scan_mask_set(indio_dev, indio_dev->buffer, 0); 1164 iio_scan_mask_set(indio_dev, indio_dev->buffer, 1); 1165 iio_scan_mask_set(indio_dev, indio_dev->buffer, 2); 1166 } 1167 1168 if (spi->irq) { 1169 ret = request_threaded_irq(spi->irq, 1170 NULL, 1171 &sca3000_event_handler, 1172 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1173 "sca3000", 1174 indio_dev); 1175 if (ret) 1176 goto error_unregister_ring; 1177 } 1178 sca3000_register_ring_funcs(indio_dev); 1179 ret = sca3000_clean_setup(st); 1180 if (ret) 1181 goto error_free_irq; 1182 return 0; 1183 1184error_free_irq: 1185 if (spi->irq) 1186 free_irq(spi->irq, indio_dev); 1187error_unregister_ring: 1188 iio_buffer_unregister(indio_dev); 1189error_unregister_dev: 1190 iio_device_unregister(indio_dev); 1191 return ret; 1192} 1193 1194static int sca3000_stop_all_interrupts(struct sca3000_state *st) 1195{ 1196 int ret; 1197 1198 mutex_lock(&st->lock); 1199 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1); 1200 if (ret) 1201 goto error_ret; 1202 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK, 1203 (st->rx[0] & 1204 ~(SCA3000_INT_MASK_RING_THREE_QUARTER | 1205 SCA3000_INT_MASK_RING_HALF | 1206 SCA3000_INT_MASK_ALL_INTS))); 1207error_ret: 1208 mutex_unlock(&st->lock); 1209 return ret; 1210} 1211 1212static int sca3000_remove(struct spi_device *spi) 1213{ 1214 struct iio_dev *indio_dev = spi_get_drvdata(spi); 1215 struct sca3000_state *st = iio_priv(indio_dev); 1216 1217 /* Must ensure no interrupts can be generated after this! */ 1218 sca3000_stop_all_interrupts(st); 1219 if (spi->irq) 1220 free_irq(spi->irq, indio_dev); 1221 iio_device_unregister(indio_dev); 1222 iio_buffer_unregister(indio_dev); 1223 sca3000_unconfigure_ring(indio_dev); 1224 1225 return 0; 1226} 1227 1228static const struct spi_device_id sca3000_id[] = { 1229 {"sca3000_d01", d01}, 1230 {"sca3000_e02", e02}, 1231 {"sca3000_e04", e04}, 1232 {"sca3000_e05", e05}, 1233 {} 1234}; 1235MODULE_DEVICE_TABLE(spi, sca3000_id); 1236 1237static struct spi_driver sca3000_driver = { 1238 .driver = { 1239 .name = "sca3000", 1240 .owner = THIS_MODULE, 1241 }, 1242 .probe = sca3000_probe, 1243 .remove = sca3000_remove, 1244 .id_table = sca3000_id, 1245}; 1246module_spi_driver(sca3000_driver); 1247 1248MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1249MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver"); 1250MODULE_LICENSE("GPL v2"); 1251