1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor
5 * Copyright (C) 2022 WolfVision GmbH
7 * Author: Gerald Loacker <gerald.loacker@wolfvision.net>
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>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
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
52 #define TMAG5273_AUTOSLEEP_DELAY_MS 5000
53 #define TMAG5273_MAX_AVERAGE 32
56 * bits in the TMAG5273_MANUFACTURER_ID_LSB / MSB register
57 * 16-bit unique manufacturer ID 0x49 / 0x54 = "TI"
59 #define TMAG5273_MANUFACTURER_ID 0x5449
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)
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)
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
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
90 /* bits in the TMAG5273_T_CONFIG register */
91 #define TMAG5273_T_CH_EN BIT(0)
93 /* bits in the TMAG5273_DEVICE_ID register */
94 #define TMAG5273_VERSION_MASK GENMASK(1, 0)
96 /* bits in the TMAG5273_CONV_STATUS register */
97 #define TMAG5273_CONV_STATUS_COMPLETE BIT(0)
99 enum tmag5273_channels
{
108 enum tmag5273_scale_index
{
114 /* state container for the TMAG5273 driver */
115 struct tmag5273_data
{
118 unsigned int version
;
120 unsigned int conv_avg
;
121 enum tmag5273_scale_index scale_index
;
122 unsigned int angle_measurement
;
126 * Locks the sensor for exclusive use during a measurement (which
127 * involves several register transactions so the regmap lock is not
128 * enough) so that measurements get serialized in a
129 * first-come-first-serve manner.
134 static const char *const tmag5273_angle_names
[] = { "off", "x-y", "y-z", "x-z" };
137 * Averaging enables additional sampling of the sensor data to reduce the noise
138 * effect, but also increases conversion time.
140 static const unsigned int tmag5273_avg_table
[] = {
145 * Magnetic resolution in Gauss for different TMAG5273 versions.
146 * Scale[Gauss] = Range[mT] * 1000 / 2^15 * 10, (1 mT = 10 Gauss)
147 * Only version 1 and 2 are valid, version 0 and 3 are reserved.
149 static const struct iio_val_int_plus_micro tmag5273_scale
[][MAGN_RANGE_NUM
] = {
150 { { 0, 0 }, { 0, 0 } },
151 { { 0, 12200 }, { 0, 24400 } },
152 { { 0, 40600 }, { 0, 81200 } },
153 { { 0, 0 }, { 0, 0 } },
156 static int tmag5273_get_measure(struct tmag5273_data
*data
, s16
*t
, s16
*x
,
157 s16
*y
, s16
*z
, u16
*angle
, u16
*magnitude
)
159 unsigned int status
, val
;
163 mutex_lock(&data
->lock
);
166 * Max. conversion time is 2425 us in 32x averaging mode for all three
167 * channels. Since we are in continuous measurement mode, a measurement
168 * may already be there, so poll for completed measurement with
171 ret
= regmap_read_poll_timeout(data
->map
, TMAG5273_CONV_STATUS
, status
,
172 status
& TMAG5273_CONV_STATUS_COMPLETE
,
175 dev_err(data
->dev
, "timeout waiting for measurement\n");
179 ret
= regmap_bulk_read(data
->map
, TMAG5273_T_MSB_RESULT
, reg_data
,
183 *t
= be16_to_cpu(reg_data
[0]);
184 *x
= be16_to_cpu(reg_data
[1]);
185 *y
= be16_to_cpu(reg_data
[2]);
186 *z
= be16_to_cpu(reg_data
[3]);
188 ret
= regmap_bulk_read(data
->map
, TMAG5273_ANGLE_RESULT_MSB
,
189 ®_data
[0], sizeof(reg_data
[0]));
193 * angle has 9 bits integer value and 4 bits fractional part
194 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
195 * 0 0 0 a a a a a a a a a f f f f
197 *angle
= be16_to_cpu(reg_data
[0]);
199 ret
= regmap_read(data
->map
, TMAG5273_MAGNITUDE_RESULT
, &val
);
205 mutex_unlock(&data
->lock
);
209 static int tmag5273_write_osr(struct tmag5273_data
*data
, int val
)
213 if (val
== data
->conv_avg
)
216 for (i
= 0; i
< ARRAY_SIZE(tmag5273_avg_table
); i
++) {
217 if (tmag5273_avg_table
[i
] == val
)
220 if (i
== ARRAY_SIZE(tmag5273_avg_table
))
222 data
->conv_avg
= val
;
224 return regmap_update_bits(data
->map
, TMAG5273_DEVICE_CONFIG_1
,
225 TMAG5273_AVG_MODE_MASK
,
226 FIELD_PREP(TMAG5273_AVG_MODE_MASK
, i
));
229 static int tmag5273_write_scale(struct tmag5273_data
*data
, int scale_micro
)
234 for (i
= 0; i
< ARRAY_SIZE(tmag5273_scale
[0]); i
++) {
235 if (tmag5273_scale
[data
->version
][i
].micro
== scale_micro
)
238 if (i
== ARRAY_SIZE(tmag5273_scale
[0]))
240 data
->scale_index
= i
;
242 if (data
->scale_index
== MAGN_RANGE_LOW
)
245 value
= TMAG5273_Z_RANGE_MASK
| TMAG5273_X_Y_RANGE_MASK
;
247 return regmap_update_bits(data
->map
, TMAG5273_SENSOR_CONFIG_2
,
248 TMAG5273_Z_RANGE_MASK
| TMAG5273_X_Y_RANGE_MASK
, value
);
251 static int tmag5273_read_avail(struct iio_dev
*indio_dev
,
252 struct iio_chan_spec
const *chan
,
253 const int **vals
, int *type
, int *length
,
256 struct tmag5273_data
*data
= iio_priv(indio_dev
);
259 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
260 *vals
= tmag5273_avg_table
;
262 *length
= ARRAY_SIZE(tmag5273_avg_table
);
263 return IIO_AVAIL_LIST
;
264 case IIO_CHAN_INFO_SCALE
:
265 switch (chan
->type
) {
267 *type
= IIO_VAL_INT_PLUS_MICRO
;
268 *vals
= (int *)tmag5273_scale
[data
->version
];
269 *length
= ARRAY_SIZE(tmag5273_scale
[data
->version
]) *
271 return IIO_AVAIL_LIST
;
280 static int tmag5273_read_raw(struct iio_dev
*indio_dev
,
281 const struct iio_chan_spec
*chan
, int *val
,
282 int *val2
, long mask
)
284 struct tmag5273_data
*data
= iio_priv(indio_dev
);
286 u16 angle
, magnitude
;
290 case IIO_CHAN_INFO_PROCESSED
:
291 case IIO_CHAN_INFO_RAW
:
292 ret
= pm_runtime_resume_and_get(data
->dev
);
296 ret
= tmag5273_get_measure(data
, &t
, &x
, &y
, &z
, &angle
, &magnitude
);
298 pm_runtime_mark_last_busy(data
->dev
);
299 pm_runtime_put_autosuspend(data
->dev
);
304 switch (chan
->address
) {
326 case IIO_CHAN_INFO_SCALE
:
327 switch (chan
->type
) {
330 * Convert device specific value to millicelsius.
331 * Resolution from the sensor is 60.1 LSB/celsius and
332 * the reference value at 25 celsius is 17508 LSBs.
336 return IIO_VAL_FRACTIONAL
;
338 /* Magnetic resolution in uT */
340 *val2
= tmag5273_scale
[data
->version
]
341 [data
->scale_index
].micro
;
342 return IIO_VAL_INT_PLUS_MICRO
;
345 * Angle is in degrees and has four fractional bits,
346 * therefore use 1/16 * pi/180 to convert to radians.
350 return IIO_VAL_FRACTIONAL
;
354 case IIO_CHAN_INFO_OFFSET
:
355 switch (chan
->type
) {
362 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
363 *val
= data
->conv_avg
;
371 static int tmag5273_write_raw(struct iio_dev
*indio_dev
,
372 struct iio_chan_spec
const *chan
, int val
,
375 struct tmag5273_data
*data
= iio_priv(indio_dev
);
378 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
379 return tmag5273_write_osr(data
, val
);
380 case IIO_CHAN_INFO_SCALE
:
381 switch (chan
->type
) {
385 return tmag5273_write_scale(data
, val2
);
394 #define TMAG5273_AXIS_CHANNEL(axis, index) \
398 .channel2 = IIO_MOD_##axis, \
399 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
400 BIT(IIO_CHAN_INFO_SCALE), \
401 .info_mask_shared_by_type_available = \
402 BIT(IIO_CHAN_INFO_SCALE), \
403 .info_mask_shared_by_all = \
404 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
405 .info_mask_shared_by_all_available = \
406 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
408 .scan_index = index, \
413 .endianness = IIO_CPU, \
417 static const struct iio_chan_spec tmag5273_channels
[] = {
420 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
421 BIT(IIO_CHAN_INFO_SCALE
) |
422 BIT(IIO_CHAN_INFO_OFFSET
),
423 .address
= TEMPERATURE
,
424 .scan_index
= TEMPERATURE
,
429 .endianness
= IIO_CPU
,
432 TMAG5273_AXIS_CHANNEL(X
, AXIS_X
),
433 TMAG5273_AXIS_CHANNEL(Y
, AXIS_Y
),
434 TMAG5273_AXIS_CHANNEL(Z
, AXIS_Z
),
437 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
438 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
439 .info_mask_shared_by_all
=
440 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
441 .info_mask_shared_by_all_available
=
442 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
449 .endianness
= IIO_CPU
,
453 .type
= IIO_DISTANCE
,
454 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
455 .info_mask_shared_by_all
=
456 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
457 .info_mask_shared_by_all_available
=
458 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
459 .address
= MAGNITUDE
,
460 .scan_index
= MAGNITUDE
,
465 .endianness
= IIO_CPU
,
468 IIO_CHAN_SOFT_TIMESTAMP(6),
471 static const struct iio_info tmag5273_info
= {
472 .read_avail
= tmag5273_read_avail
,
473 .read_raw
= tmag5273_read_raw
,
474 .write_raw
= tmag5273_write_raw
,
477 static bool tmag5273_volatile_reg(struct device
*dev
, unsigned int reg
)
479 return reg
>= TMAG5273_T_MSB_RESULT
&& reg
<= TMAG5273_MAGNITUDE_RESULT
;
482 static const struct regmap_config tmag5273_regmap_config
= {
485 .max_register
= TMAG5273_MAX_REG
,
486 .volatile_reg
= tmag5273_volatile_reg
,
489 static int tmag5273_set_operating_mode(struct tmag5273_data
*data
,
492 return regmap_write(data
->map
, TMAG5273_DEVICE_CONFIG_2
, val
);
495 static void tmag5273_read_device_property(struct tmag5273_data
*data
)
497 struct device
*dev
= data
->dev
;
500 data
->angle_measurement
= TMAG5273_ANGLE_EN_X_Y
;
502 ret
= device_property_match_property_string(dev
, "ti,angle-measurement",
503 tmag5273_angle_names
,
504 ARRAY_SIZE(tmag5273_angle_names
));
506 data
->angle_measurement
= ret
;
509 static void tmag5273_wake_up(struct tmag5273_data
*data
)
513 /* Wake up the chip by sending a dummy I2C command */
514 regmap_read(data
->map
, TMAG5273_DEVICE_ID
, &val
);
516 * Time to go to stand-by mode from sleep mode is 50us
517 * typically, during this time no I2C access is possible.
519 usleep_range(80, 200);
522 static int tmag5273_chip_init(struct tmag5273_data
*data
)
526 ret
= regmap_write(data
->map
, TMAG5273_DEVICE_CONFIG_1
,
527 TMAG5273_AVG_32_MODE
);
532 ret
= regmap_write(data
->map
, TMAG5273_DEVICE_CONFIG_2
,
533 TMAG5273_OP_MODE_CONT
);
537 ret
= regmap_write(data
->map
, TMAG5273_SENSOR_CONFIG_1
,
538 FIELD_PREP(TMAG5273_MAG_CH_EN_MASK
,
539 TMAG5273_MAG_CH_EN_X_Y_Z
));
543 ret
= regmap_write(data
->map
, TMAG5273_SENSOR_CONFIG_2
,
544 FIELD_PREP(TMAG5273_ANGLE_EN_MASK
,
545 data
->angle_measurement
));
548 data
->scale_index
= MAGN_RANGE_LOW
;
550 return regmap_write(data
->map
, TMAG5273_T_CONFIG
, TMAG5273_T_CH_EN
);
553 static int tmag5273_check_device_id(struct tmag5273_data
*data
)
558 ret
= regmap_read(data
->map
, TMAG5273_DEVICE_ID
, &val
);
560 return dev_err_probe(data
->dev
, ret
, "failed to power on device\n");
561 data
->version
= FIELD_PREP(TMAG5273_VERSION_MASK
, val
);
563 ret
= regmap_bulk_read(data
->map
, TMAG5273_MANUFACTURER_ID_LSB
, &devid
,
566 return dev_err_probe(data
->dev
, ret
, "failed to read device ID\n");
567 data
->devid
= le16_to_cpu(devid
);
569 switch (data
->devid
) {
570 case TMAG5273_MANUFACTURER_ID
:
572 * The device name matches the orderable part number. 'x' stands
573 * for A, B, C or D devices, which have different I2C addresses.
574 * Versions 1 or 2 (0 and 3 is reserved) stands for different
575 * magnetic strengths.
577 snprintf(data
->name
, sizeof(data
->name
), "tmag5273x%1u", data
->version
);
578 if (data
->version
< 1 || data
->version
> 2)
579 dev_warn(data
->dev
, "Unsupported device %s\n", data
->name
);
583 * Only print warning in case of unknown device ID to allow
584 * fallback compatible in device tree.
586 dev_warn(data
->dev
, "Unknown device ID 0x%x\n", data
->devid
);
591 static void tmag5273_power_down(void *data
)
593 tmag5273_set_operating_mode(data
, TMAG5273_OP_MODE_SLEEP
);
596 static int tmag5273_probe(struct i2c_client
*i2c
)
598 struct device
*dev
= &i2c
->dev
;
599 struct tmag5273_data
*data
;
600 struct iio_dev
*indio_dev
;
603 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
607 data
= iio_priv(indio_dev
);
609 i2c_set_clientdata(i2c
, indio_dev
);
611 data
->map
= devm_regmap_init_i2c(i2c
, &tmag5273_regmap_config
);
612 if (IS_ERR(data
->map
))
613 return dev_err_probe(dev
, PTR_ERR(data
->map
),
614 "failed to allocate register map\n");
616 mutex_init(&data
->lock
);
618 ret
= devm_regulator_get_enable(dev
, "vcc");
620 return dev_err_probe(dev
, ret
, "failed to enable regulator\n");
622 tmag5273_wake_up(data
);
624 ret
= tmag5273_check_device_id(data
);
628 ret
= tmag5273_set_operating_mode(data
, TMAG5273_OP_MODE_CONT
);
630 return dev_err_probe(dev
, ret
, "failed to power on device\n");
633 * Register powerdown deferred callback which suspends the chip
634 * after module unloaded.
636 * TMAG5273 should be in SUSPEND mode in the two cases:
637 * 1) When driver is loaded, but we do not have any data or
638 * configuration requests to it (we are solving it using
639 * autosuspend feature).
640 * 2) When driver is unloaded and device is not used (devm action is
641 * used in this case).
643 ret
= devm_add_action_or_reset(dev
, tmag5273_power_down
, data
);
645 return dev_err_probe(dev
, ret
, "failed to add powerdown action\n");
647 ret
= pm_runtime_set_active(dev
);
651 ret
= devm_pm_runtime_enable(dev
);
655 pm_runtime_get_noresume(dev
);
656 pm_runtime_set_autosuspend_delay(dev
, TMAG5273_AUTOSLEEP_DELAY_MS
);
657 pm_runtime_use_autosuspend(dev
);
659 tmag5273_read_device_property(data
);
661 ret
= tmag5273_chip_init(data
);
663 return dev_err_probe(dev
, ret
, "failed to init device\n");
665 indio_dev
->info
= &tmag5273_info
;
666 indio_dev
->modes
= INDIO_DIRECT_MODE
;
667 indio_dev
->name
= data
->name
;
668 indio_dev
->channels
= tmag5273_channels
;
669 indio_dev
->num_channels
= ARRAY_SIZE(tmag5273_channels
);
671 pm_runtime_mark_last_busy(dev
);
672 pm_runtime_put_autosuspend(dev
);
674 ret
= devm_iio_device_register(dev
, indio_dev
);
676 return dev_err_probe(dev
, ret
, "device register failed\n");
681 static int tmag5273_runtime_suspend(struct device
*dev
)
683 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
684 struct tmag5273_data
*data
= iio_priv(indio_dev
);
687 ret
= tmag5273_set_operating_mode(data
, TMAG5273_OP_MODE_SLEEP
);
689 dev_err(dev
, "failed to power off device (%pe)\n", ERR_PTR(ret
));
694 static int tmag5273_runtime_resume(struct device
*dev
)
696 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
697 struct tmag5273_data
*data
= iio_priv(indio_dev
);
700 tmag5273_wake_up(data
);
702 ret
= tmag5273_set_operating_mode(data
, TMAG5273_OP_MODE_CONT
);
704 dev_err(dev
, "failed to power on device (%pe)\n", ERR_PTR(ret
));
709 static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops
,
710 tmag5273_runtime_suspend
, tmag5273_runtime_resume
,
713 static const struct i2c_device_id tmag5273_id
[] = {
717 MODULE_DEVICE_TABLE(i2c
, tmag5273_id
);
719 static const struct of_device_id tmag5273_of_match
[] = {
720 { .compatible
= "ti,tmag5273" },
723 MODULE_DEVICE_TABLE(of
, tmag5273_of_match
);
725 static struct i2c_driver tmag5273_driver
= {
728 .of_match_table
= tmag5273_of_match
,
729 .pm
= pm_ptr(&tmag5273_pm_ops
),
731 .probe
= tmag5273_probe
,
732 .id_table
= tmag5273_id
,
734 module_i2c_driver(tmag5273_driver
);
736 MODULE_DESCRIPTION("TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor driver");
737 MODULE_AUTHOR("Gerald Loacker <gerald.loacker@wolfvision.net>");
738 MODULE_LICENSE("GPL");