2 * KXCJK-1013 3-axis accelerometer driver
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/bitops.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/acpi.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/events.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include <linux/iio/accel/kxcjk_1013.h>
34 #define KXCJK1013_DRV_NAME "kxcjk1013"
35 #define KXCJK1013_IRQ_NAME "kxcjk1013_event"
37 #define KXCJK1013_REG_XOUT_L 0x06
39 * From low byte X axis register, all the other addresses of Y and Z can be
40 * obtained by just applying axis offset. The following axis defines are just
41 * provide clarity, but not used.
43 #define KXCJK1013_REG_XOUT_H 0x07
44 #define KXCJK1013_REG_YOUT_L 0x08
45 #define KXCJK1013_REG_YOUT_H 0x09
46 #define KXCJK1013_REG_ZOUT_L 0x0A
47 #define KXCJK1013_REG_ZOUT_H 0x0B
49 #define KXCJK1013_REG_DCST_RESP 0x0C
50 #define KXCJK1013_REG_WHO_AM_I 0x0F
51 #define KXCJK1013_REG_INT_SRC1 0x16
52 #define KXCJK1013_REG_INT_SRC2 0x17
53 #define KXCJK1013_REG_STATUS_REG 0x18
54 #define KXCJK1013_REG_INT_REL 0x1A
55 #define KXCJK1013_REG_CTRL1 0x1B
56 #define KXCJK1013_REG_CTRL2 0x1D
57 #define KXCJK1013_REG_INT_CTRL1 0x1E
58 #define KXCJK1013_REG_INT_CTRL2 0x1F
59 #define KXCJK1013_REG_DATA_CTRL 0x21
60 #define KXCJK1013_REG_WAKE_TIMER 0x29
61 #define KXCJK1013_REG_SELF_TEST 0x3A
62 #define KXCJK1013_REG_WAKE_THRES 0x6A
64 #define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7)
65 #define KXCJK1013_REG_CTRL1_BIT_RES BIT(6)
66 #define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5)
67 #define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4)
68 #define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3)
69 #define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1)
70 #define KXCJK1013_REG_INT_REG1_BIT_IEA BIT(4)
71 #define KXCJK1013_REG_INT_REG1_BIT_IEN BIT(5)
73 #define KXCJK1013_DATA_MASK_12_BIT 0x0FFF
74 #define KXCJK1013_MAX_STARTUP_TIME_US 100000
76 #define KXCJK1013_SLEEP_DELAY_MS 2000
78 #define KXCJK1013_REG_INT_SRC2_BIT_ZP BIT(0)
79 #define KXCJK1013_REG_INT_SRC2_BIT_ZN BIT(1)
80 #define KXCJK1013_REG_INT_SRC2_BIT_YP BIT(2)
81 #define KXCJK1013_REG_INT_SRC2_BIT_YN BIT(3)
82 #define KXCJK1013_REG_INT_SRC2_BIT_XP BIT(4)
83 #define KXCJK1013_REG_INT_SRC2_BIT_XN BIT(5)
85 #define KXCJK1013_DEFAULT_WAKE_THRES 1
91 KX_MAX_CHIPS
/* this must be last */
94 struct kxcjk1013_data
{
95 struct i2c_client
*client
;
96 struct iio_trigger
*dready_trig
;
97 struct iio_trigger
*motion_trig
;
104 bool active_high_intr
;
105 bool dready_trigger_on
;
107 bool motion_trigger_on
;
109 enum kx_chipset chipset
;
110 bool is_smo8500_device
;
113 enum kxcjk1013_axis
{
120 enum kxcjk1013_mode
{
125 enum kxcjk1013_range
{
131 static const struct {
135 } samp_freq_table
[] = { {0, 781000, 0x08}, {1, 563000, 0x09},
136 {3, 125000, 0x0A}, {6, 250000, 0x0B}, {12, 500000, 0},
137 {25, 0, 0x01}, {50, 0, 0x02}, {100, 0, 0x03},
138 {200, 0, 0x04}, {400, 0, 0x05}, {800, 0, 0x06},
141 /* Refer to section 4 of the specification */
142 static const struct {
145 } odr_start_up_times
[KX_MAX_CHIPS
][12] = {
193 static const struct {
197 } KXCJK1013_scale_table
[] = { {9582, 0, 0},
201 static const struct {
205 } wake_odr_data_rate_table
[] = { {0, 781000, 0x00},
218 static int kxcjk1013_set_mode(struct kxcjk1013_data
*data
,
219 enum kxcjk1013_mode mode
)
223 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
225 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
230 ret
&= ~KXCJK1013_REG_CTRL1_BIT_PC1
;
232 ret
|= KXCJK1013_REG_CTRL1_BIT_PC1
;
234 ret
= i2c_smbus_write_byte_data(data
->client
,
235 KXCJK1013_REG_CTRL1
, ret
);
237 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
244 static int kxcjk1013_get_mode(struct kxcjk1013_data
*data
,
245 enum kxcjk1013_mode
*mode
)
249 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
251 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
255 if (ret
& KXCJK1013_REG_CTRL1_BIT_PC1
)
263 static int kxcjk1013_set_range(struct kxcjk1013_data
*data
, int range_index
)
267 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
269 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
273 ret
&= ~(KXCJK1013_REG_CTRL1_BIT_GSEL0
|
274 KXCJK1013_REG_CTRL1_BIT_GSEL1
);
275 ret
|= (KXCJK1013_scale_table
[range_index
].gsel_0
<< 3);
276 ret
|= (KXCJK1013_scale_table
[range_index
].gsel_1
<< 4);
278 ret
= i2c_smbus_write_byte_data(data
->client
,
282 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
286 data
->range
= range_index
;
291 static int kxcjk1013_chip_init(struct kxcjk1013_data
*data
)
295 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_WHO_AM_I
);
297 dev_err(&data
->client
->dev
, "Error reading who_am_i\n");
301 dev_dbg(&data
->client
->dev
, "KXCJK1013 Chip Id %x\n", ret
);
303 ret
= kxcjk1013_set_mode(data
, STANDBY
);
307 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
309 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
313 /* Set 12 bit mode */
314 ret
|= KXCJK1013_REG_CTRL1_BIT_RES
;
316 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_CTRL1
,
319 dev_err(&data
->client
->dev
, "Error reading reg_ctrl\n");
323 /* Setting range to 4G */
324 ret
= kxcjk1013_set_range(data
, KXCJK1013_RANGE_4G
);
328 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_DATA_CTRL
);
330 dev_err(&data
->client
->dev
, "Error reading reg_data_ctrl\n");
334 data
->odr_bits
= ret
;
336 /* Set up INT polarity */
337 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
);
339 dev_err(&data
->client
->dev
, "Error reading reg_int_ctrl1\n");
343 if (data
->active_high_intr
)
344 ret
|= KXCJK1013_REG_INT_REG1_BIT_IEA
;
346 ret
&= ~KXCJK1013_REG_INT_REG1_BIT_IEA
;
348 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
,
351 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
355 ret
= kxcjk1013_set_mode(data
, OPERATION
);
359 data
->wake_thres
= KXCJK1013_DEFAULT_WAKE_THRES
;
365 static int kxcjk1013_get_startup_times(struct kxcjk1013_data
*data
)
368 int idx
= data
->chipset
;
370 for (i
= 0; i
< ARRAY_SIZE(odr_start_up_times
[idx
]); ++i
) {
371 if (odr_start_up_times
[idx
][i
].odr_bits
== data
->odr_bits
)
372 return odr_start_up_times
[idx
][i
].usec
;
375 return KXCJK1013_MAX_STARTUP_TIME_US
;
379 static int kxcjk1013_set_power_state(struct kxcjk1013_data
*data
, bool on
)
385 ret
= pm_runtime_get_sync(&data
->client
->dev
);
387 pm_runtime_mark_last_busy(&data
->client
->dev
);
388 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
391 dev_err(&data
->client
->dev
,
392 "Failed: kxcjk1013_set_power_state for %d\n", on
);
394 pm_runtime_put_noidle(&data
->client
->dev
);
402 static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data
*data
)
406 ret
= i2c_smbus_write_byte_data(data
->client
,
407 KXCJK1013_REG_WAKE_TIMER
,
410 dev_err(&data
->client
->dev
,
411 "Error writing reg_wake_timer\n");
415 ret
= i2c_smbus_write_byte_data(data
->client
,
416 KXCJK1013_REG_WAKE_THRES
,
419 dev_err(&data
->client
->dev
, "Error writing reg_wake_thres\n");
426 static int kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data
*data
,
430 enum kxcjk1013_mode store_mode
;
432 ret
= kxcjk1013_get_mode(data
, &store_mode
);
436 /* This is requirement by spec to change state to STANDBY */
437 ret
= kxcjk1013_set_mode(data
, STANDBY
);
441 ret
= kxcjk1013_chip_update_thresholds(data
);
445 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
);
447 dev_err(&data
->client
->dev
, "Error reading reg_int_ctrl1\n");
452 ret
|= KXCJK1013_REG_INT_REG1_BIT_IEN
;
454 ret
&= ~KXCJK1013_REG_INT_REG1_BIT_IEN
;
456 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
,
459 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
463 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
465 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
470 ret
|= KXCJK1013_REG_CTRL1_BIT_WUFE
;
472 ret
&= ~KXCJK1013_REG_CTRL1_BIT_WUFE
;
474 ret
= i2c_smbus_write_byte_data(data
->client
,
475 KXCJK1013_REG_CTRL1
, ret
);
477 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
481 if (store_mode
== OPERATION
) {
482 ret
= kxcjk1013_set_mode(data
, OPERATION
);
490 static int kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data
*data
,
494 enum kxcjk1013_mode store_mode
;
496 ret
= kxcjk1013_get_mode(data
, &store_mode
);
500 /* This is requirement by spec to change state to STANDBY */
501 ret
= kxcjk1013_set_mode(data
, STANDBY
);
505 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
);
507 dev_err(&data
->client
->dev
, "Error reading reg_int_ctrl1\n");
512 ret
|= KXCJK1013_REG_INT_REG1_BIT_IEN
;
514 ret
&= ~KXCJK1013_REG_INT_REG1_BIT_IEN
;
516 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
,
519 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
523 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
525 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
530 ret
|= KXCJK1013_REG_CTRL1_BIT_DRDY
;
532 ret
&= ~KXCJK1013_REG_CTRL1_BIT_DRDY
;
534 ret
= i2c_smbus_write_byte_data(data
->client
,
535 KXCJK1013_REG_CTRL1
, ret
);
537 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
541 if (store_mode
== OPERATION
) {
542 ret
= kxcjk1013_set_mode(data
, OPERATION
);
550 static int kxcjk1013_convert_freq_to_bit(int val
, int val2
)
554 for (i
= 0; i
< ARRAY_SIZE(samp_freq_table
); ++i
) {
555 if (samp_freq_table
[i
].val
== val
&&
556 samp_freq_table
[i
].val2
== val2
) {
557 return samp_freq_table
[i
].odr_bits
;
564 static int kxcjk1013_convert_wake_odr_to_bit(int val
, int val2
)
568 for (i
= 0; i
< ARRAY_SIZE(wake_odr_data_rate_table
); ++i
) {
569 if (wake_odr_data_rate_table
[i
].val
== val
&&
570 wake_odr_data_rate_table
[i
].val2
== val2
) {
571 return wake_odr_data_rate_table
[i
].odr_bits
;
578 static int kxcjk1013_set_odr(struct kxcjk1013_data
*data
, int val
, int val2
)
582 enum kxcjk1013_mode store_mode
;
584 ret
= kxcjk1013_get_mode(data
, &store_mode
);
588 odr_bits
= kxcjk1013_convert_freq_to_bit(val
, val2
);
592 /* To change ODR, the chip must be set to STANDBY as per spec */
593 ret
= kxcjk1013_set_mode(data
, STANDBY
);
597 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_DATA_CTRL
,
600 dev_err(&data
->client
->dev
, "Error writing data_ctrl\n");
604 data
->odr_bits
= odr_bits
;
606 odr_bits
= kxcjk1013_convert_wake_odr_to_bit(val
, val2
);
610 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_CTRL2
,
613 dev_err(&data
->client
->dev
, "Error writing reg_ctrl2\n");
617 if (store_mode
== OPERATION
) {
618 ret
= kxcjk1013_set_mode(data
, OPERATION
);
626 static int kxcjk1013_get_odr(struct kxcjk1013_data
*data
, int *val
, int *val2
)
630 for (i
= 0; i
< ARRAY_SIZE(samp_freq_table
); ++i
) {
631 if (samp_freq_table
[i
].odr_bits
== data
->odr_bits
) {
632 *val
= samp_freq_table
[i
].val
;
633 *val2
= samp_freq_table
[i
].val2
;
634 return IIO_VAL_INT_PLUS_MICRO
;
641 static int kxcjk1013_get_acc_reg(struct kxcjk1013_data
*data
, int axis
)
643 u8 reg
= KXCJK1013_REG_XOUT_L
+ axis
* 2;
646 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
648 dev_err(&data
->client
->dev
,
649 "failed to read accel_%c registers\n", 'x' + axis
);
656 static int kxcjk1013_set_scale(struct kxcjk1013_data
*data
, int val
)
659 enum kxcjk1013_mode store_mode
;
661 for (i
= 0; i
< ARRAY_SIZE(KXCJK1013_scale_table
); ++i
) {
662 if (KXCJK1013_scale_table
[i
].scale
== val
) {
663 ret
= kxcjk1013_get_mode(data
, &store_mode
);
667 ret
= kxcjk1013_set_mode(data
, STANDBY
);
671 ret
= kxcjk1013_set_range(data
, i
);
675 if (store_mode
== OPERATION
) {
676 ret
= kxcjk1013_set_mode(data
, OPERATION
);
688 static int kxcjk1013_read_raw(struct iio_dev
*indio_dev
,
689 struct iio_chan_spec
const *chan
, int *val
,
690 int *val2
, long mask
)
692 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
696 case IIO_CHAN_INFO_RAW
:
697 mutex_lock(&data
->mutex
);
698 if (iio_buffer_enabled(indio_dev
))
701 ret
= kxcjk1013_set_power_state(data
, true);
703 mutex_unlock(&data
->mutex
);
706 ret
= kxcjk1013_get_acc_reg(data
, chan
->scan_index
);
708 kxcjk1013_set_power_state(data
, false);
709 mutex_unlock(&data
->mutex
);
712 *val
= sign_extend32(ret
>> 4, 11);
713 ret
= kxcjk1013_set_power_state(data
, false);
715 mutex_unlock(&data
->mutex
);
722 case IIO_CHAN_INFO_SCALE
:
724 *val2
= KXCJK1013_scale_table
[data
->range
].scale
;
725 return IIO_VAL_INT_PLUS_MICRO
;
727 case IIO_CHAN_INFO_SAMP_FREQ
:
728 mutex_lock(&data
->mutex
);
729 ret
= kxcjk1013_get_odr(data
, val
, val2
);
730 mutex_unlock(&data
->mutex
);
738 static int kxcjk1013_write_raw(struct iio_dev
*indio_dev
,
739 struct iio_chan_spec
const *chan
, int val
,
742 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
746 case IIO_CHAN_INFO_SAMP_FREQ
:
747 mutex_lock(&data
->mutex
);
748 ret
= kxcjk1013_set_odr(data
, val
, val2
);
749 mutex_unlock(&data
->mutex
);
751 case IIO_CHAN_INFO_SCALE
:
755 mutex_lock(&data
->mutex
);
756 ret
= kxcjk1013_set_scale(data
, val2
);
757 mutex_unlock(&data
->mutex
);
766 static int kxcjk1013_read_event(struct iio_dev
*indio_dev
,
767 const struct iio_chan_spec
*chan
,
768 enum iio_event_type type
,
769 enum iio_event_direction dir
,
770 enum iio_event_info info
,
773 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
777 case IIO_EV_INFO_VALUE
:
778 *val
= data
->wake_thres
;
780 case IIO_EV_INFO_PERIOD
:
781 *val
= data
->wake_dur
;
790 static int kxcjk1013_write_event(struct iio_dev
*indio_dev
,
791 const struct iio_chan_spec
*chan
,
792 enum iio_event_type type
,
793 enum iio_event_direction dir
,
794 enum iio_event_info info
,
797 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
799 if (data
->ev_enable_state
)
803 case IIO_EV_INFO_VALUE
:
804 data
->wake_thres
= val
;
806 case IIO_EV_INFO_PERIOD
:
807 data
->wake_dur
= val
;
816 static int kxcjk1013_read_event_config(struct iio_dev
*indio_dev
,
817 const struct iio_chan_spec
*chan
,
818 enum iio_event_type type
,
819 enum iio_event_direction dir
)
821 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
823 return data
->ev_enable_state
;
826 static int kxcjk1013_write_event_config(struct iio_dev
*indio_dev
,
827 const struct iio_chan_spec
*chan
,
828 enum iio_event_type type
,
829 enum iio_event_direction dir
,
832 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
835 if (state
&& data
->ev_enable_state
)
838 mutex_lock(&data
->mutex
);
840 if (!state
&& data
->motion_trigger_on
) {
841 data
->ev_enable_state
= 0;
842 mutex_unlock(&data
->mutex
);
847 * We will expect the enable and disable to do operation in
848 * in reverse order. This will happen here anyway as our
849 * resume operation uses sync mode runtime pm calls, the
850 * suspend operation will be delayed by autosuspend delay
851 * So the disable operation will still happen in reverse of
852 * enable operation. When runtime pm is disabled the mode
853 * is always on so sequence doesn't matter
855 ret
= kxcjk1013_set_power_state(data
, state
);
857 mutex_unlock(&data
->mutex
);
861 ret
= kxcjk1013_setup_any_motion_interrupt(data
, state
);
863 kxcjk1013_set_power_state(data
, false);
864 data
->ev_enable_state
= 0;
865 mutex_unlock(&data
->mutex
);
869 data
->ev_enable_state
= state
;
870 mutex_unlock(&data
->mutex
);
875 static int kxcjk1013_buffer_preenable(struct iio_dev
*indio_dev
)
877 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
879 return kxcjk1013_set_power_state(data
, true);
882 static int kxcjk1013_buffer_postdisable(struct iio_dev
*indio_dev
)
884 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
886 return kxcjk1013_set_power_state(data
, false);
889 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
890 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600");
892 static IIO_CONST_ATTR(in_accel_scale_available
, "0.009582 0.019163 0.038326");
894 static struct attribute
*kxcjk1013_attributes
[] = {
895 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
896 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
900 static const struct attribute_group kxcjk1013_attrs_group
= {
901 .attrs
= kxcjk1013_attributes
,
904 static const struct iio_event_spec kxcjk1013_event
= {
905 .type
= IIO_EV_TYPE_THRESH
,
906 .dir
= IIO_EV_DIR_EITHER
,
907 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
908 BIT(IIO_EV_INFO_ENABLE
) |
909 BIT(IIO_EV_INFO_PERIOD
)
912 #define KXCJK1013_CHANNEL(_axis) { \
915 .channel2 = IIO_MOD_##_axis, \
916 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
917 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
918 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
919 .scan_index = AXIS_##_axis, \
925 .endianness = IIO_LE, \
927 .event_spec = &kxcjk1013_event, \
928 .num_event_specs = 1 \
931 static const struct iio_chan_spec kxcjk1013_channels
[] = {
932 KXCJK1013_CHANNEL(X
),
933 KXCJK1013_CHANNEL(Y
),
934 KXCJK1013_CHANNEL(Z
),
935 IIO_CHAN_SOFT_TIMESTAMP(3),
938 static const struct iio_buffer_setup_ops kxcjk1013_buffer_setup_ops
= {
939 .preenable
= kxcjk1013_buffer_preenable
,
940 .postenable
= iio_triggered_buffer_postenable
,
941 .postdisable
= kxcjk1013_buffer_postdisable
,
942 .predisable
= iio_triggered_buffer_predisable
,
945 static const struct iio_info kxcjk1013_info
= {
946 .attrs
= &kxcjk1013_attrs_group
,
947 .read_raw
= kxcjk1013_read_raw
,
948 .write_raw
= kxcjk1013_write_raw
,
949 .read_event_value
= kxcjk1013_read_event
,
950 .write_event_value
= kxcjk1013_write_event
,
951 .write_event_config
= kxcjk1013_write_event_config
,
952 .read_event_config
= kxcjk1013_read_event_config
,
953 .driver_module
= THIS_MODULE
,
956 static const unsigned long kxcjk1013_scan_masks
[] = {0x7, 0};
958 static irqreturn_t
kxcjk1013_trigger_handler(int irq
, void *p
)
960 struct iio_poll_func
*pf
= p
;
961 struct iio_dev
*indio_dev
= pf
->indio_dev
;
962 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
965 mutex_lock(&data
->mutex
);
966 ret
= i2c_smbus_read_i2c_block_data_or_emulated(data
->client
,
967 KXCJK1013_REG_XOUT_L
,
970 mutex_unlock(&data
->mutex
);
974 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
977 iio_trigger_notify_done(indio_dev
->trig
);
982 static int kxcjk1013_trig_try_reen(struct iio_trigger
*trig
)
984 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
985 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
988 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_REL
);
990 dev_err(&data
->client
->dev
, "Error reading reg_int_rel\n");
997 static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1000 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1001 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1004 mutex_lock(&data
->mutex
);
1006 if (!state
&& data
->ev_enable_state
&& data
->motion_trigger_on
) {
1007 data
->motion_trigger_on
= false;
1008 mutex_unlock(&data
->mutex
);
1012 ret
= kxcjk1013_set_power_state(data
, state
);
1014 mutex_unlock(&data
->mutex
);
1017 if (data
->motion_trig
== trig
)
1018 ret
= kxcjk1013_setup_any_motion_interrupt(data
, state
);
1020 ret
= kxcjk1013_setup_new_data_interrupt(data
, state
);
1022 kxcjk1013_set_power_state(data
, false);
1023 mutex_unlock(&data
->mutex
);
1026 if (data
->motion_trig
== trig
)
1027 data
->motion_trigger_on
= state
;
1029 data
->dready_trigger_on
= state
;
1031 mutex_unlock(&data
->mutex
);
1036 static const struct iio_trigger_ops kxcjk1013_trigger_ops
= {
1037 .set_trigger_state
= kxcjk1013_data_rdy_trigger_set_state
,
1038 .try_reenable
= kxcjk1013_trig_try_reen
,
1039 .owner
= THIS_MODULE
,
1042 static irqreturn_t
kxcjk1013_event_handler(int irq
, void *private)
1044 struct iio_dev
*indio_dev
= private;
1045 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1048 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_SRC1
);
1050 dev_err(&data
->client
->dev
, "Error reading reg_int_src1\n");
1055 ret
= i2c_smbus_read_byte_data(data
->client
,
1056 KXCJK1013_REG_INT_SRC2
);
1058 dev_err(&data
->client
->dev
,
1059 "Error reading reg_int_src2\n");
1063 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_XN
)
1064 iio_push_event(indio_dev
,
1065 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1069 IIO_EV_DIR_FALLING
),
1071 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_XP
)
1072 iio_push_event(indio_dev
,
1073 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1081 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_YN
)
1082 iio_push_event(indio_dev
,
1083 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1087 IIO_EV_DIR_FALLING
),
1089 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_YP
)
1090 iio_push_event(indio_dev
,
1091 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1098 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_ZN
)
1099 iio_push_event(indio_dev
,
1100 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1104 IIO_EV_DIR_FALLING
),
1106 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_ZP
)
1107 iio_push_event(indio_dev
,
1108 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1117 if (data
->dready_trigger_on
)
1120 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_REL
);
1122 dev_err(&data
->client
->dev
, "Error reading reg_int_rel\n");
1127 static irqreturn_t
kxcjk1013_data_rdy_trig_poll(int irq
, void *private)
1129 struct iio_dev
*indio_dev
= private;
1130 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1132 data
->timestamp
= iio_get_time_ns(indio_dev
);
1134 if (data
->dready_trigger_on
)
1135 iio_trigger_poll(data
->dready_trig
);
1136 else if (data
->motion_trigger_on
)
1137 iio_trigger_poll(data
->motion_trig
);
1139 if (data
->ev_enable_state
)
1140 return IRQ_WAKE_THREAD
;
1145 static const char *kxcjk1013_match_acpi_device(struct device
*dev
,
1146 enum kx_chipset
*chipset
,
1147 bool *is_smo8500_device
)
1149 const struct acpi_device_id
*id
;
1151 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1155 if (strcmp(id
->id
, "SMO8500") == 0)
1156 *is_smo8500_device
= true;
1158 *chipset
= (enum kx_chipset
)id
->driver_data
;
1160 return dev_name(dev
);
1163 static int kxcjk1013_probe(struct i2c_client
*client
,
1164 const struct i2c_device_id
*id
)
1166 struct kxcjk1013_data
*data
;
1167 struct iio_dev
*indio_dev
;
1168 struct kxcjk_1013_platform_data
*pdata
;
1172 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1176 data
= iio_priv(indio_dev
);
1177 i2c_set_clientdata(client
, indio_dev
);
1178 data
->client
= client
;
1180 pdata
= dev_get_platdata(&client
->dev
);
1182 data
->active_high_intr
= pdata
->active_high_intr
;
1184 data
->active_high_intr
= true; /* default polarity */
1187 data
->chipset
= (enum kx_chipset
)(id
->driver_data
);
1189 } else if (ACPI_HANDLE(&client
->dev
)) {
1190 name
= kxcjk1013_match_acpi_device(&client
->dev
,
1192 &data
->is_smo8500_device
);
1196 ret
= kxcjk1013_chip_init(data
);
1200 mutex_init(&data
->mutex
);
1202 indio_dev
->dev
.parent
= &client
->dev
;
1203 indio_dev
->channels
= kxcjk1013_channels
;
1204 indio_dev
->num_channels
= ARRAY_SIZE(kxcjk1013_channels
);
1205 indio_dev
->available_scan_masks
= kxcjk1013_scan_masks
;
1206 indio_dev
->name
= name
;
1207 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1208 indio_dev
->info
= &kxcjk1013_info
;
1210 if (client
->irq
> 0 && !data
->is_smo8500_device
) {
1211 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1212 kxcjk1013_data_rdy_trig_poll
,
1213 kxcjk1013_event_handler
,
1214 IRQF_TRIGGER_RISING
,
1220 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
1224 if (!data
->dready_trig
) {
1229 data
->motion_trig
= devm_iio_trigger_alloc(&client
->dev
,
1230 "%s-any-motion-dev%d",
1233 if (!data
->motion_trig
) {
1238 data
->dready_trig
->dev
.parent
= &client
->dev
;
1239 data
->dready_trig
->ops
= &kxcjk1013_trigger_ops
;
1240 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
1241 indio_dev
->trig
= data
->dready_trig
;
1242 iio_trigger_get(indio_dev
->trig
);
1243 ret
= iio_trigger_register(data
->dready_trig
);
1247 data
->motion_trig
->dev
.parent
= &client
->dev
;
1248 data
->motion_trig
->ops
= &kxcjk1013_trigger_ops
;
1249 iio_trigger_set_drvdata(data
->motion_trig
, indio_dev
);
1250 ret
= iio_trigger_register(data
->motion_trig
);
1252 data
->motion_trig
= NULL
;
1253 goto err_trigger_unregister
;
1257 ret
= iio_triggered_buffer_setup(indio_dev
,
1258 &iio_pollfunc_store_time
,
1259 kxcjk1013_trigger_handler
,
1260 &kxcjk1013_buffer_setup_ops
);
1262 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
1263 goto err_trigger_unregister
;
1266 ret
= pm_runtime_set_active(&client
->dev
);
1268 goto err_buffer_cleanup
;
1270 pm_runtime_enable(&client
->dev
);
1271 pm_runtime_set_autosuspend_delay(&client
->dev
,
1272 KXCJK1013_SLEEP_DELAY_MS
);
1273 pm_runtime_use_autosuspend(&client
->dev
);
1275 ret
= iio_device_register(indio_dev
);
1277 dev_err(&client
->dev
, "unable to register iio device\n");
1278 goto err_buffer_cleanup
;
1284 if (data
->dready_trig
)
1285 iio_triggered_buffer_cleanup(indio_dev
);
1286 err_trigger_unregister
:
1287 if (data
->dready_trig
)
1288 iio_trigger_unregister(data
->dready_trig
);
1289 if (data
->motion_trig
)
1290 iio_trigger_unregister(data
->motion_trig
);
1292 kxcjk1013_set_mode(data
, STANDBY
);
1297 static int kxcjk1013_remove(struct i2c_client
*client
)
1299 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1300 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1302 iio_device_unregister(indio_dev
);
1304 pm_runtime_disable(&client
->dev
);
1305 pm_runtime_set_suspended(&client
->dev
);
1306 pm_runtime_put_noidle(&client
->dev
);
1308 if (data
->dready_trig
) {
1309 iio_triggered_buffer_cleanup(indio_dev
);
1310 iio_trigger_unregister(data
->dready_trig
);
1311 iio_trigger_unregister(data
->motion_trig
);
1314 mutex_lock(&data
->mutex
);
1315 kxcjk1013_set_mode(data
, STANDBY
);
1316 mutex_unlock(&data
->mutex
);
1321 #ifdef CONFIG_PM_SLEEP
1322 static int kxcjk1013_suspend(struct device
*dev
)
1324 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1325 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1328 mutex_lock(&data
->mutex
);
1329 ret
= kxcjk1013_set_mode(data
, STANDBY
);
1330 mutex_unlock(&data
->mutex
);
1335 static int kxcjk1013_resume(struct device
*dev
)
1337 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1338 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1341 mutex_lock(&data
->mutex
);
1342 ret
= kxcjk1013_set_mode(data
, OPERATION
);
1344 ret
= kxcjk1013_set_range(data
, data
->range
);
1345 mutex_unlock(&data
->mutex
);
1352 static int kxcjk1013_runtime_suspend(struct device
*dev
)
1354 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1355 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1358 ret
= kxcjk1013_set_mode(data
, STANDBY
);
1360 dev_err(&data
->client
->dev
, "powering off device failed\n");
1366 static int kxcjk1013_runtime_resume(struct device
*dev
)
1368 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1369 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1373 ret
= kxcjk1013_set_mode(data
, OPERATION
);
1377 sleep_val
= kxcjk1013_get_startup_times(data
);
1378 if (sleep_val
< 20000)
1379 usleep_range(sleep_val
, 20000);
1381 msleep_interruptible(sleep_val
/1000);
1387 static const struct dev_pm_ops kxcjk1013_pm_ops
= {
1388 SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend
, kxcjk1013_resume
)
1389 SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend
,
1390 kxcjk1013_runtime_resume
, NULL
)
1393 static const struct acpi_device_id kx_acpi_match
[] = {
1394 {"KXCJ1013", KXCJK1013
},
1395 {"KXCJ1008", KXCJ91008
},
1396 {"KXCJ9000", KXCJ91008
},
1397 {"KIOX000A", KXCJ91008
},
1398 {"KIOX010A", KXCJ91008
}, /* KXCJ91008 inside the display of a 2-in-1 */
1399 {"KXTJ1009", KXTJ21009
},
1400 {"SMO8500", KXCJ91008
},
1403 MODULE_DEVICE_TABLE(acpi
, kx_acpi_match
);
1405 static const struct i2c_device_id kxcjk1013_id
[] = {
1406 {"kxcjk1013", KXCJK1013
},
1407 {"kxcj91008", KXCJ91008
},
1408 {"kxtj21009", KXTJ21009
},
1409 {"SMO8500", KXCJ91008
},
1413 MODULE_DEVICE_TABLE(i2c
, kxcjk1013_id
);
1415 static struct i2c_driver kxcjk1013_driver
= {
1417 .name
= KXCJK1013_DRV_NAME
,
1418 .acpi_match_table
= ACPI_PTR(kx_acpi_match
),
1419 .pm
= &kxcjk1013_pm_ops
,
1421 .probe
= kxcjk1013_probe
,
1422 .remove
= kxcjk1013_remove
,
1423 .id_table
= kxcjk1013_id
,
1425 module_i2c_driver(kxcjk1013_driver
);
1427 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1428 MODULE_LICENSE("GPL v2");
1429 MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");