blob: e5306b4e020efca5ea45b7e20698d07053ef160d [file] [log] [blame]
Daniel Baluta20ffac22014-12-03 15:31:48 +02001/*
2 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
3 *
4 * Copyright (c) 2014, Intel Corporation.
5 *
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.
9 *
10 * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/i2c.h>
Daniel Balutab25862c2014-12-03 15:31:49 +020016#include <linux/acpi.h>
17#include <linux/gpio/consumer.h>
Daniel Balutaaff86092014-12-03 15:31:50 +020018#include <linux/interrupt.h>
Daniel Baluta3b9c40e2014-12-03 15:31:51 +020019#include <linux/pm.h>
Daniel Balutaaff86092014-12-03 15:31:50 +020020#include <linux/pm_runtime.h>
Daniel Baluta20ffac22014-12-03 15:31:48 +020021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
Daniel Balutafd3ae7a2014-12-03 15:31:53 +020023#include <linux/iio/events.h>
Daniel Balutac3a23ecc2014-12-03 15:31:52 +020024#include <linux/iio/trigger.h>
25#include <linux/iio/buffer.h>
26#include <linux/iio/triggered_buffer.h>
27#include <linux/iio/trigger_consumer.h>
Daniel Baluta20ffac22014-12-03 15:31:48 +020028
29#define KMX61_DRV_NAME "kmx61"
Daniel Balutac3a23ecc2014-12-03 15:31:52 +020030#define KMX61_IRQ_NAME "kmx61_event"
Daniel Baluta20ffac22014-12-03 15:31:48 +020031
32#define KMX61_REG_WHO_AM_I 0x00
Daniel Balutafd3ae7a2014-12-03 15:31:53 +020033#define KMX61_REG_INS1 0x01
34#define KMX61_REG_INS2 0x02
Daniel Baluta20ffac22014-12-03 15:31:48 +020035
36/*
37 * three 16-bit accelerometer output registers for X/Y/Z axis
38 * we use only XOUT_L as a base register, all other addresses
39 * can be obtained by applying an offset and are provided here
40 * only for clarity.
41 */
42#define KMX61_ACC_XOUT_L 0x0A
43#define KMX61_ACC_XOUT_H 0x0B
44#define KMX61_ACC_YOUT_L 0x0C
45#define KMX61_ACC_YOUT_H 0x0D
46#define KMX61_ACC_ZOUT_L 0x0E
47#define KMX61_ACC_ZOUT_H 0x0F
48
49/*
50 * one 16-bit temperature output register
51 */
52#define KMX61_TEMP_L 0x10
53#define KMX61_TEMP_H 0x11
54
55/*
56 * three 16-bit magnetometer output registers for X/Y/Z axis
57 */
58#define KMX61_MAG_XOUT_L 0x12
59#define KMX61_MAG_XOUT_H 0x13
60#define KMX61_MAG_YOUT_L 0x14
61#define KMX61_MAG_YOUT_H 0x15
62#define KMX61_MAG_ZOUT_L 0x16
63#define KMX61_MAG_ZOUT_H 0x17
64
Daniel Balutac3a23ecc2014-12-03 15:31:52 +020065#define KMX61_REG_INL 0x28
Daniel Baluta20ffac22014-12-03 15:31:48 +020066#define KMX61_REG_STBY 0x29
67#define KMX61_REG_CTRL1 0x2A
Daniel Balutafd3ae7a2014-12-03 15:31:53 +020068#define KMX61_REG_CTRL2 0x2B
Daniel Baluta20ffac22014-12-03 15:31:48 +020069#define KMX61_REG_ODCNTL 0x2C
Daniel Balutac3a23ecc2014-12-03 15:31:52 +020070#define KMX61_REG_INC1 0x2D
Daniel Baluta20ffac22014-12-03 15:31:48 +020071
Daniel Balutafd3ae7a2014-12-03 15:31:53 +020072#define KMX61_REG_WUF_THRESH 0x3D
73#define KMX61_REG_WUF_TIMER 0x3E
74
Daniel Baluta20ffac22014-12-03 15:31:48 +020075#define KMX61_ACC_STBY_BIT BIT(0)
76#define KMX61_MAG_STBY_BIT BIT(1)
77#define KMX61_ACT_STBY_BIT BIT(7)
78
79#define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
80
Daniel Balutafd3ae7a2014-12-03 15:31:53 +020081#define KMX61_REG_INS1_BIT_WUFS BIT(1)
82
83#define KMX61_REG_INS2_BIT_ZP BIT(0)
84#define KMX61_REG_INS2_BIT_ZN BIT(1)
85#define KMX61_REG_INS2_BIT_YP BIT(2)
86#define KMX61_REG_INS2_BIT_YN BIT(3)
87#define KMX61_REG_INS2_BIT_XP BIT(4)
88#define KMX61_REG_INS2_BIT_XN BIT(5)
89
Daniel Baluta20ffac22014-12-03 15:31:48 +020090#define KMX61_REG_CTRL1_GSEL_MASK 0x03
91
Daniel Balutac3a23ecc2014-12-03 15:31:52 +020092#define KMX61_REG_CTRL1_BIT_RES BIT(4)
93#define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
Daniel Balutafd3ae7a2014-12-03 15:31:53 +020094#define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
95#define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
Daniel Balutac3a23ecc2014-12-03 15:31:52 +020096
Daniel Balutafd3ae7a2014-12-03 15:31:53 +020097#define KMX61_REG_INC1_BIT_WUFS BIT(0)
Daniel Balutac3a23ecc2014-12-03 15:31:52 +020098#define KMX61_REG_INC1_BIT_DRDYM BIT(1)
99#define KMX61_REG_INC1_BIT_DRDYA BIT(2)
100#define KMX61_REG_INC1_BIT_IEN BIT(5)
101
Daniel Baluta20ffac22014-12-03 15:31:48 +0200102#define KMX61_ACC_ODR_SHIFT 0
103#define KMX61_MAG_ODR_SHIFT 4
104#define KMX61_ACC_ODR_MASK 0x0F
105#define KMX61_MAG_ODR_MASK 0xF0
106
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200107#define KMX61_OWUF_MASK 0x7
108
109#define KMX61_DEFAULT_WAKE_THRESH 1
110#define KMX61_DEFAULT_WAKE_DURATION 1
111
Daniel Balutaaff86092014-12-03 15:31:50 +0200112#define KMX61_SLEEP_DELAY_MS 2000
113
Daniel Baluta20ffac22014-12-03 15:31:48 +0200114#define KMX61_CHIP_ID 0x12
115
116/* KMX61 devices */
117#define KMX61_ACC 0x01
118#define KMX61_MAG 0x02
119
120struct kmx61_data {
121 struct i2c_client *client;
122
123 /* serialize access to non-atomic ops, e.g set_mode */
124 struct mutex lock;
125
126 /* standby state */
127 bool acc_stby;
128 bool mag_stby;
129
Daniel Balutaaff86092014-12-03 15:31:50 +0200130 /* power state */
131 bool acc_ps;
132 bool mag_ps;
133
Daniel Baluta20ffac22014-12-03 15:31:48 +0200134 /* config bits */
135 u8 range;
136 u8 odr_bits;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200137 u8 wake_thresh;
138 u8 wake_duration;
Daniel Baluta20ffac22014-12-03 15:31:48 +0200139
140 /* accelerometer specific data */
141 struct iio_dev *acc_indio_dev;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +0200142 struct iio_trigger *acc_dready_trig;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200143 struct iio_trigger *motion_trig;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +0200144 bool acc_dready_trig_on;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200145 bool motion_trig_on;
146 bool ev_enable_state;
Daniel Baluta20ffac22014-12-03 15:31:48 +0200147
148 /* magnetometer specific data */
149 struct iio_dev *mag_indio_dev;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +0200150 struct iio_trigger *mag_dready_trig;
151 bool mag_dready_trig_on;
Daniel Baluta20ffac22014-12-03 15:31:48 +0200152};
153
154enum kmx61_range {
155 KMX61_RANGE_2G,
156 KMX61_RANGE_4G,
157 KMX61_RANGE_8G,
158};
159
160enum kmx61_axis {
161 KMX61_AXIS_X,
162 KMX61_AXIS_Y,
163 KMX61_AXIS_Z,
164};
165
166static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
167
168static const struct {
169 int val;
170 int val2;
Daniel Balutaa36385a2015-01-05 11:21:42 +0200171} kmx61_samp_freq_table[] = { {12, 500000},
172 {25, 0},
173 {50, 0},
174 {100, 0},
175 {200, 0},
176 {400, 0},
177 {800, 0},
178 {1600, 0},
179 {0, 781000},
180 {1, 563000},
181 {3, 125000},
182 {6, 250000} };
Daniel Baluta20ffac22014-12-03 15:31:48 +0200183
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200184static const struct {
185 int val;
186 int val2;
187 int odr_bits;
188} kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
189 {1, 563000, 0x01},
190 {3, 125000, 0x02},
191 {6, 250000, 0x03},
192 {12, 500000, 0x04},
193 {25, 0, 0x05},
194 {50, 0, 0x06},
195 {100, 0, 0x06},
196 {200, 0, 0x06},
197 {400, 0, 0x06},
198 {800, 0, 0x06},
199 {1600, 0, 0x06} };
200
Daniel Baluta20ffac22014-12-03 15:31:48 +0200201static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
202static IIO_CONST_ATTR(magn_scale_available, "0.001465");
203static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
204 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
205
206static struct attribute *kmx61_acc_attributes[] = {
207 &iio_const_attr_accel_scale_available.dev_attr.attr,
208 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
209 NULL,
210};
211
212static struct attribute *kmx61_mag_attributes[] = {
213 &iio_const_attr_magn_scale_available.dev_attr.attr,
214 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
215 NULL,
216};
217
218static const struct attribute_group kmx61_acc_attribute_group = {
219 .attrs = kmx61_acc_attributes,
220};
221
222static const struct attribute_group kmx61_mag_attribute_group = {
223 .attrs = kmx61_mag_attributes,
224};
225
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200226static const struct iio_event_spec kmx61_event = {
227 .type = IIO_EV_TYPE_THRESH,
228 .dir = IIO_EV_DIR_EITHER,
229 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
230 BIT(IIO_EV_INFO_ENABLE) |
231 BIT(IIO_EV_INFO_PERIOD),
232};
233
Daniel Baluta20ffac22014-12-03 15:31:48 +0200234#define KMX61_ACC_CHAN(_axis) { \
235 .type = IIO_ACCEL, \
236 .modified = 1, \
237 .channel2 = IIO_MOD_ ## _axis, \
238 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
239 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
240 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
241 .address = KMX61_ACC, \
242 .scan_index = KMX61_AXIS_ ## _axis, \
243 .scan_type = { \
244 .sign = 's', \
245 .realbits = 12, \
246 .storagebits = 16, \
247 .shift = 4, \
248 .endianness = IIO_LE, \
249 }, \
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200250 .event_spec = &kmx61_event, \
251 .num_event_specs = 1 \
Daniel Baluta20ffac22014-12-03 15:31:48 +0200252}
253
254#define KMX61_MAG_CHAN(_axis) { \
255 .type = IIO_MAGN, \
256 .modified = 1, \
257 .channel2 = IIO_MOD_ ## _axis, \
258 .address = KMX61_MAG, \
259 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
260 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
261 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
262 .scan_index = KMX61_AXIS_ ## _axis, \
263 .scan_type = { \
264 .sign = 's', \
265 .realbits = 14, \
266 .storagebits = 16, \
267 .shift = 2, \
268 .endianness = IIO_LE, \
269 }, \
270}
271
272static const struct iio_chan_spec kmx61_acc_channels[] = {
273 KMX61_ACC_CHAN(X),
274 KMX61_ACC_CHAN(Y),
275 KMX61_ACC_CHAN(Z),
276};
277
278static const struct iio_chan_spec kmx61_mag_channels[] = {
279 KMX61_MAG_CHAN(X),
280 KMX61_MAG_CHAN(Y),
281 KMX61_MAG_CHAN(Z),
282};
283
284static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
285{
286 struct kmx61_data **priv = iio_priv(indio_dev);
287
288 *priv = data;
289}
290
291static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
292{
293 return *(struct kmx61_data **)iio_priv(indio_dev);
294}
295
296static int kmx61_convert_freq_to_bit(int val, int val2)
297{
298 int i;
299
300 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
301 if (val == kmx61_samp_freq_table[i].val &&
302 val2 == kmx61_samp_freq_table[i].val2)
Daniel Balutaa36385a2015-01-05 11:21:42 +0200303 return i;
Daniel Baluta20ffac22014-12-03 15:31:48 +0200304 return -EINVAL;
305}
306
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200307static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
308{
309 int i;
310
311 for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
312 if (kmx61_wake_up_odr_table[i].val == val &&
313 kmx61_wake_up_odr_table[i].val2 == val2)
314 return kmx61_wake_up_odr_table[i].odr_bits;
315 return -EINVAL;
316}
317
Daniel Baluta20ffac22014-12-03 15:31:48 +0200318/**
319 * kmx61_set_mode() - set KMX61 device operating mode
320 * @data - kmx61 device private data pointer
321 * @mode - bitmask, indicating operating mode for @device
322 * @device - bitmask, indicating device for which @mode needs to be set
323 * @update - update stby bits stored in device's private @data
324 *
325 * For each sensor (accelerometer/magnetometer) there are two operating modes
326 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
327 * if they are both enabled. Internal sensors state is saved in acc_stby and
328 * mag_stby members of driver's private @data.
329 */
330static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
331 bool update)
332{
333 int ret;
334 int acc_stby = -1, mag_stby = -1;
335
336 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
337 if (ret < 0) {
338 dev_err(&data->client->dev, "Error reading reg_stby\n");
339 return ret;
340 }
341 if (device & KMX61_ACC) {
342 if (mode & KMX61_ACC_STBY_BIT) {
343 ret |= KMX61_ACC_STBY_BIT;
344 acc_stby = 1;
345 } else {
346 ret &= ~KMX61_ACC_STBY_BIT;
347 acc_stby = 0;
348 }
349 }
350
351 if (device & KMX61_MAG) {
352 if (mode & KMX61_MAG_STBY_BIT) {
353 ret |= KMX61_MAG_STBY_BIT;
354 mag_stby = 1;
355 } else {
356 ret &= ~KMX61_MAG_STBY_BIT;
357 mag_stby = 0;
358 }
359 }
360
361 if (mode & KMX61_ACT_STBY_BIT)
362 ret |= KMX61_ACT_STBY_BIT;
363
364 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
365 if (ret < 0) {
366 dev_err(&data->client->dev, "Error writing reg_stby\n");
367 return ret;
368 }
369
370 if (acc_stby != -1 && update)
371 data->acc_stby = acc_stby;
372 if (mag_stby != -1 && update)
373 data->mag_stby = mag_stby;
374
375 return 0;
376}
377
378static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
379{
380 int ret;
381
382 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
383 if (ret < 0) {
384 dev_err(&data->client->dev, "Error reading reg_stby\n");
385 return ret;
386 }
387 *mode = 0;
388
389 if (device & KMX61_ACC) {
390 if (ret & KMX61_ACC_STBY_BIT)
391 *mode |= KMX61_ACC_STBY_BIT;
392 else
393 *mode &= ~KMX61_ACC_STBY_BIT;
394 }
395
396 if (device & KMX61_MAG) {
397 if (ret & KMX61_MAG_STBY_BIT)
398 *mode |= KMX61_MAG_STBY_BIT;
399 else
400 *mode &= ~KMX61_MAG_STBY_BIT;
401 }
402
403 return 0;
404}
405
kbuild test robotebd16362014-12-13 00:28:52 +0800406static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200407{
408 int ret, odr_bits;
409
410 odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
411 if (odr_bits < 0)
412 return odr_bits;
413
414 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
415 odr_bits);
416 if (ret < 0)
417 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
418 return ret;
419}
420
Daniel Baluta20ffac22014-12-03 15:31:48 +0200421static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
422{
423 int ret;
424 u8 mode;
425 int lodr_bits, odr_bits;
426
427 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
428 if (ret < 0)
429 return ret;
430
431 lodr_bits = kmx61_convert_freq_to_bit(val, val2);
432 if (lodr_bits < 0)
433 return lodr_bits;
434
435 /* To change ODR, accel and magn must be in STDBY */
436 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
437 true);
438 if (ret < 0)
439 return ret;
440
441 odr_bits = 0;
442 if (device & KMX61_ACC)
443 odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
444 if (device & KMX61_MAG)
445 odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
446
447 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
448 odr_bits);
449 if (ret < 0)
450 return ret;
451
Daniel Baluta4e843972014-12-23 15:22:24 +0200452 data->odr_bits = odr_bits;
453
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200454 if (device & KMX61_ACC) {
455 ret = kmx61_set_wake_up_odr(data, val, val2);
456 if (ret)
457 return ret;
458 }
459
Daniel Baluta20ffac22014-12-03 15:31:48 +0200460 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
461}
462
463static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
464 u8 device)
Daniel Balutaa36385a2015-01-05 11:21:42 +0200465{
Daniel Baluta20ffac22014-12-03 15:31:48 +0200466 u8 lodr_bits;
467
468 if (device & KMX61_ACC)
469 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
470 KMX61_ACC_ODR_MASK;
471 else if (device & KMX61_MAG)
472 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
473 KMX61_MAG_ODR_MASK;
474 else
475 return -EINVAL;
476
Daniel Balutaa36385a2015-01-05 11:21:42 +0200477 if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
478 return -EINVAL;
479
480 *val = kmx61_samp_freq_table[lodr_bits].val;
481 *val2 = kmx61_samp_freq_table[lodr_bits].val2;
482
483 return 0;
Daniel Baluta20ffac22014-12-03 15:31:48 +0200484}
485
486static int kmx61_set_range(struct kmx61_data *data, u8 range)
487{
488 int ret;
489
490 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
491 if (ret < 0) {
492 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
493 return ret;
494 }
495
496 ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
497 ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
498
499 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
500 if (ret < 0) {
501 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
502 return ret;
503 }
504
505 data->range = range;
506
507 return 0;
508}
509
510static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
511{
512 int ret, i;
513 u8 mode;
514
515 for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
516 if (kmx61_uscale_table[i] == uscale) {
517 ret = kmx61_get_mode(data, &mode,
518 KMX61_ACC | KMX61_MAG);
519 if (ret < 0)
520 return ret;
521
522 ret = kmx61_set_mode(data, KMX61_ALL_STBY,
523 KMX61_ACC | KMX61_MAG, true);
524 if (ret < 0)
525 return ret;
526
527 ret = kmx61_set_range(data, i);
528 if (ret < 0)
529 return ret;
530
531 return kmx61_set_mode(data, mode,
532 KMX61_ACC | KMX61_MAG, true);
533 }
534 }
535 return -EINVAL;
536}
537
538static int kmx61_chip_init(struct kmx61_data *data)
539{
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200540 int ret, val, val2;
Daniel Baluta20ffac22014-12-03 15:31:48 +0200541
542 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
543 if (ret < 0) {
544 dev_err(&data->client->dev, "Error reading who_am_i\n");
545 return ret;
546 }
547
548 if (ret != KMX61_CHIP_ID) {
549 dev_err(&data->client->dev,
550 "Wrong chip id, got %x expected %x\n",
551 ret, KMX61_CHIP_ID);
552 return -EINVAL;
553 }
554
555 /* set accel 12bit, 4g range */
556 ret = kmx61_set_range(data, KMX61_RANGE_4G);
557 if (ret < 0)
558 return ret;
559
560 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
561 if (ret < 0) {
562 dev_err(&data->client->dev, "Error reading reg_odcntl\n");
563 return ret;
564 }
565 data->odr_bits = ret;
566
Daniel Balutaa36385a2015-01-05 11:21:42 +0200567 /*
568 * set output data rate for wake up (motion detection) function
569 * to match data rate for accelerometer sampling
570 */
571 ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200572 if (ret < 0)
573 return ret;
574
575 ret = kmx61_set_wake_up_odr(data, val, val2);
576 if (ret < 0)
577 return ret;
578
Daniel Baluta20ffac22014-12-03 15:31:48 +0200579 /* set acc/magn to OPERATION mode */
580 ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
581 if (ret < 0)
582 return ret;
583
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200584 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
585 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
586
Daniel Baluta20ffac22014-12-03 15:31:48 +0200587 return 0;
588}
589
Daniel Balutac3a23ecc2014-12-03 15:31:52 +0200590static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
591 bool status, u8 device)
592{
593 u8 mode;
594 int ret;
595
596 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
597 if (ret < 0)
598 return ret;
599
600 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
601 if (ret < 0)
602 return ret;
603
604 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
605 if (ret < 0) {
606 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
607 return ret;
608 }
609
610 if (status) {
611 ret |= KMX61_REG_INC1_BIT_IEN;
612 if (device & KMX61_ACC)
613 ret |= KMX61_REG_INC1_BIT_DRDYA;
614 if (device & KMX61_MAG)
615 ret |= KMX61_REG_INC1_BIT_DRDYM;
616 } else {
617 ret &= ~KMX61_REG_INC1_BIT_IEN;
618 if (device & KMX61_ACC)
619 ret &= ~KMX61_REG_INC1_BIT_DRDYA;
620 if (device & KMX61_MAG)
621 ret &= ~KMX61_REG_INC1_BIT_DRDYM;
622 }
623 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
624 if (ret < 0) {
625 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
626 return ret;
627 }
628
629 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
630 if (ret < 0) {
631 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
632 return ret;
633 }
634
635 if (status)
636 ret |= KMX61_REG_CTRL1_BIT_DRDYE;
637 else
638 ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
639
640 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
641 if (ret < 0) {
642 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
643 return ret;
644 }
645
Daniel Baluta28ff3442014-12-23 15:22:26 +0200646 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
Daniel Balutac3a23ecc2014-12-03 15:31:52 +0200647}
648
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200649static int kmx61_chip_update_thresholds(struct kmx61_data *data)
650{
651 int ret;
652
653 ret = i2c_smbus_write_byte_data(data->client,
654 KMX61_REG_WUF_TIMER,
655 data->wake_duration);
656 if (ret < 0) {
657 dev_err(&data->client->dev, "Errow writing reg_wuf_timer\n");
658 return ret;
659 }
660
661 ret = i2c_smbus_write_byte_data(data->client,
662 KMX61_REG_WUF_THRESH,
663 data->wake_thresh);
Daniel Baluta28ff3442014-12-23 15:22:26 +0200664 if (ret < 0)
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200665 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200666
Daniel Baluta28ff3442014-12-23 15:22:26 +0200667 return ret;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200668}
669
670static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
Daniel Baluta0475c682014-12-23 15:22:28 +0200671 bool status)
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200672{
673 u8 mode;
674 int ret;
675
676 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
677 if (ret < 0)
678 return ret;
679
680 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
681 if (ret < 0)
682 return ret;
683
684 ret = kmx61_chip_update_thresholds(data);
685 if (ret < 0)
686 return ret;
687
688 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
689 if (ret < 0) {
690 dev_err(&data->client->dev, "Error reading reg_inc1\n");
691 return ret;
692 }
693 if (status)
694 ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
695 else
696 ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
697
698 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
699 if (ret < 0) {
700 dev_err(&data->client->dev, "Error writing reg_inc1\n");
701 return ret;
702 }
703
704 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
705 if (ret < 0) {
706 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
707 return ret;
708 }
709
710 if (status)
711 ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
712 else
713 ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
714
715 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
716 if (ret < 0) {
717 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
718 return ret;
719 }
720 mode |= KMX61_ACT_STBY_BIT;
Daniel Baluta28ff3442014-12-23 15:22:26 +0200721 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200722}
723
Daniel Balutaaff86092014-12-03 15:31:50 +0200724/**
725 * kmx61_set_power_state() - set power state for kmx61 @device
726 * @data - kmx61 device private pointer
727 * @on - power state to be set for @device
728 * @device - bitmask indicating device for which @on state needs to be set
729 *
730 * Notice that when ACC power state needs to be set to ON and MAG is in
731 * OPERATION then we know that kmx61_runtime_resume was already called
732 * so we must set ACC OPERATION mode here. The same happens when MAG power
733 * state needs to be set to ON and ACC is in OPERATION.
734 */
735static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
736{
Daniel Balutadf1fad82015-01-09 11:39:30 +0200737#ifdef CONFIG_PM
Daniel Balutaaff86092014-12-03 15:31:50 +0200738 int ret;
739
740 if (device & KMX61_ACC) {
741 if (on && !data->acc_ps && !data->mag_stby) {
742 ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
743 if (ret < 0)
744 return ret;
745 }
746 data->acc_ps = on;
747 }
748 if (device & KMX61_MAG) {
749 if (on && !data->mag_ps && !data->acc_stby) {
750 ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
751 if (ret < 0)
752 return ret;
753 }
754 data->mag_ps = on;
755 }
756
757 if (on) {
758 ret = pm_runtime_get_sync(&data->client->dev);
759 } else {
760 pm_runtime_mark_last_busy(&data->client->dev);
761 ret = pm_runtime_put_autosuspend(&data->client->dev);
762 }
763 if (ret < 0) {
764 dev_err(&data->client->dev,
765 "Failed: kmx61_set_power_state for %d, ret %d\n",
766 on, ret);
767 if (on)
768 pm_runtime_put_noidle(&data->client->dev);
769
770 return ret;
771 }
772#endif
773 return 0;
774}
775
Daniel Baluta20ffac22014-12-03 15:31:48 +0200776static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
777{
778 int ret;
779 u8 reg = base + offset * 2;
780
781 ret = i2c_smbus_read_word_data(data->client, reg);
782 if (ret < 0)
783 dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
784
785 return ret;
786}
787
788static int kmx61_read_raw(struct iio_dev *indio_dev,
789 struct iio_chan_spec const *chan, int *val,
790 int *val2, long mask)
791{
792 int ret;
793 u8 base_reg;
794 struct kmx61_data *data = kmx61_get_data(indio_dev);
795
796 switch (mask) {
797 case IIO_CHAN_INFO_RAW:
798 switch (chan->type) {
799 case IIO_ACCEL:
800 base_reg = KMX61_ACC_XOUT_L;
801 break;
802 case IIO_MAGN:
803 base_reg = KMX61_MAG_XOUT_L;
804 break;
805 default:
806 return -EINVAL;
807 }
808 mutex_lock(&data->lock);
809
Daniel Balutaa3da4fa2014-12-23 15:22:25 +0200810 ret = kmx61_set_power_state(data, true, chan->address);
811 if (ret) {
812 mutex_unlock(&data->lock);
813 return ret;
814 }
815
Daniel Baluta20ffac22014-12-03 15:31:48 +0200816 ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
817 if (ret < 0) {
Daniel Balutaaff86092014-12-03 15:31:50 +0200818 kmx61_set_power_state(data, false, chan->address);
Daniel Baluta20ffac22014-12-03 15:31:48 +0200819 mutex_unlock(&data->lock);
820 return ret;
821 }
822 *val = sign_extend32(ret >> chan->scan_type.shift,
823 chan->scan_type.realbits - 1);
Daniel Balutaa3da4fa2014-12-23 15:22:25 +0200824 ret = kmx61_set_power_state(data, false, chan->address);
Daniel Baluta20ffac22014-12-03 15:31:48 +0200825
826 mutex_unlock(&data->lock);
Daniel Balutaa3da4fa2014-12-23 15:22:25 +0200827 if (ret)
828 return ret;
Daniel Baluta20ffac22014-12-03 15:31:48 +0200829 return IIO_VAL_INT;
830 case IIO_CHAN_INFO_SCALE:
831 switch (chan->type) {
832 case IIO_ACCEL:
833 *val = 0;
834 *val2 = kmx61_uscale_table[data->range];
835 return IIO_VAL_INT_PLUS_MICRO;
836 case IIO_MAGN:
837 /* 14 bits res, 1465 microGauss per magn count */
838 *val = 0;
839 *val2 = 1465;
840 return IIO_VAL_INT_PLUS_MICRO;
841 default:
842 return -EINVAL;
843 }
844 case IIO_CHAN_INFO_SAMP_FREQ:
845 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
846 return -EINVAL;
847
848 mutex_lock(&data->lock);
849 ret = kmx61_get_odr(data, val, val2, chan->address);
850 mutex_unlock(&data->lock);
851 if (ret)
852 return -EINVAL;
853 return IIO_VAL_INT_PLUS_MICRO;
854 }
855 return -EINVAL;
856}
857
858static int kmx61_write_raw(struct iio_dev *indio_dev,
859 struct iio_chan_spec const *chan, int val,
860 int val2, long mask)
861{
862 int ret;
863 struct kmx61_data *data = kmx61_get_data(indio_dev);
864
865 switch (mask) {
866 case IIO_CHAN_INFO_SAMP_FREQ:
867 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
868 return -EINVAL;
869
870 mutex_lock(&data->lock);
871 ret = kmx61_set_odr(data, val, val2, chan->address);
872 mutex_unlock(&data->lock);
873 return ret;
874 case IIO_CHAN_INFO_SCALE:
875 switch (chan->type) {
876 case IIO_ACCEL:
877 if (val != 0)
878 return -EINVAL;
879 mutex_lock(&data->lock);
880 ret = kmx61_set_scale(data, val2);
881 mutex_unlock(&data->lock);
882 return ret;
883 default:
884 return -EINVAL;
885 }
886 default:
887 return -EINVAL;
888 }
889}
890
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200891static int kmx61_read_event(struct iio_dev *indio_dev,
892 const struct iio_chan_spec *chan,
893 enum iio_event_type type,
894 enum iio_event_direction dir,
895 enum iio_event_info info,
896 int *val, int *val2)
897{
898 struct kmx61_data *data = kmx61_get_data(indio_dev);
899
900 *val2 = 0;
901 switch (info) {
902 case IIO_EV_INFO_VALUE:
903 *val = data->wake_thresh;
Daniel Baluta28ff3442014-12-23 15:22:26 +0200904 return IIO_VAL_INT;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200905 case IIO_EV_INFO_PERIOD:
906 *val = data->wake_duration;
Daniel Baluta28ff3442014-12-23 15:22:26 +0200907 return IIO_VAL_INT;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200908 default:
909 return -EINVAL;
910 }
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200911}
912
913static int kmx61_write_event(struct iio_dev *indio_dev,
Daniel Balutadfb12ed2014-12-23 15:22:27 +0200914 const struct iio_chan_spec *chan,
915 enum iio_event_type type,
916 enum iio_event_direction dir,
917 enum iio_event_info info,
918 int val, int val2)
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200919{
920 struct kmx61_data *data = kmx61_get_data(indio_dev);
921
922 if (data->ev_enable_state)
923 return -EBUSY;
924
925 switch (info) {
926 case IIO_EV_INFO_VALUE:
927 data->wake_thresh = val;
Daniel Baluta28ff3442014-12-23 15:22:26 +0200928 return IIO_VAL_INT;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200929 case IIO_EV_INFO_PERIOD:
930 data->wake_duration = val;
Daniel Baluta28ff3442014-12-23 15:22:26 +0200931 return IIO_VAL_INT;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200932 default:
933 return -EINVAL;
934 }
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200935}
936
937static int kmx61_read_event_config(struct iio_dev *indio_dev,
938 const struct iio_chan_spec *chan,
939 enum iio_event_type type,
940 enum iio_event_direction dir)
941{
942 struct kmx61_data *data = kmx61_get_data(indio_dev);
943
944 return data->ev_enable_state;
945}
946
947static int kmx61_write_event_config(struct iio_dev *indio_dev,
Daniel Balutadfb12ed2014-12-23 15:22:27 +0200948 const struct iio_chan_spec *chan,
949 enum iio_event_type type,
950 enum iio_event_direction dir,
951 int state)
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200952{
953 struct kmx61_data *data = kmx61_get_data(indio_dev);
Daniel Baluta28ff3442014-12-23 15:22:26 +0200954 int ret = 0;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200955
956 if (state && data->ev_enable_state)
957 return 0;
958
959 mutex_lock(&data->lock);
960
961 if (!state && data->motion_trig_on) {
Daniel Balutad4a4ae02014-12-23 15:22:29 +0200962 data->ev_enable_state = false;
Daniel Baluta28ff3442014-12-23 15:22:26 +0200963 goto err_unlock;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200964 }
965
966 ret = kmx61_set_power_state(data, state, KMX61_ACC);
Daniel Baluta28ff3442014-12-23 15:22:26 +0200967 if (ret < 0)
968 goto err_unlock;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200969
Daniel Baluta0475c682014-12-23 15:22:28 +0200970 ret = kmx61_setup_any_motion_interrupt(data, state);
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200971 if (ret < 0) {
972 kmx61_set_power_state(data, false, KMX61_ACC);
Daniel Baluta28ff3442014-12-23 15:22:26 +0200973 goto err_unlock;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200974 }
975
976 data->ev_enable_state = state;
Daniel Baluta28ff3442014-12-23 15:22:26 +0200977
978err_unlock:
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200979 mutex_unlock(&data->lock);
980
Daniel Baluta28ff3442014-12-23 15:22:26 +0200981 return ret;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200982}
983
Daniel Balutac3a23ecc2014-12-03 15:31:52 +0200984static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
985 struct iio_trigger *trig)
986{
987 struct kmx61_data *data = kmx61_get_data(indio_dev);
988
Daniel Balutafd3ae7a2014-12-03 15:31:53 +0200989 if (data->acc_dready_trig != trig && data->motion_trig != trig)
Daniel Balutac3a23ecc2014-12-03 15:31:52 +0200990 return -EINVAL;
991
992 return 0;
993}
994
995static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
996 struct iio_trigger *trig)
997{
998 struct kmx61_data *data = kmx61_get_data(indio_dev);
999
1000 if (data->mag_dready_trig != trig)
1001 return -EINVAL;
1002
1003 return 0;
1004}
1005
Daniel Baluta20ffac22014-12-03 15:31:48 +02001006static const struct iio_info kmx61_acc_info = {
1007 .driver_module = THIS_MODULE,
1008 .read_raw = kmx61_read_raw,
1009 .write_raw = kmx61_write_raw,
1010 .attrs = &kmx61_acc_attribute_group,
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001011 .read_event_value = kmx61_read_event,
1012 .write_event_value = kmx61_write_event,
1013 .read_event_config = kmx61_read_event_config,
1014 .write_event_config = kmx61_write_event_config,
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001015 .validate_trigger = kmx61_acc_validate_trigger,
Daniel Baluta20ffac22014-12-03 15:31:48 +02001016};
1017
1018static const struct iio_info kmx61_mag_info = {
1019 .driver_module = THIS_MODULE,
1020 .read_raw = kmx61_read_raw,
1021 .write_raw = kmx61_write_raw,
1022 .attrs = &kmx61_mag_attribute_group,
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001023 .validate_trigger = kmx61_mag_validate_trigger,
Daniel Baluta20ffac22014-12-03 15:31:48 +02001024};
1025
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001026
1027static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
1028 bool state)
1029{
1030 int ret = 0;
1031 u8 device;
1032
1033 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001034 struct kmx61_data *data = kmx61_get_data(indio_dev);
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001035
1036 mutex_lock(&data->lock);
1037
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001038 if (!state && data->ev_enable_state && data->motion_trig_on) {
1039 data->motion_trig_on = false;
Daniel Baluta28ff3442014-12-23 15:22:26 +02001040 goto err_unlock;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001041 }
1042
Daniel Balutadbdd0e22014-12-23 15:22:30 +02001043 if (data->acc_dready_trig == trig || data->motion_trig == trig)
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001044 device = KMX61_ACC;
1045 else
1046 device = KMX61_MAG;
1047
1048 ret = kmx61_set_power_state(data, state, device);
Daniel Baluta28ff3442014-12-23 15:22:26 +02001049 if (ret < 0)
1050 goto err_unlock;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001051
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001052 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1053 ret = kmx61_setup_new_data_interrupt(data, state, device);
1054 else
Daniel Baluta0475c682014-12-23 15:22:28 +02001055 ret = kmx61_setup_any_motion_interrupt(data, state);
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001056 if (ret < 0) {
1057 kmx61_set_power_state(data, false, device);
Daniel Baluta28ff3442014-12-23 15:22:26 +02001058 goto err_unlock;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001059 }
1060
1061 if (data->acc_dready_trig == trig)
1062 data->acc_dready_trig_on = state;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001063 else if (data->mag_dready_trig == trig)
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001064 data->mag_dready_trig_on = state;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001065 else
1066 data->motion_trig_on = state;
Daniel Baluta28ff3442014-12-23 15:22:26 +02001067err_unlock:
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001068 mutex_unlock(&data->lock);
1069
Daniel Baluta28ff3442014-12-23 15:22:26 +02001070 return ret;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001071}
1072
1073static int kmx61_trig_try_reenable(struct iio_trigger *trig)
1074{
1075 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1076 struct kmx61_data *data = kmx61_get_data(indio_dev);
1077 int ret;
1078
1079 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1080 if (ret < 0) {
1081 dev_err(&data->client->dev, "Error reading reg_inl\n");
1082 return ret;
1083 }
1084
1085 return 0;
1086}
1087
1088static const struct iio_trigger_ops kmx61_trigger_ops = {
1089 .set_trigger_state = kmx61_data_rdy_trigger_set_state,
1090 .try_reenable = kmx61_trig_try_reenable,
1091 .owner = THIS_MODULE,
1092};
1093
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001094static irqreturn_t kmx61_event_handler(int irq, void *private)
1095{
1096 struct kmx61_data *data = private;
1097 struct iio_dev *indio_dev = data->acc_indio_dev;
1098 int ret;
1099
1100 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1101 if (ret < 0) {
1102 dev_err(&data->client->dev, "Error reading reg_ins1\n");
1103 goto ack_intr;
1104 }
1105
1106 if (ret & KMX61_REG_INS1_BIT_WUFS) {
1107 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1108 if (ret < 0) {
1109 dev_err(&data->client->dev, "Error reading reg_ins2\n");
1110 goto ack_intr;
1111 }
1112
1113 if (ret & KMX61_REG_INS2_BIT_XN)
1114 iio_push_event(indio_dev,
1115 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1116 0,
1117 IIO_MOD_X,
1118 IIO_EV_TYPE_THRESH,
1119 IIO_EV_DIR_FALLING),
1120 0);
1121
1122 if (ret & KMX61_REG_INS2_BIT_XP)
1123 iio_push_event(indio_dev,
1124 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1125 0,
1126 IIO_MOD_X,
1127 IIO_EV_TYPE_THRESH,
1128 IIO_EV_DIR_RISING),
1129 0);
1130
1131 if (ret & KMX61_REG_INS2_BIT_YN)
1132 iio_push_event(indio_dev,
1133 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1134 0,
1135 IIO_MOD_Y,
1136 IIO_EV_TYPE_THRESH,
1137 IIO_EV_DIR_FALLING),
1138 0);
1139
1140 if (ret & KMX61_REG_INS2_BIT_YP)
1141 iio_push_event(indio_dev,
1142 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1143 0,
1144 IIO_MOD_Y,
1145 IIO_EV_TYPE_THRESH,
1146 IIO_EV_DIR_RISING),
1147 0);
1148
1149 if (ret & KMX61_REG_INS2_BIT_ZN)
1150 iio_push_event(indio_dev,
1151 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1152 0,
1153 IIO_MOD_Z,
1154 IIO_EV_TYPE_THRESH,
1155 IIO_EV_DIR_FALLING),
1156 0);
1157
1158 if (ret & KMX61_REG_INS2_BIT_ZP)
1159 iio_push_event(indio_dev,
1160 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1161 0,
1162 IIO_MOD_Z,
1163 IIO_EV_TYPE_THRESH,
1164 IIO_EV_DIR_RISING),
1165 0);
1166 }
1167
1168ack_intr:
1169 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1170 if (ret < 0)
1171 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1172
1173 ret |= KMX61_REG_CTRL1_BIT_RES;
1174 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1175 if (ret < 0)
Daniel Baluta28ff3442014-12-23 15:22:26 +02001176 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001177
1178 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1179 if (ret < 0)
1180 dev_err(&data->client->dev, "Error reading reg_inl\n");
1181
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001182 return IRQ_HANDLED;
1183}
1184
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001185static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1186{
1187 struct kmx61_data *data = private;
1188
1189 if (data->acc_dready_trig_on)
1190 iio_trigger_poll(data->acc_dready_trig);
1191 if (data->mag_dready_trig_on)
1192 iio_trigger_poll(data->mag_dready_trig);
1193
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001194 if (data->motion_trig_on)
1195 iio_trigger_poll(data->motion_trig);
1196
1197 if (data->ev_enable_state)
1198 return IRQ_WAKE_THREAD;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001199 return IRQ_HANDLED;
1200}
1201
1202static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1203{
1204 struct iio_poll_func *pf = p;
1205 struct iio_dev *indio_dev = pf->indio_dev;
1206 struct kmx61_data *data = kmx61_get_data(indio_dev);
1207 int bit, ret, i = 0;
Daniel Baluta6a191c72014-12-23 15:22:33 +02001208 u8 base;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001209 s16 buffer[8];
1210
Daniel Baluta6a191c72014-12-23 15:22:33 +02001211 if (indio_dev == data->acc_indio_dev)
1212 base = KMX61_ACC_XOUT_L;
1213 else
1214 base = KMX61_MAG_XOUT_L;
1215
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001216 mutex_lock(&data->lock);
Octavian Purdila70dddee2015-03-02 21:03:05 +02001217 for_each_set_bit(bit, indio_dev->active_scan_mask,
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001218 indio_dev->masklength) {
Daniel Baluta6a191c72014-12-23 15:22:33 +02001219 ret = kmx61_read_measurement(data, base, bit);
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001220 if (ret < 0) {
1221 mutex_unlock(&data->lock);
1222 goto err;
1223 }
1224 buffer[i++] = ret;
1225 }
1226 mutex_unlock(&data->lock);
1227
1228 iio_push_to_buffers(indio_dev, buffer);
1229err:
1230 iio_trigger_notify_done(indio_dev->trig);
1231
1232 return IRQ_HANDLED;
1233}
1234
Daniel Balutab25862c2014-12-03 15:31:49 +02001235static const char *kmx61_match_acpi_device(struct device *dev)
1236{
1237 const struct acpi_device_id *id;
1238
1239 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1240 if (!id)
1241 return NULL;
1242 return dev_name(dev);
1243}
1244
Daniel Baluta20ffac22014-12-03 15:31:48 +02001245static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1246 const struct iio_info *info,
1247 const struct iio_chan_spec *chan,
1248 int num_channels,
1249 const char *name)
1250{
1251 struct iio_dev *indio_dev;
1252
1253 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1254 if (!indio_dev)
1255 return ERR_PTR(-ENOMEM);
1256
1257 kmx61_set_data(indio_dev, data);
1258
1259 indio_dev->dev.parent = &data->client->dev;
1260 indio_dev->channels = chan;
1261 indio_dev->num_channels = num_channels;
1262 indio_dev->name = name;
1263 indio_dev->modes = INDIO_DIRECT_MODE;
1264 indio_dev->info = info;
1265
1266 return indio_dev;
1267}
1268
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001269static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1270 struct iio_dev *indio_dev,
1271 const char *tag)
1272{
1273 struct iio_trigger *trig;
1274 int ret;
1275
1276 trig = devm_iio_trigger_alloc(&data->client->dev,
1277 "%s-%s-dev%d",
1278 indio_dev->name,
1279 tag,
1280 indio_dev->id);
1281 if (!trig)
1282 return ERR_PTR(-ENOMEM);
1283
1284 trig->dev.parent = &data->client->dev;
1285 trig->ops = &kmx61_trigger_ops;
1286 iio_trigger_set_drvdata(trig, indio_dev);
1287
1288 ret = iio_trigger_register(trig);
1289 if (ret)
1290 return ERR_PTR(ret);
1291
1292 return trig;
1293}
1294
Daniel Baluta20ffac22014-12-03 15:31:48 +02001295static int kmx61_probe(struct i2c_client *client,
1296 const struct i2c_device_id *id)
1297{
1298 int ret;
1299 struct kmx61_data *data;
1300 const char *name = NULL;
1301
1302 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1303 if (!data)
1304 return -ENOMEM;
1305
1306 i2c_set_clientdata(client, data);
1307 data->client = client;
1308
1309 mutex_init(&data->lock);
1310
Daniel Balutab25862c2014-12-03 15:31:49 +02001311 if (id)
1312 name = id->name;
1313 else if (ACPI_HANDLE(&client->dev))
1314 name = kmx61_match_acpi_device(&client->dev);
1315 else
1316 return -ENODEV;
1317
Daniel Baluta20ffac22014-12-03 15:31:48 +02001318 data->acc_indio_dev =
1319 kmx61_indiodev_setup(data, &kmx61_acc_info,
1320 kmx61_acc_channels,
1321 ARRAY_SIZE(kmx61_acc_channels),
1322 name);
1323 if (IS_ERR(data->acc_indio_dev))
1324 return PTR_ERR(data->acc_indio_dev);
1325
1326 data->mag_indio_dev =
1327 kmx61_indiodev_setup(data, &kmx61_mag_info,
1328 kmx61_mag_channels,
1329 ARRAY_SIZE(kmx61_mag_channels),
1330 name);
1331 if (IS_ERR(data->mag_indio_dev))
1332 return PTR_ERR(data->mag_indio_dev);
1333
1334 ret = kmx61_chip_init(data);
1335 if (ret < 0)
1336 return ret;
1337
Octavian Purdilac176bec2015-06-05 16:59:42 +03001338 if (client->irq > 0) {
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001339 ret = devm_request_threaded_irq(&client->dev, client->irq,
1340 kmx61_data_rdy_trig_poll,
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001341 kmx61_event_handler,
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001342 IRQF_TRIGGER_RISING,
1343 KMX61_IRQ_NAME,
1344 data);
1345 if (ret)
1346 goto err_chip_uninit;
1347
1348 data->acc_dready_trig =
1349 kmx61_trigger_setup(data, data->acc_indio_dev,
1350 "dready");
Daniel Baluta28ff3442014-12-23 15:22:26 +02001351 if (IS_ERR(data->acc_dready_trig)) {
1352 ret = PTR_ERR(data->acc_dready_trig);
1353 goto err_chip_uninit;
1354 }
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001355
1356 data->mag_dready_trig =
1357 kmx61_trigger_setup(data, data->mag_indio_dev,
1358 "dready");
1359 if (IS_ERR(data->mag_dready_trig)) {
1360 ret = PTR_ERR(data->mag_dready_trig);
Daniel Baluta28ff3442014-12-23 15:22:26 +02001361 goto err_trigger_unregister_acc_dready;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001362 }
1363
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001364 data->motion_trig =
1365 kmx61_trigger_setup(data, data->acc_indio_dev,
1366 "any-motion");
1367 if (IS_ERR(data->motion_trig)) {
1368 ret = PTR_ERR(data->motion_trig);
Daniel Baluta28ff3442014-12-23 15:22:26 +02001369 goto err_trigger_unregister_mag_dready;
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001370 }
1371
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001372 ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1373 &iio_pollfunc_store_time,
1374 kmx61_trigger_handler,
1375 NULL);
1376 if (ret < 0) {
1377 dev_err(&data->client->dev,
1378 "Failed to setup acc triggered buffer\n");
Daniel Baluta28ff3442014-12-23 15:22:26 +02001379 goto err_trigger_unregister_motion;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001380 }
1381
1382 ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1383 &iio_pollfunc_store_time,
1384 kmx61_trigger_handler,
1385 NULL);
1386 if (ret < 0) {
1387 dev_err(&data->client->dev,
1388 "Failed to setup mag triggered buffer\n");
Daniel Baluta28ff3442014-12-23 15:22:26 +02001389 goto err_buffer_cleanup_acc;
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001390 }
1391 }
1392
Adriana Reus7d0ead52015-11-05 16:25:29 +02001393 ret = pm_runtime_set_active(&client->dev);
1394 if (ret < 0)
1395 goto err_buffer_cleanup_mag;
1396
1397 pm_runtime_enable(&client->dev);
1398 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1399 pm_runtime_use_autosuspend(&client->dev);
1400
Daniel Baluta20ffac22014-12-03 15:31:48 +02001401 ret = iio_device_register(data->acc_indio_dev);
1402 if (ret < 0) {
1403 dev_err(&client->dev, "Failed to register acc iio device\n");
Daniel Baluta28ff3442014-12-23 15:22:26 +02001404 goto err_buffer_cleanup_mag;
Daniel Baluta20ffac22014-12-03 15:31:48 +02001405 }
1406
1407 ret = iio_device_register(data->mag_indio_dev);
1408 if (ret < 0) {
1409 dev_err(&client->dev, "Failed to register mag iio device\n");
Daniel Balutaaff86092014-12-03 15:31:50 +02001410 goto err_iio_unregister_acc;
Daniel Baluta20ffac22014-12-03 15:31:48 +02001411 }
1412
1413 return 0;
1414
Daniel Balutaaff86092014-12-03 15:31:50 +02001415err_iio_unregister_acc:
Daniel Baluta20ffac22014-12-03 15:31:48 +02001416 iio_device_unregister(data->acc_indio_dev);
Daniel Baluta28ff3442014-12-23 15:22:26 +02001417err_buffer_cleanup_mag:
Octavian Purdilac176bec2015-06-05 16:59:42 +03001418 if (client->irq > 0)
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001419 iio_triggered_buffer_cleanup(data->mag_indio_dev);
Daniel Baluta28ff3442014-12-23 15:22:26 +02001420err_buffer_cleanup_acc:
Octavian Purdilac176bec2015-06-05 16:59:42 +03001421 if (client->irq > 0)
Daniel Baluta28ff3442014-12-23 15:22:26 +02001422 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1423err_trigger_unregister_motion:
1424 iio_trigger_unregister(data->motion_trig);
1425err_trigger_unregister_mag_dready:
1426 iio_trigger_unregister(data->mag_dready_trig);
1427err_trigger_unregister_acc_dready:
1428 iio_trigger_unregister(data->acc_dready_trig);
Daniel Baluta20ffac22014-12-03 15:31:48 +02001429err_chip_uninit:
1430 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1431 return ret;
1432}
1433
1434static int kmx61_remove(struct i2c_client *client)
1435{
1436 struct kmx61_data *data = i2c_get_clientdata(client);
1437
Adriana Reus7d0ead52015-11-05 16:25:29 +02001438 iio_device_unregister(data->acc_indio_dev);
1439 iio_device_unregister(data->mag_indio_dev);
1440
Daniel Balutaaff86092014-12-03 15:31:50 +02001441 pm_runtime_disable(&client->dev);
1442 pm_runtime_set_suspended(&client->dev);
1443 pm_runtime_put_noidle(&client->dev);
1444
Octavian Purdilac176bec2015-06-05 16:59:42 +03001445 if (client->irq > 0) {
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001446 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1447 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1448 iio_trigger_unregister(data->acc_dready_trig);
1449 iio_trigger_unregister(data->mag_dready_trig);
Daniel Balutafd3ae7a2014-12-03 15:31:53 +02001450 iio_trigger_unregister(data->motion_trig);
Daniel Balutac3a23ecc2014-12-03 15:31:52 +02001451 }
1452
Daniel Baluta20ffac22014-12-03 15:31:48 +02001453 mutex_lock(&data->lock);
1454 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1455 mutex_unlock(&data->lock);
1456
1457 return 0;
1458}
1459
Daniel Baluta3b9c40e2014-12-03 15:31:51 +02001460#ifdef CONFIG_PM_SLEEP
1461static int kmx61_suspend(struct device *dev)
1462{
1463 int ret;
1464 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1465
1466 mutex_lock(&data->lock);
1467 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1468 false);
1469 mutex_unlock(&data->lock);
1470
1471 return ret;
1472}
1473
1474static int kmx61_resume(struct device *dev)
1475{
1476 u8 stby = 0;
1477 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1478
1479 if (data->acc_stby)
1480 stby |= KMX61_ACC_STBY_BIT;
1481 if (data->mag_stby)
1482 stby |= KMX61_MAG_STBY_BIT;
1483
1484 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1485}
1486#endif
Daniel Balutaaff86092014-12-03 15:31:50 +02001487
Daniel Balutadf1fad82015-01-09 11:39:30 +02001488#ifdef CONFIG_PM
Daniel Balutaaff86092014-12-03 15:31:50 +02001489static int kmx61_runtime_suspend(struct device *dev)
1490{
1491 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1492 int ret;
1493
1494 mutex_lock(&data->lock);
1495 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1496 mutex_unlock(&data->lock);
1497
1498 return ret;
1499}
1500
1501static int kmx61_runtime_resume(struct device *dev)
1502{
1503 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1504 u8 stby = 0;
1505
1506 if (!data->acc_ps)
1507 stby |= KMX61_ACC_STBY_BIT;
1508 if (!data->mag_ps)
1509 stby |= KMX61_MAG_STBY_BIT;
1510
1511 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1512}
1513#endif
1514
1515static const struct dev_pm_ops kmx61_pm_ops = {
Daniel Baluta3b9c40e2014-12-03 15:31:51 +02001516 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
Daniel Balutaaff86092014-12-03 15:31:50 +02001517 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1518};
1519
Daniel Balutab25862c2014-12-03 15:31:49 +02001520static const struct acpi_device_id kmx61_acpi_match[] = {
1521 {"KMX61021", 0},
1522 {}
1523};
1524
1525MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
1526
Daniel Baluta20ffac22014-12-03 15:31:48 +02001527static const struct i2c_device_id kmx61_id[] = {
1528 {"kmx611021", 0},
1529 {}
1530};
1531
1532MODULE_DEVICE_TABLE(i2c, kmx61_id);
1533
1534static struct i2c_driver kmx61_driver = {
1535 .driver = {
1536 .name = KMX61_DRV_NAME,
Daniel Balutab25862c2014-12-03 15:31:49 +02001537 .acpi_match_table = ACPI_PTR(kmx61_acpi_match),
Daniel Balutaaff86092014-12-03 15:31:50 +02001538 .pm = &kmx61_pm_ops,
Daniel Baluta20ffac22014-12-03 15:31:48 +02001539 },
1540 .probe = kmx61_probe,
1541 .remove = kmx61_remove,
1542 .id_table = kmx61_id,
1543};
1544
1545module_i2c_driver(kmx61_driver);
1546
1547MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1548MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1549MODULE_LICENSE("GPL v2");