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_EVENT_MASK \
423 (IIO_EV_BIT(IIO_EV_TYPE_MAG, IIO_EV_DIR_RISING))
425 #define SCA3000_CHAN(index, mod) \
430 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
431 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
433 .scan_index = index, \
440 .event_mask = SCA3000_EVENT_MASK, \
443 static const struct iio_chan_spec sca3000_channels
[] = {
444 SCA3000_CHAN(0, IIO_MOD_X
),
445 SCA3000_CHAN(1, IIO_MOD_Y
),
446 SCA3000_CHAN(2, IIO_MOD_Z
),
449 static u8 sca3000_addresses
[3][3] = {
450 [0] = {SCA3000_REG_ADDR_X_MSB
, SCA3000_REG_CTRL_SEL_MD_X_TH
,
451 SCA3000_MD_CTRL_OR_X
},
452 [1] = {SCA3000_REG_ADDR_Y_MSB
, SCA3000_REG_CTRL_SEL_MD_Y_TH
,
453 SCA3000_MD_CTRL_OR_Y
},
454 [2] = {SCA3000_REG_ADDR_Z_MSB
, SCA3000_REG_CTRL_SEL_MD_Z_TH
,
455 SCA3000_MD_CTRL_OR_Z
},
458 static int sca3000_read_raw(struct iio_dev
*indio_dev
,
459 struct iio_chan_spec
const *chan
,
464 struct sca3000_state
*st
= iio_priv(indio_dev
);
469 case IIO_CHAN_INFO_RAW
:
470 mutex_lock(&st
->lock
);
471 if (st
->mo_det_use_count
) {
472 mutex_unlock(&st
->lock
);
475 address
= sca3000_addresses
[chan
->address
][0];
476 ret
= sca3000_read_data_short(st
, address
, 2);
478 mutex_unlock(&st
->lock
);
481 *val
= (be16_to_cpup((__be16
*)st
->rx
) >> 3) & 0x1FFF;
482 *val
= ((*val
) << (sizeof(*val
)*8 - 13)) >>
483 (sizeof(*val
)*8 - 13);
484 mutex_unlock(&st
->lock
);
486 case IIO_CHAN_INFO_SCALE
:
488 if (chan
->type
== IIO_ACCEL
)
489 *val2
= st
->info
->scale
;
490 else /* temperature */
492 return IIO_VAL_INT_PLUS_MICRO
;
499 * sca3000_read_av_freq() sysfs function to get available frequencies
501 * The later modes are only relevant to the ring buffer - and depend on current
502 * mode. Note that data sheet gives rather wide tolerances for these so integer
503 * division will give good enough answer and not all chips have them specified
506 static ssize_t
sca3000_read_av_freq(struct device
*dev
,
507 struct device_attribute
*attr
,
510 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
511 struct sca3000_state
*st
= iio_priv(indio_dev
);
512 int len
= 0, ret
, val
;
514 mutex_lock(&st
->lock
);
515 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_MODE
, 1);
517 mutex_unlock(&st
->lock
);
521 switch (val
& 0x03) {
522 case SCA3000_MEAS_MODE_NORMAL
:
523 len
+= sprintf(buf
+ len
, "%d %d %d\n",
524 st
->info
->measurement_mode_freq
,
525 st
->info
->measurement_mode_freq
/2,
526 st
->info
->measurement_mode_freq
/4);
528 case SCA3000_MEAS_MODE_OP_1
:
529 len
+= sprintf(buf
+ len
, "%d %d %d\n",
530 st
->info
->option_mode_1_freq
,
531 st
->info
->option_mode_1_freq
/2,
532 st
->info
->option_mode_1_freq
/4);
534 case SCA3000_MEAS_MODE_OP_2
:
535 len
+= sprintf(buf
+ len
, "%d %d %d\n",
536 st
->info
->option_mode_2_freq
,
537 st
->info
->option_mode_2_freq
/2,
538 st
->info
->option_mode_2_freq
/4);
546 * __sca3000_get_base_frequency() obtain mode specific base frequency
550 static inline int __sca3000_get_base_freq(struct sca3000_state
*st
,
551 const struct sca3000_chip_info
*info
,
556 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_MODE
, 1);
559 switch (0x03 & st
->rx
[0]) {
560 case SCA3000_MEAS_MODE_NORMAL
:
561 *base_freq
= info
->measurement_mode_freq
;
563 case SCA3000_MEAS_MODE_OP_1
:
564 *base_freq
= info
->option_mode_1_freq
;
566 case SCA3000_MEAS_MODE_OP_2
:
567 *base_freq
= info
->option_mode_2_freq
;
575 * sca3000_read_frequency() sysfs interface to get the current frequency
577 static ssize_t
sca3000_read_frequency(struct device
*dev
,
578 struct device_attribute
*attr
,
581 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
582 struct sca3000_state
*st
= iio_priv(indio_dev
);
583 int ret
, len
= 0, base_freq
= 0, val
;
585 mutex_lock(&st
->lock
);
586 ret
= __sca3000_get_base_freq(st
, st
->info
, &base_freq
);
589 ret
= sca3000_read_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_OUT_CTRL
);
590 mutex_unlock(&st
->lock
);
595 switch (val
& 0x03) {
598 len
= sprintf(buf
, "%d\n", base_freq
);
601 len
= sprintf(buf
, "%d\n", base_freq
/2);
604 len
= sprintf(buf
, "%d\n", base_freq
/4);
610 mutex_unlock(&st
->lock
);
616 * sca3000_set_frequency() sysfs interface to set the current frequency
618 static ssize_t
sca3000_set_frequency(struct device
*dev
,
619 struct device_attribute
*attr
,
623 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
624 struct sca3000_state
*st
= iio_priv(indio_dev
);
625 int ret
, base_freq
= 0;
629 ret
= strict_strtol(buf
, 10, &val
);
633 mutex_lock(&st
->lock
);
634 /* What mode are we in? */
635 ret
= __sca3000_get_base_freq(st
, st
->info
, &base_freq
);
637 goto error_free_lock
;
639 ret
= sca3000_read_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_OUT_CTRL
);
641 goto error_free_lock
;
646 if (val
== base_freq
/2) {
647 ctrlval
|= SCA3000_OUT_CTRL_BUF_DIV_2
;
648 } else if (val
== base_freq
/4) {
649 ctrlval
|= SCA3000_OUT_CTRL_BUF_DIV_4
;
650 } else if (val
!= base_freq
) {
652 goto error_free_lock
;
654 ret
= sca3000_write_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_OUT_CTRL
,
657 mutex_unlock(&st
->lock
);
659 return ret
? ret
: len
;
662 /* Should only really be registered if ring buffer support is compiled in.
663 * Does no harm however and doing it right would add a fair bit of complexity
665 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq
);
667 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
668 sca3000_read_frequency
,
669 sca3000_set_frequency
);
673 * sca3000_read_temp() sysfs interface to get the temperature when available
675 * The alignment of data in here is downright odd. See data sheet.
676 * Converting this into a meaningful value is left to inline functions in
677 * userspace part of header.
679 static ssize_t
sca3000_read_temp(struct device
*dev
,
680 struct device_attribute
*attr
,
683 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
684 struct sca3000_state
*st
= iio_priv(indio_dev
);
687 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_TEMP_MSB
, 2);
690 val
= ((st
->rx
[0] & 0x3F) << 3) | ((st
->rx
[1] & 0xE0) >> 5);
692 return sprintf(buf
, "%d\n", val
);
697 static IIO_DEV_ATTR_TEMP_RAW(sca3000_read_temp
);
699 static IIO_CONST_ATTR_TEMP_SCALE("0.555556");
700 static IIO_CONST_ATTR_TEMP_OFFSET("-214.6");
703 * sca3000_read_thresh() - query of a threshold
705 static int sca3000_read_thresh(struct iio_dev
*indio_dev
,
710 struct sca3000_state
*st
= iio_priv(indio_dev
);
711 int num
= IIO_EVENT_CODE_EXTRACT_MODIFIER(e
);
712 mutex_lock(&st
->lock
);
713 ret
= sca3000_read_ctrl_reg(st
, sca3000_addresses
[num
][1]);
714 mutex_unlock(&st
->lock
);
719 for_each_set_bit(i
, (unsigned long *)&ret
,
720 ARRAY_SIZE(st
->info
->mot_det_mult_y
))
721 *val
+= st
->info
->mot_det_mult_y
[i
];
723 for_each_set_bit(i
, (unsigned long *)&ret
,
724 ARRAY_SIZE(st
->info
->mot_det_mult_xz
))
725 *val
+= st
->info
->mot_det_mult_xz
[i
];
731 * sca3000_write_thresh() control of threshold
733 static int sca3000_write_thresh(struct iio_dev
*indio_dev
,
737 struct sca3000_state
*st
= iio_priv(indio_dev
);
738 int num
= IIO_EVENT_CODE_EXTRACT_MODIFIER(e
);
744 i
= ARRAY_SIZE(st
->info
->mot_det_mult_y
);
746 if (val
>= st
->info
->mot_det_mult_y
[--i
]) {
747 nonlinear
|= (1 << i
);
748 val
-= st
->info
->mot_det_mult_y
[i
];
751 i
= ARRAY_SIZE(st
->info
->mot_det_mult_xz
);
753 if (val
>= st
->info
->mot_det_mult_xz
[--i
]) {
754 nonlinear
|= (1 << i
);
755 val
-= st
->info
->mot_det_mult_xz
[i
];
759 mutex_lock(&st
->lock
);
760 ret
= sca3000_write_ctrl_reg(st
, sca3000_addresses
[num
][1], nonlinear
);
761 mutex_unlock(&st
->lock
);
766 static struct attribute
*sca3000_attributes
[] = {
767 &iio_dev_attr_revision
.dev_attr
.attr
,
768 &iio_dev_attr_measurement_mode_available
.dev_attr
.attr
,
769 &iio_dev_attr_measurement_mode
.dev_attr
.attr
,
770 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
771 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
775 static struct attribute
*sca3000_attributes_with_temp
[] = {
776 &iio_dev_attr_revision
.dev_attr
.attr
,
777 &iio_dev_attr_measurement_mode_available
.dev_attr
.attr
,
778 &iio_dev_attr_measurement_mode
.dev_attr
.attr
,
779 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
780 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
781 /* Only present if temp sensor is */
782 &iio_dev_attr_in_temp_raw
.dev_attr
.attr
,
783 &iio_const_attr_in_temp_offset
.dev_attr
.attr
,
784 &iio_const_attr_in_temp_scale
.dev_attr
.attr
,
788 static const struct attribute_group sca3000_attribute_group
= {
789 .attrs
= sca3000_attributes
,
792 static const struct attribute_group sca3000_attribute_group_with_temp
= {
793 .attrs
= sca3000_attributes_with_temp
,
796 /* RING RELATED interrupt handler */
797 /* depending on event, push to the ring buffer event chrdev or the event one */
800 * sca3000_event_handler() - handling ring and non ring events
802 * This function is complicated by the fact that the devices can signify ring
803 * and non ring events via the same interrupt line and they can only
804 * be distinguished via a read of the relevant status register.
806 static irqreturn_t
sca3000_event_handler(int irq
, void *private)
808 struct iio_dev
*indio_dev
= private;
809 struct sca3000_state
*st
= iio_priv(indio_dev
);
811 s64 last_timestamp
= iio_get_time_ns();
813 /* Could lead if badly timed to an extra read of status reg,
814 * but ensures no interrupt is missed.
816 mutex_lock(&st
->lock
);
817 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_INT_STATUS
, 1);
819 mutex_unlock(&st
->lock
);
823 sca3000_ring_int_process(val
, indio_dev
->buffer
);
825 if (val
& SCA3000_INT_STATUS_FREE_FALL
)
826 iio_push_event(indio_dev
,
827 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
829 IIO_MOD_X_AND_Y_AND_Z
,
834 if (val
& SCA3000_INT_STATUS_Y_TRIGGER
)
835 iio_push_event(indio_dev
,
836 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
843 if (val
& SCA3000_INT_STATUS_X_TRIGGER
)
844 iio_push_event(indio_dev
,
845 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
852 if (val
& SCA3000_INT_STATUS_Z_TRIGGER
)
853 iio_push_event(indio_dev
,
854 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
866 * sca3000_read_event_config() what events are enabled
868 static int sca3000_read_event_config(struct iio_dev
*indio_dev
,
871 struct sca3000_state
*st
= iio_priv(indio_dev
);
873 u8 protect_mask
= 0x03;
874 int num
= IIO_EVENT_CODE_EXTRACT_MODIFIER(e
);
876 /* read current value of mode register */
877 mutex_lock(&st
->lock
);
878 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_MODE
, 1);
882 if ((st
->rx
[0] & protect_mask
) != SCA3000_MEAS_MODE_MOT_DET
)
885 ret
= sca3000_read_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_MD_CTRL
);
888 /* only supporting logical or's for now */
889 ret
= !!(ret
& sca3000_addresses
[num
][2]);
892 mutex_unlock(&st
->lock
);
897 * sca3000_query_free_fall_mode() is free fall mode enabled
899 static ssize_t
sca3000_query_free_fall_mode(struct device
*dev
,
900 struct device_attribute
*attr
,
904 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
905 struct sca3000_state
*st
= iio_priv(indio_dev
);
908 mutex_lock(&st
->lock
);
909 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_MODE
, 1);
911 mutex_unlock(&st
->lock
);
914 len
= sprintf(buf
, "%d\n",
915 !!(val
& SCA3000_FREE_FALL_DETECT
));
920 * sca3000_set_free_fall_mode() simple on off control for free fall int
922 * In these chips the free fall detector should send an interrupt if
923 * the device falls more than 25cm. This has not been tested due
927 static ssize_t
sca3000_set_free_fall_mode(struct device
*dev
,
928 struct device_attribute
*attr
,
932 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
933 struct sca3000_state
*st
= iio_priv(indio_dev
);
936 u8 protect_mask
= SCA3000_FREE_FALL_DETECT
;
938 mutex_lock(&st
->lock
);
939 ret
= strict_strtol(buf
, 10, &val
);
943 /* read current value of mode register */
944 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_MODE
, 1);
948 /*if off and should be on*/
949 if (val
&& !(st
->rx
[0] & protect_mask
))
950 ret
= sca3000_write_reg(st
, SCA3000_REG_ADDR_MODE
,
951 (st
->rx
[0] | SCA3000_FREE_FALL_DETECT
));
952 /* if on and should be off */
953 else if (!val
&& (st
->rx
[0] & protect_mask
))
954 ret
= sca3000_write_reg(st
, SCA3000_REG_ADDR_MODE
,
955 (st
->rx
[0] & ~protect_mask
));
957 mutex_unlock(&st
->lock
);
959 return ret
? ret
: len
;
963 * sca3000_set_mo_det() simple on off control for motion detector
965 * This is a per axis control, but enabling any will result in the
966 * motion detector unit being enabled.
967 * N.B. enabling motion detector stops normal data acquisition.
968 * There is a complexity in knowing which mode to return to when
969 * this mode is disabled. Currently normal mode is assumed.
971 static int sca3000_write_event_config(struct iio_dev
*indio_dev
,
975 struct sca3000_state
*st
= iio_priv(indio_dev
);
977 u8 protect_mask
= 0x03;
978 int num
= IIO_EVENT_CODE_EXTRACT_MODIFIER(e
);
980 mutex_lock(&st
->lock
);
981 /* First read the motion detector config to find out if
983 ret
= sca3000_read_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_MD_CTRL
);
987 /* Off and should be on */
988 if (state
&& !(ctrlval
& sca3000_addresses
[num
][2])) {
989 ret
= sca3000_write_ctrl_reg(st
,
990 SCA3000_REG_CTRL_SEL_MD_CTRL
,
992 sca3000_addresses
[num
][2]);
995 st
->mo_det_use_count
++;
996 } else if (!state
&& (ctrlval
& sca3000_addresses
[num
][2])) {
997 ret
= sca3000_write_ctrl_reg(st
,
998 SCA3000_REG_CTRL_SEL_MD_CTRL
,
1000 ~(sca3000_addresses
[num
][2]));
1003 st
->mo_det_use_count
--;
1006 /* read current value of mode register */
1007 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_MODE
, 1);
1010 /*if off and should be on*/
1011 if ((st
->mo_det_use_count
)
1012 && ((st
->rx
[0] & protect_mask
) != SCA3000_MEAS_MODE_MOT_DET
))
1013 ret
= sca3000_write_reg(st
, SCA3000_REG_ADDR_MODE
,
1014 (st
->rx
[0] & ~protect_mask
)
1015 | SCA3000_MEAS_MODE_MOT_DET
);
1016 /* if on and should be off */
1017 else if (!(st
->mo_det_use_count
)
1018 && ((st
->rx
[0] & protect_mask
) == SCA3000_MEAS_MODE_MOT_DET
))
1019 ret
= sca3000_write_reg(st
, SCA3000_REG_ADDR_MODE
,
1020 (st
->rx
[0] & ~protect_mask
));
1022 mutex_unlock(&st
->lock
);
1027 /* Free fall detector related event attribute */
1028 static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en
,
1029 in_accel_x
&y
&z_mag_falling_en
,
1031 sca3000_query_free_fall_mode
,
1032 sca3000_set_free_fall_mode
,
1035 static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period
,
1036 in_accel_x
&y
&z_mag_falling_period
,
1039 static struct attribute
*sca3000_event_attributes
[] = {
1040 &iio_dev_attr_accel_xayaz_mag_falling_en
.dev_attr
.attr
,
1041 &iio_const_attr_accel_xayaz_mag_falling_period
.dev_attr
.attr
,
1045 static struct attribute_group sca3000_event_attribute_group
= {
1046 .attrs
= sca3000_event_attributes
,
1051 * sca3000_clean_setup() get the device into a predictable state
1053 * Devices use flash memory to store many of the register values
1054 * and hence can come up in somewhat unpredictable states.
1055 * Hence reset everything on driver load.
1057 static int sca3000_clean_setup(struct sca3000_state
*st
)
1061 mutex_lock(&st
->lock
);
1062 /* Ensure all interrupts have been acknowledged */
1063 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_INT_STATUS
, 1);
1067 /* Turn off all motion detection channels */
1068 ret
= sca3000_read_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_MD_CTRL
);
1071 ret
= sca3000_write_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_MD_CTRL
,
1072 ret
& SCA3000_MD_CTRL_PROT_MASK
);
1076 /* Disable ring buffer */
1077 ret
= sca3000_read_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_OUT_CTRL
);
1078 ret
= sca3000_write_ctrl_reg(st
, SCA3000_REG_CTRL_SEL_OUT_CTRL
,
1079 (ret
& SCA3000_OUT_CTRL_PROT_MASK
)
1080 | SCA3000_OUT_CTRL_BUF_X_EN
1081 | SCA3000_OUT_CTRL_BUF_Y_EN
1082 | SCA3000_OUT_CTRL_BUF_Z_EN
1083 | SCA3000_OUT_CTRL_BUF_DIV_4
);
1086 /* Enable interrupts, relevant to mode and set up as active low */
1087 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_INT_MASK
, 1);
1090 ret
= sca3000_write_reg(st
,
1091 SCA3000_REG_ADDR_INT_MASK
,
1092 (ret
& SCA3000_INT_MASK_PROT_MASK
)
1093 | SCA3000_INT_MASK_ACTIVE_LOW
);
1096 /* Select normal measurement mode, free fall off, ring off */
1097 /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1098 * as that occurs in one of the example on the datasheet */
1099 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_MODE
, 1);
1102 ret
= sca3000_write_reg(st
, SCA3000_REG_ADDR_MODE
,
1103 (st
->rx
[0] & SCA3000_MODE_PROT_MASK
));
1107 mutex_unlock(&st
->lock
);
1111 static const struct iio_info sca3000_info
= {
1112 .attrs
= &sca3000_attribute_group
,
1113 .read_raw
= &sca3000_read_raw
,
1114 .event_attrs
= &sca3000_event_attribute_group
,
1115 .read_event_value
= &sca3000_read_thresh
,
1116 .write_event_value
= &sca3000_write_thresh
,
1117 .read_event_config
= &sca3000_read_event_config
,
1118 .write_event_config
= &sca3000_write_event_config
,
1119 .driver_module
= THIS_MODULE
,
1122 static const struct iio_info sca3000_info_with_temp
= {
1123 .attrs
= &sca3000_attribute_group_with_temp
,
1124 .read_raw
= &sca3000_read_raw
,
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
,
1132 static int sca3000_probe(struct spi_device
*spi
)
1135 struct sca3000_state
*st
;
1136 struct iio_dev
*indio_dev
;
1138 indio_dev
= iio_device_alloc(sizeof(*st
));
1139 if (indio_dev
== NULL
) {
1144 st
= iio_priv(indio_dev
);
1145 spi_set_drvdata(spi
, indio_dev
);
1147 mutex_init(&st
->lock
);
1148 st
->info
= &sca3000_spi_chip_info_tbl
[spi_get_device_id(spi
)
1151 indio_dev
->dev
.parent
= &spi
->dev
;
1152 indio_dev
->name
= spi_get_device_id(spi
)->name
;
1153 if (st
->info
->temp_output
)
1154 indio_dev
->info
= &sca3000_info_with_temp
;
1156 indio_dev
->info
= &sca3000_info
;
1157 indio_dev
->channels
= sca3000_channels
;
1158 indio_dev
->num_channels
= ARRAY_SIZE(sca3000_channels
);
1160 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1162 sca3000_configure_ring(indio_dev
);
1163 ret
= iio_device_register(indio_dev
);
1165 goto error_free_dev
;
1167 ret
= iio_buffer_register(indio_dev
,
1169 ARRAY_SIZE(sca3000_channels
));
1171 goto error_unregister_dev
;
1172 if (indio_dev
->buffer
) {
1173 iio_scan_mask_set(indio_dev
, indio_dev
->buffer
, 0);
1174 iio_scan_mask_set(indio_dev
, indio_dev
->buffer
, 1);
1175 iio_scan_mask_set(indio_dev
, indio_dev
->buffer
, 2);
1179 ret
= request_threaded_irq(spi
->irq
,
1181 &sca3000_event_handler
,
1182 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1186 goto error_unregister_ring
;
1188 sca3000_register_ring_funcs(indio_dev
);
1189 ret
= sca3000_clean_setup(st
);
1191 goto error_free_irq
;
1196 free_irq(spi
->irq
, indio_dev
);
1197 error_unregister_ring
:
1198 iio_buffer_unregister(indio_dev
);
1199 error_unregister_dev
:
1200 iio_device_unregister(indio_dev
);
1202 iio_device_free(indio_dev
);
1208 static int sca3000_stop_all_interrupts(struct sca3000_state
*st
)
1212 mutex_lock(&st
->lock
);
1213 ret
= sca3000_read_data_short(st
, SCA3000_REG_ADDR_INT_MASK
, 1);
1216 ret
= sca3000_write_reg(st
, SCA3000_REG_ADDR_INT_MASK
,
1218 ~(SCA3000_INT_MASK_RING_THREE_QUARTER
|
1219 SCA3000_INT_MASK_RING_HALF
|
1220 SCA3000_INT_MASK_ALL_INTS
)));
1222 mutex_unlock(&st
->lock
);
1226 static int sca3000_remove(struct spi_device
*spi
)
1228 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
1229 struct sca3000_state
*st
= iio_priv(indio_dev
);
1231 /* Must ensure no interrupts can be generated after this!*/
1232 sca3000_stop_all_interrupts(st
);
1234 free_irq(spi
->irq
, indio_dev
);
1235 iio_device_unregister(indio_dev
);
1236 iio_buffer_unregister(indio_dev
);
1237 sca3000_unconfigure_ring(indio_dev
);
1238 iio_device_free(indio_dev
);
1243 static const struct spi_device_id sca3000_id
[] = {
1244 {"sca3000_d01", d01
},
1245 {"sca3000_e02", e02
},
1246 {"sca3000_e04", e04
},
1247 {"sca3000_e05", e05
},
1250 MODULE_DEVICE_TABLE(spi
, sca3000_id
);
1252 static struct spi_driver sca3000_driver
= {
1255 .owner
= THIS_MODULE
,
1257 .probe
= sca3000_probe
,
1258 .remove
= sca3000_remove
,
1259 .id_table
= sca3000_id
,
1261 module_spi_driver(sca3000_driver
);
1263 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1264 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1265 MODULE_LICENSE("GPL v2");