1 /*
2  * Bosch BMC150 three-axis magnetic field sensor driver
3  *
4  * Copyright (c) 2015, Intel Corporation.
5  *
6  * This code is based on bmm050_api.c authored by contact@bosch.sensortec.com:
7  *
8  * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
9  *
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.
13  *
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.
18  */
19 
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>
37 
38 #define BMC150_MAGN_DRV_NAME			"bmc150_magn"
39 #define BMC150_MAGN_IRQ_NAME			"bmc150_magn_event"
40 #define BMC150_MAGN_GPIO_INT			"interrupt"
41 
42 #define BMC150_MAGN_REG_CHIP_ID			0x40
43 #define BMC150_MAGN_CHIP_ID_VAL			0x32
44 
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
56 
57 #define BMC150_MAGN_REG_INT_STATUS		0x4A
58 
59 #define BMC150_MAGN_REG_POWER			0x4B
60 #define BMC150_MAGN_MASK_POWER_CTL		BIT(0)
61 
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
70 
71 #define BMC150_MAGN_REG_INT			0x4D
72 
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)
83 
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)
89 
90 #define BMC150_MAGN_REG_TRIM_START		0x5D
91 #define BMC150_MAGN_REG_TRIM_END		0x71
92 
93 #define BMC150_MAGN_XY_OVERFLOW_VAL		-4096
94 #define BMC150_MAGN_Z_OVERFLOW_VAL		-16384
95 
96 /* Time from SUSPEND to SLEEP */
97 #define BMC150_MAGN_START_UP_TIME_MS		3
98 
99 #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS	2000
100 
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)
105 
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,
113 };
114 
115 enum bmc150_magn_power_modes {
116 	BMC150_MAGN_POWER_MODE_SUSPEND,
117 	BMC150_MAGN_POWER_MODE_SLEEP,
118 	BMC150_MAGN_POWER_MODE_NORMAL,
119 };
120 
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;
137 
138 struct bmc150_magn_data {
139 	struct i2c_client *client;
140 	/*
141 	 * 1. Protect this structure.
142 	 * 2. Serialize sequences that power on/off the device and access HW.
143 	 */
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 };
152 
153 static const struct {
154 	int freq;
155 	u8 reg_val;
156 } bmc150_magn_samp_freq_table[] = { {2, 0x01},
157 				    {6, 0x02},
158 				    {8, 0x03},
159 				    {10, 0x00},
160 				    {15, 0x04},
161 				    {20, 0x05},
162 				    {25, 0x06},
163 				    {30, 0x07} };
164 
165 enum bmc150_magn_presets {
166 	LOW_POWER_PRESET,
167 	REGULAR_PRESET,
168 	ENHANCED_REGULAR_PRESET,
169 	HIGH_ACCURACY_PRESET
170 };
171 
172 static const struct bmc150_magn_preset {
173 	u8 rep_xy;
174 	u8 rep_z;
175 	u8 odr;
176 } bmc150_magn_presets_table[] = {
177 	[LOW_POWER_PRESET] = {3, 3, 10},
178 	[REGULAR_PRESET] =  {9, 15, 10},
179 	[ENHANCED_REGULAR_PRESET] =  {15, 27, 10},
180 	[HIGH_ACCURACY_PRESET] =  {47, 83, 20},
181 };
182 
183 #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET
184 
185 static bool bmc150_magn_is_writeable_reg(struct device *dev, unsigned int reg)
186 {
187 	switch (reg) {
188 	case BMC150_MAGN_REG_POWER:
189 	case BMC150_MAGN_REG_OPMODE_ODR:
190 	case BMC150_MAGN_REG_INT:
191 	case BMC150_MAGN_REG_INT_DRDY:
192 	case BMC150_MAGN_REG_LOW_THRESH:
193 	case BMC150_MAGN_REG_HIGH_THRESH:
194 	case BMC150_MAGN_REG_REP_XY:
195 	case BMC150_MAGN_REG_REP_Z:
196 		return true;
197 	default:
198 		return false;
199 	};
200 }
201 
202 static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg)
203 {
204 	switch (reg) {
205 	case BMC150_MAGN_REG_X_L:
206 	case BMC150_MAGN_REG_X_M:
207 	case BMC150_MAGN_REG_Y_L:
208 	case BMC150_MAGN_REG_Y_M:
209 	case BMC150_MAGN_REG_Z_L:
210 	case BMC150_MAGN_REG_Z_M:
211 	case BMC150_MAGN_REG_RHALL_L:
212 	case BMC150_MAGN_REG_RHALL_M:
213 	case BMC150_MAGN_REG_INT_STATUS:
214 		return true;
215 	default:
216 		return false;
217 	}
218 }
219 
220 static const struct regmap_config bmc150_magn_regmap_config = {
221 	.reg_bits = 8,
222 	.val_bits = 8,
223 
224 	.max_register = BMC150_MAGN_REG_TRIM_END,
225 	.cache_type = REGCACHE_RBTREE,
226 
227 	.writeable_reg = bmc150_magn_is_writeable_reg,
228 	.volatile_reg = bmc150_magn_is_volatile_reg,
229 };
230 
231 static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data,
232 				      enum bmc150_magn_power_modes mode,
233 				      bool state)
234 {
235 	int ret;
236 
237 	switch (mode) {
238 	case BMC150_MAGN_POWER_MODE_SUSPEND:
239 		ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_POWER,
240 					 BMC150_MAGN_MASK_POWER_CTL, !state);
241 		if (ret < 0)
242 			return ret;
243 		usleep_range(BMC150_MAGN_START_UP_TIME_MS * 1000, 20000);
244 		return 0;
245 	case BMC150_MAGN_POWER_MODE_SLEEP:
246 		return regmap_update_bits(data->regmap,
247 					  BMC150_MAGN_REG_OPMODE_ODR,
248 					  BMC150_MAGN_MASK_OPMODE,
249 					  BMC150_MAGN_MODE_SLEEP <<
250 					  BMC150_MAGN_SHIFT_OPMODE);
251 	case BMC150_MAGN_POWER_MODE_NORMAL:
252 		return regmap_update_bits(data->regmap,
253 					  BMC150_MAGN_REG_OPMODE_ODR,
254 					  BMC150_MAGN_MASK_OPMODE,
255 					  BMC150_MAGN_MODE_NORMAL <<
256 					  BMC150_MAGN_SHIFT_OPMODE);
257 	}
258 
259 	return -EINVAL;
260 }
261 
262 static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
263 {
264 #ifdef CONFIG_PM
265 	int ret;
266 
267 	if (on) {
268 		ret = pm_runtime_get_sync(&data->client->dev);
269 	} else {
270 		pm_runtime_mark_last_busy(&data->client->dev);
271 		ret = pm_runtime_put_autosuspend(&data->client->dev);
272 	}
273 
274 	if (ret < 0) {
275 		dev_err(&data->client->dev,
276 			"failed to change power state to %d\n", on);
277 		if (on)
278 			pm_runtime_put_noidle(&data->client->dev);
279 
280 		return ret;
281 	}
282 #endif
283 
284 	return 0;
285 }
286 
287 static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val)
288 {
289 	int ret, reg_val;
290 	u8 i, odr_val;
291 
292 	ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, &reg_val);
293 	if (ret < 0)
294 		return ret;
295 	odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
296 
297 	for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++)
298 		if (bmc150_magn_samp_freq_table[i].reg_val == odr_val) {
299 			*val = bmc150_magn_samp_freq_table[i].freq;
300 			return 0;
301 		}
302 
303 	return -EINVAL;
304 }
305 
306 static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val)
307 {
308 	int ret;
309 	u8 i;
310 
311 	for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
312 		if (bmc150_magn_samp_freq_table[i].freq == val) {
313 			ret = regmap_update_bits(data->regmap,
314 						 BMC150_MAGN_REG_OPMODE_ODR,
315 						 BMC150_MAGN_MASK_ODR,
316 						 bmc150_magn_samp_freq_table[i].
317 						 reg_val <<
318 						 BMC150_MAGN_SHIFT_ODR);
319 			if (ret < 0)
320 				return ret;
321 			return 0;
322 		}
323 	}
324 
325 	return -EINVAL;
326 }
327 
328 static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy,
329 				   int rep_z, int odr)
330 {
331 	int ret, reg_val, max_odr;
332 
333 	if (rep_xy <= 0) {
334 		ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY,
335 				  &reg_val);
336 		if (ret < 0)
337 			return ret;
338 		rep_xy = BMC150_MAGN_REGVAL_TO_REPXY(reg_val);
339 	}
340 	if (rep_z <= 0) {
341 		ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z,
342 				  &reg_val);
343 		if (ret < 0)
344 			return ret;
345 		rep_z = BMC150_MAGN_REGVAL_TO_REPZ(reg_val);
346 	}
347 	if (odr <= 0) {
348 		ret = bmc150_magn_get_odr(data, &odr);
349 		if (ret < 0)
350 			return ret;
351 	}
352 	/* the maximum selectable read-out frequency from datasheet */
353 	max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980);
354 	if (odr > max_odr) {
355 		dev_err(&data->client->dev,
356 			"Can't set oversampling with sampling freq %d\n",
357 			odr);
358 		return -EINVAL;
359 	}
360 	data->max_odr = max_odr;
361 
362 	return 0;
363 }
364 
365 static s32 bmc150_magn_compensate_x(struct bmc150_magn_trim_regs *tregs, s16 x,
366 				    u16 rhall)
367 {
368 	s16 val;
369 	u16 xyz1 = le16_to_cpu(tregs->xyz1);
370 
371 	if (x == BMC150_MAGN_XY_OVERFLOW_VAL)
372 		return S32_MIN;
373 
374 	if (!rhall)
375 		rhall = xyz1;
376 
377 	val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000)));
378 	val = ((s16)((((s32)x) * ((((((((s32)tregs->xy2) * ((((s32)val) *
379 	      ((s32)val)) >> 7)) + (((s32)val) *
380 	      ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) *
381 	      ((s32)(((s16)tregs->x2) + ((s16)0xA0)))) >> 12)) >> 13)) +
382 	      (((s16)tregs->x1) << 3);
383 
384 	return (s32)val;
385 }
386 
387 static s32 bmc150_magn_compensate_y(struct bmc150_magn_trim_regs *tregs, s16 y,
388 				    u16 rhall)
389 {
390 	s16 val;
391 	u16 xyz1 = le16_to_cpu(tregs->xyz1);
392 
393 	if (y == BMC150_MAGN_XY_OVERFLOW_VAL)
394 		return S32_MIN;
395 
396 	if (!rhall)
397 		rhall = xyz1;
398 
399 	val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000)));
400 	val = ((s16)((((s32)y) * ((((((((s32)tregs->xy2) * ((((s32)val) *
401 	      ((s32)val)) >> 7)) + (((s32)val) *
402 	      ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) *
403 	      ((s32)(((s16)tregs->y2) + ((s16)0xA0)))) >> 12)) >> 13)) +
404 	      (((s16)tregs->y1) << 3);
405 
406 	return (s32)val;
407 }
408 
409 static s32 bmc150_magn_compensate_z(struct bmc150_magn_trim_regs *tregs, s16 z,
410 				    u16 rhall)
411 {
412 	s32 val;
413 	u16 xyz1 = le16_to_cpu(tregs->xyz1);
414 	u16 z1 = le16_to_cpu(tregs->z1);
415 	s16 z2 = le16_to_cpu(tregs->z2);
416 	s16 z3 = le16_to_cpu(tregs->z3);
417 	s16 z4 = le16_to_cpu(tregs->z4);
418 
419 	if (z == BMC150_MAGN_Z_OVERFLOW_VAL)
420 		return S32_MIN;
421 
422 	val = (((((s32)(z - z4)) << 15) - ((((s32)z3) * ((s32)(((s16)rhall) -
423 	      ((s16)xyz1)))) >> 2)) / (z2 + ((s16)(((((s32)z1) *
424 	      ((((s16)rhall) << 1))) + (1 << 15)) >> 16))));
425 
426 	return val;
427 }
428 
429 static int bmc150_magn_read_xyz(struct bmc150_magn_data *data, s32 *buffer)
430 {
431 	int ret;
432 	__le16 values[AXIS_XYZR_MAX];
433 	s16 raw_x, raw_y, raw_z;
434 	u16 rhall;
435 	struct bmc150_magn_trim_regs tregs;
436 
437 	ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_X_L,
438 			       values, sizeof(values));
439 	if (ret < 0)
440 		return ret;
441 
442 	raw_x = (s16)le16_to_cpu(values[AXIS_X]) >> BMC150_MAGN_SHIFT_XY_L;
443 	raw_y = (s16)le16_to_cpu(values[AXIS_Y]) >> BMC150_MAGN_SHIFT_XY_L;
444 	raw_z = (s16)le16_to_cpu(values[AXIS_Z]) >> BMC150_MAGN_SHIFT_Z_L;
445 	rhall = le16_to_cpu(values[RHALL]) >> BMC150_MAGN_SHIFT_RHALL_L;
446 
447 	ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_TRIM_START,
448 			       &tregs, sizeof(tregs));
449 	if (ret < 0)
450 		return ret;
451 
452 	buffer[AXIS_X] = bmc150_magn_compensate_x(&tregs, raw_x, rhall);
453 	buffer[AXIS_Y] = bmc150_magn_compensate_y(&tregs, raw_y, rhall);
454 	buffer[AXIS_Z] = bmc150_magn_compensate_z(&tregs, raw_z, rhall);
455 
456 	return 0;
457 }
458 
459 static int bmc150_magn_read_raw(struct iio_dev *indio_dev,
460 				struct iio_chan_spec const *chan,
461 				int *val, int *val2, long mask)
462 {
463 	struct bmc150_magn_data *data = iio_priv(indio_dev);
464 	int ret, tmp;
465 	s32 values[AXIS_XYZ_MAX];
466 
467 	switch (mask) {
468 	case IIO_CHAN_INFO_RAW:
469 		if (iio_buffer_enabled(indio_dev))
470 			return -EBUSY;
471 		mutex_lock(&data->mutex);
472 
473 		ret = bmc150_magn_set_power_state(data, true);
474 		if (ret < 0) {
475 			mutex_unlock(&data->mutex);
476 			return ret;
477 		}
478 
479 		ret = bmc150_magn_read_xyz(data, values);
480 		if (ret < 0) {
481 			bmc150_magn_set_power_state(data, false);
482 			mutex_unlock(&data->mutex);
483 			return ret;
484 		}
485 		*val = values[chan->scan_index];
486 
487 		ret = bmc150_magn_set_power_state(data, false);
488 		if (ret < 0) {
489 			mutex_unlock(&data->mutex);
490 			return ret;
491 		}
492 
493 		mutex_unlock(&data->mutex);
494 		return IIO_VAL_INT;
495 	case IIO_CHAN_INFO_SCALE:
496 		/*
497 		 * The API/driver performs an off-chip temperature
498 		 * compensation and outputs x/y/z magnetic field data in
499 		 * 16 LSB/uT to the upper application layer.
500 		 */
501 		*val = 0;
502 		*val2 = 625;
503 		return IIO_VAL_INT_PLUS_MICRO;
504 	case IIO_CHAN_INFO_SAMP_FREQ:
505 		ret = bmc150_magn_get_odr(data, val);
506 		if (ret < 0)
507 			return ret;
508 		return IIO_VAL_INT;
509 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
510 		switch (chan->channel2) {
511 		case IIO_MOD_X:
512 		case IIO_MOD_Y:
513 			ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY,
514 					  &tmp);
515 			if (ret < 0)
516 				return ret;
517 			*val = BMC150_MAGN_REGVAL_TO_REPXY(tmp);
518 			return IIO_VAL_INT;
519 		case IIO_MOD_Z:
520 			ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z,
521 					  &tmp);
522 			if (ret < 0)
523 				return ret;
524 			*val = BMC150_MAGN_REGVAL_TO_REPZ(tmp);
525 			return IIO_VAL_INT;
526 		default:
527 			return -EINVAL;
528 		}
529 	default:
530 		return -EINVAL;
531 	}
532 }
533 
534 static int bmc150_magn_write_raw(struct iio_dev *indio_dev,
535 				 struct iio_chan_spec const *chan,
536 				 int val, int val2, long mask)
537 {
538 	struct bmc150_magn_data *data = iio_priv(indio_dev);
539 	int ret;
540 
541 	switch (mask) {
542 	case IIO_CHAN_INFO_SAMP_FREQ:
543 		if (val > data->max_odr)
544 			return -EINVAL;
545 		mutex_lock(&data->mutex);
546 		ret = bmc150_magn_set_odr(data, val);
547 		mutex_unlock(&data->mutex);
548 		return ret;
549 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
550 		switch (chan->channel2) {
551 		case IIO_MOD_X:
552 		case IIO_MOD_Y:
553 			if (val < 1 || val > 511)
554 				return -EINVAL;
555 			mutex_lock(&data->mutex);
556 			ret = bmc150_magn_set_max_odr(data, val, 0, 0);
557 			if (ret < 0) {
558 				mutex_unlock(&data->mutex);
559 				return ret;
560 			}
561 			ret = regmap_update_bits(data->regmap,
562 						 BMC150_MAGN_REG_REP_XY,
563 						 BMC150_MAGN_REG_REP_DATAMASK,
564 						 BMC150_MAGN_REPXY_TO_REGVAL
565 						 (val));
566 			mutex_unlock(&data->mutex);
567 			return ret;
568 		case IIO_MOD_Z:
569 			if (val < 1 || val > 256)
570 				return -EINVAL;
571 			mutex_lock(&data->mutex);
572 			ret = bmc150_magn_set_max_odr(data, 0, val, 0);
573 			if (ret < 0) {
574 				mutex_unlock(&data->mutex);
575 				return ret;
576 			}
577 			ret = regmap_update_bits(data->regmap,
578 						 BMC150_MAGN_REG_REP_Z,
579 						 BMC150_MAGN_REG_REP_DATAMASK,
580 						 BMC150_MAGN_REPZ_TO_REGVAL
581 						 (val));
582 			mutex_unlock(&data->mutex);
583 			return ret;
584 		default:
585 			return -EINVAL;
586 		}
587 	default:
588 		return -EINVAL;
589 	}
590 }
591 
592 static ssize_t bmc150_magn_show_samp_freq_avail(struct device *dev,
593 						struct device_attribute *attr,
594 						char *buf)
595 {
596 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
597 	struct bmc150_magn_data *data = iio_priv(indio_dev);
598 	size_t len = 0;
599 	u8 i;
600 
601 	for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
602 		if (bmc150_magn_samp_freq_table[i].freq > data->max_odr)
603 			break;
604 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
605 				 bmc150_magn_samp_freq_table[i].freq);
606 	}
607 	/* replace last space with a newline */
608 	buf[len - 1] = '\n';
609 
610 	return len;
611 }
612 
613 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail);
614 
615 static struct attribute *bmc150_magn_attributes[] = {
616 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
617 	NULL,
618 };
619 
620 static const struct attribute_group bmc150_magn_attrs_group = {
621 	.attrs = bmc150_magn_attributes,
622 };
623 
624 #define BMC150_MAGN_CHANNEL(_axis) {					\
625 	.type = IIO_MAGN,						\
626 	.modified = 1,							\
627 	.channel2 = IIO_MOD_##_axis,					\
628 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |			\
629 			      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),	\
630 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
631 				    BIT(IIO_CHAN_INFO_SCALE),		\
632 	.scan_index = AXIS_##_axis,					\
633 	.scan_type = {							\
634 		.sign = 's',						\
635 		.realbits = 32,						\
636 		.storagebits = 32,					\
637 		.endianness = IIO_LE					\
638 	},								\
639 }
640 
641 static const struct iio_chan_spec bmc150_magn_channels[] = {
642 	BMC150_MAGN_CHANNEL(X),
643 	BMC150_MAGN_CHANNEL(Y),
644 	BMC150_MAGN_CHANNEL(Z),
645 	IIO_CHAN_SOFT_TIMESTAMP(3),
646 };
647 
648 static const struct iio_info bmc150_magn_info = {
649 	.attrs = &bmc150_magn_attrs_group,
650 	.read_raw = bmc150_magn_read_raw,
651 	.write_raw = bmc150_magn_write_raw,
652 	.driver_module = THIS_MODULE,
653 };
654 
655 static const unsigned long bmc150_magn_scan_masks[] = {
656 					BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
657 					0};
658 
659 static irqreturn_t bmc150_magn_trigger_handler(int irq, void *p)
660 {
661 	struct iio_poll_func *pf = p;
662 	struct iio_dev *indio_dev = pf->indio_dev;
663 	struct bmc150_magn_data *data = iio_priv(indio_dev);
664 	int ret;
665 
666 	mutex_lock(&data->mutex);
667 	ret = bmc150_magn_read_xyz(data, data->buffer);
668 	if (ret < 0)
669 		goto err;
670 
671 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
672 					   pf->timestamp);
673 
674 err:
675 	mutex_unlock(&data->mutex);
676 	iio_trigger_notify_done(indio_dev->trig);
677 
678 	return IRQ_HANDLED;
679 }
680 
681 static int bmc150_magn_init(struct bmc150_magn_data *data)
682 {
683 	int ret, chip_id;
684 	struct bmc150_magn_preset preset;
685 
686 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND,
687 					 false);
688 	if (ret < 0) {
689 		dev_err(&data->client->dev,
690 			"Failed to bring up device from suspend mode\n");
691 		return ret;
692 	}
693 
694 	ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id);
695 	if (ret < 0) {
696 		dev_err(&data->client->dev, "Failed reading chip id\n");
697 		goto err_poweroff;
698 	}
699 	if (chip_id != BMC150_MAGN_CHIP_ID_VAL) {
700 		dev_err(&data->client->dev, "Invalid chip id 0x%x\n", chip_id);
701 		ret = -ENODEV;
702 		goto err_poweroff;
703 	}
704 	dev_dbg(&data->client->dev, "Chip id %x\n", chip_id);
705 
706 	preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET];
707 	ret = bmc150_magn_set_odr(data, preset.odr);
708 	if (ret < 0) {
709 		dev_err(&data->client->dev, "Failed to set ODR to %d\n",
710 			preset.odr);
711 		goto err_poweroff;
712 	}
713 
714 	ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY,
715 			   BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy));
716 	if (ret < 0) {
717 		dev_err(&data->client->dev, "Failed to set REP XY to %d\n",
718 			preset.rep_xy);
719 		goto err_poweroff;
720 	}
721 
722 	ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z,
723 			   BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z));
724 	if (ret < 0) {
725 		dev_err(&data->client->dev, "Failed to set REP Z to %d\n",
726 			preset.rep_z);
727 		goto err_poweroff;
728 	}
729 
730 	ret = bmc150_magn_set_max_odr(data, preset.rep_xy, preset.rep_z,
731 				      preset.odr);
732 	if (ret < 0)
733 		goto err_poweroff;
734 
735 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
736 					 true);
737 	if (ret < 0) {
738 		dev_err(&data->client->dev, "Failed to power on device\n");
739 		goto err_poweroff;
740 	}
741 
742 	return 0;
743 
744 err_poweroff:
745 	bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
746 	return ret;
747 }
748 
749 static int bmc150_magn_reset_intr(struct bmc150_magn_data *data)
750 {
751 	int tmp;
752 
753 	/*
754 	 * Data Ready (DRDY) is always cleared after
755 	 * readout of data registers ends.
756 	 */
757 	return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp);
758 }
759 
760 static int bmc150_magn_trig_try_reen(struct iio_trigger *trig)
761 {
762 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
763 	struct bmc150_magn_data *data = iio_priv(indio_dev);
764 	int ret;
765 
766 	if (!data->dready_trigger_on)
767 		return 0;
768 
769 	mutex_lock(&data->mutex);
770 	ret = bmc150_magn_reset_intr(data);
771 	mutex_unlock(&data->mutex);
772 
773 	return ret;
774 }
775 
776 static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
777 						  bool state)
778 {
779 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
780 	struct bmc150_magn_data *data = iio_priv(indio_dev);
781 	int ret = 0;
782 
783 	mutex_lock(&data->mutex);
784 	if (state == data->dready_trigger_on)
785 		goto err_unlock;
786 
787 	ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
788 				 BMC150_MAGN_MASK_DRDY_EN,
789 				 state << BMC150_MAGN_SHIFT_DRDY_EN);
790 	if (ret < 0)
791 		goto err_unlock;
792 
793 	data->dready_trigger_on = state;
794 
795 	if (state) {
796 		ret = bmc150_magn_reset_intr(data);
797 		if (ret < 0)
798 			goto err_unlock;
799 	}
800 	mutex_unlock(&data->mutex);
801 
802 	return 0;
803 
804 err_unlock:
805 	mutex_unlock(&data->mutex);
806 	return ret;
807 }
808 
809 static const struct iio_trigger_ops bmc150_magn_trigger_ops = {
810 	.set_trigger_state = bmc150_magn_data_rdy_trigger_set_state,
811 	.try_reenable = bmc150_magn_trig_try_reen,
812 	.owner = THIS_MODULE,
813 };
814 
815 static int bmc150_magn_buffer_preenable(struct iio_dev *indio_dev)
816 {
817 	struct bmc150_magn_data *data = iio_priv(indio_dev);
818 
819 	return bmc150_magn_set_power_state(data, true);
820 }
821 
822 static int bmc150_magn_buffer_postdisable(struct iio_dev *indio_dev)
823 {
824 	struct bmc150_magn_data *data = iio_priv(indio_dev);
825 
826 	return bmc150_magn_set_power_state(data, false);
827 }
828 
829 static const struct iio_buffer_setup_ops bmc150_magn_buffer_setup_ops = {
830 	.preenable = bmc150_magn_buffer_preenable,
831 	.postenable = iio_triggered_buffer_postenable,
832 	.predisable = iio_triggered_buffer_predisable,
833 	.postdisable = bmc150_magn_buffer_postdisable,
834 };
835 
836 static int bmc150_magn_gpio_probe(struct i2c_client *client)
837 {
838 	struct device *dev;
839 	struct gpio_desc *gpio;
840 	int ret;
841 
842 	if (!client)
843 		return -EINVAL;
844 
845 	dev = &client->dev;
846 
847 	/* data ready GPIO interrupt pin */
848 	gpio = devm_gpiod_get_index(dev, BMC150_MAGN_GPIO_INT, 0, GPIOD_IN);
849 	if (IS_ERR(gpio)) {
850 		dev_err(dev, "ACPI GPIO get index failed\n");
851 		return PTR_ERR(gpio);
852 	}
853 
854 	ret = gpiod_to_irq(gpio);
855 
856 	dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
857 
858 	return ret;
859 }
860 
861 static const char *bmc150_magn_match_acpi_device(struct device *dev)
862 {
863 	const struct acpi_device_id *id;
864 
865 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
866 	if (!id)
867 		return NULL;
868 
869 	return dev_name(dev);
870 }
871 
872 static int bmc150_magn_probe(struct i2c_client *client,
873 			     const struct i2c_device_id *id)
874 {
875 	struct bmc150_magn_data *data;
876 	struct iio_dev *indio_dev;
877 	const char *name = NULL;
878 	int ret;
879 
880 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
881 	if (!indio_dev)
882 		return -ENOMEM;
883 
884 	data = iio_priv(indio_dev);
885 	i2c_set_clientdata(client, indio_dev);
886 	data->client = client;
887 
888 	if (id)
889 		name = id->name;
890 	else if (ACPI_HANDLE(&client->dev))
891 		name = bmc150_magn_match_acpi_device(&client->dev);
892 	else
893 		return -ENOSYS;
894 
895 	mutex_init(&data->mutex);
896 	data->regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config);
897 	if (IS_ERR(data->regmap)) {
898 		dev_err(&client->dev, "Failed to allocate register map\n");
899 		return PTR_ERR(data->regmap);
900 	}
901 
902 	ret = bmc150_magn_init(data);
903 	if (ret < 0)
904 		return ret;
905 
906 	indio_dev->dev.parent = &client->dev;
907 	indio_dev->channels = bmc150_magn_channels;
908 	indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels);
909 	indio_dev->available_scan_masks = bmc150_magn_scan_masks;
910 	indio_dev->name = name;
911 	indio_dev->modes = INDIO_DIRECT_MODE;
912 	indio_dev->info = &bmc150_magn_info;
913 
914 	if (client->irq <= 0)
915 		client->irq = bmc150_magn_gpio_probe(client);
916 
917 	if (client->irq > 0) {
918 		data->dready_trig = devm_iio_trigger_alloc(&client->dev,
919 							   "%s-dev%d",
920 							   indio_dev->name,
921 							   indio_dev->id);
922 		if (!data->dready_trig) {
923 			ret = -ENOMEM;
924 			dev_err(&client->dev, "iio trigger alloc failed\n");
925 			goto err_poweroff;
926 		}
927 
928 		data->dready_trig->dev.parent = &client->dev;
929 		data->dready_trig->ops = &bmc150_magn_trigger_ops;
930 		iio_trigger_set_drvdata(data->dready_trig, indio_dev);
931 		ret = iio_trigger_register(data->dready_trig);
932 		if (ret) {
933 			dev_err(&client->dev, "iio trigger register failed\n");
934 			goto err_poweroff;
935 		}
936 
937 		ret = request_threaded_irq(client->irq,
938 					   iio_trigger_generic_data_rdy_poll,
939 					   NULL,
940 					   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
941 					   BMC150_MAGN_IRQ_NAME,
942 					   data->dready_trig);
943 		if (ret < 0) {
944 			dev_err(&client->dev, "request irq %d failed\n",
945 				client->irq);
946 			goto err_trigger_unregister;
947 		}
948 	}
949 
950 	ret = iio_triggered_buffer_setup(indio_dev,
951 					 iio_pollfunc_store_time,
952 					 bmc150_magn_trigger_handler,
953 					 &bmc150_magn_buffer_setup_ops);
954 	if (ret < 0) {
955 		dev_err(&client->dev,
956 			"iio triggered buffer setup failed\n");
957 		goto err_free_irq;
958 	}
959 
960 	ret = iio_device_register(indio_dev);
961 	if (ret < 0) {
962 		dev_err(&client->dev, "unable to register iio device\n");
963 		goto err_buffer_cleanup;
964 	}
965 
966 	ret = pm_runtime_set_active(&client->dev);
967 	if (ret)
968 		goto err_iio_unregister;
969 
970 	pm_runtime_enable(&client->dev);
971 	pm_runtime_set_autosuspend_delay(&client->dev,
972 					 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS);
973 	pm_runtime_use_autosuspend(&client->dev);
974 
975 	dev_dbg(&indio_dev->dev, "Registered device %s\n", name);
976 
977 	return 0;
978 
979 err_iio_unregister:
980 	iio_device_unregister(indio_dev);
981 err_buffer_cleanup:
982 	iio_triggered_buffer_cleanup(indio_dev);
983 err_free_irq:
984 	if (client->irq > 0)
985 		free_irq(client->irq, data->dready_trig);
986 err_trigger_unregister:
987 	if (data->dready_trig)
988 		iio_trigger_unregister(data->dready_trig);
989 err_poweroff:
990 	bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
991 	return ret;
992 }
993 
994 static int bmc150_magn_remove(struct i2c_client *client)
995 {
996 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
997 	struct bmc150_magn_data *data = iio_priv(indio_dev);
998 
999 	pm_runtime_disable(&client->dev);
1000 	pm_runtime_set_suspended(&client->dev);
1001 	pm_runtime_put_noidle(&client->dev);
1002 
1003 	iio_device_unregister(indio_dev);
1004 	iio_triggered_buffer_cleanup(indio_dev);
1005 
1006 	if (client->irq > 0)
1007 		free_irq(data->client->irq, data->dready_trig);
1008 
1009 	if (data->dready_trig)
1010 		iio_trigger_unregister(data->dready_trig);
1011 
1012 	mutex_lock(&data->mutex);
1013 	bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
1014 	mutex_unlock(&data->mutex);
1015 
1016 	return 0;
1017 }
1018 
1019 #ifdef CONFIG_PM
1020 static int bmc150_magn_runtime_suspend(struct device *dev)
1021 {
1022 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1023 	struct bmc150_magn_data *data = iio_priv(indio_dev);
1024 	int ret;
1025 
1026 	mutex_lock(&data->mutex);
1027 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
1028 					 true);
1029 	mutex_unlock(&data->mutex);
1030 	if (ret < 0) {
1031 		dev_err(&data->client->dev, "powering off device failed\n");
1032 		return ret;
1033 	}
1034 	return 0;
1035 }
1036 
1037 /*
1038  * Should be called with data->mutex held.
1039  */
1040 static int bmc150_magn_runtime_resume(struct device *dev)
1041 {
1042 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1043 	struct bmc150_magn_data *data = iio_priv(indio_dev);
1044 
1045 	return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
1046 					  true);
1047 }
1048 #endif
1049 
1050 #ifdef CONFIG_PM_SLEEP
1051 static int bmc150_magn_suspend(struct device *dev)
1052 {
1053 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1054 	struct bmc150_magn_data *data = iio_priv(indio_dev);
1055 	int ret;
1056 
1057 	mutex_lock(&data->mutex);
1058 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
1059 					 true);
1060 	mutex_unlock(&data->mutex);
1061 
1062 	return ret;
1063 }
1064 
1065 static int bmc150_magn_resume(struct device *dev)
1066 {
1067 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1068 	struct bmc150_magn_data *data = iio_priv(indio_dev);
1069 	int ret;
1070 
1071 	mutex_lock(&data->mutex);
1072 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
1073 					 true);
1074 	mutex_unlock(&data->mutex);
1075 
1076 	return ret;
1077 }
1078 #endif
1079 
1080 static const struct dev_pm_ops bmc150_magn_pm_ops = {
1081 	SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume)
1082 	SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend,
1083 			   bmc150_magn_runtime_resume, NULL)
1084 };
1085 
1086 static const struct acpi_device_id bmc150_magn_acpi_match[] = {
1087 	{"BMC150B", 0},
1088 	{"BMC156B", 0},
1089 	{},
1090 };
1091 MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match);
1092 
1093 static const struct i2c_device_id bmc150_magn_id[] = {
1094 	{"bmc150_magn", 0},
1095 	{"bmc156_magn", 0},
1096 	{},
1097 };
1098 MODULE_DEVICE_TABLE(i2c, bmc150_magn_id);
1099 
1100 static struct i2c_driver bmc150_magn_driver = {
1101 	.driver = {
1102 		   .name = BMC150_MAGN_DRV_NAME,
1103 		   .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match),
1104 		   .pm = &bmc150_magn_pm_ops,
1105 		   },
1106 	.probe = bmc150_magn_probe,
1107 	.remove = bmc150_magn_remove,
1108 	.id_table = bmc150_magn_id,
1109 };
1110 module_i2c_driver(bmc150_magn_driver);
1111 
1112 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1113 MODULE_LICENSE("GPL v2");
1114 MODULE_DESCRIPTION("BMC150 magnetometer driver");
1115