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