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>
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>
23 #include "../buffer_generic.h"
27 enum sca3000_variant {
34 /* Note where option modes are not defined, the chip simply does not
36 * Other chips in the sca3000 series use i2c and are not included here.
38 * Some of these devices are only listed in the family data sheet and
39 * do not actually appear to be available.
41 static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
45 .measurement_mode_freq = 250,
46 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
47 .option_mode_1_freq = 250,
48 .mot_det_mult_xz = {50, 100, 200, 350, 650, 1300},
49 .mot_det_mult_y = {50, 100, 150, 250, 450, 850, 1750},
53 .measurement_mode_freq = 125,
54 .option_mode_1 = SCA3000_OP_MODE_NARROW,
55 .option_mode_1_freq = 63,
56 .mot_det_mult_xz = {100, 150, 300, 550, 1050, 2050},
57 .mot_det_mult_y = {50, 100, 200, 350, 700, 1350, 2700},
61 .measurement_mode_freq = 100,
62 .option_mode_1 = SCA3000_OP_MODE_NARROW,
63 .option_mode_1_freq = 50,
64 .option_mode_2 = SCA3000_OP_MODE_WIDE,
65 .option_mode_2_freq = 400,
66 .mot_det_mult_xz = {200, 300, 600, 1100, 2100, 4100},
67 .mot_det_mult_y = {100, 200, 400, 7000, 1400, 2700, 54000},
71 .measurement_mode_freq = 200,
72 .option_mode_1 = SCA3000_OP_MODE_NARROW,
73 .option_mode_1_freq = 50,
74 .option_mode_2 = SCA3000_OP_MODE_WIDE,
75 .option_mode_2_freq = 400,
76 .mot_det_mult_xz = {600, 900, 1700, 3200, 6100, 11900},
77 .mot_det_mult_y = {300, 600, 1200, 2000, 4100, 7800, 15600},
81 int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
83 st->tx[0] = SCA3000_WRITE_REG(address);
85 return spi_write(st->us, st->tx, 2);
88 int sca3000_read_data_short(struct sca3000_state *st,
89 uint8_t reg_address_high,
92 struct spi_message msg;
93 struct spi_transfer xfer[2] = {
102 st->tx[0] = SCA3000_READ_REG(reg_address_high);
103 spi_message_init(&msg);
104 spi_message_add_tail(&xfer[0], &msg);
105 spi_message_add_tail(&xfer[1], &msg);
107 return spi_sync(st->us, &msg);
111 * sca3000_reg_lock_on() test if the ctrl register lock is on
115 static int sca3000_reg_lock_on(struct sca3000_state *st)
119 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
123 return !(st->rx[0] & SCA3000_LOCKED);
127 * __sca3000_unlock_reg_lock() unlock the control registers
129 * Note the device does not appear to support doing this in a single transfer.
130 * This should only ever be used as part of ctrl reg read.
131 * Lock must be held before calling this
133 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
135 struct spi_message msg;
136 struct spi_transfer xfer[3] = {
144 .tx_buf = st->tx + 2,
147 .tx_buf = st->tx + 4,
150 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
152 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
154 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
156 spi_message_init(&msg);
157 spi_message_add_tail(&xfer[0], &msg);
158 spi_message_add_tail(&xfer[1], &msg);
159 spi_message_add_tail(&xfer[2], &msg);
161 return spi_sync(st->us, &msg);
165 * sca3000_write_ctrl_reg() write to a lock protect ctrl register
166 * @sel: selects which registers we wish to write to
167 * @val: the value to be written
169 * Certain control registers are protected against overwriting by the lock
170 * register and use a shared write address. This function allows writing of
174 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
181 ret = sca3000_reg_lock_on(st);
185 ret = __sca3000_unlock_reg_lock(st);
190 /* Set the control select register */
191 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
195 /* Write the actual value into the register */
196 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
202 /* Crucial that lock is called before calling this */
204 * sca3000_read_ctrl_reg() read from lock protected control register.
208 static int sca3000_read_ctrl_reg(struct sca3000_state *st,
213 ret = sca3000_reg_lock_on(st);
217 ret = __sca3000_unlock_reg_lock(st);
221 /* Set the control select register */
222 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
225 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_CTRL_DATA, 1);
236 * sca3000_check_status() check the status register
238 * Only used for debugging purposes
240 static int sca3000_check_status(struct device *dev)
243 struct iio_dev *indio_dev = dev_get_drvdata(dev);
244 struct sca3000_state *st = iio_priv(indio_dev);
246 mutex_lock(&st->lock);
247 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
250 if (st->rx[0] & SCA3000_EEPROM_CS_ERROR)
251 dev_err(dev, "eeprom error\n");
252 if (st->rx[0] & SCA3000_SPI_FRAME_ERROR)
253 dev_err(dev, "Previous SPI Frame was corrupt\n");
256 mutex_unlock(&st->lock);
259 #endif /* SCA3000_DEBUG */
263 * sca3000_show_reg() - sysfs interface to read the chip revision number
265 static ssize_t sca3000_show_rev(struct device *dev,
266 struct device_attribute *attr,
270 struct iio_dev *indio_dev = dev_get_drvdata(dev);
271 struct sca3000_state *st = iio_priv(indio_dev);
273 mutex_lock(&st->lock);
274 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_REVID, 1);
277 len += sprintf(buf + len,
278 "major=%d, minor=%d\n",
279 st->rx[0] & SCA3000_REVID_MAJOR_MASK,
280 st->rx[0] & SCA3000_REVID_MINOR_MASK);
282 mutex_unlock(&st->lock);
284 return ret ? ret : len;
288 * sca3000_show_available_measurement_modes() display available modes
290 * This is all read from chip specific data in the driver. Not all
291 * of the sca3000 series support modes other than normal.
294 sca3000_show_available_measurement_modes(struct device *dev,
295 struct device_attribute *attr,
298 struct iio_dev *indio_dev = dev_get_drvdata(dev);
299 struct sca3000_state *st = iio_priv(indio_dev);
302 len += sprintf(buf + len, "0 - normal mode");
303 switch (st->info->option_mode_1) {
304 case SCA3000_OP_MODE_NARROW:
305 len += sprintf(buf + len, ", 1 - narrow mode");
307 case SCA3000_OP_MODE_BYPASS:
308 len += sprintf(buf + len, ", 1 - bypass mode");
311 switch (st->info->option_mode_2) {
312 case SCA3000_OP_MODE_WIDE:
313 len += sprintf(buf + len, ", 2 - wide mode");
316 /* always supported */
317 len += sprintf(buf + len, " 3 - motion detection\n");
323 * sca3000_show_measurmenet_mode() sysfs read of current mode
326 sca3000_show_measurement_mode(struct device *dev,
327 struct device_attribute *attr,
330 struct iio_dev *indio_dev = dev_get_drvdata(dev);
331 struct sca3000_state *st = iio_priv(indio_dev);
334 mutex_lock(&st->lock);
335 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
338 /* mask bottom 2 bits - only ones that are relevant */
341 case SCA3000_MEAS_MODE_NORMAL:
342 len += sprintf(buf + len, "0 - normal mode\n");
344 case SCA3000_MEAS_MODE_MOT_DET:
345 len += sprintf(buf + len, "3 - motion detection\n");
347 case SCA3000_MEAS_MODE_OP_1:
348 switch (st->info->option_mode_1) {
349 case SCA3000_OP_MODE_NARROW:
350 len += sprintf(buf + len, "1 - narrow mode\n");
352 case SCA3000_OP_MODE_BYPASS:
353 len += sprintf(buf + len, "1 - bypass mode\n");
357 case SCA3000_MEAS_MODE_OP_2:
358 switch (st->info->option_mode_2) {
359 case SCA3000_OP_MODE_WIDE:
360 len += sprintf(buf + len, "2 - wide mode\n");
367 mutex_unlock(&st->lock);
369 return ret ? ret : len;
373 * sca3000_store_measurement_mode() set the current mode
376 sca3000_store_measurement_mode(struct device *dev,
377 struct device_attribute *attr,
381 struct iio_dev *indio_dev = dev_get_drvdata(dev);
382 struct sca3000_state *st = iio_priv(indio_dev);
387 mutex_lock(&st->lock);
388 ret = strict_strtol(buf, 10, &val);
391 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
395 st->rx[0] |= (val & mask);
396 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, st->rx[0]);
399 mutex_unlock(&st->lock);
404 mutex_unlock(&st->lock);
410 /* Not even vaguely standard attributes so defined here rather than
411 * in the relevant IIO core headers
413 static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO,
414 sca3000_show_available_measurement_modes,
417 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
418 sca3000_show_measurement_mode,
419 sca3000_store_measurement_mode,
422 /* More standard attributes */
424 static IIO_DEVICE_ATTR(revision, S_IRUGO, sca3000_show_rev, NULL, 0);
426 #define SCA3000_INFO_MASK \
427 (1 << IIO_CHAN_INFO_SCALE_SHARED)
428 #define SCA3000_EVENT_MASK \
429 (IIO_EV_BIT(IIO_EV_TYPE_MAG, IIO_EV_DIR_RISING))
431 static struct iio_chan_spec sca3000_channels[] = {
432 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X, SCA3000_INFO_MASK,
433 0, 0, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
434 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y, SCA3000_INFO_MASK,
435 1, 1, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
436 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z, SCA3000_INFO_MASK,
437 2, 2, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
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);
461 mutex_lock(&st->lock);
462 if (st->mo_det_use_count) {
463 mutex_unlock(&st->lock);
466 address = sca3000_addresses[chan->address][0];
467 ret = sca3000_read_data_short(st, address, 2);
469 mutex_unlock(&st->lock);
472 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
473 *val = ((*val) << (sizeof(*val)*8 - 13)) >>
474 (sizeof(*val)*8 - 13);
475 mutex_unlock(&st->lock);
477 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
479 if (chan->type == IIO_ACCEL)
480 *val2 = st->info->scale;
481 else /* temperature */
483 return IIO_VAL_INT_PLUS_MICRO;
490 * sca3000_read_av_freq() sysfs function to get available frequencies
492 * The later modes are only relevant to the ring buffer - and depend on current
493 * mode. Note that data sheet gives rather wide tolerances for these so integer
494 * division will give good enough answer and not all chips have them specified
497 static ssize_t sca3000_read_av_freq(struct device *dev,
498 struct device_attribute *attr,
501 struct iio_dev *indio_dev = dev_get_drvdata(dev);
502 struct sca3000_state *st = iio_priv(indio_dev);
503 int len = 0, ret, val;
505 mutex_lock(&st->lock);
506 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
508 mutex_unlock(&st->lock);
512 switch (val & 0x03) {
513 case SCA3000_MEAS_MODE_NORMAL:
514 len += sprintf(buf + len, "%d %d %d\n",
515 st->info->measurement_mode_freq,
516 st->info->measurement_mode_freq/2,
517 st->info->measurement_mode_freq/4);
519 case SCA3000_MEAS_MODE_OP_1:
520 len += sprintf(buf + len, "%d %d %d\n",
521 st->info->option_mode_1_freq,
522 st->info->option_mode_1_freq/2,
523 st->info->option_mode_1_freq/4);
525 case SCA3000_MEAS_MODE_OP_2:
526 len += sprintf(buf + len, "%d %d %d\n",
527 st->info->option_mode_2_freq,
528 st->info->option_mode_2_freq/2,
529 st->info->option_mode_2_freq/4);
537 * __sca3000_get_base_frequency() obtain mode specific base frequency
541 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
542 const struct sca3000_chip_info *info,
547 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
550 switch (0x03 & st->rx[0]) {
551 case SCA3000_MEAS_MODE_NORMAL:
552 *base_freq = info->measurement_mode_freq;
554 case SCA3000_MEAS_MODE_OP_1:
555 *base_freq = info->option_mode_1_freq;
557 case SCA3000_MEAS_MODE_OP_2:
558 *base_freq = info->option_mode_2_freq;
566 * sca3000_read_frequency() sysfs interface to get the current frequency
568 static ssize_t sca3000_read_frequency(struct device *dev,
569 struct device_attribute *attr,
572 struct iio_dev *indio_dev = dev_get_drvdata(dev);
573 struct sca3000_state *st = iio_priv(indio_dev);
574 int ret, len = 0, base_freq = 0, val;
576 mutex_lock(&st->lock);
577 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
580 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
581 mutex_unlock(&st->lock);
586 switch (val & 0x03) {
589 len = sprintf(buf, "%d\n", base_freq);
592 len = sprintf(buf, "%d\n", base_freq/2);
595 len = sprintf(buf, "%d\n", base_freq/4);
601 mutex_unlock(&st->lock);
607 * sca3000_set_frequency() sysfs interface to set the current frequency
609 static ssize_t sca3000_set_frequency(struct device *dev,
610 struct device_attribute *attr,
614 struct iio_dev *indio_dev = dev_get_drvdata(dev);
615 struct sca3000_state *st = iio_priv(indio_dev);
616 int ret, base_freq = 0;
620 ret = strict_strtol(buf, 10, &val);
624 mutex_lock(&st->lock);
625 /* What mode are we in? */
626 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
628 goto error_free_lock;
630 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
632 goto error_free_lock;
637 if (val == base_freq/2) {
638 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2;
639 } else if (val == base_freq/4) {
640 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4;
641 } else if (val != base_freq) {
643 goto error_free_lock;
645 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
648 mutex_unlock(&st->lock);
650 return ret ? ret : len;
653 /* Should only really be registered if ring buffer support is compiled in.
654 * Does no harm however and doing it right would add a fair bit of complexity
656 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
658 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
659 sca3000_read_frequency,
660 sca3000_set_frequency);
664 * sca3000_read_temp() sysfs interface to get the temperature when available
666 * The alignment of data in here is downright odd. See data sheet.
667 * Converting this into a meaningful value is left to inline functions in
668 * userspace part of header.
670 static ssize_t sca3000_read_temp(struct device *dev,
671 struct device_attribute *attr,
674 struct iio_dev *indio_dev = dev_get_drvdata(dev);
675 struct sca3000_state *st = iio_priv(indio_dev);
678 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_TEMP_MSB, 2);
681 val = ((st->rx[0] & 0x3F) << 3) | ((st->rx[1] & 0xE0) >> 5);
683 return sprintf(buf, "%d\n", val);
688 static IIO_DEV_ATTR_TEMP_RAW(sca3000_read_temp);
690 static IIO_CONST_ATTR_TEMP_SCALE("0.555556");
691 static IIO_CONST_ATTR_TEMP_OFFSET("-214.6");
694 * sca3000_read_thresh() - query of a threshold
696 static int sca3000_read_thresh(struct iio_dev *indio_dev,
701 struct sca3000_state *st = iio_priv(indio_dev);
702 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
703 mutex_lock(&st->lock);
704 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]);
705 mutex_unlock(&st->lock);
710 for_each_set_bit(i, (unsigned long *)&ret,
711 ARRAY_SIZE(st->info->mot_det_mult_y))
712 *val += st->info->mot_det_mult_y[i];
714 for_each_set_bit(i, (unsigned long *)&ret,
715 ARRAY_SIZE(st->info->mot_det_mult_xz))
716 *val += st->info->mot_det_mult_xz[i];
722 * sca3000_write_thresh() control of threshold
724 static int sca3000_write_thresh(struct iio_dev *indio_dev,
728 struct sca3000_state *st = iio_priv(indio_dev);
729 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
735 i = ARRAY_SIZE(st->info->mot_det_mult_y);
737 if (val >= st->info->mot_det_mult_y[--i]) {
738 nonlinear |= (1 << i);
739 val -= st->info->mot_det_mult_y[i];
742 i = ARRAY_SIZE(st->info->mot_det_mult_xz);
744 if (val >= st->info->mot_det_mult_xz[--i]) {
745 nonlinear |= (1 << i);
746 val -= st->info->mot_det_mult_xz[i];
750 mutex_lock(&st->lock);
751 ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear);
752 mutex_unlock(&st->lock);
757 static struct attribute *sca3000_attributes[] = {
758 &iio_dev_attr_revision.dev_attr.attr,
759 &iio_dev_attr_measurement_mode_available.dev_attr.attr,
760 &iio_dev_attr_measurement_mode.dev_attr.attr,
761 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
762 &iio_dev_attr_sampling_frequency.dev_attr.attr,
766 static struct attribute *sca3000_attributes_with_temp[] = {
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,
772 /* Only present if temp sensor is */
773 &iio_dev_attr_in_temp_raw.dev_attr.attr,
774 &iio_const_attr_in_temp_offset.dev_attr.attr,
775 &iio_const_attr_in_temp_scale.dev_attr.attr,
779 static const struct attribute_group sca3000_attribute_group = {
780 .attrs = sca3000_attributes,
783 static const struct attribute_group sca3000_attribute_group_with_temp = {
784 .attrs = sca3000_attributes_with_temp,
787 /* RING RELATED interrupt handler */
788 /* depending on event, push to the ring buffer event chrdev or the event one */
791 * sca3000_event_handler() - handling ring and non ring events
793 * This function is complicated by the fact that the devices can signify ring
794 * and non ring events via the same interrupt line and they can only
795 * be distinguished via a read of the relevant status register.
797 static irqreturn_t sca3000_event_handler(int irq, void *private)
799 struct iio_dev *indio_dev = private;
800 struct sca3000_state *st = iio_priv(indio_dev);
802 s64 last_timestamp = iio_get_time_ns();
804 /* Could lead if badly timed to an extra read of status reg,
805 * but ensures no interrupt is missed.
807 mutex_lock(&st->lock);
808 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
810 mutex_unlock(&st->lock);
814 sca3000_ring_int_process(val, indio_dev->buffer);
816 if (val & SCA3000_INT_STATUS_FREE_FALL)
817 iio_push_event(indio_dev,
818 IIO_MOD_EVENT_CODE(IIO_ACCEL,
820 IIO_MOD_X_AND_Y_AND_Z,
825 if (val & SCA3000_INT_STATUS_Y_TRIGGER)
826 iio_push_event(indio_dev,
827 IIO_MOD_EVENT_CODE(IIO_ACCEL,
834 if (val & SCA3000_INT_STATUS_X_TRIGGER)
835 iio_push_event(indio_dev,
836 IIO_MOD_EVENT_CODE(IIO_ACCEL,
843 if (val & SCA3000_INT_STATUS_Z_TRIGGER)
844 iio_push_event(indio_dev,
845 IIO_MOD_EVENT_CODE(IIO_ACCEL,
857 * sca3000_read_event_config() what events are enabled
859 static int sca3000_read_event_config(struct iio_dev *indio_dev,
862 struct sca3000_state *st = iio_priv(indio_dev);
864 u8 protect_mask = 0x03;
865 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
867 /* read current value of mode register */
868 mutex_lock(&st->lock);
869 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
873 if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
876 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
879 /* only supporting logical or's for now */
880 ret = !!(ret & sca3000_addresses[num][2]);
883 mutex_unlock(&st->lock);
888 * sca3000_query_free_fall_mode() is free fall mode enabled
890 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
891 struct device_attribute *attr,
895 struct iio_dev *indio_dev = dev_get_drvdata(dev);
896 struct sca3000_state *st = iio_priv(indio_dev);
899 mutex_lock(&st->lock);
900 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
902 mutex_unlock(&st->lock);
905 len = sprintf(buf, "%d\n",
906 !!(val & SCA3000_FREE_FALL_DETECT));
911 * sca3000_set_free_fall_mode() simple on off control for free fall int
913 * In these chips the free fall detector should send an interrupt if
914 * the device falls more than 25cm. This has not been tested due
918 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
919 struct device_attribute *attr,
923 struct iio_dev *indio_dev = dev_get_drvdata(dev);
924 struct sca3000_state *st = iio_priv(indio_dev);
927 u8 protect_mask = SCA3000_FREE_FALL_DETECT;
929 mutex_lock(&st->lock);
930 ret = strict_strtol(buf, 10, &val);
934 /* read current value of mode register */
935 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
939 /*if off and should be on*/
940 if (val && !(st->rx[0] & protect_mask))
941 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
942 (st->rx[0] | SCA3000_FREE_FALL_DETECT));
943 /* if on and should be off */
944 else if (!val && (st->rx[0] & protect_mask))
945 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
946 (st->rx[0] & ~protect_mask));
948 mutex_unlock(&st->lock);
950 return ret ? ret : len;
954 * sca3000_set_mo_det() simple on off control for motion detector
956 * This is a per axis control, but enabling any will result in the
957 * motion detector unit being enabled.
958 * N.B. enabling motion detector stops normal data acquisition.
959 * There is a complexity in knowing which mode to return to when
960 * this mode is disabled. Currently normal mode is assumed.
962 static int sca3000_write_event_config(struct iio_dev *indio_dev,
966 struct sca3000_state *st = iio_priv(indio_dev);
968 u8 protect_mask = 0x03;
969 int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
971 mutex_lock(&st->lock);
972 /* First read the motion detector config to find out if
974 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
978 /* Off and should be on */
979 if (state && !(ctrlval & sca3000_addresses[num][2])) {
980 ret = sca3000_write_ctrl_reg(st,
981 SCA3000_REG_CTRL_SEL_MD_CTRL,
983 sca3000_addresses[num][2]);
986 st->mo_det_use_count++;
987 } else if (!state && (ctrlval & sca3000_addresses[num][2])) {
988 ret = sca3000_write_ctrl_reg(st,
989 SCA3000_REG_CTRL_SEL_MD_CTRL,
991 ~(sca3000_addresses[num][2]));
994 st->mo_det_use_count--;
997 /* read current value of mode register */
998 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
1001 /*if off and should be on*/
1002 if ((st->mo_det_use_count)
1003 && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1004 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1005 (st->rx[0] & ~protect_mask)
1006 | SCA3000_MEAS_MODE_MOT_DET);
1007 /* if on and should be off */
1008 else if (!(st->mo_det_use_count)
1009 && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1010 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1011 (st->rx[0] & ~protect_mask));
1013 mutex_unlock(&st->lock);
1018 /* Free fall detector related event attribute */
1019 static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en,
1020 in_accel_x&y&z_mag_falling_en,
1022 sca3000_query_free_fall_mode,
1023 sca3000_set_free_fall_mode,
1026 static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period,
1027 in_accel_x&y&z_mag_falling_period,
1030 static struct attribute *sca3000_event_attributes[] = {
1031 &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1032 &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1036 static struct attribute_group sca3000_event_attribute_group = {
1037 .attrs = sca3000_event_attributes,
1042 * sca3000_clean_setup() get the device into a predictable state
1044 * Devices use flash memory to store many of the register values
1045 * and hence can come up in somewhat unpredictable states.
1046 * Hence reset everything on driver load.
1048 static int sca3000_clean_setup(struct sca3000_state *st)
1052 mutex_lock(&st->lock);
1053 /* Ensure all interrupts have been acknowledged */
1054 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
1058 /* Turn off all motion detection channels */
1059 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1062 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
1063 ret & SCA3000_MD_CTRL_PROT_MASK);
1067 /* Disable ring buffer */
1068 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
1069 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
1070 (ret & SCA3000_OUT_CTRL_PROT_MASK)
1071 | SCA3000_OUT_CTRL_BUF_X_EN
1072 | SCA3000_OUT_CTRL_BUF_Y_EN
1073 | SCA3000_OUT_CTRL_BUF_Z_EN
1074 | SCA3000_OUT_CTRL_BUF_DIV_4);
1077 /* Enable interrupts, relevant to mode and set up as active low */
1078 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1081 ret = sca3000_write_reg(st,
1082 SCA3000_REG_ADDR_INT_MASK,
1083 (ret & SCA3000_INT_MASK_PROT_MASK)
1084 | SCA3000_INT_MASK_ACTIVE_LOW);
1087 /* Select normal measurement mode, free fall off, ring off */
1088 /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1089 * as that occurs in one of the example on the datasheet */
1090 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
1093 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1094 (st->rx[0] & SCA3000_MODE_PROT_MASK));
1098 mutex_unlock(&st->lock);
1102 static const struct iio_info sca3000_info = {
1103 .attrs = &sca3000_attribute_group,
1104 .read_raw = &sca3000_read_raw,
1105 .event_attrs = &sca3000_event_attribute_group,
1106 .read_event_value = &sca3000_read_thresh,
1107 .write_event_value = &sca3000_write_thresh,
1108 .read_event_config = &sca3000_read_event_config,
1109 .write_event_config = &sca3000_write_event_config,
1110 .driver_module = THIS_MODULE,
1113 static const struct iio_info sca3000_info_with_temp = {
1114 .attrs = &sca3000_attribute_group_with_temp,
1115 .read_raw = &sca3000_read_raw,
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 int __devinit sca3000_probe(struct spi_device *spi)
1126 struct sca3000_state *st;
1127 struct iio_dev *indio_dev;
1129 indio_dev = iio_allocate_device(sizeof(*st));
1130 if (indio_dev == NULL) {
1135 st = iio_priv(indio_dev);
1136 spi_set_drvdata(spi, indio_dev);
1138 mutex_init(&st->lock);
1139 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
1142 indio_dev->dev.parent = &spi->dev;
1143 indio_dev->name = spi_get_device_id(spi)->name;
1144 if (st->info->temp_output)
1145 indio_dev->info = &sca3000_info_with_temp;
1147 indio_dev->info = &sca3000_info;
1148 indio_dev->channels = sca3000_channels;
1149 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
1151 indio_dev->modes = INDIO_DIRECT_MODE;
1153 sca3000_configure_ring(indio_dev);
1154 ret = iio_device_register(indio_dev);
1156 goto error_free_dev;
1158 ret = iio_buffer_register(indio_dev,
1160 ARRAY_SIZE(sca3000_channels));
1162 goto error_unregister_dev;
1163 if (indio_dev->buffer) {
1164 iio_scan_mask_set(indio_dev->buffer, 0);
1165 iio_scan_mask_set(indio_dev->buffer, 1);
1166 iio_scan_mask_set(indio_dev->buffer, 2);
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(indio_dev);
1180 ret = sca3000_clean_setup(st);
1182 goto error_free_irq;
1187 free_irq(spi->irq, indio_dev);
1188 error_unregister_ring:
1189 iio_buffer_unregister(indio_dev);
1190 error_unregister_dev:
1191 iio_device_unregister(indio_dev);
1193 iio_free_device(indio_dev);
1199 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1203 mutex_lock(&st->lock);
1204 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1207 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1209 ~(SCA3000_INT_MASK_RING_THREE_QUARTER |
1210 SCA3000_INT_MASK_RING_HALF |
1211 SCA3000_INT_MASK_ALL_INTS)));
1213 mutex_unlock(&st->lock);
1217 static int sca3000_remove(struct spi_device *spi)
1219 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1220 struct sca3000_state *st = iio_priv(indio_dev);
1222 /* Must ensure no interrupts can be generated after this!*/
1223 ret = sca3000_stop_all_interrupts(st);
1227 free_irq(spi->irq, indio_dev);
1228 iio_device_unregister(indio_dev);
1229 iio_buffer_unregister(indio_dev);
1230 sca3000_unconfigure_ring(indio_dev);
1231 iio_free_device(indio_dev);
1236 static const struct spi_device_id sca3000_id[] = {
1237 {"sca3000_d01", d01},
1238 {"sca3000_e02", e02},
1239 {"sca3000_e04", e04},
1240 {"sca3000_e05", e05},
1244 static struct spi_driver sca3000_driver = {
1247 .owner = THIS_MODULE,
1249 .probe = sca3000_probe,
1250 .remove = __devexit_p(sca3000_remove),
1251 .id_table = sca3000_id,
1254 static __init int sca3000_init(void)
1256 return spi_register_driver(&sca3000_driver);
1258 module_init(sca3000_init);
1260 static __exit void sca3000_exit(void)
1262 spi_unregister_driver(&sca3000_driver);
1264 module_exit(sca3000_exit);
1266 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1267 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1268 MODULE_LICENSE("GPL v2");