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 {
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_transfer xfer[2] = {
102 st->tx[0] = SCA3000_READ_REG(reg_address_high);
104 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
108 * sca3000_reg_lock_on() test if the ctrl register lock is on
112 static int sca3000_reg_lock_on(struct sca3000_state *st)
116 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
120 return !(st->rx[0] & SCA3000_LOCKED);
124 * __sca3000_unlock_reg_lock() unlock the control registers
126 * Note the device does not appear to support doing this in a single transfer.
127 * This should only ever be used as part of ctrl reg read.
128 * Lock must be held before calling this
130 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
132 struct spi_transfer xfer[3] = {
140 .tx_buf = st->tx + 2,
143 .tx_buf = st->tx + 4,
146 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
148 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
150 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
153 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
157 * sca3000_write_ctrl_reg() write to a lock protect ctrl register
158 * @sel: selects which registers we wish to write to
159 * @val: the value to be written
161 * Certain control registers are protected against overwriting by the lock
162 * register and use a shared write address. This function allows writing of
166 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
173 ret = sca3000_reg_lock_on(st);
177 ret = __sca3000_unlock_reg_lock(st);
182 /* Set the control select register */
183 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
187 /* Write the actual value into the register */
188 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
194 /* Crucial that lock is called before calling this */
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);
228 * sca3000_check_status() check the status register
230 * Only used for debugging purposes
232 static int sca3000_check_status(struct device *dev)
235 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
236 struct sca3000_state *st = iio_priv(indio_dev);
238 mutex_lock(&st->lock);
239 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
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");
248 mutex_unlock(&st->lock);
251 #endif /* SCA3000_DEBUG */
255 * sca3000_show_reg() - sysfs interface to read the chip revision number
257 static ssize_t sca3000_show_rev(struct device *dev,
258 struct device_attribute *attr,
262 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
263 struct sca3000_state *st = iio_priv(indio_dev);
265 mutex_lock(&st->lock);
266 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_REVID, 1);
269 len += sprintf(buf + len,
270 "major=%d, minor=%d\n",
271 st->rx[0] & SCA3000_REVID_MAJOR_MASK,
272 st->rx[0] & SCA3000_REVID_MINOR_MASK);
274 mutex_unlock(&st->lock);
276 return ret ? ret : len;
280 * sca3000_show_available_measurement_modes() display available modes
282 * This is all read from chip specific data in the driver. Not all
283 * of the sca3000 series support modes other than normal.
286 sca3000_show_available_measurement_modes(struct device *dev,
287 struct device_attribute *attr,
290 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
291 struct sca3000_state *st = iio_priv(indio_dev);
294 len += sprintf(buf + len, "0 - normal mode");
295 switch (st->info->option_mode_1) {
296 case SCA3000_OP_MODE_NARROW:
297 len += sprintf(buf + len, ", 1 - narrow mode");
299 case SCA3000_OP_MODE_BYPASS:
300 len += sprintf(buf + len, ", 1 - bypass mode");
303 switch (st->info->option_mode_2) {
304 case SCA3000_OP_MODE_WIDE:
305 len += sprintf(buf + len, ", 2 - wide mode");
308 /* always supported */
309 len += sprintf(buf + len, " 3 - motion detection\n");
315 * sca3000_show_measurmenet_mode() sysfs read of current mode
318 sca3000_show_measurement_mode(struct device *dev,
319 struct device_attribute *attr,
322 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
323 struct sca3000_state *st = iio_priv(indio_dev);
326 mutex_lock(&st->lock);
327 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
330 /* mask bottom 2 bits - only ones that are relevant */
333 case SCA3000_MEAS_MODE_NORMAL:
334 len += sprintf(buf + len, "0 - normal mode\n");
336 case SCA3000_MEAS_MODE_MOT_DET:
337 len += sprintf(buf + len, "3 - motion detection\n");
339 case SCA3000_MEAS_MODE_OP_1:
340 switch (st->info->option_mode_1) {
341 case SCA3000_OP_MODE_NARROW:
342 len += sprintf(buf + len, "1 - narrow mode\n");
344 case SCA3000_OP_MODE_BYPASS:
345 len += sprintf(buf + len, "1 - bypass mode\n");
349 case SCA3000_MEAS_MODE_OP_2:
350 switch (st->info->option_mode_2) {
351 case SCA3000_OP_MODE_WIDE:
352 len += sprintf(buf + len, "2 - wide mode\n");
359 mutex_unlock(&st->lock);
361 return ret ? ret : len;
365 * sca3000_store_measurement_mode() set the current mode
368 sca3000_store_measurement_mode(struct device *dev,
369 struct device_attribute *attr,
373 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
374 struct sca3000_state *st = iio_priv(indio_dev);
379 mutex_lock(&st->lock);
380 ret = kstrtou8(buf, 10, &val);
387 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
391 st->rx[0] |= (val & mask);
392 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, st->rx[0]);
395 mutex_unlock(&st->lock);
400 mutex_unlock(&st->lock);
406 /* Not even vaguely standard attributes so defined here rather than
407 * in the relevant IIO core headers
409 static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO,
410 sca3000_show_available_measurement_modes,
413 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
414 sca3000_show_measurement_mode,
415 sca3000_store_measurement_mode,
418 /* More standard attributes */
420 static IIO_DEVICE_ATTR(revision, S_IRUGO, sca3000_show_rev, NULL, 0);
422 #define SCA3000_INFO_MASK \
423 IIO_CHAN_INFO_RAW_SEPARATE_BIT | IIO_CHAN_INFO_SCALE_SHARED_BIT
424 #define SCA3000_EVENT_MASK \
425 (IIO_EV_BIT(IIO_EV_TYPE_MAG, IIO_EV_DIR_RISING))
427 #define SCA3000_CHAN(index, mod) \
432 .info_mask = SCA3000_INFO_MASK, \
434 .scan_index = index, \
441 .event_mask = SCA3000_EVENT_MASK, \
444 static const struct iio_chan_spec sca3000_channels[] = {
445 SCA3000_CHAN(0, IIO_MOD_X),
446 SCA3000_CHAN(1, IIO_MOD_Y),
447 SCA3000_CHAN(2, IIO_MOD_Z),
450 static u8 sca3000_addresses[3][3] = {
451 [0] = {SCA3000_REG_ADDR_X_MSB, SCA3000_REG_CTRL_SEL_MD_X_TH,
452 SCA3000_MD_CTRL_OR_X},
453 [1] = {SCA3000_REG_ADDR_Y_MSB, SCA3000_REG_CTRL_SEL_MD_Y_TH,
454 SCA3000_MD_CTRL_OR_Y},
455 [2] = {SCA3000_REG_ADDR_Z_MSB, SCA3000_REG_CTRL_SEL_MD_Z_TH,
456 SCA3000_MD_CTRL_OR_Z},
459 static int sca3000_read_raw(struct iio_dev *indio_dev,
460 struct iio_chan_spec const *chan,
465 struct sca3000_state *st = iio_priv(indio_dev);
470 case IIO_CHAN_INFO_RAW:
471 mutex_lock(&st->lock);
472 if (st->mo_det_use_count) {
473 mutex_unlock(&st->lock);
476 address = sca3000_addresses[chan->address][0];
477 ret = sca3000_read_data_short(st, address, 2);
479 mutex_unlock(&st->lock);
482 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
483 *val = ((*val) << (sizeof(*val)*8 - 13)) >>
484 (sizeof(*val)*8 - 13);
485 mutex_unlock(&st->lock);
487 case IIO_CHAN_INFO_SCALE:
489 if (chan->type == IIO_ACCEL)
490 *val2 = st->info->scale;
491 else /* temperature */
493 return IIO_VAL_INT_PLUS_MICRO;
500 * sca3000_read_av_freq() sysfs function to get available frequencies
502 * The later modes are only relevant to the ring buffer - and depend on current
503 * mode. Note that data sheet gives rather wide tolerances for these so integer
504 * division will give good enough answer and not all chips have them specified
507 static ssize_t sca3000_read_av_freq(struct device *dev,
508 struct device_attribute *attr,
511 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
512 struct sca3000_state *st = iio_priv(indio_dev);
513 int len = 0, ret, val;
515 mutex_lock(&st->lock);
516 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
518 mutex_unlock(&st->lock);
522 switch (val & 0x03) {
523 case SCA3000_MEAS_MODE_NORMAL:
524 len += sprintf(buf + len, "%d %d %d\n",
525 st->info->measurement_mode_freq,
526 st->info->measurement_mode_freq/2,
527 st->info->measurement_mode_freq/4);
529 case SCA3000_MEAS_MODE_OP_1:
530 len += sprintf(buf + len, "%d %d %d\n",
531 st->info->option_mode_1_freq,
532 st->info->option_mode_1_freq/2,
533 st->info->option_mode_1_freq/4);
535 case SCA3000_MEAS_MODE_OP_2:
536 len += sprintf(buf + len, "%d %d %d\n",
537 st->info->option_mode_2_freq,
538 st->info->option_mode_2_freq/2,
539 st->info->option_mode_2_freq/4);
547 * __sca3000_get_base_frequency() obtain mode specific base frequency
551 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
552 const struct sca3000_chip_info *info,
557 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
560 switch (0x03 & st->rx[0]) {
561 case SCA3000_MEAS_MODE_NORMAL:
562 *base_freq = info->measurement_mode_freq;
564 case SCA3000_MEAS_MODE_OP_1:
565 *base_freq = info->option_mode_1_freq;
567 case SCA3000_MEAS_MODE_OP_2:
568 *base_freq = info->option_mode_2_freq;
576 * sca3000_read_frequency() sysfs interface to get the current frequency
578 static ssize_t sca3000_read_frequency(struct device *dev,
579 struct device_attribute *attr,
582 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
583 struct sca3000_state *st = iio_priv(indio_dev);
584 int ret, len = 0, base_freq = 0, val;
586 mutex_lock(&st->lock);
587 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
590 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
591 mutex_unlock(&st->lock);
596 switch (val & 0x03) {
599 len = sprintf(buf, "%d\n", base_freq);
602 len = sprintf(buf, "%d\n", base_freq/2);
605 len = sprintf(buf, "%d\n", base_freq/4);
611 mutex_unlock(&st->lock);
617 * sca3000_set_frequency() sysfs interface to set the current frequency
619 static ssize_t sca3000_set_frequency(struct device *dev,
620 struct device_attribute *attr,
624 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
625 struct sca3000_state *st = iio_priv(indio_dev);
626 int ret, base_freq = 0;
630 ret = strict_strtol(buf, 10, &val);
634 mutex_lock(&st->lock);
635 /* What mode are we in? */
636 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
638 goto error_free_lock;
640 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
642 goto error_free_lock;
647 if (val == base_freq/2) {
648 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2;
649 } else if (val == base_freq/4) {
650 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4;
651 } else if (val != base_freq) {
653 goto error_free_lock;
655 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
658 mutex_unlock(&st->lock);
660 return ret ? ret : len;
663 /* Should only really be registered if ring buffer support is compiled in.
664 * Does no harm however and doing it right would add a fair bit of complexity
666 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
668 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
669 sca3000_read_frequency,
670 sca3000_set_frequency);
674 * sca3000_read_temp() sysfs interface to get the temperature when available
676 * The alignment of data in here is downright odd. See data sheet.
677 * Converting this into a meaningful value is left to inline functions in
678 * userspace part of header.
680 static ssize_t sca3000_read_temp(struct device *dev,
681 struct device_attribute *attr,
684 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
685 struct sca3000_state *st = iio_priv(indio_dev);
688 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_TEMP_MSB, 2);
691 val = ((st->rx[0] & 0x3F) << 3) | ((st->rx[1] & 0xE0) >> 5);
693 return sprintf(buf, "%d\n", val);
698 static IIO_DEV_ATTR_TEMP_RAW(sca3000_read_temp);
700 static IIO_CONST_ATTR_TEMP_SCALE("0.555556");
701 static IIO_CONST_ATTR_TEMP_OFFSET("-214.6");
704 * sca3000_read_thresh() - query of a threshold
706 static int sca3000_read_thresh(struct iio_dev *indio_dev,
711 struct sca3000_state *st = iio_priv(indio_dev);
712 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
713 mutex_lock(&st->lock);
714 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]);
715 mutex_unlock(&st->lock);
720 for_each_set_bit(i, (unsigned long *)&ret,
721 ARRAY_SIZE(st->info->mot_det_mult_y))
722 *val += st->info->mot_det_mult_y[i];
724 for_each_set_bit(i, (unsigned long *)&ret,
725 ARRAY_SIZE(st->info->mot_det_mult_xz))
726 *val += st->info->mot_det_mult_xz[i];
732 * sca3000_write_thresh() control of threshold
734 static int sca3000_write_thresh(struct iio_dev *indio_dev,
738 struct sca3000_state *st = iio_priv(indio_dev);
739 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
745 i = ARRAY_SIZE(st->info->mot_det_mult_y);
747 if (val >= st->info->mot_det_mult_y[--i]) {
748 nonlinear |= (1 << i);
749 val -= st->info->mot_det_mult_y[i];
752 i = ARRAY_SIZE(st->info->mot_det_mult_xz);
754 if (val >= st->info->mot_det_mult_xz[--i]) {
755 nonlinear |= (1 << i);
756 val -= st->info->mot_det_mult_xz[i];
760 mutex_lock(&st->lock);
761 ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear);
762 mutex_unlock(&st->lock);
767 static struct attribute *sca3000_attributes[] = {
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,
776 static struct attribute *sca3000_attributes_with_temp[] = {
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 /* Only present if temp sensor is */
783 &iio_dev_attr_in_temp_raw.dev_attr.attr,
784 &iio_const_attr_in_temp_offset.dev_attr.attr,
785 &iio_const_attr_in_temp_scale.dev_attr.attr,
789 static const struct attribute_group sca3000_attribute_group = {
790 .attrs = sca3000_attributes,
793 static const struct attribute_group sca3000_attribute_group_with_temp = {
794 .attrs = sca3000_attributes_with_temp,
797 /* RING RELATED interrupt handler */
798 /* depending on event, push to the ring buffer event chrdev or the event one */
801 * sca3000_event_handler() - handling ring and non ring events
803 * This function is complicated by the fact that the devices can signify ring
804 * and non ring events via the same interrupt line and they can only
805 * be distinguished via a read of the relevant status register.
807 static irqreturn_t sca3000_event_handler(int irq, void *private)
809 struct iio_dev *indio_dev = private;
810 struct sca3000_state *st = iio_priv(indio_dev);
812 s64 last_timestamp = iio_get_time_ns();
814 /* Could lead if badly timed to an extra read of status reg,
815 * but ensures no interrupt is missed.
817 mutex_lock(&st->lock);
818 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
820 mutex_unlock(&st->lock);
824 sca3000_ring_int_process(val, indio_dev->buffer);
826 if (val & SCA3000_INT_STATUS_FREE_FALL)
827 iio_push_event(indio_dev,
828 IIO_MOD_EVENT_CODE(IIO_ACCEL,
830 IIO_MOD_X_AND_Y_AND_Z,
835 if (val & SCA3000_INT_STATUS_Y_TRIGGER)
836 iio_push_event(indio_dev,
837 IIO_MOD_EVENT_CODE(IIO_ACCEL,
844 if (val & SCA3000_INT_STATUS_X_TRIGGER)
845 iio_push_event(indio_dev,
846 IIO_MOD_EVENT_CODE(IIO_ACCEL,
853 if (val & SCA3000_INT_STATUS_Z_TRIGGER)
854 iio_push_event(indio_dev,
855 IIO_MOD_EVENT_CODE(IIO_ACCEL,
867 * sca3000_read_event_config() what events are enabled
869 static int sca3000_read_event_config(struct iio_dev *indio_dev,
872 struct sca3000_state *st = iio_priv(indio_dev);
874 u8 protect_mask = 0x03;
875 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
877 /* read current value of mode register */
878 mutex_lock(&st->lock);
879 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
883 if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
886 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
889 /* only supporting logical or's for now */
890 ret = !!(ret & sca3000_addresses[num][2]);
893 mutex_unlock(&st->lock);
898 * sca3000_query_free_fall_mode() is free fall mode enabled
900 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
901 struct device_attribute *attr,
905 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
906 struct sca3000_state *st = iio_priv(indio_dev);
909 mutex_lock(&st->lock);
910 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
912 mutex_unlock(&st->lock);
915 len = sprintf(buf, "%d\n",
916 !!(val & SCA3000_FREE_FALL_DETECT));
921 * sca3000_set_free_fall_mode() simple on off control for free fall int
923 * In these chips the free fall detector should send an interrupt if
924 * the device falls more than 25cm. This has not been tested due
928 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
929 struct device_attribute *attr,
933 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
934 struct sca3000_state *st = iio_priv(indio_dev);
937 u8 protect_mask = SCA3000_FREE_FALL_DETECT;
939 mutex_lock(&st->lock);
940 ret = strict_strtol(buf, 10, &val);
944 /* read current value of mode register */
945 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
949 /*if off and should be on*/
950 if (val && !(st->rx[0] & protect_mask))
951 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
952 (st->rx[0] | SCA3000_FREE_FALL_DETECT));
953 /* if on and should be off */
954 else if (!val && (st->rx[0] & protect_mask))
955 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
956 (st->rx[0] & ~protect_mask));
958 mutex_unlock(&st->lock);
960 return ret ? ret : len;
964 * sca3000_set_mo_det() simple on off control for motion detector
966 * This is a per axis control, but enabling any will result in the
967 * motion detector unit being enabled.
968 * N.B. enabling motion detector stops normal data acquisition.
969 * There is a complexity in knowing which mode to return to when
970 * this mode is disabled. Currently normal mode is assumed.
972 static int sca3000_write_event_config(struct iio_dev *indio_dev,
976 struct sca3000_state *st = iio_priv(indio_dev);
978 u8 protect_mask = 0x03;
979 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
981 mutex_lock(&st->lock);
982 /* First read the motion detector config to find out if
984 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
988 /* Off and should be on */
989 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++;
997 } else if (!state && (ctrlval & sca3000_addresses[num][2])) {
998 ret = sca3000_write_ctrl_reg(st,
999 SCA3000_REG_CTRL_SEL_MD_CTRL,
1001 ~(sca3000_addresses[num][2]));
1004 st->mo_det_use_count--;
1007 /* read current value of mode register */
1008 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
1011 /*if off and should be on*/
1012 if ((st->mo_det_use_count)
1013 && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1014 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1015 (st->rx[0] & ~protect_mask)
1016 | SCA3000_MEAS_MODE_MOT_DET);
1017 /* if on and should be off */
1018 else if (!(st->mo_det_use_count)
1019 && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1020 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1021 (st->rx[0] & ~protect_mask));
1023 mutex_unlock(&st->lock);
1028 /* Free fall detector related event attribute */
1029 static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en,
1030 in_accel_x&y&z_mag_falling_en,
1032 sca3000_query_free_fall_mode,
1033 sca3000_set_free_fall_mode,
1036 static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period,
1037 in_accel_x&y&z_mag_falling_period,
1040 static struct attribute *sca3000_event_attributes[] = {
1041 &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1042 &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1046 static struct attribute_group sca3000_event_attribute_group = {
1047 .attrs = sca3000_event_attributes,
1052 * sca3000_clean_setup() get the device into a predictable state
1054 * Devices use flash memory to store many of the register values
1055 * and hence can come up in somewhat unpredictable states.
1056 * Hence reset everything on driver load.
1058 static int sca3000_clean_setup(struct sca3000_state *st)
1062 mutex_lock(&st->lock);
1063 /* Ensure all interrupts have been acknowledged */
1064 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
1068 /* Turn off all motion detection channels */
1069 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1072 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
1073 ret & SCA3000_MD_CTRL_PROT_MASK);
1077 /* Disable ring buffer */
1078 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
1079 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
1080 (ret & SCA3000_OUT_CTRL_PROT_MASK)
1081 | SCA3000_OUT_CTRL_BUF_X_EN
1082 | SCA3000_OUT_CTRL_BUF_Y_EN
1083 | SCA3000_OUT_CTRL_BUF_Z_EN
1084 | SCA3000_OUT_CTRL_BUF_DIV_4);
1087 /* Enable interrupts, relevant to mode and set up as active low */
1088 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1091 ret = sca3000_write_reg(st,
1092 SCA3000_REG_ADDR_INT_MASK,
1093 (ret & SCA3000_INT_MASK_PROT_MASK)
1094 | SCA3000_INT_MASK_ACTIVE_LOW);
1097 /* Select normal measurement mode, free fall off, ring off */
1098 /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1099 * as that occurs in one of the example on the datasheet */
1100 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
1103 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1104 (st->rx[0] & SCA3000_MODE_PROT_MASK));
1108 mutex_unlock(&st->lock);
1112 static const struct iio_info sca3000_info = {
1113 .attrs = &sca3000_attribute_group,
1114 .read_raw = &sca3000_read_raw,
1115 .event_attrs = &sca3000_event_attribute_group,
1116 .read_event_value = &sca3000_read_thresh,
1117 .write_event_value = &sca3000_write_thresh,
1118 .read_event_config = &sca3000_read_event_config,
1119 .write_event_config = &sca3000_write_event_config,
1120 .driver_module = THIS_MODULE,
1123 static const struct iio_info sca3000_info_with_temp = {
1124 .attrs = &sca3000_attribute_group_with_temp,
1125 .read_raw = &sca3000_read_raw,
1126 .read_event_value = &sca3000_read_thresh,
1127 .write_event_value = &sca3000_write_thresh,
1128 .read_event_config = &sca3000_read_event_config,
1129 .write_event_config = &sca3000_write_event_config,
1130 .driver_module = THIS_MODULE,
1133 static int sca3000_probe(struct spi_device *spi)
1136 struct sca3000_state *st;
1137 struct iio_dev *indio_dev;
1139 indio_dev = iio_device_alloc(sizeof(*st));
1140 if (indio_dev == NULL) {
1145 st = iio_priv(indio_dev);
1146 spi_set_drvdata(spi, indio_dev);
1148 mutex_init(&st->lock);
1149 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
1152 indio_dev->dev.parent = &spi->dev;
1153 indio_dev->name = spi_get_device_id(spi)->name;
1154 if (st->info->temp_output)
1155 indio_dev->info = &sca3000_info_with_temp;
1157 indio_dev->info = &sca3000_info;
1158 indio_dev->channels = sca3000_channels;
1159 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
1161 indio_dev->modes = INDIO_DIRECT_MODE;
1163 sca3000_configure_ring(indio_dev);
1164 ret = iio_device_register(indio_dev);
1166 goto error_free_dev;
1168 ret = iio_buffer_register(indio_dev,
1170 ARRAY_SIZE(sca3000_channels));
1172 goto error_unregister_dev;
1173 if (indio_dev->buffer) {
1174 iio_scan_mask_set(indio_dev, indio_dev->buffer, 0);
1175 iio_scan_mask_set(indio_dev, indio_dev->buffer, 1);
1176 iio_scan_mask_set(indio_dev, indio_dev->buffer, 2);
1180 ret = request_threaded_irq(spi->irq,
1182 &sca3000_event_handler,
1183 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1187 goto error_unregister_ring;
1189 sca3000_register_ring_funcs(indio_dev);
1190 ret = sca3000_clean_setup(st);
1192 goto error_free_irq;
1197 free_irq(spi->irq, indio_dev);
1198 error_unregister_ring:
1199 iio_buffer_unregister(indio_dev);
1200 error_unregister_dev:
1201 iio_device_unregister(indio_dev);
1203 iio_device_free(indio_dev);
1209 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1213 mutex_lock(&st->lock);
1214 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1217 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1219 ~(SCA3000_INT_MASK_RING_THREE_QUARTER |
1220 SCA3000_INT_MASK_RING_HALF |
1221 SCA3000_INT_MASK_ALL_INTS)));
1223 mutex_unlock(&st->lock);
1227 static int sca3000_remove(struct spi_device *spi)
1229 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1230 struct sca3000_state *st = iio_priv(indio_dev);
1232 /* Must ensure no interrupts can be generated after this!*/
1233 sca3000_stop_all_interrupts(st);
1235 free_irq(spi->irq, indio_dev);
1236 iio_device_unregister(indio_dev);
1237 iio_buffer_unregister(indio_dev);
1238 sca3000_unconfigure_ring(indio_dev);
1239 iio_device_free(indio_dev);
1244 static const struct spi_device_id sca3000_id[] = {
1245 {"sca3000_d01", d01},
1246 {"sca3000_e02", e02},
1247 {"sca3000_e04", e04},
1248 {"sca3000_e05", e05},
1251 MODULE_DEVICE_TABLE(spi, sca3000_id);
1253 static struct spi_driver sca3000_driver = {
1256 .owner = THIS_MODULE,
1258 .probe = sca3000_probe,
1259 .remove = sca3000_remove,
1260 .id_table = sca3000_id,
1262 module_spi_driver(sca3000_driver);
1264 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1265 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1266 MODULE_LICENSE("GPL v2");