Skip to content

Commit 753e4d5

Browse files
committed
regulator: add under-voltage support (part 2)
Merge series from Oleksij Rempel <o.rempel@pengutronix.de>: This series add under-voltage and emergency shutdown for system critical regulators
2 parents 413cfaa + 1e22152 commit 753e4d5

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

Documentation/devicetree/bindings/regulator/fixed-regulator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ properties:
105105
description:
106106
Interrupt signaling a critical under-voltage event.
107107

108+
system-critical-regulator: true
109+
108110
required:
109111
- compatible
110112
- regulator-name

Documentation/devicetree/bindings/regulator/regulator.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ properties:
114114
description: Enable pull down resistor when the regulator is disabled.
115115
type: boolean
116116

117+
system-critical-regulator:
118+
description: Set if the regulator is critical to system stability or
119+
functionality.
120+
type: boolean
121+
117122
regulator-over-current-protection:
118123
description: Enable over current protection.
119124
type: boolean
@@ -181,6 +186,14 @@ properties:
181186
be enabled but limit setting can be omitted. Limit is given as microvolt
182187
offset from voltage set to regulator.
183188

189+
regulator-uv-less-critical-window-ms:
190+
description: Specifies the time window (in milliseconds) following a
191+
critical under-voltage event during which the system can continue to
192+
operate safely while performing less critical operations. This property
193+
provides a defined duration before a more severe reaction to the
194+
under-voltage event is needed, allowing for certain non-urgent actions to
195+
be carried out in preparation for potential power loss.
196+
184197
regulator-temp-protection-kelvin:
185198
description: Set over temperature protection limit. This is a limit where
186199
hardware performs emergency shutdown. Zero can be passed to disable

drivers/regulator/core.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/delay.h>
2020
#include <linux/gpio/consumer.h>
2121
#include <linux/of.h>
22+
#include <linux/reboot.h>
2223
#include <linux/regmap.h>
2324
#include <linux/regulator/of_regulator.h>
2425
#include <linux/regulator/consumer.h>
@@ -5065,6 +5066,41 @@ void regulator_bulk_free(int num_consumers,
50655066
}
50665067
EXPORT_SYMBOL_GPL(regulator_bulk_free);
50675068

5069+
/**
5070+
* regulator_handle_critical - Handle events for system-critical regulators.
5071+
* @rdev: The regulator device.
5072+
* @event: The event being handled.
5073+
*
5074+
* This function handles critical events such as under-voltage, over-current,
5075+
* and unknown errors for regulators deemed system-critical. On detecting such
5076+
* events, it triggers a hardware protection shutdown with a defined timeout.
5077+
*/
5078+
static void regulator_handle_critical(struct regulator_dev *rdev,
5079+
unsigned long event)
5080+
{
5081+
const char *reason = NULL;
5082+
5083+
if (!rdev->constraints->system_critical)
5084+
return;
5085+
5086+
switch (event) {
5087+
case REGULATOR_EVENT_UNDER_VOLTAGE:
5088+
reason = "System critical regulator: voltage drop detected";
5089+
break;
5090+
case REGULATOR_EVENT_OVER_CURRENT:
5091+
reason = "System critical regulator: over-current detected";
5092+
break;
5093+
case REGULATOR_EVENT_FAIL:
5094+
reason = "System critical regulator: unknown error";
5095+
}
5096+
5097+
if (!reason)
5098+
return;
5099+
5100+
hw_protection_shutdown(reason,
5101+
rdev->constraints->uv_less_critical_window_ms);
5102+
}
5103+
50685104
/**
50695105
* regulator_notifier_call_chain - call regulator event notifier
50705106
* @rdev: regulator source
@@ -5077,6 +5113,8 @@ EXPORT_SYMBOL_GPL(regulator_bulk_free);
50775113
int regulator_notifier_call_chain(struct regulator_dev *rdev,
50785114
unsigned long event, void *data)
50795115
{
5116+
regulator_handle_critical(rdev, event);
5117+
50805118
_notifier_call_chain(rdev, event, data);
50815119
return NOTIFY_DONE;
50825120

drivers/regulator/of_regulator.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ static int of_get_regulation_constraints(struct device *dev,
131131
constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
132132

133133
constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
134+
constraints->system_critical = of_property_read_bool(np,
135+
"system-critical-regulator");
134136

135137
if (of_property_read_bool(np, "regulator-allow-bypass"))
136138
constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
@@ -173,6 +175,13 @@ static int of_get_regulation_constraints(struct device *dev,
173175
if (!ret)
174176
constraints->enable_time = pval;
175177

178+
ret = of_property_read_u32(np, "regulator-uv-survival-time-ms", &pval);
179+
if (!ret)
180+
constraints->uv_less_critical_window_ms = pval;
181+
else
182+
constraints->uv_less_critical_window_ms =
183+
REGULATOR_DEF_UV_LESS_CRITICAL_WINDOW_MS;
184+
176185
constraints->soft_start = of_property_read_bool(np,
177186
"regulator-soft-start");
178187
ret = of_property_read_u32(np, "regulator-active-discharge", &pval);

include/linux/regulator/machine.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ struct regulator;
4949
#define DISABLE_IN_SUSPEND 1
5050
#define ENABLE_IN_SUSPEND 2
5151

52+
/*
53+
* Default time window (in milliseconds) following a critical under-voltage
54+
* event during which less critical actions can be safely carried out by the
55+
* system.
56+
*/
57+
#define REGULATOR_DEF_UV_LESS_CRITICAL_WINDOW_MS 10
58+
5259
/* Regulator active discharge flags */
5360
enum regulator_active_discharge {
5461
REGULATOR_ACTIVE_DISCHARGE_DEFAULT,
@@ -127,6 +134,8 @@ struct notification_limit {
127134
* @ramp_disable: Disable ramp delay when initialising or when setting voltage.
128135
* @soft_start: Enable soft start so that voltage ramps slowly.
129136
* @pull_down: Enable pull down when regulator is disabled.
137+
* @system_critical: Set if the regulator is critical to system stability or
138+
* functionality.
130139
* @over_current_protection: Auto disable on over current event.
131140
*
132141
* @over_current_detection: Configure over current limits.
@@ -153,6 +162,13 @@ struct notification_limit {
153162
* regulator_active_discharge values are used for
154163
* initialisation.
155164
* @enable_time: Turn-on time of the rails (unit: microseconds)
165+
* @uv_less_critical_window_ms: Specifies the time window (in milliseconds)
166+
* following a critical under-voltage (UV) event
167+
* during which less critical actions can be
168+
* safely carried out by the system (for example
169+
* logging). After this time window more critical
170+
* actions should be done (for example prevent
171+
* HW damage).
156172
*/
157173
struct regulation_constraints {
158174

@@ -204,6 +220,7 @@ struct regulation_constraints {
204220
unsigned int settling_time_up;
205221
unsigned int settling_time_down;
206222
unsigned int enable_time;
223+
unsigned int uv_less_critical_window_ms;
207224

208225
unsigned int active_discharge;
209226

@@ -214,6 +231,7 @@ struct regulation_constraints {
214231
unsigned ramp_disable:1; /* disable ramp delay */
215232
unsigned soft_start:1; /* ramp voltage slowly */
216233
unsigned pull_down:1; /* pull down resistor when regulator off */
234+
unsigned system_critical:1; /* critical to system stability */
217235
unsigned over_current_protection:1; /* auto disable on over current */
218236
unsigned over_current_detection:1; /* notify on over current */
219237
unsigned over_voltage_detection:1; /* notify on over voltage */

0 commit comments

Comments
 (0)