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/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/spi/spi.h>
20 #include <linux/sysfs.h>
23 #include "../ring_generic.h"
28 enum sca3000_variant {
37 /* Note where option modes are not defined, the chip simply does not
39 * Other chips in the sca3000 series use i2c and are not included here.
41 * Some of these devices are only listed in the family data sheet and
42 * do not actually appear to be available.
44 static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
46 .name = "sca3000-d01",
48 .measurement_mode_freq = 250,
49 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
50 .option_mode_1_freq = 250,
52 /* No data sheet available - may be the same as the 3100-d03?*/
53 .name = "sca3000-d03",
56 .name = "sca3000-e02",
57 .measurement_mode_freq = 125,
58 .option_mode_1 = SCA3000_OP_MODE_NARROW,
59 .option_mode_1_freq = 63,
61 .name = "sca3000-e04",
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,
68 .name = "sca3000-e05",
69 .measurement_mode_freq = 200,
70 .option_mode_1 = SCA3000_OP_MODE_NARROW,
71 .option_mode_1_freq = 50,
72 .option_mode_2 = SCA3000_OP_MODE_WIDE,
73 .option_mode_2_freq = 400,
75 /* No data sheet available.
76 * Frequencies are unknown.
78 .name = "sca3000-l01",
80 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
85 int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
87 struct spi_transfer xfer = {
93 struct spi_message msg;
95 st->tx[0] = SCA3000_WRITE_REG(address);
97 spi_message_init(&msg);
98 spi_message_add_tail(&xfer, &msg);
100 return spi_sync(st->us, &msg);
103 int sca3000_read_data(struct sca3000_state *st,
104 uint8_t reg_address_high,
109 struct spi_message msg;
110 struct spi_transfer xfer = {
117 *rx_p = kmalloc(len + 1, GFP_KERNEL);
123 st->tx[0] = SCA3000_READ_REG(reg_address_high);
124 spi_message_init(&msg);
125 spi_message_add_tail(&xfer, &msg);
127 ret = spi_sync(st->us, &msg);
130 dev_err(get_device(&st->us->dev), "problem reading register");
142 * sca3000_reg_lock_on() test if the ctrl register lock is on
146 static int sca3000_reg_lock_on(struct sca3000_state *st)
151 ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1);
155 ret = !(rx[1] & SCA3000_LOCKED);
162 * __sca3000_unlock_reg_lock() unlock the control registers
164 * Note the device does not appear to support doing this in a single transfer.
165 * This should only ever be used as part of ctrl reg read.
166 * Lock must be held before calling this
168 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
170 struct spi_message msg;
171 struct spi_transfer xfer[3] = {
181 .tx_buf = st->tx + 2,
186 .tx_buf = st->tx + 4,
189 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
191 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
193 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
195 spi_message_init(&msg);
196 spi_message_add_tail(&xfer[0], &msg);
197 spi_message_add_tail(&xfer[1], &msg);
198 spi_message_add_tail(&xfer[2], &msg);
200 return spi_sync(st->us, &msg);
204 * sca3000_write_ctrl_reg() write to a lock protect ctrl register
205 * @sel: selects which registers we wish to write to
206 * @val: the value to be written
208 * Certain control registers are protected against overwriting by the lock
209 * register and use a shared write address. This function allows writing of
213 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
220 ret = sca3000_reg_lock_on(st);
224 ret = __sca3000_unlock_reg_lock(st);
229 /* Set the control select register */
230 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
234 /* Write the actual value into the register */
235 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
241 /* Crucial that lock is called before calling this */
243 * sca3000_read_ctrl_reg() read from lock protected control register.
247 static int sca3000_read_ctrl_reg(struct sca3000_state *st,
253 ret = sca3000_reg_lock_on(st);
257 ret = __sca3000_unlock_reg_lock(st);
261 /* Set the control select register */
262 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
265 ret = sca3000_read_data(st, SCA3000_REG_ADDR_CTRL_DATA, rx_p, 1);
273 * sca3000_check_status() check the status register
275 * Only used for debugging purposes
277 static int sca3000_check_status(struct device *dev)
281 struct iio_dev *indio_dev = dev_get_drvdata(dev);
282 struct sca3000_state *st = indio_dev->dev_data;
284 mutex_lock(&st->lock);
285 ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1);
288 if (rx[1] & SCA3000_EEPROM_CS_ERROR)
289 dev_err(dev, "eeprom error \n");
290 if (rx[1] & SCA3000_SPI_FRAME_ERROR)
291 dev_err(dev, "Previous SPI Frame was corrupt\n");
295 mutex_unlock(&st->lock);
298 #endif /* SCA3000_DEBUG */
301 * sca3000_read_13bit_signed() sysfs interface to read 13 bit signed registers
303 * These are described as signed 12 bit on the data sheet, which appears
304 * to be a conventional 2's complement 13 bit.
306 static ssize_t sca3000_read_13bit_signed(struct device *dev,
307 struct device_attribute *attr,
312 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
314 struct iio_dev *indio_dev = dev_get_drvdata(dev);
315 struct sca3000_state *st = indio_dev->dev_data;
317 mutex_lock(&st->lock);
318 ret = sca3000_read_data(st, this_attr->address, &rx, 2);
321 val = sca3000_13bit_convert(rx[1], rx[2]);
322 len += sprintf(buf + len, "%d\n", val);
325 mutex_unlock(&st->lock);
327 return ret ? ret : len;
331 static ssize_t sca3000_show_name(struct device *dev,
332 struct device_attribute *attr,
335 struct iio_dev *dev_info = dev_get_drvdata(dev);
336 struct sca3000_state *st = dev_info->dev_data;
337 return sprintf(buf, "%s\n", st->info->name);
340 * sca3000_show_reg() - sysfs interface to read the chip revision number
342 static ssize_t sca3000_show_rev(struct device *dev,
343 struct device_attribute *attr,
347 struct iio_dev *dev_info = dev_get_drvdata(dev);
348 struct sca3000_state *st = dev_info->dev_data;
352 mutex_lock(&st->lock);
353 ret = sca3000_read_data(st, SCA3000_REG_ADDR_REVID, &rx, 1);
356 len += sprintf(buf + len,
357 "major=%d, minor=%d\n",
358 rx[1] & SCA3000_REVID_MAJOR_MASK,
359 rx[1] & SCA3000_REVID_MINOR_MASK);
363 mutex_unlock(&st->lock);
365 return ret ? ret : len;
369 * sca3000_show_available_measurement_modes() display available modes
371 * This is all read from chip specific data in the driver. Not all
372 * of the sca3000 series support modes other than normal.
375 sca3000_show_available_measurement_modes(struct device *dev,
376 struct device_attribute *attr,
379 struct iio_dev *dev_info = dev_get_drvdata(dev);
380 struct sca3000_state *st = dev_info->dev_data;
383 len += sprintf(buf + len, "0 - normal mode");
384 switch (st->info->option_mode_1) {
385 case SCA3000_OP_MODE_NARROW:
386 len += sprintf(buf + len, ", 1 - narrow mode");
388 case SCA3000_OP_MODE_BYPASS:
389 len += sprintf(buf + len, ", 1 - bypass mode");
392 switch (st->info->option_mode_2) {
393 case SCA3000_OP_MODE_WIDE:
394 len += sprintf(buf + len, ", 2 - wide mode");
397 /* always supported */
398 len += sprintf(buf + len, " 3 - motion detection \n");
404 * sca3000_show_measurmenet_mode() sysfs read of current mode
407 sca3000_show_measurement_mode(struct device *dev,
408 struct device_attribute *attr,
411 struct iio_dev *dev_info = dev_get_drvdata(dev);
412 struct sca3000_state *st = dev_info->dev_data;
416 mutex_lock(&st->lock);
417 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
420 /* mask bottom 2 bits - only ones that are relevant */
423 case SCA3000_MEAS_MODE_NORMAL:
424 len += sprintf(buf + len, "0 - normal mode\n");
426 case SCA3000_MEAS_MODE_MOT_DET:
427 len += sprintf(buf + len, "3 - motion detection\n");
429 case SCA3000_MEAS_MODE_OP_1:
430 switch (st->info->option_mode_1) {
431 case SCA3000_OP_MODE_NARROW:
432 len += sprintf(buf + len, "1 - narrow mode\n");
434 case SCA3000_OP_MODE_BYPASS:
435 len += sprintf(buf + len, "1 - bypass mode\n");
439 case SCA3000_MEAS_MODE_OP_2:
440 switch (st->info->option_mode_2) {
441 case SCA3000_OP_MODE_WIDE:
442 len += sprintf(buf + len, "2 - wide mode\n");
449 mutex_unlock(&st->lock);
451 return ret ? ret : len;
455 * sca3000_store_measurement_mode() set the current mode
458 sca3000_store_measurement_mode(struct device *dev,
459 struct device_attribute *attr,
463 struct iio_dev *dev_info = dev_get_drvdata(dev);
464 struct sca3000_state *st = dev_info->dev_data;
470 mutex_lock(&st->lock);
471 ret = strict_strtol(buf, 10, &val);
474 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
478 rx[1] |= (val & mask);
479 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, rx[1]);
482 mutex_unlock(&st->lock);
489 mutex_unlock(&st->lock);
495 /* Not even vaguely standard attributes so defined here rather than
496 * in the relevant IIO core headers
498 static IIO_DEVICE_ATTR(available_measurement_modes, S_IRUGO,
499 sca3000_show_available_measurement_modes,
502 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
503 sca3000_show_measurement_mode,
504 sca3000_store_measurement_mode,
507 /* More standard attributes */
509 static IIO_DEV_ATTR_NAME(sca3000_show_name);
510 static IIO_DEV_ATTR_REV(sca3000_show_rev);
512 static IIO_DEV_ATTR_ACCEL_X(sca3000_read_13bit_signed,
513 SCA3000_REG_ADDR_X_MSB);
514 static IIO_DEV_ATTR_ACCEL_Y(sca3000_read_13bit_signed,
515 SCA3000_REG_ADDR_Y_MSB);
516 static IIO_DEV_ATTR_ACCEL_Z(sca3000_read_13bit_signed,
517 SCA3000_REG_ADDR_Z_MSB);
521 * sca3000_read_av_freq() sysfs function to get available frequencies
523 * The later modes are only relevant to the ring buffer - and depend on current
524 * mode. Note that data sheet gives rather wide tolerances for these so integer
525 * division will give good enough answer and not all chips have them specified
528 static ssize_t sca3000_read_av_freq(struct device *dev,
529 struct device_attribute *attr,
532 struct iio_dev *indio_dev = dev_get_drvdata(dev);
533 struct sca3000_state *st = indio_dev->dev_data;
536 mutex_lock(&st->lock);
537 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
538 mutex_unlock(&st->lock);
543 case SCA3000_MEAS_MODE_NORMAL:
544 len += sprintf(buf + len, "%d %d %d\n",
545 st->info->measurement_mode_freq,
546 st->info->measurement_mode_freq/2,
547 st->info->measurement_mode_freq/4);
549 case SCA3000_MEAS_MODE_OP_1:
550 len += sprintf(buf + len, "%d %d %d\n",
551 st->info->option_mode_1_freq,
552 st->info->option_mode_1_freq/2,
553 st->info->option_mode_1_freq/4);
555 case SCA3000_MEAS_MODE_OP_2:
556 len += sprintf(buf + len, "%d %d %d\n",
557 st->info->option_mode_2_freq,
558 st->info->option_mode_2_freq/2,
559 st->info->option_mode_2_freq/4);
568 * __sca3000_get_base_frequency() obtain mode specific base frequency
572 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
573 const struct sca3000_chip_info *info,
579 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
582 switch (0x03 & rx[1]) {
583 case SCA3000_MEAS_MODE_NORMAL:
584 *base_freq = info->measurement_mode_freq;
586 case SCA3000_MEAS_MODE_OP_1:
587 *base_freq = info->option_mode_1_freq;
589 case SCA3000_MEAS_MODE_OP_2:
590 *base_freq = info->option_mode_2_freq;
599 * sca3000_read_frequency() sysfs interface to get the current frequency
601 static ssize_t sca3000_read_frequency(struct device *dev,
602 struct device_attribute *attr,
605 struct iio_dev *indio_dev = dev_get_drvdata(dev);
606 struct sca3000_state *st = indio_dev->dev_data;
607 int ret, len = 0, base_freq = 0;
609 mutex_lock(&st->lock);
610 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
613 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx);
614 mutex_unlock(&st->lock);
618 switch (rx[1]&0x03) {
621 len = sprintf(buf, "%d\n", base_freq);
624 len = sprintf(buf, "%d\n", base_freq/2);
627 len = sprintf(buf, "%d\n", base_freq/4);
633 mutex_unlock(&st->lock);
639 * sca3000_set_frequency() sysfs interface to set the current frequency
641 static ssize_t sca3000_set_frequency(struct device *dev,
642 struct device_attribute *attr,
646 struct iio_dev *indio_dev = dev_get_drvdata(dev);
647 struct sca3000_state *st = indio_dev->dev_data;
648 int ret, base_freq = 0;
652 ret = strict_strtol(buf, 10, &val);
656 mutex_lock(&st->lock);
657 /* What mode are we in? */
658 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
660 goto error_free_lock;
662 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx);
664 goto error_free_lock;
668 if (val == base_freq/2) {
669 rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_2;
670 } else if (val == base_freq/4) {
671 rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_4;
672 } else if (val != base_freq) {
674 goto error_free_lock;
676 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, rx[1]);
678 mutex_unlock(&st->lock);
680 return ret ? ret : len;
683 /* Should only really be registered if ring buffer support is compiled in.
684 * Does no harm however and doing it right would add a fair bit of complexity
686 static IIO_DEV_ATTR_AVAIL_SAMP_FREQ(sca3000_read_av_freq);
688 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
689 sca3000_read_frequency,
690 sca3000_set_frequency);
694 * sca3000_read_temp() sysfs interface to get the temperature when available
696 * The alignment of data in here is downright odd. See data sheet.
697 * Converting this into a meaningful value is left to inline functions in
698 * userspace part of header.
700 static ssize_t sca3000_read_temp(struct device *dev,
701 struct device_attribute *attr,
704 struct iio_dev *indio_dev = dev_get_drvdata(dev);
705 struct sca3000_state *st = indio_dev->dev_data;
709 ret = sca3000_read_data(st, SCA3000_REG_ADDR_TEMP_MSB, &rx, 2);
712 val = ((rx[1]&0x3F) << 3) | ((rx[2] & 0xE0) >> 5);
713 len += sprintf(buf + len, "%d\n", val);
721 static IIO_DEV_ATTR_TEMP(sca3000_read_temp);
724 * sca3000_show_thresh() sysfs query of a threshold
726 static ssize_t sca3000_show_thresh(struct device *dev,
727 struct device_attribute *attr,
730 struct iio_dev *indio_dev = dev_get_drvdata(dev);
731 struct sca3000_state *st = indio_dev->dev_data;
732 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
736 mutex_lock(&st->lock);
737 ret = sca3000_read_ctrl_reg(st,
740 mutex_unlock(&st->lock);
743 len += sprintf(buf + len, "%d\n", rx[1]);
750 * sca3000_write_thresh() sysfs control of threshold
752 static ssize_t sca3000_write_thresh(struct device *dev,
753 struct device_attribute *attr,
757 struct iio_dev *indio_dev = dev_get_drvdata(dev);
758 struct sca3000_state *st = indio_dev->dev_data;
759 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
763 ret = strict_strtol(buf, 10, &val);
766 mutex_lock(&st->lock);
767 ret = sca3000_write_ctrl_reg(st, this_attr->address, val);
768 mutex_unlock(&st->lock);
770 return ret ? ret : len;
773 static IIO_DEV_ATTR_ACCEL_THRESH_X(S_IRUGO | S_IWUSR,
775 sca3000_write_thresh,
776 SCA3000_REG_CTRL_SEL_MD_X_TH);
777 static IIO_DEV_ATTR_ACCEL_THRESH_Y(S_IRUGO | S_IWUSR,
779 sca3000_write_thresh,
780 SCA3000_REG_CTRL_SEL_MD_Y_TH);
781 static IIO_DEV_ATTR_ACCEL_THRESH_Z(S_IRUGO | S_IWUSR,
783 sca3000_write_thresh,
784 SCA3000_REG_CTRL_SEL_MD_Z_TH);
786 static struct attribute *sca3000_attributes[] = {
787 &iio_dev_attr_name.dev_attr.attr,
788 &iio_dev_attr_revision.dev_attr.attr,
789 &iio_dev_attr_accel_x.dev_attr.attr,
790 &iio_dev_attr_accel_y.dev_attr.attr,
791 &iio_dev_attr_accel_z.dev_attr.attr,
792 &iio_dev_attr_thresh_accel_x.dev_attr.attr,
793 &iio_dev_attr_thresh_accel_y.dev_attr.attr,
794 &iio_dev_attr_thresh_accel_z.dev_attr.attr,
795 &iio_dev_attr_available_measurement_modes.dev_attr.attr,
796 &iio_dev_attr_measurement_mode.dev_attr.attr,
797 &iio_dev_attr_available_sampling_frequency.dev_attr.attr,
798 &iio_dev_attr_sampling_frequency.dev_attr.attr,
802 static struct attribute *sca3000_attributes_with_temp[] = {
803 &iio_dev_attr_name.dev_attr.attr,
804 &iio_dev_attr_revision.dev_attr.attr,
805 &iio_dev_attr_accel_x.dev_attr.attr,
806 &iio_dev_attr_accel_y.dev_attr.attr,
807 &iio_dev_attr_accel_z.dev_attr.attr,
808 &iio_dev_attr_thresh_accel_x.dev_attr.attr,
809 &iio_dev_attr_thresh_accel_y.dev_attr.attr,
810 &iio_dev_attr_thresh_accel_z.dev_attr.attr,
811 &iio_dev_attr_available_measurement_modes.dev_attr.attr,
812 &iio_dev_attr_measurement_mode.dev_attr.attr,
813 &iio_dev_attr_available_sampling_frequency.dev_attr.attr,
814 &iio_dev_attr_sampling_frequency.dev_attr.attr,
815 /* Only present if temp sensor is */
816 &iio_dev_attr_temp.dev_attr.attr,
820 static const struct attribute_group sca3000_attribute_group = {
821 .attrs = sca3000_attributes,
824 static const struct attribute_group sca3000_attribute_group_with_temp = {
825 .attrs = sca3000_attributes_with_temp,
828 /* RING RELATED interrupt handler */
829 /* depending on event, push to the ring buffer event chrdev or the event one */
832 * sca3000_interrupt_handler_bh() - handling ring and non ring events
834 * This function is complicated by the fact that the devices can signify ring
835 * and non ring events via the same interrupt line and they can only
836 * be distinguished via a read of the relevant status register.
838 static void sca3000_interrupt_handler_bh(struct work_struct *work_s)
840 struct sca3000_state *st
841 = container_of(work_s, struct sca3000_state,
842 interrupt_handler_ws);
846 /* Could lead if badly timed to an extra read of status reg,
847 * but ensures no interrupt is missed.
849 enable_irq(st->us->irq);
850 mutex_lock(&st->lock);
851 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS,
853 mutex_unlock(&st->lock);
857 sca3000_ring_int_process(rx[1], st->indio_dev->ring);
859 if (rx[1] & SCA3000_INT_STATUS_FREE_FALL)
860 iio_push_event(st->indio_dev, 0,
861 IIO_EVENT_CODE_FREE_FALL,
864 if (rx[1] & SCA3000_INT_STATUS_Y_TRIGGER)
865 iio_push_event(st->indio_dev, 0,
866 IIO_EVENT_CODE_ACCEL_Y_HIGH,
869 if (rx[1] & SCA3000_INT_STATUS_X_TRIGGER)
870 iio_push_event(st->indio_dev, 0,
871 IIO_EVENT_CODE_ACCEL_X_HIGH,
874 if (rx[1] & SCA3000_INT_STATUS_Z_TRIGGER)
875 iio_push_event(st->indio_dev, 0,
876 IIO_EVENT_CODE_ACCEL_Z_HIGH,
885 * sca3000_handler_th() handles all interrupt events from device
887 * These devices deploy unified interrupt status registers meaning
888 * all interrupts must be handled together
890 static int sca3000_handler_th(struct iio_dev *dev_info,
895 struct sca3000_state *st = dev_info->dev_data;
897 st->last_timestamp = timestamp;
898 schedule_work(&st->interrupt_handler_ws);
904 * sca3000_query_mo_det() is motion detection enabled for this axis
906 * First queries if motion detection is enabled and then if this axis is
909 static ssize_t sca3000_query_mo_det(struct device *dev,
910 struct device_attribute *attr,
913 struct iio_dev *indio_dev = dev_get_drvdata(dev);
914 struct sca3000_state *st = indio_dev->dev_data;
915 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
918 u8 protect_mask = 0x03;
920 /* read current value of mode register */
921 mutex_lock(&st->lock);
922 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
926 if ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
927 len += sprintf(buf + len, "0\n");
930 ret = sca3000_read_ctrl_reg(st,
931 SCA3000_REG_CTRL_SEL_MD_CTRL,
935 /* only supporting logical or's for now */
936 len += sprintf(buf + len, "%d\n",
937 (rx[1] & this_attr->mask) ? 1 : 0);
941 mutex_unlock(&st->lock);
943 return ret ? ret : len;
946 * sca3000_query_free_fall_mode() is free fall mode enabled
948 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
949 struct device_attribute *attr,
954 struct iio_dev *indio_dev = dev_get_drvdata(dev);
955 struct sca3000_state *st = indio_dev->dev_data;
957 mutex_lock(&st->lock);
958 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
959 mutex_unlock(&st->lock);
962 len = sprintf(buf, "%d\n",
963 !!(rx[1] & SCA3000_FREE_FALL_DETECT));
969 * sca3000_query_ring_int() is the hardware ring status interrupt enabled
971 static ssize_t sca3000_query_ring_int(struct device *dev,
972 struct device_attribute *attr,
975 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
978 struct iio_dev *indio_dev = dev_get_drvdata(dev);
979 struct sca3000_state *st = indio_dev->dev_data;
980 mutex_lock(&st->lock);
981 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
982 mutex_unlock(&st->lock);
985 len = sprintf(buf, "%d\n", (rx[1] & this_attr->mask) ? 1 : 0);
991 * sca3000_set_ring_int() set state of ring status interrupt
993 static ssize_t sca3000_set_ring_int(struct device *dev,
994 struct device_attribute *attr,
998 struct iio_dev *indio_dev = dev_get_drvdata(dev);
999 struct sca3000_state *st = indio_dev->dev_data;
1000 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1006 mutex_lock(&st->lock);
1007 ret = strict_strtol(buf, 10, &val);
1010 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1014 ret = sca3000_write_reg(st,
1015 SCA3000_REG_ADDR_INT_MASK,
1016 rx[1] | this_attr->mask);
1018 ret = sca3000_write_reg(st,
1019 SCA3000_REG_ADDR_INT_MASK,
1020 rx[1] & ~this_attr->mask);
1023 mutex_unlock(&st->lock);
1025 return ret ? ret : len;
1029 * sca3000_set_free_fall_mode() simple on off control for free fall int
1031 * In these chips the free fall detector should send an interrupt if
1032 * the device falls more than 25cm. This has not been tested due
1033 * to fragile wiring.
1036 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
1037 struct device_attribute *attr,
1041 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1042 struct sca3000_state *st = indio_dev->dev_data;
1046 u8 protect_mask = SCA3000_FREE_FALL_DETECT;
1048 mutex_lock(&st->lock);
1049 ret = strict_strtol(buf, 10, &val);
1053 /* read current value of mode register */
1054 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
1058 /*if off and should be on*/
1059 if (val && !(rx[1] & protect_mask))
1060 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1061 (rx[1] | SCA3000_FREE_FALL_DETECT));
1062 /* if on and should be off */
1063 else if (!val && (rx[1]&protect_mask))
1064 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1065 (rx[1] & ~protect_mask));
1069 mutex_unlock(&st->lock);
1071 return ret ? ret : len;
1075 * sca3000_set_mo_det() simple on off control for motion detector
1077 * This is a per axis control, but enabling any will result in the
1078 * motion detector unit being enabled.
1079 * N.B. enabling motion detector stops normal data acquisition.
1080 * There is a complexity in knowing which mode to return to when
1081 * this mode is disabled. Currently normal mode is assumed.
1083 static ssize_t sca3000_set_mo_det(struct device *dev,
1084 struct device_attribute *attr,
1088 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1089 struct sca3000_state *st = indio_dev->dev_data;
1090 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1094 u8 protect_mask = 0x03;
1095 ret = strict_strtol(buf, 10, &val);
1099 mutex_lock(&st->lock);
1100 /* First read the motion detector config to find out if
1102 ret = sca3000_read_ctrl_reg(st,
1103 SCA3000_REG_CTRL_SEL_MD_CTRL,
1107 /* Off and should be on */
1108 if (val && !(rx[1] & this_attr->mask)) {
1109 ret = sca3000_write_ctrl_reg(st,
1110 SCA3000_REG_CTRL_SEL_MD_CTRL,
1111 rx[1] | this_attr->mask);
1113 goto exit_point_free_rx;
1114 st->mo_det_use_count++;
1115 } else if (!val && (rx[1]&this_attr->mask)) {
1116 ret = sca3000_write_ctrl_reg(st,
1117 SCA3000_REG_CTRL_SEL_MD_CTRL,
1118 rx[1] & ~(this_attr->mask));
1120 goto exit_point_free_rx;
1121 st->mo_det_use_count--;
1122 } else /* relies on clean state for device on boot */
1123 goto exit_point_free_rx;
1125 /* read current value of mode register */
1126 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
1129 /*if off and should be on*/
1130 if ((st->mo_det_use_count)
1131 && ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1132 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1133 (rx[1] & ~protect_mask)
1134 | SCA3000_MEAS_MODE_MOT_DET);
1135 /* if on and should be off */
1136 else if (!(st->mo_det_use_count)
1137 && ((rx[1]&protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1138 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1139 (rx[1] & ~protect_mask));
1143 mutex_unlock(&st->lock);
1145 return ret ? ret : len;
1148 /* Shared event handler for all events as single event status register */
1149 IIO_EVENT_SH(all, &sca3000_handler_th);
1151 /* Free fall detector related event attribute */
1152 IIO_EVENT_ATTR_FREE_FALL_DETECT_SH(iio_event_all,
1153 sca3000_query_free_fall_mode,
1154 sca3000_set_free_fall_mode,
1157 /* Motion detector related event attributes */
1158 IIO_EVENT_ATTR_ACCEL_X_HIGH_SH(iio_event_all,
1159 sca3000_query_mo_det,
1161 SCA3000_MD_CTRL_OR_X);
1163 IIO_EVENT_ATTR_ACCEL_Y_HIGH_SH(iio_event_all,
1164 sca3000_query_mo_det,
1166 SCA3000_MD_CTRL_OR_Y);
1168 IIO_EVENT_ATTR_ACCEL_Z_HIGH_SH(iio_event_all,
1169 sca3000_query_mo_det,
1171 SCA3000_MD_CTRL_OR_Z);
1173 /* Hardware ring buffer related event attributes */
1174 IIO_EVENT_ATTR_RING_50_FULL_SH(iio_event_all,
1175 sca3000_query_ring_int,
1176 sca3000_set_ring_int,
1177 SCA3000_INT_MASK_RING_HALF);
1179 IIO_EVENT_ATTR_RING_75_FULL_SH(iio_event_all,
1180 sca3000_query_ring_int,
1181 sca3000_set_ring_int,
1182 SCA3000_INT_MASK_RING_THREE_QUARTER);
1184 static struct attribute *sca3000_event_attributes[] = {
1185 &iio_event_attr_free_fall.dev_attr.attr,
1186 &iio_event_attr_accel_x_high.dev_attr.attr,
1187 &iio_event_attr_accel_y_high.dev_attr.attr,
1188 &iio_event_attr_accel_z_high.dev_attr.attr,
1189 &iio_event_attr_ring_50_full.dev_attr.attr,
1190 &iio_event_attr_ring_75_full.dev_attr.attr,
1194 static struct attribute_group sca3000_event_attribute_group = {
1195 .attrs = sca3000_event_attributes,
1199 * sca3000_clean_setup() get the device into a predictable state
1201 * Devices use flash memory to store many of the register values
1202 * and hence can come up in somewhat unpredictable states.
1203 * Hence reset everything on driver load.
1205 static int sca3000_clean_setup(struct sca3000_state *st)
1210 mutex_lock(&st->lock);
1211 /* Ensure all interrupts have been acknowledged */
1212 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS, &rx, 1);
1217 /* Turn off all motion detection channels */
1218 ret = sca3000_read_ctrl_reg(st,
1219 SCA3000_REG_CTRL_SEL_MD_CTRL,
1223 ret = sca3000_write_ctrl_reg(st,
1224 SCA3000_REG_CTRL_SEL_MD_CTRL,
1225 rx[1] & SCA3000_MD_CTRL_PROT_MASK);
1230 /* Disable ring buffer */
1231 sca3000_read_ctrl_reg(st,
1232 SCA3000_REG_CTRL_SEL_OUT_CTRL,
1234 /* Frequency of ring buffer sampling deliberately restricted to make
1235 * debugging easier - add control of this later */
1236 ret = sca3000_write_ctrl_reg(st,
1237 SCA3000_REG_CTRL_SEL_OUT_CTRL,
1238 (rx[1] & SCA3000_OUT_CTRL_PROT_MASK)
1239 | SCA3000_OUT_CTRL_BUF_X_EN
1240 | SCA3000_OUT_CTRL_BUF_Y_EN
1241 | SCA3000_OUT_CTRL_BUF_Z_EN
1242 | SCA3000_OUT_CTRL_BUF_DIV_4);
1247 /* Enable interrupts, relevant to mode and set up as active low */
1248 ret = sca3000_read_data(st,
1249 SCA3000_REG_ADDR_INT_MASK,
1253 ret = sca3000_write_reg(st,
1254 SCA3000_REG_ADDR_INT_MASK,
1255 (rx[1] & SCA3000_INT_MASK_PROT_MASK)
1256 | SCA3000_INT_MASK_ACTIVE_LOW);
1260 /* Select normal measurement mode, free fall off, ring off */
1261 /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1262 * as that occurs in one of the example on the datasheet */
1263 ret = sca3000_read_data(st,
1264 SCA3000_REG_ADDR_MODE,
1268 ret = sca3000_write_reg(st,
1269 SCA3000_REG_ADDR_MODE,
1270 (rx[1] & SCA3000_MODE_PROT_MASK));
1275 mutex_unlock(&st->lock);
1279 static int __devinit __sca3000_probe(struct spi_device *spi,
1280 enum sca3000_variant variant)
1282 int ret, regdone = 0;
1283 struct sca3000_state *st;
1285 st = kzalloc(sizeof(struct sca3000_state), GFP_KERNEL);
1290 spi_set_drvdata(spi, st);
1292 st->tx = kmalloc(sizeof(*st->tx)*6, GFP_KERNEL);
1293 if (st->tx == NULL) {
1295 goto error_clear_st;
1297 st->rx = kmalloc(sizeof(*st->rx)*3, GFP_KERNEL);
1298 if (st->rx == NULL) {
1303 mutex_init(&st->lock);
1304 st->info = &sca3000_spi_chip_info_tbl[variant];
1306 st->indio_dev = iio_allocate_device();
1307 if (st->indio_dev == NULL) {
1312 st->indio_dev->dev.parent = &spi->dev;
1313 st->indio_dev->num_interrupt_lines = 1;
1314 st->indio_dev->event_attrs = &sca3000_event_attribute_group;
1315 if (st->info->temp_output)
1316 st->indio_dev->attrs = &sca3000_attribute_group_with_temp;
1318 st->indio_dev->attrs = &sca3000_attribute_group;
1319 st->indio_dev->dev_data = (void *)(st);
1320 st->indio_dev->modes = INDIO_DIRECT_MODE;
1322 sca3000_configure_ring(st->indio_dev);
1324 ret = iio_device_register(st->indio_dev);
1326 goto error_free_dev;
1328 ret = iio_ring_buffer_register(st->indio_dev->ring);
1330 goto error_unregister_dev;
1331 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
1332 INIT_WORK(&st->interrupt_handler_ws,
1333 sca3000_interrupt_handler_bh);
1334 ret = iio_register_interrupt_line(spi->irq,
1337 IRQF_TRIGGER_FALLING,
1340 goto error_unregister_ring;
1342 * Probably a common situation. All interrupts need an ack
1343 * and there is only one handler so the complicated list system
1344 * is overkill. At very least a simpler registration method
1345 * might be worthwhile.
1347 iio_add_event_to_list(iio_event_attr_accel_z_high.listel,
1349 ->interrupts[0]->ev_list);
1351 sca3000_register_ring_funcs(st->indio_dev);
1352 ret = sca3000_clean_setup(st);
1354 goto error_unregister_interrupt_line;
1357 error_unregister_interrupt_line:
1358 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1359 iio_unregister_interrupt_line(st->indio_dev, 0);
1360 error_unregister_ring:
1361 iio_ring_buffer_unregister(st->indio_dev->ring);
1362 error_unregister_dev:
1365 iio_device_unregister(st->indio_dev);
1367 iio_free_device(st->indio_dev);
1378 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1383 mutex_lock(&st->lock);
1384 ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1387 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1388 (rx[1] & ~(SCA3000_INT_MASK_RING_THREE_QUARTER
1389 | SCA3000_INT_MASK_RING_HALF
1390 | SCA3000_INT_MASK_ALL_INTS)));
1397 static int sca3000_remove(struct spi_device *spi)
1399 struct sca3000_state *st = spi_get_drvdata(spi);
1400 struct iio_dev *indio_dev = st->indio_dev;
1402 /* Must ensure no interrupts can be generated after this!*/
1403 ret = sca3000_stop_all_interrupts(st);
1406 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1407 iio_unregister_interrupt_line(indio_dev, 0);
1408 iio_ring_buffer_unregister(indio_dev->ring);
1409 sca3000_unconfigure_ring(indio_dev);
1410 iio_device_unregister(indio_dev);
1419 /* These macros save on an awful lot of repeated code */
1420 #define SCA3000_VARIANT_PROBE(_name) \
1421 static int __devinit \
1422 sca3000_##_name##_probe(struct spi_device *spi) \
1424 return __sca3000_probe(spi, _name); \
1427 #define SCA3000_VARIANT_SPI_DRIVER(_name) \
1428 struct spi_driver sca3000_##_name##_driver = { \
1430 .name = "sca3000_" #_name, \
1431 .owner = THIS_MODULE, \
1433 .probe = sca3000_##_name##_probe, \
1434 .remove = __devexit_p(sca3000_remove), \
1437 SCA3000_VARIANT_PROBE(d01);
1438 static SCA3000_VARIANT_SPI_DRIVER(d01);
1440 SCA3000_VARIANT_PROBE(d03);
1441 static SCA3000_VARIANT_SPI_DRIVER(d03);
1443 SCA3000_VARIANT_PROBE(e02);
1444 static SCA3000_VARIANT_SPI_DRIVER(e02);
1446 SCA3000_VARIANT_PROBE(e04);
1447 static SCA3000_VARIANT_SPI_DRIVER(e04);
1449 SCA3000_VARIANT_PROBE(e05);
1450 static SCA3000_VARIANT_SPI_DRIVER(e05);
1452 SCA3000_VARIANT_PROBE(l01);
1453 static SCA3000_VARIANT_SPI_DRIVER(l01);
1455 static __init int sca3000_init(void)
1459 ret = spi_register_driver(&sca3000_d01_driver);
1462 ret = spi_register_driver(&sca3000_d03_driver);
1464 goto error_unreg_d01;
1465 ret = spi_register_driver(&sca3000_e02_driver);
1467 goto error_unreg_d03;
1468 ret = spi_register_driver(&sca3000_e04_driver);
1470 goto error_unreg_e02;
1471 ret = spi_register_driver(&sca3000_e05_driver);
1473 goto error_unreg_e04;
1474 ret = spi_register_driver(&sca3000_l01_driver);
1476 goto error_unreg_e05;
1481 spi_unregister_driver(&sca3000_e05_driver);
1483 spi_unregister_driver(&sca3000_e04_driver);
1485 spi_unregister_driver(&sca3000_e02_driver);
1487 spi_unregister_driver(&sca3000_d03_driver);
1489 spi_unregister_driver(&sca3000_d01_driver);
1495 static __exit void sca3000_exit(void)
1497 spi_unregister_driver(&sca3000_l01_driver);
1498 spi_unregister_driver(&sca3000_e05_driver);
1499 spi_unregister_driver(&sca3000_e04_driver);
1500 spi_unregister_driver(&sca3000_e02_driver);
1501 spi_unregister_driver(&sca3000_d03_driver);
1502 spi_unregister_driver(&sca3000_d01_driver);
1505 module_init(sca3000_init);
1506 module_exit(sca3000_exit);
1508 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1509 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1510 MODULE_LICENSE("GPL v2");