xref: /linux/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c (revision b6e075c3cb6e57d487f6ca1be688e88eb8fc6a79)
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 /**
35aca63b76SBoris Brezillon  * Atmel HLCDC CRTC state structure
36aca63b76SBoris Brezillon  *
37aca63b76SBoris Brezillon  * @base: base CRTC state
38aca63b76SBoris Brezillon  * @output_mode: RGBXXX output mode
39aca63b76SBoris Brezillon  */
40aca63b76SBoris Brezillon struct atmel_hlcdc_crtc_state {
41aca63b76SBoris Brezillon 	struct drm_crtc_state base;
42aca63b76SBoris Brezillon 	unsigned int output_mode;
43aca63b76SBoris Brezillon };
44aca63b76SBoris Brezillon 
45aca63b76SBoris Brezillon static inline struct atmel_hlcdc_crtc_state *
46aca63b76SBoris Brezillon drm_crtc_state_to_atmel_hlcdc_crtc_state(struct drm_crtc_state *state)
47aca63b76SBoris Brezillon {
48aca63b76SBoris Brezillon 	return container_of(state, struct atmel_hlcdc_crtc_state, base);
49aca63b76SBoris Brezillon }
50aca63b76SBoris Brezillon 
51aca63b76SBoris 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)
581a396789SBoris Brezillon  */
591a396789SBoris Brezillon struct atmel_hlcdc_crtc {
601a396789SBoris Brezillon 	struct drm_crtc base;
611a396789SBoris Brezillon 	struct atmel_hlcdc_dc *dc;
621a396789SBoris Brezillon 	struct drm_pending_vblank_event *event;
631a396789SBoris Brezillon 	int id;
641a396789SBoris Brezillon };
651a396789SBoris Brezillon 
661a396789SBoris Brezillon static inline struct atmel_hlcdc_crtc *
671a396789SBoris Brezillon drm_crtc_to_atmel_hlcdc_crtc(struct drm_crtc *crtc)
681a396789SBoris Brezillon {
691a396789SBoris Brezillon 	return container_of(crtc, struct atmel_hlcdc_crtc, base);
701a396789SBoris Brezillon }
711a396789SBoris Brezillon 
722389fc13SBoris Brezillon static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c)
731a396789SBoris Brezillon {
741a396789SBoris Brezillon 	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
751a396789SBoris Brezillon 	struct regmap *regmap = crtc->dc->hlcdc->regmap;
762389fc13SBoris Brezillon 	struct drm_display_mode *adj = &c->state->adjusted_mode;
77aca63b76SBoris Brezillon 	struct atmel_hlcdc_crtc_state *state;
781a396789SBoris Brezillon 	unsigned long mode_rate;
791a396789SBoris Brezillon 	struct videomode vm;
801a396789SBoris Brezillon 	unsigned long prate;
811a396789SBoris Brezillon 	unsigned int cfg;
821a396789SBoris Brezillon 	int div;
831a396789SBoris Brezillon 
841a396789SBoris Brezillon 	vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay;
851a396789SBoris Brezillon 	vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end;
861a396789SBoris Brezillon 	vm.vsync_len = adj->crtc_vsync_end - adj->crtc_vsync_start;
871a396789SBoris Brezillon 	vm.hfront_porch = adj->crtc_hsync_start - adj->crtc_hdisplay;
881a396789SBoris Brezillon 	vm.hback_porch = adj->crtc_htotal - adj->crtc_hsync_end;
891a396789SBoris Brezillon 	vm.hsync_len = adj->crtc_hsync_end - adj->crtc_hsync_start;
901a396789SBoris Brezillon 
911a396789SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_CFG(1),
921a396789SBoris Brezillon 		     (vm.hsync_len - 1) | ((vm.vsync_len - 1) << 16));
931a396789SBoris Brezillon 
941a396789SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_CFG(2),
951a396789SBoris Brezillon 		     (vm.vfront_porch - 1) | (vm.vback_porch << 16));
961a396789SBoris Brezillon 
971a396789SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_CFG(3),
981a396789SBoris Brezillon 		     (vm.hfront_porch - 1) | ((vm.hback_porch - 1) << 16));
991a396789SBoris Brezillon 
1001a396789SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_CFG(4),
1011a396789SBoris Brezillon 		     (adj->crtc_hdisplay - 1) |
1021a396789SBoris Brezillon 		     ((adj->crtc_vdisplay - 1) << 16));
1031a396789SBoris Brezillon 
104319711f9SPeter Rosin 	cfg = ATMEL_HLCDC_CLKSEL;
1051a396789SBoris Brezillon 
106319711f9SPeter Rosin 	prate = 2 * clk_get_rate(crtc->dc->hlcdc->sys_clk);
1072389fc13SBoris Brezillon 	mode_rate = adj->crtc_clock * 1000;
1081a396789SBoris Brezillon 
1091a396789SBoris Brezillon 	div = DIV_ROUND_UP(prate, mode_rate);
110319711f9SPeter Rosin 	if (div < 2) {
1111a396789SBoris Brezillon 		div = 2;
112319711f9SPeter Rosin 	} else if (ATMEL_HLCDC_CLKDIV(div) & ~ATMEL_HLCDC_CLKDIV_MASK) {
113319711f9SPeter Rosin 		/* The divider ended up too big, try a lower base rate. */
114319711f9SPeter Rosin 		cfg &= ~ATMEL_HLCDC_CLKSEL;
115319711f9SPeter Rosin 		prate /= 2;
116319711f9SPeter Rosin 		div = DIV_ROUND_UP(prate, mode_rate);
117319711f9SPeter Rosin 		if (ATMEL_HLCDC_CLKDIV(div) & ~ATMEL_HLCDC_CLKDIV_MASK)
118319711f9SPeter Rosin 			div = ATMEL_HLCDC_CLKDIV_MASK;
1199946a3a9SPeter Rosin 	} else {
1209946a3a9SPeter Rosin 		int div_low = prate / mode_rate;
1219946a3a9SPeter Rosin 
1229946a3a9SPeter Rosin 		if (div_low >= 2 &&
1239946a3a9SPeter Rosin 		    ((prate / div_low - mode_rate) <
1249946a3a9SPeter Rosin 		     10 * (mode_rate - prate / div)))
1259946a3a9SPeter Rosin 			/*
1269946a3a9SPeter Rosin 			 * At least 10 times better when using a higher
1279946a3a9SPeter Rosin 			 * frequency than requested, instead of a lower.
1289946a3a9SPeter Rosin 			 * So, go with that.
1299946a3a9SPeter Rosin 			 */
1309946a3a9SPeter Rosin 			div = div_low;
131319711f9SPeter Rosin 	}
1321a396789SBoris Brezillon 
1331a396789SBoris Brezillon 	cfg |= ATMEL_HLCDC_CLKDIV(div);
1341a396789SBoris Brezillon 
1351a396789SBoris Brezillon 	regmap_update_bits(regmap, ATMEL_HLCDC_CFG(0),
1361a396789SBoris Brezillon 			   ATMEL_HLCDC_CLKSEL | ATMEL_HLCDC_CLKDIV_MASK |
1371a396789SBoris Brezillon 			   ATMEL_HLCDC_CLKPOL, cfg);
1381a396789SBoris Brezillon 
1391a396789SBoris Brezillon 	cfg = 0;
1401a396789SBoris Brezillon 
1412389fc13SBoris Brezillon 	if (adj->flags & DRM_MODE_FLAG_NVSYNC)
1421a396789SBoris Brezillon 		cfg |= ATMEL_HLCDC_VSPOL;
1431a396789SBoris Brezillon 
1442389fc13SBoris Brezillon 	if (adj->flags & DRM_MODE_FLAG_NHSYNC)
1451a396789SBoris Brezillon 		cfg |= ATMEL_HLCDC_HSPOL;
1461a396789SBoris Brezillon 
147aca63b76SBoris Brezillon 	state = drm_crtc_state_to_atmel_hlcdc_crtc_state(c->state);
148aca63b76SBoris Brezillon 	cfg |= state->output_mode << 8;
149aca63b76SBoris Brezillon 
1501a396789SBoris Brezillon 	regmap_update_bits(regmap, ATMEL_HLCDC_CFG(5),
1511a396789SBoris Brezillon 			   ATMEL_HLCDC_HSPOL | ATMEL_HLCDC_VSPOL |
1521a396789SBoris Brezillon 			   ATMEL_HLCDC_VSPDLYS | ATMEL_HLCDC_VSPDLYE |
1531a396789SBoris Brezillon 			   ATMEL_HLCDC_DISPPOL | ATMEL_HLCDC_DISPDLY |
1541a396789SBoris Brezillon 			   ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO |
155aca63b76SBoris Brezillon 			   ATMEL_HLCDC_GUARDTIME_MASK | ATMEL_HLCDC_MODE_MASK,
1561a396789SBoris Brezillon 			   cfg);
1571a396789SBoris Brezillon }
1581a396789SBoris Brezillon 
159a57bf53eSJose Abreu static enum drm_mode_status
160a57bf53eSJose Abreu atmel_hlcdc_crtc_mode_valid(struct drm_crtc *c,
161a57bf53eSJose Abreu 			    const struct drm_display_mode *mode)
1625ac44c8bSBoris Brezillon {
1635ac44c8bSBoris Brezillon 	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
1645ac44c8bSBoris Brezillon 
165a57bf53eSJose Abreu 	return atmel_hlcdc_dc_mode_valid(crtc->dc, mode);
1665ac44c8bSBoris Brezillon }
1675ac44c8bSBoris Brezillon 
16864581714SLaurent Pinchart static void atmel_hlcdc_crtc_atomic_disable(struct drm_crtc *c,
16964581714SLaurent Pinchart 					    struct drm_crtc_state *old_state)
1701a396789SBoris Brezillon {
1712389fc13SBoris Brezillon 	struct drm_device *dev = c->dev;
1722389fc13SBoris Brezillon 	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
1732389fc13SBoris Brezillon 	struct regmap *regmap = crtc->dc->hlcdc->regmap;
1742389fc13SBoris Brezillon 	unsigned int status;
1751a396789SBoris Brezillon 
1762389fc13SBoris Brezillon 	drm_crtc_vblank_off(c);
1771a396789SBoris Brezillon 
1782389fc13SBoris Brezillon 	pm_runtime_get_sync(dev->dev);
1792389fc13SBoris Brezillon 
1802389fc13SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_DISP);
1812389fc13SBoris Brezillon 	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
1822389fc13SBoris Brezillon 	       (status & ATMEL_HLCDC_DISP))
1832389fc13SBoris Brezillon 		cpu_relax();
1842389fc13SBoris Brezillon 
1852389fc13SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_SYNC);
1862389fc13SBoris Brezillon 	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
1872389fc13SBoris Brezillon 	       (status & ATMEL_HLCDC_SYNC))
1882389fc13SBoris Brezillon 		cpu_relax();
1892389fc13SBoris Brezillon 
1902389fc13SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PIXEL_CLK);
1912389fc13SBoris Brezillon 	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
1922389fc13SBoris Brezillon 	       (status & ATMEL_HLCDC_PIXEL_CLK))
1932389fc13SBoris Brezillon 		cpu_relax();
1942389fc13SBoris Brezillon 
1952389fc13SBoris Brezillon 	clk_disable_unprepare(crtc->dc->hlcdc->sys_clk);
19616e6004eSSylvain Rochet 	pinctrl_pm_select_sleep_state(dev->dev);
1972389fc13SBoris Brezillon 
1982389fc13SBoris Brezillon 	pm_runtime_allow(dev->dev);
1992389fc13SBoris Brezillon 
2002389fc13SBoris Brezillon 	pm_runtime_put_sync(dev->dev);
2011a396789SBoris Brezillon }
2022389fc13SBoris Brezillon 
2030b20a0f8SLaurent Pinchart static void atmel_hlcdc_crtc_atomic_enable(struct drm_crtc *c,
2040b20a0f8SLaurent Pinchart 					   struct drm_crtc_state *old_state)
2052389fc13SBoris Brezillon {
2062389fc13SBoris Brezillon 	struct drm_device *dev = c->dev;
2072389fc13SBoris Brezillon 	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
2082389fc13SBoris Brezillon 	struct regmap *regmap = crtc->dc->hlcdc->regmap;
2092389fc13SBoris Brezillon 	unsigned int status;
2102389fc13SBoris Brezillon 
2112389fc13SBoris Brezillon 	pm_runtime_get_sync(dev->dev);
2122389fc13SBoris Brezillon 
2132389fc13SBoris Brezillon 	pm_runtime_forbid(dev->dev);
2142389fc13SBoris Brezillon 
21516e6004eSSylvain Rochet 	pinctrl_pm_select_default_state(dev->dev);
2162389fc13SBoris Brezillon 	clk_prepare_enable(crtc->dc->hlcdc->sys_clk);
2172389fc13SBoris Brezillon 
2182389fc13SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PIXEL_CLK);
2192389fc13SBoris Brezillon 	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
2202389fc13SBoris Brezillon 	       !(status & ATMEL_HLCDC_PIXEL_CLK))
2212389fc13SBoris Brezillon 		cpu_relax();
2222389fc13SBoris Brezillon 
2232389fc13SBoris Brezillon 
2242389fc13SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_SYNC);
2252389fc13SBoris Brezillon 	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
2262389fc13SBoris Brezillon 	       !(status & ATMEL_HLCDC_SYNC))
2272389fc13SBoris Brezillon 		cpu_relax();
2282389fc13SBoris Brezillon 
2292389fc13SBoris Brezillon 	regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_DISP);
2302389fc13SBoris Brezillon 	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
2312389fc13SBoris Brezillon 	       !(status & ATMEL_HLCDC_DISP))
2322389fc13SBoris Brezillon 		cpu_relax();
2332389fc13SBoris Brezillon 
2342389fc13SBoris Brezillon 	pm_runtime_put_sync(dev->dev);
2352389fc13SBoris Brezillon 
2362389fc13SBoris Brezillon 	drm_crtc_vblank_on(c);
237f026eb6eSSylvain Rochet }
238f026eb6eSSylvain Rochet 
239aca63b76SBoris Brezillon #define ATMEL_HLCDC_RGB444_OUTPUT	BIT(0)
240aca63b76SBoris Brezillon #define ATMEL_HLCDC_RGB565_OUTPUT	BIT(1)
241aca63b76SBoris Brezillon #define ATMEL_HLCDC_RGB666_OUTPUT	BIT(2)
242aca63b76SBoris Brezillon #define ATMEL_HLCDC_RGB888_OUTPUT	BIT(3)
243aca63b76SBoris Brezillon #define ATMEL_HLCDC_OUTPUT_MODE_MASK	GENMASK(3, 0)
244aca63b76SBoris Brezillon 
245*b6e075c3SPeter Rosin static int atmel_hlcdc_connector_output_mode(struct drm_connector_state *state)
246aca63b76SBoris Brezillon {
247*b6e075c3SPeter Rosin 	struct drm_connector *connector = state->connector;
248aca63b76SBoris Brezillon 	struct drm_display_info *info = &connector->display_info;
249*b6e075c3SPeter Rosin 	struct drm_encoder *encoder;
250aca63b76SBoris Brezillon 	unsigned int supported_fmts = 0;
251aca63b76SBoris Brezillon 	int j;
252aca63b76SBoris Brezillon 
253*b6e075c3SPeter Rosin 	encoder = state->best_encoder;
254*b6e075c3SPeter Rosin 	if (!encoder)
255*b6e075c3SPeter Rosin 		encoder = connector->encoder;
256*b6e075c3SPeter Rosin 
257*b6e075c3SPeter Rosin 	switch (atmel_hlcdc_encoder_get_bus_fmt(encoder)) {
258*b6e075c3SPeter Rosin 	case 0:
259*b6e075c3SPeter Rosin 		break;
260*b6e075c3SPeter Rosin 	case MEDIA_BUS_FMT_RGB444_1X12:
261*b6e075c3SPeter Rosin 		return ATMEL_HLCDC_RGB444_OUTPUT;
262*b6e075c3SPeter Rosin 	case MEDIA_BUS_FMT_RGB565_1X16:
263*b6e075c3SPeter Rosin 		return ATMEL_HLCDC_RGB565_OUTPUT;
264*b6e075c3SPeter Rosin 	case MEDIA_BUS_FMT_RGB666_1X18:
265*b6e075c3SPeter Rosin 		return ATMEL_HLCDC_RGB666_OUTPUT;
266*b6e075c3SPeter Rosin 	case MEDIA_BUS_FMT_RGB888_1X24:
267*b6e075c3SPeter Rosin 		return ATMEL_HLCDC_RGB888_OUTPUT;
268*b6e075c3SPeter Rosin 	default:
269*b6e075c3SPeter Rosin 		return -EINVAL;
270*b6e075c3SPeter Rosin 	}
271aca63b76SBoris Brezillon 
272aca63b76SBoris Brezillon 	for (j = 0; j < info->num_bus_formats; j++) {
273aca63b76SBoris Brezillon 		switch (info->bus_formats[j]) {
274aca63b76SBoris Brezillon 		case MEDIA_BUS_FMT_RGB444_1X12:
275aca63b76SBoris Brezillon 			supported_fmts |= ATMEL_HLCDC_RGB444_OUTPUT;
276aca63b76SBoris Brezillon 			break;
277aca63b76SBoris Brezillon 		case MEDIA_BUS_FMT_RGB565_1X16:
278aca63b76SBoris Brezillon 			supported_fmts |= ATMEL_HLCDC_RGB565_OUTPUT;
279aca63b76SBoris Brezillon 			break;
280aca63b76SBoris Brezillon 		case MEDIA_BUS_FMT_RGB666_1X18:
281aca63b76SBoris Brezillon 			supported_fmts |= ATMEL_HLCDC_RGB666_OUTPUT;
282aca63b76SBoris Brezillon 			break;
283aca63b76SBoris Brezillon 		case MEDIA_BUS_FMT_RGB888_1X24:
284aca63b76SBoris Brezillon 			supported_fmts |= ATMEL_HLCDC_RGB888_OUTPUT;
285aca63b76SBoris Brezillon 			break;
286aca63b76SBoris Brezillon 		default:
287aca63b76SBoris Brezillon 			break;
288aca63b76SBoris Brezillon 		}
289aca63b76SBoris Brezillon 	}
290aca63b76SBoris Brezillon 
291*b6e075c3SPeter Rosin 	return supported_fmts;
292*b6e075c3SPeter Rosin }
293*b6e075c3SPeter Rosin 
294*b6e075c3SPeter Rosin static int atmel_hlcdc_crtc_select_output_mode(struct drm_crtc_state *state)
295*b6e075c3SPeter Rosin {
296*b6e075c3SPeter Rosin 	unsigned int output_fmts = ATMEL_HLCDC_OUTPUT_MODE_MASK;
297*b6e075c3SPeter Rosin 	struct atmel_hlcdc_crtc_state *hstate;
298*b6e075c3SPeter Rosin 	struct drm_connector_state *cstate;
299*b6e075c3SPeter Rosin 	struct drm_connector *connector;
300*b6e075c3SPeter Rosin 	struct atmel_hlcdc_crtc *crtc;
301*b6e075c3SPeter Rosin 	int i;
302*b6e075c3SPeter Rosin 
303*b6e075c3SPeter Rosin 	crtc = drm_crtc_to_atmel_hlcdc_crtc(state->crtc);
304*b6e075c3SPeter Rosin 
305*b6e075c3SPeter Rosin 	for_each_new_connector_in_state(state->state, connector, cstate, i) {
306*b6e075c3SPeter Rosin 		unsigned int supported_fmts = 0;
307*b6e075c3SPeter Rosin 
308*b6e075c3SPeter Rosin 		if (!cstate->crtc)
309*b6e075c3SPeter Rosin 			continue;
310*b6e075c3SPeter Rosin 
311*b6e075c3SPeter Rosin 		supported_fmts = atmel_hlcdc_connector_output_mode(cstate);
312*b6e075c3SPeter Rosin 
313aca63b76SBoris Brezillon 		if (crtc->dc->desc->conflicting_output_formats)
314aca63b76SBoris Brezillon 			output_fmts &= supported_fmts;
315aca63b76SBoris Brezillon 		else
316aca63b76SBoris Brezillon 			output_fmts |= supported_fmts;
317aca63b76SBoris Brezillon 	}
318aca63b76SBoris Brezillon 
319aca63b76SBoris Brezillon 	if (!output_fmts)
320aca63b76SBoris Brezillon 		return -EINVAL;
321aca63b76SBoris Brezillon 
322aca63b76SBoris Brezillon 	hstate = drm_crtc_state_to_atmel_hlcdc_crtc_state(state);
323aca63b76SBoris Brezillon 	hstate->output_mode = fls(output_fmts) - 1;
324aca63b76SBoris Brezillon 
325aca63b76SBoris Brezillon 	return 0;
326aca63b76SBoris Brezillon }
327aca63b76SBoris Brezillon 
3282389fc13SBoris Brezillon static int atmel_hlcdc_crtc_atomic_check(struct drm_crtc *c,
3292389fc13SBoris Brezillon 					 struct drm_crtc_state *s)
3302389fc13SBoris Brezillon {
331aca63b76SBoris Brezillon 	int ret;
3322389fc13SBoris Brezillon 
333aca63b76SBoris Brezillon 	ret = atmel_hlcdc_crtc_select_output_mode(s);
334aca63b76SBoris Brezillon 	if (ret)
335aca63b76SBoris Brezillon 		return ret;
336aca63b76SBoris Brezillon 
337ebab87abSBoris Brezillon 	ret = atmel_hlcdc_plane_prepare_disc_area(s);
338ebab87abSBoris Brezillon 	if (ret)
339ebab87abSBoris Brezillon 		return ret;
340ebab87abSBoris Brezillon 
341ebab87abSBoris Brezillon 	return atmel_hlcdc_plane_prepare_ahb_routing(s);
3422389fc13SBoris Brezillon }
3432389fc13SBoris Brezillon 
344613d2b27SMaarten Lankhorst static void atmel_hlcdc_crtc_atomic_begin(struct drm_crtc *c,
345613d2b27SMaarten Lankhorst 					  struct drm_crtc_state *old_s)
3462389fc13SBoris Brezillon {
3472389fc13SBoris Brezillon 	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
3482389fc13SBoris Brezillon 
3492389fc13SBoris Brezillon 	if (c->state->event) {
3502389fc13SBoris Brezillon 		c->state->event->pipe = drm_crtc_index(c);
3512389fc13SBoris Brezillon 
3522389fc13SBoris Brezillon 		WARN_ON(drm_crtc_vblank_get(c) != 0);
3532389fc13SBoris Brezillon 
3542389fc13SBoris Brezillon 		crtc->event = c->state->event;
3552389fc13SBoris Brezillon 		c->state->event = NULL;
3562389fc13SBoris Brezillon 	}
3572389fc13SBoris Brezillon }
3582389fc13SBoris Brezillon 
359613d2b27SMaarten Lankhorst static void atmel_hlcdc_crtc_atomic_flush(struct drm_crtc *crtc,
360613d2b27SMaarten Lankhorst 					  struct drm_crtc_state *old_s)
3612389fc13SBoris Brezillon {
3622389fc13SBoris Brezillon 	/* TODO: write common plane control register if available */
3631a396789SBoris Brezillon }
3641a396789SBoris Brezillon 
3651a396789SBoris Brezillon static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs = {
366a57bf53eSJose Abreu 	.mode_valid = atmel_hlcdc_crtc_mode_valid,
3672389fc13SBoris Brezillon 	.mode_set = drm_helper_crtc_mode_set,
3682389fc13SBoris Brezillon 	.mode_set_nofb = atmel_hlcdc_crtc_mode_set_nofb,
3692389fc13SBoris Brezillon 	.mode_set_base = drm_helper_crtc_mode_set_base,
3702389fc13SBoris Brezillon 	.atomic_check = atmel_hlcdc_crtc_atomic_check,
3712389fc13SBoris Brezillon 	.atomic_begin = atmel_hlcdc_crtc_atomic_begin,
3722389fc13SBoris Brezillon 	.atomic_flush = atmel_hlcdc_crtc_atomic_flush,
3730b20a0f8SLaurent Pinchart 	.atomic_enable = atmel_hlcdc_crtc_atomic_enable,
37464581714SLaurent Pinchart 	.atomic_disable = atmel_hlcdc_crtc_atomic_disable,
3751a396789SBoris Brezillon };
3761a396789SBoris Brezillon 
3771a396789SBoris Brezillon static void atmel_hlcdc_crtc_destroy(struct drm_crtc *c)
3781a396789SBoris Brezillon {
3791a396789SBoris Brezillon 	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
3801a396789SBoris Brezillon 
3811a396789SBoris Brezillon 	drm_crtc_cleanup(c);
3821a396789SBoris Brezillon 	kfree(crtc);
3831a396789SBoris Brezillon }
3841a396789SBoris Brezillon 
3851a396789SBoris Brezillon static void atmel_hlcdc_crtc_finish_page_flip(struct atmel_hlcdc_crtc *crtc)
3861a396789SBoris Brezillon {
3871a396789SBoris Brezillon 	struct drm_device *dev = crtc->base.dev;
3881a396789SBoris Brezillon 	unsigned long flags;
3891a396789SBoris Brezillon 
3901a396789SBoris Brezillon 	spin_lock_irqsave(&dev->event_lock, flags);
3911a396789SBoris Brezillon 	if (crtc->event) {
39281767317SGustavo Padovan 		drm_crtc_send_vblank_event(&crtc->base, crtc->event);
39323a25ed3SGustavo Padovan 		drm_crtc_vblank_put(&crtc->base);
3941a396789SBoris Brezillon 		crtc->event = NULL;
3951a396789SBoris Brezillon 	}
3961a396789SBoris Brezillon 	spin_unlock_irqrestore(&dev->event_lock, flags);
3971a396789SBoris Brezillon }
3981a396789SBoris Brezillon 
3991a396789SBoris Brezillon void atmel_hlcdc_crtc_irq(struct drm_crtc *c)
4001a396789SBoris Brezillon {
401548ebe1eSGustavo Padovan 	drm_crtc_handle_vblank(c);
4021a396789SBoris Brezillon 	atmel_hlcdc_crtc_finish_page_flip(drm_crtc_to_atmel_hlcdc_crtc(c));
4031a396789SBoris Brezillon }
4041a396789SBoris Brezillon 
4051ba7db07SThierry Reding static void atmel_hlcdc_crtc_reset(struct drm_crtc *crtc)
406aca63b76SBoris Brezillon {
407aca63b76SBoris Brezillon 	struct atmel_hlcdc_crtc_state *state;
408aca63b76SBoris Brezillon 
409aca63b76SBoris Brezillon 	if (crtc->state) {
410c2e4c994SBoris Brezillon 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
411aca63b76SBoris Brezillon 		state = drm_crtc_state_to_atmel_hlcdc_crtc_state(crtc->state);
412aca63b76SBoris Brezillon 		kfree(state);
413c2e4c994SBoris Brezillon 		crtc->state = NULL;
414aca63b76SBoris Brezillon 	}
415aca63b76SBoris Brezillon 
416aca63b76SBoris Brezillon 	state = kzalloc(sizeof(*state), GFP_KERNEL);
417aca63b76SBoris Brezillon 	if (state) {
418aca63b76SBoris Brezillon 		crtc->state = &state->base;
419aca63b76SBoris Brezillon 		crtc->state->crtc = crtc;
420aca63b76SBoris Brezillon 	}
421aca63b76SBoris Brezillon }
422aca63b76SBoris Brezillon 
423aca63b76SBoris Brezillon static struct drm_crtc_state *
424aca63b76SBoris Brezillon atmel_hlcdc_crtc_duplicate_state(struct drm_crtc *crtc)
425aca63b76SBoris Brezillon {
426aca63b76SBoris Brezillon 	struct atmel_hlcdc_crtc_state *state, *cur;
427aca63b76SBoris Brezillon 
428aca63b76SBoris Brezillon 	if (WARN_ON(!crtc->state))
429aca63b76SBoris Brezillon 		return NULL;
430aca63b76SBoris Brezillon 
431aca63b76SBoris Brezillon 	state = kmalloc(sizeof(*state), GFP_KERNEL);
43258a2ab3aSDan Carpenter 	if (!state)
43358a2ab3aSDan Carpenter 		return NULL;
434aca63b76SBoris Brezillon 	__drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
435aca63b76SBoris Brezillon 
436aca63b76SBoris Brezillon 	cur = drm_crtc_state_to_atmel_hlcdc_crtc_state(crtc->state);
437aca63b76SBoris Brezillon 	state->output_mode = cur->output_mode;
438aca63b76SBoris Brezillon 
439aca63b76SBoris Brezillon 	return &state->base;
440aca63b76SBoris Brezillon }
441aca63b76SBoris Brezillon 
442aca63b76SBoris Brezillon static void atmel_hlcdc_crtc_destroy_state(struct drm_crtc *crtc,
443aca63b76SBoris Brezillon 					   struct drm_crtc_state *s)
444aca63b76SBoris Brezillon {
445aca63b76SBoris Brezillon 	struct atmel_hlcdc_crtc_state *state;
446aca63b76SBoris Brezillon 
447aca63b76SBoris Brezillon 	state = drm_crtc_state_to_atmel_hlcdc_crtc_state(s);
448ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(s);
449aca63b76SBoris Brezillon 	kfree(state);
450aca63b76SBoris Brezillon }
451aca63b76SBoris Brezillon 
45282308e27SShawn Guo static int atmel_hlcdc_crtc_enable_vblank(struct drm_crtc *c)
45382308e27SShawn Guo {
45482308e27SShawn Guo 	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
45582308e27SShawn Guo 	struct regmap *regmap = crtc->dc->hlcdc->regmap;
45682308e27SShawn Guo 
45782308e27SShawn Guo 	/* Enable SOF (Start Of Frame) interrupt for vblank counting */
45882308e27SShawn Guo 	regmap_write(regmap, ATMEL_HLCDC_IER, ATMEL_HLCDC_SOF);
45982308e27SShawn Guo 
46082308e27SShawn Guo 	return 0;
46182308e27SShawn Guo }
46282308e27SShawn Guo 
46382308e27SShawn Guo static void atmel_hlcdc_crtc_disable_vblank(struct drm_crtc *c)
46482308e27SShawn Guo {
46582308e27SShawn Guo 	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
46682308e27SShawn Guo 	struct regmap *regmap = crtc->dc->hlcdc->regmap;
46782308e27SShawn Guo 
46882308e27SShawn Guo 	regmap_write(regmap, ATMEL_HLCDC_IDR, ATMEL_HLCDC_SOF);
46982308e27SShawn Guo }
47082308e27SShawn Guo 
4711a396789SBoris Brezillon static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs = {
4722389fc13SBoris Brezillon 	.page_flip = drm_atomic_helper_page_flip,
4732389fc13SBoris Brezillon 	.set_config = drm_atomic_helper_set_config,
4741a396789SBoris Brezillon 	.destroy = atmel_hlcdc_crtc_destroy,
475aca63b76SBoris Brezillon 	.reset = atmel_hlcdc_crtc_reset,
476aca63b76SBoris Brezillon 	.atomic_duplicate_state =  atmel_hlcdc_crtc_duplicate_state,
477aca63b76SBoris Brezillon 	.atomic_destroy_state = atmel_hlcdc_crtc_destroy_state,
47882308e27SShawn Guo 	.enable_vblank = atmel_hlcdc_crtc_enable_vblank,
47982308e27SShawn Guo 	.disable_vblank = atmel_hlcdc_crtc_disable_vblank,
480364a7bf5SPeter Rosin 	.gamma_set = drm_atomic_helper_legacy_gamma_set,
4811a396789SBoris Brezillon };
4821a396789SBoris Brezillon 
4831a396789SBoris Brezillon int atmel_hlcdc_crtc_create(struct drm_device *dev)
4841a396789SBoris Brezillon {
4859a45d33cSBoris Brezillon 	struct atmel_hlcdc_plane *primary = NULL, *cursor = NULL;
4861a396789SBoris Brezillon 	struct atmel_hlcdc_dc *dc = dev->dev_private;
4871a396789SBoris Brezillon 	struct atmel_hlcdc_crtc *crtc;
4881a396789SBoris Brezillon 	int ret;
4891a396789SBoris Brezillon 	int i;
4901a396789SBoris Brezillon 
4911a396789SBoris Brezillon 	crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
4921a396789SBoris Brezillon 	if (!crtc)
4931a396789SBoris Brezillon 		return -ENOMEM;
4941a396789SBoris Brezillon 
4951a396789SBoris Brezillon 	crtc->dc = dc;
4961a396789SBoris Brezillon 
4979a45d33cSBoris Brezillon 	for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
4989a45d33cSBoris Brezillon 		if (!dc->layers[i])
4999a45d33cSBoris Brezillon 			continue;
5009a45d33cSBoris Brezillon 
5019a45d33cSBoris Brezillon 		switch (dc->layers[i]->desc->type) {
5029a45d33cSBoris Brezillon 		case ATMEL_HLCDC_BASE_LAYER:
5039a45d33cSBoris Brezillon 			primary = atmel_hlcdc_layer_to_plane(dc->layers[i]);
5049a45d33cSBoris Brezillon 			break;
5059a45d33cSBoris Brezillon 
5069a45d33cSBoris Brezillon 		case ATMEL_HLCDC_CURSOR_LAYER:
5079a45d33cSBoris Brezillon 			cursor = atmel_hlcdc_layer_to_plane(dc->layers[i]);
5089a45d33cSBoris Brezillon 			break;
5099a45d33cSBoris Brezillon 
5109a45d33cSBoris Brezillon 		default:
5119a45d33cSBoris Brezillon 			break;
5129a45d33cSBoris Brezillon 		}
5139a45d33cSBoris Brezillon 	}
5149a45d33cSBoris Brezillon 
5159a45d33cSBoris Brezillon 	ret = drm_crtc_init_with_planes(dev, &crtc->base, &primary->base,
5169a45d33cSBoris Brezillon 					&cursor->base, &atmel_hlcdc_crtc_funcs,
5179a45d33cSBoris Brezillon 					NULL);
5181a396789SBoris Brezillon 	if (ret < 0)
5191a396789SBoris Brezillon 		goto fail;
5201a396789SBoris Brezillon 
5211a396789SBoris Brezillon 	crtc->id = drm_crtc_index(&crtc->base);
5221a396789SBoris Brezillon 
5239a45d33cSBoris Brezillon 	for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
5249a45d33cSBoris Brezillon 		struct atmel_hlcdc_plane *overlay;
5251a396789SBoris Brezillon 
5269a45d33cSBoris Brezillon 		if (dc->layers[i] &&
5279a45d33cSBoris Brezillon 		    dc->layers[i]->desc->type == ATMEL_HLCDC_OVERLAY_LAYER) {
5289a45d33cSBoris Brezillon 			overlay = atmel_hlcdc_layer_to_plane(dc->layers[i]);
5299a45d33cSBoris Brezillon 			overlay->base.possible_crtcs = 1 << crtc->id;
5309a45d33cSBoris Brezillon 		}
5319a45d33cSBoris Brezillon 	}
5321a396789SBoris Brezillon 
5331a396789SBoris Brezillon 	drm_crtc_helper_add(&crtc->base, &lcdc_crtc_helper_funcs);
5348c4b4b0dSBoris Brezillon 	drm_crtc_vblank_reset(&crtc->base);
5351a396789SBoris Brezillon 
536364a7bf5SPeter Rosin 	drm_mode_crtc_set_gamma_size(&crtc->base, ATMEL_HLCDC_CLUT_SIZE);
537364a7bf5SPeter Rosin 	drm_crtc_enable_color_mgmt(&crtc->base, 0, false,
538364a7bf5SPeter Rosin 				   ATMEL_HLCDC_CLUT_SIZE);
539364a7bf5SPeter Rosin 
5401a396789SBoris Brezillon 	dc->crtc = &crtc->base;
5411a396789SBoris Brezillon 
5421a396789SBoris Brezillon 	return 0;
5431a396789SBoris Brezillon 
5441a396789SBoris Brezillon fail:
5451a396789SBoris Brezillon 	atmel_hlcdc_crtc_destroy(&crtc->base);
5461a396789SBoris Brezillon 	return ret;
5471a396789SBoris Brezillon }
548