sca3000_core.c revision 5262d8fdd9cdc8ef3ad8e1af8968f079dd81dde5
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 u8 sca3000_addresses[3][3] = { 454 [0] = {SCA3000_REG_ADDR_X_MSB, SCA3000_REG_CTRL_SEL_MD_X_TH, 455 SCA3000_MD_CTRL_OR_X}, 456 [1] = {SCA3000_REG_ADDR_Y_MSB, SCA3000_REG_CTRL_SEL_MD_Y_TH, 457 SCA3000_MD_CTRL_OR_Y}, 458 [2] = {SCA3000_REG_ADDR_Z_MSB, SCA3000_REG_CTRL_SEL_MD_Z_TH, 459 SCA3000_MD_CTRL_OR_Z}, 460}; 461 462static int sca3000_read_raw(struct iio_dev *indio_dev, 463 struct iio_chan_spec const *chan, 464 int *val, 465 int *val2, 466 long mask) 467{ 468 struct sca3000_state *st = iio_priv(indio_dev); 469 int ret; 470 u8 address; 471 472 switch (mask) { 473 case IIO_CHAN_INFO_RAW: 474 mutex_lock(&st->lock); 475 if (st->mo_det_use_count) { 476 mutex_unlock(&st->lock); 477 return -EBUSY; 478 } 479 address = sca3000_addresses[chan->address][0]; 480 ret = sca3000_read_data_short(st, address, 2); 481 if (ret < 0) { 482 mutex_unlock(&st->lock); 483 return ret; 484 } 485 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF; 486 *val = ((*val) << (sizeof(*val)*8 - 13)) >> 487 (sizeof(*val)*8 - 13); 488 mutex_unlock(&st->lock); 489 return IIO_VAL_INT; 490 case IIO_CHAN_INFO_SCALE: 491 *val = 0; 492 if (chan->type == IIO_ACCEL) 493 *val2 = st->info->scale; 494 else /* temperature */ 495 *val2 = 555556; 496 return IIO_VAL_INT_PLUS_MICRO; 497 default: 498 return -EINVAL; 499 } 500} 501 502/** 503 * sca3000_read_av_freq() sysfs function to get available frequencies 504 * 505 * The later modes are only relevant to the ring buffer - and depend on current 506 * mode. Note that data sheet gives rather wide tolerances for these so integer 507 * division will give good enough answer and not all chips have them specified 508 * at all. 509 **/ 510static ssize_t sca3000_read_av_freq(struct device *dev, 511 struct device_attribute *attr, 512 char *buf) 513{ 514 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 515 struct sca3000_state *st = iio_priv(indio_dev); 516 int len = 0, ret, val; 517 518 mutex_lock(&st->lock); 519 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 520 val = st->rx[0]; 521 mutex_unlock(&st->lock); 522 if (ret) 523 goto error_ret; 524 525 switch (val & 0x03) { 526 case SCA3000_MEAS_MODE_NORMAL: 527 len += sprintf(buf + len, "%d %d %d\n", 528 st->info->measurement_mode_freq, 529 st->info->measurement_mode_freq/2, 530 st->info->measurement_mode_freq/4); 531 break; 532 case SCA3000_MEAS_MODE_OP_1: 533 len += sprintf(buf + len, "%d %d %d\n", 534 st->info->option_mode_1_freq, 535 st->info->option_mode_1_freq/2, 536 st->info->option_mode_1_freq/4); 537 break; 538 case SCA3000_MEAS_MODE_OP_2: 539 len += sprintf(buf + len, "%d %d %d\n", 540 st->info->option_mode_2_freq, 541 st->info->option_mode_2_freq/2, 542 st->info->option_mode_2_freq/4); 543 break; 544 } 545 return len; 546error_ret: 547 return ret; 548} 549/** 550 * __sca3000_get_base_freq() obtain mode specific base frequency 551 * 552 * lock must be held 553 **/ 554static inline int __sca3000_get_base_freq(struct sca3000_state *st, 555 const struct sca3000_chip_info *info, 556 int *base_freq) 557{ 558 int ret; 559 560 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 561 if (ret) 562 goto error_ret; 563 switch (0x03 & st->rx[0]) { 564 case SCA3000_MEAS_MODE_NORMAL: 565 *base_freq = info->measurement_mode_freq; 566 break; 567 case SCA3000_MEAS_MODE_OP_1: 568 *base_freq = info->option_mode_1_freq; 569 break; 570 case SCA3000_MEAS_MODE_OP_2: 571 *base_freq = info->option_mode_2_freq; 572 break; 573 } 574error_ret: 575 return ret; 576} 577 578/** 579 * sca3000_read_frequency() sysfs interface to get the current frequency 580 **/ 581static ssize_t sca3000_read_frequency(struct device *dev, 582 struct device_attribute *attr, 583 char *buf) 584{ 585 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 586 struct sca3000_state *st = iio_priv(indio_dev); 587 int ret, len = 0, base_freq = 0, val; 588 589 mutex_lock(&st->lock); 590 ret = __sca3000_get_base_freq(st, st->info, &base_freq); 591 if (ret) 592 goto error_ret_mut; 593 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); 594 mutex_unlock(&st->lock); 595 if (ret) 596 goto error_ret; 597 val = ret; 598 if (base_freq > 0) 599 switch (val & 0x03) { 600 case 0x00: 601 case 0x03: 602 len = sprintf(buf, "%d\n", base_freq); 603 break; 604 case 0x01: 605 len = sprintf(buf, "%d\n", base_freq/2); 606 break; 607 case 0x02: 608 len = sprintf(buf, "%d\n", base_freq/4); 609 break; 610 } 611 612 return len; 613error_ret_mut: 614 mutex_unlock(&st->lock); 615error_ret: 616 return ret; 617} 618 619/** 620 * sca3000_set_frequency() sysfs interface to set the current frequency 621 **/ 622static ssize_t sca3000_set_frequency(struct device *dev, 623 struct device_attribute *attr, 624 const char *buf, 625 size_t len) 626{ 627 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 628 struct sca3000_state *st = iio_priv(indio_dev); 629 int ret, base_freq = 0; 630 int ctrlval; 631 int val; 632 633 ret = kstrtoint(buf, 10, &val); 634 if (ret) 635 return ret; 636 637 mutex_lock(&st->lock); 638 /* What mode are we in? */ 639 ret = __sca3000_get_base_freq(st, st->info, &base_freq); 640 if (ret) 641 goto error_free_lock; 642 643 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); 644 if (ret < 0) 645 goto error_free_lock; 646 ctrlval = ret; 647 /* clear the bits */ 648 ctrlval &= ~0x03; 649 650 if (val == base_freq/2) { 651 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2; 652 } else if (val == base_freq/4) { 653 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4; 654 } else if (val != base_freq) { 655 ret = -EINVAL; 656 goto error_free_lock; 657 } 658 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, 659 ctrlval); 660error_free_lock: 661 mutex_unlock(&st->lock); 662 663 return ret ? ret : len; 664} 665 666/* 667 * Should only really be registered if ring buffer support is compiled in. 668 * Does no harm however and doing it right would add a fair bit of complexity 669 */ 670static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq); 671 672static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, 673 sca3000_read_frequency, 674 sca3000_set_frequency); 675 676 677/** 678 * sca3000_read_temp() sysfs interface to get the temperature when available 679 * 680 * The alignment of data in here is downright odd. See data sheet. 681 * Converting this into a meaningful value is left to inline functions in 682 * userspace part of header. 683 **/ 684static ssize_t sca3000_read_temp(struct device *dev, 685 struct device_attribute *attr, 686 char *buf) 687{ 688 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 689 struct sca3000_state *st = iio_priv(indio_dev); 690 int ret; 691 int val; 692 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_TEMP_MSB, 2); 693 if (ret < 0) 694 goto error_ret; 695 val = ((st->rx[0] & 0x3F) << 3) | ((st->rx[1] & 0xE0) >> 5); 696 697 return sprintf(buf, "%d\n", val); 698 699error_ret: 700 return ret; 701} 702static IIO_DEV_ATTR_TEMP_RAW(sca3000_read_temp); 703 704static IIO_CONST_ATTR_TEMP_SCALE("0.555556"); 705static IIO_CONST_ATTR_TEMP_OFFSET("-214.6"); 706 707/** 708 * sca3000_read_thresh() - query of a threshold 709 **/ 710static int sca3000_read_thresh(struct iio_dev *indio_dev, 711 const struct iio_chan_spec *chan, 712 enum iio_event_type type, 713 enum iio_event_direction dir, 714 enum iio_event_info info, 715 int *val, int *val2) 716{ 717 int ret, i; 718 struct sca3000_state *st = iio_priv(indio_dev); 719 int num = chan->channel2; 720 mutex_lock(&st->lock); 721 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]); 722 mutex_unlock(&st->lock); 723 if (ret < 0) 724 return ret; 725 *val = 0; 726 if (num == 1) 727 for_each_set_bit(i, (unsigned long *)&ret, 728 ARRAY_SIZE(st->info->mot_det_mult_y)) 729 *val += st->info->mot_det_mult_y[i]; 730 else 731 for_each_set_bit(i, (unsigned long *)&ret, 732 ARRAY_SIZE(st->info->mot_det_mult_xz)) 733 *val += st->info->mot_det_mult_xz[i]; 734 735 return IIO_VAL_INT; 736} 737 738/** 739 * sca3000_write_thresh() control of threshold 740 **/ 741static int sca3000_write_thresh(struct iio_dev *indio_dev, 742 const struct iio_chan_spec *chan, 743 enum iio_event_type type, 744 enum iio_event_direction dir, 745 enum iio_event_info info, 746 int val, int val2) 747{ 748 struct sca3000_state *st = iio_priv(indio_dev); 749 int num = chan->channel2; 750 int ret; 751 int i; 752 u8 nonlinear = 0; 753 754 if (num == 1) { 755 i = ARRAY_SIZE(st->info->mot_det_mult_y); 756 while (i > 0) 757 if (val >= st->info->mot_det_mult_y[--i]) { 758 nonlinear |= (1 << i); 759 val -= st->info->mot_det_mult_y[i]; 760 } 761 } else { 762 i = ARRAY_SIZE(st->info->mot_det_mult_xz); 763 while (i > 0) 764 if (val >= st->info->mot_det_mult_xz[--i]) { 765 nonlinear |= (1 << i); 766 val -= st->info->mot_det_mult_xz[i]; 767 } 768 } 769 770 mutex_lock(&st->lock); 771 ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear); 772 mutex_unlock(&st->lock); 773 774 return ret; 775} 776 777static struct attribute *sca3000_attributes[] = { 778 &iio_dev_attr_revision.dev_attr.attr, 779 &iio_dev_attr_measurement_mode_available.dev_attr.attr, 780 &iio_dev_attr_measurement_mode.dev_attr.attr, 781 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 782 &iio_dev_attr_sampling_frequency.dev_attr.attr, 783 NULL, 784}; 785 786static struct attribute *sca3000_attributes_with_temp[] = { 787 &iio_dev_attr_revision.dev_attr.attr, 788 &iio_dev_attr_measurement_mode_available.dev_attr.attr, 789 &iio_dev_attr_measurement_mode.dev_attr.attr, 790 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 791 &iio_dev_attr_sampling_frequency.dev_attr.attr, 792 /* Only present if temp sensor is */ 793 &iio_dev_attr_in_temp_raw.dev_attr.attr, 794 &iio_const_attr_in_temp_offset.dev_attr.attr, 795 &iio_const_attr_in_temp_scale.dev_attr.attr, 796 NULL, 797}; 798 799static const struct attribute_group sca3000_attribute_group = { 800 .attrs = sca3000_attributes, 801}; 802 803static const struct attribute_group sca3000_attribute_group_with_temp = { 804 .attrs = sca3000_attributes_with_temp, 805}; 806 807/** 808 * sca3000_event_handler() - handling ring and non ring events 809 * 810 * Ring related interrupt handler. Depending on event, push to 811 * the ring buffer event chrdev or the event one. 812 * 813 * This function is complicated by the fact that the devices can signify ring 814 * and non ring events via the same interrupt line and they can only 815 * be distinguished via a read of the relevant status register. 816 **/ 817static irqreturn_t sca3000_event_handler(int irq, void *private) 818{ 819 struct iio_dev *indio_dev = private; 820 struct sca3000_state *st = iio_priv(indio_dev); 821 int ret, val; 822 s64 last_timestamp = iio_get_time_ns(); 823 824 /* 825 * Could lead if badly timed to an extra read of status reg, 826 * but ensures no interrupt is missed. 827 */ 828 mutex_lock(&st->lock); 829 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1); 830 val = st->rx[0]; 831 mutex_unlock(&st->lock); 832 if (ret) 833 goto done; 834 835 sca3000_ring_int_process(val, indio_dev->buffer); 836 837 if (val & SCA3000_INT_STATUS_FREE_FALL) 838 iio_push_event(indio_dev, 839 IIO_MOD_EVENT_CODE(IIO_ACCEL, 840 0, 841 IIO_MOD_X_AND_Y_AND_Z, 842 IIO_EV_TYPE_MAG, 843 IIO_EV_DIR_FALLING), 844 last_timestamp); 845 846 if (val & SCA3000_INT_STATUS_Y_TRIGGER) 847 iio_push_event(indio_dev, 848 IIO_MOD_EVENT_CODE(IIO_ACCEL, 849 0, 850 IIO_MOD_Y, 851 IIO_EV_TYPE_MAG, 852 IIO_EV_DIR_RISING), 853 last_timestamp); 854 855 if (val & SCA3000_INT_STATUS_X_TRIGGER) 856 iio_push_event(indio_dev, 857 IIO_MOD_EVENT_CODE(IIO_ACCEL, 858 0, 859 IIO_MOD_X, 860 IIO_EV_TYPE_MAG, 861 IIO_EV_DIR_RISING), 862 last_timestamp); 863 864 if (val & SCA3000_INT_STATUS_Z_TRIGGER) 865 iio_push_event(indio_dev, 866 IIO_MOD_EVENT_CODE(IIO_ACCEL, 867 0, 868 IIO_MOD_Z, 869 IIO_EV_TYPE_MAG, 870 IIO_EV_DIR_RISING), 871 last_timestamp); 872 873done: 874 return IRQ_HANDLED; 875} 876 877/** 878 * sca3000_read_event_config() what events are enabled 879 **/ 880static int sca3000_read_event_config(struct iio_dev *indio_dev, 881 const struct iio_chan_spec *chan, 882 enum iio_event_type type, 883 enum iio_event_direction dir) 884{ 885 struct sca3000_state *st = iio_priv(indio_dev); 886 int ret; 887 u8 protect_mask = 0x03; 888 int num = chan->channel2; 889 890 /* read current value of mode register */ 891 mutex_lock(&st->lock); 892 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 893 if (ret) 894 goto error_ret; 895 896 if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET) 897 ret = 0; 898 else { 899 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL); 900 if (ret < 0) 901 goto error_ret; 902 /* only supporting logical or's for now */ 903 ret = !!(ret & sca3000_addresses[num][2]); 904 } 905error_ret: 906 mutex_unlock(&st->lock); 907 908 return ret; 909} 910/** 911 * sca3000_query_free_fall_mode() is free fall mode enabled 912 **/ 913static ssize_t sca3000_query_free_fall_mode(struct device *dev, 914 struct device_attribute *attr, 915 char *buf) 916{ 917 int ret, len; 918 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 919 struct sca3000_state *st = iio_priv(indio_dev); 920 int val; 921 922 mutex_lock(&st->lock); 923 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 924 val = st->rx[0]; 925 mutex_unlock(&st->lock); 926 if (ret < 0) 927 return ret; 928 len = sprintf(buf, "%d\n", 929 !!(val & SCA3000_FREE_FALL_DETECT)); 930 return len; 931} 932 933/** 934 * sca3000_set_free_fall_mode() simple on off control for free fall int 935 * 936 * In these chips the free fall detector should send an interrupt if 937 * the device falls more than 25cm. This has not been tested due 938 * to fragile wiring. 939 **/ 940static ssize_t sca3000_set_free_fall_mode(struct device *dev, 941 struct device_attribute *attr, 942 const char *buf, 943 size_t len) 944{ 945 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 946 struct sca3000_state *st = iio_priv(indio_dev); 947 u8 val; 948 int ret; 949 u8 protect_mask = SCA3000_FREE_FALL_DETECT; 950 951 mutex_lock(&st->lock); 952 ret = kstrtou8(buf, 10, &val); 953 if (ret) 954 goto error_ret; 955 956 /* read current value of mode register */ 957 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 958 if (ret) 959 goto error_ret; 960 961 /* if off and should be on */ 962 if (val && !(st->rx[0] & protect_mask)) 963 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 964 (st->rx[0] | SCA3000_FREE_FALL_DETECT)); 965 /* if on and should be off */ 966 else if (!val && (st->rx[0] & protect_mask)) 967 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 968 (st->rx[0] & ~protect_mask)); 969error_ret: 970 mutex_unlock(&st->lock); 971 972 return ret ? ret : len; 973} 974 975/** 976 * sca3000_write_event_config() simple on off control for motion detector 977 * 978 * This is a per axis control, but enabling any will result in the 979 * motion detector unit being enabled. 980 * N.B. enabling motion detector stops normal data acquisition. 981 * There is a complexity in knowing which mode to return to when 982 * this mode is disabled. Currently normal mode is assumed. 983 **/ 984static int sca3000_write_event_config(struct iio_dev *indio_dev, 985 const struct iio_chan_spec *chan, 986 enum iio_event_type type, 987 enum iio_event_direction dir, 988 int state) 989{ 990 struct sca3000_state *st = iio_priv(indio_dev); 991 int ret, ctrlval; 992 u8 protect_mask = 0x03; 993 int num = chan->channel2; 994 995 mutex_lock(&st->lock); 996 /* 997 * First read the motion detector config to find out if 998 * this axis is on 999 */ 1000 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL); 1001 if (ret < 0) 1002 goto exit_point; 1003 ctrlval = ret; 1004 /* if off and should be on */ 1005 if (state && !(ctrlval & sca3000_addresses[num][2])) { 1006 ret = sca3000_write_ctrl_reg(st, 1007 SCA3000_REG_CTRL_SEL_MD_CTRL, 1008 ctrlval | 1009 sca3000_addresses[num][2]); 1010 if (ret) 1011 goto exit_point; 1012 st->mo_det_use_count++; 1013 } else if (!state && (ctrlval & sca3000_addresses[num][2])) { 1014 ret = sca3000_write_ctrl_reg(st, 1015 SCA3000_REG_CTRL_SEL_MD_CTRL, 1016 ctrlval & 1017 ~(sca3000_addresses[num][2])); 1018 if (ret) 1019 goto exit_point; 1020 st->mo_det_use_count--; 1021 } 1022 1023 /* read current value of mode register */ 1024 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 1025 if (ret) 1026 goto exit_point; 1027 /* if off and should be on */ 1028 if ((st->mo_det_use_count) 1029 && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)) 1030 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1031 (st->rx[0] & ~protect_mask) 1032 | SCA3000_MEAS_MODE_MOT_DET); 1033 /* if on and should be off */ 1034 else if (!(st->mo_det_use_count) 1035 && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET)) 1036 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1037 (st->rx[0] & ~protect_mask)); 1038exit_point: 1039 mutex_unlock(&st->lock); 1040 1041 return ret; 1042} 1043 1044/* Free fall detector related event attribute */ 1045static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en, 1046 in_accel_x&y&z_mag_falling_en, 1047 S_IRUGO | S_IWUSR, 1048 sca3000_query_free_fall_mode, 1049 sca3000_set_free_fall_mode, 1050 0); 1051 1052static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period, 1053 in_accel_x&y&z_mag_falling_period, 1054 "0.226"); 1055 1056static struct attribute *sca3000_event_attributes[] = { 1057 &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr, 1058 &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr, 1059 NULL, 1060}; 1061 1062static struct attribute_group sca3000_event_attribute_group = { 1063 .attrs = sca3000_event_attributes, 1064 .name = "events", 1065}; 1066 1067/** 1068 * sca3000_clean_setup() get the device into a predictable state 1069 * 1070 * Devices use flash memory to store many of the register values 1071 * and hence can come up in somewhat unpredictable states. 1072 * Hence reset everything on driver load. 1073 **/ 1074static int sca3000_clean_setup(struct sca3000_state *st) 1075{ 1076 int ret; 1077 1078 mutex_lock(&st->lock); 1079 /* Ensure all interrupts have been acknowledged */ 1080 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1); 1081 if (ret) 1082 goto error_ret; 1083 1084 /* Turn off all motion detection channels */ 1085 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL); 1086 if (ret < 0) 1087 goto error_ret; 1088 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL, 1089 ret & SCA3000_MD_CTRL_PROT_MASK); 1090 if (ret) 1091 goto error_ret; 1092 1093 /* Disable ring buffer */ 1094 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL); 1095 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, 1096 (ret & SCA3000_OUT_CTRL_PROT_MASK) 1097 | SCA3000_OUT_CTRL_BUF_X_EN 1098 | SCA3000_OUT_CTRL_BUF_Y_EN 1099 | SCA3000_OUT_CTRL_BUF_Z_EN 1100 | SCA3000_OUT_CTRL_BUF_DIV_4); 1101 if (ret) 1102 goto error_ret; 1103 /* Enable interrupts, relevant to mode and set up as active low */ 1104 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1); 1105 if (ret) 1106 goto error_ret; 1107 ret = sca3000_write_reg(st, 1108 SCA3000_REG_ADDR_INT_MASK, 1109 (ret & SCA3000_INT_MASK_PROT_MASK) 1110 | SCA3000_INT_MASK_ACTIVE_LOW); 1111 if (ret) 1112 goto error_ret; 1113 /* 1114 * Select normal measurement mode, free fall off, ring off 1115 * Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5 1116 * as that occurs in one of the example on the datasheet 1117 */ 1118 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); 1119 if (ret) 1120 goto error_ret; 1121 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, 1122 (st->rx[0] & SCA3000_MODE_PROT_MASK)); 1123 st->bpse = 11; 1124 1125error_ret: 1126 mutex_unlock(&st->lock); 1127 return ret; 1128} 1129 1130static const struct iio_info sca3000_info = { 1131 .attrs = &sca3000_attribute_group, 1132 .read_raw = &sca3000_read_raw, 1133 .event_attrs = &sca3000_event_attribute_group, 1134 .read_event_value = &sca3000_read_thresh, 1135 .write_event_value = &sca3000_write_thresh, 1136 .read_event_config = &sca3000_read_event_config, 1137 .write_event_config = &sca3000_write_event_config, 1138 .driver_module = THIS_MODULE, 1139}; 1140 1141static const struct iio_info sca3000_info_with_temp = { 1142 .attrs = &sca3000_attribute_group_with_temp, 1143 .read_raw = &sca3000_read_raw, 1144 .read_event_value = &sca3000_read_thresh, 1145 .write_event_value = &sca3000_write_thresh, 1146 .read_event_config = &sca3000_read_event_config, 1147 .write_event_config = &sca3000_write_event_config, 1148 .driver_module = THIS_MODULE, 1149}; 1150 1151static int sca3000_probe(struct spi_device *spi) 1152{ 1153 int ret; 1154 struct sca3000_state *st; 1155 struct iio_dev *indio_dev; 1156 1157 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 1158 if (!indio_dev) 1159 return -ENOMEM; 1160 1161 st = iio_priv(indio_dev); 1162 spi_set_drvdata(spi, indio_dev); 1163 st->us = spi; 1164 mutex_init(&st->lock); 1165 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi) 1166 ->driver_data]; 1167 1168 indio_dev->dev.parent = &spi->dev; 1169 indio_dev->name = spi_get_device_id(spi)->name; 1170 if (st->info->temp_output) 1171 indio_dev->info = &sca3000_info_with_temp; 1172 else { 1173 indio_dev->info = &sca3000_info; 1174 indio_dev->channels = sca3000_channels; 1175 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels); 1176 } 1177 indio_dev->modes = INDIO_DIRECT_MODE; 1178 1179 sca3000_configure_ring(indio_dev); 1180 ret = iio_device_register(indio_dev); 1181 if (ret < 0) 1182 return ret; 1183 1184 ret = iio_buffer_register(indio_dev, 1185 sca3000_channels, 1186 ARRAY_SIZE(sca3000_channels)); 1187 if (ret < 0) 1188 goto error_unregister_dev; 1189 if (indio_dev->buffer) { 1190 iio_scan_mask_set(indio_dev, indio_dev->buffer, 0); 1191 iio_scan_mask_set(indio_dev, indio_dev->buffer, 1); 1192 iio_scan_mask_set(indio_dev, indio_dev->buffer, 2); 1193 } 1194 1195 if (spi->irq) { 1196 ret = request_threaded_irq(spi->irq, 1197 NULL, 1198 &sca3000_event_handler, 1199 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1200 "sca3000", 1201 indio_dev); 1202 if (ret) 1203 goto error_unregister_ring; 1204 } 1205 sca3000_register_ring_funcs(indio_dev); 1206 ret = sca3000_clean_setup(st); 1207 if (ret) 1208 goto error_free_irq; 1209 return 0; 1210 1211error_free_irq: 1212 if (spi->irq) 1213 free_irq(spi->irq, indio_dev); 1214error_unregister_ring: 1215 iio_buffer_unregister(indio_dev); 1216error_unregister_dev: 1217 iio_device_unregister(indio_dev); 1218 return ret; 1219} 1220 1221static int sca3000_stop_all_interrupts(struct sca3000_state *st) 1222{ 1223 int ret; 1224 1225 mutex_lock(&st->lock); 1226 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1); 1227 if (ret) 1228 goto error_ret; 1229 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK, 1230 (st->rx[0] & 1231 ~(SCA3000_INT_MASK_RING_THREE_QUARTER | 1232 SCA3000_INT_MASK_RING_HALF | 1233 SCA3000_INT_MASK_ALL_INTS))); 1234error_ret: 1235 mutex_unlock(&st->lock); 1236 return ret; 1237} 1238 1239static int sca3000_remove(struct spi_device *spi) 1240{ 1241 struct iio_dev *indio_dev = spi_get_drvdata(spi); 1242 struct sca3000_state *st = iio_priv(indio_dev); 1243 1244 /* Must ensure no interrupts can be generated after this! */ 1245 sca3000_stop_all_interrupts(st); 1246 if (spi->irq) 1247 free_irq(spi->irq, indio_dev); 1248 iio_device_unregister(indio_dev); 1249 iio_buffer_unregister(indio_dev); 1250 sca3000_unconfigure_ring(indio_dev); 1251 1252 return 0; 1253} 1254 1255static const struct spi_device_id sca3000_id[] = { 1256 {"sca3000_d01", d01}, 1257 {"sca3000_e02", e02}, 1258 {"sca3000_e04", e04}, 1259 {"sca3000_e05", e05}, 1260 {} 1261}; 1262MODULE_DEVICE_TABLE(spi, sca3000_id); 1263 1264static struct spi_driver sca3000_driver = { 1265 .driver = { 1266 .name = "sca3000", 1267 .owner = THIS_MODULE, 1268 }, 1269 .probe = sca3000_probe, 1270 .remove = sca3000_remove, 1271 .id_table = sca3000_id, 1272}; 1273module_spi_driver(sca3000_driver); 1274 1275MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1276MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver"); 1277MODULE_LICENSE("GPL v2"); 1278