summaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/adt7473.c
blob: ce4a7cb5a116ad59365aa846921926a92bd5fa17 (plain)
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
/*
 * A hwmon driver for the Analog Devices ADT7473
 * Copyright (C) 2007 IBM
 *
 * Author: Darrick J. Wong <djwong@us.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/module.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/log2.h>

/* Addresses to scan */
static const unsigned short normal_i2c[] = { 0x2C, 0x2D, 0x2E, I2C_CLIENT_END };

/* Insmod parameters */
I2C_CLIENT_INSMOD_1(adt7473);

/* ADT7473 registers */
#define ADT7473_REG_BASE_ADDR			0x20

#define ADT7473_REG_VOLT_BASE_ADDR		0x21
#define ADT7473_REG_VOLT_MAX_ADDR		0x22
#define ADT7473_REG_VOLT_MIN_BASE_ADDR		0x46
#define ADT7473_REG_VOLT_MIN_MAX_ADDR		0x49

#define ADT7473_REG_TEMP_BASE_ADDR		0x25
#define ADT7473_REG_TEMP_MAX_ADDR		0x27
#define ADT7473_REG_TEMP_LIMITS_BASE_ADDR	0x4E
#define ADT7473_REG_TEMP_LIMITS_MAX_ADDR	0x53
#define ADT7473_REG_TEMP_TMIN_BASE_ADDR		0x67
#define ADT7473_REG_TEMP_TMIN_MAX_ADDR		0x69
#define ADT7473_REG_TEMP_TMAX_BASE_ADDR		0x6A
#define ADT7473_REG_TEMP_TMAX_MAX_ADDR		0x6C

#define ADT7473_REG_FAN_BASE_ADDR		0x28
#define ADT7473_REG_FAN_MAX_ADDR		0x2F
#define ADT7473_REG_FAN_MIN_BASE_ADDR		0x54
#define ADT7473_REG_FAN_MIN_MAX_ADDR		0x5B

#define ADT7473_REG_PWM_BASE_ADDR		0x30
#define ADT7473_REG_PWM_MAX_ADDR		0x32
#define	ADT7473_REG_PWM_MIN_BASE_ADDR		0x64
#define ADT7473_REG_PWM_MIN_MAX_ADDR		0x66
#define ADT7473_REG_PWM_MAX_BASE_ADDR		0x38
#define ADT7473_REG_PWM_MAX_MAX_ADDR		0x3A
#define ADT7473_REG_PWM_BHVR_BASE_ADDR		0x5C
#define ADT7473_REG_PWM_BHVR_MAX_ADDR		0x5E
#define		ADT7473_PWM_BHVR_MASK		0xE0
#define		ADT7473_PWM_BHVR_SHIFT		5

#define ADT7473_REG_CFG1			0x40
#define 	ADT7473_CFG1_START		0x01
#define		ADT7473_CFG1_READY		0x04
#define ADT7473_REG_CFG2			0x73
#define ADT7473_REG_CFG3			0x78
#define ADT7473_REG_CFG4			0x7D
#define		ADT7473_CFG4_MAX_DUTY_AT_OVT	0x08
#define ADT7473_REG_CFG5			0x7C
#define		ADT7473_CFG5_TEMP_TWOS		0x01
#define		ADT7473_CFG5_TEMP_OFFSET	0x02

#define ADT7473_REG_DEVICE			0x3D
#define 	ADT7473_VENDOR			0x41
#define ADT7473_REG_VENDOR			0x3E
#define 	ADT7473_DEVICE			0x73
#define ADT7473_REG_REVISION			0x3F
#define 	ADT7473_REV_68			0x68
#define 	ADT7473_REV_69			0x69

#define ADT7473_REG_ALARM1			0x41
#define		ADT7473_VCCP_ALARM		0x02
#define		ADT7473_VCC_ALARM		0x04
#define		ADT7473_R1T_ALARM		0x10
#define		ADT7473_LT_ALARM		0x20
#define		ADT7473_R2T_ALARM		0x40
#define		ADT7473_OOL			0x80
#define ADT7473_REG_ALARM2			0x42
#define		ADT7473_OVT_ALARM		0x02
#define		ADT7473_FAN1_ALARM		0x04
#define		ADT7473_FAN2_ALARM		0x08
#define		ADT7473_FAN3_ALARM		0x10
#define		ADT7473_FAN4_ALARM		0x20
#define		ADT7473_R1T_SHORT		0x40
#define		ADT7473_R2T_SHORT		0x80
#define ADT7473_REG_MAX_ADDR			0x80

#define ALARM2(x)	((x) << 8)

#define ADT7473_VOLT_COUNT	2
#define ADT7473_REG_VOLT(x)	(ADT7473_REG_VOLT_BASE_ADDR + (x))
#define ADT7473_REG_VOLT_MIN(x)	(ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2))
#define ADT7473_REG_VOLT_MAX(x)	(ADT7473_REG_VOLT_MIN_BASE_ADDR + \
				((x) * 2) + 1)

#define ADT7473_TEMP_COUNT	3
#define ADT7473_REG_TEMP(x)	(ADT7473_REG_TEMP_BASE_ADDR + (x))
#define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
#define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \
				((x) * 2) + 1)
#define ADT7473_REG_TEMP_TMIN(x)	(ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x))
#define ADT7473_REG_TEMP_TMAX(x)	(ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x))

#define ADT7473_FAN_COUNT	4
#define ADT7473_REG_FAN(x)	(ADT7473_REG_FAN_BASE_ADDR + ((x) * 2))
#define ADT7473_REG_FAN_MIN(x)	(ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2))

#define ADT7473_PWM_COUNT	3
#define ADT7473_REG_PWM(x)	(ADT7473_REG_PWM_BASE_ADDR + (x))
#define ADT7473_REG_PWM_MAX(x)	(ADT7473_REG_PWM_MAX_BASE_ADDR + (x))
#define ADT7473_REG_PWM_MIN(x)	(ADT7473_REG_PWM_MIN_BASE_ADDR + (x))
#define ADT7473_REG_PWM_BHVR(x)	(ADT7473_REG_PWM_BHVR_BASE_ADDR + (x))

/* How often do we reread sensors values? (In jiffies) */
#define SENSOR_REFRESH_INTERVAL	(2 * HZ)

/* How often do we reread sensor limit values? (In jiffies) */
#define LIMIT_REFRESH_INTERVAL	(60 * HZ)

/* datasheet says to divide this number by the fan reading to get fan rpm */
#define FAN_PERIOD_TO_RPM(x)	((90000 * 60) / (x))
#define FAN_RPM_TO_PERIOD	FAN_PERIOD_TO_RPM
#define FAN_PERIOD_INVALID	65535
#define FAN_DATA_VALID(x)	((x) && (x) != FAN_PERIOD_INVALID)

struct adt7473_data {
	struct device		*hwmon_dev;
	struct attribute_group	attrs;
	struct mutex		lock;
	char			sensors_valid;
	char			limits_valid;
	unsigned long		sensors_last_updated;	/* In jiffies */
	unsigned long		limits_last_updated;	/* In jiffies */

	u8			volt[ADT7473_VOLT_COUNT];
	s8			volt_min[ADT7473_VOLT_COUNT];
	s8			volt_max[ADT7473_VOLT_COUNT];

	s8			temp[ADT7473_TEMP_COUNT];
	s8			temp_min[ADT7473_TEMP_COUNT];
	s8			temp_max[ADT7473_TEMP_COUNT];
	s8			temp_tmin[ADT7473_TEMP_COUNT];
	/* This is called the !THERM limit in the datasheet */
	s8			temp_tmax[ADT7473_TEMP_COUNT];

	u16			fan[ADT7473_FAN_COUNT];
	u16			fan_min[ADT7473_FAN_COUNT];

	u8			pwm[ADT7473_PWM_COUNT];
	u8			pwm_max[ADT7473_PWM_COUNT];
	u8			pwm_min[ADT7473_PWM_COUNT];
	u8			pwm_behavior[ADT7473_PWM_COUNT];

	u8			temp_twos_complement;
	u8			temp_offset;

	u16			alarm;
	u8			max_duty_at_overheat;
};

static int adt7473_probe(struct i2c_client *client,
			 const struct i2c_device_id *id);
static int adt7473_detect(struct i2c_client *client, int kind,
			  struct i2c_board_info *info);
static int adt7473_remove(struct i2c_client *client);

static const struct i2c_device_id adt7473_id[] = {
	{ "adt7473", adt7473 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, adt7473_id);

static struct i2c_driver adt7473_driver = {
	.class		= I2C_CLASS_HWMON,
	.driver = {
		.name	= "adt7473",
	},
	.probe		= adt7473_probe,
	.remove		= adt7473_remove,
	.id_table	= adt7473_id,
	.detect		= adt7473_detect,
	.address_data	= &addr_data,
};

/*
 * 16-bit registers on the ADT7473 are low-byte first.  The data sheet says
 * that the low byte must be read before the high byte.
 */
static inline int adt7473_read_word_data(struct i2c_client *client, u8 reg)
{
	u16 foo;
	foo = i2c_smbus_read_byte_data(client, reg);
	foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
	return foo;
}

static inline int adt7473_write_word_data(struct i2c_client *client, u8 reg,
					  u16 value)
{
	return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
	       && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
}

static void adt7473_init_client(struct i2c_client *client)
{
	int reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG1);

	if (!(reg & ADT7473_CFG1_READY)) {
		dev_err(&client->dev, "Chip not ready.\n");
	} else {
		/* start monitoring */
		i2c_smbus_write_byte_data(client, ADT7473_REG_CFG1,
					  reg | ADT7473_CFG1_START);
	}
}

static struct adt7473_data *adt7473_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	unsigned long local_jiffies = jiffies;
	u8 cfg;
	int i;

	mutex_lock(&data->lock);
	if (time_before(local_jiffies, data->sensors_last_updated +
		SENSOR_REFRESH_INTERVAL)
		&& data->sensors_valid)
		goto no_sensor_update;

	for (i = 0; i < ADT7473_VOLT_COUNT; i++)
		data->volt[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_VOLT(i));

	/* Determine temperature encoding */
	cfg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG5);
	data->temp_twos_complement = (cfg & ADT7473_CFG5_TEMP_TWOS);

	/*
	 * What does this do? it implies a variable temperature sensor
	 * offset, but the datasheet doesn't say anything about this bit
	 * and other parts of the datasheet imply that "offset64" mode
	 * means that you shift temp values by -64 if the above bit was set.
	 */
	data->temp_offset = (cfg & ADT7473_CFG5_TEMP_OFFSET);

	for (i = 0; i < ADT7473_TEMP_COUNT; i++)
		data->temp[i] = i2c_smbus_read_byte_data(client,
							 ADT7473_REG_TEMP(i));

	for (i = 0; i < ADT7473_FAN_COUNT; i++)
		data->fan[i] = adt7473_read_word_data(client,
						ADT7473_REG_FAN(i));

	for (i = 0; i < ADT7473_PWM_COUNT; i++)
		data->pwm[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_PWM(i));

	data->alarm = i2c_smbus_read_byte_data(client, ADT7473_REG_ALARM1);
	if (data->alarm & ADT7473_OOL)
		data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
							 ADT7473_REG_ALARM2));

	data->sensors_last_updated = local_jiffies;
	data->sensors_valid = 1;

no_sensor_update:
	if (time_before(local_jiffies, data->limits_last_updated +
		LIMIT_REFRESH_INTERVAL)
		&& data->limits_valid)
		goto out;

	for (i = 0; i < ADT7473_VOLT_COUNT; i++) {
		data->volt_min[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_VOLT_MIN(i));
		data->volt_max[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_VOLT_MAX(i));
	}

	for (i = 0; i < ADT7473_TEMP_COUNT; i++) {
		data->temp_min[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_TEMP_MIN(i));
		data->temp_max[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_TEMP_MAX(i));
		data->temp_tmin[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_TEMP_TMIN(i));
		data->temp_tmax[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_TEMP_TMAX(i));
	}

	for (i = 0; i < ADT7473_FAN_COUNT; i++)
		data->fan_min[i] = adt7473_read_word_data(client,
						ADT7473_REG_FAN_MIN(i));

	for (i = 0; i < ADT7473_PWM_COUNT; i++) {
		data->pwm_max[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_PWM_MAX(i));
		data->pwm_min[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_PWM_MIN(i));
		data->pwm_behavior[i] = i2c_smbus_read_byte_data(client,
						ADT7473_REG_PWM_BHVR(i));
	}

	i = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
	data->max_duty_at_overheat = !!(i & ADT7473_CFG4_MAX_DUTY_AT_OVT);

	data->limits_last_updated = local_jiffies;
	data->limits_valid = 1;

out:
	mutex_unlock(&data->lock);
	return data;
}

/*
 * On this chip, voltages are given as a count of steps between a minimum
 * and maximum voltage, not a direct voltage.
 */
static const int volt_convert_table[][2] = {
	{2997, 3},
	{4395, 4},
};

static int decode_volt(int volt_index, u8 raw)
{
	int cmax = volt_convert_table[volt_index][0];
	int cmin = volt_convert_table[volt_index][1];
	return ((raw * (cmax - cmin)) / 255) + cmin;
}

static u8 encode_volt(int volt_index, int cooked)
{
	int cmax = volt_convert_table[volt_index][0];
	int cmin = volt_convert_table[volt_index][1];
	u8 x;

	if (cooked > cmax)
		cooked = cmax;
	else if (cooked < cmin)
		cooked = cmin;

	x = ((cooked - cmin) * 255) / (cmax - cmin);

	return x;
}

static ssize_t show_volt_min(struct device *dev,
			     struct device_attribute *devattr,
			     char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n",
		       decode_volt(attr->index, data->volt_min[attr->index]));
}

static ssize_t set_volt_min(struct device *dev,
			    struct device_attribute *devattr,
			    const char *buf,
			    size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10));

	mutex_lock(&data->lock);
	data->volt_min[attr->index] = volt;
	i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MIN(attr->index),
				  volt);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_volt_max(struct device *dev,
			     struct device_attribute *devattr,
			     char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n",
		       decode_volt(attr->index, data->volt_max[attr->index]));
}

static ssize_t set_volt_max(struct device *dev,
			    struct device_attribute *devattr,
			    const char *buf,
			    size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10));

	mutex_lock(&data->lock);
	data->volt_max[attr->index] = volt;
	i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MAX(attr->index),
				  volt);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_volt(struct device *dev, struct device_attribute *devattr,
			 char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);

	return sprintf(buf, "%d\n",
		       decode_volt(attr->index, data->volt[attr->index]));
}

/*
 * This chip can report temperature data either as a two's complement
 * number in the range -128 to 127, or as an unsigned number that must
 * be offset by 64.
 */
static int decode_temp(u8 twos_complement, u8 raw)
{
	return twos_complement ? (s8)raw : raw - 64;
}

static u8 encode_temp(u8 twos_complement, int cooked)
{
	return twos_complement ? cooked & 0xFF : cooked + 64;
}

static ssize_t show_temp_min(struct device *dev,
			     struct device_attribute *devattr,
			     char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", 1000 * decode_temp(
						data->temp_twos_complement,
						data->temp_min[attr->index]));
}

static ssize_t set_temp_min(struct device *dev,
			    struct device_attribute *devattr,
			    const char *buf,
			    size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10) / 1000;
	temp = encode_temp(data->temp_twos_complement, temp);

	mutex_lock(&data->lock);
	data->temp_min[attr->index] = temp;
	i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MIN(attr->index),
				  temp);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_temp_max(struct device *dev,
			     struct device_attribute *devattr,
			     char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", 1000 * decode_temp(
						data->temp_twos_complement,
						data->temp_max[attr->index]));
}

static ssize_t set_temp_max(struct device *dev,
			    struct device_attribute *devattr,
			    const char *buf,
			    size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10) / 1000;
	temp = encode_temp(data->temp_twos_complement, temp);

	mutex_lock(&data->lock);
	data->temp_max[attr->index] = temp;
	i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MAX(attr->index),
				  temp);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
			 char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", 1000 * decode_temp(
						data->temp_twos_complement,
						data->temp[attr->index]));
}

static ssize_t show_fan_min(struct device *dev,
			    struct device_attribute *devattr,
			    char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);

	if (FAN_DATA_VALID(data->fan_min[attr->index]))
		return sprintf(buf, "%d\n",
			       FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
	else
		return sprintf(buf, "0\n");
}

static ssize_t set_fan_min(struct device *dev,
			   struct device_attribute *devattr,
			   const char *buf, size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10);

	if (!temp)
		return -EINVAL;
	temp = FAN_RPM_TO_PERIOD(temp);

	mutex_lock(&data->lock);
	data->fan_min[attr->index] = temp;
	adt7473_write_word_data(client, ADT7473_REG_FAN_MIN(attr->index), temp);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
			char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);

	if (FAN_DATA_VALID(data->fan[attr->index]))
		return sprintf(buf, "%d\n",
			       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
	else
		return sprintf(buf, "0\n");
}

static ssize_t show_max_duty_at_crit(struct device *dev,
				     struct device_attribute *devattr,
				     char *buf)
{
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", data->max_duty_at_overheat);
}

static ssize_t set_max_duty_at_crit(struct device *dev,
				    struct device_attribute *devattr,
				    const char *buf,
				    size_t count)
{
	u8 reg;
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10);
	temp = temp && 0xFF;

	mutex_lock(&data->lock);
	data->max_duty_at_overheat = temp;
	reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
	if (temp)
		reg |= ADT7473_CFG4_MAX_DUTY_AT_OVT;
	else
		reg &= ~ADT7473_CFG4_MAX_DUTY_AT_OVT;
	i2c_smbus_write_byte_data(client, ADT7473_REG_CFG4, reg);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
			char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", data->pwm[attr->index]);
}

static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
			const char *buf, size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10);

	mutex_lock(&data->lock);
	data->pwm[attr->index] = temp;
	i2c_smbus_write_byte_data(client, ADT7473_REG_PWM(attr->index), temp);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_pwm_max(struct device *dev,
			    struct device_attribute *devattr,
			    char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
}

static ssize_t set_pwm_max(struct device *dev,
			   struct device_attribute *devattr,
			   const char *buf,
			   size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10);

	mutex_lock(&data->lock);
	data->pwm_max[attr->index] = temp;
	i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MAX(attr->index),
				  temp);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_pwm_min(struct device *dev,
			    struct device_attribute *devattr,
			    char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
}

static ssize_t set_pwm_min(struct device *dev,
			   struct device_attribute *devattr,
			   const char *buf,
			   size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10);

	mutex_lock(&data->lock);
	data->pwm_min[attr->index] = temp;
	i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MIN(attr->index),
				  temp);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_temp_tmax(struct device *dev,
			      struct device_attribute *devattr,
			      char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", 1000 * decode_temp(
						data->temp_twos_complement,
						data->temp_tmax[attr->index]));
}

static ssize_t set_temp_tmax(struct device *dev,
			     struct device_attribute *devattr,
			     const char *buf,
			     size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10) / 1000;
	temp = encode_temp(data->temp_twos_complement, temp);

	mutex_lock(&data->lock);
	data->temp_tmax[attr->index] = temp;
	i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMAX(attr->index),
				  temp);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_temp_tmin(struct device *dev,
			      struct device_attribute *devattr,
			      char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	return sprintf(buf, "%d\n", 1000 * decode_temp(
						data->temp_twos_complement,
						data->temp_tmin[attr->index]));
}

static ssize_t set_temp_tmin(struct device *dev,
			     struct device_attribute *devattr,
			     const char *buf,
			     size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10) / 1000;
	temp = encode_temp(data->temp_twos_complement, temp);

	mutex_lock(&data->lock);
	data->temp_tmin[attr->index] = temp;
	i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMIN(attr->index),
				  temp);
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_pwm_enable(struct device *dev,
			       struct device_attribute *devattr,
			       char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);

	switch (data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT) {
	case 3:
		return sprintf(buf, "0\n");
	case 7:
		return sprintf(buf, "1\n");
	default:
		return sprintf(buf, "2\n");
	}
}

static ssize_t set_pwm_enable(struct device *dev,
			      struct device_attribute *devattr,
			      const char *buf,
			      size_t count)
{
	u8 reg;
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10);

	switch (temp) {
	case 0:
		temp = 3;
		break;
	case 1:
		temp = 7;
		break;
	case 2:
		/* Enter automatic mode with fans off */
		temp = 4;
		break;
	default:
		return -EINVAL;
	}

	mutex_lock(&data->lock);
	reg = i2c_smbus_read_byte_data(client,
				       ADT7473_REG_PWM_BHVR(attr->index));
	reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
	      (reg & ~ADT7473_PWM_BHVR_MASK);
	i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
				  reg);
	data->pwm_behavior[attr->index] = reg;
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_pwm_auto_temp(struct device *dev,
				  struct device_attribute *devattr,
				  char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);
	int bhvr = data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT;

	switch (bhvr) {
	case 3:
	case 4:
	case 7:
		return sprintf(buf, "0\n");
	case 0:
	case 1:
	case 5:
	case 6:
		return sprintf(buf, "%d\n", bhvr + 1);
	case 2:
		return sprintf(buf, "4\n");
	}
	/* shouldn't ever get here */
	BUG();
}

static ssize_t set_pwm_auto_temp(struct device *dev,
				 struct device_attribute *devattr,
				 const char *buf,
				 size_t count)
{
	u8 reg;
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adt7473_data *data = i2c_get_clientdata(client);
	int temp = simple_strtol(buf, NULL, 10);

	switch (temp) {
	case 1:
	case 2:
	case 6:
	case 7:
		temp--;
		break;
	case 0:
		temp = 4;
		break;
	default:
		return -EINVAL;
	}

	mutex_lock(&data->lock);
	reg = i2c_smbus_read_byte_data(client,
				       ADT7473_REG_PWM_BHVR(attr->index));
	reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
	      (reg & ~ADT7473_PWM_BHVR_MASK);
	i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
				  reg);
	data->pwm_behavior[attr->index] = reg;
	mutex_unlock(&data->lock);

	return count;
}

static ssize_t show_alarm(struct device *dev,
			  struct device_attribute *devattr,
			  char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adt7473_data *data = adt7473_update_device(dev);

	if (data->alarm & attr->index)
		return sprintf(buf, "1\n");
	else
		return sprintf(buf, "0\n");
}


static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max,
			  set_volt_max, 0);
static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max,
			  set_volt_max, 1);

static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min,
			  set_volt_min, 0);
static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min,
			  set_volt_min, 1);

static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_volt, NULL, 0);
static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_volt, NULL, 1);

static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL,
			  ADT7473_VCCP_ALARM);
static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL,
			  ADT7473_VCC_ALARM);

static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
			  set_temp_max, 0);
static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
			  set_temp_max, 1);
static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
			  set_temp_max, 2);

static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
			  set_temp_min, 0);
static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
			  set_temp_min, 1);
static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
			  set_temp_min, 2);

static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);

static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
			  ADT7473_R1T_ALARM | ALARM2(ADT7473_R1T_SHORT));
static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
			  ADT7473_LT_ALARM);
static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
			  ADT7473_R2T_ALARM | ALARM2(ADT7473_R2T_SHORT));

static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
			  set_fan_min, 0);
static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
			  set_fan_min, 1);
static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
			  set_fan_min, 2);
static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
			  set_fan_min, 3);

static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);

static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
			  ALARM2(ADT7473_FAN1_ALARM));
static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
			  ALARM2(ADT7473_FAN2_ALARM));
static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
			  ALARM2(ADT7473_FAN3_ALARM));
static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
			  ALARM2(ADT7473_FAN4_ALARM));

static SENSOR_DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
			  show_max_duty_at_crit, set_max_duty_at_crit, 0);

static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);

static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
			  show_pwm_min, set_pwm_min, 0);
static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
			  show_pwm_min, set_pwm_min, 1);
static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
			  show_pwm_min, set_pwm_min, 2);

static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
			  show_pwm_max, set_pwm_max, 0);
static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
			  show_pwm_max, set_pwm_max, 1);
static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
			  show_pwm_max, set_pwm_max, 2);

static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO,
			  show_temp_tmin, set_temp_tmin, 0);
static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
			  show_temp_tmin, set_temp_tmin, 1);
static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO,
			  show_temp_tmin, set_temp_tmin, 2);

static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
			  show_temp_tmax, set_temp_tmax, 0);
static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
			  show_temp_tmax, set_temp_tmax, 1);
static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO,
			  show_temp_tmax, set_temp_tmax, 2);

static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
			  set_pwm_enable, 0);
static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
			  set_pwm_enable, 1);
static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
			  set_pwm_enable, 2);

static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
			  show_pwm_auto_temp, set_pwm_auto_temp, 0);
static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
			  show_pwm_auto_temp, set_pwm_auto_temp, 1);
static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
			  show_pwm_auto_temp, set_pwm_auto_temp, 2);

static struct attribute *adt7473_attr[] =
{
	&sensor_dev_attr_in1_max.dev_attr.attr,
	&sensor_dev_attr_in2_max.dev_attr.attr,
	&sensor_dev_attr_in1_min.dev_attr.attr,
	&sensor_dev_attr_in2_min.dev_attr.attr,
	&sensor_dev_attr_in1_input.dev_attr.attr,
	&sensor_dev_attr_in2_input.dev_attr.attr,
	&sensor_dev_attr_in1_alarm.dev_attr.attr,
	&sensor_dev_attr_in2_alarm.dev_attr.attr,

	&sensor_dev_attr_temp1_max.dev_attr.attr,
	&sensor_dev_attr_temp2_max.dev_attr.attr,
	&sensor_dev_attr_temp3_max.dev_attr.attr,
	&sensor_dev_attr_temp1_min.dev_attr.attr,
	&sensor_dev_attr_temp2_min.dev_attr.attr,
	&sensor_dev_attr_temp3_min.dev_attr.attr,
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	&sensor_dev_attr_temp2_input.dev_attr.attr,
	&sensor_dev_attr_temp3_input.dev_attr.attr,
	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,

	&sensor_dev_attr_fan1_min.dev_attr.attr,
	&sensor_dev_attr_fan2_min.dev_attr.attr,
	&sensor_dev_attr_fan3_min.dev_attr.attr,
	&sensor_dev_attr_fan4_min.dev_attr.attr,
	&sensor_dev_attr_fan1_input.dev_attr.attr,
	&sensor_dev_attr_fan2_input.dev_attr.attr,
	&sensor_dev_attr_fan3_input.dev_attr.attr,
	&sensor_dev_attr_fan4_input.dev_attr.attr,
	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
	&sensor_dev_attr_fan4_alarm.dev_attr.attr,

	&sensor_dev_attr_pwm_use_point2_pwm_at_crit.dev_attr.attr,

	&sensor_dev_attr_pwm1.dev_attr.attr,
	&sensor_dev_attr_pwm2.dev_attr.attr,
	&sensor_dev_attr_pwm3.dev_attr.attr,
	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,

	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,

	NULL
};

/* Return 0 if detection is successful, -ENODEV otherwise */
static int adt7473_detect(struct i2c_client *client, int kind,
			  struct i2c_board_info *info)
{
	struct i2c_adapter *adapter = client->adapter;

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -ENODEV;

	if (kind <= 0) {
		int vendor, device, revision;

		vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR);
		if (vendor != ADT7473_VENDOR)
			return -ENODEV;

		device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE);
		if (device != ADT7473_DEVICE)
			return -ENODEV;

		revision = i2c_smbus_read_byte_data(client,
						    ADT7473_REG_REVISION);
		if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69)
			return -ENODEV;
	} else
		dev_dbg(&adapter->dev, "detection forced\n");

	strlcpy(info->type, "adt7473", I2C_NAME_SIZE);

	return 0;
}

static int adt7473_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct adt7473_data *data;
	int err;

	data = kzalloc(sizeof(struct adt7473_data), GFP_KERNEL);
	if (!data) {
		err = -ENOMEM;
		goto exit;
	}

	i2c_set_clientdata(client, data);
	mutex_init(&data->lock);

	dev_info(&client->dev, "%s chip found\n", client->name);

	/* Initialize the ADT7473 chip */
	adt7473_init_client(client);

	/* Register sysfs hooks */
	data->attrs.attrs = adt7473_attr;
	err = sysfs_create_group(&client->dev.kobj, &data->attrs);
	if (err)
		goto exit_free;

	data->hwmon_dev = hwmon_device_register(&client->dev);
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
		goto exit_remove;
	}

	return 0;

exit_remove:
	sysfs_remove_group(&client->dev.kobj, &data->attrs);
exit_free:
	kfree(data);
exit:
	return err;
}

static int adt7473_remove(struct i2c_client *client)
{
	struct adt7473_data *data = i2c_get_clientdata(client);

	hwmon_device_unregister(data->hwmon_dev);
	sysfs_remove_group(&client->dev.kobj, &data->attrs);
	kfree(data);
	return 0;
}

static int __init adt7473_init(void)
{
	return i2c_add_driver(&adt7473_driver);
}

static void __exit adt7473_exit(void)
{
	i2c_del_driver(&adt7473_driver);
}

MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
MODULE_DESCRIPTION("ADT7473 driver");
MODULE_LICENSE("GPL");

module_init(adt7473_init);
module_exit(adt7473_exit);
OpenPOWER on IntegriCloud