xref: /openbmc/linux/drivers/hwmon/sch5636.c (revision 3381df09)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3  *   Copyright (C) 2011-2012 Hans de Goede <hdegoede@redhat.com>           *
4  *                                                                         *
5  ***************************************************************************/
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/jiffies.h>
13 #include <linux/platform_device.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include "sch56xx-common.h"
19 
20 #define DRVNAME "sch5636"
21 #define DEVNAME "theseus" /* We only support one model for now */
22 
23 #define SCH5636_REG_FUJITSU_ID		0x780
24 #define SCH5636_REG_FUJITSU_REV		0x783
25 
26 #define SCH5636_NO_INS			5
27 #define SCH5636_NO_TEMPS		16
28 #define SCH5636_NO_FANS			8
29 
30 static const u16 SCH5636_REG_IN_VAL[SCH5636_NO_INS] = {
31 	0x22, 0x23, 0x24, 0x25, 0x189 };
32 static const u16 SCH5636_REG_IN_FACTORS[SCH5636_NO_INS] = {
33 	4400, 1500, 4000, 4400, 16000 };
34 static const char * const SCH5636_IN_LABELS[SCH5636_NO_INS] = {
35 	"3.3V", "VREF", "VBAT", "3.3AUX", "12V" };
36 
37 static const u16 SCH5636_REG_TEMP_VAL[SCH5636_NO_TEMPS] = {
38 	0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181,
39 	0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C };
40 #define SCH5636_REG_TEMP_CTRL(i)	(0x790 + (i))
41 #define SCH5636_TEMP_WORKING		0x01
42 #define SCH5636_TEMP_ALARM		0x02
43 #define SCH5636_TEMP_DEACTIVATED	0x80
44 
45 static const u16 SCH5636_REG_FAN_VAL[SCH5636_NO_FANS] = {
46 	0x2C, 0x2E, 0x30, 0x32, 0x62, 0x64, 0x66, 0x68 };
47 #define SCH5636_REG_FAN_CTRL(i)		(0x880 + (i))
48 /* FAULT in datasheet, but acts as an alarm */
49 #define SCH5636_FAN_ALARM		0x04
50 #define SCH5636_FAN_NOT_PRESENT		0x08
51 #define SCH5636_FAN_DEACTIVATED		0x80
52 
53 
54 struct sch5636_data {
55 	unsigned short addr;
56 	struct device *hwmon_dev;
57 	struct sch56xx_watchdog_data *watchdog;
58 
59 	struct mutex update_lock;
60 	char valid;			/* !=0 if following fields are valid */
61 	unsigned long last_updated;	/* In jiffies */
62 	u8 in[SCH5636_NO_INS];
63 	u8 temp_val[SCH5636_NO_TEMPS];
64 	u8 temp_ctrl[SCH5636_NO_TEMPS];
65 	u16 fan_val[SCH5636_NO_FANS];
66 	u8 fan_ctrl[SCH5636_NO_FANS];
67 };
68 
69 static struct sch5636_data *sch5636_update_device(struct device *dev)
70 {
71 	struct sch5636_data *data = dev_get_drvdata(dev);
72 	struct sch5636_data *ret = data;
73 	int i, val;
74 
75 	mutex_lock(&data->update_lock);
76 
77 	/* Cache the values for 1 second */
78 	if (data->valid && !time_after(jiffies, data->last_updated + HZ))
79 		goto abort;
80 
81 	for (i = 0; i < SCH5636_NO_INS; i++) {
82 		val = sch56xx_read_virtual_reg(data->addr,
83 					       SCH5636_REG_IN_VAL[i]);
84 		if (unlikely(val < 0)) {
85 			ret = ERR_PTR(val);
86 			goto abort;
87 		}
88 		data->in[i] = val;
89 	}
90 
91 	for (i = 0; i < SCH5636_NO_TEMPS; i++) {
92 		if (data->temp_ctrl[i] & SCH5636_TEMP_DEACTIVATED)
93 			continue;
94 
95 		val = sch56xx_read_virtual_reg(data->addr,
96 					       SCH5636_REG_TEMP_VAL[i]);
97 		if (unlikely(val < 0)) {
98 			ret = ERR_PTR(val);
99 			goto abort;
100 		}
101 		data->temp_val[i] = val;
102 
103 		val = sch56xx_read_virtual_reg(data->addr,
104 					       SCH5636_REG_TEMP_CTRL(i));
105 		if (unlikely(val < 0)) {
106 			ret = ERR_PTR(val);
107 			goto abort;
108 		}
109 		data->temp_ctrl[i] = val;
110 		/* Alarms need to be explicitly write-cleared */
111 		if (val & SCH5636_TEMP_ALARM) {
112 			sch56xx_write_virtual_reg(data->addr,
113 						SCH5636_REG_TEMP_CTRL(i), val);
114 		}
115 	}
116 
117 	for (i = 0; i < SCH5636_NO_FANS; i++) {
118 		if (data->fan_ctrl[i] & SCH5636_FAN_DEACTIVATED)
119 			continue;
120 
121 		val = sch56xx_read_virtual_reg16(data->addr,
122 						 SCH5636_REG_FAN_VAL[i]);
123 		if (unlikely(val < 0)) {
124 			ret = ERR_PTR(val);
125 			goto abort;
126 		}
127 		data->fan_val[i] = val;
128 
129 		val = sch56xx_read_virtual_reg(data->addr,
130 					       SCH5636_REG_FAN_CTRL(i));
131 		if (unlikely(val < 0)) {
132 			ret = ERR_PTR(val);
133 			goto abort;
134 		}
135 		data->fan_ctrl[i] = val;
136 		/* Alarms need to be explicitly write-cleared */
137 		if (val & SCH5636_FAN_ALARM) {
138 			sch56xx_write_virtual_reg(data->addr,
139 						SCH5636_REG_FAN_CTRL(i), val);
140 		}
141 	}
142 
143 	data->last_updated = jiffies;
144 	data->valid = 1;
145 abort:
146 	mutex_unlock(&data->update_lock);
147 	return ret;
148 }
149 
150 static int reg_to_rpm(u16 reg)
151 {
152 	if (reg == 0)
153 		return -EIO;
154 	if (reg == 0xffff)
155 		return 0;
156 
157 	return 5400540 / reg;
158 }
159 
160 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
161 			 char *buf)
162 {
163 	return snprintf(buf, PAGE_SIZE, "%s\n", DEVNAME);
164 }
165 
166 static ssize_t in_value_show(struct device *dev,
167 			     struct device_attribute *devattr, char *buf)
168 {
169 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
170 	struct sch5636_data *data = sch5636_update_device(dev);
171 	int val;
172 
173 	if (IS_ERR(data))
174 		return PTR_ERR(data);
175 
176 	val = DIV_ROUND_CLOSEST(
177 		data->in[attr->index] * SCH5636_REG_IN_FACTORS[attr->index],
178 		255);
179 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
180 }
181 
182 static ssize_t in_label_show(struct device *dev,
183 			     struct device_attribute *devattr, char *buf)
184 {
185 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
186 
187 	return snprintf(buf, PAGE_SIZE, "%s\n",
188 			SCH5636_IN_LABELS[attr->index]);
189 }
190 
191 static ssize_t temp_value_show(struct device *dev,
192 			       struct device_attribute *devattr, char *buf)
193 {
194 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
195 	struct sch5636_data *data = sch5636_update_device(dev);
196 	int val;
197 
198 	if (IS_ERR(data))
199 		return PTR_ERR(data);
200 
201 	val = (data->temp_val[attr->index] - 64) * 1000;
202 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
203 }
204 
205 static ssize_t temp_fault_show(struct device *dev,
206 			       struct device_attribute *devattr, char *buf)
207 {
208 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
209 	struct sch5636_data *data = sch5636_update_device(dev);
210 	int val;
211 
212 	if (IS_ERR(data))
213 		return PTR_ERR(data);
214 
215 	val = (data->temp_ctrl[attr->index] & SCH5636_TEMP_WORKING) ? 0 : 1;
216 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
217 }
218 
219 static ssize_t temp_alarm_show(struct device *dev,
220 			       struct device_attribute *devattr, char *buf)
221 {
222 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
223 	struct sch5636_data *data = sch5636_update_device(dev);
224 	int val;
225 
226 	if (IS_ERR(data))
227 		return PTR_ERR(data);
228 
229 	val = (data->temp_ctrl[attr->index] & SCH5636_TEMP_ALARM) ? 1 : 0;
230 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
231 }
232 
233 static ssize_t fan_value_show(struct device *dev,
234 			      struct device_attribute *devattr, char *buf)
235 {
236 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
237 	struct sch5636_data *data = sch5636_update_device(dev);
238 	int val;
239 
240 	if (IS_ERR(data))
241 		return PTR_ERR(data);
242 
243 	val = reg_to_rpm(data->fan_val[attr->index]);
244 	if (val < 0)
245 		return val;
246 
247 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
248 }
249 
250 static ssize_t fan_fault_show(struct device *dev,
251 			      struct device_attribute *devattr, char *buf)
252 {
253 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
254 	struct sch5636_data *data = sch5636_update_device(dev);
255 	int val;
256 
257 	if (IS_ERR(data))
258 		return PTR_ERR(data);
259 
260 	val = (data->fan_ctrl[attr->index] & SCH5636_FAN_NOT_PRESENT) ? 1 : 0;
261 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
262 }
263 
264 static ssize_t fan_alarm_show(struct device *dev,
265 			      struct device_attribute *devattr, char *buf)
266 {
267 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
268 	struct sch5636_data *data = sch5636_update_device(dev);
269 	int val;
270 
271 	if (IS_ERR(data))
272 		return PTR_ERR(data);
273 
274 	val = (data->fan_ctrl[attr->index] & SCH5636_FAN_ALARM) ? 1 : 0;
275 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
276 }
277 
278 static struct sensor_device_attribute sch5636_attr[] = {
279 	SENSOR_ATTR_RO(name, name, 0),
280 	SENSOR_ATTR_RO(in0_input, in_value, 0),
281 	SENSOR_ATTR_RO(in0_label, in_label, 0),
282 	SENSOR_ATTR_RO(in1_input, in_value, 1),
283 	SENSOR_ATTR_RO(in1_label, in_label, 1),
284 	SENSOR_ATTR_RO(in2_input, in_value, 2),
285 	SENSOR_ATTR_RO(in2_label, in_label, 2),
286 	SENSOR_ATTR_RO(in3_input, in_value, 3),
287 	SENSOR_ATTR_RO(in3_label, in_label, 3),
288 	SENSOR_ATTR_RO(in4_input, in_value, 4),
289 	SENSOR_ATTR_RO(in4_label, in_label, 4),
290 };
291 
292 static struct sensor_device_attribute sch5636_temp_attr[] = {
293 	SENSOR_ATTR_RO(temp1_input, temp_value, 0),
294 	SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
295 	SENSOR_ATTR_RO(temp1_alarm, temp_alarm, 0),
296 	SENSOR_ATTR_RO(temp2_input, temp_value, 1),
297 	SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
298 	SENSOR_ATTR_RO(temp2_alarm, temp_alarm, 1),
299 	SENSOR_ATTR_RO(temp3_input, temp_value, 2),
300 	SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
301 	SENSOR_ATTR_RO(temp3_alarm, temp_alarm, 2),
302 	SENSOR_ATTR_RO(temp4_input, temp_value, 3),
303 	SENSOR_ATTR_RO(temp4_fault, temp_fault, 3),
304 	SENSOR_ATTR_RO(temp4_alarm, temp_alarm, 3),
305 	SENSOR_ATTR_RO(temp5_input, temp_value, 4),
306 	SENSOR_ATTR_RO(temp5_fault, temp_fault, 4),
307 	SENSOR_ATTR_RO(temp5_alarm, temp_alarm, 4),
308 	SENSOR_ATTR_RO(temp6_input, temp_value, 5),
309 	SENSOR_ATTR_RO(temp6_fault, temp_fault, 5),
310 	SENSOR_ATTR_RO(temp6_alarm, temp_alarm, 5),
311 	SENSOR_ATTR_RO(temp7_input, temp_value, 6),
312 	SENSOR_ATTR_RO(temp7_fault, temp_fault, 6),
313 	SENSOR_ATTR_RO(temp7_alarm, temp_alarm, 6),
314 	SENSOR_ATTR_RO(temp8_input, temp_value, 7),
315 	SENSOR_ATTR_RO(temp8_fault, temp_fault, 7),
316 	SENSOR_ATTR_RO(temp8_alarm, temp_alarm, 7),
317 	SENSOR_ATTR_RO(temp9_input, temp_value, 8),
318 	SENSOR_ATTR_RO(temp9_fault, temp_fault, 8),
319 	SENSOR_ATTR_RO(temp9_alarm, temp_alarm, 8),
320 	SENSOR_ATTR_RO(temp10_input, temp_value, 9),
321 	SENSOR_ATTR_RO(temp10_fault, temp_fault, 9),
322 	SENSOR_ATTR_RO(temp10_alarm, temp_alarm, 9),
323 	SENSOR_ATTR_RO(temp11_input, temp_value, 10),
324 	SENSOR_ATTR_RO(temp11_fault, temp_fault, 10),
325 	SENSOR_ATTR_RO(temp11_alarm, temp_alarm, 10),
326 	SENSOR_ATTR_RO(temp12_input, temp_value, 11),
327 	SENSOR_ATTR_RO(temp12_fault, temp_fault, 11),
328 	SENSOR_ATTR_RO(temp12_alarm, temp_alarm, 11),
329 	SENSOR_ATTR_RO(temp13_input, temp_value, 12),
330 	SENSOR_ATTR_RO(temp13_fault, temp_fault, 12),
331 	SENSOR_ATTR_RO(temp13_alarm, temp_alarm, 12),
332 	SENSOR_ATTR_RO(temp14_input, temp_value, 13),
333 	SENSOR_ATTR_RO(temp14_fault, temp_fault, 13),
334 	SENSOR_ATTR_RO(temp14_alarm, temp_alarm, 13),
335 	SENSOR_ATTR_RO(temp15_input, temp_value, 14),
336 	SENSOR_ATTR_RO(temp15_fault, temp_fault, 14),
337 	SENSOR_ATTR_RO(temp15_alarm, temp_alarm, 14),
338 	SENSOR_ATTR_RO(temp16_input, temp_value, 15),
339 	SENSOR_ATTR_RO(temp16_fault, temp_fault, 15),
340 	SENSOR_ATTR_RO(temp16_alarm, temp_alarm, 15),
341 };
342 
343 static struct sensor_device_attribute sch5636_fan_attr[] = {
344 	SENSOR_ATTR_RO(fan1_input, fan_value, 0),
345 	SENSOR_ATTR_RO(fan1_fault, fan_fault, 0),
346 	SENSOR_ATTR_RO(fan1_alarm, fan_alarm, 0),
347 	SENSOR_ATTR_RO(fan2_input, fan_value, 1),
348 	SENSOR_ATTR_RO(fan2_fault, fan_fault, 1),
349 	SENSOR_ATTR_RO(fan2_alarm, fan_alarm, 1),
350 	SENSOR_ATTR_RO(fan3_input, fan_value, 2),
351 	SENSOR_ATTR_RO(fan3_fault, fan_fault, 2),
352 	SENSOR_ATTR_RO(fan3_alarm, fan_alarm, 2),
353 	SENSOR_ATTR_RO(fan4_input, fan_value, 3),
354 	SENSOR_ATTR_RO(fan4_fault, fan_fault, 3),
355 	SENSOR_ATTR_RO(fan4_alarm, fan_alarm, 3),
356 	SENSOR_ATTR_RO(fan5_input, fan_value, 4),
357 	SENSOR_ATTR_RO(fan5_fault, fan_fault, 4),
358 	SENSOR_ATTR_RO(fan5_alarm, fan_alarm, 4),
359 	SENSOR_ATTR_RO(fan6_input, fan_value, 5),
360 	SENSOR_ATTR_RO(fan6_fault, fan_fault, 5),
361 	SENSOR_ATTR_RO(fan6_alarm, fan_alarm, 5),
362 	SENSOR_ATTR_RO(fan7_input, fan_value, 6),
363 	SENSOR_ATTR_RO(fan7_fault, fan_fault, 6),
364 	SENSOR_ATTR_RO(fan7_alarm, fan_alarm, 6),
365 	SENSOR_ATTR_RO(fan8_input, fan_value, 7),
366 	SENSOR_ATTR_RO(fan8_fault, fan_fault, 7),
367 	SENSOR_ATTR_RO(fan8_alarm, fan_alarm, 7),
368 };
369 
370 static int sch5636_remove(struct platform_device *pdev)
371 {
372 	struct sch5636_data *data = platform_get_drvdata(pdev);
373 	int i;
374 
375 	if (data->watchdog)
376 		sch56xx_watchdog_unregister(data->watchdog);
377 
378 	if (data->hwmon_dev)
379 		hwmon_device_unregister(data->hwmon_dev);
380 
381 	for (i = 0; i < ARRAY_SIZE(sch5636_attr); i++)
382 		device_remove_file(&pdev->dev, &sch5636_attr[i].dev_attr);
383 
384 	for (i = 0; i < SCH5636_NO_TEMPS * 3; i++)
385 		device_remove_file(&pdev->dev,
386 				   &sch5636_temp_attr[i].dev_attr);
387 
388 	for (i = 0; i < SCH5636_NO_FANS * 3; i++)
389 		device_remove_file(&pdev->dev,
390 				   &sch5636_fan_attr[i].dev_attr);
391 
392 	return 0;
393 }
394 
395 static int sch5636_probe(struct platform_device *pdev)
396 {
397 	struct sch5636_data *data;
398 	int i, err, val, revision[2];
399 	char id[4];
400 
401 	data = devm_kzalloc(&pdev->dev, sizeof(struct sch5636_data),
402 			    GFP_KERNEL);
403 	if (!data)
404 		return -ENOMEM;
405 
406 	data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
407 	mutex_init(&data->update_lock);
408 	platform_set_drvdata(pdev, data);
409 
410 	for (i = 0; i < 3; i++) {
411 		val = sch56xx_read_virtual_reg(data->addr,
412 					       SCH5636_REG_FUJITSU_ID + i);
413 		if (val < 0) {
414 			pr_err("Could not read Fujitsu id byte at %#x\n",
415 				SCH5636_REG_FUJITSU_ID + i);
416 			err = val;
417 			goto error;
418 		}
419 		id[i] = val;
420 	}
421 	id[i] = '\0';
422 
423 	if (strcmp(id, "THS")) {
424 		pr_err("Unknown Fujitsu id: %02x%02x%02x\n",
425 		       id[0], id[1], id[2]);
426 		err = -ENODEV;
427 		goto error;
428 	}
429 
430 	for (i = 0; i < 2; i++) {
431 		val = sch56xx_read_virtual_reg(data->addr,
432 					       SCH5636_REG_FUJITSU_REV + i);
433 		if (val < 0) {
434 			err = val;
435 			goto error;
436 		}
437 		revision[i] = val;
438 	}
439 	pr_info("Found %s chip at %#hx, revision: %d.%02d\n", DEVNAME,
440 		data->addr, revision[0], revision[1]);
441 
442 	/* Read all temp + fan ctrl registers to determine which are active */
443 	for (i = 0; i < SCH5636_NO_TEMPS; i++) {
444 		val = sch56xx_read_virtual_reg(data->addr,
445 					       SCH5636_REG_TEMP_CTRL(i));
446 		if (unlikely(val < 0)) {
447 			err = val;
448 			goto error;
449 		}
450 		data->temp_ctrl[i] = val;
451 	}
452 
453 	for (i = 0; i < SCH5636_NO_FANS; i++) {
454 		val = sch56xx_read_virtual_reg(data->addr,
455 					       SCH5636_REG_FAN_CTRL(i));
456 		if (unlikely(val < 0)) {
457 			err = val;
458 			goto error;
459 		}
460 		data->fan_ctrl[i] = val;
461 	}
462 
463 	for (i = 0; i < ARRAY_SIZE(sch5636_attr); i++) {
464 		err = device_create_file(&pdev->dev,
465 					 &sch5636_attr[i].dev_attr);
466 		if (err)
467 			goto error;
468 	}
469 
470 	for (i = 0; i < (SCH5636_NO_TEMPS * 3); i++) {
471 		if (data->temp_ctrl[i/3] & SCH5636_TEMP_DEACTIVATED)
472 			continue;
473 
474 		err = device_create_file(&pdev->dev,
475 					&sch5636_temp_attr[i].dev_attr);
476 		if (err)
477 			goto error;
478 	}
479 
480 	for (i = 0; i < (SCH5636_NO_FANS * 3); i++) {
481 		if (data->fan_ctrl[i/3] & SCH5636_FAN_DEACTIVATED)
482 			continue;
483 
484 		err = device_create_file(&pdev->dev,
485 					&sch5636_fan_attr[i].dev_attr);
486 		if (err)
487 			goto error;
488 	}
489 
490 	data->hwmon_dev = hwmon_device_register(&pdev->dev);
491 	if (IS_ERR(data->hwmon_dev)) {
492 		err = PTR_ERR(data->hwmon_dev);
493 		data->hwmon_dev = NULL;
494 		goto error;
495 	}
496 
497 	/* Note failing to register the watchdog is not a fatal error */
498 	data->watchdog = sch56xx_watchdog_register(&pdev->dev, data->addr,
499 					(revision[0] << 8) | revision[1],
500 					&data->update_lock, 0);
501 
502 	return 0;
503 
504 error:
505 	sch5636_remove(pdev);
506 	return err;
507 }
508 
509 static struct platform_driver sch5636_driver = {
510 	.driver = {
511 		.name	= DRVNAME,
512 	},
513 	.probe		= sch5636_probe,
514 	.remove		= sch5636_remove,
515 };
516 
517 module_platform_driver(sch5636_driver);
518 
519 MODULE_DESCRIPTION("SMSC SCH5636 Hardware Monitoring Driver");
520 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
521 MODULE_LICENSE("GPL");
522