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
36 #define HMC5843_ID_REG 0x0a
45 * Range gain settings in (+-)Ga
46 * Beware: HMC5843 and HMC5883 have different recommended sensor field
47 * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
49 #define HMC5843_RANGE_GAIN_OFFSET 0x05
50 #define HMC5843_RANGE_GAIN_DEFAULT 0x01
51 #define HMC5843_RANGE_GAINS 8
54 #define HMC5843_DATA_READY 0x01
55 #define HMC5843_DATA_OUTPUT_LOCK 0x02
57 /* Mode register configuration */
58 #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
59 #define HMC5843_MODE_CONVERSION_SINGLE 0x01
60 #define HMC5843_MODE_IDLE 0x02
61 #define HMC5843_MODE_SLEEP 0x03
62 #define HMC5843_MODE_MASK 0x03
65 * HMC5843: Minimum data output rate
66 * HMC5883: Typical data output rate
68 #define HMC5843_RATE_OFFSET 0x02
69 #define HMC5843_RATE_DEFAULT 0x04
70 #define HMC5843_RATES 7
72 /* Device measurement configuration */
73 #define HMC5843_MEAS_CONF_NORMAL 0x00
74 #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
75 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
76 #define HMC5843_MEAS_CONF_MASK 0x03
78 /* Scaling factors: 10000000/Gain */
79 static const int hmc5843_regval_to_nanoscale[HMC5843_RANGE_GAINS] = {
80 6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
83 static const int hmc5883_regval_to_nanoscale[HMC5843_RANGE_GAINS] = {
84 7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
87 static const int hmc5883l_regval_to_nanoscale[HMC5843_RANGE_GAINS] = {
88 7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
93 * Value | HMC5843 | HMC5883/HMC5883L
94 * | Data output rate (Hz) | Data output rate (Hz)
99 * 4 | 10 (default) | 15
102 * 7 | Not used | Not used
104 static const int hmc5843_regval_to_samp_freq[7][2] = {
105 {0, 500000}, {1, 0}, {2, 0}, {5, 0}, {10, 0}, {20, 0}, {50, 0}
108 static const int hmc5883_regval_to_samp_freq[7][2] = {
109 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
113 /* Describe chip variants */
114 struct hmc5843_chip_info {
115 const struct iio_chan_spec *channels;
116 const int (*regval_to_samp_freq)[2];
117 const int *regval_to_nanoscale;
120 /* Each client has this additional data */
121 struct hmc5843_data {
122 struct i2c_client *client;
128 const struct hmc5843_chip_info *variant;
129 __be16 buffer[8]; /* 3x 16-bit channels + padding + 64-bit timestamp */
132 /* The lower two bits contain the current conversion mode */
133 static s32 hmc5843_set_mode(struct hmc5843_data *data, u8 operating_mode)
137 mutex_lock(&data->lock);
138 ret = i2c_smbus_write_byte_data(data->client, HMC5843_MODE_REG,
139 operating_mode & HMC5843_MODE_MASK);
141 data->operating_mode = operating_mode;
142 mutex_unlock(&data->lock);
147 static int hmc5843_wait_measurement(struct hmc5843_data *data)
152 while (tries-- > 0) {
153 result = i2c_smbus_read_byte_data(data->client,
157 if (result & HMC5843_DATA_READY)
163 dev_err(&data->client->dev, "data not ready\n");
170 /* Return the measurement value from the specified channel */
171 static int hmc5843_read_measurement(struct hmc5843_data *data,
177 mutex_lock(&data->lock);
178 result = hmc5843_wait_measurement(data);
180 mutex_unlock(&data->lock);
183 result = i2c_smbus_read_i2c_block_data(data->client,
184 HMC5843_DATA_OUT_MSB_REGS, sizeof(values), (u8 *) values);
185 mutex_unlock(&data->lock);
189 *val = sign_extend32(be16_to_cpu(values[idx]), 15);
194 * API for setting the measurement configuration to
195 * Normal, Positive bias and Negative bias
197 * From the datasheet:
198 * 0 - Normal measurement configuration (default): In normal measurement
199 * configuration the device follows normal measurement flow. Pins BP
200 * and BN are left floating and high impedance.
202 * 1 - Positive bias configuration: In positive bias configuration, a
203 * positive current is forced across the resistive load on pins BP
206 * 2 - Negative bias configuration. In negative bias configuration, a
207 * negative current is forced across the resistive load on pins BP
211 static s32 hmc5843_set_meas_conf(struct hmc5843_data *data, u8 meas_conf)
215 mutex_lock(&data->lock);
216 ret = i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
217 (meas_conf & HMC5843_MEAS_CONF_MASK) |
218 (data->rate << HMC5843_RATE_OFFSET));
220 data->meas_conf = meas_conf;
221 mutex_unlock(&data->lock);
226 static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
227 struct device_attribute *attr,
230 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
231 return sprintf(buf, "%d\n", data->meas_conf);
234 static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
235 struct device_attribute *attr,
239 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
240 unsigned long meas_conf = 0;
243 ret = kstrtoul(buf, 10, &meas_conf);
246 if (meas_conf >= HMC5843_MEAS_CONF_MASK)
249 ret = hmc5843_set_meas_conf(data, meas_conf);
251 return (ret < 0) ? ret : count;
254 static IIO_DEVICE_ATTR(meas_conf,
256 hmc5843_show_measurement_configuration,
257 hmc5843_set_measurement_configuration,
260 static ssize_t hmc5843_show_samp_freq_avail(struct device *dev,
261 struct device_attribute *attr, char *buf)
263 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
267 for (i = 0; i < HMC5843_RATES; i++)
268 len += scnprintf(buf + len, PAGE_SIZE - len,
269 "%d.%d ", data->variant->regval_to_samp_freq[i][0],
270 data->variant->regval_to_samp_freq[i][1]);
272 /* replace trailing space by newline */
278 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_samp_freq_avail);
280 static int hmc5843_set_samp_freq(struct hmc5843_data *data, u8 rate)
284 mutex_lock(&data->lock);
285 ret = i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
286 data->meas_conf | (rate << HMC5843_RATE_OFFSET));
289 mutex_unlock(&data->lock);
294 static int hmc5843_get_samp_freq_index(struct hmc5843_data *data,
299 for (i = 0; i < HMC5843_RATES; i++)
300 if (val == data->variant->regval_to_samp_freq[i][0] &&
301 val2 == data->variant->regval_to_samp_freq[i][1])
307 static int hmc5843_set_range_gain(struct hmc5843_data *data, u8 range)
311 mutex_lock(&data->lock);
312 ret = i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_B,
313 range << HMC5843_RANGE_GAIN_OFFSET);
316 mutex_unlock(&data->lock);
321 static ssize_t hmc5843_show_scale_avail(struct device *dev,
322 struct device_attribute *attr, char *buf)
324 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
329 for (i = 0; i < HMC5843_RANGE_GAINS; i++)
330 len += scnprintf(buf + len, PAGE_SIZE - len,
331 "0.%09d ", data->variant->regval_to_nanoscale[i]);
333 /* replace trailing space by newline */
339 static IIO_DEVICE_ATTR(scale_available, S_IRUGO,
340 hmc5843_show_scale_avail, NULL, 0);
342 static int hmc5843_get_scale_index(struct hmc5843_data *data, int val, int val2)
349 for (i = 0; i < HMC5843_RANGE_GAINS; i++)
350 if (val2 == data->variant->regval_to_nanoscale[i])
356 static int hmc5843_read_raw(struct iio_dev *indio_dev,
357 struct iio_chan_spec const *chan,
358 int *val, int *val2, long mask)
360 struct hmc5843_data *data = iio_priv(indio_dev);
363 case IIO_CHAN_INFO_RAW:
364 return hmc5843_read_measurement(data, chan->scan_index, val);
365 case IIO_CHAN_INFO_SCALE:
367 *val2 = data->variant->regval_to_nanoscale[data->range];
368 return IIO_VAL_INT_PLUS_NANO;
369 case IIO_CHAN_INFO_SAMP_FREQ:
370 *val = data->variant->regval_to_samp_freq[data->rate][0];
371 *val2 = data->variant->regval_to_samp_freq[data->rate][1];
372 return IIO_VAL_INT_PLUS_MICRO;
377 static int hmc5843_write_raw(struct iio_dev *indio_dev,
378 struct iio_chan_spec const *chan,
379 int val, int val2, long mask)
381 struct hmc5843_data *data = iio_priv(indio_dev);
385 case IIO_CHAN_INFO_SAMP_FREQ:
386 rate = hmc5843_get_samp_freq_index(data, val, val2);
390 return hmc5843_set_samp_freq(data, rate);
391 case IIO_CHAN_INFO_SCALE:
392 range = hmc5843_get_scale_index(data, val, val2);
396 return hmc5843_set_range_gain(data, range);
402 static int hmc5843_write_raw_get_fmt(struct iio_dev *indio_dev,
403 struct iio_chan_spec const *chan, long mask)
406 case IIO_CHAN_INFO_SAMP_FREQ:
407 return IIO_VAL_INT_PLUS_MICRO;
408 case IIO_CHAN_INFO_SCALE:
409 return IIO_VAL_INT_PLUS_NANO;
415 static irqreturn_t hmc5843_trigger_handler(int irq, void *p)
417 struct iio_poll_func *pf = p;
418 struct iio_dev *indio_dev = pf->indio_dev;
419 struct hmc5843_data *data = iio_priv(indio_dev);
422 mutex_lock(&data->lock);
423 ret = hmc5843_wait_measurement(data);
425 mutex_unlock(&data->lock);
429 ret = i2c_smbus_read_i2c_block_data(data->client,
430 HMC5843_DATA_OUT_MSB_REGS, 3 * sizeof(__be16),
431 (u8 *) data->buffer);
432 mutex_unlock(&data->lock);
436 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
440 iio_trigger_notify_done(indio_dev->trig);
445 #define HMC5843_CHANNEL(axis, idx) \
449 .channel2 = IIO_MOD_##axis, \
450 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
451 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
452 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
454 .scan_type = IIO_ST('s', 16, 16, IIO_BE), \
457 static const struct iio_chan_spec hmc5843_channels[] = {
458 HMC5843_CHANNEL(X, 0),
459 HMC5843_CHANNEL(Y, 1),
460 HMC5843_CHANNEL(Z, 2),
461 IIO_CHAN_SOFT_TIMESTAMP(3),
464 /* Beware: Y and Z are exchanged on HMC5883 */
465 static const struct iio_chan_spec hmc5883_channels[] = {
466 HMC5843_CHANNEL(X, 0),
467 HMC5843_CHANNEL(Z, 1),
468 HMC5843_CHANNEL(Y, 2),
469 IIO_CHAN_SOFT_TIMESTAMP(3),
472 static struct attribute *hmc5843_attributes[] = {
473 &iio_dev_attr_meas_conf.dev_attr.attr,
474 &iio_dev_attr_scale_available.dev_attr.attr,
475 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
479 static const struct attribute_group hmc5843_group = {
480 .attrs = hmc5843_attributes,
483 static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
485 .channels = hmc5843_channels,
486 .regval_to_samp_freq = hmc5843_regval_to_samp_freq,
487 .regval_to_nanoscale = hmc5843_regval_to_nanoscale,
490 .channels = hmc5883_channels,
491 .regval_to_samp_freq = hmc5883_regval_to_samp_freq,
492 .regval_to_nanoscale = hmc5883_regval_to_nanoscale,
495 .channels = hmc5883_channels,
496 .regval_to_samp_freq = hmc5883_regval_to_samp_freq,
497 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
501 static int hmc5843_init(struct hmc5843_data *data)
506 ret = i2c_smbus_read_i2c_block_data(data->client, HMC5843_ID_REG,
510 if (id[0] != 'H' || id[1] != '4' || id[2] != '3') {
511 dev_err(&data->client->dev, "no HMC5843/5883/5883L sensor\n");
515 ret = hmc5843_set_meas_conf(data, HMC5843_MEAS_CONF_NORMAL);
518 ret = hmc5843_set_samp_freq(data, HMC5843_RATE_DEFAULT);
521 ret = hmc5843_set_range_gain(data, HMC5843_RANGE_GAIN_DEFAULT);
524 return hmc5843_set_mode(data, HMC5843_MODE_CONVERSION_CONTINUOUS);
527 static const struct iio_info hmc5843_info = {
528 .attrs = &hmc5843_group,
529 .read_raw = &hmc5843_read_raw,
530 .write_raw = &hmc5843_write_raw,
531 .write_raw_get_fmt = &hmc5843_write_raw_get_fmt,
532 .driver_module = THIS_MODULE,
535 static const unsigned long hmc5843_scan_masks[] = {0x7, 0};
537 static int hmc5843_probe(struct i2c_client *client,
538 const struct i2c_device_id *id)
540 struct hmc5843_data *data;
541 struct iio_dev *indio_dev;
544 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
545 if (indio_dev == NULL)
548 /* default settings at probe */
549 data = iio_priv(indio_dev);
550 data->client = client;
551 data->variant = &hmc5843_chip_info_tbl[id->driver_data];
552 mutex_init(&data->lock);
554 i2c_set_clientdata(client, indio_dev);
555 indio_dev->info = &hmc5843_info;
556 indio_dev->name = id->name;
557 indio_dev->dev.parent = &client->dev;
558 indio_dev->modes = INDIO_DIRECT_MODE;
559 indio_dev->channels = data->variant->channels;
560 indio_dev->num_channels = 4;
561 indio_dev->available_scan_masks = hmc5843_scan_masks;
563 ret = hmc5843_init(data);
567 ret = iio_triggered_buffer_setup(indio_dev, NULL,
568 hmc5843_trigger_handler, NULL);
572 ret = iio_device_register(indio_dev);
579 iio_triggered_buffer_cleanup(indio_dev);
583 static int hmc5843_remove(struct i2c_client *client)
585 struct iio_dev *indio_dev = i2c_get_clientdata(client);
587 iio_device_unregister(indio_dev);
588 iio_triggered_buffer_cleanup(indio_dev);
590 /* sleep mode to save power */
591 hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP);
596 #ifdef CONFIG_PM_SLEEP
597 static int hmc5843_suspend(struct device *dev)
599 struct hmc5843_data *data = iio_priv(i2c_get_clientdata(
600 to_i2c_client(dev)));
602 return hmc5843_set_mode(data, HMC5843_MODE_SLEEP);
605 static int hmc5843_resume(struct device *dev)
607 struct hmc5843_data *data = iio_priv(i2c_get_clientdata(
608 to_i2c_client(dev)));
610 return hmc5843_set_mode(data, HMC5843_MODE_CONVERSION_CONTINUOUS);
613 static SIMPLE_DEV_PM_OPS(hmc5843_pm_ops, hmc5843_suspend, hmc5843_resume);
614 #define HMC5843_PM_OPS (&hmc5843_pm_ops)
616 #define HMC5843_PM_OPS NULL
619 static const struct i2c_device_id hmc5843_id[] = {
620 { "hmc5843", HMC5843_ID },
621 { "hmc5883", HMC5883_ID },
622 { "hmc5883l", HMC5883L_ID },
625 MODULE_DEVICE_TABLE(i2c, hmc5843_id);
627 static struct i2c_driver hmc5843_driver = {
630 .pm = HMC5843_PM_OPS,
632 .id_table = hmc5843_id,
633 .probe = hmc5843_probe,
634 .remove = hmc5843_remove,
636 module_i2c_driver(hmc5843_driver);
638 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>");
639 MODULE_DESCRIPTION("HMC5843/5883/5883L driver");
640 MODULE_LICENSE("GPL");