diff mbox series

[v3,6/8] iio: sx9324: Add Setting for internal compensation resistor

Message ID 20220325220827.3719273-7-gwendal@chromium.org (mailing list archive)
State Changes Requested
Headers show
Series Add settings for precharge and internal resistor | expand

Commit Message

Gwendal Grignou March 25, 2022, 10:08 p.m. UTC
Based on device tree setting, set the internal compensation resistor.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
Changes since v2:
- No changes.

Changes since v1:
- No changes.

 drivers/iio/proximity/sx9324.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

Comments

Stephen Boyd March 26, 2022, 12:04 a.m. UTC | #1
Quoting Gwendal Grignou (2022-03-25 15:08:25)
> diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
> index 3f229dffd5380..38dfdc6dc86ad 100644
> --- a/drivers/iio/proximity/sx9324.c
> +++ b/drivers/iio/proximity/sx9324.c
> @@ -875,6 +881,22 @@ sx9324_get_default_reg(struct device *dev, int idx,
>                                SX9324_REG_AFE_PH0_PIN_MASK(pin);
>                 reg_def->def = raw;
>                 break;
> +       case SX9324_REG_AFE_CTRL0:
> +               ret = device_property_read_string(dev,
> +                               "semtech,int-comp-resistor", &res);
> +               if (ret)
> +                       break;
> +               reg_def->def &= ~SX9324_REG_AFE_CTRL0_RINT_MASK;
> +               if (!strcmp(res, "lowest"))
> +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_LOWEST;
> +               else if (!strcmp(res, "low"))
> +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_LOW;
> +               else if (!strcmp(res, "high"))

Should the order of arguments be swapped? I'm wondering if I can have
'hig' in the DT property and then it will match 'high' here? I suppose
the schema validator will find that and complain first.

> +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_HIGH;
> +               else if (!strcmp(res, "highest"))
> +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_HIGHEST;
> +
> +               break;
Jonathan Cameron March 27, 2022, 1:43 p.m. UTC | #2
On Fri, 25 Mar 2022 19:04:32 -0500
Stephen Boyd <swboyd@chromium.org> wrote:

> Quoting Gwendal Grignou (2022-03-25 15:08:25)
> > diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
> > index 3f229dffd5380..38dfdc6dc86ad 100644
> > --- a/drivers/iio/proximity/sx9324.c
> > +++ b/drivers/iio/proximity/sx9324.c
> > @@ -875,6 +881,22 @@ sx9324_get_default_reg(struct device *dev, int idx,
> >                                SX9324_REG_AFE_PH0_PIN_MASK(pin);
> >                 reg_def->def = raw;
> >                 break;
> > +       case SX9324_REG_AFE_CTRL0:
> > +               ret = device_property_read_string(dev,
> > +                               "semtech,int-comp-resistor", &res);
> > +               if (ret)
> > +                       break;
> > +               reg_def->def &= ~SX9324_REG_AFE_CTRL0_RINT_MASK;
> > +               if (!strcmp(res, "lowest"))
> > +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_LOWEST;
> > +               else if (!strcmp(res, "low"))
> > +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_LOW;
> > +               else if (!strcmp(res, "high"))  
> 
> Should the order of arguments be swapped? I'm wondering if I can have
> 'hig' in the DT property and then it will match 'high' here? I suppose
> the schema validator will find that and complain first.

Wouldn't that result in a failed match between the null terminator of the first
string and the 'h'? Hence I don't think it matters but maybe I'm missing something..

> 
> > +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_HIGH;
> > +               else if (!strcmp(res, "highest"))
> > +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_HIGHEST;
> > +
> > +               break;
Andy Shevchenko March 27, 2022, 7:08 p.m. UTC | #3
On Sat, Mar 26, 2022 at 12:34 AM Gwendal Grignou <gwendal@chromium.org> wrote:
>
> Based on device tree setting, set the internal compensation resistor.

...

> +               if (!strcmp(res, "lowest"))
> +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_LOWEST;
> +               else if (!strcmp(res, "low"))
> +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_LOW;
> +               else if (!strcmp(res, "high"))
> +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_HIGH;
> +               else if (!strcmp(res, "highest"))
> +                       reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_HIGHEST;

As far as I can see the values, you can simply put the string literals
into the array:

static const char * const sx9324_rints = { "lowest", "low", "high", "highest" };

Then

  ret = match_string(sx9324_rints, ARRAY_SIZE(sx9324_rints), res);
  if (ret < 0)
    return ret;
  reg_def->def |= ret << SX9324_REG_AFE_CTRL0_RINT_SHIFT;
diff mbox series

Patch

diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
index 3f229dffd5380..38dfdc6dc86ad 100644
--- a/drivers/iio/proximity/sx9324.c
+++ b/drivers/iio/proximity/sx9324.c
@@ -52,6 +52,11 @@ 
 #define SX9324_REG_CLK_SPRD		0x15
 
 #define SX9324_REG_AFE_CTRL0		0x20
+#define SX9324_REG_AFE_CTRL0_RINT_MASK GENMASK(7, 6)
+#define SX9324_REG_AFE_CTRL0_RINT_LOWEST	0x00
+#define SX9324_REG_AFE_CTRL0_RINT_LOW		0x40
+#define SX9324_REG_AFE_CTRL0_RINT_HIGH		0x80
+#define SX9324_REG_AFE_CTRL0_RINT_HIGHEST	0xc0
 #define SX9324_REG_AFE_CTRL1		0x21
 #define SX9324_REG_AFE_CTRL2		0x22
 #define SX9324_REG_AFE_CTRL3		0x23
@@ -769,7 +774,7 @@  static const struct sx_common_reg_default sx9324_default_regs[] = {
 	 */
 	{ SX9324_REG_GNRL_CTRL1, SX9324_REG_GNRL_CTRL1_PAUSECTRL },
 
-	{ SX9324_REG_AFE_CTRL0, 0x00 },
+	{ SX9324_REG_AFE_CTRL0, SX9324_REG_AFE_CTRL0_RINT_LOWEST },
 	{ SX9324_REG_AFE_CTRL3, 0x00 },
 	{ SX9324_REG_AFE_CTRL4, SX9324_REG_AFE_CTRL4_FREQ_83_33HZ |
 		SX9324_REG_AFE_CTRL4_RES_100 },
@@ -855,6 +860,7 @@  sx9324_get_default_reg(struct device *dev, int idx,
 	char prop[] = SX9324_PROXRAW_DEF;
 	u32 start = 0, raw = 0, pos = 0;
 	int ret, count, ph, pin;
+	const char *res;
 
 	memcpy(reg_def, &sx9324_default_regs[idx], sizeof(*reg_def));
 	switch (reg_def->reg) {
@@ -875,6 +881,22 @@  sx9324_get_default_reg(struct device *dev, int idx,
 			       SX9324_REG_AFE_PH0_PIN_MASK(pin);
 		reg_def->def = raw;
 		break;
+	case SX9324_REG_AFE_CTRL0:
+		ret = device_property_read_string(dev,
+				"semtech,int-comp-resistor", &res);
+		if (ret)
+			break;
+		reg_def->def &= ~SX9324_REG_AFE_CTRL0_RINT_MASK;
+		if (!strcmp(res, "lowest"))
+			reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_LOWEST;
+		else if (!strcmp(res, "low"))
+			reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_LOW;
+		else if (!strcmp(res, "high"))
+			reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_HIGH;
+		else if (!strcmp(res, "highest"))
+			reg_def->def |= SX9324_REG_AFE_CTRL0_RINT_HIGHEST;
+
+		break;
 	case SX9324_REG_AFE_CTRL4:
 	case SX9324_REG_AFE_CTRL7:
 		if (reg_def->reg == SX9324_REG_AFE_CTRL4)