iio: magn: bmc150: use flags argument of devm_gpiod_get
[linux/fpc-iii.git] / drivers / iio / magnetometer / bmc150_magn.c
blob187a31fdc35a8db6202f9fa923cafd8a252a3316
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/gpio/consumer.h>
27 #include <linux/pm.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/events.h>
33 #include <linux/iio/trigger.h>
34 #include <linux/iio/trigger_consumer.h>
35 #include <linux/iio/triggered_buffer.h>
36 #include <linux/regmap.h>
38 #define BMC150_MAGN_DRV_NAME "bmc150_magn"
39 #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event"
40 #define BMC150_MAGN_GPIO_INT "interrupt"
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
89 #define BMC150_MAGN_REG_TRIM_START 0x5D
90 #define BMC150_MAGN_REG_TRIM_END 0x71
92 #define BMC150_MAGN_XY_OVERFLOW_VAL -4096
93 #define BMC150_MAGN_Z_OVERFLOW_VAL -16384
95 /* Time from SUSPEND to SLEEP */
96 #define BMC150_MAGN_START_UP_TIME_MS 3
98 #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS 2000
100 #define BMC150_MAGN_REGVAL_TO_REPXY(regval) (((regval) * 2) + 1)
101 #define BMC150_MAGN_REGVAL_TO_REPZ(regval) ((regval) + 1)
102 #define BMC150_MAGN_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2)
103 #define BMC150_MAGN_REPZ_TO_REGVAL(rep) ((rep) - 1)
105 enum bmc150_magn_axis {
106 AXIS_X,
107 AXIS_Y,
108 AXIS_Z,
109 RHALL,
110 AXIS_XYZ_MAX = RHALL,
111 AXIS_XYZR_MAX,
114 enum bmc150_magn_power_modes {
115 BMC150_MAGN_POWER_MODE_SUSPEND,
116 BMC150_MAGN_POWER_MODE_SLEEP,
117 BMC150_MAGN_POWER_MODE_NORMAL,
120 struct bmc150_magn_trim_regs {
121 s8 x1;
122 s8 y1;
123 __le16 reserved1;
124 u8 reserved2;
125 __le16 z4;
126 s8 x2;
127 s8 y2;
128 __le16 reserved3;
129 __le16 z2;
130 __le16 z1;
131 __le16 xyz1;
132 __le16 z3;
133 s8 xy2;
134 u8 xy1;
135 } __packed;
137 struct bmc150_magn_data {
138 struct i2c_client *client;
140 * 1. Protect this structure.
141 * 2. Serialize sequences that power on/off the device and access HW.
143 struct mutex mutex;
144 struct regmap *regmap;
145 /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */
146 s32 buffer[6];
147 struct iio_trigger *dready_trig;
148 bool dready_trigger_on;
149 int max_odr;
152 static const struct {
153 int freq;
154 u8 reg_val;
155 } bmc150_magn_samp_freq_table[] = { {2, 0x01},
156 {6, 0x02},
157 {8, 0x03},
158 {10, 0x00},
159 {15, 0x04},
160 {20, 0x05},
161 {25, 0x06},
162 {30, 0x07} };
164 enum bmc150_magn_presets {
165 LOW_POWER_PRESET,
166 REGULAR_PRESET,
167 ENHANCED_REGULAR_PRESET,
168 HIGH_ACCURACY_PRESET
171 static const struct bmc150_magn_preset {
172 u8 rep_xy;
173 u8 rep_z;
174 u8 odr;
175 } bmc150_magn_presets_table[] = {
176 [LOW_POWER_PRESET] = {3, 3, 10},
177 [REGULAR_PRESET] = {9, 15, 10},
178 [ENHANCED_REGULAR_PRESET] = {15, 27, 10},
179 [HIGH_ACCURACY_PRESET] = {47, 83, 20},
182 #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET
184 static bool bmc150_magn_is_writeable_reg(struct device *dev, unsigned int reg)
186 switch (reg) {
187 case BMC150_MAGN_REG_POWER:
188 case BMC150_MAGN_REG_OPMODE_ODR:
189 case BMC150_MAGN_REG_INT:
190 case BMC150_MAGN_REG_INT_DRDY:
191 case BMC150_MAGN_REG_LOW_THRESH:
192 case BMC150_MAGN_REG_HIGH_THRESH:
193 case BMC150_MAGN_REG_REP_XY:
194 case BMC150_MAGN_REG_REP_Z:
195 return true;
196 default:
197 return false;
201 static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg)
203 switch (reg) {
204 case BMC150_MAGN_REG_X_L:
205 case BMC150_MAGN_REG_X_M:
206 case BMC150_MAGN_REG_Y_L:
207 case BMC150_MAGN_REG_Y_M:
208 case BMC150_MAGN_REG_Z_L:
209 case BMC150_MAGN_REG_Z_M:
210 case BMC150_MAGN_REG_RHALL_L:
211 case BMC150_MAGN_REG_RHALL_M:
212 case BMC150_MAGN_REG_INT_STATUS:
213 return true;
214 default:
215 return false;
219 static const struct regmap_config bmc150_magn_regmap_config = {
220 .reg_bits = 8,
221 .val_bits = 8,
223 .max_register = BMC150_MAGN_REG_TRIM_END,
224 .cache_type = REGCACHE_RBTREE,
226 .writeable_reg = bmc150_magn_is_writeable_reg,
227 .volatile_reg = bmc150_magn_is_volatile_reg,
230 static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data,
231 enum bmc150_magn_power_modes mode,
232 bool state)
234 int ret;
236 switch (mode) {
237 case BMC150_MAGN_POWER_MODE_SUSPEND:
238 ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_POWER,
239 BMC150_MAGN_MASK_POWER_CTL, !state);
240 if (ret < 0)
241 return ret;
242 usleep_range(BMC150_MAGN_START_UP_TIME_MS * 1000, 20000);
243 return 0;
244 case BMC150_MAGN_POWER_MODE_SLEEP:
245 return regmap_update_bits(data->regmap,
246 BMC150_MAGN_REG_OPMODE_ODR,
247 BMC150_MAGN_MASK_OPMODE,
248 BMC150_MAGN_MODE_SLEEP <<
249 BMC150_MAGN_SHIFT_OPMODE);
250 case BMC150_MAGN_POWER_MODE_NORMAL:
251 return regmap_update_bits(data->regmap,
252 BMC150_MAGN_REG_OPMODE_ODR,
253 BMC150_MAGN_MASK_OPMODE,
254 BMC150_MAGN_MODE_NORMAL <<
255 BMC150_MAGN_SHIFT_OPMODE);
258 return -EINVAL;
261 static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
263 #ifdef CONFIG_PM
264 int ret;
266 if (on) {
267 ret = pm_runtime_get_sync(&data->client->dev);
268 } else {
269 pm_runtime_mark_last_busy(&data->client->dev);
270 ret = pm_runtime_put_autosuspend(&data->client->dev);
273 if (ret < 0) {
274 dev_err(&data->client->dev,
275 "failed to change power state to %d\n", on);
276 if (on)
277 pm_runtime_put_noidle(&data->client->dev);
279 return ret;
281 #endif
283 return 0;
286 static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val)
288 int ret, reg_val;
289 u8 i, odr_val;
291 ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, &reg_val);
292 if (ret < 0)
293 return ret;
294 odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
296 for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++)
297 if (bmc150_magn_samp_freq_table[i].reg_val == odr_val) {
298 *val = bmc150_magn_samp_freq_table[i].freq;
299 return 0;
302 return -EINVAL;
305 static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val)
307 int ret;
308 u8 i;
310 for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
311 if (bmc150_magn_samp_freq_table[i].freq == val) {
312 ret = regmap_update_bits(data->regmap,
313 BMC150_MAGN_REG_OPMODE_ODR,
314 BMC150_MAGN_MASK_ODR,
315 bmc150_magn_samp_freq_table[i].
316 reg_val <<
317 BMC150_MAGN_SHIFT_ODR);
318 if (ret < 0)
319 return ret;
320 return 0;
324 return -EINVAL;
327 static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy,
328 int rep_z, int odr)
330 int ret, reg_val, max_odr;
332 if (rep_xy <= 0) {
333 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY,
334 &reg_val);
335 if (ret < 0)
336 return ret;
337 rep_xy = BMC150_MAGN_REGVAL_TO_REPXY(reg_val);
339 if (rep_z <= 0) {
340 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z,
341 &reg_val);
342 if (ret < 0)
343 return ret;
344 rep_z = BMC150_MAGN_REGVAL_TO_REPZ(reg_val);
346 if (odr <= 0) {
347 ret = bmc150_magn_get_odr(data, &odr);
348 if (ret < 0)
349 return ret;
351 /* the maximum selectable read-out frequency from datasheet */
352 max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980);
353 if (odr > max_odr) {
354 dev_err(&data->client->dev,
355 "Can't set oversampling with sampling freq %d\n",
356 odr);
357 return -EINVAL;
359 data->max_odr = max_odr;
361 return 0;
364 static s32 bmc150_magn_compensate_x(struct bmc150_magn_trim_regs *tregs, s16 x,
365 u16 rhall)
367 s16 val;
368 u16 xyz1 = le16_to_cpu(tregs->xyz1);
370 if (x == BMC150_MAGN_XY_OVERFLOW_VAL)
371 return S32_MIN;
373 if (!rhall)
374 rhall = xyz1;
376 val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000)));
377 val = ((s16)((((s32)x) * ((((((((s32)tregs->xy2) * ((((s32)val) *
378 ((s32)val)) >> 7)) + (((s32)val) *
379 ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) *
380 ((s32)(((s16)tregs->x2) + ((s16)0xA0)))) >> 12)) >> 13)) +
381 (((s16)tregs->x1) << 3);
383 return (s32)val;
386 static s32 bmc150_magn_compensate_y(struct bmc150_magn_trim_regs *tregs, s16 y,
387 u16 rhall)
389 s16 val;
390 u16 xyz1 = le16_to_cpu(tregs->xyz1);
392 if (y == BMC150_MAGN_XY_OVERFLOW_VAL)
393 return S32_MIN;
395 if (!rhall)
396 rhall = xyz1;
398 val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000)));
399 val = ((s16)((((s32)y) * ((((((((s32)tregs->xy2) * ((((s32)val) *
400 ((s32)val)) >> 7)) + (((s32)val) *
401 ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) *
402 ((s32)(((s16)tregs->y2) + ((s16)0xA0)))) >> 12)) >> 13)) +
403 (((s16)tregs->y1) << 3);
405 return (s32)val;
408 static s32 bmc150_magn_compensate_z(struct bmc150_magn_trim_regs *tregs, s16 z,
409 u16 rhall)
411 s32 val;
412 u16 xyz1 = le16_to_cpu(tregs->xyz1);
413 u16 z1 = le16_to_cpu(tregs->z1);
414 s16 z2 = le16_to_cpu(tregs->z2);
415 s16 z3 = le16_to_cpu(tregs->z3);
416 s16 z4 = le16_to_cpu(tregs->z4);
418 if (z == BMC150_MAGN_Z_OVERFLOW_VAL)
419 return S32_MIN;
421 val = (((((s32)(z - z4)) << 15) - ((((s32)z3) * ((s32)(((s16)rhall) -
422 ((s16)xyz1)))) >> 2)) / (z2 + ((s16)(((((s32)z1) *
423 ((((s16)rhall) << 1))) + (1 << 15)) >> 16))));
425 return val;
428 static int bmc150_magn_read_xyz(struct bmc150_magn_data *data, s32 *buffer)
430 int ret;
431 __le16 values[AXIS_XYZR_MAX];
432 s16 raw_x, raw_y, raw_z;
433 u16 rhall;
434 struct bmc150_magn_trim_regs tregs;
436 ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_X_L,
437 values, sizeof(values));
438 if (ret < 0)
439 return ret;
441 raw_x = (s16)le16_to_cpu(values[AXIS_X]) >> BMC150_MAGN_SHIFT_XY_L;
442 raw_y = (s16)le16_to_cpu(values[AXIS_Y]) >> BMC150_MAGN_SHIFT_XY_L;
443 raw_z = (s16)le16_to_cpu(values[AXIS_Z]) >> BMC150_MAGN_SHIFT_Z_L;
444 rhall = le16_to_cpu(values[RHALL]) >> BMC150_MAGN_SHIFT_RHALL_L;
446 ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_TRIM_START,
447 &tregs, sizeof(tregs));
448 if (ret < 0)
449 return ret;
451 buffer[AXIS_X] = bmc150_magn_compensate_x(&tregs, raw_x, rhall);
452 buffer[AXIS_Y] = bmc150_magn_compensate_y(&tregs, raw_y, rhall);
453 buffer[AXIS_Z] = bmc150_magn_compensate_z(&tregs, raw_z, rhall);
455 return 0;
458 static int bmc150_magn_read_raw(struct iio_dev *indio_dev,
459 struct iio_chan_spec const *chan,
460 int *val, int *val2, long mask)
462 struct bmc150_magn_data *data = iio_priv(indio_dev);
463 int ret, tmp;
464 s32 values[AXIS_XYZ_MAX];
466 switch (mask) {
467 case IIO_CHAN_INFO_RAW:
468 if (iio_buffer_enabled(indio_dev))
469 return -EBUSY;
470 mutex_lock(&data->mutex);
472 ret = bmc150_magn_set_power_state(data, true);
473 if (ret < 0) {
474 mutex_unlock(&data->mutex);
475 return ret;
478 ret = bmc150_magn_read_xyz(data, values);
479 if (ret < 0) {
480 bmc150_magn_set_power_state(data, false);
481 mutex_unlock(&data->mutex);
482 return ret;
484 *val = values[chan->scan_index];
486 ret = bmc150_magn_set_power_state(data, false);
487 if (ret < 0) {
488 mutex_unlock(&data->mutex);
489 return ret;
492 mutex_unlock(&data->mutex);
493 return IIO_VAL_INT;
494 case IIO_CHAN_INFO_SCALE:
496 * The API/driver performs an off-chip temperature
497 * compensation and outputs x/y/z magnetic field data in
498 * 16 LSB/uT to the upper application layer.
500 *val = 0;
501 *val2 = 625;
502 return IIO_VAL_INT_PLUS_MICRO;
503 case IIO_CHAN_INFO_SAMP_FREQ:
504 ret = bmc150_magn_get_odr(data, val);
505 if (ret < 0)
506 return ret;
507 return IIO_VAL_INT;
508 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
509 switch (chan->channel2) {
510 case IIO_MOD_X:
511 case IIO_MOD_Y:
512 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY,
513 &tmp);
514 if (ret < 0)
515 return ret;
516 *val = BMC150_MAGN_REGVAL_TO_REPXY(tmp);
517 return IIO_VAL_INT;
518 case IIO_MOD_Z:
519 ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z,
520 &tmp);
521 if (ret < 0)
522 return ret;
523 *val = BMC150_MAGN_REGVAL_TO_REPZ(tmp);
524 return IIO_VAL_INT;
525 default:
526 return -EINVAL;
528 default:
529 return -EINVAL;
533 static int bmc150_magn_write_raw(struct iio_dev *indio_dev,
534 struct iio_chan_spec const *chan,
535 int val, int val2, long mask)
537 struct bmc150_magn_data *data = iio_priv(indio_dev);
538 int ret;
540 switch (mask) {
541 case IIO_CHAN_INFO_SAMP_FREQ:
542 if (val > data->max_odr)
543 return -EINVAL;
544 mutex_lock(&data->mutex);
545 ret = bmc150_magn_set_odr(data, val);
546 mutex_unlock(&data->mutex);
547 return ret;
548 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
549 switch (chan->channel2) {
550 case IIO_MOD_X:
551 case IIO_MOD_Y:
552 if (val < 1 || val > 511)
553 return -EINVAL;
554 mutex_lock(&data->mutex);
555 ret = bmc150_magn_set_max_odr(data, val, 0, 0);
556 if (ret < 0) {
557 mutex_unlock(&data->mutex);
558 return ret;
560 ret = regmap_update_bits(data->regmap,
561 BMC150_MAGN_REG_REP_XY,
562 0xFF,
563 BMC150_MAGN_REPXY_TO_REGVAL
564 (val));
565 mutex_unlock(&data->mutex);
566 return ret;
567 case IIO_MOD_Z:
568 if (val < 1 || val > 256)
569 return -EINVAL;
570 mutex_lock(&data->mutex);
571 ret = bmc150_magn_set_max_odr(data, 0, val, 0);
572 if (ret < 0) {
573 mutex_unlock(&data->mutex);
574 return ret;
576 ret = regmap_update_bits(data->regmap,
577 BMC150_MAGN_REG_REP_Z,
578 0xFF,
579 BMC150_MAGN_REPZ_TO_REGVAL
580 (val));
581 mutex_unlock(&data->mutex);
582 return ret;
583 default:
584 return -EINVAL;
586 default:
587 return -EINVAL;
591 static int bmc150_magn_validate_trigger(struct iio_dev *indio_dev,
592 struct iio_trigger *trig)
594 struct bmc150_magn_data *data = iio_priv(indio_dev);
596 if (data->dready_trig != trig)
597 return -EINVAL;
599 return 0;
602 static ssize_t bmc150_magn_show_samp_freq_avail(struct device *dev,
603 struct device_attribute *attr,
604 char *buf)
606 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
607 struct bmc150_magn_data *data = iio_priv(indio_dev);
608 size_t len = 0;
609 u8 i;
611 for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
612 if (bmc150_magn_samp_freq_table[i].freq > data->max_odr)
613 break;
614 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
615 bmc150_magn_samp_freq_table[i].freq);
617 /* replace last space with a newline */
618 buf[len - 1] = '\n';
620 return len;
623 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail);
625 static struct attribute *bmc150_magn_attributes[] = {
626 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
627 NULL,
630 static const struct attribute_group bmc150_magn_attrs_group = {
631 .attrs = bmc150_magn_attributes,
634 #define BMC150_MAGN_CHANNEL(_axis) { \
635 .type = IIO_MAGN, \
636 .modified = 1, \
637 .channel2 = IIO_MOD_##_axis, \
638 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
639 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
640 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
641 BIT(IIO_CHAN_INFO_SCALE), \
642 .scan_index = AXIS_##_axis, \
643 .scan_type = { \
644 .sign = 's', \
645 .realbits = 32, \
646 .storagebits = 32, \
647 .endianness = IIO_LE \
648 }, \
651 static const struct iio_chan_spec bmc150_magn_channels[] = {
652 BMC150_MAGN_CHANNEL(X),
653 BMC150_MAGN_CHANNEL(Y),
654 BMC150_MAGN_CHANNEL(Z),
655 IIO_CHAN_SOFT_TIMESTAMP(3),
658 static const struct iio_info bmc150_magn_info = {
659 .attrs = &bmc150_magn_attrs_group,
660 .read_raw = bmc150_magn_read_raw,
661 .write_raw = bmc150_magn_write_raw,
662 .validate_trigger = bmc150_magn_validate_trigger,
663 .driver_module = THIS_MODULE,
666 static const unsigned long bmc150_magn_scan_masks[] = {0x07, 0};
668 static irqreturn_t bmc150_magn_trigger_handler(int irq, void *p)
670 struct iio_poll_func *pf = p;
671 struct iio_dev *indio_dev = pf->indio_dev;
672 struct bmc150_magn_data *data = iio_priv(indio_dev);
673 int ret;
675 mutex_lock(&data->mutex);
676 ret = bmc150_magn_read_xyz(data, data->buffer);
677 mutex_unlock(&data->mutex);
678 if (ret < 0)
679 goto err;
681 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
682 pf->timestamp);
684 err:
685 iio_trigger_notify_done(data->dready_trig);
687 return IRQ_HANDLED;
690 static int bmc150_magn_init(struct bmc150_magn_data *data)
692 int ret, chip_id;
693 struct bmc150_magn_preset preset;
695 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND,
696 false);
697 if (ret < 0) {
698 dev_err(&data->client->dev,
699 "Failed to bring up device from suspend mode\n");
700 return ret;
703 ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id);
704 if (ret < 0) {
705 dev_err(&data->client->dev, "Failed reading chip id\n");
706 goto err_poweroff;
708 if (chip_id != BMC150_MAGN_CHIP_ID_VAL) {
709 dev_err(&data->client->dev, "Invalid chip id 0x%x\n", ret);
710 ret = -ENODEV;
711 goto err_poweroff;
713 dev_dbg(&data->client->dev, "Chip id %x\n", ret);
715 preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET];
716 ret = bmc150_magn_set_odr(data, preset.odr);
717 if (ret < 0) {
718 dev_err(&data->client->dev, "Failed to set ODR to %d\n",
719 preset.odr);
720 goto err_poweroff;
723 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY,
724 BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy));
725 if (ret < 0) {
726 dev_err(&data->client->dev, "Failed to set REP XY to %d\n",
727 preset.rep_xy);
728 goto err_poweroff;
731 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z,
732 BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z));
733 if (ret < 0) {
734 dev_err(&data->client->dev, "Failed to set REP Z to %d\n",
735 preset.rep_z);
736 goto err_poweroff;
739 ret = bmc150_magn_set_max_odr(data, preset.rep_xy, preset.rep_z,
740 preset.odr);
741 if (ret < 0)
742 goto err_poweroff;
744 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
745 true);
746 if (ret < 0) {
747 dev_err(&data->client->dev, "Failed to power on device\n");
748 goto err_poweroff;
751 return 0;
753 err_poweroff:
754 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
755 return ret;
758 static int bmc150_magn_reset_intr(struct bmc150_magn_data *data)
760 int tmp;
763 * Data Ready (DRDY) is always cleared after
764 * readout of data registers ends.
766 return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp);
769 static int bmc150_magn_trig_try_reen(struct iio_trigger *trig)
771 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
772 struct bmc150_magn_data *data = iio_priv(indio_dev);
773 int ret;
775 if (!data->dready_trigger_on)
776 return 0;
778 mutex_lock(&data->mutex);
779 ret = bmc150_magn_reset_intr(data);
780 mutex_unlock(&data->mutex);
782 return ret;
785 static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
786 bool state)
788 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
789 struct bmc150_magn_data *data = iio_priv(indio_dev);
790 int ret = 0;
792 mutex_lock(&data->mutex);
793 if (state == data->dready_trigger_on)
794 goto err_unlock;
796 ret = bmc150_magn_set_power_state(data, state);
797 if (ret < 0)
798 goto err_unlock;
800 ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
801 BMC150_MAGN_MASK_DRDY_EN,
802 state << BMC150_MAGN_SHIFT_DRDY_EN);
803 if (ret < 0)
804 goto err_poweroff;
806 data->dready_trigger_on = state;
808 if (state) {
809 ret = bmc150_magn_reset_intr(data);
810 if (ret < 0)
811 goto err_poweroff;
813 mutex_unlock(&data->mutex);
815 return 0;
817 err_poweroff:
818 bmc150_magn_set_power_state(data, false);
819 err_unlock:
820 mutex_unlock(&data->mutex);
821 return ret;
824 static const struct iio_trigger_ops bmc150_magn_trigger_ops = {
825 .set_trigger_state = bmc150_magn_data_rdy_trigger_set_state,
826 .try_reenable = bmc150_magn_trig_try_reen,
827 .owner = THIS_MODULE,
830 static int bmc150_magn_gpio_probe(struct i2c_client *client)
832 struct device *dev;
833 struct gpio_desc *gpio;
834 int ret;
836 if (!client)
837 return -EINVAL;
839 dev = &client->dev;
841 /* data ready GPIO interrupt pin */
842 gpio = devm_gpiod_get_index(dev, BMC150_MAGN_GPIO_INT, 0, GPIOD_IN);
843 if (IS_ERR(gpio)) {
844 dev_err(dev, "ACPI GPIO get index failed\n");
845 return PTR_ERR(gpio);
848 ret = gpiod_to_irq(gpio);
850 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
852 return ret;
855 static const char *bmc150_magn_match_acpi_device(struct device *dev)
857 const struct acpi_device_id *id;
859 id = acpi_match_device(dev->driver->acpi_match_table, dev);
860 if (!id)
861 return NULL;
863 return dev_name(dev);
866 static int bmc150_magn_probe(struct i2c_client *client,
867 const struct i2c_device_id *id)
869 struct bmc150_magn_data *data;
870 struct iio_dev *indio_dev;
871 const char *name = NULL;
872 int ret;
874 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
875 if (!indio_dev)
876 return -ENOMEM;
878 data = iio_priv(indio_dev);
879 i2c_set_clientdata(client, indio_dev);
880 data->client = client;
882 if (id)
883 name = id->name;
884 else if (ACPI_HANDLE(&client->dev))
885 name = bmc150_magn_match_acpi_device(&client->dev);
886 else
887 return -ENOSYS;
889 mutex_init(&data->mutex);
890 data->regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config);
891 if (IS_ERR(data->regmap)) {
892 dev_err(&client->dev, "Failed to allocate register map\n");
893 return PTR_ERR(data->regmap);
896 ret = bmc150_magn_init(data);
897 if (ret < 0)
898 return ret;
900 indio_dev->dev.parent = &client->dev;
901 indio_dev->channels = bmc150_magn_channels;
902 indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels);
903 indio_dev->available_scan_masks = bmc150_magn_scan_masks;
904 indio_dev->name = name;
905 indio_dev->modes = INDIO_DIRECT_MODE;
906 indio_dev->info = &bmc150_magn_info;
908 if (client->irq <= 0)
909 client->irq = bmc150_magn_gpio_probe(client);
911 if (client->irq > 0) {
912 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
913 "%s-dev%d",
914 indio_dev->name,
915 indio_dev->id);
916 if (!data->dready_trig) {
917 ret = -ENOMEM;
918 dev_err(&client->dev, "iio trigger alloc failed\n");
919 goto err_poweroff;
922 data->dready_trig->dev.parent = &client->dev;
923 data->dready_trig->ops = &bmc150_magn_trigger_ops;
924 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
925 ret = iio_trigger_register(data->dready_trig);
926 if (ret) {
927 dev_err(&client->dev, "iio trigger register failed\n");
928 goto err_poweroff;
931 ret = iio_triggered_buffer_setup(indio_dev,
932 &iio_pollfunc_store_time,
933 bmc150_magn_trigger_handler,
934 NULL);
935 if (ret < 0) {
936 dev_err(&client->dev,
937 "iio triggered buffer setup failed\n");
938 goto err_trigger_unregister;
941 ret = request_threaded_irq(client->irq,
942 iio_trigger_generic_data_rdy_poll,
943 NULL,
944 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
945 BMC150_MAGN_IRQ_NAME,
946 data->dready_trig);
947 if (ret < 0) {
948 dev_err(&client->dev, "request irq %d failed\n",
949 client->irq);
950 goto err_buffer_cleanup;
954 ret = iio_device_register(indio_dev);
955 if (ret < 0) {
956 dev_err(&client->dev, "unable to register iio device\n");
957 goto err_free_irq;
960 ret = pm_runtime_set_active(&client->dev);
961 if (ret)
962 goto err_iio_unregister;
964 pm_runtime_enable(&client->dev);
965 pm_runtime_set_autosuspend_delay(&client->dev,
966 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS);
967 pm_runtime_use_autosuspend(&client->dev);
969 dev_dbg(&indio_dev->dev, "Registered device %s\n", name);
971 return 0;
973 err_iio_unregister:
974 iio_device_unregister(indio_dev);
975 err_free_irq:
976 if (client->irq > 0)
977 free_irq(client->irq, data->dready_trig);
978 err_buffer_cleanup:
979 if (data->dready_trig)
980 iio_triggered_buffer_cleanup(indio_dev);
981 err_trigger_unregister:
982 if (data->dready_trig)
983 iio_trigger_unregister(data->dready_trig);
984 err_poweroff:
985 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
986 return ret;
989 static int bmc150_magn_remove(struct i2c_client *client)
991 struct iio_dev *indio_dev = i2c_get_clientdata(client);
992 struct bmc150_magn_data *data = iio_priv(indio_dev);
994 pm_runtime_disable(&client->dev);
995 pm_runtime_set_suspended(&client->dev);
996 pm_runtime_put_noidle(&client->dev);
998 iio_device_unregister(indio_dev);
1000 if (client->irq > 0)
1001 free_irq(data->client->irq, data->dready_trig);
1003 if (data->dready_trig) {
1004 iio_triggered_buffer_cleanup(indio_dev);
1005 iio_trigger_unregister(data->dready_trig);
1008 mutex_lock(&data->mutex);
1009 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
1010 mutex_unlock(&data->mutex);
1012 return 0;
1015 #ifdef CONFIG_PM
1016 static int bmc150_magn_runtime_suspend(struct device *dev)
1018 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1019 struct bmc150_magn_data *data = iio_priv(indio_dev);
1020 int ret;
1022 mutex_lock(&data->mutex);
1023 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
1024 true);
1025 mutex_unlock(&data->mutex);
1026 if (ret < 0) {
1027 dev_err(&data->client->dev, "powering off device failed\n");
1028 return ret;
1030 return 0;
1033 static int bmc150_magn_runtime_resume(struct device *dev)
1035 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1036 struct bmc150_magn_data *data = iio_priv(indio_dev);
1038 return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
1039 true);
1041 #endif
1043 #ifdef CONFIG_PM_SLEEP
1044 static int bmc150_magn_suspend(struct device *dev)
1046 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1047 struct bmc150_magn_data *data = iio_priv(indio_dev);
1048 int ret;
1050 mutex_lock(&data->mutex);
1051 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
1052 true);
1053 mutex_unlock(&data->mutex);
1055 return ret;
1058 static int bmc150_magn_resume(struct device *dev)
1060 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1061 struct bmc150_magn_data *data = iio_priv(indio_dev);
1062 int ret;
1064 mutex_lock(&data->mutex);
1065 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
1066 true);
1067 mutex_unlock(&data->mutex);
1069 return ret;
1071 #endif
1073 static const struct dev_pm_ops bmc150_magn_pm_ops = {
1074 SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume)
1075 SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend,
1076 bmc150_magn_runtime_resume, NULL)
1079 static const struct acpi_device_id bmc150_magn_acpi_match[] = {
1080 {"BMC150B", 0},
1083 MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match);
1085 static const struct i2c_device_id bmc150_magn_id[] = {
1086 {"bmc150_magn", 0},
1089 MODULE_DEVICE_TABLE(i2c, bmc150_magn_id);
1091 static struct i2c_driver bmc150_magn_driver = {
1092 .driver = {
1093 .name = BMC150_MAGN_DRV_NAME,
1094 .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match),
1095 .pm = &bmc150_magn_pm_ops,
1097 .probe = bmc150_magn_probe,
1098 .remove = bmc150_magn_remove,
1099 .id_table = bmc150_magn_id,
1101 module_i2c_driver(bmc150_magn_driver);
1103 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1104 MODULE_LICENSE("GPL v2");
1105 MODULE_DESCRIPTION("BMC150 magnetometer driver");