xref: /linux/drivers/iio/potentiometer/ds1803.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2  * Maxim Integrated DS1803 digital potentiometer driver
3  * Copyright (c) 2016 Slawomir Stepien
4  *
5  * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf
6  *
7  * DEVID	#Wipers	#Positions	Resistor Opts (kOhm)	i2c address
8  * ds1803	2	256		10, 50, 100		0101xxx
9  *
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.
13  */
14 
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/of.h>
21 
22 #define DS1803_MAX_POS		255
23 #define DS1803_WRITE(chan)	(0xa8 | ((chan) + 1))
24 
25 enum ds1803_type {
26 	DS1803_010,
27 	DS1803_050,
28 	DS1803_100,
29 };
30 
31 struct ds1803_cfg {
32 	int kohms;
33 };
34 
35 static const struct ds1803_cfg ds1803_cfg[] = {
36 	[DS1803_010] = { .kohms =  10, },
37 	[DS1803_050] = { .kohms =  50, },
38 	[DS1803_100] = { .kohms = 100, },
39 };
40 
41 struct ds1803_data {
42 	struct i2c_client *client;
43 	const struct ds1803_cfg *cfg;
44 };
45 
46 #define DS1803_CHANNEL(ch) {					\
47 	.type = IIO_RESISTANCE,					\
48 	.indexed = 1,						\
49 	.output = 1,						\
50 	.channel = (ch),					\
51 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
52 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
53 }
54 
55 static const struct iio_chan_spec ds1803_channels[] = {
56 	DS1803_CHANNEL(0),
57 	DS1803_CHANNEL(1),
58 };
59 
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)
63 {
64 	struct ds1803_data *data = iio_priv(indio_dev);
65 	int pot = chan->channel;
66 	int ret;
67 	u8 result[ARRAY_SIZE(ds1803_channels)];
68 
69 	switch (mask) {
70 	case IIO_CHAN_INFO_RAW:
71 		ret = i2c_master_recv(data->client, result,
72 				indio_dev->num_channels);
73 		if (ret < 0)
74 			return ret;
75 
76 		*val = result[pot];
77 		return IIO_VAL_INT;
78 
79 	case IIO_CHAN_INFO_SCALE:
80 		*val = 1000 * data->cfg->kohms;
81 		*val2 = DS1803_MAX_POS;
82 		return IIO_VAL_FRACTIONAL;
83 	}
84 
85 	return -EINVAL;
86 }
87 
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)
91 {
92 	struct ds1803_data *data = iio_priv(indio_dev);
93 	int pot = chan->channel;
94 
95 	if (val2 != 0)
96 		return -EINVAL;
97 
98 	switch (mask) {
99 	case IIO_CHAN_INFO_RAW:
100 		if (val > DS1803_MAX_POS || val < 0)
101 			return -EINVAL;
102 		break;
103 	default:
104 		return -EINVAL;
105 	}
106 
107 	return i2c_smbus_write_byte_data(data->client, DS1803_WRITE(pot), val);
108 }
109 
110 static const struct iio_info ds1803_info = {
111 	.read_raw = ds1803_read_raw,
112 	.write_raw = ds1803_write_raw,
113 };
114 
115 static int ds1803_probe(struct i2c_client *client,
116 			 const struct i2c_device_id *id)
117 {
118 	struct device *dev = &client->dev;
119 	struct ds1803_data *data;
120 	struct iio_dev *indio_dev;
121 
122 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
123 	if (!indio_dev)
124 		return -ENOMEM;
125 
126 	i2c_set_clientdata(client, indio_dev);
127 
128 	data = iio_priv(indio_dev);
129 	data->client = client;
130 	data->cfg = &ds1803_cfg[id->driver_data];
131 
132 	indio_dev->dev.parent = dev;
133 	indio_dev->info = &ds1803_info;
134 	indio_dev->channels = ds1803_channels;
135 	indio_dev->num_channels = ARRAY_SIZE(ds1803_channels);
136 	indio_dev->name = client->name;
137 
138 	return devm_iio_device_register(dev, indio_dev);
139 }
140 
141 #if defined(CONFIG_OF)
142 static const struct of_device_id ds1803_dt_ids[] = {
143 	{ .compatible = "maxim,ds1803-010", .data = &ds1803_cfg[DS1803_010] },
144 	{ .compatible = "maxim,ds1803-050", .data = &ds1803_cfg[DS1803_050] },
145 	{ .compatible = "maxim,ds1803-100", .data = &ds1803_cfg[DS1803_100] },
146 	{}
147 };
148 MODULE_DEVICE_TABLE(of, ds1803_dt_ids);
149 #endif /* CONFIG_OF */
150 
151 static const struct i2c_device_id ds1803_id[] = {
152 	{ "ds1803-010", DS1803_010 },
153 	{ "ds1803-050", DS1803_050 },
154 	{ "ds1803-100", DS1803_100 },
155 	{}
156 };
157 MODULE_DEVICE_TABLE(i2c, ds1803_id);
158 
159 static struct i2c_driver ds1803_driver = {
160 	.driver = {
161 		.name	= "ds1803",
162 		.of_match_table = of_match_ptr(ds1803_dt_ids),
163 	},
164 	.probe		= ds1803_probe,
165 	.id_table	= ds1803_id,
166 };
167 
168 module_i2c_driver(ds1803_driver);
169 
170 MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
171 MODULE_DESCRIPTION("DS1803 digital potentiometer");
172 MODULE_LICENSE("GPL v2");
173