[PATCH v2 2/2] regulator: bd9571mwv: Add support for toggle power switches

From: Geert Uytterhoeven
Date: Tue Jun 19 2018 - 09:55:53 EST


Extend the existing support for backup mode to toggle power switches.
With a toggle power switch (or level signal), the following steps must
be followed exactly:
1. Configure PMIC for backup mode,
2. Switch accessory power switch off, to prepare for system suspend,
which is a manual step not controlled by software,
3. Suspend system,
4. Switch accessory power switch on to wake up system.

Hence the PMIC is configured for backup mode when "enabled" is written
to the PMIC's "wakeup" virtual file in sysfs, using the newly introduced
callback for wake-up change notification. Conversely, writing
"disabled" reverts the role of the accessory switch to a power switch.

Unlike with momentary switches, backup mode is not enabled by default,
as enabling it prevents the board from being powered off using the power
switch, which may confuse the user.

Signed-off-by: Geert Uytterhoeven <geert+renesas@xxxxxxxxx>
---
v2:
- Improve patch description,
- Drop "return;" at end of function.
---
drivers/regulator/bd9571mwv-regulator.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/drivers/regulator/bd9571mwv-regulator.c b/drivers/regulator/bd9571mwv-regulator.c
index be574eb444ebda97..403bc3fa1f2513de 100644
--- a/drivers/regulator/bd9571mwv-regulator.c
+++ b/drivers/regulator/bd9571mwv-regulator.c
@@ -171,6 +171,24 @@ static int bd9571mwv_bkup_mode_write(struct bd9571mwv *bd, unsigned int mode)
return 0;
}

+static void bd9571mwv_wakeup_change_notify(struct device *dev, bool enable)
+{
+ struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
+
+ unsigned int mode;
+ int ret;
+
+ ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
+ if (ret)
+ return;
+
+ mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
+ if (enable)
+ mode |= bdreg->bkup_mode_cnt_keepon;
+
+ bd9571mwv_bkup_mode_write(bdreg->bd, mode);
+}
+
static int bd9571mwv_suspend(struct device *dev)
{
struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
@@ -277,6 +295,11 @@ static int bd9571mwv_regulator_probe(struct platform_device *pdev)
* explicit user setup in level mode.
*/
device_set_wakeup_enable(&pdev->dev, bdreg->rstbmode_pulse);
+#ifdef CONFIG_PM_SLEEP
+ if (bdreg->rstbmode_level)
+ pdev->dev.power.wakeup_change_notify =
+ bd9571mwv_wakeup_change_notify;
+#endif /* CONFIG_PM_SLEEP */
}

return 0;
--
2.17.1