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_GPIO_NAME "kmx61_int"
31 #define KMX61_IRQ_NAME "kmx61_event"
33 #define KMX61_REG_WHO_AM_I 0x00
34 #define KMX61_REG_INS1 0x01
35 #define KMX61_REG_INS2 0x02
38 * three 16-bit accelerometer output registers for X/Y/Z axis
39 * we use only XOUT_L as a base register, all other addresses
40 * can be obtained by applying an offset and are provided here
43 #define KMX61_ACC_XOUT_L 0x0A
44 #define KMX61_ACC_XOUT_H 0x0B
45 #define KMX61_ACC_YOUT_L 0x0C
46 #define KMX61_ACC_YOUT_H 0x0D
47 #define KMX61_ACC_ZOUT_L 0x0E
48 #define KMX61_ACC_ZOUT_H 0x0F
51 * one 16-bit temperature output register
53 #define KMX61_TEMP_L 0x10
54 #define KMX61_TEMP_H 0x11
57 * three 16-bit magnetometer output registers for X/Y/Z axis
59 #define KMX61_MAG_XOUT_L 0x12
60 #define KMX61_MAG_XOUT_H 0x13
61 #define KMX61_MAG_YOUT_L 0x14
62 #define KMX61_MAG_YOUT_H 0x15
63 #define KMX61_MAG_ZOUT_L 0x16
64 #define KMX61_MAG_ZOUT_H 0x17
66 #define KMX61_REG_INL 0x28
67 #define KMX61_REG_STBY 0x29
68 #define KMX61_REG_CTRL1 0x2A
69 #define KMX61_REG_CTRL2 0x2B
70 #define KMX61_REG_ODCNTL 0x2C
71 #define KMX61_REG_INC1 0x2D
73 #define KMX61_REG_WUF_THRESH 0x3D
74 #define KMX61_REG_WUF_TIMER 0x3E
76 #define KMX61_ACC_STBY_BIT BIT(0)
77 #define KMX61_MAG_STBY_BIT BIT(1)
78 #define KMX61_ACT_STBY_BIT BIT(7)
80 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
82 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
84 #define KMX61_REG_INS2_BIT_ZP BIT(0)
85 #define KMX61_REG_INS2_BIT_ZN BIT(1)
86 #define KMX61_REG_INS2_BIT_YP BIT(2)
87 #define KMX61_REG_INS2_BIT_YN BIT(3)
88 #define KMX61_REG_INS2_BIT_XP BIT(4)
89 #define KMX61_REG_INS2_BIT_XN BIT(5)
91 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
93 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
94 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
95 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
96 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
98 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
99 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
100 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
101 #define KMX61_REG_INC1_BIT_IEN BIT(5)
103 #define KMX61_ACC_ODR_SHIFT 0
104 #define KMX61_MAG_ODR_SHIFT 4
105 #define KMX61_ACC_ODR_MASK 0x0F
106 #define KMX61_MAG_ODR_MASK 0xF0
108 #define KMX61_OWUF_MASK 0x7
110 #define KMX61_DEFAULT_WAKE_THRESH 1
111 #define KMX61_DEFAULT_WAKE_DURATION 1
113 #define KMX61_SLEEP_DELAY_MS 2000
115 #define KMX61_CHIP_ID 0x12
118 #define KMX61_ACC 0x01
119 #define KMX61_MAG 0x02
122 struct i2c_client
*client
;
124 /* serialize access to non-atomic ops, e.g set_mode */
141 /* accelerometer specific data */
142 struct iio_dev
*acc_indio_dev
;
143 struct iio_trigger
*acc_dready_trig
;
144 struct iio_trigger
*motion_trig
;
145 bool acc_dready_trig_on
;
147 bool ev_enable_state
;
149 /* magnetometer specific data */
150 struct iio_dev
*mag_indio_dev
;
151 struct iio_trigger
*mag_dready_trig
;
152 bool mag_dready_trig_on
;
167 static const u16 kmx61_uscale_table
[] = {9582, 19163, 38326};
169 static const struct {
173 } kmx61_samp_freq_table
[] = { {12, 500000, 0x00},
186 static const struct {
190 } kmx61_wake_up_odr_table
[] = { {0, 781000, 0x00},
203 static IIO_CONST_ATTR(accel_scale_available
, "0.009582 0.019163 0.038326");
204 static IIO_CONST_ATTR(magn_scale_available
, "0.001465");
205 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
206 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
208 static struct attribute
*kmx61_acc_attributes
[] = {
209 &iio_const_attr_accel_scale_available
.dev_attr
.attr
,
210 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
214 static struct attribute
*kmx61_mag_attributes
[] = {
215 &iio_const_attr_magn_scale_available
.dev_attr
.attr
,
216 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
220 static const struct attribute_group kmx61_acc_attribute_group
= {
221 .attrs
= kmx61_acc_attributes
,
224 static const struct attribute_group kmx61_mag_attribute_group
= {
225 .attrs
= kmx61_mag_attributes
,
228 static const struct iio_event_spec kmx61_event
= {
229 .type
= IIO_EV_TYPE_THRESH
,
230 .dir
= IIO_EV_DIR_EITHER
,
231 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
232 BIT(IIO_EV_INFO_ENABLE
) |
233 BIT(IIO_EV_INFO_PERIOD
),
236 #define KMX61_ACC_CHAN(_axis) { \
239 .channel2 = IIO_MOD_ ## _axis, \
240 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
241 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
242 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
243 .address = KMX61_ACC, \
244 .scan_index = KMX61_AXIS_ ## _axis, \
250 .endianness = IIO_LE, \
252 .event_spec = &kmx61_event, \
253 .num_event_specs = 1 \
256 #define KMX61_MAG_CHAN(_axis) { \
259 .channel2 = IIO_MOD_ ## _axis, \
260 .address = KMX61_MAG, \
261 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
262 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
263 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
264 .scan_index = KMX61_AXIS_ ## _axis, \
270 .endianness = IIO_LE, \
274 static const struct iio_chan_spec kmx61_acc_channels
[] = {
280 static const struct iio_chan_spec kmx61_mag_channels
[] = {
286 static void kmx61_set_data(struct iio_dev
*indio_dev
, struct kmx61_data
*data
)
288 struct kmx61_data
**priv
= iio_priv(indio_dev
);
293 static struct kmx61_data
*kmx61_get_data(struct iio_dev
*indio_dev
)
295 return *(struct kmx61_data
**)iio_priv(indio_dev
);
298 static int kmx61_convert_freq_to_bit(int val
, int val2
)
302 for (i
= 0; i
< ARRAY_SIZE(kmx61_samp_freq_table
); i
++)
303 if (val
== kmx61_samp_freq_table
[i
].val
&&
304 val2
== kmx61_samp_freq_table
[i
].val2
)
305 return kmx61_samp_freq_table
[i
].odr_bits
;
309 static int kmx61_convert_bit_to_freq(u8 odr_bits
, int *val
, int *val2
)
313 for (i
= 0; i
< ARRAY_SIZE(kmx61_samp_freq_table
); i
++)
314 if (odr_bits
== kmx61_samp_freq_table
[i
].odr_bits
) {
315 *val
= kmx61_samp_freq_table
[i
].val
;
316 *val2
= kmx61_samp_freq_table
[i
].val2
;
323 static int kmx61_convert_wake_up_odr_to_bit(int val
, int val2
)
327 for (i
= 0; i
< ARRAY_SIZE(kmx61_wake_up_odr_table
); ++i
)
328 if (kmx61_wake_up_odr_table
[i
].val
== val
&&
329 kmx61_wake_up_odr_table
[i
].val2
== val2
)
330 return kmx61_wake_up_odr_table
[i
].odr_bits
;
335 * kmx61_set_mode() - set KMX61 device operating mode
336 * @data - kmx61 device private data pointer
337 * @mode - bitmask, indicating operating mode for @device
338 * @device - bitmask, indicating device for which @mode needs to be set
339 * @update - update stby bits stored in device's private @data
341 * For each sensor (accelerometer/magnetometer) there are two operating modes
342 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
343 * if they are both enabled. Internal sensors state is saved in acc_stby and
344 * mag_stby members of driver's private @data.
346 static int kmx61_set_mode(struct kmx61_data
*data
, u8 mode
, u8 device
,
350 int acc_stby
= -1, mag_stby
= -1;
352 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_STBY
);
354 dev_err(&data
->client
->dev
, "Error reading reg_stby\n");
357 if (device
& KMX61_ACC
) {
358 if (mode
& KMX61_ACC_STBY_BIT
) {
359 ret
|= KMX61_ACC_STBY_BIT
;
362 ret
&= ~KMX61_ACC_STBY_BIT
;
367 if (device
& KMX61_MAG
) {
368 if (mode
& KMX61_MAG_STBY_BIT
) {
369 ret
|= KMX61_MAG_STBY_BIT
;
372 ret
&= ~KMX61_MAG_STBY_BIT
;
377 if (mode
& KMX61_ACT_STBY_BIT
)
378 ret
|= KMX61_ACT_STBY_BIT
;
380 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_STBY
, ret
);
382 dev_err(&data
->client
->dev
, "Error writing reg_stby\n");
386 if (acc_stby
!= -1 && update
)
387 data
->acc_stby
= acc_stby
;
388 if (mag_stby
!= -1 && update
)
389 data
->mag_stby
= mag_stby
;
394 static int kmx61_get_mode(struct kmx61_data
*data
, u8
*mode
, u8 device
)
398 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_STBY
);
400 dev_err(&data
->client
->dev
, "Error reading reg_stby\n");
405 if (device
& KMX61_ACC
) {
406 if (ret
& KMX61_ACC_STBY_BIT
)
407 *mode
|= KMX61_ACC_STBY_BIT
;
409 *mode
&= ~KMX61_ACC_STBY_BIT
;
412 if (device
& KMX61_MAG
) {
413 if (ret
& KMX61_MAG_STBY_BIT
)
414 *mode
|= KMX61_MAG_STBY_BIT
;
416 *mode
&= ~KMX61_MAG_STBY_BIT
;
422 static int kmx61_set_wake_up_odr(struct kmx61_data
*data
, int val
, int val2
)
426 odr_bits
= kmx61_convert_wake_up_odr_to_bit(val
, val2
);
430 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL2
,
433 dev_err(&data
->client
->dev
, "Error writing reg_ctrl2\n");
437 static int kmx61_set_odr(struct kmx61_data
*data
, int val
, int val2
, u8 device
)
441 int lodr_bits
, odr_bits
;
443 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
447 lodr_bits
= kmx61_convert_freq_to_bit(val
, val2
);
451 /* To change ODR, accel and magn must be in STDBY */
452 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
458 if (device
& KMX61_ACC
)
459 odr_bits
|= lodr_bits
<< KMX61_ACC_ODR_SHIFT
;
460 if (device
& KMX61_MAG
)
461 odr_bits
|= lodr_bits
<< KMX61_MAG_ODR_SHIFT
;
463 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_ODCNTL
,
468 data
->odr_bits
= odr_bits
;
470 if (device
& KMX61_ACC
) {
471 ret
= kmx61_set_wake_up_odr(data
, val
, val2
);
476 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
479 static int kmx61_get_odr(struct kmx61_data
*data
, int *val
, int *val2
,
484 if (device
& KMX61_ACC
)
485 lodr_bits
= (data
->odr_bits
>> KMX61_ACC_ODR_SHIFT
) &
487 else if (device
& KMX61_MAG
)
488 lodr_bits
= (data
->odr_bits
>> KMX61_MAG_ODR_SHIFT
) &
493 for (i
= 0; i
< ARRAY_SIZE(kmx61_samp_freq_table
); i
++)
494 if (lodr_bits
== kmx61_samp_freq_table
[i
].odr_bits
) {
495 *val
= kmx61_samp_freq_table
[i
].val
;
496 *val2
= kmx61_samp_freq_table
[i
].val2
;
502 static int kmx61_set_range(struct kmx61_data
*data
, u8 range
)
506 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
508 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
512 ret
&= ~KMX61_REG_CTRL1_GSEL_MASK
;
513 ret
|= range
& KMX61_REG_CTRL1_GSEL_MASK
;
515 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
517 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
526 static int kmx61_set_scale(struct kmx61_data
*data
, u16 uscale
)
531 for (i
= 0; i
< ARRAY_SIZE(kmx61_uscale_table
); i
++) {
532 if (kmx61_uscale_table
[i
] == uscale
) {
533 ret
= kmx61_get_mode(data
, &mode
,
534 KMX61_ACC
| KMX61_MAG
);
538 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
,
539 KMX61_ACC
| KMX61_MAG
, true);
543 ret
= kmx61_set_range(data
, i
);
547 return kmx61_set_mode(data
, mode
,
548 KMX61_ACC
| KMX61_MAG
, true);
554 static int kmx61_chip_init(struct kmx61_data
*data
)
558 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_WHO_AM_I
);
560 dev_err(&data
->client
->dev
, "Error reading who_am_i\n");
564 if (ret
!= KMX61_CHIP_ID
) {
565 dev_err(&data
->client
->dev
,
566 "Wrong chip id, got %x expected %x\n",
571 /* set accel 12bit, 4g range */
572 ret
= kmx61_set_range(data
, KMX61_RANGE_4G
);
576 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_ODCNTL
);
578 dev_err(&data
->client
->dev
, "Error reading reg_odcntl\n");
581 data
->odr_bits
= ret
;
583 /* set output data rate for wake up (motion detection) function */
584 ret
= kmx61_convert_bit_to_freq(data
->odr_bits
, &val
, &val2
);
588 ret
= kmx61_set_wake_up_odr(data
, val
, val2
);
592 /* set acc/magn to OPERATION mode */
593 ret
= kmx61_set_mode(data
, 0, KMX61_ACC
| KMX61_MAG
, true);
597 data
->wake_thresh
= KMX61_DEFAULT_WAKE_THRESH
;
598 data
->wake_duration
= KMX61_DEFAULT_WAKE_DURATION
;
603 static int kmx61_setup_new_data_interrupt(struct kmx61_data
*data
,
604 bool status
, u8 device
)
609 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
613 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
617 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INC1
);
619 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
624 ret
|= KMX61_REG_INC1_BIT_IEN
;
625 if (device
& KMX61_ACC
)
626 ret
|= KMX61_REG_INC1_BIT_DRDYA
;
627 if (device
& KMX61_MAG
)
628 ret
|= KMX61_REG_INC1_BIT_DRDYM
;
630 ret
&= ~KMX61_REG_INC1_BIT_IEN
;
631 if (device
& KMX61_ACC
)
632 ret
&= ~KMX61_REG_INC1_BIT_DRDYA
;
633 if (device
& KMX61_MAG
)
634 ret
&= ~KMX61_REG_INC1_BIT_DRDYM
;
636 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_INC1
, ret
);
638 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
642 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
644 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
649 ret
|= KMX61_REG_CTRL1_BIT_DRDYE
;
651 ret
&= ~KMX61_REG_CTRL1_BIT_DRDYE
;
653 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
655 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
659 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
662 static int kmx61_chip_update_thresholds(struct kmx61_data
*data
)
666 ret
= i2c_smbus_write_byte_data(data
->client
,
668 data
->wake_duration
);
670 dev_err(&data
->client
->dev
, "Errow writing reg_wuf_timer\n");
674 ret
= i2c_smbus_write_byte_data(data
->client
,
675 KMX61_REG_WUF_THRESH
,
678 dev_err(&data
->client
->dev
, "Error writing reg_wuf_thresh\n");
683 static int kmx61_setup_any_motion_interrupt(struct kmx61_data
*data
,
689 ret
= kmx61_get_mode(data
, &mode
, KMX61_ACC
| KMX61_MAG
);
693 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
697 ret
= kmx61_chip_update_thresholds(data
);
701 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INC1
);
703 dev_err(&data
->client
->dev
, "Error reading reg_inc1\n");
707 ret
|= (KMX61_REG_INC1_BIT_IEN
| KMX61_REG_INC1_BIT_WUFS
);
709 ret
&= ~(KMX61_REG_INC1_BIT_IEN
| KMX61_REG_INC1_BIT_WUFS
);
711 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_INC1
, ret
);
713 dev_err(&data
->client
->dev
, "Error writing reg_inc1\n");
717 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
719 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
724 ret
|= KMX61_REG_CTRL1_BIT_WUFE
| KMX61_REG_CTRL1_BIT_BTSE
;
726 ret
&= ~(KMX61_REG_CTRL1_BIT_WUFE
| KMX61_REG_CTRL1_BIT_BTSE
);
728 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
730 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
733 mode
|= KMX61_ACT_STBY_BIT
;
734 return kmx61_set_mode(data
, mode
, KMX61_ACC
| KMX61_MAG
, true);
738 * kmx61_set_power_state() - set power state for kmx61 @device
739 * @data - kmx61 device private pointer
740 * @on - power state to be set for @device
741 * @device - bitmask indicating device for which @on state needs to be set
743 * Notice that when ACC power state needs to be set to ON and MAG is in
744 * OPERATION then we know that kmx61_runtime_resume was already called
745 * so we must set ACC OPERATION mode here. The same happens when MAG power
746 * state needs to be set to ON and ACC is in OPERATION.
748 static int kmx61_set_power_state(struct kmx61_data
*data
, bool on
, u8 device
)
753 if (device
& KMX61_ACC
) {
754 if (on
&& !data
->acc_ps
&& !data
->mag_stby
) {
755 ret
= kmx61_set_mode(data
, 0, KMX61_ACC
, true);
761 if (device
& KMX61_MAG
) {
762 if (on
&& !data
->mag_ps
&& !data
->acc_stby
) {
763 ret
= kmx61_set_mode(data
, 0, KMX61_MAG
, true);
771 ret
= pm_runtime_get_sync(&data
->client
->dev
);
773 pm_runtime_mark_last_busy(&data
->client
->dev
);
774 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
777 dev_err(&data
->client
->dev
,
778 "Failed: kmx61_set_power_state for %d, ret %d\n",
781 pm_runtime_put_noidle(&data
->client
->dev
);
789 static int kmx61_read_measurement(struct kmx61_data
*data
, u8 base
, u8 offset
)
792 u8 reg
= base
+ offset
* 2;
794 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
796 dev_err(&data
->client
->dev
, "failed to read reg at %x\n", reg
);
801 static int kmx61_read_raw(struct iio_dev
*indio_dev
,
802 struct iio_chan_spec
const *chan
, int *val
,
803 int *val2
, long mask
)
807 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
810 case IIO_CHAN_INFO_RAW
:
811 switch (chan
->type
) {
813 base_reg
= KMX61_ACC_XOUT_L
;
816 base_reg
= KMX61_MAG_XOUT_L
;
821 mutex_lock(&data
->lock
);
823 ret
= kmx61_set_power_state(data
, true, chan
->address
);
825 mutex_unlock(&data
->lock
);
829 ret
= kmx61_read_measurement(data
, base_reg
, chan
->scan_index
);
831 kmx61_set_power_state(data
, false, chan
->address
);
832 mutex_unlock(&data
->lock
);
835 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
836 chan
->scan_type
.realbits
- 1);
837 ret
= kmx61_set_power_state(data
, false, chan
->address
);
839 mutex_unlock(&data
->lock
);
843 case IIO_CHAN_INFO_SCALE
:
844 switch (chan
->type
) {
847 *val2
= kmx61_uscale_table
[data
->range
];
848 return IIO_VAL_INT_PLUS_MICRO
;
850 /* 14 bits res, 1465 microGauss per magn count */
853 return IIO_VAL_INT_PLUS_MICRO
;
857 case IIO_CHAN_INFO_SAMP_FREQ
:
858 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
861 mutex_lock(&data
->lock
);
862 ret
= kmx61_get_odr(data
, val
, val2
, chan
->address
);
863 mutex_unlock(&data
->lock
);
866 return IIO_VAL_INT_PLUS_MICRO
;
871 static int kmx61_write_raw(struct iio_dev
*indio_dev
,
872 struct iio_chan_spec
const *chan
, int val
,
876 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
879 case IIO_CHAN_INFO_SAMP_FREQ
:
880 if (chan
->type
!= IIO_ACCEL
&& chan
->type
!= IIO_MAGN
)
883 mutex_lock(&data
->lock
);
884 ret
= kmx61_set_odr(data
, val
, val2
, chan
->address
);
885 mutex_unlock(&data
->lock
);
887 case IIO_CHAN_INFO_SCALE
:
888 switch (chan
->type
) {
892 mutex_lock(&data
->lock
);
893 ret
= kmx61_set_scale(data
, val2
);
894 mutex_unlock(&data
->lock
);
904 static int kmx61_read_event(struct iio_dev
*indio_dev
,
905 const struct iio_chan_spec
*chan
,
906 enum iio_event_type type
,
907 enum iio_event_direction dir
,
908 enum iio_event_info info
,
911 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
915 case IIO_EV_INFO_VALUE
:
916 *val
= data
->wake_thresh
;
918 case IIO_EV_INFO_PERIOD
:
919 *val
= data
->wake_duration
;
926 static int kmx61_write_event(struct iio_dev
*indio_dev
,
927 const struct iio_chan_spec
*chan
,
928 enum iio_event_type type
,
929 enum iio_event_direction dir
,
930 enum iio_event_info info
,
933 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
935 if (data
->ev_enable_state
)
939 case IIO_EV_INFO_VALUE
:
940 data
->wake_thresh
= val
;
942 case IIO_EV_INFO_PERIOD
:
943 data
->wake_duration
= val
;
950 static int kmx61_read_event_config(struct iio_dev
*indio_dev
,
951 const struct iio_chan_spec
*chan
,
952 enum iio_event_type type
,
953 enum iio_event_direction dir
)
955 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
957 return data
->ev_enable_state
;
960 static int kmx61_write_event_config(struct iio_dev
*indio_dev
,
961 const struct iio_chan_spec
*chan
,
962 enum iio_event_type type
,
963 enum iio_event_direction dir
,
966 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
969 if (state
&& data
->ev_enable_state
)
972 mutex_lock(&data
->lock
);
974 if (!state
&& data
->motion_trig_on
) {
975 data
->ev_enable_state
= false;
979 ret
= kmx61_set_power_state(data
, state
, KMX61_ACC
);
983 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
985 kmx61_set_power_state(data
, false, KMX61_ACC
);
989 data
->ev_enable_state
= state
;
992 mutex_unlock(&data
->lock
);
997 static int kmx61_acc_validate_trigger(struct iio_dev
*indio_dev
,
998 struct iio_trigger
*trig
)
1000 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1002 if (data
->acc_dready_trig
!= trig
&& data
->motion_trig
!= trig
)
1008 static int kmx61_mag_validate_trigger(struct iio_dev
*indio_dev
,
1009 struct iio_trigger
*trig
)
1011 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1013 if (data
->mag_dready_trig
!= trig
)
1019 static const struct iio_info kmx61_acc_info
= {
1020 .driver_module
= THIS_MODULE
,
1021 .read_raw
= kmx61_read_raw
,
1022 .write_raw
= kmx61_write_raw
,
1023 .attrs
= &kmx61_acc_attribute_group
,
1024 .read_event_value
= kmx61_read_event
,
1025 .write_event_value
= kmx61_write_event
,
1026 .read_event_config
= kmx61_read_event_config
,
1027 .write_event_config
= kmx61_write_event_config
,
1028 .validate_trigger
= kmx61_acc_validate_trigger
,
1031 static const struct iio_info kmx61_mag_info
= {
1032 .driver_module
= THIS_MODULE
,
1033 .read_raw
= kmx61_read_raw
,
1034 .write_raw
= kmx61_write_raw
,
1035 .attrs
= &kmx61_mag_attribute_group
,
1036 .validate_trigger
= kmx61_mag_validate_trigger
,
1040 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1046 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1047 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1049 mutex_lock(&data
->lock
);
1051 if (!state
&& data
->ev_enable_state
&& data
->motion_trig_on
) {
1052 data
->motion_trig_on
= false;
1056 if (data
->acc_dready_trig
== trig
|| data
->motion_trig
== trig
)
1061 ret
= kmx61_set_power_state(data
, state
, device
);
1065 if (data
->acc_dready_trig
== trig
|| data
->mag_dready_trig
== trig
)
1066 ret
= kmx61_setup_new_data_interrupt(data
, state
, device
);
1068 ret
= kmx61_setup_any_motion_interrupt(data
, state
);
1070 kmx61_set_power_state(data
, false, device
);
1074 if (data
->acc_dready_trig
== trig
)
1075 data
->acc_dready_trig_on
= state
;
1076 else if (data
->mag_dready_trig
== trig
)
1077 data
->mag_dready_trig_on
= state
;
1079 data
->motion_trig_on
= state
;
1081 mutex_unlock(&data
->lock
);
1086 static int kmx61_trig_try_reenable(struct iio_trigger
*trig
)
1088 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1089 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1092 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1094 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1101 static const struct iio_trigger_ops kmx61_trigger_ops
= {
1102 .set_trigger_state
= kmx61_data_rdy_trigger_set_state
,
1103 .try_reenable
= kmx61_trig_try_reenable
,
1104 .owner
= THIS_MODULE
,
1107 static irqreturn_t
kmx61_event_handler(int irq
, void *private)
1109 struct kmx61_data
*data
= private;
1110 struct iio_dev
*indio_dev
= data
->acc_indio_dev
;
1113 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS1
);
1115 dev_err(&data
->client
->dev
, "Error reading reg_ins1\n");
1119 if (ret
& KMX61_REG_INS1_BIT_WUFS
) {
1120 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INS2
);
1122 dev_err(&data
->client
->dev
, "Error reading reg_ins2\n");
1126 if (ret
& KMX61_REG_INS2_BIT_XN
)
1127 iio_push_event(indio_dev
,
1128 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1132 IIO_EV_DIR_FALLING
),
1135 if (ret
& KMX61_REG_INS2_BIT_XP
)
1136 iio_push_event(indio_dev
,
1137 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1144 if (ret
& KMX61_REG_INS2_BIT_YN
)
1145 iio_push_event(indio_dev
,
1146 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1150 IIO_EV_DIR_FALLING
),
1153 if (ret
& KMX61_REG_INS2_BIT_YP
)
1154 iio_push_event(indio_dev
,
1155 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1162 if (ret
& KMX61_REG_INS2_BIT_ZN
)
1163 iio_push_event(indio_dev
,
1164 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1168 IIO_EV_DIR_FALLING
),
1171 if (ret
& KMX61_REG_INS2_BIT_ZP
)
1172 iio_push_event(indio_dev
,
1173 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1182 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_CTRL1
);
1184 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
1186 ret
|= KMX61_REG_CTRL1_BIT_RES
;
1187 ret
= i2c_smbus_write_byte_data(data
->client
, KMX61_REG_CTRL1
, ret
);
1189 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
1191 ret
= i2c_smbus_read_byte_data(data
->client
, KMX61_REG_INL
);
1193 dev_err(&data
->client
->dev
, "Error reading reg_inl\n");
1198 static irqreturn_t
kmx61_data_rdy_trig_poll(int irq
, void *private)
1200 struct kmx61_data
*data
= private;
1202 if (data
->acc_dready_trig_on
)
1203 iio_trigger_poll(data
->acc_dready_trig
);
1204 if (data
->mag_dready_trig_on
)
1205 iio_trigger_poll(data
->mag_dready_trig
);
1207 if (data
->motion_trig_on
)
1208 iio_trigger_poll(data
->motion_trig
);
1210 if (data
->ev_enable_state
)
1211 return IRQ_WAKE_THREAD
;
1215 static irqreturn_t
kmx61_trigger_handler(int irq
, void *p
)
1217 struct iio_poll_func
*pf
= p
;
1218 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1219 struct kmx61_data
*data
= kmx61_get_data(indio_dev
);
1220 int bit
, ret
, i
= 0;
1224 if (indio_dev
== data
->acc_indio_dev
)
1225 base
= KMX61_ACC_XOUT_L
;
1227 base
= KMX61_MAG_XOUT_L
;
1229 mutex_lock(&data
->lock
);
1230 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1231 indio_dev
->masklength
) {
1232 ret
= kmx61_read_measurement(data
, base
, bit
);
1234 mutex_unlock(&data
->lock
);
1239 mutex_unlock(&data
->lock
);
1241 iio_push_to_buffers(indio_dev
, buffer
);
1243 iio_trigger_notify_done(indio_dev
->trig
);
1248 static const char *kmx61_match_acpi_device(struct device
*dev
)
1250 const struct acpi_device_id
*id
;
1252 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1255 return dev_name(dev
);
1258 static int kmx61_gpio_probe(struct i2c_client
*client
, struct kmx61_data
*data
)
1261 struct gpio_desc
*gpio
;
1269 /* data ready gpio interrupt pin */
1270 gpio
= devm_gpiod_get_index(dev
, KMX61_GPIO_NAME
, 0);
1272 dev_err(dev
, "acpi gpio get index failed\n");
1273 return PTR_ERR(gpio
);
1276 ret
= gpiod_direction_input(gpio
);
1280 ret
= gpiod_to_irq(gpio
);
1282 dev_dbg(dev
, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio
), ret
);
1286 static struct iio_dev
*kmx61_indiodev_setup(struct kmx61_data
*data
,
1287 const struct iio_info
*info
,
1288 const struct iio_chan_spec
*chan
,
1292 struct iio_dev
*indio_dev
;
1294 indio_dev
= devm_iio_device_alloc(&data
->client
->dev
, sizeof(data
));
1296 return ERR_PTR(-ENOMEM
);
1298 kmx61_set_data(indio_dev
, data
);
1300 indio_dev
->dev
.parent
= &data
->client
->dev
;
1301 indio_dev
->channels
= chan
;
1302 indio_dev
->num_channels
= num_channels
;
1303 indio_dev
->name
= name
;
1304 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1305 indio_dev
->info
= info
;
1310 static struct iio_trigger
*kmx61_trigger_setup(struct kmx61_data
*data
,
1311 struct iio_dev
*indio_dev
,
1314 struct iio_trigger
*trig
;
1317 trig
= devm_iio_trigger_alloc(&data
->client
->dev
,
1323 return ERR_PTR(-ENOMEM
);
1325 trig
->dev
.parent
= &data
->client
->dev
;
1326 trig
->ops
= &kmx61_trigger_ops
;
1327 iio_trigger_set_drvdata(trig
, indio_dev
);
1329 ret
= iio_trigger_register(trig
);
1331 return ERR_PTR(ret
);
1336 static int kmx61_probe(struct i2c_client
*client
,
1337 const struct i2c_device_id
*id
)
1340 struct kmx61_data
*data
;
1341 const char *name
= NULL
;
1343 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1347 i2c_set_clientdata(client
, data
);
1348 data
->client
= client
;
1350 mutex_init(&data
->lock
);
1354 else if (ACPI_HANDLE(&client
->dev
))
1355 name
= kmx61_match_acpi_device(&client
->dev
);
1359 data
->acc_indio_dev
=
1360 kmx61_indiodev_setup(data
, &kmx61_acc_info
,
1362 ARRAY_SIZE(kmx61_acc_channels
),
1364 if (IS_ERR(data
->acc_indio_dev
))
1365 return PTR_ERR(data
->acc_indio_dev
);
1367 data
->mag_indio_dev
=
1368 kmx61_indiodev_setup(data
, &kmx61_mag_info
,
1370 ARRAY_SIZE(kmx61_mag_channels
),
1372 if (IS_ERR(data
->mag_indio_dev
))
1373 return PTR_ERR(data
->mag_indio_dev
);
1375 ret
= kmx61_chip_init(data
);
1379 if (client
->irq
< 0)
1380 client
->irq
= kmx61_gpio_probe(client
, data
);
1382 if (client
->irq
>= 0) {
1383 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1384 kmx61_data_rdy_trig_poll
,
1385 kmx61_event_handler
,
1386 IRQF_TRIGGER_RISING
,
1390 goto err_chip_uninit
;
1392 data
->acc_dready_trig
=
1393 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1395 if (IS_ERR(data
->acc_dready_trig
)) {
1396 ret
= PTR_ERR(data
->acc_dready_trig
);
1397 goto err_chip_uninit
;
1400 data
->mag_dready_trig
=
1401 kmx61_trigger_setup(data
, data
->mag_indio_dev
,
1403 if (IS_ERR(data
->mag_dready_trig
)) {
1404 ret
= PTR_ERR(data
->mag_dready_trig
);
1405 goto err_trigger_unregister_acc_dready
;
1409 kmx61_trigger_setup(data
, data
->acc_indio_dev
,
1411 if (IS_ERR(data
->motion_trig
)) {
1412 ret
= PTR_ERR(data
->motion_trig
);
1413 goto err_trigger_unregister_mag_dready
;
1416 ret
= iio_triggered_buffer_setup(data
->acc_indio_dev
,
1417 &iio_pollfunc_store_time
,
1418 kmx61_trigger_handler
,
1421 dev_err(&data
->client
->dev
,
1422 "Failed to setup acc triggered buffer\n");
1423 goto err_trigger_unregister_motion
;
1426 ret
= iio_triggered_buffer_setup(data
->mag_indio_dev
,
1427 &iio_pollfunc_store_time
,
1428 kmx61_trigger_handler
,
1431 dev_err(&data
->client
->dev
,
1432 "Failed to setup mag triggered buffer\n");
1433 goto err_buffer_cleanup_acc
;
1437 ret
= iio_device_register(data
->acc_indio_dev
);
1439 dev_err(&client
->dev
, "Failed to register acc iio device\n");
1440 goto err_buffer_cleanup_mag
;
1443 ret
= iio_device_register(data
->mag_indio_dev
);
1445 dev_err(&client
->dev
, "Failed to register mag iio device\n");
1446 goto err_iio_unregister_acc
;
1449 ret
= pm_runtime_set_active(&client
->dev
);
1451 goto err_iio_unregister_mag
;
1453 pm_runtime_enable(&client
->dev
);
1454 pm_runtime_set_autosuspend_delay(&client
->dev
, KMX61_SLEEP_DELAY_MS
);
1455 pm_runtime_use_autosuspend(&client
->dev
);
1459 err_iio_unregister_mag
:
1460 iio_device_unregister(data
->mag_indio_dev
);
1461 err_iio_unregister_acc
:
1462 iio_device_unregister(data
->acc_indio_dev
);
1463 err_buffer_cleanup_mag
:
1464 if (client
->irq
>= 0)
1465 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1466 err_buffer_cleanup_acc
:
1467 if (client
->irq
>= 0)
1468 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1469 err_trigger_unregister_motion
:
1470 iio_trigger_unregister(data
->motion_trig
);
1471 err_trigger_unregister_mag_dready
:
1472 iio_trigger_unregister(data
->mag_dready_trig
);
1473 err_trigger_unregister_acc_dready
:
1474 iio_trigger_unregister(data
->acc_dready_trig
);
1476 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1480 static int kmx61_remove(struct i2c_client
*client
)
1482 struct kmx61_data
*data
= i2c_get_clientdata(client
);
1484 pm_runtime_disable(&client
->dev
);
1485 pm_runtime_set_suspended(&client
->dev
);
1486 pm_runtime_put_noidle(&client
->dev
);
1488 iio_device_unregister(data
->acc_indio_dev
);
1489 iio_device_unregister(data
->mag_indio_dev
);
1491 if (client
->irq
>= 0) {
1492 iio_triggered_buffer_cleanup(data
->acc_indio_dev
);
1493 iio_triggered_buffer_cleanup(data
->mag_indio_dev
);
1494 iio_trigger_unregister(data
->acc_dready_trig
);
1495 iio_trigger_unregister(data
->mag_dready_trig
);
1496 iio_trigger_unregister(data
->motion_trig
);
1499 mutex_lock(&data
->lock
);
1500 kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1501 mutex_unlock(&data
->lock
);
1506 #ifdef CONFIG_PM_SLEEP
1507 static int kmx61_suspend(struct device
*dev
)
1510 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1512 mutex_lock(&data
->lock
);
1513 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
,
1515 mutex_unlock(&data
->lock
);
1520 static int kmx61_resume(struct device
*dev
)
1523 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1526 stby
|= KMX61_ACC_STBY_BIT
;
1528 stby
|= KMX61_MAG_STBY_BIT
;
1530 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1535 static int kmx61_runtime_suspend(struct device
*dev
)
1537 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1540 mutex_lock(&data
->lock
);
1541 ret
= kmx61_set_mode(data
, KMX61_ALL_STBY
, KMX61_ACC
| KMX61_MAG
, true);
1542 mutex_unlock(&data
->lock
);
1547 static int kmx61_runtime_resume(struct device
*dev
)
1549 struct kmx61_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
1553 stby
|= KMX61_ACC_STBY_BIT
;
1555 stby
|= KMX61_MAG_STBY_BIT
;
1557 return kmx61_set_mode(data
, stby
, KMX61_ACC
| KMX61_MAG
, true);
1561 static const struct dev_pm_ops kmx61_pm_ops
= {
1562 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend
, kmx61_resume
)
1563 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend
, kmx61_runtime_resume
, NULL
)
1566 static const struct acpi_device_id kmx61_acpi_match
[] = {
1571 MODULE_DEVICE_TABLE(acpi
, kmx61_acpi_match
);
1573 static const struct i2c_device_id kmx61_id
[] = {
1578 MODULE_DEVICE_TABLE(i2c
, kmx61_id
);
1580 static struct i2c_driver kmx61_driver
= {
1582 .name
= KMX61_DRV_NAME
,
1583 .acpi_match_table
= ACPI_PTR(kmx61_acpi_match
),
1584 .pm
= &kmx61_pm_ops
,
1586 .probe
= kmx61_probe
,
1587 .remove
= kmx61_remove
,
1588 .id_table
= kmx61_id
,
1591 module_i2c_driver(kmx61_driver
);
1593 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1594 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1595 MODULE_LICENSE("GPL v2");