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