[PATCH] iio: max5487: Add support for Maxim digital potentiometers

Daniel Baluta daniel.baluta at gmail.com
Wed Mar 9 14:43:31 EET 2016


On Wed, Mar 9, 2016 at 1:07 AM, Cristina Moraru
<cristina.moraru09 at gmail.com> wrote:
> Add implementation for Maxim MAX5487, MAX5488, MAX5489
> digital potentiometers.
>
> Signed-off-by: Cristina Moraru <cristina.moraru09 at gmail.com>
> ---
>  drivers/iio/potentiometer/Kconfig   |  11 ++
>  drivers/iio/potentiometer/Makefile  |   1 +
>  drivers/iio/potentiometer/max5487.c | 239 ++++++++++++++++++++++++++++++++++++
>  3 files changed, 251 insertions(+)
>  create mode 100644 drivers/iio/potentiometer/max5487.c
>
> diff --git a/drivers/iio/potentiometer/Kconfig b/drivers/iio/potentiometer/Kconfig
> index fd75db7..a2f8cc2 100644
> --- a/drivers/iio/potentiometer/Kconfig
> +++ b/drivers/iio/potentiometer/Kconfig
> @@ -5,6 +5,17 @@
>
>  menu "Digital potentiometers"
>
> +config MAX5487
> +        tristate "Maxim MAX5487/MAX5488/MAX5489  Digital Potentiometer driver"
> +        depends on SPI
> +        help
> +          Say yes here to build support for the Maxim
> +          MAX5487, MAX5488, MAX5489 digital potentiomenter
> +         chips.
> +
> +          To compile this driver as a module, choose M here: the
> +          module will be called max5487.
> +
>  config MCP4531
>         tristate "Microchip MCP45xx/MCP46xx Digital Potentiometer driver"
>         depends on I2C
> diff --git a/drivers/iio/potentiometer/Makefile b/drivers/iio/potentiometer/Makefile
> index 8afe492..bc2dfd8 100644
> --- a/drivers/iio/potentiometer/Makefile
> +++ b/drivers/iio/potentiometer/Makefile
> @@ -3,4 +3,5 @@
>  #
>
>  # When adding new entries keep the list in alphabetical order
> +obj-$(CONFIG_MAX5487) += max5487.o
>  obj-$(CONFIG_MCP4531) += mcp4531.o
> diff --git a/drivers/iio/potentiometer/max5487.c b/drivers/iio/potentiometer/max5487.c
> new file mode 100644
> index 0000000..c04d804
> --- /dev/null
> +++ b/drivers/iio/potentiometer/max5487.c
> @@ -0,0 +1,239 @@
> +/*
> + * max5487.c - Support for Maxim digital potentiometers
> + *
> + * Copyright (C) Cristina-Gabriela Moraru <cristina.moraru09 at gmail.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + */
> +
> +#include <linux/module.h>
> +#include <linux/spi/spi.h>
> +#include <linux/acpi.h>
> +#include <linux/regmap.h>
> +#include <linux/iio/sysfs.h>
> +
> +#include <linux/iio/iio.h>
> +
> +/* Register addresses */
> +#define MAX5487_WIPER_A                                0x01
> +#define MAX5487_WIPER_B                                0x02
> +
> +/* Non-volatile EEPROM memory addresses */
> +#define MAX5487_NONVOLATILE_A                  0x11
> +#define MAX5487_NONVOLATILE_B                  0x12
> +
> + /* Copy register values to non-volatile memory */
> +#define COPY_WP_NV_A                           0x21
> +#define COPY_WP_NV_B                           0x22
> +#define COPY_WP_NV_BOTH                                (COPY_WP_NV_A | COPY_WP_NV_B)
> +
> +/* Copy values from non-volatile memory to registers */
> +#define COPY_NV_WP_A                           0x31
> +#define COPY_NV_WP_B                           0x32
> +#define COPY_NV_WP_BOTH                                (COPY_NV_WP_A | COPY_NV_WP_B)

We decided to leave this out for the moment. We can add later patches
to take care of
saving and restoring the wiper position.

> +
> +enum {
> +       MAX5487,
> +       MAX5488,
> +       MAX5489,
> +};
> +
> +struct max5487_cfg {
> +       char *name;
This field is never used, remove it.

> +       int wipers;
> +       int max_pos;
> +       int kohms;
> +};
> +
> +#define SUPPORTED_CHIPS                3
> +static const struct max5487_cfg max5487_cfg[] = {
> +       [MAX5487] = { .name = "MAX5487", .wipers = 2,
> +                       .max_pos = 256, .kohms =  10,},
> +       [MAX5488] = { .name = "MAX5488", .wipers = 2,
> +                       .max_pos = 256, .kohms =  50,},
> +       [MAX5489] = { .name = "MAX5489", .wipers = 2,
> +                       .max_pos = 256, .kohms =  100,}
> +};
> +
> +struct max5487_data {
> +       struct device *dev;

We don't need dev here. We can always use regamp_get_device to retrieve it.

> +       struct mutex lock;
> +       struct regmap *regmap;
> +       int chip_id;
> +};
> +
> +#define MAX5487_CHANNEL(ch) {                                  \
> +       .type = IIO_RESISTANCE,                                 \
> +       .indexed = 1,                                           \
> +       .output = 1,                                            \
> +       .channel = ch,                                  \
> +       .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
> +       .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
> +}
> +
> +static const struct iio_chan_spec max5487_channels[] = {
> +       MAX5487_CHANNEL(0),
> +       MAX5487_CHANNEL(1)
> +};
> +
> +static IIO_CONST_ATTR(resistance_available, "0-255");
> +
> +static struct attribute *max5487_attributes[] = {
> +       &iio_const_attr_resistance_available.dev_attr.attr,
> +       NULL
> +};
> +

There is no standard attribute for restistance available. I'm not sure
if this is the proper way to describe a discrete range of resistor values.

So, for the moment please lets leave this out.

> +static const struct attribute_group max5487_group = {
> +       .attrs = max5487_attributes,
> +};
> +
> +static int max5487_write_raw(struct iio_dev *indio_dev,
> +                            struct iio_chan_spec const *chan,
> +                            int val, int val2, long mask)
> +{
> +       struct max5487_data *data = iio_priv(indio_dev);
> +       int wiper, ret;
> +
> +       wiper = (chan->channel) ? MAX5487_WIPER_B : MAX5487_WIPER_A;

No need for parenthesis around chan->channel.

> +
> +       switch (mask) {
> +       case IIO_CHAN_INFO_RAW:
> +               if (val < 0 || val > max5487_cfg[data->chip_id].max_pos)
> +                       return -EINVAL;
> +               mutex_lock(&data->lock);
> +               ret = regmap_write(data->regmap, wiper, val);
> +               mutex_unlock(&data->lock);
> +               return ret;
> +       default:
> +               return -EINVAL;
> +       }
> +}
> +
> +static int max5487_read_raw(struct iio_dev *indio_dev,
> +                                struct iio_chan_spec const *chan,
> +                                int *val, int *val2, long mask)
> +{
> +       struct max5487_data *data = iio_priv(indio_dev);
> +
> +       switch (mask) {
> +       case IIO_CHAN_INFO_SCALE:
> +               *val = 1000 * max5487_cfg[data->chip_id].kohms;
> +               *val2 = max5487_cfg[data->chip_id].max_pos;
> +               return IIO_VAL_FRACTIONAL;
> +       }
> +       return -EINVAL;
> +}
> +
> +static int max5487_spi_remove(struct spi_device *spi)
> +{
> +       struct iio_dev *indio_dev = dev_get_drvdata(&spi->dev);
> +       struct max5487_data *data = iio_priv(indio_dev);
> +       int ret;
> +
> +       /* Store wiper values to non-volatile memory */
> +       mutex_lock(&data->lock);
> +       ret = regmap_write(data->regmap, MAX5487_WIPER_A, 0);
> +       if (ret)
> +               goto exit_unlock;
> +
> +       ret = regmap_write(data->regmap, MAX5487_WIPER_B, 0);
> +
> +exit_unlock:
> +       mutex_unlock(&data->lock);
> +       return ret;
> +}
> +
> +static const struct iio_info max5487_info = {
> +       .attrs = &max5487_group,
> +       .read_raw = &max5487_read_raw,
> +       .write_raw = &max5487_write_raw,
> +       .driver_module = THIS_MODULE,
> +};
> +
> +static const struct regmap_range max5487_writable_ranges[] = {
> +       regmap_reg_range(0, 0x22)
> +};
> +
> +static const struct regmap_access_table max5487_writable_table = {
> +               .yes_ranges = max5487_writable_ranges,
> +               .n_yes_ranges = ARRAY_SIZE(max5487_writable_ranges),
> +};
> +
> +static const struct regmap_config max5487_regmap_config = {
> +       .reg_bits = 8,
> +       .val_bits = 8,
> +       .wr_table = &max5487_writable_table,
> +};
> +
> +static int max5487_spi_probe(struct spi_device *spi)
> +{
> +       struct iio_dev *indio_dev;
> +       struct max5487_data *data;
> +       const struct spi_device_id *id = spi_get_device_id(spi);
> +       int ret, i;
> +
> +       indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
> +       if (indio_dev == NULL)
> +               return -ENOMEM;
> +
> +       spi->mode = SPI_MODE_3;
> +       spi->max_speed_hz = 5000000;
> +       spi->bits_per_word = 16;
> +       ret = spi_setup(spi);
> +       if (ret)
> +               return ret;
> +
> +       dev_set_drvdata(&spi->dev, indio_dev);
> +       data = iio_priv(indio_dev);
> +       data->dev = &spi->dev;
> +       data->regmap = devm_regmap_init_spi(spi, &max5487_regmap_config);
> +       mutex_init(&data->lock);



> +       for (i = 0; i < SUPPORTED_CHIPS; i++)
> +               if (!strcmp(indio_dev->name, max5487_cfg[i].name))
> +                       data->chip_id = i;
> +
We should know by now for which chip the probe function was called. So, here
we can directly use id->driver_data;


> +       indio_dev->info = &max5487_info;
> +       indio_dev->name = id->name;
> +       indio_dev->dev.parent = &spi->dev;
> +       indio_dev->modes = INDIO_DIRECT_MODE;
> +       indio_dev->channels = max5487_channels;
> +       indio_dev->num_channels = ARRAY_SIZE(max5487_channels);
> +
> +       return iio_device_register(indio_dev);
> +}
> +
> +static const struct spi_device_id max5487_id[] = {
> +       { "MAX5487", MAX5487 },
> +       { "MAX5488", MAX5488 },
> +       { "MAX5489", MAX5489 },
> +       { }
> +};
> +MODULE_DEVICE_TABLE(spi, max5487_id);
> +
> +static const struct acpi_device_id max5487_acpi_match[] = {
> +       { "MAX5487", MAX5487 },
> +       { "MAX5488", MAX5488 },
> +       { "MAX5489", MAX5489 },
> +       { },
> +};
> +MODULE_DEVICE_TABLE(acpi, max5487_acpi_match);
> +
> +static struct spi_driver max5487_driver = {
> +       .driver = {
> +               .name = "max5487",
> +               .owner = THIS_MODULE,
> +               .acpi_match_table = ACPI_PTR(max5487_acpi_match),
> +       },
> +       .id_table = max5487_id,
> +       .probe = max5487_spi_probe,
> +       .remove = max5487_spi_remove
> +};
> +
> +module_spi_driver(max5487_driver);
> +
> +MODULE_AUTHOR("Cristina-Gabriela Moraru <cristina.moraru09 at gmail.com>");
> +MODULE_DESCRIPTION("max5487 SPI driver");
> +MODULE_LICENSE("GPL");
> --
> 1.9.1
>


More information about the firefly mailing list