Commit 4fd24247 authored by Jonathan Cameron's avatar Jonathan Cameron Committed by Greg Kroah-Hartman

staging:iio:adc:ad7291 bring into line with current abi + chan_spec conversion.

Also add some locking.

Some major changes to how this driver works.

For voltage channels it is currently either in single read mode or in
a monitor mode (events only). Could be much cleverer and allow
reading of any channels that happend to be monitored, but haven't
done that yet.
Signed-off-by: default avatarJonathan Cameron <jic23@cam.ac.uk>
Acked-by: default avatarMichael Hennerich <Michael.Hennerich@analog.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent fe55a170
...@@ -7,18 +7,28 @@ ...@@ -7,18 +7,28 @@
*/ */
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/sysfs.h> #include <linux/sysfs.h>
#include <linux/list.h>
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/mutex.h>
#include "../iio.h" #include "../iio.h"
#include "../sysfs.h" #include "../sysfs.h"
/*
* Simplified handling
*
* If no events enabled - single polled channel read
* If event enabled direct reads disable unless channel
* is in the read mask.
*
* The noise-delayed bit as per datasheet suggestion is always enabled.
*
* Extref control should be based on regulator provision - not handled.
*/
/* /*
* AD7291 registers definition * AD7291 registers definition
*/ */
...@@ -56,29 +66,13 @@ ...@@ -56,29 +66,13 @@
#define AD7291_T_VALUE_FLOAT_OFFSET 2 #define AD7291_T_VALUE_FLOAT_OFFSET 2
#define AD7291_T_VALUE_FLOAT_MASK 0x2 #define AD7291_T_VALUE_FLOAT_MASK 0x2
/*
* struct ad7291_chip_info - chip specifc information
*/
struct ad7291_chip_info { struct ad7291_chip_info {
struct i2c_client *client; struct i2c_client *client;
u16 command; u16 command;
u8 channels; /* Active voltage channels */ u8 c_mask; /* Active voltage channels for events */
}; struct mutex state_lock;
/*
* struct ad7291_chip_info - chip specifc information
*/
struct ad7291_limit_regs {
u16 data_high;
u16 data_low;
u16 hysteresis;
}; };
/*
* ad7291 register access by I2C
*/
static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data) static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
{ {
struct i2c_client *client = chip->client; struct i2c_client *client = chip->client;
...@@ -97,111 +91,9 @@ static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data) ...@@ -97,111 +91,9 @@ static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data) static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
{ {
struct i2c_client *client = chip->client; return i2c_smbus_write_word_data(chip->client, reg, swab16(data));
int ret = 0;
ret = i2c_smbus_write_word_data(client, reg, swab16(data));
if (ret < 0)
dev_err(&client->dev, "I2C write error\n");
return ret;
}
/* Returns negative errno, or else the number of words read. */
static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data)
{
struct i2c_client *client = chip->client;
u8 commands[4];
int ret = 0;
int i, count;
if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE)
count = 2;
else if (reg == AD7291_VOLTAGE) {
if (!chip->channels) {
dev_err(&client->dev, "No voltage channel is selected.\n");
return -EINVAL;
}
count = 2 + chip->channels * 2;
} else {
dev_err(&client->dev, "I2C wrong data register\n");
return -EINVAL;
}
commands[0] = 0;
commands[1] = (chip->command >> 8) & 0xff;
commands[2] = chip->command & 0xff;
commands[3] = reg;
ret = i2c_master_send(client, commands, 4);
if (ret < 0) {
dev_err(&client->dev, "I2C master send error\n");
return ret;
}
ret = i2c_master_recv(client, (u8 *)data, count);
if (ret < 0) {
dev_err(&client->dev, "I2C master receive error\n");
return ret;
}
ret >>= 2;
for (i = 0; i < ret; i++)
data[i] = swab16(data[i]);
return ret;
}
static ssize_t ad7291_show_mode(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
if (chip->command & AD7291_AUTOCYCLE)
return sprintf(buf, "autocycle\n");
else
return sprintf(buf, "command\n");
}
static ssize_t ad7291_store_mode(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 command;
int ret;
command = chip->command & (~AD7291_AUTOCYCLE);
if (strcmp(buf, "autocycle"))
command |= AD7291_AUTOCYCLE;
ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
if (ret)
return -EIO;
chip->command = command;
return ret;
}
static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
ad7291_show_mode,
ad7291_store_mode,
0);
static ssize_t ad7291_show_available_modes(struct device *dev,
struct device_attribute *attr,
char *buf)
{
return sprintf(buf, "command\nautocycle\n");
} }
static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0);
static ssize_t ad7291_store_reset(struct device *dev, static ssize_t ad7291_store_reset(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
const char *buf, const char *buf,
...@@ -209,240 +101,15 @@ static ssize_t ad7291_store_reset(struct device *dev, ...@@ -209,240 +101,15 @@ static ssize_t ad7291_store_reset(struct device *dev,
{ {
struct iio_dev *dev_info = dev_get_drvdata(dev); struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info); struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 command;
int ret;
command = chip->command | AD7291_RESET;
ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
if (ret)
return -EIO;
return ret;
}
static IIO_DEVICE_ATTR(reset, S_IWUSR,
NULL,
ad7291_store_reset,
0);
static ssize_t ad7291_show_ext_ref(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF));
}
static ssize_t ad7291_store_ext_ref(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 command;
int ret;
command = chip->command & (~AD7291_EXT_REF);
if (strcmp(buf, "1"))
command |= AD7291_EXT_REF;
ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
if (ret)
return -EIO;
chip->command = command;
return ret;
}
static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
ad7291_show_ext_ref,
ad7291_store_ext_ref,
0);
static ssize_t ad7291_show_noise_delay(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY));
}
static ssize_t ad7291_store_noise_delay(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 command;
int ret;
command = chip->command & (~AD7291_NOISE_DELAY);
if (strcmp(buf, "1"))
command |= AD7291_NOISE_DELAY;
ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
if (ret)
return -EIO;
chip->command = command;
return ret;
}
static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR,
ad7291_show_noise_delay,
ad7291_store_noise_delay,
0);
static ssize_t ad7291_show_t_sense(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 data;
char sign = ' ';
int ret;
ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data);
if (ret)
return -EIO;
if (data & AD7291_T_VALUE_SIGN) {
/* convert supplement to positive value */
data = (AD7291_T_VALUE_SIGN << 1) - data;
sign = '-';
}
return sprintf(buf, "%c%d.%.2d\n", sign,
(data >> AD7291_T_VALUE_FLOAT_OFFSET),
(data & AD7291_T_VALUE_FLOAT_MASK) * 25);
}
static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0);
static ssize_t ad7291_show_t_average(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 data;
char sign = ' ';
int ret;
ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data);
if (ret)
return -EIO;
if (data & AD7291_T_VALUE_SIGN) { return ad7291_i2c_write(chip, AD7291_COMMAND,
/* convert supplement to positive value */ chip->command | AD7291_RESET);
data = (AD7291_T_VALUE_SIGN << 1) - data;
sign = '-';
}
return sprintf(buf, "%c%d.%.2d\n", sign,
(data >> AD7291_T_VALUE_FLOAT_OFFSET),
(data & AD7291_T_VALUE_FLOAT_MASK) * 25);
} }
static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0); static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, ad7291_store_reset, 0);
static ssize_t ad7291_show_voltage(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 data[AD7291_VOLTAGE_LIMIT_COUNT];
int i, size, ret;
ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data);
if (ret)
return -EIO;
for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
if (chip->command & (AD7291_T_SENSE_MASK << i)) {
ret = sprintf(buf, "channel[%d]=%d\n", i,
data[i] & AD7291_VALUE_MASK);
if (ret < 0)
break;
buf += ret;
size += ret;
}
}
return size;
}
static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0);
static ssize_t ad7291_show_channel_mask(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >>
AD7291_VOLTAGE_OFFSET);
}
static ssize_t ad7291_store_channel_mask(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 command;
unsigned long data;
int i, ret;
ret = strict_strtoul(buf, 16, &data);
if (ret || data > 0xff)
return -EINVAL;
command = chip->command & (~AD7291_VOLTAGE_MASK);
command |= data << AD7291_VOLTAGE_OFFSET;
ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
if (ret)
return -EIO;
chip->command = command;
for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
if (chip->command & (AD7291_T_SENSE_MASK << i))
chip->channels++;
}
return ret;
}
static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
ad7291_show_channel_mask,
ad7291_store_channel_mask,
0);
static struct attribute *ad7291_attributes[] = { static struct attribute *ad7291_attributes[] = {
&iio_dev_attr_available_modes.dev_attr.attr,
&iio_dev_attr_mode.dev_attr.attr,
&iio_dev_attr_reset.dev_attr.attr, &iio_dev_attr_reset.dev_attr.attr,
&iio_dev_attr_ext_ref.dev_attr.attr,
&iio_dev_attr_noise_delay.dev_attr.attr,
&iio_dev_attr_t_sense.dev_attr.attr,
&iio_dev_attr_t_average.dev_attr.attr,
&iio_dev_attr_voltage.dev_attr.attr,
&iio_dev_attr_channel_mask.dev_attr.attr,
NULL, NULL,
}; };
...@@ -450,10 +117,6 @@ static const struct attribute_group ad7291_attribute_group = { ...@@ -450,10 +117,6 @@ static const struct attribute_group ad7291_attribute_group = {
.attrs = ad7291_attributes, .attrs = ad7291_attributes,
}; };
/*
* temperature bound events
*/
static irqreturn_t ad7291_event_handler(int irq, void *private) static irqreturn_t ad7291_event_handler(int irq, void *private)
{ {
struct iio_dev *indio_dev = private; struct iio_dev *indio_dev = private;
...@@ -478,28 +141,15 @@ static irqreturn_t ad7291_event_handler(int irq, void *private) ...@@ -478,28 +141,15 @@ static irqreturn_t ad7291_event_handler(int irq, void *private)
command = chip->command & ~AD7291_ALART_CLEAR; command = chip->command & ~AD7291_ALART_CLEAR;
ad7291_i2c_write(chip, AD7291_COMMAND, command); ad7291_i2c_write(chip, AD7291_COMMAND, command);
if (t_status & (1 << 0)) /* For now treat t_sense and t_sense_average the same */
iio_push_event(indio_dev, if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
IIO_UNMOD_EVENT_CODE(IIO_TEMP,
0,
IIO_EV_TYPE_THRESH,
IIO_EV_DIR_FALLING),
timestamp);
if (t_status & (1 << 1))
iio_push_event(indio_dev,
IIO_UNMOD_EVENT_CODE(IIO_TEMP,
0,
IIO_EV_TYPE_THRESH,
IIO_EV_DIR_RISING),
timestamp);
if (t_status & (1 << 2))
iio_push_event(indio_dev, iio_push_event(indio_dev,
IIO_UNMOD_EVENT_CODE(IIO_TEMP, IIO_UNMOD_EVENT_CODE(IIO_TEMP,
0, 0,
IIO_EV_TYPE_THRESH, IIO_EV_TYPE_THRESH,
IIO_EV_DIR_FALLING), IIO_EV_DIR_FALLING),
timestamp); timestamp);
if (t_status & (1 << 3)) if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
iio_push_event(indio_dev, iio_push_event(indio_dev,
IIO_UNMOD_EVENT_CODE(IIO_TEMP, IIO_UNMOD_EVENT_CODE(IIO_TEMP,
0, 0,
...@@ -527,7 +177,7 @@ static irqreturn_t ad7291_event_handler(int irq, void *private) ...@@ -527,7 +177,7 @@ static irqreturn_t ad7291_event_handler(int irq, void *private)
return IRQ_HANDLED; return IRQ_HANDLED;
} }
static inline ssize_t ad7291_show_t_bound(struct device *dev, static inline ssize_t ad7291_show_hyst(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
char *buf) char *buf)
{ {
...@@ -535,26 +185,16 @@ static inline ssize_t ad7291_show_t_bound(struct device *dev, ...@@ -535,26 +185,16 @@ static inline ssize_t ad7291_show_t_bound(struct device *dev,
struct ad7291_chip_info *chip = iio_priv(dev_info); struct ad7291_chip_info *chip = iio_priv(dev_info);
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
u16 data; u16 data;
char sign = ' ';
int ret; int ret;
ret = ad7291_i2c_read(chip, this_attr->address, &data); ret = ad7291_i2c_read(chip, this_attr->address, &data);
if (ret) if (ret < 0)
return -EIO; return ret;
data &= AD7291_VALUE_MASK;
if (data & AD7291_T_VALUE_SIGN) {
/* convert supplement to positive value */
data = (AD7291_T_VALUE_SIGN << 1) - data;
sign = '-';
}
return sprintf(buf, "%c%d.%.2d\n", sign, return sprintf(buf, "%d\n", data & 0x0FFF);
data >> AD7291_T_VALUE_FLOAT_OFFSET,
(data & AD7291_T_VALUE_FLOAT_MASK) * 25);
} }
static inline ssize_t ad7291_set_t_bound(struct device *dev, static inline ssize_t ad7291_set_hyst(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
const char *buf, const char *buf,
size_t len) size_t len)
...@@ -562,216 +202,307 @@ static inline ssize_t ad7291_set_t_bound(struct device *dev, ...@@ -562,216 +202,307 @@ static inline ssize_t ad7291_set_t_bound(struct device *dev,
struct iio_dev *dev_info = dev_get_drvdata(dev); struct iio_dev *dev_info = dev_get_drvdata(dev);
struct ad7291_chip_info *chip = iio_priv(dev_info); struct ad7291_chip_info *chip = iio_priv(dev_info);
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
long tmp1, tmp2;
u16 data; u16 data;
char *pos;
int ret; int ret;
pos = strchr(buf, '.'); ret = kstrtou16(buf, 10, &data);
ret = strict_strtol(buf, 10, &tmp1); if (ret < 0)
return ret;
if (ret || tmp1 > 127 || tmp1 < -128) if (data < 4096)
return -EINVAL; return -EINVAL;
if (pos) { return ad7291_i2c_write(chip, this_attr->address, data);
len = strlen(pos); }
if (len > AD7291_T_VALUE_FLOAT_OFFSET)
len = AD7291_T_VALUE_FLOAT_OFFSET;
pos[len] = 0;
ret = strict_strtol(pos, 10, &tmp2);
if (!ret) static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
tmp2 = (tmp2 / 25) * 25; S_IRUGO | S_IWUSR,
} ad7291_show_hyst, ad7291_set_hyst,
AD7291_T_SENSE_HYST);
static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
S_IRUGO | S_IWUSR,
ad7291_show_hyst, ad7291_set_hyst, 0x06);
static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
S_IRUGO | S_IWUSR,
ad7291_show_hyst, ad7291_set_hyst, 0x09);
static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
S_IRUGO | S_IWUSR,
ad7291_show_hyst, ad7291_set_hyst, 0x0C);
static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
S_IRUGO | S_IWUSR,
ad7291_show_hyst, ad7291_set_hyst, 0x0F);
static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
S_IRUGO | S_IWUSR,
ad7291_show_hyst, ad7291_set_hyst, 0x12);
static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
S_IRUGO | S_IWUSR,
ad7291_show_hyst, ad7291_set_hyst, 0x15);
static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
S_IRUGO | S_IWUSR,
ad7291_show_hyst, ad7291_set_hyst, 0x18);
static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
S_IRUGO | S_IWUSR,
ad7291_show_hyst, ad7291_set_hyst, 0x1B);
if (tmp1 < 0) static struct attribute *ad7291_event_attributes[] = {
data = (u16)(-tmp1); &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
else &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
data = (u16)tmp1; &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
data = (data << AD7291_T_VALUE_FLOAT_OFFSET) | &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
(tmp2 & AD7291_T_VALUE_FLOAT_MASK); &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
if (tmp1 < 0) &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
/* convert positive value to supplyment */ &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
data = (AD7291_T_VALUE_SIGN << 1) - data; &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
&iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
ret = ad7291_i2c_write(chip, this_attr->address, data); NULL,
if (ret) };
return -EIO;
return ret; /* high / low */
} static u8 ad7291_limit_regs[9][2] = {
{ 0x04, 0x05 },
{ 0x07, 0x08 },
{ 0x0A, 0x0B },
{ 0x0E, 0x0D }, /* note reversed order */
{ 0x10, 0x11 },
{ 0x13, 0x14 },
{ 0x16, 0x17 },
{ 0x19, 0x1A },
/* temp */
{ 0x1C, 0x1D },
};
static inline ssize_t ad7291_show_v_bound(struct device *dev, static int ad7291_read_event_value(struct iio_dev *indio_dev,
struct device_attribute *attr, u64 event_code,
u8 bound_reg, int *val)
char *buf)
{ {
struct iio_dev *dev_info = dev_get_drvdata(dev); struct ad7291_chip_info *chip = iio_priv(indio_dev);
struct ad7291_chip_info *chip = iio_priv(dev_info);
u16 data;
int ret; int ret;
u8 reg;
u16 uval;
s16 signval;
if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE || switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
bound_reg >= AD7291_VOLTAGE_LIMIT_BASE + case IIO_VOLTAGE:
AD7291_VOLTAGE_LIMIT_COUNT) reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)]
return -EINVAL; [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
IIO_EV_DIR_RISING)];
ret = ad7291_i2c_read(chip, bound_reg, &data); ret = ad7291_i2c_read(chip, reg, &uval);
if (ret) if (ret < 0)
return -EIO; return ret;
*val = swab16(uval) & 0x0FFF;
return 0;
data &= AD7291_VALUE_MASK; case IIO_TEMP:
reg = ad7291_limit_regs[8]
[!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
IIO_EV_DIR_RISING)];
return sprintf(buf, "%d\n", data); ret = ad7291_i2c_read(chip, reg, &signval);
if (ret < 0)
return ret;
signval = (s16)((swab16(signval) & 0x0FFF) << 4) >> 4;
*val = signval;
return 0;
default:
return -EINVAL;
};
} }
static inline ssize_t ad7291_set_v_bound(struct device *dev, static int ad7291_write_event_value(struct iio_dev *indio_dev,
struct device_attribute *attr, u64 event_code,
u8 bound_reg, int val)
const char *buf,
size_t len)
{ {
struct iio_dev *dev_info = dev_get_drvdata(dev); struct ad7291_chip_info *chip = iio_priv(indio_dev);
struct ad7291_chip_info *chip = iio_priv(dev_info); u8 reg;
unsigned long value; s16 signval;
u16 data;
int ret;
if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE || switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
bound_reg >= AD7291_VOLTAGE_LIMIT_BASE + case IIO_VOLTAGE:
AD7291_VOLTAGE_LIMIT_COUNT) if (val > 0xFFF || val < 0)
return -EINVAL; return -EINVAL;
reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)]
[!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
IIO_EV_DIR_RISING)];
ret = strict_strtoul(buf, 10, &value); return ad7291_i2c_write(chip, reg, val);
if (ret || value >= 4096) case IIO_TEMP:
if (val > 2047 || val < -2048)
return -EINVAL;
reg = ad7291_limit_regs[8]
[!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
IIO_EV_DIR_RISING)];
signval = val;
return ad7291_i2c_write(chip, reg, *(u16 *)&signval);
default:
return -EINVAL; return -EINVAL;
};
}
data = (u16)value; static int ad7291_read_event_config(struct iio_dev *indio_dev,
ret = ad7291_i2c_write(chip, bound_reg, data); u64 event_code)
if (ret) {
return -EIO; struct ad7291_chip_info *chip = iio_priv(indio_dev);
/* To be enabled the channel must simply be on. If any are enabled
we are in continuous sampling mode */
switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
case IIO_VOLTAGE:
if (chip->c_mask &
(1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)))
return 1;
else
return 0;
case IIO_TEMP:
/* always on */
return 1;
default:
return -EINVAL;
}
}
static int ad7291_write_event_config(struct iio_dev *indio_dev,
u64 event_code,
int state)
{
int ret = 0;
struct ad7291_chip_info *chip = iio_priv(indio_dev);
u16 regval;
mutex_lock(&chip->state_lock);
regval = chip->command;
/*
* To be enabled the channel must simply be on. If any are enabled
* use continuous sampling mode.
* Possible to disable temp as well but that makes single read tricky.
*/
switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
case IIO_VOLTAGE:
if ((!state) && (chip->c_mask &
(1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code))))
chip->c_mask &=
~(1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code));
else if (state && (!(chip->c_mask &
(1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)))))
chip->c_mask &=
(1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code));
else
break;
regval &= 0xFFFE;
regval |= ((u16)chip->c_mask << 8);
if (chip->c_mask) /* Enable autocycle? */
regval |= AD7291_AUTOCYCLE;
ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
if (ret < 0)
goto error_ret;
chip->command = regval;
break;
default:
ret = -EINVAL;
}
error_ret:
mutex_unlock(&chip->state_lock);
return ret; return ret;
} }
static IIO_DEVICE_ATTR(t_sense_high_value, static int ad7291_read_raw(struct iio_dev *indio_dev,
S_IRUGO | S_IWUSR, struct iio_chan_spec const *chan,
ad7291_show_t_bound, ad7291_set_t_bound, int *val,
AD7291_T_SENSE_HIGH); int *val2,
static IIO_DEVICE_ATTR(t_sense_low_value, long mask)
S_IRUGO | S_IWUSR, {
ad7291_show_t_bound, ad7291_set_t_bound, int ret;
AD7291_T_SENSE_LOW); struct ad7291_chip_info *chip = iio_priv(indio_dev);
static IIO_DEVICE_ATTR(t_sense_hyst_value, u16 regval;
S_IRUGO | S_IWUSR, s16 signval;
ad7291_show_t_bound, ad7291_set_t_bound,
AD7291_T_SENSE_HYST); switch (mask) {
static IIO_DEVICE_ATTR(v0_high, case 0:
S_IRUGO | S_IWUSR, switch (chan->type) {
ad7291_show_t_bound, ad7291_set_t_bound, 0x04); case IIO_VOLTAGE:
static IIO_DEVICE_ATTR(v0_low, mutex_lock(&chip->state_lock);
S_IRUGO | S_IWUSR, /* If in autocycle mode drop through */
ad7291_show_t_bound, ad7291_set_t_bound, 0x05); if (chip->command & 0x1) {
static IIO_DEVICE_ATTR(v0_hyst, mutex_unlock(&chip->state_lock);
S_IRUGO | S_IWUSR, return -EBUSY;
ad7291_show_t_bound, ad7291_set_t_bound, 0x06); }
static IIO_DEVICE_ATTR(v1_high, /* Enable this channel alone */
S_IRUGO | S_IWUSR, regval = chip->command & (~AD7291_VOLTAGE_MASK);
ad7291_show_t_bound, ad7291_set_t_bound, 0x07); regval |= 1 << (chan->channel + 8);
static IIO_DEVICE_ATTR(v1_low, ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
S_IRUGO | S_IWUSR, if (ret < 0) {
ad7291_show_t_bound, ad7291_set_t_bound, 0x08); mutex_unlock(&chip->state_lock);
static IIO_DEVICE_ATTR(v1_hyst, return ret;
S_IRUGO | S_IWUSR, }
ad7291_show_t_bound, ad7291_set_t_bound, 0x09); /* Read voltage */
static IIO_DEVICE_ATTR(v2_high, ret = i2c_smbus_read_word_data(chip->client,
S_IRUGO | S_IWUSR, AD7291_VOLTAGE);
ad7291_show_t_bound, ad7291_set_t_bound, 0x0A); if (ret < 0) {
static IIO_DEVICE_ATTR(v2_low, mutex_unlock(&chip->state_lock);
S_IRUGO | S_IWUSR, return ret;
ad7291_show_t_bound, ad7291_set_t_bound, 0x0B); }
static IIO_DEVICE_ATTR(v2_hyst, *val = swab16((u16)ret) & 0x0FFF;
S_IRUGO | S_IWUSR, mutex_unlock(&chip->state_lock);
ad7291_show_t_bound, ad7291_set_t_bound, 0x0C); return IIO_VAL_INT;
static IIO_DEVICE_ATTR(v3_high, case IIO_TEMP:
S_IRUGO | S_IWUSR, /* Assumes tsense bit of command register always set */
/* Datasheet suggests this one and this one only ret = i2c_smbus_read_word_data(chip->client,
has the registers in different order */ AD7291_T_SENSE);
ad7291_show_t_bound, ad7291_set_t_bound, 0x0E); if (ret < 0)
static IIO_DEVICE_ATTR(v3_low, return ret;
S_IRUGO | S_IWUSR, signval = (s16)((swab16((u16)ret) & 0x0FFF) << 4) >> 4;
ad7291_show_t_bound, ad7291_set_t_bound, 0x0D); *val = signval;
static IIO_DEVICE_ATTR(v3_hyst, return IIO_VAL_INT;
S_IRUGO | S_IWUSR, default:
ad7291_show_t_bound, ad7291_set_t_bound, 0x0F); return -EINVAL;
static IIO_DEVICE_ATTR(v4_high, }
S_IRUGO | S_IWUSR, case (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE):
ad7291_show_t_bound, ad7291_set_t_bound, 0x10); ret = i2c_smbus_read_word_data(chip->client,
static IIO_DEVICE_ATTR(v4_low, AD7291_T_AVERAGE);
S_IRUGO | S_IWUSR, if (ret < 0)
ad7291_show_t_bound, ad7291_set_t_bound, 0x11); return ret;
static IIO_DEVICE_ATTR(v4_hyst, signval = (s16)((swab16((u16)ret) & 0x0FFF) << 4) >> 4;
S_IRUGO | S_IWUSR, *val = signval;
ad7291_show_t_bound, ad7291_set_t_bound, 0x12); return IIO_VAL_INT;
static IIO_DEVICE_ATTR(v5_high, default:
S_IRUGO | S_IWUSR, return -EINVAL;
ad7291_show_t_bound, ad7291_set_t_bound, 0x13); }
static IIO_DEVICE_ATTR(v5_low, }
S_IRUGO | S_IWUSR,
ad7291_show_t_bound, ad7291_set_t_bound, 0x14);
static IIO_DEVICE_ATTR(v5_hyst,
S_IRUGO | S_IWUSR,
ad7291_show_t_bound, ad7291_set_t_bound, 0x15);
static IIO_DEVICE_ATTR(v6_high,
S_IRUGO | S_IWUSR,
ad7291_show_t_bound, ad7291_set_t_bound, 0x16);
static IIO_DEVICE_ATTR(v6_low,
S_IRUGO | S_IWUSR,
ad7291_show_t_bound, ad7291_set_t_bound, 0x17);
static IIO_DEVICE_ATTR(v6_hyst,
S_IRUGO | S_IWUSR,
ad7291_show_t_bound, ad7291_set_t_bound, 0x18);
static IIO_DEVICE_ATTR(v7_high,
S_IRUGO | S_IWUSR,
ad7291_show_t_bound, ad7291_set_t_bound, 0x19);
static IIO_DEVICE_ATTR(v7_low,
S_IRUGO | S_IWUSR,
ad7291_show_t_bound, ad7291_set_t_bound, 0x1A);
static IIO_DEVICE_ATTR(v7_hyst,
S_IRUGO | S_IWUSR,
ad7291_show_t_bound, ad7291_set_t_bound, 0x1B);
static struct attribute *ad7291_event_attributes[] = { #define AD7291_VOLTAGE_CHAN(_chan) \
&iio_dev_attr_t_sense_high_value.dev_attr.attr, { \
&iio_dev_attr_t_sense_low_value.dev_attr.attr, .type = IIO_VOLTAGE, \
&iio_dev_attr_t_sense_hyst_value.dev_attr.attr, .indexed = 1, \
&iio_dev_attr_v0_high.dev_attr.attr, .channel = _chan, \
&iio_dev_attr_v0_low.dev_attr.attr, .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
&iio_dev_attr_v0_hyst.dev_attr.attr, IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
&iio_dev_attr_v1_high.dev_attr.attr, }
&iio_dev_attr_v1_low.dev_attr.attr,
&iio_dev_attr_v1_hyst.dev_attr.attr, static const struct iio_chan_spec ad7291_channels[] = {
&iio_dev_attr_v2_high.dev_attr.attr, AD7291_VOLTAGE_CHAN(0),
&iio_dev_attr_v2_low.dev_attr.attr, AD7291_VOLTAGE_CHAN(1),
&iio_dev_attr_v2_hyst.dev_attr.attr, AD7291_VOLTAGE_CHAN(2),
&iio_dev_attr_v3_high.dev_attr.attr, AD7291_VOLTAGE_CHAN(3),
&iio_dev_attr_v3_low.dev_attr.attr, AD7291_VOLTAGE_CHAN(4),
&iio_dev_attr_v3_hyst.dev_attr.attr, AD7291_VOLTAGE_CHAN(5),
&iio_dev_attr_v4_high.dev_attr.attr, AD7291_VOLTAGE_CHAN(6),
&iio_dev_attr_v4_low.dev_attr.attr, AD7291_VOLTAGE_CHAN(7),
&iio_dev_attr_v4_hyst.dev_attr.attr, {
&iio_dev_attr_v5_high.dev_attr.attr, .type = IIO_TEMP,
&iio_dev_attr_v5_low.dev_attr.attr, .info_mask = (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE),
&iio_dev_attr_v5_hyst.dev_attr.attr, .indexed = 1,
&iio_dev_attr_v6_high.dev_attr.attr, .channel = 0,
&iio_dev_attr_v6_low.dev_attr.attr, .event_mask =
&iio_dev_attr_v6_hyst.dev_attr.attr, IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
&iio_dev_attr_v7_high.dev_attr.attr, IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
&iio_dev_attr_v7_low.dev_attr.attr, }
&iio_dev_attr_v7_hyst.dev_attr.attr,
NULL,
}; };
static struct attribute_group ad7291_event_attribute_group = { static struct attribute_group ad7291_event_attribute_group = {
...@@ -780,13 +511,14 @@ static struct attribute_group ad7291_event_attribute_group = { ...@@ -780,13 +511,14 @@ static struct attribute_group ad7291_event_attribute_group = {
static const struct iio_info ad7291_info = { static const struct iio_info ad7291_info = {
.attrs = &ad7291_attribute_group, .attrs = &ad7291_attribute_group,
.read_raw = &ad7291_read_raw,
.read_event_config = &ad7291_read_event_config,
.write_event_config = &ad7291_write_event_config,
.read_event_value = &ad7291_read_event_value,
.write_event_value = &ad7291_write_event_value,
.event_attrs = &ad7291_event_attribute_group, .event_attrs = &ad7291_event_attribute_group,
}; };
/*
* device probe and remove
*/
static int __devinit ad7291_probe(struct i2c_client *client, static int __devinit ad7291_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
...@@ -800,13 +532,18 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -800,13 +532,18 @@ static int __devinit ad7291_probe(struct i2c_client *client,
goto error_ret; goto error_ret;
} }
chip = iio_priv(indio_dev); chip = iio_priv(indio_dev);
mutex_init(&chip->state_lock);
/* this is only used for device removal purposes */ /* this is only used for device removal purposes */
i2c_set_clientdata(client, indio_dev); i2c_set_clientdata(client, indio_dev);
chip->client = client; chip->client = client;
/* Tsense always enabled */
chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK; chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
indio_dev->name = id->name; indio_dev->name = id->name;
indio_dev->channels = ad7291_channels;
indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
indio_dev->dev.parent = &client->dev; indio_dev->dev.parent = &client->dev;
indio_dev->info = &ad7291_info; indio_dev->info = &ad7291_info;
indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->modes = INDIO_DIRECT_MODE;
...@@ -841,6 +578,7 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -841,6 +578,7 @@ static int __devinit ad7291_probe(struct i2c_client *client,
return 0; return 0;
error_unreg_irq: error_unreg_irq:
if (client->irq)
free_irq(client->irq, indio_dev); free_irq(client->irq, indio_dev);
error_free_dev: error_free_dev:
iio_free_device(indio_dev); iio_free_device(indio_dev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment