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/delay.h>
28 #define HMC5843_CONFIG_REG_A 0x00
29 #define HMC5843_CONFIG_REG_B 0x01
30 #define HMC5843_MODE_REG 0x02
31 #define HMC5843_DATA_OUT_X_MSB_REG 0x03
32 #define HMC5843_DATA_OUT_X_LSB_REG 0x04
33 #define HMC5843_DATA_OUT_Y_MSB_REG 0x05
34 #define HMC5843_DATA_OUT_Y_LSB_REG 0x06
35 #define HMC5843_DATA_OUT_Z_MSB_REG 0x07
36 #define HMC5843_DATA_OUT_Z_LSB_REG 0x08
37 /* Beware: Y and Z are exchanged on HMC5883 */
38 #define HMC5883_DATA_OUT_Z_MSB_REG 0x05
39 #define HMC5883_DATA_OUT_Z_LSB_REG 0x06
40 #define HMC5883_DATA_OUT_Y_MSB_REG 0x07
41 #define HMC5883_DATA_OUT_Y_LSB_REG 0x08
42 #define HMC5843_STATUS_REG 0x09
51 * Range gain settings in (+-)Ga
52 * Beware: HMC5843 and HMC5883 have different recommended sensor field
53 * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
55 #define HMC5843_RANGE_GAIN_OFFSET 0x05
56 #define HMC5843_RANGE_GAIN_DEFAULT 0x01
57 #define HMC5843_RANGE_GAIN_MAX 0x07
62 #define HMC5843_DATA_READY 0x01
63 #define HMC5843_DATA_OUTPUT_LOCK 0x02
64 /* Does not exist on HMC5883, not used */
65 #define HMC5843_VOLTAGE_REGULATOR_ENABLED 0x04
68 * Mode register configuration
70 #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
71 #define HMC5843_MODE_CONVERSION_SINGLE 0x01
72 #define HMC5843_MODE_IDLE 0x02
73 #define HMC5843_MODE_SLEEP 0x03
74 #define HMC5843_MODE_MASK 0x03
77 * HMC5843: Minimum data output rate
78 * HMC5883: Typical data output rate
80 #define HMC5843_RATE_OFFSET 0x02
81 #define HMC5843_RATE_BITMASK 0x1C
82 #define HMC5843_RATE_NOT_USED 0x07
85 * Device measurement configuration
87 #define HMC5843_MEAS_CONF_NORMAL 0x00
88 #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
89 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
90 #define HMC5843_MEAS_CONF_NOT_USED 0x03
91 #define HMC5843_MEAS_CONF_MASK 0x03
94 * 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 HMC5843 datasheet:
110 * Value | Sensor input field range (Ga) | Gain (counts/milli-Gauss)
120 * From the HMC5883 datasheet:
121 * Value | Recommended sensor field range (Ga) | Gain (counts/Gauss)
131 * From the HMC5883L datasheet:
132 * Value | Recommended sensor field range (Ga) | Gain (LSB/Gauss)
133 * 0 | (+-)0.88 | 1370
142 static const int hmc5843_regval_to_input_field_mga[] = {
143 700, 1000, 1500, 2000, 3200, 3800, 4500, 6500
146 static const int hmc5883_regval_to_input_field_mga[] = {
147 900, 1200, 1900, 2500, 4000, 4600, 5500, 7900
150 static const int hmc5883l_regval_to_input_field_mga[] = {
151 880, 1300, 1900, 2500, 4000, 4700, 5600, 8100
155 * From the datasheet:
156 * Value | HMC5843 | HMC5883/HMC5883L
157 * | Data output rate (Hz) | Data output rate (Hz)
162 * 4 | 10 (default) | 15
165 * 7 | Not used | Not used
167 static const char * const hmc5843_regval_to_sample_freq[] = {
168 "0.5", "1", "2", "5", "10", "20", "50",
171 static const char * const hmc5883_regval_to_sample_freq[] = {
172 "0.75", "1.5", "3", "7.5", "15", "30", "75",
175 /* Describe chip variants */
176 struct hmc5843_chip_info {
177 const struct iio_chan_spec *channels;
178 const char * const *regval_to_sample_freq;
179 const int *regval_to_input_field_mga;
180 const int *regval_to_nanoscale;
183 /* Each client has this additional data */
184 struct hmc5843_data {
190 const struct hmc5843_chip_info *variant;
193 /* The lower two bits contain the current conversion mode */
194 static s32 hmc5843_configure(struct i2c_client *client,
197 return i2c_smbus_write_byte_data(client,
199 operating_mode & HMC5843_MODE_MASK);
202 /* Return the measurement value from the specified channel */
203 static int hmc5843_read_measurement(struct iio_dev *indio_dev,
207 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
208 struct hmc5843_data *data = iio_priv(indio_dev);
212 mutex_lock(&data->lock);
213 while (tries-- > 0) {
214 result = i2c_smbus_read_byte_data(client,
216 if (result & HMC5843_DATA_READY)
222 dev_err(&client->dev, "data not ready\n");
223 mutex_unlock(&data->lock);
227 result = i2c_smbus_read_word_swapped(client, address);
228 mutex_unlock(&data->lock);
232 *val = sign_extend32(result, 15);
237 * From the datasheet:
238 * 0 - Continuous-Conversion Mode: In continuous-conversion mode, the
239 * device continuously performs conversions and places the result in
242 * 1 - Single-Conversion Mode : Device performs a single measurement,
243 * sets RDY high and returns to sleep mode.
245 * 2 - Idle Mode : Device is placed in idle mode.
247 * 3 - Sleep Mode : Device is placed in sleep mode.
250 static ssize_t hmc5843_show_operating_mode(struct device *dev,
251 struct device_attribute *attr,
254 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
255 struct hmc5843_data *data = iio_priv(indio_dev);
256 return sprintf(buf, "%d\n", data->operating_mode);
259 static ssize_t hmc5843_set_operating_mode(struct device *dev,
260 struct device_attribute *attr,
264 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
265 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
266 struct hmc5843_data *data = iio_priv(indio_dev);
267 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
268 unsigned long operating_mode = 0;
272 mutex_lock(&data->lock);
273 error = kstrtoul(buf, 10, &operating_mode);
278 dev_dbg(dev, "set conversion mode to %lu\n", operating_mode);
279 if (operating_mode > HMC5843_MODE_SLEEP) {
284 status = i2c_smbus_write_byte_data(client, this_attr->address,
290 data->operating_mode = operating_mode;
293 mutex_unlock(&data->lock);
297 static IIO_DEVICE_ATTR(operating_mode,
299 hmc5843_show_operating_mode,
300 hmc5843_set_operating_mode,
304 * API for setting the measurement configuration to
305 * Normal, Positive bias and Negative bias
307 * From the datasheet:
308 * 0 - Normal measurement configuration (default): In normal measurement
309 * configuration the device follows normal measurement flow. Pins BP
310 * and BN are left floating and high impedance.
312 * 1 - Positive bias configuration: In positive bias configuration, a
313 * positive current is forced across the resistive load on pins BP
316 * 2 - Negative bias configuration. In negative bias configuration, a
317 * negative current is forced across the resistive load on pins BP
321 static s32 hmc5843_set_meas_conf(struct i2c_client *client,
324 struct iio_dev *indio_dev = i2c_get_clientdata(client);
325 struct hmc5843_data *data = iio_priv(indio_dev);
327 reg_val = (meas_conf & HMC5843_MEAS_CONF_MASK) |
328 (data->rate << HMC5843_RATE_OFFSET);
329 return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
332 static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
333 struct device_attribute *attr,
336 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
337 struct hmc5843_data *data = iio_priv(indio_dev);
338 return sprintf(buf, "%d\n", data->meas_conf);
341 static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
342 struct device_attribute *attr,
346 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
347 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
348 struct hmc5843_data *data = iio_priv(indio_dev);
349 unsigned long meas_conf = 0;
352 error = kstrtoul(buf, 10, &meas_conf);
355 if (meas_conf >= HMC5843_MEAS_CONF_NOT_USED)
358 mutex_lock(&data->lock);
359 dev_dbg(dev, "set measurement configuration to %lu\n", meas_conf);
360 if (hmc5843_set_meas_conf(client, meas_conf)) {
364 data->meas_conf = meas_conf;
367 mutex_unlock(&data->lock);
371 static IIO_DEVICE_ATTR(meas_conf,
373 hmc5843_show_measurement_configuration,
374 hmc5843_set_measurement_configuration,
377 static ssize_t hmc5843_show_sampling_frequencies_available(struct device *dev,
378 struct device_attribute *attr,
381 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
382 struct hmc5843_data *data = iio_priv(indio_dev);
386 for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
387 ssize_t n = sprintf(buf, "%s ", data->variant->regval_to_sample_freq[i]);
391 /* replace trailing space by newline */
397 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_sampling_frequencies_available);
399 static s32 hmc5843_set_rate(struct i2c_client *client,
402 struct iio_dev *indio_dev = i2c_get_clientdata(client);
403 struct hmc5843_data *data = iio_priv(indio_dev);
406 if (rate >= HMC5843_RATE_NOT_USED) {
407 dev_err(&client->dev,
408 "data output rate is not supported\n");
412 reg_val = data->meas_conf | (rate << HMC5843_RATE_OFFSET);
413 return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
416 static int hmc5843_check_sampling_frequency(struct hmc5843_data *data,
419 const char * const *samp_freq = data->variant->regval_to_sample_freq;
422 for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
423 if (sysfs_streq(buf, samp_freq[i]))
430 static ssize_t hmc5843_set_sampling_frequency(struct device *dev,
431 struct device_attribute *attr,
432 const char *buf, size_t count)
435 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
436 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
437 struct hmc5843_data *data = iio_priv(indio_dev);
440 rate = hmc5843_check_sampling_frequency(data, buf);
442 dev_err(&client->dev,
443 "sampling frequency is not supported\n");
447 mutex_lock(&data->lock);
448 dev_dbg(dev, "set rate to %d\n", rate);
449 if (hmc5843_set_rate(client, rate)) {
456 mutex_unlock(&data->lock);
460 static ssize_t hmc5843_show_sampling_frequency(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
464 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
465 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
466 struct hmc5843_data *data = iio_priv(indio_dev);
469 rate = i2c_smbus_read_byte_data(client, this_attr->address);
472 rate = (rate & HMC5843_RATE_BITMASK) >> HMC5843_RATE_OFFSET;
473 return sprintf(buf, "%s\n", data->variant->regval_to_sample_freq[rate]);
476 static IIO_DEVICE_ATTR(sampling_frequency,
478 hmc5843_show_sampling_frequency,
479 hmc5843_set_sampling_frequency,
480 HMC5843_CONFIG_REG_A);
482 static ssize_t hmc5843_show_range_gain(struct device *dev,
483 struct device_attribute *attr,
487 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
488 struct hmc5843_data *data = iio_priv(indio_dev);
491 return sprintf(buf, "%d\n", data->variant->regval_to_input_field_mga[range]);
494 static ssize_t hmc5843_set_range_gain(struct device *dev,
495 struct device_attribute *attr,
499 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
500 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
501 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
502 struct hmc5843_data *data = iio_priv(indio_dev);
503 unsigned long range = 0;
506 mutex_lock(&data->lock);
507 error = kstrtoul(buf, 10, &range);
512 dev_dbg(dev, "set range to %lu\n", range);
514 if (range > HMC5843_RANGE_GAIN_MAX) {
520 range = range << HMC5843_RANGE_GAIN_OFFSET;
521 if (i2c_smbus_write_byte_data(client, this_attr->address, range))
525 mutex_unlock(&data->lock);
529 static IIO_DEVICE_ATTR(in_magn_range,
531 hmc5843_show_range_gain,
532 hmc5843_set_range_gain,
533 HMC5843_CONFIG_REG_B);
535 static int hmc5843_read_raw(struct iio_dev *indio_dev,
536 struct iio_chan_spec const *chan,
540 struct hmc5843_data *data = iio_priv(indio_dev);
543 case IIO_CHAN_INFO_RAW:
544 return hmc5843_read_measurement(indio_dev,
547 case IIO_CHAN_INFO_SCALE:
549 *val2 = data->variant->regval_to_nanoscale[data->range];
550 return IIO_VAL_INT_PLUS_NANO;
555 #define HMC5843_CHANNEL(axis, addr) \
559 .channel2 = IIO_MOD_##axis, \
560 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
561 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
565 static const struct iio_chan_spec hmc5843_channels[] = {
566 HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
567 HMC5843_CHANNEL(Y, HMC5843_DATA_OUT_Y_MSB_REG),
568 HMC5843_CHANNEL(Z, HMC5843_DATA_OUT_Z_MSB_REG),
571 static const struct iio_chan_spec hmc5883_channels[] = {
572 HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
573 HMC5843_CHANNEL(Y, HMC5883_DATA_OUT_Y_MSB_REG),
574 HMC5843_CHANNEL(Z, HMC5883_DATA_OUT_Z_MSB_REG),
577 static struct attribute *hmc5843_attributes[] = {
578 &iio_dev_attr_meas_conf.dev_attr.attr,
579 &iio_dev_attr_operating_mode.dev_attr.attr,
580 &iio_dev_attr_sampling_frequency.dev_attr.attr,
581 &iio_dev_attr_in_magn_range.dev_attr.attr,
582 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
586 static const struct attribute_group hmc5843_group = {
587 .attrs = hmc5843_attributes,
590 static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
592 .channels = hmc5843_channels,
593 .regval_to_sample_freq = hmc5843_regval_to_sample_freq,
594 .regval_to_input_field_mga =
595 hmc5843_regval_to_input_field_mga,
596 .regval_to_nanoscale = hmc5843_regval_to_nanoscale,
599 .channels = hmc5883_channels,
600 .regval_to_sample_freq = hmc5883_regval_to_sample_freq,
601 .regval_to_input_field_mga =
602 hmc5883_regval_to_input_field_mga,
603 .regval_to_nanoscale = hmc5883_regval_to_nanoscale,
606 .channels = hmc5883_channels,
607 .regval_to_sample_freq = hmc5883_regval_to_sample_freq,
608 .regval_to_input_field_mga =
609 hmc5883l_regval_to_input_field_mga,
610 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
614 /* Called when we have found a new HMC58X3 */
615 static void hmc5843_init_client(struct i2c_client *client,
616 const struct i2c_device_id *id)
618 struct iio_dev *indio_dev = i2c_get_clientdata(client);
619 struct hmc5843_data *data = iio_priv(indio_dev);
621 data->variant = &hmc5843_chip_info_tbl[id->driver_data];
622 indio_dev->channels = data->variant->channels;
623 indio_dev->num_channels = 3;
624 hmc5843_set_meas_conf(client, data->meas_conf);
625 hmc5843_set_rate(client, data->rate);
626 hmc5843_configure(client, data->operating_mode);
627 i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_B, data->range);
628 mutex_init(&data->lock);
630 pr_info("%s initialized\n", id->name);
633 static const struct iio_info hmc5843_info = {
634 .attrs = &hmc5843_group,
635 .read_raw = &hmc5843_read_raw,
636 .driver_module = THIS_MODULE,
639 static int hmc5843_probe(struct i2c_client *client,
640 const struct i2c_device_id *id)
642 struct hmc5843_data *data;
643 struct iio_dev *indio_dev;
646 indio_dev = iio_device_alloc(sizeof(*data));
647 if (indio_dev == NULL) {
652 /* default settings at probe */
653 data = iio_priv(indio_dev);
654 data->meas_conf = HMC5843_MEAS_CONF_NORMAL;
655 data->range = HMC5843_RANGE_GAIN_DEFAULT;
656 data->operating_mode = HMC5843_MODE_CONVERSION_CONTINUOUS;
658 i2c_set_clientdata(client, indio_dev);
659 hmc5843_init_client(client, id);
661 indio_dev->info = &hmc5843_info;
662 indio_dev->name = id->name;
663 indio_dev->dev.parent = &client->dev;
664 indio_dev->modes = INDIO_DIRECT_MODE;
666 err = iio_device_register(indio_dev);
673 iio_device_free(indio_dev);
678 static int hmc5843_remove(struct i2c_client *client)
680 struct iio_dev *indio_dev = i2c_get_clientdata(client);
682 iio_device_unregister(indio_dev);
683 /* sleep mode to save power */
684 hmc5843_configure(client, HMC5843_MODE_SLEEP);
685 iio_device_free(indio_dev);
690 #ifdef CONFIG_PM_SLEEP
691 static int hmc5843_suspend(struct device *dev)
693 hmc5843_configure(to_i2c_client(dev), HMC5843_MODE_SLEEP);
697 static int hmc5843_resume(struct device *dev)
699 struct i2c_client *client = to_i2c_client(dev);
700 struct iio_dev *indio_dev = i2c_get_clientdata(client);
701 struct hmc5843_data *data = iio_priv(indio_dev);
703 hmc5843_configure(client, data->operating_mode);
708 static SIMPLE_DEV_PM_OPS(hmc5843_pm_ops, hmc5843_suspend, hmc5843_resume);
709 #define HMC5843_PM_OPS (&hmc5843_pm_ops)
711 #define HMC5843_PM_OPS NULL
714 static const struct i2c_device_id hmc5843_id[] = {
715 { "hmc5843", HMC5843_ID },
716 { "hmc5883", HMC5883_ID },
717 { "hmc5883l", HMC5883L_ID },
720 MODULE_DEVICE_TABLE(i2c, hmc5843_id);
722 static struct i2c_driver hmc5843_driver = {
725 .pm = HMC5843_PM_OPS,
727 .id_table = hmc5843_id,
728 .probe = hmc5843_probe,
729 .remove = hmc5843_remove,
731 module_i2c_driver(hmc5843_driver);
733 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com");
734 MODULE_DESCRIPTION("HMC5843/5883/5883L driver");
735 MODULE_LICENSE("GPL");