1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fbbc5e70SSlawomir Stepien /*
373c16985SJagath Jog J * Maxim Integrated DS1803 and similar digital potentiometer driver
4fbbc5e70SSlawomir Stepien * Copyright (c) 2016 Slawomir Stepien
573c16985SJagath Jog J * Copyright (c) 2022 Jagath Jog J
6fbbc5e70SSlawomir Stepien *
7fbbc5e70SSlawomir Stepien * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf
873c16985SJagath Jog J * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3502.pdf
9fbbc5e70SSlawomir Stepien *
10fbbc5e70SSlawomir Stepien * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
11fbbc5e70SSlawomir Stepien * ds1803 2 256 10, 50, 100 0101xxx
1273c16985SJagath Jog J * ds3502 1 128 10 01010xx
13fbbc5e70SSlawomir Stepien */
14fbbc5e70SSlawomir Stepien
15fbbc5e70SSlawomir Stepien #include <linux/err.h>
16fbbc5e70SSlawomir Stepien #include <linux/export.h>
17fbbc5e70SSlawomir Stepien #include <linux/i2c.h>
18fbbc5e70SSlawomir Stepien #include <linux/iio/iio.h>
19fbbc5e70SSlawomir Stepien #include <linux/module.h>
20c34bed92SJonathan Cameron #include <linux/mod_devicetable.h>
216450b62cSJagath Jog J #include <linux/property.h>
22fbbc5e70SSlawomir Stepien
23a98d2b02SJagath Jog J #define DS1803_WIPER_0 0xA9
24a98d2b02SJagath Jog J #define DS1803_WIPER_1 0xAA
2573c16985SJagath Jog J #define DS3502_WR_IVR 0x00
26fbbc5e70SSlawomir Stepien
27fbbc5e70SSlawomir Stepien enum ds1803_type {
28fbbc5e70SSlawomir Stepien DS1803_010,
29fbbc5e70SSlawomir Stepien DS1803_050,
30fbbc5e70SSlawomir Stepien DS1803_100,
3173c16985SJagath Jog J DS3502,
32fbbc5e70SSlawomir Stepien };
33fbbc5e70SSlawomir Stepien
34fbbc5e70SSlawomir Stepien struct ds1803_cfg {
35a98d2b02SJagath Jog J int wipers;
364a68b326SJagath Jog J int avail[3];
37fbbc5e70SSlawomir Stepien int kohms;
38a98d2b02SJagath Jog J const struct iio_chan_spec *channels;
39a98d2b02SJagath Jog J u8 num_channels;
409b68725eSJagath Jog J int (*read)(struct iio_dev *indio_dev,
419b68725eSJagath Jog J struct iio_chan_spec const *chan, int *val);
42fbbc5e70SSlawomir Stepien };
43fbbc5e70SSlawomir Stepien
44fbbc5e70SSlawomir Stepien struct ds1803_data {
45fbbc5e70SSlawomir Stepien struct i2c_client *client;
46fbbc5e70SSlawomir Stepien const struct ds1803_cfg *cfg;
47fbbc5e70SSlawomir Stepien };
48fbbc5e70SSlawomir Stepien
49a98d2b02SJagath Jog J #define DS1803_CHANNEL(ch, addr) { \
50fbbc5e70SSlawomir Stepien .type = IIO_RESISTANCE, \
51fbbc5e70SSlawomir Stepien .indexed = 1, \
52fbbc5e70SSlawomir Stepien .output = 1, \
53fbbc5e70SSlawomir Stepien .channel = (ch), \
54a98d2b02SJagath Jog J .address = (addr), \
55fbbc5e70SSlawomir Stepien .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
56fbbc5e70SSlawomir Stepien .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
574a68b326SJagath Jog J .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_RAW), \
58fbbc5e70SSlawomir Stepien }
59fbbc5e70SSlawomir Stepien
60fbbc5e70SSlawomir Stepien static const struct iio_chan_spec ds1803_channels[] = {
61a98d2b02SJagath Jog J DS1803_CHANNEL(0, DS1803_WIPER_0),
62a98d2b02SJagath Jog J DS1803_CHANNEL(1, DS1803_WIPER_1),
63fbbc5e70SSlawomir Stepien };
64fbbc5e70SSlawomir Stepien
6573c16985SJagath Jog J static const struct iio_chan_spec ds3502_channels[] = {
6673c16985SJagath Jog J DS1803_CHANNEL(0, DS3502_WR_IVR),
6773c16985SJagath Jog J };
6873c16985SJagath Jog J
ds1803_read(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val)699b68725eSJagath Jog J static int ds1803_read(struct iio_dev *indio_dev,
709b68725eSJagath Jog J struct iio_chan_spec const *chan,
719b68725eSJagath Jog J int *val)
729b68725eSJagath Jog J {
739b68725eSJagath Jog J struct ds1803_data *data = iio_priv(indio_dev);
749b68725eSJagath Jog J int ret;
759b68725eSJagath Jog J u8 result[ARRAY_SIZE(ds1803_channels)];
769b68725eSJagath Jog J
779b68725eSJagath Jog J ret = i2c_master_recv(data->client, result, indio_dev->num_channels);
789b68725eSJagath Jog J if (ret < 0)
799b68725eSJagath Jog J return ret;
809b68725eSJagath Jog J
819b68725eSJagath Jog J *val = result[chan->channel];
829b68725eSJagath Jog J return ret;
839b68725eSJagath Jog J }
849b68725eSJagath Jog J
ds3502_read(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val)8573c16985SJagath Jog J static int ds3502_read(struct iio_dev *indio_dev,
8673c16985SJagath Jog J struct iio_chan_spec const *chan,
8773c16985SJagath Jog J int *val)
8873c16985SJagath Jog J {
8973c16985SJagath Jog J struct ds1803_data *data = iio_priv(indio_dev);
9073c16985SJagath Jog J int ret;
9173c16985SJagath Jog J
9273c16985SJagath Jog J ret = i2c_smbus_read_byte_data(data->client, chan->address);
9373c16985SJagath Jog J if (ret < 0)
9473c16985SJagath Jog J return ret;
9573c16985SJagath Jog J
9673c16985SJagath Jog J *val = ret;
9773c16985SJagath Jog J return ret;
9873c16985SJagath Jog J }
9973c16985SJagath Jog J
1004a68b326SJagath Jog J static const struct ds1803_cfg ds1803_cfg[] = {
1014a68b326SJagath Jog J [DS1803_010] = {
102a98d2b02SJagath Jog J .wipers = 2,
1034a68b326SJagath Jog J .avail = { 0, 1, 255 },
1044a68b326SJagath Jog J .kohms = 10,
105a98d2b02SJagath Jog J .channels = ds1803_channels,
106a98d2b02SJagath Jog J .num_channels = ARRAY_SIZE(ds1803_channels),
1079b68725eSJagath Jog J .read = ds1803_read,
1084a68b326SJagath Jog J },
1094a68b326SJagath Jog J [DS1803_050] = {
110a98d2b02SJagath Jog J .wipers = 2,
1114a68b326SJagath Jog J .avail = { 0, 1, 255 },
1124a68b326SJagath Jog J .kohms = 50,
113a98d2b02SJagath Jog J .channels = ds1803_channels,
114a98d2b02SJagath Jog J .num_channels = ARRAY_SIZE(ds1803_channels),
1159b68725eSJagath Jog J .read = ds1803_read,
1164a68b326SJagath Jog J },
1174a68b326SJagath Jog J [DS1803_100] = {
118a98d2b02SJagath Jog J .wipers = 2,
1194a68b326SJagath Jog J .avail = { 0, 1, 255 },
1204a68b326SJagath Jog J .kohms = 100,
121a98d2b02SJagath Jog J .channels = ds1803_channels,
122a98d2b02SJagath Jog J .num_channels = ARRAY_SIZE(ds1803_channels),
1239b68725eSJagath Jog J .read = ds1803_read,
1244a68b326SJagath Jog J },
12573c16985SJagath Jog J [DS3502] = {
12673c16985SJagath Jog J .wipers = 1,
12773c16985SJagath Jog J .avail = { 0, 1, 127 },
12873c16985SJagath Jog J .kohms = 10,
12973c16985SJagath Jog J .channels = ds3502_channels,
13073c16985SJagath Jog J .num_channels = ARRAY_SIZE(ds3502_channels),
13173c16985SJagath Jog J .read = ds3502_read,
13273c16985SJagath Jog J },
1334a68b326SJagath Jog J };
1344a68b326SJagath Jog J
ds1803_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)135fbbc5e70SSlawomir Stepien static int ds1803_read_raw(struct iio_dev *indio_dev,
136fbbc5e70SSlawomir Stepien struct iio_chan_spec const *chan,
137fbbc5e70SSlawomir Stepien int *val, int *val2, long mask)
138fbbc5e70SSlawomir Stepien {
139fbbc5e70SSlawomir Stepien struct ds1803_data *data = iio_priv(indio_dev);
140fbbc5e70SSlawomir Stepien int ret;
141fbbc5e70SSlawomir Stepien
142fbbc5e70SSlawomir Stepien switch (mask) {
143fbbc5e70SSlawomir Stepien case IIO_CHAN_INFO_RAW:
1449b68725eSJagath Jog J ret = data->cfg->read(indio_dev, chan, val);
145fbbc5e70SSlawomir Stepien if (ret < 0)
146fbbc5e70SSlawomir Stepien return ret;
147fbbc5e70SSlawomir Stepien
148fbbc5e70SSlawomir Stepien return IIO_VAL_INT;
149fbbc5e70SSlawomir Stepien
150fbbc5e70SSlawomir Stepien case IIO_CHAN_INFO_SCALE:
151fbbc5e70SSlawomir Stepien *val = 1000 * data->cfg->kohms;
1524a68b326SJagath Jog J *val2 = data->cfg->avail[2]; /* Max wiper position */
153fbbc5e70SSlawomir Stepien return IIO_VAL_FRACTIONAL;
154fbbc5e70SSlawomir Stepien }
155fbbc5e70SSlawomir Stepien
156fbbc5e70SSlawomir Stepien return -EINVAL;
157fbbc5e70SSlawomir Stepien }
158fbbc5e70SSlawomir Stepien
ds1803_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)159fbbc5e70SSlawomir Stepien static int ds1803_write_raw(struct iio_dev *indio_dev,
160fbbc5e70SSlawomir Stepien struct iio_chan_spec const *chan,
161fbbc5e70SSlawomir Stepien int val, int val2, long mask)
162fbbc5e70SSlawomir Stepien {
163fbbc5e70SSlawomir Stepien struct ds1803_data *data = iio_priv(indio_dev);
164a98d2b02SJagath Jog J u8 addr = chan->address;
1654a68b326SJagath Jog J int max_pos = data->cfg->avail[2];
166fbbc5e70SSlawomir Stepien
167fbbc5e70SSlawomir Stepien if (val2 != 0)
168fbbc5e70SSlawomir Stepien return -EINVAL;
169fbbc5e70SSlawomir Stepien
170fbbc5e70SSlawomir Stepien switch (mask) {
171fbbc5e70SSlawomir Stepien case IIO_CHAN_INFO_RAW:
1724a68b326SJagath Jog J if (val > max_pos || val < 0)
173fbbc5e70SSlawomir Stepien return -EINVAL;
174fbbc5e70SSlawomir Stepien break;
175fbbc5e70SSlawomir Stepien default:
176fbbc5e70SSlawomir Stepien return -EINVAL;
177fbbc5e70SSlawomir Stepien }
178fbbc5e70SSlawomir Stepien
179a98d2b02SJagath Jog J return i2c_smbus_write_byte_data(data->client, addr, val);
180fbbc5e70SSlawomir Stepien }
181fbbc5e70SSlawomir Stepien
ds1803_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)1824a68b326SJagath Jog J static int ds1803_read_avail(struct iio_dev *indio_dev,
1834a68b326SJagath Jog J struct iio_chan_spec const *chan,
1844a68b326SJagath Jog J const int **vals, int *type,
1854a68b326SJagath Jog J int *length, long mask)
1864a68b326SJagath Jog J {
1874a68b326SJagath Jog J struct ds1803_data *data = iio_priv(indio_dev);
1884a68b326SJagath Jog J
1894a68b326SJagath Jog J switch (mask) {
1904a68b326SJagath Jog J case IIO_CHAN_INFO_RAW:
1914a68b326SJagath Jog J *vals = data->cfg->avail;
1924a68b326SJagath Jog J *length = ARRAY_SIZE(data->cfg->avail);
1934a68b326SJagath Jog J *type = IIO_VAL_INT;
1944a68b326SJagath Jog J return IIO_AVAIL_RANGE;
1954a68b326SJagath Jog J }
1964a68b326SJagath Jog J return -EINVAL;
1974a68b326SJagath Jog J }
1984a68b326SJagath Jog J
199fbbc5e70SSlawomir Stepien static const struct iio_info ds1803_info = {
200fbbc5e70SSlawomir Stepien .read_raw = ds1803_read_raw,
201fbbc5e70SSlawomir Stepien .write_raw = ds1803_write_raw,
2024a68b326SJagath Jog J .read_avail = ds1803_read_avail,
203fbbc5e70SSlawomir Stepien };
204fbbc5e70SSlawomir Stepien
ds1803_probe(struct i2c_client * client)205705f1ce2SUwe Kleine-König static int ds1803_probe(struct i2c_client *client)
206fbbc5e70SSlawomir Stepien {
207fbbc5e70SSlawomir Stepien struct device *dev = &client->dev;
208fbbc5e70SSlawomir Stepien struct ds1803_data *data;
209fbbc5e70SSlawomir Stepien struct iio_dev *indio_dev;
210fbbc5e70SSlawomir Stepien
211fbbc5e70SSlawomir Stepien indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
212fbbc5e70SSlawomir Stepien if (!indio_dev)
213fbbc5e70SSlawomir Stepien return -ENOMEM;
214fbbc5e70SSlawomir Stepien
215fbbc5e70SSlawomir Stepien i2c_set_clientdata(client, indio_dev);
216fbbc5e70SSlawomir Stepien
217fbbc5e70SSlawomir Stepien data = iio_priv(indio_dev);
218fbbc5e70SSlawomir Stepien data->client = client;
219*0f5cecd1SBiju Das data->cfg = i2c_get_match_data(client);
220fbbc5e70SSlawomir Stepien
221fbbc5e70SSlawomir Stepien indio_dev->info = &ds1803_info;
222a98d2b02SJagath Jog J indio_dev->channels = data->cfg->channels;
223a98d2b02SJagath Jog J indio_dev->num_channels = data->cfg->num_channels;
224fbbc5e70SSlawomir Stepien indio_dev->name = client->name;
225fbbc5e70SSlawomir Stepien
226fbbc5e70SSlawomir Stepien return devm_iio_device_register(dev, indio_dev);
227fbbc5e70SSlawomir Stepien }
228fbbc5e70SSlawomir Stepien
229fbbc5e70SSlawomir Stepien static const struct of_device_id ds1803_dt_ids[] = {
230fbbc5e70SSlawomir Stepien { .compatible = "maxim,ds1803-010", .data = &ds1803_cfg[DS1803_010] },
231fbbc5e70SSlawomir Stepien { .compatible = "maxim,ds1803-050", .data = &ds1803_cfg[DS1803_050] },
232fbbc5e70SSlawomir Stepien { .compatible = "maxim,ds1803-100", .data = &ds1803_cfg[DS1803_100] },
23373c16985SJagath Jog J { .compatible = "maxim,ds3502", .data = &ds1803_cfg[DS3502] },
234fbbc5e70SSlawomir Stepien {}
235fbbc5e70SSlawomir Stepien };
236fbbc5e70SSlawomir Stepien MODULE_DEVICE_TABLE(of, ds1803_dt_ids);
237fbbc5e70SSlawomir Stepien
238fbbc5e70SSlawomir Stepien static const struct i2c_device_id ds1803_id[] = {
239*0f5cecd1SBiju Das { "ds1803-010", (kernel_ulong_t)&ds1803_cfg[DS1803_010] },
240*0f5cecd1SBiju Das { "ds1803-050", (kernel_ulong_t)&ds1803_cfg[DS1803_050] },
241*0f5cecd1SBiju Das { "ds1803-100", (kernel_ulong_t)&ds1803_cfg[DS1803_100] },
242*0f5cecd1SBiju Das { "ds3502", (kernel_ulong_t)&ds1803_cfg[DS3502] },
243fbbc5e70SSlawomir Stepien {}
244fbbc5e70SSlawomir Stepien };
245fbbc5e70SSlawomir Stepien MODULE_DEVICE_TABLE(i2c, ds1803_id);
246fbbc5e70SSlawomir Stepien
247fbbc5e70SSlawomir Stepien static struct i2c_driver ds1803_driver = {
248fbbc5e70SSlawomir Stepien .driver = {
249fbbc5e70SSlawomir Stepien .name = "ds1803",
250c34bed92SJonathan Cameron .of_match_table = ds1803_dt_ids,
251fbbc5e70SSlawomir Stepien },
2527cf15f42SUwe Kleine-König .probe = ds1803_probe,
253fbbc5e70SSlawomir Stepien .id_table = ds1803_id,
254fbbc5e70SSlawomir Stepien };
255fbbc5e70SSlawomir Stepien
256fbbc5e70SSlawomir Stepien module_i2c_driver(ds1803_driver);
257fbbc5e70SSlawomir Stepien
258fbbc5e70SSlawomir Stepien MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
25973c16985SJagath Jog J MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
260fbbc5e70SSlawomir Stepien MODULE_DESCRIPTION("DS1803 digital potentiometer");
261fbbc5e70SSlawomir Stepien MODULE_LICENSE("GPL v2");
262