2 * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
8 * Copyright (c) 2009 Jonathan Cameron <jic23@cam.ac.uk>
10 * See industrialio/accels/sca3000.h for comments.
13 #include <linux/interrupt.h>
14 #include <linux/gpio.h>
16 #include <linux/device.h>
17 #include <linux/kernel.h>
18 #include <linux/spi/spi.h>
19 #include <linux/sysfs.h>
22 #include "../ring_generic.h"
27 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[] = {
45 .name = "sca3000-d01",
47 .measurement_mode_freq = 250,
48 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
49 .option_mode_1_freq = 250,
51 /* No data sheet available - may be the same as the 3100-d03?*/
52 .name = "sca3000-d03",
55 .name = "sca3000-e02",
56 .measurement_mode_freq = 125,
57 .option_mode_1 = SCA3000_OP_MODE_NARROW,
58 .option_mode_1_freq = 63,
60 .name = "sca3000-e04",
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,
67 .name = "sca3000-e05",
68 .measurement_mode_freq = 200,
69 .option_mode_1 = SCA3000_OP_MODE_NARROW,
70 .option_mode_1_freq = 50,
71 .option_mode_2 = SCA3000_OP_MODE_WIDE,
72 .option_mode_2_freq = 400,
74 /* No data sheet available.
75 * Frequencies are unknown.
77 .name = "sca3000-l01",
79 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
84 int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
86 struct spi_transfer xfer = {
92 struct spi_message msg;
94 st->tx[0] = SCA3000_WRITE_REG(address);
96 spi_message_init(&msg);
97 spi_message_add_tail(&xfer, &msg);
99 return spi_sync(st->us, &msg);
102 int sca3000_read_data(struct sca3000_state *st,
103 uint8_t reg_address_high,
108 struct spi_message msg;
109 struct spi_transfer xfer = {
116 *rx_p = kmalloc(len + 1, GFP_KERNEL);
122 st->tx[0] = SCA3000_READ_REG(reg_address_high);
123 spi_message_init(&msg);
124 spi_message_add_tail(&xfer, &msg);
126 ret = spi_sync(st->us, &msg);
129 dev_err(get_device(&st->us->dev), "problem reading register");
141 * sca3000_reg_lock_on() test if the ctrl register lock is on
145 static int sca3000_reg_lock_on(struct sca3000_state *st)
150 ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1);
154 ret = !(rx[1] & SCA3000_LOCKED);
161 * __sca3000_unlock_reg_lock() unlock the control registers
163 * Note the device does not appear to support doing this in a single transfer.
164 * This should only ever be used as part of ctrl reg read.
165 * Lock must be held before calling this
167 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
169 struct spi_message msg;
170 struct spi_transfer xfer[3] = {
180 .tx_buf = st->tx + 2,
185 .tx_buf = st->tx + 4,
188 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
190 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
192 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
194 spi_message_init(&msg);
195 spi_message_add_tail(&xfer[0], &msg);
196 spi_message_add_tail(&xfer[1], &msg);
197 spi_message_add_tail(&xfer[2], &msg);
199 return spi_sync(st->us, &msg);
203 * sca3000_write_ctrl_reg() write to a lock protect ctrl register
204 * @sel: selects which registers we wish to write to
205 * @val: the value to be written
207 * Certain control registers are protected against overwriting by the lock
208 * register and use a shared write address. This function allows writing of
212 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
219 ret = sca3000_reg_lock_on(st);
223 ret = __sca3000_unlock_reg_lock(st);
228 /* Set the control select register */
229 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
233 /* Write the actual value into the register */
234 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
240 /* Crucial that lock is called before calling this */
242 * sca3000_read_ctrl_reg() read from lock protected control register.
246 static int sca3000_read_ctrl_reg(struct sca3000_state *st,
252 ret = sca3000_reg_lock_on(st);
256 ret = __sca3000_unlock_reg_lock(st);
260 /* Set the control select register */
261 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
264 ret = sca3000_read_data(st, SCA3000_REG_ADDR_CTRL_DATA, rx_p, 1);
272 * sca3000_check_status() check the status register
274 * Only used for debugging purposes
276 static int sca3000_check_status(struct device *dev)
280 struct iio_dev *indio_dev = dev_get_drvdata(dev);
281 struct sca3000_state *st = indio_dev->dev_data;
283 mutex_lock(&st->lock);
284 ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1);
287 if (rx[1] & SCA3000_EEPROM_CS_ERROR)
288 dev_err(dev, "eeprom error \n");
289 if (rx[1] & SCA3000_SPI_FRAME_ERROR)
290 dev_err(dev, "Previous SPI Frame was corrupt\n");
294 mutex_unlock(&st->lock);
297 #endif /* SCA3000_DEBUG */
300 * sca3000_read_13bit_signed() sysfs interface to read 13 bit signed registers
302 * These are described as signed 12 bit on the data sheet, which appears
303 * to be a conventional 2's complement 13 bit.
305 static ssize_t sca3000_read_13bit_signed(struct device *dev,
306 struct device_attribute *attr,
311 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
313 struct iio_dev *indio_dev = dev_get_drvdata(dev);
314 struct sca3000_state *st = indio_dev->dev_data;
316 mutex_lock(&st->lock);
317 ret = sca3000_read_data(st, this_attr->address, &rx, 2);
320 val = sca3000_13bit_convert(rx[1], rx[2]);
321 len += sprintf(buf + len, "%d\n", val);
324 mutex_unlock(&st->lock);
326 return ret ? ret : len;
330 static ssize_t sca3000_show_name(struct device *dev,
331 struct device_attribute *attr,
334 struct iio_dev *dev_info = dev_get_drvdata(dev);
335 struct sca3000_state *st = dev_info->dev_data;
336 return sprintf(buf, "%s\n", st->info->name);
339 * sca3000_show_reg() - sysfs interface to read the chip revision number
341 static ssize_t sca3000_show_rev(struct device *dev,
342 struct device_attribute *attr,
346 struct iio_dev *dev_info = dev_get_drvdata(dev);
347 struct sca3000_state *st = dev_info->dev_data;
351 mutex_lock(&st->lock);
352 ret = sca3000_read_data(st, SCA3000_REG_ADDR_REVID, &rx, 1);
355 len += sprintf(buf + len,
356 "major=%d, minor=%d\n",
357 rx[1] & SCA3000_REVID_MAJOR_MASK,
358 rx[1] & SCA3000_REVID_MINOR_MASK);
362 mutex_unlock(&st->lock);
364 return ret ? ret : len;
368 * sca3000_show_available_measurement_modes() display available modes
370 * This is all read from chip specific data in the driver. Not all
371 * of the sca3000 series support modes other than normal.
374 sca3000_show_available_measurement_modes(struct device *dev,
375 struct device_attribute *attr,
378 struct iio_dev *dev_info = dev_get_drvdata(dev);
379 struct sca3000_state *st = dev_info->dev_data;
382 len += sprintf(buf + len, "0 - normal mode");
383 switch (st->info->option_mode_1) {
384 case SCA3000_OP_MODE_NARROW:
385 len += sprintf(buf + len, ", 1 - narrow mode");
387 case SCA3000_OP_MODE_BYPASS:
388 len += sprintf(buf + len, ", 1 - bypass mode");
391 switch (st->info->option_mode_2) {
392 case SCA3000_OP_MODE_WIDE:
393 len += sprintf(buf + len, ", 2 - wide mode");
396 /* always supported */
397 len += sprintf(buf + len, " 3 - motion detection \n");
403 * sca3000_show_measurmenet_mode() sysfs read of current mode
406 sca3000_show_measurement_mode(struct device *dev,
407 struct device_attribute *attr,
410 struct iio_dev *dev_info = dev_get_drvdata(dev);
411 struct sca3000_state *st = dev_info->dev_data;
415 mutex_lock(&st->lock);
416 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
419 /* mask bottom 2 bits - only ones that are relevant */
422 case SCA3000_MEAS_MODE_NORMAL:
423 len += sprintf(buf + len, "0 - normal mode\n");
425 case SCA3000_MEAS_MODE_MOT_DET:
426 len += sprintf(buf + len, "3 - motion detection\n");
428 case SCA3000_MEAS_MODE_OP_1:
429 switch (st->info->option_mode_1) {
430 case SCA3000_OP_MODE_NARROW:
431 len += sprintf(buf + len, "1 - narrow mode\n");
433 case SCA3000_OP_MODE_BYPASS:
434 len += sprintf(buf + len, "1 - bypass mode\n");
438 case SCA3000_MEAS_MODE_OP_2:
439 switch (st->info->option_mode_2) {
440 case SCA3000_OP_MODE_WIDE:
441 len += sprintf(buf + len, "2 - wide mode\n");
448 mutex_unlock(&st->lock);
450 return ret ? ret : len;
454 * sca3000_store_measurement_mode() set the current mode
457 sca3000_store_measurement_mode(struct device *dev,
458 struct device_attribute *attr,
462 struct iio_dev *dev_info = dev_get_drvdata(dev);
463 struct sca3000_state *st = dev_info->dev_data;
469 mutex_lock(&st->lock);
470 ret = strict_strtol(buf, 10, &val);
473 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
477 rx[1] |= (val & mask);
478 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, rx[1]);
481 mutex_unlock(&st->lock);
488 mutex_unlock(&st->lock);
494 /* Not even vaguely standard attributes so defined here rather than
495 * in the relevant IIO core headers
497 static IIO_DEVICE_ATTR(available_measurement_modes, S_IRUGO,
498 sca3000_show_available_measurement_modes,
501 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
502 sca3000_show_measurement_mode,
503 sca3000_store_measurement_mode,
506 /* More standard attributes */
508 static IIO_DEV_ATTR_NAME(sca3000_show_name);
509 static IIO_DEV_ATTR_REV(sca3000_show_rev);
511 static IIO_DEV_ATTR_ACCEL_X(sca3000_read_13bit_signed,
512 SCA3000_REG_ADDR_X_MSB);
513 static IIO_DEV_ATTR_ACCEL_Y(sca3000_read_13bit_signed,
514 SCA3000_REG_ADDR_Y_MSB);
515 static IIO_DEV_ATTR_ACCEL_Z(sca3000_read_13bit_signed,
516 SCA3000_REG_ADDR_Z_MSB);
520 * sca3000_read_av_freq() sysfs function to get available frequencies
522 * The later modes are only relevant to the ring buffer - and depend on current
523 * mode. Note that data sheet gives rather wide tolerances for these so integer
524 * division will give good enough answer and not all chips have them specified
527 static ssize_t sca3000_read_av_freq(struct device *dev,
528 struct device_attribute *attr,
531 struct iio_dev *indio_dev = dev_get_drvdata(dev);
532 struct sca3000_state *st = indio_dev->dev_data;
535 mutex_lock(&st->lock);
536 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
537 mutex_unlock(&st->lock);
542 case SCA3000_MEAS_MODE_NORMAL:
543 len += sprintf(buf + len, "%d %d %d\n",
544 st->info->measurement_mode_freq,
545 st->info->measurement_mode_freq/2,
546 st->info->measurement_mode_freq/4);
548 case SCA3000_MEAS_MODE_OP_1:
549 len += sprintf(buf + len, "%d %d %d\n",
550 st->info->option_mode_1_freq,
551 st->info->option_mode_1_freq/2,
552 st->info->option_mode_1_freq/4);
554 case SCA3000_MEAS_MODE_OP_2:
555 len += sprintf(buf + len, "%d %d %d\n",
556 st->info->option_mode_2_freq,
557 st->info->option_mode_2_freq/2,
558 st->info->option_mode_2_freq/4);
567 * __sca3000_get_base_frequency() obtain mode specific base frequency
571 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
572 const struct sca3000_chip_info *info,
578 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
581 switch (0x03 & rx[1]) {
582 case SCA3000_MEAS_MODE_NORMAL:
583 *base_freq = info->measurement_mode_freq;
585 case SCA3000_MEAS_MODE_OP_1:
586 *base_freq = info->option_mode_1_freq;
588 case SCA3000_MEAS_MODE_OP_2:
589 *base_freq = info->option_mode_2_freq;
598 * sca3000_read_frequency() sysfs interface to get the current frequency
600 static ssize_t sca3000_read_frequency(struct device *dev,
601 struct device_attribute *attr,
604 struct iio_dev *indio_dev = dev_get_drvdata(dev);
605 struct sca3000_state *st = indio_dev->dev_data;
606 int ret, len = 0, base_freq = 0;
608 mutex_lock(&st->lock);
609 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
612 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx);
613 mutex_unlock(&st->lock);
617 switch (rx[1]&0x03) {
620 len = sprintf(buf, "%d\n", base_freq);
623 len = sprintf(buf, "%d\n", base_freq/2);
626 len = sprintf(buf, "%d\n", base_freq/4);
632 mutex_unlock(&st->lock);
638 * sca3000_set_frequency() sysfs interface to set the current frequency
640 static ssize_t sca3000_set_frequency(struct device *dev,
641 struct device_attribute *attr,
645 struct iio_dev *indio_dev = dev_get_drvdata(dev);
646 struct sca3000_state *st = indio_dev->dev_data;
647 int ret, base_freq = 0;
651 ret = strict_strtol(buf, 10, &val);
655 mutex_lock(&st->lock);
656 /* What mode are we in? */
657 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
659 goto error_free_lock;
661 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx);
663 goto error_free_lock;
667 if (val == base_freq/2) {
668 rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_2;
669 } else if (val == base_freq/4) {
670 rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_4;
671 } else if (val != base_freq) {
673 goto error_free_lock;
675 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, rx[1]);
677 mutex_unlock(&st->lock);
679 return ret ? ret : len;
682 /* Should only really be registered if ring buffer support is compiled in.
683 * Does no harm however and doing it right would add a fair bit of complexity
685 static IIO_DEV_ATTR_AVAIL_SAMP_FREQ(sca3000_read_av_freq);
687 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
688 sca3000_read_frequency,
689 sca3000_set_frequency);
693 * sca3000_read_temp() sysfs interface to get the temperature when available
695 * The alignment of data in here is downright odd. See data sheet.
696 * Converting this into a meaningful value is left to inline functions in
697 * userspace part of header.
699 static ssize_t sca3000_read_temp(struct device *dev,
700 struct device_attribute *attr,
703 struct iio_dev *indio_dev = dev_get_drvdata(dev);
704 struct sca3000_state *st = indio_dev->dev_data;
708 ret = sca3000_read_data(st, SCA3000_REG_ADDR_TEMP_MSB, &rx, 2);
711 val = ((rx[1]&0x3F) << 3) | ((rx[2] & 0xE0) >> 5);
712 len += sprintf(buf + len, "%d\n", val);
720 static IIO_DEV_ATTR_TEMP(sca3000_read_temp);
723 * sca3000_show_thresh() sysfs query of a threshold
725 static ssize_t sca3000_show_thresh(struct device *dev,
726 struct device_attribute *attr,
729 struct iio_dev *indio_dev = dev_get_drvdata(dev);
730 struct sca3000_state *st = indio_dev->dev_data;
731 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
735 mutex_lock(&st->lock);
736 ret = sca3000_read_ctrl_reg(st,
739 mutex_unlock(&st->lock);
742 len += sprintf(buf + len, "%d\n", rx[1]);
749 * sca3000_write_thresh() sysfs control of threshold
751 static ssize_t sca3000_write_thresh(struct device *dev,
752 struct device_attribute *attr,
756 struct iio_dev *indio_dev = dev_get_drvdata(dev);
757 struct sca3000_state *st = indio_dev->dev_data;
758 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
762 ret = strict_strtol(buf, 10, &val);
765 mutex_lock(&st->lock);
766 ret = sca3000_write_ctrl_reg(st, this_attr->address, val);
767 mutex_unlock(&st->lock);
769 return ret ? ret : len;
772 static IIO_DEV_ATTR_ACCEL_THRESH_X(S_IRUGO | S_IWUSR,
774 sca3000_write_thresh,
775 SCA3000_REG_CTRL_SEL_MD_X_TH);
776 static IIO_DEV_ATTR_ACCEL_THRESH_Y(S_IRUGO | S_IWUSR,
778 sca3000_write_thresh,
779 SCA3000_REG_CTRL_SEL_MD_Y_TH);
780 static IIO_DEV_ATTR_ACCEL_THRESH_Z(S_IRUGO | S_IWUSR,
782 sca3000_write_thresh,
783 SCA3000_REG_CTRL_SEL_MD_Z_TH);
785 static struct attribute *sca3000_attributes[] = {
786 &iio_dev_attr_name.dev_attr.attr,
787 &iio_dev_attr_revision.dev_attr.attr,
788 &iio_dev_attr_accel_x.dev_attr.attr,
789 &iio_dev_attr_accel_y.dev_attr.attr,
790 &iio_dev_attr_accel_z.dev_attr.attr,
791 &iio_dev_attr_thresh_accel_x.dev_attr.attr,
792 &iio_dev_attr_thresh_accel_y.dev_attr.attr,
793 &iio_dev_attr_thresh_accel_z.dev_attr.attr,
794 &iio_dev_attr_available_measurement_modes.dev_attr.attr,
795 &iio_dev_attr_measurement_mode.dev_attr.attr,
796 &iio_dev_attr_available_sampling_frequency.dev_attr.attr,
797 &iio_dev_attr_sampling_frequency.dev_attr.attr,
801 static struct attribute *sca3000_attributes_with_temp[] = {
802 &iio_dev_attr_name.dev_attr.attr,
803 &iio_dev_attr_revision.dev_attr.attr,
804 &iio_dev_attr_accel_x.dev_attr.attr,
805 &iio_dev_attr_accel_y.dev_attr.attr,
806 &iio_dev_attr_accel_z.dev_attr.attr,
807 &iio_dev_attr_thresh_accel_x.dev_attr.attr,
808 &iio_dev_attr_thresh_accel_y.dev_attr.attr,
809 &iio_dev_attr_thresh_accel_z.dev_attr.attr,
810 &iio_dev_attr_available_measurement_modes.dev_attr.attr,
811 &iio_dev_attr_measurement_mode.dev_attr.attr,
812 &iio_dev_attr_available_sampling_frequency.dev_attr.attr,
813 &iio_dev_attr_sampling_frequency.dev_attr.attr,
814 /* Only present if temp sensor is */
815 &iio_dev_attr_temp.dev_attr.attr,
819 static const struct attribute_group sca3000_attribute_group = {
820 .attrs = sca3000_attributes,
823 static const struct attribute_group sca3000_attribute_group_with_temp = {
824 .attrs = sca3000_attributes_with_temp,
827 /* RING RELATED interrupt handler */
828 /* depending on event, push to the ring buffer event chrdev or the event one */
831 * sca3000_interrupt_handler_bh() - handling ring and non ring events
833 * This function is complicated by the fact that the devices can signify ring
834 * and non ring events via the same interrupt line and they can only
835 * be distinguished via a read of the relevant status register.
837 static void sca3000_interrupt_handler_bh(struct work_struct *work_s)
839 struct sca3000_state *st
840 = container_of(work_s, struct sca3000_state,
841 interrupt_handler_ws);
845 /* Could lead if badly timed to an extra read of status reg,
846 * but ensures no interrupt is missed.
848 enable_irq(st->us->irq);
849 mutex_lock(&st->lock);
850 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS,
852 mutex_unlock(&st->lock);
856 sca3000_ring_int_process(rx[1], st->indio_dev->ring);
858 if (rx[1] & SCA3000_INT_STATUS_FREE_FALL)
859 iio_push_event(st->indio_dev, 0,
860 IIO_EVENT_CODE_FREE_FALL,
863 if (rx[1] & SCA3000_INT_STATUS_Y_TRIGGER)
864 iio_push_event(st->indio_dev, 0,
865 IIO_EVENT_CODE_ACCEL_Y_HIGH,
868 if (rx[1] & SCA3000_INT_STATUS_X_TRIGGER)
869 iio_push_event(st->indio_dev, 0,
870 IIO_EVENT_CODE_ACCEL_X_HIGH,
873 if (rx[1] & SCA3000_INT_STATUS_Z_TRIGGER)
874 iio_push_event(st->indio_dev, 0,
875 IIO_EVENT_CODE_ACCEL_Z_HIGH,
884 * sca3000_handler_th() handles all interrupt events from device
886 * These devices deploy unified interrupt status registers meaning
887 * all interrupts must be handled together
889 static int sca3000_handler_th(struct iio_dev *dev_info,
894 struct sca3000_state *st = dev_info->dev_data;
896 st->last_timestamp = timestamp;
897 schedule_work(&st->interrupt_handler_ws);
903 * sca3000_query_mo_det() is motion detection enabled for this axis
905 * First queries if motion detection is enabled and then if this axis is
908 static ssize_t sca3000_query_mo_det(struct device *dev,
909 struct device_attribute *attr,
912 struct iio_dev *indio_dev = dev_get_drvdata(dev);
913 struct sca3000_state *st = indio_dev->dev_data;
914 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
917 u8 protect_mask = 0x03;
919 /* read current value of mode register */
920 mutex_lock(&st->lock);
921 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
925 if ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
926 len += sprintf(buf + len, "0\n");
929 ret = sca3000_read_ctrl_reg(st,
930 SCA3000_REG_CTRL_SEL_MD_CTRL,
934 /* only supporting logical or's for now */
935 len += sprintf(buf + len, "%d\n",
936 (rx[1] & this_attr->mask) ? 1 : 0);
940 mutex_unlock(&st->lock);
942 return ret ? ret : len;
945 * sca3000_query_free_fall_mode() is free fall mode enabled
947 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
948 struct device_attribute *attr,
953 struct iio_dev *indio_dev = dev_get_drvdata(dev);
954 struct sca3000_state *st = indio_dev->dev_data;
956 mutex_lock(&st->lock);
957 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
958 mutex_unlock(&st->lock);
961 len = sprintf(buf, "%d\n",
962 !!(rx[1] & SCA3000_FREE_FALL_DETECT));
968 * sca3000_query_ring_int() is the hardware ring status interrupt enabled
970 static ssize_t sca3000_query_ring_int(struct device *dev,
971 struct device_attribute *attr,
974 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
977 struct iio_dev *indio_dev = dev_get_drvdata(dev);
978 struct sca3000_state *st = indio_dev->dev_data;
979 mutex_lock(&st->lock);
980 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
981 mutex_unlock(&st->lock);
984 len = sprintf(buf, "%d\n", (rx[1] & this_attr->mask) ? 1 : 0);
990 * sca3000_set_ring_int() set state of ring status interrupt
992 static ssize_t sca3000_set_ring_int(struct device *dev,
993 struct device_attribute *attr,
997 struct iio_dev *indio_dev = dev_get_drvdata(dev);
998 struct sca3000_state *st = indio_dev->dev_data;
999 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1005 mutex_lock(&st->lock);
1006 ret = strict_strtol(buf, 10, &val);
1009 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1013 ret = sca3000_write_reg(st,
1014 SCA3000_REG_ADDR_INT_MASK,
1015 rx[1] | this_attr->mask);
1017 ret = sca3000_write_reg(st,
1018 SCA3000_REG_ADDR_INT_MASK,
1019 rx[1] & ~this_attr->mask);
1022 mutex_unlock(&st->lock);
1024 return ret ? ret : len;
1028 * sca3000_set_free_fall_mode() simple on off control for free fall int
1030 * In these chips the free fall detector should send an interrupt if
1031 * the device falls more than 25cm. This has not been tested due
1032 * to fragile wiring.
1035 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
1036 struct device_attribute *attr,
1040 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1041 struct sca3000_state *st = indio_dev->dev_data;
1045 u8 protect_mask = SCA3000_FREE_FALL_DETECT;
1047 mutex_lock(&st->lock);
1048 ret = strict_strtol(buf, 10, &val);
1052 /* read current value of mode register */
1053 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
1057 /*if off and should be on*/
1058 if (val && !(rx[1] & protect_mask))
1059 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1060 (rx[1] | SCA3000_FREE_FALL_DETECT));
1061 /* if on and should be off */
1062 else if (!val && (rx[1]&protect_mask))
1063 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1064 (rx[1] & ~protect_mask));
1068 mutex_unlock(&st->lock);
1070 return ret ? ret : len;
1074 * sca3000_set_mo_det() simple on off control for motion detector
1076 * This is a per axis control, but enabling any will result in the
1077 * motion detector unit being enabled.
1078 * N.B. enabling motion detector stops normal data acquisition.
1079 * There is a complexity in knowing which mode to return to when
1080 * this mode is disabled. Currently normal mode is assumed.
1082 static ssize_t sca3000_set_mo_det(struct device *dev,
1083 struct device_attribute *attr,
1087 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1088 struct sca3000_state *st = indio_dev->dev_data;
1089 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1093 u8 protect_mask = 0x03;
1094 ret = strict_strtol(buf, 10, &val);
1098 mutex_lock(&st->lock);
1099 /* First read the motion detector config to find out if
1101 ret = sca3000_read_ctrl_reg(st,
1102 SCA3000_REG_CTRL_SEL_MD_CTRL,
1106 /* Off and should be on */
1107 if (val && !(rx[1] & this_attr->mask)) {
1108 ret = sca3000_write_ctrl_reg(st,
1109 SCA3000_REG_CTRL_SEL_MD_CTRL,
1110 rx[1] | this_attr->mask);
1112 goto exit_point_free_rx;
1113 st->mo_det_use_count++;
1114 } else if (!val && (rx[1]&this_attr->mask)) {
1115 ret = sca3000_write_ctrl_reg(st,
1116 SCA3000_REG_CTRL_SEL_MD_CTRL,
1117 rx[1] & ~(this_attr->mask));
1119 goto exit_point_free_rx;
1120 st->mo_det_use_count--;
1121 } else /* relies on clean state for device on boot */
1122 goto exit_point_free_rx;
1124 /* read current value of mode register */
1125 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
1128 /*if off and should be on*/
1129 if ((st->mo_det_use_count)
1130 && ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1131 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1132 (rx[1] & ~protect_mask)
1133 | SCA3000_MEAS_MODE_MOT_DET);
1134 /* if on and should be off */
1135 else if (!(st->mo_det_use_count)
1136 && ((rx[1]&protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1137 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1138 (rx[1] & ~protect_mask));
1142 mutex_unlock(&st->lock);
1144 return ret ? ret : len;
1147 /* Shared event handler for all events as single event status register */
1148 IIO_EVENT_SH(all, &sca3000_handler_th);
1150 /* Free fall detector related event attribute */
1151 IIO_EVENT_ATTR_FREE_FALL_DETECT_SH(iio_event_all,
1152 sca3000_query_free_fall_mode,
1153 sca3000_set_free_fall_mode,
1156 /* Motion detector related event attributes */
1157 IIO_EVENT_ATTR_ACCEL_X_HIGH_SH(iio_event_all,
1158 sca3000_query_mo_det,
1160 SCA3000_MD_CTRL_OR_X);
1162 IIO_EVENT_ATTR_ACCEL_Y_HIGH_SH(iio_event_all,
1163 sca3000_query_mo_det,
1165 SCA3000_MD_CTRL_OR_Y);
1167 IIO_EVENT_ATTR_ACCEL_Z_HIGH_SH(iio_event_all,
1168 sca3000_query_mo_det,
1170 SCA3000_MD_CTRL_OR_Z);
1172 /* Hardware ring buffer related event attributes */
1173 IIO_EVENT_ATTR_RING_50_FULL_SH(iio_event_all,
1174 sca3000_query_ring_int,
1175 sca3000_set_ring_int,
1176 SCA3000_INT_MASK_RING_HALF);
1178 IIO_EVENT_ATTR_RING_75_FULL_SH(iio_event_all,
1179 sca3000_query_ring_int,
1180 sca3000_set_ring_int,
1181 SCA3000_INT_MASK_RING_THREE_QUARTER);
1183 static struct attribute *sca3000_event_attributes[] = {
1184 &iio_event_attr_free_fall.dev_attr.attr,
1185 &iio_event_attr_accel_x_high.dev_attr.attr,
1186 &iio_event_attr_accel_y_high.dev_attr.attr,
1187 &iio_event_attr_accel_z_high.dev_attr.attr,
1188 &iio_event_attr_ring_50_full.dev_attr.attr,
1189 &iio_event_attr_ring_75_full.dev_attr.attr,
1193 static struct attribute_group sca3000_event_attribute_group = {
1194 .attrs = sca3000_event_attributes,
1198 * sca3000_clean_setup() get the device into a predictable state
1200 * Devices use flash memory to store many of the register values
1201 * and hence can come up in somewhat unpredictable states.
1202 * Hence reset everything on driver load.
1204 static int sca3000_clean_setup(struct sca3000_state *st)
1209 mutex_lock(&st->lock);
1210 /* Ensure all interrupts have been acknowledged */
1211 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS, &rx, 1);
1216 /* Turn off all motion detection channels */
1217 ret = sca3000_read_ctrl_reg(st,
1218 SCA3000_REG_CTRL_SEL_MD_CTRL,
1222 ret = sca3000_write_ctrl_reg(st,
1223 SCA3000_REG_CTRL_SEL_MD_CTRL,
1224 rx[1] & SCA3000_MD_CTRL_PROT_MASK);
1229 /* Disable ring buffer */
1230 sca3000_read_ctrl_reg(st,
1231 SCA3000_REG_CTRL_SEL_OUT_CTRL,
1233 /* Frequency of ring buffer sampling deliberately restricted to make
1234 * debugging easier - add control of this later */
1235 ret = sca3000_write_ctrl_reg(st,
1236 SCA3000_REG_CTRL_SEL_OUT_CTRL,
1237 (rx[1] & SCA3000_OUT_CTRL_PROT_MASK)
1238 | SCA3000_OUT_CTRL_BUF_X_EN
1239 | SCA3000_OUT_CTRL_BUF_Y_EN
1240 | SCA3000_OUT_CTRL_BUF_Z_EN
1241 | SCA3000_OUT_CTRL_BUF_DIV_4);
1246 /* Enable interrupts, relevant to mode and set up as active low */
1247 ret = sca3000_read_data(st,
1248 SCA3000_REG_ADDR_INT_MASK,
1252 ret = sca3000_write_reg(st,
1253 SCA3000_REG_ADDR_INT_MASK,
1254 (rx[1] & SCA3000_INT_MASK_PROT_MASK)
1255 | SCA3000_INT_MASK_ACTIVE_LOW);
1259 /* Select normal measurement mode, free fall off, ring off */
1260 /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1261 * as that occurs in one of the example on the datasheet */
1262 ret = sca3000_read_data(st,
1263 SCA3000_REG_ADDR_MODE,
1267 ret = sca3000_write_reg(st,
1268 SCA3000_REG_ADDR_MODE,
1269 (rx[1] & SCA3000_MODE_PROT_MASK));
1274 mutex_unlock(&st->lock);
1278 static int __devinit __sca3000_probe(struct spi_device *spi,
1279 enum sca3000_variant variant)
1281 int ret, regdone = 0;
1282 struct sca3000_state *st;
1284 st = kzalloc(sizeof(struct sca3000_state), GFP_KERNEL);
1289 spi_set_drvdata(spi, st);
1291 st->tx = kmalloc(sizeof(*st->tx)*6, GFP_KERNEL);
1292 if (st->tx == NULL) {
1294 goto error_clear_st;
1296 st->rx = kmalloc(sizeof(*st->rx)*3, GFP_KERNEL);
1297 if (st->rx == NULL) {
1302 mutex_init(&st->lock);
1303 st->info = &sca3000_spi_chip_info_tbl[variant];
1305 st->indio_dev = iio_allocate_device();
1306 if (st->indio_dev == NULL) {
1311 st->indio_dev->dev.parent = &spi->dev;
1312 st->indio_dev->num_interrupt_lines = 1;
1313 st->indio_dev->event_attrs = &sca3000_event_attribute_group;
1314 if (st->info->temp_output)
1315 st->indio_dev->attrs = &sca3000_attribute_group_with_temp;
1317 st->indio_dev->attrs = &sca3000_attribute_group;
1318 st->indio_dev->dev_data = (void *)(st);
1319 st->indio_dev->modes = INDIO_DIRECT_MODE;
1321 sca3000_configure_ring(st->indio_dev);
1323 ret = iio_device_register(st->indio_dev);
1325 goto error_free_dev;
1327 ret = iio_ring_buffer_register(st->indio_dev->ring);
1329 goto error_unregister_dev;
1330 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
1331 INIT_WORK(&st->interrupt_handler_ws,
1332 sca3000_interrupt_handler_bh);
1333 ret = iio_register_interrupt_line(spi->irq,
1336 IRQF_TRIGGER_FALLING,
1339 goto error_unregister_ring;
1341 * Probably a common situation. All interrupts need an ack
1342 * and there is only one handler so the complicated list system
1343 * is overkill. At very least a simpler registration method
1344 * might be worthwhile.
1346 iio_add_event_to_list(iio_event_attr_accel_z_high.listel,
1348 ->interrupts[0]->ev_list);
1350 sca3000_register_ring_funcs(st->indio_dev);
1351 ret = sca3000_clean_setup(st);
1353 goto error_unregister_interrupt_line;
1356 error_unregister_interrupt_line:
1357 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1358 iio_unregister_interrupt_line(st->indio_dev, 0);
1359 error_unregister_ring:
1360 iio_ring_buffer_unregister(st->indio_dev->ring);
1361 error_unregister_dev:
1364 iio_device_unregister(st->indio_dev);
1366 iio_free_device(st->indio_dev);
1377 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1382 mutex_lock(&st->lock);
1383 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1386 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1387 (rx[1] & ~(SCA3000_INT_MASK_RING_THREE_QUARTER
1388 | SCA3000_INT_MASK_RING_HALF
1389 | SCA3000_INT_MASK_ALL_INTS)));
1396 static int sca3000_remove(struct spi_device *spi)
1398 struct sca3000_state *st = spi_get_drvdata(spi);
1399 struct iio_dev *indio_dev = st->indio_dev;
1401 /* Must ensure no interrupts can be generated after this!*/
1402 ret = sca3000_stop_all_interrupts(st);
1405 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1406 iio_unregister_interrupt_line(indio_dev, 0);
1407 iio_ring_buffer_unregister(indio_dev->ring);
1408 sca3000_unconfigure_ring(indio_dev);
1409 iio_device_unregister(indio_dev);
1418 /* These macros save on an awful lot of repeated code */
1419 #define SCA3000_VARIANT_PROBE(_name) \
1420 static int __devinit \
1421 sca3000_##_name##_probe(struct spi_device *spi) \
1423 return __sca3000_probe(spi, _name); \
1426 #define SCA3000_VARIANT_SPI_DRIVER(_name) \
1427 struct spi_driver sca3000_##_name##_driver = { \
1429 .name = "sca3000_" #_name, \
1430 .owner = THIS_MODULE, \
1432 .probe = sca3000_##_name##_probe, \
1433 .remove = __devexit_p(sca3000_remove), \
1436 SCA3000_VARIANT_PROBE(d01);
1437 static SCA3000_VARIANT_SPI_DRIVER(d01);
1439 SCA3000_VARIANT_PROBE(d03);
1440 static SCA3000_VARIANT_SPI_DRIVER(d03);
1442 SCA3000_VARIANT_PROBE(e02);
1443 static SCA3000_VARIANT_SPI_DRIVER(e02);
1445 SCA3000_VARIANT_PROBE(e04);
1446 static SCA3000_VARIANT_SPI_DRIVER(e04);
1448 SCA3000_VARIANT_PROBE(e05);
1449 static SCA3000_VARIANT_SPI_DRIVER(e05);
1451 SCA3000_VARIANT_PROBE(l01);
1452 static SCA3000_VARIANT_SPI_DRIVER(l01);
1454 static __init int sca3000_init(void)
1458 ret = spi_register_driver(&sca3000_d01_driver);
1461 ret = spi_register_driver(&sca3000_d03_driver);
1463 goto error_unreg_d01;
1464 ret = spi_register_driver(&sca3000_e02_driver);
1466 goto error_unreg_d03;
1467 ret = spi_register_driver(&sca3000_e04_driver);
1469 goto error_unreg_e02;
1470 ret = spi_register_driver(&sca3000_e05_driver);
1472 goto error_unreg_e04;
1473 ret = spi_register_driver(&sca3000_l01_driver);
1475 goto error_unreg_e05;
1480 spi_unregister_driver(&sca3000_e05_driver);
1482 spi_unregister_driver(&sca3000_e04_driver);
1484 spi_unregister_driver(&sca3000_e02_driver);
1486 spi_unregister_driver(&sca3000_d03_driver);
1488 spi_unregister_driver(&sca3000_d01_driver);
1494 static __exit void sca3000_exit(void)
1496 spi_unregister_driver(&sca3000_l01_driver);
1497 spi_unregister_driver(&sca3000_e05_driver);
1498 spi_unregister_driver(&sca3000_e04_driver);
1499 spi_unregister_driver(&sca3000_e02_driver);
1500 spi_unregister_driver(&sca3000_d03_driver);
1501 spi_unregister_driver(&sca3000_d01_driver);
1504 module_init(sca3000_init);
1505 module_exit(sca3000_exit);
1507 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1508 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1509 MODULE_LICENSE("GPL v2");