2022-06-23 19:18:14

by Marcus Folkesson

[permalink] [raw]
Subject: [PATCH 01/10] iio: adc: mcp3911: correct "microchip,device-addr" property

Go for the right property name that is documented in the bindings.

Signed-off-by: Marcus Folkesson <[email protected]>
---
drivers/iio/adc/mcp3911.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c
index 1cb4590fe412..4338552cd0ca 100644
--- a/drivers/iio/adc/mcp3911.c
+++ b/drivers/iio/adc/mcp3911.c
@@ -208,7 +208,7 @@ static int mcp3911_config(struct mcp3911 *adc)
u32 configreg;
int ret;

- device_property_read_u32(dev, "device-addr", &adc->dev_addr);
+ device_property_read_u32(dev, "microchip,device-addr", &adc->dev_addr);
if (adc->dev_addr > 3) {
dev_err(&adc->spi->dev,
"invalid device address (%i). Must be in range 0-3.\n",
--
2.36.1


2022-06-23 19:18:55

by Marcus Folkesson

[permalink] [raw]
Subject: [PATCH 07/10] iio: adc: mcp3911: use correct formula for AD conversion

The ADC conversion is actually not rail-to-rail but with a factor 1.5.
Make use of this factor when calculating actual voltage.

Signed-off-by: Marcus Folkesson <[email protected]>
---
drivers/iio/adc/mcp3911.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c
index 65831bef12f6..9e365947d285 100644
--- a/drivers/iio/adc/mcp3911.c
+++ b/drivers/iio/adc/mcp3911.c
@@ -48,8 +48,8 @@
#define MCP3911_CHANNEL(x) (MCP3911_REG_CHANNEL0 + x * 3)
#define MCP3911_OFFCAL(x) (MCP3911_REG_OFFCAL_CH0 + x * 6)

-/* Internal voltage reference in uV */
-#define MCP3911_INT_VREF_UV 1200000
+/* Internal voltage reference in mV */
+#define MCP3911_INT_VREF_MV 1200

#define MCP3911_REG_READ(reg, id) ((((reg) << 1) | ((id) << 5) | (1 << 0)) & 0xff)
#define MCP3911_REG_WRITE(reg, id) ((((reg) << 1) | ((id) << 5) | (0 << 0)) & 0xff)
@@ -178,11 +178,18 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev,

*val = ret / 1000;
} else {
- *val = MCP3911_INT_VREF_UV;
+ *val = MCP3911_INT_VREF_MV;
}

- *val2 = 24;
- ret = IIO_VAL_FRACTIONAL_LOG2;
+ /*
+ * For 24bit Conversion
+ * Raw = ((Voltage)/(Vref) * 2^23 * Gain * 1.5
+ * Voltage = Raw * (Vref)/(2^23 * Gain * 1.5)
+ */
+
+ /* val2 = (2^23 * 1.5) */
+ *val2 = 12582912;
+ ret = IIO_VAL_FRACTIONAL;
break;
}

--
2.36.1

2022-06-23 19:34:02

by Marcus Folkesson

[permalink] [raw]
Subject: [PATCH 04/10] iio: adc: mcp3911: add support for interrupts

Make it possible to read values upon interrupts.
Configure Data Ready Signal Output Pin to either HiZ or push-pull and
use it as interrupt source.

Signed-off-by: Marcus Folkesson <[email protected]>
---
drivers/iio/adc/mcp3911.c | 69 +++++++++++++++++++++++++++++++++++++++
1 file changed, 69 insertions(+)

diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c
index 768cb0203f52..e761feed5303 100644
--- a/drivers/iio/adc/mcp3911.c
+++ b/drivers/iio/adc/mcp3911.c
@@ -26,6 +26,7 @@
#define MCP3911_REG_GAIN 0x09

#define MCP3911_REG_STATUSCOM 0x0a
+#define MCP3911_STATUSCOM_DRHIZ BIT(12)
#define MCP3911_STATUSCOM_CH1_24WIDTH BIT(4)
#define MCP3911_STATUSCOM_CH0_24WIDTH BIT(3)
#define MCP3911_STATUSCOM_EN_OFFCAL BIT(2)
@@ -58,6 +59,7 @@ struct mcp3911 {
struct regulator *vref;
struct clk *clki;
u32 dev_addr;
+ struct iio_trigger *trig;
struct {
u32 channels[2];
s64 ts __aligned(8);
@@ -252,6 +254,17 @@ static const struct iio_info mcp3911_info = {
.write_raw = mcp3911_write_raw,
};

+static irqreturn_t mcp3911_interrupt(int irq, void *dev_id)
+{
+ struct iio_dev *indio_dev = dev_id;
+ struct mcp3911 *adc = iio_priv(indio_dev);
+
+ if (iio_buffer_enabled(indio_dev))
+ iio_trigger_poll(adc->trig);
+
+ return IRQ_HANDLED;
+};
+
static int mcp3911_config(struct mcp3911 *adc)
{
struct device *dev = &adc->spi->dev;
@@ -292,11 +305,30 @@ static int mcp3911_config(struct mcp3911 *adc)
return mcp3911_write(adc, MCP3911_REG_CONFIG, configreg, 2);
}

+
+static int mcp3911_set_trigger_state(struct iio_trigger *trig, bool enable)
+{
+ struct mcp3911 *adc = iio_trigger_get_drvdata(trig);
+
+ if (enable)
+ enable_irq(adc->spi->irq);
+ else
+ disable_irq(adc->spi->irq);
+
+ return 0;
+}
+
+static const struct iio_trigger_ops mcp3911_trigger_ops = {
+ .validate_device = iio_trigger_validate_own_device,
+ .set_trigger_state = mcp3911_set_trigger_state,
+};
+
static int mcp3911_probe(struct spi_device *spi)
{
struct iio_dev *indio_dev;
struct mcp3911 *adc;
int ret;
+ bool dr_hiz;

indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
if (!indio_dev)
@@ -346,6 +378,17 @@ static int mcp3911_probe(struct spi_device *spi)
if (ret)
goto clk_disable;

+ dr_hiz = device_property_read_bool(&adc->spi->dev, "microchip,data-ready-hiz");
+ if (dr_hiz)
+ ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ,
+ 0, 2);
+ else
+ ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ,
+ MCP3911_STATUSCOM_DRHIZ, 2);
+
+ if (ret < 0)
+ goto clk_disable;
+
indio_dev->name = spi_get_device_id(spi)->name;
indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_TRIGGERED;
indio_dev->info = &mcp3911_info;
@@ -356,6 +399,32 @@ static int mcp3911_probe(struct spi_device *spi)

mutex_init(&adc->lock);

+ if (spi->irq > 0) {
+ adc->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
+ indio_dev->name,
+ iio_device_id(indio_dev));
+ if (!adc->trig)
+ goto clk_disable;
+
+ adc->trig->ops = &mcp3911_trigger_ops;
+ iio_trigger_set_drvdata(adc->trig, adc);
+ ret = devm_iio_trigger_register(&spi->dev, adc->trig);
+ if (ret)
+ goto clk_disable;
+
+ /*
+ * The device generates interrupts as long as it is powered up.
+ * Some platforms might not allow the option to power it down so
+ * don't enable the interrupt to avoid extra load on the system
+ */
+ ret = devm_request_irq(&spi->dev, spi->irq,
+ &mcp3911_interrupt,
+ IRQF_TRIGGER_FALLING | IRQF_NO_AUTOEN | IRQF_ONESHOT,
+ indio_dev->name, indio_dev);
+ if (ret)
+ goto clk_disable;
+ }
+
ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
NULL,
mcp3911_trigger_handler, NULL);
--
2.36.1

2022-06-23 19:34:09

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 01/10] iio: adc: mcp3911: correct "microchip,device-addr" property

On Thu, Jun 23, 2022 at 7:40 PM Marcus Folkesson
<[email protected]> wrote:
>
> Go for the right property name that is documented in the bindings.

If the driver is already for a while in the kernel, I'm afraid we may
not do this, since it's part of ABI (firmware <--> OS). You can add a
new property and try it first.

--
With Best Regards,
Andy Shevchenko

2022-06-23 19:42:51

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 04/10] iio: adc: mcp3911: add support for interrupts

On Thu, Jun 23, 2022 at 7:40 PM Marcus Folkesson
<[email protected]> wrote:
>
> Make it possible to read values upon interrupts.
> Configure Data Ready Signal Output Pin to either HiZ or push-pull and
> use it as interrupt source.

...

> }
>
> +

Unwanted blank line.

> +static int mcp3911_set_trigger_state(struct iio_trigger *trig, bool enable)
> +{

> + bool dr_hiz;

As far as I can see you don't need this variable, just call if
(device_property_...(...)).

> + dr_hiz = device_property_read_bool(&adc->spi->dev, "microchip,data-ready-hiz");
> + if (dr_hiz)
> + ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ,
> + 0, 2);
> + else
> + ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM, MCP3911_STATUSCOM_DRHIZ,
> + MCP3911_STATUSCOM_DRHIZ, 2);

> +

Unneeded blank line.

> + if (ret < 0)
> + goto clk_disable;


--
With Best Regards,
Andy Shevchenko

2022-06-24 12:14:34

by Marcus Folkesson

[permalink] [raw]
Subject: Re: [PATCH 04/10] iio: adc: mcp3911: add support for interrupts


On Thu, Jun 23, 2022 at 09:04:51PM +0200, Andy Shevchenko wrote:
> Unwanted blank line.
>

Removed

> > +static int mcp3911_set_trigger_state(struct iio_trigger *trig, bool enable)
> > +{
>
> > + bool dr_hiz;
>
> As far as I can see you don't need this variable, just call if
> (device_property_...(...)).

Fixed

>
> Unneeded blank line.

Removed

Thanks,
Marcus Folkesson


Attachments:
(No filename) (409.00 B)
signature.asc (849.00 B)
Download all attachments