Merge tag 'mvebu-soc-4.9-1' of git://git.infradead.org/linux-mvebu into next/soc
[linux/fpc-iii.git] / drivers / iio / potentiometer / max5487.c
blob6c50939a2e8395c41dec1799f80e0ef5a02b3026
1 /*
2 * max5487.c - Support for MAX5487, MAX5488, MAX5489 digital potentiometers
4 * Copyright (C) 2016 Cristina-Gabriela Moraru <cristina.moraru09@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/spi/spi.h>
13 #include <linux/acpi.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/iio.h>
18 #define MAX5487_WRITE_WIPER_A (0x01 << 8)
19 #define MAX5487_WRITE_WIPER_B (0x02 << 8)
21 /* copy both wiper regs to NV regs */
22 #define MAX5487_COPY_AB_TO_NV (0x23 << 8)
23 /* copy both NV regs to wiper regs */
24 #define MAX5487_COPY_NV_TO_AB (0x33 << 8)
26 #define MAX5487_MAX_POS 255
28 struct max5487_data {
29 struct spi_device *spi;
30 int kohms;
33 #define MAX5487_CHANNEL(ch, addr) { \
34 .type = IIO_RESISTANCE, \
35 .indexed = 1, \
36 .output = 1, \
37 .channel = ch, \
38 .address = addr, \
39 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
40 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
43 static const struct iio_chan_spec max5487_channels[] = {
44 MAX5487_CHANNEL(0, MAX5487_WRITE_WIPER_A),
45 MAX5487_CHANNEL(1, MAX5487_WRITE_WIPER_B),
48 static int max5487_write_cmd(struct spi_device *spi, u16 cmd)
50 return spi_write(spi, (const void *) &cmd, sizeof(u16));
53 static int max5487_read_raw(struct iio_dev *indio_dev,
54 struct iio_chan_spec const *chan,
55 int *val, int *val2, long mask)
57 struct max5487_data *data = iio_priv(indio_dev);
59 if (mask != IIO_CHAN_INFO_SCALE)
60 return -EINVAL;
62 *val = 1000 * data->kohms;
63 *val2 = MAX5487_MAX_POS;
65 return IIO_VAL_FRACTIONAL;
68 static int max5487_write_raw(struct iio_dev *indio_dev,
69 struct iio_chan_spec const *chan,
70 int val, int val2, long mask)
72 struct max5487_data *data = iio_priv(indio_dev);
74 if (mask != IIO_CHAN_INFO_RAW)
75 return -EINVAL;
77 if (val < 0 || val > MAX5487_MAX_POS)
78 return -EINVAL;
80 return max5487_write_cmd(data->spi, chan->address | val);
83 static const struct iio_info max5487_info = {
84 .read_raw = max5487_read_raw,
85 .write_raw = max5487_write_raw,
86 .driver_module = THIS_MODULE,
89 static int max5487_spi_probe(struct spi_device *spi)
91 struct iio_dev *indio_dev;
92 struct max5487_data *data;
93 const struct spi_device_id *id = spi_get_device_id(spi);
94 int ret;
96 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
97 if (!indio_dev)
98 return -ENOMEM;
100 dev_set_drvdata(&spi->dev, indio_dev);
101 data = iio_priv(indio_dev);
103 data->spi = spi;
104 data->kohms = id->driver_data;
106 indio_dev->info = &max5487_info;
107 indio_dev->name = id->name;
108 indio_dev->dev.parent = &spi->dev;
109 indio_dev->modes = INDIO_DIRECT_MODE;
110 indio_dev->channels = max5487_channels;
111 indio_dev->num_channels = ARRAY_SIZE(max5487_channels);
113 /* restore both wiper regs from NV regs */
114 ret = max5487_write_cmd(data->spi, MAX5487_COPY_NV_TO_AB);
115 if (ret < 0)
116 return ret;
118 return iio_device_register(indio_dev);
121 static int max5487_spi_remove(struct spi_device *spi)
123 struct iio_dev *indio_dev = dev_get_drvdata(&spi->dev);
125 iio_device_unregister(indio_dev);
127 /* save both wiper regs to NV regs */
128 return max5487_write_cmd(spi, MAX5487_COPY_AB_TO_NV);
131 static const struct spi_device_id max5487_id[] = {
132 { "MAX5487", 10 },
133 { "MAX5488", 50 },
134 { "MAX5489", 100 },
137 MODULE_DEVICE_TABLE(spi, max5487_id);
139 static const struct acpi_device_id max5487_acpi_match[] = {
140 { "MAX5487", 10 },
141 { "MAX5488", 50 },
142 { "MAX5489", 100 },
143 { },
145 MODULE_DEVICE_TABLE(acpi, max5487_acpi_match);
147 static struct spi_driver max5487_driver = {
148 .driver = {
149 .name = "max5487",
150 .owner = THIS_MODULE,
151 .acpi_match_table = ACPI_PTR(max5487_acpi_match),
153 .id_table = max5487_id,
154 .probe = max5487_spi_probe,
155 .remove = max5487_spi_remove
157 module_spi_driver(max5487_driver);
159 MODULE_AUTHOR("Cristina-Gabriela Moraru <cristina.moraru09@gmail.com>");
160 MODULE_DESCRIPTION("max5487 SPI driver");
161 MODULE_LICENSE("GPL v2");