GNU Linux-libre 4.4.292-gnu1
[releases.git] / drivers / iio / adc / mcp3422.c
1 /*
2  * mcp3422.c - driver for the Microchip mcp3422/3/4/6/7/8 chip family
3  *
4  * Copyright (C) 2013, Angelo Compagnucci
5  * Author: Angelo Compagnucci <angelo.compagnucci@gmail.com>
6  *
7  * Datasheet: http://ww1.microchip.com/downloads/en/devicedoc/22088b.pdf
8  *            http://ww1.microchip.com/downloads/en/DeviceDoc/22226a.pdf
9  *
10  * This driver exports the value of analog input voltage to sysfs, the
11  * voltage unit is nV.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  */
18
19 #include <linux/err.h>
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/sysfs.h>
24 #include <linux/of.h>
25
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28
29 /* Masks */
30 #define MCP3422_CHANNEL_MASK    0x60
31 #define MCP3422_PGA_MASK        0x03
32 #define MCP3422_SRATE_MASK      0x0C
33 #define MCP3422_SRATE_240       0x0
34 #define MCP3422_SRATE_60        0x1
35 #define MCP3422_SRATE_15        0x2
36 #define MCP3422_SRATE_3 0x3
37 #define MCP3422_PGA_1   0
38 #define MCP3422_PGA_2   1
39 #define MCP3422_PGA_4   2
40 #define MCP3422_PGA_8   3
41 #define MCP3422_CONT_SAMPLING   0x10
42
43 #define MCP3422_CHANNEL(config) (((config) & MCP3422_CHANNEL_MASK) >> 5)
44 #define MCP3422_PGA(config)     ((config) & MCP3422_PGA_MASK)
45 #define MCP3422_SAMPLE_RATE(config)     (((config) & MCP3422_SRATE_MASK) >> 2)
46
47 #define MCP3422_CHANNEL_VALUE(value) (((value) << 5) & MCP3422_CHANNEL_MASK)
48 #define MCP3422_PGA_VALUE(value) ((value) & MCP3422_PGA_MASK)
49 #define MCP3422_SAMPLE_RATE_VALUE(value) ((value << 2) & MCP3422_SRATE_MASK)
50
51 #define MCP3422_CHAN(_index) \
52         { \
53                 .type = IIO_VOLTAGE, \
54                 .indexed = 1, \
55                 .channel = _index, \
56                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
57                                 | BIT(IIO_CHAN_INFO_SCALE), \
58                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
59         }
60
61 static const int mcp3422_scales[4][4] = {
62         { 1000000, 500000, 250000, 125000 },
63         { 250000 , 125000, 62500 , 31250  },
64         { 62500  , 31250 , 15625 , 7812   },
65         { 15625  , 7812  , 3906  , 1953   } };
66
67 /* Constant msleep times for data acquisitions */
68 static const int mcp3422_read_times[4] = {
69         [MCP3422_SRATE_240] = 1000 / 240,
70         [MCP3422_SRATE_60] = 1000 / 60,
71         [MCP3422_SRATE_15] = 1000 / 15,
72         [MCP3422_SRATE_3] = 1000 / 3 };
73
74 /* sample rates to integer conversion table */
75 static const int mcp3422_sample_rates[4] = {
76         [MCP3422_SRATE_240] = 240,
77         [MCP3422_SRATE_60] = 60,
78         [MCP3422_SRATE_15] = 15,
79         [MCP3422_SRATE_3] = 3 };
80
81 /* sample rates to sign extension table */
82 static const int mcp3422_sign_extend[4] = {
83         [MCP3422_SRATE_240] = 11,
84         [MCP3422_SRATE_60] = 13,
85         [MCP3422_SRATE_15] = 15,
86         [MCP3422_SRATE_3] = 17 };
87
88 /* Client data (each client gets its own) */
89 struct mcp3422 {
90         struct i2c_client *i2c;
91         u8 id;
92         u8 config;
93         u8 pga[4];
94         struct mutex lock;
95 };
96
97 static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
98 {
99         int ret;
100
101         ret = i2c_master_send(adc->i2c, &newconfig, 1);
102         if (ret > 0) {
103                 adc->config = newconfig;
104                 ret = 0;
105         }
106
107         return ret;
108 }
109
110 static int mcp3422_read(struct mcp3422 *adc, int *value, u8 *config)
111 {
112         int ret = 0;
113         u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
114         u8 buf[4] = {0, 0, 0, 0};
115         u32 temp;
116
117         if (sample_rate == MCP3422_SRATE_3) {
118                 ret = i2c_master_recv(adc->i2c, buf, 4);
119                 temp = buf[0] << 16 | buf[1] << 8 | buf[2];
120                 *config = buf[3];
121         } else {
122                 ret = i2c_master_recv(adc->i2c, buf, 3);
123                 temp = buf[0] << 8 | buf[1];
124                 *config = buf[2];
125         }
126
127         *value = sign_extend32(temp, mcp3422_sign_extend[sample_rate]);
128
129         return ret;
130 }
131
132 static int mcp3422_read_channel(struct mcp3422 *adc,
133                                 struct iio_chan_spec const *channel, int *value)
134 {
135         int ret;
136         u8 config;
137         u8 req_channel = channel->channel;
138
139         mutex_lock(&adc->lock);
140
141         if (req_channel != MCP3422_CHANNEL(adc->config)) {
142                 config = adc->config;
143                 config &= ~MCP3422_CHANNEL_MASK;
144                 config |= MCP3422_CHANNEL_VALUE(req_channel);
145                 config &= ~MCP3422_PGA_MASK;
146                 config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
147                 ret = mcp3422_update_config(adc, config);
148                 if (ret < 0) {
149                         mutex_unlock(&adc->lock);
150                         return ret;
151                 }
152                 msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
153         }
154
155         ret = mcp3422_read(adc, value, &config);
156
157         mutex_unlock(&adc->lock);
158
159         return ret;
160 }
161
162 static int mcp3422_read_raw(struct iio_dev *iio,
163                         struct iio_chan_spec const *channel, int *val1,
164                         int *val2, long mask)
165 {
166         struct mcp3422 *adc = iio_priv(iio);
167         int err;
168
169         u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
170         u8 pga           = MCP3422_PGA(adc->config);
171
172         switch (mask) {
173         case IIO_CHAN_INFO_RAW:
174                 err = mcp3422_read_channel(adc, channel, val1);
175                 if (err < 0)
176                         return -EINVAL;
177                 return IIO_VAL_INT;
178
179         case IIO_CHAN_INFO_SCALE:
180
181                 *val1 = 0;
182                 *val2 = mcp3422_scales[sample_rate][pga];
183                 return IIO_VAL_INT_PLUS_NANO;
184
185         case IIO_CHAN_INFO_SAMP_FREQ:
186                 *val1 = mcp3422_sample_rates[MCP3422_SAMPLE_RATE(adc->config)];
187                 return IIO_VAL_INT;
188
189         default:
190                 break;
191         }
192
193         return -EINVAL;
194 }
195
196 static int mcp3422_write_raw(struct iio_dev *iio,
197                         struct iio_chan_spec const *channel, int val1,
198                         int val2, long mask)
199 {
200         struct mcp3422 *adc = iio_priv(iio);
201         u8 temp;
202         u8 config = adc->config;
203         u8 req_channel = channel->channel;
204         u8 sample_rate = MCP3422_SAMPLE_RATE(config);
205         u8 i;
206
207         switch (mask) {
208         case IIO_CHAN_INFO_SCALE:
209                 if (val1 != 0)
210                         return -EINVAL;
211
212                 for (i = 0; i < ARRAY_SIZE(mcp3422_scales[0]); i++) {
213                         if (val2 == mcp3422_scales[sample_rate][i]) {
214                                 adc->pga[req_channel] = i;
215
216                                 config &= ~MCP3422_CHANNEL_MASK;
217                                 config |= MCP3422_CHANNEL_VALUE(req_channel);
218                                 config &= ~MCP3422_PGA_MASK;
219                                 config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
220
221                                 return mcp3422_update_config(adc, config);
222                         }
223                 }
224                 return -EINVAL;
225
226         case IIO_CHAN_INFO_SAMP_FREQ:
227                 switch (val1) {
228                 case 240:
229                         temp = MCP3422_SRATE_240;
230                         break;
231                 case 60:
232                         temp = MCP3422_SRATE_60;
233                         break;
234                 case 15:
235                         temp = MCP3422_SRATE_15;
236                         break;
237                 case 3:
238                         if (adc->id > 4)
239                                 return -EINVAL;
240                         temp = MCP3422_SRATE_3;
241                         break;
242                 default:
243                         return -EINVAL;
244                 }
245
246                 config &= ~MCP3422_CHANNEL_MASK;
247                 config |= MCP3422_CHANNEL_VALUE(req_channel);
248                 config &= ~MCP3422_SRATE_MASK;
249                 config |= MCP3422_SAMPLE_RATE_VALUE(temp);
250
251                 return mcp3422_update_config(adc, config);
252
253         default:
254                 break;
255         }
256
257         return -EINVAL;
258 }
259
260 static int mcp3422_write_raw_get_fmt(struct iio_dev *indio_dev,
261                 struct iio_chan_spec const *chan, long mask)
262 {
263         switch (mask) {
264         case IIO_CHAN_INFO_SCALE:
265                 return IIO_VAL_INT_PLUS_NANO;
266         case IIO_CHAN_INFO_SAMP_FREQ:
267                 return IIO_VAL_INT_PLUS_MICRO;
268         default:
269                 return -EINVAL;
270         }
271 }
272
273 static ssize_t mcp3422_show_samp_freqs(struct device *dev,
274                 struct device_attribute *attr, char *buf)
275 {
276         struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
277
278         if (adc->id > 4)
279                 return sprintf(buf, "240 60 15\n");
280
281         return sprintf(buf, "240 60 15 3\n");
282 }
283
284 static ssize_t mcp3422_show_scales(struct device *dev,
285                 struct device_attribute *attr, char *buf)
286 {
287         struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
288         u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
289
290         return sprintf(buf, "0.%09u 0.%09u 0.%09u 0.%09u\n",
291                 mcp3422_scales[sample_rate][0],
292                 mcp3422_scales[sample_rate][1],
293                 mcp3422_scales[sample_rate][2],
294                 mcp3422_scales[sample_rate][3]);
295 }
296
297 static IIO_DEVICE_ATTR(sampling_frequency_available, S_IRUGO,
298                 mcp3422_show_samp_freqs, NULL, 0);
299 static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
300                 mcp3422_show_scales, NULL, 0);
301
302 static struct attribute *mcp3422_attributes[] = {
303         &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
304         &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
305         NULL,
306 };
307
308 static const struct attribute_group mcp3422_attribute_group = {
309         .attrs = mcp3422_attributes,
310 };
311
312 static const struct iio_chan_spec mcp3422_channels[] = {
313         MCP3422_CHAN(0),
314         MCP3422_CHAN(1),
315 };
316
317 static const struct iio_chan_spec mcp3424_channels[] = {
318         MCP3422_CHAN(0),
319         MCP3422_CHAN(1),
320         MCP3422_CHAN(2),
321         MCP3422_CHAN(3),
322 };
323
324 static const struct iio_info mcp3422_info = {
325         .read_raw = mcp3422_read_raw,
326         .write_raw = mcp3422_write_raw,
327         .write_raw_get_fmt = mcp3422_write_raw_get_fmt,
328         .attrs = &mcp3422_attribute_group,
329         .driver_module = THIS_MODULE,
330 };
331
332 static int mcp3422_probe(struct i2c_client *client,
333                          const struct i2c_device_id *id)
334 {
335         struct iio_dev *indio_dev;
336         struct mcp3422 *adc;
337         int err;
338         u8 config;
339
340         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
341                 return -ENODEV;
342
343         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adc));
344         if (!indio_dev)
345                 return -ENOMEM;
346
347         adc = iio_priv(indio_dev);
348         adc->i2c = client;
349         adc->id = (u8)(id->driver_data);
350
351         mutex_init(&adc->lock);
352
353         indio_dev->dev.parent = &client->dev;
354         indio_dev->name = dev_name(&client->dev);
355         indio_dev->modes = INDIO_DIRECT_MODE;
356         indio_dev->info = &mcp3422_info;
357
358         switch (adc->id) {
359         case 2:
360         case 3:
361         case 6:
362         case 7:
363                 indio_dev->channels = mcp3422_channels;
364                 indio_dev->num_channels = ARRAY_SIZE(mcp3422_channels);
365                 break;
366         case 4:
367         case 8:
368                 indio_dev->channels = mcp3424_channels;
369                 indio_dev->num_channels = ARRAY_SIZE(mcp3424_channels);
370                 break;
371         }
372
373         /* meaningful default configuration */
374         config = (MCP3422_CONT_SAMPLING
375                 | MCP3422_CHANNEL_VALUE(1)
376                 | MCP3422_PGA_VALUE(MCP3422_PGA_1)
377                 | MCP3422_SAMPLE_RATE_VALUE(MCP3422_SRATE_240));
378         mcp3422_update_config(adc, config);
379
380         err = devm_iio_device_register(&client->dev, indio_dev);
381         if (err < 0)
382                 return err;
383
384         i2c_set_clientdata(client, indio_dev);
385
386         return 0;
387 }
388
389 static const struct i2c_device_id mcp3422_id[] = {
390         { "mcp3422", 2 },
391         { "mcp3423", 3 },
392         { "mcp3424", 4 },
393         { "mcp3426", 6 },
394         { "mcp3427", 7 },
395         { "mcp3428", 8 },
396         { }
397 };
398 MODULE_DEVICE_TABLE(i2c, mcp3422_id);
399
400 #ifdef CONFIG_OF
401 static const struct of_device_id mcp3422_of_match[] = {
402         { .compatible = "mcp3422" },
403         { }
404 };
405 MODULE_DEVICE_TABLE(of, mcp3422_of_match);
406 #endif
407
408 static struct i2c_driver mcp3422_driver = {
409         .driver = {
410                 .name = "mcp3422",
411                 .of_match_table = of_match_ptr(mcp3422_of_match),
412         },
413         .probe = mcp3422_probe,
414         .id_table = mcp3422_id,
415 };
416 module_i2c_driver(mcp3422_driver);
417
418 MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
419 MODULE_DESCRIPTION("Microchip mcp3422/3/4/6/7/8 driver");
420 MODULE_LICENSE("GPL v2");