drm/arm/komeda: Remove DRIVER_HAVE_IRQ
[sfrench/cifs-2.6.git] / drivers / gpu / drm / arm / display / komeda / komeda_kms.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
4  * Author: James.Qian.Wang <james.qian.wang@arm.com>
5  *
6  */
7 #include <linux/component.h>
8 #include <linux/interrupt.h>
9
10 #include <drm/drm_atomic.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_drv.h>
13 #include <drm/drm_fb_helper.h>
14 #include <drm/drm_gem_cma_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_irq.h>
17 #include <drm/drm_vblank.h>
18
19 #include "komeda_dev.h"
20 #include "komeda_framebuffer.h"
21 #include "komeda_kms.h"
22
23 DEFINE_DRM_GEM_CMA_FOPS(komeda_cma_fops);
24
25 static int komeda_gem_cma_dumb_create(struct drm_file *file,
26                                       struct drm_device *dev,
27                                       struct drm_mode_create_dumb *args)
28 {
29         struct komeda_dev *mdev = dev->dev_private;
30         u32 pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
31
32         args->pitch = ALIGN(pitch, mdev->chip.bus_width);
33
34         return drm_gem_cma_dumb_create_internal(file, dev, args);
35 }
36
37 static irqreturn_t komeda_kms_irq_handler(int irq, void *data)
38 {
39         struct drm_device *drm = data;
40         struct komeda_dev *mdev = drm->dev_private;
41         struct komeda_kms_dev *kms = to_kdev(drm);
42         struct komeda_events evts;
43         irqreturn_t status;
44         u32 i;
45
46         /* Call into the CHIP to recognize events */
47         memset(&evts, 0, sizeof(evts));
48         status = mdev->funcs->irq_handler(mdev, &evts);
49
50         /* Notify the crtc to handle the events */
51         for (i = 0; i < kms->n_crtcs; i++)
52                 komeda_crtc_handle_event(&kms->crtcs[i], &evts);
53
54         return status;
55 }
56
57 static struct drm_driver komeda_kms_driver = {
58         .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
59         .lastclose                      = drm_fb_helper_lastclose,
60         .irq_handler                    = komeda_kms_irq_handler,
61         .gem_free_object_unlocked       = drm_gem_cma_free_object,
62         .gem_vm_ops                     = &drm_gem_cma_vm_ops,
63         .dumb_create                    = komeda_gem_cma_dumb_create,
64         .prime_handle_to_fd             = drm_gem_prime_handle_to_fd,
65         .prime_fd_to_handle             = drm_gem_prime_fd_to_handle,
66         .gem_prime_export               = drm_gem_prime_export,
67         .gem_prime_import               = drm_gem_prime_import,
68         .gem_prime_get_sg_table         = drm_gem_cma_prime_get_sg_table,
69         .gem_prime_import_sg_table      = drm_gem_cma_prime_import_sg_table,
70         .gem_prime_vmap                 = drm_gem_cma_prime_vmap,
71         .gem_prime_vunmap               = drm_gem_cma_prime_vunmap,
72         .gem_prime_mmap                 = drm_gem_cma_prime_mmap,
73         .fops = &komeda_cma_fops,
74         .name = "komeda",
75         .desc = "Arm Komeda Display Processor driver",
76         .date = "20181101",
77         .major = 0,
78         .minor = 1,
79 };
80
81 static void komeda_kms_commit_tail(struct drm_atomic_state *old_state)
82 {
83         struct drm_device *dev = old_state->dev;
84
85         drm_atomic_helper_commit_modeset_disables(dev, old_state);
86
87         drm_atomic_helper_commit_planes(dev, old_state, 0);
88
89         drm_atomic_helper_commit_modeset_enables(dev, old_state);
90
91         drm_atomic_helper_wait_for_flip_done(dev, old_state);
92
93         drm_atomic_helper_commit_hw_done(old_state);
94
95         drm_atomic_helper_cleanup_planes(dev, old_state);
96 }
97
98 static const struct drm_mode_config_helper_funcs komeda_mode_config_helpers = {
99         .atomic_commit_tail = komeda_kms_commit_tail,
100 };
101
102 static int komeda_kms_check(struct drm_device *dev,
103                             struct drm_atomic_state *state)
104 {
105         struct drm_crtc *crtc;
106         struct drm_crtc_state *old_crtc_st, *new_crtc_st;
107         int i, err;
108
109         err = drm_atomic_helper_check_modeset(dev, state);
110         if (err)
111                 return err;
112
113         /* komeda need to re-calculate resource assumption in every commit
114          * so need to add all affected_planes (even unchanged) to
115          * drm_atomic_state.
116          */
117         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_st, new_crtc_st, i) {
118                 err = drm_atomic_add_affected_planes(state, crtc);
119                 if (err)
120                         return err;
121         }
122
123         err = drm_atomic_helper_check_planes(dev, state);
124         if (err)
125                 return err;
126
127         return 0;
128 }
129
130 static const struct drm_mode_config_funcs komeda_mode_config_funcs = {
131         .fb_create              = komeda_fb_create,
132         .atomic_check           = komeda_kms_check,
133         .atomic_commit          = drm_atomic_helper_commit,
134 };
135
136 static void komeda_kms_mode_config_init(struct komeda_kms_dev *kms,
137                                         struct komeda_dev *mdev)
138 {
139         struct drm_mode_config *config = &kms->base.mode_config;
140
141         drm_mode_config_init(&kms->base);
142
143         komeda_kms_setup_crtcs(kms, mdev);
144
145         /* Get value from dev */
146         config->min_width       = 0;
147         config->min_height      = 0;
148         config->max_width       = 4096;
149         config->max_height      = 4096;
150         config->allow_fb_modifiers = false;
151
152         config->funcs = &komeda_mode_config_funcs;
153         config->helper_private = &komeda_mode_config_helpers;
154 }
155
156 struct komeda_kms_dev *komeda_kms_attach(struct komeda_dev *mdev)
157 {
158         struct komeda_kms_dev *kms = kzalloc(sizeof(*kms), GFP_KERNEL);
159         struct drm_device *drm;
160         int err;
161
162         if (!kms)
163                 return ERR_PTR(-ENOMEM);
164
165         drm = &kms->base;
166         err = drm_dev_init(drm, &komeda_kms_driver, mdev->dev);
167         if (err)
168                 goto free_kms;
169
170         drm->dev_private = mdev;
171
172         komeda_kms_mode_config_init(kms, mdev);
173
174         err = komeda_kms_add_private_objs(kms, mdev);
175         if (err)
176                 goto cleanup_mode_config;
177
178         err = komeda_kms_add_planes(kms, mdev);
179         if (err)
180                 goto cleanup_mode_config;
181
182         err = drm_vblank_init(drm, kms->n_crtcs);
183         if (err)
184                 goto cleanup_mode_config;
185
186         err = komeda_kms_add_crtcs(kms, mdev);
187         if (err)
188                 goto cleanup_mode_config;
189
190         err = component_bind_all(mdev->dev, kms);
191         if (err)
192                 goto cleanup_mode_config;
193
194         drm_mode_config_reset(drm);
195
196         err = drm_irq_install(drm, mdev->irq);
197         if (err)
198                 goto cleanup_mode_config;
199
200         err = mdev->funcs->enable_irq(mdev);
201         if (err)
202                 goto uninstall_irq;
203
204         err = drm_dev_register(drm, 0);
205         if (err)
206                 goto uninstall_irq;
207
208         return kms;
209
210 uninstall_irq:
211         drm_irq_uninstall(drm);
212 cleanup_mode_config:
213         drm_mode_config_cleanup(drm);
214         komeda_kms_cleanup_private_objs(kms);
215 free_kms:
216         kfree(kms);
217         return ERR_PTR(err);
218 }
219
220 void komeda_kms_detach(struct komeda_kms_dev *kms)
221 {
222         struct drm_device *drm = &kms->base;
223         struct komeda_dev *mdev = drm->dev_private;
224
225         mdev->funcs->disable_irq(mdev);
226         drm_dev_unregister(drm);
227         drm_irq_uninstall(drm);
228         component_unbind_all(mdev->dev, drm);
229         komeda_kms_cleanup_private_objs(kms);
230         drm_mode_config_cleanup(drm);
231         drm->dev_private = NULL;
232         drm_dev_put(drm);
233 }