Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / imu / kmx61.c
blob44b3f5397343072ebc4d1ee026993102d331dc7d
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/interrupt.h>
18 #include <linux/pm.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/events.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/iio/trigger_consumer.h>
28 #define KMX61_DRV_NAME "kmx61"
29 #define KMX61_IRQ_NAME "kmx61_event"
31 #define KMX61_REG_WHO_AM_I 0x00
32 #define KMX61_REG_INS1 0x01
33 #define KMX61_REG_INS2 0x02
36 * three 16-bit accelerometer output registers for X/Y/Z axis
37 * we use only XOUT_L as a base register, all other addresses
38 * can be obtained by applying an offset and are provided here
39 * only for clarity.
41 #define KMX61_ACC_XOUT_L 0x0A
42 #define KMX61_ACC_XOUT_H 0x0B
43 #define KMX61_ACC_YOUT_L 0x0C
44 #define KMX61_ACC_YOUT_H 0x0D
45 #define KMX61_ACC_ZOUT_L 0x0E
46 #define KMX61_ACC_ZOUT_H 0x0F
49 * one 16-bit temperature output register
51 #define KMX61_TEMP_L 0x10
52 #define KMX61_TEMP_H 0x11
55 * three 16-bit magnetometer output registers for X/Y/Z axis
57 #define KMX61_MAG_XOUT_L 0x12
58 #define KMX61_MAG_XOUT_H 0x13
59 #define KMX61_MAG_YOUT_L 0x14
60 #define KMX61_MAG_YOUT_H 0x15
61 #define KMX61_MAG_ZOUT_L 0x16
62 #define KMX61_MAG_ZOUT_H 0x17
64 #define KMX61_REG_INL 0x28
65 #define KMX61_REG_STBY 0x29
66 #define KMX61_REG_CTRL1 0x2A
67 #define KMX61_REG_CTRL2 0x2B
68 #define KMX61_REG_ODCNTL 0x2C
69 #define KMX61_REG_INC1 0x2D
71 #define KMX61_REG_WUF_THRESH 0x3D
72 #define KMX61_REG_WUF_TIMER 0x3E
74 #define KMX61_ACC_STBY_BIT BIT(0)
75 #define KMX61_MAG_STBY_BIT BIT(1)
76 #define KMX61_ACT_STBY_BIT BIT(7)
78 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
80 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
82 #define KMX61_REG_INS2_BIT_ZP BIT(0)
83 #define KMX61_REG_INS2_BIT_ZN BIT(1)
84 #define KMX61_REG_INS2_BIT_YP BIT(2)
85 #define KMX61_REG_INS2_BIT_YN BIT(3)
86 #define KMX61_REG_INS2_BIT_XP BIT(4)
87 #define KMX61_REG_INS2_BIT_XN BIT(5)
89 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
91 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
92 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
93 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
94 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
96 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
97 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
98 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
99 #define KMX61_REG_INC1_BIT_IEN BIT(5)
101 #define KMX61_ACC_ODR_SHIFT 0
102 #define KMX61_MAG_ODR_SHIFT 4
103 #define KMX61_ACC_ODR_MASK 0x0F
104 #define KMX61_MAG_ODR_MASK 0xF0
106 #define KMX61_OWUF_MASK 0x7
108 #define KMX61_DEFAULT_WAKE_THRESH 1
109 #define KMX61_DEFAULT_WAKE_DURATION 1
111 #define KMX61_SLEEP_DELAY_MS 2000
113 #define KMX61_CHIP_ID 0x12
115 /* KMX61 devices */
116 #define KMX61_ACC 0x01
117 #define KMX61_MAG 0x02
119 struct kmx61_data {
120 struct i2c_client *client;
122 /* serialize access to non-atomic ops, e.g set_mode */
123 struct mutex lock;
125 /* standby state */
126 bool acc_stby;
127 bool mag_stby;
129 /* power state */
130 bool acc_ps;
131 bool mag_ps;
133 /* config bits */
134 u8 range;
135 u8 odr_bits;
136 u8 wake_thresh;
137 u8 wake_duration;
139 /* accelerometer specific data */
140 struct iio_dev *acc_indio_dev;
141 struct iio_trigger *acc_dready_trig;
142 struct iio_trigger *motion_trig;
143 bool acc_dready_trig_on;
144 bool motion_trig_on;
145 bool ev_enable_state;
147 /* magnetometer specific data */
148 struct iio_dev *mag_indio_dev;
149 struct iio_trigger *mag_dready_trig;
150 bool mag_dready_trig_on;
153 enum kmx61_range {
154 KMX61_RANGE_2G,
155 KMX61_RANGE_4G,
156 KMX61_RANGE_8G,
159 enum kmx61_axis {
160 KMX61_AXIS_X,
161 KMX61_AXIS_Y,
162 KMX61_AXIS_Z,
165 static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
167 static const struct {
168 int val;
169 int val2;
170 } kmx61_samp_freq_table[] = { {12, 500000},
171 {25, 0},
172 {50, 0},
173 {100, 0},
174 {200, 0},
175 {400, 0},
176 {800, 0},
177 {1600, 0},
178 {0, 781000},
179 {1, 563000},
180 {3, 125000},
181 {6, 250000} };
183 static const struct {
184 int val;
185 int val2;
186 int odr_bits;
187 } kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
188 {1, 563000, 0x01},
189 {3, 125000, 0x02},
190 {6, 250000, 0x03},
191 {12, 500000, 0x04},
192 {25, 0, 0x05},
193 {50, 0, 0x06},
194 {100, 0, 0x06},
195 {200, 0, 0x06},
196 {400, 0, 0x06},
197 {800, 0, 0x06},
198 {1600, 0, 0x06} };
200 static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
201 static IIO_CONST_ATTR(magn_scale_available, "0.001465");
202 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
203 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
205 static struct attribute *kmx61_acc_attributes[] = {
206 &iio_const_attr_accel_scale_available.dev_attr.attr,
207 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
208 NULL,
211 static struct attribute *kmx61_mag_attributes[] = {
212 &iio_const_attr_magn_scale_available.dev_attr.attr,
213 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
214 NULL,
217 static const struct attribute_group kmx61_acc_attribute_group = {
218 .attrs = kmx61_acc_attributes,
221 static const struct attribute_group kmx61_mag_attribute_group = {
222 .attrs = kmx61_mag_attributes,
225 static const struct iio_event_spec kmx61_event = {
226 .type = IIO_EV_TYPE_THRESH,
227 .dir = IIO_EV_DIR_EITHER,
228 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
229 BIT(IIO_EV_INFO_ENABLE) |
230 BIT(IIO_EV_INFO_PERIOD),
233 #define KMX61_ACC_CHAN(_axis) { \
234 .type = IIO_ACCEL, \
235 .modified = 1, \
236 .channel2 = IIO_MOD_ ## _axis, \
237 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
238 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
239 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
240 .address = KMX61_ACC, \
241 .scan_index = KMX61_AXIS_ ## _axis, \
242 .scan_type = { \
243 .sign = 's', \
244 .realbits = 12, \
245 .storagebits = 16, \
246 .shift = 4, \
247 .endianness = IIO_LE, \
248 }, \
249 .event_spec = &kmx61_event, \
250 .num_event_specs = 1 \
253 #define KMX61_MAG_CHAN(_axis) { \
254 .type = IIO_MAGN, \
255 .modified = 1, \
256 .channel2 = IIO_MOD_ ## _axis, \
257 .address = KMX61_MAG, \
258 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
259 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
260 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
261 .scan_index = KMX61_AXIS_ ## _axis, \
262 .scan_type = { \
263 .sign = 's', \
264 .realbits = 14, \
265 .storagebits = 16, \
266 .shift = 2, \
267 .endianness = IIO_LE, \
268 }, \
271 static const struct iio_chan_spec kmx61_acc_channels[] = {
272 KMX61_ACC_CHAN(X),
273 KMX61_ACC_CHAN(Y),
274 KMX61_ACC_CHAN(Z),
277 static const struct iio_chan_spec kmx61_mag_channels[] = {
278 KMX61_MAG_CHAN(X),
279 KMX61_MAG_CHAN(Y),
280 KMX61_MAG_CHAN(Z),
283 static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
285 struct kmx61_data **priv = iio_priv(indio_dev);
287 *priv = data;
290 static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
292 return *(struct kmx61_data **)iio_priv(indio_dev);
295 static int kmx61_convert_freq_to_bit(int val, int val2)
297 int i;
299 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
300 if (val == kmx61_samp_freq_table[i].val &&
301 val2 == kmx61_samp_freq_table[i].val2)
302 return i;
303 return -EINVAL;
306 static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
308 int i;
310 for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
311 if (kmx61_wake_up_odr_table[i].val == val &&
312 kmx61_wake_up_odr_table[i].val2 == val2)
313 return kmx61_wake_up_odr_table[i].odr_bits;
314 return -EINVAL;
318 * kmx61_set_mode() - set KMX61 device operating mode
319 * @data - kmx61 device private data pointer
320 * @mode - bitmask, indicating operating mode for @device
321 * @device - bitmask, indicating device for which @mode needs to be set
322 * @update - update stby bits stored in device's private @data
324 * For each sensor (accelerometer/magnetometer) there are two operating modes
325 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
326 * if they are both enabled. Internal sensors state is saved in acc_stby and
327 * mag_stby members of driver's private @data.
329 static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
330 bool update)
332 int ret;
333 int acc_stby = -1, mag_stby = -1;
335 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
336 if (ret < 0) {
337 dev_err(&data->client->dev, "Error reading reg_stby\n");
338 return ret;
340 if (device & KMX61_ACC) {
341 if (mode & KMX61_ACC_STBY_BIT) {
342 ret |= KMX61_ACC_STBY_BIT;
343 acc_stby = 1;
344 } else {
345 ret &= ~KMX61_ACC_STBY_BIT;
346 acc_stby = 0;
350 if (device & KMX61_MAG) {
351 if (mode & KMX61_MAG_STBY_BIT) {
352 ret |= KMX61_MAG_STBY_BIT;
353 mag_stby = 1;
354 } else {
355 ret &= ~KMX61_MAG_STBY_BIT;
356 mag_stby = 0;
360 if (mode & KMX61_ACT_STBY_BIT)
361 ret |= KMX61_ACT_STBY_BIT;
363 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
364 if (ret < 0) {
365 dev_err(&data->client->dev, "Error writing reg_stby\n");
366 return ret;
369 if (acc_stby != -1 && update)
370 data->acc_stby = acc_stby;
371 if (mag_stby != -1 && update)
372 data->mag_stby = mag_stby;
374 return 0;
377 static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
379 int ret;
381 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
382 if (ret < 0) {
383 dev_err(&data->client->dev, "Error reading reg_stby\n");
384 return ret;
386 *mode = 0;
388 if (device & KMX61_ACC) {
389 if (ret & KMX61_ACC_STBY_BIT)
390 *mode |= KMX61_ACC_STBY_BIT;
391 else
392 *mode &= ~KMX61_ACC_STBY_BIT;
395 if (device & KMX61_MAG) {
396 if (ret & KMX61_MAG_STBY_BIT)
397 *mode |= KMX61_MAG_STBY_BIT;
398 else
399 *mode &= ~KMX61_MAG_STBY_BIT;
402 return 0;
405 static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
407 int ret, odr_bits;
409 odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
410 if (odr_bits < 0)
411 return odr_bits;
413 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
414 odr_bits);
415 if (ret < 0)
416 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
417 return ret;
420 static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
422 int ret;
423 u8 mode;
424 int lodr_bits, odr_bits;
426 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
427 if (ret < 0)
428 return ret;
430 lodr_bits = kmx61_convert_freq_to_bit(val, val2);
431 if (lodr_bits < 0)
432 return lodr_bits;
434 /* To change ODR, accel and magn must be in STDBY */
435 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
436 true);
437 if (ret < 0)
438 return ret;
440 odr_bits = 0;
441 if (device & KMX61_ACC)
442 odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
443 if (device & KMX61_MAG)
444 odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
446 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
447 odr_bits);
448 if (ret < 0)
449 return ret;
451 data->odr_bits = odr_bits;
453 if (device & KMX61_ACC) {
454 ret = kmx61_set_wake_up_odr(data, val, val2);
455 if (ret)
456 return ret;
459 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
462 static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
463 u8 device)
465 u8 lodr_bits;
467 if (device & KMX61_ACC)
468 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
469 KMX61_ACC_ODR_MASK;
470 else if (device & KMX61_MAG)
471 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
472 KMX61_MAG_ODR_MASK;
473 else
474 return -EINVAL;
476 if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
477 return -EINVAL;
479 *val = kmx61_samp_freq_table[lodr_bits].val;
480 *val2 = kmx61_samp_freq_table[lodr_bits].val2;
482 return 0;
485 static int kmx61_set_range(struct kmx61_data *data, u8 range)
487 int ret;
489 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
490 if (ret < 0) {
491 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
492 return ret;
495 ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
496 ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
498 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
499 if (ret < 0) {
500 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
501 return ret;
504 data->range = range;
506 return 0;
509 static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
511 int ret, i;
512 u8 mode;
514 for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
515 if (kmx61_uscale_table[i] == uscale) {
516 ret = kmx61_get_mode(data, &mode,
517 KMX61_ACC | KMX61_MAG);
518 if (ret < 0)
519 return ret;
521 ret = kmx61_set_mode(data, KMX61_ALL_STBY,
522 KMX61_ACC | KMX61_MAG, true);
523 if (ret < 0)
524 return ret;
526 ret = kmx61_set_range(data, i);
527 if (ret < 0)
528 return ret;
530 return kmx61_set_mode(data, mode,
531 KMX61_ACC | KMX61_MAG, true);
534 return -EINVAL;
537 static int kmx61_chip_init(struct kmx61_data *data)
539 int ret, val, val2;
541 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
542 if (ret < 0) {
543 dev_err(&data->client->dev, "Error reading who_am_i\n");
544 return ret;
547 if (ret != KMX61_CHIP_ID) {
548 dev_err(&data->client->dev,
549 "Wrong chip id, got %x expected %x\n",
550 ret, KMX61_CHIP_ID);
551 return -EINVAL;
554 /* set accel 12bit, 4g range */
555 ret = kmx61_set_range(data, KMX61_RANGE_4G);
556 if (ret < 0)
557 return ret;
559 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
560 if (ret < 0) {
561 dev_err(&data->client->dev, "Error reading reg_odcntl\n");
562 return ret;
564 data->odr_bits = ret;
567 * set output data rate for wake up (motion detection) function
568 * to match data rate for accelerometer sampling
570 ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
571 if (ret < 0)
572 return ret;
574 ret = kmx61_set_wake_up_odr(data, val, val2);
575 if (ret < 0)
576 return ret;
578 /* set acc/magn to OPERATION mode */
579 ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
580 if (ret < 0)
581 return ret;
583 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
584 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
586 return 0;
589 static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
590 bool status, u8 device)
592 u8 mode;
593 int ret;
595 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
596 if (ret < 0)
597 return ret;
599 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
600 if (ret < 0)
601 return ret;
603 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
604 if (ret < 0) {
605 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
606 return ret;
609 if (status) {
610 ret |= KMX61_REG_INC1_BIT_IEN;
611 if (device & KMX61_ACC)
612 ret |= KMX61_REG_INC1_BIT_DRDYA;
613 if (device & KMX61_MAG)
614 ret |= KMX61_REG_INC1_BIT_DRDYM;
615 } else {
616 ret &= ~KMX61_REG_INC1_BIT_IEN;
617 if (device & KMX61_ACC)
618 ret &= ~KMX61_REG_INC1_BIT_DRDYA;
619 if (device & KMX61_MAG)
620 ret &= ~KMX61_REG_INC1_BIT_DRDYM;
622 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
623 if (ret < 0) {
624 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
625 return ret;
628 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
629 if (ret < 0) {
630 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
631 return ret;
634 if (status)
635 ret |= KMX61_REG_CTRL1_BIT_DRDYE;
636 else
637 ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
639 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
640 if (ret < 0) {
641 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
642 return ret;
645 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
648 static int kmx61_chip_update_thresholds(struct kmx61_data *data)
650 int ret;
652 ret = i2c_smbus_write_byte_data(data->client,
653 KMX61_REG_WUF_TIMER,
654 data->wake_duration);
655 if (ret < 0) {
656 dev_err(&data->client->dev, "Errow writing reg_wuf_timer\n");
657 return ret;
660 ret = i2c_smbus_write_byte_data(data->client,
661 KMX61_REG_WUF_THRESH,
662 data->wake_thresh);
663 if (ret < 0)
664 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
666 return ret;
669 static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
670 bool status)
672 u8 mode;
673 int ret;
675 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
676 if (ret < 0)
677 return ret;
679 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
680 if (ret < 0)
681 return ret;
683 ret = kmx61_chip_update_thresholds(data);
684 if (ret < 0)
685 return ret;
687 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
688 if (ret < 0) {
689 dev_err(&data->client->dev, "Error reading reg_inc1\n");
690 return ret;
692 if (status)
693 ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
694 else
695 ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
697 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
698 if (ret < 0) {
699 dev_err(&data->client->dev, "Error writing reg_inc1\n");
700 return ret;
703 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
704 if (ret < 0) {
705 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
706 return ret;
709 if (status)
710 ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
711 else
712 ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
714 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
715 if (ret < 0) {
716 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
717 return ret;
719 mode |= KMX61_ACT_STBY_BIT;
720 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
724 * kmx61_set_power_state() - set power state for kmx61 @device
725 * @data - kmx61 device private pointer
726 * @on - power state to be set for @device
727 * @device - bitmask indicating device for which @on state needs to be set
729 * Notice that when ACC power state needs to be set to ON and MAG is in
730 * OPERATION then we know that kmx61_runtime_resume was already called
731 * so we must set ACC OPERATION mode here. The same happens when MAG power
732 * state needs to be set to ON and ACC is in OPERATION.
734 static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
736 #ifdef CONFIG_PM
737 int ret;
739 if (device & KMX61_ACC) {
740 if (on && !data->acc_ps && !data->mag_stby) {
741 ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
742 if (ret < 0)
743 return ret;
745 data->acc_ps = on;
747 if (device & KMX61_MAG) {
748 if (on && !data->mag_ps && !data->acc_stby) {
749 ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
750 if (ret < 0)
751 return ret;
753 data->mag_ps = on;
756 if (on) {
757 ret = pm_runtime_get_sync(&data->client->dev);
758 } else {
759 pm_runtime_mark_last_busy(&data->client->dev);
760 ret = pm_runtime_put_autosuspend(&data->client->dev);
762 if (ret < 0) {
763 dev_err(&data->client->dev,
764 "Failed: kmx61_set_power_state for %d, ret %d\n",
765 on, ret);
766 if (on)
767 pm_runtime_put_noidle(&data->client->dev);
769 return ret;
771 #endif
772 return 0;
775 static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
777 int ret;
778 u8 reg = base + offset * 2;
780 ret = i2c_smbus_read_word_data(data->client, reg);
781 if (ret < 0)
782 dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
784 return ret;
787 static int kmx61_read_raw(struct iio_dev *indio_dev,
788 struct iio_chan_spec const *chan, int *val,
789 int *val2, long mask)
791 int ret;
792 u8 base_reg;
793 struct kmx61_data *data = kmx61_get_data(indio_dev);
795 switch (mask) {
796 case IIO_CHAN_INFO_RAW:
797 switch (chan->type) {
798 case IIO_ACCEL:
799 base_reg = KMX61_ACC_XOUT_L;
800 break;
801 case IIO_MAGN:
802 base_reg = KMX61_MAG_XOUT_L;
803 break;
804 default:
805 return -EINVAL;
807 mutex_lock(&data->lock);
809 ret = kmx61_set_power_state(data, true, chan->address);
810 if (ret) {
811 mutex_unlock(&data->lock);
812 return ret;
815 ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
816 if (ret < 0) {
817 kmx61_set_power_state(data, false, chan->address);
818 mutex_unlock(&data->lock);
819 return ret;
821 *val = sign_extend32(ret >> chan->scan_type.shift,
822 chan->scan_type.realbits - 1);
823 ret = kmx61_set_power_state(data, false, chan->address);
825 mutex_unlock(&data->lock);
826 if (ret)
827 return ret;
828 return IIO_VAL_INT;
829 case IIO_CHAN_INFO_SCALE:
830 switch (chan->type) {
831 case IIO_ACCEL:
832 *val = 0;
833 *val2 = kmx61_uscale_table[data->range];
834 return IIO_VAL_INT_PLUS_MICRO;
835 case IIO_MAGN:
836 /* 14 bits res, 1465 microGauss per magn count */
837 *val = 0;
838 *val2 = 1465;
839 return IIO_VAL_INT_PLUS_MICRO;
840 default:
841 return -EINVAL;
843 case IIO_CHAN_INFO_SAMP_FREQ:
844 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
845 return -EINVAL;
847 mutex_lock(&data->lock);
848 ret = kmx61_get_odr(data, val, val2, chan->address);
849 mutex_unlock(&data->lock);
850 if (ret)
851 return -EINVAL;
852 return IIO_VAL_INT_PLUS_MICRO;
854 return -EINVAL;
857 static int kmx61_write_raw(struct iio_dev *indio_dev,
858 struct iio_chan_spec const *chan, int val,
859 int val2, long mask)
861 int ret;
862 struct kmx61_data *data = kmx61_get_data(indio_dev);
864 switch (mask) {
865 case IIO_CHAN_INFO_SAMP_FREQ:
866 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
867 return -EINVAL;
869 mutex_lock(&data->lock);
870 ret = kmx61_set_odr(data, val, val2, chan->address);
871 mutex_unlock(&data->lock);
872 return ret;
873 case IIO_CHAN_INFO_SCALE:
874 switch (chan->type) {
875 case IIO_ACCEL:
876 if (val != 0)
877 return -EINVAL;
878 mutex_lock(&data->lock);
879 ret = kmx61_set_scale(data, val2);
880 mutex_unlock(&data->lock);
881 return ret;
882 default:
883 return -EINVAL;
885 default:
886 return -EINVAL;
890 static int kmx61_read_event(struct iio_dev *indio_dev,
891 const struct iio_chan_spec *chan,
892 enum iio_event_type type,
893 enum iio_event_direction dir,
894 enum iio_event_info info,
895 int *val, int *val2)
897 struct kmx61_data *data = kmx61_get_data(indio_dev);
899 *val2 = 0;
900 switch (info) {
901 case IIO_EV_INFO_VALUE:
902 *val = data->wake_thresh;
903 return IIO_VAL_INT;
904 case IIO_EV_INFO_PERIOD:
905 *val = data->wake_duration;
906 return IIO_VAL_INT;
907 default:
908 return -EINVAL;
912 static int kmx61_write_event(struct iio_dev *indio_dev,
913 const struct iio_chan_spec *chan,
914 enum iio_event_type type,
915 enum iio_event_direction dir,
916 enum iio_event_info info,
917 int val, int val2)
919 struct kmx61_data *data = kmx61_get_data(indio_dev);
921 if (data->ev_enable_state)
922 return -EBUSY;
924 switch (info) {
925 case IIO_EV_INFO_VALUE:
926 data->wake_thresh = val;
927 return IIO_VAL_INT;
928 case IIO_EV_INFO_PERIOD:
929 data->wake_duration = val;
930 return IIO_VAL_INT;
931 default:
932 return -EINVAL;
936 static int kmx61_read_event_config(struct iio_dev *indio_dev,
937 const struct iio_chan_spec *chan,
938 enum iio_event_type type,
939 enum iio_event_direction dir)
941 struct kmx61_data *data = kmx61_get_data(indio_dev);
943 return data->ev_enable_state;
946 static int kmx61_write_event_config(struct iio_dev *indio_dev,
947 const struct iio_chan_spec *chan,
948 enum iio_event_type type,
949 enum iio_event_direction dir,
950 int state)
952 struct kmx61_data *data = kmx61_get_data(indio_dev);
953 int ret = 0;
955 if (state && data->ev_enable_state)
956 return 0;
958 mutex_lock(&data->lock);
960 if (!state && data->motion_trig_on) {
961 data->ev_enable_state = false;
962 goto err_unlock;
965 ret = kmx61_set_power_state(data, state, KMX61_ACC);
966 if (ret < 0)
967 goto err_unlock;
969 ret = kmx61_setup_any_motion_interrupt(data, state);
970 if (ret < 0) {
971 kmx61_set_power_state(data, false, KMX61_ACC);
972 goto err_unlock;
975 data->ev_enable_state = state;
977 err_unlock:
978 mutex_unlock(&data->lock);
980 return ret;
983 static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
984 struct iio_trigger *trig)
986 struct kmx61_data *data = kmx61_get_data(indio_dev);
988 if (data->acc_dready_trig != trig && data->motion_trig != trig)
989 return -EINVAL;
991 return 0;
994 static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
995 struct iio_trigger *trig)
997 struct kmx61_data *data = kmx61_get_data(indio_dev);
999 if (data->mag_dready_trig != trig)
1000 return -EINVAL;
1002 return 0;
1005 static const struct iio_info kmx61_acc_info = {
1006 .read_raw = kmx61_read_raw,
1007 .write_raw = kmx61_write_raw,
1008 .attrs = &kmx61_acc_attribute_group,
1009 .read_event_value = kmx61_read_event,
1010 .write_event_value = kmx61_write_event,
1011 .read_event_config = kmx61_read_event_config,
1012 .write_event_config = kmx61_write_event_config,
1013 .validate_trigger = kmx61_acc_validate_trigger,
1016 static const struct iio_info kmx61_mag_info = {
1017 .read_raw = kmx61_read_raw,
1018 .write_raw = kmx61_write_raw,
1019 .attrs = &kmx61_mag_attribute_group,
1020 .validate_trigger = kmx61_mag_validate_trigger,
1024 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
1025 bool state)
1027 int ret = 0;
1028 u8 device;
1030 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1031 struct kmx61_data *data = kmx61_get_data(indio_dev);
1033 mutex_lock(&data->lock);
1035 if (!state && data->ev_enable_state && data->motion_trig_on) {
1036 data->motion_trig_on = false;
1037 goto err_unlock;
1040 if (data->acc_dready_trig == trig || data->motion_trig == trig)
1041 device = KMX61_ACC;
1042 else
1043 device = KMX61_MAG;
1045 ret = kmx61_set_power_state(data, state, device);
1046 if (ret < 0)
1047 goto err_unlock;
1049 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1050 ret = kmx61_setup_new_data_interrupt(data, state, device);
1051 else
1052 ret = kmx61_setup_any_motion_interrupt(data, state);
1053 if (ret < 0) {
1054 kmx61_set_power_state(data, false, device);
1055 goto err_unlock;
1058 if (data->acc_dready_trig == trig)
1059 data->acc_dready_trig_on = state;
1060 else if (data->mag_dready_trig == trig)
1061 data->mag_dready_trig_on = state;
1062 else
1063 data->motion_trig_on = state;
1064 err_unlock:
1065 mutex_unlock(&data->lock);
1067 return ret;
1070 static int kmx61_trig_try_reenable(struct iio_trigger *trig)
1072 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1073 struct kmx61_data *data = kmx61_get_data(indio_dev);
1074 int ret;
1076 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1077 if (ret < 0) {
1078 dev_err(&data->client->dev, "Error reading reg_inl\n");
1079 return ret;
1082 return 0;
1085 static const struct iio_trigger_ops kmx61_trigger_ops = {
1086 .set_trigger_state = kmx61_data_rdy_trigger_set_state,
1087 .try_reenable = kmx61_trig_try_reenable,
1090 static irqreturn_t kmx61_event_handler(int irq, void *private)
1092 struct kmx61_data *data = private;
1093 struct iio_dev *indio_dev = data->acc_indio_dev;
1094 int ret;
1096 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1097 if (ret < 0) {
1098 dev_err(&data->client->dev, "Error reading reg_ins1\n");
1099 goto ack_intr;
1102 if (ret & KMX61_REG_INS1_BIT_WUFS) {
1103 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1104 if (ret < 0) {
1105 dev_err(&data->client->dev, "Error reading reg_ins2\n");
1106 goto ack_intr;
1109 if (ret & KMX61_REG_INS2_BIT_XN)
1110 iio_push_event(indio_dev,
1111 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1113 IIO_MOD_X,
1114 IIO_EV_TYPE_THRESH,
1115 IIO_EV_DIR_FALLING),
1118 if (ret & KMX61_REG_INS2_BIT_XP)
1119 iio_push_event(indio_dev,
1120 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1122 IIO_MOD_X,
1123 IIO_EV_TYPE_THRESH,
1124 IIO_EV_DIR_RISING),
1127 if (ret & KMX61_REG_INS2_BIT_YN)
1128 iio_push_event(indio_dev,
1129 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1131 IIO_MOD_Y,
1132 IIO_EV_TYPE_THRESH,
1133 IIO_EV_DIR_FALLING),
1136 if (ret & KMX61_REG_INS2_BIT_YP)
1137 iio_push_event(indio_dev,
1138 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1140 IIO_MOD_Y,
1141 IIO_EV_TYPE_THRESH,
1142 IIO_EV_DIR_RISING),
1145 if (ret & KMX61_REG_INS2_BIT_ZN)
1146 iio_push_event(indio_dev,
1147 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1149 IIO_MOD_Z,
1150 IIO_EV_TYPE_THRESH,
1151 IIO_EV_DIR_FALLING),
1154 if (ret & KMX61_REG_INS2_BIT_ZP)
1155 iio_push_event(indio_dev,
1156 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1158 IIO_MOD_Z,
1159 IIO_EV_TYPE_THRESH,
1160 IIO_EV_DIR_RISING),
1164 ack_intr:
1165 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1166 if (ret < 0)
1167 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1169 ret |= KMX61_REG_CTRL1_BIT_RES;
1170 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1171 if (ret < 0)
1172 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1174 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1175 if (ret < 0)
1176 dev_err(&data->client->dev, "Error reading reg_inl\n");
1178 return IRQ_HANDLED;
1181 static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1183 struct kmx61_data *data = private;
1185 if (data->acc_dready_trig_on)
1186 iio_trigger_poll(data->acc_dready_trig);
1187 if (data->mag_dready_trig_on)
1188 iio_trigger_poll(data->mag_dready_trig);
1190 if (data->motion_trig_on)
1191 iio_trigger_poll(data->motion_trig);
1193 if (data->ev_enable_state)
1194 return IRQ_WAKE_THREAD;
1195 return IRQ_HANDLED;
1198 static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1200 struct iio_poll_func *pf = p;
1201 struct iio_dev *indio_dev = pf->indio_dev;
1202 struct kmx61_data *data = kmx61_get_data(indio_dev);
1203 int bit, ret, i = 0;
1204 u8 base;
1205 s16 buffer[8];
1207 if (indio_dev == data->acc_indio_dev)
1208 base = KMX61_ACC_XOUT_L;
1209 else
1210 base = KMX61_MAG_XOUT_L;
1212 mutex_lock(&data->lock);
1213 for_each_set_bit(bit, indio_dev->active_scan_mask,
1214 indio_dev->masklength) {
1215 ret = kmx61_read_measurement(data, base, bit);
1216 if (ret < 0) {
1217 mutex_unlock(&data->lock);
1218 goto err;
1220 buffer[i++] = ret;
1222 mutex_unlock(&data->lock);
1224 iio_push_to_buffers(indio_dev, buffer);
1225 err:
1226 iio_trigger_notify_done(indio_dev->trig);
1228 return IRQ_HANDLED;
1231 static const char *kmx61_match_acpi_device(struct device *dev)
1233 const struct acpi_device_id *id;
1235 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1236 if (!id)
1237 return NULL;
1238 return dev_name(dev);
1241 static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1242 const struct iio_info *info,
1243 const struct iio_chan_spec *chan,
1244 int num_channels,
1245 const char *name)
1247 struct iio_dev *indio_dev;
1249 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1250 if (!indio_dev)
1251 return ERR_PTR(-ENOMEM);
1253 kmx61_set_data(indio_dev, data);
1255 indio_dev->dev.parent = &data->client->dev;
1256 indio_dev->channels = chan;
1257 indio_dev->num_channels = num_channels;
1258 indio_dev->name = name;
1259 indio_dev->modes = INDIO_DIRECT_MODE;
1260 indio_dev->info = info;
1262 return indio_dev;
1265 static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1266 struct iio_dev *indio_dev,
1267 const char *tag)
1269 struct iio_trigger *trig;
1270 int ret;
1272 trig = devm_iio_trigger_alloc(&data->client->dev,
1273 "%s-%s-dev%d",
1274 indio_dev->name,
1275 tag,
1276 indio_dev->id);
1277 if (!trig)
1278 return ERR_PTR(-ENOMEM);
1280 trig->dev.parent = &data->client->dev;
1281 trig->ops = &kmx61_trigger_ops;
1282 iio_trigger_set_drvdata(trig, indio_dev);
1284 ret = iio_trigger_register(trig);
1285 if (ret)
1286 return ERR_PTR(ret);
1288 return trig;
1291 static int kmx61_probe(struct i2c_client *client,
1292 const struct i2c_device_id *id)
1294 int ret;
1295 struct kmx61_data *data;
1296 const char *name = NULL;
1298 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1299 if (!data)
1300 return -ENOMEM;
1302 i2c_set_clientdata(client, data);
1303 data->client = client;
1305 mutex_init(&data->lock);
1307 if (id)
1308 name = id->name;
1309 else if (ACPI_HANDLE(&client->dev))
1310 name = kmx61_match_acpi_device(&client->dev);
1311 else
1312 return -ENODEV;
1314 data->acc_indio_dev =
1315 kmx61_indiodev_setup(data, &kmx61_acc_info,
1316 kmx61_acc_channels,
1317 ARRAY_SIZE(kmx61_acc_channels),
1318 name);
1319 if (IS_ERR(data->acc_indio_dev))
1320 return PTR_ERR(data->acc_indio_dev);
1322 data->mag_indio_dev =
1323 kmx61_indiodev_setup(data, &kmx61_mag_info,
1324 kmx61_mag_channels,
1325 ARRAY_SIZE(kmx61_mag_channels),
1326 name);
1327 if (IS_ERR(data->mag_indio_dev))
1328 return PTR_ERR(data->mag_indio_dev);
1330 ret = kmx61_chip_init(data);
1331 if (ret < 0)
1332 return ret;
1334 if (client->irq > 0) {
1335 ret = devm_request_threaded_irq(&client->dev, client->irq,
1336 kmx61_data_rdy_trig_poll,
1337 kmx61_event_handler,
1338 IRQF_TRIGGER_RISING,
1339 KMX61_IRQ_NAME,
1340 data);
1341 if (ret)
1342 goto err_chip_uninit;
1344 data->acc_dready_trig =
1345 kmx61_trigger_setup(data, data->acc_indio_dev,
1346 "dready");
1347 if (IS_ERR(data->acc_dready_trig)) {
1348 ret = PTR_ERR(data->acc_dready_trig);
1349 goto err_chip_uninit;
1352 data->mag_dready_trig =
1353 kmx61_trigger_setup(data, data->mag_indio_dev,
1354 "dready");
1355 if (IS_ERR(data->mag_dready_trig)) {
1356 ret = PTR_ERR(data->mag_dready_trig);
1357 goto err_trigger_unregister_acc_dready;
1360 data->motion_trig =
1361 kmx61_trigger_setup(data, data->acc_indio_dev,
1362 "any-motion");
1363 if (IS_ERR(data->motion_trig)) {
1364 ret = PTR_ERR(data->motion_trig);
1365 goto err_trigger_unregister_mag_dready;
1368 ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1369 &iio_pollfunc_store_time,
1370 kmx61_trigger_handler,
1371 NULL);
1372 if (ret < 0) {
1373 dev_err(&data->client->dev,
1374 "Failed to setup acc triggered buffer\n");
1375 goto err_trigger_unregister_motion;
1378 ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1379 &iio_pollfunc_store_time,
1380 kmx61_trigger_handler,
1381 NULL);
1382 if (ret < 0) {
1383 dev_err(&data->client->dev,
1384 "Failed to setup mag triggered buffer\n");
1385 goto err_buffer_cleanup_acc;
1389 ret = pm_runtime_set_active(&client->dev);
1390 if (ret < 0)
1391 goto err_buffer_cleanup_mag;
1393 pm_runtime_enable(&client->dev);
1394 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1395 pm_runtime_use_autosuspend(&client->dev);
1397 ret = iio_device_register(data->acc_indio_dev);
1398 if (ret < 0) {
1399 dev_err(&client->dev, "Failed to register acc iio device\n");
1400 goto err_buffer_cleanup_mag;
1403 ret = iio_device_register(data->mag_indio_dev);
1404 if (ret < 0) {
1405 dev_err(&client->dev, "Failed to register mag iio device\n");
1406 goto err_iio_unregister_acc;
1409 return 0;
1411 err_iio_unregister_acc:
1412 iio_device_unregister(data->acc_indio_dev);
1413 err_buffer_cleanup_mag:
1414 if (client->irq > 0)
1415 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1416 err_buffer_cleanup_acc:
1417 if (client->irq > 0)
1418 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1419 err_trigger_unregister_motion:
1420 iio_trigger_unregister(data->motion_trig);
1421 err_trigger_unregister_mag_dready:
1422 iio_trigger_unregister(data->mag_dready_trig);
1423 err_trigger_unregister_acc_dready:
1424 iio_trigger_unregister(data->acc_dready_trig);
1425 err_chip_uninit:
1426 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1427 return ret;
1430 static int kmx61_remove(struct i2c_client *client)
1432 struct kmx61_data *data = i2c_get_clientdata(client);
1434 iio_device_unregister(data->acc_indio_dev);
1435 iio_device_unregister(data->mag_indio_dev);
1437 pm_runtime_disable(&client->dev);
1438 pm_runtime_set_suspended(&client->dev);
1439 pm_runtime_put_noidle(&client->dev);
1441 if (client->irq > 0) {
1442 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1443 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1444 iio_trigger_unregister(data->acc_dready_trig);
1445 iio_trigger_unregister(data->mag_dready_trig);
1446 iio_trigger_unregister(data->motion_trig);
1449 mutex_lock(&data->lock);
1450 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1451 mutex_unlock(&data->lock);
1453 return 0;
1456 #ifdef CONFIG_PM_SLEEP
1457 static int kmx61_suspend(struct device *dev)
1459 int ret;
1460 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1462 mutex_lock(&data->lock);
1463 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1464 false);
1465 mutex_unlock(&data->lock);
1467 return ret;
1470 static int kmx61_resume(struct device *dev)
1472 u8 stby = 0;
1473 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1475 if (data->acc_stby)
1476 stby |= KMX61_ACC_STBY_BIT;
1477 if (data->mag_stby)
1478 stby |= KMX61_MAG_STBY_BIT;
1480 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1482 #endif
1484 #ifdef CONFIG_PM
1485 static int kmx61_runtime_suspend(struct device *dev)
1487 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1488 int ret;
1490 mutex_lock(&data->lock);
1491 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1492 mutex_unlock(&data->lock);
1494 return ret;
1497 static int kmx61_runtime_resume(struct device *dev)
1499 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1500 u8 stby = 0;
1502 if (!data->acc_ps)
1503 stby |= KMX61_ACC_STBY_BIT;
1504 if (!data->mag_ps)
1505 stby |= KMX61_MAG_STBY_BIT;
1507 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1509 #endif
1511 static const struct dev_pm_ops kmx61_pm_ops = {
1512 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
1513 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1516 static const struct acpi_device_id kmx61_acpi_match[] = {
1517 {"KMX61021", 0},
1521 MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
1523 static const struct i2c_device_id kmx61_id[] = {
1524 {"kmx611021", 0},
1528 MODULE_DEVICE_TABLE(i2c, kmx61_id);
1530 static struct i2c_driver kmx61_driver = {
1531 .driver = {
1532 .name = KMX61_DRV_NAME,
1533 .acpi_match_table = ACPI_PTR(kmx61_acpi_match),
1534 .pm = &kmx61_pm_ops,
1536 .probe = kmx61_probe,
1537 .remove = kmx61_remove,
1538 .id_table = kmx61_id,
1541 module_i2c_driver(kmx61_driver);
1543 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1544 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1545 MODULE_LICENSE("GPL v2");