staging:iio: use the new central name attribute creation code
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / iio / adc / adt7410.c
1 /*
2 * ADT7410 digital temperature sensor driver supporting ADT7410
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/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/list.h>
15 #include <linux/i2c.h>
16
17 #include "../iio.h"
18 #include "../sysfs.h"
19
20 /*
21 * ADT7410 registers definition
22 */
23
24 #define ADT7410_TEMPERATURE 0
25 #define ADT7410_STATUS 2
26 #define ADT7410_CONFIG 3
27 #define ADT7410_T_ALARM_HIGH 4
28 #define ADT7410_T_ALARM_LOW 6
29 #define ADT7410_T_CRIT 8
30 #define ADT7410_T_HYST 0xA
31 #define ADT7410_ID 0xB
32 #define ADT7410_RESET 0x2F
33
34 /*
35 * ADT7410 status
36 */
37 #define ADT7410_STAT_T_LOW 0x10
38 #define ADT7410_STAT_T_HIGH 0x20
39 #define ADT7410_STAT_T_CRIT 0x40
40 #define ADT7410_STAT_NOT_RDY 0x80
41
42 /*
43 * ADT7410 config
44 */
45 #define ADT7410_FAULT_QUEUE_MASK 0x3
46 #define ADT7410_CT_POLARITY 0x4
47 #define ADT7410_INT_POLARITY 0x8
48 #define ADT7410_EVENT_MODE 0x10
49 #define ADT7410_MODE_MASK 0x60
50 #define ADT7410_ONESHOT 0x20
51 #define ADT7410_SPS 0x40
52 #define ADT7410_PD 0x60
53 #define ADT7410_RESOLUTION 0x80
54
55 /*
56 * ADT7410 masks
57 */
58 #define ADT7410_T16_VALUE_SIGN 0x8000
59 #define ADT7410_T16_VALUE_FLOAT_OFFSET 7
60 #define ADT7410_T16_VALUE_FLOAT_MASK 0x7F
61 #define ADT7410_T13_VALUE_SIGN 0x1000
62 #define ADT7410_T13_VALUE_OFFSET 3
63 #define ADT7410_T13_VALUE_FLOAT_OFFSET 4
64 #define ADT7410_T13_VALUE_FLOAT_MASK 0xF
65 #define ADT7410_T_HYST_MASK 0xF
66 #define ADT7410_DEVICE_ID_MASK 0xF
67 #define ADT7410_MANUFACTORY_ID_MASK 0xF0
68 #define ADT7410_MANUFACTORY_ID_OFFSET 4
69
70 #define ADT7410_IRQS 2
71
72 /*
73 * struct adt7410_chip_info - chip specifc information
74 */
75
76 struct adt7410_chip_info {
77 struct i2c_client *client;
78 struct iio_dev *indio_dev;
79 u8 config;
80 };
81
82 /*
83 * adt7410 register access by I2C
84 */
85
86 static int adt7410_i2c_read_word(struct adt7410_chip_info *chip, u8 reg, u16 *data)
87 {
88 struct i2c_client *client = chip->client;
89 int ret = 0;
90
91 ret = i2c_smbus_read_word_data(client, reg);
92 if (ret < 0) {
93 dev_err(&client->dev, "I2C read error\n");
94 return ret;
95 }
96
97 *data = swab16((u16)ret);
98
99 return 0;
100 }
101
102 static int adt7410_i2c_write_word(struct adt7410_chip_info *chip, u8 reg, u16 data)
103 {
104 struct i2c_client *client = chip->client;
105 int ret = 0;
106
107 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
108 if (ret < 0)
109 dev_err(&client->dev, "I2C write error\n");
110
111 return ret;
112 }
113
114 static int adt7410_i2c_read_byte(struct adt7410_chip_info *chip, u8 reg, u8 *data)
115 {
116 struct i2c_client *client = chip->client;
117 int ret = 0;
118
119 ret = i2c_smbus_read_byte_data(client, reg);
120 if (ret < 0) {
121 dev_err(&client->dev, "I2C read error\n");
122 return ret;
123 }
124
125 *data = (u8)ret;
126
127 return 0;
128 }
129
130 static int adt7410_i2c_write_byte(struct adt7410_chip_info *chip, u8 reg, u8 data)
131 {
132 struct i2c_client *client = chip->client;
133 int ret = 0;
134
135 ret = i2c_smbus_write_byte_data(client, reg, data);
136 if (ret < 0)
137 dev_err(&client->dev, "I2C write error\n");
138
139 return ret;
140 }
141
142 static ssize_t adt7410_show_mode(struct device *dev,
143 struct device_attribute *attr,
144 char *buf)
145 {
146 struct iio_dev *dev_info = dev_get_drvdata(dev);
147 struct adt7410_chip_info *chip = dev_info->dev_data;
148 u8 config;
149
150 config = chip->config & ADT7410_MODE_MASK;
151
152 switch (config) {
153 case ADT7410_PD:
154 return sprintf(buf, "power-down\n");
155 case ADT7410_ONESHOT:
156 return sprintf(buf, "one-shot\n");
157 case ADT7410_SPS:
158 return sprintf(buf, "sps\n");
159 default:
160 return sprintf(buf, "full\n");
161 }
162 }
163
164 static ssize_t adt7410_store_mode(struct device *dev,
165 struct device_attribute *attr,
166 const char *buf,
167 size_t len)
168 {
169 struct iio_dev *dev_info = dev_get_drvdata(dev);
170 struct adt7410_chip_info *chip = dev_info->dev_data;
171 u16 config;
172 int ret;
173
174 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
175 if (ret)
176 return -EIO;
177
178 config = chip->config & (~ADT7410_MODE_MASK);
179 if (strcmp(buf, "power-down"))
180 config |= ADT7410_PD;
181 else if (strcmp(buf, "one-shot"))
182 config |= ADT7410_ONESHOT;
183 else if (strcmp(buf, "sps"))
184 config |= ADT7410_SPS;
185
186 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
187 if (ret)
188 return -EIO;
189
190 chip->config = config;
191
192 return ret;
193 }
194
195 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
196 adt7410_show_mode,
197 adt7410_store_mode,
198 0);
199
200 static ssize_t adt7410_show_available_modes(struct device *dev,
201 struct device_attribute *attr,
202 char *buf)
203 {
204 return sprintf(buf, "full\none-shot\nsps\npower-down\n");
205 }
206
207 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, adt7410_show_available_modes, NULL, 0);
208
209 static ssize_t adt7410_show_resolution(struct device *dev,
210 struct device_attribute *attr,
211 char *buf)
212 {
213 struct iio_dev *dev_info = dev_get_drvdata(dev);
214 struct adt7410_chip_info *chip = dev_info->dev_data;
215 int ret;
216 int bits;
217
218 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
219 if (ret)
220 return -EIO;
221
222 if (chip->config & ADT7410_RESOLUTION)
223 bits = 16;
224 else
225 bits = 13;
226
227 return sprintf(buf, "%d bits\n", bits);
228 }
229
230 static ssize_t adt7410_store_resolution(struct device *dev,
231 struct device_attribute *attr,
232 const char *buf,
233 size_t len)
234 {
235 struct iio_dev *dev_info = dev_get_drvdata(dev);
236 struct adt7410_chip_info *chip = dev_info->dev_data;
237 unsigned long data;
238 u16 config;
239 int ret;
240
241 ret = strict_strtoul(buf, 10, &data);
242 if (ret)
243 return -EINVAL;
244
245 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
246 if (ret)
247 return -EIO;
248
249 config = chip->config & (~ADT7410_RESOLUTION);
250 if (data)
251 config |= ADT7410_RESOLUTION;
252
253 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
254 if (ret)
255 return -EIO;
256
257 chip->config = config;
258
259 return ret;
260 }
261
262 static IIO_DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR,
263 adt7410_show_resolution,
264 adt7410_store_resolution,
265 0);
266
267 static ssize_t adt7410_show_id(struct device *dev,
268 struct device_attribute *attr,
269 char *buf)
270 {
271 struct iio_dev *dev_info = dev_get_drvdata(dev);
272 struct adt7410_chip_info *chip = dev_info->dev_data;
273 u8 id;
274 int ret;
275
276 ret = adt7410_i2c_read_byte(chip, ADT7410_ID, &id);
277 if (ret)
278 return -EIO;
279
280 return sprintf(buf, "device id: 0x%x\nmanufactory id: 0x%x\n",
281 id & ADT7410_DEVICE_ID_MASK,
282 (id & ADT7410_MANUFACTORY_ID_MASK) >> ADT7410_MANUFACTORY_ID_OFFSET);
283 }
284
285 static IIO_DEVICE_ATTR(id, S_IRUGO | S_IWUSR,
286 adt7410_show_id,
287 NULL,
288 0);
289
290 static ssize_t adt7410_convert_temperature(struct adt7410_chip_info *chip,
291 u16 data, char *buf)
292 {
293 char sign = ' ';
294
295 if (chip->config & ADT7410_RESOLUTION) {
296 if (data & ADT7410_T16_VALUE_SIGN) {
297 /* convert supplement to positive value */
298 data = (u16)((ADT7410_T16_VALUE_SIGN << 1) - (u32)data);
299 sign = '-';
300 }
301 return sprintf(buf, "%c%d.%.7d\n", sign,
302 (data >> ADT7410_T16_VALUE_FLOAT_OFFSET),
303 (data & ADT7410_T16_VALUE_FLOAT_MASK) * 78125);
304 } else {
305 if (data & ADT7410_T13_VALUE_SIGN) {
306 /* convert supplement to positive value */
307 data >>= ADT7410_T13_VALUE_OFFSET;
308 data = (ADT7410_T13_VALUE_SIGN << 1) - data;
309 sign = '-';
310 }
311 return sprintf(buf, "%c%d.%.4d\n", sign,
312 (data >> ADT7410_T13_VALUE_FLOAT_OFFSET),
313 (data & ADT7410_T13_VALUE_FLOAT_MASK) * 625);
314 }
315 }
316
317 static ssize_t adt7410_show_value(struct device *dev,
318 struct device_attribute *attr,
319 char *buf)
320 {
321 struct iio_dev *dev_info = dev_get_drvdata(dev);
322 struct adt7410_chip_info *chip = dev_info->dev_data;
323 u8 status;
324 u16 data;
325 int ret, i = 0;
326
327 do {
328 ret = adt7410_i2c_read_byte(chip, ADT7410_STATUS, &status);
329 if (ret)
330 return -EIO;
331 i++;
332 if (i == 10000)
333 return -EIO;
334 } while (status & ADT7410_STAT_NOT_RDY);
335
336 ret = adt7410_i2c_read_word(chip, ADT7410_TEMPERATURE, &data);
337 if (ret)
338 return -EIO;
339
340 return adt7410_convert_temperature(chip, data, buf);
341 }
342
343 static IIO_DEVICE_ATTR(value, S_IRUGO, adt7410_show_value, NULL, 0);
344
345 static struct attribute *adt7410_attributes[] = {
346 &iio_dev_attr_available_modes.dev_attr.attr,
347 &iio_dev_attr_mode.dev_attr.attr,
348 &iio_dev_attr_resolution.dev_attr.attr,
349 &iio_dev_attr_id.dev_attr.attr,
350 &iio_dev_attr_value.dev_attr.attr,
351 NULL,
352 };
353
354 static const struct attribute_group adt7410_attribute_group = {
355 .attrs = adt7410_attributes,
356 };
357
358 /*
359 * temperature bound events
360 */
361
362 #define IIO_EVENT_CODE_ADT7410_ABOVE_ALARM IIO_BUFFER_EVENT_CODE(0)
363 #define IIO_EVENT_CODE_ADT7410_BELLOW_ALARM IIO_BUFFER_EVENT_CODE(1)
364 #define IIO_EVENT_CODE_ADT7410_ABOVE_CRIT IIO_BUFFER_EVENT_CODE(2)
365
366 static irqreturn_t adt7410_event_handler(int irq, void *private)
367 {
368 struct iio_dev *indio_dev = private;
369 struct adt7410_chip_info *chip = iio_dev_get_devdata(indio_dev);
370 s64 timestamp = iio_get_time_ns();
371 u8 status;
372
373 if (adt7410_i2c_read_byte(chip, ADT7410_STATUS, &status))
374 return IRQ_HANDLED;
375
376 if (status & ADT7410_STAT_T_HIGH)
377 iio_push_event(indio_dev, 0,
378 IIO_EVENT_CODE_ADT7410_ABOVE_ALARM,
379 timestamp);
380 if (status & ADT7410_STAT_T_LOW)
381 iio_push_event(indio_dev, 0,
382 IIO_EVENT_CODE_ADT7410_BELLOW_ALARM,
383 timestamp);
384 if (status & ADT7410_STAT_T_CRIT)
385 iio_push_event(indio_dev, 0,
386 IIO_EVENT_CODE_ADT7410_ABOVE_CRIT,
387 timestamp);
388
389 return IRQ_HANDLED;
390 }
391
392 static ssize_t adt7410_show_event_mode(struct device *dev,
393 struct device_attribute *attr,
394 char *buf)
395 {
396 struct iio_dev *dev_info = dev_get_drvdata(dev);
397 struct adt7410_chip_info *chip = dev_info->dev_data;
398 int ret;
399
400 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
401 if (ret)
402 return -EIO;
403
404 if (chip->config & ADT7410_EVENT_MODE)
405 return sprintf(buf, "interrupt\n");
406 else
407 return sprintf(buf, "comparator\n");
408 }
409
410 static ssize_t adt7410_set_event_mode(struct device *dev,
411 struct device_attribute *attr,
412 const char *buf,
413 size_t len)
414 {
415 struct iio_dev *dev_info = dev_get_drvdata(dev);
416 struct adt7410_chip_info *chip = dev_info->dev_data;
417 u16 config;
418 int ret;
419
420 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
421 if (ret)
422 return -EIO;
423
424 config = chip->config &= ~ADT7410_EVENT_MODE;
425 if (strcmp(buf, "comparator") != 0)
426 config |= ADT7410_EVENT_MODE;
427
428 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
429 if (ret)
430 return -EIO;
431
432 chip->config = config;
433
434 return ret;
435 }
436
437 static ssize_t adt7410_show_available_event_modes(struct device *dev,
438 struct device_attribute *attr,
439 char *buf)
440 {
441 return sprintf(buf, "comparator\ninterrupt\n");
442 }
443
444 static ssize_t adt7410_show_fault_queue(struct device *dev,
445 struct device_attribute *attr,
446 char *buf)
447 {
448 struct iio_dev *dev_info = dev_get_drvdata(dev);
449 struct adt7410_chip_info *chip = dev_info->dev_data;
450 int ret;
451
452 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
453 if (ret)
454 return -EIO;
455
456 return sprintf(buf, "%d\n", chip->config & ADT7410_FAULT_QUEUE_MASK);
457 }
458
459 static ssize_t adt7410_set_fault_queue(struct device *dev,
460 struct device_attribute *attr,
461 const char *buf,
462 size_t len)
463 {
464 struct iio_dev *dev_info = dev_get_drvdata(dev);
465 struct adt7410_chip_info *chip = dev_info->dev_data;
466 unsigned long data;
467 int ret;
468 u8 config;
469
470 ret = strict_strtoul(buf, 10, &data);
471 if (ret || data > 3)
472 return -EINVAL;
473
474 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
475 if (ret)
476 return -EIO;
477
478 config = chip->config & ~ADT7410_FAULT_QUEUE_MASK;
479 config |= data;
480 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
481 if (ret)
482 return -EIO;
483
484 chip->config = config;
485
486 return ret;
487 }
488
489 static inline ssize_t adt7410_show_t_bound(struct device *dev,
490 struct device_attribute *attr,
491 u8 bound_reg,
492 char *buf)
493 {
494 struct iio_dev *dev_info = dev_get_drvdata(dev);
495 struct adt7410_chip_info *chip = dev_info->dev_data;
496 u16 data;
497 int ret;
498
499 ret = adt7410_i2c_read_word(chip, bound_reg, &data);
500 if (ret)
501 return -EIO;
502
503 return adt7410_convert_temperature(chip, data, buf);
504 }
505
506 static inline ssize_t adt7410_set_t_bound(struct device *dev,
507 struct device_attribute *attr,
508 u8 bound_reg,
509 const char *buf,
510 size_t len)
511 {
512 struct iio_dev *dev_info = dev_get_drvdata(dev);
513 struct adt7410_chip_info *chip = dev_info->dev_data;
514 long tmp1, tmp2;
515 u16 data;
516 char *pos;
517 int ret;
518
519 pos = strchr(buf, '.');
520
521 ret = strict_strtol(buf, 10, &tmp1);
522
523 if (ret || tmp1 > 127 || tmp1 < -128)
524 return -EINVAL;
525
526 if (pos) {
527 len = strlen(pos);
528
529 if (chip->config & ADT7410_RESOLUTION) {
530 if (len > ADT7410_T16_VALUE_FLOAT_OFFSET)
531 len = ADT7410_T16_VALUE_FLOAT_OFFSET;
532 pos[len] = 0;
533 ret = strict_strtol(pos, 10, &tmp2);
534
535 if (!ret)
536 tmp2 = (tmp2 / 78125) * 78125;
537 } else {
538 if (len > ADT7410_T13_VALUE_FLOAT_OFFSET)
539 len = ADT7410_T13_VALUE_FLOAT_OFFSET;
540 pos[len] = 0;
541 ret = strict_strtol(pos, 10, &tmp2);
542
543 if (!ret)
544 tmp2 = (tmp2 / 625) * 625;
545 }
546 }
547
548 if (tmp1 < 0)
549 data = (u16)(-tmp1);
550 else
551 data = (u16)tmp1;
552
553 if (chip->config & ADT7410_RESOLUTION) {
554 data = (data << ADT7410_T16_VALUE_FLOAT_OFFSET) |
555 (tmp2 & ADT7410_T16_VALUE_FLOAT_MASK);
556
557 if (tmp1 < 0)
558 /* convert positive value to supplyment */
559 data = (u16)((ADT7410_T16_VALUE_SIGN << 1) - (u32)data);
560 } else {
561 data = (data << ADT7410_T13_VALUE_FLOAT_OFFSET) |
562 (tmp2 & ADT7410_T13_VALUE_FLOAT_MASK);
563
564 if (tmp1 < 0)
565 /* convert positive value to supplyment */
566 data = (ADT7410_T13_VALUE_SIGN << 1) - data;
567 data <<= ADT7410_T13_VALUE_OFFSET;
568 }
569
570 ret = adt7410_i2c_write_word(chip, bound_reg, data);
571 if (ret)
572 return -EIO;
573
574 return ret;
575 }
576
577 static ssize_t adt7410_show_t_alarm_high(struct device *dev,
578 struct device_attribute *attr,
579 char *buf)
580 {
581 return adt7410_show_t_bound(dev, attr,
582 ADT7410_T_ALARM_HIGH, buf);
583 }
584
585 static inline ssize_t adt7410_set_t_alarm_high(struct device *dev,
586 struct device_attribute *attr,
587 const char *buf,
588 size_t len)
589 {
590 return adt7410_set_t_bound(dev, attr,
591 ADT7410_T_ALARM_HIGH, buf, len);
592 }
593
594 static ssize_t adt7410_show_t_alarm_low(struct device *dev,
595 struct device_attribute *attr,
596 char *buf)
597 {
598 return adt7410_show_t_bound(dev, attr,
599 ADT7410_T_ALARM_LOW, buf);
600 }
601
602 static inline ssize_t adt7410_set_t_alarm_low(struct device *dev,
603 struct device_attribute *attr,
604 const char *buf,
605 size_t len)
606 {
607 return adt7410_set_t_bound(dev, attr,
608 ADT7410_T_ALARM_LOW, buf, len);
609 }
610
611 static ssize_t adt7410_show_t_crit(struct device *dev,
612 struct device_attribute *attr,
613 char *buf)
614 {
615 return adt7410_show_t_bound(dev, attr,
616 ADT7410_T_CRIT, buf);
617 }
618
619 static inline ssize_t adt7410_set_t_crit(struct device *dev,
620 struct device_attribute *attr,
621 const char *buf,
622 size_t len)
623 {
624 return adt7410_set_t_bound(dev, attr,
625 ADT7410_T_CRIT, buf, len);
626 }
627
628 static ssize_t adt7410_show_t_hyst(struct device *dev,
629 struct device_attribute *attr,
630 char *buf)
631 {
632 struct iio_dev *dev_info = dev_get_drvdata(dev);
633 struct adt7410_chip_info *chip = dev_info->dev_data;
634 int ret;
635 u8 t_hyst;
636
637 ret = adt7410_i2c_read_byte(chip, ADT7410_T_HYST, &t_hyst);
638 if (ret)
639 return -EIO;
640
641 return sprintf(buf, "%d\n", t_hyst & ADT7410_T_HYST_MASK);
642 }
643
644 static inline ssize_t adt7410_set_t_hyst(struct device *dev,
645 struct device_attribute *attr,
646 const char *buf,
647 size_t len)
648 {
649 struct iio_dev *dev_info = dev_get_drvdata(dev);
650 struct adt7410_chip_info *chip = dev_info->dev_data;
651 int ret;
652 unsigned long data;
653 u8 t_hyst;
654
655 ret = strict_strtol(buf, 10, &data);
656
657 if (ret || data > ADT7410_T_HYST_MASK)
658 return -EINVAL;
659
660 t_hyst = (u8)data;
661
662 ret = adt7410_i2c_write_byte(chip, ADT7410_T_HYST, t_hyst);
663 if (ret)
664 return -EIO;
665
666 return ret;
667 }
668
669 static IIO_DEVICE_ATTR(event_mode,
670 S_IRUGO | S_IWUSR,
671 adt7410_show_event_mode, adt7410_set_event_mode, 0);
672 static IIO_DEVICE_ATTR(available_event_modes,
673 S_IRUGO,
674 adt7410_show_available_event_modes, NULL, 0);
675 static IIO_DEVICE_ATTR(fault_queue,
676 S_IRUGO | S_IWUSR,
677 adt7410_show_fault_queue, adt7410_set_fault_queue, 0);
678 static IIO_DEVICE_ATTR(t_alarm_high,
679 S_IRUGO | S_IWUSR,
680 adt7410_show_t_alarm_high, adt7410_set_t_alarm_high, 0);
681 static IIO_DEVICE_ATTR(t_alarm_low,
682 S_IRUGO | S_IWUSR,
683 adt7410_show_t_alarm_low, adt7410_set_t_alarm_low, 0);
684 static IIO_DEVICE_ATTR(t_crit,
685 S_IRUGO | S_IWUSR,
686 adt7410_show_t_crit, adt7410_set_t_crit, 0);
687 static IIO_DEVICE_ATTR(t_hyst,
688 S_IRUGO | S_IWUSR,
689 adt7410_show_t_hyst, adt7410_set_t_hyst, 0);
690
691 static struct attribute *adt7410_event_int_attributes[] = {
692 &iio_dev_attr_event_mode.dev_attr.attr,
693 &iio_dev_attr_available_event_modes.dev_attr.attr,
694 &iio_dev_attr_fault_queue.dev_attr.attr,
695 &iio_dev_attr_t_alarm_high.dev_attr.attr,
696 &iio_dev_attr_t_alarm_low.dev_attr.attr,
697 &iio_dev_attr_t_hyst.dev_attr.attr,
698 NULL,
699 };
700
701 static struct attribute *adt7410_event_ct_attributes[] = {
702 &iio_dev_attr_event_mode.dev_attr.attr,
703 &iio_dev_attr_available_event_modes.dev_attr.attr,
704 &iio_dev_attr_fault_queue.dev_attr.attr,
705 &iio_dev_attr_t_crit.dev_attr.attr,
706 &iio_dev_attr_t_hyst.dev_attr.attr,
707 NULL,
708 };
709
710 static struct attribute_group adt7410_event_attribute_group[ADT7410_IRQS] = {
711 {
712 .attrs = adt7410_event_int_attributes,
713 }, {
714 .attrs = adt7410_event_ct_attributes,
715 }
716 };
717
718 /*
719 * device probe and remove
720 */
721
722 static int __devinit adt7410_probe(struct i2c_client *client,
723 const struct i2c_device_id *id)
724 {
725 struct adt7410_chip_info *chip;
726 int ret = 0;
727 unsigned long *adt7410_platform_data = client->dev.platform_data;
728
729 chip = kzalloc(sizeof(struct adt7410_chip_info), GFP_KERNEL);
730
731 if (chip == NULL)
732 return -ENOMEM;
733
734 /* this is only used for device removal purposes */
735 i2c_set_clientdata(client, chip);
736
737 chip->client = client;
738
739 chip->indio_dev = iio_allocate_device(0);
740 if (chip->indio_dev == NULL) {
741 ret = -ENOMEM;
742 goto error_free_chip;
743 }
744 chip->indio_dev->name = id->name;
745 chip->indio_dev->dev.parent = &client->dev;
746 chip->indio_dev->attrs = &adt7410_attribute_group;
747 chip->indio_dev->event_attrs = adt7410_event_attribute_group;
748 chip->indio_dev->dev_data = (void *)chip;
749 chip->indio_dev->driver_module = THIS_MODULE;
750 chip->indio_dev->num_interrupt_lines = ADT7410_IRQS;
751 chip->indio_dev->modes = INDIO_DIRECT_MODE;
752
753 ret = iio_device_register(chip->indio_dev);
754 if (ret)
755 goto error_free_dev;
756
757 /* CT critcal temperature event. line 0 */
758 if (client->irq) {
759 ret = request_threaded_irq(client->irq,
760 NULL,
761 &adt7410_event_handler,
762 IRQF_TRIGGER_LOW,
763 id->name,
764 chip->indio_dev);
765 if (ret)
766 goto error_unreg_dev;
767 }
768
769 /* INT bound temperature alarm event. line 1 */
770 if (adt7410_platform_data[0]) {
771 ret = request_threaded_irq(adt7410_platform_data[0],
772 NULL,
773 &adt7410_event_handler,
774 adt7410_platform_data[1],
775 id->name,
776 chip->indio_dev);
777 if (ret)
778 goto error_unreg_ct_irq;
779 }
780
781 if (client->irq && adt7410_platform_data[0]) {
782
783 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
784 if (ret) {
785 ret = -EIO;
786 goto error_unreg_int_irq;
787 }
788
789 /* set irq polarity low level */
790 chip->config &= ~ADT7410_CT_POLARITY;
791
792 if (adt7410_platform_data[1] & IRQF_TRIGGER_HIGH)
793 chip->config |= ADT7410_INT_POLARITY;
794 else
795 chip->config &= ~ADT7410_INT_POLARITY;
796
797 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, chip->config);
798 if (ret) {
799 ret = -EIO;
800 goto error_unreg_int_irq;
801 }
802 }
803
804 dev_info(&client->dev, "%s temperature sensor registered.\n",
805 id->name);
806
807 return 0;
808
809 error_unreg_int_irq:
810 free_irq(adt7410_platform_data[0], chip->indio_dev);
811 error_unreg_ct_irq:
812 free_irq(client->irq, chip->indio_dev);
813 error_unreg_dev:
814 iio_device_unregister(chip->indio_dev);
815 error_free_dev:
816 iio_free_device(chip->indio_dev);
817 error_free_chip:
818 kfree(chip);
819
820 return ret;
821 }
822
823 static int __devexit adt7410_remove(struct i2c_client *client)
824 {
825 struct adt7410_chip_info *chip = i2c_get_clientdata(client);
826 struct iio_dev *indio_dev = chip->indio_dev;
827 unsigned long *adt7410_platform_data = client->dev.platform_data;
828
829 if (adt7410_platform_data[0])
830 free_irq(adt7410_platform_data[0], chip->indio_dev);
831 if (client->irq)
832 free_irq(client->irq, chip->indio_dev);
833 iio_device_unregister(indio_dev);
834 iio_free_device(chip->indio_dev);
835 kfree(chip);
836
837 return 0;
838 }
839
840 static const struct i2c_device_id adt7410_id[] = {
841 { "adt7410", 0 },
842 {}
843 };
844
845 MODULE_DEVICE_TABLE(i2c, adt7410_id);
846
847 static struct i2c_driver adt7410_driver = {
848 .driver = {
849 .name = "adt7410",
850 },
851 .probe = adt7410_probe,
852 .remove = __devexit_p(adt7410_remove),
853 .id_table = adt7410_id,
854 };
855
856 static __init int adt7410_init(void)
857 {
858 return i2c_add_driver(&adt7410_driver);
859 }
860
861 static __exit void adt7410_exit(void)
862 {
863 i2c_del_driver(&adt7410_driver);
864 }
865
866 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
867 MODULE_DESCRIPTION("Analog Devices ADT7410 digital"
868 " temperature sensor driver");
869 MODULE_LICENSE("GPL v2");
870
871 module_init(adt7410_init);
872 module_exit(adt7410_exit);