1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Device driver for the the HMC5843 multi-chip module designed
4 * for low field magnetic sensing.
6 * Copyright (C) 2010 Texas Instruments
8 * Author: Shubhrajyoti Datta <shubhrajyoti@ti.com>
9 * Acknowledgment: Jonathan Cameron <jic23@kernel.org> for valuable inputs.
10 * Support for HMC5883 and HMC5883L by Peter Meerwald <pmeerw@pmeerw.net>.
11 * Split to multiple files by Josef Gajdusek <atx@atx.name> - 2014
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/delay.h>
26 * Range gain settings in (+-)Ga
27 * Beware: HMC5843 and HMC5883 have different recommended sensor field
28 * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
30 #define HMC5843_RANGE_GAIN_OFFSET 0x05
31 #define HMC5843_RANGE_GAIN_DEFAULT 0x01
32 #define HMC5843_RANGE_GAIN_MASK 0xe0
35 #define HMC5843_DATA_READY 0x01
36 #define HMC5843_DATA_OUTPUT_LOCK 0x02
38 /* Mode register configuration */
39 #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
40 #define HMC5843_MODE_CONVERSION_SINGLE 0x01
41 #define HMC5843_MODE_IDLE 0x02
42 #define HMC5843_MODE_SLEEP 0x03
43 #define HMC5843_MODE_MASK 0x03
46 * HMC5843: Minimum data output rate
47 * HMC5883: Typical data output rate
49 #define HMC5843_RATE_OFFSET 0x02
50 #define HMC5843_RATE_DEFAULT 0x04
51 #define HMC5843_RATE_MASK 0x1c
53 /* Device measurement configuration */
54 #define HMC5843_MEAS_CONF_NORMAL 0x00
55 #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
56 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
57 #define HMC5843_MEAS_CONF_MASK 0x03
60 * API for setting the measurement configuration to
61 * Normal, Positive bias and Negative bias
64 * 0 - Normal measurement configuration (default): In normal measurement
65 * configuration the device follows normal measurement flow. Pins BP
66 * and BN are left floating and high impedance.
68 * 1 - Positive bias configuration: In positive bias configuration, a
69 * positive current is forced across the resistive load on pins BP
72 * 2 - Negative bias configuration. In negative bias configuration, a
73 * negative current is forced across the resistive load on pins BP
76 * 3 - Only available on HMC5983. Magnetic sensor is disabled.
77 * Temperature sensor is enabled.
80 static const char *const hmc5843_meas_conf_modes
[] = {"normal", "positivebias",
83 static const char *const hmc5983_meas_conf_modes
[] = {"normal", "positivebias",
86 /* Scaling factors: 10000000/Gain */
87 static const int hmc5843_regval_to_nanoscale
[] = {
88 6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
91 static const int hmc5883_regval_to_nanoscale
[] = {
92 7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
95 static const int hmc5883l_regval_to_nanoscale
[] = {
96 7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
100 * From the datasheet:
101 * Value | HMC5843 | HMC5883/HMC5883L
102 * | Data output rate (Hz) | Data output rate (Hz)
107 * 4 | 10 (default) | 15
110 * 7 | Not used | Not used
112 static const int hmc5843_regval_to_samp_freq
[][2] = {
113 {0, 500000}, {1, 0}, {2, 0}, {5, 0}, {10, 0}, {20, 0}, {50, 0}
116 static const int hmc5883_regval_to_samp_freq
[][2] = {
117 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
121 static const int hmc5983_regval_to_samp_freq
[][2] = {
122 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
126 /* Describe chip variants */
127 struct hmc5843_chip_info
{
128 const struct iio_chan_spec
*channels
;
129 const int (*regval_to_samp_freq
)[2];
130 const int n_regval_to_samp_freq
;
131 const int *regval_to_nanoscale
;
132 const int n_regval_to_nanoscale
;
135 /* The lower two bits contain the current conversion mode */
136 static s32
hmc5843_set_mode(struct hmc5843_data
*data
, u8 operating_mode
)
140 mutex_lock(&data
->lock
);
141 ret
= regmap_update_bits(data
->regmap
, HMC5843_MODE_REG
,
142 HMC5843_MODE_MASK
, operating_mode
);
143 mutex_unlock(&data
->lock
);
148 static int hmc5843_wait_measurement(struct hmc5843_data
*data
)
154 while (tries
-- > 0) {
155 ret
= regmap_read(data
->regmap
, HMC5843_STATUS_REG
, &val
);
158 if (val
& HMC5843_DATA_READY
)
164 dev_err(data
->dev
, "data not ready\n");
171 /* Return the measurement value from the specified channel */
172 static int hmc5843_read_measurement(struct hmc5843_data
*data
,
178 mutex_lock(&data
->lock
);
179 ret
= hmc5843_wait_measurement(data
);
181 mutex_unlock(&data
->lock
);
184 ret
= regmap_bulk_read(data
->regmap
, HMC5843_DATA_OUT_MSB_REGS
,
185 values
, sizeof(values
));
186 mutex_unlock(&data
->lock
);
190 *val
= sign_extend32(be16_to_cpu(values
[idx
]), 15);
194 static int hmc5843_set_meas_conf(struct hmc5843_data
*data
, u8 meas_conf
)
198 mutex_lock(&data
->lock
);
199 ret
= regmap_update_bits(data
->regmap
, HMC5843_CONFIG_REG_A
,
200 HMC5843_MEAS_CONF_MASK
, meas_conf
);
201 mutex_unlock(&data
->lock
);
207 int hmc5843_show_measurement_configuration(struct iio_dev
*indio_dev
,
208 const struct iio_chan_spec
*chan
)
210 struct hmc5843_data
*data
= iio_priv(indio_dev
);
214 ret
= regmap_read(data
->regmap
, HMC5843_CONFIG_REG_A
, &val
);
218 return val
& HMC5843_MEAS_CONF_MASK
;
222 int hmc5843_set_measurement_configuration(struct iio_dev
*indio_dev
,
223 const struct iio_chan_spec
*chan
,
224 unsigned int meas_conf
)
226 struct hmc5843_data
*data
= iio_priv(indio_dev
);
228 return hmc5843_set_meas_conf(data
, meas_conf
);
231 static const struct iio_mount_matrix
*
232 hmc5843_get_mount_matrix(const struct iio_dev
*indio_dev
,
233 const struct iio_chan_spec
*chan
)
235 struct hmc5843_data
*data
= iio_priv(indio_dev
);
237 return &data
->orientation
;
240 static const struct iio_enum hmc5843_meas_conf_enum
= {
241 .items
= hmc5843_meas_conf_modes
,
242 .num_items
= ARRAY_SIZE(hmc5843_meas_conf_modes
),
243 .get
= hmc5843_show_measurement_configuration
,
244 .set
= hmc5843_set_measurement_configuration
,
247 static const struct iio_chan_spec_ext_info hmc5843_ext_info
[] = {
248 IIO_ENUM("meas_conf", IIO_SHARED_BY_TYPE
, &hmc5843_meas_conf_enum
),
249 IIO_ENUM_AVAILABLE("meas_conf", &hmc5843_meas_conf_enum
),
250 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, hmc5843_get_mount_matrix
),
254 static const struct iio_enum hmc5983_meas_conf_enum
= {
255 .items
= hmc5983_meas_conf_modes
,
256 .num_items
= ARRAY_SIZE(hmc5983_meas_conf_modes
),
257 .get
= hmc5843_show_measurement_configuration
,
258 .set
= hmc5843_set_measurement_configuration
,
261 static const struct iio_chan_spec_ext_info hmc5983_ext_info
[] = {
262 IIO_ENUM("meas_conf", IIO_SHARED_BY_TYPE
, &hmc5983_meas_conf_enum
),
263 IIO_ENUM_AVAILABLE("meas_conf", &hmc5983_meas_conf_enum
),
264 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, hmc5843_get_mount_matrix
),
269 ssize_t
hmc5843_show_samp_freq_avail(struct device
*dev
,
270 struct device_attribute
*attr
, char *buf
)
272 struct hmc5843_data
*data
= iio_priv(dev_to_iio_dev(dev
));
276 for (i
= 0; i
< data
->variant
->n_regval_to_samp_freq
; i
++)
277 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
278 "%d.%d ", data
->variant
->regval_to_samp_freq
[i
][0],
279 data
->variant
->regval_to_samp_freq
[i
][1]);
281 /* replace trailing space by newline */
287 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_samp_freq_avail
);
289 static int hmc5843_set_samp_freq(struct hmc5843_data
*data
, u8 rate
)
293 mutex_lock(&data
->lock
);
294 ret
= regmap_update_bits(data
->regmap
, HMC5843_CONFIG_REG_A
,
296 rate
<< HMC5843_RATE_OFFSET
);
297 mutex_unlock(&data
->lock
);
302 static int hmc5843_get_samp_freq_index(struct hmc5843_data
*data
,
307 for (i
= 0; i
< data
->variant
->n_regval_to_samp_freq
; i
++)
308 if (val
== data
->variant
->regval_to_samp_freq
[i
][0] &&
309 val2
== data
->variant
->regval_to_samp_freq
[i
][1])
315 static int hmc5843_set_range_gain(struct hmc5843_data
*data
, u8 range
)
319 mutex_lock(&data
->lock
);
320 ret
= regmap_update_bits(data
->regmap
, HMC5843_CONFIG_REG_B
,
321 HMC5843_RANGE_GAIN_MASK
,
322 range
<< HMC5843_RANGE_GAIN_OFFSET
);
323 mutex_unlock(&data
->lock
);
328 static ssize_t
hmc5843_show_scale_avail(struct device
*dev
,
329 struct device_attribute
*attr
,
332 struct hmc5843_data
*data
= iio_priv(dev_to_iio_dev(dev
));
337 for (i
= 0; i
< data
->variant
->n_regval_to_nanoscale
; i
++)
338 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
339 "0.%09d ", data
->variant
->regval_to_nanoscale
[i
]);
341 /* replace trailing space by newline */
347 static IIO_DEVICE_ATTR(scale_available
, S_IRUGO
,
348 hmc5843_show_scale_avail
, NULL
, 0);
350 static int hmc5843_get_scale_index(struct hmc5843_data
*data
, int val
, int val2
)
357 for (i
= 0; i
< data
->variant
->n_regval_to_nanoscale
; i
++)
358 if (val2
== data
->variant
->regval_to_nanoscale
[i
])
364 static int hmc5843_read_raw(struct iio_dev
*indio_dev
,
365 struct iio_chan_spec
const *chan
,
366 int *val
, int *val2
, long mask
)
368 struct hmc5843_data
*data
= iio_priv(indio_dev
);
373 case IIO_CHAN_INFO_RAW
:
374 return hmc5843_read_measurement(data
, chan
->scan_index
, val
);
375 case IIO_CHAN_INFO_SCALE
:
376 ret
= regmap_read(data
->regmap
, HMC5843_CONFIG_REG_B
, &rval
);
379 rval
>>= HMC5843_RANGE_GAIN_OFFSET
;
381 *val2
= data
->variant
->regval_to_nanoscale
[rval
];
382 return IIO_VAL_INT_PLUS_NANO
;
383 case IIO_CHAN_INFO_SAMP_FREQ
:
384 ret
= regmap_read(data
->regmap
, HMC5843_CONFIG_REG_A
, &rval
);
387 rval
>>= HMC5843_RATE_OFFSET
;
388 *val
= data
->variant
->regval_to_samp_freq
[rval
][0];
389 *val2
= data
->variant
->regval_to_samp_freq
[rval
][1];
390 return IIO_VAL_INT_PLUS_MICRO
;
395 static int hmc5843_write_raw(struct iio_dev
*indio_dev
,
396 struct iio_chan_spec
const *chan
,
397 int val
, int val2
, long mask
)
399 struct hmc5843_data
*data
= iio_priv(indio_dev
);
403 case IIO_CHAN_INFO_SAMP_FREQ
:
404 rate
= hmc5843_get_samp_freq_index(data
, val
, val2
);
408 return hmc5843_set_samp_freq(data
, rate
);
409 case IIO_CHAN_INFO_SCALE
:
410 range
= hmc5843_get_scale_index(data
, val
, val2
);
414 return hmc5843_set_range_gain(data
, range
);
420 static int hmc5843_write_raw_get_fmt(struct iio_dev
*indio_dev
,
421 struct iio_chan_spec
const *chan
,
425 case IIO_CHAN_INFO_SAMP_FREQ
:
426 return IIO_VAL_INT_PLUS_MICRO
;
427 case IIO_CHAN_INFO_SCALE
:
428 return IIO_VAL_INT_PLUS_NANO
;
434 static irqreturn_t
hmc5843_trigger_handler(int irq
, void *p
)
436 struct iio_poll_func
*pf
= p
;
437 struct iio_dev
*indio_dev
= pf
->indio_dev
;
438 struct hmc5843_data
*data
= iio_priv(indio_dev
);
441 mutex_lock(&data
->lock
);
442 ret
= hmc5843_wait_measurement(data
);
444 mutex_unlock(&data
->lock
);
448 ret
= regmap_bulk_read(data
->regmap
, HMC5843_DATA_OUT_MSB_REGS
,
449 data
->buffer
, 3 * sizeof(__be16
));
451 mutex_unlock(&data
->lock
);
455 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
456 iio_get_time_ns(indio_dev
));
459 iio_trigger_notify_done(indio_dev
->trig
);
464 #define HMC5843_CHANNEL(axis, idx) \
468 .channel2 = IIO_MOD_##axis, \
469 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
470 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
471 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
477 .endianness = IIO_BE, \
479 .ext_info = hmc5843_ext_info, \
482 #define HMC5983_CHANNEL(axis, idx) \
486 .channel2 = IIO_MOD_##axis, \
487 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
488 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
489 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
495 .endianness = IIO_BE, \
497 .ext_info = hmc5983_ext_info, \
500 static const struct iio_chan_spec hmc5843_channels
[] = {
501 HMC5843_CHANNEL(X
, 0),
502 HMC5843_CHANNEL(Y
, 1),
503 HMC5843_CHANNEL(Z
, 2),
504 IIO_CHAN_SOFT_TIMESTAMP(3),
507 /* Beware: Y and Z are exchanged on HMC5883 and 5983 */
508 static const struct iio_chan_spec hmc5883_channels
[] = {
509 HMC5843_CHANNEL(X
, 0),
510 HMC5843_CHANNEL(Z
, 1),
511 HMC5843_CHANNEL(Y
, 2),
512 IIO_CHAN_SOFT_TIMESTAMP(3),
515 static const struct iio_chan_spec hmc5983_channels
[] = {
516 HMC5983_CHANNEL(X
, 0),
517 HMC5983_CHANNEL(Z
, 1),
518 HMC5983_CHANNEL(Y
, 2),
519 IIO_CHAN_SOFT_TIMESTAMP(3),
522 static struct attribute
*hmc5843_attributes
[] = {
523 &iio_dev_attr_scale_available
.dev_attr
.attr
,
524 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
528 static const struct attribute_group hmc5843_group
= {
529 .attrs
= hmc5843_attributes
,
532 static const struct hmc5843_chip_info hmc5843_chip_info_tbl
[] = {
534 .channels
= hmc5843_channels
,
535 .regval_to_samp_freq
= hmc5843_regval_to_samp_freq
,
536 .n_regval_to_samp_freq
=
537 ARRAY_SIZE(hmc5843_regval_to_samp_freq
),
538 .regval_to_nanoscale
= hmc5843_regval_to_nanoscale
,
539 .n_regval_to_nanoscale
=
540 ARRAY_SIZE(hmc5843_regval_to_nanoscale
),
543 .channels
= hmc5883_channels
,
544 .regval_to_samp_freq
= hmc5883_regval_to_samp_freq
,
545 .n_regval_to_samp_freq
=
546 ARRAY_SIZE(hmc5883_regval_to_samp_freq
),
547 .regval_to_nanoscale
= hmc5883_regval_to_nanoscale
,
548 .n_regval_to_nanoscale
=
549 ARRAY_SIZE(hmc5883_regval_to_nanoscale
),
552 .channels
= hmc5883_channels
,
553 .regval_to_samp_freq
= hmc5883_regval_to_samp_freq
,
554 .n_regval_to_samp_freq
=
555 ARRAY_SIZE(hmc5883_regval_to_samp_freq
),
556 .regval_to_nanoscale
= hmc5883l_regval_to_nanoscale
,
557 .n_regval_to_nanoscale
=
558 ARRAY_SIZE(hmc5883l_regval_to_nanoscale
),
561 .channels
= hmc5983_channels
,
562 .regval_to_samp_freq
= hmc5983_regval_to_samp_freq
,
563 .n_regval_to_samp_freq
=
564 ARRAY_SIZE(hmc5983_regval_to_samp_freq
),
565 .regval_to_nanoscale
= hmc5883l_regval_to_nanoscale
,
566 .n_regval_to_nanoscale
=
567 ARRAY_SIZE(hmc5883l_regval_to_nanoscale
),
571 static int hmc5843_init(struct hmc5843_data
*data
)
576 ret
= regmap_bulk_read(data
->regmap
, HMC5843_ID_REG
,
580 if (id
[0] != 'H' || id
[1] != '4' || id
[2] != '3') {
581 dev_err(data
->dev
, "no HMC5843/5883/5883L/5983 sensor\n");
585 ret
= hmc5843_set_meas_conf(data
, HMC5843_MEAS_CONF_NORMAL
);
588 ret
= hmc5843_set_samp_freq(data
, HMC5843_RATE_DEFAULT
);
591 ret
= hmc5843_set_range_gain(data
, HMC5843_RANGE_GAIN_DEFAULT
);
594 return hmc5843_set_mode(data
, HMC5843_MODE_CONVERSION_CONTINUOUS
);
597 static const struct iio_info hmc5843_info
= {
598 .attrs
= &hmc5843_group
,
599 .read_raw
= &hmc5843_read_raw
,
600 .write_raw
= &hmc5843_write_raw
,
601 .write_raw_get_fmt
= &hmc5843_write_raw_get_fmt
,
604 static const unsigned long hmc5843_scan_masks
[] = {0x7, 0};
606 int hmc5843_common_suspend(struct device
*dev
)
608 return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev
)),
611 EXPORT_SYMBOL(hmc5843_common_suspend
);
613 int hmc5843_common_resume(struct device
*dev
)
615 return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev
)),
616 HMC5843_MODE_CONVERSION_CONTINUOUS
);
618 EXPORT_SYMBOL(hmc5843_common_resume
);
620 int hmc5843_common_probe(struct device
*dev
, struct regmap
*regmap
,
621 enum hmc5843_ids id
, const char *name
)
623 struct hmc5843_data
*data
;
624 struct iio_dev
*indio_dev
;
627 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
631 dev_set_drvdata(dev
, indio_dev
);
633 /* default settings at probe */
634 data
= iio_priv(indio_dev
);
636 data
->regmap
= regmap
;
637 data
->variant
= &hmc5843_chip_info_tbl
[id
];
638 mutex_init(&data
->lock
);
640 ret
= iio_read_mount_matrix(dev
, "mount-matrix",
645 indio_dev
->name
= name
;
646 indio_dev
->info
= &hmc5843_info
;
647 indio_dev
->modes
= INDIO_DIRECT_MODE
;
648 indio_dev
->channels
= data
->variant
->channels
;
649 indio_dev
->num_channels
= 4;
650 indio_dev
->available_scan_masks
= hmc5843_scan_masks
;
652 ret
= hmc5843_init(data
);
656 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
657 hmc5843_trigger_handler
, NULL
);
659 goto buffer_setup_err
;
661 ret
= iio_device_register(indio_dev
);
668 iio_triggered_buffer_cleanup(indio_dev
);
670 hmc5843_set_mode(iio_priv(indio_dev
), HMC5843_MODE_SLEEP
);
673 EXPORT_SYMBOL(hmc5843_common_probe
);
675 int hmc5843_common_remove(struct device
*dev
)
677 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
679 iio_device_unregister(indio_dev
);
680 iio_triggered_buffer_cleanup(indio_dev
);
682 /* sleep mode to save power */
683 hmc5843_set_mode(iio_priv(indio_dev
), HMC5843_MODE_SLEEP
);
687 EXPORT_SYMBOL(hmc5843_common_remove
);
689 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>");
690 MODULE_DESCRIPTION("HMC5843/5883/5883L/5983 core driver");
691 MODULE_LICENSE("GPL");