xref: /openbmc/linux/drivers/hwmon/lm78.c (revision 8f762fe5)
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  * Copyright (c) 2007, 2011  Jean Delvare <jdelvare@suse.de>
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 
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-vid.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 
35 #ifdef CONFIG_ISA
36 #include <linux/platform_device.h>
37 #include <linux/ioport.h>
38 #include <linux/io.h>
39 #endif
40 
41 /* Addresses to scan */
42 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 						0x2e, 0x2f, I2C_CLIENT_END };
44 enum chips { lm78, lm79 };
45 
46 /* Many LM78 constants specified below */
47 
48 /* Length of ISA address segment */
49 #define LM78_EXTENT 8
50 
51 /* Where are the ISA address/data registers relative to the base address */
52 #define LM78_ADDR_REG_OFFSET 5
53 #define LM78_DATA_REG_OFFSET 6
54 
55 /* The LM78 registers */
56 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
57 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
58 #define LM78_REG_IN(nr) (0x20 + (nr))
59 
60 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
61 #define LM78_REG_FAN(nr) (0x28 + (nr))
62 
63 #define LM78_REG_TEMP 0x27
64 #define LM78_REG_TEMP_OVER 0x39
65 #define LM78_REG_TEMP_HYST 0x3a
66 
67 #define LM78_REG_ALARM1 0x41
68 #define LM78_REG_ALARM2 0x42
69 
70 #define LM78_REG_VID_FANDIV 0x47
71 
72 #define LM78_REG_CONFIG 0x40
73 #define LM78_REG_CHIPID 0x49
74 #define LM78_REG_I2C_ADDR 0x48
75 
76 /*
77  * Conversions. Rounding and limit checking is only done on the TO_REG
78  * variants.
79  */
80 
81 /*
82  * IN: mV (0V to 4.08V)
83  * REG: 16mV/bit
84  */
85 static inline u8 IN_TO_REG(unsigned long val)
86 {
87 	unsigned long nval = clamp_val(val, 0, 4080);
88 	return (nval + 8) / 16;
89 }
90 #define IN_FROM_REG(val) ((val) *  16)
91 
92 static inline u8 FAN_TO_REG(long rpm, int div)
93 {
94 	if (rpm <= 0)
95 		return 255;
96 	if (rpm > 1350000)
97 		return 1;
98 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
99 }
100 
101 static inline int FAN_FROM_REG(u8 val, int div)
102 {
103 	return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
104 }
105 
106 /*
107  * TEMP: mC (-128C to +127C)
108  * REG: 1C/bit, two's complement
109  */
110 static inline s8 TEMP_TO_REG(long val)
111 {
112 	int nval = clamp_val(val, -128000, 127000) ;
113 	return nval < 0 ? (nval - 500) / 1000 : (nval + 500) / 1000;
114 }
115 
116 static inline int TEMP_FROM_REG(s8 val)
117 {
118 	return val * 1000;
119 }
120 
121 #define DIV_FROM_REG(val) (1 << (val))
122 
123 struct lm78_data {
124 	struct i2c_client *client;
125 	struct mutex lock;
126 	enum chips type;
127 
128 	/* For ISA device only */
129 	const char *name;
130 	int isa_addr;
131 
132 	struct mutex update_lock;
133 	char valid;		/* !=0 if following fields are valid */
134 	unsigned long last_updated;	/* In jiffies */
135 
136 	u8 in[7];		/* Register value */
137 	u8 in_max[7];		/* Register value */
138 	u8 in_min[7];		/* Register value */
139 	u8 fan[3];		/* Register value */
140 	u8 fan_min[3];		/* Register value */
141 	s8 temp;		/* Register value */
142 	s8 temp_over;		/* Register value */
143 	s8 temp_hyst;		/* Register value */
144 	u8 fan_div[3];		/* Register encoding, shifted right */
145 	u8 vid;			/* Register encoding, combined */
146 	u16 alarms;		/* Register encoding, combined */
147 };
148 
149 static int lm78_read_value(struct lm78_data *data, u8 reg);
150 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
151 static struct lm78_data *lm78_update_device(struct device *dev);
152 static void lm78_init_device(struct lm78_data *data);
153 
154 /* 7 Voltages */
155 static ssize_t in_show(struct device *dev, struct device_attribute *da,
156 		       char *buf)
157 {
158 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
159 	struct lm78_data *data = lm78_update_device(dev);
160 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index]));
161 }
162 
163 static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
164 			   char *buf)
165 {
166 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
167 	struct lm78_data *data = lm78_update_device(dev);
168 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index]));
169 }
170 
171 static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
172 			   char *buf)
173 {
174 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
175 	struct lm78_data *data = lm78_update_device(dev);
176 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index]));
177 }
178 
179 static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
180 			    const char *buf, size_t count)
181 {
182 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
183 	struct lm78_data *data = dev_get_drvdata(dev);
184 	int nr = attr->index;
185 	unsigned long val;
186 	int err;
187 
188 	err = kstrtoul(buf, 10, &val);
189 	if (err)
190 		return err;
191 
192 	mutex_lock(&data->update_lock);
193 	data->in_min[nr] = IN_TO_REG(val);
194 	lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
195 	mutex_unlock(&data->update_lock);
196 	return count;
197 }
198 
199 static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
200 			    const char *buf, size_t count)
201 {
202 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
203 	struct lm78_data *data = dev_get_drvdata(dev);
204 	int nr = attr->index;
205 	unsigned long val;
206 	int err;
207 
208 	err = kstrtoul(buf, 10, &val);
209 	if (err)
210 		return err;
211 
212 	mutex_lock(&data->update_lock);
213 	data->in_max[nr] = IN_TO_REG(val);
214 	lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
215 	mutex_unlock(&data->update_lock);
216 	return count;
217 }
218 
219 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
220 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
221 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
222 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
223 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
224 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
225 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
226 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
227 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
228 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
229 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
230 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
231 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
232 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
233 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
234 static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
235 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
236 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
237 static SENSOR_DEVICE_ATTR_RO(in6_input, in, 6);
238 static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
239 static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
240 
241 /* Temperature */
242 static ssize_t temp1_input_show(struct device *dev,
243 				struct device_attribute *da, char *buf)
244 {
245 	struct lm78_data *data = lm78_update_device(dev);
246 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
247 }
248 
249 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *da,
250 			      char *buf)
251 {
252 	struct lm78_data *data = lm78_update_device(dev);
253 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
254 }
255 
256 static ssize_t temp1_max_store(struct device *dev,
257 			       struct device_attribute *da, const char *buf,
258 			       size_t count)
259 {
260 	struct lm78_data *data = dev_get_drvdata(dev);
261 	long val;
262 	int err;
263 
264 	err = kstrtol(buf, 10, &val);
265 	if (err)
266 		return err;
267 
268 	mutex_lock(&data->update_lock);
269 	data->temp_over = TEMP_TO_REG(val);
270 	lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
271 	mutex_unlock(&data->update_lock);
272 	return count;
273 }
274 
275 static ssize_t temp1_max_hyst_show(struct device *dev,
276 				   struct device_attribute *da, char *buf)
277 {
278 	struct lm78_data *data = lm78_update_device(dev);
279 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
280 }
281 
282 static ssize_t temp1_max_hyst_store(struct device *dev,
283 				    struct device_attribute *da,
284 				    const char *buf, size_t count)
285 {
286 	struct lm78_data *data = dev_get_drvdata(dev);
287 	long val;
288 	int err;
289 
290 	err = kstrtol(buf, 10, &val);
291 	if (err)
292 		return err;
293 
294 	mutex_lock(&data->update_lock);
295 	data->temp_hyst = TEMP_TO_REG(val);
296 	lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
297 	mutex_unlock(&data->update_lock);
298 	return count;
299 }
300 
301 static DEVICE_ATTR_RO(temp1_input);
302 static DEVICE_ATTR_RW(temp1_max);
303 static DEVICE_ATTR_RW(temp1_max_hyst);
304 
305 /* 3 Fans */
306 static ssize_t fan_show(struct device *dev, struct device_attribute *da,
307 			char *buf)
308 {
309 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
310 	struct lm78_data *data = lm78_update_device(dev);
311 	int nr = attr->index;
312 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
313 		DIV_FROM_REG(data->fan_div[nr])));
314 }
315 
316 static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
317 			    char *buf)
318 {
319 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
320 	struct lm78_data *data = lm78_update_device(dev);
321 	int nr = attr->index;
322 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
323 		DIV_FROM_REG(data->fan_div[nr])));
324 }
325 
326 static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
327 			     const char *buf, size_t count)
328 {
329 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
330 	struct lm78_data *data = dev_get_drvdata(dev);
331 	int nr = attr->index;
332 	unsigned long val;
333 	int err;
334 
335 	err = kstrtoul(buf, 10, &val);
336 	if (err)
337 		return err;
338 
339 	mutex_lock(&data->update_lock);
340 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
341 	lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
342 	mutex_unlock(&data->update_lock);
343 	return count;
344 }
345 
346 static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
347 			    char *buf)
348 {
349 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
350 	struct lm78_data *data = lm78_update_device(dev);
351 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
352 }
353 
354 /*
355  * Note: we save and restore the fan minimum here, because its value is
356  * determined in part by the fan divisor.  This follows the principle of
357  * least surprise; the user doesn't expect the fan minimum to change just
358  * because the divisor changed.
359  */
360 static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
361 			     const char *buf, size_t count)
362 {
363 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
364 	struct lm78_data *data = dev_get_drvdata(dev);
365 	int nr = attr->index;
366 	unsigned long min;
367 	u8 reg;
368 	unsigned long val;
369 	int err;
370 
371 	err = kstrtoul(buf, 10, &val);
372 	if (err)
373 		return err;
374 
375 	mutex_lock(&data->update_lock);
376 	min = FAN_FROM_REG(data->fan_min[nr],
377 			   DIV_FROM_REG(data->fan_div[nr]));
378 
379 	switch (val) {
380 	case 1:
381 		data->fan_div[nr] = 0;
382 		break;
383 	case 2:
384 		data->fan_div[nr] = 1;
385 		break;
386 	case 4:
387 		data->fan_div[nr] = 2;
388 		break;
389 	case 8:
390 		data->fan_div[nr] = 3;
391 		break;
392 	default:
393 		dev_err(dev,
394 			"fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
395 			val);
396 		mutex_unlock(&data->update_lock);
397 		return -EINVAL;
398 	}
399 
400 	reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
401 	switch (nr) {
402 	case 0:
403 		reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
404 		break;
405 	case 1:
406 		reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
407 		break;
408 	}
409 	lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
410 
411 	data->fan_min[nr] =
412 		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
413 	lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
414 	mutex_unlock(&data->update_lock);
415 
416 	return count;
417 }
418 
419 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
420 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
421 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
422 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
423 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
424 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
425 
426 /* Fan 3 divisor is locked in H/W */
427 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
428 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
429 static SENSOR_DEVICE_ATTR_RO(fan3_div, fan_div, 2);
430 
431 /* VID */
432 static ssize_t cpu0_vid_show(struct device *dev, struct device_attribute *da,
433 			     char *buf)
434 {
435 	struct lm78_data *data = lm78_update_device(dev);
436 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
437 }
438 static DEVICE_ATTR_RO(cpu0_vid);
439 
440 /* Alarms */
441 static ssize_t alarms_show(struct device *dev, struct device_attribute *da,
442 			   char *buf)
443 {
444 	struct lm78_data *data = lm78_update_device(dev);
445 	return sprintf(buf, "%u\n", data->alarms);
446 }
447 static DEVICE_ATTR_RO(alarms);
448 
449 static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
450 			  char *buf)
451 {
452 	struct lm78_data *data = lm78_update_device(dev);
453 	int nr = to_sensor_dev_attr(da)->index;
454 	return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
455 }
456 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
457 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
458 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
459 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
460 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
461 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
462 static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 10);
463 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
464 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
465 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 11);
466 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
467 
468 static struct attribute *lm78_attrs[] = {
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,
472 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
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,
476 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
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,
480 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
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,
484 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
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,
488 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
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,
492 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
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,
496 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
497 	&dev_attr_temp1_input.attr,
498 	&dev_attr_temp1_max.attr,
499 	&dev_attr_temp1_max_hyst.attr,
500 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
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,
504 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
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,
508 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
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,
512 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
513 	&dev_attr_alarms.attr,
514 	&dev_attr_cpu0_vid.attr,
515 
516 	NULL
517 };
518 
519 ATTRIBUTE_GROUPS(lm78);
520 
521 /*
522  * ISA related code
523  */
524 #ifdef CONFIG_ISA
525 
526 /* ISA device, if found */
527 static struct platform_device *pdev;
528 
529 static unsigned short isa_address = 0x290;
530 
531 static struct lm78_data *lm78_data_if_isa(void)
532 {
533 	return pdev ? platform_get_drvdata(pdev) : NULL;
534 }
535 
536 /* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
537 static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
538 {
539 	struct lm78_data *isa;
540 	int i;
541 
542 	if (!pdev)	/* No ISA chip */
543 		return 0;
544 	isa = platform_get_drvdata(pdev);
545 
546 	if (lm78_read_value(isa, LM78_REG_I2C_ADDR) != client->addr)
547 		return 0;	/* Address doesn't match */
548 	if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe))
549 		return 0;	/* Chip type doesn't match */
550 
551 	/*
552 	 * We compare all the limit registers, the config register and the
553 	 * interrupt mask registers
554 	 */
555 	for (i = 0x2b; i <= 0x3d; i++) {
556 		if (lm78_read_value(isa, i) !=
557 		    i2c_smbus_read_byte_data(client, i))
558 			return 0;
559 	}
560 	if (lm78_read_value(isa, LM78_REG_CONFIG) !=
561 	    i2c_smbus_read_byte_data(client, LM78_REG_CONFIG))
562 		return 0;
563 	for (i = 0x43; i <= 0x46; i++) {
564 		if (lm78_read_value(isa, i) !=
565 		    i2c_smbus_read_byte_data(client, i))
566 			return 0;
567 	}
568 
569 	return 1;
570 }
571 #else /* !CONFIG_ISA */
572 
573 static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
574 {
575 	return 0;
576 }
577 
578 static struct lm78_data *lm78_data_if_isa(void)
579 {
580 	return NULL;
581 }
582 #endif /* CONFIG_ISA */
583 
584 static int lm78_i2c_detect(struct i2c_client *client,
585 			   struct i2c_board_info *info)
586 {
587 	int i;
588 	struct lm78_data *isa = lm78_data_if_isa();
589 	const char *client_name;
590 	struct i2c_adapter *adapter = client->adapter;
591 	int address = client->addr;
592 
593 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
594 		return -ENODEV;
595 
596 	/*
597 	 * We block updates of the ISA device to minimize the risk of
598 	 * concurrent access to the same LM78 chip through different
599 	 * interfaces.
600 	 */
601 	if (isa)
602 		mutex_lock(&isa->update_lock);
603 
604 	if ((i2c_smbus_read_byte_data(client, LM78_REG_CONFIG) & 0x80)
605 	 || i2c_smbus_read_byte_data(client, LM78_REG_I2C_ADDR) != address)
606 		goto err_nodev;
607 
608 	/* Explicitly prevent the misdetection of Winbond chips */
609 	i = i2c_smbus_read_byte_data(client, 0x4f);
610 	if (i == 0xa3 || i == 0x5c)
611 		goto err_nodev;
612 
613 	/* Determine the chip type. */
614 	i = i2c_smbus_read_byte_data(client, LM78_REG_CHIPID);
615 	if (i == 0x00 || i == 0x20	/* LM78 */
616 	 || i == 0x40)			/* LM78-J */
617 		client_name = "lm78";
618 	else if ((i & 0xfe) == 0xc0)
619 		client_name = "lm79";
620 	else
621 		goto err_nodev;
622 
623 	if (lm78_alias_detect(client, i)) {
624 		dev_dbg(&adapter->dev,
625 			"Device at 0x%02x appears to be the same as ISA device\n",
626 			address);
627 		goto err_nodev;
628 	}
629 
630 	if (isa)
631 		mutex_unlock(&isa->update_lock);
632 
633 	strlcpy(info->type, client_name, I2C_NAME_SIZE);
634 
635 	return 0;
636 
637  err_nodev:
638 	if (isa)
639 		mutex_unlock(&isa->update_lock);
640 	return -ENODEV;
641 }
642 
643 static int lm78_i2c_probe(struct i2c_client *client,
644 			  const struct i2c_device_id *id)
645 {
646 	struct device *dev = &client->dev;
647 	struct device *hwmon_dev;
648 	struct lm78_data *data;
649 
650 	data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
651 	if (!data)
652 		return -ENOMEM;
653 
654 	data->client = client;
655 	data->type = id->driver_data;
656 
657 	/* Initialize the LM78 chip */
658 	lm78_init_device(data);
659 
660 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
661 							   data, lm78_groups);
662 	return PTR_ERR_OR_ZERO(hwmon_dev);
663 }
664 
665 static const struct i2c_device_id lm78_i2c_id[] = {
666 	{ "lm78", lm78 },
667 	{ "lm79", lm79 },
668 	{ }
669 };
670 MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
671 
672 static struct i2c_driver lm78_driver = {
673 	.class		= I2C_CLASS_HWMON,
674 	.driver = {
675 		.name	= "lm78",
676 	},
677 	.probe		= lm78_i2c_probe,
678 	.id_table	= lm78_i2c_id,
679 	.detect		= lm78_i2c_detect,
680 	.address_list	= normal_i2c,
681 };
682 
683 /*
684  * The SMBus locks itself, but ISA access must be locked explicitly!
685  * We don't want to lock the whole ISA bus, so we lock each client
686  * separately.
687  * We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
688  * would slow down the LM78 access and should not be necessary.
689  */
690 static int lm78_read_value(struct lm78_data *data, u8 reg)
691 {
692 	struct i2c_client *client = data->client;
693 
694 #ifdef CONFIG_ISA
695 	if (!client) { /* ISA device */
696 		int res;
697 		mutex_lock(&data->lock);
698 		outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
699 		res = inb_p(data->isa_addr + LM78_DATA_REG_OFFSET);
700 		mutex_unlock(&data->lock);
701 		return res;
702 	} else
703 #endif
704 		return i2c_smbus_read_byte_data(client, reg);
705 }
706 
707 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
708 {
709 	struct i2c_client *client = data->client;
710 
711 #ifdef CONFIG_ISA
712 	if (!client) { /* ISA device */
713 		mutex_lock(&data->lock);
714 		outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
715 		outb_p(value, data->isa_addr + LM78_DATA_REG_OFFSET);
716 		mutex_unlock(&data->lock);
717 		return 0;
718 	} else
719 #endif
720 		return i2c_smbus_write_byte_data(client, reg, value);
721 }
722 
723 static void lm78_init_device(struct lm78_data *data)
724 {
725 	u8 config;
726 	int i;
727 
728 	/* Start monitoring */
729 	config = lm78_read_value(data, LM78_REG_CONFIG);
730 	if ((config & 0x09) != 0x01)
731 		lm78_write_value(data, LM78_REG_CONFIG,
732 				 (config & 0xf7) | 0x01);
733 
734 	/* A few vars need to be filled upon startup */
735 	for (i = 0; i < 3; i++) {
736 		data->fan_min[i] = lm78_read_value(data,
737 					LM78_REG_FAN_MIN(i));
738 	}
739 
740 	mutex_init(&data->update_lock);
741 }
742 
743 static struct lm78_data *lm78_update_device(struct device *dev)
744 {
745 	struct lm78_data *data = dev_get_drvdata(dev);
746 	int i;
747 
748 	mutex_lock(&data->update_lock);
749 
750 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
751 	    || !data->valid) {
752 
753 		dev_dbg(dev, "Starting lm78 update\n");
754 
755 		for (i = 0; i <= 6; i++) {
756 			data->in[i] =
757 			    lm78_read_value(data, LM78_REG_IN(i));
758 			data->in_min[i] =
759 			    lm78_read_value(data, LM78_REG_IN_MIN(i));
760 			data->in_max[i] =
761 			    lm78_read_value(data, LM78_REG_IN_MAX(i));
762 		}
763 		for (i = 0; i < 3; i++) {
764 			data->fan[i] =
765 			    lm78_read_value(data, LM78_REG_FAN(i));
766 			data->fan_min[i] =
767 			    lm78_read_value(data, LM78_REG_FAN_MIN(i));
768 		}
769 		data->temp = lm78_read_value(data, LM78_REG_TEMP);
770 		data->temp_over =
771 		    lm78_read_value(data, LM78_REG_TEMP_OVER);
772 		data->temp_hyst =
773 		    lm78_read_value(data, LM78_REG_TEMP_HYST);
774 		i = lm78_read_value(data, LM78_REG_VID_FANDIV);
775 		data->vid = i & 0x0f;
776 		if (data->type == lm79)
777 			data->vid |=
778 			    (lm78_read_value(data, LM78_REG_CHIPID) &
779 			     0x01) << 4;
780 		else
781 			data->vid |= 0x10;
782 		data->fan_div[0] = (i >> 4) & 0x03;
783 		data->fan_div[1] = i >> 6;
784 		data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
785 		    (lm78_read_value(data, LM78_REG_ALARM2) << 8);
786 		data->last_updated = jiffies;
787 		data->valid = 1;
788 
789 		data->fan_div[2] = 1;
790 	}
791 
792 	mutex_unlock(&data->update_lock);
793 
794 	return data;
795 }
796 
797 #ifdef CONFIG_ISA
798 static int lm78_isa_probe(struct platform_device *pdev)
799 {
800 	struct device *dev = &pdev->dev;
801 	struct device *hwmon_dev;
802 	struct lm78_data *data;
803 	struct resource *res;
804 
805 	/* Reserve the ISA region */
806 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
807 	if (!devm_request_region(dev, res->start + LM78_ADDR_REG_OFFSET,
808 				 2, "lm78"))
809 		return -EBUSY;
810 
811 	data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
812 	if (!data)
813 		return -ENOMEM;
814 
815 	mutex_init(&data->lock);
816 	data->isa_addr = res->start;
817 	platform_set_drvdata(pdev, data);
818 
819 	if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
820 		data->type = lm79;
821 		data->name = "lm79";
822 	} else {
823 		data->type = lm78;
824 		data->name = "lm78";
825 	}
826 
827 	/* Initialize the LM78 chip */
828 	lm78_init_device(data);
829 
830 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
831 							   data, lm78_groups);
832 	return PTR_ERR_OR_ZERO(hwmon_dev);
833 }
834 
835 static struct platform_driver lm78_isa_driver = {
836 	.driver = {
837 		.name	= "lm78",
838 	},
839 	.probe		= lm78_isa_probe,
840 };
841 
842 /* return 1 if a supported chip is found, 0 otherwise */
843 static int __init lm78_isa_found(unsigned short address)
844 {
845 	int val, save, found = 0;
846 	int port;
847 
848 	/*
849 	 * Some boards declare base+0 to base+7 as a PNP device, some base+4
850 	 * to base+7 and some base+5 to base+6. So we better request each port
851 	 * individually for the probing phase.
852 	 */
853 	for (port = address; port < address + LM78_EXTENT; port++) {
854 		if (!request_region(port, 1, "lm78")) {
855 			pr_debug("Failed to request port 0x%x\n", port);
856 			goto release;
857 		}
858 	}
859 
860 #define REALLY_SLOW_IO
861 	/*
862 	 * We need the timeouts for at least some LM78-like
863 	 * chips. But only if we read 'undefined' registers.
864 	 */
865 	val = inb_p(address + 1);
866 	if (inb_p(address + 2) != val
867 	 || inb_p(address + 3) != val
868 	 || inb_p(address + 7) != val)
869 		goto release;
870 #undef REALLY_SLOW_IO
871 
872 	/*
873 	 * We should be able to change the 7 LSB of the address port. The
874 	 * MSB (busy flag) should be clear initially, set after the write.
875 	 */
876 	save = inb_p(address + LM78_ADDR_REG_OFFSET);
877 	if (save & 0x80)
878 		goto release;
879 	val = ~save & 0x7f;
880 	outb_p(val, address + LM78_ADDR_REG_OFFSET);
881 	if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
882 		outb_p(save, address + LM78_ADDR_REG_OFFSET);
883 		goto release;
884 	}
885 
886 	/* We found a device, now see if it could be an LM78 */
887 	outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
888 	val = inb_p(address + LM78_DATA_REG_OFFSET);
889 	if (val & 0x80)
890 		goto release;
891 	outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
892 	val = inb_p(address + LM78_DATA_REG_OFFSET);
893 	if (val < 0x03 || val > 0x77)	/* Not a valid I2C address */
894 		goto release;
895 
896 	/* The busy flag should be clear again */
897 	if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
898 		goto release;
899 
900 	/* Explicitly prevent the misdetection of Winbond chips */
901 	outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
902 	val = inb_p(address + LM78_DATA_REG_OFFSET);
903 	if (val == 0xa3 || val == 0x5c)
904 		goto release;
905 
906 	/* Explicitly prevent the misdetection of ITE chips */
907 	outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
908 	val = inb_p(address + LM78_DATA_REG_OFFSET);
909 	if (val == 0x90)
910 		goto release;
911 
912 	/* Determine the chip type */
913 	outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
914 	val = inb_p(address + LM78_DATA_REG_OFFSET);
915 	if (val == 0x00 || val == 0x20	/* LM78 */
916 	 || val == 0x40			/* LM78-J */
917 	 || (val & 0xfe) == 0xc0)	/* LM79 */
918 		found = 1;
919 
920 	if (found)
921 		pr_info("Found an %s chip at %#x\n",
922 			val & 0x80 ? "LM79" : "LM78", (int)address);
923 
924  release:
925 	for (port--; port >= address; port--)
926 		release_region(port, 1);
927 	return found;
928 }
929 
930 static int __init lm78_isa_device_add(unsigned short address)
931 {
932 	struct resource res = {
933 		.start	= address,
934 		.end	= address + LM78_EXTENT - 1,
935 		.name	= "lm78",
936 		.flags	= IORESOURCE_IO,
937 	};
938 	int err;
939 
940 	pdev = platform_device_alloc("lm78", address);
941 	if (!pdev) {
942 		err = -ENOMEM;
943 		pr_err("Device allocation failed\n");
944 		goto exit;
945 	}
946 
947 	err = platform_device_add_resources(pdev, &res, 1);
948 	if (err) {
949 		pr_err("Device resource addition failed (%d)\n", err);
950 		goto exit_device_put;
951 	}
952 
953 	err = platform_device_add(pdev);
954 	if (err) {
955 		pr_err("Device addition failed (%d)\n", err);
956 		goto exit_device_put;
957 	}
958 
959 	return 0;
960 
961  exit_device_put:
962 	platform_device_put(pdev);
963  exit:
964 	pdev = NULL;
965 	return err;
966 }
967 
968 static int __init lm78_isa_register(void)
969 {
970 	int res;
971 
972 	if (lm78_isa_found(isa_address)) {
973 		res = platform_driver_register(&lm78_isa_driver);
974 		if (res)
975 			goto exit;
976 
977 		/* Sets global pdev as a side effect */
978 		res = lm78_isa_device_add(isa_address);
979 		if (res)
980 			goto exit_unreg_isa_driver;
981 	}
982 
983 	return 0;
984 
985  exit_unreg_isa_driver:
986 	platform_driver_unregister(&lm78_isa_driver);
987  exit:
988 	return res;
989 }
990 
991 static void lm78_isa_unregister(void)
992 {
993 	if (pdev) {
994 		platform_device_unregister(pdev);
995 		platform_driver_unregister(&lm78_isa_driver);
996 	}
997 }
998 #else /* !CONFIG_ISA */
999 
1000 static int __init lm78_isa_register(void)
1001 {
1002 	return 0;
1003 }
1004 
1005 static void lm78_isa_unregister(void)
1006 {
1007 }
1008 #endif /* CONFIG_ISA */
1009 
1010 static int __init sm_lm78_init(void)
1011 {
1012 	int res;
1013 
1014 	/*
1015 	 * We register the ISA device first, so that we can skip the
1016 	 * registration of an I2C interface to the same device.
1017 	 */
1018 	res = lm78_isa_register();
1019 	if (res)
1020 		goto exit;
1021 
1022 	res = i2c_add_driver(&lm78_driver);
1023 	if (res)
1024 		goto exit_unreg_isa_device;
1025 
1026 	return 0;
1027 
1028  exit_unreg_isa_device:
1029 	lm78_isa_unregister();
1030  exit:
1031 	return res;
1032 }
1033 
1034 static void __exit sm_lm78_exit(void)
1035 {
1036 	lm78_isa_unregister();
1037 	i2c_del_driver(&lm78_driver);
1038 }
1039 
1040 MODULE_AUTHOR("Frodo Looijaard, Jean Delvare <jdelvare@suse.de>");
1041 MODULE_DESCRIPTION("LM78/LM79 driver");
1042 MODULE_LICENSE("GPL");
1043 
1044 module_init(sm_lm78_init);
1045 module_exit(sm_lm78_exit);
1046