Commit | Line | Data |
---|---|---|
61fc4131 PZ |
1 | /* |
2 | * Reset Controller framework | |
3 | * | |
4 | * Copyright 2013 Philipp Zabel, Pengutronix | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | #include <linux/device.h> | |
12 | #include <linux/err.h> | |
13 | #include <linux/export.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/of.h> | |
17 | #include <linux/reset.h> | |
18 | #include <linux/reset-controller.h> | |
19 | #include <linux/slab.h> | |
20 | ||
21 | static DEFINE_MUTEX(reset_controller_list_mutex); | |
22 | static LIST_HEAD(reset_controller_list); | |
23 | ||
24 | /** | |
25 | * struct reset_control - a reset control | |
26 | * @rcdev: a pointer to the reset controller device | |
27 | * this reset control belongs to | |
28 | * @id: ID of the reset controller in the reset | |
29 | * controller device | |
30 | */ | |
31 | struct reset_control { | |
32 | struct reset_controller_dev *rcdev; | |
33 | struct device *dev; | |
34 | unsigned int id; | |
35 | }; | |
36 | ||
37 | /** | |
38 | * of_reset_simple_xlate - translate reset_spec to the reset line number | |
39 | * @rcdev: a pointer to the reset controller device | |
40 | * @reset_spec: reset line specifier as found in the device tree | |
41 | * @flags: a flags pointer to fill in (optional) | |
42 | * | |
43 | * This simple translation function should be used for reset controllers | |
44 | * with 1:1 mapping, where reset lines can be indexed by number without gaps. | |
45 | */ | |
0c5b2b91 | 46 | static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, |
61fc4131 PZ |
47 | const struct of_phandle_args *reset_spec) |
48 | { | |
49 | if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells)) | |
50 | return -EINVAL; | |
51 | ||
52 | if (reset_spec->args[0] >= rcdev->nr_resets) | |
53 | return -EINVAL; | |
54 | ||
55 | return reset_spec->args[0]; | |
56 | } | |
61fc4131 PZ |
57 | |
58 | /** | |
59 | * reset_controller_register - register a reset controller device | |
60 | * @rcdev: a pointer to the initialized reset controller device | |
61 | */ | |
62 | int reset_controller_register(struct reset_controller_dev *rcdev) | |
63 | { | |
64 | if (!rcdev->of_xlate) { | |
65 | rcdev->of_reset_n_cells = 1; | |
66 | rcdev->of_xlate = of_reset_simple_xlate; | |
67 | } | |
68 | ||
69 | mutex_lock(&reset_controller_list_mutex); | |
70 | list_add(&rcdev->list, &reset_controller_list); | |
71 | mutex_unlock(&reset_controller_list_mutex); | |
72 | ||
73 | return 0; | |
74 | } | |
75 | EXPORT_SYMBOL_GPL(reset_controller_register); | |
76 | ||
77 | /** | |
78 | * reset_controller_unregister - unregister a reset controller device | |
79 | * @rcdev: a pointer to the reset controller device | |
80 | */ | |
81 | void reset_controller_unregister(struct reset_controller_dev *rcdev) | |
82 | { | |
83 | mutex_lock(&reset_controller_list_mutex); | |
84 | list_del(&rcdev->list); | |
85 | mutex_unlock(&reset_controller_list_mutex); | |
86 | } | |
87 | EXPORT_SYMBOL_GPL(reset_controller_unregister); | |
88 | ||
89 | /** | |
90 | * reset_control_reset - reset the controlled device | |
91 | * @rstc: reset controller | |
92 | */ | |
93 | int reset_control_reset(struct reset_control *rstc) | |
94 | { | |
95 | if (rstc->rcdev->ops->reset) | |
96 | return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); | |
97 | ||
98 | return -ENOSYS; | |
99 | } | |
100 | EXPORT_SYMBOL_GPL(reset_control_reset); | |
101 | ||
102 | /** | |
103 | * reset_control_assert - asserts the reset line | |
104 | * @rstc: reset controller | |
105 | */ | |
106 | int reset_control_assert(struct reset_control *rstc) | |
107 | { | |
108 | if (rstc->rcdev->ops->assert) | |
109 | return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); | |
110 | ||
111 | return -ENOSYS; | |
112 | } | |
113 | EXPORT_SYMBOL_GPL(reset_control_assert); | |
114 | ||
115 | /** | |
116 | * reset_control_deassert - deasserts the reset line | |
117 | * @rstc: reset controller | |
118 | */ | |
119 | int reset_control_deassert(struct reset_control *rstc) | |
120 | { | |
121 | if (rstc->rcdev->ops->deassert) | |
122 | return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); | |
123 | ||
124 | return -ENOSYS; | |
125 | } | |
126 | EXPORT_SYMBOL_GPL(reset_control_deassert); | |
127 | ||
128 | /** | |
129 | * reset_control_get - Lookup and obtain a reference to a reset controller. | |
130 | * @dev: device to be reset by the controller | |
131 | * @id: reset line name | |
132 | * | |
133 | * Returns a struct reset_control or IS_ERR() condition containing errno. | |
134 | * | |
135 | * Use of id names is optional. | |
136 | */ | |
137 | struct reset_control *reset_control_get(struct device *dev, const char *id) | |
138 | { | |
139 | struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER); | |
140 | struct reset_controller_dev *r, *rcdev; | |
141 | struct of_phandle_args args; | |
142 | int index = 0; | |
143 | int rstc_id; | |
144 | int ret; | |
145 | ||
146 | if (!dev) | |
147 | return ERR_PTR(-EINVAL); | |
148 | ||
149 | if (id) | |
150 | index = of_property_match_string(dev->of_node, | |
151 | "reset-names", id); | |
152 | ret = of_parse_phandle_with_args(dev->of_node, "resets", "#reset-cells", | |
153 | index, &args); | |
154 | if (ret) | |
155 | return ERR_PTR(ret); | |
156 | ||
157 | mutex_lock(&reset_controller_list_mutex); | |
158 | rcdev = NULL; | |
159 | list_for_each_entry(r, &reset_controller_list, list) { | |
160 | if (args.np == r->of_node) { | |
161 | rcdev = r; | |
162 | break; | |
163 | } | |
164 | } | |
165 | of_node_put(args.np); | |
166 | ||
167 | if (!rcdev) { | |
168 | mutex_unlock(&reset_controller_list_mutex); | |
3d103020 | 169 | return ERR_PTR(-EPROBE_DEFER); |
61fc4131 PZ |
170 | } |
171 | ||
172 | rstc_id = rcdev->of_xlate(rcdev, &args); | |
173 | if (rstc_id < 0) { | |
174 | mutex_unlock(&reset_controller_list_mutex); | |
175 | return ERR_PTR(rstc_id); | |
176 | } | |
177 | ||
178 | try_module_get(rcdev->owner); | |
179 | mutex_unlock(&reset_controller_list_mutex); | |
180 | ||
181 | rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); | |
182 | if (!rstc) { | |
6034bb22 | 183 | module_put(rcdev->owner); |
61fc4131 PZ |
184 | return ERR_PTR(-ENOMEM); |
185 | } | |
186 | ||
187 | rstc->dev = dev; | |
188 | rstc->rcdev = rcdev; | |
189 | rstc->id = rstc_id; | |
190 | ||
191 | return rstc; | |
192 | } | |
193 | EXPORT_SYMBOL_GPL(reset_control_get); | |
194 | ||
195 | /** | |
196 | * reset_control_put - free the reset controller | |
197 | * @rstc: reset controller | |
198 | */ | |
199 | ||
200 | void reset_control_put(struct reset_control *rstc) | |
201 | { | |
202 | if (IS_ERR(rstc)) | |
203 | return; | |
204 | ||
205 | module_put(rstc->rcdev->owner); | |
206 | kfree(rstc); | |
207 | } | |
208 | EXPORT_SYMBOL_GPL(reset_control_put); | |
209 | ||
210 | static void devm_reset_control_release(struct device *dev, void *res) | |
211 | { | |
212 | reset_control_put(*(struct reset_control **)res); | |
213 | } | |
214 | ||
215 | /** | |
216 | * devm_reset_control_get - resource managed reset_control_get() | |
217 | * @dev: device to be reset by the controller | |
218 | * @id: reset line name | |
219 | * | |
220 | * Managed reset_control_get(). For reset controllers returned from this | |
221 | * function, reset_control_put() is called automatically on driver detach. | |
222 | * See reset_control_get() for more information. | |
223 | */ | |
224 | struct reset_control *devm_reset_control_get(struct device *dev, const char *id) | |
225 | { | |
226 | struct reset_control **ptr, *rstc; | |
227 | ||
228 | ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), | |
229 | GFP_KERNEL); | |
230 | if (!ptr) | |
231 | return ERR_PTR(-ENOMEM); | |
232 | ||
233 | rstc = reset_control_get(dev, id); | |
234 | if (!IS_ERR(rstc)) { | |
235 | *ptr = rstc; | |
236 | devres_add(dev, ptr); | |
237 | } else { | |
238 | devres_free(ptr); | |
239 | } | |
240 | ||
241 | return rstc; | |
242 | } | |
243 | EXPORT_SYMBOL_GPL(devm_reset_control_get); | |
244 | ||
61fc4131 PZ |
245 | /** |
246 | * device_reset - find reset controller associated with the device | |
247 | * and perform reset | |
248 | * @dev: device to be reset by the controller | |
249 | * | |
250 | * Convenience wrapper for reset_control_get() and reset_control_reset(). | |
251 | * This is useful for the common case of devices with single, dedicated reset | |
252 | * lines. | |
253 | */ | |
254 | int device_reset(struct device *dev) | |
255 | { | |
256 | struct reset_control *rstc; | |
257 | int ret; | |
258 | ||
259 | rstc = reset_control_get(dev, NULL); | |
260 | if (IS_ERR(rstc)) | |
261 | return PTR_ERR(rstc); | |
262 | ||
263 | ret = reset_control_reset(rstc); | |
264 | ||
265 | reset_control_put(rstc); | |
266 | ||
267 | return ret; | |
268 | } | |
269 | EXPORT_SYMBOL_GPL(device_reset); |