]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/adc/ad7816.c
Merge 4.3-rc7 into staging-next
[karo-tx-linux.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.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         struct spi_device *spi_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         if (chip->mode == AD7816_PD) { /* operating mode 2 */
73                 gpio_set_value(chip->convert_pin, 1);
74                 gpio_set_value(chip->convert_pin, 0);
75         } else { /* operating mode 1 */
76                 gpio_set_value(chip->convert_pin, 0);
77                 gpio_set_value(chip->convert_pin, 1);
78         }
79
80         while (gpio_get_value(chip->busy_pin))
81                 cpu_relax();
82
83         gpio_set_value(chip->rdwr_pin, 0);
84         gpio_set_value(chip->rdwr_pin, 1);
85         ret = spi_read(spi_dev, (u8 *)data, sizeof(*data));
86         if (ret < 0) {
87                 dev_err(&spi_dev->dev, "SPI data read error\n");
88                 return ret;
89         }
90
91         *data = be16_to_cpu(*data);
92
93         return ret;
94 }
95
96 static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data)
97 {
98         struct spi_device *spi_dev = chip->spi_dev;
99         int ret = 0;
100
101         gpio_set_value(chip->rdwr_pin, 1);
102         gpio_set_value(chip->rdwr_pin, 0);
103         ret = spi_write(spi_dev, &data, sizeof(data));
104         if (ret < 0)
105                 dev_err(&spi_dev->dev, "SPI oti data write error\n");
106
107         return ret;
108 }
109
110 static ssize_t ad7816_show_mode(struct device *dev,
111                                 struct device_attribute *attr,
112                                 char *buf)
113 {
114         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
115         struct ad7816_chip_info *chip = iio_priv(indio_dev);
116
117         if (chip->mode)
118                 return sprintf(buf, "power-save\n");
119         return sprintf(buf, "full\n");
120 }
121
122 static ssize_t ad7816_store_mode(struct device *dev,
123                                  struct device_attribute *attr,
124                                  const char *buf,
125                                  size_t len)
126 {
127         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
128         struct ad7816_chip_info *chip = iio_priv(indio_dev);
129
130         if (strcmp(buf, "full")) {
131                 gpio_set_value(chip->rdwr_pin, 1);
132                 chip->mode = AD7816_FULL;
133         } else {
134                 gpio_set_value(chip->rdwr_pin, 0);
135                 chip->mode = AD7816_PD;
136         }
137
138         return len;
139 }
140
141 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
142                 ad7816_show_mode,
143                 ad7816_store_mode,
144                 0);
145
146 static ssize_t ad7816_show_available_modes(struct device *dev,
147                                            struct device_attribute *attr,
148                                            char *buf)
149 {
150         return sprintf(buf, "full\npower-save\n");
151 }
152
153 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7816_show_available_modes,
154                         NULL, 0);
155
156 static ssize_t ad7816_show_channel(struct device *dev,
157                                    struct device_attribute *attr,
158                                    char *buf)
159 {
160         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
161         struct ad7816_chip_info *chip = iio_priv(indio_dev);
162
163         return sprintf(buf, "%d\n", chip->channel_id);
164 }
165
166 static ssize_t ad7816_store_channel(struct device *dev,
167                                     struct device_attribute *attr,
168                                     const char *buf,
169                                     size_t len)
170 {
171         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
172         struct ad7816_chip_info *chip = iio_priv(indio_dev);
173         unsigned long data;
174         int ret;
175
176         ret = kstrtoul(buf, 10, &data);
177         if (ret)
178                 return ret;
179
180         if (data > AD7816_CS_MAX && data != AD7816_CS_MASK) {
181                 dev_err(&chip->spi_dev->dev, "Invalid channel id %lu for %s.\n",
182                         data, indio_dev->name);
183                 return -EINVAL;
184         } else if (strcmp(indio_dev->name, "ad7818") == 0 && data > 1) {
185                 dev_err(&chip->spi_dev->dev,
186                         "Invalid channel id %lu for ad7818.\n", data);
187                 return -EINVAL;
188         } else if (strcmp(indio_dev->name, "ad7816") == 0 && data > 0) {
189                 dev_err(&chip->spi_dev->dev,
190                         "Invalid channel id %lu for ad7816.\n", data);
191                 return -EINVAL;
192         }
193
194         chip->channel_id = data;
195
196         return len;
197 }
198
199 static IIO_DEVICE_ATTR(channel, S_IRUGO | S_IWUSR,
200                 ad7816_show_channel,
201                 ad7816_store_channel,
202                 0);
203
204 static ssize_t ad7816_show_value(struct device *dev,
205                                  struct device_attribute *attr,
206                                  char *buf)
207 {
208         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
209         struct ad7816_chip_info *chip = iio_priv(indio_dev);
210         u16 data;
211         s8 value;
212         int ret;
213
214         ret = ad7816_spi_read(chip, &data);
215         if (ret)
216                 return -EIO;
217
218         data >>= AD7816_VALUE_OFFSET;
219
220         if (chip->channel_id == 0) {
221                 value = (s8)((data >> AD7816_TEMP_FLOAT_OFFSET) - 103);
222                 data &= AD7816_TEMP_FLOAT_MASK;
223                 if (value < 0)
224                         data = (1 << AD7816_TEMP_FLOAT_OFFSET) - data;
225                 return sprintf(buf, "%d.%.2d\n", value, data * 25);
226         }
227         return sprintf(buf, "%u\n", data);
228 }
229
230 static IIO_DEVICE_ATTR(value, S_IRUGO, ad7816_show_value, NULL, 0);
231
232 static struct attribute *ad7816_attributes[] = {
233         &iio_dev_attr_available_modes.dev_attr.attr,
234         &iio_dev_attr_mode.dev_attr.attr,
235         &iio_dev_attr_channel.dev_attr.attr,
236         &iio_dev_attr_value.dev_attr.attr,
237         NULL,
238 };
239
240 static const struct attribute_group ad7816_attribute_group = {
241         .attrs = ad7816_attributes,
242 };
243
244 /*
245  * temperature bound events
246  */
247
248 #define IIO_EVENT_CODE_AD7816_OTI IIO_UNMOD_EVENT_CODE(IIO_TEMP,        \
249                                                        0,               \
250                                                        IIO_EV_TYPE_THRESH, \
251                                                        IIO_EV_DIR_FALLING)
252
253 static irqreturn_t ad7816_event_handler(int irq, void *private)
254 {
255         iio_push_event(private, IIO_EVENT_CODE_AD7816_OTI, iio_get_time_ns());
256         return IRQ_HANDLED;
257 }
258
259 static ssize_t ad7816_show_oti(struct device *dev,
260                                struct device_attribute *attr,
261                                char *buf)
262 {
263         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
264         struct ad7816_chip_info *chip = iio_priv(indio_dev);
265         int value;
266
267         if (chip->channel_id > AD7816_CS_MAX) {
268                 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
269                 return -EINVAL;
270         } else if (chip->channel_id == 0) {
271                 value = AD7816_BOUND_VALUE_MIN +
272                         (chip->oti_data[chip->channel_id] -
273                         AD7816_BOUND_VALUE_BASE);
274                 return sprintf(buf, "%d\n", value);
275         }
276         return sprintf(buf, "%u\n", chip->oti_data[chip->channel_id]);
277 }
278
279 static inline ssize_t ad7816_set_oti(struct device *dev,
280                                      struct device_attribute *attr,
281                                      const char *buf,
282                                      size_t len)
283 {
284         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
285         struct ad7816_chip_info *chip = iio_priv(indio_dev);
286         long value;
287         u8 data;
288         int ret;
289
290         ret = kstrtol(buf, 10, &value);
291         if (ret)
292                 return ret;
293
294         if (chip->channel_id > AD7816_CS_MAX) {
295                 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
296                 return -EINVAL;
297         } else if (chip->channel_id == 0) {
298                 if (ret || value < AD7816_BOUND_VALUE_MIN ||
299                     value > AD7816_BOUND_VALUE_MAX)
300                         return -EINVAL;
301
302                 data = (u8)(value - AD7816_BOUND_VALUE_MIN +
303                         AD7816_BOUND_VALUE_BASE);
304         } else {
305                 if (ret || value < AD7816_BOUND_VALUE_BASE || value > 255)
306                         return -EINVAL;
307
308                 data = (u8)value;
309         }
310
311         ret = ad7816_spi_write(chip, data);
312         if (ret)
313                 return -EIO;
314
315         chip->oti_data[chip->channel_id] = data;
316
317         return len;
318 }
319
320 static IIO_DEVICE_ATTR(oti, S_IRUGO | S_IWUSR,
321                        ad7816_show_oti, ad7816_set_oti, 0);
322
323 static struct attribute *ad7816_event_attributes[] = {
324         &iio_dev_attr_oti.dev_attr.attr,
325         NULL,
326 };
327
328 static struct attribute_group ad7816_event_attribute_group = {
329         .attrs = ad7816_event_attributes,
330         .name = "events",
331 };
332
333 static const struct iio_info ad7816_info = {
334         .attrs = &ad7816_attribute_group,
335         .event_attrs = &ad7816_event_attribute_group,
336         .driver_module = THIS_MODULE,
337 };
338
339 /*
340  * device probe and remove
341  */
342
343 static int ad7816_probe(struct spi_device *spi_dev)
344 {
345         struct ad7816_chip_info *chip;
346         struct iio_dev *indio_dev;
347         unsigned short *pins = spi_dev->dev.platform_data;
348         int ret = 0;
349         int i;
350
351         if (!pins) {
352                 dev_err(&spi_dev->dev, "No necessary GPIO platform data.\n");
353                 return -EINVAL;
354         }
355
356         indio_dev = devm_iio_device_alloc(&spi_dev->dev, sizeof(*chip));
357         if (!indio_dev)
358                 return -ENOMEM;
359         chip = iio_priv(indio_dev);
360         /* this is only used for device removal purposes */
361         dev_set_drvdata(&spi_dev->dev, indio_dev);
362
363         chip->spi_dev = spi_dev;
364         for (i = 0; i <= AD7816_CS_MAX; i++)
365                 chip->oti_data[i] = 203;
366         chip->rdwr_pin = pins[0];
367         chip->convert_pin = pins[1];
368         chip->busy_pin = pins[2];
369
370         ret = devm_gpio_request(&spi_dev->dev, chip->rdwr_pin,
371                                 spi_get_device_id(spi_dev)->name);
372         if (ret) {
373                 dev_err(&spi_dev->dev, "Fail to request rdwr gpio PIN %d.\n",
374                         chip->rdwr_pin);
375                 return ret;
376         }
377         gpio_direction_input(chip->rdwr_pin);
378         ret = devm_gpio_request(&spi_dev->dev, chip->convert_pin,
379                                 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                 return ret;
384         }
385         gpio_direction_input(chip->convert_pin);
386         ret = devm_gpio_request(&spi_dev->dev, chip->busy_pin,
387                                 spi_get_device_id(spi_dev)->name);
388         if (ret) {
389                 dev_err(&spi_dev->dev, "Fail to request busy gpio PIN %d.\n",
390                         chip->busy_pin);
391                 return ret;
392         }
393         gpio_direction_input(chip->busy_pin);
394
395         indio_dev->name = spi_get_device_id(spi_dev)->name;
396         indio_dev->dev.parent = &spi_dev->dev;
397         indio_dev->info = &ad7816_info;
398         indio_dev->modes = INDIO_DIRECT_MODE;
399
400         if (spi_dev->irq) {
401                 /* Only low trigger is supported in ad7816/7/8 */
402                 ret = devm_request_threaded_irq(&spi_dev->dev, spi_dev->irq,
403                                                 NULL,
404                                                 &ad7816_event_handler,
405                                                 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
406                                                 indio_dev->name,
407                                                 indio_dev);
408                 if (ret)
409                         return ret;
410         }
411
412         ret = devm_iio_device_register(&spi_dev->dev, indio_dev);
413         if (ret)
414                 return ret;
415
416         dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n",
417                  indio_dev->name);
418
419         return 0;
420 }
421
422 static const struct spi_device_id ad7816_id[] = {
423         { "ad7816", 0 },
424         { "ad7817", 0 },
425         { "ad7818", 0 },
426         {}
427 };
428
429 MODULE_DEVICE_TABLE(spi, ad7816_id);
430
431 static struct spi_driver ad7816_driver = {
432         .driver = {
433                 .name = "ad7816",
434                 .owner = THIS_MODULE,
435         },
436         .probe = ad7816_probe,
437         .id_table = ad7816_id,
438 };
439 module_spi_driver(ad7816_driver);
440
441 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
442 MODULE_DESCRIPTION("Analog Devices AD7816/7/8 digital temperature sensor driver");
443 MODULE_LICENSE("GPL v2");