Commit 3b424f33 authored by Michael Hennerich's avatar Michael Hennerich Committed by Greg Kroah-Hartman

staging:iio:adc:ad7291: Add regulator, reference voltage and scale handling

Signed-off-by: default avatarMichael Hennerich <michael.hennerich@analog.com>
Signed-off-by: default avatarJonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 00feefbb
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include "../iio.h" #include "../iio.h"
#include "../sysfs.h" #include "../sysfs.h"
...@@ -27,8 +29,8 @@ ...@@ -27,8 +29,8 @@
* *
* The noise-delayed bit as per datasheet suggestion is always enabled. * 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
*/ */
...@@ -61,16 +63,21 @@ ...@@ -61,16 +63,21 @@
* AD7291 value masks * AD7291 value masks
*/ */
#define AD7291_CHANNEL_MASK 0xF000 #define AD7291_CHANNEL_MASK 0xF000
#define AD7291_BITS 12
#define AD7291_VALUE_MASK 0xFFF #define AD7291_VALUE_MASK 0xFFF
#define AD7291_T_VALUE_SIGN 0x400 #define AD7291_T_VALUE_SIGN 0x400
#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
#define AD7291_BITS 12
struct ad7291_chip_info { struct ad7291_chip_info {
struct i2c_client *client; struct i2c_client *client;
u16 command; struct regulator *reg;
u8 c_mask; /* Active voltage channels for events */ u16 int_vref_mv;
struct mutex state_lock; u16 command;
u8 c_mask; /* Active voltage channels for events */
struct mutex state_lock;
}; };
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)
...@@ -420,6 +427,7 @@ static int ad7291_read_raw(struct iio_dev *indio_dev, ...@@ -420,6 +427,7 @@ static int ad7291_read_raw(struct iio_dev *indio_dev,
{ {
int ret; int ret;
struct ad7291_chip_info *chip = iio_priv(indio_dev); struct ad7291_chip_info *chip = iio_priv(indio_dev);
unsigned int scale_uv;
u16 regval; u16 regval;
s16 signval; s16 signval;
...@@ -471,6 +479,11 @@ static int ad7291_read_raw(struct iio_dev *indio_dev, ...@@ -471,6 +479,11 @@ static int ad7291_read_raw(struct iio_dev *indio_dev,
signval = (s16)((swab16((u16)ret) & 0x0FFF) << 4) >> 4; signval = (s16)((swab16((u16)ret) & 0x0FFF) << 4) >> 4;
*val = signval; *val = signval;
return IIO_VAL_INT; return IIO_VAL_INT;
case (1 << IIO_CHAN_INFO_SCALE_SHARED):
scale_uv = (chip->int_vref_mv * 1000) >> AD7291_BITS;
*val = scale_uv / 1000;
*val2 = (scale_uv % 1000) * 1000;
return IIO_VAL_INT_PLUS_MICRO;
default: default:
return -EINVAL; return -EINVAL;
} }
...@@ -479,6 +492,7 @@ static int ad7291_read_raw(struct iio_dev *indio_dev, ...@@ -479,6 +492,7 @@ static int ad7291_read_raw(struct iio_dev *indio_dev,
#define AD7291_VOLTAGE_CHAN(_chan) \ #define AD7291_VOLTAGE_CHAN(_chan) \
{ \ { \
.type = IIO_VOLTAGE, \ .type = IIO_VOLTAGE, \
.info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED), \
.indexed = 1, \ .indexed = 1, \
.channel = _chan, \ .channel = _chan, \
.event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\ .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
...@@ -524,7 +538,7 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -524,7 +538,7 @@ static int __devinit ad7291_probe(struct i2c_client *client,
{ {
struct ad7291_chip_info *chip; struct ad7291_chip_info *chip;
struct iio_dev *indio_dev; struct iio_dev *indio_dev;
int ret = 0; int ret = 0, voltage_uv = 0;
indio_dev = iio_allocate_device(sizeof(*chip)); indio_dev = iio_allocate_device(sizeof(*chip));
if (indio_dev == NULL) { if (indio_dev == NULL) {
...@@ -532,6 +546,15 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -532,6 +546,15 @@ 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);
chip->reg = regulator_get(&client->dev, "vcc");
if (!IS_ERR(chip->reg)) {
ret = regulator_enable(chip->reg);
if (ret)
goto error_put_reg;
voltage_uv = regulator_get_voltage(chip->reg);
}
mutex_init(&chip->state_lock); 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);
...@@ -540,6 +563,13 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -540,6 +563,13 @@ static int __devinit ad7291_probe(struct i2c_client *client,
/* Tsense always enabled */ /* Tsense always enabled */
chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK; chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
if (voltage_uv) {
chip->int_vref_mv = voltage_uv / 1000;
chip->command |= AD7291_EXT_REF;
} else {
chip->int_vref_mv = 2500; /* Build-in ref */
}
indio_dev->name = id->name; indio_dev->name = id->name;
indio_dev->channels = ad7291_channels; indio_dev->channels = ad7291_channels;
indio_dev->num_channels = ARRAY_SIZE(ad7291_channels); indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
...@@ -556,7 +586,7 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -556,7 +586,7 @@ static int __devinit ad7291_probe(struct i2c_client *client,
id->name, id->name,
indio_dev); indio_dev);
if (ret) if (ret)
goto error_free_dev; goto error_disable_reg;
/* set irq polarity low level */ /* set irq polarity low level */
chip->command |= AD7291_ALERT_POLARITY; chip->command |= AD7291_ALERT_POLARITY;
...@@ -572,7 +602,7 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -572,7 +602,7 @@ static int __devinit ad7291_probe(struct i2c_client *client,
if (ret) if (ret)
goto error_unreg_irq; goto error_unreg_irq;
dev_info(&client->dev, "%s temperature sensor registered.\n", dev_info(&client->dev, "%s ADC registered.\n",
id->name); id->name);
return 0; return 0;
...@@ -580,7 +610,13 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -580,7 +610,13 @@ static int __devinit ad7291_probe(struct i2c_client *client,
error_unreg_irq: error_unreg_irq:
if (client->irq) if (client->irq)
free_irq(client->irq, indio_dev); free_irq(client->irq, indio_dev);
error_free_dev: error_disable_reg:
if (!IS_ERR(chip->reg))
regulator_disable(chip->reg);
error_put_reg:
if (!IS_ERR(chip->reg))
regulator_put(chip->reg);
iio_free_device(indio_dev); iio_free_device(indio_dev);
error_ret: error_ret:
return ret; return ret;
...@@ -589,9 +625,16 @@ static int __devinit ad7291_probe(struct i2c_client *client, ...@@ -589,9 +625,16 @@ static int __devinit ad7291_probe(struct i2c_client *client,
static int __devexit ad7291_remove(struct i2c_client *client) static int __devexit ad7291_remove(struct i2c_client *client)
{ {
struct iio_dev *indio_dev = i2c_get_clientdata(client); struct iio_dev *indio_dev = i2c_get_clientdata(client);
struct ad7291_chip_info *chip = iio_priv(indio_dev);
if (client->irq) if (client->irq)
free_irq(client->irq, indio_dev); free_irq(client->irq, indio_dev);
if (!IS_ERR(chip->reg)) {
regulator_disable(chip->reg);
regulator_put(chip->reg);
}
iio_device_unregister(indio_dev); iio_device_unregister(indio_dev);
return 0; return 0;
......
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