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 	const char *str;
501 	int ret;
502 
503 	data->angle_measurement = TMAG5273_ANGLE_EN_X_Y;
504 
505 	ret = device_property_read_string(dev, "ti,angle-measurement", &str);
506 	if (ret)
507 		return;
508 
509 	ret = match_string(tmag5273_angle_names,
510 			   ARRAY_SIZE(tmag5273_angle_names), str);
511 	if (ret >= 0)
512 		data->angle_measurement = ret;
513 }
514 
515 static void tmag5273_wake_up(struct tmag5273_data *data)
516 {
517 	int val;
518 
519 	/* Wake up the chip by sending a dummy I2C command */
520 	regmap_read(data->map, TMAG5273_DEVICE_ID, &val);
521 	/*
522 	 * Time to go to stand-by mode from sleep mode is 50us
523 	 * typically, during this time no I2C access is possible.
524 	 */
525 	usleep_range(80, 200);
526 }
527 
528 static int tmag5273_chip_init(struct tmag5273_data *data)
529 {
530 	int ret;
531 
532 	ret = regmap_write(data->map, TMAG5273_DEVICE_CONFIG_1,
533 			   TMAG5273_AVG_32_MODE);
534 	if (ret)
535 		return ret;
536 	data->conv_avg = 32;
537 
538 	ret = regmap_write(data->map, TMAG5273_DEVICE_CONFIG_2,
539 			   TMAG5273_OP_MODE_CONT);
540 	if (ret)
541 		return ret;
542 
543 	ret = regmap_write(data->map, TMAG5273_SENSOR_CONFIG_1,
544 			   FIELD_PREP(TMAG5273_MAG_CH_EN_MASK,
545 				      TMAG5273_MAG_CH_EN_X_Y_Z));
546 	if (ret)
547 		return ret;
548 
549 	ret = regmap_write(data->map, TMAG5273_SENSOR_CONFIG_2,
550 			   FIELD_PREP(TMAG5273_ANGLE_EN_MASK,
551 				      data->angle_measurement));
552 	if (ret)
553 		return ret;
554 	data->scale_index = MAGN_RANGE_LOW;
555 
556 	return regmap_write(data->map, TMAG5273_T_CONFIG, TMAG5273_T_CH_EN);
557 }
558 
559 static int tmag5273_check_device_id(struct tmag5273_data *data)
560 {
561 	__le16 devid;
562 	int val, ret;
563 
564 	ret = regmap_read(data->map, TMAG5273_DEVICE_ID, &val);
565 	if (ret)
566 		return dev_err_probe(data->dev, ret, "failed to power on device\n");
567 	data->version = FIELD_PREP(TMAG5273_VERSION_MASK, val);
568 
569 	ret = regmap_bulk_read(data->map, TMAG5273_MANUFACTURER_ID_LSB, &devid,
570 			       sizeof(devid));
571 	if (ret)
572 		return dev_err_probe(data->dev, ret, "failed to read device ID\n");
573 	data->devid = le16_to_cpu(devid);
574 
575 	switch (data->devid) {
576 	case TMAG5273_MANUFACTURER_ID:
577 		/*
578 		 * The device name matches the orderable part number. 'x' stands
579 		 * for A, B, C or D devices, which have different I2C addresses.
580 		 * Versions 1 or 2 (0 and 3 is reserved) stands for different
581 		 * magnetic strengths.
582 		 */
583 		snprintf(data->name, sizeof(data->name), "tmag5273x%1u", data->version);
584 		if (data->version < 1 || data->version > 2)
585 			dev_warn(data->dev, "Unsupported device %s\n", data->name);
586 		return 0;
587 	default:
588 		/*
589 		 * Only print warning in case of unknown device ID to allow
590 		 * fallback compatible in device tree.
591 		 */
592 		dev_warn(data->dev, "Unknown device ID 0x%x\n", data->devid);
593 		return 0;
594 	}
595 }
596 
597 static void tmag5273_power_down(void *data)
598 {
599 	tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_SLEEP);
600 }
601 
602 static int tmag5273_probe(struct i2c_client *i2c)
603 {
604 	struct device *dev = &i2c->dev;
605 	struct tmag5273_data *data;
606 	struct iio_dev *indio_dev;
607 	int ret;
608 
609 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
610 	if (!indio_dev)
611 		return -ENOMEM;
612 
613 	data = iio_priv(indio_dev);
614 	data->dev = dev;
615 	i2c_set_clientdata(i2c, indio_dev);
616 
617 	data->map = devm_regmap_init_i2c(i2c, &tmag5273_regmap_config);
618 	if (IS_ERR(data->map))
619 		return dev_err_probe(dev, PTR_ERR(data->map),
620 				     "failed to allocate register map\n");
621 
622 	mutex_init(&data->lock);
623 
624 	ret = devm_regulator_get_enable(dev, "vcc");
625 	if (ret)
626 		return dev_err_probe(dev, ret, "failed to enable regulator\n");
627 
628 	tmag5273_wake_up(data);
629 
630 	ret = tmag5273_check_device_id(data);
631 	if (ret)
632 		return ret;
633 
634 	ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT);
635 	if (ret)
636 		return dev_err_probe(dev, ret, "failed to power on device\n");
637 
638 	/*
639 	 * Register powerdown deferred callback which suspends the chip
640 	 * after module unloaded.
641 	 *
642 	 * TMAG5273 should be in SUSPEND mode in the two cases:
643 	 * 1) When driver is loaded, but we do not have any data or
644 	 *    configuration requests to it (we are solving it using
645 	 *    autosuspend feature).
646 	 * 2) When driver is unloaded and device is not used (devm action is
647 	 *    used in this case).
648 	 */
649 	ret = devm_add_action_or_reset(dev, tmag5273_power_down, data);
650 	if (ret)
651 		return dev_err_probe(dev, ret, "failed to add powerdown action\n");
652 
653 	ret = pm_runtime_set_active(dev);
654 	if (ret < 0)
655 		return ret;
656 
657 	ret = devm_pm_runtime_enable(dev);
658 	if (ret)
659 		return ret;
660 
661 	pm_runtime_get_noresume(dev);
662 	pm_runtime_set_autosuspend_delay(dev, TMAG5273_AUTOSLEEP_DELAY_MS);
663 	pm_runtime_use_autosuspend(dev);
664 
665 	tmag5273_read_device_property(data);
666 
667 	ret = tmag5273_chip_init(data);
668 	if (ret)
669 		return dev_err_probe(dev, ret, "failed to init device\n");
670 
671 	indio_dev->info = &tmag5273_info;
672 	indio_dev->modes = INDIO_DIRECT_MODE;
673 	indio_dev->name = data->name;
674 	indio_dev->channels = tmag5273_channels;
675 	indio_dev->num_channels = ARRAY_SIZE(tmag5273_channels);
676 
677 	pm_runtime_mark_last_busy(dev);
678 	pm_runtime_put_autosuspend(dev);
679 
680 	ret = devm_iio_device_register(dev, indio_dev);
681 	if (ret)
682 		return dev_err_probe(dev, ret, "device register failed\n");
683 
684 	return 0;
685 }
686 
687 static int tmag5273_runtime_suspend(struct device *dev)
688 {
689 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
690 	struct tmag5273_data *data = iio_priv(indio_dev);
691 	int ret;
692 
693 	ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_SLEEP);
694 	if (ret)
695 		dev_err(dev, "failed to power off device (%pe)\n", ERR_PTR(ret));
696 
697 	return ret;
698 }
699 
700 static int tmag5273_runtime_resume(struct device *dev)
701 {
702 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
703 	struct tmag5273_data *data = iio_priv(indio_dev);
704 	int ret;
705 
706 	tmag5273_wake_up(data);
707 
708 	ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT);
709 	if (ret)
710 		dev_err(dev, "failed to power on device (%pe)\n", ERR_PTR(ret));
711 
712 	return ret;
713 }
714 
715 static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops,
716 				 tmag5273_runtime_suspend, tmag5273_runtime_resume,
717 				 NULL);
718 
719 static const struct i2c_device_id tmag5273_id[] = {
720 	{ "tmag5273" },
721 	{ /* sentinel */ }
722 };
723 MODULE_DEVICE_TABLE(i2c, tmag5273_id);
724 
725 static const struct of_device_id tmag5273_of_match[] = {
726 	{ .compatible = "ti,tmag5273" },
727 	{ /* sentinel */ }
728 };
729 MODULE_DEVICE_TABLE(of, tmag5273_of_match);
730 
731 static struct i2c_driver tmag5273_driver = {
732 	.driver	 = {
733 		.name = "tmag5273",
734 		.of_match_table = tmag5273_of_match,
735 		.pm = pm_ptr(&tmag5273_pm_ops),
736 	},
737 	.probe = tmag5273_probe,
738 	.id_table = tmag5273_id,
739 };
740 module_i2c_driver(tmag5273_driver);
741 
742 MODULE_DESCRIPTION("TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor driver");
743 MODULE_AUTHOR("Gerald Loacker <gerald.loacker@wolfvision.net>");
744 MODULE_LICENSE("GPL");
745