2010-07-17 15:02:52 +02:00
|
|
|
/* Copyright (C) 2010 Texas Instruments
|
|
|
|
Author: Shubhrajyoti Datta <shubhrajyoti@ti.com>
|
2012-09-02 22:34:59 +02:00
|
|
|
Acknowledgement: Jonathan Cameron <jic23@kernel.org> for valuable inputs.
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2012-05-17 02:28:13 +02:00
|
|
|
Support for HMC5883 and HMC5883L by Peter Meerwald <pmeerw@pmeerw.net>.
|
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/i2c.h>
|
2012-04-25 16:54:58 +02:00
|
|
|
#include <linux/iio/iio.h>
|
|
|
|
#include <linux/iio/sysfs.h>
|
2013-07-27 17:31:00 +02:00
|
|
|
#include <linux/delay.h>
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
#define HMC5843_CONFIG_REG_A 0x00
|
|
|
|
#define HMC5843_CONFIG_REG_B 0x01
|
|
|
|
#define HMC5843_MODE_REG 0x02
|
|
|
|
#define HMC5843_DATA_OUT_X_MSB_REG 0x03
|
|
|
|
#define HMC5843_DATA_OUT_X_LSB_REG 0x04
|
|
|
|
#define HMC5843_DATA_OUT_Y_MSB_REG 0x05
|
|
|
|
#define HMC5843_DATA_OUT_Y_LSB_REG 0x06
|
|
|
|
#define HMC5843_DATA_OUT_Z_MSB_REG 0x07
|
|
|
|
#define HMC5843_DATA_OUT_Z_LSB_REG 0x08
|
2012-05-16 18:22:05 +02:00
|
|
|
/* Beware: Y and Z are exchanged on HMC5883 */
|
|
|
|
#define HMC5883_DATA_OUT_Z_MSB_REG 0x05
|
|
|
|
#define HMC5883_DATA_OUT_Z_LSB_REG 0x06
|
|
|
|
#define HMC5883_DATA_OUT_Y_MSB_REG 0x07
|
|
|
|
#define HMC5883_DATA_OUT_Y_LSB_REG 0x08
|
2010-07-17 15:02:52 +02:00
|
|
|
#define HMC5843_STATUS_REG 0x09
|
|
|
|
|
2012-05-17 02:28:13 +02:00
|
|
|
enum hmc5843_ids {
|
|
|
|
HMC5843_ID,
|
|
|
|
HMC5883_ID,
|
|
|
|
HMC5883L_ID,
|
|
|
|
};
|
2012-05-16 18:22:05 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
/*
|
2012-05-16 18:22:05 +02:00
|
|
|
* Range gain settings in (+-)Ga
|
|
|
|
* Beware: HMC5843 and HMC5883 have different recommended sensor field
|
|
|
|
* ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
|
|
|
|
*/
|
|
|
|
#define HMC5843_RANGE_GAIN_OFFSET 0x05
|
|
|
|
#define HMC5843_RANGE_GAIN_DEFAULT 0x01
|
|
|
|
#define HMC5843_RANGE_GAIN_MAX 0x07
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Device status
|
|
|
|
*/
|
2012-05-16 18:22:05 +02:00
|
|
|
#define HMC5843_DATA_READY 0x01
|
|
|
|
#define HMC5843_DATA_OUTPUT_LOCK 0x02
|
|
|
|
/* Does not exist on HMC5883, not used */
|
|
|
|
#define HMC5843_VOLTAGE_REGULATOR_ENABLED 0x04
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mode register configuration
|
|
|
|
*/
|
2012-05-16 18:22:05 +02:00
|
|
|
#define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
|
|
|
|
#define HMC5843_MODE_CONVERSION_SINGLE 0x01
|
|
|
|
#define HMC5843_MODE_IDLE 0x02
|
|
|
|
#define HMC5843_MODE_SLEEP 0x03
|
|
|
|
#define HMC5843_MODE_MASK 0x03
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HMC5843: Minimum data output rate
|
|
|
|
* HMC5883: Typical data output rate
|
|
|
|
*/
|
|
|
|
#define HMC5843_RATE_OFFSET 0x02
|
2013-09-19 21:35:00 +02:00
|
|
|
#define HMC5843_RATE_DEFAULT 0x04
|
2012-05-16 18:22:05 +02:00
|
|
|
#define HMC5843_RATE_BITMASK 0x1C
|
|
|
|
#define HMC5843_RATE_NOT_USED 0x07
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
/*
|
2012-05-16 18:22:05 +02:00
|
|
|
* Device measurement configuration
|
2010-07-17 15:02:52 +02:00
|
|
|
*/
|
2012-05-16 18:22:05 +02:00
|
|
|
#define HMC5843_MEAS_CONF_NORMAL 0x00
|
|
|
|
#define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
|
|
|
|
#define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
|
|
|
|
#define HMC5843_MEAS_CONF_NOT_USED 0x03
|
|
|
|
#define HMC5843_MEAS_CONF_MASK 0x03
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2012-05-16 18:22:05 +02:00
|
|
|
/*
|
|
|
|
* Scaling factors: 10000000/Gain
|
|
|
|
*/
|
|
|
|
static const int hmc5843_regval_to_nanoscale[] = {
|
2011-08-12 18:47:58 +02:00
|
|
|
6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
|
2010-07-17 15:02:52 +02:00
|
|
|
};
|
2011-08-12 18:47:58 +02:00
|
|
|
|
2012-05-17 02:28:13 +02:00
|
|
|
static const int hmc5883_regval_to_nanoscale[] = {
|
|
|
|
7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int hmc5883l_regval_to_nanoscale[] = {
|
|
|
|
7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
|
|
|
|
};
|
|
|
|
|
2012-05-17 02:28:10 +02:00
|
|
|
/*
|
2012-05-17 02:28:13 +02:00
|
|
|
* From the HMC5843 datasheet:
|
2012-05-17 02:28:10 +02:00
|
|
|
* Value | Sensor input field range (Ga) | Gain (counts/milli-Gauss)
|
|
|
|
* 0 | (+-)0.7 | 1620
|
|
|
|
* 1 | (+-)1.0 | 1300
|
|
|
|
* 2 | (+-)1.5 | 970
|
|
|
|
* 3 | (+-)2.0 | 780
|
|
|
|
* 4 | (+-)3.2 | 530
|
|
|
|
* 5 | (+-)3.8 | 460
|
|
|
|
* 6 | (+-)4.5 | 390
|
|
|
|
* 7 | (+-)6.5 | 280
|
2012-05-17 02:28:13 +02:00
|
|
|
*
|
|
|
|
* From the HMC5883 datasheet:
|
|
|
|
* Value | Recommended sensor field range (Ga) | Gain (counts/Gauss)
|
|
|
|
* 0 | (+-)0.9 | 1280
|
|
|
|
* 1 | (+-)1.2 | 1024
|
|
|
|
* 2 | (+-)1.9 | 768
|
|
|
|
* 3 | (+-)2.5 | 614
|
|
|
|
* 4 | (+-)4.0 | 415
|
|
|
|
* 5 | (+-)4.6 | 361
|
|
|
|
* 6 | (+-)5.5 | 307
|
|
|
|
* 7 | (+-)7.9 | 219
|
|
|
|
*
|
|
|
|
* From the HMC5883L datasheet:
|
|
|
|
* Value | Recommended sensor field range (Ga) | Gain (LSB/Gauss)
|
|
|
|
* 0 | (+-)0.88 | 1370
|
|
|
|
* 1 | (+-)1.3 | 1090
|
|
|
|
* 2 | (+-)1.9 | 820
|
|
|
|
* 3 | (+-)2.5 | 660
|
|
|
|
* 4 | (+-)4.0 | 440
|
|
|
|
* 5 | (+-)4.7 | 390
|
|
|
|
* 6 | (+-)5.6 | 330
|
|
|
|
* 7 | (+-)8.1 | 230
|
2012-05-17 02:28:10 +02:00
|
|
|
*/
|
2012-05-17 02:28:11 +02:00
|
|
|
static const int hmc5843_regval_to_input_field_mga[] = {
|
2012-05-17 02:28:13 +02:00
|
|
|
700, 1000, 1500, 2000, 3200, 3800, 4500, 6500
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int hmc5883_regval_to_input_field_mga[] = {
|
|
|
|
900, 1200, 1900, 2500, 4000, 4600, 5500, 7900
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int hmc5883l_regval_to_input_field_mga[] = {
|
|
|
|
880, 1300, 1900, 2500, 4000, 4700, 5600, 8100
|
2010-07-17 15:02:52 +02:00
|
|
|
};
|
2012-05-17 02:28:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* From the datasheet:
|
2012-05-17 02:28:13 +02:00
|
|
|
* Value | HMC5843 | HMC5883/HMC5883L
|
|
|
|
* | Data output rate (Hz) | Data output rate (Hz)
|
|
|
|
* 0 | 0.5 | 0.75
|
|
|
|
* 1 | 1 | 1.5
|
|
|
|
* 2 | 2 | 3
|
|
|
|
* 3 | 5 | 7.5
|
|
|
|
* 4 | 10 (default) | 15
|
|
|
|
* 5 | 20 | 30
|
|
|
|
* 6 | 50 | 75
|
|
|
|
* 7 | Not used | Not used
|
2012-05-17 02:28:10 +02:00
|
|
|
*/
|
2012-05-17 02:28:11 +02:00
|
|
|
static const char * const hmc5843_regval_to_sample_freq[] = {
|
2012-05-17 02:28:13 +02:00
|
|
|
"0.5", "1", "2", "5", "10", "20", "50",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const hmc5883_regval_to_sample_freq[] = {
|
|
|
|
"0.75", "1.5", "3", "7.5", "15", "30", "75",
|
2010-07-17 15:02:52 +02:00
|
|
|
};
|
|
|
|
|
2012-05-17 02:28:13 +02:00
|
|
|
/* Describe chip variants */
|
|
|
|
struct hmc5843_chip_info {
|
|
|
|
const struct iio_chan_spec *channels;
|
|
|
|
const char * const *regval_to_sample_freq;
|
|
|
|
const int *regval_to_input_field_mga;
|
|
|
|
const int *regval_to_nanoscale;
|
|
|
|
};
|
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
/* Each client has this additional data */
|
|
|
|
struct hmc5843_data {
|
2013-09-19 21:35:00 +02:00
|
|
|
struct i2c_client *client;
|
2010-07-17 15:02:52 +02:00
|
|
|
struct mutex lock;
|
2012-05-17 02:28:10 +02:00
|
|
|
u8 rate;
|
|
|
|
u8 meas_conf;
|
|
|
|
u8 operating_mode;
|
|
|
|
u8 range;
|
2012-05-17 02:28:13 +02:00
|
|
|
const struct hmc5843_chip_info *variant;
|
2010-07-17 15:02:52 +02:00
|
|
|
};
|
|
|
|
|
2012-05-17 02:28:10 +02:00
|
|
|
/* The lower two bits contain the current conversion mode */
|
2010-07-17 15:02:52 +02:00
|
|
|
static s32 hmc5843_configure(struct i2c_client *client,
|
|
|
|
u8 operating_mode)
|
|
|
|
{
|
|
|
|
return i2c_smbus_write_byte_data(client,
|
|
|
|
HMC5843_MODE_REG,
|
2012-05-16 18:22:05 +02:00
|
|
|
operating_mode & HMC5843_MODE_MASK);
|
2010-07-17 15:02:52 +02:00
|
|
|
}
|
|
|
|
|
2012-03-01 07:00:38 +01:00
|
|
|
/* Return the measurement value from the specified channel */
|
2013-09-19 21:35:00 +02:00
|
|
|
static int hmc5843_read_measurement(struct hmc5843_data *data,
|
|
|
|
int address, int *val)
|
2010-07-17 15:02:52 +02:00
|
|
|
{
|
|
|
|
s32 result;
|
2013-07-27 17:31:00 +02:00
|
|
|
int tries = 150;
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
2013-07-27 17:31:00 +02:00
|
|
|
while (tries-- > 0) {
|
2013-09-19 21:35:00 +02:00
|
|
|
result = i2c_smbus_read_byte_data(data->client,
|
2013-07-27 17:31:00 +02:00
|
|
|
HMC5843_STATUS_REG);
|
|
|
|
if (result & HMC5843_DATA_READY)
|
|
|
|
break;
|
|
|
|
msleep(20);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tries < 0) {
|
2013-09-19 21:35:00 +02:00
|
|
|
dev_err(&data->client->dev, "data not ready\n");
|
2013-07-27 17:31:00 +02:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2013-09-19 21:35:00 +02:00
|
|
|
result = i2c_smbus_read_word_swapped(data->client, address);
|
2010-07-17 15:02:52 +02:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
if (result < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2013-09-03 03:05:00 +02:00
|
|
|
*val = sign_extend32(result, 15);
|
2011-08-12 18:47:58 +02:00
|
|
|
return IIO_VAL_INT;
|
2010-07-17 15:02:52 +02:00
|
|
|
}
|
2011-08-12 18:47:58 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
/*
|
2012-05-17 02:28:10 +02:00
|
|
|
* From the datasheet:
|
2010-07-17 15:02:52 +02:00
|
|
|
* 0 - Continuous-Conversion Mode: In continuous-conversion mode, the
|
2012-05-17 02:28:10 +02:00
|
|
|
* device continuously performs conversions and places the result in
|
|
|
|
* the data register.
|
2010-07-17 15:02:52 +02:00
|
|
|
*
|
2012-05-17 02:28:10 +02:00
|
|
|
* 1 - Single-Conversion Mode : Device performs a single measurement,
|
|
|
|
* sets RDY high and returns to sleep mode.
|
2010-07-17 15:02:52 +02:00
|
|
|
*
|
2012-05-17 02:28:10 +02:00
|
|
|
* 2 - Idle Mode : Device is placed in idle mode.
|
2010-07-17 15:02:52 +02:00
|
|
|
*
|
2012-05-17 02:28:10 +02:00
|
|
|
* 3 - Sleep Mode : Device is placed in sleep mode.
|
2010-07-17 15:02:52 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static ssize_t hmc5843_show_operating_mode(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2012-05-12 15:39:52 +02:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2011-06-27 14:07:56 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
return sprintf(buf, "%d\n", data->operating_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t hmc5843_set_operating_mode(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf,
|
|
|
|
size_t count)
|
|
|
|
{
|
2012-05-12 15:39:52 +02:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
|
2011-06-27 14:07:56 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
|
|
|
|
unsigned long operating_mode = 0;
|
|
|
|
s32 status;
|
|
|
|
int error;
|
2012-05-17 02:28:10 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
mutex_lock(&data->lock);
|
2012-05-16 18:22:04 +02:00
|
|
|
error = kstrtoul(buf, 10, &operating_mode);
|
2010-08-04 21:54:26 +02:00
|
|
|
if (error) {
|
|
|
|
count = error;
|
|
|
|
goto exit;
|
|
|
|
}
|
2012-05-17 02:28:10 +02:00
|
|
|
dev_dbg(dev, "set conversion mode to %lu\n", operating_mode);
|
2012-05-16 18:22:05 +02:00
|
|
|
if (operating_mode > HMC5843_MODE_SLEEP) {
|
2010-08-04 21:54:26 +02:00
|
|
|
count = -EINVAL;
|
|
|
|
goto exit;
|
|
|
|
}
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
status = i2c_smbus_write_byte_data(client, this_attr->address,
|
|
|
|
operating_mode);
|
|
|
|
if (status) {
|
|
|
|
count = -EINVAL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
data->operating_mode = operating_mode;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
|
|
}
|
2012-05-17 02:28:10 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
static IIO_DEVICE_ATTR(operating_mode,
|
|
|
|
S_IWUSR | S_IRUGO,
|
|
|
|
hmc5843_show_operating_mode,
|
|
|
|
hmc5843_set_operating_mode,
|
|
|
|
HMC5843_MODE_REG);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* API for setting the measurement configuration to
|
|
|
|
* Normal, Positive bias and Negative bias
|
|
|
|
*
|
2012-05-17 02:28:10 +02:00
|
|
|
* From the datasheet:
|
|
|
|
* 0 - Normal measurement configuration (default): In normal measurement
|
|
|
|
* configuration the device follows normal measurement flow. Pins BP
|
|
|
|
* and BN are left floating and high impedance.
|
2010-07-17 15:02:52 +02:00
|
|
|
*
|
2012-05-17 02:28:10 +02:00
|
|
|
* 1 - Positive bias configuration: In positive bias configuration, a
|
|
|
|
* positive current is forced across the resistive load on pins BP
|
|
|
|
* and BN.
|
2010-07-17 15:02:52 +02:00
|
|
|
*
|
2012-05-17 02:28:10 +02:00
|
|
|
* 2 - Negative bias configuration. In negative bias configuration, a
|
|
|
|
* negative current is forced across the resistive load on pins BP
|
|
|
|
* and BN.
|
2010-07-17 15:02:52 +02:00
|
|
|
*
|
|
|
|
*/
|
2013-09-19 21:35:00 +02:00
|
|
|
static s32 hmc5843_set_meas_conf(struct hmc5843_data *data, u8 meas_conf)
|
2010-07-17 15:02:52 +02:00
|
|
|
{
|
|
|
|
u8 reg_val;
|
2012-05-16 18:22:05 +02:00
|
|
|
reg_val = (meas_conf & HMC5843_MEAS_CONF_MASK) |
|
|
|
|
(data->rate << HMC5843_RATE_OFFSET);
|
2013-09-19 21:35:00 +02:00
|
|
|
return i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
|
|
|
|
reg_val);
|
2010-07-17 15:02:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2012-05-12 15:39:52 +02:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2011-06-27 14:07:56 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
return sprintf(buf, "%d\n", data->meas_conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf,
|
|
|
|
size_t count)
|
|
|
|
{
|
2012-05-12 15:39:52 +02:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2012-05-16 18:22:03 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
unsigned long meas_conf = 0;
|
2012-05-17 02:28:09 +02:00
|
|
|
int error;
|
|
|
|
|
|
|
|
error = kstrtoul(buf, 10, &meas_conf);
|
2010-07-17 15:02:52 +02:00
|
|
|
if (error)
|
|
|
|
return error;
|
2012-05-17 02:28:09 +02:00
|
|
|
if (meas_conf >= HMC5843_MEAS_CONF_NOT_USED)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
mutex_lock(&data->lock);
|
2012-05-17 02:28:10 +02:00
|
|
|
dev_dbg(dev, "set measurement configuration to %lu\n", meas_conf);
|
2013-09-19 21:35:00 +02:00
|
|
|
if (hmc5843_set_meas_conf(data, meas_conf)) {
|
2010-07-17 15:02:52 +02:00
|
|
|
count = -EINVAL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
data->meas_conf = meas_conf;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
|
|
}
|
2012-05-17 02:28:10 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
static IIO_DEVICE_ATTR(meas_conf,
|
|
|
|
S_IWUSR | S_IRUGO,
|
|
|
|
hmc5843_show_measurement_configuration,
|
|
|
|
hmc5843_set_measurement_configuration,
|
|
|
|
0);
|
|
|
|
|
2012-05-17 02:28:13 +02:00
|
|
|
static ssize_t hmc5843_show_sampling_frequencies_available(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
|
|
|
ssize_t total_n = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
|
|
|
|
ssize_t n = sprintf(buf, "%s ", data->variant->regval_to_sample_freq[i]);
|
|
|
|
buf += n;
|
|
|
|
total_n += n;
|
|
|
|
}
|
|
|
|
/* replace trailing space by newline */
|
|
|
|
buf[-1] = '\n';
|
|
|
|
|
|
|
|
return total_n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_sampling_frequencies_available);
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2013-09-19 21:35:00 +02:00
|
|
|
static s32 hmc5843_set_rate(struct hmc5843_data *data, u8 rate)
|
2010-07-17 15:02:52 +02:00
|
|
|
{
|
|
|
|
u8 reg_val;
|
|
|
|
|
2012-05-16 18:22:05 +02:00
|
|
|
if (rate >= HMC5843_RATE_NOT_USED) {
|
2013-09-19 21:35:00 +02:00
|
|
|
dev_err(&data->client->dev,
|
2012-05-17 02:28:10 +02:00
|
|
|
"data output rate is not supported\n");
|
2010-07-17 15:02:52 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2012-05-17 02:28:12 +02:00
|
|
|
|
|
|
|
reg_val = data->meas_conf | (rate << HMC5843_RATE_OFFSET);
|
2013-09-19 21:35:00 +02:00
|
|
|
return i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
|
|
|
|
reg_val);
|
2010-07-17 15:02:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-17 02:28:08 +02:00
|
|
|
static int hmc5843_check_sampling_frequency(struct hmc5843_data *data,
|
|
|
|
const char *buf)
|
|
|
|
{
|
2012-05-17 02:28:13 +02:00
|
|
|
const char * const *samp_freq = data->variant->regval_to_sample_freq;
|
2012-05-17 02:28:08 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
|
2012-05-17 02:28:13 +02:00
|
|
|
if (sysfs_streq(buf, samp_freq[i]))
|
2012-05-17 02:28:08 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-05-17 02:28:11 +02:00
|
|
|
static ssize_t hmc5843_set_sampling_frequency(struct device *dev,
|
2010-07-17 15:02:52 +02:00
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
|
2012-05-12 15:39:52 +02:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2011-06-27 14:07:56 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2012-05-17 02:28:08 +02:00
|
|
|
int rate;
|
|
|
|
|
|
|
|
rate = hmc5843_check_sampling_frequency(data, buf);
|
|
|
|
if (rate < 0) {
|
2013-09-19 21:35:00 +02:00
|
|
|
dev_err(&data->client->dev,
|
2012-05-17 02:28:08 +02:00
|
|
|
"sampling frequency is not supported\n");
|
|
|
|
return rate;
|
|
|
|
}
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
2012-05-17 02:28:08 +02:00
|
|
|
dev_dbg(dev, "set rate to %d\n", rate);
|
2013-09-19 21:35:00 +02:00
|
|
|
if (hmc5843_set_rate(data, rate)) {
|
2010-07-17 15:02:52 +02:00
|
|
|
count = -EINVAL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
data->rate = rate;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-05-17 02:28:11 +02:00
|
|
|
static ssize_t hmc5843_show_sampling_frequency(struct device *dev,
|
2010-07-17 15:02:52 +02:00
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2012-05-12 15:39:52 +02:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
|
2012-05-17 02:28:13 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2010-08-10 07:37:33 +02:00
|
|
|
s32 rate;
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2013-09-19 21:35:00 +02:00
|
|
|
rate = i2c_smbus_read_byte_data(data->client, this_attr->address);
|
2010-07-17 15:02:52 +02:00
|
|
|
if (rate < 0)
|
2010-08-10 07:37:33 +02:00
|
|
|
return rate;
|
2012-05-16 18:22:05 +02:00
|
|
|
rate = (rate & HMC5843_RATE_BITMASK) >> HMC5843_RATE_OFFSET;
|
2012-05-17 02:28:13 +02:00
|
|
|
return sprintf(buf, "%s\n", data->variant->regval_to_sample_freq[rate]);
|
2010-07-17 15:02:52 +02:00
|
|
|
}
|
2012-05-17 02:28:10 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
static IIO_DEVICE_ATTR(sampling_frequency,
|
|
|
|
S_IWUSR | S_IRUGO,
|
2012-05-17 02:28:11 +02:00
|
|
|
hmc5843_show_sampling_frequency,
|
|
|
|
hmc5843_set_sampling_frequency,
|
2010-07-17 15:02:52 +02:00
|
|
|
HMC5843_CONFIG_REG_A);
|
|
|
|
|
2012-05-17 02:28:11 +02:00
|
|
|
static ssize_t hmc5843_show_range_gain(struct device *dev,
|
2010-07-17 15:02:52 +02:00
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
u8 range;
|
2012-05-12 15:39:52 +02:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2011-06-27 14:07:56 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
range = data->range;
|
2012-05-17 02:28:13 +02:00
|
|
|
return sprintf(buf, "%d\n", data->variant->regval_to_input_field_mga[range]);
|
2010-07-17 15:02:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-17 02:28:11 +02:00
|
|
|
static ssize_t hmc5843_set_range_gain(struct device *dev,
|
2010-07-17 15:02:52 +02:00
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf,
|
|
|
|
size_t count)
|
|
|
|
{
|
2012-05-12 15:39:52 +02:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
|
2011-06-27 14:07:56 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
unsigned long range = 0;
|
|
|
|
int error;
|
2012-05-17 02:28:10 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
mutex_lock(&data->lock);
|
2012-05-16 18:22:04 +02:00
|
|
|
error = kstrtoul(buf, 10, &range);
|
2010-08-04 21:54:26 +02:00
|
|
|
if (error) {
|
|
|
|
count = error;
|
|
|
|
goto exit;
|
|
|
|
}
|
2010-07-17 15:02:52 +02:00
|
|
|
dev_dbg(dev, "set range to %lu\n", range);
|
|
|
|
|
2012-05-16 18:22:05 +02:00
|
|
|
if (range > HMC5843_RANGE_GAIN_MAX) {
|
2010-08-04 21:54:26 +02:00
|
|
|
count = -EINVAL;
|
|
|
|
goto exit;
|
|
|
|
}
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
data->range = range;
|
2012-05-16 18:22:05 +02:00
|
|
|
range = range << HMC5843_RANGE_GAIN_OFFSET;
|
2013-09-19 21:35:00 +02:00
|
|
|
if (i2c_smbus_write_byte_data(data->client, this_attr->address, range))
|
2010-07-17 15:02:52 +02:00
|
|
|
count = -EINVAL;
|
|
|
|
|
2010-08-04 21:54:26 +02:00
|
|
|
exit:
|
2010-07-17 15:02:52 +02:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
|
|
}
|
2012-05-17 02:28:10 +02:00
|
|
|
|
2011-09-14 14:01:23 +02:00
|
|
|
static IIO_DEVICE_ATTR(in_magn_range,
|
2010-07-17 15:02:52 +02:00
|
|
|
S_IWUSR | S_IRUGO,
|
2012-05-17 02:28:11 +02:00
|
|
|
hmc5843_show_range_gain,
|
|
|
|
hmc5843_set_range_gain,
|
2010-07-17 15:02:52 +02:00
|
|
|
HMC5843_CONFIG_REG_B);
|
|
|
|
|
2011-08-12 18:47:58 +02:00
|
|
|
static int hmc5843_read_raw(struct iio_dev *indio_dev,
|
|
|
|
struct iio_chan_spec const *chan,
|
|
|
|
int *val, int *val2,
|
|
|
|
long mask)
|
2010-07-17 15:02:52 +02:00
|
|
|
{
|
2011-06-27 14:07:56 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(indio_dev);
|
2011-08-12 18:47:58 +02:00
|
|
|
|
|
|
|
switch (mask) {
|
2012-04-15 18:41:23 +02:00
|
|
|
case IIO_CHAN_INFO_RAW:
|
2013-09-19 21:35:00 +02:00
|
|
|
return hmc5843_read_measurement(data, chan->address, val);
|
2011-10-26 18:41:36 +02:00
|
|
|
case IIO_CHAN_INFO_SCALE:
|
2011-08-12 18:47:58 +02:00
|
|
|
*val = 0;
|
2012-05-17 02:28:13 +02:00
|
|
|
*val2 = data->variant->regval_to_nanoscale[data->range];
|
2011-08-12 18:47:58 +02:00
|
|
|
return IIO_VAL_INT_PLUS_NANO;
|
2012-09-28 11:57:00 +02:00
|
|
|
}
|
2011-08-12 18:47:58 +02:00
|
|
|
return -EINVAL;
|
2010-07-17 15:02:52 +02:00
|
|
|
}
|
2011-08-12 18:47:58 +02:00
|
|
|
|
2013-07-27 17:31:00 +02:00
|
|
|
#define HMC5843_CHANNEL(axis, addr) \
|
2011-08-12 18:47:58 +02:00
|
|
|
{ \
|
|
|
|
.type = IIO_MAGN, \
|
|
|
|
.modified = 1, \
|
|
|
|
.channel2 = IIO_MOD_##axis, \
|
2013-02-27 20:40:58 +01:00
|
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
|
|
|
|
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
|
2013-07-27 17:31:00 +02:00
|
|
|
.address = addr \
|
2011-08-12 18:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct iio_chan_spec hmc5843_channels[] = {
|
|
|
|
HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
|
|
|
|
HMC5843_CHANNEL(Y, HMC5843_DATA_OUT_Y_MSB_REG),
|
|
|
|
HMC5843_CHANNEL(Z, HMC5843_DATA_OUT_Z_MSB_REG),
|
|
|
|
};
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2012-05-17 02:28:13 +02:00
|
|
|
static const struct iio_chan_spec hmc5883_channels[] = {
|
|
|
|
HMC5843_CHANNEL(X, HMC5843_DATA_OUT_X_MSB_REG),
|
|
|
|
HMC5843_CHANNEL(Y, HMC5883_DATA_OUT_Y_MSB_REG),
|
|
|
|
HMC5843_CHANNEL(Z, HMC5883_DATA_OUT_Z_MSB_REG),
|
|
|
|
};
|
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
static struct attribute *hmc5843_attributes[] = {
|
|
|
|
&iio_dev_attr_meas_conf.dev_attr.attr,
|
|
|
|
&iio_dev_attr_operating_mode.dev_attr.attr,
|
|
|
|
&iio_dev_attr_sampling_frequency.dev_attr.attr,
|
2011-09-14 14:01:23 +02:00
|
|
|
&iio_dev_attr_in_magn_range.dev_attr.attr,
|
2012-05-17 02:28:13 +02:00
|
|
|
&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
|
2010-07-17 15:02:52 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group hmc5843_group = {
|
|
|
|
.attrs = hmc5843_attributes,
|
|
|
|
};
|
|
|
|
|
2012-05-17 02:28:13 +02:00
|
|
|
static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
|
|
|
|
[HMC5843_ID] = {
|
|
|
|
.channels = hmc5843_channels,
|
|
|
|
.regval_to_sample_freq = hmc5843_regval_to_sample_freq,
|
|
|
|
.regval_to_input_field_mga =
|
|
|
|
hmc5843_regval_to_input_field_mga,
|
|
|
|
.regval_to_nanoscale = hmc5843_regval_to_nanoscale,
|
|
|
|
},
|
|
|
|
[HMC5883_ID] = {
|
|
|
|
.channels = hmc5883_channels,
|
|
|
|
.regval_to_sample_freq = hmc5883_regval_to_sample_freq,
|
|
|
|
.regval_to_input_field_mga =
|
|
|
|
hmc5883_regval_to_input_field_mga,
|
|
|
|
.regval_to_nanoscale = hmc5883_regval_to_nanoscale,
|
|
|
|
},
|
|
|
|
[HMC5883L_ID] = {
|
|
|
|
.channels = hmc5883_channels,
|
|
|
|
.regval_to_sample_freq = hmc5883_regval_to_sample_freq,
|
|
|
|
.regval_to_input_field_mga =
|
|
|
|
hmc5883l_regval_to_input_field_mga,
|
|
|
|
.regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-09-19 21:35:00 +02:00
|
|
|
static void hmc5843_init(struct hmc5843_data *data)
|
2010-07-17 15:02:52 +02:00
|
|
|
{
|
2013-09-19 21:35:00 +02:00
|
|
|
hmc5843_set_meas_conf(data, HMC5843_MEAS_CONF_NORMAL);
|
|
|
|
hmc5843_set_rate(data, HMC5843_RATE_DEFAULT);
|
|
|
|
hmc5843_configure(data->client, HMC5843_MODE_CONVERSION_CONTINUOUS);
|
|
|
|
i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_B,
|
|
|
|
HMC5843_RANGE_GAIN_DEFAULT);
|
2010-07-17 15:02:52 +02:00
|
|
|
}
|
|
|
|
|
2011-05-18 15:42:37 +02:00
|
|
|
static const struct iio_info hmc5843_info = {
|
|
|
|
.attrs = &hmc5843_group,
|
2011-08-12 18:47:58 +02:00
|
|
|
.read_raw = &hmc5843_read_raw,
|
2011-05-18 15:42:37 +02:00
|
|
|
.driver_module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2012-11-19 19:21:57 +01:00
|
|
|
static int hmc5843_probe(struct i2c_client *client,
|
2010-07-17 15:02:52 +02:00
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
|
|
|
struct hmc5843_data *data;
|
2011-06-27 14:07:56 +02:00
|
|
|
struct iio_dev *indio_dev;
|
2010-07-17 15:02:52 +02:00
|
|
|
int err = 0;
|
|
|
|
|
2013-09-03 03:05:00 +02:00
|
|
|
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
|
|
|
|
if (indio_dev == NULL)
|
|
|
|
return -ENOMEM;
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2012-05-16 18:22:05 +02:00
|
|
|
/* default settings at probe */
|
|
|
|
data = iio_priv(indio_dev);
|
2013-09-19 21:35:00 +02:00
|
|
|
data->client = client;
|
2013-09-19 21:35:00 +02:00
|
|
|
data->variant = &hmc5843_chip_info_tbl[id->driver_data];
|
|
|
|
mutex_init(&data->lock);
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2011-06-27 14:07:56 +02:00
|
|
|
i2c_set_clientdata(client, indio_dev);
|
|
|
|
indio_dev->info = &hmc5843_info;
|
2011-08-12 18:47:58 +02:00
|
|
|
indio_dev->name = id->name;
|
2011-06-27 14:07:56 +02:00
|
|
|
indio_dev->dev.parent = &client->dev;
|
|
|
|
indio_dev->modes = INDIO_DIRECT_MODE;
|
2013-09-19 21:35:00 +02:00
|
|
|
indio_dev->channels = data->variant->channels;
|
|
|
|
indio_dev->num_channels = 3;
|
|
|
|
|
|
|
|
hmc5843_init(data);
|
2012-05-17 02:28:10 +02:00
|
|
|
|
2011-06-27 14:07:56 +02:00
|
|
|
err = iio_device_register(indio_dev);
|
2010-07-17 15:02:52 +02:00
|
|
|
if (err)
|
2013-09-03 03:05:00 +02:00
|
|
|
return err;
|
2012-05-17 02:28:10 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-19 19:26:37 +01:00
|
|
|
static int hmc5843_remove(struct i2c_client *client)
|
2010-07-17 15:02:52 +02:00
|
|
|
{
|
2013-09-03 03:05:00 +02:00
|
|
|
iio_device_unregister(i2c_get_clientdata(client));
|
2010-07-17 15:02:52 +02:00
|
|
|
/* sleep mode to save power */
|
2012-05-16 18:22:05 +02:00
|
|
|
hmc5843_configure(client, HMC5843_MODE_SLEEP);
|
2011-06-27 14:07:56 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-20 19:37:05 +01:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
static int hmc5843_suspend(struct device *dev)
|
2010-07-17 15:02:52 +02:00
|
|
|
{
|
2012-05-16 18:22:05 +02:00
|
|
|
hmc5843_configure(to_i2c_client(dev), HMC5843_MODE_SLEEP);
|
2013-09-03 03:05:00 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-20 19:37:05 +01:00
|
|
|
static int hmc5843_resume(struct device *dev)
|
2010-07-17 15:02:52 +02:00
|
|
|
{
|
2013-09-19 21:35:00 +02:00
|
|
|
struct hmc5843_data *data = iio_priv(i2c_get_clientdata(
|
|
|
|
to_i2c_client(dev)));
|
2012-05-16 18:22:03 +02:00
|
|
|
|
2013-09-19 21:35:00 +02:00
|
|
|
hmc5843_configure(data->client, data->operating_mode);
|
2012-05-16 18:22:03 +02:00
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-20 19:37:05 +01:00
|
|
|
static SIMPLE_DEV_PM_OPS(hmc5843_pm_ops, hmc5843_suspend, hmc5843_resume);
|
|
|
|
#define HMC5843_PM_OPS (&hmc5843_pm_ops)
|
|
|
|
#else
|
|
|
|
#define HMC5843_PM_OPS NULL
|
|
|
|
#endif
|
|
|
|
|
2010-07-17 15:02:52 +02:00
|
|
|
static const struct i2c_device_id hmc5843_id[] = {
|
2012-05-17 02:28:13 +02:00
|
|
|
{ "hmc5843", HMC5843_ID },
|
|
|
|
{ "hmc5883", HMC5883_ID },
|
|
|
|
{ "hmc5883l", HMC5883L_ID },
|
2010-07-17 15:02:52 +02:00
|
|
|
{ }
|
|
|
|
};
|
2011-11-16 08:53:31 +01:00
|
|
|
MODULE_DEVICE_TABLE(i2c, hmc5843_id);
|
2010-07-17 15:02:52 +02:00
|
|
|
|
|
|
|
static struct i2c_driver hmc5843_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "hmc5843",
|
2012-02-20 19:37:05 +01:00
|
|
|
.pm = HMC5843_PM_OPS,
|
2010-07-17 15:02:52 +02:00
|
|
|
},
|
|
|
|
.id_table = hmc5843_id,
|
|
|
|
.probe = hmc5843_probe,
|
2012-11-19 19:21:38 +01:00
|
|
|
.remove = hmc5843_remove,
|
2010-07-17 15:02:52 +02:00
|
|
|
};
|
2011-11-16 10:13:38 +01:00
|
|
|
module_i2c_driver(hmc5843_driver);
|
2010-07-17 15:02:52 +02:00
|
|
|
|
2013-09-05 11:29:00 +02:00
|
|
|
MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>");
|
2012-05-17 02:28:13 +02:00
|
|
|
MODULE_DESCRIPTION("HMC5843/5883/5883L driver");
|
2010-07-17 15:02:52 +02:00
|
|
|
MODULE_LICENSE("GPL");
|