Merge branch 'imx-for-2.6.38' of git://git.pengutronix.de/git/ukl/linux-2.6 into...
[pandora-kernel.git] / drivers / hwmon / asc7621.c
1 /*
2  * asc7621.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3  * Copyright (c) 2007, 2010 George Joseph  <george.joseph@fairview5.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29
30 /* Addresses to scan */
31 static const unsigned short normal_i2c[] = {
32         0x2c, 0x2d, 0x2e, I2C_CLIENT_END
33 };
34
35 enum asc7621_type {
36         asc7621,
37         asc7621a
38 };
39
40 #define INTERVAL_HIGH   (HZ + HZ / 2)
41 #define INTERVAL_LOW    (1 * 60 * HZ)
42 #define PRI_NONE        0
43 #define PRI_LOW         1
44 #define PRI_HIGH        2
45 #define FIRST_CHIP      asc7621
46 #define LAST_CHIP       asc7621a
47
48 struct asc7621_chip {
49         char *name;
50         enum asc7621_type chip_type;
51         u8 company_reg;
52         u8 company_id;
53         u8 verstep_reg;
54         u8 verstep_id;
55         const unsigned short *addresses;
56 };
57
58 static struct asc7621_chip asc7621_chips[] = {
59         {
60                 .name = "asc7621",
61                 .chip_type = asc7621,
62                 .company_reg = 0x3e,
63                 .company_id = 0x61,
64                 .verstep_reg = 0x3f,
65                 .verstep_id = 0x6c,
66                 .addresses = normal_i2c,
67          },
68         {
69                 .name = "asc7621a",
70                 .chip_type = asc7621a,
71                 .company_reg = 0x3e,
72                 .company_id = 0x61,
73                 .verstep_reg = 0x3f,
74                 .verstep_id = 0x6d,
75                 .addresses = normal_i2c,
76          },
77 };
78
79 /*
80  * Defines the highest register to be used, not the count.
81  * The actual count will probably be smaller because of gaps
82  * in the implementation (unused register locations).
83  * This define will safely set the array size of both the parameter
84  * and data arrays.
85  * This comes from the data sheet register description table.
86  */
87 #define LAST_REGISTER 0xff
88
89 struct asc7621_data {
90         struct i2c_client client;
91         struct device *class_dev;
92         struct mutex update_lock;
93         int valid;              /* !=0 if following fields are valid */
94         unsigned long last_high_reading;        /* In jiffies */
95         unsigned long last_low_reading;         /* In jiffies */
96         /*
97          * Registers we care about occupy the corresponding index
98          * in the array.  Registers we don't care about are left
99          * at 0.
100          */
101         u8 reg[LAST_REGISTER + 1];
102 };
103
104 /*
105  * Macro to get the parent asc7621_param structure
106  * from a sensor_device_attribute passed into the
107  * show/store functions.
108  */
109 #define to_asc7621_param(_sda) \
110         container_of(_sda, struct asc7621_param, sda)
111
112 /*
113  * Each parameter to be retrieved needs an asc7621_param structure
114  * allocated.  It contains the sensor_device_attribute structure
115  * and the control info needed to retrieve the value from the register map.
116  */
117 struct asc7621_param {
118         struct sensor_device_attribute sda;
119         u8 priority;
120         u8 msb[3];
121         u8 lsb[3];
122         u8 mask[3];
123         u8 shift[3];
124 };
125
126 /*
127  * This is the map that ultimately indicates whether we'll be
128  * retrieving a register value or not, and at what frequency.
129  */
130 static u8 asc7621_register_priorities[255];
131
132 static struct asc7621_data *asc7621_update_device(struct device *dev);
133
134 static inline u8 read_byte(struct i2c_client *client, u8 reg)
135 {
136         int res = i2c_smbus_read_byte_data(client, reg);
137         if (res < 0) {
138                 dev_err(&client->dev,
139                         "Unable to read from register 0x%02x.\n", reg);
140                 return 0;
141         };
142         return res & 0xff;
143 }
144
145 static inline int write_byte(struct i2c_client *client, u8 reg, u8 data)
146 {
147         int res = i2c_smbus_write_byte_data(client, reg, data);
148         if (res < 0) {
149                 dev_err(&client->dev,
150                         "Unable to write value 0x%02x to register 0x%02x.\n",
151                         data, reg);
152         };
153         return res;
154 }
155
156 /*
157  * Data Handlers
158  * Each function handles the formatting, storage
159  * and retrieval of like parameters.
160  */
161
162 #define SETUP_SHOW_data_param(d, a) \
163         struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
164         struct asc7621_data *data = asc7621_update_device(d); \
165         struct asc7621_param *param = to_asc7621_param(sda)
166
167 #define SETUP_STORE_data_param(d, a) \
168         struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
169         struct i2c_client *client = to_i2c_client(d); \
170         struct asc7621_data *data = i2c_get_clientdata(client); \
171         struct asc7621_param *param = to_asc7621_param(sda)
172
173 /*
174  * u8 is just what it sounds like...an unsigned byte with no
175  * special formatting.
176  */
177 static ssize_t show_u8(struct device *dev, struct device_attribute *attr,
178                        char *buf)
179 {
180         SETUP_SHOW_data_param(dev, attr);
181
182         return sprintf(buf, "%u\n", data->reg[param->msb[0]]);
183 }
184
185 static ssize_t store_u8(struct device *dev, struct device_attribute *attr,
186                         const char *buf, size_t count)
187 {
188         SETUP_STORE_data_param(dev, attr);
189         long reqval;
190
191         if (strict_strtol(buf, 10, &reqval))
192                 return -EINVAL;
193
194         reqval = SENSORS_LIMIT(reqval, 0, 255);
195
196         mutex_lock(&data->update_lock);
197         data->reg[param->msb[0]] = reqval;
198         write_byte(client, param->msb[0], reqval);
199         mutex_unlock(&data->update_lock);
200         return count;
201 }
202
203 /*
204  * Many of the config values occupy only a few bits of a register.
205  */
206 static ssize_t show_bitmask(struct device *dev,
207                             struct device_attribute *attr, char *buf)
208 {
209         SETUP_SHOW_data_param(dev, attr);
210
211         return sprintf(buf, "%u\n",
212                        (data->reg[param->msb[0]] >> param->
213                         shift[0]) & param->mask[0]);
214 }
215
216 static ssize_t store_bitmask(struct device *dev,
217                              struct device_attribute *attr,
218                              const char *buf, size_t count)
219 {
220         SETUP_STORE_data_param(dev, attr);
221         long reqval;
222         u8 currval;
223
224         if (strict_strtol(buf, 10, &reqval))
225                 return -EINVAL;
226
227         reqval = SENSORS_LIMIT(reqval, 0, param->mask[0]);
228
229         reqval = (reqval & param->mask[0]) << param->shift[0];
230
231         mutex_lock(&data->update_lock);
232         currval = read_byte(client, param->msb[0]);
233         reqval |= (currval & ~(param->mask[0] << param->shift[0]));
234         data->reg[param->msb[0]] = reqval;
235         write_byte(client, param->msb[0], reqval);
236         mutex_unlock(&data->update_lock);
237         return count;
238 }
239
240 /*
241  * 16 bit fan rpm values
242  * reported by the device as the number of 11.111us periods (90khz)
243  * between full fan rotations.  Therefore...
244  * RPM = (90000 * 60) / register value
245  */
246 static ssize_t show_fan16(struct device *dev,
247                           struct device_attribute *attr, char *buf)
248 {
249         SETUP_SHOW_data_param(dev, attr);
250         u16 regval;
251
252         mutex_lock(&data->update_lock);
253         regval = (data->reg[param->msb[0]] << 8) | data->reg[param->lsb[0]];
254         mutex_unlock(&data->update_lock);
255
256         return sprintf(buf, "%u\n",
257                        (regval == 0 ? -1 : (regval) ==
258                         0xffff ? 0 : 5400000 / regval));
259 }
260
261 static ssize_t store_fan16(struct device *dev,
262                            struct device_attribute *attr, const char *buf,
263                            size_t count)
264 {
265         SETUP_STORE_data_param(dev, attr);
266         long reqval;
267
268         if (strict_strtol(buf, 10, &reqval))
269                 return -EINVAL;
270
271         /* If a minimum RPM of zero is requested, then we set the register to
272            0xffff. This value allows the fan to be stopped completely without
273            generating an alarm. */
274         reqval =
275             (reqval <= 0 ? 0xffff : SENSORS_LIMIT(5400000 / reqval, 0, 0xfffe));
276
277         mutex_lock(&data->update_lock);
278         data->reg[param->msb[0]] = (reqval >> 8) & 0xff;
279         data->reg[param->lsb[0]] = reqval & 0xff;
280         write_byte(client, param->msb[0], data->reg[param->msb[0]]);
281         write_byte(client, param->lsb[0], data->reg[param->lsb[0]]);
282         mutex_unlock(&data->update_lock);
283
284         return count;
285 }
286
287 /*
288  * Voltages are scaled in the device so that the nominal voltage
289  * is 3/4ths of the 0-255 range (i.e. 192).
290  * If all voltages are 'normal' then all voltage registers will
291  * read 0xC0.
292  *
293  * The data sheet provides us with the 3/4 scale value for each voltage
294  * which is stored in in_scaling.  The sda->index parameter value provides
295  * the index into in_scaling.
296  *
297  * NOTE: The chip expects the first 2 inputs be 2.5 and 2.25 volts
298  * respectively. That doesn't mean that's what the motherboard provides. :)
299  */
300
301 static int asc7621_in_scaling[] = {
302         2500, 2250, 3300, 5000, 12000
303 };
304
305 static ssize_t show_in10(struct device *dev, struct device_attribute *attr,
306                          char *buf)
307 {
308         SETUP_SHOW_data_param(dev, attr);
309         u16 regval;
310         u8 nr = sda->index;
311
312         mutex_lock(&data->update_lock);
313         regval = (data->reg[param->msb[0]] << 8) | (data->reg[param->lsb[0]]);
314         mutex_unlock(&data->update_lock);
315
316         /* The LSB value is a 2-bit scaling of the MSB's LSbit value. */
317         regval = (regval >> 6) * asc7621_in_scaling[nr] / (0xc0 << 2);
318
319         return sprintf(buf, "%u\n", regval);
320 }
321
322 /* 8 bit voltage values (the mins and maxs) */
323 static ssize_t show_in8(struct device *dev, struct device_attribute *attr,
324                         char *buf)
325 {
326         SETUP_SHOW_data_param(dev, attr);
327         u8 nr = sda->index;
328
329         return sprintf(buf, "%u\n",
330                        ((data->reg[param->msb[0]] *
331                          asc7621_in_scaling[nr]) / 0xc0));
332 }
333
334 static ssize_t store_in8(struct device *dev, struct device_attribute *attr,
335                          const char *buf, size_t count)
336 {
337         SETUP_STORE_data_param(dev, attr);
338         long reqval;
339         u8 nr = sda->index;
340
341         if (strict_strtol(buf, 10, &reqval))
342                 return -EINVAL;
343
344         reqval = SENSORS_LIMIT(reqval, 0, 0xffff);
345
346         reqval = reqval * 0xc0 / asc7621_in_scaling[nr];
347
348         reqval = SENSORS_LIMIT(reqval, 0, 0xff);
349
350         mutex_lock(&data->update_lock);
351         data->reg[param->msb[0]] = reqval;
352         write_byte(client, param->msb[0], reqval);
353         mutex_unlock(&data->update_lock);
354
355         return count;
356 }
357
358 static ssize_t show_temp8(struct device *dev,
359                           struct device_attribute *attr, char *buf)
360 {
361         SETUP_SHOW_data_param(dev, attr);
362
363         return sprintf(buf, "%d\n", ((s8) data->reg[param->msb[0]]) * 1000);
364 }
365
366 static ssize_t store_temp8(struct device *dev,
367                            struct device_attribute *attr, const char *buf,
368                            size_t count)
369 {
370         SETUP_STORE_data_param(dev, attr);
371         long reqval;
372         s8 temp;
373
374         if (strict_strtol(buf, 10, &reqval))
375                 return -EINVAL;
376
377         reqval = SENSORS_LIMIT(reqval, -127000, 127000);
378
379         temp = reqval / 1000;
380
381         mutex_lock(&data->update_lock);
382         data->reg[param->msb[0]] = temp;
383         write_byte(client, param->msb[0], temp);
384         mutex_unlock(&data->update_lock);
385         return count;
386 }
387
388 /*
389  * Temperatures that occupy 2 bytes always have the whole
390  * number of degrees in the MSB with some part of the LSB
391  * indicating fractional degrees.
392  */
393
394 /*   mmmmmmmm.llxxxxxx */
395 static ssize_t show_temp10(struct device *dev,
396                            struct device_attribute *attr, char *buf)
397 {
398         SETUP_SHOW_data_param(dev, attr);
399         u8 msb, lsb;
400         int temp;
401
402         mutex_lock(&data->update_lock);
403         msb = data->reg[param->msb[0]];
404         lsb = (data->reg[param->lsb[0]] >> 6) & 0x03;
405         temp = (((s8) msb) * 1000) + (lsb * 250);
406         mutex_unlock(&data->update_lock);
407
408         return sprintf(buf, "%d\n", temp);
409 }
410
411 /*   mmmmmm.ll */
412 static ssize_t show_temp62(struct device *dev,
413                            struct device_attribute *attr, char *buf)
414 {
415         SETUP_SHOW_data_param(dev, attr);
416         u8 regval = data->reg[param->msb[0]];
417         int temp = ((s8) (regval & 0xfc) * 1000) + ((regval & 0x03) * 250);
418
419         return sprintf(buf, "%d\n", temp);
420 }
421
422 static ssize_t store_temp62(struct device *dev,
423                             struct device_attribute *attr, const char *buf,
424                             size_t count)
425 {
426         SETUP_STORE_data_param(dev, attr);
427         long reqval, i, f;
428         s8 temp;
429
430         if (strict_strtol(buf, 10, &reqval))
431                 return -EINVAL;
432
433         reqval = SENSORS_LIMIT(reqval, -32000, 31750);
434         i = reqval / 1000;
435         f = reqval - (i * 1000);
436         temp = i << 2;
437         temp |= f / 250;
438
439         mutex_lock(&data->update_lock);
440         data->reg[param->msb[0]] = temp;
441         write_byte(client, param->msb[0], temp);
442         mutex_unlock(&data->update_lock);
443         return count;
444 }
445
446 /*
447  * The aSC7621 doesn't provide an "auto_point2".  Instead, you
448  * specify the auto_point1 and a range.  To keep with the sysfs
449  * hwmon specs, we synthesize the auto_point_2 from them.
450  */
451
452 static u32 asc7621_range_map[] = {
453         2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
454         13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
455 };
456
457 static ssize_t show_ap2_temp(struct device *dev,
458                              struct device_attribute *attr, char *buf)
459 {
460         SETUP_SHOW_data_param(dev, attr);
461         long auto_point1;
462         u8 regval;
463         int temp;
464
465         mutex_lock(&data->update_lock);
466         auto_point1 = ((s8) data->reg[param->msb[1]]) * 1000;
467         regval =
468             ((data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0]);
469         temp = auto_point1 + asc7621_range_map[SENSORS_LIMIT(regval, 0, 15)];
470         mutex_unlock(&data->update_lock);
471
472         return sprintf(buf, "%d\n", temp);
473
474 }
475
476 static ssize_t store_ap2_temp(struct device *dev,
477                               struct device_attribute *attr,
478                               const char *buf, size_t count)
479 {
480         SETUP_STORE_data_param(dev, attr);
481         long reqval, auto_point1;
482         int i;
483         u8 currval, newval = 0;
484
485         if (strict_strtol(buf, 10, &reqval))
486                 return -EINVAL;
487
488         mutex_lock(&data->update_lock);
489         auto_point1 = data->reg[param->msb[1]] * 1000;
490         reqval = SENSORS_LIMIT(reqval, auto_point1 + 2000, auto_point1 + 80000);
491
492         for (i = ARRAY_SIZE(asc7621_range_map) - 1; i >= 0; i--) {
493                 if (reqval >= auto_point1 + asc7621_range_map[i]) {
494                         newval = i;
495                         break;
496                 }
497         }
498
499         newval = (newval & param->mask[0]) << param->shift[0];
500         currval = read_byte(client, param->msb[0]);
501         newval |= (currval & ~(param->mask[0] << param->shift[0]));
502         data->reg[param->msb[0]] = newval;
503         write_byte(client, param->msb[0], newval);
504         mutex_unlock(&data->update_lock);
505         return count;
506 }
507
508 static ssize_t show_pwm_ac(struct device *dev,
509                            struct device_attribute *attr, char *buf)
510 {
511         SETUP_SHOW_data_param(dev, attr);
512         u8 config, altbit, regval;
513         u8 map[] = {
514                 0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
515                 0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
516         };
517
518         mutex_lock(&data->update_lock);
519         config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
520         altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
521         regval = config | (altbit << 3);
522         mutex_unlock(&data->update_lock);
523
524         return sprintf(buf, "%u\n", map[SENSORS_LIMIT(regval, 0, 15)]);
525 }
526
527 static ssize_t store_pwm_ac(struct device *dev,
528                             struct device_attribute *attr,
529                             const char *buf, size_t count)
530 {
531         SETUP_STORE_data_param(dev, attr);
532         unsigned long reqval;
533         u8 currval, config, altbit, newval;
534         u16 map[] = {
535                 0x04, 0x00, 0x01, 0xff, 0x02, 0xff, 0x05, 0x06,
536                 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
537                 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
538                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
539         };
540
541         if (strict_strtoul(buf, 10, &reqval))
542                 return -EINVAL;
543
544         if (reqval > 31)
545                 return -EINVAL;
546
547         reqval = map[reqval];
548         if (reqval == 0xff)
549                 return -EINVAL;
550
551         config = reqval & 0x07;
552         altbit = (reqval >> 3) & 0x01;
553
554         config = (config & param->mask[0]) << param->shift[0];
555         altbit = (altbit & param->mask[1]) << param->shift[1];
556
557         mutex_lock(&data->update_lock);
558         currval = read_byte(client, param->msb[0]);
559         newval = config | (currval & ~(param->mask[0] << param->shift[0]));
560         newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
561         data->reg[param->msb[0]] = newval;
562         write_byte(client, param->msb[0], newval);
563         mutex_unlock(&data->update_lock);
564         return count;
565 }
566
567 static ssize_t show_pwm_enable(struct device *dev,
568                                struct device_attribute *attr, char *buf)
569 {
570         SETUP_SHOW_data_param(dev, attr);
571         u8 config, altbit, minoff, val, newval;
572
573         mutex_lock(&data->update_lock);
574         config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
575         altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
576         minoff = (data->reg[param->msb[2]] >> param->shift[2]) & param->mask[2];
577         mutex_unlock(&data->update_lock);
578
579         val = config | (altbit << 3);
580         newval = 0;
581
582         if (val == 3 || val >= 10)
583                 newval = 255;
584         else if (val == 4)
585                 newval = 0;
586         else if (val == 7)
587                 newval = 1;
588         else if (minoff == 1)
589                 newval = 2;
590         else
591                 newval = 3;
592
593         return sprintf(buf, "%u\n", newval);
594 }
595
596 static ssize_t store_pwm_enable(struct device *dev,
597                                 struct device_attribute *attr,
598                                 const char *buf, size_t count)
599 {
600         SETUP_STORE_data_param(dev, attr);
601         long reqval;
602         u8 currval, config, altbit, newval, minoff = 255;
603
604         if (strict_strtol(buf, 10, &reqval))
605                 return -EINVAL;
606
607         switch (reqval) {
608         case 0:
609                 newval = 0x04;
610                 break;
611         case 1:
612                 newval = 0x07;
613                 break;
614         case 2:
615                 newval = 0x00;
616                 minoff = 1;
617                 break;
618         case 3:
619                 newval = 0x00;
620                 minoff = 0;
621                 break;
622         case 255:
623                 newval = 0x03;
624                 break;
625         default:
626                 return -EINVAL;
627         }
628
629         config = newval & 0x07;
630         altbit = (newval >> 3) & 0x01;
631
632         mutex_lock(&data->update_lock);
633         config = (config & param->mask[0]) << param->shift[0];
634         altbit = (altbit & param->mask[1]) << param->shift[1];
635         currval = read_byte(client, param->msb[0]);
636         newval = config | (currval & ~(param->mask[0] << param->shift[0]));
637         newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
638         data->reg[param->msb[0]] = newval;
639         write_byte(client, param->msb[0], newval);
640         if (minoff < 255) {
641                 minoff = (minoff & param->mask[2]) << param->shift[2];
642                 currval = read_byte(client, param->msb[2]);
643                 newval =
644                     minoff | (currval & ~(param->mask[2] << param->shift[2]));
645                 data->reg[param->msb[2]] = newval;
646                 write_byte(client, param->msb[2], newval);
647         }
648         mutex_unlock(&data->update_lock);
649         return count;
650 }
651
652 static u32 asc7621_pwm_freq_map[] = {
653         10, 15, 23, 30, 38, 47, 62, 94,
654         23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000
655 };
656
657 static ssize_t show_pwm_freq(struct device *dev,
658                              struct device_attribute *attr, char *buf)
659 {
660         SETUP_SHOW_data_param(dev, attr);
661         u8 regval =
662             (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
663
664         regval = SENSORS_LIMIT(regval, 0, 15);
665
666         return sprintf(buf, "%u\n", asc7621_pwm_freq_map[regval]);
667 }
668
669 static ssize_t store_pwm_freq(struct device *dev,
670                               struct device_attribute *attr,
671                               const char *buf, size_t count)
672 {
673         SETUP_STORE_data_param(dev, attr);
674         unsigned long reqval;
675         u8 currval, newval = 255;
676         int i;
677
678         if (strict_strtoul(buf, 10, &reqval))
679                 return -EINVAL;
680
681         for (i = 0; i < ARRAY_SIZE(asc7621_pwm_freq_map); i++) {
682                 if (reqval == asc7621_pwm_freq_map[i]) {
683                         newval = i;
684                         break;
685                 }
686         }
687         if (newval == 255)
688                 return -EINVAL;
689
690         newval = (newval & param->mask[0]) << param->shift[0];
691
692         mutex_lock(&data->update_lock);
693         currval = read_byte(client, param->msb[0]);
694         newval |= (currval & ~(param->mask[0] << param->shift[0]));
695         data->reg[param->msb[0]] = newval;
696         write_byte(client, param->msb[0], newval);
697         mutex_unlock(&data->update_lock);
698         return count;
699 }
700
701 static u32 asc7621_pwm_auto_spinup_map[] =  {
702         0, 100, 250, 400, 700, 1000, 2000, 4000
703 };
704
705 static ssize_t show_pwm_ast(struct device *dev,
706                             struct device_attribute *attr, char *buf)
707 {
708         SETUP_SHOW_data_param(dev, attr);
709         u8 regval =
710             (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
711
712         regval = SENSORS_LIMIT(regval, 0, 7);
713
714         return sprintf(buf, "%u\n", asc7621_pwm_auto_spinup_map[regval]);
715
716 }
717
718 static ssize_t store_pwm_ast(struct device *dev,
719                              struct device_attribute *attr,
720                              const char *buf, size_t count)
721 {
722         SETUP_STORE_data_param(dev, attr);
723         long reqval;
724         u8 currval, newval = 255;
725         u32 i;
726
727         if (strict_strtol(buf, 10, &reqval))
728                 return -EINVAL;
729
730         for (i = 0; i < ARRAY_SIZE(asc7621_pwm_auto_spinup_map); i++) {
731                 if (reqval == asc7621_pwm_auto_spinup_map[i]) {
732                         newval = i;
733                         break;
734                 }
735         }
736         if (newval == 255)
737                 return -EINVAL;
738
739         newval = (newval & param->mask[0]) << param->shift[0];
740
741         mutex_lock(&data->update_lock);
742         currval = read_byte(client, param->msb[0]);
743         newval |= (currval & ~(param->mask[0] << param->shift[0]));
744         data->reg[param->msb[0]] = newval;
745         write_byte(client, param->msb[0], newval);
746         mutex_unlock(&data->update_lock);
747         return count;
748 }
749
750 static u32 asc7621_temp_smoothing_time_map[] = {
751         35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
752 };
753
754 static ssize_t show_temp_st(struct device *dev,
755                             struct device_attribute *attr, char *buf)
756 {
757         SETUP_SHOW_data_param(dev, attr);
758         u8 regval =
759             (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
760         regval = SENSORS_LIMIT(regval, 0, 7);
761
762         return sprintf(buf, "%u\n", asc7621_temp_smoothing_time_map[regval]);
763 }
764
765 static ssize_t store_temp_st(struct device *dev,
766                              struct device_attribute *attr,
767                              const char *buf, size_t count)
768 {
769         SETUP_STORE_data_param(dev, attr);
770         long reqval;
771         u8 currval, newval = 255;
772         u32 i;
773
774         if (strict_strtol(buf, 10, &reqval))
775                 return -EINVAL;
776
777         for (i = 0; i < ARRAY_SIZE(asc7621_temp_smoothing_time_map); i++) {
778                 if (reqval == asc7621_temp_smoothing_time_map[i]) {
779                         newval = i;
780                         break;
781                 }
782         }
783
784         if (newval == 255)
785                 return -EINVAL;
786
787         newval = (newval & param->mask[0]) << param->shift[0];
788
789         mutex_lock(&data->update_lock);
790         currval = read_byte(client, param->msb[0]);
791         newval |= (currval & ~(param->mask[0] << param->shift[0]));
792         data->reg[param->msb[0]] = newval;
793         write_byte(client, param->msb[0], newval);
794         mutex_unlock(&data->update_lock);
795         return count;
796 }
797
798 /*
799  * End of data handlers
800  *
801  * These defines do nothing more than make the table easier
802  * to read when wrapped at column 80.
803  */
804
805 /*
806  * Creates a variable length array inititalizer.
807  * VAA(1,3,5,7) would produce {1,3,5,7}
808  */
809 #define VAA(args...) {args}
810
811 #define PREAD(name, n, pri, rm, rl, m, s, r) \
812         {.sda = SENSOR_ATTR(name, S_IRUGO, show_##r, NULL, n), \
813           .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
814           .shift[0] = s,}
815
816 #define PWRITE(name, n, pri, rm, rl, m, s, r) \
817         {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
818           .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
819           .shift[0] = s,}
820
821 /*
822  * PWRITEM assumes that the initializers for the .msb, .lsb, .mask and .shift
823  * were created using the VAA macro.
824  */
825 #define PWRITEM(name, n, pri, rm, rl, m, s, r) \
826         {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
827           .priority = pri, .msb = rm, .lsb = rl, .mask = m, .shift = s,}
828
829 static struct asc7621_param asc7621_params[] = {
830         PREAD(in0_input, 0, PRI_HIGH, 0x20, 0x13, 0, 0, in10),
831         PREAD(in1_input, 1, PRI_HIGH, 0x21, 0x18, 0, 0, in10),
832         PREAD(in2_input, 2, PRI_HIGH, 0x22, 0x11, 0, 0, in10),
833         PREAD(in3_input, 3, PRI_HIGH, 0x23, 0x12, 0, 0, in10),
834         PREAD(in4_input, 4, PRI_HIGH, 0x24, 0x14, 0, 0, in10),
835
836         PWRITE(in0_min, 0, PRI_LOW, 0x44, 0, 0, 0, in8),
837         PWRITE(in1_min, 1, PRI_LOW, 0x46, 0, 0, 0, in8),
838         PWRITE(in2_min, 2, PRI_LOW, 0x48, 0, 0, 0, in8),
839         PWRITE(in3_min, 3, PRI_LOW, 0x4a, 0, 0, 0, in8),
840         PWRITE(in4_min, 4, PRI_LOW, 0x4c, 0, 0, 0, in8),
841
842         PWRITE(in0_max, 0, PRI_LOW, 0x45, 0, 0, 0, in8),
843         PWRITE(in1_max, 1, PRI_LOW, 0x47, 0, 0, 0, in8),
844         PWRITE(in2_max, 2, PRI_LOW, 0x49, 0, 0, 0, in8),
845         PWRITE(in3_max, 3, PRI_LOW, 0x4b, 0, 0, 0, in8),
846         PWRITE(in4_max, 4, PRI_LOW, 0x4d, 0, 0, 0, in8),
847
848         PREAD(in0_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 0, bitmask),
849         PREAD(in1_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 1, bitmask),
850         PREAD(in2_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 2, bitmask),
851         PREAD(in3_alarm, 3, PRI_HIGH, 0x41, 0, 0x01, 3, bitmask),
852         PREAD(in4_alarm, 4, PRI_HIGH, 0x42, 0, 0x01, 0, bitmask),
853
854         PREAD(fan1_input, 0, PRI_HIGH, 0x29, 0x28, 0, 0, fan16),
855         PREAD(fan2_input, 1, PRI_HIGH, 0x2b, 0x2a, 0, 0, fan16),
856         PREAD(fan3_input, 2, PRI_HIGH, 0x2d, 0x2c, 0, 0, fan16),
857         PREAD(fan4_input, 3, PRI_HIGH, 0x2f, 0x2e, 0, 0, fan16),
858
859         PWRITE(fan1_min, 0, PRI_LOW, 0x55, 0x54, 0, 0, fan16),
860         PWRITE(fan2_min, 1, PRI_LOW, 0x57, 0x56, 0, 0, fan16),
861         PWRITE(fan3_min, 2, PRI_LOW, 0x59, 0x58, 0, 0, fan16),
862         PWRITE(fan4_min, 3, PRI_LOW, 0x5b, 0x5a, 0, 0, fan16),
863
864         PREAD(fan1_alarm, 0, PRI_HIGH, 0x42, 0, 0x01, 2, bitmask),
865         PREAD(fan2_alarm, 1, PRI_HIGH, 0x42, 0, 0x01, 3, bitmask),
866         PREAD(fan3_alarm, 2, PRI_HIGH, 0x42, 0, 0x01, 4, bitmask),
867         PREAD(fan4_alarm, 3, PRI_HIGH, 0x42, 0, 0x01, 5, bitmask),
868
869         PREAD(temp1_input, 0, PRI_HIGH, 0x25, 0x10, 0, 0, temp10),
870         PREAD(temp2_input, 1, PRI_HIGH, 0x26, 0x15, 0, 0, temp10),
871         PREAD(temp3_input, 2, PRI_HIGH, 0x27, 0x16, 0, 0, temp10),
872         PREAD(temp4_input, 3, PRI_HIGH, 0x33, 0x17, 0, 0, temp10),
873         PREAD(temp5_input, 4, PRI_HIGH, 0xf7, 0xf6, 0, 0, temp10),
874         PREAD(temp6_input, 5, PRI_HIGH, 0xf9, 0xf8, 0, 0, temp10),
875         PREAD(temp7_input, 6, PRI_HIGH, 0xfb, 0xfa, 0, 0, temp10),
876         PREAD(temp8_input, 7, PRI_HIGH, 0xfd, 0xfc, 0, 0, temp10),
877
878         PWRITE(temp1_min, 0, PRI_LOW, 0x4e, 0, 0, 0, temp8),
879         PWRITE(temp2_min, 1, PRI_LOW, 0x50, 0, 0, 0, temp8),
880         PWRITE(temp3_min, 2, PRI_LOW, 0x52, 0, 0, 0, temp8),
881         PWRITE(temp4_min, 3, PRI_LOW, 0x34, 0, 0, 0, temp8),
882
883         PWRITE(temp1_max, 0, PRI_LOW, 0x4f, 0, 0, 0, temp8),
884         PWRITE(temp2_max, 1, PRI_LOW, 0x51, 0, 0, 0, temp8),
885         PWRITE(temp3_max, 2, PRI_LOW, 0x53, 0, 0, 0, temp8),
886         PWRITE(temp4_max, 3, PRI_LOW, 0x35, 0, 0, 0, temp8),
887
888         PREAD(temp1_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 4, bitmask),
889         PREAD(temp2_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 5, bitmask),
890         PREAD(temp3_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 6, bitmask),
891         PREAD(temp4_alarm, 3, PRI_HIGH, 0x43, 0, 0x01, 0, bitmask),
892
893         PWRITE(temp1_source, 0, PRI_LOW, 0x02, 0, 0x07, 4, bitmask),
894         PWRITE(temp2_source, 1, PRI_LOW, 0x02, 0, 0x07, 0, bitmask),
895         PWRITE(temp3_source, 2, PRI_LOW, 0x03, 0, 0x07, 4, bitmask),
896         PWRITE(temp4_source, 3, PRI_LOW, 0x03, 0, 0x07, 0, bitmask),
897
898         PWRITE(temp1_smoothing_enable, 0, PRI_LOW, 0x62, 0, 0x01, 3, bitmask),
899         PWRITE(temp2_smoothing_enable, 1, PRI_LOW, 0x63, 0, 0x01, 7, bitmask),
900         PWRITE(temp3_smoothing_enable, 2, PRI_LOW, 0x63, 0, 0x01, 3, bitmask),
901         PWRITE(temp4_smoothing_enable, 3, PRI_LOW, 0x3c, 0, 0x01, 3, bitmask),
902
903         PWRITE(temp1_smoothing_time, 0, PRI_LOW, 0x62, 0, 0x07, 0, temp_st),
904         PWRITE(temp2_smoothing_time, 1, PRI_LOW, 0x63, 0, 0x07, 4, temp_st),
905         PWRITE(temp3_smoothing_time, 2, PRI_LOW, 0x63, 0, 0x07, 0, temp_st),
906         PWRITE(temp4_smoothing_time, 3, PRI_LOW, 0x3c, 0, 0x07, 0, temp_st),
907
908         PWRITE(temp1_auto_point1_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
909                bitmask),
910         PWRITE(temp2_auto_point1_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
911                bitmask),
912         PWRITE(temp3_auto_point1_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
913                bitmask),
914         PWRITE(temp4_auto_point1_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
915                bitmask),
916
917         PREAD(temp1_auto_point2_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
918               bitmask),
919         PREAD(temp2_auto_point2_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
920               bitmask),
921         PREAD(temp3_auto_point2_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
922               bitmask),
923         PREAD(temp4_auto_point2_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
924               bitmask),
925
926         PWRITE(temp1_auto_point1_temp, 0, PRI_LOW, 0x67, 0, 0, 0, temp8),
927         PWRITE(temp2_auto_point1_temp, 1, PRI_LOW, 0x68, 0, 0, 0, temp8),
928         PWRITE(temp3_auto_point1_temp, 2, PRI_LOW, 0x69, 0, 0, 0, temp8),
929         PWRITE(temp4_auto_point1_temp, 3, PRI_LOW, 0x3b, 0, 0, 0, temp8),
930
931         PWRITEM(temp1_auto_point2_temp, 0, PRI_LOW, VAA(0x5f, 0x67), VAA(0),
932                 VAA(0x0f), VAA(4), ap2_temp),
933         PWRITEM(temp2_auto_point2_temp, 1, PRI_LOW, VAA(0x60, 0x68), VAA(0),
934                 VAA(0x0f), VAA(4), ap2_temp),
935         PWRITEM(temp3_auto_point2_temp, 2, PRI_LOW, VAA(0x61, 0x69), VAA(0),
936                 VAA(0x0f), VAA(4), ap2_temp),
937         PWRITEM(temp4_auto_point2_temp, 3, PRI_LOW, VAA(0x3c, 0x3b), VAA(0),
938                 VAA(0x0f), VAA(4), ap2_temp),
939
940         PWRITE(temp1_crit, 0, PRI_LOW, 0x6a, 0, 0, 0, temp8),
941         PWRITE(temp2_crit, 1, PRI_LOW, 0x6b, 0, 0, 0, temp8),
942         PWRITE(temp3_crit, 2, PRI_LOW, 0x6c, 0, 0, 0, temp8),
943         PWRITE(temp4_crit, 3, PRI_LOW, 0x3d, 0, 0, 0, temp8),
944
945         PWRITE(temp5_enable, 4, PRI_LOW, 0x0e, 0, 0x01, 0, bitmask),
946         PWRITE(temp6_enable, 5, PRI_LOW, 0x0e, 0, 0x01, 1, bitmask),
947         PWRITE(temp7_enable, 6, PRI_LOW, 0x0e, 0, 0x01, 2, bitmask),
948         PWRITE(temp8_enable, 7, PRI_LOW, 0x0e, 0, 0x01, 3, bitmask),
949
950         PWRITE(remote1_offset, 0, PRI_LOW, 0x1c, 0, 0, 0, temp62),
951         PWRITE(remote2_offset, 1, PRI_LOW, 0x1d, 0, 0, 0, temp62),
952
953         PWRITE(pwm1, 0, PRI_HIGH, 0x30, 0, 0, 0, u8),
954         PWRITE(pwm2, 1, PRI_HIGH, 0x31, 0, 0, 0, u8),
955         PWRITE(pwm3, 2, PRI_HIGH, 0x32, 0, 0, 0, u8),
956
957         PWRITE(pwm1_invert, 0, PRI_LOW, 0x5c, 0, 0x01, 4, bitmask),
958         PWRITE(pwm2_invert, 1, PRI_LOW, 0x5d, 0, 0x01, 4, bitmask),
959         PWRITE(pwm3_invert, 2, PRI_LOW, 0x5e, 0, 0x01, 4, bitmask),
960
961         PWRITEM(pwm1_enable, 0, PRI_LOW, VAA(0x5c, 0x5c, 0x62), VAA(0, 0, 0),
962                 VAA(0x07, 0x01, 0x01), VAA(5, 3, 5), pwm_enable),
963         PWRITEM(pwm2_enable, 1, PRI_LOW, VAA(0x5d, 0x5d, 0x62), VAA(0, 0, 0),
964                 VAA(0x07, 0x01, 0x01), VAA(5, 3, 6), pwm_enable),
965         PWRITEM(pwm3_enable, 2, PRI_LOW, VAA(0x5e, 0x5e, 0x62), VAA(0, 0, 0),
966                 VAA(0x07, 0x01, 0x01), VAA(5, 3, 7), pwm_enable),
967
968         PWRITEM(pwm1_auto_channels, 0, PRI_LOW, VAA(0x5c, 0x5c), VAA(0, 0),
969                 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
970         PWRITEM(pwm2_auto_channels, 1, PRI_LOW, VAA(0x5d, 0x5d), VAA(0, 0),
971                 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
972         PWRITEM(pwm3_auto_channels, 2, PRI_LOW, VAA(0x5e, 0x5e), VAA(0, 0),
973                 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
974
975         PWRITE(pwm1_auto_point1_pwm, 0, PRI_LOW, 0x64, 0, 0, 0, u8),
976         PWRITE(pwm2_auto_point1_pwm, 1, PRI_LOW, 0x65, 0, 0, 0, u8),
977         PWRITE(pwm3_auto_point1_pwm, 2, PRI_LOW, 0x66, 0, 0, 0, u8),
978
979         PWRITE(pwm1_auto_point2_pwm, 0, PRI_LOW, 0x38, 0, 0, 0, u8),
980         PWRITE(pwm2_auto_point2_pwm, 1, PRI_LOW, 0x39, 0, 0, 0, u8),
981         PWRITE(pwm3_auto_point2_pwm, 2, PRI_LOW, 0x3a, 0, 0, 0, u8),
982
983         PWRITE(pwm1_freq, 0, PRI_LOW, 0x5f, 0, 0x0f, 0, pwm_freq),
984         PWRITE(pwm2_freq, 1, PRI_LOW, 0x60, 0, 0x0f, 0, pwm_freq),
985         PWRITE(pwm3_freq, 2, PRI_LOW, 0x61, 0, 0x0f, 0, pwm_freq),
986
987         PREAD(pwm1_auto_zone_assigned, 0, PRI_LOW, 0, 0, 0x03, 2, bitmask),
988         PREAD(pwm2_auto_zone_assigned, 1, PRI_LOW, 0, 0, 0x03, 4, bitmask),
989         PREAD(pwm3_auto_zone_assigned, 2, PRI_LOW, 0, 0, 0x03, 6, bitmask),
990
991         PWRITE(pwm1_auto_spinup_time, 0, PRI_LOW, 0x5c, 0, 0x07, 0, pwm_ast),
992         PWRITE(pwm2_auto_spinup_time, 1, PRI_LOW, 0x5d, 0, 0x07, 0, pwm_ast),
993         PWRITE(pwm3_auto_spinup_time, 2, PRI_LOW, 0x5e, 0, 0x07, 0, pwm_ast),
994
995         PWRITE(peci_enable, 0, PRI_LOW, 0x40, 0, 0x01, 4, bitmask),
996         PWRITE(peci_avg, 0, PRI_LOW, 0x36, 0, 0x07, 0, bitmask),
997         PWRITE(peci_domain, 0, PRI_LOW, 0x36, 0, 0x01, 3, bitmask),
998         PWRITE(peci_legacy, 0, PRI_LOW, 0x36, 0, 0x01, 4, bitmask),
999         PWRITE(peci_diode, 0, PRI_LOW, 0x0e, 0, 0x07, 4, bitmask),
1000         PWRITE(peci_4domain, 0, PRI_LOW, 0x0e, 0, 0x01, 4, bitmask),
1001
1002 };
1003
1004 static struct asc7621_data *asc7621_update_device(struct device *dev)
1005 {
1006         struct i2c_client *client = to_i2c_client(dev);
1007         struct asc7621_data *data = i2c_get_clientdata(client);
1008         int i;
1009
1010 /*
1011  * The asc7621 chips guarantee consistent reads of multi-byte values
1012  * regardless of the order of the reads.  No special logic is needed
1013  * so we can just read the registers in whatever  order they appear
1014  * in the asc7621_params array.
1015  */
1016
1017         mutex_lock(&data->update_lock);
1018
1019         /* Read all the high priority registers */
1020
1021         if (!data->valid ||
1022             time_after(jiffies, data->last_high_reading + INTERVAL_HIGH)) {
1023
1024                 for (i = 0; i < ARRAY_SIZE(asc7621_register_priorities); i++) {
1025                         if (asc7621_register_priorities[i] == PRI_HIGH) {
1026                                 data->reg[i] =
1027                                     i2c_smbus_read_byte_data(client, i) & 0xff;
1028                         }
1029                 }
1030                 data->last_high_reading = jiffies;
1031         };                      /* last_reading */
1032
1033         /* Read all the low priority registers. */
1034
1035         if (!data->valid ||
1036             time_after(jiffies, data->last_low_reading + INTERVAL_LOW)) {
1037
1038                 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1039                         if (asc7621_register_priorities[i] == PRI_LOW) {
1040                                 data->reg[i] =
1041                                     i2c_smbus_read_byte_data(client, i) & 0xff;
1042                         }
1043                 }
1044                 data->last_low_reading = jiffies;
1045         };                      /* last_reading */
1046
1047         data->valid = 1;
1048
1049         mutex_unlock(&data->update_lock);
1050
1051         return data;
1052 }
1053
1054 /*
1055  * Standard detection and initialization below
1056  *
1057  * Helper function that checks if an address is valid
1058  * for a particular chip.
1059  */
1060
1061 static inline int valid_address_for_chip(int chip_type, int address)
1062 {
1063         int i;
1064
1065         for (i = 0; asc7621_chips[chip_type].addresses[i] != I2C_CLIENT_END;
1066              i++) {
1067                 if (asc7621_chips[chip_type].addresses[i] == address)
1068                         return 1;
1069         }
1070         return 0;
1071 }
1072
1073 static void asc7621_init_client(struct i2c_client *client)
1074 {
1075         int value;
1076
1077         /* Warn if part was not "READY" */
1078
1079         value = read_byte(client, 0x40);
1080
1081         if (value & 0x02) {
1082                 dev_err(&client->dev,
1083                         "Client (%d,0x%02x) config is locked.\n",
1084                         i2c_adapter_id(client->adapter), client->addr);
1085         };
1086         if (!(value & 0x04)) {
1087                 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1088                         i2c_adapter_id(client->adapter), client->addr);
1089         };
1090
1091 /*
1092  * Start monitoring
1093  *
1094  * Try to clear LOCK, Set START, save everything else
1095  */
1096         value = (value & ~0x02) | 0x01;
1097         write_byte(client, 0x40, value & 0xff);
1098
1099 }
1100
1101 static int
1102 asc7621_probe(struct i2c_client *client, const struct i2c_device_id *id)
1103 {
1104         struct asc7621_data *data;
1105         int i, err;
1106
1107         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1108                 return -EIO;
1109
1110         data = kzalloc(sizeof(struct asc7621_data), GFP_KERNEL);
1111         if (data == NULL)
1112                 return -ENOMEM;
1113
1114         i2c_set_clientdata(client, data);
1115         data->valid = 0;
1116         mutex_init(&data->update_lock);
1117
1118         /* Initialize the asc7621 chip */
1119         asc7621_init_client(client);
1120
1121         /* Create the sysfs entries */
1122         for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1123                 err =
1124                     device_create_file(&client->dev,
1125                                        &(asc7621_params[i].sda.dev_attr));
1126                 if (err)
1127                         goto exit_remove;
1128         }
1129
1130         data->class_dev = hwmon_device_register(&client->dev);
1131         if (IS_ERR(data->class_dev)) {
1132                 err = PTR_ERR(data->class_dev);
1133                 goto exit_remove;
1134         }
1135
1136         return 0;
1137
1138 exit_remove:
1139         for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1140                 device_remove_file(&client->dev,
1141                                    &(asc7621_params[i].sda.dev_attr));
1142         }
1143
1144         kfree(data);
1145         return err;
1146 }
1147
1148 static int asc7621_detect(struct i2c_client *client,
1149                           struct i2c_board_info *info)
1150 {
1151         struct i2c_adapter *adapter = client->adapter;
1152         int company, verstep, chip_index;
1153
1154         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1155                 return -ENODEV;
1156
1157         for (chip_index = FIRST_CHIP; chip_index <= LAST_CHIP; chip_index++) {
1158
1159                 if (!valid_address_for_chip(chip_index, client->addr))
1160                         continue;
1161
1162                 company = read_byte(client,
1163                         asc7621_chips[chip_index].company_reg);
1164                 verstep = read_byte(client,
1165                         asc7621_chips[chip_index].verstep_reg);
1166
1167                 if (company == asc7621_chips[chip_index].company_id &&
1168                     verstep == asc7621_chips[chip_index].verstep_id) {
1169                         strlcpy(info->type, asc7621_chips[chip_index].name,
1170                                 I2C_NAME_SIZE);
1171
1172                         dev_info(&adapter->dev, "Matched %s at 0x%02x\n",
1173                                  asc7621_chips[chip_index].name, client->addr);
1174                         return 0;
1175                 }
1176         }
1177
1178         return -ENODEV;
1179 }
1180
1181 static int asc7621_remove(struct i2c_client *client)
1182 {
1183         struct asc7621_data *data = i2c_get_clientdata(client);
1184         int i;
1185
1186         hwmon_device_unregister(data->class_dev);
1187
1188         for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1189                 device_remove_file(&client->dev,
1190                                    &(asc7621_params[i].sda.dev_attr));
1191         }
1192
1193         kfree(data);
1194         return 0;
1195 }
1196
1197 static const struct i2c_device_id asc7621_id[] = {
1198         {"asc7621", asc7621},
1199         {"asc7621a", asc7621a},
1200         {},
1201 };
1202
1203 MODULE_DEVICE_TABLE(i2c, asc7621_id);
1204
1205 static struct i2c_driver asc7621_driver = {
1206         .class = I2C_CLASS_HWMON,
1207         .driver = {
1208                 .name = "asc7621",
1209         },
1210         .probe = asc7621_probe,
1211         .remove = asc7621_remove,
1212         .id_table = asc7621_id,
1213         .detect = asc7621_detect,
1214         .address_list = normal_i2c,
1215 };
1216
1217 static int __init sm_asc7621_init(void)
1218 {
1219         int i, j;
1220 /*
1221  * Collect all the registers needed into a single array.
1222  * This way, if a register isn't actually used for anything,
1223  * we don't retrieve it.
1224  */
1225
1226         for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1227                 for (j = 0; j < ARRAY_SIZE(asc7621_params[i].msb); j++)
1228                         asc7621_register_priorities[asc7621_params[i].msb[j]] =
1229                             asc7621_params[i].priority;
1230                 for (j = 0; j < ARRAY_SIZE(asc7621_params[i].lsb); j++)
1231                         asc7621_register_priorities[asc7621_params[i].lsb[j]] =
1232                             asc7621_params[i].priority;
1233         }
1234         return i2c_add_driver(&asc7621_driver);
1235 }
1236
1237 static void __exit sm_asc7621_exit(void)
1238 {
1239         i2c_del_driver(&asc7621_driver);
1240 }
1241
1242 MODULE_LICENSE("GPL");
1243 MODULE_AUTHOR("George Joseph");
1244 MODULE_DESCRIPTION("Andigilog aSC7621 and aSC7621a driver");
1245
1246 module_init(sm_asc7621_init);
1247 module_exit(sm_asc7621_exit);