2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2013, NVIDIA Corporation
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/host1x.h>
20 #include <linux/slab.h>
21 #include <linux/of_device.h>
26 static DEFINE_MUTEX(clients_lock);
27 static LIST_HEAD(clients);
29 static DEFINE_MUTEX(drivers_lock);
30 static LIST_HEAD(drivers);
32 static DEFINE_MUTEX(devices_lock);
33 static LIST_HEAD(devices);
35 struct host1x_subdev {
36 struct host1x_client *client;
37 struct device_node *np;
38 struct list_head list;
42 * host1x_subdev_add() - add a new subdevice with an associated device node
44 static int host1x_subdev_add(struct host1x_device *device,
45 struct device_node *np)
47 struct host1x_subdev *subdev;
49 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
53 INIT_LIST_HEAD(&subdev->list);
54 subdev->np = of_node_get(np);
56 mutex_lock(&device->subdevs_lock);
57 list_add_tail(&subdev->list, &device->subdevs);
58 mutex_unlock(&device->subdevs_lock);
64 * host1x_subdev_del() - remove subdevice
66 static void host1x_subdev_del(struct host1x_subdev *subdev)
68 list_del(&subdev->list);
69 of_node_put(subdev->np);
74 * host1x_device_parse_dt() - scan device tree and add matching subdevices
76 static int host1x_device_parse_dt(struct host1x_device *device,
77 struct host1x_driver *driver)
79 struct device_node *np;
82 for_each_child_of_node(device->dev.parent->of_node, np) {
83 if (of_match_node(driver->subdevs, np) &&
84 of_device_is_available(np)) {
85 err = host1x_subdev_add(device, np);
94 static void host1x_subdev_register(struct host1x_device *device,
95 struct host1x_subdev *subdev,
96 struct host1x_client *client)
101 * Move the subdevice to the list of active (registered) subdevices
102 * and associate it with a client. At the same time, associate the
103 * client with its parent device.
105 mutex_lock(&device->subdevs_lock);
106 mutex_lock(&device->clients_lock);
107 list_move_tail(&client->list, &device->clients);
108 list_move_tail(&subdev->list, &device->active);
109 client->parent = &device->dev;
110 subdev->client = client;
111 mutex_unlock(&device->clients_lock);
112 mutex_unlock(&device->subdevs_lock);
114 if (list_empty(&device->subdevs)) {
115 err = device_add(&device->dev);
117 dev_err(&device->dev, "failed to add: %d\n", err);
119 device->registered = true;
123 static void __host1x_subdev_unregister(struct host1x_device *device,
124 struct host1x_subdev *subdev)
126 struct host1x_client *client = subdev->client;
129 * If all subdevices have been activated, we're about to remove the
130 * first active subdevice, so unload the driver first.
132 if (list_empty(&device->subdevs)) {
133 if (device->registered) {
134 device->registered = false;
135 device_del(&device->dev);
140 * Move the subdevice back to the list of idle subdevices and remove
141 * it from list of clients.
143 mutex_lock(&device->clients_lock);
144 subdev->client = NULL;
145 client->parent = NULL;
146 list_move_tail(&subdev->list, &device->subdevs);
148 * XXX: Perhaps don't do this here, but rather explicitly remove it
149 * when the device is about to be deleted.
151 * This is somewhat complicated by the fact that this function is
152 * used to remove the subdevice when a client is unregistered but
153 * also when the composite device is about to be removed.
155 list_del_init(&client->list);
156 mutex_unlock(&device->clients_lock);
159 static void host1x_subdev_unregister(struct host1x_device *device,
160 struct host1x_subdev *subdev)
162 mutex_lock(&device->subdevs_lock);
163 __host1x_subdev_unregister(device, subdev);
164 mutex_unlock(&device->subdevs_lock);
167 int host1x_device_init(struct host1x_device *device)
169 struct host1x_client *client;
172 mutex_lock(&device->clients_lock);
174 list_for_each_entry(client, &device->clients, list) {
175 if (client->ops && client->ops->init) {
176 err = client->ops->init(client);
178 dev_err(&device->dev,
179 "failed to initialize %s: %d\n",
180 dev_name(client->dev), err);
181 mutex_unlock(&device->clients_lock);
187 mutex_unlock(&device->clients_lock);
191 EXPORT_SYMBOL(host1x_device_init);
193 int host1x_device_exit(struct host1x_device *device)
195 struct host1x_client *client;
198 mutex_lock(&device->clients_lock);
200 list_for_each_entry_reverse(client, &device->clients, list) {
201 if (client->ops && client->ops->exit) {
202 err = client->ops->exit(client);
204 dev_err(&device->dev,
205 "failed to cleanup %s: %d\n",
206 dev_name(client->dev), err);
207 mutex_unlock(&device->clients_lock);
213 mutex_unlock(&device->clients_lock);
217 EXPORT_SYMBOL(host1x_device_exit);
219 static int host1x_add_client(struct host1x *host1x,
220 struct host1x_client *client)
222 struct host1x_device *device;
223 struct host1x_subdev *subdev;
225 mutex_lock(&host1x->devices_lock);
227 list_for_each_entry(device, &host1x->devices, list) {
228 list_for_each_entry(subdev, &device->subdevs, list) {
229 if (subdev->np == client->dev->of_node) {
230 host1x_subdev_register(device, subdev, client);
231 mutex_unlock(&host1x->devices_lock);
237 mutex_unlock(&host1x->devices_lock);
241 static int host1x_del_client(struct host1x *host1x,
242 struct host1x_client *client)
244 struct host1x_device *device, *dt;
245 struct host1x_subdev *subdev;
247 mutex_lock(&host1x->devices_lock);
249 list_for_each_entry_safe(device, dt, &host1x->devices, list) {
250 list_for_each_entry(subdev, &device->active, list) {
251 if (subdev->client == client) {
252 host1x_subdev_unregister(device, subdev);
253 mutex_unlock(&host1x->devices_lock);
259 mutex_unlock(&host1x->devices_lock);
263 static int host1x_device_match(struct device *dev, struct device_driver *drv)
265 return strcmp(dev_name(dev), drv->name) == 0;
268 static int host1x_device_probe(struct device *dev)
270 struct host1x_driver *driver = to_host1x_driver(dev->driver);
271 struct host1x_device *device = to_host1x_device(dev);
274 return driver->probe(device);
279 static int host1x_device_remove(struct device *dev)
281 struct host1x_driver *driver = to_host1x_driver(dev->driver);
282 struct host1x_device *device = to_host1x_device(dev);
285 return driver->remove(device);
290 static void host1x_device_shutdown(struct device *dev)
292 struct host1x_driver *driver = to_host1x_driver(dev->driver);
293 struct host1x_device *device = to_host1x_device(dev);
295 if (driver->shutdown)
296 driver->shutdown(device);
299 static const struct dev_pm_ops host1x_device_pm_ops = {
300 .suspend = pm_generic_suspend,
301 .resume = pm_generic_resume,
302 .freeze = pm_generic_freeze,
303 .thaw = pm_generic_thaw,
304 .poweroff = pm_generic_poweroff,
305 .restore = pm_generic_restore,
308 struct bus_type host1x_bus_type = {
310 .match = host1x_device_match,
311 .probe = host1x_device_probe,
312 .remove = host1x_device_remove,
313 .shutdown = host1x_device_shutdown,
314 .pm = &host1x_device_pm_ops,
317 static void __host1x_device_del(struct host1x_device *device)
319 struct host1x_subdev *subdev, *sd;
320 struct host1x_client *client, *cl;
322 mutex_lock(&device->subdevs_lock);
324 /* unregister subdevices */
325 list_for_each_entry_safe(subdev, sd, &device->active, list) {
327 * host1x_subdev_unregister() will remove the client from
328 * any lists, so we'll need to manually add it back to the
329 * list of idle clients.
331 * XXX: Alternatively, perhaps don't remove the client from
332 * any lists in host1x_subdev_unregister() and instead do
333 * that explicitly from host1x_unregister_client()?
335 client = subdev->client;
337 __host1x_subdev_unregister(device, subdev);
339 /* add the client to the list of idle clients */
340 mutex_lock(&clients_lock);
341 list_add_tail(&client->list, &clients);
342 mutex_unlock(&clients_lock);
345 /* remove subdevices */
346 list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
347 host1x_subdev_del(subdev);
349 mutex_unlock(&device->subdevs_lock);
351 /* move clients to idle list */
352 mutex_lock(&clients_lock);
353 mutex_lock(&device->clients_lock);
355 list_for_each_entry_safe(client, cl, &device->clients, list)
356 list_move_tail(&client->list, &clients);
358 mutex_unlock(&device->clients_lock);
359 mutex_unlock(&clients_lock);
361 /* finally remove the device */
362 list_del_init(&device->list);
365 static void host1x_device_release(struct device *dev)
367 struct host1x_device *device = to_host1x_device(dev);
369 __host1x_device_del(device);
373 static int host1x_device_add(struct host1x *host1x,
374 struct host1x_driver *driver)
376 struct host1x_client *client, *tmp;
377 struct host1x_subdev *subdev;
378 struct host1x_device *device;
381 device = kzalloc(sizeof(*device), GFP_KERNEL);
385 device_initialize(&device->dev);
387 mutex_init(&device->subdevs_lock);
388 INIT_LIST_HEAD(&device->subdevs);
389 INIT_LIST_HEAD(&device->active);
390 mutex_init(&device->clients_lock);
391 INIT_LIST_HEAD(&device->clients);
392 INIT_LIST_HEAD(&device->list);
393 device->driver = driver;
395 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
396 device->dev.dma_mask = &device->dev.coherent_dma_mask;
397 dev_set_name(&device->dev, "%s", driver->driver.name);
398 of_dma_configure(&device->dev, host1x->dev->of_node);
399 device->dev.release = host1x_device_release;
400 device->dev.bus = &host1x_bus_type;
401 device->dev.parent = host1x->dev;
403 err = host1x_device_parse_dt(device, driver);
409 list_add_tail(&device->list, &host1x->devices);
411 mutex_lock(&clients_lock);
413 list_for_each_entry_safe(client, tmp, &clients, list) {
414 list_for_each_entry(subdev, &device->subdevs, list) {
415 if (subdev->np == client->dev->of_node) {
416 host1x_subdev_register(device, subdev, client);
422 mutex_unlock(&clients_lock);
428 * Removes a device by first unregistering any subdevices and then removing
429 * itself from the list of devices.
431 * This function must be called with the host1x->devices_lock held.
433 static void host1x_device_del(struct host1x *host1x,
434 struct host1x_device *device)
436 if (device->registered) {
437 device->registered = false;
438 device_del(&device->dev);
441 put_device(&device->dev);
444 static void host1x_attach_driver(struct host1x *host1x,
445 struct host1x_driver *driver)
447 struct host1x_device *device;
450 mutex_lock(&host1x->devices_lock);
452 list_for_each_entry(device, &host1x->devices, list) {
453 if (device->driver == driver) {
454 mutex_unlock(&host1x->devices_lock);
459 err = host1x_device_add(host1x, driver);
461 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
463 mutex_unlock(&host1x->devices_lock);
466 static void host1x_detach_driver(struct host1x *host1x,
467 struct host1x_driver *driver)
469 struct host1x_device *device, *tmp;
471 mutex_lock(&host1x->devices_lock);
473 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
474 if (device->driver == driver)
475 host1x_device_del(host1x, device);
477 mutex_unlock(&host1x->devices_lock);
480 int host1x_register(struct host1x *host1x)
482 struct host1x_driver *driver;
484 mutex_lock(&devices_lock);
485 list_add_tail(&host1x->list, &devices);
486 mutex_unlock(&devices_lock);
488 mutex_lock(&drivers_lock);
490 list_for_each_entry(driver, &drivers, list)
491 host1x_attach_driver(host1x, driver);
493 mutex_unlock(&drivers_lock);
498 int host1x_unregister(struct host1x *host1x)
500 struct host1x_driver *driver;
502 mutex_lock(&drivers_lock);
504 list_for_each_entry(driver, &drivers, list)
505 host1x_detach_driver(host1x, driver);
507 mutex_unlock(&drivers_lock);
509 mutex_lock(&devices_lock);
510 list_del_init(&host1x->list);
511 mutex_unlock(&devices_lock);
516 int host1x_driver_register_full(struct host1x_driver *driver,
517 struct module *owner)
519 struct host1x *host1x;
521 INIT_LIST_HEAD(&driver->list);
523 mutex_lock(&drivers_lock);
524 list_add_tail(&driver->list, &drivers);
525 mutex_unlock(&drivers_lock);
527 mutex_lock(&devices_lock);
529 list_for_each_entry(host1x, &devices, list)
530 host1x_attach_driver(host1x, driver);
532 mutex_unlock(&devices_lock);
534 driver->driver.bus = &host1x_bus_type;
535 driver->driver.owner = owner;
537 return driver_register(&driver->driver);
539 EXPORT_SYMBOL(host1x_driver_register_full);
541 void host1x_driver_unregister(struct host1x_driver *driver)
543 driver_unregister(&driver->driver);
545 mutex_lock(&drivers_lock);
546 list_del_init(&driver->list);
547 mutex_unlock(&drivers_lock);
549 EXPORT_SYMBOL(host1x_driver_unregister);
551 int host1x_client_register(struct host1x_client *client)
553 struct host1x *host1x;
556 mutex_lock(&devices_lock);
558 list_for_each_entry(host1x, &devices, list) {
559 err = host1x_add_client(host1x, client);
561 mutex_unlock(&devices_lock);
566 mutex_unlock(&devices_lock);
568 mutex_lock(&clients_lock);
569 list_add_tail(&client->list, &clients);
570 mutex_unlock(&clients_lock);
574 EXPORT_SYMBOL(host1x_client_register);
576 int host1x_client_unregister(struct host1x_client *client)
578 struct host1x_client *c;
579 struct host1x *host1x;
582 mutex_lock(&devices_lock);
584 list_for_each_entry(host1x, &devices, list) {
585 err = host1x_del_client(host1x, client);
587 mutex_unlock(&devices_lock);
592 mutex_unlock(&devices_lock);
593 mutex_lock(&clients_lock);
595 list_for_each_entry(c, &clients, list) {
597 list_del_init(&c->list);
602 mutex_unlock(&clients_lock);
606 EXPORT_SYMBOL(host1x_client_unregister);