video: omap: fix pixel-per-line bitfield setting
[oweals/u-boot.git] / drivers / video / videomodes.c
index 245917f1891bddbb645103a72f67c5ebbf4bcdd9..ac25b45f8196e67a34cd61182f35695090879b7c 100644 (file)
@@ -1,9 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2004
  * Pierre Aubert, Staubli Faverges , <p.aubert@staubli.com>
  * Copyright 2011 Freescale Semiconductor, Inc.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /************************************************************************
@@ -58,6 +57,9 @@
 ****************************************************************************/
 
 #include <common.h>
+#include <edid.h>
+#include <env.h>
+#include <errno.h>
 #include <linux/ctype.h>
 
 #include "videomodes.h"
@@ -113,7 +115,7 @@ const struct ctfb_res_modes res_mode_init[RES_MODES_COUNT] = {
  * returns the length to the next seperator
  */
 static int
-video_get_param_len (char *start, char sep)
+video_get_param_len(const char *start, char sep)
 {
        int i = 0;
        while ((*start != 0) && (*start != sep)) {
@@ -163,7 +165,8 @@ int video_get_params (struct ctfb_res_modes *pPar, char *penv)
        /* first search for the environment containing the real param string */
        s = penv;
 
-       if ((p = getenv (s)) != NULL)
+       p = env_get(s);
+       if (p)
                s = p;
 
        /*
@@ -232,7 +235,7 @@ int video_get_params (struct ctfb_res_modes *pPar, char *penv)
 int video_get_video_mode(unsigned int *xres, unsigned int *yres,
        unsigned int *depth, unsigned int *freq, const char **options)
 {
-       char *p = getenv("video-mode");
+       char *p = env_get("video-mode");
        if (!p)
                return 0;
 
@@ -275,3 +278,169 @@ int video_get_video_mode(unsigned int *xres, unsigned int *yres,
 
        return 1;
 }
+
+/*
+ * Parse the 'video-mode' environment variable using video_get_video_mode()
+ * and lookup the matching ctfb_res_modes in res_mode_init.
+ *
+ * @default_mode: RES_MODE_##x## define for the mode to store in mode_ret
+ *   when 'video-mode' is not set or does not contain a valid mode
+ * @default_depth: depth to set when 'video-mode' is not set
+ * @mode_ret: pointer where the mode will be stored
+ * @depth_ret: pointer where the depth will be stored
+ * @options: pointer to any remaining options, or NULL
+ */
+void video_get_ctfb_res_modes(int default_mode, unsigned int default_depth,
+                             const struct ctfb_res_modes **mode_ret,
+                             unsigned int *depth_ret,
+                             const char **options)
+{
+       unsigned int i, xres, yres, depth, refresh;
+
+       *mode_ret = &res_mode_init[default_mode];
+       *depth_ret = default_depth;
+       *options = NULL;
+
+       if (!video_get_video_mode(&xres, &yres, &depth, &refresh, options))
+               return;
+
+       for (i = 0; i < RES_MODES_COUNT; i++) {
+               if (res_mode_init[i].xres == xres &&
+                   res_mode_init[i].yres == yres &&
+                   res_mode_init[i].refresh == refresh) {
+                       *mode_ret = &res_mode_init[i];
+                       *depth_ret = depth;
+                       return;
+               }
+       }
+
+       printf("video-mode %dx%d-%d@%d not available, falling back to %dx%d-%d@%d\n",
+              xres, yres, depth, refresh, (*mode_ret)->xres,
+              (*mode_ret)->yres, *depth_ret, (*mode_ret)->refresh);
+}
+
+/*
+ * Find the named string option within the ',' separated options string, and
+ * store its value in dest.
+ *
+ * @options: ',' separated options string
+ * @name: name of the option to look for
+ * @dest: destination buffer to store the value of the option in
+ * @dest_len: length of dest
+ * @def: value to store in dest if the option is not present in options
+ */
+void video_get_option_string(const char *options, const char *name,
+                            char *dest, int dest_len, const char *def)
+{
+       const char *p = options;
+       const int name_len = strlen(name);
+       int i, len;
+
+       while (p && (i = video_get_param_len(p, ',')) != 0) {
+               if (strncmp(p, name, name_len) == 0 && p[name_len] == '=') {
+                       len = i - (name_len + 1);
+                       if (len >= dest_len)
+                               len = dest_len - 1;
+                       memcpy(dest, &p[name_len + 1], len);
+                       dest[len] = 0;
+                       return;
+               }
+               p += i;
+               if (*p != 0)
+                       p++;    /* skip ',' */
+       }
+       strcpy(dest, def);
+}
+
+/*
+ * Find the named integer option within the ',' separated options string, and
+ * return its value.
+ *
+ * @options: ',' separated options string
+ * @name: name of the option to look for
+ * @def: value to return if the option is not present in options
+ */
+int video_get_option_int(const char *options, const char *name, int def)
+{
+       const char *p = options;
+       const int name_len = strlen(name);
+       int i;
+
+       while (p && (i = video_get_param_len(p, ',')) != 0) {
+               if (strncmp(p, name, name_len) == 0 && p[name_len] == '=')
+                       return simple_strtoul(&p[name_len + 1], NULL, 10);
+
+               p += i;
+               if (*p != 0)
+                       p++;    /* skip ',' */
+       }
+       return def;
+}
+
+/**
+ * Convert an EDID detailed timing to a struct ctfb_res_modes
+ *
+ * @param t            The EDID detailed timing to be converted
+ * @param mode         Returns the converted timing
+ *
+ * @return 0 on success, or a negative errno on error
+ */
+int video_edid_dtd_to_ctfb_res_modes(struct edid_detailed_timing *t,
+                                    struct ctfb_res_modes *mode)
+{
+       int margin, h_total, v_total;
+
+       /* Check all timings are non 0 */
+       if (EDID_DETAILED_TIMING_PIXEL_CLOCK(*t) == 0 ||
+           EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*t) == 0 ||
+           EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*t) == 0 ||
+           EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*t) == 0 ||
+           EDID_DETAILED_TIMING_VERTICAL_BLANKING(*t) == 0 ||
+           EDID_DETAILED_TIMING_HSYNC_OFFSET(*t) == 0 ||
+           EDID_DETAILED_TIMING_VSYNC_OFFSET(*t) == 0 ||
+           /* 3d formats are not supported */
+           EDID_DETAILED_TIMING_FLAG_STEREO(*t) != 0)
+               return -EINVAL;
+
+       mode->xres = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*t);
+       mode->yres = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*t);
+
+       h_total = mode->xres + EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*t);
+       v_total = mode->yres + EDID_DETAILED_TIMING_VERTICAL_BLANKING(*t);
+       mode->refresh = EDID_DETAILED_TIMING_PIXEL_CLOCK(*t) /
+                       (h_total * v_total);
+
+       mode->pixclock_khz = EDID_DETAILED_TIMING_PIXEL_CLOCK(*t) / 1000;
+       mode->pixclock = 1000000000L / mode->pixclock_khz;
+
+       mode->right_margin = EDID_DETAILED_TIMING_HSYNC_OFFSET(*t);
+       mode->hsync_len = EDID_DETAILED_TIMING_HSYNC_PULSE_WIDTH(*t);
+       margin = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*t) -
+                       (mode->right_margin + mode->hsync_len);
+       if (margin <= 0)
+               return -EINVAL;
+
+       mode->left_margin = margin;
+
+       mode->lower_margin = EDID_DETAILED_TIMING_VSYNC_OFFSET(*t);
+       mode->vsync_len = EDID_DETAILED_TIMING_VSYNC_PULSE_WIDTH(*t);
+       margin = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*t) -
+                       (mode->lower_margin + mode->vsync_len);
+       if (margin <= 0)
+               return -EINVAL;
+
+       mode->upper_margin = margin;
+
+       mode->sync = 0;
+       if (EDID_DETAILED_TIMING_FLAG_HSYNC_POLARITY(*t))
+               mode->sync |= FB_SYNC_HOR_HIGH_ACT;
+       if (EDID_DETAILED_TIMING_FLAG_VSYNC_POLARITY(*t))
+               mode->sync |= FB_SYNC_VERT_HIGH_ACT;
+
+       if (EDID_DETAILED_TIMING_FLAG_INTERLACED(*t))
+               mode->vmode = FB_VMODE_INTERLACED;
+       else
+               mode->vmode = FB_VMODE_NONINTERLACED;
+
+       return 0;
+}