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 */
106 struct kxcjk1013_data {
107 struct i2c_client *client;
108 struct iio_trigger *dready_trig;
109 struct iio_trigger *motion_trig;
111 /* Ensure timestamp naturally aligned */
114 s64 timestamp __aligned(8);
120 bool active_high_intr;
121 bool dready_trigger_on;
123 bool motion_trigger_on;
125 enum kx_chipset chipset;
126 enum kx_acpi_type acpi_type;
129 enum kxcjk1013_mode {
134 enum kxcjk1013_range {
140 static const struct {
144 } samp_freq_table[] = { {0, 781000, 0x08}, {1, 563000, 0x09},
145 {3, 125000, 0x0A}, {6, 250000, 0x0B}, {12, 500000, 0},
146 {25, 0, 0x01}, {50, 0, 0x02}, {100, 0, 0x03},
147 {200, 0, 0x04}, {400, 0, 0x05}, {800, 0, 0x06},
150 /* Refer to section 4 of the specification */
151 static const struct {
154 } odr_start_up_times[KX_MAX_CHIPS][12] = {
202 static const struct {
206 } KXCJK1013_scale_table[] = { {9582, 0, 0},
210 static const struct {
214 } wake_odr_data_rate_table[] = { {0, 781000, 0x00},
227 static int kxcjk1013_set_mode(struct kxcjk1013_data *data,
228 enum kxcjk1013_mode mode)
232 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
234 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
239 ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1;
241 ret |= KXCJK1013_REG_CTRL1_BIT_PC1;
243 ret = i2c_smbus_write_byte_data(data->client,
244 KXCJK1013_REG_CTRL1, ret);
246 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
253 static int kxcjk1013_get_mode(struct kxcjk1013_data *data,
254 enum kxcjk1013_mode *mode)
258 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
260 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
264 if (ret & KXCJK1013_REG_CTRL1_BIT_PC1)
272 static int kxcjk1013_set_range(struct kxcjk1013_data *data, int range_index)
276 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
278 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
282 ret &= ~(KXCJK1013_REG_CTRL1_BIT_GSEL0 |
283 KXCJK1013_REG_CTRL1_BIT_GSEL1);
284 ret |= (KXCJK1013_scale_table[range_index].gsel_0 << 3);
285 ret |= (KXCJK1013_scale_table[range_index].gsel_1 << 4);
287 ret = i2c_smbus_write_byte_data(data->client,
291 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
295 data->range = range_index;
300 static int kxcjk1013_chip_init(struct kxcjk1013_data *data)
304 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I);
306 dev_err(&data->client->dev, "Error reading who_am_i\n");
310 dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret);
312 ret = kxcjk1013_set_mode(data, STANDBY);
316 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
318 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
322 /* Set 12 bit mode */
323 ret |= KXCJK1013_REG_CTRL1_BIT_RES;
325 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL1,
328 dev_err(&data->client->dev, "Error reading reg_ctrl\n");
332 /* Setting range to 4G */
333 ret = kxcjk1013_set_range(data, KXCJK1013_RANGE_4G);
337 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL);
339 dev_err(&data->client->dev, "Error reading reg_data_ctrl\n");
343 data->odr_bits = ret;
345 /* Set up INT polarity */
346 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
348 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
352 if (data->active_high_intr)
353 ret |= KXCJK1013_REG_INT_REG1_BIT_IEA;
355 ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEA;
357 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
360 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
364 ret = kxcjk1013_set_mode(data, OPERATION);
368 data->wake_thres = KXCJK1013_DEFAULT_WAKE_THRES;
374 static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data)
377 int idx = data->chipset;
379 for (i = 0; i < ARRAY_SIZE(odr_start_up_times[idx]); ++i) {
380 if (odr_start_up_times[idx][i].odr_bits == data->odr_bits)
381 return odr_start_up_times[idx][i].usec;
384 return KXCJK1013_MAX_STARTUP_TIME_US;
388 static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
394 ret = pm_runtime_get_sync(&data->client->dev);
396 pm_runtime_mark_last_busy(&data->client->dev);
397 ret = pm_runtime_put_autosuspend(&data->client->dev);
400 dev_err(&data->client->dev,
401 "Failed: kxcjk1013_set_power_state for %d\n", on);
403 pm_runtime_put_noidle(&data->client->dev);
411 static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data *data)
415 ret = i2c_smbus_write_byte_data(data->client,
416 KXCJK1013_REG_WAKE_TIMER,
419 dev_err(&data->client->dev,
420 "Error writing reg_wake_timer\n");
424 ret = i2c_smbus_write_byte_data(data->client,
425 KXCJK1013_REG_WAKE_THRES,
428 dev_err(&data->client->dev, "Error writing reg_wake_thres\n");
435 static int kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data *data,
439 enum kxcjk1013_mode store_mode;
441 ret = kxcjk1013_get_mode(data, &store_mode);
445 /* This is requirement by spec to change state to STANDBY */
446 ret = kxcjk1013_set_mode(data, STANDBY);
450 ret = kxcjk1013_chip_update_thresholds(data);
454 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
456 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
461 ret |= KXCJK1013_REG_INT_REG1_BIT_IEN;
463 ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEN;
465 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
468 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
472 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
474 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
479 ret |= KXCJK1013_REG_CTRL1_BIT_WUFE;
481 ret &= ~KXCJK1013_REG_CTRL1_BIT_WUFE;
483 ret = i2c_smbus_write_byte_data(data->client,
484 KXCJK1013_REG_CTRL1, ret);
486 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
490 if (store_mode == OPERATION) {
491 ret = kxcjk1013_set_mode(data, OPERATION);
499 static int kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data *data,
503 enum kxcjk1013_mode store_mode;
505 ret = kxcjk1013_get_mode(data, &store_mode);
509 /* This is requirement by spec to change state to STANDBY */
510 ret = kxcjk1013_set_mode(data, STANDBY);
514 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
516 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
521 ret |= KXCJK1013_REG_INT_REG1_BIT_IEN;
523 ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEN;
525 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
528 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
532 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
534 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
539 ret |= KXCJK1013_REG_CTRL1_BIT_DRDY;
541 ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY;
543 ret = i2c_smbus_write_byte_data(data->client,
544 KXCJK1013_REG_CTRL1, ret);
546 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
550 if (store_mode == OPERATION) {
551 ret = kxcjk1013_set_mode(data, OPERATION);
559 static int kxcjk1013_convert_freq_to_bit(int val, int val2)
563 for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) {
564 if (samp_freq_table[i].val == val &&
565 samp_freq_table[i].val2 == val2) {
566 return samp_freq_table[i].odr_bits;
573 static int kxcjk1013_convert_wake_odr_to_bit(int val, int val2)
577 for (i = 0; i < ARRAY_SIZE(wake_odr_data_rate_table); ++i) {
578 if (wake_odr_data_rate_table[i].val == val &&
579 wake_odr_data_rate_table[i].val2 == val2) {
580 return wake_odr_data_rate_table[i].odr_bits;
587 static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2)
591 enum kxcjk1013_mode store_mode;
593 ret = kxcjk1013_get_mode(data, &store_mode);
597 odr_bits = kxcjk1013_convert_freq_to_bit(val, val2);
601 /* To change ODR, the chip must be set to STANDBY as per spec */
602 ret = kxcjk1013_set_mode(data, STANDBY);
606 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_DATA_CTRL,
609 dev_err(&data->client->dev, "Error writing data_ctrl\n");
613 data->odr_bits = odr_bits;
615 odr_bits = kxcjk1013_convert_wake_odr_to_bit(val, val2);
619 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL2,
622 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
626 if (store_mode == OPERATION) {
627 ret = kxcjk1013_set_mode(data, OPERATION);
635 static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2)
639 for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) {
640 if (samp_freq_table[i].odr_bits == data->odr_bits) {
641 *val = samp_freq_table[i].val;
642 *val2 = samp_freq_table[i].val2;
643 return IIO_VAL_INT_PLUS_MICRO;
650 static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis)
652 u8 reg = KXCJK1013_REG_XOUT_L + axis * 2;
655 ret = i2c_smbus_read_word_data(data->client, reg);
657 dev_err(&data->client->dev,
658 "failed to read accel_%c registers\n", 'x' + axis);
665 static int kxcjk1013_set_scale(struct kxcjk1013_data *data, int val)
668 enum kxcjk1013_mode store_mode;
670 for (i = 0; i < ARRAY_SIZE(KXCJK1013_scale_table); ++i) {
671 if (KXCJK1013_scale_table[i].scale == val) {
672 ret = kxcjk1013_get_mode(data, &store_mode);
676 ret = kxcjk1013_set_mode(data, STANDBY);
680 ret = kxcjk1013_set_range(data, i);
684 if (store_mode == OPERATION) {
685 ret = kxcjk1013_set_mode(data, OPERATION);
697 static int kxcjk1013_read_raw(struct iio_dev *indio_dev,
698 struct iio_chan_spec const *chan, int *val,
699 int *val2, long mask)
701 struct kxcjk1013_data *data = iio_priv(indio_dev);
705 case IIO_CHAN_INFO_RAW:
706 mutex_lock(&data->mutex);
707 if (iio_buffer_enabled(indio_dev))
710 ret = kxcjk1013_set_power_state(data, true);
712 mutex_unlock(&data->mutex);
715 ret = kxcjk1013_get_acc_reg(data, chan->scan_index);
717 kxcjk1013_set_power_state(data, false);
718 mutex_unlock(&data->mutex);
721 *val = sign_extend32(ret >> 4, 11);
722 ret = kxcjk1013_set_power_state(data, false);
724 mutex_unlock(&data->mutex);
731 case IIO_CHAN_INFO_SCALE:
733 *val2 = KXCJK1013_scale_table[data->range].scale;
734 return IIO_VAL_INT_PLUS_MICRO;
736 case IIO_CHAN_INFO_SAMP_FREQ:
737 mutex_lock(&data->mutex);
738 ret = kxcjk1013_get_odr(data, val, val2);
739 mutex_unlock(&data->mutex);
747 static int kxcjk1013_write_raw(struct iio_dev *indio_dev,
748 struct iio_chan_spec const *chan, int val,
751 struct kxcjk1013_data *data = iio_priv(indio_dev);
755 case IIO_CHAN_INFO_SAMP_FREQ:
756 mutex_lock(&data->mutex);
757 ret = kxcjk1013_set_odr(data, val, val2);
758 mutex_unlock(&data->mutex);
760 case IIO_CHAN_INFO_SCALE:
764 mutex_lock(&data->mutex);
765 ret = kxcjk1013_set_scale(data, val2);
766 mutex_unlock(&data->mutex);
775 static int kxcjk1013_read_event(struct iio_dev *indio_dev,
776 const struct iio_chan_spec *chan,
777 enum iio_event_type type,
778 enum iio_event_direction dir,
779 enum iio_event_info info,
782 struct kxcjk1013_data *data = iio_priv(indio_dev);
786 case IIO_EV_INFO_VALUE:
787 *val = data->wake_thres;
789 case IIO_EV_INFO_PERIOD:
790 *val = data->wake_dur;
799 static int kxcjk1013_write_event(struct iio_dev *indio_dev,
800 const struct iio_chan_spec *chan,
801 enum iio_event_type type,
802 enum iio_event_direction dir,
803 enum iio_event_info info,
806 struct kxcjk1013_data *data = iio_priv(indio_dev);
808 if (data->ev_enable_state)
812 case IIO_EV_INFO_VALUE:
813 data->wake_thres = val;
815 case IIO_EV_INFO_PERIOD:
816 data->wake_dur = val;
825 static int kxcjk1013_read_event_config(struct iio_dev *indio_dev,
826 const struct iio_chan_spec *chan,
827 enum iio_event_type type,
828 enum iio_event_direction dir)
830 struct kxcjk1013_data *data = iio_priv(indio_dev);
832 return data->ev_enable_state;
835 static int kxcjk1013_write_event_config(struct iio_dev *indio_dev,
836 const struct iio_chan_spec *chan,
837 enum iio_event_type type,
838 enum iio_event_direction dir,
841 struct kxcjk1013_data *data = iio_priv(indio_dev);
844 if (state && data->ev_enable_state)
847 mutex_lock(&data->mutex);
849 if (!state && data->motion_trigger_on) {
850 data->ev_enable_state = 0;
851 mutex_unlock(&data->mutex);
856 * We will expect the enable and disable to do operation in
857 * in reverse order. This will happen here anyway as our
858 * resume operation uses sync mode runtime pm calls, the
859 * suspend operation will be delayed by autosuspend delay
860 * So the disable operation will still happen in reverse of
861 * enable operation. When runtime pm is disabled the mode
862 * is always on so sequence doesn't matter
864 ret = kxcjk1013_set_power_state(data, state);
866 mutex_unlock(&data->mutex);
870 ret = kxcjk1013_setup_any_motion_interrupt(data, state);
872 kxcjk1013_set_power_state(data, false);
873 data->ev_enable_state = 0;
874 mutex_unlock(&data->mutex);
878 data->ev_enable_state = state;
879 mutex_unlock(&data->mutex);
884 static int kxcjk1013_buffer_preenable(struct iio_dev *indio_dev)
886 struct kxcjk1013_data *data = iio_priv(indio_dev);
888 return kxcjk1013_set_power_state(data, true);
891 static int kxcjk1013_buffer_postdisable(struct iio_dev *indio_dev)
893 struct kxcjk1013_data *data = iio_priv(indio_dev);
895 return kxcjk1013_set_power_state(data, false);
898 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
899 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600");
901 static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019163 0.038326");
903 static struct attribute *kxcjk1013_attributes[] = {
904 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
905 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
909 static const struct attribute_group kxcjk1013_attrs_group = {
910 .attrs = kxcjk1013_attributes,
913 static const struct iio_event_spec kxcjk1013_event = {
914 .type = IIO_EV_TYPE_THRESH,
915 .dir = IIO_EV_DIR_EITHER,
916 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
917 BIT(IIO_EV_INFO_ENABLE) |
918 BIT(IIO_EV_INFO_PERIOD)
921 #define KXCJK1013_CHANNEL(_axis) { \
924 .channel2 = IIO_MOD_##_axis, \
925 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
926 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
927 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
928 .scan_index = AXIS_##_axis, \
934 .endianness = IIO_LE, \
936 .event_spec = &kxcjk1013_event, \
937 .num_event_specs = 1 \
940 static const struct iio_chan_spec kxcjk1013_channels[] = {
941 KXCJK1013_CHANNEL(X),
942 KXCJK1013_CHANNEL(Y),
943 KXCJK1013_CHANNEL(Z),
944 IIO_CHAN_SOFT_TIMESTAMP(3),
947 static const struct iio_buffer_setup_ops kxcjk1013_buffer_setup_ops = {
948 .preenable = kxcjk1013_buffer_preenable,
949 .postenable = iio_triggered_buffer_postenable,
950 .postdisable = kxcjk1013_buffer_postdisable,
951 .predisable = iio_triggered_buffer_predisable,
954 static const struct iio_info kxcjk1013_info = {
955 .attrs = &kxcjk1013_attrs_group,
956 .read_raw = kxcjk1013_read_raw,
957 .write_raw = kxcjk1013_write_raw,
958 .read_event_value = kxcjk1013_read_event,
959 .write_event_value = kxcjk1013_write_event,
960 .write_event_config = kxcjk1013_write_event_config,
961 .read_event_config = kxcjk1013_read_event_config,
962 .driver_module = THIS_MODULE,
965 static const unsigned long kxcjk1013_scan_masks[] = {0x7, 0};
967 static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p)
969 struct iio_poll_func *pf = p;
970 struct iio_dev *indio_dev = pf->indio_dev;
971 struct kxcjk1013_data *data = iio_priv(indio_dev);
974 mutex_lock(&data->mutex);
975 ret = i2c_smbus_read_i2c_block_data_or_emulated(data->client,
976 KXCJK1013_REG_XOUT_L,
978 (u8 *)data->scan.chans);
979 mutex_unlock(&data->mutex);
983 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
986 iio_trigger_notify_done(indio_dev->trig);
991 static int kxcjk1013_trig_try_reen(struct iio_trigger *trig)
993 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
994 struct kxcjk1013_data *data = iio_priv(indio_dev);
997 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL);
999 dev_err(&data->client->dev, "Error reading reg_int_rel\n");
1006 static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig,
1009 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1010 struct kxcjk1013_data *data = iio_priv(indio_dev);
1013 mutex_lock(&data->mutex);
1015 if (!state && data->ev_enable_state && data->motion_trigger_on) {
1016 data->motion_trigger_on = false;
1017 mutex_unlock(&data->mutex);
1021 ret = kxcjk1013_set_power_state(data, state);
1023 mutex_unlock(&data->mutex);
1026 if (data->motion_trig == trig)
1027 ret = kxcjk1013_setup_any_motion_interrupt(data, state);
1029 ret = kxcjk1013_setup_new_data_interrupt(data, state);
1031 kxcjk1013_set_power_state(data, false);
1032 mutex_unlock(&data->mutex);
1035 if (data->motion_trig == trig)
1036 data->motion_trigger_on = state;
1038 data->dready_trigger_on = state;
1040 mutex_unlock(&data->mutex);
1045 static const struct iio_trigger_ops kxcjk1013_trigger_ops = {
1046 .set_trigger_state = kxcjk1013_data_rdy_trigger_set_state,
1047 .try_reenable = kxcjk1013_trig_try_reen,
1048 .owner = THIS_MODULE,
1051 static irqreturn_t kxcjk1013_event_handler(int irq, void *private)
1053 struct iio_dev *indio_dev = private;
1054 struct kxcjk1013_data *data = iio_priv(indio_dev);
1057 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_SRC1);
1059 dev_err(&data->client->dev, "Error reading reg_int_src1\n");
1064 ret = i2c_smbus_read_byte_data(data->client,
1065 KXCJK1013_REG_INT_SRC2);
1067 dev_err(&data->client->dev,
1068 "Error reading reg_int_src2\n");
1072 if (ret & KXCJK1013_REG_INT_SRC2_BIT_XN)
1073 iio_push_event(indio_dev,
1074 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1078 IIO_EV_DIR_FALLING),
1080 if (ret & KXCJK1013_REG_INT_SRC2_BIT_XP)
1081 iio_push_event(indio_dev,
1082 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1090 if (ret & KXCJK1013_REG_INT_SRC2_BIT_YN)
1091 iio_push_event(indio_dev,
1092 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1096 IIO_EV_DIR_FALLING),
1098 if (ret & KXCJK1013_REG_INT_SRC2_BIT_YP)
1099 iio_push_event(indio_dev,
1100 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1107 if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZN)
1108 iio_push_event(indio_dev,
1109 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1113 IIO_EV_DIR_FALLING),
1115 if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZP)
1116 iio_push_event(indio_dev,
1117 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1126 if (data->dready_trigger_on)
1129 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL);
1131 dev_err(&data->client->dev, "Error reading reg_int_rel\n");
1136 static irqreturn_t kxcjk1013_data_rdy_trig_poll(int irq, void *private)
1138 struct iio_dev *indio_dev = private;
1139 struct kxcjk1013_data *data = iio_priv(indio_dev);
1141 data->timestamp = iio_get_time_ns(indio_dev);
1143 if (data->dready_trigger_on)
1144 iio_trigger_poll(data->dready_trig);
1145 else if (data->motion_trigger_on)
1146 iio_trigger_poll(data->motion_trig);
1148 if (data->ev_enable_state)
1149 return IRQ_WAKE_THREAD;
1154 static const char *kxcjk1013_match_acpi_device(struct device *dev,
1155 enum kx_chipset *chipset,
1156 enum kx_acpi_type *acpi_type)
1158 const struct acpi_device_id *id;
1160 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1164 if (strcmp(id->id, "SMO8500") == 0)
1165 *acpi_type = ACPI_SMO8500;
1167 *chipset = (enum kx_chipset)id->driver_data;
1169 return dev_name(dev);
1172 static int kxcjk1013_probe(struct i2c_client *client,
1173 const struct i2c_device_id *id)
1175 struct kxcjk1013_data *data;
1176 struct iio_dev *indio_dev;
1177 struct kxcjk_1013_platform_data *pdata;
1181 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1185 data = iio_priv(indio_dev);
1186 i2c_set_clientdata(client, indio_dev);
1187 data->client = client;
1189 pdata = dev_get_platdata(&client->dev);
1191 data->active_high_intr = pdata->active_high_intr;
1193 data->active_high_intr = true; /* default polarity */
1196 data->chipset = (enum kx_chipset)(id->driver_data);
1198 } else if (ACPI_HANDLE(&client->dev)) {
1199 name = kxcjk1013_match_acpi_device(&client->dev,
1205 ret = kxcjk1013_chip_init(data);
1209 mutex_init(&data->mutex);
1211 indio_dev->dev.parent = &client->dev;
1212 indio_dev->channels = kxcjk1013_channels;
1213 indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels);
1214 indio_dev->available_scan_masks = kxcjk1013_scan_masks;
1215 indio_dev->name = name;
1216 indio_dev->modes = INDIO_DIRECT_MODE;
1217 indio_dev->info = &kxcjk1013_info;
1219 if (client->irq > 0 && data->acpi_type != ACPI_SMO8500) {
1220 ret = devm_request_threaded_irq(&client->dev, client->irq,
1221 kxcjk1013_data_rdy_trig_poll,
1222 kxcjk1013_event_handler,
1223 IRQF_TRIGGER_RISING,
1229 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
1233 if (!data->dready_trig) {
1238 data->motion_trig = devm_iio_trigger_alloc(&client->dev,
1239 "%s-any-motion-dev%d",
1242 if (!data->motion_trig) {
1247 data->dready_trig->dev.parent = &client->dev;
1248 data->dready_trig->ops = &kxcjk1013_trigger_ops;
1249 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
1250 indio_dev->trig = data->dready_trig;
1251 iio_trigger_get(indio_dev->trig);
1252 ret = iio_trigger_register(data->dready_trig);
1256 data->motion_trig->dev.parent = &client->dev;
1257 data->motion_trig->ops = &kxcjk1013_trigger_ops;
1258 iio_trigger_set_drvdata(data->motion_trig, indio_dev);
1259 ret = iio_trigger_register(data->motion_trig);
1261 data->motion_trig = NULL;
1262 goto err_trigger_unregister;
1266 ret = iio_triggered_buffer_setup(indio_dev,
1267 &iio_pollfunc_store_time,
1268 kxcjk1013_trigger_handler,
1269 &kxcjk1013_buffer_setup_ops);
1271 dev_err(&client->dev, "iio triggered buffer setup failed\n");
1272 goto err_trigger_unregister;
1275 ret = pm_runtime_set_active(&client->dev);
1277 goto err_buffer_cleanup;
1279 pm_runtime_enable(&client->dev);
1280 pm_runtime_set_autosuspend_delay(&client->dev,
1281 KXCJK1013_SLEEP_DELAY_MS);
1282 pm_runtime_use_autosuspend(&client->dev);
1284 ret = iio_device_register(indio_dev);
1286 dev_err(&client->dev, "unable to register iio device\n");
1287 goto err_buffer_cleanup;
1293 if (data->dready_trig)
1294 iio_triggered_buffer_cleanup(indio_dev);
1295 err_trigger_unregister:
1296 if (data->dready_trig)
1297 iio_trigger_unregister(data->dready_trig);
1298 if (data->motion_trig)
1299 iio_trigger_unregister(data->motion_trig);
1301 kxcjk1013_set_mode(data, STANDBY);
1306 static int kxcjk1013_remove(struct i2c_client *client)
1308 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1309 struct kxcjk1013_data *data = iio_priv(indio_dev);
1311 iio_device_unregister(indio_dev);
1313 pm_runtime_disable(&client->dev);
1314 pm_runtime_set_suspended(&client->dev);
1315 pm_runtime_put_noidle(&client->dev);
1317 if (data->dready_trig) {
1318 iio_triggered_buffer_cleanup(indio_dev);
1319 iio_trigger_unregister(data->dready_trig);
1320 iio_trigger_unregister(data->motion_trig);
1323 mutex_lock(&data->mutex);
1324 kxcjk1013_set_mode(data, STANDBY);
1325 mutex_unlock(&data->mutex);
1330 #ifdef CONFIG_PM_SLEEP
1331 static int kxcjk1013_suspend(struct device *dev)
1333 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1334 struct kxcjk1013_data *data = iio_priv(indio_dev);
1337 mutex_lock(&data->mutex);
1338 ret = kxcjk1013_set_mode(data, STANDBY);
1339 mutex_unlock(&data->mutex);
1344 static int kxcjk1013_resume(struct device *dev)
1346 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1347 struct kxcjk1013_data *data = iio_priv(indio_dev);
1350 mutex_lock(&data->mutex);
1351 ret = kxcjk1013_set_mode(data, OPERATION);
1353 ret = kxcjk1013_set_range(data, data->range);
1354 mutex_unlock(&data->mutex);
1361 static int kxcjk1013_runtime_suspend(struct device *dev)
1363 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1364 struct kxcjk1013_data *data = iio_priv(indio_dev);
1367 ret = kxcjk1013_set_mode(data, STANDBY);
1369 dev_err(&data->client->dev, "powering off device failed\n");
1375 static int kxcjk1013_runtime_resume(struct device *dev)
1377 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1378 struct kxcjk1013_data *data = iio_priv(indio_dev);
1382 ret = kxcjk1013_set_mode(data, OPERATION);
1386 sleep_val = kxcjk1013_get_startup_times(data);
1387 if (sleep_val < 20000)
1388 usleep_range(sleep_val, 20000);
1390 msleep_interruptible(sleep_val/1000);
1396 static const struct dev_pm_ops kxcjk1013_pm_ops = {
1397 SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend, kxcjk1013_resume)
1398 SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend,
1399 kxcjk1013_runtime_resume, NULL)
1402 static const struct acpi_device_id kx_acpi_match[] = {
1403 {"KXCJ1013", KXCJK1013},
1404 {"KXCJ1008", KXCJ91008},
1405 {"KXCJ9000", KXCJ91008},
1406 {"KIOX000A", KXCJ91008},
1407 {"KIOX010A", KXCJ91008}, /* KXCJ91008 inside the display of a 2-in-1 */
1408 {"KXTJ1009", KXTJ21009},
1409 {"SMO8500", KXCJ91008},
1412 MODULE_DEVICE_TABLE(acpi, kx_acpi_match);
1414 static const struct i2c_device_id kxcjk1013_id[] = {
1415 {"kxcjk1013", KXCJK1013},
1416 {"kxcj91008", KXCJ91008},
1417 {"kxtj21009", KXTJ21009},
1418 {"SMO8500", KXCJ91008},
1422 MODULE_DEVICE_TABLE(i2c, kxcjk1013_id);
1424 static struct i2c_driver kxcjk1013_driver = {
1426 .name = KXCJK1013_DRV_NAME,
1427 .acpi_match_table = ACPI_PTR(kx_acpi_match),
1428 .pm = &kxcjk1013_pm_ops,
1430 .probe = kxcjk1013_probe,
1431 .remove = kxcjk1013_remove,
1432 .id_table = kxcjk1013_id,
1434 module_i2c_driver(kxcjk1013_driver);
1436 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1437 MODULE_LICENSE("GPL v2");
1438 MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");