aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/accel
diff options
context:
space:
mode:
authorSrinivas Pandruvada <srinivas.pandruvada@linux.intel.com>2014-11-06 18:07:00 -0500
committerJonathan Cameron <jic23@kernel.org>2014-06-14 12:10:21 -0400
commit1a4fbf6a9286a6e3db497bc7bbae2024f0f1ad90 (patch)
treed436f52669a239d8494c565715114aaa336a9946 /drivers/iio/accel
parent398fd22b6b94cb15c1c299bceecd63644a1b17b4 (diff)
iio: accel: kxcjk1013 3-axis accelerometer driver
This patch adds IIO driver for KXCJK 1013 triaxis accelerometer sensor. The specifications for this driver is downloaded from: http://www.kionix.com/sites/default/files/KXCJK-1013%20Specifications%20Rev%202.pdf Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio/accel')
-rw-r--r--drivers/iio/accel/Kconfig12
-rw-r--r--drivers/iio/accel/Makefile1
-rw-r--r--drivers/iio/accel/kxcjk-1013.c760
3 files changed, 773 insertions, 0 deletions
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index 1e120fa1e156..12addf272a61 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -77,4 +77,16 @@ config MMA8452
77 To compile this driver as a module, choose M here: the module 77 To compile this driver as a module, choose M here: the module
78 will be called mma8452. 78 will be called mma8452.
79 79
80config KXCJK1013
81 tristate "Kionix 3-Axis Accelerometer Driver"
82 depends on I2C
83 select IIO_BUFFER
84 select IIO_TRIGGERED_BUFFER
85 help
86 Say Y here if you want to build a driver for the Kionix KXCJK-1013
87 triaxial acceleration sensor.
88
89 To compile this driver as a module, choose M here: the module will
90 be called kxcjk-1013.
91
80endmenu 92endmenu
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index dc0e379c2592..6578ca1a8e09 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -5,6 +5,7 @@
5# When adding new entries keep the list in alphabetical order 5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_BMA180) += bma180.o 6obj-$(CONFIG_BMA180) += bma180.o
7obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o 7obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
8obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
8obj-$(CONFIG_KXSD9) += kxsd9.o 9obj-$(CONFIG_KXSD9) += kxsd9.o
9obj-$(CONFIG_MMA8452) += mma8452.o 10obj-$(CONFIG_MMA8452) += mma8452.o
10 11
diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c
new file mode 100644
index 000000000000..2326e430c999
--- /dev/null
+++ b/drivers/iio/accel/kxcjk-1013.c
@@ -0,0 +1,760 @@
1/*
2 * KXCJK-1013 3-axis accelerometer driver
3 * Copyright (c) 2014, Intel Corporation.
4 *
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.
8 *
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
12 * more details.
13 */
14
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>
23#include <linux/gpio/consumer.h>
24#include <linux/iio/iio.h>
25#include <linux/iio/sysfs.h>
26#include <linux/iio/buffer.h>
27#include <linux/iio/trigger.h>
28#include <linux/iio/trigger_consumer.h>
29#include <linux/iio/triggered_buffer.h>
30#include <linux/iio/accel/kxcjk_1013.h>
31
32#define KXCJK1013_DRV_NAME "kxcjk1013"
33#define KXCJK1013_IRQ_NAME "kxcjk1013_event"
34
35#define KXCJK1013_REG_XOUT_L 0x06
36/*
37 * From low byte X axis register, all the other addresses of Y and Z can be
38 * obtained by just applying axis offset. The following axis defines are just
39 * provide clarity, but not used.
40 */
41#define KXCJK1013_REG_XOUT_H 0x07
42#define KXCJK1013_REG_YOUT_L 0x08
43#define KXCJK1013_REG_YOUT_H 0x09
44#define KXCJK1013_REG_ZOUT_L 0x0A
45#define KXCJK1013_REG_ZOUT_H 0x0B
46
47#define KXCJK1013_REG_DCST_RESP 0x0C
48#define KXCJK1013_REG_WHO_AM_I 0x0F
49#define KXCJK1013_REG_INT_SRC1 0x16
50#define KXCJK1013_REG_INT_SRC2 0x17
51#define KXCJK1013_REG_STATUS_REG 0x18
52#define KXCJK1013_REG_INT_REL 0x1A
53#define KXCJK1013_REG_CTRL1 0x1B
54#define KXCJK1013_REG_CTRL2 0x1D
55#define KXCJK1013_REG_INT_CTRL1 0x1E
56#define KXCJK1013_REG_INT_CTRL2 0x1F
57#define KXCJK1013_REG_DATA_CTRL 0x21
58#define KXCJK1013_REG_WAKE_TIMER 0x29
59#define KXCJK1013_REG_SELF_TEST 0x3A
60#define KXCJK1013_REG_WAKE_THRES 0x6A
61
62#define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7)
63#define KXCJK1013_REG_CTRL1_BIT_RES BIT(6)
64#define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5)
65#define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4)
66#define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3)
67#define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1)
68#define KXCJK1013_REG_INT_REG1_BIT_IEA BIT(4)
69#define KXCJK1013_REG_INT_REG1_BIT_IEN BIT(5)
70
71#define KXCJK1013_DATA_MASK_12_BIT 0x0FFF
72#define KXCJK1013_MAX_STARTUP_TIME_US 100000
73
74struct kxcjk1013_data {
75 struct i2c_client *client;
76 struct iio_trigger *trig;
77 bool trig_mode;
78 struct mutex mutex;
79 s16 buffer[8];
80 int power_state;
81 u8 odr_bits;
82 bool active_high_intr;
83};
84
85enum kxcjk1013_axis {
86 AXIS_X,
87 AXIS_Y,
88 AXIS_Z,
89};
90
91enum kxcjk1013_mode {
92 STANDBY,
93 OPERATION,
94};
95
96static const struct {
97 int val;
98 int val2;
99 int odr_bits;
100} samp_freq_table[] = { {0, 781000, 0x08}, {1, 563000, 0x09},
101 {3, 125000, 0x0A}, {6, 25000, 0x0B}, {12, 5000, 0},
102 {25, 0, 0x01}, {50, 0, 0x02}, {100, 0, 0x03},
103 {200, 0, 0x04}, {400, 0, 0x05}, {800, 0, 0x06},
104 {1600, 0, 0x07} };
105
106/* Refer to section 4 of the specification */
107static const struct {
108 int odr_bits;
109 int usec;
110} odr_start_up_times[] = { {0x08, 100000}, {0x09, 100000}, {0x0A, 100000},
111 {0x0B, 100000}, { 0, 80000}, {0x01, 41000},
112 {0x02, 21000}, {0x03, 11000}, {0x04, 6400},
113 {0x05, 3900}, {0x06, 2700}, {0x07, 2100} };
114
115static int kxcjk1013_set_mode(struct kxcjk1013_data *data,
116 enum kxcjk1013_mode mode)
117{
118 int ret;
119
120 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
121 if (ret < 0) {
122 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
123 return ret;
124 }
125
126 if (mode == STANDBY)
127 ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1;
128 else
129 ret |= KXCJK1013_REG_CTRL1_BIT_PC1;
130
131 ret = i2c_smbus_write_byte_data(data->client,
132 KXCJK1013_REG_CTRL1, ret);
133 if (ret < 0) {
134 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
135 return ret;
136 }
137
138 return 0;
139}
140
141static int kxcjk1013_chip_ack_intr(struct kxcjk1013_data *data)
142{
143 int ret;
144
145 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL);
146 if (ret < 0) {
147 dev_err(&data->client->dev, "Error writing reg_int_rel\n");
148 return ret;
149 }
150
151 return ret;
152}
153
154static int kxcjk1013_chip_init(struct kxcjk1013_data *data)
155{
156 int ret;
157
158 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I);
159 if (ret < 0) {
160 dev_err(&data->client->dev, "Error reading who_am_i\n");
161 return ret;
162 }
163
164 dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret);
165
166 ret = kxcjk1013_set_mode(data, STANDBY);
167 if (ret < 0)
168 return ret;
169
170 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
171 if (ret < 0) {
172 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
173 return ret;
174 }
175
176 /* Setting range to 4G */
177 ret |= KXCJK1013_REG_CTRL1_BIT_GSEL0;
178 ret &= ~KXCJK1013_REG_CTRL1_BIT_GSEL1;
179
180 /* Set 12 bit mode */
181 ret |= KXCJK1013_REG_CTRL1_BIT_RES;
182
183 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL1,
184 ret);
185 if (ret < 0) {
186 dev_err(&data->client->dev, "Error reading reg_ctrl\n");
187 return ret;
188 }
189
190 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL);
191 if (ret < 0) {
192 dev_err(&data->client->dev, "Error reading reg_data_ctrl\n");
193 return ret;
194 }
195
196 data->odr_bits = ret;
197
198 /* Set up INT polarity */
199 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
200 if (ret < 0) {
201 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
202 return ret;
203 }
204
205 if (data->active_high_intr)
206 ret |= KXCJK1013_REG_INT_REG1_BIT_IEA;
207 else
208 ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEA;
209
210 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
211 ret);
212 if (ret < 0) {
213 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
214 return ret;
215 }
216
217 return 0;
218}
219
220static int kxcjk1013_chip_setup_interrupt(struct kxcjk1013_data *data,
221 bool status)
222{
223 int ret;
224
225 /* This is requirement by spec to change state to STANDBY */
226 ret = kxcjk1013_set_mode(data, STANDBY);
227 if (ret < 0)
228 return ret;
229
230 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
231 if (ret < 0) {
232 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
233 return ret;
234 }
235
236 if (status)
237 ret |= KXCJK1013_REG_INT_REG1_BIT_IEN;
238 else
239 ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEN;
240
241 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
242 ret);
243 if (ret < 0) {
244 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
245 return ret;
246 }
247
248 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
249 if (ret < 0) {
250 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
251 return ret;
252 }
253
254 if (status)
255 ret |= KXCJK1013_REG_CTRL1_BIT_DRDY;
256 else
257 ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY;
258
259 ret = i2c_smbus_write_byte_data(data->client,
260 KXCJK1013_REG_CTRL1, ret);
261 if (ret < 0) {
262 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
263 return ret;
264 }
265
266 return ret;
267}
268
269static int kxcjk1013_convert_freq_to_bit(int val, int val2)
270{
271 int i;
272
273 for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) {
274 if (samp_freq_table[i].val == val &&
275 samp_freq_table[i].val2 == val2) {
276 return samp_freq_table[i].odr_bits;
277 }
278 }
279
280 return -EINVAL;
281}
282
283static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2)
284{
285 int ret;
286 int odr_bits;
287
288 odr_bits = kxcjk1013_convert_freq_to_bit(val, val2);
289 if (odr_bits < 0)
290 return odr_bits;
291
292 /* To change ODR, the chip must be set to STANDBY as per spec */
293 ret = kxcjk1013_set_mode(data, STANDBY);
294 if (ret < 0)
295 return ret;
296
297 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_DATA_CTRL,
298 odr_bits);
299 if (ret < 0) {
300 dev_err(&data->client->dev, "Error writing data_ctrl\n");
301 return ret;
302 }
303
304 data->odr_bits = odr_bits;
305
306 /* Check, if the ODR is changed after data enable */
307 if (data->power_state) {
308 /* Set the state back to operation */
309 ret = kxcjk1013_set_mode(data, OPERATION);
310 if (ret < 0)
311 return ret;
312 }
313
314 return 0;
315}
316
317static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2)
318{
319 int i;
320
321 for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) {
322 if (samp_freq_table[i].odr_bits == data->odr_bits) {
323 *val = samp_freq_table[i].val;
324 *val2 = samp_freq_table[i].val2;
325 return IIO_VAL_INT_PLUS_MICRO;
326 }
327 }
328
329 return -EINVAL;
330}
331
332static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis)
333{
334 u8 reg = KXCJK1013_REG_XOUT_L + axis * 2;
335 int ret;
336
337 ret = i2c_smbus_read_word_data(data->client, reg);
338 if (ret < 0) {
339 dev_err(&data->client->dev,
340 "failed to read accel_%c registers\n", 'x' + axis);
341 return ret;
342 }
343
344 return ret;
345}
346
347static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data)
348{
349 int i;
350
351 for (i = 0; i < ARRAY_SIZE(odr_start_up_times); ++i) {
352 if (odr_start_up_times[i].odr_bits == data->odr_bits)
353 return odr_start_up_times[i].usec;
354 }
355
356 return KXCJK1013_MAX_STARTUP_TIME_US;
357}
358
359static int kxcjk1013_read_raw(struct iio_dev *indio_dev,
360 struct iio_chan_spec const *chan, int *val,
361 int *val2, long mask)
362{
363 struct kxcjk1013_data *data = iio_priv(indio_dev);
364 int ret;
365
366 switch (mask) {
367 case IIO_CHAN_INFO_RAW:
368 mutex_lock(&data->mutex);
369 if (iio_buffer_enabled(indio_dev))
370 ret = -EBUSY;
371 else {
372 int sleep_val;
373
374 ret = kxcjk1013_set_mode(data, OPERATION);
375 if (ret < 0)
376 return ret;
377 ++data->power_state;
378 sleep_val = kxcjk1013_get_startup_times(data);
379 if (sleep_val < 20000)
380 usleep_range(sleep_val, 20000);
381 else
382 msleep_interruptible(sleep_val/1000);
383 ret = kxcjk1013_get_acc_reg(data, chan->scan_index);
384 if (--data->power_state == 0)
385 kxcjk1013_set_mode(data, STANDBY);
386 }
387 mutex_unlock(&data->mutex);
388
389 if (ret < 0)
390 return ret;
391
392 *val = sign_extend32(ret >> 4, 11);
393 return IIO_VAL_INT;
394
395 case IIO_CHAN_INFO_SCALE:
396 *val = 0;
397 *val2 = 19163; /* range +-4g (4/2047*9.806650) */
398 return IIO_VAL_INT_PLUS_MICRO;
399
400 case IIO_CHAN_INFO_SAMP_FREQ:
401 mutex_lock(&data->mutex);
402 ret = kxcjk1013_get_odr(data, val, val2);
403 mutex_unlock(&data->mutex);
404 return ret;
405
406 default:
407 return -EINVAL;
408 }
409}
410
411static int kxcjk1013_write_raw(struct iio_dev *indio_dev,
412 struct iio_chan_spec const *chan, int val,
413 int val2, long mask)
414{
415 struct kxcjk1013_data *data = iio_priv(indio_dev);
416 int ret;
417
418 switch (mask) {
419 case IIO_CHAN_INFO_SAMP_FREQ:
420 mutex_lock(&data->mutex);
421 ret = kxcjk1013_set_odr(data, val, val2);
422 mutex_unlock(&data->mutex);
423 break;
424 default:
425 ret = -EINVAL;
426 }
427
428 return ret;
429}
430
431static int kxcjk1013_validate_trigger(struct iio_dev *indio_dev,
432 struct iio_trigger *trig)
433{
434 struct kxcjk1013_data *data = iio_priv(indio_dev);
435
436 if (data->trig != trig)
437 return -EINVAL;
438
439 return 0;
440}
441
442static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
443 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600");
444
445static struct attribute *kxcjk1013_attributes[] = {
446 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
447 NULL,
448};
449
450static const struct attribute_group kxcjk1013_attrs_group = {
451 .attrs = kxcjk1013_attributes,
452};
453
454#define KXCJK1013_CHANNEL(_axis) { \
455 .type = IIO_ACCEL, \
456 .modified = 1, \
457 .channel2 = IIO_MOD_##_axis, \
458 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
459 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
460 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
461 .scan_index = AXIS_##_axis, \
462 .scan_type = { \
463 .sign = 's', \
464 .realbits = 12, \
465 .storagebits = 16, \
466 .shift = 4, \
467 .endianness = IIO_LE, \
468 }, \
469}
470
471static const struct iio_chan_spec kxcjk1013_channels[] = {
472 KXCJK1013_CHANNEL(X),
473 KXCJK1013_CHANNEL(Y),
474 KXCJK1013_CHANNEL(Z),
475 IIO_CHAN_SOFT_TIMESTAMP(3),
476};
477
478static const struct iio_info kxcjk1013_info = {
479 .attrs = &kxcjk1013_attrs_group,
480 .read_raw = kxcjk1013_read_raw,
481 .write_raw = kxcjk1013_write_raw,
482 .validate_trigger = kxcjk1013_validate_trigger,
483 .driver_module = THIS_MODULE,
484};
485
486static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p)
487{
488 struct iio_poll_func *pf = p;
489 struct iio_dev *indio_dev = pf->indio_dev;
490 struct kxcjk1013_data *data = iio_priv(indio_dev);
491 int bit, ret, i = 0;
492
493 mutex_lock(&data->mutex);
494
495 for_each_set_bit(bit, indio_dev->buffer->scan_mask,
496 indio_dev->masklength) {
497 ret = kxcjk1013_get_acc_reg(data, bit);
498 if (ret < 0) {
499 kxcjk1013_chip_ack_intr(data);
500 mutex_unlock(&data->mutex);
501 goto err;
502 }
503 data->buffer[i++] = ret;
504 }
505
506 kxcjk1013_chip_ack_intr(data);
507
508 mutex_unlock(&data->mutex);
509
510 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
511 pf->timestamp);
512err:
513 iio_trigger_notify_done(indio_dev->trig);
514
515 return IRQ_HANDLED;
516}
517
518static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig,
519 bool state)
520{
521 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
522 struct kxcjk1013_data *data = iio_priv(indio_dev);
523
524 mutex_lock(&data->mutex);
525 if (state) {
526 kxcjk1013_chip_setup_interrupt(data, true);
527 kxcjk1013_set_mode(data, OPERATION);
528 ++data->power_state;
529 } else {
530 if (--data->power_state)
531 return 0;
532 kxcjk1013_chip_setup_interrupt(data, false);
533 kxcjk1013_set_mode(data, STANDBY);
534 }
535 mutex_unlock(&data->mutex);
536
537 return 0;
538}
539
540static const struct iio_trigger_ops kxcjk1013_trigger_ops = {
541 .set_trigger_state = kxcjk1013_data_rdy_trigger_set_state,
542 .owner = THIS_MODULE,
543};
544
545static int kxcjk1013_acpi_gpio_probe(struct i2c_client *client,
546 struct kxcjk1013_data *data)
547{
548 const struct acpi_device_id *id;
549 struct device *dev;
550 struct gpio_desc *gpio;
551 int ret;
552
553 if (!client)
554 return -EINVAL;
555
556 dev = &client->dev;
557 if (!ACPI_HANDLE(dev))
558 return -ENODEV;
559
560 id = acpi_match_device(dev->driver->acpi_match_table, dev);
561 if (!id)
562 return -ENODEV;
563
564 /* data ready gpio interrupt pin */
565 gpio = devm_gpiod_get_index(dev, "kxcjk1013_int", 0);
566 if (IS_ERR(gpio)) {
567 dev_err(dev, "acpi gpio get index failed\n");
568 return PTR_ERR(gpio);
569 }
570
571 ret = gpiod_direction_input(gpio);
572 if (ret)
573 return ret;
574
575 ret = gpiod_to_irq(gpio);
576
577 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
578
579 return ret;
580}
581
582static int kxcjk1013_probe(struct i2c_client *client,
583 const struct i2c_device_id *id)
584{
585 struct kxcjk1013_data *data;
586 struct iio_dev *indio_dev;
587 struct iio_trigger *trig = NULL;
588 struct kxcjk_1013_platform_data *pdata;
589 int ret;
590
591 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
592 if (!indio_dev)
593 return -ENOMEM;
594
595 data = iio_priv(indio_dev);
596 i2c_set_clientdata(client, indio_dev);
597 data->client = client;
598
599 pdata = dev_get_platdata(&client->dev);
600 if (pdata)
601 data->active_high_intr = pdata->active_high_intr;
602 else
603 data->active_high_intr = true; /* default polarity */
604
605 ret = kxcjk1013_chip_init(data);
606 if (ret < 0)
607 return ret;
608
609 mutex_init(&data->mutex);
610
611 indio_dev->dev.parent = &client->dev;
612 indio_dev->channels = kxcjk1013_channels;
613 indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels);
614 indio_dev->name = KXCJK1013_DRV_NAME;
615 indio_dev->modes = INDIO_DIRECT_MODE;
616 indio_dev->info = &kxcjk1013_info;
617
618 if (client->irq < 0)
619 client->irq = kxcjk1013_acpi_gpio_probe(client, data);
620
621 if (client->irq >= 0) {
622 trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
623 indio_dev->id);
624 if (!trig)
625 return -ENOMEM;
626
627 data->trig_mode = true;
628
629 ret = devm_request_irq(&client->dev, client->irq,
630 iio_trigger_generic_data_rdy_poll,
631 IRQF_TRIGGER_RISING,
632 KXCJK1013_IRQ_NAME,
633 trig);
634 if (ret) {
635 dev_err(&client->dev, "unable to request IRQ\n");
636 goto err_trigger_free;
637 }
638
639 trig->dev.parent = &client->dev;
640 trig->ops = &kxcjk1013_trigger_ops;
641 iio_trigger_set_drvdata(trig, indio_dev);
642 data->trig = trig;
643 indio_dev->trig = trig;
644
645 ret = iio_trigger_register(trig);
646 if (ret)
647 goto err_trigger_free;
648
649 ret = iio_triggered_buffer_setup(indio_dev,
650 &iio_pollfunc_store_time,
651 kxcjk1013_trigger_handler,
652 NULL);
653 if (ret < 0) {
654 dev_err(&client->dev,
655 "iio triggered buffer setup failed\n");
656 goto err_trigger_unregister;
657 }
658 }
659
660 ret = devm_iio_device_register(&client->dev, indio_dev);
661 if (ret < 0) {
662 dev_err(&client->dev, "unable to register iio device\n");
663 goto err_buffer_cleanup;
664 }
665
666 return 0;
667
668err_buffer_cleanup:
669 if (data->trig_mode)
670 iio_triggered_buffer_cleanup(indio_dev);
671err_trigger_unregister:
672 if (data->trig_mode)
673 iio_trigger_unregister(trig);
674err_trigger_free:
675 if (data->trig_mode)
676 iio_trigger_free(trig);
677
678 return ret;
679}
680
681static int kxcjk1013_remove(struct i2c_client *client)
682{
683 struct iio_dev *indio_dev = i2c_get_clientdata(client);
684 struct kxcjk1013_data *data = iio_priv(indio_dev);
685
686 if (data->trig_mode) {
687 iio_triggered_buffer_cleanup(indio_dev);
688 iio_trigger_unregister(data->trig);
689 iio_trigger_free(data->trig);
690 }
691
692 mutex_lock(&data->mutex);
693 kxcjk1013_set_mode(data, STANDBY);
694 mutex_unlock(&data->mutex);
695
696 return 0;
697}
698
699#ifdef CONFIG_PM_SLEEP
700static int kxcjk1013_suspend(struct device *dev)
701{
702 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
703 struct kxcjk1013_data *data = iio_priv(indio_dev);
704
705 mutex_lock(&data->mutex);
706 kxcjk1013_set_mode(data, STANDBY);
707 mutex_unlock(&data->mutex);
708
709 return 0;
710}
711
712static int kxcjk1013_resume(struct device *dev)
713{
714 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
715 struct kxcjk1013_data *data = iio_priv(indio_dev);
716
717 mutex_lock(&data->mutex);
718
719 if (data->power_state)
720 kxcjk1013_set_mode(data, OPERATION);
721
722 mutex_unlock(&data->mutex);
723
724 return 0;
725}
726
727static SIMPLE_DEV_PM_OPS(kxcjk1013_pm_ops, kxcjk1013_suspend, kxcjk1013_resume);
728#define KXCJK1013_PM_OPS (&kxcjk1013_pm_ops)
729#else
730#define KXCJK1013_PM_OPS NULL
731#endif
732
733static const struct acpi_device_id kx_acpi_match[] = {
734 {"KXCJ1013", 0},
735 { },
736};
737MODULE_DEVICE_TABLE(acpi, kx_acpi_match);
738
739static const struct i2c_device_id kxcjk1013_id[] = {
740 {"kxcjk1013", 0},
741 {}
742};
743
744MODULE_DEVICE_TABLE(i2c, kxcjk1013_id);
745
746static struct i2c_driver kxcjk1013_driver = {
747 .driver = {
748 .name = KXCJK1013_DRV_NAME,
749 .acpi_match_table = ACPI_PTR(kx_acpi_match),
750 .pm = KXCJK1013_PM_OPS,
751 },
752 .probe = kxcjk1013_probe,
753 .remove = kxcjk1013_remove,
754 .id_table = kxcjk1013_id,
755};
756module_i2c_driver(kxcjk1013_driver);
757
758MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
759MODULE_LICENSE("GPL v2");
760MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");