1 /* Copyright (C) 2010 Texas Instruments
2 Author: Shubhrajyoti Datta <shubhrajyoti@ti.com>
3 Acknowledgement: Jonathan Cameron <jic23@cam.ac.uk> for valuable inputs.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/i2c.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
29 #define HMC5843_I2C_ADDRESS 0x1E
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_X_MSB_REG 0x03
35 #define HMC5843_DATA_OUT_X_LSB_REG 0x04
36 #define HMC5843_DATA_OUT_Y_MSB_REG 0x05
37 #define HMC5843_DATA_OUT_Y_LSB_REG 0x06
38 #define HMC5843_DATA_OUT_Z_MSB_REG 0x07
39 #define HMC5843_DATA_OUT_Z_LSB_REG 0x08
40 #define HMC5843_STATUS_REG 0x09
41 #define HMC5843_ID_REG_A 0x0A
42 #define HMC5843_ID_REG_B 0x0B
43 #define HMC5843_ID_REG_C 0x0C
45 #define HMC5843_ID_REG_LENGTH 0x03
46 #define HMC5843_ID_STRING "H43"
49 * Range settings in (+-)Ga
51 #define RANGE_GAIN_OFFSET 0x05
53 #define RANGE_0_7 0x00
54 #define RANGE_1_0 0x01 /* default */
55 #define RANGE_1_5 0x02
56 #define RANGE_2_0 0x03
57 #define RANGE_3_2 0x04
58 #define RANGE_3_8 0x05
59 #define RANGE_4_5 0x06
60 #define RANGE_6_5 0x07 /* Not recommended */
65 #define DATA_READY 0x01
66 #define DATA_OUTPUT_LOCK 0x02
67 #define VOLTAGE_REGULATOR_ENABLED 0x04
70 * Mode register configuration
72 #define MODE_CONVERSION_CONTINUOUS 0x00
73 #define MODE_CONVERSION_SINGLE 0x01
74 #define MODE_IDLE 0x02
75 #define MODE_SLEEP 0x03
77 /* Minimum Data Output Rate in 1/10 Hz */
78 #define RATE_OFFSET 0x02
79 #define RATE_BITMASK 0x1C
87 #define RATE_NOT_USED 0x07
90 * Device Configutration
92 #define CONF_NORMAL 0x00
93 #define CONF_POSITIVE_BIAS 0x01
94 #define CONF_NEGATIVE_BIAS 0x02
95 #define CONF_NOT_USED 0x03
96 #define MEAS_CONF_MASK 0x03
98 static const char *regval_to_scale
[] = {
108 static const int regval_to_input_field_mg
[] = {
118 static const char *regval_to_samp_freq
[] = {
128 /* Addresses to scan: 0x1E */
129 static const unsigned short normal_i2c
[] = { HMC5843_I2C_ADDRESS
,
132 /* Each client has this additional data */
133 struct hmc5843_data
{
141 static void hmc5843_init_client(struct i2c_client
*client
);
143 static s32
hmc5843_configure(struct i2c_client
*client
,
146 /* The lower two bits contain the current conversion mode */
147 return i2c_smbus_write_byte_data(client
,
149 (operating_mode
& 0x03));
152 /* Return the measurement value from the specified channel */
153 static ssize_t
hmc5843_read_measurement(struct device
*dev
,
154 struct device_attribute
*attr
,
157 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
158 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
160 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
161 struct hmc5843_data
*data
= iio_priv(indio_dev
);
164 mutex_lock(&data
->lock
);
166 result
= i2c_smbus_read_byte_data(client
, HMC5843_STATUS_REG
);
167 while (!(result
& DATA_READY
))
168 result
= i2c_smbus_read_byte_data(client
, HMC5843_STATUS_REG
);
170 result
= i2c_smbus_read_word_data(client
, this_attr
->address
);
171 mutex_unlock(&data
->lock
);
175 coordinate_val
= (s16
)swab16((u16
)result
);
176 return sprintf(buf
, "%d\n", coordinate_val
);
178 static IIO_DEV_ATTR_MAGN_X(hmc5843_read_measurement
,
179 HMC5843_DATA_OUT_X_MSB_REG
);
180 static IIO_DEV_ATTR_MAGN_Y(hmc5843_read_measurement
,
181 HMC5843_DATA_OUT_Y_MSB_REG
);
182 static IIO_DEV_ATTR_MAGN_Z(hmc5843_read_measurement
,
183 HMC5843_DATA_OUT_Z_MSB_REG
);
187 * 0 - Continuous-Conversion Mode: In continuous-conversion mode, the
188 * device continuously performs conversions an places the result in the
191 * 1 - Single-Conversion Mode : device performs a single measurement,
192 * sets RDY high and returned to sleep mode
194 * 2 - Idle Mode : Device is placed in idle mode.
196 * 3 - Sleep Mode. Device is placed in sleep mode.
199 static ssize_t
hmc5843_show_operating_mode(struct device
*dev
,
200 struct device_attribute
*attr
,
203 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
204 struct hmc5843_data
*data
= iio_priv(indio_dev
);
205 return sprintf(buf
, "%d\n", data
->operating_mode
);
208 static ssize_t
hmc5843_set_operating_mode(struct device
*dev
,
209 struct device_attribute
*attr
,
213 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
214 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
215 struct hmc5843_data
*data
= iio_priv(indio_dev
);
216 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
217 unsigned long operating_mode
= 0;
220 mutex_lock(&data
->lock
);
221 error
= strict_strtoul(buf
, 10, &operating_mode
);
226 dev_dbg(dev
, "set Conversion mode to %lu\n", operating_mode
);
227 if (operating_mode
> MODE_SLEEP
) {
232 status
= i2c_smbus_write_byte_data(client
, this_attr
->address
,
238 data
->operating_mode
= operating_mode
;
241 mutex_unlock(&data
->lock
);
244 static IIO_DEVICE_ATTR(operating_mode
,
246 hmc5843_show_operating_mode
,
247 hmc5843_set_operating_mode
,
251 * API for setting the measurement configuration to
252 * Normal, Positive bias and Negative bias
255 * Normal measurement configuration (default): In normal measurement
256 * configuration the device follows normal measurement flow. Pins BP and BN
257 * are left floating and high impedance.
259 * Positive bias configuration: In positive bias configuration, a positive
260 * current is forced across the resistive load on pins BP and BN.
262 * Negative bias configuration. In negative bias configuration, a negative
263 * current is forced across the resistive load on pins BP and BN.
266 static s32
hmc5843_set_meas_conf(struct i2c_client
*client
,
269 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
271 reg_val
= (meas_conf
& MEAS_CONF_MASK
) | (data
->rate
<< RATE_OFFSET
);
272 return i2c_smbus_write_byte_data(client
, HMC5843_CONFIG_REG_A
, reg_val
);
275 static ssize_t
hmc5843_show_measurement_configuration(struct device
*dev
,
276 struct device_attribute
*attr
,
279 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
280 struct hmc5843_data
*data
= iio_priv(indio_dev
);
281 return sprintf(buf
, "%d\n", data
->meas_conf
);
284 static ssize_t
hmc5843_set_measurement_configuration(struct device
*dev
,
285 struct device_attribute
*attr
,
289 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
290 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
291 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
292 unsigned long meas_conf
= 0;
293 int error
= strict_strtoul(buf
, 10, &meas_conf
);
296 mutex_lock(&data
->lock
);
298 dev_dbg(dev
, "set mode to %lu\n", meas_conf
);
299 if (hmc5843_set_meas_conf(client
, meas_conf
)) {
303 data
->meas_conf
= meas_conf
;
306 mutex_unlock(&data
->lock
);
309 static IIO_DEVICE_ATTR(meas_conf
,
311 hmc5843_show_measurement_configuration
,
312 hmc5843_set_measurement_configuration
,
317 * The table shows the minimum data output
318 * Value | Minimum data output rate(Hz)
328 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("0.5 1 2 5 10 20 50");
330 static s32
hmc5843_set_rate(struct i2c_client
*client
,
333 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
336 reg_val
= (data
->meas_conf
) | (rate
<< RATE_OFFSET
);
337 if (rate
>= RATE_NOT_USED
) {
338 dev_err(&client
->dev
,
339 "This data output rate is not supported \n");
342 return i2c_smbus_write_byte_data(client
, HMC5843_CONFIG_REG_A
, reg_val
);
345 static ssize_t
set_sampling_frequency(struct device
*dev
,
346 struct device_attribute
*attr
,
347 const char *buf
, size_t count
)
350 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
351 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
352 struct hmc5843_data
*data
= iio_priv(indio_dev
);
353 unsigned long rate
= 0;
355 if (strncmp(buf
, "0.5" , 3) == 0)
357 else if (strncmp(buf
, "1" , 1) == 0)
359 else if (strncmp(buf
, "2", 1) == 0)
361 else if (strncmp(buf
, "5", 1) == 0)
363 else if (strncmp(buf
, "10", 2) == 0)
365 else if (strncmp(buf
, "20" , 2) == 0)
367 else if (strncmp(buf
, "50" , 2) == 0)
372 mutex_lock(&data
->lock
);
373 dev_dbg(dev
, "set rate to %lu\n", rate
);
374 if (hmc5843_set_rate(client
, rate
)) {
381 mutex_unlock(&data
->lock
);
385 static ssize_t
show_sampling_frequency(struct device
*dev
,
386 struct device_attribute
*attr
, char *buf
)
388 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
389 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
390 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
393 rate
= i2c_smbus_read_byte_data(client
, this_attr
->address
);
396 rate
= (rate
& RATE_BITMASK
) >> RATE_OFFSET
;
397 return sprintf(buf
, "%s\n", regval_to_samp_freq
[rate
]);
399 static IIO_DEVICE_ATTR(sampling_frequency
,
401 show_sampling_frequency
,
402 set_sampling_frequency
,
403 HMC5843_CONFIG_REG_A
);
407 * Nominal gain settings
408 * Value | Sensor Input Field Range(Ga) | Gain(counts/ milli-gauss)
418 static ssize_t
show_range(struct device
*dev
,
419 struct device_attribute
*attr
,
423 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
424 struct hmc5843_data
*data
= iio_priv(indio_dev
);
427 return sprintf(buf
, "%d\n", regval_to_input_field_mg
[range
]);
430 static ssize_t
set_range(struct device
*dev
,
431 struct device_attribute
*attr
,
435 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
436 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
437 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
438 struct hmc5843_data
*data
= iio_priv(indio_dev
);
439 unsigned long range
= 0;
441 mutex_lock(&data
->lock
);
442 error
= strict_strtoul(buf
, 10, &range
);
447 dev_dbg(dev
, "set range to %lu\n", range
);
449 if (range
> RANGE_6_5
) {
455 range
= range
<< RANGE_GAIN_OFFSET
;
456 if (i2c_smbus_write_byte_data(client
, this_attr
->address
, range
))
460 mutex_unlock(&data
->lock
);
464 static IIO_DEVICE_ATTR(magn_range
,
468 HMC5843_CONFIG_REG_B
);
470 static ssize_t
show_scale(struct device
*dev
,
471 struct device_attribute
*attr
,
474 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
475 struct hmc5843_data
*data
= iio_priv(indio_dev
);
476 return strlen(strcpy(buf
, regval_to_scale
[data
->range
]));
478 static IIO_DEVICE_ATTR(magn_scale
,
483 static struct attribute
*hmc5843_attributes
[] = {
484 &iio_dev_attr_meas_conf
.dev_attr
.attr
,
485 &iio_dev_attr_operating_mode
.dev_attr
.attr
,
486 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
487 &iio_dev_attr_magn_range
.dev_attr
.attr
,
488 &iio_dev_attr_magn_scale
.dev_attr
.attr
,
489 &iio_dev_attr_magn_x_raw
.dev_attr
.attr
,
490 &iio_dev_attr_magn_y_raw
.dev_attr
.attr
,
491 &iio_dev_attr_magn_z_raw
.dev_attr
.attr
,
492 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
496 static const struct attribute_group hmc5843_group
= {
497 .attrs
= hmc5843_attributes
,
500 static int hmc5843_detect(struct i2c_client
*client
,
501 struct i2c_board_info
*info
)
503 unsigned char id_str
[HMC5843_ID_REG_LENGTH
];
505 if (client
->addr
!= HMC5843_I2C_ADDRESS
)
508 if (i2c_smbus_read_i2c_block_data(client
, HMC5843_ID_REG_A
,
509 HMC5843_ID_REG_LENGTH
, id_str
)
510 != HMC5843_ID_REG_LENGTH
)
513 if (0 != strncmp(id_str
, HMC5843_ID_STRING
, HMC5843_ID_REG_LENGTH
))
519 /* Called when we have found a new HMC5843. */
520 static void hmc5843_init_client(struct i2c_client
*client
)
522 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
523 hmc5843_set_meas_conf(client
, data
->meas_conf
);
524 hmc5843_set_rate(client
, data
->rate
);
525 hmc5843_configure(client
, data
->operating_mode
);
526 i2c_smbus_write_byte_data(client
, HMC5843_CONFIG_REG_B
, data
->range
);
527 mutex_init(&data
->lock
);
528 pr_info("HMC5843 initialized\n");
531 static const struct iio_info hmc5843_info
= {
532 .attrs
= &hmc5843_group
,
533 .driver_module
= THIS_MODULE
,
536 static int hmc5843_probe(struct i2c_client
*client
,
537 const struct i2c_device_id
*id
)
539 struct hmc5843_data
*data
;
540 struct iio_dev
*indio_dev
;
543 indio_dev
= iio_allocate_device(sizeof(*data
));
544 if (indio_dev
== NULL
) {
548 data
= iio_priv(indio_dev
);
549 /* default settings at probe */
551 data
->meas_conf
= CONF_NORMAL
;
552 data
->range
= RANGE_1_0
;
553 data
->operating_mode
= MODE_CONVERSION_CONTINUOUS
;
555 i2c_set_clientdata(client
, indio_dev
);
557 /* Initialize the HMC5843 chip */
558 hmc5843_init_client(client
);
560 indio_dev
->info
= &hmc5843_info
;
561 indio_dev
->dev
.parent
= &client
->dev
;
562 indio_dev
->modes
= INDIO_DIRECT_MODE
;
563 err
= iio_device_register(indio_dev
);
568 iio_free_device(indio_dev
);
573 static int hmc5843_remove(struct i2c_client
*client
)
575 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
576 /* sleep mode to save power */
577 hmc5843_configure(client
, MODE_SLEEP
);
578 iio_device_unregister(indio_dev
);
583 static int hmc5843_suspend(struct i2c_client
*client
, pm_message_t mesg
)
585 hmc5843_configure(client
, MODE_SLEEP
);
589 static int hmc5843_resume(struct i2c_client
*client
)
591 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
592 hmc5843_configure(client
, data
->operating_mode
);
596 static const struct i2c_device_id hmc5843_id
[] = {
601 static struct i2c_driver hmc5843_driver
= {
605 .id_table
= hmc5843_id
,
606 .probe
= hmc5843_probe
,
607 .remove
= hmc5843_remove
,
608 .detect
= hmc5843_detect
,
609 .address_list
= normal_i2c
,
610 .suspend
= hmc5843_suspend
,
611 .resume
= hmc5843_resume
,
614 static int __init
hmc5843_init(void)
616 return i2c_add_driver(&hmc5843_driver
);
619 static void __exit
hmc5843_exit(void)
621 i2c_del_driver(&hmc5843_driver
);
624 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com");
625 MODULE_DESCRIPTION("HMC5843 driver");
626 MODULE_LICENSE("GPL");
628 module_init(hmc5843_init
);
629 module_exit(hmc5843_exit
);