#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/dma-buf.h>
#include <linux/media-bus-format.h>
#include <linux/of_graph.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_fb_dma_helper.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_gem_atomic_helper.h>
#include <drm/drm_gem_dma_helper.h>
#include <drm/drm_print.h>
#include <drm/drm_vblank.h>
#include "pl111_drm.h"
irqreturn_t pl111_irq(int irq, void *data)
{
struct pl111_drm_dev_private *priv = data;
u32 irq_stat;
irqreturn_t status = IRQ_NONE;
irq_stat = readl(priv->regs + CLCD_PL111_MIS);
if (!irq_stat)
return IRQ_NONE;
if (irq_stat & CLCD_IRQ_NEXTBASE_UPDATE) {
drm_crtc_handle_vblank(&priv->crtc);
status = IRQ_HANDLED;
}
writel(irq_stat, priv->regs + CLCD_PL111_ICR);
return status;
}
static enum drm_mode_status
pl111_crtc_helper_mode_valid(struct drm_crtc *crtc,
const struct drm_display_mode *mode)
{
struct drm_device *drm = crtc->dev;
struct pl111_drm_dev_private *priv = drm->dev_private;
u32 cpp = DIV_ROUND_UP(priv->variant->fb_depth, 8);
u64 bw;
bw = mode->clock * 1000ULL;
bw = bw * mode->hdisplay * mode->vdisplay * cpp;
bw = div_u64(bw, mode->htotal * mode->vtotal);
if (priv->memory_bw && (bw > priv->memory_bw)) {
DRM_DEBUG_KMS("%d x %d @ %d Hz, %d cpp, bw %llu too fast\n",
mode->hdisplay, mode->vdisplay,
mode->clock * 1000, cpp, bw);
return MODE_BAD;
}
DRM_DEBUG_KMS("%d x %d @ %d Hz, %d cpp, bw %llu bytes/s OK\n",
mode->hdisplay, mode->vdisplay,
mode->clock * 1000, cpp, bw);
return MODE_OK;
}
static int pl111_plane_helper_atomic_check(struct drm_plane *plane,
struct drm_atomic_commit *commit)
{
struct drm_plane_state *pstate = drm_atomic_get_new_plane_state(commit, plane);
struct drm_plane_state *old_pstate = drm_atomic_get_old_plane_state(commit, plane);
struct drm_crtc_state *cstate = NULL;
const struct drm_display_mode *mode;
struct drm_framebuffer *old_fb = old_pstate->fb;
struct drm_framebuffer *fb = pstate->fb;
int ret;
if (pstate->crtc) {
cstate = drm_atomic_get_crtc_state(commit, pstate->crtc);
if (IS_ERR(cstate))
return PTR_ERR(cstate);
}
ret = drm_atomic_helper_check_plane_state(pstate, cstate,
DRM_PLANE_NO_SCALING,
DRM_PLANE_NO_SCALING,
false, false);
if (ret)
return ret;
if (!pstate->visible)
return 0;
mode = &cstate->mode;
if (mode->hdisplay % 16)
return -EINVAL;
if (fb) {
u32 offset = drm_fb_dma_get_gem_addr(fb, pstate, 0);
if (offset & 3)
return -EINVAL;
if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0])
return -EINVAL;
if (old_fb && old_fb->format != fb->format)
cstate->mode_changed = true;
}
return 0;
}
static void pl111_crtc_helper_atomic_enable(struct drm_crtc *crtc,
struct drm_atomic_commit *commit)
{
struct drm_device *drm = crtc->dev;
struct pl111_drm_dev_private *priv = drm->dev_private;
struct drm_crtc_state *cstate = drm_atomic_get_new_crtc_state(commit, crtc);
struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(commit, &priv->plane);
const struct drm_display_mode *mode = &cstate->mode;
struct drm_framebuffer *fb = plane_state->fb;
struct drm_connector *connector = priv->connector;
struct drm_bridge *bridge = priv->bridge;
bool grayscale = false;
u32 cntl;
u32 ppl, hsw, hfp, hbp;
u32 lpp, vsw, vfp, vbp;
u32 cpl, tim2;
int ret;
ret = clk_set_rate(priv->clk, mode->clock * 1000);
if (ret) {
drm_err(drm,
"Failed to set pixel clock rate to %d: %d\n",
mode->clock * 1000, ret);
}
clk_prepare_enable(priv->clk);
ppl = (mode->hdisplay / 16) - 1;
hsw = mode->hsync_end - mode->hsync_start - 1;
hfp = mode->hsync_start - mode->hdisplay - 1;
hbp = mode->htotal - mode->hsync_end - 1;
lpp = mode->vdisplay - 1;
vsw = mode->vsync_end - mode->vsync_start - 1;
vfp = mode->vsync_start - mode->vdisplay;
vbp = mode->vtotal - mode->vsync_end;
cpl = mode->hdisplay - 1;
writel((ppl << 2) |
(hsw << 8) |
(hfp << 16) |
(hbp << 24),
priv->regs + CLCD_TIM0);
writel(lpp |
(vsw << 10) |
(vfp << 16) |
(vbp << 24),
priv->regs + CLCD_TIM1);
spin_lock(&priv->tim2_lock);
tim2 = readl(priv->regs + CLCD_TIM2);
tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
if (priv->variant->broken_clockdivider)
tim2 |= TIM2_BCD;
if (mode->flags & DRM_MODE_FLAG_NHSYNC)
tim2 |= TIM2_IHS;
if (mode->flags & DRM_MODE_FLAG_NVSYNC)
tim2 |= TIM2_IVS;
if (connector) {
if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
tim2 |= TIM2_IOE;
if (connector->display_info.bus_flags &
DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
tim2 |= TIM2_IPC;
if (connector->display_info.num_bus_formats == 1 &&
connector->display_info.bus_formats[0] ==
MEDIA_BUS_FMT_Y8_1X8)
grayscale = true;
if (grayscale)
tim2 |= TIM2_ACB_MASK;
}
if (bridge) {
const struct drm_bridge_timings *btimings = bridge->timings;
if (btimings && btimings->setup_time_ps >= 3000)
tim2 ^= TIM2_IPC;
}
tim2 |= cpl << 16;
writel(tim2, priv->regs + CLCD_TIM2);
spin_unlock(&priv->tim2_lock);
writel(0, priv->regs + CLCD_TIM3);
if (grayscale)
cntl = CNTL_LCDEN | CNTL_LCDMONO8;
else
cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDVCOMP(1);
if (priv->variant->st_bitmux_control)
cntl |= CNTL_ST_CDWID_24;
switch (fb->format->format) {
case DRM_FORMAT_BGR888:
if (priv->variant->st_bitmux_control)
cntl |= CNTL_ST_LCDBPP24_PACKED | CNTL_BGR;
break;
case DRM_FORMAT_RGB888:
if (priv->variant->st_bitmux_control)
cntl |= CNTL_ST_LCDBPP24_PACKED;
break;
case DRM_FORMAT_XBGR8888:
if (priv->variant->st_bitmux_control)
cntl |= CNTL_LCDBPP24 | CNTL_BGR;
else
cntl |= CNTL_LCDBPP24;
break;
case DRM_FORMAT_XRGB8888:
if (priv->variant->st_bitmux_control)
cntl |= CNTL_LCDBPP24;
else
cntl |= CNTL_LCDBPP24 | CNTL_BGR;
break;
case DRM_FORMAT_BGR565:
if (priv->variant->is_pl110)
cntl |= CNTL_LCDBPP16;
else if (priv->variant->st_bitmux_control)
cntl |= CNTL_LCDBPP16 | CNTL_ST_1XBPP_565 | CNTL_BGR;
else
cntl |= CNTL_LCDBPP16_565;
break;
case DRM_FORMAT_RGB565:
if (priv->variant->is_pl110)
cntl |= CNTL_LCDBPP16 | CNTL_BGR;
else if (priv->variant->st_bitmux_control)
cntl |= CNTL_LCDBPP16 | CNTL_ST_1XBPP_565;
else
cntl |= CNTL_LCDBPP16_565 | CNTL_BGR;
break;
case DRM_FORMAT_XBGR1555:
cntl |= CNTL_LCDBPP16;
if (priv->variant->st_bitmux_control)
cntl |= CNTL_ST_1XBPP_5551 | CNTL_BGR;
break;
case DRM_FORMAT_XRGB1555:
cntl |= CNTL_LCDBPP16;
if (priv->variant->st_bitmux_control)
cntl |= CNTL_ST_1XBPP_5551;
else
cntl |= CNTL_BGR;
break;
case DRM_FORMAT_XBGR4444:
cntl |= CNTL_LCDBPP16_444;
if (priv->variant->st_bitmux_control)
cntl |= CNTL_ST_1XBPP_444 | CNTL_BGR;
break;
case DRM_FORMAT_XRGB4444:
cntl |= CNTL_LCDBPP16_444;
if (priv->variant->st_bitmux_control)
cntl |= CNTL_ST_1XBPP_444;
else
cntl |= CNTL_BGR;
break;
default:
WARN_ONCE(true, "Unknown FB format 0x%08x\n",
fb->format->format);
break;
}
if (priv->variant->external_bgr)
cntl &= ~CNTL_BGR;
writel(cntl, priv->regs + priv->ctrl);
msleep(20);
if (priv->variant_display_enable)
priv->variant_display_enable(drm, fb->format->format);
cntl |= CNTL_LCDPWR;
writel(cntl, priv->regs + priv->ctrl);
if (!priv->variant->broken_vblank)
drm_crtc_vblank_on(crtc);
}
static void pl111_crtc_helper_atomic_disable(struct drm_crtc *crtc,
struct drm_atomic_commit *commit)
{
struct drm_device *drm = crtc->dev;
struct pl111_drm_dev_private *priv = drm->dev_private;
u32 cntl;
if (!priv->variant->broken_vblank)
drm_crtc_vblank_off(crtc);
cntl = readl(priv->regs + priv->ctrl);
if (cntl & CNTL_LCDPWR) {
cntl &= ~CNTL_LCDPWR;
writel(cntl, priv->regs + priv->ctrl);
}
msleep(20);
if (priv->variant_display_disable)
priv->variant_display_disable(drm);
writel(0, priv->regs + priv->ctrl);
clk_disable_unprepare(priv->clk);
}
static void pl111_plane_helper_atomic_update(struct drm_plane *plane,
struct drm_atomic_commit *commit)
{
struct drm_device *drm = plane->dev;
struct pl111_drm_dev_private *priv = drm->dev_private;
struct drm_plane_state *pstate = drm_atomic_get_new_plane_state(commit, plane);
struct drm_framebuffer *fb = pstate->fb;
if (!fb)
return;
u32 addr = drm_fb_dma_get_gem_addr(fb, pstate, 0);
writel(addr, priv->regs + CLCD_UBAS);
}
static void pl111_crtc_helper_atomic_flush(struct drm_crtc *crtc,
struct drm_atomic_commit *commit)
{
struct drm_crtc_state *cstate = drm_atomic_get_new_crtc_state(commit, crtc);
struct drm_pending_vblank_event *event = cstate->event;
if (!event)
return;
cstate->event = NULL;
spin_lock_irq(&crtc->dev->event_lock);
if (cstate->active && drm_crtc_vblank_get(crtc) == 0)
drm_crtc_arm_vblank_event(crtc, event);
else
drm_crtc_send_vblank_event(crtc, event);
spin_unlock_irq(&crtc->dev->event_lock);
}
static int pl111_display_enable_vblank(struct drm_crtc *crtc)
{
struct drm_device *drm = crtc->dev;
struct pl111_drm_dev_private *priv = drm->dev_private;
writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + priv->ienb);
return 0;
}
static void pl111_display_disable_vblank(struct drm_crtc *crtc)
{
struct drm_device *drm = crtc->dev;
struct pl111_drm_dev_private *priv = drm->dev_private;
writel(0, priv->regs + priv->ienb);
}
static int pl111_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_commit *commit)
{
struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(commit, crtc);
int ret;
if (crtc_state->enable) {
ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
if (ret)
return ret;
}
return drm_atomic_add_affected_planes(commit, crtc);
}
static struct drm_crtc_funcs pl111_crtc_funcs = {
.atomic_create_state = drm_atomic_helper_crtc_create_state,
.destroy = drm_crtc_cleanup,
.set_config = drm_atomic_helper_set_config,
.page_flip = drm_atomic_helper_page_flip,
.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
};
static const struct drm_crtc_helper_funcs pl111_crtc_helper_funcs = {
.mode_valid = pl111_crtc_helper_mode_valid,
.atomic_check = pl111_crtc_helper_atomic_check,
.atomic_enable = pl111_crtc_helper_atomic_enable,
.atomic_disable = pl111_crtc_helper_atomic_disable,
.atomic_flush = pl111_crtc_helper_atomic_flush,
};
static const struct drm_plane_funcs pl111_plane_funcs = {
.update_plane = drm_atomic_helper_update_plane,
.disable_plane = drm_atomic_helper_disable_plane,
.atomic_create_state = drm_atomic_helper_plane_create_state,
.destroy = drm_plane_cleanup,
.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
};
static const struct drm_plane_helper_funcs pl111_plane_helper_funcs = {
.prepare_fb = drm_gem_plane_helper_prepare_fb,
.atomic_check = pl111_plane_helper_atomic_check,
.atomic_update = pl111_plane_helper_atomic_update,
};
static const struct drm_encoder_funcs pl111_encoder_funcs = {
.destroy = drm_encoder_cleanup,
};
static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
unsigned long *prate, bool set_parent)
{
int best_div = 1, div;
struct clk_hw *parent = clk_hw_get_parent(hw);
unsigned long best_prate = 0;
unsigned long best_diff = ~0ul;
int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
for (div = 1; div < max_div; div++) {
unsigned long this_prate, div_rate, diff;
if (set_parent)
this_prate = clk_hw_round_rate(parent, rate * div);
else
this_prate = *prate;
div_rate = DIV_ROUND_UP_ULL(this_prate, div);
diff = abs(rate - div_rate);
if (diff < best_diff) {
best_div = div;
best_diff = diff;
best_prate = this_prate;
}
}
*prate = best_prate;
return best_div;
}
static int pl111_clk_div_determine_rate(struct clk_hw *hw,
struct clk_rate_request *req)
{
int div = pl111_clk_div_choose_div(hw, req->rate,
&req->best_parent_rate, true);
req->rate = DIV_ROUND_UP_ULL(req->best_parent_rate, div);
return 0;
}
static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
unsigned long prate)
{
struct pl111_drm_dev_private *priv =
container_of(hw, struct pl111_drm_dev_private, clk_div);
u32 tim2 = readl(priv->regs + CLCD_TIM2);
int div;
if (tim2 & TIM2_BCD)
return prate;
div = tim2 & TIM2_PCD_LO_MASK;
div |= (tim2 & TIM2_PCD_HI_MASK) >>
(TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
div += 2;
return DIV_ROUND_UP_ULL(prate, div);
}
static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
unsigned long prate)
{
struct pl111_drm_dev_private *priv =
container_of(hw, struct pl111_drm_dev_private, clk_div);
int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
u32 tim2;
spin_lock(&priv->tim2_lock);
tim2 = readl(priv->regs + CLCD_TIM2);
tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
if (div == 1) {
tim2 |= TIM2_BCD;
} else {
div -= 2;
tim2 |= div & TIM2_PCD_LO_MASK;
tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
}
writel(tim2, priv->regs + CLCD_TIM2);
spin_unlock(&priv->tim2_lock);
return 0;
}
static const struct clk_ops pl111_clk_div_ops = {
.recalc_rate = pl111_clk_div_recalc_rate,
.determine_rate = pl111_clk_div_determine_rate,
.set_rate = pl111_clk_div_set_rate,
};
static int
pl111_init_clock_divider(struct drm_device *drm)
{
struct pl111_drm_dev_private *priv = drm->dev_private;
struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
struct clk_hw *div = &priv->clk_div;
const char *parent_name;
struct clk_init_data init = {
.name = "pl111_div",
.ops = &pl111_clk_div_ops,
.parent_names = &parent_name,
.num_parents = 1,
.flags = CLK_SET_RATE_PARENT,
};
int ret;
if (IS_ERR(parent)) {
drm_err(drm, "CLCD: unable to get clcdclk.\n");
return PTR_ERR(parent);
}
spin_lock_init(&priv->tim2_lock);
if (priv->variant->broken_clockdivider) {
priv->clk = parent;
return 0;
}
parent_name = __clk_get_name(parent);
div->init = &init;
ret = devm_clk_hw_register(drm->dev, div);
priv->clk = div->clk;
return ret;
}
int pl111_display_init(struct drm_device *drm)
{
struct pl111_drm_dev_private *priv = drm->dev_private;
int ret;
ret = pl111_init_clock_divider(drm);
if (ret)
return ret;
if (!priv->variant->broken_vblank) {
pl111_crtc_funcs.enable_vblank = pl111_display_enable_vblank;
pl111_crtc_funcs.disable_vblank = pl111_display_disable_vblank;
}
ret = drm_universal_plane_init(drm, &priv->plane, 0,
&pl111_plane_funcs,
priv->variant->formats,
priv->variant->nformats,
NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
if (ret)
return ret;
drm_plane_helper_add(&priv->plane, &pl111_plane_helper_funcs);
ret = drm_crtc_init_with_planes(drm, &priv->crtc, &priv->plane,
NULL, &pl111_crtc_funcs, NULL);
if (ret)
return ret;
drm_crtc_helper_add(&priv->crtc, &pl111_crtc_helper_funcs);
ret = drm_encoder_init(drm, &priv->encoder, &pl111_encoder_funcs,
DRM_MODE_ENCODER_NONE, NULL);
if (ret)
return ret;
priv->encoder.possible_crtcs = drm_crtc_mask(&priv->crtc);
if (priv->connector) {
ret = drm_connector_attach_encoder(priv->connector,
&priv->encoder);
if (ret)
return ret;
}
return 0;
}