Free Electrons

Embedded Linux Experts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*
 * ADT7410 digital temperature sensor driver supporting ADT7410
 *
 * Copyright 2010 Analog Devices Inc.
 *
 * Licensed under the GPL-2 or later.
 */

#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/list.h>
#include <linux/i2c.h>
#include <linux/module.h>

#include "../iio.h"
#include "../sysfs.h"
#include "../events.h"

/*
 * ADT7410 registers definition
 */

#define ADT7410_TEMPERATURE		0
#define ADT7410_STATUS			2
#define ADT7410_CONFIG			3
#define ADT7410_T_ALARM_HIGH		4
#define ADT7410_T_ALARM_LOW		6
#define ADT7410_T_CRIT			8
#define ADT7410_T_HYST			0xA
#define ADT7410_ID			0xB
#define ADT7410_RESET			0x2F

/*
 * ADT7410 status
 */
#define ADT7410_STAT_T_LOW		0x10
#define ADT7410_STAT_T_HIGH		0x20
#define ADT7410_STAT_T_CRIT		0x40
#define ADT7410_STAT_NOT_RDY		0x80

/*
 * ADT7410 config
 */
#define ADT7410_FAULT_QUEUE_MASK	0x3
#define ADT7410_CT_POLARITY		0x4
#define ADT7410_INT_POLARITY		0x8
#define ADT7410_EVENT_MODE		0x10
#define ADT7410_MODE_MASK		0x60
#define ADT7410_ONESHOT			0x20
#define ADT7410_SPS			0x40
#define ADT7410_PD			0x60
#define ADT7410_RESOLUTION		0x80

/*
 * ADT7410 masks
 */
#define ADT7410_T16_VALUE_SIGN			0x8000
#define ADT7410_T16_VALUE_FLOAT_OFFSET		7
#define ADT7410_T16_VALUE_FLOAT_MASK		0x7F
#define ADT7410_T13_VALUE_SIGN			0x1000
#define ADT7410_T13_VALUE_OFFSET		3
#define ADT7410_T13_VALUE_FLOAT_OFFSET		4
#define ADT7410_T13_VALUE_FLOAT_MASK		0xF
#define ADT7410_T_HYST_MASK			0xF
#define ADT7410_DEVICE_ID_MASK			0xF
#define ADT7410_MANUFACTORY_ID_MASK		0xF0
#define ADT7410_MANUFACTORY_ID_OFFSET		4

#define ADT7410_IRQS				2

/*
 * struct adt7410_chip_info - chip specifc information
 */

struct adt7410_chip_info {
	struct i2c_client *client;
	u8  config;
};

/*
 * adt7410 register access by I2C
 */

static int adt7410_i2c_read_word(struct adt7410_chip_info *chip, u8 reg, u16 *data)
{
	struct i2c_client *client = chip->client;
	int ret = 0;

	ret = i2c_smbus_read_word_data(client, reg);
	if (ret < 0) {
		dev_err(&client->dev, "I2C read error\n");
		return ret;
	}

	*data = swab16((u16)ret);

	return 0;
}

static int adt7410_i2c_write_word(struct adt7410_chip_info *chip, u8 reg, u16 data)
{
	struct i2c_client *client = chip->client;
	int ret = 0;

	ret = i2c_smbus_write_word_data(client, reg, swab16(data));
	if (ret < 0)
		dev_err(&client->dev, "I2C write error\n");

	return ret;
}

static int adt7410_i2c_read_byte(struct adt7410_chip_info *chip, u8 reg, u8 *data)
{
	struct i2c_client *client = chip->client;
	int ret = 0;

	ret = i2c_smbus_read_byte_data(client, reg);
	if (ret < 0) {
		dev_err(&client->dev, "I2C read error\n");
		return ret;
	}

	*data = (u8)ret;

	return 0;
}

static int adt7410_i2c_write_byte(struct adt7410_chip_info *chip, u8 reg, u8 data)
{
	struct i2c_client *client = chip->client;
	int ret = 0;

	ret = i2c_smbus_write_byte_data(client, reg, data);
	if (ret < 0)
		dev_err(&client->dev, "I2C write error\n");

	return ret;
}

static ssize_t adt7410_show_mode(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	u8 config;

	config = chip->config & ADT7410_MODE_MASK;

	switch (config) {
	case ADT7410_PD:
		return sprintf(buf, "power-down\n");
	case ADT7410_ONESHOT:
		return sprintf(buf, "one-shot\n");
	case ADT7410_SPS:
		return sprintf(buf, "sps\n");
	default:
		return sprintf(buf, "full\n");
	}
}

static ssize_t adt7410_store_mode(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	u16 config;
	int ret;

	ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
	if (ret)
		return -EIO;

	config = chip->config & (~ADT7410_MODE_MASK);
	if (strcmp(buf, "power-down"))
		config |= ADT7410_PD;
	else if (strcmp(buf, "one-shot"))
		config |= ADT7410_ONESHOT;
	else if (strcmp(buf, "sps"))
		config |= ADT7410_SPS;

	ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
	if (ret)
		return -EIO;

	chip->config = config;

	return ret;
}

static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
		adt7410_show_mode,
		adt7410_store_mode,
		0);

static ssize_t adt7410_show_available_modes(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	return sprintf(buf, "full\none-shot\nsps\npower-down\n");
}

static IIO_DEVICE_ATTR(available_modes, S_IRUGO, adt7410_show_available_modes, NULL, 0);

static ssize_t adt7410_show_resolution(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	int ret;
	int bits;

	ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
	if (ret)
		return -EIO;

	if (chip->config & ADT7410_RESOLUTION)
		bits = 16;
	else
		bits = 13;

	return sprintf(buf, "%d bits\n", bits);
}

static ssize_t adt7410_store_resolution(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	unsigned long data;
	u16 config;
	int ret;

	ret = strict_strtoul(buf, 10, &data);
	if (ret)
		return -EINVAL;

	ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
	if (ret)
		return -EIO;

	config = chip->config & (~ADT7410_RESOLUTION);
	if (data)
		config |= ADT7410_RESOLUTION;

	ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
	if (ret)
		return -EIO;

	chip->config = config;

	return ret;
}

static IIO_DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR,
		adt7410_show_resolution,
		adt7410_store_resolution,
		0);

static ssize_t adt7410_show_id(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	u8 id;
	int ret;

	ret = adt7410_i2c_read_byte(chip, ADT7410_ID, &id);
	if (ret)
		return -EIO;

	return sprintf(buf, "device id: 0x%x\nmanufactory id: 0x%x\n",
			id & ADT7410_DEVICE_ID_MASK,
			(id & ADT7410_MANUFACTORY_ID_MASK) >> ADT7410_MANUFACTORY_ID_OFFSET);
}

static IIO_DEVICE_ATTR(id, S_IRUGO | S_IWUSR,
		adt7410_show_id,
		NULL,
		0);

static ssize_t adt7410_convert_temperature(struct adt7410_chip_info *chip,
		u16 data, char *buf)
{
	char sign = ' ';

	if (chip->config & ADT7410_RESOLUTION) {
		if (data & ADT7410_T16_VALUE_SIGN) {
			/* convert supplement to positive value */
			data = (u16)((ADT7410_T16_VALUE_SIGN << 1) - (u32)data);
			sign = '-';
		}
		return sprintf(buf, "%c%d.%.7d\n", sign,
				(data >> ADT7410_T16_VALUE_FLOAT_OFFSET),
				(data & ADT7410_T16_VALUE_FLOAT_MASK) * 78125);
	} else {
		if (data & ADT7410_T13_VALUE_SIGN) {
			/* convert supplement to positive value */
			data >>= ADT7410_T13_VALUE_OFFSET;
			data = (ADT7410_T13_VALUE_SIGN << 1) - data;
			sign = '-';
		}
		return sprintf(buf, "%c%d.%.4d\n", sign,
				(data >> ADT7410_T13_VALUE_FLOAT_OFFSET),
				(data & ADT7410_T13_VALUE_FLOAT_MASK) * 625);
	}
}

static ssize_t adt7410_show_value(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	u8 status;
	u16 data;
	int ret, i = 0;

	do {
		ret = adt7410_i2c_read_byte(chip, ADT7410_STATUS, &status);
		if (ret)
			return -EIO;
		i++;
		if (i == 10000)
			return -EIO;
	} while (status & ADT7410_STAT_NOT_RDY);

	ret = adt7410_i2c_read_word(chip, ADT7410_TEMPERATURE, &data);
	if (ret)
		return -EIO;

	return adt7410_convert_temperature(chip, data, buf);
}

static IIO_DEVICE_ATTR(value, S_IRUGO, adt7410_show_value, NULL, 0);

static struct attribute *adt7410_attributes[] = {
	&iio_dev_attr_available_modes.dev_attr.attr,
	&iio_dev_attr_mode.dev_attr.attr,
	&iio_dev_attr_resolution.dev_attr.attr,
	&iio_dev_attr_id.dev_attr.attr,
	&iio_dev_attr_value.dev_attr.attr,
	NULL,
};

static const struct attribute_group adt7410_attribute_group = {
	.attrs = adt7410_attributes,
};

static irqreturn_t adt7410_event_handler(int irq, void *private)
{
	struct iio_dev *indio_dev = private;
	struct adt7410_chip_info *chip = iio_priv(indio_dev);
	s64 timestamp = iio_get_time_ns();
	u8 status;

	if (adt7410_i2c_read_byte(chip, ADT7410_STATUS, &status))
		return IRQ_HANDLED;

	if (status & ADT7410_STAT_T_HIGH)
		iio_push_event(indio_dev,
			       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_RISING),
			       timestamp);
	if (status & ADT7410_STAT_T_LOW)
		iio_push_event(indio_dev,
			       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_FALLING),
			       timestamp);
	if (status & ADT7410_STAT_T_CRIT)
		iio_push_event(indio_dev,
			       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_RISING),
			       timestamp);

	return IRQ_HANDLED;
}

static ssize_t adt7410_show_event_mode(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	int ret;

	ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
	if (ret)
		return -EIO;

	if (chip->config & ADT7410_EVENT_MODE)
		return sprintf(buf, "interrupt\n");
	else
		return sprintf(buf, "comparator\n");
}

static ssize_t adt7410_set_event_mode(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	u16 config;
	int ret;

	ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
	if (ret)
		return -EIO;

	config = chip->config &= ~ADT7410_EVENT_MODE;
	if (strcmp(buf, "comparator") != 0)
		config |= ADT7410_EVENT_MODE;

	ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
	if (ret)
		return -EIO;

	chip->config = config;

	return ret;
}

static ssize_t adt7410_show_available_event_modes(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	return sprintf(buf, "comparator\ninterrupt\n");
}

static ssize_t adt7410_show_fault_queue(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	int ret;

	ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
	if (ret)
		return -EIO;

	return sprintf(buf, "%d\n", chip->config & ADT7410_FAULT_QUEUE_MASK);
}

static ssize_t adt7410_set_fault_queue(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	unsigned long data;
	int ret;
	u8 config;

	ret = strict_strtoul(buf, 10, &data);
	if (ret || data > 3)
		return -EINVAL;

	ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
	if (ret)
		return -EIO;

	config = chip->config & ~ADT7410_FAULT_QUEUE_MASK;
	config |= data;
	ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
	if (ret)
		return -EIO;

	chip->config = config;

	return ret;
}

static inline ssize_t adt7410_show_t_bound(struct device *dev,
		struct device_attribute *attr,
		u8 bound_reg,
		char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	u16 data;
	int ret;

	ret = adt7410_i2c_read_word(chip, bound_reg, &data);
	if (ret)
		return -EIO;

	return adt7410_convert_temperature(chip, data, buf);
}

static inline ssize_t adt7410_set_t_bound(struct device *dev,
		struct device_attribute *attr,
		u8 bound_reg,
		const char *buf,
		size_t len)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	long tmp1, tmp2;
	u16 data;
	char *pos;
	int ret;

	pos = strchr(buf, '.');

	ret = strict_strtol(buf, 10, &tmp1);

	if (ret || tmp1 > 127 || tmp1 < -128)
		return -EINVAL;

	if (pos) {
		len = strlen(pos);

		if (chip->config & ADT7410_RESOLUTION) {
			if (len > ADT7410_T16_VALUE_FLOAT_OFFSET)
				len = ADT7410_T16_VALUE_FLOAT_OFFSET;
			pos[len] = 0;
			ret = strict_strtol(pos, 10, &tmp2);

			if (!ret)
				tmp2 = (tmp2 / 78125) * 78125;
		} else {
			if (len > ADT7410_T13_VALUE_FLOAT_OFFSET)
				len = ADT7410_T13_VALUE_FLOAT_OFFSET;
			pos[len] = 0;
			ret = strict_strtol(pos, 10, &tmp2);

			if (!ret)
				tmp2 = (tmp2 / 625) * 625;
		}
	}

	if (tmp1 < 0)
		data = (u16)(-tmp1);
	else
		data = (u16)tmp1;

	if (chip->config & ADT7410_RESOLUTION) {
		data = (data << ADT7410_T16_VALUE_FLOAT_OFFSET) |
			(tmp2 & ADT7410_T16_VALUE_FLOAT_MASK);

		if (tmp1 < 0)
			/* convert positive value to supplyment */
			data = (u16)((ADT7410_T16_VALUE_SIGN << 1) - (u32)data);
	} else {
		data = (data << ADT7410_T13_VALUE_FLOAT_OFFSET) |
			(tmp2 & ADT7410_T13_VALUE_FLOAT_MASK);

		if (tmp1 < 0)
			/* convert positive value to supplyment */
			data = (ADT7410_T13_VALUE_SIGN << 1) - data;
		data <<= ADT7410_T13_VALUE_OFFSET;
	}

	ret = adt7410_i2c_write_word(chip, bound_reg, data);
	if (ret)
		return -EIO;

	return ret;
}

static ssize_t adt7410_show_t_alarm_high(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	return adt7410_show_t_bound(dev, attr,
			ADT7410_T_ALARM_HIGH, buf);
}

static inline ssize_t adt7410_set_t_alarm_high(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
	return adt7410_set_t_bound(dev, attr,
			ADT7410_T_ALARM_HIGH, buf, len);
}

static ssize_t adt7410_show_t_alarm_low(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	return adt7410_show_t_bound(dev, attr,
			ADT7410_T_ALARM_LOW, buf);
}

static inline ssize_t adt7410_set_t_alarm_low(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
	return adt7410_set_t_bound(dev, attr,
			ADT7410_T_ALARM_LOW, buf, len);
}

static ssize_t adt7410_show_t_crit(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	return adt7410_show_t_bound(dev, attr,
			ADT7410_T_CRIT, buf);
}

static inline ssize_t adt7410_set_t_crit(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
	return adt7410_set_t_bound(dev, attr,
			ADT7410_T_CRIT, buf, len);
}

static ssize_t adt7410_show_t_hyst(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	int ret;
	u8 t_hyst;

	ret = adt7410_i2c_read_byte(chip, ADT7410_T_HYST, &t_hyst);
	if (ret)
		return -EIO;

	return sprintf(buf, "%d\n", t_hyst & ADT7410_T_HYST_MASK);
}

static inline ssize_t adt7410_set_t_hyst(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct adt7410_chip_info *chip = iio_priv(dev_info);
	int ret;
	unsigned long data;
	u8 t_hyst;

	ret = strict_strtol(buf, 10, &data);

	if (ret || data > ADT7410_T_HYST_MASK)
		return -EINVAL;

	t_hyst = (u8)data;

	ret = adt7410_i2c_write_byte(chip, ADT7410_T_HYST, t_hyst);
	if (ret)
		return -EIO;

	return ret;
}

static IIO_DEVICE_ATTR(event_mode,
		       S_IRUGO | S_IWUSR,
		       adt7410_show_event_mode, adt7410_set_event_mode, 0);
static IIO_DEVICE_ATTR(available_event_modes,
		       S_IRUGO,
		       adt7410_show_available_event_modes, NULL, 0);
static IIO_DEVICE_ATTR(fault_queue,
		       S_IRUGO | S_IWUSR,
		       adt7410_show_fault_queue, adt7410_set_fault_queue, 0);
static IIO_DEVICE_ATTR(t_alarm_high,
		       S_IRUGO | S_IWUSR,
		       adt7410_show_t_alarm_high, adt7410_set_t_alarm_high, 0);
static IIO_DEVICE_ATTR(t_alarm_low,
		       S_IRUGO | S_IWUSR,
		       adt7410_show_t_alarm_low, adt7410_set_t_alarm_low, 0);
static IIO_DEVICE_ATTR(t_crit,
		       S_IRUGO | S_IWUSR,
		       adt7410_show_t_crit, adt7410_set_t_crit, 0);
static IIO_DEVICE_ATTR(t_hyst,
		       S_IRUGO | S_IWUSR,
		       adt7410_show_t_hyst, adt7410_set_t_hyst, 0);

static struct attribute *adt7410_event_int_attributes[] = {
	&iio_dev_attr_event_mode.dev_attr.attr,
	&iio_dev_attr_available_event_modes.dev_attr.attr,
	&iio_dev_attr_fault_queue.dev_attr.attr,
	&iio_dev_attr_t_alarm_high.dev_attr.attr,
	&iio_dev_attr_t_alarm_low.dev_attr.attr,
	&iio_dev_attr_t_crit.dev_attr.attr,
	&iio_dev_attr_t_hyst.dev_attr.attr,
	NULL,
};

static struct attribute_group adt7410_event_attribute_group = {
	.attrs = adt7410_event_int_attributes,
	.name = "events",
};

static const struct iio_info adt7410_info = {
	.attrs = &adt7410_attribute_group,
	.event_attrs = &adt7410_event_attribute_group,
	.driver_module = THIS_MODULE,
};

/*
 * device probe and remove
 */

static int __devinit adt7410_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	struct adt7410_chip_info *chip;
	struct iio_dev *indio_dev;
	int ret = 0;
	unsigned long *adt7410_platform_data = client->dev.platform_data;

	indio_dev = iio_allocate_device(sizeof(*chip));
	if (indio_dev == NULL) {
		ret = -ENOMEM;
		goto error_ret;
	}
	chip = iio_priv(indio_dev);
	/* this is only used for device removal purposes */
	i2c_set_clientdata(client, indio_dev);

	chip->client = client;

	indio_dev->name = id->name;
	indio_dev->dev.parent = &client->dev;
	indio_dev->info = &adt7410_info;
	indio_dev->modes = INDIO_DIRECT_MODE;

	/* CT critcal temperature event. line 0 */
	if (client->irq) {
		ret = request_threaded_irq(client->irq,
					   NULL,
					   &adt7410_event_handler,
					   IRQF_TRIGGER_LOW,
					   id->name,
					   indio_dev);
		if (ret)
			goto error_free_dev;
	}

	/* INT bound temperature alarm event. line 1 */
	if (adt7410_platform_data[0]) {
		ret = request_threaded_irq(adt7410_platform_data[0],
					   NULL,
					   &adt7410_event_handler,
					   adt7410_platform_data[1],
					   id->name,
					   indio_dev);
		if (ret)
			goto error_unreg_ct_irq;
	}

	if (client->irq && adt7410_platform_data[0]) {

		ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
		if (ret) {
			ret = -EIO;
			goto error_unreg_int_irq;
		}

		/* set irq polarity low level */
		chip->config &= ~ADT7410_CT_POLARITY;

		if (adt7410_platform_data[1] & IRQF_TRIGGER_HIGH)
			chip->config |= ADT7410_INT_POLARITY;
		else
			chip->config &= ~ADT7410_INT_POLARITY;

		ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, chip->config);
		if (ret) {
			ret = -EIO;
			goto error_unreg_int_irq;
		}
	}
	ret = iio_device_register(indio_dev);
	if (ret)
		goto error_unreg_int_irq;

	dev_info(&client->dev, "%s temperature sensor registered.\n",
			 id->name);

	return 0;

error_unreg_int_irq:
	free_irq(adt7410_platform_data[0], indio_dev);
error_unreg_ct_irq:
	free_irq(client->irq, indio_dev);
error_free_dev:
	iio_free_device(indio_dev);
error_ret:
	return ret;
}

static int __devexit adt7410_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	unsigned long *adt7410_platform_data = client->dev.platform_data;

	iio_device_unregister(indio_dev);
	if (adt7410_platform_data[0])
		free_irq(adt7410_platform_data[0], indio_dev);
	if (client->irq)
		free_irq(client->irq, indio_dev);
	iio_free_device(indio_dev);

	return 0;
}

static const struct i2c_device_id adt7410_id[] = {
	{ "adt7410", 0 },
	{}
};

MODULE_DEVICE_TABLE(i2c, adt7410_id);

static struct i2c_driver adt7410_driver = {
	.driver = {
		.name = "adt7410",
	},
	.probe = adt7410_probe,
	.remove = __devexit_p(adt7410_remove),
	.id_table = adt7410_id,
};
module_i2c_driver(adt7410_driver);

MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
MODULE_DESCRIPTION("Analog Devices ADT7410 digital"
			" temperature sensor driver");
MODULE_LICENSE("GPL v2");