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