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 | |
| 28 | #include "sun4i_backend.h" |
| 29 | #include "sun4i_crtc.h" |
| 30 | #include "sun4i_drv.h" |
Chen-Yu Tsai | b3f266e | 2017-02-23 16:05:36 +0800 | [diff] [blame] | 31 | #include "sun4i_layer.h" |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 32 | #include "sun4i_tcon.h" |
| 33 | |
| 34 | static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc, |
| 35 | struct drm_crtc_state *old_state) |
| 36 | { |
| 37 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
| 38 | struct drm_device *dev = crtc->dev; |
| 39 | unsigned long flags; |
| 40 | |
| 41 | if (crtc->state->event) { |
| 42 | WARN_ON(drm_crtc_vblank_get(crtc) != 0); |
| 43 | |
| 44 | spin_lock_irqsave(&dev->event_lock, flags); |
| 45 | scrtc->event = crtc->state->event; |
| 46 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 47 | crtc->state->event = NULL; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc, |
| 52 | struct drm_crtc_state *old_state) |
| 53 | { |
| 54 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
| 55 | struct sun4i_drv *drv = scrtc->drv; |
Daniel Vetter | a33e93d | 2016-06-08 14:18:58 +0200 | [diff] [blame] | 56 | struct drm_pending_vblank_event *event = crtc->state->event; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 57 | |
| 58 | DRM_DEBUG_DRIVER("Committing plane changes\n"); |
| 59 | |
| 60 | sun4i_backend_commit(drv->backend); |
Daniel Vetter | a33e93d | 2016-06-08 14:18:58 +0200 | [diff] [blame] | 61 | |
| 62 | if (event) { |
| 63 | crtc->state->event = NULL; |
| 64 | |
| 65 | spin_lock_irq(&crtc->dev->event_lock); |
| 66 | if (drm_crtc_vblank_get(crtc) == 0) |
| 67 | drm_crtc_arm_vblank_event(crtc, event); |
| 68 | else |
| 69 | drm_crtc_send_vblank_event(crtc, event); |
| 70 | spin_unlock_irq(&crtc->dev->event_lock); |
| 71 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void sun4i_crtc_disable(struct drm_crtc *crtc) |
| 75 | { |
| 76 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
| 77 | struct sun4i_drv *drv = scrtc->drv; |
| 78 | |
| 79 | DRM_DEBUG_DRIVER("Disabling the CRTC\n"); |
| 80 | |
| 81 | sun4i_tcon_disable(drv->tcon); |
Maxime Ripard | 2cd3683 | 2016-06-20 12:20:59 +0200 | [diff] [blame] | 82 | |
| 83 | if (crtc->state->event && !crtc->state->active) { |
| 84 | spin_lock_irq(&crtc->dev->event_lock); |
| 85 | drm_crtc_send_vblank_event(crtc, crtc->state->event); |
| 86 | spin_unlock_irq(&crtc->dev->event_lock); |
| 87 | |
| 88 | crtc->state->event = NULL; |
| 89 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void sun4i_crtc_enable(struct drm_crtc *crtc) |
| 93 | { |
| 94 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
| 95 | struct sun4i_drv *drv = scrtc->drv; |
| 96 | |
| 97 | DRM_DEBUG_DRIVER("Enabling the CRTC\n"); |
| 98 | |
| 99 | sun4i_tcon_enable(drv->tcon); |
| 100 | } |
| 101 | |
| 102 | static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = { |
| 103 | .atomic_begin = sun4i_crtc_atomic_begin, |
| 104 | .atomic_flush = sun4i_crtc_atomic_flush, |
| 105 | .disable = sun4i_crtc_disable, |
| 106 | .enable = sun4i_crtc_enable, |
| 107 | }; |
| 108 | |
Shawn Guo | 50480a7 | 2017-02-07 17:16:31 +0800 | [diff] [blame] | 109 | static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc) |
| 110 | { |
| 111 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
| 112 | struct sun4i_drv *drv = scrtc->drv; |
| 113 | |
| 114 | DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc); |
| 115 | |
| 116 | sun4i_tcon_enable_vblank(drv->tcon, true); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc) |
| 122 | { |
| 123 | struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc); |
| 124 | struct sun4i_drv *drv = scrtc->drv; |
| 125 | |
| 126 | DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc); |
| 127 | |
| 128 | sun4i_tcon_enable_vblank(drv->tcon, false); |
| 129 | } |
| 130 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 131 | static const struct drm_crtc_funcs sun4i_crtc_funcs = { |
| 132 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, |
| 133 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, |
| 134 | .destroy = drm_crtc_cleanup, |
| 135 | .page_flip = drm_atomic_helper_page_flip, |
| 136 | .reset = drm_atomic_helper_crtc_reset, |
| 137 | .set_config = drm_atomic_helper_set_config, |
Shawn Guo | 50480a7 | 2017-02-07 17:16:31 +0800 | [diff] [blame] | 138 | .enable_vblank = sun4i_crtc_enable_vblank, |
| 139 | .disable_vblank = sun4i_crtc_disable_vblank, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm) |
| 143 | { |
| 144 | struct sun4i_drv *drv = drm->dev_private; |
| 145 | struct sun4i_crtc *scrtc; |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame^] | 146 | struct drm_plane *primary = NULL, *cursor = NULL; |
| 147 | int ret, i; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 148 | |
| 149 | scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL); |
| 150 | if (!scrtc) |
Chen-Yu Tsai | ea411fd | 2017-02-17 11:13:30 +0800 | [diff] [blame] | 151 | return ERR_PTR(-ENOMEM); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 152 | scrtc->drv = drv; |
| 153 | |
Chen-Yu Tsai | b3f266e | 2017-02-23 16:05:36 +0800 | [diff] [blame] | 154 | /* Create our layers */ |
| 155 | scrtc->layers = sun4i_layers_init(drm); |
| 156 | if (IS_ERR(scrtc->layers)) { |
| 157 | dev_err(drm->dev, "Couldn't create the planes\n"); |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame^] | 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | /* find primary and cursor planes for drm_crtc_init_with_planes */ |
| 162 | for (i = 0; scrtc->layers[i]; i++) { |
| 163 | struct sun4i_layer *layer = scrtc->layers[i]; |
| 164 | |
| 165 | switch (layer->plane.type) { |
| 166 | case DRM_PLANE_TYPE_PRIMARY: |
| 167 | primary = &layer->plane; |
| 168 | break; |
| 169 | case DRM_PLANE_TYPE_CURSOR: |
| 170 | cursor = &layer->plane; |
| 171 | break; |
| 172 | default: |
| 173 | break; |
| 174 | } |
Chen-Yu Tsai | b3f266e | 2017-02-23 16:05:36 +0800 | [diff] [blame] | 175 | } |
| 176 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 177 | ret = drm_crtc_init_with_planes(drm, &scrtc->crtc, |
Chen-Yu Tsai | dcd2158 | 2017-02-23 16:05:38 +0800 | [diff] [blame^] | 178 | primary, |
| 179 | cursor, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 180 | &sun4i_crtc_funcs, |
| 181 | NULL); |
| 182 | if (ret) { |
| 183 | dev_err(drm->dev, "Couldn't init DRM CRTC\n"); |
Chen-Yu Tsai | ea411fd | 2017-02-17 11:13:30 +0800 | [diff] [blame] | 184 | return ERR_PTR(ret); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs); |
| 188 | |
Chen-Yu Tsai | 7544860 | 2017-02-23 16:05:34 +0800 | [diff] [blame] | 189 | /* Set crtc.port to output port node of the tcon */ |
| 190 | scrtc->crtc.port = of_graph_get_port_by_id(drv->tcon->dev->of_node, |
| 191 | 1); |
| 192 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 193 | return scrtc; |
| 194 | } |