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 
41 #define BMC150_MAGN_REG_CHIP_ID			0x40
42 #define BMC150_MAGN_CHIP_ID_VAL			0x32
43 
44 #define BMC150_MAGN_REG_X_L			0x42
45 #define BMC150_MAGN_REG_X_M			0x43
46 #define BMC150_MAGN_REG_Y_L			0x44
47 #define BMC150_MAGN_REG_Y_M			0x45
48 #define BMC150_MAGN_SHIFT_XY_L			3
49 #define BMC150_MAGN_REG_Z_L			0x46
50 #define BMC150_MAGN_REG_Z_M			0x47
51 #define BMC150_MAGN_SHIFT_Z_L			1
52 #define BMC150_MAGN_REG_RHALL_L			0x48
53 #define BMC150_MAGN_REG_RHALL_M			0x49
54 #define BMC150_MAGN_SHIFT_RHALL_L		2
55 
56 #define BMC150_MAGN_REG_INT_STATUS		0x4A
57 
58 #define BMC150_MAGN_REG_POWER			0x4B
59 #define BMC150_MAGN_MASK_POWER_CTL		BIT(0)
60 
61 #define BMC150_MAGN_REG_OPMODE_ODR		0x4C
62 #define BMC150_MAGN_MASK_OPMODE			GENMASK(2, 1)
63 #define BMC150_MAGN_SHIFT_OPMODE		1
64 #define BMC150_MAGN_MODE_NORMAL			0x00
65 #define BMC150_MAGN_MODE_FORCED			0x01
66 #define BMC150_MAGN_MODE_SLEEP			0x03
67 #define BMC150_MAGN_MASK_ODR			GENMASK(5, 3)
68 #define BMC150_MAGN_SHIFT_ODR			3
69 
70 #define BMC150_MAGN_REG_INT			0x4D
71 
72 #define BMC150_MAGN_REG_INT_DRDY		0x4E
73 #define BMC150_MAGN_MASK_DRDY_EN		BIT(7)
74 #define BMC150_MAGN_SHIFT_DRDY_EN		7
75 #define BMC150_MAGN_MASK_DRDY_INT3		BIT(6)
76 #define BMC150_MAGN_MASK_DRDY_Z_EN		BIT(5)
77 #define BMC150_MAGN_MASK_DRDY_Y_EN		BIT(4)
78 #define BMC150_MAGN_MASK_DRDY_X_EN		BIT(3)
79 #define BMC150_MAGN_MASK_DRDY_DR_POLARITY	BIT(2)
80 #define BMC150_MAGN_MASK_DRDY_LATCHING		BIT(1)
81 #define BMC150_MAGN_MASK_DRDY_INT3_POLARITY	BIT(0)
82 
83 #define BMC150_MAGN_REG_LOW_THRESH		0x4F
84 #define BMC150_MAGN_REG_HIGH_THRESH		0x50
85 #define BMC150_MAGN_REG_REP_XY			0x51
86 #define BMC150_MAGN_REG_REP_Z			0x52
87 #define BMC150_MAGN_REG_REP_DATAMASK		GENMASK(7, 0)
88 
89 #define BMC150_MAGN_REG_TRIM_START		0x5D
90 #define BMC150_MAGN_REG_TRIM_END		0x71
91 
92 #define BMC150_MAGN_XY_OVERFLOW_VAL		-4096
93 #define BMC150_MAGN_Z_OVERFLOW_VAL		-16384
94 
95 /* Time from SUSPEND to SLEEP */
96 #define BMC150_MAGN_START_UP_TIME_MS		3
97 
98 #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS	2000
99 
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)
104 
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,
112 };
113 
114 enum bmc150_magn_power_modes {
115 	BMC150_MAGN_POWER_MODE_SUSPEND,
116 	BMC150_MAGN_POWER_MODE_SLEEP,
117 	BMC150_MAGN_POWER_MODE_NORMAL,
118 };
119 
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;
136 
137 struct bmc150_magn_data {
138 	struct i2c_client *client;
139 	/*
140 	 * 1. Protect this structure.
141 	 * 2. Serialize sequences that power on/off the device and access HW.
142 	 */
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;
150 };
151 
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} };
163 
164 enum bmc150_magn_presets {
165 	LOW_POWER_PRESET,
166 	REGULAR_PRESET,
167 	ENHANCED_REGULAR_PRESET,
168 	HIGH_ACCURACY_PRESET
169 };
170 
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},
180 };
181 
182 #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET
183 
184 static bool bmc150_magn_is_writeable_reg(struct device *dev, unsigned int reg)
185 {
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;
198 	};
199 }
200 
201 static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg)
202 {
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;
216 	}
217 }
218 
219 static const struct regmap_config bmc150_magn_regmap_config = {
220 	.reg_bits = 8,
221 	.val_bits = 8,
222 
223 	.max_register = BMC150_MAGN_REG_TRIM_END,
224 	.cache_type = REGCACHE_RBTREE,
225 
226 	.writeable_reg = bmc150_magn_is_writeable_reg,
227 	.volatile_reg = bmc150_magn_is_volatile_reg,
228 };
229 
230 static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data,
231 				      enum bmc150_magn_power_modes mode,
232 				      bool state)
233 {
234 	int ret;
235 
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);
256 	}
257 
258 	return -EINVAL;
259 }
260 
261 static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
262 {
263 #ifdef CONFIG_PM
264 	int ret;
265 
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);
271 	}
272 
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);
278 
279 		return ret;
280 	}
281 #endif
282 
283 	return 0;
284 }
285 
286 static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val)
287 {
288 	int ret, reg_val;
289 	u8 i, odr_val;
290 
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;
295 
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;
300 		}
301 
302 	return -EINVAL;
303 }
304 
305 static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val)
306 {
307 	int ret;
308 	u8 i;
309 
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;
321 		}
322 	}
323 
324 	return -EINVAL;
325 }
326 
327 static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy,
328 				   int rep_z, int odr)
329 {
330 	int ret, reg_val, max_odr;
331 
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);
338 	}
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);
345 	}
346 	if (odr <= 0) {
347 		ret = bmc150_magn_get_odr(data, &odr);
348 		if (ret < 0)
349 			return ret;
350 	}
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;
358 	}
359 	data->max_odr = max_odr;
360 
361 	return 0;
362 }
363 
364 static s32 bmc150_magn_compensate_x(struct bmc150_magn_trim_regs *tregs, s16 x,
365 				    u16 rhall)
366 {
367 	s16 val;
368 	u16 xyz1 = le16_to_cpu(tregs->xyz1);
369 
370 	if (x == BMC150_MAGN_XY_OVERFLOW_VAL)
371 		return S32_MIN;
372 
373 	if (!rhall)
374 		rhall = xyz1;
375 
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);
382 
383 	return (s32)val;
384 }
385 
386 static s32 bmc150_magn_compensate_y(struct bmc150_magn_trim_regs *tregs, s16 y,
387 				    u16 rhall)
388 {
389 	s16 val;
390 	u16 xyz1 = le16_to_cpu(tregs->xyz1);
391 
392 	if (y == BMC150_MAGN_XY_OVERFLOW_VAL)
393 		return S32_MIN;
394 
395 	if (!rhall)
396 		rhall = xyz1;
397 
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);
404 
405 	return (s32)val;
406 }
407 
408 static s32 bmc150_magn_compensate_z(struct bmc150_magn_trim_regs *tregs, s16 z,
409 				    u16 rhall)
410 {
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);
417 
418 	if (z == BMC150_MAGN_Z_OVERFLOW_VAL)
419 		return S32_MIN;
420 
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))));
424 
425 	return val;
426 }
427 
428 static int bmc150_magn_read_xyz(struct bmc150_magn_data *data, s32 *buffer)
429 {
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;
435 
436 	ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_X_L,
437 			       values, sizeof(values));
438 	if (ret < 0)
439 		return ret;
440 
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;
445 
446 	ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_TRIM_START,
447 			       &tregs, sizeof(tregs));
448 	if (ret < 0)
449 		return ret;
450 
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);
454 
455 	return 0;
456 }
457 
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)
461 {
462 	struct bmc150_magn_data *data = iio_priv(indio_dev);
463 	int ret, tmp;
464 	s32 values[AXIS_XYZ_MAX];
465 
466 	switch (mask) {
467 	case IIO_CHAN_INFO_RAW:
468 		if (iio_buffer_enabled(indio_dev))
469 			return -EBUSY;
470 		mutex_lock(&data->mutex);
471 
472 		ret = bmc150_magn_set_power_state(data, true);
473 		if (ret < 0) {
474 			mutex_unlock(&data->mutex);
475 			return ret;
476 		}
477 
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;
483 		}
484 		*val = values[chan->scan_index];
485 
486 		ret = bmc150_magn_set_power_state(data, false);
487 		if (ret < 0) {
488 			mutex_unlock(&data->mutex);
489 			return ret;
490 		}
491 
492 		mutex_unlock(&data->mutex);
493 		return IIO_VAL_INT;
494 	case IIO_CHAN_INFO_SCALE:
495 		/*
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.
499 		 */
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;
527 		}
528 	default:
529 		return -EINVAL;
530 	}
531 }
532 
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)
536 {
537 	struct bmc150_magn_data *data = iio_priv(indio_dev);
538 	int ret;
539 
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;
559 			}
560 			ret = regmap_update_bits(data->regmap,
561 						 BMC150_MAGN_REG_REP_XY,
562 						 BMC150_MAGN_REG_REP_DATAMASK,
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;
575 			}
576 			ret = regmap_update_bits(data->regmap,
577 						 BMC150_MAGN_REG_REP_Z,
578 						 BMC150_MAGN_REG_REP_DATAMASK,
579 						 BMC150_MAGN_REPZ_TO_REGVAL
580 						 (val));
581 			mutex_unlock(&data->mutex);
582 			return ret;
583 		default:
584 			return -EINVAL;
585 		}
586 	default:
587 		return -EINVAL;
588 	}
589 }
590 
591 static ssize_t bmc150_magn_show_samp_freq_avail(struct device *dev,
592 						struct device_attribute *attr,
593 						char *buf)
594 {
595 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
596 	struct bmc150_magn_data *data = iio_priv(indio_dev);
597 	size_t len = 0;
598 	u8 i;
599 
600 	for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
601 		if (bmc150_magn_samp_freq_table[i].freq > data->max_odr)
602 			break;
603 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
604 				 bmc150_magn_samp_freq_table[i].freq);
605 	}
606 	/* replace last space with a newline */
607 	buf[len - 1] = '\n';
608 
609 	return len;
610 }
611 
612 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail);
613 
614 static struct attribute *bmc150_magn_attributes[] = {
615 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
616 	NULL,
617 };
618 
619 static const struct attribute_group bmc150_magn_attrs_group = {
620 	.attrs = bmc150_magn_attributes,
621 };
622 
623 #define BMC150_MAGN_CHANNEL(_axis) {					\
624 	.type = IIO_MAGN,						\
625 	.modified = 1,							\
626 	.channel2 = IIO_MOD_##_axis,					\
627 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |			\
628 			      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),	\
629 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
630 				    BIT(IIO_CHAN_INFO_SCALE),		\
631 	.scan_index = AXIS_##_axis,					\
632 	.scan_type = {							\
633 		.sign = 's',						\
634 		.realbits = 32,						\
635 		.storagebits = 32,					\
636 		.endianness = IIO_LE					\
637 	},								\
638 }
639 
640 static const struct iio_chan_spec bmc150_magn_channels[] = {
641 	BMC150_MAGN_CHANNEL(X),
642 	BMC150_MAGN_CHANNEL(Y),
643 	BMC150_MAGN_CHANNEL(Z),
644 	IIO_CHAN_SOFT_TIMESTAMP(3),
645 };
646 
647 static const struct iio_info bmc150_magn_info = {
648 	.attrs = &bmc150_magn_attrs_group,
649 	.read_raw = bmc150_magn_read_raw,
650 	.write_raw = bmc150_magn_write_raw,
651 	.driver_module = THIS_MODULE,
652 };
653 
654 static const unsigned long bmc150_magn_scan_masks[] = {
655 					BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
656 					0};
657 
658 static irqreturn_t bmc150_magn_trigger_handler(int irq, void *p)
659 {
660 	struct iio_poll_func *pf = p;
661 	struct iio_dev *indio_dev = pf->indio_dev;
662 	struct bmc150_magn_data *data = iio_priv(indio_dev);
663 	int ret;
664 
665 	mutex_lock(&data->mutex);
666 	ret = bmc150_magn_read_xyz(data, data->buffer);
667 	if (ret < 0)
668 		goto err;
669 
670 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
671 					   pf->timestamp);
672 
673 err:
674 	mutex_unlock(&data->mutex);
675 	iio_trigger_notify_done(indio_dev->trig);
676 
677 	return IRQ_HANDLED;
678 }
679 
680 static int bmc150_magn_init(struct bmc150_magn_data *data)
681 {
682 	int ret, chip_id;
683 	struct bmc150_magn_preset preset;
684 
685 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND,
686 					 false);
687 	if (ret < 0) {
688 		dev_err(&data->client->dev,
689 			"Failed to bring up device from suspend mode\n");
690 		return ret;
691 	}
692 
693 	ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id);
694 	if (ret < 0) {
695 		dev_err(&data->client->dev, "Failed reading chip id\n");
696 		goto err_poweroff;
697 	}
698 	if (chip_id != BMC150_MAGN_CHIP_ID_VAL) {
699 		dev_err(&data->client->dev, "Invalid chip id 0x%x\n", chip_id);
700 		ret = -ENODEV;
701 		goto err_poweroff;
702 	}
703 	dev_dbg(&data->client->dev, "Chip id %x\n", chip_id);
704 
705 	preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET];
706 	ret = bmc150_magn_set_odr(data, preset.odr);
707 	if (ret < 0) {
708 		dev_err(&data->client->dev, "Failed to set ODR to %d\n",
709 			preset.odr);
710 		goto err_poweroff;
711 	}
712 
713 	ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY,
714 			   BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy));
715 	if (ret < 0) {
716 		dev_err(&data->client->dev, "Failed to set REP XY to %d\n",
717 			preset.rep_xy);
718 		goto err_poweroff;
719 	}
720 
721 	ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z,
722 			   BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z));
723 	if (ret < 0) {
724 		dev_err(&data->client->dev, "Failed to set REP Z to %d\n",
725 			preset.rep_z);
726 		goto err_poweroff;
727 	}
728 
729 	ret = bmc150_magn_set_max_odr(data, preset.rep_xy, preset.rep_z,
730 				      preset.odr);
731 	if (ret < 0)
732 		goto err_poweroff;
733 
734 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
735 					 true);
736 	if (ret < 0) {
737 		dev_err(&data->client->dev, "Failed to power on device\n");
738 		goto err_poweroff;
739 	}
740 
741 	return 0;
742 
743 err_poweroff:
744 	bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
745 	return ret;
746 }
747 
748 static int bmc150_magn_reset_intr(struct bmc150_magn_data *data)
749 {
750 	int tmp;
751 
752 	/*
753 	 * Data Ready (DRDY) is always cleared after
754 	 * readout of data registers ends.
755 	 */
756 	return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp);
757 }
758 
759 static int bmc150_magn_trig_try_reen(struct iio_trigger *trig)
760 {
761 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
762 	struct bmc150_magn_data *data = iio_priv(indio_dev);
763 	int ret;
764 
765 	if (!data->dready_trigger_on)
766 		return 0;
767 
768 	mutex_lock(&data->mutex);
769 	ret = bmc150_magn_reset_intr(data);
770 	mutex_unlock(&data->mutex);
771 
772 	return ret;
773 }
774 
775 static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
776 						  bool state)
777 {
778 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
779 	struct bmc150_magn_data *data = iio_priv(indio_dev);
780 	int ret = 0;
781 
782 	mutex_lock(&data->mutex);
783 	if (state == data->dready_trigger_on)
784 		goto err_unlock;
785 
786 	ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
787 				 BMC150_MAGN_MASK_DRDY_EN,
788 				 state << BMC150_MAGN_SHIFT_DRDY_EN);
789 	if (ret < 0)
790 		goto err_unlock;
791 
792 	data->dready_trigger_on = state;
793 
794 	if (state) {
795 		ret = bmc150_magn_reset_intr(data);
796 		if (ret < 0)
797 			goto err_unlock;
798 	}
799 	mutex_unlock(&data->mutex);
800 
801 	return 0;
802 
803 err_unlock:
804 	mutex_unlock(&data->mutex);
805 	return ret;
806 }
807 
808 static const struct iio_trigger_ops bmc150_magn_trigger_ops = {
809 	.set_trigger_state = bmc150_magn_data_rdy_trigger_set_state,
810 	.try_reenable = bmc150_magn_trig_try_reen,
811 	.owner = THIS_MODULE,
812 };
813 
814 static int bmc150_magn_buffer_preenable(struct iio_dev *indio_dev)
815 {
816 	struct bmc150_magn_data *data = iio_priv(indio_dev);
817 
818 	return bmc150_magn_set_power_state(data, true);
819 }
820 
821 static int bmc150_magn_buffer_postdisable(struct iio_dev *indio_dev)
822 {
823 	struct bmc150_magn_data *data = iio_priv(indio_dev);
824 
825 	return bmc150_magn_set_power_state(data, false);
826 }
827 
828 static const struct iio_buffer_setup_ops bmc150_magn_buffer_setup_ops = {
829 	.preenable = bmc150_magn_buffer_preenable,
830 	.postenable = iio_triggered_buffer_postenable,
831 	.predisable = iio_triggered_buffer_predisable,
832 	.postdisable = bmc150_magn_buffer_postdisable,
833 };
834 
835 static const char *bmc150_magn_match_acpi_device(struct device *dev)
836 {
837 	const struct acpi_device_id *id;
838 
839 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
840 	if (!id)
841 		return NULL;
842 
843 	return dev_name(dev);
844 }
845 
846 static int bmc150_magn_probe(struct i2c_client *client,
847 			     const struct i2c_device_id *id)
848 {
849 	struct bmc150_magn_data *data;
850 	struct iio_dev *indio_dev;
851 	const char *name = NULL;
852 	int ret;
853 
854 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
855 	if (!indio_dev)
856 		return -ENOMEM;
857 
858 	data = iio_priv(indio_dev);
859 	i2c_set_clientdata(client, indio_dev);
860 	data->client = client;
861 
862 	if (id)
863 		name = id->name;
864 	else if (ACPI_HANDLE(&client->dev))
865 		name = bmc150_magn_match_acpi_device(&client->dev);
866 	else
867 		return -ENOSYS;
868 
869 	mutex_init(&data->mutex);
870 	data->regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config);
871 	if (IS_ERR(data->regmap)) {
872 		dev_err(&client->dev, "Failed to allocate register map\n");
873 		return PTR_ERR(data->regmap);
874 	}
875 
876 	ret = bmc150_magn_init(data);
877 	if (ret < 0)
878 		return ret;
879 
880 	indio_dev->dev.parent = &client->dev;
881 	indio_dev->channels = bmc150_magn_channels;
882 	indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels);
883 	indio_dev->available_scan_masks = bmc150_magn_scan_masks;
884 	indio_dev->name = name;
885 	indio_dev->modes = INDIO_DIRECT_MODE;
886 	indio_dev->info = &bmc150_magn_info;
887 
888 	if (client->irq > 0) {
889 		data->dready_trig = devm_iio_trigger_alloc(&client->dev,
890 							   "%s-dev%d",
891 							   indio_dev->name,
892 							   indio_dev->id);
893 		if (!data->dready_trig) {
894 			ret = -ENOMEM;
895 			dev_err(&client->dev, "iio trigger alloc failed\n");
896 			goto err_poweroff;
897 		}
898 
899 		data->dready_trig->dev.parent = &client->dev;
900 		data->dready_trig->ops = &bmc150_magn_trigger_ops;
901 		iio_trigger_set_drvdata(data->dready_trig, indio_dev);
902 		ret = iio_trigger_register(data->dready_trig);
903 		if (ret) {
904 			dev_err(&client->dev, "iio trigger register failed\n");
905 			goto err_poweroff;
906 		}
907 
908 		ret = request_threaded_irq(client->irq,
909 					   iio_trigger_generic_data_rdy_poll,
910 					   NULL,
911 					   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
912 					   BMC150_MAGN_IRQ_NAME,
913 					   data->dready_trig);
914 		if (ret < 0) {
915 			dev_err(&client->dev, "request irq %d failed\n",
916 				client->irq);
917 			goto err_trigger_unregister;
918 		}
919 	}
920 
921 	ret = iio_triggered_buffer_setup(indio_dev,
922 					 iio_pollfunc_store_time,
923 					 bmc150_magn_trigger_handler,
924 					 &bmc150_magn_buffer_setup_ops);
925 	if (ret < 0) {
926 		dev_err(&client->dev,
927 			"iio triggered buffer setup failed\n");
928 		goto err_free_irq;
929 	}
930 
931 	ret = iio_device_register(indio_dev);
932 	if (ret < 0) {
933 		dev_err(&client->dev, "unable to register iio device\n");
934 		goto err_buffer_cleanup;
935 	}
936 
937 	ret = pm_runtime_set_active(&client->dev);
938 	if (ret)
939 		goto err_iio_unregister;
940 
941 	pm_runtime_enable(&client->dev);
942 	pm_runtime_set_autosuspend_delay(&client->dev,
943 					 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS);
944 	pm_runtime_use_autosuspend(&client->dev);
945 
946 	dev_dbg(&indio_dev->dev, "Registered device %s\n", name);
947 
948 	return 0;
949 
950 err_iio_unregister:
951 	iio_device_unregister(indio_dev);
952 err_buffer_cleanup:
953 	iio_triggered_buffer_cleanup(indio_dev);
954 err_free_irq:
955 	if (client->irq > 0)
956 		free_irq(client->irq, data->dready_trig);
957 err_trigger_unregister:
958 	if (data->dready_trig)
959 		iio_trigger_unregister(data->dready_trig);
960 err_poweroff:
961 	bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
962 	return ret;
963 }
964 
965 static int bmc150_magn_remove(struct i2c_client *client)
966 {
967 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
968 	struct bmc150_magn_data *data = iio_priv(indio_dev);
969 
970 	pm_runtime_disable(&client->dev);
971 	pm_runtime_set_suspended(&client->dev);
972 	pm_runtime_put_noidle(&client->dev);
973 
974 	iio_device_unregister(indio_dev);
975 	iio_triggered_buffer_cleanup(indio_dev);
976 
977 	if (client->irq > 0)
978 		free_irq(data->client->irq, data->dready_trig);
979 
980 	if (data->dready_trig)
981 		iio_trigger_unregister(data->dready_trig);
982 
983 	mutex_lock(&data->mutex);
984 	bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
985 	mutex_unlock(&data->mutex);
986 
987 	return 0;
988 }
989 
990 #ifdef CONFIG_PM
991 static int bmc150_magn_runtime_suspend(struct device *dev)
992 {
993 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
994 	struct bmc150_magn_data *data = iio_priv(indio_dev);
995 	int ret;
996 
997 	mutex_lock(&data->mutex);
998 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
999 					 true);
1000 	mutex_unlock(&data->mutex);
1001 	if (ret < 0) {
1002 		dev_err(&data->client->dev, "powering off device failed\n");
1003 		return ret;
1004 	}
1005 	return 0;
1006 }
1007 
1008 /*
1009  * Should be called with data->mutex held.
1010  */
1011 static int bmc150_magn_runtime_resume(struct device *dev)
1012 {
1013 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1014 	struct bmc150_magn_data *data = iio_priv(indio_dev);
1015 
1016 	return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
1017 					  true);
1018 }
1019 #endif
1020 
1021 #ifdef CONFIG_PM_SLEEP
1022 static int bmc150_magn_suspend(struct device *dev)
1023 {
1024 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1025 	struct bmc150_magn_data *data = iio_priv(indio_dev);
1026 	int ret;
1027 
1028 	mutex_lock(&data->mutex);
1029 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
1030 					 true);
1031 	mutex_unlock(&data->mutex);
1032 
1033 	return ret;
1034 }
1035 
1036 static int bmc150_magn_resume(struct device *dev)
1037 {
1038 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1039 	struct bmc150_magn_data *data = iio_priv(indio_dev);
1040 	int ret;
1041 
1042 	mutex_lock(&data->mutex);
1043 	ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
1044 					 true);
1045 	mutex_unlock(&data->mutex);
1046 
1047 	return ret;
1048 }
1049 #endif
1050 
1051 static const struct dev_pm_ops bmc150_magn_pm_ops = {
1052 	SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume)
1053 	SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend,
1054 			   bmc150_magn_runtime_resume, NULL)
1055 };
1056 
1057 static const struct acpi_device_id bmc150_magn_acpi_match[] = {
1058 	{"BMC150B", 0},
1059 	{"BMC156B", 0},
1060 	{},
1061 };
1062 MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match);
1063 
1064 static const struct i2c_device_id bmc150_magn_id[] = {
1065 	{"bmc150_magn", 0},
1066 	{"bmc156_magn", 0},
1067 	{},
1068 };
1069 MODULE_DEVICE_TABLE(i2c, bmc150_magn_id);
1070 
1071 static struct i2c_driver bmc150_magn_driver = {
1072 	.driver = {
1073 		   .name = BMC150_MAGN_DRV_NAME,
1074 		   .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match),
1075 		   .pm = &bmc150_magn_pm_ops,
1076 		   },
1077 	.probe = bmc150_magn_probe,
1078 	.remove = bmc150_magn_remove,
1079 	.id_table = bmc150_magn_id,
1080 };
1081 module_i2c_driver(bmc150_magn_driver);
1082 
1083 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1084 MODULE_LICENSE("GPL v2");
1085 MODULE_DESCRIPTION("BMC150 magnetometer driver");
1086