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/bits.h>
10 #include <linux/minmax.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
14 #include <linux/init.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <linux/jiffies.h>
18 #include <linux/platform_device.h>
19 #include <linux/hwmon.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include "sch56xx-common.h"
24 #define DRVNAME "sch5627"
25 #define DEVNAME DRVNAME /* We only support one model */
27 #define SCH5627_HWMON_ID 0xa5
28 #define SCH5627_COMPANY_ID 0x5c
29 #define SCH5627_PRIMARY_ID 0xa0
31 #define SCH5627_REG_BUILD_CODE 0x39
32 #define SCH5627_REG_BUILD_ID 0x3a
33 #define SCH5627_REG_HWMON_ID 0x3c
34 #define SCH5627_REG_HWMON_REV 0x3d
35 #define SCH5627_REG_COMPANY_ID 0x3e
36 #define SCH5627_REG_PRIMARY_ID 0x3f
37 #define SCH5627_REG_CTRL 0x40
39 #define SCH5627_CTRL_START BIT(0)
40 #define SCH5627_CTRL_LOCK BIT(1)
41 #define SCH5627_CTRL_VBAT BIT(4)
43 #define SCH5627_NO_TEMPS 8
44 #define SCH5627_NO_FANS 4
45 #define SCH5627_NO_IN 5
47 static const u16 SCH5627_REG_TEMP_MSB
[SCH5627_NO_TEMPS
] = {
48 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 };
49 static const u16 SCH5627_REG_TEMP_LSN
[SCH5627_NO_TEMPS
] = {
50 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 };
51 static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE
[SCH5627_NO_TEMPS
] = {
52 0, 0, 1, 1, 0, 0, 0, 1 };
53 static const u16 SCH5627_REG_TEMP_HIGH
[SCH5627_NO_TEMPS
] = {
54 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 };
55 static const u16 SCH5627_REG_TEMP_ABS
[SCH5627_NO_TEMPS
] = {
56 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 };
58 static const u16 SCH5627_REG_FAN
[SCH5627_NO_FANS
] = {
59 0x2C, 0x2E, 0x30, 0x32 };
60 static const u16 SCH5627_REG_FAN_MIN
[SCH5627_NO_FANS
] = {
61 0x62, 0x64, 0x66, 0x68 };
63 static const u16 SCH5627_REG_PWM_MAP
[SCH5627_NO_FANS
] = {
64 0xA0, 0xA1, 0xA2, 0xA3 };
66 static const u16 SCH5627_REG_IN_MSB
[SCH5627_NO_IN
] = {
67 0x22, 0x23, 0x24, 0x25, 0x189 };
68 static const u16 SCH5627_REG_IN_LSN
[SCH5627_NO_IN
] = {
69 0xE4, 0xE4, 0xE3, 0xE3, 0x18A };
70 static const u16 SCH5627_REG_IN_HIGH_NIBBLE
[SCH5627_NO_IN
] = {
72 static const u16 SCH5627_REG_IN_FACTOR
[SCH5627_NO_IN
] = {
73 10745, 3660, 9765, 10745, 3660 };
74 static const char * const SCH5627_IN_LABELS
[SCH5627_NO_IN
] = {
75 "VCC", "VTT", "VBAT", "VTR", "V_IN" };
78 struct regmap
*regmap
;
82 struct mutex update_lock
;
83 unsigned long last_battery
; /* In jiffies */
84 char temp_valid
; /* !=0 if following fields are valid */
87 unsigned long temp_last_updated
; /* In jiffies */
88 unsigned long fan_last_updated
;
89 unsigned long in_last_updated
;
90 u16 temp
[SCH5627_NO_TEMPS
];
91 u16 fan
[SCH5627_NO_FANS
];
92 u16 in
[SCH5627_NO_IN
];
95 static const struct regmap_range sch5627_tunables_ranges
[] = {
96 regmap_reg_range(0x57, 0x57),
97 regmap_reg_range(0x59, 0x59),
98 regmap_reg_range(0x5B, 0x5B),
99 regmap_reg_range(0x5D, 0x5D),
100 regmap_reg_range(0x5F, 0x5F),
101 regmap_reg_range(0x61, 0x69),
102 regmap_reg_range(0x96, 0x9B),
103 regmap_reg_range(0xA0, 0xA3),
104 regmap_reg_range(0x184, 0x184),
105 regmap_reg_range(0x186, 0x186),
106 regmap_reg_range(0x1A8, 0x1A9),
109 static const struct regmap_access_table sch5627_tunables_table
= {
110 .yes_ranges
= sch5627_tunables_ranges
,
111 .n_yes_ranges
= ARRAY_SIZE(sch5627_tunables_ranges
),
114 static const struct regmap_config sch5627_regmap_config
= {
117 .wr_table
= &sch5627_tunables_table
,
118 .rd_table
= &sch5627_tunables_table
,
119 .cache_type
= REGCACHE_MAPLE
,
120 .use_single_read
= true,
121 .use_single_write
= true,
125 static int sch5627_update_temp(struct sch5627_data
*data
)
130 mutex_lock(&data
->update_lock
);
132 /* Cache the values for 1 second */
133 if (time_after(jiffies
, data
->temp_last_updated
+ HZ
) || !data
->temp_valid
) {
134 for (i
= 0; i
< SCH5627_NO_TEMPS
; i
++) {
135 val
= sch56xx_read_virtual_reg12(data
->addr
, SCH5627_REG_TEMP_MSB
[i
],
136 SCH5627_REG_TEMP_LSN
[i
],
137 SCH5627_REG_TEMP_HIGH_NIBBLE
[i
]);
138 if (unlikely(val
< 0)) {
144 data
->temp_last_updated
= jiffies
;
145 data
->temp_valid
= 1;
148 mutex_unlock(&data
->update_lock
);
152 static int sch5627_update_fan(struct sch5627_data
*data
)
157 mutex_lock(&data
->update_lock
);
159 /* Cache the values for 1 second */
160 if (time_after(jiffies
, data
->fan_last_updated
+ HZ
) || !data
->fan_valid
) {
161 for (i
= 0; i
< SCH5627_NO_FANS
; i
++) {
162 val
= sch56xx_read_virtual_reg16(data
->addr
, SCH5627_REG_FAN
[i
]);
163 if (unlikely(val
< 0)) {
169 data
->fan_last_updated
= jiffies
;
173 mutex_unlock(&data
->update_lock
);
177 static int sch5627_update_in(struct sch5627_data
*data
)
182 mutex_lock(&data
->update_lock
);
184 /* Trigger a Vbat voltage measurement every 5 minutes */
185 if (time_after(jiffies
, data
->last_battery
+ 300 * HZ
)) {
186 sch56xx_write_virtual_reg(data
->addr
, SCH5627_REG_CTRL
,
187 data
->control
| SCH5627_CTRL_VBAT
);
188 data
->last_battery
= jiffies
;
191 /* Cache the values for 1 second */
192 if (time_after(jiffies
, data
->in_last_updated
+ HZ
) || !data
->in_valid
) {
193 for (i
= 0; i
< SCH5627_NO_IN
; i
++) {
194 val
= sch56xx_read_virtual_reg12(data
->addr
, SCH5627_REG_IN_MSB
[i
],
195 SCH5627_REG_IN_LSN
[i
],
196 SCH5627_REG_IN_HIGH_NIBBLE
[i
]);
197 if (unlikely(val
< 0)) {
203 data
->in_last_updated
= jiffies
;
207 mutex_unlock(&data
->update_lock
);
211 static int reg_to_temp(u16 reg
)
213 return (reg
* 625) / 10 - 64000;
216 static int reg_to_temp_limit(u8 reg
)
218 return (reg
- 64) * 1000;
221 static int reg_to_rpm(u16 reg
)
228 return 5400540 / reg
;
231 static u8
sch5627_temp_limit_to_reg(long value
)
233 long limit
= (value
/ 1000) + 64;
235 return clamp_val(limit
, 0, U8_MAX
);
238 static u16
sch5627_rpm_to_reg(long value
)
245 pulses
= 5400540 / value
;
247 return clamp_val(pulses
, 1, U16_MAX
- 1);
250 static umode_t
sch5627_is_visible(const void *drvdata
, enum hwmon_sensor_types type
, u32 attr
,
253 const struct sch5627_data
*data
= drvdata
;
255 /* Once the lock bit is set, the virtual registers become read-only
256 * until the next power cycle.
258 if (data
->control
& SCH5627_CTRL_LOCK
)
265 case hwmon_temp_crit
:
281 case hwmon_pwm_auto_channels_temp
:
294 static int sch5627_read(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
, int channel
,
297 struct sch5627_data
*data
= dev_get_drvdata(dev
);
303 case hwmon_temp_input
:
304 ret
= sch5627_update_temp(data
);
308 *val
= reg_to_temp(data
->temp
[channel
]);
311 ret
= regmap_read(data
->regmap
, SCH5627_REG_TEMP_ABS
[channel
], &value
);
315 *val
= reg_to_temp_limit((u8
)value
);
317 case hwmon_temp_crit
:
318 ret
= regmap_read(data
->regmap
, SCH5627_REG_TEMP_HIGH
[channel
], &value
);
322 *val
= reg_to_temp_limit((u8
)value
);
324 case hwmon_temp_fault
:
325 ret
= sch5627_update_temp(data
);
329 *val
= (data
->temp
[channel
] == 0);
337 case hwmon_fan_input
:
338 ret
= sch5627_update_fan(data
);
342 ret
= reg_to_rpm(data
->fan
[channel
]);
349 ret
= sch56xx_regmap_read16(data
->regmap
, SCH5627_REG_FAN_MIN
[channel
],
354 ret
= reg_to_rpm((u16
)value
);
360 case hwmon_fan_fault
:
361 ret
= sch5627_update_fan(data
);
365 *val
= (data
->fan
[channel
] == 0xffff);
373 case hwmon_pwm_auto_channels_temp
:
374 ret
= regmap_read(data
->regmap
, SCH5627_REG_PWM_MAP
[channel
], &value
);
385 ret
= sch5627_update_in(data
);
390 *val
= DIV_ROUND_CLOSEST(data
->in
[channel
] * SCH5627_REG_IN_FACTOR
[channel
],
404 static int sch5627_read_string(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
405 int channel
, const char **str
)
411 *str
= SCH5627_IN_LABELS
[channel
];
424 static int sch5627_write(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
, int channel
,
427 struct sch5627_data
*data
= dev_get_drvdata(dev
);
433 temp
= sch5627_temp_limit_to_reg(val
);
437 return regmap_write(data
->regmap
, SCH5627_REG_TEMP_ABS
[channel
], temp
);
438 case hwmon_temp_crit
:
439 return regmap_write(data
->regmap
, SCH5627_REG_TEMP_HIGH
[channel
], temp
);
447 fan
= sch5627_rpm_to_reg(val
);
449 return sch56xx_regmap_write16(data
->regmap
, SCH5627_REG_FAN_MIN
[channel
],
457 case hwmon_pwm_auto_channels_temp
:
458 /* registers are 8 bit wide */
459 if (val
> U8_MAX
|| val
< 0)
462 return regmap_write(data
->regmap
, SCH5627_REG_PWM_MAP
[channel
], val
);
474 static const struct hwmon_ops sch5627_ops
= {
475 .is_visible
= sch5627_is_visible
,
476 .read
= sch5627_read
,
477 .read_string
= sch5627_read_string
,
478 .write
= sch5627_write
,
481 static const struct hwmon_channel_info
* const sch5627_info
[] = {
482 HWMON_CHANNEL_INFO(chip
, HWMON_C_REGISTER_TZ
),
483 HWMON_CHANNEL_INFO(temp
,
484 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
| HWMON_T_FAULT
,
485 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
| HWMON_T_FAULT
,
486 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
| HWMON_T_FAULT
,
487 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
| HWMON_T_FAULT
,
488 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
| HWMON_T_FAULT
,
489 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
| HWMON_T_FAULT
,
490 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
| HWMON_T_FAULT
,
491 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
| HWMON_T_FAULT
493 HWMON_CHANNEL_INFO(fan
,
494 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_FAULT
,
495 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_FAULT
,
496 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_FAULT
,
497 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_FAULT
499 HWMON_CHANNEL_INFO(pwm
,
500 HWMON_PWM_AUTO_CHANNELS_TEMP
,
501 HWMON_PWM_AUTO_CHANNELS_TEMP
,
502 HWMON_PWM_AUTO_CHANNELS_TEMP
,
503 HWMON_PWM_AUTO_CHANNELS_TEMP
505 HWMON_CHANNEL_INFO(in
,
506 HWMON_I_INPUT
| HWMON_I_LABEL
,
507 HWMON_I_INPUT
| HWMON_I_LABEL
,
508 HWMON_I_INPUT
| HWMON_I_LABEL
,
509 HWMON_I_INPUT
| HWMON_I_LABEL
,
515 static const struct hwmon_chip_info sch5627_chip_info
= {
517 .info
= sch5627_info
,
520 static int sch5627_probe(struct platform_device
*pdev
)
522 struct sch5627_data
*data
;
523 struct device
*hwmon_dev
;
524 int build_code
, build_id
, hwmon_rev
, val
;
526 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct sch5627_data
),
531 data
->addr
= platform_get_resource(pdev
, IORESOURCE_IO
, 0)->start
;
532 mutex_init(&data
->update_lock
);
533 platform_set_drvdata(pdev
, data
);
535 val
= sch56xx_read_virtual_reg(data
->addr
, SCH5627_REG_HWMON_ID
);
539 if (val
!= SCH5627_HWMON_ID
) {
540 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon",
541 val
, SCH5627_HWMON_ID
);
545 val
= sch56xx_read_virtual_reg(data
->addr
, SCH5627_REG_COMPANY_ID
);
549 if (val
!= SCH5627_COMPANY_ID
) {
550 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company",
551 val
, SCH5627_COMPANY_ID
);
555 val
= sch56xx_read_virtual_reg(data
->addr
, SCH5627_REG_PRIMARY_ID
);
559 if (val
!= SCH5627_PRIMARY_ID
) {
560 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary",
561 val
, SCH5627_PRIMARY_ID
);
565 build_code
= sch56xx_read_virtual_reg(data
->addr
,
566 SCH5627_REG_BUILD_CODE
);
570 build_id
= sch56xx_read_virtual_reg16(data
->addr
,
571 SCH5627_REG_BUILD_ID
);
575 hwmon_rev
= sch56xx_read_virtual_reg(data
->addr
,
576 SCH5627_REG_HWMON_REV
);
580 val
= sch56xx_read_virtual_reg(data
->addr
, SCH5627_REG_CTRL
);
585 if (!(data
->control
& SCH5627_CTRL_START
)) {
586 pr_err("hardware monitoring not enabled\n");
590 data
->regmap
= devm_regmap_init_sch56xx(&pdev
->dev
, &data
->update_lock
, data
->addr
,
591 &sch5627_regmap_config
);
592 if (IS_ERR(data
->regmap
))
593 return PTR_ERR(data
->regmap
);
595 /* Trigger a Vbat voltage measurement, so that we get a valid reading
596 the first time we read Vbat */
597 sch56xx_write_virtual_reg(data
->addr
, SCH5627_REG_CTRL
, data
->control
| SCH5627_CTRL_VBAT
);
598 data
->last_battery
= jiffies
;
600 pr_info("found %s chip at %#hx\n", DEVNAME
, data
->addr
);
601 pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n",
602 build_code
, build_id
, hwmon_rev
);
604 hwmon_dev
= devm_hwmon_device_register_with_info(&pdev
->dev
, DEVNAME
, data
,
605 &sch5627_chip_info
, NULL
);
606 if (IS_ERR(hwmon_dev
))
607 return PTR_ERR(hwmon_dev
);
609 /* Note failing to register the watchdog is not a fatal error */
610 sch56xx_watchdog_register(&pdev
->dev
, data
->addr
,
611 (build_code
<< 24) | (build_id
<< 8) | hwmon_rev
,
612 &data
->update_lock
, 1);
617 static int sch5627_suspend(struct device
*dev
)
619 struct sch5627_data
*data
= dev_get_drvdata(dev
);
621 regcache_cache_only(data
->regmap
, true);
622 regcache_mark_dirty(data
->regmap
);
627 static int sch5627_resume(struct device
*dev
)
629 struct sch5627_data
*data
= dev_get_drvdata(dev
);
631 regcache_cache_only(data
->regmap
, false);
632 /* We must not access the virtual registers when the lock bit is set */
633 if (data
->control
& SCH5627_CTRL_LOCK
)
634 return regcache_drop_region(data
->regmap
, 0, U16_MAX
);
636 return regcache_sync(data
->regmap
);
639 static DEFINE_SIMPLE_DEV_PM_OPS(sch5627_dev_pm_ops
, sch5627_suspend
, sch5627_resume
);
641 static const struct platform_device_id sch5627_device_id
[] = {
647 MODULE_DEVICE_TABLE(platform
, sch5627_device_id
);
649 static struct platform_driver sch5627_driver
= {
652 .pm
= pm_sleep_ptr(&sch5627_dev_pm_ops
),
654 .probe
= sch5627_probe
,
655 .id_table
= sch5627_device_id
,
658 module_platform_driver(sch5627_driver
);
660 MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver");
661 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
662 MODULE_LICENSE("GPL");