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