staging: octeon: prevent poll during rx init
[linux/fpc-iii.git] / drivers / iio / magnetometer / bmc150_magn.c
blobd104fb8d937979a7d8296607fa5f34a09d6b321e
1 /*
2 * Bosch BMC150 three-axis magnetic field sensor driver
4 * Copyright (c) 2015, Intel Corporation.
6 * This code is based on bmm050_api.c authored by contact@bosch.sensortec.com:
8 * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/acpi.h>
26 #include <linux/pm.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/iio/events.h>
32 #include <linux/iio/trigger.h>
33 #include <linux/iio/trigger_consumer.h>
34 #include <linux/iio/triggered_buffer.h>
35 #include <linux/regmap.h>
37 #include "bmc150_magn.h"
39 #define BMC150_MAGN_DRV_NAME "bmc150_magn"
40 #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event"
42 #define BMC150_MAGN_REG_CHIP_ID 0x40
43 #define BMC150_MAGN_CHIP_ID_VAL 0x32
45 #define BMC150_MAGN_REG_X_L 0x42
46 #define BMC150_MAGN_REG_X_M 0x43
47 #define BMC150_MAGN_REG_Y_L 0x44
48 #define BMC150_MAGN_REG_Y_M 0x45
49 #define BMC150_MAGN_SHIFT_XY_L 3
50 #define BMC150_MAGN_REG_Z_L 0x46
51 #define BMC150_MAGN_REG_Z_M 0x47
52 #define BMC150_MAGN_SHIFT_Z_L 1
53 #define BMC150_MAGN_REG_RHALL_L 0x48
54 #define BMC150_MAGN_REG_RHALL_M 0x49
55 #define BMC150_MAGN_SHIFT_RHALL_L 2
57 #define BMC150_MAGN_REG_INT_STATUS 0x4A
59 #define BMC150_MAGN_REG_POWER 0x4B
60 #define BMC150_MAGN_MASK_POWER_CTL BIT(0)
62 #define BMC150_MAGN_REG_OPMODE_ODR 0x4C
63 #define BMC150_MAGN_MASK_OPMODE GENMASK(2, 1)
64 #define BMC150_MAGN_SHIFT_OPMODE 1
65 #define BMC150_MAGN_MODE_NORMAL 0x00
66 #define BMC150_MAGN_MODE_FORCED 0x01
67 #define BMC150_MAGN_MODE_SLEEP 0x03
68 #define BMC150_MAGN_MASK_ODR GENMASK(5, 3)
69 #define BMC150_MAGN_SHIFT_ODR 3
71 #define BMC150_MAGN_REG_INT 0x4D
73 #define BMC150_MAGN_REG_INT_DRDY 0x4E
74 #define BMC150_MAGN_MASK_DRDY_EN BIT(7)
75 #define BMC150_MAGN_SHIFT_DRDY_EN 7
76 #define BMC150_MAGN_MASK_DRDY_INT3 BIT(6)
77 #define BMC150_MAGN_MASK_DRDY_Z_EN BIT(5)
78 #define BMC150_MAGN_MASK_DRDY_Y_EN BIT(4)
79 #define BMC150_MAGN_MASK_DRDY_X_EN BIT(3)
80 #define BMC150_MAGN_MASK_DRDY_DR_POLARITY BIT(2)
81 #define BMC150_MAGN_MASK_DRDY_LATCHING BIT(1)
82 #define BMC150_MAGN_MASK_DRDY_INT3_POLARITY BIT(0)
84 #define BMC150_MAGN_REG_LOW_THRESH 0x4F
85 #define BMC150_MAGN_REG_HIGH_THRESH 0x50
86 #define BMC150_MAGN_REG_REP_XY 0x51
87 #define BMC150_MAGN_REG_REP_Z 0x52
88 #define BMC150_MAGN_REG_REP_DATAMASK GENMASK(7, 0)
90 #define BMC150_MAGN_REG_TRIM_START 0x5D
91 #define BMC150_MAGN_REG_TRIM_END 0x71
93 #define BMC150_MAGN_XY_OVERFLOW_VAL -4096
94 #define BMC150_MAGN_Z_OVERFLOW_VAL -16384
96 /* Time from SUSPEND to SLEEP */
97 #define BMC150_MAGN_START_UP_TIME_MS 3
99 #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS 2000
101 #define BMC150_MAGN_REGVAL_TO_REPXY(regval) (((regval) * 2) + 1)
102 #define BMC150_MAGN_REGVAL_TO_REPZ(regval) ((regval) + 1)
103 #define BMC150_MAGN_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2)
104 #define BMC150_MAGN_REPZ_TO_REGVAL(rep) ((rep) - 1)
106 enum bmc150_magn_axis {
107 AXIS_X,
108 AXIS_Y,
109 AXIS_Z,
110 RHALL,
111 AXIS_XYZ_MAX = RHALL,
112 AXIS_XYZR_MAX,
115 enum bmc150_magn_power_modes {
116 BMC150_MAGN_POWER_MODE_SUSPEND,
117 BMC150_MAGN_POWER_MODE_SLEEP,
118 BMC150_MAGN_POWER_MODE_NORMAL,
121 struct bmc150_magn_trim_regs {
122 s8 x1;
123 s8 y1;
124 __le16 reserved1;
125 u8 reserved2;
126 __le16 z4;
127 s8 x2;
128 s8 y2;
129 __le16 reserved3;
130 __le16 z2;
131 __le16 z1;
132 __le16 xyz1;
133 __le16 z3;
134 s8 xy2;
135 u8 xy1;
136 } __packed;
138 struct bmc150_magn_data {
139 struct device *dev;
141 * 1. Protect this structure.
142 * 2. Serialize sequences that power on/off the device and access HW.
144 struct mutex mutex;
145 struct regmap *regmap;
146 /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */
147 s32 buffer[6];
148 struct iio_trigger *dready_trig;
149 bool dready_trigger_on;
150 int max_odr;
151 int irq;
154 static const struct {
155 int freq;
156 u8 reg_val;
157 } bmc150_magn_samp_freq_table[] = { {2, 0x01},
158 {6, 0x02},
159 {8, 0x03},
160 {10, 0x00},
161 {15, 0x04},
162 {20, 0x05},
163 {25, 0x06},
164 {30, 0x07} };
166 enum bmc150_magn_presets {
167 LOW_POWER_PRESET,
168 REGULAR_PRESET,
169 ENHANCED_REGULAR_PRESET,
170 HIGH_ACCURACY_PRESET
173 static const struct bmc150_magn_preset {
174 u8 rep_xy;
175 u8 rep_z;
176 u8 odr;
177 } bmc150_magn_presets_table[] = {
178 [LOW_POWER_PRESET] = {3, 3, 10},
179 [REGULAR_PRESET] = {9, 15, 10},
180 [ENHANCED_REGULAR_PRESET] = {15, 27, 10},
181 [HIGH_ACCURACY_PRESET] = {47, 83, 20},
184 #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET
186 static bool bmc150_magn_is_writeable_reg(struct device *dev, unsigned int reg)
188 switch (reg) {
189 case BMC150_MAGN_REG_POWER:
190 case BMC150_MAGN_REG_OPMODE_ODR:
191 case BMC150_MAGN_REG_INT:
192 case BMC150_MAGN_REG_INT_DRDY:
193 case BMC150_MAGN_REG_LOW_THRESH:
194 case BMC150_MAGN_REG_HIGH_THRESH:
195 case BMC150_MAGN_REG_REP_XY:
196 case BMC150_MAGN_REG_REP_Z:
197 return true;
198 default:
199 return false;
203 static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg)
205 switch (reg) {
206 case BMC150_MAGN_REG_X_L:
207 case BMC150_MAGN_REG_X_M:
208 case BMC150_MAGN_REG_Y_L:
209 case BMC150_MAGN_REG_Y_M:
210 case BMC150_MAGN_REG_Z_L:
211 case BMC150_MAGN_REG_Z_M:
212 case BMC150_MAGN_REG_RHALL_L:
213 case BMC150_MAGN_REG_RHALL_M:
214 case BMC150_MAGN_REG_INT_STATUS:
215 return true;
216 default:
217 return false;
221 const struct regmap_config bmc150_magn_regmap_config = {
222 .reg_bits = 8,
223 .val_bits = 8,
225 .max_register = BMC150_MAGN_REG_TRIM_END,
226 .cache_type = REGCACHE_RBTREE,
228 .writeable_reg = bmc150_magn_is_writeable_reg,
229 .volatile_reg = bmc150_magn_is_volatile_reg,
231 EXPORT_SYMBOL(bmc150_magn_regmap_config);
233 static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data,
234 enum bmc150_magn_power_modes mode,
235 bool state)
237 int ret;
239 switch (mode) {
240 case BMC150_MAGN_POWER_MODE_SUSPEND:
241 ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_POWER,
242 BMC150_MAGN_MASK_POWER_CTL, !state);
243 if (ret < 0)
244 return ret;
245 usleep_range(BMC150_MAGN_START_UP_TIME_MS * 1000, 20000);
246 return 0;
247 case BMC150_MAGN_POWER_MODE_SLEEP:
248 return regmap_update_bits(data->regmap,
249 BMC150_MAGN_REG_OPMODE_ODR,
250 BMC150_MAGN_MASK_OPMODE,
251 BMC150_MAGN_MODE_SLEEP <<
252 BMC150_MAGN_SHIFT_OPMODE);
253 case BMC150_MAGN_POWER_MODE_NORMAL:
254 return regmap_update_bits(data->regmap,
255 BMC150_MAGN_REG_OPMODE_ODR,
256 BMC150_MAGN_MASK_OPMODE,
257 BMC150_MAGN_MODE_NORMAL <<
258 BMC150_MAGN_SHIFT_OPMODE);
261 return -EINVAL;
264 static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
266 #ifdef CONFIG_PM
267 int ret;
269 if (on) {
270 ret = pm_runtime_get_sync(data->dev);
271 } else {
272 pm_runtime_mark_last_busy(data->dev);
273 ret = pm_runtime_put_autosuspend(data->dev);
276 if (ret < 0) {
277 dev_err(data->dev,
278 "failed to change power state to %d\n", on);
279 if (on)
280 pm_runtime_put_noidle(data->dev);
282 return ret;
284 #endif
286 return 0;
289 static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val)
291 int ret, reg_val;
292 u8 i, odr_val;
294 ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, &reg_val);
295 if (ret < 0)
296 return ret;
297 odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
299 for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++)
300 if (bmc150_magn_samp_freq_table[i].reg_val == odr_val) {
301 *val = bmc150_magn_samp_freq_table[i].freq;
302 return 0;
305 return -EINVAL;
308 static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val)
310 int ret;
311 u8 i;
313 for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
314 if (bmc150_magn_samp_freq_table[i].freq == val) {
315 ret = regmap_update_bits(data->regmap,
316 BMC150_MAGN_REG_OPMODE_ODR,
317 BMC150_MAGN_MASK_ODR,
318 bmc150_magn_samp_freq_table[i].
319 reg_val <<
320 BMC150_MAGN_SHIFT_ODR);
321 if (ret < 0)
322 return ret;
323 return 0;
327 return -EINVAL;
330 static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy,
331 int rep_z, int odr)
333 int ret, reg_val, max_odr;
335 if (rep_xy <= 0) {
336 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY,
337 &reg_val);
338 if (ret < 0)
339 return ret;
340 rep_xy = BMC150_MAGN_REGVAL_TO_REPXY(reg_val);
342 if (rep_z <= 0) {
343 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z,
344 &reg_val);
345 if (ret < 0)
346 return ret;
347 rep_z = BMC150_MAGN_REGVAL_TO_REPZ(reg_val);
349 if (odr <= 0) {
350 ret = bmc150_magn_get_odr(data, &odr);
351 if (ret < 0)
352 return ret;
354 /* the maximum selectable read-out frequency from datasheet */
355 max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980);
356 if (odr > max_odr) {
357 dev_err(data->dev,
358 "Can't set oversampling with sampling freq %d\n",
359 odr);
360 return -EINVAL;
362 data->max_odr = max_odr;
364 return 0;
367 static s32 bmc150_magn_compensate_x(struct bmc150_magn_trim_regs *tregs, s16 x,
368 u16 rhall)
370 s16 val;
371 u16 xyz1 = le16_to_cpu(tregs->xyz1);
373 if (x == BMC150_MAGN_XY_OVERFLOW_VAL)
374 return S32_MIN;
376 if (!rhall)
377 rhall = xyz1;
379 val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000)));
380 val = ((s16)((((s32)x) * ((((((((s32)tregs->xy2) * ((((s32)val) *
381 ((s32)val)) >> 7)) + (((s32)val) *
382 ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) *
383 ((s32)(((s16)tregs->x2) + ((s16)0xA0)))) >> 12)) >> 13)) +
384 (((s16)tregs->x1) << 3);
386 return (s32)val;
389 static s32 bmc150_magn_compensate_y(struct bmc150_magn_trim_regs *tregs, s16 y,
390 u16 rhall)
392 s16 val;
393 u16 xyz1 = le16_to_cpu(tregs->xyz1);
395 if (y == BMC150_MAGN_XY_OVERFLOW_VAL)
396 return S32_MIN;
398 if (!rhall)
399 rhall = xyz1;
401 val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000)));
402 val = ((s16)((((s32)y) * ((((((((s32)tregs->xy2) * ((((s32)val) *
403 ((s32)val)) >> 7)) + (((s32)val) *
404 ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) *
405 ((s32)(((s16)tregs->y2) + ((s16)0xA0)))) >> 12)) >> 13)) +
406 (((s16)tregs->y1) << 3);
408 return (s32)val;
411 static s32 bmc150_magn_compensate_z(struct bmc150_magn_trim_regs *tregs, s16 z,
412 u16 rhall)
414 s32 val;
415 u16 xyz1 = le16_to_cpu(tregs->xyz1);
416 u16 z1 = le16_to_cpu(tregs->z1);
417 s16 z2 = le16_to_cpu(tregs->z2);
418 s16 z3 = le16_to_cpu(tregs->z3);
419 s16 z4 = le16_to_cpu(tregs->z4);
421 if (z == BMC150_MAGN_Z_OVERFLOW_VAL)
422 return S32_MIN;
424 val = (((((s32)(z - z4)) << 15) - ((((s32)z3) * ((s32)(((s16)rhall) -
425 ((s16)xyz1)))) >> 2)) / (z2 + ((s16)(((((s32)z1) *
426 ((((s16)rhall) << 1))) + (1 << 15)) >> 16))));
428 return val;
431 static int bmc150_magn_read_xyz(struct bmc150_magn_data *data, s32 *buffer)
433 int ret;
434 __le16 values[AXIS_XYZR_MAX];
435 s16 raw_x, raw_y, raw_z;
436 u16 rhall;
437 struct bmc150_magn_trim_regs tregs;
439 ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_X_L,
440 values, sizeof(values));
441 if (ret < 0)
442 return ret;
444 raw_x = (s16)le16_to_cpu(values[AXIS_X]) >> BMC150_MAGN_SHIFT_XY_L;
445 raw_y = (s16)le16_to_cpu(values[AXIS_Y]) >> BMC150_MAGN_SHIFT_XY_L;
446 raw_z = (s16)le16_to_cpu(values[AXIS_Z]) >> BMC150_MAGN_SHIFT_Z_L;
447 rhall = le16_to_cpu(values[RHALL]) >> BMC150_MAGN_SHIFT_RHALL_L;
449 ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_TRIM_START,
450 &tregs, sizeof(tregs));
451 if (ret < 0)
452 return ret;
454 buffer[AXIS_X] = bmc150_magn_compensate_x(&tregs, raw_x, rhall);
455 buffer[AXIS_Y] = bmc150_magn_compensate_y(&tregs, raw_y, rhall);
456 buffer[AXIS_Z] = bmc150_magn_compensate_z(&tregs, raw_z, rhall);
458 return 0;
461 static int bmc150_magn_read_raw(struct iio_dev *indio_dev,
462 struct iio_chan_spec const *chan,
463 int *val, int *val2, long mask)
465 struct bmc150_magn_data *data = iio_priv(indio_dev);
466 int ret, tmp;
467 s32 values[AXIS_XYZ_MAX];
469 switch (mask) {
470 case IIO_CHAN_INFO_RAW:
471 if (iio_buffer_enabled(indio_dev))
472 return -EBUSY;
473 mutex_lock(&data->mutex);
475 ret = bmc150_magn_set_power_state(data, true);
476 if (ret < 0) {
477 mutex_unlock(&data->mutex);
478 return ret;
481 ret = bmc150_magn_read_xyz(data, values);
482 if (ret < 0) {
483 bmc150_magn_set_power_state(data, false);
484 mutex_unlock(&data->mutex);
485 return ret;
487 *val = values[chan->scan_index];
489 ret = bmc150_magn_set_power_state(data, false);
490 if (ret < 0) {
491 mutex_unlock(&data->mutex);
492 return ret;
495 mutex_unlock(&data->mutex);
496 return IIO_VAL_INT;
497 case IIO_CHAN_INFO_SCALE:
499 * The API/driver performs an off-chip temperature
500 * compensation and outputs x/y/z magnetic field data in
501 * 16 LSB/uT to the upper application layer.
503 *val = 0;
504 *val2 = 625;
505 return IIO_VAL_INT_PLUS_MICRO;
506 case IIO_CHAN_INFO_SAMP_FREQ:
507 ret = bmc150_magn_get_odr(data, val);
508 if (ret < 0)
509 return ret;
510 return IIO_VAL_INT;
511 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
512 switch (chan->channel2) {
513 case IIO_MOD_X:
514 case IIO_MOD_Y:
515 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY,
516 &tmp);
517 if (ret < 0)
518 return ret;
519 *val = BMC150_MAGN_REGVAL_TO_REPXY(tmp);
520 return IIO_VAL_INT;
521 case IIO_MOD_Z:
522 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z,
523 &tmp);
524 if (ret < 0)
525 return ret;
526 *val = BMC150_MAGN_REGVAL_TO_REPZ(tmp);
527 return IIO_VAL_INT;
528 default:
529 return -EINVAL;
531 default:
532 return -EINVAL;
536 static int bmc150_magn_write_raw(struct iio_dev *indio_dev,
537 struct iio_chan_spec const *chan,
538 int val, int val2, long mask)
540 struct bmc150_magn_data *data = iio_priv(indio_dev);
541 int ret;
543 switch (mask) {
544 case IIO_CHAN_INFO_SAMP_FREQ:
545 if (val > data->max_odr)
546 return -EINVAL;
547 mutex_lock(&data->mutex);
548 ret = bmc150_magn_set_odr(data, val);
549 mutex_unlock(&data->mutex);
550 return ret;
551 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
552 switch (chan->channel2) {
553 case IIO_MOD_X:
554 case IIO_MOD_Y:
555 if (val < 1 || val > 511)
556 return -EINVAL;
557 mutex_lock(&data->mutex);
558 ret = bmc150_magn_set_max_odr(data, val, 0, 0);
559 if (ret < 0) {
560 mutex_unlock(&data->mutex);
561 return ret;
563 ret = regmap_update_bits(data->regmap,
564 BMC150_MAGN_REG_REP_XY,
565 BMC150_MAGN_REG_REP_DATAMASK,
566 BMC150_MAGN_REPXY_TO_REGVAL
567 (val));
568 mutex_unlock(&data->mutex);
569 return ret;
570 case IIO_MOD_Z:
571 if (val < 1 || val > 256)
572 return -EINVAL;
573 mutex_lock(&data->mutex);
574 ret = bmc150_magn_set_max_odr(data, 0, val, 0);
575 if (ret < 0) {
576 mutex_unlock(&data->mutex);
577 return ret;
579 ret = regmap_update_bits(data->regmap,
580 BMC150_MAGN_REG_REP_Z,
581 BMC150_MAGN_REG_REP_DATAMASK,
582 BMC150_MAGN_REPZ_TO_REGVAL
583 (val));
584 mutex_unlock(&data->mutex);
585 return ret;
586 default:
587 return -EINVAL;
589 default:
590 return -EINVAL;
594 static ssize_t bmc150_magn_show_samp_freq_avail(struct device *dev,
595 struct device_attribute *attr,
596 char *buf)
598 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
599 struct bmc150_magn_data *data = iio_priv(indio_dev);
600 size_t len = 0;
601 u8 i;
603 for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
604 if (bmc150_magn_samp_freq_table[i].freq > data->max_odr)
605 break;
606 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
607 bmc150_magn_samp_freq_table[i].freq);
609 /* replace last space with a newline */
610 buf[len - 1] = '\n';
612 return len;
615 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail);
617 static struct attribute *bmc150_magn_attributes[] = {
618 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
619 NULL,
622 static const struct attribute_group bmc150_magn_attrs_group = {
623 .attrs = bmc150_magn_attributes,
626 #define BMC150_MAGN_CHANNEL(_axis) { \
627 .type = IIO_MAGN, \
628 .modified = 1, \
629 .channel2 = IIO_MOD_##_axis, \
630 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
631 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
632 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
633 BIT(IIO_CHAN_INFO_SCALE), \
634 .scan_index = AXIS_##_axis, \
635 .scan_type = { \
636 .sign = 's', \
637 .realbits = 32, \
638 .storagebits = 32, \
639 .endianness = IIO_LE \
640 }, \
643 static const struct iio_chan_spec bmc150_magn_channels[] = {
644 BMC150_MAGN_CHANNEL(X),
645 BMC150_MAGN_CHANNEL(Y),
646 BMC150_MAGN_CHANNEL(Z),
647 IIO_CHAN_SOFT_TIMESTAMP(3),
650 static const struct iio_info bmc150_magn_info = {
651 .attrs = &bmc150_magn_attrs_group,
652 .read_raw = bmc150_magn_read_raw,
653 .write_raw = bmc150_magn_write_raw,
654 .driver_module = THIS_MODULE,
657 static const unsigned long bmc150_magn_scan_masks[] = {
658 BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
661 static irqreturn_t bmc150_magn_trigger_handler(int irq, void *p)
663 struct iio_poll_func *pf = p;
664 struct iio_dev *indio_dev = pf->indio_dev;
665 struct bmc150_magn_data *data = iio_priv(indio_dev);
666 int ret;
668 mutex_lock(&data->mutex);
669 ret = bmc150_magn_read_xyz(data, data->buffer);
670 if (ret < 0)
671 goto err;
673 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
674 pf->timestamp);
676 err:
677 mutex_unlock(&data->mutex);
678 iio_trigger_notify_done(indio_dev->trig);
680 return IRQ_HANDLED;
683 static int bmc150_magn_init(struct bmc150_magn_data *data)
685 int ret, chip_id;
686 struct bmc150_magn_preset preset;
688 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND,
689 false);
690 if (ret < 0) {
691 dev_err(data->dev,
692 "Failed to bring up device from suspend mode\n");
693 return ret;
696 ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id);
697 if (ret < 0) {
698 dev_err(data->dev, "Failed reading chip id\n");
699 goto err_poweroff;
701 if (chip_id != BMC150_MAGN_CHIP_ID_VAL) {
702 dev_err(data->dev, "Invalid chip id 0x%x\n", chip_id);
703 ret = -ENODEV;
704 goto err_poweroff;
706 dev_dbg(data->dev, "Chip id %x\n", chip_id);
708 preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET];
709 ret = bmc150_magn_set_odr(data, preset.odr);
710 if (ret < 0) {
711 dev_err(data->dev, "Failed to set ODR to %d\n",
712 preset.odr);
713 goto err_poweroff;
716 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY,
717 BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy));
718 if (ret < 0) {
719 dev_err(data->dev, "Failed to set REP XY to %d\n",
720 preset.rep_xy);
721 goto err_poweroff;
724 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z,
725 BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z));
726 if (ret < 0) {
727 dev_err(data->dev, "Failed to set REP Z to %d\n",
728 preset.rep_z);
729 goto err_poweroff;
732 ret = bmc150_magn_set_max_odr(data, preset.rep_xy, preset.rep_z,
733 preset.odr);
734 if (ret < 0)
735 goto err_poweroff;
737 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
738 true);
739 if (ret < 0) {
740 dev_err(data->dev, "Failed to power on device\n");
741 goto err_poweroff;
744 return 0;
746 err_poweroff:
747 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
748 return ret;
751 static int bmc150_magn_reset_intr(struct bmc150_magn_data *data)
753 int tmp;
756 * Data Ready (DRDY) is always cleared after
757 * readout of data registers ends.
759 return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp);
762 static int bmc150_magn_trig_try_reen(struct iio_trigger *trig)
764 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
765 struct bmc150_magn_data *data = iio_priv(indio_dev);
766 int ret;
768 if (!data->dready_trigger_on)
769 return 0;
771 mutex_lock(&data->mutex);
772 ret = bmc150_magn_reset_intr(data);
773 mutex_unlock(&data->mutex);
775 return ret;
778 static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
779 bool state)
781 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
782 struct bmc150_magn_data *data = iio_priv(indio_dev);
783 int ret = 0;
785 mutex_lock(&data->mutex);
786 if (state == data->dready_trigger_on)
787 goto err_unlock;
789 ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
790 BMC150_MAGN_MASK_DRDY_EN,
791 state << BMC150_MAGN_SHIFT_DRDY_EN);
792 if (ret < 0)
793 goto err_unlock;
795 data->dready_trigger_on = state;
797 if (state) {
798 ret = bmc150_magn_reset_intr(data);
799 if (ret < 0)
800 goto err_unlock;
802 mutex_unlock(&data->mutex);
804 return 0;
806 err_unlock:
807 mutex_unlock(&data->mutex);
808 return ret;
811 static const struct iio_trigger_ops bmc150_magn_trigger_ops = {
812 .set_trigger_state = bmc150_magn_data_rdy_trigger_set_state,
813 .try_reenable = bmc150_magn_trig_try_reen,
814 .owner = THIS_MODULE,
817 static int bmc150_magn_buffer_preenable(struct iio_dev *indio_dev)
819 struct bmc150_magn_data *data = iio_priv(indio_dev);
821 return bmc150_magn_set_power_state(data, true);
824 static int bmc150_magn_buffer_postdisable(struct iio_dev *indio_dev)
826 struct bmc150_magn_data *data = iio_priv(indio_dev);
828 return bmc150_magn_set_power_state(data, false);
831 static const struct iio_buffer_setup_ops bmc150_magn_buffer_setup_ops = {
832 .preenable = bmc150_magn_buffer_preenable,
833 .postenable = iio_triggered_buffer_postenable,
834 .predisable = iio_triggered_buffer_predisable,
835 .postdisable = bmc150_magn_buffer_postdisable,
838 static const char *bmc150_magn_match_acpi_device(struct device *dev)
840 const struct acpi_device_id *id;
842 id = acpi_match_device(dev->driver->acpi_match_table, dev);
843 if (!id)
844 return NULL;
846 return dev_name(dev);
849 int bmc150_magn_probe(struct device *dev, struct regmap *regmap,
850 int irq, const char *name)
852 struct bmc150_magn_data *data;
853 struct iio_dev *indio_dev;
854 int ret;
856 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
857 if (!indio_dev)
858 return -ENOMEM;
860 data = iio_priv(indio_dev);
861 dev_set_drvdata(dev, indio_dev);
862 data->regmap = regmap;
863 data->irq = irq;
864 data->dev = dev;
866 if (!name && ACPI_HANDLE(dev))
867 name = bmc150_magn_match_acpi_device(dev);
869 mutex_init(&data->mutex);
871 ret = bmc150_magn_init(data);
872 if (ret < 0)
873 return ret;
875 indio_dev->dev.parent = dev;
876 indio_dev->channels = bmc150_magn_channels;
877 indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels);
878 indio_dev->available_scan_masks = bmc150_magn_scan_masks;
879 indio_dev->name = name;
880 indio_dev->modes = INDIO_DIRECT_MODE;
881 indio_dev->info = &bmc150_magn_info;
883 if (irq > 0) {
884 data->dready_trig = devm_iio_trigger_alloc(dev,
885 "%s-dev%d",
886 indio_dev->name,
887 indio_dev->id);
888 if (!data->dready_trig) {
889 ret = -ENOMEM;
890 dev_err(dev, "iio trigger alloc failed\n");
891 goto err_poweroff;
894 data->dready_trig->dev.parent = dev;
895 data->dready_trig->ops = &bmc150_magn_trigger_ops;
896 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
897 ret = iio_trigger_register(data->dready_trig);
898 if (ret) {
899 dev_err(dev, "iio trigger register failed\n");
900 goto err_poweroff;
903 ret = request_threaded_irq(irq,
904 iio_trigger_generic_data_rdy_poll,
905 NULL,
906 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
907 BMC150_MAGN_IRQ_NAME,
908 data->dready_trig);
909 if (ret < 0) {
910 dev_err(dev, "request irq %d failed\n", irq);
911 goto err_trigger_unregister;
915 ret = iio_triggered_buffer_setup(indio_dev,
916 iio_pollfunc_store_time,
917 bmc150_magn_trigger_handler,
918 &bmc150_magn_buffer_setup_ops);
919 if (ret < 0) {
920 dev_err(dev, "iio triggered buffer setup failed\n");
921 goto err_free_irq;
924 ret = pm_runtime_set_active(dev);
925 if (ret)
926 goto err_buffer_cleanup;
928 pm_runtime_enable(dev);
929 pm_runtime_set_autosuspend_delay(dev,
930 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS);
931 pm_runtime_use_autosuspend(dev);
933 ret = iio_device_register(indio_dev);
934 if (ret < 0) {
935 dev_err(dev, "unable to register iio device\n");
936 goto err_buffer_cleanup;
939 dev_dbg(dev, "Registered device %s\n", name);
940 return 0;
942 err_buffer_cleanup:
943 iio_triggered_buffer_cleanup(indio_dev);
944 err_free_irq:
945 if (irq > 0)
946 free_irq(irq, data->dready_trig);
947 err_trigger_unregister:
948 if (data->dready_trig)
949 iio_trigger_unregister(data->dready_trig);
950 err_poweroff:
951 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
952 return ret;
954 EXPORT_SYMBOL(bmc150_magn_probe);
956 int bmc150_magn_remove(struct device *dev)
958 struct iio_dev *indio_dev = dev_get_drvdata(dev);
959 struct bmc150_magn_data *data = iio_priv(indio_dev);
961 iio_device_unregister(indio_dev);
963 pm_runtime_disable(dev);
964 pm_runtime_set_suspended(dev);
965 pm_runtime_put_noidle(dev);
967 iio_triggered_buffer_cleanup(indio_dev);
969 if (data->irq > 0)
970 free_irq(data->irq, data->dready_trig);
972 if (data->dready_trig)
973 iio_trigger_unregister(data->dready_trig);
975 mutex_lock(&data->mutex);
976 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
977 mutex_unlock(&data->mutex);
979 return 0;
981 EXPORT_SYMBOL(bmc150_magn_remove);
983 #ifdef CONFIG_PM
984 static int bmc150_magn_runtime_suspend(struct device *dev)
986 struct iio_dev *indio_dev = dev_get_drvdata(dev);
987 struct bmc150_magn_data *data = iio_priv(indio_dev);
988 int ret;
990 mutex_lock(&data->mutex);
991 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
992 true);
993 mutex_unlock(&data->mutex);
994 if (ret < 0) {
995 dev_err(dev, "powering off device failed\n");
996 return ret;
998 return 0;
1002 * Should be called with data->mutex held.
1004 static int bmc150_magn_runtime_resume(struct device *dev)
1006 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1007 struct bmc150_magn_data *data = iio_priv(indio_dev);
1009 return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
1010 true);
1012 #endif
1014 #ifdef CONFIG_PM_SLEEP
1015 static int bmc150_magn_suspend(struct device *dev)
1017 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1018 struct bmc150_magn_data *data = iio_priv(indio_dev);
1019 int ret;
1021 mutex_lock(&data->mutex);
1022 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
1023 true);
1024 mutex_unlock(&data->mutex);
1026 return ret;
1029 static int bmc150_magn_resume(struct device *dev)
1031 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1032 struct bmc150_magn_data *data = iio_priv(indio_dev);
1033 int ret;
1035 mutex_lock(&data->mutex);
1036 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
1037 true);
1038 mutex_unlock(&data->mutex);
1040 return ret;
1042 #endif
1044 const struct dev_pm_ops bmc150_magn_pm_ops = {
1045 SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume)
1046 SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend,
1047 bmc150_magn_runtime_resume, NULL)
1049 EXPORT_SYMBOL(bmc150_magn_pm_ops);
1051 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1052 MODULE_LICENSE("GPL v2");
1053 MODULE_DESCRIPTION("BMC150 magnetometer core driver");