Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Free Electrons |
| 3 | * Copyright (C) 2015 NextThing Co |
| 4 | * |
| 5 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <drm/drmP.h> |
| 14 | #include <drm/drm_atomic_helper.h> |
| 15 | #include <drm/drm_crtc.h> |
| 16 | #include <drm/drm_crtc_helper.h> |
| 17 | #include <drm/drm_modes.h> |
| 18 | |
| 19 | #include <linux/clk-provider.h> |
| 20 | #include <linux/ioport.h> |
| 21 | #include <linux/of_address.h> |
Chen-Yu Tsai | 7544860 | 2017-02-23 16:05:34 +0800 | [diff] [blame] | 22 | #include <linux/of_graph.h> |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 23 | #include <linux/of_irq.h> |
| 24 | #include <linux/regmap.h> |
| 25 | |
| 26 | #include <video/videomode.h> |
| 27 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 28 | #include "sun4i_crtc.h" |
| 29 | #include "sun4i_drv.h" |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 30 | #include "sunxi_engine.h" |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 31 | #include "sun4i_tcon.h" |
| 32 | |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 33 | /* |
| 34 | * While this isn't really working in the DRM theory, in practice we |
| 35 | * can only ever have one encoder per TCON since we have a mux in our |
| 36 | * TCON. |
| 37 | */ |
| 38 | static struct drm_encoder *sun4i_crtc_get_encoder(struct drm_crtc *crtc) |
| 39 | { |
| 40 | struct drm_encoder *encoder; |
| 41 | |
| 42 | drm_for_each_encoder(encoder, crtc->dev) |
| 43 | if (encoder->crtc == crtc) |
| 44 | return encoder; |
| 45 | |
| 46 | return NULL; |
| 47 | } |
| 48 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 49 | static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc, |
| 50 | struct drm_crtc_state *old_state) |
| 51 | { |
| 52 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
| 53 | struct drm_device *dev = crtc->dev; |
| 54 | unsigned long flags; |
| 55 | |
| 56 | if (crtc->state->event) { |
| 57 | WARN_ON(drm_crtc_vblank_get(crtc) != 0); |
| 58 | |
| 59 | spin_lock_irqsave(&dev->event_lock, flags); |
| 60 | scrtc->event = crtc->state->event; |
| 61 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 62 | crtc->state->event = NULL; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc, |
| 67 | struct drm_crtc_state *old_state) |
| 68 | { |
| 69 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
Daniel Vetter | a33e93d | 2016-06-08 14:18:58 +0200 | [diff] [blame] | 70 | struct drm_pending_vblank_event *event = crtc->state->event; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 71 | |
| 72 | DRM_DEBUG_DRIVER("Committing plane changes\n"); |
| 73 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 74 | sunxi_engine_commit(scrtc->engine); |
Daniel Vetter | a33e93d | 2016-06-08 14:18:58 +0200 | [diff] [blame] | 75 | |
| 76 | if (event) { |
| 77 | crtc->state->event = NULL; |
| 78 | |
| 79 | spin_lock_irq(&crtc->dev->event_lock); |
| 80 | if (drm_crtc_vblank_get(crtc) == 0) |
| 81 | drm_crtc_arm_vblank_event(crtc, event); |
| 82 | else |
| 83 | drm_crtc_send_vblank_event(crtc, event); |
| 84 | spin_unlock_irq(&crtc->dev->event_lock); |
| 85 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Laurent Pinchart | 6458171 | 2017-06-30 12:36:45 +0300 | [diff] [blame] | 88 | static void sun4i_crtc_atomic_disable(struct drm_crtc *crtc, |
| 89 | struct drm_crtc_state *old_state) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 90 | { |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 91 | struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 92 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 93 | |
| 94 | DRM_DEBUG_DRIVER("Disabling the CRTC\n"); |
| 95 | |
Maxime Ripard | fd00c4e | 2018-02-21 13:57:03 +0100 | [diff] [blame] | 96 | drm_crtc_vblank_off(crtc); |
| 97 | |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 98 | sun4i_tcon_set_status(scrtc->tcon, encoder, false); |
Maxime Ripard | 2cd3683 | 2016-06-20 12:20:59 +0200 | [diff] [blame] | 99 | |
| 100 | if (crtc->state->event && !crtc->state->active) { |
| 101 | spin_lock_irq(&crtc->dev->event_lock); |
| 102 | drm_crtc_send_vblank_event(crtc, crtc->state->event); |
| 103 | spin_unlock_irq(&crtc->dev->event_lock); |
| 104 | |
| 105 | crtc->state->event = NULL; |
| 106 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Laurent Pinchart | 0b20a0f | 2017-06-30 12:36:44 +0300 | [diff] [blame] | 109 | static void sun4i_crtc_atomic_enable(struct drm_crtc *crtc, |
| 110 | struct drm_crtc_state *old_state) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 111 | { |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 112 | struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 113 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 114 | |
| 115 | DRM_DEBUG_DRIVER("Enabling the CRTC\n"); |
| 116 | |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 117 | sun4i_tcon_set_status(scrtc->tcon, encoder, true); |
Maxime Ripard | fd00c4e | 2018-02-21 13:57:03 +0100 | [diff] [blame] | 118 | |
| 119 | drm_crtc_vblank_on(crtc); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Maxime Ripard | 5b8f091 | 2017-10-17 11:06:13 +0200 | [diff] [blame] | 122 | static void sun4i_crtc_mode_set_nofb(struct drm_crtc *crtc) |
| 123 | { |
| 124 | struct drm_display_mode *mode = &crtc->state->adjusted_mode; |
| 125 | struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc); |
| 126 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
| 127 | |
| 128 | sun4i_tcon_mode_set(scrtc->tcon, encoder, mode); |
| 129 | } |
| 130 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 131 | static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = { |
| 132 | .atomic_begin = sun4i_crtc_atomic_begin, |
| 133 | .atomic_flush = sun4i_crtc_atomic_flush, |
Laurent Pinchart | 0b20a0f | 2017-06-30 12:36:44 +0300 | [diff] [blame] | 134 | .atomic_enable = sun4i_crtc_atomic_enable, |
Laurent Pinchart | 6458171 | 2017-06-30 12:36:45 +0300 | [diff] [blame] | 135 | .atomic_disable = sun4i_crtc_atomic_disable, |
Maxime Ripard | 5b8f091 | 2017-10-17 11:06:13 +0200 | [diff] [blame] | 136 | .mode_set_nofb = sun4i_crtc_mode_set_nofb, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
Shawn Guo | 50480a7 | 2017-02-07 17:16:31 +0800 | [diff] [blame] | 139 | static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc) |
| 140 | { |
| 141 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
Shawn Guo | 50480a7 | 2017-02-07 17:16:31 +0800 | [diff] [blame] | 142 | |
| 143 | DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc); |
| 144 | |
Chen-Yu Tsai | 3c64fb3 | 2017-02-23 16:05:43 +0800 | [diff] [blame] | 145 | sun4i_tcon_enable_vblank(scrtc->tcon, true); |
Shawn Guo | 50480a7 | 2017-02-07 17:16:31 +0800 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc) |
| 151 | { |
| 152 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
Shawn Guo | 50480a7 | 2017-02-07 17:16:31 +0800 | [diff] [blame] | 153 | |
| 154 | DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc); |
| 155 | |
Chen-Yu Tsai | 3c64fb3 | 2017-02-23 16:05:43 +0800 | [diff] [blame] | 156 | sun4i_tcon_enable_vblank(scrtc->tcon, false); |
Shawn Guo | 50480a7 | 2017-02-07 17:16:31 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 159 | static const struct drm_crtc_funcs sun4i_crtc_funcs = { |
| 160 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, |
| 161 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, |
| 162 | .destroy = drm_crtc_cleanup, |
| 163 | .page_flip = drm_atomic_helper_page_flip, |
| 164 | .reset = drm_atomic_helper_crtc_reset, |
| 165 | .set_config = drm_atomic_helper_set_config, |
Shawn Guo | 50480a7 | 2017-02-07 17:16:31 +0800 | [diff] [blame] | 166 | .enable_vblank = sun4i_crtc_enable_vblank, |
| 167 | .disable_vblank = sun4i_crtc_disable_vblank, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 168 | }; |
| 169 | |
Chen-Yu Tsai | 18c3b30 | 2017-03-09 18:05:28 +0800 | [diff] [blame] | 170 | struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm, |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 171 | struct sunxi_engine *engine, |
Chen-Yu Tsai | 18c3b30 | 2017-03-09 18:05:28 +0800 | [diff] [blame] | 172 | struct sun4i_tcon *tcon) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 173 | { |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 174 | struct sun4i_crtc *scrtc; |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 175 | struct drm_plane **planes; |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame] | 176 | struct drm_plane *primary = NULL, *cursor = NULL; |
| 177 | int ret, i; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 178 | |
| 179 | scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL); |
| 180 | if (!scrtc) |
Chen-Yu Tsai | ea411fd | 2017-02-17 11:13:30 +0800 | [diff] [blame] | 181 | return ERR_PTR(-ENOMEM); |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 182 | scrtc->engine = engine; |
Chen-Yu Tsai | 18c3b30 | 2017-03-09 18:05:28 +0800 | [diff] [blame] | 183 | scrtc->tcon = tcon; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 184 | |
Chen-Yu Tsai | b3f266e | 2017-02-23 16:05:36 +0800 | [diff] [blame] | 185 | /* Create our layers */ |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 186 | planes = sunxi_engine_layers_init(drm, engine); |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 187 | if (IS_ERR(planes)) { |
Chen-Yu Tsai | b3f266e | 2017-02-23 16:05:36 +0800 | [diff] [blame] | 188 | dev_err(drm->dev, "Couldn't create the planes\n"); |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame] | 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | /* find primary and cursor planes for drm_crtc_init_with_planes */ |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 193 | for (i = 0; planes[i]; i++) { |
| 194 | struct drm_plane *plane = planes[i]; |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame] | 195 | |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 196 | switch (plane->type) { |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame] | 197 | case DRM_PLANE_TYPE_PRIMARY: |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 198 | primary = plane; |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame] | 199 | break; |
| 200 | case DRM_PLANE_TYPE_CURSOR: |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 201 | cursor = plane; |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame] | 202 | break; |
| 203 | default: |
| 204 | break; |
| 205 | } |
Chen-Yu Tsai | b3f266e | 2017-02-23 16:05:36 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 208 | ret = drm_crtc_init_with_planes(drm, &scrtc->crtc, |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame] | 209 | primary, |
| 210 | cursor, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 211 | &sun4i_crtc_funcs, |
| 212 | NULL); |
| 213 | if (ret) { |
| 214 | dev_err(drm->dev, "Couldn't init DRM CRTC\n"); |
Chen-Yu Tsai | ea411fd | 2017-02-17 11:13:30 +0800 | [diff] [blame] | 215 | return ERR_PTR(ret); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs); |
| 219 | |
Chen-Yu Tsai | 7544860 | 2017-02-23 16:05:34 +0800 | [diff] [blame] | 220 | /* Set crtc.port to output port node of the tcon */ |
Chen-Yu Tsai | e4cdcb7 | 2017-03-09 18:05:26 +0800 | [diff] [blame] | 221 | scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node, |
Chen-Yu Tsai | 7544860 | 2017-02-23 16:05:34 +0800 | [diff] [blame] | 222 | 1); |
| 223 | |
Chen-Yu Tsai | a5154a4 | 2017-02-23 16:05:39 +0800 | [diff] [blame] | 224 | /* Set possible_crtcs to this crtc for overlay planes */ |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 225 | for (i = 0; planes[i]; i++) { |
Chen-Yu Tsai | a5154a4 | 2017-02-23 16:05:39 +0800 | [diff] [blame] | 226 | uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc)); |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 227 | struct drm_plane *plane = planes[i]; |
Chen-Yu Tsai | a5154a4 | 2017-02-23 16:05:39 +0800 | [diff] [blame] | 228 | |
Icenowy Zheng | 7921e14 | 2017-05-15 00:30:36 +0800 | [diff] [blame] | 229 | if (plane->type == DRM_PLANE_TYPE_OVERLAY) |
| 230 | plane->possible_crtcs = possible_crtcs; |
Chen-Yu Tsai | a5154a4 | 2017-02-23 16:05:39 +0800 | [diff] [blame] | 231 | } |
| 232 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 233 | return scrtc; |
| 234 | } |