staging:iio:magnetometer:ak8975: allocate chip state with iio_dev.

Here the ordering is a little tricky, so to keep changes minimal, a copy of the gpio
number is introduced.

Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
Jonathan Cameron 2011-06-27 13:07:54 +01:00 committed by Greg Kroah-Hartman
parent 58f08b0af8
commit 338473c819
1 changed files with 40 additions and 51 deletions

View File

@ -89,7 +89,6 @@
*/ */
struct ak8975_data { struct ak8975_data {
struct i2c_client *client; struct i2c_client *client;
struct iio_dev *indio_dev;
struct attribute_group attrs; struct attribute_group attrs;
struct mutex lock; struct mutex lock;
u8 asa[3]; u8 asa[3];
@ -221,7 +220,7 @@ static ssize_t show_mode(struct device *dev, struct device_attribute *devattr,
char *buf) char *buf)
{ {
struct iio_dev *indio_dev = dev_get_drvdata(dev); struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct ak8975_data *data = indio_dev->dev_data; struct ak8975_data *data = iio_priv(indio_dev);
return sprintf(buf, "%lu\n", data->mode); return sprintf(buf, "%lu\n", data->mode);
} }
@ -234,7 +233,7 @@ static ssize_t store_mode(struct device *dev, struct device_attribute *devattr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct iio_dev *indio_dev = dev_get_drvdata(dev); struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct ak8975_data *data = indio_dev->dev_data; struct ak8975_data *data = iio_priv(indio_dev);
struct i2c_client *client = data->client; struct i2c_client *client = data->client;
unsigned long oval; unsigned long oval;
int ret; int ret;
@ -310,7 +309,7 @@ static ssize_t show_scale(struct device *dev, struct device_attribute *devattr,
char *buf) char *buf)
{ {
struct iio_dev *indio_dev = dev_get_drvdata(dev); struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct ak8975_data *data = indio_dev->dev_data; struct ak8975_data *data = iio_priv(indio_dev);
struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr); struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr);
return sprintf(buf, "%ld\n", data->raw_to_gauss[this_attr->address]); return sprintf(buf, "%ld\n", data->raw_to_gauss[this_attr->address]);
@ -376,7 +375,7 @@ static ssize_t show_raw(struct device *dev, struct device_attribute *devattr,
char *buf) char *buf)
{ {
struct iio_dev *indio_dev = dev_get_drvdata(dev); struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct ak8975_data *data = indio_dev->dev_data; struct ak8975_data *data = iio_priv(indio_dev);
struct i2c_client *client = data->client; struct i2c_client *client = data->client;
struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr); struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr);
u16 meas_reg; u16 meas_reg;
@ -483,46 +482,41 @@ static int ak8975_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
struct ak8975_data *data; struct ak8975_data *data;
struct iio_dev *indio_dev;
int eoc_gpio;
int err; int err;
/* Allocate our device context. */
data = kzalloc(sizeof(struct ak8975_data), GFP_KERNEL);
if (!data) {
dev_err(&client->dev, "Memory allocation fails\n");
err = -ENOMEM;
goto exit;
}
i2c_set_clientdata(client, data);
data->client = client;
mutex_init(&data->lock);
/* Grab and set up the supplied GPIO. */ /* Grab and set up the supplied GPIO. */
data->eoc_irq = client->irq; eoc_gpio = irq_to_gpio(client->irq);
data->eoc_gpio = irq_to_gpio(client->irq);
/* We may not have a GPIO based IRQ to scan, that is fine, we will /* We may not have a GPIO based IRQ to scan, that is fine, we will
poll if so */ poll if so */
if (data->eoc_gpio > 0) { if (eoc_gpio > 0) {
err = gpio_request(data->eoc_gpio, "ak_8975"); err = gpio_request(eoc_gpio, "ak_8975");
if (err < 0) { if (err < 0) {
dev_err(&client->dev, dev_err(&client->dev,
"failed to request GPIO %d, error %d\n", "failed to request GPIO %d, error %d\n",
data->eoc_gpio, err); eoc_gpio, err);
goto exit_free; goto exit;
} }
err = gpio_direction_input(data->eoc_gpio); err = gpio_direction_input(eoc_gpio);
if (err < 0) { if (err < 0) {
dev_err(&client->dev, dev_err(&client->dev,
"Failed to configure input direction for GPIO %d, error %d\n", "Failed to configure input direction for GPIO %d, error %d\n",
data->eoc_gpio, err); eoc_gpio, err);
goto exit_gpio; goto exit_gpio;
} }
} else } else
data->eoc_gpio = 0; /* No GPIO available */ eoc_gpio = 0; /* No GPIO available */
/* Register with IIO */
indio_dev = iio_allocate_device(sizeof(*data));
if (indio_dev == NULL) {
err = -ENOMEM;
goto exit_gpio;
}
data = iio_priv(indio_dev);
/* Perform some basic start-of-day setup of the device. */ /* Perform some basic start-of-day setup of the device. */
err = ak8975_setup(client); err = ak8975_setup(client);
if (err < 0) { if (err < 0) {
@ -530,46 +524,41 @@ static int ak8975_probe(struct i2c_client *client,
goto exit_gpio; goto exit_gpio;
} }
/* Register with IIO */ i2c_set_clientdata(client, indio_dev);
data->indio_dev = iio_allocate_device(0); data->client = client;
if (data->indio_dev == NULL) { mutex_init(&data->lock);
err = -ENOMEM; data->eoc_irq = client->irq;
goto exit_gpio; data->eoc_gpio = eoc_gpio;
} indio_dev->dev.parent = &client->dev;
indio_dev->info = &ak8975_info;
indio_dev->modes = INDIO_DIRECT_MODE;
data->indio_dev->dev.parent = &client->dev; err = iio_device_register(indio_dev);
data->indio_dev->info = &ak8975_info;
data->indio_dev->dev_data = (void *)(data);
data->indio_dev->modes = INDIO_DIRECT_MODE;
err = iio_device_register(data->indio_dev);
if (err < 0) if (err < 0)
goto exit_free_iio; goto exit_free_iio;
return 0; return 0;
exit_free_iio: exit_free_iio:
iio_free_device(data->indio_dev); iio_free_device(indio_dev);
exit_gpio: exit_gpio:
if (data->eoc_gpio) if (eoc_gpio)
gpio_free(data->eoc_gpio); gpio_free(eoc_gpio);
exit_free:
kfree(data);
exit: exit:
return err; return err;
} }
static int ak8975_remove(struct i2c_client *client) static int ak8975_remove(struct i2c_client *client)
{ {
struct ak8975_data *data = i2c_get_clientdata(client); struct iio_dev *indio_dev = i2c_get_clientdata(client);
struct ak8975_data *data = iio_priv(indio_dev);
int eoc_gpio = data->eoc_gpio;
iio_device_unregister(data->indio_dev); iio_device_unregister(indio_dev);
iio_free_device(data->indio_dev); iio_free_device(indio_dev);
if (data->eoc_gpio) if (eoc_gpio)
gpio_free(data->eoc_gpio); gpio_free(eoc_gpio);
kfree(data);
return 0; return 0;
} }