xref: /linux/drivers/iio/potentiometer/mcp4531.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Industrial I/O driver for Microchip digital potentiometers
4  * Copyright (c) 2015  Axentia Technologies AB
5  * Author: Peter Rosin <peda@axentia.se>
6  *
7  * Datasheet: http://www.microchip.com/downloads/en/DeviceDoc/22096b.pdf
8  *
9  * DEVID	#Wipers	#Positions	Resistor Opts (kOhm)	i2c address
10  * mcp4531	1	129		5, 10, 50, 100          010111x
11  * mcp4532	1	129		5, 10, 50, 100          01011xx
12  * mcp4541	1	129		5, 10, 50, 100          010111x
13  * mcp4542	1	129		5, 10, 50, 100          01011xx
14  * mcp4551	1	257		5, 10, 50, 100          010111x
15  * mcp4552	1	257		5, 10, 50, 100          01011xx
16  * mcp4561	1	257		5, 10, 50, 100          010111x
17  * mcp4562	1	257		5, 10, 50, 100          01011xx
18  * mcp4631	2	129		5, 10, 50, 100          0101xxx
19  * mcp4632	2	129		5, 10, 50, 100          01011xx
20  * mcp4641	2	129		5, 10, 50, 100          0101xxx
21  * mcp4642	2	129		5, 10, 50, 100          01011xx
22  * mcp4651	2	257		5, 10, 50, 100          0101xxx
23  * mcp4652	2	257		5, 10, 50, 100          01011xx
24  * mcp4661	2	257		5, 10, 50, 100          0101xxx
25  * mcp4662	2	257		5, 10, 50, 100          01011xx
26  */
27 
28 #include <linux/module.h>
29 #include <linux/i2c.h>
30 #include <linux/err.h>
31 #include <linux/property.h>
32 
33 #include <linux/iio/iio.h>
34 
35 struct mcp4531_cfg {
36 	int wipers;
37 	int avail[3];
38 	int kohms;
39 };
40 
41 enum mcp4531_type {
42 	MCP453x_502,
43 	MCP453x_103,
44 	MCP453x_503,
45 	MCP453x_104,
46 	MCP454x_502,
47 	MCP454x_103,
48 	MCP454x_503,
49 	MCP454x_104,
50 	MCP455x_502,
51 	MCP455x_103,
52 	MCP455x_503,
53 	MCP455x_104,
54 	MCP456x_502,
55 	MCP456x_103,
56 	MCP456x_503,
57 	MCP456x_104,
58 	MCP463x_502,
59 	MCP463x_103,
60 	MCP463x_503,
61 	MCP463x_104,
62 	MCP464x_502,
63 	MCP464x_103,
64 	MCP464x_503,
65 	MCP464x_104,
66 	MCP465x_502,
67 	MCP465x_103,
68 	MCP465x_503,
69 	MCP465x_104,
70 	MCP466x_502,
71 	MCP466x_103,
72 	MCP466x_503,
73 	MCP466x_104,
74 };
75 
76 static const struct mcp4531_cfg mcp4531_cfg[] = {
77 	[MCP453x_502] = { .wipers = 1, .avail = { 0, 1, 128 }, .kohms =   5, },
78 	[MCP453x_103] = { .wipers = 1, .avail = { 0, 1, 128 }, .kohms =  10, },
79 	[MCP453x_503] = { .wipers = 1, .avail = { 0, 1, 128 }, .kohms =  50, },
80 	[MCP453x_104] = { .wipers = 1, .avail = { 0, 1, 128 }, .kohms = 100, },
81 	[MCP454x_502] = { .wipers = 1, .avail = { 0, 1, 128 }, .kohms =   5, },
82 	[MCP454x_103] = { .wipers = 1, .avail = { 0, 1, 128 }, .kohms =  10, },
83 	[MCP454x_503] = { .wipers = 1, .avail = { 0, 1, 128 }, .kohms =  50, },
84 	[MCP454x_104] = { .wipers = 1, .avail = { 0, 1, 128 }, .kohms = 100, },
85 	[MCP455x_502] = { .wipers = 1, .avail = { 0, 1, 256 }, .kohms =   5, },
86 	[MCP455x_103] = { .wipers = 1, .avail = { 0, 1, 256 }, .kohms =  10, },
87 	[MCP455x_503] = { .wipers = 1, .avail = { 0, 1, 256 }, .kohms =  50, },
88 	[MCP455x_104] = { .wipers = 1, .avail = { 0, 1, 256 }, .kohms = 100, },
89 	[MCP456x_502] = { .wipers = 1, .avail = { 0, 1, 256 }, .kohms =   5, },
90 	[MCP456x_103] = { .wipers = 1, .avail = { 0, 1, 256 }, .kohms =  10, },
91 	[MCP456x_503] = { .wipers = 1, .avail = { 0, 1, 256 }, .kohms =  50, },
92 	[MCP456x_104] = { .wipers = 1, .avail = { 0, 1, 256 }, .kohms = 100, },
93 	[MCP463x_502] = { .wipers = 2, .avail = { 0, 1, 128 }, .kohms =   5, },
94 	[MCP463x_103] = { .wipers = 2, .avail = { 0, 1, 128 }, .kohms =  10, },
95 	[MCP463x_503] = { .wipers = 2, .avail = { 0, 1, 128 }, .kohms =  50, },
96 	[MCP463x_104] = { .wipers = 2, .avail = { 0, 1, 128 }, .kohms = 100, },
97 	[MCP464x_502] = { .wipers = 2, .avail = { 0, 1, 128 }, .kohms =   5, },
98 	[MCP464x_103] = { .wipers = 2, .avail = { 0, 1, 128 }, .kohms =  10, },
99 	[MCP464x_503] = { .wipers = 2, .avail = { 0, 1, 128 }, .kohms =  50, },
100 	[MCP464x_104] = { .wipers = 2, .avail = { 0, 1, 128 }, .kohms = 100, },
101 	[MCP465x_502] = { .wipers = 2, .avail = { 0, 1, 256 }, .kohms =   5, },
102 	[MCP465x_103] = { .wipers = 2, .avail = { 0, 1, 256 }, .kohms =  10, },
103 	[MCP465x_503] = { .wipers = 2, .avail = { 0, 1, 256 }, .kohms =  50, },
104 	[MCP465x_104] = { .wipers = 2, .avail = { 0, 1, 256 }, .kohms = 100, },
105 	[MCP466x_502] = { .wipers = 2, .avail = { 0, 1, 256 }, .kohms =   5, },
106 	[MCP466x_103] = { .wipers = 2, .avail = { 0, 1, 256 }, .kohms =  10, },
107 	[MCP466x_503] = { .wipers = 2, .avail = { 0, 1, 256 }, .kohms =  50, },
108 	[MCP466x_104] = { .wipers = 2, .avail = { 0, 1, 256 }, .kohms = 100, },
109 };
110 
111 #define MCP4531_WRITE (0 << 2)
112 #define MCP4531_INCR  (1 << 2)
113 #define MCP4531_DECR  (2 << 2)
114 #define MCP4531_READ  (3 << 2)
115 
116 #define MCP4531_WIPER_SHIFT (4)
117 
118 struct mcp4531_data {
119 	struct i2c_client *client;
120 	const struct mcp4531_cfg *cfg;
121 };
122 
123 #define MCP4531_CHANNEL(ch) {						\
124 	.type = IIO_RESISTANCE,						\
125 	.indexed = 1,							\
126 	.output = 1,							\
127 	.channel = (ch),						\
128 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
129 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
130 	.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_RAW),	\
131 }
132 
133 static const struct iio_chan_spec mcp4531_channels[] = {
134 	MCP4531_CHANNEL(0),
135 	MCP4531_CHANNEL(1),
136 };
137 
mcp4531_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)138 static int mcp4531_read_raw(struct iio_dev *indio_dev,
139 			    struct iio_chan_spec const *chan,
140 			    int *val, int *val2, long mask)
141 {
142 	struct mcp4531_data *data = iio_priv(indio_dev);
143 	int address = chan->channel << MCP4531_WIPER_SHIFT;
144 	s32 ret;
145 
146 	switch (mask) {
147 	case IIO_CHAN_INFO_RAW:
148 		ret = i2c_smbus_read_word_swapped(data->client,
149 						  MCP4531_READ | address);
150 		if (ret < 0)
151 			return ret;
152 		*val = ret;
153 		return IIO_VAL_INT;
154 	case IIO_CHAN_INFO_SCALE:
155 		*val = 1000 * data->cfg->kohms;
156 		*val2 = data->cfg->avail[2];
157 		return IIO_VAL_FRACTIONAL;
158 	}
159 
160 	return -EINVAL;
161 }
162 
mcp4531_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)163 static int mcp4531_read_avail(struct iio_dev *indio_dev,
164 			      struct iio_chan_spec const *chan,
165 			      const int **vals, int *type, int *length,
166 			      long mask)
167 {
168 	struct mcp4531_data *data = iio_priv(indio_dev);
169 
170 	switch (mask) {
171 	case IIO_CHAN_INFO_RAW:
172 		*length = ARRAY_SIZE(data->cfg->avail);
173 		*vals = data->cfg->avail;
174 		*type = IIO_VAL_INT;
175 		return IIO_AVAIL_RANGE;
176 	}
177 
178 	return -EINVAL;
179 }
180 
mcp4531_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)181 static int mcp4531_write_raw(struct iio_dev *indio_dev,
182 			     struct iio_chan_spec const *chan,
183 			     int val, int val2, long mask)
184 {
185 	struct mcp4531_data *data = iio_priv(indio_dev);
186 	int address = chan->channel << MCP4531_WIPER_SHIFT;
187 
188 	switch (mask) {
189 	case IIO_CHAN_INFO_RAW:
190 		if (val > data->cfg->avail[2] || val < 0)
191 			return -EINVAL;
192 		break;
193 	default:
194 		return -EINVAL;
195 	}
196 
197 	return i2c_smbus_write_byte_data(data->client,
198 					 MCP4531_WRITE | address | (val >> 8),
199 					 val & 0xff);
200 }
201 
202 static const struct iio_info mcp4531_info = {
203 	.read_raw = mcp4531_read_raw,
204 	.read_avail = mcp4531_read_avail,
205 	.write_raw = mcp4531_write_raw,
206 };
207 
208 #define MCP4531_ID_TABLE(_name, cfg) {				\
209 	.name = _name,						\
210 	.driver_data = (kernel_ulong_t)&mcp4531_cfg[cfg],	\
211 }
212 
213 static const struct i2c_device_id mcp4531_id[] = {
214 	MCP4531_ID_TABLE("mcp4531-502", MCP453x_502),
215 	MCP4531_ID_TABLE("mcp4531-103", MCP453x_103),
216 	MCP4531_ID_TABLE("mcp4531-503", MCP453x_503),
217 	MCP4531_ID_TABLE("mcp4531-104", MCP453x_104),
218 	MCP4531_ID_TABLE("mcp4532-502", MCP453x_502),
219 	MCP4531_ID_TABLE("mcp4532-103", MCP453x_103),
220 	MCP4531_ID_TABLE("mcp4532-503", MCP453x_503),
221 	MCP4531_ID_TABLE("mcp4532-104", MCP453x_104),
222 	MCP4531_ID_TABLE("mcp4541-502", MCP454x_502),
223 	MCP4531_ID_TABLE("mcp4541-103", MCP454x_103),
224 	MCP4531_ID_TABLE("mcp4541-503", MCP454x_503),
225 	MCP4531_ID_TABLE("mcp4541-104", MCP454x_104),
226 	MCP4531_ID_TABLE("mcp4542-502", MCP454x_502),
227 	MCP4531_ID_TABLE("mcp4542-103", MCP454x_103),
228 	MCP4531_ID_TABLE("mcp4542-503", MCP454x_503),
229 	MCP4531_ID_TABLE("mcp4542-104", MCP454x_104),
230 	MCP4531_ID_TABLE("mcp4551-502", MCP455x_502),
231 	MCP4531_ID_TABLE("mcp4551-103", MCP455x_103),
232 	MCP4531_ID_TABLE("mcp4551-503", MCP455x_503),
233 	MCP4531_ID_TABLE("mcp4551-104", MCP455x_104),
234 	MCP4531_ID_TABLE("mcp4552-502", MCP455x_502),
235 	MCP4531_ID_TABLE("mcp4552-103", MCP455x_103),
236 	MCP4531_ID_TABLE("mcp4552-503", MCP455x_503),
237 	MCP4531_ID_TABLE("mcp4552-104", MCP455x_104),
238 	MCP4531_ID_TABLE("mcp4561-502", MCP456x_502),
239 	MCP4531_ID_TABLE("mcp4561-103", MCP456x_103),
240 	MCP4531_ID_TABLE("mcp4561-503", MCP456x_503),
241 	MCP4531_ID_TABLE("mcp4561-104", MCP456x_104),
242 	MCP4531_ID_TABLE("mcp4562-502", MCP456x_502),
243 	MCP4531_ID_TABLE("mcp4562-103", MCP456x_103),
244 	MCP4531_ID_TABLE("mcp4562-503", MCP456x_503),
245 	MCP4531_ID_TABLE("mcp4562-104", MCP456x_104),
246 	MCP4531_ID_TABLE("mcp4631-502", MCP463x_502),
247 	MCP4531_ID_TABLE("mcp4631-103", MCP463x_103),
248 	MCP4531_ID_TABLE("mcp4631-503", MCP463x_503),
249 	MCP4531_ID_TABLE("mcp4631-104", MCP463x_104),
250 	MCP4531_ID_TABLE("mcp4632-502", MCP463x_502),
251 	MCP4531_ID_TABLE("mcp4632-103", MCP463x_103),
252 	MCP4531_ID_TABLE("mcp4632-503", MCP463x_503),
253 	MCP4531_ID_TABLE("mcp4632-104", MCP463x_104),
254 	MCP4531_ID_TABLE("mcp4641-502", MCP464x_502),
255 	MCP4531_ID_TABLE("mcp4641-103", MCP464x_103),
256 	MCP4531_ID_TABLE("mcp4641-503", MCP464x_503),
257 	MCP4531_ID_TABLE("mcp4641-104", MCP464x_104),
258 	MCP4531_ID_TABLE("mcp4642-502", MCP464x_502),
259 	MCP4531_ID_TABLE("mcp4642-103", MCP464x_103),
260 	MCP4531_ID_TABLE("mcp4642-503", MCP464x_503),
261 	MCP4531_ID_TABLE("mcp4642-104", MCP464x_104),
262 	MCP4531_ID_TABLE("mcp4651-502", MCP465x_502),
263 	MCP4531_ID_TABLE("mcp4651-103", MCP465x_103),
264 	MCP4531_ID_TABLE("mcp4651-503", MCP465x_503),
265 	MCP4531_ID_TABLE("mcp4651-104", MCP465x_104),
266 	MCP4531_ID_TABLE("mcp4652-502", MCP465x_502),
267 	MCP4531_ID_TABLE("mcp4652-103", MCP465x_103),
268 	MCP4531_ID_TABLE("mcp4652-503", MCP465x_503),
269 	MCP4531_ID_TABLE("mcp4652-104", MCP465x_104),
270 	MCP4531_ID_TABLE("mcp4661-502", MCP466x_502),
271 	MCP4531_ID_TABLE("mcp4661-103", MCP466x_103),
272 	MCP4531_ID_TABLE("mcp4661-503", MCP466x_503),
273 	MCP4531_ID_TABLE("mcp4661-104", MCP466x_104),
274 	MCP4531_ID_TABLE("mcp4662-502", MCP466x_502),
275 	MCP4531_ID_TABLE("mcp4662-103", MCP466x_103),
276 	MCP4531_ID_TABLE("mcp4662-503", MCP466x_503),
277 	MCP4531_ID_TABLE("mcp4662-104", MCP466x_104),
278 	{ }
279 };
280 MODULE_DEVICE_TABLE(i2c, mcp4531_id);
281 
282 #define MCP4531_COMPATIBLE(of_compatible, cfg) {	\
283 			.compatible = of_compatible,	\
284 			.data = &mcp4531_cfg[cfg],	\
285 }
286 
287 static const struct of_device_id mcp4531_of_match[] = {
288 	MCP4531_COMPATIBLE("microchip,mcp4531-502", MCP453x_502),
289 	MCP4531_COMPATIBLE("microchip,mcp4531-103", MCP453x_103),
290 	MCP4531_COMPATIBLE("microchip,mcp4531-503", MCP453x_503),
291 	MCP4531_COMPATIBLE("microchip,mcp4531-104", MCP453x_104),
292 	MCP4531_COMPATIBLE("microchip,mcp4532-502", MCP453x_502),
293 	MCP4531_COMPATIBLE("microchip,mcp4532-103", MCP453x_103),
294 	MCP4531_COMPATIBLE("microchip,mcp4532-503", MCP453x_503),
295 	MCP4531_COMPATIBLE("microchip,mcp4532-104", MCP453x_104),
296 	MCP4531_COMPATIBLE("microchip,mcp4541-502", MCP454x_502),
297 	MCP4531_COMPATIBLE("microchip,mcp4541-103", MCP454x_103),
298 	MCP4531_COMPATIBLE("microchip,mcp4541-503", MCP454x_503),
299 	MCP4531_COMPATIBLE("microchip,mcp4541-104", MCP454x_104),
300 	MCP4531_COMPATIBLE("microchip,mcp4542-502", MCP454x_502),
301 	MCP4531_COMPATIBLE("microchip,mcp4542-103", MCP454x_103),
302 	MCP4531_COMPATIBLE("microchip,mcp4542-503", MCP454x_503),
303 	MCP4531_COMPATIBLE("microchip,mcp4542-104", MCP454x_104),
304 	MCP4531_COMPATIBLE("microchip,mcp4551-502", MCP455x_502),
305 	MCP4531_COMPATIBLE("microchip,mcp4551-103", MCP455x_103),
306 	MCP4531_COMPATIBLE("microchip,mcp4551-503", MCP455x_503),
307 	MCP4531_COMPATIBLE("microchip,mcp4551-104", MCP455x_104),
308 	MCP4531_COMPATIBLE("microchip,mcp4552-502", MCP455x_502),
309 	MCP4531_COMPATIBLE("microchip,mcp4552-103", MCP455x_103),
310 	MCP4531_COMPATIBLE("microchip,mcp4552-503", MCP455x_503),
311 	MCP4531_COMPATIBLE("microchip,mcp4552-104", MCP455x_104),
312 	MCP4531_COMPATIBLE("microchip,mcp4561-502", MCP456x_502),
313 	MCP4531_COMPATIBLE("microchip,mcp4561-103", MCP456x_103),
314 	MCP4531_COMPATIBLE("microchip,mcp4561-503", MCP456x_503),
315 	MCP4531_COMPATIBLE("microchip,mcp4561-104", MCP456x_104),
316 	MCP4531_COMPATIBLE("microchip,mcp4562-502", MCP456x_502),
317 	MCP4531_COMPATIBLE("microchip,mcp4562-103", MCP456x_103),
318 	MCP4531_COMPATIBLE("microchip,mcp4562-503", MCP456x_503),
319 	MCP4531_COMPATIBLE("microchip,mcp4562-104", MCP456x_104),
320 	MCP4531_COMPATIBLE("microchip,mcp4631-502", MCP463x_502),
321 	MCP4531_COMPATIBLE("microchip,mcp4631-103", MCP463x_103),
322 	MCP4531_COMPATIBLE("microchip,mcp4631-503", MCP463x_503),
323 	MCP4531_COMPATIBLE("microchip,mcp4631-104", MCP463x_104),
324 	MCP4531_COMPATIBLE("microchip,mcp4632-502", MCP463x_502),
325 	MCP4531_COMPATIBLE("microchip,mcp4632-103", MCP463x_103),
326 	MCP4531_COMPATIBLE("microchip,mcp4632-503", MCP463x_503),
327 	MCP4531_COMPATIBLE("microchip,mcp4632-104", MCP463x_104),
328 	MCP4531_COMPATIBLE("microchip,mcp4641-502", MCP464x_502),
329 	MCP4531_COMPATIBLE("microchip,mcp4641-103", MCP464x_103),
330 	MCP4531_COMPATIBLE("microchip,mcp4641-503", MCP464x_503),
331 	MCP4531_COMPATIBLE("microchip,mcp4641-104", MCP464x_104),
332 	MCP4531_COMPATIBLE("microchip,mcp4642-502", MCP464x_502),
333 	MCP4531_COMPATIBLE("microchip,mcp4642-103", MCP464x_103),
334 	MCP4531_COMPATIBLE("microchip,mcp4642-503", MCP464x_503),
335 	MCP4531_COMPATIBLE("microchip,mcp4642-104", MCP464x_104),
336 	MCP4531_COMPATIBLE("microchip,mcp4651-502", MCP465x_502),
337 	MCP4531_COMPATIBLE("microchip,mcp4651-103", MCP465x_103),
338 	MCP4531_COMPATIBLE("microchip,mcp4651-503", MCP465x_503),
339 	MCP4531_COMPATIBLE("microchip,mcp4651-104", MCP465x_104),
340 	MCP4531_COMPATIBLE("microchip,mcp4652-502", MCP465x_502),
341 	MCP4531_COMPATIBLE("microchip,mcp4652-103", MCP465x_103),
342 	MCP4531_COMPATIBLE("microchip,mcp4652-503", MCP465x_503),
343 	MCP4531_COMPATIBLE("microchip,mcp4652-104", MCP465x_104),
344 	MCP4531_COMPATIBLE("microchip,mcp4661-502", MCP466x_502),
345 	MCP4531_COMPATIBLE("microchip,mcp4661-103", MCP466x_103),
346 	MCP4531_COMPATIBLE("microchip,mcp4661-503", MCP466x_503),
347 	MCP4531_COMPATIBLE("microchip,mcp4661-104", MCP466x_104),
348 	MCP4531_COMPATIBLE("microchip,mcp4662-502", MCP466x_502),
349 	MCP4531_COMPATIBLE("microchip,mcp4662-103", MCP466x_103),
350 	MCP4531_COMPATIBLE("microchip,mcp4662-503", MCP466x_503),
351 	MCP4531_COMPATIBLE("microchip,mcp4662-104", MCP466x_104),
352 	{ }
353 };
354 MODULE_DEVICE_TABLE(of, mcp4531_of_match);
355 
mcp4531_probe(struct i2c_client * client)356 static int mcp4531_probe(struct i2c_client *client)
357 {
358 	struct device *dev = &client->dev;
359 	struct mcp4531_data *data;
360 	struct iio_dev *indio_dev;
361 
362 	if (!i2c_check_functionality(client->adapter,
363 				     I2C_FUNC_SMBUS_WORD_DATA)) {
364 		dev_err(dev, "SMBUS Word Data not supported\n");
365 		return -EOPNOTSUPP;
366 	}
367 
368 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
369 	if (!indio_dev)
370 		return -ENOMEM;
371 	data = iio_priv(indio_dev);
372 	i2c_set_clientdata(client, indio_dev);
373 	data->client = client;
374 
375 	data->cfg = i2c_get_match_data(client);
376 
377 	indio_dev->info = &mcp4531_info;
378 	indio_dev->channels = mcp4531_channels;
379 	indio_dev->num_channels = data->cfg->wipers;
380 	indio_dev->name = client->name;
381 
382 	return devm_iio_device_register(dev, indio_dev);
383 }
384 
385 static struct i2c_driver mcp4531_driver = {
386 	.driver = {
387 		.name	= "mcp4531",
388 		.of_match_table = mcp4531_of_match,
389 	},
390 	.probe		= mcp4531_probe,
391 	.id_table	= mcp4531_id,
392 };
393 
394 module_i2c_driver(mcp4531_driver);
395 
396 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
397 MODULE_DESCRIPTION("MCP4531 digital potentiometer");
398 MODULE_LICENSE("GPL v2");
399