Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

power: reset: at91-reset: add sysfs interface to the power on reason

Introduce a list of generic reset sources and use them to export the
power on reason through sysfs. Update the ABI documentation to describe
this new interface.

Signed-off-by: Kamel Bouhara <kamel.bouhara@bootlin.com>
Acked-by: Nicolas Ferre <nicolas.ferre@microchip.com>
[Miquel Raynal: Follow-up on Kamel's work, 4 years later]
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>

authored by

Kamel Bouhara and committed by
Sebastian Reichel
d40befed 7b9fc309

+58 -10
+12
Documentation/ABI/testing/sysfs-platform-power-on-reason
··· 1 + What: /sys/devices/platform/.../power_on_reason 2 + Date: June 2023 3 + KernelVersion: 6.5 4 + Contact: Kamel Bouhara <kamel.bouhara@bootlin.com> 5 + Description: Shows system power on reason. The following strings/reasons can 6 + be read (the list can be extended): 7 + "regular power-up", "RTC wakeup", "watchdog timeout", 8 + "software reset", "reset button action", "CPU clock failure", 9 + "crystal oscillator failure", "brown-out reset", 10 + "unknown reason". 11 + 12 + The file is read only.
+27 -10
drivers/power/reset/at91-reset.c
··· 18 18 #include <linux/platform_device.h> 19 19 #include <linux/reboot.h> 20 20 #include <linux/reset-controller.h> 21 + #include <linux/power/power_on_reason.h> 21 22 22 23 #include <soc/at91/at91sam9_ddrsdr.h> 23 24 #include <soc/at91/at91sam9_sdramc.h> ··· 150 149 return NOTIFY_DONE; 151 150 } 152 151 153 - static const char * __init at91_reset_reason(struct at91_reset *reset) 152 + static const char *at91_reset_reason(struct at91_reset *reset) 154 153 { 155 154 u32 reg = readl(reset->rstc_base + AT91_RSTC_SR); 156 155 const char *reason; 157 156 158 157 switch ((reg & AT91_RSTC_RSTTYP) >> 8) { 159 158 case RESET_TYPE_GENERAL: 160 - reason = "general reset"; 159 + reason = POWER_ON_REASON_REGULAR; 161 160 break; 162 161 case RESET_TYPE_WAKEUP: 163 - reason = "wakeup"; 162 + reason = POWER_ON_REASON_RTC; 164 163 break; 165 164 case RESET_TYPE_WATCHDOG: 166 - reason = "watchdog reset"; 165 + reason = POWER_ON_REASON_WATCHDOG; 167 166 break; 168 167 case RESET_TYPE_SOFTWARE: 169 - reason = "software reset"; 168 + reason = POWER_ON_REASON_SOFTWARE; 170 169 break; 171 170 case RESET_TYPE_USER: 172 - reason = "user reset"; 171 + reason = POWER_ON_REASON_RST_BTN; 173 172 break; 174 173 case RESET_TYPE_CPU_FAIL: 175 - reason = "CPU clock failure detection"; 174 + reason = POWER_ON_REASON_CPU_CLK_FAIL; 176 175 break; 177 176 case RESET_TYPE_XTAL_FAIL: 178 - reason = "32.768 kHz crystal failure detection"; 177 + reason = POWER_ON_REASON_XTAL_FAIL; 179 178 break; 180 179 case RESET_TYPE_ULP2: 181 - reason = "ULP2 reset"; 180 + reason = POWER_ON_REASON_BROWN_OUT; 182 181 break; 183 182 default: 184 - reason = "unknown reset"; 183 + reason = POWER_ON_REASON_UNKNOWN; 185 184 break; 186 185 } 187 186 188 187 return reason; 189 188 } 189 + 190 + static ssize_t power_on_reason_show(struct device *dev, 191 + struct device_attribute *attr, char *buf) 192 + { 193 + struct platform_device *pdev = to_platform_device(dev); 194 + struct at91_reset *reset = platform_get_drvdata(pdev); 195 + 196 + return sprintf(buf, "%s\n", at91_reset_reason(reset)); 197 + } 198 + static DEVICE_ATTR_RO(power_on_reason); 190 199 191 200 static const struct of_device_id at91_ramc_of_match[] = { 192 201 { ··· 401 390 ret = register_restart_handler(&reset->nb); 402 391 if (ret) 403 392 goto disable_clk; 393 + 394 + ret = device_create_file(&pdev->dev, &dev_attr_power_on_reason); 395 + if (ret) { 396 + dev_err(&pdev->dev, "Could not create sysfs entry\n"); 397 + return ret; 398 + } 404 399 405 400 dev_info(&pdev->dev, "Starting after %s\n", at91_reset_reason(reset)); 406 401
+19
include/linux/power/power_on_reason.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * Author: Kamel Bouhra <kamel.bouhara@bootlin.com> 4 + */ 5 + 6 + #ifndef POWER_ON_REASON_H 7 + #define POWER_ON_REASON_H 8 + 9 + #define POWER_ON_REASON_REGULAR "regular power-up" 10 + #define POWER_ON_REASON_RTC "RTC wakeup" 11 + #define POWER_ON_REASON_WATCHDOG "watchdog timeout" 12 + #define POWER_ON_REASON_SOFTWARE "software reset" 13 + #define POWER_ON_REASON_RST_BTN "reset button action" 14 + #define POWER_ON_REASON_CPU_CLK_FAIL "CPU clock failure" 15 + #define POWER_ON_REASON_XTAL_FAIL "crystal oscillator failure" 16 + #define POWER_ON_REASON_BROWN_OUT "brown-out reset" 17 + #define POWER_ON_REASON_UNKNOWN "unknown reason" 18 + 19 + #endif /* POWER_ON_REASON_H */