blob: 9497934581c7ab083f076ddeaf7c65ea40d9d199 [file] [log] [blame]
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -07003 * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -07004 *
5 * Not a Contribution, Apache license notifications and license are
6 * retained for attribution purposes only.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
Naseer Ahmed72cf9762012-07-21 12:17:13 -070021#define DEBUG 0
Naseer Ahmed72cf9762012-07-21 12:17:13 -070022#include <fcntl.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070023#include <linux/msm_mdp.h>
Arun Kumar K.R25187572013-02-28 18:47:36 -080024#include <video/msm_hdmi_modes.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070025#include <linux/fb.h>
26#include <sys/ioctl.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070027#include <cutils/properties.h>
28#include "hwc_utils.h"
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -070029#include "hdmi.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070030#include "overlayUtils.h"
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080031#include "overlay.h"
Tatenda Chipeperekwaf080b792014-03-05 19:32:33 -080032#include "qd_utils.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070033
34using namespace android;
Tatenda Chipeperekwace8d5c42014-08-13 10:53:49 -070035using namespace qdutils;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070036
37namespace qhwc {
Arun Kumar K.R26808f32013-02-11 19:17:05 -080038#define UNKNOWN_STRING "unknown"
39#define SPD_NAME_LENGTH 16
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070040
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -070041/* The array gEDIDData contains a list of modes currently
42 * supported by HDMI and display, and modes that are not
43 * supported i.e. interlaced modes.
44
45 * In order to add support for a new mode, the mode must be
46 * appended to the end of the array.
47 *
48 * Each new entry must contain the following:
49 * -Mode: a video format defined in msm_hdmi_modes.h
50 * -Width: x resolution for the mode
51 * -Height: y resolution for the mode
52 * -FPS: the frame rate for the mode
53 * -Mode Order: the priority for the new mode that is used when determining
54 * the best mode when the HDMI display is connected.
55 */
56EDIDData gEDIDData [] = {
57 EDIDData(HDMI_VFRMT_1440x480i60_4_3, 1440, 480, 60, 1),
58 EDIDData(HDMI_VFRMT_1440x480i60_16_9, 1440, 480, 60, 2),
59 EDIDData(HDMI_VFRMT_1440x576i50_4_3, 1440, 576, 50, 3),
60 EDIDData(HDMI_VFRMT_1440x576i50_16_9, 1440, 576, 50, 4),
61 EDIDData(HDMI_VFRMT_1920x1080i60_16_9, 1920, 1080, 60, 5),
62 EDIDData(HDMI_VFRMT_640x480p60_4_3, 640, 480, 60, 6),
63 EDIDData(HDMI_VFRMT_720x480p60_4_3, 720, 480, 60, 7),
64 EDIDData(HDMI_VFRMT_720x480p60_16_9, 720, 480, 60, 8),
65 EDIDData(HDMI_VFRMT_720x576p50_4_3, 720, 576, 50, 9),
66 EDIDData(HDMI_VFRMT_720x576p50_16_9, 720, 576, 50, 10),
67 EDIDData(HDMI_VFRMT_1024x768p60_4_3, 1024, 768, 60, 11),
68 EDIDData(HDMI_VFRMT_1280x1024p60_5_4, 1280, 1024, 60, 12),
69 EDIDData(HDMI_VFRMT_1280x720p50_16_9, 1280, 720, 50, 13),
70 EDIDData(HDMI_VFRMT_1280x720p60_16_9, 1280, 720, 60, 14),
71 EDIDData(HDMI_VFRMT_1920x1080p24_16_9, 1920, 1080, 24, 15),
72 EDIDData(HDMI_VFRMT_1920x1080p25_16_9, 1920, 1080, 25, 16),
73 EDIDData(HDMI_VFRMT_1920x1080p30_16_9, 1920, 1080, 30, 17),
74 EDIDData(HDMI_VFRMT_1920x1080p50_16_9, 1920, 1080, 50, 18),
75 EDIDData(HDMI_VFRMT_1920x1080p60_16_9, 1920, 1080, 60, 19),
76 EDIDData(HDMI_VFRMT_2560x1600p60_16_9, 2560, 1600, 60, 20),
77 EDIDData(HDMI_VFRMT_3840x2160p24_16_9, 3840, 2160, 24, 21),
78 EDIDData(HDMI_VFRMT_3840x2160p25_16_9, 3840, 2160, 25, 22),
79 EDIDData(HDMI_VFRMT_3840x2160p30_16_9, 3840, 2160, 30, 23),
80 EDIDData(HDMI_VFRMT_4096x2160p24_16_9, 4096, 2160, 24, 24),
81};
82
83// Number of modes in gEDIDData
84const int gEDIDCount = (sizeof(gEDIDData)/sizeof(gEDIDData)[0]);
85
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -070086int HDMIDisplay::configure() {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070087 if(!openFrameBuffer()) {
88 ALOGE("%s: Failed to open FB: %d", __FUNCTION__, mFbNum);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080089 return -1;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070090 }
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -080091 readCEUnderscanInfo();
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080092 readResolution();
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080093 // TODO: Move this to activate
Arun Kumar K.R37552c52012-12-10 12:47:18 -080094 /* Used for changing the resolution
95 * getUserMode will get the preferred
96 * mode set thru adb shell */
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -070097 mCurrentMode = getUserMode();
98 if (mCurrentMode == -1) {
Arun Kumar K.R37552c52012-12-10 12:47:18 -080099 //Get the best mode and set
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700100 mCurrentMode = getBestMode();
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800101 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700102 setAttributes();
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800103 // set system property
104 property_set("hw.hdmiON", "1");
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700105
106 // Read the system property to determine if downscale feature is enabled.
107 char value[PROPERTY_VALUE_MAX];
108 mMDPDownscaleEnabled = false;
109 if(property_get("sys.hwc.mdp_downscale_enabled", value, "false")
110 && !strcmp(value, "true")) {
111 mMDPDownscaleEnabled = true;
112 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800113 return 0;
114}
115
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700116void HDMIDisplay::getAttributes(uint32_t& width, uint32_t& height) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700117 uint32_t fps = 0;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700118 getAttrForMode(width, height, fps);
119}
120
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700121int HDMIDisplay::teardown() {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700122 closeFrameBuffer();
123 resetInfo();
124 // unset system property
125 property_set("hw.hdmiON", "0");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800126 return 0;
127}
128
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700129HDMIDisplay::HDMIDisplay():mFd(-1),
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700130 mCurrentMode(-1), mModeCount(0), mPrimaryWidth(0), mPrimaryHeight(0),
131 mUnderscanSupported(false)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700132{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700133 memset(&mVInfo, 0, sizeof(mVInfo));
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700134
135 mDisplayId = HWC_DISPLAY_EXTERNAL;
136 // Update the display if HDMI is connected as primary
137 if (isHDMIPrimaryDisplay()) {
138 mDisplayId = HWC_DISPLAY_PRIMARY;
139 }
140
141 mFbNum = overlay::Overlay::getInstance()->getFbForDpy(mDisplayId);
Arun Kumar K.Re746ac52013-03-20 15:56:15 -0700142 // disable HPD at start, it will be enabled later
143 // when the display powers on
144 // This helps for framework reboot or adb shell stop/start
145 writeHPDOption(0);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800146
Arun Kumar K.R25187572013-02-28 18:47:36 -0800147 // for HDMI - retreive all the modes supported by the driver
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700148 if(mFbNum != -1) {
Arun Kumar K.R25187572013-02-28 18:47:36 -0800149 supported_video_mode_lut =
150 new msm_hdmi_mode_timing_info[HDMI_VFRMT_MAX];
151 // Populate the mode table for supported modes
152 MSM_HDMI_MODES_INIT_TIMINGS(supported_video_mode_lut);
153 MSM_HDMI_MODES_SET_SUPP_TIMINGS(supported_video_mode_lut,
154 MSM_HDMI_MODES_ALL);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800155 // Update the Source Product Information
156 // Vendor Name
157 setSPDInfo("vendor_name", "ro.product.manufacturer");
158 // Product Description
159 setSPDInfo("product_description", "ro.product.name");
160 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700161
162 ALOGD_IF(DEBUG, "%s mDisplayId(%d) mFbNum(%d)",
163 __FUNCTION__, mDisplayId, mFbNum);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800164}
165/* gets the product manufacturer and product name and writes it
166 * to the sysfs node, so that the driver can get that information
167 * Used to show QCOM 8974 instead of Input 1 for example
168 */
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700169void HDMIDisplay::setSPDInfo(const char* node, const char* property) {
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800170 char info[PROPERTY_VALUE_MAX];
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700171 ssize_t err = -1;
172 int spdFile = openDeviceNode(node, O_RDWR);
173 if (spdFile >= 0) {
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800174 memset(info, 0, sizeof(info));
175 property_get(property, info, UNKNOWN_STRING);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700176 ALOGD_IF(DEBUG, "In %s: %s = %s",
177 __FUNCTION__, property, info);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800178 if (strncmp(info, UNKNOWN_STRING, SPD_NAME_LENGTH)) {
179 err = write(spdFile, info, strlen(info));
180 if (err <= 0) {
181 ALOGE("%s: file write failed for '%s'"
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700182 "err no = %d", __FUNCTION__, node, errno);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800183 }
184 } else {
185 ALOGD_IF(DEBUG, "%s: property_get failed for SPD %s",
186 __FUNCTION__, node);
187 }
188 close(spdFile);
Arun Kumar K.R25187572013-02-28 18:47:36 -0800189 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700190}
191
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700192void HDMIDisplay::setHPD(uint32_t value) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700193 ALOGD_IF(DEBUG,"HPD enabled=%d", value);
194 writeHPDOption(value);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700195}
196
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700197void HDMIDisplay::setActionSafeDimension(int w, int h) {
Saurabh Shah56f610d2012-08-07 15:27:06 -0700198 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800199 char actionsafeWidth[PROPERTY_VALUE_MAX];
200 char actionsafeHeight[PROPERTY_VALUE_MAX];
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800201 snprintf(actionsafeWidth, sizeof(actionsafeWidth), "%d", w);
Arun Kumar K.R4d73d642013-04-10 17:06:39 -0700202 property_set("persist.sys.actionsafe.width", actionsafeWidth);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800203 snprintf(actionsafeHeight, sizeof(actionsafeHeight), "%d", h);
Arun Kumar K.R4d73d642013-04-10 17:06:39 -0700204 property_set("persist.sys.actionsafe.height", actionsafeHeight);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700205}
206
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700207int HDMIDisplay::getModeCount() const {
Saurabh Shah56f610d2012-08-07 15:27:06 -0700208 ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700209 return mModeCount;
210}
211
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700212void HDMIDisplay::readCEUnderscanInfo()
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800213{
214 int hdmiScanInfoFile = -1;
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530215 ssize_t len = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800216 char scanInfo[17];
217 char *ce_info_str = NULL;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800218 char *save_ptr;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800219 const char token[] = ", \n";
220 int ce_info = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800221
222 memset(scanInfo, 0, sizeof(scanInfo));
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700223 hdmiScanInfoFile = openDeviceNode("scan_info", O_RDONLY);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800224 if (hdmiScanInfoFile < 0) {
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800225 return;
226 } else {
227 len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700228 ALOGD("%s: Scan Info string: %s length = %zu",
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800229 __FUNCTION__, scanInfo, len);
230 if (len <= 0) {
231 close(hdmiScanInfoFile);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700232 ALOGE("%s: Scan Info file empty", __FUNCTION__);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800233 return;
234 }
235 scanInfo[len] = '\0'; /* null terminate the string */
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530236 close(hdmiScanInfoFile);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800237 }
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800238
239 /*
240 * The scan_info contains the three fields
241 * PT - preferred video format
242 * IT - video format
243 * CE video format - containing the underscan support information
244 */
245
246 /* PT */
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800247 ce_info_str = strtok_r(scanInfo, token, &save_ptr);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800248 if (ce_info_str) {
249 /* IT */
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800250 ce_info_str = strtok_r(NULL, token, &save_ptr);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800251 if (ce_info_str) {
252 /* CE */
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800253 ce_info_str = strtok_r(NULL, token, &save_ptr);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800254 if (ce_info_str)
255 ce_info = atoi(ce_info_str);
256 }
257 }
258
259 if (ce_info_str) {
260 // ce_info contains the underscan information
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700261 if (ce_info == HDMI_SCAN_ALWAYS_UNDERSCANED ||
262 ce_info == HDMI_SCAN_BOTH_SUPPORTED)
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800263 // if TV supported underscan, then driver will always underscan
264 // hence no need to apply action safe rectangle
265 mUnderscanSupported = true;
266 } else {
267 ALOGE("%s: scan_info string error", __FUNCTION__);
268 }
269
270 // Store underscan support info in a system property
271 const char* prop = (mUnderscanSupported) ? "1" : "0";
272 property_set("hw.underscan_supported", prop);
273 return;
274}
275
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700276HDMIDisplay::~HDMIDisplay()
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700277{
Arun Kumar K.R25187572013-02-28 18:47:36 -0800278 delete [] supported_video_mode_lut;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700279 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700280}
281
Arun Kumar K.R25187572013-02-28 18:47:36 -0800282/*
283 * sets the fb_var_screeninfo from the hdmi_mode_timing_info
284 */
285void setDisplayTiming(struct fb_var_screeninfo &info,
286 const msm_hdmi_mode_timing_info* mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700287{
288 info.reserved[0] = 0;
289 info.reserved[1] = 0;
290 info.reserved[2] = 0;
Ken Zhang7b03a952013-01-16 13:23:48 -0500291#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Arun Kumar K.R25187572013-02-28 18:47:36 -0800292 info.reserved[3] = (info.reserved[3] & 0xFFFF) |
293 (mode->video_format << 16);
Ken Zhang7b03a952013-01-16 13:23:48 -0500294#endif
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700295 info.xoffset = 0;
296 info.yoffset = 0;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800297 info.xres = mode->active_h;
298 info.yres = mode->active_v;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700299
Arun Kumar K.R25187572013-02-28 18:47:36 -0800300 info.pixclock = (mode->pixel_freq)*1000;
301 info.vmode = mode->interlaced ?
302 FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700303
Arun Kumar K.R25187572013-02-28 18:47:36 -0800304 info.right_margin = mode->front_porch_h;
305 info.hsync_len = mode->pulse_width_h;
306 info.left_margin = mode->back_porch_h;
307 info.lower_margin = mode->front_porch_v;
308 info.vsync_len = mode->pulse_width_v;
309 info.upper_margin = mode->back_porch_v;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700310}
311
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700312int HDMIDisplay::parseResolution(char* edidStr)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700313{
314 char delim = ',';
315 int count = 0;
316 char *start, *end;
317 // EDIDs are string delimited by ','
318 // Ex: 16,4,5,3,32,34,1
319 // Parse this string to get mode(int)
320 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700321 end = &delim;
322 while(*end == delim) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700323 mEDIDModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700324 start = end+1;
325 count++;
326 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700327 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
328 for (int i = 0; i < count; i++)
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700329 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, mEDIDModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700330 return count;
331}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700332
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700333bool HDMIDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700334{
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530335 ssize_t len = -1;
Arun Kumar K.R83812342013-09-24 17:30:36 -0700336 char edidStr[128] = {'\0'};
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700337
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700338 int hdmiEDIDFile = openDeviceNode("edid_modes", O_RDONLY);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700339 if (hdmiEDIDFile < 0) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700340 return false;
341 } else {
Arun Kumar K.R83812342013-09-24 17:30:36 -0700342 len = read(hdmiEDIDFile, edidStr, sizeof(edidStr)-1);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700343 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %zu",
Arun Kumar K.R83812342013-09-24 17:30:36 -0700344 __FUNCTION__, edidStr, len);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700345 if (len <= 0) {
346 ALOGE("%s: edid_modes file empty", __FUNCTION__);
Arun Kumar K.R83812342013-09-24 17:30:36 -0700347 edidStr[0] = '\0';
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700348 }
349 else {
Arun Kumar K.R83812342013-09-24 17:30:36 -0700350 while (len > 1 && isspace(edidStr[len-1])) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700351 --len;
Arun Kumar K.R83812342013-09-24 17:30:36 -0700352 }
353 edidStr[len] = '\0';
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700354 }
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530355 close(hdmiEDIDFile);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700356 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700357 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800358 // Get EDID modes from the EDID strings
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700359 mModeCount = parseResolution(edidStr);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700360 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
361 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700362 }
363
Arun Kumar K.R83812342013-09-24 17:30:36 -0700364 return (len > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700365}
366
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700367bool HDMIDisplay::openFrameBuffer()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700368{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700369 if (mFd == -1) {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700370 char strDevPath[MAX_SYSFS_FILE_PATH];
371 snprintf(strDevPath, MAX_SYSFS_FILE_PATH, "/dev/graphics/fb%d", mFbNum);
372 mFd = open(strDevPath, O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700373 if (mFd < 0)
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700374 ALOGE("%s: %s is not available", __FUNCTION__, strDevPath);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700375 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700376 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700377}
378
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700379bool HDMIDisplay::closeFrameBuffer()
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700380{
381 int ret = 0;
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500382 if(mFd >= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700383 ret = close(mFd);
384 mFd = -1;
385 }
386 return (ret == 0);
387}
388
389// clears the vinfo, edid, best modes
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700390void HDMIDisplay::resetInfo()
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700391{
392 memset(&mVInfo, 0, sizeof(mVInfo));
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700393 memset(mEDIDModes, 0, sizeof(mEDIDModes));
394 mModeCount = 0;
395 mCurrentMode = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800396 mUnderscanSupported = false;
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700397 mXres = 0;
398 mYres = 0;
399 mVsyncPeriod = 0;
400 mMDPScalingMode = false;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800401 // Reset the underscan supported system property
402 const char* prop = "0";
403 property_set("hw.underscan_supported", prop);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700404}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700405
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700406int HDMIDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700407{
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700408 for (int dataIndex = 0; dataIndex < gEDIDCount; dataIndex++) {
409 if (gEDIDData[dataIndex].mMode == mode) {
410 return gEDIDData[dataIndex].mModeOrder;
411 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700412 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700413 ALOGE("%s Mode not found: %d", __FUNCTION__, mode);
414 return -1;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700415}
416
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800417/// Returns the user mode set(if any) using adb shell
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700418int HDMIDisplay::getUserMode() {
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800419 /* Based on the property set the resolution */
420 char property_value[PROPERTY_VALUE_MAX];
Arun Kumar K.Rc31bdcb2013-02-25 17:47:42 -0800421 property_get("hw.hdmi.resolution", property_value, "-1");
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800422 int mode = atoi(property_value);
423 // We dont support interlaced modes
424 if(isValidMode(mode) && !isInterlacedMode(mode)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500425 ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800426 return mode;
427 }
428 return -1;
429}
430
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700431// Get the best mode for the current HD TV
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700432int HDMIDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700433 int bestOrder = 0;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800434 int bestMode = HDMI_VFRMT_640x480p60_4_3;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700435 // for all the edid read, get the best mode
436 for(int i = 0; i < mModeCount; i++) {
437 int mode = mEDIDModes[i];
438 int order = getModeOrder(mode);
439 if (order > bestOrder) {
440 bestOrder = order;
441 bestMode = mode;
442 }
443 }
444 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700445}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700446
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700447inline bool HDMIDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700448{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800449 bool valid = false;
450 for (int i = 0; i < mModeCount; i++) {
451 if(ID == mEDIDModes[i]) {
452 valid = true;
453 break;
454 }
455 }
456 return valid;
457}
458
459// returns true if the mode(ID) is interlaced mode format
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700460bool HDMIDisplay::isInterlacedMode(int ID) {
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800461 bool interlaced = false;
462 switch(ID) {
Arun Kumar K.R25187572013-02-28 18:47:36 -0800463 case HDMI_VFRMT_1440x480i60_4_3:
464 case HDMI_VFRMT_1440x480i60_16_9:
465 case HDMI_VFRMT_1440x576i50_4_3:
466 case HDMI_VFRMT_1440x576i50_16_9:
467 case HDMI_VFRMT_1920x1080i60_16_9:
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800468 interlaced = true;
Arun Kumar K.Rae46f3a2013-05-16 16:40:10 -0700469 break;
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800470 default:
471 interlaced = false;
Arun Kumar K.Rae46f3a2013-05-16 16:40:10 -0700472 break;
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800473 }
474 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700475}
476
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700477// Does a put_vscreen info on the HDMI interface which will update
478// the configuration (resolution, timing info) to match mCurrentMode
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700479void HDMIDisplay::activateDisplay()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700480{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700481 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700482 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
483 if(ret < 0) {
484 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
485 strerror(errno));
486 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700487 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
488 "(%d,%d,%d) %dMHz>", __FUNCTION__,
489 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
490 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
491 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
492 mVInfo.pixclock/1000/1000);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700493
494 const struct msm_hdmi_mode_timing_info *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700495 &supported_video_mode_lut[0];
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700496 for (unsigned int i = 0; i < HDMI_VFRMT_MAX; ++i) {
497 const struct msm_hdmi_mode_timing_info *cur =
498 &supported_video_mode_lut[i];
499 if (cur->video_format == (uint32_t)mCurrentMode) {
500 mode = cur;
501 break;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700502 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700503 }
504 setDisplayTiming(mVInfo, mode);
505 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
506 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, mCurrentMode,
507 mode->video_format, mVInfo.xres, mVInfo.yres,
508 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
509 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
510 mVInfo.pixclock/1000/1000);
Ken Zhang7b03a952013-01-16 13:23:48 -0500511#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700512 struct msmfb_metadata metadata;
513 memset(&metadata, 0 , sizeof(metadata));
514 metadata.op = metadata_op_vic;
515 metadata.data.video_info_code = mode->video_format;
516 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
517 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
518 __FUNCTION__, strerror(errno));
519 }
Ken Zhang7b03a952013-01-16 13:23:48 -0500520#endif
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700521 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
522 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
523 if(ret < 0) {
524 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
525 __FUNCTION__, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700526 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700527}
528
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700529bool HDMIDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700530{
531 bool ret = true;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700532 if(mFbNum != -1) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700533 int hdmiHPDFile = openDeviceNode("hpd", O_RDWR);
534 if (hdmiHPDFile >= 0) {
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530535 ssize_t err = -1;
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700536 ALOGD_IF(DEBUG, "%s: option = %d",
537 __FUNCTION__, userOption);
Arun Kumar K.R47d6b642013-06-26 16:20:30 -0700538 if(userOption)
539 err = write(hdmiHPDFile, "1", 2);
540 else
541 err = write(hdmiHPDFile, "0" , 2);
542 if (err <= 0) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700543 ALOGE("%s: file write failed 'hpd'", __FUNCTION__);
Arun Kumar K.R47d6b642013-06-26 16:20:30 -0700544 ret = false;
545 }
546 close(hdmiHPDFile);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700547 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700548 }
549 return ret;
550}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700551
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700552
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700553void HDMIDisplay::setAttributes() {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700554 uint32_t fps = 0;
555 // Always set dpyAttr res to mVInfo res
556 getAttrForMode(mXres, mYres, fps);
557 mMDPScalingMode = false;
558
559 if(overlay::Overlay::getInstance()->isUIScalingOnExternalSupported()
560 && mMDPDownscaleEnabled) {
561 // if primary resolution is more than the hdmi resolution
562 // configure dpy attr to primary resolution and set MDP
563 // scaling mode
564 // Restrict this upto 1080p resolution max, if target does not
565 // support source split feature.
566 uint32_t primaryArea = mPrimaryWidth * mPrimaryHeight;
567 if(((primaryArea) > (mXres * mYres)) &&
568 (((primaryArea) <= SUPPORTED_DOWNSCALE_AREA) ||
Ramkumar Radhakrishnana5c72872014-08-28 19:04:18 -0700569 qdutils::MDPVersion::getInstance().isSrcSplit())) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700570 // tmpW and tmpH will hold the primary dimensions before we
571 // update the aspect ratio if necessary.
572 int tmpW = mPrimaryWidth;
573 int tmpH = mPrimaryHeight;
574 // HDMI is always in landscape, so always assign the higher
575 // dimension to hdmi's xres
576 if(mPrimaryHeight > mPrimaryWidth) {
577 tmpW = mPrimaryHeight;
578 tmpH = mPrimaryWidth;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700579 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700580 // The aspect ratios of the external and primary displays
581 // can be different. As a result, directly assigning primary
582 // resolution could lead to an incorrect final image.
583 // We get around this by calculating a new resolution by
584 // keeping aspect ratio intact.
585 hwc_rect r = {0, 0, 0, 0};
586 qdutils::getAspectRatioPosition(tmpW, tmpH, mXres, mYres, r);
587 uint32_t newExtW = r.right - r.left;
588 uint32_t newExtH = r.bottom - r.top;
589 uint32_t alignedExtW;
590 uint32_t alignedExtH;
591 // On 8994 and below targets MDP supports only 4X downscaling,
592 // Restricting selected external resolution to be exactly 4X
593 // greater resolution than actual external resolution
594 uint32_t maxMDPDownScale =
595 qdutils::MDPVersion::getInstance().getMaxMDPDownscale();
596 if((mXres * mYres * maxMDPDownScale) < (newExtW * newExtH)) {
597 float upScaleFactor = (float)maxMDPDownScale / 2.0f;
598 newExtW = (int)((float)mXres * upScaleFactor);
599 newExtH = (int)((float)mYres * upScaleFactor);
600 }
601 // Align it down so that the new aligned resolution does not
602 // exceed the maxMDPDownscale factor
603 alignedExtW = overlay::utils::aligndown(newExtW, 4);
604 alignedExtH = overlay::utils::aligndown(newExtH, 4);
605 mXres = alignedExtW;
606 mYres = alignedExtH;
607 // Set External Display MDP Downscale mode indicator
608 mMDPScalingMode = true;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700609 }
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700610 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700611 ALOGD_IF(DEBUG_MDPDOWNSCALE, "Selected external resolution [%d X %d] "
612 "maxMDPDownScale %d mMDPScalingMode %d srcSplitEnabled %d "
613 "MDPDownscale feature %d",
614 mXres, mYres,
615 qdutils::MDPVersion::getInstance().getMaxMDPDownscale(),
616 mMDPScalingMode, qdutils::MDPVersion::getInstance().isSrcSplit(),
617 mMDPDownscaleEnabled);
618 mVsyncPeriod = (int) 1000000000l / fps;
619 ALOGD_IF(DEBUG, "%s xres=%d, yres=%d", __FUNCTION__, mXres, mYres);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700620}
621
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700622void HDMIDisplay::getAttrForMode(uint32_t& width, uint32_t& height,
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700623 uint32_t& fps) {
624 for (int dataIndex = 0; dataIndex < gEDIDCount; dataIndex++) {
625 if (gEDIDData[dataIndex].mMode == mCurrentMode) {
626 width = gEDIDData[dataIndex].mWidth;
627 height = gEDIDData[dataIndex].mHeight;
628 fps = gEDIDData[dataIndex].mFps;
629 return;
630 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700631 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700632 ALOGE("%s Unable to get attributes for %d", __FUNCTION__, mCurrentMode);
633}
634
635/* returns the fd related to the node specified*/
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700636int HDMIDisplay::openDeviceNode(const char* node, int fileMode) const {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700637 char sysFsFilePath[MAX_SYSFS_FILE_PATH];
638 memset(sysFsFilePath, 0, sizeof(sysFsFilePath));
639 snprintf(sysFsFilePath , sizeof(sysFsFilePath),
640 "/sys/devices/virtual/graphics/fb%d/%s",
641 mFbNum, node);
642
643 int fd = open(sysFsFilePath, fileMode, 0);
644
645 if (fd < 0) {
646 ALOGE("%s: file '%s' not found : ret = %d err str: %s",
647 __FUNCTION__, sysFsFilePath, fd, strerror(errno));
648 }
649 return fd;
650}
651
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700652bool HDMIDisplay::isHDMIPrimaryDisplay() {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700653 int hdmiNode = qdutils::getHDMINode();
654 return (hdmiNode == HWC_DISPLAY_PRIMARY);
655}
656
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700657int HDMIDisplay::getConnectedState() {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700658 int ret = -1;
659 int mFbNum = qdutils::getHDMINode();
660 int connectedNode = openDeviceNode("connected", O_RDONLY);
661 if(connectedNode >= 0) {
662 char opStr[4];
663 ssize_t bytesRead = read(connectedNode, opStr, sizeof(opStr) - 1);
664 if(bytesRead > 0) {
665 opStr[bytesRead] = '\0';
666 ret = atoi(opStr);
667 ALOGD_IF(DEBUG, "%s: Read %d from connected", __FUNCTION__, ret);
668 } else if(bytesRead == 0) {
669 ALOGE("%s: HDMI connected node empty", __FUNCTION__);
670 } else {
671 ALOGE("%s: Read from HDMI connected node failed with error %s",
672 __FUNCTION__, strerror(errno));
673 }
674 close(connectedNode);
675 } else {
676 ALOGD("%s: /sys/class/graphics/fb%d/connected could not be opened : %s",
677 __FUNCTION__, mFbNum, strerror(errno));
678 }
679 return ret;
680}
681
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700682void HDMIDisplay::setPrimaryAttributes(uint32_t primaryWidth,
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700683 uint32_t primaryHeight) {
684 mPrimaryHeight = primaryHeight;
685 mPrimaryWidth = primaryWidth;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700686}
687
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700688};