aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2016-02-23 12:46:26 -0500
committerPhilipp Zabel <p.zabel@pengutronix.de>2016-03-30 09:42:05 -0400
commit0b52297f2288ca239e598afe6c92db83d8d2bfcd (patch)
treeaf0f180666543d4a45bf4c68270edbc3c606a5d9
parentc15ddec2ca06076a11195313aa1fce47d2a28c5d (diff)
reset: Add support for shared reset controls
In some SoCs some hw-blocks share a reset control. Add support for this setup by adding new: reset_control_get_shared() devm_reset_control_get_shared() devm_reset_control_get_shared_by_index() methods to get a reset_control. Note that this patch omits adding of_ variants, if these are needed later they can be easily added. This patch also changes the behavior of the existing exclusive reset_control_get() variants, if these are now called more then once for the same reset_control they will return -EBUSY. To catch existing drivers triggering this error (there should not be any) a WARN_ON(1) is added in this path. When a reset_control is shared, the behavior of reset_control_assert / deassert is changed, for shared reset_controls these will work like the clock-enable/disable and regulator-on/off functions. They will keep a deassert_count, and only (re-)assert the reset after reset_control_assert has been called as many times as reset_control_deassert was called. Calling reset_control_assert without first calling reset_control_deassert is not allowed on a shared reset control. Calling reset_control_reset is also not allowed on a shared reset control. Signed-off-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
-rw-r--r--drivers/reset/core.c59
-rw-r--r--include/linux/reset.h96
2 files changed, 129 insertions, 26 deletions
diff --git a/drivers/reset/core.c b/drivers/reset/core.c
index 957750600ff3..72b32bd15549 100644
--- a/drivers/reset/core.c
+++ b/drivers/reset/core.c
@@ -8,6 +8,7 @@
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 */ 10 */
11#include <linux/atomic.h>
11#include <linux/device.h> 12#include <linux/device.h>
12#include <linux/err.h> 13#include <linux/err.h>
13#include <linux/export.h> 14#include <linux/export.h>
@@ -29,12 +30,16 @@ static LIST_HEAD(reset_controller_list);
29 * @id: ID of the reset controller in the reset 30 * @id: ID of the reset controller in the reset
30 * controller device 31 * controller device
31 * @refcnt: Number of gets of this reset_control 32 * @refcnt: Number of gets of this reset_control
33 * @shared: Is this a shared (1), or an exclusive (0) reset_control?
34 * @deassert_cnt: Number of times this reset line has been deasserted
32 */ 35 */
33struct reset_control { 36struct reset_control {
34 struct reset_controller_dev *rcdev; 37 struct reset_controller_dev *rcdev;
35 struct list_head list; 38 struct list_head list;
36 unsigned int id; 39 unsigned int id;
37 unsigned int refcnt; 40 unsigned int refcnt;
41 int shared;
42 atomic_t deassert_count;
38}; 43};
39 44
40/** 45/**
@@ -91,9 +96,14 @@ EXPORT_SYMBOL_GPL(reset_controller_unregister);
91/** 96/**
92 * reset_control_reset - reset the controlled device 97 * reset_control_reset - reset the controlled device
93 * @rstc: reset controller 98 * @rstc: reset controller
99 *
100 * Calling this on a shared reset controller is an error.
94 */ 101 */
95int reset_control_reset(struct reset_control *rstc) 102int reset_control_reset(struct reset_control *rstc)
96{ 103{
104 if (WARN_ON(rstc->shared))
105 return -EINVAL;
106
97 if (rstc->rcdev->ops->reset) 107 if (rstc->rcdev->ops->reset)
98 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); 108 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
99 109
@@ -104,26 +114,48 @@ EXPORT_SYMBOL_GPL(reset_control_reset);
104/** 114/**
105 * reset_control_assert - asserts the reset line 115 * reset_control_assert - asserts the reset line
106 * @rstc: reset controller 116 * @rstc: reset controller
117 *
118 * Calling this on an exclusive reset controller guarantees that the reset
119 * will be asserted. When called on a shared reset controller the line may
120 * still be deasserted, as long as other users keep it so.
121 *
122 * For shared reset controls a driver cannot expect the hw's registers and
123 * internal state to be reset, but must be prepared for this to happen.
107 */ 124 */
108int reset_control_assert(struct reset_control *rstc) 125int reset_control_assert(struct reset_control *rstc)
109{ 126{
110 if (rstc->rcdev->ops->assert) 127 if (!rstc->rcdev->ops->assert)
111 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); 128 return -ENOTSUPP;
112 129
113 return -ENOTSUPP; 130 if (rstc->shared) {
131 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
132 return -EINVAL;
133
134 if (atomic_dec_return(&rstc->deassert_count) != 0)
135 return 0;
136 }
137
138 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
114} 139}
115EXPORT_SYMBOL_GPL(reset_control_assert); 140EXPORT_SYMBOL_GPL(reset_control_assert);
116 141
117/** 142/**
118 * reset_control_deassert - deasserts the reset line 143 * reset_control_deassert - deasserts the reset line
119 * @rstc: reset controller 144 * @rstc: reset controller
145 *
146 * After calling this function, the reset is guaranteed to be deasserted.
120 */ 147 */
121int reset_control_deassert(struct reset_control *rstc) 148int reset_control_deassert(struct reset_control *rstc)
122{ 149{
123 if (rstc->rcdev->ops->deassert) 150 if (!rstc->rcdev->ops->deassert)
124 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); 151 return -ENOTSUPP;
125 152
126 return -ENOTSUPP; 153 if (rstc->shared) {
154 if (atomic_inc_return(&rstc->deassert_count) != 1)
155 return 0;
156 }
157
158 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
127} 159}
128EXPORT_SYMBOL_GPL(reset_control_deassert); 160EXPORT_SYMBOL_GPL(reset_control_deassert);
129 161
@@ -144,7 +176,7 @@ EXPORT_SYMBOL_GPL(reset_control_status);
144 176
145static struct reset_control *__reset_control_get( 177static struct reset_control *__reset_control_get(
146 struct reset_controller_dev *rcdev, 178 struct reset_controller_dev *rcdev,
147 unsigned int index) 179 unsigned int index, int shared)
148{ 180{
149 struct reset_control *rstc; 181 struct reset_control *rstc;
150 182
@@ -152,6 +184,9 @@ static struct reset_control *__reset_control_get(
152 184
153 list_for_each_entry(rstc, &rcdev->reset_control_head, list) { 185 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
154 if (rstc->id == index) { 186 if (rstc->id == index) {
187 if (WARN_ON(!rstc->shared || !shared))
188 return ERR_PTR(-EBUSY);
189
155 rstc->refcnt++; 190 rstc->refcnt++;
156 return rstc; 191 return rstc;
157 } 192 }
@@ -167,6 +202,7 @@ static struct reset_control *__reset_control_get(
167 list_add(&rstc->list, &rcdev->reset_control_head); 202 list_add(&rstc->list, &rcdev->reset_control_head);
168 rstc->id = index; 203 rstc->id = index;
169 rstc->refcnt = 1; 204 rstc->refcnt = 1;
205 rstc->shared = shared;
170 206
171 return rstc; 207 return rstc;
172} 208}
@@ -185,7 +221,7 @@ static void __reset_control_put(struct reset_control *rstc)
185} 221}
186 222
187struct reset_control *__of_reset_control_get(struct device_node *node, 223struct reset_control *__of_reset_control_get(struct device_node *node,
188 const char *id, int index) 224 const char *id, int index, int shared)
189{ 225{
190 struct reset_control *rstc; 226 struct reset_control *rstc;
191 struct reset_controller_dev *r, *rcdev; 227 struct reset_controller_dev *r, *rcdev;
@@ -235,7 +271,7 @@ struct reset_control *__of_reset_control_get(struct device_node *node,
235 } 271 }
236 272
237 /* reset_list_mutex also protects the rcdev's reset_control list */ 273 /* reset_list_mutex also protects the rcdev's reset_control list */
238 rstc = __reset_control_get(rcdev, rstc_id); 274 rstc = __reset_control_get(rcdev, rstc_id, shared);
239 275
240 mutex_unlock(&reset_list_mutex); 276 mutex_unlock(&reset_list_mutex);
241 277
@@ -265,7 +301,7 @@ static void devm_reset_control_release(struct device *dev, void *res)
265} 301}
266 302
267struct reset_control *__devm_reset_control_get(struct device *dev, 303struct reset_control *__devm_reset_control_get(struct device *dev,
268 const char *id, int index) 304 const char *id, int index, int shared)
269{ 305{
270 struct reset_control **ptr, *rstc; 306 struct reset_control **ptr, *rstc;
271 307
@@ -274,7 +310,8 @@ struct reset_control *__devm_reset_control_get(struct device *dev,
274 if (!ptr) 310 if (!ptr)
275 return ERR_PTR(-ENOMEM); 311 return ERR_PTR(-ENOMEM);
276 312
277 rstc = __of_reset_control_get(dev ? dev->of_node : NULL, id, index); 313 rstc = __of_reset_control_get(dev ? dev->of_node : NULL,
314 id, index, shared);
278 if (!IS_ERR(rstc)) { 315 if (!IS_ERR(rstc)) {
279 *ptr = rstc; 316 *ptr = rstc;
280 devres_add(dev, ptr); 317 devres_add(dev, ptr);
diff --git a/include/linux/reset.h b/include/linux/reset.h
index 1bb69a29d6db..a552134a209e 100644
--- a/include/linux/reset.h
+++ b/include/linux/reset.h
@@ -13,10 +13,10 @@ int reset_control_deassert(struct reset_control *rstc);
13int reset_control_status(struct reset_control *rstc); 13int reset_control_status(struct reset_control *rstc);
14 14
15struct reset_control *__of_reset_control_get(struct device_node *node, 15struct reset_control *__of_reset_control_get(struct device_node *node,
16 const char *id, int index); 16 const char *id, int index, int shared);
17void reset_control_put(struct reset_control *rstc); 17void reset_control_put(struct reset_control *rstc);
18struct reset_control *__devm_reset_control_get(struct device *dev, 18struct reset_control *__devm_reset_control_get(struct device *dev,
19 const char *id, int index); 19 const char *id, int index, int shared);
20 20
21int __must_check device_reset(struct device *dev); 21int __must_check device_reset(struct device *dev);
22 22
@@ -63,14 +63,14 @@ static inline int device_reset_optional(struct device *dev)
63 63
64static inline struct reset_control *__of_reset_control_get( 64static inline struct reset_control *__of_reset_control_get(
65 struct device_node *node, 65 struct device_node *node,
66 const char *id, int index) 66 const char *id, int index, int shared)
67{ 67{
68 return ERR_PTR(-EINVAL); 68 return ERR_PTR(-EINVAL);
69} 69}
70 70
71static inline struct reset_control *__devm_reset_control_get( 71static inline struct reset_control *__devm_reset_control_get(
72 struct device *dev, 72 struct device *dev,
73 const char *id, int index) 73 const char *id, int index, int shared)
74{ 74{
75 return ERR_PTR(-EINVAL); 75 return ERR_PTR(-EINVAL);
76} 76}
@@ -78,11 +78,17 @@ static inline struct reset_control *__devm_reset_control_get(
78#endif /* CONFIG_RESET_CONTROLLER */ 78#endif /* CONFIG_RESET_CONTROLLER */
79 79
80/** 80/**
81 * reset_control_get - Lookup and obtain a reference to a reset controller. 81 * reset_control_get - Lookup and obtain an exclusive reference to a
82 * reset controller.
82 * @dev: device to be reset by the controller 83 * @dev: device to be reset by the controller
83 * @id: reset line name 84 * @id: reset line name
84 * 85 *
85 * Returns a struct reset_control or IS_ERR() condition containing errno. 86 * Returns a struct reset_control or IS_ERR() condition containing errno.
87 * If this function is called more then once for the same reset_control it will
88 * return -EBUSY.
89 *
90 * See reset_control_get_shared for details on shared references to
91 * reset-controls.
86 * 92 *
87 * Use of id names is optional. 93 * Use of id names is optional.
88 */ 94 */
@@ -92,17 +98,46 @@ static inline struct reset_control *__must_check reset_control_get(
92#ifndef CONFIG_RESET_CONTROLLER 98#ifndef CONFIG_RESET_CONTROLLER
93 WARN_ON(1); 99 WARN_ON(1);
94#endif 100#endif
95 return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0); 101 return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0, 0);
96} 102}
97 103
98static inline struct reset_control *reset_control_get_optional( 104static inline struct reset_control *reset_control_get_optional(
99 struct device *dev, const char *id) 105 struct device *dev, const char *id)
100{ 106{
101 return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0); 107 return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0, 0);
102} 108}
103 109
104/** 110/**
105 * of_reset_control_get - Lookup and obtain a reference to a reset controller. 111 * reset_control_get_shared - Lookup and obtain a shared reference to a
112 * reset controller.
113 * @dev: device to be reset by the controller
114 * @id: reset line name
115 *
116 * Returns a struct reset_control or IS_ERR() condition containing errno.
117 * This function is intended for use with reset-controls which are shared
118 * between hardware-blocks.
119 *
120 * When a reset-control is shared, the behavior of reset_control_assert /
121 * deassert is changed, the reset-core will keep track of a deassert_count
122 * and only (re-)assert the reset after reset_control_assert has been called
123 * as many times as reset_control_deassert was called. Also see the remark
124 * about shared reset-controls in the reset_control_assert docs.
125 *
126 * Calling reset_control_assert without first calling reset_control_deassert
127 * is not allowed on a shared reset control. Calling reset_control_reset is
128 * also not allowed on a shared reset control.
129 *
130 * Use of id names is optional.
131 */
132static inline struct reset_control *reset_control_get_shared(
133 struct device *dev, const char *id)
134{
135 return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0, 1);
136}
137
138/**
139 * of_reset_control_get - Lookup and obtain an exclusive reference to a
140 * reset controller.
106 * @node: device to be reset by the controller 141 * @node: device to be reset by the controller
107 * @id: reset line name 142 * @id: reset line name
108 * 143 *
@@ -113,12 +148,12 @@ static inline struct reset_control *reset_control_get_optional(
113static inline struct reset_control *of_reset_control_get( 148static inline struct reset_control *of_reset_control_get(
114 struct device_node *node, const char *id) 149 struct device_node *node, const char *id)
115{ 150{
116 return __of_reset_control_get(node, id, 0); 151 return __of_reset_control_get(node, id, 0, 0);
117} 152}
118 153
119/** 154/**
120 * of_reset_control_get_by_index - Lookup and obtain a reference to a reset 155 * of_reset_control_get_by_index - Lookup and obtain an exclusive reference to
121 * controller by index. 156 * a reset controller by index.
122 * @node: device to be reset by the controller 157 * @node: device to be reset by the controller
123 * @index: index of the reset controller 158 * @index: index of the reset controller
124 * 159 *
@@ -129,7 +164,7 @@ static inline struct reset_control *of_reset_control_get(
129static inline struct reset_control *of_reset_control_get_by_index( 164static inline struct reset_control *of_reset_control_get_by_index(
130 struct device_node *node, int index) 165 struct device_node *node, int index)
131{ 166{
132 return __of_reset_control_get(node, NULL, index); 167 return __of_reset_control_get(node, NULL, index, 0);
133} 168}
134 169
135/** 170/**
@@ -147,13 +182,13 @@ static inline struct reset_control *__must_check devm_reset_control_get(
147#ifndef CONFIG_RESET_CONTROLLER 182#ifndef CONFIG_RESET_CONTROLLER
148 WARN_ON(1); 183 WARN_ON(1);
149#endif 184#endif
150 return __devm_reset_control_get(dev, id, 0); 185 return __devm_reset_control_get(dev, id, 0, 0);
151} 186}
152 187
153static inline struct reset_control *devm_reset_control_get_optional( 188static inline struct reset_control *devm_reset_control_get_optional(
154 struct device *dev, const char *id) 189 struct device *dev, const char *id)
155{ 190{
156 return __devm_reset_control_get(dev, id, 0); 191 return __devm_reset_control_get(dev, id, 0, 0);
157} 192}
158 193
159/** 194/**
@@ -168,7 +203,38 @@ static inline struct reset_control *devm_reset_control_get_optional(
168static inline struct reset_control *devm_reset_control_get_by_index( 203static inline struct reset_control *devm_reset_control_get_by_index(
169 struct device *dev, int index) 204 struct device *dev, int index)
170{ 205{
171 return __devm_reset_control_get(dev, NULL, index); 206 return __devm_reset_control_get(dev, NULL, index, 0);
207}
208
209/**
210 * devm_reset_control_get_shared - resource managed reset_control_get_shared()
211 * @dev: device to be reset by the controller
212 * @id: reset line name
213 *
214 * Managed reset_control_get_shared(). For reset controllers returned from
215 * this function, reset_control_put() is called automatically on driver detach.
216 * See reset_control_get_shared() for more information.
217 */
218static inline struct reset_control *devm_reset_control_get_shared(
219 struct device *dev, const char *id)
220{
221 return __devm_reset_control_get(dev, id, 0, 1);
222}
223
224/**
225 * devm_reset_control_get_shared_by_index - resource managed
226 * reset_control_get_shared
227 * @dev: device to be reset by the controller
228 * @index: index of the reset controller
229 *
230 * Managed reset_control_get_shared(). For reset controllers returned from
231 * this function, reset_control_put() is called automatically on driver detach.
232 * See reset_control_get_shared() for more information.
233 */
234static inline struct reset_control *devm_reset_control_get_shared_by_index(
235 struct device *dev, int index)
236{
237 return __devm_reset_control_get(dev, NULL, index, 1);
172} 238}
173 239
174#endif 240#endif