All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marek Vasut <marek.vasut@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: linux-renesas-soc@vger.kernel.org,
	Marek Vasut <marek.vasut+renesas@gmail.com>,
	Linus Walleij <linus.walleij@linaro.org>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: [PATCH 14/14] gpio: pca953x: Restore registers after suspend/resume cycle
Date: Sun,  2 Dec 2018 20:35:53 +0100	[thread overview]
Message-ID: <20181202193553.29704-14-marek.vasut+renesas@gmail.com> (raw)
In-Reply-To: <20181202193553.29704-1-marek.vasut+renesas@gmail.com>

It is possible that the PCA953x is powered down during suspend.
Use regmap cache to assure the registers in the PCA953x are in
line with the driver state after resume.

Signed-off-by: Marek Vasut <marek.vasut+renesas@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/gpio/gpio-pca953x.c | 92 +++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)

diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index 7c0122fac383..e2c2e97b7321 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -972,6 +972,95 @@ static int pca953x_remove(struct i2c_client *client)
 	return ret;
 }
 
+#ifdef CONFIG_PM_SLEEP
+static int pca953x_regcache_sync(struct device *dev)
+{
+	struct pca953x_chip *chip = dev_get_drvdata(dev);
+	int ret;
+
+	/*
+	 * The ordering between direction and output is important,
+	 * sync these registers first and only then sync the rest.
+	 */
+	ret = regcache_sync_region(chip->regmap, chip->regs->direction,
+				   chip->regs->direction + NBANK(chip));
+	if (ret != 0) {
+		dev_err(dev, "Failed to sync GPIO dir registers: %d\n", ret);
+		return ret;
+	}
+
+	ret = regcache_sync_region(chip->regmap, chip->regs->output,
+				   chip->regs->output + NBANK(chip));
+	if (ret != 0) {
+		dev_err(dev, "Failed to sync GPIO out registers: %d\n", ret);
+		return ret;
+	}
+
+#ifdef CONFIG_GPIO_PCA953X_IRQ
+	if (chip->driver_data & PCA_PCAL) {
+		pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
+		pca953x_write_regs(chip, PCAL953X_INT_MASK, invert_irq_mask);
+
+		ret = regcache_sync_region(chip->regmap, PCAL953X_IN_LATCH,
+					   PCAL953X_IN_LATCH + NBANK(chip));
+		if (ret != 0) {
+			dev_err(dev, "Failed to sync INT latch registers: %d\n",
+				ret);
+			return ret;
+		}
+
+		ret = regcache_sync_region(chip->regmap, PCAL953X_INT_MASK,
+					   PCAL953X_INT_MASK + NBANK(chip));
+		if (ret != 0) {
+			dev_err(dev, "Failed to sync INT mask registers: %d\n",
+				ret);
+			return ret;
+		}
+	}
+#endif
+
+	return 0;
+}
+
+static int pca953x_suspend(struct device *dev)
+{
+	struct pca953x_chip *chip = dev_get_drvdata(dev);
+
+	regcache_mark_dirty(chip->regmap);
+	pca953x_regcache_sync(dev);
+	regcache_cache_only(chip->regmap, true);
+
+	regulator_disable(chip->regulator);
+
+	return 0;
+}
+
+static int pca953x_resume(struct device *dev)
+{
+	struct pca953x_chip *chip = dev_get_drvdata(dev);
+	int ret;
+
+	ret = regulator_enable(chip->regulator);
+	if (ret != 0) {
+		dev_err(dev, "Failed to enable regulator: %d\n", ret);
+		return 0;
+	}
+
+	regcache_cache_only(chip->regmap, false);
+	ret = pca953x_regcache_sync(dev);
+	if (ret)
+		return ret;
+
+	ret = regcache_sync(chip->regmap);
+	if (ret != 0) {
+		dev_err(dev, "Failed to restore register map: %d\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+#endif
+
 /* convenience to stop overlong match-table lines */
 #define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
 #define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
@@ -1015,9 +1104,12 @@ static const struct of_device_id pca953x_dt_ids[] = {
 
 MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
 
+static SIMPLE_DEV_PM_OPS(pca953x_pm_ops, pca953x_suspend, pca953x_resume);
+
 static struct i2c_driver pca953x_driver = {
 	.driver = {
 		.name	= "pca953x",
+		.pm	= &pca953x_pm_ops,
 		.of_match_table = pca953x_dt_ids,
 		.acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
 	},
-- 
2.18.0

  parent reply	other threads:[~2018-12-02 19:36 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-02 19:35 [PATCH 01/14] gpio: pca953x: Deduplicate the bank_size Marek Vasut
2018-12-02 19:35 ` [PATCH 02/14] gpio: pca953x: Fix AI overflow on PCAL6524 Marek Vasut
2018-12-03  9:37   ` Bartosz Golaszewski
2018-12-02 19:35 ` [PATCH 03/14] gpio: pca953x: Repair multi-byte IO address increment on PCA9575 Marek Vasut
2018-12-03 10:09   ` Geert Uytterhoeven
2018-12-02 19:35 ` [PATCH 04/14] gpio: pca953x: Unify pca95{3,7}x_write_regs_16() Marek Vasut
2018-12-02 19:35 ` [PATCH 05/14] gpio: pca953x: Unify pca953x_{read,write}_regs_{16,24}() Marek Vasut
2018-12-02 19:35 ` [PATCH 06/14] gpio: pca953x: Unify pca953x_{read,write}_regs_{8,mul}() Marek Vasut
2018-12-02 19:35 ` [PATCH 07/14] gpio: pca953x: Factor out common code from device_pca95xx_init() Marek Vasut
2018-12-02 19:35 ` [PATCH 08/14] gpio: pca953x: Zap ad-hoc I2C block write in multi GPIO set Marek Vasut
2018-12-02 19:35 ` [PATCH 09/14] gpio: pca953x: Extract the register address mangling to single function Marek Vasut
2018-12-02 19:35 ` [PATCH 10/14] gpio: pca953x: Perform basic regmap conversion Marek Vasut
2018-12-02 19:35 ` [PATCH 11/14] gpio: pca953x: Zap ad-hoc reg_direction cache Marek Vasut
2018-12-02 19:35 ` [PATCH 12/14] gpio: pca953x: Zap ad-hoc reg_output cache Marek Vasut
2018-12-02 19:35 ` [PATCH 13/14] gpio: pca953x: Zap single use of pca953x_read_single() Marek Vasut
2018-12-02 19:35 ` Marek Vasut [this message]
2018-12-03 17:55   ` [PATCH 14/14] gpio: pca953x: Restore registers after suspend/resume cycle Geert Uytterhoeven
2018-12-03 21:42     ` Marek Vasut
2018-12-18 12:57     ` Geert Uytterhoeven
2018-12-18 13:43       ` Geert Uytterhoeven
2018-12-05 14:39   ` Geert Uytterhoeven
2018-12-05 14:45     ` Marek Vasut
2018-12-03  9:36 ` [PATCH 01/14] gpio: pca953x: Deduplicate the bank_size Bartosz Golaszewski
2018-12-03 11:00   ` Marek Vasut

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181202193553.29704-14-marek.vasut+renesas@gmail.com \
    --to=marek.vasut@gmail.com \
    --cc=bgolaszewski@baylibre.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=marek.vasut+renesas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.