Path: blob/master/drivers/gpu/drm/aspeed/aspeed_gfx_crtc.c
26482 views
// SPDX-License-Identifier: GPL-2.0+1// Copyright 2018 IBM Corporation23#include <linux/clk.h>4#include <linux/reset.h>5#include <linux/regmap.h>67#include <drm/drm_device.h>8#include <drm/drm_fb_dma_helper.h>9#include <drm/drm_fourcc.h>10#include <drm/drm_framebuffer.h>11#include <drm/drm_gem_atomic_helper.h>12#include <drm/drm_gem_dma_helper.h>13#include <drm/drm_panel.h>14#include <drm/drm_simple_kms_helper.h>15#include <drm/drm_vblank.h>1617#include "aspeed_gfx.h"1819static struct aspeed_gfx *20drm_pipe_to_aspeed_gfx(struct drm_simple_display_pipe *pipe)21{22return container_of(pipe, struct aspeed_gfx, pipe);23}2425static int aspeed_gfx_set_pixel_fmt(struct aspeed_gfx *priv, u32 *bpp)26{27struct drm_crtc *crtc = &priv->pipe.crtc;28struct drm_device *drm = crtc->dev;29const u32 format = crtc->primary->state->fb->format->format;30u32 ctrl1;3132ctrl1 = readl(priv->base + CRT_CTRL1);33ctrl1 &= ~CRT_CTRL_COLOR_MASK;3435switch (format) {36case DRM_FORMAT_RGB565:37dev_dbg(drm->dev, "Setting up RGB565 mode\n");38ctrl1 |= CRT_CTRL_COLOR_RGB565;39*bpp = 16;40break;41case DRM_FORMAT_XRGB8888:42dev_dbg(drm->dev, "Setting up XRGB8888 mode\n");43ctrl1 |= CRT_CTRL_COLOR_XRGB8888;44*bpp = 32;45break;46default:47dev_err(drm->dev, "Unhandled pixel format %08x\n", format);48return -EINVAL;49}5051writel(ctrl1, priv->base + CRT_CTRL1);5253return 0;54}5556static void aspeed_gfx_enable_controller(struct aspeed_gfx *priv)57{58u32 ctrl1 = readl(priv->base + CRT_CTRL1);59u32 ctrl2 = readl(priv->base + CRT_CTRL2);6061/* Set DAC source for display output to Graphics CRT (GFX) */62regmap_update_bits(priv->scu, priv->dac_reg, BIT(16), BIT(16));6364writel(ctrl1 | CRT_CTRL_EN, priv->base + CRT_CTRL1);65writel(ctrl2 | CRT_CTRL_DAC_EN, priv->base + CRT_CTRL2);66}6768static void aspeed_gfx_disable_controller(struct aspeed_gfx *priv)69{70u32 ctrl1 = readl(priv->base + CRT_CTRL1);71u32 ctrl2 = readl(priv->base + CRT_CTRL2);7273writel(ctrl1 & ~CRT_CTRL_EN, priv->base + CRT_CTRL1);74writel(ctrl2 & ~CRT_CTRL_DAC_EN, priv->base + CRT_CTRL2);7576regmap_update_bits(priv->scu, priv->dac_reg, BIT(16), 0);77}7879static void aspeed_gfx_crtc_mode_set_nofb(struct aspeed_gfx *priv)80{81struct drm_display_mode *m = &priv->pipe.crtc.state->adjusted_mode;82u32 ctrl1, d_offset, t_count, bpp;83int err;8485err = aspeed_gfx_set_pixel_fmt(priv, &bpp);86if (err)87return;8889#if 090/* TODO: we have only been able to test with the 40MHz USB clock. The91* clock is fixed, so we cannot adjust it here. */92clk_set_rate(priv->pixel_clk, m->crtc_clock * 1000);93#endif9495ctrl1 = readl(priv->base + CRT_CTRL1);96ctrl1 &= ~(CRT_CTRL_INTERLACED |97CRT_CTRL_HSYNC_NEGATIVE |98CRT_CTRL_VSYNC_NEGATIVE);99100if (m->flags & DRM_MODE_FLAG_INTERLACE)101ctrl1 |= CRT_CTRL_INTERLACED;102103if (!(m->flags & DRM_MODE_FLAG_PHSYNC))104ctrl1 |= CRT_CTRL_HSYNC_NEGATIVE;105106if (!(m->flags & DRM_MODE_FLAG_PVSYNC))107ctrl1 |= CRT_CTRL_VSYNC_NEGATIVE;108109writel(ctrl1, priv->base + CRT_CTRL1);110111/* Horizontal timing */112writel(CRT_H_TOTAL(m->htotal - 1) | CRT_H_DE(m->hdisplay - 1),113priv->base + CRT_HORIZ0);114writel(CRT_H_RS_START(m->hsync_start - 1) | CRT_H_RS_END(m->hsync_end),115priv->base + CRT_HORIZ1);116117118/* Vertical timing */119writel(CRT_V_TOTAL(m->vtotal - 1) | CRT_V_DE(m->vdisplay - 1),120priv->base + CRT_VERT0);121writel(CRT_V_RS_START(m->vsync_start) | CRT_V_RS_END(m->vsync_end),122priv->base + CRT_VERT1);123124/*125* Display Offset: address difference between consecutive scan lines126* Terminal Count: memory size of one scan line127*/128d_offset = m->hdisplay * bpp / 8;129t_count = DIV_ROUND_UP(m->hdisplay * bpp, priv->scan_line_max);130131writel(CRT_DISP_OFFSET(d_offset) | CRT_TERM_COUNT(t_count),132priv->base + CRT_OFFSET);133134/*135* Threshold: FIFO thresholds of refill and stop (16 byte chunks136* per line, rounded up)137*/138writel(priv->throd_val, priv->base + CRT_THROD);139}140141static void aspeed_gfx_pipe_enable(struct drm_simple_display_pipe *pipe,142struct drm_crtc_state *crtc_state,143struct drm_plane_state *plane_state)144{145struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);146struct drm_crtc *crtc = &pipe->crtc;147148aspeed_gfx_crtc_mode_set_nofb(priv);149aspeed_gfx_enable_controller(priv);150drm_crtc_vblank_on(crtc);151}152153static void aspeed_gfx_pipe_disable(struct drm_simple_display_pipe *pipe)154{155struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);156struct drm_crtc *crtc = &pipe->crtc;157158drm_crtc_vblank_off(crtc);159aspeed_gfx_disable_controller(priv);160}161162static void aspeed_gfx_pipe_update(struct drm_simple_display_pipe *pipe,163struct drm_plane_state *plane_state)164{165struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);166struct drm_crtc *crtc = &pipe->crtc;167struct drm_framebuffer *fb = pipe->plane.state->fb;168struct drm_pending_vblank_event *event;169struct drm_gem_dma_object *gem;170171spin_lock_irq(&crtc->dev->event_lock);172event = crtc->state->event;173if (event) {174crtc->state->event = NULL;175176if (drm_crtc_vblank_get(crtc) == 0)177drm_crtc_arm_vblank_event(crtc, event);178else179drm_crtc_send_vblank_event(crtc, event);180}181spin_unlock_irq(&crtc->dev->event_lock);182183if (!fb)184return;185186gem = drm_fb_dma_get_gem_obj(fb, 0);187if (!gem)188return;189writel(gem->dma_addr, priv->base + CRT_ADDR);190}191192static int aspeed_gfx_enable_vblank(struct drm_simple_display_pipe *pipe)193{194struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);195u32 reg = readl(priv->base + CRT_CTRL1);196197/* Clear pending VBLANK IRQ */198writel(reg | CRT_CTRL_VERTICAL_INTR_STS, priv->base + CRT_CTRL1);199200reg |= CRT_CTRL_VERTICAL_INTR_EN;201writel(reg, priv->base + CRT_CTRL1);202203return 0;204}205206static void aspeed_gfx_disable_vblank(struct drm_simple_display_pipe *pipe)207{208struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);209u32 reg = readl(priv->base + CRT_CTRL1);210211reg &= ~CRT_CTRL_VERTICAL_INTR_EN;212writel(reg, priv->base + CRT_CTRL1);213214/* Clear pending VBLANK IRQ */215writel(reg | CRT_CTRL_VERTICAL_INTR_STS, priv->base + CRT_CTRL1);216}217218static const struct drm_simple_display_pipe_funcs aspeed_gfx_funcs = {219.enable = aspeed_gfx_pipe_enable,220.disable = aspeed_gfx_pipe_disable,221.update = aspeed_gfx_pipe_update,222.enable_vblank = aspeed_gfx_enable_vblank,223.disable_vblank = aspeed_gfx_disable_vblank,224};225226static const uint32_t aspeed_gfx_formats[] = {227DRM_FORMAT_XRGB8888,228DRM_FORMAT_RGB565,229};230231int aspeed_gfx_create_pipe(struct drm_device *drm)232{233struct aspeed_gfx *priv = to_aspeed_gfx(drm);234235return drm_simple_display_pipe_init(drm, &priv->pipe, &aspeed_gfx_funcs,236aspeed_gfx_formats,237ARRAY_SIZE(aspeed_gfx_formats),238NULL,239&priv->connector);240}241242243