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