Skip to content

Commit d40befe

Browse files
kamel-bouharasre
authored andcommitted
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>
1 parent 7b9fc30 commit d40befe

3 files changed

Lines changed: 58 additions & 10 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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.

drivers/power/reset/at91-reset.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/platform_device.h>
1919
#include <linux/reboot.h>
2020
#include <linux/reset-controller.h>
21+
#include <linux/power/power_on_reason.h>
2122

2223
#include <soc/at91/at91sam9_ddrsdr.h>
2324
#include <soc/at91/at91sam9_sdramc.h>
@@ -149,44 +150,54 @@ static int at91_reset(struct notifier_block *this, unsigned long mode,
149150
return NOTIFY_DONE;
150151
}
151152

152-
static const char * __init at91_reset_reason(struct at91_reset *reset)
153+
static const char *at91_reset_reason(struct at91_reset *reset)
153154
{
154155
u32 reg = readl(reset->rstc_base + AT91_RSTC_SR);
155156
const char *reason;
156157

157158
switch ((reg & AT91_RSTC_RSTTYP) >> 8) {
158159
case RESET_TYPE_GENERAL:
159-
reason = "general reset";
160+
reason = POWER_ON_REASON_REGULAR;
160161
break;
161162
case RESET_TYPE_WAKEUP:
162-
reason = "wakeup";
163+
reason = POWER_ON_REASON_RTC;
163164
break;
164165
case RESET_TYPE_WATCHDOG:
165-
reason = "watchdog reset";
166+
reason = POWER_ON_REASON_WATCHDOG;
166167
break;
167168
case RESET_TYPE_SOFTWARE:
168-
reason = "software reset";
169+
reason = POWER_ON_REASON_SOFTWARE;
169170
break;
170171
case RESET_TYPE_USER:
171-
reason = "user reset";
172+
reason = POWER_ON_REASON_RST_BTN;
172173
break;
173174
case RESET_TYPE_CPU_FAIL:
174-
reason = "CPU clock failure detection";
175+
reason = POWER_ON_REASON_CPU_CLK_FAIL;
175176
break;
176177
case RESET_TYPE_XTAL_FAIL:
177-
reason = "32.768 kHz crystal failure detection";
178+
reason = POWER_ON_REASON_XTAL_FAIL;
178179
break;
179180
case RESET_TYPE_ULP2:
180-
reason = "ULP2 reset";
181+
reason = POWER_ON_REASON_BROWN_OUT;
181182
break;
182183
default:
183-
reason = "unknown reset";
184+
reason = POWER_ON_REASON_UNKNOWN;
184185
break;
185186
}
186187

187188
return reason;
188189
}
189190

191+
static ssize_t power_on_reason_show(struct device *dev,
192+
struct device_attribute *attr, char *buf)
193+
{
194+
struct platform_device *pdev = to_platform_device(dev);
195+
struct at91_reset *reset = platform_get_drvdata(pdev);
196+
197+
return sprintf(buf, "%s\n", at91_reset_reason(reset));
198+
}
199+
static DEVICE_ATTR_RO(power_on_reason);
200+
190201
static const struct of_device_id at91_ramc_of_match[] = {
191202
{
192203
.compatible = "atmel,at91sam9260-sdramc",
@@ -391,6 +402,12 @@ static int __init at91_reset_probe(struct platform_device *pdev)
391402
if (ret)
392403
goto disable_clk;
393404

405+
ret = device_create_file(&pdev->dev, &dev_attr_power_on_reason);
406+
if (ret) {
407+
dev_err(&pdev->dev, "Could not create sysfs entry\n");
408+
return ret;
409+
}
410+
394411
dev_info(&pdev->dev, "Starting after %s\n", at91_reset_reason(reset));
395412

396413
return 0;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 */

0 commit comments

Comments
 (0)