2 * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
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.
8 * Copyright (c) 2009 Jonathan Cameron <jic23@cam.ac.uk>
10 * See industrialio/accels/sca3000.h for comments.
13 #include <linux/interrupt.h>
14 #include <linux/gpio.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/spi/spi.h>
20 #include <linux/sysfs.h>
23 #include "../ring_generic.h"
28 enum sca3000_variant {
35 /* Note where option modes are not defined, the chip simply does not
37 * Other chips in the sca3000 series use i2c and are not included here.
39 * Some of these devices are only listed in the family data sheet and
40 * do not actually appear to be available.
42 static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
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},
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},
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},
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},
82 int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
84 st->tx[0] = SCA3000_WRITE_REG(address);
86 return spi_write(st->us, st->tx, 2);
89 int sca3000_read_data_short(struct sca3000_state *st,
90 uint8_t reg_address_high,
93 struct spi_message msg;
94 struct spi_transfer xfer[2] = {
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);
108 return spi_sync(st->us, &msg);
112 * sca3000_reg_lock_on() test if the ctrl register lock is on
116 static int sca3000_reg_lock_on(struct sca3000_state *st)
120 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
124 return !(st->rx[0] & SCA3000_LOCKED);
128 * __sca3000_unlock_reg_lock() unlock the control registers
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
134 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
136 struct spi_message msg;
137 struct spi_transfer xfer[3] = {
145 .tx_buf = st->tx + 2,
148 .tx_buf = st->tx + 4,
151 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
153 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
155 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
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);
162 return spi_sync(st->us, &msg);
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
170 * Certain control registers are protected against overwriting by the lock
171 * register and use a shared write address. This function allows writing of
175 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
182 ret = sca3000_reg_lock_on(st);
186 ret = __sca3000_unlock_reg_lock(st);
191 /* Set the control select register */
192 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
196 /* Write the actual value into the register */
197 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
203 /* Crucial that lock is called before calling this */
205 * sca3000_read_ctrl_reg() read from lock protected control register.
209 static int sca3000_read_ctrl_reg(struct sca3000_state *st,
214 ret = sca3000_reg_lock_on(st);
218 ret = __sca3000_unlock_reg_lock(st);
222 /* Set the control select register */
223 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
226 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_CTRL_DATA, 1);
237 * sca3000_check_status() check the status register
239 * Only used for debugging purposes
241 static int sca3000_check_status(struct device *dev)
244 struct iio_dev *indio_dev = dev_get_drvdata(dev);
245 struct sca3000_state *st = indio_dev->dev_data;
247 mutex_lock(&st->lock);
248 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
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");
257 mutex_unlock(&st->lock);
260 #endif /* SCA3000_DEBUG */
264 * sca3000_show_reg() - sysfs interface to read the chip revision number
266 static ssize_t sca3000_show_rev(struct device *dev,
267 struct device_attribute *attr,
271 struct iio_dev *dev_info = dev_get_drvdata(dev);
272 struct sca3000_state *st = dev_info->dev_data;
274 mutex_lock(&st->lock);
275 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_REVID, 1);
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);
283 mutex_unlock(&st->lock);
285 return ret ? ret : len;
289 * sca3000_show_available_measurement_modes() display available modes
291 * This is all read from chip specific data in the driver. Not all
292 * of the sca3000 series support modes other than normal.
295 sca3000_show_available_measurement_modes(struct device *dev,
296 struct device_attribute *attr,
299 struct iio_dev *dev_info = dev_get_drvdata(dev);
300 struct sca3000_state *st = dev_info->dev_data;
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");
308 case SCA3000_OP_MODE_BYPASS:
309 len += sprintf(buf + len, ", 1 - bypass mode");
312 switch (st->info->option_mode_2) {
313 case SCA3000_OP_MODE_WIDE:
314 len += sprintf(buf + len, ", 2 - wide mode");
317 /* always supported */
318 len += sprintf(buf + len, " 3 - motion detection\n");
324 * sca3000_show_measurmenet_mode() sysfs read of current mode
327 sca3000_show_measurement_mode(struct device *dev,
328 struct device_attribute *attr,
331 struct iio_dev *dev_info = dev_get_drvdata(dev);
332 struct sca3000_state *st = dev_info->dev_data;
335 mutex_lock(&st->lock);
336 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
339 /* mask bottom 2 bits - only ones that are relevant */
342 case SCA3000_MEAS_MODE_NORMAL:
343 len += sprintf(buf + len, "0 - normal mode\n");
345 case SCA3000_MEAS_MODE_MOT_DET:
346 len += sprintf(buf + len, "3 - motion detection\n");
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");
353 case SCA3000_OP_MODE_BYPASS:
354 len += sprintf(buf + len, "1 - bypass mode\n");
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");
368 mutex_unlock(&st->lock);
370 return ret ? ret : len;
374 * sca3000_store_measurement_mode() set the current mode
377 sca3000_store_measurement_mode(struct device *dev,
378 struct device_attribute *attr,
382 struct iio_dev *dev_info = dev_get_drvdata(dev);
383 struct sca3000_state *st = dev_info->dev_data;
388 mutex_lock(&st->lock);
389 ret = strict_strtol(buf, 10, &val);
392 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
396 st->rx[0] |= (val & mask);
397 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, st->rx[0]);
400 mutex_unlock(&st->lock);
405 mutex_unlock(&st->lock);
411 /* Not even vaguely standard attributes so defined here rather than
412 * in the relevant IIO core headers
414 static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO,
415 sca3000_show_available_measurement_modes,
418 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
419 sca3000_show_measurement_mode,
420 sca3000_store_measurement_mode,
423 /* More standard attributes */
425 static IIO_DEV_ATTR_REV(sca3000_show_rev);
427 #define SCA3000_INFO_MASK \
428 (1 << IIO_CHAN_INFO_SCALE_SHARED)
429 #define SCA3000_EVENT_MASK \
430 (IIO_EV_BIT(IIO_EV_TYPE_MAG, IIO_EV_DIR_RISING))
432 static struct iio_chan_spec sca3000_channels[] = {
433 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X, SCA3000_INFO_MASK,
434 0, 0, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
435 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y, SCA3000_INFO_MASK,
436 1, 1, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
437 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z, SCA3000_INFO_MASK,
438 2, 2, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
441 static u8 sca3000_addresses[3][3] = {
442 [0] = {SCA3000_REG_ADDR_X_MSB, SCA3000_REG_CTRL_SEL_MD_X_TH,
443 SCA3000_MD_CTRL_OR_X},
444 [1] = {SCA3000_REG_ADDR_Y_MSB, SCA3000_REG_CTRL_SEL_MD_Y_TH,
445 SCA3000_MD_CTRL_OR_Y},
446 [2] = {SCA3000_REG_ADDR_Z_MSB, SCA3000_REG_CTRL_SEL_MD_Z_TH,
447 SCA3000_MD_CTRL_OR_Z},
450 static int sca3000_read_raw(struct iio_dev *indio_dev,
451 struct iio_chan_spec const *chan,
456 struct sca3000_state *st = indio_dev->dev_data;
462 mutex_lock(&st->lock);
463 if (st->mo_det_use_count) {
464 mutex_unlock(&st->lock);
467 address = sca3000_addresses[chan->address][0];
468 ret = sca3000_read_data_short(st, address, 2);
470 mutex_unlock(&st->lock);
473 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
474 *val = ((*val) << (sizeof(*val)*8 - 13)) >>
475 (sizeof(*val)*8 - 13);
476 mutex_unlock(&st->lock);
478 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
480 if (chan->type == IIO_ACCEL)
481 *val2 = st->info->scale;
482 else /* temperature */
484 return IIO_VAL_INT_PLUS_MICRO;
491 * sca3000_read_av_freq() sysfs function to get available frequencies
493 * The later modes are only relevant to the ring buffer - and depend on current
494 * mode. Note that data sheet gives rather wide tolerances for these so integer
495 * division will give good enough answer and not all chips have them specified
498 static ssize_t sca3000_read_av_freq(struct device *dev,
499 struct device_attribute *attr,
502 struct iio_dev *indio_dev = dev_get_drvdata(dev);
503 struct sca3000_state *st = indio_dev->dev_data;
504 int len = 0, ret, val;
506 mutex_lock(&st->lock);
507 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
509 mutex_unlock(&st->lock);
513 switch (val & 0x03) {
514 case SCA3000_MEAS_MODE_NORMAL:
515 len += sprintf(buf + len, "%d %d %d\n",
516 st->info->measurement_mode_freq,
517 st->info->measurement_mode_freq/2,
518 st->info->measurement_mode_freq/4);
520 case SCA3000_MEAS_MODE_OP_1:
521 len += sprintf(buf + len, "%d %d %d\n",
522 st->info->option_mode_1_freq,
523 st->info->option_mode_1_freq/2,
524 st->info->option_mode_1_freq/4);
526 case SCA3000_MEAS_MODE_OP_2:
527 len += sprintf(buf + len, "%d %d %d\n",
528 st->info->option_mode_2_freq,
529 st->info->option_mode_2_freq/2,
530 st->info->option_mode_2_freq/4);
538 * __sca3000_get_base_frequency() obtain mode specific base frequency
542 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
543 const struct sca3000_chip_info *info,
548 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
551 switch (0x03 & st->rx[0]) {
552 case SCA3000_MEAS_MODE_NORMAL:
553 *base_freq = info->measurement_mode_freq;
555 case SCA3000_MEAS_MODE_OP_1:
556 *base_freq = info->option_mode_1_freq;
558 case SCA3000_MEAS_MODE_OP_2:
559 *base_freq = info->option_mode_2_freq;
567 * sca3000_read_frequency() sysfs interface to get the current frequency
569 static ssize_t sca3000_read_frequency(struct device *dev,
570 struct device_attribute *attr,
573 struct iio_dev *indio_dev = dev_get_drvdata(dev);
574 struct sca3000_state *st = indio_dev->dev_data;
575 int ret, len = 0, base_freq = 0, val;
577 mutex_lock(&st->lock);
578 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
581 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
582 mutex_unlock(&st->lock);
587 switch (val & 0x03) {
590 len = sprintf(buf, "%d\n", base_freq);
593 len = sprintf(buf, "%d\n", base_freq/2);
596 len = sprintf(buf, "%d\n", base_freq/4);
602 mutex_unlock(&st->lock);
608 * sca3000_set_frequency() sysfs interface to set the current frequency
610 static ssize_t sca3000_set_frequency(struct device *dev,
611 struct device_attribute *attr,
615 struct iio_dev *indio_dev = dev_get_drvdata(dev);
616 struct sca3000_state *st = indio_dev->dev_data;
617 int ret, base_freq = 0;
621 ret = strict_strtol(buf, 10, &val);
625 mutex_lock(&st->lock);
626 /* What mode are we in? */
627 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
629 goto error_free_lock;
631 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
633 goto error_free_lock;
638 if (val == base_freq/2) {
639 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2;
640 } else if (val == base_freq/4) {
641 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4;
642 } else if (val != base_freq) {
644 goto error_free_lock;
646 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
649 mutex_unlock(&st->lock);
651 return ret ? ret : len;
654 /* Should only really be registered if ring buffer support is compiled in.
655 * Does no harm however and doing it right would add a fair bit of complexity
657 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
659 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
660 sca3000_read_frequency,
661 sca3000_set_frequency);
665 * sca3000_read_temp() sysfs interface to get the temperature when available
667 * The alignment of data in here is downright odd. See data sheet.
668 * Converting this into a meaningful value is left to inline functions in
669 * userspace part of header.
671 static ssize_t sca3000_read_temp(struct device *dev,
672 struct device_attribute *attr,
675 struct iio_dev *indio_dev = dev_get_drvdata(dev);
676 struct sca3000_state *st = indio_dev->dev_data;
679 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_TEMP_MSB, 2);
682 val = ((st->rx[0] & 0x3F) << 3) | ((st->rx[1] & 0xE0) >> 5);
684 return sprintf(buf, "%d\n", val);
689 static IIO_DEV_ATTR_TEMP_RAW(sca3000_read_temp);
691 static IIO_CONST_ATTR_TEMP_SCALE("0.555556");
692 static IIO_CONST_ATTR_TEMP_OFFSET("-214.6");
695 * sca3000_read_thresh() - query of a threshold
697 static int sca3000_read_thresh(struct iio_dev *indio_dev,
702 struct sca3000_state *st = indio_dev->dev_data;
703 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
704 mutex_lock(&st->lock);
705 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]);
706 mutex_unlock(&st->lock);
711 for_each_set_bit(i, (unsigned long *)&ret,
712 ARRAY_SIZE(st->info->mot_det_mult_y))
713 *val += st->info->mot_det_mult_y[i];
715 for_each_set_bit(i, (unsigned long *)&ret,
716 ARRAY_SIZE(st->info->mot_det_mult_xz))
717 *val += st->info->mot_det_mult_xz[i];
723 * sca3000_write_thresh() control of threshold
725 static int sca3000_write_thresh(struct iio_dev *indio_dev,
729 struct sca3000_state *st = indio_dev->dev_data;
730 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
736 i = ARRAY_SIZE(st->info->mot_det_mult_y);
738 if (val >= st->info->mot_det_mult_y[--i]) {
739 nonlinear |= (1 << i);
740 val -= st->info->mot_det_mult_y[i];
743 i = ARRAY_SIZE(st->info->mot_det_mult_xz);
745 if (val >= st->info->mot_det_mult_xz[--i]) {
746 nonlinear |= (1 << i);
747 val -= st->info->mot_det_mult_xz[i];
751 mutex_lock(&st->lock);
752 ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear);
753 mutex_unlock(&st->lock);
758 static struct attribute *sca3000_attributes[] = {
759 &iio_dev_attr_revision.dev_attr.attr,
760 &iio_dev_attr_measurement_mode_available.dev_attr.attr,
761 &iio_dev_attr_measurement_mode.dev_attr.attr,
762 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
763 &iio_dev_attr_sampling_frequency.dev_attr.attr,
767 static struct attribute *sca3000_attributes_with_temp[] = {
768 &iio_dev_attr_revision.dev_attr.attr,
769 &iio_dev_attr_measurement_mode_available.dev_attr.attr,
770 &iio_dev_attr_measurement_mode.dev_attr.attr,
771 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
772 &iio_dev_attr_sampling_frequency.dev_attr.attr,
773 /* Only present if temp sensor is */
774 &iio_dev_attr_temp_raw.dev_attr.attr,
775 &iio_const_attr_temp_offset.dev_attr.attr,
776 &iio_const_attr_temp_scale.dev_attr.attr,
780 static const struct attribute_group sca3000_attribute_group = {
781 .attrs = sca3000_attributes,
784 static const struct attribute_group sca3000_attribute_group_with_temp = {
785 .attrs = sca3000_attributes_with_temp,
788 /* RING RELATED interrupt handler */
789 /* depending on event, push to the ring buffer event chrdev or the event one */
792 * sca3000_event_handler() - handling ring and non ring events
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.
798 static irqreturn_t sca3000_event_handler(int irq, void *private)
800 struct iio_dev *indio_dev = private;
801 struct sca3000_state *st;
803 s64 last_timestamp = iio_get_time_ns();
805 st = indio_dev->dev_data;
806 /* Could lead if badly timed to an extra read of status reg,
807 * but ensures no interrupt is missed.
809 mutex_lock(&st->lock);
810 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
812 mutex_unlock(&st->lock);
816 sca3000_ring_int_process(val, st->indio_dev->ring);
818 if (val & SCA3000_INT_STATUS_FREE_FALL)
819 iio_push_event(st->indio_dev, 0,
820 IIO_MOD_EVENT_CODE(IIO_EV_CLASS_ACCEL,
822 IIO_EV_MOD_X_AND_Y_AND_Z,
827 if (val & SCA3000_INT_STATUS_Y_TRIGGER)
828 iio_push_event(st->indio_dev, 0,
829 IIO_MOD_EVENT_CODE(IIO_EV_CLASS_ACCEL,
836 if (val & SCA3000_INT_STATUS_X_TRIGGER)
837 iio_push_event(st->indio_dev, 0,
838 IIO_MOD_EVENT_CODE(IIO_EV_CLASS_ACCEL,
845 if (val & SCA3000_INT_STATUS_Z_TRIGGER)
846 iio_push_event(st->indio_dev, 0,
847 IIO_MOD_EVENT_CODE(IIO_EV_CLASS_ACCEL,
859 * sca3000_read_event_config() what events are enabled
861 static int sca3000_read_event_config(struct iio_dev *indio_dev,
864 struct sca3000_state *st = indio_dev->dev_data;
866 u8 protect_mask = 0x03;
867 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
869 /* read current value of mode register */
870 mutex_lock(&st->lock);
871 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
875 if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
878 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
881 /* only supporting logical or's for now */
882 ret = !!(ret & sca3000_addresses[num][2]);
885 mutex_unlock(&st->lock);
890 * sca3000_query_free_fall_mode() is free fall mode enabled
892 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
893 struct device_attribute *attr,
897 struct iio_dev *indio_dev = dev_get_drvdata(dev);
898 struct sca3000_state *st = indio_dev->dev_data;
901 mutex_lock(&st->lock);
902 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
904 mutex_unlock(&st->lock);
907 len = sprintf(buf, "%d\n",
908 !!(val & SCA3000_FREE_FALL_DETECT));
913 * sca3000_set_free_fall_mode() simple on off control for free fall int
915 * In these chips the free fall detector should send an interrupt if
916 * the device falls more than 25cm. This has not been tested due
920 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
921 struct device_attribute *attr,
925 struct iio_dev *indio_dev = dev_get_drvdata(dev);
926 struct sca3000_state *st = indio_dev->dev_data;
929 u8 protect_mask = SCA3000_FREE_FALL_DETECT;
931 mutex_lock(&st->lock);
932 ret = strict_strtol(buf, 10, &val);
936 /* read current value of mode register */
937 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
941 /*if off and should be on*/
942 if (val && !(st->rx[0] & protect_mask))
943 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
944 (st->rx[0] | SCA3000_FREE_FALL_DETECT));
945 /* if on and should be off */
946 else if (!val && (st->rx[0] & protect_mask))
947 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
948 (st->rx[0] & ~protect_mask));
950 mutex_unlock(&st->lock);
952 return ret ? ret : len;
956 * sca3000_set_mo_det() simple on off control for motion detector
958 * This is a per axis control, but enabling any will result in the
959 * motion detector unit being enabled.
960 * N.B. enabling motion detector stops normal data acquisition.
961 * There is a complexity in knowing which mode to return to when
962 * this mode is disabled. Currently normal mode is assumed.
964 static int sca3000_write_event_config(struct iio_dev *indio_dev,
968 struct sca3000_state *st = indio_dev->dev_data;
970 u8 protect_mask = 0x03;
971 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
973 mutex_lock(&st->lock);
974 /* First read the motion detector config to find out if
976 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
980 /* Off and should be on */
981 if (state && !(ctrlval & sca3000_addresses[num][2])) {
982 ret = sca3000_write_ctrl_reg(st,
983 SCA3000_REG_CTRL_SEL_MD_CTRL,
985 sca3000_addresses[num][2]);
988 st->mo_det_use_count++;
989 } else if (!state && (ctrlval & sca3000_addresses[num][2])) {
990 ret = sca3000_write_ctrl_reg(st,
991 SCA3000_REG_CTRL_SEL_MD_CTRL,
993 ~(sca3000_addresses[num][2]));
996 st->mo_det_use_count--;
999 /* read current value of mode register */
1000 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
1003 /*if off and should be on*/
1004 if ((st->mo_det_use_count)
1005 && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1006 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1007 (st->rx[0] & ~protect_mask)
1008 | SCA3000_MEAS_MODE_MOT_DET);
1009 /* if on and should be off */
1010 else if (!(st->mo_det_use_count)
1011 && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1012 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1013 (st->rx[0] & ~protect_mask));
1015 mutex_unlock(&st->lock);
1020 /* Free fall detector related event attribute */
1021 static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en,
1022 accel_x&y&z_mag_falling_en,
1024 sca3000_query_free_fall_mode,
1025 sca3000_set_free_fall_mode,
1028 static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period,
1029 accel_x&y&z_mag_falling_period,
1032 static struct attribute *sca3000_event_attributes[] = {
1033 &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1034 &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1038 static struct attribute_group sca3000_event_attribute_group = {
1039 .attrs = sca3000_event_attributes,
1043 * sca3000_clean_setup() get the device into a predictable state
1045 * Devices use flash memory to store many of the register values
1046 * and hence can come up in somewhat unpredictable states.
1047 * Hence reset everything on driver load.
1049 static int sca3000_clean_setup(struct sca3000_state *st)
1053 mutex_lock(&st->lock);
1054 /* Ensure all interrupts have been acknowledged */
1055 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
1059 /* Turn off all motion detection channels */
1060 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1063 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
1064 ret & SCA3000_MD_CTRL_PROT_MASK);
1068 /* Disable ring buffer */
1069 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
1070 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
1071 (ret & SCA3000_OUT_CTRL_PROT_MASK)
1072 | SCA3000_OUT_CTRL_BUF_X_EN
1073 | SCA3000_OUT_CTRL_BUF_Y_EN
1074 | SCA3000_OUT_CTRL_BUF_Z_EN
1075 | SCA3000_OUT_CTRL_BUF_DIV_4);
1078 /* Enable interrupts, relevant to mode and set up as active low */
1079 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1082 ret = sca3000_write_reg(st,
1083 SCA3000_REG_ADDR_INT_MASK,
1084 (ret & SCA3000_INT_MASK_PROT_MASK)
1085 | SCA3000_INT_MASK_ACTIVE_LOW);
1088 /* Select normal measurement mode, free fall off, ring off */
1089 /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1090 * as that occurs in one of the example on the datasheet */
1091 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
1094 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1095 (st->rx[0] & SCA3000_MODE_PROT_MASK));
1099 mutex_unlock(&st->lock);
1103 static const struct iio_info sca3000_info = {
1104 .attrs = &sca3000_attribute_group,
1105 .read_raw = &sca3000_read_raw,
1106 .num_interrupt_lines = 1,
1107 .event_attrs = &sca3000_event_attribute_group,
1108 .read_event_value = &sca3000_read_thresh,
1109 .write_event_value = &sca3000_write_thresh,
1110 .read_event_config = &sca3000_read_event_config,
1111 .write_event_config = &sca3000_write_event_config,
1112 .driver_module = THIS_MODULE,
1115 static const struct iio_info sca3000_info_with_temp = {
1116 .attrs = &sca3000_attribute_group_with_temp,
1117 .read_raw = &sca3000_read_raw,
1118 .read_event_value = &sca3000_read_thresh,
1119 .write_event_value = &sca3000_write_thresh,
1120 .read_event_config = &sca3000_read_event_config,
1121 .write_event_config = &sca3000_write_event_config,
1122 .driver_module = THIS_MODULE,
1125 static int __devinit sca3000_probe(struct spi_device *spi)
1127 int ret, regdone = 0;
1128 struct sca3000_state *st;
1130 st = kzalloc(sizeof(struct sca3000_state), GFP_KERNEL);
1135 spi_set_drvdata(spi, st);
1138 mutex_init(&st->lock);
1139 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
1142 st->indio_dev = iio_allocate_device(0);
1143 if (st->indio_dev == NULL) {
1145 goto error_clear_st;
1147 st->indio_dev->dev.parent = &spi->dev;
1148 st->indio_dev->name = spi_get_device_id(spi)->name;
1149 if (st->info->temp_output)
1150 st->indio_dev->info = &sca3000_info_with_temp;
1152 st->indio_dev->info = &sca3000_info;
1153 st->indio_dev->channels = sca3000_channels;
1154 st->indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
1156 st->indio_dev->dev_data = (void *)(st);
1157 st->indio_dev->modes = INDIO_DIRECT_MODE;
1159 sca3000_configure_ring(st->indio_dev);
1160 ret = iio_device_register(st->indio_dev);
1162 goto error_free_dev;
1164 ret = iio_ring_buffer_register_ex(st->indio_dev->ring, 0,
1166 ARRAY_SIZE(sca3000_channels));
1168 goto error_unregister_dev;
1169 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
1170 ret = request_threaded_irq(spi->irq,
1172 &sca3000_event_handler,
1173 IRQF_TRIGGER_FALLING,
1177 goto error_unregister_ring;
1179 sca3000_register_ring_funcs(st->indio_dev);
1180 ret = sca3000_clean_setup(st);
1182 goto error_free_irq;
1186 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1187 free_irq(spi->irq, st->indio_dev);
1188 error_unregister_ring:
1189 iio_ring_buffer_unregister(st->indio_dev->ring);
1190 error_unregister_dev:
1193 iio_device_unregister(st->indio_dev);
1195 iio_free_device(st->indio_dev);
1202 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1206 mutex_lock(&st->lock);
1207 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1210 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1212 ~(SCA3000_INT_MASK_RING_THREE_QUARTER |
1213 SCA3000_INT_MASK_RING_HALF |
1214 SCA3000_INT_MASK_ALL_INTS)));
1216 mutex_unlock(&st->lock);
1220 static int sca3000_remove(struct spi_device *spi)
1222 struct sca3000_state *st = spi_get_drvdata(spi);
1223 struct iio_dev *indio_dev = st->indio_dev;
1225 /* Must ensure no interrupts can be generated after this!*/
1226 ret = sca3000_stop_all_interrupts(st);
1229 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1230 free_irq(spi->irq, indio_dev);
1231 iio_ring_buffer_unregister(indio_dev->ring);
1232 sca3000_unconfigure_ring(indio_dev);
1233 iio_device_unregister(indio_dev);
1240 static const struct spi_device_id sca3000_id[] = {
1241 {"sca3000_d01", d01},
1242 {"sca3000_e02", e02},
1243 {"sca3000_e04", e04},
1244 {"sca3000_e05", e05},
1248 static struct spi_driver sca3000_driver = {
1251 .owner = THIS_MODULE,
1253 .probe = sca3000_probe,
1254 .remove = __devexit_p(sca3000_remove),
1255 .id_table = sca3000_id,
1258 static __init int sca3000_init(void)
1260 return spi_register_driver(&sca3000_driver);
1262 module_init(sca3000_init);
1264 static __exit void sca3000_exit(void)
1266 spi_unregister_driver(&sca3000_driver);
1268 module_exit(sca3000_exit);
1270 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1271 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1272 MODULE_LICENSE("GPL v2");