Linux-libre 5.0.10-gnu
[librecmc/linux-libre.git] / drivers / staging / iio / adc / ad7816.c
1 /*
2  * AD7816 digital temperature sensor driver supporting AD7816/7/8
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/spi/spi.h>
17 #include <linux/module.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/events.h>
22
23 /*
24  * AD7816 config masks
25  */
26 #define AD7816_FULL                     0x1
27 #define AD7816_PD                       0x2
28 #define AD7816_CS_MASK                  0x7
29 #define AD7816_CS_MAX                   0x4
30
31 /*
32  * AD7816 temperature masks
33  */
34 #define AD7816_VALUE_OFFSET             6
35 #define AD7816_BOUND_VALUE_BASE         0x8
36 #define AD7816_BOUND_VALUE_MIN          -95
37 #define AD7816_BOUND_VALUE_MAX          152
38 #define AD7816_TEMP_FLOAT_OFFSET        2
39 #define AD7816_TEMP_FLOAT_MASK          0x3
40
41 /*
42  * struct ad7816_chip_info - chip specific information
43  */
44
45 struct ad7816_chip_info {
46         kernel_ulong_t id;
47         struct spi_device *spi_dev;
48         struct gpio_desc *rdwr_pin;
49         struct gpio_desc *convert_pin;
50         struct gpio_desc *busy_pin;
51         u8  oti_data[AD7816_CS_MAX + 1];
52         u8  channel_id; /* 0 always be temperature */
53         u8  mode;
54 };
55
56 enum ad7816_type {
57         ID_AD7816,
58         ID_AD7817,
59         ID_AD7818,
60 };
61
62 /*
63  * ad7816 data access by SPI
64  */
65 static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
66 {
67         struct spi_device *spi_dev = chip->spi_dev;
68         int ret = 0;
69         __be16 buf;
70
71         gpiod_set_value(chip->rdwr_pin, 1);
72         gpiod_set_value(chip->rdwr_pin, 0);
73         ret = spi_write(spi_dev, &chip->channel_id, sizeof(chip->channel_id));
74         if (ret < 0) {
75                 dev_err(&spi_dev->dev, "SPI channel setting error\n");
76                 return ret;
77         }
78         gpiod_set_value(chip->rdwr_pin, 1);
79
80         if (chip->mode == AD7816_PD) { /* operating mode 2 */
81                 gpiod_set_value(chip->convert_pin, 1);
82                 gpiod_set_value(chip->convert_pin, 0);
83         } else { /* operating mode 1 */
84                 gpiod_set_value(chip->convert_pin, 0);
85                 gpiod_set_value(chip->convert_pin, 1);
86         }
87
88         if (chip->id == ID_AD7816 || chip->id == ID_AD7817) {
89                 while (gpiod_get_value(chip->busy_pin))
90                         cpu_relax();
91         }
92
93         gpiod_set_value(chip->rdwr_pin, 0);
94         gpiod_set_value(chip->rdwr_pin, 1);
95         ret = spi_read(spi_dev, &buf, sizeof(*data));
96         if (ret < 0) {
97                 dev_err(&spi_dev->dev, "SPI data read error\n");
98                 return ret;
99         }
100
101         *data = be16_to_cpu(buf);
102
103         return ret;
104 }
105
106 static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data)
107 {
108         struct spi_device *spi_dev = chip->spi_dev;
109         int ret = 0;
110
111         gpiod_set_value(chip->rdwr_pin, 1);
112         gpiod_set_value(chip->rdwr_pin, 0);
113         ret = spi_write(spi_dev, &data, sizeof(data));
114         if (ret < 0)
115                 dev_err(&spi_dev->dev, "SPI oti data write error\n");
116
117         return ret;
118 }
119
120 static ssize_t ad7816_show_mode(struct device *dev,
121                                 struct device_attribute *attr,
122                                 char *buf)
123 {
124         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
125         struct ad7816_chip_info *chip = iio_priv(indio_dev);
126
127         if (chip->mode)
128                 return sprintf(buf, "power-save\n");
129         return sprintf(buf, "full\n");
130 }
131
132 static ssize_t ad7816_store_mode(struct device *dev,
133                                  struct device_attribute *attr,
134                                  const char *buf,
135                                  size_t len)
136 {
137         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
138         struct ad7816_chip_info *chip = iio_priv(indio_dev);
139
140         if (strcmp(buf, "full")) {
141                 gpiod_set_value(chip->rdwr_pin, 1);
142                 chip->mode = AD7816_FULL;
143         } else {
144                 gpiod_set_value(chip->rdwr_pin, 0);
145                 chip->mode = AD7816_PD;
146         }
147
148         return len;
149 }
150
151 static IIO_DEVICE_ATTR(mode, 0644,
152                 ad7816_show_mode,
153                 ad7816_store_mode,
154                 0);
155
156 static ssize_t ad7816_show_available_modes(struct device *dev,
157                                            struct device_attribute *attr,
158                                            char *buf)
159 {
160         return sprintf(buf, "full\npower-save\n");
161 }
162
163 static IIO_DEVICE_ATTR(available_modes, 0444, ad7816_show_available_modes,
164                         NULL, 0);
165
166 static ssize_t ad7816_show_channel(struct device *dev,
167                                    struct device_attribute *attr,
168                                    char *buf)
169 {
170         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
171         struct ad7816_chip_info *chip = iio_priv(indio_dev);
172
173         return sprintf(buf, "%d\n", chip->channel_id);
174 }
175
176 static ssize_t ad7816_store_channel(struct device *dev,
177                                     struct device_attribute *attr,
178                                     const char *buf,
179                                     size_t len)
180 {
181         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
182         struct ad7816_chip_info *chip = iio_priv(indio_dev);
183         unsigned long data;
184         int ret;
185
186         ret = kstrtoul(buf, 10, &data);
187         if (ret)
188                 return ret;
189
190         if (data > AD7816_CS_MAX && data != AD7816_CS_MASK) {
191                 dev_err(&chip->spi_dev->dev, "Invalid channel id %lu for %s.\n",
192                         data, indio_dev->name);
193                 return -EINVAL;
194         } else if (strcmp(indio_dev->name, "ad7818") == 0 && data > 1) {
195                 dev_err(&chip->spi_dev->dev,
196                         "Invalid channel id %lu for ad7818.\n", data);
197                 return -EINVAL;
198         } else if (strcmp(indio_dev->name, "ad7816") == 0 && data > 0) {
199                 dev_err(&chip->spi_dev->dev,
200                         "Invalid channel id %lu for ad7816.\n", data);
201                 return -EINVAL;
202         }
203
204         chip->channel_id = data;
205
206         return len;
207 }
208
209 static IIO_DEVICE_ATTR(channel, 0644,
210                 ad7816_show_channel,
211                 ad7816_store_channel,
212                 0);
213
214 static ssize_t ad7816_show_value(struct device *dev,
215                                  struct device_attribute *attr,
216                                  char *buf)
217 {
218         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
219         struct ad7816_chip_info *chip = iio_priv(indio_dev);
220         u16 data;
221         s8 value;
222         int ret;
223
224         ret = ad7816_spi_read(chip, &data);
225         if (ret)
226                 return -EIO;
227
228         data >>= AD7816_VALUE_OFFSET;
229
230         if (chip->channel_id == 0) {
231                 value = (s8)((data >> AD7816_TEMP_FLOAT_OFFSET) - 103);
232                 data &= AD7816_TEMP_FLOAT_MASK;
233                 if (value < 0)
234                         data = (1 << AD7816_TEMP_FLOAT_OFFSET) - data;
235                 return sprintf(buf, "%d.%.2d\n", value, data * 25);
236         }
237         return sprintf(buf, "%u\n", data);
238 }
239
240 static IIO_DEVICE_ATTR(value, 0444, ad7816_show_value, NULL, 0);
241
242 static struct attribute *ad7816_attributes[] = {
243         &iio_dev_attr_available_modes.dev_attr.attr,
244         &iio_dev_attr_mode.dev_attr.attr,
245         &iio_dev_attr_channel.dev_attr.attr,
246         &iio_dev_attr_value.dev_attr.attr,
247         NULL,
248 };
249
250 static const struct attribute_group ad7816_attribute_group = {
251         .attrs = ad7816_attributes,
252 };
253
254 /*
255  * temperature bound events
256  */
257
258 #define IIO_EVENT_CODE_AD7816_OTI IIO_UNMOD_EVENT_CODE(IIO_TEMP,        \
259                                                        0,               \
260                                                        IIO_EV_TYPE_THRESH, \
261                                                        IIO_EV_DIR_FALLING)
262
263 static irqreturn_t ad7816_event_handler(int irq, void *private)
264 {
265         iio_push_event(private, IIO_EVENT_CODE_AD7816_OTI,
266                        iio_get_time_ns(private));
267         return IRQ_HANDLED;
268 }
269
270 static ssize_t ad7816_show_oti(struct device *dev,
271                                struct device_attribute *attr,
272                                char *buf)
273 {
274         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
275         struct ad7816_chip_info *chip = iio_priv(indio_dev);
276         int value;
277
278         if (chip->channel_id > AD7816_CS_MAX) {
279                 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
280                 return -EINVAL;
281         } else if (chip->channel_id == 0) {
282                 value = AD7816_BOUND_VALUE_MIN +
283                         (chip->oti_data[chip->channel_id] -
284                         AD7816_BOUND_VALUE_BASE);
285                 return sprintf(buf, "%d\n", value);
286         }
287         return sprintf(buf, "%u\n", chip->oti_data[chip->channel_id]);
288 }
289
290 static inline ssize_t ad7816_set_oti(struct device *dev,
291                                      struct device_attribute *attr,
292                                      const char *buf,
293                                      size_t len)
294 {
295         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
296         struct ad7816_chip_info *chip = iio_priv(indio_dev);
297         long value;
298         u8 data;
299         int ret;
300
301         ret = kstrtol(buf, 10, &value);
302         if (ret)
303                 return ret;
304
305         if (chip->channel_id > AD7816_CS_MAX) {
306                 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
307                 return -EINVAL;
308         } else if (chip->channel_id == 0) {
309                 if (value < AD7816_BOUND_VALUE_MIN ||
310                     value > AD7816_BOUND_VALUE_MAX)
311                         return -EINVAL;
312
313                 data = (u8)(value - AD7816_BOUND_VALUE_MIN +
314                         AD7816_BOUND_VALUE_BASE);
315         } else {
316                 if (value < AD7816_BOUND_VALUE_BASE || value > 255)
317                         return -EINVAL;
318
319                 data = (u8)value;
320         }
321
322         ret = ad7816_spi_write(chip, data);
323         if (ret)
324                 return -EIO;
325
326         chip->oti_data[chip->channel_id] = data;
327
328         return len;
329 }
330
331 static IIO_DEVICE_ATTR(oti, 0644,
332                        ad7816_show_oti, ad7816_set_oti, 0);
333
334 static struct attribute *ad7816_event_attributes[] = {
335         &iio_dev_attr_oti.dev_attr.attr,
336         NULL,
337 };
338
339 static const struct attribute_group ad7816_event_attribute_group = {
340         .attrs = ad7816_event_attributes,
341         .name = "events",
342 };
343
344 static const struct iio_info ad7816_info = {
345         .attrs = &ad7816_attribute_group,
346         .event_attrs = &ad7816_event_attribute_group,
347 };
348
349 /*
350  * device probe and remove
351  */
352
353 static int ad7816_probe(struct spi_device *spi_dev)
354 {
355         struct ad7816_chip_info *chip;
356         struct iio_dev *indio_dev;
357         int ret = 0;
358         int i;
359
360         indio_dev = devm_iio_device_alloc(&spi_dev->dev, sizeof(*chip));
361         if (!indio_dev)
362                 return -ENOMEM;
363         chip = iio_priv(indio_dev);
364         /* this is only used for device removal purposes */
365         dev_set_drvdata(&spi_dev->dev, indio_dev);
366
367         chip->spi_dev = spi_dev;
368         for (i = 0; i <= AD7816_CS_MAX; i++)
369                 chip->oti_data[i] = 203;
370
371         chip->id = spi_get_device_id(spi_dev)->driver_data;
372         chip->rdwr_pin = devm_gpiod_get(&spi_dev->dev, "rdwr", GPIOD_OUT_HIGH);
373         if (IS_ERR(chip->rdwr_pin)) {
374                 ret = PTR_ERR(chip->rdwr_pin);
375                 dev_err(&spi_dev->dev, "Failed to request rdwr GPIO: %d\n",
376                         ret);
377                 return ret;
378         }
379         chip->convert_pin = devm_gpiod_get(&spi_dev->dev, "convert",
380                                            GPIOD_OUT_HIGH);
381         if (IS_ERR(chip->convert_pin)) {
382                 ret = PTR_ERR(chip->convert_pin);
383                 dev_err(&spi_dev->dev, "Failed to request convert GPIO: %d\n",
384                         ret);
385                 return ret;
386         }
387         if (chip->id == ID_AD7816 || chip->id == ID_AD7817) {
388                 chip->busy_pin = devm_gpiod_get(&spi_dev->dev, "busy",
389                                                 GPIOD_IN);
390                 if (IS_ERR(chip->busy_pin)) {
391                         ret = PTR_ERR(chip->busy_pin);
392                         dev_err(&spi_dev->dev, "Failed to request busy GPIO: %d\n",
393                                 ret);
394                         return ret;
395                 }
396         }
397
398         indio_dev->name = spi_get_device_id(spi_dev)->name;
399         indio_dev->dev.parent = &spi_dev->dev;
400         indio_dev->info = &ad7816_info;
401         indio_dev->modes = INDIO_DIRECT_MODE;
402
403         if (spi_dev->irq) {
404                 /* Only low trigger is supported in ad7816/7/8 */
405                 ret = devm_request_threaded_irq(&spi_dev->dev, spi_dev->irq,
406                                                 NULL,
407                                                 &ad7816_event_handler,
408                                                 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
409                                                 indio_dev->name,
410                                                 indio_dev);
411                 if (ret)
412                         return ret;
413         }
414
415         ret = devm_iio_device_register(&spi_dev->dev, indio_dev);
416         if (ret)
417                 return ret;
418
419         dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n",
420                  indio_dev->name);
421
422         return 0;
423 }
424
425 static const struct of_device_id ad7816_of_match[] = {
426         { .compatible = "adi,ad7816", },
427         { .compatible = "adi,ad7817", },
428         { .compatible = "adi,ad7818", },
429         { }
430 };
431 MODULE_DEVICE_TABLE(of, ad7816_of_match);
432
433 static const struct spi_device_id ad7816_id[] = {
434         { "ad7816", ID_AD7816 },
435         { "ad7817", ID_AD7817 },
436         { "ad7818", ID_AD7818 },
437         {}
438 };
439
440 MODULE_DEVICE_TABLE(spi, ad7816_id);
441
442 static struct spi_driver ad7816_driver = {
443         .driver = {
444                 .name = "ad7816",
445                 .of_match_table = ad7816_of_match,
446         },
447         .probe = ad7816_probe,
448         .id_table = ad7816_id,
449 };
450 module_spi_driver(ad7816_driver);
451
452 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
453 MODULE_DESCRIPTION("Analog Devices AD7816/7/8 digital temperature sensor driver");
454 MODULE_LICENSE("GPL v2");