treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / omapdrm / omap_drv.c
blobd2750f60f519249831e5a519d1f185873ac1bd94
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
4 * Author: Rob Clark <rob@ti.com>
5 */
7 #include <linux/dma-mapping.h>
8 #include <linux/platform_device.h>
9 #include <linux/sort.h>
10 #include <linux/sys_soc.h>
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_bridge.h>
15 #include <drm/drm_drv.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_file.h>
18 #include <drm/drm_ioctl.h>
19 #include <drm/drm_panel.h>
20 #include <drm/drm_prime.h>
21 #include <drm/drm_probe_helper.h>
22 #include <drm/drm_vblank.h>
24 #include "omap_dmm_tiler.h"
25 #include "omap_drv.h"
27 #define DRIVER_NAME MODULE_NAME
28 #define DRIVER_DESC "OMAP DRM"
29 #define DRIVER_DATE "20110917"
30 #define DRIVER_MAJOR 1
31 #define DRIVER_MINOR 0
32 #define DRIVER_PATCHLEVEL 0
35 * mode config funcs
38 /* Notes about mapping DSS and DRM entities:
39 * CRTC: overlay
40 * encoder: manager.. with some extension to allow one primary CRTC
41 * and zero or more video CRTC's to be mapped to one encoder?
42 * connector: dssdev.. manager can be attached/detached from different
43 * devices
46 static void omap_atomic_wait_for_completion(struct drm_device *dev,
47 struct drm_atomic_state *old_state)
49 struct drm_crtc_state *new_crtc_state;
50 struct drm_crtc *crtc;
51 unsigned int i;
52 int ret;
54 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
55 if (!new_crtc_state->active)
56 continue;
58 ret = omap_crtc_wait_pending(crtc);
60 if (!ret)
61 dev_warn(dev->dev,
62 "atomic complete timeout (pipe %u)!\n", i);
66 static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
68 struct drm_device *dev = old_state->dev;
69 struct omap_drm_private *priv = dev->dev_private;
71 priv->dispc_ops->runtime_get(priv->dispc);
73 /* Apply the atomic update. */
74 drm_atomic_helper_commit_modeset_disables(dev, old_state);
76 if (priv->omaprev != 0x3430) {
77 /* With the current dss dispc implementation we have to enable
78 * the new modeset before we can commit planes. The dispc ovl
79 * configuration relies on the video mode configuration been
80 * written into the HW when the ovl configuration is
81 * calculated.
83 * This approach is not ideal because after a mode change the
84 * plane update is executed only after the first vblank
85 * interrupt. The dispc implementation should be fixed so that
86 * it is able use uncommitted drm state information.
88 drm_atomic_helper_commit_modeset_enables(dev, old_state);
89 omap_atomic_wait_for_completion(dev, old_state);
91 drm_atomic_helper_commit_planes(dev, old_state, 0);
93 drm_atomic_helper_commit_hw_done(old_state);
94 } else {
96 * OMAP3 DSS seems to have issues with the work-around above,
97 * resulting in endless sync losts if a crtc is enabled without
98 * a plane. For now, skip the WA for OMAP3.
100 drm_atomic_helper_commit_planes(dev, old_state, 0);
102 drm_atomic_helper_commit_modeset_enables(dev, old_state);
104 drm_atomic_helper_commit_hw_done(old_state);
108 * Wait for completion of the page flips to ensure that old buffers
109 * can't be touched by the hardware anymore before cleaning up planes.
111 omap_atomic_wait_for_completion(dev, old_state);
113 drm_atomic_helper_cleanup_planes(dev, old_state);
115 priv->dispc_ops->runtime_put(priv->dispc);
118 static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
119 .atomic_commit_tail = omap_atomic_commit_tail,
122 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
123 .fb_create = omap_framebuffer_create,
124 .output_poll_changed = drm_fb_helper_output_poll_changed,
125 .atomic_check = drm_atomic_helper_check,
126 .atomic_commit = drm_atomic_helper_commit,
129 static void omap_disconnect_pipelines(struct drm_device *ddev)
131 struct omap_drm_private *priv = ddev->dev_private;
132 unsigned int i;
134 for (i = 0; i < priv->num_pipes; i++) {
135 struct omap_drm_pipeline *pipe = &priv->pipes[i];
137 if (pipe->output->panel)
138 drm_panel_detach(pipe->output->panel);
140 omapdss_device_disconnect(NULL, pipe->output);
142 omapdss_device_put(pipe->output);
143 pipe->output = NULL;
146 memset(&priv->channels, 0, sizeof(priv->channels));
148 priv->num_pipes = 0;
151 static int omap_connect_pipelines(struct drm_device *ddev)
153 struct omap_drm_private *priv = ddev->dev_private;
154 struct omap_dss_device *output = NULL;
155 int r;
157 for_each_dss_output(output) {
158 r = omapdss_device_connect(priv->dss, NULL, output);
159 if (r == -EPROBE_DEFER) {
160 omapdss_device_put(output);
161 return r;
162 } else if (r) {
163 dev_warn(output->dev, "could not connect output %s\n",
164 output->name);
165 } else {
166 struct omap_drm_pipeline *pipe;
168 pipe = &priv->pipes[priv->num_pipes++];
169 pipe->output = omapdss_device_get(output);
171 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
172 /* To balance the 'for_each_dss_output' loop */
173 omapdss_device_put(output);
174 break;
179 return 0;
182 static int omap_compare_pipelines(const void *a, const void *b)
184 const struct omap_drm_pipeline *pipe1 = a;
185 const struct omap_drm_pipeline *pipe2 = b;
187 if (pipe1->alias_id > pipe2->alias_id)
188 return 1;
189 else if (pipe1->alias_id < pipe2->alias_id)
190 return -1;
191 return 0;
194 static int omap_modeset_init_properties(struct drm_device *dev)
196 struct omap_drm_private *priv = dev->dev_private;
197 unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
199 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
200 num_planes - 1);
201 if (!priv->zorder_prop)
202 return -ENOMEM;
204 return 0;
207 static int omap_display_id(struct omap_dss_device *output)
209 struct device_node *node = NULL;
211 if (output->next) {
212 struct omap_dss_device *display;
214 display = omapdss_display_get(output);
215 node = display->dev->of_node;
216 omapdss_device_put(display);
217 } else if (output->bridge) {
218 struct drm_bridge *bridge = output->bridge;
220 while (drm_bridge_get_next_bridge(bridge))
221 bridge = drm_bridge_get_next_bridge(bridge);
223 node = bridge->of_node;
224 } else if (output->panel) {
225 node = output->panel->dev->of_node;
228 return node ? of_alias_get_id(node, "display") : -ENODEV;
231 static int omap_modeset_init(struct drm_device *dev)
233 struct omap_drm_private *priv = dev->dev_private;
234 int num_ovls = priv->dispc_ops->get_num_ovls(priv->dispc);
235 int num_mgrs = priv->dispc_ops->get_num_mgrs(priv->dispc);
236 unsigned int i;
237 int ret;
238 u32 plane_crtc_mask;
240 if (!omapdss_stack_is_ready())
241 return -EPROBE_DEFER;
243 drm_mode_config_init(dev);
245 ret = omap_modeset_init_properties(dev);
246 if (ret < 0)
247 return ret;
250 * This function creates exactly one connector, encoder, crtc,
251 * and primary plane per each connected dss-device. Each
252 * connector->encoder->crtc chain is expected to be separate
253 * and each crtc is connect to a single dss-channel. If the
254 * configuration does not match the expectations or exceeds
255 * the available resources, the configuration is rejected.
257 ret = omap_connect_pipelines(dev);
258 if (ret < 0)
259 return ret;
261 if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
262 dev_err(dev->dev, "%s(): Too many connected displays\n",
263 __func__);
264 return -EINVAL;
267 /* Create all planes first. They can all be put to any CRTC. */
268 plane_crtc_mask = (1 << priv->num_pipes) - 1;
270 for (i = 0; i < num_ovls; i++) {
271 enum drm_plane_type type = i < priv->num_pipes
272 ? DRM_PLANE_TYPE_PRIMARY
273 : DRM_PLANE_TYPE_OVERLAY;
274 struct drm_plane *plane;
276 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
277 return -EINVAL;
279 plane = omap_plane_init(dev, i, type, plane_crtc_mask);
280 if (IS_ERR(plane))
281 return PTR_ERR(plane);
283 priv->planes[priv->num_planes++] = plane;
287 * Create the encoders, attach the bridges and get the pipeline alias
288 * IDs.
290 for (i = 0; i < priv->num_pipes; i++) {
291 struct omap_drm_pipeline *pipe = &priv->pipes[i];
292 int id;
294 pipe->encoder = omap_encoder_init(dev, pipe->output);
295 if (!pipe->encoder)
296 return -ENOMEM;
298 if (pipe->output->bridge) {
299 ret = drm_bridge_attach(pipe->encoder,
300 pipe->output->bridge, NULL);
301 if (ret < 0)
302 return ret;
305 id = omap_display_id(pipe->output);
306 pipe->alias_id = id >= 0 ? id : i;
309 /* Sort the pipelines by DT aliases. */
310 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
311 omap_compare_pipelines, NULL);
314 * Populate the pipeline lookup table by DISPC channel. Only one display
315 * is allowed per channel.
317 for (i = 0; i < priv->num_pipes; ++i) {
318 struct omap_drm_pipeline *pipe = &priv->pipes[i];
319 enum omap_channel channel = pipe->output->dispc_channel;
321 if (WARN_ON(priv->channels[channel] != NULL))
322 return -EINVAL;
324 priv->channels[channel] = pipe;
327 /* Create the connectors and CRTCs. */
328 for (i = 0; i < priv->num_pipes; i++) {
329 struct omap_drm_pipeline *pipe = &priv->pipes[i];
330 struct drm_encoder *encoder = pipe->encoder;
331 struct drm_crtc *crtc;
333 if (!pipe->output->bridge) {
334 pipe->connector = omap_connector_init(dev, pipe->output,
335 encoder);
336 if (!pipe->connector)
337 return -ENOMEM;
339 drm_connector_attach_encoder(pipe->connector, encoder);
341 if (pipe->output->panel) {
342 ret = drm_panel_attach(pipe->output->panel,
343 pipe->connector);
344 if (ret < 0)
345 return ret;
349 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
350 if (IS_ERR(crtc))
351 return PTR_ERR(crtc);
353 encoder->possible_crtcs = 1 << i;
354 pipe->crtc = crtc;
357 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
358 priv->num_planes, priv->num_pipes);
360 dev->mode_config.min_width = 8;
361 dev->mode_config.min_height = 2;
364 * Note: these values are used for multiple independent things:
365 * connector mode filtering, buffer sizes, crtc sizes...
366 * Use big enough values here to cover all use cases, and do more
367 * specific checking in the respective code paths.
369 dev->mode_config.max_width = 8192;
370 dev->mode_config.max_height = 8192;
372 /* We want the zpos to be normalized */
373 dev->mode_config.normalize_zpos = true;
375 dev->mode_config.funcs = &omap_mode_config_funcs;
376 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
378 drm_mode_config_reset(dev);
380 omap_drm_irq_install(dev);
382 return 0;
386 * Enable the HPD in external components if supported
388 static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
390 struct omap_drm_private *priv = ddev->dev_private;
391 unsigned int i;
393 for (i = 0; i < priv->num_pipes; i++) {
394 if (priv->pipes[i].connector)
395 omap_connector_enable_hpd(priv->pipes[i].connector);
400 * Disable the HPD in external components if supported
402 static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
404 struct omap_drm_private *priv = ddev->dev_private;
405 unsigned int i;
407 for (i = 0; i < priv->num_pipes; i++) {
408 if (priv->pipes[i].connector)
409 omap_connector_disable_hpd(priv->pipes[i].connector);
414 * drm ioctl funcs
418 static int ioctl_get_param(struct drm_device *dev, void *data,
419 struct drm_file *file_priv)
421 struct omap_drm_private *priv = dev->dev_private;
422 struct drm_omap_param *args = data;
424 DBG("%p: param=%llu", dev, args->param);
426 switch (args->param) {
427 case OMAP_PARAM_CHIPSET_ID:
428 args->value = priv->omaprev;
429 break;
430 default:
431 DBG("unknown parameter %lld", args->param);
432 return -EINVAL;
435 return 0;
438 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
440 static int ioctl_gem_new(struct drm_device *dev, void *data,
441 struct drm_file *file_priv)
443 struct drm_omap_gem_new *args = data;
444 u32 flags = args->flags & OMAP_BO_USER_MASK;
446 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
447 args->size.bytes, flags);
449 return omap_gem_new_handle(dev, file_priv, args->size, flags,
450 &args->handle);
453 static int ioctl_gem_info(struct drm_device *dev, void *data,
454 struct drm_file *file_priv)
456 struct drm_omap_gem_info *args = data;
457 struct drm_gem_object *obj;
458 int ret = 0;
460 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
462 obj = drm_gem_object_lookup(file_priv, args->handle);
463 if (!obj)
464 return -ENOENT;
466 args->size = omap_gem_mmap_size(obj);
467 args->offset = omap_gem_mmap_offset(obj);
469 drm_gem_object_put_unlocked(obj);
471 return ret;
474 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
475 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
476 DRM_RENDER_ALLOW),
477 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
478 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
479 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
480 DRM_RENDER_ALLOW),
481 /* Deprecated, to be removed. */
482 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
483 DRM_RENDER_ALLOW),
484 /* Deprecated, to be removed. */
485 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
486 DRM_RENDER_ALLOW),
487 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
488 DRM_RENDER_ALLOW),
492 * drm driver funcs
495 static int dev_open(struct drm_device *dev, struct drm_file *file)
497 file->driver_priv = NULL;
499 DBG("open: dev=%p, file=%p", dev, file);
501 return 0;
504 static const struct vm_operations_struct omap_gem_vm_ops = {
505 .fault = omap_gem_fault,
506 .open = drm_gem_vm_open,
507 .close = drm_gem_vm_close,
510 static const struct file_operations omapdriver_fops = {
511 .owner = THIS_MODULE,
512 .open = drm_open,
513 .unlocked_ioctl = drm_ioctl,
514 .compat_ioctl = drm_compat_ioctl,
515 .release = drm_release,
516 .mmap = omap_gem_mmap,
517 .poll = drm_poll,
518 .read = drm_read,
519 .llseek = noop_llseek,
522 static struct drm_driver omap_drm_driver = {
523 .driver_features = DRIVER_MODESET | DRIVER_GEM |
524 DRIVER_ATOMIC | DRIVER_RENDER,
525 .open = dev_open,
526 .lastclose = drm_fb_helper_lastclose,
527 #ifdef CONFIG_DEBUG_FS
528 .debugfs_init = omap_debugfs_init,
529 #endif
530 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
531 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
532 .gem_prime_export = omap_gem_prime_export,
533 .gem_prime_import = omap_gem_prime_import,
534 .gem_free_object_unlocked = omap_gem_free_object,
535 .gem_vm_ops = &omap_gem_vm_ops,
536 .dumb_create = omap_gem_dumb_create,
537 .dumb_map_offset = omap_gem_dumb_map_offset,
538 .ioctls = ioctls,
539 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
540 .fops = &omapdriver_fops,
541 .name = DRIVER_NAME,
542 .desc = DRIVER_DESC,
543 .date = DRIVER_DATE,
544 .major = DRIVER_MAJOR,
545 .minor = DRIVER_MINOR,
546 .patchlevel = DRIVER_PATCHLEVEL,
549 static const struct soc_device_attribute omapdrm_soc_devices[] = {
550 { .family = "OMAP3", .data = (void *)0x3430 },
551 { .family = "OMAP4", .data = (void *)0x4430 },
552 { .family = "OMAP5", .data = (void *)0x5430 },
553 { .family = "DRA7", .data = (void *)0x0752 },
554 { /* sentinel */ }
557 static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
559 const struct soc_device_attribute *soc;
560 struct drm_device *ddev;
561 unsigned int i;
562 int ret;
564 DBG("%s", dev_name(dev));
566 /* Allocate and initialize the DRM device. */
567 ddev = drm_dev_alloc(&omap_drm_driver, dev);
568 if (IS_ERR(ddev))
569 return PTR_ERR(ddev);
571 priv->ddev = ddev;
572 ddev->dev_private = priv;
574 priv->dev = dev;
575 priv->dss = omapdss_get_dss();
576 priv->dispc = dispc_get_dispc(priv->dss);
577 priv->dispc_ops = dispc_get_ops(priv->dss);
579 omap_crtc_pre_init(priv);
581 soc = soc_device_match(omapdrm_soc_devices);
582 priv->omaprev = soc ? (unsigned int)soc->data : 0;
583 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
585 mutex_init(&priv->list_lock);
586 INIT_LIST_HEAD(&priv->obj_list);
588 /* Get memory bandwidth limits */
589 if (priv->dispc_ops->get_memory_bandwidth_limit)
590 priv->max_bandwidth =
591 priv->dispc_ops->get_memory_bandwidth_limit(priv->dispc);
593 omap_gem_init(ddev);
595 ret = omap_modeset_init(ddev);
596 if (ret) {
597 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
598 goto err_gem_deinit;
601 /* Initialize vblank handling, start with all CRTCs disabled. */
602 ret = drm_vblank_init(ddev, priv->num_pipes);
603 if (ret) {
604 dev_err(priv->dev, "could not init vblank\n");
605 goto err_cleanup_modeset;
608 for (i = 0; i < priv->num_pipes; i++)
609 drm_crtc_vblank_off(priv->pipes[i].crtc);
611 omap_fbdev_init(ddev);
613 drm_kms_helper_poll_init(ddev);
614 omap_modeset_enable_external_hpd(ddev);
617 * Register the DRM device with the core and the connectors with
618 * sysfs.
620 ret = drm_dev_register(ddev, 0);
621 if (ret)
622 goto err_cleanup_helpers;
624 return 0;
626 err_cleanup_helpers:
627 omap_modeset_disable_external_hpd(ddev);
628 drm_kms_helper_poll_fini(ddev);
630 omap_fbdev_fini(ddev);
631 err_cleanup_modeset:
632 drm_mode_config_cleanup(ddev);
633 omap_drm_irq_uninstall(ddev);
634 err_gem_deinit:
635 omap_gem_deinit(ddev);
636 destroy_workqueue(priv->wq);
637 omap_disconnect_pipelines(ddev);
638 omap_crtc_pre_uninit(priv);
639 drm_dev_put(ddev);
640 return ret;
643 static void omapdrm_cleanup(struct omap_drm_private *priv)
645 struct drm_device *ddev = priv->ddev;
647 DBG("");
649 drm_dev_unregister(ddev);
651 omap_modeset_disable_external_hpd(ddev);
652 drm_kms_helper_poll_fini(ddev);
654 omap_fbdev_fini(ddev);
656 drm_atomic_helper_shutdown(ddev);
658 drm_mode_config_cleanup(ddev);
660 omap_drm_irq_uninstall(ddev);
661 omap_gem_deinit(ddev);
663 destroy_workqueue(priv->wq);
665 omap_disconnect_pipelines(ddev);
666 omap_crtc_pre_uninit(priv);
668 drm_dev_put(ddev);
671 static int pdev_probe(struct platform_device *pdev)
673 struct omap_drm_private *priv;
674 int ret;
676 if (omapdss_is_initialized() == false)
677 return -EPROBE_DEFER;
679 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
680 if (ret) {
681 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
682 return ret;
685 /* Allocate and initialize the driver private structure. */
686 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
687 if (!priv)
688 return -ENOMEM;
690 platform_set_drvdata(pdev, priv);
692 ret = omapdrm_init(priv, &pdev->dev);
693 if (ret < 0)
694 kfree(priv);
696 return ret;
699 static int pdev_remove(struct platform_device *pdev)
701 struct omap_drm_private *priv = platform_get_drvdata(pdev);
703 omapdrm_cleanup(priv);
704 kfree(priv);
706 return 0;
709 #ifdef CONFIG_PM_SLEEP
710 static int omap_drm_suspend(struct device *dev)
712 struct omap_drm_private *priv = dev_get_drvdata(dev);
713 struct drm_device *drm_dev = priv->ddev;
715 return drm_mode_config_helper_suspend(drm_dev);
718 static int omap_drm_resume(struct device *dev)
720 struct omap_drm_private *priv = dev_get_drvdata(dev);
721 struct drm_device *drm_dev = priv->ddev;
723 drm_mode_config_helper_resume(drm_dev);
725 return omap_gem_resume(drm_dev);
727 #endif
729 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
731 static struct platform_driver pdev = {
732 .driver = {
733 .name = "omapdrm",
734 .pm = &omapdrm_pm_ops,
736 .probe = pdev_probe,
737 .remove = pdev_remove,
740 static struct platform_driver * const drivers[] = {
741 &omap_dmm_driver,
742 &pdev,
745 static int __init omap_drm_init(void)
747 DBG("init");
749 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
752 static void __exit omap_drm_fini(void)
754 DBG("fini");
756 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
759 /* need late_initcall() so we load after dss_driver's are loaded */
760 late_initcall(omap_drm_init);
761 module_exit(omap_drm_fini);
763 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
764 MODULE_DESCRIPTION("OMAP DRM Display Driver");
765 MODULE_ALIAS("platform:" DRIVER_NAME);
766 MODULE_LICENSE("GPL v2");