PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / base / component.c
blobc4778995cd728047d83a284d2e9248bbde89389f
1 /*
2 * Componentized device handling.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This is work in progress. We gather up the component devices into a list,
9 * and bind them when instructed. At the moment, we're specific to the DRM
10 * subsystem, and only handles one master device, but this doesn't have to be
11 * the case.
13 #include <linux/component.h>
14 #include <linux/device.h>
15 #include <linux/kref.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
21 struct master {
22 struct list_head node;
23 struct list_head components;
24 bool bound;
26 const struct component_master_ops *ops;
27 struct device *dev;
30 struct component {
31 struct list_head node;
32 struct list_head master_node;
33 struct master *master;
34 bool bound;
36 const struct component_ops *ops;
37 struct device *dev;
40 static DEFINE_MUTEX(component_mutex);
41 static LIST_HEAD(component_list);
42 static LIST_HEAD(masters);
44 static struct master *__master_find(struct device *dev,
45 const struct component_master_ops *ops)
47 struct master *m;
49 list_for_each_entry(m, &masters, node)
50 if (m->dev == dev && (!ops || m->ops == ops))
51 return m;
53 return NULL;
56 /* Attach an unattached component to a master. */
57 static void component_attach_master(struct master *master, struct component *c)
59 c->master = master;
61 list_add_tail(&c->master_node, &master->components);
64 /* Detach a component from a master. */
65 static void component_detach_master(struct master *master, struct component *c)
67 list_del(&c->master_node);
69 c->master = NULL;
72 int component_master_add_child(struct master *master,
73 int (*compare)(struct device *, void *), void *compare_data)
75 struct component *c;
76 int ret = -ENXIO;
78 list_for_each_entry(c, &component_list, node) {
79 if (c->master)
80 continue;
82 if (compare(c->dev, compare_data)) {
83 component_attach_master(master, c);
84 ret = 0;
85 break;
89 return ret;
91 EXPORT_SYMBOL_GPL(component_master_add_child);
93 /* Detach all attached components from this master */
94 static void master_remove_components(struct master *master)
96 while (!list_empty(&master->components)) {
97 struct component *c = list_first_entry(&master->components,
98 struct component, master_node);
100 WARN_ON(c->master != master);
102 component_detach_master(master, c);
107 * Try to bring up a master. If component is NULL, we're interested in
108 * this master, otherwise it's a component which must be present to try
109 * and bring up the master.
111 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
113 static int try_to_bring_up_master(struct master *master,
114 struct component *component)
116 int ret = 0;
118 if (!master->bound) {
120 * Search the list of components, looking for components that
121 * belong to this master, and attach them to the master.
123 if (master->ops->add_components(master->dev, master)) {
124 /* Failed to find all components */
125 master_remove_components(master);
126 ret = 0;
127 goto out;
130 if (component && component->master != master) {
131 master_remove_components(master);
132 ret = 0;
133 goto out;
136 if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
137 ret = -ENOMEM;
138 goto out;
141 /* Found all components */
142 ret = master->ops->bind(master->dev);
143 if (ret < 0) {
144 devres_release_group(master->dev, NULL);
145 dev_info(master->dev, "master bind failed: %d\n", ret);
146 master_remove_components(master);
147 goto out;
150 master->bound = true;
151 ret = 1;
153 out:
155 return ret;
158 static int try_to_bring_up_masters(struct component *component)
160 struct master *m;
161 int ret = 0;
163 list_for_each_entry(m, &masters, node) {
164 ret = try_to_bring_up_master(m, component);
165 if (ret != 0)
166 break;
169 return ret;
172 static void take_down_master(struct master *master)
174 if (master->bound) {
175 master->ops->unbind(master->dev);
176 devres_release_group(master->dev, NULL);
177 master->bound = false;
180 master_remove_components(master);
183 int component_master_add(struct device *dev,
184 const struct component_master_ops *ops)
186 struct master *master;
187 int ret;
189 master = kzalloc(sizeof(*master), GFP_KERNEL);
190 if (!master)
191 return -ENOMEM;
193 master->dev = dev;
194 master->ops = ops;
195 INIT_LIST_HEAD(&master->components);
197 /* Add to the list of available masters. */
198 mutex_lock(&component_mutex);
199 list_add(&master->node, &masters);
201 ret = try_to_bring_up_master(master, NULL);
203 if (ret < 0) {
204 /* Delete off the list if we weren't successful */
205 list_del(&master->node);
206 kfree(master);
208 mutex_unlock(&component_mutex);
210 return ret < 0 ? ret : 0;
212 EXPORT_SYMBOL_GPL(component_master_add);
214 void component_master_del(struct device *dev,
215 const struct component_master_ops *ops)
217 struct master *master;
219 mutex_lock(&component_mutex);
220 master = __master_find(dev, ops);
221 if (master) {
222 take_down_master(master);
224 list_del(&master->node);
225 kfree(master);
227 mutex_unlock(&component_mutex);
229 EXPORT_SYMBOL_GPL(component_master_del);
231 static void component_unbind(struct component *component,
232 struct master *master, void *data)
234 WARN_ON(!component->bound);
236 component->ops->unbind(component->dev, master->dev, data);
237 component->bound = false;
239 /* Release all resources claimed in the binding of this component */
240 devres_release_group(component->dev, component);
243 void component_unbind_all(struct device *master_dev, void *data)
245 struct master *master;
246 struct component *c;
248 WARN_ON(!mutex_is_locked(&component_mutex));
250 master = __master_find(master_dev, NULL);
251 if (!master)
252 return;
254 list_for_each_entry_reverse(c, &master->components, master_node)
255 component_unbind(c, master, data);
257 EXPORT_SYMBOL_GPL(component_unbind_all);
259 static int component_bind(struct component *component, struct master *master,
260 void *data)
262 int ret;
265 * Each component initialises inside its own devres group.
266 * This allows us to roll-back a failed component without
267 * affecting anything else.
269 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
270 return -ENOMEM;
273 * Also open a group for the device itself: this allows us
274 * to release the resources claimed against the sub-device
275 * at the appropriate moment.
277 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
278 devres_release_group(master->dev, NULL);
279 return -ENOMEM;
282 dev_dbg(master->dev, "binding %s (ops %ps)\n",
283 dev_name(component->dev), component->ops);
285 ret = component->ops->bind(component->dev, master->dev, data);
286 if (!ret) {
287 component->bound = true;
290 * Close the component device's group so that resources
291 * allocated in the binding are encapsulated for removal
292 * at unbind. Remove the group on the DRM device as we
293 * can clean those resources up independently.
295 devres_close_group(component->dev, NULL);
296 devres_remove_group(master->dev, NULL);
298 dev_info(master->dev, "bound %s (ops %ps)\n",
299 dev_name(component->dev), component->ops);
300 } else {
301 devres_release_group(component->dev, NULL);
302 devres_release_group(master->dev, NULL);
304 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
305 dev_name(component->dev), component->ops, ret);
308 return ret;
311 int component_bind_all(struct device *master_dev, void *data)
313 struct master *master;
314 struct component *c;
315 int ret = 0;
317 WARN_ON(!mutex_is_locked(&component_mutex));
319 master = __master_find(master_dev, NULL);
320 if (!master)
321 return -EINVAL;
323 list_for_each_entry(c, &master->components, master_node) {
324 ret = component_bind(c, master, data);
325 if (ret)
326 break;
329 if (ret != 0) {
330 list_for_each_entry_continue_reverse(c, &master->components,
331 master_node)
332 component_unbind(c, master, data);
335 return ret;
337 EXPORT_SYMBOL_GPL(component_bind_all);
339 int component_add(struct device *dev, const struct component_ops *ops)
341 struct component *component;
342 int ret;
344 component = kzalloc(sizeof(*component), GFP_KERNEL);
345 if (!component)
346 return -ENOMEM;
348 component->ops = ops;
349 component->dev = dev;
351 dev_dbg(dev, "adding component (ops %ps)\n", ops);
353 mutex_lock(&component_mutex);
354 list_add_tail(&component->node, &component_list);
356 ret = try_to_bring_up_masters(component);
357 if (ret < 0) {
358 list_del(&component->node);
360 kfree(component);
362 mutex_unlock(&component_mutex);
364 return ret < 0 ? ret : 0;
366 EXPORT_SYMBOL_GPL(component_add);
368 void component_del(struct device *dev, const struct component_ops *ops)
370 struct component *c, *component = NULL;
372 mutex_lock(&component_mutex);
373 list_for_each_entry(c, &component_list, node)
374 if (c->dev == dev && c->ops == ops) {
375 list_del(&c->node);
376 component = c;
377 break;
380 if (component && component->master)
381 take_down_master(component->master);
383 mutex_unlock(&component_mutex);
385 WARN_ON(!component);
386 kfree(component);
388 EXPORT_SYMBOL_GPL(component_del);
390 MODULE_LICENSE("GPL v2");