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