2 * Device driver for the the HMC5843 multi-chip module designed
3 * for low field magnetic sensing.
5 * Copyright (C) 2010 Texas Instruments
7 * Author: Shubhrajyoti Datta <shubhrajyoti@ti.com>
8 * Acknowledgment: Jonathan Cameron <jic23@kernel.org> for valuable inputs.
9 * Support for HMC5883 and HMC5883L by Peter Meerwald <pmeerw@pmeerw.net>.
10 * Split to multiple files by Josef Gajdusek <atx@atx.name> - 2014
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 #include <linux/module.h>
24 #include <linux/regmap.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/trigger_consumer.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/delay.h>
35 * Range gain settings in (+-)Ga
36 * Beware: HMC5843 and HMC5883 have different recommended sensor field
37 * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
39 #define HMC5843_RANGE_GAIN_OFFSET 0x05
40 #define HMC5843_RANGE_GAIN_DEFAULT 0x01
41 #define HMC5843_RANGE_GAIN_MASK 0xe0
44 #define HMC5843_DATA_READY 0x01
45 #define HMC5843_DATA_OUTPUT_LOCK 0x02
47 /* Mode register configuration */
48 #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
49 #define HMC5843_MODE_CONVERSION_SINGLE 0x01
50 #define HMC5843_MODE_IDLE 0x02
51 #define HMC5843_MODE_SLEEP 0x03
52 #define HMC5843_MODE_MASK 0x03
55 * HMC5843: Minimum data output rate
56 * HMC5883: Typical data output rate
58 #define HMC5843_RATE_OFFSET 0x02
59 #define HMC5843_RATE_DEFAULT 0x04
60 #define HMC5843_RATE_MASK 0x1c
62 /* Device measurement configuration */
63 #define HMC5843_MEAS_CONF_NORMAL 0x00
64 #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
65 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
66 #define HMC5843_MEAS_CONF_MASK 0x03
69 * API for setting the measurement configuration to
70 * Normal, Positive bias and Negative bias
73 * 0 - Normal measurement configuration (default): In normal measurement
74 * configuration the device follows normal measurement flow. Pins BP
75 * and BN are left floating and high impedance.
77 * 1 - Positive bias configuration: In positive bias configuration, a
78 * positive current is forced across the resistive load on pins BP
81 * 2 - Negative bias configuration. In negative bias configuration, a
82 * negative current is forced across the resistive load on pins BP
85 * 3 - Only available on HMC5983. Magnetic sensor is disabled.
86 * Temperature sensor is enabled.
89 static const char *const hmc5843_meas_conf_modes
[] = {"normal", "positivebias",
92 static const char *const hmc5983_meas_conf_modes
[] = {"normal", "positivebias",
95 /* Scaling factors: 10000000/Gain */
96 static const int hmc5843_regval_to_nanoscale
[] = {
97 6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
100 static const int hmc5883_regval_to_nanoscale
[] = {
101 7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
104 static const int hmc5883l_regval_to_nanoscale
[] = {
105 7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
109 * From the datasheet:
110 * Value | HMC5843 | HMC5883/HMC5883L
111 * | Data output rate (Hz) | Data output rate (Hz)
116 * 4 | 10 (default) | 15
119 * 7 | Not used | Not used
121 static const int hmc5843_regval_to_samp_freq
[][2] = {
122 {0, 500000}, {1, 0}, {2, 0}, {5, 0}, {10, 0}, {20, 0}, {50, 0}
125 static const int hmc5883_regval_to_samp_freq
[][2] = {
126 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
130 static const int hmc5983_regval_to_samp_freq
[][2] = {
131 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
135 /* Describe chip variants */
136 struct hmc5843_chip_info
{
137 const struct iio_chan_spec
*channels
;
138 const int (*regval_to_samp_freq
)[2];
139 const int n_regval_to_samp_freq
;
140 const int *regval_to_nanoscale
;
141 const int n_regval_to_nanoscale
;
144 /* The lower two bits contain the current conversion mode */
145 static s32
hmc5843_set_mode(struct hmc5843_data
*data
, u8 operating_mode
)
149 mutex_lock(&data
->lock
);
150 ret
= regmap_update_bits(data
->regmap
, HMC5843_MODE_REG
,
151 HMC5843_MODE_MASK
, operating_mode
);
152 mutex_unlock(&data
->lock
);
157 static int hmc5843_wait_measurement(struct hmc5843_data
*data
)
163 while (tries
-- > 0) {
164 ret
= regmap_read(data
->regmap
, HMC5843_STATUS_REG
, &val
);
167 if (val
& HMC5843_DATA_READY
)
173 dev_err(data
->dev
, "data not ready\n");
180 /* Return the measurement value from the specified channel */
181 static int hmc5843_read_measurement(struct hmc5843_data
*data
,
187 mutex_lock(&data
->lock
);
188 ret
= hmc5843_wait_measurement(data
);
190 mutex_unlock(&data
->lock
);
193 ret
= regmap_bulk_read(data
->regmap
, HMC5843_DATA_OUT_MSB_REGS
,
194 values
, sizeof(values
));
195 mutex_unlock(&data
->lock
);
199 *val
= sign_extend32(be16_to_cpu(values
[idx
]), 15);
203 static int hmc5843_set_meas_conf(struct hmc5843_data
*data
, u8 meas_conf
)
207 mutex_lock(&data
->lock
);
208 ret
= regmap_update_bits(data
->regmap
, HMC5843_CONFIG_REG_A
,
209 HMC5843_MEAS_CONF_MASK
, meas_conf
);
210 mutex_unlock(&data
->lock
);
216 int hmc5843_show_measurement_configuration(struct iio_dev
*indio_dev
,
217 const struct iio_chan_spec
*chan
)
219 struct hmc5843_data
*data
= iio_priv(indio_dev
);
223 ret
= regmap_read(data
->regmap
, HMC5843_CONFIG_REG_A
, &val
);
227 return val
& HMC5843_MEAS_CONF_MASK
;
231 int hmc5843_set_measurement_configuration(struct iio_dev
*indio_dev
,
232 const struct iio_chan_spec
*chan
,
233 unsigned int meas_conf
)
235 struct hmc5843_data
*data
= iio_priv(indio_dev
);
237 return hmc5843_set_meas_conf(data
, meas_conf
);
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", true, &hmc5843_meas_conf_enum
),
249 IIO_ENUM_AVAILABLE("meas_conf", &hmc5843_meas_conf_enum
),
253 static const struct iio_enum hmc5983_meas_conf_enum
= {
254 .items
= hmc5983_meas_conf_modes
,
255 .num_items
= ARRAY_SIZE(hmc5983_meas_conf_modes
),
256 .get
= hmc5843_show_measurement_configuration
,
257 .set
= hmc5843_set_measurement_configuration
,
260 static const struct iio_chan_spec_ext_info hmc5983_ext_info
[] = {
261 IIO_ENUM("meas_conf", true, &hmc5983_meas_conf_enum
),
262 IIO_ENUM_AVAILABLE("meas_conf", &hmc5983_meas_conf_enum
),
267 ssize_t
hmc5843_show_samp_freq_avail(struct device
*dev
,
268 struct device_attribute
*attr
, char *buf
)
270 struct hmc5843_data
*data
= iio_priv(dev_to_iio_dev(dev
));
274 for (i
= 0; i
< data
->variant
->n_regval_to_samp_freq
; i
++)
275 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
276 "%d.%d ", data
->variant
->regval_to_samp_freq
[i
][0],
277 data
->variant
->regval_to_samp_freq
[i
][1]);
279 /* replace trailing space by newline */
285 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_samp_freq_avail
);
287 static int hmc5843_set_samp_freq(struct hmc5843_data
*data
, u8 rate
)
291 mutex_lock(&data
->lock
);
292 ret
= regmap_update_bits(data
->regmap
, HMC5843_CONFIG_REG_A
,
294 rate
<< HMC5843_RATE_OFFSET
);
295 mutex_unlock(&data
->lock
);
300 static int hmc5843_get_samp_freq_index(struct hmc5843_data
*data
,
305 for (i
= 0; i
< data
->variant
->n_regval_to_samp_freq
; i
++)
306 if (val
== data
->variant
->regval_to_samp_freq
[i
][0] &&
307 val2
== data
->variant
->regval_to_samp_freq
[i
][1])
313 static int hmc5843_set_range_gain(struct hmc5843_data
*data
, u8 range
)
317 mutex_lock(&data
->lock
);
318 ret
= regmap_update_bits(data
->regmap
, HMC5843_CONFIG_REG_B
,
319 HMC5843_RANGE_GAIN_MASK
,
320 range
<< HMC5843_RANGE_GAIN_OFFSET
);
321 mutex_unlock(&data
->lock
);
326 static ssize_t
hmc5843_show_scale_avail(struct device
*dev
,
327 struct device_attribute
*attr
,
330 struct hmc5843_data
*data
= iio_priv(dev_to_iio_dev(dev
));
335 for (i
= 0; i
< data
->variant
->n_regval_to_nanoscale
; i
++)
336 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
337 "0.%09d ", data
->variant
->regval_to_nanoscale
[i
]);
339 /* replace trailing space by newline */
345 static IIO_DEVICE_ATTR(scale_available
, S_IRUGO
,
346 hmc5843_show_scale_avail
, NULL
, 0);
348 static int hmc5843_get_scale_index(struct hmc5843_data
*data
, int val
, int val2
)
355 for (i
= 0; i
< data
->variant
->n_regval_to_nanoscale
; i
++)
356 if (val2
== data
->variant
->regval_to_nanoscale
[i
])
362 static int hmc5843_read_raw(struct iio_dev
*indio_dev
,
363 struct iio_chan_spec
const *chan
,
364 int *val
, int *val2
, long mask
)
366 struct hmc5843_data
*data
= iio_priv(indio_dev
);
371 case IIO_CHAN_INFO_RAW
:
372 return hmc5843_read_measurement(data
, chan
->scan_index
, val
);
373 case IIO_CHAN_INFO_SCALE
:
374 ret
= regmap_read(data
->regmap
, HMC5843_CONFIG_REG_B
, &rval
);
377 rval
>>= HMC5843_RANGE_GAIN_OFFSET
;
379 *val2
= data
->variant
->regval_to_nanoscale
[rval
];
380 return IIO_VAL_INT_PLUS_NANO
;
381 case IIO_CHAN_INFO_SAMP_FREQ
:
382 ret
= regmap_read(data
->regmap
, HMC5843_CONFIG_REG_A
, &rval
);
385 rval
>>= HMC5843_RATE_OFFSET
;
386 *val
= data
->variant
->regval_to_samp_freq
[rval
][0];
387 *val2
= data
->variant
->regval_to_samp_freq
[rval
][1];
388 return IIO_VAL_INT_PLUS_MICRO
;
393 static int hmc5843_write_raw(struct iio_dev
*indio_dev
,
394 struct iio_chan_spec
const *chan
,
395 int val
, int val2
, long mask
)
397 struct hmc5843_data
*data
= iio_priv(indio_dev
);
401 case IIO_CHAN_INFO_SAMP_FREQ
:
402 rate
= hmc5843_get_samp_freq_index(data
, val
, val2
);
406 return hmc5843_set_samp_freq(data
, rate
);
407 case IIO_CHAN_INFO_SCALE
:
408 range
= hmc5843_get_scale_index(data
, val
, val2
);
412 return hmc5843_set_range_gain(data
, range
);
418 static int hmc5843_write_raw_get_fmt(struct iio_dev
*indio_dev
,
419 struct iio_chan_spec
const *chan
,
423 case IIO_CHAN_INFO_SAMP_FREQ
:
424 return IIO_VAL_INT_PLUS_MICRO
;
425 case IIO_CHAN_INFO_SCALE
:
426 return IIO_VAL_INT_PLUS_NANO
;
432 static irqreturn_t
hmc5843_trigger_handler(int irq
, void *p
)
434 struct iio_poll_func
*pf
= p
;
435 struct iio_dev
*indio_dev
= pf
->indio_dev
;
436 struct hmc5843_data
*data
= iio_priv(indio_dev
);
439 mutex_lock(&data
->lock
);
440 ret
= hmc5843_wait_measurement(data
);
442 mutex_unlock(&data
->lock
);
446 ret
= regmap_bulk_read(data
->regmap
, HMC5843_DATA_OUT_MSB_REGS
,
447 data
->buffer
, 3 * sizeof(__be16
));
449 mutex_unlock(&data
->lock
);
453 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
454 iio_get_time_ns(indio_dev
));
457 iio_trigger_notify_done(indio_dev
->trig
);
462 #define HMC5843_CHANNEL(axis, idx) \
466 .channel2 = IIO_MOD_##axis, \
467 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
468 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
469 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
475 .endianness = IIO_BE, \
477 .ext_info = hmc5843_ext_info, \
480 #define HMC5983_CHANNEL(axis, idx) \
484 .channel2 = IIO_MOD_##axis, \
485 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
486 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
487 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
493 .endianness = IIO_BE, \
495 .ext_info = hmc5983_ext_info, \
498 static const struct iio_chan_spec hmc5843_channels
[] = {
499 HMC5843_CHANNEL(X
, 0),
500 HMC5843_CHANNEL(Y
, 1),
501 HMC5843_CHANNEL(Z
, 2),
502 IIO_CHAN_SOFT_TIMESTAMP(3),
505 /* Beware: Y and Z are exchanged on HMC5883 and 5983 */
506 static const struct iio_chan_spec hmc5883_channels
[] = {
507 HMC5843_CHANNEL(X
, 0),
508 HMC5843_CHANNEL(Z
, 1),
509 HMC5843_CHANNEL(Y
, 2),
510 IIO_CHAN_SOFT_TIMESTAMP(3),
513 static const struct iio_chan_spec hmc5983_channels
[] = {
514 HMC5983_CHANNEL(X
, 0),
515 HMC5983_CHANNEL(Z
, 1),
516 HMC5983_CHANNEL(Y
, 2),
517 IIO_CHAN_SOFT_TIMESTAMP(3),
520 static struct attribute
*hmc5843_attributes
[] = {
521 &iio_dev_attr_scale_available
.dev_attr
.attr
,
522 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
526 static const struct attribute_group hmc5843_group
= {
527 .attrs
= hmc5843_attributes
,
530 static const struct hmc5843_chip_info hmc5843_chip_info_tbl
[] = {
532 .channels
= hmc5843_channels
,
533 .regval_to_samp_freq
= hmc5843_regval_to_samp_freq
,
534 .n_regval_to_samp_freq
=
535 ARRAY_SIZE(hmc5843_regval_to_samp_freq
),
536 .regval_to_nanoscale
= hmc5843_regval_to_nanoscale
,
537 .n_regval_to_nanoscale
=
538 ARRAY_SIZE(hmc5843_regval_to_nanoscale
),
541 .channels
= hmc5883_channels
,
542 .regval_to_samp_freq
= hmc5883_regval_to_samp_freq
,
543 .n_regval_to_samp_freq
=
544 ARRAY_SIZE(hmc5883_regval_to_samp_freq
),
545 .regval_to_nanoscale
= hmc5883_regval_to_nanoscale
,
546 .n_regval_to_nanoscale
=
547 ARRAY_SIZE(hmc5883_regval_to_nanoscale
),
550 .channels
= hmc5883_channels
,
551 .regval_to_samp_freq
= hmc5883_regval_to_samp_freq
,
552 .n_regval_to_samp_freq
=
553 ARRAY_SIZE(hmc5883_regval_to_samp_freq
),
554 .regval_to_nanoscale
= hmc5883l_regval_to_nanoscale
,
555 .n_regval_to_nanoscale
=
556 ARRAY_SIZE(hmc5883l_regval_to_nanoscale
),
559 .channels
= hmc5983_channels
,
560 .regval_to_samp_freq
= hmc5983_regval_to_samp_freq
,
561 .n_regval_to_samp_freq
=
562 ARRAY_SIZE(hmc5983_regval_to_samp_freq
),
563 .regval_to_nanoscale
= hmc5883l_regval_to_nanoscale
,
564 .n_regval_to_nanoscale
=
565 ARRAY_SIZE(hmc5883l_regval_to_nanoscale
),
569 static int hmc5843_init(struct hmc5843_data
*data
)
574 ret
= regmap_bulk_read(data
->regmap
, HMC5843_ID_REG
,
578 if (id
[0] != 'H' || id
[1] != '4' || id
[2] != '3') {
579 dev_err(data
->dev
, "no HMC5843/5883/5883L/5983 sensor\n");
583 ret
= hmc5843_set_meas_conf(data
, HMC5843_MEAS_CONF_NORMAL
);
586 ret
= hmc5843_set_samp_freq(data
, HMC5843_RATE_DEFAULT
);
589 ret
= hmc5843_set_range_gain(data
, HMC5843_RANGE_GAIN_DEFAULT
);
592 return hmc5843_set_mode(data
, HMC5843_MODE_CONVERSION_CONTINUOUS
);
595 static const struct iio_info hmc5843_info
= {
596 .attrs
= &hmc5843_group
,
597 .read_raw
= &hmc5843_read_raw
,
598 .write_raw
= &hmc5843_write_raw
,
599 .write_raw_get_fmt
= &hmc5843_write_raw_get_fmt
,
602 static const unsigned long hmc5843_scan_masks
[] = {0x7, 0};
604 int hmc5843_common_suspend(struct device
*dev
)
606 return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev
)),
609 EXPORT_SYMBOL(hmc5843_common_suspend
);
611 int hmc5843_common_resume(struct device
*dev
)
613 return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev
)),
614 HMC5843_MODE_CONVERSION_CONTINUOUS
);
616 EXPORT_SYMBOL(hmc5843_common_resume
);
618 int hmc5843_common_probe(struct device
*dev
, struct regmap
*regmap
,
619 enum hmc5843_ids id
, const char *name
)
621 struct hmc5843_data
*data
;
622 struct iio_dev
*indio_dev
;
625 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
629 dev_set_drvdata(dev
, indio_dev
);
631 /* default settings at probe */
632 data
= iio_priv(indio_dev
);
634 data
->regmap
= regmap
;
635 data
->variant
= &hmc5843_chip_info_tbl
[id
];
636 mutex_init(&data
->lock
);
638 indio_dev
->dev
.parent
= dev
;
639 indio_dev
->name
= name
;
640 indio_dev
->info
= &hmc5843_info
;
641 indio_dev
->modes
= INDIO_DIRECT_MODE
;
642 indio_dev
->channels
= data
->variant
->channels
;
643 indio_dev
->num_channels
= 4;
644 indio_dev
->available_scan_masks
= hmc5843_scan_masks
;
646 ret
= hmc5843_init(data
);
650 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
651 hmc5843_trigger_handler
, NULL
);
653 goto buffer_setup_err
;
655 ret
= iio_device_register(indio_dev
);
662 iio_triggered_buffer_cleanup(indio_dev
);
664 hmc5843_set_mode(iio_priv(indio_dev
), HMC5843_MODE_SLEEP
);
667 EXPORT_SYMBOL(hmc5843_common_probe
);
669 int hmc5843_common_remove(struct device
*dev
)
671 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
673 iio_device_unregister(indio_dev
);
674 iio_triggered_buffer_cleanup(indio_dev
);
676 /* sleep mode to save power */
677 hmc5843_set_mode(iio_priv(indio_dev
), HMC5843_MODE_SLEEP
);
681 EXPORT_SYMBOL(hmc5843_common_remove
);
683 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>");
684 MODULE_DESCRIPTION("HMC5843/5883/5883L/5983 core driver");
685 MODULE_LICENSE("GPL");