79511759d2a5c39b36c763813e607f0452af0680
[firefly-linux-kernel-4.4.55.git] / drivers / video / adf / adf.c
1 /*
2  * Copyright (C) 2013 Google, Inc.
3  * adf_modeinfo_{set_name,set_vrefresh} modified from
4  * drivers/gpu/drm/drm_modes.c
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/device.h>
18 #include <linux/idr.h>
19 #include <linux/highmem.h>
20 #include <linux/memblock.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24
25 #include <video/adf_format.h>
26
27 #include "sw_sync.h"
28 #include "sync.h"
29
30 #include "adf.h"
31 #include "adf_fops.h"
32 #include "adf_sysfs.h"
33
34 #define CREATE_TRACE_POINTS
35 #include "adf_trace.h"
36
37 #define ADF_SHORT_FENCE_TIMEOUT (1 * MSEC_PER_SEC)
38 #define ADF_LONG_FENCE_TIMEOUT (10 * MSEC_PER_SEC)
39
40 static void adf_fence_wait(struct adf_device *dev, struct sync_fence *fence)
41 {
42         /* sync_fence_wait() dumps debug information on timeout.  Experience
43            has shown that if the pipeline gets stuck, a short timeout followed
44            by a longer one provides useful information for debugging. */
45         int err = sync_fence_wait(fence, ADF_SHORT_FENCE_TIMEOUT);
46         if (err >= 0)
47                 return;
48
49         if (err == -ETIME)
50                 err = sync_fence_wait(fence, ADF_LONG_FENCE_TIMEOUT);
51
52         if (err < 0)
53                 dev_warn(&dev->base.dev, "error waiting on fence: %d\n", err);
54 }
55
56 void adf_buffer_cleanup(struct adf_buffer *buf)
57 {
58         size_t i;
59         for (i = 0; i < ARRAY_SIZE(buf->dma_bufs); i++)
60                 if (buf->dma_bufs[i])
61                         dma_buf_put(buf->dma_bufs[i]);
62
63         if (buf->acquire_fence)
64                 sync_fence_put(buf->acquire_fence);
65 }
66
67 void adf_buffer_mapping_cleanup(struct adf_buffer_mapping *mapping,
68                 struct adf_buffer *buf)
69 {
70         /* calling adf_buffer_mapping_cleanup() is safe even if mapping is
71            uninitialized or partially-initialized, as long as it was
72            zeroed on allocation */
73         size_t i;
74         for (i = 0; i < ARRAY_SIZE(mapping->sg_tables); i++) {
75                 if (mapping->sg_tables[i])
76                         dma_buf_unmap_attachment(mapping->attachments[i],
77                                         mapping->sg_tables[i], DMA_TO_DEVICE);
78                 if (mapping->attachments[i])
79                         dma_buf_detach(buf->dma_bufs[i],
80                                         mapping->attachments[i]);
81         }
82 }
83
84 void adf_post_cleanup(struct adf_device *dev, struct adf_pending_post *post)
85 {
86         size_t i;
87
88         if (post->state)
89                 dev->ops->state_free(dev, post->state);
90
91         for (i = 0; i < post->config.n_bufs; i++) {
92                 adf_buffer_mapping_cleanup(&post->config.mappings[i],
93                                 &post->config.bufs[i]);
94                 adf_buffer_cleanup(&post->config.bufs[i]);
95         }
96
97         kfree(post->config.custom_data);
98         kfree(post->config.mappings);
99         kfree(post->config.bufs);
100         kfree(post);
101 }
102
103 static void adf_sw_advance_timeline(struct adf_device *dev)
104 {
105 #ifdef CONFIG_SW_SYNC
106         sw_sync_timeline_inc(dev->timeline, 1);
107 #else
108         BUG();
109 #endif
110 }
111
112 static void adf_post_work_func(struct kthread_work *work)
113 {
114         struct adf_device *dev =
115                         container_of(work, struct adf_device, post_work);
116         struct adf_pending_post *post, *next;
117         struct list_head saved_list;
118
119         mutex_lock(&dev->post_lock);
120         memcpy(&saved_list, &dev->post_list, sizeof(saved_list));
121         list_replace_init(&dev->post_list, &saved_list);
122         mutex_unlock(&dev->post_lock);
123
124         list_for_each_entry_safe(post, next, &saved_list, head) {
125                 int i;
126
127                 for (i = 0; i < post->config.n_bufs; i++) {
128                         struct sync_fence *fence =
129                                         post->config.bufs[i].acquire_fence;
130                         if (fence)
131                                 adf_fence_wait(dev, fence);
132                 }
133
134                 dev->ops->post(dev, &post->config, post->state);
135
136                 if (dev->ops->advance_timeline)
137                         dev->ops->advance_timeline(dev, &post->config,
138                                         post->state);
139                 else
140                         adf_sw_advance_timeline(dev);
141
142                 list_del(&post->head);
143                 if (dev->onscreen)
144                         adf_post_cleanup(dev, dev->onscreen);
145                 dev->onscreen = post;
146         }
147 }
148
149 void adf_attachment_free(struct adf_attachment_list *attachment)
150 {
151         list_del(&attachment->head);
152         kfree(attachment);
153 }
154
155 struct adf_event_refcount *adf_obj_find_event_refcount(struct adf_obj *obj,
156                 enum adf_event_type type)
157 {
158         struct rb_root *root = &obj->event_refcount;
159         struct rb_node **new = &(root->rb_node);
160         struct rb_node *parent = NULL;
161         struct adf_event_refcount *refcount;
162
163         while (*new) {
164                 refcount = container_of(*new, struct adf_event_refcount, node);
165                 parent = *new;
166
167                 if (refcount->type > type)
168                         new = &(*new)->rb_left;
169                 else if (refcount->type < type)
170                         new = &(*new)->rb_right;
171                 else
172                         return refcount;
173         }
174
175         refcount = kzalloc(sizeof(*refcount), GFP_KERNEL);
176         if (!refcount)
177                 return NULL;
178         refcount->type = type;
179
180         rb_link_node(&refcount->node, parent, new);
181         rb_insert_color(&refcount->node, root);
182         return refcount;
183 }
184
185 /**
186  * adf_event_get - increase the refcount for an event
187  *
188  * @obj: the object that produces the event
189  * @type: the event type
190  *
191  * ADF will call the object's set_event() op if needed.  ops are allowed
192  * to sleep, so adf_event_get() must NOT be called from an atomic context.
193  *
194  * Returns 0 if successful, or -%EINVAL if the object does not support the
195  * requested event type.
196  */
197 int adf_event_get(struct adf_obj *obj, enum adf_event_type type)
198 {
199         struct adf_event_refcount *refcount;
200         int old_refcount;
201         int ret;
202
203         ret = adf_obj_check_supports_event(obj, type);
204         if (ret < 0)
205                 return ret;
206
207         mutex_lock(&obj->event_lock);
208
209         refcount = adf_obj_find_event_refcount(obj, type);
210         if (!refcount) {
211                 ret = -ENOMEM;
212                 goto done;
213         }
214
215         old_refcount = refcount->refcount++;
216
217         if (old_refcount == 0) {
218                 obj->ops->set_event(obj, type, true);
219                 trace_adf_event_enable(obj, type);
220         }
221
222 done:
223         mutex_unlock(&obj->event_lock);
224         return ret;
225 }
226 EXPORT_SYMBOL(adf_event_get);
227
228 /**
229  * adf_event_put - decrease the refcount for an event
230  *
231  * @obj: the object that produces the event
232  * @type: the event type
233  *
234  * ADF will call the object's set_event() op if needed.  ops are allowed
235  * to sleep, so adf_event_put() must NOT be called from an atomic context.
236  *
237  * Returns 0 if successful, -%EINVAL if the object does not support the
238  * requested event type, or -%EALREADY if the refcount is already 0.
239  */
240 int adf_event_put(struct adf_obj *obj, enum adf_event_type type)
241 {
242         struct adf_event_refcount *refcount;
243         int old_refcount;
244         int ret;
245
246         ret = adf_obj_check_supports_event(obj, type);
247         if (ret < 0)
248                 return ret;
249
250
251         mutex_lock(&obj->event_lock);
252
253         refcount = adf_obj_find_event_refcount(obj, type);
254         if (!refcount) {
255                 ret = -ENOMEM;
256                 goto done;
257         }
258
259         old_refcount = refcount->refcount--;
260
261         if (WARN_ON(old_refcount == 0)) {
262                 refcount->refcount++;
263                 ret = -EALREADY;
264         } else if (old_refcount == 1) {
265                 obj->ops->set_event(obj, type, false);
266                 trace_adf_event_disable(obj, type);
267         }
268
269 done:
270         mutex_unlock(&obj->event_lock);
271         return ret;
272 }
273 EXPORT_SYMBOL(adf_event_put);
274
275 /**
276  * adf_vsync_wait - wait for a vsync event on a display interface
277  *
278  * @intf: the display interface
279  * @timeout: timeout in jiffies (0 = wait indefinitely)
280  *
281  * adf_vsync_wait() may sleep, so it must NOT be called from an atomic context.
282  *
283  * This function returns -%ERESTARTSYS if it is interrupted by a signal.
284  * If @timeout == 0 then this function returns 0 on vsync. If @timeout > 0 then
285  * this function returns the number of remaining jiffies or -%ETIMEDOUT on
286  * timeout.
287  */
288 int adf_vsync_wait(struct adf_interface *intf, long timeout)
289 {
290         ktime_t timestamp;
291         int ret;
292         unsigned long flags;
293
294         read_lock_irqsave(&intf->vsync_lock, flags);
295         timestamp = intf->vsync_timestamp;
296         read_unlock_irqrestore(&intf->vsync_lock, flags);
297
298         adf_vsync_get(intf);
299         if (timeout) {
300                 ret = wait_event_interruptible_timeout(intf->vsync_wait,
301                                 !ktime_equal(timestamp,
302                                                 intf->vsync_timestamp),
303                                 msecs_to_jiffies(timeout));
304                 if (ret == 0 && ktime_equal(timestamp, intf->vsync_timestamp))
305                         ret = -ETIMEDOUT;
306         } else {
307                 ret = wait_event_interruptible(intf->vsync_wait,
308                                 !ktime_equal(timestamp,
309                                                 intf->vsync_timestamp));
310         }
311         adf_vsync_put(intf);
312
313         return ret;
314 }
315 EXPORT_SYMBOL(adf_vsync_wait);
316
317 static void adf_event_queue(struct adf_obj *obj, struct adf_event *event)
318 {
319         struct adf_file *file;
320         unsigned long flags;
321
322         trace_adf_event(obj, event->type);
323
324         spin_lock_irqsave(&obj->file_lock, flags);
325
326         list_for_each_entry(file, &obj->file_list, head)
327                 if (test_bit(event->type, file->event_subscriptions))
328                         adf_file_queue_event(file, event);
329
330         spin_unlock_irqrestore(&obj->file_lock, flags);
331 }
332
333 /**
334  * adf_event_notify - notify userspace of a driver-private event
335  *
336  * @obj: the ADF object that produced the event
337  * @event: the event
338  *
339  * adf_event_notify() may be called safely from an atomic context.  It will
340  * copy @event if needed, so @event may point to a variable on the stack.
341  *
342  * Drivers must NOT call adf_event_notify() for vsync and hotplug events.
343  * ADF provides adf_vsync_notify() and
344  * adf_hotplug_notify_{connected,disconnected}() for these events.
345  */
346 int adf_event_notify(struct adf_obj *obj, struct adf_event *event)
347 {
348         if (WARN_ON(event->type == ADF_EVENT_VSYNC ||
349                         event->type == ADF_EVENT_HOTPLUG))
350                 return -EINVAL;
351
352         adf_event_queue(obj, event);
353         return 0;
354 }
355 EXPORT_SYMBOL(adf_event_notify);
356
357 /**
358  * adf_vsync_notify - notify ADF of a display interface's vsync event
359  *
360  * @intf: the display interface
361  * @timestamp: the time the vsync occurred
362  *
363  * adf_vsync_notify() may be called safely from an atomic context.
364  */
365 void adf_vsync_notify(struct adf_interface *intf, ktime_t timestamp)
366 {
367         unsigned long flags;
368         struct adf_vsync_event event;
369
370         write_lock_irqsave(&intf->vsync_lock, flags);
371         intf->vsync_timestamp = timestamp;
372         write_unlock_irqrestore(&intf->vsync_lock, flags);
373
374         wake_up_interruptible_all(&intf->vsync_wait);
375
376         event.base.type = ADF_EVENT_VSYNC;
377         event.base.length = sizeof(event);
378         event.timestamp = ktime_to_ns(timestamp);
379         adf_event_queue(&intf->base, &event.base);
380 }
381 EXPORT_SYMBOL(adf_vsync_notify);
382
383 void adf_hotplug_notify(struct adf_interface *intf, bool connected,
384                 struct drm_mode_modeinfo *modelist, size_t n_modes)
385 {
386         unsigned long flags;
387         struct adf_hotplug_event event;
388         struct drm_mode_modeinfo *old_modelist;
389
390         write_lock_irqsave(&intf->hotplug_modelist_lock, flags);
391         old_modelist = intf->modelist;
392         intf->hotplug_detect = connected;
393         intf->modelist = modelist;
394         intf->n_modes = n_modes;
395         write_unlock_irqrestore(&intf->hotplug_modelist_lock, flags);
396
397         kfree(old_modelist);
398
399         event.base.length = sizeof(event);
400         event.base.type = ADF_EVENT_HOTPLUG;
401         event.connected = connected;
402         adf_event_queue(&intf->base, &event.base);
403 }
404
405 /**
406  * adf_hotplug_notify_connected - notify ADF of a display interface being
407  * connected to a display
408  *
409  * @intf: the display interface
410  * @modelist: hardware modes supported by display
411  * @n_modes: length of modelist
412  *
413  * @modelist is copied as needed, so it may point to a variable on the stack.
414  *
415  * adf_hotplug_notify_connected() may NOT be called safely from an atomic
416  * context.
417  *
418  * Returns 0 on success or error code (<0) on error.
419  */
420 int adf_hotplug_notify_connected(struct adf_interface *intf,
421                 struct drm_mode_modeinfo *modelist, size_t n_modes)
422 {
423         struct drm_mode_modeinfo *modelist_copy;
424
425         if (n_modes > ADF_MAX_MODES)
426                 return -ENOMEM;
427
428         modelist_copy = kzalloc(sizeof(modelist_copy[0]) * n_modes,
429                         GFP_KERNEL);
430         if (!modelist_copy)
431                 return -ENOMEM;
432         memcpy(modelist_copy, modelist, sizeof(modelist_copy[0]) * n_modes);
433
434         adf_hotplug_notify(intf, true, modelist_copy, n_modes);
435         return 0;
436 }
437 EXPORT_SYMBOL(adf_hotplug_notify_connected);
438
439 /**
440  * adf_hotplug_notify_disconnected - notify ADF of a display interface being
441  * disconnected from a display
442  *
443  * @intf: the display interface
444  *
445  * adf_hotplug_notify_disconnected() may be called safely from an atomic
446  * context.
447  */
448 void adf_hotplug_notify_disconnected(struct adf_interface *intf)
449 {
450         adf_hotplug_notify(intf, false, NULL, 0);
451 }
452 EXPORT_SYMBOL(adf_hotplug_notify_disconnected);
453
454 static int adf_obj_init(struct adf_obj *obj, enum adf_obj_type type,
455                 struct idr *idr, struct adf_device *parent,
456                 const struct adf_obj_ops *ops, const char *fmt, va_list args)
457 {
458         if (ops && ops->supports_event && !ops->set_event) {
459                 pr_err("%s: %s implements supports_event but not set_event\n",
460                                 __func__, adf_obj_type_str(type));
461                 return -EINVAL;
462         }
463
464         if (idr) {
465                 int ret = idr_alloc(idr, obj, 0, 0, GFP_KERNEL);
466                 if (ret < 0) {
467                         pr_err("%s: allocating object id failed: %d\n",
468                                         __func__, ret);
469                         return ret;
470                 }
471                 obj->id = ret;
472         } else {
473                 obj->id = -1;
474         }
475
476         vscnprintf(obj->name, sizeof(obj->name), fmt, args);
477
478         obj->type = type;
479         obj->ops = ops;
480         obj->parent = parent;
481         mutex_init(&obj->event_lock);
482         obj->event_refcount = RB_ROOT;
483         spin_lock_init(&obj->file_lock);
484         INIT_LIST_HEAD(&obj->file_list);
485         return 0;
486 }
487
488 static void adf_obj_destroy(struct adf_obj *obj, struct idr *idr)
489 {
490         struct rb_node *node = rb_first(&obj->event_refcount);
491
492         while (node) {
493                 struct adf_event_refcount *refcount =
494                                 container_of(node, struct adf_event_refcount,
495                                                 node);
496                 kfree(refcount);
497                 node = rb_first(&obj->event_refcount);
498         }
499
500         mutex_destroy(&obj->event_lock);
501         if (idr)
502                 idr_remove(idr, obj->id);
503 }
504
505 /**
506  * adf_device_init - initialize ADF-internal data for a display device
507  * and create sysfs entries
508  *
509  * @dev: the display device
510  * @parent: the device's parent device
511  * @ops: the device's associated ops
512  * @fmt: formatting string for the display device's name
513  *
514  * @fmt specifies the device's sysfs filename and the name returned to
515  * userspace through the %ADF_GET_DEVICE_DATA ioctl.
516  *
517  * Returns 0 on success or error code (<0) on failure.
518  */
519 int adf_device_init(struct adf_device *dev, struct device *parent,
520                 const struct adf_device_ops *ops, const char *fmt, ...)
521 {
522         int ret;
523         va_list args;
524
525         if (!ops->validate || !ops->post) {
526                 pr_err("%s: device must implement validate and post\n",
527                                 __func__);
528                 return -EINVAL;
529         }
530
531         if (!ops->complete_fence && !ops->advance_timeline) {
532                 if (!IS_ENABLED(CONFIG_SW_SYNC)) {
533                         pr_err("%s: device requires sw_sync but it is not enabled in the kernel\n",
534                                         __func__);
535                         return -EINVAL;
536                 }
537         } else if (!(ops->complete_fence && ops->advance_timeline)) {
538                 pr_err("%s: device must implement both complete_fence and advance_timeline, or implement neither\n",
539                                 __func__);
540                 return -EINVAL;
541         }
542
543         memset(dev, 0, sizeof(*dev));
544
545         va_start(args, fmt);
546         ret = adf_obj_init(&dev->base, ADF_OBJ_DEVICE, NULL, dev, &ops->base,
547                         fmt, args);
548         va_end(args);
549         if (ret < 0)
550                 return ret;
551
552         dev->dev = parent;
553         dev->ops = ops;
554         idr_init(&dev->overlay_engines);
555         idr_init(&dev->interfaces);
556         mutex_init(&dev->client_lock);
557         INIT_LIST_HEAD(&dev->post_list);
558         mutex_init(&dev->post_lock);
559         init_kthread_worker(&dev->post_worker);
560         INIT_LIST_HEAD(&dev->attached);
561         INIT_LIST_HEAD(&dev->attach_allowed);
562
563         dev->post_thread = kthread_run(kthread_worker_fn,
564                         &dev->post_worker, dev->base.name);
565         if (IS_ERR(dev->post_thread)) {
566                 ret = PTR_ERR(dev->post_thread);
567                 dev->post_thread = NULL;
568
569                 pr_err("%s: failed to run config posting thread: %d\n",
570                                 __func__, ret);
571                 goto err;
572         }
573         init_kthread_work(&dev->post_work, adf_post_work_func);
574
575         ret = adf_device_sysfs_init(dev);
576         if (ret < 0)
577                 goto err;
578
579         return 0;
580
581 err:
582         adf_device_destroy(dev);
583         return ret;
584 }
585 EXPORT_SYMBOL(adf_device_init);
586
587 /**
588  * adf_device_destroy - clean up ADF-internal data for a display device
589  *
590  * @dev: the display device
591  */
592 void adf_device_destroy(struct adf_device *dev)
593 {
594         struct adf_attachment_list *entry, *next;
595
596         idr_destroy(&dev->interfaces);
597         idr_destroy(&dev->overlay_engines);
598
599         if (dev->post_thread) {
600                 flush_kthread_worker(&dev->post_worker);
601                 kthread_stop(dev->post_thread);
602         }
603
604         if (dev->onscreen)
605                 adf_post_cleanup(dev, dev->onscreen);
606         adf_device_sysfs_destroy(dev);
607         list_for_each_entry_safe(entry, next, &dev->attach_allowed, head) {
608                 adf_attachment_free(entry);
609         }
610         list_for_each_entry_safe(entry, next, &dev->attached, head) {
611                 adf_attachment_free(entry);
612         }
613         mutex_destroy(&dev->post_lock);
614         mutex_destroy(&dev->client_lock);
615         adf_obj_destroy(&dev->base, NULL);
616 }
617 EXPORT_SYMBOL(adf_device_destroy);
618
619 /**
620  * adf_interface_init - initialize ADF-internal data for a display interface
621  * and create sysfs entries
622  *
623  * @intf: the display interface
624  * @dev: the interface's "parent" display device
625  * @type: interface type (see enum @adf_interface_type)
626  * @idx: which interface of type @type;
627  *      e.g. interface DSI.1 -> @type=%ADF_INTF_TYPE_DSI, @idx=1
628  * @flags: informational flags (bitmask of %ADF_INTF_FLAG_* values)
629  * @ops: the interface's associated ops
630  * @fmt: formatting string for the display interface's name
631  *
632  * @dev must have previously been initialized with adf_device_init().
633  *
634  * @fmt affects the name returned to userspace through the
635  * %ADF_GET_INTERFACE_DATA ioctl.  It does not affect the sysfs filename,
636  * which is derived from @dev's name.
637  *
638  * Returns 0 on success or error code (<0) on failure.
639  */
640 int adf_interface_init(struct adf_interface *intf, struct adf_device *dev,
641                 enum adf_interface_type type, u32 idx, u32 flags,
642                 const struct adf_interface_ops *ops, const char *fmt, ...)
643 {
644         int ret;
645         va_list args;
646         const u32 allowed_flags = ADF_INTF_FLAG_PRIMARY |
647                         ADF_INTF_FLAG_EXTERNAL;
648
649         if (dev->n_interfaces == ADF_MAX_INTERFACES) {
650                 pr_err("%s: parent device %s has too many interfaces\n",
651                                 __func__, dev->base.name);
652                 return -ENOMEM;
653         }
654
655         if (type >= ADF_INTF_MEMORY && type <= ADF_INTF_TYPE_DEVICE_CUSTOM) {
656                 pr_err("%s: invalid interface type %u\n", __func__, type);
657                 return -EINVAL;
658         }
659
660         if (flags & ~allowed_flags) {
661                 pr_err("%s: invalid interface flags 0x%X\n", __func__,
662                                 flags & ~allowed_flags);
663                 return -EINVAL;
664         }
665
666         memset(intf, 0, sizeof(*intf));
667
668         va_start(args, fmt);
669         ret = adf_obj_init(&intf->base, ADF_OBJ_INTERFACE, &dev->interfaces,
670                         dev, ops ? &ops->base : NULL, fmt, args);
671         va_end(args);
672         if (ret < 0)
673                 return ret;
674
675         intf->type = type;
676         intf->idx = idx;
677         intf->flags = flags;
678         intf->ops = ops;
679         init_waitqueue_head(&intf->vsync_wait);
680         rwlock_init(&intf->vsync_lock);
681         rwlock_init(&intf->hotplug_modelist_lock);
682
683         ret = adf_interface_sysfs_init(intf);
684         if (ret < 0)
685                 goto err;
686         dev->n_interfaces++;
687
688         return 0;
689
690 err:
691         adf_obj_destroy(&intf->base, &dev->interfaces);
692         return ret;
693 }
694 EXPORT_SYMBOL(adf_interface_init);
695
696 /**
697  * adf_interface_destroy - clean up ADF-internal data for a display interface
698  *
699  * @intf: the display interface
700  */
701 void adf_interface_destroy(struct adf_interface *intf)
702 {
703         struct adf_device *dev = adf_interface_parent(intf);
704         struct adf_attachment_list *entry, *next;
705
706         mutex_lock(&dev->client_lock);
707         list_for_each_entry_safe(entry, next, &dev->attach_allowed, head) {
708                 if (entry->attachment.interface == intf) {
709                         adf_attachment_free(entry);
710                         dev->n_attach_allowed--;
711                 }
712         }
713         list_for_each_entry_safe(entry, next, &dev->attached, head) {
714                 if (entry->attachment.interface == intf) {
715                         adf_device_detach_op(dev,
716                                         entry->attachment.overlay_engine, intf);
717                         adf_attachment_free(entry);
718                         dev->n_attached--;
719                 }
720         }
721         kfree(intf->modelist);
722         adf_interface_sysfs_destroy(intf);
723         adf_obj_destroy(&intf->base, &dev->interfaces);
724         dev->n_interfaces--;
725         mutex_unlock(&dev->client_lock);
726 }
727 EXPORT_SYMBOL(adf_interface_destroy);
728
729 static bool adf_overlay_engine_has_custom_formats(
730                 const struct adf_overlay_engine_ops *ops)
731 {
732         size_t i;
733         for (i = 0; i < ops->n_supported_formats; i++)
734                 if (!adf_format_is_standard(ops->supported_formats[i]))
735                         return true;
736         return false;
737 }
738
739 /**
740  * adf_overlay_engine_init - initialize ADF-internal data for an
741  * overlay engine and create sysfs entries
742  *
743  * @eng: the overlay engine
744  * @dev: the overlay engine's "parent" display device
745  * @ops: the overlay engine's associated ops
746  * @fmt: formatting string for the overlay engine's name
747  *
748  * @dev must have previously been initialized with adf_device_init().
749  *
750  * @fmt affects the name returned to userspace through the
751  * %ADF_GET_OVERLAY_ENGINE_DATA ioctl.  It does not affect the sysfs filename,
752  * which is derived from @dev's name.
753  *
754  * Returns 0 on success or error code (<0) on failure.
755  */
756 int adf_overlay_engine_init(struct adf_overlay_engine *eng,
757                 struct adf_device *dev,
758                 const struct adf_overlay_engine_ops *ops, const char *fmt, ...)
759 {
760         int ret;
761         va_list args;
762
763         if (!ops->supported_formats) {
764                 pr_err("%s: overlay engine must support at least one format\n",
765                                 __func__);
766                 return -EINVAL;
767         }
768
769         if (ops->n_supported_formats > ADF_MAX_SUPPORTED_FORMATS) {
770                 pr_err("%s: overlay engine supports too many formats\n",
771                                 __func__);
772                 return -EINVAL;
773         }
774
775         if (adf_overlay_engine_has_custom_formats(ops) &&
776                         !dev->ops->validate_custom_format) {
777                 pr_err("%s: overlay engine has custom formats but parent device %s does not implement validate_custom_format\n",
778                                 __func__, dev->base.name);
779                 return -EINVAL;
780         }
781
782         memset(eng, 0, sizeof(*eng));
783
784         va_start(args, fmt);
785         ret = adf_obj_init(&eng->base, ADF_OBJ_OVERLAY_ENGINE,
786                         &dev->overlay_engines, dev, &ops->base, fmt, args);
787         va_end(args);
788         if (ret < 0)
789                 return ret;
790
791         eng->ops = ops;
792
793         ret = adf_overlay_engine_sysfs_init(eng);
794         if (ret < 0)
795                 goto err;
796
797         return 0;
798
799 err:
800         adf_obj_destroy(&eng->base, &dev->overlay_engines);
801         return ret;
802 }
803 EXPORT_SYMBOL(adf_overlay_engine_init);
804
805 /**
806  * adf_interface_destroy - clean up ADF-internal data for an overlay engine
807  *
808  * @eng: the overlay engine
809  */
810 void adf_overlay_engine_destroy(struct adf_overlay_engine *eng)
811 {
812         struct adf_device *dev = adf_overlay_engine_parent(eng);
813         struct adf_attachment_list *entry, *next;
814
815         mutex_lock(&dev->client_lock);
816         list_for_each_entry_safe(entry, next, &dev->attach_allowed, head) {
817                 if (entry->attachment.overlay_engine == eng) {
818                         adf_attachment_free(entry);
819                         dev->n_attach_allowed--;
820                 }
821         }
822         list_for_each_entry_safe(entry, next, &dev->attached, head) {
823                 if (entry->attachment.overlay_engine == eng) {
824                         adf_device_detach_op(dev, eng,
825                                         entry->attachment.interface);
826                         adf_attachment_free(entry);
827                         dev->n_attached--;
828                 }
829         }
830         adf_overlay_engine_sysfs_destroy(eng);
831         adf_obj_destroy(&eng->base, &dev->overlay_engines);
832         mutex_unlock(&dev->client_lock);
833 }
834 EXPORT_SYMBOL(adf_overlay_engine_destroy);
835
836 struct adf_attachment_list *adf_attachment_find(struct list_head *list,
837                 struct adf_overlay_engine *eng, struct adf_interface *intf)
838 {
839         struct adf_attachment_list *entry;
840         list_for_each_entry(entry, list, head) {
841                 if (entry->attachment.interface == intf &&
842                                 entry->attachment.overlay_engine == eng)
843                         return entry;
844         }
845         return NULL;
846 }
847
848 int adf_attachment_validate(struct adf_device *dev,
849                 struct adf_overlay_engine *eng, struct adf_interface *intf)
850 {
851         struct adf_device *intf_dev = adf_interface_parent(intf);
852         struct adf_device *eng_dev = adf_overlay_engine_parent(eng);
853
854         if (intf_dev != dev) {
855                 dev_err(&dev->base.dev, "can't attach interface %s belonging to device %s\n",
856                                 intf->base.name, intf_dev->base.name);
857                 return -EINVAL;
858         }
859
860         if (eng_dev != dev) {
861                 dev_err(&dev->base.dev, "can't attach overlay engine %s belonging to device %s\n",
862                                 eng->base.name, eng_dev->base.name);
863                 return -EINVAL;
864         }
865
866         return 0;
867 }
868
869 /**
870  * adf_attachment_allow - add a new entry to the list of allowed
871  * attachments
872  *
873  * @dev: the parent device
874  * @eng: the overlay engine
875  * @intf: the interface
876  *
877  * adf_attachment_allow() indicates that the underlying display hardware allows
878  * @intf to scan out @eng's output.  It is intended to be called at
879  * driver initialization for each supported overlay engine + interface pair.
880  *
881  * Returns 0 on success, -%EALREADY if the entry already exists, or -errno on
882  * any other failure.
883  */
884 int adf_attachment_allow(struct adf_device *dev,
885                 struct adf_overlay_engine *eng, struct adf_interface *intf)
886 {
887         int ret;
888         struct adf_attachment_list *entry = NULL;
889
890         ret = adf_attachment_validate(dev, eng, intf);
891         if (ret < 0)
892                 return ret;
893
894         mutex_lock(&dev->client_lock);
895
896         if (dev->n_attach_allowed == ADF_MAX_ATTACHMENTS) {
897                 ret = -ENOMEM;
898                 goto done;
899         }
900
901         if (adf_attachment_find(&dev->attach_allowed, eng, intf)) {
902                 ret = -EALREADY;
903                 goto done;
904         }
905
906         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
907         if (!entry) {
908                 ret = -ENOMEM;
909                 goto done;
910         }
911
912         entry->attachment.interface = intf;
913         entry->attachment.overlay_engine = eng;
914         list_add_tail(&entry->head, &dev->attach_allowed);
915         dev->n_attach_allowed++;
916
917 done:
918         mutex_unlock(&dev->client_lock);
919         if (ret < 0)
920                 kfree(entry);
921
922         return ret;
923 }
924
925 /**
926  * adf_obj_type_str - string representation of an adf_obj_type
927  *
928  * @type: the object type
929  */
930 const char *adf_obj_type_str(enum adf_obj_type type)
931 {
932         switch (type) {
933         case ADF_OBJ_OVERLAY_ENGINE:
934                 return "overlay engine";
935
936         case ADF_OBJ_INTERFACE:
937                 return "interface";
938
939         case ADF_OBJ_DEVICE:
940                 return "device";
941
942         default:
943                 return "unknown";
944         }
945 }
946 EXPORT_SYMBOL(adf_obj_type_str);
947
948 /**
949  * adf_interface_type_str - string representation of an adf_interface's type
950  *
951  * @intf: the interface
952  */
953 const char *adf_interface_type_str(struct adf_interface *intf)
954 {
955         switch (intf->type) {
956         case ADF_INTF_DSI:
957                 return "DSI";
958
959         case ADF_INTF_eDP:
960                 return "eDP";
961
962         case ADF_INTF_DPI:
963                 return "DPI";
964
965         case ADF_INTF_VGA:
966                 return "VGA";
967
968         case ADF_INTF_DVI:
969                 return "DVI";
970
971         case ADF_INTF_HDMI:
972                 return "HDMI";
973
974         case ADF_INTF_MEMORY:
975                 return "memory";
976
977         default:
978                 if (intf->type >= ADF_INTF_TYPE_DEVICE_CUSTOM) {
979                         if (intf->ops && intf->ops->type_str)
980                                 return intf->ops->type_str(intf);
981                         return "custom";
982                 }
983                 return "unknown";
984         }
985 }
986 EXPORT_SYMBOL(adf_interface_type_str);
987
988 /**
989  * adf_event_type_str - string representation of an adf_event_type
990  *
991  * @obj: ADF object that produced the event
992  * @type: event type
993  */
994 const char *adf_event_type_str(struct adf_obj *obj, enum adf_event_type type)
995 {
996         switch (type) {
997         case ADF_EVENT_VSYNC:
998                 return "vsync";
999
1000         case ADF_EVENT_HOTPLUG:
1001                 return "hotplug";
1002
1003         default:
1004                 if (type >= ADF_EVENT_DEVICE_CUSTOM) {
1005                         if (obj->ops && obj->ops->event_type_str)
1006                                 return obj->ops->event_type_str(obj, type);
1007                         return "custom";
1008                 }
1009                 return "unknown";
1010         }
1011 }
1012 EXPORT_SYMBOL(adf_event_type_str);
1013
1014 /**
1015  * adf_format_str - string representation of an ADF/DRM fourcc format
1016  *
1017  * @format: format fourcc
1018  * @buf: target buffer for the format's string representation
1019  */
1020 void adf_format_str(u32 format, char buf[ADF_FORMAT_STR_SIZE])
1021 {
1022         buf[0] = format & 0xFF;
1023         buf[1] = (format >> 8) & 0xFF;
1024         buf[2] = (format >> 16) & 0xFF;
1025         buf[3] = (format >> 24) & 0xFF;
1026         buf[4] = '\0';
1027 }
1028 EXPORT_SYMBOL(adf_format_str);
1029
1030 void adf_modeinfo_set_name(struct drm_mode_modeinfo *mode)
1031 {
1032         bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1033
1034         snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
1035                  mode->hdisplay, mode->vdisplay,
1036                  interlaced ? "i" : "");
1037 }
1038
1039 void adf_modeinfo_set_vrefresh(struct drm_mode_modeinfo *mode)
1040 {
1041         int refresh = 0;
1042         unsigned int calc_val;
1043
1044         if (mode->vrefresh > 0)
1045                 return;
1046
1047         if (mode->htotal <= 0 || mode->vtotal <= 0)
1048                 return;
1049
1050         /* work out vrefresh the value will be x1000 */
1051         calc_val = (mode->clock * 1000);
1052         calc_val /= mode->htotal;
1053         refresh = (calc_val + mode->vtotal / 2) / mode->vtotal;
1054
1055         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1056                 refresh *= 2;
1057         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1058                 refresh /= 2;
1059         if (mode->vscan > 1)
1060                 refresh /= mode->vscan;
1061
1062         mode->vrefresh = refresh;
1063 }
1064
1065 static int __init adf_init(void)
1066 {
1067         int err;
1068
1069         err = adf_sysfs_init();
1070         if (err < 0)
1071                 return err;
1072
1073         return 0;
1074 }
1075
1076 static void __exit adf_exit(void)
1077 {
1078         adf_sysfs_destroy();
1079 }
1080
1081 module_init(adf_init);
1082 module_exit(adf_exit);