1 // SPDX-License-Identifier: GPL-2.0-only
3 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
5 * Copyright (c) 2014, Intel Corporation.
7 * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/acpi.h>
13 #include <linux/interrupt.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/events.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/iio/trigger_consumer.h>
24 #define KMX61_DRV_NAME "kmx61"
25 #define KMX61_IRQ_NAME "kmx61_event"
27 #define KMX61_REG_WHO_AM_I 0x00
28 #define KMX61_REG_INS1 0x01
29 #define KMX61_REG_INS2 0x02
32 * three 16-bit accelerometer output registers for X/Y/Z axis
33 * we use only XOUT_L as a base register, all other addresses
34 * can be obtained by applying an offset and are provided here
37 #define KMX61_ACC_XOUT_L 0x0A
38 #define KMX61_ACC_XOUT_H 0x0B
39 #define KMX61_ACC_YOUT_L 0x0C
40 #define KMX61_ACC_YOUT_H 0x0D
41 #define KMX61_ACC_ZOUT_L 0x0E
42 #define KMX61_ACC_ZOUT_H 0x0F
45 * one 16-bit temperature output register
47 #define KMX61_TEMP_L 0x10
48 #define KMX61_TEMP_H 0x11
51 * three 16-bit magnetometer output registers for X/Y/Z axis
53 #define KMX61_MAG_XOUT_L 0x12
54 #define KMX61_MAG_XOUT_H 0x13
55 #define KMX61_MAG_YOUT_L 0x14
56 #define KMX61_MAG_YOUT_H 0x15
57 #define KMX61_MAG_ZOUT_L 0x16
58 #define KMX61_MAG_ZOUT_H 0x17
60 #define KMX61_REG_INL 0x28
61 #define KMX61_REG_STBY 0x29
62 #define KMX61_REG_CTRL1 0x2A
63 #define KMX61_REG_CTRL2 0x2B
64 #define KMX61_REG_ODCNTL 0x2C
65 #define KMX61_REG_INC1 0x2D
67 #define KMX61_REG_WUF_THRESH 0x3D
68 #define KMX61_REG_WUF_TIMER 0x3E
70 #define KMX61_ACC_STBY_BIT BIT(0)
71 #define KMX61_MAG_STBY_BIT BIT(1)
72 #define KMX61_ACT_STBY_BIT BIT(7)
74 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
76 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
78 #define KMX61_REG_INS2_BIT_ZP BIT(0)
79 #define KMX61_REG_INS2_BIT_ZN BIT(1)
80 #define KMX61_REG_INS2_BIT_YP BIT(2)
81 #define KMX61_REG_INS2_BIT_YN BIT(3)
82 #define KMX61_REG_INS2_BIT_XP BIT(4)
83 #define KMX61_REG_INS2_BIT_XN BIT(5)
85 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
87 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
88 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
89 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
90 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
92 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
93 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
94 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
95 #define KMX61_REG_INC1_BIT_IEN BIT(5)
97 #define KMX61_ACC_ODR_SHIFT 0
98 #define KMX61_MAG_ODR_SHIFT 4
99 #define KMX61_ACC_ODR_MASK 0x0F
100 #define KMX61_MAG_ODR_MASK 0xF0
102 #define KMX61_OWUF_MASK 0x7
104 #define KMX61_DEFAULT_WAKE_THRESH 1
105 #define KMX61_DEFAULT_WAKE_DURATION 1
107 #define KMX61_SLEEP_DELAY_MS 2000
109 #define KMX61_CHIP_ID 0x12
112 #define KMX61_ACC 0x01
113 #define KMX61_MAG 0x02
116 struct i2c_client *client;
118 /* serialize access to non-atomic ops, e.g set_mode */
135 /* accelerometer specific data */
136 struct iio_dev *acc_indio_dev;
137 struct iio_trigger *acc_dready_trig;
138 struct iio_trigger *motion_trig;
139 bool acc_dready_trig_on;
141 bool ev_enable_state;
143 /* magnetometer specific data */
144 struct iio_dev *mag_indio_dev;
145 struct iio_trigger *mag_dready_trig;
146 bool mag_dready_trig_on;
161 static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
163 static const struct {
166 } kmx61_samp_freq_table[] = { {12, 500000},
179 static const struct {
183 } kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
196 static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
197 static IIO_CONST_ATTR(magn_scale_available, "0.001465");
198 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
199 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
201 static struct attribute *kmx61_acc_attributes[] = {
202 &iio_const_attr_accel_scale_available.dev_attr.attr,
203 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
207 static struct attribute *kmx61_mag_attributes[] = {
208 &iio_const_attr_magn_scale_available.dev_attr.attr,
209 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
213 static const struct attribute_group kmx61_acc_attribute_group = {
214 .attrs = kmx61_acc_attributes,
217 static const struct attribute_group kmx61_mag_attribute_group = {
218 .attrs = kmx61_mag_attributes,
221 static const struct iio_event_spec kmx61_event = {
222 .type = IIO_EV_TYPE_THRESH,
223 .dir = IIO_EV_DIR_EITHER,
224 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
225 BIT(IIO_EV_INFO_ENABLE) |
226 BIT(IIO_EV_INFO_PERIOD),
229 #define KMX61_ACC_CHAN(_axis) { \
232 .channel2 = IIO_MOD_ ## _axis, \
233 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
234 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
235 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
236 .address = KMX61_ACC, \
237 .scan_index = KMX61_AXIS_ ## _axis, \
243 .endianness = IIO_LE, \
245 .event_spec = &kmx61_event, \
246 .num_event_specs = 1 \
249 #define KMX61_MAG_CHAN(_axis) { \
252 .channel2 = IIO_MOD_ ## _axis, \
253 .address = KMX61_MAG, \
254 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
255 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
256 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
257 .scan_index = KMX61_AXIS_ ## _axis, \
263 .endianness = IIO_LE, \
267 static const struct iio_chan_spec kmx61_acc_channels[] = {
273 static const struct iio_chan_spec kmx61_mag_channels[] = {
279 static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
281 struct kmx61_data **priv = iio_priv(indio_dev);
286 static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
288 return *(struct kmx61_data **)iio_priv(indio_dev);
291 static int kmx61_convert_freq_to_bit(int val, int val2)
295 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
296 if (val == kmx61_samp_freq_table[i].val &&
297 val2 == kmx61_samp_freq_table[i].val2)
302 static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
306 for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
307 if (kmx61_wake_up_odr_table[i].val == val &&
308 kmx61_wake_up_odr_table[i].val2 == val2)
309 return kmx61_wake_up_odr_table[i].odr_bits;
314 * kmx61_set_mode() - set KMX61 device operating mode
315 * @data: kmx61 device private data pointer
316 * @mode: bitmask, indicating operating mode for @device
317 * @device: bitmask, indicating device for which @mode needs to be set
318 * @update: update stby bits stored in device's private @data
320 * For each sensor (accelerometer/magnetometer) there are two operating modes
321 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
322 * if they are both enabled. Internal sensors state is saved in acc_stby and
323 * mag_stby members of driver's private @data.
325 static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
329 int acc_stby = -1, mag_stby = -1;
331 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
333 dev_err(&data->client->dev, "Error reading reg_stby\n");
336 if (device & KMX61_ACC) {
337 if (mode & KMX61_ACC_STBY_BIT) {
338 ret |= KMX61_ACC_STBY_BIT;
341 ret &= ~KMX61_ACC_STBY_BIT;
346 if (device & KMX61_MAG) {
347 if (mode & KMX61_MAG_STBY_BIT) {
348 ret |= KMX61_MAG_STBY_BIT;
351 ret &= ~KMX61_MAG_STBY_BIT;
356 if (mode & KMX61_ACT_STBY_BIT)
357 ret |= KMX61_ACT_STBY_BIT;
359 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
361 dev_err(&data->client->dev, "Error writing reg_stby\n");
365 if (acc_stby != -1 && update)
366 data->acc_stby = acc_stby;
367 if (mag_stby != -1 && update)
368 data->mag_stby = mag_stby;
373 static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
377 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
379 dev_err(&data->client->dev, "Error reading reg_stby\n");
384 if (device & KMX61_ACC) {
385 if (ret & KMX61_ACC_STBY_BIT)
386 *mode |= KMX61_ACC_STBY_BIT;
388 *mode &= ~KMX61_ACC_STBY_BIT;
391 if (device & KMX61_MAG) {
392 if (ret & KMX61_MAG_STBY_BIT)
393 *mode |= KMX61_MAG_STBY_BIT;
395 *mode &= ~KMX61_MAG_STBY_BIT;
401 static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
405 odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
409 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
412 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
416 static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
420 int lodr_bits, odr_bits;
422 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
426 lodr_bits = kmx61_convert_freq_to_bit(val, val2);
430 /* To change ODR, accel and magn must be in STDBY */
431 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
437 if (device & KMX61_ACC)
438 odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
439 if (device & KMX61_MAG)
440 odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
442 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
447 data->odr_bits = odr_bits;
449 if (device & KMX61_ACC) {
450 ret = kmx61_set_wake_up_odr(data, val, val2);
455 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
458 static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
463 if (device & KMX61_ACC)
464 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
466 else if (device & KMX61_MAG)
467 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
472 if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
475 *val = kmx61_samp_freq_table[lodr_bits].val;
476 *val2 = kmx61_samp_freq_table[lodr_bits].val2;
481 static int kmx61_set_range(struct kmx61_data *data, u8 range)
485 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
487 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
491 ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
492 ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
494 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
496 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
505 static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
510 for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
511 if (kmx61_uscale_table[i] == uscale) {
512 ret = kmx61_get_mode(data, &mode,
513 KMX61_ACC | KMX61_MAG);
517 ret = kmx61_set_mode(data, KMX61_ALL_STBY,
518 KMX61_ACC | KMX61_MAG, true);
522 ret = kmx61_set_range(data, i);
526 return kmx61_set_mode(data, mode,
527 KMX61_ACC | KMX61_MAG, true);
533 static int kmx61_chip_init(struct kmx61_data *data)
537 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
539 dev_err(&data->client->dev, "Error reading who_am_i\n");
543 if (ret != KMX61_CHIP_ID) {
544 dev_err(&data->client->dev,
545 "Wrong chip id, got %x expected %x\n",
550 /* set accel 12bit, 4g range */
551 ret = kmx61_set_range(data, KMX61_RANGE_4G);
555 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
557 dev_err(&data->client->dev, "Error reading reg_odcntl\n");
560 data->odr_bits = ret;
563 * set output data rate for wake up (motion detection) function
564 * to match data rate for accelerometer sampling
566 ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
570 ret = kmx61_set_wake_up_odr(data, val, val2);
574 /* set acc/magn to OPERATION mode */
575 ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
579 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
580 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
585 static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
586 bool status, u8 device)
591 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
595 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
599 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
601 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
606 ret |= KMX61_REG_INC1_BIT_IEN;
607 if (device & KMX61_ACC)
608 ret |= KMX61_REG_INC1_BIT_DRDYA;
609 if (device & KMX61_MAG)
610 ret |= KMX61_REG_INC1_BIT_DRDYM;
612 ret &= ~KMX61_REG_INC1_BIT_IEN;
613 if (device & KMX61_ACC)
614 ret &= ~KMX61_REG_INC1_BIT_DRDYA;
615 if (device & KMX61_MAG)
616 ret &= ~KMX61_REG_INC1_BIT_DRDYM;
618 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
620 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
624 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
626 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
631 ret |= KMX61_REG_CTRL1_BIT_DRDYE;
633 ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
635 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
637 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
641 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
644 static int kmx61_chip_update_thresholds(struct kmx61_data *data)
648 ret = i2c_smbus_write_byte_data(data->client,
650 data->wake_duration);
652 dev_err(&data->client->dev, "Errow writing reg_wuf_timer\n");
656 ret = i2c_smbus_write_byte_data(data->client,
657 KMX61_REG_WUF_THRESH,
660 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
665 static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
671 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
675 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
679 ret = kmx61_chip_update_thresholds(data);
683 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
685 dev_err(&data->client->dev, "Error reading reg_inc1\n");
689 ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
691 ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
693 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
695 dev_err(&data->client->dev, "Error writing reg_inc1\n");
699 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
701 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
706 ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
708 ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
710 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
712 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
715 mode |= KMX61_ACT_STBY_BIT;
716 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
720 * kmx61_set_power_state() - set power state for kmx61 @device
721 * @data: kmx61 device private pointer
722 * @on: power state to be set for @device
723 * @device: bitmask indicating device for which @on state needs to be set
725 * Notice that when ACC power state needs to be set to ON and MAG is in
726 * OPERATION then we know that kmx61_runtime_resume was already called
727 * so we must set ACC OPERATION mode here. The same happens when MAG power
728 * state needs to be set to ON and ACC is in OPERATION.
730 static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
735 if (device & KMX61_ACC) {
736 if (on && !data->acc_ps && !data->mag_stby) {
737 ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
743 if (device & KMX61_MAG) {
744 if (on && !data->mag_ps && !data->acc_stby) {
745 ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
753 ret = pm_runtime_resume_and_get(&data->client->dev);
755 pm_runtime_mark_last_busy(&data->client->dev);
756 ret = pm_runtime_put_autosuspend(&data->client->dev);
759 dev_err(&data->client->dev,
760 "Failed: kmx61_set_power_state for %d, ret %d\n",
769 static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
772 u8 reg = base + offset * 2;
774 ret = i2c_smbus_read_word_data(data->client, reg);
776 dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
781 static int kmx61_read_raw(struct iio_dev *indio_dev,
782 struct iio_chan_spec const *chan, int *val,
783 int *val2, long mask)
787 struct kmx61_data *data = kmx61_get_data(indio_dev);
790 case IIO_CHAN_INFO_RAW:
791 switch (chan->type) {
793 base_reg = KMX61_ACC_XOUT_L;
796 base_reg = KMX61_MAG_XOUT_L;
801 mutex_lock(&data->lock);
803 ret = kmx61_set_power_state(data, true, chan->address);
805 mutex_unlock(&data->lock);
809 ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
811 kmx61_set_power_state(data, false, chan->address);
812 mutex_unlock(&data->lock);
815 *val = sign_extend32(ret >> chan->scan_type.shift,
816 chan->scan_type.realbits - 1);
817 ret = kmx61_set_power_state(data, false, chan->address);
819 mutex_unlock(&data->lock);
823 case IIO_CHAN_INFO_SCALE:
824 switch (chan->type) {
827 *val2 = kmx61_uscale_table[data->range];
828 return IIO_VAL_INT_PLUS_MICRO;
830 /* 14 bits res, 1465 microGauss per magn count */
833 return IIO_VAL_INT_PLUS_MICRO;
837 case IIO_CHAN_INFO_SAMP_FREQ:
838 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
841 mutex_lock(&data->lock);
842 ret = kmx61_get_odr(data, val, val2, chan->address);
843 mutex_unlock(&data->lock);
846 return IIO_VAL_INT_PLUS_MICRO;
851 static int kmx61_write_raw(struct iio_dev *indio_dev,
852 struct iio_chan_spec const *chan, int val,
856 struct kmx61_data *data = kmx61_get_data(indio_dev);
859 case IIO_CHAN_INFO_SAMP_FREQ:
860 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
863 mutex_lock(&data->lock);
864 ret = kmx61_set_odr(data, val, val2, chan->address);
865 mutex_unlock(&data->lock);
867 case IIO_CHAN_INFO_SCALE:
868 switch (chan->type) {
872 mutex_lock(&data->lock);
873 ret = kmx61_set_scale(data, val2);
874 mutex_unlock(&data->lock);
884 static int kmx61_read_event(struct iio_dev *indio_dev,
885 const struct iio_chan_spec *chan,
886 enum iio_event_type type,
887 enum iio_event_direction dir,
888 enum iio_event_info info,
891 struct kmx61_data *data = kmx61_get_data(indio_dev);
895 case IIO_EV_INFO_VALUE:
896 *val = data->wake_thresh;
898 case IIO_EV_INFO_PERIOD:
899 *val = data->wake_duration;
906 static int kmx61_write_event(struct iio_dev *indio_dev,
907 const struct iio_chan_spec *chan,
908 enum iio_event_type type,
909 enum iio_event_direction dir,
910 enum iio_event_info info,
913 struct kmx61_data *data = kmx61_get_data(indio_dev);
915 if (data->ev_enable_state)
919 case IIO_EV_INFO_VALUE:
920 data->wake_thresh = val;
922 case IIO_EV_INFO_PERIOD:
923 data->wake_duration = val;
930 static int kmx61_read_event_config(struct iio_dev *indio_dev,
931 const struct iio_chan_spec *chan,
932 enum iio_event_type type,
933 enum iio_event_direction dir)
935 struct kmx61_data *data = kmx61_get_data(indio_dev);
937 return data->ev_enable_state;
940 static int kmx61_write_event_config(struct iio_dev *indio_dev,
941 const struct iio_chan_spec *chan,
942 enum iio_event_type type,
943 enum iio_event_direction dir,
946 struct kmx61_data *data = kmx61_get_data(indio_dev);
949 if (state && data->ev_enable_state)
952 mutex_lock(&data->lock);
954 if (!state && data->motion_trig_on) {
955 data->ev_enable_state = false;
959 ret = kmx61_set_power_state(data, state, KMX61_ACC);
963 ret = kmx61_setup_any_motion_interrupt(data, state);
965 kmx61_set_power_state(data, false, KMX61_ACC);
969 data->ev_enable_state = state;
972 mutex_unlock(&data->lock);
977 static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
978 struct iio_trigger *trig)
980 struct kmx61_data *data = kmx61_get_data(indio_dev);
982 if (data->acc_dready_trig != trig && data->motion_trig != trig)
988 static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
989 struct iio_trigger *trig)
991 struct kmx61_data *data = kmx61_get_data(indio_dev);
993 if (data->mag_dready_trig != trig)
999 static const struct iio_info kmx61_acc_info = {
1000 .read_raw = kmx61_read_raw,
1001 .write_raw = kmx61_write_raw,
1002 .attrs = &kmx61_acc_attribute_group,
1003 .read_event_value = kmx61_read_event,
1004 .write_event_value = kmx61_write_event,
1005 .read_event_config = kmx61_read_event_config,
1006 .write_event_config = kmx61_write_event_config,
1007 .validate_trigger = kmx61_acc_validate_trigger,
1010 static const struct iio_info kmx61_mag_info = {
1011 .read_raw = kmx61_read_raw,
1012 .write_raw = kmx61_write_raw,
1013 .attrs = &kmx61_mag_attribute_group,
1014 .validate_trigger = kmx61_mag_validate_trigger,
1018 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
1024 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1025 struct kmx61_data *data = kmx61_get_data(indio_dev);
1027 mutex_lock(&data->lock);
1029 if (!state && data->ev_enable_state && data->motion_trig_on) {
1030 data->motion_trig_on = false;
1034 if (data->acc_dready_trig == trig || data->motion_trig == trig)
1039 ret = kmx61_set_power_state(data, state, device);
1043 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1044 ret = kmx61_setup_new_data_interrupt(data, state, device);
1046 ret = kmx61_setup_any_motion_interrupt(data, state);
1048 kmx61_set_power_state(data, false, device);
1052 if (data->acc_dready_trig == trig)
1053 data->acc_dready_trig_on = state;
1054 else if (data->mag_dready_trig == trig)
1055 data->mag_dready_trig_on = state;
1057 data->motion_trig_on = state;
1059 mutex_unlock(&data->lock);
1064 static void kmx61_trig_reenable(struct iio_trigger *trig)
1066 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1067 struct kmx61_data *data = kmx61_get_data(indio_dev);
1070 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1072 dev_err(&data->client->dev, "Error reading reg_inl\n");
1075 static const struct iio_trigger_ops kmx61_trigger_ops = {
1076 .set_trigger_state = kmx61_data_rdy_trigger_set_state,
1077 .reenable = kmx61_trig_reenable,
1080 static irqreturn_t kmx61_event_handler(int irq, void *private)
1082 struct kmx61_data *data = private;
1083 struct iio_dev *indio_dev = data->acc_indio_dev;
1086 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1088 dev_err(&data->client->dev, "Error reading reg_ins1\n");
1092 if (ret & KMX61_REG_INS1_BIT_WUFS) {
1093 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1095 dev_err(&data->client->dev, "Error reading reg_ins2\n");
1099 if (ret & KMX61_REG_INS2_BIT_XN)
1100 iio_push_event(indio_dev,
1101 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1105 IIO_EV_DIR_FALLING),
1108 if (ret & KMX61_REG_INS2_BIT_XP)
1109 iio_push_event(indio_dev,
1110 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1117 if (ret & KMX61_REG_INS2_BIT_YN)
1118 iio_push_event(indio_dev,
1119 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1123 IIO_EV_DIR_FALLING),
1126 if (ret & KMX61_REG_INS2_BIT_YP)
1127 iio_push_event(indio_dev,
1128 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1135 if (ret & KMX61_REG_INS2_BIT_ZN)
1136 iio_push_event(indio_dev,
1137 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1141 IIO_EV_DIR_FALLING),
1144 if (ret & KMX61_REG_INS2_BIT_ZP)
1145 iio_push_event(indio_dev,
1146 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1155 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1157 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1159 ret |= KMX61_REG_CTRL1_BIT_RES;
1160 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1162 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1164 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1166 dev_err(&data->client->dev, "Error reading reg_inl\n");
1171 static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1173 struct kmx61_data *data = private;
1175 if (data->acc_dready_trig_on)
1176 iio_trigger_poll(data->acc_dready_trig);
1177 if (data->mag_dready_trig_on)
1178 iio_trigger_poll(data->mag_dready_trig);
1180 if (data->motion_trig_on)
1181 iio_trigger_poll(data->motion_trig);
1183 if (data->ev_enable_state)
1184 return IRQ_WAKE_THREAD;
1188 static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1190 struct iio_poll_func *pf = p;
1191 struct iio_dev *indio_dev = pf->indio_dev;
1192 struct kmx61_data *data = kmx61_get_data(indio_dev);
1193 int bit, ret, i = 0;
1197 if (indio_dev == data->acc_indio_dev)
1198 base = KMX61_ACC_XOUT_L;
1200 base = KMX61_MAG_XOUT_L;
1202 mutex_lock(&data->lock);
1203 for_each_set_bit(bit, indio_dev->active_scan_mask,
1204 indio_dev->masklength) {
1205 ret = kmx61_read_measurement(data, base, bit);
1207 mutex_unlock(&data->lock);
1212 mutex_unlock(&data->lock);
1214 iio_push_to_buffers(indio_dev, buffer);
1216 iio_trigger_notify_done(indio_dev->trig);
1221 static const char *kmx61_match_acpi_device(struct device *dev)
1223 const struct acpi_device_id *id;
1225 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1228 return dev_name(dev);
1231 static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1232 const struct iio_info *info,
1233 const struct iio_chan_spec *chan,
1237 struct iio_dev *indio_dev;
1239 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1241 return ERR_PTR(-ENOMEM);
1243 kmx61_set_data(indio_dev, data);
1245 indio_dev->channels = chan;
1246 indio_dev->num_channels = num_channels;
1247 indio_dev->name = name;
1248 indio_dev->modes = INDIO_DIRECT_MODE;
1249 indio_dev->info = info;
1254 static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1255 struct iio_dev *indio_dev,
1258 struct iio_trigger *trig;
1261 trig = devm_iio_trigger_alloc(&data->client->dev,
1265 iio_device_id(indio_dev));
1267 return ERR_PTR(-ENOMEM);
1269 trig->ops = &kmx61_trigger_ops;
1270 iio_trigger_set_drvdata(trig, indio_dev);
1272 ret = iio_trigger_register(trig);
1274 return ERR_PTR(ret);
1279 static int kmx61_probe(struct i2c_client *client,
1280 const struct i2c_device_id *id)
1283 struct kmx61_data *data;
1284 const char *name = NULL;
1286 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1290 i2c_set_clientdata(client, data);
1291 data->client = client;
1293 mutex_init(&data->lock);
1297 else if (ACPI_HANDLE(&client->dev))
1298 name = kmx61_match_acpi_device(&client->dev);
1302 data->acc_indio_dev =
1303 kmx61_indiodev_setup(data, &kmx61_acc_info,
1305 ARRAY_SIZE(kmx61_acc_channels),
1307 if (IS_ERR(data->acc_indio_dev))
1308 return PTR_ERR(data->acc_indio_dev);
1310 data->mag_indio_dev =
1311 kmx61_indiodev_setup(data, &kmx61_mag_info,
1313 ARRAY_SIZE(kmx61_mag_channels),
1315 if (IS_ERR(data->mag_indio_dev))
1316 return PTR_ERR(data->mag_indio_dev);
1318 ret = kmx61_chip_init(data);
1322 if (client->irq > 0) {
1323 ret = devm_request_threaded_irq(&client->dev, client->irq,
1324 kmx61_data_rdy_trig_poll,
1325 kmx61_event_handler,
1326 IRQF_TRIGGER_RISING,
1330 goto err_chip_uninit;
1332 data->acc_dready_trig =
1333 kmx61_trigger_setup(data, data->acc_indio_dev,
1335 if (IS_ERR(data->acc_dready_trig)) {
1336 ret = PTR_ERR(data->acc_dready_trig);
1337 goto err_chip_uninit;
1340 data->mag_dready_trig =
1341 kmx61_trigger_setup(data, data->mag_indio_dev,
1343 if (IS_ERR(data->mag_dready_trig)) {
1344 ret = PTR_ERR(data->mag_dready_trig);
1345 goto err_trigger_unregister_acc_dready;
1349 kmx61_trigger_setup(data, data->acc_indio_dev,
1351 if (IS_ERR(data->motion_trig)) {
1352 ret = PTR_ERR(data->motion_trig);
1353 goto err_trigger_unregister_mag_dready;
1356 ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1357 &iio_pollfunc_store_time,
1358 kmx61_trigger_handler,
1361 dev_err(&data->client->dev,
1362 "Failed to setup acc triggered buffer\n");
1363 goto err_trigger_unregister_motion;
1366 ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1367 &iio_pollfunc_store_time,
1368 kmx61_trigger_handler,
1371 dev_err(&data->client->dev,
1372 "Failed to setup mag triggered buffer\n");
1373 goto err_buffer_cleanup_acc;
1377 ret = pm_runtime_set_active(&client->dev);
1379 goto err_buffer_cleanup_mag;
1381 pm_runtime_enable(&client->dev);
1382 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1383 pm_runtime_use_autosuspend(&client->dev);
1385 ret = iio_device_register(data->acc_indio_dev);
1387 dev_err(&client->dev, "Failed to register acc iio device\n");
1388 goto err_pm_cleanup;
1391 ret = iio_device_register(data->mag_indio_dev);
1393 dev_err(&client->dev, "Failed to register mag iio device\n");
1394 goto err_iio_unregister_acc;
1399 err_iio_unregister_acc:
1400 iio_device_unregister(data->acc_indio_dev);
1402 pm_runtime_dont_use_autosuspend(&client->dev);
1403 pm_runtime_disable(&client->dev);
1404 err_buffer_cleanup_mag:
1405 if (client->irq > 0)
1406 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1407 err_buffer_cleanup_acc:
1408 if (client->irq > 0)
1409 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1410 err_trigger_unregister_motion:
1411 iio_trigger_unregister(data->motion_trig);
1412 err_trigger_unregister_mag_dready:
1413 iio_trigger_unregister(data->mag_dready_trig);
1414 err_trigger_unregister_acc_dready:
1415 iio_trigger_unregister(data->acc_dready_trig);
1417 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1421 static int kmx61_remove(struct i2c_client *client)
1423 struct kmx61_data *data = i2c_get_clientdata(client);
1425 iio_device_unregister(data->acc_indio_dev);
1426 iio_device_unregister(data->mag_indio_dev);
1428 pm_runtime_disable(&client->dev);
1429 pm_runtime_set_suspended(&client->dev);
1431 if (client->irq > 0) {
1432 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1433 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1434 iio_trigger_unregister(data->acc_dready_trig);
1435 iio_trigger_unregister(data->mag_dready_trig);
1436 iio_trigger_unregister(data->motion_trig);
1439 mutex_lock(&data->lock);
1440 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1441 mutex_unlock(&data->lock);
1446 #ifdef CONFIG_PM_SLEEP
1447 static int kmx61_suspend(struct device *dev)
1450 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1452 mutex_lock(&data->lock);
1453 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1455 mutex_unlock(&data->lock);
1460 static int kmx61_resume(struct device *dev)
1463 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1466 stby |= KMX61_ACC_STBY_BIT;
1468 stby |= KMX61_MAG_STBY_BIT;
1470 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1475 static int kmx61_runtime_suspend(struct device *dev)
1477 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1480 mutex_lock(&data->lock);
1481 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1482 mutex_unlock(&data->lock);
1487 static int kmx61_runtime_resume(struct device *dev)
1489 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1493 stby |= KMX61_ACC_STBY_BIT;
1495 stby |= KMX61_MAG_STBY_BIT;
1497 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1501 static const struct dev_pm_ops kmx61_pm_ops = {
1502 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
1503 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1506 static const struct acpi_device_id kmx61_acpi_match[] = {
1511 MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
1513 static const struct i2c_device_id kmx61_id[] = {
1518 MODULE_DEVICE_TABLE(i2c, kmx61_id);
1520 static struct i2c_driver kmx61_driver = {
1522 .name = KMX61_DRV_NAME,
1523 .acpi_match_table = ACPI_PTR(kmx61_acpi_match),
1524 .pm = &kmx61_pm_ops,
1526 .probe = kmx61_probe,
1527 .remove = kmx61_remove,
1528 .id_table = kmx61_id,
1531 module_i2c_driver(kmx61_driver);
1533 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1534 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1535 MODULE_LICENSE("GPL v2");