2 * Maxim Integrated DS1803 digital potentiometer driver
3 * Copyright (c) 2016 Slawomir Stepien
5 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf
7 * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
8 * ds1803 2 256 10, 50, 100 0101xxx
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
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>
22 #define DS1803_MAX_POS 255
23 #define DS1803_WRITE(chan) (0xa8 | ((chan) + 1))
35 static const struct ds1803_cfg ds1803_cfg
[] = {
36 [DS1803_010
] = { .kohms
= 10, },
37 [DS1803_050
] = { .kohms
= 50, },
38 [DS1803_100
] = { .kohms
= 100, },
42 struct i2c_client
*client
;
43 const struct ds1803_cfg
*cfg
;
46 #define DS1803_CHANNEL(ch) { \
47 .type = IIO_RESISTANCE, \
51 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
52 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
55 static const struct iio_chan_spec ds1803_channels
[] = {
60 static int ds1803_read_raw(struct iio_dev
*indio_dev
,
61 struct iio_chan_spec
const *chan
,
62 int *val
, int *val2
, long mask
)
64 struct ds1803_data
*data
= iio_priv(indio_dev
);
65 int pot
= chan
->channel
;
67 u8 result
[indio_dev
->num_channels
];
70 case IIO_CHAN_INFO_RAW
:
71 ret
= i2c_master_recv(data
->client
, result
,
72 indio_dev
->num_channels
);
79 case IIO_CHAN_INFO_SCALE
:
80 *val
= 1000 * data
->cfg
->kohms
;
81 *val2
= DS1803_MAX_POS
;
82 return IIO_VAL_FRACTIONAL
;
88 static int ds1803_write_raw(struct iio_dev
*indio_dev
,
89 struct iio_chan_spec
const *chan
,
90 int val
, int val2
, long mask
)
92 struct ds1803_data
*data
= iio_priv(indio_dev
);
93 int pot
= chan
->channel
;
99 case IIO_CHAN_INFO_RAW
:
100 if (val
> DS1803_MAX_POS
|| val
< 0)
107 return i2c_smbus_write_byte_data(data
->client
, DS1803_WRITE(pot
), val
);
110 static const struct iio_info ds1803_info
= {
111 .read_raw
= ds1803_read_raw
,
112 .write_raw
= ds1803_write_raw
,
113 .driver_module
= THIS_MODULE
,
116 static int ds1803_probe(struct i2c_client
*client
,
117 const struct i2c_device_id
*id
)
119 struct device
*dev
= &client
->dev
;
120 struct ds1803_data
*data
;
121 struct iio_dev
*indio_dev
;
123 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
127 i2c_set_clientdata(client
, indio_dev
);
129 data
= iio_priv(indio_dev
);
130 data
->client
= client
;
131 data
->cfg
= &ds1803_cfg
[id
->driver_data
];
133 indio_dev
->dev
.parent
= dev
;
134 indio_dev
->info
= &ds1803_info
;
135 indio_dev
->channels
= ds1803_channels
;
136 indio_dev
->num_channels
= ARRAY_SIZE(ds1803_channels
);
137 indio_dev
->name
= client
->name
;
139 return devm_iio_device_register(dev
, indio_dev
);
142 #if defined(CONFIG_OF)
143 static const struct of_device_id ds1803_dt_ids
[] = {
144 { .compatible
= "maxim,ds1803-010", .data
= &ds1803_cfg
[DS1803_010
] },
145 { .compatible
= "maxim,ds1803-050", .data
= &ds1803_cfg
[DS1803_050
] },
146 { .compatible
= "maxim,ds1803-100", .data
= &ds1803_cfg
[DS1803_100
] },
149 MODULE_DEVICE_TABLE(of
, ds1803_dt_ids
);
150 #endif /* CONFIG_OF */
152 static const struct i2c_device_id ds1803_id
[] = {
153 { "ds1803-010", DS1803_010
},
154 { "ds1803-050", DS1803_050
},
155 { "ds1803-100", DS1803_100
},
158 MODULE_DEVICE_TABLE(i2c
, ds1803_id
);
160 static struct i2c_driver ds1803_driver
= {
163 .of_match_table
= of_match_ptr(ds1803_dt_ids
),
165 .probe
= ds1803_probe
,
166 .id_table
= ds1803_id
,
169 module_i2c_driver(ds1803_driver
);
171 MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
172 MODULE_DESCRIPTION("DS1803 digital potentiometer");
173 MODULE_LICENSE("GPL v2");