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/init.h>
24 #include <linux/i2c.h>
25 #include <linux/slab.h>
26 #include <linux/types.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
30 #define HMC5843_CONFIG_REG_A 0x00
31 #define HMC5843_CONFIG_REG_B 0x01
32 #define HMC5843_MODE_REG 0x02
33 #define HMC5843_DATA_OUT_X_MSB_REG 0x03
34 #define HMC5843_DATA_OUT_X_LSB_REG 0x04
35 #define HMC5843_DATA_OUT_Y_MSB_REG 0x05
36 #define HMC5843_DATA_OUT_Y_LSB_REG 0x06
37 #define HMC5843_DATA_OUT_Z_MSB_REG 0x07
38 #define HMC5843_DATA_OUT_Z_LSB_REG 0x08
39 /* Beware: Y and Z are exchanged on HMC5883 */
40 #define HMC5883_DATA_OUT_Z_MSB_REG 0x05
41 #define HMC5883_DATA_OUT_Z_LSB_REG 0x06
42 #define HMC5883_DATA_OUT_Y_MSB_REG 0x07
43 #define HMC5883_DATA_OUT_Y_LSB_REG 0x08
44 #define HMC5843_STATUS_REG 0x09
45 #define HMC5843_ID_REG_A 0x0A
46 #define HMC5843_ID_REG_B 0x0B
47 #define HMC5843_ID_REG_C 0x0C
56 * Beware: identification of the HMC5883 is still "H43";
57 * I2C address is also unchanged
59 #define HMC5843_ID_REG_LENGTH 0x03
60 #define HMC5843_ID_STRING "H43"
61 #define HMC5843_I2C_ADDRESS 0x1E
64 * Range gain settings in (+-)Ga
65 * Beware: HMC5843 and HMC5883 have different recommended sensor field
66 * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
68 #define HMC5843_RANGE_GAIN_OFFSET 0x05
69 #define HMC5843_RANGE_GAIN_DEFAULT 0x01
70 #define HMC5843_RANGE_GAIN_MAX 0x07
75 #define HMC5843_DATA_READY 0x01
76 #define HMC5843_DATA_OUTPUT_LOCK 0x02
77 /* Does not exist on HMC5883, not used */
78 #define HMC5843_VOLTAGE_REGULATOR_ENABLED 0x04
81 * Mode register configuration
83 #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
84 #define HMC5843_MODE_CONVERSION_SINGLE 0x01
85 #define HMC5843_MODE_IDLE 0x02
86 #define HMC5843_MODE_SLEEP 0x03
87 #define HMC5843_MODE_MASK 0x03
90 * HMC5843: Minimum data output rate
91 * HMC5883: Typical data output rate
93 #define HMC5843_RATE_OFFSET 0x02
94 #define HMC5843_RATE_BITMASK 0x1C
95 #define HMC5843_RATE_NOT_USED 0x07
98 * Device measurement configuration
100 #define HMC5843_MEAS_CONF_NORMAL 0x00
101 #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
102 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
103 #define HMC5843_MEAS_CONF_NOT_USED 0x03
104 #define HMC5843_MEAS_CONF_MASK 0x03
107 * Scaling factors: 10000000/Gain
109 static const int hmc5843_regval_to_nanoscale[] = {
110 6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
113 static const int hmc5883_regval_to_nanoscale[] = {
114 7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
117 static const int hmc5883l_regval_to_nanoscale[] = {
118 7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
122 * From the HMC5843 datasheet:
123 * Value | Sensor input field range (Ga) | Gain (counts/milli-Gauss)
133 * From the HMC5883 datasheet:
134 * Value | Recommended sensor field range (Ga) | Gain (counts/Gauss)
144 * From the HMC5883L datasheet:
145 * Value | Recommended sensor field range (Ga) | Gain (LSB/Gauss)
146 * 0 | (+-)0.88 | 1370
155 static const int hmc5843_regval_to_input_field_mga[] = {
156 700, 1000, 1500, 2000, 3200, 3800, 4500, 6500
159 static const int hmc5883_regval_to_input_field_mga[] = {
160 900, 1200, 1900, 2500, 4000, 4600, 5500, 7900
163 static const int hmc5883l_regval_to_input_field_mga[] = {
164 880, 1300, 1900, 2500, 4000, 4700, 5600, 8100
168 * From the datasheet:
169 * Value | HMC5843 | HMC5883/HMC5883L
170 * | Data output rate (Hz) | Data output rate (Hz)
175 * 4 | 10 (default) | 15
178 * 7 | Not used | Not used
180 static const char * const hmc5843_regval_to_sample_freq[] = {
181 "0.5", "1", "2", "5", "10", "20", "50",
184 static const char * const hmc5883_regval_to_sample_freq[] = {
185 "0.75", "1.5", "3", "7.5", "15", "30", "75",
188 /* Addresses to scan: 0x1E */
189 static const unsigned short normal_i2c[] = { HMC5843_I2C_ADDRESS,
192 /* Describe chip variants */
193 struct hmc5843_chip_info {
194 const struct iio_chan_spec *channels;
196 const char * const *regval_to_sample_freq;
197 const int *regval_to_input_field_mga;
198 const int *regval_to_nanoscale;
201 /* Each client has this additional data */
202 struct hmc5843_data {
208 const struct hmc5843_chip_info *variant;
211 /* The lower two bits contain the current conversion mode */
212 static s32 hmc5843_configure(struct i2c_client *client,
215 return i2c_smbus_write_byte_data(client,
217 operating_mode & HMC5843_MODE_MASK);
220 /* Return the measurement value from the specified channel */
221 static int hmc5843_read_measurement(struct iio_dev *indio_dev,
225 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
226 struct hmc5843_data *data = iio_priv(indio_dev);
229 mutex_lock(&data->lock);
230 result = i2c_smbus_read_byte_data(client, HMC5843_STATUS_REG);
231 while (!(result & HMC5843_DATA_READY))
232 result = i2c_smbus_read_byte_data(client, HMC5843_STATUS_REG);
234 result = i2c_smbus_read_word_data(client, address);
235 mutex_unlock(&data->lock);
239 *val = (s16)swab16((u16)result);
244 * From the datasheet:
245 * 0 - Continuous-Conversion Mode: In continuous-conversion mode, the
246 * device continuously performs conversions and places the result in
249 * 1 - Single-Conversion Mode : Device performs a single measurement,
250 * sets RDY high and returns to sleep mode.
252 * 2 - Idle Mode : Device is placed in idle mode.
254 * 3 - Sleep Mode : Device is placed in sleep mode.
257 static ssize_t hmc5843_show_operating_mode(struct device *dev,
258 struct device_attribute *attr,
261 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
262 struct hmc5843_data *data = iio_priv(indio_dev);
263 return sprintf(buf, "%d\n", data->operating_mode);
266 static ssize_t hmc5843_set_operating_mode(struct device *dev,
267 struct device_attribute *attr,
271 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
272 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
273 struct hmc5843_data *data = iio_priv(indio_dev);
274 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
275 unsigned long operating_mode = 0;
279 mutex_lock(&data->lock);
280 error = kstrtoul(buf, 10, &operating_mode);
285 dev_dbg(dev, "set conversion mode to %lu\n", operating_mode);
286 if (operating_mode > HMC5843_MODE_SLEEP) {
291 status = i2c_smbus_write_byte_data(client, this_attr->address,
297 data->operating_mode = operating_mode;
300 mutex_unlock(&data->lock);
304 static IIO_DEVICE_ATTR(operating_mode,
306 hmc5843_show_operating_mode,
307 hmc5843_set_operating_mode,
311 * API for setting the measurement configuration to
312 * Normal, Positive bias and Negative bias
314 * From the datasheet:
315 * 0 - Normal measurement configuration (default): In normal measurement
316 * configuration the device follows normal measurement flow. Pins BP
317 * and BN are left floating and high impedance.
319 * 1 - Positive bias configuration: In positive bias configuration, a
320 * positive current is forced across the resistive load on pins BP
323 * 2 - Negative bias configuration. In negative bias configuration, a
324 * negative current is forced across the resistive load on pins BP
328 static s32 hmc5843_set_meas_conf(struct i2c_client *client,
331 struct iio_dev *indio_dev = i2c_get_clientdata(client);
332 struct hmc5843_data *data = iio_priv(indio_dev);
334 reg_val = (meas_conf & HMC5843_MEAS_CONF_MASK) |
335 (data->rate << HMC5843_RATE_OFFSET);
336 return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
339 static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
340 struct device_attribute *attr,
343 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
344 struct hmc5843_data *data = iio_priv(indio_dev);
345 return sprintf(buf, "%d\n", data->meas_conf);
348 static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
349 struct device_attribute *attr,
353 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
354 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
355 struct hmc5843_data *data = iio_priv(indio_dev);
356 unsigned long meas_conf = 0;
359 error = kstrtoul(buf, 10, &meas_conf);
362 if (meas_conf >= HMC5843_MEAS_CONF_NOT_USED)
365 mutex_lock(&data->lock);
366 dev_dbg(dev, "set measurement configuration to %lu\n", meas_conf);
367 if (hmc5843_set_meas_conf(client, meas_conf)) {
371 data->meas_conf = meas_conf;
374 mutex_unlock(&data->lock);
378 static IIO_DEVICE_ATTR(meas_conf,
380 hmc5843_show_measurement_configuration,
381 hmc5843_set_measurement_configuration,
384 static ssize_t hmc5843_show_sampling_frequencies_available(struct device *dev,
385 struct device_attribute *attr,
388 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
389 struct hmc5843_data *data = iio_priv(indio_dev);
393 for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
394 ssize_t n = sprintf(buf, "%s ", data->variant->regval_to_sample_freq[i]);
398 /* replace trailing space by newline */
404 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_sampling_frequencies_available);
406 static s32 hmc5843_set_rate(struct i2c_client *client,
409 struct iio_dev *indio_dev = i2c_get_clientdata(client);
410 struct hmc5843_data *data = iio_priv(indio_dev);
413 if (rate >= HMC5843_RATE_NOT_USED) {
414 dev_err(&client->dev,
415 "data output rate is not supported\n");
419 reg_val = data->meas_conf | (rate << HMC5843_RATE_OFFSET);
420 return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
423 static int hmc5843_check_sampling_frequency(struct hmc5843_data *data,
426 const char * const *samp_freq = data->variant->regval_to_sample_freq;
429 for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
430 if (sysfs_streq(buf, samp_freq[i]))
437 static ssize_t hmc5843_set_sampling_frequency(struct device *dev,
438 struct device_attribute *attr,
439 const char *buf, size_t count)
442 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
443 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
444 struct hmc5843_data *data = iio_priv(indio_dev);
447 rate = hmc5843_check_sampling_frequency(data, buf);
449 dev_err(&client->dev,
450 "sampling frequency is not supported\n");
454 mutex_lock(&data->lock);
455 dev_dbg(dev, "set rate to %d\n", rate);
456 if (hmc5843_set_rate(client, rate)) {
463 mutex_unlock(&data->lock);
467 static ssize_t hmc5843_show_sampling_frequency(struct device *dev,
468 struct device_attribute *attr, char *buf)
470 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
471 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
472 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
473 struct hmc5843_data *data = iio_priv(indio_dev);
476 rate = i2c_smbus_read_byte_data(client, this_attr->address);
479 rate = (rate & HMC5843_RATE_BITMASK) >> HMC5843_RATE_OFFSET;
480 return sprintf(buf, "%s\n", data->variant->regval_to_sample_freq[rate]);
483 static IIO_DEVICE_ATTR(sampling_frequency,
485 hmc5843_show_sampling_frequency,
486 hmc5843_set_sampling_frequency,
487 HMC5843_CONFIG_REG_A);
489 static ssize_t hmc5843_show_range_gain(struct device *dev,
490 struct device_attribute *attr,
494 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
495 struct hmc5843_data *data = iio_priv(indio_dev);
498 return sprintf(buf, "%d\n", data->variant->regval_to_input_field_mga[range]);
501 static ssize_t hmc5843_set_range_gain(struct device *dev,
502 struct device_attribute *attr,
506 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
507 struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
508 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
509 struct hmc5843_data *data = iio_priv(indio_dev);
510 unsigned long range = 0;
513 mutex_lock(&data->lock);
514 error = kstrtoul(buf, 10, &range);
519 dev_dbg(dev, "set range to %lu\n", range);
521 if (range > HMC5843_RANGE_GAIN_MAX) {
527 range = range << HMC5843_RANGE_GAIN_OFFSET;
528 if (i2c_smbus_write_byte_data(client, this_attr->address, range))
532 mutex_unlock(&data->lock);
536 static IIO_DEVICE_ATTR(in_magn_range,
538 hmc5843_show_range_gain,
539 hmc5843_set_range_gain,
540 HMC5843_CONFIG_REG_B);
542 static int hmc5843_read_raw(struct iio_dev *indio_dev,
543 struct iio_chan_spec const *chan,
547 struct hmc5843_data *data = iio_priv(indio_dev);
550 case IIO_CHAN_INFO_RAW:
551 return hmc5843_read_measurement(indio_dev,
554 case IIO_CHAN_INFO_SCALE:
556 *val2 = data->variant->regval_to_nanoscale[data->range];
557 return IIO_VAL_INT_PLUS_NANO;
562 #define HMC5843_CHANNEL(axis, add) \
566 .channel2 = IIO_MOD_##axis, \
567 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
568 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
572 static const struct iio_chan_spec hmc5843_channels[] = {
573 HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
574 HMC5843_CHANNEL(Y, HMC5843_DATA_OUT_Y_MSB_REG),
575 HMC5843_CHANNEL(Z, HMC5843_DATA_OUT_Z_MSB_REG),
578 static const struct iio_chan_spec hmc5883_channels[] = {
579 HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
580 HMC5843_CHANNEL(Y, HMC5883_DATA_OUT_Y_MSB_REG),
581 HMC5843_CHANNEL(Z, HMC5883_DATA_OUT_Z_MSB_REG),
584 static struct attribute *hmc5843_attributes[] = {
585 &iio_dev_attr_meas_conf.dev_attr.attr,
586 &iio_dev_attr_operating_mode.dev_attr.attr,
587 &iio_dev_attr_sampling_frequency.dev_attr.attr,
588 &iio_dev_attr_in_magn_range.dev_attr.attr,
589 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
593 static const struct attribute_group hmc5843_group = {
594 .attrs = hmc5843_attributes,
597 static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
599 .channels = hmc5843_channels,
600 .num_channels = ARRAY_SIZE(hmc5843_channels),
601 .regval_to_sample_freq = hmc5843_regval_to_sample_freq,
602 .regval_to_input_field_mga =
603 hmc5843_regval_to_input_field_mga,
604 .regval_to_nanoscale = hmc5843_regval_to_nanoscale,
607 .channels = hmc5883_channels,
608 .num_channels = ARRAY_SIZE(hmc5883_channels),
609 .regval_to_sample_freq = hmc5883_regval_to_sample_freq,
610 .regval_to_input_field_mga =
611 hmc5883_regval_to_input_field_mga,
612 .regval_to_nanoscale = hmc5883_regval_to_nanoscale,
615 .channels = hmc5883_channels,
616 .num_channels = ARRAY_SIZE(hmc5883_channels),
617 .regval_to_sample_freq = hmc5883_regval_to_sample_freq,
618 .regval_to_input_field_mga =
619 hmc5883l_regval_to_input_field_mga,
620 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
624 static int hmc5843_detect(struct i2c_client *client,
625 struct i2c_board_info *info)
627 unsigned char id_str[HMC5843_ID_REG_LENGTH];
629 if (client->addr != HMC5843_I2C_ADDRESS)
632 if (i2c_smbus_read_i2c_block_data(client, HMC5843_ID_REG_A,
633 HMC5843_ID_REG_LENGTH, id_str)
634 != HMC5843_ID_REG_LENGTH)
637 if (0 != strncmp(id_str, HMC5843_ID_STRING, HMC5843_ID_REG_LENGTH))
643 /* Called when we have found a new HMC58X3 */
644 static void hmc5843_init_client(struct i2c_client *client,
645 const struct i2c_device_id *id)
647 struct iio_dev *indio_dev = i2c_get_clientdata(client);
648 struct hmc5843_data *data = iio_priv(indio_dev);
650 data->variant = &hmc5843_chip_info_tbl[id->driver_data];
651 indio_dev->channels = data->variant->channels;
652 indio_dev->num_channels = data->variant->num_channels;
653 hmc5843_set_meas_conf(client, data->meas_conf);
654 hmc5843_set_rate(client, data->rate);
655 hmc5843_configure(client, data->operating_mode);
656 i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_B, data->range);
657 mutex_init(&data->lock);
659 pr_info("%s initialized\n", id->name);
662 static const struct iio_info hmc5843_info = {
663 .attrs = &hmc5843_group,
664 .read_raw = &hmc5843_read_raw,
665 .driver_module = THIS_MODULE,
668 static int __devinit hmc5843_probe(struct i2c_client *client,
669 const struct i2c_device_id *id)
671 struct hmc5843_data *data;
672 struct iio_dev *indio_dev;
675 indio_dev = iio_device_alloc(sizeof(*data));
676 if (indio_dev == NULL) {
681 /* default settings at probe */
682 data = iio_priv(indio_dev);
683 data->meas_conf = HMC5843_MEAS_CONF_NORMAL;
684 data->range = HMC5843_RANGE_GAIN_DEFAULT;
685 data->operating_mode = HMC5843_MODE_CONVERSION_CONTINUOUS;
687 i2c_set_clientdata(client, indio_dev);
688 hmc5843_init_client(client, id);
690 indio_dev->info = &hmc5843_info;
691 indio_dev->name = id->name;
692 indio_dev->dev.parent = &client->dev;
693 indio_dev->modes = INDIO_DIRECT_MODE;
695 err = iio_device_register(indio_dev);
702 iio_device_free(indio_dev);
707 static int __devexit hmc5843_remove(struct i2c_client *client)
709 struct iio_dev *indio_dev = i2c_get_clientdata(client);
711 iio_device_unregister(indio_dev);
712 /* sleep mode to save power */
713 hmc5843_configure(client, HMC5843_MODE_SLEEP);
714 iio_device_free(indio_dev);
719 #ifdef CONFIG_PM_SLEEP
720 static int hmc5843_suspend(struct device *dev)
722 hmc5843_configure(to_i2c_client(dev), HMC5843_MODE_SLEEP);
726 static int hmc5843_resume(struct device *dev)
728 struct i2c_client *client = to_i2c_client(dev);
729 struct iio_dev *indio_dev = i2c_get_clientdata(client);
730 struct hmc5843_data *data = iio_priv(indio_dev);
732 hmc5843_configure(client, data->operating_mode);
737 static SIMPLE_DEV_PM_OPS(hmc5843_pm_ops, hmc5843_suspend, hmc5843_resume);
738 #define HMC5843_PM_OPS (&hmc5843_pm_ops)
740 #define HMC5843_PM_OPS NULL
743 static const struct i2c_device_id hmc5843_id[] = {
744 { "hmc5843", HMC5843_ID },
745 { "hmc5883", HMC5883_ID },
746 { "hmc5883l", HMC5883L_ID },
749 MODULE_DEVICE_TABLE(i2c, hmc5843_id);
751 static struct i2c_driver hmc5843_driver = {
754 .pm = HMC5843_PM_OPS,
756 .id_table = hmc5843_id,
757 .probe = hmc5843_probe,
758 .remove = __devexit_p(hmc5843_remove),
759 .detect = hmc5843_detect,
760 .address_list = normal_i2c,
762 module_i2c_driver(hmc5843_driver);
764 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com");
765 MODULE_DESCRIPTION("HMC5843/5883/5883L driver");
766 MODULE_LICENSE("GPL");