Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2389fc13 authored by Boris Brezillon's avatar Boris Brezillon
Browse files

drm: atmel-hlcdc: Atomic mode-setting conversion



Convert the HLCDC driver to atomic mode-setting.

Signed-off-by: default avatarBoris Brezillon <boris.brezillon@free-electrons.com>
Tested-by: default avatarSylvain Rochet <sylvain.rochet@finsecur.com>
Acked-by: default avatarDaniel Vetter <daniel.vetter@ffwll.ch>
parent 45ee2dbc
Loading
Loading
Loading
Loading
+114 −177
Original line number Diff line number Diff line
@@ -37,14 +37,14 @@
 * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device
 * @event: pointer to the current page flip event
 * @id: CRTC id (returned by drm_crtc_index)
 * @dpms: DPMS mode
 * @enabled: CRTC state
 */
struct atmel_hlcdc_crtc {
	struct drm_crtc base;
	struct atmel_hlcdc_dc *dc;
	struct drm_pending_vblank_event *event;
	int id;
	int dpms;
	bool enabled;
};

static inline struct atmel_hlcdc_crtc *
@@ -53,86 +53,17 @@ drm_crtc_to_atmel_hlcdc_crtc(struct drm_crtc *crtc)
	return container_of(crtc, struct atmel_hlcdc_crtc, base);
}

static void atmel_hlcdc_crtc_dpms(struct drm_crtc *c, int mode)
{
	struct drm_device *dev = c->dev;
	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
	struct regmap *regmap = crtc->dc->hlcdc->regmap;
	unsigned int status;

	if (mode != DRM_MODE_DPMS_ON)
		mode = DRM_MODE_DPMS_OFF;

	if (crtc->dpms == mode)
		return;

	pm_runtime_get_sync(dev->dev);

	if (mode != DRM_MODE_DPMS_ON) {
		regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_DISP);
		while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
		       (status & ATMEL_HLCDC_DISP))
			cpu_relax();

		regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_SYNC);
		while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
		       (status & ATMEL_HLCDC_SYNC))
			cpu_relax();

		regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PIXEL_CLK);
		while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
		       (status & ATMEL_HLCDC_PIXEL_CLK))
			cpu_relax();

		clk_disable_unprepare(crtc->dc->hlcdc->sys_clk);

		pm_runtime_allow(dev->dev);
	} else {
		pm_runtime_forbid(dev->dev);

		clk_prepare_enable(crtc->dc->hlcdc->sys_clk);

		regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PIXEL_CLK);
		while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
		       !(status & ATMEL_HLCDC_PIXEL_CLK))
			cpu_relax();


		regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_SYNC);
		while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
		       !(status & ATMEL_HLCDC_SYNC))
			cpu_relax();

		regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_DISP);
		while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
		       !(status & ATMEL_HLCDC_DISP))
			cpu_relax();
	}

	pm_runtime_put_sync(dev->dev);

	crtc->dpms = mode;
}

static int atmel_hlcdc_crtc_mode_set(struct drm_crtc *c,
				     struct drm_display_mode *mode,
				     struct drm_display_mode *adj,
				     int x, int y,
				     struct drm_framebuffer *old_fb)
static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c)
{
	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
	struct regmap *regmap = crtc->dc->hlcdc->regmap;
	struct drm_plane *plane = c->primary;
	struct drm_framebuffer *fb;
	struct drm_display_mode *adj = &c->state->adjusted_mode;
	unsigned long mode_rate;
	struct videomode vm;
	unsigned long prate;
	unsigned int cfg;
	int div;

	if (atmel_hlcdc_dc_mode_valid(crtc->dc, adj) != MODE_OK)
		return -EINVAL;

	vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay;
	vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end;
	vm.vsync_len = adj->crtc_vsync_end - adj->crtc_vsync_start;
@@ -156,7 +87,7 @@ static int atmel_hlcdc_crtc_mode_set(struct drm_crtc *c,
	cfg = ATMEL_HLCDC_CLKPOL;

	prate = clk_get_rate(crtc->dc->hlcdc->sys_clk);
	mode_rate = mode->crtc_clock * 1000;
	mode_rate = adj->crtc_clock * 1000;
	if ((prate / 2) < mode_rate) {
		prate *= 2;
		cfg |= ATMEL_HLCDC_CLKSEL;
@@ -174,10 +105,10 @@ static int atmel_hlcdc_crtc_mode_set(struct drm_crtc *c,

	cfg = 0;

	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
	if (adj->flags & DRM_MODE_FLAG_NVSYNC)
		cfg |= ATMEL_HLCDC_VSPOL;

	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
	if (adj->flags & DRM_MODE_FLAG_NHSYNC)
		cfg |= ATMEL_HLCDC_HSPOL;

	regmap_update_bits(regmap, ATMEL_HLCDC_CFG(5),
@@ -187,77 +118,132 @@ static int atmel_hlcdc_crtc_mode_set(struct drm_crtc *c,
			   ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO |
			   ATMEL_HLCDC_GUARDTIME_MASK,
			   cfg);

	fb = plane->fb;
	plane->fb = old_fb;

	return atmel_hlcdc_plane_update_with_mode(plane, c, fb, 0, 0,
						  adj->hdisplay, adj->vdisplay,
						  x << 16, y << 16,
						  adj->hdisplay << 16,
						  adj->vdisplay << 16,
						  adj);
}

int atmel_hlcdc_crtc_mode_set_base(struct drm_crtc *c, int x, int y,
				   struct drm_framebuffer *old_fb)
static bool atmel_hlcdc_crtc_mode_fixup(struct drm_crtc *crtc,
					const struct drm_display_mode *mode,
					struct drm_display_mode *adjusted_mode)
{
	struct drm_plane *plane = c->primary;
	struct drm_framebuffer *fb = plane->fb;
	struct drm_display_mode *mode = &c->hwmode;

	plane->fb = old_fb;

	return plane->funcs->update_plane(plane, c, fb,
					  0, 0,
					  mode->hdisplay,
					  mode->vdisplay,
					  x << 16, y << 16,
					  mode->hdisplay << 16,
					  mode->vdisplay << 16);
	return true;
}

static void atmel_hlcdc_crtc_prepare(struct drm_crtc *crtc)
static void atmel_hlcdc_crtc_disable(struct drm_crtc *c)
{
	atmel_hlcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
	struct drm_device *dev = c->dev;
	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
	struct regmap *regmap = crtc->dc->hlcdc->regmap;
	unsigned int status;

	if (!crtc->enabled)
		return;

	drm_crtc_vblank_off(c);

	pm_runtime_get_sync(dev->dev);

	regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_DISP);
	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
	       (status & ATMEL_HLCDC_DISP))
		cpu_relax();

	regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_SYNC);
	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
	       (status & ATMEL_HLCDC_SYNC))
		cpu_relax();

	regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PIXEL_CLK);
	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
	       (status & ATMEL_HLCDC_PIXEL_CLK))
		cpu_relax();

	clk_disable_unprepare(crtc->dc->hlcdc->sys_clk);

	pm_runtime_allow(dev->dev);

	pm_runtime_put_sync(dev->dev);

	crtc->enabled = false;
}

static void atmel_hlcdc_crtc_commit(struct drm_crtc *crtc)
static void atmel_hlcdc_crtc_enable(struct drm_crtc *c)
{
	atmel_hlcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
	struct drm_device *dev = c->dev;
	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
	struct regmap *regmap = crtc->dc->hlcdc->regmap;
	unsigned int status;

	if (crtc->enabled)
		return;

	pm_runtime_get_sync(dev->dev);

	pm_runtime_forbid(dev->dev);

	clk_prepare_enable(crtc->dc->hlcdc->sys_clk);

	regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PIXEL_CLK);
	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
	       !(status & ATMEL_HLCDC_PIXEL_CLK))
		cpu_relax();


	regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_SYNC);
	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
	       !(status & ATMEL_HLCDC_SYNC))
		cpu_relax();

	regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_DISP);
	while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
	       !(status & ATMEL_HLCDC_DISP))
		cpu_relax();

	pm_runtime_put_sync(dev->dev);

	drm_crtc_vblank_on(c);

	crtc->enabled = true;
}

static bool atmel_hlcdc_crtc_mode_fixup(struct drm_crtc *crtc,
					const struct drm_display_mode *mode,
					struct drm_display_mode *adjusted_mode)
static int atmel_hlcdc_crtc_atomic_check(struct drm_crtc *c,
					 struct drm_crtc_state *s)
{
	return true;
	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);

	if (atmel_hlcdc_dc_mode_valid(crtc->dc, &s->adjusted_mode) != MODE_OK)
		return -EINVAL;

	return 0;
}

static void atmel_hlcdc_crtc_disable(struct drm_crtc *crtc)
static void atmel_hlcdc_crtc_atomic_begin(struct drm_crtc *c)
{
	struct drm_plane *plane;
	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);

	atmel_hlcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
	crtc->primary->funcs->disable_plane(crtc->primary);
	if (c->state->event) {
		c->state->event->pipe = drm_crtc_index(c);

	drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
		if (plane->crtc != crtc)
			continue;
		WARN_ON(drm_crtc_vblank_get(c) != 0);

		plane->funcs->disable_plane(crtc->primary);
		plane->crtc = NULL;
		crtc->event = c->state->event;
		c->state->event = NULL;
	}
}

static void atmel_hlcdc_crtc_atomic_flush(struct drm_crtc *crtc)
{
	/* TODO: write common plane control register if available */
}

static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs = {
	.mode_fixup = atmel_hlcdc_crtc_mode_fixup,
	.dpms = atmel_hlcdc_crtc_dpms,
	.mode_set = atmel_hlcdc_crtc_mode_set,
	.mode_set_base = atmel_hlcdc_crtc_mode_set_base,
	.prepare = atmel_hlcdc_crtc_prepare,
	.commit = atmel_hlcdc_crtc_commit,
	.mode_set = drm_helper_crtc_mode_set,
	.mode_set_nofb = atmel_hlcdc_crtc_mode_set_nofb,
	.mode_set_base = drm_helper_crtc_mode_set_base,
	.disable = atmel_hlcdc_crtc_disable,
	.enable = atmel_hlcdc_crtc_enable,
	.atomic_check = atmel_hlcdc_crtc_atomic_check,
	.atomic_begin = atmel_hlcdc_crtc_atomic_begin,
	.atomic_flush = atmel_hlcdc_crtc_atomic_flush,
};

static void atmel_hlcdc_crtc_destroy(struct drm_crtc *c)
@@ -306,61 +292,13 @@ void atmel_hlcdc_crtc_irq(struct drm_crtc *c)
	atmel_hlcdc_crtc_finish_page_flip(drm_crtc_to_atmel_hlcdc_crtc(c));
}

static int atmel_hlcdc_crtc_page_flip(struct drm_crtc *c,
				      struct drm_framebuffer *fb,
				      struct drm_pending_vblank_event *event,
				      uint32_t page_flip_flags)
{
	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
	struct atmel_hlcdc_plane_update_req req;
	struct drm_plane *plane = c->primary;
	struct drm_device *dev = c->dev;
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&dev->event_lock, flags);
	if (crtc->event)
		ret = -EBUSY;
	spin_unlock_irqrestore(&dev->event_lock, flags);

	if (ret)
		return ret;

	memset(&req, 0, sizeof(req));
	req.crtc_x = 0;
	req.crtc_y = 0;
	req.crtc_h = c->mode.crtc_vdisplay;
	req.crtc_w = c->mode.crtc_hdisplay;
	req.src_x = c->x << 16;
	req.src_y = c->y << 16;
	req.src_w = req.crtc_w << 16;
	req.src_h = req.crtc_h << 16;
	req.fb = fb;

	ret = atmel_hlcdc_plane_prepare_update_req(plane, &req, &c->hwmode);
	if (ret)
		return ret;

	if (event) {
		drm_vblank_get(c->dev, crtc->id);
		spin_lock_irqsave(&dev->event_lock, flags);
		crtc->event = event;
		spin_unlock_irqrestore(&dev->event_lock, flags);
	}

	ret = atmel_hlcdc_plane_apply_update_req(plane, &req);
	if (ret)
		crtc->event = NULL;
	else
		plane->fb = fb;

	return ret;
}

static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs = {
	.page_flip = atmel_hlcdc_crtc_page_flip,
	.set_config = drm_crtc_helper_set_config,
	.page_flip = drm_atomic_helper_page_flip,
	.set_config = drm_atomic_helper_set_config,
	.destroy = atmel_hlcdc_crtc_destroy,
	.reset = drm_atomic_helper_crtc_reset,
	.atomic_duplicate_state =  drm_atomic_helper_crtc_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
};

int atmel_hlcdc_crtc_create(struct drm_device *dev)
@@ -375,7 +313,6 @@ int atmel_hlcdc_crtc_create(struct drm_device *dev)
	if (!crtc)
		return -ENOMEM;

	crtc->dpms = DRM_MODE_DPMS_OFF;
	crtc->dc = dc;

	ret = drm_crtc_init_with_planes(dev, &crtc->base,
+4 −0
Original line number Diff line number Diff line
@@ -222,6 +222,8 @@ static void atmel_hlcdc_fb_output_poll_changed(struct drm_device *dev)
static const struct drm_mode_config_funcs mode_config_funcs = {
	.fb_create = atmel_hlcdc_fb_create,
	.output_poll_changed = atmel_hlcdc_fb_output_poll_changed,
	.atomic_check = drm_atomic_helper_check,
	.atomic_commit = drm_atomic_helper_commit,
};

static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
@@ -319,6 +321,8 @@ static int atmel_hlcdc_dc_load(struct drm_device *dev)
		goto err_periph_clk_disable;
	}

	drm_mode_config_reset(dev);

	ret = drm_vblank_init(dev, 1);
	if (ret < 0) {
		dev_err(dev->dev, "failed to initialize vblank\n");
+3 −56
Original line number Diff line number Diff line
@@ -26,11 +26,14 @@
#include <linux/irqdomain.h>
#include <linux/pwm.h>

#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_fb_cma_helper.h>
#include <drm/drm_gem_cma_helper.h>
#include <drm/drm_panel.h>
#include <drm/drm_plane_helper.h>
#include <drm/drmP.h>

#include "atmel_hlcdc_layer.h"
@@ -69,7 +72,6 @@ struct atmel_hlcdc_dc_desc {
 */
struct atmel_hlcdc_plane_properties {
	struct drm_property *alpha;
	struct drm_property *rotation;
};

/**
@@ -84,7 +86,6 @@ struct atmel_hlcdc_plane {
	struct drm_plane base;
	struct atmel_hlcdc_layer layer;
	struct atmel_hlcdc_plane_properties *properties;
	unsigned int rotation;
};

static inline struct atmel_hlcdc_plane *
@@ -99,43 +100,6 @@ atmel_hlcdc_layer_to_plane(struct atmel_hlcdc_layer *l)
	return container_of(l, struct atmel_hlcdc_plane, layer);
}

/**
 * Atmel HLCDC Plane update request structure.
 *
 * @crtc_x: x position of the plane relative to the CRTC
 * @crtc_y: y position of the plane relative to the CRTC
 * @crtc_w: visible width of the plane
 * @crtc_h: visible height of the plane
 * @src_x: x buffer position
 * @src_y: y buffer position
 * @src_w: buffer width
 * @src_h: buffer height
 * @fb: framebuffer object object
 * @bpp: bytes per pixel deduced from pixel_format
 * @offsets: offsets to apply to the GEM buffers
 * @xstride: value to add to the pixel pointer between each line
 * @pstride: value to add to the pixel pointer between each pixel
 * @nplanes: number of planes (deduced from pixel_format)
 */
struct atmel_hlcdc_plane_update_req {
	int crtc_x;
	int crtc_y;
	unsigned int crtc_w;
	unsigned int crtc_h;
	uint32_t src_x;
	uint32_t src_y;
	uint32_t src_w;
	uint32_t src_h;
	struct drm_framebuffer *fb;

	/* These fields are private and should not be touched */
	int bpp[ATMEL_HLCDC_MAX_PLANES];
	unsigned int offsets[ATMEL_HLCDC_MAX_PLANES];
	int xstride[ATMEL_HLCDC_MAX_PLANES];
	int pstride[ATMEL_HLCDC_MAX_PLANES];
	int nplanes;
};

/**
 * Atmel HLCDC Planes.
 *
@@ -184,23 +148,6 @@ int atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
struct atmel_hlcdc_planes *
atmel_hlcdc_create_planes(struct drm_device *dev);

int atmel_hlcdc_plane_prepare_update_req(struct drm_plane *p,
				struct atmel_hlcdc_plane_update_req *req,
				const struct drm_display_mode *mode);

int atmel_hlcdc_plane_apply_update_req(struct drm_plane *p,
				struct atmel_hlcdc_plane_update_req *req);

int atmel_hlcdc_plane_update_with_mode(struct drm_plane *p,
				       struct drm_crtc *crtc,
				       struct drm_framebuffer *fb,
				       int crtc_x, int crtc_y,
				       unsigned int crtc_w,
				       unsigned int crtc_h,
				       uint32_t src_x, uint32_t src_y,
				       uint32_t src_w, uint32_t src_h,
				       const struct drm_display_mode *mode);

void atmel_hlcdc_crtc_irq(struct drm_crtc *c);

void atmel_hlcdc_crtc_cancel_page_flip(struct drm_crtc *crtc,
+1 −3
Original line number Diff line number Diff line
@@ -298,7 +298,7 @@ void atmel_hlcdc_layer_irq(struct atmel_hlcdc_layer *layer)
	spin_unlock_irqrestore(&layer->lock, flags);
}

int atmel_hlcdc_layer_disable(struct atmel_hlcdc_layer *layer)
void atmel_hlcdc_layer_disable(struct atmel_hlcdc_layer *layer)
{
	struct atmel_hlcdc_layer_dma_channel *dma = &layer->dma;
	struct atmel_hlcdc_layer_update *upd = &layer->update;
@@ -340,8 +340,6 @@ int atmel_hlcdc_layer_disable(struct atmel_hlcdc_layer *layer)
	dma->status = ATMEL_HLCDC_LAYER_DISABLED;

	spin_unlock_irqrestore(&layer->lock, flags);

	return 0;
}

int atmel_hlcdc_layer_update_start(struct atmel_hlcdc_layer *layer)
+2 −1
Original line number Diff line number Diff line
@@ -120,6 +120,7 @@
#define ATMEL_HLCDC_LAYER_DISCEN		BIT(11)
#define ATMEL_HLCDC_LAYER_GA_SHIFT		16
#define ATMEL_HLCDC_LAYER_GA_MASK		GENMASK(23, ATMEL_HLCDC_LAYER_GA_SHIFT)
#define ATMEL_HLCDC_LAYER_GA(x)			((x) << ATMEL_HLCDC_LAYER_GA_SHIFT)

#define ATMEL_HLCDC_LAYER_CSC_CFG(p, o)		ATMEL_HLCDC_LAYER_CFG(p, (p)->desc->layout.csc + o)

@@ -376,7 +377,7 @@ int atmel_hlcdc_layer_init(struct drm_device *dev,
void atmel_hlcdc_layer_cleanup(struct drm_device *dev,
			       struct atmel_hlcdc_layer *layer);

int atmel_hlcdc_layer_disable(struct atmel_hlcdc_layer *layer);
void atmel_hlcdc_layer_disable(struct atmel_hlcdc_layer *layer);

int atmel_hlcdc_layer_update_start(struct atmel_hlcdc_layer *layer);

Loading