xref: /openbmc/linux/drivers/hwmon/sch5627.c (revision aeb30fa6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3  *   Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>           *
4  *                                                                         *
5  ***************************************************************************/
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/bits.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/jiffies.h>
15 #include <linux/platform_device.h>
16 #include <linux/hwmon.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include "sch56xx-common.h"
20 
21 #define DRVNAME "sch5627"
22 #define DEVNAME DRVNAME /* We only support one model */
23 
24 #define SCH5627_HWMON_ID		0xa5
25 #define SCH5627_COMPANY_ID		0x5c
26 #define SCH5627_PRIMARY_ID		0xa0
27 
28 #define SCH5627_REG_BUILD_CODE		0x39
29 #define SCH5627_REG_BUILD_ID		0x3a
30 #define SCH5627_REG_HWMON_ID		0x3c
31 #define SCH5627_REG_HWMON_REV		0x3d
32 #define SCH5627_REG_COMPANY_ID		0x3e
33 #define SCH5627_REG_PRIMARY_ID		0x3f
34 #define SCH5627_REG_CTRL		0x40
35 
36 #define SCH5627_CTRL_START		BIT(0)
37 #define SCH5627_CTRL_LOCK		BIT(1)
38 #define SCH5627_CTRL_VBAT		BIT(4)
39 
40 #define SCH5627_NO_TEMPS		8
41 #define SCH5627_NO_FANS			4
42 #define SCH5627_NO_IN			5
43 
44 static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = {
45 	0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 };
46 static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = {
47 	0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 };
48 static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = {
49 	0, 0, 1, 1, 0, 0, 0, 1 };
50 static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = {
51 	0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 };
52 static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = {
53 	0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 };
54 
55 static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = {
56 	0x2C, 0x2E, 0x30, 0x32 };
57 static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = {
58 	0x62, 0x64, 0x66, 0x68 };
59 
60 static const u16 SCH5627_REG_PWM_MAP[SCH5627_NO_FANS] = {
61 	0xA0, 0xA1, 0xA2, 0xA3 };
62 
63 static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = {
64 	0x22, 0x23, 0x24, 0x25, 0x189 };
65 static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = {
66 	0xE4, 0xE4, 0xE3, 0xE3, 0x18A };
67 static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = {
68 	1, 0, 1, 0, 1 };
69 static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = {
70 	10745, 3660, 9765, 10745, 3660 };
71 static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = {
72 	"VCC", "VTT", "VBAT", "VTR", "V_IN" };
73 
74 struct sch5627_data {
75 	unsigned short addr;
76 	u8 control;
77 	u8 temp_max[SCH5627_NO_TEMPS];
78 	u8 temp_crit[SCH5627_NO_TEMPS];
79 	u16 fan_min[SCH5627_NO_FANS];
80 
81 	struct mutex update_lock;
82 	unsigned long last_battery;	/* In jiffies */
83 	char temp_valid;		/* !=0 if following fields are valid */
84 	char fan_valid;
85 	char in_valid;
86 	unsigned long temp_last_updated;	/* In jiffies */
87 	unsigned long fan_last_updated;
88 	unsigned long in_last_updated;
89 	u16 temp[SCH5627_NO_TEMPS];
90 	u16 fan[SCH5627_NO_FANS];
91 	u16 in[SCH5627_NO_IN];
92 };
93 
sch5627_update_temp(struct sch5627_data * data)94 static int sch5627_update_temp(struct sch5627_data *data)
95 {
96 	int ret = 0;
97 	int i, val;
98 
99 	mutex_lock(&data->update_lock);
100 
101 	/* Cache the values for 1 second */
102 	if (time_after(jiffies, data->temp_last_updated + HZ) || !data->temp_valid) {
103 		for (i = 0; i < SCH5627_NO_TEMPS; i++) {
104 			val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_TEMP_MSB[i],
105 							 SCH5627_REG_TEMP_LSN[i],
106 							 SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
107 			if (unlikely(val < 0)) {
108 				ret = val;
109 				goto abort;
110 			}
111 			data->temp[i] = val;
112 		}
113 		data->temp_last_updated = jiffies;
114 		data->temp_valid = 1;
115 	}
116 abort:
117 	mutex_unlock(&data->update_lock);
118 	return ret;
119 }
120 
sch5627_update_fan(struct sch5627_data * data)121 static int sch5627_update_fan(struct sch5627_data *data)
122 {
123 	int ret = 0;
124 	int i, val;
125 
126 	mutex_lock(&data->update_lock);
127 
128 	/* Cache the values for 1 second */
129 	if (time_after(jiffies, data->fan_last_updated + HZ) || !data->fan_valid) {
130 		for (i = 0; i < SCH5627_NO_FANS; i++) {
131 			val = sch56xx_read_virtual_reg16(data->addr, SCH5627_REG_FAN[i]);
132 			if (unlikely(val < 0)) {
133 				ret = val;
134 				goto abort;
135 			}
136 			data->fan[i] = val;
137 		}
138 		data->fan_last_updated = jiffies;
139 		data->fan_valid = 1;
140 	}
141 abort:
142 	mutex_unlock(&data->update_lock);
143 	return ret;
144 }
145 
sch5627_update_in(struct sch5627_data * data)146 static int sch5627_update_in(struct sch5627_data *data)
147 {
148 	int ret = 0;
149 	int i, val;
150 
151 	mutex_lock(&data->update_lock);
152 
153 	/* Trigger a Vbat voltage measurement every 5 minutes */
154 	if (time_after(jiffies, data->last_battery + 300 * HZ)) {
155 		sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL,
156 					  data->control | SCH5627_CTRL_VBAT);
157 		data->last_battery = jiffies;
158 	}
159 
160 	/* Cache the values for 1 second */
161 	if (time_after(jiffies, data->in_last_updated + HZ) || !data->in_valid) {
162 		for (i = 0; i < SCH5627_NO_IN; i++) {
163 			val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_IN_MSB[i],
164 							 SCH5627_REG_IN_LSN[i],
165 							 SCH5627_REG_IN_HIGH_NIBBLE[i]);
166 			if (unlikely(val < 0)) {
167 				ret = val;
168 				goto abort;
169 			}
170 			data->in[i] = val;
171 		}
172 		data->in_last_updated = jiffies;
173 		data->in_valid = 1;
174 	}
175 abort:
176 	mutex_unlock(&data->update_lock);
177 	return ret;
178 }
179 
sch5627_read_limits(struct sch5627_data * data)180 static int sch5627_read_limits(struct sch5627_data *data)
181 {
182 	int i, val;
183 
184 	for (i = 0; i < SCH5627_NO_TEMPS; i++) {
185 		/*
186 		 * Note what SMSC calls ABS, is what lm_sensors calls max
187 		 * (aka high), and HIGH is what lm_sensors calls crit.
188 		 */
189 		val = sch56xx_read_virtual_reg(data->addr,
190 					       SCH5627_REG_TEMP_ABS[i]);
191 		if (val < 0)
192 			return val;
193 		data->temp_max[i] = val;
194 
195 		val = sch56xx_read_virtual_reg(data->addr,
196 					       SCH5627_REG_TEMP_HIGH[i]);
197 		if (val < 0)
198 			return val;
199 		data->temp_crit[i] = val;
200 	}
201 	for (i = 0; i < SCH5627_NO_FANS; i++) {
202 		val = sch56xx_read_virtual_reg16(data->addr,
203 						 SCH5627_REG_FAN_MIN[i]);
204 		if (val < 0)
205 			return val;
206 		data->fan_min[i] = val;
207 	}
208 
209 	return 0;
210 }
211 
reg_to_temp(u16 reg)212 static int reg_to_temp(u16 reg)
213 {
214 	return (reg * 625) / 10 - 64000;
215 }
216 
reg_to_temp_limit(u8 reg)217 static int reg_to_temp_limit(u8 reg)
218 {
219 	return (reg - 64) * 1000;
220 }
221 
reg_to_rpm(u16 reg)222 static int reg_to_rpm(u16 reg)
223 {
224 	if (reg == 0)
225 		return -EIO;
226 	if (reg == 0xffff)
227 		return 0;
228 
229 	return 5400540 / reg;
230 }
231 
sch5627_is_visible(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int channel)232 static umode_t sch5627_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr,
233 				  int channel)
234 {
235 	const struct sch5627_data *data = drvdata;
236 
237 	/* Once the lock bit is set, the virtual registers become read-only
238 	 * until the next power cycle.
239 	 */
240 	if (data->control & SCH5627_CTRL_LOCK)
241 		return 0444;
242 
243 	if (type == hwmon_pwm && attr == hwmon_pwm_auto_channels_temp)
244 		return 0644;
245 
246 	return 0444;
247 }
248 
sch5627_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)249 static int sch5627_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
250 			long *val)
251 {
252 	struct sch5627_data *data = dev_get_drvdata(dev);
253 	int ret;
254 
255 	switch (type) {
256 	case hwmon_temp:
257 		ret = sch5627_update_temp(data);
258 		if (ret < 0)
259 			return ret;
260 		switch (attr) {
261 		case hwmon_temp_input:
262 			*val = reg_to_temp(data->temp[channel]);
263 			return 0;
264 		case hwmon_temp_max:
265 			*val = reg_to_temp_limit(data->temp_max[channel]);
266 			return 0;
267 		case hwmon_temp_crit:
268 			*val = reg_to_temp_limit(data->temp_crit[channel]);
269 			return 0;
270 		case hwmon_temp_fault:
271 			*val = (data->temp[channel] == 0);
272 			return 0;
273 		default:
274 			break;
275 		}
276 		break;
277 	case hwmon_fan:
278 		ret = sch5627_update_fan(data);
279 		if (ret < 0)
280 			return ret;
281 		switch (attr) {
282 		case hwmon_fan_input:
283 			ret = reg_to_rpm(data->fan[channel]);
284 			if (ret < 0)
285 				return ret;
286 			*val = ret;
287 			return 0;
288 		case hwmon_fan_min:
289 			ret = reg_to_rpm(data->fan_min[channel]);
290 			if (ret < 0)
291 				return ret;
292 			*val = ret;
293 			return 0;
294 		case hwmon_fan_fault:
295 			*val = (data->fan[channel] == 0xffff);
296 			return 0;
297 		default:
298 			break;
299 		}
300 		break;
301 	case hwmon_pwm:
302 		switch (attr) {
303 		case hwmon_pwm_auto_channels_temp:
304 			mutex_lock(&data->update_lock);
305 			ret = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PWM_MAP[channel]);
306 			mutex_unlock(&data->update_lock);
307 
308 			if (ret < 0)
309 				return ret;
310 
311 			*val = ret;
312 
313 			return 0;
314 		default:
315 			break;
316 		}
317 		break;
318 	case hwmon_in:
319 		ret = sch5627_update_in(data);
320 		if (ret < 0)
321 			return ret;
322 		switch (attr) {
323 		case hwmon_in_input:
324 			*val = DIV_ROUND_CLOSEST(data->in[channel] * SCH5627_REG_IN_FACTOR[channel],
325 						 10000);
326 			return 0;
327 		default:
328 			break;
329 		}
330 		break;
331 	default:
332 		break;
333 	}
334 
335 	return -EOPNOTSUPP;
336 }
337 
sch5627_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)338 static int sch5627_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
339 			       int channel, const char **str)
340 {
341 	switch (type) {
342 	case hwmon_in:
343 		switch (attr) {
344 		case hwmon_in_label:
345 			*str = SCH5627_IN_LABELS[channel];
346 			return 0;
347 		default:
348 			break;
349 		}
350 		break;
351 	default:
352 		break;
353 	}
354 
355 	return -EOPNOTSUPP;
356 }
357 
sch5627_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)358 static int sch5627_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
359 			 long val)
360 {
361 	struct sch5627_data *data = dev_get_drvdata(dev);
362 	int ret;
363 
364 	switch (type) {
365 	case hwmon_pwm:
366 		switch (attr) {
367 		case hwmon_pwm_auto_channels_temp:
368 			/* registers are 8 bit wide */
369 			if (val > U8_MAX || val < 0)
370 				return -EINVAL;
371 
372 			mutex_lock(&data->update_lock);
373 			ret = sch56xx_write_virtual_reg(data->addr, SCH5627_REG_PWM_MAP[channel],
374 							val);
375 			mutex_unlock(&data->update_lock);
376 
377 			return ret;
378 		default:
379 			break;
380 		}
381 		break;
382 	default:
383 		break;
384 	}
385 
386 	return -EOPNOTSUPP;
387 }
388 
389 static const struct hwmon_ops sch5627_ops = {
390 	.is_visible = sch5627_is_visible,
391 	.read = sch5627_read,
392 	.read_string = sch5627_read_string,
393 	.write = sch5627_write,
394 };
395 
396 static const struct hwmon_channel_info * const sch5627_info[] = {
397 	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
398 	HWMON_CHANNEL_INFO(temp,
399 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
400 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
401 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
402 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
403 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
404 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
405 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
406 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT
407 			   ),
408 	HWMON_CHANNEL_INFO(fan,
409 			   HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
410 			   HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
411 			   HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
412 			   HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT
413 			   ),
414 	HWMON_CHANNEL_INFO(pwm,
415 			   HWMON_PWM_AUTO_CHANNELS_TEMP,
416 			   HWMON_PWM_AUTO_CHANNELS_TEMP,
417 			   HWMON_PWM_AUTO_CHANNELS_TEMP,
418 			   HWMON_PWM_AUTO_CHANNELS_TEMP
419 			   ),
420 	HWMON_CHANNEL_INFO(in,
421 			   HWMON_I_INPUT | HWMON_I_LABEL,
422 			   HWMON_I_INPUT | HWMON_I_LABEL,
423 			   HWMON_I_INPUT | HWMON_I_LABEL,
424 			   HWMON_I_INPUT | HWMON_I_LABEL,
425 			   HWMON_I_INPUT
426 			   ),
427 	NULL
428 };
429 
430 static const struct hwmon_chip_info sch5627_chip_info = {
431 	.ops = &sch5627_ops,
432 	.info = sch5627_info,
433 };
434 
sch5627_probe(struct platform_device * pdev)435 static int sch5627_probe(struct platform_device *pdev)
436 {
437 	struct sch5627_data *data;
438 	struct device *hwmon_dev;
439 	int err, build_code, build_id, hwmon_rev, val;
440 
441 	data = devm_kzalloc(&pdev->dev, sizeof(struct sch5627_data),
442 			    GFP_KERNEL);
443 	if (!data)
444 		return -ENOMEM;
445 
446 	data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
447 	mutex_init(&data->update_lock);
448 	platform_set_drvdata(pdev, data);
449 
450 	val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_HWMON_ID);
451 	if (val < 0)
452 		return val;
453 
454 	if (val != SCH5627_HWMON_ID) {
455 		pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon",
456 		       val, SCH5627_HWMON_ID);
457 		return -ENODEV;
458 	}
459 
460 	val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_COMPANY_ID);
461 	if (val < 0)
462 		return val;
463 
464 	if (val != SCH5627_COMPANY_ID) {
465 		pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company",
466 		       val, SCH5627_COMPANY_ID);
467 		return -ENODEV;
468 	}
469 
470 	val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PRIMARY_ID);
471 	if (val < 0)
472 		return val;
473 
474 	if (val != SCH5627_PRIMARY_ID) {
475 		pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary",
476 		       val, SCH5627_PRIMARY_ID);
477 		return -ENODEV;
478 	}
479 
480 	build_code = sch56xx_read_virtual_reg(data->addr,
481 					      SCH5627_REG_BUILD_CODE);
482 	if (build_code < 0)
483 		return build_code;
484 
485 	build_id = sch56xx_read_virtual_reg16(data->addr,
486 					      SCH5627_REG_BUILD_ID);
487 	if (build_id < 0)
488 		return build_id;
489 
490 	hwmon_rev = sch56xx_read_virtual_reg(data->addr,
491 					     SCH5627_REG_HWMON_REV);
492 	if (hwmon_rev < 0)
493 		return hwmon_rev;
494 
495 	val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_CTRL);
496 	if (val < 0)
497 		return val;
498 
499 	data->control = val;
500 	if (!(data->control & SCH5627_CTRL_START)) {
501 		pr_err("hardware monitoring not enabled\n");
502 		return -ENODEV;
503 	}
504 	/* Trigger a Vbat voltage measurement, so that we get a valid reading
505 	   the first time we read Vbat */
506 	sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL, data->control | SCH5627_CTRL_VBAT);
507 	data->last_battery = jiffies;
508 
509 	/*
510 	 * Read limits, we do this only once as reading a register on
511 	 * the sch5627 is quite expensive (and they don't change).
512 	 */
513 	err = sch5627_read_limits(data);
514 	if (err)
515 		return err;
516 
517 	pr_info("found %s chip at %#hx\n", DEVNAME, data->addr);
518 	pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n",
519 		build_code, build_id, hwmon_rev);
520 
521 	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, DEVNAME, data,
522 							 &sch5627_chip_info, NULL);
523 	if (IS_ERR(hwmon_dev))
524 		return PTR_ERR(hwmon_dev);
525 
526 	/* Note failing to register the watchdog is not a fatal error */
527 	sch56xx_watchdog_register(&pdev->dev, data->addr,
528 				  (build_code << 24) | (build_id << 8) | hwmon_rev,
529 				  &data->update_lock, 1);
530 
531 	return 0;
532 }
533 
534 static const struct platform_device_id sch5627_device_id[] = {
535 	{
536 		.name = "sch5627",
537 	},
538 	{ }
539 };
540 MODULE_DEVICE_TABLE(platform, sch5627_device_id);
541 
542 static struct platform_driver sch5627_driver = {
543 	.driver = {
544 		.name	= DRVNAME,
545 	},
546 	.probe		= sch5627_probe,
547 	.id_table	= sch5627_device_id,
548 };
549 
550 module_platform_driver(sch5627_driver);
551 
552 MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver");
553 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
554 MODULE_LICENSE("GPL");
555