2010-10-28 01:43:55 +00:00
|
|
|
/*
|
2011-09-02 16:25:37 +00:00
|
|
|
* AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
|
2010-10-28 01:43:55 +00:00
|
|
|
*
|
2011-09-02 16:25:37 +00:00
|
|
|
* Copyright 2010-2011 Analog Devices Inc.
|
2010-10-28 01:43:55 +00:00
|
|
|
*
|
|
|
|
* Licensed under the GPL-2 or later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/device.h>
|
2014-06-30 08:50:00 +00:00
|
|
|
#include <linux/err.h>
|
2010-10-28 01:43:55 +00:00
|
|
|
#include <linux/i2c.h>
|
2014-06-30 08:50:00 +00:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/kernel.h>
|
2011-07-03 19:49:50 +00:00
|
|
|
#include <linux/module.h>
|
2011-09-02 16:25:32 +00:00
|
|
|
#include <linux/mutex.h>
|
2011-09-02 16:25:39 +00:00
|
|
|
#include <linux/regulator/consumer.h>
|
2014-06-30 08:50:00 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/sysfs.h>
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2012-04-25 14:54:58 +00:00
|
|
|
#include <linux/iio/iio.h>
|
|
|
|
#include <linux/iio/sysfs.h>
|
|
|
|
#include <linux/iio/events.h>
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2014-06-30 08:50:00 +00:00
|
|
|
#include <linux/platform_data/ad7291.h>
|
2013-06-14 15:58:00 +00:00
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-09-02 16:25:39 +00:00
|
|
|
|
2010-10-28 01:43:55 +00:00
|
|
|
/*
|
|
|
|
* AD7291 registers definition
|
|
|
|
*/
|
2011-09-02 16:25:40 +00:00
|
|
|
#define AD7291_COMMAND 0x00
|
|
|
|
#define AD7291_VOLTAGE 0x01
|
|
|
|
#define AD7291_T_SENSE 0x02
|
|
|
|
#define AD7291_T_AVERAGE 0x03
|
2013-06-14 15:58:00 +00:00
|
|
|
#define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
|
|
|
|
#define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
|
|
|
|
#define AD7291_HYST(x) ((x) * 3 + 0x6)
|
2011-09-02 16:25:38 +00:00
|
|
|
#define AD7291_VOLTAGE_ALERT_STATUS 0x1F
|
2010-10-28 01:43:55 +00:00
|
|
|
#define AD7291_T_ALERT_STATUS 0x20
|
|
|
|
|
2014-08-07 11:06:00 +00:00
|
|
|
#define AD7291_BITS 12
|
2011-09-02 16:25:40 +00:00
|
|
|
#define AD7291_VOLTAGE_LIMIT_COUNT 8
|
|
|
|
|
|
|
|
|
2010-10-28 01:43:55 +00:00
|
|
|
/*
|
|
|
|
* AD7291 command
|
|
|
|
*/
|
2014-08-07 11:06:00 +00:00
|
|
|
#define AD7291_AUTOCYCLE BIT(0)
|
|
|
|
#define AD7291_RESET BIT(1)
|
|
|
|
#define AD7291_ALERT_CLEAR BIT(2)
|
|
|
|
#define AD7291_ALERT_POLARITY BIT(3)
|
|
|
|
#define AD7291_EXT_REF BIT(4)
|
|
|
|
#define AD7291_NOISE_DELAY BIT(5)
|
|
|
|
#define AD7291_T_SENSE_MASK BIT(7)
|
|
|
|
#define AD7291_VOLTAGE_MASK GENMASK(15, 8)
|
|
|
|
#define AD7291_VOLTAGE_OFFSET 8
|
2010-10-28 01:43:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* AD7291 value masks
|
|
|
|
*/
|
2014-08-07 11:06:00 +00:00
|
|
|
#define AD7291_VALUE_MASK GENMASK(11, 0)
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2014-08-07 11:06:00 +00:00
|
|
|
/*
|
|
|
|
* AD7291 alert register bits
|
|
|
|
*/
|
|
|
|
#define AD7291_T_LOW BIT(0)
|
|
|
|
#define AD7291_T_HIGH BIT(1)
|
|
|
|
#define AD7291_T_AVG_LOW BIT(2)
|
|
|
|
#define AD7291_T_AVG_HIGH BIT(3)
|
|
|
|
#define AD7291_V_LOW(x) BIT((x) * 2)
|
|
|
|
#define AD7291_V_HIGH(x) BIT((x) * 2 + 1)
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-09-02 16:25:39 +00:00
|
|
|
|
2010-10-28 01:43:55 +00:00
|
|
|
struct ad7291_chip_info {
|
2011-09-02 16:25:39 +00:00
|
|
|
struct i2c_client *client;
|
|
|
|
struct regulator *reg;
|
|
|
|
u16 command;
|
2011-09-02 16:25:43 +00:00
|
|
|
u16 c_mask; /* Active voltage channels for events */
|
2011-09-02 16:25:45 +00:00
|
|
|
struct mutex state_lock;
|
2010-10-28 01:43:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = chip->client;
|
|
|
|
int ret = 0;
|
|
|
|
|
2013-06-14 15:58:00 +00:00
|
|
|
ret = i2c_smbus_read_word_swapped(client, reg);
|
2010-10-28 01:43:55 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "I2C read error\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:58:00 +00:00
|
|
|
*data = ret;
|
2010-10-28 01:43:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
|
|
|
|
{
|
2013-06-14 15:58:00 +00:00
|
|
|
return i2c_smbus_write_word_swapped(chip->client, reg, data);
|
2010-10-28 01:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-05-18 13:41:11 +00:00
|
|
|
static irqreturn_t ad7291_event_handler(int irq, void *private)
|
2010-10-28 01:43:55 +00:00
|
|
|
{
|
2011-05-18 13:41:11 +00:00
|
|
|
struct iio_dev *indio_dev = private;
|
2011-06-27 12:07:23 +00:00
|
|
|
struct ad7291_chip_info *chip = iio_priv(private);
|
2010-10-28 01:43:55 +00:00
|
|
|
u16 t_status, v_status;
|
|
|
|
u16 command;
|
|
|
|
int i;
|
2016-03-09 18:05:49 +00:00
|
|
|
s64 timestamp = iio_get_time_ns(indio_dev);
|
2010-10-28 01:43:55 +00:00
|
|
|
|
|
|
|
if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
|
2011-05-18 13:41:11 +00:00
|
|
|
return IRQ_HANDLED;
|
2010-10-28 01:43:55 +00:00
|
|
|
|
|
|
|
if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
|
2011-05-18 13:41:11 +00:00
|
|
|
return IRQ_HANDLED;
|
2010-10-28 01:43:55 +00:00
|
|
|
|
|
|
|
if (!(t_status || v_status))
|
2011-05-18 13:41:11 +00:00
|
|
|
return IRQ_HANDLED;
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-09-02 16:25:37 +00:00
|
|
|
command = chip->command | AD7291_ALERT_CLEAR;
|
2010-10-28 01:43:55 +00:00
|
|
|
ad7291_i2c_write(chip, AD7291_COMMAND, command);
|
|
|
|
|
2011-09-02 16:25:37 +00:00
|
|
|
command = chip->command & ~AD7291_ALERT_CLEAR;
|
2010-10-28 01:43:55 +00:00
|
|
|
ad7291_i2c_write(chip, AD7291_COMMAND, command);
|
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
/* For now treat t_sense and t_sense_average the same */
|
2014-08-07 11:06:00 +00:00
|
|
|
if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW))
|
2011-08-30 11:41:06 +00:00
|
|
|
iio_push_event(indio_dev,
|
2011-05-18 13:42:15 +00:00
|
|
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP,
|
|
|
|
0,
|
|
|
|
IIO_EV_TYPE_THRESH,
|
|
|
|
IIO_EV_DIR_FALLING),
|
|
|
|
timestamp);
|
2014-08-07 11:06:00 +00:00
|
|
|
if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH))
|
2011-08-30 11:41:06 +00:00
|
|
|
iio_push_event(indio_dev,
|
2011-05-18 13:42:15 +00:00
|
|
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP,
|
|
|
|
0,
|
|
|
|
IIO_EV_TYPE_THRESH,
|
|
|
|
IIO_EV_DIR_RISING),
|
|
|
|
timestamp);
|
|
|
|
|
2014-08-07 11:06:00 +00:00
|
|
|
for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
|
|
|
|
if (v_status & AD7291_V_LOW(i))
|
2011-08-30 11:41:06 +00:00
|
|
|
iio_push_event(indio_dev,
|
2011-09-27 08:56:41 +00:00
|
|
|
IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
|
2014-08-07 11:06:00 +00:00
|
|
|
i,
|
2011-05-18 13:42:15 +00:00
|
|
|
IIO_EV_TYPE_THRESH,
|
|
|
|
IIO_EV_DIR_FALLING),
|
|
|
|
timestamp);
|
2014-08-07 11:06:00 +00:00
|
|
|
if (v_status & AD7291_V_HIGH(i))
|
2011-08-30 11:41:06 +00:00
|
|
|
iio_push_event(indio_dev,
|
2011-09-27 08:56:41 +00:00
|
|
|
IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
|
2014-08-07 11:06:00 +00:00
|
|
|
i,
|
2011-05-18 13:42:15 +00:00
|
|
|
IIO_EV_TYPE_THRESH,
|
|
|
|
IIO_EV_DIR_RISING),
|
|
|
|
timestamp);
|
2010-10-28 01:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-05-18 13:41:11 +00:00
|
|
|
return IRQ_HANDLED;
|
2010-10-28 01:43:55 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
|
2014-08-07 11:06:00 +00:00
|
|
|
enum iio_event_direction dir,
|
|
|
|
enum iio_event_info info)
|
2013-06-14 15:58:00 +00:00
|
|
|
{
|
|
|
|
unsigned int offset;
|
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
switch (chan->type) {
|
2013-06-14 15:58:00 +00:00
|
|
|
case IIO_VOLTAGE:
|
2013-10-07 14:11:00 +00:00
|
|
|
offset = chan->channel;
|
2013-06-14 15:58:00 +00:00
|
|
|
break;
|
|
|
|
case IIO_TEMP:
|
2014-08-07 11:06:00 +00:00
|
|
|
offset = AD7291_VOLTAGE_OFFSET;
|
2013-06-14 15:58:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
switch (info) {
|
|
|
|
case IIO_EV_INFO_VALUE:
|
2014-08-07 11:06:00 +00:00
|
|
|
if (dir == IIO_EV_DIR_FALLING)
|
|
|
|
return AD7291_DATA_HIGH(offset);
|
|
|
|
else
|
|
|
|
return AD7291_DATA_LOW(offset);
|
2013-10-07 14:11:00 +00:00
|
|
|
case IIO_EV_INFO_HYSTERESIS:
|
2014-08-07 11:06:00 +00:00
|
|
|
return AD7291_HYST(offset);
|
2013-10-07 14:11:00 +00:00
|
|
|
default:
|
2014-08-07 11:06:00 +00:00
|
|
|
break;
|
2013-10-07 14:11:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2013-06-14 15:58:00 +00:00
|
|
|
}
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
static int ad7291_read_event_value(struct iio_dev *indio_dev,
|
2013-10-07 14:11:00 +00:00
|
|
|
const struct iio_chan_spec *chan,
|
|
|
|
enum iio_event_type type,
|
|
|
|
enum iio_event_direction dir,
|
|
|
|
enum iio_event_info info,
|
|
|
|
int *val, int *val2)
|
2010-10-28 01:43:55 +00:00
|
|
|
{
|
2011-09-02 16:25:32 +00:00
|
|
|
struct ad7291_chip_info *chip = iio_priv(indio_dev);
|
|
|
|
int ret;
|
|
|
|
u16 uval;
|
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
|
2014-08-07 11:06:00 +00:00
|
|
|
&uval);
|
2013-06-14 15:58:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
|
2011-09-02 16:25:40 +00:00
|
|
|
*val = uval & AD7291_VALUE_MASK;
|
2013-10-07 14:11:00 +00:00
|
|
|
|
|
|
|
else
|
2013-06-14 15:58:00 +00:00
|
|
|
*val = sign_extend32(uval, 11);
|
2013-10-07 14:11:00 +00:00
|
|
|
|
|
|
|
return IIO_VAL_INT;
|
2011-09-02 16:25:32 +00:00
|
|
|
}
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
static int ad7291_write_event_value(struct iio_dev *indio_dev,
|
2013-10-07 14:11:00 +00:00
|
|
|
const struct iio_chan_spec *chan,
|
|
|
|
enum iio_event_type type,
|
|
|
|
enum iio_event_direction dir,
|
|
|
|
enum iio_event_info info,
|
|
|
|
int val, int val2)
|
2011-09-02 16:25:32 +00:00
|
|
|
{
|
|
|
|
struct ad7291_chip_info *chip = iio_priv(indio_dev);
|
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
|
2011-09-02 16:25:46 +00:00
|
|
|
if (val > AD7291_VALUE_MASK || val < 0)
|
2011-09-02 16:25:32 +00:00
|
|
|
return -EINVAL;
|
2013-10-07 14:11:00 +00:00
|
|
|
} else {
|
2011-09-02 16:25:32 +00:00
|
|
|
if (val > 2047 || val < -2048)
|
|
|
|
return -EINVAL;
|
2013-06-14 15:58:00 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
|
2014-08-07 11:06:00 +00:00
|
|
|
val);
|
2010-10-28 01:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
static int ad7291_read_event_config(struct iio_dev *indio_dev,
|
2013-10-07 14:11:00 +00:00
|
|
|
const struct iio_chan_spec *chan,
|
|
|
|
enum iio_event_type type,
|
|
|
|
enum iio_event_direction dir)
|
2010-10-28 01:43:55 +00:00
|
|
|
{
|
2011-09-02 16:25:32 +00:00
|
|
|
struct ad7291_chip_info *chip = iio_priv(indio_dev);
|
2014-08-07 11:06:00 +00:00
|
|
|
/*
|
|
|
|
* To be enabled the channel must simply be on. If any are enabled
|
|
|
|
* we are in continuous sampling mode
|
|
|
|
*/
|
2011-09-02 16:25:32 +00:00
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
switch (chan->type) {
|
2011-09-02 16:25:32 +00:00
|
|
|
case IIO_VOLTAGE:
|
2014-08-07 11:06:00 +00:00
|
|
|
return !!(chip->c_mask & BIT(15 - chan->channel));
|
2011-09-02 16:25:32 +00:00
|
|
|
case IIO_TEMP:
|
|
|
|
/* always on */
|
|
|
|
return 1;
|
|
|
|
default:
|
2010-10-28 01:43:55 +00:00
|
|
|
return -EINVAL;
|
2011-09-02 16:25:32 +00:00
|
|
|
}
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
}
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
static int ad7291_write_event_config(struct iio_dev *indio_dev,
|
2013-10-07 14:11:00 +00:00
|
|
|
const struct iio_chan_spec *chan,
|
|
|
|
enum iio_event_type type,
|
|
|
|
enum iio_event_direction dir,
|
2011-09-02 16:25:32 +00:00
|
|
|
int state)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct ad7291_chip_info *chip = iio_priv(indio_dev);
|
2013-10-07 14:11:00 +00:00
|
|
|
unsigned int mask;
|
2011-09-02 16:25:32 +00:00
|
|
|
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.
|
|
|
|
*/
|
2011-09-02 16:25:40 +00:00
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
mask = BIT(15 - chan->channel);
|
|
|
|
|
|
|
|
switch (chan->type) {
|
2011-09-02 16:25:32 +00:00
|
|
|
case IIO_VOLTAGE:
|
2013-10-07 14:11:00 +00:00
|
|
|
if ((!state) && (chip->c_mask & mask))
|
|
|
|
chip->c_mask &= ~mask;
|
|
|
|
else if (state && (!(chip->c_mask & mask)))
|
|
|
|
chip->c_mask |= mask;
|
2011-09-02 16:25:32 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2011-09-02 16:25:40 +00:00
|
|
|
regval &= ~AD7291_AUTOCYCLE;
|
2011-09-02 16:25:43 +00:00
|
|
|
regval |= chip->c_mask;
|
2011-09-02 16:25:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
error_ret:
|
|
|
|
mutex_unlock(&chip->state_lock);
|
2010-10-28 01:43:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
static int ad7291_read_raw(struct iio_dev *indio_dev,
|
|
|
|
struct iio_chan_spec const *chan,
|
|
|
|
int *val,
|
|
|
|
int *val2,
|
|
|
|
long mask)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct ad7291_chip_info *chip = iio_priv(indio_dev);
|
|
|
|
u16 regval;
|
|
|
|
|
|
|
|
switch (mask) {
|
2012-04-15 16:41:18 +00:00
|
|
|
case IIO_CHAN_INFO_RAW:
|
2011-09-02 16:25:32 +00:00
|
|
|
switch (chan->type) {
|
|
|
|
case IIO_VOLTAGE:
|
|
|
|
mutex_lock(&chip->state_lock);
|
|
|
|
/* If in autocycle mode drop through */
|
2011-09-02 16:25:43 +00:00
|
|
|
if (chip->command & AD7291_AUTOCYCLE) {
|
2011-09-02 16:25:32 +00:00
|
|
|
mutex_unlock(&chip->state_lock);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
/* Enable this channel alone */
|
|
|
|
regval = chip->command & (~AD7291_VOLTAGE_MASK);
|
2014-08-07 11:06:00 +00:00
|
|
|
regval |= BIT(15 - chan->channel);
|
2011-09-02 16:25:32 +00:00
|
|
|
ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
|
|
|
|
if (ret < 0) {
|
|
|
|
mutex_unlock(&chip->state_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* Read voltage */
|
2013-06-14 15:58:00 +00:00
|
|
|
ret = i2c_smbus_read_word_swapped(chip->client,
|
2014-08-07 11:06:00 +00:00
|
|
|
AD7291_VOLTAGE);
|
2011-09-02 16:25:32 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
mutex_unlock(&chip->state_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-06-14 15:58:00 +00:00
|
|
|
*val = ret & AD7291_VALUE_MASK;
|
2011-09-02 16:25:32 +00:00
|
|
|
mutex_unlock(&chip->state_lock);
|
|
|
|
return IIO_VAL_INT;
|
|
|
|
case IIO_TEMP:
|
|
|
|
/* Assumes tsense bit of command register always set */
|
2013-06-14 15:58:00 +00:00
|
|
|
ret = i2c_smbus_read_word_swapped(chip->client,
|
2014-08-07 11:06:00 +00:00
|
|
|
AD7291_T_SENSE);
|
2011-09-02 16:25:32 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-06-14 15:58:00 +00:00
|
|
|
*val = sign_extend32(ret, 11);
|
2011-09-02 16:25:32 +00:00
|
|
|
return IIO_VAL_INT;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-10-26 16:41:36 +00:00
|
|
|
case IIO_CHAN_INFO_AVERAGE_RAW:
|
2013-06-14 15:58:00 +00:00
|
|
|
ret = i2c_smbus_read_word_swapped(chip->client,
|
2014-08-07 11:06:00 +00:00
|
|
|
AD7291_T_AVERAGE);
|
2011-09-02 16:25:32 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-06-14 15:58:00 +00:00
|
|
|
*val = sign_extend32(ret, 11);
|
2011-09-02 16:25:32 +00:00
|
|
|
return IIO_VAL_INT;
|
2011-10-26 16:41:36 +00:00
|
|
|
case IIO_CHAN_INFO_SCALE:
|
|
|
|
switch (chan->type) {
|
|
|
|
case IIO_VOLTAGE:
|
2013-06-14 15:58:00 +00:00
|
|
|
if (chip->reg) {
|
|
|
|
int vref;
|
2014-06-30 08:50:00 +00:00
|
|
|
|
2013-06-14 15:58:00 +00:00
|
|
|
vref = regulator_get_voltage(chip->reg);
|
|
|
|
if (vref < 0)
|
|
|
|
return vref;
|
|
|
|
*val = vref / 1000;
|
|
|
|
} else {
|
|
|
|
*val = 2500;
|
|
|
|
}
|
2013-06-14 15:58:00 +00:00
|
|
|
*val2 = AD7291_BITS;
|
|
|
|
return IIO_VAL_FRACTIONAL_LOG2;
|
2011-10-26 16:41:36 +00:00
|
|
|
case IIO_TEMP:
|
|
|
|
/*
|
|
|
|
* One LSB of the ADC corresponds to 0.25 deg C.
|
|
|
|
* The temperature reading is in 12-bit twos
|
|
|
|
* complement format
|
|
|
|
*/
|
|
|
|
*val = 250;
|
|
|
|
return IIO_VAL_INT;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-09-02 16:25:32 +00:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2013-10-07 14:11:00 +00:00
|
|
|
static const struct iio_event_spec ad7291_events[] = {
|
|
|
|
{
|
|
|
|
.type = IIO_EV_TYPE_THRESH,
|
|
|
|
.dir = IIO_EV_DIR_RISING,
|
|
|
|
.mask_separate = BIT(IIO_EV_INFO_VALUE) |
|
|
|
|
BIT(IIO_EV_INFO_ENABLE),
|
|
|
|
}, {
|
|
|
|
.type = IIO_EV_TYPE_THRESH,
|
|
|
|
.dir = IIO_EV_DIR_FALLING,
|
|
|
|
.mask_separate = BIT(IIO_EV_INFO_VALUE) |
|
|
|
|
BIT(IIO_EV_INFO_ENABLE),
|
2013-10-07 14:11:00 +00:00
|
|
|
}, {
|
|
|
|
.type = IIO_EV_TYPE_THRESH,
|
|
|
|
.dir = IIO_EV_DIR_EITHER,
|
|
|
|
.mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
|
2013-10-07 14:11:00 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
#define AD7291_VOLTAGE_CHAN(_chan) \
|
|
|
|
{ \
|
|
|
|
.type = IIO_VOLTAGE, \
|
2013-02-27 19:37:16 +00:00
|
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
|
|
|
|
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
|
2011-09-02 16:25:32 +00:00
|
|
|
.indexed = 1, \
|
|
|
|
.channel = _chan, \
|
2013-10-07 14:11:00 +00:00
|
|
|
.event_spec = ad7291_events, \
|
|
|
|
.num_event_specs = ARRAY_SIZE(ad7291_events), \
|
2011-09-02 16:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct iio_chan_spec ad7291_channels[] = {
|
|
|
|
AD7291_VOLTAGE_CHAN(0),
|
|
|
|
AD7291_VOLTAGE_CHAN(1),
|
|
|
|
AD7291_VOLTAGE_CHAN(2),
|
|
|
|
AD7291_VOLTAGE_CHAN(3),
|
|
|
|
AD7291_VOLTAGE_CHAN(4),
|
|
|
|
AD7291_VOLTAGE_CHAN(5),
|
|
|
|
AD7291_VOLTAGE_CHAN(6),
|
|
|
|
AD7291_VOLTAGE_CHAN(7),
|
|
|
|
{
|
|
|
|
.type = IIO_TEMP,
|
2013-02-27 19:37:16 +00:00
|
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
|
|
|
BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
|
|
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
2011-09-02 16:25:32 +00:00
|
|
|
.indexed = 1,
|
|
|
|
.channel = 0,
|
2013-10-07 14:11:00 +00:00
|
|
|
.event_spec = ad7291_events,
|
|
|
|
.num_event_specs = ARRAY_SIZE(ad7291_events),
|
2011-09-02 16:25:32 +00:00
|
|
|
}
|
2010-10-28 01:43:55 +00:00
|
|
|
};
|
|
|
|
|
2011-05-18 13:42:37 +00:00
|
|
|
static const struct iio_info ad7291_info = {
|
2011-09-02 16:25:32 +00:00
|
|
|
.read_raw = &ad7291_read_raw,
|
2013-12-07 10:45:00 +00:00
|
|
|
.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,
|
2011-05-18 13:42:37 +00:00
|
|
|
};
|
|
|
|
|
2012-11-19 18:21:57 +00:00
|
|
|
static int ad7291_probe(struct i2c_client *client,
|
2014-08-07 11:06:00 +00:00
|
|
|
const struct i2c_device_id *id)
|
2010-10-28 01:43:55 +00:00
|
|
|
{
|
2013-06-14 15:58:00 +00:00
|
|
|
struct ad7291_platform_data *pdata = client->dev.platform_data;
|
2010-10-28 01:43:55 +00:00
|
|
|
struct ad7291_chip_info *chip;
|
2011-06-27 12:07:23 +00:00
|
|
|
struct iio_dev *indio_dev;
|
2014-06-20 19:22:00 +00:00
|
|
|
int ret;
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2013-08-31 17:12:00 +00:00
|
|
|
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
|
|
|
|
if (!indio_dev)
|
|
|
|
return -ENOMEM;
|
2011-06-29 19:50:48 +00:00
|
|
|
chip = iio_priv(indio_dev);
|
2011-09-02 16:25:39 +00:00
|
|
|
|
2013-06-14 15:58:00 +00:00
|
|
|
if (pdata && pdata->use_external_ref) {
|
2013-08-31 17:12:00 +00:00
|
|
|
chip->reg = devm_regulator_get(&client->dev, "vref");
|
2013-06-14 15:58:00 +00:00
|
|
|
if (IS_ERR(chip->reg))
|
2014-06-20 19:22:00 +00:00
|
|
|
return PTR_ERR(chip->reg);
|
2013-06-14 15:58:00 +00:00
|
|
|
|
2011-09-02 16:25:39 +00:00
|
|
|
ret = regulator_enable(chip->reg);
|
|
|
|
if (ret)
|
2013-08-31 17:12:00 +00:00
|
|
|
return ret;
|
2011-09-02 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 16:25:32 +00:00
|
|
|
mutex_init(&chip->state_lock);
|
2010-10-28 01:43:55 +00:00
|
|
|
/* this is only used for device removal purposes */
|
2011-06-27 12:07:23 +00:00
|
|
|
i2c_set_clientdata(client, indio_dev);
|
2010-10-28 01:43:55 +00:00
|
|
|
|
|
|
|
chip->client = client;
|
2011-09-02 16:25:44 +00:00
|
|
|
|
|
|
|
chip->command = AD7291_NOISE_DELAY |
|
|
|
|
AD7291_T_SENSE_MASK | /* Tsense always enabled */
|
|
|
|
AD7291_ALERT_POLARITY; /* set irq polarity low level */
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2013-06-14 15:58:00 +00:00
|
|
|
if (pdata && pdata->use_external_ref)
|
2011-09-02 16:25:39 +00:00
|
|
|
chip->command |= AD7291_EXT_REF;
|
|
|
|
|
2011-06-27 12:07:23 +00:00
|
|
|
indio_dev->name = id->name;
|
2011-09-02 16:25:32 +00:00
|
|
|
indio_dev->channels = ad7291_channels;
|
|
|
|
indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
|
|
|
|
|
2011-06-27 12:07:23 +00:00
|
|
|
indio_dev->dev.parent = &client->dev;
|
2016-07-03 00:26:33 +00:00
|
|
|
indio_dev->dev.of_node = client->dev.of_node;
|
2011-06-27 12:07:23 +00:00
|
|
|
indio_dev->info = &ad7291_info;
|
|
|
|
indio_dev->modes = INDIO_DIRECT_MODE;
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-09-02 16:25:44 +00:00
|
|
|
ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
|
|
|
|
if (ret) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto error_disable_reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
|
|
|
|
if (ret) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto error_disable_reg;
|
|
|
|
}
|
|
|
|
|
2010-10-28 01:43:55 +00:00
|
|
|
if (client->irq > 0) {
|
2011-05-18 13:41:11 +00:00
|
|
|
ret = request_threaded_irq(client->irq,
|
|
|
|
NULL,
|
|
|
|
&ad7291_event_handler,
|
|
|
|
IRQF_TRIGGER_LOW | IRQF_ONESHOT,
|
2011-05-18 13:41:44 +00:00
|
|
|
id->name,
|
2011-06-27 12:07:23 +00:00
|
|
|
indio_dev);
|
2010-10-28 01:43:55 +00:00
|
|
|
if (ret)
|
2011-09-02 16:25:39 +00:00
|
|
|
goto error_disable_reg;
|
2010-10-28 01:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 16:14:40 +00:00
|
|
|
ret = iio_device_register(indio_dev);
|
|
|
|
if (ret)
|
|
|
|
goto error_unreg_irq;
|
|
|
|
|
2010-10-28 01:43:55 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_unreg_irq:
|
2011-09-02 16:25:32 +00:00
|
|
|
if (client->irq)
|
|
|
|
free_irq(client->irq, indio_dev);
|
2011-09-02 16:25:39 +00:00
|
|
|
error_disable_reg:
|
2013-06-14 15:58:00 +00:00
|
|
|
if (chip->reg)
|
2011-09-02 16:25:39 +00:00
|
|
|
regulator_disable(chip->reg);
|
2013-08-31 17:12:00 +00:00
|
|
|
|
2010-10-28 01:43:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-19 18:26:37 +00:00
|
|
|
static int ad7291_remove(struct i2c_client *client)
|
2010-10-28 01:43:55 +00:00
|
|
|
{
|
2011-06-27 12:07:23 +00:00
|
|
|
struct iio_dev *indio_dev = i2c_get_clientdata(client);
|
2011-09-02 16:25:39 +00:00
|
|
|
struct ad7291_chip_info *chip = iio_priv(indio_dev);
|
2010-10-28 01:43:55 +00:00
|
|
|
|
2011-10-14 13:46:58 +00:00
|
|
|
iio_device_unregister(indio_dev);
|
|
|
|
|
2010-10-28 01:43:55 +00:00
|
|
|
if (client->irq)
|
2011-06-27 12:07:23 +00:00
|
|
|
free_irq(client->irq, indio_dev);
|
2011-09-02 16:25:39 +00:00
|
|
|
|
2013-08-31 17:12:00 +00:00
|
|
|
if (chip->reg)
|
2011-09-02 16:25:39 +00:00
|
|
|
regulator_disable(chip->reg);
|
2010-10-28 01:43:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_device_id ad7291_id[] = {
|
|
|
|
{ "ad7291", 0 },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(i2c, ad7291_id);
|
|
|
|
|
|
|
|
static struct i2c_driver ad7291_driver = {
|
|
|
|
.driver = {
|
2011-09-02 16:25:45 +00:00
|
|
|
.name = KBUILD_MODNAME,
|
2010-10-28 01:43:55 +00:00
|
|
|
},
|
|
|
|
.probe = ad7291_probe,
|
2012-11-19 18:21:38 +00:00
|
|
|
.remove = ad7291_remove,
|
2010-10-28 01:43:55 +00:00
|
|
|
.id_table = ad7291_id,
|
|
|
|
};
|
2011-11-16 09:13:38 +00:00
|
|
|
module_i2c_driver(ad7291_driver);
|
2010-10-28 01:43:55 +00:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
|
2011-09-02 16:25:45 +00:00
|
|
|
MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
|
2010-10-28 01:43:55 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|