1 /* Copyright (C) 2010 Texas Instruments
2 Author: Shubhrajyoti Datta <shubhrajyoti@ti.com>
3 Acknowledgement: Jonathan Cameron <jic23@kernel.org> for valuable inputs.
5 Support for HMC5883 and HMC5883L by Peter Meerwald <pmeerw@pmeerw.net>.
7 Split to multiple files by Josef Gajdusek <atx@atx.name> - 2014
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/regmap.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include <linux/delay.h>
36 * Range gain settings in (+-)Ga
37 * Beware: HMC5843 and HMC5883 have different recommended sensor field
38 * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
40 #define HMC5843_RANGE_GAIN_OFFSET 0x05
41 #define HMC5843_RANGE_GAIN_DEFAULT 0x01
42 #define HMC5843_RANGE_GAIN_MASK 0xe0
45 #define HMC5843_DATA_READY 0x01
46 #define HMC5843_DATA_OUTPUT_LOCK 0x02
48 /* Mode register configuration */
49 #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
50 #define HMC5843_MODE_CONVERSION_SINGLE 0x01
51 #define HMC5843_MODE_IDLE 0x02
52 #define HMC5843_MODE_SLEEP 0x03
53 #define HMC5843_MODE_MASK 0x03
56 * HMC5843: Minimum data output rate
57 * HMC5883: Typical data output rate
59 #define HMC5843_RATE_OFFSET 0x02
60 #define HMC5843_RATE_DEFAULT 0x04
61 #define HMC5843_RATE_MASK 0x1c
63 /* Device measurement configuration */
64 #define HMC5843_MEAS_CONF_NORMAL 0x00
65 #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
66 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
67 #define HMC5843_MEAS_CONF_MASK 0x03
69 /* Scaling factors: 10000000/Gain */
70 static const int hmc5843_regval_to_nanoscale[] = {
71 6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
74 static const int hmc5883_regval_to_nanoscale[] = {
75 7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
78 static const int hmc5883l_regval_to_nanoscale[] = {
79 7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
84 * Value | HMC5843 | HMC5883/HMC5883L
85 * | Data output rate (Hz) | Data output rate (Hz)
90 * 4 | 10 (default) | 15
93 * 7 | Not used | Not used
95 static const int hmc5843_regval_to_samp_freq[][2] = {
96 {0, 500000}, {1, 0}, {2, 0}, {5, 0}, {10, 0}, {20, 0}, {50, 0}
99 static const int hmc5883_regval_to_samp_freq[][2] = {
100 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
104 static const int hmc5983_regval_to_samp_freq[][2] = {
105 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
109 /* Describe chip variants */
110 struct hmc5843_chip_info {
111 const struct iio_chan_spec *channels;
112 const int (*regval_to_samp_freq)[2];
113 const int n_regval_to_samp_freq;
114 const int *regval_to_nanoscale;
115 const int n_regval_to_nanoscale;
118 /* The lower two bits contain the current conversion mode */
119 static s32 hmc5843_set_mode(struct hmc5843_data *data, u8 operating_mode)
123 mutex_lock(&data->lock);
124 ret = regmap_update_bits(data->regmap, HMC5843_MODE_REG,
125 HMC5843_MODE_MASK, operating_mode);
126 mutex_unlock(&data->lock);
131 static int hmc5843_wait_measurement(struct hmc5843_data *data)
137 while (tries-- > 0) {
138 ret = regmap_read(data->regmap, HMC5843_STATUS_REG, &val);
141 if (val & HMC5843_DATA_READY)
147 dev_err(data->dev, "data not ready\n");
154 /* Return the measurement value from the specified channel */
155 static int hmc5843_read_measurement(struct hmc5843_data *data,
161 mutex_lock(&data->lock);
162 ret = hmc5843_wait_measurement(data);
164 mutex_unlock(&data->lock);
167 ret = regmap_bulk_read(data->regmap, HMC5843_DATA_OUT_MSB_REGS,
168 values, sizeof(values));
169 mutex_unlock(&data->lock);
173 *val = sign_extend32(be16_to_cpu(values[idx]), 15);
178 * API for setting the measurement configuration to
179 * Normal, Positive bias and Negative bias
181 * From the datasheet:
182 * 0 - Normal measurement configuration (default): In normal measurement
183 * configuration the device follows normal measurement flow. Pins BP
184 * and BN are left floating and high impedance.
186 * 1 - Positive bias configuration: In positive bias configuration, a
187 * positive current is forced across the resistive load on pins BP
190 * 2 - Negative bias configuration. In negative bias configuration, a
191 * negative current is forced across the resistive load on pins BP
195 static int hmc5843_set_meas_conf(struct hmc5843_data *data, u8 meas_conf)
199 mutex_lock(&data->lock);
200 ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_A,
201 HMC5843_MEAS_CONF_MASK, meas_conf);
202 mutex_unlock(&data->lock);
207 static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
208 struct device_attribute *attr,
211 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
215 ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &val);
218 val &= HMC5843_MEAS_CONF_MASK;
220 return sprintf(buf, "%d\n", val);
223 static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
224 struct device_attribute *attr,
228 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
229 unsigned long meas_conf = 0;
232 ret = kstrtoul(buf, 10, &meas_conf);
235 if (meas_conf >= HMC5843_MEAS_CONF_MASK)
238 ret = hmc5843_set_meas_conf(data, meas_conf);
240 return (ret < 0) ? ret : count;
243 static IIO_DEVICE_ATTR(meas_conf,
245 hmc5843_show_measurement_configuration,
246 hmc5843_set_measurement_configuration,
249 static ssize_t hmc5843_show_samp_freq_avail(struct device *dev,
250 struct device_attribute *attr, char *buf)
252 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
256 for (i = 0; i < data->variant->n_regval_to_samp_freq; i++)
257 len += scnprintf(buf + len, PAGE_SIZE - len,
258 "%d.%d ", data->variant->regval_to_samp_freq[i][0],
259 data->variant->regval_to_samp_freq[i][1]);
261 /* replace trailing space by newline */
267 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_samp_freq_avail);
269 static int hmc5843_set_samp_freq(struct hmc5843_data *data, u8 rate)
273 mutex_lock(&data->lock);
274 ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_A,
275 HMC5843_RATE_MASK, rate << HMC5843_RATE_OFFSET);
276 mutex_unlock(&data->lock);
281 static int hmc5843_get_samp_freq_index(struct hmc5843_data *data,
286 for (i = 0; i < data->variant->n_regval_to_samp_freq; i++)
287 if (val == data->variant->regval_to_samp_freq[i][0] &&
288 val2 == data->variant->regval_to_samp_freq[i][1])
294 static int hmc5843_set_range_gain(struct hmc5843_data *data, u8 range)
298 mutex_lock(&data->lock);
299 ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_B,
300 HMC5843_RANGE_GAIN_MASK,
301 range << HMC5843_RANGE_GAIN_OFFSET);
302 mutex_unlock(&data->lock);
307 static ssize_t hmc5843_show_scale_avail(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
315 for (i = 0; i < data->variant->n_regval_to_nanoscale; i++)
316 len += scnprintf(buf + len, PAGE_SIZE - len,
317 "0.%09d ", data->variant->regval_to_nanoscale[i]);
319 /* replace trailing space by newline */
325 static IIO_DEVICE_ATTR(scale_available, S_IRUGO,
326 hmc5843_show_scale_avail, NULL, 0);
328 static int hmc5843_get_scale_index(struct hmc5843_data *data, int val, int val2)
335 for (i = 0; i < data->variant->n_regval_to_nanoscale; i++)
336 if (val2 == data->variant->regval_to_nanoscale[i])
342 static int hmc5843_read_raw(struct iio_dev *indio_dev,
343 struct iio_chan_spec const *chan,
344 int *val, int *val2, long mask)
346 struct hmc5843_data *data = iio_priv(indio_dev);
351 case IIO_CHAN_INFO_RAW:
352 return hmc5843_read_measurement(data, chan->scan_index, val);
353 case IIO_CHAN_INFO_SCALE:
354 ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_B, &rval);
357 rval >>= HMC5843_RANGE_GAIN_OFFSET;
359 *val2 = data->variant->regval_to_nanoscale[rval];
360 return IIO_VAL_INT_PLUS_NANO;
361 case IIO_CHAN_INFO_SAMP_FREQ:
362 ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &rval);
365 rval >>= HMC5843_RATE_OFFSET;
366 *val = data->variant->regval_to_samp_freq[rval][0];
367 *val2 = data->variant->regval_to_samp_freq[rval][1];
368 return IIO_VAL_INT_PLUS_MICRO;
373 static int hmc5843_write_raw(struct iio_dev *indio_dev,
374 struct iio_chan_spec const *chan,
375 int val, int val2, long mask)
377 struct hmc5843_data *data = iio_priv(indio_dev);
381 case IIO_CHAN_INFO_SAMP_FREQ:
382 rate = hmc5843_get_samp_freq_index(data, val, val2);
386 return hmc5843_set_samp_freq(data, rate);
387 case IIO_CHAN_INFO_SCALE:
388 range = hmc5843_get_scale_index(data, val, val2);
392 return hmc5843_set_range_gain(data, range);
398 static int hmc5843_write_raw_get_fmt(struct iio_dev *indio_dev,
399 struct iio_chan_spec const *chan, long mask)
402 case IIO_CHAN_INFO_SAMP_FREQ:
403 return IIO_VAL_INT_PLUS_MICRO;
404 case IIO_CHAN_INFO_SCALE:
405 return IIO_VAL_INT_PLUS_NANO;
411 static irqreturn_t hmc5843_trigger_handler(int irq, void *p)
413 struct iio_poll_func *pf = p;
414 struct iio_dev *indio_dev = pf->indio_dev;
415 struct hmc5843_data *data = iio_priv(indio_dev);
418 mutex_lock(&data->lock);
419 ret = hmc5843_wait_measurement(data);
421 mutex_unlock(&data->lock);
425 ret = regmap_bulk_read(data->regmap, HMC5843_DATA_OUT_MSB_REGS,
426 data->buffer, 3 * sizeof(__be16));
428 mutex_unlock(&data->lock);
432 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
436 iio_trigger_notify_done(indio_dev->trig);
441 #define HMC5843_CHANNEL(axis, idx) \
445 .channel2 = IIO_MOD_##axis, \
446 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
447 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
448 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
454 .endianness = IIO_BE, \
458 static const struct iio_chan_spec hmc5843_channels[] = {
459 HMC5843_CHANNEL(X, 0),
460 HMC5843_CHANNEL(Y, 1),
461 HMC5843_CHANNEL(Z, 2),
462 IIO_CHAN_SOFT_TIMESTAMP(3),
465 /* Beware: Y and Z are exchanged on HMC5883 and 5983 */
466 static const struct iio_chan_spec hmc5883_channels[] = {
467 HMC5843_CHANNEL(X, 0),
468 HMC5843_CHANNEL(Z, 1),
469 HMC5843_CHANNEL(Y, 2),
470 IIO_CHAN_SOFT_TIMESTAMP(3),
473 static struct attribute *hmc5843_attributes[] = {
474 &iio_dev_attr_meas_conf.dev_attr.attr,
475 &iio_dev_attr_scale_available.dev_attr.attr,
476 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
480 static const struct attribute_group hmc5843_group = {
481 .attrs = hmc5843_attributes,
484 static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
486 .channels = hmc5843_channels,
487 .regval_to_samp_freq = hmc5843_regval_to_samp_freq,
488 .n_regval_to_samp_freq =
489 ARRAY_SIZE(hmc5843_regval_to_samp_freq),
490 .regval_to_nanoscale = hmc5843_regval_to_nanoscale,
491 .n_regval_to_nanoscale =
492 ARRAY_SIZE(hmc5843_regval_to_nanoscale),
495 .channels = hmc5883_channels,
496 .regval_to_samp_freq = hmc5883_regval_to_samp_freq,
497 .n_regval_to_samp_freq =
498 ARRAY_SIZE(hmc5883_regval_to_samp_freq),
499 .regval_to_nanoscale = hmc5883_regval_to_nanoscale,
500 .n_regval_to_nanoscale =
501 ARRAY_SIZE(hmc5883_regval_to_nanoscale),
504 .channels = hmc5883_channels,
505 .regval_to_samp_freq = hmc5883_regval_to_samp_freq,
506 .n_regval_to_samp_freq =
507 ARRAY_SIZE(hmc5883_regval_to_samp_freq),
508 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
509 .n_regval_to_nanoscale =
510 ARRAY_SIZE(hmc5883l_regval_to_nanoscale),
513 .channels = hmc5883_channels,
514 .regval_to_samp_freq = hmc5983_regval_to_samp_freq,
515 .n_regval_to_samp_freq =
516 ARRAY_SIZE(hmc5983_regval_to_samp_freq),
517 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
518 .n_regval_to_nanoscale =
519 ARRAY_SIZE(hmc5883l_regval_to_nanoscale),
523 static int hmc5843_init(struct hmc5843_data *data)
528 ret = regmap_bulk_read(data->regmap, HMC5843_ID_REG,
532 if (id[0] != 'H' || id[1] != '4' || id[2] != '3') {
533 dev_err(data->dev, "no HMC5843/5883/5883L/5983 sensor\n");
537 ret = hmc5843_set_meas_conf(data, HMC5843_MEAS_CONF_NORMAL);
540 ret = hmc5843_set_samp_freq(data, HMC5843_RATE_DEFAULT);
543 ret = hmc5843_set_range_gain(data, HMC5843_RANGE_GAIN_DEFAULT);
546 return hmc5843_set_mode(data, HMC5843_MODE_CONVERSION_CONTINUOUS);
549 static const struct iio_info hmc5843_info = {
550 .attrs = &hmc5843_group,
551 .read_raw = &hmc5843_read_raw,
552 .write_raw = &hmc5843_write_raw,
553 .write_raw_get_fmt = &hmc5843_write_raw_get_fmt,
554 .driver_module = THIS_MODULE,
557 static const unsigned long hmc5843_scan_masks[] = {0x7, 0};
560 int hmc5843_common_suspend(struct device *dev)
562 return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev)),
563 HMC5843_MODE_CONVERSION_CONTINUOUS);
565 EXPORT_SYMBOL(hmc5843_common_suspend);
567 int hmc5843_common_resume(struct device *dev)
569 return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev)),
572 EXPORT_SYMBOL(hmc5843_common_resume);
574 int hmc5843_common_probe(struct device *dev, struct regmap *regmap,
577 struct hmc5843_data *data;
578 struct iio_dev *indio_dev;
581 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
582 if (indio_dev == NULL)
585 dev_set_drvdata(dev, indio_dev);
587 /* default settings at probe */
588 data = iio_priv(indio_dev);
590 data->regmap = regmap;
591 data->variant = &hmc5843_chip_info_tbl[id];
592 mutex_init(&data->lock);
594 indio_dev->dev.parent = dev;
595 indio_dev->info = &hmc5843_info;
596 indio_dev->modes = INDIO_DIRECT_MODE;
597 indio_dev->channels = data->variant->channels;
598 indio_dev->num_channels = 4;
599 indio_dev->available_scan_masks = hmc5843_scan_masks;
601 ret = hmc5843_init(data);
605 ret = iio_triggered_buffer_setup(indio_dev, NULL,
606 hmc5843_trigger_handler, NULL);
610 ret = iio_device_register(indio_dev);
617 iio_triggered_buffer_cleanup(indio_dev);
620 EXPORT_SYMBOL(hmc5843_common_probe);
622 int hmc5843_common_remove(struct device *dev)
624 struct iio_dev *indio_dev = dev_get_drvdata(dev);
626 iio_device_unregister(indio_dev);
627 iio_triggered_buffer_cleanup(indio_dev);
629 /* sleep mode to save power */
630 hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP);
634 EXPORT_SYMBOL(hmc5843_common_remove);
636 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>");
637 MODULE_DESCRIPTION("HMC5843/5883/5883L/5983 core driver");
638 MODULE_LICENSE("GPL");