1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
4 *
5 * Copyright (c) 2014, Intel Corporation.
6 *
7 * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
8 */
9
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/acpi.h>
13 #include <linux/interrupt.h>
14 #include <linux/pm.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/events.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/iio/trigger_consumer.h>
23
24 #define KMX61_DRV_NAME "kmx61"
25 #define KMX61_IRQ_NAME "kmx61_event"
26
27 #define KMX61_REG_WHO_AM_I 0x00
28 #define KMX61_REG_INS1 0x01
29 #define KMX61_REG_INS2 0x02
30
31 /*
32 * three 16-bit accelerometer output registers for X/Y/Z axis
33 * we use only XOUT_L as a base register, all other addresses
34 * can be obtained by applying an offset and are provided here
35 * only for clarity.
36 */
37 #define KMX61_ACC_XOUT_L 0x0A
38 #define KMX61_ACC_XOUT_H 0x0B
39 #define KMX61_ACC_YOUT_L 0x0C
40 #define KMX61_ACC_YOUT_H 0x0D
41 #define KMX61_ACC_ZOUT_L 0x0E
42 #define KMX61_ACC_ZOUT_H 0x0F
43
44 /*
45 * one 16-bit temperature output register
46 */
47 #define KMX61_TEMP_L 0x10
48 #define KMX61_TEMP_H 0x11
49
50 /*
51 * three 16-bit magnetometer output registers for X/Y/Z axis
52 */
53 #define KMX61_MAG_XOUT_L 0x12
54 #define KMX61_MAG_XOUT_H 0x13
55 #define KMX61_MAG_YOUT_L 0x14
56 #define KMX61_MAG_YOUT_H 0x15
57 #define KMX61_MAG_ZOUT_L 0x16
58 #define KMX61_MAG_ZOUT_H 0x17
59
60 #define KMX61_REG_INL 0x28
61 #define KMX61_REG_STBY 0x29
62 #define KMX61_REG_CTRL1 0x2A
63 #define KMX61_REG_CTRL2 0x2B
64 #define KMX61_REG_ODCNTL 0x2C
65 #define KMX61_REG_INC1 0x2D
66
67 #define KMX61_REG_WUF_THRESH 0x3D
68 #define KMX61_REG_WUF_TIMER 0x3E
69
70 #define KMX61_ACC_STBY_BIT BIT(0)
71 #define KMX61_MAG_STBY_BIT BIT(1)
72 #define KMX61_ACT_STBY_BIT BIT(7)
73
74 #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
75
76 #define KMX61_REG_INS1_BIT_WUFS BIT(1)
77
78 #define KMX61_REG_INS2_BIT_ZP BIT(0)
79 #define KMX61_REG_INS2_BIT_ZN BIT(1)
80 #define KMX61_REG_INS2_BIT_YP BIT(2)
81 #define KMX61_REG_INS2_BIT_YN BIT(3)
82 #define KMX61_REG_INS2_BIT_XP BIT(4)
83 #define KMX61_REG_INS2_BIT_XN BIT(5)
84
85 #define KMX61_REG_CTRL1_GSEL_MASK 0x03
86
87 #define KMX61_REG_CTRL1_BIT_RES BIT(4)
88 #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
89 #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
90 #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
91
92 #define KMX61_REG_INC1_BIT_WUFS BIT(0)
93 #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
94 #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
95 #define KMX61_REG_INC1_BIT_IEN BIT(5)
96
97 #define KMX61_ACC_ODR_SHIFT 0
98 #define KMX61_MAG_ODR_SHIFT 4
99 #define KMX61_ACC_ODR_MASK 0x0F
100 #define KMX61_MAG_ODR_MASK 0xF0
101
102 #define KMX61_OWUF_MASK 0x7
103
104 #define KMX61_DEFAULT_WAKE_THRESH 1
105 #define KMX61_DEFAULT_WAKE_DURATION 1
106
107 #define KMX61_SLEEP_DELAY_MS 2000
108
109 #define KMX61_CHIP_ID 0x12
110
111 /* KMX61 devices */
112 #define KMX61_ACC 0x01
113 #define KMX61_MAG 0x02
114
115 struct kmx61_data {
116 struct i2c_client *client;
117
118 /* serialize access to non-atomic ops, e.g set_mode */
119 struct mutex lock;
120
121 /* standby state */
122 bool acc_stby;
123 bool mag_stby;
124
125 /* power state */
126 bool acc_ps;
127 bool mag_ps;
128
129 /* config bits */
130 u8 range;
131 u8 odr_bits;
132 u8 wake_thresh;
133 u8 wake_duration;
134
135 /* accelerometer specific data */
136 struct iio_dev *acc_indio_dev;
137 struct iio_trigger *acc_dready_trig;
138 struct iio_trigger *motion_trig;
139 bool acc_dready_trig_on;
140 bool motion_trig_on;
141 bool ev_enable_state;
142
143 /* magnetometer specific data */
144 struct iio_dev *mag_indio_dev;
145 struct iio_trigger *mag_dready_trig;
146 bool mag_dready_trig_on;
147 };
148
149 enum kmx61_range {
150 KMX61_RANGE_2G,
151 KMX61_RANGE_4G,
152 KMX61_RANGE_8G,
153 };
154
155 enum kmx61_axis {
156 KMX61_AXIS_X,
157 KMX61_AXIS_Y,
158 KMX61_AXIS_Z,
159 };
160
161 static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
162
163 static const struct {
164 int val;
165 int val2;
166 } kmx61_samp_freq_table[] = { {12, 500000},
167 {25, 0},
168 {50, 0},
169 {100, 0},
170 {200, 0},
171 {400, 0},
172 {800, 0},
173 {1600, 0},
174 {0, 781000},
175 {1, 563000},
176 {3, 125000},
177 {6, 250000} };
178
179 static const struct {
180 int val;
181 int val2;
182 int odr_bits;
183 } kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
184 {1, 563000, 0x01},
185 {3, 125000, 0x02},
186 {6, 250000, 0x03},
187 {12, 500000, 0x04},
188 {25, 0, 0x05},
189 {50, 0, 0x06},
190 {100, 0, 0x06},
191 {200, 0, 0x06},
192 {400, 0, 0x06},
193 {800, 0, 0x06},
194 {1600, 0, 0x06} };
195
196 static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
197 static IIO_CONST_ATTR(magn_scale_available, "0.001465");
198 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
199 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
200
201 static struct attribute *kmx61_acc_attributes[] = {
202 &iio_const_attr_accel_scale_available.dev_attr.attr,
203 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
204 NULL,
205 };
206
207 static struct attribute *kmx61_mag_attributes[] = {
208 &iio_const_attr_magn_scale_available.dev_attr.attr,
209 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
210 NULL,
211 };
212
213 static const struct attribute_group kmx61_acc_attribute_group = {
214 .attrs = kmx61_acc_attributes,
215 };
216
217 static const struct attribute_group kmx61_mag_attribute_group = {
218 .attrs = kmx61_mag_attributes,
219 };
220
221 static const struct iio_event_spec kmx61_event = {
222 .type = IIO_EV_TYPE_THRESH,
223 .dir = IIO_EV_DIR_EITHER,
224 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
225 BIT(IIO_EV_INFO_ENABLE) |
226 BIT(IIO_EV_INFO_PERIOD),
227 };
228
229 #define KMX61_ACC_CHAN(_axis) { \
230 .type = IIO_ACCEL, \
231 .modified = 1, \
232 .channel2 = IIO_MOD_ ## _axis, \
233 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
234 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
235 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
236 .address = KMX61_ACC, \
237 .scan_index = KMX61_AXIS_ ## _axis, \
238 .scan_type = { \
239 .sign = 's', \
240 .realbits = 12, \
241 .storagebits = 16, \
242 .shift = 4, \
243 .endianness = IIO_LE, \
244 }, \
245 .event_spec = &kmx61_event, \
246 .num_event_specs = 1 \
247 }
248
249 #define KMX61_MAG_CHAN(_axis) { \
250 .type = IIO_MAGN, \
251 .modified = 1, \
252 .channel2 = IIO_MOD_ ## _axis, \
253 .address = KMX61_MAG, \
254 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
255 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
256 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
257 .scan_index = KMX61_AXIS_ ## _axis, \
258 .scan_type = { \
259 .sign = 's', \
260 .realbits = 14, \
261 .storagebits = 16, \
262 .shift = 2, \
263 .endianness = IIO_LE, \
264 }, \
265 }
266
267 static const struct iio_chan_spec kmx61_acc_channels[] = {
268 KMX61_ACC_CHAN(X),
269 KMX61_ACC_CHAN(Y),
270 KMX61_ACC_CHAN(Z),
271 };
272
273 static const struct iio_chan_spec kmx61_mag_channels[] = {
274 KMX61_MAG_CHAN(X),
275 KMX61_MAG_CHAN(Y),
276 KMX61_MAG_CHAN(Z),
277 };
278
kmx61_set_data(struct iio_dev * indio_dev,struct kmx61_data * data)279 static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
280 {
281 struct kmx61_data **priv = iio_priv(indio_dev);
282
283 *priv = data;
284 }
285
kmx61_get_data(struct iio_dev * indio_dev)286 static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
287 {
288 return *(struct kmx61_data **)iio_priv(indio_dev);
289 }
290
kmx61_convert_freq_to_bit(int val,int val2)291 static int kmx61_convert_freq_to_bit(int val, int val2)
292 {
293 int i;
294
295 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
296 if (val == kmx61_samp_freq_table[i].val &&
297 val2 == kmx61_samp_freq_table[i].val2)
298 return i;
299 return -EINVAL;
300 }
301
kmx61_convert_wake_up_odr_to_bit(int val,int val2)302 static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
303 {
304 int i;
305
306 for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
307 if (kmx61_wake_up_odr_table[i].val == val &&
308 kmx61_wake_up_odr_table[i].val2 == val2)
309 return kmx61_wake_up_odr_table[i].odr_bits;
310 return -EINVAL;
311 }
312
313 /**
314 * kmx61_set_mode() - set KMX61 device operating mode
315 * @data: kmx61 device private data pointer
316 * @mode: bitmask, indicating operating mode for @device
317 * @device: bitmask, indicating device for which @mode needs to be set
318 * @update: update stby bits stored in device's private @data
319 *
320 * For each sensor (accelerometer/magnetometer) there are two operating modes
321 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
322 * if they are both enabled. Internal sensors state is saved in acc_stby and
323 * mag_stby members of driver's private @data.
324 */
kmx61_set_mode(struct kmx61_data * data,u8 mode,u8 device,bool update)325 static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
326 bool update)
327 {
328 int ret;
329 int acc_stby = -1, mag_stby = -1;
330
331 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
332 if (ret < 0) {
333 dev_err(&data->client->dev, "Error reading reg_stby\n");
334 return ret;
335 }
336 if (device & KMX61_ACC) {
337 if (mode & KMX61_ACC_STBY_BIT) {
338 ret |= KMX61_ACC_STBY_BIT;
339 acc_stby = 1;
340 } else {
341 ret &= ~KMX61_ACC_STBY_BIT;
342 acc_stby = 0;
343 }
344 }
345
346 if (device & KMX61_MAG) {
347 if (mode & KMX61_MAG_STBY_BIT) {
348 ret |= KMX61_MAG_STBY_BIT;
349 mag_stby = 1;
350 } else {
351 ret &= ~KMX61_MAG_STBY_BIT;
352 mag_stby = 0;
353 }
354 }
355
356 if (mode & KMX61_ACT_STBY_BIT)
357 ret |= KMX61_ACT_STBY_BIT;
358
359 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
360 if (ret < 0) {
361 dev_err(&data->client->dev, "Error writing reg_stby\n");
362 return ret;
363 }
364
365 if (acc_stby != -1 && update)
366 data->acc_stby = acc_stby;
367 if (mag_stby != -1 && update)
368 data->mag_stby = mag_stby;
369
370 return 0;
371 }
372
kmx61_get_mode(struct kmx61_data * data,u8 * mode,u8 device)373 static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
374 {
375 int ret;
376
377 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
378 if (ret < 0) {
379 dev_err(&data->client->dev, "Error reading reg_stby\n");
380 return ret;
381 }
382 *mode = 0;
383
384 if (device & KMX61_ACC) {
385 if (ret & KMX61_ACC_STBY_BIT)
386 *mode |= KMX61_ACC_STBY_BIT;
387 else
388 *mode &= ~KMX61_ACC_STBY_BIT;
389 }
390
391 if (device & KMX61_MAG) {
392 if (ret & KMX61_MAG_STBY_BIT)
393 *mode |= KMX61_MAG_STBY_BIT;
394 else
395 *mode &= ~KMX61_MAG_STBY_BIT;
396 }
397
398 return 0;
399 }
400
kmx61_set_wake_up_odr(struct kmx61_data * data,int val,int val2)401 static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
402 {
403 int ret, odr_bits;
404
405 odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
406 if (odr_bits < 0)
407 return odr_bits;
408
409 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
410 odr_bits);
411 if (ret < 0)
412 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
413 return ret;
414 }
415
kmx61_set_odr(struct kmx61_data * data,int val,int val2,u8 device)416 static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
417 {
418 int ret;
419 u8 mode;
420 int lodr_bits, odr_bits;
421
422 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
423 if (ret < 0)
424 return ret;
425
426 lodr_bits = kmx61_convert_freq_to_bit(val, val2);
427 if (lodr_bits < 0)
428 return lodr_bits;
429
430 /* To change ODR, accel and magn must be in STDBY */
431 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
432 true);
433 if (ret < 0)
434 return ret;
435
436 odr_bits = 0;
437 if (device & KMX61_ACC)
438 odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
439 if (device & KMX61_MAG)
440 odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
441
442 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
443 odr_bits);
444 if (ret < 0)
445 return ret;
446
447 data->odr_bits = odr_bits;
448
449 if (device & KMX61_ACC) {
450 ret = kmx61_set_wake_up_odr(data, val, val2);
451 if (ret)
452 return ret;
453 }
454
455 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
456 }
457
kmx61_get_odr(struct kmx61_data * data,int * val,int * val2,u8 device)458 static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
459 u8 device)
460 {
461 u8 lodr_bits;
462
463 if (device & KMX61_ACC)
464 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
465 KMX61_ACC_ODR_MASK;
466 else if (device & KMX61_MAG)
467 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
468 KMX61_MAG_ODR_MASK;
469 else
470 return -EINVAL;
471
472 if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
473 return -EINVAL;
474
475 *val = kmx61_samp_freq_table[lodr_bits].val;
476 *val2 = kmx61_samp_freq_table[lodr_bits].val2;
477
478 return 0;
479 }
480
kmx61_set_range(struct kmx61_data * data,u8 range)481 static int kmx61_set_range(struct kmx61_data *data, u8 range)
482 {
483 int ret;
484
485 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
486 if (ret < 0) {
487 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
488 return ret;
489 }
490
491 ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
492 ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
493
494 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
495 if (ret < 0) {
496 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
497 return ret;
498 }
499
500 data->range = range;
501
502 return 0;
503 }
504
kmx61_set_scale(struct kmx61_data * data,u16 uscale)505 static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
506 {
507 int ret, i;
508 u8 mode;
509
510 for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
511 if (kmx61_uscale_table[i] == uscale) {
512 ret = kmx61_get_mode(data, &mode,
513 KMX61_ACC | KMX61_MAG);
514 if (ret < 0)
515 return ret;
516
517 ret = kmx61_set_mode(data, KMX61_ALL_STBY,
518 KMX61_ACC | KMX61_MAG, true);
519 if (ret < 0)
520 return ret;
521
522 ret = kmx61_set_range(data, i);
523 if (ret < 0)
524 return ret;
525
526 return kmx61_set_mode(data, mode,
527 KMX61_ACC | KMX61_MAG, true);
528 }
529 }
530 return -EINVAL;
531 }
532
kmx61_chip_init(struct kmx61_data * data)533 static int kmx61_chip_init(struct kmx61_data *data)
534 {
535 int ret, val, val2;
536
537 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
538 if (ret < 0) {
539 dev_err(&data->client->dev, "Error reading who_am_i\n");
540 return ret;
541 }
542
543 if (ret != KMX61_CHIP_ID) {
544 dev_err(&data->client->dev,
545 "Wrong chip id, got %x expected %x\n",
546 ret, KMX61_CHIP_ID);
547 return -EINVAL;
548 }
549
550 /* set accel 12bit, 4g range */
551 ret = kmx61_set_range(data, KMX61_RANGE_4G);
552 if (ret < 0)
553 return ret;
554
555 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
556 if (ret < 0) {
557 dev_err(&data->client->dev, "Error reading reg_odcntl\n");
558 return ret;
559 }
560 data->odr_bits = ret;
561
562 /*
563 * set output data rate for wake up (motion detection) function
564 * to match data rate for accelerometer sampling
565 */
566 ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
567 if (ret < 0)
568 return ret;
569
570 ret = kmx61_set_wake_up_odr(data, val, val2);
571 if (ret < 0)
572 return ret;
573
574 /* set acc/magn to OPERATION mode */
575 ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
576 if (ret < 0)
577 return ret;
578
579 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
580 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
581
582 return 0;
583 }
584
kmx61_setup_new_data_interrupt(struct kmx61_data * data,bool status,u8 device)585 static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
586 bool status, u8 device)
587 {
588 u8 mode;
589 int ret;
590
591 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
592 if (ret < 0)
593 return ret;
594
595 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
596 if (ret < 0)
597 return ret;
598
599 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
600 if (ret < 0) {
601 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
602 return ret;
603 }
604
605 if (status) {
606 ret |= KMX61_REG_INC1_BIT_IEN;
607 if (device & KMX61_ACC)
608 ret |= KMX61_REG_INC1_BIT_DRDYA;
609 if (device & KMX61_MAG)
610 ret |= KMX61_REG_INC1_BIT_DRDYM;
611 } else {
612 ret &= ~KMX61_REG_INC1_BIT_IEN;
613 if (device & KMX61_ACC)
614 ret &= ~KMX61_REG_INC1_BIT_DRDYA;
615 if (device & KMX61_MAG)
616 ret &= ~KMX61_REG_INC1_BIT_DRDYM;
617 }
618 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
619 if (ret < 0) {
620 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
621 return ret;
622 }
623
624 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
625 if (ret < 0) {
626 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
627 return ret;
628 }
629
630 if (status)
631 ret |= KMX61_REG_CTRL1_BIT_DRDYE;
632 else
633 ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
634
635 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
636 if (ret < 0) {
637 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
638 return ret;
639 }
640
641 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
642 }
643
kmx61_chip_update_thresholds(struct kmx61_data * data)644 static int kmx61_chip_update_thresholds(struct kmx61_data *data)
645 {
646 int ret;
647
648 ret = i2c_smbus_write_byte_data(data->client,
649 KMX61_REG_WUF_TIMER,
650 data->wake_duration);
651 if (ret < 0) {
652 dev_err(&data->client->dev, "Error writing reg_wuf_timer\n");
653 return ret;
654 }
655
656 ret = i2c_smbus_write_byte_data(data->client,
657 KMX61_REG_WUF_THRESH,
658 data->wake_thresh);
659 if (ret < 0)
660 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
661
662 return ret;
663 }
664
kmx61_setup_any_motion_interrupt(struct kmx61_data * data,bool status)665 static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
666 bool status)
667 {
668 u8 mode;
669 int ret;
670
671 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
672 if (ret < 0)
673 return ret;
674
675 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
676 if (ret < 0)
677 return ret;
678
679 ret = kmx61_chip_update_thresholds(data);
680 if (ret < 0)
681 return ret;
682
683 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
684 if (ret < 0) {
685 dev_err(&data->client->dev, "Error reading reg_inc1\n");
686 return ret;
687 }
688 if (status)
689 ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
690 else
691 ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
692
693 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
694 if (ret < 0) {
695 dev_err(&data->client->dev, "Error writing reg_inc1\n");
696 return ret;
697 }
698
699 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
700 if (ret < 0) {
701 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
702 return ret;
703 }
704
705 if (status)
706 ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
707 else
708 ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
709
710 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
711 if (ret < 0) {
712 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
713 return ret;
714 }
715 mode |= KMX61_ACT_STBY_BIT;
716 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
717 }
718
719 /**
720 * kmx61_set_power_state() - set power state for kmx61 @device
721 * @data: kmx61 device private pointer
722 * @on: power state to be set for @device
723 * @device: bitmask indicating device for which @on state needs to be set
724 *
725 * Notice that when ACC power state needs to be set to ON and MAG is in
726 * OPERATION then we know that kmx61_runtime_resume was already called
727 * so we must set ACC OPERATION mode here. The same happens when MAG power
728 * state needs to be set to ON and ACC is in OPERATION.
729 */
kmx61_set_power_state(struct kmx61_data * data,bool on,u8 device)730 static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
731 {
732 #ifdef CONFIG_PM
733 int ret;
734
735 if (device & KMX61_ACC) {
736 if (on && !data->acc_ps && !data->mag_stby) {
737 ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
738 if (ret < 0)
739 return ret;
740 }
741 data->acc_ps = on;
742 }
743 if (device & KMX61_MAG) {
744 if (on && !data->mag_ps && !data->acc_stby) {
745 ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
746 if (ret < 0)
747 return ret;
748 }
749 data->mag_ps = on;
750 }
751
752 if (on) {
753 ret = pm_runtime_resume_and_get(&data->client->dev);
754 } else {
755 pm_runtime_mark_last_busy(&data->client->dev);
756 ret = pm_runtime_put_autosuspend(&data->client->dev);
757 }
758 if (ret < 0) {
759 dev_err(&data->client->dev,
760 "Failed: kmx61_set_power_state for %d, ret %d\n",
761 on, ret);
762
763 return ret;
764 }
765 #endif
766 return 0;
767 }
768
kmx61_read_measurement(struct kmx61_data * data,u8 base,u8 offset)769 static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
770 {
771 int ret;
772 u8 reg = base + offset * 2;
773
774 ret = i2c_smbus_read_word_data(data->client, reg);
775 if (ret < 0)
776 dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
777
778 return ret;
779 }
780
kmx61_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)781 static int kmx61_read_raw(struct iio_dev *indio_dev,
782 struct iio_chan_spec const *chan, int *val,
783 int *val2, long mask)
784 {
785 int ret;
786 u8 base_reg;
787 struct kmx61_data *data = kmx61_get_data(indio_dev);
788
789 switch (mask) {
790 case IIO_CHAN_INFO_RAW:
791 switch (chan->type) {
792 case IIO_ACCEL:
793 base_reg = KMX61_ACC_XOUT_L;
794 break;
795 case IIO_MAGN:
796 base_reg = KMX61_MAG_XOUT_L;
797 break;
798 default:
799 return -EINVAL;
800 }
801 mutex_lock(&data->lock);
802
803 ret = kmx61_set_power_state(data, true, chan->address);
804 if (ret) {
805 mutex_unlock(&data->lock);
806 return ret;
807 }
808
809 ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
810 if (ret < 0) {
811 kmx61_set_power_state(data, false, chan->address);
812 mutex_unlock(&data->lock);
813 return ret;
814 }
815 *val = sign_extend32(ret >> chan->scan_type.shift,
816 chan->scan_type.realbits - 1);
817 ret = kmx61_set_power_state(data, false, chan->address);
818
819 mutex_unlock(&data->lock);
820 if (ret)
821 return ret;
822 return IIO_VAL_INT;
823 case IIO_CHAN_INFO_SCALE:
824 switch (chan->type) {
825 case IIO_ACCEL:
826 *val = 0;
827 *val2 = kmx61_uscale_table[data->range];
828 return IIO_VAL_INT_PLUS_MICRO;
829 case IIO_MAGN:
830 /* 14 bits res, 1465 microGauss per magn count */
831 *val = 0;
832 *val2 = 1465;
833 return IIO_VAL_INT_PLUS_MICRO;
834 default:
835 return -EINVAL;
836 }
837 case IIO_CHAN_INFO_SAMP_FREQ:
838 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
839 return -EINVAL;
840
841 mutex_lock(&data->lock);
842 ret = kmx61_get_odr(data, val, val2, chan->address);
843 mutex_unlock(&data->lock);
844 if (ret)
845 return -EINVAL;
846 return IIO_VAL_INT_PLUS_MICRO;
847 }
848 return -EINVAL;
849 }
850
kmx61_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)851 static int kmx61_write_raw(struct iio_dev *indio_dev,
852 struct iio_chan_spec const *chan, int val,
853 int val2, long mask)
854 {
855 int ret;
856 struct kmx61_data *data = kmx61_get_data(indio_dev);
857
858 switch (mask) {
859 case IIO_CHAN_INFO_SAMP_FREQ:
860 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
861 return -EINVAL;
862
863 mutex_lock(&data->lock);
864 ret = kmx61_set_odr(data, val, val2, chan->address);
865 mutex_unlock(&data->lock);
866 return ret;
867 case IIO_CHAN_INFO_SCALE:
868 switch (chan->type) {
869 case IIO_ACCEL:
870 if (val != 0)
871 return -EINVAL;
872 mutex_lock(&data->lock);
873 ret = kmx61_set_scale(data, val2);
874 mutex_unlock(&data->lock);
875 return ret;
876 default:
877 return -EINVAL;
878 }
879 default:
880 return -EINVAL;
881 }
882 }
883
kmx61_read_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)884 static int kmx61_read_event(struct iio_dev *indio_dev,
885 const struct iio_chan_spec *chan,
886 enum iio_event_type type,
887 enum iio_event_direction dir,
888 enum iio_event_info info,
889 int *val, int *val2)
890 {
891 struct kmx61_data *data = kmx61_get_data(indio_dev);
892
893 *val2 = 0;
894 switch (info) {
895 case IIO_EV_INFO_VALUE:
896 *val = data->wake_thresh;
897 return IIO_VAL_INT;
898 case IIO_EV_INFO_PERIOD:
899 *val = data->wake_duration;
900 return IIO_VAL_INT;
901 default:
902 return -EINVAL;
903 }
904 }
905
kmx61_write_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)906 static int kmx61_write_event(struct iio_dev *indio_dev,
907 const struct iio_chan_spec *chan,
908 enum iio_event_type type,
909 enum iio_event_direction dir,
910 enum iio_event_info info,
911 int val, int val2)
912 {
913 struct kmx61_data *data = kmx61_get_data(indio_dev);
914
915 if (data->ev_enable_state)
916 return -EBUSY;
917
918 switch (info) {
919 case IIO_EV_INFO_VALUE:
920 data->wake_thresh = val;
921 return IIO_VAL_INT;
922 case IIO_EV_INFO_PERIOD:
923 data->wake_duration = val;
924 return IIO_VAL_INT;
925 default:
926 return -EINVAL;
927 }
928 }
929
kmx61_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)930 static int kmx61_read_event_config(struct iio_dev *indio_dev,
931 const struct iio_chan_spec *chan,
932 enum iio_event_type type,
933 enum iio_event_direction dir)
934 {
935 struct kmx61_data *data = kmx61_get_data(indio_dev);
936
937 return data->ev_enable_state;
938 }
939
kmx61_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)940 static int kmx61_write_event_config(struct iio_dev *indio_dev,
941 const struct iio_chan_spec *chan,
942 enum iio_event_type type,
943 enum iio_event_direction dir,
944 int state)
945 {
946 struct kmx61_data *data = kmx61_get_data(indio_dev);
947 int ret = 0;
948
949 if (state && data->ev_enable_state)
950 return 0;
951
952 mutex_lock(&data->lock);
953
954 if (!state && data->motion_trig_on) {
955 data->ev_enable_state = false;
956 goto err_unlock;
957 }
958
959 ret = kmx61_set_power_state(data, state, KMX61_ACC);
960 if (ret < 0)
961 goto err_unlock;
962
963 ret = kmx61_setup_any_motion_interrupt(data, state);
964 if (ret < 0) {
965 kmx61_set_power_state(data, false, KMX61_ACC);
966 goto err_unlock;
967 }
968
969 data->ev_enable_state = state;
970
971 err_unlock:
972 mutex_unlock(&data->lock);
973
974 return ret;
975 }
976
kmx61_acc_validate_trigger(struct iio_dev * indio_dev,struct iio_trigger * trig)977 static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
978 struct iio_trigger *trig)
979 {
980 struct kmx61_data *data = kmx61_get_data(indio_dev);
981
982 if (data->acc_dready_trig != trig && data->motion_trig != trig)
983 return -EINVAL;
984
985 return 0;
986 }
987
kmx61_mag_validate_trigger(struct iio_dev * indio_dev,struct iio_trigger * trig)988 static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
989 struct iio_trigger *trig)
990 {
991 struct kmx61_data *data = kmx61_get_data(indio_dev);
992
993 if (data->mag_dready_trig != trig)
994 return -EINVAL;
995
996 return 0;
997 }
998
999 static const struct iio_info kmx61_acc_info = {
1000 .read_raw = kmx61_read_raw,
1001 .write_raw = kmx61_write_raw,
1002 .attrs = &kmx61_acc_attribute_group,
1003 .read_event_value = kmx61_read_event,
1004 .write_event_value = kmx61_write_event,
1005 .read_event_config = kmx61_read_event_config,
1006 .write_event_config = kmx61_write_event_config,
1007 .validate_trigger = kmx61_acc_validate_trigger,
1008 };
1009
1010 static const struct iio_info kmx61_mag_info = {
1011 .read_raw = kmx61_read_raw,
1012 .write_raw = kmx61_write_raw,
1013 .attrs = &kmx61_mag_attribute_group,
1014 .validate_trigger = kmx61_mag_validate_trigger,
1015 };
1016
1017
kmx61_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)1018 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
1019 bool state)
1020 {
1021 int ret = 0;
1022 u8 device;
1023
1024 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1025 struct kmx61_data *data = kmx61_get_data(indio_dev);
1026
1027 mutex_lock(&data->lock);
1028
1029 if (!state && data->ev_enable_state && data->motion_trig_on) {
1030 data->motion_trig_on = false;
1031 goto err_unlock;
1032 }
1033
1034 if (data->acc_dready_trig == trig || data->motion_trig == trig)
1035 device = KMX61_ACC;
1036 else
1037 device = KMX61_MAG;
1038
1039 ret = kmx61_set_power_state(data, state, device);
1040 if (ret < 0)
1041 goto err_unlock;
1042
1043 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1044 ret = kmx61_setup_new_data_interrupt(data, state, device);
1045 else
1046 ret = kmx61_setup_any_motion_interrupt(data, state);
1047 if (ret < 0) {
1048 kmx61_set_power_state(data, false, device);
1049 goto err_unlock;
1050 }
1051
1052 if (data->acc_dready_trig == trig)
1053 data->acc_dready_trig_on = state;
1054 else if (data->mag_dready_trig == trig)
1055 data->mag_dready_trig_on = state;
1056 else
1057 data->motion_trig_on = state;
1058 err_unlock:
1059 mutex_unlock(&data->lock);
1060
1061 return ret;
1062 }
1063
kmx61_trig_reenable(struct iio_trigger * trig)1064 static void kmx61_trig_reenable(struct iio_trigger *trig)
1065 {
1066 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1067 struct kmx61_data *data = kmx61_get_data(indio_dev);
1068 int ret;
1069
1070 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1071 if (ret < 0)
1072 dev_err(&data->client->dev, "Error reading reg_inl\n");
1073 }
1074
1075 static const struct iio_trigger_ops kmx61_trigger_ops = {
1076 .set_trigger_state = kmx61_data_rdy_trigger_set_state,
1077 .reenable = kmx61_trig_reenable,
1078 };
1079
kmx61_event_handler(int irq,void * private)1080 static irqreturn_t kmx61_event_handler(int irq, void *private)
1081 {
1082 struct kmx61_data *data = private;
1083 struct iio_dev *indio_dev = data->acc_indio_dev;
1084 int ret;
1085
1086 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1087 if (ret < 0) {
1088 dev_err(&data->client->dev, "Error reading reg_ins1\n");
1089 goto ack_intr;
1090 }
1091
1092 if (ret & KMX61_REG_INS1_BIT_WUFS) {
1093 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1094 if (ret < 0) {
1095 dev_err(&data->client->dev, "Error reading reg_ins2\n");
1096 goto ack_intr;
1097 }
1098
1099 if (ret & KMX61_REG_INS2_BIT_XN)
1100 iio_push_event(indio_dev,
1101 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1102 0,
1103 IIO_MOD_X,
1104 IIO_EV_TYPE_THRESH,
1105 IIO_EV_DIR_FALLING),
1106 0);
1107
1108 if (ret & KMX61_REG_INS2_BIT_XP)
1109 iio_push_event(indio_dev,
1110 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1111 0,
1112 IIO_MOD_X,
1113 IIO_EV_TYPE_THRESH,
1114 IIO_EV_DIR_RISING),
1115 0);
1116
1117 if (ret & KMX61_REG_INS2_BIT_YN)
1118 iio_push_event(indio_dev,
1119 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1120 0,
1121 IIO_MOD_Y,
1122 IIO_EV_TYPE_THRESH,
1123 IIO_EV_DIR_FALLING),
1124 0);
1125
1126 if (ret & KMX61_REG_INS2_BIT_YP)
1127 iio_push_event(indio_dev,
1128 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1129 0,
1130 IIO_MOD_Y,
1131 IIO_EV_TYPE_THRESH,
1132 IIO_EV_DIR_RISING),
1133 0);
1134
1135 if (ret & KMX61_REG_INS2_BIT_ZN)
1136 iio_push_event(indio_dev,
1137 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1138 0,
1139 IIO_MOD_Z,
1140 IIO_EV_TYPE_THRESH,
1141 IIO_EV_DIR_FALLING),
1142 0);
1143
1144 if (ret & KMX61_REG_INS2_BIT_ZP)
1145 iio_push_event(indio_dev,
1146 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1147 0,
1148 IIO_MOD_Z,
1149 IIO_EV_TYPE_THRESH,
1150 IIO_EV_DIR_RISING),
1151 0);
1152 }
1153
1154 ack_intr:
1155 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1156 if (ret < 0)
1157 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1158
1159 ret |= KMX61_REG_CTRL1_BIT_RES;
1160 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1161 if (ret < 0)
1162 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1163
1164 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1165 if (ret < 0)
1166 dev_err(&data->client->dev, "Error reading reg_inl\n");
1167
1168 return IRQ_HANDLED;
1169 }
1170
kmx61_data_rdy_trig_poll(int irq,void * private)1171 static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1172 {
1173 struct kmx61_data *data = private;
1174
1175 if (data->acc_dready_trig_on)
1176 iio_trigger_poll(data->acc_dready_trig);
1177 if (data->mag_dready_trig_on)
1178 iio_trigger_poll(data->mag_dready_trig);
1179
1180 if (data->motion_trig_on)
1181 iio_trigger_poll(data->motion_trig);
1182
1183 if (data->ev_enable_state)
1184 return IRQ_WAKE_THREAD;
1185 return IRQ_HANDLED;
1186 }
1187
kmx61_trigger_handler(int irq,void * p)1188 static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1189 {
1190 struct iio_poll_func *pf = p;
1191 struct iio_dev *indio_dev = pf->indio_dev;
1192 struct kmx61_data *data = kmx61_get_data(indio_dev);
1193 int bit, ret, i = 0;
1194 u8 base;
1195 s16 buffer[8];
1196
1197 if (indio_dev == data->acc_indio_dev)
1198 base = KMX61_ACC_XOUT_L;
1199 else
1200 base = KMX61_MAG_XOUT_L;
1201
1202 mutex_lock(&data->lock);
1203 iio_for_each_active_channel(indio_dev, bit) {
1204 ret = kmx61_read_measurement(data, base, bit);
1205 if (ret < 0) {
1206 mutex_unlock(&data->lock);
1207 goto err;
1208 }
1209 buffer[i++] = ret;
1210 }
1211 mutex_unlock(&data->lock);
1212
1213 iio_push_to_buffers(indio_dev, buffer);
1214 err:
1215 iio_trigger_notify_done(indio_dev->trig);
1216
1217 return IRQ_HANDLED;
1218 }
1219
kmx61_match_acpi_device(struct device * dev)1220 static const char *kmx61_match_acpi_device(struct device *dev)
1221 {
1222 const struct acpi_device_id *id;
1223
1224 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1225 if (!id)
1226 return NULL;
1227 return dev_name(dev);
1228 }
1229
kmx61_indiodev_setup(struct kmx61_data * data,const struct iio_info * info,const struct iio_chan_spec * chan,int num_channels,const char * name)1230 static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1231 const struct iio_info *info,
1232 const struct iio_chan_spec *chan,
1233 int num_channels,
1234 const char *name)
1235 {
1236 struct iio_dev *indio_dev;
1237
1238 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1239 if (!indio_dev)
1240 return ERR_PTR(-ENOMEM);
1241
1242 kmx61_set_data(indio_dev, data);
1243
1244 indio_dev->channels = chan;
1245 indio_dev->num_channels = num_channels;
1246 indio_dev->name = name;
1247 indio_dev->modes = INDIO_DIRECT_MODE;
1248 indio_dev->info = info;
1249
1250 return indio_dev;
1251 }
1252
kmx61_trigger_setup(struct kmx61_data * data,struct iio_dev * indio_dev,const char * tag)1253 static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1254 struct iio_dev *indio_dev,
1255 const char *tag)
1256 {
1257 struct iio_trigger *trig;
1258 int ret;
1259
1260 trig = devm_iio_trigger_alloc(&data->client->dev,
1261 "%s-%s-dev%d",
1262 indio_dev->name,
1263 tag,
1264 iio_device_id(indio_dev));
1265 if (!trig)
1266 return ERR_PTR(-ENOMEM);
1267
1268 trig->ops = &kmx61_trigger_ops;
1269 iio_trigger_set_drvdata(trig, indio_dev);
1270
1271 ret = iio_trigger_register(trig);
1272 if (ret)
1273 return ERR_PTR(ret);
1274
1275 return trig;
1276 }
1277
kmx61_probe(struct i2c_client * client)1278 static int kmx61_probe(struct i2c_client *client)
1279 {
1280 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1281 int ret;
1282 struct kmx61_data *data;
1283 const char *name = NULL;
1284
1285 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1286 if (!data)
1287 return -ENOMEM;
1288
1289 i2c_set_clientdata(client, data);
1290 data->client = client;
1291
1292 mutex_init(&data->lock);
1293
1294 if (id)
1295 name = id->name;
1296 else if (ACPI_HANDLE(&client->dev))
1297 name = kmx61_match_acpi_device(&client->dev);
1298 else
1299 return -ENODEV;
1300
1301 data->acc_indio_dev =
1302 kmx61_indiodev_setup(data, &kmx61_acc_info,
1303 kmx61_acc_channels,
1304 ARRAY_SIZE(kmx61_acc_channels),
1305 name);
1306 if (IS_ERR(data->acc_indio_dev))
1307 return PTR_ERR(data->acc_indio_dev);
1308
1309 data->mag_indio_dev =
1310 kmx61_indiodev_setup(data, &kmx61_mag_info,
1311 kmx61_mag_channels,
1312 ARRAY_SIZE(kmx61_mag_channels),
1313 name);
1314 if (IS_ERR(data->mag_indio_dev))
1315 return PTR_ERR(data->mag_indio_dev);
1316
1317 ret = kmx61_chip_init(data);
1318 if (ret < 0)
1319 return ret;
1320
1321 if (client->irq > 0) {
1322 ret = devm_request_threaded_irq(&client->dev, client->irq,
1323 kmx61_data_rdy_trig_poll,
1324 kmx61_event_handler,
1325 IRQF_TRIGGER_RISING,
1326 KMX61_IRQ_NAME,
1327 data);
1328 if (ret)
1329 goto err_chip_uninit;
1330
1331 data->acc_dready_trig =
1332 kmx61_trigger_setup(data, data->acc_indio_dev,
1333 "dready");
1334 if (IS_ERR(data->acc_dready_trig)) {
1335 ret = PTR_ERR(data->acc_dready_trig);
1336 goto err_chip_uninit;
1337 }
1338
1339 data->mag_dready_trig =
1340 kmx61_trigger_setup(data, data->mag_indio_dev,
1341 "dready");
1342 if (IS_ERR(data->mag_dready_trig)) {
1343 ret = PTR_ERR(data->mag_dready_trig);
1344 goto err_trigger_unregister_acc_dready;
1345 }
1346
1347 data->motion_trig =
1348 kmx61_trigger_setup(data, data->acc_indio_dev,
1349 "any-motion");
1350 if (IS_ERR(data->motion_trig)) {
1351 ret = PTR_ERR(data->motion_trig);
1352 goto err_trigger_unregister_mag_dready;
1353 }
1354
1355 ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1356 &iio_pollfunc_store_time,
1357 kmx61_trigger_handler,
1358 NULL);
1359 if (ret < 0) {
1360 dev_err(&data->client->dev,
1361 "Failed to setup acc triggered buffer\n");
1362 goto err_trigger_unregister_motion;
1363 }
1364
1365 ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1366 &iio_pollfunc_store_time,
1367 kmx61_trigger_handler,
1368 NULL);
1369 if (ret < 0) {
1370 dev_err(&data->client->dev,
1371 "Failed to setup mag triggered buffer\n");
1372 goto err_buffer_cleanup_acc;
1373 }
1374 }
1375
1376 ret = pm_runtime_set_active(&client->dev);
1377 if (ret < 0)
1378 goto err_buffer_cleanup_mag;
1379
1380 pm_runtime_enable(&client->dev);
1381 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1382 pm_runtime_use_autosuspend(&client->dev);
1383
1384 ret = iio_device_register(data->acc_indio_dev);
1385 if (ret < 0) {
1386 dev_err(&client->dev, "Failed to register acc iio device\n");
1387 goto err_pm_cleanup;
1388 }
1389
1390 ret = iio_device_register(data->mag_indio_dev);
1391 if (ret < 0) {
1392 dev_err(&client->dev, "Failed to register mag iio device\n");
1393 goto err_iio_unregister_acc;
1394 }
1395
1396 return 0;
1397
1398 err_iio_unregister_acc:
1399 iio_device_unregister(data->acc_indio_dev);
1400 err_pm_cleanup:
1401 pm_runtime_dont_use_autosuspend(&client->dev);
1402 pm_runtime_disable(&client->dev);
1403 err_buffer_cleanup_mag:
1404 if (client->irq > 0)
1405 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1406 err_buffer_cleanup_acc:
1407 if (client->irq > 0)
1408 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1409 err_trigger_unregister_motion:
1410 iio_trigger_unregister(data->motion_trig);
1411 err_trigger_unregister_mag_dready:
1412 iio_trigger_unregister(data->mag_dready_trig);
1413 err_trigger_unregister_acc_dready:
1414 iio_trigger_unregister(data->acc_dready_trig);
1415 err_chip_uninit:
1416 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1417 return ret;
1418 }
1419
kmx61_remove(struct i2c_client * client)1420 static void kmx61_remove(struct i2c_client *client)
1421 {
1422 struct kmx61_data *data = i2c_get_clientdata(client);
1423
1424 iio_device_unregister(data->acc_indio_dev);
1425 iio_device_unregister(data->mag_indio_dev);
1426
1427 pm_runtime_disable(&client->dev);
1428 pm_runtime_set_suspended(&client->dev);
1429
1430 if (client->irq > 0) {
1431 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1432 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1433 iio_trigger_unregister(data->acc_dready_trig);
1434 iio_trigger_unregister(data->mag_dready_trig);
1435 iio_trigger_unregister(data->motion_trig);
1436 }
1437
1438 mutex_lock(&data->lock);
1439 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1440 mutex_unlock(&data->lock);
1441 }
1442
kmx61_suspend(struct device * dev)1443 static int kmx61_suspend(struct device *dev)
1444 {
1445 int ret;
1446 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1447
1448 mutex_lock(&data->lock);
1449 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1450 false);
1451 mutex_unlock(&data->lock);
1452
1453 return ret;
1454 }
1455
kmx61_resume(struct device * dev)1456 static int kmx61_resume(struct device *dev)
1457 {
1458 u8 stby = 0;
1459 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1460
1461 if (data->acc_stby)
1462 stby |= KMX61_ACC_STBY_BIT;
1463 if (data->mag_stby)
1464 stby |= KMX61_MAG_STBY_BIT;
1465
1466 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1467 }
1468
kmx61_runtime_suspend(struct device * dev)1469 static int kmx61_runtime_suspend(struct device *dev)
1470 {
1471 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1472 int ret;
1473
1474 mutex_lock(&data->lock);
1475 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1476 mutex_unlock(&data->lock);
1477
1478 return ret;
1479 }
1480
kmx61_runtime_resume(struct device * dev)1481 static int kmx61_runtime_resume(struct device *dev)
1482 {
1483 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1484 u8 stby = 0;
1485
1486 if (!data->acc_ps)
1487 stby |= KMX61_ACC_STBY_BIT;
1488 if (!data->mag_ps)
1489 stby |= KMX61_MAG_STBY_BIT;
1490
1491 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1492 }
1493
1494 static const struct dev_pm_ops kmx61_pm_ops = {
1495 SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
1496 RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1497 };
1498
1499 static const struct acpi_device_id kmx61_acpi_match[] = {
1500 {"KMX61021", 0},
1501 {}
1502 };
1503
1504 MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
1505
1506 static const struct i2c_device_id kmx61_id[] = {
1507 { "kmx611021" },
1508 {}
1509 };
1510
1511 MODULE_DEVICE_TABLE(i2c, kmx61_id);
1512
1513 static struct i2c_driver kmx61_driver = {
1514 .driver = {
1515 .name = KMX61_DRV_NAME,
1516 .acpi_match_table = kmx61_acpi_match,
1517 .pm = pm_ptr(&kmx61_pm_ops),
1518 },
1519 .probe = kmx61_probe,
1520 .remove = kmx61_remove,
1521 .id_table = kmx61_id,
1522 };
1523
1524 module_i2c_driver(kmx61_driver);
1525
1526 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1527 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1528 MODULE_LICENSE("GPL v2");
1529