Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Traphandler |
| 3 | * Copyright (C) 2014 Free Electrons |
| 4 | * |
| 5 | * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com> |
| 6 | * Author: Boris BREZILLON <boris.brezillon@free-electrons.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published by |
| 10 | * the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | * more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/pm.h> |
| 23 | #include <linux/pm_runtime.h> |
Sylvain Rochet | 16e6004 | 2015-02-22 18:51:04 +0100 | [diff] [blame] | 24 | #include <linux/pinctrl/consumer.h> |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 25 | |
| 26 | #include <drm/drm_crtc.h> |
| 27 | #include <drm/drm_crtc_helper.h> |
| 28 | #include <drm/drmP.h> |
| 29 | |
| 30 | #include <video/videomode.h> |
| 31 | |
| 32 | #include "atmel_hlcdc_dc.h" |
| 33 | |
| 34 | /** |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 35 | * Atmel HLCDC CRTC state structure |
| 36 | * |
| 37 | * @base: base CRTC state |
| 38 | * @output_mode: RGBXXX output mode |
| 39 | */ |
| 40 | struct atmel_hlcdc_crtc_state { |
| 41 | struct drm_crtc_state base; |
| 42 | unsigned int output_mode; |
| 43 | }; |
| 44 | |
| 45 | static inline struct atmel_hlcdc_crtc_state * |
| 46 | drm_crtc_state_to_atmel_hlcdc_crtc_state(struct drm_crtc_state *state) |
| 47 | { |
| 48 | return container_of(state, struct atmel_hlcdc_crtc_state, base); |
| 49 | } |
| 50 | |
| 51 | /** |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 52 | * Atmel HLCDC CRTC structure |
| 53 | * |
| 54 | * @base: base DRM CRTC structure |
| 55 | * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device |
| 56 | * @event: pointer to the current page flip event |
| 57 | * @id: CRTC id (returned by drm_crtc_index) |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 58 | * @enabled: CRTC state |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 59 | */ |
| 60 | struct atmel_hlcdc_crtc { |
| 61 | struct drm_crtc base; |
| 62 | struct atmel_hlcdc_dc *dc; |
| 63 | struct drm_pending_vblank_event *event; |
| 64 | int id; |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 65 | bool enabled; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static inline struct atmel_hlcdc_crtc * |
| 69 | drm_crtc_to_atmel_hlcdc_crtc(struct drm_crtc *crtc) |
| 70 | { |
| 71 | return container_of(crtc, struct atmel_hlcdc_crtc, base); |
| 72 | } |
| 73 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 74 | static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 75 | { |
| 76 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 77 | struct regmap *regmap = crtc->dc->hlcdc->regmap; |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 78 | struct drm_display_mode *adj = &c->state->adjusted_mode; |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 79 | struct atmel_hlcdc_crtc_state *state; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 80 | unsigned long mode_rate; |
| 81 | struct videomode vm; |
| 82 | unsigned long prate; |
| 83 | unsigned int cfg; |
| 84 | int div; |
| 85 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 86 | vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay; |
| 87 | vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end; |
| 88 | vm.vsync_len = adj->crtc_vsync_end - adj->crtc_vsync_start; |
| 89 | vm.hfront_porch = adj->crtc_hsync_start - adj->crtc_hdisplay; |
| 90 | vm.hback_porch = adj->crtc_htotal - adj->crtc_hsync_end; |
| 91 | vm.hsync_len = adj->crtc_hsync_end - adj->crtc_hsync_start; |
| 92 | |
| 93 | regmap_write(regmap, ATMEL_HLCDC_CFG(1), |
| 94 | (vm.hsync_len - 1) | ((vm.vsync_len - 1) << 16)); |
| 95 | |
| 96 | regmap_write(regmap, ATMEL_HLCDC_CFG(2), |
| 97 | (vm.vfront_porch - 1) | (vm.vback_porch << 16)); |
| 98 | |
| 99 | regmap_write(regmap, ATMEL_HLCDC_CFG(3), |
| 100 | (vm.hfront_porch - 1) | ((vm.hback_porch - 1) << 16)); |
| 101 | |
| 102 | regmap_write(regmap, ATMEL_HLCDC_CFG(4), |
| 103 | (adj->crtc_hdisplay - 1) | |
| 104 | ((adj->crtc_vdisplay - 1) << 16)); |
| 105 | |
Nicolas Ferre | 0bb59cb | 2015-02-25 18:44:51 +0100 | [diff] [blame] | 106 | cfg = 0; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 107 | |
| 108 | prate = clk_get_rate(crtc->dc->hlcdc->sys_clk); |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 109 | mode_rate = adj->crtc_clock * 1000; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 110 | if ((prate / 2) < mode_rate) { |
| 111 | prate *= 2; |
| 112 | cfg |= ATMEL_HLCDC_CLKSEL; |
| 113 | } |
| 114 | |
| 115 | div = DIV_ROUND_UP(prate, mode_rate); |
| 116 | if (div < 2) |
| 117 | div = 2; |
| 118 | |
| 119 | cfg |= ATMEL_HLCDC_CLKDIV(div); |
| 120 | |
| 121 | regmap_update_bits(regmap, ATMEL_HLCDC_CFG(0), |
| 122 | ATMEL_HLCDC_CLKSEL | ATMEL_HLCDC_CLKDIV_MASK | |
| 123 | ATMEL_HLCDC_CLKPOL, cfg); |
| 124 | |
| 125 | cfg = 0; |
| 126 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 127 | if (adj->flags & DRM_MODE_FLAG_NVSYNC) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 128 | cfg |= ATMEL_HLCDC_VSPOL; |
| 129 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 130 | if (adj->flags & DRM_MODE_FLAG_NHSYNC) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 131 | cfg |= ATMEL_HLCDC_HSPOL; |
| 132 | |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 133 | state = drm_crtc_state_to_atmel_hlcdc_crtc_state(c->state); |
| 134 | cfg |= state->output_mode << 8; |
| 135 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 136 | regmap_update_bits(regmap, ATMEL_HLCDC_CFG(5), |
| 137 | ATMEL_HLCDC_HSPOL | ATMEL_HLCDC_VSPOL | |
| 138 | ATMEL_HLCDC_VSPDLYS | ATMEL_HLCDC_VSPDLYE | |
| 139 | ATMEL_HLCDC_DISPPOL | ATMEL_HLCDC_DISPDLY | |
| 140 | ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO | |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 141 | ATMEL_HLCDC_GUARDTIME_MASK | ATMEL_HLCDC_MODE_MASK, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 142 | cfg); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 145 | static void atmel_hlcdc_crtc_disable(struct drm_crtc *c) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 146 | { |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 147 | struct drm_device *dev = c->dev; |
| 148 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 149 | struct regmap *regmap = crtc->dc->hlcdc->regmap; |
| 150 | unsigned int status; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 151 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 152 | if (!crtc->enabled) |
| 153 | return; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 154 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 155 | drm_crtc_vblank_off(c); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 156 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 157 | pm_runtime_get_sync(dev->dev); |
| 158 | |
| 159 | regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_DISP); |
| 160 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 161 | (status & ATMEL_HLCDC_DISP)) |
| 162 | cpu_relax(); |
| 163 | |
| 164 | regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_SYNC); |
| 165 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 166 | (status & ATMEL_HLCDC_SYNC)) |
| 167 | cpu_relax(); |
| 168 | |
| 169 | regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PIXEL_CLK); |
| 170 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 171 | (status & ATMEL_HLCDC_PIXEL_CLK)) |
| 172 | cpu_relax(); |
| 173 | |
| 174 | clk_disable_unprepare(crtc->dc->hlcdc->sys_clk); |
Sylvain Rochet | 16e6004 | 2015-02-22 18:51:04 +0100 | [diff] [blame] | 175 | pinctrl_pm_select_sleep_state(dev->dev); |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 176 | |
| 177 | pm_runtime_allow(dev->dev); |
| 178 | |
| 179 | pm_runtime_put_sync(dev->dev); |
| 180 | |
| 181 | crtc->enabled = false; |
| 182 | } |
| 183 | |
| 184 | static void atmel_hlcdc_crtc_enable(struct drm_crtc *c) |
| 185 | { |
| 186 | struct drm_device *dev = c->dev; |
| 187 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 188 | struct regmap *regmap = crtc->dc->hlcdc->regmap; |
| 189 | unsigned int status; |
| 190 | |
| 191 | if (crtc->enabled) |
| 192 | return; |
| 193 | |
| 194 | pm_runtime_get_sync(dev->dev); |
| 195 | |
| 196 | pm_runtime_forbid(dev->dev); |
| 197 | |
Sylvain Rochet | 16e6004 | 2015-02-22 18:51:04 +0100 | [diff] [blame] | 198 | pinctrl_pm_select_default_state(dev->dev); |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 199 | clk_prepare_enable(crtc->dc->hlcdc->sys_clk); |
| 200 | |
| 201 | regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PIXEL_CLK); |
| 202 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 203 | !(status & ATMEL_HLCDC_PIXEL_CLK)) |
| 204 | cpu_relax(); |
| 205 | |
| 206 | |
| 207 | regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_SYNC); |
| 208 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 209 | !(status & ATMEL_HLCDC_SYNC)) |
| 210 | cpu_relax(); |
| 211 | |
| 212 | regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_DISP); |
| 213 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 214 | !(status & ATMEL_HLCDC_DISP)) |
| 215 | cpu_relax(); |
| 216 | |
| 217 | pm_runtime_put_sync(dev->dev); |
| 218 | |
| 219 | drm_crtc_vblank_on(c); |
| 220 | |
| 221 | crtc->enabled = true; |
| 222 | } |
| 223 | |
Sylvain Rochet | f026eb6 | 2015-03-12 19:47:19 +0100 | [diff] [blame] | 224 | void atmel_hlcdc_crtc_suspend(struct drm_crtc *c) |
| 225 | { |
| 226 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 227 | |
| 228 | if (crtc->enabled) { |
| 229 | atmel_hlcdc_crtc_disable(c); |
| 230 | /* save enable state for resume */ |
| 231 | crtc->enabled = true; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void atmel_hlcdc_crtc_resume(struct drm_crtc *c) |
| 236 | { |
| 237 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 238 | |
| 239 | if (crtc->enabled) { |
| 240 | crtc->enabled = false; |
| 241 | atmel_hlcdc_crtc_enable(c); |
| 242 | } |
| 243 | } |
| 244 | |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 245 | #define ATMEL_HLCDC_RGB444_OUTPUT BIT(0) |
| 246 | #define ATMEL_HLCDC_RGB565_OUTPUT BIT(1) |
| 247 | #define ATMEL_HLCDC_RGB666_OUTPUT BIT(2) |
| 248 | #define ATMEL_HLCDC_RGB888_OUTPUT BIT(3) |
| 249 | #define ATMEL_HLCDC_OUTPUT_MODE_MASK GENMASK(3, 0) |
| 250 | |
| 251 | static int atmel_hlcdc_crtc_select_output_mode(struct drm_crtc_state *state) |
| 252 | { |
| 253 | unsigned int output_fmts = ATMEL_HLCDC_OUTPUT_MODE_MASK; |
| 254 | struct atmel_hlcdc_crtc_state *hstate; |
| 255 | struct drm_connector_state *cstate; |
| 256 | struct drm_connector *connector; |
| 257 | struct atmel_hlcdc_crtc *crtc; |
| 258 | int i; |
| 259 | |
| 260 | crtc = drm_crtc_to_atmel_hlcdc_crtc(state->crtc); |
| 261 | |
| 262 | for_each_connector_in_state(state->state, connector, cstate, i) { |
| 263 | struct drm_display_info *info = &connector->display_info; |
| 264 | unsigned int supported_fmts = 0; |
| 265 | int j; |
| 266 | |
| 267 | if (!cstate->crtc) |
| 268 | continue; |
| 269 | |
| 270 | for (j = 0; j < info->num_bus_formats; j++) { |
| 271 | switch (info->bus_formats[j]) { |
| 272 | case MEDIA_BUS_FMT_RGB444_1X12: |
| 273 | supported_fmts |= ATMEL_HLCDC_RGB444_OUTPUT; |
| 274 | break; |
| 275 | case MEDIA_BUS_FMT_RGB565_1X16: |
| 276 | supported_fmts |= ATMEL_HLCDC_RGB565_OUTPUT; |
| 277 | break; |
| 278 | case MEDIA_BUS_FMT_RGB666_1X18: |
| 279 | supported_fmts |= ATMEL_HLCDC_RGB666_OUTPUT; |
| 280 | break; |
| 281 | case MEDIA_BUS_FMT_RGB888_1X24: |
| 282 | supported_fmts |= ATMEL_HLCDC_RGB888_OUTPUT; |
| 283 | break; |
| 284 | default: |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (crtc->dc->desc->conflicting_output_formats) |
| 290 | output_fmts &= supported_fmts; |
| 291 | else |
| 292 | output_fmts |= supported_fmts; |
| 293 | } |
| 294 | |
| 295 | if (!output_fmts) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | hstate = drm_crtc_state_to_atmel_hlcdc_crtc_state(state); |
| 299 | hstate->output_mode = fls(output_fmts) - 1; |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 304 | static int atmel_hlcdc_crtc_atomic_check(struct drm_crtc *c, |
| 305 | struct drm_crtc_state *s) |
| 306 | { |
| 307 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 308 | int ret; |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 309 | |
| 310 | if (atmel_hlcdc_dc_mode_valid(crtc->dc, &s->adjusted_mode) != MODE_OK) |
| 311 | return -EINVAL; |
| 312 | |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 313 | ret = atmel_hlcdc_crtc_select_output_mode(s); |
| 314 | if (ret) |
| 315 | return ret; |
| 316 | |
Boris Brezillon | 5957017 | 2015-02-06 16:25:06 +0100 | [diff] [blame] | 317 | return atmel_hlcdc_plane_prepare_disc_area(s); |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Maarten Lankhorst | 613d2b2 | 2015-07-21 13:28:58 +0200 | [diff] [blame] | 320 | static void atmel_hlcdc_crtc_atomic_begin(struct drm_crtc *c, |
| 321 | struct drm_crtc_state *old_s) |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 322 | { |
| 323 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 324 | |
| 325 | if (c->state->event) { |
| 326 | c->state->event->pipe = drm_crtc_index(c); |
| 327 | |
| 328 | WARN_ON(drm_crtc_vblank_get(c) != 0); |
| 329 | |
| 330 | crtc->event = c->state->event; |
| 331 | c->state->event = NULL; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Maarten Lankhorst | 613d2b2 | 2015-07-21 13:28:58 +0200 | [diff] [blame] | 335 | static void atmel_hlcdc_crtc_atomic_flush(struct drm_crtc *crtc, |
| 336 | struct drm_crtc_state *old_s) |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 337 | { |
| 338 | /* TODO: write common plane control register if available */ |
| 339 | } |
| 340 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 341 | static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs = { |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 342 | .mode_set = drm_helper_crtc_mode_set, |
| 343 | .mode_set_nofb = atmel_hlcdc_crtc_mode_set_nofb, |
| 344 | .mode_set_base = drm_helper_crtc_mode_set_base, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 345 | .disable = atmel_hlcdc_crtc_disable, |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 346 | .enable = atmel_hlcdc_crtc_enable, |
| 347 | .atomic_check = atmel_hlcdc_crtc_atomic_check, |
| 348 | .atomic_begin = atmel_hlcdc_crtc_atomic_begin, |
| 349 | .atomic_flush = atmel_hlcdc_crtc_atomic_flush, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | static void atmel_hlcdc_crtc_destroy(struct drm_crtc *c) |
| 353 | { |
| 354 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 355 | |
| 356 | drm_crtc_cleanup(c); |
| 357 | kfree(crtc); |
| 358 | } |
| 359 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 360 | static void atmel_hlcdc_crtc_finish_page_flip(struct atmel_hlcdc_crtc *crtc) |
| 361 | { |
| 362 | struct drm_device *dev = crtc->base.dev; |
| 363 | unsigned long flags; |
| 364 | |
| 365 | spin_lock_irqsave(&dev->event_lock, flags); |
| 366 | if (crtc->event) { |
| 367 | drm_send_vblank_event(dev, crtc->id, crtc->event); |
| 368 | drm_vblank_put(dev, crtc->id); |
| 369 | crtc->event = NULL; |
| 370 | } |
| 371 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 372 | } |
| 373 | |
| 374 | void atmel_hlcdc_crtc_irq(struct drm_crtc *c) |
| 375 | { |
| 376 | drm_handle_vblank(c->dev, 0); |
| 377 | atmel_hlcdc_crtc_finish_page_flip(drm_crtc_to_atmel_hlcdc_crtc(c)); |
| 378 | } |
| 379 | |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 380 | void atmel_hlcdc_crtc_reset(struct drm_crtc *crtc) |
| 381 | { |
| 382 | struct atmel_hlcdc_crtc_state *state; |
| 383 | |
| 384 | if (crtc->state && crtc->state->mode_blob) |
| 385 | drm_property_unreference_blob(crtc->state->mode_blob); |
| 386 | |
| 387 | if (crtc->state) { |
| 388 | state = drm_crtc_state_to_atmel_hlcdc_crtc_state(crtc->state); |
| 389 | kfree(state); |
| 390 | } |
| 391 | |
| 392 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 393 | if (state) { |
| 394 | crtc->state = &state->base; |
| 395 | crtc->state->crtc = crtc; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static struct drm_crtc_state * |
| 400 | atmel_hlcdc_crtc_duplicate_state(struct drm_crtc *crtc) |
| 401 | { |
| 402 | struct atmel_hlcdc_crtc_state *state, *cur; |
| 403 | |
| 404 | if (WARN_ON(!crtc->state)) |
| 405 | return NULL; |
| 406 | |
| 407 | state = kmalloc(sizeof(*state), GFP_KERNEL); |
| 408 | if (state) |
| 409 | __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); |
| 410 | |
| 411 | cur = drm_crtc_state_to_atmel_hlcdc_crtc_state(crtc->state); |
| 412 | state->output_mode = cur->output_mode; |
| 413 | |
| 414 | return &state->base; |
| 415 | } |
| 416 | |
| 417 | static void atmel_hlcdc_crtc_destroy_state(struct drm_crtc *crtc, |
| 418 | struct drm_crtc_state *s) |
| 419 | { |
| 420 | struct atmel_hlcdc_crtc_state *state; |
| 421 | |
| 422 | state = drm_crtc_state_to_atmel_hlcdc_crtc_state(s); |
| 423 | __drm_atomic_helper_crtc_destroy_state(crtc, s); |
| 424 | kfree(state); |
| 425 | } |
| 426 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 427 | static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs = { |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 428 | .page_flip = drm_atomic_helper_page_flip, |
| 429 | .set_config = drm_atomic_helper_set_config, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 430 | .destroy = atmel_hlcdc_crtc_destroy, |
Boris Brezillon | aca63b7 | 2016-01-06 11:14:15 +0100 | [diff] [blame^] | 431 | .reset = atmel_hlcdc_crtc_reset, |
| 432 | .atomic_duplicate_state = atmel_hlcdc_crtc_duplicate_state, |
| 433 | .atomic_destroy_state = atmel_hlcdc_crtc_destroy_state, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | int atmel_hlcdc_crtc_create(struct drm_device *dev) |
| 437 | { |
| 438 | struct atmel_hlcdc_dc *dc = dev->dev_private; |
| 439 | struct atmel_hlcdc_planes *planes = dc->planes; |
| 440 | struct atmel_hlcdc_crtc *crtc; |
| 441 | int ret; |
| 442 | int i; |
| 443 | |
| 444 | crtc = kzalloc(sizeof(*crtc), GFP_KERNEL); |
| 445 | if (!crtc) |
| 446 | return -ENOMEM; |
| 447 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 448 | crtc->dc = dc; |
| 449 | |
| 450 | ret = drm_crtc_init_with_planes(dev, &crtc->base, |
| 451 | &planes->primary->base, |
| 452 | planes->cursor ? &planes->cursor->base : NULL, |
Ville Syrjälä | f988287 | 2015-12-09 16:19:31 +0200 | [diff] [blame] | 453 | &atmel_hlcdc_crtc_funcs, NULL); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 454 | if (ret < 0) |
| 455 | goto fail; |
| 456 | |
| 457 | crtc->id = drm_crtc_index(&crtc->base); |
| 458 | |
| 459 | if (planes->cursor) |
| 460 | planes->cursor->base.possible_crtcs = 1 << crtc->id; |
| 461 | |
| 462 | for (i = 0; i < planes->noverlays; i++) |
| 463 | planes->overlays[i]->base.possible_crtcs = 1 << crtc->id; |
| 464 | |
| 465 | drm_crtc_helper_add(&crtc->base, &lcdc_crtc_helper_funcs); |
Boris Brezillon | 8c4b4b0 | 2015-07-16 20:55:34 +0200 | [diff] [blame] | 466 | drm_crtc_vblank_reset(&crtc->base); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 467 | |
| 468 | dc->crtc = &crtc->base; |
| 469 | |
| 470 | return 0; |
| 471 | |
| 472 | fail: |
| 473 | atmel_hlcdc_crtc_destroy(&crtc->base); |
| 474 | return ret; |
| 475 | } |