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 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/i2c.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/triggered_buffer.h>
29 #include <linux/delay.h>
31 #define HMC5843_CONFIG_REG_A 0x00
32 #define HMC5843_CONFIG_REG_B 0x01
33 #define HMC5843_MODE_REG 0x02
34 #define HMC5843_DATA_OUT_MSB_REGS 0x03
35 #define HMC5843_STATUS_REG 0x09
44 * Range gain settings in (+-)Ga
45 * Beware: HMC5843 and HMC5883 have different recommended sensor field
46 * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
48 #define HMC5843_RANGE_GAIN_OFFSET 0x05
49 #define HMC5843_RANGE_GAIN_DEFAULT 0x01
50 #define HMC5843_RANGE_GAINS 8
53 #define HMC5843_DATA_READY 0x01
54 #define HMC5843_DATA_OUTPUT_LOCK 0x02
56 /* Mode register configuration */
57 #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
58 #define HMC5843_MODE_CONVERSION_SINGLE 0x01
59 #define HMC5843_MODE_IDLE 0x02
60 #define HMC5843_MODE_SLEEP 0x03
61 #define HMC5843_MODE_MASK 0x03
64 * HMC5843: Minimum data output rate
65 * HMC5883: Typical data output rate
67 #define HMC5843_RATE_OFFSET 0x02
68 #define HMC5843_RATE_DEFAULT 0x04
69 #define HMC5843_RATES 7
71 /* Device measurement configuration */
72 #define HMC5843_MEAS_CONF_NORMAL 0x00
73 #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
74 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
75 #define HMC5843_MEAS_CONF_MASK 0x03
77 /* Scaling factors: 10000000/Gain */
78 static const int hmc5843_regval_to_nanoscale[HMC5843_RANGE_GAINS] = {
79 6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
82 static const int hmc5883_regval_to_nanoscale[HMC5843_RANGE_GAINS] = {
83 7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
86 static const int hmc5883l_regval_to_nanoscale[HMC5843_RANGE_GAINS] = {
87 7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
92 * Value | HMC5843 | HMC5883/HMC5883L
93 * | Data output rate (Hz) | Data output rate (Hz)
98 * 4 | 10 (default) | 15
101 * 7 | Not used | Not used
103 static const int hmc5843_regval_to_samp_freq[7][2] = {
104 {0, 500000}, {1, 0}, {2, 0}, {5, 0}, {10, 0}, {20, 0}, {50, 0}
107 static const int hmc5883_regval_to_samp_freq[7][2] = {
108 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
112 /* Describe chip variants */
113 struct hmc5843_chip_info {
114 const struct iio_chan_spec *channels;
115 const int (*regval_to_samp_freq)[2];
116 const int *regval_to_nanoscale;
119 /* Each client has this additional data */
120 struct hmc5843_data {
121 struct i2c_client *client;
127 const struct hmc5843_chip_info *variant;
128 __be16 buffer[8]; /* 3x 16-bit channels + padding + 64-bit timestamp */
131 /* The lower two bits contain the current conversion mode */
132 static s32 hmc5843_set_mode(struct hmc5843_data *data, u8 operating_mode)
136 mutex_lock(&data->lock);
137 ret = i2c_smbus_write_byte_data(data->client, HMC5843_MODE_REG,
138 operating_mode & HMC5843_MODE_MASK);
140 data->operating_mode = operating_mode;
141 mutex_unlock(&data->lock);
146 static int hmc5843_wait_measurement(struct hmc5843_data *data)
151 while (tries-- > 0) {
152 result = i2c_smbus_read_byte_data(data->client,
156 if (result & HMC5843_DATA_READY)
162 dev_err(&data->client->dev, "data not ready\n");
169 /* Return the measurement value from the specified channel */
170 static int hmc5843_read_measurement(struct hmc5843_data *data,
176 mutex_lock(&data->lock);
177 result = hmc5843_wait_measurement(data);
179 mutex_unlock(&data->lock);
182 result = i2c_smbus_read_i2c_block_data(data->client,
183 HMC5843_DATA_OUT_MSB_REGS, sizeof(values), (u8 *) values);
184 mutex_unlock(&data->lock);
188 *val = sign_extend32(be16_to_cpu(values[idx]), 15);
193 * API for setting the measurement configuration to
194 * Normal, Positive bias and Negative bias
196 * From the datasheet:
197 * 0 - Normal measurement configuration (default): In normal measurement
198 * configuration the device follows normal measurement flow. Pins BP
199 * and BN are left floating and high impedance.
201 * 1 - Positive bias configuration: In positive bias configuration, a
202 * positive current is forced across the resistive load on pins BP
205 * 2 - Negative bias configuration. In negative bias configuration, a
206 * negative current is forced across the resistive load on pins BP
210 static s32 hmc5843_set_meas_conf(struct hmc5843_data *data, u8 meas_conf)
214 mutex_lock(&data->lock);
215 ret = i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
216 (meas_conf & HMC5843_MEAS_CONF_MASK) |
217 (data->rate << HMC5843_RATE_OFFSET));
219 data->meas_conf = meas_conf;
220 mutex_unlock(&data->lock);
225 static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
226 struct device_attribute *attr,
229 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
230 return sprintf(buf, "%d\n", data->meas_conf);
233 static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
234 struct device_attribute *attr,
238 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
239 unsigned long meas_conf = 0;
242 ret = kstrtoul(buf, 10, &meas_conf);
245 if (meas_conf >= HMC5843_MEAS_CONF_MASK)
248 ret = hmc5843_set_meas_conf(data, meas_conf);
250 return (ret < 0) ? ret : count;
253 static IIO_DEVICE_ATTR(meas_conf,
255 hmc5843_show_measurement_configuration,
256 hmc5843_set_measurement_configuration,
259 static ssize_t hmc5843_show_samp_freq_avail(struct device *dev,
260 struct device_attribute *attr, char *buf)
262 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
266 for (i = 0; i < HMC5843_RATES; i++)
267 len += scnprintf(buf + len, PAGE_SIZE - len,
268 "%d.%d ", data->variant->regval_to_samp_freq[i][0],
269 data->variant->regval_to_samp_freq[i][1]);
271 /* replace trailing space by newline */
277 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_samp_freq_avail);
279 static int hmc5843_set_samp_freq(struct hmc5843_data *data, u8 rate)
283 mutex_lock(&data->lock);
284 ret = i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
285 data->meas_conf | (rate << HMC5843_RATE_OFFSET));
288 mutex_unlock(&data->lock);
293 static int hmc5843_get_samp_freq_index(struct hmc5843_data *data,
298 for (i = 0; i < HMC5843_RATES; i++)
299 if (val == data->variant->regval_to_samp_freq[i][0] &&
300 val2 == data->variant->regval_to_samp_freq[i][1])
306 static ssize_t hmc5843_show_scale_avail(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
314 for (i = 0; i < HMC5843_RANGE_GAINS; i++)
315 len += scnprintf(buf + len, PAGE_SIZE - len,
316 "0.%09d ", data->variant->regval_to_nanoscale[i]);
318 /* replace trailing space by newline */
324 static IIO_DEVICE_ATTR(scale_available, S_IRUGO,
325 hmc5843_show_scale_avail, NULL, 0);
327 static int hmc5843_get_scale_index(struct hmc5843_data *data, int val, int val2)
334 for (i = 0; i < HMC5843_RANGE_GAINS; i++)
335 if (val2 == data->variant->regval_to_nanoscale[i])
341 static int hmc5843_read_raw(struct iio_dev *indio_dev,
342 struct iio_chan_spec const *chan,
343 int *val, int *val2, long mask)
345 struct hmc5843_data *data = iio_priv(indio_dev);
348 case IIO_CHAN_INFO_RAW:
349 return hmc5843_read_measurement(data, chan->scan_index, val);
350 case IIO_CHAN_INFO_SCALE:
352 *val2 = data->variant->regval_to_nanoscale[data->range];
353 return IIO_VAL_INT_PLUS_NANO;
354 case IIO_CHAN_INFO_SAMP_FREQ:
355 *val = data->variant->regval_to_samp_freq[data->rate][0];
356 *val2 = data->variant->regval_to_samp_freq[data->rate][1];
357 return IIO_VAL_INT_PLUS_MICRO;
362 static int hmc5843_write_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);
367 int ret, rate, range;
370 case IIO_CHAN_INFO_SAMP_FREQ:
371 rate = hmc5843_get_samp_freq_index(data, val, val2);
375 return hmc5843_set_samp_freq(data, rate);
376 case IIO_CHAN_INFO_SCALE:
377 range = hmc5843_get_scale_index(data, val, val2);
381 range <<= HMC5843_RANGE_GAIN_OFFSET;
382 mutex_lock(&data->lock);
383 ret = i2c_smbus_write_byte_data(data->client,
384 HMC5843_CONFIG_REG_B, range);
387 mutex_unlock(&data->lock);
395 static int hmc5843_write_raw_get_fmt(struct iio_dev *indio_dev,
396 struct iio_chan_spec const *chan, long mask)
399 case IIO_CHAN_INFO_SAMP_FREQ:
400 return IIO_VAL_INT_PLUS_MICRO;
401 case IIO_CHAN_INFO_SCALE:
402 return IIO_VAL_INT_PLUS_NANO;
408 static irqreturn_t hmc5843_trigger_handler(int irq, void *p)
410 struct iio_poll_func *pf = p;
411 struct iio_dev *indio_dev = pf->indio_dev;
412 struct hmc5843_data *data = iio_priv(indio_dev);
415 mutex_lock(&data->lock);
416 ret = hmc5843_wait_measurement(data);
418 mutex_unlock(&data->lock);
422 ret = i2c_smbus_read_i2c_block_data(data->client,
423 HMC5843_DATA_OUT_MSB_REGS, 3 * sizeof(__be16),
424 (u8 *) data->buffer);
425 mutex_unlock(&data->lock);
429 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
433 iio_trigger_notify_done(indio_dev->trig);
438 #define HMC5843_CHANNEL(axis, idx) \
442 .channel2 = IIO_MOD_##axis, \
443 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
444 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
445 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
447 .scan_type = IIO_ST('s', 16, 16, IIO_BE), \
450 static const struct iio_chan_spec hmc5843_channels[] = {
451 HMC5843_CHANNEL(X, 0),
452 HMC5843_CHANNEL(Y, 1),
453 HMC5843_CHANNEL(Z, 2),
454 IIO_CHAN_SOFT_TIMESTAMP(3),
457 /* Beware: Y and Z are exchanged on HMC5883 */
458 static const struct iio_chan_spec hmc5883_channels[] = {
459 HMC5843_CHANNEL(X, 0),
460 HMC5843_CHANNEL(Z, 1),
461 HMC5843_CHANNEL(Y, 2),
462 IIO_CHAN_SOFT_TIMESTAMP(3),
465 static struct attribute *hmc5843_attributes[] = {
466 &iio_dev_attr_meas_conf.dev_attr.attr,
467 &iio_dev_attr_scale_available.dev_attr.attr,
468 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
472 static const struct attribute_group hmc5843_group = {
473 .attrs = hmc5843_attributes,
476 static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
478 .channels = hmc5843_channels,
479 .regval_to_samp_freq = hmc5843_regval_to_samp_freq,
480 .regval_to_nanoscale = hmc5843_regval_to_nanoscale,
483 .channels = hmc5883_channels,
484 .regval_to_samp_freq = hmc5883_regval_to_samp_freq,
485 .regval_to_nanoscale = hmc5883_regval_to_nanoscale,
488 .channels = hmc5883_channels,
489 .regval_to_samp_freq = hmc5883_regval_to_samp_freq,
490 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
494 static void hmc5843_init(struct hmc5843_data *data)
496 hmc5843_set_meas_conf(data, HMC5843_MEAS_CONF_NORMAL);
497 hmc5843_set_samp_freq(data, HMC5843_RATE_DEFAULT);
498 hmc5843_set_mode(data, HMC5843_MODE_CONVERSION_CONTINUOUS);
499 i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_B,
500 HMC5843_RANGE_GAIN_DEFAULT);
503 static const struct iio_info hmc5843_info = {
504 .attrs = &hmc5843_group,
505 .read_raw = &hmc5843_read_raw,
506 .write_raw = &hmc5843_write_raw,
507 .write_raw_get_fmt = &hmc5843_write_raw_get_fmt,
508 .driver_module = THIS_MODULE,
511 static const unsigned long hmc5843_scan_masks[] = {0x7, 0};
513 static int hmc5843_probe(struct i2c_client *client,
514 const struct i2c_device_id *id)
516 struct hmc5843_data *data;
517 struct iio_dev *indio_dev;
520 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
521 if (indio_dev == NULL)
524 /* default settings at probe */
525 data = iio_priv(indio_dev);
526 data->client = client;
527 data->variant = &hmc5843_chip_info_tbl[id->driver_data];
528 mutex_init(&data->lock);
530 i2c_set_clientdata(client, indio_dev);
531 indio_dev->info = &hmc5843_info;
532 indio_dev->name = id->name;
533 indio_dev->dev.parent = &client->dev;
534 indio_dev->modes = INDIO_DIRECT_MODE;
535 indio_dev->channels = data->variant->channels;
536 indio_dev->num_channels = 4;
537 indio_dev->available_scan_masks = hmc5843_scan_masks;
541 ret = iio_triggered_buffer_setup(indio_dev, NULL,
542 hmc5843_trigger_handler, NULL);
546 ret = iio_device_register(indio_dev);
553 iio_triggered_buffer_cleanup(indio_dev);
557 static int hmc5843_remove(struct i2c_client *client)
559 struct iio_dev *indio_dev = i2c_get_clientdata(client);
561 iio_device_unregister(indio_dev);
562 iio_triggered_buffer_cleanup(indio_dev);
564 /* sleep mode to save power */
565 hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP);
570 #ifdef CONFIG_PM_SLEEP
571 static int hmc5843_suspend(struct device *dev)
573 struct hmc5843_data *data = iio_priv(i2c_get_clientdata(
574 to_i2c_client(dev)));
576 return hmc5843_set_mode(data, HMC5843_MODE_SLEEP);
579 static int hmc5843_resume(struct device *dev)
581 struct hmc5843_data *data = iio_priv(i2c_get_clientdata(
582 to_i2c_client(dev)));
584 return hmc5843_set_mode(data, HMC5843_MODE_CONVERSION_CONTINUOUS);
587 static SIMPLE_DEV_PM_OPS(hmc5843_pm_ops, hmc5843_suspend, hmc5843_resume);
588 #define HMC5843_PM_OPS (&hmc5843_pm_ops)
590 #define HMC5843_PM_OPS NULL
593 static const struct i2c_device_id hmc5843_id[] = {
594 { "hmc5843", HMC5843_ID },
595 { "hmc5883", HMC5883_ID },
596 { "hmc5883l", HMC5883L_ID },
599 MODULE_DEVICE_TABLE(i2c, hmc5843_id);
601 static struct i2c_driver hmc5843_driver = {
604 .pm = HMC5843_PM_OPS,
606 .id_table = hmc5843_id,
607 .probe = hmc5843_probe,
608 .remove = hmc5843_remove,
610 module_i2c_driver(hmc5843_driver);
612 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>");
613 MODULE_DESCRIPTION("HMC5843/5883/5883L driver");
614 MODULE_LICENSE("GPL");