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