22 #include <linux/module.h>
24 #include <linux/i2c.h>
25 #include <linux/slab.h>
26 #include <linux/types.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
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
59 #define HMC5843_ID_REG_LENGTH 0x03
60 #define HMC5843_ID_STRING "H43"
61 #define HMC5843_I2C_ADDRESS 0x1E
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
78 #define HMC5843_VOLTAGE_REGULATOR_ENABLED 0x04
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
93 #define HMC5843_RATE_OFFSET 0x02
94 #define HMC5843_RATE_BITMASK 0x1C
95 #define HMC5843_RATE_NOT_USED 0x07
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
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
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
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",
221 static int hmc5843_read_measurement(
struct iio_dev *indio_dev,
261 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
271 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
275 unsigned long operating_mode = 0;
280 error = kstrtoul(buf, 10, &operating_mode);
285 dev_dbg(dev,
"set conversion mode to %lu\n", operating_mode);
306 hmc5843_show_operating_mode,
307 hmc5843_set_operating_mode,
331 struct iio_dev *indio_dev = i2c_get_clientdata(client);
339 static ssize_t hmc5843_show_measurement_configuration(
struct device *dev,
343 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
348 static ssize_t hmc5843_set_measurement_configuration(
struct device *dev,
353 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
356 unsigned long meas_conf = 0;
359 error = kstrtoul(buf, 10, &meas_conf);
366 dev_dbg(dev,
"set measurement configuration to %lu\n", meas_conf);
367 if (hmc5843_set_meas_conf(client, meas_conf)) {
380 hmc5843_show_measurement_configuration,
381 hmc5843_set_measurement_configuration,
384 static ssize_t hmc5843_show_sampling_frequencies_available(
struct device *dev,
388 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
409 struct iio_dev *indio_dev = i2c_get_clientdata(client);
415 "data output rate is not supported\n");
423 static int hmc5843_check_sampling_frequency(
struct hmc5843_data *data,
426 const char *
const *samp_freq = data->
variant->regval_to_sample_freq;
437 static ssize_t hmc5843_set_sampling_frequency(
struct device *dev,
439 const char *buf,
size_t count)
442 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
447 rate = hmc5843_check_sampling_frequency(data, buf);
450 "sampling frequency is not supported\n");
455 dev_dbg(dev,
"set rate to %d\n", rate);
456 if (hmc5843_set_rate(client, rate)) {
467 static ssize_t hmc5843_show_sampling_frequency(
struct device *dev,
470 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
480 return sprintf(buf,
"%s\n", data->
variant->regval_to_sample_freq[rate]);
485 hmc5843_show_sampling_frequency,
486 hmc5843_set_sampling_frequency,
494 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
498 return sprintf(buf,
"%d\n", data->
variant->regval_to_input_field_mga[range]);
506 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
510 unsigned long range = 0;
514 error = kstrtoul(buf, 10, &range);
519 dev_dbg(dev,
"set range to %lu\n", range);
538 hmc5843_show_range_gain,
539 hmc5843_set_range_gain,
542 static int hmc5843_read_raw(
struct iio_dev *indio_dev,
551 return hmc5843_read_measurement(indio_dev,
556 *val2 = data->
variant->regval_to_nanoscale[data->
range];
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, \
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,
594 .attrs = hmc5843_attributes,
599 .channels = 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,
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,
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,
644 static void hmc5843_init_client(
struct i2c_client *client,
647 struct iio_dev *indio_dev = i2c_get_clientdata(client);
650 data->
variant = &hmc5843_chip_info_tbl[
id->driver_data];
653 hmc5843_set_meas_conf(client, data->
meas_conf);
654 hmc5843_set_rate(client, data->
rate);
662 static const struct iio_info hmc5843_info = {
663 .attrs = &hmc5843_group,
664 .read_raw = &hmc5843_read_raw,
676 if (indio_dev ==
NULL) {
682 data = iio_priv(indio_dev);
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;
709 struct iio_dev *indio_dev = i2c_get_clientdata(client);
719 #ifdef CONFIG_PM_SLEEP
720 static int hmc5843_suspend(
struct device *dev)
726 static int hmc5843_resume(
struct device *dev)
729 struct iio_dev *indio_dev = i2c_get_clientdata(client);
738 #define HMC5843_PM_OPS (&hmc5843_pm_ops)
740 #define HMC5843_PM_OPS NULL
756 .id_table = hmc5843_id,
757 .probe = hmc5843_probe,
759 .detect = hmc5843_detect,
760 .address_list = normal_i2c,