devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: "Alessandro Zummo" <a.zummo@towertech.it>,
	"Tony Lindgren" <tony@atomide.com>,
	"Benoît Cousson" <bcousson@baylibre.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Felipe Balbi <balbi@ti.com>, Lokesh Vutla <lokeshvutla@ti.com>,
	Guenter Roeck <linux@roeck-us.net>,
	Colin Foe-Parker <colin.foeparker@logicpd.com>,
	AnilKumar Ch <anilkumar@ti.com>,
	nsekhar@ti.com, t-kristo@ti.com, j-keerthy@ti.com,
	linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	devicetree@vger.kernel.org, rtc-linux@googlegroups.com,
	linux-kernel@vger.kernel.org, Johan Hovold <johan@kernel.org>
Subject: [PATCH 09/12] rtc: omap: add support for pmic_power_en
Date: Thu,  9 Oct 2014 21:06:31 +0200	[thread overview]
Message-ID: <1412881594-25678-10-git-send-email-johan@kernel.org> (raw)
In-Reply-To: <1412881594-25678-1-git-send-email-johan@kernel.org>

Add new property "ti,system-power-controller" to register the RTC as a
power-off handler.

Some RTC IP revisions can control an external PMIC via the pmic_power_en
pin, which can be configured to transition to OFF on ALARM2 events and
back to ON on subsequent ALARM (wakealarm) events.

This is based on earlier work by Colin Foe-Parker and AnilKumar Ch. [1]

[1] https://www.mail-archive.com/linux-omap@vger.kernel.org/msg82127.html

Signed-off-by: Johan Hovold <johan@kernel.org>
---
 Documentation/devicetree/bindings/rtc/rtc-omap.txt |   9 +-
 drivers/rtc/rtc-omap.c                             | 107 ++++++++++++++++++++-
 2 files changed, 110 insertions(+), 6 deletions(-)

diff --git a/Documentation/devicetree/bindings/rtc/rtc-omap.txt b/Documentation/devicetree/bindings/rtc/rtc-omap.txt
index 5a0f02d34d95..750efd40c72e 100644
--- a/Documentation/devicetree/bindings/rtc/rtc-omap.txt
+++ b/Documentation/devicetree/bindings/rtc/rtc-omap.txt
@@ -5,11 +5,17 @@ Required properties:
 	- "ti,da830-rtc"  - for RTC IP used similar to that on DA8xx SoC family.
 	- "ti,am3352-rtc" - for RTC IP used similar to that on AM335x SoC family.
 			    This RTC IP has special WAKE-EN Register to enable
-			    Wakeup generation for event Alarm.
+			    Wakeup generation for event Alarm. It can also be
+			    used to control an external PMIC via the
+			    pmic_power_en pin.
 - reg: Address range of rtc register set
 - interrupts: rtc timer, alarm interrupts in order
 - interrupt-parent: phandle for the interrupt controller
 
+Optional properties:
+- ti,system-power-controller: whether the rtc is controlling the system power
+  through pmic_power_en
+
 Example:
 
 rtc@1c23000 {
@@ -18,4 +24,5 @@ rtc@1c23000 {
 	interrupts = <19
 		      19>;
 	interrupt-parent = <&intc>;
+	ti,system-power-controller;
 };
diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c
index 62e2e9a9887a..b0319864dd42 100644
--- a/drivers/rtc/rtc-omap.c
+++ b/drivers/rtc/rtc-omap.c
@@ -70,6 +70,15 @@
 
 #define OMAP_RTC_IRQWAKEEN		0x7c
 
+#define OMAP_RTC_ALARM2_SECONDS_REG	0x80
+#define OMAP_RTC_ALARM2_MINUTES_REG	0x84
+#define OMAP_RTC_ALARM2_HOURS_REG	0x88
+#define OMAP_RTC_ALARM2_DAYS_REG	0x8c
+#define OMAP_RTC_ALARM2_MONTHS_REG	0x90
+#define OMAP_RTC_ALARM2_YEARS_REG	0x94
+
+#define OMAP_RTC_PMIC_REG		0x98
+
 /* OMAP_RTC_CTRL_REG bit fields: */
 #define OMAP_RTC_CTRL_SPLIT		BIT(7)
 #define OMAP_RTC_CTRL_DISABLE		BIT(6)
@@ -82,6 +91,7 @@
 
 /* OMAP_RTC_STATUS_REG bit fields: */
 #define OMAP_RTC_STATUS_POWER_UP	BIT(7)
+#define OMAP_RTC_STATUS_ALARM2		BIT(7)
 #define OMAP_RTC_STATUS_ALARM		BIT(6)
 #define OMAP_RTC_STATUS_1D_EVENT	BIT(5)
 #define OMAP_RTC_STATUS_1H_EVENT	BIT(4)
@@ -91,6 +101,7 @@
 #define OMAP_RTC_STATUS_BUSY		BIT(0)
 
 /* OMAP_RTC_INTERRUPTS_REG bit fields: */
+#define OMAP_RTC_INTERRUPTS_IT_ALARM2	BIT(4)
 #define OMAP_RTC_INTERRUPTS_IT_ALARM	BIT(3)
 #define OMAP_RTC_INTERRUPTS_IT_TIMER	BIT(2)
 
@@ -100,6 +111,9 @@
 /* OMAP_RTC_IRQWAKEEN bit fields: */
 #define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN	BIT(1)
 
+/* OMAP_RTC_PMIC bit fields: */
+#define OMAP_RTC_PMIC_POWER_EN_EN	BIT(16)
+
 /* OMAP_RTC_KICKER values */
 #define	KICK0_VALUE			0x83e70b13
 #define	KICK1_VALUE			0x95a4f1e0
@@ -124,11 +138,18 @@
  */
 #define OMAP_RTC_HAS_POWER_UP_RESET	BIT(3)
 
+/*
+ * Some RTC IP revisions can control an external PMIC via the pmic_power_en
+ * pin.
+ */
+#define OMAP_RTC_HAS_PMIC_MODE		BIT(4)
+
 static void __iomem	*rtc_base;
 
 #define rtc_read(addr)		readb(rtc_base + (addr))
 #define rtc_write(val, addr)	writeb(val, rtc_base + (addr))
 
+#define rtc_readl(addr)		readl(rtc_base + (addr))
 #define rtc_writel(val, addr)	writel(val, rtc_base + (addr))
 
 
@@ -338,6 +359,61 @@ static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
 	return 0;
 }
 
+static struct platform_device *omap_rtc_power_off_dev;
+
+/*
+ * omap_rtc_poweroff: RTC-controlled power off
+ *
+ * The RTC can be used to control an external PMIC via the pmic_power_en pin,
+ * which can be configured to transition to OFF on ALARM2 events.
+ *
+ * Notes:
+ * The two-second alarm offset is the shortest offset possible as the alarm
+ * registers must be set before the next timer update and the offset
+ * calculation is to heavy for everything to be done within a single access
+ * period (~15us).
+ *
+ * Called with local interrupts disabled.
+ */
+static void omap_rtc_power_off(void)
+{
+	struct rtc_time tm;
+	unsigned long now;
+	u32 val;
+
+	/* enable pmic_power_en control */
+	val = rtc_readl(OMAP_RTC_PMIC_REG);
+	rtc_writel(val | OMAP_RTC_PMIC_POWER_EN_EN, OMAP_RTC_PMIC_REG);
+
+	/* set alarm two seconds from now */
+	omap_rtc_read_time(&omap_rtc_power_off_dev->dev, &tm);
+	rtc_tm_to_time(&tm, &now);
+	rtc_time_to_tm(now + 2, &tm);
+
+	if (tm2bcd(&tm) < 0) {
+		pr_err("%s - power off failed\n", __func__);
+		return;
+	}
+
+	rtc_wait_not_busy();
+
+	rtc_write(tm.tm_sec, OMAP_RTC_ALARM2_SECONDS_REG);
+	rtc_write(tm.tm_min, OMAP_RTC_ALARM2_MINUTES_REG);
+	rtc_write(tm.tm_hour, OMAP_RTC_ALARM2_HOURS_REG);
+	rtc_write(tm.tm_mday, OMAP_RTC_ALARM2_DAYS_REG);
+	rtc_write(tm.tm_mon, OMAP_RTC_ALARM2_MONTHS_REG);
+	rtc_write(tm.tm_year, OMAP_RTC_ALARM2_YEARS_REG);
+
+	/*
+	 * enable ALARM2 interrupt
+	 *
+	 * NOTE: this fails on AM3352 if rtc_write (writeb) is used
+	 */
+	val = rtc_read(OMAP_RTC_INTERRUPTS_REG);
+	rtc_writel(val | OMAP_RTC_INTERRUPTS_IT_ALARM2,
+			OMAP_RTC_INTERRUPTS_REG);
+}
+
 static struct rtc_class_ops omap_rtc_ops = {
 	.read_time	= omap_rtc_read_time,
 	.set_time	= omap_rtc_set_time,
@@ -360,7 +436,7 @@ static struct platform_device_id omap_rtc_devtype[] = {
 	[OMAP_RTC_DATA_AM3352_IDX] = {
 		.name	= "am3352-rtc",
 		.driver_data = OMAP_RTC_HAS_KICKER | OMAP_RTC_HAS_IRQWAKEEN |
-			       OMAP_RTC_HAS_32KCLK_EN,
+			       OMAP_RTC_HAS_32KCLK_EN | OMAP_RTC_HAS_PMIC_MODE,
 	},
 	[OMAP_RTC_DATA_DA830_IDX] = {
 		.name	= "da830-rtc",
@@ -386,12 +462,16 @@ static int __init omap_rtc_probe(struct platform_device *pdev)
 	struct resource		*res;
 	struct rtc_device	*rtc;
 	u8			reg, mask, new_ctrl;
+	bool			pm_off = false;
 	const struct platform_device_id *id_entry;
 	const struct of_device_id *of_id;
 
 	of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
-	if (of_id)
+	if (of_id) {
 		pdev->id_entry = of_id->data;
+		pm_off = of_property_read_bool(pdev->dev.of_node,
+				"ti,system-power-controller");
+	}
 
 	id_entry = platform_get_device_id(pdev);
 	if (!id_entry) {
@@ -425,10 +505,12 @@ static int __init omap_rtc_probe(struct platform_device *pdev)
 		rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG);
 	}
 
-	/* clear pending irqs, and set 1/second periodic,
-	 * which we'll use instead of update irqs
+	/*
+	 * disable interrupts
+	 *
+	 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
 	 */
-	rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
+	rtc_writel(0, OMAP_RTC_INTERRUPTS_REG);
 
 	/* enable RTC functional clock */
 	if (id_entry->driver_data & OMAP_RTC_HAS_32KCLK_EN) {
@@ -440,6 +522,8 @@ static int __init omap_rtc_probe(struct platform_device *pdev)
 	reg = rtc_read(OMAP_RTC_STATUS_REG);
 
 	mask = OMAP_RTC_STATUS_ALARM;
+	if (id_entry->driver_data & OMAP_RTC_HAS_PMIC_MODE)
+		mask |= OMAP_RTC_STATUS_ALARM2;
 	if (id_entry->driver_data & OMAP_RTC_HAS_POWER_UP_RESET) {
 		mask |= OMAP_RTC_STATUS_POWER_UP;
 		if (reg & OMAP_RTC_STATUS_POWER_UP)
@@ -502,6 +586,13 @@ static int __init omap_rtc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, rtc);
 
+	if (id_entry->driver_data & OMAP_RTC_HAS_PMIC_MODE) {
+		if (pm_off && !pm_power_off) {
+			omap_rtc_power_off_dev = pdev;
+			pm_power_off = omap_rtc_power_off;
+		}
+	}
+
 	return 0;
 
 fail0:
@@ -518,6 +609,12 @@ static int __exit omap_rtc_remove(struct platform_device *pdev)
 	const struct platform_device_id *id_entry =
 				platform_get_device_id(pdev);
 
+	if (pm_power_off == omap_rtc_power_off &&
+			omap_rtc_power_off_dev == pdev) {
+		pm_power_off = NULL;
+		omap_rtc_power_off_dev = NULL;
+	}
+
 	device_init_wakeup(&pdev->dev, 0);
 
 	/* leave rtc running, but disable irqs */
-- 
2.0.4

  parent reply	other threads:[~2014-10-09 19:06 UTC|newest]

Thread overview: 113+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-09 19:06 [PATCH 00/12] rtc: omap: fixes and power-off feature Johan Hovold
2014-10-09 19:06 ` [PATCH 01/12] rtc: omap: fix clock-source configuration Johan Hovold
2014-10-10 17:55   ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 02/12] rtc: omap: fix missing wakealarm attribute Johan Hovold
2014-10-10 17:55   ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 03/12] rtc: omap: fix class-device registration Johan Hovold
2014-10-10 17:59   ` Felipe Balbi
2014-10-11  9:59     ` Johan Hovold
2014-10-13 15:57       ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 04/12] rtc: omap: remove unused register-base define Johan Hovold
2014-10-10 17:59   ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 05/12] rtc: omap: remove redundant debug message Johan Hovold
2014-10-10 17:59   ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 06/12] rtc: omap: use dev_info and dev_dbg Johan Hovold
2014-10-10 18:00   ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 07/12] rtc: omap: silence bogus power-up reset message at probe Johan Hovold
2014-10-10 18:00   ` Felipe Balbi
2014-10-10 18:02     ` Felipe Balbi
2014-10-11 10:20       ` Johan Hovold
2014-10-12  0:50         ` Felipe Balbi
2014-10-12 18:42           ` Johan Hovold
2014-10-13 15:56             ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 08/12] rtc: omap: restore irq state after reading TC registers Johan Hovold
2014-10-10 18:02   ` Felipe Balbi
2014-10-11 10:12     ` Johan Hovold
2014-10-12  0:47       ` Felipe Balbi
2014-10-22 10:50         ` Johan Hovold
2014-10-23 18:52           ` Felipe Balbi
2014-10-09 19:06 ` Johan Hovold [this message]
2014-10-10 18:07   ` [PATCH 09/12] rtc: omap: add support for pmic_power_en Felipe Balbi
2014-10-11 10:31     ` Johan Hovold
2014-10-22 10:18     ` Johan Hovold
2014-10-23 18:52       ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 10/12] rtc: omap: enable wake-up from power off Johan Hovold
2014-10-10 18:08   ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 11/12] ARM: dts: am33xx: update rtc node compatible property Johan Hovold
2014-10-10 18:08   ` Felipe Balbi
2014-10-09 19:06 ` [PATCH 12/12] ARM: dts: am335x-bone-common: enable power off and rtc wake up Johan Hovold
2014-10-10 18:09   ` Felipe Balbi
2014-10-11 10:34     ` Johan Hovold
2014-10-10 17:54 ` [PATCH 00/12] rtc: omap: fixes and power-off feature Felipe Balbi
2014-10-11  9:38   ` Johan Hovold
2014-10-11 19:08     ` Tony Lindgren
2014-10-22 11:07       ` Johan Hovold
2014-10-22 15:29         ` Tony Lindgren
2014-10-22 16:21           ` Johan Hovold
2014-10-22 16:43             ` Tony Lindgren
2014-10-15 16:55 ` Felipe Balbi
2014-10-15 17:06   ` Johan Hovold
2014-10-15 17:08     ` Felipe Balbi
2014-10-22 10:23       ` Johan Hovold
2014-10-23 18:55         ` Felipe Balbi
2014-10-24  7:58           ` Johan Hovold
2014-10-21 17:37 ` [PATCH v2 00/20] " Johan Hovold
     [not found]   ` <1413913086-12730-1-git-send-email-johan-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2014-10-21 17:37     ` [PATCH v2 01/20] rtc: omap: fix clock-source configuration Johan Hovold
2014-10-21 17:37   ` [PATCH v2 02/20] rtc: omap: fix missing wakealarm attribute Johan Hovold
2014-10-21 17:37   ` [PATCH v2 03/20] rtc: omap: fix interrupt disable at probe Johan Hovold
2014-10-21 17:37   ` [PATCH v2 04/20] rtc: omap: clean up probe error handling Johan Hovold
2014-10-21 17:37   ` [PATCH v2 05/20] rtc: omap: fix class-device registration Johan Hovold
2014-10-21 17:37   ` [PATCH v2 06/20] rtc: omap: remove unused register-base define Johan Hovold
2014-10-21 17:37   ` [PATCH v2 07/20] rtc: omap: use dev_info Johan Hovold
2014-10-21 17:37   ` [PATCH v2 08/20] rtc: omap: make platform-device id table const Johan Hovold
2014-10-21 17:37   ` [PATCH v2 09/20] rtc: omap: add device abstraction Johan Hovold
2014-10-21 17:37   ` [PATCH v2 10/20] rtc: omap: remove DRIVER_NAME macro Johan Hovold
2014-10-21 17:37   ` [PATCH v2 11/20] rtc: omap: add structured device-type info Johan Hovold
2014-10-21 17:37   ` [PATCH v2 12/20] rtc: omap: silence bogus power-up reset message at probe Johan Hovold
2014-10-21 17:37   ` [PATCH v2 13/20] rtc: omap: add helper to read raw bcd time Johan Hovold
2014-10-21 17:38   ` [PATCH v2 14/20] rtc: omap: add helper to read 32-bit registers Johan Hovold
2014-10-21 17:38   ` [PATCH v2 15/20] rtc: omap: add support for pmic_power_en Johan Hovold
2014-10-21 17:38   ` [PATCH v2 16/20] rtc: omap: enable wake-up from power off Johan Hovold
2014-10-21 17:38   ` [PATCH v2 17/20] rtc: omap: fix minor coding style issues Johan Hovold
2014-10-21 17:38   ` [PATCH v2 18/20] rtc: omap: add copyright entry Johan Hovold
2014-10-21 17:38   ` [PATCH v2 19/20] ARM: dts: am33xx: update rtc-node compatible property Johan Hovold
2014-10-21 17:38   ` [PATCH v2 20/20] ARM: dts: am335x-boneblack: enable power off and rtc wake up Johan Hovold
2014-10-24 16:08   ` [PATCH v2 00/20] rtc: omap: fixes and power-off feature Felipe Balbi
2014-10-24 19:02     ` Johan Hovold
2014-10-24 19:25       ` Felipe Balbi
2014-10-24 19:29         ` Felipe Balbi
2014-10-24 19:36           ` Johan Hovold
2014-10-24 19:44             ` Felipe Balbi
2014-10-24 19:55               ` Johan Hovold
2014-10-24 20:08                 ` Felipe Balbi
2014-10-27 23:22                 ` Andrew Morton
2014-10-28  0:25                   ` Russell King - ARM Linux
     [not found]                     ` <20141028002552.GX12379-l+eeeJia6m9vn6HldHNs0ANdhmdF6hFW@public.gmane.org>
2014-10-28  8:16                       ` Johan Hovold
2014-10-28  8:47                         ` Russell King - ARM Linux
2014-10-28 13:12                           ` Johan Hovold
2014-10-28 15:16                             ` Russell King - ARM Linux
2014-10-29 12:34                               ` Johan Hovold
2014-10-29 12:55                                 ` Romain Perier
     [not found]                                   ` <CABgxDo+ewxNwb3RrBVZA7kqYMvSujLb9U6A6FCvnHQH1W8V0kg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-10-29 13:00                                     ` Johan Hovold
2014-10-29 13:11                                       ` Romain Perier
2014-10-29 13:44                                         ` Johan Hovold
2014-10-29 13:10                                 ` Russell King - ARM Linux
2014-10-29 13:22                                   ` Johan Hovold
2014-10-29 15:25                                     ` Guenter Roeck
2014-10-29 15:51                                       ` Johan Hovold
2014-10-30 10:01                                         ` Johan Hovold
2014-10-29 13:20                                 ` Guenter Roeck
2014-10-29 13:35                                   ` Johan Hovold
2014-10-29 15:36                                     ` Guenter Roeck
2014-10-29 15:54                                       ` Johan Hovold
2014-10-27  8:09   ` [PATCH v3] rtc: omap: add support for pmic_power_en Johan Hovold
2014-10-27 16:45     ` Felipe Balbi
2014-10-27 16:56       ` Johan Hovold
2014-10-27 17:09         ` Felipe Balbi
2014-10-27 22:40     ` Andrew Morton
     [not found]       ` <20141027154031.4492ea11d401045ca04a3ff8-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
2014-10-28  8:36         ` Johan Hovold
2014-10-28 21:18           ` Andrew Morton
2014-10-29 12:46             ` Johan Hovold
2014-10-29 12:50           ` Johan Hovold
2014-10-29 19:14             ` Andrew Morton
2014-10-30  9:55   ` [PATCH v4] " Johan Hovold

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=1412881594-25678-10-git-send-email-johan@kernel.org \
    --to=johan@kernel.org \
    --cc=a.zummo@towertech.it \
    --cc=akpm@linux-foundation.org \
    --cc=anilkumar@ti.com \
    --cc=balbi@ti.com \
    --cc=bcousson@baylibre.com \
    --cc=colin.foeparker@logicpd.com \
    --cc=devicetree@vger.kernel.org \
    --cc=j-keerthy@ti.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=lokeshvutla@ti.com \
    --cc=nsekhar@ti.com \
    --cc=rtc-linux@googlegroups.com \
    --cc=t-kristo@ti.com \
    --cc=tony@atomide.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).