drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / imu / kmx61.c
blob324c38764656ad74e2bbdc54824b4321db3c0b81
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
5 * Copyright (c) 2014, Intel Corporation.
7 * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
8 */
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/pm.h>
15 #include <linux/pm_runtime.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/events.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/trigger_consumer.h>
25 #define KMX61_DRV_NAME "kmx61"
26 #define KMX61_IRQ_NAME "kmx61_event"
28 #define KMX61_REG_WHO_AM_I 0x00
29 #define KMX61_REG_INS1 0x01
30 #define KMX61_REG_INS2 0x02
33 * three 16-bit accelerometer output registers for X/Y/Z axis
34 * we use only XOUT_L as a base register, all other addresses
35 * can be obtained by applying an offset and are provided here
36 * only for clarity.
38 #define KMX61_ACC_XOUT_L 0x0A
39 #define KMX61_ACC_XOUT_H 0x0B
40 #define KMX61_ACC_YOUT_L 0x0C
41 #define KMX61_ACC_YOUT_H 0x0D
42 #define KMX61_ACC_ZOUT_L 0x0E
43 #define KMX61_ACC_ZOUT_H 0x0F
46 * one 16-bit temperature output register
48 #define KMX61_TEMP_L 0x10
49 #define KMX61_TEMP_H 0x11
52 * three 16-bit magnetometer output registers for X/Y/Z axis
54 #define KMX61_MAG_XOUT_L 0x12
55 #define KMX61_MAG_XOUT_H 0x13
56 #define KMX61_MAG_YOUT_L 0x14
57 #define KMX61_MAG_YOUT_H 0x15
58 #define KMX61_MAG_ZOUT_L 0x16
59 #define KMX61_MAG_ZOUT_H 0x17
61 #define KMX61_REG_INL 0x28
62 #define KMX61_REG_STBY 0x29
63 #define KMX61_REG_CTRL1 0x2A
64 #define KMX61_REG_CTRL2 0x2B
65 #define KMX61_REG_ODCNTL 0x2C
66 #define KMX61_REG_INC1 0x2D
68 #define KMX61_REG_WUF_THRESH 0x3D
69 #define KMX61_REG_WUF_TIMER 0x3E
71 #define KMX61_ACC_STBY_BIT BIT(0)
72 #define KMX61_MAG_STBY_BIT BIT(1)
73 #define KMX61_ACT_STBY_BIT BIT(7)
75 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
77 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
79 #define KMX61_REG_INS2_BIT_ZP BIT(0)
80 #define KMX61_REG_INS2_BIT_ZN BIT(1)
81 #define KMX61_REG_INS2_BIT_YP BIT(2)
82 #define KMX61_REG_INS2_BIT_YN BIT(3)
83 #define KMX61_REG_INS2_BIT_XP BIT(4)
84 #define KMX61_REG_INS2_BIT_XN BIT(5)
86 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
88 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
89 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
90 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
91 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
93 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
94 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
95 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
96 #define KMX61_REG_INC1_BIT_IEN BIT(5)
98 #define KMX61_ACC_ODR_SHIFT 0
99 #define KMX61_MAG_ODR_SHIFT 4
100 #define KMX61_ACC_ODR_MASK 0x0F
101 #define KMX61_MAG_ODR_MASK 0xF0
103 #define KMX61_OWUF_MASK 0x7
105 #define KMX61_DEFAULT_WAKE_THRESH 1
106 #define KMX61_DEFAULT_WAKE_DURATION 1
108 #define KMX61_SLEEP_DELAY_MS 2000
110 #define KMX61_CHIP_ID 0x12
112 /* KMX61 devices */
113 #define KMX61_ACC 0x01
114 #define KMX61_MAG 0x02
116 struct kmx61_data {
117 struct i2c_client *client;
119 /* serialize access to non-atomic ops, e.g set_mode */
120 struct mutex lock;
122 /* standby state */
123 bool acc_stby;
124 bool mag_stby;
126 /* power state */
127 bool acc_ps;
128 bool mag_ps;
130 /* config bits */
131 u8 range;
132 u8 odr_bits;
133 u8 wake_thresh;
134 u8 wake_duration;
136 /* accelerometer specific data */
137 struct iio_dev *acc_indio_dev;
138 struct iio_trigger *acc_dready_trig;
139 struct iio_trigger *motion_trig;
140 bool acc_dready_trig_on;
141 bool motion_trig_on;
142 bool ev_enable_state;
144 /* magnetometer specific data */
145 struct iio_dev *mag_indio_dev;
146 struct iio_trigger *mag_dready_trig;
147 bool mag_dready_trig_on;
150 enum kmx61_range {
151 KMX61_RANGE_2G,
152 KMX61_RANGE_4G,
153 KMX61_RANGE_8G,
156 enum kmx61_axis {
157 KMX61_AXIS_X,
158 KMX61_AXIS_Y,
159 KMX61_AXIS_Z,
162 static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
164 static const struct {
165 int val;
166 int val2;
167 } kmx61_samp_freq_table[] = { {12, 500000},
168 {25, 0},
169 {50, 0},
170 {100, 0},
171 {200, 0},
172 {400, 0},
173 {800, 0},
174 {1600, 0},
175 {0, 781000},
176 {1, 563000},
177 {3, 125000},
178 {6, 250000} };
180 static const struct {
181 int val;
182 int val2;
183 int odr_bits;
184 } kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
185 {1, 563000, 0x01},
186 {3, 125000, 0x02},
187 {6, 250000, 0x03},
188 {12, 500000, 0x04},
189 {25, 0, 0x05},
190 {50, 0, 0x06},
191 {100, 0, 0x06},
192 {200, 0, 0x06},
193 {400, 0, 0x06},
194 {800, 0, 0x06},
195 {1600, 0, 0x06} };
197 static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
198 static IIO_CONST_ATTR(magn_scale_available, "0.001465");
199 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
200 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
202 static struct attribute *kmx61_acc_attributes[] = {
203 &iio_const_attr_accel_scale_available.dev_attr.attr,
204 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
205 NULL,
208 static struct attribute *kmx61_mag_attributes[] = {
209 &iio_const_attr_magn_scale_available.dev_attr.attr,
210 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
211 NULL,
214 static const struct attribute_group kmx61_acc_attribute_group = {
215 .attrs = kmx61_acc_attributes,
218 static const struct attribute_group kmx61_mag_attribute_group = {
219 .attrs = kmx61_mag_attributes,
222 static const struct iio_event_spec kmx61_event = {
223 .type = IIO_EV_TYPE_THRESH,
224 .dir = IIO_EV_DIR_EITHER,
225 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
226 BIT(IIO_EV_INFO_ENABLE) |
227 BIT(IIO_EV_INFO_PERIOD),
230 #define KMX61_ACC_CHAN(_axis) { \
231 .type = IIO_ACCEL, \
232 .modified = 1, \
233 .channel2 = IIO_MOD_ ## _axis, \
234 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
235 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
236 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
237 .address = KMX61_ACC, \
238 .scan_index = KMX61_AXIS_ ## _axis, \
239 .scan_type = { \
240 .sign = 's', \
241 .realbits = 12, \
242 .storagebits = 16, \
243 .shift = 4, \
244 .endianness = IIO_LE, \
245 }, \
246 .event_spec = &kmx61_event, \
247 .num_event_specs = 1 \
250 #define KMX61_MAG_CHAN(_axis) { \
251 .type = IIO_MAGN, \
252 .modified = 1, \
253 .channel2 = IIO_MOD_ ## _axis, \
254 .address = KMX61_MAG, \
255 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
256 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
257 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
258 .scan_index = KMX61_AXIS_ ## _axis, \
259 .scan_type = { \
260 .sign = 's', \
261 .realbits = 14, \
262 .storagebits = 16, \
263 .shift = 2, \
264 .endianness = IIO_LE, \
265 }, \
268 static const struct iio_chan_spec kmx61_acc_channels[] = {
269 KMX61_ACC_CHAN(X),
270 KMX61_ACC_CHAN(Y),
271 KMX61_ACC_CHAN(Z),
274 static const struct iio_chan_spec kmx61_mag_channels[] = {
275 KMX61_MAG_CHAN(X),
276 KMX61_MAG_CHAN(Y),
277 KMX61_MAG_CHAN(Z),
280 static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
282 struct kmx61_data **priv = iio_priv(indio_dev);
284 *priv = data;
287 static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
289 return *(struct kmx61_data **)iio_priv(indio_dev);
292 static int kmx61_convert_freq_to_bit(int val, int val2)
294 int i;
296 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
297 if (val == kmx61_samp_freq_table[i].val &&
298 val2 == kmx61_samp_freq_table[i].val2)
299 return i;
300 return -EINVAL;
303 static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
305 int i;
307 for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
308 if (kmx61_wake_up_odr_table[i].val == val &&
309 kmx61_wake_up_odr_table[i].val2 == val2)
310 return kmx61_wake_up_odr_table[i].odr_bits;
311 return -EINVAL;
315 * kmx61_set_mode() - set KMX61 device operating mode
316 * @data: kmx61 device private data pointer
317 * @mode: bitmask, indicating operating mode for @device
318 * @device: bitmask, indicating device for which @mode needs to be set
319 * @update: update stby bits stored in device's private @data
321 * For each sensor (accelerometer/magnetometer) there are two operating modes
322 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
323 * if they are both enabled. Internal sensors state is saved in acc_stby and
324 * mag_stby members of driver's private @data.
326 static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
327 bool update)
329 int ret;
330 int acc_stby = -1, mag_stby = -1;
332 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
333 if (ret < 0) {
334 dev_err(&data->client->dev, "Error reading reg_stby\n");
335 return ret;
337 if (device & KMX61_ACC) {
338 if (mode & KMX61_ACC_STBY_BIT) {
339 ret |= KMX61_ACC_STBY_BIT;
340 acc_stby = 1;
341 } else {
342 ret &= ~KMX61_ACC_STBY_BIT;
343 acc_stby = 0;
347 if (device & KMX61_MAG) {
348 if (mode & KMX61_MAG_STBY_BIT) {
349 ret |= KMX61_MAG_STBY_BIT;
350 mag_stby = 1;
351 } else {
352 ret &= ~KMX61_MAG_STBY_BIT;
353 mag_stby = 0;
357 if (mode & KMX61_ACT_STBY_BIT)
358 ret |= KMX61_ACT_STBY_BIT;
360 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
361 if (ret < 0) {
362 dev_err(&data->client->dev, "Error writing reg_stby\n");
363 return ret;
366 if (acc_stby != -1 && update)
367 data->acc_stby = acc_stby;
368 if (mag_stby != -1 && update)
369 data->mag_stby = mag_stby;
371 return 0;
374 static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
376 int ret;
378 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
379 if (ret < 0) {
380 dev_err(&data->client->dev, "Error reading reg_stby\n");
381 return ret;
383 *mode = 0;
385 if (device & KMX61_ACC) {
386 if (ret & KMX61_ACC_STBY_BIT)
387 *mode |= KMX61_ACC_STBY_BIT;
388 else
389 *mode &= ~KMX61_ACC_STBY_BIT;
392 if (device & KMX61_MAG) {
393 if (ret & KMX61_MAG_STBY_BIT)
394 *mode |= KMX61_MAG_STBY_BIT;
395 else
396 *mode &= ~KMX61_MAG_STBY_BIT;
399 return 0;
402 static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
404 int ret, odr_bits;
406 odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
407 if (odr_bits < 0)
408 return odr_bits;
410 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
411 odr_bits);
412 if (ret < 0)
413 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
414 return ret;
417 static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
419 int ret;
420 u8 mode;
421 int lodr_bits, odr_bits;
423 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
424 if (ret < 0)
425 return ret;
427 lodr_bits = kmx61_convert_freq_to_bit(val, val2);
428 if (lodr_bits < 0)
429 return lodr_bits;
431 /* To change ODR, accel and magn must be in STDBY */
432 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
433 true);
434 if (ret < 0)
435 return ret;
437 odr_bits = 0;
438 if (device & KMX61_ACC)
439 odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
440 if (device & KMX61_MAG)
441 odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
443 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
444 odr_bits);
445 if (ret < 0)
446 return ret;
448 data->odr_bits = odr_bits;
450 if (device & KMX61_ACC) {
451 ret = kmx61_set_wake_up_odr(data, val, val2);
452 if (ret)
453 return ret;
456 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
459 static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
460 u8 device)
462 u8 lodr_bits;
464 if (device & KMX61_ACC)
465 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
466 KMX61_ACC_ODR_MASK;
467 else if (device & KMX61_MAG)
468 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
469 KMX61_MAG_ODR_MASK;
470 else
471 return -EINVAL;
473 if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
474 return -EINVAL;
476 *val = kmx61_samp_freq_table[lodr_bits].val;
477 *val2 = kmx61_samp_freq_table[lodr_bits].val2;
479 return 0;
482 static int kmx61_set_range(struct kmx61_data *data, u8 range)
484 int ret;
486 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
487 if (ret < 0) {
488 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
489 return ret;
492 ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
493 ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
495 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
496 if (ret < 0) {
497 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
498 return ret;
501 data->range = range;
503 return 0;
506 static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
508 int ret, i;
509 u8 mode;
511 for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
512 if (kmx61_uscale_table[i] == uscale) {
513 ret = kmx61_get_mode(data, &mode,
514 KMX61_ACC | KMX61_MAG);
515 if (ret < 0)
516 return ret;
518 ret = kmx61_set_mode(data, KMX61_ALL_STBY,
519 KMX61_ACC | KMX61_MAG, true);
520 if (ret < 0)
521 return ret;
523 ret = kmx61_set_range(data, i);
524 if (ret < 0)
525 return ret;
527 return kmx61_set_mode(data, mode,
528 KMX61_ACC | KMX61_MAG, true);
531 return -EINVAL;
534 static int kmx61_chip_init(struct kmx61_data *data)
536 int ret, val, val2;
538 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
539 if (ret < 0) {
540 dev_err(&data->client->dev, "Error reading who_am_i\n");
541 return ret;
544 if (ret != KMX61_CHIP_ID) {
545 dev_err(&data->client->dev,
546 "Wrong chip id, got %x expected %x\n",
547 ret, KMX61_CHIP_ID);
548 return -EINVAL;
551 /* set accel 12bit, 4g range */
552 ret = kmx61_set_range(data, KMX61_RANGE_4G);
553 if (ret < 0)
554 return ret;
556 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
557 if (ret < 0) {
558 dev_err(&data->client->dev, "Error reading reg_odcntl\n");
559 return ret;
561 data->odr_bits = ret;
564 * set output data rate for wake up (motion detection) function
565 * to match data rate for accelerometer sampling
567 ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
568 if (ret < 0)
569 return ret;
571 ret = kmx61_set_wake_up_odr(data, val, val2);
572 if (ret < 0)
573 return ret;
575 /* set acc/magn to OPERATION mode */
576 ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
577 if (ret < 0)
578 return ret;
580 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
581 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
583 return 0;
586 static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
587 bool status, u8 device)
589 u8 mode;
590 int ret;
592 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
593 if (ret < 0)
594 return ret;
596 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
597 if (ret < 0)
598 return ret;
600 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
601 if (ret < 0) {
602 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
603 return ret;
606 if (status) {
607 ret |= KMX61_REG_INC1_BIT_IEN;
608 if (device & KMX61_ACC)
609 ret |= KMX61_REG_INC1_BIT_DRDYA;
610 if (device & KMX61_MAG)
611 ret |= KMX61_REG_INC1_BIT_DRDYM;
612 } else {
613 ret &= ~KMX61_REG_INC1_BIT_IEN;
614 if (device & KMX61_ACC)
615 ret &= ~KMX61_REG_INC1_BIT_DRDYA;
616 if (device & KMX61_MAG)
617 ret &= ~KMX61_REG_INC1_BIT_DRDYM;
619 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
620 if (ret < 0) {
621 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
622 return ret;
625 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
626 if (ret < 0) {
627 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
628 return ret;
631 if (status)
632 ret |= KMX61_REG_CTRL1_BIT_DRDYE;
633 else
634 ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
636 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
637 if (ret < 0) {
638 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
639 return ret;
642 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
645 static int kmx61_chip_update_thresholds(struct kmx61_data *data)
647 int ret;
649 ret = i2c_smbus_write_byte_data(data->client,
650 KMX61_REG_WUF_TIMER,
651 data->wake_duration);
652 if (ret < 0) {
653 dev_err(&data->client->dev, "Error writing reg_wuf_timer\n");
654 return ret;
657 ret = i2c_smbus_write_byte_data(data->client,
658 KMX61_REG_WUF_THRESH,
659 data->wake_thresh);
660 if (ret < 0)
661 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
663 return ret;
666 static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
667 bool status)
669 u8 mode;
670 int ret;
672 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
673 if (ret < 0)
674 return ret;
676 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
677 if (ret < 0)
678 return ret;
680 ret = kmx61_chip_update_thresholds(data);
681 if (ret < 0)
682 return ret;
684 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
685 if (ret < 0) {
686 dev_err(&data->client->dev, "Error reading reg_inc1\n");
687 return ret;
689 if (status)
690 ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
691 else
692 ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
694 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
695 if (ret < 0) {
696 dev_err(&data->client->dev, "Error writing reg_inc1\n");
697 return ret;
700 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
701 if (ret < 0) {
702 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
703 return ret;
706 if (status)
707 ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
708 else
709 ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
711 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
712 if (ret < 0) {
713 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
714 return ret;
716 mode |= KMX61_ACT_STBY_BIT;
717 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
721 * kmx61_set_power_state() - set power state for kmx61 @device
722 * @data: kmx61 device private pointer
723 * @on: power state to be set for @device
724 * @device: bitmask indicating device for which @on state needs to be set
726 * Notice that when ACC power state needs to be set to ON and MAG is in
727 * OPERATION then we know that kmx61_runtime_resume was already called
728 * so we must set ACC OPERATION mode here. The same happens when MAG power
729 * state needs to be set to ON and ACC is in OPERATION.
731 static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
733 #ifdef CONFIG_PM
734 int ret;
736 if (device & KMX61_ACC) {
737 if (on && !data->acc_ps && !data->mag_stby) {
738 ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
739 if (ret < 0)
740 return ret;
742 data->acc_ps = on;
744 if (device & KMX61_MAG) {
745 if (on && !data->mag_ps && !data->acc_stby) {
746 ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
747 if (ret < 0)
748 return ret;
750 data->mag_ps = on;
753 if (on) {
754 ret = pm_runtime_resume_and_get(&data->client->dev);
755 } else {
756 pm_runtime_mark_last_busy(&data->client->dev);
757 ret = pm_runtime_put_autosuspend(&data->client->dev);
759 if (ret < 0) {
760 dev_err(&data->client->dev,
761 "Failed: kmx61_set_power_state for %d, ret %d\n",
762 on, ret);
764 return ret;
766 #endif
767 return 0;
770 static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
772 int ret;
773 u8 reg = base + offset * 2;
775 ret = i2c_smbus_read_word_data(data->client, reg);
776 if (ret < 0)
777 dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
779 return ret;
782 static int kmx61_read_raw(struct iio_dev *indio_dev,
783 struct iio_chan_spec const *chan, int *val,
784 int *val2, long mask)
786 int ret;
787 u8 base_reg;
788 struct kmx61_data *data = kmx61_get_data(indio_dev);
790 switch (mask) {
791 case IIO_CHAN_INFO_RAW:
792 switch (chan->type) {
793 case IIO_ACCEL:
794 base_reg = KMX61_ACC_XOUT_L;
795 break;
796 case IIO_MAGN:
797 base_reg = KMX61_MAG_XOUT_L;
798 break;
799 default:
800 return -EINVAL;
802 mutex_lock(&data->lock);
804 ret = kmx61_set_power_state(data, true, chan->address);
805 if (ret) {
806 mutex_unlock(&data->lock);
807 return ret;
810 ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
811 if (ret < 0) {
812 kmx61_set_power_state(data, false, chan->address);
813 mutex_unlock(&data->lock);
814 return ret;
816 *val = sign_extend32(ret >> chan->scan_type.shift,
817 chan->scan_type.realbits - 1);
818 ret = kmx61_set_power_state(data, false, chan->address);
820 mutex_unlock(&data->lock);
821 if (ret)
822 return ret;
823 return IIO_VAL_INT;
824 case IIO_CHAN_INFO_SCALE:
825 switch (chan->type) {
826 case IIO_ACCEL:
827 *val = 0;
828 *val2 = kmx61_uscale_table[data->range];
829 return IIO_VAL_INT_PLUS_MICRO;
830 case IIO_MAGN:
831 /* 14 bits res, 1465 microGauss per magn count */
832 *val = 0;
833 *val2 = 1465;
834 return IIO_VAL_INT_PLUS_MICRO;
835 default:
836 return -EINVAL;
838 case IIO_CHAN_INFO_SAMP_FREQ:
839 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
840 return -EINVAL;
842 mutex_lock(&data->lock);
843 ret = kmx61_get_odr(data, val, val2, chan->address);
844 mutex_unlock(&data->lock);
845 if (ret)
846 return -EINVAL;
847 return IIO_VAL_INT_PLUS_MICRO;
849 return -EINVAL;
852 static int kmx61_write_raw(struct iio_dev *indio_dev,
853 struct iio_chan_spec const *chan, int val,
854 int val2, long mask)
856 int ret;
857 struct kmx61_data *data = kmx61_get_data(indio_dev);
859 switch (mask) {
860 case IIO_CHAN_INFO_SAMP_FREQ:
861 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
862 return -EINVAL;
864 mutex_lock(&data->lock);
865 ret = kmx61_set_odr(data, val, val2, chan->address);
866 mutex_unlock(&data->lock);
867 return ret;
868 case IIO_CHAN_INFO_SCALE:
869 switch (chan->type) {
870 case IIO_ACCEL:
871 if (val != 0)
872 return -EINVAL;
873 mutex_lock(&data->lock);
874 ret = kmx61_set_scale(data, val2);
875 mutex_unlock(&data->lock);
876 return ret;
877 default:
878 return -EINVAL;
880 default:
881 return -EINVAL;
885 static int kmx61_read_event(struct iio_dev *indio_dev,
886 const struct iio_chan_spec *chan,
887 enum iio_event_type type,
888 enum iio_event_direction dir,
889 enum iio_event_info info,
890 int *val, int *val2)
892 struct kmx61_data *data = kmx61_get_data(indio_dev);
894 *val2 = 0;
895 switch (info) {
896 case IIO_EV_INFO_VALUE:
897 *val = data->wake_thresh;
898 return IIO_VAL_INT;
899 case IIO_EV_INFO_PERIOD:
900 *val = data->wake_duration;
901 return IIO_VAL_INT;
902 default:
903 return -EINVAL;
907 static int kmx61_write_event(struct iio_dev *indio_dev,
908 const struct iio_chan_spec *chan,
909 enum iio_event_type type,
910 enum iio_event_direction dir,
911 enum iio_event_info info,
912 int val, int val2)
914 struct kmx61_data *data = kmx61_get_data(indio_dev);
916 if (data->ev_enable_state)
917 return -EBUSY;
919 switch (info) {
920 case IIO_EV_INFO_VALUE:
921 data->wake_thresh = val;
922 return IIO_VAL_INT;
923 case IIO_EV_INFO_PERIOD:
924 data->wake_duration = val;
925 return IIO_VAL_INT;
926 default:
927 return -EINVAL;
931 static int kmx61_read_event_config(struct iio_dev *indio_dev,
932 const struct iio_chan_spec *chan,
933 enum iio_event_type type,
934 enum iio_event_direction dir)
936 struct kmx61_data *data = kmx61_get_data(indio_dev);
938 return data->ev_enable_state;
941 static int kmx61_write_event_config(struct iio_dev *indio_dev,
942 const struct iio_chan_spec *chan,
943 enum iio_event_type type,
944 enum iio_event_direction dir,
945 bool state)
947 struct kmx61_data *data = kmx61_get_data(indio_dev);
948 int ret = 0;
950 if (state && data->ev_enable_state)
951 return 0;
953 mutex_lock(&data->lock);
955 if (!state && data->motion_trig_on) {
956 data->ev_enable_state = false;
957 goto err_unlock;
960 ret = kmx61_set_power_state(data, state, KMX61_ACC);
961 if (ret < 0)
962 goto err_unlock;
964 ret = kmx61_setup_any_motion_interrupt(data, state);
965 if (ret < 0) {
966 kmx61_set_power_state(data, false, KMX61_ACC);
967 goto err_unlock;
970 data->ev_enable_state = state;
972 err_unlock:
973 mutex_unlock(&data->lock);
975 return ret;
978 static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
979 struct iio_trigger *trig)
981 struct kmx61_data *data = kmx61_get_data(indio_dev);
983 if (data->acc_dready_trig != trig && data->motion_trig != trig)
984 return -EINVAL;
986 return 0;
989 static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
990 struct iio_trigger *trig)
992 struct kmx61_data *data = kmx61_get_data(indio_dev);
994 if (data->mag_dready_trig != trig)
995 return -EINVAL;
997 return 0;
1000 static const struct iio_info kmx61_acc_info = {
1001 .read_raw = kmx61_read_raw,
1002 .write_raw = kmx61_write_raw,
1003 .attrs = &kmx61_acc_attribute_group,
1004 .read_event_value = kmx61_read_event,
1005 .write_event_value = kmx61_write_event,
1006 .read_event_config = kmx61_read_event_config,
1007 .write_event_config = kmx61_write_event_config,
1008 .validate_trigger = kmx61_acc_validate_trigger,
1011 static const struct iio_info kmx61_mag_info = {
1012 .read_raw = kmx61_read_raw,
1013 .write_raw = kmx61_write_raw,
1014 .attrs = &kmx61_mag_attribute_group,
1015 .validate_trigger = kmx61_mag_validate_trigger,
1019 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
1020 bool state)
1022 int ret = 0;
1023 u8 device;
1025 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1026 struct kmx61_data *data = kmx61_get_data(indio_dev);
1028 mutex_lock(&data->lock);
1030 if (!state && data->ev_enable_state && data->motion_trig_on) {
1031 data->motion_trig_on = false;
1032 goto err_unlock;
1035 if (data->acc_dready_trig == trig || data->motion_trig == trig)
1036 device = KMX61_ACC;
1037 else
1038 device = KMX61_MAG;
1040 ret = kmx61_set_power_state(data, state, device);
1041 if (ret < 0)
1042 goto err_unlock;
1044 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1045 ret = kmx61_setup_new_data_interrupt(data, state, device);
1046 else
1047 ret = kmx61_setup_any_motion_interrupt(data, state);
1048 if (ret < 0) {
1049 kmx61_set_power_state(data, false, device);
1050 goto err_unlock;
1053 if (data->acc_dready_trig == trig)
1054 data->acc_dready_trig_on = state;
1055 else if (data->mag_dready_trig == trig)
1056 data->mag_dready_trig_on = state;
1057 else
1058 data->motion_trig_on = state;
1059 err_unlock:
1060 mutex_unlock(&data->lock);
1062 return ret;
1065 static void kmx61_trig_reenable(struct iio_trigger *trig)
1067 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1068 struct kmx61_data *data = kmx61_get_data(indio_dev);
1069 int ret;
1071 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1072 if (ret < 0)
1073 dev_err(&data->client->dev, "Error reading reg_inl\n");
1076 static const struct iio_trigger_ops kmx61_trigger_ops = {
1077 .set_trigger_state = kmx61_data_rdy_trigger_set_state,
1078 .reenable = kmx61_trig_reenable,
1081 static irqreturn_t kmx61_event_handler(int irq, void *private)
1083 struct kmx61_data *data = private;
1084 struct iio_dev *indio_dev = data->acc_indio_dev;
1085 int ret;
1087 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1088 if (ret < 0) {
1089 dev_err(&data->client->dev, "Error reading reg_ins1\n");
1090 goto ack_intr;
1093 if (ret & KMX61_REG_INS1_BIT_WUFS) {
1094 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1095 if (ret < 0) {
1096 dev_err(&data->client->dev, "Error reading reg_ins2\n");
1097 goto ack_intr;
1100 if (ret & KMX61_REG_INS2_BIT_XN)
1101 iio_push_event(indio_dev,
1102 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1104 IIO_MOD_X,
1105 IIO_EV_TYPE_THRESH,
1106 IIO_EV_DIR_FALLING),
1109 if (ret & KMX61_REG_INS2_BIT_XP)
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_RISING),
1118 if (ret & KMX61_REG_INS2_BIT_YN)
1119 iio_push_event(indio_dev,
1120 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1122 IIO_MOD_Y,
1123 IIO_EV_TYPE_THRESH,
1124 IIO_EV_DIR_FALLING),
1127 if (ret & KMX61_REG_INS2_BIT_YP)
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_RISING),
1136 if (ret & KMX61_REG_INS2_BIT_ZN)
1137 iio_push_event(indio_dev,
1138 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1140 IIO_MOD_Z,
1141 IIO_EV_TYPE_THRESH,
1142 IIO_EV_DIR_FALLING),
1145 if (ret & KMX61_REG_INS2_BIT_ZP)
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_RISING),
1155 ack_intr:
1156 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1157 if (ret < 0)
1158 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1160 ret |= KMX61_REG_CTRL1_BIT_RES;
1161 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1162 if (ret < 0)
1163 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1165 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1166 if (ret < 0)
1167 dev_err(&data->client->dev, "Error reading reg_inl\n");
1169 return IRQ_HANDLED;
1172 static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1174 struct kmx61_data *data = private;
1176 if (data->acc_dready_trig_on)
1177 iio_trigger_poll(data->acc_dready_trig);
1178 if (data->mag_dready_trig_on)
1179 iio_trigger_poll(data->mag_dready_trig);
1181 if (data->motion_trig_on)
1182 iio_trigger_poll(data->motion_trig);
1184 if (data->ev_enable_state)
1185 return IRQ_WAKE_THREAD;
1186 return IRQ_HANDLED;
1189 static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1191 struct iio_poll_func *pf = p;
1192 struct iio_dev *indio_dev = pf->indio_dev;
1193 struct kmx61_data *data = kmx61_get_data(indio_dev);
1194 int bit, ret, i = 0;
1195 u8 base;
1196 s16 buffer[8];
1198 if (indio_dev == data->acc_indio_dev)
1199 base = KMX61_ACC_XOUT_L;
1200 else
1201 base = KMX61_MAG_XOUT_L;
1203 mutex_lock(&data->lock);
1204 iio_for_each_active_channel(indio_dev, bit) {
1205 ret = kmx61_read_measurement(data, base, bit);
1206 if (ret < 0) {
1207 mutex_unlock(&data->lock);
1208 goto err;
1210 buffer[i++] = ret;
1212 mutex_unlock(&data->lock);
1214 iio_push_to_buffers(indio_dev, buffer);
1215 err:
1216 iio_trigger_notify_done(indio_dev->trig);
1218 return IRQ_HANDLED;
1221 static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1222 const struct iio_info *info,
1223 const struct iio_chan_spec *chan,
1224 int num_channels,
1225 const char *name)
1227 struct iio_dev *indio_dev;
1229 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1230 if (!indio_dev)
1231 return ERR_PTR(-ENOMEM);
1233 kmx61_set_data(indio_dev, data);
1235 indio_dev->channels = chan;
1236 indio_dev->num_channels = num_channels;
1237 indio_dev->name = name;
1238 indio_dev->modes = INDIO_DIRECT_MODE;
1239 indio_dev->info = info;
1241 return indio_dev;
1244 static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1245 struct iio_dev *indio_dev,
1246 const char *tag)
1248 struct iio_trigger *trig;
1249 int ret;
1251 trig = devm_iio_trigger_alloc(&data->client->dev,
1252 "%s-%s-dev%d",
1253 indio_dev->name,
1254 tag,
1255 iio_device_id(indio_dev));
1256 if (!trig)
1257 return ERR_PTR(-ENOMEM);
1259 trig->ops = &kmx61_trigger_ops;
1260 iio_trigger_set_drvdata(trig, indio_dev);
1262 ret = iio_trigger_register(trig);
1263 if (ret)
1264 return ERR_PTR(ret);
1266 return trig;
1269 static int kmx61_probe(struct i2c_client *client)
1271 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1272 int ret;
1273 struct kmx61_data *data;
1274 const char *name = NULL;
1276 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1277 if (!data)
1278 return -ENOMEM;
1280 i2c_set_clientdata(client, data);
1281 data->client = client;
1283 mutex_init(&data->lock);
1285 if (id)
1286 name = id->name;
1287 else
1288 return -ENODEV;
1290 data->acc_indio_dev =
1291 kmx61_indiodev_setup(data, &kmx61_acc_info,
1292 kmx61_acc_channels,
1293 ARRAY_SIZE(kmx61_acc_channels),
1294 name);
1295 if (IS_ERR(data->acc_indio_dev))
1296 return PTR_ERR(data->acc_indio_dev);
1298 data->mag_indio_dev =
1299 kmx61_indiodev_setup(data, &kmx61_mag_info,
1300 kmx61_mag_channels,
1301 ARRAY_SIZE(kmx61_mag_channels),
1302 name);
1303 if (IS_ERR(data->mag_indio_dev))
1304 return PTR_ERR(data->mag_indio_dev);
1306 ret = kmx61_chip_init(data);
1307 if (ret < 0)
1308 return ret;
1310 if (client->irq > 0) {
1311 ret = devm_request_threaded_irq(&client->dev, client->irq,
1312 kmx61_data_rdy_trig_poll,
1313 kmx61_event_handler,
1314 IRQF_TRIGGER_RISING,
1315 KMX61_IRQ_NAME,
1316 data);
1317 if (ret)
1318 goto err_chip_uninit;
1320 data->acc_dready_trig =
1321 kmx61_trigger_setup(data, data->acc_indio_dev,
1322 "dready");
1323 if (IS_ERR(data->acc_dready_trig)) {
1324 ret = PTR_ERR(data->acc_dready_trig);
1325 goto err_chip_uninit;
1328 data->mag_dready_trig =
1329 kmx61_trigger_setup(data, data->mag_indio_dev,
1330 "dready");
1331 if (IS_ERR(data->mag_dready_trig)) {
1332 ret = PTR_ERR(data->mag_dready_trig);
1333 goto err_trigger_unregister_acc_dready;
1336 data->motion_trig =
1337 kmx61_trigger_setup(data, data->acc_indio_dev,
1338 "any-motion");
1339 if (IS_ERR(data->motion_trig)) {
1340 ret = PTR_ERR(data->motion_trig);
1341 goto err_trigger_unregister_mag_dready;
1344 ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1345 &iio_pollfunc_store_time,
1346 kmx61_trigger_handler,
1347 NULL);
1348 if (ret < 0) {
1349 dev_err(&data->client->dev,
1350 "Failed to setup acc triggered buffer\n");
1351 goto err_trigger_unregister_motion;
1354 ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1355 &iio_pollfunc_store_time,
1356 kmx61_trigger_handler,
1357 NULL);
1358 if (ret < 0) {
1359 dev_err(&data->client->dev,
1360 "Failed to setup mag triggered buffer\n");
1361 goto err_buffer_cleanup_acc;
1365 ret = pm_runtime_set_active(&client->dev);
1366 if (ret < 0)
1367 goto err_buffer_cleanup_mag;
1369 pm_runtime_enable(&client->dev);
1370 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1371 pm_runtime_use_autosuspend(&client->dev);
1373 ret = iio_device_register(data->acc_indio_dev);
1374 if (ret < 0) {
1375 dev_err(&client->dev, "Failed to register acc iio device\n");
1376 goto err_pm_cleanup;
1379 ret = iio_device_register(data->mag_indio_dev);
1380 if (ret < 0) {
1381 dev_err(&client->dev, "Failed to register mag iio device\n");
1382 goto err_iio_unregister_acc;
1385 return 0;
1387 err_iio_unregister_acc:
1388 iio_device_unregister(data->acc_indio_dev);
1389 err_pm_cleanup:
1390 pm_runtime_dont_use_autosuspend(&client->dev);
1391 pm_runtime_disable(&client->dev);
1392 err_buffer_cleanup_mag:
1393 if (client->irq > 0)
1394 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1395 err_buffer_cleanup_acc:
1396 if (client->irq > 0)
1397 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1398 err_trigger_unregister_motion:
1399 iio_trigger_unregister(data->motion_trig);
1400 err_trigger_unregister_mag_dready:
1401 iio_trigger_unregister(data->mag_dready_trig);
1402 err_trigger_unregister_acc_dready:
1403 iio_trigger_unregister(data->acc_dready_trig);
1404 err_chip_uninit:
1405 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1406 return ret;
1409 static void kmx61_remove(struct i2c_client *client)
1411 struct kmx61_data *data = i2c_get_clientdata(client);
1413 iio_device_unregister(data->acc_indio_dev);
1414 iio_device_unregister(data->mag_indio_dev);
1416 pm_runtime_disable(&client->dev);
1417 pm_runtime_set_suspended(&client->dev);
1419 if (client->irq > 0) {
1420 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1421 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1422 iio_trigger_unregister(data->acc_dready_trig);
1423 iio_trigger_unregister(data->mag_dready_trig);
1424 iio_trigger_unregister(data->motion_trig);
1427 mutex_lock(&data->lock);
1428 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1429 mutex_unlock(&data->lock);
1432 static int kmx61_suspend(struct device *dev)
1434 int ret;
1435 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1437 mutex_lock(&data->lock);
1438 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1439 false);
1440 mutex_unlock(&data->lock);
1442 return ret;
1445 static int kmx61_resume(struct device *dev)
1447 u8 stby = 0;
1448 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1450 if (data->acc_stby)
1451 stby |= KMX61_ACC_STBY_BIT;
1452 if (data->mag_stby)
1453 stby |= KMX61_MAG_STBY_BIT;
1455 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1458 static int kmx61_runtime_suspend(struct device *dev)
1460 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1461 int ret;
1463 mutex_lock(&data->lock);
1464 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1465 mutex_unlock(&data->lock);
1467 return ret;
1470 static int kmx61_runtime_resume(struct device *dev)
1472 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1473 u8 stby = 0;
1475 if (!data->acc_ps)
1476 stby |= KMX61_ACC_STBY_BIT;
1477 if (!data->mag_ps)
1478 stby |= KMX61_MAG_STBY_BIT;
1480 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1483 static const struct dev_pm_ops kmx61_pm_ops = {
1484 SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
1485 RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1488 static const struct i2c_device_id kmx61_id[] = {
1489 { "kmx611021" },
1493 MODULE_DEVICE_TABLE(i2c, kmx61_id);
1495 static struct i2c_driver kmx61_driver = {
1496 .driver = {
1497 .name = KMX61_DRV_NAME,
1498 .pm = pm_ptr(&kmx61_pm_ops),
1500 .probe = kmx61_probe,
1501 .remove = kmx61_remove,
1502 .id_table = kmx61_id,
1505 module_i2c_driver(kmx61_driver);
1507 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1508 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1509 MODULE_LICENSE("GPL v2");