[PATCH next 1/4] iio: sca3000: cache SPI device ID in probe

Harshit Mogalapalli posted 4 patches 1 week, 3 days ago
There is a newer version of this series
[PATCH next 1/4] iio: sca3000: cache SPI device ID in probe
Posted by Harshit Mogalapalli 1 week, 3 days ago
Store the spi_device_id at probe entry and reuse it for the name and
chip info instead of calling spi_get_device_id() repeatedly.

Reuse the local dev pointer for resource managed helpers. While at it,
reshuffle variable list in a reverse Christmas tree order.

No functional change intended.

Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
 drivers/iio/accel/sca3000.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
index 9ef4d6e27466..afe6ef61a53b 100644
--- a/drivers/iio/accel/sca3000.c
+++ b/drivers/iio/accel/sca3000.c
@@ -1439,11 +1439,13 @@ static const struct iio_info sca3000_info = {
 
 static int sca3000_probe(struct spi_device *spi)
 {
-	int ret;
+	const struct spi_device_id *id = spi_get_device_id(spi);
+	struct device *dev = &spi->dev;
 	struct sca3000_state *st;
 	struct iio_dev *indio_dev;
+	int ret;
 
-	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
 	if (!indio_dev)
 		return -ENOMEM;
 
@@ -1451,10 +1453,9 @@ static int sca3000_probe(struct spi_device *spi)
 	spi_set_drvdata(spi, indio_dev);
 	st->us = spi;
 	mutex_init(&st->lock);
-	st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
-					      ->driver_data];
+	st->info = &sca3000_spi_chip_info_tbl[id->driver_data];
 
-	indio_dev->name = spi_get_device_id(spi)->name;
+	indio_dev->name = id->name;
 	indio_dev->info = &sca3000_info;
 	if (st->info->temp_output) {
 		indio_dev->channels = sca3000_channels_with_temp;
@@ -1466,7 +1467,7 @@ static int sca3000_probe(struct spi_device *spi)
 	}
 	indio_dev->modes = INDIO_DIRECT_MODE;
 
-	ret = devm_iio_kfifo_buffer_setup(&spi->dev, indio_dev,
+	ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
 					  &sca3000_ring_setup_ops);
 	if (ret)
 		return ret;
-- 
2.50.1
Re: [PATCH next 1/4] iio: sca3000: cache SPI device ID in probe
Posted by Jonathan Cameron 1 week, 2 days ago
On Fri, 30 Jan 2026 13:43:08 -0800
Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com> wrote:

> Store the spi_device_id at probe entry and reuse it for the name and
> chip info instead of calling spi_get_device_id() repeatedly.
> 
> Reuse the local dev pointer for resource managed helpers. While at it,
> reshuffle variable list in a reverse Christmas tree order.
> 
> No functional change intended.
> 
> Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
Hi Harshit,

A few comments inline but they are all about extra things to do rather
than really being comments on this patch which is fine as a starting point.

> ---
>  drivers/iio/accel/sca3000.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
> index 9ef4d6e27466..afe6ef61a53b 100644
> --- a/drivers/iio/accel/sca3000.c
> +++ b/drivers/iio/accel/sca3000.c
> @@ -1439,11 +1439,13 @@ static const struct iio_info sca3000_info = {
>  
>  static int sca3000_probe(struct spi_device *spi)
>  {
> -	int ret;
> +	const struct spi_device_id *id = spi_get_device_id(spi);

This is probably ok as an intermediate step, but ultimately we
shouldn't be using the spi_device_id at all.

id->driver_data should be a pointer to a named (not in array)
sca3000_chip_info structure and so should the data in the of_match_id
table etc.

Then we use spi_get_device_match_data() which prefers the of_device_id
table entry (which would needed adding).


> +	struct device *dev = &spi->dev;
>  	struct sca3000_state *st;
>  	struct iio_dev *indio_dev;
> +	int ret;
>  
> -	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
>  	if (!indio_dev)
>  		return -ENOMEM;
>  
> @@ -1451,10 +1453,9 @@ static int sca3000_probe(struct spi_device *spi)
>  	spi_set_drvdata(spi, indio_dev);
>  	st->us = spi;
>  	mutex_init(&st->lock);
> -	st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
> -					      ->driver_data];  
> +	st->info = &sca3000_spi_chip_info_tbl[id->driver_data];
>  
> -	indio_dev->name = spi_get_device_id(spi)->name;
> +	indio_dev->name = id->name;

This also needs an update to make it get the name from within the
chip_info structure.  Whilst this works today it can be fragile
if we have dt compatibles that don't match exactly with the spi_device_id
table entries.

>  	indio_dev->info = &sca3000_info;
>  	if (st->info->temp_output) {
>  		indio_dev->channels = sca3000_channels_with_temp;
> @@ -1466,7 +1467,7 @@ static int sca3000_probe(struct spi_device *spi)
>  	}
>  	indio_dev->modes = INDIO_DIRECT_MODE;
>  
> -	ret = devm_iio_kfifo_buffer_setup(&spi->dev, indio_dev,
> +	ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
>  					  &sca3000_ring_setup_ops);
>  	if (ret)
>  		return ret;
Re: [PATCH next 1/4] iio: sca3000: cache SPI device ID in probe
Posted by Harshit Mogalapalli 1 week, 2 days ago
Hi Jonathan,


> Hi Harshit,
> 
> A few comments inline but they are all about extra things to do rather
> than really being comments on this patch which is fine as a starting point.
> 
....
>>   static int sca3000_probe(struct spi_device *spi)
>>   {
>> -	int ret;
>> +	const struct spi_device_id *id = spi_get_device_id(spi);
> 
> This is probably ok as an intermediate step, but ultimately we
> shouldn't be using the spi_device_id at all.
> 
> id->driver_data should be a pointer to a named (not in array)
> sca3000_chip_info structure and so should the data in the of_match_id
> table etc.
> 
> Then we use spi_get_device_match_data() which prefers the of_device_id
> table entry (which would needed adding).
> 
> 
...
>> -	st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
>> -					      ->driver_data];
>> +	st->info = &sca3000_spi_chip_info_tbl[id->driver_data];
>>   
>> -	indio_dev->name = spi_get_device_id(spi)->name;
>> +	indio_dev->name = id->name;
> 
> This also needs an update to make it get the name from within the
> chip_info structure.  Whilst this works today it can be fragile
> if we have dt compatibles that don't match exactly with the spi_device_id
> table entries.
> 

Thanks a lot for the review, will try to make this change on top of 
these refactors.

Regards,
Harshit