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