Skip to content

Commit

Permalink
staging:iio:adc:adt75: allocate chip state with iio_dev and cleanup s…
Browse files Browse the repository at this point in the history
…ome function calls.

Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk>
Acked-by: Michael Hennerich <michael.hennerich@analog.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Jonathan Cameron authored and Greg Kroah-Hartman committed Jun 28, 2011
1 parent b0011d6 commit 84e8d84
Showing 1 changed file with 56 additions and 67 deletions.
123 changes: 56 additions & 67 deletions drivers/staging/iio/adc/adt75.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@

struct adt75_chip_info {
struct i2c_client *client;
struct iio_dev *indio_dev;
u8 config;
};

/*
* adt75 register access by I2C
*/

static int adt75_i2c_read(struct adt75_chip_info *chip, u8 reg, u8 *data)
static int adt75_i2c_read(struct iio_dev *dev_info, u8 reg, u8 *data)
{
struct adt75_chip_info *chip = iio_priv(dev_info);
struct i2c_client *client = chip->client;
int ret = 0, len;

Expand All @@ -84,8 +84,9 @@ static int adt75_i2c_read(struct adt75_chip_info *chip, u8 reg, u8 *data)
return ret;
}

static int adt75_i2c_write(struct adt75_chip_info *chip, u8 reg, u8 data)
static int adt75_i2c_write(struct iio_dev *dev_info, u8 reg, u8 data)
{
struct adt75_chip_info *chip = iio_priv(dev_info);
struct i2c_client *client = chip->client;
int ret = 0;

Expand All @@ -104,8 +105,7 @@ static ssize_t adt75_show_mode(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_get_drvdata(dev));

if (chip->config & ADT75_PD)
return sprintf(buf, "power-save\n");
Expand All @@ -119,19 +119,19 @@ static ssize_t adt75_store_mode(struct device *dev,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
int ret;
u8 config;

ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
if (ret)
return -EIO;

config = chip->config & ~ADT75_PD;
if (!strcmp(buf, "full"))
config |= ADT75_PD;

ret = adt75_i2c_write(chip, ADT75_CONFIG, config);
ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
if (ret)
return -EIO;

Expand All @@ -158,8 +158,7 @@ static ssize_t adt75_show_oneshot(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_get_drvdata(dev));

return sprintf(buf, "%d\n", !!(chip->config & ADT75_ONESHOT));
}
Expand All @@ -170,7 +169,7 @@ static ssize_t adt75_store_oneshot(struct device *dev,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
unsigned long data = 0;
int ret;
u8 config;
Expand All @@ -180,15 +179,15 @@ static ssize_t adt75_store_oneshot(struct device *dev,
return -EINVAL;


ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
if (ret)
return -EIO;

config = chip->config & ~ADT75_ONESHOT;
if (data)
config |= ADT75_ONESHOT;

ret = adt75_i2c_write(chip, ADT75_CONFIG, config);
ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
if (ret)
return -EIO;

Expand All @@ -207,7 +206,7 @@ static ssize_t adt75_show_value(struct device *dev,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
u16 data;
char sign = ' ';
int ret;
Expand All @@ -224,7 +223,7 @@ static ssize_t adt75_show_value(struct device *dev,
return -EIO;
}

ret = adt75_i2c_read(chip, ADT75_TEMPERATURE, (u8 *)&data);
ret = adt75_i2c_read(dev_info, ADT75_TEMPERATURE, (u8 *)&data);
if (ret)
return -EIO;

Expand Down Expand Up @@ -277,11 +276,11 @@ static ssize_t adt75_show_oti_mode(struct device *dev,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
int ret;

/* retrive ALART status */
ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
if (ret)
return -EIO;

Expand All @@ -297,20 +296,20 @@ static ssize_t adt75_set_oti_mode(struct device *dev,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
int ret;
u8 config;

/* retrive ALART status */
ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
if (ret)
return -EIO;

config = chip->config & ~ADT75_OS_INT;
if (strcmp(buf, "comparator") != 0)
config |= ADT75_OS_INT;

ret = adt75_i2c_write(chip, ADT75_CONFIG, config);
ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
if (ret)
return -EIO;

Expand All @@ -331,11 +330,11 @@ static ssize_t adt75_show_smbus_alart(struct device *dev,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
int ret;

/* retrive ALART status */
ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
if (ret)
return -EIO;

Expand All @@ -348,7 +347,7 @@ static ssize_t adt75_set_smbus_alart(struct device *dev,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
unsigned long data = 0;
int ret;
u8 config;
Expand All @@ -358,15 +357,15 @@ static ssize_t adt75_set_smbus_alart(struct device *dev,
return -EINVAL;

/* retrive ALART status */
ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
if (ret)
return -EIO;

config = chip->config & ~ADT75_SMBUS_ALART;
if (data)
config |= ADT75_SMBUS_ALART;

ret = adt75_i2c_write(chip, ADT75_CONFIG, config);
ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
if (ret)
return -EIO;

Expand All @@ -380,11 +379,11 @@ static ssize_t adt75_show_fault_queue(struct device *dev,
char *buf)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
int ret;

/* retrive ALART status */
ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
if (ret)
return -EIO;

Expand All @@ -398,7 +397,7 @@ static ssize_t adt75_set_fault_queue(struct device *dev,
size_t len)
{
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
struct adt75_chip_info *chip = iio_priv(dev_info);
unsigned long data;
int ret;
u8 config;
Expand All @@ -408,13 +407,13 @@ static ssize_t adt75_set_fault_queue(struct device *dev,
return -EINVAL;

/* retrive ALART status */
ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
if (ret)
return -EIO;

config = chip->config & ~ADT75_FAULT_QUEUE_MASK;
config |= (data << ADT75_FAULT_QUEUE_OFFSET);
ret = adt75_i2c_write(chip, ADT75_CONFIG, config);
ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
if (ret)
return -EIO;

Expand All @@ -428,12 +427,11 @@ static inline ssize_t adt75_show_t_bound(struct device *dev,
{
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
u16 data;
char sign = ' ';
int ret;

ret = adt75_i2c_read(chip, this_attr->address, (u8 *)&data);
ret = adt75_i2c_read(dev_info, this_attr->address, (u8 *)&data);
if (ret)
return -EIO;

Expand All @@ -456,7 +454,6 @@ static inline ssize_t adt75_set_t_bound(struct device *dev,
{
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
struct iio_dev *dev_info = dev_get_drvdata(dev);
struct adt75_chip_info *chip = dev_info->dev_data;
long tmp1, tmp2;
u16 data;
char *pos;
Expand Down Expand Up @@ -491,7 +488,7 @@ static inline ssize_t adt75_set_t_bound(struct device *dev,
data <<= ADT75_VALUE_OFFSET;
data = swab16(data);

ret = adt75_i2c_write(chip, this_attr->address, (u8)data);
ret = adt75_i2c_write(dev_info, this_attr->address, (u8)data);
if (ret)
return -EIO;

Expand Down Expand Up @@ -549,31 +546,27 @@ static int __devinit adt75_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct adt75_chip_info *chip;
struct iio_dev *indio_dev;
int ret = 0;

chip = kzalloc(sizeof(struct adt75_chip_info), GFP_KERNEL);

if (chip == NULL)
return -ENOMEM;
indio_dev = iio_allocate_device(sizeof(*chip));
if (indio_dev == NULL) {
ret = -ENOMEM;
goto error_ret;
}
chip = iio_priv(indio_dev);

/* this is only used for device removal purposes */
i2c_set_clientdata(client, chip);
i2c_set_clientdata(client, indio_dev);

chip->client = client;

chip->indio_dev = iio_allocate_device(0);
if (chip->indio_dev == NULL) {
ret = -ENOMEM;
goto error_free_chip;
}

chip->indio_dev->name = id->name;
chip->indio_dev->dev.parent = &client->dev;
chip->indio_dev->info = &adt75_info;
chip->indio_dev->dev_data = (void *)chip;
chip->indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->name = id->name;
indio_dev->dev.parent = &client->dev;
indio_dev->info = &adt75_info;
indio_dev->modes = INDIO_DIRECT_MODE;

ret = iio_device_register(chip->indio_dev);
ret = iio_device_register(indio_dev);
if (ret)
goto error_free_dev;

Expand All @@ -582,12 +575,12 @@ static int __devinit adt75_probe(struct i2c_client *client,
NULL,
&adt75_event_handler,
IRQF_TRIGGER_LOW,
chip->indio_dev->name,
chip->indio_dev);
indio_dev->name,
indio_dev);
if (ret)
goto error_unreg_dev;

ret = adt75_i2c_read(chip, ADT75_CONFIG, &chip->config);
ret = adt75_i2c_read(indio_dev, ADT75_CONFIG, &chip->config);
if (ret) {
ret = -EIO;
goto error_unreg_irq;
Expand All @@ -596,39 +589,35 @@ static int __devinit adt75_probe(struct i2c_client *client,
/* set irq polarity low level */
chip->config &= ~ADT75_OS_POLARITY;

ret = adt75_i2c_write(chip, ADT75_CONFIG, chip->config);
ret = adt75_i2c_write(indio_dev, ADT75_CONFIG, chip->config);
if (ret) {
ret = -EIO;
goto error_unreg_irq;
}
}

dev_info(&client->dev, "%s temperature sensor registered.\n",
chip->indio_dev->name);
indio_dev->name);

return 0;
error_unreg_irq:
free_irq(client->irq, chip->indio_dev);
free_irq(client->irq, indio_dev);
error_unreg_dev:
iio_device_unregister(chip->indio_dev);
iio_device_unregister(indio_dev);
error_free_dev:
iio_free_device(chip->indio_dev);
error_free_chip:
kfree(chip);

iio_free_device(indio_dev);
error_ret:
return ret;
}

static int __devexit adt75_remove(struct i2c_client *client)
{
struct adt75_chip_info *chip = i2c_get_clientdata(client);
struct iio_dev *indio_dev = chip->indio_dev;
struct iio_dev *indio_dev = i2c_get_clientdata(client);

if (client->irq)
free_irq(client->irq, chip->indio_dev);
free_irq(client->irq, indio_dev);
iio_device_unregister(indio_dev);
iio_free_device(chip->indio_dev);
kfree(chip);
iio_free_device(indio_dev);

return 0;
}
Expand Down

0 comments on commit 84e8d84

Please sign in to comment.