Re: [PATCH v2] iio : Add cm3218 smbus ara and acpi support

2017-10-26 Thread Peter Meerwald-Stadler

> On asus T100, Capella cm3218 chip is implemented as ambiant light
> sensor. This chip expose an smbus ARA protocol device on standard
> address 0x0c. The chip is not functional before all alerts are
> acknowledged.
> On asus T100, this device is enumerated on ACPI bus and the description
> give tow I2C connection. The first is the connection to the ARA device
> and the second gives the real address of the device.
> 
> So, on device probe,If the i2c address is the ARA address and the
> device is enumerated via acpi, we lookup for the real address in
> the ACPI resource list and change it in the client structure.
> if an interrupt resource is given, and only for cm3218 chip,
> we declare an smbus_alert device.

comments below; some are unrelated cleanups if you care...
 
> Signed-off-by: Marc CAPDEVILLE 
> ---
> v2 : 
>- cm3218 support always build
>- Cleanup of unneeded #if statement
>- Beter identifying chip in platform device, acpi and of_device
> 
>  drivers/iio/light/cm32181.c | 165 
> +++-
>  1 file changed, 162 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
> index d6fd0da..7bc97ff 100644
> --- a/drivers/iio/light/cm32181.c
> +++ b/drivers/iio/light/cm32181.c
> @@ -18,6 +18,9 @@
>  #include 
>  #include 
>  #include 
> +#include 
> +#include 
> +#include 
>  
>  /* Registers Address */

maybe better 'Register Addresses'

>  #define CM32181_REG_ADDR_CMD 0x00
> @@ -47,6 +50,11 @@
>  #define CM32181_CALIBSCALE_RESOLUTION1000
>  #define MLUX_PER_LUX 1000

CM32181_ prefix?

>  
> +#define CM32181_ID   0x81
> +#define CM3218_ID0x18
> +
> +#define CM3218_ARA_ADDR  0x0c
> +
>  static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
>   CM32181_REG_ADDR_CMD,
>  };
> @@ -57,6 +65,8 @@ static const int als_it_value[] = {25000, 5, 10, 
> 20, 40,

cm32181_ prefix?

>  
>  struct cm32181_chip {
>   struct i2c_client *client;
> + int chip_id;
> + struct i2c_client *ara;
>   struct mutex lock;
>   u16 conf_regs[CM32181_CONF_REG_NUM];
>   int calibscale;
> @@ -81,7 +91,7 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
>   return ret;
>  
>   /* check device ID */
> - if ((ret & 0xFF) != 0x81)
> + if ((ret & 0xFF) != cm32181->chip_id)
>   return -ENODEV;
>  
>   /* Default Values */
> @@ -298,12 +308,81 @@ static const struct iio_info cm32181_info = {
>   .attrs  = _attribute_group,
>  };
>  
> +#if defined(CONFIG_ACPI)
> +/* Parse acpi resources for a second i2c address on same adapter

could be a single line comment :)

the function does more than parse; I suggest to use strut i2c_client * as 
the type for the second argument

please describe that the i2c address is put into data

the return type could be bool, 1 vs -1 is unusual and not clear, -1 on 
success?

> + */
> +static int cm3218_get_i2c_address(struct acpi_resource *ares, void *data)
> +{
> + struct i2c_client *client = data;
> + struct acpi_resource_i2c_serialbus *sb;
> + acpi_status status;
> + acpi_handle adapter_handle;
> +
> + if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
> + return 1;
> +
> + sb = >data.i2c_serial_bus;
> + if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
> + return 1;
> +
> + status = acpi_get_handle(ACPI_HANDLE(>dev),
> +  sb->resource_source.string_ptr,
> +  _handle);
> + if (!ACPI_SUCCESS(status))
> + return 1;
> +
> + if (adapter_handle != ACPI_HANDLE(>adapter->dev))
> + return 1;
> +
> + if (sb->slave_address == CM3218_ARA_ADDR)
> + return 1;
> +
> + client->addr = sb->slave_address;
> + client->flags &= ~I2C_CLIENT_TEN;
> + if (sb->access_mode == ACPI_I2C_10BIT_MODE)
> + client->flags |= I2C_CLIENT_TEN;
> +
> + return -1;
> +}
> +
> +/* Walk through acpi resources to find second i2c connection

single line comment?

> + */
> +static int cm3218_acpi_get_address(struct i2c_client *client)
> +{
> + int ret;
> + struct acpi_device *adev;
> + LIST_HEAD(ares);
> +
> + adev = ACPI_COMPANION(>dev);
> + if (!adev)
> + return -ENODEV;
> +
> + ret = acpi_dev_get_resources(adev,
> +  ,
> +  cm3218_get_i2c_address,
> +  client);
> + acpi_dev_free_resource_list();
> + if (ret < 0)
> + return -ENODEV;
> +
> + return 0;
> +}
> +#else
> +static inline int cm3218_acpi_get_address(struct i2c_client *client)

why inline?

> +{
> + return -ENODEV;
> +}
> +#endif
> +
>  static int cm32181_probe(struct i2c_client *client,
>   const struct i2c_device_id 

Re: [PATCH v2] iio : Add cm3218 smbus ara and acpi support

2017-10-26 Thread Peter Meerwald-Stadler

> On asus T100, Capella cm3218 chip is implemented as ambiant light
> sensor. This chip expose an smbus ARA protocol device on standard
> address 0x0c. The chip is not functional before all alerts are
> acknowledged.
> On asus T100, this device is enumerated on ACPI bus and the description
> give tow I2C connection. The first is the connection to the ARA device
> and the second gives the real address of the device.
> 
> So, on device probe,If the i2c address is the ARA address and the
> device is enumerated via acpi, we lookup for the real address in
> the ACPI resource list and change it in the client structure.
> if an interrupt resource is given, and only for cm3218 chip,
> we declare an smbus_alert device.

comments below; some are unrelated cleanups if you care...
 
> Signed-off-by: Marc CAPDEVILLE 
> ---
> v2 : 
>- cm3218 support always build
>- Cleanup of unneeded #if statement
>- Beter identifying chip in platform device, acpi and of_device
> 
>  drivers/iio/light/cm32181.c | 165 
> +++-
>  1 file changed, 162 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
> index d6fd0da..7bc97ff 100644
> --- a/drivers/iio/light/cm32181.c
> +++ b/drivers/iio/light/cm32181.c
> @@ -18,6 +18,9 @@
>  #include 
>  #include 
>  #include 
> +#include 
> +#include 
> +#include 
>  
>  /* Registers Address */

maybe better 'Register Addresses'

>  #define CM32181_REG_ADDR_CMD 0x00
> @@ -47,6 +50,11 @@
>  #define CM32181_CALIBSCALE_RESOLUTION1000
>  #define MLUX_PER_LUX 1000

CM32181_ prefix?

>  
> +#define CM32181_ID   0x81
> +#define CM3218_ID0x18
> +
> +#define CM3218_ARA_ADDR  0x0c
> +
>  static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
>   CM32181_REG_ADDR_CMD,
>  };
> @@ -57,6 +65,8 @@ static const int als_it_value[] = {25000, 5, 10, 
> 20, 40,

cm32181_ prefix?

>  
>  struct cm32181_chip {
>   struct i2c_client *client;
> + int chip_id;
> + struct i2c_client *ara;
>   struct mutex lock;
>   u16 conf_regs[CM32181_CONF_REG_NUM];
>   int calibscale;
> @@ -81,7 +91,7 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
>   return ret;
>  
>   /* check device ID */
> - if ((ret & 0xFF) != 0x81)
> + if ((ret & 0xFF) != cm32181->chip_id)
>   return -ENODEV;
>  
>   /* Default Values */
> @@ -298,12 +308,81 @@ static const struct iio_info cm32181_info = {
>   .attrs  = _attribute_group,
>  };
>  
> +#if defined(CONFIG_ACPI)
> +/* Parse acpi resources for a second i2c address on same adapter

could be a single line comment :)

the function does more than parse; I suggest to use strut i2c_client * as 
the type for the second argument

please describe that the i2c address is put into data

the return type could be bool, 1 vs -1 is unusual and not clear, -1 on 
success?

> + */
> +static int cm3218_get_i2c_address(struct acpi_resource *ares, void *data)
> +{
> + struct i2c_client *client = data;
> + struct acpi_resource_i2c_serialbus *sb;
> + acpi_status status;
> + acpi_handle adapter_handle;
> +
> + if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
> + return 1;
> +
> + sb = >data.i2c_serial_bus;
> + if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
> + return 1;
> +
> + status = acpi_get_handle(ACPI_HANDLE(>dev),
> +  sb->resource_source.string_ptr,
> +  _handle);
> + if (!ACPI_SUCCESS(status))
> + return 1;
> +
> + if (adapter_handle != ACPI_HANDLE(>adapter->dev))
> + return 1;
> +
> + if (sb->slave_address == CM3218_ARA_ADDR)
> + return 1;
> +
> + client->addr = sb->slave_address;
> + client->flags &= ~I2C_CLIENT_TEN;
> + if (sb->access_mode == ACPI_I2C_10BIT_MODE)
> + client->flags |= I2C_CLIENT_TEN;
> +
> + return -1;
> +}
> +
> +/* Walk through acpi resources to find second i2c connection

single line comment?

> + */
> +static int cm3218_acpi_get_address(struct i2c_client *client)
> +{
> + int ret;
> + struct acpi_device *adev;
> + LIST_HEAD(ares);
> +
> + adev = ACPI_COMPANION(>dev);
> + if (!adev)
> + return -ENODEV;
> +
> + ret = acpi_dev_get_resources(adev,
> +  ,
> +  cm3218_get_i2c_address,
> +  client);
> + acpi_dev_free_resource_list();
> + if (ret < 0)
> + return -ENODEV;
> +
> + return 0;
> +}
> +#else
> +static inline int cm3218_acpi_get_address(struct i2c_client *client)

why inline?

> +{
> + return -ENODEV;
> +}
> +#endif
> +
>  static int cm32181_probe(struct i2c_client *client,
>   const struct i2c_device_id *id)
>  {
>   struct 

Re: [PATCH v2] iio : Add cm3218 smbus ara and acpi support

2017-10-26 Thread Jonathan Cameron
On Wed, 25 Oct 2017 16:55:15 +0200
Marc CAPDEVILLE  wrote:

> On asus T100, Capella cm3218 chip is implemented as ambiant light
> sensor. This chip expose an smbus ARA protocol device on standard
> address 0x0c. The chip is not functional before all alerts are
> acknowledged.
> On asus T100, this device is enumerated on ACPI bus and the description
> give tow I2C connection. The first is the connection to the ARA device
> and the second gives the real address of the device.
> 
> So, on device probe,If the i2c address is the ARA address and the
> device is enumerated via acpi, we lookup for the real address in
> the ACPI resource list and change it in the client structure.
> if an interrupt resource is given, and only for cm3218 chip,
> we declare an smbus_alert device.
> 
> Signed-off-by: Marc CAPDEVILLE 
Hi Marc,

The ACPI resource filtering is not being used as that infrastructure
is intended.  What you have here may be shorter, but it is non
standard so both fragile and harder to review.

I will also be needed an ack from Kevin and please cc wolfram as
the smbus usage is relatively unusual.

We have probably missed the upcoming merge window now (unless it
is delayed for some reason).  I am travelling for the next
two weeks so any response may be delayed (or quicker depending
on how much time I spend using airport wifi :)

Jonathan

> ---
> v2 : 
>- cm3218 support always build
>- Cleanup of unneeded #if statement
>- Beter identifying chip in platform device, acpi and of_device
> 
>  drivers/iio/light/cm32181.c | 165 
> +++-
>  1 file changed, 162 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
> index d6fd0da..7bc97ff 100644
> --- a/drivers/iio/light/cm32181.c
> +++ b/drivers/iio/light/cm32181.c
> @@ -18,6 +18,9 @@
>  #include 
>  #include 
>  #include 
> +#include 
> +#include 
> +#include 
>  
>  /* Registers Address */
>  #define CM32181_REG_ADDR_CMD 0x00
> @@ -47,6 +50,11 @@
>  #define CM32181_CALIBSCALE_RESOLUTION1000
>  #define MLUX_PER_LUX 1000
>  
> +#define CM32181_ID   0x81
> +#define CM3218_ID0x18
> +
> +#define CM3218_ARA_ADDR  0x0c
> +
>  static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
>   CM32181_REG_ADDR_CMD,
>  };
> @@ -57,6 +65,8 @@ static const int als_it_value[] = {25000, 5, 10, 
> 20, 40,
>  
>  struct cm32181_chip {
>   struct i2c_client *client;
> + int chip_id;
> + struct i2c_client *ara;
>   struct mutex lock;
>   u16 conf_regs[CM32181_CONF_REG_NUM];
>   int calibscale;
> @@ -81,7 +91,7 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
>   return ret;
>  
>   /* check device ID */
> - if ((ret & 0xFF) != 0x81)
> + if ((ret & 0xFF) != cm32181->chip_id)
>   return -ENODEV;
>  
>   /* Default Values */
> @@ -298,12 +308,81 @@ static const struct iio_info cm32181_info = {
>   .attrs  = _attribute_group,
>  };
>  
> +#if defined(CONFIG_ACPI)
> +/* Parse acpi resources for a second i2c address on same adapter
> + */
Please use kernel style comment syntax with correct single and multiple
line comments.

> +static int cm3218_get_i2c_address(struct acpi_resource *ares, void *data)

Read 
http://elixir.free-electrons.com/linux/latest/source/drivers/acpi/resource.c#L600
help text and make this function do the actions described rrather than what it
is doing currently.

> +{
> + struct i2c_client *client = data;
> + struct acpi_resource_i2c_serialbus *sb;
> + acpi_status status;
> + acpi_handle adapter_handle;
> +
> + if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
> + return 1;
> +
> + sb = >data.i2c_serial_bus;
> + if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
> + return 1;
> +
> + status = acpi_get_handle(ACPI_HANDLE(>dev),
> +  sb->resource_source.string_ptr,
> +  _handle);
> + if (!ACPI_SUCCESS(status))
> + return 1;
> +
> + if (adapter_handle != ACPI_HANDLE(>adapter->dev))
> + return 1;
> +
> + if (sb->slave_address == CM3218_ARA_ADDR)
> + return 1;
> +
> + client->addr = sb->slave_address;
> + client->flags &= ~I2C_CLIENT_TEN;
> + if (sb->access_mode == ACPI_I2C_10BIT_MODE)
> + client->flags |= I2C_CLIENT_TEN;
> +
> + return -1;
> +}
> +
> +/* Walk through acpi resources to find second i2c connection
> + */
Single line comment syntax please.
> +static int cm3218_acpi_get_address(struct i2c_client *client)
> +{
> + int ret;
> + struct acpi_device *adev;
> + LIST_HEAD(ares);
> +
> + adev = ACPI_COMPANION(>dev);
> + if (!adev)
> + return -ENODEV;
> +
> + ret = acpi_dev_get_resources(adev,
> +  

Re: [PATCH v2] iio : Add cm3218 smbus ara and acpi support

2017-10-26 Thread Jonathan Cameron
On Wed, 25 Oct 2017 16:55:15 +0200
Marc CAPDEVILLE  wrote:

> On asus T100, Capella cm3218 chip is implemented as ambiant light
> sensor. This chip expose an smbus ARA protocol device on standard
> address 0x0c. The chip is not functional before all alerts are
> acknowledged.
> On asus T100, this device is enumerated on ACPI bus and the description
> give tow I2C connection. The first is the connection to the ARA device
> and the second gives the real address of the device.
> 
> So, on device probe,If the i2c address is the ARA address and the
> device is enumerated via acpi, we lookup for the real address in
> the ACPI resource list and change it in the client structure.
> if an interrupt resource is given, and only for cm3218 chip,
> we declare an smbus_alert device.
> 
> Signed-off-by: Marc CAPDEVILLE 
Hi Marc,

The ACPI resource filtering is not being used as that infrastructure
is intended.  What you have here may be shorter, but it is non
standard so both fragile and harder to review.

I will also be needed an ack from Kevin and please cc wolfram as
the smbus usage is relatively unusual.

We have probably missed the upcoming merge window now (unless it
is delayed for some reason).  I am travelling for the next
two weeks so any response may be delayed (or quicker depending
on how much time I spend using airport wifi :)

Jonathan

> ---
> v2 : 
>- cm3218 support always build
>- Cleanup of unneeded #if statement
>- Beter identifying chip in platform device, acpi and of_device
> 
>  drivers/iio/light/cm32181.c | 165 
> +++-
>  1 file changed, 162 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
> index d6fd0da..7bc97ff 100644
> --- a/drivers/iio/light/cm32181.c
> +++ b/drivers/iio/light/cm32181.c
> @@ -18,6 +18,9 @@
>  #include 
>  #include 
>  #include 
> +#include 
> +#include 
> +#include 
>  
>  /* Registers Address */
>  #define CM32181_REG_ADDR_CMD 0x00
> @@ -47,6 +50,11 @@
>  #define CM32181_CALIBSCALE_RESOLUTION1000
>  #define MLUX_PER_LUX 1000
>  
> +#define CM32181_ID   0x81
> +#define CM3218_ID0x18
> +
> +#define CM3218_ARA_ADDR  0x0c
> +
>  static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
>   CM32181_REG_ADDR_CMD,
>  };
> @@ -57,6 +65,8 @@ static const int als_it_value[] = {25000, 5, 10, 
> 20, 40,
>  
>  struct cm32181_chip {
>   struct i2c_client *client;
> + int chip_id;
> + struct i2c_client *ara;
>   struct mutex lock;
>   u16 conf_regs[CM32181_CONF_REG_NUM];
>   int calibscale;
> @@ -81,7 +91,7 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
>   return ret;
>  
>   /* check device ID */
> - if ((ret & 0xFF) != 0x81)
> + if ((ret & 0xFF) != cm32181->chip_id)
>   return -ENODEV;
>  
>   /* Default Values */
> @@ -298,12 +308,81 @@ static const struct iio_info cm32181_info = {
>   .attrs  = _attribute_group,
>  };
>  
> +#if defined(CONFIG_ACPI)
> +/* Parse acpi resources for a second i2c address on same adapter
> + */
Please use kernel style comment syntax with correct single and multiple
line comments.

> +static int cm3218_get_i2c_address(struct acpi_resource *ares, void *data)

Read 
http://elixir.free-electrons.com/linux/latest/source/drivers/acpi/resource.c#L600
help text and make this function do the actions described rrather than what it
is doing currently.

> +{
> + struct i2c_client *client = data;
> + struct acpi_resource_i2c_serialbus *sb;
> + acpi_status status;
> + acpi_handle adapter_handle;
> +
> + if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
> + return 1;
> +
> + sb = >data.i2c_serial_bus;
> + if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
> + return 1;
> +
> + status = acpi_get_handle(ACPI_HANDLE(>dev),
> +  sb->resource_source.string_ptr,
> +  _handle);
> + if (!ACPI_SUCCESS(status))
> + return 1;
> +
> + if (adapter_handle != ACPI_HANDLE(>adapter->dev))
> + return 1;
> +
> + if (sb->slave_address == CM3218_ARA_ADDR)
> + return 1;
> +
> + client->addr = sb->slave_address;
> + client->flags &= ~I2C_CLIENT_TEN;
> + if (sb->access_mode == ACPI_I2C_10BIT_MODE)
> + client->flags |= I2C_CLIENT_TEN;
> +
> + return -1;
> +}
> +
> +/* Walk through acpi resources to find second i2c connection
> + */
Single line comment syntax please.
> +static int cm3218_acpi_get_address(struct i2c_client *client)
> +{
> + int ret;
> + struct acpi_device *adev;
> + LIST_HEAD(ares);
> +
> + adev = ACPI_COMPANION(>dev);
> + if (!adev)
> + return -ENODEV;
> +
> + ret = acpi_dev_get_resources(adev,
> +  ,
> + 

[PATCH v2] iio : Add cm3218 smbus ara and acpi support

2017-10-25 Thread Marc CAPDEVILLE
On asus T100, Capella cm3218 chip is implemented as ambiant light
sensor. This chip expose an smbus ARA protocol device on standard
address 0x0c. The chip is not functional before all alerts are
acknowledged.
On asus T100, this device is enumerated on ACPI bus and the description
give tow I2C connection. The first is the connection to the ARA device
and the second gives the real address of the device.

So, on device probe,If the i2c address is the ARA address and the
device is enumerated via acpi, we lookup for the real address in
the ACPI resource list and change it in the client structure.
if an interrupt resource is given, and only for cm3218 chip,
we declare an smbus_alert device.

Signed-off-by: Marc CAPDEVILLE 
---
v2 : 
   - cm3218 support always build
   - Cleanup of unneeded #if statement
   - Beter identifying chip in platform device, acpi and of_device

 drivers/iio/light/cm32181.c | 165 +++-
 1 file changed, 162 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index d6fd0da..7bc97ff 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -18,6 +18,9 @@
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
 
 /* Registers Address */
 #define CM32181_REG_ADDR_CMD   0x00
@@ -47,6 +50,11 @@
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define MLUX_PER_LUX   1000
 
+#define CM32181_ID 0x81
+#define CM3218_ID  0x18
+
+#define CM3218_ARA_ADDR0x0c
+
 static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
CM32181_REG_ADDR_CMD,
 };
@@ -57,6 +65,8 @@ static const int als_it_value[] = {25000, 5, 10, 
20, 40,
 
 struct cm32181_chip {
struct i2c_client *client;
+   int chip_id;
+   struct i2c_client *ara;
struct mutex lock;
u16 conf_regs[CM32181_CONF_REG_NUM];
int calibscale;
@@ -81,7 +91,7 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
return ret;
 
/* check device ID */
-   if ((ret & 0xFF) != 0x81)
+   if ((ret & 0xFF) != cm32181->chip_id)
return -ENODEV;
 
/* Default Values */
@@ -298,12 +308,81 @@ static const struct iio_info cm32181_info = {
.attrs  = _attribute_group,
 };
 
+#if defined(CONFIG_ACPI)
+/* Parse acpi resources for a second i2c address on same adapter
+ */
+static int cm3218_get_i2c_address(struct acpi_resource *ares, void *data)
+{
+   struct i2c_client *client = data;
+   struct acpi_resource_i2c_serialbus *sb;
+   acpi_status status;
+   acpi_handle adapter_handle;
+
+   if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
+   return 1;
+
+   sb = >data.i2c_serial_bus;
+   if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
+   return 1;
+
+   status = acpi_get_handle(ACPI_HANDLE(>dev),
+sb->resource_source.string_ptr,
+_handle);
+   if (!ACPI_SUCCESS(status))
+   return 1;
+
+   if (adapter_handle != ACPI_HANDLE(>adapter->dev))
+   return 1;
+
+   if (sb->slave_address == CM3218_ARA_ADDR)
+   return 1;
+
+   client->addr = sb->slave_address;
+   client->flags &= ~I2C_CLIENT_TEN;
+   if (sb->access_mode == ACPI_I2C_10BIT_MODE)
+   client->flags |= I2C_CLIENT_TEN;
+
+   return -1;
+}
+
+/* Walk through acpi resources to find second i2c connection
+ */
+static int cm3218_acpi_get_address(struct i2c_client *client)
+{
+   int ret;
+   struct acpi_device *adev;
+   LIST_HEAD(ares);
+
+   adev = ACPI_COMPANION(>dev);
+   if (!adev)
+   return -ENODEV;
+
+   ret = acpi_dev_get_resources(adev,
+,
+cm3218_get_i2c_address,
+client);
+   acpi_dev_free_resource_list();
+   if (ret < 0)
+   return -ENODEV;
+
+   return 0;
+}
+#else
+static inline int cm3218_acpi_get_address(struct i2c_client *client)
+{
+   return -ENODEV;
+}
+#endif
+
 static int cm32181_probe(struct i2c_client *client,
const struct i2c_device_id *id)
 {
struct cm32181_chip *cm32181;
struct iio_dev *indio_dev;
int ret;
+   struct i2c_smbus_alert_setup ara_setup;
+   const struct of_device_id *of_id;
+   const struct acpi_device_id *acpi_id;
 
indio_dev = devm_iio_device_alloc(>dev, sizeof(*cm32181));
if (!indio_dev) {
@@ -323,11 +402,65 @@ static int cm32181_probe(struct i2c_client *client,
indio_dev->name = id->name;
indio_dev->modes = INDIO_DIRECT_MODE;
 
+   /* Lookup for chip ID from platform, acpi or of device table */
+   if (id) {
+   cm32181->chip_id = id->driver_data;

[PATCH v2] iio : Add cm3218 smbus ara and acpi support

2017-10-25 Thread Marc CAPDEVILLE
On asus T100, Capella cm3218 chip is implemented as ambiant light
sensor. This chip expose an smbus ARA protocol device on standard
address 0x0c. The chip is not functional before all alerts are
acknowledged.
On asus T100, this device is enumerated on ACPI bus and the description
give tow I2C connection. The first is the connection to the ARA device
and the second gives the real address of the device.

So, on device probe,If the i2c address is the ARA address and the
device is enumerated via acpi, we lookup for the real address in
the ACPI resource list and change it in the client structure.
if an interrupt resource is given, and only for cm3218 chip,
we declare an smbus_alert device.

Signed-off-by: Marc CAPDEVILLE 
---
v2 : 
   - cm3218 support always build
   - Cleanup of unneeded #if statement
   - Beter identifying chip in platform device, acpi and of_device

 drivers/iio/light/cm32181.c | 165 +++-
 1 file changed, 162 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index d6fd0da..7bc97ff 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -18,6 +18,9 @@
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
 
 /* Registers Address */
 #define CM32181_REG_ADDR_CMD   0x00
@@ -47,6 +50,11 @@
 #define CM32181_CALIBSCALE_RESOLUTION  1000
 #define MLUX_PER_LUX   1000
 
+#define CM32181_ID 0x81
+#define CM3218_ID  0x18
+
+#define CM3218_ARA_ADDR0x0c
+
 static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
CM32181_REG_ADDR_CMD,
 };
@@ -57,6 +65,8 @@ static const int als_it_value[] = {25000, 5, 10, 
20, 40,
 
 struct cm32181_chip {
struct i2c_client *client;
+   int chip_id;
+   struct i2c_client *ara;
struct mutex lock;
u16 conf_regs[CM32181_CONF_REG_NUM];
int calibscale;
@@ -81,7 +91,7 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181)
return ret;
 
/* check device ID */
-   if ((ret & 0xFF) != 0x81)
+   if ((ret & 0xFF) != cm32181->chip_id)
return -ENODEV;
 
/* Default Values */
@@ -298,12 +308,81 @@ static const struct iio_info cm32181_info = {
.attrs  = _attribute_group,
 };
 
+#if defined(CONFIG_ACPI)
+/* Parse acpi resources for a second i2c address on same adapter
+ */
+static int cm3218_get_i2c_address(struct acpi_resource *ares, void *data)
+{
+   struct i2c_client *client = data;
+   struct acpi_resource_i2c_serialbus *sb;
+   acpi_status status;
+   acpi_handle adapter_handle;
+
+   if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
+   return 1;
+
+   sb = >data.i2c_serial_bus;
+   if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
+   return 1;
+
+   status = acpi_get_handle(ACPI_HANDLE(>dev),
+sb->resource_source.string_ptr,
+_handle);
+   if (!ACPI_SUCCESS(status))
+   return 1;
+
+   if (adapter_handle != ACPI_HANDLE(>adapter->dev))
+   return 1;
+
+   if (sb->slave_address == CM3218_ARA_ADDR)
+   return 1;
+
+   client->addr = sb->slave_address;
+   client->flags &= ~I2C_CLIENT_TEN;
+   if (sb->access_mode == ACPI_I2C_10BIT_MODE)
+   client->flags |= I2C_CLIENT_TEN;
+
+   return -1;
+}
+
+/* Walk through acpi resources to find second i2c connection
+ */
+static int cm3218_acpi_get_address(struct i2c_client *client)
+{
+   int ret;
+   struct acpi_device *adev;
+   LIST_HEAD(ares);
+
+   adev = ACPI_COMPANION(>dev);
+   if (!adev)
+   return -ENODEV;
+
+   ret = acpi_dev_get_resources(adev,
+,
+cm3218_get_i2c_address,
+client);
+   acpi_dev_free_resource_list();
+   if (ret < 0)
+   return -ENODEV;
+
+   return 0;
+}
+#else
+static inline int cm3218_acpi_get_address(struct i2c_client *client)
+{
+   return -ENODEV;
+}
+#endif
+
 static int cm32181_probe(struct i2c_client *client,
const struct i2c_device_id *id)
 {
struct cm32181_chip *cm32181;
struct iio_dev *indio_dev;
int ret;
+   struct i2c_smbus_alert_setup ara_setup;
+   const struct of_device_id *of_id;
+   const struct acpi_device_id *acpi_id;
 
indio_dev = devm_iio_device_alloc(>dev, sizeof(*cm32181));
if (!indio_dev) {
@@ -323,11 +402,65 @@ static int cm32181_probe(struct i2c_client *client,
indio_dev->name = id->name;
indio_dev->modes = INDIO_DIRECT_MODE;
 
+   /* Lookup for chip ID from platform, acpi or of device table */
+   if (id) {
+   cm32181->chip_id = id->driver_data;
+   } else if