2 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
4 * Copyright (c) 2014, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/acpi.h>
17 #include <linux/interrupt.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/events.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/iio/trigger_consumer.h>
28 #define KMX61_DRV_NAME "kmx61"
29 #define KMX61_IRQ_NAME "kmx61_event"
31 #define KMX61_REG_WHO_AM_I 0x00
32 #define KMX61_REG_INS1 0x01
33 #define KMX61_REG_INS2 0x02
36 * three 16-bit accelerometer output registers for X/Y/Z axis
37 * we use only XOUT_L as a base register, all other addresses
38 * can be obtained by applying an offset and are provided here
41 #define KMX61_ACC_XOUT_L 0x0A
42 #define KMX61_ACC_XOUT_H 0x0B
43 #define KMX61_ACC_YOUT_L 0x0C
44 #define KMX61_ACC_YOUT_H 0x0D
45 #define KMX61_ACC_ZOUT_L 0x0E
46 #define KMX61_ACC_ZOUT_H 0x0F
49 * one 16-bit temperature output register
51 #define KMX61_TEMP_L 0x10
52 #define KMX61_TEMP_H 0x11
55 * three 16-bit magnetometer output registers for X/Y/Z axis
57 #define KMX61_MAG_XOUT_L 0x12
58 #define KMX61_MAG_XOUT_H 0x13
59 #define KMX61_MAG_YOUT_L 0x14
60 #define KMX61_MAG_YOUT_H 0x15
61 #define KMX61_MAG_ZOUT_L 0x16
62 #define KMX61_MAG_ZOUT_H 0x17
64 #define KMX61_REG_INL 0x28
65 #define KMX61_REG_STBY 0x29
66 #define KMX61_REG_CTRL1 0x2A
67 #define KMX61_REG_CTRL2 0x2B
68 #define KMX61_REG_ODCNTL 0x2C
69 #define KMX61_REG_INC1 0x2D
71 #define KMX61_REG_WUF_THRESH 0x3D
72 #define KMX61_REG_WUF_TIMER 0x3E
74 #define KMX61_ACC_STBY_BIT BIT(0)
75 #define KMX61_MAG_STBY_BIT BIT(1)
76 #define KMX61_ACT_STBY_BIT BIT(7)
78 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
80 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
82 #define KMX61_REG_INS2_BIT_ZP BIT(0)
83 #define KMX61_REG_INS2_BIT_ZN BIT(1)
84 #define KMX61_REG_INS2_BIT_YP BIT(2)
85 #define KMX61_REG_INS2_BIT_YN BIT(3)
86 #define KMX61_REG_INS2_BIT_XP BIT(4)
87 #define KMX61_REG_INS2_BIT_XN BIT(5)
89 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
91 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
92 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
93 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
94 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
96 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
97 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
98 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
99 #define KMX61_REG_INC1_BIT_IEN BIT(5)
101 #define KMX61_ACC_ODR_SHIFT 0
102 #define KMX61_MAG_ODR_SHIFT 4
103 #define KMX61_ACC_ODR_MASK 0x0F
104 #define KMX61_MAG_ODR_MASK 0xF0
106 #define KMX61_OWUF_MASK 0x7
108 #define KMX61_DEFAULT_WAKE_THRESH 1
109 #define KMX61_DEFAULT_WAKE_DURATION 1
111 #define KMX61_SLEEP_DELAY_MS 2000
113 #define KMX61_CHIP_ID 0x12
116 #define KMX61_ACC 0x01
117 #define KMX61_MAG 0x02
120 struct i2c_client
*client
;
122 /* serialize access to non-atomic ops, e.g set_mode */
139 /* accelerometer specific data */
140 struct iio_dev
*acc_indio_dev
;
141 struct iio_trigger
*acc_dready_trig
;
142 struct iio_trigger
*motion_trig
;
143 bool acc_dready_trig_on
;
145 bool ev_enable_state
;
147 /* magnetometer specific data */
148 struct iio_dev
*mag_indio_dev
;
149 struct iio_trigger
*mag_dready_trig
;
150 bool mag_dready_trig_on
;
165 static const u16 kmx61_uscale_table
[] = {9582, 19163, 38326};
167 static const struct {
170 } kmx61_samp_freq_table
[] = { {12, 500000},
183 static const struct {
187 } kmx61_wake_up_odr_table
[] = { {0, 781000, 0x00},
200 static IIO_CONST_ATTR(accel_scale_available
, "0.009582 0.019163 0.038326");
201 static IIO_CONST_ATTR(magn_scale_available
, "0.001465");
202 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
203 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
205 static struct attribute
*kmx61_acc_attributes
[] = {
206 &iio_const_attr_accel_scale_available
.dev_attr
.attr
,
207 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
211 static struct attribute
*kmx61_mag_attributes
[] = {
212 &iio_const_attr_magn_scale_available
.dev_attr
.attr
,
213 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
217 static const struct attribute_group kmx61_acc_attribute_group
= {
218 .attrs
= kmx61_acc_attributes
,
221 static const struct attribute_group kmx61_mag_attribute_group
= {
222 .attrs
= kmx61_mag_attributes
,
225 static const struct iio_event_spec kmx61_event
= {
226 .type
= IIO_EV_TYPE_THRESH
,
227 .dir
= IIO_EV_DIR_EITHER
,
228 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
229 BIT(IIO_EV_INFO_ENABLE
) |
230 BIT(IIO_EV_INFO_PERIOD
),
233 #define KMX61_ACC_CHAN(_axis) { \
236 .channel2 = IIO_MOD_ ## _axis, \
237 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
238 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
239 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
240 .address = KMX61_ACC, \
241 .scan_index = KMX61_AXIS_ ## _axis, \
247 .endianness = IIO_LE, \
249 .event_spec = &kmx61_event, \
250 .num_event_specs = 1 \
253 #define KMX61_MAG_CHAN(_axis) { \
256 .channel2 = IIO_MOD_ ## _axis, \
257 .address = KMX61_MAG, \
258 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
259 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
260 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
261 .scan_index = KMX61_AXIS_ ## _axis, \
267 .endianness = IIO_LE, \
271 static const struct iio_chan_spec kmx61_acc_channels
[] = {
277 static const struct iio_chan_spec kmx61_mag_channels
[] = {
283 static void kmx61_set_data(struct iio_dev
*indio_dev
, struct kmx61_data
*data
)
285 struct kmx61_data
**priv
= iio_priv(indio_dev
);
290 static struct kmx61_data
*kmx61_get_data(struct iio_dev
*indio_dev
)
292 return *(struct kmx61_data
**)iio_priv(indio_dev
);
295 static int kmx61_convert_freq_to_bit(int val
, int val2
)
299 for (i
= 0; i
< ARRAY_SIZE(kmx61_samp_freq_table
); i
++)
300 if (val
== kmx61_samp_freq_table
[i
].val
&&
301 val2
== kmx61_samp_freq_table
[i
].val2
)
306 static int kmx61_convert_wake_up_odr_to_bit(int val
, int val2
)
310 for (i
= 0; i
< ARRAY_SIZE(kmx61_wake_up_odr_table
); ++i
)
311 if (kmx61_wake_up_odr_table
[i
].val
== val
&&
312 kmx61_wake_up_odr_table
[i
].val2
== val2
)
313 return kmx61_wake_up_odr_table
[i
].odr_bits
;
318 * kmx61_set_mode() - set KMX61 device operating mode
319 * @data - kmx61 device private data pointer
320 * @mode - bitmask, indicating operating mode for @device
321 * @device - bitmask, indicating device for which @mode needs to be set
322 * @update - update stby bits stored in device's private @data
324 * For each sensor (accelerometer/magnetometer) there are two operating modes
325 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
326 * if they are both enabled. Internal sensors state is saved in acc_stby and
327 * mag_stby members of driver's private @data.
329 static int kmx61_set_mode(struct kmx61_data
*data
, u8 mode
, u8 device
,
333 int acc_stby
= -1, mag_stby
= -1;
335 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_STBY
);
337 dev_err(&data
->client
->dev
, "Error reading reg_stby\n");
340 if (device
& KMX61_ACC
) {
341 if (mode
& KMX61_ACC_STBY_BIT
) {
342 ret
|= KMX61_ACC_STBY_BIT
;
345 ret
&= ~KMX61_ACC_STBY_BIT
;
350 if (device
& KMX61_MAG
) {
351 if (mode
& KMX61_MAG_STBY_BIT
) {
352 ret
|= KMX61_MAG_STBY_BIT
;
355 ret
&= ~KMX61_MAG_STBY_BIT
;
360 if (mode
& KMX61_ACT_STBY_BIT
)
361 ret
|= KMX61_ACT_STBY_BIT
;
363 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_STBY
, ret
);
365 dev_err(&data
->client
->dev
, "Error writing reg_stby\n");
369 if (acc_stby
!= -1 && update
)
370 data
->acc_stby
= acc_stby
;
371 if (mag_stby
!= -1 && update
)
372 data
->mag_stby
= mag_stby
;
377 static int kmx61_get_mode(struct kmx61_data
*data
, u8
*mode
, u8 device
)
381 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_STBY
);
383 dev_err(&data
->client
->dev
, "Error reading reg_stby\n");
388 if (device
& KMX61_ACC
) {
389 if (ret
& KMX61_ACC_STBY_BIT
)
390 *mode
|= KMX61_ACC_STBY_BIT
;
392 *mode
&= ~KMX61_ACC_STBY_BIT
;
395 if (device
& KMX61_MAG
) {
396 if (ret
& KMX61_MAG_STBY_BIT
)
397 *mode
|= KMX61_MAG_STBY_BIT
;
399 *mode
&= ~KMX61_MAG_STBY_BIT
;
405 static int kmx61_set_wake_up_odr(struct kmx61_data
*data
, int val
, int val2
)
409 odr_bits
= kmx61_convert_wake_up_odr_to_bit(val
, val2
);
413 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL2
,
416 dev_err(&data
->client
->dev
, "Error writing reg_ctrl2\n");
420 static int kmx61_set_odr(struct kmx61_data
*data
, int val
, int val2
, u8 device
)
424 int lodr_bits
, odr_bits
;
426 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
430 lodr_bits
= kmx61_convert_freq_to_bit(val
, val2
);
434 /* To change ODR, accel and magn must be in STDBY */
435 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
441 if (device
& KMX61_ACC
)
442 odr_bits
|= lodr_bits
<< KMX61_ACC_ODR_SHIFT
;
443 if (device
& KMX61_MAG
)
444 odr_bits
|= lodr_bits
<< KMX61_MAG_ODR_SHIFT
;
446 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_ODCNTL
,
451 data
->odr_bits
= odr_bits
;
453 if (device
& KMX61_ACC
) {
454 ret
= kmx61_set_wake_up_odr(data
, val
, val2
);
459 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
462 static int kmx61_get_odr(struct kmx61_data
*data
, int *val
, int *val2
,
467 if (device
& KMX61_ACC
)
468 lodr_bits
= (data
->odr_bits
>> KMX61_ACC_ODR_SHIFT
) &
470 else if (device
& KMX61_MAG
)
471 lodr_bits
= (data
->odr_bits
>> KMX61_MAG_ODR_SHIFT
) &
476 if (lodr_bits
>= ARRAY_SIZE(kmx61_samp_freq_table
))
479 *val
= kmx61_samp_freq_table
[lodr_bits
].val
;
480 *val2
= kmx61_samp_freq_table
[lodr_bits
].val2
;
485 static int kmx61_set_range(struct kmx61_data
*data
, u8 range
)
489 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
491 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
495 ret
&= ~KMX61_REG_CTRL1_GSEL_MASK
;
496 ret
|= range
& KMX61_REG_CTRL1_GSEL_MASK
;
498 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
500 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
509 static int kmx61_set_scale(struct kmx61_data
*data
, u16 uscale
)
514 for (i
= 0; i
< ARRAY_SIZE(kmx61_uscale_table
); i
++) {
515 if (kmx61_uscale_table
[i
] == uscale
) {
516 ret
= kmx61_get_mode(data
, &mode
,
517 KMX61_ACC
| KMX61_MAG
);
521 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
,
522 KMX61_ACC
| KMX61_MAG
, true);
526 ret
= kmx61_set_range(data
, i
);
530 return kmx61_set_mode(data
, mode
,
531 KMX61_ACC
| KMX61_MAG
, true);
537 static int kmx61_chip_init(struct kmx61_data
*data
)
541 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_WHO_AM_I
);
543 dev_err(&data
->client
->dev
, "Error reading who_am_i\n");
547 if (ret
!= KMX61_CHIP_ID
) {
548 dev_err(&data
->client
->dev
,
549 "Wrong chip id, got %x expected %x\n",
554 /* set accel 12bit, 4g range */
555 ret
= kmx61_set_range(data
, KMX61_RANGE_4G
);
559 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_ODCNTL
);
561 dev_err(&data
->client
->dev
, "Error reading reg_odcntl\n");
564 data
->odr_bits
= ret
;
567 * set output data rate for wake up (motion detection) function
568 * to match data rate for accelerometer sampling
570 ret
= kmx61_get_odr(data
, &val
, &val2
, KMX61_ACC
);
574 ret
= kmx61_set_wake_up_odr(data
, val
, val2
);
578 /* set acc/magn to OPERATION mode */
579 ret
= kmx61_set_mode(data
, 0, KMX61_ACC
| KMX61_MAG
, true);
583 data
->wake_thresh
= KMX61_DEFAULT_WAKE_THRESH
;
584 data
->wake_duration
= KMX61_DEFAULT_WAKE_DURATION
;
589 static int kmx61_setup_new_data_interrupt(struct kmx61_data
*data
,
590 bool status
, u8 device
)
595 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
599 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
603 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INC1
);
605 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
610 ret
|= KMX61_REG_INC1_BIT_IEN
;
611 if (device
& KMX61_ACC
)
612 ret
|= KMX61_REG_INC1_BIT_DRDYA
;
613 if (device
& KMX61_MAG
)
614 ret
|= KMX61_REG_INC1_BIT_DRDYM
;
616 ret
&= ~KMX61_REG_INC1_BIT_IEN
;
617 if (device
& KMX61_ACC
)
618 ret
&= ~KMX61_REG_INC1_BIT_DRDYA
;
619 if (device
& KMX61_MAG
)
620 ret
&= ~KMX61_REG_INC1_BIT_DRDYM
;
622 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_INC1
, ret
);
624 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
628 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
630 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
635 ret
|= KMX61_REG_CTRL1_BIT_DRDYE
;
637 ret
&= ~KMX61_REG_CTRL1_BIT_DRDYE
;
639 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
641 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
645 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
648 static int kmx61_chip_update_thresholds(struct kmx61_data
*data
)
652 ret
= i2c_smbus_write_byte_data(data
->client
,
654 data
->wake_duration
);
656 dev_err(&data
->client
->dev
, "Errow writing reg_wuf_timer\n");
660 ret
= i2c_smbus_write_byte_data(data
->client
,
661 KMX61_REG_WUF_THRESH
,
664 dev_err(&data
->client
->dev
, "Error writing reg_wuf_thresh\n");
669 static int kmx61_setup_any_motion_interrupt(struct kmx61_data
*data
,
675 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
679 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
683 ret
= kmx61_chip_update_thresholds(data
);
687 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INC1
);
689 dev_err(&data
->client
->dev
, "Error reading reg_inc1\n");
693 ret
|= (KMX61_REG_INC1_BIT_IEN
| KMX61_REG_INC1_BIT_WUFS
);
695 ret
&= ~(KMX61_REG_INC1_BIT_IEN
| KMX61_REG_INC1_BIT_WUFS
);
697 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_INC1
, ret
);
699 dev_err(&data
->client
->dev
, "Error writing reg_inc1\n");
703 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
705 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
710 ret
|= KMX61_REG_CTRL1_BIT_WUFE
| KMX61_REG_CTRL1_BIT_BTSE
;
712 ret
&= ~(KMX61_REG_CTRL1_BIT_WUFE
| KMX61_REG_CTRL1_BIT_BTSE
);
714 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
716 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
719 mode
|= KMX61_ACT_STBY_BIT
;
720 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
724 * kmx61_set_power_state() - set power state for kmx61 @device
725 * @data - kmx61 device private pointer
726 * @on - power state to be set for @device
727 * @device - bitmask indicating device for which @on state needs to be set
729 * Notice that when ACC power state needs to be set to ON and MAG is in
730 * OPERATION then we know that kmx61_runtime_resume was already called
731 * so we must set ACC OPERATION mode here. The same happens when MAG power
732 * state needs to be set to ON and ACC is in OPERATION.
734 static int kmx61_set_power_state(struct kmx61_data
*data
, bool on
, u8 device
)
739 if (device
& KMX61_ACC
) {
740 if (on
&& !data
->acc_ps
&& !data
->mag_stby
) {
741 ret
= kmx61_set_mode(data
, 0, KMX61_ACC
, true);
747 if (device
& KMX61_MAG
) {
748 if (on
&& !data
->mag_ps
&& !data
->acc_stby
) {
749 ret
= kmx61_set_mode(data
, 0, KMX61_MAG
, true);
757 ret
= pm_runtime_get_sync(&data
->client
->dev
);
759 pm_runtime_mark_last_busy(&data
->client
->dev
);
760 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
763 dev_err(&data
->client
->dev
,
764 "Failed: kmx61_set_power_state for %d, ret %d\n",
767 pm_runtime_put_noidle(&data
->client
->dev
);
775 static int kmx61_read_measurement(struct kmx61_data
*data
, u8 base
, u8 offset
)
778 u8 reg
= base
+ offset
* 2;
780 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
782 dev_err(&data
->client
->dev
, "failed to read reg at %x\n", reg
);
787 static int kmx61_read_raw(struct iio_dev
*indio_dev
,
788 struct iio_chan_spec
const *chan
, int *val
,
789 int *val2
, long mask
)
793 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
796 case IIO_CHAN_INFO_RAW
:
797 switch (chan
->type
) {
799 base_reg
= KMX61_ACC_XOUT_L
;
802 base_reg
= KMX61_MAG_XOUT_L
;
807 mutex_lock(&data
->lock
);
809 ret
= kmx61_set_power_state(data
, true, chan
->address
);
811 mutex_unlock(&data
->lock
);
815 ret
= kmx61_read_measurement(data
, base_reg
, chan
->scan_index
);
817 kmx61_set_power_state(data
, false, chan
->address
);
818 mutex_unlock(&data
->lock
);
821 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
822 chan
->scan_type
.realbits
- 1);
823 ret
= kmx61_set_power_state(data
, false, chan
->address
);
825 mutex_unlock(&data
->lock
);
829 case IIO_CHAN_INFO_SCALE
:
830 switch (chan
->type
) {
833 *val2
= kmx61_uscale_table
[data
->range
];
834 return IIO_VAL_INT_PLUS_MICRO
;
836 /* 14 bits res, 1465 microGauss per magn count */
839 return IIO_VAL_INT_PLUS_MICRO
;
843 case IIO_CHAN_INFO_SAMP_FREQ
:
844 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
847 mutex_lock(&data
->lock
);
848 ret
= kmx61_get_odr(data
, val
, val2
, chan
->address
);
849 mutex_unlock(&data
->lock
);
852 return IIO_VAL_INT_PLUS_MICRO
;
857 static int kmx61_write_raw(struct iio_dev
*indio_dev
,
858 struct iio_chan_spec
const *chan
, int val
,
862 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
865 case IIO_CHAN_INFO_SAMP_FREQ
:
866 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
869 mutex_lock(&data
->lock
);
870 ret
= kmx61_set_odr(data
, val
, val2
, chan
->address
);
871 mutex_unlock(&data
->lock
);
873 case IIO_CHAN_INFO_SCALE
:
874 switch (chan
->type
) {
878 mutex_lock(&data
->lock
);
879 ret
= kmx61_set_scale(data
, val2
);
880 mutex_unlock(&data
->lock
);
890 static int kmx61_read_event(struct iio_dev
*indio_dev
,
891 const struct iio_chan_spec
*chan
,
892 enum iio_event_type type
,
893 enum iio_event_direction dir
,
894 enum iio_event_info info
,
897 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
901 case IIO_EV_INFO_VALUE
:
902 *val
= data
->wake_thresh
;
904 case IIO_EV_INFO_PERIOD
:
905 *val
= data
->wake_duration
;
912 static int kmx61_write_event(struct iio_dev
*indio_dev
,
913 const struct iio_chan_spec
*chan
,
914 enum iio_event_type type
,
915 enum iio_event_direction dir
,
916 enum iio_event_info info
,
919 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
921 if (data
->ev_enable_state
)
925 case IIO_EV_INFO_VALUE
:
926 data
->wake_thresh
= val
;
928 case IIO_EV_INFO_PERIOD
:
929 data
->wake_duration
= val
;
936 static int kmx61_read_event_config(struct iio_dev
*indio_dev
,
937 const struct iio_chan_spec
*chan
,
938 enum iio_event_type type
,
939 enum iio_event_direction dir
)
941 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
943 return data
->ev_enable_state
;
946 static int kmx61_write_event_config(struct iio_dev
*indio_dev
,
947 const struct iio_chan_spec
*chan
,
948 enum iio_event_type type
,
949 enum iio_event_direction dir
,
952 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
955 if (state
&& data
->ev_enable_state
)
958 mutex_lock(&data
->lock
);
960 if (!state
&& data
->motion_trig_on
) {
961 data
->ev_enable_state
= false;
965 ret
= kmx61_set_power_state(data
, state
, KMX61_ACC
);
969 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
971 kmx61_set_power_state(data
, false, KMX61_ACC
);
975 data
->ev_enable_state
= state
;
978 mutex_unlock(&data
->lock
);
983 static int kmx61_acc_validate_trigger(struct iio_dev
*indio_dev
,
984 struct iio_trigger
*trig
)
986 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
988 if (data
->acc_dready_trig
!= trig
&& data
->motion_trig
!= trig
)
994 static int kmx61_mag_validate_trigger(struct iio_dev
*indio_dev
,
995 struct iio_trigger
*trig
)
997 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
999 if (data
->mag_dready_trig
!= trig
)
1005 static const struct iio_info kmx61_acc_info
= {
1006 .read_raw
= kmx61_read_raw
,
1007 .write_raw
= kmx61_write_raw
,
1008 .attrs
= &kmx61_acc_attribute_group
,
1009 .read_event_value
= kmx61_read_event
,
1010 .write_event_value
= kmx61_write_event
,
1011 .read_event_config
= kmx61_read_event_config
,
1012 .write_event_config
= kmx61_write_event_config
,
1013 .validate_trigger
= kmx61_acc_validate_trigger
,
1016 static const struct iio_info kmx61_mag_info
= {
1017 .read_raw
= kmx61_read_raw
,
1018 .write_raw
= kmx61_write_raw
,
1019 .attrs
= &kmx61_mag_attribute_group
,
1020 .validate_trigger
= kmx61_mag_validate_trigger
,
1024 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1030 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1031 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1033 mutex_lock(&data
->lock
);
1035 if (!state
&& data
->ev_enable_state
&& data
->motion_trig_on
) {
1036 data
->motion_trig_on
= false;
1040 if (data
->acc_dready_trig
== trig
|| data
->motion_trig
== trig
)
1045 ret
= kmx61_set_power_state(data
, state
, device
);
1049 if (data
->acc_dready_trig
== trig
|| data
->mag_dready_trig
== trig
)
1050 ret
= kmx61_setup_new_data_interrupt(data
, state
, device
);
1052 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
1054 kmx61_set_power_state(data
, false, device
);
1058 if (data
->acc_dready_trig
== trig
)
1059 data
->acc_dready_trig_on
= state
;
1060 else if (data
->mag_dready_trig
== trig
)
1061 data
->mag_dready_trig_on
= state
;
1063 data
->motion_trig_on
= state
;
1065 mutex_unlock(&data
->lock
);
1070 static int kmx61_trig_try_reenable(struct iio_trigger
*trig
)
1072 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1073 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1076 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1078 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1085 static const struct iio_trigger_ops kmx61_trigger_ops
= {
1086 .set_trigger_state
= kmx61_data_rdy_trigger_set_state
,
1087 .try_reenable
= kmx61_trig_try_reenable
,
1090 static irqreturn_t
kmx61_event_handler(int irq
, void *private)
1092 struct kmx61_data
*data
= private;
1093 struct iio_dev
*indio_dev
= data
->acc_indio_dev
;
1096 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS1
);
1098 dev_err(&data
->client
->dev
, "Error reading reg_ins1\n");
1102 if (ret
& KMX61_REG_INS1_BIT_WUFS
) {
1103 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS2
);
1105 dev_err(&data
->client
->dev
, "Error reading reg_ins2\n");
1109 if (ret
& KMX61_REG_INS2_BIT_XN
)
1110 iio_push_event(indio_dev
,
1111 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1115 IIO_EV_DIR_FALLING
),
1118 if (ret
& KMX61_REG_INS2_BIT_XP
)
1119 iio_push_event(indio_dev
,
1120 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1127 if (ret
& KMX61_REG_INS2_BIT_YN
)
1128 iio_push_event(indio_dev
,
1129 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1133 IIO_EV_DIR_FALLING
),
1136 if (ret
& KMX61_REG_INS2_BIT_YP
)
1137 iio_push_event(indio_dev
,
1138 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1145 if (ret
& KMX61_REG_INS2_BIT_ZN
)
1146 iio_push_event(indio_dev
,
1147 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1151 IIO_EV_DIR_FALLING
),
1154 if (ret
& KMX61_REG_INS2_BIT_ZP
)
1155 iio_push_event(indio_dev
,
1156 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1165 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
1167 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
1169 ret
|= KMX61_REG_CTRL1_BIT_RES
;
1170 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
1172 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
1174 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1176 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1181 static irqreturn_t
kmx61_data_rdy_trig_poll(int irq
, void *private)
1183 struct kmx61_data
*data
= private;
1185 if (data
->acc_dready_trig_on
)
1186 iio_trigger_poll(data
->acc_dready_trig
);
1187 if (data
->mag_dready_trig_on
)
1188 iio_trigger_poll(data
->mag_dready_trig
);
1190 if (data
->motion_trig_on
)
1191 iio_trigger_poll(data
->motion_trig
);
1193 if (data
->ev_enable_state
)
1194 return IRQ_WAKE_THREAD
;
1198 static irqreturn_t
kmx61_trigger_handler(int irq
, void *p
)
1200 struct iio_poll_func
*pf
= p
;
1201 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1202 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1203 int bit
, ret
, i
= 0;
1207 if (indio_dev
== data
->acc_indio_dev
)
1208 base
= KMX61_ACC_XOUT_L
;
1210 base
= KMX61_MAG_XOUT_L
;
1212 mutex_lock(&data
->lock
);
1213 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1214 indio_dev
->masklength
) {
1215 ret
= kmx61_read_measurement(data
, base
, bit
);
1217 mutex_unlock(&data
->lock
);
1222 mutex_unlock(&data
->lock
);
1224 iio_push_to_buffers(indio_dev
, buffer
);
1226 iio_trigger_notify_done(indio_dev
->trig
);
1231 static const char *kmx61_match_acpi_device(struct device
*dev
)
1233 const struct acpi_device_id
*id
;
1235 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1238 return dev_name(dev
);
1241 static struct iio_dev
*kmx61_indiodev_setup(struct kmx61_data
*data
,
1242 const struct iio_info
*info
,
1243 const struct iio_chan_spec
*chan
,
1247 struct iio_dev
*indio_dev
;
1249 indio_dev
= devm_iio_device_alloc(&data
->client
->dev
, sizeof(data
));
1251 return ERR_PTR(-ENOMEM
);
1253 kmx61_set_data(indio_dev
, data
);
1255 indio_dev
->dev
.parent
= &data
->client
->dev
;
1256 indio_dev
->channels
= chan
;
1257 indio_dev
->num_channels
= num_channels
;
1258 indio_dev
->name
= name
;
1259 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1260 indio_dev
->info
= info
;
1265 static struct iio_trigger
*kmx61_trigger_setup(struct kmx61_data
*data
,
1266 struct iio_dev
*indio_dev
,
1269 struct iio_trigger
*trig
;
1272 trig
= devm_iio_trigger_alloc(&data
->client
->dev
,
1278 return ERR_PTR(-ENOMEM
);
1280 trig
->dev
.parent
= &data
->client
->dev
;
1281 trig
->ops
= &kmx61_trigger_ops
;
1282 iio_trigger_set_drvdata(trig
, indio_dev
);
1284 ret
= iio_trigger_register(trig
);
1286 return ERR_PTR(ret
);
1291 static int kmx61_probe(struct i2c_client
*client
,
1292 const struct i2c_device_id
*id
)
1295 struct kmx61_data
*data
;
1296 const char *name
= NULL
;
1298 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1302 i2c_set_clientdata(client
, data
);
1303 data
->client
= client
;
1305 mutex_init(&data
->lock
);
1309 else if (ACPI_HANDLE(&client
->dev
))
1310 name
= kmx61_match_acpi_device(&client
->dev
);
1314 data
->acc_indio_dev
=
1315 kmx61_indiodev_setup(data
, &kmx61_acc_info
,
1317 ARRAY_SIZE(kmx61_acc_channels
),
1319 if (IS_ERR(data
->acc_indio_dev
))
1320 return PTR_ERR(data
->acc_indio_dev
);
1322 data
->mag_indio_dev
=
1323 kmx61_indiodev_setup(data
, &kmx61_mag_info
,
1325 ARRAY_SIZE(kmx61_mag_channels
),
1327 if (IS_ERR(data
->mag_indio_dev
))
1328 return PTR_ERR(data
->mag_indio_dev
);
1330 ret
= kmx61_chip_init(data
);
1334 if (client
->irq
> 0) {
1335 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1336 kmx61_data_rdy_trig_poll
,
1337 kmx61_event_handler
,
1338 IRQF_TRIGGER_RISING
,
1342 goto err_chip_uninit
;
1344 data
->acc_dready_trig
=
1345 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1347 if (IS_ERR(data
->acc_dready_trig
)) {
1348 ret
= PTR_ERR(data
->acc_dready_trig
);
1349 goto err_chip_uninit
;
1352 data
->mag_dready_trig
=
1353 kmx61_trigger_setup(data
, data
->mag_indio_dev
,
1355 if (IS_ERR(data
->mag_dready_trig
)) {
1356 ret
= PTR_ERR(data
->mag_dready_trig
);
1357 goto err_trigger_unregister_acc_dready
;
1361 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1363 if (IS_ERR(data
->motion_trig
)) {
1364 ret
= PTR_ERR(data
->motion_trig
);
1365 goto err_trigger_unregister_mag_dready
;
1368 ret
= iio_triggered_buffer_setup(data
->acc_indio_dev
,
1369 &iio_pollfunc_store_time
,
1370 kmx61_trigger_handler
,
1373 dev_err(&data
->client
->dev
,
1374 "Failed to setup acc triggered buffer\n");
1375 goto err_trigger_unregister_motion
;
1378 ret
= iio_triggered_buffer_setup(data
->mag_indio_dev
,
1379 &iio_pollfunc_store_time
,
1380 kmx61_trigger_handler
,
1383 dev_err(&data
->client
->dev
,
1384 "Failed to setup mag triggered buffer\n");
1385 goto err_buffer_cleanup_acc
;
1389 ret
= pm_runtime_set_active(&client
->dev
);
1391 goto err_buffer_cleanup_mag
;
1393 pm_runtime_enable(&client
->dev
);
1394 pm_runtime_set_autosuspend_delay(&client
->dev
, KMX61_SLEEP_DELAY_MS
);
1395 pm_runtime_use_autosuspend(&client
->dev
);
1397 ret
= iio_device_register(data
->acc_indio_dev
);
1399 dev_err(&client
->dev
, "Failed to register acc iio device\n");
1400 goto err_buffer_cleanup_mag
;
1403 ret
= iio_device_register(data
->mag_indio_dev
);
1405 dev_err(&client
->dev
, "Failed to register mag iio device\n");
1406 goto err_iio_unregister_acc
;
1411 err_iio_unregister_acc
:
1412 iio_device_unregister(data
->acc_indio_dev
);
1413 err_buffer_cleanup_mag
:
1414 if (client
->irq
> 0)
1415 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1416 err_buffer_cleanup_acc
:
1417 if (client
->irq
> 0)
1418 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1419 err_trigger_unregister_motion
:
1420 iio_trigger_unregister(data
->motion_trig
);
1421 err_trigger_unregister_mag_dready
:
1422 iio_trigger_unregister(data
->mag_dready_trig
);
1423 err_trigger_unregister_acc_dready
:
1424 iio_trigger_unregister(data
->acc_dready_trig
);
1426 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1430 static int kmx61_remove(struct i2c_client
*client
)
1432 struct kmx61_data
*data
= i2c_get_clientdata(client
);
1434 iio_device_unregister(data
->acc_indio_dev
);
1435 iio_device_unregister(data
->mag_indio_dev
);
1437 pm_runtime_disable(&client
->dev
);
1438 pm_runtime_set_suspended(&client
->dev
);
1439 pm_runtime_put_noidle(&client
->dev
);
1441 if (client
->irq
> 0) {
1442 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1443 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1444 iio_trigger_unregister(data
->acc_dready_trig
);
1445 iio_trigger_unregister(data
->mag_dready_trig
);
1446 iio_trigger_unregister(data
->motion_trig
);
1449 mutex_lock(&data
->lock
);
1450 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1451 mutex_unlock(&data
->lock
);
1456 #ifdef CONFIG_PM_SLEEP
1457 static int kmx61_suspend(struct device
*dev
)
1460 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1462 mutex_lock(&data
->lock
);
1463 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
1465 mutex_unlock(&data
->lock
);
1470 static int kmx61_resume(struct device
*dev
)
1473 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);
1485 static int kmx61_runtime_suspend(struct device
*dev
)
1487 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1490 mutex_lock(&data
->lock
);
1491 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1492 mutex_unlock(&data
->lock
);
1497 static int kmx61_runtime_resume(struct device
*dev
)
1499 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1503 stby
|= KMX61_ACC_STBY_BIT
;
1505 stby
|= KMX61_MAG_STBY_BIT
;
1507 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1511 static const struct dev_pm_ops kmx61_pm_ops
= {
1512 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend
, kmx61_resume
)
1513 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend
, kmx61_runtime_resume
, NULL
)
1516 static const struct acpi_device_id kmx61_acpi_match
[] = {
1521 MODULE_DEVICE_TABLE(acpi
, kmx61_acpi_match
);
1523 static const struct i2c_device_id kmx61_id
[] = {
1528 MODULE_DEVICE_TABLE(i2c
, kmx61_id
);
1530 static struct i2c_driver kmx61_driver
= {
1532 .name
= KMX61_DRV_NAME
,
1533 .acpi_match_table
= ACPI_PTR(kmx61_acpi_match
),
1534 .pm
= &kmx61_pm_ops
,
1536 .probe
= kmx61_probe
,
1537 .remove
= kmx61_remove
,
1538 .id_table
= kmx61_id
,
1541 module_i2c_driver(kmx61_driver
);
1543 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1544 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1545 MODULE_LICENSE("GPL v2");