rtc: honor device tree /alias entries when assigning IDs
Stephen Warren [Thu, 23 Jan 2014 23:55:14 +0000 (15:55 -0800)]
Assign RTC device IDs based on device tree /aliases entries if present,
falling back to the existing numbering scheme if there is no /aliases
entry (which includes when the system isn't booted using DT), or there is
a numbering conflict.

This is useful in systems with multiple RTC devices, to ensure that the
best RTC device is selected as /dev/rtc0, which provides the overall
system time.

For example, Tegra has an on-SoC RTC that is not battery backed, typically
coupled with an off-SoC RTC that is battery backed.  Only the latter is
useful for populating the system time, yet the former is useful e.g.  for
wakeup timing, since the time is not lost when the system is sleeps.

Change-Id: Id8b925fb7a8e1b80487a8e246cdc4ce4654abfbe
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit 9d2b7e532da8aadfcc1bd85b62ec5dd853e870e3 in mainline v3.14)
Signed-off-by: Krishna Yarlagadda <kyarlagadda@nvidia.com>
Reviewed-on: http://git-master/r/926525
Reviewed-by: Laxman Dewangan <ldewangan@nvidia.com>

drivers/rtc/class.c

index bb2f9aa..b75922e 100644 (file)
@@ -14,6 +14,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/module.h>
+#include <linux/of.h>
 #include <linux/rtc.h>
 #include <linux/kdev_t.h>
 #include <linux/idr.h>
@@ -156,12 +157,27 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
 {
        struct rtc_device *rtc;
        struct rtc_wkalrm alrm;
-       int id, err;
+       int of_id = -1, id = -1, err;
+
+       if (dev->of_node)
+               of_id = of_alias_get_id(dev->of_node, "rtc");
+       else if (dev->parent && dev->parent->of_node)
+               of_id = of_alias_get_id(dev->parent->of_node, "rtc");
+
+       if (of_id >= 0) {
+               id = ida_simple_get(&rtc_ida, of_id, of_id + 1,
+                                   GFP_KERNEL);
+               if (id < 0)
+                       dev_warn(dev, "/aliases ID %d not available\n",
+                                   of_id);
+       }
 
-       id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
        if (id < 0) {
-               err = id;
-               goto exit;
+               id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
+               if (id < 0) {
+                       err = id;
+                       goto exit;
+               }
        }
 
        rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);