From: Vincent Whitchurch <vincent.whitchurch@axis.com>
To: <jic23@kernel.org>
Cc: <kernel@axis.com>,
Vincent Whitchurch <vincent.whitchurch@axis.com>,
<andy.shevchenko@gmail.com>, <lars@metafoo.de>,
<linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: [PATCH v2 2/5] iio: adc: mcp320x: remove device_index check for TX
Date: Wed, 31 Aug 2022 12:05:03 +0200 [thread overview]
Message-ID: <20220831100506.3368103-3-vincent.whitchurch@axis.com> (raw)
In-Reply-To: <20220831100506.3368103-1-vincent.whitchurch@axis.com>
Replace the device_index switch with a TX value computation based on the
number of channels in the chip_info structure, so that the latter has
all the information needed to handle the variants.
Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com>
---
drivers/iio/adc/mcp320x.c | 46 +++++++++++++++++++--------------------
1 file changed, 23 insertions(+), 23 deletions(-)
diff --git a/drivers/iio/adc/mcp320x.c b/drivers/iio/adc/mcp320x.c
index c71d90babb39..77fb4522a378 100644
--- a/drivers/iio/adc/mcp320x.c
+++ b/drivers/iio/adc/mcp320x.c
@@ -147,29 +147,34 @@ static int mcp3550_convert_rx(struct mcp320x *adc)
return (s32)raw;
}
-static int mcp320x_channel_to_tx_data(int device_index,
- const unsigned int channel, bool differential)
+static int mcp320x_channel_to_tx_data(const struct mcp320x_chip_info *info,
+ const struct iio_chan_spec *channel)
{
int start_bit = 1;
+ bool differential = channel->differential;
+ u8 address = channel->address;
+ /*
+ * This happens to be the same as the last number of the model name for
+ * multi-channel MCP300X and MCP320X.
+ */
+ unsigned int num_nondiff_channels = info->num_channels / 2;
- switch (device_index) {
- case mcp3002:
- case mcp3202:
+ switch (num_nondiff_channels) {
+ case 2:
return ((start_bit << 4) | (!differential << 3) |
- (channel << 2));
- case mcp3004:
- case mcp3204:
- case mcp3008:
- case mcp3208:
+ (address << 2));
+ case 4:
+ case 8:
return ((start_bit << 6) | (!differential << 5) |
- (channel << 2));
+ (address << 2));
default:
- return -EINVAL;
+ return 0;
}
}
-static int mcp320x_adc_conversion(struct mcp320x *adc, u8 channel,
- bool differential, int device_index, int *val)
+static int mcp320x_adc_conversion(struct mcp320x *adc,
+ const struct iio_chan_spec *channel,
+ int *val)
{
const struct mcp320x_chip_info *info = adc->chip_info;
int ret;
@@ -185,8 +190,7 @@ static int mcp320x_adc_conversion(struct mcp320x *adc, u8 channel,
memset(&adc->rx_buf, 0, sizeof(adc->rx_buf));
if (adc->chip_info->num_channels > 1)
- adc->tx_buf = mcp320x_channel_to_tx_data(device_index, channel,
- differential);
+ adc->tx_buf = mcp320x_channel_to_tx_data(info, channel);
ret = spi_sync(adc->spi, &adc->msg);
if (ret < 0)
@@ -203,16 +207,12 @@ static int mcp320x_read_raw(struct iio_dev *indio_dev,
{
struct mcp320x *adc = iio_priv(indio_dev);
int ret = -EINVAL;
- int device_index = 0;
mutex_lock(&adc->lock);
- device_index = spi_get_device_id(adc->spi)->driver_data;
-
switch (mask) {
case IIO_CHAN_INFO_RAW:
- ret = mcp320x_adc_conversion(adc, channel->address,
- channel->differential, device_index, val);
+ ret = mcp320x_adc_conversion(adc, channel, val);
if (ret < 0)
goto out;
@@ -452,8 +452,8 @@ static int mcp320x_probe(struct spi_device *spi)
* conversions without delay between them resets the chip
* and ensures all subsequent conversions succeed.
*/
- mcp320x_adc_conversion(adc, 0, 1, device_index, &ret);
- mcp320x_adc_conversion(adc, 0, 1, device_index, &ret);
+ mcp320x_adc_conversion(adc, &chip_info->channels[0], &ret);
+ mcp320x_adc_conversion(adc, &chip_info->channels[0], &ret);
}
adc->reg = devm_regulator_get(&spi->dev, "vref");
--
2.34.1
next prev parent reply other threads:[~2022-08-31 10:05 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-31 10:05 [PATCH v2 0/5] iio: adc: mcp320x: Add triggered buffer support Vincent Whitchurch
2022-08-31 10:05 ` [PATCH v2 1/5] iio: adc: mcp320x: use callbacks for RX conversion Vincent Whitchurch
2022-08-31 12:40 ` Andy Shevchenko
2022-08-31 14:19 ` Vincent Whitchurch
2022-08-31 20:21 ` Andy Shevchenko
2022-09-04 16:15 ` Jonathan Cameron
2022-09-05 8:27 ` Vincent Whitchurch
2022-09-05 8:38 ` Andy Shevchenko
2022-08-31 10:05 ` Vincent Whitchurch [this message]
2022-08-31 12:42 ` [PATCH v2 2/5] iio: adc: mcp320x: remove device_index check for TX Andy Shevchenko
2022-08-31 10:05 ` [PATCH v2 3/5] iio: adc: mcp320x: use conv_time instead of device_index switch Vincent Whitchurch
2022-08-31 12:43 ` Andy Shevchenko
2022-08-31 10:05 ` [PATCH v2 4/5] iio: adc: mcp320x: use device managed functions Vincent Whitchurch
2022-08-31 12:50 ` Andy Shevchenko
2022-09-04 16:08 ` Jonathan Cameron
2022-08-31 10:05 ` [PATCH v2 5/5] iio: adc: mcp320x: add triggered buffer support Vincent Whitchurch
2022-09-04 16:26 ` Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220831100506.3368103-3-vincent.whitchurch@axis.com \
--to=vincent.whitchurch@axis.com \
--cc=andy.shevchenko@gmail.com \
--cc=jic23@kernel.org \
--cc=kernel@axis.com \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®