2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
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.
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.
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.
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>
29 /* Addresses to scan */
30 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
31 static unsigned short normal_i2c_range[] = { 0x20, 0x2f, I2C_CLIENT_END };
32 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
33 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
35 /* Insmod parameters */
36 SENSORS_INSMOD_3(lm78, lm78j, lm79);
38 /* Many LM78 constants specified below */
40 /* Length of ISA address segment */
43 /* Where are the ISA address/data registers relative to the base address */
44 #define LM78_ADDR_REG_OFFSET 5
45 #define LM78_DATA_REG_OFFSET 6
47 /* The LM78 registers */
48 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
49 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
50 #define LM78_REG_IN(nr) (0x20 + (nr))
52 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
53 #define LM78_REG_FAN(nr) (0x28 + (nr))
55 #define LM78_REG_TEMP 0x27
56 #define LM78_REG_TEMP_OVER 0x39
57 #define LM78_REG_TEMP_HYST 0x3a
59 #define LM78_REG_ALARM1 0x41
60 #define LM78_REG_ALARM2 0x42
62 #define LM78_REG_VID_FANDIV 0x47
64 #define LM78_REG_CONFIG 0x40
65 #define LM78_REG_CHIPID 0x49
66 #define LM78_REG_I2C_ADDR 0x48
69 /* Conversions. Rounding and limit checking is only done on the TO_REG
72 /* IN: mV, (0V to 4.08V)
74 static inline u8 IN_TO_REG(unsigned long val)
76 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
77 return (nval + 8) / 16;
79 #define IN_FROM_REG(val) ((val) * 16)
81 static inline u8 FAN_TO_REG(long rpm, int div)
85 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
86 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
89 static inline int FAN_FROM_REG(u8 val, int div)
91 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
94 /* TEMP: mC (-128C to +127C)
95 REG: 1C/bit, two's complement */
96 static inline u8 TEMP_TO_REG(int val)
98 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
99 return nval<0 ? (nval-500)/1000+0x100 : (nval+500)/1000;
102 static inline int TEMP_FROM_REG(u8 val)
104 return (val>=0x80 ? val-0x100 : val) * 1000;
108 REG: (see doc/vid) */
109 static inline int VID_FROM_REG(u8 val)
111 return val==0x1f ? 0 : val>=0x10 ? 5100-val*100 : 2050-val*50;
114 /* ALARMS: chip-specific bitmask
116 #define ALARMS_FROM_REG(val) (val)
118 /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
119 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
120 static inline u8 DIV_TO_REG(int val)
122 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
124 #define DIV_FROM_REG(val) (1 << (val))
126 /* Initial limits. To keep them sane, we use the 'standard' translation as
127 specified in the LM78 sheet. Use the config file to set better limits. */
128 #define LM78_INIT_IN_0(vid) ((vid)==3500 ? 2800 : (vid))
129 #define LM78_INIT_IN_1(vid) ((vid)==3500 ? 2800 : (vid))
130 #define LM78_INIT_IN_2 3300
131 #define LM78_INIT_IN_3 (((5000) * 100)/168)
132 #define LM78_INIT_IN_4 (((12000) * 10)/38)
133 #define LM78_INIT_IN_5 (((-12000) * -604)/2100)
134 #define LM78_INIT_IN_6 (((-5000) * -604)/909)
136 #define LM78_INIT_IN_PERCENTAGE 10
138 #define LM78_INIT_IN_MIN_0(vid) (LM78_INIT_IN_0(vid) - \
139 LM78_INIT_IN_0(vid) * LM78_INIT_IN_PERCENTAGE / 100)
140 #define LM78_INIT_IN_MAX_0(vid) (LM78_INIT_IN_0(vid) + \
141 LM78_INIT_IN_0(vid) * LM78_INIT_IN_PERCENTAGE / 100)
142 #define LM78_INIT_IN_MIN_1(vid) (LM78_INIT_IN_1(vid) - \
143 LM78_INIT_IN_1(vid) * LM78_INIT_IN_PERCENTAGE / 100)
144 #define LM78_INIT_IN_MAX_1(vid) (LM78_INIT_IN_1(vid) + \
145 LM78_INIT_IN_1(vid) * LM78_INIT_IN_PERCENTAGE / 100)
147 #define LM78_INIT_IN_MIN_2 \
148 (LM78_INIT_IN_2 - LM78_INIT_IN_2 * LM78_INIT_IN_PERCENTAGE / 100)
149 #define LM78_INIT_IN_MAX_2 \
150 (LM78_INIT_IN_2 + LM78_INIT_IN_2 * LM78_INIT_IN_PERCENTAGE / 100)
151 #define LM78_INIT_IN_MIN_3 \
152 (LM78_INIT_IN_3 - LM78_INIT_IN_3 * LM78_INIT_IN_PERCENTAGE / 100)
153 #define LM78_INIT_IN_MAX_3 \
154 (LM78_INIT_IN_3 + LM78_INIT_IN_3 * LM78_INIT_IN_PERCENTAGE / 100)
155 #define LM78_INIT_IN_MIN_4 \
156 (LM78_INIT_IN_4 - LM78_INIT_IN_4 * LM78_INIT_IN_PERCENTAGE / 100)
157 #define LM78_INIT_IN_MAX_4 \
158 (LM78_INIT_IN_4 + LM78_INIT_IN_4 * LM78_INIT_IN_PERCENTAGE / 100)
159 #define LM78_INIT_IN_MIN_5 \
160 (LM78_INIT_IN_5 - LM78_INIT_IN_5 * LM78_INIT_IN_PERCENTAGE / 100)
161 #define LM78_INIT_IN_MAX_5 \
162 (LM78_INIT_IN_5 + LM78_INIT_IN_5 * LM78_INIT_IN_PERCENTAGE / 100)
163 #define LM78_INIT_IN_MIN_6 \
164 (LM78_INIT_IN_6 - LM78_INIT_IN_6 * LM78_INIT_IN_PERCENTAGE / 100)
165 #define LM78_INIT_IN_MAX_6 \
166 (LM78_INIT_IN_6 + LM78_INIT_IN_6 * LM78_INIT_IN_PERCENTAGE / 100)
168 #define LM78_INIT_FAN_MIN_1 3000
169 #define LM78_INIT_FAN_MIN_2 3000
170 #define LM78_INIT_FAN_MIN_3 3000
172 #define LM78_INIT_TEMP_OVER 60000
173 #define LM78_INIT_TEMP_HYST 50000
175 /* There are some complications in a module like this. First off, LM78 chips
176 may be both present on the SMBus and the ISA bus, and we have to handle
177 those cases separately at some places. Second, there might be several
178 LM78 chips available (well, actually, that is probably never done; but
179 it is a clean illustration of how to handle a case like that). Finally,
180 a specific chip may be attached to *both* ISA and SMBus, and we would
181 not like to detect it double. Fortunately, in the case of the LM78 at
182 least, a register tells us what SMBus address we are on, so that helps
183 a bit - except if there could be more than one SMBus. Groan. No solution
186 /* This module may seem overly long and complicated. In fact, it is not so
187 bad. Quite a lot of bookkeeping is done. A real driver can often cut
190 /* For each registered LM78, we need to keep some data in memory. That
191 data is pointed to by lm78_list[NR]->data. The structure itself is
192 dynamically allocated, at the same time when a new lm78 client is
195 struct i2c_client client;
196 struct semaphore lock;
199 struct semaphore update_lock;
200 char valid; /* !=0 if following fields are valid */
201 unsigned long last_updated; /* In jiffies */
203 u8 in[7]; /* Register value */
204 u8 in_max[7]; /* Register value */
205 u8 in_min[7]; /* Register value */
206 u8 fan[3]; /* Register value */
207 u8 fan_min[3]; /* Register value */
208 u8 temp; /* Register value */
209 u8 temp_over; /* Register value */
210 u8 temp_hyst; /* Register value */
211 u8 fan_div[3]; /* Register encoding, shifted right */
212 u8 vid; /* Register encoding, combined */
213 u16 alarms; /* Register encoding, combined */
217 static int lm78_attach_adapter(struct i2c_adapter *adapter);
218 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
219 static int lm78_detach_client(struct i2c_client *client);
221 static int lm78_read_value(struct i2c_client *client, u8 register);
222 static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
223 static struct lm78_data *lm78_update_device(struct device *dev);
224 static void lm78_init_client(struct i2c_client *client);
227 static struct i2c_driver lm78_driver = {
228 .owner = THIS_MODULE,
230 .id = I2C_DRIVERID_LM78,
231 .flags = I2C_DF_NOTIFY,
232 .attach_adapter = lm78_attach_adapter,
233 .detach_client = lm78_detach_client,
237 static ssize_t show_in(struct device *dev, char *buf, int nr)
239 struct lm78_data *data = lm78_update_device(dev);
240 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
243 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
245 struct lm78_data *data = lm78_update_device(dev);
246 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
249 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
251 struct lm78_data *data = lm78_update_device(dev);
252 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
255 static ssize_t set_in_min(struct device *dev, const char *buf,
256 size_t count, int nr)
258 struct i2c_client *client = to_i2c_client(dev);
259 struct lm78_data *data = i2c_get_clientdata(client);
260 unsigned long val = simple_strtoul(buf, NULL, 10);
261 data->in_min[nr] = IN_TO_REG(val);
262 lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
266 static ssize_t set_in_max(struct device *dev, const char *buf,
267 size_t count, int nr)
269 struct i2c_client *client = to_i2c_client(dev);
270 struct lm78_data *data = i2c_get_clientdata(client);
271 unsigned long val = simple_strtoul(buf, NULL, 10);
272 data->in_max[nr] = IN_TO_REG(val);
273 lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
277 #define show_in_offset(offset) \
279 show_in##offset (struct device *dev, char *buf) \
281 return show_in(dev, buf, 0x##offset); \
283 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
284 show_in##offset, NULL) \
286 show_in##offset##_min (struct device *dev, char *buf) \
288 return show_in_min(dev, buf, 0x##offset); \
291 show_in##offset##_max (struct device *dev, char *buf) \
293 return show_in_max(dev, buf, 0x##offset); \
295 static ssize_t set_in##offset##_min (struct device *dev, \
296 const char *buf, size_t count) \
298 return set_in_min(dev, buf, count, 0x##offset); \
300 static ssize_t set_in##offset##_max (struct device *dev, \
301 const char *buf, size_t count) \
303 return set_in_max(dev, buf, count, 0x##offset); \
305 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
306 show_in##offset##_min, set_in##offset##_min) \
307 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
308 show_in##offset##_max, set_in##offset##_max)
319 static ssize_t show_temp(struct device *dev, char *buf)
321 struct lm78_data *data = lm78_update_device(dev);
322 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
325 static ssize_t show_temp_over(struct device *dev, char *buf)
327 struct lm78_data *data = lm78_update_device(dev);
328 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
331 static ssize_t set_temp_over(struct device *dev, const char *buf, size_t count)
333 struct i2c_client *client = to_i2c_client(dev);
334 struct lm78_data *data = i2c_get_clientdata(client);
335 long val = simple_strtol(buf, NULL, 10);
336 data->temp_over = TEMP_TO_REG(val);
337 lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
341 static ssize_t show_temp_hyst(struct device *dev, char *buf)
343 struct lm78_data *data = lm78_update_device(dev);
344 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
347 static ssize_t set_temp_hyst(struct device *dev, const char *buf, size_t count)
349 struct i2c_client *client = to_i2c_client(dev);
350 struct lm78_data *data = i2c_get_clientdata(client);
351 long val = simple_strtol(buf, NULL, 10);
352 data->temp_hyst = TEMP_TO_REG(val);
353 lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
357 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL)
358 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
359 show_temp_over, set_temp_over)
360 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
361 show_temp_hyst, set_temp_hyst)
364 static ssize_t show_fan(struct device *dev, char *buf, int nr)
366 struct lm78_data *data = lm78_update_device(dev);
367 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
368 DIV_FROM_REG(data->fan_div[nr])) );
371 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
373 struct lm78_data *data = lm78_update_device(dev);
374 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
375 DIV_FROM_REG(data->fan_div[nr])) );
378 static ssize_t set_fan_min(struct device *dev, const char *buf,
379 size_t count, int nr)
381 struct i2c_client *client = to_i2c_client(dev);
382 struct lm78_data *data = i2c_get_clientdata(client);
383 unsigned long val = simple_strtoul(buf, NULL, 10);
384 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
385 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
389 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
391 struct lm78_data *data = lm78_update_device(dev);
392 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
395 /* Note: we save and restore the fan minimum here, because its value is
396 determined in part by the fan divisor. This follows the principle of
397 least suprise; the user doesn't expect the fan minimum to change just
398 because the divisor changed. */
399 static ssize_t set_fan_div(struct device *dev, const char *buf,
400 size_t count, int nr)
402 struct i2c_client *client = to_i2c_client(dev);
403 struct lm78_data *data = i2c_get_clientdata(client);
404 unsigned long min = FAN_FROM_REG(data->fan_min[nr],
405 DIV_FROM_REG(data->fan_div[nr]));
406 unsigned long val = simple_strtoul(buf, NULL, 10);
407 int reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
408 data->fan_div[nr] = DIV_TO_REG(val);
411 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
414 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
417 lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
419 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
420 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
424 #define show_fan_offset(offset) \
425 static ssize_t show_fan_##offset (struct device *dev, char *buf) \
427 return show_fan(dev, buf, 0x##offset - 1); \
429 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
431 return show_fan_min(dev, buf, 0x##offset - 1); \
433 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
435 return show_fan_div(dev, buf, 0x##offset - 1); \
437 static ssize_t set_fan_##offset##_min (struct device *dev, \
438 const char *buf, size_t count) \
440 return set_fan_min(dev, buf, count, 0x##offset - 1); \
442 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL) \
443 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
444 show_fan_##offset##_min, set_fan_##offset##_min)
446 static ssize_t set_fan_1_div(struct device *dev, const char *buf,
449 return set_fan_div(dev, buf, count, 0) ;
452 static ssize_t set_fan_2_div(struct device *dev, const char *buf,
455 return set_fan_div(dev, buf, count, 1) ;
462 /* Fan 3 divisor is locked in H/W */
463 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
464 show_fan_1_div, set_fan_1_div)
465 static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
466 show_fan_2_div, set_fan_2_div)
467 static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL)
470 static ssize_t show_vid(struct device *dev, char *buf)
472 struct lm78_data *data = lm78_update_device(dev);
473 return sprintf(buf, "%d\n", VID_FROM_REG(data->vid));
475 static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid, NULL);
478 static ssize_t show_alarms(struct device *dev, char *buf)
480 struct lm78_data *data = lm78_update_device(dev);
481 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
483 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
485 /* This function is called when:
486 * lm78_driver is inserted (when this module is loaded), for each
488 * when a new adapter is inserted (and lm78_driver is still present) */
489 static int lm78_attach_adapter(struct i2c_adapter *adapter)
491 if (!(adapter->class & I2C_ADAP_CLASS_SMBUS))
493 return i2c_detect(adapter, &addr_data, lm78_detect);
496 /* This function is called by i2c_detect */
497 int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
500 struct i2c_client *new_client;
501 struct lm78_data *data;
502 const char *client_name = "";
503 int is_isa = i2c_is_isa_adapter(adapter);
506 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
511 /* Reserve the ISA region */
513 if (!request_region(address, LM78_EXTENT, "lm78")) {
518 /* Probe whether there is anything available on this address. Already
519 done for SMBus clients */
523 #define REALLY_SLOW_IO
524 /* We need the timeouts for at least some LM78-like
525 chips. But only if we read 'undefined' registers. */
526 i = inb_p(address + 1);
527 if (inb_p(address + 2) != i) {
531 if (inb_p(address + 3) != i) {
535 if (inb_p(address + 7) != i) {
539 #undef REALLY_SLOW_IO
541 /* Let's just hope nothing breaks here */
542 i = inb_p(address + 5) & 0x7f;
543 outb_p(~i & 0x7f, address + 5);
544 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
545 outb_p(i, address + 5);
552 /* OK. For now, we presume we have a valid client. We now create the
553 client structure, even though we cannot fill it completely yet.
554 But it allows us to access lm78_{read,write}_value. */
556 if (!(data = kmalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
560 memset(data, 0, sizeof(struct lm78_data));
562 new_client = &data->client;
564 init_MUTEX(&data->lock);
565 i2c_set_clientdata(new_client, data);
566 new_client->addr = address;
567 new_client->adapter = adapter;
568 new_client->driver = &lm78_driver;
569 new_client->flags = 0;
571 /* Now, we do the remaining detection. */
573 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
577 if (!is_isa && (lm78_read_value(
578 new_client, LM78_REG_I2C_ADDR) != address)) {
584 /* Determine the chip type. */
586 i = lm78_read_value(new_client, LM78_REG_CHIPID);
587 if (i == 0x00 || i == 0x20)
591 else if ((i & 0xfe) == 0xc0)
595 dev_warn(&adapter->dev, "Ignoring 'force' "
596 "parameter for unknown chip at "
597 "adapter %d, address 0x%02x\n",
598 i2c_adapter_id(adapter), address);
605 client_name = "lm78";
606 } else if (kind == lm78j) {
607 client_name = "lm78-j";
608 } else if (kind == lm79) {
609 client_name = "lm79";
612 /* Fill in the remaining client fields and put into the global list */
613 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
617 init_MUTEX(&data->update_lock);
619 /* Tell the I2C layer a new client has arrived */
620 if ((err = i2c_attach_client(new_client)))
623 /* Initialize the LM78 chip */
624 lm78_init_client(new_client);
626 /* A few vars need to be filled upon startup */
627 for (i = 0; i < 3; i++) {
628 data->fan_min[i] = lm78_read_value(new_client,
629 LM78_REG_FAN_MIN(i));
632 /* Register sysfs hooks */
633 device_create_file(&new_client->dev, &dev_attr_in0_input);
634 device_create_file(&new_client->dev, &dev_attr_in0_min);
635 device_create_file(&new_client->dev, &dev_attr_in0_max);
636 device_create_file(&new_client->dev, &dev_attr_in1_input);
637 device_create_file(&new_client->dev, &dev_attr_in1_min);
638 device_create_file(&new_client->dev, &dev_attr_in1_max);
639 device_create_file(&new_client->dev, &dev_attr_in2_input);
640 device_create_file(&new_client->dev, &dev_attr_in2_min);
641 device_create_file(&new_client->dev, &dev_attr_in2_max);
642 device_create_file(&new_client->dev, &dev_attr_in3_input);
643 device_create_file(&new_client->dev, &dev_attr_in3_min);
644 device_create_file(&new_client->dev, &dev_attr_in3_max);
645 device_create_file(&new_client->dev, &dev_attr_in4_input);
646 device_create_file(&new_client->dev, &dev_attr_in4_min);
647 device_create_file(&new_client->dev, &dev_attr_in4_max);
648 device_create_file(&new_client->dev, &dev_attr_in5_input);
649 device_create_file(&new_client->dev, &dev_attr_in5_min);
650 device_create_file(&new_client->dev, &dev_attr_in5_max);
651 device_create_file(&new_client->dev, &dev_attr_in6_input);
652 device_create_file(&new_client->dev, &dev_attr_in6_min);
653 device_create_file(&new_client->dev, &dev_attr_in6_max);
654 device_create_file(&new_client->dev, &dev_attr_temp1_input);
655 device_create_file(&new_client->dev, &dev_attr_temp1_max);
656 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
657 device_create_file(&new_client->dev, &dev_attr_fan1_input);
658 device_create_file(&new_client->dev, &dev_attr_fan1_min);
659 device_create_file(&new_client->dev, &dev_attr_fan1_div);
660 device_create_file(&new_client->dev, &dev_attr_fan2_input);
661 device_create_file(&new_client->dev, &dev_attr_fan2_min);
662 device_create_file(&new_client->dev, &dev_attr_fan2_div);
663 device_create_file(&new_client->dev, &dev_attr_fan3_input);
664 device_create_file(&new_client->dev, &dev_attr_fan3_min);
665 device_create_file(&new_client->dev, &dev_attr_fan3_div);
666 device_create_file(&new_client->dev, &dev_attr_alarms);
667 device_create_file(&new_client->dev, &dev_attr_in0_ref);
675 release_region(address, LM78_EXTENT);
680 static int lm78_detach_client(struct i2c_client *client)
684 /* release ISA region first */
685 if(i2c_is_isa_client(client))
686 release_region(client->addr, LM78_EXTENT);
688 /* now it's safe to scrap the rest */
689 if ((err = i2c_detach_client(client))) {
690 dev_err(&client->dev,
691 "Client deregistration failed, client not detached.\n");
695 kfree(i2c_get_clientdata(client));
700 /* The SMBus locks itself, but ISA access must be locked explicitely!
701 We don't want to lock the whole ISA bus, so we lock each client
703 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
704 would slow down the LM78 access and should not be necessary.
705 There are some ugly typecasts here, but the good new is - they should
706 nowhere else be necessary! */
707 static int lm78_read_value(struct i2c_client *client, u8 reg)
710 if (i2c_is_isa_client(client)) {
711 struct lm78_data *data = i2c_get_clientdata(client);
713 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
714 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
718 return i2c_smbus_read_byte_data(client, reg);
721 /* The SMBus locks itself, but ISA access muse be locked explicitely!
722 We don't want to lock the whole ISA bus, so we lock each client
724 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
725 would slow down the LM78 access and should not be necessary.
726 There are some ugly typecasts here, but the good new is - they should
727 nowhere else be necessary! */
728 static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
730 if (i2c_is_isa_client(client)) {
731 struct lm78_data *data = i2c_get_clientdata(client);
733 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
734 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
738 return i2c_smbus_write_byte_data(client, reg, value);
741 /* Called when we have found a new LM78. It should set limits, etc. */
742 static void lm78_init_client(struct i2c_client *client)
744 struct lm78_data *data = i2c_get_clientdata(client);
747 /* Reset all except Watchdog values and last conversion values
748 This sets fan-divs to 2, among others */
749 lm78_write_value(client, LM78_REG_CONFIG, 0x80);
751 vid = lm78_read_value(client, LM78_REG_VID_FANDIV) & 0x0f;
752 if (data->type == lm79)
754 (lm78_read_value(client, LM78_REG_CHIPID) & 0x01) << 4;
757 vid = VID_FROM_REG(vid);
759 lm78_write_value(client, LM78_REG_IN_MIN(0),
760 IN_TO_REG(LM78_INIT_IN_MIN_0(vid)));
761 lm78_write_value(client, LM78_REG_IN_MAX(0),
762 IN_TO_REG(LM78_INIT_IN_MAX_0(vid)));
763 lm78_write_value(client, LM78_REG_IN_MIN(1),
764 IN_TO_REG(LM78_INIT_IN_MIN_1(vid)));
765 lm78_write_value(client, LM78_REG_IN_MAX(1),
766 IN_TO_REG(LM78_INIT_IN_MAX_1(vid)));
767 lm78_write_value(client, LM78_REG_IN_MIN(2),
768 IN_TO_REG(LM78_INIT_IN_MIN_2));
769 lm78_write_value(client, LM78_REG_IN_MAX(2),
770 IN_TO_REG(LM78_INIT_IN_MAX_2));
771 lm78_write_value(client, LM78_REG_IN_MIN(3),
772 IN_TO_REG(LM78_INIT_IN_MIN_3));
773 lm78_write_value(client, LM78_REG_IN_MAX(3),
774 IN_TO_REG(LM78_INIT_IN_MAX_3));
775 lm78_write_value(client, LM78_REG_IN_MIN(4),
776 IN_TO_REG(LM78_INIT_IN_MIN_4));
777 lm78_write_value(client, LM78_REG_IN_MAX(4),
778 IN_TO_REG(LM78_INIT_IN_MAX_4));
779 lm78_write_value(client, LM78_REG_IN_MIN(5),
780 IN_TO_REG(LM78_INIT_IN_MIN_5));
781 lm78_write_value(client, LM78_REG_IN_MAX(5),
782 IN_TO_REG(LM78_INIT_IN_MAX_5));
783 lm78_write_value(client, LM78_REG_IN_MIN(6),
784 IN_TO_REG(LM78_INIT_IN_MIN_6));
785 lm78_write_value(client, LM78_REG_IN_MAX(6),
786 IN_TO_REG(LM78_INIT_IN_MAX_6));
787 lm78_write_value(client, LM78_REG_FAN_MIN(0),
788 FAN_TO_REG(LM78_INIT_FAN_MIN_1, 2));
789 lm78_write_value(client, LM78_REG_FAN_MIN(1),
790 FAN_TO_REG(LM78_INIT_FAN_MIN_2, 2));
791 lm78_write_value(client, LM78_REG_FAN_MIN(2),
792 FAN_TO_REG(LM78_INIT_FAN_MIN_3, 2));
793 lm78_write_value(client, LM78_REG_TEMP_OVER,
794 TEMP_TO_REG(LM78_INIT_TEMP_OVER));
795 lm78_write_value(client, LM78_REG_TEMP_HYST,
796 TEMP_TO_REG(LM78_INIT_TEMP_HYST));
798 /* Start monitoring */
799 lm78_write_value(client, LM78_REG_CONFIG,
800 (lm78_read_value(client, LM78_REG_CONFIG) & 0xf7)
805 static struct lm78_data *lm78_update_device(struct device *dev)
807 struct i2c_client *client = to_i2c_client(dev);
808 struct lm78_data *data = i2c_get_clientdata(client);
811 down(&data->update_lock);
813 if ((jiffies - data->last_updated > HZ + HZ / 2) ||
814 (jiffies < data->last_updated) || !data->valid) {
816 dev_dbg(&client->dev, "Starting lm78 update\n");
818 for (i = 0; i <= 6; i++) {
820 lm78_read_value(client, LM78_REG_IN(i));
822 lm78_read_value(client, LM78_REG_IN_MIN(i));
824 lm78_read_value(client, LM78_REG_IN_MAX(i));
826 for (i = 0; i < 3; i++) {
828 lm78_read_value(client, LM78_REG_FAN(i));
830 lm78_read_value(client, LM78_REG_FAN_MIN(i));
832 data->temp = lm78_read_value(client, LM78_REG_TEMP);
834 lm78_read_value(client, LM78_REG_TEMP_OVER);
836 lm78_read_value(client, LM78_REG_TEMP_HYST);
837 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
838 data->vid = i & 0x0f;
839 if (data->type == lm79)
841 (lm78_read_value(client, LM78_REG_CHIPID) &
845 data->fan_div[0] = (i >> 4) & 0x03;
846 data->fan_div[1] = i >> 6;
847 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
848 (lm78_read_value(client, LM78_REG_ALARM2) << 8);
849 data->last_updated = jiffies;
852 data->fan_div[2] = 1;
855 up(&data->update_lock);
860 static int __init sm_lm78_init(void)
862 return i2c_add_driver(&lm78_driver);
865 static void __exit sm_lm78_exit(void)
867 i2c_del_driver(&lm78_driver);
872 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
873 MODULE_DESCRIPTION("LM78, LM78-J and LM79 driver");
874 MODULE_LICENSE("GPL");
876 module_init(sm_lm78_init);
877 module_exit(sm_lm78_exit);