iio: sx9310: Support ACPI property

Use device_property_read_... to support both device tree and ACPI
bindings.
Simplify the logic for reading "combined-sensors" array, as we assume
it has been vetted at firmware build time.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-by: Stephen Boyd <swboyd@chromium.org>
Link: https://lore.kernel.org/r/20210728181757.187627-1-gwendal@chromium.org
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
Gwendal Grignou 2021-07-28 11:17:57 -07:00 committed by Jonathan Cameron
parent 9491b9177f
commit 7a3605bef8

View File

@ -20,6 +20,7 @@
#include <linux/mod_devicetable.h> #include <linux/mod_devicetable.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/pm.h> #include <linux/pm.h>
#include <linux/property.h>
#include <linux/regmap.h> #include <linux/regmap.h>
#include <linux/regulator/consumer.h> #include <linux/regulator/consumer.h>
#include <linux/slab.h> #include <linux/slab.h>
@ -1221,10 +1222,9 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
} }
static const struct sx9310_reg_default * 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) struct sx9310_reg_default *reg_def)
{ {
const struct device_node *np = data->client->dev.of_node;
u32 combined[SX9310_NUM_CHANNELS]; u32 combined[SX9310_NUM_CHANNELS];
u32 start = 0, raw = 0, pos = 0; u32 start = 0, raw = 0, pos = 0;
unsigned long comb_mask = 0; unsigned long comb_mask = 0;
@ -1232,39 +1232,23 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
const char *res; const char *res;
memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def)); memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def));
if (!np)
return reg_def;
switch (reg_def->reg) { switch (reg_def->reg) {
case SX9310_REG_PROX_CTRL2: 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_MASK;
reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND; reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
} }
count = of_property_count_elems_of_size(np, "semtech,combined-sensors", count = device_property_count_u32(dev, "semtech,combined-sensors");
sizeof(u32)); if (count < 0 || count > ARRAY_SIZE(combined))
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; break;
} ret = device_property_read_u32_array(dev, "semtech,combined-sensors",
for (i = 0; i < count; i++) { combined, count);
if (combined[i] >= SX9310_NUM_CHANNELS) { if (ret)
/* Invalid sensor (invalid DT). */ break;
break;
} for (i = 0; i < count; i++)
comb_mask |= BIT(combined[i]); comb_mask |= BIT(combined[i]);
}
if (i < count)
break;
reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK; reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
if (comb_mask == (BIT(3) | BIT(2) | BIT(1) | BIT(0))) if (comb_mask == (BIT(3) | BIT(2) | BIT(1) | BIT(0)))
@ -1278,7 +1262,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
break; break;
case SX9310_REG_PROX_CTRL4: 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) if (ret)
break; break;
@ -1302,7 +1286,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
break; break;
case SX9310_REG_PROX_CTRL5: 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) { if (ret) {
start = FIELD_GET(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK, start = FIELD_GET(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
reg_def->def); reg_def->def);
@ -1312,7 +1296,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
reg_def->def |= FIELD_PREP(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK, reg_def->def |= FIELD_PREP(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
start); start);
ret = of_property_read_u32(np, "semtech,proxraw-strength", &raw); ret = device_property_read_u32(dev, "semtech,proxraw-strength", &raw);
if (ret) { if (ret) {
raw = FIELD_GET(SX9310_REG_PROX_CTRL5_RAWFILT_MASK, raw = FIELD_GET(SX9310_REG_PROX_CTRL5_RAWFILT_MASK,
reg_def->def); reg_def->def);
@ -1325,7 +1309,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int idx,
raw); raw);
break; break;
case SX9310_REG_PROX_CTRL7: 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) if (ret)
break; break;
@ -1361,7 +1345,7 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
/* Program some sane defaults. */ /* Program some sane defaults. */
for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) { 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); ret = regmap_write(data->regmap, initval->reg, initval->def);
if (ret) if (ret)
return ret; return ret;