WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / potentiometer / max5481.c
bloba88ed0eb3adc69838b34071a8beac93f85530a85
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Maxim Integrated MAX5481-MAX5484 digital potentiometer driver
4 * Copyright 2016 Rockwell Collins
6 * Datasheet:
7 * https://datasheets.maximintegrated.com/en/ds/MAX5481-MAX5484.pdf
8 */
10 #include <linux/iio/iio.h>
11 #include <linux/iio/sysfs.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/property.h>
15 #include <linux/spi/spi.h>
17 /* write wiper reg */
18 #define MAX5481_WRITE_WIPER (0 << 4)
19 /* copy wiper reg to NV reg */
20 #define MAX5481_COPY_AB_TO_NV (2 << 4)
21 /* copy NV reg to wiper reg */
22 #define MAX5481_COPY_NV_TO_AB (3 << 4)
24 #define MAX5481_MAX_POS 1023
26 enum max5481_variant {
27 max5481,
28 max5482,
29 max5483,
30 max5484,
33 struct max5481_cfg {
34 int kohms;
37 static const struct max5481_cfg max5481_cfg[] = {
38 [max5481] = { .kohms = 10, },
39 [max5482] = { .kohms = 50, },
40 [max5483] = { .kohms = 10, },
41 [max5484] = { .kohms = 50, },
44 struct max5481_data {
45 struct spi_device *spi;
46 const struct max5481_cfg *cfg;
47 u8 msg[3] ____cacheline_aligned;
50 #define MAX5481_CHANNEL { \
51 .type = IIO_RESISTANCE, \
52 .indexed = 1, \
53 .output = 1, \
54 .channel = 0, \
55 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
56 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
59 static const struct iio_chan_spec max5481_channels[] = {
60 MAX5481_CHANNEL,
63 static int max5481_write_cmd(struct max5481_data *data, u8 cmd, u16 val)
65 struct spi_device *spi = data->spi;
67 data->msg[0] = cmd;
69 switch (cmd) {
70 case MAX5481_WRITE_WIPER:
71 data->msg[1] = val >> 2;
72 data->msg[2] = (val & 0x3) << 6;
73 return spi_write(spi, data->msg, 3);
75 case MAX5481_COPY_AB_TO_NV:
76 case MAX5481_COPY_NV_TO_AB:
77 return spi_write(spi, data->msg, 1);
79 default:
80 return -EIO;
84 static int max5481_read_raw(struct iio_dev *indio_dev,
85 struct iio_chan_spec const *chan,
86 int *val, int *val2, long mask)
88 struct max5481_data *data = iio_priv(indio_dev);
90 if (mask != IIO_CHAN_INFO_SCALE)
91 return -EINVAL;
93 *val = 1000 * data->cfg->kohms;
94 *val2 = MAX5481_MAX_POS;
96 return IIO_VAL_FRACTIONAL;
99 static int max5481_write_raw(struct iio_dev *indio_dev,
100 struct iio_chan_spec const *chan,
101 int val, int val2, long mask)
103 struct max5481_data *data = iio_priv(indio_dev);
105 if (mask != IIO_CHAN_INFO_RAW)
106 return -EINVAL;
108 if (val < 0 || val > MAX5481_MAX_POS)
109 return -EINVAL;
111 return max5481_write_cmd(data, MAX5481_WRITE_WIPER, val);
114 static const struct iio_info max5481_info = {
115 .read_raw = max5481_read_raw,
116 .write_raw = max5481_write_raw,
119 static const struct of_device_id max5481_match[] = {
120 { .compatible = "maxim,max5481", .data = &max5481_cfg[max5481] },
121 { .compatible = "maxim,max5482", .data = &max5481_cfg[max5482] },
122 { .compatible = "maxim,max5483", .data = &max5481_cfg[max5483] },
123 { .compatible = "maxim,max5484", .data = &max5481_cfg[max5484] },
126 MODULE_DEVICE_TABLE(of, max5481_match);
128 static int max5481_probe(struct spi_device *spi)
130 struct iio_dev *indio_dev;
131 struct max5481_data *data;
132 const struct spi_device_id *id = spi_get_device_id(spi);
133 int ret;
135 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
136 if (!indio_dev)
137 return -ENOMEM;
139 dev_set_drvdata(&spi->dev, indio_dev);
140 data = iio_priv(indio_dev);
142 data->spi = spi;
144 data->cfg = device_get_match_data(&spi->dev);
145 if (!data->cfg)
146 data->cfg = &max5481_cfg[id->driver_data];
148 indio_dev->name = id->name;
149 indio_dev->modes = INDIO_DIRECT_MODE;
151 /* variant specific configuration */
152 indio_dev->info = &max5481_info;
153 indio_dev->channels = max5481_channels;
154 indio_dev->num_channels = ARRAY_SIZE(max5481_channels);
156 /* restore wiper from NV */
157 ret = max5481_write_cmd(data, MAX5481_COPY_NV_TO_AB, 0);
158 if (ret < 0)
159 return ret;
161 return iio_device_register(indio_dev);
164 static int max5481_remove(struct spi_device *spi)
166 struct iio_dev *indio_dev = dev_get_drvdata(&spi->dev);
167 struct max5481_data *data = iio_priv(indio_dev);
169 iio_device_unregister(indio_dev);
171 /* save wiper reg to NV reg */
172 return max5481_write_cmd(data, MAX5481_COPY_AB_TO_NV, 0);
175 static const struct spi_device_id max5481_id_table[] = {
176 { "max5481", max5481 },
177 { "max5482", max5482 },
178 { "max5483", max5483 },
179 { "max5484", max5484 },
182 MODULE_DEVICE_TABLE(spi, max5481_id_table);
184 static struct spi_driver max5481_driver = {
185 .driver = {
186 .name = "max5481",
187 .of_match_table = max5481_match,
189 .probe = max5481_probe,
190 .remove = max5481_remove,
191 .id_table = max5481_id_table,
194 module_spi_driver(max5481_driver);
196 MODULE_AUTHOR("Maury Anderson <maury.anderson@rockwellcollins.com>");
197 MODULE_DESCRIPTION("max5481 SPI driver");
198 MODULE_LICENSE("GPL v2");