RDMA/mlx5: Fix query DCT via DEVX
[linux-2.6-microblaze.git] / drivers / iio / potentiometer / max5481.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Maxim Integrated MAX5481-MAX5484 digital potentiometer driver
4  * Copyright 2016 Rockwell Collins
5  *
6  * Datasheet:
7  * https://datasheets.maximintegrated.com/en/ds/MAX5481-MAX5484.pdf
8  */
9
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>
16
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)
23
24 #define MAX5481_MAX_POS    1023
25
26 enum max5481_variant {
27         max5481,
28         max5482,
29         max5483,
30         max5484,
31 };
32
33 struct max5481_cfg {
34         int kohms;
35 };
36
37 static const struct max5481_cfg max5481_cfg[] = {
38         [max5481] = { .kohms =  10, },
39         [max5482] = { .kohms =  50, },
40         [max5483] = { .kohms =  10, },
41         [max5484] = { .kohms =  50, },
42 };
43
44 struct max5481_data {
45         struct spi_device *spi;
46         const struct max5481_cfg *cfg;
47         u8 msg[3] ____cacheline_aligned;
48 };
49
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),   \
57 }
58
59 static const struct iio_chan_spec max5481_channels[] = {
60         MAX5481_CHANNEL,
61 };
62
63 static int max5481_write_cmd(struct max5481_data *data, u8 cmd, u16 val)
64 {
65         struct spi_device *spi = data->spi;
66
67         data->msg[0] = cmd;
68
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);
74
75         case MAX5481_COPY_AB_TO_NV:
76         case MAX5481_COPY_NV_TO_AB:
77                 return spi_write(spi, data->msg, 1);
78
79         default:
80                 return -EIO;
81         }
82 }
83
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)
87 {
88         struct max5481_data *data = iio_priv(indio_dev);
89
90         if (mask != IIO_CHAN_INFO_SCALE)
91                 return -EINVAL;
92
93         *val = 1000 * data->cfg->kohms;
94         *val2 = MAX5481_MAX_POS;
95
96         return IIO_VAL_FRACTIONAL;
97 }
98
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)
102 {
103         struct max5481_data *data = iio_priv(indio_dev);
104
105         if (mask != IIO_CHAN_INFO_RAW)
106                 return -EINVAL;
107
108         if (val < 0 || val > MAX5481_MAX_POS)
109                 return -EINVAL;
110
111         return max5481_write_cmd(data, MAX5481_WRITE_WIPER, val);
112 }
113
114 static const struct iio_info max5481_info = {
115         .read_raw = max5481_read_raw,
116         .write_raw = max5481_write_raw,
117 };
118
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] },
124         { }
125 };
126 MODULE_DEVICE_TABLE(of, max5481_match);
127
128 static int max5481_probe(struct spi_device *spi)
129 {
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;
134
135         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
136         if (!indio_dev)
137                 return -ENOMEM;
138
139         spi_set_drvdata(spi, indio_dev);
140         data = iio_priv(indio_dev);
141
142         data->spi = spi;
143
144         data->cfg = device_get_match_data(&spi->dev);
145         if (!data->cfg)
146                 data->cfg = &max5481_cfg[id->driver_data];
147
148         indio_dev->name = id->name;
149         indio_dev->modes = INDIO_DIRECT_MODE;
150
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);
155
156         /* restore wiper from NV */
157         ret = max5481_write_cmd(data, MAX5481_COPY_NV_TO_AB, 0);
158         if (ret < 0)
159                 return ret;
160
161         return iio_device_register(indio_dev);
162 }
163
164 static int max5481_remove(struct spi_device *spi)
165 {
166         struct iio_dev *indio_dev = spi_get_drvdata(spi);
167         struct max5481_data *data = iio_priv(indio_dev);
168
169         iio_device_unregister(indio_dev);
170
171         /* save wiper reg to NV reg */
172         return max5481_write_cmd(data, MAX5481_COPY_AB_TO_NV, 0);
173 }
174
175 static const struct spi_device_id max5481_id_table[] = {
176         { "max5481", max5481 },
177         { "max5482", max5482 },
178         { "max5483", max5483 },
179         { "max5484", max5484 },
180         { }
181 };
182 MODULE_DEVICE_TABLE(spi, max5481_id_table);
183
184 static struct spi_driver max5481_driver = {
185         .driver = {
186                 .name  = "max5481",
187                 .of_match_table = max5481_match,
188         },
189         .probe = max5481_probe,
190         .remove = max5481_remove,
191         .id_table = max5481_id_table,
192 };
193
194 module_spi_driver(max5481_driver);
195
196 MODULE_AUTHOR("Maury Anderson <maury.anderson@rockwellcollins.com>");
197 MODULE_DESCRIPTION("max5481 SPI driver");
198 MODULE_LICENSE("GPL v2");