patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / lm78.c
1 /*
2     lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> 
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-sensor.h>
27 #include <asm/io.h>
28
29 /* Addresses to scan */
30 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
31 static unsigned short normal_i2c_range[] = { 0x20, 0x2f, I2C_CLIENT_END };
32 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
33 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
34
35 /* Insmod parameters */
36 SENSORS_INSMOD_3(lm78, lm78j, lm79);
37
38 /* Many LM78 constants specified below */
39
40 /* Length of ISA address segment */
41 #define LM78_EXTENT 8
42
43 /* Where are the ISA address/data registers relative to the base address */
44 #define LM78_ADDR_REG_OFFSET 5
45 #define LM78_DATA_REG_OFFSET 6
46
47 /* The LM78 registers */
48 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
49 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
50 #define LM78_REG_IN(nr) (0x20 + (nr))
51
52 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
53 #define LM78_REG_FAN(nr) (0x28 + (nr))
54
55 #define LM78_REG_TEMP 0x27
56 #define LM78_REG_TEMP_OVER 0x39
57 #define LM78_REG_TEMP_HYST 0x3a
58
59 #define LM78_REG_ALARM1 0x41
60 #define LM78_REG_ALARM2 0x42
61
62 #define LM78_REG_VID_FANDIV 0x47
63
64 #define LM78_REG_CONFIG 0x40
65 #define LM78_REG_CHIPID 0x49
66 #define LM78_REG_I2C_ADDR 0x48
67
68
69 /* Conversions. Rounding and limit checking is only done on the TO_REG 
70    variants. */
71
72 /* IN: mV, (0V to 4.08V)
73    REG: 16mV/bit */
74 static inline u8 IN_TO_REG(unsigned long val)
75 {
76         unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
77         return (nval + 8) / 16;
78 }
79 #define IN_FROM_REG(val) ((val) *  16)
80
81 static inline u8 FAN_TO_REG(long rpm, int div)
82 {
83         if (rpm == 0)
84                 return 255;
85         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
86         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
87 }
88
89 static inline int FAN_FROM_REG(u8 val, int div)
90 {
91         return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
92 }
93
94 /* TEMP: mC (-128C to +127C)
95    REG: 1C/bit, two's complement */
96 static inline u8 TEMP_TO_REG(int val)
97 {
98         int nval = SENSORS_LIMIT(val, -128000, 127000) ;
99         return nval<0 ? (nval-500)/1000+0x100 : (nval+500)/1000;
100 }
101
102 static inline int TEMP_FROM_REG(u8 val)
103 {
104         return (val>=0x80 ? val-0x100 : val) * 1000;
105 }
106
107 /* VID: mV
108    REG: (see doc/vid) */
109 static inline int VID_FROM_REG(u8 val)
110 {
111         return val==0x1f ? 0 : val>=0x10 ? 5100-val*100 : 2050-val*50;
112 }
113
114 /* ALARMS: chip-specific bitmask
115    REG: (same) */
116 #define ALARMS_FROM_REG(val) (val)
117
118 /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
119    REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
120 static inline u8 DIV_TO_REG(int val)
121 {
122         return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
123 }
124 #define DIV_FROM_REG(val) (1 << (val))
125
126 /* Initial limits. To keep them sane, we use the 'standard' translation as
127    specified in the LM78 sheet. Use the config file to set better limits. */
128 #define LM78_INIT_IN_0(vid) ((vid)==3500 ? 2800 : (vid))
129 #define LM78_INIT_IN_1(vid) ((vid)==3500 ? 2800 : (vid))
130 #define LM78_INIT_IN_2 3300
131 #define LM78_INIT_IN_3 (((5000)   * 100)/168)
132 #define LM78_INIT_IN_4 (((12000)  * 10)/38)
133 #define LM78_INIT_IN_5 (((-12000) * -604)/2100)
134 #define LM78_INIT_IN_6 (((-5000)  * -604)/909)
135
136 #define LM78_INIT_IN_PERCENTAGE 10
137
138 #define LM78_INIT_IN_MIN_0(vid) (LM78_INIT_IN_0(vid) - \
139         LM78_INIT_IN_0(vid) * LM78_INIT_IN_PERCENTAGE / 100)
140 #define LM78_INIT_IN_MAX_0(vid) (LM78_INIT_IN_0(vid) + \
141         LM78_INIT_IN_0(vid) * LM78_INIT_IN_PERCENTAGE / 100)
142 #define LM78_INIT_IN_MIN_1(vid) (LM78_INIT_IN_1(vid) - \
143         LM78_INIT_IN_1(vid) * LM78_INIT_IN_PERCENTAGE / 100)
144 #define LM78_INIT_IN_MAX_1(vid) (LM78_INIT_IN_1(vid) + \
145         LM78_INIT_IN_1(vid) * LM78_INIT_IN_PERCENTAGE / 100)
146
147 #define LM78_INIT_IN_MIN_2 \
148         (LM78_INIT_IN_2 - LM78_INIT_IN_2 * LM78_INIT_IN_PERCENTAGE / 100)
149 #define LM78_INIT_IN_MAX_2 \
150         (LM78_INIT_IN_2 + LM78_INIT_IN_2 * LM78_INIT_IN_PERCENTAGE / 100)
151 #define LM78_INIT_IN_MIN_3 \
152         (LM78_INIT_IN_3 - LM78_INIT_IN_3 * LM78_INIT_IN_PERCENTAGE / 100)
153 #define LM78_INIT_IN_MAX_3 \
154         (LM78_INIT_IN_3 + LM78_INIT_IN_3 * LM78_INIT_IN_PERCENTAGE / 100)
155 #define LM78_INIT_IN_MIN_4 \
156         (LM78_INIT_IN_4 - LM78_INIT_IN_4 * LM78_INIT_IN_PERCENTAGE / 100)
157 #define LM78_INIT_IN_MAX_4 \
158         (LM78_INIT_IN_4 + LM78_INIT_IN_4 * LM78_INIT_IN_PERCENTAGE / 100)
159 #define LM78_INIT_IN_MIN_5 \
160         (LM78_INIT_IN_5 - LM78_INIT_IN_5 * LM78_INIT_IN_PERCENTAGE / 100)
161 #define LM78_INIT_IN_MAX_5 \
162         (LM78_INIT_IN_5 + LM78_INIT_IN_5 * LM78_INIT_IN_PERCENTAGE / 100)
163 #define LM78_INIT_IN_MIN_6 \
164         (LM78_INIT_IN_6 - LM78_INIT_IN_6 * LM78_INIT_IN_PERCENTAGE / 100)
165 #define LM78_INIT_IN_MAX_6 \
166         (LM78_INIT_IN_6 + LM78_INIT_IN_6 * LM78_INIT_IN_PERCENTAGE / 100)
167
168 #define LM78_INIT_FAN_MIN_1 3000
169 #define LM78_INIT_FAN_MIN_2 3000
170 #define LM78_INIT_FAN_MIN_3 3000
171
172 #define LM78_INIT_TEMP_OVER 60000
173 #define LM78_INIT_TEMP_HYST 50000
174
175 /* There are some complications in a module like this. First off, LM78 chips
176    may be both present on the SMBus and the ISA bus, and we have to handle
177    those cases separately at some places. Second, there might be several
178    LM78 chips available (well, actually, that is probably never done; but
179    it is a clean illustration of how to handle a case like that). Finally,
180    a specific chip may be attached to *both* ISA and SMBus, and we would
181    not like to detect it double. Fortunately, in the case of the LM78 at
182    least, a register tells us what SMBus address we are on, so that helps
183    a bit - except if there could be more than one SMBus. Groan. No solution
184    for this yet. */
185
186 /* This module may seem overly long and complicated. In fact, it is not so
187    bad. Quite a lot of bookkeeping is done. A real driver can often cut
188    some corners. */
189
190 /* For each registered LM78, we need to keep some data in memory. That
191    data is pointed to by lm78_list[NR]->data. The structure itself is
192    dynamically allocated, at the same time when a new lm78 client is
193    allocated. */
194 struct lm78_data {
195         struct i2c_client client;
196         struct semaphore lock;
197         enum chips type;
198
199         struct semaphore update_lock;
200         char valid;             /* !=0 if following fields are valid */
201         unsigned long last_updated;     /* In jiffies */
202
203         u8 in[7];               /* Register value */
204         u8 in_max[7];           /* Register value */
205         u8 in_min[7];           /* Register value */
206         u8 fan[3];              /* Register value */
207         u8 fan_min[3];          /* Register value */
208         u8 temp;                /* Register value */
209         u8 temp_over;           /* Register value */
210         u8 temp_hyst;           /* Register value */
211         u8 fan_div[3];          /* Register encoding, shifted right */
212         u8 vid;                 /* Register encoding, combined */
213         u16 alarms;             /* Register encoding, combined */
214 };
215
216
217 static int lm78_attach_adapter(struct i2c_adapter *adapter);
218 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
219 static int lm78_detach_client(struct i2c_client *client);
220
221 static int lm78_read_value(struct i2c_client *client, u8 register);
222 static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
223 static struct lm78_data *lm78_update_device(struct device *dev);
224 static void lm78_init_client(struct i2c_client *client);
225
226
227 static struct i2c_driver lm78_driver = {
228         .owner          = THIS_MODULE,
229         .name           = "lm78",
230         .id             = I2C_DRIVERID_LM78,
231         .flags          = I2C_DF_NOTIFY,
232         .attach_adapter = lm78_attach_adapter,
233         .detach_client  = lm78_detach_client,
234 };
235
236 /* 7 Voltages */
237 static ssize_t show_in(struct device *dev, char *buf, int nr)
238 {
239         struct lm78_data *data = lm78_update_device(dev);
240         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
241 }
242
243 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
244 {
245         struct lm78_data *data = lm78_update_device(dev);
246         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
247 }
248
249 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
250 {
251         struct lm78_data *data = lm78_update_device(dev);
252         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
253 }
254
255 static ssize_t set_in_min(struct device *dev, const char *buf,
256                 size_t count, int nr)
257 {
258         struct i2c_client *client = to_i2c_client(dev);
259         struct lm78_data *data = i2c_get_clientdata(client);
260         unsigned long val = simple_strtoul(buf, NULL, 10);
261         data->in_min[nr] = IN_TO_REG(val);
262         lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
263         return count;
264 }
265
266 static ssize_t set_in_max(struct device *dev, const char *buf,
267                 size_t count, int nr)
268 {
269         struct i2c_client *client = to_i2c_client(dev);
270         struct lm78_data *data = i2c_get_clientdata(client);
271         unsigned long val = simple_strtoul(buf, NULL, 10);
272         data->in_max[nr] = IN_TO_REG(val);
273         lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
274         return count;
275 }
276         
277 #define show_in_offset(offset)                                  \
278 static ssize_t                                                  \
279         show_in##offset (struct device *dev, char *buf)         \
280 {                                                               \
281         return show_in(dev, buf, 0x##offset);                   \
282 }                                                               \
283 static DEVICE_ATTR(in##offset##_input, S_IRUGO,                 \
284                 show_in##offset, NULL)                          \
285 static ssize_t                                                  \
286         show_in##offset##_min (struct device *dev, char *buf)   \
287 {                                                               \
288         return show_in_min(dev, buf, 0x##offset);               \
289 }                                                               \
290 static ssize_t                                                  \
291         show_in##offset##_max (struct device *dev, char *buf)   \
292 {                                                               \
293         return show_in_max(dev, buf, 0x##offset);               \
294 }                                                               \
295 static ssize_t set_in##offset##_min (struct device *dev,        \
296                 const char *buf, size_t count)                  \
297 {                                                               \
298         return set_in_min(dev, buf, count, 0x##offset);         \
299 }                                                               \
300 static ssize_t set_in##offset##_max (struct device *dev,        \
301                 const char *buf, size_t count)                  \
302 {                                                               \
303         return set_in_max(dev, buf, count, 0x##offset);         \
304 }                                                               \
305 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,         \
306                 show_in##offset##_min, set_in##offset##_min)    \
307 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,         \
308                 show_in##offset##_max, set_in##offset##_max)
309
310 show_in_offset(0);
311 show_in_offset(1);
312 show_in_offset(2);
313 show_in_offset(3);
314 show_in_offset(4);
315 show_in_offset(5);
316 show_in_offset(6);
317
318 /* Temperature */
319 static ssize_t show_temp(struct device *dev, char *buf)
320 {
321         struct lm78_data *data = lm78_update_device(dev);
322         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
323 }
324
325 static ssize_t show_temp_over(struct device *dev, char *buf)
326 {
327         struct lm78_data *data = lm78_update_device(dev);
328         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
329 }
330
331 static ssize_t set_temp_over(struct device *dev, const char *buf, size_t count)
332 {
333         struct i2c_client *client = to_i2c_client(dev);
334         struct lm78_data *data = i2c_get_clientdata(client);
335         long val = simple_strtol(buf, NULL, 10);
336         data->temp_over = TEMP_TO_REG(val);
337         lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
338         return count;
339 }
340
341 static ssize_t show_temp_hyst(struct device *dev, char *buf)
342 {
343         struct lm78_data *data = lm78_update_device(dev);
344         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
345 }
346
347 static ssize_t set_temp_hyst(struct device *dev, const char *buf, size_t count)
348 {
349         struct i2c_client *client = to_i2c_client(dev);
350         struct lm78_data *data = i2c_get_clientdata(client);
351         long val = simple_strtol(buf, NULL, 10);
352         data->temp_hyst = TEMP_TO_REG(val);
353         lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
354         return count;
355 }
356
357 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL)
358 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
359                 show_temp_over, set_temp_over)
360 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
361                 show_temp_hyst, set_temp_hyst)
362
363 /* 3 Fans */
364 static ssize_t show_fan(struct device *dev, char *buf, int nr)
365 {
366         struct lm78_data *data = lm78_update_device(dev);
367         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
368                 DIV_FROM_REG(data->fan_div[nr])) );
369 }
370
371 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
372 {
373         struct lm78_data *data = lm78_update_device(dev);
374         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
375                 DIV_FROM_REG(data->fan_div[nr])) );
376 }
377
378 static ssize_t set_fan_min(struct device *dev, const char *buf,
379                 size_t count, int nr)
380 {
381         struct i2c_client *client = to_i2c_client(dev);
382         struct lm78_data *data = i2c_get_clientdata(client);
383         unsigned long val = simple_strtoul(buf, NULL, 10);
384         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
385         lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
386         return count;
387 }
388
389 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
390 {
391         struct lm78_data *data = lm78_update_device(dev);
392         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
393 }
394
395 /* Note: we save and restore the fan minimum here, because its value is
396    determined in part by the fan divisor.  This follows the principle of
397    least suprise; the user doesn't expect the fan minimum to change just
398    because the divisor changed. */
399 static ssize_t set_fan_div(struct device *dev, const char *buf,
400         size_t count, int nr)
401 {
402         struct i2c_client *client = to_i2c_client(dev);
403         struct lm78_data *data = i2c_get_clientdata(client);
404         unsigned long min = FAN_FROM_REG(data->fan_min[nr],
405                         DIV_FROM_REG(data->fan_div[nr]));
406         unsigned long val = simple_strtoul(buf, NULL, 10);
407         int reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
408         data->fan_div[nr] = DIV_TO_REG(val);
409         switch (nr) {
410         case 0:
411                 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
412                 break;
413         case 1:
414                 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
415                 break;
416         }
417         lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
418         data->fan_min[nr] =
419                 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
420         lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
421         return count;
422 }
423
424 #define show_fan_offset(offset)                                         \
425 static ssize_t show_fan_##offset (struct device *dev, char *buf)        \
426 {                                                                       \
427         return show_fan(dev, buf, 0x##offset - 1);                      \
428 }                                                                       \
429 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf)  \
430 {                                                                       \
431         return show_fan_min(dev, buf, 0x##offset - 1);                  \
432 }                                                                       \
433 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf)  \
434 {                                                                       \
435         return show_fan_div(dev, buf, 0x##offset - 1);                  \
436 }                                                                       \
437 static ssize_t set_fan_##offset##_min (struct device *dev,              \
438                 const char *buf, size_t count)                          \
439 {                                                                       \
440         return set_fan_min(dev, buf, count, 0x##offset - 1);            \
441 }                                                                       \
442 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL) \
443 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
444                 show_fan_##offset##_min, set_fan_##offset##_min)
445
446 static ssize_t set_fan_1_div(struct device *dev, const char *buf,
447                 size_t count)
448 {
449         return set_fan_div(dev, buf, count, 0) ;
450 }
451
452 static ssize_t set_fan_2_div(struct device *dev, const char *buf,
453                 size_t count)
454 {
455         return set_fan_div(dev, buf, count, 1) ;
456 }
457
458 show_fan_offset(1);
459 show_fan_offset(2);
460 show_fan_offset(3);
461
462 /* Fan 3 divisor is locked in H/W */
463 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
464                 show_fan_1_div, set_fan_1_div)
465 static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
466                 show_fan_2_div, set_fan_2_div)
467 static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL)
468
469 /* VID */
470 static ssize_t show_vid(struct device *dev, char *buf)
471 {
472         struct lm78_data *data = lm78_update_device(dev);
473         return sprintf(buf, "%d\n", VID_FROM_REG(data->vid));
474 }
475 static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid, NULL);
476
477 /* Alarms */
478 static ssize_t show_alarms(struct device *dev, char *buf)
479 {
480         struct lm78_data *data = lm78_update_device(dev);
481         return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
482 }
483 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
484
485 /* This function is called when:
486      * lm78_driver is inserted (when this module is loaded), for each
487        available adapter
488      * when a new adapter is inserted (and lm78_driver is still present) */
489 static int lm78_attach_adapter(struct i2c_adapter *adapter)
490 {
491         if (!(adapter->class & I2C_CLASS_HWMON))
492                 return 0;
493         return i2c_detect(adapter, &addr_data, lm78_detect);
494 }
495
496 /* This function is called by i2c_detect */
497 int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
498 {
499         int i, err;
500         struct i2c_client *new_client;
501         struct lm78_data *data;
502         const char *client_name = "";
503         int is_isa = i2c_is_isa_adapter(adapter);
504
505         if (!is_isa &&
506             !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
507                 err = -ENODEV;
508                 goto ERROR0;
509         }
510
511         /* Reserve the ISA region */
512         if (is_isa)
513                 if (!request_region(address, LM78_EXTENT, "lm78")) {
514                         err = -EBUSY;
515                         goto ERROR0;
516                 }
517
518         /* Probe whether there is anything available on this address. Already
519            done for SMBus clients */
520         if (kind < 0) {
521                 if (is_isa) {
522
523 #define REALLY_SLOW_IO
524                         /* We need the timeouts for at least some LM78-like
525                            chips. But only if we read 'undefined' registers. */
526                         i = inb_p(address + 1);
527                         if (inb_p(address + 2) != i) {
528                                 err = -ENODEV;
529                                 goto ERROR1;
530                         }
531                         if (inb_p(address + 3) != i) {
532                                 err = -ENODEV;
533                                 goto ERROR1;
534                         }
535                         if (inb_p(address + 7) != i) {
536                                 err = -ENODEV;
537                                 goto ERROR1;
538                         }
539 #undef REALLY_SLOW_IO
540
541                         /* Let's just hope nothing breaks here */
542                         i = inb_p(address + 5) & 0x7f;
543                         outb_p(~i & 0x7f, address + 5);
544                         if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
545                                 outb_p(i, address + 5);
546                                 err = -ENODEV;
547                                 goto ERROR1;
548                         }
549                 }
550         }
551
552         /* OK. For now, we presume we have a valid client. We now create the
553            client structure, even though we cannot fill it completely yet.
554            But it allows us to access lm78_{read,write}_value. */
555
556         if (!(data = kmalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
557                 err = -ENOMEM;
558                 goto ERROR1;
559         }
560         memset(data, 0, sizeof(struct lm78_data));
561
562         new_client = &data->client;
563         if (is_isa)
564                 init_MUTEX(&data->lock);
565         i2c_set_clientdata(new_client, data);
566         new_client->addr = address;
567         new_client->adapter = adapter;
568         new_client->driver = &lm78_driver;
569         new_client->flags = 0;
570
571         /* Now, we do the remaining detection. */
572         if (kind < 0) {
573                 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
574                         err = -ENODEV;
575                         goto ERROR2;
576                 }
577                 if (!is_isa && (lm78_read_value(
578                                 new_client, LM78_REG_I2C_ADDR) != address)) {
579                         err = -ENODEV;
580                         goto ERROR2;
581                 }
582         }
583
584         /* Determine the chip type. */
585         if (kind <= 0) {
586                 i = lm78_read_value(new_client, LM78_REG_CHIPID);
587                 if (i == 0x00 || i == 0x20)
588                         kind = lm78;
589                 else if (i == 0x40)
590                         kind = lm78j;
591                 else if ((i & 0xfe) == 0xc0)
592                         kind = lm79;
593                 else {
594                         if (kind == 0)
595                                 dev_warn(&adapter->dev, "Ignoring 'force' "
596                                         "parameter for unknown chip at "
597                                         "adapter %d, address 0x%02x\n",
598                                         i2c_adapter_id(adapter), address);
599                         err = -ENODEV;
600                         goto ERROR2;
601                 }
602         }
603
604         if (kind == lm78) {
605                 client_name = "lm78";
606         } else if (kind == lm78j) {
607                 client_name = "lm78-j";
608         } else if (kind == lm79) {
609                 client_name = "lm79";
610         }
611
612         /* Fill in the remaining client fields and put into the global list */
613         strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
614         data->type = kind;
615
616         data->valid = 0;
617         init_MUTEX(&data->update_lock);
618
619         /* Tell the I2C layer a new client has arrived */
620         if ((err = i2c_attach_client(new_client)))
621                 goto ERROR2;
622
623         /* Initialize the LM78 chip */
624         lm78_init_client(new_client);
625
626         /* A few vars need to be filled upon startup */
627         for (i = 0; i < 3; i++) {
628                 data->fan_min[i] = lm78_read_value(new_client,
629                                         LM78_REG_FAN_MIN(i));
630         }
631
632         /* Register sysfs hooks */
633         device_create_file(&new_client->dev, &dev_attr_in0_input);
634         device_create_file(&new_client->dev, &dev_attr_in0_min);
635         device_create_file(&new_client->dev, &dev_attr_in0_max);
636         device_create_file(&new_client->dev, &dev_attr_in1_input);
637         device_create_file(&new_client->dev, &dev_attr_in1_min);
638         device_create_file(&new_client->dev, &dev_attr_in1_max);
639         device_create_file(&new_client->dev, &dev_attr_in2_input);
640         device_create_file(&new_client->dev, &dev_attr_in2_min);
641         device_create_file(&new_client->dev, &dev_attr_in2_max);
642         device_create_file(&new_client->dev, &dev_attr_in3_input);
643         device_create_file(&new_client->dev, &dev_attr_in3_min);
644         device_create_file(&new_client->dev, &dev_attr_in3_max);
645         device_create_file(&new_client->dev, &dev_attr_in4_input);
646         device_create_file(&new_client->dev, &dev_attr_in4_min);
647         device_create_file(&new_client->dev, &dev_attr_in4_max);
648         device_create_file(&new_client->dev, &dev_attr_in5_input);
649         device_create_file(&new_client->dev, &dev_attr_in5_min);
650         device_create_file(&new_client->dev, &dev_attr_in5_max);
651         device_create_file(&new_client->dev, &dev_attr_in6_input);
652         device_create_file(&new_client->dev, &dev_attr_in6_min);
653         device_create_file(&new_client->dev, &dev_attr_in6_max);
654         device_create_file(&new_client->dev, &dev_attr_temp1_input);
655         device_create_file(&new_client->dev, &dev_attr_temp1_max);
656         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
657         device_create_file(&new_client->dev, &dev_attr_fan1_input);
658         device_create_file(&new_client->dev, &dev_attr_fan1_min);
659         device_create_file(&new_client->dev, &dev_attr_fan1_div);
660         device_create_file(&new_client->dev, &dev_attr_fan2_input);
661         device_create_file(&new_client->dev, &dev_attr_fan2_min);
662         device_create_file(&new_client->dev, &dev_attr_fan2_div);
663         device_create_file(&new_client->dev, &dev_attr_fan3_input);
664         device_create_file(&new_client->dev, &dev_attr_fan3_min);
665         device_create_file(&new_client->dev, &dev_attr_fan3_div);
666         device_create_file(&new_client->dev, &dev_attr_alarms);
667         device_create_file(&new_client->dev, &dev_attr_in0_ref);
668
669         return 0;
670
671 ERROR2:
672         kfree(data);
673 ERROR1:
674         if (is_isa)
675                 release_region(address, LM78_EXTENT);
676 ERROR0:
677         return err;
678 }
679
680 static int lm78_detach_client(struct i2c_client *client)
681 {
682         int err;
683
684         /* release ISA region first */
685         if(i2c_is_isa_client(client))
686                 release_region(client->addr, LM78_EXTENT);
687
688         /* now it's safe to scrap the rest */
689         if ((err = i2c_detach_client(client))) {
690                 dev_err(&client->dev,
691                     "Client deregistration failed, client not detached.\n");
692                 return err;
693         }
694
695         kfree(i2c_get_clientdata(client));
696
697         return 0;
698 }
699
700 /* The SMBus locks itself, but ISA access must be locked explicitely! 
701    We don't want to lock the whole ISA bus, so we lock each client
702    separately.
703    We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
704    would slow down the LM78 access and should not be necessary. 
705    There are some ugly typecasts here, but the good new is - they should
706    nowhere else be necessary! */
707 static int lm78_read_value(struct i2c_client *client, u8 reg)
708 {
709         int res;
710         if (i2c_is_isa_client(client)) {
711                 struct lm78_data *data = i2c_get_clientdata(client);
712                 down(&data->lock);
713                 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
714                 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
715                 up(&data->lock);
716                 return res;
717         } else
718                 return i2c_smbus_read_byte_data(client, reg);
719 }
720
721 /* The SMBus locks itself, but ISA access muse be locked explicitely! 
722    We don't want to lock the whole ISA bus, so we lock each client
723    separately.
724    We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
725    would slow down the LM78 access and should not be necessary. 
726    There are some ugly typecasts here, but the good new is - they should
727    nowhere else be necessary! */
728 static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
729 {
730         if (i2c_is_isa_client(client)) {
731                 struct lm78_data *data = i2c_get_clientdata(client);
732                 down(&data->lock);
733                 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
734                 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
735                 up(&data->lock);
736                 return 0;
737         } else
738                 return i2c_smbus_write_byte_data(client, reg, value);
739 }
740
741 /* Called when we have found a new LM78. It should set limits, etc. */
742 static void lm78_init_client(struct i2c_client *client)
743 {
744         struct lm78_data *data = i2c_get_clientdata(client);
745         int vid;
746
747         /* Reset all except Watchdog values and last conversion values
748            This sets fan-divs to 2, among others */
749         lm78_write_value(client, LM78_REG_CONFIG, 0x80);
750
751         vid = lm78_read_value(client, LM78_REG_VID_FANDIV) & 0x0f;
752         if (data->type == lm79)
753                 vid |=
754                     (lm78_read_value(client, LM78_REG_CHIPID) & 0x01) << 4;
755         else
756                 vid |= 0x10;
757         vid = VID_FROM_REG(vid);
758
759         lm78_write_value(client, LM78_REG_IN_MIN(0),
760                          IN_TO_REG(LM78_INIT_IN_MIN_0(vid)));
761         lm78_write_value(client, LM78_REG_IN_MAX(0),
762                          IN_TO_REG(LM78_INIT_IN_MAX_0(vid)));
763         lm78_write_value(client, LM78_REG_IN_MIN(1),
764                          IN_TO_REG(LM78_INIT_IN_MIN_1(vid)));
765         lm78_write_value(client, LM78_REG_IN_MAX(1),
766                          IN_TO_REG(LM78_INIT_IN_MAX_1(vid)));
767         lm78_write_value(client, LM78_REG_IN_MIN(2),
768                          IN_TO_REG(LM78_INIT_IN_MIN_2));
769         lm78_write_value(client, LM78_REG_IN_MAX(2),
770                          IN_TO_REG(LM78_INIT_IN_MAX_2));
771         lm78_write_value(client, LM78_REG_IN_MIN(3),
772                          IN_TO_REG(LM78_INIT_IN_MIN_3));
773         lm78_write_value(client, LM78_REG_IN_MAX(3),
774                          IN_TO_REG(LM78_INIT_IN_MAX_3));
775         lm78_write_value(client, LM78_REG_IN_MIN(4),
776                          IN_TO_REG(LM78_INIT_IN_MIN_4));
777         lm78_write_value(client, LM78_REG_IN_MAX(4),
778                          IN_TO_REG(LM78_INIT_IN_MAX_4));
779         lm78_write_value(client, LM78_REG_IN_MIN(5),
780                          IN_TO_REG(LM78_INIT_IN_MIN_5));
781         lm78_write_value(client, LM78_REG_IN_MAX(5),
782                          IN_TO_REG(LM78_INIT_IN_MAX_5));
783         lm78_write_value(client, LM78_REG_IN_MIN(6),
784                          IN_TO_REG(LM78_INIT_IN_MIN_6));
785         lm78_write_value(client, LM78_REG_IN_MAX(6),
786                          IN_TO_REG(LM78_INIT_IN_MAX_6));
787         lm78_write_value(client, LM78_REG_FAN_MIN(0),
788                          FAN_TO_REG(LM78_INIT_FAN_MIN_1, 2));
789         lm78_write_value(client, LM78_REG_FAN_MIN(1),
790                          FAN_TO_REG(LM78_INIT_FAN_MIN_2, 2));
791         lm78_write_value(client, LM78_REG_FAN_MIN(2),
792                          FAN_TO_REG(LM78_INIT_FAN_MIN_3, 2));
793         lm78_write_value(client, LM78_REG_TEMP_OVER,
794                          TEMP_TO_REG(LM78_INIT_TEMP_OVER));
795         lm78_write_value(client, LM78_REG_TEMP_HYST,
796                          TEMP_TO_REG(LM78_INIT_TEMP_HYST));
797
798         /* Start monitoring */
799         lm78_write_value(client, LM78_REG_CONFIG,
800                          (lm78_read_value(client, LM78_REG_CONFIG) & 0xf7)
801                          | 0x01);
802
803 }
804
805 static struct lm78_data *lm78_update_device(struct device *dev)
806 {
807         struct i2c_client *client = to_i2c_client(dev);
808         struct lm78_data *data = i2c_get_clientdata(client);
809         int i;
810
811         down(&data->update_lock);
812
813         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
814             (jiffies < data->last_updated) || !data->valid) {
815
816                 dev_dbg(&client->dev, "Starting lm78 update\n");
817
818                 for (i = 0; i <= 6; i++) {
819                         data->in[i] =
820                             lm78_read_value(client, LM78_REG_IN(i));
821                         data->in_min[i] =
822                             lm78_read_value(client, LM78_REG_IN_MIN(i));
823                         data->in_max[i] =
824                             lm78_read_value(client, LM78_REG_IN_MAX(i));
825                 }
826                 for (i = 0; i < 3; i++) {
827                         data->fan[i] =
828                             lm78_read_value(client, LM78_REG_FAN(i));
829                         data->fan_min[i] =
830                             lm78_read_value(client, LM78_REG_FAN_MIN(i));
831                 }
832                 data->temp = lm78_read_value(client, LM78_REG_TEMP);
833                 data->temp_over =
834                     lm78_read_value(client, LM78_REG_TEMP_OVER);
835                 data->temp_hyst =
836                     lm78_read_value(client, LM78_REG_TEMP_HYST);
837                 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
838                 data->vid = i & 0x0f;
839                 if (data->type == lm79)
840                         data->vid |=
841                             (lm78_read_value(client, LM78_REG_CHIPID) &
842                              0x01) << 4;
843                 else
844                         data->vid |= 0x10;
845                 data->fan_div[0] = (i >> 4) & 0x03;
846                 data->fan_div[1] = i >> 6;
847                 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
848                     (lm78_read_value(client, LM78_REG_ALARM2) << 8);
849                 data->last_updated = jiffies;
850                 data->valid = 1;
851
852                 data->fan_div[2] = 1;
853         }
854
855         up(&data->update_lock);
856
857         return data;
858 }
859
860 static int __init sm_lm78_init(void)
861 {
862         return i2c_add_driver(&lm78_driver);
863 }
864
865 static void __exit sm_lm78_exit(void)
866 {
867         i2c_del_driver(&lm78_driver);
868 }
869
870
871
872 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
873 MODULE_DESCRIPTION("LM78, LM78-J and LM79 driver");
874 MODULE_LICENSE("GPL");
875
876 module_init(sm_lm78_init);
877 module_exit(sm_lm78_exit);