Message ID | 20221001173104.492027-1-angelo.dureghello@timesys.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | iio: dac: add support for max5522 | expand |
I apologize. Please discard this wrong patch, resending. regards, angelo On Sat, Oct 1, 2022 at 7:32 PM Angelo Dureghello <angelo.dureghello@timesys.com> wrote: > > Add initial support for dac max5522. > > Tested writing DAC A and B with some values, > from 0 to 1023, measured output voltages, driver works properly. > > Signed-off-by: Angelo Dureghello <angelo.dureghello@timesys.com> > --- > drivers/iio/dac/Kconfig | 1 + > drivers/iio/dac/max5522.c | 159 ++++++++++++++++++++------------------ > 2 files changed, 86 insertions(+), 74 deletions(-) > > diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig > index 52bb393d043b..91b284342db6 100644 > --- a/drivers/iio/dac/Kconfig > +++ b/drivers/iio/dac/Kconfig > @@ -360,6 +360,7 @@ config MAX517 > config MAX5522 > tristate "Maxim MAX5522 DAC driver" > depends on SPI > + select REGMAP_SPI if SPI_MASTER > help > Support for the MAX5522 2 channels > Digital to Analog Converters (DAC). > diff --git a/drivers/iio/dac/max5522.c b/drivers/iio/dac/max5522.c > index 5d3dce0e6bb1..3932834cc7b7 100644 > --- a/drivers/iio/dac/max5522.c > +++ b/drivers/iio/dac/max5522.c > @@ -3,11 +3,10 @@ > * Maxim MAX5522 > * Dual, Ultra-Low-Power 10-Bit, Voltage-Output DACs > * > - * Copyright 2022 TImesys (C) > + * Copyright 2022 Timesys Corp. > */ > > #include <linux/device.h> > -#include <linux/err.h> > #include <linux/kernel.h> > #include <linux/module.h> > #include <linux/spi/spi.h> > @@ -20,7 +19,6 @@ > #include <linux/iio/sysfs.h> > > #define MAX5522_MAX_ADDR 15 > - > #define MAX5522_CTRL_NONE 0 > #define MAX5522_CTRL_LOAD_IN_A 9 > #define MAX5522_CTRL_LOAD_IN_B 10 > @@ -35,46 +33,33 @@ struct max5522_chip_info { > struct max5522_state { > struct regmap *regmap; > const struct max5522_chip_info *chip_info; > - struct mutex lock; > + unsigned short dac_cache[2]; > + unsigned int vrefin_mv; > + struct regulator *vrefin_reg; > }; > > -const struct iio_chan_spec max5522_channels[] = { > -{ > - .type = IIO_VOLTAGE, > - .indexed = 1, > - .output = 1, > - .channel = 0, > - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | > - BIT(IIO_CHAN_INFO_SCALE), > - .address = 0, > - .scan_type = { > - .sign = 'u', > - .realbits = 10, > - .storagebits = 16, > - .shift = 6, > - }, > -}, { > - .type = IIO_VOLTAGE, > - .indexed = 1, > - .output = 1, > - .channel = 1, > - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | > - BIT(IIO_CHAN_INFO_SCALE), > - .address = 1, > - .scan_type = { > - .sign = 'u', > - .realbits = 10, > - .storagebits = 16, > - .shift = 6, > - }, > +#define MAX5522_CHANNEL(chan) { \ > + .type = IIO_VOLTAGE, \ > + .indexed = 1, \ > + .output = 1, \ > + .channel = chan, \ > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ > + BIT(IIO_CHAN_INFO_SCALE), \ > + .scan_type = { \ > + .sign = 'u', \ > + .realbits = 10, \ > + .storagebits = 16, \ > + .shift = 2, \ > + } \ > } > + > +const struct iio_chan_spec max5522_channels[] = { > + MAX5522_CHANNEL(0), > + MAX5522_CHANNEL(1), > }; > > enum max5522_type { > ID_MAX5522, > - ID_MAX5523, > - ID_MAX5524, > - ID_MAX5525, > }; > > static const struct max5522_chip_info max5522_chip_info_tbl[] = { > @@ -82,43 +67,54 @@ static const struct max5522_chip_info max5522_chip_info_tbl[] = { > .channels = max5522_channels, > .num_channels = 2, > }, > - [ID_MAX5523] = { > - .channels = max5522_channels, > - .num_channels = 2, > - }, > - [ID_MAX5524] = { > - .channels = max5522_channels, > - .num_channels = 2, > - }, > - [ID_MAX5525] = { > - .channels = max5522_channels, > - .num_channels = 2, > - }, > }; > > -static unsigned int max5522_info_to_reg(struct iio_chan_spec const *chan, > - long info) > +static inline int max5522_info_to_reg(struct iio_chan_spec const *chan) > { > + return MAX5522_REG_DATA(chan->channel); > +} > + > +static int max5522_read_raw(struct iio_dev *indio_dev, > + struct iio_chan_spec const *chan, int *val, int *val2, long info) > +{ > + struct max5522_state *state = iio_priv(indio_dev); > + > switch (info) { > case IIO_CHAN_INFO_RAW: > - return MAX5522_REG_DATA(chan->address); > + *val = state->dac_cache[chan->channel]; > + return IIO_VAL_INT; > + case IIO_CHAN_INFO_SCALE: > + *val = state->vrefin_mv; > + *val2 = 10; > + return IIO_VAL_FRACTIONAL_LOG2; > default: > - break; > + return -EINVAL; > } > > - return 0; > + return -EINVAL; > } > > static int max5522_write_raw(struct iio_dev *indio_dev, > struct iio_chan_spec const *chan, int val, int val2, long info) > { > - struct max5522_state *st = iio_priv(indio_dev); > + struct max5522_state *state = iio_priv(indio_dev); > + int rval; > > - return regmap_write(st->regmap, max5522_info_to_reg(chan, info), > + if (val > 1023 || val < 0) > + return -EINVAL; > + > + rval = regmap_write(state->regmap, max5522_info_to_reg(chan), > val << chan->scan_type.shift); > + if (rval < 0) > + return rval; > + > + state->dac_cache[chan->channel] = val; > + > + return 0; > } > > static const struct iio_info max5522_info = { > + .read_raw = max5522_read_raw, > .write_raw = max5522_write_raw, > }; > > @@ -131,40 +127,61 @@ static void max5522_remove(struct device *dev) > } > > static const struct regmap_config max5522_regmap_config = { > - > + .reg_bits = 4, > .val_bits = 12, > - > .max_register = MAX5522_MAX_ADDR, > - .cache_type = REGCACHE_RBTREE, > }; > > static int max5522_spi_probe(struct spi_device *spi) > { > const struct spi_device_id *id = spi_get_device_id(spi); > - struct regmap *regmap; > struct iio_dev *indio_dev; > - struct max5522_state *st; > + struct max5522_state *state; > + int ret; > > - indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); > + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state)); > if (indio_dev == NULL) { > dev_err(&spi->dev, "failed to allocate iio device\n"); > return -ENOMEM; > } > > - st = iio_priv(indio_dev); > + state = iio_priv(indio_dev); > + state->chip_info = &max5522_chip_info_tbl[id->driver_data]; > > - st->chip_info = &max5522_chip_info_tbl[id->driver_data]; > + state->vrefin_reg = devm_regulator_get(&spi->dev, "vrefin"); > + if (IS_ERR(state->vrefin_reg)) > + return dev_err_probe(&spi->dev, PTR_ERR(state->vrefin_reg), > + "Vrefin regulator not specified\n"); > + > + ret = regulator_get_voltage(state->vrefin_reg); > + if (ret < 0) { > + dev_err(&spi->dev, "Failed to read vrefin regulator: %d\n", > + ret); > + goto error_disable_vrefin_reg; > + } > + state->vrefin_mv = ret / 1000; > > spi_set_drvdata(spi, indio_dev); > > - regmap = devm_regmap_init_spi(spi, &max5522_regmap_config); > + state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config); > > - if (IS_ERR(regmap)) > - return PTR_ERR(regmap); > + if (IS_ERR(state->regmap)) > + return PTR_ERR(state->regmap); > > - dev_err(&spi->dev, "device probed\n"); > + dev_info(&spi->dev, "iio dac ready"); > > - return 0; > + indio_dev->info = &max5522_info; > + indio_dev->modes = INDIO_DIRECT_MODE; > + indio_dev->channels = max5522_channels; > + indio_dev->num_channels = ARRAY_SIZE(max5522_channels); > + indio_dev->name = id->name; > + > + return iio_device_register(indio_dev); > + > +error_disable_vrefin_reg: > + regulator_disable(state->vrefin_reg); > + > + return ret; > } > > static void max5522_spi_remove(struct spi_device *spi) > @@ -174,9 +191,6 @@ static void max5522_spi_remove(struct spi_device *spi) > > static const struct spi_device_id max5522_ids[] = { > { "max5522", ID_MAX5522 }, > - { "max5522", ID_MAX5523 }, > - { "max5522", ID_MAX5524 }, > - { "max5525", ID_MAX5525 }, > {} > }; > MODULE_DEVICE_TABLE(spi, ad5360_ids); > @@ -184,9 +198,6 @@ MODULE_DEVICE_TABLE(spi, ad5360_ids); > > static const struct of_device_id max5522_of_match[] = { > { .compatible = "maxim,max5522", }, > - { .compatible = "maxim,max5523", }, > - { .compatible = "maxim,max5524", }, > - { .compatible = "maxim,max5525", }, > {}, > }; > > -- > 2.37.3 >
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig index 52bb393d043b..91b284342db6 100644 --- a/drivers/iio/dac/Kconfig +++ b/drivers/iio/dac/Kconfig @@ -360,6 +360,7 @@ config MAX517 config MAX5522 tristate "Maxim MAX5522 DAC driver" depends on SPI + select REGMAP_SPI if SPI_MASTER help Support for the MAX5522 2 channels Digital to Analog Converters (DAC). diff --git a/drivers/iio/dac/max5522.c b/drivers/iio/dac/max5522.c index 5d3dce0e6bb1..3932834cc7b7 100644 --- a/drivers/iio/dac/max5522.c +++ b/drivers/iio/dac/max5522.c @@ -3,11 +3,10 @@ * Maxim MAX5522 * Dual, Ultra-Low-Power 10-Bit, Voltage-Output DACs * - * Copyright 2022 TImesys (C) + * Copyright 2022 Timesys Corp. */ #include <linux/device.h> -#include <linux/err.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/spi/spi.h> @@ -20,7 +19,6 @@ #include <linux/iio/sysfs.h> #define MAX5522_MAX_ADDR 15 - #define MAX5522_CTRL_NONE 0 #define MAX5522_CTRL_LOAD_IN_A 9 #define MAX5522_CTRL_LOAD_IN_B 10 @@ -35,46 +33,33 @@ struct max5522_chip_info { struct max5522_state { struct regmap *regmap; const struct max5522_chip_info *chip_info; - struct mutex lock; + unsigned short dac_cache[2]; + unsigned int vrefin_mv; + struct regulator *vrefin_reg; }; -const struct iio_chan_spec max5522_channels[] = { -{ - .type = IIO_VOLTAGE, - .indexed = 1, - .output = 1, - .channel = 0, - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE), - .address = 0, - .scan_type = { - .sign = 'u', - .realbits = 10, - .storagebits = 16, - .shift = 6, - }, -}, { - .type = IIO_VOLTAGE, - .indexed = 1, - .output = 1, - .channel = 1, - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE), - .address = 1, - .scan_type = { - .sign = 'u', - .realbits = 10, - .storagebits = 16, - .shift = 6, - }, +#define MAX5522_CHANNEL(chan) { \ + .type = IIO_VOLTAGE, \ + .indexed = 1, \ + .output = 1, \ + .channel = chan, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \ + .scan_type = { \ + .sign = 'u', \ + .realbits = 10, \ + .storagebits = 16, \ + .shift = 2, \ + } \ } + +const struct iio_chan_spec max5522_channels[] = { + MAX5522_CHANNEL(0), + MAX5522_CHANNEL(1), }; enum max5522_type { ID_MAX5522, - ID_MAX5523, - ID_MAX5524, - ID_MAX5525, }; static const struct max5522_chip_info max5522_chip_info_tbl[] = { @@ -82,43 +67,54 @@ static const struct max5522_chip_info max5522_chip_info_tbl[] = { .channels = max5522_channels, .num_channels = 2, }, - [ID_MAX5523] = { - .channels = max5522_channels, - .num_channels = 2, - }, - [ID_MAX5524] = { - .channels = max5522_channels, - .num_channels = 2, - }, - [ID_MAX5525] = { - .channels = max5522_channels, - .num_channels = 2, - }, }; -static unsigned int max5522_info_to_reg(struct iio_chan_spec const *chan, - long info) +static inline int max5522_info_to_reg(struct iio_chan_spec const *chan) { + return MAX5522_REG_DATA(chan->channel); +} + +static int max5522_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, int *val, int *val2, long info) +{ + struct max5522_state *state = iio_priv(indio_dev); + switch (info) { case IIO_CHAN_INFO_RAW: - return MAX5522_REG_DATA(chan->address); + *val = state->dac_cache[chan->channel]; + return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: + *val = state->vrefin_mv; + *val2 = 10; + return IIO_VAL_FRACTIONAL_LOG2; default: - break; + return -EINVAL; } - return 0; + return -EINVAL; } static int max5522_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long info) { - struct max5522_state *st = iio_priv(indio_dev); + struct max5522_state *state = iio_priv(indio_dev); + int rval; - return regmap_write(st->regmap, max5522_info_to_reg(chan, info), + if (val > 1023 || val < 0) + return -EINVAL; + + rval = regmap_write(state->regmap, max5522_info_to_reg(chan), val << chan->scan_type.shift); + if (rval < 0) + return rval; + + state->dac_cache[chan->channel] = val; + + return 0; } static const struct iio_info max5522_info = { + .read_raw = max5522_read_raw, .write_raw = max5522_write_raw, }; @@ -131,40 +127,61 @@ static void max5522_remove(struct device *dev) } static const struct regmap_config max5522_regmap_config = { - + .reg_bits = 4, .val_bits = 12, - .max_register = MAX5522_MAX_ADDR, - .cache_type = REGCACHE_RBTREE, }; static int max5522_spi_probe(struct spi_device *spi) { const struct spi_device_id *id = spi_get_device_id(spi); - struct regmap *regmap; struct iio_dev *indio_dev; - struct max5522_state *st; + struct max5522_state *state; + int ret; - indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state)); if (indio_dev == NULL) { dev_err(&spi->dev, "failed to allocate iio device\n"); return -ENOMEM; } - st = iio_priv(indio_dev); + state = iio_priv(indio_dev); + state->chip_info = &max5522_chip_info_tbl[id->driver_data]; - st->chip_info = &max5522_chip_info_tbl[id->driver_data]; + state->vrefin_reg = devm_regulator_get(&spi->dev, "vrefin"); + if (IS_ERR(state->vrefin_reg)) + return dev_err_probe(&spi->dev, PTR_ERR(state->vrefin_reg), + "Vrefin regulator not specified\n"); + + ret = regulator_get_voltage(state->vrefin_reg); + if (ret < 0) { + dev_err(&spi->dev, "Failed to read vrefin regulator: %d\n", + ret); + goto error_disable_vrefin_reg; + } + state->vrefin_mv = ret / 1000; spi_set_drvdata(spi, indio_dev); - regmap = devm_regmap_init_spi(spi, &max5522_regmap_config); + state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config); - if (IS_ERR(regmap)) - return PTR_ERR(regmap); + if (IS_ERR(state->regmap)) + return PTR_ERR(state->regmap); - dev_err(&spi->dev, "device probed\n"); + dev_info(&spi->dev, "iio dac ready"); - return 0; + indio_dev->info = &max5522_info; + indio_dev->modes = INDIO_DIRECT_MODE; + indio_dev->channels = max5522_channels; + indio_dev->num_channels = ARRAY_SIZE(max5522_channels); + indio_dev->name = id->name; + + return iio_device_register(indio_dev); + +error_disable_vrefin_reg: + regulator_disable(state->vrefin_reg); + + return ret; } static void max5522_spi_remove(struct spi_device *spi) @@ -174,9 +191,6 @@ static void max5522_spi_remove(struct spi_device *spi) static const struct spi_device_id max5522_ids[] = { { "max5522", ID_MAX5522 }, - { "max5522", ID_MAX5523 }, - { "max5522", ID_MAX5524 }, - { "max5525", ID_MAX5525 }, {} }; MODULE_DEVICE_TABLE(spi, ad5360_ids); @@ -184,9 +198,6 @@ MODULE_DEVICE_TABLE(spi, ad5360_ids); static const struct of_device_id max5522_of_match[] = { { .compatible = "maxim,max5522", }, - { .compatible = "maxim,max5523", }, - { .compatible = "maxim,max5524", }, - { .compatible = "maxim,max5525", }, {}, };
Add initial support for dac max5522. Tested writing DAC A and B with some values, from 0 to 1023, measured output voltages, driver works properly. Signed-off-by: Angelo Dureghello <angelo.dureghello@timesys.com> --- drivers/iio/dac/Kconfig | 1 + drivers/iio/dac/max5522.c | 159 ++++++++++++++++++++------------------ 2 files changed, 86 insertions(+), 74 deletions(-)