The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] iio: dac: rohm-bd79703: Fix full-scale voltages
@ 2026-11-26 13:23 Matti Vaittinen
  2026-11-26 13:31 ` Joshua Crofts
  0 siblings, 1 reply; 3+ messages in thread
From: Matti Vaittinen @ 2026-11-26 13:23 UTC (permalink / raw)
  To: Matti Vaittinen
  Cc: Matti Vaittinen, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 6502 bytes --]

From: Matti Vaittinen <mazziesaccount@gmail.com>

The BD79702 and BD79703 have a VFS pin which can be used to supply a
full-scale voltage to be output with maximum DAC word. The driver
assumes this voltage is the full-scale voltage for all of the DAC
channels. This, however, is not true.

The VFS-pin voltage is only used for the first half of the channels. Eg.
on BD79703, channels AO1 and AO2 use VFS, AO3 and, AO4 use the VCC.
Similarly, the BD79703 channels AO1, AO2, and, AO3 use VFS, AO4, AO5, and
AO6 use the VCC.

Fix the driver to use correct full-scale voltages (VCC instead of VFS) on
the last channels of the BD79702 and BD79703. The BD79700 and BD79701
logic should not be changed.

NOTE: This change is visible to the user-space, it makes the scale a
per-channel property so the sysFS file out_voltage_scale gets replaced
by per-channel variants out_voltageN_scale

Fixes: af6aca656a85 ("iio: dac: Support ROHM BD79703 DAC")
Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
---
 drivers/iio/dac/rohm-bd79703.c | 77 ++++++++++++++++++++++------------
 1 file changed, 51 insertions(+), 26 deletions(-)

diff --git a/drivers/iio/dac/rohm-bd79703.c b/drivers/iio/dac/rohm-bd79703.c
index 127ccc2c4a22..1d58bc1544a1 100644
--- a/drivers/iio/dac/rohm-bd79703.c
+++ b/drivers/iio/dac/rohm-bd79703.c
@@ -41,7 +41,9 @@ static const struct regmap_config bd79703_regmap_config = {
 /* Dynamic driver private data */
 struct bd79703_data {
 	struct regmap *regmap;
-	int vfs;
+	int vcc_mv;
+	int vfs_mv;
+	unsigned int num_vfs_chan;
 };
 
 /* Static, IC type specific data for different variants */
@@ -49,7 +51,11 @@ struct bd7970x_chip_data {
 	const char *name;
 	const struct iio_chan_spec *channels;
 	int num_channels;
-	bool has_vfs;
+	/*
+	 * Some ICs use VFS-pin for full-scale voltage for first N channels.
+	 * We need to know these channels to get correct scale
+	 */
+	unsigned int num_vfs_chan;
 };
 
 static int bd79703_read_raw(struct iio_dev *idev,
@@ -61,7 +67,11 @@ static int bd79703_read_raw(struct iio_dev *idev,
 	if (mask != IIO_CHAN_INFO_SCALE)
 		return -EINVAL;
 
-	*val = data->vfs / 1000;
+	if (data->num_vfs_chan > chan->channel)
+		*val = data->vfs_mv;
+	else
+		*val = data->vcc_mv;
+
 	*val2 = BD79703_DAC_BITS;
 
 	return IIO_VAL_FRACTIONAL_LOG2;
@@ -88,16 +98,26 @@ static const struct iio_info bd79703_info = {
 };
 
 #define BD79703_CHAN_ADDR(_chan, _addr) {			\
+	.type = IIO_VOLTAGE,					\
+	.indexed = 1,						\
+	.output = 1,						\
+	.channel = (_chan),					\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
+			      BIT(IIO_CHAN_INFO_SCALE),		\
+	.address = (_addr),					\
+}
+
+#define BD79703_CHAN(_chan) {					\
 	.type = IIO_VOLTAGE,					\
 	.indexed = 1,						\
 	.output = 1,						\
 	.channel = (_chan),					\
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
-	.address = (_addr),					\
+	.address = (_chan) + 1,					\
 }
 
-#define BD79703_CHAN(_chan) BD79703_CHAN_ADDR((_chan), (_chan) + 1)
+#define BD79703_CHAN_VFS(_chan) BD79703_CHAN_ADDR((_chan), (_chan) + 1)
 
 static const struct iio_chan_spec bd79700_channels[] = {
 	BD79703_CHAN(0),
@@ -115,6 +135,8 @@ static const struct iio_chan_spec bd79701_channels[] = {
  * and 3, but to the channels 0, 1, 4, 5. So the addressing used with SPI
  * accesses is 1, 2, 5 and 6 for them. Thus, they're not constant offset to
  * the channel number as with other IC variants.
+ *
+ * Furthermore, the BD79702 and BD79703 can have different scale for channels.
  */
 static const struct iio_chan_spec bd79702_channels[] = {
 	BD79703_CHAN_ADDR(0, 1),
@@ -124,40 +146,40 @@ static const struct iio_chan_spec bd79702_channels[] = {
 };
 
 static const struct iio_chan_spec bd79703_channels[] = {
-	BD79703_CHAN(0),
-	BD79703_CHAN(1),
-	BD79703_CHAN(2),
-	BD79703_CHAN(3),
-	BD79703_CHAN(4),
-	BD79703_CHAN(5),
+	BD79703_CHAN_VFS(0),
+	BD79703_CHAN_VFS(1),
+	BD79703_CHAN_VFS(2),
+	BD79703_CHAN_VFS(3),
+	BD79703_CHAN_VFS(4),
+	BD79703_CHAN_VFS(5),
 };
 
 static const struct bd7970x_chip_data bd79700_chip_data = {
 	.name = "bd79700",
 	.channels = bd79700_channels,
 	.num_channels = ARRAY_SIZE(bd79700_channels),
-	.has_vfs = false,
+	.num_vfs_chan = 0,
 };
 
 static const struct bd7970x_chip_data bd79701_chip_data = {
 	.name = "bd79701",
 	.channels = bd79701_channels,
 	.num_channels = ARRAY_SIZE(bd79701_channels),
-	.has_vfs = false,
+	.num_vfs_chan = 0,
 };
 
 static const struct bd7970x_chip_data bd79702_chip_data = {
 	.name = "bd79702",
 	.channels = bd79702_channels,
 	.num_channels = ARRAY_SIZE(bd79702_channels),
-	.has_vfs = true,
+	.num_vfs_chan = 2,
 };
 
 static const struct bd7970x_chip_data bd79703_chip_data = {
 	.name = "bd79703",
 	.channels = bd79703_channels,
 	.num_channels = ARRAY_SIZE(bd79703_channels),
-	.has_vfs = true,
+	.num_vfs_chan = 3,
 };
 
 static int bd79703_probe(struct spi_device *spi)
@@ -184,23 +206,26 @@ static int bd79703_probe(struct spi_device *spi)
 				     "Failed to initialize Regmap\n");
 
 	/*
-	 * BD79703 has a separate VFS pin, whereas the BD79700 and BD79701 use
-	 * VCC for their full-scale output voltage.
+	 * BD79702 and BD79703 have a separate VFS pin. On these ICs, the VFS
+	 * pin provides the full-scale output voltage for half of the channels,
+	 * and use VCC for the rest.
+	 * The BD79700 and BD79701 use VCC for their full-scale output voltage
+	 * for all of the channels.
 	 */
-	if (cd->has_vfs) {
-		ret = devm_regulator_get_enable(dev, "vcc");
-		if (ret)
-			return dev_err_probe(dev, ret, "Failed to enable VCC\n");
+	ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Failed to enable VCC\n");
+
+	data->vcc_mv = ret / 1000;
 
+	if (cd->num_vfs_chan > 0) {
 		ret = devm_regulator_get_enable_read_voltage(dev, "vfs");
 		if (ret < 0)
 			return dev_err_probe(dev, ret, "Failed to get Vfs\n");
-	} else {
-		ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
-		if (ret < 0)
-			return dev_err_probe(dev, ret, "Failed to get VCC\n");
+
+		data->vfs_mv = ret / 1000;
+		data->num_vfs_chan = cd->num_vfs_chan;
 	}
-	data->vfs = ret;
 
 	idev->channels = cd->channels;
 	idev->num_channels = cd->num_channels;

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.56.1


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: dac: rohm-bd79703: Fix full-scale voltages
  2026-11-26 13:23 [PATCH] iio: dac: rohm-bd79703: Fix full-scale voltages Matti Vaittinen
@ 2026-11-26 13:31 ` Joshua Crofts
  2026-11-27  6:34   ` Matti Vaittinen
  0 siblings, 1 reply; 3+ messages in thread
From: Joshua Crofts @ 2026-11-26 13:31 UTC (permalink / raw)
  To: Matti Vaittinen
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel

On 26-Nov-26 14:23, Matti Vaittinen wrote:

...

> diff --git a/drivers/iio/dac/rohm-bd79703.c b/drivers/iio/dac/rohm-bd79703.c
> index 127ccc2c4a22..1d58bc1544a1 100644
> --- a/drivers/iio/dac/rohm-bd79703.c
> +++ b/drivers/iio/dac/rohm-bd79703.c
> @@ -41,7 +41,9 @@ static const struct regmap_config bd79703_regmap_config = {
>  /* Dynamic driver private data */
>  struct bd79703_data {
>  	struct regmap *regmap;
> -	int vfs;
> +	int vcc_mv;

_mV is the preferred style to denote SI unit variables.

> +	int vfs_mv;
> +	unsigned int num_vfs_chan;
>  };
>  
>  /* Static, IC type specific data for different variants */
> @@ -49,7 +51,11 @@ struct bd7970x_chip_data {
>  	const char *name;
>  	const struct iio_chan_spec *channels;
>  	int num_channels;
> -	bool has_vfs;
> +	/*
> +	 * Some ICs use VFS-pin for full-scale voltage for first N channels.
> +	 * We need to know these channels to get correct scale

Small nit, but missing period at the end.

> +	 */
> +	unsigned int num_vfs_chan;
>  };
>  

...

>  static int bd79703_probe(struct spi_device *spi)
> @@ -184,23 +206,26 @@ static int bd79703_probe(struct spi_device *spi)
>  				     "Failed to initialize Regmap\n");
>  
>  	/*
> -	 * BD79703 has a separate VFS pin, whereas the BD79700 and BD79701 use
> -	 * VCC for their full-scale output voltage.
> +	 * BD79702 and BD79703 have a separate VFS pin. On these ICs, the VFS
> +	 * pin provides the full-scale output voltage for half of the channels,
> +	 * and use VCC for the rest.
> +	 * The BD79700 and BD79701 use VCC for their full-scale output voltage
> +	 * for all of the channels.
>  	 */
> -	if (cd->has_vfs) {
> -		ret = devm_regulator_get_enable(dev, "vcc");
> -		if (ret)
> -			return dev_err_probe(dev, ret, "Failed to enable VCC\n");
> +	ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
> +	if (ret < 0)
> +		return dev_err_probe(dev, ret, "Failed to enable VCC\n");
> +
> +	data->vcc_mv = ret / 1000;

Better to do ret / (MICRO / MILLI).

>  
> +	if (cd->num_vfs_chan > 0) {
>  		ret = devm_regulator_get_enable_read_voltage(dev, "vfs");
>  		if (ret < 0)
>  			return dev_err_probe(dev, ret, "Failed to get Vfs\n");
> -	} else {
> -		ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
> -		if (ret < 0)
> -			return dev_err_probe(dev, ret, "Failed to get VCC\n");
> +
> +		data->vfs_mv = ret / 1000;

Same here.

-- 
Kind regards,
Joshua Crofts


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: dac: rohm-bd79703: Fix full-scale voltages
  2026-11-26 13:31 ` Joshua Crofts
@ 2026-11-27  6:34   ` Matti Vaittinen
  0 siblings, 0 replies; 3+ messages in thread
From: Matti Vaittinen @ 2026-11-27  6:34 UTC (permalink / raw)
  To: Joshua Crofts
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel

Thanks for the review Joshua. (I appreciate reviewing effort even though 
it may not sound like it.)

On 26/11/2026 15:31, Joshua Crofts wrote:
> On 26-Nov-26 14:23, Matti Vaittinen wrote:
> 
> ...
> 
>> diff --git a/drivers/iio/dac/rohm-bd79703.c b/drivers/iio/dac/rohm-bd79703.c
>> index 127ccc2c4a22..1d58bc1544a1 100644
>> --- a/drivers/iio/dac/rohm-bd79703.c
>> +++ b/drivers/iio/dac/rohm-bd79703.c
>> @@ -41,7 +41,9 @@ static const struct regmap_config bd79703_regmap_config = {
>>   /* Dynamic driver private data */
>>   struct bd79703_data {
>>   	struct regmap *regmap;
>> -	int vfs;
>> +	int vcc_mv;
> 
> _mV is the preferred style to denote SI unit variables.

I have discussed this before, and I disagree. Regular variables have 
been lower case since forever. Capitals have been used to scream "Note 
me, here is something unusal you need to pay attention for" (like 
macros). In my, admittably not always humble enough, opinion, the whole 
"denote SI units" is stupid. I haven't yet seen an engineer who wouldn't 
understand the meaning of vcc_mv in this context. And, to be very blunt, 
if I saw such an engineer, I would rather him stay away from the driver 
code. On the other hand, I have seen engineers who have spent long time 
with kernel code, stop reading to wonder "Hey, we have capitals here, 
what's special with this variable/member". So for me a change from 
vcc_mv to vcc_mV makes this worse.

>> +	int vfs_mv;
>> +	unsigned int num_vfs_chan;
>>   };
>>   
>>   /* Static, IC type specific data for different variants */
>> @@ -49,7 +51,11 @@ struct bd7970x_chip_data {
>>   	const char *name;
>>   	const struct iio_chan_spec *channels;
>>   	int num_channels;
>> -	bool has_vfs;
>> +	/*
>> +	 * Some ICs use VFS-pin for full-scale voltage for first N channels.
>> +	 * We need to know these channels to get correct scale
> 
> Small nit, but missing period at the end.

Thanks.

> 
>> +	 */
>> +	unsigned int num_vfs_chan;
>>   };
>>   
> 
> ...
> 
>>   static int bd79703_probe(struct spi_device *spi)
>> @@ -184,23 +206,26 @@ static int bd79703_probe(struct spi_device *spi)
>>   				     "Failed to initialize Regmap\n");
>>   
>>   	/*
>> -	 * BD79703 has a separate VFS pin, whereas the BD79700 and BD79701 use
>> -	 * VCC for their full-scale output voltage.
>> +	 * BD79702 and BD79703 have a separate VFS pin. On these ICs, the VFS
>> +	 * pin provides the full-scale output voltage for half of the channels,
>> +	 * and use VCC for the rest.
>> +	 * The BD79700 and BD79701 use VCC for their full-scale output voltage
>> +	 * for all of the channels.
>>   	 */
>> -	if (cd->has_vfs) {
>> -		ret = devm_regulator_get_enable(dev, "vcc");
>> -		if (ret)
>> -			return dev_err_probe(dev, ret, "Failed to enable VCC\n");
>> +	ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
>> +	if (ret < 0)
>> +		return dev_err_probe(dev, ret, "Failed to enable VCC\n");
>> +
>> +	data->vcc_mv = ret / 1000;
> 
> Better to do ret / (MICRO / MILLI).

This is another thing I've discussed also in the past. If you genuinely 
stop to think this, why would MICRO / MILLI be better than 1000? Which 
one brings smaller cognitional load, huh? And assigning the result to 
variable vcc_mv makes units explicit in any case. And speaking of SI 
units and scientific accuracy, what actually is MICRO? What is MILLI?

...

Micro is 10^-6! Milli is 10^-3. So, this becomes "ret / (10^-6 / 
10^-3)", which is plain incorrect. So, instead of making things better, 
this obfuscates stuff even further. So please, let's not spread this 
madness any further. (So actually, this should be 1_PER_MICRO / 
1_PER_MILLI - and that'd be even more cognitive load. When did our goal 
changed from making easy-to-understand -code back to making 
fancy-looking code, which is harder to understand, while claiming it to 
be an improvement? That's what these discussions really look to me.)

I agree on using MEGA for 1000000 and even 10 * KILO. This makes sense 
as when the amount of zeroes increases, counting them becomes harder - 
especially with old eyes like mine. But using (MICRO / MILLI) instead of 
1000, that's, in my opinion, just plain stupid. Intentional obfuscation 
of a perfectly clear thing.

I am ready to discuss these points when I submit new features, and I've 
done this quite a lot in the past with Andy ;) But with a fix for a 
pretty severe bug potentially causing unexpected voltages being driven 
out of a device - which may cause a havoc to hardware. I'd rather get 
this fixed ASAP. Maybe you can send an improvement patch with suggested 
styling "fixes" if you feel really strongly about them, and we can 
continue discussion there? I am sure Andy will also join us ;)

Yours,
   -- Matti

-- 
Matti Vaittinen
Linux kernel developer at ROHM Semiconductor
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-27  6:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 13:23 [PATCH] iio: dac: rohm-bd79703: Fix full-scale voltages Matti Vaittinen
2026-11-26 13:31 ` Joshua Crofts
2026-11-27  6:34   ` Matti Vaittinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox