2020-07-25 00:41:36

by Daniel Campello

[permalink] [raw]
Subject: [PATCH] iio: sx9310: Fixes dropped on initial commit

This patch brings back fixes on v9 of initial patch that got dropped
when v8 was taken instead.
- Updated Copyright
- Updated macro definitions
- Simplified return condition checks
- Removed ACPI and of table macros

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 377 ++++++++++++++++-----------------
1 file changed, 177 insertions(+), 200 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index d161f3061e353d..55b3d5b83e5a6a 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: GPL-2.0
/*
- * Copyright 2018 Google LLC.
+ * Copyright 2020 Google LLC.
*
* Driver for Semtech's SX9310/SX9311 capacitive proximity/button solution.
* Based on SX9500 driver and Semtech driver using the input framework
* <https://my.syncplicity.com/share/teouwsim8niiaud/
* linux-driver-SX9310_NoSmartHSensing>.
- * Reworked April 2019 by Evan Green <[email protected]>
- * and January 2020 by Daniel Campello <[email protected]>
+ * Reworked in April 2019 by Evan Green <[email protected]>
+ * and in January 2020 by Daniel Campello <[email protected]>.
*/

#include <linux/acpi.h>
@@ -16,7 +16,6 @@
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
-#include <linux/of.h>
#include <linux/pm.h>
#include <linux/regmap.h>
#include <linux/slab.h>
@@ -33,45 +32,45 @@
#define SX9310_REG_IRQ_SRC 0x00
#define SX9310_REG_STAT0 0x01
#define SX9310_REG_STAT1 0x02
+#define SX9310_REG_STAT1_COMPSTAT_MASK GENMASK(3, 0)
#define SX9310_REG_IRQ_MSK 0x03
#define SX9310_CONVDONE_IRQ BIT(3)
#define SX9310_FAR_IRQ BIT(5)
#define SX9310_CLOSE_IRQ BIT(6)
-#define SX9310_EVENT_IRQ (SX9310_FAR_IRQ | \
- SX9310_CLOSE_IRQ)
#define SX9310_REG_IRQ_FUNC 0x04

#define SX9310_REG_PROX_CTRL0 0x10
-#define SX9310_REG_PROX_CTRL0_PROXSTAT2 0x10
-#define SX9310_REG_PROX_CTRL0_EN_MASK 0x0F
+#define SX9310_REG_PROX_CTRL0_SENSOREN_MASK GENMASK(3, 0)
+#define SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK GENMASK(7, 4)
+#define SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT 4
+#define SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS 0x01
#define SX9310_REG_PROX_CTRL1 0x11
#define SX9310_REG_PROX_CTRL2 0x12
-#define SX9310_REG_PROX_CTRL2_COMBMODE_ALL 0x80
-#define SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC 0x04
+#define SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 (0x02 << 6)
+#define SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC (0x01 << 2)
#define SX9310_REG_PROX_CTRL3 0x13
-#define SX9310_REG_PROX_CTRL3_GAIN0_X8 0x0c
+#define SX9310_REG_PROX_CTRL3_GAIN0_X8 (0x03 << 2)
#define SX9310_REG_PROX_CTRL3_GAIN12_X4 0x02
#define SX9310_REG_PROX_CTRL4 0x14
#define SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST 0x07
#define SX9310_REG_PROX_CTRL5 0x15
-#define SX9310_REG_PROX_CTRL5_RANGE_SMALL 0xc0
-#define SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 0x04
+#define SX9310_REG_PROX_CTRL5_RANGE_SMALL (0x03 << 6)
+#define SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 (0x01 << 2)
#define SX9310_REG_PROX_CTRL5_RAWFILT_1P25 0x02
#define SX9310_REG_PROX_CTRL6 0x16
-#define SX9310_REG_PROX_CTRL6_COMP_COMMON 0x20
+#define SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT 0x20
#define SX9310_REG_PROX_CTRL7 0x17
-#define SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 0x08
+#define SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 (0x01 << 3)
#define SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 0x05
#define SX9310_REG_PROX_CTRL8 0x18
#define SX9310_REG_PROX_CTRL9 0x19
-#define SX9310_REG_PROX_CTRL8_9_PTHRESH12_28 0x40
-#define SX9310_REG_PROX_CTRL8_9_PTHRESH_96 0x88
+#define SX9310_REG_PROX_CTRL8_9_PTHRESH_28 (0x08 << 3)
+#define SX9310_REG_PROX_CTRL8_9_PTHRESH_96 (0x11 << 3)
#define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 0x03
#define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 0x05
#define SX9310_REG_PROX_CTRL10 0x1a
-#define SX9310_REG_PROX_CTRL10_HYST_6PCT 0x10
-#define SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8 0x12
-#define SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8 0x03
+#define SX9310_REG_PROX_CTRL10_HYST_6PCT (0x01 << 4)
+#define SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 0x01
#define SX9310_REG_PROX_CTRL11 0x1b
#define SX9310_REG_PROX_CTRL12 0x1c
#define SX9310_REG_PROX_CTRL13 0x1d
@@ -82,8 +81,8 @@
#define SX9310_REG_PROX_CTRL18 0x22
#define SX9310_REG_PROX_CTRL19 0x23
#define SX9310_REG_SAR_CTRL0 0x2a
-#define SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES 0x40
-#define SX9310_REG_SAR_CTRL0_SARHYST_8 0x10
+#define SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES (0x02 << 5)
+#define SX9310_REG_SAR_CTRL0_SARHYST_8 (0x02 << 3)
#define SX9310_REG_SAR_CTRL1 0x2b
/* Each increment of the slope register is 0.0078125. */
#define SX9310_REG_SAR_CTRL1_SLOPE(_hnslope) (_hnslope / 78125)
@@ -91,39 +90,27 @@
#define SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT 0x3c

#define SX9310_REG_SENSOR_SEL 0x30
-
#define SX9310_REG_USE_MSB 0x31
#define SX9310_REG_USE_LSB 0x32
-
#define SX9310_REG_AVG_MSB 0x33
#define SX9310_REG_AVG_LSB 0x34
-
#define SX9310_REG_DIFF_MSB 0x35
#define SX9310_REG_DIFF_LSB 0x36
-
#define SX9310_REG_OFFSET_MSB 0x37
#define SX9310_REG_OFFSET_LSB 0x38
-
#define SX9310_REG_SAR_MSB 0x39
#define SX9310_REG_SAR_LSB 0x3a
-
-#define SX9310_REG_I2CADDR 0x40
+#define SX9310_REG_I2C_ADDR 0x40
#define SX9310_REG_PAUSE 0x41
#define SX9310_REG_WHOAMI 0x42
#define SX9310_WHOAMI_VALUE 0x01
#define SX9311_WHOAMI_VALUE 0x02
-
#define SX9310_REG_RESET 0x7f
#define SX9310_SOFT_RESET 0xde

-#define SX9310_SCAN_PERIOD_MASK GENMASK(7, 4)
-#define SX9310_SCAN_PERIOD_SHIFT 4
-
-#define SX9310_COMPSTAT_MASK GENMASK(3, 0)

/* 4 hardware channels, as defined in STAT0: COMB, CS2, CS1 and CS0. */
#define SX9310_NUM_CHANNELS 4
-#define SX9310_CHAN_ENABLED_MASK GENMASK(3, 0)

struct sx9310_data {
/* Serialize access to registers and channel configuration */
@@ -137,12 +124,12 @@ struct sx9310_data {
*/
bool prox_stat[SX9310_NUM_CHANNELS];
bool trigger_enabled;
- __be16 buffer[SX9310_NUM_CHANNELS +
- 4]; /* 64-bit data + 64-bit timestamp */
+ /* 64-bit data + 64-bit timestamp buffer */
+ __be16 buffer[SX9310_NUM_CHANNELS + 4];
/* Remember enabled channels and sample rate during suspend. */
unsigned int suspend_ctrl0;
struct completion completion;
- unsigned int chan_read, chan_event;
+ unsigned long chan_read, chan_event;
int channel_users[SX9310_NUM_CHANNELS];
int whoami;
};
@@ -251,7 +238,7 @@ static const struct regmap_range sx9310_readable_reg_ranges[] = {
regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19),
regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2),
regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SAR_LSB),
- regmap_reg_range(SX9310_REG_I2CADDR, SX9310_REG_WHOAMI),
+ regmap_reg_range(SX9310_REG_I2C_ADDR, SX9310_REG_WHOAMI),
regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
};

@@ -285,15 +272,16 @@ static const struct regmap_config sx9310_regmap_config = {
};

static int sx9310_update_chan_en(struct sx9310_data *data,
- unsigned int chan_read,
- unsigned int chan_event)
+ unsigned long chan_read,
+ unsigned long chan_event)
{
int ret;
+ unsigned long channels = chan_read | chan_event;

- if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) {
+ if ((data->chan_read | data->chan_event) != channels) {
ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
- SX9310_CHAN_ENABLED_MASK,
- chan_read | chan_event);
+ SX9310_REG_PROX_CTRL0_SENSOREN_MASK,
+ channels);
if (ret)
return ret;
}
@@ -342,10 +330,10 @@ static int sx9310_read_prox_data(struct sx9310_data *data,
int ret;

ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel);
- if (ret < 0)
+ if (ret)
return ret;

- return regmap_bulk_read(data->regmap, chan->address, val, 2);
+ return regmap_bulk_read(data->regmap, chan->address, val, sizeof(*val));
}

/*
@@ -358,10 +346,11 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
unsigned int val;

ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val);
- if (ret < 0)
+ if (ret)
return ret;

- val = (val & SX9310_SCAN_PERIOD_MASK) >> SX9310_SCAN_PERIOD_SHIFT;
+ val = (val & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
+ SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT;

msleep(sx9310_scan_period_table[val]);

@@ -371,22 +360,24 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
static int sx9310_read_proximity(struct sx9310_data *data,
const struct iio_chan_spec *chan, int *val)
{
- int ret = 0;
+ int ret;
__be16 rawval;

mutex_lock(&data->mutex);

ret = sx9310_get_read_channel(data, chan->channel);
- if (ret < 0)
+ if (ret)
goto out;

- ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
- if (ret < 0)
- goto out_put_channel;
+ if (data->client->irq) {
+ ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
+ if (ret)
+ goto out_put_channel;
+ }

mutex_unlock(&data->mutex);

- if (data->client->irq > 0) {
+ if (data->client->irq) {
ret = wait_for_completion_interruptible(&data->completion);
reinit_completion(&data->completion);
} else {
@@ -395,22 +386,24 @@ static int sx9310_read_proximity(struct sx9310_data *data,

mutex_lock(&data->mutex);

- if (ret < 0)
+ if (ret)
goto out_disable_irq;

ret = sx9310_read_prox_data(data, chan, &rawval);
- if (ret < 0)
+ if (ret)
goto out_disable_irq;

*val = sign_extend32(be16_to_cpu(rawval),
- (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));
+ chan->address == SX9310_REG_DIFF_MSB ? 11 : 15);

- ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
- if (ret < 0)
- goto out_put_channel;
+ if (data->client->irq) {
+ ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
+ if (ret)
+ goto out_put_channel;
+ }

ret = sx9310_put_read_channel(data, chan->channel);
- if (ret < 0)
+ if (ret)
goto out;

mutex_unlock(&data->mutex);
@@ -418,7 +411,8 @@ static int sx9310_read_proximity(struct sx9310_data *data,
return IIO_VAL_INT;

out_disable_irq:
- sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
+ if (data->client->irq)
+ sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
out_put_channel:
sx9310_put_read_channel(data, chan->channel);
out:
@@ -430,12 +424,14 @@ static int sx9310_read_proximity(struct sx9310_data *data,
static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
{
unsigned int regval;
- int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
+ int ret;

- if (ret < 0)
+ ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
+ if (ret)
return ret;

- regval = (regval & SX9310_SCAN_PERIOD_MASK) >> SX9310_SCAN_PERIOD_SHIFT;
+ regval = (regval & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
+ SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT;
*val = sx9310_samp_freq_table[regval].val;
*val2 = sx9310_samp_freq_table[regval].val2;

@@ -483,8 +479,8 @@ static int sx9310_set_samp_freq(struct sx9310_data *data, int val, int val2)
mutex_lock(&data->mutex);

ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
- SX9310_SCAN_PERIOD_MASK,
- i << SX9310_SCAN_PERIOD_SHIFT);
+ SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK,
+ i << SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT);

mutex_unlock(&data->mutex);

@@ -515,10 +511,9 @@ static irqreturn_t sx9310_irq_handler(int irq, void *private)
iio_trigger_poll(data->trig);

/*
- * Even if no event is enabled, we need to wake the thread to
- * clear the interrupt state by reading SX9310_REG_IRQ_SRC. It
- * is not possible to do that here because regmap_read takes a
- * mutex.
+ * Even if no event is enabled, we need to wake the thread to clear the
+ * interrupt state by reading SX9310_REG_IRQ_SRC.
+ * It is not possible to do that here because regmap_read takes a mutex.
*/
return IRQ_WAKE_THREAD;
}
@@ -532,18 +527,18 @@ static void sx9310_push_events(struct iio_dev *indio_dev)

/* Read proximity state on all channels */
ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val);
- if (ret < 0) {
+ if (ret) {
dev_err(&data->client->dev, "i2c transfer error in irq\n");
return;
}

- for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) {
+ for_each_set_bit(chan, &data->chan_event, SX9310_NUM_CHANNELS) {
int dir;
u64 ev;
- bool new_prox = val & BIT(chan);
+ bool new_prox;
+
+ new_prox = val & BIT(chan);

- if (!(data->chan_event & BIT(chan)))
- continue;
if (new_prox == data->prox_stat[chan])
/* No change on this channel. */
continue;
@@ -567,12 +562,12 @@ static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
mutex_lock(&data->mutex);

ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
- if (ret < 0) {
+ if (ret) {
dev_err(&data->client->dev, "i2c transfer error in irq\n");
goto out;
}

- if (val & SX9310_EVENT_IRQ)
+ if (val & (SX9310_FAR_IRQ | SX9310_CLOSE_IRQ))
sx9310_push_events(indio_dev);

if (val & SX9310_CONVDONE_IRQ)
@@ -600,6 +595,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
enum iio_event_direction dir, int state)
{
struct sx9310_data *data = iio_priv(indio_dev);
+ unsigned int eventirq = SX9310_FAR_IRQ | SX9310_CLOSE_IRQ;
int ret;

/* If the state hasn't changed, there's nothing to do. */
@@ -609,20 +605,20 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
mutex_lock(&data->mutex);
if (state) {
ret = sx9310_get_event_channel(data, chan->channel);
- if (ret < 0)
+ if (ret)
goto out_unlock;
if (!(data->chan_event & ~BIT(chan->channel))) {
- ret = sx9310_enable_irq(data, SX9310_EVENT_IRQ);
- if (ret < 0)
+ ret = sx9310_enable_irq(data, eventirq);
+ if (ret)
sx9310_put_event_channel(data, chan->channel);
}
} else {
ret = sx9310_put_event_channel(data, chan->channel);
- if (ret < 0)
+ if (ret)
goto out_unlock;
if (!data->chan_event) {
- ret = sx9310_disable_irq(data, SX9310_EVENT_IRQ);
- if (ret < 0)
+ ret = sx9310_disable_irq(data, eventirq);
+ if (ret)
sx9310_get_event_channel(data, chan->channel);
}
}
@@ -634,7 +630,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,

static struct attribute *sx9310_attributes[] = {
&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
- NULL,
+ NULL
};

static const struct attribute_group sx9310_attribute_group = {
@@ -661,7 +657,7 @@ static int sx9310_set_trigger_state(struct iio_trigger *trig, bool state)
ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
else if (!data->chan_read)
ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
- if (ret < 0)
+ if (ret)
goto out;

data->trigger_enabled = state;
@@ -690,7 +686,7 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
indio_dev->masklength) {
ret = sx9310_read_prox_data(data, &indio_dev->channels[bit],
&val);
- if (ret < 0)
+ if (ret)
goto out;

data->buffer[i++] = val;
@@ -710,13 +706,13 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
{
struct sx9310_data *data = iio_priv(indio_dev);
- unsigned int channels = 0;
+ unsigned long channels = 0;
int bit, ret;

mutex_lock(&data->mutex);
for_each_set_bit(bit, indio_dev->active_scan_mask,
indio_dev->masklength)
- channels |= BIT(indio_dev->channels[bit].channel);
+ __set_bit(indio_dev->channels[bit].channel, &channels);

ret = sx9310_update_chan_en(data, channels, data->chan_event);
mutex_unlock(&data->mutex);
@@ -746,89 +742,77 @@ struct sx9310_reg_default {
u8 def;
};

-#define SX_INIT(_reg, _def) \
- { \
- .reg = SX9310_REG_##_reg, \
- .def = _def, \
- }
-
static const struct sx9310_reg_default sx9310_default_regs[] = {
- SX_INIT(IRQ_MSK, 0x00),
- SX_INIT(IRQ_FUNC, 0x00),
+ { SX9310_REG_IRQ_MSK, 0x00 },
+ { SX9310_REG_IRQ_FUNC, 0x00 },
/*
* The lower 4 bits should not be set as it enable sensors measurements.
* Turning the detection on before the configuration values are set to
* good values can cause the device to return erroneous readings.
*/
- SX_INIT(PROX_CTRL0, SX9310_REG_PROX_CTRL0_PROXSTAT2),
- SX_INIT(PROX_CTRL1, 0x00),
- SX_INIT(PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_ALL |
- SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC),
- SX_INIT(PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
- SX9310_REG_PROX_CTRL3_GAIN12_X4),
- SX_INIT(PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST),
- SX_INIT(PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
- SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
- SX9310_REG_PROX_CTRL5_RAWFILT_1P25),
- SX_INIT(PROX_CTRL6, SX9310_REG_PROX_CTRL6_COMP_COMMON),
- SX_INIT(PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
- SX9310_REG_PROX_CTRL7_AVGPOSFILT_512),
- SX_INIT(PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
- SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500),
- SX_INIT(PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH12_28 |
- SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900),
- SX_INIT(PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
- SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8 |
- SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8),
- SX_INIT(PROX_CTRL11, 0x00),
- SX_INIT(PROX_CTRL12, 0x00),
- SX_INIT(PROX_CTRL13, 0x00),
- SX_INIT(PROX_CTRL14, 0x00),
- SX_INIT(PROX_CTRL15, 0x00),
- SX_INIT(PROX_CTRL16, 0x00),
- SX_INIT(PROX_CTRL17, 0x00),
- SX_INIT(PROX_CTRL18, 0x00),
- SX_INIT(PROX_CTRL19, 0x00),
- SX_INIT(SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
- SX9310_REG_SAR_CTRL0_SARHYST_8),
- SX_INIT(SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250)),
- SX_INIT(SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT),
+ { SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS },
+ { SX9310_REG_PROX_CTRL1, 0x00 },
+ { SX9310_REG_PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 |
+ SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC },
+ { SX9310_REG_PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
+ SX9310_REG_PROX_CTRL3_GAIN12_X4 },
+ { SX9310_REG_PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST },
+ { SX9310_REG_PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
+ SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
+ SX9310_REG_PROX_CTRL5_RAWFILT_1P25 },
+ { SX9310_REG_PROX_CTRL6, SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT },
+ { SX9310_REG_PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
+ SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 },
+ { SX9310_REG_PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
+ SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 },
+ { SX9310_REG_PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH_28 |
+ SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 },
+ { SX9310_REG_PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
+ SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 },
+ { SX9310_REG_PROX_CTRL11, 0x00 },
+ { SX9310_REG_PROX_CTRL12, 0x00 },
+ { SX9310_REG_PROX_CTRL13, 0x00 },
+ { SX9310_REG_PROX_CTRL14, 0x00 },
+ { SX9310_REG_PROX_CTRL15, 0x00 },
+ { SX9310_REG_PROX_CTRL16, 0x00 },
+ { SX9310_REG_PROX_CTRL17, 0x00 },
+ { SX9310_REG_PROX_CTRL18, 0x00 },
+ { SX9310_REG_PROX_CTRL19, 0x00 },
+ { SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
+ SX9310_REG_SAR_CTRL0_SARHYST_8 },
+ { SX9310_REG_SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250) },
+ { SX9310_REG_SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT },
};

/* Activate all channels and perform an initial compensation. */
static int sx9310_init_compensation(struct iio_dev *indio_dev)
{
struct sx9310_data *data = iio_priv(indio_dev);
- int i, ret;
+ int ret;
unsigned int val;
unsigned int ctrl0;

ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0);
- if (ret < 0)
+ if (ret)
return ret;

/* run the compensation phase on all channels */
ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
- ctrl0 | SX9310_REG_PROX_CTRL0_EN_MASK);
- if (ret < 0)
+ ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK);
+ if (ret)
return ret;

- for (i = 100; i >= 0; i--) {
- msleep(20);
- ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
- if (ret < 0)
- goto out;
- if (!(val & SX9310_COMPSTAT_MASK))
- break;
- }
-
- if (i < 0) {
- dev_err(&data->client->dev,
- "initial compensation timed out: 0x%02x", val);
- ret = -ETIMEDOUT;
+ ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
+ !(val & SX9310_REG_STAT1_COMPSTAT_MASK),
+ 20000, 2000000);
+ if (ret) {
+ if (ret == -ETIMEDOUT)
+ dev_err(&data->client->dev,
+ "0x02 << 3l compensation timed out: 0x%02x",
+ val);
+ return ret;
}

-out:
regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
return ret;
}
@@ -841,21 +825,21 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
unsigned int i, val;

ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET);
- if (ret < 0)
+ if (ret)
return ret;

usleep_range(1000, 2000); /* power-up time is ~1ms. */

/* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
- if (ret < 0)
+ if (ret)
return ret;

/* Program some sane defaults. */
for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
initval = &sx9310_default_regs[i];
ret = regmap_write(data->regmap, initval->reg, initval->def);
- if (ret < 0)
+ if (ret)
return ret;
}

@@ -863,25 +847,15 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
}

static int sx9310_set_indio_dev_name(struct device *dev,
- struct iio_dev *indio_dev,
- const struct i2c_device_id *id, int whoami)
+ struct iio_dev *indio_dev, int whoami)
{
- const struct acpi_device_id *acpi_id;
-
- /* id will be NULL when enumerated via ACPI */
- if (id) {
- if (id->driver_data != whoami)
- dev_err(dev, "WHOAMI does not match i2c_device_id: %s",
- id->name);
- } else if (ACPI_HANDLE(dev)) {
- acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
- if (!acpi_id)
- return -ENODEV;
- if (acpi_id->driver_data != whoami)
- dev_err(dev, "WHOAMI does not match acpi_device_id: %s",
- acpi_id->id);
- } else
+ unsigned int long ddata;
+
+ ddata = (uintptr_t)device_get_match_data(dev);
+ if (ddata != whoami) {
+ dev_err(dev, "WHOAMI does not match device data: %u", whoami);
return -ENODEV;
+ }

switch (whoami) {
case SX9310_WHOAMI_VALUE:
@@ -898,15 +872,15 @@ static int sx9310_set_indio_dev_name(struct device *dev,
return 0;
}

-static int sx9310_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
+static int sx9310_probe(struct i2c_client *client)
{
int ret;
+ struct device *dev = &client->dev;
struct iio_dev *indio_dev;
struct sx9310_data *data;

- indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
- if (indio_dev == NULL)
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+ if (!indio_dev)
return -ENOMEM;

data = iio_priv(indio_dev);
@@ -919,19 +893,17 @@ static int sx9310_probe(struct i2c_client *client,
return PTR_ERR(data->regmap);

ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
- if (ret < 0) {
- dev_err(&client->dev, "error in reading WHOAMI register: %d",
- ret);
+ if (ret) {
+ dev_err(dev, "error in reading WHOAMI register: %d", ret);
return ret;
}

- ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, id,
- data->whoami);
- if (ret < 0)
+ ret = sx9310_set_indio_dev_name(dev, indio_dev, data->whoami);
+ if (ret)
return ret;

- ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
- indio_dev->dev.parent = &client->dev;
+ ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(dev));
+ indio_dev->dev.parent = dev;
indio_dev->channels = sx9310_channels;
indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
indio_dev->info = &sx9310_info;
@@ -939,41 +911,41 @@ static int sx9310_probe(struct i2c_client *client,
i2c_set_clientdata(client, indio_dev);

ret = sx9310_init_device(indio_dev);
- if (ret < 0)
+ if (ret)
return ret;

if (client->irq) {
- ret = devm_request_threaded_irq(&client->dev, client->irq,
+ ret = devm_request_threaded_irq(dev, client->irq,
sx9310_irq_handler,
sx9310_irq_thread_handler,
IRQF_TRIGGER_LOW | IRQF_ONESHOT,
"sx9310_event", indio_dev);
- if (ret < 0)
+ if (ret)
return ret;

- data->trig =
- devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
- indio_dev->name, indio_dev->id);
+ data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
+ indio_dev->name,
+ indio_dev->id);
if (!data->trig)
return -ENOMEM;

- data->trig->dev.parent = &client->dev;
+ data->trig->dev.parent = dev;
data->trig->ops = &sx9310_trigger_ops;
iio_trigger_set_drvdata(data->trig, indio_dev);

- ret = devm_iio_trigger_register(&client->dev, data->trig);
+ ret = devm_iio_trigger_register(dev, data->trig);
if (ret)
return ret;
}

- ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
+ ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
iio_pollfunc_store_time,
sx9310_trigger_handler,
&sx9310_buffer_setup_ops);
- if (ret < 0)
+ if (ret)
return ret;

- return devm_iio_device_register(&client->dev, indio_dev);
+ return devm_iio_device_register(dev, indio_dev);
}

static int __maybe_unused sx9310_suspend(struct device *dev)
@@ -988,11 +960,10 @@ static int __maybe_unused sx9310_suspend(struct device *dev)
mutex_lock(&data->mutex);
ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
&data->suspend_ctrl0);
-
if (ret)
goto out;

- ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_EN_MASK;
+ ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK;
ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
if (ret)
goto out;
@@ -1017,12 +988,18 @@ static int __maybe_unused sx9310_resume(struct device *dev)

ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
data->suspend_ctrl0);
+ if (ret)
+ goto out;

-out:
mutex_unlock(&data->mutex);

enable_irq(data->client->irq);

+ return 0;
+
+out:
+ mutex_unlock(&data->mutex);
+
return ret;
}

@@ -1033,32 +1010,32 @@ static const struct dev_pm_ops sx9310_pm_ops = {
static const struct acpi_device_id sx9310_acpi_match[] = {
{ "STH9310", SX9310_WHOAMI_VALUE },
{ "STH9311", SX9311_WHOAMI_VALUE },
- {},
+ {}
};
MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);

static const struct of_device_id sx9310_of_match[] = {
{ .compatible = "semtech,sx9310" },
{ .compatible = "semtech,sx9311" },
- {},
+ {}
};
MODULE_DEVICE_TABLE(of, sx9310_of_match);

static const struct i2c_device_id sx9310_id[] = {
{ "sx9310", SX9310_WHOAMI_VALUE },
{ "sx9311", SX9311_WHOAMI_VALUE },
- {},
+ {}
};
MODULE_DEVICE_TABLE(i2c, sx9310_id);

static struct i2c_driver sx9310_driver = {
.driver = {
.name = "sx9310",
- .acpi_match_table = ACPI_PTR(sx9310_acpi_match),
- .of_match_table = of_match_ptr(sx9310_of_match),
+ .acpi_match_table = sx9310_acpi_match,
+ .of_match_table = sx9310_of_match,
.pm = &sx9310_pm_ops,
},
- .probe = sx9310_probe,
+ .probe_new = sx9310_probe,
.id_table = sx9310_id,
};
module_i2c_driver(sx9310_driver);
--
2.28.0.rc0.142.g3c755180ce-goog


2020-07-26 08:11:00

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH] iio: sx9310: Fixes dropped on initial commit

On Sat, Jul 25, 2020 at 3:41 AM Daniel Campello <[email protected]> wrote:
>
> This patch brings back fixes on v9 of initial patch that got dropped
> when v8 was taken instead.
> - Updated Copyright
> - Updated macro definitions
> - Simplified return condition checks
> - Removed ACPI and of table macros

Please, split to a series of patches.
I see at least ~4-5 by the changes below.

--
With Best Regards,
Andy Shevchenko

2020-07-26 11:54:55

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH] iio: sx9310: Fixes dropped on initial commit

On Fri, 24 Jul 2020 18:40:26 -0600
Daniel Campello <[email protected]> wrote:

> This patch brings back fixes on v9 of initial patch that got dropped
> when v8 was taken instead.

Oops. Not quite sure what happened there and I can't even find a record
of me saying I was applying v8. Sorry about that.

> - Updated Copyright
> - Updated macro definitions
> - Simplified return condition checks
> - Removed ACPI and of table macros
>
> Signed-off-by: Daniel Campello <[email protected]>

A few minor things I noticed whilst going through this.
As Andy pointed out it needs breaking down into a series of patches.

Please order any changes that are bug fixes at the front.

Given we also have Stephen's series in flight, it would be great if you
also pick up his patches to send me one combined series. That way
all the merge issues should be resolved by people who can test
the changes!

Thanks,

Jonathan

> ---
>
> drivers/iio/proximity/sx9310.c | 377 ++++++++++++++++-----------------
> 1 file changed, 177 insertions(+), 200 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index d161f3061e353d..55b3d5b83e5a6a 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -1,13 +1,13 @@
> // SPDX-License-Identifier: GPL-2.0
> /*
> - * Copyright 2018 Google LLC.
> + * Copyright 2020 Google LLC.
> *
> * Driver for Semtech's SX9310/SX9311 capacitive proximity/button solution.
> * Based on SX9500 driver and Semtech driver using the input framework
> * <https://my.syncplicity.com/share/teouwsim8niiaud/
> * linux-driver-SX9310_NoSmartHSensing>.
> - * Reworked April 2019 by Evan Green <[email protected]>
> - * and January 2020 by Daniel Campello <[email protected]>
> + * Reworked in April 2019 by Evan Green <[email protected]>
> + * and in January 2020 by Daniel Campello <[email protected]>.
> */
>
> #include <linux/acpi.h>
> @@ -16,7 +16,6 @@
> #include <linux/irq.h>
> #include <linux/kernel.h>
> #include <linux/module.h>
> -#include <linux/of.h>
> #include <linux/pm.h>
> #include <linux/regmap.h>
> #include <linux/slab.h>
> @@ -33,45 +32,45 @@
> #define SX9310_REG_IRQ_SRC 0x00
> #define SX9310_REG_STAT0 0x01
> #define SX9310_REG_STAT1 0x02
> +#define SX9310_REG_STAT1_COMPSTAT_MASK GENMASK(3, 0)
> #define SX9310_REG_IRQ_MSK 0x03
> #define SX9310_CONVDONE_IRQ BIT(3)
> #define SX9310_FAR_IRQ BIT(5)
> #define SX9310_CLOSE_IRQ BIT(6)
> -#define SX9310_EVENT_IRQ (SX9310_FAR_IRQ | \
> - SX9310_CLOSE_IRQ)
> #define SX9310_REG_IRQ_FUNC 0x04
>
> #define SX9310_REG_PROX_CTRL0 0x10
> -#define SX9310_REG_PROX_CTRL0_PROXSTAT2 0x10
> -#define SX9310_REG_PROX_CTRL0_EN_MASK 0x0F
> +#define SX9310_REG_PROX_CTRL0_SENSOREN_MASK GENMASK(3, 0)
> +#define SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK GENMASK(7, 4)
> +#define SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT 4
> +#define SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS 0x01
> #define SX9310_REG_PROX_CTRL1 0x11
> #define SX9310_REG_PROX_CTRL2 0x12
> -#define SX9310_REG_PROX_CTRL2_COMBMODE_ALL 0x80
> -#define SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC 0x04
> +#define SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 (0x02 << 6)
> +#define SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC (0x01 << 2)
> #define SX9310_REG_PROX_CTRL3 0x13
> -#define SX9310_REG_PROX_CTRL3_GAIN0_X8 0x0c
> +#define SX9310_REG_PROX_CTRL3_GAIN0_X8 (0x03 << 2)
> #define SX9310_REG_PROX_CTRL3_GAIN12_X4 0x02
> #define SX9310_REG_PROX_CTRL4 0x14
> #define SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST 0x07
> #define SX9310_REG_PROX_CTRL5 0x15
> -#define SX9310_REG_PROX_CTRL5_RANGE_SMALL 0xc0
> -#define SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 0x04
> +#define SX9310_REG_PROX_CTRL5_RANGE_SMALL (0x03 << 6)
> +#define SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 (0x01 << 2)
> #define SX9310_REG_PROX_CTRL5_RAWFILT_1P25 0x02
> #define SX9310_REG_PROX_CTRL6 0x16
> -#define SX9310_REG_PROX_CTRL6_COMP_COMMON 0x20
> +#define SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT 0x20
> #define SX9310_REG_PROX_CTRL7 0x17
> -#define SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 0x08
> +#define SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 (0x01 << 3)
> #define SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 0x05
> #define SX9310_REG_PROX_CTRL8 0x18
> #define SX9310_REG_PROX_CTRL9 0x19
> -#define SX9310_REG_PROX_CTRL8_9_PTHRESH12_28 0x40
> -#define SX9310_REG_PROX_CTRL8_9_PTHRESH_96 0x88
> +#define SX9310_REG_PROX_CTRL8_9_PTHRESH_28 (0x08 << 3)
> +#define SX9310_REG_PROX_CTRL8_9_PTHRESH_96 (0x11 << 3)
> #define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 0x03
> #define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 0x05
> #define SX9310_REG_PROX_CTRL10 0x1a
> -#define SX9310_REG_PROX_CTRL10_HYST_6PCT 0x10
> -#define SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8 0x12
> -#define SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8 0x03
> +#define SX9310_REG_PROX_CTRL10_HYST_6PCT (0x01 << 4)
> +#define SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 0x01
> #define SX9310_REG_PROX_CTRL11 0x1b
> #define SX9310_REG_PROX_CTRL12 0x1c
> #define SX9310_REG_PROX_CTRL13 0x1d
> @@ -82,8 +81,8 @@
> #define SX9310_REG_PROX_CTRL18 0x22
> #define SX9310_REG_PROX_CTRL19 0x23
> #define SX9310_REG_SAR_CTRL0 0x2a
> -#define SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES 0x40
> -#define SX9310_REG_SAR_CTRL0_SARHYST_8 0x10
> +#define SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES (0x02 << 5)
> +#define SX9310_REG_SAR_CTRL0_SARHYST_8 (0x02 << 3)
> #define SX9310_REG_SAR_CTRL1 0x2b
> /* Each increment of the slope register is 0.0078125. */
> #define SX9310_REG_SAR_CTRL1_SLOPE(_hnslope) (_hnslope / 78125)
> @@ -91,39 +90,27 @@
> #define SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT 0x3c
>
> #define SX9310_REG_SENSOR_SEL 0x30
> -

Prune out changes that don't really matter like these new line removals.

> #define SX9310_REG_USE_MSB 0x31
> #define SX9310_REG_USE_LSB 0x32
> -
> #define SX9310_REG_AVG_MSB 0x33
> #define SX9310_REG_AVG_LSB 0x34
> -
> #define SX9310_REG_DIFF_MSB 0x35
> #define SX9310_REG_DIFF_LSB 0x36
> -
> #define SX9310_REG_OFFSET_MSB 0x37
> #define SX9310_REG_OFFSET_LSB 0x38
> -
> #define SX9310_REG_SAR_MSB 0x39
> #define SX9310_REG_SAR_LSB 0x3a
> -
> -#define SX9310_REG_I2CADDR 0x40
> +#define SX9310_REG_I2C_ADDR 0x40
> #define SX9310_REG_PAUSE 0x41
> #define SX9310_REG_WHOAMI 0x42
> #define SX9310_WHOAMI_VALUE 0x01
> #define SX9311_WHOAMI_VALUE 0x02
> -
> #define SX9310_REG_RESET 0x7f
> #define SX9310_SOFT_RESET 0xde
>
> -#define SX9310_SCAN_PERIOD_MASK GENMASK(7, 4)
> -#define SX9310_SCAN_PERIOD_SHIFT 4
> -
> -#define SX9310_COMPSTAT_MASK GENMASK(3, 0)
>
> /* 4 hardware channels, as defined in STAT0: COMB, CS2, CS1 and CS0. */
> #define SX9310_NUM_CHANNELS 4
> -#define SX9310_CHAN_ENABLED_MASK GENMASK(3, 0)
>
> struct sx9310_data {
> /* Serialize access to registers and channel configuration */
> @@ -137,12 +124,12 @@ struct sx9310_data {
> */
> bool prox_stat[SX9310_NUM_CHANNELS];
> bool trigger_enabled;
> - __be16 buffer[SX9310_NUM_CHANNELS +
> - 4]; /* 64-bit data + 64-bit timestamp */
> + /* 64-bit data + 64-bit timestamp buffer */
> + __be16 buffer[SX9310_NUM_CHANNELS + 4];

Needs __aligned(8) to ensure that the timestamp is correctly aligned
when we call push_to_buffers. Might as well fix that whilst we are here.

> /* Remember enabled channels and sample rate during suspend. */
> unsigned int suspend_ctrl0;
> struct completion completion;
> - unsigned int chan_read, chan_event;
> + unsigned long chan_read, chan_event;
> int channel_users[SX9310_NUM_CHANNELS];
> int whoami;
> };
> @@ -251,7 +238,7 @@ static const struct regmap_range sx9310_readable_reg_ranges[] = {
> regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19),
> regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2),
> regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SAR_LSB),
> - regmap_reg_range(SX9310_REG_I2CADDR, SX9310_REG_WHOAMI),
> + regmap_reg_range(SX9310_REG_I2C_ADDR, SX9310_REG_WHOAMI),
> regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
> };
>
> @@ -285,15 +272,16 @@ static const struct regmap_config sx9310_regmap_config = {
> };
>
> static int sx9310_update_chan_en(struct sx9310_data *data,
> - unsigned int chan_read,
> - unsigned int chan_event)
> + unsigned long chan_read,
> + unsigned long chan_event)
> {
> int ret;
> + unsigned long channels = chan_read | chan_event;
>
> - if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) {
> + if ((data->chan_read | data->chan_event) != channels) {
> ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
> - SX9310_CHAN_ENABLED_MASK,
> - chan_read | chan_event);
> + SX9310_REG_PROX_CTRL0_SENSOREN_MASK,
> + channels);
> if (ret)
> return ret;
> }
> @@ -342,10 +330,10 @@ static int sx9310_read_prox_data(struct sx9310_data *data,
> int ret;
>
> ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> - return regmap_bulk_read(data->regmap, chan->address, val, 2);
> + return regmap_bulk_read(data->regmap, chan->address, val, sizeof(*val));
> }
>
> /*
> @@ -358,10 +346,11 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
> unsigned int val;
>
> ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> - val = (val & SX9310_SCAN_PERIOD_MASK) >> SX9310_SCAN_PERIOD_SHIFT;
> + val = (val & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
> + SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT;
>
> msleep(sx9310_scan_period_table[val]);
>
> @@ -371,22 +360,24 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
> static int sx9310_read_proximity(struct sx9310_data *data,
> const struct iio_chan_spec *chan, int *val)
> {
> - int ret = 0;
> + int ret;
> __be16 rawval;
>
> mutex_lock(&data->mutex);
>
> ret = sx9310_get_read_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> - ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
> - if (ret < 0)
> - goto out_put_channel;
> + if (data->client->irq) {
> + ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
> + if (ret)
> + goto out_put_channel;
> + }
>
> mutex_unlock(&data->mutex);
>
> - if (data->client->irq > 0) {
> + if (data->client->irq) {
> ret = wait_for_completion_interruptible(&data->completion);
> reinit_completion(&data->completion);
> } else {
> @@ -395,22 +386,24 @@ static int sx9310_read_proximity(struct sx9310_data *data,
>
> mutex_lock(&data->mutex);
>
> - if (ret < 0)
> + if (ret)
> goto out_disable_irq;
>
> ret = sx9310_read_prox_data(data, chan, &rawval);
> - if (ret < 0)
> + if (ret)
> goto out_disable_irq;
>
> *val = sign_extend32(be16_to_cpu(rawval),
> - (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));
> + chan->address == SX9310_REG_DIFF_MSB ? 11 : 15);
>
> - ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> - if (ret < 0)
> - goto out_put_channel;
> + if (data->client->irq) {
> + ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> + if (ret)
> + goto out_put_channel;
> + }
>
> ret = sx9310_put_read_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> mutex_unlock(&data->mutex);
> @@ -418,7 +411,8 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> return IIO_VAL_INT;
>
> out_disable_irq:
> - sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> + if (data->client->irq)
> + sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> out_put_channel:
> sx9310_put_read_channel(data, chan->channel);
> out:
> @@ -430,12 +424,14 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
> {
> unsigned int regval;
> - int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
> + int ret;
>
> - if (ret < 0)
> + ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
> + if (ret)
> return ret;
>
> - regval = (regval & SX9310_SCAN_PERIOD_MASK) >> SX9310_SCAN_PERIOD_SHIFT;
> + regval = (regval & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
> + SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT;
> *val = sx9310_samp_freq_table[regval].val;
> *val2 = sx9310_samp_freq_table[regval].val2;
>
> @@ -483,8 +479,8 @@ static int sx9310_set_samp_freq(struct sx9310_data *data, int val, int val2)
> mutex_lock(&data->mutex);
>
> ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
> - SX9310_SCAN_PERIOD_MASK,
> - i << SX9310_SCAN_PERIOD_SHIFT);
> + SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK,
> + i << SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT);
>
> mutex_unlock(&data->mutex);
>
> @@ -515,10 +511,9 @@ static irqreturn_t sx9310_irq_handler(int irq, void *private)
> iio_trigger_poll(data->trig);
>
> /*
> - * Even if no event is enabled, we need to wake the thread to
> - * clear the interrupt state by reading SX9310_REG_IRQ_SRC. It
> - * is not possible to do that here because regmap_read takes a
> - * mutex.
> + * Even if no event is enabled, we need to wake the thread to clear the
> + * interrupt state by reading SX9310_REG_IRQ_SRC.
> + * It is not possible to do that here because regmap_read takes a mutex.
> */
> return IRQ_WAKE_THREAD;
> }
> @@ -532,18 +527,18 @@ static void sx9310_push_events(struct iio_dev *indio_dev)
>
> /* Read proximity state on all channels */
> ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val);
> - if (ret < 0) {
> + if (ret) {
> dev_err(&data->client->dev, "i2c transfer error in irq\n");
> return;
> }
>
> - for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) {
> + for_each_set_bit(chan, &data->chan_event, SX9310_NUM_CHANNELS) {
> int dir;
> u64 ev;
> - bool new_prox = val & BIT(chan);
> + bool new_prox;
> +
> + new_prox = val & BIT(chan);
>
> - if (!(data->chan_event & BIT(chan)))
> - continue;
> if (new_prox == data->prox_stat[chan])
> /* No change on this channel. */
> continue;
> @@ -567,12 +562,12 @@ static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
> mutex_lock(&data->mutex);
>
> ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
> - if (ret < 0) {
> + if (ret) {
> dev_err(&data->client->dev, "i2c transfer error in irq\n");
> goto out;
> }
>
> - if (val & SX9310_EVENT_IRQ)
> + if (val & (SX9310_FAR_IRQ | SX9310_CLOSE_IRQ))
> sx9310_push_events(indio_dev);
>
> if (val & SX9310_CONVDONE_IRQ)
> @@ -600,6 +595,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
> enum iio_event_direction dir, int state)
> {
> struct sx9310_data *data = iio_priv(indio_dev);
> + unsigned int eventirq = SX9310_FAR_IRQ | SX9310_CLOSE_IRQ;
> int ret;
>
> /* If the state hasn't changed, there's nothing to do. */
> @@ -609,20 +605,20 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
> mutex_lock(&data->mutex);
> if (state) {
> ret = sx9310_get_event_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out_unlock;
> if (!(data->chan_event & ~BIT(chan->channel))) {
> - ret = sx9310_enable_irq(data, SX9310_EVENT_IRQ);
> - if (ret < 0)
> + ret = sx9310_enable_irq(data, eventirq);
> + if (ret)
> sx9310_put_event_channel(data, chan->channel);
> }
> } else {
> ret = sx9310_put_event_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out_unlock;
> if (!data->chan_event) {
> - ret = sx9310_disable_irq(data, SX9310_EVENT_IRQ);
> - if (ret < 0)
> + ret = sx9310_disable_irq(data, eventirq);
> + if (ret)
> sx9310_get_event_channel(data, chan->channel);
> }
> }
> @@ -634,7 +630,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
>
> static struct attribute *sx9310_attributes[] = {
> &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
> - NULL,
> + NULL
> };
>
> static const struct attribute_group sx9310_attribute_group = {
> @@ -661,7 +657,7 @@ static int sx9310_set_trigger_state(struct iio_trigger *trig, bool state)
> ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
> else if (!data->chan_read)
> ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> data->trigger_enabled = state;
> @@ -690,7 +686,7 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
> indio_dev->masklength) {
> ret = sx9310_read_prox_data(data, &indio_dev->channels[bit],
> &val);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> data->buffer[i++] = val;
> @@ -710,13 +706,13 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
> static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
> {
> struct sx9310_data *data = iio_priv(indio_dev);
> - unsigned int channels = 0;
> + unsigned long channels = 0;
> int bit, ret;
>
> mutex_lock(&data->mutex);
> for_each_set_bit(bit, indio_dev->active_scan_mask,
> indio_dev->masklength)
> - channels |= BIT(indio_dev->channels[bit].channel);
> + __set_bit(indio_dev->channels[bit].channel, &channels);
>
> ret = sx9310_update_chan_en(data, channels, data->chan_event);
> mutex_unlock(&data->mutex);
> @@ -746,89 +742,77 @@ struct sx9310_reg_default {
> u8 def;
> };
>
> -#define SX_INIT(_reg, _def) \
> - { \
> - .reg = SX9310_REG_##_reg, \
> - .def = _def, \
> - }
> -
> static const struct sx9310_reg_default sx9310_default_regs[] = {
> - SX_INIT(IRQ_MSK, 0x00),
> - SX_INIT(IRQ_FUNC, 0x00),
> + { SX9310_REG_IRQ_MSK, 0x00 },
> + { SX9310_REG_IRQ_FUNC, 0x00 },
> /*
> * The lower 4 bits should not be set as it enable sensors measurements.
> * Turning the detection on before the configuration values are set to
> * good values can cause the device to return erroneous readings.
> */
> - SX_INIT(PROX_CTRL0, SX9310_REG_PROX_CTRL0_PROXSTAT2),
> - SX_INIT(PROX_CTRL1, 0x00),
> - SX_INIT(PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_ALL |
> - SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC),
> - SX_INIT(PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
> - SX9310_REG_PROX_CTRL3_GAIN12_X4),
> - SX_INIT(PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST),
> - SX_INIT(PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
> - SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
> - SX9310_REG_PROX_CTRL5_RAWFILT_1P25),
> - SX_INIT(PROX_CTRL6, SX9310_REG_PROX_CTRL6_COMP_COMMON),
> - SX_INIT(PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
> - SX9310_REG_PROX_CTRL7_AVGPOSFILT_512),
> - SX_INIT(PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
> - SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500),
> - SX_INIT(PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH12_28 |
> - SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900),
> - SX_INIT(PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
> - SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8 |
> - SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8),
> - SX_INIT(PROX_CTRL11, 0x00),
> - SX_INIT(PROX_CTRL12, 0x00),
> - SX_INIT(PROX_CTRL13, 0x00),
> - SX_INIT(PROX_CTRL14, 0x00),
> - SX_INIT(PROX_CTRL15, 0x00),
> - SX_INIT(PROX_CTRL16, 0x00),
> - SX_INIT(PROX_CTRL17, 0x00),
> - SX_INIT(PROX_CTRL18, 0x00),
> - SX_INIT(PROX_CTRL19, 0x00),
> - SX_INIT(SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
> - SX9310_REG_SAR_CTRL0_SARHYST_8),
> - SX_INIT(SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250)),
> - SX_INIT(SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT),
> + { SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS },
> + { SX9310_REG_PROX_CTRL1, 0x00 },
> + { SX9310_REG_PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 |
> + SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC },
> + { SX9310_REG_PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
> + SX9310_REG_PROX_CTRL3_GAIN12_X4 },
> + { SX9310_REG_PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST },
> + { SX9310_REG_PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
> + SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
> + SX9310_REG_PROX_CTRL5_RAWFILT_1P25 },
> + { SX9310_REG_PROX_CTRL6, SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT },
> + { SX9310_REG_PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
> + SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 },
> + { SX9310_REG_PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
> + SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 },
> + { SX9310_REG_PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH_28 |
> + SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 },
> + { SX9310_REG_PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
> + SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 },
> + { SX9310_REG_PROX_CTRL11, 0x00 },
> + { SX9310_REG_PROX_CTRL12, 0x00 },
> + { SX9310_REG_PROX_CTRL13, 0x00 },
> + { SX9310_REG_PROX_CTRL14, 0x00 },
> + { SX9310_REG_PROX_CTRL15, 0x00 },
> + { SX9310_REG_PROX_CTRL16, 0x00 },
> + { SX9310_REG_PROX_CTRL17, 0x00 },
> + { SX9310_REG_PROX_CTRL18, 0x00 },
> + { SX9310_REG_PROX_CTRL19, 0x00 },
> + { SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
> + SX9310_REG_SAR_CTRL0_SARHYST_8 },
> + { SX9310_REG_SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250) },
> + { SX9310_REG_SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT },
> };
>
> /* Activate all channels and perform an initial compensation. */
> static int sx9310_init_compensation(struct iio_dev *indio_dev)
> {
> struct sx9310_data *data = iio_priv(indio_dev);
> - int i, ret;
> + int ret;
> unsigned int val;
> unsigned int ctrl0;
>
> ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> /* run the compensation phase on all channels */
> ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
> - ctrl0 | SX9310_REG_PROX_CTRL0_EN_MASK);
> - if (ret < 0)
> + ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK);
> + if (ret)
> return ret;
>
> - for (i = 100; i >= 0; i--) {
> - msleep(20);
> - ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
> - if (ret < 0)
> - goto out;
> - if (!(val & SX9310_COMPSTAT_MASK))
> - break;
> - }
> -
> - if (i < 0) {
> - dev_err(&data->client->dev,
> - "initial compensation timed out: 0x%02x", val);
> - ret = -ETIMEDOUT;
> + ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
> + !(val & SX9310_REG_STAT1_COMPSTAT_MASK),
> + 20000, 2000000);
> + if (ret) {
> + if (ret == -ETIMEDOUT)
> + dev_err(&data->client->dev,
> + "0x02 << 3l compensation timed out: 0x%02x",
> + val);
> + return ret;
> }
>
> -out:
> regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
> return ret;
> }
> @@ -841,21 +825,21 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
> unsigned int i, val;
>
> ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> usleep_range(1000, 2000); /* power-up time is ~1ms. */
>
> /* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
> ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> /* Program some sane defaults. */
> for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
> initval = &sx9310_default_regs[i];
> ret = regmap_write(data->regmap, initval->reg, initval->def);
> - if (ret < 0)
> + if (ret)
> return ret;
> }
>
> @@ -863,25 +847,15 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
> }
>
> static int sx9310_set_indio_dev_name(struct device *dev,
> - struct iio_dev *indio_dev,
> - const struct i2c_device_id *id, int whoami)
> + struct iio_dev *indio_dev, int whoami)
> {
> - const struct acpi_device_id *acpi_id;
> -
> - /* id will be NULL when enumerated via ACPI */
> - if (id) {
> - if (id->driver_data != whoami)
> - dev_err(dev, "WHOAMI does not match i2c_device_id: %s",
> - id->name);
> - } else if (ACPI_HANDLE(dev)) {
> - acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
> - if (!acpi_id)
> - return -ENODEV;
> - if (acpi_id->driver_data != whoami)
> - dev_err(dev, "WHOAMI does not match acpi_device_id: %s",
> - acpi_id->id);
> - } else
> + unsigned int long ddata;
> +
> + ddata = (uintptr_t)device_get_match_data(dev);
> + if (ddata != whoami) {
> + dev_err(dev, "WHOAMI does not match device data: %u", whoami);
> return -ENODEV;
> + }
>
> switch (whoami) {
> case SX9310_WHOAMI_VALUE:
> @@ -898,15 +872,15 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> return 0;
> }
>
> -static int sx9310_probe(struct i2c_client *client,
> - const struct i2c_device_id *id)
> +static int sx9310_probe(struct i2c_client *client)
> {
> int ret;
> + struct device *dev = &client->dev;
> struct iio_dev *indio_dev;
> struct sx9310_data *data;
>
> - indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> - if (indio_dev == NULL)
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> + if (!indio_dev)
> return -ENOMEM;
>
> data = iio_priv(indio_dev);
> @@ -919,19 +893,17 @@ static int sx9310_probe(struct i2c_client *client,
> return PTR_ERR(data->regmap);
>
> ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
> - if (ret < 0) {
> - dev_err(&client->dev, "error in reading WHOAMI register: %d",
> - ret);
> + if (ret) {
> + dev_err(dev, "error in reading WHOAMI register: %d", ret);
> return ret;
> }
>
> - ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, id,
> - data->whoami);
> - if (ret < 0)
> + ret = sx9310_set_indio_dev_name(dev, indio_dev, data->whoami);
> + if (ret)
> return ret;
>
> - ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
> - indio_dev->dev.parent = &client->dev;
> + ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(dev));
> + indio_dev->dev.parent = dev;
> indio_dev->channels = sx9310_channels;
> indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
> indio_dev->info = &sx9310_info;
> @@ -939,41 +911,41 @@ static int sx9310_probe(struct i2c_client *client,
> i2c_set_clientdata(client, indio_dev);
>
> ret = sx9310_init_device(indio_dev);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> if (client->irq) {
> - ret = devm_request_threaded_irq(&client->dev, client->irq,
> + ret = devm_request_threaded_irq(dev, client->irq,
> sx9310_irq_handler,
> sx9310_irq_thread_handler,
> IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> "sx9310_event", indio_dev);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> - data->trig =
> - devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
> - indio_dev->name, indio_dev->id);
> + data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
> + indio_dev->name,
> + indio_dev->id);
> if (!data->trig)
> return -ENOMEM;
>
> - data->trig->dev.parent = &client->dev;
> + data->trig->dev.parent = dev;
> data->trig->ops = &sx9310_trigger_ops;
> iio_trigger_set_drvdata(data->trig, indio_dev);
>
> - ret = devm_iio_trigger_register(&client->dev, data->trig);
> + ret = devm_iio_trigger_register(dev, data->trig);
> if (ret)
> return ret;
> }
>
> - ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
> + ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
> iio_pollfunc_store_time,
> sx9310_trigger_handler,
> &sx9310_buffer_setup_ops);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> - return devm_iio_device_register(&client->dev, indio_dev);
> + return devm_iio_device_register(dev, indio_dev);
> }
>
> static int __maybe_unused sx9310_suspend(struct device *dev)
> @@ -988,11 +960,10 @@ static int __maybe_unused sx9310_suspend(struct device *dev)
> mutex_lock(&data->mutex);
> ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
> &data->suspend_ctrl0);
> -
> if (ret)
> goto out;
>
> - ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_EN_MASK;
> + ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK;
> ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
> if (ret)
> goto out;
> @@ -1017,12 +988,18 @@ static int __maybe_unused sx9310_resume(struct device *dev)
>
> ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
> data->suspend_ctrl0);
> + if (ret)
> + goto out;
>
> -out:
> mutex_unlock(&data->mutex);
>
> enable_irq(data->client->irq);
>
> + return 0;
> +
> +out:
> + mutex_unlock(&data->mutex);
> +
> return ret;
> }
>
> @@ -1033,32 +1010,32 @@ static const struct dev_pm_ops sx9310_pm_ops = {
> static const struct acpi_device_id sx9310_acpi_match[] = {
> { "STH9310", SX9310_WHOAMI_VALUE },
> { "STH9311", SX9311_WHOAMI_VALUE },
> - {},
> + {}
> };
> MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);
>
> static const struct of_device_id sx9310_of_match[] = {
> { .compatible = "semtech,sx9310" },
> { .compatible = "semtech,sx9311" },
> - {},
> + {}
> };
> MODULE_DEVICE_TABLE(of, sx9310_of_match);
>
> static const struct i2c_device_id sx9310_id[] = {
> { "sx9310", SX9310_WHOAMI_VALUE },
> { "sx9311", SX9311_WHOAMI_VALUE },
> - {},
> + {}
> };
> MODULE_DEVICE_TABLE(i2c, sx9310_id);
>
> static struct i2c_driver sx9310_driver = {
> .driver = {
> .name = "sx9310",
> - .acpi_match_table = ACPI_PTR(sx9310_acpi_match),
> - .of_match_table = of_match_ptr(sx9310_of_match),
> + .acpi_match_table = sx9310_acpi_match,
> + .of_match_table = sx9310_of_match,
> .pm = &sx9310_pm_ops,
> },
> - .probe = sx9310_probe,
> + .probe_new = sx9310_probe,
> .id_table = sx9310_id,
> };
> module_i2c_driver(sx9310_driver);
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>

2020-07-28 15:13:57

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 00/15] sx9310 iio driver updates

The first patch resends the DT binding for the driver that was merged in
v5.8-rc1 with a small change to update for proper regulators. The second
through the eleventh patch fixes several issues dropped from v8 to v9
when the initial patch was merged. The twelveth patch fixes a few
printks that are missing newlines and should be totally non-trivial to
apply. The thirteenth patch drops channel_users because it's unused. The
final patch adds support to enable the svdd and vdd supplies so that
this driver can work on a board where the svdd supply isn't enabled at
boot and needs to be turned on before this driver starts to communicate
with the chip.


Daniel Campello (12):
dt-bindings: iio: Add bindings for sx9310 sensor
iio: sx9310: Update macros declarations
iio: sx9310: Fix irq handling
iio: sx9310: Remove acpi and of table macros
iio: sx9310: Change from .probe to .probe_new
iio: sx9310: Align memory
iio: sx9310: Use long instead of int for channel bitmaps
iio: sx9310: Use regmap_read_poll_timeout() for compensation
iio: sx9310: Update copyright
iio: sx9310: Simplify error return handling
iio: sx9310: Use variable to hold &client->dev
iio: sx9310: Miscellaneous format fixes

Stephen Boyd (3):
iio: sx9310: Add newlines to printks
iio: sx9310: Drop channel_users[]
iio: sx9310: Enable vdd and svdd regulators at probe

.../iio/proximity/semtech,sx9310.yaml | 60 +++
drivers/iio/proximity/sx9310.c | 407 +++++++++---------
2 files changed, 263 insertions(+), 204 deletions(-)
create mode 100644 Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml

--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:14:00

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 01/15] dt-bindings: iio: Add bindings for sx9310 sensor

Adds device tree bandings for sx9310 sensor.

Signed-off-by: Daniel Campello <[email protected]>
Cc: Hartmut Knaack <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: Peter Meerwald-Stadler <[email protected]>
Cc: Rob Herring <[email protected]>
Reviewed-by: Douglas Anderson <[email protected]>
[[email protected]: Add both regulators and make them optional]
Signed-off-by: Stephen Boyd <[email protected]>
---

.../iio/proximity/semtech,sx9310.yaml | 60 +++++++++++++++++++
1 file changed, 60 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml

diff --git a/Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml b/Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml
new file mode 100644
index 00000000000000..ba734ee868c77f
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml
@@ -0,0 +1,60 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/proximity/semtech,sx9310.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Semtech's SX9310 capacitive proximity sensor
+
+maintainers:
+ - Daniel Campello <[email protected]>
+
+description: |
+ Semtech's SX9310/SX9311 capacitive proximity/button solution.
+
+ Specifications about the devices can be found at:
+ https://www.semtech.com/products/smart-sensing/sar-sensors/sx9310
+
+properties:
+ compatible:
+ enum:
+ - semtech,sx9310
+ - semtech,sx9311
+
+ reg:
+ maxItems: 1
+
+ interrupts:
+ description:
+ The sole interrupt generated by the device used to announce the
+ preceding reading request has finished and that data is
+ available or that a close/far proximity event has happened.
+ maxItems: 1
+
+ vdd-supply:
+ description: Main power supply
+
+ svdd-supply:
+ description: Host interface power supply
+
+required:
+ - compatible
+ - reg
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/interrupt-controller/irq.h>
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ proximity@28 {
+ compatible = "semtech,sx9310";
+ reg = <0x28>;
+ interrupt-parent = <&pio>;
+ interrupts = <5 IRQ_TYPE_LEVEL_LOW 5>;
+ vdd-supply = <&pp3300_a>;
+ svdd-supply = <&pp1800_prox>;
+ };
+ };
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:14:05

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 02/15] iio: sx9310: Update macros declarations

Follows spec sheet for macro declarations.

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 143 +++++++++++++++------------------
1 file changed, 67 insertions(+), 76 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index d161f3061e353d..07895d4b935d12 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -33,45 +33,45 @@
#define SX9310_REG_IRQ_SRC 0x00
#define SX9310_REG_STAT0 0x01
#define SX9310_REG_STAT1 0x02
+#define SX9310_REG_STAT1_COMPSTAT_MASK GENMASK(3, 0)
#define SX9310_REG_IRQ_MSK 0x03
#define SX9310_CONVDONE_IRQ BIT(3)
#define SX9310_FAR_IRQ BIT(5)
#define SX9310_CLOSE_IRQ BIT(6)
-#define SX9310_EVENT_IRQ (SX9310_FAR_IRQ | \
- SX9310_CLOSE_IRQ)
#define SX9310_REG_IRQ_FUNC 0x04

#define SX9310_REG_PROX_CTRL0 0x10
-#define SX9310_REG_PROX_CTRL0_PROXSTAT2 0x10
-#define SX9310_REG_PROX_CTRL0_EN_MASK 0x0F
+#define SX9310_REG_PROX_CTRL0_SENSOREN_MASK GENMASK(3, 0)
+#define SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK GENMASK(7, 4)
+#define SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT 4
+#define SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS 0x01
#define SX9310_REG_PROX_CTRL1 0x11
#define SX9310_REG_PROX_CTRL2 0x12
-#define SX9310_REG_PROX_CTRL2_COMBMODE_ALL 0x80
-#define SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC 0x04
+#define SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 (0x02 << 6)
+#define SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC (0x01 << 2)
#define SX9310_REG_PROX_CTRL3 0x13
-#define SX9310_REG_PROX_CTRL3_GAIN0_X8 0x0c
+#define SX9310_REG_PROX_CTRL3_GAIN0_X8 (0x03 << 2)
#define SX9310_REG_PROX_CTRL3_GAIN12_X4 0x02
#define SX9310_REG_PROX_CTRL4 0x14
#define SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST 0x07
#define SX9310_REG_PROX_CTRL5 0x15
-#define SX9310_REG_PROX_CTRL5_RANGE_SMALL 0xc0
-#define SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 0x04
+#define SX9310_REG_PROX_CTRL5_RANGE_SMALL (0x03 << 6)
+#define SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 (0x01 << 2)
#define SX9310_REG_PROX_CTRL5_RAWFILT_1P25 0x02
#define SX9310_REG_PROX_CTRL6 0x16
-#define SX9310_REG_PROX_CTRL6_COMP_COMMON 0x20
+#define SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT 0x20
#define SX9310_REG_PROX_CTRL7 0x17
-#define SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 0x08
+#define SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 (0x01 << 3)
#define SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 0x05
#define SX9310_REG_PROX_CTRL8 0x18
#define SX9310_REG_PROX_CTRL9 0x19
-#define SX9310_REG_PROX_CTRL8_9_PTHRESH12_28 0x40
-#define SX9310_REG_PROX_CTRL8_9_PTHRESH_96 0x88
+#define SX9310_REG_PROX_CTRL8_9_PTHRESH_28 (0x08 << 3)
+#define SX9310_REG_PROX_CTRL8_9_PTHRESH_96 (0x11 << 3)
#define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 0x03
#define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 0x05
#define SX9310_REG_PROX_CTRL10 0x1a
-#define SX9310_REG_PROX_CTRL10_HYST_6PCT 0x10
-#define SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8 0x12
-#define SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8 0x03
+#define SX9310_REG_PROX_CTRL10_HYST_6PCT (0x01 << 4)
+#define SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 0x01
#define SX9310_REG_PROX_CTRL11 0x1b
#define SX9310_REG_PROX_CTRL12 0x1c
#define SX9310_REG_PROX_CTRL13 0x1d
@@ -82,8 +82,8 @@
#define SX9310_REG_PROX_CTRL18 0x22
#define SX9310_REG_PROX_CTRL19 0x23
#define SX9310_REG_SAR_CTRL0 0x2a
-#define SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES 0x40
-#define SX9310_REG_SAR_CTRL0_SARHYST_8 0x10
+#define SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES (0x02 << 5)
+#define SX9310_REG_SAR_CTRL0_SARHYST_8 (0x02 << 3)
#define SX9310_REG_SAR_CTRL1 0x2b
/* Each increment of the slope register is 0.0078125. */
#define SX9310_REG_SAR_CTRL1_SLOPE(_hnslope) (_hnslope / 78125)
@@ -107,7 +107,7 @@
#define SX9310_REG_SAR_MSB 0x39
#define SX9310_REG_SAR_LSB 0x3a

-#define SX9310_REG_I2CADDR 0x40
+#define SX9310_REG_I2C_ADDR 0x40
#define SX9310_REG_PAUSE 0x41
#define SX9310_REG_WHOAMI 0x42
#define SX9310_WHOAMI_VALUE 0x01
@@ -116,14 +116,9 @@
#define SX9310_REG_RESET 0x7f
#define SX9310_SOFT_RESET 0xde

-#define SX9310_SCAN_PERIOD_MASK GENMASK(7, 4)
-#define SX9310_SCAN_PERIOD_SHIFT 4
-
-#define SX9310_COMPSTAT_MASK GENMASK(3, 0)

/* 4 hardware channels, as defined in STAT0: COMB, CS2, CS1 and CS0. */
#define SX9310_NUM_CHANNELS 4
-#define SX9310_CHAN_ENABLED_MASK GENMASK(3, 0)

struct sx9310_data {
/* Serialize access to registers and channel configuration */
@@ -251,7 +246,7 @@ static const struct regmap_range sx9310_readable_reg_ranges[] = {
regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19),
regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2),
regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SAR_LSB),
- regmap_reg_range(SX9310_REG_I2CADDR, SX9310_REG_WHOAMI),
+ regmap_reg_range(SX9310_REG_I2C_ADDR, SX9310_REG_WHOAMI),
regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
};

@@ -292,7 +287,7 @@ static int sx9310_update_chan_en(struct sx9310_data *data,

if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) {
ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
- SX9310_CHAN_ENABLED_MASK,
+ SX9310_REG_PROX_CTRL0_SENSOREN_MASK,
chan_read | chan_event);
if (ret)
return ret;
@@ -361,7 +356,8 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
if (ret < 0)
return ret;

- val = (val & SX9310_SCAN_PERIOD_MASK) >> SX9310_SCAN_PERIOD_SHIFT;
+ val = (val & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
+ SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT;

msleep(sx9310_scan_period_table[val]);

@@ -435,7 +431,8 @@ static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
if (ret < 0)
return ret;

- regval = (regval & SX9310_SCAN_PERIOD_MASK) >> SX9310_SCAN_PERIOD_SHIFT;
+ regval = (regval & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
+ SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT;
*val = sx9310_samp_freq_table[regval].val;
*val2 = sx9310_samp_freq_table[regval].val2;

@@ -483,8 +480,8 @@ static int sx9310_set_samp_freq(struct sx9310_data *data, int val, int val2)
mutex_lock(&data->mutex);

ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
- SX9310_SCAN_PERIOD_MASK,
- i << SX9310_SCAN_PERIOD_SHIFT);
+ SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK,
+ i << SX9310_REG_PROX_CTRL0_SCANPERIOD_SHIFT);

mutex_unlock(&data->mutex);

@@ -572,7 +569,7 @@ static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
goto out;
}

- if (val & SX9310_EVENT_IRQ)
+ if (val & (SX9310_FAR_IRQ | SX9310_CLOSE_IRQ))
sx9310_push_events(indio_dev);

if (val & SX9310_CONVDONE_IRQ)
@@ -600,6 +597,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
enum iio_event_direction dir, int state)
{
struct sx9310_data *data = iio_priv(indio_dev);
+ unsigned int eventirq = SX9310_FAR_IRQ | SX9310_CLOSE_IRQ;
int ret;

/* If the state hasn't changed, there's nothing to do. */
@@ -612,7 +610,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
if (ret < 0)
goto out_unlock;
if (!(data->chan_event & ~BIT(chan->channel))) {
- ret = sx9310_enable_irq(data, SX9310_EVENT_IRQ);
+ ret = sx9310_enable_irq(data, eventirq);
if (ret < 0)
sx9310_put_event_channel(data, chan->channel);
}
@@ -621,7 +619,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
if (ret < 0)
goto out_unlock;
if (!data->chan_event) {
- ret = sx9310_disable_irq(data, SX9310_EVENT_IRQ);
+ ret = sx9310_disable_irq(data, eventirq);
if (ret < 0)
sx9310_get_event_channel(data, chan->channel);
}
@@ -746,53 +744,46 @@ struct sx9310_reg_default {
u8 def;
};

-#define SX_INIT(_reg, _def) \
- { \
- .reg = SX9310_REG_##_reg, \
- .def = _def, \
- }
-
static const struct sx9310_reg_default sx9310_default_regs[] = {
- SX_INIT(IRQ_MSK, 0x00),
- SX_INIT(IRQ_FUNC, 0x00),
+ { SX9310_REG_IRQ_MSK, 0x00 },
+ { SX9310_REG_IRQ_FUNC, 0x00 },
/*
* The lower 4 bits should not be set as it enable sensors measurements.
* Turning the detection on before the configuration values are set to
* good values can cause the device to return erroneous readings.
*/
- SX_INIT(PROX_CTRL0, SX9310_REG_PROX_CTRL0_PROXSTAT2),
- SX_INIT(PROX_CTRL1, 0x00),
- SX_INIT(PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_ALL |
- SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC),
- SX_INIT(PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
- SX9310_REG_PROX_CTRL3_GAIN12_X4),
- SX_INIT(PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST),
- SX_INIT(PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
- SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
- SX9310_REG_PROX_CTRL5_RAWFILT_1P25),
- SX_INIT(PROX_CTRL6, SX9310_REG_PROX_CTRL6_COMP_COMMON),
- SX_INIT(PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
- SX9310_REG_PROX_CTRL7_AVGPOSFILT_512),
- SX_INIT(PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
- SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500),
- SX_INIT(PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH12_28 |
- SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900),
- SX_INIT(PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
- SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8 |
- SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8),
- SX_INIT(PROX_CTRL11, 0x00),
- SX_INIT(PROX_CTRL12, 0x00),
- SX_INIT(PROX_CTRL13, 0x00),
- SX_INIT(PROX_CTRL14, 0x00),
- SX_INIT(PROX_CTRL15, 0x00),
- SX_INIT(PROX_CTRL16, 0x00),
- SX_INIT(PROX_CTRL17, 0x00),
- SX_INIT(PROX_CTRL18, 0x00),
- SX_INIT(PROX_CTRL19, 0x00),
- SX_INIT(SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
- SX9310_REG_SAR_CTRL0_SARHYST_8),
- SX_INIT(SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250)),
- SX_INIT(SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT),
+ { SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS },
+ { SX9310_REG_PROX_CTRL1, 0x00 },
+ { SX9310_REG_PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 |
+ SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC },
+ { SX9310_REG_PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
+ SX9310_REG_PROX_CTRL3_GAIN12_X4 },
+ { SX9310_REG_PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST },
+ { SX9310_REG_PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
+ SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
+ SX9310_REG_PROX_CTRL5_RAWFILT_1P25 },
+ { SX9310_REG_PROX_CTRL6, SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT },
+ { SX9310_REG_PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
+ SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 },
+ { SX9310_REG_PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
+ SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 },
+ { SX9310_REG_PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH_28 |
+ SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 },
+ { SX9310_REG_PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
+ SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 },
+ { SX9310_REG_PROX_CTRL11, 0x00 },
+ { SX9310_REG_PROX_CTRL12, 0x00 },
+ { SX9310_REG_PROX_CTRL13, 0x00 },
+ { SX9310_REG_PROX_CTRL14, 0x00 },
+ { SX9310_REG_PROX_CTRL15, 0x00 },
+ { SX9310_REG_PROX_CTRL16, 0x00 },
+ { SX9310_REG_PROX_CTRL17, 0x00 },
+ { SX9310_REG_PROX_CTRL18, 0x00 },
+ { SX9310_REG_PROX_CTRL19, 0x00 },
+ { SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
+ SX9310_REG_SAR_CTRL0_SARHYST_8 },
+ { SX9310_REG_SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250) },
+ { SX9310_REG_SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT },
};

/* Activate all channels and perform an initial compensation. */
@@ -809,7 +800,7 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)

/* run the compensation phase on all channels */
ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
- ctrl0 | SX9310_REG_PROX_CTRL0_EN_MASK);
+ ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK);
if (ret < 0)
return ret;

@@ -992,7 +983,7 @@ static int __maybe_unused sx9310_suspend(struct device *dev)
if (ret)
goto out;

- ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_EN_MASK;
+ ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK;
ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
if (ret)
goto out;
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:14:09

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 06/15] iio: sx9310: Align memory

Use __aligned(8) to ensure that the timestamp is correctly aligned
when we call push_to_buffers

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index de52afd7c13333..fb5c16f2aa6b1a 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -131,8 +131,8 @@ struct sx9310_data {
*/
bool prox_stat[SX9310_NUM_CHANNELS];
bool trigger_enabled;
- __be16 buffer[SX9310_NUM_CHANNELS +
- 4]; /* 64-bit data + 64-bit timestamp */
+ /* 64-bit data + 64-bit timestamp buffer */
+ __be16 buffer[SX9310_NUM_CHANNELS + 4] __aligned(8);
/* Remember enabled channels and sample rate during suspend. */
unsigned int suspend_ctrl0;
struct completion completion;
@@ -339,7 +339,7 @@ static int sx9310_read_prox_data(struct sx9310_data *data,
if (ret < 0)
return ret;

- return regmap_bulk_read(data->regmap, chan->address, val, 2);
+ return regmap_bulk_read(data->regmap, chan->address, val, sizeof(*val));
}

/*
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:14:18

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 14/15] iio: sx9310: Drop channel_users[]

From: Stephen Boyd <[email protected]>

This struct member isn't used. Drop it.

Fixes: 72ad02b15d63 ("iio: Add SEMTECH SX9310/9311 sensor driver")
Signed-off-by: Stephen Boyd <[email protected]>
Reviewed-by: Douglas Anderson <[email protected]>
Reviewed-by: Daniel Campello <[email protected]>
Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 1 -
1 file changed, 1 deletion(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 4553ee83a016a3..202018b726e32f 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -131,7 +131,6 @@ struct sx9310_data {
struct completion completion;
unsigned long chan_read;
unsigned long chan_event;
- int channel_users[SX9310_NUM_CHANNELS];
unsigned int whoami;
};

--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:14:23

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 12/15] iio: sx9310: Miscellaneous format fixes

Miscellaneous format fixes throughout the whole file.

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 28 ++++++++++------------------
1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index ba383710ef0dcf..3f981d28ee4056 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -90,28 +90,21 @@
#define SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT 0x3c

#define SX9310_REG_SENSOR_SEL 0x30
-
#define SX9310_REG_USE_MSB 0x31
#define SX9310_REG_USE_LSB 0x32
-
#define SX9310_REG_AVG_MSB 0x33
#define SX9310_REG_AVG_LSB 0x34
-
#define SX9310_REG_DIFF_MSB 0x35
#define SX9310_REG_DIFF_LSB 0x36
-
#define SX9310_REG_OFFSET_MSB 0x37
#define SX9310_REG_OFFSET_LSB 0x38
-
#define SX9310_REG_SAR_MSB 0x39
#define SX9310_REG_SAR_LSB 0x3a
-
#define SX9310_REG_I2C_ADDR 0x40
#define SX9310_REG_PAUSE 0x41
#define SX9310_REG_WHOAMI 0x42
#define SX9310_WHOAMI_VALUE 0x01
#define SX9311_WHOAMI_VALUE 0x02
-
#define SX9310_REG_RESET 0x7f
#define SX9310_SOFT_RESET 0xde

@@ -402,7 +395,7 @@ static int sx9310_read_proximity(struct sx9310_data *data,
goto out_disable_irq;

*val = sign_extend32(be16_to_cpu(rawval),
- (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));
+ chan->address == SX9310_REG_DIFF_MSB ? 11 : 15);

if (data->client->irq) {
ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
@@ -432,8 +425,9 @@ static int sx9310_read_proximity(struct sx9310_data *data,
static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
{
unsigned int regval;
- int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
+ int ret;

+ ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
if (ret)
return ret;

@@ -518,10 +512,9 @@ static irqreturn_t sx9310_irq_handler(int irq, void *private)
iio_trigger_poll(data->trig);

/*
- * Even if no event is enabled, we need to wake the thread to
- * clear the interrupt state by reading SX9310_REG_IRQ_SRC. It
- * is not possible to do that here because regmap_read takes a
- * mutex.
+ * Even if no event is enabled, we need to wake the thread to clear the
+ * interrupt state by reading SX9310_REG_IRQ_SRC.
+ * It is not possible to do that here because regmap_read takes a mutex.
*/
return IRQ_WAKE_THREAD;
}
@@ -638,7 +631,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,

static struct attribute *sx9310_attributes[] = {
&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
- NULL,
+ NULL
};

static const struct attribute_group sx9310_attribute_group = {
@@ -969,7 +962,6 @@ static int __maybe_unused sx9310_suspend(struct device *dev)
mutex_lock(&data->mutex);
ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
&data->suspend_ctrl0);
-
if (ret)
goto out;

@@ -1015,21 +1007,21 @@ static const struct dev_pm_ops sx9310_pm_ops = {
static const struct acpi_device_id sx9310_acpi_match[] = {
{ "STH9310", SX9310_WHOAMI_VALUE },
{ "STH9311", SX9311_WHOAMI_VALUE },
- {},
+ {}
};
MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);

static const struct of_device_id sx9310_of_match[] = {
{ .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE },
{ .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE },
- {},
+ {}
};
MODULE_DEVICE_TABLE(of, sx9310_of_match);

static const struct i2c_device_id sx9310_id[] = {
{ "sx9310", SX9310_WHOAMI_VALUE },
{ "sx9311", SX9311_WHOAMI_VALUE },
- {},
+ {}
};
MODULE_DEVICE_TABLE(i2c, sx9310_id);

--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:14:33

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 13/15] iio: sx9310: Add newlines to printks

From: Stephen Boyd <[email protected]>

Printks in the kernel have newlines at the end. Add them to the few
printks in this driver.

Reviewed-by: Daniel Campello <[email protected]>
Reviewed-by: Douglas Anderson <[email protected]>
Fixes: 72ad02b15d63 ("iio: Add SEMTECH SX9310/9311 sensor driver")
Signed-off-by: Stephen Boyd <[email protected]>
Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 3f981d28ee4056..4553ee83a016a3 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -809,7 +809,7 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
if (ret) {
if (ret == -ETIMEDOUT)
dev_err(&data->client->dev,
- "0x02 << 3l compensation timed out: 0x%02x",
+ "0x02 << 3l compensation timed out: 0x%02x\n",
val);
return ret;
}
@@ -855,7 +855,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,

ddata = (uintptr_t)device_get_match_data(dev);
if (ddata != whoami) {
- dev_err(dev, "WHOAMI does not match device data: %u", whoami);
+ dev_err(dev, "WHOAMI does not match device data: %u\n", whoami);
return -ENODEV;
}

@@ -867,7 +867,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
indio_dev->name = "sx9311";
break;
default:
- dev_err(dev, "unexpected WHOAMI response: %u", whoami);
+ dev_err(dev, "unexpected WHOAMI response: %u\n", whoami);
return -ENODEV;
}

@@ -896,7 +896,7 @@ static int sx9310_probe(struct i2c_client *client)

ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
if (ret) {
- dev_err(dev, "error in reading WHOAMI register: %d", ret);
+ dev_err(dev, "error in reading WHOAMI register: %d\n", ret);
return ret;
}

--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:14:46

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 11/15] iio: sx9310: Use variable to hold &client->dev

Improves readability by storing &client->dev in a local variable.

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 051c6515e62c18..ba383710ef0dcf 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -884,11 +884,12 @@ static int sx9310_set_indio_dev_name(struct device *dev,
static int sx9310_probe(struct i2c_client *client)
{
int ret;
+ struct device *dev = &client->dev;
struct iio_dev *indio_dev;
struct sx9310_data *data;

- indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
- if (indio_dev == NULL)
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+ if (!indio_dev)
return -ENOMEM;

data = iio_priv(indio_dev);
@@ -902,17 +903,16 @@ static int sx9310_probe(struct i2c_client *client)

ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
if (ret) {
- dev_err(&client->dev, "error in reading WHOAMI register: %d",
- ret);
+ dev_err(dev, "error in reading WHOAMI register: %d", ret);
return ret;
}

- ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami);
+ ret = sx9310_set_indio_dev_name(dev, indio_dev, data->whoami);
if (ret)
return ret;

- ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
- indio_dev->dev.parent = &client->dev;
+ ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(dev));
+ indio_dev->dev.parent = dev;
indio_dev->channels = sx9310_channels;
indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
indio_dev->info = &sx9310_info;
@@ -924,7 +924,7 @@ static int sx9310_probe(struct i2c_client *client)
return ret;

if (client->irq) {
- ret = devm_request_threaded_irq(&client->dev, client->irq,
+ ret = devm_request_threaded_irq(dev, client->irq,
sx9310_irq_handler,
sx9310_irq_thread_handler,
IRQF_TRIGGER_LOW | IRQF_ONESHOT,
@@ -932,29 +932,29 @@ static int sx9310_probe(struct i2c_client *client)
if (ret)
return ret;

- data->trig =
- devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
- indio_dev->name, indio_dev->id);
+ data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
+ indio_dev->name,
+ indio_dev->id);
if (!data->trig)
return -ENOMEM;

- data->trig->dev.parent = &client->dev;
+ data->trig->dev.parent = dev;
data->trig->ops = &sx9310_trigger_ops;
iio_trigger_set_drvdata(data->trig, indio_dev);

- ret = devm_iio_trigger_register(&client->dev, data->trig);
+ ret = devm_iio_trigger_register(dev, data->trig);
if (ret)
return ret;
}

- ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
+ ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
iio_pollfunc_store_time,
sx9310_trigger_handler,
&sx9310_buffer_setup_ops);
if (ret)
return ret;

- return devm_iio_device_register(&client->dev, indio_dev);
+ return devm_iio_device_register(dev, indio_dev);
}

static int __maybe_unused sx9310_suspend(struct device *dev)
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:14:58

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 10/15] iio: sx9310: Simplify error return handling

Checks for non-zero return values to signal error conditions.

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 52 +++++++++++++++++-----------------
1 file changed, 26 insertions(+), 26 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 31234691a31abf..051c6515e62c18 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -338,7 +338,7 @@ static int sx9310_read_prox_data(struct sx9310_data *data,
int ret;

ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel);
- if (ret < 0)
+ if (ret)
return ret;

return regmap_bulk_read(data->regmap, chan->address, val, sizeof(*val));
@@ -354,7 +354,7 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
unsigned int val;

ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val);
- if (ret < 0)
+ if (ret)
return ret;

val = (val & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
@@ -368,13 +368,13 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
static int sx9310_read_proximity(struct sx9310_data *data,
const struct iio_chan_spec *chan, int *val)
{
- int ret = 0;
+ int ret;
__be16 rawval;

mutex_lock(&data->mutex);

ret = sx9310_get_read_channel(data, chan->channel);
- if (ret < 0)
+ if (ret)
goto out;

if (data->client->irq) {
@@ -394,11 +394,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,

mutex_lock(&data->mutex);

- if (ret < 0)
+ if (ret)
goto out_disable_irq;

ret = sx9310_read_prox_data(data, chan, &rawval);
- if (ret < 0)
+ if (ret)
goto out_disable_irq;

*val = sign_extend32(be16_to_cpu(rawval),
@@ -411,7 +411,7 @@ static int sx9310_read_proximity(struct sx9310_data *data,
}

ret = sx9310_put_read_channel(data, chan->channel);
- if (ret < 0)
+ if (ret)
goto out;

mutex_unlock(&data->mutex);
@@ -434,7 +434,7 @@ static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
unsigned int regval;
int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);

- if (ret < 0)
+ if (ret)
return ret;

regval = (regval & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
@@ -535,7 +535,7 @@ static void sx9310_push_events(struct iio_dev *indio_dev)

/* Read proximity state on all channels */
ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val);
- if (ret < 0) {
+ if (ret) {
dev_err(&data->client->dev, "i2c transfer error in irq\n");
return;
}
@@ -570,7 +570,7 @@ static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
mutex_lock(&data->mutex);

ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
- if (ret < 0) {
+ if (ret) {
dev_err(&data->client->dev, "i2c transfer error in irq\n");
goto out;
}
@@ -613,20 +613,20 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
mutex_lock(&data->mutex);
if (state) {
ret = sx9310_get_event_channel(data, chan->channel);
- if (ret < 0)
+ if (ret)
goto out_unlock;
if (!(data->chan_event & ~BIT(chan->channel))) {
ret = sx9310_enable_irq(data, eventirq);
- if (ret < 0)
+ if (ret)
sx9310_put_event_channel(data, chan->channel);
}
} else {
ret = sx9310_put_event_channel(data, chan->channel);
- if (ret < 0)
+ if (ret)
goto out_unlock;
if (!data->chan_event) {
ret = sx9310_disable_irq(data, eventirq);
- if (ret < 0)
+ if (ret)
sx9310_get_event_channel(data, chan->channel);
}
}
@@ -665,7 +665,7 @@ static int sx9310_set_trigger_state(struct iio_trigger *trig, bool state)
ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
else if (!data->chan_read)
ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
- if (ret < 0)
+ if (ret)
goto out;

data->trigger_enabled = state;
@@ -694,7 +694,7 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
indio_dev->masklength) {
ret = sx9310_read_prox_data(data, &indio_dev->channels[bit],
&val);
- if (ret < 0)
+ if (ret)
goto out;

data->buffer[i++] = val;
@@ -801,13 +801,13 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
unsigned int ctrl0;

ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0);
- if (ret < 0)
+ if (ret)
return ret;

/* run the compensation phase on all channels */
ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK);
- if (ret < 0)
+ if (ret)
return ret;

ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
@@ -833,21 +833,21 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
unsigned int i, val;

ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET);
- if (ret < 0)
+ if (ret)
return ret;

usleep_range(1000, 2000); /* power-up time is ~1ms. */

/* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
- if (ret < 0)
+ if (ret)
return ret;

/* Program some sane defaults. */
for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
initval = &sx9310_default_regs[i];
ret = regmap_write(data->regmap, initval->reg, initval->def);
- if (ret < 0)
+ if (ret)
return ret;
}

@@ -901,14 +901,14 @@ static int sx9310_probe(struct i2c_client *client)
return PTR_ERR(data->regmap);

ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
- if (ret < 0) {
+ if (ret) {
dev_err(&client->dev, "error in reading WHOAMI register: %d",
ret);
return ret;
}

ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami);
- if (ret < 0)
+ if (ret)
return ret;

ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
@@ -920,7 +920,7 @@ static int sx9310_probe(struct i2c_client *client)
i2c_set_clientdata(client, indio_dev);

ret = sx9310_init_device(indio_dev);
- if (ret < 0)
+ if (ret)
return ret;

if (client->irq) {
@@ -929,7 +929,7 @@ static int sx9310_probe(struct i2c_client *client)
sx9310_irq_thread_handler,
IRQF_TRIGGER_LOW | IRQF_ONESHOT,
"sx9310_event", indio_dev);
- if (ret < 0)
+ if (ret)
return ret;

data->trig =
@@ -951,7 +951,7 @@ static int sx9310_probe(struct i2c_client *client)
iio_pollfunc_store_time,
sx9310_trigger_handler,
&sx9310_buffer_setup_ops);
- if (ret < 0)
+ if (ret)
return ret;

return devm_iio_device_register(&client->dev, indio_dev);
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:15:02

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 15/15] iio: sx9310: Enable vdd and svdd regulators at probe

From: Stephen Boyd <[email protected]>

Enable the main power supply (vdd) and digital IO power supply (svdd)
during probe so that the i2c communication and device works properly on
boards that aggressively power gate these supplies.

Reviewed-by: Douglas Anderson <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 202018b726e32f..24a2360b6314ef 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -18,6 +18,7 @@
#include <linux/module.h>
#include <linux/pm.h>
#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
#include <linux/slab.h>

#include <linux/iio/buffer.h>
@@ -118,6 +119,7 @@ struct sx9310_data {
struct i2c_client *client;
struct iio_trigger *trig;
struct regmap *regmap;
+ struct regulator_bulk_data supplies[2];
/*
* Last reading of the proximity status for each channel.
* We only send an event to user space when this changes.
@@ -873,6 +875,13 @@ static int sx9310_set_indio_dev_name(struct device *dev,
return 0;
}

+static void sx9310_regulator_disable(void *_data)
+{
+ struct sx9310_data *data = _data;
+
+ regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
+}
+
static int sx9310_probe(struct i2c_client *client)
{
int ret;
@@ -886,6 +895,8 @@ static int sx9310_probe(struct i2c_client *client)

data = iio_priv(indio_dev);
data->client = client;
+ data->supplies[0].supply = "vdd";
+ data->supplies[1].supply = "svdd";
mutex_init(&data->mutex);
init_completion(&data->completion);

@@ -893,6 +904,21 @@ static int sx9310_probe(struct i2c_client *client)
if (IS_ERR(data->regmap))
return PTR_ERR(data->regmap);

+ ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
+ data->supplies);
+ if (ret)
+ return ret;
+
+ ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies);
+ if (ret)
+ return ret;
+ /* Must wait for Tpor time after initial power up */
+ usleep_range(1000, 1100);
+
+ ret = devm_add_action_or_reset(dev, sx9310_regulator_disable, data);
+ if (ret)
+ return ret;
+
ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
if (ret) {
dev_err(dev, "error in reading WHOAMI register: %d\n", ret);
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:15:06

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 08/15] iio: sx9310: Use regmap_read_poll_timeout() for compensation

Simplify compensation stage by using regmap_read_poll_timeout().

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 25 ++++++++++---------------
1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 2465064971d0a7..3956fd679c6db9 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -796,7 +796,7 @@ static const struct sx9310_reg_default sx9310_default_regs[] = {
static int sx9310_init_compensation(struct iio_dev *indio_dev)
{
struct sx9310_data *data = iio_priv(indio_dev);
- int i, ret;
+ int ret;
unsigned int val;
unsigned int ctrl0;

@@ -810,22 +810,17 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
if (ret < 0)
return ret;

- for (i = 100; i >= 0; i--) {
- msleep(20);
- ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
- if (ret < 0)
- goto out;
- if (!(val & SX9310_COMPSTAT_MASK))
- break;
- }
-
- if (i < 0) {
- dev_err(&data->client->dev,
- "initial compensation timed out: 0x%02x", val);
- ret = -ETIMEDOUT;
+ ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
+ !(val & SX9310_REG_STAT1_COMPSTAT_MASK),
+ 20000, 2000000);
+ if (ret) {
+ if (ret == -ETIMEDOUT)
+ dev_err(&data->client->dev,
+ "0x02 << 3l compensation timed out: 0x%02x",
+ val);
+ return ret;
}

-out:
regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
return ret;
}
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:15:10

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 07/15] iio: sx9310: Use long instead of int for channel bitmaps

Uses for_each_set_bit() macro to loop over channel bitmaps.

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index fb5c16f2aa6b1a..2465064971d0a7 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -136,7 +136,8 @@ struct sx9310_data {
/* Remember enabled channels and sample rate during suspend. */
unsigned int suspend_ctrl0;
struct completion completion;
- unsigned int chan_read, chan_event;
+ unsigned long chan_read;
+ unsigned long chan_event;
int channel_users[SX9310_NUM_CHANNELS];
unsigned int whoami;
};
@@ -279,15 +280,16 @@ static const struct regmap_config sx9310_regmap_config = {
};

static int sx9310_update_chan_en(struct sx9310_data *data,
- unsigned int chan_read,
- unsigned int chan_event)
+ unsigned long chan_read,
+ unsigned long chan_event)
{
int ret;
+ unsigned long channels = chan_read | chan_event;

- if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) {
+ if ((data->chan_read | data->chan_event) != channels) {
ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
SX9310_REG_PROX_CTRL0_SENSOREN_MASK,
- chan_read | chan_event);
+ channels);
if (ret)
return ret;
}
@@ -538,13 +540,13 @@ static void sx9310_push_events(struct iio_dev *indio_dev)
return;
}

- for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) {
+ for_each_set_bit(chan, &data->chan_event, SX9310_NUM_CHANNELS) {
int dir;
u64 ev;
- bool new_prox = val & BIT(chan);
+ bool new_prox;
+
+ new_prox = val & BIT(chan);

- if (!(data->chan_event & BIT(chan)))
- continue;
if (new_prox == data->prox_stat[chan])
/* No change on this channel. */
continue;
@@ -712,13 +714,13 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
{
struct sx9310_data *data = iio_priv(indio_dev);
- unsigned int channels = 0;
+ unsigned long channels = 0;
int bit, ret;

mutex_lock(&data->mutex);
for_each_set_bit(bit, indio_dev->active_scan_mask,
indio_dev->masklength)
- channels |= BIT(indio_dev->channels[bit].channel);
+ __set_bit(indio_dev->channels[bit].channel, &channels);

ret = sx9310_update_chan_en(data, channels, data->chan_event);
mutex_unlock(&data->mutex);
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:15:35

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 09/15] iio: sx9310: Update copyright

Fixes wrong copyright year.

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 3956fd679c6db9..31234691a31abf 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: GPL-2.0
/*
- * Copyright 2018 Google LLC.
+ * Copyright 2020 Google LLC.
*
* Driver for Semtech's SX9310/SX9311 capacitive proximity/button solution.
* Based on SX9500 driver and Semtech driver using the input framework
* <https://my.syncplicity.com/share/teouwsim8niiaud/
* linux-driver-SX9310_NoSmartHSensing>.
- * Reworked April 2019 by Evan Green <[email protected]>
- * and January 2020 by Daniel Campello <[email protected]>
+ * Reworked in April 2019 by Evan Green <[email protected]>
+ * and in January 2020 by Daniel Campello <[email protected]>.
*/

#include <linux/acpi.h>
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:15:37

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 05/15] iio: sx9310: Change from .probe to .probe_new

Uses .probe_new in place of .probe. Also uses device_get_match_data()
for whoami matching.

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 37 ++++++++++++----------------------
1 file changed, 13 insertions(+), 24 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 0fb88ad66f7342..de52afd7c13333 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -138,7 +138,7 @@ struct sx9310_data {
struct completion completion;
unsigned int chan_read, chan_event;
int channel_users[SX9310_NUM_CHANNELS];
- int whoami;
+ unsigned int whoami;
};

static const struct iio_event_spec sx9310_events[] = {
@@ -859,24 +859,15 @@ static int sx9310_init_device(struct iio_dev *indio_dev)

static int sx9310_set_indio_dev_name(struct device *dev,
struct iio_dev *indio_dev,
- const struct i2c_device_id *id, int whoami)
+ unsigned int whoami)
{
- const struct acpi_device_id *acpi_id;
-
- /* id will be NULL when enumerated via ACPI */
- if (id) {
- if (id->driver_data != whoami)
- dev_err(dev, "WHOAMI does not match i2c_device_id: %s",
- id->name);
- } else if (ACPI_HANDLE(dev)) {
- acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
- if (!acpi_id)
- return -ENODEV;
- if (acpi_id->driver_data != whoami)
- dev_err(dev, "WHOAMI does not match acpi_device_id: %s",
- acpi_id->id);
- } else
+ unsigned int long ddata;
+
+ ddata = (uintptr_t)device_get_match_data(dev);
+ if (ddata != whoami) {
+ dev_err(dev, "WHOAMI does not match device data: %u", whoami);
return -ENODEV;
+ }

switch (whoami) {
case SX9310_WHOAMI_VALUE:
@@ -893,8 +884,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
return 0;
}

-static int sx9310_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
+static int sx9310_probe(struct i2c_client *client)
{
int ret;
struct iio_dev *indio_dev;
@@ -920,8 +910,7 @@ static int sx9310_probe(struct i2c_client *client,
return ret;
}

- ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, id,
- data->whoami);
+ ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami);
if (ret < 0)
return ret;

@@ -1034,8 +1023,8 @@ static const struct acpi_device_id sx9310_acpi_match[] = {
MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);

static const struct of_device_id sx9310_of_match[] = {
- { .compatible = "semtech,sx9310" },
- { .compatible = "semtech,sx9311" },
+ { .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE },
+ { .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE },
{},
};
MODULE_DEVICE_TABLE(of, sx9310_of_match);
@@ -1054,7 +1043,7 @@ static struct i2c_driver sx9310_driver = {
.of_match_table = sx9310_of_match,
.pm = &sx9310_pm_ops,
},
- .probe = sx9310_probe,
+ .probe_new = sx9310_probe,
.id_table = sx9310_id,
};
module_i2c_driver(sx9310_driver);
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:17:42

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 03/15] iio: sx9310: Fix irq handling

Fixes enable/disable irq handling at various points. The driver needs to
only enable/disable irqs if there is an actual irq handler installed.

Signed-off-by: Daniel Campello <[email protected]>
---

drivers/iio/proximity/sx9310.c | 24 +++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 07895d4b935d12..76b8bedebeef50 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -376,13 +376,15 @@ static int sx9310_read_proximity(struct sx9310_data *data,
if (ret < 0)
goto out;

- ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
- if (ret < 0)
- goto out_put_channel;
+ if (data->client->irq) {
+ ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
+ if (ret)
+ goto out_put_channel;
+ }

mutex_unlock(&data->mutex);

- if (data->client->irq > 0) {
+ if (data->client->irq) {
ret = wait_for_completion_interruptible(&data->completion);
reinit_completion(&data->completion);
} else {
@@ -401,9 +403,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,
*val = sign_extend32(be16_to_cpu(rawval),
(chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));

- ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
- if (ret < 0)
- goto out_put_channel;
+ if (data->client->irq) {
+ ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
+ if (ret)
+ goto out_put_channel;
+ }

ret = sx9310_put_read_channel(data, chan->channel);
if (ret < 0)
@@ -414,7 +418,8 @@ static int sx9310_read_proximity(struct sx9310_data *data,
return IIO_VAL_INT;

out_disable_irq:
- sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
+ if (data->client->irq)
+ sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
out_put_channel:
sx9310_put_read_channel(data, chan->channel);
out:
@@ -1012,7 +1017,8 @@ static int __maybe_unused sx9310_resume(struct device *dev)
out:
mutex_unlock(&data->mutex);

- enable_irq(data->client->irq);
+ if (!ret)
+ enable_irq(data->client->irq);

return ret;
}
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 15:17:56

by Daniel Campello

[permalink] [raw]
Subject: [PATCH 04/15] iio: sx9310: Remove acpi and of table macros

Avoids unused warnings due to acpi/of table macros.

Signed-off-by: Daniel Campello <[email protected]>
Reported-by: kbuild test robot <[email protected]>
---

drivers/iio/proximity/sx9310.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 76b8bedebeef50..0fb88ad66f7342 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -16,7 +16,6 @@
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
-#include <linux/of.h>
#include <linux/pm.h>
#include <linux/regmap.h>
#include <linux/slab.h>
@@ -1051,8 +1050,8 @@ MODULE_DEVICE_TABLE(i2c, sx9310_id);
static struct i2c_driver sx9310_driver = {
.driver = {
.name = "sx9310",
- .acpi_match_table = ACPI_PTR(sx9310_acpi_match),
- .of_match_table = of_match_ptr(sx9310_of_match),
+ .acpi_match_table = sx9310_acpi_match,
+ .of_match_table = sx9310_of_match,
.pm = &sx9310_pm_ops,
},
.probe = sx9310_probe,
--
2.28.0.rc0.142.g3c755180ce-goog

2020-07-28 20:00:13

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 04/15] iio: sx9310: Remove acpi and of table macros

On Tue, Jul 28, 2020 at 6:16 PM Daniel Campello <[email protected]> wrote:
>
> Avoids unused warnings due to acpi/of table macros.
>

At the same time I would check if mod_devicetable.h is included.

> Signed-off-by: Daniel Campello <[email protected]>
> Reported-by: kbuild test robot <[email protected]>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:00:23

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 07/15] iio: sx9310: Use long instead of int for channel bitmaps

On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
>
> Uses for_each_set_bit() macro to loop over channel bitmaps.
>

Reviewed-by: Andy Shevchenko <[email protected]>

> Signed-off-by: Daniel Campello <[email protected]>
> ---
>
> drivers/iio/proximity/sx9310.c | 24 +++++++++++++-----------
> 1 file changed, 13 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index fb5c16f2aa6b1a..2465064971d0a7 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -136,7 +136,8 @@ struct sx9310_data {
> /* Remember enabled channels and sample rate during suspend. */
> unsigned int suspend_ctrl0;
> struct completion completion;
> - unsigned int chan_read, chan_event;
> + unsigned long chan_read;
> + unsigned long chan_event;
> int channel_users[SX9310_NUM_CHANNELS];
> unsigned int whoami;
> };
> @@ -279,15 +280,16 @@ static const struct regmap_config sx9310_regmap_config = {
> };
>
> static int sx9310_update_chan_en(struct sx9310_data *data,
> - unsigned int chan_read,
> - unsigned int chan_event)
> + unsigned long chan_read,
> + unsigned long chan_event)
> {
> int ret;
> + unsigned long channels = chan_read | chan_event;
>
> - if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) {
> + if ((data->chan_read | data->chan_event) != channels) {
> ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
> SX9310_REG_PROX_CTRL0_SENSOREN_MASK,
> - chan_read | chan_event);
> + channels);
> if (ret)
> return ret;
> }
> @@ -538,13 +540,13 @@ static void sx9310_push_events(struct iio_dev *indio_dev)
> return;
> }
>
> - for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) {
> + for_each_set_bit(chan, &data->chan_event, SX9310_NUM_CHANNELS) {
> int dir;
> u64 ev;
> - bool new_prox = val & BIT(chan);
> + bool new_prox;
> +
> + new_prox = val & BIT(chan);
>
> - if (!(data->chan_event & BIT(chan)))
> - continue;
> if (new_prox == data->prox_stat[chan])
> /* No change on this channel. */
> continue;
> @@ -712,13 +714,13 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
> static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
> {
> struct sx9310_data *data = iio_priv(indio_dev);
> - unsigned int channels = 0;
> + unsigned long channels = 0;
> int bit, ret;
>
> mutex_lock(&data->mutex);
> for_each_set_bit(bit, indio_dev->active_scan_mask,
> indio_dev->masklength)
> - channels |= BIT(indio_dev->channels[bit].channel);
> + __set_bit(indio_dev->channels[bit].channel, &channels);
>
> ret = sx9310_update_chan_en(data, channels, data->chan_event);
> mutex_unlock(&data->mutex);
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:00:25

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 08/15] iio: sx9310: Use regmap_read_poll_timeout() for compensation

On Tue, Jul 28, 2020 at 6:15 PM Daniel Campello <[email protected]> wrote:
>
> Simplify compensation stage by using regmap_read_poll_timeout().

Reviewed-by: Andy Shevchenko <[email protected]>

> Signed-off-by: Daniel Campello <[email protected]>
> ---
>
> drivers/iio/proximity/sx9310.c | 25 ++++++++++---------------
> 1 file changed, 10 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 2465064971d0a7..3956fd679c6db9 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -796,7 +796,7 @@ static const struct sx9310_reg_default sx9310_default_regs[] = {
> static int sx9310_init_compensation(struct iio_dev *indio_dev)
> {
> struct sx9310_data *data = iio_priv(indio_dev);
> - int i, ret;
> + int ret;
> unsigned int val;
> unsigned int ctrl0;
>
> @@ -810,22 +810,17 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> if (ret < 0)
> return ret;
>
> - for (i = 100; i >= 0; i--) {
> - msleep(20);
> - ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
> - if (ret < 0)
> - goto out;
> - if (!(val & SX9310_COMPSTAT_MASK))
> - break;
> - }
> -
> - if (i < 0) {
> - dev_err(&data->client->dev,
> - "initial compensation timed out: 0x%02x", val);
> - ret = -ETIMEDOUT;
> + ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
> + !(val & SX9310_REG_STAT1_COMPSTAT_MASK),
> + 20000, 2000000);
> + if (ret) {
> + if (ret == -ETIMEDOUT)
> + dev_err(&data->client->dev,
> + "0x02 << 3l compensation timed out: 0x%02x",
> + val);
> + return ret;
> }
>
> -out:
> regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
> return ret;
> }
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:00:26

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 10/15] iio: sx9310: Simplify error return handling

On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
>
> Checks for non-zero return values to signal error conditions.

Reviewed-by: Andy Shevchenko <[email protected]>

> Signed-off-by: Daniel Campello <[email protected]>
> ---
>
> drivers/iio/proximity/sx9310.c | 52 +++++++++++++++++-----------------
> 1 file changed, 26 insertions(+), 26 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 31234691a31abf..051c6515e62c18 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -338,7 +338,7 @@ static int sx9310_read_prox_data(struct sx9310_data *data,
> int ret;
>
> ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> return regmap_bulk_read(data->regmap, chan->address, val, sizeof(*val));
> @@ -354,7 +354,7 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
> unsigned int val;
>
> ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> val = (val & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
> @@ -368,13 +368,13 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
> static int sx9310_read_proximity(struct sx9310_data *data,
> const struct iio_chan_spec *chan, int *val)
> {
> - int ret = 0;
> + int ret;
> __be16 rawval;
>
> mutex_lock(&data->mutex);
>
> ret = sx9310_get_read_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> if (data->client->irq) {
> @@ -394,11 +394,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,
>
> mutex_lock(&data->mutex);
>
> - if (ret < 0)
> + if (ret)
> goto out_disable_irq;
>
> ret = sx9310_read_prox_data(data, chan, &rawval);
> - if (ret < 0)
> + if (ret)
> goto out_disable_irq;
>
> *val = sign_extend32(be16_to_cpu(rawval),
> @@ -411,7 +411,7 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> }
>
> ret = sx9310_put_read_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> mutex_unlock(&data->mutex);
> @@ -434,7 +434,7 @@ static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
> unsigned int regval;
> int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
>
> - if (ret < 0)
> + if (ret)
> return ret;
>
> regval = (regval & SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK) >>
> @@ -535,7 +535,7 @@ static void sx9310_push_events(struct iio_dev *indio_dev)
>
> /* Read proximity state on all channels */
> ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val);
> - if (ret < 0) {
> + if (ret) {
> dev_err(&data->client->dev, "i2c transfer error in irq\n");
> return;
> }
> @@ -570,7 +570,7 @@ static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
> mutex_lock(&data->mutex);
>
> ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
> - if (ret < 0) {
> + if (ret) {
> dev_err(&data->client->dev, "i2c transfer error in irq\n");
> goto out;
> }
> @@ -613,20 +613,20 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
> mutex_lock(&data->mutex);
> if (state) {
> ret = sx9310_get_event_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out_unlock;
> if (!(data->chan_event & ~BIT(chan->channel))) {
> ret = sx9310_enable_irq(data, eventirq);
> - if (ret < 0)
> + if (ret)
> sx9310_put_event_channel(data, chan->channel);
> }
> } else {
> ret = sx9310_put_event_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out_unlock;
> if (!data->chan_event) {
> ret = sx9310_disable_irq(data, eventirq);
> - if (ret < 0)
> + if (ret)
> sx9310_get_event_channel(data, chan->channel);
> }
> }
> @@ -665,7 +665,7 @@ static int sx9310_set_trigger_state(struct iio_trigger *trig, bool state)
> ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
> else if (!data->chan_read)
> ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> data->trigger_enabled = state;
> @@ -694,7 +694,7 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private)
> indio_dev->masklength) {
> ret = sx9310_read_prox_data(data, &indio_dev->channels[bit],
> &val);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> data->buffer[i++] = val;
> @@ -801,13 +801,13 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> unsigned int ctrl0;
>
> ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> /* run the compensation phase on all channels */
> ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
> ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
> @@ -833,21 +833,21 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
> unsigned int i, val;
>
> ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> usleep_range(1000, 2000); /* power-up time is ~1ms. */
>
> /* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
> ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> /* Program some sane defaults. */
> for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
> initval = &sx9310_default_regs[i];
> ret = regmap_write(data->regmap, initval->reg, initval->def);
> - if (ret < 0)
> + if (ret)
> return ret;
> }
>
> @@ -901,14 +901,14 @@ static int sx9310_probe(struct i2c_client *client)
> return PTR_ERR(data->regmap);
>
> ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
> - if (ret < 0) {
> + if (ret) {
> dev_err(&client->dev, "error in reading WHOAMI register: %d",
> ret);
> return ret;
> }
>
> ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
> @@ -920,7 +920,7 @@ static int sx9310_probe(struct i2c_client *client)
> i2c_set_clientdata(client, indio_dev);
>
> ret = sx9310_init_device(indio_dev);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> if (client->irq) {
> @@ -929,7 +929,7 @@ static int sx9310_probe(struct i2c_client *client)
> sx9310_irq_thread_handler,
> IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> "sx9310_event", indio_dev);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> data->trig =
> @@ -951,7 +951,7 @@ static int sx9310_probe(struct i2c_client *client)
> iio_pollfunc_store_time,
> sx9310_trigger_handler,
> &sx9310_buffer_setup_ops);
> - if (ret < 0)
> + if (ret)
> return ret;
>
> return devm_iio_device_register(&client->dev, indio_dev);
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:00:32

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 11/15] iio: sx9310: Use variable to hold &client->dev

On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
>
> Improves readability by storing &client->dev in a local variable.

Reviewed-by: Andy Shevchenko <[email protected]>

> Signed-off-by: Daniel Campello <[email protected]>
> ---
>
> drivers/iio/proximity/sx9310.c | 30 +++++++++++++++---------------
> 1 file changed, 15 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 051c6515e62c18..ba383710ef0dcf 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -884,11 +884,12 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> static int sx9310_probe(struct i2c_client *client)
> {
> int ret;
> + struct device *dev = &client->dev;
> struct iio_dev *indio_dev;
> struct sx9310_data *data;
>
> - indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> - if (indio_dev == NULL)
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> + if (!indio_dev)
> return -ENOMEM;
>
> data = iio_priv(indio_dev);
> @@ -902,17 +903,16 @@ static int sx9310_probe(struct i2c_client *client)
>
> ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
> if (ret) {
> - dev_err(&client->dev, "error in reading WHOAMI register: %d",
> - ret);
> + dev_err(dev, "error in reading WHOAMI register: %d", ret);
> return ret;
> }
>
> - ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami);
> + ret = sx9310_set_indio_dev_name(dev, indio_dev, data->whoami);
> if (ret)
> return ret;
>
> - ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
> - indio_dev->dev.parent = &client->dev;
> + ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(dev));
> + indio_dev->dev.parent = dev;
> indio_dev->channels = sx9310_channels;
> indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
> indio_dev->info = &sx9310_info;
> @@ -924,7 +924,7 @@ static int sx9310_probe(struct i2c_client *client)
> return ret;
>
> if (client->irq) {
> - ret = devm_request_threaded_irq(&client->dev, client->irq,
> + ret = devm_request_threaded_irq(dev, client->irq,
> sx9310_irq_handler,
> sx9310_irq_thread_handler,
> IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> @@ -932,29 +932,29 @@ static int sx9310_probe(struct i2c_client *client)
> if (ret)
> return ret;
>
> - data->trig =
> - devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
> - indio_dev->name, indio_dev->id);
> + data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
> + indio_dev->name,
> + indio_dev->id);
> if (!data->trig)
> return -ENOMEM;
>
> - data->trig->dev.parent = &client->dev;
> + data->trig->dev.parent = dev;
> data->trig->ops = &sx9310_trigger_ops;
> iio_trigger_set_drvdata(data->trig, indio_dev);
>
> - ret = devm_iio_trigger_register(&client->dev, data->trig);
> + ret = devm_iio_trigger_register(dev, data->trig);
> if (ret)
> return ret;
> }
>
> - ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
> + ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
> iio_pollfunc_store_time,
> sx9310_trigger_handler,
> &sx9310_buffer_setup_ops);
> if (ret)
> return ret;
>
> - return devm_iio_device_register(&client->dev, indio_dev);
> + return devm_iio_device_register(dev, indio_dev);
> }
>
> static int __maybe_unused sx9310_suspend(struct device *dev)
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:00:34

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 12/15] iio: sx9310: Miscellaneous format fixes

On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
>
> Miscellaneous format fixes throughout the whole file.

Reviewed-by: Andy Shevchenko <[email protected]>

> Signed-off-by: Daniel Campello <[email protected]>
> ---
>
> drivers/iio/proximity/sx9310.c | 28 ++++++++++------------------
> 1 file changed, 10 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index ba383710ef0dcf..3f981d28ee4056 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -90,28 +90,21 @@
> #define SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT 0x3c
>
> #define SX9310_REG_SENSOR_SEL 0x30
> -
> #define SX9310_REG_USE_MSB 0x31
> #define SX9310_REG_USE_LSB 0x32
> -
> #define SX9310_REG_AVG_MSB 0x33
> #define SX9310_REG_AVG_LSB 0x34
> -
> #define SX9310_REG_DIFF_MSB 0x35
> #define SX9310_REG_DIFF_LSB 0x36
> -
> #define SX9310_REG_OFFSET_MSB 0x37
> #define SX9310_REG_OFFSET_LSB 0x38
> -
> #define SX9310_REG_SAR_MSB 0x39
> #define SX9310_REG_SAR_LSB 0x3a
> -
> #define SX9310_REG_I2C_ADDR 0x40
> #define SX9310_REG_PAUSE 0x41
> #define SX9310_REG_WHOAMI 0x42
> #define SX9310_WHOAMI_VALUE 0x01
> #define SX9311_WHOAMI_VALUE 0x02
> -
> #define SX9310_REG_RESET 0x7f
> #define SX9310_SOFT_RESET 0xde
>
> @@ -402,7 +395,7 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> goto out_disable_irq;
>
> *val = sign_extend32(be16_to_cpu(rawval),
> - (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));
> + chan->address == SX9310_REG_DIFF_MSB ? 11 : 15);
>
> if (data->client->irq) {
> ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> @@ -432,8 +425,9 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
> {
> unsigned int regval;
> - int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
> + int ret;
>
> + ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
> if (ret)
> return ret;
>
> @@ -518,10 +512,9 @@ static irqreturn_t sx9310_irq_handler(int irq, void *private)
> iio_trigger_poll(data->trig);
>
> /*
> - * Even if no event is enabled, we need to wake the thread to
> - * clear the interrupt state by reading SX9310_REG_IRQ_SRC. It
> - * is not possible to do that here because regmap_read takes a
> - * mutex.
> + * Even if no event is enabled, we need to wake the thread to clear the
> + * interrupt state by reading SX9310_REG_IRQ_SRC.
> + * It is not possible to do that here because regmap_read takes a mutex.
> */
> return IRQ_WAKE_THREAD;
> }
> @@ -638,7 +631,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,
>
> static struct attribute *sx9310_attributes[] = {
> &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
> - NULL,
> + NULL
> };
>
> static const struct attribute_group sx9310_attribute_group = {
> @@ -969,7 +962,6 @@ static int __maybe_unused sx9310_suspend(struct device *dev)
> mutex_lock(&data->mutex);
> ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
> &data->suspend_ctrl0);
> -
> if (ret)
> goto out;
>
> @@ -1015,21 +1007,21 @@ static const struct dev_pm_ops sx9310_pm_ops = {
> static const struct acpi_device_id sx9310_acpi_match[] = {
> { "STH9310", SX9310_WHOAMI_VALUE },
> { "STH9311", SX9311_WHOAMI_VALUE },
> - {},
> + {}
> };
> MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);
>
> static const struct of_device_id sx9310_of_match[] = {
> { .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE },
> { .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE },
> - {},
> + {}
> };
> MODULE_DEVICE_TABLE(of, sx9310_of_match);
>
> static const struct i2c_device_id sx9310_id[] = {
> { "sx9310", SX9310_WHOAMI_VALUE },
> { "sx9311", SX9311_WHOAMI_VALUE },
> - {},
> + {}
> };
> MODULE_DEVICE_TABLE(i2c, sx9310_id);
>
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:00:46

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 13/15] iio: sx9310: Add newlines to printks

On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
>
> From: Stephen Boyd <[email protected]>
>
> Printks in the kernel have newlines at the end. Add them to the few

Printk()s

> printks in this driver.

printk()s

> Reviewed-by: Daniel Campello <[email protected]>
> Reviewed-by: Douglas Anderson <[email protected]>
> Fixes: 72ad02b15d63 ("iio: Add SEMTECH SX9310/9311 sensor driver")
> Signed-off-by: Stephen Boyd <[email protected]>
> Signed-off-by: Daniel Campello <[email protected]>

It has ordering issues
Should be

Fixes:
SoB: Stephen
Rb: Douglas
Rb: Daniel
SoB: Daniel


> ---
>
> drivers/iio/proximity/sx9310.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 3f981d28ee4056..4553ee83a016a3 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -809,7 +809,7 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> if (ret) {
> if (ret == -ETIMEDOUT)
> dev_err(&data->client->dev,
> - "0x02 << 3l compensation timed out: 0x%02x",
> + "0x02 << 3l compensation timed out: 0x%02x\n",

Looks like ping-pong style in the series, i.e. you may fix this when
you introduced this line.

Check the rest (and not only printk()s) for the similar style and
avoid the latter.

> val);
> return ret;
> }
> @@ -855,7 +855,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
>
> ddata = (uintptr_t)device_get_match_data(dev);
> if (ddata != whoami) {
> - dev_err(dev, "WHOAMI does not match device data: %u", whoami);
> + dev_err(dev, "WHOAMI does not match device data: %u\n", whoami);
> return -ENODEV;
> }
>
> @@ -867,7 +867,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> indio_dev->name = "sx9311";
> break;
> default:
> - dev_err(dev, "unexpected WHOAMI response: %u", whoami);
> + dev_err(dev, "unexpected WHOAMI response: %u\n", whoami);
> return -ENODEV;
> }
>
> @@ -896,7 +896,7 @@ static int sx9310_probe(struct i2c_client *client)
>
> ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
> if (ret) {
> - dev_err(dev, "error in reading WHOAMI register: %d", ret);
> + dev_err(dev, "error in reading WHOAMI register: %d\n", ret);
> return ret;
> }
>
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:00:49

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 14/15] iio: sx9310: Drop channel_users[]

On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
>
> From: Stephen Boyd <[email protected]>
>
> This struct member isn't used. Drop it.
>
> Fixes: 72ad02b15d63 ("iio: Add SEMTECH SX9310/9311 sensor driver")
> Signed-off-by: Stephen Boyd <[email protected]>
> Reviewed-by: Douglas Anderson <[email protected]>
> Reviewed-by: Daniel Campello <[email protected]>
> Signed-off-by: Daniel Campello <[email protected]>

Here is everything correct!

Reviewed-by: Andy Shevchenko <[email protected]>

> ---
>
> drivers/iio/proximity/sx9310.c | 1 -
> 1 file changed, 1 deletion(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 4553ee83a016a3..202018b726e32f 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -131,7 +131,6 @@ struct sx9310_data {
> struct completion completion;
> unsigned long chan_read;
> unsigned long chan_event;
> - int channel_users[SX9310_NUM_CHANNELS];
> unsigned int whoami;
> };
>
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:00:51

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 15/15] iio: sx9310: Enable vdd and svdd regulators at probe

On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
>
> From: Stephen Boyd <[email protected]>
>
> Enable the main power supply (vdd) and digital IO power supply (svdd)
> during probe so that the i2c communication and device works properly on
> boards that aggressively power gate these supplies.
>
> Reviewed-by: Douglas Anderson <[email protected]>
> Signed-off-by: Stephen Boyd <[email protected]>
> Signed-off-by: Daniel Campello <[email protected]>

Again wrong order.
After fixing,
Reviewed-by: Andy Shevchenko <[email protected]>

> ---
>
> drivers/iio/proximity/sx9310.c | 26 ++++++++++++++++++++++++++
> 1 file changed, 26 insertions(+)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 202018b726e32f..24a2360b6314ef 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -18,6 +18,7 @@
> #include <linux/module.h>
> #include <linux/pm.h>
> #include <linux/regmap.h>
> +#include <linux/regulator/consumer.h>
> #include <linux/slab.h>
>
> #include <linux/iio/buffer.h>
> @@ -118,6 +119,7 @@ struct sx9310_data {
> struct i2c_client *client;
> struct iio_trigger *trig;
> struct regmap *regmap;
> + struct regulator_bulk_data supplies[2];
> /*
> * Last reading of the proximity status for each channel.
> * We only send an event to user space when this changes.
> @@ -873,6 +875,13 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> return 0;
> }
>
> +static void sx9310_regulator_disable(void *_data)
> +{
> + struct sx9310_data *data = _data;
> +
> + regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
> +}
> +
> static int sx9310_probe(struct i2c_client *client)
> {
> int ret;
> @@ -886,6 +895,8 @@ static int sx9310_probe(struct i2c_client *client)
>
> data = iio_priv(indio_dev);
> data->client = client;
> + data->supplies[0].supply = "vdd";
> + data->supplies[1].supply = "svdd";
> mutex_init(&data->mutex);
> init_completion(&data->completion);
>
> @@ -893,6 +904,21 @@ static int sx9310_probe(struct i2c_client *client)
> if (IS_ERR(data->regmap))
> return PTR_ERR(data->regmap);
>
> + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
> + data->supplies);
> + if (ret)
> + return ret;
> +
> + ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies);
> + if (ret)
> + return ret;
> + /* Must wait for Tpor time after initial power up */
> + usleep_range(1000, 1100);
> +
> + ret = devm_add_action_or_reset(dev, sx9310_regulator_disable, data);
> + if (ret)
> + return ret;
> +
> ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
> if (ret) {
> dev_err(dev, "error in reading WHOAMI register: %d\n", ret);
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:01:09

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH 13/15] iio: sx9310: Add newlines to printks

On Tue, 2020-07-28 at 21:19 +0300, Andy Shevchenko wrote:
> On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
> > From: Stephen Boyd <[email protected]>
> >
> > Printks in the kernel have newlines at the end. Add them to the few
>
> Printk()s
>
> > printks in this driver.
>
> printk()s

Random kernel pedantry.
This patch should not need to be respun for any of these.

> > Reviewed-by: Daniel Campello <[email protected]>
> > Reviewed-by: Douglas Anderson <[email protected]>
> > Fixes: 72ad02b15d63 ("iio: Add SEMTECH SX9310/9311 sensor driver")
> > Signed-off-by: Stephen Boyd <[email protected]>
> > Signed-off-by: Daniel Campello <[email protected]>
>
> It has ordering issues
> Should be
>
> Fixes:
> SoB: Stephen
> Rb: Douglas
> Rb: Daniel
> SoB: Daniel
>
>
> > ---
> >
> > drivers/iio/proximity/sx9310.c | 8 ++++----
> > 1 file changed, 4 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> > index 3f981d28ee4056..4553ee83a016a3 100644
> > --- a/drivers/iio/proximity/sx9310.c
> > +++ b/drivers/iio/proximity/sx9310.c
> > @@ -809,7 +809,7 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> > if (ret) {
> > if (ret == -ETIMEDOUT)
> > dev_err(&data->client->dev,
> > - "0x02 << 3l compensation timed out: 0x%02x",
> > + "0x02 << 3l compensation timed out: 0x%02x\n",
>
> Looks like ping-pong style in the series, i.e. you may fix this when
> you introduced this line.
>
> Check the rest (and not only printk()s) for the similar style and
> avoid the latter.
>
> > val);
> > return ret;
> > }
> > @@ -855,7 +855,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> >
> > ddata = (uintptr_t)device_get_match_data(dev);
> > if (ddata != whoami) {
> > - dev_err(dev, "WHOAMI does not match device data: %u", whoami);
> > + dev_err(dev, "WHOAMI does not match device data: %u\n", whoami);
> > return -ENODEV;
> > }
> >
> > @@ -867,7 +867,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> > indio_dev->name = "sx9311";
> > break;
> > default:
> > - dev_err(dev, "unexpected WHOAMI response: %u", whoami);
> > + dev_err(dev, "unexpected WHOAMI response: %u\n", whoami);
> > return -ENODEV;
> > }
> >
> > @@ -896,7 +896,7 @@ static int sx9310_probe(struct i2c_client *client)
> >
> > ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
> > if (ret) {
> > - dev_err(dev, "error in reading WHOAMI register: %d", ret);
> > + dev_err(dev, "error in reading WHOAMI register: %d\n", ret);
> > return ret;
> > }
> >
> > --
> > 2.28.0.rc0.142.g3c755180ce-goog
> >
>
>

2020-07-28 20:01:38

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 13/15] iio: sx9310: Add newlines to printks

On Tue, Jul 28, 2020 at 9:24 PM Joe Perches <[email protected]> wrote:
> On Tue, 2020-07-28 at 21:19 +0300, Andy Shevchenko wrote:
> > On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
> > > From: Stephen Boyd <[email protected]>
> > >
> > > Printks in the kernel have newlines at the end. Add them to the few
> >
> > Printk()s
> >
> > > printks in this driver.
> >
> > printk()s
>
> Random kernel pedantry.
> This patch should not need to be respun for any of these.

If for above I can agree with you, below is definitely subject to improvement.

>
> > > Reviewed-by: Daniel Campello <[email protected]>
> > > Reviewed-by: Douglas Anderson <[email protected]>
> > > Fixes: 72ad02b15d63 ("iio: Add SEMTECH SX9310/9311 sensor driver")
> > > Signed-off-by: Stephen Boyd <[email protected]>
> > > Signed-off-by: Daniel Campello <[email protected]>
> >
> > It has ordering issues
> > Should be
> >
> > Fixes:
> > SoB: Stephen
> > Rb: Douglas
> > Rb: Daniel
> > SoB: Daniel
> >
> >
> > > ---
> > >
> > > drivers/iio/proximity/sx9310.c | 8 ++++----
> > > 1 file changed, 4 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> > > index 3f981d28ee4056..4553ee83a016a3 100644
> > > --- a/drivers/iio/proximity/sx9310.c
> > > +++ b/drivers/iio/proximity/sx9310.c
> > > @@ -809,7 +809,7 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> > > if (ret) {
> > > if (ret == -ETIMEDOUT)
> > > dev_err(&data->client->dev,
> > > - "0x02 << 3l compensation timed out: 0x%02x",
> > > + "0x02 << 3l compensation timed out: 0x%02x\n",
> >
> > Looks like ping-pong style in the series, i.e. you may fix this when
> > you introduced this line.
> >
> > Check the rest (and not only printk()s) for the similar style and
> > avoid the latter.
> >
> > > val);
> > > return ret;
> > > }
> > > @@ -855,7 +855,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> > >
> > > ddata = (uintptr_t)device_get_match_data(dev);
> > > if (ddata != whoami) {
> > > - dev_err(dev, "WHOAMI does not match device data: %u", whoami);
> > > + dev_err(dev, "WHOAMI does not match device data: %u\n", whoami);
> > > return -ENODEV;
> > > }
> > >
> > > @@ -867,7 +867,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> > > indio_dev->name = "sx9311";
> > > break;
> > > default:
> > > - dev_err(dev, "unexpected WHOAMI response: %u", whoami);
> > > + dev_err(dev, "unexpected WHOAMI response: %u\n", whoami);
> > > return -ENODEV;
> > > }
> > >
> > > @@ -896,7 +896,7 @@ static int sx9310_probe(struct i2c_client *client)
> > >
> > > ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
> > > if (ret) {
> > > - dev_err(dev, "error in reading WHOAMI register: %d", ret);
> > > + dev_err(dev, "error in reading WHOAMI register: %d\n", ret);
> > > return ret;
> > > }
> > >
> > > --
> > > 2.28.0.rc0.142.g3c755180ce-goog
> > >
> >
> >
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:02:08

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 03/15] iio: sx9310: Fix irq handling

On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
>
> Fixes enable/disable irq handling at various points. The driver needs to
> only enable/disable irqs if there is an actual irq handler installed.

> - enable_irq(data->client->irq);
> + if (!ret)
> + enable_irq(data->client->irq);
>
> return ret;
> }

Can it be a usual pattern?

if (ret)
return ret;
...
return 0;

--
With Best Regards,
Andy Shevchenko

2020-07-28 20:02:13

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 05/15] iio: sx9310: Change from .probe to .probe_new

On Tue, Jul 28, 2020 at 6:15 PM Daniel Campello <[email protected]> wrote:
>
> Uses .probe_new in place of .probe. Also uses device_get_match_data()
> for whoami matching.

Good one!
Reviewed-by: Andy Shevchenko <[email protected]>

> Signed-off-by: Daniel Campello <[email protected]>
> ---
>
> drivers/iio/proximity/sx9310.c | 37 ++++++++++++----------------------
> 1 file changed, 13 insertions(+), 24 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 0fb88ad66f7342..de52afd7c13333 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -138,7 +138,7 @@ struct sx9310_data {
> struct completion completion;
> unsigned int chan_read, chan_event;
> int channel_users[SX9310_NUM_CHANNELS];
> - int whoami;
> + unsigned int whoami;
> };
>
> static const struct iio_event_spec sx9310_events[] = {
> @@ -859,24 +859,15 @@ static int sx9310_init_device(struct iio_dev *indio_dev)
>
> static int sx9310_set_indio_dev_name(struct device *dev,
> struct iio_dev *indio_dev,
> - const struct i2c_device_id *id, int whoami)
> + unsigned int whoami)
> {
> - const struct acpi_device_id *acpi_id;
> -
> - /* id will be NULL when enumerated via ACPI */
> - if (id) {
> - if (id->driver_data != whoami)
> - dev_err(dev, "WHOAMI does not match i2c_device_id: %s",
> - id->name);
> - } else if (ACPI_HANDLE(dev)) {
> - acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
> - if (!acpi_id)
> - return -ENODEV;
> - if (acpi_id->driver_data != whoami)
> - dev_err(dev, "WHOAMI does not match acpi_device_id: %s",
> - acpi_id->id);
> - } else
> + unsigned int long ddata;
> +
> + ddata = (uintptr_t)device_get_match_data(dev);
> + if (ddata != whoami) {
> + dev_err(dev, "WHOAMI does not match device data: %u", whoami);
> return -ENODEV;
> + }
>
> switch (whoami) {
> case SX9310_WHOAMI_VALUE:
> @@ -893,8 +884,7 @@ static int sx9310_set_indio_dev_name(struct device *dev,
> return 0;
> }
>
> -static int sx9310_probe(struct i2c_client *client,
> - const struct i2c_device_id *id)
> +static int sx9310_probe(struct i2c_client *client)
> {
> int ret;
> struct iio_dev *indio_dev;
> @@ -920,8 +910,7 @@ static int sx9310_probe(struct i2c_client *client,
> return ret;
> }
>
> - ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, id,
> - data->whoami);
> + ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami);
> if (ret < 0)
> return ret;
>
> @@ -1034,8 +1023,8 @@ static const struct acpi_device_id sx9310_acpi_match[] = {
> MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);
>
> static const struct of_device_id sx9310_of_match[] = {
> - { .compatible = "semtech,sx9310" },
> - { .compatible = "semtech,sx9311" },
> + { .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE },
> + { .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE },
> {},
> };
> MODULE_DEVICE_TABLE(of, sx9310_of_match);
> @@ -1054,7 +1043,7 @@ static struct i2c_driver sx9310_driver = {
> .of_match_table = sx9310_of_match,
> .pm = &sx9310_pm_ops,
> },
> - .probe = sx9310_probe,
> + .probe_new = sx9310_probe,
> .id_table = sx9310_id,
> };
> module_i2c_driver(sx9310_driver);
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>


--
With Best Regards,
Andy Shevchenko

2020-07-28 20:02:22

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 06/15] iio: sx9310: Align memory

On Tue, Jul 28, 2020 at 6:15 PM Daniel Campello <[email protected]> wrote:
>
> Use __aligned(8) to ensure that the timestamp is correctly aligned
> when we call push_to_buffers
>
> Signed-off-by: Daniel Campello <[email protected]>
> ---
>
> drivers/iio/proximity/sx9310.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index de52afd7c13333..fb5c16f2aa6b1a 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -131,8 +131,8 @@ struct sx9310_data {
> */
> bool prox_stat[SX9310_NUM_CHANNELS];
> bool trigger_enabled;
> - __be16 buffer[SX9310_NUM_CHANNELS +
> - 4]; /* 64-bit data + 64-bit timestamp */
> + /* 64-bit data + 64-bit timestamp buffer */
> + __be16 buffer[SX9310_NUM_CHANNELS + 4] __aligned(8);

If the data amount (channels) is always the same, please, use struct approach.
Otherwise put a comment explaining dynamic data.

--
With Best Regards,
Andy Shevchenko

2020-07-28 20:04:15

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 07/15] iio: sx9310: Use long instead of int for channel bitmaps

Quoting Daniel Campello (2020-07-28 08:12:50)
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index fb5c16f2aa6b1a..2465064971d0a7 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -538,13 +540,13 @@ static void sx9310_push_events(struct iio_dev *indio_dev)
> return;
> }
>
> - for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) {
> + for_each_set_bit(chan, &data->chan_event, SX9310_NUM_CHANNELS) {
> int dir;
> u64 ev;
> - bool new_prox = val & BIT(chan);
> + bool new_prox;
> +
> + new_prox = val & BIT(chan);
>
> - if (!(data->chan_event & BIT(chan)))
> - continue;
> if (new_prox == data->prox_stat[chan])

Why not make 'prox_stat' a bitmap too and then xor them to iterate over
that bitmap instead?

> /* No change on this channel. */
> continue;

2020-07-28 20:04:20

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 08/15] iio: sx9310: Use regmap_read_poll_timeout() for compensation

Quoting Daniel Campello (2020-07-28 08:12:51)
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 2465064971d0a7..3956fd679c6db9 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -810,22 +810,17 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> if (ret < 0)
> return ret;
>
> - for (i = 100; i >= 0; i--) {
> - msleep(20);
> - ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
> - if (ret < 0)
> - goto out;
> - if (!(val & SX9310_COMPSTAT_MASK))
> - break;
> - }
> -
> - if (i < 0) {
> - dev_err(&data->client->dev,
> - "initial compensation timed out: 0x%02x", val);
> - ret = -ETIMEDOUT;
> + ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
> + !(val & SX9310_REG_STAT1_COMPSTAT_MASK),
> + 20000, 2000000);
> + if (ret) {
> + if (ret == -ETIMEDOUT)
> + dev_err(&data->client->dev,
> + "0x02 << 3l compensation timed out: 0x%02x",

What does 0x02 << 3l mean?

> + val);
> + return ret;
> }
>

2020-07-28 20:04:34

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 11/15] iio: sx9310: Use variable to hold &client->dev

Quoting Daniel Campello (2020-07-28 08:12:54)
> Improves readability by storing &client->dev in a local variable.
>
> Signed-off-by: Daniel Campello <[email protected]>
> ---

Reviewed-by: Stephen Boyd <[email protected]>

2020-07-28 20:04:47

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 02/15] iio: sx9310: Update macros declarations

Quoting Daniel Campello (2020-07-28 08:12:45)
> Follows spec sheet for macro declarations.
>
> Signed-off-by: Daniel Campello <[email protected]>
> ---

Reviewed-by: Stephen Boyd <[email protected]>

2020-07-28 20:05:56

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 05/15] iio: sx9310: Change from .probe to .probe_new

Quoting Daniel Campello (2020-07-28 08:12:48)
> Uses .probe_new in place of .probe. Also uses device_get_match_data()
> for whoami matching.
>
> Signed-off-by: Daniel Campello <[email protected]>
> ---

Reviewed-by: Stephen Boyd <[email protected]>

2020-07-28 20:06:29

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 10/15] iio: sx9310: Simplify error return handling

Quoting Daniel Campello (2020-07-28 08:12:53)
> @@ -368,13 +368,13 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
> static int sx9310_read_proximity(struct sx9310_data *data,
> const struct iio_chan_spec *chan, int *val)
> {
> - int ret = 0;
> + int ret;
> __be16 rawval;
>
> mutex_lock(&data->mutex);
>
> ret = sx9310_get_read_channel(data, chan->channel);
> - if (ret < 0)
> + if (ret)
> goto out;
>
> if (data->client->irq) {
> @@ -394,11 +394,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,
>
> mutex_lock(&data->mutex);
>
> - if (ret < 0)
> + if (ret)
> goto out_disable_irq;

Why is this condition checked after grabbing the mutex? Shouldn't it be
checked before grabbing the mutex? Or is that supposed to be a
mutex_unlock()?

>
> ret = sx9310_read_prox_data(data, chan, &rawval);
> - if (ret < 0)
> + if (ret)
> goto out_disable_irq;
>
> *val = sign_extend32(be16_to_cpu(rawval),

2020-07-28 20:06:43

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 12/15] iio: sx9310: Miscellaneous format fixes

Quoting Daniel Campello (2020-07-28 08:12:55)
> Miscellaneous format fixes throughout the whole file.
>
> Signed-off-by: Daniel Campello <[email protected]>
> ---

Reviewed-by: Stephen Boyd <[email protected]>

2020-07-28 20:08:05

by Daniel Campello

[permalink] [raw]
Subject: Re: [PATCH 03/15] iio: sx9310: Fix irq handling

On Tue, Jul 28, 2020 at 12:08 PM Andy Shevchenko
<[email protected]> wrote:
>
> On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
> >
> > Fixes enable/disable irq handling at various points. The driver needs to
> > only enable/disable irqs if there is an actual irq handler installed.
>
> > - enable_irq(data->client->irq);
> > + if (!ret)
> > + enable_irq(data->client->irq);
> >
> > return ret;
> > }
>
> Can it be a usual pattern?
>
> if (ret)
> return ret;
> ...
> return 0;

I think this way is more readable. The alternative would have to be
something like this:

....
if (ret)
goto out;
mutex_unlock(&data->mutex);
enable_irq(data->client->irq);
return 0;

out:
mutex_unlock(&data->mutex);
return ret;

>
> --
> With Best Regards,
> Andy Shevchenko

Regards,
Daniel Campello

2020-07-28 20:17:45

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 03/15] iio: sx9310: Fix irq handling

Quoting Daniel Campello (2020-07-28 13:07:00)
> On Tue, Jul 28, 2020 at 12:08 PM Andy Shevchenko
> <[email protected]> wrote:
> >
> > On Tue, Jul 28, 2020 at 6:14 PM Daniel Campello <[email protected]> wrote:
> > >
> > > Fixes enable/disable irq handling at various points. The driver needs to
> > > only enable/disable irqs if there is an actual irq handler installed.
> >
> > > - enable_irq(data->client->irq);
> > > + if (!ret)
> > > + enable_irq(data->client->irq);
> > >
> > > return ret;
> > > }
> >
> > Can it be a usual pattern?
> >
> > if (ret)
> > return ret;
> > ...
> > return 0;
>
> I think this way is more readable. The alternative would have to be
> something like this:
>
> ....
> if (ret)
> goto out;
> mutex_unlock(&data->mutex);
> enable_irq(data->client->irq);
> return 0;
>
> out:
> mutex_unlock(&data->mutex);
> return ret;
>

I think the suggestion is

mutex_unlock(&data->mutex);
if (ret)
return ret;

enable_irq(data->client->irq);

return 0;

2020-07-28 20:49:02

by Daniel Campello

[permalink] [raw]
Subject: Re: [PATCH 04/15] iio: sx9310: Remove acpi and of table macros

On Tue, Jul 28, 2020 at 12:09 PM Andy Shevchenko
<[email protected]> wrote:
>
> On Tue, Jul 28, 2020 at 6:16 PM Daniel Campello <[email protected]> wrote:
> >
> > Avoids unused warnings due to acpi/of table macros.
> >
>
> At the same time I would check if mod_devicetable.h is included.
I did the following and no error showed up:
#ifndef LINUX_MOD_DEVICETABLE_H
#error Missing include
#endif

>
> > Signed-off-by: Daniel Campello <[email protected]>
> > Reported-by: kbuild test robot <[email protected]>
>
>
> --
> With Best Regards,
> Andy Shevchenko

Regards,
Daniel Campello

2020-07-28 21:26:36

by Daniel Campello

[permalink] [raw]
Subject: Re: [PATCH 10/15] iio: sx9310: Simplify error return handling

On Tue, Jul 28, 2020 at 1:40 PM Stephen Boyd <[email protected]> wrote:
>
> Quoting Daniel Campello (2020-07-28 08:12:53)
> > @@ -368,13 +368,13 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
> > static int sx9310_read_proximity(struct sx9310_data *data,
> > const struct iio_chan_spec *chan, int *val)
> > {
> > - int ret = 0;
> > + int ret;
> > __be16 rawval;
> >
> > mutex_lock(&data->mutex);
> >
> > ret = sx9310_get_read_channel(data, chan->channel);
> > - if (ret < 0)
> > + if (ret)
> > goto out;
> >
> > if (data->client->irq) {
> > @@ -394,11 +394,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> >
> > mutex_lock(&data->mutex);
> >
> > - if (ret < 0)
> > + if (ret)
> > goto out_disable_irq;
>
> Why is this condition checked after grabbing the mutex? Shouldn't it be
> checked before grabbing the mutex? Or is that supposed to be a
> mutex_unlock()?
We acquire the lock before jumping to out_disable_irq which is before
a mutex_unlock()
>
> >
> > ret = sx9310_read_prox_data(data, chan, &rawval);
> > - if (ret < 0)
> > + if (ret)
> > goto out_disable_irq;
> >
> > *val = sign_extend32(be16_to_cpu(rawval),

2020-07-28 21:30:15

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 04/15] iio: sx9310: Remove acpi and of table macros

Quoting Daniel Campello (2020-07-28 13:47:15)
> On Tue, Jul 28, 2020 at 12:09 PM Andy Shevchenko
> <[email protected]> wrote:
> >
> > On Tue, Jul 28, 2020 at 6:16 PM Daniel Campello <[email protected]> wrote:
> > >
> > > Avoids unused warnings due to acpi/of table macros.
> > >
> >
> > At the same time I would check if mod_devicetable.h is included.
> I did the following and no error showed up:
> #ifndef LINUX_MOD_DEVICETABLE_H
> #error Missing include
> #endif

That's fine, but it's usually better to avoid implicit include
dependencies so that order of includes doesn't matter and so that if the
headers change outside of this driver this driver doesn't have to be
fixed to include something it needs like mod_devicetable.h

2020-07-28 21:30:21

by Daniel Campello

[permalink] [raw]
Subject: Re: [PATCH 06/15] iio: sx9310: Align memory

On Tue, Jul 28, 2020 at 12:11 PM Andy Shevchenko
<[email protected]> wrote:
>
> On Tue, Jul 28, 2020 at 6:15 PM Daniel Campello <[email protected]> wrote:
> >
> > Use __aligned(8) to ensure that the timestamp is correctly aligned
> > when we call push_to_buffers
> >
> > Signed-off-by: Daniel Campello <[email protected]>
> > ---
> >
> > drivers/iio/proximity/sx9310.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> > index de52afd7c13333..fb5c16f2aa6b1a 100644
> > --- a/drivers/iio/proximity/sx9310.c
> > +++ b/drivers/iio/proximity/sx9310.c
> > @@ -131,8 +131,8 @@ struct sx9310_data {
> > */
> > bool prox_stat[SX9310_NUM_CHANNELS];
> > bool trigger_enabled;
> > - __be16 buffer[SX9310_NUM_CHANNELS +
> > - 4]; /* 64-bit data + 64-bit timestamp */
> > + /* 64-bit data + 64-bit timestamp buffer */
> > + __be16 buffer[SX9310_NUM_CHANNELS + 4] __aligned(8);
>
> If the data amount (channels) is always the same, please, use struct approach.
> Otherwise put a comment explaining dynamic data.
I'm not sure what you mean here. I have a comment above for the size
of the array.
>
> --
> With Best Regards,
> Andy Shevchenko

2020-07-28 21:33:33

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 10/15] iio: sx9310: Simplify error return handling

Quoting Daniel Campello (2020-07-28 14:23:29)
> On Tue, Jul 28, 2020 at 1:40 PM Stephen Boyd <[email protected]> wrote:
> >
> > Quoting Daniel Campello (2020-07-28 08:12:53)
> > > @@ -368,13 +368,13 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
> > > static int sx9310_read_proximity(struct sx9310_data *data,
> > > const struct iio_chan_spec *chan, int *val)
> > > {
> > > - int ret = 0;
> > > + int ret;
> > > __be16 rawval;
> > >
> > > mutex_lock(&data->mutex);
> > >
> > > ret = sx9310_get_read_channel(data, chan->channel);
> > > - if (ret < 0)
> > > + if (ret)
> > > goto out;
> > >
> > > if (data->client->irq) {
> > > @@ -394,11 +394,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> > >
> > > mutex_lock(&data->mutex);
> > >
> > > - if (ret < 0)
> > > + if (ret)
> > > goto out_disable_irq;
> >
> > Why is this condition checked after grabbing the mutex? Shouldn't it be
> > checked before grabbing the mutex? Or is that supposed to be a
> > mutex_unlock()?
> We acquire the lock before jumping to out_disable_irq which is before
> a mutex_unlock()

Does this function need to hold the mutex lock around get/put_read_channel?
It drops the lock while waiting and then regrabs it which seems to
imply that another reader could come in and try to get the channel again
during the wait. So put another way, it may be simpler to shorten the
lock area and then bail out of this function to a place where the lock
isn't held already on the return path.

2020-07-28 22:09:07

by Daniel Campello

[permalink] [raw]
Subject: Re: [PATCH 10/15] iio: sx9310: Simplify error return handling

On Tue, Jul 28, 2020 at 3:32 PM Stephen Boyd <[email protected]> wrote:
>
> Quoting Daniel Campello (2020-07-28 14:23:29)
> > On Tue, Jul 28, 2020 at 1:40 PM Stephen Boyd <[email protected]> wrote:
> > >
> > > Quoting Daniel Campello (2020-07-28 08:12:53)
> > > > @@ -368,13 +368,13 @@ static int sx9310_wait_for_sample(struct sx9310_data *data)
> > > > static int sx9310_read_proximity(struct sx9310_data *data,
> > > > const struct iio_chan_spec *chan, int *val)
> > > > {
> > > > - int ret = 0;
> > > > + int ret;
> > > > __be16 rawval;
> > > >
> > > > mutex_lock(&data->mutex);
> > > >
> > > > ret = sx9310_get_read_channel(data, chan->channel);
> > > > - if (ret < 0)
> > > > + if (ret)
> > > > goto out;
> > > >
> > > > if (data->client->irq) {
> > > > @@ -394,11 +394,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> > > >
> > > > mutex_lock(&data->mutex);
> > > >
> > > > - if (ret < 0)
> > > > + if (ret)
> > > > goto out_disable_irq;
> > >
> > > Why is this condition checked after grabbing the mutex? Shouldn't it be
> > > checked before grabbing the mutex? Or is that supposed to be a
> > > mutex_unlock()?
> > We acquire the lock before jumping to out_disable_irq which is before
> > a mutex_unlock()
>
> Does this function need to hold the mutex lock around get/put_read_channel?
Yes, both get/put_read_channel and get/put_event_channel use
sx9310_update_chan_en which is updating data->chan_{read,event}
bitmaps.
> It drops the lock while waiting and then regrabs it which seems to
> imply that another reader could come in and try to get the channel again
> during the wait. So put another way, it may be simpler to shorten the
> lock area and then bail out of this function to a place where the lock
> isn't held already on the return path.

2020-07-28 22:09:48

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH 01/15] dt-bindings: iio: Add bindings for sx9310 sensor

Quoting Daniel Campello (2020-07-28 08:12:44)
> diff --git a/Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml b/Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml
> new file mode 100644
> index 00000000000000..ba734ee868c77f
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml
> @@ -0,0 +1,60 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/iio/proximity/semtech,sx9310.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Semtech's SX9310 capacitive proximity sensor
> +
> +maintainers:
> + - Daniel Campello <[email protected]>
> +
> +description: |
> + Semtech's SX9310/SX9311 capacitive proximity/button solution.
> +
> + Specifications about the devices can be found at:
> + https://www.semtech.com/products/smart-sensing/sar-sensors/sx9310
> +
> +properties:
> + compatible:
> + enum:
> + - semtech,sx9310
> + - semtech,sx9311
> +
> + reg:
> + maxItems: 1
> +
> + interrupts:
> + description:
> + The sole interrupt generated by the device used to announce the
> + preceding reading request has finished and that data is
> + available or that a close/far proximity event has happened.
> + maxItems: 1
> +
> + vdd-supply:
> + description: Main power supply
> +
> + svdd-supply:
> + description: Host interface power supply

I think we need to add #io-channel-cells = <1> here as a required
property.

"#io-channel-cells":
const: 1

> +
> +required:
> + - compatible
> + - reg

And

- "#io-channel-cells"

> +
> +additionalProperties: false
> +

2020-07-28 23:08:15

by Daniel Campello

[permalink] [raw]
Subject: [PATCH v2 00/14] sx9310 iio driver updates

The first patch resends the DT binding for the driver that was merged in
v5.8-rc1 with a small change to update for proper regulators. The second
through the eleventh patch fixes several issues dropped from v8 to v9
when the initial patch was merged. The twelveth patch fixes a few
printks that are missing newlines and should be totally non-trivial to
apply. The thirteenth patch drops channel_users because it's unused. The
final patch adds support to enable the svdd and vdd supplies so that
this driver can work on a board where the svdd supply isn't enabled at
boot and needs to be turned on before this driver starts to communicate
with the chip.

Changes in v2:
- Added #io-channel-cells as a required property
- Reordered error handling on sx9310_resume()
- Added #include <linux/mod_devicetable.h>
- Added '\n' to dev_err()
- Fixed commit message from "iio: sx9310: Align memory"
- Changed prox_stat to chan_prox_stat bitmap.
- Fixed dev_err() message
- Added '\n' to dev_err()

Daniel Campello (12):
dt-bindings: iio: Add bindings for sx9310 sensor
iio: sx9310: Update macros declarations
iio: sx9310: Fix irq handling
iio: sx9310: Remove acpi and of table macros
iio: sx9310: Change from .probe to .probe_new
iio: sx9310: Fixes various memory handling
iio: sx9310: Use long instead of int for channel bitmaps
iio: sx9310: Use regmap_read_poll_timeout() for compensation
iio: sx9310: Update copyright
iio: sx9310: Simplify error return handling
iio: sx9310: Use variable to hold &client->dev
iio: sx9310: Miscellaneous format fixes

Stephen Boyd (2):
iio: sx9310: Drop channel_users[]
iio: sx9310: Enable vdd and svdd regulators at probe

.../iio/proximity/semtech,sx9310.yaml | 65 +++
drivers/iio/proximity/sx9310.c | 424 +++++++++---------
2 files changed, 277 insertions(+), 212 deletions(-)
create mode 100644 Documentation/devicetree/bindings/iio/proximity/semtech,sx9310.yaml

--
2.28.0.163.g6104cc2f0b6-goog

2020-07-28 23:08:43

by Daniel Campello

[permalink] [raw]
Subject: [PATCH v2 03/14] iio: sx9310: Fix irq handling

Fixes enable/disable irq handling at various points. The driver needs to
only enable/disable irqs if there is an actual irq handler installed.

Signed-off-by: Daniel Campello <[email protected]>
---

Changes in v2:
- Reordered error handling on sx9310_resume()

drivers/iio/proximity/sx9310.c | 26 ++++++++++++++++----------
1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 07895d4b935d12..108d82ba81146e 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -376,13 +376,15 @@ static int sx9310_read_proximity(struct sx9310_data *data,
if (ret < 0)
goto out;

- ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
- if (ret < 0)
- goto out_put_channel;
+ if (data->client->irq) {
+ ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
+ if (ret)
+ goto out_put_channel;
+ }

mutex_unlock(&data->mutex);

- if (data->client->irq > 0) {
+ if (data->client->irq) {
ret = wait_for_completion_interruptible(&data->completion);
reinit_completion(&data->completion);
} else {
@@ -401,9 +403,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,
*val = sign_extend32(be16_to_cpu(rawval),
(chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));

- ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
- if (ret < 0)
- goto out_put_channel;
+ if (data->client->irq) {
+ ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
+ if (ret)
+ goto out_put_channel;
+ }

ret = sx9310_put_read_channel(data, chan->channel);
if (ret < 0)
@@ -414,7 +418,8 @@ static int sx9310_read_proximity(struct sx9310_data *data,
return IIO_VAL_INT;

out_disable_irq:
- sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
+ if (data->client->irq)
+ sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
out_put_channel:
sx9310_put_read_channel(data, chan->channel);
out:
@@ -1011,10 +1016,11 @@ static int __maybe_unused sx9310_resume(struct device *dev)

out:
mutex_unlock(&data->mutex);
+ if (ret)
+ return ret;

enable_irq(data->client->irq);
-
- return ret;
+ return 0;
}

static const struct dev_pm_ops sx9310_pm_ops = {
--
2.28.0.163.g6104cc2f0b6-goog

2020-07-28 23:08:49

by Daniel Campello

[permalink] [raw]
Subject: [PATCH v2 11/14] iio: sx9310: Use variable to hold &client->dev

Improves readability by storing &client->dev in a local variable.

Signed-off-by: Daniel Campello <[email protected]>
Reviewed-by: Andy Shevchenko <[email protected]>
Reviewed-by: Stephen Boyd <[email protected]>
---

Changes in v2:
- Added '\n' to dev_err()

drivers/iio/proximity/sx9310.c | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index f43ca6a0acf2fc..517ff76acd00a0 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -885,11 +885,12 @@ static int sx9310_set_indio_dev_name(struct device *dev,
static int sx9310_probe(struct i2c_client *client)
{
int ret;
+ struct device *dev = &client->dev;
struct iio_dev *indio_dev;
struct sx9310_data *data;

- indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
- if (indio_dev == NULL)
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+ if (!indio_dev)
return -ENOMEM;

data = iio_priv(indio_dev);
@@ -903,17 +904,16 @@ static int sx9310_probe(struct i2c_client *client)

ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
if (ret) {
- dev_err(&client->dev, "error in reading WHOAMI register: %d",
- ret);
+ dev_err(dev, "error in reading WHOAMI register: %d\n", ret);
return ret;
}

- ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami);
+ ret = sx9310_set_indio_dev_name(dev, indio_dev, data->whoami);
if (ret)
return ret;

- ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
- indio_dev->dev.parent = &client->dev;
+ ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(dev));
+ indio_dev->dev.parent = dev;
indio_dev->channels = sx9310_channels;
indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
indio_dev->info = &sx9310_info;
@@ -925,7 +925,7 @@ static int sx9310_probe(struct i2c_client *client)
return ret;

if (client->irq) {
- ret = devm_request_threaded_irq(&client->dev, client->irq,
+ ret = devm_request_threaded_irq(dev, client->irq,
sx9310_irq_handler,
sx9310_irq_thread_handler,
IRQF_TRIGGER_LOW | IRQF_ONESHOT,
@@ -933,29 +933,29 @@ static int sx9310_probe(struct i2c_client *client)
if (ret)
return ret;

- data->trig =
- devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
- indio_dev->name, indio_dev->id);
+ data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
+ indio_dev->name,
+ indio_dev->id);
if (!data->trig)
return -ENOMEM;

- data->trig->dev.parent = &client->dev;
+ data->trig->dev.parent = dev;
data->trig->ops = &sx9310_trigger_ops;
iio_trigger_set_drvdata(data->trig, indio_dev);

- ret = devm_iio_trigger_register(&client->dev, data->trig);
+ ret = devm_iio_trigger_register(dev, data->trig);
if (ret)
return ret;
}

- ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
+ ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
iio_pollfunc_store_time,
sx9310_trigger_handler,
&sx9310_buffer_setup_ops);
if (ret)
return ret;

- return devm_iio_device_register(&client->dev, indio_dev);
+ return devm_iio_device_register(dev, indio_dev);
}

static int __maybe_unused sx9310_suspend(struct device *dev)
--
2.28.0.163.g6104cc2f0b6-goog

2020-07-28 23:08:55

by Daniel Campello

[permalink] [raw]
Subject: [PATCH v2 12/14] iio: sx9310: Miscellaneous format fixes

Miscellaneous format fixes throughout the whole file.

Signed-off-by: Daniel Campello <[email protected]>
Reviewed-by: Andy Shevchenko <[email protected]>
Reviewed-by: Stephen Boyd <[email protected]>
---

Changes in v2: None

drivers/iio/proximity/sx9310.c | 28 ++++++++++------------------
1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 517ff76acd00a0..b15ace422862ff 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -91,28 +91,21 @@
#define SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT 0x3c

#define SX9310_REG_SENSOR_SEL 0x30
-
#define SX9310_REG_USE_MSB 0x31
#define SX9310_REG_USE_LSB 0x32
-
#define SX9310_REG_AVG_MSB 0x33
#define SX9310_REG_AVG_LSB 0x34
-
#define SX9310_REG_DIFF_MSB 0x35
#define SX9310_REG_DIFF_LSB 0x36
-
#define SX9310_REG_OFFSET_MSB 0x37
#define SX9310_REG_OFFSET_LSB 0x38
-
#define SX9310_REG_SAR_MSB 0x39
#define SX9310_REG_SAR_LSB 0x3a
-
#define SX9310_REG_I2C_ADDR 0x40
#define SX9310_REG_PAUSE 0x41
#define SX9310_REG_WHOAMI 0x42
#define SX9310_WHOAMI_VALUE 0x01
#define SX9311_WHOAMI_VALUE 0x02
-
#define SX9310_REG_RESET 0x7f
#define SX9310_SOFT_RESET 0xde

@@ -403,7 +396,7 @@ static int sx9310_read_proximity(struct sx9310_data *data,
goto out_disable_irq;

*val = sign_extend32(be16_to_cpu(rawval),
- (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));
+ chan->address == SX9310_REG_DIFF_MSB ? 11 : 15);

if (data->client->irq) {
ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
@@ -433,8 +426,9 @@ static int sx9310_read_proximity(struct sx9310_data *data,
static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
{
unsigned int regval;
- int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
+ int ret;

+ ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
if (ret)
return ret;

@@ -519,10 +513,9 @@ static irqreturn_t sx9310_irq_handler(int irq, void *private)
iio_trigger_poll(data->trig);

/*
- * Even if no event is enabled, we need to wake the thread to
- * clear the interrupt state by reading SX9310_REG_IRQ_SRC. It
- * is not possible to do that here because regmap_read takes a
- * mutex.
+ * Even if no event is enabled, we need to wake the thread to clear the
+ * interrupt state by reading SX9310_REG_IRQ_SRC.
+ * It is not possible to do that here because regmap_read takes a mutex.
*/
return IRQ_WAKE_THREAD;
}
@@ -639,7 +632,7 @@ static int sx9310_write_event_config(struct iio_dev *indio_dev,

static struct attribute *sx9310_attributes[] = {
&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
- NULL,
+ NULL
};

static const struct attribute_group sx9310_attribute_group = {
@@ -970,7 +963,6 @@ static int __maybe_unused sx9310_suspend(struct device *dev)
mutex_lock(&data->mutex);
ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
&data->suspend_ctrl0);
-
if (ret)
goto out;

@@ -1016,21 +1008,21 @@ static const struct dev_pm_ops sx9310_pm_ops = {
static const struct acpi_device_id sx9310_acpi_match[] = {
{ "STH9310", SX9310_WHOAMI_VALUE },
{ "STH9311", SX9311_WHOAMI_VALUE },
- {},
+ {}
};
MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);

static const struct of_device_id sx9310_of_match[] = {
{ .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE },
{ .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE },
- {},
+ {}
};
MODULE_DEVICE_TABLE(of, sx9310_of_match);

static const struct i2c_device_id sx9310_id[] = {
{ "sx9310", SX9310_WHOAMI_VALUE },
{ "sx9311", SX9311_WHOAMI_VALUE },
- {},
+ {}
};
MODULE_DEVICE_TABLE(i2c, sx9310_id);

--
2.28.0.163.g6104cc2f0b6-goog

2020-07-28 23:09:00

by Daniel Campello

[permalink] [raw]
Subject: [PATCH v2 08/14] iio: sx9310: Use regmap_read_poll_timeout() for compensation

Simplify compensation stage by using regmap_read_poll_timeout().

Signed-off-by: Daniel Campello <[email protected]>
Reviewed-by: Andy Shevchenko <[email protected]>
---

Changes in v2:
- Fixed dev_err() message

drivers/iio/proximity/sx9310.c | 25 ++++++++++---------------
1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 1f04ab8507ec62..f7f44fd9198499 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -797,7 +797,7 @@ static const struct sx9310_reg_default sx9310_default_regs[] = {
static int sx9310_init_compensation(struct iio_dev *indio_dev)
{
struct sx9310_data *data = iio_priv(indio_dev);
- int i, ret;
+ int ret;
unsigned int val;
unsigned int ctrl0;

@@ -811,22 +811,17 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
if (ret < 0)
return ret;

- for (i = 100; i >= 0; i--) {
- msleep(20);
- ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
- if (ret < 0)
- goto out;
- if (!(val & SX9310_COMPSTAT_MASK))
- break;
- }
-
- if (i < 0) {
- dev_err(&data->client->dev,
- "initial compensation timed out: 0x%02x", val);
- ret = -ETIMEDOUT;
+ ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
+ !(val & SX9310_REG_STAT1_COMPSTAT_MASK),
+ 20000, 2000000);
+ if (ret) {
+ if (ret == -ETIMEDOUT)
+ dev_err(&data->client->dev,
+ "initial compensation timed out: 0x%02x\n",
+ val);
+ return ret;
}

-out:
regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
return ret;
}
--
2.28.0.163.g6104cc2f0b6-goog

2020-07-28 23:09:23

by Daniel Campello

[permalink] [raw]
Subject: [PATCH v2 05/14] iio: sx9310: Change from .probe to .probe_new

Uses .probe_new in place of .probe. Also uses device_get_match_data()
for whoami matching.

Signed-off-by: Daniel Campello <[email protected]>
Reviewed-by: Andy Shevchenko <[email protected]>
Reviewed-by: Stephen Boyd <[email protected]>
---

Changes in v2:
- Added '\n' to dev_err()

drivers/iio/proximity/sx9310.c | 39 ++++++++++++----------------------
1 file changed, 14 insertions(+), 25 deletions(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 04b646ae8a1009..bb007673f758d5 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -139,7 +139,7 @@ struct sx9310_data {
struct completion completion;
unsigned int chan_read, chan_event;
int channel_users[SX9310_NUM_CHANNELS];
- int whoami;
+ unsigned int whoami;
};

static const struct iio_event_spec sx9310_events[] = {
@@ -860,24 +860,15 @@ static int sx9310_init_device(struct iio_dev *indio_dev)

static int sx9310_set_indio_dev_name(struct device *dev,
struct iio_dev *indio_dev,
- const struct i2c_device_id *id, int whoami)
+ unsigned int whoami)
{
- const struct acpi_device_id *acpi_id;
-
- /* id will be NULL when enumerated via ACPI */
- if (id) {
- if (id->driver_data != whoami)
- dev_err(dev, "WHOAMI does not match i2c_device_id: %s",
- id->name);
- } else if (ACPI_HANDLE(dev)) {
- acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
- if (!acpi_id)
- return -ENODEV;
- if (acpi_id->driver_data != whoami)
- dev_err(dev, "WHOAMI does not match acpi_device_id: %s",
- acpi_id->id);
- } else
+ unsigned int long ddata;
+
+ ddata = (uintptr_t)device_get_match_data(dev);
+ if (ddata != whoami) {
+ dev_err(dev, "WHOAMI does not match device data: %u\n", whoami);
return -ENODEV;
+ }

switch (whoami) {
case SX9310_WHOAMI_VALUE:
@@ -887,15 +878,14 @@ static int sx9310_set_indio_dev_name(struct device *dev,
indio_dev->name = "sx9311";
break;
default:
- dev_err(dev, "unexpected WHOAMI response: %u", whoami);
+ dev_err(dev, "unexpected WHOAMI response: %u\n", whoami);
return -ENODEV;
}

return 0;
}

-static int sx9310_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
+static int sx9310_probe(struct i2c_client *client)
{
int ret;
struct iio_dev *indio_dev;
@@ -921,8 +911,7 @@ static int sx9310_probe(struct i2c_client *client,
return ret;
}

- ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, id,
- data->whoami);
+ ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami);
if (ret < 0)
return ret;

@@ -1035,8 +1024,8 @@ static const struct acpi_device_id sx9310_acpi_match[] = {
MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);

static const struct of_device_id sx9310_of_match[] = {
- { .compatible = "semtech,sx9310" },
- { .compatible = "semtech,sx9311" },
+ { .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE },
+ { .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE },
{},
};
MODULE_DEVICE_TABLE(of, sx9310_of_match);
@@ -1055,7 +1044,7 @@ static struct i2c_driver sx9310_driver = {
.of_match_table = sx9310_of_match,
.pm = &sx9310_pm_ops,
},
- .probe = sx9310_probe,
+ .probe_new = sx9310_probe,
.id_table = sx9310_id,
};
module_i2c_driver(sx9310_driver);
--
2.28.0.163.g6104cc2f0b6-goog

2020-07-29 00:51:42

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v2 00/14] sx9310 iio driver updates

Quoting Daniel Campello (2020-07-28 16:05:06)
> The first patch resends the DT binding for the driver that was merged in
> v5.8-rc1 with a small change to update for proper regulators. The second
> through the eleventh patch fixes several issues dropped from v8 to v9
> when the initial patch was merged. The twelveth patch fixes a few
> printks that are missing newlines and should be totally non-trivial to
> apply. The thirteenth patch drops channel_users because it's unused. The
> final patch adds support to enable the svdd and vdd supplies so that
> this driver can work on a board where the svdd supply isn't enabled at
> boot and needs to be turned on before this driver starts to communicate
> with the chip.

Can you please send this as not an in-reply-to the previous series? My
inbox has a hard time realizing that this is a new patch series.

2020-07-29 06:53:28

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 06/15] iio: sx9310: Align memory

On Wed, Jul 29, 2020 at 12:26 AM Daniel Campello <[email protected]> wrote:
> On Tue, Jul 28, 2020 at 12:11 PM Andy Shevchenko
> <[email protected]> wrote:
> > On Tue, Jul 28, 2020 at 6:15 PM Daniel Campello <[email protected]> wrote:

...

> > > - __be16 buffer[SX9310_NUM_CHANNELS +
> > > - 4]; /* 64-bit data + 64-bit timestamp */
> > > + /* 64-bit data + 64-bit timestamp buffer */
> > > + __be16 buffer[SX9310_NUM_CHANNELS + 4] __aligned(8);
> >
> > If the data amount (channels) is always the same, please, use struct approach.
> > Otherwise put a comment explaining dynamic data.
> I'm not sure what you mean here. I have a comment above for the size
> of the array.

Here [1] was a discussion about commenting on the dynamic amount of
data [see the cover letter and replies to it] in the buffer and the
struct approach [e.g. very first patch in the series].

[1]: https://lore.kernel.org/linux-iio/MN2PR12MB43905A2256F98BB5EFCE7DD3C4770@MN2PR12MB4390.namprd12.prod.outlook.com/T/

--
With Best Regards,
Andy Shevchenko

2020-07-29 08:42:06

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v2 03/14] iio: sx9310: Fix irq handling

Quoting Daniel Campello (2020-07-28 16:05:09)
> Fixes enable/disable irq handling at various points. The driver needs to
> only enable/disable irqs if there is an actual irq handler installed.
>
> Signed-off-by: Daniel Campello <[email protected]>
> ---
>
> Changes in v2:
> - Reordered error handling on sx9310_resume()
>
> drivers/iio/proximity/sx9310.c | 26 ++++++++++++++++----------
> 1 file changed, 16 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 07895d4b935d12..108d82ba81146e 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -376,13 +376,15 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> if (ret < 0)
> goto out;
>
> - ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
> - if (ret < 0)
> - goto out_put_channel;
> + if (data->client->irq) {
> + ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);

I still think it makes more sense to push the if condition inside the
enable/disable irq functions so that the call sites read simpler.

> + if (ret)
> + goto out_put_channel;
> + }
>
> mutex_unlock(&data->mutex);
>
> - if (data->client->irq > 0) {
> + if (data->client->irq) {
> ret = wait_for_completion_interruptible(&data->completion);
> reinit_completion(&data->completion);
> } else {
> @@ -401,9 +403,11 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> *val = sign_extend32(be16_to_cpu(rawval),
> (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));
>
> - ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> - if (ret < 0)
> - goto out_put_channel;
> + if (data->client->irq) {
> + ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> + if (ret)
> + goto out_put_channel;
> + }
>
> ret = sx9310_put_read_channel(data, chan->channel);
> if (ret < 0)
> @@ -414,7 +418,8 @@ static int sx9310_read_proximity(struct sx9310_data *data,
> return IIO_VAL_INT;
>
> out_disable_irq:
> - sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
> + if (data->client->irq)
> + sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);

And so this isn't duplicated check.

> out_put_channel:
> sx9310_put_read_channel(data, chan->channel);
> out:

2020-07-29 09:23:57

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v2 00/14] sx9310 iio driver updates

Quoting Daniel Campello (2020-07-28 16:05:06)
> The first patch resends the DT binding for the driver that was merged in
> v5.8-rc1 with a small change to update for proper regulators. The second
> through the eleventh patch fixes several issues dropped from v8 to v9
> when the initial patch was merged. The twelveth patch fixes a few
> printks that are missing newlines and should be totally non-trivial to
> apply. The thirteenth patch drops channel_users because it's unused. The
> final patch adds support to enable the svdd and vdd supplies so that
> this driver can work on a board where the svdd supply isn't enabled at
> boot and needs to be turned on before this driver starts to communicate
> with the chip.
>

Can you add this patch onto the end?

----8<----
From: Stephen Boyd <[email protected]>
Subject: [PATCH] iio: sx9310: Use irq trigger flags from firmware

We shouldn't need to set default irq trigger flags here as the firmware
should have properly indicated the trigger type, i.e. level low, in the
DT or ACPI tables.

Signed-off-by: Stephen Boyd <[email protected]>
---
drivers/iio/proximity/sx9310.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 24a2360b6314..2106b9141928 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -945,7 +945,7 @@ static int sx9310_probe(struct i2c_client *client)
ret = devm_request_threaded_irq(dev, client->irq,
sx9310_irq_handler,
sx9310_irq_thread_handler,
- IRQF_TRIGGER_LOW | IRQF_ONESHOT,
+ IRQF_ONESHOT,
"sx9310_event", indio_dev);
if (ret)
return ret;
--
Sent by a computer, using git, on the internet