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