| /* |
| * Copyright (c) 2016, The Linux Foundation. All rights reserved. |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 and |
| * only version 2 as published by the Free Software Foundation. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| */ |
| |
| |
| #define pr_fmt(fmt) "dsi-drm:[%s] " fmt, __func__ |
| #include <drm/drm_atomic_helper.h> |
| #include <drm/drm_atomic.h> |
| |
| #include "msm_kms.h" |
| #include "sde_connector.h" |
| #include "dsi_drm.h" |
| |
| #define to_dsi_bridge(x) container_of((x), struct dsi_bridge, base) |
| #define to_dsi_state(x) container_of((x), struct dsi_connector_state, base) |
| |
| static void convert_to_dsi_mode(const struct drm_display_mode *drm_mode, |
| struct dsi_display_mode *dsi_mode) |
| { |
| memset(dsi_mode, 0, sizeof(*dsi_mode)); |
| |
| dsi_mode->timing.h_active = drm_mode->hdisplay; |
| dsi_mode->timing.h_back_porch = drm_mode->htotal - drm_mode->hsync_end; |
| dsi_mode->timing.h_sync_width = drm_mode->htotal - |
| (drm_mode->hsync_start + dsi_mode->timing.h_back_porch); |
| dsi_mode->timing.h_front_porch = drm_mode->hsync_start - |
| drm_mode->hdisplay; |
| dsi_mode->timing.h_skew = drm_mode->hskew; |
| |
| dsi_mode->timing.v_active = drm_mode->vdisplay; |
| dsi_mode->timing.v_back_porch = drm_mode->vtotal - drm_mode->vsync_end; |
| dsi_mode->timing.v_sync_width = drm_mode->vtotal - |
| (drm_mode->vsync_start + dsi_mode->timing.v_back_porch); |
| |
| dsi_mode->timing.v_front_porch = drm_mode->vsync_start - |
| drm_mode->vdisplay; |
| |
| dsi_mode->timing.refresh_rate = drm_mode->vrefresh; |
| |
| dsi_mode->pixel_clk_khz = drm_mode->clock; |
| dsi_mode->panel_mode = 0; /* TODO: Panel Mode */ |
| |
| if (msm_is_mode_seamless(drm_mode)) |
| dsi_mode->flags |= DSI_MODE_FLAG_SEAMLESS; |
| if (msm_is_mode_dynamic_fps(drm_mode)) |
| dsi_mode->flags |= DSI_MODE_FLAG_DFPS; |
| if (msm_needs_vblank_pre_modeset(drm_mode)) |
| dsi_mode->flags |= DSI_MODE_FLAG_VBLANK_PRE_MODESET; |
| } |
| |
| static void convert_to_drm_mode(const struct dsi_display_mode *dsi_mode, |
| struct drm_display_mode *drm_mode) |
| { |
| memset(drm_mode, 0, sizeof(*drm_mode)); |
| |
| drm_mode->hdisplay = dsi_mode->timing.h_active; |
| drm_mode->hsync_start = drm_mode->hdisplay + |
| dsi_mode->timing.h_front_porch; |
| drm_mode->hsync_end = drm_mode->hsync_start + |
| dsi_mode->timing.h_sync_width; |
| drm_mode->htotal = drm_mode->hsync_end + dsi_mode->timing.h_back_porch; |
| drm_mode->hskew = dsi_mode->timing.h_skew; |
| |
| drm_mode->vdisplay = dsi_mode->timing.v_active; |
| drm_mode->vsync_start = drm_mode->vdisplay + |
| dsi_mode->timing.v_front_porch; |
| drm_mode->vsync_end = drm_mode->vsync_start + |
| dsi_mode->timing.v_sync_width; |
| drm_mode->vtotal = drm_mode->vsync_end + dsi_mode->timing.v_back_porch; |
| |
| drm_mode->vrefresh = dsi_mode->timing.refresh_rate; |
| drm_mode->clock = dsi_mode->pixel_clk_khz; |
| |
| if (dsi_mode->flags & DSI_MODE_FLAG_SEAMLESS) |
| drm_mode->flags |= DRM_MODE_FLAG_SEAMLESS; |
| if (dsi_mode->flags & DSI_MODE_FLAG_DFPS) |
| drm_mode->private_flags |= MSM_MODE_FLAG_SEAMLESS_DYNAMIC_FPS; |
| if (dsi_mode->flags & DSI_MODE_FLAG_VBLANK_PRE_MODESET) |
| drm_mode->private_flags |= MSM_MODE_FLAG_VBLANK_PRE_MODESET; |
| |
| drm_mode_set_name(drm_mode); |
| } |
| |
| static int dsi_bridge_attach(struct drm_bridge *bridge) |
| { |
| struct dsi_bridge *c_bridge = to_dsi_bridge(bridge); |
| |
| if (!bridge) { |
| pr_err("Invalid params\n"); |
| return -EINVAL; |
| } |
| |
| pr_debug("[%d] attached\n", c_bridge->id); |
| |
| return 0; |
| |
| } |
| |
| static void dsi_bridge_pre_enable(struct drm_bridge *bridge) |
| { |
| int rc = 0; |
| struct dsi_bridge *c_bridge = to_dsi_bridge(bridge); |
| |
| if (!bridge) { |
| pr_err("Invalid params\n"); |
| return; |
| } |
| |
| /* By this point mode should have been validated through mode_fixup */ |
| rc = dsi_display_set_mode(c_bridge->display, |
| &(c_bridge->dsi_mode), 0x0); |
| if (rc) { |
| pr_err("[%d] failed to perform a mode set, rc=%d\n", |
| c_bridge->id, rc); |
| return; |
| } |
| |
| if (c_bridge->dsi_mode.flags & DSI_MODE_FLAG_SEAMLESS) { |
| pr_debug("[%d] seamless pre-enable\n", c_bridge->id); |
| return; |
| } |
| |
| rc = dsi_display_prepare(c_bridge->display); |
| if (rc) { |
| pr_err("[%d] DSI display prepare failed, rc=%d\n", |
| c_bridge->id, rc); |
| return; |
| } |
| |
| rc = dsi_display_enable(c_bridge->display); |
| if (rc) { |
| pr_err("[%d] DSI display enable failed, rc=%d\n", |
| c_bridge->id, rc); |
| (void)dsi_display_unprepare(c_bridge->display); |
| } |
| } |
| |
| static void dsi_bridge_enable(struct drm_bridge *bridge) |
| { |
| int rc = 0; |
| struct dsi_bridge *c_bridge = to_dsi_bridge(bridge); |
| |
| if (!bridge) { |
| pr_err("Invalid params\n"); |
| return; |
| } |
| |
| if (c_bridge->dsi_mode.flags & DSI_MODE_FLAG_SEAMLESS) { |
| pr_debug("[%d] seamless enable\n", c_bridge->id); |
| return; |
| } |
| |
| rc = dsi_display_post_enable(c_bridge->display); |
| if (rc) |
| pr_err("[%d] DSI display post enabled failed, rc=%d\n", |
| c_bridge->id, rc); |
| } |
| |
| static void dsi_bridge_disable(struct drm_bridge *bridge) |
| { |
| int rc = 0; |
| struct dsi_bridge *c_bridge = to_dsi_bridge(bridge); |
| |
| if (!bridge) { |
| pr_err("Invalid params\n"); |
| return; |
| } |
| |
| rc = dsi_display_pre_disable(c_bridge->display); |
| if (rc) { |
| pr_err("[%d] DSI display pre disable failed, rc=%d\n", |
| c_bridge->id, rc); |
| } |
| } |
| |
| static void dsi_bridge_post_disable(struct drm_bridge *bridge) |
| { |
| int rc = 0; |
| struct dsi_bridge *c_bridge = to_dsi_bridge(bridge); |
| |
| if (!bridge) { |
| pr_err("Invalid params\n"); |
| return; |
| } |
| |
| rc = dsi_display_disable(c_bridge->display); |
| if (rc) { |
| pr_err("[%d] DSI display disable failed, rc=%d\n", |
| c_bridge->id, rc); |
| return; |
| } |
| |
| rc = dsi_display_unprepare(c_bridge->display); |
| if (rc) { |
| pr_err("[%d] DSI display unprepare failed, rc=%d\n", |
| c_bridge->id, rc); |
| return; |
| } |
| } |
| |
| static void dsi_bridge_mode_set(struct drm_bridge *bridge, |
| struct drm_display_mode *mode, |
| struct drm_display_mode *adjusted_mode) |
| { |
| struct dsi_bridge *c_bridge = to_dsi_bridge(bridge); |
| |
| if (!bridge || !mode || !adjusted_mode) { |
| pr_err("Invalid params\n"); |
| return; |
| } |
| |
| memset(&(c_bridge->dsi_mode), 0x0, sizeof(struct dsi_display_mode)); |
| convert_to_dsi_mode(adjusted_mode, &(c_bridge->dsi_mode)); |
| |
| pr_debug("note: using panel cmd/vid mode instead of user val\n"); |
| c_bridge->dsi_mode.panel_mode = |
| c_bridge->display->panel->mode.panel_mode; |
| } |
| |
| static bool dsi_bridge_mode_fixup(struct drm_bridge *bridge, |
| const struct drm_display_mode *mode, |
| struct drm_display_mode *adjusted_mode) |
| { |
| int rc = 0; |
| bool ret = true; |
| struct dsi_bridge *c_bridge = to_dsi_bridge(bridge); |
| struct dsi_display_mode dsi_mode; |
| |
| if (!bridge || !mode || !adjusted_mode) { |
| pr_err("Invalid params\n"); |
| return false; |
| } |
| |
| convert_to_dsi_mode(mode, &dsi_mode); |
| |
| rc = dsi_display_validate_mode(c_bridge->display, &dsi_mode, |
| DSI_VALIDATE_FLAG_ALLOW_ADJUST); |
| if (rc) { |
| pr_err("[%d] mode is not valid, rc=%d\n", c_bridge->id, rc); |
| ret = false; |
| } else { |
| convert_to_drm_mode(&dsi_mode, adjusted_mode); |
| } |
| |
| return ret; |
| } |
| |
| static const struct drm_bridge_funcs dsi_bridge_ops = { |
| .attach = dsi_bridge_attach, |
| .mode_fixup = dsi_bridge_mode_fixup, |
| .pre_enable = dsi_bridge_pre_enable, |
| .enable = dsi_bridge_enable, |
| .disable = dsi_bridge_disable, |
| .post_disable = dsi_bridge_post_disable, |
| .mode_set = dsi_bridge_mode_set, |
| }; |
| |
| int dsi_conn_post_init(struct drm_connector *connector, |
| void *info, |
| void *display) |
| { |
| struct dsi_display *dsi_display = display; |
| struct dsi_panel *panel; |
| |
| if (!info || !dsi_display) |
| return -EINVAL; |
| |
| sde_kms_info_add_keystr(info, |
| "display type", dsi_display->display_type); |
| |
| switch (dsi_display->type) { |
| case DSI_DISPLAY_SINGLE: |
| sde_kms_info_add_keystr(info, "display config", |
| "single display"); |
| break; |
| case DSI_DISPLAY_EXT_BRIDGE: |
| sde_kms_info_add_keystr(info, "display config", "ext bridge"); |
| break; |
| case DSI_DISPLAY_SPLIT: |
| sde_kms_info_add_keystr(info, "display config", |
| "split display"); |
| break; |
| case DSI_DISPLAY_SPLIT_EXT_BRIDGE: |
| sde_kms_info_add_keystr(info, "display config", |
| "split ext bridge"); |
| break; |
| default: |
| pr_debug("invalid display type:%d\n", dsi_display->type); |
| break; |
| } |
| |
| if (!dsi_display->panel) { |
| pr_debug("invalid panel data\n"); |
| goto end; |
| } |
| |
| panel = dsi_display->panel; |
| sde_kms_info_add_keystr(info, "panel name", panel->name); |
| |
| switch (panel->mode.panel_mode) { |
| case DSI_OP_VIDEO_MODE: |
| sde_kms_info_add_keystr(info, "panel mode", "video"); |
| break; |
| case DSI_OP_CMD_MODE: |
| sde_kms_info_add_keystr(info, "panel mode", "command"); |
| sde_kms_info_add_keyint(info, "mdp_transfer_time_us", |
| panel->cmd_config.mdp_transfer_time_us); |
| break; |
| default: |
| pr_debug("invalid panel type:%d\n", panel->mode.panel_mode); |
| break; |
| } |
| sde_kms_info_add_keystr(info, "dfps support", |
| panel->dfps_caps.dfps_support ? "true" : "false"); |
| |
| switch (panel->phy_props.rotation) { |
| case DSI_PANEL_ROTATE_NONE: |
| sde_kms_info_add_keystr(info, "panel orientation", "none"); |
| break; |
| case DSI_PANEL_ROTATE_H_FLIP: |
| sde_kms_info_add_keystr(info, "panel orientation", "horz flip"); |
| break; |
| case DSI_PANEL_ROTATE_V_FLIP: |
| sde_kms_info_add_keystr(info, "panel orientation", "vert flip"); |
| break; |
| default: |
| pr_debug("invalid panel rotation:%d\n", |
| panel->phy_props.rotation); |
| break; |
| } |
| |
| switch (panel->bl_config.type) { |
| case DSI_BACKLIGHT_PWM: |
| sde_kms_info_add_keystr(info, "backlight type", "pwm"); |
| break; |
| case DSI_BACKLIGHT_WLED: |
| sde_kms_info_add_keystr(info, "backlight type", "wled"); |
| break; |
| case DSI_BACKLIGHT_DCS: |
| sde_kms_info_add_keystr(info, "backlight type", "dcs"); |
| break; |
| default: |
| pr_debug("invalid panel backlight type:%d\n", |
| panel->bl_config.type); |
| break; |
| } |
| |
| end: |
| return 0; |
| } |
| |
| enum drm_connector_status dsi_conn_detect(struct drm_connector *conn, |
| bool force, |
| void *display) |
| { |
| enum drm_connector_status status = connector_status_unknown; |
| struct msm_display_info info; |
| int rc; |
| |
| if (!conn || !display) |
| return status; |
| |
| /* get display dsi_info */ |
| memset(&info, 0x0, sizeof(info)); |
| rc = dsi_display_get_info(&info, display); |
| if (rc) { |
| pr_err("failed to get display info, rc=%d\n", rc); |
| return connector_status_disconnected; |
| } |
| |
| if (info.capabilities & MSM_DISPLAY_CAP_HOT_PLUG) |
| status = (info.is_connected ? connector_status_connected : |
| connector_status_disconnected); |
| else |
| status = connector_status_connected; |
| |
| conn->display_info.width_mm = info.width_mm; |
| conn->display_info.height_mm = info.height_mm; |
| |
| return status; |
| } |
| |
| int dsi_connector_get_modes(struct drm_connector *connector, |
| void *display) |
| { |
| u32 count = 0; |
| u32 size = 0; |
| struct dsi_display_mode *modes; |
| struct drm_display_mode drm_mode; |
| int rc, i; |
| |
| if (sde_connector_get_panel(connector)) { |
| /* |
| * TODO: If drm_panel is attached, query modes from the panel. |
| * This is complicated in split dsi cases because panel is not |
| * attached to both connectors. |
| */ |
| goto end; |
| } |
| rc = dsi_display_get_modes(display, NULL, &count); |
| if (rc) { |
| pr_err("failed to get num of modes, rc=%d\n", rc); |
| goto error; |
| } |
| |
| size = count * sizeof(*modes); |
| modes = kzalloc(size, GFP_KERNEL); |
| if (!modes) { |
| count = 0; |
| goto end; |
| } |
| |
| rc = dsi_display_get_modes(display, modes, &count); |
| if (rc) { |
| pr_err("failed to get modes, rc=%d\n", rc); |
| count = 0; |
| goto error; |
| } |
| |
| for (i = 0; i < count; i++) { |
| struct drm_display_mode *m; |
| |
| memset(&drm_mode, 0x0, sizeof(drm_mode)); |
| convert_to_drm_mode(&modes[i], &drm_mode); |
| m = drm_mode_duplicate(connector->dev, &drm_mode); |
| if (!m) { |
| pr_err("failed to add mode %ux%u\n", |
| drm_mode.hdisplay, |
| drm_mode.vdisplay); |
| count = -ENOMEM; |
| goto error; |
| } |
| m->width_mm = connector->display_info.width_mm; |
| m->height_mm = connector->display_info.height_mm; |
| drm_mode_probed_add(connector, m); |
| } |
| error: |
| kfree(modes); |
| end: |
| pr_debug("MODE COUNT =%d\n\n", count); |
| return count; |
| } |
| |
| enum drm_mode_status dsi_conn_mode_valid(struct drm_connector *connector, |
| struct drm_display_mode *mode, |
| void *display) |
| { |
| struct dsi_display_mode dsi_mode; |
| int rc; |
| |
| if (!connector || !mode) { |
| pr_err("Invalid params\n"); |
| return MODE_ERROR; |
| } |
| |
| convert_to_dsi_mode(mode, &dsi_mode); |
| |
| rc = dsi_display_validate_mode(display, &dsi_mode, |
| DSI_VALIDATE_FLAG_ALLOW_ADJUST); |
| if (rc) { |
| pr_err("mode not supported, rc=%d\n", rc); |
| return MODE_BAD; |
| } |
| |
| return MODE_OK; |
| } |
| |
| struct dsi_bridge *dsi_drm_bridge_init(struct dsi_display *display, |
| struct drm_device *dev, |
| struct drm_encoder *encoder) |
| { |
| int rc = 0; |
| struct dsi_bridge *bridge; |
| |
| bridge = kzalloc(sizeof(*bridge), GFP_KERNEL); |
| if (!bridge) { |
| rc = -ENOMEM; |
| goto error; |
| } |
| |
| bridge->display = display; |
| bridge->base.funcs = &dsi_bridge_ops; |
| bridge->base.encoder = encoder; |
| |
| rc = drm_bridge_attach(dev, &bridge->base); |
| if (rc) { |
| pr_err("failed to attach bridge, rc=%d\n", rc); |
| goto error_free_bridge; |
| } |
| |
| encoder->bridge = &bridge->base; |
| return bridge; |
| error_free_bridge: |
| kfree(bridge); |
| error: |
| return ERR_PTR(rc); |
| } |
| |
| void dsi_drm_bridge_cleanup(struct dsi_bridge *bridge) |
| { |
| if (bridge && bridge->base.encoder) |
| bridge->base.encoder->bridge = NULL; |
| |
| kfree(bridge); |
| } |