staging:iio:adc:ad7152: Miscellaneous fixes and touch-up
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / adc / ad7152.c
1 /*
2  * AD7152 capacitive sensor driver supporting AD7152/3
3  *
4  * Copyright 2010-2011a Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17
18 #include "../iio.h"
19 #include "../sysfs.h"
20
21 /*
22  * TODO: Check compliance of calibscale and calibbias with abi (units)
23  */
24 /*
25  * AD7152 registers definition
26  */
27
28 #define AD7152_REG_STATUS               0
29 #define AD7152_REG_CH1_DATA_HIGH        1
30 #define AD7152_REG_CH2_DATA_HIGH        3
31 #define AD7152_REG_CH1_OFFS_HIGH        5
32 #define AD7152_REG_CH2_OFFS_HIGH        7
33 #define AD7152_REG_CH1_GAIN_HIGH        9
34 #define AD7152_REG_CH1_SETUP            11
35 #define AD7152_REG_CH2_GAIN_HIGH        12
36 #define AD7152_REG_CH2_SETUP            14
37 #define AD7152_REG_CFG                  15
38 #define AD7152_REG_RESEVERD             16
39 #define AD7152_REG_CAPDAC_POS           17
40 #define AD7152_REG_CAPDAC_NEG           18
41 #define AD7152_REG_CFG2                 26
42
43 /* Status Register Bit Designations (AD7152_REG_STATUS) */
44 #define AD7152_STATUS_RDY1              (1 << 0)
45 #define AD7152_STATUS_RDY2              (1 << 1)
46 #define AD7152_STATUS_C1C2              (1 << 2)
47 #define AD7152_STATUS_PWDN              (1 << 7)
48
49 /* Setup Register Bit Designations (AD7152_REG_CHx_SETUP) */
50 #define AD7152_SETUP_CAPDIFF            (1 << 5)
51 #define AD7152_SETUP_RANGE_2pF          (0 << 6)
52 #define AD7152_SETUP_RANGE_0_5pF        (1 << 6)
53 #define AD7152_SETUP_RANGE_1pF          (2 << 6)
54 #define AD7152_SETUP_RANGE_4pF          (3 << 6)
55 #define AD7152_SETUP_RANGE(x)           ((x) << 6)
56
57 /* Config Register Bit Designations (AD7152_REG_CFG) */
58 #define AD7152_CONF_CH2EN               (1 << 3)
59 #define AD7152_CONF_CH1EN               (1 << 4)
60 #define AD7152_CONF_MODE_IDLE           (0 << 0)
61 #define AD7152_CONF_MODE_CONT_CONV      (1 << 0)
62 #define AD7152_CONF_MODE_SINGLE_CONV    (2 << 0)
63 #define AD7152_CONF_MODE_OFFS_CAL       (5 << 0)
64 #define AD7152_CONF_MODE_GAIN_CAL       (6 << 0)
65
66 /* Capdac Register Bit Designations (AD7152_REG_CAPDAC_XXX) */
67 #define AD7152_CAPDAC_DACEN             (1 << 7)
68 #define AD7152_CAPDAC_DACP(x)           ((x) & 0x1F)
69
70 enum {
71         AD7152_DATA,
72         AD7152_OFFS,
73         AD7152_GAIN,
74         AD7152_SETUP
75 };
76
77 /*
78  * struct ad7152_chip_info - chip specifc information
79  */
80
81 struct ad7152_chip_info {
82         struct i2c_client *client;
83         /*
84          * Capacitive channel digital filter setup;
85          * conversion time/update rate setup per channel
86          */
87         u8      filter_rate_setup;
88         u8      setup[2];
89 };
90
91 static inline ssize_t ad7152_start_calib(struct device *dev,
92                                          struct device_attribute *attr,
93                                          const char *buf,
94                                          size_t len,
95                                          u8 regval)
96 {
97         struct iio_dev *dev_info = dev_get_drvdata(dev);
98         struct ad7152_chip_info *chip = iio_priv(dev_info);
99         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
100         bool doit;
101         int ret, timeout = 10;
102
103         ret = strtobool(buf, &doit);
104         if (ret < 0)
105                 return ret;
106
107         if (!doit)
108                 return 0;
109
110         if (this_attr->address == 0)
111                 regval |= AD7152_CONF_CH1EN;
112         else
113                 regval |= AD7152_CONF_CH2EN;
114
115         ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG, regval);
116         if (ret < 0)
117                 return ret;
118
119         do {
120                 mdelay(20);
121                 ret = i2c_smbus_read_byte_data(chip->client, AD7152_REG_CFG);
122                 if (ret < 0)
123                         return ret;
124         } while ((ret == regval) && timeout--);
125
126         return len;
127 }
128 static ssize_t ad7152_start_offset_calib(struct device *dev,
129                                          struct device_attribute *attr,
130                                          const char *buf,
131                                          size_t len)
132 {
133         return ad7152_start_calib(dev, attr, buf, len,
134                                   AD7152_CONF_MODE_OFFS_CAL);
135 }
136 static ssize_t ad7152_start_gain_calib(struct device *dev,
137                                        struct device_attribute *attr,
138                                        const char *buf,
139                                        size_t len)
140 {
141         return ad7152_start_calib(dev, attr, buf, len,
142                                   AD7152_CONF_MODE_GAIN_CAL);
143 }
144
145 static IIO_DEVICE_ATTR(in_capacitance0_calibbias_calibration,
146                        S_IWUSR, NULL, ad7152_start_offset_calib, 0);
147 static IIO_DEVICE_ATTR(in_capacitance1_calibbias_calibration,
148                        S_IWUSR, NULL, ad7152_start_offset_calib, 1);
149 static IIO_DEVICE_ATTR(in_capacitance0_calibscale_calibration,
150                        S_IWUSR, NULL, ad7152_start_gain_calib, 0);
151 static IIO_DEVICE_ATTR(in_capacitance1_calibscale_calibration,
152                        S_IWUSR, NULL, ad7152_start_gain_calib, 1);
153
154 #define IIO_DEV_ATTR_FILTER_RATE_SETUP(_mode, _show, _store)              \
155         IIO_DEVICE_ATTR(filter_rate_setup, _mode, _show, _store, 0)
156
157 static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
158                 struct device_attribute *attr,
159                 char *buf)
160 {
161         struct iio_dev *dev_info = dev_get_drvdata(dev);
162         struct ad7152_chip_info *chip = iio_priv(dev_info);
163
164         return sprintf(buf, "0x%02x\n", chip->filter_rate_setup);
165 }
166
167 static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
168                 struct device_attribute *attr,
169                 const char *buf,
170                 size_t len)
171 {
172         struct iio_dev *dev_info = dev_get_drvdata(dev);
173         struct ad7152_chip_info *chip = iio_priv(dev_info);
174         u8 data;
175         int ret;
176
177         ret = kstrtou8(buf, 10, &data);
178         if (ret < 0)
179                 return ret;
180
181         ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG2, data);
182         if (ret < 0)
183                 return ret;
184
185         chip->filter_rate_setup = data;
186
187         return len;
188 }
189
190 static IIO_DEV_ATTR_FILTER_RATE_SETUP(S_IRUGO | S_IWUSR,
191                 ad7152_show_filter_rate_setup,
192                 ad7152_store_filter_rate_setup);
193
194 static struct attribute *ad7152_attributes[] = {
195         &iio_dev_attr_filter_rate_setup.dev_attr.attr,
196         &iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
197         &iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
198         &iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
199         &iio_dev_attr_in_capacitance1_calibscale_calibration.dev_attr.attr,
200         NULL,
201 };
202
203 static const struct attribute_group ad7152_attribute_group = {
204         .attrs = ad7152_attributes,
205 };
206
207 static const u8 ad7152_addresses[][4] = {
208         { AD7152_REG_CH1_DATA_HIGH, AD7152_REG_CH1_OFFS_HIGH,
209           AD7152_REG_CH1_GAIN_HIGH, AD7152_REG_CH1_SETUP },
210         { AD7152_REG_CH2_DATA_HIGH, AD7152_REG_CH2_OFFS_HIGH,
211           AD7152_REG_CH2_GAIN_HIGH, AD7152_REG_CH2_SETUP },
212 };
213
214 /* Values are micro relative to pf base. */
215 static const int ad7152_scale_table[] = {
216         488, 244, 122, 61
217 };
218
219 static int ad7152_write_raw(struct iio_dev *dev_info,
220                             struct iio_chan_spec const *chan,
221                             int val,
222                             int val2,
223                             long mask)
224 {
225         struct ad7152_chip_info *chip = iio_priv(dev_info);
226         int ret, i;
227
228         switch (mask) {
229         case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
230                 if (val != 1)
231                         return -EINVAL;
232
233                 val = (val2 * 1024) / 15625;
234
235                 ret = i2c_smbus_write_word_data(chip->client,
236                                 ad7152_addresses[chan->channel][AD7152_GAIN],
237                                 swab16(val));
238                 if (ret < 0)
239                         return ret;
240
241                 return 0;
242
243         case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
244                 if ((val < 0) | (val > 0xFFFF))
245                         return -EINVAL;
246                 ret = i2c_smbus_write_word_data(chip->client,
247                                 ad7152_addresses[chan->channel][AD7152_OFFS],
248                                 swab16(val));
249                 if (ret < 0)
250                         return ret;
251
252                 return 0;
253         case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
254                 if (val != 0)
255                         return -EINVAL;
256                 for (i = 0; i < ARRAY_SIZE(ad7152_scale_table); i++)
257                         if (val2 <= ad7152_scale_table[i])
258                                 break;
259
260                 chip->setup[chan->channel] &= ~AD7152_SETUP_RANGE_4pF;
261                 chip->setup[chan->channel] |= AD7152_SETUP_RANGE(i);
262
263                 ret = i2c_smbus_write_byte_data(chip->client,
264                                 ad7152_addresses[chan->channel][AD7152_SETUP],
265                                 chip->setup[chan->channel]);
266                 if (ret < 0)
267                         return ret;
268                 else
269                         return 0;
270         default:
271                 return -EINVAL;
272         }
273 }
274 static int ad7152_read_raw(struct iio_dev *dev_info,
275                            struct iio_chan_spec const *chan,
276                            int *val, int *val2,
277                            long mask)
278 {
279         struct ad7152_chip_info *chip = iio_priv(dev_info);
280         int ret;
281         u8 regval = 0;
282         switch (mask) {
283         case 0:
284                 /* First set whether in differential mode */
285
286                 regval = chip->setup[chan->channel];
287
288                 if (chan->differential)
289                         chip->setup[chan->channel] |= AD7152_SETUP_CAPDIFF;
290                 else
291                         chip->setup[chan->channel] &= ~AD7152_SETUP_CAPDIFF;
292
293                 if (regval != chip->setup[chan->channel]) {
294                         ret = i2c_smbus_write_byte_data(chip->client,
295                                 ad7152_addresses[chan->channel][AD7152_SETUP],
296                                 chip->setup[chan->channel]);
297                         if (ret < 0)
298                                 return ret;
299                 }
300                 /* Make sure the channel is enabled */
301                 if (chan->channel == 0)
302                         regval = AD7152_CONF_CH1EN;
303                 else
304                         regval = AD7152_CONF_CH2EN;
305
306                 /* Trigger a single read */
307                 regval |= AD7152_CONF_MODE_SINGLE_CONV;
308                 ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG,
309                                 regval);
310                 if (ret < 0)
311                         return ret;
312
313                 msleep(60); /* Slowest conversion time */
314                 /* Now read the actual register */
315                 ret = i2c_smbus_read_word_data(chip->client,
316                                 ad7152_addresses[chan->channel][AD7152_DATA]);
317                 if (ret < 0)
318                         return ret;
319                 *val = swab16(ret);
320
321                 return IIO_VAL_INT;
322         case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
323
324                 ret = i2c_smbus_read_word_data(chip->client,
325                                 ad7152_addresses[chan->channel][AD7152_GAIN]);
326                 if (ret < 0)
327                         return ret;
328                 /* 1 + gain_val / 2^16 */
329                 *val = 1;
330                 *val2 = (15625 * swab16(ret)) / 1024;
331
332                 return IIO_VAL_INT_PLUS_MICRO;
333         case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
334                 ret = i2c_smbus_read_word_data(chip->client,
335                                 ad7152_addresses[chan->channel][AD7152_OFFS]);
336                 if (ret < 0)
337                         return ret;
338                 *val = swab16(ret);
339
340                 return IIO_VAL_INT;
341         case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
342                 ret = i2c_smbus_read_byte_data(chip->client,
343                                 ad7152_addresses[chan->channel][AD7152_SETUP]);
344                 if (ret < 0)
345                         return ret;
346                 *val = 0;
347                 *val2 = ad7152_scale_table[ret >> 6];
348
349                 return IIO_VAL_INT_PLUS_MICRO;
350         default:
351                 return -EINVAL;
352         };
353 }
354 static const struct iio_info ad7152_info = {
355         .attrs = &ad7152_attribute_group,
356         .read_raw = &ad7152_read_raw,
357         .write_raw = &ad7152_write_raw,
358         .driver_module = THIS_MODULE,
359 };
360
361 static const struct iio_chan_spec ad7152_channels[] = {
362         {
363                 .type = IIO_CAPACITANCE,
364                 .indexed = 1,
365                 .channel = 0,
366                 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) |
367                 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
368                 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
369         }, {
370                 .type = IIO_CAPACITANCE,
371                 .indexed = 1,
372                 .channel = 1,
373                 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) |
374                 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
375                 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
376         }, {
377                 .type = IIO_CAPACITANCE,
378                 .differential = 1,
379                 .indexed = 1,
380                 .channel = 0,
381                 .channel2 = 2,
382                 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) |
383                 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
384                 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
385         }, {
386                 .type = IIO_CAPACITANCE,
387                 .differential = 1,
388                 .indexed = 1,
389                 .channel = 1,
390                 .channel2 = 3,
391                 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) |
392                 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
393                 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
394         }
395 };
396 /*
397  * device probe and remove
398  */
399
400 static int __devinit ad7152_probe(struct i2c_client *client,
401                 const struct i2c_device_id *id)
402 {
403         int ret = 0;
404         struct ad7152_chip_info *chip;
405         struct iio_dev *indio_dev;
406
407         indio_dev = iio_allocate_device(sizeof(*chip));
408         if (indio_dev == NULL) {
409                 ret = -ENOMEM;
410                 goto error_ret;
411         }
412         chip = iio_priv(indio_dev);
413         /* this is only used for device removal purposes */
414         i2c_set_clientdata(client, indio_dev);
415
416         chip->client = client;
417
418         /* Establish that the iio_dev is a child of the i2c device */
419         indio_dev->name = id->name;
420         indio_dev->dev.parent = &client->dev;
421         indio_dev->info = &ad7152_info;
422         indio_dev->channels = ad7152_channels;
423         if (id->driver_data == 0)
424                 indio_dev->num_channels = ARRAY_SIZE(ad7152_channels);
425         else
426                 indio_dev->num_channels = 2;
427         indio_dev->num_channels = ARRAY_SIZE(ad7152_channels);
428         indio_dev->modes = INDIO_DIRECT_MODE;
429
430         ret = iio_device_register(indio_dev);
431         if (ret)
432                 goto error_free_dev;
433
434         dev_err(&client->dev, "%s capacitive sensor registered\n", id->name);
435
436         return 0;
437
438 error_free_dev:
439         iio_free_device(indio_dev);
440 error_ret:
441         return ret;
442 }
443
444 static int __devexit ad7152_remove(struct i2c_client *client)
445 {
446         struct iio_dev *indio_dev = i2c_get_clientdata(client);
447
448         iio_device_unregister(indio_dev);
449
450         return 0;
451 }
452
453 static const struct i2c_device_id ad7152_id[] = {
454         { "ad7152", 0 },
455         { "ad7153", 1 },
456         {}
457 };
458
459 MODULE_DEVICE_TABLE(i2c, ad7152_id);
460
461 static struct i2c_driver ad7152_driver = {
462         .driver = {
463                 .name = KBUILD_MODNAME,
464         },
465         .probe = ad7152_probe,
466         .remove = __devexit_p(ad7152_remove),
467         .id_table = ad7152_id,
468 };
469
470 static __init int ad7152_init(void)
471 {
472         return i2c_add_driver(&ad7152_driver);
473 }
474
475 static __exit void ad7152_exit(void)
476 {
477         i2c_del_driver(&ad7152_driver);
478 }
479
480 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
481 MODULE_DESCRIPTION("Analog Devices AD7152/3 capacitive sensor driver");
482 MODULE_LICENSE("GPL v2");
483
484 module_init(ad7152_init);
485 module_exit(ad7152_exit);