summaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/asc7621.c
blob: 7f948105d8addb79af23d7ec3e6c89b338cab79d (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
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
/*
 * asc7621.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
 * Copyright (c) 2007, 2010 George Joseph  <george.joseph@fairview5.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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.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>

/* Addresses to scan */
static unsigned short normal_i2c[] = {
	0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};

enum asc7621_type {
	asc7621,
	asc7621a
};

#define INTERVAL_HIGH   (HZ + HZ / 2)
#define INTERVAL_LOW    (1 * 60 * HZ)
#define PRI_NONE        0
#define PRI_LOW         1
#define PRI_HIGH        2
#define FIRST_CHIP      asc7621
#define LAST_CHIP       asc7621a

struct asc7621_chip {
	char *name;
	enum asc7621_type chip_type;
	u8 company_reg;
	u8 company_id;
	u8 verstep_reg;
	u8 verstep_id;
	unsigned short *addresses;
};

static struct asc7621_chip asc7621_chips[] = {
	{
		.name = "asc7621",
		.chip_type = asc7621,
		.company_reg = 0x3e,
		.company_id = 0x61,
		.verstep_reg = 0x3f,
		.verstep_id = 0x6c,
		.addresses = normal_i2c,
	 },
	{
		.name = "asc7621a",
		.chip_type = asc7621a,
		.company_reg = 0x3e,
		.company_id = 0x61,
		.verstep_reg = 0x3f,
		.verstep_id = 0x6d,
		.addresses = normal_i2c,
	 },
};

/*
 * Defines the highest register to be used, not the count.
 * The actual count will probably be smaller because of gaps
 * in the implementation (unused register locations).
 * This define will safely set the array size of both the parameter
 * and data arrays.
 * This comes from the data sheet register description table.
 */
#define LAST_REGISTER 0xff

struct asc7621_data {
	struct i2c_client client;
	struct device *class_dev;
	struct mutex update_lock;
	int valid;		/* !=0 if following fields are valid */
	unsigned long last_high_reading;	/* In jiffies */
	unsigned long last_low_reading;		/* In jiffies */
	/*
	 * Registers we care about occupy the corresponding index
	 * in the array.  Registers we don't care about are left
	 * at 0.
	 */
	u8 reg[LAST_REGISTER + 1];
};

/*
 * Macro to get the parent asc7621_param structure
 * from a sensor_device_attribute passed into the
 * show/store functions.
 */
#define to_asc7621_param(_sda) \
	container_of(_sda, struct asc7621_param, sda)

/*
 * Each parameter to be retrieved needs an asc7621_param structure
 * allocated.  It contains the sensor_device_attribute structure
 * and the control info needed to retrieve the value from the register map.
 */
struct asc7621_param {
	struct sensor_device_attribute sda;
	u8 priority;
	u8 msb[3];
	u8 lsb[3];
	u8 mask[3];
	u8 shift[3];
};

/*
 * This is the map that ultimately indicates whether we'll be
 * retrieving a register value or not, and at what frequency.
 */
static u8 asc7621_register_priorities[255];

static struct asc7621_data *asc7621_update_device(struct device *dev);

static inline u8 read_byte(struct i2c_client *client, u8 reg)
{
	int res = i2c_smbus_read_byte_data(client, reg);
	if (res < 0) {
		dev_err(&client->dev,
			"Unable to read from register 0x%02x.\n", reg);
		return 0;
	};
	return res & 0xff;
}

static inline int write_byte(struct i2c_client *client, u8 reg, u8 data)
{
	int res = i2c_smbus_write_byte_data(client, reg, data);
	if (res < 0) {
		dev_err(&client->dev,
			"Unable to write value 0x%02x to register 0x%02x.\n",
			data, reg);
	};
	return res;
}

/*
 * Data Handlers
 * Each function handles the formatting, storage
 * and retrieval of like parameters.
 */

#define SETUP_SHOW_data_param(d, a) \
	struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
	struct asc7621_data *data = asc7621_update_device(d); \
	struct asc7621_param *param = to_asc7621_param(sda)

#define SETUP_STORE_data_param(d, a) \
	struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
	struct i2c_client *client = to_i2c_client(d); \
	struct asc7621_data *data = i2c_get_clientdata(client); \
	struct asc7621_param *param = to_asc7621_param(sda)

/*
 * u8 is just what it sounds like...an unsigned byte with no
 * special formatting.
 */
static ssize_t show_u8(struct device *dev, struct device_attribute *attr,
		       char *buf)
{
	SETUP_SHOW_data_param(dev, attr);

	return sprintf(buf, "%u\n", data->reg[param->msb[0]]);
}

static ssize_t store_u8(struct device *dev, struct device_attribute *attr,
			const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	reqval = SENSORS_LIMIT(reqval, 0, 255);

	mutex_lock(&data->update_lock);
	data->reg[param->msb[0]] = reqval;
	write_byte(client, param->msb[0], reqval);
	mutex_unlock(&data->update_lock);
	return count;
}

/*
 * Many of the config values occupy only a few bits of a register.
 */
static ssize_t show_bitmask(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);

	return sprintf(buf, "%u\n",
		       (data->reg[param->msb[0]] >> param->
			shift[0]) & param->mask[0]);
}

static ssize_t store_bitmask(struct device *dev,
			     struct device_attribute *attr,
			     const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval;
	u8 currval;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	reqval = SENSORS_LIMIT(reqval, 0, param->mask[0]);

	reqval = (reqval & param->mask[0]) << param->shift[0];

	mutex_lock(&data->update_lock);
	currval = read_byte(client, param->msb[0]);
	reqval |= (currval & ~(param->mask[0] << param->shift[0]));
	data->reg[param->msb[0]] = reqval;
	write_byte(client, param->msb[0], reqval);
	mutex_unlock(&data->update_lock);
	return count;
}

/*
 * 16 bit fan rpm values
 * reported by the device as the number of 11.111us periods (90khz)
 * between full fan rotations.  Therefore...
 * RPM = (90000 * 60) / register value
 */
static ssize_t show_fan16(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u16 regval;

	mutex_lock(&data->update_lock);
	regval = (data->reg[param->msb[0]] << 8) | data->reg[param->lsb[0]];
	mutex_unlock(&data->update_lock);

	return sprintf(buf, "%u\n",
		       (regval == 0 ? -1 : (regval) ==
			0xffff ? 0 : 5400000 / regval));
}

static ssize_t store_fan16(struct device *dev,
			   struct device_attribute *attr, const char *buf,
			   size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	reqval =
	    (SENSORS_LIMIT((reqval) <= 0 ? 0 : 5400000 / (reqval), 0, 65534));

	mutex_lock(&data->update_lock);
	data->reg[param->msb[0]] = (reqval >> 8) & 0xff;
	data->reg[param->lsb[0]] = reqval & 0xff;
	write_byte(client, param->msb[0], data->reg[param->msb[0]]);
	write_byte(client, param->lsb[0], data->reg[param->lsb[0]]);
	mutex_unlock(&data->update_lock);

	return count;
}

/*
 * Voltages are scaled in the device so that the nominal voltage
 * is 3/4ths of the 0-255 range (i.e. 192).
 * If all voltages are 'normal' then all voltage registers will
 * read 0xC0.  This doesn't help us if we don't have a point of refernce.
 * The data sheet however provides us with the full scale value for each
 * which is stored in in_scaling.  The sda->index parameter value provides
 * the index into in_scaling.
 *
 * NOTE: The chip expects the first 2 inputs be 2.5 and 2.25 volts
 * respectively. That doesn't mean that's what the motherboard provides. :)
 */

static int asc7621_in_scaling[] = {
	3320, 3000, 4380, 6640, 16000
};

static ssize_t show_in10(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u16 regval;
	u8 nr = sda->index;

	mutex_lock(&data->update_lock);
	regval = (data->reg[param->msb[0]] * asc7621_in_scaling[nr]) / 256;

	/* The LSB value is a 2-bit scaling of the MSB's LSbit value.
	 * I.E.  If the maximim voltage for this input is 6640 millivolts then
	 * a MSB register value of 0 = 0mv and 255 = 6640mv.
	 * A 1 step change therefore represents 25.9mv (6640 / 256).
	 * The extra 2-bits therefore represent increments of 6.48mv.
	 */
	regval += ((asc7621_in_scaling[nr] / 256) / 4) *
	    (data->reg[param->lsb[0]] >> 6);

	mutex_unlock(&data->update_lock);

	return sprintf(buf, "%u\n", regval);
}

/* 8 bit voltage values (the mins and maxs) */
static ssize_t show_in8(struct device *dev, struct device_attribute *attr,
			char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u8 nr = sda->index;

	return sprintf(buf, "%u\n",
		       ((data->reg[param->msb[0]] *
			 asc7621_in_scaling[nr]) / 256));
}

static ssize_t store_in8(struct device *dev, struct device_attribute *attr,
			 const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval;
	u8 nr = sda->index;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	reqval = SENSORS_LIMIT(reqval, 0, asc7621_in_scaling[nr]);

	reqval = (reqval * 255 + 128) / asc7621_in_scaling[nr];

	mutex_lock(&data->update_lock);
	data->reg[param->msb[0]] = reqval;
	write_byte(client, param->msb[0], reqval);
	mutex_unlock(&data->update_lock);

	return count;
}

static ssize_t show_temp8(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);

	return sprintf(buf, "%d\n", ((s8) data->reg[param->msb[0]]) * 1000);
}

static ssize_t store_temp8(struct device *dev,
			   struct device_attribute *attr, const char *buf,
			   size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval;
	s8 temp;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	reqval = SENSORS_LIMIT(reqval, -127000, 127000);

	temp = reqval / 1000;

	mutex_lock(&data->update_lock);
	data->reg[param->msb[0]] = temp;
	write_byte(client, param->msb[0], temp);
	mutex_unlock(&data->update_lock);
	return count;
}

/*
 * Temperatures that occupy 2 bytes always have the whole
 * number of degrees in the MSB with some part of the LSB
 * indicating fractional degrees.
 */

/*   mmmmmmmm.llxxxxxx */
static ssize_t show_temp10(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u8 msb, lsb;
	int temp;

	mutex_lock(&data->update_lock);
	msb = data->reg[param->msb[0]];
	lsb = (data->reg[param->lsb[0]] >> 6) & 0x03;
	temp = (((s8) msb) * 1000) + (lsb * 250);
	mutex_unlock(&data->update_lock);

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

/*   mmmmmm.ll */
static ssize_t show_temp62(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u8 regval = data->reg[param->msb[0]];
	int temp = ((s8) (regval & 0xfc) * 1000) + ((regval & 0x03) * 250);

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

static ssize_t store_temp62(struct device *dev,
			    struct device_attribute *attr, const char *buf,
			    size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval, i, f;
	s8 temp;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	reqval = SENSORS_LIMIT(reqval, -32000, 31750);
	i = reqval / 1000;
	f = reqval - (i * 1000);
	temp = i << 2;
	temp |= f / 250;

	mutex_lock(&data->update_lock);
	data->reg[param->msb[0]] = temp;
	write_byte(client, param->msb[0], temp);
	mutex_unlock(&data->update_lock);
	return count;
}

/*
 * The aSC7621 doesn't provide an "auto_point2".  Instead, you
 * specify the auto_point1 and a range.  To keep with the sysfs
 * hwmon specs, we synthesize the auto_point_2 from them.
 */

static u32 asc7621_range_map[] = {
	2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
	13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
};

static ssize_t show_ap2_temp(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	long auto_point1;
	u8 regval;
	int temp;

	mutex_lock(&data->update_lock);
	auto_point1 = ((s8) data->reg[param->msb[1]]) * 1000;
	regval =
	    ((data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0]);
	temp = auto_point1 + asc7621_range_map[SENSORS_LIMIT(regval, 0, 15)];
	mutex_unlock(&data->update_lock);

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

}

static ssize_t store_ap2_temp(struct device *dev,
			      struct device_attribute *attr,
			      const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval, auto_point1;
	int i;
	u8 currval, newval = 0;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	mutex_lock(&data->update_lock);
	auto_point1 = data->reg[param->msb[1]] * 1000;
	reqval = SENSORS_LIMIT(reqval, auto_point1 + 2000, auto_point1 + 80000);

	for (i = ARRAY_SIZE(asc7621_range_map) - 1; i >= 0; i--) {
		if (reqval >= auto_point1 + asc7621_range_map[i]) {
			newval = i;
			break;
		}
	}

	newval = (newval & param->mask[0]) << param->shift[0];
	currval = read_byte(client, param->msb[0]);
	newval |= (currval & ~(param->mask[0] << param->shift[0]));
	data->reg[param->msb[0]] = newval;
	write_byte(client, param->msb[0], newval);
	mutex_unlock(&data->update_lock);
	return count;
}

static ssize_t show_pwm_ac(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u8 config, altbit, regval;
	u8 map[] = {
		0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
		0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
	};

	mutex_lock(&data->update_lock);
	config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
	altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
	regval = config | (altbit << 3);
	mutex_unlock(&data->update_lock);

	return sprintf(buf, "%u\n", map[SENSORS_LIMIT(regval, 0, 15)]);
}

static ssize_t store_pwm_ac(struct device *dev,
			    struct device_attribute *attr,
			    const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	unsigned long reqval;
	u8 currval, config, altbit, newval;
	u16 map[] = {
		0x04, 0x00, 0x01, 0xff, 0x02, 0xff, 0x05, 0x06,
		0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
		0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
	};

	if (strict_strtoul(buf, 10, &reqval))
		return -EINVAL;

	if (reqval > 31)
		return -EINVAL;

	reqval = map[reqval];
	if (reqval == 0xff)
		return -EINVAL;

	config = reqval & 0x07;
	altbit = (reqval >> 3) & 0x01;

	config = (config & param->mask[0]) << param->shift[0];
	altbit = (altbit & param->mask[1]) << param->shift[1];

	mutex_lock(&data->update_lock);
	currval = read_byte(client, param->msb[0]);
	newval = config | (currval & ~(param->mask[0] << param->shift[0]));
	newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
	data->reg[param->msb[0]] = newval;
	write_byte(client, param->msb[0], newval);
	mutex_unlock(&data->update_lock);
	return count;
}

static ssize_t show_pwm_enable(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u8 config, altbit, minoff, val, newval;

	mutex_lock(&data->update_lock);
	config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
	altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
	minoff = (data->reg[param->msb[2]] >> param->shift[2]) & param->mask[2];
	mutex_unlock(&data->update_lock);

	val = config | (altbit << 3);
	newval = 0;

	if (val == 3 || val >= 10)
		newval = 255;
	else if (val == 4)
		newval = 0;
	else if (val == 7)
		newval = 1;
	else if (minoff == 1)
		newval = 2;
	else
		newval = 3;

	return sprintf(buf, "%u\n", newval);
}

static ssize_t store_pwm_enable(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval;
	u8 currval, config, altbit, newval, minoff = 255;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	switch (reqval) {
	case 0:
		newval = 0x04;
		break;
	case 1:
		newval = 0x07;
		break;
	case 2:
		newval = 0x00;
		minoff = 1;
		break;
	case 3:
		newval = 0x00;
		minoff = 0;
		break;
	case 255:
		newval = 0x03;
		break;
	default:
		return -EINVAL;
	}

	config = newval & 0x07;
	altbit = (newval >> 3) & 0x01;

	mutex_lock(&data->update_lock);
	config = (config & param->mask[0]) << param->shift[0];
	altbit = (altbit & param->mask[1]) << param->shift[1];
	currval = read_byte(client, param->msb[0]);
	newval = config | (currval & ~(param->mask[0] << param->shift[0]));
	newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
	data->reg[param->msb[0]] = newval;
	write_byte(client, param->msb[0], newval);
	if (minoff < 255) {
		minoff = (minoff & param->mask[2]) << param->shift[2];
		currval = read_byte(client, param->msb[2]);
		newval =
		    minoff | (currval & ~(param->mask[2] << param->shift[2]));
		data->reg[param->msb[2]] = newval;
		write_byte(client, param->msb[2], newval);
	}
	mutex_unlock(&data->update_lock);
	return count;
}

static u32 asc7621_pwm_freq_map[] = {
	10, 15, 23, 30, 38, 47, 62, 94,
	23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000
};

static ssize_t show_pwm_freq(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u8 regval =
	    (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];

	regval = SENSORS_LIMIT(regval, 0, 15);

	return sprintf(buf, "%u\n", asc7621_pwm_freq_map[regval]);
}

static ssize_t store_pwm_freq(struct device *dev,
			      struct device_attribute *attr,
			      const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	unsigned long reqval;
	u8 currval, newval = 255;
	int i;

	if (strict_strtoul(buf, 10, &reqval))
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(asc7621_pwm_freq_map); i++) {
		if (reqval == asc7621_pwm_freq_map[i]) {
			newval = i;
			break;
		}
	}
	if (newval == 255)
		return -EINVAL;

	newval = (newval & param->mask[0]) << param->shift[0];

	mutex_lock(&data->update_lock);
	currval = read_byte(client, param->msb[0]);
	newval |= (currval & ~(param->mask[0] << param->shift[0]));
	data->reg[param->msb[0]] = newval;
	write_byte(client, param->msb[0], newval);
	mutex_unlock(&data->update_lock);
	return count;
}

static u32 asc7621_pwm_auto_spinup_map[] =  {
	0, 100, 250, 400, 700, 1000, 2000, 4000
};

static ssize_t show_pwm_ast(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u8 regval =
	    (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];

	regval = SENSORS_LIMIT(regval, 0, 7);

	return sprintf(buf, "%u\n", asc7621_pwm_auto_spinup_map[regval]);

}

static ssize_t store_pwm_ast(struct device *dev,
			     struct device_attribute *attr,
			     const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval;
	u8 currval, newval = 255;
	u32 i;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(asc7621_pwm_auto_spinup_map); i++) {
		if (reqval == asc7621_pwm_auto_spinup_map[i]) {
			newval = i;
			break;
		}
	}
	if (newval == 255)
		return -EINVAL;

	newval = (newval & param->mask[0]) << param->shift[0];

	mutex_lock(&data->update_lock);
	currval = read_byte(client, param->msb[0]);
	newval |= (currval & ~(param->mask[0] << param->shift[0]));
	data->reg[param->msb[0]] = newval;
	write_byte(client, param->msb[0], newval);
	mutex_unlock(&data->update_lock);
	return count;
}

static u32 asc7621_temp_smoothing_time_map[] = {
	35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
};

static ssize_t show_temp_st(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	SETUP_SHOW_data_param(dev, attr);
	u8 regval =
	    (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
	regval = SENSORS_LIMIT(regval, 0, 7);

	return sprintf(buf, "%u\n", asc7621_temp_smoothing_time_map[regval]);
}

static ssize_t store_temp_st(struct device *dev,
			     struct device_attribute *attr,
			     const char *buf, size_t count)
{
	SETUP_STORE_data_param(dev, attr);
	long reqval;
	u8 currval, newval = 255;
	u32 i;

	if (strict_strtol(buf, 10, &reqval))
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(asc7621_temp_smoothing_time_map); i++) {
		if (reqval == asc7621_temp_smoothing_time_map[i]) {
			newval = i;
			break;
		}
	}

	if (newval == 255)
		return -EINVAL;

	newval = (newval & param->mask[0]) << param->shift[0];

	mutex_lock(&data->update_lock);
	currval = read_byte(client, param->msb[0]);
	newval |= (currval & ~(param->mask[0] << param->shift[0]));
	data->reg[param->msb[0]] = newval;
	write_byte(client, param->msb[0], newval);
	mutex_unlock(&data->update_lock);
	return count;
}

/*
 * End of data handlers
 *
 * These defines do nothing more than make the table easier
 * to read when wrapped at column 80.
 */

/*
 * Creates a variable length array inititalizer.
 * VAA(1,3,5,7) would produce {1,3,5,7}
 */
#define VAA(args...) {args}

#define PREAD(name, n, pri, rm, rl, m, s, r) \
	{.sda = SENSOR_ATTR(name, S_IRUGO, show_##r, NULL, n), \
	  .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
	  .shift[0] = s,}

#define PWRITE(name, n, pri, rm, rl, m, s, r) \
	{.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
	  .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
	  .shift[0] = s,}

/*
 * PWRITEM assumes that the initializers for the .msb, .lsb, .mask and .shift
 * were created using the VAA macro.
 */
#define PWRITEM(name, n, pri, rm, rl, m, s, r) \
	{.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
	  .priority = pri, .msb = rm, .lsb = rl, .mask = m, .shift = s,}

static struct asc7621_param asc7621_params[] = {
	PREAD(in0_input, 0, PRI_HIGH, 0x20, 0x13, 0, 0, in10),
	PREAD(in1_input, 1, PRI_HIGH, 0x21, 0x18, 0, 0, in10),
	PREAD(in2_input, 2, PRI_HIGH, 0x22, 0x11, 0, 0, in10),
	PREAD(in3_input, 3, PRI_HIGH, 0x23, 0x12, 0, 0, in10),
	PREAD(in4_input, 4, PRI_HIGH, 0x24, 0x14, 0, 0, in10),

	PWRITE(in0_min, 0, PRI_LOW, 0x44, 0, 0, 0, in8),
	PWRITE(in1_min, 1, PRI_LOW, 0x46, 0, 0, 0, in8),
	PWRITE(in2_min, 2, PRI_LOW, 0x48, 0, 0, 0, in8),
	PWRITE(in3_min, 3, PRI_LOW, 0x4a, 0, 0, 0, in8),
	PWRITE(in4_min, 4, PRI_LOW, 0x4c, 0, 0, 0, in8),

	PWRITE(in0_max, 0, PRI_LOW, 0x45, 0, 0, 0, in8),
	PWRITE(in1_max, 1, PRI_LOW, 0x47, 0, 0, 0, in8),
	PWRITE(in2_max, 2, PRI_LOW, 0x49, 0, 0, 0, in8),
	PWRITE(in3_max, 3, PRI_LOW, 0x4b, 0, 0, 0, in8),
	PWRITE(in4_max, 4, PRI_LOW, 0x4d, 0, 0, 0, in8),

	PREAD(in0_alarm, 0, PRI_LOW, 0x41, 0, 0x01, 0, bitmask),
	PREAD(in1_alarm, 1, PRI_LOW, 0x41, 0, 0x01, 1, bitmask),
	PREAD(in2_alarm, 2, PRI_LOW, 0x41, 0, 0x01, 2, bitmask),
	PREAD(in3_alarm, 3, PRI_LOW, 0x41, 0, 0x01, 3, bitmask),
	PREAD(in4_alarm, 4, PRI_LOW, 0x42, 0, 0x01, 0, bitmask),

	PREAD(fan1_input, 0, PRI_HIGH, 0x29, 0x28, 0, 0, fan16),
	PREAD(fan2_input, 1, PRI_HIGH, 0x2b, 0x2a, 0, 0, fan16),
	PREAD(fan3_input, 2, PRI_HIGH, 0x2d, 0x2c, 0, 0, fan16),
	PREAD(fan4_input, 3, PRI_HIGH, 0x2f, 0x2e, 0, 0, fan16),

	PWRITE(fan1_min, 0, PRI_LOW, 0x55, 0x54, 0, 0, fan16),
	PWRITE(fan2_min, 1, PRI_LOW, 0x57, 0x56, 0, 0, fan16),
	PWRITE(fan3_min, 2, PRI_LOW, 0x59, 0x58, 0, 0, fan16),
	PWRITE(fan4_min, 3, PRI_LOW, 0x5b, 0x5a, 0, 0, fan16),

	PREAD(fan1_alarm, 0, PRI_LOW, 0x42, 0, 0x01, 0, bitmask),
	PREAD(fan2_alarm, 1, PRI_LOW, 0x42, 0, 0x01, 1, bitmask),
	PREAD(fan3_alarm, 2, PRI_LOW, 0x42, 0, 0x01, 2, bitmask),
	PREAD(fan4_alarm, 3, PRI_LOW, 0x42, 0, 0x01, 3, bitmask),

	PREAD(temp1_input, 0, PRI_HIGH, 0x25, 0x10, 0, 0, temp10),
	PREAD(temp2_input, 1, PRI_HIGH, 0x26, 0x15, 0, 0, temp10),
	PREAD(temp3_input, 2, PRI_HIGH, 0x27, 0x16, 0, 0, temp10),
	PREAD(temp4_input, 3, PRI_HIGH, 0x33, 0x17, 0, 0, temp10),
	PREAD(temp5_input, 4, PRI_HIGH, 0xf7, 0xf6, 0, 0, temp10),
	PREAD(temp6_input, 5, PRI_HIGH, 0xf9, 0xf8, 0, 0, temp10),
	PREAD(temp7_input, 6, PRI_HIGH, 0xfb, 0xfa, 0, 0, temp10),
	PREAD(temp8_input, 7, PRI_HIGH, 0xfd, 0xfc, 0, 0, temp10),

	PWRITE(temp1_min, 0, PRI_LOW, 0x4e, 0, 0, 0, temp8),
	PWRITE(temp2_min, 1, PRI_LOW, 0x50, 0, 0, 0, temp8),
	PWRITE(temp3_min, 2, PRI_LOW, 0x52, 0, 0, 0, temp8),
	PWRITE(temp4_min, 3, PRI_LOW, 0x34, 0, 0, 0, temp8),

	PWRITE(temp1_max, 0, PRI_LOW, 0x4f, 0, 0, 0, temp8),
	PWRITE(temp2_max, 1, PRI_LOW, 0x51, 0, 0, 0, temp8),
	PWRITE(temp3_max, 2, PRI_LOW, 0x53, 0, 0, 0, temp8),
	PWRITE(temp4_max, 3, PRI_LOW, 0x35, 0, 0, 0, temp8),

	PREAD(temp1_alarm, 0, PRI_LOW, 0x41, 0, 0x01, 4, bitmask),
	PREAD(temp2_alarm, 1, PRI_LOW, 0x41, 0, 0x01, 5, bitmask),
	PREAD(temp3_alarm, 2, PRI_LOW, 0x41, 0, 0x01, 6, bitmask),
	PREAD(temp4_alarm, 3, PRI_LOW, 0x43, 0, 0x01, 0, bitmask),

	PWRITE(temp1_source, 0, PRI_LOW, 0x02, 0, 0x07, 4, bitmask),
	PWRITE(temp2_source, 1, PRI_LOW, 0x02, 0, 0x07, 0, bitmask),
	PWRITE(temp3_source, 2, PRI_LOW, 0x03, 0, 0x07, 4, bitmask),
	PWRITE(temp4_source, 3, PRI_LOW, 0x03, 0, 0x07, 0, bitmask),

	PWRITE(temp1_smoothing_enable, 0, PRI_LOW, 0x62, 0, 0x01, 3, bitmask),
	PWRITE(temp2_smoothing_enable, 1, PRI_LOW, 0x63, 0, 0x01, 7, bitmask),
	PWRITE(temp3_smoothing_enable, 2, PRI_LOW, 0x64, 0, 0x01, 3, bitmask),
	PWRITE(temp4_smoothing_enable, 3, PRI_LOW, 0x3c, 0, 0x01, 3, bitmask),

	PWRITE(temp1_smoothing_time, 0, PRI_LOW, 0x62, 0, 0x07, 0, temp_st),
	PWRITE(temp2_smoothing_time, 1, PRI_LOW, 0x63, 0, 0x07, 4, temp_st),
	PWRITE(temp3_smoothing_time, 2, PRI_LOW, 0x63, 0, 0x07, 0, temp_st),
	PWRITE(temp4_smoothing_time, 3, PRI_LOW, 0x3c, 0, 0x07, 0, temp_st),

	PWRITE(temp1_auto_point1_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
	       bitmask),
	PWRITE(temp2_auto_point1_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
	       bitmask),
	PWRITE(temp3_auto_point1_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
	       bitmask),
	PWRITE(temp4_auto_point1_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
	       bitmask),

	PREAD(temp1_auto_point2_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
	      bitmask),
	PREAD(temp2_auto_point2_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
	      bitmask),
	PREAD(temp3_auto_point2_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
	      bitmask),
	PREAD(temp4_auto_point2_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
	      bitmask),

	PWRITE(temp1_auto_point1_temp, 0, PRI_LOW, 0x67, 0, 0, 0, temp8),
	PWRITE(temp2_auto_point1_temp, 1, PRI_LOW, 0x68, 0, 0, 0, temp8),
	PWRITE(temp3_auto_point1_temp, 2, PRI_LOW, 0x69, 0, 0, 0, temp8),
	PWRITE(temp4_auto_point1_temp, 3, PRI_LOW, 0x3b, 0, 0, 0, temp8),

	PWRITEM(temp1_auto_point2_temp, 0, PRI_LOW, VAA(0x5f, 0x67), VAA(0),
		VAA(0x0f), VAA(4), ap2_temp),
	PWRITEM(temp2_auto_point2_temp, 1, PRI_LOW, VAA(0x60, 0x68), VAA(0),
		VAA(0x0f), VAA(4), ap2_temp),
	PWRITEM(temp3_auto_point2_temp, 2, PRI_LOW, VAA(0x61, 0x69), VAA(0),
		VAA(0x0f), VAA(4), ap2_temp),
	PWRITEM(temp4_auto_point2_temp, 3, PRI_LOW, VAA(0x3c, 0x3b), VAA(0),
		VAA(0x0f), VAA(4), ap2_temp),

	PWRITE(temp1_crit, 0, PRI_LOW, 0x6a, 0, 0, 0, temp8),
	PWRITE(temp2_crit, 1, PRI_LOW, 0x6b, 0, 0, 0, temp8),
	PWRITE(temp3_crit, 2, PRI_LOW, 0x6c, 0, 0, 0, temp8),
	PWRITE(temp4_crit, 3, PRI_LOW, 0x3d, 0, 0, 0, temp8),

	PWRITE(temp5_enable, 4, PRI_LOW, 0x0e, 0, 0x01, 0, bitmask),
	PWRITE(temp6_enable, 5, PRI_LOW, 0x0e, 0, 0x01, 1, bitmask),
	PWRITE(temp7_enable, 6, PRI_LOW, 0x0e, 0, 0x01, 2, bitmask),
	PWRITE(temp8_enable, 7, PRI_LOW, 0x0e, 0, 0x01, 3, bitmask),

	PWRITE(remote1_offset, 0, PRI_LOW, 0x1c, 0, 0, 0, temp62),
	PWRITE(remote2_offset, 1, PRI_LOW, 0x1d, 0, 0, 0, temp62),

	PWRITE(pwm1, 0, PRI_HIGH, 0x30, 0, 0, 0, u8),
	PWRITE(pwm2, 1, PRI_HIGH, 0x31, 0, 0, 0, u8),
	PWRITE(pwm3, 2, PRI_HIGH, 0x32, 0, 0, 0, u8),

	PWRITE(pwm1_invert, 0, PRI_LOW, 0x5c, 0, 0x01, 4, bitmask),
	PWRITE(pwm2_invert, 1, PRI_LOW, 0x5d, 0, 0x01, 4, bitmask),
	PWRITE(pwm3_invert, 2, PRI_LOW, 0x5e, 0, 0x01, 4, bitmask),

	PWRITEM(pwm1_enable, 0, PRI_LOW, VAA(0x5c, 0x5c, 0x62), VAA(0, 0, 0),
		VAA(0x07, 0x01, 0x01), VAA(5, 3, 5), pwm_enable),
	PWRITEM(pwm2_enable, 1, PRI_LOW, VAA(0x5d, 0x5d, 0x62), VAA(0, 0, 0),
		VAA(0x07, 0x01, 0x01), VAA(5, 3, 6), pwm_enable),
	PWRITEM(pwm3_enable, 2, PRI_LOW, VAA(0x5e, 0x5e, 0x62), VAA(0, 0, 0),
		VAA(0x07, 0x01, 0x01), VAA(5, 3, 7), pwm_enable),

	PWRITEM(pwm1_auto_channels, 0, PRI_LOW, VAA(0x5c, 0x5c), VAA(0, 0),
		VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
	PWRITEM(pwm2_auto_channels, 1, PRI_LOW, VAA(0x5d, 0x5d), VAA(0, 0),
		VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
	PWRITEM(pwm3_auto_channels, 2, PRI_LOW, VAA(0x5e, 0x5e), VAA(0, 0),
		VAA(0x07, 0x01), VAA(5, 3), pwm_ac),

	PWRITE(pwm1_auto_point1_pwm, 0, PRI_LOW, 0x64, 0, 0, 0, u8),
	PWRITE(pwm2_auto_point1_pwm, 1, PRI_LOW, 0x65, 0, 0, 0, u8),
	PWRITE(pwm3_auto_point1_pwm, 2, PRI_LOW, 0x66, 0, 0, 0, u8),

	PWRITE(pwm1_auto_point2_pwm, 0, PRI_LOW, 0x38, 0, 0, 0, u8),
	PWRITE(pwm2_auto_point2_pwm, 1, PRI_LOW, 0x39, 0, 0, 0, u8),
	PWRITE(pwm3_auto_point2_pwm, 2, PRI_LOW, 0x3a, 0, 0, 0, u8),

	PWRITE(pwm1_freq, 0, PRI_LOW, 0x5f, 0, 0x0f, 0, pwm_freq),
	PWRITE(pwm2_freq, 1, PRI_LOW, 0x60, 0, 0x0f, 0, pwm_freq),
	PWRITE(pwm3_freq, 2, PRI_LOW, 0x61, 0, 0x0f, 0, pwm_freq),

	PREAD(pwm1_auto_zone_assigned, 0, PRI_LOW, 0, 0, 0x03, 2, bitmask),
	PREAD(pwm2_auto_zone_assigned, 1, PRI_LOW, 0, 0, 0x03, 4, bitmask),
	PREAD(pwm3_auto_zone_assigned, 2, PRI_LOW, 0, 0, 0x03, 6, bitmask),

	PWRITE(pwm1_auto_spinup_time, 0, PRI_LOW, 0x5c, 0, 0x07, 0, pwm_ast),
	PWRITE(pwm2_auto_spinup_time, 1, PRI_LOW, 0x5d, 0, 0x07, 0, pwm_ast),
	PWRITE(pwm3_auto_spinup_time, 2, PRI_LOW, 0x5e, 0, 0x07, 0, pwm_ast),

	PWRITE(peci_enable, 0, PRI_LOW, 0x40, 0, 0x01, 4, bitmask),
	PWRITE(peci_avg, 0, PRI_LOW, 0x36, 0, 0x07, 0, bitmask),
	PWRITE(peci_domain, 0, PRI_LOW, 0x36, 0, 0x01, 3, bitmask),
	PWRITE(peci_legacy, 0, PRI_LOW, 0x36, 0, 0x01, 4, bitmask),
	PWRITE(peci_diode, 0, PRI_LOW, 0x0e, 0, 0x07, 4, bitmask),
	PWRITE(peci_4domain, 0, PRI_LOW, 0x0e, 0, 0x01, 4, bitmask),

};

static struct asc7621_data *asc7621_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct asc7621_data *data = i2c_get_clientdata(client);
	int i;

/*
 * The asc7621 chips guarantee consistent reads of multi-byte values
 * regardless of the order of the reads.  No special logic is needed
 * so we can just read the registers in whatever  order they appear
 * in the asc7621_params array.
 */

	mutex_lock(&data->update_lock);

	/* Read all the high priority registers */

	if (!data->valid ||
	    time_after(jiffies, data->last_high_reading + INTERVAL_HIGH)) {

		for (i = 0; i < ARRAY_SIZE(asc7621_register_priorities); i++) {
			if (asc7621_register_priorities[i] == PRI_HIGH) {
				data->reg[i] =
				    i2c_smbus_read_byte_data(client, i) & 0xff;
			}
		}
		data->last_high_reading = jiffies;
	};			/* last_reading */

	/* Read all the low priority registers. */

	if (!data->valid ||
	    time_after(jiffies, data->last_low_reading + INTERVAL_LOW)) {

		for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
			if (asc7621_register_priorities[i] == PRI_LOW) {
				data->reg[i] =
				    i2c_smbus_read_byte_data(client, i) & 0xff;
			}
		}
		data->last_low_reading = jiffies;
	};			/* last_reading */

	data->valid = 1;

	mutex_unlock(&data->update_lock);

	return data;
}

/*
 * Standard detection and initialization below
 *
 * Helper function that checks if an address is valid
 * for a particular chip.
 */

static inline int valid_address_for_chip(int chip_type, int address)
{
	int i;

	for (i = 0; asc7621_chips[chip_type].addresses[i] != I2C_CLIENT_END;
	     i++) {
		if (asc7621_chips[chip_type].addresses[i] == address)
			return 1;
	}
	return 0;
}

static void asc7621_init_client(struct i2c_client *client)
{
	int value;

	/* Warn if part was not "READY" */

	value = read_byte(client, 0x40);

	if (value & 0x02) {
		dev_err(&client->dev,
			"Client (%d,0x%02x) config is locked.\n",
			i2c_adapter_id(client->adapter), client->addr);
	};
	if (!(value & 0x04)) {
		dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
			i2c_adapter_id(client->adapter), client->addr);
	};

/*
 * Start monitoring
 *
 * Try to clear LOCK, Set START, save everything else
 */
	value = (value & ~0x02) | 0x01;
	write_byte(client, 0x40, value & 0xff);

}

static int
asc7621_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	struct asc7621_data *data;
	int i, err;

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -EIO;

	data = kzalloc(sizeof(struct asc7621_data), GFP_KERNEL);
	if (data == NULL)
		return -ENOMEM;

	i2c_set_clientdata(client, data);
	data->valid = 0;
	mutex_init(&data->update_lock);

	/* Initialize the asc7621 chip */
	asc7621_init_client(client);

	/* Create the sysfs entries */
	for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
		err =
		    device_create_file(&client->dev,
				       &(asc7621_params[i].sda.dev_attr));
		if (err)
			goto exit_remove;
	}

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

	return 0;

exit_remove:
	for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
		device_remove_file(&client->dev,
				   &(asc7621_params[i].sda.dev_attr));
	}

	i2c_set_clientdata(client, NULL);
	kfree(data);
	return err;
}

static int asc7621_detect(struct i2c_client *client,
			  struct i2c_board_info *info)
{
	struct i2c_adapter *adapter = client->adapter;
	int company, verstep, chip_index;
	struct device *dev;

	dev = &client->dev;

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

	for (chip_index = FIRST_CHIP; chip_index <= LAST_CHIP; chip_index++) {

		if (!valid_address_for_chip(chip_index, client->addr))
			continue;

		company = read_byte(client,
			asc7621_chips[chip_index].company_reg);
		verstep = read_byte(client,
			asc7621_chips[chip_index].verstep_reg);

		if (company == asc7621_chips[chip_index].company_id &&
		    verstep == asc7621_chips[chip_index].verstep_id) {
			strlcpy(client->name, asc7621_chips[chip_index].name,
				I2C_NAME_SIZE);
			strlcpy(info->type, asc7621_chips[chip_index].name,
				I2C_NAME_SIZE);

			dev_info(&adapter->dev, "Matched %s\n",
				 asc7621_chips[chip_index].name);
			return 0;
		}
	}

	return -ENODEV;
}

static int asc7621_remove(struct i2c_client *client)
{
	struct asc7621_data *data = i2c_get_clientdata(client);
	int i;

	hwmon_device_unregister(data->class_dev);

	for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
		device_remove_file(&client->dev,
				   &(asc7621_params[i].sda.dev_attr));
	}

	i2c_set_clientdata(client, NULL);
	kfree(data);
	return 0;
}

static const struct i2c_device_id asc7621_id[] = {
	{"asc7621", asc7621},
	{"asc7621a", asc7621a},
	{},
};

MODULE_DEVICE_TABLE(i2c, asc7621_id);

static struct i2c_driver asc7621_driver = {
	.class = I2C_CLASS_HWMON,
	.driver = {
		.name = "asc7621",
	},
	.probe = asc7621_probe,
	.remove = asc7621_remove,
	.id_table = asc7621_id,
	.detect = asc7621_detect,
	.address_list = normal_i2c,
};

static int __init sm_asc7621_init(void)
{
	int i, j;
/*
 * Collect all the registers needed into a single array.
 * This way, if a register isn't actually used for anything,
 * we don't retrieve it.
 */

	for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
		for (j = 0; j < ARRAY_SIZE(asc7621_params[i].msb); j++)
			asc7621_register_priorities[asc7621_params[i].msb[j]] =
			    asc7621_params[i].priority;
		for (j = 0; j < ARRAY_SIZE(asc7621_params[i].lsb); j++)
			asc7621_register_priorities[asc7621_params[i].lsb[j]] =
			    asc7621_params[i].priority;
	}
	return i2c_add_driver(&asc7621_driver);
}

static void __exit sm_asc7621_exit(void)
{
	i2c_del_driver(&asc7621_driver);
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("George Joseph");
MODULE_DESCRIPTION("Andigilog aSC7621 and aSC7621a driver");

module_init(sm_asc7621_init);
module_exit(sm_asc7621_exit);
OpenPOWER on IntegriCloud