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> | |
c40769fe | 5 | Copyright (c) 2007 Jean Delvare <khali@linux-fr.org> |
1da177e4 LT |
6 | |
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
21 | ||
1da177e4 LT |
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> | |
c40769fe JD |
27 | #include <linux/platform_device.h> |
28 | #include <linux/ioport.h> | |
943b0830 | 29 | #include <linux/hwmon.h> |
19f673ed | 30 | #include <linux/hwmon-vid.h> |
247dde4c | 31 | #include <linux/hwmon-sysfs.h> |
943b0830 | 32 | #include <linux/err.h> |
9a61bf63 | 33 | #include <linux/mutex.h> |
1da177e4 LT |
34 | #include <asm/io.h> |
35 | ||
c40769fe JD |
36 | /* ISA device, if found */ |
37 | static struct platform_device *pdev; | |
38 | ||
1da177e4 | 39 | /* Addresses to scan */ |
25e9c86d MH |
40 | static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, |
41 | 0x2e, 0x2f, I2C_CLIENT_END }; | |
2d8672c5 | 42 | static unsigned short isa_address = 0x290; |
1da177e4 LT |
43 | |
44 | /* Insmod parameters */ | |
f4b50261 | 45 | I2C_CLIENT_INSMOD_2(lm78, lm79); |
1da177e4 LT |
46 | |
47 | /* Many LM78 constants specified below */ | |
48 | ||
49 | /* Length of ISA address segment */ | |
50 | #define LM78_EXTENT 8 | |
51 | ||
52 | /* Where are the ISA address/data registers relative to the base address */ | |
53 | #define LM78_ADDR_REG_OFFSET 5 | |
54 | #define LM78_DATA_REG_OFFSET 6 | |
55 | ||
56 | /* The LM78 registers */ | |
57 | #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2) | |
58 | #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2) | |
59 | #define LM78_REG_IN(nr) (0x20 + (nr)) | |
60 | ||
61 | #define LM78_REG_FAN_MIN(nr) (0x3b + (nr)) | |
62 | #define LM78_REG_FAN(nr) (0x28 + (nr)) | |
63 | ||
64 | #define LM78_REG_TEMP 0x27 | |
65 | #define LM78_REG_TEMP_OVER 0x39 | |
66 | #define LM78_REG_TEMP_HYST 0x3a | |
67 | ||
68 | #define LM78_REG_ALARM1 0x41 | |
69 | #define LM78_REG_ALARM2 0x42 | |
70 | ||
71 | #define LM78_REG_VID_FANDIV 0x47 | |
72 | ||
73 | #define LM78_REG_CONFIG 0x40 | |
74 | #define LM78_REG_CHIPID 0x49 | |
75 | #define LM78_REG_I2C_ADDR 0x48 | |
76 | ||
77 | ||
78 | /* Conversions. Rounding and limit checking is only done on the TO_REG | |
79 | variants. */ | |
80 | ||
81 | /* IN: mV, (0V to 4.08V) | |
82 | REG: 16mV/bit */ | |
83 | static inline u8 IN_TO_REG(unsigned long val) | |
84 | { | |
85 | unsigned long nval = SENSORS_LIMIT(val, 0, 4080); | |
86 | return (nval + 8) / 16; | |
87 | } | |
88 | #define IN_FROM_REG(val) ((val) * 16) | |
89 | ||
90 | static inline u8 FAN_TO_REG(long rpm, int div) | |
91 | { | |
92 | if (rpm <= 0) | |
93 | return 255; | |
94 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); | |
95 | } | |
96 | ||
97 | static inline int FAN_FROM_REG(u8 val, int div) | |
98 | { | |
99 | return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div); | |
100 | } | |
101 | ||
102 | /* TEMP: mC (-128C to +127C) | |
103 | REG: 1C/bit, two's complement */ | |
104 | static inline s8 TEMP_TO_REG(int val) | |
105 | { | |
106 | int nval = SENSORS_LIMIT(val, -128000, 127000) ; | |
107 | return nval<0 ? (nval-500)/1000 : (nval+500)/1000; | |
108 | } | |
109 | ||
110 | static inline int TEMP_FROM_REG(s8 val) | |
111 | { | |
112 | return val * 1000; | |
113 | } | |
114 | ||
1da177e4 LT |
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 | ||
c40769fe JD |
128 | /* For ISA chips, we abuse the i2c_client addr and name fields. We also use |
129 | the driver field to differentiate between I2C and ISA chips. */ | |
1da177e4 LT |
130 | struct lm78_data { |
131 | struct i2c_client client; | |
1beeffe4 | 132 | struct device *hwmon_dev; |
9a61bf63 | 133 | struct mutex lock; |
1da177e4 LT |
134 | enum chips type; |
135 | ||
9a61bf63 | 136 | struct mutex update_lock; |
1da177e4 LT |
137 | char valid; /* !=0 if following fields are valid */ |
138 | unsigned long last_updated; /* In jiffies */ | |
139 | ||
140 | u8 in[7]; /* Register value */ | |
141 | u8 in_max[7]; /* Register value */ | |
142 | u8 in_min[7]; /* Register value */ | |
143 | u8 fan[3]; /* Register value */ | |
144 | u8 fan_min[3]; /* Register value */ | |
145 | s8 temp; /* Register value */ | |
146 | s8 temp_over; /* Register value */ | |
147 | s8 temp_hyst; /* Register value */ | |
148 | u8 fan_div[3]; /* Register encoding, shifted right */ | |
149 | u8 vid; /* Register encoding, combined */ | |
150 | u16 alarms; /* Register encoding, combined */ | |
151 | }; | |
152 | ||
153 | ||
154 | static int lm78_attach_adapter(struct i2c_adapter *adapter); | |
155 | static int lm78_detect(struct i2c_adapter *adapter, int address, int kind); | |
156 | static int lm78_detach_client(struct i2c_client *client); | |
157 | ||
c40769fe JD |
158 | static int __devinit lm78_isa_probe(struct platform_device *pdev); |
159 | static int __devexit lm78_isa_remove(struct platform_device *pdev); | |
160 | ||
c59cc301 JD |
161 | static int lm78_read_value(struct lm78_data *data, u8 reg); |
162 | static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value); | |
1da177e4 | 163 | static struct lm78_data *lm78_update_device(struct device *dev); |
c59cc301 | 164 | static void lm78_init_device(struct lm78_data *data); |
1da177e4 LT |
165 | |
166 | ||
167 | static struct i2c_driver lm78_driver = { | |
cdaf7934 | 168 | .driver = { |
cdaf7934 LR |
169 | .name = "lm78", |
170 | }, | |
1da177e4 LT |
171 | .attach_adapter = lm78_attach_adapter, |
172 | .detach_client = lm78_detach_client, | |
173 | }; | |
174 | ||
c40769fe | 175 | static struct platform_driver lm78_isa_driver = { |
cdaf7934 | 176 | .driver = { |
87218842 | 177 | .owner = THIS_MODULE, |
c40769fe | 178 | .name = "lm78", |
cdaf7934 | 179 | }, |
c40769fe JD |
180 | .probe = lm78_isa_probe, |
181 | .remove = lm78_isa_remove, | |
fde09509 JD |
182 | }; |
183 | ||
184 | ||
1da177e4 | 185 | /* 7 Voltages */ |
247dde4c JD |
186 | static ssize_t show_in(struct device *dev, struct device_attribute *da, |
187 | char *buf) | |
1da177e4 | 188 | { |
247dde4c | 189 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
1da177e4 | 190 | struct lm78_data *data = lm78_update_device(dev); |
247dde4c | 191 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index])); |
1da177e4 LT |
192 | } |
193 | ||
247dde4c JD |
194 | static ssize_t show_in_min(struct device *dev, struct device_attribute *da, |
195 | char *buf) | |
1da177e4 | 196 | { |
247dde4c | 197 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
1da177e4 | 198 | struct lm78_data *data = lm78_update_device(dev); |
247dde4c | 199 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index])); |
1da177e4 LT |
200 | } |
201 | ||
247dde4c JD |
202 | static ssize_t show_in_max(struct device *dev, struct device_attribute *da, |
203 | char *buf) | |
1da177e4 | 204 | { |
247dde4c | 205 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
1da177e4 | 206 | struct lm78_data *data = lm78_update_device(dev); |
247dde4c | 207 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index])); |
1da177e4 LT |
208 | } |
209 | ||
247dde4c JD |
210 | static ssize_t set_in_min(struct device *dev, struct device_attribute *da, |
211 | const char *buf, size_t count) | |
1da177e4 | 212 | { |
247dde4c | 213 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
c40769fe | 214 | struct lm78_data *data = dev_get_drvdata(dev); |
1da177e4 | 215 | unsigned long val = simple_strtoul(buf, NULL, 10); |
247dde4c | 216 | int nr = attr->index; |
1da177e4 | 217 | |
9a61bf63 | 218 | mutex_lock(&data->update_lock); |
1da177e4 | 219 | data->in_min[nr] = IN_TO_REG(val); |
c59cc301 | 220 | lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]); |
9a61bf63 | 221 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
222 | return count; |
223 | } | |
224 | ||
247dde4c JD |
225 | static ssize_t set_in_max(struct device *dev, struct device_attribute *da, |
226 | const char *buf, size_t count) | |
1da177e4 | 227 | { |
247dde4c | 228 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
c40769fe | 229 | struct lm78_data *data = dev_get_drvdata(dev); |
1da177e4 | 230 | unsigned long val = simple_strtoul(buf, NULL, 10); |
247dde4c | 231 | int nr = attr->index; |
1da177e4 | 232 | |
9a61bf63 | 233 | mutex_lock(&data->update_lock); |
1da177e4 | 234 | data->in_max[nr] = IN_TO_REG(val); |
c59cc301 | 235 | lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]); |
9a61bf63 | 236 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
237 | return count; |
238 | } | |
239 | ||
240 | #define show_in_offset(offset) \ | |
247dde4c JD |
241 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
242 | show_in, NULL, offset); \ | |
243 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ | |
244 | show_in_min, set_in_min, offset); \ | |
245 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
246 | show_in_max, set_in_max, offset); | |
1da177e4 LT |
247 | |
248 | show_in_offset(0); | |
249 | show_in_offset(1); | |
250 | show_in_offset(2); | |
251 | show_in_offset(3); | |
252 | show_in_offset(4); | |
253 | show_in_offset(5); | |
254 | show_in_offset(6); | |
255 | ||
256 | /* Temperature */ | |
247dde4c JD |
257 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
258 | char *buf) | |
1da177e4 LT |
259 | { |
260 | struct lm78_data *data = lm78_update_device(dev); | |
261 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp)); | |
262 | } | |
263 | ||
247dde4c JD |
264 | static ssize_t show_temp_over(struct device *dev, struct device_attribute *da, |
265 | char *buf) | |
1da177e4 LT |
266 | { |
267 | struct lm78_data *data = lm78_update_device(dev); | |
268 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over)); | |
269 | } | |
270 | ||
247dde4c JD |
271 | static ssize_t set_temp_over(struct device *dev, struct device_attribute *da, |
272 | const char *buf, size_t count) | |
1da177e4 | 273 | { |
c40769fe | 274 | struct lm78_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
275 | long val = simple_strtol(buf, NULL, 10); |
276 | ||
9a61bf63 | 277 | mutex_lock(&data->update_lock); |
1da177e4 | 278 | data->temp_over = TEMP_TO_REG(val); |
c59cc301 | 279 | lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over); |
9a61bf63 | 280 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
281 | return count; |
282 | } | |
283 | ||
247dde4c JD |
284 | static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da, |
285 | char *buf) | |
1da177e4 LT |
286 | { |
287 | struct lm78_data *data = lm78_update_device(dev); | |
288 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst)); | |
289 | } | |
290 | ||
247dde4c JD |
291 | static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da, |
292 | const char *buf, size_t count) | |
1da177e4 | 293 | { |
c40769fe | 294 | struct lm78_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
295 | long val = simple_strtol(buf, NULL, 10); |
296 | ||
9a61bf63 | 297 | mutex_lock(&data->update_lock); |
1da177e4 | 298 | data->temp_hyst = TEMP_TO_REG(val); |
c59cc301 | 299 | lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst); |
9a61bf63 | 300 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
301 | return count; |
302 | } | |
303 | ||
304 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); | |
305 | static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, | |
306 | show_temp_over, set_temp_over); | |
307 | static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, | |
308 | show_temp_hyst, set_temp_hyst); | |
309 | ||
310 | /* 3 Fans */ | |
247dde4c JD |
311 | static ssize_t show_fan(struct device *dev, struct device_attribute *da, |
312 | char *buf) | |
1da177e4 | 313 | { |
247dde4c | 314 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
1da177e4 | 315 | struct lm78_data *data = lm78_update_device(dev); |
247dde4c | 316 | int nr = attr->index; |
1da177e4 LT |
317 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
318 | DIV_FROM_REG(data->fan_div[nr])) ); | |
319 | } | |
320 | ||
247dde4c JD |
321 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *da, |
322 | char *buf) | |
1da177e4 | 323 | { |
247dde4c | 324 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
1da177e4 | 325 | struct lm78_data *data = lm78_update_device(dev); |
247dde4c | 326 | int nr = attr->index; |
1da177e4 LT |
327 | return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr], |
328 | DIV_FROM_REG(data->fan_div[nr])) ); | |
329 | } | |
330 | ||
247dde4c JD |
331 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *da, |
332 | const char *buf, size_t count) | |
1da177e4 | 333 | { |
247dde4c | 334 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
c40769fe | 335 | struct lm78_data *data = dev_get_drvdata(dev); |
247dde4c | 336 | int nr = attr->index; |
1da177e4 LT |
337 | unsigned long val = simple_strtoul(buf, NULL, 10); |
338 | ||
9a61bf63 | 339 | mutex_lock(&data->update_lock); |
1da177e4 | 340 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
c59cc301 | 341 | lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); |
9a61bf63 | 342 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
343 | return count; |
344 | } | |
345 | ||
247dde4c JD |
346 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *da, |
347 | char *buf) | |
1da177e4 | 348 | { |
247dde4c | 349 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
1da177e4 | 350 | struct lm78_data *data = lm78_update_device(dev); |
247dde4c | 351 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index])); |
1da177e4 LT |
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 | |
d6e05edc | 356 | least surprise; the user doesn't expect the fan minimum to change just |
1da177e4 | 357 | because the divisor changed. */ |
247dde4c JD |
358 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *da, |
359 | const char *buf, size_t count) | |
1da177e4 | 360 | { |
247dde4c | 361 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
c40769fe | 362 | struct lm78_data *data = dev_get_drvdata(dev); |
247dde4c | 363 | int nr = attr->index; |
1da177e4 LT |
364 | unsigned long val = simple_strtoul(buf, NULL, 10); |
365 | unsigned long min; | |
366 | u8 reg; | |
367 | ||
9a61bf63 | 368 | mutex_lock(&data->update_lock); |
1da177e4 LT |
369 | min = FAN_FROM_REG(data->fan_min[nr], |
370 | DIV_FROM_REG(data->fan_div[nr])); | |
371 | ||
372 | switch (val) { | |
373 | case 1: data->fan_div[nr] = 0; break; | |
374 | case 2: data->fan_div[nr] = 1; break; | |
375 | case 4: data->fan_div[nr] = 2; break; | |
376 | case 8: data->fan_div[nr] = 3; break; | |
377 | default: | |
c40769fe | 378 | dev_err(dev, "fan_div value %ld not " |
1da177e4 | 379 | "supported. Choose one of 1, 2, 4 or 8!\n", val); |
9a61bf63 | 380 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
381 | return -EINVAL; |
382 | } | |
383 | ||
c59cc301 | 384 | reg = lm78_read_value(data, LM78_REG_VID_FANDIV); |
1da177e4 LT |
385 | switch (nr) { |
386 | case 0: | |
387 | reg = (reg & 0xcf) | (data->fan_div[nr] << 4); | |
388 | break; | |
389 | case 1: | |
390 | reg = (reg & 0x3f) | (data->fan_div[nr] << 6); | |
391 | break; | |
392 | } | |
c59cc301 | 393 | lm78_write_value(data, LM78_REG_VID_FANDIV, reg); |
1da177e4 LT |
394 | |
395 | data->fan_min[nr] = | |
396 | FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); | |
c59cc301 | 397 | lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); |
9a61bf63 | 398 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
399 | |
400 | return count; | |
401 | } | |
402 | ||
247dde4c JD |
403 | #define show_fan_offset(offset) \ |
404 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ | |
405 | show_fan, NULL, offset - 1); \ | |
406 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
407 | show_fan_min, set_fan_min, offset - 1); | |
1da177e4 LT |
408 | |
409 | show_fan_offset(1); | |
410 | show_fan_offset(2); | |
411 | show_fan_offset(3); | |
412 | ||
413 | /* Fan 3 divisor is locked in H/W */ | |
247dde4c JD |
414 | static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, |
415 | show_fan_div, set_fan_div, 0); | |
416 | static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, | |
417 | show_fan_div, set_fan_div, 1); | |
418 | static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2); | |
1da177e4 LT |
419 | |
420 | /* VID */ | |
247dde4c JD |
421 | static ssize_t show_vid(struct device *dev, struct device_attribute *da, |
422 | char *buf) | |
1da177e4 LT |
423 | { |
424 | struct lm78_data *data = lm78_update_device(dev); | |
d0d3cd69 | 425 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82)); |
1da177e4 LT |
426 | } |
427 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); | |
428 | ||
429 | /* Alarms */ | |
247dde4c JD |
430 | static ssize_t show_alarms(struct device *dev, struct device_attribute *da, |
431 | char *buf) | |
1da177e4 LT |
432 | { |
433 | struct lm78_data *data = lm78_update_device(dev); | |
434 | return sprintf(buf, "%u\n", data->alarms); | |
435 | } | |
436 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | |
437 | ||
428a7039 JD |
438 | static ssize_t show_alarm(struct device *dev, struct device_attribute *da, |
439 | char *buf) | |
440 | { | |
441 | struct lm78_data *data = lm78_update_device(dev); | |
442 | int nr = to_sensor_dev_attr(da)->index; | |
443 | return sprintf(buf, "%u\n", (data->alarms >> nr) & 1); | |
444 | } | |
445 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); | |
446 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); | |
447 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); | |
448 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); | |
449 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); | |
450 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); | |
451 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10); | |
452 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); | |
453 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); | |
454 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11); | |
455 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); | |
456 | ||
1da177e4 LT |
457 | /* This function is called when: |
458 | * lm78_driver is inserted (when this module is loaded), for each | |
459 | available adapter | |
460 | * when a new adapter is inserted (and lm78_driver is still present) */ | |
461 | static int lm78_attach_adapter(struct i2c_adapter *adapter) | |
462 | { | |
463 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
464 | return 0; | |
2ed2dc3c | 465 | return i2c_probe(adapter, &addr_data, lm78_detect); |
1da177e4 LT |
466 | } |
467 | ||
c1685f61 | 468 | static struct attribute *lm78_attributes[] = { |
247dde4c JD |
469 | &sensor_dev_attr_in0_input.dev_attr.attr, |
470 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
471 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
428a7039 | 472 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
247dde4c JD |
473 | &sensor_dev_attr_in1_input.dev_attr.attr, |
474 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
475 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
428a7039 | 476 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
247dde4c JD |
477 | &sensor_dev_attr_in2_input.dev_attr.attr, |
478 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
479 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
428a7039 | 480 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
247dde4c JD |
481 | &sensor_dev_attr_in3_input.dev_attr.attr, |
482 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
483 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
428a7039 | 484 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
247dde4c JD |
485 | &sensor_dev_attr_in4_input.dev_attr.attr, |
486 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
487 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
428a7039 | 488 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
247dde4c JD |
489 | &sensor_dev_attr_in5_input.dev_attr.attr, |
490 | &sensor_dev_attr_in5_min.dev_attr.attr, | |
491 | &sensor_dev_attr_in5_max.dev_attr.attr, | |
428a7039 | 492 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
247dde4c JD |
493 | &sensor_dev_attr_in6_input.dev_attr.attr, |
494 | &sensor_dev_attr_in6_min.dev_attr.attr, | |
495 | &sensor_dev_attr_in6_max.dev_attr.attr, | |
428a7039 | 496 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
c1685f61 MH |
497 | &dev_attr_temp1_input.attr, |
498 | &dev_attr_temp1_max.attr, | |
499 | &dev_attr_temp1_max_hyst.attr, | |
428a7039 | 500 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
247dde4c JD |
501 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
502 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
503 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
428a7039 | 504 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
247dde4c JD |
505 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
506 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
507 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
428a7039 | 508 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
247dde4c JD |
509 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
510 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
511 | &sensor_dev_attr_fan3_div.dev_attr.attr, | |
428a7039 | 512 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
c1685f61 MH |
513 | &dev_attr_alarms.attr, |
514 | &dev_attr_cpu0_vid.attr, | |
515 | ||
516 | NULL | |
517 | }; | |
518 | ||
519 | static const struct attribute_group lm78_group = { | |
520 | .attrs = lm78_attributes, | |
521 | }; | |
522 | ||
c40769fe JD |
523 | /* I2C devices get this name attribute automatically, but for ISA devices |
524 | we must create it by ourselves. */ | |
525 | static ssize_t show_name(struct device *dev, struct device_attribute | |
526 | *devattr, char *buf) | |
527 | { | |
528 | struct lm78_data *data = dev_get_drvdata(dev); | |
529 | ||
530 | return sprintf(buf, "%s\n", data->client.name); | |
531 | } | |
532 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | |
533 | ||
2ed2dc3c | 534 | /* This function is called by i2c_probe */ |
d8d20615 | 535 | static int lm78_detect(struct i2c_adapter *adapter, int address, int kind) |
1da177e4 LT |
536 | { |
537 | int i, err; | |
538 | struct i2c_client *new_client; | |
539 | struct lm78_data *data; | |
540 | const char *client_name = ""; | |
1da177e4 | 541 | |
c40769fe | 542 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
1da177e4 | 543 | err = -ENODEV; |
c40769fe | 544 | goto ERROR1; |
1da177e4 LT |
545 | } |
546 | ||
547 | /* OK. For now, we presume we have a valid client. We now create the | |
548 | client structure, even though we cannot fill it completely yet. | |
549 | But it allows us to access lm78_{read,write}_value. */ | |
550 | ||
ba9c2e8d | 551 | if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) { |
1da177e4 LT |
552 | err = -ENOMEM; |
553 | goto ERROR1; | |
554 | } | |
1da177e4 LT |
555 | |
556 | new_client = &data->client; | |
1da177e4 LT |
557 | i2c_set_clientdata(new_client, data); |
558 | new_client->addr = address; | |
559 | new_client->adapter = adapter; | |
c40769fe | 560 | new_client->driver = &lm78_driver; |
1da177e4 LT |
561 | |
562 | /* Now, we do the remaining detection. */ | |
563 | if (kind < 0) { | |
c59cc301 | 564 | if (lm78_read_value(data, LM78_REG_CONFIG) & 0x80) { |
1da177e4 LT |
565 | err = -ENODEV; |
566 | goto ERROR2; | |
567 | } | |
c59cc301 | 568 | if (lm78_read_value(data, LM78_REG_I2C_ADDR) != |
c40769fe | 569 | address) { |
1da177e4 LT |
570 | err = -ENODEV; |
571 | goto ERROR2; | |
572 | } | |
573 | } | |
574 | ||
575 | /* Determine the chip type. */ | |
576 | if (kind <= 0) { | |
c59cc301 | 577 | i = lm78_read_value(data, LM78_REG_CHIPID); |
27fe048e JD |
578 | if (i == 0x00 || i == 0x20 /* LM78 */ |
579 | || i == 0x40) /* LM78-J */ | |
1da177e4 | 580 | kind = lm78; |
1da177e4 LT |
581 | else if ((i & 0xfe) == 0xc0) |
582 | kind = lm79; | |
583 | else { | |
584 | if (kind == 0) | |
585 | dev_warn(&adapter->dev, "Ignoring 'force' " | |
586 | "parameter for unknown chip at " | |
587 | "adapter %d, address 0x%02x\n", | |
588 | i2c_adapter_id(adapter), address); | |
589 | err = -ENODEV; | |
590 | goto ERROR2; | |
591 | } | |
592 | } | |
593 | ||
594 | if (kind == lm78) { | |
595 | client_name = "lm78"; | |
1da177e4 LT |
596 | } else if (kind == lm79) { |
597 | client_name = "lm79"; | |
598 | } | |
599 | ||
600 | /* Fill in the remaining client fields and put into the global list */ | |
601 | strlcpy(new_client->name, client_name, I2C_NAME_SIZE); | |
602 | data->type = kind; | |
603 | ||
1da177e4 LT |
604 | /* Tell the I2C layer a new client has arrived */ |
605 | if ((err = i2c_attach_client(new_client))) | |
606 | goto ERROR2; | |
607 | ||
608 | /* Initialize the LM78 chip */ | |
c59cc301 | 609 | lm78_init_device(data); |
1da177e4 | 610 | |
1da177e4 | 611 | /* Register sysfs hooks */ |
c1685f61 MH |
612 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group))) |
613 | goto ERROR3; | |
614 | ||
1beeffe4 TJ |
615 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
616 | if (IS_ERR(data->hwmon_dev)) { | |
617 | err = PTR_ERR(data->hwmon_dev); | |
c1685f61 | 618 | goto ERROR4; |
943b0830 MH |
619 | } |
620 | ||
1da177e4 LT |
621 | return 0; |
622 | ||
c1685f61 MH |
623 | ERROR4: |
624 | sysfs_remove_group(&new_client->dev.kobj, &lm78_group); | |
943b0830 MH |
625 | ERROR3: |
626 | i2c_detach_client(new_client); | |
1da177e4 LT |
627 | ERROR2: |
628 | kfree(data); | |
629 | ERROR1: | |
1da177e4 LT |
630 | return err; |
631 | } | |
632 | ||
633 | static int lm78_detach_client(struct i2c_client *client) | |
634 | { | |
943b0830 | 635 | struct lm78_data *data = i2c_get_clientdata(client); |
1da177e4 LT |
636 | int err; |
637 | ||
1beeffe4 | 638 | hwmon_device_unregister(data->hwmon_dev); |
c1685f61 | 639 | sysfs_remove_group(&client->dev.kobj, &lm78_group); |
943b0830 | 640 | |
7bef5594 | 641 | if ((err = i2c_detach_client(client))) |
1da177e4 | 642 | return err; |
1da177e4 | 643 | |
c40769fe JD |
644 | kfree(data); |
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
649 | static int __devinit lm78_isa_probe(struct platform_device *pdev) | |
650 | { | |
651 | int err; | |
652 | struct lm78_data *data; | |
653 | struct resource *res; | |
654 | const char *name; | |
655 | ||
656 | /* Reserve the ISA region */ | |
657 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); | |
658 | if (!request_region(res->start, LM78_EXTENT, "lm78")) { | |
659 | err = -EBUSY; | |
660 | goto exit; | |
661 | } | |
662 | ||
663 | if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) { | |
664 | err = -ENOMEM; | |
665 | goto exit_release_region; | |
666 | } | |
667 | mutex_init(&data->lock); | |
668 | data->client.addr = res->start; | |
669 | i2c_set_clientdata(&data->client, data); | |
670 | platform_set_drvdata(pdev, data); | |
671 | ||
c59cc301 | 672 | if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) { |
c40769fe JD |
673 | data->type = lm79; |
674 | name = "lm79"; | |
675 | } else { | |
676 | data->type = lm78; | |
677 | name = "lm78"; | |
678 | } | |
679 | strlcpy(data->client.name, name, I2C_NAME_SIZE); | |
680 | ||
681 | /* Initialize the LM78 chip */ | |
c59cc301 | 682 | lm78_init_device(data); |
c40769fe JD |
683 | |
684 | /* Register sysfs hooks */ | |
685 | if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group)) | |
686 | || (err = device_create_file(&pdev->dev, &dev_attr_name))) | |
687 | goto exit_remove_files; | |
688 | ||
1beeffe4 TJ |
689 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
690 | if (IS_ERR(data->hwmon_dev)) { | |
691 | err = PTR_ERR(data->hwmon_dev); | |
c40769fe JD |
692 | goto exit_remove_files; |
693 | } | |
694 | ||
695 | return 0; | |
696 | ||
697 | exit_remove_files: | |
698 | sysfs_remove_group(&pdev->dev.kobj, &lm78_group); | |
699 | device_remove_file(&pdev->dev, &dev_attr_name); | |
700 | kfree(data); | |
701 | exit_release_region: | |
702 | release_region(res->start, LM78_EXTENT); | |
703 | exit: | |
704 | return err; | |
705 | } | |
706 | ||
707 | static int __devexit lm78_isa_remove(struct platform_device *pdev) | |
708 | { | |
709 | struct lm78_data *data = platform_get_drvdata(pdev); | |
1da177e4 | 710 | |
1beeffe4 | 711 | hwmon_device_unregister(data->hwmon_dev); |
c40769fe JD |
712 | sysfs_remove_group(&pdev->dev.kobj, &lm78_group); |
713 | device_remove_file(&pdev->dev, &dev_attr_name); | |
714 | release_region(data->client.addr, LM78_EXTENT); | |
943b0830 | 715 | kfree(data); |
1da177e4 LT |
716 | |
717 | return 0; | |
718 | } | |
719 | ||
44bbe87e | 720 | /* The SMBus locks itself, but ISA access must be locked explicitly! |
1da177e4 LT |
721 | We don't want to lock the whole ISA bus, so we lock each client |
722 | separately. | |
723 | We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, | |
724 | would slow down the LM78 access and should not be necessary. */ | |
c59cc301 | 725 | static int lm78_read_value(struct lm78_data *data, u8 reg) |
1da177e4 | 726 | { |
c59cc301 JD |
727 | struct i2c_client *client = &data->client; |
728 | ||
c40769fe | 729 | if (!client->driver) { /* ISA device */ |
c59cc301 | 730 | int res; |
9a61bf63 | 731 | mutex_lock(&data->lock); |
1da177e4 LT |
732 | outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET); |
733 | res = inb_p(client->addr + LM78_DATA_REG_OFFSET); | |
9a61bf63 | 734 | mutex_unlock(&data->lock); |
1da177e4 LT |
735 | return res; |
736 | } else | |
737 | return i2c_smbus_read_byte_data(client, reg); | |
738 | } | |
739 | ||
44bbe87e | 740 | /* The SMBus locks itself, but ISA access muse be locked explicitly! |
1da177e4 LT |
741 | We don't want to lock the whole ISA bus, so we lock each client |
742 | separately. | |
743 | We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, | |
744 | would slow down the LM78 access and should not be necessary. | |
745 | There are some ugly typecasts here, but the good new is - they should | |
746 | nowhere else be necessary! */ | |
c59cc301 | 747 | static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value) |
1da177e4 | 748 | { |
c59cc301 JD |
749 | struct i2c_client *client = &data->client; |
750 | ||
c40769fe | 751 | if (!client->driver) { /* ISA device */ |
9a61bf63 | 752 | mutex_lock(&data->lock); |
1da177e4 LT |
753 | outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET); |
754 | outb_p(value, client->addr + LM78_DATA_REG_OFFSET); | |
9a61bf63 | 755 | mutex_unlock(&data->lock); |
1da177e4 LT |
756 | return 0; |
757 | } else | |
758 | return i2c_smbus_write_byte_data(client, reg, value); | |
759 | } | |
760 | ||
c59cc301 | 761 | static void lm78_init_device(struct lm78_data *data) |
1da177e4 | 762 | { |
c40769fe JD |
763 | u8 config; |
764 | int i; | |
1da177e4 LT |
765 | |
766 | /* Start monitoring */ | |
c59cc301 | 767 | config = lm78_read_value(data, LM78_REG_CONFIG); |
c40769fe | 768 | if ((config & 0x09) != 0x01) |
c59cc301 | 769 | lm78_write_value(data, LM78_REG_CONFIG, |
1da177e4 | 770 | (config & 0xf7) | 0x01); |
c40769fe JD |
771 | |
772 | /* A few vars need to be filled upon startup */ | |
773 | for (i = 0; i < 3; i++) { | |
c59cc301 | 774 | data->fan_min[i] = lm78_read_value(data, |
c40769fe JD |
775 | LM78_REG_FAN_MIN(i)); |
776 | } | |
777 | ||
778 | mutex_init(&data->update_lock); | |
1da177e4 LT |
779 | } |
780 | ||
781 | static struct lm78_data *lm78_update_device(struct device *dev) | |
782 | { | |
c40769fe | 783 | struct lm78_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
784 | int i; |
785 | ||
9a61bf63 | 786 | mutex_lock(&data->update_lock); |
1da177e4 LT |
787 | |
788 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
789 | || !data->valid) { | |
790 | ||
c40769fe | 791 | dev_dbg(dev, "Starting lm78 update\n"); |
1da177e4 LT |
792 | |
793 | for (i = 0; i <= 6; i++) { | |
794 | data->in[i] = | |
c59cc301 | 795 | lm78_read_value(data, LM78_REG_IN(i)); |
1da177e4 | 796 | data->in_min[i] = |
c59cc301 | 797 | lm78_read_value(data, LM78_REG_IN_MIN(i)); |
1da177e4 | 798 | data->in_max[i] = |
c59cc301 | 799 | lm78_read_value(data, LM78_REG_IN_MAX(i)); |
1da177e4 LT |
800 | } |
801 | for (i = 0; i < 3; i++) { | |
802 | data->fan[i] = | |
c59cc301 | 803 | lm78_read_value(data, LM78_REG_FAN(i)); |
1da177e4 | 804 | data->fan_min[i] = |
c59cc301 | 805 | lm78_read_value(data, LM78_REG_FAN_MIN(i)); |
1da177e4 | 806 | } |
c59cc301 | 807 | data->temp = lm78_read_value(data, LM78_REG_TEMP); |
1da177e4 | 808 | data->temp_over = |
c59cc301 | 809 | lm78_read_value(data, LM78_REG_TEMP_OVER); |
1da177e4 | 810 | data->temp_hyst = |
c59cc301 JD |
811 | lm78_read_value(data, LM78_REG_TEMP_HYST); |
812 | i = lm78_read_value(data, LM78_REG_VID_FANDIV); | |
1da177e4 LT |
813 | data->vid = i & 0x0f; |
814 | if (data->type == lm79) | |
815 | data->vid |= | |
c59cc301 | 816 | (lm78_read_value(data, LM78_REG_CHIPID) & |
1da177e4 LT |
817 | 0x01) << 4; |
818 | else | |
819 | data->vid |= 0x10; | |
820 | data->fan_div[0] = (i >> 4) & 0x03; | |
821 | data->fan_div[1] = i >> 6; | |
c59cc301 JD |
822 | data->alarms = lm78_read_value(data, LM78_REG_ALARM1) + |
823 | (lm78_read_value(data, LM78_REG_ALARM2) << 8); | |
1da177e4 LT |
824 | data->last_updated = jiffies; |
825 | data->valid = 1; | |
826 | ||
827 | data->fan_div[2] = 1; | |
828 | } | |
829 | ||
9a61bf63 | 830 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
831 | |
832 | return data; | |
833 | } | |
834 | ||
c40769fe JD |
835 | /* return 1 if a supported chip is found, 0 otherwise */ |
836 | static int __init lm78_isa_found(unsigned short address) | |
837 | { | |
838 | int val, save, found = 0; | |
839 | ||
840 | if (!request_region(address, LM78_EXTENT, "lm78")) | |
841 | return 0; | |
842 | ||
843 | #define REALLY_SLOW_IO | |
844 | /* We need the timeouts for at least some LM78-like | |
845 | chips. But only if we read 'undefined' registers. */ | |
846 | val = inb_p(address + 1); | |
847 | if (inb_p(address + 2) != val | |
848 | || inb_p(address + 3) != val | |
849 | || inb_p(address + 7) != val) | |
850 | goto release; | |
851 | #undef REALLY_SLOW_IO | |
852 | ||
853 | /* We should be able to change the 7 LSB of the address port. The | |
854 | MSB (busy flag) should be clear initially, set after the write. */ | |
855 | save = inb_p(address + LM78_ADDR_REG_OFFSET); | |
856 | if (save & 0x80) | |
857 | goto release; | |
858 | val = ~save & 0x7f; | |
859 | outb_p(val, address + LM78_ADDR_REG_OFFSET); | |
860 | if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) { | |
861 | outb_p(save, address + LM78_ADDR_REG_OFFSET); | |
862 | goto release; | |
863 | } | |
864 | ||
865 | /* We found a device, now see if it could be an LM78 */ | |
866 | outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET); | |
867 | val = inb_p(address + LM78_DATA_REG_OFFSET); | |
868 | if (val & 0x80) | |
869 | goto release; | |
870 | outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET); | |
871 | val = inb_p(address + LM78_DATA_REG_OFFSET); | |
872 | if (val < 0x03 || val > 0x77) /* Not a valid I2C address */ | |
873 | goto release; | |
874 | ||
875 | /* The busy flag should be clear again */ | |
876 | if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80) | |
877 | goto release; | |
878 | ||
879 | /* Explicitly prevent the misdetection of Winbond chips */ | |
880 | outb_p(0x4f, address + LM78_ADDR_REG_OFFSET); | |
881 | val = inb_p(address + LM78_DATA_REG_OFFSET); | |
882 | if (val == 0xa3 || val == 0x5c) | |
883 | goto release; | |
884 | ||
885 | /* Explicitly prevent the misdetection of ITE chips */ | |
886 | outb_p(0x58, address + LM78_ADDR_REG_OFFSET); | |
887 | val = inb_p(address + LM78_DATA_REG_OFFSET); | |
888 | if (val == 0x90) | |
889 | goto release; | |
890 | ||
891 | /* Determine the chip type */ | |
892 | outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET); | |
893 | val = inb_p(address + LM78_DATA_REG_OFFSET); | |
acf346a3 | 894 | if (val == 0x00 || val == 0x20 /* LM78 */ |
c40769fe JD |
895 | || val == 0x40 /* LM78-J */ |
896 | || (val & 0xfe) == 0xc0) /* LM79 */ | |
897 | found = 1; | |
898 | ||
899 | if (found) | |
900 | pr_info("lm78: Found an %s chip at %#x\n", | |
901 | val & 0x80 ? "LM79" : "LM78", (int)address); | |
902 | ||
903 | release: | |
904 | release_region(address, LM78_EXTENT); | |
905 | return found; | |
906 | } | |
907 | ||
908 | static int __init lm78_isa_device_add(unsigned short address) | |
909 | { | |
910 | struct resource res = { | |
911 | .start = address, | |
15bde2f1 | 912 | .end = address + LM78_EXTENT - 1, |
c40769fe JD |
913 | .name = "lm78", |
914 | .flags = IORESOURCE_IO, | |
915 | }; | |
916 | int err; | |
917 | ||
918 | pdev = platform_device_alloc("lm78", address); | |
919 | if (!pdev) { | |
920 | err = -ENOMEM; | |
921 | printk(KERN_ERR "lm78: Device allocation failed\n"); | |
922 | goto exit; | |
923 | } | |
924 | ||
925 | err = platform_device_add_resources(pdev, &res, 1); | |
926 | if (err) { | |
927 | printk(KERN_ERR "lm78: Device resource addition failed " | |
928 | "(%d)\n", err); | |
929 | goto exit_device_put; | |
930 | } | |
931 | ||
932 | err = platform_device_add(pdev); | |
933 | if (err) { | |
934 | printk(KERN_ERR "lm78: Device addition failed (%d)\n", | |
935 | err); | |
936 | goto exit_device_put; | |
937 | } | |
938 | ||
939 | return 0; | |
940 | ||
941 | exit_device_put: | |
942 | platform_device_put(pdev); | |
943 | exit: | |
944 | pdev = NULL; | |
945 | return err; | |
946 | } | |
947 | ||
1da177e4 LT |
948 | static int __init sm_lm78_init(void) |
949 | { | |
fde09509 JD |
950 | int res; |
951 | ||
952 | res = i2c_add_driver(&lm78_driver); | |
953 | if (res) | |
c40769fe JD |
954 | goto exit; |
955 | ||
956 | if (lm78_isa_found(isa_address)) { | |
957 | res = platform_driver_register(&lm78_isa_driver); | |
958 | if (res) | |
959 | goto exit_unreg_i2c_driver; | |
fde09509 | 960 | |
c40769fe JD |
961 | /* Sets global pdev as a side effect */ |
962 | res = lm78_isa_device_add(isa_address); | |
963 | if (res) | |
964 | goto exit_unreg_isa_driver; | |
965 | } | |
fde09509 JD |
966 | |
967 | return 0; | |
c40769fe JD |
968 | |
969 | exit_unreg_isa_driver: | |
970 | platform_driver_unregister(&lm78_isa_driver); | |
971 | exit_unreg_i2c_driver: | |
972 | i2c_del_driver(&lm78_driver); | |
973 | exit: | |
974 | return res; | |
1da177e4 LT |
975 | } |
976 | ||
977 | static void __exit sm_lm78_exit(void) | |
978 | { | |
c40769fe JD |
979 | if (pdev) { |
980 | platform_device_unregister(pdev); | |
981 | platform_driver_unregister(&lm78_isa_driver); | |
982 | } | |
1da177e4 LT |
983 | i2c_del_driver(&lm78_driver); |
984 | } | |
985 | ||
986 | ||
987 | ||
988 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); | |
27fe048e | 989 | MODULE_DESCRIPTION("LM78/LM79 driver"); |
1da177e4 LT |
990 | MODULE_LICENSE("GPL"); |
991 | ||
992 | module_init(sm_lm78_init); | |
993 | module_exit(sm_lm78_exit); |