Skip to contentSkip to conversation

add mcp4728 I2C DAC driver

PATCH Andrea Collamati 2 participants 2 messages Jul 10, 2023 – Jul 11, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

From 01b156ca1b27be83f4c74c288dbc0bcad178fe0b Mon Sep 17 00:00:00 2001
From: Andrea Collamati <[email protected]>
Date: Mon, 10 Jul 2023 16:20:40 +0200
Subject: [PATCH] iio: add mcp4728 I2C DAC driver

Microchip MCP4728 is a 12-bit quad channel digital-to-analog converter (DAC) with I2C interface.

This patch adds support for per-channel gain, power state and power down mode control. Current state could be saved to on-chip EEPROM.
Internal voltage reference and external vdd ref are supported.

---
 .../bindings/iio/dac/microchip,mcp4728.yaml   |  42 ++ drivers/iio/dac/Kconfig                       |  12 + drivers/iio/dac/Makefile                      |   1 + drivers/iio/dac/mcp4728.c                     | 641 ++++++++++++++++++

 4 files changed, 696 insertions(+)

 create mode 100644 Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml create mode 100644 drivers/iio/dac/mcp4728.c

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +42 -0
new file mode 100644
index 000000000000..68f4e359a921
@@ -0,0 +1,42 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/dac/microchip,mcp4728.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Microchip mcp4728
+
+maintainers:
+  - Andrea Collamati <[email protected]>
+
+properties:
+  compatible:
+    enum:
+      - microchip,mcp4728      
+  reg:
+    maxItems: 1
+
+  vdd-supply:
+    description: |
+      Provides both power and acts as the reference supply on the mcp4728
+      when Internal Vref is not selected.      
+
+required:
+  - compatible
+  - reg
+  - vdd-supply
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        mcp4728_dac@64 {
+            compatible = "microchip,mcp4728";
+            reg = <0x60>;
+            vdd-supply = <&vdac_vdd>;
+        };
+    };
drivers/iio/dac/Kconfig +0 -0
index c0bf0d84197f..6c96cd30994a 100644
@@ -376,6 +376,18 @@ config MCP4725

       To compile this driver as a module, choose M here: the module will be called mcp4725.

+config MCP4728
+    tristate "MCP4728 DAC driver"
+    depends on I2C
+    help
+      Say Y here if you want to build a driver for the Microchip +      MCP4728 quad channel, 12-bit digital-to-analog converter (DAC) +      with I2C interface.
+
+      To compile this driver as a module, choose M here: the module +      will be called mcp4728.
+
+
 config MCP4922
     tristate "MCP4902, MCP4912, MCP4922 DAC driver" depends on SPI

drivers/iio/dac/Makefile +0 -0
index ec3e42713f00..35ac62a61b05 100644
@@ -40,6 +40,7 @@ obj-$(CONFIG_M62332) += m62332.o

 obj-$(CONFIG_MAX517) += max517.o
 obj-$(CONFIG_MAX5821) += max5821.o
 obj-$(CONFIG_MCP4725) += mcp4725.o
+obj-$(CONFIG_MCP4728) += mcp4728.o
 obj-$(CONFIG_MCP4922) += mcp4922.o
 obj-$(CONFIG_STM32_DAC_CORE) += stm32-dac-core.o
 obj-$(CONFIG_STM32_DAC) += stm32-dac.o

drivers/iio/dac/mcp4728.c new file +641 -0

641 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

Thank you for your patch. There is something to discuss/improve.

Quoted 4 lines · Andrea Collamati

From 01b156ca1b27be83f4c74c288dbc0bcad178fe0b Mon Sep 17 00:00:00 2001
From: Andrea Collamati <[email protected]>
Date: Mon, 10 Jul 2023 16:20:40 +0200
Subject: [PATCH] iio: add mcp4728 I2C DAC driver

1. That is not a proper patch header. I don't know how you got it, but it's wrong. Use just b4 or git format-patch and git send-email.

2. Please use scripts/get_maintainers.pl to get a list of necessary people and lists to CC (and consider --no-git-fallback argument). It might happen, that command when run on an older kernel, gives you outdated entries. Therefore please be sure you base your patches on recent Linux kernel.

Quoted 5 lines

Microchip MCP4728 is a 12-bit quad channel digital-to-analog converter (DAC) with I2C interface.

This patch adds support for per-channel gain, power state and power down mode control.

3. Please do not use "This commit/patch", but imperative mood. See longer explanation here:
https://elixir.bootlin.com/linux/v5.17.1/source/Documentation/process/submitting-patches.rst#L95

Quoted 5 lines

Current state could be saved to on-chip EEPROM. Internal voltage reference and external vdd ref are supported.

---
 .../bindings/iio/dac/microchip,mcp4728.yaml   |  42 ++

4. Bindings are always separate patches.

5. Please run scripts/checkpatch.pl and fix reported warnings. Some warnings can be ignored, but the code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.

Quoted 13 lines

 drivers/iio/dac/Kconfig                       |  12 + drivers/iio/dac/Makefile                      |   1 + drivers/iio/dac/mcp4728.c                     | 641 ++++++++++++++++++

 4 files changed, 696 insertions(+)

 create mode 100644 Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml create mode 100644 drivers/iio/dac/mcp4728.c

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +0 -0
new file mode 100644
index 000000000000..68f4e359a921

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml line 7

Quoted 7 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +7 -0
@@ -0,0 +1,42 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/dac/microchip,mcp4728.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Microchip mcp4728

6. mcp or MCP? What is this? Proper title is missing... also no description.

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml line 15

Quoted 8 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +8 -0
+
+maintainers:
+  - Andrea Collamati <[email protected]>
+
+properties:
+  compatible:
+    enum:
+      - microchip,mcp4728      

7. Blank line
8. Whitespace errors

Quoted 12 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +12 -0
+  reg:
+    maxItems: 1
+
+  vdd-supply:
+    description: |
+      Provides both power and acts as the reference supply on the mcp4728
+      when Internal Vref is not selected.      
+
+required:
+  - compatible
+  - reg
+  - vdd-supply

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml line 37

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +10 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        mcp4728_dac@64 {

9. Node names should be generic. See also an explanation and list of examples (not exhaustive) in DT specification:
https://devicetree-specification.readthedocs.io/en/latest/chapter2-devicetree-basics.html#generic-names-recommendation

Also, underscores are not allowed in node names.

Shouldn't this binding be just merged with existing mcp4725? Are you sure it's not similar device, IOW, are you sure you do not have vref supply?

Quoted 5 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +5 -0
+            compatible = "microchip,mcp4728";
+            reg = <0x60>;
+            vdd-supply = <&vdac_vdd>;
+        };
+    };

...

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml

Quoted 11 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +11 -0
+
+static int mcp4728_probe(struct i2c_client *client,
+             const struct i2c_device_id *id)
+{
+    struct mcp4728_data *data;
+    struct iio_dev *indio_dev;
+    int err;
+
+    indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+    if (indio_dev == NULL)
+        return -ENOMEM;

Missing blank line

Quoted 7 lines

+    data = iio_priv(indio_dev);
+    i2c_set_clientdata(client, indio_dev);
+    data->client = client;
+    if (dev_fwnode(&client->dev))
+        data->id = (uintptr_t)device_get_match_data(&client->dev); +    else
+        data->id = id->driver_data;

Dead code, drop.

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml

Quoted 13 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +13 -0
+
+    data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
+    if (IS_ERR(data->vdd_reg))
+        return PTR_ERR(data->vdd_reg);
+
+    err = regulator_enable(data->vdd_reg);
+    if (err)
+        goto err_disable_vdd_reg;
+
+    err = mcp4728_init_channels_data(data);
+    if (err) {
+        dev_err(&client->dev,
+            "failed to read mcp4728 current configuration\n");

None of your statements look properly aligned. What's more everything has incorrect indentation.

Run checkpatch --strict and fix ALL warnings and errors.

Quoted 22 lines

+        goto err_disable_vdd_reg;
+    }

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +20 -0
+
+    indio_dev->name = id->name;
+    indio_dev->info = &mcp4728_info;
+    indio_dev->channels = mcp4728_channels;
+    indio_dev->num_channels = MCP4728_N_CHANNELS;
+    indio_dev->modes = INDIO_DIRECT_MODE;
+
+    err = iio_device_register(indio_dev);
+    if (err)
+        goto err_disable_vdd_reg;
+
+    return 0;
+
+err_disable_vdd_reg:
+    regulator_disable(data->vdd_reg);
+
+    return err;
+}
+
+static int mcp4728_remove(struct i2c_client *client)

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +10 -0
+{
+    struct iio_dev *indio_dev = i2c_get_clientdata(client);
+    struct mcp4728_data *data = iio_priv(indio_dev);
+
+    iio_device_unregister(indio_dev);
+    regulator_disable(data->vdd_reg);
+    return 0;
+}
+
+static const struct i2c_device_id mcp4728_id[] = { { "mcp4728", MCP4728 }, {} };

That's some odd formatting. Look at existing files:
git grep i2c_device_id

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml

Quoted 4 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +4 -0
+MODULE_DEVICE_TABLE(i2c, mcp4728_id);
+
+static const struct of_device_id mcp4728_of_match[] = {
+    { .compatible = "microchip,mcp4728", .data = (void *)MCP4728 },

Drop unused MCP4728.

Quoted 9 lines

+    {}

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +8 -0
+};
+MODULE_DEVICE_TABLE(of, mcp4728_of_match);
+
+static struct i2c_driver mcp4728_driver = {
+        .driver = {
+                .name = MCP4728_DRV_NAME,
+                .of_match_table = mcp4728_of_match,
+                .pm = pm_sleep_ptr(&mcp4728_pm_ops),

Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml new file +10 -0
+        },
+        .probe = mcp4728_probe,
+        .remove = mcp4728_remove,
+        .id_table = mcp4728_id,
+};
+module_i2c_driver(mcp4728_driver);
+
+MODULE_AUTHOR("Andrea Collamati <[email protected]>");
+MODULE_DESCRIPTION("MCP4728 12-bit DAC");
+MODULE_LICENSE("GPL");

Best regards,
Krzysztof