[PATCH] iio: add mcp4728 I2C DAC driver
From: Andrea Collamati
Date: Mon Jul 10 2023 - 10:20:40 EST
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
diff --git a/Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml b/Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml
new file mode 100644
index 000000000000..68f4e359a921
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/dac/microchip,mcp4728.yaml
@@ -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 <andrea.collamati@xxxxxxxxx>
+
+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>;
+Â Â Â Â Â Â Â };
+Â Â Â };
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index c0bf0d84197f..6c96cd30994a 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -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
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index ec3e42713f00..35ac62a61b05 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -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
diff --git a/drivers/iio/dac/mcp4728.c b/drivers/iio/dac/mcp4728.c
new file mode 100644
index 000000000000..d304fa8ffa26
--- /dev/null
+++ b/drivers/iio/dac/mcp4728.c
@@ -0,0 +1,641 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Support for Microchip MCP4728
+ *
+ * Copyright (C) 2023 Andrea Collamati <andrea.collamati@xxxxxxxxx>
+ *
+ * Based on mcp4725 by Peter Meerwald <pmeerw@xxxxxxxxxx>
+ *
+ * Driver for the Microchip I2C 12-bit digital-to-analog quad channels
+ * converter (DAC).
+ *
+ * (7-bit I2C slave address 0x60, the three LSBs can be configured in
+ * hardware)
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/err.h>
+#include <linux/delay.h>
+#include <linux/regulator/consumer.h>
+#include <linux/mod_devicetable.h>
+#include <linux/property.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define MCP4728_DRV_NAME "mcp4728"
+
+#define MCP4728_RESOLUTION 12
+#define MCP4728_N_CHANNELS 4
+
+#define MCP4728_CMD_POS 3
+#define MCP4728_CMD_UDAC_POS 0
+#define MCP4728_CMD_CH_SEL_POS 1
+
+#define MCP4728_CMD_VREF_MASK 0x80
+#define MCP4728_CMD_VREF_POS 7
+
+#define MCP4728_CMD_PDMODE_MASK 0x60
+#define MCP4728_CMD_PDMODE_POS 5
+
+#define MCP4728_CMD_GAIN_MASK 0x10
+#define MCP4728_CMD_GAIN_POS 4
+
+#define MCP4728_MW_CMD 0x08 // Multiwrite Command
+#define MCP4728_SW_CMD 0x0A // Sequential Write Command (include eeprom)
+
+#define MCP4728_READ_RESPONSE_LEN (MCP4728_N_CHANNELS * 3 * 2) // Read Message
+#define MCP4728_WRITE_EEPROM_LEN \
+Â Â Â (1 + MCP4728_N_CHANNELS * 2) // Sequential Write
+
+enum vref_mode {
+Â Â Â MCP4728_VREF_EXTERNAL_VDD = 0,
+Â Â Â MCP4728_VRED_INTERNAL_2048mV = 1,
+};
+
+enum gain_mode {
+Â Â Â MCP4728_GAIN_X1 = 0,
+Â Â Â MCP4728_GAIN_X2 = 1,
+};
+
+enum iio_powerdown_mode {
+Â Â Â MCP4728_IIO_1K,
+Â Â Â MCP4728_IIO_100K,
+Â Â Â MCP4728_IIO_500K,
+};
+
+struct mcp4728_channel_data {
+Â Â Â enum vref_mode ref_mode;
+Â Â Â enum iio_powerdown_mode pd_mode;
+Â Â Â enum gain_mode g_mode;
+Â Â Â u16 dac_value;
+};
+
+struct mcp4728_data {
+Â Â Â struct i2c_client *client;
+Â Â Â int id;
+Â Â Â struct regulator *vdd_reg;
+Â Â Â bool powerdown;
+Â Â Â struct mcp4728_channel_data channel_data[MCP4728_N_CHANNELS];
+};
+
+#define MCP4728_CHAN(chan)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
+Â Â Â {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
+      .type = IIO_VOLTAGE, .output = 1, .indexed = 1,               \
+Â Â Â Â Â Â .channel = chan, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+      .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),         \
+      .ext_info = mcp4728_ext_info,                                 \
+Â Â Â }
+
+static int mcp4728_suspend(struct device *dev);
+static int mcp4728_resume(struct device *dev);
+
+static ssize_t mcp4728_store_eeprom(struct device *dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct device_attribute *attr,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â const char *buf, size_t len)
+{
+Â Â Â struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â u8 outbuf[MCP4728_WRITE_EEPROM_LEN];
+Â Â Â int tries = 20;
+Â Â Â u8 inbuf[3];
+Â Â Â bool state;
+Â Â Â int ret;
+Â Â Â unsigned int i;
+
+Â Â Â ret = kstrtobool(buf, &state);
+Â Â Â if (ret < 0)
+Â Â Â Â Â Â return ret;
+
+Â Â Â if (!state)
+Â Â Â Â Â Â return 0;
+
+Â Â Â outbuf[0] = MCP4728_SW_CMD << MCP4728_CMD_POS; // Command ID
+
+Â Â Â for (i = 0; i < MCP4728_N_CHANNELS; i++) {
+Â Â Â Â Â Â struct mcp4728_channel_data *ch = &(data->channel_data[i]);
+Â Â Â Â Â Â int offset = 1 + i * 2;
+
+Â Â Â Â Â Â outbuf[offset] = ch->ref_mode << MCP4728_CMD_VREF_POS;
+Â Â Â Â Â Â if (data->powerdown) {
+Â Â Â Â Â Â Â Â Â u8 mcp4728_pd_mode = ch->pd_mode + 1;
+
+Â Â Â Â Â Â Â Â Â outbuf[1] |= mcp4728_pd_mode << MCP4728_CMD_PDMODE_POS;
+Â Â Â Â Â Â }
+
+Â Â Â Â Â Â outbuf[offset] |= ch->g_mode << MCP4728_CMD_GAIN_POS;
+Â Â Â Â Â Â outbuf[offset] |= ch->dac_value >> 8;
+Â Â Â Â Â Â outbuf[offset + 1] = ch->dac_value & 0xff;
+Â Â Â }
+
+Â Â Â ret = i2c_master_send(data->client, outbuf, MCP4728_WRITE_EEPROM_LEN);
+Â Â Â if (ret < 0)
+Â Â Â Â Â Â return ret;
+Â Â Â else if (ret != MCP4728_WRITE_EEPROM_LEN)
+Â Â Â Â Â Â return -EIO;
+
+Â Â Â /* wait RDY signal for write complete, takes up to 50ms */
+Â Â Â while (tries--) {
+Â Â Â Â Â Â msleep(20);
+Â Â Â Â Â Â ret = i2c_master_recv(data->client, inbuf, 3);
+Â Â Â Â Â Â if (ret < 0)
+Â Â Â Â Â Â Â Â Â return ret;
+Â Â Â Â Â Â else if (ret != 3)
+Â Â Â Â Â Â Â Â Â return -EIO;
+
+Â Â Â Â Â Â if (inbuf[0] & 0x80) // check RDY flag
+Â Â Â Â Â Â Â Â Â break;
+Â Â Â }
+
+Â Â Â if (tries < 0) {
+Â Â Â Â Â Â dev_err(&data->client->dev, "%s failed, incomplete\n",
+Â Â Â Â Â Â Â Â Â __func__);
+Â Â Â Â Â Â return -EIO;
+Â Â Â }
+Â Â Â return len;
+}
+
+static IIO_DEVICE_ATTR(store_eeprom, 0200, NULL, mcp4728_store_eeprom, 0);
+
+static struct attribute *mcp4728_attributes[] = {
+Â Â Â &iio_dev_attr_store_eeprom.dev_attr.attr,
+Â Â Â NULL,
+};
+
+static const struct attribute_group mcp4728_attribute_group = {
+Â Â Â .attrs = mcp4728_attributes,
+};
+
+enum chip_id {
+Â Â Â MCP4728,
+};
+
+static int mcp4728_program_channel_cfg(int channel, struct iio_dev *indio_dev)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â struct mcp4728_channel_data *ch = &(data->channel_data[channel]);
+Â Â Â u8 outbuf[3];
+Â Â Â int ret;
+
+Â Â Â outbuf[0] = MCP4728_MW_CMD << MCP4728_CMD_POS; // Command ID
+Â Â Â outbuf[0] |= channel << MCP4728_CMD_CH_SEL_POS; // Channel Selector
+Â Â Â outbuf[0] |= 0; // UDAC = 0
+
+Â Â Â outbuf[1] = ch->ref_mode << MCP4728_CMD_VREF_POS;
+Â Â Â if (data->powerdown) {
+Â Â Â Â Â Â u8 mcp4728_pd_mode = ch->pd_mode + 1;
+
+Â Â Â Â Â Â outbuf[1] |= mcp4728_pd_mode << MCP4728_CMD_PDMODE_POS;
+Â Â Â }
+
+Â Â Â outbuf[1] |= ch->g_mode << MCP4728_CMD_GAIN_POS;
+
+Â Â Â outbuf[1] |= ch->dac_value >> 8;
+Â Â Â outbuf[2] = ch->dac_value & 0xff;
+
+Â Â Â ret = i2c_master_send(data->client, outbuf, 3);
+Â Â Â if (ret < 0)
+Â Â Â Â Â Â return ret;
+Â Â Â else if (ret != 3)
+Â Â Â Â Â Â return -EIO;
+Â Â Â else
+Â Â Â Â Â Â return 0;
+}
+
+// powerdown mode
+static const char *const mcp4728_powerdown_modes[] = {
+Â Â Â "1kohm_to_gnd",
+Â Â Â "100kohm_to_gnd",
+Â Â Â "500kohm_to_gnd"
+};
+
+static int mcp4728_get_powerdown_mode(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â const struct iio_chan_spec *chan)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+
+Â Â Â return data->channel_data[chan->channel].pd_mode;
+}
+
+static int mcp4728_set_powerdown_mode(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â const struct iio_chan_spec *chan,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int mode)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+
+Â Â Â data->channel_data[chan->channel].pd_mode = mode;
+
+Â Â Â return 0;
+}
+
+static ssize_t mcp4728_read_powerdown(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â uintptr_t private,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â const struct iio_chan_spec *chan,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â char *buf)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+
+Â Â Â return sysfs_emit(buf, "%d\n", data->powerdown);
+}
+
+static ssize_t mcp4728_write_powerdown(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â uintptr_t private,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â const struct iio_chan_spec *chan,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â const char *buf, size_t len)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â bool state;
+Â Â Â int ret;
+
+Â Â Â ret = kstrtobool(buf, &state);
+Â Â Â if (ret)
+Â Â Â Â Â Â return ret;
+
+Â Â Â if (state)
+Â Â Â Â Â Â ret = mcp4728_suspend(&data->client->dev);
+Â Â Â else
+Â Â Â Â Â Â ret = mcp4728_resume(&data->client->dev);
+Â Â Â if (ret < 0)
+Â Â Â Â Â Â return ret;
+
+Â Â Â return len;
+}
+
+static const struct iio_enum mcp4728_powerdown_mode_enum = {
+Â Â Â .items = mcp4728_powerdown_modes,
+Â Â Â .num_items = ARRAY_SIZE(mcp4728_powerdown_modes),
+Â Â Â .get = mcp4728_get_powerdown_mode,
+Â Â Â .set = mcp4728_set_powerdown_mode,
+};
+
+// vref mode
+static const char *const mcp4728_vref_modes[] = {
+Â Â Â "vdd_ext",
+Â Â Â "internal",
+};
+
+static int mcp4728_get_vref_mode(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â const struct iio_chan_spec *chan)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+
+Â Â Â return data->channel_data[chan->channel].ref_mode;
+}
+
+static int mcp4728_set_vref_mode(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â const struct iio_chan_spec *chan,
+Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int mode)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â int ret;
+
+Â Â Â data->channel_data[chan->channel].ref_mode = mode;
+
+Â Â Â if (mode == MCP4728_VREF_EXTERNAL_VDD &&
+Â Â Â Â Â Â data->channel_data[chan->channel].g_mode == MCP4728_GAIN_X2) {
+Â Â Â Â Â Â dev_warn(
+Â Â Â Â Â Â Â Â Â &data->client->dev,
+Â Â Â Â Â Â Â Â Â "CH%d: Gain x2 not effective when vref is vdd, force to x1",
+Â Â Â Â Â Â Â Â Â chan->channel);
+Â Â Â Â Â Â data->channel_data[chan->channel].g_mode = MCP4728_GAIN_X1;
+Â Â Â }
+
+Â Â Â ret = mcp4728_program_channel_cfg(chan->channel, indio_dev);
+
+Â Â Â return ret;
+}
+static const struct iio_enum mcp4728_vref_mode_enum = {
+Â Â Â .items = mcp4728_vref_modes,
+Â Â Â .num_items = ARRAY_SIZE(mcp4728_vref_modes),
+Â Â Â .get = mcp4728_get_vref_mode,
+Â Â Â .set = mcp4728_set_vref_mode,
+};
+
+// gain
+static const char *const mcp4728_gain_modes[] = {
+Â Â Â "x1",
+Â Â Â "x2",
+};
+
+static int mcp4728_get_gain_mode(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â const struct iio_chan_spec *chan)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+
+Â Â Â return data->channel_data[chan->channel].g_mode;
+}
+
+static int mcp4728_set_gain_mode(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â const struct iio_chan_spec *chan,
+Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int mode)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â int ret;
+
+Â Â Â if (mode == MCP4728_GAIN_X2 &&
+Â Â Â Â Â Â data->channel_data[chan->channel].ref_mode ==
+Â Â Â Â Â Â Â Â Â MCP4728_VREF_EXTERNAL_VDD) {
+Â Â Â Â Â Â dev_err(&data->client->dev,
+Â Â Â Â Â Â Â Â Â "CH%d: Gain x2 not effective when vref is vdd",
+Â Â Â Â Â Â Â Â Â chan->channel);
+Â Â Â Â Â Â return -EINVAL;
+Â Â Â }
+
+Â Â Â data->channel_data[chan->channel].g_mode = mode;
+
+Â Â Â ret = mcp4728_program_channel_cfg(chan->channel, indio_dev);
+
+Â Â Â return ret;
+}
+
+static const struct iio_enum mcp4728_gain_mode_enum = {
+Â Â Â .items = mcp4728_gain_modes,
+Â Â Â .num_items = ARRAY_SIZE(mcp4728_gain_modes),
+Â Â Â .get = mcp4728_get_gain_mode,
+Â Â Â .set = mcp4728_set_gain_mode,
+};
+
+static const struct iio_chan_spec_ext_info mcp4728_ext_info[] = {
+Â Â Â {
+Â Â Â Â Â Â .name = "powerdown",
+Â Â Â Â Â Â .read = mcp4728_read_powerdown,
+Â Â Â Â Â Â .write = mcp4728_write_powerdown,
+Â Â Â Â Â Â .shared = IIO_SEPARATE,
+Â Â Â },
+Â Â Â IIO_ENUM("powerdown_mode", IIO_SEPARATE, &mcp4728_powerdown_mode_enum),
+Â Â Â IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE,
+Â Â Â Â Â Â Â Â Â Â Â &mcp4728_powerdown_mode_enum),
+Â Â Â IIO_ENUM("vref_mode", IIO_SEPARATE, &mcp4728_vref_mode_enum),
+Â Â Â IIO_ENUM_AVAILABLE("vref_mode", IIO_SHARED_BY_TYPE,
+Â Â Â Â Â Â Â Â Â Â Â &mcp4728_vref_mode_enum),
+Â Â Â IIO_ENUM("gain_mode", IIO_SEPARATE, &mcp4728_gain_mode_enum),
+Â Â Â IIO_ENUM_AVAILABLE("gain_mode", IIO_SHARED_BY_TYPE,
+Â Â Â Â Â Â Â Â Â Â Â &mcp4728_gain_mode_enum),
+Â Â Â {},
+};
+
+static const struct iio_chan_spec mcp4728_channels[MCP4728_N_CHANNELS] = {
+Â Â Â MCP4728_CHAN(0),
+Â Â Â MCP4728_CHAN(1),
+Â Â Â MCP4728_CHAN(2),
+Â Â Â MCP4728_CHAN(3),
+};
+
+static int mcp4728_full_scale_mV(u32 *full_scale_mV, int channel,
+Â Â Â Â Â Â Â Â Â Â Â Â Â struct mcp4728_data *data)
+{
+Â Â Â int ret;
+
+Â Â Â if (data->channel_data[channel].ref_mode == MCP4728_VREF_EXTERNAL_VDD)
+Â Â Â Â Â Â ret = regulator_get_voltage(data->vdd_reg);
+Â Â Â else
+Â Â Â Â Â Â ret = 2048000;
+
+Â Â Â if (ret < 0)
+Â Â Â Â Â Â return ret;
+
+Â Â Â if (ret == 0)
+Â Â Â Â Â Â return -EINVAL;
+
+Â Â Â *full_scale_mV = ret / 1000;
+Â Â Â return 0;
+}
+
+static u32 mcp4728_raw_to_mV(u32 raw, int channel, struct mcp4728_data *data)
+{
+Â Â Â int ret;
+Â Â Â u32 full_scale_mV;
+
+Â Â Â ret = mcp4728_full_scale_mV(&full_scale_mV, channel, data);
+Â Â Â if (ret)
+Â Â Â Â Â Â return ret;
+
+Â Â Â return (((raw + 1) * full_scale_mV) >> MCP4728_RESOLUTION);
+}
+
+static int mcp4728_read_raw(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â struct iio_chan_spec const *chan, int *val,
+Â Â Â Â Â Â Â Â Â Â Â Â int *val2, long mask)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â int ret;
+
+Â Â Â switch (mask) {
+Â Â Â case IIO_CHAN_INFO_RAW:
+Â Â Â Â Â Â *val = data->channel_data[chan->channel].dac_value;
+Â Â Â Â Â Â return IIO_VAL_INT;
+Â Â Â case IIO_CHAN_INFO_SCALE:
+Â Â Â Â Â Â if (data->channel_data[chan->channel].ref_mode ==
+Â Â Â Â Â Â Â Â Â MCP4728_VREF_EXTERNAL_VDD)
+Â Â Â Â Â Â Â Â Â ret = regulator_get_voltage(data->vdd_reg);
+Â Â Â Â Â Â else
+Â Â Â Â Â Â Â Â Â ret = 2048000;
+
+Â Â Â Â Â Â if (ret < 0)
+Â Â Â Â Â Â Â Â Â return ret;
+
+Â Â Â Â Â Â *val = ret / 1000;
+Â Â Â Â Â Â *val2 = MCP4728_RESOLUTION;
+Â Â Â Â Â Â return IIO_VAL_FRACTIONAL_LOG2;
+Â Â Â }
+Â Â Â return -EINVAL;
+}
+
+static int mcp4728_write_raw(struct iio_dev *indio_dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â struct iio_chan_spec const *chan, int val,
+Â Â Â Â Â Â Â Â Â Â Â Â Â int val2, long mask)
+{
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â int ret;
+
+Â Â Â switch (mask) {
+Â Â Â case IIO_CHAN_INFO_RAW:
+Â Â Â Â Â Â if (val < 0 || val > GENMASK(MCP4728_RESOLUTION - 1, 0))
+Â Â Â Â Â Â Â Â Â return -EINVAL;
+Â Â Â Â Â Â data->channel_data[chan->channel].dac_value = val;
+Â Â Â Â Â Â ret = mcp4728_program_channel_cfg(chan->channel, indio_dev);
+Â Â Â Â Â Â break;
+Â Â Â default:
+Â Â Â Â Â Â ret = -EINVAL;
+Â Â Â Â Â Â break;
+Â Â Â }
+
+Â Â Â return ret;
+}
+
+static const struct iio_info mcp4728_info = {
+Â Â Â .read_raw = mcp4728_read_raw,
+Â Â Â .write_raw = mcp4728_write_raw,
+Â Â Â .attrs = &mcp4728_attribute_group,
+};
+
+static int mcp4728_suspend(struct device *dev)
+{
+Â Â Â struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â int err = 0;
+Â Â Â unsigned int i;
+
+Â Â Â data->powerdown = true;
+
+Â Â Â for (i = 0; i < MCP4728_N_CHANNELS; i++) {
+Â Â Â Â Â Â int ret = mcp4728_program_channel_cfg(i, indio_dev);
+
+Â Â Â Â Â Â if (ret)
+Â Â Â Â Â Â Â Â Â err = ret; //save last error
+Â Â Â }
+Â Â Â return err;
+}
+
+static int mcp4728_resume(struct device *dev)
+{
+Â Â Â struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+Â Â Â struct mcp4728_data *data = iio_priv(indio_dev);
+Â Â Â int err = 0;
+Â Â Â unsigned int i;
+
+Â Â Â data->powerdown = false;
+
+Â Â Â for (i = 0; i < MCP4728_N_CHANNELS; i++) {
+Â Â Â Â Â Â int ret = mcp4728_program_channel_cfg(i, indio_dev);
+
+Â Â Â Â Â Â if (ret)
+Â Â Â Â Â Â Â Â Â err = ret; //save last error
+Â Â Â }
+Â Â Â return err;
+}
+static DEFINE_SIMPLE_DEV_PM_OPS(mcp4728_pm_ops, mcp4728_suspend,
+Â Â Â Â Â Â Â Â Â Â Â Â mcp4728_resume);
+
+static int mcp4728_init_channels_data(struct mcp4728_data *data)
+{
+Â Â Â u8 inbuf[MCP4728_READ_RESPONSE_LEN];
+Â Â Â int ret;
+Â Â Â unsigned int i;
+
+Â Â Â ret = i2c_master_recv(data->client, inbuf, MCP4728_READ_RESPONSE_LEN);
+Â Â Â if (ret < 0) {
+Â Â Â Â Â Â dev_err(&data->client->dev,
+Â Â Â Â Â Â Â Â Â "failed to read mcp4728 conf. Err=%d\n", ret);
+Â Â Â Â Â Â return ret;
+Â Â Â } else if (ret != MCP4728_READ_RESPONSE_LEN) {
+Â Â Â Â Â Â dev_err(&data->client->dev,
+Â Â Â Â Â Â Â Â Â "failed to read mcp4728 conf. Wrong Response Len ret=%d\n",
+Â Â Â Â Â Â Â Â Â ret);
+Â Â Â Â Â Â return -EIO;
+Â Â Â }
+
+Â Â Â for (i = 0; i < MCP4728_N_CHANNELS; i++) {
+Â Â Â Â Â Â struct mcp4728_channel_data *ch = &(data->channel_data[i]);
+Â Â Â Â Â Â u8 r2 = inbuf[i * 6 + 1];
+Â Â Â Â Â Â u8 r3 = inbuf[i * 6 + 2];
+Â Â Â Â Â Â u32 dac_mv;
+
+Â Â Â Â Â Â ch->dac_value = (r2 & 0x0F) << 8 | r3;
+Â Â Â Â Â Â ch->ref_mode = (r2 & MCP4728_CMD_VREF_MASK) >>
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â MCP4728_CMD_VREF_POS;
+Â Â Â Â Â Â ch->pd_mode = (r2 & MCP4728_CMD_PDMODE_MASK) >>
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â MCP4728_CMD_PDMODE_POS;
+Â Â Â Â Â Â ch->g_mode = (r2 & MCP4728_CMD_GAIN_MASK) >>
+Â Â Â Â Â Â Â Â Â Â Â Â Â MCP4728_CMD_GAIN_POS;
+Â Â Â Â Â Â if (ch->g_mode == MCP4728_GAIN_X2 &&
+Â Â Â Â Â Â Â Â Â ch->ref_mode == MCP4728_VREF_EXTERNAL_VDD)
+Â Â Â Â Â Â Â Â Â dev_warn(&data->client->dev,
+Â Â Â Â Â Â Â Â Â Â Â Â Â "CH%d: Gain x2 not effective when vref is vdd",
+Â Â Â Â Â Â Â Â Â Â Â Â Â i);
+
+Â Â Â Â Â Â dac_mv = mcp4728_raw_to_mV(ch->dac_value, i, data);
+Â Â Â Â Â Â dev_info(&data->client->dev,
+Â Â Â Â Â Â Â Â Â Â "CH%d: Voltage=%dmV VRef=%d PowerDown=%d Gain=%d\n", i,
+Â Â Â Â Â Â Â Â Â Â dac_mv, ch->ref_mode, ch->pd_mode, ch->g_mode);
+Â Â Â }
+
+Â Â Â return 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;
+Â Â Â 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;
+
+Â Â Â 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");
+Â Â Â Â Â Â goto err_disable_vdd_reg;
+Â Â Â }
+
+Â Â Â 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)
+{
+Â Â Â 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 }, {} };
+MODULE_DEVICE_TABLE(i2c, mcp4728_id);
+
+static const struct of_device_id mcp4728_of_match[] = {
+Â Â Â { .compatible = "microchip,mcp4728", .data = (void *)MCP4728 },
+Â Â Â {}
+};
+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),
+Â Â Â Â Â Â },
+Â Â Â Â Â Â .probe = mcp4728_probe,
+Â Â Â Â Â Â .remove = mcp4728_remove,
+Â Â Â Â Â Â .id_table = mcp4728_id,
+};
+module_i2c_driver(mcp4728_driver);
+
+MODULE_AUTHOR("Andrea Collamati <andrea.collamati@xxxxxxxxx>");
+MODULE_DESCRIPTION("MCP4728 12-bit DAC");
+MODULE_LICENSE("GPL");
--
2.17.1