xref: /linux/drivers/iio/accel/kxcjk-1013.c (revision 59bfeaba3b62089f98952fe689d44a093275b1a0)
11a4fbf6aSSrinivas Pandruvada /*
21a4fbf6aSSrinivas Pandruvada  * KXCJK-1013 3-axis accelerometer driver
31a4fbf6aSSrinivas Pandruvada  * Copyright (c) 2014, Intel Corporation.
41a4fbf6aSSrinivas Pandruvada  *
51a4fbf6aSSrinivas Pandruvada  * This program is free software; you can redistribute it and/or modify it
61a4fbf6aSSrinivas Pandruvada  * under the terms and conditions of the GNU General Public License,
71a4fbf6aSSrinivas Pandruvada  * version 2, as published by the Free Software Foundation.
81a4fbf6aSSrinivas Pandruvada  *
91a4fbf6aSSrinivas Pandruvada  * This program is distributed in the hope it will be useful, but WITHOUT
101a4fbf6aSSrinivas Pandruvada  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111a4fbf6aSSrinivas Pandruvada  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
121a4fbf6aSSrinivas Pandruvada  * more details.
131a4fbf6aSSrinivas Pandruvada  */
141a4fbf6aSSrinivas Pandruvada 
151a4fbf6aSSrinivas Pandruvada #include <linux/module.h>
161a4fbf6aSSrinivas Pandruvada #include <linux/i2c.h>
171a4fbf6aSSrinivas Pandruvada #include <linux/interrupt.h>
181a4fbf6aSSrinivas Pandruvada #include <linux/delay.h>
191a4fbf6aSSrinivas Pandruvada #include <linux/bitops.h>
201a4fbf6aSSrinivas Pandruvada #include <linux/slab.h>
211a4fbf6aSSrinivas Pandruvada #include <linux/string.h>
221a4fbf6aSSrinivas Pandruvada #include <linux/acpi.h>
231a4fbf6aSSrinivas Pandruvada #include <linux/gpio/consumer.h>
241a4fbf6aSSrinivas Pandruvada #include <linux/iio/iio.h>
251a4fbf6aSSrinivas Pandruvada #include <linux/iio/sysfs.h>
261a4fbf6aSSrinivas Pandruvada #include <linux/iio/buffer.h>
271a4fbf6aSSrinivas Pandruvada #include <linux/iio/trigger.h>
281a4fbf6aSSrinivas Pandruvada #include <linux/iio/trigger_consumer.h>
291a4fbf6aSSrinivas Pandruvada #include <linux/iio/triggered_buffer.h>
301a4fbf6aSSrinivas Pandruvada #include <linux/iio/accel/kxcjk_1013.h>
311a4fbf6aSSrinivas Pandruvada 
321a4fbf6aSSrinivas Pandruvada #define KXCJK1013_DRV_NAME "kxcjk1013"
331a4fbf6aSSrinivas Pandruvada #define KXCJK1013_IRQ_NAME "kxcjk1013_event"
341a4fbf6aSSrinivas Pandruvada 
351a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_XOUT_L		0x06
361a4fbf6aSSrinivas Pandruvada /*
371a4fbf6aSSrinivas Pandruvada  * From low byte X axis register, all the other addresses of Y and Z can be
381a4fbf6aSSrinivas Pandruvada  * obtained by just applying axis offset. The following axis defines are just
391a4fbf6aSSrinivas Pandruvada  * provide clarity, but not used.
401a4fbf6aSSrinivas Pandruvada  */
411a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_XOUT_H		0x07
421a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_YOUT_L		0x08
431a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_YOUT_H		0x09
441a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_ZOUT_L		0x0A
451a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_ZOUT_H		0x0B
461a4fbf6aSSrinivas Pandruvada 
471a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_DCST_RESP		0x0C
481a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_WHO_AM_I		0x0F
491a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC1		0x16
501a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC2		0x17
511a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_STATUS_REG	0x18
521a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_REL		0x1A
531a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1		0x1B
541a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL2		0x1D
551a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_CTRL1		0x1E
561a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_CTRL2		0x1F
571a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_DATA_CTRL		0x21
581a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_WAKE_TIMER	0x29
591a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_SELF_TEST		0x3A
601a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_WAKE_THRES	0x6A
611a4fbf6aSSrinivas Pandruvada 
621a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_PC1	BIT(7)
631a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_RES	BIT(6)
641a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_DRDY	BIT(5)
651a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_GSEL1	BIT(4)
661a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_GSEL0	BIT(3)
671a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_WUFE	BIT(1)
681a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_REG1_BIT_IEA	BIT(4)
691a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_REG1_BIT_IEN	BIT(5)
701a4fbf6aSSrinivas Pandruvada 
711a4fbf6aSSrinivas Pandruvada #define KXCJK1013_DATA_MASK_12_BIT	0x0FFF
721a4fbf6aSSrinivas Pandruvada #define KXCJK1013_MAX_STARTUP_TIME_US	100000
731a4fbf6aSSrinivas Pandruvada 
741a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data {
751a4fbf6aSSrinivas Pandruvada 	struct i2c_client *client;
761a4fbf6aSSrinivas Pandruvada 	struct iio_trigger *trig;
771a4fbf6aSSrinivas Pandruvada 	bool trig_mode;
781a4fbf6aSSrinivas Pandruvada 	struct mutex mutex;
791a4fbf6aSSrinivas Pandruvada 	s16 buffer[8];
801a4fbf6aSSrinivas Pandruvada 	int power_state;
811a4fbf6aSSrinivas Pandruvada 	u8 odr_bits;
821a4fbf6aSSrinivas Pandruvada 	bool active_high_intr;
831a4fbf6aSSrinivas Pandruvada };
841a4fbf6aSSrinivas Pandruvada 
851a4fbf6aSSrinivas Pandruvada enum kxcjk1013_axis {
861a4fbf6aSSrinivas Pandruvada 	AXIS_X,
871a4fbf6aSSrinivas Pandruvada 	AXIS_Y,
881a4fbf6aSSrinivas Pandruvada 	AXIS_Z,
891a4fbf6aSSrinivas Pandruvada };
901a4fbf6aSSrinivas Pandruvada 
911a4fbf6aSSrinivas Pandruvada enum kxcjk1013_mode {
921a4fbf6aSSrinivas Pandruvada 	STANDBY,
931a4fbf6aSSrinivas Pandruvada 	OPERATION,
941a4fbf6aSSrinivas Pandruvada };
951a4fbf6aSSrinivas Pandruvada 
961a4fbf6aSSrinivas Pandruvada static const struct {
971a4fbf6aSSrinivas Pandruvada 	int val;
981a4fbf6aSSrinivas Pandruvada 	int val2;
991a4fbf6aSSrinivas Pandruvada 	int odr_bits;
1001a4fbf6aSSrinivas Pandruvada } samp_freq_table[] = { {0, 781000, 0x08}, {1, 563000, 0x09},
101f0ca9749SSrinivas Pandruvada 			{3, 125000, 0x0A}, {6, 250000, 0x0B}, {12, 500000, 0},
1021a4fbf6aSSrinivas Pandruvada 			{25, 0, 0x01}, {50, 0, 0x02}, {100, 0, 0x03},
1031a4fbf6aSSrinivas Pandruvada 			{200, 0, 0x04}, {400, 0, 0x05}, {800, 0, 0x06},
1041a4fbf6aSSrinivas Pandruvada 			{1600, 0, 0x07} };
1051a4fbf6aSSrinivas Pandruvada 
1061a4fbf6aSSrinivas Pandruvada /* Refer to section 4 of the specification */
1071a4fbf6aSSrinivas Pandruvada static const struct {
1081a4fbf6aSSrinivas Pandruvada 	int odr_bits;
1091a4fbf6aSSrinivas Pandruvada 	int usec;
1101a4fbf6aSSrinivas Pandruvada } odr_start_up_times[] = { {0x08, 100000}, {0x09, 100000}, {0x0A, 100000},
1111a4fbf6aSSrinivas Pandruvada 			   {0x0B, 100000}, { 0, 80000}, {0x01, 41000},
1121a4fbf6aSSrinivas Pandruvada 			   {0x02, 21000}, {0x03, 11000}, {0x04, 6400},
1131a4fbf6aSSrinivas Pandruvada 			   {0x05, 3900}, {0x06, 2700}, {0x07, 2100} };
1141a4fbf6aSSrinivas Pandruvada 
1151a4fbf6aSSrinivas Pandruvada static int kxcjk1013_set_mode(struct kxcjk1013_data *data,
1161a4fbf6aSSrinivas Pandruvada 			      enum kxcjk1013_mode mode)
1171a4fbf6aSSrinivas Pandruvada {
1181a4fbf6aSSrinivas Pandruvada 	int ret;
1191a4fbf6aSSrinivas Pandruvada 
1201a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
1211a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
1221a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1231a4fbf6aSSrinivas Pandruvada 		return ret;
1241a4fbf6aSSrinivas Pandruvada 	}
1251a4fbf6aSSrinivas Pandruvada 
1261a4fbf6aSSrinivas Pandruvada 	if (mode == STANDBY)
1271a4fbf6aSSrinivas Pandruvada 		ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1;
1281a4fbf6aSSrinivas Pandruvada 	else
1291a4fbf6aSSrinivas Pandruvada 		ret |= KXCJK1013_REG_CTRL1_BIT_PC1;
1301a4fbf6aSSrinivas Pandruvada 
1311a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_write_byte_data(data->client,
1321a4fbf6aSSrinivas Pandruvada 					KXCJK1013_REG_CTRL1, ret);
1331a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
1341a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1351a4fbf6aSSrinivas Pandruvada 		return ret;
1361a4fbf6aSSrinivas Pandruvada 	}
1371a4fbf6aSSrinivas Pandruvada 
1381a4fbf6aSSrinivas Pandruvada 	return 0;
1391a4fbf6aSSrinivas Pandruvada }
1401a4fbf6aSSrinivas Pandruvada 
1411a4fbf6aSSrinivas Pandruvada static int kxcjk1013_chip_init(struct kxcjk1013_data *data)
1421a4fbf6aSSrinivas Pandruvada {
1431a4fbf6aSSrinivas Pandruvada 	int ret;
1441a4fbf6aSSrinivas Pandruvada 
1451a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I);
1461a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
1471a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading who_am_i\n");
1481a4fbf6aSSrinivas Pandruvada 		return ret;
1491a4fbf6aSSrinivas Pandruvada 	}
1501a4fbf6aSSrinivas Pandruvada 
1511a4fbf6aSSrinivas Pandruvada 	dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret);
1521a4fbf6aSSrinivas Pandruvada 
1531a4fbf6aSSrinivas Pandruvada 	ret = kxcjk1013_set_mode(data, STANDBY);
1541a4fbf6aSSrinivas Pandruvada 	if (ret < 0)
1551a4fbf6aSSrinivas Pandruvada 		return ret;
1561a4fbf6aSSrinivas Pandruvada 
1571a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
1581a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
1591a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1601a4fbf6aSSrinivas Pandruvada 		return ret;
1611a4fbf6aSSrinivas Pandruvada 	}
1621a4fbf6aSSrinivas Pandruvada 
1631a4fbf6aSSrinivas Pandruvada 	/* Setting range to 4G */
1641a4fbf6aSSrinivas Pandruvada 	ret |= KXCJK1013_REG_CTRL1_BIT_GSEL0;
1651a4fbf6aSSrinivas Pandruvada 	ret &= ~KXCJK1013_REG_CTRL1_BIT_GSEL1;
1661a4fbf6aSSrinivas Pandruvada 
1671a4fbf6aSSrinivas Pandruvada 	/* Set 12 bit mode */
1681a4fbf6aSSrinivas Pandruvada 	ret |= KXCJK1013_REG_CTRL1_BIT_RES;
1691a4fbf6aSSrinivas Pandruvada 
1701a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL1,
1711a4fbf6aSSrinivas Pandruvada 					ret);
1721a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
1731a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading reg_ctrl\n");
1741a4fbf6aSSrinivas Pandruvada 		return ret;
1751a4fbf6aSSrinivas Pandruvada 	}
1761a4fbf6aSSrinivas Pandruvada 
1771a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL);
1781a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
1791a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading reg_data_ctrl\n");
1801a4fbf6aSSrinivas Pandruvada 		return ret;
1811a4fbf6aSSrinivas Pandruvada 	}
1821a4fbf6aSSrinivas Pandruvada 
1831a4fbf6aSSrinivas Pandruvada 	data->odr_bits = ret;
1841a4fbf6aSSrinivas Pandruvada 
1851a4fbf6aSSrinivas Pandruvada 	/* Set up INT polarity */
1861a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
1871a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
1881a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
1891a4fbf6aSSrinivas Pandruvada 		return ret;
1901a4fbf6aSSrinivas Pandruvada 	}
1911a4fbf6aSSrinivas Pandruvada 
1921a4fbf6aSSrinivas Pandruvada 	if (data->active_high_intr)
1931a4fbf6aSSrinivas Pandruvada 		ret |= KXCJK1013_REG_INT_REG1_BIT_IEA;
1941a4fbf6aSSrinivas Pandruvada 	else
1951a4fbf6aSSrinivas Pandruvada 		ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEA;
1961a4fbf6aSSrinivas Pandruvada 
1971a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
1981a4fbf6aSSrinivas Pandruvada 					ret);
1991a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
2001a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
2011a4fbf6aSSrinivas Pandruvada 		return ret;
2021a4fbf6aSSrinivas Pandruvada 	}
2031a4fbf6aSSrinivas Pandruvada 
2041a4fbf6aSSrinivas Pandruvada 	return 0;
2051a4fbf6aSSrinivas Pandruvada }
2061a4fbf6aSSrinivas Pandruvada 
2071a4fbf6aSSrinivas Pandruvada static int kxcjk1013_chip_setup_interrupt(struct kxcjk1013_data *data,
2081a4fbf6aSSrinivas Pandruvada 					  bool status)
2091a4fbf6aSSrinivas Pandruvada {
2101a4fbf6aSSrinivas Pandruvada 	int ret;
2111a4fbf6aSSrinivas Pandruvada 
2121a4fbf6aSSrinivas Pandruvada 	/* This is requirement by spec to change state to STANDBY */
2131a4fbf6aSSrinivas Pandruvada 	ret = kxcjk1013_set_mode(data, STANDBY);
2141a4fbf6aSSrinivas Pandruvada 	if (ret < 0)
2151a4fbf6aSSrinivas Pandruvada 		return ret;
2161a4fbf6aSSrinivas Pandruvada 
2171a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
2181a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
2191a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
2201a4fbf6aSSrinivas Pandruvada 		return ret;
2211a4fbf6aSSrinivas Pandruvada 	}
2221a4fbf6aSSrinivas Pandruvada 
2231a4fbf6aSSrinivas Pandruvada 	if (status)
2241a4fbf6aSSrinivas Pandruvada 		ret |= KXCJK1013_REG_INT_REG1_BIT_IEN;
2251a4fbf6aSSrinivas Pandruvada 	else
2261a4fbf6aSSrinivas Pandruvada 		ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEN;
2271a4fbf6aSSrinivas Pandruvada 
2281a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
2291a4fbf6aSSrinivas Pandruvada 					ret);
2301a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
2311a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
2321a4fbf6aSSrinivas Pandruvada 		return ret;
2331a4fbf6aSSrinivas Pandruvada 	}
2341a4fbf6aSSrinivas Pandruvada 
2351a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
2361a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
2371a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
2381a4fbf6aSSrinivas Pandruvada 		return ret;
2391a4fbf6aSSrinivas Pandruvada 	}
2401a4fbf6aSSrinivas Pandruvada 
2411a4fbf6aSSrinivas Pandruvada 	if (status)
2421a4fbf6aSSrinivas Pandruvada 		ret |= KXCJK1013_REG_CTRL1_BIT_DRDY;
2431a4fbf6aSSrinivas Pandruvada 	else
2441a4fbf6aSSrinivas Pandruvada 		ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY;
2451a4fbf6aSSrinivas Pandruvada 
2461a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_write_byte_data(data->client,
2471a4fbf6aSSrinivas Pandruvada 					KXCJK1013_REG_CTRL1, ret);
2481a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
2491a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
2501a4fbf6aSSrinivas Pandruvada 		return ret;
2511a4fbf6aSSrinivas Pandruvada 	}
2521a4fbf6aSSrinivas Pandruvada 
2531a4fbf6aSSrinivas Pandruvada 	return ret;
2541a4fbf6aSSrinivas Pandruvada }
2551a4fbf6aSSrinivas Pandruvada 
2561a4fbf6aSSrinivas Pandruvada static int kxcjk1013_convert_freq_to_bit(int val, int val2)
2571a4fbf6aSSrinivas Pandruvada {
2581a4fbf6aSSrinivas Pandruvada 	int i;
2591a4fbf6aSSrinivas Pandruvada 
2601a4fbf6aSSrinivas Pandruvada 	for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) {
2611a4fbf6aSSrinivas Pandruvada 		if (samp_freq_table[i].val == val &&
2621a4fbf6aSSrinivas Pandruvada 			samp_freq_table[i].val2 == val2) {
2631a4fbf6aSSrinivas Pandruvada 			return samp_freq_table[i].odr_bits;
2641a4fbf6aSSrinivas Pandruvada 		}
2651a4fbf6aSSrinivas Pandruvada 	}
2661a4fbf6aSSrinivas Pandruvada 
2671a4fbf6aSSrinivas Pandruvada 	return -EINVAL;
2681a4fbf6aSSrinivas Pandruvada }
2691a4fbf6aSSrinivas Pandruvada 
2701a4fbf6aSSrinivas Pandruvada static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2)
2711a4fbf6aSSrinivas Pandruvada {
2721a4fbf6aSSrinivas Pandruvada 	int ret;
2731a4fbf6aSSrinivas Pandruvada 	int odr_bits;
2741a4fbf6aSSrinivas Pandruvada 
2751a4fbf6aSSrinivas Pandruvada 	odr_bits = kxcjk1013_convert_freq_to_bit(val, val2);
2761a4fbf6aSSrinivas Pandruvada 	if (odr_bits < 0)
2771a4fbf6aSSrinivas Pandruvada 		return odr_bits;
2781a4fbf6aSSrinivas Pandruvada 
2791a4fbf6aSSrinivas Pandruvada 	/* To change ODR, the chip must be set to STANDBY as per spec */
2801a4fbf6aSSrinivas Pandruvada 	ret = kxcjk1013_set_mode(data, STANDBY);
2811a4fbf6aSSrinivas Pandruvada 	if (ret < 0)
2821a4fbf6aSSrinivas Pandruvada 		return ret;
2831a4fbf6aSSrinivas Pandruvada 
2841a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_DATA_CTRL,
2851a4fbf6aSSrinivas Pandruvada 					odr_bits);
2861a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
2871a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error writing data_ctrl\n");
2881a4fbf6aSSrinivas Pandruvada 		return ret;
2891a4fbf6aSSrinivas Pandruvada 	}
2901a4fbf6aSSrinivas Pandruvada 
2911a4fbf6aSSrinivas Pandruvada 	data->odr_bits = odr_bits;
2921a4fbf6aSSrinivas Pandruvada 
2931a4fbf6aSSrinivas Pandruvada 	/* Check, if the ODR is changed after data enable */
2941a4fbf6aSSrinivas Pandruvada 	if (data->power_state) {
2951a4fbf6aSSrinivas Pandruvada 		/* Set the state back to operation */
2961a4fbf6aSSrinivas Pandruvada 		ret = kxcjk1013_set_mode(data, OPERATION);
2971a4fbf6aSSrinivas Pandruvada 		if (ret < 0)
2981a4fbf6aSSrinivas Pandruvada 			return ret;
2991a4fbf6aSSrinivas Pandruvada 	}
3001a4fbf6aSSrinivas Pandruvada 
3011a4fbf6aSSrinivas Pandruvada 	return 0;
3021a4fbf6aSSrinivas Pandruvada }
3031a4fbf6aSSrinivas Pandruvada 
3041a4fbf6aSSrinivas Pandruvada static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2)
3051a4fbf6aSSrinivas Pandruvada {
3061a4fbf6aSSrinivas Pandruvada 	int i;
3071a4fbf6aSSrinivas Pandruvada 
3081a4fbf6aSSrinivas Pandruvada 	for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) {
3091a4fbf6aSSrinivas Pandruvada 		if (samp_freq_table[i].odr_bits == data->odr_bits) {
3101a4fbf6aSSrinivas Pandruvada 			*val = samp_freq_table[i].val;
3111a4fbf6aSSrinivas Pandruvada 			*val2 = samp_freq_table[i].val2;
3121a4fbf6aSSrinivas Pandruvada 			return IIO_VAL_INT_PLUS_MICRO;
3131a4fbf6aSSrinivas Pandruvada 		}
3141a4fbf6aSSrinivas Pandruvada 	}
3151a4fbf6aSSrinivas Pandruvada 
3161a4fbf6aSSrinivas Pandruvada 	return -EINVAL;
3171a4fbf6aSSrinivas Pandruvada }
3181a4fbf6aSSrinivas Pandruvada 
3191a4fbf6aSSrinivas Pandruvada static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis)
3201a4fbf6aSSrinivas Pandruvada {
3211a4fbf6aSSrinivas Pandruvada 	u8 reg = KXCJK1013_REG_XOUT_L + axis * 2;
3221a4fbf6aSSrinivas Pandruvada 	int ret;
3231a4fbf6aSSrinivas Pandruvada 
3241a4fbf6aSSrinivas Pandruvada 	ret = i2c_smbus_read_word_data(data->client, reg);
3251a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
3261a4fbf6aSSrinivas Pandruvada 		dev_err(&data->client->dev,
3271a4fbf6aSSrinivas Pandruvada 			"failed to read accel_%c registers\n", 'x' + axis);
3281a4fbf6aSSrinivas Pandruvada 		return ret;
3291a4fbf6aSSrinivas Pandruvada 	}
3301a4fbf6aSSrinivas Pandruvada 
3311a4fbf6aSSrinivas Pandruvada 	return ret;
3321a4fbf6aSSrinivas Pandruvada }
3331a4fbf6aSSrinivas Pandruvada 
3341a4fbf6aSSrinivas Pandruvada static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data)
3351a4fbf6aSSrinivas Pandruvada {
3361a4fbf6aSSrinivas Pandruvada 	int i;
3371a4fbf6aSSrinivas Pandruvada 
3381a4fbf6aSSrinivas Pandruvada 	for (i = 0; i < ARRAY_SIZE(odr_start_up_times); ++i) {
3391a4fbf6aSSrinivas Pandruvada 		if (odr_start_up_times[i].odr_bits == data->odr_bits)
3401a4fbf6aSSrinivas Pandruvada 			return odr_start_up_times[i].usec;
3411a4fbf6aSSrinivas Pandruvada 	}
3421a4fbf6aSSrinivas Pandruvada 
3431a4fbf6aSSrinivas Pandruvada 	return KXCJK1013_MAX_STARTUP_TIME_US;
3441a4fbf6aSSrinivas Pandruvada }
3451a4fbf6aSSrinivas Pandruvada 
3461a4fbf6aSSrinivas Pandruvada static int kxcjk1013_read_raw(struct iio_dev *indio_dev,
3471a4fbf6aSSrinivas Pandruvada 			      struct iio_chan_spec const *chan, int *val,
3481a4fbf6aSSrinivas Pandruvada 			      int *val2, long mask)
3491a4fbf6aSSrinivas Pandruvada {
3501a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
3511a4fbf6aSSrinivas Pandruvada 	int ret;
3521a4fbf6aSSrinivas Pandruvada 
3531a4fbf6aSSrinivas Pandruvada 	switch (mask) {
3541a4fbf6aSSrinivas Pandruvada 	case IIO_CHAN_INFO_RAW:
3551a4fbf6aSSrinivas Pandruvada 		mutex_lock(&data->mutex);
3561a4fbf6aSSrinivas Pandruvada 		if (iio_buffer_enabled(indio_dev))
3571a4fbf6aSSrinivas Pandruvada 			ret = -EBUSY;
3581a4fbf6aSSrinivas Pandruvada 		else {
3591a4fbf6aSSrinivas Pandruvada 			int sleep_val;
3601a4fbf6aSSrinivas Pandruvada 
3611a4fbf6aSSrinivas Pandruvada 			ret = kxcjk1013_set_mode(data, OPERATION);
36288f6da77SSrinivas Pandruvada 			if (ret < 0) {
36388f6da77SSrinivas Pandruvada 				mutex_unlock(&data->mutex);
3641a4fbf6aSSrinivas Pandruvada 				return ret;
36588f6da77SSrinivas Pandruvada 			}
3661a4fbf6aSSrinivas Pandruvada 			++data->power_state;
3671a4fbf6aSSrinivas Pandruvada 			sleep_val = kxcjk1013_get_startup_times(data);
3681a4fbf6aSSrinivas Pandruvada 			if (sleep_val < 20000)
3691a4fbf6aSSrinivas Pandruvada 				usleep_range(sleep_val, 20000);
3701a4fbf6aSSrinivas Pandruvada 			else
3711a4fbf6aSSrinivas Pandruvada 				msleep_interruptible(sleep_val/1000);
3721a4fbf6aSSrinivas Pandruvada 			ret = kxcjk1013_get_acc_reg(data, chan->scan_index);
3731a4fbf6aSSrinivas Pandruvada 			if (--data->power_state == 0)
3741a4fbf6aSSrinivas Pandruvada 				kxcjk1013_set_mode(data, STANDBY);
3751a4fbf6aSSrinivas Pandruvada 		}
3761a4fbf6aSSrinivas Pandruvada 		mutex_unlock(&data->mutex);
3771a4fbf6aSSrinivas Pandruvada 
3781a4fbf6aSSrinivas Pandruvada 		if (ret < 0)
3791a4fbf6aSSrinivas Pandruvada 			return ret;
3801a4fbf6aSSrinivas Pandruvada 
3811a4fbf6aSSrinivas Pandruvada 		*val = sign_extend32(ret >> 4, 11);
3821a4fbf6aSSrinivas Pandruvada 		return IIO_VAL_INT;
3831a4fbf6aSSrinivas Pandruvada 
3841a4fbf6aSSrinivas Pandruvada 	case IIO_CHAN_INFO_SCALE:
3851a4fbf6aSSrinivas Pandruvada 		*val = 0;
3861a4fbf6aSSrinivas Pandruvada 		*val2 = 19163; /* range +-4g (4/2047*9.806650) */
3871a4fbf6aSSrinivas Pandruvada 		return IIO_VAL_INT_PLUS_MICRO;
3881a4fbf6aSSrinivas Pandruvada 
3891a4fbf6aSSrinivas Pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
3901a4fbf6aSSrinivas Pandruvada 		mutex_lock(&data->mutex);
3911a4fbf6aSSrinivas Pandruvada 		ret = kxcjk1013_get_odr(data, val, val2);
3921a4fbf6aSSrinivas Pandruvada 		mutex_unlock(&data->mutex);
3931a4fbf6aSSrinivas Pandruvada 		return ret;
3941a4fbf6aSSrinivas Pandruvada 
3951a4fbf6aSSrinivas Pandruvada 	default:
3961a4fbf6aSSrinivas Pandruvada 		return -EINVAL;
3971a4fbf6aSSrinivas Pandruvada 	}
3981a4fbf6aSSrinivas Pandruvada }
3991a4fbf6aSSrinivas Pandruvada 
4001a4fbf6aSSrinivas Pandruvada static int kxcjk1013_write_raw(struct iio_dev *indio_dev,
4011a4fbf6aSSrinivas Pandruvada 			       struct iio_chan_spec const *chan, int val,
4021a4fbf6aSSrinivas Pandruvada 			       int val2, long mask)
4031a4fbf6aSSrinivas Pandruvada {
4041a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
4051a4fbf6aSSrinivas Pandruvada 	int ret;
4061a4fbf6aSSrinivas Pandruvada 
4071a4fbf6aSSrinivas Pandruvada 	switch (mask) {
4081a4fbf6aSSrinivas Pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
4091a4fbf6aSSrinivas Pandruvada 		mutex_lock(&data->mutex);
4101a4fbf6aSSrinivas Pandruvada 		ret = kxcjk1013_set_odr(data, val, val2);
4111a4fbf6aSSrinivas Pandruvada 		mutex_unlock(&data->mutex);
4121a4fbf6aSSrinivas Pandruvada 		break;
4131a4fbf6aSSrinivas Pandruvada 	default:
4141a4fbf6aSSrinivas Pandruvada 		ret = -EINVAL;
4151a4fbf6aSSrinivas Pandruvada 	}
4161a4fbf6aSSrinivas Pandruvada 
4171a4fbf6aSSrinivas Pandruvada 	return ret;
4181a4fbf6aSSrinivas Pandruvada }
4191a4fbf6aSSrinivas Pandruvada 
4201a4fbf6aSSrinivas Pandruvada static int kxcjk1013_validate_trigger(struct iio_dev *indio_dev,
4211a4fbf6aSSrinivas Pandruvada 				      struct iio_trigger *trig)
4221a4fbf6aSSrinivas Pandruvada {
4231a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
4241a4fbf6aSSrinivas Pandruvada 
4251a4fbf6aSSrinivas Pandruvada 	if (data->trig != trig)
4261a4fbf6aSSrinivas Pandruvada 		return -EINVAL;
4271a4fbf6aSSrinivas Pandruvada 
4281a4fbf6aSSrinivas Pandruvada 	return 0;
4291a4fbf6aSSrinivas Pandruvada }
4301a4fbf6aSSrinivas Pandruvada 
4311a4fbf6aSSrinivas Pandruvada static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
4321a4fbf6aSSrinivas Pandruvada 	"0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600");
4331a4fbf6aSSrinivas Pandruvada 
4341a4fbf6aSSrinivas Pandruvada static struct attribute *kxcjk1013_attributes[] = {
4351a4fbf6aSSrinivas Pandruvada 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
4361a4fbf6aSSrinivas Pandruvada 	NULL,
4371a4fbf6aSSrinivas Pandruvada };
4381a4fbf6aSSrinivas Pandruvada 
4391a4fbf6aSSrinivas Pandruvada static const struct attribute_group kxcjk1013_attrs_group = {
4401a4fbf6aSSrinivas Pandruvada 	.attrs = kxcjk1013_attributes,
4411a4fbf6aSSrinivas Pandruvada };
4421a4fbf6aSSrinivas Pandruvada 
4431a4fbf6aSSrinivas Pandruvada #define KXCJK1013_CHANNEL(_axis) {					\
4441a4fbf6aSSrinivas Pandruvada 	.type = IIO_ACCEL,						\
4451a4fbf6aSSrinivas Pandruvada 	.modified = 1,							\
4461a4fbf6aSSrinivas Pandruvada 	.channel2 = IIO_MOD_##_axis,					\
4471a4fbf6aSSrinivas Pandruvada 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
4481a4fbf6aSSrinivas Pandruvada 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
4491a4fbf6aSSrinivas Pandruvada 				BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
4501a4fbf6aSSrinivas Pandruvada 	.scan_index = AXIS_##_axis,					\
4511a4fbf6aSSrinivas Pandruvada 	.scan_type = {							\
4521a4fbf6aSSrinivas Pandruvada 		.sign = 's',						\
4531a4fbf6aSSrinivas Pandruvada 		.realbits = 12,						\
4541a4fbf6aSSrinivas Pandruvada 		.storagebits = 16,					\
4551a4fbf6aSSrinivas Pandruvada 		.shift = 4,						\
4561a4fbf6aSSrinivas Pandruvada 		.endianness = IIO_LE,					\
4571a4fbf6aSSrinivas Pandruvada 	},								\
4581a4fbf6aSSrinivas Pandruvada }
4591a4fbf6aSSrinivas Pandruvada 
4601a4fbf6aSSrinivas Pandruvada static const struct iio_chan_spec kxcjk1013_channels[] = {
4611a4fbf6aSSrinivas Pandruvada 	KXCJK1013_CHANNEL(X),
4621a4fbf6aSSrinivas Pandruvada 	KXCJK1013_CHANNEL(Y),
4631a4fbf6aSSrinivas Pandruvada 	KXCJK1013_CHANNEL(Z),
4641a4fbf6aSSrinivas Pandruvada 	IIO_CHAN_SOFT_TIMESTAMP(3),
4651a4fbf6aSSrinivas Pandruvada };
4661a4fbf6aSSrinivas Pandruvada 
4671a4fbf6aSSrinivas Pandruvada static const struct iio_info kxcjk1013_info = {
4681a4fbf6aSSrinivas Pandruvada 	.attrs			= &kxcjk1013_attrs_group,
4691a4fbf6aSSrinivas Pandruvada 	.read_raw		= kxcjk1013_read_raw,
4701a4fbf6aSSrinivas Pandruvada 	.write_raw		= kxcjk1013_write_raw,
4711a4fbf6aSSrinivas Pandruvada 	.validate_trigger	= kxcjk1013_validate_trigger,
4721a4fbf6aSSrinivas Pandruvada 	.driver_module		= THIS_MODULE,
4731a4fbf6aSSrinivas Pandruvada };
4741a4fbf6aSSrinivas Pandruvada 
4751a4fbf6aSSrinivas Pandruvada static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p)
4761a4fbf6aSSrinivas Pandruvada {
4771a4fbf6aSSrinivas Pandruvada 	struct iio_poll_func *pf = p;
4781a4fbf6aSSrinivas Pandruvada 	struct iio_dev *indio_dev = pf->indio_dev;
4791a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
4801a4fbf6aSSrinivas Pandruvada 	int bit, ret, i = 0;
4811a4fbf6aSSrinivas Pandruvada 
4821a4fbf6aSSrinivas Pandruvada 	mutex_lock(&data->mutex);
4831a4fbf6aSSrinivas Pandruvada 
4841a4fbf6aSSrinivas Pandruvada 	for_each_set_bit(bit, indio_dev->buffer->scan_mask,
4851a4fbf6aSSrinivas Pandruvada 			 indio_dev->masklength) {
4861a4fbf6aSSrinivas Pandruvada 		ret = kxcjk1013_get_acc_reg(data, bit);
4871a4fbf6aSSrinivas Pandruvada 		if (ret < 0) {
4881a4fbf6aSSrinivas Pandruvada 			mutex_unlock(&data->mutex);
4891a4fbf6aSSrinivas Pandruvada 			goto err;
4901a4fbf6aSSrinivas Pandruvada 		}
4911a4fbf6aSSrinivas Pandruvada 		data->buffer[i++] = ret;
4921a4fbf6aSSrinivas Pandruvada 	}
4931a4fbf6aSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
4941a4fbf6aSSrinivas Pandruvada 
4951a4fbf6aSSrinivas Pandruvada 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
4961a4fbf6aSSrinivas Pandruvada 					   pf->timestamp);
4971a4fbf6aSSrinivas Pandruvada err:
4981a4fbf6aSSrinivas Pandruvada 	iio_trigger_notify_done(indio_dev->trig);
4991a4fbf6aSSrinivas Pandruvada 
5001a4fbf6aSSrinivas Pandruvada 	return IRQ_HANDLED;
5011a4fbf6aSSrinivas Pandruvada }
5021a4fbf6aSSrinivas Pandruvada 
503*59bfeabaSSrinivas Pandruvada static int kxcjk1013_trig_try_reen(struct iio_trigger *trig)
504*59bfeabaSSrinivas Pandruvada {
505*59bfeabaSSrinivas Pandruvada 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
506*59bfeabaSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
507*59bfeabaSSrinivas Pandruvada 	int ret;
508*59bfeabaSSrinivas Pandruvada 
509*59bfeabaSSrinivas Pandruvada 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL);
510*59bfeabaSSrinivas Pandruvada 	if (ret < 0) {
511*59bfeabaSSrinivas Pandruvada 		dev_err(&data->client->dev, "Error reading reg_int_rel\n");
512*59bfeabaSSrinivas Pandruvada 		return ret;
513*59bfeabaSSrinivas Pandruvada 	}
514*59bfeabaSSrinivas Pandruvada 
515*59bfeabaSSrinivas Pandruvada 	return 0;
516*59bfeabaSSrinivas Pandruvada }
517*59bfeabaSSrinivas Pandruvada 
5181a4fbf6aSSrinivas Pandruvada static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig,
5191a4fbf6aSSrinivas Pandruvada 						bool state)
5201a4fbf6aSSrinivas Pandruvada {
5211a4fbf6aSSrinivas Pandruvada 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
5221a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
5231a4fbf6aSSrinivas Pandruvada 
5241a4fbf6aSSrinivas Pandruvada 	mutex_lock(&data->mutex);
5251a4fbf6aSSrinivas Pandruvada 	if (state) {
5261a4fbf6aSSrinivas Pandruvada 		kxcjk1013_chip_setup_interrupt(data, true);
5271a4fbf6aSSrinivas Pandruvada 		kxcjk1013_set_mode(data, OPERATION);
5281a4fbf6aSSrinivas Pandruvada 		++data->power_state;
5291a4fbf6aSSrinivas Pandruvada 	} else {
53088f6da77SSrinivas Pandruvada 		if (--data->power_state) {
53188f6da77SSrinivas Pandruvada 			mutex_unlock(&data->mutex);
5321a4fbf6aSSrinivas Pandruvada 			return 0;
53388f6da77SSrinivas Pandruvada 		}
5341a4fbf6aSSrinivas Pandruvada 		kxcjk1013_chip_setup_interrupt(data, false);
5351a4fbf6aSSrinivas Pandruvada 		kxcjk1013_set_mode(data, STANDBY);
5361a4fbf6aSSrinivas Pandruvada 	}
5371a4fbf6aSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5381a4fbf6aSSrinivas Pandruvada 
5391a4fbf6aSSrinivas Pandruvada 	return 0;
5401a4fbf6aSSrinivas Pandruvada }
5411a4fbf6aSSrinivas Pandruvada 
5421a4fbf6aSSrinivas Pandruvada static const struct iio_trigger_ops kxcjk1013_trigger_ops = {
5431a4fbf6aSSrinivas Pandruvada 	.set_trigger_state = kxcjk1013_data_rdy_trigger_set_state,
544*59bfeabaSSrinivas Pandruvada 	.try_reenable = kxcjk1013_trig_try_reen,
5451a4fbf6aSSrinivas Pandruvada 	.owner = THIS_MODULE,
5461a4fbf6aSSrinivas Pandruvada };
5471a4fbf6aSSrinivas Pandruvada 
5481a4fbf6aSSrinivas Pandruvada static int kxcjk1013_acpi_gpio_probe(struct i2c_client *client,
5491a4fbf6aSSrinivas Pandruvada 				     struct kxcjk1013_data *data)
5501a4fbf6aSSrinivas Pandruvada {
5511a4fbf6aSSrinivas Pandruvada 	const struct acpi_device_id *id;
5521a4fbf6aSSrinivas Pandruvada 	struct device *dev;
5531a4fbf6aSSrinivas Pandruvada 	struct gpio_desc *gpio;
5541a4fbf6aSSrinivas Pandruvada 	int ret;
5551a4fbf6aSSrinivas Pandruvada 
5561a4fbf6aSSrinivas Pandruvada 	if (!client)
5571a4fbf6aSSrinivas Pandruvada 		return -EINVAL;
5581a4fbf6aSSrinivas Pandruvada 
5591a4fbf6aSSrinivas Pandruvada 	dev = &client->dev;
5601a4fbf6aSSrinivas Pandruvada 	if (!ACPI_HANDLE(dev))
5611a4fbf6aSSrinivas Pandruvada 		return -ENODEV;
5621a4fbf6aSSrinivas Pandruvada 
5631a4fbf6aSSrinivas Pandruvada 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
5641a4fbf6aSSrinivas Pandruvada 	if (!id)
5651a4fbf6aSSrinivas Pandruvada 		return -ENODEV;
5661a4fbf6aSSrinivas Pandruvada 
5671a4fbf6aSSrinivas Pandruvada 	/* data ready gpio interrupt pin */
5681a4fbf6aSSrinivas Pandruvada 	gpio = devm_gpiod_get_index(dev, "kxcjk1013_int", 0);
5691a4fbf6aSSrinivas Pandruvada 	if (IS_ERR(gpio)) {
5701a4fbf6aSSrinivas Pandruvada 		dev_err(dev, "acpi gpio get index failed\n");
5711a4fbf6aSSrinivas Pandruvada 		return PTR_ERR(gpio);
5721a4fbf6aSSrinivas Pandruvada 	}
5731a4fbf6aSSrinivas Pandruvada 
5741a4fbf6aSSrinivas Pandruvada 	ret = gpiod_direction_input(gpio);
5751a4fbf6aSSrinivas Pandruvada 	if (ret)
5761a4fbf6aSSrinivas Pandruvada 		return ret;
5771a4fbf6aSSrinivas Pandruvada 
5781a4fbf6aSSrinivas Pandruvada 	ret = gpiod_to_irq(gpio);
5791a4fbf6aSSrinivas Pandruvada 
5801a4fbf6aSSrinivas Pandruvada 	dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
5811a4fbf6aSSrinivas Pandruvada 
5821a4fbf6aSSrinivas Pandruvada 	return ret;
5831a4fbf6aSSrinivas Pandruvada }
5841a4fbf6aSSrinivas Pandruvada 
5851a4fbf6aSSrinivas Pandruvada static int kxcjk1013_probe(struct i2c_client *client,
5861a4fbf6aSSrinivas Pandruvada 			   const struct i2c_device_id *id)
5871a4fbf6aSSrinivas Pandruvada {
5881a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data;
5891a4fbf6aSSrinivas Pandruvada 	struct iio_dev *indio_dev;
5901a4fbf6aSSrinivas Pandruvada 	struct iio_trigger *trig = NULL;
5911a4fbf6aSSrinivas Pandruvada 	struct kxcjk_1013_platform_data *pdata;
5921a4fbf6aSSrinivas Pandruvada 	int ret;
5931a4fbf6aSSrinivas Pandruvada 
5941a4fbf6aSSrinivas Pandruvada 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
5951a4fbf6aSSrinivas Pandruvada 	if (!indio_dev)
5961a4fbf6aSSrinivas Pandruvada 		return -ENOMEM;
5971a4fbf6aSSrinivas Pandruvada 
5981a4fbf6aSSrinivas Pandruvada 	data = iio_priv(indio_dev);
5991a4fbf6aSSrinivas Pandruvada 	i2c_set_clientdata(client, indio_dev);
6001a4fbf6aSSrinivas Pandruvada 	data->client = client;
6011a4fbf6aSSrinivas Pandruvada 
6021a4fbf6aSSrinivas Pandruvada 	pdata = dev_get_platdata(&client->dev);
6031a4fbf6aSSrinivas Pandruvada 	if (pdata)
6041a4fbf6aSSrinivas Pandruvada 		data->active_high_intr = pdata->active_high_intr;
6051a4fbf6aSSrinivas Pandruvada 	else
6061a4fbf6aSSrinivas Pandruvada 		data->active_high_intr = true; /* default polarity */
6071a4fbf6aSSrinivas Pandruvada 
6081a4fbf6aSSrinivas Pandruvada 	ret = kxcjk1013_chip_init(data);
6091a4fbf6aSSrinivas Pandruvada 	if (ret < 0)
6101a4fbf6aSSrinivas Pandruvada 		return ret;
6111a4fbf6aSSrinivas Pandruvada 
6121a4fbf6aSSrinivas Pandruvada 	mutex_init(&data->mutex);
6131a4fbf6aSSrinivas Pandruvada 
6141a4fbf6aSSrinivas Pandruvada 	indio_dev->dev.parent = &client->dev;
6151a4fbf6aSSrinivas Pandruvada 	indio_dev->channels = kxcjk1013_channels;
6161a4fbf6aSSrinivas Pandruvada 	indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels);
6171a4fbf6aSSrinivas Pandruvada 	indio_dev->name = KXCJK1013_DRV_NAME;
6181a4fbf6aSSrinivas Pandruvada 	indio_dev->modes = INDIO_DIRECT_MODE;
6191a4fbf6aSSrinivas Pandruvada 	indio_dev->info = &kxcjk1013_info;
6201a4fbf6aSSrinivas Pandruvada 
6211a4fbf6aSSrinivas Pandruvada 	if (client->irq < 0)
6221a4fbf6aSSrinivas Pandruvada 		client->irq = kxcjk1013_acpi_gpio_probe(client, data);
6231a4fbf6aSSrinivas Pandruvada 
6241a4fbf6aSSrinivas Pandruvada 	if (client->irq >= 0) {
6251a4fbf6aSSrinivas Pandruvada 		trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
6261a4fbf6aSSrinivas Pandruvada 					 indio_dev->id);
6271a4fbf6aSSrinivas Pandruvada 		if (!trig)
6281a4fbf6aSSrinivas Pandruvada 			return -ENOMEM;
6291a4fbf6aSSrinivas Pandruvada 
6301a4fbf6aSSrinivas Pandruvada 		data->trig_mode = true;
6311a4fbf6aSSrinivas Pandruvada 
6321a4fbf6aSSrinivas Pandruvada 		ret = devm_request_irq(&client->dev, client->irq,
6331a4fbf6aSSrinivas Pandruvada 					iio_trigger_generic_data_rdy_poll,
6341a4fbf6aSSrinivas Pandruvada 					IRQF_TRIGGER_RISING,
6351a4fbf6aSSrinivas Pandruvada 					KXCJK1013_IRQ_NAME,
6361a4fbf6aSSrinivas Pandruvada 					trig);
6371a4fbf6aSSrinivas Pandruvada 		if (ret) {
6381a4fbf6aSSrinivas Pandruvada 			dev_err(&client->dev, "unable to request IRQ\n");
6391a4fbf6aSSrinivas Pandruvada 			goto err_trigger_free;
6401a4fbf6aSSrinivas Pandruvada 		}
6411a4fbf6aSSrinivas Pandruvada 
6421a4fbf6aSSrinivas Pandruvada 		trig->dev.parent = &client->dev;
6431a4fbf6aSSrinivas Pandruvada 		trig->ops = &kxcjk1013_trigger_ops;
6441a4fbf6aSSrinivas Pandruvada 		iio_trigger_set_drvdata(trig, indio_dev);
6451a4fbf6aSSrinivas Pandruvada 		data->trig = trig;
6461a4fbf6aSSrinivas Pandruvada 		indio_dev->trig = trig;
6471a4fbf6aSSrinivas Pandruvada 
6481a4fbf6aSSrinivas Pandruvada 		ret = iio_trigger_register(trig);
6491a4fbf6aSSrinivas Pandruvada 		if (ret)
6501a4fbf6aSSrinivas Pandruvada 			goto err_trigger_free;
6511a4fbf6aSSrinivas Pandruvada 
6521a4fbf6aSSrinivas Pandruvada 		ret = iio_triggered_buffer_setup(indio_dev,
6531a4fbf6aSSrinivas Pandruvada 						&iio_pollfunc_store_time,
6541a4fbf6aSSrinivas Pandruvada 						kxcjk1013_trigger_handler,
6551a4fbf6aSSrinivas Pandruvada 						NULL);
6561a4fbf6aSSrinivas Pandruvada 		if (ret < 0) {
6571a4fbf6aSSrinivas Pandruvada 			dev_err(&client->dev,
6581a4fbf6aSSrinivas Pandruvada 					"iio triggered buffer setup failed\n");
6591a4fbf6aSSrinivas Pandruvada 			goto err_trigger_unregister;
6601a4fbf6aSSrinivas Pandruvada 		}
6611a4fbf6aSSrinivas Pandruvada 	}
6621a4fbf6aSSrinivas Pandruvada 
6631a4fbf6aSSrinivas Pandruvada 	ret = devm_iio_device_register(&client->dev, indio_dev);
6641a4fbf6aSSrinivas Pandruvada 	if (ret < 0) {
6651a4fbf6aSSrinivas Pandruvada 		dev_err(&client->dev, "unable to register iio device\n");
6661a4fbf6aSSrinivas Pandruvada 		goto err_buffer_cleanup;
6671a4fbf6aSSrinivas Pandruvada 	}
6681a4fbf6aSSrinivas Pandruvada 
6691a4fbf6aSSrinivas Pandruvada 	return 0;
6701a4fbf6aSSrinivas Pandruvada 
6711a4fbf6aSSrinivas Pandruvada err_buffer_cleanup:
6721a4fbf6aSSrinivas Pandruvada 	if (data->trig_mode)
6731a4fbf6aSSrinivas Pandruvada 		iio_triggered_buffer_cleanup(indio_dev);
6741a4fbf6aSSrinivas Pandruvada err_trigger_unregister:
6751a4fbf6aSSrinivas Pandruvada 	if (data->trig_mode)
6761a4fbf6aSSrinivas Pandruvada 		iio_trigger_unregister(trig);
6771a4fbf6aSSrinivas Pandruvada err_trigger_free:
6781a4fbf6aSSrinivas Pandruvada 	if (data->trig_mode)
6791a4fbf6aSSrinivas Pandruvada 		iio_trigger_free(trig);
6801a4fbf6aSSrinivas Pandruvada 
6811a4fbf6aSSrinivas Pandruvada 	return ret;
6821a4fbf6aSSrinivas Pandruvada }
6831a4fbf6aSSrinivas Pandruvada 
6841a4fbf6aSSrinivas Pandruvada static int kxcjk1013_remove(struct i2c_client *client)
6851a4fbf6aSSrinivas Pandruvada {
6861a4fbf6aSSrinivas Pandruvada 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
6871a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
6881a4fbf6aSSrinivas Pandruvada 
6891a4fbf6aSSrinivas Pandruvada 	if (data->trig_mode) {
6901a4fbf6aSSrinivas Pandruvada 		iio_triggered_buffer_cleanup(indio_dev);
6911a4fbf6aSSrinivas Pandruvada 		iio_trigger_unregister(data->trig);
6921a4fbf6aSSrinivas Pandruvada 		iio_trigger_free(data->trig);
6931a4fbf6aSSrinivas Pandruvada 	}
6941a4fbf6aSSrinivas Pandruvada 
6951a4fbf6aSSrinivas Pandruvada 	mutex_lock(&data->mutex);
6961a4fbf6aSSrinivas Pandruvada 	kxcjk1013_set_mode(data, STANDBY);
6971a4fbf6aSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
6981a4fbf6aSSrinivas Pandruvada 
6991a4fbf6aSSrinivas Pandruvada 	return 0;
7001a4fbf6aSSrinivas Pandruvada }
7011a4fbf6aSSrinivas Pandruvada 
7021a4fbf6aSSrinivas Pandruvada #ifdef CONFIG_PM_SLEEP
7031a4fbf6aSSrinivas Pandruvada static int kxcjk1013_suspend(struct device *dev)
7041a4fbf6aSSrinivas Pandruvada {
7051a4fbf6aSSrinivas Pandruvada 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
7061a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
7071a4fbf6aSSrinivas Pandruvada 
7081a4fbf6aSSrinivas Pandruvada 	mutex_lock(&data->mutex);
7091a4fbf6aSSrinivas Pandruvada 	kxcjk1013_set_mode(data, STANDBY);
7101a4fbf6aSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
7111a4fbf6aSSrinivas Pandruvada 
7121a4fbf6aSSrinivas Pandruvada 	return 0;
7131a4fbf6aSSrinivas Pandruvada }
7141a4fbf6aSSrinivas Pandruvada 
7151a4fbf6aSSrinivas Pandruvada static int kxcjk1013_resume(struct device *dev)
7161a4fbf6aSSrinivas Pandruvada {
7171a4fbf6aSSrinivas Pandruvada 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
7181a4fbf6aSSrinivas Pandruvada 	struct kxcjk1013_data *data = iio_priv(indio_dev);
7191a4fbf6aSSrinivas Pandruvada 
7201a4fbf6aSSrinivas Pandruvada 	mutex_lock(&data->mutex);
7211a4fbf6aSSrinivas Pandruvada 
7221a4fbf6aSSrinivas Pandruvada 	if (data->power_state)
7231a4fbf6aSSrinivas Pandruvada 		kxcjk1013_set_mode(data, OPERATION);
7241a4fbf6aSSrinivas Pandruvada 
7251a4fbf6aSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
7261a4fbf6aSSrinivas Pandruvada 
7271a4fbf6aSSrinivas Pandruvada 	return 0;
7281a4fbf6aSSrinivas Pandruvada }
7291a4fbf6aSSrinivas Pandruvada 
7301a4fbf6aSSrinivas Pandruvada static SIMPLE_DEV_PM_OPS(kxcjk1013_pm_ops, kxcjk1013_suspend, kxcjk1013_resume);
7311a4fbf6aSSrinivas Pandruvada #define KXCJK1013_PM_OPS (&kxcjk1013_pm_ops)
7321a4fbf6aSSrinivas Pandruvada #else
7331a4fbf6aSSrinivas Pandruvada #define KXCJK1013_PM_OPS NULL
7341a4fbf6aSSrinivas Pandruvada #endif
7351a4fbf6aSSrinivas Pandruvada 
7361a4fbf6aSSrinivas Pandruvada static const struct acpi_device_id kx_acpi_match[] = {
7371a4fbf6aSSrinivas Pandruvada 	{"KXCJ1013", 0},
7381a4fbf6aSSrinivas Pandruvada 	{ },
7391a4fbf6aSSrinivas Pandruvada };
7401a4fbf6aSSrinivas Pandruvada MODULE_DEVICE_TABLE(acpi, kx_acpi_match);
7411a4fbf6aSSrinivas Pandruvada 
7421a4fbf6aSSrinivas Pandruvada static const struct i2c_device_id kxcjk1013_id[] = {
7431a4fbf6aSSrinivas Pandruvada 	{"kxcjk1013", 0},
7441a4fbf6aSSrinivas Pandruvada 	{}
7451a4fbf6aSSrinivas Pandruvada };
7461a4fbf6aSSrinivas Pandruvada 
7471a4fbf6aSSrinivas Pandruvada MODULE_DEVICE_TABLE(i2c, kxcjk1013_id);
7481a4fbf6aSSrinivas Pandruvada 
7491a4fbf6aSSrinivas Pandruvada static struct i2c_driver kxcjk1013_driver = {
7501a4fbf6aSSrinivas Pandruvada 	.driver = {
7511a4fbf6aSSrinivas Pandruvada 		.name	= KXCJK1013_DRV_NAME,
7521a4fbf6aSSrinivas Pandruvada 		.acpi_match_table = ACPI_PTR(kx_acpi_match),
7531a4fbf6aSSrinivas Pandruvada 		.pm	= KXCJK1013_PM_OPS,
7541a4fbf6aSSrinivas Pandruvada 	},
7551a4fbf6aSSrinivas Pandruvada 	.probe		= kxcjk1013_probe,
7561a4fbf6aSSrinivas Pandruvada 	.remove		= kxcjk1013_remove,
7571a4fbf6aSSrinivas Pandruvada 	.id_table	= kxcjk1013_id,
7581a4fbf6aSSrinivas Pandruvada };
7591a4fbf6aSSrinivas Pandruvada module_i2c_driver(kxcjk1013_driver);
7601a4fbf6aSSrinivas Pandruvada 
7611a4fbf6aSSrinivas Pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
7621a4fbf6aSSrinivas Pandruvada MODULE_LICENSE("GPL v2");
7631a4fbf6aSSrinivas Pandruvada MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");
764