1 // SPDX-License-Identifier: GPL-2.0
3 * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
4 * Author: James.Qian.Wang <james.qian.wang@arm.com>
7 #include <linux/component.h>
8 #include <linux/interrupt.h>
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_managed.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_vblank.h>
21 #include "komeda_dev.h"
22 #include "komeda_framebuffer.h"
23 #include "komeda_kms.h"
25 DEFINE_DRM_GEM_CMA_FOPS(komeda_cma_fops);
27 static int komeda_gem_cma_dumb_create(struct drm_file *file,
28 struct drm_device *dev,
29 struct drm_mode_create_dumb *args)
31 struct komeda_dev *mdev = dev->dev_private;
32 u32 pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
34 args->pitch = ALIGN(pitch, mdev->chip.bus_width);
36 return drm_gem_cma_dumb_create_internal(file, dev, args);
39 static irqreturn_t komeda_kms_irq_handler(int irq, void *data)
41 struct drm_device *drm = data;
42 struct komeda_dev *mdev = drm->dev_private;
43 struct komeda_kms_dev *kms = to_kdev(drm);
44 struct komeda_events evts;
48 /* Call into the CHIP to recognize events */
49 memset(&evts, 0, sizeof(evts));
50 status = mdev->funcs->irq_handler(mdev, &evts);
52 komeda_print_events(&evts, drm);
54 /* Notify the crtc to handle the events */
55 for (i = 0; i < kms->n_crtcs; i++)
56 komeda_crtc_handle_event(&kms->crtcs[i], &evts);
61 static const struct drm_driver komeda_kms_driver = {
62 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
63 .lastclose = drm_fb_helper_lastclose,
64 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(komeda_gem_cma_dumb_create),
65 .fops = &komeda_cma_fops,
67 .desc = "Arm Komeda Display Processor driver",
73 static void komeda_kms_commit_tail(struct drm_atomic_state *old_state)
75 struct drm_device *dev = old_state->dev;
77 drm_atomic_helper_commit_modeset_disables(dev, old_state);
79 drm_atomic_helper_commit_planes(dev, old_state,
80 DRM_PLANE_COMMIT_ACTIVE_ONLY);
82 drm_atomic_helper_commit_modeset_enables(dev, old_state);
84 drm_atomic_helper_wait_for_flip_done(dev, old_state);
86 drm_atomic_helper_commit_hw_done(old_state);
88 drm_atomic_helper_cleanup_planes(dev, old_state);
91 static const struct drm_mode_config_helper_funcs komeda_mode_config_helpers = {
92 .atomic_commit_tail = komeda_kms_commit_tail,
95 static int komeda_plane_state_list_add(struct drm_plane_state *plane_st,
96 struct list_head *zorder_list)
98 struct komeda_plane_state *new = to_kplane_st(plane_st);
99 struct komeda_plane_state *node, *last;
101 last = list_empty(zorder_list) ?
102 NULL : list_last_entry(zorder_list, typeof(*last), zlist_node);
104 /* Considering the list sequence is zpos increasing, so if list is empty
105 * or the zpos of new node bigger than the last node in list, no need
106 * loop and just insert the new one to the tail of the list.
108 if (!last || (new->base.zpos > last->base.zpos)) {
109 list_add_tail(&new->zlist_node, zorder_list);
113 /* Build the list by zpos increasing */
114 list_for_each_entry(node, zorder_list, zlist_node) {
115 if (new->base.zpos < node->base.zpos) {
116 list_add_tail(&new->zlist_node, &node->zlist_node);
118 } else if (node->base.zpos == new->base.zpos) {
119 struct drm_plane *a = node->base.plane;
120 struct drm_plane *b = new->base.plane;
122 /* Komeda doesn't support setting a same zpos for
125 DRM_DEBUG_ATOMIC("PLANE: %s and PLANE: %s are configured same zpos: %d.\n",
126 a->name, b->name, node->base.zpos);
134 static int komeda_crtc_normalize_zpos(struct drm_crtc *crtc,
135 struct drm_crtc_state *crtc_st)
137 struct drm_atomic_state *state = crtc_st->state;
138 struct komeda_crtc *kcrtc = to_kcrtc(crtc);
139 struct komeda_crtc_state *kcrtc_st = to_kcrtc_st(crtc_st);
140 struct komeda_plane_state *kplane_st;
141 struct drm_plane_state *plane_st;
142 struct drm_plane *plane;
143 struct list_head zorder_list;
146 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
147 crtc->base.id, crtc->name);
149 INIT_LIST_HEAD(&zorder_list);
151 /* This loop also added all effected planes into the new state */
152 drm_for_each_plane_mask(plane, crtc->dev, crtc_st->plane_mask) {
153 plane_st = drm_atomic_get_plane_state(state, plane);
154 if (IS_ERR(plane_st))
155 return PTR_ERR(plane_st);
157 /* Build a list by zpos increasing */
158 err = komeda_plane_state_list_add(plane_st, &zorder_list);
163 kcrtc_st->max_slave_zorder = 0;
165 list_for_each_entry(kplane_st, &zorder_list, zlist_node) {
166 plane_st = &kplane_st->base;
167 plane = plane_st->plane;
169 plane_st->normalized_zpos = order++;
170 /* When layer_split has been enabled, one plane will be handled
171 * by two separated komeda layers (left/right), which may needs
173 * - zorder: for left_layer for left display part.
174 * - zorder + 1: will be reserved for right layer.
176 if (to_kplane_st(plane_st)->layer_split)
179 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] zpos:%d, normalized zpos: %d\n",
180 plane->base.id, plane->name,
181 plane_st->zpos, plane_st->normalized_zpos);
183 /* calculate max slave zorder */
184 if (has_bit(drm_plane_index(plane), kcrtc->slave_planes))
185 kcrtc_st->max_slave_zorder =
186 max(plane_st->normalized_zpos,
187 kcrtc_st->max_slave_zorder);
190 crtc_st->zpos_changed = true;
195 static int komeda_kms_check(struct drm_device *dev,
196 struct drm_atomic_state *state)
198 struct drm_crtc *crtc;
199 struct drm_crtc_state *new_crtc_st;
202 err = drm_atomic_helper_check_modeset(dev, state);
206 /* Komeda need to re-calculate resource assumption in every commit
207 * so need to add all affected_planes (even unchanged) to
210 for_each_new_crtc_in_state(state, crtc, new_crtc_st, i) {
211 err = drm_atomic_add_affected_planes(state, crtc);
215 err = komeda_crtc_normalize_zpos(crtc, new_crtc_st);
220 err = drm_atomic_helper_check_planes(dev, state);
227 static const struct drm_mode_config_funcs komeda_mode_config_funcs = {
228 .fb_create = komeda_fb_create,
229 .atomic_check = komeda_kms_check,
230 .atomic_commit = drm_atomic_helper_commit,
233 static void komeda_kms_mode_config_init(struct komeda_kms_dev *kms,
234 struct komeda_dev *mdev)
236 struct drm_mode_config *config = &kms->base.mode_config;
238 drm_mode_config_init(&kms->base);
240 komeda_kms_setup_crtcs(kms, mdev);
242 /* Get value from dev */
243 config->min_width = 0;
244 config->min_height = 0;
245 config->max_width = 4096;
246 config->max_height = 4096;
247 config->allow_fb_modifiers = true;
249 config->funcs = &komeda_mode_config_funcs;
250 config->helper_private = &komeda_mode_config_helpers;
253 struct komeda_kms_dev *komeda_kms_attach(struct komeda_dev *mdev)
255 struct komeda_kms_dev *kms;
256 struct drm_device *drm;
259 kms = devm_drm_dev_alloc(mdev->dev, &komeda_kms_driver,
260 struct komeda_kms_dev, base);
266 drm->dev_private = mdev;
268 komeda_kms_mode_config_init(kms, mdev);
270 err = komeda_kms_add_private_objs(kms, mdev);
272 goto cleanup_mode_config;
274 err = komeda_kms_add_planes(kms, mdev);
276 goto cleanup_mode_config;
278 err = drm_vblank_init(drm, kms->n_crtcs);
280 goto cleanup_mode_config;
282 err = komeda_kms_add_crtcs(kms, mdev);
284 goto cleanup_mode_config;
286 err = komeda_kms_add_wb_connectors(kms, mdev);
288 goto cleanup_mode_config;
290 err = component_bind_all(mdev->dev, kms);
292 goto cleanup_mode_config;
294 drm_mode_config_reset(drm);
296 err = devm_request_irq(drm->dev, mdev->irq,
297 komeda_kms_irq_handler, IRQF_SHARED,
298 drm->driver->name, drm);
300 goto free_component_binding;
302 drm->irq_enabled = true;
304 drm_kms_helper_poll_init(drm);
306 err = drm_dev_register(drm, 0);
308 goto free_interrupts;
313 drm_kms_helper_poll_fini(drm);
314 drm->irq_enabled = false;
315 free_component_binding:
316 component_unbind_all(mdev->dev, drm);
318 drm_mode_config_cleanup(drm);
319 komeda_kms_cleanup_private_objs(kms);
320 drm->dev_private = NULL;
324 void komeda_kms_detach(struct komeda_kms_dev *kms)
326 struct drm_device *drm = &kms->base;
327 struct komeda_dev *mdev = drm->dev_private;
329 drm_dev_unregister(drm);
330 drm_kms_helper_poll_fini(drm);
331 drm_atomic_helper_shutdown(drm);
332 drm->irq_enabled = false;
333 component_unbind_all(mdev->dev, drm);
334 drm_mode_config_cleanup(drm);
335 komeda_kms_cleanup_private_objs(kms);
336 drm->dev_private = NULL;