Skip to contentSkip to conversation

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

PATCH In review Ariana Lazar 2 participants 4 messages May 27 – Jun 4

Subsystems (2)
lore
Share & tools
lore

Ariana Lazar

author maintainer
#0 lore raw

PATCH v2 1/1 iio: dac: mcp47feb02: refactor MCP47FEB02 I2C driver into two modules

The MCP47FEB02 driver was refactored into two modules: mcp47feb02-core.c and mcp47feb02-i2c.c in order to prepare the support for SPI (MCP48F(E/V)BXX) DAC family on top of the current implementation.

---
The MCP47FEB02 driver was refactored into two modules: mcp47feb02-core.c and mcp47feb02-i2c.c in order to prepare the support for SPI (MCP48F(E/V)BXX) DAC family on top of the current implementation.

Changes in v2:
Refactor the code into core and I2C modules and update MAINTAINERS,
Kconfig and Makefile to the new format.
Handle mismatch between vref modes at power up and enable regulator
only after the user corrects it.
Improve comments and debug messages for clarity.
Avoid 64-bit division in mcp47feb02_init_scale().
Add available scale array in channel data struct.
Add a device_property_present() check for the Vref1 supply before
reading the regulator.
Rewrite conditions and warning messages for better readablity in
init_ctrl_regs().
Reorder channel_data struct fields as recommended and run pahole again. Rewrite DAC_GAIN_MASK and DAC_GAIN_VAL formula as recommended.
Move initialization of Vref/Vref1 accordingly in probe().
Add missing newlines in debug messages.
Remove lock in parse_fw().
Remove fwnode_get_name function from missing label debug message.
Remove redundant reg initialization.
Split iio includes from the rest.
Rename scale and scale_1 to scale_0 and scale_1.
Declare tmp_eval as unsigned in mcp47feb02_set_scale().
Declare eewa_val as unsigned in mcp47feb02_write_to_eeprom().
Use 'num_channels == 0' instead of '!num_channels' in parse_fw().
Move lock from mcp47feb02_write_to_eeprom() to store_eeprom_store().
Correct timeout value to 20 USEC_PER_MSEC.
Remove blank line before IIO_DEVICE_ATTR_WO.
v1:
- first version committed to review
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 MAINTAINERS                       |    4 +-
 drivers/iio/dac/Kconfig           |    5 +
 drivers/iio/dac/Makefile          |    3 +-
 drivers/iio/dac/mcp47feb02-core.c |  992 +++++++++++++++++++++++++++++
 drivers/iio/dac/mcp47feb02-i2c.c  |  145 +++++
 drivers/iio/dac/mcp47feb02.c      | 1250 -------------------------------------
 drivers/iio/dac/mcp47feb02.h      |  173 +++++
 7 files changed, 1320 insertions(+), 1252 deletions(-)
MAINTAINERS +3 -1
index d664add6d40891fb4a94dba34c58d401df81d657..e7e640de710bc7cffb55b96a28fa620c83ab658e 100644
@@ -15850,7 +15850,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 +5 -0
index cd4870b654153e91c3c44860be43d231ee3b5519..7f37a8c6d36efb4e07836fe02b83e247844b72ca 100644
@@ -539,8 +539,13 @@ config MCP4728
 	  will be called mcp4728.
 
 config MCP47FEB02
+	tristate
+
+config MCP47FEB02_I2C
 	tristate "MCP47F(E/V)B01/02/04/08/11/12/14/18/21/22/24/28 DAC driver"
 	depends on I2C
+	select MCP47FEB02
+	select REGMAP_I2C
 	help
 	  Say yes here if you want to build the driver for the Microchip:
 	  - 8-bit DAC:
drivers/iio/dac/Makefile +2 -1
index 2a80bbf4e80ad557da79ed916027cedff286984b..9ca576b0a25f25b85378c5557403d57cef369850 100644
@@ -53,7 +53,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 new file +992 -0

992 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.c deleted +0 -1250

1,250 lines not on this page · raw message

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

173 lines not on this page · raw message

---
base-commit: 8625d418d24bc0ff463267b26b7cb2e7a612495f
change-id: 20260331-mcp47feb02-fix2-95c6baa7fb2b

Best regards,

Signature · 1 line
Ariana Lazar <[email protected]>
maintainer
#1 lore raw
Quoted 9 lines · On Wed, 27 May 2026 17:50:40 +0300 Ariana Lazar <[email protected]>…

The MCP47FEB02 driver was refactored into two modules: mcp47feb02-core.c and mcp47feb02-i2c.c in order to prepare the support for SPI (MCP48F(E/V)BXX) DAC family on top of the current implementation.

---
The MCP47FEB02 driver was refactored into two modules: mcp47feb02-core.c and mcp47feb02-i2c.c in order to prepare the support for SPI (MCP48F(E/V)BXX) DAC family on top of the current implementation.

Hi Ariana,

I don't mind reviewing this on it's own but won't merge it until we have a reason (i.e. that SPI part)

Quoted 6 lines

Changes in v2:
Refactor the code into core and I2C modules and update MAINTAINERS,
Kconfig and Makefile to the new format.
Handle mismatch between vref modes at power up and enable regulator
only after the user corrects it.

This doesn't sound like it's related to a refactor..

All of these belong in separate patches from the fundamental code move. Probably before it given they seem like a good idea anyway..

Quoted 25 lines

Improve comments and debug messages for clarity.
Avoid 64-bit division in mcp47feb02_init_scale().
Add available scale array in channel data struct.
Add a device_property_present() check for the Vref1 supply before
reading the regulator.
Rewrite conditions and warning messages for better readablity in
init_ctrl_regs().
Reorder channel_data struct fields as recommended and run pahole again. Rewrite DAC_GAIN_MASK and DAC_GAIN_VAL formula as recommended.
Move initialization of Vref/Vref1 accordingly in probe().
Add missing newlines in debug messages.
Remove lock in parse_fw().
Remove fwnode_get_name function from missing label debug message.
Remove redundant reg initialization.
Split iio includes from the rest.
Rename scale and scale_1 to scale_0 and scale_1.
Declare tmp_eval as unsigned in mcp47feb02_set_scale().
Declare eewa_val as unsigned in mcp47feb02_write_to_eeprom().
Use 'num_channels == 0' instead of '!num_channels' in parse_fw().
Move lock from mcp47feb02_write_to_eeprom() to store_eeprom_store().
Correct timeout value to 20 USEC_PER_MSEC.
Remove blank line before IIO_DEVICE_ATTR_WO.
v1:
- first version committed to review
- Link to v1: https://lore.kernel.org/r/[email protected]

Quoted 7 lines
---
 MAINTAINERS                       |    4 +-
 drivers/iio/dac/Kconfig           |    5 +
 drivers/iio/dac/Makefile          |    3 +-
 drivers/iio/dac/mcp47feb02-core.c |  992 +++++++++++++++++++++++++++++
 drivers/iio/dac/mcp47feb02-i2c.c  |  145 +++++
 drivers/iio/dac/mcp47feb02.c      | 1250 -------------------------------------

Did you turn rename detection off? For a patch like this I'd rather have it on as much of the code moved from .c to core.c and it will be much easier to spot any changes.

drivers/iio/dac/mcp47feb02-core.c line 3

Quoted 12 lines
 drivers/iio/dac/mcp47feb02.h      |  173 +++++
 7 files changed, 1320 insertions(+), 1252 deletions(-)
drivers/iio/dac/mcp47feb02-core.c new file +3 -0
new file mode 100644
index 0000000000000000000000000000000000000000..fa3e3d06c2c96a5d25c0761b29ffe785e29a622c
@@ -0,0 +1,992 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * IIO driver for MCP47FEB02 Multi-Channel DAC with I2C interface

That needs an update given this isn't the bus specific code any more.

Quoted 5 lines
drivers/iio/dac/mcp47feb02-core.c new file +5 -0
+ *
+ * Copyright (C) 2026 Microchip Technology Inc. and its subsidiaries
+ *
+ * Author: Ariana Lazar <[email protected]>
+ *

drivers/iio/dac/mcp47feb02-core.c line 18

Quoted 10 lines
drivers/iio/dac/mcp47feb02-core.c new file +10 -0
+ * Datasheet links for devices with I2C interface:
+ * [MCP47FEBxx] https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20005375A.pdf
+ * [MCP47FVBxx] https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20005405A.pdf
+ * [MCP47FxBx4/8] https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/MCP47FXBX48-Data-Sheet-DS200006368A.pdf
+ */
+#include <linux/array_size.h>
+#include <linux/bitops.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>

As part of this rework (probably separate patch) good to update these headers to include more specific ones and drop device.h

Quoted 6 lines
drivers/iio/dac/mcp47feb02-core.c new file +6 -0
+#include <linux/export.h>
+#include <linux/kstrtox.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/property.h>
+#include <linux/regmap.h>

drivers/iio/dac/mcp47feb02-core.c

Quoted 10 lines
drivers/iio/dac/mcp47feb02-core.c new file +8 -0
+#include <linux/regulator/consumer.h>
+#include <linux/types.h>
+#include <linux/units.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
 
 
+
+int mcp47feb02_common_probe(const struct mcp47feb02_features *chip_features, struct regmap *regmap)

As below. Very long line, break that after ,

Quoted 9 lines
drivers/iio/dac/mcp47feb02-core.c new file +1 -0
+{
drivers/iio/dac/mcp47feb02.h new file +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..5dee70cb67bf3aca52a0234d30aea1561aa12c58

drivers/iio/dac/mcp47feb02.h line 22

Quoted 7 lines
drivers/iio/dac/mcp47feb02.h new file +5 -0
 
 
+/* Register addresses must be left shifted by 3 bits in order to append the command mask */
+#define MCP47FEB02_DAC0_REG_ADDR			0x00
+#define MCP47FEB02_VREF_REG_ADDR			0x40
+#define MCP47FEB02_POWER_DOWN_REG_ADDR			0x48
+#define MCP47FEB02_DAC_CTRL_MASK			GENMASK(1, 0)

Is the the command mask? Odd mismatch in naming if so. Perhaps update the comment to match whilst doing this move.

So far I'm not seeing a reason to move these all to the header. They don't seem to be used in the i2c specific file.

Quoted 5 lines
drivers/iio/dac/mcp47feb02.h new file +5 -0
+
+#define MCP47FEB02_GAIN_CTRL_STATUS_REG_ADDR		0x50
+#define MCP47FEB02_GAIN_BIT_MASK			BIT(0)
+#define MCP47FEB02_GAIN_BIT_STATUS_EEWA_MASK		BIT(6)
+#define MCP47FEB02_GAIN_BITS_MASK			GENMASK(15, 8)

...

drivers/iio/dac/mcp47feb02.h line 92

Quoted 7 lines
drivers/iio/dac/mcp47feb02.h new file +7 -0
+
+enum mcp47feb02_vref_mode {
+	MCP47FEB02_VREF_VDD = 0,
+	MCP47FEB02_INTERNAL_BAND_GAP = 1,
+	MCP47FEB02_EXTERNAL_VREF_UNBUFFERED = 2,
+	MCP47FEB02_EXTERNAL_VREF_BUFFERED = 3,
+};

Similar to the above - I'd not expect to see these in the buffer specific drivers, so can we keep the definitions in the core c file?

Quoted 67 lines
drivers/iio/dac/mcp47feb02.h new file +67 -0
+
+enum mcp47feb02_scale {
+	MCP47FEB02_SCALE_VDD = 0,
+	MCP47FEB02_SCALE_GAIN_X1 = 1,
+	MCP47FEB02_SCALE_GAIN_X2 = 2,
+};
+
+enum mcp47feb02_gain_bit_mode {
+	MCP47FEB02_GAIN_BIT_X1 = 0,
+	MCP47FEB02_GAIN_BIT_X2 = 1,
+};
+
+/**
+ * struct mcp47feb02_channel_data - channel configuration
+ * @scale_avail: scales available for the channel based on current configuration
+ * @dac_data: DAC value
+ * @ref_mode: chosen voltage for reference
+ * @powerdown_mode: selected power-down mode
+ * @powerdown: is false if the channel is in normal operation mode
+ * @use_2x_gain: output driver gain control
+ * @ref_mismatch: true if the restored register configuration is different from the device tree one
+ */
+struct mcp47feb02_channel_data {
+	int *scale_avail;
+	u16 dac_data;
+	u8 ref_mode;
+	u8 powerdown_mode;
+	bool powerdown;
+	bool use_2x_gain;
+	bool ref_mismatch;
+};
+
+/**
+ * struct mcp47feb02_data - chip configuration
+ * @chdata: options configured for each channel on the device
+ * @lock: prevents concurrent reads/writes to driver's state member
+ * @chip_features: pointer to features struct
+ * @scale_1: scales set on channels that are using Vref1
+ * @scale_0: scales set on channels that are using Vref/Vref0
+ * @active_channels_mask: enabled channels
+ * @regmap: regmap for directly accessing device register
+ * @labels: table with channels labels
+ * @vref1_reg: Vref1 regulator to be enabled after correcting reference mismatch
+ * @vref_reg: Vref/Vref0 regulator to be enabled after correcting reference mismatch
+ * @phys_channels: physical channels on the device
+ * @vref1_buffered: Vref1 buffer is enabled
+ * @vref_buffered: Vref/Vref0 buffer is enabled
+ * @use_vref1: vref1-supply is defined
+ * @use_vref: vref-supply is defined
+ * @vref1_enabled: true if Vref1 regulator has been enabled
+ * @vref_enabled: true if Vref/Vref0 regulator has been enabled
+ */
+struct mcp47feb02_data {
+	struct mcp47feb02_channel_data chdata[MCP47FEB02_MAX_CH];
+	struct mutex lock; /* prevents concurrent reads/writes to driver state */
+	const struct mcp47feb02_features *chip_features;
+	int scale_1[2 * MCP47FEB02_MAX_SCALES_CH];
+	int scale_0[2 * MCP47FEB02_MAX_SCALES_CH];
+	unsigned long active_channels_mask;
+	struct regmap *regmap;
+	const char *labels[MCP47FEB02_MAX_CH];
+	struct regulator *vref_reg;
+	struct regulator *vref1_reg;
+	u16 phys_channels;
+	bool vref1_buffered;
+	bool vref_buffered;
+	bool use_vref1;

drivers/iio/dac/mcp47feb02.h line 169

Quoted 10 lines
drivers/iio/dac/mcp47feb02.h new file +10 -0
+	bool use_vref;
+	bool vref1_enabled;
+	bool vref_enabled;
+};
+
+extern const struct regmap_config mcp47feb02_regmap_config;
+extern const struct regmap_config mcp47fvb02_regmap_config;
+
+/* Properties shared by I2C families */
+int mcp47feb02_common_probe(const struct mcp47feb02_features *chip_features, struct regmap *regmap);

That's a very long line. I'd break it after ,

Quoted 10 lines
drivers/iio/dac/mcp47feb02.h new file +4 -1
+
+extern const struct dev_pm_ops mcp47feb02_pm_ops;
+
+#endif /* _MCP47FEB02_H_ */
 
---

base-commit: 8625d418d24bc0ff463267b26b7cb2e7a612495f
change-id: 20260331-mcp47feb02-fix2-95c6baa7fb2b

Best regards,

author maintainer
#2 lore raw

Hi Jonathan,

Thank you for the review.

Quoted 3 lines

I don't mind reviewing this on it's own but won't merge it until we have a reason (i.e. that SPI part)

In the first version of this patch series the SPI file was included too, but it was recommended not to for better clarity:
https://lore.kernel.org/all/20260419171658.2dbb9884@jic23-huawei/

My understanding was to submit a driver refactoring (splitting the driver into core and I2C modules) and submit later another patch to add the SPI part (the SPI module and modified dt binding). Please tell me if this is alright or I should do it differently.

Quoted 5 lines

All of these belong in separate patches from the fundamental code move. Probably before it given they seem like a good idea anyway..

After mcp47feb02 I2C driver was accepted in upstream I've submitted a new patch series for mcp48feb02 SPI driver. Changes I did among the refactor were from the review for that driver:
https://lore.kernel.org/all/[email protected]/

I will remove the reference mismatch part, which is a fix for mcp47feb02, and I will submit it in v3 for the series:
https://lore.kernel.org/all/[email protected]/
in order to add the fixes from later reviews (even if those patches will not be continued).

Best regards,
Ariana

maintainer
#3 lore raw
Quoted 9 lines · On Wed, 3 Jun 2026 14:52:41 +0000 <[email protected]> wrote:

Hi Jonathan,

Thank you for the review.

Quoted 3 lines

I don't mind reviewing this on it's own but won't merge it until we have a reason (i.e. that SPI part)

Quoted 9 lines

In the first version of this patch series the SPI file was included too, but it was recommended not to for better clarity:
https://lore.kernel.org/all/20260419171658.2dbb9884@jic23-huawei/

My understanding was to submit a driver refactoring (splitting the driver into core and I2C modules) and submit later another patch to add the SPI part (the SPI module and modified dt binding). Please tell me if this is alright or I should do it differently.

Ah. I didn't explain that clearly sorry.

Same series, different patches. So this is the first patch and should result in no funcational changes (looks good)
I've kind of lost track but there may be some precursor cleanup / improvements to make in additional patches before that one splitting core and I2C apart.

The patch after this then adds the new device support over the SPI bus.

Jonathan

Quoted 20 lines
Quoted 6 lines

All of these belong in separate patches from the fundamental code move. Probably before it given they seem like a good idea anyway..

After mcp47feb02 I2C driver was accepted in upstream I've submitted a new patch series for mcp48feb02 SPI driver. Changes I did among the refactor were from the review for that driver:
https://lore.kernel.org/all/[email protected]/

I will remove the reference mismatch part, which is a fix for mcp47feb02, and I will submit it in v3 for the series:
https://lore.kernel.org/all/[email protected]/
in order to add the fixes from later reviews (even if those patches will not be continued).

Best regards,
Ariana