Path: blob/master/drivers/gpu/drm/arm/display/komeda/komeda_crtc.c
26519 views
// SPDX-License-Identifier: GPL-2.01/*2* (C) COPYRIGHT 2018 ARM Limited. All rights reserved.3* Author: James.Qian.Wang <[email protected]>4*5*/6#include <linux/clk.h>7#include <linux/of.h>8#include <linux/pm_runtime.h>9#include <linux/spinlock.h>1011#include <drm/drm_atomic.h>12#include <drm/drm_atomic_helper.h>13#include <drm/drm_print.h>14#include <drm/drm_vblank.h>15#include <drm/drm_simple_kms_helper.h>16#include <drm/drm_bridge.h>1718#include "komeda_dev.h"19#include "komeda_kms.h"2021void komeda_crtc_get_color_config(struct drm_crtc_state *crtc_st,22u32 *color_depths, u32 *color_formats)23{24struct drm_connector *conn;25struct drm_connector_state *conn_st;26u32 conn_color_formats = ~0u;27int i, min_bpc = 31, conn_bpc = 0;2829for_each_new_connector_in_state(crtc_st->state, conn, conn_st, i) {30if (conn_st->crtc != crtc_st->crtc)31continue;3233conn_bpc = conn->display_info.bpc ? conn->display_info.bpc : 8;34conn_color_formats &= conn->display_info.color_formats;3536if (conn_bpc < min_bpc)37min_bpc = conn_bpc;38}3940/* connector doesn't config any color_format, use RGB444 as default */41if (!conn_color_formats)42conn_color_formats = DRM_COLOR_FORMAT_RGB444;4344*color_depths = GENMASK(min_bpc, 0);45*color_formats = conn_color_formats;46}4748static void komeda_crtc_update_clock_ratio(struct komeda_crtc_state *kcrtc_st)49{50u64 pxlclk, aclk;5152if (!kcrtc_st->base.active) {53kcrtc_st->clock_ratio = 0;54return;55}5657pxlclk = kcrtc_st->base.adjusted_mode.crtc_clock * 1000ULL;58aclk = komeda_crtc_get_aclk(kcrtc_st);5960kcrtc_st->clock_ratio = div64_u64(aclk << 32, pxlclk);61}6263/**64* komeda_crtc_atomic_check - build display output data flow65* @crtc: DRM crtc66* @state: the crtc state object67*68* crtc_atomic_check is the final check stage, so beside build a display data69* pipeline according to the crtc_state, but still needs to release or disable70* the unclaimed pipeline resources.71*72* RETURNS:73* Zero for success or -errno74*/75static int76komeda_crtc_atomic_check(struct drm_crtc *crtc,77struct drm_atomic_state *state)78{79struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,80crtc);81struct komeda_crtc *kcrtc = to_kcrtc(crtc);82struct komeda_crtc_state *kcrtc_st = to_kcrtc_st(crtc_state);83int err;8485if (drm_atomic_crtc_needs_modeset(crtc_state))86komeda_crtc_update_clock_ratio(kcrtc_st);8788if (crtc_state->active) {89err = komeda_build_display_data_flow(kcrtc, kcrtc_st);90if (err)91return err;92}9394/* release unclaimed pipeline resources */95err = komeda_release_unclaimed_resources(kcrtc->slave, kcrtc_st);96if (err)97return err;9899err = komeda_release_unclaimed_resources(kcrtc->master, kcrtc_st);100if (err)101return err;102103return 0;104}105106/* For active a crtc, mainly need two parts of preparation107* 1. adjust display operation mode.108* 2. enable needed clk109*/110static int111komeda_crtc_prepare(struct komeda_crtc *kcrtc)112{113struct komeda_dev *mdev = kcrtc->base.dev->dev_private;114struct komeda_pipeline *master = kcrtc->master;115struct komeda_crtc_state *kcrtc_st = to_kcrtc_st(kcrtc->base.state);116struct drm_display_mode *mode = &kcrtc_st->base.adjusted_mode;117u32 new_mode;118int err;119120mutex_lock(&mdev->lock);121122new_mode = mdev->dpmode | BIT(master->id);123if (WARN_ON(new_mode == mdev->dpmode)) {124err = 0;125goto unlock;126}127128err = mdev->funcs->change_opmode(mdev, new_mode);129if (err) {130DRM_ERROR("failed to change opmode: 0x%x -> 0x%x.\n,",131mdev->dpmode, new_mode);132goto unlock;133}134135mdev->dpmode = new_mode;136/* Only need to enable aclk on single display mode, but no need to137* enable aclk it on dual display mode, since the dual mode always138* switch from single display mode, the aclk already enabled, no need139* to enable it again.140*/141if (new_mode != KOMEDA_MODE_DUAL_DISP) {142err = clk_set_rate(mdev->aclk, komeda_crtc_get_aclk(kcrtc_st));143if (err)144DRM_ERROR("failed to set aclk.\n");145err = clk_prepare_enable(mdev->aclk);146if (err)147DRM_ERROR("failed to enable aclk.\n");148}149150err = clk_set_rate(master->pxlclk, mode->crtc_clock * 1000);151if (err)152DRM_ERROR("failed to set pxlclk for pipe%d\n", master->id);153err = clk_prepare_enable(master->pxlclk);154if (err)155DRM_ERROR("failed to enable pxl clk for pipe%d.\n", master->id);156157unlock:158mutex_unlock(&mdev->lock);159160return err;161}162163static int164komeda_crtc_unprepare(struct komeda_crtc *kcrtc)165{166struct komeda_dev *mdev = kcrtc->base.dev->dev_private;167struct komeda_pipeline *master = kcrtc->master;168u32 new_mode;169int err;170171mutex_lock(&mdev->lock);172173new_mode = mdev->dpmode & (~BIT(master->id));174175if (WARN_ON(new_mode == mdev->dpmode)) {176err = 0;177goto unlock;178}179180err = mdev->funcs->change_opmode(mdev, new_mode);181if (err) {182DRM_ERROR("failed to change opmode: 0x%x -> 0x%x.\n,",183mdev->dpmode, new_mode);184goto unlock;185}186187mdev->dpmode = new_mode;188189clk_disable_unprepare(master->pxlclk);190if (new_mode == KOMEDA_MODE_INACTIVE)191clk_disable_unprepare(mdev->aclk);192193unlock:194mutex_unlock(&mdev->lock);195196return err;197}198199void komeda_crtc_handle_event(struct komeda_crtc *kcrtc,200struct komeda_events *evts)201{202struct drm_crtc *crtc = &kcrtc->base;203u32 events = evts->pipes[kcrtc->master->id];204205if (events & KOMEDA_EVENT_VSYNC)206drm_crtc_handle_vblank(crtc);207208if (events & KOMEDA_EVENT_EOW) {209struct komeda_wb_connector *wb_conn = kcrtc->wb_conn;210211if (wb_conn)212drm_writeback_signal_completion(&wb_conn->base, 0);213else214DRM_WARN("CRTC[%d]: EOW happen but no wb_connector.\n",215drm_crtc_index(&kcrtc->base));216}217/* will handle it together with the write back support */218if (events & KOMEDA_EVENT_EOW)219DRM_DEBUG("EOW.\n");220221if (events & KOMEDA_EVENT_FLIP) {222unsigned long flags;223struct drm_pending_vblank_event *event;224225spin_lock_irqsave(&crtc->dev->event_lock, flags);226if (kcrtc->disable_done) {227complete_all(kcrtc->disable_done);228kcrtc->disable_done = NULL;229} else if (crtc->state->event) {230event = crtc->state->event;231/*232* Consume event before notifying drm core that flip233* happened.234*/235crtc->state->event = NULL;236drm_crtc_send_vblank_event(crtc, event);237} else {238DRM_WARN("CRTC[%d]: FLIP happened but no pending commit.\n",239drm_crtc_index(&kcrtc->base));240}241spin_unlock_irqrestore(&crtc->dev->event_lock, flags);242}243}244245static void246komeda_crtc_do_flush(struct drm_crtc *crtc,247struct drm_crtc_state *old)248{249struct komeda_crtc *kcrtc = to_kcrtc(crtc);250struct komeda_crtc_state *kcrtc_st = to_kcrtc_st(crtc->state);251struct komeda_dev *mdev = kcrtc->base.dev->dev_private;252struct komeda_pipeline *master = kcrtc->master;253struct komeda_pipeline *slave = kcrtc->slave;254struct komeda_wb_connector *wb_conn = kcrtc->wb_conn;255struct drm_connector_state *conn_st;256257DRM_DEBUG_ATOMIC("CRTC%d_FLUSH: active_pipes: 0x%x, affected: 0x%x.\n",258drm_crtc_index(crtc),259kcrtc_st->active_pipes, kcrtc_st->affected_pipes);260261/* step 1: update the pipeline/component state to HW */262if (has_bit(master->id, kcrtc_st->affected_pipes))263komeda_pipeline_update(master, old->state);264265if (slave && has_bit(slave->id, kcrtc_st->affected_pipes))266komeda_pipeline_update(slave, old->state);267268conn_st = wb_conn ? wb_conn->base.base.state : NULL;269if (conn_st && conn_st->writeback_job)270drm_writeback_queue_job(&wb_conn->base, conn_st);271272/* step 2: notify the HW to kickoff the update */273mdev->funcs->flush(mdev, master->id, kcrtc_st->active_pipes);274}275276static void277komeda_crtc_atomic_enable(struct drm_crtc *crtc,278struct drm_atomic_state *state)279{280struct drm_crtc_state *old = drm_atomic_get_old_crtc_state(state,281crtc);282pm_runtime_get_sync(crtc->dev->dev);283komeda_crtc_prepare(to_kcrtc(crtc));284drm_crtc_vblank_on(crtc);285WARN_ON(drm_crtc_vblank_get(crtc));286komeda_crtc_do_flush(crtc, old);287}288289void290komeda_crtc_flush_and_wait_for_flip_done(struct komeda_crtc *kcrtc,291struct completion *input_flip_done)292{293struct drm_device *drm = kcrtc->base.dev;294struct komeda_dev *mdev = kcrtc->master->mdev;295struct completion *flip_done;296struct completion temp;297298/* if caller doesn't send a flip_done, use a private flip_done */299if (input_flip_done) {300flip_done = input_flip_done;301} else {302init_completion(&temp);303kcrtc->disable_done = &temp;304flip_done = &temp;305}306307mdev->funcs->flush(mdev, kcrtc->master->id, 0);308309/* wait the flip take affect.*/310if (wait_for_completion_timeout(flip_done, HZ) == 0) {311DRM_ERROR("wait pipe%d flip done timeout\n", kcrtc->master->id);312if (!input_flip_done) {313unsigned long flags;314315spin_lock_irqsave(&drm->event_lock, flags);316kcrtc->disable_done = NULL;317spin_unlock_irqrestore(&drm->event_lock, flags);318}319}320}321322static void323komeda_crtc_atomic_disable(struct drm_crtc *crtc,324struct drm_atomic_state *state)325{326struct drm_crtc_state *old = drm_atomic_get_old_crtc_state(state,327crtc);328struct komeda_crtc *kcrtc = to_kcrtc(crtc);329struct komeda_crtc_state *old_st = to_kcrtc_st(old);330struct komeda_pipeline *master = kcrtc->master;331struct komeda_pipeline *slave = kcrtc->slave;332struct completion *disable_done;333bool needs_phase2 = false;334335DRM_DEBUG_ATOMIC("CRTC%d_DISABLE: active_pipes: 0x%x, affected: 0x%x\n",336drm_crtc_index(crtc),337old_st->active_pipes, old_st->affected_pipes);338339if (slave && has_bit(slave->id, old_st->active_pipes))340komeda_pipeline_disable(slave, old->state);341342if (has_bit(master->id, old_st->active_pipes))343needs_phase2 = komeda_pipeline_disable(master, old->state);344345/* crtc_disable has two scenarios according to the state->active switch.346* 1. active -> inactive347* this commit is a disable commit. and the commit will be finished348* or done after the disable operation. on this case we can directly349* use the crtc->state->event to tracking the HW disable operation.350* 2. active -> active351* the crtc->commit is not for disable, but a modeset operation when352* crtc is active, such commit actually has been completed by 3353* DRM operations:354* crtc_disable, update_planes(crtc_flush), crtc_enable355* so on this case the crtc->commit is for the whole process.356* we can not use it for tracing the disable, we need a temporary357* flip_done for tracing the disable. and crtc->state->event for358* the crtc_enable operation.359* That's also the reason why skip modeset commit in360* komeda_crtc_atomic_flush()361*/362disable_done = (needs_phase2 || crtc->state->active) ?363NULL : &crtc->state->commit->flip_done;364365/* wait phase 1 disable done */366komeda_crtc_flush_and_wait_for_flip_done(kcrtc, disable_done);367368/* phase 2 */369if (needs_phase2) {370komeda_pipeline_disable(kcrtc->master, old->state);371372disable_done = crtc->state->active ?373NULL : &crtc->state->commit->flip_done;374375komeda_crtc_flush_and_wait_for_flip_done(kcrtc, disable_done);376}377378drm_crtc_vblank_put(crtc);379drm_crtc_vblank_off(crtc);380komeda_crtc_unprepare(kcrtc);381pm_runtime_put(crtc->dev->dev);382}383384static void385komeda_crtc_atomic_flush(struct drm_crtc *crtc,386struct drm_atomic_state *state)387{388struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,389crtc);390struct drm_crtc_state *old = drm_atomic_get_old_crtc_state(state,391crtc);392/* commit with modeset will be handled in enable/disable */393if (drm_atomic_crtc_needs_modeset(crtc_state))394return;395396komeda_crtc_do_flush(crtc, old);397}398399/* Returns the minimum frequency of the aclk rate (main engine clock) in Hz */400static unsigned long401komeda_calc_min_aclk_rate(struct komeda_crtc *kcrtc,402unsigned long pxlclk)403{404/* Once dual-link one display pipeline drives two display outputs,405* the aclk needs run on the double rate of pxlclk406*/407if (kcrtc->master->dual_link)408return pxlclk * 2;409else410return pxlclk;411}412413/* Get current aclk rate that specified by state */414unsigned long komeda_crtc_get_aclk(struct komeda_crtc_state *kcrtc_st)415{416struct drm_crtc *crtc = kcrtc_st->base.crtc;417struct komeda_dev *mdev = crtc->dev->dev_private;418unsigned long pxlclk = kcrtc_st->base.adjusted_mode.crtc_clock * 1000;419unsigned long min_aclk;420421min_aclk = komeda_calc_min_aclk_rate(to_kcrtc(crtc), pxlclk);422423return clk_round_rate(mdev->aclk, min_aclk);424}425426static enum drm_mode_status427komeda_crtc_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *m)428{429struct komeda_dev *mdev = crtc->dev->dev_private;430struct komeda_crtc *kcrtc = to_kcrtc(crtc);431struct komeda_pipeline *master = kcrtc->master;432unsigned long min_pxlclk, min_aclk;433434if (m->flags & DRM_MODE_FLAG_INTERLACE)435return MODE_NO_INTERLACE;436437min_pxlclk = m->clock * 1000;438if (master->dual_link)439min_pxlclk /= 2;440441if (min_pxlclk != clk_round_rate(master->pxlclk, min_pxlclk)) {442DRM_DEBUG_ATOMIC("pxlclk doesn't support %lu Hz\n", min_pxlclk);443444return MODE_NOCLOCK;445}446447min_aclk = komeda_calc_min_aclk_rate(to_kcrtc(crtc), min_pxlclk);448if (clk_round_rate(mdev->aclk, min_aclk) < min_aclk) {449DRM_DEBUG_ATOMIC("engine clk can't satisfy the requirement of %s-clk: %lu.\n",450m->name, min_pxlclk);451452return MODE_CLOCK_HIGH;453}454455return MODE_OK;456}457458static bool komeda_crtc_mode_fixup(struct drm_crtc *crtc,459const struct drm_display_mode *m,460struct drm_display_mode *adjusted_mode)461{462struct komeda_crtc *kcrtc = to_kcrtc(crtc);463unsigned long clk_rate;464465drm_mode_set_crtcinfo(adjusted_mode, 0);466/* In dual link half the horizontal settings */467if (kcrtc->master->dual_link) {468adjusted_mode->crtc_clock /= 2;469adjusted_mode->crtc_hdisplay /= 2;470adjusted_mode->crtc_hsync_start /= 2;471adjusted_mode->crtc_hsync_end /= 2;472adjusted_mode->crtc_htotal /= 2;473}474475clk_rate = adjusted_mode->crtc_clock * 1000;476/* crtc_clock will be used as the komeda output pixel clock */477adjusted_mode->crtc_clock = clk_round_rate(kcrtc->master->pxlclk,478clk_rate) / 1000;479480return true;481}482483static const struct drm_crtc_helper_funcs komeda_crtc_helper_funcs = {484.atomic_check = komeda_crtc_atomic_check,485.atomic_flush = komeda_crtc_atomic_flush,486.atomic_enable = komeda_crtc_atomic_enable,487.atomic_disable = komeda_crtc_atomic_disable,488.mode_valid = komeda_crtc_mode_valid,489.mode_fixup = komeda_crtc_mode_fixup,490};491492static void komeda_crtc_reset(struct drm_crtc *crtc)493{494struct komeda_crtc_state *state;495496if (crtc->state)497__drm_atomic_helper_crtc_destroy_state(crtc->state);498499kfree(to_kcrtc_st(crtc->state));500crtc->state = NULL;501502state = kzalloc(sizeof(*state), GFP_KERNEL);503if (state)504__drm_atomic_helper_crtc_reset(crtc, &state->base);505}506507static struct drm_crtc_state *508komeda_crtc_atomic_duplicate_state(struct drm_crtc *crtc)509{510struct komeda_crtc_state *old = to_kcrtc_st(crtc->state);511struct komeda_crtc_state *new;512513new = kzalloc(sizeof(*new), GFP_KERNEL);514if (!new)515return NULL;516517__drm_atomic_helper_crtc_duplicate_state(crtc, &new->base);518519new->affected_pipes = old->active_pipes;520new->clock_ratio = old->clock_ratio;521new->max_slave_zorder = old->max_slave_zorder;522523return &new->base;524}525526static void komeda_crtc_atomic_destroy_state(struct drm_crtc *crtc,527struct drm_crtc_state *state)528{529__drm_atomic_helper_crtc_destroy_state(state);530kfree(to_kcrtc_st(state));531}532533static int komeda_crtc_vblank_enable(struct drm_crtc *crtc)534{535struct komeda_dev *mdev = crtc->dev->dev_private;536struct komeda_crtc *kcrtc = to_kcrtc(crtc);537538mdev->funcs->on_off_vblank(mdev, kcrtc->master->id, true);539return 0;540}541542static void komeda_crtc_vblank_disable(struct drm_crtc *crtc)543{544struct komeda_dev *mdev = crtc->dev->dev_private;545struct komeda_crtc *kcrtc = to_kcrtc(crtc);546547mdev->funcs->on_off_vblank(mdev, kcrtc->master->id, false);548}549550static const struct drm_crtc_funcs komeda_crtc_funcs = {551.destroy = drm_crtc_cleanup,552.set_config = drm_atomic_helper_set_config,553.page_flip = drm_atomic_helper_page_flip,554.reset = komeda_crtc_reset,555.atomic_duplicate_state = komeda_crtc_atomic_duplicate_state,556.atomic_destroy_state = komeda_crtc_atomic_destroy_state,557.enable_vblank = komeda_crtc_vblank_enable,558.disable_vblank = komeda_crtc_vblank_disable,559};560561int komeda_kms_setup_crtcs(struct komeda_kms_dev *kms,562struct komeda_dev *mdev)563{564struct komeda_crtc *crtc;565struct komeda_pipeline *master;566char str[16];567int i;568569kms->n_crtcs = 0;570571for (i = 0; i < mdev->n_pipelines; i++) {572crtc = &kms->crtcs[kms->n_crtcs];573master = mdev->pipelines[i];574575crtc->master = master;576crtc->slave = komeda_pipeline_get_slave(master);577578if (crtc->slave)579sprintf(str, "pipe-%d", crtc->slave->id);580else581sprintf(str, "None");582583DRM_INFO("CRTC-%d: master(pipe-%d) slave(%s).\n",584kms->n_crtcs, master->id, str);585586kms->n_crtcs++;587}588589return 0;590}591592static struct drm_plane *593get_crtc_primary(struct komeda_kms_dev *kms, struct komeda_crtc *crtc)594{595struct komeda_plane *kplane;596struct drm_plane *plane;597598drm_for_each_plane(plane, &kms->base) {599if (plane->type != DRM_PLANE_TYPE_PRIMARY)600continue;601602kplane = to_kplane(plane);603/* only master can be primary */604if (kplane->layer->base.pipeline == crtc->master)605return plane;606}607608return NULL;609}610611static int komeda_attach_bridge(struct device *dev,612struct komeda_pipeline *pipe,613struct drm_encoder *encoder)614{615struct drm_bridge *bridge;616int err;617618bridge = devm_drm_of_get_bridge(dev, pipe->of_node,619KOMEDA_OF_PORT_OUTPUT, 0);620if (IS_ERR(bridge))621return dev_err_probe(dev, PTR_ERR(bridge), "remote bridge not found for pipe: %s\n",622of_node_full_name(pipe->of_node));623624err = drm_bridge_attach(encoder, bridge, NULL, 0);625if (err)626dev_err(dev, "bridge_attach() failed for pipe: %s\n",627of_node_full_name(pipe->of_node));628629return err;630}631632static int komeda_crtc_add(struct komeda_kms_dev *kms,633struct komeda_crtc *kcrtc)634{635struct drm_crtc *crtc = &kcrtc->base;636struct drm_device *base = &kms->base;637struct komeda_pipeline *pipe = kcrtc->master;638struct drm_encoder *encoder = &kcrtc->encoder;639int err;640641err = drm_crtc_init_with_planes(base, crtc,642get_crtc_primary(kms, kcrtc), NULL,643&komeda_crtc_funcs, NULL);644if (err)645return err;646647drm_crtc_helper_add(crtc, &komeda_crtc_helper_funcs);648649crtc->port = pipe->of_output_port;650651/* Construct an encoder for each pipeline and attach it to the remote652* bridge653*/654kcrtc->encoder.possible_crtcs = drm_crtc_mask(crtc);655err = drm_simple_encoder_init(base, encoder, DRM_MODE_ENCODER_TMDS);656if (err)657return err;658659if (pipe->of_output_links[0]) {660err = komeda_attach_bridge(base->dev, pipe, encoder);661if (err)662return err;663}664665drm_crtc_enable_color_mgmt(crtc, 0, true, KOMEDA_COLOR_LUT_SIZE);666667komeda_pipeline_dump(pipe);668669return 0;670}671672int komeda_kms_add_crtcs(struct komeda_kms_dev *kms, struct komeda_dev *mdev)673{674int i, err;675676for (i = 0; i < kms->n_crtcs; i++) {677err = komeda_crtc_add(kms, &kms->crtcs[i]);678if (err)679return err;680}681682return 0;683}684685686