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