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>
25 static DEFINE_MUTEX(clients_lock
);
26 static LIST_HEAD(clients
);
28 static DEFINE_MUTEX(drivers_lock
);
29 static LIST_HEAD(drivers
);
31 static DEFINE_MUTEX(devices_lock
);
32 static LIST_HEAD(devices
);
34 struct host1x_subdev
{
35 struct host1x_client
*client
;
36 struct device_node
*np
;
37 struct list_head list
;
41 * host1x_subdev_add() - add a new subdevice with an associated device node
43 static int host1x_subdev_add(struct host1x_device
*device
,
44 struct device_node
*np
)
46 struct host1x_subdev
*subdev
;
48 subdev
= kzalloc(sizeof(*subdev
), GFP_KERNEL
);
52 INIT_LIST_HEAD(&subdev
->list
);
53 subdev
->np
= of_node_get(np
);
55 mutex_lock(&device
->subdevs_lock
);
56 list_add_tail(&subdev
->list
, &device
->subdevs
);
57 mutex_unlock(&device
->subdevs_lock
);
63 * host1x_subdev_del() - remove subdevice
65 static void host1x_subdev_del(struct host1x_subdev
*subdev
)
67 list_del(&subdev
->list
);
68 of_node_put(subdev
->np
);
73 * host1x_device_parse_dt() - scan device tree and add matching subdevices
75 static int host1x_device_parse_dt(struct host1x_device
*device
,
76 struct host1x_driver
*driver
)
78 struct device_node
*np
;
81 for_each_child_of_node(device
->dev
.parent
->of_node
, np
) {
82 if (of_match_node(driver
->subdevs
, np
) &&
83 of_device_is_available(np
)) {
84 err
= host1x_subdev_add(device
, np
);
93 static void host1x_subdev_register(struct host1x_device
*device
,
94 struct host1x_subdev
*subdev
,
95 struct host1x_client
*client
)
100 * Move the subdevice to the list of active (registered) subdevices
101 * and associate it with a client. At the same time, associate the
102 * client with its parent device.
104 mutex_lock(&device
->subdevs_lock
);
105 mutex_lock(&device
->clients_lock
);
106 list_move_tail(&client
->list
, &device
->clients
);
107 list_move_tail(&subdev
->list
, &device
->active
);
108 client
->parent
= &device
->dev
;
109 subdev
->client
= client
;
110 mutex_unlock(&device
->clients_lock
);
111 mutex_unlock(&device
->subdevs_lock
);
113 if (list_empty(&device
->subdevs
)) {
114 err
= device_add(&device
->dev
);
116 dev_err(&device
->dev
, "failed to add: %d\n", err
);
118 device
->registered
= true;
122 static void __host1x_subdev_unregister(struct host1x_device
*device
,
123 struct host1x_subdev
*subdev
)
125 struct host1x_client
*client
= subdev
->client
;
128 * If all subdevices have been activated, we're about to remove the
129 * first active subdevice, so unload the driver first.
131 if (list_empty(&device
->subdevs
)) {
132 if (device
->registered
) {
133 device
->registered
= false;
134 device_del(&device
->dev
);
139 * Move the subdevice back to the list of idle subdevices and remove
140 * it from list of clients.
142 mutex_lock(&device
->clients_lock
);
143 subdev
->client
= NULL
;
144 client
->parent
= NULL
;
145 list_move_tail(&subdev
->list
, &device
->subdevs
);
147 * XXX: Perhaps don't do this here, but rather explicitly remove it
148 * when the device is about to be deleted.
150 * This is somewhat complicated by the fact that this function is
151 * used to remove the subdevice when a client is unregistered but
152 * also when the composite device is about to be removed.
154 list_del_init(&client
->list
);
155 mutex_unlock(&device
->clients_lock
);
158 static void host1x_subdev_unregister(struct host1x_device
*device
,
159 struct host1x_subdev
*subdev
)
161 mutex_lock(&device
->subdevs_lock
);
162 __host1x_subdev_unregister(device
, subdev
);
163 mutex_unlock(&device
->subdevs_lock
);
166 int host1x_device_init(struct host1x_device
*device
)
168 struct host1x_client
*client
;
171 mutex_lock(&device
->clients_lock
);
173 list_for_each_entry(client
, &device
->clients
, list
) {
174 if (client
->ops
&& client
->ops
->init
) {
175 err
= client
->ops
->init(client
);
177 dev_err(&device
->dev
,
178 "failed to initialize %s: %d\n",
179 dev_name(client
->dev
), err
);
180 mutex_unlock(&device
->clients_lock
);
186 mutex_unlock(&device
->clients_lock
);
190 EXPORT_SYMBOL(host1x_device_init
);
192 int host1x_device_exit(struct host1x_device
*device
)
194 struct host1x_client
*client
;
197 mutex_lock(&device
->clients_lock
);
199 list_for_each_entry_reverse(client
, &device
->clients
, list
) {
200 if (client
->ops
&& client
->ops
->exit
) {
201 err
= client
->ops
->exit(client
);
203 dev_err(&device
->dev
,
204 "failed to cleanup %s: %d\n",
205 dev_name(client
->dev
), err
);
206 mutex_unlock(&device
->clients_lock
);
212 mutex_unlock(&device
->clients_lock
);
216 EXPORT_SYMBOL(host1x_device_exit
);
218 static int host1x_add_client(struct host1x
*host1x
,
219 struct host1x_client
*client
)
221 struct host1x_device
*device
;
222 struct host1x_subdev
*subdev
;
224 mutex_lock(&host1x
->devices_lock
);
226 list_for_each_entry(device
, &host1x
->devices
, list
) {
227 list_for_each_entry(subdev
, &device
->subdevs
, list
) {
228 if (subdev
->np
== client
->dev
->of_node
) {
229 host1x_subdev_register(device
, subdev
, client
);
230 mutex_unlock(&host1x
->devices_lock
);
236 mutex_unlock(&host1x
->devices_lock
);
240 static int host1x_del_client(struct host1x
*host1x
,
241 struct host1x_client
*client
)
243 struct host1x_device
*device
, *dt
;
244 struct host1x_subdev
*subdev
;
246 mutex_lock(&host1x
->devices_lock
);
248 list_for_each_entry_safe(device
, dt
, &host1x
->devices
, list
) {
249 list_for_each_entry(subdev
, &device
->active
, list
) {
250 if (subdev
->client
== client
) {
251 host1x_subdev_unregister(device
, subdev
);
252 mutex_unlock(&host1x
->devices_lock
);
258 mutex_unlock(&host1x
->devices_lock
);
262 static int host1x_device_match(struct device
*dev
, struct device_driver
*drv
)
264 return strcmp(dev_name(dev
), drv
->name
) == 0;
267 static int host1x_device_probe(struct device
*dev
)
269 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
270 struct host1x_device
*device
= to_host1x_device(dev
);
273 return driver
->probe(device
);
278 static int host1x_device_remove(struct device
*dev
)
280 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
281 struct host1x_device
*device
= to_host1x_device(dev
);
284 return driver
->remove(device
);
289 static void host1x_device_shutdown(struct device
*dev
)
291 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
292 struct host1x_device
*device
= to_host1x_device(dev
);
294 if (driver
->shutdown
)
295 driver
->shutdown(device
);
298 static const struct dev_pm_ops host1x_device_pm_ops
= {
299 .suspend
= pm_generic_suspend
,
300 .resume
= pm_generic_resume
,
301 .freeze
= pm_generic_freeze
,
302 .thaw
= pm_generic_thaw
,
303 .poweroff
= pm_generic_poweroff
,
304 .restore
= pm_generic_restore
,
307 struct bus_type host1x_bus_type
= {
309 .match
= host1x_device_match
,
310 .probe
= host1x_device_probe
,
311 .remove
= host1x_device_remove
,
312 .shutdown
= host1x_device_shutdown
,
313 .pm
= &host1x_device_pm_ops
,
316 static void __host1x_device_del(struct host1x_device
*device
)
318 struct host1x_subdev
*subdev
, *sd
;
319 struct host1x_client
*client
, *cl
;
321 mutex_lock(&device
->subdevs_lock
);
323 /* unregister subdevices */
324 list_for_each_entry_safe(subdev
, sd
, &device
->active
, list
) {
326 * host1x_subdev_unregister() will remove the client from
327 * any lists, so we'll need to manually add it back to the
328 * list of idle clients.
330 * XXX: Alternatively, perhaps don't remove the client from
331 * any lists in host1x_subdev_unregister() and instead do
332 * that explicitly from host1x_unregister_client()?
334 client
= subdev
->client
;
336 __host1x_subdev_unregister(device
, subdev
);
338 /* add the client to the list of idle clients */
339 mutex_lock(&clients_lock
);
340 list_add_tail(&client
->list
, &clients
);
341 mutex_unlock(&clients_lock
);
344 /* remove subdevices */
345 list_for_each_entry_safe(subdev
, sd
, &device
->subdevs
, list
)
346 host1x_subdev_del(subdev
);
348 mutex_unlock(&device
->subdevs_lock
);
350 /* move clients to idle list */
351 mutex_lock(&clients_lock
);
352 mutex_lock(&device
->clients_lock
);
354 list_for_each_entry_safe(client
, cl
, &device
->clients
, list
)
355 list_move_tail(&client
->list
, &clients
);
357 mutex_unlock(&device
->clients_lock
);
358 mutex_unlock(&clients_lock
);
360 /* finally remove the device */
361 list_del_init(&device
->list
);
364 static void host1x_device_release(struct device
*dev
)
366 struct host1x_device
*device
= to_host1x_device(dev
);
368 __host1x_device_del(device
);
372 static int host1x_device_add(struct host1x
*host1x
,
373 struct host1x_driver
*driver
)
375 struct host1x_client
*client
, *tmp
;
376 struct host1x_subdev
*subdev
;
377 struct host1x_device
*device
;
380 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
384 device_initialize(&device
->dev
);
386 mutex_init(&device
->subdevs_lock
);
387 INIT_LIST_HEAD(&device
->subdevs
);
388 INIT_LIST_HEAD(&device
->active
);
389 mutex_init(&device
->clients_lock
);
390 INIT_LIST_HEAD(&device
->clients
);
391 INIT_LIST_HEAD(&device
->list
);
392 device
->driver
= driver
;
394 device
->dev
.coherent_dma_mask
= host1x
->dev
->coherent_dma_mask
;
395 device
->dev
.dma_mask
= &device
->dev
.coherent_dma_mask
;
396 dev_set_name(&device
->dev
, "%s", driver
->driver
.name
);
397 device
->dev
.release
= host1x_device_release
;
398 device
->dev
.bus
= &host1x_bus_type
;
399 device
->dev
.parent
= host1x
->dev
;
401 err
= host1x_device_parse_dt(device
, driver
);
407 list_add_tail(&device
->list
, &host1x
->devices
);
409 mutex_lock(&clients_lock
);
411 list_for_each_entry_safe(client
, tmp
, &clients
, list
) {
412 list_for_each_entry(subdev
, &device
->subdevs
, list
) {
413 if (subdev
->np
== client
->dev
->of_node
) {
414 host1x_subdev_register(device
, subdev
, client
);
420 mutex_unlock(&clients_lock
);
426 * Removes a device by first unregistering any subdevices and then removing
427 * itself from the list of devices.
429 * This function must be called with the host1x->devices_lock held.
431 static void host1x_device_del(struct host1x
*host1x
,
432 struct host1x_device
*device
)
434 if (device
->registered
) {
435 device
->registered
= false;
436 device_del(&device
->dev
);
439 put_device(&device
->dev
);
442 static void host1x_attach_driver(struct host1x
*host1x
,
443 struct host1x_driver
*driver
)
445 struct host1x_device
*device
;
448 mutex_lock(&host1x
->devices_lock
);
450 list_for_each_entry(device
, &host1x
->devices
, list
) {
451 if (device
->driver
== driver
) {
452 mutex_unlock(&host1x
->devices_lock
);
457 err
= host1x_device_add(host1x
, driver
);
459 dev_err(host1x
->dev
, "failed to allocate device: %d\n", err
);
461 mutex_unlock(&host1x
->devices_lock
);
464 static void host1x_detach_driver(struct host1x
*host1x
,
465 struct host1x_driver
*driver
)
467 struct host1x_device
*device
, *tmp
;
469 mutex_lock(&host1x
->devices_lock
);
471 list_for_each_entry_safe(device
, tmp
, &host1x
->devices
, list
)
472 if (device
->driver
== driver
)
473 host1x_device_del(host1x
, device
);
475 mutex_unlock(&host1x
->devices_lock
);
478 int host1x_register(struct host1x
*host1x
)
480 struct host1x_driver
*driver
;
482 mutex_lock(&devices_lock
);
483 list_add_tail(&host1x
->list
, &devices
);
484 mutex_unlock(&devices_lock
);
486 mutex_lock(&drivers_lock
);
488 list_for_each_entry(driver
, &drivers
, list
)
489 host1x_attach_driver(host1x
, driver
);
491 mutex_unlock(&drivers_lock
);
496 int host1x_unregister(struct host1x
*host1x
)
498 struct host1x_driver
*driver
;
500 mutex_lock(&drivers_lock
);
502 list_for_each_entry(driver
, &drivers
, list
)
503 host1x_detach_driver(host1x
, driver
);
505 mutex_unlock(&drivers_lock
);
507 mutex_lock(&devices_lock
);
508 list_del_init(&host1x
->list
);
509 mutex_unlock(&devices_lock
);
514 int host1x_driver_register_full(struct host1x_driver
*driver
,
515 struct module
*owner
)
517 struct host1x
*host1x
;
519 INIT_LIST_HEAD(&driver
->list
);
521 mutex_lock(&drivers_lock
);
522 list_add_tail(&driver
->list
, &drivers
);
523 mutex_unlock(&drivers_lock
);
525 mutex_lock(&devices_lock
);
527 list_for_each_entry(host1x
, &devices
, list
)
528 host1x_attach_driver(host1x
, driver
);
530 mutex_unlock(&devices_lock
);
532 driver
->driver
.bus
= &host1x_bus_type
;
533 driver
->driver
.owner
= owner
;
535 return driver_register(&driver
->driver
);
537 EXPORT_SYMBOL(host1x_driver_register_full
);
539 void host1x_driver_unregister(struct host1x_driver
*driver
)
541 mutex_lock(&drivers_lock
);
542 list_del_init(&driver
->list
);
543 mutex_unlock(&drivers_lock
);
545 EXPORT_SYMBOL(host1x_driver_unregister
);
547 int host1x_client_register(struct host1x_client
*client
)
549 struct host1x
*host1x
;
552 mutex_lock(&devices_lock
);
554 list_for_each_entry(host1x
, &devices
, list
) {
555 err
= host1x_add_client(host1x
, client
);
557 mutex_unlock(&devices_lock
);
562 mutex_unlock(&devices_lock
);
564 mutex_lock(&clients_lock
);
565 list_add_tail(&client
->list
, &clients
);
566 mutex_unlock(&clients_lock
);
570 EXPORT_SYMBOL(host1x_client_register
);
572 int host1x_client_unregister(struct host1x_client
*client
)
574 struct host1x_client
*c
;
575 struct host1x
*host1x
;
578 mutex_lock(&devices_lock
);
580 list_for_each_entry(host1x
, &devices
, list
) {
581 err
= host1x_del_client(host1x
, client
);
583 mutex_unlock(&devices_lock
);
588 mutex_unlock(&devices_lock
);
589 mutex_lock(&clients_lock
);
591 list_for_each_entry(c
, &clients
, list
) {
593 list_del_init(&c
->list
);
598 mutex_unlock(&clients_lock
);
602 EXPORT_SYMBOL(host1x_client_unregister
);