blob: 1be3453479b75e333343efbaafc2ba90377669ae [file] [log] [blame]
Sonic Zhangddaecd52010-10-27 21:43:55 -04001/*
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>
Sonic Zhangddaecd52010-10-27 21:43:55 -040011#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/i2c.h>
Sonic Zhangddaecd52010-10-27 21:43:55 -040017
18#include "../iio.h"
19#include "../sysfs.h"
20
21/*
22 * AD7291 registers definition
23 */
24#define AD7291_COMMAND 0
25#define AD7291_VOLTAGE 1
26#define AD7291_T_SENSE 2
27#define AD7291_T_AVERAGE 3
28#define AD7291_VOLTAGE_LIMIT_BASE 4
29#define AD7291_VOLTAGE_LIMIT_COUNT 8
30#define AD7291_T_SENSE_HIGH 0x1c
31#define AD7291_T_SENSE_LOW 0x1d
32#define AD7291_T_SENSE_HYST 0x1e
33#define AD7291_VOLTAGE_ALERT_STATUS 0x1f
34#define AD7291_T_ALERT_STATUS 0x20
35
36/*
37 * AD7291 command
38 */
39#define AD7291_AUTOCYCLE 0x1
40#define AD7291_RESET 0x2
41#define AD7291_ALART_CLEAR 0x4
42#define AD7291_ALART_POLARITY 0x8
43#define AD7291_EXT_REF 0x10
44#define AD7291_NOISE_DELAY 0x20
45#define AD7291_T_SENSE_MASK 0x40
46#define AD7291_VOLTAGE_MASK 0xff00
47#define AD7291_VOLTAGE_OFFSET 0x8
48
49/*
50 * AD7291 value masks
51 */
52#define AD7291_CHANNEL_MASK 0xf000
53#define AD7291_VALUE_MASK 0xfff
54#define AD7291_T_VALUE_SIGN 0x400
55#define AD7291_T_VALUE_FLOAT_OFFSET 2
56#define AD7291_T_VALUE_FLOAT_MASK 0x2
57
58/*
59 * struct ad7291_chip_info - chip specifc information
60 */
61
62struct ad7291_chip_info {
Sonic Zhangddaecd52010-10-27 21:43:55 -040063 struct i2c_client *client;
64 struct iio_dev *indio_dev;
Sonic Zhangddaecd52010-10-27 21:43:55 -040065 u16 command;
66 u8 channels; /* Active voltage channels */
67};
68
69/*
70 * struct ad7291_chip_info - chip specifc information
71 */
72
73struct ad7291_limit_regs {
74 u16 data_high;
75 u16 data_low;
76 u16 hysteresis;
77};
78
79/*
80 * ad7291 register access by I2C
81 */
82static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
83{
84 struct i2c_client *client = chip->client;
85 int ret = 0;
86
87 ret = i2c_smbus_read_word_data(client, reg);
88 if (ret < 0) {
89 dev_err(&client->dev, "I2C read error\n");
90 return ret;
91 }
92
93 *data = swab16((u16)ret);
94
95 return 0;
96}
97
98static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
99{
100 struct i2c_client *client = chip->client;
101 int ret = 0;
102
103 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
104 if (ret < 0)
105 dev_err(&client->dev, "I2C write error\n");
106
107 return ret;
108}
109
110/* Returns negative errno, or else the number of words read. */
111static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data)
112{
113 struct i2c_client *client = chip->client;
114 u8 commands[4];
115 int ret = 0;
116 int i, count;
117
118 if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE)
119 count = 2;
120 else if (reg == AD7291_VOLTAGE) {
121 if (!chip->channels) {
122 dev_err(&client->dev, "No voltage channel is selected.\n");
123 return -EINVAL;
124 }
125 count = 2 + chip->channels * 2;
126 } else {
127 dev_err(&client->dev, "I2C wrong data register\n");
128 return -EINVAL;
129 }
130
131 commands[0] = 0;
132 commands[1] = (chip->command >> 8) & 0xff;
133 commands[2] = chip->command & 0xff;
134 commands[3] = reg;
135
136 ret = i2c_master_send(client, commands, 4);
137 if (ret < 0) {
138 dev_err(&client->dev, "I2C master send error\n");
139 return ret;
140 }
141
142 ret = i2c_master_recv(client, (u8 *)data, count);
143 if (ret < 0) {
144 dev_err(&client->dev, "I2C master receive error\n");
145 return ret;
146 }
147 ret >>= 2;
148
149 for (i = 0; i < ret; i++)
150 data[i] = swab16(data[i]);
151
152 return ret;
153}
154
155static ssize_t ad7291_show_mode(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
158{
159 struct iio_dev *dev_info = dev_get_drvdata(dev);
160 struct ad7291_chip_info *chip = dev_info->dev_data;
161
162 if (chip->command & AD7291_AUTOCYCLE)
163 return sprintf(buf, "autocycle\n");
164 else
165 return sprintf(buf, "command\n");
166}
167
168static ssize_t ad7291_store_mode(struct device *dev,
169 struct device_attribute *attr,
170 const char *buf,
171 size_t len)
172{
173 struct iio_dev *dev_info = dev_get_drvdata(dev);
174 struct ad7291_chip_info *chip = dev_info->dev_data;
175 u16 command;
176 int ret;
177
178 command = chip->command & (~AD7291_AUTOCYCLE);
179 if (strcmp(buf, "autocycle"))
180 command |= AD7291_AUTOCYCLE;
181
182 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
183 if (ret)
184 return -EIO;
185
186 chip->command = command;
187
188 return ret;
189}
190
191static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
192 ad7291_show_mode,
193 ad7291_store_mode,
194 0);
195
196static ssize_t ad7291_show_available_modes(struct device *dev,
197 struct device_attribute *attr,
198 char *buf)
199{
200 return sprintf(buf, "command\nautocycle\n");
201}
202
203static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0);
204
205static ssize_t ad7291_store_reset(struct device *dev,
206 struct device_attribute *attr,
207 const char *buf,
208 size_t len)
209{
210 struct iio_dev *dev_info = dev_get_drvdata(dev);
211 struct ad7291_chip_info *chip = dev_info->dev_data;
212 u16 command;
213 int ret;
214
215 command = chip->command | AD7291_RESET;
216
217 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
218 if (ret)
219 return -EIO;
220
221 return ret;
222}
223
224static IIO_DEVICE_ATTR(reset, S_IWUSR,
225 NULL,
226 ad7291_store_reset,
227 0);
228
229static ssize_t ad7291_show_ext_ref(struct device *dev,
230 struct device_attribute *attr,
231 char *buf)
232{
233 struct iio_dev *dev_info = dev_get_drvdata(dev);
234 struct ad7291_chip_info *chip = dev_info->dev_data;
235
236 return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF));
237}
238
239static ssize_t ad7291_store_ext_ref(struct device *dev,
240 struct device_attribute *attr,
241 const char *buf,
242 size_t len)
243{
244 struct iio_dev *dev_info = dev_get_drvdata(dev);
245 struct ad7291_chip_info *chip = dev_info->dev_data;
246 u16 command;
247 int ret;
248
249 command = chip->command & (~AD7291_EXT_REF);
250 if (strcmp(buf, "1"))
251 command |= AD7291_EXT_REF;
252
253 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
254 if (ret)
255 return -EIO;
256
257 chip->command = command;
258
259 return ret;
260}
261
262static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
263 ad7291_show_ext_ref,
264 ad7291_store_ext_ref,
265 0);
266
267static ssize_t ad7291_show_noise_delay(struct device *dev,
268 struct device_attribute *attr,
269 char *buf)
270{
271 struct iio_dev *dev_info = dev_get_drvdata(dev);
272 struct ad7291_chip_info *chip = dev_info->dev_data;
273
274 return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY));
275}
276
277static ssize_t ad7291_store_noise_delay(struct device *dev,
278 struct device_attribute *attr,
279 const char *buf,
280 size_t len)
281{
282 struct iio_dev *dev_info = dev_get_drvdata(dev);
283 struct ad7291_chip_info *chip = dev_info->dev_data;
284 u16 command;
285 int ret;
286
287 command = chip->command & (~AD7291_NOISE_DELAY);
288 if (strcmp(buf, "1"))
289 command |= AD7291_NOISE_DELAY;
290
291 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
292 if (ret)
293 return -EIO;
294
295 chip->command = command;
296
297 return ret;
298}
299
300static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR,
301 ad7291_show_noise_delay,
302 ad7291_store_noise_delay,
303 0);
304
305static ssize_t ad7291_show_t_sense(struct device *dev,
306 struct device_attribute *attr,
307 char *buf)
308{
309 struct iio_dev *dev_info = dev_get_drvdata(dev);
310 struct ad7291_chip_info *chip = dev_info->dev_data;
311 u16 data;
312 char sign = ' ';
313 int ret;
314
315 ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data);
316 if (ret)
317 return -EIO;
318
319 if (data & AD7291_T_VALUE_SIGN) {
320 /* convert supplement to positive value */
321 data = (AD7291_T_VALUE_SIGN << 1) - data;
322 sign = '-';
323 }
324
325 return sprintf(buf, "%c%d.%.2d\n", sign,
326 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
327 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
328}
329
330static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0);
331
332static ssize_t ad7291_show_t_average(struct device *dev,
333 struct device_attribute *attr,
334 char *buf)
335{
336 struct iio_dev *dev_info = dev_get_drvdata(dev);
337 struct ad7291_chip_info *chip = dev_info->dev_data;
338 u16 data;
339 char sign = ' ';
340 int ret;
341
342 ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data);
343 if (ret)
344 return -EIO;
345
346 if (data & AD7291_T_VALUE_SIGN) {
347 /* convert supplement to positive value */
348 data = (AD7291_T_VALUE_SIGN << 1) - data;
349 sign = '-';
350 }
351
352 return sprintf(buf, "%c%d.%.2d\n", sign,
353 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
354 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
355}
356
357static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0);
358
359static ssize_t ad7291_show_voltage(struct device *dev,
360 struct device_attribute *attr,
361 char *buf)
362{
363 struct iio_dev *dev_info = dev_get_drvdata(dev);
364 struct ad7291_chip_info *chip = dev_info->dev_data;
365 u16 data[AD7291_VOLTAGE_LIMIT_COUNT];
366 int i, size, ret;
367
368 ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data);
369 if (ret)
370 return -EIO;
371
372 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
373 if (chip->command & (AD7291_T_SENSE_MASK << i)) {
374 ret = sprintf(buf, "channel[%d]=%d\n", i,
375 data[i] & AD7291_VALUE_MASK);
376 if (ret < 0)
377 break;
378 buf += ret;
379 size += ret;
380 }
381 }
382
383 return size;
384}
385
386static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0);
387
388static ssize_t ad7291_show_channel_mask(struct device *dev,
389 struct device_attribute *attr,
390 char *buf)
391{
392 struct iio_dev *dev_info = dev_get_drvdata(dev);
393 struct ad7291_chip_info *chip = dev_info->dev_data;
394
395 return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >>
396 AD7291_VOLTAGE_OFFSET);
397}
398
399static ssize_t ad7291_store_channel_mask(struct device *dev,
400 struct device_attribute *attr,
401 const char *buf,
402 size_t len)
403{
404 struct iio_dev *dev_info = dev_get_drvdata(dev);
405 struct ad7291_chip_info *chip = dev_info->dev_data;
406 u16 command;
407 unsigned long data;
408 int i, ret;
409
410 ret = strict_strtoul(buf, 16, &data);
411 if (ret || data > 0xff)
412 return -EINVAL;
413
414 command = chip->command & (~AD7291_VOLTAGE_MASK);
415 command |= data << AD7291_VOLTAGE_OFFSET;
416
417 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
418 if (ret)
419 return -EIO;
420
421 chip->command = command;
422
423 for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
424 if (chip->command & (AD7291_T_SENSE_MASK << i))
425 chip->channels++;
426 }
427
428 return ret;
429}
430
431static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
432 ad7291_show_channel_mask,
433 ad7291_store_channel_mask,
434 0);
435
Sonic Zhangddaecd52010-10-27 21:43:55 -0400436static struct attribute *ad7291_attributes[] = {
437 &iio_dev_attr_available_modes.dev_attr.attr,
438 &iio_dev_attr_mode.dev_attr.attr,
439 &iio_dev_attr_reset.dev_attr.attr,
440 &iio_dev_attr_ext_ref.dev_attr.attr,
441 &iio_dev_attr_noise_delay.dev_attr.attr,
442 &iio_dev_attr_t_sense.dev_attr.attr,
443 &iio_dev_attr_t_average.dev_attr.attr,
444 &iio_dev_attr_voltage.dev_attr.attr,
445 &iio_dev_attr_channel_mask.dev_attr.attr,
Sonic Zhangddaecd52010-10-27 21:43:55 -0400446 NULL,
447};
448
449static const struct attribute_group ad7291_attribute_group = {
450 .attrs = ad7291_attributes,
451};
452
453/*
454 * temperature bound events
455 */
456
Jonathan Cameron58c03232011-05-18 14:41:11 +0100457static irqreturn_t ad7291_event_handler(int irq, void *private)
Sonic Zhangddaecd52010-10-27 21:43:55 -0400458{
Jonathan Cameron58c03232011-05-18 14:41:11 +0100459 struct iio_dev *indio_dev = private;
460 struct ad7291_chip_info *chip = iio_dev_get_devdata(private);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400461 u16 t_status, v_status;
462 u16 command;
463 int i;
Jonathan Cameron58c03232011-05-18 14:41:11 +0100464 s64 timestamp = iio_get_time_ns();
Sonic Zhangddaecd52010-10-27 21:43:55 -0400465
466 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
Jonathan Cameron58c03232011-05-18 14:41:11 +0100467 return IRQ_HANDLED;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400468
469 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
Jonathan Cameron58c03232011-05-18 14:41:11 +0100470 return IRQ_HANDLED;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400471
472 if (!(t_status || v_status))
Jonathan Cameron58c03232011-05-18 14:41:11 +0100473 return IRQ_HANDLED;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400474
475 command = chip->command | AD7291_ALART_CLEAR;
476 ad7291_i2c_write(chip, AD7291_COMMAND, command);
477
478 command = chip->command & ~AD7291_ALART_CLEAR;
479 ad7291_i2c_write(chip, AD7291_COMMAND, command);
480
Jonathan Cameronb206c3b2011-05-18 14:42:15 +0100481 if (t_status & (1 << 0))
482 iio_push_event(indio_dev, 0,
483 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
484 0,
485 IIO_EV_TYPE_THRESH,
486 IIO_EV_DIR_FALLING),
487 timestamp);
488 if (t_status & (1 << 1))
489 iio_push_event(indio_dev, 0,
490 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
491 0,
492 IIO_EV_TYPE_THRESH,
493 IIO_EV_DIR_RISING),
494 timestamp);
495 if (t_status & (1 << 2))
496 iio_push_event(indio_dev, 0,
497 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
498 0,
499 IIO_EV_TYPE_THRESH,
500 IIO_EV_DIR_FALLING),
501 timestamp);
502 if (t_status & (1 << 3))
503 iio_push_event(indio_dev, 0,
504 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
505 0,
506 IIO_EV_TYPE_THRESH,
507 IIO_EV_DIR_RISING),
508 timestamp);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400509
Jonathan Cameronb206c3b2011-05-18 14:42:15 +0100510 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
Sonic Zhangddaecd52010-10-27 21:43:55 -0400511 if (v_status & (1 << i))
Jonathan Cameron58c03232011-05-18 14:41:11 +0100512 iio_push_event(indio_dev, 0,
Jonathan Cameronb206c3b2011-05-18 14:42:15 +0100513 IIO_UNMOD_EVENT_CODE(IIO_IN,
514 i/2,
515 IIO_EV_TYPE_THRESH,
516 IIO_EV_DIR_FALLING),
517 timestamp);
518 if (v_status & (1 << (i + 1)))
519 iio_push_event(indio_dev, 0,
520 IIO_UNMOD_EVENT_CODE(IIO_IN,
521 i/2,
522 IIO_EV_TYPE_THRESH,
523 IIO_EV_DIR_RISING),
524 timestamp);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400525 }
Jonathan Cameron58c03232011-05-18 14:41:11 +0100526
527 return IRQ_HANDLED;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400528}
529
Sonic Zhangddaecd52010-10-27 21:43:55 -0400530static inline ssize_t ad7291_show_t_bound(struct device *dev,
531 struct device_attribute *attr,
Sonic Zhangddaecd52010-10-27 21:43:55 -0400532 char *buf)
533{
534 struct iio_dev *dev_info = dev_get_drvdata(dev);
535 struct ad7291_chip_info *chip = dev_info->dev_data;
Jonathan Cameron58c03232011-05-18 14:41:11 +0100536 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400537 u16 data;
538 char sign = ' ';
539 int ret;
540
Jonathan Cameron58c03232011-05-18 14:41:11 +0100541 ret = ad7291_i2c_read(chip, this_attr->address, &data);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400542 if (ret)
543 return -EIO;
544
545 data &= AD7291_VALUE_MASK;
546 if (data & AD7291_T_VALUE_SIGN) {
547 /* convert supplement to positive value */
548 data = (AD7291_T_VALUE_SIGN << 1) - data;
549 sign = '-';
550 }
551
552 return sprintf(buf, "%c%d.%.2d\n", sign,
553 data >> AD7291_T_VALUE_FLOAT_OFFSET,
554 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
555}
556
557static inline ssize_t ad7291_set_t_bound(struct device *dev,
558 struct device_attribute *attr,
Sonic Zhangddaecd52010-10-27 21:43:55 -0400559 const char *buf,
560 size_t len)
561{
562 struct iio_dev *dev_info = dev_get_drvdata(dev);
563 struct ad7291_chip_info *chip = dev_info->dev_data;
Jonathan Cameron58c03232011-05-18 14:41:11 +0100564 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400565 long tmp1, tmp2;
566 u16 data;
567 char *pos;
568 int ret;
569
570 pos = strchr(buf, '.');
571
572 ret = strict_strtol(buf, 10, &tmp1);
573
574 if (ret || tmp1 > 127 || tmp1 < -128)
575 return -EINVAL;
576
577 if (pos) {
578 len = strlen(pos);
579 if (len > AD7291_T_VALUE_FLOAT_OFFSET)
580 len = AD7291_T_VALUE_FLOAT_OFFSET;
581 pos[len] = 0;
582 ret = strict_strtol(pos, 10, &tmp2);
583
584 if (!ret)
585 tmp2 = (tmp2 / 25) * 25;
586 }
587
588 if (tmp1 < 0)
589 data = (u16)(-tmp1);
590 else
591 data = (u16)tmp1;
592 data = (data << AD7291_T_VALUE_FLOAT_OFFSET) |
593 (tmp2 & AD7291_T_VALUE_FLOAT_MASK);
594 if (tmp1 < 0)
595 /* convert positive value to supplyment */
596 data = (AD7291_T_VALUE_SIGN << 1) - data;
597
Jonathan Cameron58c03232011-05-18 14:41:11 +0100598 ret = ad7291_i2c_write(chip, this_attr->address, data);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400599 if (ret)
600 return -EIO;
601
602 return ret;
603}
604
Sonic Zhangddaecd52010-10-27 21:43:55 -0400605static inline ssize_t ad7291_show_v_bound(struct device *dev,
606 struct device_attribute *attr,
607 u8 bound_reg,
608 char *buf)
609{
610 struct iio_dev *dev_info = dev_get_drvdata(dev);
611 struct ad7291_chip_info *chip = dev_info->dev_data;
612 u16 data;
613 int ret;
614
615 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
616 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
617 AD7291_VOLTAGE_LIMIT_COUNT)
618 return -EINVAL;
619
620 ret = ad7291_i2c_read(chip, bound_reg, &data);
621 if (ret)
622 return -EIO;
623
624 data &= AD7291_VALUE_MASK;
625
626 return sprintf(buf, "%d\n", data);
627}
628
629static inline ssize_t ad7291_set_v_bound(struct device *dev,
630 struct device_attribute *attr,
631 u8 bound_reg,
632 const char *buf,
633 size_t len)
634{
635 struct iio_dev *dev_info = dev_get_drvdata(dev);
636 struct ad7291_chip_info *chip = dev_info->dev_data;
637 unsigned long value;
638 u16 data;
639 int ret;
640
641 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
642 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
643 AD7291_VOLTAGE_LIMIT_COUNT)
644 return -EINVAL;
645
646 ret = strict_strtoul(buf, 10, &value);
647
648 if (ret || value >= 4096)
649 return -EINVAL;
650
651 data = (u16)value;
652 ret = ad7291_i2c_write(chip, bound_reg, data);
653 if (ret)
654 return -EIO;
655
656 return ret;
657}
658
Jonathan Cameron58c03232011-05-18 14:41:11 +0100659static IIO_DEVICE_ATTR(t_sense_high_value,
660 S_IRUGO | S_IWUSR,
661 ad7291_show_t_bound, ad7291_set_t_bound,
662 AD7291_T_SENSE_HIGH);
663static IIO_DEVICE_ATTR(t_sense_low_value,
664 S_IRUGO | S_IWUSR,
665 ad7291_show_t_bound, ad7291_set_t_bound,
666 AD7291_T_SENSE_LOW);
667static IIO_DEVICE_ATTR(t_sense_hyst_value,
668 S_IRUGO | S_IWUSR,
669 ad7291_show_t_bound, ad7291_set_t_bound,
670 AD7291_T_SENSE_HYST);
671static IIO_DEVICE_ATTR(v0_high,
672 S_IRUGO | S_IWUSR,
673 ad7291_show_t_bound, ad7291_set_t_bound, 0x04);
674static IIO_DEVICE_ATTR(v0_low,
675 S_IRUGO | S_IWUSR,
676 ad7291_show_t_bound, ad7291_set_t_bound, 0x05);
677static IIO_DEVICE_ATTR(v0_hyst,
678 S_IRUGO | S_IWUSR,
679 ad7291_show_t_bound, ad7291_set_t_bound, 0x06);
680static IIO_DEVICE_ATTR(v1_high,
681 S_IRUGO | S_IWUSR,
682 ad7291_show_t_bound, ad7291_set_t_bound, 0x07);
683static IIO_DEVICE_ATTR(v1_low,
684 S_IRUGO | S_IWUSR,
685 ad7291_show_t_bound, ad7291_set_t_bound, 0x08);
686static IIO_DEVICE_ATTR(v1_hyst,
687 S_IRUGO | S_IWUSR,
688 ad7291_show_t_bound, ad7291_set_t_bound, 0x09);
689static IIO_DEVICE_ATTR(v2_high,
690 S_IRUGO | S_IWUSR,
691 ad7291_show_t_bound, ad7291_set_t_bound, 0x0A);
692static IIO_DEVICE_ATTR(v2_low,
693 S_IRUGO | S_IWUSR,
694 ad7291_show_t_bound, ad7291_set_t_bound, 0x0B);
695static IIO_DEVICE_ATTR(v2_hyst,
696 S_IRUGO | S_IWUSR,
697 ad7291_show_t_bound, ad7291_set_t_bound, 0x0C);
698static IIO_DEVICE_ATTR(v3_high,
699 S_IRUGO | S_IWUSR,
700 /* Datasheet suggests this one and this one only
701 has the registers in different order */
702 ad7291_show_t_bound, ad7291_set_t_bound, 0x0E);
703static IIO_DEVICE_ATTR(v3_low,
704 S_IRUGO | S_IWUSR,
705 ad7291_show_t_bound, ad7291_set_t_bound, 0x0D);
706static IIO_DEVICE_ATTR(v3_hyst,
707 S_IRUGO | S_IWUSR,
708 ad7291_show_t_bound, ad7291_set_t_bound, 0x0F);
709static IIO_DEVICE_ATTR(v4_high,
710 S_IRUGO | S_IWUSR,
711 ad7291_show_t_bound, ad7291_set_t_bound, 0x10);
712static IIO_DEVICE_ATTR(v4_low,
713 S_IRUGO | S_IWUSR,
714 ad7291_show_t_bound, ad7291_set_t_bound, 0x11);
715static IIO_DEVICE_ATTR(v4_hyst,
716 S_IRUGO | S_IWUSR,
717 ad7291_show_t_bound, ad7291_set_t_bound, 0x12);
718static IIO_DEVICE_ATTR(v5_high,
719 S_IRUGO | S_IWUSR,
720 ad7291_show_t_bound, ad7291_set_t_bound, 0x13);
721static IIO_DEVICE_ATTR(v5_low,
722 S_IRUGO | S_IWUSR,
723 ad7291_show_t_bound, ad7291_set_t_bound, 0x14);
724static IIO_DEVICE_ATTR(v5_hyst,
725 S_IRUGO | S_IWUSR,
726 ad7291_show_t_bound, ad7291_set_t_bound, 0x15);
727static IIO_DEVICE_ATTR(v6_high,
728 S_IRUGO | S_IWUSR,
729 ad7291_show_t_bound, ad7291_set_t_bound, 0x16);
730static IIO_DEVICE_ATTR(v6_low,
731 S_IRUGO | S_IWUSR,
732 ad7291_show_t_bound, ad7291_set_t_bound, 0x17);
733static IIO_DEVICE_ATTR(v6_hyst,
734 S_IRUGO | S_IWUSR,
735 ad7291_show_t_bound, ad7291_set_t_bound, 0x18);
736static IIO_DEVICE_ATTR(v7_high,
737 S_IRUGO | S_IWUSR,
738 ad7291_show_t_bound, ad7291_set_t_bound, 0x19);
739static IIO_DEVICE_ATTR(v7_low,
740 S_IRUGO | S_IWUSR,
741 ad7291_show_t_bound, ad7291_set_t_bound, 0x1A);
742static IIO_DEVICE_ATTR(v7_hyst,
743 S_IRUGO | S_IWUSR,
744 ad7291_show_t_bound, ad7291_set_t_bound, 0x1B);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400745
746static struct attribute *ad7291_event_attributes[] = {
Jonathan Cameron58c03232011-05-18 14:41:11 +0100747 &iio_dev_attr_t_sense_high_value.dev_attr.attr,
748 &iio_dev_attr_t_sense_low_value.dev_attr.attr,
749 &iio_dev_attr_t_sense_hyst_value.dev_attr.attr,
750 &iio_dev_attr_v0_high.dev_attr.attr,
751 &iio_dev_attr_v0_low.dev_attr.attr,
752 &iio_dev_attr_v0_hyst.dev_attr.attr,
753 &iio_dev_attr_v1_high.dev_attr.attr,
754 &iio_dev_attr_v1_low.dev_attr.attr,
755 &iio_dev_attr_v1_hyst.dev_attr.attr,
756 &iio_dev_attr_v2_high.dev_attr.attr,
757 &iio_dev_attr_v2_low.dev_attr.attr,
758 &iio_dev_attr_v2_hyst.dev_attr.attr,
759 &iio_dev_attr_v3_high.dev_attr.attr,
760 &iio_dev_attr_v3_low.dev_attr.attr,
761 &iio_dev_attr_v3_hyst.dev_attr.attr,
762 &iio_dev_attr_v4_high.dev_attr.attr,
763 &iio_dev_attr_v4_low.dev_attr.attr,
764 &iio_dev_attr_v4_hyst.dev_attr.attr,
765 &iio_dev_attr_v5_high.dev_attr.attr,
766 &iio_dev_attr_v5_low.dev_attr.attr,
767 &iio_dev_attr_v5_hyst.dev_attr.attr,
768 &iio_dev_attr_v6_high.dev_attr.attr,
769 &iio_dev_attr_v6_low.dev_attr.attr,
770 &iio_dev_attr_v6_hyst.dev_attr.attr,
771 &iio_dev_attr_v7_high.dev_attr.attr,
772 &iio_dev_attr_v7_low.dev_attr.attr,
773 &iio_dev_attr_v7_hyst.dev_attr.attr,
Sonic Zhangddaecd52010-10-27 21:43:55 -0400774 NULL,
775};
776
777static struct attribute_group ad7291_event_attribute_group = {
778 .attrs = ad7291_event_attributes,
779};
780
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100781static const struct iio_info ad7291_info = {
782 .attrs = &ad7291_attribute_group,
783 .num_interrupt_lines = 1,
784 .event_attrs = &ad7291_event_attribute_group,
785};
786
Sonic Zhangddaecd52010-10-27 21:43:55 -0400787/*
788 * device probe and remove
789 */
790
791static int __devinit ad7291_probe(struct i2c_client *client,
792 const struct i2c_device_id *id)
793{
794 struct ad7291_chip_info *chip;
795 int ret = 0;
796
797 chip = kzalloc(sizeof(struct ad7291_chip_info), GFP_KERNEL);
798
799 if (chip == NULL)
800 return -ENOMEM;
801
802 /* this is only used for device removal purposes */
803 i2c_set_clientdata(client, chip);
804
805 chip->client = client;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400806 chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
807
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100808 chip->indio_dev = iio_allocate_device(0);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400809 if (chip->indio_dev == NULL) {
810 ret = -ENOMEM;
811 goto error_free_chip;
812 }
813
Jonathan Cameron845bd122011-05-18 14:41:44 +0100814 chip->indio_dev->name = id->name;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400815 chip->indio_dev->dev.parent = &client->dev;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100816 chip->indio_dev->info = &ad7291_info;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400817 chip->indio_dev->dev_data = (void *)chip;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400818 chip->indio_dev->modes = INDIO_DIRECT_MODE;
819
820 ret = iio_device_register(chip->indio_dev);
821 if (ret)
822 goto error_free_dev;
823
824 if (client->irq > 0) {
Jonathan Cameron58c03232011-05-18 14:41:11 +0100825 ret = request_threaded_irq(client->irq,
826 NULL,
827 &ad7291_event_handler,
828 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
Jonathan Cameron845bd122011-05-18 14:41:44 +0100829 id->name,
Jonathan Cameron58c03232011-05-18 14:41:11 +0100830 chip->indio_dev);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400831 if (ret)
832 goto error_unreg_dev;
833
Sonic Zhangddaecd52010-10-27 21:43:55 -0400834 /* set irq polarity low level */
835 chip->command |= AD7291_ALART_POLARITY;
836 }
837
838 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
839 if (ret) {
840 ret = -EIO;
841 goto error_unreg_irq;
842 }
843
844 dev_info(&client->dev, "%s temperature sensor registered.\n",
845 id->name);
846
847 return 0;
848
849error_unreg_irq:
Jonathan Cameron58c03232011-05-18 14:41:11 +0100850 free_irq(client->irq, chip->indio_dev);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400851error_unreg_dev:
852 iio_device_unregister(chip->indio_dev);
853error_free_dev:
854 iio_free_device(chip->indio_dev);
855error_free_chip:
856 kfree(chip);
857
858 return ret;
859}
860
861static int __devexit ad7291_remove(struct i2c_client *client)
862{
863 struct ad7291_chip_info *chip = i2c_get_clientdata(client);
864 struct iio_dev *indio_dev = chip->indio_dev;
865
866 if (client->irq)
Jonathan Cameron58c03232011-05-18 14:41:11 +0100867 free_irq(client->irq, chip->indio_dev);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400868 iio_device_unregister(indio_dev);
869 iio_free_device(chip->indio_dev);
870 kfree(chip);
871
872 return 0;
873}
874
875static const struct i2c_device_id ad7291_id[] = {
876 { "ad7291", 0 },
877 {}
878};
879
880MODULE_DEVICE_TABLE(i2c, ad7291_id);
881
882static struct i2c_driver ad7291_driver = {
883 .driver = {
884 .name = "ad7291",
885 },
886 .probe = ad7291_probe,
887 .remove = __devexit_p(ad7291_remove),
888 .id_table = ad7291_id,
889};
890
891static __init int ad7291_init(void)
892{
893 return i2c_add_driver(&ad7291_driver);
894}
895
896static __exit void ad7291_exit(void)
897{
898 i2c_del_driver(&ad7291_driver);
899}
900
901MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
902MODULE_DESCRIPTION("Analog Devices AD7291 digital"
903 " temperature sensor driver");
904MODULE_LICENSE("GPL v2");
905
906module_init(ad7291_init);
907module_exit(ad7291_exit);