19 #include <linux/device.h>
20 #include <linux/kernel.h>
22 #include <linux/slab.h>
24 #include <linux/module.h>
38 #ifndef CONFIG_IIO_BUFFER
39 static irqreturn_t lis3l02dq_nobuffer(
int irq,
void *
private)
68 spi_message_init(&msg);
69 spi_message_add_tail(&xfer, &msg);
93 ret = spi_write(st->
us, st->tx, 2);
106 static int lis3l02dq_spi_write_reg_s16(
struct iio_dev *indio_dev,
107 u8 lower_reg_address,
119 .tx_buf = st->tx + 2,
127 st->tx[1] = value & 0xFF;
129 st->tx[3] = (value >> 8) & 0xFF;
131 spi_message_init(&
msg);
132 spi_message_add_tail(&xfers[0], &
msg);
133 spi_message_add_tail(&xfers[1], &
msg);
140 static int lis3l02dq_read_reg_s16(
struct iio_dev *indio_dev,
141 u8 lower_reg_address,
156 .tx_buf = st->tx + 2,
157 .rx_buf = st->rx + 2,
169 spi_message_init(&
msg);
170 spi_message_add_tail(&xfers[0], &
msg);
171 spi_message_add_tail(&xfers[1], &
msg);
174 dev_err(&st->
us->dev,
"problem when reading 16 bit register");
177 tempval = (
s16)(st->rx[1]) | ((
s16)(st->rx[3]) << 8);
191 static u8 lis3l02dq_axis_map[3][3] = {
203 static int lis3l02dq_read_thresh(
struct iio_dev *indio_dev,
210 static int lis3l02dq_write_thresh(
struct iio_dev *indio_dev,
215 return lis3l02dq_spi_write_reg_s16(indio_dev,
220 static int lis3l02dq_write_raw(
struct iio_dev *indio_dev,
231 if (val > 255 || val < -256)
248 static int lis3l02dq_read_raw(
struct iio_dev *indio_dev,
266 reg = lis3l02dq_axis_map
268 ret = lis3l02dq_read_reg_s16(indio_dev, reg, val);
300 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
331 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
336 ret = kstrtoul(buf, 10, &val);
345 goto error_ret_mutex;
363 goto error_ret_mutex;
373 return ret ? ret : len;
376 static int lis3l02dq_initial_setup(
struct iio_dev *indio_dev)
392 dev_err(&st->
us->dev,
"problem with setup control register 1");
400 dev_err(&st->
us->dev,
"problem with setup control register 1");
409 if (ret || (valtest != val)) {
411 "device not playing ball %d %d\n", valtest, val);
421 dev_err(&st->
us->dev,
"problem with setup control register 2");
430 dev_err(&st->
us->dev,
"problem with interrupt cfg register");
437 lis3l02dq_read_frequency,
438 lis3l02dq_write_frequency);
442 static irqreturn_t lis3l02dq_event_handler(
int irq,
void *
private)
444 struct iio_dev *indio_dev =
private;
515 #define LIS3L02DQ_INFO_MASK \
516 (IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
517 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
518 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
519 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT)
521 #define LIS3L02DQ_EVENT_MASK \
522 (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
523 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
525 #define LIS3L02DQ_CHAN(index, mod) \
530 .info_mask = LIS3L02DQ_INFO_MASK, \
532 .scan_index = index, \
538 .event_mask = LIS3L02DQ_EVENT_MASK, \
549 static int lis3l02dq_read_event_config(
struct iio_dev *indio_dev,
564 return !!(val &
mask);
601 static int lis3l02dq_write_event_config(
struct iio_dev *indio_dev,
625 currentlyset = val &
mask;
627 if (!currentlyset && state) {
630 }
else if (currentlyset && !state) {
641 control = val & 0x3f ?
656 static struct attribute *lis3l02dq_attributes[] = {
657 &iio_dev_attr_sampling_frequency.dev_attr.attr,
658 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
663 .attrs = lis3l02dq_attributes,
666 static const struct iio_info lis3l02dq_info = {
667 .read_raw = &lis3l02dq_read_raw,
668 .write_raw = &lis3l02dq_write_raw,
669 .read_event_value = &lis3l02dq_read_thresh,
670 .write_event_value = &lis3l02dq_write_thresh,
671 .write_event_config = &lis3l02dq_write_event_config,
672 .read_event_config = &lis3l02dq_read_event_config,
674 .attrs = &lis3l02dq_attribute_group,
684 if (indio_dev ==
NULL) {
688 st = iio_priv(indio_dev);
690 spi_set_drvdata(spi, indio_dev);
694 indio_dev->
name = spi->
dev.driver->name;
695 indio_dev->
dev.parent = &spi->
dev;
696 indio_dev->
info = &lis3l02dq_info;
697 indio_dev->
channels = lis3l02dq_channels;
711 goto error_unreg_buffer_funcs;
717 &lis3l02dq_event_handler,
722 goto error_uninitialize_buffer;
726 goto error_free_interrupt;
730 ret = lis3l02dq_initial_setup(indio_dev);
732 goto error_remove_trigger;
736 goto error_remove_trigger;
740 error_remove_trigger:
743 error_free_interrupt:
746 error_uninitialize_buffer:
748 error_unreg_buffer_funcs:
757 static int lis3l02dq_stop_device(
struct iio_dev *indio_dev)
768 dev_err(&st->
us->dev,
"problem with turning device off: ctrl1");
776 dev_err(&st->
us->dev,
"problem with turning device off: ctrl2");
785 struct iio_dev *indio_dev = spi_get_drvdata(spi);
791 lis3l02dq_stop_device(indio_dev);
810 .probe = lis3l02dq_probe,