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/mod_devicetable.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/jiffies.h>
14 #include <linux/platform_device.h>
15 #include <linux/hwmon.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include "sch56xx-common.h"
20 #define DRVNAME "sch5627"
21 #define DEVNAME DRVNAME /* We only support one model */
23 #define SCH5627_HWMON_ID 0xa5
24 #define SCH5627_COMPANY_ID 0x5c
25 #define SCH5627_PRIMARY_ID 0xa0
27 #define SCH5627_REG_BUILD_CODE 0x39
28 #define SCH5627_REG_BUILD_ID 0x3a
29 #define SCH5627_REG_HWMON_ID 0x3c
30 #define SCH5627_REG_HWMON_REV 0x3d
31 #define SCH5627_REG_COMPANY_ID 0x3e
32 #define SCH5627_REG_PRIMARY_ID 0x3f
33 #define SCH5627_REG_CTRL 0x40
35 #define SCH5627_NO_TEMPS 8
36 #define SCH5627_NO_FANS 4
37 #define SCH5627_NO_IN 5
39 static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = {
40 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 };
41 static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = {
42 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 };
43 static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = {
44 0, 0, 1, 1, 0, 0, 0, 1 };
45 static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = {
46 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 };
47 static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = {
48 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 };
50 static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = {
51 0x2C, 0x2E, 0x30, 0x32 };
52 static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = {
53 0x62, 0x64, 0x66, 0x68 };
55 static const u16 SCH5627_REG_PWM_MAP[SCH5627_NO_FANS] = {
56 0xA0, 0xA1, 0xA2, 0xA3 };
58 static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = {
59 0x22, 0x23, 0x24, 0x25, 0x189 };
60 static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = {
61 0xE4, 0xE4, 0xE3, 0xE3, 0x18A };
62 static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = {
64 static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = {
65 10745, 3660, 9765, 10745, 3660 };
66 static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = {
67 "VCC", "VTT", "VBAT", "VTR", "V_IN" };
72 u8 temp_max[SCH5627_NO_TEMPS];
73 u8 temp_crit[SCH5627_NO_TEMPS];
74 u16 fan_min[SCH5627_NO_FANS];
76 struct mutex update_lock;
77 unsigned long last_battery; /* In jiffies */
78 char temp_valid; /* !=0 if following fields are valid */
81 unsigned long temp_last_updated; /* In jiffies */
82 unsigned long fan_last_updated;
83 unsigned long in_last_updated;
84 u16 temp[SCH5627_NO_TEMPS];
85 u16 fan[SCH5627_NO_FANS];
86 u16 in[SCH5627_NO_IN];
89 static int sch5627_update_temp(struct sch5627_data *data)
94 mutex_lock(&data->update_lock);
96 /* Cache the values for 1 second */
97 if (time_after(jiffies, data->temp_last_updated + HZ) || !data->temp_valid) {
98 for (i = 0; i < SCH5627_NO_TEMPS; i++) {
99 val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_TEMP_MSB[i],
100 SCH5627_REG_TEMP_LSN[i],
101 SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
102 if (unlikely(val < 0)) {
108 data->temp_last_updated = jiffies;
109 data->temp_valid = 1;
112 mutex_unlock(&data->update_lock);
116 static int sch5627_update_fan(struct sch5627_data *data)
121 mutex_lock(&data->update_lock);
123 /* Cache the values for 1 second */
124 if (time_after(jiffies, data->fan_last_updated + HZ) || !data->fan_valid) {
125 for (i = 0; i < SCH5627_NO_FANS; i++) {
126 val = sch56xx_read_virtual_reg16(data->addr, SCH5627_REG_FAN[i]);
127 if (unlikely(val < 0)) {
133 data->fan_last_updated = jiffies;
137 mutex_unlock(&data->update_lock);
141 static int sch5627_update_in(struct sch5627_data *data)
146 mutex_lock(&data->update_lock);
148 /* Trigger a Vbat voltage measurement every 5 minutes */
149 if (time_after(jiffies, data->last_battery + 300 * HZ)) {
150 sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL, data->control | 0x10);
151 data->last_battery = jiffies;
154 /* Cache the values for 1 second */
155 if (time_after(jiffies, data->in_last_updated + HZ) || !data->in_valid) {
156 for (i = 0; i < SCH5627_NO_IN; i++) {
157 val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_IN_MSB[i],
158 SCH5627_REG_IN_LSN[i],
159 SCH5627_REG_IN_HIGH_NIBBLE[i]);
160 if (unlikely(val < 0)) {
166 data->in_last_updated = jiffies;
170 mutex_unlock(&data->update_lock);
174 static int sch5627_read_limits(struct sch5627_data *data)
178 for (i = 0; i < SCH5627_NO_TEMPS; i++) {
180 * Note what SMSC calls ABS, is what lm_sensors calls max
181 * (aka high), and HIGH is what lm_sensors calls crit.
183 val = sch56xx_read_virtual_reg(data->addr,
184 SCH5627_REG_TEMP_ABS[i]);
187 data->temp_max[i] = val;
189 val = sch56xx_read_virtual_reg(data->addr,
190 SCH5627_REG_TEMP_HIGH[i]);
193 data->temp_crit[i] = val;
195 for (i = 0; i < SCH5627_NO_FANS; i++) {
196 val = sch56xx_read_virtual_reg16(data->addr,
197 SCH5627_REG_FAN_MIN[i]);
200 data->fan_min[i] = val;
206 static int reg_to_temp(u16 reg)
208 return (reg * 625) / 10 - 64000;
211 static int reg_to_temp_limit(u8 reg)
213 return (reg - 64) * 1000;
216 static int reg_to_rpm(u16 reg)
223 return 5400540 / reg;
226 static umode_t sch5627_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr,
229 if (type == hwmon_pwm && attr == hwmon_pwm_auto_channels_temp)
235 static int sch5627_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
238 struct sch5627_data *data = dev_get_drvdata(dev);
243 ret = sch5627_update_temp(data);
247 case hwmon_temp_input:
248 *val = reg_to_temp(data->temp[channel]);
251 *val = reg_to_temp_limit(data->temp_max[channel]);
253 case hwmon_temp_crit:
254 *val = reg_to_temp_limit(data->temp_crit[channel]);
256 case hwmon_temp_fault:
257 *val = (data->temp[channel] == 0);
264 ret = sch5627_update_fan(data);
268 case hwmon_fan_input:
269 ret = reg_to_rpm(data->fan[channel]);
275 ret = reg_to_rpm(data->fan_min[channel]);
280 case hwmon_fan_fault:
281 *val = (data->fan[channel] == 0xffff);
289 case hwmon_pwm_auto_channels_temp:
290 mutex_lock(&data->update_lock);
291 ret = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PWM_MAP[channel]);
292 mutex_unlock(&data->update_lock);
305 ret = sch5627_update_in(data);
310 *val = DIV_ROUND_CLOSEST(data->in[channel] * SCH5627_REG_IN_FACTOR[channel],
324 static int sch5627_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
325 int channel, const char **str)
331 *str = SCH5627_IN_LABELS[channel];
344 static int sch5627_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
347 struct sch5627_data *data = dev_get_drvdata(dev);
353 case hwmon_pwm_auto_channels_temp:
354 /* registers are 8 bit wide */
355 if (val > U8_MAX || val < 0)
358 mutex_lock(&data->update_lock);
359 ret = sch56xx_write_virtual_reg(data->addr, SCH5627_REG_PWM_MAP[channel],
361 mutex_unlock(&data->update_lock);
375 static const struct hwmon_ops sch5627_ops = {
376 .is_visible = sch5627_is_visible,
377 .read = sch5627_read,
378 .read_string = sch5627_read_string,
379 .write = sch5627_write,
382 static const struct hwmon_channel_info * const sch5627_info[] = {
383 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
384 HWMON_CHANNEL_INFO(temp,
385 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
386 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
387 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
388 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
389 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
390 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
391 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT,
392 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT
394 HWMON_CHANNEL_INFO(fan,
395 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
396 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
397 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT,
398 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT
400 HWMON_CHANNEL_INFO(pwm,
401 HWMON_PWM_AUTO_CHANNELS_TEMP,
402 HWMON_PWM_AUTO_CHANNELS_TEMP,
403 HWMON_PWM_AUTO_CHANNELS_TEMP,
404 HWMON_PWM_AUTO_CHANNELS_TEMP
406 HWMON_CHANNEL_INFO(in,
407 HWMON_I_INPUT | HWMON_I_LABEL,
408 HWMON_I_INPUT | HWMON_I_LABEL,
409 HWMON_I_INPUT | HWMON_I_LABEL,
410 HWMON_I_INPUT | HWMON_I_LABEL,
416 static const struct hwmon_chip_info sch5627_chip_info = {
418 .info = sch5627_info,
421 static int sch5627_probe(struct platform_device *pdev)
423 struct sch5627_data *data;
424 struct device *hwmon_dev;
425 int err, build_code, build_id, hwmon_rev, val;
427 data = devm_kzalloc(&pdev->dev, sizeof(struct sch5627_data),
432 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
433 mutex_init(&data->update_lock);
434 platform_set_drvdata(pdev, data);
436 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_HWMON_ID);
440 if (val != SCH5627_HWMON_ID) {
441 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon",
442 val, SCH5627_HWMON_ID);
446 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_COMPANY_ID);
450 if (val != SCH5627_COMPANY_ID) {
451 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company",
452 val, SCH5627_COMPANY_ID);
456 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PRIMARY_ID);
460 if (val != SCH5627_PRIMARY_ID) {
461 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary",
462 val, SCH5627_PRIMARY_ID);
466 build_code = sch56xx_read_virtual_reg(data->addr,
467 SCH5627_REG_BUILD_CODE);
471 build_id = sch56xx_read_virtual_reg16(data->addr,
472 SCH5627_REG_BUILD_ID);
476 hwmon_rev = sch56xx_read_virtual_reg(data->addr,
477 SCH5627_REG_HWMON_REV);
481 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_CTRL);
486 if (!(data->control & 0x01)) {
487 pr_err("hardware monitoring not enabled\n");
490 /* Trigger a Vbat voltage measurement, so that we get a valid reading
491 the first time we read Vbat */
492 sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL,
493 data->control | 0x10);
494 data->last_battery = jiffies;
497 * Read limits, we do this only once as reading a register on
498 * the sch5627 is quite expensive (and they don't change).
500 err = sch5627_read_limits(data);
504 pr_info("found %s chip at %#hx\n", DEVNAME, data->addr);
505 pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n",
506 build_code, build_id, hwmon_rev);
508 hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, DEVNAME, data,
509 &sch5627_chip_info, NULL);
510 if (IS_ERR(hwmon_dev))
511 return PTR_ERR(hwmon_dev);
513 /* Note failing to register the watchdog is not a fatal error */
514 sch56xx_watchdog_register(&pdev->dev, data->addr,
515 (build_code << 24) | (build_id << 8) | hwmon_rev,
516 &data->update_lock, 1);
521 static const struct platform_device_id sch5627_device_id[] = {
527 MODULE_DEVICE_TABLE(platform, sch5627_device_id);
529 static struct platform_driver sch5627_driver = {
533 .probe = sch5627_probe,
534 .id_table = sch5627_device_id,
537 module_platform_driver(sch5627_driver);
539 MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver");
540 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
541 MODULE_LICENSE("GPL");