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
{
134 struct iio_dev
*indio_dev
;
142 static void hmc5843_init_client(struct i2c_client
*client
);
144 static s32
hmc5843_configure(struct i2c_client
*client
,
147 /* The lower two bits contain the current conversion mode */
148 return i2c_smbus_write_byte_data(client
,
150 (operating_mode
& 0x03));
153 /* Return the measurement value from the specified channel */
154 static ssize_t
hmc5843_read_measurement(struct device
*dev
,
155 struct device_attribute
*attr
,
158 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
159 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
161 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
162 struct hmc5843_data
*data
= indio_dev
->dev_data
;
165 mutex_lock(&data
->lock
);
167 result
= i2c_smbus_read_byte_data(client
, HMC5843_STATUS_REG
);
168 while (!(result
& DATA_READY
))
169 result
= i2c_smbus_read_byte_data(client
, HMC5843_STATUS_REG
);
171 result
= i2c_smbus_read_word_data(client
, this_attr
->address
);
172 mutex_unlock(&data
->lock
);
176 coordinate_val
= (s16
)swab16((u16
)result
);
177 return sprintf(buf
, "%d\n", coordinate_val
);
179 static IIO_DEV_ATTR_MAGN_X(hmc5843_read_measurement
,
180 HMC5843_DATA_OUT_X_MSB_REG
);
181 static IIO_DEV_ATTR_MAGN_Y(hmc5843_read_measurement
,
182 HMC5843_DATA_OUT_Y_MSB_REG
);
183 static IIO_DEV_ATTR_MAGN_Z(hmc5843_read_measurement
,
184 HMC5843_DATA_OUT_Z_MSB_REG
);
188 * 0 - Continuous-Conversion Mode: In continuous-conversion mode, the
189 * device continuously performs conversions an places the result in the
192 * 1 - Single-Conversion Mode : device performs a single measurement,
193 * sets RDY high and returned to sleep mode
195 * 2 - Idle Mode : Device is placed in idle mode.
197 * 3 - Sleep Mode. Device is placed in sleep mode.
200 static ssize_t
hmc5843_show_operating_mode(struct device
*dev
,
201 struct device_attribute
*attr
,
204 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
205 struct hmc5843_data
*data
= indio_dev
->dev_data
;
206 return sprintf(buf
, "%d\n", data
->operating_mode
);
209 static ssize_t
hmc5843_set_operating_mode(struct device
*dev
,
210 struct device_attribute
*attr
,
214 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
215 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
216 struct hmc5843_data
*data
= indio_dev
->dev_data
;
217 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
218 unsigned long operating_mode
= 0;
221 mutex_lock(&data
->lock
);
222 error
= strict_strtoul(buf
, 10, &operating_mode
);
227 dev_dbg(dev
, "set Conversion mode to %lu\n", operating_mode
);
228 if (operating_mode
> MODE_SLEEP
) {
233 status
= i2c_smbus_write_byte_data(client
, this_attr
->address
,
239 data
->operating_mode
= operating_mode
;
242 mutex_unlock(&data
->lock
);
245 static IIO_DEVICE_ATTR(operating_mode
,
247 hmc5843_show_operating_mode
,
248 hmc5843_set_operating_mode
,
252 * API for setting the measurement configuration to
253 * Normal, Positive bias and Negative bias
256 * Normal measurement configuration (default): In normal measurement
257 * configuration the device follows normal measurement flow. Pins BP and BN
258 * are left floating and high impedance.
260 * Positive bias configuration: In positive bias configuration, a positive
261 * current is forced across the resistive load on pins BP and BN.
263 * Negative bias configuration. In negative bias configuration, a negative
264 * current is forced across the resistive load on pins BP and BN.
267 static s32
hmc5843_set_meas_conf(struct i2c_client
*client
,
270 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
272 reg_val
= (meas_conf
& MEAS_CONF_MASK
) | (data
->rate
<< RATE_OFFSET
);
273 return i2c_smbus_write_byte_data(client
, HMC5843_CONFIG_REG_A
, reg_val
);
276 static ssize_t
hmc5843_show_measurement_configuration(struct device
*dev
,
277 struct device_attribute
*attr
,
280 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
281 struct hmc5843_data
*data
= indio_dev
->dev_data
;
282 return sprintf(buf
, "%d\n", data
->meas_conf
);
285 static ssize_t
hmc5843_set_measurement_configuration(struct device
*dev
,
286 struct device_attribute
*attr
,
290 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
291 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
292 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
293 unsigned long meas_conf
= 0;
294 int error
= strict_strtoul(buf
, 10, &meas_conf
);
297 mutex_lock(&data
->lock
);
299 dev_dbg(dev
, "set mode to %lu\n", meas_conf
);
300 if (hmc5843_set_meas_conf(client
, meas_conf
)) {
304 data
->meas_conf
= meas_conf
;
307 mutex_unlock(&data
->lock
);
310 static IIO_DEVICE_ATTR(meas_conf
,
312 hmc5843_show_measurement_configuration
,
313 hmc5843_set_measurement_configuration
,
318 * The table shows the minimum data output
319 * Value | Minimum data output rate(Hz)
329 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("0.5 1 2 5 10 20 50");
331 static s32
hmc5843_set_rate(struct i2c_client
*client
,
334 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
337 reg_val
= (data
->meas_conf
) | (rate
<< RATE_OFFSET
);
338 if (rate
>= RATE_NOT_USED
) {
339 dev_err(&client
->dev
,
340 "This data output rate is not supported \n");
343 return i2c_smbus_write_byte_data(client
, HMC5843_CONFIG_REG_A
, reg_val
);
346 static ssize_t
set_sampling_frequency(struct device
*dev
,
347 struct device_attribute
*attr
,
348 const char *buf
, size_t count
)
351 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
352 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
353 struct hmc5843_data
*data
= indio_dev
->dev_data
;
354 unsigned long rate
= 0;
356 if (strncmp(buf
, "0.5" , 3) == 0)
358 else if (strncmp(buf
, "1" , 1) == 0)
360 else if (strncmp(buf
, "2", 1) == 0)
362 else if (strncmp(buf
, "5", 1) == 0)
364 else if (strncmp(buf
, "10", 2) == 0)
366 else if (strncmp(buf
, "20" , 2) == 0)
368 else if (strncmp(buf
, "50" , 2) == 0)
373 mutex_lock(&data
->lock
);
374 dev_dbg(dev
, "set rate to %lu\n", rate
);
375 if (hmc5843_set_rate(client
, rate
)) {
382 mutex_unlock(&data
->lock
);
386 static ssize_t
show_sampling_frequency(struct device
*dev
,
387 struct device_attribute
*attr
, char *buf
)
389 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
390 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
391 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
394 rate
= i2c_smbus_read_byte_data(client
, this_attr
->address
);
397 rate
= (rate
& RATE_BITMASK
) >> RATE_OFFSET
;
398 return sprintf(buf
, "%s\n", regval_to_samp_freq
[rate
]);
400 static IIO_DEVICE_ATTR(sampling_frequency
,
402 show_sampling_frequency
,
403 set_sampling_frequency
,
404 HMC5843_CONFIG_REG_A
);
408 * Nominal gain settings
409 * Value | Sensor Input Field Range(Ga) | Gain(counts/ milli-gauss)
419 static ssize_t
show_range(struct device
*dev
,
420 struct device_attribute
*attr
,
424 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
425 struct hmc5843_data
*data
= indio_dev
->dev_data
;
428 return sprintf(buf
, "%d\n", regval_to_input_field_mg
[range
]);
431 static ssize_t
set_range(struct device
*dev
,
432 struct device_attribute
*attr
,
436 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
437 struct i2c_client
*client
= to_i2c_client(indio_dev
->dev
.parent
);
438 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
439 struct hmc5843_data
*data
= indio_dev
->dev_data
;
440 unsigned long range
= 0;
442 mutex_lock(&data
->lock
);
443 error
= strict_strtoul(buf
, 10, &range
);
448 dev_dbg(dev
, "set range to %lu\n", range
);
450 if (range
> RANGE_6_5
) {
456 range
= range
<< RANGE_GAIN_OFFSET
;
457 if (i2c_smbus_write_byte_data(client
, this_attr
->address
, range
))
461 mutex_unlock(&data
->lock
);
465 static IIO_DEVICE_ATTR(magn_range
,
469 HMC5843_CONFIG_REG_B
);
471 static ssize_t
show_scale(struct device
*dev
,
472 struct device_attribute
*attr
,
475 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
476 struct hmc5843_data
*data
= indio_dev
->dev_data
;
477 return strlen(strcpy(buf
, regval_to_scale
[data
->range
]));
479 static IIO_DEVICE_ATTR(magn_scale
,
484 static struct attribute
*hmc5843_attributes
[] = {
485 &iio_dev_attr_meas_conf
.dev_attr
.attr
,
486 &iio_dev_attr_operating_mode
.dev_attr
.attr
,
487 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
488 &iio_dev_attr_magn_range
.dev_attr
.attr
,
489 &iio_dev_attr_magn_scale
.dev_attr
.attr
,
490 &iio_dev_attr_magn_x_raw
.dev_attr
.attr
,
491 &iio_dev_attr_magn_y_raw
.dev_attr
.attr
,
492 &iio_dev_attr_magn_z_raw
.dev_attr
.attr
,
493 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
497 static const struct attribute_group hmc5843_group
= {
498 .attrs
= hmc5843_attributes
,
501 static int hmc5843_detect(struct i2c_client
*client
,
502 struct i2c_board_info
*info
)
504 unsigned char id_str
[HMC5843_ID_REG_LENGTH
];
506 if (client
->addr
!= HMC5843_I2C_ADDRESS
)
509 if (i2c_smbus_read_i2c_block_data(client
, HMC5843_ID_REG_A
,
510 HMC5843_ID_REG_LENGTH
, id_str
)
511 != HMC5843_ID_REG_LENGTH
)
514 if (0 != strncmp(id_str
, HMC5843_ID_STRING
, HMC5843_ID_REG_LENGTH
))
520 /* Called when we have found a new HMC5843. */
521 static void hmc5843_init_client(struct i2c_client
*client
)
523 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
524 hmc5843_set_meas_conf(client
, data
->meas_conf
);
525 hmc5843_set_rate(client
, data
->rate
);
526 hmc5843_configure(client
, data
->operating_mode
);
527 i2c_smbus_write_byte_data(client
, HMC5843_CONFIG_REG_B
, data
->range
);
528 mutex_init(&data
->lock
);
529 pr_info("HMC5843 initialized\n");
532 static const struct iio_info hmc5843_info
= {
533 .attrs
= &hmc5843_group
,
534 .driver_module
= THIS_MODULE
,
537 static int hmc5843_probe(struct i2c_client
*client
,
538 const struct i2c_device_id
*id
)
540 struct hmc5843_data
*data
;
543 data
= kzalloc(sizeof(struct hmc5843_data
), GFP_KERNEL
);
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
, data
);
557 /* Initialize the HMC5843 chip */
558 hmc5843_init_client(client
);
560 data
->indio_dev
= iio_allocate_device(0);
561 if (!data
->indio_dev
) {
565 data
->indio_dev
->info
= &hmc5843_info
;
566 data
->indio_dev
->dev
.parent
= &client
->dev
;
567 data
->indio_dev
->dev_data
= (void *)(data
);
568 data
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
569 err
= iio_device_register(data
->indio_dev
);
574 iio_free_device(data
->indio_dev
);
581 static int hmc5843_remove(struct i2c_client
*client
)
583 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
584 /* sleep mode to save power */
585 hmc5843_configure(client
, MODE_SLEEP
);
586 iio_device_unregister(data
->indio_dev
);
587 kfree(i2c_get_clientdata(client
));
591 static int hmc5843_suspend(struct i2c_client
*client
, pm_message_t mesg
)
593 hmc5843_configure(client
, MODE_SLEEP
);
597 static int hmc5843_resume(struct i2c_client
*client
)
599 struct hmc5843_data
*data
= i2c_get_clientdata(client
);
600 hmc5843_configure(client
, data
->operating_mode
);
604 static const struct i2c_device_id hmc5843_id
[] = {
609 static struct i2c_driver hmc5843_driver
= {
613 .id_table
= hmc5843_id
,
614 .probe
= hmc5843_probe
,
615 .remove
= hmc5843_remove
,
616 .detect
= hmc5843_detect
,
617 .address_list
= normal_i2c
,
618 .suspend
= hmc5843_suspend
,
619 .resume
= hmc5843_resume
,
622 static int __init
hmc5843_init(void)
624 return i2c_add_driver(&hmc5843_driver
);
627 static void __exit
hmc5843_exit(void)
629 i2c_del_driver(&hmc5843_driver
);
632 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com");
633 MODULE_DESCRIPTION("HMC5843 driver");
634 MODULE_LICENSE("GPL");
636 module_init(hmc5843_init
);
637 module_exit(hmc5843_exit
);