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> |
| 24 | |
| 25 | #include <drm/drm_crtc.h> |
| 26 | #include <drm/drm_crtc_helper.h> |
| 27 | #include <drm/drmP.h> |
| 28 | |
| 29 | #include <video/videomode.h> |
| 30 | |
| 31 | #include "atmel_hlcdc_dc.h" |
| 32 | |
| 33 | /** |
| 34 | * Atmel HLCDC CRTC structure |
| 35 | * |
| 36 | * @base: base DRM CRTC structure |
| 37 | * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device |
| 38 | * @event: pointer to the current page flip event |
| 39 | * @id: CRTC id (returned by drm_crtc_index) |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 40 | * @enabled: CRTC state |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 41 | */ |
| 42 | struct atmel_hlcdc_crtc { |
| 43 | struct drm_crtc base; |
| 44 | struct atmel_hlcdc_dc *dc; |
| 45 | struct drm_pending_vblank_event *event; |
| 46 | int id; |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 47 | bool enabled; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static inline struct atmel_hlcdc_crtc * |
| 51 | drm_crtc_to_atmel_hlcdc_crtc(struct drm_crtc *crtc) |
| 52 | { |
| 53 | return container_of(crtc, struct atmel_hlcdc_crtc, base); |
| 54 | } |
| 55 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 56 | static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 57 | { |
| 58 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 59 | struct regmap *regmap = crtc->dc->hlcdc->regmap; |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 60 | struct drm_display_mode *adj = &c->state->adjusted_mode; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 61 | unsigned long mode_rate; |
| 62 | struct videomode vm; |
| 63 | unsigned long prate; |
| 64 | unsigned int cfg; |
| 65 | int div; |
| 66 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 67 | vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay; |
| 68 | vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end; |
| 69 | vm.vsync_len = adj->crtc_vsync_end - adj->crtc_vsync_start; |
| 70 | vm.hfront_porch = adj->crtc_hsync_start - adj->crtc_hdisplay; |
| 71 | vm.hback_porch = adj->crtc_htotal - adj->crtc_hsync_end; |
| 72 | vm.hsync_len = adj->crtc_hsync_end - adj->crtc_hsync_start; |
| 73 | |
| 74 | regmap_write(regmap, ATMEL_HLCDC_CFG(1), |
| 75 | (vm.hsync_len - 1) | ((vm.vsync_len - 1) << 16)); |
| 76 | |
| 77 | regmap_write(regmap, ATMEL_HLCDC_CFG(2), |
| 78 | (vm.vfront_porch - 1) | (vm.vback_porch << 16)); |
| 79 | |
| 80 | regmap_write(regmap, ATMEL_HLCDC_CFG(3), |
| 81 | (vm.hfront_porch - 1) | ((vm.hback_porch - 1) << 16)); |
| 82 | |
| 83 | regmap_write(regmap, ATMEL_HLCDC_CFG(4), |
| 84 | (adj->crtc_hdisplay - 1) | |
| 85 | ((adj->crtc_vdisplay - 1) << 16)); |
| 86 | |
| 87 | cfg = ATMEL_HLCDC_CLKPOL; |
| 88 | |
| 89 | prate = clk_get_rate(crtc->dc->hlcdc->sys_clk); |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 90 | mode_rate = adj->crtc_clock * 1000; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 91 | if ((prate / 2) < mode_rate) { |
| 92 | prate *= 2; |
| 93 | cfg |= ATMEL_HLCDC_CLKSEL; |
| 94 | } |
| 95 | |
| 96 | div = DIV_ROUND_UP(prate, mode_rate); |
| 97 | if (div < 2) |
| 98 | div = 2; |
| 99 | |
| 100 | cfg |= ATMEL_HLCDC_CLKDIV(div); |
| 101 | |
| 102 | regmap_update_bits(regmap, ATMEL_HLCDC_CFG(0), |
| 103 | ATMEL_HLCDC_CLKSEL | ATMEL_HLCDC_CLKDIV_MASK | |
| 104 | ATMEL_HLCDC_CLKPOL, cfg); |
| 105 | |
| 106 | cfg = 0; |
| 107 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 108 | if (adj->flags & DRM_MODE_FLAG_NVSYNC) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 109 | cfg |= ATMEL_HLCDC_VSPOL; |
| 110 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 111 | if (adj->flags & DRM_MODE_FLAG_NHSYNC) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 112 | cfg |= ATMEL_HLCDC_HSPOL; |
| 113 | |
| 114 | regmap_update_bits(regmap, ATMEL_HLCDC_CFG(5), |
| 115 | ATMEL_HLCDC_HSPOL | ATMEL_HLCDC_VSPOL | |
| 116 | ATMEL_HLCDC_VSPDLYS | ATMEL_HLCDC_VSPDLYE | |
| 117 | ATMEL_HLCDC_DISPPOL | ATMEL_HLCDC_DISPDLY | |
| 118 | ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO | |
| 119 | ATMEL_HLCDC_GUARDTIME_MASK, |
| 120 | cfg); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static bool atmel_hlcdc_crtc_mode_fixup(struct drm_crtc *crtc, |
| 124 | const struct drm_display_mode *mode, |
| 125 | struct drm_display_mode *adjusted_mode) |
| 126 | { |
| 127 | return true; |
| 128 | } |
| 129 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 130 | static void atmel_hlcdc_crtc_disable(struct drm_crtc *c) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 131 | { |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 132 | struct drm_device *dev = c->dev; |
| 133 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 134 | struct regmap *regmap = crtc->dc->hlcdc->regmap; |
| 135 | unsigned int status; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 136 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 137 | if (!crtc->enabled) |
| 138 | return; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 139 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 140 | drm_crtc_vblank_off(c); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 141 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 142 | pm_runtime_get_sync(dev->dev); |
| 143 | |
| 144 | regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_DISP); |
| 145 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 146 | (status & ATMEL_HLCDC_DISP)) |
| 147 | cpu_relax(); |
| 148 | |
| 149 | regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_SYNC); |
| 150 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 151 | (status & ATMEL_HLCDC_SYNC)) |
| 152 | cpu_relax(); |
| 153 | |
| 154 | regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PIXEL_CLK); |
| 155 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 156 | (status & ATMEL_HLCDC_PIXEL_CLK)) |
| 157 | cpu_relax(); |
| 158 | |
| 159 | clk_disable_unprepare(crtc->dc->hlcdc->sys_clk); |
| 160 | |
| 161 | pm_runtime_allow(dev->dev); |
| 162 | |
| 163 | pm_runtime_put_sync(dev->dev); |
| 164 | |
| 165 | crtc->enabled = false; |
| 166 | } |
| 167 | |
| 168 | static void atmel_hlcdc_crtc_enable(struct drm_crtc *c) |
| 169 | { |
| 170 | struct drm_device *dev = c->dev; |
| 171 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 172 | struct regmap *regmap = crtc->dc->hlcdc->regmap; |
| 173 | unsigned int status; |
| 174 | |
| 175 | if (crtc->enabled) |
| 176 | return; |
| 177 | |
| 178 | pm_runtime_get_sync(dev->dev); |
| 179 | |
| 180 | pm_runtime_forbid(dev->dev); |
| 181 | |
| 182 | clk_prepare_enable(crtc->dc->hlcdc->sys_clk); |
| 183 | |
| 184 | regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PIXEL_CLK); |
| 185 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 186 | !(status & ATMEL_HLCDC_PIXEL_CLK)) |
| 187 | cpu_relax(); |
| 188 | |
| 189 | |
| 190 | regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_SYNC); |
| 191 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 192 | !(status & ATMEL_HLCDC_SYNC)) |
| 193 | cpu_relax(); |
| 194 | |
| 195 | regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_DISP); |
| 196 | while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && |
| 197 | !(status & ATMEL_HLCDC_DISP)) |
| 198 | cpu_relax(); |
| 199 | |
| 200 | pm_runtime_put_sync(dev->dev); |
| 201 | |
| 202 | drm_crtc_vblank_on(c); |
| 203 | |
| 204 | crtc->enabled = true; |
| 205 | } |
| 206 | |
| 207 | static int atmel_hlcdc_crtc_atomic_check(struct drm_crtc *c, |
| 208 | struct drm_crtc_state *s) |
| 209 | { |
| 210 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 211 | |
| 212 | if (atmel_hlcdc_dc_mode_valid(crtc->dc, &s->adjusted_mode) != MODE_OK) |
| 213 | return -EINVAL; |
| 214 | |
Boris Brezillon | 5957017 | 2015-02-06 16:25:06 +0100 | [diff] [blame] | 215 | return atmel_hlcdc_plane_prepare_disc_area(s); |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static void atmel_hlcdc_crtc_atomic_begin(struct drm_crtc *c) |
| 219 | { |
| 220 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 221 | |
| 222 | if (c->state->event) { |
| 223 | c->state->event->pipe = drm_crtc_index(c); |
| 224 | |
| 225 | WARN_ON(drm_crtc_vblank_get(c) != 0); |
| 226 | |
| 227 | crtc->event = c->state->event; |
| 228 | c->state->event = NULL; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 232 | static void atmel_hlcdc_crtc_atomic_flush(struct drm_crtc *crtc) |
| 233 | { |
| 234 | /* TODO: write common plane control register if available */ |
| 235 | } |
| 236 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 237 | static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs = { |
| 238 | .mode_fixup = atmel_hlcdc_crtc_mode_fixup, |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 239 | .mode_set = drm_helper_crtc_mode_set, |
| 240 | .mode_set_nofb = atmel_hlcdc_crtc_mode_set_nofb, |
| 241 | .mode_set_base = drm_helper_crtc_mode_set_base, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 242 | .disable = atmel_hlcdc_crtc_disable, |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 243 | .enable = atmel_hlcdc_crtc_enable, |
| 244 | .atomic_check = atmel_hlcdc_crtc_atomic_check, |
| 245 | .atomic_begin = atmel_hlcdc_crtc_atomic_begin, |
| 246 | .atomic_flush = atmel_hlcdc_crtc_atomic_flush, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | static void atmel_hlcdc_crtc_destroy(struct drm_crtc *c) |
| 250 | { |
| 251 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 252 | |
| 253 | drm_crtc_cleanup(c); |
| 254 | kfree(crtc); |
| 255 | } |
| 256 | |
| 257 | void atmel_hlcdc_crtc_cancel_page_flip(struct drm_crtc *c, |
| 258 | struct drm_file *file) |
| 259 | { |
| 260 | struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); |
| 261 | struct drm_pending_vblank_event *event; |
| 262 | struct drm_device *dev = c->dev; |
| 263 | unsigned long flags; |
| 264 | |
| 265 | spin_lock_irqsave(&dev->event_lock, flags); |
| 266 | event = crtc->event; |
| 267 | if (event && event->base.file_priv == file) { |
| 268 | event->base.destroy(&event->base); |
| 269 | drm_vblank_put(dev, crtc->id); |
| 270 | crtc->event = NULL; |
| 271 | } |
| 272 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 273 | } |
| 274 | |
| 275 | static void atmel_hlcdc_crtc_finish_page_flip(struct atmel_hlcdc_crtc *crtc) |
| 276 | { |
| 277 | struct drm_device *dev = crtc->base.dev; |
| 278 | unsigned long flags; |
| 279 | |
| 280 | spin_lock_irqsave(&dev->event_lock, flags); |
| 281 | if (crtc->event) { |
| 282 | drm_send_vblank_event(dev, crtc->id, crtc->event); |
| 283 | drm_vblank_put(dev, crtc->id); |
| 284 | crtc->event = NULL; |
| 285 | } |
| 286 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 287 | } |
| 288 | |
| 289 | void atmel_hlcdc_crtc_irq(struct drm_crtc *c) |
| 290 | { |
| 291 | drm_handle_vblank(c->dev, 0); |
| 292 | atmel_hlcdc_crtc_finish_page_flip(drm_crtc_to_atmel_hlcdc_crtc(c)); |
| 293 | } |
| 294 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 295 | static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs = { |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 296 | .page_flip = drm_atomic_helper_page_flip, |
| 297 | .set_config = drm_atomic_helper_set_config, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 298 | .destroy = atmel_hlcdc_crtc_destroy, |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 299 | .reset = drm_atomic_helper_crtc_reset, |
| 300 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, |
| 301 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | int atmel_hlcdc_crtc_create(struct drm_device *dev) |
| 305 | { |
| 306 | struct atmel_hlcdc_dc *dc = dev->dev_private; |
| 307 | struct atmel_hlcdc_planes *planes = dc->planes; |
| 308 | struct atmel_hlcdc_crtc *crtc; |
| 309 | int ret; |
| 310 | int i; |
| 311 | |
| 312 | crtc = kzalloc(sizeof(*crtc), GFP_KERNEL); |
| 313 | if (!crtc) |
| 314 | return -ENOMEM; |
| 315 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 316 | crtc->dc = dc; |
| 317 | |
| 318 | ret = drm_crtc_init_with_planes(dev, &crtc->base, |
| 319 | &planes->primary->base, |
| 320 | planes->cursor ? &planes->cursor->base : NULL, |
| 321 | &atmel_hlcdc_crtc_funcs); |
| 322 | if (ret < 0) |
| 323 | goto fail; |
| 324 | |
| 325 | crtc->id = drm_crtc_index(&crtc->base); |
| 326 | |
| 327 | if (planes->cursor) |
| 328 | planes->cursor->base.possible_crtcs = 1 << crtc->id; |
| 329 | |
| 330 | for (i = 0; i < planes->noverlays; i++) |
| 331 | planes->overlays[i]->base.possible_crtcs = 1 << crtc->id; |
| 332 | |
| 333 | drm_crtc_helper_add(&crtc->base, &lcdc_crtc_helper_funcs); |
| 334 | |
| 335 | dc->crtc = &crtc->base; |
| 336 | |
| 337 | return 0; |
| 338 | |
| 339 | fail: |
| 340 | atmel_hlcdc_crtc_destroy(&crtc->base); |
| 341 | return ret; |
| 342 | } |
| 343 | |