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