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/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
15 #include <linux/pm_runtime.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/events.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/trigger_consumer.h>
25 #define KMX61_DRV_NAME "kmx61"
26 #define KMX61_IRQ_NAME "kmx61_event"
28 #define KMX61_REG_WHO_AM_I 0x00
29 #define KMX61_REG_INS1 0x01
30 #define KMX61_REG_INS2 0x02
33 * three 16-bit accelerometer output registers for X/Y/Z axis
34 * we use only XOUT_L as a base register, all other addresses
35 * can be obtained by applying an offset and are provided here
38 #define KMX61_ACC_XOUT_L 0x0A
39 #define KMX61_ACC_XOUT_H 0x0B
40 #define KMX61_ACC_YOUT_L 0x0C
41 #define KMX61_ACC_YOUT_H 0x0D
42 #define KMX61_ACC_ZOUT_L 0x0E
43 #define KMX61_ACC_ZOUT_H 0x0F
46 * one 16-bit temperature output register
48 #define KMX61_TEMP_L 0x10
49 #define KMX61_TEMP_H 0x11
52 * three 16-bit magnetometer output registers for X/Y/Z axis
54 #define KMX61_MAG_XOUT_L 0x12
55 #define KMX61_MAG_XOUT_H 0x13
56 #define KMX61_MAG_YOUT_L 0x14
57 #define KMX61_MAG_YOUT_H 0x15
58 #define KMX61_MAG_ZOUT_L 0x16
59 #define KMX61_MAG_ZOUT_H 0x17
61 #define KMX61_REG_INL 0x28
62 #define KMX61_REG_STBY 0x29
63 #define KMX61_REG_CTRL1 0x2A
64 #define KMX61_REG_CTRL2 0x2B
65 #define KMX61_REG_ODCNTL 0x2C
66 #define KMX61_REG_INC1 0x2D
68 #define KMX61_REG_WUF_THRESH 0x3D
69 #define KMX61_REG_WUF_TIMER 0x3E
71 #define KMX61_ACC_STBY_BIT BIT(0)
72 #define KMX61_MAG_STBY_BIT BIT(1)
73 #define KMX61_ACT_STBY_BIT BIT(7)
75 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
77 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
79 #define KMX61_REG_INS2_BIT_ZP BIT(0)
80 #define KMX61_REG_INS2_BIT_ZN BIT(1)
81 #define KMX61_REG_INS2_BIT_YP BIT(2)
82 #define KMX61_REG_INS2_BIT_YN BIT(3)
83 #define KMX61_REG_INS2_BIT_XP BIT(4)
84 #define KMX61_REG_INS2_BIT_XN BIT(5)
86 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
88 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
89 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
90 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
91 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
93 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
94 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
95 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
96 #define KMX61_REG_INC1_BIT_IEN BIT(5)
98 #define KMX61_ACC_ODR_SHIFT 0
99 #define KMX61_MAG_ODR_SHIFT 4
100 #define KMX61_ACC_ODR_MASK 0x0F
101 #define KMX61_MAG_ODR_MASK 0xF0
103 #define KMX61_OWUF_MASK 0x7
105 #define KMX61_DEFAULT_WAKE_THRESH 1
106 #define KMX61_DEFAULT_WAKE_DURATION 1
108 #define KMX61_SLEEP_DELAY_MS 2000
110 #define KMX61_CHIP_ID 0x12
113 #define KMX61_ACC 0x01
114 #define KMX61_MAG 0x02
117 struct i2c_client
*client
;
119 /* serialize access to non-atomic ops, e.g set_mode */
136 /* accelerometer specific data */
137 struct iio_dev
*acc_indio_dev
;
138 struct iio_trigger
*acc_dready_trig
;
139 struct iio_trigger
*motion_trig
;
140 bool acc_dready_trig_on
;
142 bool ev_enable_state
;
144 /* magnetometer specific data */
145 struct iio_dev
*mag_indio_dev
;
146 struct iio_trigger
*mag_dready_trig
;
147 bool mag_dready_trig_on
;
162 static const u16 kmx61_uscale_table
[] = {9582, 19163, 38326};
164 static const struct {
167 } kmx61_samp_freq_table
[] = { {12, 500000},
180 static const struct {
184 } kmx61_wake_up_odr_table
[] = { {0, 781000, 0x00},
197 static IIO_CONST_ATTR(accel_scale_available
, "0.009582 0.019163 0.038326");
198 static IIO_CONST_ATTR(magn_scale_available
, "0.001465");
199 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
200 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
202 static struct attribute
*kmx61_acc_attributes
[] = {
203 &iio_const_attr_accel_scale_available
.dev_attr
.attr
,
204 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
208 static struct attribute
*kmx61_mag_attributes
[] = {
209 &iio_const_attr_magn_scale_available
.dev_attr
.attr
,
210 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
214 static const struct attribute_group kmx61_acc_attribute_group
= {
215 .attrs
= kmx61_acc_attributes
,
218 static const struct attribute_group kmx61_mag_attribute_group
= {
219 .attrs
= kmx61_mag_attributes
,
222 static const struct iio_event_spec kmx61_event
= {
223 .type
= IIO_EV_TYPE_THRESH
,
224 .dir
= IIO_EV_DIR_EITHER
,
225 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
226 BIT(IIO_EV_INFO_ENABLE
) |
227 BIT(IIO_EV_INFO_PERIOD
),
230 #define KMX61_ACC_CHAN(_axis) { \
233 .channel2 = IIO_MOD_ ## _axis, \
234 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
235 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
236 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
237 .address = KMX61_ACC, \
238 .scan_index = KMX61_AXIS_ ## _axis, \
244 .endianness = IIO_LE, \
246 .event_spec = &kmx61_event, \
247 .num_event_specs = 1 \
250 #define KMX61_MAG_CHAN(_axis) { \
253 .channel2 = IIO_MOD_ ## _axis, \
254 .address = KMX61_MAG, \
255 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
256 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
257 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
258 .scan_index = KMX61_AXIS_ ## _axis, \
264 .endianness = IIO_LE, \
268 static const struct iio_chan_spec kmx61_acc_channels
[] = {
274 static const struct iio_chan_spec kmx61_mag_channels
[] = {
280 static void kmx61_set_data(struct iio_dev
*indio_dev
, struct kmx61_data
*data
)
282 struct kmx61_data
**priv
= iio_priv(indio_dev
);
287 static struct kmx61_data
*kmx61_get_data(struct iio_dev
*indio_dev
)
289 return *(struct kmx61_data
**)iio_priv(indio_dev
);
292 static int kmx61_convert_freq_to_bit(int val
, int val2
)
296 for (i
= 0; i
< ARRAY_SIZE(kmx61_samp_freq_table
); i
++)
297 if (val
== kmx61_samp_freq_table
[i
].val
&&
298 val2
== kmx61_samp_freq_table
[i
].val2
)
303 static int kmx61_convert_wake_up_odr_to_bit(int val
, int val2
)
307 for (i
= 0; i
< ARRAY_SIZE(kmx61_wake_up_odr_table
); ++i
)
308 if (kmx61_wake_up_odr_table
[i
].val
== val
&&
309 kmx61_wake_up_odr_table
[i
].val2
== val2
)
310 return kmx61_wake_up_odr_table
[i
].odr_bits
;
315 * kmx61_set_mode() - set KMX61 device operating mode
316 * @data: kmx61 device private data pointer
317 * @mode: bitmask, indicating operating mode for @device
318 * @device: bitmask, indicating device for which @mode needs to be set
319 * @update: update stby bits stored in device's private @data
321 * For each sensor (accelerometer/magnetometer) there are two operating modes
322 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
323 * if they are both enabled. Internal sensors state is saved in acc_stby and
324 * mag_stby members of driver's private @data.
326 static int kmx61_set_mode(struct kmx61_data
*data
, u8 mode
, u8 device
,
330 int acc_stby
= -1, mag_stby
= -1;
332 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_STBY
);
334 dev_err(&data
->client
->dev
, "Error reading reg_stby\n");
337 if (device
& KMX61_ACC
) {
338 if (mode
& KMX61_ACC_STBY_BIT
) {
339 ret
|= KMX61_ACC_STBY_BIT
;
342 ret
&= ~KMX61_ACC_STBY_BIT
;
347 if (device
& KMX61_MAG
) {
348 if (mode
& KMX61_MAG_STBY_BIT
) {
349 ret
|= KMX61_MAG_STBY_BIT
;
352 ret
&= ~KMX61_MAG_STBY_BIT
;
357 if (mode
& KMX61_ACT_STBY_BIT
)
358 ret
|= KMX61_ACT_STBY_BIT
;
360 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_STBY
, ret
);
362 dev_err(&data
->client
->dev
, "Error writing reg_stby\n");
366 if (acc_stby
!= -1 && update
)
367 data
->acc_stby
= acc_stby
;
368 if (mag_stby
!= -1 && update
)
369 data
->mag_stby
= mag_stby
;
374 static int kmx61_get_mode(struct kmx61_data
*data
, u8
*mode
, u8 device
)
378 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_STBY
);
380 dev_err(&data
->client
->dev
, "Error reading reg_stby\n");
385 if (device
& KMX61_ACC
) {
386 if (ret
& KMX61_ACC_STBY_BIT
)
387 *mode
|= KMX61_ACC_STBY_BIT
;
389 *mode
&= ~KMX61_ACC_STBY_BIT
;
392 if (device
& KMX61_MAG
) {
393 if (ret
& KMX61_MAG_STBY_BIT
)
394 *mode
|= KMX61_MAG_STBY_BIT
;
396 *mode
&= ~KMX61_MAG_STBY_BIT
;
402 static int kmx61_set_wake_up_odr(struct kmx61_data
*data
, int val
, int val2
)
406 odr_bits
= kmx61_convert_wake_up_odr_to_bit(val
, val2
);
410 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL2
,
413 dev_err(&data
->client
->dev
, "Error writing reg_ctrl2\n");
417 static int kmx61_set_odr(struct kmx61_data
*data
, int val
, int val2
, u8 device
)
421 int lodr_bits
, odr_bits
;
423 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
427 lodr_bits
= kmx61_convert_freq_to_bit(val
, val2
);
431 /* To change ODR, accel and magn must be in STDBY */
432 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
438 if (device
& KMX61_ACC
)
439 odr_bits
|= lodr_bits
<< KMX61_ACC_ODR_SHIFT
;
440 if (device
& KMX61_MAG
)
441 odr_bits
|= lodr_bits
<< KMX61_MAG_ODR_SHIFT
;
443 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_ODCNTL
,
448 data
->odr_bits
= odr_bits
;
450 if (device
& KMX61_ACC
) {
451 ret
= kmx61_set_wake_up_odr(data
, val
, val2
);
456 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
459 static int kmx61_get_odr(struct kmx61_data
*data
, int *val
, int *val2
,
464 if (device
& KMX61_ACC
)
465 lodr_bits
= (data
->odr_bits
>> KMX61_ACC_ODR_SHIFT
) &
467 else if (device
& KMX61_MAG
)
468 lodr_bits
= (data
->odr_bits
>> KMX61_MAG_ODR_SHIFT
) &
473 if (lodr_bits
>= ARRAY_SIZE(kmx61_samp_freq_table
))
476 *val
= kmx61_samp_freq_table
[lodr_bits
].val
;
477 *val2
= kmx61_samp_freq_table
[lodr_bits
].val2
;
482 static int kmx61_set_range(struct kmx61_data
*data
, u8 range
)
486 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
488 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
492 ret
&= ~KMX61_REG_CTRL1_GSEL_MASK
;
493 ret
|= range
& KMX61_REG_CTRL1_GSEL_MASK
;
495 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
497 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
506 static int kmx61_set_scale(struct kmx61_data
*data
, u16 uscale
)
511 for (i
= 0; i
< ARRAY_SIZE(kmx61_uscale_table
); i
++) {
512 if (kmx61_uscale_table
[i
] == uscale
) {
513 ret
= kmx61_get_mode(data
, &mode
,
514 KMX61_ACC
| KMX61_MAG
);
518 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
,
519 KMX61_ACC
| KMX61_MAG
, true);
523 ret
= kmx61_set_range(data
, i
);
527 return kmx61_set_mode(data
, mode
,
528 KMX61_ACC
| KMX61_MAG
, true);
534 static int kmx61_chip_init(struct kmx61_data
*data
)
538 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_WHO_AM_I
);
540 dev_err(&data
->client
->dev
, "Error reading who_am_i\n");
544 if (ret
!= KMX61_CHIP_ID
) {
545 dev_err(&data
->client
->dev
,
546 "Wrong chip id, got %x expected %x\n",
551 /* set accel 12bit, 4g range */
552 ret
= kmx61_set_range(data
, KMX61_RANGE_4G
);
556 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_ODCNTL
);
558 dev_err(&data
->client
->dev
, "Error reading reg_odcntl\n");
561 data
->odr_bits
= ret
;
564 * set output data rate for wake up (motion detection) function
565 * to match data rate for accelerometer sampling
567 ret
= kmx61_get_odr(data
, &val
, &val2
, KMX61_ACC
);
571 ret
= kmx61_set_wake_up_odr(data
, val
, val2
);
575 /* set acc/magn to OPERATION mode */
576 ret
= kmx61_set_mode(data
, 0, KMX61_ACC
| KMX61_MAG
, true);
580 data
->wake_thresh
= KMX61_DEFAULT_WAKE_THRESH
;
581 data
->wake_duration
= KMX61_DEFAULT_WAKE_DURATION
;
586 static int kmx61_setup_new_data_interrupt(struct kmx61_data
*data
,
587 bool status
, u8 device
)
592 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
596 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
600 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INC1
);
602 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
607 ret
|= KMX61_REG_INC1_BIT_IEN
;
608 if (device
& KMX61_ACC
)
609 ret
|= KMX61_REG_INC1_BIT_DRDYA
;
610 if (device
& KMX61_MAG
)
611 ret
|= KMX61_REG_INC1_BIT_DRDYM
;
613 ret
&= ~KMX61_REG_INC1_BIT_IEN
;
614 if (device
& KMX61_ACC
)
615 ret
&= ~KMX61_REG_INC1_BIT_DRDYA
;
616 if (device
& KMX61_MAG
)
617 ret
&= ~KMX61_REG_INC1_BIT_DRDYM
;
619 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_INC1
, ret
);
621 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
625 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
627 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
632 ret
|= KMX61_REG_CTRL1_BIT_DRDYE
;
634 ret
&= ~KMX61_REG_CTRL1_BIT_DRDYE
;
636 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
638 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
642 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
645 static int kmx61_chip_update_thresholds(struct kmx61_data
*data
)
649 ret
= i2c_smbus_write_byte_data(data
->client
,
651 data
->wake_duration
);
653 dev_err(&data
->client
->dev
, "Error writing reg_wuf_timer\n");
657 ret
= i2c_smbus_write_byte_data(data
->client
,
658 KMX61_REG_WUF_THRESH
,
661 dev_err(&data
->client
->dev
, "Error writing reg_wuf_thresh\n");
666 static int kmx61_setup_any_motion_interrupt(struct kmx61_data
*data
,
672 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
676 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
680 ret
= kmx61_chip_update_thresholds(data
);
684 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INC1
);
686 dev_err(&data
->client
->dev
, "Error reading reg_inc1\n");
690 ret
|= (KMX61_REG_INC1_BIT_IEN
| KMX61_REG_INC1_BIT_WUFS
);
692 ret
&= ~(KMX61_REG_INC1_BIT_IEN
| KMX61_REG_INC1_BIT_WUFS
);
694 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_INC1
, ret
);
696 dev_err(&data
->client
->dev
, "Error writing reg_inc1\n");
700 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
702 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
707 ret
|= KMX61_REG_CTRL1_BIT_WUFE
| KMX61_REG_CTRL1_BIT_BTSE
;
709 ret
&= ~(KMX61_REG_CTRL1_BIT_WUFE
| KMX61_REG_CTRL1_BIT_BTSE
);
711 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
713 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
716 mode
|= KMX61_ACT_STBY_BIT
;
717 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
721 * kmx61_set_power_state() - set power state for kmx61 @device
722 * @data: kmx61 device private pointer
723 * @on: power state to be set for @device
724 * @device: bitmask indicating device for which @on state needs to be set
726 * Notice that when ACC power state needs to be set to ON and MAG is in
727 * OPERATION then we know that kmx61_runtime_resume was already called
728 * so we must set ACC OPERATION mode here. The same happens when MAG power
729 * state needs to be set to ON and ACC is in OPERATION.
731 static int kmx61_set_power_state(struct kmx61_data
*data
, bool on
, u8 device
)
736 if (device
& KMX61_ACC
) {
737 if (on
&& !data
->acc_ps
&& !data
->mag_stby
) {
738 ret
= kmx61_set_mode(data
, 0, KMX61_ACC
, true);
744 if (device
& KMX61_MAG
) {
745 if (on
&& !data
->mag_ps
&& !data
->acc_stby
) {
746 ret
= kmx61_set_mode(data
, 0, KMX61_MAG
, true);
754 ret
= pm_runtime_resume_and_get(&data
->client
->dev
);
756 pm_runtime_mark_last_busy(&data
->client
->dev
);
757 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
760 dev_err(&data
->client
->dev
,
761 "Failed: kmx61_set_power_state for %d, ret %d\n",
770 static int kmx61_read_measurement(struct kmx61_data
*data
, u8 base
, u8 offset
)
773 u8 reg
= base
+ offset
* 2;
775 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
777 dev_err(&data
->client
->dev
, "failed to read reg at %x\n", reg
);
782 static int kmx61_read_raw(struct iio_dev
*indio_dev
,
783 struct iio_chan_spec
const *chan
, int *val
,
784 int *val2
, long mask
)
788 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
791 case IIO_CHAN_INFO_RAW
:
792 switch (chan
->type
) {
794 base_reg
= KMX61_ACC_XOUT_L
;
797 base_reg
= KMX61_MAG_XOUT_L
;
802 mutex_lock(&data
->lock
);
804 ret
= kmx61_set_power_state(data
, true, chan
->address
);
806 mutex_unlock(&data
->lock
);
810 ret
= kmx61_read_measurement(data
, base_reg
, chan
->scan_index
);
812 kmx61_set_power_state(data
, false, chan
->address
);
813 mutex_unlock(&data
->lock
);
816 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
817 chan
->scan_type
.realbits
- 1);
818 ret
= kmx61_set_power_state(data
, false, chan
->address
);
820 mutex_unlock(&data
->lock
);
824 case IIO_CHAN_INFO_SCALE
:
825 switch (chan
->type
) {
828 *val2
= kmx61_uscale_table
[data
->range
];
829 return IIO_VAL_INT_PLUS_MICRO
;
831 /* 14 bits res, 1465 microGauss per magn count */
834 return IIO_VAL_INT_PLUS_MICRO
;
838 case IIO_CHAN_INFO_SAMP_FREQ
:
839 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
842 mutex_lock(&data
->lock
);
843 ret
= kmx61_get_odr(data
, val
, val2
, chan
->address
);
844 mutex_unlock(&data
->lock
);
847 return IIO_VAL_INT_PLUS_MICRO
;
852 static int kmx61_write_raw(struct iio_dev
*indio_dev
,
853 struct iio_chan_spec
const *chan
, int val
,
857 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
860 case IIO_CHAN_INFO_SAMP_FREQ
:
861 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
864 mutex_lock(&data
->lock
);
865 ret
= kmx61_set_odr(data
, val
, val2
, chan
->address
);
866 mutex_unlock(&data
->lock
);
868 case IIO_CHAN_INFO_SCALE
:
869 switch (chan
->type
) {
873 mutex_lock(&data
->lock
);
874 ret
= kmx61_set_scale(data
, val2
);
875 mutex_unlock(&data
->lock
);
885 static int kmx61_read_event(struct iio_dev
*indio_dev
,
886 const struct iio_chan_spec
*chan
,
887 enum iio_event_type type
,
888 enum iio_event_direction dir
,
889 enum iio_event_info info
,
892 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
896 case IIO_EV_INFO_VALUE
:
897 *val
= data
->wake_thresh
;
899 case IIO_EV_INFO_PERIOD
:
900 *val
= data
->wake_duration
;
907 static int kmx61_write_event(struct iio_dev
*indio_dev
,
908 const struct iio_chan_spec
*chan
,
909 enum iio_event_type type
,
910 enum iio_event_direction dir
,
911 enum iio_event_info info
,
914 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
916 if (data
->ev_enable_state
)
920 case IIO_EV_INFO_VALUE
:
921 data
->wake_thresh
= val
;
923 case IIO_EV_INFO_PERIOD
:
924 data
->wake_duration
= val
;
931 static int kmx61_read_event_config(struct iio_dev
*indio_dev
,
932 const struct iio_chan_spec
*chan
,
933 enum iio_event_type type
,
934 enum iio_event_direction dir
)
936 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
938 return data
->ev_enable_state
;
941 static int kmx61_write_event_config(struct iio_dev
*indio_dev
,
942 const struct iio_chan_spec
*chan
,
943 enum iio_event_type type
,
944 enum iio_event_direction dir
,
947 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
950 if (state
&& data
->ev_enable_state
)
953 mutex_lock(&data
->lock
);
955 if (!state
&& data
->motion_trig_on
) {
956 data
->ev_enable_state
= false;
960 ret
= kmx61_set_power_state(data
, state
, KMX61_ACC
);
964 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
966 kmx61_set_power_state(data
, false, KMX61_ACC
);
970 data
->ev_enable_state
= state
;
973 mutex_unlock(&data
->lock
);
978 static int kmx61_acc_validate_trigger(struct iio_dev
*indio_dev
,
979 struct iio_trigger
*trig
)
981 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
983 if (data
->acc_dready_trig
!= trig
&& data
->motion_trig
!= trig
)
989 static int kmx61_mag_validate_trigger(struct iio_dev
*indio_dev
,
990 struct iio_trigger
*trig
)
992 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
994 if (data
->mag_dready_trig
!= trig
)
1000 static const struct iio_info kmx61_acc_info
= {
1001 .read_raw
= kmx61_read_raw
,
1002 .write_raw
= kmx61_write_raw
,
1003 .attrs
= &kmx61_acc_attribute_group
,
1004 .read_event_value
= kmx61_read_event
,
1005 .write_event_value
= kmx61_write_event
,
1006 .read_event_config
= kmx61_read_event_config
,
1007 .write_event_config
= kmx61_write_event_config
,
1008 .validate_trigger
= kmx61_acc_validate_trigger
,
1011 static const struct iio_info kmx61_mag_info
= {
1012 .read_raw
= kmx61_read_raw
,
1013 .write_raw
= kmx61_write_raw
,
1014 .attrs
= &kmx61_mag_attribute_group
,
1015 .validate_trigger
= kmx61_mag_validate_trigger
,
1019 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1025 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1026 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1028 mutex_lock(&data
->lock
);
1030 if (!state
&& data
->ev_enable_state
&& data
->motion_trig_on
) {
1031 data
->motion_trig_on
= false;
1035 if (data
->acc_dready_trig
== trig
|| data
->motion_trig
== trig
)
1040 ret
= kmx61_set_power_state(data
, state
, device
);
1044 if (data
->acc_dready_trig
== trig
|| data
->mag_dready_trig
== trig
)
1045 ret
= kmx61_setup_new_data_interrupt(data
, state
, device
);
1047 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
1049 kmx61_set_power_state(data
, false, device
);
1053 if (data
->acc_dready_trig
== trig
)
1054 data
->acc_dready_trig_on
= state
;
1055 else if (data
->mag_dready_trig
== trig
)
1056 data
->mag_dready_trig_on
= state
;
1058 data
->motion_trig_on
= state
;
1060 mutex_unlock(&data
->lock
);
1065 static void kmx61_trig_reenable(struct iio_trigger
*trig
)
1067 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1068 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1071 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1073 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1076 static const struct iio_trigger_ops kmx61_trigger_ops
= {
1077 .set_trigger_state
= kmx61_data_rdy_trigger_set_state
,
1078 .reenable
= kmx61_trig_reenable
,
1081 static irqreturn_t
kmx61_event_handler(int irq
, void *private)
1083 struct kmx61_data
*data
= private;
1084 struct iio_dev
*indio_dev
= data
->acc_indio_dev
;
1087 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS1
);
1089 dev_err(&data
->client
->dev
, "Error reading reg_ins1\n");
1093 if (ret
& KMX61_REG_INS1_BIT_WUFS
) {
1094 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS2
);
1096 dev_err(&data
->client
->dev
, "Error reading reg_ins2\n");
1100 if (ret
& KMX61_REG_INS2_BIT_XN
)
1101 iio_push_event(indio_dev
,
1102 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1106 IIO_EV_DIR_FALLING
),
1109 if (ret
& KMX61_REG_INS2_BIT_XP
)
1110 iio_push_event(indio_dev
,
1111 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1118 if (ret
& KMX61_REG_INS2_BIT_YN
)
1119 iio_push_event(indio_dev
,
1120 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1124 IIO_EV_DIR_FALLING
),
1127 if (ret
& KMX61_REG_INS2_BIT_YP
)
1128 iio_push_event(indio_dev
,
1129 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1136 if (ret
& KMX61_REG_INS2_BIT_ZN
)
1137 iio_push_event(indio_dev
,
1138 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1142 IIO_EV_DIR_FALLING
),
1145 if (ret
& KMX61_REG_INS2_BIT_ZP
)
1146 iio_push_event(indio_dev
,
1147 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1156 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
1158 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
1160 ret
|= KMX61_REG_CTRL1_BIT_RES
;
1161 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
1163 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
1165 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1167 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1172 static irqreturn_t
kmx61_data_rdy_trig_poll(int irq
, void *private)
1174 struct kmx61_data
*data
= private;
1176 if (data
->acc_dready_trig_on
)
1177 iio_trigger_poll(data
->acc_dready_trig
);
1178 if (data
->mag_dready_trig_on
)
1179 iio_trigger_poll(data
->mag_dready_trig
);
1181 if (data
->motion_trig_on
)
1182 iio_trigger_poll(data
->motion_trig
);
1184 if (data
->ev_enable_state
)
1185 return IRQ_WAKE_THREAD
;
1189 static irqreturn_t
kmx61_trigger_handler(int irq
, void *p
)
1191 struct iio_poll_func
*pf
= p
;
1192 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1193 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1194 int bit
, ret
, i
= 0;
1198 if (indio_dev
== data
->acc_indio_dev
)
1199 base
= KMX61_ACC_XOUT_L
;
1201 base
= KMX61_MAG_XOUT_L
;
1203 mutex_lock(&data
->lock
);
1204 iio_for_each_active_channel(indio_dev
, bit
) {
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 struct iio_dev
*kmx61_indiodev_setup(struct kmx61_data
*data
,
1222 const struct iio_info
*info
,
1223 const struct iio_chan_spec
*chan
,
1227 struct iio_dev
*indio_dev
;
1229 indio_dev
= devm_iio_device_alloc(&data
->client
->dev
, sizeof(data
));
1231 return ERR_PTR(-ENOMEM
);
1233 kmx61_set_data(indio_dev
, data
);
1235 indio_dev
->channels
= chan
;
1236 indio_dev
->num_channels
= num_channels
;
1237 indio_dev
->name
= name
;
1238 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1239 indio_dev
->info
= info
;
1244 static struct iio_trigger
*kmx61_trigger_setup(struct kmx61_data
*data
,
1245 struct iio_dev
*indio_dev
,
1248 struct iio_trigger
*trig
;
1251 trig
= devm_iio_trigger_alloc(&data
->client
->dev
,
1255 iio_device_id(indio_dev
));
1257 return ERR_PTR(-ENOMEM
);
1259 trig
->ops
= &kmx61_trigger_ops
;
1260 iio_trigger_set_drvdata(trig
, indio_dev
);
1262 ret
= iio_trigger_register(trig
);
1264 return ERR_PTR(ret
);
1269 static int kmx61_probe(struct i2c_client
*client
)
1271 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
1273 struct kmx61_data
*data
;
1274 const char *name
= NULL
;
1276 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1280 i2c_set_clientdata(client
, data
);
1281 data
->client
= client
;
1283 mutex_init(&data
->lock
);
1290 data
->acc_indio_dev
=
1291 kmx61_indiodev_setup(data
, &kmx61_acc_info
,
1293 ARRAY_SIZE(kmx61_acc_channels
),
1295 if (IS_ERR(data
->acc_indio_dev
))
1296 return PTR_ERR(data
->acc_indio_dev
);
1298 data
->mag_indio_dev
=
1299 kmx61_indiodev_setup(data
, &kmx61_mag_info
,
1301 ARRAY_SIZE(kmx61_mag_channels
),
1303 if (IS_ERR(data
->mag_indio_dev
))
1304 return PTR_ERR(data
->mag_indio_dev
);
1306 ret
= kmx61_chip_init(data
);
1310 if (client
->irq
> 0) {
1311 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1312 kmx61_data_rdy_trig_poll
,
1313 kmx61_event_handler
,
1314 IRQF_TRIGGER_RISING
,
1318 goto err_chip_uninit
;
1320 data
->acc_dready_trig
=
1321 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1323 if (IS_ERR(data
->acc_dready_trig
)) {
1324 ret
= PTR_ERR(data
->acc_dready_trig
);
1325 goto err_chip_uninit
;
1328 data
->mag_dready_trig
=
1329 kmx61_trigger_setup(data
, data
->mag_indio_dev
,
1331 if (IS_ERR(data
->mag_dready_trig
)) {
1332 ret
= PTR_ERR(data
->mag_dready_trig
);
1333 goto err_trigger_unregister_acc_dready
;
1337 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1339 if (IS_ERR(data
->motion_trig
)) {
1340 ret
= PTR_ERR(data
->motion_trig
);
1341 goto err_trigger_unregister_mag_dready
;
1344 ret
= iio_triggered_buffer_setup(data
->acc_indio_dev
,
1345 &iio_pollfunc_store_time
,
1346 kmx61_trigger_handler
,
1349 dev_err(&data
->client
->dev
,
1350 "Failed to setup acc triggered buffer\n");
1351 goto err_trigger_unregister_motion
;
1354 ret
= iio_triggered_buffer_setup(data
->mag_indio_dev
,
1355 &iio_pollfunc_store_time
,
1356 kmx61_trigger_handler
,
1359 dev_err(&data
->client
->dev
,
1360 "Failed to setup mag triggered buffer\n");
1361 goto err_buffer_cleanup_acc
;
1365 ret
= pm_runtime_set_active(&client
->dev
);
1367 goto err_buffer_cleanup_mag
;
1369 pm_runtime_enable(&client
->dev
);
1370 pm_runtime_set_autosuspend_delay(&client
->dev
, KMX61_SLEEP_DELAY_MS
);
1371 pm_runtime_use_autosuspend(&client
->dev
);
1373 ret
= iio_device_register(data
->acc_indio_dev
);
1375 dev_err(&client
->dev
, "Failed to register acc iio device\n");
1376 goto err_pm_cleanup
;
1379 ret
= iio_device_register(data
->mag_indio_dev
);
1381 dev_err(&client
->dev
, "Failed to register mag iio device\n");
1382 goto err_iio_unregister_acc
;
1387 err_iio_unregister_acc
:
1388 iio_device_unregister(data
->acc_indio_dev
);
1390 pm_runtime_dont_use_autosuspend(&client
->dev
);
1391 pm_runtime_disable(&client
->dev
);
1392 err_buffer_cleanup_mag
:
1393 if (client
->irq
> 0)
1394 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1395 err_buffer_cleanup_acc
:
1396 if (client
->irq
> 0)
1397 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1398 err_trigger_unregister_motion
:
1399 iio_trigger_unregister(data
->motion_trig
);
1400 err_trigger_unregister_mag_dready
:
1401 iio_trigger_unregister(data
->mag_dready_trig
);
1402 err_trigger_unregister_acc_dready
:
1403 iio_trigger_unregister(data
->acc_dready_trig
);
1405 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1409 static void kmx61_remove(struct i2c_client
*client
)
1411 struct kmx61_data
*data
= i2c_get_clientdata(client
);
1413 iio_device_unregister(data
->acc_indio_dev
);
1414 iio_device_unregister(data
->mag_indio_dev
);
1416 pm_runtime_disable(&client
->dev
);
1417 pm_runtime_set_suspended(&client
->dev
);
1419 if (client
->irq
> 0) {
1420 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1421 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1422 iio_trigger_unregister(data
->acc_dready_trig
);
1423 iio_trigger_unregister(data
->mag_dready_trig
);
1424 iio_trigger_unregister(data
->motion_trig
);
1427 mutex_lock(&data
->lock
);
1428 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1429 mutex_unlock(&data
->lock
);
1432 static int kmx61_suspend(struct device
*dev
)
1435 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1437 mutex_lock(&data
->lock
);
1438 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
1440 mutex_unlock(&data
->lock
);
1445 static int kmx61_resume(struct device
*dev
)
1448 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1451 stby
|= KMX61_ACC_STBY_BIT
;
1453 stby
|= KMX61_MAG_STBY_BIT
;
1455 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1458 static int kmx61_runtime_suspend(struct device
*dev
)
1460 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1463 mutex_lock(&data
->lock
);
1464 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1465 mutex_unlock(&data
->lock
);
1470 static int kmx61_runtime_resume(struct device
*dev
)
1472 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1476 stby
|= KMX61_ACC_STBY_BIT
;
1478 stby
|= KMX61_MAG_STBY_BIT
;
1480 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1483 static const struct dev_pm_ops kmx61_pm_ops
= {
1484 SYSTEM_SLEEP_PM_OPS(kmx61_suspend
, kmx61_resume
)
1485 RUNTIME_PM_OPS(kmx61_runtime_suspend
, kmx61_runtime_resume
, NULL
)
1488 static const struct i2c_device_id kmx61_id
[] = {
1493 MODULE_DEVICE_TABLE(i2c
, kmx61_id
);
1495 static struct i2c_driver kmx61_driver
= {
1497 .name
= KMX61_DRV_NAME
,
1498 .pm
= pm_ptr(&kmx61_pm_ops
),
1500 .probe
= kmx61_probe
,
1501 .remove
= kmx61_remove
,
1502 .id_table
= kmx61_id
,
1505 module_i2c_driver(kmx61_driver
);
1507 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1508 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1509 MODULE_LICENSE("GPL v2");