vserver 1.9.3
[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 /* There are some complications in a module like this. First off, LM78 chips
127    may be both present on the SMBus and the ISA bus, and we have to handle
128    those cases separately at some places. Second, there might be several
129    LM78 chips available (well, actually, that is probably never done; but
130    it is a clean illustration of how to handle a case like that). Finally,
131    a specific chip may be attached to *both* ISA and SMBus, and we would
132    not like to detect it double. Fortunately, in the case of the LM78 at
133    least, a register tells us what SMBus address we are on, so that helps
134    a bit - except if there could be more than one SMBus. Groan. No solution
135    for this yet. */
136
137 /* This module may seem overly long and complicated. In fact, it is not so
138    bad. Quite a lot of bookkeeping is done. A real driver can often cut
139    some corners. */
140
141 /* For each registered LM78, we need to keep some data in memory. That
142    data is pointed to by lm78_list[NR]->data. The structure itself is
143    dynamically allocated, at the same time when a new lm78 client is
144    allocated. */
145 struct lm78_data {
146         struct i2c_client client;
147         struct semaphore lock;
148         enum chips type;
149
150         struct semaphore update_lock;
151         char valid;             /* !=0 if following fields are valid */
152         unsigned long last_updated;     /* In jiffies */
153
154         u8 in[7];               /* Register value */
155         u8 in_max[7];           /* Register value */
156         u8 in_min[7];           /* Register value */
157         u8 fan[3];              /* Register value */
158         u8 fan_min[3];          /* Register value */
159         u8 temp;                /* Register value */
160         u8 temp_over;           /* Register value */
161         u8 temp_hyst;           /* Register value */
162         u8 fan_div[3];          /* Register encoding, shifted right */
163         u8 vid;                 /* Register encoding, combined */
164         u16 alarms;             /* Register encoding, combined */
165 };
166
167
168 static int lm78_attach_adapter(struct i2c_adapter *adapter);
169 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
170 static int lm78_detach_client(struct i2c_client *client);
171
172 static int lm78_read_value(struct i2c_client *client, u8 register);
173 static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
174 static struct lm78_data *lm78_update_device(struct device *dev);
175 static void lm78_init_client(struct i2c_client *client);
176
177
178 static struct i2c_driver lm78_driver = {
179         .owner          = THIS_MODULE,
180         .name           = "lm78",
181         .id             = I2C_DRIVERID_LM78,
182         .flags          = I2C_DF_NOTIFY,
183         .attach_adapter = lm78_attach_adapter,
184         .detach_client  = lm78_detach_client,
185 };
186
187 /* 7 Voltages */
188 static ssize_t show_in(struct device *dev, char *buf, int nr)
189 {
190         struct lm78_data *data = lm78_update_device(dev);
191         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
192 }
193
194 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
195 {
196         struct lm78_data *data = lm78_update_device(dev);
197         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
198 }
199
200 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
201 {
202         struct lm78_data *data = lm78_update_device(dev);
203         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
204 }
205
206 static ssize_t set_in_min(struct device *dev, const char *buf,
207                 size_t count, int nr)
208 {
209         struct i2c_client *client = to_i2c_client(dev);
210         struct lm78_data *data = i2c_get_clientdata(client);
211         unsigned long val = simple_strtoul(buf, NULL, 10);
212         data->in_min[nr] = IN_TO_REG(val);
213         lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
214         return count;
215 }
216
217 static ssize_t set_in_max(struct device *dev, const char *buf,
218                 size_t count, int nr)
219 {
220         struct i2c_client *client = to_i2c_client(dev);
221         struct lm78_data *data = i2c_get_clientdata(client);
222         unsigned long val = simple_strtoul(buf, NULL, 10);
223         data->in_max[nr] = IN_TO_REG(val);
224         lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
225         return count;
226 }
227         
228 #define show_in_offset(offset)                                  \
229 static ssize_t                                                  \
230         show_in##offset (struct device *dev, char *buf)         \
231 {                                                               \
232         return show_in(dev, buf, 0x##offset);                   \
233 }                                                               \
234 static DEVICE_ATTR(in##offset##_input, S_IRUGO,                 \
235                 show_in##offset, NULL);                         \
236 static ssize_t                                                  \
237         show_in##offset##_min (struct device *dev, char *buf)   \
238 {                                                               \
239         return show_in_min(dev, buf, 0x##offset);               \
240 }                                                               \
241 static ssize_t                                                  \
242         show_in##offset##_max (struct device *dev, char *buf)   \
243 {                                                               \
244         return show_in_max(dev, buf, 0x##offset);               \
245 }                                                               \
246 static ssize_t set_in##offset##_min (struct device *dev,        \
247                 const char *buf, size_t count)                  \
248 {                                                               \
249         return set_in_min(dev, buf, count, 0x##offset);         \
250 }                                                               \
251 static ssize_t set_in##offset##_max (struct device *dev,        \
252                 const char *buf, size_t count)                  \
253 {                                                               \
254         return set_in_max(dev, buf, count, 0x##offset);         \
255 }                                                               \
256 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,         \
257                 show_in##offset##_min, set_in##offset##_min);   \
258 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,         \
259                 show_in##offset##_max, set_in##offset##_max);
260
261 show_in_offset(0);
262 show_in_offset(1);
263 show_in_offset(2);
264 show_in_offset(3);
265 show_in_offset(4);
266 show_in_offset(5);
267 show_in_offset(6);
268
269 /* Temperature */
270 static ssize_t show_temp(struct device *dev, char *buf)
271 {
272         struct lm78_data *data = lm78_update_device(dev);
273         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
274 }
275
276 static ssize_t show_temp_over(struct device *dev, char *buf)
277 {
278         struct lm78_data *data = lm78_update_device(dev);
279         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
280 }
281
282 static ssize_t set_temp_over(struct device *dev, const char *buf, size_t count)
283 {
284         struct i2c_client *client = to_i2c_client(dev);
285         struct lm78_data *data = i2c_get_clientdata(client);
286         long val = simple_strtol(buf, NULL, 10);
287         data->temp_over = TEMP_TO_REG(val);
288         lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
289         return count;
290 }
291
292 static ssize_t show_temp_hyst(struct device *dev, char *buf)
293 {
294         struct lm78_data *data = lm78_update_device(dev);
295         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
296 }
297
298 static ssize_t set_temp_hyst(struct device *dev, const char *buf, size_t count)
299 {
300         struct i2c_client *client = to_i2c_client(dev);
301         struct lm78_data *data = i2c_get_clientdata(client);
302         long val = simple_strtol(buf, NULL, 10);
303         data->temp_hyst = TEMP_TO_REG(val);
304         lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
305         return count;
306 }
307
308 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
309 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
310                 show_temp_over, set_temp_over);
311 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
312                 show_temp_hyst, set_temp_hyst);
313
314 /* 3 Fans */
315 static ssize_t show_fan(struct device *dev, char *buf, int nr)
316 {
317         struct lm78_data *data = lm78_update_device(dev);
318         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
319                 DIV_FROM_REG(data->fan_div[nr])) );
320 }
321
322 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
323 {
324         struct lm78_data *data = lm78_update_device(dev);
325         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
326                 DIV_FROM_REG(data->fan_div[nr])) );
327 }
328
329 static ssize_t set_fan_min(struct device *dev, const char *buf,
330                 size_t count, int nr)
331 {
332         struct i2c_client *client = to_i2c_client(dev);
333         struct lm78_data *data = i2c_get_clientdata(client);
334         unsigned long val = simple_strtoul(buf, NULL, 10);
335         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
336         lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
337         return count;
338 }
339
340 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
341 {
342         struct lm78_data *data = lm78_update_device(dev);
343         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
344 }
345
346 /* Note: we save and restore the fan minimum here, because its value is
347    determined in part by the fan divisor.  This follows the principle of
348    least suprise; the user doesn't expect the fan minimum to change just
349    because the divisor changed. */
350 static ssize_t set_fan_div(struct device *dev, const char *buf,
351         size_t count, int nr)
352 {
353         struct i2c_client *client = to_i2c_client(dev);
354         struct lm78_data *data = i2c_get_clientdata(client);
355         unsigned long min = FAN_FROM_REG(data->fan_min[nr],
356                         DIV_FROM_REG(data->fan_div[nr]));
357         unsigned long val = simple_strtoul(buf, NULL, 10);
358         int reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
359         data->fan_div[nr] = DIV_TO_REG(val);
360         switch (nr) {
361         case 0:
362                 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
363                 break;
364         case 1:
365                 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
366                 break;
367         }
368         lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
369         data->fan_min[nr] =
370                 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
371         lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
372         return count;
373 }
374
375 #define show_fan_offset(offset)                                         \
376 static ssize_t show_fan_##offset (struct device *dev, char *buf)        \
377 {                                                                       \
378         return show_fan(dev, buf, 0x##offset - 1);                      \
379 }                                                                       \
380 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf)  \
381 {                                                                       \
382         return show_fan_min(dev, buf, 0x##offset - 1);                  \
383 }                                                                       \
384 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf)  \
385 {                                                                       \
386         return show_fan_div(dev, buf, 0x##offset - 1);                  \
387 }                                                                       \
388 static ssize_t set_fan_##offset##_min (struct device *dev,              \
389                 const char *buf, size_t count)                          \
390 {                                                                       \
391         return set_fan_min(dev, buf, count, 0x##offset - 1);            \
392 }                                                                       \
393 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
394 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
395                 show_fan_##offset##_min, set_fan_##offset##_min);
396
397 static ssize_t set_fan_1_div(struct device *dev, const char *buf,
398                 size_t count)
399 {
400         return set_fan_div(dev, buf, count, 0) ;
401 }
402
403 static ssize_t set_fan_2_div(struct device *dev, const char *buf,
404                 size_t count)
405 {
406         return set_fan_div(dev, buf, count, 1) ;
407 }
408
409 show_fan_offset(1);
410 show_fan_offset(2);
411 show_fan_offset(3);
412
413 /* Fan 3 divisor is locked in H/W */
414 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
415                 show_fan_1_div, set_fan_1_div);
416 static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
417                 show_fan_2_div, set_fan_2_div);
418 static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
419
420 /* VID */
421 static ssize_t show_vid(struct device *dev, char *buf)
422 {
423         struct lm78_data *data = lm78_update_device(dev);
424         return sprintf(buf, "%d\n", VID_FROM_REG(data->vid));
425 }
426 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
427
428 /* Alarms */
429 static ssize_t show_alarms(struct device *dev, char *buf)
430 {
431         struct lm78_data *data = lm78_update_device(dev);
432         return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
433 }
434 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
435
436 /* This function is called when:
437      * lm78_driver is inserted (when this module is loaded), for each
438        available adapter
439      * when a new adapter is inserted (and lm78_driver is still present) */
440 static int lm78_attach_adapter(struct i2c_adapter *adapter)
441 {
442         if (!(adapter->class & I2C_CLASS_HWMON))
443                 return 0;
444         return i2c_detect(adapter, &addr_data, lm78_detect);
445 }
446
447 /* This function is called by i2c_detect */
448 int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
449 {
450         int i, err;
451         struct i2c_client *new_client;
452         struct lm78_data *data;
453         const char *client_name = "";
454         int is_isa = i2c_is_isa_adapter(adapter);
455
456         if (!is_isa &&
457             !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
458                 err = -ENODEV;
459                 goto ERROR0;
460         }
461
462         /* Reserve the ISA region */
463         if (is_isa)
464                 if (!request_region(address, LM78_EXTENT, "lm78")) {
465                         err = -EBUSY;
466                         goto ERROR0;
467                 }
468
469         /* Probe whether there is anything available on this address. Already
470            done for SMBus clients */
471         if (kind < 0) {
472                 if (is_isa) {
473
474 #define REALLY_SLOW_IO
475                         /* We need the timeouts for at least some LM78-like
476                            chips. But only if we read 'undefined' registers. */
477                         i = inb_p(address + 1);
478                         if (inb_p(address + 2) != i) {
479                                 err = -ENODEV;
480                                 goto ERROR1;
481                         }
482                         if (inb_p(address + 3) != i) {
483                                 err = -ENODEV;
484                                 goto ERROR1;
485                         }
486                         if (inb_p(address + 7) != i) {
487                                 err = -ENODEV;
488                                 goto ERROR1;
489                         }
490 #undef REALLY_SLOW_IO
491
492                         /* Let's just hope nothing breaks here */
493                         i = inb_p(address + 5) & 0x7f;
494                         outb_p(~i & 0x7f, address + 5);
495                         if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
496                                 outb_p(i, address + 5);
497                                 err = -ENODEV;
498                                 goto ERROR1;
499                         }
500                 }
501         }
502
503         /* OK. For now, we presume we have a valid client. We now create the
504            client structure, even though we cannot fill it completely yet.
505            But it allows us to access lm78_{read,write}_value. */
506
507         if (!(data = kmalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
508                 err = -ENOMEM;
509                 goto ERROR1;
510         }
511         memset(data, 0, sizeof(struct lm78_data));
512
513         new_client = &data->client;
514         if (is_isa)
515                 init_MUTEX(&data->lock);
516         i2c_set_clientdata(new_client, data);
517         new_client->addr = address;
518         new_client->adapter = adapter;
519         new_client->driver = &lm78_driver;
520         new_client->flags = 0;
521
522         /* Now, we do the remaining detection. */
523         if (kind < 0) {
524                 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
525                         err = -ENODEV;
526                         goto ERROR2;
527                 }
528                 if (!is_isa && (lm78_read_value(
529                                 new_client, LM78_REG_I2C_ADDR) != address)) {
530                         err = -ENODEV;
531                         goto ERROR2;
532                 }
533         }
534
535         /* Determine the chip type. */
536         if (kind <= 0) {
537                 i = lm78_read_value(new_client, LM78_REG_CHIPID);
538                 if (i == 0x00 || i == 0x20)
539                         kind = lm78;
540                 else if (i == 0x40)
541                         kind = lm78j;
542                 else if ((i & 0xfe) == 0xc0)
543                         kind = lm79;
544                 else {
545                         if (kind == 0)
546                                 dev_warn(&adapter->dev, "Ignoring 'force' "
547                                         "parameter for unknown chip at "
548                                         "adapter %d, address 0x%02x\n",
549                                         i2c_adapter_id(adapter), address);
550                         err = -ENODEV;
551                         goto ERROR2;
552                 }
553         }
554
555         if (kind == lm78) {
556                 client_name = "lm78";
557         } else if (kind == lm78j) {
558                 client_name = "lm78-j";
559         } else if (kind == lm79) {
560                 client_name = "lm79";
561         }
562
563         /* Fill in the remaining client fields and put into the global list */
564         strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
565         data->type = kind;
566
567         data->valid = 0;
568         init_MUTEX(&data->update_lock);
569
570         /* Tell the I2C layer a new client has arrived */
571         if ((err = i2c_attach_client(new_client)))
572                 goto ERROR2;
573
574         /* Initialize the LM78 chip */
575         lm78_init_client(new_client);
576
577         /* A few vars need to be filled upon startup */
578         for (i = 0; i < 3; i++) {
579                 data->fan_min[i] = lm78_read_value(new_client,
580                                         LM78_REG_FAN_MIN(i));
581         }
582
583         /* Register sysfs hooks */
584         device_create_file(&new_client->dev, &dev_attr_in0_input);
585         device_create_file(&new_client->dev, &dev_attr_in0_min);
586         device_create_file(&new_client->dev, &dev_attr_in0_max);
587         device_create_file(&new_client->dev, &dev_attr_in1_input);
588         device_create_file(&new_client->dev, &dev_attr_in1_min);
589         device_create_file(&new_client->dev, &dev_attr_in1_max);
590         device_create_file(&new_client->dev, &dev_attr_in2_input);
591         device_create_file(&new_client->dev, &dev_attr_in2_min);
592         device_create_file(&new_client->dev, &dev_attr_in2_max);
593         device_create_file(&new_client->dev, &dev_attr_in3_input);
594         device_create_file(&new_client->dev, &dev_attr_in3_min);
595         device_create_file(&new_client->dev, &dev_attr_in3_max);
596         device_create_file(&new_client->dev, &dev_attr_in4_input);
597         device_create_file(&new_client->dev, &dev_attr_in4_min);
598         device_create_file(&new_client->dev, &dev_attr_in4_max);
599         device_create_file(&new_client->dev, &dev_attr_in5_input);
600         device_create_file(&new_client->dev, &dev_attr_in5_min);
601         device_create_file(&new_client->dev, &dev_attr_in5_max);
602         device_create_file(&new_client->dev, &dev_attr_in6_input);
603         device_create_file(&new_client->dev, &dev_attr_in6_min);
604         device_create_file(&new_client->dev, &dev_attr_in6_max);
605         device_create_file(&new_client->dev, &dev_attr_temp1_input);
606         device_create_file(&new_client->dev, &dev_attr_temp1_max);
607         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
608         device_create_file(&new_client->dev, &dev_attr_fan1_input);
609         device_create_file(&new_client->dev, &dev_attr_fan1_min);
610         device_create_file(&new_client->dev, &dev_attr_fan1_div);
611         device_create_file(&new_client->dev, &dev_attr_fan2_input);
612         device_create_file(&new_client->dev, &dev_attr_fan2_min);
613         device_create_file(&new_client->dev, &dev_attr_fan2_div);
614         device_create_file(&new_client->dev, &dev_attr_fan3_input);
615         device_create_file(&new_client->dev, &dev_attr_fan3_min);
616         device_create_file(&new_client->dev, &dev_attr_fan3_div);
617         device_create_file(&new_client->dev, &dev_attr_alarms);
618         device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
619
620         return 0;
621
622 ERROR2:
623         kfree(data);
624 ERROR1:
625         if (is_isa)
626                 release_region(address, LM78_EXTENT);
627 ERROR0:
628         return err;
629 }
630
631 static int lm78_detach_client(struct i2c_client *client)
632 {
633         int err;
634
635         /* release ISA region first */
636         if(i2c_is_isa_client(client))
637                 release_region(client->addr, LM78_EXTENT);
638
639         /* now it's safe to scrap the rest */
640         if ((err = i2c_detach_client(client))) {
641                 dev_err(&client->dev,
642                     "Client deregistration failed, client not detached.\n");
643                 return err;
644         }
645
646         kfree(i2c_get_clientdata(client));
647
648         return 0;
649 }
650
651 /* The SMBus locks itself, but ISA access must be locked explicitely! 
652    We don't want to lock the whole ISA bus, so we lock each client
653    separately.
654    We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
655    would slow down the LM78 access and should not be necessary. 
656    There are some ugly typecasts here, but the good new is - they should
657    nowhere else be necessary! */
658 static int lm78_read_value(struct i2c_client *client, u8 reg)
659 {
660         int res;
661         if (i2c_is_isa_client(client)) {
662                 struct lm78_data *data = i2c_get_clientdata(client);
663                 down(&data->lock);
664                 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
665                 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
666                 up(&data->lock);
667                 return res;
668         } else
669                 return i2c_smbus_read_byte_data(client, reg);
670 }
671
672 /* The SMBus locks itself, but ISA access muse be locked explicitely! 
673    We don't want to lock the whole ISA bus, so we lock each client
674    separately.
675    We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
676    would slow down the LM78 access and should not be necessary. 
677    There are some ugly typecasts here, but the good new is - they should
678    nowhere else be necessary! */
679 static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
680 {
681         if (i2c_is_isa_client(client)) {
682                 struct lm78_data *data = i2c_get_clientdata(client);
683                 down(&data->lock);
684                 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
685                 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
686                 up(&data->lock);
687                 return 0;
688         } else
689                 return i2c_smbus_write_byte_data(client, reg, value);
690 }
691
692 /* Called when we have found a new LM78. It should set limits, etc. */
693 static void lm78_init_client(struct i2c_client *client)
694 {
695         struct lm78_data *data = i2c_get_clientdata(client);
696         int vid;
697
698         /* Reset all except Watchdog values and last conversion values
699            This sets fan-divs to 2, among others */
700         lm78_write_value(client, LM78_REG_CONFIG, 0x80);
701
702         vid = lm78_read_value(client, LM78_REG_VID_FANDIV) & 0x0f;
703         if (data->type == lm79)
704                 vid |=
705                     (lm78_read_value(client, LM78_REG_CHIPID) & 0x01) << 4;
706         else
707                 vid |= 0x10;
708         vid = VID_FROM_REG(vid);
709
710         /* Start monitoring */
711         lm78_write_value(client, LM78_REG_CONFIG,
712                          (lm78_read_value(client, LM78_REG_CONFIG) & 0xf7)
713                          | 0x01);
714
715 }
716
717 static struct lm78_data *lm78_update_device(struct device *dev)
718 {
719         struct i2c_client *client = to_i2c_client(dev);
720         struct lm78_data *data = i2c_get_clientdata(client);
721         int i;
722
723         down(&data->update_lock);
724
725         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
726             (jiffies < data->last_updated) || !data->valid) {
727
728                 dev_dbg(&client->dev, "Starting lm78 update\n");
729
730                 for (i = 0; i <= 6; i++) {
731                         data->in[i] =
732                             lm78_read_value(client, LM78_REG_IN(i));
733                         data->in_min[i] =
734                             lm78_read_value(client, LM78_REG_IN_MIN(i));
735                         data->in_max[i] =
736                             lm78_read_value(client, LM78_REG_IN_MAX(i));
737                 }
738                 for (i = 0; i < 3; i++) {
739                         data->fan[i] =
740                             lm78_read_value(client, LM78_REG_FAN(i));
741                         data->fan_min[i] =
742                             lm78_read_value(client, LM78_REG_FAN_MIN(i));
743                 }
744                 data->temp = lm78_read_value(client, LM78_REG_TEMP);
745                 data->temp_over =
746                     lm78_read_value(client, LM78_REG_TEMP_OVER);
747                 data->temp_hyst =
748                     lm78_read_value(client, LM78_REG_TEMP_HYST);
749                 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
750                 data->vid = i & 0x0f;
751                 if (data->type == lm79)
752                         data->vid |=
753                             (lm78_read_value(client, LM78_REG_CHIPID) &
754                              0x01) << 4;
755                 else
756                         data->vid |= 0x10;
757                 data->fan_div[0] = (i >> 4) & 0x03;
758                 data->fan_div[1] = i >> 6;
759                 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
760                     (lm78_read_value(client, LM78_REG_ALARM2) << 8);
761                 data->last_updated = jiffies;
762                 data->valid = 1;
763
764                 data->fan_div[2] = 1;
765         }
766
767         up(&data->update_lock);
768
769         return data;
770 }
771
772 static int __init sm_lm78_init(void)
773 {
774         return i2c_add_driver(&lm78_driver);
775 }
776
777 static void __exit sm_lm78_exit(void)
778 {
779         i2c_del_driver(&lm78_driver);
780 }
781
782
783
784 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
785 MODULE_DESCRIPTION("LM78, LM78-J and LM79 driver");
786 MODULE_LICENSE("GPL");
787
788 module_init(sm_lm78_init);
789 module_exit(sm_lm78_exit);