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