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_get_sync(&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",
763 pm_runtime_put_noidle(&data
->client
->dev
);
771 static int kmx61_read_measurement(struct kmx61_data
*data
, u8 base
, u8 offset
)
774 u8 reg
= base
+ offset
* 2;
776 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
778 dev_err(&data
->client
->dev
, "failed to read reg at %x\n", reg
);
783 static int kmx61_read_raw(struct iio_dev
*indio_dev
,
784 struct iio_chan_spec
const *chan
, int *val
,
785 int *val2
, long mask
)
789 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
792 case IIO_CHAN_INFO_RAW
:
793 switch (chan
->type
) {
795 base_reg
= KMX61_ACC_XOUT_L
;
798 base_reg
= KMX61_MAG_XOUT_L
;
803 mutex_lock(&data
->lock
);
805 ret
= kmx61_set_power_state(data
, true, chan
->address
);
807 mutex_unlock(&data
->lock
);
811 ret
= kmx61_read_measurement(data
, base_reg
, chan
->scan_index
);
813 kmx61_set_power_state(data
, false, chan
->address
);
814 mutex_unlock(&data
->lock
);
817 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
818 chan
->scan_type
.realbits
- 1);
819 ret
= kmx61_set_power_state(data
, false, chan
->address
);
821 mutex_unlock(&data
->lock
);
825 case IIO_CHAN_INFO_SCALE
:
826 switch (chan
->type
) {
829 *val2
= kmx61_uscale_table
[data
->range
];
830 return IIO_VAL_INT_PLUS_MICRO
;
832 /* 14 bits res, 1465 microGauss per magn count */
835 return IIO_VAL_INT_PLUS_MICRO
;
839 case IIO_CHAN_INFO_SAMP_FREQ
:
840 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
843 mutex_lock(&data
->lock
);
844 ret
= kmx61_get_odr(data
, val
, val2
, chan
->address
);
845 mutex_unlock(&data
->lock
);
848 return IIO_VAL_INT_PLUS_MICRO
;
853 static int kmx61_write_raw(struct iio_dev
*indio_dev
,
854 struct iio_chan_spec
const *chan
, int val
,
858 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
861 case IIO_CHAN_INFO_SAMP_FREQ
:
862 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
865 mutex_lock(&data
->lock
);
866 ret
= kmx61_set_odr(data
, val
, val2
, chan
->address
);
867 mutex_unlock(&data
->lock
);
869 case IIO_CHAN_INFO_SCALE
:
870 switch (chan
->type
) {
874 mutex_lock(&data
->lock
);
875 ret
= kmx61_set_scale(data
, val2
);
876 mutex_unlock(&data
->lock
);
886 static int kmx61_read_event(struct iio_dev
*indio_dev
,
887 const struct iio_chan_spec
*chan
,
888 enum iio_event_type type
,
889 enum iio_event_direction dir
,
890 enum iio_event_info info
,
893 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
897 case IIO_EV_INFO_VALUE
:
898 *val
= data
->wake_thresh
;
900 case IIO_EV_INFO_PERIOD
:
901 *val
= data
->wake_duration
;
908 static int kmx61_write_event(struct iio_dev
*indio_dev
,
909 const struct iio_chan_spec
*chan
,
910 enum iio_event_type type
,
911 enum iio_event_direction dir
,
912 enum iio_event_info info
,
915 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
917 if (data
->ev_enable_state
)
921 case IIO_EV_INFO_VALUE
:
922 data
->wake_thresh
= val
;
924 case IIO_EV_INFO_PERIOD
:
925 data
->wake_duration
= val
;
932 static int kmx61_read_event_config(struct iio_dev
*indio_dev
,
933 const struct iio_chan_spec
*chan
,
934 enum iio_event_type type
,
935 enum iio_event_direction dir
)
937 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
939 return data
->ev_enable_state
;
942 static int kmx61_write_event_config(struct iio_dev
*indio_dev
,
943 const struct iio_chan_spec
*chan
,
944 enum iio_event_type type
,
945 enum iio_event_direction dir
,
948 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
951 if (state
&& data
->ev_enable_state
)
954 mutex_lock(&data
->lock
);
956 if (!state
&& data
->motion_trig_on
) {
957 data
->ev_enable_state
= false;
961 ret
= kmx61_set_power_state(data
, state
, KMX61_ACC
);
965 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
967 kmx61_set_power_state(data
, false, KMX61_ACC
);
971 data
->ev_enable_state
= state
;
974 mutex_unlock(&data
->lock
);
979 static int kmx61_acc_validate_trigger(struct iio_dev
*indio_dev
,
980 struct iio_trigger
*trig
)
982 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
984 if (data
->acc_dready_trig
!= trig
&& data
->motion_trig
!= trig
)
990 static int kmx61_mag_validate_trigger(struct iio_dev
*indio_dev
,
991 struct iio_trigger
*trig
)
993 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
995 if (data
->mag_dready_trig
!= trig
)
1001 static const struct iio_info kmx61_acc_info
= {
1002 .read_raw
= kmx61_read_raw
,
1003 .write_raw
= kmx61_write_raw
,
1004 .attrs
= &kmx61_acc_attribute_group
,
1005 .read_event_value
= kmx61_read_event
,
1006 .write_event_value
= kmx61_write_event
,
1007 .read_event_config
= kmx61_read_event_config
,
1008 .write_event_config
= kmx61_write_event_config
,
1009 .validate_trigger
= kmx61_acc_validate_trigger
,
1012 static const struct iio_info kmx61_mag_info
= {
1013 .read_raw
= kmx61_read_raw
,
1014 .write_raw
= kmx61_write_raw
,
1015 .attrs
= &kmx61_mag_attribute_group
,
1016 .validate_trigger
= kmx61_mag_validate_trigger
,
1020 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1026 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1027 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1029 mutex_lock(&data
->lock
);
1031 if (!state
&& data
->ev_enable_state
&& data
->motion_trig_on
) {
1032 data
->motion_trig_on
= false;
1036 if (data
->acc_dready_trig
== trig
|| data
->motion_trig
== trig
)
1041 ret
= kmx61_set_power_state(data
, state
, device
);
1045 if (data
->acc_dready_trig
== trig
|| data
->mag_dready_trig
== trig
)
1046 ret
= kmx61_setup_new_data_interrupt(data
, state
, device
);
1048 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
1050 kmx61_set_power_state(data
, false, device
);
1054 if (data
->acc_dready_trig
== trig
)
1055 data
->acc_dready_trig_on
= state
;
1056 else if (data
->mag_dready_trig
== trig
)
1057 data
->mag_dready_trig_on
= state
;
1059 data
->motion_trig_on
= state
;
1061 mutex_unlock(&data
->lock
);
1066 static int kmx61_trig_try_reenable(struct iio_trigger
*trig
)
1068 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1069 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1072 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1074 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1081 static const struct iio_trigger_ops kmx61_trigger_ops
= {
1082 .set_trigger_state
= kmx61_data_rdy_trigger_set_state
,
1083 .try_reenable
= kmx61_trig_try_reenable
,
1086 static irqreturn_t
kmx61_event_handler(int irq
, void *private)
1088 struct kmx61_data
*data
= private;
1089 struct iio_dev
*indio_dev
= data
->acc_indio_dev
;
1092 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS1
);
1094 dev_err(&data
->client
->dev
, "Error reading reg_ins1\n");
1098 if (ret
& KMX61_REG_INS1_BIT_WUFS
) {
1099 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS2
);
1101 dev_err(&data
->client
->dev
, "Error reading reg_ins2\n");
1105 if (ret
& KMX61_REG_INS2_BIT_XN
)
1106 iio_push_event(indio_dev
,
1107 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1111 IIO_EV_DIR_FALLING
),
1114 if (ret
& KMX61_REG_INS2_BIT_XP
)
1115 iio_push_event(indio_dev
,
1116 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1123 if (ret
& KMX61_REG_INS2_BIT_YN
)
1124 iio_push_event(indio_dev
,
1125 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1129 IIO_EV_DIR_FALLING
),
1132 if (ret
& KMX61_REG_INS2_BIT_YP
)
1133 iio_push_event(indio_dev
,
1134 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1141 if (ret
& KMX61_REG_INS2_BIT_ZN
)
1142 iio_push_event(indio_dev
,
1143 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1147 IIO_EV_DIR_FALLING
),
1150 if (ret
& KMX61_REG_INS2_BIT_ZP
)
1151 iio_push_event(indio_dev
,
1152 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1161 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
1163 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
1165 ret
|= KMX61_REG_CTRL1_BIT_RES
;
1166 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
1168 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
1170 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1172 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1177 static irqreturn_t
kmx61_data_rdy_trig_poll(int irq
, void *private)
1179 struct kmx61_data
*data
= private;
1181 if (data
->acc_dready_trig_on
)
1182 iio_trigger_poll(data
->acc_dready_trig
);
1183 if (data
->mag_dready_trig_on
)
1184 iio_trigger_poll(data
->mag_dready_trig
);
1186 if (data
->motion_trig_on
)
1187 iio_trigger_poll(data
->motion_trig
);
1189 if (data
->ev_enable_state
)
1190 return IRQ_WAKE_THREAD
;
1194 static irqreturn_t
kmx61_trigger_handler(int irq
, void *p
)
1196 struct iio_poll_func
*pf
= p
;
1197 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1198 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1199 int bit
, ret
, i
= 0;
1203 if (indio_dev
== data
->acc_indio_dev
)
1204 base
= KMX61_ACC_XOUT_L
;
1206 base
= KMX61_MAG_XOUT_L
;
1208 mutex_lock(&data
->lock
);
1209 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1210 indio_dev
->masklength
) {
1211 ret
= kmx61_read_measurement(data
, base
, bit
);
1213 mutex_unlock(&data
->lock
);
1218 mutex_unlock(&data
->lock
);
1220 iio_push_to_buffers(indio_dev
, buffer
);
1222 iio_trigger_notify_done(indio_dev
->trig
);
1227 static const char *kmx61_match_acpi_device(struct device
*dev
)
1229 const struct acpi_device_id
*id
;
1231 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1234 return dev_name(dev
);
1237 static struct iio_dev
*kmx61_indiodev_setup(struct kmx61_data
*data
,
1238 const struct iio_info
*info
,
1239 const struct iio_chan_spec
*chan
,
1243 struct iio_dev
*indio_dev
;
1245 indio_dev
= devm_iio_device_alloc(&data
->client
->dev
, sizeof(data
));
1247 return ERR_PTR(-ENOMEM
);
1249 kmx61_set_data(indio_dev
, data
);
1251 indio_dev
->dev
.parent
= &data
->client
->dev
;
1252 indio_dev
->channels
= chan
;
1253 indio_dev
->num_channels
= num_channels
;
1254 indio_dev
->name
= name
;
1255 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1256 indio_dev
->info
= info
;
1261 static struct iio_trigger
*kmx61_trigger_setup(struct kmx61_data
*data
,
1262 struct iio_dev
*indio_dev
,
1265 struct iio_trigger
*trig
;
1268 trig
= devm_iio_trigger_alloc(&data
->client
->dev
,
1274 return ERR_PTR(-ENOMEM
);
1276 trig
->dev
.parent
= &data
->client
->dev
;
1277 trig
->ops
= &kmx61_trigger_ops
;
1278 iio_trigger_set_drvdata(trig
, indio_dev
);
1280 ret
= iio_trigger_register(trig
);
1282 return ERR_PTR(ret
);
1287 static int kmx61_probe(struct i2c_client
*client
,
1288 const struct i2c_device_id
*id
)
1291 struct kmx61_data
*data
;
1292 const char *name
= NULL
;
1294 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1298 i2c_set_clientdata(client
, data
);
1299 data
->client
= client
;
1301 mutex_init(&data
->lock
);
1305 else if (ACPI_HANDLE(&client
->dev
))
1306 name
= kmx61_match_acpi_device(&client
->dev
);
1310 data
->acc_indio_dev
=
1311 kmx61_indiodev_setup(data
, &kmx61_acc_info
,
1313 ARRAY_SIZE(kmx61_acc_channels
),
1315 if (IS_ERR(data
->acc_indio_dev
))
1316 return PTR_ERR(data
->acc_indio_dev
);
1318 data
->mag_indio_dev
=
1319 kmx61_indiodev_setup(data
, &kmx61_mag_info
,
1321 ARRAY_SIZE(kmx61_mag_channels
),
1323 if (IS_ERR(data
->mag_indio_dev
))
1324 return PTR_ERR(data
->mag_indio_dev
);
1326 ret
= kmx61_chip_init(data
);
1330 if (client
->irq
> 0) {
1331 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1332 kmx61_data_rdy_trig_poll
,
1333 kmx61_event_handler
,
1334 IRQF_TRIGGER_RISING
,
1338 goto err_chip_uninit
;
1340 data
->acc_dready_trig
=
1341 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1343 if (IS_ERR(data
->acc_dready_trig
)) {
1344 ret
= PTR_ERR(data
->acc_dready_trig
);
1345 goto err_chip_uninit
;
1348 data
->mag_dready_trig
=
1349 kmx61_trigger_setup(data
, data
->mag_indio_dev
,
1351 if (IS_ERR(data
->mag_dready_trig
)) {
1352 ret
= PTR_ERR(data
->mag_dready_trig
);
1353 goto err_trigger_unregister_acc_dready
;
1357 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1359 if (IS_ERR(data
->motion_trig
)) {
1360 ret
= PTR_ERR(data
->motion_trig
);
1361 goto err_trigger_unregister_mag_dready
;
1364 ret
= iio_triggered_buffer_setup(data
->acc_indio_dev
,
1365 &iio_pollfunc_store_time
,
1366 kmx61_trigger_handler
,
1369 dev_err(&data
->client
->dev
,
1370 "Failed to setup acc triggered buffer\n");
1371 goto err_trigger_unregister_motion
;
1374 ret
= iio_triggered_buffer_setup(data
->mag_indio_dev
,
1375 &iio_pollfunc_store_time
,
1376 kmx61_trigger_handler
,
1379 dev_err(&data
->client
->dev
,
1380 "Failed to setup mag triggered buffer\n");
1381 goto err_buffer_cleanup_acc
;
1385 ret
= pm_runtime_set_active(&client
->dev
);
1387 goto err_buffer_cleanup_mag
;
1389 pm_runtime_enable(&client
->dev
);
1390 pm_runtime_set_autosuspend_delay(&client
->dev
, KMX61_SLEEP_DELAY_MS
);
1391 pm_runtime_use_autosuspend(&client
->dev
);
1393 ret
= iio_device_register(data
->acc_indio_dev
);
1395 dev_err(&client
->dev
, "Failed to register acc iio device\n");
1396 goto err_buffer_cleanup_mag
;
1399 ret
= iio_device_register(data
->mag_indio_dev
);
1401 dev_err(&client
->dev
, "Failed to register mag iio device\n");
1402 goto err_iio_unregister_acc
;
1407 err_iio_unregister_acc
:
1408 iio_device_unregister(data
->acc_indio_dev
);
1409 err_buffer_cleanup_mag
:
1410 if (client
->irq
> 0)
1411 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1412 err_buffer_cleanup_acc
:
1413 if (client
->irq
> 0)
1414 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1415 err_trigger_unregister_motion
:
1416 iio_trigger_unregister(data
->motion_trig
);
1417 err_trigger_unregister_mag_dready
:
1418 iio_trigger_unregister(data
->mag_dready_trig
);
1419 err_trigger_unregister_acc_dready
:
1420 iio_trigger_unregister(data
->acc_dready_trig
);
1422 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1426 static int kmx61_remove(struct i2c_client
*client
)
1428 struct kmx61_data
*data
= i2c_get_clientdata(client
);
1430 iio_device_unregister(data
->acc_indio_dev
);
1431 iio_device_unregister(data
->mag_indio_dev
);
1433 pm_runtime_disable(&client
->dev
);
1434 pm_runtime_set_suspended(&client
->dev
);
1435 pm_runtime_put_noidle(&client
->dev
);
1437 if (client
->irq
> 0) {
1438 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1439 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1440 iio_trigger_unregister(data
->acc_dready_trig
);
1441 iio_trigger_unregister(data
->mag_dready_trig
);
1442 iio_trigger_unregister(data
->motion_trig
);
1445 mutex_lock(&data
->lock
);
1446 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1447 mutex_unlock(&data
->lock
);
1452 #ifdef CONFIG_PM_SLEEP
1453 static int kmx61_suspend(struct device
*dev
)
1456 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1458 mutex_lock(&data
->lock
);
1459 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
1461 mutex_unlock(&data
->lock
);
1466 static int kmx61_resume(struct device
*dev
)
1469 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1472 stby
|= KMX61_ACC_STBY_BIT
;
1474 stby
|= KMX61_MAG_STBY_BIT
;
1476 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1481 static int kmx61_runtime_suspend(struct device
*dev
)
1483 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1486 mutex_lock(&data
->lock
);
1487 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1488 mutex_unlock(&data
->lock
);
1493 static int kmx61_runtime_resume(struct device
*dev
)
1495 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1499 stby
|= KMX61_ACC_STBY_BIT
;
1501 stby
|= KMX61_MAG_STBY_BIT
;
1503 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1507 static const struct dev_pm_ops kmx61_pm_ops
= {
1508 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend
, kmx61_resume
)
1509 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend
, kmx61_runtime_resume
, NULL
)
1512 static const struct acpi_device_id kmx61_acpi_match
[] = {
1517 MODULE_DEVICE_TABLE(acpi
, kmx61_acpi_match
);
1519 static const struct i2c_device_id kmx61_id
[] = {
1524 MODULE_DEVICE_TABLE(i2c
, kmx61_id
);
1526 static struct i2c_driver kmx61_driver
= {
1528 .name
= KMX61_DRV_NAME
,
1529 .acpi_match_table
= ACPI_PTR(kmx61_acpi_match
),
1530 .pm
= &kmx61_pm_ops
,
1532 .probe
= kmx61_probe
,
1533 .remove
= kmx61_remove
,
1534 .id_table
= kmx61_id
,
1537 module_i2c_driver(kmx61_driver
);
1539 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1540 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1541 MODULE_LICENSE("GPL v2");