* [PATCH v6 0/2] iio: sx9310: Support ACPI properties
@ 2021-03-22 7:32 Gwendal Grignou
2021-03-22 7:32 ` [PATCH v6 1/2] iio: sx9310: Fix access to variable DT array Gwendal Grignou
2021-03-22 7:32 ` [PATCH v6 2/2] iio: sx9310: Support ACPI properties Gwendal Grignou
0 siblings, 2 replies; 8+ messages in thread
From: Gwendal Grignou @ 2021-03-22 7:32 UTC (permalink / raw)
To: jic23, lars, swboyd, campello, andy.shevchenko; +Cc: linux-iio, Gwendal Grignou
Current sx9310 driver only support device tree properties.
Add support to read ACPI properties as well by converting calls
of_property_read_uXX() to device_property_read_uXX().
A bug was uncovered: if "semtech,combined-sensors" array was less than
4 entries, its content would be ignored, as of_property_read_u32_array
would return -EOVERFLOW.
Gwendal Grignou (2):
iio: sx9310: Fix access to variable DT array
iio: sx9310: Support ACPI properties
drivers/iio/proximity/sx9310.c | 56 +++++++++++++++++++++-------------
1 file changed, 34 insertions(+), 22 deletions(-)
--
2.31.0.291.g576ba9dcdaf-goog
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v6 1/2] iio: sx9310: Fix access to variable DT array
2021-03-22 7:32 [PATCH v6 0/2] iio: sx9310: Support ACPI properties Gwendal Grignou
@ 2021-03-22 7:32 ` Gwendal Grignou
2021-03-22 11:16 ` Alexandru Ardelean
2021-03-22 7:32 ` [PATCH v6 2/2] iio: sx9310: Support ACPI properties Gwendal Grignou
1 sibling, 1 reply; 8+ messages in thread
From: Gwendal Grignou @ 2021-03-22 7:32 UTC (permalink / raw)
To: jic23, lars, swboyd, campello, andy.shevchenko; +Cc: linux-iio, Gwendal Grignou
With the current code, we want to read 4 entries from DT array
"semtech,combined-sensors". If there are less, we silently fail as
of_property_read_u32_array() returns -EOVERFLOW.
First count the number of entries and if between 1 and 4, collect the
content of the array.
Fixes: 5b19ca2c78a0 ("iio: sx9310: Set various settings from DT")
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
Changes in v6:
Fix error in of_property_count_elems_of_size() argumnent:
Used ARRAY_SIZE(combined) [4] instead of sizeof(u32).
Changes in v5:
new, split fixes from changes needed for ACPI support.
drivers/iio/proximity/sx9310.c | 40 ++++++++++++++++++++++++----------
1 file changed, 28 insertions(+), 12 deletions(-)
diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 394c2afe0f233..289c76bb3b024 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -1213,17 +1213,17 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
}
static const struct sx9310_reg_default *
-sx9310_get_default_reg(struct sx9310_data *data, int i,
+sx9310_get_default_reg(struct sx9310_data *data, int idx,
struct sx9310_reg_default *reg_def)
{
- int ret;
const struct device_node *np = data->client->dev.of_node;
- u32 combined[SX9310_NUM_CHANNELS] = { 4, 4, 4, 4 };
+ u32 combined[SX9310_NUM_CHANNELS];
+ u32 start = 0, raw = 0, pos = 0;
unsigned long comb_mask = 0;
+ int ret, i, count;
const char *res;
- u32 start = 0, raw = 0, pos = 0;
- memcpy(reg_def, &sx9310_default_regs[i], sizeof(*reg_def));
+ memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def));
if (!np)
return reg_def;
@@ -1234,15 +1234,31 @@ sx9310_get_default_reg(struct sx9310_data *data, int i,
reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
}
- reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
- of_property_read_u32_array(np, "semtech,combined-sensors",
- combined, ARRAY_SIZE(combined));
- for (i = 0; i < ARRAY_SIZE(combined); i++) {
- if (combined[i] <= SX9310_NUM_CHANNELS)
- comb_mask |= BIT(combined[i]);
+ count = of_property_count_elems_of_size(np, "semtech,combined-sensors",
+ sizeof(u32));
+ if (count > 0 && count <= ARRAY_SIZE(combined)) {
+ ret = of_property_read_u32_array(np, "semtech,combined-sensors",
+ combined, count);
+ if (ret)
+ break;
+ } else {
+ /*
+ * Either the property does not exist in the DT or the
+ * number of entries is incorrect.
+ */
+ break;
+ }
+ for (i = 0; i < count; i++) {
+ if (combined[i] >= SX9310_NUM_CHANNELS) {
+ /* Invalid sensor (invalid DT). */
+ break;
+ }
+ comb_mask |= BIT(combined[i]);
}
+ if (i < count)
+ break;
- comb_mask &= 0xf;
+ reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
if (comb_mask == (BIT(3) | BIT(2) | BIT(1) | BIT(0)))
reg_def->def |= SX9310_REG_PROX_CTRL2_COMBMODE_CS0_CS1_CS2_CS3;
else if (comb_mask == (BIT(1) | BIT(2)))
--
2.31.0.291.g576ba9dcdaf-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v6 2/2] iio: sx9310: Support ACPI properties
2021-03-22 7:32 [PATCH v6 0/2] iio: sx9310: Support ACPI properties Gwendal Grignou
2021-03-22 7:32 ` [PATCH v6 1/2] iio: sx9310: Fix access to variable DT array Gwendal Grignou
@ 2021-03-22 7:32 ` Gwendal Grignou
2021-03-22 9:53 ` Alexandru Ardelean
1 sibling, 1 reply; 8+ messages in thread
From: Gwendal Grignou @ 2021-03-22 7:32 UTC (permalink / raw)
To: jic23, lars, swboyd, campello, andy.shevchenko; +Cc: linux-iio, Gwendal Grignou
Use device_property_read_uXX to support both device tree and ACPI
bindings when reading the properties we need to configure the SAR
sensor.
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
---
Changes in v6:
Use proper function to gather the number of elements in an array.
Changes in v5:
Split in 2 patches, one for fixing access to propery array, the other
to support ACPI.
drivers/iio/proximity/sx9310.c | 26 +++++++++++---------------
1 file changed, 11 insertions(+), 15 deletions(-)
diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 289c76bb3b024..3817a5714aab3 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -20,6 +20,7 @@
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/pm.h>
+#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
@@ -1213,10 +1214,9 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
}
static const struct sx9310_reg_default *
-sx9310_get_default_reg(struct sx9310_data *data, int idx,
+sx9310_get_default_reg(struct device *dev, int idx,
struct sx9310_reg_default *reg_def)
{
- const struct device_node *np = data->client->dev.of_node;
u32 combined[SX9310_NUM_CHANNELS];
u32 start = 0, raw = 0, pos = 0;
unsigned long comb_mask = 0;
@@ -1224,21 +1224,17 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
const char *res;
memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def));
- if (!np)
- return reg_def;
-
switch (reg_def->reg) {
case SX9310_REG_PROX_CTRL2:
- if (of_property_read_bool(np, "semtech,cs0-ground")) {
+ if (device_property_read_bool(dev, "semtech,cs0-ground")) {
reg_def->def &= ~SX9310_REG_PROX_CTRL2_SHIELDEN_MASK;
reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
}
- count = of_property_count_elems_of_size(np, "semtech,combined-sensors",
- sizeof(u32));
+ count = device_property_count_u32(dev, "semtech,combined-sensors");
if (count > 0 && count <= ARRAY_SIZE(combined)) {
- ret = of_property_read_u32_array(np, "semtech,combined-sensors",
- combined, count);
+ ret = device_property_read_u32_array(dev, "semtech,combined-sensors",
+ combined, count);
if (ret)
break;
} else {
@@ -1270,7 +1266,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
break;
case SX9310_REG_PROX_CTRL4:
- ret = of_property_read_string(np, "semtech,resolution", &res);
+ ret = device_property_read_string(dev, "semtech,resolution", &res);
if (ret)
break;
@@ -1294,7 +1290,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
break;
case SX9310_REG_PROX_CTRL5:
- ret = of_property_read_u32(np, "semtech,startup-sensor", &start);
+ ret = device_property_read_u32(dev, "semtech,startup-sensor", &start);
if (ret) {
start = FIELD_GET(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
reg_def->def);
@@ -1304,7 +1300,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
reg_def->def |= FIELD_PREP(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
start);
- ret = of_property_read_u32(np, "semtech,proxraw-strength", &raw);
+ ret = device_property_read_u32(dev, "semtech,proxraw-strength", &raw);
if (ret) {
raw = FIELD_GET(SX9310_REG_PROX_CTRL5_RAWFILT_MASK,
reg_def->def);
@@ -1317,7 +1313,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
raw);
break;
case SX9310_REG_PROX_CTRL7:
- ret = of_property_read_u32(np, "semtech,avg-pos-strength", &pos);
+ ret = device_property_read_u32(dev, "semtech,avg-pos-strength", &pos);
if (ret)
break;
@@ -1353,7 +1349,7 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
/* Program some sane defaults. */
for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
- initval = sx9310_get_default_reg(data, i, &tmp);
+ initval = sx9310_get_default_reg(&indio_dev->dev, i, &tmp);
ret = regmap_write(data->regmap, initval->reg, initval->def);
if (ret)
return ret;
--
2.31.0.291.g576ba9dcdaf-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v6 2/2] iio: sx9310: Support ACPI properties
2021-03-22 7:32 ` [PATCH v6 2/2] iio: sx9310: Support ACPI properties Gwendal Grignou
@ 2021-03-22 9:53 ` Alexandru Ardelean
2021-03-26 18:27 ` Gwendal Grignou
0 siblings, 1 reply; 8+ messages in thread
From: Alexandru Ardelean @ 2021-03-22 9:53 UTC (permalink / raw)
To: Gwendal Grignou
Cc: Jonathan Cameron, Lars-Peter Clausen, swboyd, campello,
Andy Shevchenko, linux-iio
On Mon, Mar 22, 2021 at 9:33 AM Gwendal Grignou <gwendal@chromium.org> wrote:
>
> Use device_property_read_uXX to support both device tree and ACPI
> bindings when reading the properties we need to configure the SAR
> sensor.
>
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> ---
> Changes in v6:
> Use proper function to gather the number of elements in an array.
>
> Changes in v5:
> Split in 2 patches, one for fixing access to propery array, the other
> to support ACPI.
>
> drivers/iio/proximity/sx9310.c | 26 +++++++++++---------------
> 1 file changed, 11 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 289c76bb3b024..3817a5714aab3 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -20,6 +20,7 @@
> #include <linux/mod_devicetable.h>
> #include <linux/module.h>
> #include <linux/pm.h>
> +#include <linux/property.h>
> #include <linux/regmap.h>
> #include <linux/regulator/consumer.h>
> #include <linux/slab.h>
> @@ -1213,10 +1214,9 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> }
>
> static const struct sx9310_reg_default *
> -sx9310_get_default_reg(struct sx9310_data *data, int idx,
> +sx9310_get_default_reg(struct device *dev, int idx,
> struct sx9310_reg_default *reg_def)
> {
> - const struct device_node *np = data->client->dev.of_node;
There may be an issue with this.
'np' was coming from the parent device (which is an i2c device)
So, 'data->client->dev' should be the same pointer as 'indio_dev->dev.parent'
Now, it's a reference to 'indio_dev->dev', so it's probably going to
miss a lot of DT properties.
Maybe an alternative would be to do:
const struct device *dev = &data->client->dev; [1]
Or instead of pass '&data->client->dev' or 'indio_dev->dev.parent'
when calling sx9310_get_default_reg()
Though [1] looks good to me as well.
> u32 combined[SX9310_NUM_CHANNELS];
> u32 start = 0, raw = 0, pos = 0;
> unsigned long comb_mask = 0;
> @@ -1224,21 +1224,17 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
> const char *res;
>
> memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def));
> - if (!np)
> - return reg_def;
> -
> switch (reg_def->reg) {
> case SX9310_REG_PROX_CTRL2:
> - if (of_property_read_bool(np, "semtech,cs0-ground")) {
> + if (device_property_read_bool(dev, "semtech,cs0-ground")) {
> reg_def->def &= ~SX9310_REG_PROX_CTRL2_SHIELDEN_MASK;
> reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
> }
>
> - count = of_property_count_elems_of_size(np, "semtech,combined-sensors",
> - sizeof(u32));
> + count = device_property_count_u32(dev, "semtech,combined-sensors");
> if (count > 0 && count <= ARRAY_SIZE(combined)) {
> - ret = of_property_read_u32_array(np, "semtech,combined-sensors",
> - combined, count);
> + ret = device_property_read_u32_array(dev, "semtech,combined-sensors",
> + combined, count);
> if (ret)
> break;
> } else {
> @@ -1270,7 +1266,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
>
> break;
> case SX9310_REG_PROX_CTRL4:
> - ret = of_property_read_string(np, "semtech,resolution", &res);
> + ret = device_property_read_string(dev, "semtech,resolution", &res);
> if (ret)
> break;
>
> @@ -1294,7 +1290,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
>
> break;
> case SX9310_REG_PROX_CTRL5:
> - ret = of_property_read_u32(np, "semtech,startup-sensor", &start);
> + ret = device_property_read_u32(dev, "semtech,startup-sensor", &start);
> if (ret) {
> start = FIELD_GET(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
> reg_def->def);
> @@ -1304,7 +1300,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
> reg_def->def |= FIELD_PREP(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
> start);
>
> - ret = of_property_read_u32(np, "semtech,proxraw-strength", &raw);
> + ret = device_property_read_u32(dev, "semtech,proxraw-strength", &raw);
> if (ret) {
> raw = FIELD_GET(SX9310_REG_PROX_CTRL5_RAWFILT_MASK,
> reg_def->def);
> @@ -1317,7 +1313,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
> raw);
> break;
> case SX9310_REG_PROX_CTRL7:
> - ret = of_property_read_u32(np, "semtech,avg-pos-strength", &pos);
> + ret = device_property_read_u32(dev, "semtech,avg-pos-strength", &pos);
> if (ret)
> break;
>
> @@ -1353,7 +1349,7 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
>
> /* Program some sane defaults. */
> for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
> - initval = sx9310_get_default_reg(data, i, &tmp);
> + initval = sx9310_get_default_reg(&indio_dev->dev, i, &tmp);
> ret = regmap_write(data->regmap, initval->reg, initval->def);
> if (ret)
> return ret;
> --
> 2.31.0.291.g576ba9dcdaf-goog
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v6 1/2] iio: sx9310: Fix access to variable DT array
2021-03-22 7:32 ` [PATCH v6 1/2] iio: sx9310: Fix access to variable DT array Gwendal Grignou
@ 2021-03-22 11:16 ` Alexandru Ardelean
2021-03-22 13:28 ` Andy Shevchenko
0 siblings, 1 reply; 8+ messages in thread
From: Alexandru Ardelean @ 2021-03-22 11:16 UTC (permalink / raw)
To: Gwendal Grignou
Cc: Jonathan Cameron, Lars-Peter Clausen, swboyd, campello,
Andy Shevchenko, linux-iio
On Mon, Mar 22, 2021 at 9:33 AM Gwendal Grignou <gwendal@chromium.org> wrote:
>
> With the current code, we want to read 4 entries from DT array
> "semtech,combined-sensors". If there are less, we silently fail as
> of_property_read_u32_array() returns -EOVERFLOW.
>
> First count the number of entries and if between 1 and 4, collect the
> content of the array.
>
> Fixes: 5b19ca2c78a0 ("iio: sx9310: Set various settings from DT")
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> ---
> Changes in v6:
> Fix error in of_property_count_elems_of_size() argumnent:
> Used ARRAY_SIZE(combined) [4] instead of sizeof(u32).
>
> Changes in v5:
> new, split fixes from changes needed for ACPI support.
>
> drivers/iio/proximity/sx9310.c | 40 ++++++++++++++++++++++++----------
> 1 file changed, 28 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 394c2afe0f233..289c76bb3b024 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -1213,17 +1213,17 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> }
>
> static const struct sx9310_reg_default *
> -sx9310_get_default_reg(struct sx9310_data *data, int i,
> +sx9310_get_default_reg(struct sx9310_data *data, int idx,
> struct sx9310_reg_default *reg_def)
> {
> - int ret;
> const struct device_node *np = data->client->dev.of_node;
> - u32 combined[SX9310_NUM_CHANNELS] = { 4, 4, 4, 4 };
> + u32 combined[SX9310_NUM_CHANNELS];
> + u32 start = 0, raw = 0, pos = 0;
> unsigned long comb_mask = 0;
> + int ret, i, count;
> const char *res;
> - u32 start = 0, raw = 0, pos = 0;
>
> - memcpy(reg_def, &sx9310_default_regs[i], sizeof(*reg_def));
> + memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def));
> if (!np)
> return reg_def;
>
> @@ -1234,15 +1234,31 @@ sx9310_get_default_reg(struct sx9310_data *data, int i,
> reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
> }
>
> - reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
> - of_property_read_u32_array(np, "semtech,combined-sensors",
> - combined, ARRAY_SIZE(combined));
> - for (i = 0; i < ARRAY_SIZE(combined); i++) {
> - if (combined[i] <= SX9310_NUM_CHANNELS)
> - comb_mask |= BIT(combined[i]);
> + count = of_property_count_elems_of_size(np, "semtech,combined-sensors",
> + sizeof(u32));
instead of doing this manual validation with arrays, why not take the
error codes from of_property_read_u32_array() and check them?
ret = of_property_read_u32_array(np, "semtech,combined-sensors",
combined, ARRAY_SIZE(combined));
checking for ret < 0 looks sufficient;
it should return an error code if the number of elements in the DT
array isn't exactly ARRAY_SIZE(combined)
but stepping back a bit, the current logic (before this patch) looks
reasonably ok to me;
albeit, it's a bit quirky; and may require some more explanation
somewhere, also in the DT
semtech,combined-sensors:
$ref: /schemas/types.yaml#/definitions/uint32-array
description: |
List of which sensors are combined and represented by CS3.
Possible values are -
3 - CS3 (internal)
0 1 - CS0 + CS1
1 2 - CS1 + CS2 (default)
0 1 2 3 - CS0 + CS1 + CS2 + CS3
items:
enum: [ 0, 1, 2, 3 ]
minItems: 1
maxItems: 4
i mean, if nothing is defined there, value is 4 for the elements that
are not defined in the DT;
but i think there may be a bug, in the fact that number of elements in
the array must be 4 (no more, no less)
that is, when reading the DT, it implies a variable array size of 1 to
4, which doesn't seem true in the code;
maybe a better idea would be to use of_property_read_***variable***_u32_array()
this would allow for a flexible array size;
count = of_property_read_variable_u32_array(np,
"semtech,combined-sensors",
combined, 1,
ARRAY_SIZE(combined)); // min 1 , max 4 elements
// count must be int, so that if it is negative, the
array doesn't loop;
for (i = 0; i < count; i++) {
if (combined[i] <= SX9310_NUM_CHANNELS)
comb_mask |= BIT(combined[i]);
}
> + if (count > 0 && count <= ARRAY_SIZE(combined)) {
> + ret = of_property_read_u32_array(np, "semtech,combined-sensors",
> + combined, count);
> + if (ret)
> + break;
> + } else {
> + /*
> + * Either the property does not exist in the DT or the
> + * number of entries is incorrect.
> + */
> + break;
> + }
> + for (i = 0; i < count; i++) {
> + if (combined[i] >= SX9310_NUM_CHANNELS) {
> + /* Invalid sensor (invalid DT). */
> + break;
> + }
> + comb_mask |= BIT(combined[i]);
> }
> + if (i < count)
> + break;
>
> - comb_mask &= 0xf;
> + reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
> if (comb_mask == (BIT(3) | BIT(2) | BIT(1) | BIT(0)))
> reg_def->def |= SX9310_REG_PROX_CTRL2_COMBMODE_CS0_CS1_CS2_CS3;
> else if (comb_mask == (BIT(1) | BIT(2)))
> --
> 2.31.0.291.g576ba9dcdaf-goog
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v6 1/2] iio: sx9310: Fix access to variable DT array
2021-03-22 11:16 ` Alexandru Ardelean
@ 2021-03-22 13:28 ` Andy Shevchenko
2021-03-22 15:16 ` Alexandru Ardelean
0 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2021-03-22 13:28 UTC (permalink / raw)
To: Alexandru Ardelean
Cc: Gwendal Grignou, Jonathan Cameron, Lars-Peter Clausen,
Stephen Boyd, Daniel Campello, linux-iio
On Mon, Mar 22, 2021 at 1:16 PM Alexandru Ardelean
<ardeleanalex@gmail.com> wrote:
> On Mon, Mar 22, 2021 at 9:33 AM Gwendal Grignou <gwendal@chromium.org> wrote:
...
> but i think there may be a bug, in the fact that number of elements in
> the array must be 4 (no more, no less)
> that is, when reading the DT, it implies a variable array size of 1 to
> 4, which doesn't seem true in the code;
>
> maybe a better idea would be to use of_property_read_***variable***_u32_array()
> this would allow for a flexible array size;
Wouldn't work in conjunction with the second patch.
The above API is OF specific.
> count = of_property_read_variable_u32_array(np,
> "semtech,combined-sensors",
> combined, 1,
> ARRAY_SIZE(combined)); // min 1 , max 4 elements
> // count must be int, so that if it is negative, the
> array doesn't loop;
> for (i = 0; i < count; i++) {
> if (combined[i] <= SX9310_NUM_CHANNELS)
> comb_mask |= BIT(combined[i]);
> }
>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v6 1/2] iio: sx9310: Fix access to variable DT array
2021-03-22 13:28 ` Andy Shevchenko
@ 2021-03-22 15:16 ` Alexandru Ardelean
0 siblings, 0 replies; 8+ messages in thread
From: Alexandru Ardelean @ 2021-03-22 15:16 UTC (permalink / raw)
To: Andy Shevchenko
Cc: Gwendal Grignou, Jonathan Cameron, Lars-Peter Clausen,
Stephen Boyd, Daniel Campello, linux-iio
On Mon, Mar 22, 2021 at 3:29 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Mon, Mar 22, 2021 at 1:16 PM Alexandru Ardelean
> <ardeleanalex@gmail.com> wrote:
> > On Mon, Mar 22, 2021 at 9:33 AM Gwendal Grignou <gwendal@chromium.org> wrote:
>
> ...
>
> > but i think there may be a bug, in the fact that number of elements in
> > the array must be 4 (no more, no less)
> > that is, when reading the DT, it implies a variable array size of 1 to
> > 4, which doesn't seem true in the code;
> >
> > maybe a better idea would be to use of_property_read_***variable***_u32_array()
> > this would allow for a flexible array size;
>
> Wouldn't work in conjunction with the second patch.
> The above API is OF specific.
oh
good point
>
> > count = of_property_read_variable_u32_array(np,
> > "semtech,combined-sensors",
> > combined, 1,
> > ARRAY_SIZE(combined)); // min 1 , max 4 elements
> > // count must be int, so that if it is negative, the
> > array doesn't loop;
> > for (i = 0; i < count; i++) {
> > if (combined[i] <= SX9310_NUM_CHANNELS)
> > comb_mask |= BIT(combined[i]);
> > }
> >
>
> --
> With Best Regards,
> Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v6 2/2] iio: sx9310: Support ACPI properties
2021-03-22 9:53 ` Alexandru Ardelean
@ 2021-03-26 18:27 ` Gwendal Grignou
0 siblings, 0 replies; 8+ messages in thread
From: Gwendal Grignou @ 2021-03-26 18:27 UTC (permalink / raw)
To: Alexandru Ardelean
Cc: Jonathan Cameron, Lars-Peter Clausen, Stephen Boyd,
Daniel Campello, Andy Shevchenko, linux-iio
On Mon, Mar 22, 2021 at 2:54 AM Alexandru Ardelean
<ardeleanalex@gmail.com> wrote:
>
> On Mon, Mar 22, 2021 at 9:33 AM Gwendal Grignou <gwendal@chromium.org> wrote:
> >
> > Use device_property_read_uXX to support both device tree and ACPI
> > bindings when reading the properties we need to configure the SAR
> > sensor.
> >
> > Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> > Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> > ---
> > Changes in v6:
> > Use proper function to gather the number of elements in an array.
> >
> > Changes in v5:
> > Split in 2 patches, one for fixing access to propery array, the other
> > to support ACPI.
> >
> > drivers/iio/proximity/sx9310.c | 26 +++++++++++---------------
> > 1 file changed, 11 insertions(+), 15 deletions(-)
> >
> > diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> > index 289c76bb3b024..3817a5714aab3 100644
> > --- a/drivers/iio/proximity/sx9310.c
> > +++ b/drivers/iio/proximity/sx9310.c
> > @@ -20,6 +20,7 @@
> > #include <linux/mod_devicetable.h>
> > #include <linux/module.h>
> > #include <linux/pm.h>
> > +#include <linux/property.h>
> > #include <linux/regmap.h>
> > #include <linux/regulator/consumer.h>
> > #include <linux/slab.h>
> > @@ -1213,10 +1214,9 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> > }
> >
> > static const struct sx9310_reg_default *
> > -sx9310_get_default_reg(struct sx9310_data *data, int idx,
> > +sx9310_get_default_reg(struct device *dev, int idx,
> > struct sx9310_reg_default *reg_def)
> > {
> > - const struct device_node *np = data->client->dev.of_node;
>
> There may be an issue with this.
> 'np' was coming from the parent device (which is an i2c device)
>
> So, 'data->client->dev' should be the same pointer as 'indio_dev->dev.parent'
>
> Now, it's a reference to 'indio_dev->dev', so it's probably going to
> miss a lot of DT properties.
> Maybe an alternative would be to do:
>
> const struct device *dev = &data->client->dev; [1]
>
> Or instead of pass '&data->client->dev' or 'indio_dev->dev.parent'
> when calling sx9310_get_default_reg()
>
> Though [1] looks good to me as well.
Indeed, the current code does not work with device-tree bindings. The
proposed fix solves the issue, sending v7.
Thanks,
Gwendal.
>
> > u32 combined[SX9310_NUM_CHANNELS];
> > u32 start = 0, raw = 0, pos = 0;
> > unsigned long comb_mask = 0;
> > @@ -1224,21 +1224,17 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
> > const char *res;
> >
> > memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def));
> > - if (!np)
> > - return reg_def;
> > -
> > switch (reg_def->reg) {
> > case SX9310_REG_PROX_CTRL2:
> > - if (of_property_read_bool(np, "semtech,cs0-ground")) {
> > + if (device_property_read_bool(dev, "semtech,cs0-ground")) {
> > reg_def->def &= ~SX9310_REG_PROX_CTRL2_SHIELDEN_MASK;
> > reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
> > }
> >
> > - count = of_property_count_elems_of_size(np, "semtech,combined-sensors",
> > - sizeof(u32));
> > + count = device_property_count_u32(dev, "semtech,combined-sensors");
> > if (count > 0 && count <= ARRAY_SIZE(combined)) {
> > - ret = of_property_read_u32_array(np, "semtech,combined-sensors",
> > - combined, count);
> > + ret = device_property_read_u32_array(dev, "semtech,combined-sensors",
> > + combined, count);
> > if (ret)
> > break;
> > } else {
> > @@ -1270,7 +1266,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
> >
> > break;
> > case SX9310_REG_PROX_CTRL4:
> > - ret = of_property_read_string(np, "semtech,resolution", &res);
> > + ret = device_property_read_string(dev, "semtech,resolution", &res);
> > if (ret)
> > break;
> >
> > @@ -1294,7 +1290,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
> >
> > break;
> > case SX9310_REG_PROX_CTRL5:
> > - ret = of_property_read_u32(np, "semtech,startup-sensor", &start);
> > + ret = device_property_read_u32(dev, "semtech,startup-sensor", &start);
> > if (ret) {
> > start = FIELD_GET(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
> > reg_def->def);
> > @@ -1304,7 +1300,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
> > reg_def->def |= FIELD_PREP(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
> > start);
> >
> > - ret = of_property_read_u32(np, "semtech,proxraw-strength", &raw);
> > + ret = device_property_read_u32(dev, "semtech,proxraw-strength", &raw);
> > if (ret) {
> > raw = FIELD_GET(SX9310_REG_PROX_CTRL5_RAWFILT_MASK,
> > reg_def->def);
> > @@ -1317,7 +1313,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
> > raw);
> > break;
> > case SX9310_REG_PROX_CTRL7:
> > - ret = of_property_read_u32(np, "semtech,avg-pos-strength", &pos);
> > + ret = device_property_read_u32(dev, "semtech,avg-pos-strength", &pos);
> > if (ret)
> > break;
> >
> > @@ -1353,7 +1349,7 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
> >
> > /* Program some sane defaults. */
> > for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
> > - initval = sx9310_get_default_reg(data, i, &tmp);
> > + initval = sx9310_get_default_reg(&indio_dev->dev, i, &tmp);
> > ret = regmap_write(data->regmap, initval->reg, initval->def);
> > if (ret)
> > return ret;
> > --
> > 2.31.0.291.g576ba9dcdaf-goog
> >
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2021-03-26 18:27 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-22 7:32 [PATCH v6 0/2] iio: sx9310: Support ACPI properties Gwendal Grignou
2021-03-22 7:32 ` [PATCH v6 1/2] iio: sx9310: Fix access to variable DT array Gwendal Grignou
2021-03-22 11:16 ` Alexandru Ardelean
2021-03-22 13:28 ` Andy Shevchenko
2021-03-22 15:16 ` Alexandru Ardelean
2021-03-22 7:32 ` [PATCH v6 2/2] iio: sx9310: Support ACPI properties Gwendal Grignou
2021-03-22 9:53 ` Alexandru Ardelean
2021-03-26 18:27 ` Gwendal Grignou
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.