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 void kmx61_trig_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");
1077 static const struct iio_trigger_ops kmx61_trigger_ops
= {
1078 .set_trigger_state
= kmx61_data_rdy_trigger_set_state
,
1079 .reenable
= kmx61_trig_reenable
,
1082 static irqreturn_t
kmx61_event_handler(int irq
, void *private)
1084 struct kmx61_data
*data
= private;
1085 struct iio_dev
*indio_dev
= data
->acc_indio_dev
;
1088 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS1
);
1090 dev_err(&data
->client
->dev
, "Error reading reg_ins1\n");
1094 if (ret
& KMX61_REG_INS1_BIT_WUFS
) {
1095 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS2
);
1097 dev_err(&data
->client
->dev
, "Error reading reg_ins2\n");
1101 if (ret
& KMX61_REG_INS2_BIT_XN
)
1102 iio_push_event(indio_dev
,
1103 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1107 IIO_EV_DIR_FALLING
),
1110 if (ret
& KMX61_REG_INS2_BIT_XP
)
1111 iio_push_event(indio_dev
,
1112 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1119 if (ret
& KMX61_REG_INS2_BIT_YN
)
1120 iio_push_event(indio_dev
,
1121 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1125 IIO_EV_DIR_FALLING
),
1128 if (ret
& KMX61_REG_INS2_BIT_YP
)
1129 iio_push_event(indio_dev
,
1130 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1137 if (ret
& KMX61_REG_INS2_BIT_ZN
)
1138 iio_push_event(indio_dev
,
1139 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1143 IIO_EV_DIR_FALLING
),
1146 if (ret
& KMX61_REG_INS2_BIT_ZP
)
1147 iio_push_event(indio_dev
,
1148 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1157 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
1159 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
1161 ret
|= KMX61_REG_CTRL1_BIT_RES
;
1162 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
1164 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
1166 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1168 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1173 static irqreturn_t
kmx61_data_rdy_trig_poll(int irq
, void *private)
1175 struct kmx61_data
*data
= private;
1177 if (data
->acc_dready_trig_on
)
1178 iio_trigger_poll(data
->acc_dready_trig
);
1179 if (data
->mag_dready_trig_on
)
1180 iio_trigger_poll(data
->mag_dready_trig
);
1182 if (data
->motion_trig_on
)
1183 iio_trigger_poll(data
->motion_trig
);
1185 if (data
->ev_enable_state
)
1186 return IRQ_WAKE_THREAD
;
1190 static irqreturn_t
kmx61_trigger_handler(int irq
, void *p
)
1192 struct iio_poll_func
*pf
= p
;
1193 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1194 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1195 int bit
, ret
, i
= 0;
1199 if (indio_dev
== data
->acc_indio_dev
)
1200 base
= KMX61_ACC_XOUT_L
;
1202 base
= KMX61_MAG_XOUT_L
;
1204 mutex_lock(&data
->lock
);
1205 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1206 indio_dev
->masklength
) {
1207 ret
= kmx61_read_measurement(data
, base
, bit
);
1209 mutex_unlock(&data
->lock
);
1214 mutex_unlock(&data
->lock
);
1216 iio_push_to_buffers(indio_dev
, buffer
);
1218 iio_trigger_notify_done(indio_dev
->trig
);
1223 static const char *kmx61_match_acpi_device(struct device
*dev
)
1225 const struct acpi_device_id
*id
;
1227 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1230 return dev_name(dev
);
1233 static struct iio_dev
*kmx61_indiodev_setup(struct kmx61_data
*data
,
1234 const struct iio_info
*info
,
1235 const struct iio_chan_spec
*chan
,
1239 struct iio_dev
*indio_dev
;
1241 indio_dev
= devm_iio_device_alloc(&data
->client
->dev
, sizeof(data
));
1243 return ERR_PTR(-ENOMEM
);
1245 kmx61_set_data(indio_dev
, data
);
1247 indio_dev
->channels
= chan
;
1248 indio_dev
->num_channels
= num_channels
;
1249 indio_dev
->name
= name
;
1250 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1251 indio_dev
->info
= info
;
1256 static struct iio_trigger
*kmx61_trigger_setup(struct kmx61_data
*data
,
1257 struct iio_dev
*indio_dev
,
1260 struct iio_trigger
*trig
;
1263 trig
= devm_iio_trigger_alloc(&data
->client
->dev
,
1269 return ERR_PTR(-ENOMEM
);
1271 trig
->dev
.parent
= &data
->client
->dev
;
1272 trig
->ops
= &kmx61_trigger_ops
;
1273 iio_trigger_set_drvdata(trig
, indio_dev
);
1275 ret
= iio_trigger_register(trig
);
1277 return ERR_PTR(ret
);
1282 static int kmx61_probe(struct i2c_client
*client
,
1283 const struct i2c_device_id
*id
)
1286 struct kmx61_data
*data
;
1287 const char *name
= NULL
;
1289 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1293 i2c_set_clientdata(client
, data
);
1294 data
->client
= client
;
1296 mutex_init(&data
->lock
);
1300 else if (ACPI_HANDLE(&client
->dev
))
1301 name
= kmx61_match_acpi_device(&client
->dev
);
1305 data
->acc_indio_dev
=
1306 kmx61_indiodev_setup(data
, &kmx61_acc_info
,
1308 ARRAY_SIZE(kmx61_acc_channels
),
1310 if (IS_ERR(data
->acc_indio_dev
))
1311 return PTR_ERR(data
->acc_indio_dev
);
1313 data
->mag_indio_dev
=
1314 kmx61_indiodev_setup(data
, &kmx61_mag_info
,
1316 ARRAY_SIZE(kmx61_mag_channels
),
1318 if (IS_ERR(data
->mag_indio_dev
))
1319 return PTR_ERR(data
->mag_indio_dev
);
1321 ret
= kmx61_chip_init(data
);
1325 if (client
->irq
> 0) {
1326 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1327 kmx61_data_rdy_trig_poll
,
1328 kmx61_event_handler
,
1329 IRQF_TRIGGER_RISING
,
1333 goto err_chip_uninit
;
1335 data
->acc_dready_trig
=
1336 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1338 if (IS_ERR(data
->acc_dready_trig
)) {
1339 ret
= PTR_ERR(data
->acc_dready_trig
);
1340 goto err_chip_uninit
;
1343 data
->mag_dready_trig
=
1344 kmx61_trigger_setup(data
, data
->mag_indio_dev
,
1346 if (IS_ERR(data
->mag_dready_trig
)) {
1347 ret
= PTR_ERR(data
->mag_dready_trig
);
1348 goto err_trigger_unregister_acc_dready
;
1352 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1354 if (IS_ERR(data
->motion_trig
)) {
1355 ret
= PTR_ERR(data
->motion_trig
);
1356 goto err_trigger_unregister_mag_dready
;
1359 ret
= iio_triggered_buffer_setup(data
->acc_indio_dev
,
1360 &iio_pollfunc_store_time
,
1361 kmx61_trigger_handler
,
1364 dev_err(&data
->client
->dev
,
1365 "Failed to setup acc triggered buffer\n");
1366 goto err_trigger_unregister_motion
;
1369 ret
= iio_triggered_buffer_setup(data
->mag_indio_dev
,
1370 &iio_pollfunc_store_time
,
1371 kmx61_trigger_handler
,
1374 dev_err(&data
->client
->dev
,
1375 "Failed to setup mag triggered buffer\n");
1376 goto err_buffer_cleanup_acc
;
1380 ret
= pm_runtime_set_active(&client
->dev
);
1382 goto err_buffer_cleanup_mag
;
1384 pm_runtime_enable(&client
->dev
);
1385 pm_runtime_set_autosuspend_delay(&client
->dev
, KMX61_SLEEP_DELAY_MS
);
1386 pm_runtime_use_autosuspend(&client
->dev
);
1388 ret
= iio_device_register(data
->acc_indio_dev
);
1390 dev_err(&client
->dev
, "Failed to register acc iio device\n");
1391 goto err_buffer_cleanup_mag
;
1394 ret
= iio_device_register(data
->mag_indio_dev
);
1396 dev_err(&client
->dev
, "Failed to register mag iio device\n");
1397 goto err_iio_unregister_acc
;
1402 err_iio_unregister_acc
:
1403 iio_device_unregister(data
->acc_indio_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
);
1430 pm_runtime_put_noidle(&client
->dev
);
1432 if (client
->irq
> 0) {
1433 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1434 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1435 iio_trigger_unregister(data
->acc_dready_trig
);
1436 iio_trigger_unregister(data
->mag_dready_trig
);
1437 iio_trigger_unregister(data
->motion_trig
);
1440 mutex_lock(&data
->lock
);
1441 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1442 mutex_unlock(&data
->lock
);
1447 #ifdef CONFIG_PM_SLEEP
1448 static int kmx61_suspend(struct device
*dev
)
1451 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1453 mutex_lock(&data
->lock
);
1454 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
1456 mutex_unlock(&data
->lock
);
1461 static int kmx61_resume(struct device
*dev
)
1464 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1467 stby
|= KMX61_ACC_STBY_BIT
;
1469 stby
|= KMX61_MAG_STBY_BIT
;
1471 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1476 static int kmx61_runtime_suspend(struct device
*dev
)
1478 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1481 mutex_lock(&data
->lock
);
1482 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1483 mutex_unlock(&data
->lock
);
1488 static int kmx61_runtime_resume(struct device
*dev
)
1490 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1494 stby
|= KMX61_ACC_STBY_BIT
;
1496 stby
|= KMX61_MAG_STBY_BIT
;
1498 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1502 static const struct dev_pm_ops kmx61_pm_ops
= {
1503 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend
, kmx61_resume
)
1504 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend
, kmx61_runtime_resume
, NULL
)
1507 static const struct acpi_device_id kmx61_acpi_match
[] = {
1512 MODULE_DEVICE_TABLE(acpi
, kmx61_acpi_match
);
1514 static const struct i2c_device_id kmx61_id
[] = {
1519 MODULE_DEVICE_TABLE(i2c
, kmx61_id
);
1521 static struct i2c_driver kmx61_driver
= {
1523 .name
= KMX61_DRV_NAME
,
1524 .acpi_match_table
= ACPI_PTR(kmx61_acpi_match
),
1525 .pm
= &kmx61_pm_ops
,
1527 .probe
= kmx61_probe
,
1528 .remove
= kmx61_remove
,
1529 .id_table
= kmx61_id
,
1532 module_i2c_driver(kmx61_driver
);
1534 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1535 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1536 MODULE_LICENSE("GPL v2");