0
\$\begingroup\$

i2c_smbus_read_byte() returns a negative value when there is an error in reading data. But what if it returns 0?

Following is the code from adt7316-i2c driver

static int adt7316_i2c_read(void *client, u8 reg, u8 *data)
{
   struct i2c_client *cl = client;
   int ret;

   ret = i2c_smbus_write_byte(cl, reg);
   if (ret < 0) {
     dev_err(&cl->dev, "I2C fail to select reg\n");
     return ret;
   }

   ret = i2c_smbus_read_byte(client);
   if (ret < 0) {
      dev_err(&cl->dev, "I2C read error\n");
      return ret;
   }

   *data = ret;

   return 0;
}

Even if i2c_smbus_read_byte() returns 0 here then the driver will successfully probe. So my main concern is to know whether returing of 0 from i2c_smbus_read_byte() is a valid case or not for a driver to probe.

I am not sure but maybe it means that there is no error and also no data has been read. If this is the case then under what situation does this happen?

\$\endgroup\$
  • \$\begingroup\$ You should specify what platform this is, but I would assume it's reading a value of zero. \$\endgroup\$ – Hearth Dec 5 '18 at 22:52
  • 1
    \$\begingroup\$ @Hearth so you mean that a byte was read with a value of 0. For instance, if the input to the adc is 0V. Right? \$\endgroup\$ – Shreeya Patel Dec 5 '18 at 22:59
  • \$\begingroup\$ Yes, that is what I mean. \$\endgroup\$ – Hearth Dec 5 '18 at 23:00
  • 1
    \$\begingroup\$ You should post your function code. Otherwise we can never be sure what function you are using. If this is based on a library, there must be some documentation which tells you about the return codes of the function. \$\endgroup\$ – A.R.C. Dec 6 '18 at 14:47
  • \$\begingroup\$ @A.R.C., ok I am posting the code. Please check it out. \$\endgroup\$ – Shreeya Patel Dec 7 '18 at 18:12
-1
\$\begingroup\$

As explained on LKML a return value of zero indicates that this is the byte received from the I2C bus. It is data, not the length of data.

What receiving 0 from the chip means, and if the chip is or isn't supposed to be sending a zero is entirely contextual, essentially depending on what question it was asked.

Since the code you depict appears to be a pass-through function that will be used for many purposes, the code can't really know if 0 is or is not valid data, unless you know for a fact that there is no legitimate situation with this chip in which any access could return data of value 0... which seems a bit unlikely, as 0 is actually an extremely common value for configuration or status registers and especially in multi-byte values...

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ It's further worth noting that the only way an I2C master can fail to read data is to decide that an actual error has occurred. If some master's driver is buggy in that it makes this decision but fails to return an error code, that would need to be dealt with in that particular master's driver, not in the driver for a peripheral that could potentially by used with any system's I2C master. \$\endgroup\$ – Chris Stratton Dec 7 '18 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.