Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 259652
b: refs/heads/master
c: d9b42c0
h: refs/heads/master
v: v3
  • Loading branch information
Bryan Freed authored and Greg Kroah-Hartman committed Jun 28, 2011
1 parent 3f28092 commit a18c651
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 62 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: 9e4216fd6a547261c54bede60108347646c95a69
refs/heads/master: d9b42c01ae8d17fdf8d910a4255d18ed2ac22f90
106 changes: 45 additions & 61 deletions trunk/drivers/staging/iio/light/tsl2563.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,37 +137,14 @@ struct tsl2563_chip {
u32 data1;
};

static int tsl2563_write(struct i2c_client *client, u8 reg, u8 value)
{
int ret;
u8 buf[2];

buf[0] = TSL2563_CMD | reg;
buf[1] = value;

ret = i2c_master_send(client, buf, sizeof(buf));
return (ret == sizeof(buf)) ? 0 : ret;
}

static int tsl2563_read(struct i2c_client *client, u8 reg, void *buf, int len)
{
int ret;
u8 cmd = TSL2563_CMD | reg;

ret = i2c_master_send(client, &cmd, sizeof(cmd));
if (ret != sizeof(cmd))
return ret;

return i2c_master_recv(client, buf, len);
}

static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
{
struct i2c_client *client = chip->client;
u8 cmd;

cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
return tsl2563_write(client, TSL2563_REG_CTRL, cmd);
return i2c_smbus_write_byte_data(client,
TSL2563_CMD | TSL2563_REG_CTRL, cmd);
}

/*
Expand All @@ -178,36 +155,40 @@ static int tsl2563_get_power(struct tsl2563_chip *chip)
{
struct i2c_client *client = chip->client;
int ret;
u8 val;

ret = tsl2563_read(client, TSL2563_REG_CTRL, &val, sizeof(val));
if (ret != sizeof(val))
ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
if (ret < 0)
return ret;

return (val & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
}

static int tsl2563_configure(struct tsl2563_chip *chip)
{
int ret;

ret = tsl2563_write(chip->client, TSL2563_REG_TIMING,
ret = i2c_smbus_write_byte_data(chip->client,
TSL2563_CMD | TSL2563_REG_TIMING,
chip->gainlevel->gaintime);
if (ret)
goto error_ret;
ret = tsl2563_write(chip->client, TSL2563_REG_HIGHLOW,
ret = i2c_smbus_write_byte_data(chip->client,
TSL2563_CMD | TSL2563_REG_HIGHLOW,
chip->high_thres & 0xFF);
if (ret)
goto error_ret;
ret = tsl2563_write(chip->client, TSL2563_REG_HIGHHIGH,
ret = i2c_smbus_write_byte_data(chip->client,
TSL2563_CMD | TSL2563_REG_HIGHHIGH,
(chip->high_thres >> 8) & 0xFF);
if (ret)
goto error_ret;
ret = tsl2563_write(chip->client, TSL2563_REG_LOWLOW,
ret = i2c_smbus_write_byte_data(chip->client,
TSL2563_CMD | TSL2563_REG_LOWLOW,
chip->low_thres & 0xFF);
if (ret)
goto error_ret;
ret = tsl2563_write(chip->client, TSL2563_REG_LOWHIGH,
ret = i2c_smbus_write_byte_data(chip->client,
TSL2563_CMD | TSL2563_REG_LOWHIGH,
(chip->low_thres >> 8) & 0xFF);
/* Interrupt register is automatically written anyway if it is relevant
so is not here */
Expand Down Expand Up @@ -242,8 +223,8 @@ static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
struct i2c_client *client = chip->client;
int ret;

ret = tsl2563_read(client, TSL2563_REG_ID, id, sizeof(*id));
if (ret != sizeof(*id))
ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
if (ret < 0)
return ret;

return 0;
Expand Down Expand Up @@ -313,8 +294,9 @@ static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
(adc > chip->gainlevel->max) ?
chip->gainlevel++ : chip->gainlevel--;

tsl2563_write(client, TSL2563_REG_TIMING,
chip->gainlevel->gaintime);
i2c_smbus_write_byte_data(client,
TSL2563_CMD | TSL2563_REG_TIMING,
chip->gainlevel->gaintime);

tsl2563_wait_adc(chip);
tsl2563_wait_adc(chip);
Expand All @@ -327,7 +309,6 @@ static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
static int tsl2563_get_adc(struct tsl2563_chip *chip)
{
struct i2c_client *client = chip->client;
u8 buf0[2], buf1[2];
u16 adc0, adc1;
int retry = 1;
int ret = 0;
Expand All @@ -350,19 +331,17 @@ static int tsl2563_get_adc(struct tsl2563_chip *chip)
}

while (retry) {
ret = tsl2563_read(client,
TSL2563_REG_DATA0LOW,
buf0, sizeof(buf0));
if (ret != sizeof(buf0))
ret = i2c_smbus_read_word_data(client,
TSL2563_CMD | TSL2563_REG_DATA0LOW);
if (ret < 0)
goto out;
adc0 = ret;

ret = tsl2563_read(client, TSL2563_REG_DATA1LOW,
buf1, sizeof(buf1));
if (ret != sizeof(buf1))
ret = i2c_smbus_read_word_data(client,
TSL2563_CMD | TSL2563_REG_DATA1LOW);
if (ret < 0)
goto out;

adc0 = (buf0[1] << 8) + buf0[0];
adc1 = (buf1[1] << 8) + buf1[0];
adc1 = ret;

retry = tsl2563_adjust_gainlevel(chip, adc0);
}
Expand Down Expand Up @@ -593,11 +572,13 @@ static ssize_t tsl2563_write_thresh(struct iio_dev *indio_dev,
else
address = TSL2563_REG_LOWLOW;
mutex_lock(&chip->lock);
ret = tsl2563_write(chip->client, address, val & 0xFF);
ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
val & 0xFF);
if (ret)
goto error_ret;
ret = tsl2563_write(chip->client, address + 1,
(val >> 8) & 0xFF);
ret = i2c_smbus_write_byte_data(chip->client,
TSL2563_CMD | (address + 1),
(val >> 8) & 0xFF);
if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
chip->high_thres = val;
else
Expand All @@ -613,7 +594,6 @@ static irqreturn_t tsl2563_event_handler(int irq, void *private)
{
struct iio_dev *dev_info = private;
struct tsl2563_chip *chip = iio_priv(dev_info);
u8 cmd = TSL2563_CMD | TSL2563_CLEARINT;

iio_push_event(dev_info, 0,
IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_LIGHT,
Expand All @@ -623,7 +603,7 @@ static irqreturn_t tsl2563_event_handler(int irq, void *private)
iio_get_time_ns());

/* clear the interrupt and push the event */
i2c_master_send(chip->client, &cmd, sizeof(cmd));
i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
return IRQ_HANDLED;
}

Expand All @@ -648,13 +628,17 @@ static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
if (ret)
goto out;
}
ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
ret = i2c_smbus_write_byte_data(chip->client,
TSL2563_CMD | TSL2563_REG_INT,
chip->intr);
chip->int_enabled = true;
}

if (!state && (chip->intr & 0x30)) {
chip->intr |= ~0x30;
ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
ret = i2c_smbus_write_byte_data(chip->client,
TSL2563_CMD | TSL2563_REG_INT,
chip->intr);
chip->int_enabled = false;
/* now the interrupt is not enabled, we can go to sleep */
schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
Expand All @@ -669,16 +653,15 @@ static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
int event_code)
{
struct tsl2563_chip *chip = iio_priv(indio_dev);
u8 rxbuf;
int ret;

mutex_lock(&chip->lock);
ret = tsl2563_read(chip->client, TSL2563_REG_INT,
&rxbuf, sizeof(rxbuf));
ret = i2c_smbus_read_byte_data(chip->client,
TSL2563_CMD | TSL2563_REG_INT);
mutex_unlock(&chip->lock);
if (ret < 0)
goto error_ret;
ret = !!(rxbuf & 0x30);
ret = !!(ret & 0x30);
error_ret:

return ret;
Expand Down Expand Up @@ -800,7 +783,8 @@ static int tsl2563_remove(struct i2c_client *client)
cancel_delayed_work(&chip->poweroff_work);
/* Ensure that interrupts are disabled - then flush any bottom halves */
chip->intr |= ~0x30;
tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
chip->intr);
flush_scheduled_work();
tsl2563_set_power(chip, 0);
if (client->irq)
Expand Down

0 comments on commit a18c651

Please sign in to comment.