Re: [PATCH v4 4/4] iio: humidity: si7020: added No Hold read mode

2015-10-29 Thread Lars-Peter Clausen
>> So maybe more like this:
>>
>> static inline bool i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
>> {
>> if (!adap->quirks)
>> return false;
>> return (adap->quirks->flags & quirks) == quirks;
>> }
> 
> Should I use bool (like in your snippet) or int (like 
> i2c_check_functionality) as return type?

I'd use bool, given that the result is a boolean value. It's semantically
more clear this way.
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v4 4/4] iio: humidity: si7020: added No Hold read mode

2015-10-28 Thread Nicola Corna
October 28 2015 7:46 PM, "Lars-Peter Clausen"  wrote:

> On 10/28/2015 07:35 PM, Nicola Corna wrote:
> 
>> October 28 2015 10:38 AM, "Lars-Peter Clausen"  wrote:
>> 
>>> On 10/28/2015 07:58 AM, Nicola Corna wrote:
>>> [...]
>>> 
 + holdmode = !((*client)->adapter->quirks &&
 + (*client)->adapter->quirks->flags &
>>> 
>>> [...]
>>> 
 + client->adapter->quirks &&
 + client->adapter->quirks->flags & I2C_AQ_NO_CLK_STRETCH)
>>> 
>>> This is rather ugly, can we get a helper in the I2C core something along the
>>> lines of
>>> 
>>> i2c_check_quirks(client->adapter, I2C_AQ_NO_CLK_STRETCH)
>>> 
>>> - Lars
>> 
>> Something like this?
>> 
>> ---
>> diff --git a/include/linux/i2c.h b/include/linux/i2c.h
>> index a69a9a0..a06ffc0 100644
>> --- a/include/linux/i2c.h
>> +++ b/include/linux/i2c.h
>> @@ -613,6 +613,12 @@ static inline int i2c_check_functionality(struct 
>> i2c_adapter *adap, u32 func)
>> return (func & i2c_get_functionality(adap)) == func;
>> }
>> 
>> +/* Return 1 if adapter has the specified quirks, 0 if not. */
>> +static inline int i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
>> +{
>> + return (quirks & (adap->quirks ? adap->quirks->flags : 0)) == quirks;
>> +}
> 
> This is not a code obfuscation contest ;)

I love one-liners ;)

> So maybe more like this:
> 
> static inline bool i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
> {
> if (!adap->quirks)
> return false;
> return (adap->quirks->flags & quirks) == quirks;
> }

Should I use bool (like in your snippet) or int (like i2c_check_functionality) 
as return type?

> And please use kernel-doc for the documentation.
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v4 4/4] iio: humidity: si7020: added No Hold read mode

2015-10-28 Thread Nicola Corna
October 28 2015 7:46 PM, "Lars-Peter Clausen"  wrote:

> On 10/28/2015 07:35 PM, Nicola Corna wrote:
> 
>> October 28 2015 10:38 AM, "Lars-Peter Clausen"  wrote:
>> 
>>> On 10/28/2015 07:58 AM, Nicola Corna wrote:
>>> [...]
>>> 
 + holdmode = !((*client)->adapter->quirks &&
 + (*client)->adapter->quirks->flags &
>>> 
>>> [...]
>>> 
 + client->adapter->quirks &&
 + client->adapter->quirks->flags & I2C_AQ_NO_CLK_STRETCH)
>>> 
>>> This is rather ugly, can we get a helper in the I2C core something along the
>>> lines of
>>> 
>>> i2c_check_quirks(client->adapter, I2C_AQ_NO_CLK_STRETCH)
>>> 
>>> - Lars
>> 
>> Something like this?
>> 
>> ---
>> diff --git a/include/linux/i2c.h b/include/linux/i2c.h
>> index a69a9a0..a06ffc0 100644
>> --- a/include/linux/i2c.h
>> +++ b/include/linux/i2c.h
>> @@ -613,6 +613,12 @@ static inline int i2c_check_functionality(struct 
>> i2c_adapter *adap, u32 func)
>> return (func & i2c_get_functionality(adap)) == func;
>> }
>> 
>> +/* Return 1 if adapter has the specified quirks, 0 if not. */
>> +static inline int i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
>> +{
>> + return (quirks & (adap->quirks ? adap->quirks->flags : 0)) == quirks;
>> +}
> 
> This is not a code obfuscation contest ;)

I love one-liners ;)

> So maybe more like this:
> 
> static inline bool i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
> {
> if (!adap->quirks)
> return false;
> return (adap->quirks->flags & quirks) == quirks;
> }

Should I use bool (like in your snippet) or int (like i2c_check_functionality) 
as return type?

> And please use kernel-doc for the documentation.
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v4 4/4] iio: humidity: si7020: added No Hold read mode

2015-10-28 Thread Lars-Peter Clausen
On 10/28/2015 07:35 PM, Nicola Corna wrote:
> October 28 2015 10:38 AM, "Lars-Peter Clausen"  wrote:
> 
>> On 10/28/2015 07:58 AM, Nicola Corna wrote:
>> [...]
>>
>>> + holdmode = !((*client)->adapter->quirks &&
>>> + (*client)->adapter->quirks->flags &
>>
>> [...]
>>
>>> + client->adapter->quirks &&
>>> + client->adapter->quirks->flags & I2C_AQ_NO_CLK_STRETCH)
>>
>> This is rather ugly, can we get a helper in the I2C core something along the
>> lines of
>>
>> i2c_check_quirks(client->adapter, I2C_AQ_NO_CLK_STRETCH)
>>
>> - Lars
> 
> Something like this?
> 
> ---
> diff --git a/include/linux/i2c.h b/include/linux/i2c.h
> index a69a9a0..a06ffc0 100644
> --- a/include/linux/i2c.h
> +++ b/include/linux/i2c.h
> @@ -613,6 +613,12 @@ static inline int i2c_check_functionality(struct 
> i2c_adapter *adap, u32 func)
> return (func & i2c_get_functionality(adap)) == func;
> }
> 
> +/* Return 1 if adapter has the specified quirks, 0 if not. */
> +static inline int i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
> +{
> + return (quirks & (adap->quirks ? adap->quirks->flags : 0)) == quirks;
> +}

This is not a code obfuscation contest ;)

So maybe more like this:

static inline bool i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
{
if (!adap->quirks)
return false;
return (adap->quirks->flags & quirks) == quirks;
}

And please use kernel-doc for the documentation.
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v4 4/4] iio: humidity: si7020: added No Hold read mode

2015-10-28 Thread Nicola Corna
October 28 2015 10:38 AM, "Lars-Peter Clausen"  wrote:

> On 10/28/2015 07:58 AM, Nicola Corna wrote:
> [...]
> 
>> + holdmode = !((*client)->adapter->quirks &&
>> + (*client)->adapter->quirks->flags &
> 
> [...]
> 
>> + client->adapter->quirks &&
>> + client->adapter->quirks->flags & I2C_AQ_NO_CLK_STRETCH)
> 
> This is rather ugly, can we get a helper in the I2C core something along the
> lines of
> 
> i2c_check_quirks(client->adapter, I2C_AQ_NO_CLK_STRETCH)
> 
> - Lars

Something like this?

---
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index a69a9a0..a06ffc0 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -613,6 +613,12 @@ static inline int i2c_check_functionality(struct 
i2c_adapter *adap, u32 func)
return (func & i2c_get_functionality(adap)) == func;
}

+/* Return 1 if adapter has the specified quirks, 0 if not. */
+static inline int i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
+{
+ return (quirks & (adap->quirks ? adap->quirks->flags : 0)) == quirks;
+}
+
/* Return the adapter number for a specific adapter */
static inline int i2c_adapter_id(struct i2c_adapter *adap)
{
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v4 4/4] iio: humidity: si7020: added No Hold read mode

2015-10-28 Thread Lars-Peter Clausen
On 10/28/2015 07:58 AM, Nicola Corna wrote:
[...]
> + holdmode = !((*client)->adapter->quirks &&
> + (*client)->adapter->quirks->flags &
[...]
> +client->adapter->quirks &&
> +client->adapter->quirks->flags & I2C_AQ_NO_CLK_STRETCH)

This is rather ugly, can we get a helper in the I2C core something along the
lines of

i2c_check_quirks(client->adapter, I2C_AQ_NO_CLK_STRETCH)

- Lars

--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 4/4] iio: humidity: si7020: added No Hold read mode

2015-10-28 Thread Nicola Corna
The Si7013/20/21 modules support 2 read modes:
 * Hold mode (blocking), where the device stretches the clock until the end
of the measurement
 * No Hold mode (non-blocking), where the device replies NACK for every I2C
call during the measurement
Here the No Hold mode is implemented, selectable with the blocking_io
variable within si7020_platform_data. The default mode is Hold, unless the
adapter does not support clock stretching, in which case the No Hold mode
is used.

Signed-off-by: Nicola Corna 
---
This patch depends on patch "[PATCH v4 1/2] iio: humidity: si7020: replaced
bitmask on humidity values with range check"
 drivers/iio/humidity/si7020.c| 78 
 include/linux/platform_data/si7020.h | 21 ++
 2 files changed, 92 insertions(+), 7 deletions(-)
 create mode 100644 include/linux/platform_data/si7020.h

diff --git a/drivers/iio/humidity/si7020.c b/drivers/iio/humidity/si7020.c
index 12128d1..c728681 100644
--- a/drivers/iio/humidity/si7020.c
+++ b/drivers/iio/humidity/si7020.c
@@ -2,6 +2,7 @@
  * si7020.c - Silicon Labs Si7013/20/21 Relative Humidity and Temp Sensors
  * Copyright (c) 2013,2014  Uplogix, Inc.
  * David Barksdale 
+ * Copyright (c) 2015 Nicola Corna 
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU General Public License,
@@ -30,33 +31,79 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
+#include 
 
 /* Measure Relative Humidity, Hold Master Mode */
 #define SI7020CMD_RH_HOLD  0xE5
+/* Measure Relative Humidity, No Hold Master Mode */
+#define SI7020CMD_RH_NO_HOLD   0xF5
 /* Measure Temperature, Hold Master Mode */
 #define SI7020CMD_TEMP_HOLD0xE3
+/* Measure Temperature, No Hold Master Mode */
+#define SI7020CMD_TEMP_NO_HOLD 0xF3
 /* Software Reset */
 #define SI7020CMD_RESET0xFE
+/* Relative humidity measurement timeout (us) */
+#define SI7020_RH_TIMEOUT  22800
+/* Temperature measurement timeout (us) */
+#define SI7020_TEMP_TIMEOUT10800
+/* Minimum delay between retries (No Hold Mode) in us */
+#define SI7020_NOHOLD_SLEEP_MIN2000
+/* Maximum delay between retries (No Hold Mode) in us */
+#define SI7020_NOHOLD_SLEEP_MAX6000
 
 static int si7020_read_raw(struct iio_dev *indio_dev,
   struct iio_chan_spec const *chan, int *val,
   int *val2, long mask)
 {
struct i2c_client **client = iio_priv(indio_dev);
+   struct si7020_platform_data *pdata;
int ret;
+   bool holdmode;
+   unsigned char buf[2];
+   unsigned long start;
 
switch (mask) {
case IIO_CHAN_INFO_RAW:
-   ret = i2c_smbus_read_word_data(*client,
-  chan->type == IIO_TEMP ?
-  SI7020CMD_TEMP_HOLD :
-  SI7020CMD_RH_HOLD);
-   if (ret < 0)
-   return ret;
-   *val = ret >> 2;
+   pdata = dev_get_platdata(&(*client)->dev);
+   if (pdata)
+   holdmode = pdata->blocking_io;
+   else
+   holdmode = !((*client)->adapter->quirks &&
+   (*client)->adapter->quirks->flags &
+   I2C_AQ_NO_CLK_STRETCH);
+   if (holdmode) {
+   ret = i2c_smbus_read_word_data(*client,
+  chan->type == IIO_TEMP ?
+  SI7020CMD_TEMP_HOLD :
+  SI7020CMD_RH_HOLD);
+   if (ret < 0)
+   return ret;
+   *val = ret >> 2;
+   } else {
+   ret = i2c_smbus_write_byte(*client,
+  chan->type == IIO_TEMP ?
+  SI7020CMD_TEMP_NO_HOLD :
+  SI7020CMD_RH_NO_HOLD);
+   if (ret < 0)
+   return ret;
+   start = jiffies;
+   while ((ret = i2c_master_recv(*client, buf, 2)) < 0) {
+   if (time_after(jiffies, start +
+  usecs_to_jiffies(
+   chan->type == IIO_TEMP ?
+   SI7020_TEMP_TIMEOUT :
+   SI7020_RH_TIMEOUT)))
+   return ret;
+   usleep_range(SI7020_NOHOLD_SLEEP_MIN,
+SI7020_NOHOLD_SLEEP_MAX);
+   }
+