]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/adc/ad7816.c
Merge branch 'iommu-for-tony' of git://github.com/ohadbc/omap-iommu into devel-fixes
[~andy/linux] / 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.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
18 #include "../iio.h"
19 #include "../sysfs.h"
20
21 /*
22  * AD7816 config masks
23  */
24 #define AD7816_FULL                     0x1
25 #define AD7816_PD                       0x2
26 #define AD7816_CS_MASK                  0x7
27 #define AD7816_CS_MAX                   0x4
28
29 /*
30  * AD7816 temperature masks
31  */
32 #define AD7816_VALUE_OFFSET             6
33 #define AD7816_BOUND_VALUE_BASE         0x8
34 #define AD7816_BOUND_VALUE_MIN          -95
35 #define AD7816_BOUND_VALUE_MAX          152
36 #define AD7816_TEMP_FLOAT_OFFSET        2
37 #define AD7816_TEMP_FLOAT_MASK          0x3
38
39
40 /*
41  * struct ad7816_chip_info - chip specifc information
42  */
43
44 struct ad7816_chip_info {
45         struct spi_device *spi_dev;
46         struct iio_dev *indio_dev;
47         u16 rdwr_pin;
48         u16 convert_pin;
49         u16 busy_pin;
50         u8  oti_data[AD7816_CS_MAX+1];
51         u8  channel_id; /* 0 always be temperature */
52         u8  mode;
53 };
54
55 /*
56  * ad7816 data access by SPI
57  */
58 static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
59 {
60         struct spi_device *spi_dev = chip->spi_dev;
61         int ret = 0;
62
63         gpio_set_value(chip->rdwr_pin, 1);
64         gpio_set_value(chip->rdwr_pin, 0);
65         ret = spi_write(spi_dev, &chip->channel_id, sizeof(chip->channel_id));
66         if (ret < 0) {
67                 dev_err(&spi_dev->dev, "SPI channel setting error\n");
68                 return ret;
69         }
70         gpio_set_value(chip->rdwr_pin, 1);
71
72
73         if (chip->mode == AD7816_PD) { /* operating mode 2 */
74                 gpio_set_value(chip->convert_pin, 1);
75                 gpio_set_value(chip->convert_pin, 0);
76         } else { /* operating mode 1 */
77                 gpio_set_value(chip->convert_pin, 0);
78                 gpio_set_value(chip->convert_pin, 1);
79         }
80
81         while (gpio_get_value(chip->busy_pin))
82                 cpu_relax();
83
84         gpio_set_value(chip->rdwr_pin, 0);
85         gpio_set_value(chip->rdwr_pin, 1);
86         ret = spi_read(spi_dev, (u8 *)data, sizeof(*data));
87         if (ret < 0) {
88                 dev_err(&spi_dev->dev, "SPI data read error\n");
89                 return ret;
90         }
91
92         *data = be16_to_cpu(*data);
93
94         return ret;
95 }
96
97 static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data)
98 {
99         struct spi_device *spi_dev = chip->spi_dev;
100         int ret = 0;
101
102         gpio_set_value(chip->rdwr_pin, 1);
103         gpio_set_value(chip->rdwr_pin, 0);
104         ret = spi_write(spi_dev, &data, sizeof(data));
105         if (ret < 0)
106                 dev_err(&spi_dev->dev, "SPI oti data write error\n");
107
108         return ret;
109 }
110
111 static ssize_t ad7816_show_mode(struct device *dev,
112                 struct device_attribute *attr,
113                 char *buf)
114 {
115         struct iio_dev *dev_info = dev_get_drvdata(dev);
116         struct ad7816_chip_info *chip = dev_info->dev_data;
117
118         if (chip->mode)
119                 return sprintf(buf, "power-save\n");
120         else
121                 return sprintf(buf, "full\n");
122 }
123
124 static ssize_t ad7816_store_mode(struct device *dev,
125                 struct device_attribute *attr,
126                 const char *buf,
127                 size_t len)
128 {
129         struct iio_dev *dev_info = dev_get_drvdata(dev);
130         struct ad7816_chip_info *chip = dev_info->dev_data;
131
132         if (strcmp(buf, "full")) {
133                 gpio_set_value(chip->rdwr_pin, 1);
134                 chip->mode = AD7816_FULL;
135         } else {
136                 gpio_set_value(chip->rdwr_pin, 0);
137                 chip->mode = AD7816_PD;
138         }
139
140         return len;
141 }
142
143 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
144                 ad7816_show_mode,
145                 ad7816_store_mode,
146                 0);
147
148 static ssize_t ad7816_show_available_modes(struct device *dev,
149                 struct device_attribute *attr,
150                 char *buf)
151 {
152         return sprintf(buf, "full\npower-save\n");
153 }
154
155 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7816_show_available_modes, NULL, 0);
156
157 static ssize_t ad7816_show_channel(struct device *dev,
158                 struct device_attribute *attr,
159                 char *buf)
160 {
161         struct iio_dev *dev_info = dev_get_drvdata(dev);
162         struct ad7816_chip_info *chip = dev_info->dev_data;
163
164         return sprintf(buf, "%d\n", chip->channel_id);
165 }
166
167 static ssize_t ad7816_store_channel(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 ad7816_chip_info *chip = dev_info->dev_data;
174         unsigned long data;
175         int ret;
176
177         ret = strict_strtoul(buf, 10, &data);
178         if (ret)
179                 return -EINVAL;
180
181         if (data > AD7816_CS_MAX && data != AD7816_CS_MASK) {
182                 dev_err(&chip->spi_dev->dev, "Invalid channel id %lu for %s.\n",
183                         data, dev_info->name);
184                 return -EINVAL;
185         } else if (strcmp(dev_info->name, "ad7818") == 0 && data > 1) {
186                 dev_err(&chip->spi_dev->dev,
187                         "Invalid channel id %lu for ad7818.\n", data);
188                 return -EINVAL;
189         } else if (strcmp(dev_info->name, "ad7816") == 0 && data > 0) {
190                 dev_err(&chip->spi_dev->dev,
191                         "Invalid channel id %lu for ad7816.\n", data);
192                 return -EINVAL;
193         }
194
195         chip->channel_id = data;
196
197         return len;
198 }
199
200 static IIO_DEVICE_ATTR(channel, S_IRUGO | S_IWUSR,
201                 ad7816_show_channel,
202                 ad7816_store_channel,
203                 0);
204
205
206 static ssize_t ad7816_show_value(struct device *dev,
207                 struct device_attribute *attr,
208                 char *buf)
209 {
210         struct iio_dev *dev_info = dev_get_drvdata(dev);
211         struct ad7816_chip_info *chip = dev_info->dev_data;
212         u16 data;
213         s8 value;
214         int ret;
215
216         ret = ad7816_spi_read(chip, &data);
217         if (ret)
218                 return -EIO;
219
220         data >>= AD7816_VALUE_OFFSET;
221
222         if (chip->channel_id == 0) {
223                 value = (s8)((data >> AD7816_TEMP_FLOAT_OFFSET) - 103);
224                 data &= AD7816_TEMP_FLOAT_MASK;
225                 if (value < 0)
226                         data = (1 << AD7816_TEMP_FLOAT_OFFSET) - data;
227                 return sprintf(buf, "%d.%.2d\n", value, data * 25);
228         } else
229                 return sprintf(buf, "%u\n", data);
230 }
231
232 static IIO_DEVICE_ATTR(value, S_IRUGO, ad7816_show_value, NULL, 0);
233
234 static struct attribute *ad7816_attributes[] = {
235         &iio_dev_attr_available_modes.dev_attr.attr,
236         &iio_dev_attr_mode.dev_attr.attr,
237         &iio_dev_attr_channel.dev_attr.attr,
238         &iio_dev_attr_value.dev_attr.attr,
239         NULL,
240 };
241
242 static const struct attribute_group ad7816_attribute_group = {
243         .attrs = ad7816_attributes,
244 };
245
246 /*
247  * temperature bound events
248  */
249
250 #define IIO_EVENT_CODE_AD7816_OTI IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_TEMP, \
251                                                        0,               \
252                                                        IIO_EV_TYPE_THRESH, \
253                                                        IIO_EV_DIR_FALLING)
254
255 static irqreturn_t ad7816_event_handler(int irq, void *private)
256 {
257         iio_push_event(private, 0,
258                        IIO_EVENT_CODE_AD7816_OTI,
259                        iio_get_time_ns());
260         return IRQ_HANDLED;
261 }
262
263 static ssize_t ad7816_show_oti(struct device *dev,
264                 struct device_attribute *attr,
265                 char *buf)
266 {
267         struct iio_dev *dev_info = dev_get_drvdata(dev);
268         struct ad7816_chip_info *chip = dev_info->dev_data;
269         int value;
270
271         if (chip->channel_id > AD7816_CS_MAX) {
272                 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
273                 return -EINVAL;
274         } else if (chip->channel_id == 0) {
275                 value = AD7816_BOUND_VALUE_MIN +
276                         (chip->oti_data[chip->channel_id] -
277                         AD7816_BOUND_VALUE_BASE);
278                 return sprintf(buf, "%d\n", value);
279         } else
280                 return sprintf(buf, "%u\n", chip->oti_data[chip->channel_id]);
281 }
282
283 static inline ssize_t ad7816_set_oti(struct device *dev,
284                 struct device_attribute *attr,
285                 const char *buf,
286                 size_t len)
287 {
288         struct iio_dev *dev_info = dev_get_drvdata(dev);
289         struct ad7816_chip_info *chip = dev_info->dev_data;
290         long value;
291         u8 data;
292         int ret;
293
294         ret = strict_strtol(buf, 10, &value);
295
296         if (chip->channel_id > AD7816_CS_MAX) {
297                 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
298                 return -EINVAL;
299         } else if (chip->channel_id == 0) {
300                 if (ret || value < AD7816_BOUND_VALUE_MIN ||
301                         value > AD7816_BOUND_VALUE_MAX)
302                         return -EINVAL;
303
304                 data = (u8)(value - AD7816_BOUND_VALUE_MIN +
305                         AD7816_BOUND_VALUE_BASE);
306         } else {
307                 if (ret || value < AD7816_BOUND_VALUE_BASE || value > 255)
308                         return -EINVAL;
309
310                 data = (u8)value;
311         }
312
313         ret = ad7816_spi_write(chip, data);
314         if (ret)
315                 return -EIO;
316
317         chip->oti_data[chip->channel_id] = data;
318
319         return len;
320 }
321
322 static IIO_DEVICE_ATTR(oti, S_IRUGO | S_IWUSR,
323                        ad7816_show_oti, ad7816_set_oti, 0);
324
325 static struct attribute *ad7816_event_attributes[] = {
326         &iio_dev_attr_oti.dev_attr.attr,
327         NULL,
328 };
329
330 static struct attribute_group ad7816_event_attribute_group = {
331         .attrs = ad7816_event_attributes,
332 };
333
334 static const struct iio_info ad7816_info = {
335         .attrs = &ad7816_attribute_group,
336         .num_interrupt_lines = 1,
337         .event_attrs = &ad7816_event_attribute_group,
338         .driver_module = THIS_MODULE,
339 };
340
341 /*
342  * device probe and remove
343  */
344
345 static int __devinit ad7816_probe(struct spi_device *spi_dev)
346 {
347         struct ad7816_chip_info *chip;
348         unsigned short *pins = spi_dev->dev.platform_data;
349         int ret = 0;
350         int i;
351
352         if (!pins) {
353                 dev_err(&spi_dev->dev, "No necessary GPIO platform data.\n");
354                 return -EINVAL;
355         }
356
357         chip = kzalloc(sizeof(struct ad7816_chip_info), GFP_KERNEL);
358
359         if (chip == NULL)
360                 return -ENOMEM;
361
362         /* this is only used for device removal purposes */
363         dev_set_drvdata(&spi_dev->dev, chip);
364
365         chip->spi_dev = spi_dev;
366         for (i = 0; i <= AD7816_CS_MAX; i++)
367                 chip->oti_data[i] = 203;
368         chip->rdwr_pin = pins[0];
369         chip->convert_pin = pins[1];
370         chip->busy_pin = pins[2];
371
372         ret = gpio_request(chip->rdwr_pin, spi_get_device_id(spi_dev)->name);
373         if (ret) {
374                 dev_err(&spi_dev->dev, "Fail to request rdwr gpio PIN %d.\n",
375                         chip->rdwr_pin);
376                 goto error_free_chip;
377         }
378         gpio_direction_input(chip->rdwr_pin);
379         ret = gpio_request(chip->convert_pin, spi_get_device_id(spi_dev)->name);
380         if (ret) {
381                 dev_err(&spi_dev->dev, "Fail to request convert gpio PIN %d.\n",
382                         chip->convert_pin);
383                 goto error_free_gpio_rdwr;
384         }
385         gpio_direction_input(chip->convert_pin);
386         ret = gpio_request(chip->busy_pin, spi_get_device_id(spi_dev)->name);
387         if (ret) {
388                 dev_err(&spi_dev->dev, "Fail to request busy gpio PIN %d.\n",
389                         chip->busy_pin);
390                 goto error_free_gpio_convert;
391         }
392         gpio_direction_input(chip->busy_pin);
393
394         chip->indio_dev = iio_allocate_device(0);
395         if (chip->indio_dev == NULL) {
396                 ret = -ENOMEM;
397                 goto error_free_gpio;
398         }
399         chip->indio_dev->name = spi_get_device_id(spi_dev)->name;
400         chip->indio_dev->dev.parent = &spi_dev->dev;
401         chip->indio_dev->info = &ad7816_info;
402         chip->indio_dev->dev_data = (void *)chip;
403         chip->indio_dev->modes = INDIO_DIRECT_MODE;
404
405         ret = iio_device_register(chip->indio_dev);
406         if (ret)
407                 goto error_free_dev;
408
409         if (spi_dev->irq) {
410                 /* Only low trigger is supported in ad7816/7/8 */
411                 ret = request_threaded_irq(spi_dev->irq,
412                                            NULL,
413                                            &ad7816_event_handler,
414                                            IRQF_TRIGGER_LOW,
415                                            chip->indio_dev->name,
416                                            chip->indio_dev);
417                 if (ret)
418                         goto error_unreg_dev;
419         }
420
421         dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n",
422                          chip->indio_dev->name);
423
424         return 0;
425
426 error_unreg_dev:
427         iio_device_unregister(chip->indio_dev);
428 error_free_dev:
429         iio_free_device(chip->indio_dev);
430 error_free_gpio:
431         gpio_free(chip->busy_pin);
432 error_free_gpio_convert:
433         gpio_free(chip->convert_pin);
434 error_free_gpio_rdwr:
435         gpio_free(chip->rdwr_pin);
436 error_free_chip:
437         kfree(chip);
438
439         return ret;
440 }
441
442 static int __devexit ad7816_remove(struct spi_device *spi_dev)
443 {
444         struct ad7816_chip_info *chip = dev_get_drvdata(&spi_dev->dev);
445         struct iio_dev *indio_dev = chip->indio_dev;
446
447         dev_set_drvdata(&spi_dev->dev, NULL);
448         if (spi_dev->irq)
449                 free_irq(spi_dev->irq, indio_dev);
450         iio_device_unregister(indio_dev);
451         iio_free_device(chip->indio_dev);
452         gpio_free(chip->busy_pin);
453         gpio_free(chip->convert_pin);
454         gpio_free(chip->rdwr_pin);
455         kfree(chip);
456
457         return 0;
458 }
459
460 static const struct spi_device_id ad7816_id[] = {
461         { "ad7816", 0 },
462         { "ad7817", 0 },
463         { "ad7818", 0 },
464         {}
465 };
466
467 MODULE_DEVICE_TABLE(spi, ad7816_id);
468
469 static struct spi_driver ad7816_driver = {
470         .driver = {
471                 .name = "ad7816",
472                 .bus = &spi_bus_type,
473                 .owner = THIS_MODULE,
474         },
475         .probe = ad7816_probe,
476         .remove = __devexit_p(ad7816_remove),
477         .id_table = ad7816_id,
478 };
479
480 static __init int ad7816_init(void)
481 {
482         return spi_register_driver(&ad7816_driver);
483 }
484
485 static __exit void ad7816_exit(void)
486 {
487         spi_unregister_driver(&ad7816_driver);
488 }
489
490 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
491 MODULE_DESCRIPTION("Analog Devices AD7816/7/8 digital"
492                         " temperature sensor driver");
493 MODULE_LICENSE("GPL v2");
494
495 module_init(ad7816_init);
496 module_exit(ad7816_exit);