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> 241a396789SBoris Brezillon 251a396789SBoris Brezillon #include <drm/drm_crtc.h> 261a396789SBoris Brezillon #include <drm/drm_crtc_helper.h> 271a396789SBoris Brezillon #include <drm/drmP.h> 281a396789SBoris Brezillon 291a396789SBoris Brezillon #include <video/videomode.h> 301a396789SBoris Brezillon 311a396789SBoris Brezillon #include "atmel_hlcdc_dc.h" 321a396789SBoris Brezillon 331a396789SBoris Brezillon /** 341a396789SBoris Brezillon * Atmel HLCDC CRTC structure 351a396789SBoris Brezillon * 361a396789SBoris Brezillon * @base: base DRM CRTC structure 371a396789SBoris Brezillon * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device 381a396789SBoris Brezillon * @event: pointer to the current page flip event 391a396789SBoris Brezillon * @id: CRTC id (returned by drm_crtc_index) 402389fc13SBoris Brezillon * @enabled: CRTC state 411a396789SBoris Brezillon */ 421a396789SBoris Brezillon struct atmel_hlcdc_crtc { 431a396789SBoris Brezillon struct drm_crtc base; 441a396789SBoris Brezillon struct atmel_hlcdc_dc *dc; 451a396789SBoris Brezillon struct drm_pending_vblank_event *event; 461a396789SBoris Brezillon int id; 472389fc13SBoris Brezillon bool enabled; 481a396789SBoris Brezillon }; 491a396789SBoris Brezillon 501a396789SBoris Brezillon static inline struct atmel_hlcdc_crtc * 511a396789SBoris Brezillon drm_crtc_to_atmel_hlcdc_crtc(struct drm_crtc *crtc) 521a396789SBoris Brezillon { 531a396789SBoris Brezillon return container_of(crtc, struct atmel_hlcdc_crtc, base); 541a396789SBoris Brezillon } 551a396789SBoris Brezillon 562389fc13SBoris Brezillon static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c) 571a396789SBoris Brezillon { 581a396789SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 591a396789SBoris Brezillon struct regmap *regmap = crtc->dc->hlcdc->regmap; 602389fc13SBoris Brezillon struct drm_display_mode *adj = &c->state->adjusted_mode; 611a396789SBoris Brezillon unsigned long mode_rate; 621a396789SBoris Brezillon struct videomode vm; 631a396789SBoris Brezillon unsigned long prate; 641a396789SBoris Brezillon unsigned int cfg; 651a396789SBoris Brezillon int div; 661a396789SBoris Brezillon 671a396789SBoris Brezillon vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay; 681a396789SBoris Brezillon vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end; 691a396789SBoris Brezillon vm.vsync_len = adj->crtc_vsync_end - adj->crtc_vsync_start; 701a396789SBoris Brezillon vm.hfront_porch = adj->crtc_hsync_start - adj->crtc_hdisplay; 711a396789SBoris Brezillon vm.hback_porch = adj->crtc_htotal - adj->crtc_hsync_end; 721a396789SBoris Brezillon vm.hsync_len = adj->crtc_hsync_end - adj->crtc_hsync_start; 731a396789SBoris Brezillon 741a396789SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_CFG(1), 751a396789SBoris Brezillon (vm.hsync_len - 1) | ((vm.vsync_len - 1) << 16)); 761a396789SBoris Brezillon 771a396789SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_CFG(2), 781a396789SBoris Brezillon (vm.vfront_porch - 1) | (vm.vback_porch << 16)); 791a396789SBoris Brezillon 801a396789SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_CFG(3), 811a396789SBoris Brezillon (vm.hfront_porch - 1) | ((vm.hback_porch - 1) << 16)); 821a396789SBoris Brezillon 831a396789SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_CFG(4), 841a396789SBoris Brezillon (adj->crtc_hdisplay - 1) | 851a396789SBoris Brezillon ((adj->crtc_vdisplay - 1) << 16)); 861a396789SBoris Brezillon 871a396789SBoris Brezillon cfg = ATMEL_HLCDC_CLKPOL; 881a396789SBoris Brezillon 891a396789SBoris Brezillon prate = clk_get_rate(crtc->dc->hlcdc->sys_clk); 902389fc13SBoris Brezillon mode_rate = adj->crtc_clock * 1000; 911a396789SBoris Brezillon if ((prate / 2) < mode_rate) { 921a396789SBoris Brezillon prate *= 2; 931a396789SBoris Brezillon cfg |= ATMEL_HLCDC_CLKSEL; 941a396789SBoris Brezillon } 951a396789SBoris Brezillon 961a396789SBoris Brezillon div = DIV_ROUND_UP(prate, mode_rate); 971a396789SBoris Brezillon if (div < 2) 981a396789SBoris Brezillon div = 2; 991a396789SBoris Brezillon 1001a396789SBoris Brezillon cfg |= ATMEL_HLCDC_CLKDIV(div); 1011a396789SBoris Brezillon 1021a396789SBoris Brezillon regmap_update_bits(regmap, ATMEL_HLCDC_CFG(0), 1031a396789SBoris Brezillon ATMEL_HLCDC_CLKSEL | ATMEL_HLCDC_CLKDIV_MASK | 1041a396789SBoris Brezillon ATMEL_HLCDC_CLKPOL, cfg); 1051a396789SBoris Brezillon 1061a396789SBoris Brezillon cfg = 0; 1071a396789SBoris Brezillon 1082389fc13SBoris Brezillon if (adj->flags & DRM_MODE_FLAG_NVSYNC) 1091a396789SBoris Brezillon cfg |= ATMEL_HLCDC_VSPOL; 1101a396789SBoris Brezillon 1112389fc13SBoris Brezillon if (adj->flags & DRM_MODE_FLAG_NHSYNC) 1121a396789SBoris Brezillon cfg |= ATMEL_HLCDC_HSPOL; 1131a396789SBoris Brezillon 1141a396789SBoris Brezillon regmap_update_bits(regmap, ATMEL_HLCDC_CFG(5), 1151a396789SBoris Brezillon ATMEL_HLCDC_HSPOL | ATMEL_HLCDC_VSPOL | 1161a396789SBoris Brezillon ATMEL_HLCDC_VSPDLYS | ATMEL_HLCDC_VSPDLYE | 1171a396789SBoris Brezillon ATMEL_HLCDC_DISPPOL | ATMEL_HLCDC_DISPDLY | 1181a396789SBoris Brezillon ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO | 1191a396789SBoris Brezillon ATMEL_HLCDC_GUARDTIME_MASK, 1201a396789SBoris Brezillon cfg); 1211a396789SBoris Brezillon } 1221a396789SBoris Brezillon 1231a396789SBoris Brezillon static bool atmel_hlcdc_crtc_mode_fixup(struct drm_crtc *crtc, 1241a396789SBoris Brezillon const struct drm_display_mode *mode, 1251a396789SBoris Brezillon struct drm_display_mode *adjusted_mode) 1261a396789SBoris Brezillon { 1271a396789SBoris Brezillon return true; 1281a396789SBoris Brezillon } 1291a396789SBoris Brezillon 1302389fc13SBoris Brezillon static void atmel_hlcdc_crtc_disable(struct drm_crtc *c) 1311a396789SBoris Brezillon { 1322389fc13SBoris Brezillon struct drm_device *dev = c->dev; 1332389fc13SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 1342389fc13SBoris Brezillon struct regmap *regmap = crtc->dc->hlcdc->regmap; 1352389fc13SBoris Brezillon unsigned int status; 1361a396789SBoris Brezillon 1372389fc13SBoris Brezillon if (!crtc->enabled) 1382389fc13SBoris Brezillon return; 1391a396789SBoris Brezillon 1402389fc13SBoris Brezillon drm_crtc_vblank_off(c); 1411a396789SBoris Brezillon 1422389fc13SBoris Brezillon pm_runtime_get_sync(dev->dev); 1432389fc13SBoris Brezillon 1442389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_DISP); 1452389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1462389fc13SBoris Brezillon (status & ATMEL_HLCDC_DISP)) 1472389fc13SBoris Brezillon cpu_relax(); 1482389fc13SBoris Brezillon 1492389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_SYNC); 1502389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1512389fc13SBoris Brezillon (status & ATMEL_HLCDC_SYNC)) 1522389fc13SBoris Brezillon cpu_relax(); 1532389fc13SBoris Brezillon 1542389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PIXEL_CLK); 1552389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1562389fc13SBoris Brezillon (status & ATMEL_HLCDC_PIXEL_CLK)) 1572389fc13SBoris Brezillon cpu_relax(); 1582389fc13SBoris Brezillon 1592389fc13SBoris Brezillon clk_disable_unprepare(crtc->dc->hlcdc->sys_clk); 1602389fc13SBoris Brezillon 1612389fc13SBoris Brezillon pm_runtime_allow(dev->dev); 1622389fc13SBoris Brezillon 1632389fc13SBoris Brezillon pm_runtime_put_sync(dev->dev); 1642389fc13SBoris Brezillon 1652389fc13SBoris Brezillon crtc->enabled = false; 1661a396789SBoris Brezillon } 1672389fc13SBoris Brezillon 1682389fc13SBoris Brezillon static void atmel_hlcdc_crtc_enable(struct drm_crtc *c) 1692389fc13SBoris Brezillon { 1702389fc13SBoris Brezillon struct drm_device *dev = c->dev; 1712389fc13SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 1722389fc13SBoris Brezillon struct regmap *regmap = crtc->dc->hlcdc->regmap; 1732389fc13SBoris Brezillon unsigned int status; 1742389fc13SBoris Brezillon 1752389fc13SBoris Brezillon if (crtc->enabled) 1762389fc13SBoris Brezillon return; 1772389fc13SBoris Brezillon 1782389fc13SBoris Brezillon pm_runtime_get_sync(dev->dev); 1792389fc13SBoris Brezillon 1802389fc13SBoris Brezillon pm_runtime_forbid(dev->dev); 1812389fc13SBoris Brezillon 1822389fc13SBoris Brezillon clk_prepare_enable(crtc->dc->hlcdc->sys_clk); 1832389fc13SBoris Brezillon 1842389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PIXEL_CLK); 1852389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1862389fc13SBoris Brezillon !(status & ATMEL_HLCDC_PIXEL_CLK)) 1872389fc13SBoris Brezillon cpu_relax(); 1882389fc13SBoris Brezillon 1892389fc13SBoris Brezillon 1902389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_SYNC); 1912389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1922389fc13SBoris Brezillon !(status & ATMEL_HLCDC_SYNC)) 1932389fc13SBoris Brezillon cpu_relax(); 1942389fc13SBoris Brezillon 1952389fc13SBoris Brezillon regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_DISP); 1962389fc13SBoris Brezillon while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) && 1972389fc13SBoris Brezillon !(status & ATMEL_HLCDC_DISP)) 1982389fc13SBoris Brezillon cpu_relax(); 1992389fc13SBoris Brezillon 2002389fc13SBoris Brezillon pm_runtime_put_sync(dev->dev); 2012389fc13SBoris Brezillon 2022389fc13SBoris Brezillon drm_crtc_vblank_on(c); 2032389fc13SBoris Brezillon 2042389fc13SBoris Brezillon crtc->enabled = true; 2052389fc13SBoris Brezillon } 2062389fc13SBoris Brezillon 2072389fc13SBoris Brezillon static int atmel_hlcdc_crtc_atomic_check(struct drm_crtc *c, 2082389fc13SBoris Brezillon struct drm_crtc_state *s) 2092389fc13SBoris Brezillon { 2102389fc13SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 2112389fc13SBoris Brezillon 2122389fc13SBoris Brezillon if (atmel_hlcdc_dc_mode_valid(crtc->dc, &s->adjusted_mode) != MODE_OK) 2132389fc13SBoris Brezillon return -EINVAL; 2142389fc13SBoris Brezillon 215*5957017dSBoris Brezillon return atmel_hlcdc_plane_prepare_disc_area(s); 2162389fc13SBoris Brezillon } 2172389fc13SBoris Brezillon 2182389fc13SBoris Brezillon static void atmel_hlcdc_crtc_atomic_begin(struct drm_crtc *c) 2192389fc13SBoris Brezillon { 2202389fc13SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 2212389fc13SBoris Brezillon 2222389fc13SBoris Brezillon if (c->state->event) { 2232389fc13SBoris Brezillon c->state->event->pipe = drm_crtc_index(c); 2242389fc13SBoris Brezillon 2252389fc13SBoris Brezillon WARN_ON(drm_crtc_vblank_get(c) != 0); 2262389fc13SBoris Brezillon 2272389fc13SBoris Brezillon crtc->event = c->state->event; 2282389fc13SBoris Brezillon c->state->event = NULL; 2292389fc13SBoris Brezillon } 2302389fc13SBoris Brezillon } 2312389fc13SBoris Brezillon 2322389fc13SBoris Brezillon static void atmel_hlcdc_crtc_atomic_flush(struct drm_crtc *crtc) 2332389fc13SBoris Brezillon { 2342389fc13SBoris Brezillon /* TODO: write common plane control register if available */ 2351a396789SBoris Brezillon } 2361a396789SBoris Brezillon 2371a396789SBoris Brezillon static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs = { 2381a396789SBoris Brezillon .mode_fixup = atmel_hlcdc_crtc_mode_fixup, 2392389fc13SBoris Brezillon .mode_set = drm_helper_crtc_mode_set, 2402389fc13SBoris Brezillon .mode_set_nofb = atmel_hlcdc_crtc_mode_set_nofb, 2412389fc13SBoris Brezillon .mode_set_base = drm_helper_crtc_mode_set_base, 2421a396789SBoris Brezillon .disable = atmel_hlcdc_crtc_disable, 2432389fc13SBoris Brezillon .enable = atmel_hlcdc_crtc_enable, 2442389fc13SBoris Brezillon .atomic_check = atmel_hlcdc_crtc_atomic_check, 2452389fc13SBoris Brezillon .atomic_begin = atmel_hlcdc_crtc_atomic_begin, 2462389fc13SBoris Brezillon .atomic_flush = atmel_hlcdc_crtc_atomic_flush, 2471a396789SBoris Brezillon }; 2481a396789SBoris Brezillon 2491a396789SBoris Brezillon static void atmel_hlcdc_crtc_destroy(struct drm_crtc *c) 2501a396789SBoris Brezillon { 2511a396789SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 2521a396789SBoris Brezillon 2531a396789SBoris Brezillon drm_crtc_cleanup(c); 2541a396789SBoris Brezillon kfree(crtc); 2551a396789SBoris Brezillon } 2561a396789SBoris Brezillon 2571a396789SBoris Brezillon void atmel_hlcdc_crtc_cancel_page_flip(struct drm_crtc *c, 2581a396789SBoris Brezillon struct drm_file *file) 2591a396789SBoris Brezillon { 2601a396789SBoris Brezillon struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c); 2611a396789SBoris Brezillon struct drm_pending_vblank_event *event; 2621a396789SBoris Brezillon struct drm_device *dev = c->dev; 2631a396789SBoris Brezillon unsigned long flags; 2641a396789SBoris Brezillon 2651a396789SBoris Brezillon spin_lock_irqsave(&dev->event_lock, flags); 2661a396789SBoris Brezillon event = crtc->event; 2671a396789SBoris Brezillon if (event && event->base.file_priv == file) { 2681a396789SBoris Brezillon event->base.destroy(&event->base); 2691a396789SBoris Brezillon drm_vblank_put(dev, crtc->id); 2701a396789SBoris Brezillon crtc->event = NULL; 2711a396789SBoris Brezillon } 2721a396789SBoris Brezillon spin_unlock_irqrestore(&dev->event_lock, flags); 2731a396789SBoris Brezillon } 2741a396789SBoris Brezillon 2751a396789SBoris Brezillon static void atmel_hlcdc_crtc_finish_page_flip(struct atmel_hlcdc_crtc *crtc) 2761a396789SBoris Brezillon { 2771a396789SBoris Brezillon struct drm_device *dev = crtc->base.dev; 2781a396789SBoris Brezillon unsigned long flags; 2791a396789SBoris Brezillon 2801a396789SBoris Brezillon spin_lock_irqsave(&dev->event_lock, flags); 2811a396789SBoris Brezillon if (crtc->event) { 2821a396789SBoris Brezillon drm_send_vblank_event(dev, crtc->id, crtc->event); 2831a396789SBoris Brezillon drm_vblank_put(dev, crtc->id); 2841a396789SBoris Brezillon crtc->event = NULL; 2851a396789SBoris Brezillon } 2861a396789SBoris Brezillon spin_unlock_irqrestore(&dev->event_lock, flags); 2871a396789SBoris Brezillon } 2881a396789SBoris Brezillon 2891a396789SBoris Brezillon void atmel_hlcdc_crtc_irq(struct drm_crtc *c) 2901a396789SBoris Brezillon { 2911a396789SBoris Brezillon drm_handle_vblank(c->dev, 0); 2921a396789SBoris Brezillon atmel_hlcdc_crtc_finish_page_flip(drm_crtc_to_atmel_hlcdc_crtc(c)); 2931a396789SBoris Brezillon } 2941a396789SBoris Brezillon 2951a396789SBoris Brezillon static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs = { 2962389fc13SBoris Brezillon .page_flip = drm_atomic_helper_page_flip, 2972389fc13SBoris Brezillon .set_config = drm_atomic_helper_set_config, 2981a396789SBoris Brezillon .destroy = atmel_hlcdc_crtc_destroy, 2992389fc13SBoris Brezillon .reset = drm_atomic_helper_crtc_reset, 3002389fc13SBoris Brezillon .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 3012389fc13SBoris Brezillon .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 3021a396789SBoris Brezillon }; 3031a396789SBoris Brezillon 3041a396789SBoris Brezillon int atmel_hlcdc_crtc_create(struct drm_device *dev) 3051a396789SBoris Brezillon { 3061a396789SBoris Brezillon struct atmel_hlcdc_dc *dc = dev->dev_private; 3071a396789SBoris Brezillon struct atmel_hlcdc_planes *planes = dc->planes; 3081a396789SBoris Brezillon struct atmel_hlcdc_crtc *crtc; 3091a396789SBoris Brezillon int ret; 3101a396789SBoris Brezillon int i; 3111a396789SBoris Brezillon 3121a396789SBoris Brezillon crtc = kzalloc(sizeof(*crtc), GFP_KERNEL); 3131a396789SBoris Brezillon if (!crtc) 3141a396789SBoris Brezillon return -ENOMEM; 3151a396789SBoris Brezillon 3161a396789SBoris Brezillon crtc->dc = dc; 3171a396789SBoris Brezillon 3181a396789SBoris Brezillon ret = drm_crtc_init_with_planes(dev, &crtc->base, 3191a396789SBoris Brezillon &planes->primary->base, 3201a396789SBoris Brezillon planes->cursor ? &planes->cursor->base : NULL, 3211a396789SBoris Brezillon &atmel_hlcdc_crtc_funcs); 3221a396789SBoris Brezillon if (ret < 0) 3231a396789SBoris Brezillon goto fail; 3241a396789SBoris Brezillon 3251a396789SBoris Brezillon crtc->id = drm_crtc_index(&crtc->base); 3261a396789SBoris Brezillon 3271a396789SBoris Brezillon if (planes->cursor) 3281a396789SBoris Brezillon planes->cursor->base.possible_crtcs = 1 << crtc->id; 3291a396789SBoris Brezillon 3301a396789SBoris Brezillon for (i = 0; i < planes->noverlays; i++) 3311a396789SBoris Brezillon planes->overlays[i]->base.possible_crtcs = 1 << crtc->id; 3321a396789SBoris Brezillon 3331a396789SBoris Brezillon drm_crtc_helper_add(&crtc->base, &lcdc_crtc_helper_funcs); 3341a396789SBoris Brezillon 3351a396789SBoris Brezillon dc->crtc = &crtc->base; 3361a396789SBoris Brezillon 3371a396789SBoris Brezillon return 0; 3381a396789SBoris Brezillon 3391a396789SBoris Brezillon fail: 3401a396789SBoris Brezillon atmel_hlcdc_crtc_destroy(&crtc->base); 3411a396789SBoris Brezillon return ret; 3421a396789SBoris Brezillon } 3431a396789SBoris Brezillon 344