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/gpio/consumer.h>
18 #include <linux/interrupt.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/events.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/trigger_consumer.h>
29 #define KMX61_DRV_NAME "kmx61"
30 #define KMX61_IRQ_NAME "kmx61_event"
32 #define KMX61_REG_WHO_AM_I 0x00
33 #define KMX61_REG_INS1 0x01
34 #define KMX61_REG_INS2 0x02
37 * three 16-bit accelerometer output registers for X/Y/Z axis
38 * we use only XOUT_L as a base register, all other addresses
39 * can be obtained by applying an offset and are provided here
42 #define KMX61_ACC_XOUT_L 0x0A
43 #define KMX61_ACC_XOUT_H 0x0B
44 #define KMX61_ACC_YOUT_L 0x0C
45 #define KMX61_ACC_YOUT_H 0x0D
46 #define KMX61_ACC_ZOUT_L 0x0E
47 #define KMX61_ACC_ZOUT_H 0x0F
50 * one 16-bit temperature output register
52 #define KMX61_TEMP_L 0x10
53 #define KMX61_TEMP_H 0x11
56 * three 16-bit magnetometer output registers for X/Y/Z axis
58 #define KMX61_MAG_XOUT_L 0x12
59 #define KMX61_MAG_XOUT_H 0x13
60 #define KMX61_MAG_YOUT_L 0x14
61 #define KMX61_MAG_YOUT_H 0x15
62 #define KMX61_MAG_ZOUT_L 0x16
63 #define KMX61_MAG_ZOUT_H 0x17
65 #define KMX61_REG_INL 0x28
66 #define KMX61_REG_STBY 0x29
67 #define KMX61_REG_CTRL1 0x2A
68 #define KMX61_REG_CTRL2 0x2B
69 #define KMX61_REG_ODCNTL 0x2C
70 #define KMX61_REG_INC1 0x2D
72 #define KMX61_REG_WUF_THRESH 0x3D
73 #define KMX61_REG_WUF_TIMER 0x3E
75 #define KMX61_ACC_STBY_BIT BIT(0)
76 #define KMX61_MAG_STBY_BIT BIT(1)
77 #define KMX61_ACT_STBY_BIT BIT(7)
79 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
81 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
83 #define KMX61_REG_INS2_BIT_ZP BIT(0)
84 #define KMX61_REG_INS2_BIT_ZN BIT(1)
85 #define KMX61_REG_INS2_BIT_YP BIT(2)
86 #define KMX61_REG_INS2_BIT_YN BIT(3)
87 #define KMX61_REG_INS2_BIT_XP BIT(4)
88 #define KMX61_REG_INS2_BIT_XN BIT(5)
90 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
92 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
93 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
94 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
95 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
97 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
98 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
99 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
100 #define KMX61_REG_INC1_BIT_IEN BIT(5)
102 #define KMX61_ACC_ODR_SHIFT 0
103 #define KMX61_MAG_ODR_SHIFT 4
104 #define KMX61_ACC_ODR_MASK 0x0F
105 #define KMX61_MAG_ODR_MASK 0xF0
107 #define KMX61_OWUF_MASK 0x7
109 #define KMX61_DEFAULT_WAKE_THRESH 1
110 #define KMX61_DEFAULT_WAKE_DURATION 1
112 #define KMX61_SLEEP_DELAY_MS 2000
114 #define KMX61_CHIP_ID 0x12
117 #define KMX61_ACC 0x01
118 #define KMX61_MAG 0x02
121 struct i2c_client
*client
;
123 /* serialize access to non-atomic ops, e.g set_mode */
140 /* accelerometer specific data */
141 struct iio_dev
*acc_indio_dev
;
142 struct iio_trigger
*acc_dready_trig
;
143 struct iio_trigger
*motion_trig
;
144 bool acc_dready_trig_on
;
146 bool ev_enable_state
;
148 /* magnetometer specific data */
149 struct iio_dev
*mag_indio_dev
;
150 struct iio_trigger
*mag_dready_trig
;
151 bool mag_dready_trig_on
;
166 static const u16 kmx61_uscale_table
[] = {9582, 19163, 38326};
168 static const struct {
171 } kmx61_samp_freq_table
[] = { {12, 500000},
184 static const struct {
188 } kmx61_wake_up_odr_table
[] = { {0, 781000, 0x00},
201 static IIO_CONST_ATTR(accel_scale_available
, "0.009582 0.019163 0.038326");
202 static IIO_CONST_ATTR(magn_scale_available
, "0.001465");
203 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
204 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
206 static struct attribute
*kmx61_acc_attributes
[] = {
207 &iio_const_attr_accel_scale_available
.dev_attr
.attr
,
208 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
212 static struct attribute
*kmx61_mag_attributes
[] = {
213 &iio_const_attr_magn_scale_available
.dev_attr
.attr
,
214 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
218 static const struct attribute_group kmx61_acc_attribute_group
= {
219 .attrs
= kmx61_acc_attributes
,
222 static const struct attribute_group kmx61_mag_attribute_group
= {
223 .attrs
= kmx61_mag_attributes
,
226 static const struct iio_event_spec kmx61_event
= {
227 .type
= IIO_EV_TYPE_THRESH
,
228 .dir
= IIO_EV_DIR_EITHER
,
229 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
230 BIT(IIO_EV_INFO_ENABLE
) |
231 BIT(IIO_EV_INFO_PERIOD
),
234 #define KMX61_ACC_CHAN(_axis) { \
237 .channel2 = IIO_MOD_ ## _axis, \
238 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
239 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
240 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
241 .address = KMX61_ACC, \
242 .scan_index = KMX61_AXIS_ ## _axis, \
248 .endianness = IIO_LE, \
250 .event_spec = &kmx61_event, \
251 .num_event_specs = 1 \
254 #define KMX61_MAG_CHAN(_axis) { \
257 .channel2 = IIO_MOD_ ## _axis, \
258 .address = KMX61_MAG, \
259 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
260 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
261 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
262 .scan_index = KMX61_AXIS_ ## _axis, \
268 .endianness = IIO_LE, \
272 static const struct iio_chan_spec kmx61_acc_channels
[] = {
278 static const struct iio_chan_spec kmx61_mag_channels
[] = {
284 static void kmx61_set_data(struct iio_dev
*indio_dev
, struct kmx61_data
*data
)
286 struct kmx61_data
**priv
= iio_priv(indio_dev
);
291 static struct kmx61_data
*kmx61_get_data(struct iio_dev
*indio_dev
)
293 return *(struct kmx61_data
**)iio_priv(indio_dev
);
296 static int kmx61_convert_freq_to_bit(int val
, int val2
)
300 for (i
= 0; i
< ARRAY_SIZE(kmx61_samp_freq_table
); i
++)
301 if (val
== kmx61_samp_freq_table
[i
].val
&&
302 val2
== kmx61_samp_freq_table
[i
].val2
)
307 static int kmx61_convert_wake_up_odr_to_bit(int val
, int val2
)
311 for (i
= 0; i
< ARRAY_SIZE(kmx61_wake_up_odr_table
); ++i
)
312 if (kmx61_wake_up_odr_table
[i
].val
== val
&&
313 kmx61_wake_up_odr_table
[i
].val2
== val2
)
314 return kmx61_wake_up_odr_table
[i
].odr_bits
;
319 * kmx61_set_mode() - set KMX61 device operating mode
320 * @data - kmx61 device private data pointer
321 * @mode - bitmask, indicating operating mode for @device
322 * @device - bitmask, indicating device for which @mode needs to be set
323 * @update - update stby bits stored in device's private @data
325 * For each sensor (accelerometer/magnetometer) there are two operating modes
326 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
327 * if they are both enabled. Internal sensors state is saved in acc_stby and
328 * mag_stby members of driver's private @data.
330 static int kmx61_set_mode(struct kmx61_data
*data
, u8 mode
, u8 device
,
334 int acc_stby
= -1, mag_stby
= -1;
336 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_STBY
);
338 dev_err(&data
->client
->dev
, "Error reading reg_stby\n");
341 if (device
& KMX61_ACC
) {
342 if (mode
& KMX61_ACC_STBY_BIT
) {
343 ret
|= KMX61_ACC_STBY_BIT
;
346 ret
&= ~KMX61_ACC_STBY_BIT
;
351 if (device
& KMX61_MAG
) {
352 if (mode
& KMX61_MAG_STBY_BIT
) {
353 ret
|= KMX61_MAG_STBY_BIT
;
356 ret
&= ~KMX61_MAG_STBY_BIT
;
361 if (mode
& KMX61_ACT_STBY_BIT
)
362 ret
|= KMX61_ACT_STBY_BIT
;
364 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_STBY
, ret
);
366 dev_err(&data
->client
->dev
, "Error writing reg_stby\n");
370 if (acc_stby
!= -1 && update
)
371 data
->acc_stby
= acc_stby
;
372 if (mag_stby
!= -1 && update
)
373 data
->mag_stby
= mag_stby
;
378 static int kmx61_get_mode(struct kmx61_data
*data
, u8
*mode
, u8 device
)
382 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_STBY
);
384 dev_err(&data
->client
->dev
, "Error reading reg_stby\n");
389 if (device
& KMX61_ACC
) {
390 if (ret
& KMX61_ACC_STBY_BIT
)
391 *mode
|= KMX61_ACC_STBY_BIT
;
393 *mode
&= ~KMX61_ACC_STBY_BIT
;
396 if (device
& KMX61_MAG
) {
397 if (ret
& KMX61_MAG_STBY_BIT
)
398 *mode
|= KMX61_MAG_STBY_BIT
;
400 *mode
&= ~KMX61_MAG_STBY_BIT
;
406 static int kmx61_set_wake_up_odr(struct kmx61_data
*data
, int val
, int val2
)
410 odr_bits
= kmx61_convert_wake_up_odr_to_bit(val
, val2
);
414 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL2
,
417 dev_err(&data
->client
->dev
, "Error writing reg_ctrl2\n");
421 static int kmx61_set_odr(struct kmx61_data
*data
, int val
, int val2
, u8 device
)
425 int lodr_bits
, odr_bits
;
427 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
431 lodr_bits
= kmx61_convert_freq_to_bit(val
, val2
);
435 /* To change ODR, accel and magn must be in STDBY */
436 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
442 if (device
& KMX61_ACC
)
443 odr_bits
|= lodr_bits
<< KMX61_ACC_ODR_SHIFT
;
444 if (device
& KMX61_MAG
)
445 odr_bits
|= lodr_bits
<< KMX61_MAG_ODR_SHIFT
;
447 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_ODCNTL
,
452 data
->odr_bits
= odr_bits
;
454 if (device
& KMX61_ACC
) {
455 ret
= kmx61_set_wake_up_odr(data
, val
, val2
);
460 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
463 static int kmx61_get_odr(struct kmx61_data
*data
, int *val
, int *val2
,
468 if (device
& KMX61_ACC
)
469 lodr_bits
= (data
->odr_bits
>> KMX61_ACC_ODR_SHIFT
) &
471 else if (device
& KMX61_MAG
)
472 lodr_bits
= (data
->odr_bits
>> KMX61_MAG_ODR_SHIFT
) &
477 if (lodr_bits
>= ARRAY_SIZE(kmx61_samp_freq_table
))
480 *val
= kmx61_samp_freq_table
[lodr_bits
].val
;
481 *val2
= kmx61_samp_freq_table
[lodr_bits
].val2
;
486 static int kmx61_set_range(struct kmx61_data
*data
, u8 range
)
490 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
492 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
496 ret
&= ~KMX61_REG_CTRL1_GSEL_MASK
;
497 ret
|= range
& KMX61_REG_CTRL1_GSEL_MASK
;
499 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
501 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
510 static int kmx61_set_scale(struct kmx61_data
*data
, u16 uscale
)
515 for (i
= 0; i
< ARRAY_SIZE(kmx61_uscale_table
); i
++) {
516 if (kmx61_uscale_table
[i
] == uscale
) {
517 ret
= kmx61_get_mode(data
, &mode
,
518 KMX61_ACC
| KMX61_MAG
);
522 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
,
523 KMX61_ACC
| KMX61_MAG
, true);
527 ret
= kmx61_set_range(data
, i
);
531 return kmx61_set_mode(data
, mode
,
532 KMX61_ACC
| KMX61_MAG
, true);
538 static int kmx61_chip_init(struct kmx61_data
*data
)
542 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_WHO_AM_I
);
544 dev_err(&data
->client
->dev
, "Error reading who_am_i\n");
548 if (ret
!= KMX61_CHIP_ID
) {
549 dev_err(&data
->client
->dev
,
550 "Wrong chip id, got %x expected %x\n",
555 /* set accel 12bit, 4g range */
556 ret
= kmx61_set_range(data
, KMX61_RANGE_4G
);
560 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_ODCNTL
);
562 dev_err(&data
->client
->dev
, "Error reading reg_odcntl\n");
565 data
->odr_bits
= ret
;
568 * set output data rate for wake up (motion detection) function
569 * to match data rate for accelerometer sampling
571 ret
= kmx61_get_odr(data
, &val
, &val2
, KMX61_ACC
);
575 ret
= kmx61_set_wake_up_odr(data
, val
, val2
);
579 /* set acc/magn to OPERATION mode */
580 ret
= kmx61_set_mode(data
, 0, KMX61_ACC
| KMX61_MAG
, true);
584 data
->wake_thresh
= KMX61_DEFAULT_WAKE_THRESH
;
585 data
->wake_duration
= KMX61_DEFAULT_WAKE_DURATION
;
590 static int kmx61_setup_new_data_interrupt(struct kmx61_data
*data
,
591 bool status
, u8 device
)
596 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
600 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
604 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INC1
);
606 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
611 ret
|= KMX61_REG_INC1_BIT_IEN
;
612 if (device
& KMX61_ACC
)
613 ret
|= KMX61_REG_INC1_BIT_DRDYA
;
614 if (device
& KMX61_MAG
)
615 ret
|= KMX61_REG_INC1_BIT_DRDYM
;
617 ret
&= ~KMX61_REG_INC1_BIT_IEN
;
618 if (device
& KMX61_ACC
)
619 ret
&= ~KMX61_REG_INC1_BIT_DRDYA
;
620 if (device
& KMX61_MAG
)
621 ret
&= ~KMX61_REG_INC1_BIT_DRDYM
;
623 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_INC1
, ret
);
625 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
629 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
631 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
636 ret
|= KMX61_REG_CTRL1_BIT_DRDYE
;
638 ret
&= ~KMX61_REG_CTRL1_BIT_DRDYE
;
640 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
642 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
646 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
649 static int kmx61_chip_update_thresholds(struct kmx61_data
*data
)
653 ret
= i2c_smbus_write_byte_data(data
->client
,
655 data
->wake_duration
);
657 dev_err(&data
->client
->dev
, "Errow writing reg_wuf_timer\n");
661 ret
= i2c_smbus_write_byte_data(data
->client
,
662 KMX61_REG_WUF_THRESH
,
665 dev_err(&data
->client
->dev
, "Error writing reg_wuf_thresh\n");
670 static int kmx61_setup_any_motion_interrupt(struct kmx61_data
*data
,
676 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
680 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
684 ret
= kmx61_chip_update_thresholds(data
);
688 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INC1
);
690 dev_err(&data
->client
->dev
, "Error reading reg_inc1\n");
694 ret
|= (KMX61_REG_INC1_BIT_IEN
| KMX61_REG_INC1_BIT_WUFS
);
696 ret
&= ~(KMX61_REG_INC1_BIT_IEN
| KMX61_REG_INC1_BIT_WUFS
);
698 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_INC1
, ret
);
700 dev_err(&data
->client
->dev
, "Error writing reg_inc1\n");
704 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
706 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
711 ret
|= KMX61_REG_CTRL1_BIT_WUFE
| KMX61_REG_CTRL1_BIT_BTSE
;
713 ret
&= ~(KMX61_REG_CTRL1_BIT_WUFE
| KMX61_REG_CTRL1_BIT_BTSE
);
715 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
717 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
720 mode
|= KMX61_ACT_STBY_BIT
;
721 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
725 * kmx61_set_power_state() - set power state for kmx61 @device
726 * @data - kmx61 device private pointer
727 * @on - power state to be set for @device
728 * @device - bitmask indicating device for which @on state needs to be set
730 * Notice that when ACC power state needs to be set to ON and MAG is in
731 * OPERATION then we know that kmx61_runtime_resume was already called
732 * so we must set ACC OPERATION mode here. The same happens when MAG power
733 * state needs to be set to ON and ACC is in OPERATION.
735 static int kmx61_set_power_state(struct kmx61_data
*data
, bool on
, u8 device
)
740 if (device
& KMX61_ACC
) {
741 if (on
&& !data
->acc_ps
&& !data
->mag_stby
) {
742 ret
= kmx61_set_mode(data
, 0, KMX61_ACC
, true);
748 if (device
& KMX61_MAG
) {
749 if (on
&& !data
->mag_ps
&& !data
->acc_stby
) {
750 ret
= kmx61_set_mode(data
, 0, KMX61_MAG
, true);
758 ret
= pm_runtime_get_sync(&data
->client
->dev
);
760 pm_runtime_mark_last_busy(&data
->client
->dev
);
761 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
764 dev_err(&data
->client
->dev
,
765 "Failed: kmx61_set_power_state for %d, ret %d\n",
768 pm_runtime_put_noidle(&data
->client
->dev
);
776 static int kmx61_read_measurement(struct kmx61_data
*data
, u8 base
, u8 offset
)
779 u8 reg
= base
+ offset
* 2;
781 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
783 dev_err(&data
->client
->dev
, "failed to read reg at %x\n", reg
);
788 static int kmx61_read_raw(struct iio_dev
*indio_dev
,
789 struct iio_chan_spec
const *chan
, int *val
,
790 int *val2
, long mask
)
794 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
797 case IIO_CHAN_INFO_RAW
:
798 switch (chan
->type
) {
800 base_reg
= KMX61_ACC_XOUT_L
;
803 base_reg
= KMX61_MAG_XOUT_L
;
808 mutex_lock(&data
->lock
);
810 ret
= kmx61_set_power_state(data
, true, chan
->address
);
812 mutex_unlock(&data
->lock
);
816 ret
= kmx61_read_measurement(data
, base_reg
, chan
->scan_index
);
818 kmx61_set_power_state(data
, false, chan
->address
);
819 mutex_unlock(&data
->lock
);
822 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
823 chan
->scan_type
.realbits
- 1);
824 ret
= kmx61_set_power_state(data
, false, chan
->address
);
826 mutex_unlock(&data
->lock
);
830 case IIO_CHAN_INFO_SCALE
:
831 switch (chan
->type
) {
834 *val2
= kmx61_uscale_table
[data
->range
];
835 return IIO_VAL_INT_PLUS_MICRO
;
837 /* 14 bits res, 1465 microGauss per magn count */
840 return IIO_VAL_INT_PLUS_MICRO
;
844 case IIO_CHAN_INFO_SAMP_FREQ
:
845 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
848 mutex_lock(&data
->lock
);
849 ret
= kmx61_get_odr(data
, val
, val2
, chan
->address
);
850 mutex_unlock(&data
->lock
);
853 return IIO_VAL_INT_PLUS_MICRO
;
858 static int kmx61_write_raw(struct iio_dev
*indio_dev
,
859 struct iio_chan_spec
const *chan
, int val
,
863 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
866 case IIO_CHAN_INFO_SAMP_FREQ
:
867 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
870 mutex_lock(&data
->lock
);
871 ret
= kmx61_set_odr(data
, val
, val2
, chan
->address
);
872 mutex_unlock(&data
->lock
);
874 case IIO_CHAN_INFO_SCALE
:
875 switch (chan
->type
) {
879 mutex_lock(&data
->lock
);
880 ret
= kmx61_set_scale(data
, val2
);
881 mutex_unlock(&data
->lock
);
891 static int kmx61_read_event(struct iio_dev
*indio_dev
,
892 const struct iio_chan_spec
*chan
,
893 enum iio_event_type type
,
894 enum iio_event_direction dir
,
895 enum iio_event_info info
,
898 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
902 case IIO_EV_INFO_VALUE
:
903 *val
= data
->wake_thresh
;
905 case IIO_EV_INFO_PERIOD
:
906 *val
= data
->wake_duration
;
913 static int kmx61_write_event(struct iio_dev
*indio_dev
,
914 const struct iio_chan_spec
*chan
,
915 enum iio_event_type type
,
916 enum iio_event_direction dir
,
917 enum iio_event_info info
,
920 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
922 if (data
->ev_enable_state
)
926 case IIO_EV_INFO_VALUE
:
927 data
->wake_thresh
= val
;
929 case IIO_EV_INFO_PERIOD
:
930 data
->wake_duration
= val
;
937 static int kmx61_read_event_config(struct iio_dev
*indio_dev
,
938 const struct iio_chan_spec
*chan
,
939 enum iio_event_type type
,
940 enum iio_event_direction dir
)
942 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
944 return data
->ev_enable_state
;
947 static int kmx61_write_event_config(struct iio_dev
*indio_dev
,
948 const struct iio_chan_spec
*chan
,
949 enum iio_event_type type
,
950 enum iio_event_direction dir
,
953 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
956 if (state
&& data
->ev_enable_state
)
959 mutex_lock(&data
->lock
);
961 if (!state
&& data
->motion_trig_on
) {
962 data
->ev_enable_state
= false;
966 ret
= kmx61_set_power_state(data
, state
, KMX61_ACC
);
970 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
972 kmx61_set_power_state(data
, false, KMX61_ACC
);
976 data
->ev_enable_state
= state
;
979 mutex_unlock(&data
->lock
);
984 static int kmx61_acc_validate_trigger(struct iio_dev
*indio_dev
,
985 struct iio_trigger
*trig
)
987 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
989 if (data
->acc_dready_trig
!= trig
&& data
->motion_trig
!= trig
)
995 static int kmx61_mag_validate_trigger(struct iio_dev
*indio_dev
,
996 struct iio_trigger
*trig
)
998 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1000 if (data
->mag_dready_trig
!= trig
)
1006 static const struct iio_info kmx61_acc_info
= {
1007 .driver_module
= THIS_MODULE
,
1008 .read_raw
= kmx61_read_raw
,
1009 .write_raw
= kmx61_write_raw
,
1010 .attrs
= &kmx61_acc_attribute_group
,
1011 .read_event_value
= kmx61_read_event
,
1012 .write_event_value
= kmx61_write_event
,
1013 .read_event_config
= kmx61_read_event_config
,
1014 .write_event_config
= kmx61_write_event_config
,
1015 .validate_trigger
= kmx61_acc_validate_trigger
,
1018 static const struct iio_info kmx61_mag_info
= {
1019 .driver_module
= THIS_MODULE
,
1020 .read_raw
= kmx61_read_raw
,
1021 .write_raw
= kmx61_write_raw
,
1022 .attrs
= &kmx61_mag_attribute_group
,
1023 .validate_trigger
= kmx61_mag_validate_trigger
,
1027 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1033 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1034 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1036 mutex_lock(&data
->lock
);
1038 if (!state
&& data
->ev_enable_state
&& data
->motion_trig_on
) {
1039 data
->motion_trig_on
= false;
1043 if (data
->acc_dready_trig
== trig
|| data
->motion_trig
== trig
)
1048 ret
= kmx61_set_power_state(data
, state
, device
);
1052 if (data
->acc_dready_trig
== trig
|| data
->mag_dready_trig
== trig
)
1053 ret
= kmx61_setup_new_data_interrupt(data
, state
, device
);
1055 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
1057 kmx61_set_power_state(data
, false, device
);
1061 if (data
->acc_dready_trig
== trig
)
1062 data
->acc_dready_trig_on
= state
;
1063 else if (data
->mag_dready_trig
== trig
)
1064 data
->mag_dready_trig_on
= state
;
1066 data
->motion_trig_on
= state
;
1068 mutex_unlock(&data
->lock
);
1073 static int kmx61_trig_try_reenable(struct iio_trigger
*trig
)
1075 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1076 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1079 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1081 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1088 static const struct iio_trigger_ops kmx61_trigger_ops
= {
1089 .set_trigger_state
= kmx61_data_rdy_trigger_set_state
,
1090 .try_reenable
= kmx61_trig_try_reenable
,
1091 .owner
= THIS_MODULE
,
1094 static irqreturn_t
kmx61_event_handler(int irq
, void *private)
1096 struct kmx61_data
*data
= private;
1097 struct iio_dev
*indio_dev
= data
->acc_indio_dev
;
1100 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS1
);
1102 dev_err(&data
->client
->dev
, "Error reading reg_ins1\n");
1106 if (ret
& KMX61_REG_INS1_BIT_WUFS
) {
1107 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS2
);
1109 dev_err(&data
->client
->dev
, "Error reading reg_ins2\n");
1113 if (ret
& KMX61_REG_INS2_BIT_XN
)
1114 iio_push_event(indio_dev
,
1115 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1119 IIO_EV_DIR_FALLING
),
1122 if (ret
& KMX61_REG_INS2_BIT_XP
)
1123 iio_push_event(indio_dev
,
1124 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1131 if (ret
& KMX61_REG_INS2_BIT_YN
)
1132 iio_push_event(indio_dev
,
1133 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1137 IIO_EV_DIR_FALLING
),
1140 if (ret
& KMX61_REG_INS2_BIT_YP
)
1141 iio_push_event(indio_dev
,
1142 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1149 if (ret
& KMX61_REG_INS2_BIT_ZN
)
1150 iio_push_event(indio_dev
,
1151 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1155 IIO_EV_DIR_FALLING
),
1158 if (ret
& KMX61_REG_INS2_BIT_ZP
)
1159 iio_push_event(indio_dev
,
1160 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1169 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
1171 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
1173 ret
|= KMX61_REG_CTRL1_BIT_RES
;
1174 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
1176 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
1178 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1180 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1185 static irqreturn_t
kmx61_data_rdy_trig_poll(int irq
, void *private)
1187 struct kmx61_data
*data
= private;
1189 if (data
->acc_dready_trig_on
)
1190 iio_trigger_poll(data
->acc_dready_trig
);
1191 if (data
->mag_dready_trig_on
)
1192 iio_trigger_poll(data
->mag_dready_trig
);
1194 if (data
->motion_trig_on
)
1195 iio_trigger_poll(data
->motion_trig
);
1197 if (data
->ev_enable_state
)
1198 return IRQ_WAKE_THREAD
;
1202 static irqreturn_t
kmx61_trigger_handler(int irq
, void *p
)
1204 struct iio_poll_func
*pf
= p
;
1205 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1206 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1207 int bit
, ret
, i
= 0;
1211 if (indio_dev
== data
->acc_indio_dev
)
1212 base
= KMX61_ACC_XOUT_L
;
1214 base
= KMX61_MAG_XOUT_L
;
1216 mutex_lock(&data
->lock
);
1217 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1218 indio_dev
->masklength
) {
1219 ret
= kmx61_read_measurement(data
, base
, bit
);
1221 mutex_unlock(&data
->lock
);
1226 mutex_unlock(&data
->lock
);
1228 iio_push_to_buffers(indio_dev
, buffer
);
1230 iio_trigger_notify_done(indio_dev
->trig
);
1235 static const char *kmx61_match_acpi_device(struct device
*dev
)
1237 const struct acpi_device_id
*id
;
1239 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1242 return dev_name(dev
);
1245 static struct iio_dev
*kmx61_indiodev_setup(struct kmx61_data
*data
,
1246 const struct iio_info
*info
,
1247 const struct iio_chan_spec
*chan
,
1251 struct iio_dev
*indio_dev
;
1253 indio_dev
= devm_iio_device_alloc(&data
->client
->dev
, sizeof(data
));
1255 return ERR_PTR(-ENOMEM
);
1257 kmx61_set_data(indio_dev
, data
);
1259 indio_dev
->dev
.parent
= &data
->client
->dev
;
1260 indio_dev
->channels
= chan
;
1261 indio_dev
->num_channels
= num_channels
;
1262 indio_dev
->name
= name
;
1263 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1264 indio_dev
->info
= info
;
1269 static struct iio_trigger
*kmx61_trigger_setup(struct kmx61_data
*data
,
1270 struct iio_dev
*indio_dev
,
1273 struct iio_trigger
*trig
;
1276 trig
= devm_iio_trigger_alloc(&data
->client
->dev
,
1282 return ERR_PTR(-ENOMEM
);
1284 trig
->dev
.parent
= &data
->client
->dev
;
1285 trig
->ops
= &kmx61_trigger_ops
;
1286 iio_trigger_set_drvdata(trig
, indio_dev
);
1288 ret
= iio_trigger_register(trig
);
1290 return ERR_PTR(ret
);
1295 static int kmx61_probe(struct i2c_client
*client
,
1296 const struct i2c_device_id
*id
)
1299 struct kmx61_data
*data
;
1300 const char *name
= NULL
;
1302 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1306 i2c_set_clientdata(client
, data
);
1307 data
->client
= client
;
1309 mutex_init(&data
->lock
);
1313 else if (ACPI_HANDLE(&client
->dev
))
1314 name
= kmx61_match_acpi_device(&client
->dev
);
1318 data
->acc_indio_dev
=
1319 kmx61_indiodev_setup(data
, &kmx61_acc_info
,
1321 ARRAY_SIZE(kmx61_acc_channels
),
1323 if (IS_ERR(data
->acc_indio_dev
))
1324 return PTR_ERR(data
->acc_indio_dev
);
1326 data
->mag_indio_dev
=
1327 kmx61_indiodev_setup(data
, &kmx61_mag_info
,
1329 ARRAY_SIZE(kmx61_mag_channels
),
1331 if (IS_ERR(data
->mag_indio_dev
))
1332 return PTR_ERR(data
->mag_indio_dev
);
1334 ret
= kmx61_chip_init(data
);
1338 if (client
->irq
> 0) {
1339 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1340 kmx61_data_rdy_trig_poll
,
1341 kmx61_event_handler
,
1342 IRQF_TRIGGER_RISING
,
1346 goto err_chip_uninit
;
1348 data
->acc_dready_trig
=
1349 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1351 if (IS_ERR(data
->acc_dready_trig
)) {
1352 ret
= PTR_ERR(data
->acc_dready_trig
);
1353 goto err_chip_uninit
;
1356 data
->mag_dready_trig
=
1357 kmx61_trigger_setup(data
, data
->mag_indio_dev
,
1359 if (IS_ERR(data
->mag_dready_trig
)) {
1360 ret
= PTR_ERR(data
->mag_dready_trig
);
1361 goto err_trigger_unregister_acc_dready
;
1365 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1367 if (IS_ERR(data
->motion_trig
)) {
1368 ret
= PTR_ERR(data
->motion_trig
);
1369 goto err_trigger_unregister_mag_dready
;
1372 ret
= iio_triggered_buffer_setup(data
->acc_indio_dev
,
1373 &iio_pollfunc_store_time
,
1374 kmx61_trigger_handler
,
1377 dev_err(&data
->client
->dev
,
1378 "Failed to setup acc triggered buffer\n");
1379 goto err_trigger_unregister_motion
;
1382 ret
= iio_triggered_buffer_setup(data
->mag_indio_dev
,
1383 &iio_pollfunc_store_time
,
1384 kmx61_trigger_handler
,
1387 dev_err(&data
->client
->dev
,
1388 "Failed to setup mag triggered buffer\n");
1389 goto err_buffer_cleanup_acc
;
1393 ret
= iio_device_register(data
->acc_indio_dev
);
1395 dev_err(&client
->dev
, "Failed to register acc iio device\n");
1396 goto err_buffer_cleanup_mag
;
1399 ret
= iio_device_register(data
->mag_indio_dev
);
1401 dev_err(&client
->dev
, "Failed to register mag iio device\n");
1402 goto err_iio_unregister_acc
;
1405 ret
= pm_runtime_set_active(&client
->dev
);
1407 goto err_iio_unregister_mag
;
1409 pm_runtime_enable(&client
->dev
);
1410 pm_runtime_set_autosuspend_delay(&client
->dev
, KMX61_SLEEP_DELAY_MS
);
1411 pm_runtime_use_autosuspend(&client
->dev
);
1415 err_iio_unregister_mag
:
1416 iio_device_unregister(data
->mag_indio_dev
);
1417 err_iio_unregister_acc
:
1418 iio_device_unregister(data
->acc_indio_dev
);
1419 err_buffer_cleanup_mag
:
1420 if (client
->irq
> 0)
1421 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1422 err_buffer_cleanup_acc
:
1423 if (client
->irq
> 0)
1424 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1425 err_trigger_unregister_motion
:
1426 iio_trigger_unregister(data
->motion_trig
);
1427 err_trigger_unregister_mag_dready
:
1428 iio_trigger_unregister(data
->mag_dready_trig
);
1429 err_trigger_unregister_acc_dready
:
1430 iio_trigger_unregister(data
->acc_dready_trig
);
1432 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1436 static int kmx61_remove(struct i2c_client
*client
)
1438 struct kmx61_data
*data
= i2c_get_clientdata(client
);
1440 pm_runtime_disable(&client
->dev
);
1441 pm_runtime_set_suspended(&client
->dev
);
1442 pm_runtime_put_noidle(&client
->dev
);
1444 iio_device_unregister(data
->acc_indio_dev
);
1445 iio_device_unregister(data
->mag_indio_dev
);
1447 if (client
->irq
> 0) {
1448 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1449 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1450 iio_trigger_unregister(data
->acc_dready_trig
);
1451 iio_trigger_unregister(data
->mag_dready_trig
);
1452 iio_trigger_unregister(data
->motion_trig
);
1455 mutex_lock(&data
->lock
);
1456 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1457 mutex_unlock(&data
->lock
);
1462 #ifdef CONFIG_PM_SLEEP
1463 static int kmx61_suspend(struct device
*dev
)
1466 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1468 mutex_lock(&data
->lock
);
1469 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
1471 mutex_unlock(&data
->lock
);
1476 static int kmx61_resume(struct device
*dev
)
1479 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1482 stby
|= KMX61_ACC_STBY_BIT
;
1484 stby
|= KMX61_MAG_STBY_BIT
;
1486 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1491 static int kmx61_runtime_suspend(struct device
*dev
)
1493 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1496 mutex_lock(&data
->lock
);
1497 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1498 mutex_unlock(&data
->lock
);
1503 static int kmx61_runtime_resume(struct device
*dev
)
1505 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1509 stby
|= KMX61_ACC_STBY_BIT
;
1511 stby
|= KMX61_MAG_STBY_BIT
;
1513 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1517 static const struct dev_pm_ops kmx61_pm_ops
= {
1518 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend
, kmx61_resume
)
1519 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend
, kmx61_runtime_resume
, NULL
)
1522 static const struct acpi_device_id kmx61_acpi_match
[] = {
1527 MODULE_DEVICE_TABLE(acpi
, kmx61_acpi_match
);
1529 static const struct i2c_device_id kmx61_id
[] = {
1534 MODULE_DEVICE_TABLE(i2c
, kmx61_id
);
1536 static struct i2c_driver kmx61_driver
= {
1538 .name
= KMX61_DRV_NAME
,
1539 .acpi_match_table
= ACPI_PTR(kmx61_acpi_match
),
1540 .pm
= &kmx61_pm_ops
,
1542 .probe
= kmx61_probe
,
1543 .remove
= kmx61_remove
,
1544 .id_table
= kmx61_id
,
1547 module_i2c_driver(kmx61_driver
);
1549 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1550 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1551 MODULE_LICENSE("GPL v2");