Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_crtc.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Authors: |
| 5 | * Inki Dae <inki.dae@samsung.com> |
| 6 | * Joonyoung Shim <jy0922.shim@samsung.com> |
| 7 | * Seung-Woo Kim <sw0312.kim@samsung.com> |
| 8 | * |
Inki Dae | d81aecb | 2012-12-18 02:30:17 +0900 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 13 | */ |
| 14 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 15 | #include <drm/drmP.h> |
| 16 | #include <drm/drm_crtc_helper.h> |
Gustavo Padovan | 4ea9526 | 2015-06-01 12:04:44 -0300 | [diff] [blame] | 17 | #include <drm/drm_atomic.h> |
| 18 | #include <drm/drm_atomic_helper.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 19 | |
Mark Brown | e30655d | 2013-08-13 00:46:40 +0100 | [diff] [blame] | 20 | #include "exynos_drm_crtc.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 21 | #include "exynos_drm_drv.h" |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 22 | #include "exynos_drm_plane.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 23 | |
Laurent Pinchart | 0b20a0f | 2017-06-30 12:36:44 +0300 | [diff] [blame] | 24 | static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc, |
| 25 | struct drm_crtc_state *old_state) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 26 | { |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 27 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 28 | |
Gustavo Padovan | 3cecda0 | 2015-06-01 12:04:55 -0300 | [diff] [blame] | 29 | if (exynos_crtc->ops->enable) |
| 30 | exynos_crtc->ops->enable(exynos_crtc); |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 31 | |
Gustavo Padovan | 63498e3 | 2015-06-01 12:04:53 -0300 | [diff] [blame] | 32 | drm_crtc_vblank_on(crtc); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 33 | } |
| 34 | |
Laurent Pinchart | 6458171 | 2017-06-30 12:36:45 +0300 | [diff] [blame^] | 35 | static void exynos_drm_crtc_atomic_disable(struct drm_crtc *crtc, |
| 36 | struct drm_crtc_state *old_state) |
Gustavo Padovan | 3fc4867 | 2015-06-01 12:04:51 -0300 | [diff] [blame] | 37 | { |
Gustavo Padovan | 63498e3 | 2015-06-01 12:04:53 -0300 | [diff] [blame] | 38 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
Gustavo Padovan | 3fc4867 | 2015-06-01 12:04:51 -0300 | [diff] [blame] | 39 | |
Gustavo Padovan | 63498e3 | 2015-06-01 12:04:53 -0300 | [diff] [blame] | 40 | drm_crtc_vblank_off(crtc); |
| 41 | |
Gustavo Padovan | 3cecda0 | 2015-06-01 12:04:55 -0300 | [diff] [blame] | 42 | if (exynos_crtc->ops->disable) |
| 43 | exynos_crtc->ops->disable(exynos_crtc); |
Inki Dae | 41cbf0f | 2017-01-20 12:51:41 +0900 | [diff] [blame] | 44 | |
| 45 | if (crtc->state->event && !crtc->state->active) { |
| 46 | spin_lock_irq(&crtc->dev->event_lock); |
| 47 | drm_crtc_send_vblank_event(crtc, crtc->state->event); |
| 48 | spin_unlock_irq(&crtc->dev->event_lock); |
| 49 | |
| 50 | crtc->state->event = NULL; |
| 51 | } |
Gustavo Padovan | 3fc4867 | 2015-06-01 12:04:51 -0300 | [diff] [blame] | 52 | } |
| 53 | |
Andrzej Hajda | 5625b34 | 2015-10-26 13:03:39 +0100 | [diff] [blame] | 54 | static int exynos_crtc_atomic_check(struct drm_crtc *crtc, |
| 55 | struct drm_crtc_state *state) |
| 56 | { |
| 57 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 58 | |
Andrzej Hajda | c4e0740 | 2015-11-27 07:57:31 +0100 | [diff] [blame] | 59 | if (!state->enable) |
| 60 | return 0; |
| 61 | |
Andrzej Hajda | 5625b34 | 2015-10-26 13:03:39 +0100 | [diff] [blame] | 62 | if (exynos_crtc->ops->atomic_check) |
| 63 | return exynos_crtc->ops->atomic_check(exynos_crtc, state); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Maarten Lankhorst | 613d2b2 | 2015-07-21 13:28:58 +0200 | [diff] [blame] | 68 | static void exynos_crtc_atomic_begin(struct drm_crtc *crtc, |
| 69 | struct drm_crtc_state *old_crtc_state) |
Gustavo Padovan | 9d5ab6a | 2015-06-01 12:04:48 -0300 | [diff] [blame] | 70 | { |
| 71 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 72 | |
Marek Szyprowski | d29c2c1 | 2016-01-05 13:52:51 +0100 | [diff] [blame] | 73 | if (exynos_crtc->ops->atomic_begin) |
| 74 | exynos_crtc->ops->atomic_begin(exynos_crtc); |
Gustavo Padovan | 9d5ab6a | 2015-06-01 12:04:48 -0300 | [diff] [blame] | 75 | } |
| 76 | |
Maarten Lankhorst | 613d2b2 | 2015-07-21 13:28:58 +0200 | [diff] [blame] | 77 | static void exynos_crtc_atomic_flush(struct drm_crtc *crtc, |
| 78 | struct drm_crtc_state *old_crtc_state) |
Gustavo Padovan | 9d5ab6a | 2015-06-01 12:04:48 -0300 | [diff] [blame] | 79 | { |
Gustavo Padovan | d9220d4 | 2015-08-24 20:36:59 +0900 | [diff] [blame] | 80 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
Gustavo Padovan | d9220d4 | 2015-08-24 20:36:59 +0900 | [diff] [blame] | 81 | |
Marek Szyprowski | d29c2c1 | 2016-01-05 13:52:51 +0100 | [diff] [blame] | 82 | if (exynos_crtc->ops->atomic_flush) |
| 83 | exynos_crtc->ops->atomic_flush(exynos_crtc); |
Gustavo Padovan | 9d5ab6a | 2015-06-01 12:04:48 -0300 | [diff] [blame] | 84 | } |
| 85 | |
Ville Syrjälä | 800ba2b | 2015-12-15 12:21:06 +0100 | [diff] [blame] | 86 | static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = { |
Andrzej Hajda | 5625b34 | 2015-10-26 13:03:39 +0100 | [diff] [blame] | 87 | .atomic_check = exynos_crtc_atomic_check, |
Gustavo Padovan | 9d5ab6a | 2015-06-01 12:04:48 -0300 | [diff] [blame] | 88 | .atomic_begin = exynos_crtc_atomic_begin, |
| 89 | .atomic_flush = exynos_crtc_atomic_flush, |
Laurent Pinchart | 0b20a0f | 2017-06-30 12:36:44 +0300 | [diff] [blame] | 90 | .atomic_enable = exynos_drm_crtc_atomic_enable, |
Laurent Pinchart | 6458171 | 2017-06-30 12:36:45 +0300 | [diff] [blame^] | 91 | .atomic_disable = exynos_drm_crtc_atomic_disable, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 92 | }; |
| 93 | |
Andrzej Hajda | a392276 | 2017-03-14 09:27:56 +0100 | [diff] [blame] | 94 | void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc) |
| 95 | { |
| 96 | struct drm_crtc *crtc = &exynos_crtc->base; |
| 97 | struct drm_pending_vblank_event *event = crtc->state->event; |
| 98 | unsigned long flags; |
| 99 | |
Andrzej Hajda | 73b7b44 | 2017-03-15 15:41:01 +0100 | [diff] [blame] | 100 | if (!event) |
| 101 | return; |
| 102 | crtc->state->event = NULL; |
Andrzej Hajda | a392276 | 2017-03-14 09:27:56 +0100 | [diff] [blame] | 103 | |
Andrzej Hajda | 73b7b44 | 2017-03-15 15:41:01 +0100 | [diff] [blame] | 104 | WARN_ON(drm_crtc_vblank_get(crtc) != 0); |
| 105 | |
| 106 | spin_lock_irqsave(&crtc->dev->event_lock, flags); |
| 107 | drm_crtc_arm_vblank_event(crtc, event); |
| 108 | spin_unlock_irqrestore(&crtc->dev->event_lock, flags); |
Andrzej Hajda | a392276 | 2017-03-14 09:27:56 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 111 | static void exynos_drm_crtc_destroy(struct drm_crtc *crtc) |
| 112 | { |
| 113 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 114 | |
| 115 | drm_crtc_cleanup(crtc); |
| 116 | kfree(exynos_crtc); |
| 117 | } |
| 118 | |
Shawn Guo | 64b0e1d | 2017-02-07 17:16:20 +0800 | [diff] [blame] | 119 | static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc) |
| 120 | { |
| 121 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 122 | |
| 123 | if (exynos_crtc->ops->enable_vblank) |
| 124 | return exynos_crtc->ops->enable_vblank(exynos_crtc); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc) |
| 130 | { |
| 131 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 132 | |
| 133 | if (exynos_crtc->ops->disable_vblank) |
| 134 | exynos_crtc->ops->disable_vblank(exynos_crtc); |
| 135 | } |
| 136 | |
Andrzej Hajda | 0586feb | 2017-03-15 15:41:02 +0100 | [diff] [blame] | 137 | static u32 exynos_drm_crtc_get_vblank_counter(struct drm_crtc *crtc) |
| 138 | { |
| 139 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 140 | |
| 141 | if (exynos_crtc->ops->get_vblank_counter) |
| 142 | return exynos_crtc->ops->get_vblank_counter(exynos_crtc); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Ville Syrjälä | 800ba2b | 2015-12-15 12:21:06 +0100 | [diff] [blame] | 147 | static const struct drm_crtc_funcs exynos_crtc_funcs = { |
Gustavo Padovan | 47a7def | 2015-06-01 12:04:47 -0300 | [diff] [blame] | 148 | .set_config = drm_atomic_helper_set_config, |
Gustavo Padovan | 9d5ab6a | 2015-06-01 12:04:48 -0300 | [diff] [blame] | 149 | .page_flip = drm_atomic_helper_page_flip, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 150 | .destroy = exynos_drm_crtc_destroy, |
Gustavo Padovan | 4ea9526 | 2015-06-01 12:04:44 -0300 | [diff] [blame] | 151 | .reset = drm_atomic_helper_crtc_reset, |
| 152 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, |
| 153 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, |
Shawn Guo | 64b0e1d | 2017-02-07 17:16:20 +0800 | [diff] [blame] | 154 | .enable_vblank = exynos_drm_crtc_enable_vblank, |
| 155 | .disable_vblank = exynos_drm_crtc_disable_vblank, |
Andrzej Hajda | 0586feb | 2017-03-15 15:41:02 +0100 | [diff] [blame] | 156 | .get_vblank_counter = exynos_drm_crtc_get_vblank_counter, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 157 | }; |
| 158 | |
Gustavo Padovan | 93bca24 | 2015-01-18 18:16:23 +0900 | [diff] [blame] | 159 | struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev, |
Krzysztof Kozlowski | f3aaf76 | 2015-05-07 09:04:45 +0900 | [diff] [blame] | 160 | struct drm_plane *plane, |
Krzysztof Kozlowski | f3aaf76 | 2015-05-07 09:04:45 +0900 | [diff] [blame] | 161 | enum exynos_drm_output_type type, |
| 162 | const struct exynos_drm_crtc_ops *ops, |
| 163 | void *ctx) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 164 | { |
| 165 | struct exynos_drm_crtc *exynos_crtc; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 166 | struct drm_crtc *crtc; |
Andrzej Hajda | 72ed6cc | 2014-09-19 14:58:53 +0200 | [diff] [blame] | 167 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 168 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 169 | exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL); |
Sachin Kamat | 38bb525 | 2013-08-19 19:04:55 +0900 | [diff] [blame] | 170 | if (!exynos_crtc) |
Gustavo Padovan | 93bca24 | 2015-01-18 18:16:23 +0900 | [diff] [blame] | 171 | return ERR_PTR(-ENOMEM); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 172 | |
Gustavo Padovan | 5d1741a | 2014-11-05 19:51:35 -0200 | [diff] [blame] | 173 | exynos_crtc->type = type; |
Gustavo Padovan | 93bca24 | 2015-01-18 18:16:23 +0900 | [diff] [blame] | 174 | exynos_crtc->ops = ops; |
| 175 | exynos_crtc->ctx = ctx; |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 176 | |
Gustavo Padovan | 357193c | 2014-11-03 18:20:29 -0200 | [diff] [blame] | 177 | crtc = &exynos_crtc->base; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 178 | |
Gustavo Padovan | eb88e42 | 2014-11-26 16:43:27 -0200 | [diff] [blame] | 179 | ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL, |
Ville Syrjälä | f988287 | 2015-12-09 16:19:31 +0200 | [diff] [blame] | 180 | &exynos_crtc_funcs, NULL); |
Andrzej Hajda | 72ed6cc | 2014-09-19 14:58:53 +0200 | [diff] [blame] | 181 | if (ret < 0) |
| 182 | goto err_crtc; |
| 183 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 184 | drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs); |
| 185 | |
Gustavo Padovan | 93bca24 | 2015-01-18 18:16:23 +0900 | [diff] [blame] | 186 | return exynos_crtc; |
Andrzej Hajda | 72ed6cc | 2014-09-19 14:58:53 +0200 | [diff] [blame] | 187 | |
| 188 | err_crtc: |
| 189 | plane->funcs->destroy(plane); |
Andrzej Hajda | 72ed6cc | 2014-09-19 14:58:53 +0200 | [diff] [blame] | 190 | kfree(exynos_crtc); |
Gustavo Padovan | 93bca24 | 2015-01-18 18:16:23 +0900 | [diff] [blame] | 191 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 192 | } |
| 193 | |
Inki Dae | f37cd5e | 2014-05-09 14:25:20 +0900 | [diff] [blame] | 194 | int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev, |
Gustavo Padovan | cf67cc9 | 2015-08-11 17:38:06 +0900 | [diff] [blame] | 195 | enum exynos_drm_output_type out_type) |
Inki Dae | f37cd5e | 2014-05-09 14:25:20 +0900 | [diff] [blame] | 196 | { |
| 197 | struct drm_crtc *crtc; |
| 198 | |
Andrzej Hajda | d644951 | 2017-05-29 10:05:25 +0900 | [diff] [blame] | 199 | drm_for_each_crtc(crtc, drm_dev) |
| 200 | if (to_exynos_crtc(crtc)->type == out_type) |
| 201 | return drm_crtc_index(crtc); |
Inki Dae | f37cd5e | 2014-05-09 14:25:20 +0900 | [diff] [blame] | 202 | |
| 203 | return -EPERM; |
| 204 | } |
YoungJun Cho | 5595d4d | 2014-07-17 18:01:19 +0900 | [diff] [blame] | 205 | |
| 206 | void exynos_drm_crtc_te_handler(struct drm_crtc *crtc) |
| 207 | { |
Gustavo Padovan | 93bca24 | 2015-01-18 18:16:23 +0900 | [diff] [blame] | 208 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
YoungJun Cho | 5595d4d | 2014-07-17 18:01:19 +0900 | [diff] [blame] | 209 | |
Gustavo Padovan | 93bca24 | 2015-01-18 18:16:23 +0900 | [diff] [blame] | 210 | if (exynos_crtc->ops->te_handler) |
| 211 | exynos_crtc->ops->te_handler(exynos_crtc); |
YoungJun Cho | 5595d4d | 2014-07-17 18:01:19 +0900 | [diff] [blame] | 212 | } |