vserver 1.9.3
[linux-2.6.git] / drivers / i2c / chips / asb100.c
1 /*
2     asb100.c - Part of lm_sensors, Linux kernel modules for hardware
3                 monitoring
4
5     Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
6
7         (derived from w83781d.c)
8
9     Copyright (C) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
10     Philip Edelbrock <phil@netroedge.com>, and
11     Mark Studebaker <mdsxyz123@yahoo.com>
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28 /*
29     This driver supports the hardware sensor chips: Asus ASB100 and
30     ASB100-A "BACH".
31
32     ASB100-A supports pwm1, while plain ASB100 does not.  There is no known
33     way for the driver to tell which one is there.
34
35     Chip        #vin    #fanin  #pwm    #temp   wchipid vendid  i2c     ISA
36     asb100      7       3       1       4       0x31    0x0694  yes     no
37 */
38
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/ioport.h>
43 #include <linux/types.h>
44 #include <linux/i2c.h>
45 #include <linux/i2c-sensor.h>
46 #include <linux/i2c-vid.h>
47 #include <linux/init.h>
48 #include <asm/errno.h>
49 #include <asm/io.h>
50 #include "lm75.h"
51
52 /*
53         HISTORY:
54         2003-12-29      1.0.0   Ported from lm_sensors project for kernel 2.6
55 */
56 #define ASB100_VERSION "1.0.0"
57
58 /* I2C addresses to scan */
59 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
60 static unsigned short normal_i2c_range[] = { 0x28, 0x2f, I2C_CLIENT_END };
61
62 /* ISA addresses to scan (none) */
63 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
64 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
65
66 /* Insmod parameters */
67 SENSORS_INSMOD_1(asb100);
68 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
69         "{bus, clientaddr, subclientaddr1, subclientaddr2}");
70
71 /* Voltage IN registers 0-6 */
72 #define ASB100_REG_IN(nr)       (0x20 + (nr))
73 #define ASB100_REG_IN_MAX(nr)   (0x2b + (nr * 2))
74 #define ASB100_REG_IN_MIN(nr)   (0x2c + (nr * 2))
75
76 /* FAN IN registers 1-3 */
77 #define ASB100_REG_FAN(nr)      (0x28 + (nr))
78 #define ASB100_REG_FAN_MIN(nr)  (0x3b + (nr))
79
80 /* TEMPERATURE registers 1-4 */
81 static const u16 asb100_reg_temp[]      = {0, 0x27, 0x150, 0x250, 0x17};
82 static const u16 asb100_reg_temp_max[]  = {0, 0x39, 0x155, 0x255, 0x18};
83 static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
84
85 #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
86 #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
87 #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
88
89 #define ASB100_REG_TEMP2_CONFIG 0x0152
90 #define ASB100_REG_TEMP3_CONFIG 0x0252
91
92
93 #define ASB100_REG_CONFIG       0x40
94 #define ASB100_REG_ALARM1       0x41
95 #define ASB100_REG_ALARM2       0x42
96 #define ASB100_REG_SMIM1        0x43
97 #define ASB100_REG_SMIM2        0x44
98 #define ASB100_REG_VID_FANDIV   0x47
99 #define ASB100_REG_I2C_ADDR     0x48
100 #define ASB100_REG_CHIPID       0x49
101 #define ASB100_REG_I2C_SUBADDR  0x4a
102 #define ASB100_REG_PIN          0x4b
103 #define ASB100_REG_IRQ          0x4c
104 #define ASB100_REG_BANK         0x4e
105 #define ASB100_REG_CHIPMAN      0x4f
106
107 #define ASB100_REG_WCHIPID      0x58
108
109 /* bit 7 -> enable, bits 0-3 -> duty cycle */
110 #define ASB100_REG_PWM1         0x59
111
112 /* CONVERSIONS
113    Rounding and limit checking is only done on the TO_REG variants. */
114
115 /* These constants are a guess, consistent w/ w83781d */
116 #define ASB100_IN_MIN (   0)
117 #define ASB100_IN_MAX (4080)
118
119 /* IN: 1/1000 V (0V to 4.08V)
120    REG: 16mV/bit */
121 static u8 IN_TO_REG(unsigned val)
122 {
123         unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
124         return (nval + 8) / 16;
125 }
126
127 static unsigned IN_FROM_REG(u8 reg)
128 {
129         return reg * 16;
130 }
131
132 static u8 FAN_TO_REG(long rpm, int div)
133 {
134         if (rpm == -1)
135                 return 0;
136         if (rpm == 0)
137                 return 255;
138         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
139         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
140 }
141
142 static int FAN_FROM_REG(u8 val, int div)
143 {
144         return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
145 }
146
147 /* These constants are a guess, consistent w/ w83781d */
148 #define ASB100_TEMP_MIN (-128000)
149 #define ASB100_TEMP_MAX ( 127000)
150
151 /* TEMP: 0.001C/bit (-128C to +127C)
152    REG: 1C/bit, two's complement */
153 static u8 TEMP_TO_REG(int temp)
154 {
155         int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
156         ntemp += (ntemp<0 ? -500 : 500);
157         return (u8)(ntemp / 1000);
158 }
159
160 static int TEMP_FROM_REG(u8 reg)
161 {
162         return (s8)reg * 1000;
163 }
164
165 /* PWM: 0 - 255 per sensors documentation
166    REG: (6.25% duty cycle per bit) */
167 static u8 ASB100_PWM_TO_REG(int pwm)
168 {
169         pwm = SENSORS_LIMIT(pwm, 0, 255);
170         return (u8)(pwm / 16);
171 }
172
173 static int ASB100_PWM_FROM_REG(u8 reg)
174 {
175         return reg * 16;
176 }
177
178 #define ALARMS_FROM_REG(val) (val)
179
180 #define DIV_FROM_REG(val) (1 << (val))
181
182 /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
183    REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
184 static u8 DIV_TO_REG(long val)
185 {
186         return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
187 }
188
189 /* For each registered client, we need to keep some data in memory. That
190    data is pointed to by client->data. The structure itself is
191    dynamically allocated, at the same time the client itself is allocated. */
192 struct asb100_data {
193         struct i2c_client client;
194         struct semaphore lock;
195         enum chips type;
196
197         struct semaphore update_lock;
198         unsigned long last_updated;     /* In jiffies */
199
200         /* array of 2 pointers to subclients */
201         struct i2c_client *lm75[2];
202
203         char valid;             /* !=0 if following fields are valid */
204         u8 in[7];               /* Register value */
205         u8 in_max[7];           /* Register value */
206         u8 in_min[7];           /* Register value */
207         u8 fan[3];              /* Register value */
208         u8 fan_min[3];          /* Register value */
209         u16 temp[4];            /* Register value (0 and 3 are u8 only) */
210         u16 temp_max[4];        /* Register value (0 and 3 are u8 only) */
211         u16 temp_hyst[4];       /* Register value (0 and 3 are u8 only) */
212         u8 fan_div[3];          /* Register encoding, right justified */
213         u8 pwm;                 /* Register encoding */
214         u8 vid;                 /* Register encoding, combined */
215         u32 alarms;             /* Register encoding, combined */
216         u8 vrm;
217 };
218
219 static int asb100_read_value(struct i2c_client *client, u16 reg);
220 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
221
222 static int asb100_attach_adapter(struct i2c_adapter *adapter);
223 static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
224 static int asb100_detach_client(struct i2c_client *client);
225 static struct asb100_data *asb100_update_device(struct device *dev);
226 static void asb100_init_client(struct i2c_client *client);
227
228 static struct i2c_driver asb100_driver = {
229         .owner          = THIS_MODULE,
230         .name           = "asb100",
231         .id             = I2C_DRIVERID_ASB100,
232         .flags          = I2C_DF_NOTIFY,
233         .attach_adapter = asb100_attach_adapter,
234         .detach_client  = asb100_detach_client,
235 };
236
237 /* 7 Voltages */
238 #define show_in_reg(reg) \
239 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
240 { \
241         struct asb100_data *data = asb100_update_device(dev); \
242         return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
243 }
244
245 show_in_reg(in)
246 show_in_reg(in_min)
247 show_in_reg(in_max)
248
249 #define set_in_reg(REG, reg) \
250 static ssize_t set_in_##reg(struct device *dev, const char *buf, \
251                 size_t count, int nr) \
252 { \
253         struct i2c_client *client = to_i2c_client(dev); \
254         struct asb100_data *data = i2c_get_clientdata(client); \
255         unsigned long val = simple_strtoul(buf, NULL, 10); \
256         data->in_##reg[nr] = IN_TO_REG(val); \
257         asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
258                 data->in_##reg[nr]); \
259         return count; \
260 }
261
262 set_in_reg(MIN, min)
263 set_in_reg(MAX, max)
264
265 #define sysfs_in(offset) \
266 static ssize_t \
267         show_in##offset (struct device *dev, char *buf) \
268 { \
269         return show_in(dev, buf, 0x##offset); \
270 } \
271 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
272                 show_in##offset, NULL); \
273 static ssize_t \
274         show_in##offset##_min (struct device *dev, char *buf) \
275 { \
276         return show_in_min(dev, buf, 0x##offset); \
277 } \
278 static ssize_t \
279         show_in##offset##_max (struct device *dev, char *buf) \
280 { \
281         return show_in_max(dev, buf, 0x##offset); \
282 } \
283 static ssize_t set_in##offset##_min (struct device *dev, \
284                 const char *buf, size_t count) \
285 { \
286         return set_in_min(dev, buf, count, 0x##offset); \
287 } \
288 static ssize_t set_in##offset##_max (struct device *dev, \
289                 const char *buf, size_t count) \
290 { \
291         return set_in_max(dev, buf, count, 0x##offset); \
292 } \
293 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
294                 show_in##offset##_min, set_in##offset##_min); \
295 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
296                 show_in##offset##_max, set_in##offset##_max);
297
298 sysfs_in(0);
299 sysfs_in(1);
300 sysfs_in(2);
301 sysfs_in(3);
302 sysfs_in(4);
303 sysfs_in(5);
304 sysfs_in(6);
305
306 #define device_create_file_in(client, offset) do { \
307         device_create_file(&client->dev, &dev_attr_in##offset##_input); \
308         device_create_file(&client->dev, &dev_attr_in##offset##_min); \
309         device_create_file(&client->dev, &dev_attr_in##offset##_max); \
310 } while (0)
311
312 /* 3 Fans */
313 static ssize_t show_fan(struct device *dev, char *buf, int nr)
314 {
315         struct asb100_data *data = asb100_update_device(dev);
316         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
317                 DIV_FROM_REG(data->fan_div[nr])));
318 }
319
320 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
321 {
322         struct asb100_data *data = asb100_update_device(dev);
323         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
324                 DIV_FROM_REG(data->fan_div[nr])));
325 }
326
327 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
328 {
329         struct asb100_data *data = asb100_update_device(dev);
330         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
331 }
332
333 static ssize_t set_fan_min(struct device *dev, const char *buf,
334                                 size_t count, int nr)
335 {
336         struct i2c_client *client = to_i2c_client(dev);
337         struct asb100_data *data = i2c_get_clientdata(client);
338         u32 val = simple_strtoul(buf, NULL, 10);
339         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
340         asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
341         return count;
342 }
343
344 /* Note: we save and restore the fan minimum here, because its value is
345    determined in part by the fan divisor.  This follows the principle of
346    least suprise; the user doesn't expect the fan minimum to change just
347    because the divisor changed. */
348 static ssize_t set_fan_div(struct device *dev, const char *buf,
349                                 size_t count, int nr)
350 {
351         struct i2c_client *client = to_i2c_client(dev);
352         struct asb100_data *data = i2c_get_clientdata(client);
353         unsigned long min = FAN_FROM_REG(data->fan_min[nr],
354                         DIV_FROM_REG(data->fan_div[nr]));
355         unsigned long val = simple_strtoul(buf, NULL, 10);
356         int reg;
357         
358         data->fan_div[nr] = DIV_TO_REG(val);
359
360         switch(nr) {
361         case 0: /* fan 1 */
362                 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
363                 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
364                 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
365                 break;
366
367         case 1: /* fan 2 */
368                 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
369                 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
370                 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
371                 break;
372
373         case 2: /* fan 3 */
374                 reg = asb100_read_value(client, ASB100_REG_PIN);
375                 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
376                 asb100_write_value(client, ASB100_REG_PIN, reg);
377                 break;
378         }
379
380         data->fan_min[nr] =
381                 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
382         asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
383         return count;
384 }
385
386 #define sysfs_fan(offset) \
387 static ssize_t show_fan##offset(struct device *dev, char *buf) \
388 { \
389         return show_fan(dev, buf, offset - 1); \
390 } \
391 static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
392 { \
393         return show_fan_min(dev, buf, offset - 1); \
394 } \
395 static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
396 { \
397         return show_fan_div(dev, buf, offset - 1); \
398 } \
399 static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
400                                         size_t count) \
401 { \
402         return set_fan_min(dev, buf, count, offset - 1); \
403 } \
404 static ssize_t set_fan##offset##_div(struct device *dev, const char *buf, \
405                                         size_t count) \
406 { \
407         return set_fan_div(dev, buf, count, offset - 1); \
408 } \
409 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
410                 show_fan##offset, NULL); \
411 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
412                 show_fan##offset##_min, set_fan##offset##_min); \
413 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
414                 show_fan##offset##_div, set_fan##offset##_div);
415
416 sysfs_fan(1);
417 sysfs_fan(2);
418 sysfs_fan(3);
419
420 #define device_create_file_fan(client, offset) do { \
421         device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
422         device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
423         device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
424 } while (0)
425
426 /* 4 Temp. Sensors */
427 static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
428 {
429         int ret = 0;
430
431         switch (nr) {
432         case 1: case 2:
433                 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
434                 break;
435         case 0: case 3: default:
436                 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
437                 break;
438         }
439         return ret;
440 }
441                         
442 #define show_temp_reg(reg) \
443 static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
444 { \
445         struct asb100_data *data = asb100_update_device(dev); \
446         return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
447 }
448
449 show_temp_reg(temp);
450 show_temp_reg(temp_max);
451 show_temp_reg(temp_hyst);
452
453 #define set_temp_reg(REG, reg) \
454 static ssize_t set_##reg(struct device *dev, const char *buf, \
455                         size_t count, int nr) \
456 { \
457         struct i2c_client *client = to_i2c_client(dev); \
458         struct asb100_data *data = i2c_get_clientdata(client); \
459         unsigned long val = simple_strtoul(buf, NULL, 10); \
460         switch (nr) { \
461         case 1: case 2: \
462                 data->reg[nr] = LM75_TEMP_TO_REG(val); \
463                 break; \
464         case 0: case 3: default: \
465                 data->reg[nr] = TEMP_TO_REG(val); \
466                 break; \
467         } \
468         asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
469                         data->reg[nr]); \
470         return count; \
471 }
472
473 set_temp_reg(MAX, temp_max);
474 set_temp_reg(HYST, temp_hyst);
475
476 #define sysfs_temp(num) \
477 static ssize_t show_temp##num(struct device *dev, char *buf) \
478 { \
479         return show_temp(dev, buf, num-1); \
480 } \
481 static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
482 static ssize_t show_temp_max##num(struct device *dev, char *buf) \
483 { \
484         return show_temp_max(dev, buf, num-1); \
485 } \
486 static ssize_t set_temp_max##num(struct device *dev, const char *buf, \
487                                         size_t count) \
488 { \
489         return set_temp_max(dev, buf, count, num-1); \
490 } \
491 static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
492                 show_temp_max##num, set_temp_max##num); \
493 static ssize_t show_temp_hyst##num(struct device *dev, char *buf) \
494 { \
495         return show_temp_hyst(dev, buf, num-1); \
496 } \
497 static ssize_t set_temp_hyst##num(struct device *dev, const char *buf, \
498                                         size_t count) \
499 { \
500         return set_temp_hyst(dev, buf, count, num-1); \
501 } \
502 static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
503                 show_temp_hyst##num, set_temp_hyst##num);
504
505 sysfs_temp(1);
506 sysfs_temp(2);
507 sysfs_temp(3);
508 sysfs_temp(4);
509
510 /* VID */
511 #define device_create_file_temp(client, num) do { \
512         device_create_file(&client->dev, &dev_attr_temp##num##_input); \
513         device_create_file(&client->dev, &dev_attr_temp##num##_max); \
514         device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \
515 } while (0)
516
517 static ssize_t show_vid(struct device *dev, char *buf)
518 {
519         struct asb100_data *data = asb100_update_device(dev);
520         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
521 }
522
523 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
524 #define device_create_file_vid(client) \
525 device_create_file(&client->dev, &dev_attr_cpu0_vid)
526
527 /* VRM */
528 static ssize_t show_vrm(struct device *dev, char *buf)
529 {
530         struct asb100_data *data = asb100_update_device(dev);
531         return sprintf(buf, "%d\n", data->vrm);
532 }
533
534 static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
535 {
536         struct i2c_client *client = to_i2c_client(dev);
537         struct asb100_data *data = i2c_get_clientdata(client);
538         unsigned long val = simple_strtoul(buf, NULL, 10);
539         data->vrm = val;
540         return count;
541 }
542
543 /* Alarms */
544 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
545 #define device_create_file_vrm(client) \
546 device_create_file(&client->dev, &dev_attr_vrm);
547
548 static ssize_t show_alarms(struct device *dev, char *buf)
549 {
550         struct asb100_data *data = asb100_update_device(dev);
551         return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
552 }
553
554 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
555 #define device_create_file_alarms(client) \
556 device_create_file(&client->dev, &dev_attr_alarms)
557
558 /* 1 PWM */
559 static ssize_t show_pwm1(struct device *dev, char *buf)
560 {
561         struct asb100_data *data = asb100_update_device(dev);
562         return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
563 }
564
565 static ssize_t set_pwm1(struct device *dev, const char *buf, size_t count)
566 {
567         struct i2c_client *client = to_i2c_client(dev);
568         struct asb100_data *data = i2c_get_clientdata(client);
569         unsigned long val = simple_strtoul(buf, NULL, 10);
570         data->pwm &= 0x80; /* keep the enable bit */
571         data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
572         asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
573         return count;
574 }
575
576 static ssize_t show_pwm_enable1(struct device *dev, char *buf)
577 {
578         struct asb100_data *data = asb100_update_device(dev);
579         return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
580 }
581
582 static ssize_t set_pwm_enable1(struct device *dev, const char *buf,
583                                 size_t count)
584 {
585         struct i2c_client *client = to_i2c_client(dev);
586         struct asb100_data *data = i2c_get_clientdata(client);
587         unsigned long val = simple_strtoul(buf, NULL, 10);
588         data->pwm &= 0x0f; /* keep the duty cycle bits */
589         data->pwm |= (val ? 0x80 : 0x00);
590         asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
591         return count;
592 }
593
594 static DEVICE_ATTR(fan1_pwm, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
595 static DEVICE_ATTR(fan1_pwm_enable, S_IRUGO | S_IWUSR,
596                 show_pwm_enable1, set_pwm_enable1);
597 #define device_create_file_pwm1(client) do { \
598         device_create_file(&new_client->dev, &dev_attr_fan1_pwm); \
599         device_create_file(&new_client->dev, &dev_attr_fan1_pwm_enable); \
600 } while (0)
601
602 /* This function is called when:
603         asb100_driver is inserted (when this module is loaded), for each
604                 available adapter
605         when a new adapter is inserted (and asb100_driver is still present)
606  */
607 static int asb100_attach_adapter(struct i2c_adapter *adapter)
608 {
609         if (!(adapter->class & I2C_CLASS_HWMON))
610                 return 0;
611         return i2c_detect(adapter, &addr_data, asb100_detect);
612 }
613
614 static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
615                 int kind, struct i2c_client *new_client)
616 {
617         int i, id, err;
618         struct asb100_data *data = i2c_get_clientdata(new_client);
619
620         data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
621         if (!(data->lm75[0])) {
622                 err = -ENOMEM;
623                 goto ERROR_SC_0;
624         }
625         memset(data->lm75[0], 0x00, sizeof(struct i2c_client));
626
627         data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
628         if (!(data->lm75[1])) {
629                 err = -ENOMEM;
630                 goto ERROR_SC_1;
631         }
632         memset(data->lm75[1], 0x00, sizeof(struct i2c_client));
633
634         id = i2c_adapter_id(adapter);
635
636         if (force_subclients[0] == id && force_subclients[1] == address) {
637                 for (i = 2; i <= 3; i++) {
638                         if (force_subclients[i] < 0x48 ||
639                             force_subclients[i] > 0x4f) {
640                                 dev_err(&new_client->dev, "invalid subclient "
641                                         "address %d; must be 0x48-0x4f\n",
642                                         force_subclients[i]);
643                                 err = -ENODEV;
644                                 goto ERROR_SC_2;
645                         }
646                 }
647                 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
648                                         (force_subclients[2] & 0x07) |
649                                         ((force_subclients[3] & 0x07) <<4));
650                 data->lm75[0]->addr = force_subclients[2];
651                 data->lm75[1]->addr = force_subclients[3];
652         } else {
653                 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
654                 data->lm75[0]->addr = 0x48 + (val & 0x07);
655                 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
656         }
657
658         if(data->lm75[0]->addr == data->lm75[1]->addr) {
659                 dev_err(&new_client->dev, "duplicate addresses 0x%x "
660                                 "for subclients\n", data->lm75[0]->addr);
661                 err = -ENODEV;
662                 goto ERROR_SC_2;
663         }
664
665         for (i = 0; i <= 1; i++) {
666                 i2c_set_clientdata(data->lm75[i], NULL);
667                 data->lm75[i]->adapter = adapter;
668                 data->lm75[i]->driver = &asb100_driver;
669                 data->lm75[i]->flags = 0;
670                 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
671         }
672
673         if ((err = i2c_attach_client(data->lm75[0]))) {
674                 dev_err(&new_client->dev, "subclient %d registration "
675                         "at address 0x%x failed.\n", i, data->lm75[0]->addr);
676                 goto ERROR_SC_2;
677         }
678
679         if ((err = i2c_attach_client(data->lm75[1]))) {
680                 dev_err(&new_client->dev, "subclient %d registration "
681                         "at address 0x%x failed.\n", i, data->lm75[1]->addr);
682                 goto ERROR_SC_3;
683         }
684
685         return 0;
686
687 /* Undo inits in case of errors */
688 ERROR_SC_3:
689         i2c_detach_client(data->lm75[0]);
690 ERROR_SC_2:
691         kfree(data->lm75[1]);
692 ERROR_SC_1:
693         kfree(data->lm75[0]);
694 ERROR_SC_0:
695         return err;
696 }
697
698 static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
699 {
700         int err;
701         struct i2c_client *new_client;
702         struct asb100_data *data;
703
704         /* asb100 is SMBus only */
705         if (i2c_is_isa_adapter(adapter)) {
706                 pr_debug("asb100.o: detect failed, "
707                                 "cannot attach to legacy adapter!\n");
708                 err = -ENODEV;
709                 goto ERROR0;
710         }
711
712         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
713                 pr_debug("asb100.o: detect failed, "
714                                 "smbus byte data not supported!\n");
715                 err = -ENODEV;
716                 goto ERROR0;
717         }
718
719         /* OK. For now, we presume we have a valid client. We now create the
720            client structure, even though we cannot fill it completely yet.
721            But it allows us to access asb100_{read,write}_value. */
722
723         if (!(data = kmalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
724                 pr_debug("asb100.o: detect failed, kmalloc failed!\n");
725                 err = -ENOMEM;
726                 goto ERROR0;
727         }
728         memset(data, 0, sizeof(struct asb100_data));
729
730         new_client = &data->client;
731         init_MUTEX(&data->lock);
732         i2c_set_clientdata(new_client, data);
733         new_client->addr = address;
734         new_client->adapter = adapter;
735         new_client->driver = &asb100_driver;
736         new_client->flags = 0;
737
738         /* Now, we do the remaining detection. */
739
740         /* The chip may be stuck in some other bank than bank 0. This may
741            make reading other information impossible. Specify a force=... or
742            force_*=... parameter, and the chip will be reset to the right
743            bank. */
744         if (kind < 0) {
745
746                 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
747                 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
748
749                 /* If we're in bank 0 */
750                 if ( (!(val1 & 0x07)) &&
751                                 /* Check for ASB100 ID (low byte) */
752                                 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
753                                 /* Check for ASB100 ID (high byte ) */
754                                 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
755                         pr_debug("asb100.o: detect failed, "
756                                         "bad chip id 0x%02x!\n", val2);
757                         err = -ENODEV;
758                         goto ERROR1;
759                 }
760
761         } /* kind < 0 */
762
763         /* We have either had a force parameter, or we have already detected
764            Winbond. Put it now into bank 0 and Vendor ID High Byte */
765         asb100_write_value(new_client, ASB100_REG_BANK,
766                 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
767
768         /* Determine the chip type. */
769         if (kind <= 0) {
770                 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
771                 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
772
773                 if ((val1 == 0x31) && (val2 == 0x06))
774                         kind = asb100;
775                 else {
776                         if (kind == 0)
777                                 dev_warn(&new_client->dev, "ignoring "
778                                         "'force' parameter for unknown chip "
779                                         "at adapter %d, address 0x%02x.\n",
780                                         i2c_adapter_id(adapter), address);
781                         err = -ENODEV;
782                         goto ERROR1;
783                 }
784         }
785
786         /* Fill in remaining client fields and put it into the global list */
787         strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
788         data->type = kind;
789
790         data->valid = 0;
791         init_MUTEX(&data->update_lock);
792
793         /* Tell the I2C layer a new client has arrived */
794         if ((err = i2c_attach_client(new_client)))
795                 goto ERROR1;
796
797         /* Attach secondary lm75 clients */
798         if ((err = asb100_detect_subclients(adapter, address, kind,
799                         new_client)))
800                 goto ERROR2;
801
802         /* Initialize the chip */
803         asb100_init_client(new_client);
804
805         /* A few vars need to be filled upon startup */
806         data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
807         data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
808         data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
809
810         /* Register sysfs hooks */
811         device_create_file_in(new_client, 0);
812         device_create_file_in(new_client, 1);
813         device_create_file_in(new_client, 2);
814         device_create_file_in(new_client, 3);
815         device_create_file_in(new_client, 4);
816         device_create_file_in(new_client, 5);
817         device_create_file_in(new_client, 6);
818
819         device_create_file_fan(new_client, 1);
820         device_create_file_fan(new_client, 2);
821         device_create_file_fan(new_client, 3);
822
823         device_create_file_temp(new_client, 1);
824         device_create_file_temp(new_client, 2);
825         device_create_file_temp(new_client, 3);
826         device_create_file_temp(new_client, 4);
827
828         device_create_file_vid(new_client);
829         device_create_file_vrm(new_client);
830
831         device_create_file_alarms(new_client);
832
833         device_create_file_pwm1(new_client);
834
835         return 0;
836
837 ERROR2:
838         i2c_detach_client(new_client);
839 ERROR1:
840         kfree(data);
841 ERROR0:
842         return err;
843 }
844
845 static int asb100_detach_client(struct i2c_client *client)
846 {
847         int err;
848
849         if ((err = i2c_detach_client(client))) {
850                 dev_err(&client->dev, "client deregistration failed; "
851                         "client not detached.\n");
852                 return err;
853         }
854
855         if (i2c_get_clientdata(client)==NULL) {
856                 /* subclients */
857                 kfree(client);
858         } else {
859                 /* main client */
860                 kfree(i2c_get_clientdata(client));
861         }
862
863         return 0;
864 }
865
866 /* The SMBus locks itself, usually, but nothing may access the chip between
867    bank switches. */
868 static int asb100_read_value(struct i2c_client *client, u16 reg)
869 {
870         struct asb100_data *data = i2c_get_clientdata(client);
871         struct i2c_client *cl;
872         int res, bank;
873
874         down(&data->lock);
875
876         bank = (reg >> 8) & 0x0f;
877         if (bank > 2)
878                 /* switch banks */
879                 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
880
881         if (bank == 0 || bank > 2) {
882                 res = i2c_smbus_read_byte_data(client, reg & 0xff);
883         } else {
884                 /* switch to subclient */
885                 cl = data->lm75[bank - 1];
886
887                 /* convert from ISA to LM75 I2C addresses */
888                 switch (reg & 0xff) {
889                 case 0x50: /* TEMP */
890                         res = swab16(i2c_smbus_read_word_data (cl, 0));
891                         break;
892                 case 0x52: /* CONFIG */
893                         res = i2c_smbus_read_byte_data(cl, 1);
894                         break;
895                 case 0x53: /* HYST */
896                         res = swab16(i2c_smbus_read_word_data (cl, 2));
897                         break;
898                 case 0x55: /* MAX */
899                 default:
900                         res = swab16(i2c_smbus_read_word_data (cl, 3));
901                         break;
902                 }
903         }
904
905         if (bank > 2)
906                 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
907
908         up(&data->lock);
909
910         return res;
911 }
912
913 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
914 {
915         struct asb100_data *data = i2c_get_clientdata(client);
916         struct i2c_client *cl;
917         int bank;
918
919         down(&data->lock);
920
921         bank = (reg >> 8) & 0x0f;
922         if (bank > 2)
923                 /* switch banks */
924                 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
925
926         if (bank == 0 || bank > 2) {
927                 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
928         } else {
929                 /* switch to subclient */
930                 cl = data->lm75[bank - 1];
931
932                 /* convert from ISA to LM75 I2C addresses */
933                 switch (reg & 0xff) {
934                 case 0x52: /* CONFIG */
935                         i2c_smbus_write_byte_data(cl, 1, value & 0xff);
936                         break;
937                 case 0x53: /* HYST */
938                         i2c_smbus_write_word_data(cl, 2, swab16(value));
939                         break;
940                 case 0x55: /* MAX */
941                         i2c_smbus_write_word_data(cl, 3, swab16(value));
942                         break;
943                 }
944         }
945
946         if (bank > 2)
947                 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
948
949         up(&data->lock);
950 }
951
952 static void asb100_init_client(struct i2c_client *client)
953 {
954         struct asb100_data *data = i2c_get_clientdata(client);
955         int vid = 0;
956
957         vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
958         vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
959         data->vrm = i2c_which_vrm();
960         vid = vid_from_reg(vid, data->vrm);
961
962         /* Start monitoring */
963         asb100_write_value(client, ASB100_REG_CONFIG, 
964                 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
965 }
966
967 static struct asb100_data *asb100_update_device(struct device *dev)
968 {
969         struct i2c_client *client = to_i2c_client(dev);
970         struct asb100_data *data = i2c_get_clientdata(client);
971         int i;
972
973         down(&data->update_lock);
974
975         if (time_after(jiffies - data->last_updated, (unsigned long)(HZ+HZ/2))
976                 || time_before(jiffies, data->last_updated) || !data->valid) {
977
978                 dev_dbg(&client->dev, "starting device update...\n");
979
980                 /* 7 voltage inputs */
981                 for (i = 0; i < 7; i++) {
982                         data->in[i] = asb100_read_value(client,
983                                 ASB100_REG_IN(i));
984                         data->in_min[i] = asb100_read_value(client,
985                                 ASB100_REG_IN_MIN(i));
986                         data->in_max[i] = asb100_read_value(client,
987                                 ASB100_REG_IN_MAX(i));
988                 }
989
990                 /* 3 fan inputs */
991                 for (i = 0; i < 3; i++) {
992                         data->fan[i] = asb100_read_value(client,
993                                         ASB100_REG_FAN(i));
994                         data->fan_min[i] = asb100_read_value(client,
995                                         ASB100_REG_FAN_MIN(i));
996                 }
997
998                 /* 4 temperature inputs */
999                 for (i = 1; i <= 4; i++) {
1000                         data->temp[i-1] = asb100_read_value(client,
1001                                         ASB100_REG_TEMP(i));
1002                         data->temp_max[i-1] = asb100_read_value(client,
1003                                         ASB100_REG_TEMP_MAX(i));
1004                         data->temp_hyst[i-1] = asb100_read_value(client,
1005                                         ASB100_REG_TEMP_HYST(i));
1006                 }
1007
1008                 /* VID and fan divisors */
1009                 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1010                 data->vid = i & 0x0f;
1011                 data->vid |= (asb100_read_value(client,
1012                                 ASB100_REG_CHIPID) & 0x01) << 4;
1013                 data->fan_div[0] = (i >> 4) & 0x03;
1014                 data->fan_div[1] = (i >> 6) & 0x03;
1015                 data->fan_div[2] = (asb100_read_value(client,
1016                                 ASB100_REG_PIN) >> 6) & 0x03;
1017
1018                 /* PWM */
1019                 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1020
1021                 /* alarms */
1022                 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1023                         (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1024
1025                 data->last_updated = jiffies;
1026                 data->valid = 1;
1027
1028                 dev_dbg(&client->dev, "... device update complete\n");
1029         }
1030
1031         up(&data->update_lock);
1032
1033         return data;
1034 }
1035
1036 static int __init asb100_init(void)
1037 {
1038         return i2c_add_driver(&asb100_driver);
1039 }
1040
1041 static void __exit asb100_exit(void)
1042 {
1043         i2c_del_driver(&asb100_driver);
1044 }
1045
1046 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1047 MODULE_DESCRIPTION("ASB100 Bach driver");
1048 MODULE_LICENSE("GPL");
1049
1050 module_init(asb100_init);
1051 module_exit(asb100_exit);
1052