15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/kernel.h>
20 #include <linux/module.h>
46 .measurement_mode_freq = 250,
48 .option_mode_1_freq = 250,
49 .mot_det_mult_xz = {50, 100, 200, 350, 650, 1300},
50 .mot_det_mult_y = {50, 100, 150, 250, 450, 850, 1750},
54 .measurement_mode_freq = 125,
56 .option_mode_1_freq = 63,
57 .mot_det_mult_xz = {100, 150, 300, 550, 1050, 2050},
58 .mot_det_mult_y = {50, 100, 200, 350, 700, 1350, 2700},
62 .measurement_mode_freq = 100,
64 .option_mode_1_freq = 50,
66 .option_mode_2_freq = 400,
67 .mot_det_mult_xz = {200, 300, 600, 1100, 2100, 4100},
68 .mot_det_mult_y = {100, 200, 400, 7000, 1400, 2700, 54000},
72 .measurement_mode_freq = 200,
74 .option_mode_1_freq = 50,
76 .option_mode_2_freq = 400,
77 .mot_det_mult_xz = {600, 900, 1700, 3200, 6100, 11900},
78 .mot_det_mult_y = {300, 600, 1200, 2000, 4100, 7800, 15600},
86 return spi_write(st->
us, st->tx, 2);
104 spi_message_init(&msg);
105 spi_message_add_tail(&xfer[0], &msg);
106 spi_message_add_tail(&xfer[1], &msg);
134 static int __sca3000_unlock_reg_lock(
struct sca3000_state *st)
145 .tx_buf = st->tx + 2,
148 .tx_buf = st->tx + 4,
157 spi_message_init(&
msg);
158 spi_message_add_tail(&xfer[0], &
msg);
159 spi_message_add_tail(&xfer[1], &
msg);
160 spi_message_add_tail(&xfer[2], &
msg);
182 ret = sca3000_reg_lock_on(st);
186 ret = __sca3000_unlock_reg_lock(st);
214 ret = sca3000_reg_lock_on(st);
218 ret = __sca3000_unlock_reg_lock(st);
241 static int sca3000_check_status(
struct device *
dev)
244 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
252 dev_err(dev,
"eeprom error\n");
254 dev_err(dev,
"Previous SPI Frame was corrupt\n");
271 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
279 "major=%d, minor=%d\n",
285 return ret ? ret : len;
295 sca3000_show_available_measurement_modes(
struct device *dev,
299 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
303 len +=
sprintf(buf + len,
"0 - normal mode");
304 switch (st->
info->option_mode_1) {
306 len +=
sprintf(buf + len,
", 1 - narrow mode");
309 len +=
sprintf(buf + len,
", 1 - bypass mode");
312 switch (st->
info->option_mode_2) {
314 len +=
sprintf(buf + len,
", 2 - wide mode");
318 len +=
sprintf(buf + len,
" 3 - motion detection\n");
327 sca3000_show_measurement_mode(
struct device *dev,
331 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
343 len +=
sprintf(buf + len,
"0 - normal mode\n");
346 len +=
sprintf(buf + len,
"3 - motion detection\n");
349 switch (st->
info->option_mode_1) {
351 len +=
sprintf(buf + len,
"1 - narrow mode\n");
354 len +=
sprintf(buf + len,
"1 - bypass mode\n");
359 switch (st->
info->option_mode_2) {
361 len +=
sprintf(buf + len,
"2 - wide mode\n");
370 return ret ? ret : len;
377 sca3000_store_measurement_mode(
struct device *dev,
382 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
400 st->rx[0] |= (val &
mask);
419 sca3000_show_available_measurement_modes,
423 sca3000_show_measurement_mode,
424 sca3000_store_measurement_mode,
431 #define SCA3000_INFO_MASK \
432 IIO_CHAN_INFO_RAW_SEPARATE_BIT | IIO_CHAN_INFO_SCALE_SHARED_BIT
433 #define SCA3000_EVENT_MASK \
434 (IIO_EV_BIT(IIO_EV_TYPE_MAG, IIO_EV_DIR_RISING))
436 #define SCA3000_CHAN(index, mod) \
441 .info_mask = SCA3000_INFO_MASK, \
443 .scan_index = index, \
450 .event_mask = SCA3000_EVENT_MASK, \
459 static u8 sca3000_addresses[3][3] = {
468 static int sca3000_read_raw(
struct iio_dev *indio_dev,
485 address = sca3000_addresses[chan->
address][0];
492 *val = ((*val) << (
sizeof(*val)*8 - 13)) >>
493 (
sizeof(*val)*8 - 13);
499 *val2 = st->
info->scale;
520 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
531 switch (val & 0x03) {
533 len +=
sprintf(buf + len,
"%d %d %d\n",
534 st->
info->measurement_mode_freq,
535 st->
info->measurement_mode_freq/2,
536 st->
info->measurement_mode_freq/4);
539 len +=
sprintf(buf + len,
"%d %d %d\n",
540 st->
info->option_mode_1_freq,
541 st->
info->option_mode_1_freq/2,
542 st->
info->option_mode_1_freq/4);
545 len +=
sprintf(buf + len,
"%d %d %d\n",
546 st->
info->option_mode_2_freq,
547 st->
info->option_mode_2_freq/2,
548 st->
info->option_mode_2_freq/4);
560 static inline int __sca3000_get_base_freq(
struct sca3000_state *st,
569 switch (0x03 & st->rx[0]) {
591 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
593 int ret, len = 0, base_freq = 0,
val;
596 ret = __sca3000_get_base_freq(st, st->
info, &base_freq);
605 switch (val & 0x03) {
608 len =
sprintf(buf,
"%d\n", base_freq);
611 len =
sprintf(buf,
"%d\n", base_freq/2);
614 len =
sprintf(buf,
"%d\n", base_freq/4);
633 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
635 int ret, base_freq = 0;
645 ret = __sca3000_get_base_freq(st, st->
info, &base_freq);
647 goto error_free_lock;
651 goto error_free_lock;
656 if (val == base_freq/2) {
658 }
else if (val == base_freq/4) {
660 }
else if (val != base_freq) {
662 goto error_free_lock;
669 return ret ? ret : len;
678 sca3000_read_frequency,
679 sca3000_set_frequency);
693 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
700 val = ((st->rx[0] & 0x3F) << 3) | ((st->rx[1] & 0xE0) >> 5);
702 return sprintf(buf,
"%d\n", val);
715 static int sca3000_read_thresh(
struct iio_dev *indio_dev,
723 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]);
731 *val += st->
info->mot_det_mult_y[
i];
735 *val += st->
info->mot_det_mult_xz[
i];
743 static int sca3000_write_thresh(
struct iio_dev *indio_dev,
756 if (val >= st->
info->mot_det_mult_y[--i]) {
757 nonlinear |= (1 <<
i);
758 val -= st->
info->mot_det_mult_y[
i];
763 if (val >= st->
info->mot_det_mult_xz[--i]) {
764 nonlinear |= (1 <<
i);
765 val -= st->
info->mot_det_mult_xz[
i];
770 ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear);
776 static struct attribute *sca3000_attributes[] = {
777 &iio_dev_attr_revision.dev_attr.attr,
778 &iio_dev_attr_measurement_mode_available.dev_attr.attr,
779 &iio_dev_attr_measurement_mode.dev_attr.attr,
780 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
781 &iio_dev_attr_sampling_frequency.dev_attr.attr,
785 static struct attribute *sca3000_attributes_with_temp[] = {
786 &iio_dev_attr_revision.dev_attr.attr,
787 &iio_dev_attr_measurement_mode_available.dev_attr.attr,
788 &iio_dev_attr_measurement_mode.dev_attr.attr,
789 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
790 &iio_dev_attr_sampling_frequency.dev_attr.attr,
792 &iio_dev_attr_in_temp_raw.dev_attr.attr,
793 &iio_const_attr_in_temp_offset.dev_attr.attr,
794 &iio_const_attr_in_temp_scale.dev_attr.attr,
799 .attrs = sca3000_attributes,
802 static const struct attribute_group sca3000_attribute_group_with_temp = {
803 .attrs = sca3000_attributes_with_temp,
816 static irqreturn_t sca3000_event_handler(
int irq,
void *
private)
818 struct iio_dev *indio_dev =
private;
878 static int sca3000_read_event_config(
struct iio_dev *indio_dev,
883 u8 protect_mask = 0x03;
899 ret = !!(ret & sca3000_addresses[num][2]);
909 static ssize_t sca3000_query_free_fall_mode(
struct device *dev,
914 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
937 static ssize_t sca3000_set_free_fall_mode(
struct device *dev,
942 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
959 if (val && !(st->rx[0] & protect_mask))
963 else if (!val && (st->rx[0] & protect_mask))
965 (st->rx[0] & ~protect_mask));
969 return ret ? ret : len;
981 static int sca3000_write_event_config(
struct iio_dev *indio_dev,
987 u8 protect_mask = 0x03;
998 if (state && !(ctrlval & sca3000_addresses[num][2])) {
999 ret = sca3000_write_ctrl_reg(st,
1002 sca3000_addresses[num][2]);
1006 }
else if (!state && (ctrlval & sca3000_addresses[num][2])) {
1007 ret = sca3000_write_ctrl_reg(st,
1010 ~(sca3000_addresses[num][2]));
1024 (st->rx[0] & ~protect_mask)
1030 (st->rx[0] & ~protect_mask));
1039 in_accel_x&
y&z_mag_falling_en,
1041 sca3000_query_free_fall_mode,
1042 sca3000_set_free_fall_mode,
1046 in_accel_x&
y&z_mag_falling_period,
1049 static struct attribute *sca3000_event_attributes[] = {
1050 &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1051 &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1056 .attrs = sca3000_event_attributes,
1121 static const struct iio_info sca3000_info = {
1122 .attrs = &sca3000_attribute_group,
1123 .read_raw = &sca3000_read_raw,
1124 .event_attrs = &sca3000_event_attribute_group,
1125 .read_event_value = &sca3000_read_thresh,
1126 .write_event_value = &sca3000_write_thresh,
1127 .read_event_config = &sca3000_read_event_config,
1128 .write_event_config = &sca3000_write_event_config,
1132 static const struct iio_info sca3000_info_with_temp = {
1133 .attrs = &sca3000_attribute_group_with_temp,
1134 .read_raw = &sca3000_read_raw,
1135 .read_event_value = &sca3000_read_thresh,
1136 .write_event_value = &sca3000_write_thresh,
1137 .read_event_config = &sca3000_read_event_config,
1138 .write_event_config = &sca3000_write_event_config,
1149 if (indio_dev ==
NULL) {
1154 st = iio_priv(indio_dev);
1155 spi_set_drvdata(spi, indio_dev);
1161 indio_dev->
dev.parent = &spi->
dev;
1163 if (st->
info->temp_output)
1164 indio_dev->
info = &sca3000_info_with_temp;
1166 indio_dev->
info = &sca3000_info;
1167 indio_dev->
channels = sca3000_channels;
1175 goto error_free_dev;
1181 goto error_unregister_dev;
1191 &sca3000_event_handler,
1196 goto error_unregister_ring;
1199 ret = sca3000_clean_setup(st);
1201 goto error_free_irq;
1207 error_unregister_ring:
1209 error_unregister_dev:
1218 static int sca3000_stop_all_interrupts(
struct sca3000_state *st)
1238 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1242 sca3000_stop_all_interrupts(st);
1254 {
"sca3000_d01",
d01},
1255 {
"sca3000_e02",
e02},
1256 {
"sca3000_e04",
e04},
1257 {
"sca3000_e05",
e05},
1267 .probe = sca3000_probe,
1269 .id_table = sca3000_id,