xref: /linux/drivers/iio/magnetometer/tmag5273.c (revision 2e21dee6a46a66e4c2ced778485e1044101edee4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor
4  *
5  * Copyright (C) 2022 WolfVision GmbH
6  *
7  * Author: Gerald Loacker <gerald.loacker@wolfvision.net>
8  */
9 
10 #include <linux/bitfield.h>
11 #include <linux/bits.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/regmap.h>
16 #include <linux/pm_runtime.h>
17 
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 
21 #define TMAG5273_DEVICE_CONFIG_1	 0x00
22 #define TMAG5273_DEVICE_CONFIG_2	 0x01
23 #define TMAG5273_SENSOR_CONFIG_1	 0x02
24 #define TMAG5273_SENSOR_CONFIG_2	 0x03
25 #define TMAG5273_X_THR_CONFIG		 0x04
26 #define TMAG5273_Y_THR_CONFIG		 0x05
27 #define TMAG5273_Z_THR_CONFIG		 0x06
28 #define TMAG5273_T_CONFIG		 0x07
29 #define TMAG5273_INT_CONFIG_1		 0x08
30 #define TMAG5273_MAG_GAIN_CONFIG	 0x09
31 #define TMAG5273_MAG_OFFSET_CONFIG_1	 0x0A
32 #define TMAG5273_MAG_OFFSET_CONFIG_2	 0x0B
33 #define TMAG5273_I2C_ADDRESS		 0x0C
34 #define TMAG5273_DEVICE_ID		 0x0D
35 #define TMAG5273_MANUFACTURER_ID_LSB	 0x0E
36 #define TMAG5273_MANUFACTURER_ID_MSB	 0x0F
37 #define TMAG5273_T_MSB_RESULT		 0x10
38 #define TMAG5273_T_LSB_RESULT		 0x11
39 #define TMAG5273_X_MSB_RESULT		 0x12
40 #define TMAG5273_X_LSB_RESULT		 0x13
41 #define TMAG5273_Y_MSB_RESULT		 0x14
42 #define TMAG5273_Y_LSB_RESULT		 0x15
43 #define TMAG5273_Z_MSB_RESULT		 0x16
44 #define TMAG5273_Z_LSB_RESULT		 0x17
45 #define TMAG5273_CONV_STATUS		 0x18
46 #define TMAG5273_ANGLE_RESULT_MSB	 0x19
47 #define TMAG5273_ANGLE_RESULT_LSB	 0x1A
48 #define TMAG5273_MAGNITUDE_RESULT	 0x1B
49 #define TMAG5273_DEVICE_STATUS		 0x1C
50 #define TMAG5273_MAX_REG		 TMAG5273_DEVICE_STATUS
51 
52 #define TMAG5273_AUTOSLEEP_DELAY_MS	 5000
53 #define TMAG5273_MAX_AVERAGE             32
54 
55 /*
56  * bits in the TMAG5273_MANUFACTURER_ID_LSB / MSB register
57  * 16-bit unique manufacturer ID 0x49 / 0x54 = "TI"
58  */
59 #define TMAG5273_MANUFACTURER_ID	 0x5449
60 
61 /* bits in the TMAG5273_DEVICE_CONFIG_1 register */
62 #define TMAG5273_AVG_MODE_MASK		 GENMASK(4, 2)
63 #define TMAG5273_AVG_1_MODE		 FIELD_PREP(TMAG5273_AVG_MODE_MASK, 0)
64 #define TMAG5273_AVG_2_MODE		 FIELD_PREP(TMAG5273_AVG_MODE_MASK, 1)
65 #define TMAG5273_AVG_4_MODE		 FIELD_PREP(TMAG5273_AVG_MODE_MASK, 2)
66 #define TMAG5273_AVG_8_MODE		 FIELD_PREP(TMAG5273_AVG_MODE_MASK, 3)
67 #define TMAG5273_AVG_16_MODE		 FIELD_PREP(TMAG5273_AVG_MODE_MASK, 4)
68 #define TMAG5273_AVG_32_MODE		 FIELD_PREP(TMAG5273_AVG_MODE_MASK, 5)
69 
70 /* bits in the TMAG5273_DEVICE_CONFIG_2 register */
71 #define TMAG5273_OP_MODE_MASK		 GENMASK(1, 0)
72 #define TMAG5273_OP_MODE_STANDBY	 FIELD_PREP(TMAG5273_OP_MODE_MASK, 0)
73 #define TMAG5273_OP_MODE_SLEEP		 FIELD_PREP(TMAG5273_OP_MODE_MASK, 1)
74 #define TMAG5273_OP_MODE_CONT		 FIELD_PREP(TMAG5273_OP_MODE_MASK, 2)
75 #define TMAG5273_OP_MODE_WAKEUP		 FIELD_PREP(TMAG5273_OP_MODE_MASK, 3)
76 
77 /* bits in the TMAG5273_SENSOR_CONFIG_1 register */
78 #define TMAG5273_MAG_CH_EN_MASK		 GENMASK(7, 4)
79 #define TMAG5273_MAG_CH_EN_X_Y_Z	 7
80 
81 /* bits in the TMAG5273_SENSOR_CONFIG_2 register */
82 #define TMAG5273_Z_RANGE_MASK		 BIT(0)
83 #define TMAG5273_X_Y_RANGE_MASK		 BIT(1)
84 #define TMAG5273_ANGLE_EN_MASK		 GENMASK(3, 2)
85 #define TMAG5273_ANGLE_EN_OFF		 0
86 #define TMAG5273_ANGLE_EN_X_Y		 1
87 #define TMAG5273_ANGLE_EN_Y_Z		 2
88 #define TMAG5273_ANGLE_EN_X_Z		 3
89 
90 /* bits in the TMAG5273_T_CONFIG register */
91 #define TMAG5273_T_CH_EN		 BIT(0)
92 
93 /* bits in the TMAG5273_DEVICE_ID register */
94 #define TMAG5273_VERSION_MASK		 GENMASK(1, 0)
95 
96 /* bits in the TMAG5273_CONV_STATUS register */
97 #define TMAG5273_CONV_STATUS_COMPLETE	 BIT(0)
98 
99 enum tmag5273_channels {
100 	TEMPERATURE = 0,
101 	AXIS_X,
102 	AXIS_Y,
103 	AXIS_Z,
104 	ANGLE,
105 	MAGNITUDE,
106 };
107 
108 enum tmag5273_scale_index {
109 	MAGN_RANGE_LOW = 0,
110 	MAGN_RANGE_HIGH,
111 	MAGN_RANGE_NUM
112 };
113 
114 /* state container for the TMAG5273 driver */
115 struct tmag5273_data {
116 	struct device *dev;
117 	unsigned int devid;
118 	unsigned int version;
119 	char name[16];
120 	unsigned int conv_avg;
121 	unsigned int scale;
122 	enum tmag5273_scale_index scale_index;
123 	unsigned int angle_measurement;
124 	struct regmap *map;
125 	struct regulator *vcc;
126 
127 	/*
128 	 * Locks the sensor for exclusive use during a measurement (which
129 	 * involves several register transactions so the regmap lock is not
130 	 * enough) so that measurements get serialized in a
131 	 * first-come-first-serve manner.
132 	 */
133 	struct mutex lock;
134 };
135 
136 static const char *const tmag5273_angle_names[] = { "off", "x-y", "y-z", "x-z" };
137 
138 /*
139  * Averaging enables additional sampling of the sensor data to reduce the noise
140  * effect, but also increases conversion time.
141  */
142 static const unsigned int tmag5273_avg_table[] = {
143 	1, 2, 4, 8, 16, 32,
144 };
145 
146 /*
147  * Magnetic resolution in Gauss for different TMAG5273 versions.
148  * Scale[Gauss] = Range[mT] * 1000 / 2^15 * 10, (1 mT = 10 Gauss)
149  * Only version 1 and 2 are valid, version 0 and 3 are reserved.
150  */
151 static const struct iio_val_int_plus_micro tmag5273_scale[][MAGN_RANGE_NUM] = {
152 	{ { 0,     0 }, { 0,     0 } },
153 	{ { 0, 12200 }, { 0, 24400 } },
154 	{ { 0, 40600 }, { 0, 81200 } },
155 	{ { 0,     0 }, { 0,     0 } },
156 };
157 
158 static int tmag5273_get_measure(struct tmag5273_data *data, s16 *t, s16 *x,
159 				s16 *y, s16 *z, u16 *angle, u16 *magnitude)
160 {
161 	unsigned int status, val;
162 	__be16 reg_data[4];
163 	int ret;
164 
165 	mutex_lock(&data->lock);
166 
167 	/*
168 	 * Max. conversion time is 2425 us in 32x averaging mode for all three
169 	 * channels. Since we are in continuous measurement mode, a measurement
170 	 * may already be there, so poll for completed measurement with
171 	 * timeout.
172 	 */
173 	ret = regmap_read_poll_timeout(data->map, TMAG5273_CONV_STATUS, status,
174 				       status & TMAG5273_CONV_STATUS_COMPLETE,
175 				       100, 10000);
176 	if (ret) {
177 		dev_err(data->dev, "timeout waiting for measurement\n");
178 		goto out_unlock;
179 	}
180 
181 	ret = regmap_bulk_read(data->map, TMAG5273_T_MSB_RESULT, reg_data,
182 			       sizeof(reg_data));
183 	if (ret)
184 		goto out_unlock;
185 	*t = be16_to_cpu(reg_data[0]);
186 	*x = be16_to_cpu(reg_data[1]);
187 	*y = be16_to_cpu(reg_data[2]);
188 	*z = be16_to_cpu(reg_data[3]);
189 
190 	ret = regmap_bulk_read(data->map, TMAG5273_ANGLE_RESULT_MSB,
191 			       &reg_data[0], sizeof(reg_data[0]));
192 	if (ret)
193 		goto out_unlock;
194 	/*
195 	 * angle has 9 bits integer value and 4 bits fractional part
196 	 * 15 14 13 12 11 10 9  8  7  6  5  4  3  2  1  0
197 	 * 0  0  0  a  a  a  a  a  a  a  a  a  f  f  f  f
198 	 */
199 	*angle = be16_to_cpu(reg_data[0]);
200 
201 	ret = regmap_read(data->map, TMAG5273_MAGNITUDE_RESULT, &val);
202 	if (ret < 0)
203 		goto out_unlock;
204 	*magnitude = val;
205 
206 out_unlock:
207 	mutex_unlock(&data->lock);
208 	return ret;
209 }
210 
211 static int tmag5273_write_osr(struct tmag5273_data *data, int val)
212 {
213 	int i;
214 
215 	if (val == data->conv_avg)
216 		return 0;
217 
218 	for (i = 0; i < ARRAY_SIZE(tmag5273_avg_table); i++) {
219 		if (tmag5273_avg_table[i] == val)
220 			break;
221 	}
222 	if (i == ARRAY_SIZE(tmag5273_avg_table))
223 		return -EINVAL;
224 	data->conv_avg = val;
225 
226 	return regmap_update_bits(data->map, TMAG5273_DEVICE_CONFIG_1,
227 				  TMAG5273_AVG_MODE_MASK,
228 				  FIELD_PREP(TMAG5273_AVG_MODE_MASK, i));
229 }
230 
231 static int tmag5273_write_scale(struct tmag5273_data *data, int scale_micro)
232 {
233 	u32 value;
234 	int i;
235 
236 	for (i = 0; i < ARRAY_SIZE(tmag5273_scale[0]); i++) {
237 		if (tmag5273_scale[data->version][i].micro == scale_micro)
238 			break;
239 	}
240 	if (i == ARRAY_SIZE(tmag5273_scale[0]))
241 		return -EINVAL;
242 	data->scale_index = i;
243 
244 	if (data->scale_index == MAGN_RANGE_LOW)
245 		value = 0;
246 	else
247 		value = TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK;
248 
249 	return regmap_update_bits(data->map, TMAG5273_SENSOR_CONFIG_2,
250 				  TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK, value);
251 }
252 
253 static int tmag5273_read_avail(struct iio_dev *indio_dev,
254 			       struct iio_chan_spec const *chan,
255 			       const int **vals, int *type, int *length,
256 			       long mask)
257 {
258 	struct tmag5273_data *data = iio_priv(indio_dev);
259 
260 	switch (mask) {
261 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
262 		*vals = tmag5273_avg_table;
263 		*type = IIO_VAL_INT;
264 		*length = ARRAY_SIZE(tmag5273_avg_table);
265 		return IIO_AVAIL_LIST;
266 	case IIO_CHAN_INFO_SCALE:
267 		switch (chan->type) {
268 		case IIO_MAGN:
269 			*type = IIO_VAL_INT_PLUS_MICRO;
270 			*vals = (int *)tmag5273_scale[data->version];
271 			*length = ARRAY_SIZE(tmag5273_scale[data->version]) *
272 				  MAGN_RANGE_NUM;
273 			return IIO_AVAIL_LIST;
274 		default:
275 			return -EINVAL;
276 		}
277 	default:
278 		return -EINVAL;
279 	}
280 }
281 
282 static int tmag5273_read_raw(struct iio_dev *indio_dev,
283 			     const struct iio_chan_spec *chan, int *val,
284 			     int *val2, long mask)
285 {
286 	struct tmag5273_data *data = iio_priv(indio_dev);
287 	s16 t, x, y, z;
288 	u16 angle, magnitude;
289 	int ret;
290 
291 	switch (mask) {
292 	case IIO_CHAN_INFO_PROCESSED:
293 	case IIO_CHAN_INFO_RAW:
294 		ret = pm_runtime_resume_and_get(data->dev);
295 		if (ret < 0)
296 			return ret;
297 
298 		ret = tmag5273_get_measure(data, &t, &x, &y, &z, &angle, &magnitude);
299 
300 		pm_runtime_mark_last_busy(data->dev);
301 		pm_runtime_put_autosuspend(data->dev);
302 
303 		if (ret)
304 			return ret;
305 
306 		switch (chan->address) {
307 		case TEMPERATURE:
308 			*val = t;
309 			return IIO_VAL_INT;
310 		case AXIS_X:
311 			*val = x;
312 			return IIO_VAL_INT;
313 		case AXIS_Y:
314 			*val = y;
315 			return IIO_VAL_INT;
316 		case AXIS_Z:
317 			*val = z;
318 			return IIO_VAL_INT;
319 		case ANGLE:
320 			*val = angle;
321 			return IIO_VAL_INT;
322 		case MAGNITUDE:
323 			*val = magnitude;
324 			return IIO_VAL_INT;
325 		default:
326 			return -EINVAL;
327 		}
328 	case IIO_CHAN_INFO_SCALE:
329 		switch (chan->type) {
330 		case IIO_TEMP:
331 			/*
332 			 * Convert device specific value to millicelsius.
333 			 * Resolution from the sensor is 60.1 LSB/celsius and
334 			 * the reference value at 25 celsius is 17508 LSBs.
335 			 */
336 			*val = 10000;
337 			*val2 = 601;
338 			return IIO_VAL_FRACTIONAL;
339 		case IIO_MAGN:
340 			/* Magnetic resolution in uT */
341 			*val = 0;
342 			*val2 = tmag5273_scale[data->version]
343 					      [data->scale_index].micro;
344 			return IIO_VAL_INT_PLUS_MICRO;
345 		case IIO_ANGL:
346 			/*
347 			 * Angle is in degrees and has four fractional bits,
348 			 * therefore use 1/16 * pi/180 to convert to radians.
349 			 */
350 			*val = 1000;
351 			*val2 = 916732;
352 			return IIO_VAL_FRACTIONAL;
353 		default:
354 			return -EINVAL;
355 		}
356 	case IIO_CHAN_INFO_OFFSET:
357 		switch (chan->type) {
358 		case IIO_TEMP:
359 			*val = -16005;
360 			return IIO_VAL_INT;
361 		default:
362 			return -EINVAL;
363 		}
364 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
365 		*val = data->conv_avg;
366 		return IIO_VAL_INT;
367 
368 	default:
369 		return -EINVAL;
370 	}
371 }
372 
373 static int tmag5273_write_raw(struct iio_dev *indio_dev,
374 			      struct iio_chan_spec const *chan, int val,
375 			      int val2, long mask)
376 {
377 	struct tmag5273_data *data = iio_priv(indio_dev);
378 
379 	switch (mask) {
380 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
381 		return tmag5273_write_osr(data, val);
382 	case IIO_CHAN_INFO_SCALE:
383 		switch (chan->type) {
384 		case IIO_MAGN:
385 			if (val)
386 				return -EINVAL;
387 			return tmag5273_write_scale(data, val2);
388 		default:
389 			return -EINVAL;
390 		}
391 	default:
392 		return -EINVAL;
393 	}
394 }
395 
396 #define TMAG5273_AXIS_CHANNEL(axis, index)				     \
397 	{								     \
398 		.type = IIO_MAGN,					     \
399 		.modified = 1,						     \
400 		.channel2 = IIO_MOD_##axis,				     \
401 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		     \
402 				      BIT(IIO_CHAN_INFO_SCALE),		     \
403 		.info_mask_shared_by_type_available =			     \
404 				      BIT(IIO_CHAN_INFO_SCALE),		     \
405 		.info_mask_shared_by_all =				     \
406 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
407 		.info_mask_shared_by_all_available =			     \
408 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
409 		.address = index,					     \
410 		.scan_index = index,					     \
411 		.scan_type = {						     \
412 			.sign = 's',					     \
413 			.realbits = 16,					     \
414 			.storagebits = 16,				     \
415 			.endianness = IIO_CPU,				     \
416 		},							     \
417 	}
418 
419 static const struct iio_chan_spec tmag5273_channels[] = {
420 	{
421 		.type = IIO_TEMP,
422 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
423 			BIT(IIO_CHAN_INFO_SCALE) |
424 			BIT(IIO_CHAN_INFO_OFFSET),
425 		.address = TEMPERATURE,
426 		.scan_index = TEMPERATURE,
427 		.scan_type = {
428 			.sign = 'u',
429 			.realbits = 16,
430 			.storagebits = 16,
431 			.endianness = IIO_CPU,
432 		},
433 	},
434 	TMAG5273_AXIS_CHANNEL(X, AXIS_X),
435 	TMAG5273_AXIS_CHANNEL(Y, AXIS_Y),
436 	TMAG5273_AXIS_CHANNEL(Z, AXIS_Z),
437 	{
438 		.type = IIO_ANGL,
439 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
440 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
441 		.info_mask_shared_by_all =
442 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
443 		.info_mask_shared_by_all_available =
444 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
445 		.address = ANGLE,
446 		.scan_index = ANGLE,
447 		.scan_type = {
448 			.sign = 'u',
449 			.realbits = 16,
450 			.storagebits = 16,
451 			.endianness = IIO_CPU,
452 		},
453 	},
454 	{
455 		.type = IIO_DISTANCE,
456 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
457 		.info_mask_shared_by_all =
458 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
459 		.info_mask_shared_by_all_available =
460 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
461 		.address = MAGNITUDE,
462 		.scan_index = MAGNITUDE,
463 		.scan_type = {
464 			.sign = 'u',
465 			.realbits = 16,
466 			.storagebits = 16,
467 			.endianness = IIO_CPU,
468 		},
469 	},
470 	IIO_CHAN_SOFT_TIMESTAMP(6),
471 };
472 
473 static const struct iio_info tmag5273_info = {
474 	.read_avail = tmag5273_read_avail,
475 	.read_raw = tmag5273_read_raw,
476 	.write_raw = tmag5273_write_raw,
477 };
478 
479 static bool tmag5273_volatile_reg(struct device *dev, unsigned int reg)
480 {
481 	return reg >= TMAG5273_T_MSB_RESULT && reg <= TMAG5273_MAGNITUDE_RESULT;
482 }
483 
484 static const struct regmap_config tmag5273_regmap_config = {
485 	.reg_bits = 8,
486 	.val_bits = 8,
487 	.max_register = TMAG5273_MAX_REG,
488 	.volatile_reg = tmag5273_volatile_reg,
489 };
490 
491 static int tmag5273_set_operating_mode(struct tmag5273_data *data,
492 				       unsigned int val)
493 {
494 	return regmap_write(data->map, TMAG5273_DEVICE_CONFIG_2, val);
495 }
496 
497 static void tmag5273_read_device_property(struct tmag5273_data *data)
498 {
499 	struct device *dev = data->dev;
500 	int ret;
501 
502 	data->angle_measurement = TMAG5273_ANGLE_EN_X_Y;
503 
504 	ret = device_property_match_property_string(dev, "ti,angle-measurement",
505 						    tmag5273_angle_names,
506 						    ARRAY_SIZE(tmag5273_angle_names));
507 	if (ret >= 0)
508 		data->angle_measurement = ret;
509 }
510 
511 static void tmag5273_wake_up(struct tmag5273_data *data)
512 {
513 	int val;
514 
515 	/* Wake up the chip by sending a dummy I2C command */
516 	regmap_read(data->map, TMAG5273_DEVICE_ID, &val);
517 	/*
518 	 * Time to go to stand-by mode from sleep mode is 50us
519 	 * typically, during this time no I2C access is possible.
520 	 */
521 	usleep_range(80, 200);
522 }
523 
524 static int tmag5273_chip_init(struct tmag5273_data *data)
525 {
526 	int ret;
527 
528 	ret = regmap_write(data->map, TMAG5273_DEVICE_CONFIG_1,
529 			   TMAG5273_AVG_32_MODE);
530 	if (ret)
531 		return ret;
532 	data->conv_avg = 32;
533 
534 	ret = regmap_write(data->map, TMAG5273_DEVICE_CONFIG_2,
535 			   TMAG5273_OP_MODE_CONT);
536 	if (ret)
537 		return ret;
538 
539 	ret = regmap_write(data->map, TMAG5273_SENSOR_CONFIG_1,
540 			   FIELD_PREP(TMAG5273_MAG_CH_EN_MASK,
541 				      TMAG5273_MAG_CH_EN_X_Y_Z));
542 	if (ret)
543 		return ret;
544 
545 	ret = regmap_write(data->map, TMAG5273_SENSOR_CONFIG_2,
546 			   FIELD_PREP(TMAG5273_ANGLE_EN_MASK,
547 				      data->angle_measurement));
548 	if (ret)
549 		return ret;
550 	data->scale_index = MAGN_RANGE_LOW;
551 
552 	return regmap_write(data->map, TMAG5273_T_CONFIG, TMAG5273_T_CH_EN);
553 }
554 
555 static int tmag5273_check_device_id(struct tmag5273_data *data)
556 {
557 	__le16 devid;
558 	int val, ret;
559 
560 	ret = regmap_read(data->map, TMAG5273_DEVICE_ID, &val);
561 	if (ret)
562 		return dev_err_probe(data->dev, ret, "failed to power on device\n");
563 	data->version = FIELD_PREP(TMAG5273_VERSION_MASK, val);
564 
565 	ret = regmap_bulk_read(data->map, TMAG5273_MANUFACTURER_ID_LSB, &devid,
566 			       sizeof(devid));
567 	if (ret)
568 		return dev_err_probe(data->dev, ret, "failed to read device ID\n");
569 	data->devid = le16_to_cpu(devid);
570 
571 	switch (data->devid) {
572 	case TMAG5273_MANUFACTURER_ID:
573 		/*
574 		 * The device name matches the orderable part number. 'x' stands
575 		 * for A, B, C or D devices, which have different I2C addresses.
576 		 * Versions 1 or 2 (0 and 3 is reserved) stands for different
577 		 * magnetic strengths.
578 		 */
579 		snprintf(data->name, sizeof(data->name), "tmag5273x%1u", data->version);
580 		if (data->version < 1 || data->version > 2)
581 			dev_warn(data->dev, "Unsupported device %s\n", data->name);
582 		return 0;
583 	default:
584 		/*
585 		 * Only print warning in case of unknown device ID to allow
586 		 * fallback compatible in device tree.
587 		 */
588 		dev_warn(data->dev, "Unknown device ID 0x%x\n", data->devid);
589 		return 0;
590 	}
591 }
592 
593 static void tmag5273_power_down(void *data)
594 {
595 	tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_SLEEP);
596 }
597 
598 static int tmag5273_probe(struct i2c_client *i2c)
599 {
600 	struct device *dev = &i2c->dev;
601 	struct tmag5273_data *data;
602 	struct iio_dev *indio_dev;
603 	int ret;
604 
605 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
606 	if (!indio_dev)
607 		return -ENOMEM;
608 
609 	data = iio_priv(indio_dev);
610 	data->dev = dev;
611 	i2c_set_clientdata(i2c, indio_dev);
612 
613 	data->map = devm_regmap_init_i2c(i2c, &tmag5273_regmap_config);
614 	if (IS_ERR(data->map))
615 		return dev_err_probe(dev, PTR_ERR(data->map),
616 				     "failed to allocate register map\n");
617 
618 	mutex_init(&data->lock);
619 
620 	ret = devm_regulator_get_enable(dev, "vcc");
621 	if (ret)
622 		return dev_err_probe(dev, ret, "failed to enable regulator\n");
623 
624 	tmag5273_wake_up(data);
625 
626 	ret = tmag5273_check_device_id(data);
627 	if (ret)
628 		return ret;
629 
630 	ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT);
631 	if (ret)
632 		return dev_err_probe(dev, ret, "failed to power on device\n");
633 
634 	/*
635 	 * Register powerdown deferred callback which suspends the chip
636 	 * after module unloaded.
637 	 *
638 	 * TMAG5273 should be in SUSPEND mode in the two cases:
639 	 * 1) When driver is loaded, but we do not have any data or
640 	 *    configuration requests to it (we are solving it using
641 	 *    autosuspend feature).
642 	 * 2) When driver is unloaded and device is not used (devm action is
643 	 *    used in this case).
644 	 */
645 	ret = devm_add_action_or_reset(dev, tmag5273_power_down, data);
646 	if (ret)
647 		return dev_err_probe(dev, ret, "failed to add powerdown action\n");
648 
649 	ret = pm_runtime_set_active(dev);
650 	if (ret < 0)
651 		return ret;
652 
653 	ret = devm_pm_runtime_enable(dev);
654 	if (ret)
655 		return ret;
656 
657 	pm_runtime_get_noresume(dev);
658 	pm_runtime_set_autosuspend_delay(dev, TMAG5273_AUTOSLEEP_DELAY_MS);
659 	pm_runtime_use_autosuspend(dev);
660 
661 	tmag5273_read_device_property(data);
662 
663 	ret = tmag5273_chip_init(data);
664 	if (ret)
665 		return dev_err_probe(dev, ret, "failed to init device\n");
666 
667 	indio_dev->info = &tmag5273_info;
668 	indio_dev->modes = INDIO_DIRECT_MODE;
669 	indio_dev->name = data->name;
670 	indio_dev->channels = tmag5273_channels;
671 	indio_dev->num_channels = ARRAY_SIZE(tmag5273_channels);
672 
673 	pm_runtime_mark_last_busy(dev);
674 	pm_runtime_put_autosuspend(dev);
675 
676 	ret = devm_iio_device_register(dev, indio_dev);
677 	if (ret)
678 		return dev_err_probe(dev, ret, "device register failed\n");
679 
680 	return 0;
681 }
682 
683 static int tmag5273_runtime_suspend(struct device *dev)
684 {
685 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
686 	struct tmag5273_data *data = iio_priv(indio_dev);
687 	int ret;
688 
689 	ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_SLEEP);
690 	if (ret)
691 		dev_err(dev, "failed to power off device (%pe)\n", ERR_PTR(ret));
692 
693 	return ret;
694 }
695 
696 static int tmag5273_runtime_resume(struct device *dev)
697 {
698 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
699 	struct tmag5273_data *data = iio_priv(indio_dev);
700 	int ret;
701 
702 	tmag5273_wake_up(data);
703 
704 	ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT);
705 	if (ret)
706 		dev_err(dev, "failed to power on device (%pe)\n", ERR_PTR(ret));
707 
708 	return ret;
709 }
710 
711 static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops,
712 				 tmag5273_runtime_suspend, tmag5273_runtime_resume,
713 				 NULL);
714 
715 static const struct i2c_device_id tmag5273_id[] = {
716 	{ "tmag5273" },
717 	{ /* sentinel */ }
718 };
719 MODULE_DEVICE_TABLE(i2c, tmag5273_id);
720 
721 static const struct of_device_id tmag5273_of_match[] = {
722 	{ .compatible = "ti,tmag5273" },
723 	{ /* sentinel */ }
724 };
725 MODULE_DEVICE_TABLE(of, tmag5273_of_match);
726 
727 static struct i2c_driver tmag5273_driver = {
728 	.driver	 = {
729 		.name = "tmag5273",
730 		.of_match_table = tmag5273_of_match,
731 		.pm = pm_ptr(&tmag5273_pm_ops),
732 	},
733 	.probe = tmag5273_probe,
734 	.id_table = tmag5273_id,
735 };
736 module_i2c_driver(tmag5273_driver);
737 
738 MODULE_DESCRIPTION("TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor driver");
739 MODULE_AUTHOR("Gerald Loacker <gerald.loacker@wolfvision.net>");
740 MODULE_LICENSE("GPL");
741