9b2a8ec750a6ed6215bc8ae5c72c09073b08ea9a
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / iio / adc / adt7310.c
1 /*
2 * ADT7310 digital temperature sensor driver supporting ADT7310
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/spi/spi.h>
16
17 #include "../iio.h"
18 #include "../sysfs.h"
19
20 /*
21 * ADT7310 registers definition
22 */
23
24 #define ADT7310_STATUS 0
25 #define ADT7310_CONFIG 1
26 #define ADT7310_TEMPERATURE 2
27 #define ADT7310_ID 3
28 #define ADT7310_T_CRIT 4
29 #define ADT7310_T_HYST 5
30 #define ADT7310_T_ALARM_HIGH 6
31 #define ADT7310_T_ALARM_LOW 7
32
33 /*
34 * ADT7310 status
35 */
36 #define ADT7310_STAT_T_LOW 0x10
37 #define ADT7310_STAT_T_HIGH 0x20
38 #define ADT7310_STAT_T_CRIT 0x40
39 #define ADT7310_STAT_NOT_RDY 0x80
40
41 /*
42 * ADT7310 config
43 */
44 #define ADT7310_FAULT_QUEUE_MASK 0x3
45 #define ADT7310_CT_POLARITY 0x4
46 #define ADT7310_INT_POLARITY 0x8
47 #define ADT7310_EVENT_MODE 0x10
48 #define ADT7310_MODE_MASK 0x60
49 #define ADT7310_ONESHOT 0x20
50 #define ADT7310_SPS 0x40
51 #define ADT7310_PD 0x60
52 #define ADT7310_RESOLUTION 0x80
53
54 /*
55 * ADT7310 masks
56 */
57 #define ADT7310_T16_VALUE_SIGN 0x8000
58 #define ADT7310_T16_VALUE_FLOAT_OFFSET 7
59 #define ADT7310_T16_VALUE_FLOAT_MASK 0x7F
60 #define ADT7310_T13_VALUE_SIGN 0x1000
61 #define ADT7310_T13_VALUE_OFFSET 3
62 #define ADT7310_T13_VALUE_FLOAT_OFFSET 4
63 #define ADT7310_T13_VALUE_FLOAT_MASK 0xF
64 #define ADT7310_T_HYST_MASK 0xF
65 #define ADT7310_DEVICE_ID_MASK 0x7
66 #define ADT7310_MANUFACTORY_ID_MASK 0xF8
67 #define ADT7310_MANUFACTORY_ID_OFFSET 3
68
69
70 #define ADT7310_CMD_REG_MASK 0x28
71 #define ADT7310_CMD_REG_OFFSET 3
72 #define ADT7310_CMD_READ 0x40
73 #define ADT7310_CMD_CON_READ 0x4
74
75 #define ADT7310_IRQS 2
76
77 /*
78 * struct adt7310_chip_info - chip specifc information
79 */
80
81 struct adt7310_chip_info {
82 const char *name;
83 struct spi_device *spi_dev;
84 struct iio_dev *indio_dev;
85 u8 config;
86 };
87
88 /*
89 * adt7310 register access by SPI
90 */
91
92 static int adt7310_spi_read_word(struct adt7310_chip_info *chip, u8 reg, u16 *data)
93 {
94 struct spi_device *spi_dev = chip->spi_dev;
95 u8 command = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
96 int ret = 0;
97
98 command |= ADT7310_CMD_READ;
99 ret = spi_write(spi_dev, &command, sizeof(command));
100 if (ret < 0) {
101 dev_err(&spi_dev->dev, "SPI write command error\n");
102 return ret;
103 }
104
105 ret = spi_read(spi_dev, (u8 *)data, sizeof(*data));
106 if (ret < 0) {
107 dev_err(&spi_dev->dev, "SPI read word error\n");
108 return ret;
109 }
110
111 *data = be16_to_cpu(*data);
112
113 return 0;
114 }
115
116 static int adt7310_spi_write_word(struct adt7310_chip_info *chip, u8 reg, u16 data)
117 {
118 struct spi_device *spi_dev = chip->spi_dev;
119 u8 buf[3];
120 int ret = 0;
121
122 buf[0] = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
123 buf[1] = (u8)(data >> 8);
124 buf[2] = (u8)(data & 0xFF);
125
126 ret = spi_write(spi_dev, buf, 3);
127 if (ret < 0) {
128 dev_err(&spi_dev->dev, "SPI write word error\n");
129 return ret;
130 }
131
132 return ret;
133 }
134
135 static int adt7310_spi_read_byte(struct adt7310_chip_info *chip, u8 reg, u8 *data)
136 {
137 struct spi_device *spi_dev = chip->spi_dev;
138 u8 command = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
139 int ret = 0;
140
141 command |= ADT7310_CMD_READ;
142 ret = spi_write(spi_dev, &command, sizeof(command));
143 if (ret < 0) {
144 dev_err(&spi_dev->dev, "SPI write command error\n");
145 return ret;
146 }
147
148 ret = spi_read(spi_dev, data, sizeof(*data));
149 if (ret < 0) {
150 dev_err(&spi_dev->dev, "SPI read byte error\n");
151 return ret;
152 }
153
154 return 0;
155 }
156
157 static int adt7310_spi_write_byte(struct adt7310_chip_info *chip, u8 reg, u8 data)
158 {
159 struct spi_device *spi_dev = chip->spi_dev;
160 u8 buf[2];
161 int ret = 0;
162
163 buf[0] = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
164 buf[1] = data;
165
166 ret = spi_write(spi_dev, buf, 2);
167 if (ret < 0) {
168 dev_err(&spi_dev->dev, "SPI write byte error\n");
169 return ret;
170 }
171
172 return ret;
173 }
174
175 static ssize_t adt7310_show_mode(struct device *dev,
176 struct device_attribute *attr,
177 char *buf)
178 {
179 struct iio_dev *dev_info = dev_get_drvdata(dev);
180 struct adt7310_chip_info *chip = dev_info->dev_data;
181 u8 config;
182
183 config = chip->config & ADT7310_MODE_MASK;
184
185 switch (config) {
186 case ADT7310_PD:
187 return sprintf(buf, "power-down\n");
188 case ADT7310_ONESHOT:
189 return sprintf(buf, "one-shot\n");
190 case ADT7310_SPS:
191 return sprintf(buf, "sps\n");
192 default:
193 return sprintf(buf, "full\n");
194 }
195 }
196
197 static ssize_t adt7310_store_mode(struct device *dev,
198 struct device_attribute *attr,
199 const char *buf,
200 size_t len)
201 {
202 struct iio_dev *dev_info = dev_get_drvdata(dev);
203 struct adt7310_chip_info *chip = dev_info->dev_data;
204 u16 config;
205 int ret;
206
207 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
208 if (ret)
209 return -EIO;
210
211 config = chip->config & (~ADT7310_MODE_MASK);
212 if (strcmp(buf, "power-down"))
213 config |= ADT7310_PD;
214 else if (strcmp(buf, "one-shot"))
215 config |= ADT7310_ONESHOT;
216 else if (strcmp(buf, "sps"))
217 config |= ADT7310_SPS;
218
219 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
220 if (ret)
221 return -EIO;
222
223 chip->config = config;
224
225 return len;
226 }
227
228 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
229 adt7310_show_mode,
230 adt7310_store_mode,
231 0);
232
233 static ssize_t adt7310_show_available_modes(struct device *dev,
234 struct device_attribute *attr,
235 char *buf)
236 {
237 return sprintf(buf, "full\none-shot\nsps\npower-down\n");
238 }
239
240 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, adt7310_show_available_modes, NULL, 0);
241
242 static ssize_t adt7310_show_resolution(struct device *dev,
243 struct device_attribute *attr,
244 char *buf)
245 {
246 struct iio_dev *dev_info = dev_get_drvdata(dev);
247 struct adt7310_chip_info *chip = dev_info->dev_data;
248 int ret;
249 int bits;
250
251 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
252 if (ret)
253 return -EIO;
254
255 if (chip->config & ADT7310_RESOLUTION)
256 bits = 16;
257 else
258 bits = 13;
259
260 return sprintf(buf, "%d bits\n", bits);
261 }
262
263 static ssize_t adt7310_store_resolution(struct device *dev,
264 struct device_attribute *attr,
265 const char *buf,
266 size_t len)
267 {
268 struct iio_dev *dev_info = dev_get_drvdata(dev);
269 struct adt7310_chip_info *chip = dev_info->dev_data;
270 unsigned long data;
271 u16 config;
272 int ret;
273
274 ret = strict_strtoul(buf, 10, &data);
275 if (ret)
276 return -EINVAL;
277
278 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
279 if (ret)
280 return -EIO;
281
282 config = chip->config & (~ADT7310_RESOLUTION);
283 if (data)
284 config |= ADT7310_RESOLUTION;
285
286 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
287 if (ret)
288 return -EIO;
289
290 chip->config = config;
291
292 return len;
293 }
294
295 static IIO_DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR,
296 adt7310_show_resolution,
297 adt7310_store_resolution,
298 0);
299
300 static ssize_t adt7310_show_id(struct device *dev,
301 struct device_attribute *attr,
302 char *buf)
303 {
304 struct iio_dev *dev_info = dev_get_drvdata(dev);
305 struct adt7310_chip_info *chip = dev_info->dev_data;
306 u8 id;
307 int ret;
308
309 ret = adt7310_spi_read_byte(chip, ADT7310_ID, &id);
310 if (ret)
311 return -EIO;
312
313 return sprintf(buf, "device id: 0x%x\nmanufactory id: 0x%x\n",
314 id & ADT7310_DEVICE_ID_MASK,
315 (id & ADT7310_MANUFACTORY_ID_MASK) >> ADT7310_MANUFACTORY_ID_OFFSET);
316 }
317
318 static IIO_DEVICE_ATTR(id, S_IRUGO | S_IWUSR,
319 adt7310_show_id,
320 NULL,
321 0);
322
323 static ssize_t adt7310_convert_temperature(struct adt7310_chip_info *chip,
324 u16 data, char *buf)
325 {
326 char sign = ' ';
327
328 if (chip->config & ADT7310_RESOLUTION) {
329 if (data & ADT7310_T16_VALUE_SIGN) {
330 /* convert supplement to positive value */
331 data = (u16)((ADT7310_T16_VALUE_SIGN << 1) - (u32)data);
332 sign = '-';
333 }
334 return sprintf(buf, "%c%d.%.7d\n", sign,
335 (data >> ADT7310_T16_VALUE_FLOAT_OFFSET),
336 (data & ADT7310_T16_VALUE_FLOAT_MASK) * 78125);
337 } else {
338 if (data & ADT7310_T13_VALUE_SIGN) {
339 /* convert supplement to positive value */
340 data >>= ADT7310_T13_VALUE_OFFSET;
341 data = (ADT7310_T13_VALUE_SIGN << 1) - data;
342 sign = '-';
343 }
344 return sprintf(buf, "%c%d.%.4d\n", sign,
345 (data >> ADT7310_T13_VALUE_FLOAT_OFFSET),
346 (data & ADT7310_T13_VALUE_FLOAT_MASK) * 625);
347 }
348 }
349
350 static ssize_t adt7310_show_value(struct device *dev,
351 struct device_attribute *attr,
352 char *buf)
353 {
354 struct iio_dev *dev_info = dev_get_drvdata(dev);
355 struct adt7310_chip_info *chip = dev_info->dev_data;
356 u8 status;
357 u16 data;
358 int ret, i = 0;
359
360 do {
361 ret = adt7310_spi_read_byte(chip, ADT7310_STATUS, &status);
362 if (ret)
363 return -EIO;
364 i++;
365 if (i == 10000)
366 return -EIO;
367 } while (status & ADT7310_STAT_NOT_RDY);
368
369 ret = adt7310_spi_read_word(chip, ADT7310_TEMPERATURE, &data);
370 if (ret)
371 return -EIO;
372
373 return adt7310_convert_temperature(chip, data, buf);
374 }
375
376 static IIO_DEVICE_ATTR(value, S_IRUGO, adt7310_show_value, NULL, 0);
377
378 static ssize_t adt7310_show_name(struct device *dev,
379 struct device_attribute *attr,
380 char *buf)
381 {
382 struct iio_dev *dev_info = dev_get_drvdata(dev);
383 struct adt7310_chip_info *chip = dev_info->dev_data;
384 return sprintf(buf, "%s\n", chip->name);
385 }
386
387 static IIO_DEVICE_ATTR(name, S_IRUGO, adt7310_show_name, NULL, 0);
388
389 static struct attribute *adt7310_attributes[] = {
390 &iio_dev_attr_available_modes.dev_attr.attr,
391 &iio_dev_attr_mode.dev_attr.attr,
392 &iio_dev_attr_resolution.dev_attr.attr,
393 &iio_dev_attr_id.dev_attr.attr,
394 &iio_dev_attr_value.dev_attr.attr,
395 &iio_dev_attr_name.dev_attr.attr,
396 NULL,
397 };
398
399 static const struct attribute_group adt7310_attribute_group = {
400 .attrs = adt7310_attributes,
401 };
402
403 /*
404 * temperature bound events
405 */
406
407 #define IIO_EVENT_CODE_ADT7310_ABOVE_ALARM IIO_BUFFER_EVENT_CODE(0)
408 #define IIO_EVENT_CODE_ADT7310_BELLOW_ALARM IIO_BUFFER_EVENT_CODE(1)
409 #define IIO_EVENT_CODE_ADT7310_ABOVE_CRIT IIO_BUFFER_EVENT_CODE(2)
410
411 static irqreturn_t adt7310_event_handler(int irq, void *private)
412 {
413 struct iio_dev *indio_dev = private;
414 struct adt7310_chip_info *chip = iio_dev_get_devdata(indio_dev);
415 s64 timestamp = iio_get_time_ns();
416 u8 status;
417 int ret;
418
419 ret = adt7310_spi_read_byte(chip, ADT7310_STATUS, &status);
420 if (ret)
421 return ret;
422
423 if (status & ADT7310_STAT_T_HIGH)
424 iio_push_event(indio_dev, 0,
425 IIO_EVENT_CODE_ADT7310_ABOVE_ALARM,
426 timestamp);
427 if (status & ADT7310_STAT_T_LOW)
428 iio_push_event(indio_dev, 0,
429 IIO_EVENT_CODE_ADT7310_BELLOW_ALARM,
430 timestamp);
431 if (status & ADT7310_STAT_T_CRIT)
432 iio_push_event(indio_dev, 0,
433 IIO_EVENT_CODE_ADT7310_ABOVE_CRIT,
434 timestamp);
435 return IRQ_HANDLED;
436 }
437
438 static ssize_t adt7310_show_event_mode(struct device *dev,
439 struct device_attribute *attr,
440 char *buf)
441 {
442 struct iio_dev *dev_info = dev_get_drvdata(dev);
443 struct adt7310_chip_info *chip = dev_info->dev_data;
444 int ret;
445
446 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
447 if (ret)
448 return -EIO;
449
450 if (chip->config & ADT7310_EVENT_MODE)
451 return sprintf(buf, "interrupt\n");
452 else
453 return sprintf(buf, "comparator\n");
454 }
455
456 static ssize_t adt7310_set_event_mode(struct device *dev,
457 struct device_attribute *attr,
458 const char *buf,
459 size_t len)
460 {
461 struct iio_dev *dev_info = dev_get_drvdata(dev);
462 struct adt7310_chip_info *chip = dev_info->dev_data;
463 u16 config;
464 int ret;
465
466 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
467 if (ret)
468 return -EIO;
469
470 config = chip->config &= ~ADT7310_EVENT_MODE;
471 if (strcmp(buf, "comparator") != 0)
472 config |= ADT7310_EVENT_MODE;
473
474 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
475 if (ret)
476 return -EIO;
477
478 chip->config = config;
479
480 return len;
481 }
482
483 static ssize_t adt7310_show_available_event_modes(struct device *dev,
484 struct device_attribute *attr,
485 char *buf)
486 {
487 return sprintf(buf, "comparator\ninterrupt\n");
488 }
489
490 static ssize_t adt7310_show_fault_queue(struct device *dev,
491 struct device_attribute *attr,
492 char *buf)
493 {
494 struct iio_dev *dev_info = dev_get_drvdata(dev);
495 struct adt7310_chip_info *chip = dev_info->dev_data;
496 int ret;
497
498 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
499 if (ret)
500 return -EIO;
501
502 return sprintf(buf, "%d\n", chip->config & ADT7310_FAULT_QUEUE_MASK);
503 }
504
505 static ssize_t adt7310_set_fault_queue(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf,
508 size_t len)
509 {
510 struct iio_dev *dev_info = dev_get_drvdata(dev);
511 struct adt7310_chip_info *chip = dev_info->dev_data;
512 unsigned long data;
513 int ret;
514 u8 config;
515
516 ret = strict_strtoul(buf, 10, &data);
517 if (ret || data > 3)
518 return -EINVAL;
519
520 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
521 if (ret)
522 return -EIO;
523
524 config = chip->config & ~ADT7310_FAULT_QUEUE_MASK;
525 config |= data;
526 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
527 if (ret)
528 return -EIO;
529
530 chip->config = config;
531
532 return len;
533 }
534
535 static inline ssize_t adt7310_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 adt7310_chip_info *chip = dev_info->dev_data;
542 u16 data;
543 int ret;
544
545 ret = adt7310_spi_read_word(chip, bound_reg, &data);
546 if (ret)
547 return -EIO;
548
549 return adt7310_convert_temperature(chip, data, buf);
550 }
551
552 static inline ssize_t adt7310_set_t_bound(struct device *dev,
553 struct device_attribute *attr,
554 u8 bound_reg,
555 const char *buf,
556 size_t len)
557 {
558 struct iio_dev *dev_info = dev_get_drvdata(dev);
559 struct adt7310_chip_info *chip = dev_info->dev_data;
560 long tmp1, tmp2;
561 u16 data;
562 char *pos;
563 int ret;
564
565 pos = strchr(buf, '.');
566
567 ret = strict_strtol(buf, 10, &tmp1);
568
569 if (ret || tmp1 > 127 || tmp1 < -128)
570 return -EINVAL;
571
572 if (pos) {
573 len = strlen(pos);
574
575 if (chip->config & ADT7310_RESOLUTION) {
576 if (len > ADT7310_T16_VALUE_FLOAT_OFFSET)
577 len = ADT7310_T16_VALUE_FLOAT_OFFSET;
578 pos[len] = 0;
579 ret = strict_strtol(pos, 10, &tmp2);
580
581 if (!ret)
582 tmp2 = (tmp2 / 78125) * 78125;
583 } else {
584 if (len > ADT7310_T13_VALUE_FLOAT_OFFSET)
585 len = ADT7310_T13_VALUE_FLOAT_OFFSET;
586 pos[len] = 0;
587 ret = strict_strtol(pos, 10, &tmp2);
588
589 if (!ret)
590 tmp2 = (tmp2 / 625) * 625;
591 }
592 }
593
594 if (tmp1 < 0)
595 data = (u16)(-tmp1);
596 else
597 data = (u16)tmp1;
598
599 if (chip->config & ADT7310_RESOLUTION) {
600 data = (data << ADT7310_T16_VALUE_FLOAT_OFFSET) |
601 (tmp2 & ADT7310_T16_VALUE_FLOAT_MASK);
602
603 if (tmp1 < 0)
604 /* convert positive value to supplyment */
605 data = (u16)((ADT7310_T16_VALUE_SIGN << 1) - (u32)data);
606 } else {
607 data = (data << ADT7310_T13_VALUE_FLOAT_OFFSET) |
608 (tmp2 & ADT7310_T13_VALUE_FLOAT_MASK);
609
610 if (tmp1 < 0)
611 /* convert positive value to supplyment */
612 data = (ADT7310_T13_VALUE_SIGN << 1) - data;
613 data <<= ADT7310_T13_VALUE_OFFSET;
614 }
615
616 ret = adt7310_spi_write_word(chip, bound_reg, data);
617 if (ret)
618 return -EIO;
619
620 return len;
621 }
622
623 static ssize_t adt7310_show_t_alarm_high(struct device *dev,
624 struct device_attribute *attr,
625 char *buf)
626 {
627 return adt7310_show_t_bound(dev, attr,
628 ADT7310_T_ALARM_HIGH, buf);
629 }
630
631 static inline ssize_t adt7310_set_t_alarm_high(struct device *dev,
632 struct device_attribute *attr,
633 const char *buf,
634 size_t len)
635 {
636 return adt7310_set_t_bound(dev, attr,
637 ADT7310_T_ALARM_HIGH, buf, len);
638 }
639
640 static ssize_t adt7310_show_t_alarm_low(struct device *dev,
641 struct device_attribute *attr,
642 char *buf)
643 {
644 return adt7310_show_t_bound(dev, attr,
645 ADT7310_T_ALARM_LOW, buf);
646 }
647
648 static inline ssize_t adt7310_set_t_alarm_low(struct device *dev,
649 struct device_attribute *attr,
650 const char *buf,
651 size_t len)
652 {
653 return adt7310_set_t_bound(dev, attr,
654 ADT7310_T_ALARM_LOW, buf, len);
655 }
656
657 static ssize_t adt7310_show_t_crit(struct device *dev,
658 struct device_attribute *attr,
659 char *buf)
660 {
661 return adt7310_show_t_bound(dev, attr,
662 ADT7310_T_CRIT, buf);
663 }
664
665 static inline ssize_t adt7310_set_t_crit(struct device *dev,
666 struct device_attribute *attr,
667 const char *buf,
668 size_t len)
669 {
670 return adt7310_set_t_bound(dev, attr,
671 ADT7310_T_CRIT, buf, len);
672 }
673
674 static ssize_t adt7310_show_t_hyst(struct device *dev,
675 struct device_attribute *attr,
676 char *buf)
677 {
678 struct iio_dev *dev_info = dev_get_drvdata(dev);
679 struct adt7310_chip_info *chip = dev_info->dev_data;
680 int ret;
681 u8 t_hyst;
682
683 ret = adt7310_spi_read_byte(chip, ADT7310_T_HYST, &t_hyst);
684 if (ret)
685 return -EIO;
686
687 return sprintf(buf, "%d\n", t_hyst & ADT7310_T_HYST_MASK);
688 }
689
690 static inline ssize_t adt7310_set_t_hyst(struct device *dev,
691 struct device_attribute *attr,
692 const char *buf,
693 size_t len)
694 {
695 struct iio_dev *dev_info = dev_get_drvdata(dev);
696 struct adt7310_chip_info *chip = dev_info->dev_data;
697 int ret;
698 unsigned long data;
699 u8 t_hyst;
700
701 ret = strict_strtol(buf, 10, &data);
702
703 if (ret || data > ADT7310_T_HYST_MASK)
704 return -EINVAL;
705
706 t_hyst = (u8)data;
707
708 ret = adt7310_spi_write_byte(chip, ADT7310_T_HYST, t_hyst);
709 if (ret)
710 return -EIO;
711
712 return len;
713 }
714
715 static IIO_DEVICE_ATTR(event_mode,
716 S_IRUGO | S_IWUSR,
717 adt7310_show_event_mode, adt7310_set_event_mode, 0);
718 static IIO_DEVICE_ATTR(available_event_modes,
719 S_IRUGO | S_IWUSR,
720 adt7310_show_available_event_modes, NULL, 0);
721 static IIO_DEVICE_ATTR(fault_queue,
722 S_IRUGO | S_IWUSR,
723 adt7310_show_fault_queue, adt7310_set_fault_queue, 0);
724 static IIO_DEVICE_ATTR(t_alarm_high,
725 S_IRUGO | S_IWUSR,
726 adt7310_show_t_alarm_high, adt7310_set_t_alarm_high, 0);
727 static IIO_DEVICE_ATTR(t_alarm_low,
728 S_IRUGO | S_IWUSR,
729 adt7310_show_t_alarm_low, adt7310_set_t_alarm_low, 0);
730 static IIO_DEVICE_ATTR(t_crit,
731 S_IRUGO | S_IWUSR,
732 adt7310_show_t_crit, adt7310_set_t_crit, 0);
733 static IIO_DEVICE_ATTR(t_hyst,
734 S_IRUGO | S_IWUSR,
735 adt7310_show_t_hyst, adt7310_set_t_hyst, 0);
736
737 static struct attribute *adt7310_event_int_attributes[] = {
738 &iio_dev_attr_event_mode.dev_attr.attr,
739 &iio_dev_attr_available_event_modes.dev_attr.attr,
740 &iio_dev_attr_fault_queue.dev_attr.attr,
741 &iio_dev_attr_t_alarm_high.dev_attr.attr,
742 &iio_dev_attr_t_alarm_low.dev_attr.attr,
743 &iio_dev_attr_t_hyst.dev_attr.attr,
744 NULL,
745 };
746
747 static struct attribute *adt7310_event_ct_attributes[] = {
748 &iio_dev_attr_event_mode.dev_attr.attr,
749 &iio_dev_attr_available_event_modes.dev_attr.attr,
750 &iio_dev_attr_fault_queue.dev_attr.attr,
751 &iio_dev_attr_t_crit.dev_attr.attr,
752 &iio_dev_attr_t_hyst.dev_attr.attr,
753 NULL,
754 };
755
756 static struct attribute_group adt7310_event_attribute_group[ADT7310_IRQS] = {
757 {
758 .attrs = adt7310_event_int_attributes,
759 }, {
760 .attrs = adt7310_event_ct_attributes,
761 }
762 };
763
764 /*
765 * device probe and remove
766 */
767
768 static int __devinit adt7310_probe(struct spi_device *spi_dev)
769 {
770 struct adt7310_chip_info *chip;
771 int ret = 0;
772 unsigned long *adt7310_platform_data = spi_dev->dev.platform_data;
773 unsigned long irq_flags;
774
775 chip = kzalloc(sizeof(struct adt7310_chip_info), GFP_KERNEL);
776
777 if (chip == NULL)
778 return -ENOMEM;
779
780 /* this is only used for device removal purposes */
781 dev_set_drvdata(&spi_dev->dev, chip);
782
783 chip->spi_dev = spi_dev;
784 chip->name = spi_dev->modalias;
785
786 chip->indio_dev = iio_allocate_device(0);
787 if (chip->indio_dev == NULL) {
788 ret = -ENOMEM;
789 goto error_free_chip;
790 }
791
792 chip->indio_dev->dev.parent = &spi_dev->dev;
793 chip->indio_dev->attrs = &adt7310_attribute_group;
794 chip->indio_dev->event_attrs = adt7310_event_attribute_group;
795 chip->indio_dev->dev_data = (void *)chip;
796 chip->indio_dev->driver_module = THIS_MODULE;
797 chip->indio_dev->num_interrupt_lines = ADT7310_IRQS;
798 chip->indio_dev->modes = INDIO_DIRECT_MODE;
799
800 ret = iio_device_register(chip->indio_dev);
801 if (ret)
802 goto error_free_dev;
803
804 /* CT critcal temperature event. line 0 */
805 if (spi_dev->irq) {
806 if (adt7310_platform_data[2])
807 irq_flags = adt7310_platform_data[2];
808 else
809 irq_flags = IRQF_TRIGGER_LOW;
810 ret = request_threaded_irq(spi_dev->irq,
811 NULL,
812 &adt7310_event_handler,
813 irq_flags,
814 chip->name,
815 chip->indio_dev);
816 if (ret)
817 goto error_unreg_dev;
818 }
819
820 /* INT bound temperature alarm event. line 1 */
821 if (adt7310_platform_data[0]) {
822 ret = request_threaded_irq(adt7310_platform_data[0],
823 NULL,
824 &adt7310_event_handler,
825 adt7310_platform_data[1],
826 chip->name,
827 chip->indio_dev);
828 if (ret)
829 goto error_unreg_ct_irq;
830 }
831
832 if (spi_dev->irq && adt7310_platform_data[0]) {
833 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
834 if (ret) {
835 ret = -EIO;
836 goto error_unreg_int_irq;
837 }
838
839 /* set irq polarity low level */
840 chip->config &= ~ADT7310_CT_POLARITY;
841
842 if (adt7310_platform_data[1] & IRQF_TRIGGER_HIGH)
843 chip->config |= ADT7310_INT_POLARITY;
844 else
845 chip->config &= ~ADT7310_INT_POLARITY;
846
847 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, chip->config);
848 if (ret) {
849 ret = -EIO;
850 goto error_unreg_int_irq;
851 }
852 }
853
854 dev_info(&spi_dev->dev, "%s temperature sensor registered.\n",
855 chip->name);
856
857 return 0;
858
859 error_unreg_int_irq:
860 free_irq(adt7310_platform_data[0], chip->indio_dev);
861 error_unreg_ct_irq:
862 free_irq(spi_dev->irq, chip->indio_dev);
863 error_unreg_dev:
864 iio_device_unregister(chip->indio_dev);
865 error_free_dev:
866 iio_free_device(chip->indio_dev);
867 error_free_chip:
868 kfree(chip);
869
870 return ret;
871 }
872
873 static int __devexit adt7310_remove(struct spi_device *spi_dev)
874 {
875 struct adt7310_chip_info *chip = dev_get_drvdata(&spi_dev->dev);
876 struct iio_dev *indio_dev = chip->indio_dev;
877 unsigned long *adt7310_platform_data = spi_dev->dev.platform_data;
878
879 dev_set_drvdata(&spi_dev->dev, NULL);
880 if (adt7310_platform_data[0])
881 free_irq(adt7310_platform_data[0], chip->indio_dev);
882 if (spi_dev->irq)
883 free_irq(spi_dev->irq, chip->indio_dev);
884 iio_device_unregister(indio_dev);
885 iio_free_device(chip->indio_dev);
886 kfree(chip);
887
888 return 0;
889 }
890
891 static const struct spi_device_id adt7310_id[] = {
892 { "adt7310", 0 },
893 {}
894 };
895
896 MODULE_DEVICE_TABLE(spi, adt7310_id);
897
898 static struct spi_driver adt7310_driver = {
899 .driver = {
900 .name = "adt7310",
901 .bus = &spi_bus_type,
902 .owner = THIS_MODULE,
903 },
904 .probe = adt7310_probe,
905 .remove = __devexit_p(adt7310_remove),
906 .id_table = adt7310_id,
907 };
908
909 static __init int adt7310_init(void)
910 {
911 return spi_register_driver(&adt7310_driver);
912 }
913
914 static __exit void adt7310_exit(void)
915 {
916 spi_unregister_driver(&adt7310_driver);
917 }
918
919 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
920 MODULE_DESCRIPTION("Analog Devices ADT7310 digital"
921 " temperature sensor driver");
922 MODULE_LICENSE("GPL v2");
923
924 module_init(adt7310_init);
925 module_exit(adt7310_exit);