]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/iio/adc/ad7291.c
Merge branch 'for-paul-38-rebased' of git://gitorious.org/linux-omap-dss2/linux
[mv-sheeva.git] / drivers / staging / iio / adc / ad7291.c
1 /*
2  * AD7291 digital temperature sensor driver supporting AD7291
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/workqueue.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/list.h>
17 #include <linux/i2c.h>
18 #include <linux/rtc.h>
19
20 #include "../iio.h"
21 #include "../sysfs.h"
22
23 /*
24  * AD7291 registers definition
25  */
26 #define AD7291_COMMAND                  0
27 #define AD7291_VOLTAGE                  1
28 #define AD7291_T_SENSE                  2
29 #define AD7291_T_AVERAGE                3
30 #define AD7291_VOLTAGE_LIMIT_BASE       4
31 #define AD7291_VOLTAGE_LIMIT_COUNT      8
32 #define AD7291_T_SENSE_HIGH             0x1c
33 #define AD7291_T_SENSE_LOW              0x1d
34 #define AD7291_T_SENSE_HYST             0x1e
35 #define AD7291_VOLTAGE_ALERT_STATUS     0x1f
36 #define AD7291_T_ALERT_STATUS           0x20
37
38 /*
39  * AD7291 command
40  */
41 #define AD7291_AUTOCYCLE                0x1
42 #define AD7291_RESET                    0x2
43 #define AD7291_ALART_CLEAR              0x4
44 #define AD7291_ALART_POLARITY           0x8
45 #define AD7291_EXT_REF                  0x10
46 #define AD7291_NOISE_DELAY              0x20
47 #define AD7291_T_SENSE_MASK             0x40
48 #define AD7291_VOLTAGE_MASK             0xff00
49 #define AD7291_VOLTAGE_OFFSET           0x8
50
51 /*
52  * AD7291 value masks
53  */
54 #define AD7291_CHANNEL_MASK             0xf000
55 #define AD7291_VALUE_MASK               0xfff
56 #define AD7291_T_VALUE_SIGN             0x400
57 #define AD7291_T_VALUE_FLOAT_OFFSET     2
58 #define AD7291_T_VALUE_FLOAT_MASK       0x2
59
60 /*
61  * struct ad7291_chip_info - chip specifc information
62  */
63
64 struct ad7291_chip_info {
65         const char *name;
66         struct i2c_client *client;
67         struct iio_dev *indio_dev;
68         struct work_struct thresh_work;
69         s64 last_timestamp;
70         u16 command;
71         u8  channels;   /* Active voltage channels */
72 };
73
74 /*
75  * struct ad7291_chip_info - chip specifc information
76  */
77
78 struct ad7291_limit_regs {
79         u16     data_high;
80         u16     data_low;
81         u16     hysteresis;
82 };
83
84 /*
85  * ad7291 register access by I2C
86  */
87 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
88 {
89         struct i2c_client *client = chip->client;
90         int ret = 0;
91
92         ret = i2c_smbus_read_word_data(client, reg);
93         if (ret < 0) {
94                 dev_err(&client->dev, "I2C read error\n");
95                 return ret;
96         }
97
98         *data = swab16((u16)ret);
99
100         return 0;
101 }
102
103 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
104 {
105         struct i2c_client *client = chip->client;
106         int ret = 0;
107
108         ret = i2c_smbus_write_word_data(client, reg, swab16(data));
109         if (ret < 0)
110                 dev_err(&client->dev, "I2C write error\n");
111
112         return ret;
113 }
114
115 /* Returns negative errno, or else the number of words read. */
116 static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data)
117 {
118         struct i2c_client *client = chip->client;
119         u8 commands[4];
120         int ret = 0;
121         int i, count;
122
123         if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE)
124                 count = 2;
125         else if (reg == AD7291_VOLTAGE) {
126                 if (!chip->channels) {
127                         dev_err(&client->dev, "No voltage channel is selected.\n");
128                         return -EINVAL;
129                 }
130                 count = 2 + chip->channels * 2;
131         } else {
132                 dev_err(&client->dev, "I2C wrong data register\n");
133                 return -EINVAL;
134         }
135
136         commands[0] = 0;
137         commands[1] = (chip->command >> 8) & 0xff;
138         commands[2] = chip->command & 0xff;
139         commands[3] = reg;
140
141         ret = i2c_master_send(client, commands, 4);
142         if (ret < 0) {
143                 dev_err(&client->dev, "I2C master send error\n");
144                 return ret;
145         }
146
147         ret = i2c_master_recv(client, (u8 *)data, count);
148         if (ret < 0) {
149                 dev_err(&client->dev, "I2C master receive error\n");
150                 return ret;
151         }
152         ret >>= 2;
153
154         for (i = 0; i < ret; i++)
155                 data[i] = swab16(data[i]);
156
157         return ret;
158 }
159
160 static ssize_t ad7291_show_mode(struct device *dev,
161                 struct device_attribute *attr,
162                 char *buf)
163 {
164         struct iio_dev *dev_info = dev_get_drvdata(dev);
165         struct ad7291_chip_info *chip = dev_info->dev_data;
166
167         if (chip->command & AD7291_AUTOCYCLE)
168                 return sprintf(buf, "autocycle\n");
169         else
170                 return sprintf(buf, "command\n");
171 }
172
173 static ssize_t ad7291_store_mode(struct device *dev,
174                 struct device_attribute *attr,
175                 const char *buf,
176                 size_t len)
177 {
178         struct iio_dev *dev_info = dev_get_drvdata(dev);
179         struct ad7291_chip_info *chip = dev_info->dev_data;
180         u16 command;
181         int ret;
182
183         command = chip->command & (~AD7291_AUTOCYCLE);
184         if (strcmp(buf, "autocycle"))
185                 command |= AD7291_AUTOCYCLE;
186
187         ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
188         if (ret)
189                 return -EIO;
190
191         chip->command = command;
192
193         return ret;
194 }
195
196 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
197                 ad7291_show_mode,
198                 ad7291_store_mode,
199                 0);
200
201 static ssize_t ad7291_show_available_modes(struct device *dev,
202                 struct device_attribute *attr,
203                 char *buf)
204 {
205         return sprintf(buf, "command\nautocycle\n");
206 }
207
208 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0);
209
210 static ssize_t ad7291_store_reset(struct device *dev,
211                 struct device_attribute *attr,
212                 const char *buf,
213                 size_t len)
214 {
215         struct iio_dev *dev_info = dev_get_drvdata(dev);
216         struct ad7291_chip_info *chip = dev_info->dev_data;
217         u16 command;
218         int ret;
219
220         command = chip->command | AD7291_RESET;
221
222         ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
223         if (ret)
224                 return -EIO;
225
226         return ret;
227 }
228
229 static IIO_DEVICE_ATTR(reset, S_IWUSR,
230                 NULL,
231                 ad7291_store_reset,
232                 0);
233
234 static ssize_t ad7291_show_ext_ref(struct device *dev,
235                 struct device_attribute *attr,
236                 char *buf)
237 {
238         struct iio_dev *dev_info = dev_get_drvdata(dev);
239         struct ad7291_chip_info *chip = dev_info->dev_data;
240
241         return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF));
242 }
243
244 static ssize_t ad7291_store_ext_ref(struct device *dev,
245                 struct device_attribute *attr,
246                 const char *buf,
247                 size_t len)
248 {
249         struct iio_dev *dev_info = dev_get_drvdata(dev);
250         struct ad7291_chip_info *chip = dev_info->dev_data;
251         u16 command;
252         int ret;
253
254         command = chip->command & (~AD7291_EXT_REF);
255         if (strcmp(buf, "1"))
256                 command |= AD7291_EXT_REF;
257
258         ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
259         if (ret)
260                 return -EIO;
261
262         chip->command = command;
263
264         return ret;
265 }
266
267 static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
268                 ad7291_show_ext_ref,
269                 ad7291_store_ext_ref,
270                 0);
271
272 static ssize_t ad7291_show_noise_delay(struct device *dev,
273                 struct device_attribute *attr,
274                 char *buf)
275 {
276         struct iio_dev *dev_info = dev_get_drvdata(dev);
277         struct ad7291_chip_info *chip = dev_info->dev_data;
278
279         return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY));
280 }
281
282 static ssize_t ad7291_store_noise_delay(struct device *dev,
283                 struct device_attribute *attr,
284                 const char *buf,
285                 size_t len)
286 {
287         struct iio_dev *dev_info = dev_get_drvdata(dev);
288         struct ad7291_chip_info *chip = dev_info->dev_data;
289         u16 command;
290         int ret;
291
292         command = chip->command & (~AD7291_NOISE_DELAY);
293         if (strcmp(buf, "1"))
294                 command |= AD7291_NOISE_DELAY;
295
296         ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
297         if (ret)
298                 return -EIO;
299
300         chip->command = command;
301
302         return ret;
303 }
304
305 static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR,
306                 ad7291_show_noise_delay,
307                 ad7291_store_noise_delay,
308                 0);
309
310 static ssize_t ad7291_show_t_sense(struct device *dev,
311                 struct device_attribute *attr,
312                 char *buf)
313 {
314         struct iio_dev *dev_info = dev_get_drvdata(dev);
315         struct ad7291_chip_info *chip = dev_info->dev_data;
316         u16 data;
317         char sign = ' ';
318         int ret;
319
320         ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data);
321         if (ret)
322                 return -EIO;
323
324         if (data & AD7291_T_VALUE_SIGN) {
325                 /* convert supplement to positive value */
326                 data = (AD7291_T_VALUE_SIGN << 1) - data;
327                 sign = '-';
328         }
329
330         return sprintf(buf, "%c%d.%.2d\n", sign,
331                 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
332                 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
333 }
334
335 static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0);
336
337 static ssize_t ad7291_show_t_average(struct device *dev,
338                 struct device_attribute *attr,
339                 char *buf)
340 {
341         struct iio_dev *dev_info = dev_get_drvdata(dev);
342         struct ad7291_chip_info *chip = dev_info->dev_data;
343         u16 data;
344         char sign = ' ';
345         int ret;
346
347         ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data);
348         if (ret)
349                 return -EIO;
350
351         if (data & AD7291_T_VALUE_SIGN) {
352                 /* convert supplement to positive value */
353                 data = (AD7291_T_VALUE_SIGN << 1) - data;
354                 sign = '-';
355         }
356
357         return sprintf(buf, "%c%d.%.2d\n", sign,
358                 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
359                 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
360 }
361
362 static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0);
363
364 static ssize_t ad7291_show_voltage(struct device *dev,
365                 struct device_attribute *attr,
366                 char *buf)
367 {
368         struct iio_dev *dev_info = dev_get_drvdata(dev);
369         struct ad7291_chip_info *chip = dev_info->dev_data;
370         u16 data[AD7291_VOLTAGE_LIMIT_COUNT];
371         int i, size, ret;
372
373         ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data);
374         if (ret)
375                 return -EIO;
376
377         for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
378                 if (chip->command & (AD7291_T_SENSE_MASK << i)) {
379                         ret = sprintf(buf, "channel[%d]=%d\n", i,
380                                         data[i] & AD7291_VALUE_MASK);
381                         if (ret < 0)
382                                 break;
383                         buf += ret;
384                         size += ret;
385                 }
386         }
387
388         return size;
389 }
390
391 static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0);
392
393 static ssize_t ad7291_show_channel_mask(struct device *dev,
394                 struct device_attribute *attr,
395                 char *buf)
396 {
397         struct iio_dev *dev_info = dev_get_drvdata(dev);
398         struct ad7291_chip_info *chip = dev_info->dev_data;
399
400         return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >>
401                         AD7291_VOLTAGE_OFFSET);
402 }
403
404 static ssize_t ad7291_store_channel_mask(struct device *dev,
405                 struct device_attribute *attr,
406                 const char *buf,
407                 size_t len)
408 {
409         struct iio_dev *dev_info = dev_get_drvdata(dev);
410         struct ad7291_chip_info *chip = dev_info->dev_data;
411         u16 command;
412         unsigned long data;
413         int i, ret;
414
415         ret = strict_strtoul(buf, 16, &data);
416         if (ret || data > 0xff)
417                 return -EINVAL;
418
419         command = chip->command & (~AD7291_VOLTAGE_MASK);
420         command |= data << AD7291_VOLTAGE_OFFSET;
421
422         ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
423         if (ret)
424                 return -EIO;
425
426         chip->command = command;
427
428         for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
429                 if (chip->command & (AD7291_T_SENSE_MASK << i))
430                         chip->channels++;
431         }
432
433         return ret;
434 }
435
436 static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
437                 ad7291_show_channel_mask,
438                 ad7291_store_channel_mask,
439                 0);
440
441 static ssize_t ad7291_show_name(struct device *dev,
442                 struct device_attribute *attr,
443                 char *buf)
444 {
445         struct iio_dev *dev_info = dev_get_drvdata(dev);
446         struct ad7291_chip_info *chip = dev_info->dev_data;
447         return sprintf(buf, "%s\n", chip->name);
448 }
449
450 static IIO_DEVICE_ATTR(name, S_IRUGO, ad7291_show_name, NULL, 0);
451
452 static struct attribute *ad7291_attributes[] = {
453         &iio_dev_attr_available_modes.dev_attr.attr,
454         &iio_dev_attr_mode.dev_attr.attr,
455         &iio_dev_attr_reset.dev_attr.attr,
456         &iio_dev_attr_ext_ref.dev_attr.attr,
457         &iio_dev_attr_noise_delay.dev_attr.attr,
458         &iio_dev_attr_t_sense.dev_attr.attr,
459         &iio_dev_attr_t_average.dev_attr.attr,
460         &iio_dev_attr_voltage.dev_attr.attr,
461         &iio_dev_attr_channel_mask.dev_attr.attr,
462         &iio_dev_attr_name.dev_attr.attr,
463         NULL,
464 };
465
466 static const struct attribute_group ad7291_attribute_group = {
467         .attrs = ad7291_attributes,
468 };
469
470 /*
471  * temperature bound events
472  */
473
474 #define IIO_EVENT_CODE_AD7291_T_SENSE_HIGH  IIO_BUFFER_EVENT_CODE(0)
475 #define IIO_EVENT_CODE_AD7291_T_SENSE_LOW   IIO_BUFFER_EVENT_CODE(1)
476 #define IIO_EVENT_CODE_AD7291_T_AVG_HIGH    IIO_BUFFER_EVENT_CODE(2)
477 #define IIO_EVENT_CODE_AD7291_T_AVG_LOW     IIO_BUFFER_EVENT_CODE(3)
478 #define IIO_EVENT_CODE_AD7291_VOLTAGE_BASE  IIO_BUFFER_EVENT_CODE(4)
479
480 static void ad7291_interrupt_bh(struct work_struct *work_s)
481 {
482         struct ad7291_chip_info *chip =
483                 container_of(work_s, struct ad7291_chip_info, thresh_work);
484         u16 t_status, v_status;
485         u16 command;
486         int i;
487
488         if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
489                 return;
490
491         if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
492                 return;
493
494         if (!(t_status || v_status))
495                 return;
496
497         command = chip->command | AD7291_ALART_CLEAR;
498         ad7291_i2c_write(chip, AD7291_COMMAND, command);
499
500         command = chip->command & ~AD7291_ALART_CLEAR;
501         ad7291_i2c_write(chip, AD7291_COMMAND, command);
502
503         enable_irq(chip->client->irq);
504
505         for (i = 0; i < 4; i++) {
506                 if (t_status & (1 << i))
507                         iio_push_event(chip->indio_dev, 0,
508                                 IIO_EVENT_CODE_AD7291_T_SENSE_HIGH + i,
509                                 chip->last_timestamp);
510         }
511
512         for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i++) {
513                 if (v_status & (1 << i))
514                         iio_push_event(chip->indio_dev, 0,
515                                 IIO_EVENT_CODE_AD7291_VOLTAGE_BASE + i,
516                                 chip->last_timestamp);
517         }
518 }
519
520 static int ad7291_interrupt(struct iio_dev *dev_info,
521                 int index,
522                 s64 timestamp,
523                 int no_test)
524 {
525         struct ad7291_chip_info *chip = dev_info->dev_data;
526
527         chip->last_timestamp = timestamp;
528         schedule_work(&chip->thresh_work);
529
530         return 0;
531 }
532
533 IIO_EVENT_SH(ad7291, &ad7291_interrupt);
534
535 static inline ssize_t ad7291_show_t_bound(struct device *dev,
536                 struct device_attribute *attr,
537                 u8 bound_reg,
538                 char *buf)
539 {
540         struct iio_dev *dev_info = dev_get_drvdata(dev);
541         struct ad7291_chip_info *chip = dev_info->dev_data;
542         u16 data;
543         char sign = ' ';
544         int ret;
545
546         ret = ad7291_i2c_read(chip, bound_reg, &data);
547         if (ret)
548                 return -EIO;
549
550         data &= AD7291_VALUE_MASK;
551         if (data & AD7291_T_VALUE_SIGN) {
552                 /* convert supplement to positive value */
553                 data = (AD7291_T_VALUE_SIGN << 1) - data;
554                 sign = '-';
555         }
556
557         return sprintf(buf, "%c%d.%.2d\n", sign,
558                         data >> AD7291_T_VALUE_FLOAT_OFFSET,
559                         (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
560 }
561
562 static inline ssize_t ad7291_set_t_bound(struct device *dev,
563                 struct device_attribute *attr,
564                 u8 bound_reg,
565                 const char *buf,
566                 size_t len)
567 {
568         struct iio_dev *dev_info = dev_get_drvdata(dev);
569         struct ad7291_chip_info *chip = dev_info->dev_data;
570         long tmp1, tmp2;
571         u16 data;
572         char *pos;
573         int ret;
574
575         pos = strchr(buf, '.');
576
577         ret = strict_strtol(buf, 10, &tmp1);
578
579         if (ret || tmp1 > 127 || tmp1 < -128)
580                 return -EINVAL;
581
582         if (pos) {
583                 len = strlen(pos);
584                 if (len > AD7291_T_VALUE_FLOAT_OFFSET)
585                         len = AD7291_T_VALUE_FLOAT_OFFSET;
586                 pos[len] = 0;
587                 ret = strict_strtol(pos, 10, &tmp2);
588
589                 if (!ret)
590                         tmp2 = (tmp2 / 25) * 25;
591         }
592
593         if (tmp1 < 0)
594                 data = (u16)(-tmp1);
595         else
596                 data = (u16)tmp1;
597         data = (data << AD7291_T_VALUE_FLOAT_OFFSET) |
598                 (tmp2 & AD7291_T_VALUE_FLOAT_MASK);
599         if (tmp1 < 0)
600                 /* convert positive value to supplyment */
601                 data = (AD7291_T_VALUE_SIGN << 1) - data;
602
603         ret = ad7291_i2c_write(chip, bound_reg, data);
604         if (ret)
605                 return -EIO;
606
607         return ret;
608 }
609
610 static ssize_t ad7291_show_t_sense_high(struct device *dev,
611                 struct device_attribute *attr,
612                 char *buf)
613 {
614         return ad7291_show_t_bound(dev, attr,
615                         AD7291_T_SENSE_HIGH, buf);
616 }
617
618 static inline ssize_t ad7291_set_t_sense_high(struct device *dev,
619                 struct device_attribute *attr,
620                 const char *buf,
621                 size_t len)
622 {
623         return ad7291_set_t_bound(dev, attr,
624                         AD7291_T_SENSE_HIGH, buf, len);
625 }
626
627 static ssize_t ad7291_show_t_sense_low(struct device *dev,
628                 struct device_attribute *attr,
629                 char *buf)
630 {
631         return ad7291_show_t_bound(dev, attr,
632                         AD7291_T_SENSE_LOW, buf);
633 }
634
635 static inline ssize_t ad7291_set_t_sense_low(struct device *dev,
636                 struct device_attribute *attr,
637                 const char *buf,
638                 size_t len)
639 {
640         return ad7291_set_t_bound(dev, attr,
641                         AD7291_T_SENSE_LOW, buf, len);
642 }
643
644 static ssize_t ad7291_show_t_sense_hyst(struct device *dev,
645                 struct device_attribute *attr,
646                 char *buf)
647 {
648         return ad7291_show_t_bound(dev, attr,
649                         AD7291_T_SENSE_HYST, buf);
650 }
651
652 static inline ssize_t ad7291_set_t_sense_hyst(struct device *dev,
653                 struct device_attribute *attr,
654                 const char *buf,
655                 size_t len)
656 {
657         return ad7291_set_t_bound(dev, attr,
658                         AD7291_T_SENSE_HYST, buf, len);
659 }
660
661 static inline ssize_t ad7291_show_v_bound(struct device *dev,
662                 struct device_attribute *attr,
663                 u8 bound_reg,
664                 char *buf)
665 {
666         struct iio_dev *dev_info = dev_get_drvdata(dev);
667         struct ad7291_chip_info *chip = dev_info->dev_data;
668         u16 data;
669         int ret;
670
671         if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
672                 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
673                 AD7291_VOLTAGE_LIMIT_COUNT)
674                 return -EINVAL;
675
676         ret = ad7291_i2c_read(chip, bound_reg, &data);
677         if (ret)
678                 return -EIO;
679
680         data &= AD7291_VALUE_MASK;
681
682         return sprintf(buf, "%d\n", data);
683 }
684
685 static inline ssize_t ad7291_set_v_bound(struct device *dev,
686                 struct device_attribute *attr,
687                 u8 bound_reg,
688                 const char *buf,
689                 size_t len)
690 {
691         struct iio_dev *dev_info = dev_get_drvdata(dev);
692         struct ad7291_chip_info *chip = dev_info->dev_data;
693         unsigned long value;
694         u16 data;
695         int ret;
696
697         if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
698                 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
699                 AD7291_VOLTAGE_LIMIT_COUNT)
700                 return -EINVAL;
701
702         ret = strict_strtoul(buf, 10, &value);
703
704         if (ret || value >= 4096)
705                 return -EINVAL;
706
707         data = (u16)value;
708         ret = ad7291_i2c_write(chip, bound_reg, data);
709         if (ret)
710                 return -EIO;
711
712         return ret;
713 }
714
715 static int ad7291_get_voltage_limit_regs(const char *channel)
716 {
717         int index;
718
719         if (strlen(channel) < 3 && channel[0] != 'v')
720                 return -EINVAL;
721
722         index = channel[1] - '0';
723         if (index >= AD7291_VOLTAGE_LIMIT_COUNT)
724                 return -EINVAL;
725
726         return index;
727 }
728
729 static ssize_t ad7291_show_voltage_high(struct device *dev,
730                 struct device_attribute *attr,
731                 char *buf)
732 {
733         int regs;
734
735         regs = ad7291_get_voltage_limit_regs(attr->attr.name);
736
737         if (regs < 0)
738                 return regs;
739
740         return ad7291_show_t_bound(dev, attr, regs, buf);
741 }
742
743 static inline ssize_t ad7291_set_voltage_high(struct device *dev,
744                 struct device_attribute *attr,
745                 const char *buf,
746                 size_t len)
747 {
748         int regs;
749
750         regs = ad7291_get_voltage_limit_regs(attr->attr.name);
751
752         if (regs < 0)
753                 return regs;
754
755         return ad7291_set_t_bound(dev, attr, regs, buf, len);
756 }
757
758 static ssize_t ad7291_show_voltage_low(struct device *dev,
759                 struct device_attribute *attr,
760                 char *buf)
761 {
762         int regs;
763
764         regs = ad7291_get_voltage_limit_regs(attr->attr.name);
765
766         if (regs < 0)
767                 return regs;
768
769         return ad7291_show_t_bound(dev, attr, regs+1, buf);
770 }
771
772 static inline ssize_t ad7291_set_voltage_low(struct device *dev,
773                 struct device_attribute *attr,
774                 const char *buf,
775                 size_t len)
776 {
777         int regs;
778
779         regs = ad7291_get_voltage_limit_regs(attr->attr.name);
780
781         if (regs < 0)
782                 return regs;
783
784         return ad7291_set_t_bound(dev, attr, regs+1, buf, len);
785 }
786
787 static ssize_t ad7291_show_voltage_hyst(struct device *dev,
788                 struct device_attribute *attr,
789                 char *buf)
790 {
791         int regs;
792
793         regs = ad7291_get_voltage_limit_regs(attr->attr.name);
794
795         if (regs < 0)
796                 return regs;
797
798         return ad7291_show_t_bound(dev, attr, regs+2, buf);
799 }
800
801 static inline ssize_t ad7291_set_voltage_hyst(struct device *dev,
802                 struct device_attribute *attr,
803                 const char *buf,
804                 size_t len)
805 {
806         int regs;
807
808         regs = ad7291_get_voltage_limit_regs(attr->attr.name);
809
810         if (regs < 0)
811                 return regs;
812
813         return ad7291_set_t_bound(dev, attr, regs+2, buf, len);
814 }
815
816 IIO_EVENT_ATTR_SH(t_sense_high, iio_event_ad7291,
817                 ad7291_show_t_sense_high, ad7291_set_t_sense_high, 0);
818 IIO_EVENT_ATTR_SH(t_sense_low, iio_event_ad7291,
819                 ad7291_show_t_sense_low, ad7291_set_t_sense_low, 0);
820 IIO_EVENT_ATTR_SH(t_sense_hyst, iio_event_ad7291,
821                 ad7291_show_t_sense_hyst, ad7291_set_t_sense_hyst, 0);
822
823 IIO_EVENT_ATTR_SH(v0_high, iio_event_ad7291,
824                 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
825 IIO_EVENT_ATTR_SH(v0_low, iio_event_ad7291,
826                 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
827 IIO_EVENT_ATTR_SH(v0_hyst, iio_event_ad7291,
828                 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
829 IIO_EVENT_ATTR_SH(v1_high, iio_event_ad7291,
830                 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
831 IIO_EVENT_ATTR_SH(v1_low, iio_event_ad7291,
832                 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
833 IIO_EVENT_ATTR_SH(v1_hyst, iio_event_ad7291,
834                 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
835 IIO_EVENT_ATTR_SH(v2_high, iio_event_ad7291,
836                 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
837 IIO_EVENT_ATTR_SH(v2_low, iio_event_ad7291,
838                 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
839 IIO_EVENT_ATTR_SH(v2_hyst, iio_event_ad7291,
840                 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
841 IIO_EVENT_ATTR_SH(v3_high, iio_event_ad7291,
842                 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
843 IIO_EVENT_ATTR_SH(v3_low, iio_event_ad7291,
844                 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
845 IIO_EVENT_ATTR_SH(v3_hyst, iio_event_ad7291,
846                 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
847 IIO_EVENT_ATTR_SH(v4_high, iio_event_ad7291,
848                 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
849 IIO_EVENT_ATTR_SH(v4_low, iio_event_ad7291,
850                 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
851 IIO_EVENT_ATTR_SH(v4_hyst, iio_event_ad7291,
852                 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
853 IIO_EVENT_ATTR_SH(v5_high, iio_event_ad7291,
854                 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
855 IIO_EVENT_ATTR_SH(v5_low, iio_event_ad7291,
856                 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
857 IIO_EVENT_ATTR_SH(v5_hyst, iio_event_ad7291,
858                 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
859 IIO_EVENT_ATTR_SH(v6_high, iio_event_ad7291,
860                 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
861 IIO_EVENT_ATTR_SH(v6_low, iio_event_ad7291,
862                 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
863 IIO_EVENT_ATTR_SH(v6_hyst, iio_event_ad7291,
864                 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
865 IIO_EVENT_ATTR_SH(v7_high, iio_event_ad7291,
866                 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
867 IIO_EVENT_ATTR_SH(v7_low, iio_event_ad7291,
868                 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
869 IIO_EVENT_ATTR_SH(v7_hyst, iio_event_ad7291,
870                 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
871
872 static struct attribute *ad7291_event_attributes[] = {
873         &iio_event_attr_t_sense_high.dev_attr.attr,
874         &iio_event_attr_t_sense_low.dev_attr.attr,
875         &iio_event_attr_t_sense_hyst.dev_attr.attr,
876         &iio_event_attr_v0_high.dev_attr.attr,
877         &iio_event_attr_v0_low.dev_attr.attr,
878         &iio_event_attr_v0_hyst.dev_attr.attr,
879         &iio_event_attr_v1_high.dev_attr.attr,
880         &iio_event_attr_v1_low.dev_attr.attr,
881         &iio_event_attr_v1_hyst.dev_attr.attr,
882         &iio_event_attr_v2_high.dev_attr.attr,
883         &iio_event_attr_v2_low.dev_attr.attr,
884         &iio_event_attr_v2_hyst.dev_attr.attr,
885         &iio_event_attr_v3_high.dev_attr.attr,
886         &iio_event_attr_v3_low.dev_attr.attr,
887         &iio_event_attr_v3_hyst.dev_attr.attr,
888         &iio_event_attr_v4_high.dev_attr.attr,
889         &iio_event_attr_v4_low.dev_attr.attr,
890         &iio_event_attr_v4_hyst.dev_attr.attr,
891         &iio_event_attr_v5_high.dev_attr.attr,
892         &iio_event_attr_v5_low.dev_attr.attr,
893         &iio_event_attr_v5_hyst.dev_attr.attr,
894         &iio_event_attr_v6_high.dev_attr.attr,
895         &iio_event_attr_v6_low.dev_attr.attr,
896         &iio_event_attr_v6_hyst.dev_attr.attr,
897         &iio_event_attr_v7_high.dev_attr.attr,
898         &iio_event_attr_v7_low.dev_attr.attr,
899         &iio_event_attr_v7_hyst.dev_attr.attr,
900         NULL,
901 };
902
903 static struct attribute_group ad7291_event_attribute_group = {
904         .attrs = ad7291_event_attributes,
905 };
906
907 /*
908  * device probe and remove
909  */
910
911 static int __devinit ad7291_probe(struct i2c_client *client,
912                 const struct i2c_device_id *id)
913 {
914         struct ad7291_chip_info *chip;
915         int ret = 0;
916
917         chip = kzalloc(sizeof(struct ad7291_chip_info), GFP_KERNEL);
918
919         if (chip == NULL)
920                 return -ENOMEM;
921
922         /* this is only used for device removal purposes */
923         i2c_set_clientdata(client, chip);
924
925         chip->client = client;
926         chip->name = id->name;
927         chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
928
929         chip->indio_dev = iio_allocate_device();
930         if (chip->indio_dev == NULL) {
931                 ret = -ENOMEM;
932                 goto error_free_chip;
933         }
934
935         chip->indio_dev->dev.parent = &client->dev;
936         chip->indio_dev->attrs = &ad7291_attribute_group;
937         chip->indio_dev->event_attrs = &ad7291_event_attribute_group;
938         chip->indio_dev->dev_data = (void *)chip;
939         chip->indio_dev->driver_module = THIS_MODULE;
940         chip->indio_dev->num_interrupt_lines = 1;
941         chip->indio_dev->modes = INDIO_DIRECT_MODE;
942
943         ret = iio_device_register(chip->indio_dev);
944         if (ret)
945                 goto error_free_dev;
946
947         if (client->irq > 0) {
948                 ret = iio_register_interrupt_line(client->irq,
949                                 chip->indio_dev,
950                                 0,
951                                 IRQF_TRIGGER_LOW,
952                                 chip->name);
953                 if (ret)
954                         goto error_unreg_dev;
955
956                 /*
957                  * The event handler list element refer to iio_event_ad7291.
958                  * All event attributes bind to the same event handler.
959                  * So, only register event handler once.
960                  */
961                 iio_add_event_to_list(&iio_event_ad7291,
962                                 &chip->indio_dev->interrupts[0]->ev_list);
963
964                 INIT_WORK(&chip->thresh_work, ad7291_interrupt_bh);
965
966                 /* set irq polarity low level */
967                 chip->command |= AD7291_ALART_POLARITY;
968         }
969
970         ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
971         if (ret) {
972                 ret = -EIO;
973                 goto error_unreg_irq;
974         }
975
976         dev_info(&client->dev, "%s temperature sensor registered.\n",
977                          id->name);
978
979         return 0;
980
981 error_unreg_irq:
982         iio_unregister_interrupt_line(chip->indio_dev, 0);
983 error_unreg_dev:
984         iio_device_unregister(chip->indio_dev);
985 error_free_dev:
986         iio_free_device(chip->indio_dev);
987 error_free_chip:
988         kfree(chip);
989
990         return ret;
991 }
992
993 static int __devexit ad7291_remove(struct i2c_client *client)
994 {
995         struct ad7291_chip_info *chip = i2c_get_clientdata(client);
996         struct iio_dev *indio_dev = chip->indio_dev;
997
998         if (client->irq)
999                 iio_unregister_interrupt_line(indio_dev, 0);
1000         iio_device_unregister(indio_dev);
1001         iio_free_device(chip->indio_dev);
1002         kfree(chip);
1003
1004         return 0;
1005 }
1006
1007 static const struct i2c_device_id ad7291_id[] = {
1008         { "ad7291", 0 },
1009         {}
1010 };
1011
1012 MODULE_DEVICE_TABLE(i2c, ad7291_id);
1013
1014 static struct i2c_driver ad7291_driver = {
1015         .driver = {
1016                 .name = "ad7291",
1017         },
1018         .probe = ad7291_probe,
1019         .remove = __devexit_p(ad7291_remove),
1020         .id_table = ad7291_id,
1021 };
1022
1023 static __init int ad7291_init(void)
1024 {
1025         return i2c_add_driver(&ad7291_driver);
1026 }
1027
1028 static __exit void ad7291_exit(void)
1029 {
1030         i2c_del_driver(&ad7291_driver);
1031 }
1032
1033 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
1034 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
1035                         " temperature sensor driver");
1036 MODULE_LICENSE("GPL v2");
1037
1038 module_init(ad7291_init);
1039 module_exit(ad7291_exit);