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@kernel.org>
10 * See industrialio/accels/sca3000.h for comments.
13 #include <linux/interrupt.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>
28 enum sca3000_variant {
36 * Note where option modes are not defined, the chip simply does not
38 * Other chips in the sca3000 series use i2c and are not included here.
40 * Some of these devices are only listed in the family data sheet and
41 * do not actually appear to be available.
43 static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
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},
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},
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},
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},
83 int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
85 st->tx[0] = SCA3000_WRITE_REG(address);
87 return spi_write(st->us, st->tx, 2);
90 int sca3000_read_data_short(struct sca3000_state *st,
91 uint8_t reg_address_high,
94 struct spi_transfer xfer[2] = {
103 st->tx[0] = SCA3000_READ_REG(reg_address_high);
105 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
109 * sca3000_reg_lock_on() test if the ctrl register lock is on
113 static int sca3000_reg_lock_on(struct sca3000_state *st)
117 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
121 return !(st->rx[0] & SCA3000_LOCKED);
125 * __sca3000_unlock_reg_lock() unlock the control registers
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
131 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
133 struct spi_transfer xfer[3] = {
141 .tx_buf = st->tx + 2,
144 .tx_buf = st->tx + 4,
147 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
149 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
151 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
154 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
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
162 * Certain control registers are protected against overwriting by the lock
163 * register and use a shared write address. This function allows writing of
167 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
174 ret = sca3000_reg_lock_on(st);
178 ret = __sca3000_unlock_reg_lock(st);
183 /* Set the control select register */
184 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
188 /* Write the actual value into the register */
189 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
196 * sca3000_read_ctrl_reg() read from lock protected control register.
200 static int sca3000_read_ctrl_reg(struct sca3000_state *st,
205 ret = sca3000_reg_lock_on(st);
209 ret = __sca3000_unlock_reg_lock(st);
213 /* Set the control select register */
214 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
217 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_CTRL_DATA, 1);
227 * sca3000_show_rev() - sysfs interface to read the chip revision number
229 static ssize_t sca3000_show_rev(struct device *dev,
230 struct device_attribute *attr,
234 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
235 struct sca3000_state *st = iio_priv(indio_dev);
237 mutex_lock(&st->lock);
238 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_REVID, 1);
241 len += sprintf(buf + len,
242 "major=%d, minor=%d\n",
243 st->rx[0] & SCA3000_REVID_MAJOR_MASK,
244 st->rx[0] & SCA3000_REVID_MINOR_MASK);
246 mutex_unlock(&st->lock);
248 return ret ? ret : len;
252 * sca3000_show_available_measurement_modes() display available modes
254 * This is all read from chip specific data in the driver. Not all
255 * of the sca3000 series support modes other than normal.
258 sca3000_show_available_measurement_modes(struct device *dev,
259 struct device_attribute *attr,
262 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
263 struct sca3000_state *st = iio_priv(indio_dev);
266 len += sprintf(buf + len, "0 - normal mode");
267 switch (st->info->option_mode_1) {
268 case SCA3000_OP_MODE_NARROW:
269 len += sprintf(buf + len, ", 1 - narrow mode");
271 case SCA3000_OP_MODE_BYPASS:
272 len += sprintf(buf + len, ", 1 - bypass mode");
275 switch (st->info->option_mode_2) {
276 case SCA3000_OP_MODE_WIDE:
277 len += sprintf(buf + len, ", 2 - wide mode");
280 /* always supported */
281 len += sprintf(buf + len, " 3 - motion detection\n");
287 * sca3000_show_measurement_mode() sysfs read of current mode
290 sca3000_show_measurement_mode(struct device *dev,
291 struct device_attribute *attr,
294 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
295 struct sca3000_state *st = iio_priv(indio_dev);
298 mutex_lock(&st->lock);
299 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
302 /* mask bottom 2 bits - only ones that are relevant */
305 case SCA3000_MEAS_MODE_NORMAL:
306 len += sprintf(buf + len, "0 - normal mode\n");
308 case SCA3000_MEAS_MODE_MOT_DET:
309 len += sprintf(buf + len, "3 - motion detection\n");
311 case SCA3000_MEAS_MODE_OP_1:
312 switch (st->info->option_mode_1) {
313 case SCA3000_OP_MODE_NARROW:
314 len += sprintf(buf + len, "1 - narrow mode\n");
316 case SCA3000_OP_MODE_BYPASS:
317 len += sprintf(buf + len, "1 - bypass mode\n");
321 case SCA3000_MEAS_MODE_OP_2:
322 switch (st->info->option_mode_2) {
323 case SCA3000_OP_MODE_WIDE:
324 len += sprintf(buf + len, "2 - wide mode\n");
331 mutex_unlock(&st->lock);
333 return ret ? ret : len;
337 * sca3000_store_measurement_mode() set the current mode
340 sca3000_store_measurement_mode(struct device *dev,
341 struct device_attribute *attr,
345 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
346 struct sca3000_state *st = iio_priv(indio_dev);
351 mutex_lock(&st->lock);
352 ret = kstrtou8(buf, 10, &val);
359 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
363 st->rx[0] |= (val & mask);
364 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, st->rx[0]);
367 mutex_unlock(&st->lock);
372 mutex_unlock(&st->lock);
379 * Not even vaguely standard attributes so defined here rather than
380 * in the relevant IIO core headers
382 static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO,
383 sca3000_show_available_measurement_modes,
386 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
387 sca3000_show_measurement_mode,
388 sca3000_store_measurement_mode,
391 /* More standard attributes */
393 static IIO_DEVICE_ATTR(revision, S_IRUGO, sca3000_show_rev, NULL, 0);
395 static const struct iio_event_spec sca3000_event = {
396 .type = IIO_EV_TYPE_MAG,
397 .dir = IIO_EV_DIR_RISING,
398 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
401 #define SCA3000_CHAN(index, mod) \
406 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
407 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
409 .scan_index = index, \
416 .event_spec = &sca3000_event, \
417 .num_event_specs = 1, \
420 static const struct iio_chan_spec sca3000_channels[] = {
421 SCA3000_CHAN(0, IIO_MOD_X),
422 SCA3000_CHAN(1, IIO_MOD_Y),
423 SCA3000_CHAN(2, IIO_MOD_Z),
426 static const struct iio_chan_spec sca3000_channels_with_temp[] = {
427 SCA3000_CHAN(0, IIO_MOD_X),
428 SCA3000_CHAN(1, IIO_MOD_Y),
429 SCA3000_CHAN(2, IIO_MOD_Z),
432 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
433 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
434 BIT(IIO_CHAN_INFO_OFFSET),
435 /* No buffer support */
440 static u8 sca3000_addresses[3][3] = {
441 [0] = {SCA3000_REG_ADDR_X_MSB, SCA3000_REG_CTRL_SEL_MD_X_TH,
442 SCA3000_MD_CTRL_OR_X},
443 [1] = {SCA3000_REG_ADDR_Y_MSB, SCA3000_REG_CTRL_SEL_MD_Y_TH,
444 SCA3000_MD_CTRL_OR_Y},
445 [2] = {SCA3000_REG_ADDR_Z_MSB, SCA3000_REG_CTRL_SEL_MD_Z_TH,
446 SCA3000_MD_CTRL_OR_Z},
449 static int sca3000_read_raw(struct iio_dev *indio_dev,
450 struct iio_chan_spec const *chan,
455 struct sca3000_state *st = iio_priv(indio_dev);
460 case IIO_CHAN_INFO_RAW:
461 mutex_lock(&st->lock);
462 if (chan->type == IIO_ACCEL) {
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);
477 /* get the temperature when available */
478 ret = sca3000_read_data_short(st,
479 SCA3000_REG_ADDR_TEMP_MSB, 2);
481 mutex_unlock(&st->lock);
484 *val = ((st->rx[0] & 0x3F) << 3) |
485 ((st->rx[1] & 0xE0) >> 5);
487 mutex_unlock(&st->lock);
489 case IIO_CHAN_INFO_SCALE:
491 if (chan->type == IIO_ACCEL)
492 *val2 = st->info->scale;
493 else /* temperature */
495 return IIO_VAL_INT_PLUS_MICRO;
496 case IIO_CHAN_INFO_OFFSET:
499 return IIO_VAL_INT_PLUS_MICRO;
506 * sca3000_read_av_freq() sysfs function to get available frequencies
508 * The later modes are only relevant to the ring buffer - and depend on current
509 * mode. Note that data sheet gives rather wide tolerances for these so integer
510 * division will give good enough answer and not all chips have them specified
513 static ssize_t sca3000_read_av_freq(struct device *dev,
514 struct device_attribute *attr,
517 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
518 struct sca3000_state *st = iio_priv(indio_dev);
519 int len = 0, ret, val;
521 mutex_lock(&st->lock);
522 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
524 mutex_unlock(&st->lock);
528 switch (val & 0x03) {
529 case SCA3000_MEAS_MODE_NORMAL:
530 len += sprintf(buf + len, "%d %d %d\n",
531 st->info->measurement_mode_freq,
532 st->info->measurement_mode_freq/2,
533 st->info->measurement_mode_freq/4);
535 case SCA3000_MEAS_MODE_OP_1:
536 len += sprintf(buf + len, "%d %d %d\n",
537 st->info->option_mode_1_freq,
538 st->info->option_mode_1_freq/2,
539 st->info->option_mode_1_freq/4);
541 case SCA3000_MEAS_MODE_OP_2:
542 len += sprintf(buf + len, "%d %d %d\n",
543 st->info->option_mode_2_freq,
544 st->info->option_mode_2_freq/2,
545 st->info->option_mode_2_freq/4);
553 * __sca3000_get_base_freq() obtain mode specific base frequency
557 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
558 const struct sca3000_chip_info *info,
563 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
566 switch (0x03 & st->rx[0]) {
567 case SCA3000_MEAS_MODE_NORMAL:
568 *base_freq = info->measurement_mode_freq;
570 case SCA3000_MEAS_MODE_OP_1:
571 *base_freq = info->option_mode_1_freq;
573 case SCA3000_MEAS_MODE_OP_2:
574 *base_freq = info->option_mode_2_freq;
582 * sca3000_read_frequency() sysfs interface to get the current frequency
584 static ssize_t sca3000_read_frequency(struct device *dev,
585 struct device_attribute *attr,
588 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
589 struct sca3000_state *st = iio_priv(indio_dev);
590 int ret, len = 0, base_freq = 0, val;
592 mutex_lock(&st->lock);
593 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
596 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
597 mutex_unlock(&st->lock);
602 switch (val & 0x03) {
605 len = sprintf(buf, "%d\n", base_freq);
608 len = sprintf(buf, "%d\n", base_freq/2);
611 len = sprintf(buf, "%d\n", base_freq/4);
617 mutex_unlock(&st->lock);
623 * sca3000_set_frequency() sysfs interface to set the current frequency
625 static ssize_t sca3000_set_frequency(struct device *dev,
626 struct device_attribute *attr,
630 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
631 struct sca3000_state *st = iio_priv(indio_dev);
632 int ret, base_freq = 0;
636 ret = kstrtoint(buf, 10, &val);
640 mutex_lock(&st->lock);
641 /* What mode are we in? */
642 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
644 goto error_free_lock;
646 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
648 goto error_free_lock;
653 if (val == base_freq/2) {
654 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2;
655 } else if (val == base_freq/4) {
656 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4;
657 } else if (val != base_freq) {
659 goto error_free_lock;
661 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
664 mutex_unlock(&st->lock);
666 return ret ? ret : len;
670 * Should only really be registered if ring buffer support is compiled in.
671 * Does no harm however and doing it right would add a fair bit of complexity
673 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
675 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
676 sca3000_read_frequency,
677 sca3000_set_frequency);
680 * sca3000_read_thresh() - query of a threshold
682 static int sca3000_read_thresh(struct iio_dev *indio_dev,
683 const struct iio_chan_spec *chan,
684 enum iio_event_type type,
685 enum iio_event_direction dir,
686 enum iio_event_info info,
690 struct sca3000_state *st = iio_priv(indio_dev);
691 int num = chan->channel2;
693 mutex_lock(&st->lock);
694 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]);
695 mutex_unlock(&st->lock);
700 for_each_set_bit(i, (unsigned long *)&ret,
701 ARRAY_SIZE(st->info->mot_det_mult_y))
702 *val += st->info->mot_det_mult_y[i];
704 for_each_set_bit(i, (unsigned long *)&ret,
705 ARRAY_SIZE(st->info->mot_det_mult_xz))
706 *val += st->info->mot_det_mult_xz[i];
712 * sca3000_write_thresh() control of threshold
714 static int sca3000_write_thresh(struct iio_dev *indio_dev,
715 const struct iio_chan_spec *chan,
716 enum iio_event_type type,
717 enum iio_event_direction dir,
718 enum iio_event_info info,
721 struct sca3000_state *st = iio_priv(indio_dev);
722 int num = chan->channel2;
728 i = ARRAY_SIZE(st->info->mot_det_mult_y);
730 if (val >= st->info->mot_det_mult_y[--i]) {
731 nonlinear |= (1 << i);
732 val -= st->info->mot_det_mult_y[i];
735 i = ARRAY_SIZE(st->info->mot_det_mult_xz);
737 if (val >= st->info->mot_det_mult_xz[--i]) {
738 nonlinear |= (1 << i);
739 val -= st->info->mot_det_mult_xz[i];
743 mutex_lock(&st->lock);
744 ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear);
745 mutex_unlock(&st->lock);
750 static struct attribute *sca3000_attributes[] = {
751 &iio_dev_attr_revision.dev_attr.attr,
752 &iio_dev_attr_measurement_mode_available.dev_attr.attr,
753 &iio_dev_attr_measurement_mode.dev_attr.attr,
754 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
755 &iio_dev_attr_sampling_frequency.dev_attr.attr,
759 static const struct attribute_group sca3000_attribute_group = {
760 .attrs = sca3000_attributes,
764 * sca3000_event_handler() - handling ring and non ring events
766 * Ring related interrupt handler. Depending on event, push to
767 * the ring buffer event chrdev or the event one.
769 * This function is complicated by the fact that the devices can signify ring
770 * and non ring events via the same interrupt line and they can only
771 * be distinguished via a read of the relevant status register.
773 static irqreturn_t sca3000_event_handler(int irq, void *private)
775 struct iio_dev *indio_dev = private;
776 struct sca3000_state *st = iio_priv(indio_dev);
778 s64 last_timestamp = iio_get_time_ns();
781 * Could lead if badly timed to an extra read of status reg,
782 * but ensures no interrupt is missed.
784 mutex_lock(&st->lock);
785 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
787 mutex_unlock(&st->lock);
791 sca3000_ring_int_process(val, indio_dev->buffer);
793 if (val & SCA3000_INT_STATUS_FREE_FALL)
794 iio_push_event(indio_dev,
795 IIO_MOD_EVENT_CODE(IIO_ACCEL,
797 IIO_MOD_X_AND_Y_AND_Z,
802 if (val & SCA3000_INT_STATUS_Y_TRIGGER)
803 iio_push_event(indio_dev,
804 IIO_MOD_EVENT_CODE(IIO_ACCEL,
811 if (val & SCA3000_INT_STATUS_X_TRIGGER)
812 iio_push_event(indio_dev,
813 IIO_MOD_EVENT_CODE(IIO_ACCEL,
820 if (val & SCA3000_INT_STATUS_Z_TRIGGER)
821 iio_push_event(indio_dev,
822 IIO_MOD_EVENT_CODE(IIO_ACCEL,
834 * sca3000_read_event_config() what events are enabled
836 static int sca3000_read_event_config(struct iio_dev *indio_dev,
837 const struct iio_chan_spec *chan,
838 enum iio_event_type type,
839 enum iio_event_direction dir)
841 struct sca3000_state *st = iio_priv(indio_dev);
843 u8 protect_mask = 0x03;
844 int num = chan->channel2;
846 /* read current value of mode register */
847 mutex_lock(&st->lock);
848 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
852 if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
855 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
858 /* only supporting logical or's for now */
859 ret = !!(ret & sca3000_addresses[num][2]);
862 mutex_unlock(&st->lock);
867 * sca3000_query_free_fall_mode() is free fall mode enabled
869 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
870 struct device_attribute *attr,
874 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
875 struct sca3000_state *st = iio_priv(indio_dev);
878 mutex_lock(&st->lock);
879 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
881 mutex_unlock(&st->lock);
884 return sprintf(buf, "%d\n", !!(val & SCA3000_FREE_FALL_DETECT));
888 * sca3000_set_free_fall_mode() simple on off control for free fall int
890 * In these chips the free fall detector should send an interrupt if
891 * the device falls more than 25cm. This has not been tested due
894 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
895 struct device_attribute *attr,
899 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
900 struct sca3000_state *st = iio_priv(indio_dev);
903 u8 protect_mask = SCA3000_FREE_FALL_DETECT;
905 mutex_lock(&st->lock);
906 ret = kstrtou8(buf, 10, &val);
910 /* read current value of mode register */
911 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
915 /* if off and should be on */
916 if (val && !(st->rx[0] & protect_mask))
917 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
918 (st->rx[0] | SCA3000_FREE_FALL_DETECT));
919 /* if on and should be off */
920 else if (!val && (st->rx[0] & protect_mask))
921 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
922 (st->rx[0] & ~protect_mask));
924 mutex_unlock(&st->lock);
926 return ret ? ret : len;
930 * sca3000_write_event_config() simple on off control for motion detector
932 * This is a per axis control, but enabling any will result in the
933 * motion detector unit being enabled.
934 * N.B. enabling motion detector stops normal data acquisition.
935 * There is a complexity in knowing which mode to return to when
936 * this mode is disabled. Currently normal mode is assumed.
938 static int sca3000_write_event_config(struct iio_dev *indio_dev,
939 const struct iio_chan_spec *chan,
940 enum iio_event_type type,
941 enum iio_event_direction dir,
944 struct sca3000_state *st = iio_priv(indio_dev);
946 u8 protect_mask = 0x03;
947 int num = chan->channel2;
949 mutex_lock(&st->lock);
951 * First read the motion detector config to find out if
954 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
958 /* if off and should be on */
959 if (state && !(ctrlval & sca3000_addresses[num][2])) {
960 ret = sca3000_write_ctrl_reg(st,
961 SCA3000_REG_CTRL_SEL_MD_CTRL,
963 sca3000_addresses[num][2]);
966 st->mo_det_use_count++;
967 } else if (!state && (ctrlval & sca3000_addresses[num][2])) {
968 ret = sca3000_write_ctrl_reg(st,
969 SCA3000_REG_CTRL_SEL_MD_CTRL,
971 ~(sca3000_addresses[num][2]));
974 st->mo_det_use_count--;
977 /* read current value of mode register */
978 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
981 /* if off and should be on */
982 if ((st->mo_det_use_count)
983 && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
984 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
985 (st->rx[0] & ~protect_mask)
986 | SCA3000_MEAS_MODE_MOT_DET);
987 /* if on and should be off */
988 else if (!(st->mo_det_use_count)
989 && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
990 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
991 (st->rx[0] & ~protect_mask));
993 mutex_unlock(&st->lock);
998 /* Free fall detector related event attribute */
999 static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en,
1000 in_accel_x&y&z_mag_falling_en,
1002 sca3000_query_free_fall_mode,
1003 sca3000_set_free_fall_mode,
1006 static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period,
1007 in_accel_x&y&z_mag_falling_period,
1010 static struct attribute *sca3000_event_attributes[] = {
1011 &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1012 &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1016 static struct attribute_group sca3000_event_attribute_group = {
1017 .attrs = sca3000_event_attributes,
1022 * sca3000_clean_setup() get the device into a predictable state
1024 * Devices use flash memory to store many of the register values
1025 * and hence can come up in somewhat unpredictable states.
1026 * Hence reset everything on driver load.
1028 static int sca3000_clean_setup(struct sca3000_state *st)
1032 mutex_lock(&st->lock);
1033 /* Ensure all interrupts have been acknowledged */
1034 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
1038 /* Turn off all motion detection channels */
1039 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1042 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
1043 ret & SCA3000_MD_CTRL_PROT_MASK);
1047 /* Disable ring buffer */
1048 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
1049 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
1050 (ret & SCA3000_OUT_CTRL_PROT_MASK)
1051 | SCA3000_OUT_CTRL_BUF_X_EN
1052 | SCA3000_OUT_CTRL_BUF_Y_EN
1053 | SCA3000_OUT_CTRL_BUF_Z_EN
1054 | SCA3000_OUT_CTRL_BUF_DIV_4);
1057 /* Enable interrupts, relevant to mode and set up as active low */
1058 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1061 ret = sca3000_write_reg(st,
1062 SCA3000_REG_ADDR_INT_MASK,
1063 (ret & SCA3000_INT_MASK_PROT_MASK)
1064 | SCA3000_INT_MASK_ACTIVE_LOW);
1068 * Select normal measurement mode, free fall off, ring off
1069 * Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1070 * as that occurs in one of the example on the datasheet
1072 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
1075 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1076 (st->rx[0] & SCA3000_MODE_PROT_MASK));
1080 mutex_unlock(&st->lock);
1084 static const struct iio_info sca3000_info = {
1085 .attrs = &sca3000_attribute_group,
1086 .read_raw = &sca3000_read_raw,
1087 .event_attrs = &sca3000_event_attribute_group,
1088 .read_event_value = &sca3000_read_thresh,
1089 .write_event_value = &sca3000_write_thresh,
1090 .read_event_config = &sca3000_read_event_config,
1091 .write_event_config = &sca3000_write_event_config,
1092 .driver_module = THIS_MODULE,
1095 static int sca3000_probe(struct spi_device *spi)
1098 struct sca3000_state *st;
1099 struct iio_dev *indio_dev;
1101 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1105 st = iio_priv(indio_dev);
1106 spi_set_drvdata(spi, indio_dev);
1108 mutex_init(&st->lock);
1109 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
1112 indio_dev->dev.parent = &spi->dev;
1113 indio_dev->name = spi_get_device_id(spi)->name;
1114 indio_dev->info = &sca3000_info;
1115 if (st->info->temp_output) {
1116 indio_dev->channels = sca3000_channels_with_temp;
1117 indio_dev->num_channels =
1118 ARRAY_SIZE(sca3000_channels_with_temp);
1120 indio_dev->channels = sca3000_channels;
1121 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
1123 indio_dev->modes = INDIO_DIRECT_MODE;
1125 sca3000_configure_ring(indio_dev);
1126 ret = iio_device_register(indio_dev);
1131 ret = request_threaded_irq(spi->irq,
1133 &sca3000_event_handler,
1134 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1138 goto error_unregister_dev;
1140 sca3000_register_ring_funcs(indio_dev);
1141 ret = sca3000_clean_setup(st);
1143 goto error_free_irq;
1148 free_irq(spi->irq, indio_dev);
1149 error_unregister_dev:
1150 iio_device_unregister(indio_dev);
1154 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1158 mutex_lock(&st->lock);
1159 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1162 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1164 ~(SCA3000_INT_MASK_RING_THREE_QUARTER |
1165 SCA3000_INT_MASK_RING_HALF |
1166 SCA3000_INT_MASK_ALL_INTS)));
1168 mutex_unlock(&st->lock);
1172 static int sca3000_remove(struct spi_device *spi)
1174 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1175 struct sca3000_state *st = iio_priv(indio_dev);
1177 /* Must ensure no interrupts can be generated after this! */
1178 sca3000_stop_all_interrupts(st);
1180 free_irq(spi->irq, indio_dev);
1181 iio_device_unregister(indio_dev);
1182 sca3000_unconfigure_ring(indio_dev);
1187 static const struct spi_device_id sca3000_id[] = {
1188 {"sca3000_d01", d01},
1189 {"sca3000_e02", e02},
1190 {"sca3000_e04", e04},
1191 {"sca3000_e05", e05},
1194 MODULE_DEVICE_TABLE(spi, sca3000_id);
1196 static struct spi_driver sca3000_driver = {
1199 .owner = THIS_MODULE,
1201 .probe = sca3000_probe,
1202 .remove = sca3000_remove,
1203 .id_table = sca3000_id,
1205 module_spi_driver(sca3000_driver);
1207 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1208 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1209 MODULE_LICENSE("GPL v2");