1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3 * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com> *
5 ***************************************************************************/
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/err.h>
16 #include <linux/mutex.h>
17 #include "sch56xx-common.h"
19 #define DRVNAME "sch5627"
20 #define DEVNAME DRVNAME /* We only support one model */
22 #define SCH5627_HWMON_ID 0xa5
23 #define SCH5627_COMPANY_ID 0x5c
24 #define SCH5627_PRIMARY_ID 0xa0
26 #define SCH5627_REG_BUILD_CODE 0x39
27 #define SCH5627_REG_BUILD_ID 0x3a
28 #define SCH5627_REG_HWMON_ID 0x3c
29 #define SCH5627_REG_HWMON_REV 0x3d
30 #define SCH5627_REG_COMPANY_ID 0x3e
31 #define SCH5627_REG_PRIMARY_ID 0x3f
32 #define SCH5627_REG_CTRL 0x40
34 #define SCH5627_NO_TEMPS 8
35 #define SCH5627_NO_FANS 4
36 #define SCH5627_NO_IN 5
38 static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = {
39 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 };
40 static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = {
41 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 };
42 static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = {
43 0, 0, 1, 1, 0, 0, 0, 1 };
44 static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = {
45 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 };
46 static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = {
47 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 };
49 static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = {
50 0x2C, 0x2E, 0x30, 0x32 };
51 static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = {
52 0x62, 0x64, 0x66, 0x68 };
54 static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = {
55 0x22, 0x23, 0x24, 0x25, 0x189 };
56 static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = {
57 0xE4, 0xE4, 0xE3, 0xE3, 0x18A };
58 static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = {
60 static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = {
61 10745, 3660, 9765, 10745, 3660 };
62 static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = {
63 "VCC", "VTT", "VBAT", "VTR", "V_IN" };
68 u8 temp_max[SCH5627_NO_TEMPS];
69 u8 temp_crit[SCH5627_NO_TEMPS];
70 u16 fan_min[SCH5627_NO_FANS];
72 struct mutex update_lock;
73 unsigned long last_battery; /* In jiffies */
74 char temp_valid; /* !=0 if following fields are valid */
77 unsigned long temp_last_updated; /* In jiffies */
78 unsigned long fan_last_updated;
79 unsigned long in_last_updated;
80 u16 temp[SCH5627_NO_TEMPS];
81 u16 fan[SCH5627_NO_FANS];
82 u16 in[SCH5627_NO_IN];
85 static int sch5627_update_temp(struct sch5627_data *data)
90 mutex_lock(&data->update_lock);
92 /* Cache the values for 1 second */
93 if (time_after(jiffies, data->temp_last_updated + HZ) || !data->temp_valid) {
94 for (i = 0; i < SCH5627_NO_TEMPS; i++) {
95 val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_TEMP_MSB[i],
96 SCH5627_REG_TEMP_LSN[i],
97 SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
98 if (unlikely(val < 0)) {
104 data->temp_last_updated = jiffies;
105 data->temp_valid = 1;
108 mutex_unlock(&data->update_lock);
112 static int sch5627_update_fan(struct sch5627_data *data)
117 mutex_lock(&data->update_lock);
119 /* Cache the values for 1 second */
120 if (time_after(jiffies, data->fan_last_updated + HZ) || !data->fan_valid) {
121 for (i = 0; i < SCH5627_NO_FANS; i++) {
122 val = sch56xx_read_virtual_reg16(data->addr, SCH5627_REG_FAN[i]);
123 if (unlikely(val < 0)) {
129 data->fan_last_updated = jiffies;
133 mutex_unlock(&data->update_lock);
137 static int sch5627_update_in(struct sch5627_data *data)
142 mutex_lock(&data->update_lock);
144 /* Trigger a Vbat voltage measurement every 5 minutes */
145 if (time_after(jiffies, data->last_battery + 300 * HZ)) {
146 sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL, data->control | 0x10);
147 data->last_battery = jiffies;
150 /* Cache the values for 1 second */
151 if (time_after(jiffies, data->in_last_updated + HZ) || !data->in_valid) {
152 for (i = 0; i < SCH5627_NO_IN; i++) {
153 val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_IN_MSB[i],
154 SCH5627_REG_IN_LSN[i],
155 SCH5627_REG_IN_HIGH_NIBBLE[i]);
156 if (unlikely(val < 0)) {
162 data->in_last_updated = jiffies;
166 mutex_unlock(&data->update_lock);
170 static int sch5627_read_limits(struct sch5627_data *data)
174 for (i = 0; i < SCH5627_NO_TEMPS; i++) {
176 * Note what SMSC calls ABS, is what lm_sensors calls max
177 * (aka high), and HIGH is what lm_sensors calls crit.
179 val = sch56xx_read_virtual_reg(data->addr,
180 SCH5627_REG_TEMP_ABS[i]);
183 data->temp_max[i] = val;
185 val = sch56xx_read_virtual_reg(data->addr,
186 SCH5627_REG_TEMP_HIGH[i]);
189 data->temp_crit[i] = val;
191 for (i = 0; i < SCH5627_NO_FANS; i++) {
192 val = sch56xx_read_virtual_reg16(data->addr,
193 SCH5627_REG_FAN_MIN[i]);
196 data->fan_min[i] = val;
202 static int reg_to_temp(u16 reg)
204 return (reg * 625) / 10 - 64000;
207 static int reg_to_temp_limit(u8 reg)
209 return (reg - 64) * 1000;
212 static int reg_to_rpm(u16 reg)
219 return 5400540 / reg;
222 static umode_t sch5627_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr,
228 static int sch5627_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
231 struct sch5627_data *data = dev_get_drvdata(dev);
236 ret = sch5627_update_temp(data);
240 case hwmon_temp_input:
241 *val = reg_to_temp(data->temp[channel]);
244 *val = reg_to_temp_limit(data->temp_max[channel]);
246 case hwmon_temp_crit:
247 *val = reg_to_temp_limit(data->temp_crit[channel]);
249 case hwmon_temp_fault:
250 *val = (data->temp[channel] == 0);
257 ret = sch5627_update_fan(data);
261 case hwmon_fan_input:
262 ret = reg_to_rpm(data->fan[channel]);
268 ret = reg_to_rpm(data->fan_min[channel]);
273 case hwmon_fan_fault:
274 *val = (data->fan[channel] == 0xffff);
281 ret = sch5627_update_in(data);
286 *val = DIV_ROUND_CLOSEST(data->in[channel] * SCH5627_REG_IN_FACTOR[channel],
300 static int sch5627_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
301 int channel, const char **str)
307 *str = SCH5627_IN_LABELS[channel];
320 static const struct hwmon_ops sch5627_ops = {
321 .is_visible = sch5627_is_visible,
322 .read = sch5627_read,
323 .read_string = sch5627_read_string,
326 static const struct hwmon_channel_info *sch5627_info[] = {
327 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
328 HWMON_CHANNEL_INFO(temp,
329 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
330 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
331 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
332 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
333 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
334 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
335 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
336 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT
338 HWMON_CHANNEL_INFO(fan,
339 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
340 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
341 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
342 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT
344 HWMON_CHANNEL_INFO(in,
345 HWMON_I_INPUT | HWMON_I_LABEL,
346 HWMON_I_INPUT | HWMON_I_LABEL,
347 HWMON_I_INPUT | HWMON_I_LABEL,
348 HWMON_I_INPUT | HWMON_I_LABEL,
354 static const struct hwmon_chip_info sch5627_chip_info = {
356 .info = sch5627_info,
359 static int sch5627_probe(struct platform_device *pdev)
361 struct sch5627_data *data;
362 struct device *hwmon_dev;
363 int err, build_code, build_id, hwmon_rev, val;
365 data = devm_kzalloc(&pdev->dev, sizeof(struct sch5627_data),
370 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
371 mutex_init(&data->update_lock);
372 platform_set_drvdata(pdev, data);
374 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_HWMON_ID);
378 if (val != SCH5627_HWMON_ID) {
379 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon",
380 val, SCH5627_HWMON_ID);
384 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_COMPANY_ID);
388 if (val != SCH5627_COMPANY_ID) {
389 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company",
390 val, SCH5627_COMPANY_ID);
394 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PRIMARY_ID);
398 if (val != SCH5627_PRIMARY_ID) {
399 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary",
400 val, SCH5627_PRIMARY_ID);
404 build_code = sch56xx_read_virtual_reg(data->addr,
405 SCH5627_REG_BUILD_CODE);
409 build_id = sch56xx_read_virtual_reg16(data->addr,
410 SCH5627_REG_BUILD_ID);
414 hwmon_rev = sch56xx_read_virtual_reg(data->addr,
415 SCH5627_REG_HWMON_REV);
419 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_CTRL);
424 if (!(data->control & 0x01)) {
425 pr_err("hardware monitoring not enabled\n");
428 /* Trigger a Vbat voltage measurement, so that we get a valid reading
429 the first time we read Vbat */
430 sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL,
431 data->control | 0x10);
432 data->last_battery = jiffies;
435 * Read limits, we do this only once as reading a register on
436 * the sch5627 is quite expensive (and they don't change).
438 err = sch5627_read_limits(data);
442 pr_info("found %s chip at %#hx\n", DEVNAME, data->addr);
443 pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n",
444 build_code, build_id, hwmon_rev);
446 hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, DEVNAME, data,
447 &sch5627_chip_info, NULL);
448 if (IS_ERR(hwmon_dev))
449 return PTR_ERR(hwmon_dev);
451 /* Note failing to register the watchdog is not a fatal error */
452 sch56xx_watchdog_register(&pdev->dev, data->addr,
453 (build_code << 24) | (build_id << 8) | hwmon_rev,
454 &data->update_lock, 1);
459 static struct platform_driver sch5627_driver = {
463 .probe = sch5627_probe,
466 module_platform_driver(sch5627_driver);
468 MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver");
469 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
470 MODULE_LICENSE("GPL");