]>
Commit | Line | Data |
---|---|---|
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 */ | |
33 | static 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 | 37 | static unsigned short isa_address = 0x290; |
1da177e4 LT |
38 | |
39 | /* Insmod parameters */ | |
f4b50261 | 40 | I2C_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 */ | |
78 | static 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 | ||
85 | static 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 | ||
92 | static 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 */ | |
99 | static 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 | ||
105 | static 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. */ | |
131 | struct 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 | ||
155 | static int lm78_attach_adapter(struct i2c_adapter *adapter); | |
2d8672c5 | 156 | static int lm78_isa_attach_adapter(struct i2c_adapter *adapter); |
1da177e4 LT |
157 | static int lm78_detect(struct i2c_adapter *adapter, int address, int kind); |
158 | static int lm78_detach_client(struct i2c_client *client); | |
159 | ||
160 | static int lm78_read_value(struct i2c_client *client, u8 register); | |
161 | static int lm78_write_value(struct i2c_client *client, u8 register, u8 value); | |
162 | static struct lm78_data *lm78_update_device(struct device *dev); | |
163 | static void lm78_init_client(struct i2c_client *client); | |
164 | ||
165 | ||
166 | static struct i2c_driver lm78_driver = { | |
167 | .owner = THIS_MODULE, | |
168 | .name = "lm78", | |
169 | .id = I2C_DRIVERID_LM78, | |
170 | .flags = I2C_DF_NOTIFY, | |
171 | .attach_adapter = lm78_attach_adapter, | |
172 | .detach_client = lm78_detach_client, | |
173 | }; | |
174 | ||
fde09509 JD |
175 | static struct i2c_driver lm78_isa_driver = { |
176 | .owner = THIS_MODULE, | |
177 | .name = "lm78-isa", | |
2d8672c5 | 178 | .attach_adapter = lm78_isa_attach_adapter, |
fde09509 JD |
179 | .detach_client = lm78_detach_client, |
180 | }; | |
181 | ||
182 | ||
1da177e4 LT |
183 | /* 7 Voltages */ |
184 | static ssize_t show_in(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[nr])); | |
188 | } | |
189 | ||
190 | static ssize_t show_in_min(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_min[nr])); | |
194 | } | |
195 | ||
196 | static ssize_t show_in_max(struct device *dev, char *buf, int nr) | |
197 | { | |
198 | struct lm78_data *data = lm78_update_device(dev); | |
199 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr])); | |
200 | } | |
201 | ||
202 | static ssize_t set_in_min(struct device *dev, const char *buf, | |
203 | size_t count, int nr) | |
204 | { | |
205 | struct i2c_client *client = to_i2c_client(dev); | |
206 | struct lm78_data *data = i2c_get_clientdata(client); | |
207 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
208 | ||
209 | down(&data->update_lock); | |
210 | data->in_min[nr] = IN_TO_REG(val); | |
211 | lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]); | |
212 | up(&data->update_lock); | |
213 | return count; | |
214 | } | |
215 | ||
216 | static ssize_t set_in_max(struct device *dev, const char *buf, | |
217 | size_t count, int nr) | |
218 | { | |
219 | struct i2c_client *client = to_i2c_client(dev); | |
220 | struct lm78_data *data = i2c_get_clientdata(client); | |
221 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
222 | ||
223 | down(&data->update_lock); | |
224 | data->in_max[nr] = IN_TO_REG(val); | |
225 | lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]); | |
226 | up(&data->update_lock); | |
227 | return count; | |
228 | } | |
229 | ||
230 | #define show_in_offset(offset) \ | |
231 | static ssize_t \ | |
8627f9ba | 232 | show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
233 | { \ |
234 | return show_in(dev, buf, offset); \ | |
235 | } \ | |
236 | static DEVICE_ATTR(in##offset##_input, S_IRUGO, \ | |
237 | show_in##offset, NULL); \ | |
238 | static ssize_t \ | |
8627f9ba | 239 | show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
240 | { \ |
241 | return show_in_min(dev, buf, offset); \ | |
242 | } \ | |
243 | static ssize_t \ | |
8627f9ba | 244 | show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
245 | { \ |
246 | return show_in_max(dev, buf, offset); \ | |
247 | } \ | |
8627f9ba | 248 | static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \ |
1da177e4 LT |
249 | const char *buf, size_t count) \ |
250 | { \ | |
251 | return set_in_min(dev, buf, count, offset); \ | |
252 | } \ | |
8627f9ba | 253 | static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \ |
1da177e4 LT |
254 | const char *buf, size_t count) \ |
255 | { \ | |
256 | return set_in_max(dev, buf, count, offset); \ | |
257 | } \ | |
258 | static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ | |
259 | show_in##offset##_min, set_in##offset##_min); \ | |
260 | static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
261 | show_in##offset##_max, set_in##offset##_max); | |
262 | ||
263 | show_in_offset(0); | |
264 | show_in_offset(1); | |
265 | show_in_offset(2); | |
266 | show_in_offset(3); | |
267 | show_in_offset(4); | |
268 | show_in_offset(5); | |
269 | show_in_offset(6); | |
270 | ||
271 | /* Temperature */ | |
8627f9ba | 272 | static ssize_t show_temp(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)); | |
276 | } | |
277 | ||
8627f9ba | 278 | static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
279 | { |
280 | struct lm78_data *data = lm78_update_device(dev); | |
281 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over)); | |
282 | } | |
283 | ||
8627f9ba | 284 | static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
285 | { |
286 | struct i2c_client *client = to_i2c_client(dev); | |
287 | struct lm78_data *data = i2c_get_clientdata(client); | |
288 | long val = simple_strtol(buf, NULL, 10); | |
289 | ||
290 | down(&data->update_lock); | |
291 | data->temp_over = TEMP_TO_REG(val); | |
292 | lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over); | |
293 | up(&data->update_lock); | |
294 | return count; | |
295 | } | |
296 | ||
8627f9ba | 297 | static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
298 | { |
299 | struct lm78_data *data = lm78_update_device(dev); | |
300 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst)); | |
301 | } | |
302 | ||
8627f9ba | 303 | static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
304 | { |
305 | struct i2c_client *client = to_i2c_client(dev); | |
306 | struct lm78_data *data = i2c_get_clientdata(client); | |
307 | long val = simple_strtol(buf, NULL, 10); | |
308 | ||
309 | down(&data->update_lock); | |
310 | data->temp_hyst = TEMP_TO_REG(val); | |
311 | lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst); | |
312 | up(&data->update_lock); | |
313 | return count; | |
314 | } | |
315 | ||
316 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); | |
317 | static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, | |
318 | show_temp_over, set_temp_over); | |
319 | static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, | |
320 | show_temp_hyst, set_temp_hyst); | |
321 | ||
322 | /* 3 Fans */ | |
323 | static ssize_t show_fan(struct device *dev, char *buf, int nr) | |
324 | { | |
325 | struct lm78_data *data = lm78_update_device(dev); | |
326 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], | |
327 | DIV_FROM_REG(data->fan_div[nr])) ); | |
328 | } | |
329 | ||
330 | static ssize_t show_fan_min(struct device *dev, char *buf, int nr) | |
331 | { | |
332 | struct lm78_data *data = lm78_update_device(dev); | |
333 | return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr], | |
334 | DIV_FROM_REG(data->fan_div[nr])) ); | |
335 | } | |
336 | ||
337 | static ssize_t set_fan_min(struct device *dev, const char *buf, | |
338 | size_t count, int nr) | |
339 | { | |
340 | struct i2c_client *client = to_i2c_client(dev); | |
341 | struct lm78_data *data = i2c_get_clientdata(client); | |
342 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
343 | ||
344 | down(&data->update_lock); | |
345 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); | |
346 | lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); | |
347 | up(&data->update_lock); | |
348 | return count; | |
349 | } | |
350 | ||
351 | static ssize_t show_fan_div(struct device *dev, char *buf, int nr) | |
352 | { | |
353 | struct lm78_data *data = lm78_update_device(dev); | |
354 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) ); | |
355 | } | |
356 | ||
357 | /* Note: we save and restore the fan minimum here, because its value is | |
358 | determined in part by the fan divisor. This follows the principle of | |
359 | least suprise; the user doesn't expect the fan minimum to change just | |
360 | because the divisor changed. */ | |
361 | static ssize_t set_fan_div(struct device *dev, const char *buf, | |
362 | size_t count, int nr) | |
363 | { | |
364 | struct i2c_client *client = to_i2c_client(dev); | |
365 | struct lm78_data *data = i2c_get_clientdata(client); | |
366 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
367 | unsigned long min; | |
368 | u8 reg; | |
369 | ||
370 | down(&data->update_lock); | |
371 | min = FAN_FROM_REG(data->fan_min[nr], | |
372 | DIV_FROM_REG(data->fan_div[nr])); | |
373 | ||
374 | switch (val) { | |
375 | case 1: data->fan_div[nr] = 0; break; | |
376 | case 2: data->fan_div[nr] = 1; break; | |
377 | case 4: data->fan_div[nr] = 2; break; | |
378 | case 8: data->fan_div[nr] = 3; break; | |
379 | default: | |
380 | dev_err(&client->dev, "fan_div value %ld not " | |
381 | "supported. Choose one of 1, 2, 4 or 8!\n", val); | |
382 | up(&data->update_lock); | |
383 | return -EINVAL; | |
384 | } | |
385 | ||
386 | reg = lm78_read_value(client, LM78_REG_VID_FANDIV); | |
387 | switch (nr) { | |
388 | case 0: | |
389 | reg = (reg & 0xcf) | (data->fan_div[nr] << 4); | |
390 | break; | |
391 | case 1: | |
392 | reg = (reg & 0x3f) | (data->fan_div[nr] << 6); | |
393 | break; | |
394 | } | |
395 | lm78_write_value(client, LM78_REG_VID_FANDIV, reg); | |
396 | ||
397 | data->fan_min[nr] = | |
398 | FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); | |
399 | lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); | |
400 | up(&data->update_lock); | |
401 | ||
402 | return count; | |
403 | } | |
404 | ||
405 | #define show_fan_offset(offset) \ | |
8627f9ba | 406 | static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
407 | { \ |
408 | return show_fan(dev, buf, offset - 1); \ | |
409 | } \ | |
8627f9ba | 410 | static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
411 | { \ |
412 | return show_fan_min(dev, buf, offset - 1); \ | |
413 | } \ | |
8627f9ba | 414 | static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
415 | { \ |
416 | return show_fan_div(dev, buf, offset - 1); \ | |
417 | } \ | |
8627f9ba | 418 | static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \ |
1da177e4 LT |
419 | const char *buf, size_t count) \ |
420 | { \ | |
421 | return set_fan_min(dev, buf, count, offset - 1); \ | |
422 | } \ | |
423 | static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\ | |
424 | static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
425 | show_fan_##offset##_min, set_fan_##offset##_min); | |
426 | ||
8627f9ba | 427 | static ssize_t set_fan_1_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, 0) ; | |
431 | } | |
432 | ||
8627f9ba | 433 | static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf, |
1da177e4 LT |
434 | size_t count) |
435 | { | |
436 | return set_fan_div(dev, buf, count, 1) ; | |
437 | } | |
438 | ||
439 | show_fan_offset(1); | |
440 | show_fan_offset(2); | |
441 | show_fan_offset(3); | |
442 | ||
443 | /* Fan 3 divisor is locked in H/W */ | |
444 | static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, | |
445 | show_fan_1_div, set_fan_1_div); | |
446 | static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, | |
447 | show_fan_2_div, set_fan_2_div); | |
448 | static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL); | |
449 | ||
450 | /* VID */ | |
8627f9ba | 451 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
452 | { |
453 | struct lm78_data *data = lm78_update_device(dev); | |
19f673ed | 454 | return sprintf(buf, "%d\n", vid_from_reg(82, data->vid)); |
1da177e4 LT |
455 | } |
456 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); | |
457 | ||
458 | /* Alarms */ | |
8627f9ba | 459 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
460 | { |
461 | struct lm78_data *data = lm78_update_device(dev); | |
462 | return sprintf(buf, "%u\n", data->alarms); | |
463 | } | |
464 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | |
465 | ||
466 | /* This function is called when: | |
467 | * lm78_driver is inserted (when this module is loaded), for each | |
468 | available adapter | |
469 | * when a new adapter is inserted (and lm78_driver is still present) */ | |
470 | static int lm78_attach_adapter(struct i2c_adapter *adapter) | |
471 | { | |
472 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
473 | return 0; | |
2ed2dc3c | 474 | return i2c_probe(adapter, &addr_data, lm78_detect); |
1da177e4 LT |
475 | } |
476 | ||
2d8672c5 JD |
477 | static int lm78_isa_attach_adapter(struct i2c_adapter *adapter) |
478 | { | |
479 | return lm78_detect(adapter, isa_address, -1); | |
480 | } | |
481 | ||
2ed2dc3c | 482 | /* This function is called by i2c_probe */ |
d8d20615 | 483 | static int lm78_detect(struct i2c_adapter *adapter, int address, int kind) |
1da177e4 LT |
484 | { |
485 | int i, err; | |
486 | struct i2c_client *new_client; | |
487 | struct lm78_data *data; | |
488 | const char *client_name = ""; | |
489 | int is_isa = i2c_is_isa_adapter(adapter); | |
490 | ||
491 | if (!is_isa && | |
492 | !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | |
493 | err = -ENODEV; | |
494 | goto ERROR0; | |
495 | } | |
496 | ||
497 | /* Reserve the ISA region */ | |
498 | if (is_isa) | |
fde09509 JD |
499 | if (!request_region(address, LM78_EXTENT, |
500 | lm78_isa_driver.name)) { | |
1da177e4 LT |
501 | err = -EBUSY; |
502 | goto ERROR0; | |
503 | } | |
504 | ||
505 | /* Probe whether there is anything available on this address. Already | |
506 | done for SMBus clients */ | |
507 | if (kind < 0) { | |
508 | if (is_isa) { | |
509 | ||
510 | #define REALLY_SLOW_IO | |
511 | /* We need the timeouts for at least some LM78-like | |
512 | chips. But only if we read 'undefined' registers. */ | |
513 | i = inb_p(address + 1); | |
514 | if (inb_p(address + 2) != i) { | |
515 | err = -ENODEV; | |
516 | goto ERROR1; | |
517 | } | |
518 | if (inb_p(address + 3) != i) { | |
519 | err = -ENODEV; | |
520 | goto ERROR1; | |
521 | } | |
522 | if (inb_p(address + 7) != i) { | |
523 | err = -ENODEV; | |
524 | goto ERROR1; | |
525 | } | |
526 | #undef REALLY_SLOW_IO | |
527 | ||
528 | /* Let's just hope nothing breaks here */ | |
529 | i = inb_p(address + 5) & 0x7f; | |
530 | outb_p(~i & 0x7f, address + 5); | |
531 | if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) { | |
532 | outb_p(i, address + 5); | |
533 | err = -ENODEV; | |
534 | goto ERROR1; | |
535 | } | |
536 | } | |
537 | } | |
538 | ||
539 | /* OK. For now, we presume we have a valid client. We now create the | |
540 | client structure, even though we cannot fill it completely yet. | |
541 | But it allows us to access lm78_{read,write}_value. */ | |
542 | ||
ba9c2e8d | 543 | if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) { |
1da177e4 LT |
544 | err = -ENOMEM; |
545 | goto ERROR1; | |
546 | } | |
1da177e4 LT |
547 | |
548 | new_client = &data->client; | |
549 | if (is_isa) | |
550 | init_MUTEX(&data->lock); | |
551 | i2c_set_clientdata(new_client, data); | |
552 | new_client->addr = address; | |
553 | new_client->adapter = adapter; | |
fde09509 | 554 | new_client->driver = is_isa ? &lm78_isa_driver : &lm78_driver; |
1da177e4 LT |
555 | new_client->flags = 0; |
556 | ||
557 | /* Now, we do the remaining detection. */ | |
558 | if (kind < 0) { | |
559 | if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) { | |
560 | err = -ENODEV; | |
561 | goto ERROR2; | |
562 | } | |
563 | if (!is_isa && (lm78_read_value( | |
564 | new_client, LM78_REG_I2C_ADDR) != address)) { | |
565 | err = -ENODEV; | |
566 | goto ERROR2; | |
567 | } | |
568 | } | |
569 | ||
570 | /* Determine the chip type. */ | |
571 | if (kind <= 0) { | |
572 | i = lm78_read_value(new_client, LM78_REG_CHIPID); | |
27fe048e JD |
573 | if (i == 0x00 || i == 0x20 /* LM78 */ |
574 | || i == 0x40) /* LM78-J */ | |
1da177e4 | 575 | kind = lm78; |
1da177e4 LT |
576 | else if ((i & 0xfe) == 0xc0) |
577 | kind = lm79; | |
578 | else { | |
579 | if (kind == 0) | |
580 | dev_warn(&adapter->dev, "Ignoring 'force' " | |
581 | "parameter for unknown chip at " | |
582 | "adapter %d, address 0x%02x\n", | |
583 | i2c_adapter_id(adapter), address); | |
584 | err = -ENODEV; | |
585 | goto ERROR2; | |
586 | } | |
587 | } | |
588 | ||
589 | if (kind == lm78) { | |
590 | client_name = "lm78"; | |
1da177e4 LT |
591 | } else if (kind == lm79) { |
592 | client_name = "lm79"; | |
593 | } | |
594 | ||
595 | /* Fill in the remaining client fields and put into the global list */ | |
596 | strlcpy(new_client->name, client_name, I2C_NAME_SIZE); | |
597 | data->type = kind; | |
598 | ||
599 | data->valid = 0; | |
600 | init_MUTEX(&data->update_lock); | |
601 | ||
602 | /* Tell the I2C layer a new client has arrived */ | |
603 | if ((err = i2c_attach_client(new_client))) | |
604 | goto ERROR2; | |
605 | ||
606 | /* Initialize the LM78 chip */ | |
607 | lm78_init_client(new_client); | |
608 | ||
609 | /* A few vars need to be filled upon startup */ | |
610 | for (i = 0; i < 3; i++) { | |
611 | data->fan_min[i] = lm78_read_value(new_client, | |
612 | LM78_REG_FAN_MIN(i)); | |
613 | } | |
614 | ||
615 | /* Register sysfs hooks */ | |
943b0830 MH |
616 | data->class_dev = hwmon_device_register(&new_client->dev); |
617 | if (IS_ERR(data->class_dev)) { | |
618 | err = PTR_ERR(data->class_dev); | |
619 | goto ERROR3; | |
620 | } | |
621 | ||
1da177e4 LT |
622 | device_create_file(&new_client->dev, &dev_attr_in0_input); |
623 | device_create_file(&new_client->dev, &dev_attr_in0_min); | |
624 | device_create_file(&new_client->dev, &dev_attr_in0_max); | |
625 | device_create_file(&new_client->dev, &dev_attr_in1_input); | |
626 | device_create_file(&new_client->dev, &dev_attr_in1_min); | |
627 | device_create_file(&new_client->dev, &dev_attr_in1_max); | |
628 | device_create_file(&new_client->dev, &dev_attr_in2_input); | |
629 | device_create_file(&new_client->dev, &dev_attr_in2_min); | |
630 | device_create_file(&new_client->dev, &dev_attr_in2_max); | |
631 | device_create_file(&new_client->dev, &dev_attr_in3_input); | |
632 | device_create_file(&new_client->dev, &dev_attr_in3_min); | |
633 | device_create_file(&new_client->dev, &dev_attr_in3_max); | |
634 | device_create_file(&new_client->dev, &dev_attr_in4_input); | |
635 | device_create_file(&new_client->dev, &dev_attr_in4_min); | |
636 | device_create_file(&new_client->dev, &dev_attr_in4_max); | |
637 | device_create_file(&new_client->dev, &dev_attr_in5_input); | |
638 | device_create_file(&new_client->dev, &dev_attr_in5_min); | |
639 | device_create_file(&new_client->dev, &dev_attr_in5_max); | |
640 | device_create_file(&new_client->dev, &dev_attr_in6_input); | |
641 | device_create_file(&new_client->dev, &dev_attr_in6_min); | |
642 | device_create_file(&new_client->dev, &dev_attr_in6_max); | |
643 | device_create_file(&new_client->dev, &dev_attr_temp1_input); | |
644 | device_create_file(&new_client->dev, &dev_attr_temp1_max); | |
645 | device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst); | |
646 | device_create_file(&new_client->dev, &dev_attr_fan1_input); | |
647 | device_create_file(&new_client->dev, &dev_attr_fan1_min); | |
648 | device_create_file(&new_client->dev, &dev_attr_fan1_div); | |
649 | device_create_file(&new_client->dev, &dev_attr_fan2_input); | |
650 | device_create_file(&new_client->dev, &dev_attr_fan2_min); | |
651 | device_create_file(&new_client->dev, &dev_attr_fan2_div); | |
652 | device_create_file(&new_client->dev, &dev_attr_fan3_input); | |
653 | device_create_file(&new_client->dev, &dev_attr_fan3_min); | |
654 | device_create_file(&new_client->dev, &dev_attr_fan3_div); | |
655 | device_create_file(&new_client->dev, &dev_attr_alarms); | |
656 | device_create_file(&new_client->dev, &dev_attr_cpu0_vid); | |
657 | ||
658 | return 0; | |
659 | ||
943b0830 MH |
660 | ERROR3: |
661 | i2c_detach_client(new_client); | |
1da177e4 LT |
662 | ERROR2: |
663 | kfree(data); | |
664 | ERROR1: | |
665 | if (is_isa) | |
666 | release_region(address, LM78_EXTENT); | |
667 | ERROR0: | |
668 | return err; | |
669 | } | |
670 | ||
671 | static int lm78_detach_client(struct i2c_client *client) | |
672 | { | |
943b0830 | 673 | struct lm78_data *data = i2c_get_clientdata(client); |
1da177e4 LT |
674 | int err; |
675 | ||
943b0830 MH |
676 | hwmon_device_unregister(data->class_dev); |
677 | ||
7bef5594 | 678 | if ((err = i2c_detach_client(client))) |
1da177e4 | 679 | return err; |
1da177e4 LT |
680 | |
681 | if(i2c_is_isa_client(client)) | |
682 | release_region(client->addr, LM78_EXTENT); | |
683 | ||
943b0830 | 684 | kfree(data); |
1da177e4 LT |
685 | |
686 | return 0; | |
687 | } | |
688 | ||
44bbe87e | 689 | /* The SMBus locks itself, but ISA access must 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 | static int lm78_read_value(struct i2c_client *client, u8 reg) | |
695 | { | |
696 | int res; | |
697 | if (i2c_is_isa_client(client)) { | |
698 | struct lm78_data *data = i2c_get_clientdata(client); | |
699 | down(&data->lock); | |
700 | outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET); | |
701 | res = inb_p(client->addr + LM78_DATA_REG_OFFSET); | |
702 | up(&data->lock); | |
703 | return res; | |
704 | } else | |
705 | return i2c_smbus_read_byte_data(client, reg); | |
706 | } | |
707 | ||
44bbe87e | 708 | /* The SMBus locks itself, but ISA access muse be locked explicitly! |
1da177e4 LT |
709 | We don't want to lock the whole ISA bus, so we lock each client |
710 | separately. | |
711 | We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, | |
712 | would slow down the LM78 access and should not be necessary. | |
713 | There are some ugly typecasts here, but the good new is - they should | |
714 | nowhere else be necessary! */ | |
715 | static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value) | |
716 | { | |
717 | if (i2c_is_isa_client(client)) { | |
718 | struct lm78_data *data = i2c_get_clientdata(client); | |
719 | down(&data->lock); | |
720 | outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET); | |
721 | outb_p(value, client->addr + LM78_DATA_REG_OFFSET); | |
722 | up(&data->lock); | |
723 | return 0; | |
724 | } else | |
725 | return i2c_smbus_write_byte_data(client, reg, value); | |
726 | } | |
727 | ||
1da177e4 LT |
728 | static void lm78_init_client(struct i2c_client *client) |
729 | { | |
730 | u8 config = lm78_read_value(client, LM78_REG_CONFIG); | |
731 | ||
732 | /* Start monitoring */ | |
733 | if (!(config & 0x01)) | |
734 | lm78_write_value(client, LM78_REG_CONFIG, | |
735 | (config & 0xf7) | 0x01); | |
736 | } | |
737 | ||
738 | static struct lm78_data *lm78_update_device(struct device *dev) | |
739 | { | |
740 | struct i2c_client *client = to_i2c_client(dev); | |
741 | struct lm78_data *data = i2c_get_clientdata(client); | |
742 | int i; | |
743 | ||
744 | down(&data->update_lock); | |
745 | ||
746 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
747 | || !data->valid) { | |
748 | ||
749 | dev_dbg(&client->dev, "Starting lm78 update\n"); | |
750 | ||
751 | for (i = 0; i <= 6; i++) { | |
752 | data->in[i] = | |
753 | lm78_read_value(client, LM78_REG_IN(i)); | |
754 | data->in_min[i] = | |
755 | lm78_read_value(client, LM78_REG_IN_MIN(i)); | |
756 | data->in_max[i] = | |
757 | lm78_read_value(client, LM78_REG_IN_MAX(i)); | |
758 | } | |
759 | for (i = 0; i < 3; i++) { | |
760 | data->fan[i] = | |
761 | lm78_read_value(client, LM78_REG_FAN(i)); | |
762 | data->fan_min[i] = | |
763 | lm78_read_value(client, LM78_REG_FAN_MIN(i)); | |
764 | } | |
765 | data->temp = lm78_read_value(client, LM78_REG_TEMP); | |
766 | data->temp_over = | |
767 | lm78_read_value(client, LM78_REG_TEMP_OVER); | |
768 | data->temp_hyst = | |
769 | lm78_read_value(client, LM78_REG_TEMP_HYST); | |
770 | i = lm78_read_value(client, LM78_REG_VID_FANDIV); | |
771 | data->vid = i & 0x0f; | |
772 | if (data->type == lm79) | |
773 | data->vid |= | |
774 | (lm78_read_value(client, LM78_REG_CHIPID) & | |
775 | 0x01) << 4; | |
776 | else | |
777 | data->vid |= 0x10; | |
778 | data->fan_div[0] = (i >> 4) & 0x03; | |
779 | data->fan_div[1] = i >> 6; | |
780 | data->alarms = lm78_read_value(client, LM78_REG_ALARM1) + | |
781 | (lm78_read_value(client, LM78_REG_ALARM2) << 8); | |
782 | data->last_updated = jiffies; | |
783 | data->valid = 1; | |
784 | ||
785 | data->fan_div[2] = 1; | |
786 | } | |
787 | ||
788 | up(&data->update_lock); | |
789 | ||
790 | return data; | |
791 | } | |
792 | ||
793 | static int __init sm_lm78_init(void) | |
794 | { | |
fde09509 JD |
795 | int res; |
796 | ||
797 | res = i2c_add_driver(&lm78_driver); | |
798 | if (res) | |
799 | return res; | |
800 | ||
801 | res = i2c_isa_add_driver(&lm78_isa_driver); | |
802 | if (res) { | |
803 | i2c_del_driver(&lm78_driver); | |
804 | return res; | |
805 | } | |
806 | ||
807 | return 0; | |
1da177e4 LT |
808 | } |
809 | ||
810 | static void __exit sm_lm78_exit(void) | |
811 | { | |
fde09509 | 812 | i2c_isa_del_driver(&lm78_isa_driver); |
1da177e4 LT |
813 | i2c_del_driver(&lm78_driver); |
814 | } | |
815 | ||
816 | ||
817 | ||
818 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); | |
27fe048e | 819 | MODULE_DESCRIPTION("LM78/LM79 driver"); |
1da177e4 LT |
820 | MODULE_LICENSE("GPL"); |
821 | ||
822 | module_init(sm_lm78_init); | |
823 | module_exit(sm_lm78_exit); |