Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 249091
b: refs/heads/master
c: 01fbb47
h: refs/heads/master
i:
  249089: ad3b1a3
  249087: c9f126e
v: v3
  • Loading branch information
Alan Cox authored and Greg Kroah-Hartman committed Apr 20, 2011
1 parent 2c46e70 commit 47833ed
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 38 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: acf0b9e8194b9e70e661d38f7485d801f3e10e51
refs/heads/master: 01fbb4781fc2865a55a19c44403bc69b34f1de43
120 changes: 83 additions & 37 deletions trunk/drivers/staging/iio/magnetometer/ak8975.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static int ak8975_setup(struct i2c_client *client)
}

/* Precalculate scale factor for each axis and
store in the device data. */
store in the device data. */
data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
Expand Down Expand Up @@ -316,6 +316,59 @@ static ssize_t show_scale(struct device *dev, struct device_attribute *devattr,
return sprintf(buf, "%ld\n", data->raw_to_gauss[this_attr->address]);
}

static int wait_conversion_complete_gpio(struct ak8975_data *data)
{
struct i2c_client *client = data->client;
u8 read_status;
u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
int ret;

/* Wait for the conversion to complete. */
while (timeout_ms) {
msleep(AK8975_CONVERSION_DONE_POLL_TIME);
if (gpio_get_value(data->eoc_gpio))
break;
timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
}
if (!timeout_ms) {
dev_err(&client->dev, "Conversion timeout happened\n");
return -EINVAL;
}

ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
if (ret < 0) {
dev_err(&client->dev, "Error in reading ST1\n");
return ret;
}
return read_status;
}

static int wait_conversion_complete_polled(struct ak8975_data *data)
{
struct i2c_client *client = data->client;
u8 read_status;
u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
int ret;

/* Wait for the conversion to complete. */
while (timeout_ms) {
msleep(AK8975_CONVERSION_DONE_POLL_TIME);
ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
if (ret < 0) {
dev_err(&client->dev, "Error in reading ST1\n");
return ret;
}
if (read_status)
break;
timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
}
if (!timeout_ms) {
dev_err(&client->dev, "Conversion timeout happened\n");
return -EINVAL;
}
return read_status;
}

/*
* Emits the raw flux value for the x, y, or z axis.
*/
Expand All @@ -326,7 +379,6 @@ static ssize_t show_raw(struct device *dev, struct device_attribute *devattr,
struct ak8975_data *data = indio_dev->dev_data;
struct i2c_client *client = data->client;
struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr);
u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
u16 meas_reg;
s16 raw;
u8 read_status;
Expand All @@ -352,23 +404,14 @@ static ssize_t show_raw(struct device *dev, struct device_attribute *devattr,
}

/* Wait for the conversion to complete. */
while (timeout_ms) {
msleep(AK8975_CONVERSION_DONE_POLL_TIME);
if (gpio_get_value(data->eoc_gpio))
break;
timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
}
if (!timeout_ms) {
dev_err(&client->dev, "Conversion timeout happened\n");
ret = -EINVAL;
if (data->eoc_gpio)
ret = wait_conversion_complete_gpio(data);
else
ret = wait_conversion_complete_polled(data);
if (ret < 0)
goto exit;
}

ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
if (ret < 0) {
dev_err(&client->dev, "Error in reading ST1\n");
goto exit;
}
read_status = ret;

if (read_status & AK8975_REG_ST1_DRDY_MASK) {
ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status);
Expand Down Expand Up @@ -454,25 +497,26 @@ static int ak8975_probe(struct i2c_client *client,
data->eoc_irq = client->irq;
data->eoc_gpio = irq_to_gpio(client->irq);

if (!data->eoc_gpio) {
dev_err(&client->dev, "failed, no valid GPIO\n");
err = -EINVAL;
goto exit_free;
}

err = gpio_request(data->eoc_gpio, "ak_8975");
if (err < 0) {
dev_err(&client->dev, "failed to request GPIO %d, error %d\n",
data->eoc_gpio, err);
goto exit_free;
}
/* We may not have a GPIO based IRQ to scan, that is fine, we will
poll if so */
if (data->eoc_gpio > 0) {
err = gpio_request(data->eoc_gpio, "ak_8975");
if (err < 0) {
dev_err(&client->dev,
"failed to request GPIO %d, error %d\n",
data->eoc_gpio, err);
goto exit_free;
}

err = gpio_direction_input(data->eoc_gpio);
if (err < 0) {
dev_err(&client->dev, "Failed to configure input direction for"
" GPIO %d, error %d\n", data->eoc_gpio, err);
goto exit_gpio;
}
err = gpio_direction_input(data->eoc_gpio);
if (err < 0) {
dev_err(&client->dev,
"Failed to configure input direction for GPIO %d, error %d\n",
data->eoc_gpio, err);
goto exit_gpio;
}
} else
data->eoc_gpio = 0; /* No GPIO available */

/* Perform some basic start-of-day setup of the device. */
err = ak8975_setup(client);
Expand Down Expand Up @@ -503,7 +547,8 @@ static int ak8975_probe(struct i2c_client *client,
exit_free_iio:
iio_free_device(data->indio_dev);
exit_gpio:
gpio_free(data->eoc_gpio);
if (data->eoc_gpio)
gpio_free(data->eoc_gpio);
exit_free:
kfree(data);
exit:
Expand All @@ -517,7 +562,8 @@ static int ak8975_remove(struct i2c_client *client)
iio_device_unregister(data->indio_dev);
iio_free_device(data->indio_dev);

gpio_free(data->eoc_gpio);
if (data->eoc_gpio)
gpio_free(data->eoc_gpio);

kfree(data);

Expand Down

0 comments on commit 47833ed

Please sign in to comment.