11a396789SBoris Brezillon /* 21a396789SBoris Brezillon * Copyright (C) 2014 Traphandler 31a396789SBoris Brezillon * Copyright (C) 2014 Free Electrons 41a396789SBoris Brezillon * 51a396789SBoris Brezillon * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com> 61a396789SBoris Brezillon * Author: Boris BREZILLON <boris.brezillon@free-electrons.com> 71a396789SBoris Brezillon * 81a396789SBoris Brezillon * This program is free software; you can redistribute it and/or modify it 91a396789SBoris Brezillon * under the terms of the GNU General Public License version 2 as published by 101a396789SBoris Brezillon * the Free Software Foundation. 111a396789SBoris Brezillon * 121a396789SBoris Brezillon * This program is distributed in the hope that it will be useful, but WITHOUT 131a396789SBoris Brezillon * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 141a396789SBoris Brezillon * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 151a396789SBoris Brezillon * more details. 161a396789SBoris Brezillon * 171a396789SBoris Brezillon * You should have received a copy of the GNU General Public License along with 181a396789SBoris Brezillon * this program. If not, see <http://www.gnu.org/licenses/>. 191a396789SBoris Brezillon */ 201a396789SBoris Brezillon 211a396789SBoris Brezillon #include <linux/clk.h> 221a396789SBoris Brezillon #include <linux/pm.h> 231a396789SBoris Brezillon #include <linux/pm_runtime.h> 2416e6004eSSylvain Rochet #include <linux/pinctrl/consumer.h> 251a396789SBoris Brezillon 261a396789SBoris Brezillon #include <drm/drm_crtc.h> 271a396789SBoris Brezillon #include <drm/drm_crtc_helper.h> 281a396789SBoris Brezillon #include <drm/drmP.h> 291a396789SBoris Brezillon 301a396789SBoris Brezillon #include <video/videomode.h> 311a396789SBoris Brezillon 321a396789SBoris Brezillon #include "atmel_hlcdc_dc.h" 331a396789SBoris Brezillon 341a396789SBoris Brezillon /** 35*aca63b76SBoris Brezillon * Atmel HLCDC CRTC state structure 36*aca63b76SBoris Brezillon * 37*aca63b76SBoris Brezillon * @base: base CRTC state 38*aca63b76SBoris Brezillon * @output_mode: RGBXXX output mode 39*aca63b76SBoris Brezillon */ 40*aca63b76SBoris Brezillon struct atmel_hlcdc_crtc_state { 41*aca63b76SBoris Brezillon struct drm_crtc_state base; 42*aca63b76SBoris Brezillon unsigned int output_mode; 43*aca63b76SBoris Brezillon }; 44*aca63b76SBoris Brezillon 45*aca63b76SBoris Brezillon static inline struct atmel_hlcdc_crtc_state * 46*aca63b76SBoris Brezillon drm_crtc_state_to_atmel_hlcdc_crtc_state(struct drm_crtc_state *state) 47*aca63b76SBoris Brezillon { 48*aca63b76SBoris Brezillon return container_of(state, struct atmel_hlcdc_crtc_state, base); 49*aca63b76SBoris Brezillon } 50*aca63b76SBoris Brezillon 51*aca63b76SBoris Brezillon /** 521a396789SBoris Brezillon * Atmel HLCDC CRTC structure 531a396789SBoris Brezillon * 541a396789SBoris Brezillon * @base: base DRM CRTC structure 551a396789SBoris Brezillon * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device 561a396789SBoris Brezillon * @event: pointer to the current page flip event 571a396789SBoris Brezillon * @id: CRTC id (returned by drm_crtc_index) 582389fc13SBoris Brezillon * @enabled: CRTC state 591a396789SBoris Brezillon */ 601a396789SBoris Brezillon struct atmel_hlcdc_crtc { 611a396789SBoris Brezillon struct drm_crtc base; 621a396789SBoris Brezillon struct atmel_hlcdc_dc *dc; 631a396789SBoris Brezillon struct drm_pending_vblank_event *event; 641a396789SBoris Brezillon int id; 652389fc13SBoris Brezillon bool enabled; 661a396789SBoris Brezillon }; 671a396789SBoris Brezillon 681a396789SBoris Brezillon static inline struct atmel_hlcdc_crtc * 691a396789SBoris Brezillon drm_crtc_to_atmel_hlcdc_crtc(struct drm_crtc *crtc) 701a396789SBoris Brezillon { 711a396789SBoris Brezillon return container_of(crtc, struct atmel_hlcdc_crtc, base); 721a396789SBoris Brezillon } 731a396789SBoris Brezillon 742389fc13SBoris Brezillon static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c) 751a396789SBoris Brezillon { 761a396789SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 771a396789SBoris Brezillon struct regmap *regmap = crtc->dc->hlcdc->regmap; 782389fc13SBoris Brezillon struct drm_display_mode *adj = &c->state->adjusted_mode; 79*aca63b76SBoris Brezillon struct atmel_hlcdc_crtc_state *state; 801a396789SBoris Brezillon unsigned long mode_rate; 811a396789SBoris Brezillon struct videomode vm; 821a396789SBoris Brezillon unsigned long prate; 831a396789SBoris Brezillon unsigned int cfg; 841a396789SBoris Brezillon int div; 851a396789SBoris Brezillon 861a396789SBoris Brezillon vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay; 871a396789SBoris Brezillon vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end; 881a396789SBoris Brezillon vm.vsync_len = adj->crtc_vsync_end - adj->crtc_vsync_start; 891a396789SBoris Brezillon vm.hfront_porch = adj->crtc_hsync_start - adj->crtc_hdisplay; 901a396789SBoris Brezillon vm.hback_porch = adj->crtc_htotal - adj->crtc_hsync_end; 911a396789SBoris Brezillon vm.hsync_len = adj->crtc_hsync_end - adj->crtc_hsync_start; 921a396789SBoris Brezillon 931a396789SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_CFG(1), 941a396789SBoris Brezillon (vm.hsync_len - 1) | ((vm.vsync_len - 1) << 16)); 951a396789SBoris Brezillon 961a396789SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_CFG(2), 971a396789SBoris Brezillon (vm.vfront_porch - 1) | (vm.vback_porch << 16)); 981a396789SBoris Brezillon 991a396789SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_CFG(3), 1001a396789SBoris Brezillon (vm.hfront_porch - 1) | ((vm.hback_porch - 1) << 16)); 1011a396789SBoris Brezillon 1021a396789SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_CFG(4), 1031a396789SBoris Brezillon (adj->crtc_hdisplay - 1) | 1041a396789SBoris Brezillon ((adj->crtc_vdisplay - 1) << 16)); 1051a396789SBoris Brezillon 1060bb59cb0SNicolas Ferre cfg = 0; 1071a396789SBoris Brezillon 1081a396789SBoris Brezillon prate = clk_get_rate(crtc->dc->hlcdc->sys_clk); 1092389fc13SBoris Brezillon mode_rate = adj->crtc_clock * 1000; 1101a396789SBoris Brezillon if ((prate / 2) < mode_rate) { 1111a396789SBoris Brezillon prate *= 2; 1121a396789SBoris Brezillon cfg |= ATMEL_HLCDC_CLKSEL; 1131a396789SBoris Brezillon } 1141a396789SBoris Brezillon 1151a396789SBoris Brezillon div = DIV_ROUND_UP(prate, mode_rate); 1161a396789SBoris Brezillon if (div < 2) 1171a396789SBoris Brezillon div = 2; 1181a396789SBoris Brezillon 1191a396789SBoris Brezillon cfg |= ATMEL_HLCDC_CLKDIV(div); 1201a396789SBoris Brezillon 1211a396789SBoris Brezillon regmap_update_bits(regmap, ATMEL_HLCDC_CFG(0), 1221a396789SBoris Brezillon ATMEL_HLCDC_CLKSEL | ATMEL_HLCDC_CLKDIV_MASK | 1231a396789SBoris Brezillon ATMEL_HLCDC_CLKPOL, cfg); 1241a396789SBoris Brezillon 1251a396789SBoris Brezillon cfg = 0; 1261a396789SBoris Brezillon 1272389fc13SBoris Brezillon if (adj->flags & DRM_MODE_FLAG_NVSYNC) 1281a396789SBoris Brezillon cfg |= ATMEL_HLCDC_VSPOL; 1291a396789SBoris Brezillon 1302389fc13SBoris Brezillon if (adj->flags & DRM_MODE_FLAG_NHSYNC) 1311a396789SBoris Brezillon cfg |= ATMEL_HLCDC_HSPOL; 1321a396789SBoris Brezillon 133*aca63b76SBoris Brezillon state = drm_crtc_state_to_atmel_hlcdc_crtc_state(c->state); 134*aca63b76SBoris Brezillon cfg |= state->output_mode << 8; 135*aca63b76SBoris Brezillon 1361a396789SBoris Brezillon regmap_update_bits(regmap, ATMEL_HLCDC_CFG(5), 1371a396789SBoris Brezillon ATMEL_HLCDC_HSPOL | ATMEL_HLCDC_VSPOL | 1381a396789SBoris Brezillon ATMEL_HLCDC_VSPDLYS | ATMEL_HLCDC_VSPDLYE | 1391a396789SBoris Brezillon ATMEL_HLCDC_DISPPOL | ATMEL_HLCDC_DISPDLY | 1401a396789SBoris Brezillon ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO | 141*aca63b76SBoris Brezillon ATMEL_HLCDC_GUARDTIME_MASK | ATMEL_HLCDC_MODE_MASK, 1421a396789SBoris Brezillon cfg); 1431a396789SBoris Brezillon } 1441a396789SBoris Brezillon 1452389fc13SBoris Brezillon static void atmel_hlcdc_crtc_disable(struct drm_crtc *c) 1461a396789SBoris Brezillon { 1472389fc13SBoris Brezillon struct drm_device *dev = c->dev; 1482389fc13SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 1492389fc13SBoris Brezillon struct regmap *regmap = crtc->dc->hlcdc->regmap; 1502389fc13SBoris Brezillon unsigned int status; 1511a396789SBoris Brezillon 1522389fc13SBoris Brezillon if (!crtc->enabled) 1532389fc13SBoris Brezillon return; 1541a396789SBoris Brezillon 1552389fc13SBoris Brezillon drm_crtc_vblank_off(c); 1561a396789SBoris Brezillon 1572389fc13SBoris Brezillon pm_runtime_get_sync(dev->dev); 1582389fc13SBoris Brezillon 1592389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_DISP); 1602389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1612389fc13SBoris Brezillon (status & ATMEL_HLCDC_DISP)) 1622389fc13SBoris Brezillon cpu_relax(); 1632389fc13SBoris Brezillon 1642389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_SYNC); 1652389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1662389fc13SBoris Brezillon (status & ATMEL_HLCDC_SYNC)) 1672389fc13SBoris Brezillon cpu_relax(); 1682389fc13SBoris Brezillon 1692389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PIXEL_CLK); 1702389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1712389fc13SBoris Brezillon (status & ATMEL_HLCDC_PIXEL_CLK)) 1722389fc13SBoris Brezillon cpu_relax(); 1732389fc13SBoris Brezillon 1742389fc13SBoris Brezillon clk_disable_unprepare(crtc->dc->hlcdc->sys_clk); 17516e6004eSSylvain Rochet pinctrl_pm_select_sleep_state(dev->dev); 1762389fc13SBoris Brezillon 1772389fc13SBoris Brezillon pm_runtime_allow(dev->dev); 1782389fc13SBoris Brezillon 1792389fc13SBoris Brezillon pm_runtime_put_sync(dev->dev); 1802389fc13SBoris Brezillon 1812389fc13SBoris Brezillon crtc->enabled = false; 1821a396789SBoris Brezillon } 1832389fc13SBoris Brezillon 1842389fc13SBoris Brezillon static void atmel_hlcdc_crtc_enable(struct drm_crtc *c) 1852389fc13SBoris Brezillon { 1862389fc13SBoris Brezillon struct drm_device *dev = c->dev; 1872389fc13SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 1882389fc13SBoris Brezillon struct regmap *regmap = crtc->dc->hlcdc->regmap; 1892389fc13SBoris Brezillon unsigned int status; 1902389fc13SBoris Brezillon 1912389fc13SBoris Brezillon if (crtc->enabled) 1922389fc13SBoris Brezillon return; 1932389fc13SBoris Brezillon 1942389fc13SBoris Brezillon pm_runtime_get_sync(dev->dev); 1952389fc13SBoris Brezillon 1962389fc13SBoris Brezillon pm_runtime_forbid(dev->dev); 1972389fc13SBoris Brezillon 19816e6004eSSylvain Rochet pinctrl_pm_select_default_state(dev->dev); 1992389fc13SBoris Brezillon clk_prepare_enable(crtc->dc->hlcdc->sys_clk); 2002389fc13SBoris Brezillon 2012389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PIXEL_CLK); 2022389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 2032389fc13SBoris Brezillon !(status & ATMEL_HLCDC_PIXEL_CLK)) 2042389fc13SBoris Brezillon cpu_relax(); 2052389fc13SBoris Brezillon 2062389fc13SBoris Brezillon 2072389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_SYNC); 2082389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 2092389fc13SBoris Brezillon !(status & ATMEL_HLCDC_SYNC)) 2102389fc13SBoris Brezillon cpu_relax(); 2112389fc13SBoris Brezillon 2122389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_DISP); 2132389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 2142389fc13SBoris Brezillon !(status & ATMEL_HLCDC_DISP)) 2152389fc13SBoris Brezillon cpu_relax(); 2162389fc13SBoris Brezillon 2172389fc13SBoris Brezillon pm_runtime_put_sync(dev->dev); 2182389fc13SBoris Brezillon 2192389fc13SBoris Brezillon drm_crtc_vblank_on(c); 2202389fc13SBoris Brezillon 2212389fc13SBoris Brezillon crtc->enabled = true; 2222389fc13SBoris Brezillon } 2232389fc13SBoris Brezillon 224f026eb6eSSylvain Rochet void atmel_hlcdc_crtc_suspend(struct drm_crtc *c) 225f026eb6eSSylvain Rochet { 226f026eb6eSSylvain Rochet struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 227f026eb6eSSylvain Rochet 228f026eb6eSSylvain Rochet if (crtc->enabled) { 229f026eb6eSSylvain Rochet atmel_hlcdc_crtc_disable(c); 230f026eb6eSSylvain Rochet /* save enable state for resume */ 231f026eb6eSSylvain Rochet crtc->enabled = true; 232f026eb6eSSylvain Rochet } 233f026eb6eSSylvain Rochet } 234f026eb6eSSylvain Rochet 235f026eb6eSSylvain Rochet void atmel_hlcdc_crtc_resume(struct drm_crtc *c) 236f026eb6eSSylvain Rochet { 237f026eb6eSSylvain Rochet struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 238f026eb6eSSylvain Rochet 239f026eb6eSSylvain Rochet if (crtc->enabled) { 240f026eb6eSSylvain Rochet crtc->enabled = false; 241f026eb6eSSylvain Rochet atmel_hlcdc_crtc_enable(c); 242f026eb6eSSylvain Rochet } 243f026eb6eSSylvain Rochet } 244f026eb6eSSylvain Rochet 245*aca63b76SBoris Brezillon #define ATMEL_HLCDC_RGB444_OUTPUT BIT(0) 246*aca63b76SBoris Brezillon #define ATMEL_HLCDC_RGB565_OUTPUT BIT(1) 247*aca63b76SBoris Brezillon #define ATMEL_HLCDC_RGB666_OUTPUT BIT(2) 248*aca63b76SBoris Brezillon #define ATMEL_HLCDC_RGB888_OUTPUT BIT(3) 249*aca63b76SBoris Brezillon #define ATMEL_HLCDC_OUTPUT_MODE_MASK GENMASK(3, 0) 250*aca63b76SBoris Brezillon 251*aca63b76SBoris Brezillon static int atmel_hlcdc_crtc_select_output_mode(struct drm_crtc_state *state) 252*aca63b76SBoris Brezillon { 253*aca63b76SBoris Brezillon unsigned int output_fmts = ATMEL_HLCDC_OUTPUT_MODE_MASK; 254*aca63b76SBoris Brezillon struct atmel_hlcdc_crtc_state *hstate; 255*aca63b76SBoris Brezillon struct drm_connector_state *cstate; 256*aca63b76SBoris Brezillon struct drm_connector *connector; 257*aca63b76SBoris Brezillon struct atmel_hlcdc_crtc *crtc; 258*aca63b76SBoris Brezillon int i; 259*aca63b76SBoris Brezillon 260*aca63b76SBoris Brezillon crtc = drm_crtc_to_atmel_hlcdc_crtc(state->crtc); 261*aca63b76SBoris Brezillon 262*aca63b76SBoris Brezillon for_each_connector_in_state(state->state, connector, cstate, i) { 263*aca63b76SBoris Brezillon struct drm_display_info *info = &connector->display_info; 264*aca63b76SBoris Brezillon unsigned int supported_fmts = 0; 265*aca63b76SBoris Brezillon int j; 266*aca63b76SBoris Brezillon 267*aca63b76SBoris Brezillon if (!cstate->crtc) 268*aca63b76SBoris Brezillon continue; 269*aca63b76SBoris Brezillon 270*aca63b76SBoris Brezillon for (j = 0; j < info->num_bus_formats; j++) { 271*aca63b76SBoris Brezillon switch (info->bus_formats[j]) { 272*aca63b76SBoris Brezillon case MEDIA_BUS_FMT_RGB444_1X12: 273*aca63b76SBoris Brezillon supported_fmts |= ATMEL_HLCDC_RGB444_OUTPUT; 274*aca63b76SBoris Brezillon break; 275*aca63b76SBoris Brezillon case MEDIA_BUS_FMT_RGB565_1X16: 276*aca63b76SBoris Brezillon supported_fmts |= ATMEL_HLCDC_RGB565_OUTPUT; 277*aca63b76SBoris Brezillon break; 278*aca63b76SBoris Brezillon case MEDIA_BUS_FMT_RGB666_1X18: 279*aca63b76SBoris Brezillon supported_fmts |= ATMEL_HLCDC_RGB666_OUTPUT; 280*aca63b76SBoris Brezillon break; 281*aca63b76SBoris Brezillon case MEDIA_BUS_FMT_RGB888_1X24: 282*aca63b76SBoris Brezillon supported_fmts |= ATMEL_HLCDC_RGB888_OUTPUT; 283*aca63b76SBoris Brezillon break; 284*aca63b76SBoris Brezillon default: 285*aca63b76SBoris Brezillon break; 286*aca63b76SBoris Brezillon } 287*aca63b76SBoris Brezillon } 288*aca63b76SBoris Brezillon 289*aca63b76SBoris Brezillon if (crtc->dc->desc->conflicting_output_formats) 290*aca63b76SBoris Brezillon output_fmts &= supported_fmts; 291*aca63b76SBoris Brezillon else 292*aca63b76SBoris Brezillon output_fmts |= supported_fmts; 293*aca63b76SBoris Brezillon } 294*aca63b76SBoris Brezillon 295*aca63b76SBoris Brezillon if (!output_fmts) 296*aca63b76SBoris Brezillon return -EINVAL; 297*aca63b76SBoris Brezillon 298*aca63b76SBoris Brezillon hstate = drm_crtc_state_to_atmel_hlcdc_crtc_state(state); 299*aca63b76SBoris Brezillon hstate->output_mode = fls(output_fmts) - 1; 300*aca63b76SBoris Brezillon 301*aca63b76SBoris Brezillon return 0; 302*aca63b76SBoris Brezillon } 303*aca63b76SBoris Brezillon 3042389fc13SBoris Brezillon static int atmel_hlcdc_crtc_atomic_check(struct drm_crtc *c, 3052389fc13SBoris Brezillon struct drm_crtc_state *s) 3062389fc13SBoris Brezillon { 3072389fc13SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 308*aca63b76SBoris Brezillon int ret; 3092389fc13SBoris Brezillon 3102389fc13SBoris Brezillon if (atmel_hlcdc_dc_mode_valid(crtc->dc, &s->adjusted_mode) != MODE_OK) 3112389fc13SBoris Brezillon return -EINVAL; 3122389fc13SBoris Brezillon 313*aca63b76SBoris Brezillon ret = atmel_hlcdc_crtc_select_output_mode(s); 314*aca63b76SBoris Brezillon if (ret) 315*aca63b76SBoris Brezillon return ret; 316*aca63b76SBoris Brezillon 3175957017dSBoris Brezillon return atmel_hlcdc_plane_prepare_disc_area(s); 3182389fc13SBoris Brezillon } 3192389fc13SBoris Brezillon 320613d2b27SMaarten Lankhorst static void atmel_hlcdc_crtc_atomic_begin(struct drm_crtc *c, 321613d2b27SMaarten Lankhorst struct drm_crtc_state *old_s) 3222389fc13SBoris Brezillon { 3232389fc13SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 3242389fc13SBoris Brezillon 3252389fc13SBoris Brezillon if (c->state->event) { 3262389fc13SBoris Brezillon c->state->event->pipe = drm_crtc_index(c); 3272389fc13SBoris Brezillon 3282389fc13SBoris Brezillon WARN_ON(drm_crtc_vblank_get(c) != 0); 3292389fc13SBoris Brezillon 3302389fc13SBoris Brezillon crtc->event = c->state->event; 3312389fc13SBoris Brezillon c->state->event = NULL; 3322389fc13SBoris Brezillon } 3332389fc13SBoris Brezillon } 3342389fc13SBoris Brezillon 335613d2b27SMaarten Lankhorst static void atmel_hlcdc_crtc_atomic_flush(struct drm_crtc *crtc, 336613d2b27SMaarten Lankhorst struct drm_crtc_state *old_s) 3372389fc13SBoris Brezillon { 3382389fc13SBoris Brezillon /* TODO: write common plane control register if available */ 3391a396789SBoris Brezillon } 3401a396789SBoris Brezillon 3411a396789SBoris Brezillon static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs = { 3422389fc13SBoris Brezillon .mode_set = drm_helper_crtc_mode_set, 3432389fc13SBoris Brezillon .mode_set_nofb = atmel_hlcdc_crtc_mode_set_nofb, 3442389fc13SBoris Brezillon .mode_set_base = drm_helper_crtc_mode_set_base, 3451a396789SBoris Brezillon .disable = atmel_hlcdc_crtc_disable, 3462389fc13SBoris Brezillon .enable = atmel_hlcdc_crtc_enable, 3472389fc13SBoris Brezillon .atomic_check = atmel_hlcdc_crtc_atomic_check, 3482389fc13SBoris Brezillon .atomic_begin = atmel_hlcdc_crtc_atomic_begin, 3492389fc13SBoris Brezillon .atomic_flush = atmel_hlcdc_crtc_atomic_flush, 3501a396789SBoris Brezillon }; 3511a396789SBoris Brezillon 3521a396789SBoris Brezillon static void atmel_hlcdc_crtc_destroy(struct drm_crtc *c) 3531a396789SBoris Brezillon { 3541a396789SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 3551a396789SBoris Brezillon 3561a396789SBoris Brezillon drm_crtc_cleanup(c); 3571a396789SBoris Brezillon kfree(crtc); 3581a396789SBoris Brezillon } 3591a396789SBoris Brezillon 3601a396789SBoris Brezillon static void atmel_hlcdc_crtc_finish_page_flip(struct atmel_hlcdc_crtc *crtc) 3611a396789SBoris Brezillon { 3621a396789SBoris Brezillon struct drm_device *dev = crtc->base.dev; 3631a396789SBoris Brezillon unsigned long flags; 3641a396789SBoris Brezillon 3651a396789SBoris Brezillon spin_lock_irqsave(&dev->event_lock, flags); 3661a396789SBoris Brezillon if (crtc->event) { 3671a396789SBoris Brezillon drm_send_vblank_event(dev, crtc->id, crtc->event); 3681a396789SBoris Brezillon drm_vblank_put(dev, crtc->id); 3691a396789SBoris Brezillon crtc->event = NULL; 3701a396789SBoris Brezillon } 3711a396789SBoris Brezillon spin_unlock_irqrestore(&dev->event_lock, flags); 3721a396789SBoris Brezillon } 3731a396789SBoris Brezillon 3741a396789SBoris Brezillon void atmel_hlcdc_crtc_irq(struct drm_crtc *c) 3751a396789SBoris Brezillon { 3761a396789SBoris Brezillon drm_handle_vblank(c->dev, 0); 3771a396789SBoris Brezillon atmel_hlcdc_crtc_finish_page_flip(drm_crtc_to_atmel_hlcdc_crtc(c)); 3781a396789SBoris Brezillon } 3791a396789SBoris Brezillon 380*aca63b76SBoris Brezillon void atmel_hlcdc_crtc_reset(struct drm_crtc *crtc) 381*aca63b76SBoris Brezillon { 382*aca63b76SBoris Brezillon struct atmel_hlcdc_crtc_state *state; 383*aca63b76SBoris Brezillon 384*aca63b76SBoris Brezillon if (crtc->state && crtc->state->mode_blob) 385*aca63b76SBoris Brezillon drm_property_unreference_blob(crtc->state->mode_blob); 386*aca63b76SBoris Brezillon 387*aca63b76SBoris Brezillon if (crtc->state) { 388*aca63b76SBoris Brezillon state = drm_crtc_state_to_atmel_hlcdc_crtc_state(crtc->state); 389*aca63b76SBoris Brezillon kfree(state); 390*aca63b76SBoris Brezillon } 391*aca63b76SBoris Brezillon 392*aca63b76SBoris Brezillon state = kzalloc(sizeof(*state), GFP_KERNEL); 393*aca63b76SBoris Brezillon if (state) { 394*aca63b76SBoris Brezillon crtc->state = &state->base; 395*aca63b76SBoris Brezillon crtc->state->crtc = crtc; 396*aca63b76SBoris Brezillon } 397*aca63b76SBoris Brezillon } 398*aca63b76SBoris Brezillon 399*aca63b76SBoris Brezillon static struct drm_crtc_state * 400*aca63b76SBoris Brezillon atmel_hlcdc_crtc_duplicate_state(struct drm_crtc *crtc) 401*aca63b76SBoris Brezillon { 402*aca63b76SBoris Brezillon struct atmel_hlcdc_crtc_state *state, *cur; 403*aca63b76SBoris Brezillon 404*aca63b76SBoris Brezillon if (WARN_ON(!crtc->state)) 405*aca63b76SBoris Brezillon return NULL; 406*aca63b76SBoris Brezillon 407*aca63b76SBoris Brezillon state = kmalloc(sizeof(*state), GFP_KERNEL); 408*aca63b76SBoris Brezillon if (state) 409*aca63b76SBoris Brezillon __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 410*aca63b76SBoris Brezillon 411*aca63b76SBoris Brezillon cur = drm_crtc_state_to_atmel_hlcdc_crtc_state(crtc->state); 412*aca63b76SBoris Brezillon state->output_mode = cur->output_mode; 413*aca63b76SBoris Brezillon 414*aca63b76SBoris Brezillon return &state->base; 415*aca63b76SBoris Brezillon } 416*aca63b76SBoris Brezillon 417*aca63b76SBoris Brezillon static void atmel_hlcdc_crtc_destroy_state(struct drm_crtc *crtc, 418*aca63b76SBoris Brezillon struct drm_crtc_state *s) 419*aca63b76SBoris Brezillon { 420*aca63b76SBoris Brezillon struct atmel_hlcdc_crtc_state *state; 421*aca63b76SBoris Brezillon 422*aca63b76SBoris Brezillon state = drm_crtc_state_to_atmel_hlcdc_crtc_state(s); 423*aca63b76SBoris Brezillon __drm_atomic_helper_crtc_destroy_state(crtc, s); 424*aca63b76SBoris Brezillon kfree(state); 425*aca63b76SBoris Brezillon } 426*aca63b76SBoris Brezillon 4271a396789SBoris Brezillon static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs = { 4282389fc13SBoris Brezillon .page_flip = drm_atomic_helper_page_flip, 4292389fc13SBoris Brezillon .set_config = drm_atomic_helper_set_config, 4301a396789SBoris Brezillon .destroy = atmel_hlcdc_crtc_destroy, 431*aca63b76SBoris Brezillon .reset = atmel_hlcdc_crtc_reset, 432*aca63b76SBoris Brezillon .atomic_duplicate_state = atmel_hlcdc_crtc_duplicate_state, 433*aca63b76SBoris Brezillon .atomic_destroy_state = atmel_hlcdc_crtc_destroy_state, 4341a396789SBoris Brezillon }; 4351a396789SBoris Brezillon 4361a396789SBoris Brezillon int atmel_hlcdc_crtc_create(struct drm_device *dev) 4371a396789SBoris Brezillon { 4381a396789SBoris Brezillon struct atmel_hlcdc_dc *dc = dev->dev_private; 4391a396789SBoris Brezillon struct atmel_hlcdc_planes *planes = dc->planes; 4401a396789SBoris Brezillon struct atmel_hlcdc_crtc *crtc; 4411a396789SBoris Brezillon int ret; 4421a396789SBoris Brezillon int i; 4431a396789SBoris Brezillon 4441a396789SBoris Brezillon crtc = kzalloc(sizeof(*crtc), GFP_KERNEL); 4451a396789SBoris Brezillon if (!crtc) 4461a396789SBoris Brezillon return -ENOMEM; 4471a396789SBoris Brezillon 4481a396789SBoris Brezillon crtc->dc = dc; 4491a396789SBoris Brezillon 4501a396789SBoris Brezillon ret = drm_crtc_init_with_planes(dev, &crtc->base, 4511a396789SBoris Brezillon &planes->primary->base, 4521a396789SBoris Brezillon planes->cursor ? &planes->cursor->base : NULL, 453f9882876SVille Syrjälä &atmel_hlcdc_crtc_funcs, NULL); 4541a396789SBoris Brezillon if (ret < 0) 4551a396789SBoris Brezillon goto fail; 4561a396789SBoris Brezillon 4571a396789SBoris Brezillon crtc->id = drm_crtc_index(&crtc->base); 4581a396789SBoris Brezillon 4591a396789SBoris Brezillon if (planes->cursor) 4601a396789SBoris Brezillon planes->cursor->base.possible_crtcs = 1 << crtc->id; 4611a396789SBoris Brezillon 4621a396789SBoris Brezillon for (i = 0; i < planes->noverlays; i++) 4631a396789SBoris Brezillon planes->overlays[i]->base.possible_crtcs = 1 << crtc->id; 4641a396789SBoris Brezillon 4651a396789SBoris Brezillon drm_crtc_helper_add(&crtc->base, &lcdc_crtc_helper_funcs); 4668c4b4b0dSBoris Brezillon drm_crtc_vblank_reset(&crtc->base); 4671a396789SBoris Brezillon 4681a396789SBoris Brezillon dc->crtc = &crtc->base; 4691a396789SBoris Brezillon 4701a396789SBoris Brezillon return 0; 4711a396789SBoris Brezillon 4721a396789SBoris Brezillon fail: 4731a396789SBoris Brezillon atmel_hlcdc_crtc_destroy(&crtc->base); 4741a396789SBoris Brezillon return ret; 4751a396789SBoris Brezillon } 476