Skip to contentSkip to conversation

Refactor Microchip MCP47FEB02 I2C driver in separate modules to add support for MCP48FEB02 SPI driver

PATCH Superseded Ariana Lazar 4 participants 22 messages Aug 19 – Aug 23

Subsystems (3)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Ariana Lazar

author maintainer
#0 lore raw

v4 cover Refactor Microchip MCP47FEB02 I2C driver in separate modules to add support for MCP48FEB02 SPI driver

Refactor I2C driver implementation into separate modules in order to add support for SPI MCP48FxBy1/2/4/8 DAC family on top of the I2C implementation. The I2C and SPI devices have the same memory map and supported functionalities.

---
Changes in v4:
- remove unused headers
- add missing header types.h in protocol files
- create 6 precursor commits before the refactor patch for handling:
- comment typo
- renaming command mask define
- for an invalid label use a debug message instead of probe error
- correct return value in store_eeprom_store() for false state case
- correct channel gain formula
- read DAC value register to initialize channel data struct field at
probe
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- renumbering the patch version to include the first patch submision
- fix review comments device tree binding:
- change 'additionalProperties' to unevaluatedProperties
- add else branch with spi-max-frequency in the condition which checks
if a device has SPI interface
- correct SPI address example
- add clock polarity and clock phase properties for SPI devices
- enumerate I2C devices in lexicographic order from the description section
- specify supported SPI modes using dependencies
- fix review comments driver:
- remove unneccesarry libraries and add the missing ones
- correct comment typo
- return -ENODEV using dev_err_probe() in protocol probe functions
- use named initializers
- move defines for registers and enums from the header to the core file
- add dev_set_drvdata() in probe()
- move mutex aquiring from mcp47feb02_write_to_eeprom() to
store_eeprom_store()
- write correct value into Vref register in resume()
- Link to v2: https://lore.kernel.org/r/[email protected]

v2:
- include in the same patch series the refactoring of the driver and the
support for SPI devices, but in different patches
- remove changes regarding review comments received not related to the
code refactoring
- first version of the combined refactoring I2C and adding support for SPI
driver
- Link to v1: https://lore.kernel.org/all/[email protected]

v1:
- first version which did not separate the refactoring and adding support for SPI devices (into different patches from the same series)

---
Ariana Lazar (9):
iio: dac: mcp47feb02: initialize dac_data field in channel data struct at probe
iio: dac: mcp47feb02: correct gain bitshift at probe
iio: dac: mcp47feb02: correct false state case in store_eeprom_store
iio: dac: mcp47feb02: use dev_dbg for invalid label
iio: dac: mcp47feb02: correct typo from a comment
iio: dac: mcp47feb02: rename command mask define
iio: dac: mcp47feb02: refactor MCP47FEB02 I2C driver into two modules
dt-bindings: iio: dac: add support for MCP48FEB02 SPI
iio: dac: add support for Microchip MCP48FEB02

 .../bindings/iio/dac/microchip,mcp47feb02.yaml     | 221 ++++++++++---
 MAINTAINERS                                        |   5 +-
 drivers/iio/dac/Kconfig                            |  33 +-
 drivers/iio/dac/Makefile                           |   4 +-
 .../iio/dac/{mcp47feb02.c => mcp47feb02-core.c}    | 352 +++------------------
 drivers/iio/dac/mcp47feb02-i2c.c                   | 145 +++++++++
 drivers/iio/dac/mcp47feb02-spi.c                   | 145 +++++++++
 drivers/iio/dac/mcp47feb02.h                       |  46 +++
 8 files changed, 585 insertions(+), 366 deletions(-)

---
base-commit: 9c6acc6e6555d34198e45cb0d309569dfbadf929
change-id: 20260625-mcp47feb02_refactor-fa8420ae3282

Best regards,

Signature · 1 line
Ariana Lazar <[email protected]>
author maintainer
#1 lore raw

PATCH v4 1/9 iio: dac: mcp47feb02: initialize dac_data field in channel data struct at probe

Read DAC value register to initialize corresponding field in channel_data struct when the module is inserted.

---
 drivers/iio/dac/mcp47feb02.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/dac/mcp47feb02.c +6 -1
index a823c2a673a26d70e5829cb587034da435af0451..7502959d98eab27941d58b6b961e6e3dee4222e6 100644
@@ -1020,7 +1020,12 @@ static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data)
 	gain_ch = gain_ch & MCP47FEB02_GAIN_BITS_MASK;
 	for_each_set_bit(i, &data->active_channels_mask, data->phys_channels) {
 		struct device *dev = regmap_get_device(data->regmap);
-		unsigned int pd_tmp;
+		unsigned int pd_tmp, dac_val;
+
+		ret = regmap_read(data->regmap, REG_ADDR(i), &dac_val);
+		if (ret)
+			return ret;
+		data->chdata[i].dac_data = dac_val;
 
 		data->chdata[i].ref_mode = (vref_ch >> (2 * i)) & MCP47FEB02_DAC_CTRL_MASK;
 		data->chdata[i].use_2x_gain = (gain_ch >> i)  & MCP47FEB02_GAIN_BIT_MASK;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v4 1/9] iio: dac: mcp47feb02: initialize dac_data field in channel data struct at probe

Quoted 2 lines · Ariana Lazar

Read DAC value register to initialize corresponding field in channel_data struct when the module is inserted.

Since we have a cache enabled for regmap, can we rather read back initial HW values at the probe time for all non-volatile or precise registers?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw

Re: [PATCH v4 1/9] iio: dac: mcp47feb02: initialize dac_data field in channel data struct at probe

Quoted 7 lines · On Wed, 19 Aug 2026 17:17:11 +0300 Andy Shevchenko…
Quoted 2 lines · Ariana Lazar

Read DAC value register to initialize corresponding field in channel_data struct when the module is inserted.

Since we have a cache enabled for regmap, can we rather read back initial HW values at the probe time for all non-volatile or precise registers?

That should indeed be explored. This is also missing any description of 'why' this needs to be read and cached at all.

Jonathan

author maintainer
#4 lore raw

PATCH v4 2/9 iio: dac: mcp47feb02: correct gain bitshift at probe

In reply to Ariana Lazar

Correct the formula for initializing gain struct field for each channel with the correct bits from Gain Register.

---
 drivers/iio/dac/mcp47feb02.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/dac/mcp47feb02.c +1 -2
index 7502959d98eab27941d58b6b961e6e3dee4222e6..bf78618ac2c896b94e494a1ce76ef5b3e520f482 100644
@@ -1017,7 +1017,6 @@ static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data)
 	if (ret)
 		return ret;
 
-	gain_ch = gain_ch & MCP47FEB02_GAIN_BITS_MASK;
 	for_each_set_bit(i, &data->active_channels_mask, data->phys_channels) {
 		struct device *dev = regmap_get_device(data->regmap);
 		unsigned int pd_tmp, dac_val;
@@ -1028,7 +1027,7 @@ static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data)
 		data->chdata[i].dac_data = dac_val;
 
 		data->chdata[i].ref_mode = (vref_ch >> (2 * i)) & MCP47FEB02_DAC_CTRL_MASK;
-		data->chdata[i].use_2x_gain = (gain_ch >> i)  & MCP47FEB02_GAIN_BIT_MASK;
+		data->chdata[i].use_2x_gain = (gain_ch & DAC_GAIN_MASK(i)) ? 1 : 0;
 
 		/*
 		 * Inform the user that the current voltage reference read from the volatile
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v4 2/9] iio: dac: mcp47feb02: correct gain bitshift at probe

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] System suspend and resume forcefully overwrite and lose the user-configured powerdown state.
- [High] System resume corrupts the VREF (voltage reference) configuration by overwriting it with the powerdown mode.
- [High] Sysfs store handler returns 0 on false state, potentially causing userspace infinite loops. --

--- Patch [2]: [PATCH v4 2/9] iio: dac: mcp47feb02: correct gain bitshift at probe --- commit f1be9d71af346cda91f297b65aff2d8b9c5d0198
Author: Ariana Lazar <[email protected]>

iio: dac: mcp47feb02: correct gain bitshift at probe

This commit corrects the formula for initializing the gain struct field for each channel. It ensures the correct bits are extracted from the Gain Register.

Quoted 13 lines
drivers/iio/dac/mcp47feb02.c +0 -1
index 7502959d98eab..bf78618ac2c89 100644
@@ -1017,7 +1017,6 @@ static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data)
 	if (ret)
 		return ret;
 
-	gain_ch = gain_ch & MCP47FEB02_GAIN_BITS_MASK;
 	for_each_set_bit(i, &data->active_channels_mask, data->phys_channels) {
 		struct device *dev = regmap_get_device(data->regmap);
 		unsigned int pd_tmp, dac_val;

drivers/iio/dac/mcp47feb02.c static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data) · line 1033

Quoted 8 lines
drivers/iio/dac/mcp47feb02.c +1 -1
@@ -1028,7 +1027,7 @@ static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data)
 		data->chdata[i].dac_data = dac_val;
 
 		data->chdata[i].ref_mode = (vref_ch >> (2 * i)) & MCP47FEB02_DAC_CTRL_MASK;
-		data->chdata[i].use_2x_gain = (gain_ch >> i)  & MCP47FEB02_GAIN_BIT_MASK;
+		data->chdata[i].use_2x_gain = (gain_ch & DAC_GAIN_MASK(i)) ? 1 : 0;
 
 		/*
 		 * Inform the user that the current voltage reference read from the volatile

While reviewing this patch, I noticed a few pre-existing issues in this file:

drivers/iio/dac/mcp47feb02.c:mcp47feb02_suspend() {
    ...
	for_each_set_bit(ch, &data->active_channels_mask, data->phys_channels) {
		u8 pd_mode;

		data->chdata[ch].powerdown = true;
    ...
}
drivers/iio/dac/mcp47feb02.c:mcp47feb02_resume() {
    ...
	for_each_set_bit(ch, &data->active_channels_mask, data->phys_channels) {
		u8 pd_mode;
		int ret;

		data->chdata[ch].powerdown = false;
    ...
		ret = regmap_update_bits(data->regmap, MCP47FEB02_POWER_DOWN_REG_ADDR,
					 DAC_CTRL_MASK(ch),
					 DAC_CTRL_VAL(ch, MCP47FEB02_NORMAL_OPERATION));
    ...
}

[Severity: High]
This isn't a bug introduced by this patch, but does system suspend and resume forcefully overwrite and lose the user-configured powerdown state?

The state is blindly overwritten with true during suspend and false during resume. The hardware is then unconditionally awakened, ignoring any prior user configuration to keep the channel powered down.

drivers/iio/dac/mcp47feb02.c:mcp47feb02_resume() {
    ...
		pd_mode = data->chdata[ch].powerdown_mode + 1;

		ret = regmap_write(data->regmap, REG_ADDR(ch), data->chdata[ch].dac_data);
		if (ret)
			return ret;

		ret = regmap_update_bits(data->regmap, MCP47FEB02_VREF_REG_ADDR,
					 DAC_CTRL_MASK(ch), DAC_CTRL_VAL(ch, pd_mode));
    ...
}

[Severity: High]
This is a pre-existing issue, but does system resume corrupt the VREF configuration by overwriting it with the powerdown mode?

The pd_mode variable appears to be mistakenly passed as the value for the MCP47FEB02_VREF_REG_ADDR register instead of the correct reference mode.

drivers/iio/dac/mcp47feb02.c:store_eeprom_store() {
    ...
	ret = kstrtobool(buf, &state);
	if (ret)
		return ret;

	if (!state)
		return 0;
    ...
}

[Severity: High]
This isn't a bug introduced by this patch, but is it safe to return 0 here?

Returning 0 from a sysfs store handler instead of the length of the written buffer can trick userspace tools into an infinite loop as they repeatedly try to write the remaining bytes.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH v4 2/9] iio: dac: mcp47feb02: correct gain bitshift at probe

In reply to Ariana Lazar
Quoted 2 lines · Ariana Lazar

Correct the formula for initializing gain struct field for each channel with the correct bits from Gain Register.

This needs more information, ideally with the reference to the datasheet. For example, it's unclear what's wrong with the existing calculations.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#7 lore raw

PATCH v4 3/9 iio: dac: mcp47feb02: correct false state case in store_eeprom_store

In reply to Ariana Lazar

Return len also when the state is false instead of 0.

---
 drivers/iio/dac/mcp47feb02.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/mcp47feb02.c +1 -1
index bf78618ac2c896b94e494a1ce76ef5b3e520f482..2ee981393aa1a2dc0bf65d25ab937132fe2e7bf1 100644
@@ -469,7 +469,7 @@ static ssize_t store_eeprom_store(struct device *dev, struct device_attribute *a
 		return ret;
 
 	if (!state)
-		return 0;
+		return len;
 
 	/*
 	 * Verify DAC Wiper and DAC Configuration are unlocked. If both are disabled,
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH v4 3/9] iio: dac: mcp47feb02: correct false state case in store_eeprom_store

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but mcp47feb02_resume incorrectly writes the powerdown mode to the Voltage Reference (VREF) register, corrupting the channel's reference voltage setting.
- [High] This problem wasn't introduced by this patch, but mcp47feb02_suspend and mcp47feb02_resume overwrite the software powerdown state, causing all channels to be forcefully activated on resume regardless of user configuration.
- [High] This problem wasn't introduced by this patch, but mcp47feb02_init_ctrl_regs fails to identify the powerdown state when the hardware boots in '1kohm_to_gnd' mode.
- [High] This problem wasn't introduced by this patch, but store_eeprom_store performs a sequence of volatile register reads and EEPROM writes without holding the state mutex, leading to a TOCTOU race condition.
- [High] This problem wasn't introduced by this patch, but mcp47feb02_write_raw does not validate the incoming user value against bounds for IIO_CHAN_INFO_RAW.
--

--- Patch [3]: [PATCH v4 3/9] iio: dac: mcp47feb02: correct false state case in store_eeprom_store --- commit dc6f30f8cc576f94a887f7f0e174caf833ebb683
Author: Ariana Lazar <[email protected]>

iio: dac: mcp47feb02: correct false state case in store_eeprom_store

Return len also when the state is false instead of 0.

drivers/iio/dac/mcp47feb02.c static ssize_t store_eeprom_store(struct device *dev, struct device_attribute *a · line 475

Quoted 13 lines
drivers/iio/dac/mcp47feb02.c +1 -1
index bf78618ac2c89..2ee981393aa1a 100644
@@ -469,7 +469,7 @@ static ssize_t store_eeprom_store(struct device *dev, struct device_attribute *a
 		return ret;
 
 	if (!state)
-		return 0;
+		return len;
 
 	/*
 	 * Verify DAC Wiper and DAC Configuration are unlocked. If both are disabled,

[ ... ]

[Severity: High]
This isn't a bug introduced by this patch, but store_eeprom_store performs a sequence of volatile register reads and EEPROM writes without holding the state mutex, leading to a race condition with concurrent state modifications.

Can an inconsistent combination of driver states be written to the NV EEPROM because the data->lock is not held across the snapshot sequence?

drivers/iio/dac/mcp47feb02.c:store_eeprom_store() {
    ...
    ret = mcp47feb02_write_to_eeprom(data, NV_REG_ADDR(i),
                                     data->chdata[i].dac_data);
    ...
}

[Severity: High]
This is a pre-existing issue, but mcp47feb02_resume incorrectly writes the powerdown mode to the Voltage Reference (VREF) register, corrupting the channel's reference voltage setting.

During system resume, does this corrupt the voltage reference configuration because it targets MCP47FEB02_VREF_REG_ADDR instead of the powerdown register?

drivers/iio/dac/mcp47feb02.c:mcp47feb02_resume() {
    ...
    ret = regmap_update_bits(data->regmap, MCP47FEB02_VREF_REG_ADDR,
                             DAC_CTRL_MASK(ch), DAC_CTRL_VAL(ch, pd_mode));
    ...
}

[Severity: High]
This is a pre-existing issue, but mcp47feb02_suspend and mcp47feb02_resume overwrite the software powerdown state, causing all channels to be forcefully activated on resume regardless of user configuration.

Does this unconditionally wake the channel and destroy the user's previously configured powerdown preference?

drivers/iio/dac/mcp47feb02.c:mcp47feb02_suspend() {
    ...
    data->chdata[ch].powerdown = true;
    ...
}
drivers/iio/dac/mcp47feb02.c:mcp47feb02_resume() {
    ...
    data->chdata[ch].powerdown = false;
    ...
}

[Severity: High]
This isn't a bug introduced by this patch, but mcp47feb02_init_ctrl_regs fails to identify the powerdown state when the hardware boots in 1kohm_to_gnd mode.

When pd_tmp is 1, powerdown_mode becomes 0. Does this cause the powerdown check to evaluate to false even though the hardware is in a powerdown state?

drivers/iio/dac/mcp47feb02.c:mcp47feb02_init_ctrl_regs() {
    ...
    data->chdata[i].powerdown = !!(data->chdata[i].powerdown_mode);
    ...
}

[Severity: High]
This isn't a bug introduced by this patch, but mcp47feb02_write_raw does not validate the incoming user value against bounds for IIO_CHAN_INFO_RAW.

Can out-of-bounds values be written directly to the DAC data registers since the user-supplied value is not validated?

drivers/iio/dac/mcp47feb02.c:mcp47feb02_write_raw() {
    ...
    ret = regmap_write(data->regmap, REG_ADDR(ch->address), val);
    ...
}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#9 lore raw

Re: [PATCH v4 3/9] iio: dac: mcp47feb02: correct false state case in store_eeprom_store

In reply to Ariana Lazar
Quoted 1 line · Ariana Lazar

Return len also when the state is false instead of 0.

Why? The main problem with your commit messages is that they answer to the Q "what?" and not "why?". The latter is more important.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#10 lore raw

PATCH v4 4/9 iio: dac: mcp47feb02: use dev_dbg for invalid label

In reply to Ariana Lazar

Correct unneccesarry probe error returned at an invalid label with a debug message instead.

---
 drivers/iio/dac/mcp47feb02.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/dac/mcp47feb02.c +2 -2
index 2ee981393aa1a2dc0bf65d25ab937132fe2e7bf1..e5533fa3c31c0006d170764791177879c1e794b0 100644
@@ -15,6 +15,7 @@
 #include <linux/bits.h>
 #include <linux/bitfield.h>
 #include <linux/delay.h>
+#include <linux/dev_printk.h>
 #include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/iio/iio.h>
@@ -978,8 +979,7 @@ static int mcp47feb02_parse_fw(struct iio_dev *indio_dev,
 
 		ret = fwnode_property_read_string(child, "label", &data->labels[reg]);
 		if (ret)
-			return dev_err_probe(dev, ret, "%pfw: invalid label\n",
-					     fwnode_get_name(child));
+			dev_dbg(dev, "%s: invalid label\n", fwnode_get_name(child));
 
 		chanspec.address = reg;
 		chanspec.channel = reg;
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v4 4/9] iio: dac: mcp47feb02: use dev_dbg for invalid label

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Making the label property optional causes the `label` sysfs attribute to return the literal…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Making the label property optional causes the label sysfs attribute to return the literal string (null) for channels without a label. --

--- Patch [4]: [PATCH v4 4/9] iio: dac: mcp47feb02: use dev_dbg for invalid label --- commit 60f7652ad06abf540ec40667fe14b057bf9a477b
Author: Ariana Lazar <[email protected]>

iio: dac: mcp47feb02: use dev_dbg for invalid label

This commit modifies the firmware parsing logic to tolerate missing or invalid label properties. Instead of aborting the probe with an error, it logs a debug message and continues initialization.

Quoted 4 lines
drivers/iio/dac/mcp47feb02.c +0 -0
index 2ee981393aa1a..e5533fa3c31c0 100644

[ ... ]

drivers/iio/dac/mcp47feb02.c static int mcp47feb02_parse_fw(struct iio_dev *indio_dev, · line 982

Quoted 7 lines
drivers/iio/dac/mcp47feb02.c +1 -2
@@ -978,8 +979,7 @@ static int mcp47feb02_parse_fw(struct iio_dev *indio_dev,
 
 		ret = fwnode_property_read_string(child, "label", &data->labels[reg]);
 		if (ret)
-			return dev_err_probe(dev, ret, "%pfw: invalid label\n",
-					     fwnode_get_name(child));
+			dev_dbg(dev, "%s: invalid label\n", fwnode_get_name(child));

[Severity: Medium]
When the label property is invalid or missing, data->labels[reg] will now remain NULL.

Because the IIO core still registers the label sysfs attribute when the read_label callback is populated, could this introduce a regression where mcp47feb02_read_label() outputs the literal string "(null)"?

drivers/iio/dac/mcp47feb02.c:mcp47feb02_read_label() {
	struct mcp47feb02_data *data = iio_priv(indio_dev);

	return sysfs_emit(label, "%s\n", data->labels[ch->address]);
}

Since sysfs_emit() unconditionally formats a NULL pointer as "(null)", userspace tools might parse "(null)" as the channel's actual label. Should mcp47feb02_read_label() handle the NULL case and return an error code like -ENOENT instead?

Quoted 3 lines
drivers/iio/dac/mcp47feb02.c +0 -0
 		chanspec.address = reg;
 		chanspec.channel = reg;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#12 lore raw

Re: [PATCH v4 4/9] iio: dac: mcp47feb02: use dev_dbg for invalid label

In reply to Ariana Lazar
Quoted 2 lines · Ariana Lazar

Correct unneccesarry probe error returned at an invalid label with a debug message instead.

Why?!

...

drivers/iio/dac/mcp47feb02.c static int mcp47feb02_parse_fw(struct iio_dev *indio_dev, · line 982

Quoted 5 lines
drivers/iio/dac/mcp47feb02.c +1 -2
 		ret = fwnode_property_read_string(child, "label", &data->labels[reg]);
 		if (ret)
-			return dev_err_probe(dev, ret, "%pfw: invalid label\n",
-					     fwnode_get_name(child));
+			dev_dbg(dev, "%s: invalid label\n", fwnode_get_name(child));

My gosh, the patch before this one should really fix the fwnode_get_name() usage here.

			return dev_err_probe(dev, ret, "%pfw: invalid label\n", child);

Otherwise it's a good way to Oops.

I haven't checked the rest, but this is how it has to be when %pfw is used.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#13 lore raw

PATCH v4 5/9 iio: dac: mcp47feb02: correct typo from a comment

In reply to Ariana Lazar

Typo corrected to 'Register' in the comment regarding defined masks for the gain register.

---
 drivers/iio/dac/mcp47feb02.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/mcp47feb02.c +1 -1
index e5533fa3c31c0006d170764791177879c1e794b0..6f084eb71240b475bf85763139ecfafc006da83d 100644
@@ -54,7 +54,7 @@
 #define DAC_CTRL_MASK(ch)				(GENMASK(1, 0) << (2 * (ch)))
 #define DAC_CTRL_VAL(ch, val)				((val) << (2 * (ch)))
 
-/* Gain Control and I2C Slave Address Reguster fields */
+/* Gain Control and I2C Slave Address Register fields */
 #define DAC_GAIN_MASK(ch)				(BIT(0) << (8 + (ch)))
 #define DAC_GAIN_VAL(ch, val)				((val) << (8 + (ch)))
 
Signature · 1 line
2.43.0
author maintainer
#14 lore raw

PATCH v4 6/9 iio: dac: mcp47feb02: rename command mask define

In reply to Ariana Lazar

Use 2 new defines to select the corresponding bits for each channel in VREF and Power Down registers instead of the initial define without a meaningful name.

---
 drivers/iio/dac/mcp47feb02.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/dac/mcp47feb02.c +4 -3
index 6f084eb71240b475bf85763139ecfafc006da83d..8ca67c4021a86a6d5034495c1dbb18772ada354b 100644
@@ -34,8 +34,9 @@
 /* Register addresses must be left shifted with 3 positions in order to append command mask */
 #define MCP47FEB02_DAC0_REG_ADDR			0x00
 #define MCP47FEB02_VREF_REG_ADDR			0x40
+#define MCP47FEB02_GET_VREF_MODE(reg, ch)		(((reg) >> (2 * (ch))) & GENMASK(1, 0))
 #define MCP47FEB02_POWER_DOWN_REG_ADDR			0x48
-#define MCP47FEB02_DAC_CTRL_MASK			GENMASK(1, 0)
+#define MCP47FEB02_GET_POWER_DOWN_MODE(reg, ch)		(((reg) >> (2 * (ch))) & GENMASK(1, 0))
 
 #define MCP47FEB02_GAIN_CTRL_STATUS_REG_ADDR		0x50
 #define MCP47FEB02_GAIN_BIT_MASK			BIT(0)
@@ -1026,7 +1027,7 @@ static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data)
 			return ret;
 		data->chdata[i].dac_data = dac_val;
 
-		data->chdata[i].ref_mode = (vref_ch >> (2 * i)) & MCP47FEB02_DAC_CTRL_MASK;
+		data->chdata[i].ref_mode = MCP47FEB02_GET_VREF_MODE(vref_ch, i);
 		data->chdata[i].use_2x_gain = (gain_ch & DAC_GAIN_MASK(i)) ? 1 : 0;
 
 		/*
@@ -1070,7 +1071,7 @@ static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data)
 			break;
 		}
 
-		pd_tmp = (pd_ch >> (2 * i)) & MCP47FEB02_DAC_CTRL_MASK;
+		pd_tmp = MCP47FEB02_GET_POWER_DOWN_MODE(pd_ch, i);
 		data->chdata[i].powerdown_mode = pd_tmp ? (pd_tmp - 1) : pd_tmp;
 		data->chdata[i].powerdown = !!(data->chdata[i].powerdown_mode);
 	}
Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH v4 6/9] iio: dac: mcp47feb02: rename command mask define

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Incorrect initialization of the powerdown state when the device boots in 1kohm_to_gnd power-down mode. --

--- Patch [6]: [PATCH v4 6/9] iio: dac: mcp47feb02: rename command mask define --- commit 5165e39a3476c86b84b827af722b000649c5b496
Author: Ariana Lazar <[email protected]>

iio: dac: mcp47feb02: rename command mask define

This commit replaces a generic mask definition with two new macros, MCP47FEB02_GET_VREF_MODE and MCP47FEB02_GET_POWER_DOWN_MODE. These improve code readability when extracting channel configurations.

Quoted 4 lines
drivers/iio/dac/mcp47feb02.c +0 -0
index 6f084eb71240b..8ca67c4021a86 100644

[ ... ]

drivers/iio/dac/mcp47feb02.c static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data) · line 1076

Quoted 8 lines
drivers/iio/dac/mcp47feb02.c +1 -1
@@ -1070,7 +1071,7 @@ static int mcp47feb02_init_ctrl_regs(struct mcp47feb02_data *data)
 			break;
 		}
 
-		pd_tmp = (pd_ch >> (2 * i)) & MCP47FEB02_DAC_CTRL_MASK;
+		pd_tmp = MCP47FEB02_GET_POWER_DOWN_MODE(pd_ch, i);
 		data->chdata[i].powerdown_mode = pd_tmp ? (pd_tmp - 1) : pd_tmp;
 		data->chdata[i].powerdown = !!(data->chdata[i].powerdown_mode);

[Severity: High]
This is a pre-existing issue, but does this logic incorrectly set the channel to an active state when the device boots in 1kohm_to_gnd power-down mode?

When the device is configured in 1kohm_to_gnd mode in mcp47feb02_init_ctrl_regs(), pd_tmp evaluates to 1. The preceding line sets powerdown_mode to 0 (1 - 1 = 0). As a result, the check !!(data->chdata[i].powerdown_mode) evaluates to false.

Does this cause the driver to report the channel as active, desynchronizing the software state from the actual hardware state?

Quoted 1 line
drivers/iio/dac/mcp47feb02.c +0 -0
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author maintainer
#16 lore raw

PATCH v4 7/9 iio: dac: mcp47feb02: refactor MCP47FEB02 I2C driver into two modules

In reply to Ariana Lazar

Prepare the driver for the bus-specific code by refactoring into separate files. The renamed file will contain the common DAC functionality shared by the MCP47FxBy1/2/4/8 I2C and MCP48FxBy1/2/4/8 SPI drivers. The MCP47FEB02 driver was refactored into two modules: mcp47feb02-core.c and mcp47feb02-i2c.c in order to prepare the support for SPI MCP48FxBy1/2/4/8 DAC family on top of the current implementation.

---
 MAINTAINERS                                        |   4 +-
 drivers/iio/dac/Kconfig                            |  10 +-
 drivers/iio/dac/Makefile                           |   3 +-
 .../iio/dac/{mcp47feb02.c => mcp47feb02-core.c}    | 327 ++-------------------
 drivers/iio/dac/mcp47feb02-i2c.c                   | 145 +++++++++
 drivers/iio/dac/mcp47feb02.h                       |  46 +++
 6 files changed, 227 insertions(+), 308 deletions(-)
MAINTAINERS +3 -1
index 6b4560681b51cfabf0b0e98fd89b69d8c527228b..fc87609865f26a9f484edc42058f7df659a0fa4f 100644
@@ -16118,7 +16118,9 @@ M:	Ariana Lazar <[email protected]>
 L:	[email protected]
 S:	Supported
 F:	Documentation/devicetree/bindings/iio/dac/microchip,mcp47feb02.yaml
-F:	drivers/iio/dac/mcp47feb02.c
+F:	drivers/iio/dac/mcp47feb02-core.c
+F:	drivers/iio/dac/mcp47feb02-i2c.c
+F:	drivers/iio/dac/mcp47feb02.h
 
 MCP4821 DAC DRIVER
 M:	Anshul Dalal <[email protected]>
drivers/iio/dac/Kconfig +8 -2
index 657c68e75542e47ad5603165bcbc3660e90c6450..cc10d43ffd1e89bbf945ef89ea5af0abe1a91084 100644
@@ -550,8 +550,13 @@ config MCP4728
 	  will be called mcp4728.
 
 config MCP47FEB02
-	tristate "MCP47F(E/V)B01/02/04/08/11/12/14/18/21/22/24/28 DAC driver"
+	tristate
+
+config MCP47FEB02_I2C
+	tristate "MCP47F(E/V)B01/02/04/08/11/12/14/18/21/22/24/28 I2C DAC driver"
 	depends on I2C
+	select REGMAP_I2C
+	select MCP47FEB02
 	help
 	  Say yes here if you want to build the driver for the Microchip:
 	  - 8-bit DAC:
@@ -567,7 +572,8 @@ config MCP47FEB02
 	  (DAC) with I2C interface.
 
 	  To compile this driver as a module, choose M here: the module
-	  will be called mcp47feb02.
+	  will be called mcp47feb02_i2c and you will also get
+	  mcp47feb02_core for the core module.
 
 config MCP4821
 	tristate "MCP4801/02/11/12/21/22 DAC driver"
drivers/iio/dac/Makefile +2 -1
index 0034317984985035f7987a744899924bfd4612e3..28c8646a987cff1ac4d693fe86898d66c901d5bd 100644
@@ -54,7 +54,8 @@ obj-$(CONFIG_MAX5522) += max5522.o
 obj-$(CONFIG_MAX5821) += max5821.o
 obj-$(CONFIG_MCP4725) += mcp4725.o
 obj-$(CONFIG_MCP4728) += mcp4728.o
-obj-$(CONFIG_MCP47FEB02) += mcp47feb02.o
+obj-$(CONFIG_MCP47FEB02) += mcp47feb02-core.o
+obj-$(CONFIG_MCP47FEB02_I2C) += mcp47feb02-i2c.o
 obj-$(CONFIG_MCP4821) += mcp4821.o
 obj-$(CONFIG_MCP4922) += mcp4922.o
 obj-$(CONFIG_STM32_DAC_CORE) += stm32-dac-core.o
drivers/iio/dac/mcp47feb02-core.c renamed from drivers/iio/dac/mcp47feb02.c +23 -304

441 lines not on this page · raw message

drivers/iio/dac/mcp47feb02-i2c.c new file +145 -0

145 lines not on this page · raw message

drivers/iio/dac/mcp47feb02.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.43.0
#17 lore raw

Re: [PATCH v4 7/9] iio: dac: mcp47feb02: refactor MCP47FEB02 I2C driver into two modules

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Making the device tree 'label' property optional causes the literal string '(null)' to be…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Making the device tree 'label' property optional causes the literal string '(null)' to be printed to sysfs. --

--- Patch [7]: [PATCH v4 7/9] iio: dac: mcp47feb02: refactor MCP47FEB02 I2C driver into two modules --- commit ed5f5f581011130b05d16462d5287ab79c40d657
Author: Ariana Lazar <[email protected]>

iio: dac: mcp47feb02: refactor MCP47FEB02 I2C driver into two modules

This commit refactors the MCP47FEB02 driver into a core module and an I2C-specific module to prepare for future SPI support.

Quoted 6 lines
drivers/iio/dac/mcp47feb02-core.c renamed from drivers/iio/dac/mcp47feb02.c +0 -0
rename from drivers/iio/dac/mcp47feb02.c
rename to drivers/iio/dac/mcp47feb02-core.c
index a823c2a673a26..8bee01a823673 100644

[ ... ]

drivers/iio/dac/mcp47feb02-core.c static int mcp47feb02_parse_fw(struct iio_dev *indio_dev, · line 779

Quoted 10 lines
drivers/iio/dac/mcp47feb02-core.c renamed from drivers/iio/dac/mcp47feb02.c +1 -2
@@ -978,8 +773,7 @@ static int mcp47feb02_parse_fw(struct iio_dev *indio_dev,
 
 		ret = fwnode_property_read_string(child, "label", &data->labels[reg]);
 		if (ret)
-			return dev_err_probe(dev, ret, "%pfw: invalid label\n",
-					     fwnode_get_name(child));
+			dev_dbg(dev, "%s: invalid label\n", fwnode_get_name(child));
 
 		chanspec.address = reg;
 		chanspec.channel = reg;

[Severity: Medium]
Will this cause an unintended sysfs output when a channel lacks the label property?

By continuing the probe without an error, data->labels[reg] remains NULL. When userspace later reads the label via sysfs, mcp47feb02_read_label() passes this pointer to sysfs_emit():

mcp47feb02_read_label() {
	...
	return sysfs_emit(label, "%s\n", data->labels[ch->address]);
}

Passing a NULL pointer to sysfs_emit() will print the literal string "(null)\n" to userspace instead of returning a standard error like -ENOENT.

Would it be better to add a NULL check in mcp47feb02_read_label() before emitting the string?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer
#18 lore raw

Re: [PATCH v4 7/9] iio: dac: mcp47feb02: refactor MCP47FEB02 I2C driver into two modules

In reply to Ariana Lazar
Quoted 8 lines · On Wed, 19 Aug 2026 15:41:11 +0300 Ariana Lazar <[email protected]>…

Prepare the driver for the bus-specific code by refactoring into separate files. The renamed file will contain the common DAC functionality shared by the MCP47FxBy1/2/4/8 I2C and MCP48FxBy1/2/4/8 SPI drivers. The MCP47FEB02 driver was refactored into two modules: mcp47feb02-core.c and mcp47feb02-i2c.c in order to prepare the support for SPI MCP48FxBy1/2/4/8 DAC family on top of the current implementation.

A few trivial things inline.

Thanks,

Jonathan

Quoted 14 lines
---
 MAINTAINERS                                        |   4 +-
 drivers/iio/dac/Kconfig                            |  10 +-
 drivers/iio/dac/Makefile                           |   3 +-
 .../iio/dac/{mcp47feb02.c => mcp47feb02-core.c}    | 327 ++-------------------
 drivers/iio/dac/mcp47feb02-i2c.c                   | 145 +++++++++
 drivers/iio/dac/mcp47feb02.h                       |  46 +++
 6 files changed, 227 insertions(+), 308 deletions(-)
MAINTAINERS +0 -0
index 6b4560681b51cfabf0b0e98fd89b69d8c527228b..fc87609865f26a9f484edc42058f7df659a0fa4f 100644

MAINTAINERS M: Ariana Lazar <[email protected]> · line 16124

Quoted 8 lines
MAINTAINERS +3 -1
@@ -16118,7 +16118,9 @@ M:	Ariana Lazar <[email protected]>
 L:	[email protected]
 S:	Supported
 F:	Documentation/devicetree/bindings/iio/dac/microchip,mcp47feb02.yaml
-F:	drivers/iio/dac/mcp47feb02.c
+F:	drivers/iio/dac/mcp47feb02-core.c
+F:	drivers/iio/dac/mcp47feb02-i2c.c
+F:	drivers/iio/dac/mcp47feb02.h
 

Can use wild cards. We should probably do this more often!

F: drivers/iio/dac/mcp47feb02*

....

Quoted 6 lines
drivers/iio/dac/mcp47feb02.h new file +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..64620c865871254f5db373f951ffad3b9c273f29

drivers/iio/dac/mcp47feb02.h line 10

Quoted 10 lines
drivers/iio/dac/mcp47feb02.h new file +10 -0
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+#ifndef __DRIVERS_IIO_DAC_MCP47FEB02_H__
+#define __DRIVERS_IIO_DAC_MCP47FEB02_H__
+
+#include <linux/pm.h>
+#include <linux/types.h>
+
+extern const char * const mcp47feb02_powerdown_modes[];
+
+/* Macro used for generating chip features structures */

That seems kind of obvious. I'd drop the comment!

drivers/iio/dac/mcp47feb02.h line 39

Quoted 9 lines
drivers/iio/dac/mcp47feb02.h new file +9 -0
+#define MCP47FEB02_CHIP_INFO(_name, _channels, _res, _vref1, _eeprom) \
+static const struct mcp47feb02_features _name##_chip_features = { \
+	.name = #_name, \
+	.phys_channels = _channels, \
+	.resolution = _res, \
+	.have_ext_vref1 = _vref1, \
+	.have_eeprom = _eeprom, \
+}
+/* Properties shared by I2C and SPI families */

I wouldn't call the following function a property. Probably just drop this comment as not that useful.

drivers/iio/dac/mcp47feb02.h line 46

Quoted 7 lines
drivers/iio/dac/mcp47feb02.h new file +7 -0
+int mcp47feb02_common_probe(const struct mcp47feb02_features *chip_features,
+			    struct regmap *regmap);
+
+extern const struct dev_pm_ops mcp47feb02_pm_ops;
+
+#endif /* __DRIVERS_IIO_DAC_MCP47FEB02_H__ */
+

Seems to be an extra trailing blank line. Delete it.

author maintainer
#19 lore raw

PATCH v4 8/9 dt-bindings: iio: dac: add support for MCP48FEB02 SPI

In reply to Ariana Lazar

Add the SPI MCP48FxBy1/2/4/8 part numbers, the spi-max-frequency property and a devicetree example for SPI usage to the existing binding.

---
 .../bindings/iio/dac/microchip,mcp47feb02.yaml     | 221 ++++++++++++++++-----
 1 file changed, 173 insertions(+), 48 deletions(-)
Documentation/devicetree/bindings/iio/dac/microchip,mcp47feb02.yaml +173 -48
index d131f136bd15161666932261bcacd6d124b23ede..a86d094f0d4b54f9d9cc39fd54222f5968714668 100644
@@ -4,58 +4,92 @@
 $id: http://devicetree.org/schemas/iio/dac/microchip,mcp47feb02.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: Microchip MCP47F(E/V)B(0/1/2)(1/2/4/8) DAC with I2C Interface Families
+title: Microchip MCP4(7/8)F(E/V)B(0/1/2)(1/2/4/8) DAC with I2C/SPI Interface Families
 
 maintainers:
   - Ariana Lazar <[email protected]>
 
 description: |
-  Datasheet for MCP47FEB01, MCP47FEB11, MCP47FEB21, MCP47FEB02, MCP47FEB12,
-  MCP47FEB22 can be found here:
-    https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20005375A.pdf
-  Datasheet for MCP47FVB01, MCP47FVB11, MCP47FVB21, MCP47FVB02, MCP47FVB12,
-  MCP47FVB22 can be found here:
-    https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20005405A.pdf
-  Datasheet for MCP47FEB04, MCP47FEB14, MCP47FEB24, MCP47FEB08, MCP47FEB18,
-  MCP47FEB28, MCP47FVB04, MCP47FVB14, MCP47FVB24, MCP47FVB08, MCP47FVB18,
-  MCP47FVB28 can be found here:
-    https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/MCP47FXBX48-Data-Sheet-DS200006368A.pdf
-
-  +------------+--------------+-------------+-------------+------------+
-  | Device     |  Resolution  |   Channels  | Vref number | Memory     |
-  |------------|--------------|-------------|-------------|------------|
-  | MCP47FEB01 |     8-bit    |      1      |      1      |   EEPROM   |
-  | MCP47FEB11 |    10-bit    |      1      |      1      |   EEPROM   |
-  | MCP47FEB21 |    12-bit    |      1      |      1      |   EEPROM   |
-  |------------|--------------|-------------|-------------|------------|
-  | MCP47FEB02 |     8-bit    |      2      |      1      |   EEPROM   |
-  | MCP47FEB12 |    10-bit    |      2      |      1      |   EEPROM   |
-  | MCP47FEB22 |    12-bit    |      2      |      1      |   EEPROM   |
-  |------------|--------------|-------------|-------------|------------|
-  | MCP47FVB01 |     8-bit    |      1      |      1      |      RAM   |
-  | MCP47FVB11 |    10-bit    |      1      |      1      |      RAM   |
-  | MCP47FVB21 |    12-bit    |      1      |      1      |      RAM   |
-  |------------|--------------|-------------|-------------|------------|
-  | MCP47FVB02 |     8-bit    |      2      |      1      |      RAM   |
-  | MCP47FVB12 |    10-bit    |      2      |      1      |      RAM   |
-  | MCP47FVB22 |    12-bit    |      2      |      1      |      RAM   |
-  |------------|--------------|-------------|-------------|------------|
-  | MCP47FVB04 |     8-bit    |      4      |      2      |      RAM   |
-  | MCP47FVB14 |    10-bit    |      4      |      2      |      RAM   |
-  | MCP47FVB24 |    12-bit    |      4      |      2      |      RAM   |
-  |------------|--------------|-------------|-------------|------------|
-  | MCP47FVB08 |     8-bit    |      8      |      2      |      RAM   |
-  | MCP47FVB18 |    10-bit    |      8      |      2      |      RAM   |
-  | MCP47FVB28 |    12-bit    |      8      |      2      |      RAM   |
-  |------------|--------------|-------------|-------------|------------|
-  | MCP47FEB04 |     8-bit    |      4      |      2      |   EEPROM   |
-  | MCP47FEB14 |    10-bit    |      4      |      2      |   EEPROM   |
-  | MCP47FEB24 |    12-bit    |      4      |      2      |   EEPROM   |
-  |------------|--------------|-------------|-------------|------------|
-  | MCP47FEB08 |     8-bit    |      8      |      2      |   EEPROM   |
-  | MCP47FEB18 |    10-bit    |      8      |      2      |   EEPROM   |
-  | MCP47FEB28 |    12-bit    |      8      |      2      |   EEPROM   |
-  +------------+--------------+-------------+-------------+------------+
+  Datasheets for MCP47F(E/V)B(0/1/2)(1/2/4/8) DAC with I2C Interface Families:
+   Datasheet for MCP47FEB01, MCP47FEB02, MCP47FEB11, MCP47FEB12, MCP47FEB21,
+   MCP47FEB22 can be found here:
+     https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20005375A.pdf
+   Datasheet for MCP47FVB01, MCP47FVB02, MCP47FVB11, MCP47FVB12, MCP47FVB21,
+   MCP47FVB22 can be found here:
+     https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20005405A.pdf
+   Datasheet for MCP47FEB04, MCP47FEB08, MCP47FEB14, MCP47FEB18, MCP47FEB24,
+   MCP47FEB28, MCP47FVB04, MCP47FVB08, MCP47FVB14, MCP47FVB18, MCP47FVB24,
+   MCP47FVB28 can be found here:
+     https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/MCP47FXBX48-Data-Sheet-DS200006368A.pdf
+   Datasheets for MCP48F(E/V)B(0/1/2)(1/2/4/8) DAC with SPI Interface Families:
+   Datasheet for MCP48FEB01, MCP48FEB02, MCP48FEB11, MCP48FEB12, MCP48FEB21,
+   MCP48FEB22 can be found here:
+     https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20005429B.pdf
+   Datasheet for MCP48FVB01, MCP48FVB02, MCP48FVB11, MCP48FVB12, MCP48FVB21,
+   MCP48FVB22 can be found here:
+     https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20005466A.pdf
+   Datasheet for MCP48FEB04, MCP48FEB14, MCP48FEB24, MCP48FEB08, MCP48FEB18,
+   MCP48FEB28, MCP48FVB04, MCP48FVB14, MCP48FVB24, MCP48FVB08, MCP48FVB18,
+   MCP48FVB28 can be found here:
+     https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/MCP48FXBX4-8-Family-Data-Sheet-DS20006362A.pdf
+
+  +------------+--------------+-------------+-------------+------------+------------+
+  | Device     |  Resolution  |   Channels  | Vref number |   Memory   | Interface  |
+  |------------|--------------|-------------|-------------|------------|------------|
+  | MCP47FEB01 |              |      1      |      1      |   EEPROM   |    I2C     |
+  | MCP47FEB02 |              |      2      |      1      |   EEPROM   |    I2C     |
+  | MCP47FEB04 |              |      4      |      2      |   EEPROM   |    I2C     |
+  | MCP47FEB08 |     8-bit    |      8      |      2      |   EEPROM   |    I2C     |
+  | MCP48FEB01 |              |      1      |      1      |   EEPROM   |    SPI     |
+  | MCP48FEB02 |              |      2      |      1      |   EEPROM   |    SPI     |
+  | MCP48FEB04 |              |      4      |      2      |   EEPROM   |    SPI     |
+  | MCP48FEB08 |              |      8      |      2      |   EEPROM   |    SPI     |
+  |------------|--------------|-------------|-------------|------------|------------|
+  | MCP47FEB11 |              |      1      |      1      |   EEPROM   |    I2C     |
+  | MCP47FEB12 |              |      2      |      1      |   EEPROM   |    I2C     |
+  | MCP47FEB14 |              |      4      |      2      |   EEPROM   |    I2C     |
+  | MCP47FEB18 |    10-bit    |      8      |      2      |   EEPROM   |    I2C     |
+  | MCP48FEB11 |              |      1      |      1      |   EEPROM   |    SPI     |
+  | MCP48FEB12 |              |      2      |      1      |   EEPROM   |    SPI     |
+  | MCP48FEB14 |              |      4      |      2      |   EEPROM   |    SPI     |
+  | MCP48FEB18 |              |      8      |      2      |   EEPROM   |    SPI     |
+  |------------|--------------|-------------|-------------|------------|------------|
+  | MCP47FEB21 |              |      1      |      1      |   EEPROM   |    I2C     |
+  | MCP47FEB22 |              |      2      |      1      |   EEPROM   |    I2C     |
+  | MCP47FEB24 |              |      4      |      2      |   EEPROM   |    I2C     |
+  | MCP47FEB28 |    12-bit    |      8      |      2      |   EEPROM   |    I2C     |
+  | MCP48FEB21 |              |      1      |      1      |   EEPROM   |    SPI     |
+  | MCP48FEB22 |              |      2      |      1      |   EEPROM   |    SPI     |
+  | MCP48FEB24 |              |      4      |      2      |   EEPROM   |    SPI     |
+  | MCP48FEB28 |              |      8      |      2      |   EEPROM   |    SPI     |
+  |------------|--------------|-------------|-------------|------------|------------|
+  | MCP47FVB01 |              |      1      |      1      |   RAM      |    I2C     |
+  | MCP47FVB02 |              |      2      |      1      |   RAM      |    I2C     |
+  | MCP47FVB04 |              |      4      |      2      |   RAM      |    I2C     |
+  | MCP47FVB08 |     8-bit    |      8      |      2      |   RAM      |    I2C     |
+  | MCP48FVB01 |              |      1      |      1      |   RAM      |    SPI     |
+  | MCP48FVB02 |              |      2      |      1      |   RAM      |    SPI     |
+  | MCP48FVB04 |              |      4      |      2      |   RAM      |    SPI     |
+  | MCP48FVB08 |              |      8      |      2      |   RAM      |    SPI     |
+  |------------|--------------|-------------|-------------|------------|------------|
+  | MCP47FVB11 |              |      1      |      1      |   RAM      |    I2C     |
+  | MCP47FVB12 |              |      2      |      1      |   RAM      |    I2C     |
+  | MCP47FVB14 |              |      4      |      2      |   RAM      |    I2C     |
+  | MCP47FVB18 |    10-bit    |      8      |      2      |   RAM      |    I2C     |
+  | MCP48FVB11 |              |      1      |      1      |   RAM      |    SPI     |
+  | MCP48FVB12 |              |      2      |      1      |   RAM      |    SPI     |
+  | MCP48FVB14 |              |      4      |      2      |   RAM      |    SPI     |
+  | MCP48FVB18 |              |      8      |      2      |   RAM      |    SPI     |
+  |------------|--------------|-------------|-------------|------------|------------|
+  | MCP47FVB21 |              |      1      |      1      |   RAM      |    I2C     |
+  | MCP47FVB22 |              |      2      |      1      |   RAM      |    I2C     |
+  | MCP47FVB24 |              |      4      |      2      |   RAM      |    I2C     |
+  | MCP47FVB28 |    12-bit    |      8      |      2      |   RAM      |    I2C     |
+  | MCP48FVB21 |              |      1      |      1      |   RAM      |    SPI     |
+  | MCP48FVB22 |              |      2      |      1      |   RAM      |    SPI     |
+  | MCP48FVB24 |              |      4      |      2      |   RAM      |    SPI     |
+  | MCP48FVB28 |              |      8      |      2      |   RAM      |    SPI     |
+  +------------+--------------+-------------+-------------+------------+------------+
 
 properties:
   compatible:
@@ -84,10 +118,37 @@ properties:
       - microchip,mcp47feb08
       - microchip,mcp47feb18
       - microchip,mcp47feb28
+      - microchip,mcp48feb01
+      - microchip,mcp48feb02
+      - microchip,mcp48feb04
+      - microchip,mcp48feb08
+      - microchip,mcp48feb11
+      - microchip,mcp48feb12
+      - microchip,mcp48feb14
+      - microchip,mcp48feb18
+      - microchip,mcp48feb21
+      - microchip,mcp48feb22
+      - microchip,mcp48feb24
+      - microchip,mcp48feb28
+      - microchip,mcp48fvb01
+      - microchip,mcp48fvb02
+      - microchip,mcp48fvb04
+      - microchip,mcp48fvb08
+      - microchip,mcp48fvb11
+      - microchip,mcp48fvb12
+      - microchip,mcp48fvb14
+      - microchip,mcp48fvb18
+      - microchip,mcp48fvb21
+      - microchip,mcp48fvb22
+      - microchip,mcp48fvb24
+      - microchip,mcp48fvb28
 
   reg:
     maxItems: 1
 
+  spi-max-frequency:
+    maximum: 10000000
+
   "#address-cells":
     const: 1
 
@@ -178,6 +239,21 @@ required:
   - vdd-supply
 
 allOf:
+  - if:
+      properties:
+        compatible:
+          contains:
+            pattern: "^microchip,mcp48f[ev]b[0-2][1248]$"
+    then:
+      $ref: /schemas/spi/spi-peripheral-props.yaml#
+      dependencies:
+        spi-cpha: [ spi-cpol ]
+        spi-cpol: [ spi-cpha ]
+    else:
+      properties:
+        spi-max-frequency: false
+        spi-cpha: false
+        spi-cpol: false
   - if:
       properties:
         compatible:
@@ -189,6 +265,12 @@ allOf:
               - microchip,mcp47fvb01
               - microchip,mcp47fvb11
               - microchip,mcp47fvb21
+              - microchip,mcp48feb01
+              - microchip,mcp48feb11
+              - microchip,mcp48feb21
+              - microchip,mcp48fvb01
+              - microchip,mcp48fvb11
+              - microchip,mcp48fvb21
     then:
       properties:
         lat1-gpios: false
@@ -211,6 +293,12 @@ allOf:
               - microchip,mcp47fvb02
               - microchip,mcp47fvb12
               - microchip,mcp47fvb22
+              - microchip,mcp48feb02
+              - microchip,mcp48feb12
+              - microchip,mcp48feb22
+              - microchip,mcp48fvb02
+              - microchip,mcp48fvb12
+              - microchip,mcp48fvb22
     then:
       properties:
         lat1-gpios: false
@@ -233,6 +321,12 @@ allOf:
               - microchip,mcp47feb04
               - microchip,mcp47feb14
               - microchip,mcp47feb24
+              - microchip,mcp48feb04
+              - microchip,mcp48feb14
+              - microchip,mcp48feb24
+              - microchip,mcp48fvb04
+              - microchip,mcp48fvb14
+              - microchip,mcp48fvb24
     then:
       patternProperties:
         "^channel@[0-3]$":
@@ -251,6 +345,12 @@ allOf:
               - microchip,mcp47feb08
               - microchip,mcp47feb18
               - microchip,mcp47feb28
+              - microchip,mcp48feb08
+              - microchip,mcp48feb18
+              - microchip,mcp48feb28
+              - microchip,mcp48fvb08
+              - microchip,mcp48fvb18
+              - microchip,mcp48fvb28
     then:
       patternProperties:
         "^channel@[0-7]$":
@@ -272,7 +372,7 @@ allOf:
       properties:
         microchip,vref1-buffered: false
 
-additionalProperties: false
+unevaluatedProperties: false
 
 examples:
   - |
@@ -299,4 +399,29 @@ examples:
             };
         };
     };
+  - |
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        dac@0 {
+          compatible = "microchip,mcp48feb02";
+          reg = <0x0>;
+          vdd-supply = <&vdac_vdd>;
+          vref-supply = <&vref_reg>;
+          spi-max-frequency = <10000000>;
+
+          #address-cells = <1>;
+          #size-cells = <0>;
+          channel@0 {
+            reg = <0>;
+            label = "Adjustable_voltage_ch0";
+          };
+
+          channel@1 {
+            reg = <0x1>;
+            label = "Adjustable_voltage_ch1";
+          };
+        };
+    };
 ...
Signature · 1 line
2.43.0
author maintainer
#20 lore raw

PATCH v4 9/9 iio: dac: add support for Microchip MCP48FEB02

In reply to Ariana Lazar

This is the iio driver for Microchip MCP48FxBy1/2/4/8 series of buffered voltage output Digital-to-Analog Converters with nonvolatile or volatile memory and an SPI Interface.

The families support up to 8 output channels.

The devices can be 8-bit, 10-bit and 12-bit.

---
 MAINTAINERS                      |   1 +
 drivers/iio/dac/Kconfig          |  23 +++++++
 drivers/iio/dac/Makefile         |   1 +
 drivers/iio/dac/mcp47feb02-spi.c | 145 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 170 insertions(+)
MAINTAINERS +1 -0
index fc87609865f26a9f484edc42058f7df659a0fa4f..b2ffcc0161ba0d147df35558312cbf99052ed13e 100644
@@ -16120,6 +16120,7 @@ S:	Supported
 F:	Documentation/devicetree/bindings/iio/dac/microchip,mcp47feb02.yaml
 F:	drivers/iio/dac/mcp47feb02-core.c
 F:	drivers/iio/dac/mcp47feb02-i2c.c
+F:	drivers/iio/dac/mcp47feb02-spi.c
 F:	drivers/iio/dac/mcp47feb02.h
 
 MCP4821 DAC DRIVER
drivers/iio/dac/Kconfig +23 -0
index cc10d43ffd1e89bbf945ef89ea5af0abe1a91084..d941f78f7110b185fee6f025be829bae1e4534da 100644
@@ -575,6 +575,29 @@ config MCP47FEB02_I2C
 	  will be called mcp47feb02_i2c and you will also get
 	  mcp47feb02_core for the core module.
 
+config MCP47FEB02_SPI
+	tristate "MCP48F(E/V)B01/02/04/08/11/12/14/18/21/22/24/28 SPI DAC driver"
+	depends on SPI
+	select REGMAP_SPI
+	select MCP47FEB02
+	help
+	  Say yes here if you want to build the driver for the Microchip:
+	  - 8-bit DAC:
+	    MCP48FEB01, MCP48FEB02, MCP48FEB04, MCP48FEB08,
+	    MCP48FVB01, MCP48FVB02, MCP48FVB04, MCP48FVB08
+	  - 10-bit DAC:
+	    MCP48FEB11, MCP48FEB12, MCP48FEB14, MCP48FEB18,
+	    MCP48FVB11, MCP48FVB12, MCP48FVB14, MCP48FVB18
+	  - 12-bit DAC:
+	    MCP48FEB21, MCP48FEB22, MCP48FEB24, MCP48FEB28,
+	    MCP48FVB21, MCP48FVB22, MCP48FVB24, MCP48FVB28
+	  having 1 to 8 channels, 8/10/12-bit digital-to-analog converter
+	  (DAC) with SPI interface.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called mcp47feb02_spi and you will also get
+	  mcp47feb02_core for the core module.
+
 config MCP4821
 	tristate "MCP4801/02/11/12/21/22 DAC driver"
 	depends on SPI
drivers/iio/dac/Makefile +1 -0
index 28c8646a987cff1ac4d693fe86898d66c901d5bd..9903198b934e8b66a4ea6562b662d8073ed9cc59 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_MCP4725) += mcp4725.o
 obj-$(CONFIG_MCP4728) += mcp4728.o
 obj-$(CONFIG_MCP47FEB02) += mcp47feb02-core.o
 obj-$(CONFIG_MCP47FEB02_I2C) += mcp47feb02-i2c.o
+obj-$(CONFIG_MCP47FEB02_SPI) += mcp47feb02-spi.o
 obj-$(CONFIG_MCP4821) += mcp4821.o
 obj-$(CONFIG_MCP4922) += mcp4922.o
 obj-$(CONFIG_STM32_DAC_CORE) += stm32-dac-core.o
drivers/iio/dac/mcp47feb02-spi.c new file +145 -0

145 lines not on this page · raw message

Signature · 1 line
2.43.0
#21 lore raw

Re: [PATCH v4 9/9] iio: dac: add support for Microchip MCP48FEB02

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The EEPROM write cycle polling timeout is too short, leading to sporadic -ETIMEDOUT errors and partial EEPROM updates. --

--- Patch [9]: [PATCH v4 9/9] iio: dac: add support for Microchip MCP48FEB02 --- commit f9a8ca3d3f3e9e1b7296c59e5fdc69c68349d1ae
Author: Ariana Lazar <[email protected]>

iio: dac: add support for Microchip MCP48FEB02

This patch adds SPI support for the Microchip MCP48FxBy1/2/4/8 series of DACs. It introduces a new SPI driver module that registers devices using the common core probe function.

[ ... ]

Quoted 6 lines
drivers/iio/dac/mcp47feb02-spi.c new file +0 -0
new file mode 100644
index 0000000000000..0c68d7c318cb2
@@ -0,0 +1,145 @@

[ ... ]

Quoted 10 lines
drivers/iio/dac/mcp47feb02-spi.c new file +10 -0
+static int mcp47feb02_spi_probe(struct spi_device *spi)
+{
+	const struct mcp47feb02_features *chip_features;
+	struct device *dev = &spi->dev;
+	struct regmap *regmap;
+
+	chip_features = spi_get_device_match_data(spi);
+	if (!chip_features)
+		return dev_err_probe(dev, -ENODEV, "No SPI device found\n");
+

drivers/iio/dac/mcp47feb02-spi.c line 71

Quoted 10 lines
drivers/iio/dac/mcp47feb02-spi.c new file +10 -0
+	if (chip_features->have_eeprom)
+		regmap = devm_regmap_init_spi(spi, &mcp47feb02_regmap_config);
+	else
+		regmap = devm_regmap_init_spi(spi, &mcp47fvb02_regmap_config);
+
+	if (IS_ERR(regmap))
+		return dev_err_probe(dev, PTR_ERR(regmap), "Error initializing SPI regmap\n");
+
+	return mcp47feb02_common_probe(chip_features, regmap);
+}

[Severity: High]
This isn't a bug introduced by this patch, but I noticed a potential timeout problem in the core driver that might affect these newly supported SPI devices.

In drivers/iio/dac/mcp47feb02-core.c:mcp47feb02_write_to_eeprom(), the EEPROM write cycle polling timeout is set to 5 ms:

	ret = regmap_read_poll_timeout(data->regmap, MCP47FEB02_GAIN_CTRL_STATUS_REG_ADDR,
				       eewa_val,
				       !(eewa_val & MCP47FEB02_GAIN_BIT_STATUS_EEWA_MASK),
				       USEC_PER_MSEC, USEC_PER_MSEC * 5);

Could this short timeout cause sporadic -ETIMEDOUT errors during EEPROM writes?

The EEPROM write cycle (t_WC) for the MCP47/48FEBxx series can take up to 10 ms. When a user writes to the store_eeprom sysfs attribute, this function is called in a loop. Could a timeout here cause the sequence to abort midway, leaving the EEPROM in an inconsistent, partially updated state?

The same 5 ms timeout appears again in store_eeprom_store():

	ret = regmap_read_poll_timeout(data->regmap, MCP47FEB02_GAIN_CTRL_STATUS_REG_ADDR, eewa_val,
				       !(eewa_val & MCP47FEB02_GAIN_BIT_STATUS_EEWA_MASK),
				       USEC_PER_MSEC, USEC_PER_MSEC * 5);

Would it be better to increase the timeout to at least 10 ms to match the hardware maximum?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9