blob: f9cde03030fd9042bea5a4528854b786cdd0451a [file] [log] [blame]
Thierry Reding776dc382013-10-14 14:43:22 +02001/*
2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2013, NVIDIA Corporation
4 *
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.
8 *
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
12 * more details.
13 *
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/>.
16 */
17
18#include <linux/host1x.h>
19#include <linux/of.h>
20#include <linux/slab.h>
Alexandre Courbotc95469a2016-02-26 18:06:53 +090021#include <linux/of_device.h>
Thierry Reding776dc382013-10-14 14:43:22 +020022
Thierry Redingd24b2892013-11-08 13:20:23 +010023#include "bus.h"
Thierry Reding776dc382013-10-14 14:43:22 +020024#include "dev.h"
25
26static DEFINE_MUTEX(clients_lock);
27static LIST_HEAD(clients);
28
29static DEFINE_MUTEX(drivers_lock);
30static LIST_HEAD(drivers);
31
32static DEFINE_MUTEX(devices_lock);
33static LIST_HEAD(devices);
34
35struct host1x_subdev {
36 struct host1x_client *client;
37 struct device_node *np;
38 struct list_head list;
39};
40
41/**
42 * host1x_subdev_add() - add a new subdevice with an associated device node
Thierry Reding466749f2017-04-10 12:27:01 +020043 * @device: host1x device to add the subdevice to
Thierry Reding466749f2017-04-10 12:27:01 +020044 * @np: device node
Thierry Reding776dc382013-10-14 14:43:22 +020045 */
46static int host1x_subdev_add(struct host1x_device *device,
Thierry Reding25ae30d2017-08-15 15:46:22 +020047 struct host1x_driver *driver,
Thierry Reding776dc382013-10-14 14:43:22 +020048 struct device_node *np)
49{
50 struct host1x_subdev *subdev;
Thierry Reding25ae30d2017-08-15 15:46:22 +020051 struct device_node *child;
52 int err;
Thierry Reding776dc382013-10-14 14:43:22 +020053
54 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
55 if (!subdev)
56 return -ENOMEM;
57
58 INIT_LIST_HEAD(&subdev->list);
59 subdev->np = of_node_get(np);
60
61 mutex_lock(&device->subdevs_lock);
62 list_add_tail(&subdev->list, &device->subdevs);
63 mutex_unlock(&device->subdevs_lock);
64
Thierry Reding25ae30d2017-08-15 15:46:22 +020065 /* recursively add children */
66 for_each_child_of_node(np, child) {
67 if (of_match_node(driver->subdevs, child) &&
68 of_device_is_available(child)) {
69 err = host1x_subdev_add(device, driver, child);
70 if (err < 0) {
71 /* XXX cleanup? */
72 of_node_put(child);
73 return err;
74 }
75 }
76 }
77
Thierry Reding776dc382013-10-14 14:43:22 +020078 return 0;
79}
80
81/**
82 * host1x_subdev_del() - remove subdevice
Thierry Reding466749f2017-04-10 12:27:01 +020083 * @subdev: subdevice to remove
Thierry Reding776dc382013-10-14 14:43:22 +020084 */
85static void host1x_subdev_del(struct host1x_subdev *subdev)
86{
87 list_del(&subdev->list);
88 of_node_put(subdev->np);
89 kfree(subdev);
90}
91
92/**
93 * host1x_device_parse_dt() - scan device tree and add matching subdevices
Thierry Reding466749f2017-04-10 12:27:01 +020094 * @device: host1x logical device
95 * @driver: host1x driver
Thierry Reding776dc382013-10-14 14:43:22 +020096 */
Thierry Redingf4c5cf82014-12-18 15:29:14 +010097static int host1x_device_parse_dt(struct host1x_device *device,
98 struct host1x_driver *driver)
Thierry Reding776dc382013-10-14 14:43:22 +020099{
100 struct device_node *np;
101 int err;
102
103 for_each_child_of_node(device->dev.parent->of_node, np) {
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100104 if (of_match_node(driver->subdevs, np) &&
Thierry Reding776dc382013-10-14 14:43:22 +0200105 of_device_is_available(np)) {
Thierry Reding25ae30d2017-08-15 15:46:22 +0200106 err = host1x_subdev_add(device, driver, np);
Amitoj Kaur Chawla93ec3022016-01-24 22:02:10 +0530107 if (err < 0) {
108 of_node_put(np);
Thierry Reding776dc382013-10-14 14:43:22 +0200109 return err;
Amitoj Kaur Chawla93ec3022016-01-24 22:02:10 +0530110 }
Thierry Reding776dc382013-10-14 14:43:22 +0200111 }
112 }
113
114 return 0;
115}
116
117static void host1x_subdev_register(struct host1x_device *device,
118 struct host1x_subdev *subdev,
119 struct host1x_client *client)
120{
121 int err;
122
123 /*
124 * Move the subdevice to the list of active (registered) subdevices
125 * and associate it with a client. At the same time, associate the
126 * client with its parent device.
127 */
128 mutex_lock(&device->subdevs_lock);
129 mutex_lock(&device->clients_lock);
130 list_move_tail(&client->list, &device->clients);
131 list_move_tail(&subdev->list, &device->active);
132 client->parent = &device->dev;
133 subdev->client = client;
134 mutex_unlock(&device->clients_lock);
135 mutex_unlock(&device->subdevs_lock);
136
Thierry Reding776dc382013-10-14 14:43:22 +0200137 if (list_empty(&device->subdevs)) {
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100138 err = device_add(&device->dev);
Thierry Reding776dc382013-10-14 14:43:22 +0200139 if (err < 0)
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100140 dev_err(&device->dev, "failed to add: %d\n", err);
Thierry Reding536e1712014-11-05 11:43:26 +0100141 else
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100142 device->registered = true;
Thierry Reding776dc382013-10-14 14:43:22 +0200143 }
144}
145
146static void __host1x_subdev_unregister(struct host1x_device *device,
147 struct host1x_subdev *subdev)
148{
149 struct host1x_client *client = subdev->client;
Thierry Reding776dc382013-10-14 14:43:22 +0200150
151 /*
152 * If all subdevices have been activated, we're about to remove the
153 * first active subdevice, so unload the driver first.
154 */
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100155 if (list_empty(&device->subdevs)) {
156 if (device->registered) {
157 device->registered = false;
158 device_del(&device->dev);
159 }
Thierry Reding776dc382013-10-14 14:43:22 +0200160 }
161
162 /*
163 * Move the subdevice back to the list of idle subdevices and remove
164 * it from list of clients.
165 */
166 mutex_lock(&device->clients_lock);
167 subdev->client = NULL;
168 client->parent = NULL;
169 list_move_tail(&subdev->list, &device->subdevs);
170 /*
171 * XXX: Perhaps don't do this here, but rather explicitly remove it
172 * when the device is about to be deleted.
173 *
174 * This is somewhat complicated by the fact that this function is
175 * used to remove the subdevice when a client is unregistered but
176 * also when the composite device is about to be removed.
177 */
178 list_del_init(&client->list);
179 mutex_unlock(&device->clients_lock);
180}
181
182static void host1x_subdev_unregister(struct host1x_device *device,
183 struct host1x_subdev *subdev)
184{
185 mutex_lock(&device->subdevs_lock);
186 __host1x_subdev_unregister(device, subdev);
187 mutex_unlock(&device->subdevs_lock);
188}
189
Thierry Reding466749f2017-04-10 12:27:01 +0200190/**
191 * host1x_device_init() - initialize a host1x logical device
192 * @device: host1x logical device
193 *
194 * The driver for the host1x logical device can call this during execution of
195 * its &host1x_driver.probe implementation to initialize each of its clients.
196 * The client drivers access the subsystem specific driver data using the
197 * &host1x_client.parent field and driver data associated with it (usually by
198 * calling dev_get_drvdata()).
199 */
Thierry Reding776dc382013-10-14 14:43:22 +0200200int host1x_device_init(struct host1x_device *device)
201{
202 struct host1x_client *client;
203 int err;
204
205 mutex_lock(&device->clients_lock);
206
207 list_for_each_entry(client, &device->clients, list) {
208 if (client->ops && client->ops->init) {
209 err = client->ops->init(client);
210 if (err < 0) {
211 dev_err(&device->dev,
212 "failed to initialize %s: %d\n",
213 dev_name(client->dev), err);
214 mutex_unlock(&device->clients_lock);
215 return err;
216 }
217 }
218 }
219
220 mutex_unlock(&device->clients_lock);
221
222 return 0;
223}
Thierry Redingfae798a2013-11-08 11:41:42 +0100224EXPORT_SYMBOL(host1x_device_init);
Thierry Reding776dc382013-10-14 14:43:22 +0200225
Thierry Reding466749f2017-04-10 12:27:01 +0200226/**
227 * host1x_device_exit() - uninitialize host1x logical device
228 * @device: host1x logical device
229 *
230 * When the driver for a host1x logical device is unloaded, it can call this
231 * function to tear down each of its clients. Typically this is done after a
232 * subsystem-specific data structure is removed and the functionality can no
233 * longer be used.
234 */
Thierry Reding776dc382013-10-14 14:43:22 +0200235int host1x_device_exit(struct host1x_device *device)
236{
237 struct host1x_client *client;
238 int err;
239
240 mutex_lock(&device->clients_lock);
241
242 list_for_each_entry_reverse(client, &device->clients, list) {
243 if (client->ops && client->ops->exit) {
244 err = client->ops->exit(client);
245 if (err < 0) {
246 dev_err(&device->dev,
247 "failed to cleanup %s: %d\n",
248 dev_name(client->dev), err);
249 mutex_unlock(&device->clients_lock);
250 return err;
251 }
252 }
253 }
254
255 mutex_unlock(&device->clients_lock);
256
257 return 0;
258}
Thierry Redingfae798a2013-11-08 11:41:42 +0100259EXPORT_SYMBOL(host1x_device_exit);
Thierry Reding776dc382013-10-14 14:43:22 +0200260
Thierry Reding0c7dfd32014-05-22 11:12:17 +0200261static int host1x_add_client(struct host1x *host1x,
262 struct host1x_client *client)
Thierry Reding776dc382013-10-14 14:43:22 +0200263{
264 struct host1x_device *device;
265 struct host1x_subdev *subdev;
266
267 mutex_lock(&host1x->devices_lock);
268
269 list_for_each_entry(device, &host1x->devices, list) {
270 list_for_each_entry(subdev, &device->subdevs, list) {
271 if (subdev->np == client->dev->of_node) {
272 host1x_subdev_register(device, subdev, client);
273 mutex_unlock(&host1x->devices_lock);
274 return 0;
275 }
276 }
277 }
278
279 mutex_unlock(&host1x->devices_lock);
280 return -ENODEV;
281}
282
Thierry Reding0c7dfd32014-05-22 11:12:17 +0200283static int host1x_del_client(struct host1x *host1x,
284 struct host1x_client *client)
Thierry Reding776dc382013-10-14 14:43:22 +0200285{
286 struct host1x_device *device, *dt;
287 struct host1x_subdev *subdev;
288
289 mutex_lock(&host1x->devices_lock);
290
291 list_for_each_entry_safe(device, dt, &host1x->devices, list) {
292 list_for_each_entry(subdev, &device->active, list) {
293 if (subdev->client == client) {
294 host1x_subdev_unregister(device, subdev);
295 mutex_unlock(&host1x->devices_lock);
296 return 0;
297 }
298 }
299 }
300
301 mutex_unlock(&host1x->devices_lock);
302 return -ENODEV;
303}
304
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100305static int host1x_device_match(struct device *dev, struct device_driver *drv)
306{
307 return strcmp(dev_name(dev), drv->name) == 0;
308}
309
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100310static const struct dev_pm_ops host1x_device_pm_ops = {
311 .suspend = pm_generic_suspend,
312 .resume = pm_generic_resume,
313 .freeze = pm_generic_freeze,
314 .thaw = pm_generic_thaw,
315 .poweroff = pm_generic_poweroff,
316 .restore = pm_generic_restore,
Thierry Reding776dc382013-10-14 14:43:22 +0200317};
318
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100319struct bus_type host1x_bus_type = {
320 .name = "host1x",
321 .match = host1x_device_match,
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100322 .pm = &host1x_device_pm_ops,
323};
Thierry Reding776dc382013-10-14 14:43:22 +0200324
Thierry Reding99d2cd82014-12-18 15:26:02 +0100325static void __host1x_device_del(struct host1x_device *device)
326{
327 struct host1x_subdev *subdev, *sd;
328 struct host1x_client *client, *cl;
329
330 mutex_lock(&device->subdevs_lock);
331
332 /* unregister subdevices */
333 list_for_each_entry_safe(subdev, sd, &device->active, list) {
334 /*
335 * host1x_subdev_unregister() will remove the client from
336 * any lists, so we'll need to manually add it back to the
337 * list of idle clients.
338 *
339 * XXX: Alternatively, perhaps don't remove the client from
340 * any lists in host1x_subdev_unregister() and instead do
341 * that explicitly from host1x_unregister_client()?
342 */
343 client = subdev->client;
344
345 __host1x_subdev_unregister(device, subdev);
346
347 /* add the client to the list of idle clients */
348 mutex_lock(&clients_lock);
349 list_add_tail(&client->list, &clients);
350 mutex_unlock(&clients_lock);
351 }
352
353 /* remove subdevices */
354 list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
355 host1x_subdev_del(subdev);
356
357 mutex_unlock(&device->subdevs_lock);
358
359 /* move clients to idle list */
360 mutex_lock(&clients_lock);
361 mutex_lock(&device->clients_lock);
362
363 list_for_each_entry_safe(client, cl, &device->clients, list)
364 list_move_tail(&client->list, &clients);
365
366 mutex_unlock(&device->clients_lock);
367 mutex_unlock(&clients_lock);
368
369 /* finally remove the device */
370 list_del_init(&device->list);
371}
372
Thierry Reding776dc382013-10-14 14:43:22 +0200373static void host1x_device_release(struct device *dev)
374{
375 struct host1x_device *device = to_host1x_device(dev);
376
Thierry Reding99d2cd82014-12-18 15:26:02 +0100377 __host1x_device_del(device);
Thierry Reding776dc382013-10-14 14:43:22 +0200378 kfree(device);
379}
380
381static int host1x_device_add(struct host1x *host1x,
382 struct host1x_driver *driver)
383{
384 struct host1x_client *client, *tmp;
385 struct host1x_subdev *subdev;
386 struct host1x_device *device;
387 int err;
388
389 device = kzalloc(sizeof(*device), GFP_KERNEL);
390 if (!device)
391 return -ENOMEM;
392
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100393 device_initialize(&device->dev);
394
Thierry Reding776dc382013-10-14 14:43:22 +0200395 mutex_init(&device->subdevs_lock);
396 INIT_LIST_HEAD(&device->subdevs);
397 INIT_LIST_HEAD(&device->active);
398 mutex_init(&device->clients_lock);
399 INIT_LIST_HEAD(&device->clients);
400 INIT_LIST_HEAD(&device->list);
401 device->driver = driver;
402
403 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
404 device->dev.dma_mask = &device->dev.coherent_dma_mask;
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100405 dev_set_name(&device->dev, "%s", driver->driver.name);
Alexandre Courbotc95469a2016-02-26 18:06:53 +0900406 of_dma_configure(&device->dev, host1x->dev->of_node);
Thierry Reding776dc382013-10-14 14:43:22 +0200407 device->dev.release = host1x_device_release;
Thierry Reding7b1d4182017-01-20 15:57:35 +0100408 device->dev.of_node = host1x->dev->of_node;
Thierry Reding776dc382013-10-14 14:43:22 +0200409 device->dev.bus = &host1x_bus_type;
410 device->dev.parent = host1x->dev;
411
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100412 err = host1x_device_parse_dt(device, driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200413 if (err < 0) {
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100414 kfree(device);
Thierry Reding776dc382013-10-14 14:43:22 +0200415 return err;
416 }
417
Thierry Reding776dc382013-10-14 14:43:22 +0200418 list_add_tail(&device->list, &host1x->devices);
Thierry Reding776dc382013-10-14 14:43:22 +0200419
420 mutex_lock(&clients_lock);
421
422 list_for_each_entry_safe(client, tmp, &clients, list) {
423 list_for_each_entry(subdev, &device->subdevs, list) {
424 if (subdev->np == client->dev->of_node) {
425 host1x_subdev_register(device, subdev, client);
426 break;
427 }
428 }
429 }
430
431 mutex_unlock(&clients_lock);
432
433 return 0;
434}
435
436/*
437 * Removes a device by first unregistering any subdevices and then removing
438 * itself from the list of devices.
439 *
440 * This function must be called with the host1x->devices_lock held.
441 */
442static void host1x_device_del(struct host1x *host1x,
443 struct host1x_device *device)
444{
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100445 if (device->registered) {
446 device->registered = false;
447 device_del(&device->dev);
448 }
449
450 put_device(&device->dev);
Thierry Reding776dc382013-10-14 14:43:22 +0200451}
452
453static void host1x_attach_driver(struct host1x *host1x,
454 struct host1x_driver *driver)
455{
456 struct host1x_device *device;
457 int err;
458
459 mutex_lock(&host1x->devices_lock);
460
461 list_for_each_entry(device, &host1x->devices, list) {
462 if (device->driver == driver) {
463 mutex_unlock(&host1x->devices_lock);
464 return;
465 }
466 }
467
Thierry Reding776dc382013-10-14 14:43:22 +0200468 err = host1x_device_add(host1x, driver);
469 if (err < 0)
470 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
Thierry Reding38d98de2014-12-18 15:06:56 +0100471
472 mutex_unlock(&host1x->devices_lock);
Thierry Reding776dc382013-10-14 14:43:22 +0200473}
474
475static void host1x_detach_driver(struct host1x *host1x,
476 struct host1x_driver *driver)
477{
478 struct host1x_device *device, *tmp;
479
480 mutex_lock(&host1x->devices_lock);
481
482 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
483 if (device->driver == driver)
484 host1x_device_del(host1x, device);
485
486 mutex_unlock(&host1x->devices_lock);
487}
488
Thierry Reding466749f2017-04-10 12:27:01 +0200489/**
490 * host1x_register() - register a host1x controller
491 * @host1x: host1x controller
492 *
493 * The host1x controller driver uses this to register a host1x controller with
494 * the infrastructure. Note that all Tegra SoC generations have only ever come
495 * with a single host1x instance, so this function is somewhat academic.
496 */
Thierry Reding776dc382013-10-14 14:43:22 +0200497int host1x_register(struct host1x *host1x)
498{
499 struct host1x_driver *driver;
500
501 mutex_lock(&devices_lock);
502 list_add_tail(&host1x->list, &devices);
503 mutex_unlock(&devices_lock);
504
505 mutex_lock(&drivers_lock);
506
507 list_for_each_entry(driver, &drivers, list)
508 host1x_attach_driver(host1x, driver);
509
510 mutex_unlock(&drivers_lock);
511
512 return 0;
513}
514
Thierry Reding466749f2017-04-10 12:27:01 +0200515/**
516 * host1x_unregister() - unregister a host1x controller
517 * @host1x: host1x controller
518 *
519 * The host1x controller driver uses this to remove a host1x controller from
520 * the infrastructure.
521 */
Thierry Reding776dc382013-10-14 14:43:22 +0200522int host1x_unregister(struct host1x *host1x)
523{
524 struct host1x_driver *driver;
525
526 mutex_lock(&drivers_lock);
527
528 list_for_each_entry(driver, &drivers, list)
529 host1x_detach_driver(host1x, driver);
530
531 mutex_unlock(&drivers_lock);
532
533 mutex_lock(&devices_lock);
534 list_del_init(&host1x->list);
535 mutex_unlock(&devices_lock);
536
537 return 0;
538}
539
Thierry Redingb0d36da2017-03-22 19:15:18 +0100540static int host1x_device_probe(struct device *dev)
541{
542 struct host1x_driver *driver = to_host1x_driver(dev->driver);
543 struct host1x_device *device = to_host1x_device(dev);
544
545 if (driver->probe)
546 return driver->probe(device);
547
548 return 0;
549}
550
551static int host1x_device_remove(struct device *dev)
552{
553 struct host1x_driver *driver = to_host1x_driver(dev->driver);
554 struct host1x_device *device = to_host1x_device(dev);
555
556 if (driver->remove)
557 return driver->remove(device);
558
559 return 0;
560}
561
562static void host1x_device_shutdown(struct device *dev)
563{
564 struct host1x_driver *driver = to_host1x_driver(dev->driver);
565 struct host1x_device *device = to_host1x_device(dev);
566
567 if (driver->shutdown)
568 driver->shutdown(device);
569}
570
Thierry Reding466749f2017-04-10 12:27:01 +0200571/**
572 * host1x_driver_register_full() - register a host1x driver
573 * @driver: host1x driver
574 * @owner: owner module
575 *
576 * Drivers for host1x logical devices call this function to register a driver
577 * with the infrastructure. Note that since these drive logical devices, the
578 * registration of the driver actually triggers tho logical device creation.
579 * A logical device will be created for each host1x instance.
580 */
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100581int host1x_driver_register_full(struct host1x_driver *driver,
582 struct module *owner)
Thierry Reding776dc382013-10-14 14:43:22 +0200583{
584 struct host1x *host1x;
585
586 INIT_LIST_HEAD(&driver->list);
587
588 mutex_lock(&drivers_lock);
589 list_add_tail(&driver->list, &drivers);
590 mutex_unlock(&drivers_lock);
591
592 mutex_lock(&devices_lock);
593
594 list_for_each_entry(host1x, &devices, list)
595 host1x_attach_driver(host1x, driver);
596
597 mutex_unlock(&devices_lock);
598
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100599 driver->driver.bus = &host1x_bus_type;
600 driver->driver.owner = owner;
Thierry Redingb0d36da2017-03-22 19:15:18 +0100601 driver->driver.probe = host1x_device_probe;
602 driver->driver.remove = host1x_device_remove;
603 driver->driver.shutdown = host1x_device_shutdown;
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100604
605 return driver_register(&driver->driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200606}
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100607EXPORT_SYMBOL(host1x_driver_register_full);
Thierry Reding776dc382013-10-14 14:43:22 +0200608
Thierry Reding466749f2017-04-10 12:27:01 +0200609/**
610 * host1x_driver_unregister() - unregister a host1x driver
611 * @driver: host1x driver
612 *
613 * Unbinds the driver from each of the host1x logical devices that it is
614 * bound to, effectively removing the subsystem devices that they represent.
615 */
Thierry Reding776dc382013-10-14 14:43:22 +0200616void host1x_driver_unregister(struct host1x_driver *driver)
617{
Thierry Redinge3e70812015-08-24 14:51:04 +0200618 driver_unregister(&driver->driver);
619
Thierry Reding776dc382013-10-14 14:43:22 +0200620 mutex_lock(&drivers_lock);
621 list_del_init(&driver->list);
622 mutex_unlock(&drivers_lock);
623}
624EXPORT_SYMBOL(host1x_driver_unregister);
625
Thierry Reding466749f2017-04-10 12:27:01 +0200626/**
627 * host1x_client_register() - register a host1x client
628 * @client: host1x client
629 *
630 * Registers a host1x client with each host1x controller instance. Note that
631 * each client will only match their parent host1x controller and will only be
632 * associated with that instance. Once all clients have been registered with
633 * their parent host1x controller, the infrastructure will set up the logical
634 * device and call host1x_device_init(), which will in turn call each client's
635 * &host1x_client_ops.init implementation.
636 */
Thierry Reding776dc382013-10-14 14:43:22 +0200637int host1x_client_register(struct host1x_client *client)
638{
639 struct host1x *host1x;
640 int err;
641
642 mutex_lock(&devices_lock);
643
644 list_for_each_entry(host1x, &devices, list) {
Thierry Reding0c7dfd32014-05-22 11:12:17 +0200645 err = host1x_add_client(host1x, client);
Thierry Reding776dc382013-10-14 14:43:22 +0200646 if (!err) {
647 mutex_unlock(&devices_lock);
648 return 0;
649 }
650 }
651
652 mutex_unlock(&devices_lock);
653
654 mutex_lock(&clients_lock);
655 list_add_tail(&client->list, &clients);
656 mutex_unlock(&clients_lock);
657
658 return 0;
659}
660EXPORT_SYMBOL(host1x_client_register);
661
Thierry Reding466749f2017-04-10 12:27:01 +0200662/**
663 * host1x_client_unregister() - unregister a host1x client
664 * @client: host1x client
665 *
666 * Removes a host1x client from its host1x controller instance. If a logical
667 * device has already been initialized, it will be torn down.
668 */
Thierry Reding776dc382013-10-14 14:43:22 +0200669int host1x_client_unregister(struct host1x_client *client)
670{
671 struct host1x_client *c;
672 struct host1x *host1x;
673 int err;
674
675 mutex_lock(&devices_lock);
676
677 list_for_each_entry(host1x, &devices, list) {
Thierry Reding0c7dfd32014-05-22 11:12:17 +0200678 err = host1x_del_client(host1x, client);
Thierry Reding776dc382013-10-14 14:43:22 +0200679 if (!err) {
680 mutex_unlock(&devices_lock);
681 return 0;
682 }
683 }
684
685 mutex_unlock(&devices_lock);
686 mutex_lock(&clients_lock);
687
688 list_for_each_entry(c, &clients, list) {
689 if (c == client) {
690 list_del_init(&c->list);
691 break;
692 }
693 }
694
695 mutex_unlock(&clients_lock);
696
697 return 0;
698}
699EXPORT_SYMBOL(host1x_client_unregister);