6427df5cc1a13cdf4dc94e91013cac674b45df1d
[openwrt.git] / target / linux / sunxi / patches-3.18 / 200-mmc-add-sdio-function-subnode.patch
1 From 8c2057afe84c074ef7cd3ee2ec8e9bed835b9e93 Mon Sep 17 00:00:00 2001
2 From: Sascha Hauer <s.hauer@pengutronix.de>
3 Date: Wed, 21 May 2014 19:50:04 +0200
4 Subject: [PATCH] mmc: Add SDIO function devicetree subnode parsing
5
6 This adds SDIO devicetree subnode parsing to the mmc core. While
7 SDIO devices are runtime probable they sometimes need nonprobable
8 additional information on embedded systems, like an additional gpio
9 interrupt or a clock. This patch makes it possible to supply this
10 information from the devicetree. SDIO drivers will find a pointer
11 to the devicenode in their devices of_node pointer.
12
13 Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
14 [hdegoede@redhat.com: Misc. cleanups]
15 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
16 ---
17  drivers/mmc/core/bus.c      |  4 ++++
18  drivers/mmc/core/core.c     | 28 ++++++++++++++++++++++++++++
19  drivers/mmc/core/core.h     |  3 +++
20  drivers/mmc/core/sdio_bus.c | 11 +++++++++++
21  4 files changed, 46 insertions(+)
22
23 diff --git a/drivers/mmc/core/bus.c b/drivers/mmc/core/bus.c
24 index 8a1f124..7868565 100644
25 --- a/drivers/mmc/core/bus.c
26 +++ b/drivers/mmc/core/bus.c
27 @@ -16,6 +16,7 @@
28  #include <linux/err.h>
29  #include <linux/slab.h>
30  #include <linux/stat.h>
31 +#include <linux/of.h>
32  #include <linux/pm_runtime.h>
33  
34  #include <linux/mmc/card.h>
35 @@ -352,6 +353,8 @@ int mmc_add_card(struct mmc_card *card)
36  #endif
37         mmc_init_context_info(card->host);
38  
39 +       card->dev.of_node = mmc_of_find_child_device(card->host, 0);
40 +
41         ret = device_add(&card->dev);
42         if (ret)
43                 return ret;
44 @@ -380,6 +383,7 @@ void mmc_remove_card(struct mmc_card *card)
45                                 mmc_hostname(card->host), card->rca);
46                 }
47                 device_del(&card->dev);
48 +               of_node_put(card->dev.of_node);
49         }
50  
51         put_device(&card->dev);
52 diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
53 index f26a5f1..7f7f66c 100644
54 --- a/drivers/mmc/core/core.c
55 +++ b/drivers/mmc/core/core.c
56 @@ -1205,6 +1205,34 @@ EXPORT_SYMBOL(mmc_of_parse_voltage);
57  
58  #endif /* CONFIG_OF */
59  
60 +static int mmc_of_get_func_num(struct device_node *node)
61 +{
62 +       u32 reg;
63 +       int ret;
64 +
65 +       ret = of_property_read_u32(node, "reg", &reg);
66 +       if (ret < 0)
67 +               return ret;
68 +
69 +       return reg;
70 +}
71 +
72 +struct device_node *mmc_of_find_child_device(struct mmc_host *host,
73 +               unsigned func_num)
74 +{
75 +       struct device_node *node;
76 +
77 +       if (!host->parent || !host->parent->of_node)
78 +               return NULL;
79 +
80 +       for_each_child_of_node(host->parent->of_node, node) {
81 +               if (mmc_of_get_func_num(node) == func_num)
82 +                       return node;
83 +       }
84 +
85 +       return NULL;
86 +}
87 +
88  #ifdef CONFIG_REGULATOR
89  
90  /**
91 diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h
92 index 443a5846..f712f6e 100644
93 --- a/drivers/mmc/core/core.h
94 +++ b/drivers/mmc/core/core.h
95 @@ -32,6 +32,9 @@ struct mmc_bus_ops {
96  void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
97  void mmc_detach_bus(struct mmc_host *host);
98  
99 +struct device_node *mmc_of_find_child_device(struct mmc_host *host,
100 +               unsigned func_num);
101 +
102  void mmc_init_erase(struct mmc_card *card);
103  
104  void mmc_set_chip_select(struct mmc_host *host, int mode);
105 diff --git a/drivers/mmc/core/sdio_bus.c b/drivers/mmc/core/sdio_bus.c
106 index 6da97b1..f63223a 100644
107 --- a/drivers/mmc/core/sdio_bus.c
108 +++ b/drivers/mmc/core/sdio_bus.c
109 @@ -22,7 +22,9 @@
110  #include <linux/mmc/card.h>
111  #include <linux/mmc/host.h>
112  #include <linux/mmc/sdio_func.h>
113 +#include <linux/of.h>
114  
115 +#include "core.h"
116  #include "sdio_cis.h"
117  #include "sdio_bus.h"
118  
119 @@ -303,6 +305,13 @@ static void sdio_acpi_set_handle(struct sdio_func *func)
120  static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
121  #endif
122  
123 +static void sdio_set_of_node(struct sdio_func *func)
124 +{
125 +       struct mmc_host *host = func->card->host;
126 +
127 +       func->dev.of_node = mmc_of_find_child_device(host, func->num);
128 +}
129 +
130  /*
131   * Register a new SDIO function with the driver model.
132   */
133 @@ -312,6 +321,7 @@ int sdio_add_func(struct sdio_func *func)
134  
135         dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
136  
137 +       sdio_set_of_node(func);
138         sdio_acpi_set_handle(func);
139         ret = device_add(&func->dev);
140         if (ret == 0) {
141 @@ -335,6 +345,7 @@ void sdio_remove_func(struct sdio_func *func)
142  
143         dev_pm_domain_detach(&func->dev, false);
144         device_del(&func->dev);
145 +       of_node_put(func->dev.of_node);
146         put_device(&func->dev);
147  }
148