irqchip/s3c24xx: Mark init_eint as __maybe_unused
[linux/fpc-iii.git] / drivers / iio / imu / kmx61.c
blobdbf5e99366358815cd3d840440d6d8cc3adf8b54
1 /*
2 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
4 * Copyright (c) 2014, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/acpi.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/interrupt.h>
19 #include <linux/pm.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/events.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/trigger_consumer.h>
29 #define KMX61_DRV_NAME "kmx61"
30 #define KMX61_IRQ_NAME "kmx61_event"
32 #define KMX61_REG_WHO_AM_I 0x00
33 #define KMX61_REG_INS1 0x01
34 #define KMX61_REG_INS2 0x02
37 * three 16-bit accelerometer output registers for X/Y/Z axis
38 * we use only XOUT_L as a base register, all other addresses
39 * can be obtained by applying an offset and are provided here
40 * only for clarity.
42 #define KMX61_ACC_XOUT_L 0x0A
43 #define KMX61_ACC_XOUT_H 0x0B
44 #define KMX61_ACC_YOUT_L 0x0C
45 #define KMX61_ACC_YOUT_H 0x0D
46 #define KMX61_ACC_ZOUT_L 0x0E
47 #define KMX61_ACC_ZOUT_H 0x0F
50 * one 16-bit temperature output register
52 #define KMX61_TEMP_L 0x10
53 #define KMX61_TEMP_H 0x11
56 * three 16-bit magnetometer output registers for X/Y/Z axis
58 #define KMX61_MAG_XOUT_L 0x12
59 #define KMX61_MAG_XOUT_H 0x13
60 #define KMX61_MAG_YOUT_L 0x14
61 #define KMX61_MAG_YOUT_H 0x15
62 #define KMX61_MAG_ZOUT_L 0x16
63 #define KMX61_MAG_ZOUT_H 0x17
65 #define KMX61_REG_INL 0x28
66 #define KMX61_REG_STBY 0x29
67 #define KMX61_REG_CTRL1 0x2A
68 #define KMX61_REG_CTRL2 0x2B
69 #define KMX61_REG_ODCNTL 0x2C
70 #define KMX61_REG_INC1 0x2D
72 #define KMX61_REG_WUF_THRESH 0x3D
73 #define KMX61_REG_WUF_TIMER 0x3E
75 #define KMX61_ACC_STBY_BIT BIT(0)
76 #define KMX61_MAG_STBY_BIT BIT(1)
77 #define KMX61_ACT_STBY_BIT BIT(7)
79 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
81 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
83 #define KMX61_REG_INS2_BIT_ZP BIT(0)
84 #define KMX61_REG_INS2_BIT_ZN BIT(1)
85 #define KMX61_REG_INS2_BIT_YP BIT(2)
86 #define KMX61_REG_INS2_BIT_YN BIT(3)
87 #define KMX61_REG_INS2_BIT_XP BIT(4)
88 #define KMX61_REG_INS2_BIT_XN BIT(5)
90 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
92 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
93 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
94 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
95 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
97 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
98 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
99 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
100 #define KMX61_REG_INC1_BIT_IEN BIT(5)
102 #define KMX61_ACC_ODR_SHIFT 0
103 #define KMX61_MAG_ODR_SHIFT 4
104 #define KMX61_ACC_ODR_MASK 0x0F
105 #define KMX61_MAG_ODR_MASK 0xF0
107 #define KMX61_OWUF_MASK 0x7
109 #define KMX61_DEFAULT_WAKE_THRESH 1
110 #define KMX61_DEFAULT_WAKE_DURATION 1
112 #define KMX61_SLEEP_DELAY_MS 2000
114 #define KMX61_CHIP_ID 0x12
116 /* KMX61 devices */
117 #define KMX61_ACC 0x01
118 #define KMX61_MAG 0x02
120 struct kmx61_data {
121 struct i2c_client *client;
123 /* serialize access to non-atomic ops, e.g set_mode */
124 struct mutex lock;
126 /* standby state */
127 bool acc_stby;
128 bool mag_stby;
130 /* power state */
131 bool acc_ps;
132 bool mag_ps;
134 /* config bits */
135 u8 range;
136 u8 odr_bits;
137 u8 wake_thresh;
138 u8 wake_duration;
140 /* accelerometer specific data */
141 struct iio_dev *acc_indio_dev;
142 struct iio_trigger *acc_dready_trig;
143 struct iio_trigger *motion_trig;
144 bool acc_dready_trig_on;
145 bool motion_trig_on;
146 bool ev_enable_state;
148 /* magnetometer specific data */
149 struct iio_dev *mag_indio_dev;
150 struct iio_trigger *mag_dready_trig;
151 bool mag_dready_trig_on;
154 enum kmx61_range {
155 KMX61_RANGE_2G,
156 KMX61_RANGE_4G,
157 KMX61_RANGE_8G,
160 enum kmx61_axis {
161 KMX61_AXIS_X,
162 KMX61_AXIS_Y,
163 KMX61_AXIS_Z,
166 static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
168 static const struct {
169 int val;
170 int val2;
171 } kmx61_samp_freq_table[] = { {12, 500000},
172 {25, 0},
173 {50, 0},
174 {100, 0},
175 {200, 0},
176 {400, 0},
177 {800, 0},
178 {1600, 0},
179 {0, 781000},
180 {1, 563000},
181 {3, 125000},
182 {6, 250000} };
184 static const struct {
185 int val;
186 int val2;
187 int odr_bits;
188 } kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
189 {1, 563000, 0x01},
190 {3, 125000, 0x02},
191 {6, 250000, 0x03},
192 {12, 500000, 0x04},
193 {25, 0, 0x05},
194 {50, 0, 0x06},
195 {100, 0, 0x06},
196 {200, 0, 0x06},
197 {400, 0, 0x06},
198 {800, 0, 0x06},
199 {1600, 0, 0x06} };
201 static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
202 static IIO_CONST_ATTR(magn_scale_available, "0.001465");
203 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
204 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
206 static struct attribute *kmx61_acc_attributes[] = {
207 &iio_const_attr_accel_scale_available.dev_attr.attr,
208 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
209 NULL,
212 static struct attribute *kmx61_mag_attributes[] = {
213 &iio_const_attr_magn_scale_available.dev_attr.attr,
214 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
215 NULL,
218 static const struct attribute_group kmx61_acc_attribute_group = {
219 .attrs = kmx61_acc_attributes,
222 static const struct attribute_group kmx61_mag_attribute_group = {
223 .attrs = kmx61_mag_attributes,
226 static const struct iio_event_spec kmx61_event = {
227 .type = IIO_EV_TYPE_THRESH,
228 .dir = IIO_EV_DIR_EITHER,
229 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
230 BIT(IIO_EV_INFO_ENABLE) |
231 BIT(IIO_EV_INFO_PERIOD),
234 #define KMX61_ACC_CHAN(_axis) { \
235 .type = IIO_ACCEL, \
236 .modified = 1, \
237 .channel2 = IIO_MOD_ ## _axis, \
238 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
239 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
240 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
241 .address = KMX61_ACC, \
242 .scan_index = KMX61_AXIS_ ## _axis, \
243 .scan_type = { \
244 .sign = 's', \
245 .realbits = 12, \
246 .storagebits = 16, \
247 .shift = 4, \
248 .endianness = IIO_LE, \
249 }, \
250 .event_spec = &kmx61_event, \
251 .num_event_specs = 1 \
254 #define KMX61_MAG_CHAN(_axis) { \
255 .type = IIO_MAGN, \
256 .modified = 1, \
257 .channel2 = IIO_MOD_ ## _axis, \
258 .address = KMX61_MAG, \
259 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
260 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
261 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
262 .scan_index = KMX61_AXIS_ ## _axis, \
263 .scan_type = { \
264 .sign = 's', \
265 .realbits = 14, \
266 .storagebits = 16, \
267 .shift = 2, \
268 .endianness = IIO_LE, \
269 }, \
272 static const struct iio_chan_spec kmx61_acc_channels[] = {
273 KMX61_ACC_CHAN(X),
274 KMX61_ACC_CHAN(Y),
275 KMX61_ACC_CHAN(Z),
278 static const struct iio_chan_spec kmx61_mag_channels[] = {
279 KMX61_MAG_CHAN(X),
280 KMX61_MAG_CHAN(Y),
281 KMX61_MAG_CHAN(Z),
284 static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
286 struct kmx61_data **priv = iio_priv(indio_dev);
288 *priv = data;
291 static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
293 return *(struct kmx61_data **)iio_priv(indio_dev);
296 static int kmx61_convert_freq_to_bit(int val, int val2)
298 int i;
300 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
301 if (val == kmx61_samp_freq_table[i].val &&
302 val2 == kmx61_samp_freq_table[i].val2)
303 return i;
304 return -EINVAL;
307 static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
309 int i;
311 for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
312 if (kmx61_wake_up_odr_table[i].val == val &&
313 kmx61_wake_up_odr_table[i].val2 == val2)
314 return kmx61_wake_up_odr_table[i].odr_bits;
315 return -EINVAL;
319 * kmx61_set_mode() - set KMX61 device operating mode
320 * @data - kmx61 device private data pointer
321 * @mode - bitmask, indicating operating mode for @device
322 * @device - bitmask, indicating device for which @mode needs to be set
323 * @update - update stby bits stored in device's private @data
325 * For each sensor (accelerometer/magnetometer) there are two operating modes
326 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
327 * if they are both enabled. Internal sensors state is saved in acc_stby and
328 * mag_stby members of driver's private @data.
330 static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
331 bool update)
333 int ret;
334 int acc_stby = -1, mag_stby = -1;
336 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
337 if (ret < 0) {
338 dev_err(&data->client->dev, "Error reading reg_stby\n");
339 return ret;
341 if (device & KMX61_ACC) {
342 if (mode & KMX61_ACC_STBY_BIT) {
343 ret |= KMX61_ACC_STBY_BIT;
344 acc_stby = 1;
345 } else {
346 ret &= ~KMX61_ACC_STBY_BIT;
347 acc_stby = 0;
351 if (device & KMX61_MAG) {
352 if (mode & KMX61_MAG_STBY_BIT) {
353 ret |= KMX61_MAG_STBY_BIT;
354 mag_stby = 1;
355 } else {
356 ret &= ~KMX61_MAG_STBY_BIT;
357 mag_stby = 0;
361 if (mode & KMX61_ACT_STBY_BIT)
362 ret |= KMX61_ACT_STBY_BIT;
364 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
365 if (ret < 0) {
366 dev_err(&data->client->dev, "Error writing reg_stby\n");
367 return ret;
370 if (acc_stby != -1 && update)
371 data->acc_stby = acc_stby;
372 if (mag_stby != -1 && update)
373 data->mag_stby = mag_stby;
375 return 0;
378 static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
380 int ret;
382 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
383 if (ret < 0) {
384 dev_err(&data->client->dev, "Error reading reg_stby\n");
385 return ret;
387 *mode = 0;
389 if (device & KMX61_ACC) {
390 if (ret & KMX61_ACC_STBY_BIT)
391 *mode |= KMX61_ACC_STBY_BIT;
392 else
393 *mode &= ~KMX61_ACC_STBY_BIT;
396 if (device & KMX61_MAG) {
397 if (ret & KMX61_MAG_STBY_BIT)
398 *mode |= KMX61_MAG_STBY_BIT;
399 else
400 *mode &= ~KMX61_MAG_STBY_BIT;
403 return 0;
406 static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
408 int ret, odr_bits;
410 odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
411 if (odr_bits < 0)
412 return odr_bits;
414 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
415 odr_bits);
416 if (ret < 0)
417 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
418 return ret;
421 static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
423 int ret;
424 u8 mode;
425 int lodr_bits, odr_bits;
427 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
428 if (ret < 0)
429 return ret;
431 lodr_bits = kmx61_convert_freq_to_bit(val, val2);
432 if (lodr_bits < 0)
433 return lodr_bits;
435 /* To change ODR, accel and magn must be in STDBY */
436 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
437 true);
438 if (ret < 0)
439 return ret;
441 odr_bits = 0;
442 if (device & KMX61_ACC)
443 odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
444 if (device & KMX61_MAG)
445 odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
447 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
448 odr_bits);
449 if (ret < 0)
450 return ret;
452 data->odr_bits = odr_bits;
454 if (device & KMX61_ACC) {
455 ret = kmx61_set_wake_up_odr(data, val, val2);
456 if (ret)
457 return ret;
460 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
463 static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
464 u8 device)
466 u8 lodr_bits;
468 if (device & KMX61_ACC)
469 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
470 KMX61_ACC_ODR_MASK;
471 else if (device & KMX61_MAG)
472 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
473 KMX61_MAG_ODR_MASK;
474 else
475 return -EINVAL;
477 if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
478 return -EINVAL;
480 *val = kmx61_samp_freq_table[lodr_bits].val;
481 *val2 = kmx61_samp_freq_table[lodr_bits].val2;
483 return 0;
486 static int kmx61_set_range(struct kmx61_data *data, u8 range)
488 int ret;
490 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
491 if (ret < 0) {
492 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
493 return ret;
496 ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
497 ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
499 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
500 if (ret < 0) {
501 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
502 return ret;
505 data->range = range;
507 return 0;
510 static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
512 int ret, i;
513 u8 mode;
515 for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
516 if (kmx61_uscale_table[i] == uscale) {
517 ret = kmx61_get_mode(data, &mode,
518 KMX61_ACC | KMX61_MAG);
519 if (ret < 0)
520 return ret;
522 ret = kmx61_set_mode(data, KMX61_ALL_STBY,
523 KMX61_ACC | KMX61_MAG, true);
524 if (ret < 0)
525 return ret;
527 ret = kmx61_set_range(data, i);
528 if (ret < 0)
529 return ret;
531 return kmx61_set_mode(data, mode,
532 KMX61_ACC | KMX61_MAG, true);
535 return -EINVAL;
538 static int kmx61_chip_init(struct kmx61_data *data)
540 int ret, val, val2;
542 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
543 if (ret < 0) {
544 dev_err(&data->client->dev, "Error reading who_am_i\n");
545 return ret;
548 if (ret != KMX61_CHIP_ID) {
549 dev_err(&data->client->dev,
550 "Wrong chip id, got %x expected %x\n",
551 ret, KMX61_CHIP_ID);
552 return -EINVAL;
555 /* set accel 12bit, 4g range */
556 ret = kmx61_set_range(data, KMX61_RANGE_4G);
557 if (ret < 0)
558 return ret;
560 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
561 if (ret < 0) {
562 dev_err(&data->client->dev, "Error reading reg_odcntl\n");
563 return ret;
565 data->odr_bits = ret;
568 * set output data rate for wake up (motion detection) function
569 * to match data rate for accelerometer sampling
571 ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
572 if (ret < 0)
573 return ret;
575 ret = kmx61_set_wake_up_odr(data, val, val2);
576 if (ret < 0)
577 return ret;
579 /* set acc/magn to OPERATION mode */
580 ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
581 if (ret < 0)
582 return ret;
584 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
585 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
587 return 0;
590 static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
591 bool status, u8 device)
593 u8 mode;
594 int ret;
596 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
597 if (ret < 0)
598 return ret;
600 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
601 if (ret < 0)
602 return ret;
604 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
605 if (ret < 0) {
606 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
607 return ret;
610 if (status) {
611 ret |= KMX61_REG_INC1_BIT_IEN;
612 if (device & KMX61_ACC)
613 ret |= KMX61_REG_INC1_BIT_DRDYA;
614 if (device & KMX61_MAG)
615 ret |= KMX61_REG_INC1_BIT_DRDYM;
616 } else {
617 ret &= ~KMX61_REG_INC1_BIT_IEN;
618 if (device & KMX61_ACC)
619 ret &= ~KMX61_REG_INC1_BIT_DRDYA;
620 if (device & KMX61_MAG)
621 ret &= ~KMX61_REG_INC1_BIT_DRDYM;
623 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
624 if (ret < 0) {
625 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
626 return ret;
629 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
630 if (ret < 0) {
631 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
632 return ret;
635 if (status)
636 ret |= KMX61_REG_CTRL1_BIT_DRDYE;
637 else
638 ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
640 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
641 if (ret < 0) {
642 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
643 return ret;
646 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
649 static int kmx61_chip_update_thresholds(struct kmx61_data *data)
651 int ret;
653 ret = i2c_smbus_write_byte_data(data->client,
654 KMX61_REG_WUF_TIMER,
655 data->wake_duration);
656 if (ret < 0) {
657 dev_err(&data->client->dev, "Errow writing reg_wuf_timer\n");
658 return ret;
661 ret = i2c_smbus_write_byte_data(data->client,
662 KMX61_REG_WUF_THRESH,
663 data->wake_thresh);
664 if (ret < 0)
665 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
667 return ret;
670 static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
671 bool status)
673 u8 mode;
674 int ret;
676 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
677 if (ret < 0)
678 return ret;
680 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
681 if (ret < 0)
682 return ret;
684 ret = kmx61_chip_update_thresholds(data);
685 if (ret < 0)
686 return ret;
688 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
689 if (ret < 0) {
690 dev_err(&data->client->dev, "Error reading reg_inc1\n");
691 return ret;
693 if (status)
694 ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
695 else
696 ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
698 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
699 if (ret < 0) {
700 dev_err(&data->client->dev, "Error writing reg_inc1\n");
701 return ret;
704 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
705 if (ret < 0) {
706 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
707 return ret;
710 if (status)
711 ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
712 else
713 ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
715 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
716 if (ret < 0) {
717 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
718 return ret;
720 mode |= KMX61_ACT_STBY_BIT;
721 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
725 * kmx61_set_power_state() - set power state for kmx61 @device
726 * @data - kmx61 device private pointer
727 * @on - power state to be set for @device
728 * @device - bitmask indicating device for which @on state needs to be set
730 * Notice that when ACC power state needs to be set to ON and MAG is in
731 * OPERATION then we know that kmx61_runtime_resume was already called
732 * so we must set ACC OPERATION mode here. The same happens when MAG power
733 * state needs to be set to ON and ACC is in OPERATION.
735 static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
737 #ifdef CONFIG_PM
738 int ret;
740 if (device & KMX61_ACC) {
741 if (on && !data->acc_ps && !data->mag_stby) {
742 ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
743 if (ret < 0)
744 return ret;
746 data->acc_ps = on;
748 if (device & KMX61_MAG) {
749 if (on && !data->mag_ps && !data->acc_stby) {
750 ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
751 if (ret < 0)
752 return ret;
754 data->mag_ps = on;
757 if (on) {
758 ret = pm_runtime_get_sync(&data->client->dev);
759 } else {
760 pm_runtime_mark_last_busy(&data->client->dev);
761 ret = pm_runtime_put_autosuspend(&data->client->dev);
763 if (ret < 0) {
764 dev_err(&data->client->dev,
765 "Failed: kmx61_set_power_state for %d, ret %d\n",
766 on, ret);
767 if (on)
768 pm_runtime_put_noidle(&data->client->dev);
770 return ret;
772 #endif
773 return 0;
776 static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
778 int ret;
779 u8 reg = base + offset * 2;
781 ret = i2c_smbus_read_word_data(data->client, reg);
782 if (ret < 0)
783 dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
785 return ret;
788 static int kmx61_read_raw(struct iio_dev *indio_dev,
789 struct iio_chan_spec const *chan, int *val,
790 int *val2, long mask)
792 int ret;
793 u8 base_reg;
794 struct kmx61_data *data = kmx61_get_data(indio_dev);
796 switch (mask) {
797 case IIO_CHAN_INFO_RAW:
798 switch (chan->type) {
799 case IIO_ACCEL:
800 base_reg = KMX61_ACC_XOUT_L;
801 break;
802 case IIO_MAGN:
803 base_reg = KMX61_MAG_XOUT_L;
804 break;
805 default:
806 return -EINVAL;
808 mutex_lock(&data->lock);
810 ret = kmx61_set_power_state(data, true, chan->address);
811 if (ret) {
812 mutex_unlock(&data->lock);
813 return ret;
816 ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
817 if (ret < 0) {
818 kmx61_set_power_state(data, false, chan->address);
819 mutex_unlock(&data->lock);
820 return ret;
822 *val = sign_extend32(ret >> chan->scan_type.shift,
823 chan->scan_type.realbits - 1);
824 ret = kmx61_set_power_state(data, false, chan->address);
826 mutex_unlock(&data->lock);
827 if (ret)
828 return ret;
829 return IIO_VAL_INT;
830 case IIO_CHAN_INFO_SCALE:
831 switch (chan->type) {
832 case IIO_ACCEL:
833 *val = 0;
834 *val2 = kmx61_uscale_table[data->range];
835 return IIO_VAL_INT_PLUS_MICRO;
836 case IIO_MAGN:
837 /* 14 bits res, 1465 microGauss per magn count */
838 *val = 0;
839 *val2 = 1465;
840 return IIO_VAL_INT_PLUS_MICRO;
841 default:
842 return -EINVAL;
844 case IIO_CHAN_INFO_SAMP_FREQ:
845 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
846 return -EINVAL;
848 mutex_lock(&data->lock);
849 ret = kmx61_get_odr(data, val, val2, chan->address);
850 mutex_unlock(&data->lock);
851 if (ret)
852 return -EINVAL;
853 return IIO_VAL_INT_PLUS_MICRO;
855 return -EINVAL;
858 static int kmx61_write_raw(struct iio_dev *indio_dev,
859 struct iio_chan_spec const *chan, int val,
860 int val2, long mask)
862 int ret;
863 struct kmx61_data *data = kmx61_get_data(indio_dev);
865 switch (mask) {
866 case IIO_CHAN_INFO_SAMP_FREQ:
867 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
868 return -EINVAL;
870 mutex_lock(&data->lock);
871 ret = kmx61_set_odr(data, val, val2, chan->address);
872 mutex_unlock(&data->lock);
873 return ret;
874 case IIO_CHAN_INFO_SCALE:
875 switch (chan->type) {
876 case IIO_ACCEL:
877 if (val != 0)
878 return -EINVAL;
879 mutex_lock(&data->lock);
880 ret = kmx61_set_scale(data, val2);
881 mutex_unlock(&data->lock);
882 return ret;
883 default:
884 return -EINVAL;
886 default:
887 return -EINVAL;
891 static int kmx61_read_event(struct iio_dev *indio_dev,
892 const struct iio_chan_spec *chan,
893 enum iio_event_type type,
894 enum iio_event_direction dir,
895 enum iio_event_info info,
896 int *val, int *val2)
898 struct kmx61_data *data = kmx61_get_data(indio_dev);
900 *val2 = 0;
901 switch (info) {
902 case IIO_EV_INFO_VALUE:
903 *val = data->wake_thresh;
904 return IIO_VAL_INT;
905 case IIO_EV_INFO_PERIOD:
906 *val = data->wake_duration;
907 return IIO_VAL_INT;
908 default:
909 return -EINVAL;
913 static int kmx61_write_event(struct iio_dev *indio_dev,
914 const struct iio_chan_spec *chan,
915 enum iio_event_type type,
916 enum iio_event_direction dir,
917 enum iio_event_info info,
918 int val, int val2)
920 struct kmx61_data *data = kmx61_get_data(indio_dev);
922 if (data->ev_enable_state)
923 return -EBUSY;
925 switch (info) {
926 case IIO_EV_INFO_VALUE:
927 data->wake_thresh = val;
928 return IIO_VAL_INT;
929 case IIO_EV_INFO_PERIOD:
930 data->wake_duration = val;
931 return IIO_VAL_INT;
932 default:
933 return -EINVAL;
937 static int kmx61_read_event_config(struct iio_dev *indio_dev,
938 const struct iio_chan_spec *chan,
939 enum iio_event_type type,
940 enum iio_event_direction dir)
942 struct kmx61_data *data = kmx61_get_data(indio_dev);
944 return data->ev_enable_state;
947 static int kmx61_write_event_config(struct iio_dev *indio_dev,
948 const struct iio_chan_spec *chan,
949 enum iio_event_type type,
950 enum iio_event_direction dir,
951 int state)
953 struct kmx61_data *data = kmx61_get_data(indio_dev);
954 int ret = 0;
956 if (state && data->ev_enable_state)
957 return 0;
959 mutex_lock(&data->lock);
961 if (!state && data->motion_trig_on) {
962 data->ev_enable_state = false;
963 goto err_unlock;
966 ret = kmx61_set_power_state(data, state, KMX61_ACC);
967 if (ret < 0)
968 goto err_unlock;
970 ret = kmx61_setup_any_motion_interrupt(data, state);
971 if (ret < 0) {
972 kmx61_set_power_state(data, false, KMX61_ACC);
973 goto err_unlock;
976 data->ev_enable_state = state;
978 err_unlock:
979 mutex_unlock(&data->lock);
981 return ret;
984 static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
985 struct iio_trigger *trig)
987 struct kmx61_data *data = kmx61_get_data(indio_dev);
989 if (data->acc_dready_trig != trig && data->motion_trig != trig)
990 return -EINVAL;
992 return 0;
995 static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
996 struct iio_trigger *trig)
998 struct kmx61_data *data = kmx61_get_data(indio_dev);
1000 if (data->mag_dready_trig != trig)
1001 return -EINVAL;
1003 return 0;
1006 static const struct iio_info kmx61_acc_info = {
1007 .driver_module = THIS_MODULE,
1008 .read_raw = kmx61_read_raw,
1009 .write_raw = kmx61_write_raw,
1010 .attrs = &kmx61_acc_attribute_group,
1011 .read_event_value = kmx61_read_event,
1012 .write_event_value = kmx61_write_event,
1013 .read_event_config = kmx61_read_event_config,
1014 .write_event_config = kmx61_write_event_config,
1015 .validate_trigger = kmx61_acc_validate_trigger,
1018 static const struct iio_info kmx61_mag_info = {
1019 .driver_module = THIS_MODULE,
1020 .read_raw = kmx61_read_raw,
1021 .write_raw = kmx61_write_raw,
1022 .attrs = &kmx61_mag_attribute_group,
1023 .validate_trigger = kmx61_mag_validate_trigger,
1027 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
1028 bool state)
1030 int ret = 0;
1031 u8 device;
1033 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1034 struct kmx61_data *data = kmx61_get_data(indio_dev);
1036 mutex_lock(&data->lock);
1038 if (!state && data->ev_enable_state && data->motion_trig_on) {
1039 data->motion_trig_on = false;
1040 goto err_unlock;
1043 if (data->acc_dready_trig == trig || data->motion_trig == trig)
1044 device = KMX61_ACC;
1045 else
1046 device = KMX61_MAG;
1048 ret = kmx61_set_power_state(data, state, device);
1049 if (ret < 0)
1050 goto err_unlock;
1052 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1053 ret = kmx61_setup_new_data_interrupt(data, state, device);
1054 else
1055 ret = kmx61_setup_any_motion_interrupt(data, state);
1056 if (ret < 0) {
1057 kmx61_set_power_state(data, false, device);
1058 goto err_unlock;
1061 if (data->acc_dready_trig == trig)
1062 data->acc_dready_trig_on = state;
1063 else if (data->mag_dready_trig == trig)
1064 data->mag_dready_trig_on = state;
1065 else
1066 data->motion_trig_on = state;
1067 err_unlock:
1068 mutex_unlock(&data->lock);
1070 return ret;
1073 static int kmx61_trig_try_reenable(struct iio_trigger *trig)
1075 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1076 struct kmx61_data *data = kmx61_get_data(indio_dev);
1077 int ret;
1079 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1080 if (ret < 0) {
1081 dev_err(&data->client->dev, "Error reading reg_inl\n");
1082 return ret;
1085 return 0;
1088 static const struct iio_trigger_ops kmx61_trigger_ops = {
1089 .set_trigger_state = kmx61_data_rdy_trigger_set_state,
1090 .try_reenable = kmx61_trig_try_reenable,
1091 .owner = THIS_MODULE,
1094 static irqreturn_t kmx61_event_handler(int irq, void *private)
1096 struct kmx61_data *data = private;
1097 struct iio_dev *indio_dev = data->acc_indio_dev;
1098 int ret;
1100 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1101 if (ret < 0) {
1102 dev_err(&data->client->dev, "Error reading reg_ins1\n");
1103 goto ack_intr;
1106 if (ret & KMX61_REG_INS1_BIT_WUFS) {
1107 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1108 if (ret < 0) {
1109 dev_err(&data->client->dev, "Error reading reg_ins2\n");
1110 goto ack_intr;
1113 if (ret & KMX61_REG_INS2_BIT_XN)
1114 iio_push_event(indio_dev,
1115 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1117 IIO_MOD_X,
1118 IIO_EV_TYPE_THRESH,
1119 IIO_EV_DIR_FALLING),
1122 if (ret & KMX61_REG_INS2_BIT_XP)
1123 iio_push_event(indio_dev,
1124 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1126 IIO_MOD_X,
1127 IIO_EV_TYPE_THRESH,
1128 IIO_EV_DIR_RISING),
1131 if (ret & KMX61_REG_INS2_BIT_YN)
1132 iio_push_event(indio_dev,
1133 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1135 IIO_MOD_Y,
1136 IIO_EV_TYPE_THRESH,
1137 IIO_EV_DIR_FALLING),
1140 if (ret & KMX61_REG_INS2_BIT_YP)
1141 iio_push_event(indio_dev,
1142 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1144 IIO_MOD_Y,
1145 IIO_EV_TYPE_THRESH,
1146 IIO_EV_DIR_RISING),
1149 if (ret & KMX61_REG_INS2_BIT_ZN)
1150 iio_push_event(indio_dev,
1151 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1153 IIO_MOD_Z,
1154 IIO_EV_TYPE_THRESH,
1155 IIO_EV_DIR_FALLING),
1158 if (ret & KMX61_REG_INS2_BIT_ZP)
1159 iio_push_event(indio_dev,
1160 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1162 IIO_MOD_Z,
1163 IIO_EV_TYPE_THRESH,
1164 IIO_EV_DIR_RISING),
1168 ack_intr:
1169 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1170 if (ret < 0)
1171 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1173 ret |= KMX61_REG_CTRL1_BIT_RES;
1174 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1175 if (ret < 0)
1176 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1178 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1179 if (ret < 0)
1180 dev_err(&data->client->dev, "Error reading reg_inl\n");
1182 return IRQ_HANDLED;
1185 static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1187 struct kmx61_data *data = private;
1189 if (data->acc_dready_trig_on)
1190 iio_trigger_poll(data->acc_dready_trig);
1191 if (data->mag_dready_trig_on)
1192 iio_trigger_poll(data->mag_dready_trig);
1194 if (data->motion_trig_on)
1195 iio_trigger_poll(data->motion_trig);
1197 if (data->ev_enable_state)
1198 return IRQ_WAKE_THREAD;
1199 return IRQ_HANDLED;
1202 static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1204 struct iio_poll_func *pf = p;
1205 struct iio_dev *indio_dev = pf->indio_dev;
1206 struct kmx61_data *data = kmx61_get_data(indio_dev);
1207 int bit, ret, i = 0;
1208 u8 base;
1209 s16 buffer[8];
1211 if (indio_dev == data->acc_indio_dev)
1212 base = KMX61_ACC_XOUT_L;
1213 else
1214 base = KMX61_MAG_XOUT_L;
1216 mutex_lock(&data->lock);
1217 for_each_set_bit(bit, indio_dev->active_scan_mask,
1218 indio_dev->masklength) {
1219 ret = kmx61_read_measurement(data, base, bit);
1220 if (ret < 0) {
1221 mutex_unlock(&data->lock);
1222 goto err;
1224 buffer[i++] = ret;
1226 mutex_unlock(&data->lock);
1228 iio_push_to_buffers(indio_dev, buffer);
1229 err:
1230 iio_trigger_notify_done(indio_dev->trig);
1232 return IRQ_HANDLED;
1235 static const char *kmx61_match_acpi_device(struct device *dev)
1237 const struct acpi_device_id *id;
1239 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1240 if (!id)
1241 return NULL;
1242 return dev_name(dev);
1245 static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1246 const struct iio_info *info,
1247 const struct iio_chan_spec *chan,
1248 int num_channels,
1249 const char *name)
1251 struct iio_dev *indio_dev;
1253 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1254 if (!indio_dev)
1255 return ERR_PTR(-ENOMEM);
1257 kmx61_set_data(indio_dev, data);
1259 indio_dev->dev.parent = &data->client->dev;
1260 indio_dev->channels = chan;
1261 indio_dev->num_channels = num_channels;
1262 indio_dev->name = name;
1263 indio_dev->modes = INDIO_DIRECT_MODE;
1264 indio_dev->info = info;
1266 return indio_dev;
1269 static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1270 struct iio_dev *indio_dev,
1271 const char *tag)
1273 struct iio_trigger *trig;
1274 int ret;
1276 trig = devm_iio_trigger_alloc(&data->client->dev,
1277 "%s-%s-dev%d",
1278 indio_dev->name,
1279 tag,
1280 indio_dev->id);
1281 if (!trig)
1282 return ERR_PTR(-ENOMEM);
1284 trig->dev.parent = &data->client->dev;
1285 trig->ops = &kmx61_trigger_ops;
1286 iio_trigger_set_drvdata(trig, indio_dev);
1288 ret = iio_trigger_register(trig);
1289 if (ret)
1290 return ERR_PTR(ret);
1292 return trig;
1295 static int kmx61_probe(struct i2c_client *client,
1296 const struct i2c_device_id *id)
1298 int ret;
1299 struct kmx61_data *data;
1300 const char *name = NULL;
1302 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1303 if (!data)
1304 return -ENOMEM;
1306 i2c_set_clientdata(client, data);
1307 data->client = client;
1309 mutex_init(&data->lock);
1311 if (id)
1312 name = id->name;
1313 else if (ACPI_HANDLE(&client->dev))
1314 name = kmx61_match_acpi_device(&client->dev);
1315 else
1316 return -ENODEV;
1318 data->acc_indio_dev =
1319 kmx61_indiodev_setup(data, &kmx61_acc_info,
1320 kmx61_acc_channels,
1321 ARRAY_SIZE(kmx61_acc_channels),
1322 name);
1323 if (IS_ERR(data->acc_indio_dev))
1324 return PTR_ERR(data->acc_indio_dev);
1326 data->mag_indio_dev =
1327 kmx61_indiodev_setup(data, &kmx61_mag_info,
1328 kmx61_mag_channels,
1329 ARRAY_SIZE(kmx61_mag_channels),
1330 name);
1331 if (IS_ERR(data->mag_indio_dev))
1332 return PTR_ERR(data->mag_indio_dev);
1334 ret = kmx61_chip_init(data);
1335 if (ret < 0)
1336 return ret;
1338 if (client->irq > 0) {
1339 ret = devm_request_threaded_irq(&client->dev, client->irq,
1340 kmx61_data_rdy_trig_poll,
1341 kmx61_event_handler,
1342 IRQF_TRIGGER_RISING,
1343 KMX61_IRQ_NAME,
1344 data);
1345 if (ret)
1346 goto err_chip_uninit;
1348 data->acc_dready_trig =
1349 kmx61_trigger_setup(data, data->acc_indio_dev,
1350 "dready");
1351 if (IS_ERR(data->acc_dready_trig)) {
1352 ret = PTR_ERR(data->acc_dready_trig);
1353 goto err_chip_uninit;
1356 data->mag_dready_trig =
1357 kmx61_trigger_setup(data, data->mag_indio_dev,
1358 "dready");
1359 if (IS_ERR(data->mag_dready_trig)) {
1360 ret = PTR_ERR(data->mag_dready_trig);
1361 goto err_trigger_unregister_acc_dready;
1364 data->motion_trig =
1365 kmx61_trigger_setup(data, data->acc_indio_dev,
1366 "any-motion");
1367 if (IS_ERR(data->motion_trig)) {
1368 ret = PTR_ERR(data->motion_trig);
1369 goto err_trigger_unregister_mag_dready;
1372 ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1373 &iio_pollfunc_store_time,
1374 kmx61_trigger_handler,
1375 NULL);
1376 if (ret < 0) {
1377 dev_err(&data->client->dev,
1378 "Failed to setup acc triggered buffer\n");
1379 goto err_trigger_unregister_motion;
1382 ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1383 &iio_pollfunc_store_time,
1384 kmx61_trigger_handler,
1385 NULL);
1386 if (ret < 0) {
1387 dev_err(&data->client->dev,
1388 "Failed to setup mag triggered buffer\n");
1389 goto err_buffer_cleanup_acc;
1393 ret = iio_device_register(data->acc_indio_dev);
1394 if (ret < 0) {
1395 dev_err(&client->dev, "Failed to register acc iio device\n");
1396 goto err_buffer_cleanup_mag;
1399 ret = iio_device_register(data->mag_indio_dev);
1400 if (ret < 0) {
1401 dev_err(&client->dev, "Failed to register mag iio device\n");
1402 goto err_iio_unregister_acc;
1405 ret = pm_runtime_set_active(&client->dev);
1406 if (ret < 0)
1407 goto err_iio_unregister_mag;
1409 pm_runtime_enable(&client->dev);
1410 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1411 pm_runtime_use_autosuspend(&client->dev);
1413 return 0;
1415 err_iio_unregister_mag:
1416 iio_device_unregister(data->mag_indio_dev);
1417 err_iio_unregister_acc:
1418 iio_device_unregister(data->acc_indio_dev);
1419 err_buffer_cleanup_mag:
1420 if (client->irq > 0)
1421 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1422 err_buffer_cleanup_acc:
1423 if (client->irq > 0)
1424 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1425 err_trigger_unregister_motion:
1426 iio_trigger_unregister(data->motion_trig);
1427 err_trigger_unregister_mag_dready:
1428 iio_trigger_unregister(data->mag_dready_trig);
1429 err_trigger_unregister_acc_dready:
1430 iio_trigger_unregister(data->acc_dready_trig);
1431 err_chip_uninit:
1432 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1433 return ret;
1436 static int kmx61_remove(struct i2c_client *client)
1438 struct kmx61_data *data = i2c_get_clientdata(client);
1440 pm_runtime_disable(&client->dev);
1441 pm_runtime_set_suspended(&client->dev);
1442 pm_runtime_put_noidle(&client->dev);
1444 iio_device_unregister(data->acc_indio_dev);
1445 iio_device_unregister(data->mag_indio_dev);
1447 if (client->irq > 0) {
1448 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1449 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1450 iio_trigger_unregister(data->acc_dready_trig);
1451 iio_trigger_unregister(data->mag_dready_trig);
1452 iio_trigger_unregister(data->motion_trig);
1455 mutex_lock(&data->lock);
1456 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1457 mutex_unlock(&data->lock);
1459 return 0;
1462 #ifdef CONFIG_PM_SLEEP
1463 static int kmx61_suspend(struct device *dev)
1465 int ret;
1466 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1468 mutex_lock(&data->lock);
1469 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1470 false);
1471 mutex_unlock(&data->lock);
1473 return ret;
1476 static int kmx61_resume(struct device *dev)
1478 u8 stby = 0;
1479 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1481 if (data->acc_stby)
1482 stby |= KMX61_ACC_STBY_BIT;
1483 if (data->mag_stby)
1484 stby |= KMX61_MAG_STBY_BIT;
1486 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1488 #endif
1490 #ifdef CONFIG_PM
1491 static int kmx61_runtime_suspend(struct device *dev)
1493 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1494 int ret;
1496 mutex_lock(&data->lock);
1497 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1498 mutex_unlock(&data->lock);
1500 return ret;
1503 static int kmx61_runtime_resume(struct device *dev)
1505 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1506 u8 stby = 0;
1508 if (!data->acc_ps)
1509 stby |= KMX61_ACC_STBY_BIT;
1510 if (!data->mag_ps)
1511 stby |= KMX61_MAG_STBY_BIT;
1513 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1515 #endif
1517 static const struct dev_pm_ops kmx61_pm_ops = {
1518 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
1519 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1522 static const struct acpi_device_id kmx61_acpi_match[] = {
1523 {"KMX61021", 0},
1527 MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
1529 static const struct i2c_device_id kmx61_id[] = {
1530 {"kmx611021", 0},
1534 MODULE_DEVICE_TABLE(i2c, kmx61_id);
1536 static struct i2c_driver kmx61_driver = {
1537 .driver = {
1538 .name = KMX61_DRV_NAME,
1539 .acpi_match_table = ACPI_PTR(kmx61_acpi_match),
1540 .pm = &kmx61_pm_ops,
1542 .probe = kmx61_probe,
1543 .remove = kmx61_remove,
1544 .id_table = kmx61_id,
1547 module_i2c_driver(kmx61_driver);
1549 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1550 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1551 MODULE_LICENSE("GPL v2");