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