1 // SPDX-License-Identifier: GPL-2.0-only
3 * Maxim Integrated DS1803 and similar digital potentiometer driver
4 * Copyright (c) 2016 Slawomir Stepien
5 * Copyright (c) 2022 Jagath Jog J
7 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf
8 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3502.pdf
10 * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
11 * ds1803 2 256 10, 50, 100 0101xxx
12 * ds3502 1 128 10 01010xx
15 #include <linux/err.h>
16 #include <linux/export.h>
17 #include <linux/i2c.h>
18 #include <linux/iio/iio.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/property.h>
23 #define DS1803_WIPER_0 0xA9
24 #define DS1803_WIPER_1 0xAA
25 #define DS3502_WR_IVR 0x00
38 const struct iio_chan_spec
*channels
;
40 int (*read
)(struct iio_dev
*indio_dev
,
41 struct iio_chan_spec
const *chan
, int *val
);
45 struct i2c_client
*client
;
46 const struct ds1803_cfg
*cfg
;
49 #define DS1803_CHANNEL(ch, addr) { \
50 .type = IIO_RESISTANCE, \
55 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
56 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
57 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_RAW), \
60 static const struct iio_chan_spec ds1803_channels
[] = {
61 DS1803_CHANNEL(0, DS1803_WIPER_0
),
62 DS1803_CHANNEL(1, DS1803_WIPER_1
),
65 static const struct iio_chan_spec ds3502_channels
[] = {
66 DS1803_CHANNEL(0, DS3502_WR_IVR
),
69 static int ds1803_read(struct iio_dev
*indio_dev
,
70 struct iio_chan_spec
const *chan
,
73 struct ds1803_data
*data
= iio_priv(indio_dev
);
75 u8 result
[ARRAY_SIZE(ds1803_channels
)];
77 ret
= i2c_master_recv(data
->client
, result
, indio_dev
->num_channels
);
81 *val
= result
[chan
->channel
];
85 static int ds3502_read(struct iio_dev
*indio_dev
,
86 struct iio_chan_spec
const *chan
,
89 struct ds1803_data
*data
= iio_priv(indio_dev
);
92 ret
= i2c_smbus_read_byte_data(data
->client
, chan
->address
);
100 static const struct ds1803_cfg ds1803_cfg
[] = {
103 .avail
= { 0, 1, 255 },
105 .channels
= ds1803_channels
,
106 .num_channels
= ARRAY_SIZE(ds1803_channels
),
111 .avail
= { 0, 1, 255 },
113 .channels
= ds1803_channels
,
114 .num_channels
= ARRAY_SIZE(ds1803_channels
),
119 .avail
= { 0, 1, 255 },
121 .channels
= ds1803_channels
,
122 .num_channels
= ARRAY_SIZE(ds1803_channels
),
127 .avail
= { 0, 1, 127 },
129 .channels
= ds3502_channels
,
130 .num_channels
= ARRAY_SIZE(ds3502_channels
),
135 static int ds1803_read_raw(struct iio_dev
*indio_dev
,
136 struct iio_chan_spec
const *chan
,
137 int *val
, int *val2
, long mask
)
139 struct ds1803_data
*data
= iio_priv(indio_dev
);
143 case IIO_CHAN_INFO_RAW
:
144 ret
= data
->cfg
->read(indio_dev
, chan
, val
);
150 case IIO_CHAN_INFO_SCALE
:
151 *val
= 1000 * data
->cfg
->kohms
;
152 *val2
= data
->cfg
->avail
[2]; /* Max wiper position */
153 return IIO_VAL_FRACTIONAL
;
159 static int ds1803_write_raw(struct iio_dev
*indio_dev
,
160 struct iio_chan_spec
const *chan
,
161 int val
, int val2
, long mask
)
163 struct ds1803_data
*data
= iio_priv(indio_dev
);
164 u8 addr
= chan
->address
;
165 int max_pos
= data
->cfg
->avail
[2];
171 case IIO_CHAN_INFO_RAW
:
172 if (val
> max_pos
|| val
< 0)
179 return i2c_smbus_write_byte_data(data
->client
, addr
, val
);
182 static int ds1803_read_avail(struct iio_dev
*indio_dev
,
183 struct iio_chan_spec
const *chan
,
184 const int **vals
, int *type
,
185 int *length
, long mask
)
187 struct ds1803_data
*data
= iio_priv(indio_dev
);
190 case IIO_CHAN_INFO_RAW
:
191 *vals
= data
->cfg
->avail
;
192 *length
= ARRAY_SIZE(data
->cfg
->avail
);
194 return IIO_AVAIL_RANGE
;
199 static const struct iio_info ds1803_info
= {
200 .read_raw
= ds1803_read_raw
,
201 .write_raw
= ds1803_write_raw
,
202 .read_avail
= ds1803_read_avail
,
205 static int ds1803_probe(struct i2c_client
*client
)
207 struct device
*dev
= &client
->dev
;
208 struct ds1803_data
*data
;
209 struct iio_dev
*indio_dev
;
211 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
215 i2c_set_clientdata(client
, indio_dev
);
217 data
= iio_priv(indio_dev
);
218 data
->client
= client
;
219 data
->cfg
= i2c_get_match_data(client
);
221 indio_dev
->info
= &ds1803_info
;
222 indio_dev
->channels
= data
->cfg
->channels
;
223 indio_dev
->num_channels
= data
->cfg
->num_channels
;
224 indio_dev
->name
= client
->name
;
226 return devm_iio_device_register(dev
, indio_dev
);
229 static const struct of_device_id ds1803_dt_ids
[] = {
230 { .compatible
= "maxim,ds1803-010", .data
= &ds1803_cfg
[DS1803_010
] },
231 { .compatible
= "maxim,ds1803-050", .data
= &ds1803_cfg
[DS1803_050
] },
232 { .compatible
= "maxim,ds1803-100", .data
= &ds1803_cfg
[DS1803_100
] },
233 { .compatible
= "maxim,ds3502", .data
= &ds1803_cfg
[DS3502
] },
236 MODULE_DEVICE_TABLE(of
, ds1803_dt_ids
);
238 static const struct i2c_device_id ds1803_id
[] = {
239 { "ds1803-010", (kernel_ulong_t
)&ds1803_cfg
[DS1803_010
] },
240 { "ds1803-050", (kernel_ulong_t
)&ds1803_cfg
[DS1803_050
] },
241 { "ds1803-100", (kernel_ulong_t
)&ds1803_cfg
[DS1803_100
] },
242 { "ds3502", (kernel_ulong_t
)&ds1803_cfg
[DS3502
] },
245 MODULE_DEVICE_TABLE(i2c
, ds1803_id
);
247 static struct i2c_driver ds1803_driver
= {
250 .of_match_table
= ds1803_dt_ids
,
252 .probe
= ds1803_probe
,
253 .id_table
= ds1803_id
,
256 module_i2c_driver(ds1803_driver
);
258 MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
259 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
260 MODULE_DESCRIPTION("DS1803 digital potentiometer");
261 MODULE_LICENSE("GPL v2");