1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Maxim Integrated DS1803 and similar digital potentiometer driver
4 * Copyright (c) 2016 Slawomir Stepien
5 * Copyright (c) 2022 Jagath Jog J
6 *
7 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf
8 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3502.pdf
9 *
10 * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
11 * ds1803 2 256 10, 50, 100 0101xxx
12 * ds3502 1 128 10 01010xx
13 */
14
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/iio/iio.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/property.h>
21
22 #define DS1803_WIPER_0 0xA9
23 #define DS1803_WIPER_1 0xAA
24 #define DS3502_WR_IVR 0x00
25
26 enum ds1803_type {
27 DS1803_010,
28 DS1803_050,
29 DS1803_100,
30 DS3502,
31 };
32
33 struct ds1803_cfg {
34 int wipers;
35 int avail[3];
36 int kohms;
37 const struct iio_chan_spec *channels;
38 u8 num_channels;
39 int (*read)(struct iio_dev *indio_dev,
40 struct iio_chan_spec const *chan, int *val);
41 };
42
43 struct ds1803_data {
44 struct i2c_client *client;
45 const struct ds1803_cfg *cfg;
46 };
47
48 #define DS1803_CHANNEL(ch, addr) { \
49 .type = IIO_RESISTANCE, \
50 .indexed = 1, \
51 .output = 1, \
52 .channel = (ch), \
53 .address = (addr), \
54 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
55 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
56 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_RAW), \
57 }
58
59 static const struct iio_chan_spec ds1803_channels[] = {
60 DS1803_CHANNEL(0, DS1803_WIPER_0),
61 DS1803_CHANNEL(1, DS1803_WIPER_1),
62 };
63
64 static const struct iio_chan_spec ds3502_channels[] = {
65 DS1803_CHANNEL(0, DS3502_WR_IVR),
66 };
67
ds1803_read(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val)68 static int ds1803_read(struct iio_dev *indio_dev,
69 struct iio_chan_spec const *chan,
70 int *val)
71 {
72 struct ds1803_data *data = iio_priv(indio_dev);
73 int ret;
74 u8 result[ARRAY_SIZE(ds1803_channels)];
75
76 ret = i2c_master_recv(data->client, result, indio_dev->num_channels);
77 if (ret < 0)
78 return ret;
79
80 *val = result[chan->channel];
81 return ret;
82 }
83
ds3502_read(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val)84 static int ds3502_read(struct iio_dev *indio_dev,
85 struct iio_chan_spec const *chan,
86 int *val)
87 {
88 struct ds1803_data *data = iio_priv(indio_dev);
89 int ret;
90
91 ret = i2c_smbus_read_byte_data(data->client, chan->address);
92 if (ret < 0)
93 return ret;
94
95 *val = ret;
96 return ret;
97 }
98
99 static const struct ds1803_cfg ds1803_cfg[] = {
100 [DS1803_010] = {
101 .wipers = 2,
102 .avail = { 0, 1, 255 },
103 .kohms = 10,
104 .channels = ds1803_channels,
105 .num_channels = ARRAY_SIZE(ds1803_channels),
106 .read = ds1803_read,
107 },
108 [DS1803_050] = {
109 .wipers = 2,
110 .avail = { 0, 1, 255 },
111 .kohms = 50,
112 .channels = ds1803_channels,
113 .num_channels = ARRAY_SIZE(ds1803_channels),
114 .read = ds1803_read,
115 },
116 [DS1803_100] = {
117 .wipers = 2,
118 .avail = { 0, 1, 255 },
119 .kohms = 100,
120 .channels = ds1803_channels,
121 .num_channels = ARRAY_SIZE(ds1803_channels),
122 .read = ds1803_read,
123 },
124 [DS3502] = {
125 .wipers = 1,
126 .avail = { 0, 1, 127 },
127 .kohms = 10,
128 .channels = ds3502_channels,
129 .num_channels = ARRAY_SIZE(ds3502_channels),
130 .read = ds3502_read,
131 },
132 };
133
ds1803_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)134 static int ds1803_read_raw(struct iio_dev *indio_dev,
135 struct iio_chan_spec const *chan,
136 int *val, int *val2, long mask)
137 {
138 struct ds1803_data *data = iio_priv(indio_dev);
139 int ret;
140
141 switch (mask) {
142 case IIO_CHAN_INFO_RAW:
143 ret = data->cfg->read(indio_dev, chan, val);
144 if (ret < 0)
145 return ret;
146
147 return IIO_VAL_INT;
148
149 case IIO_CHAN_INFO_SCALE:
150 *val = 1000 * data->cfg->kohms;
151 *val2 = data->cfg->avail[2]; /* Max wiper position */
152 return IIO_VAL_FRACTIONAL;
153 }
154
155 return -EINVAL;
156 }
157
ds1803_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)158 static int ds1803_write_raw(struct iio_dev *indio_dev,
159 struct iio_chan_spec const *chan,
160 int val, int val2, long mask)
161 {
162 struct ds1803_data *data = iio_priv(indio_dev);
163 u8 addr = chan->address;
164 int max_pos = data->cfg->avail[2];
165
166 if (val2 != 0)
167 return -EINVAL;
168
169 switch (mask) {
170 case IIO_CHAN_INFO_RAW:
171 if (val > max_pos || val < 0)
172 return -EINVAL;
173 break;
174 default:
175 return -EINVAL;
176 }
177
178 return i2c_smbus_write_byte_data(data->client, addr, val);
179 }
180
ds1803_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)181 static int ds1803_read_avail(struct iio_dev *indio_dev,
182 struct iio_chan_spec const *chan,
183 const int **vals, int *type,
184 int *length, long mask)
185 {
186 struct ds1803_data *data = iio_priv(indio_dev);
187
188 switch (mask) {
189 case IIO_CHAN_INFO_RAW:
190 *vals = data->cfg->avail;
191 *length = ARRAY_SIZE(data->cfg->avail);
192 *type = IIO_VAL_INT;
193 return IIO_AVAIL_RANGE;
194 }
195 return -EINVAL;
196 }
197
198 static const struct iio_info ds1803_info = {
199 .read_raw = ds1803_read_raw,
200 .write_raw = ds1803_write_raw,
201 .read_avail = ds1803_read_avail,
202 };
203
ds1803_probe(struct i2c_client * client)204 static int ds1803_probe(struct i2c_client *client)
205 {
206 struct device *dev = &client->dev;
207 struct ds1803_data *data;
208 struct iio_dev *indio_dev;
209
210 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
211 if (!indio_dev)
212 return -ENOMEM;
213
214 i2c_set_clientdata(client, indio_dev);
215
216 data = iio_priv(indio_dev);
217 data->client = client;
218 data->cfg = i2c_get_match_data(client);
219
220 indio_dev->info = &ds1803_info;
221 indio_dev->channels = data->cfg->channels;
222 indio_dev->num_channels = data->cfg->num_channels;
223 indio_dev->name = client->name;
224
225 return devm_iio_device_register(dev, indio_dev);
226 }
227
228 static const struct of_device_id ds1803_dt_ids[] = {
229 { .compatible = "maxim,ds1803-010", .data = &ds1803_cfg[DS1803_010] },
230 { .compatible = "maxim,ds1803-050", .data = &ds1803_cfg[DS1803_050] },
231 { .compatible = "maxim,ds1803-100", .data = &ds1803_cfg[DS1803_100] },
232 { .compatible = "maxim,ds3502", .data = &ds1803_cfg[DS3502] },
233 { }
234 };
235 MODULE_DEVICE_TABLE(of, ds1803_dt_ids);
236
237 static const struct i2c_device_id ds1803_id[] = {
238 { "ds1803-010", (kernel_ulong_t)&ds1803_cfg[DS1803_010] },
239 { "ds1803-050", (kernel_ulong_t)&ds1803_cfg[DS1803_050] },
240 { "ds1803-100", (kernel_ulong_t)&ds1803_cfg[DS1803_100] },
241 { "ds3502", (kernel_ulong_t)&ds1803_cfg[DS3502] },
242 { }
243 };
244 MODULE_DEVICE_TABLE(i2c, ds1803_id);
245
246 static struct i2c_driver ds1803_driver = {
247 .driver = {
248 .name = "ds1803",
249 .of_match_table = ds1803_dt_ids,
250 },
251 .probe = ds1803_probe,
252 .id_table = ds1803_id,
253 };
254
255 module_i2c_driver(ds1803_driver);
256
257 MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
258 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
259 MODULE_DESCRIPTION("DS1803 digital potentiometer");
260 MODULE_LICENSE("GPL v2");
261