drm: add convenience function to create an enum property
Sascha Hauer [Mon, 6 Feb 2012 09:58:17 +0000 (10:58 +0100)]
Creating an enum property is a common pattern, so create
a convenience function for this and use it where appropriate.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Dave Airlie <airlied@redhat.com>

drivers/gpu/drm/drm_crtc.c
drivers/gpu/drm/i915/intel_modes.c
drivers/gpu/drm/nouveau/nouveau_display.c
drivers/gpu/drm/radeon/radeon_display.c
include/drm/drm_crtc.h

index 322bc7b..3fe99c4 100644 (file)
 #include "drm_edid.h"
 #include "drm_fourcc.h"
 
-struct drm_prop_enum_list {
-       int type;
-       char *name;
-};
-
 /* Avoid boilerplate.  I'm tired of typing. */
 #define DRM_ENUM_NAME_FN(fnname, list)                         \
        char *fnname(int val)                                   \
@@ -658,7 +653,6 @@ static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
 {
        struct drm_property *edid;
        struct drm_property *dpms;
-       int i;
 
        /*
         * Standard properties (apply to all connectors)
@@ -668,11 +662,9 @@ static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
                                   "EDID", 0);
        dev->mode_config.edid_property = edid;
 
-       dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
-                                  "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
-       for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
-               drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
-                                     drm_dpms_enum_list[i].name);
+       dpms = drm_property_create_enum(dev, 0,
+                                  "DPMS", drm_dpms_enum_list,
+                                  ARRAY_SIZE(drm_dpms_enum_list));
        dev->mode_config.dpms_property = dpms;
 
        return 0;
@@ -688,30 +680,21 @@ int drm_mode_create_dvi_i_properties(struct drm_device *dev)
 {
        struct drm_property *dvi_i_selector;
        struct drm_property *dvi_i_subconnector;
-       int i;
 
        if (dev->mode_config.dvi_i_select_subconnector_property)
                return 0;
 
        dvi_i_selector =
-               drm_property_create(dev, DRM_MODE_PROP_ENUM,
+               drm_property_create_enum(dev, 0,
                                    "select subconnector",
+                                   drm_dvi_i_select_enum_list,
                                    ARRAY_SIZE(drm_dvi_i_select_enum_list));
-       for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
-               drm_property_add_enum(dvi_i_selector, i,
-                                     drm_dvi_i_select_enum_list[i].type,
-                                     drm_dvi_i_select_enum_list[i].name);
        dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
 
-       dvi_i_subconnector =
-               drm_property_create(dev, DRM_MODE_PROP_ENUM |
-                                   DRM_MODE_PROP_IMMUTABLE,
+       dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
                                    "subconnector",
+                                   drm_dvi_i_subconnector_enum_list,
                                    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
-       for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
-               drm_property_add_enum(dvi_i_subconnector, i,
-                                     drm_dvi_i_subconnector_enum_list[i].type,
-                                     drm_dvi_i_subconnector_enum_list[i].name);
        dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
 
        return 0;
@@ -742,23 +725,17 @@ int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
        /*
         * Basic connector properties
         */
-       tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
+       tv_selector = drm_property_create_enum(dev, 0,
                                          "select subconnector",
+                                         drm_tv_select_enum_list,
                                          ARRAY_SIZE(drm_tv_select_enum_list));
-       for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
-               drm_property_add_enum(tv_selector, i,
-                                     drm_tv_select_enum_list[i].type,
-                                     drm_tv_select_enum_list[i].name);
        dev->mode_config.tv_select_subconnector_property = tv_selector;
 
        tv_subconnector =
-               drm_property_create(dev, DRM_MODE_PROP_ENUM |
-                                   DRM_MODE_PROP_IMMUTABLE, "subconnector",
+               drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
+                                   "subconnector",
+                                   drm_tv_subconnector_enum_list,
                                    ARRAY_SIZE(drm_tv_subconnector_enum_list));
-       for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
-               drm_property_add_enum(tv_subconnector, i,
-                                     drm_tv_subconnector_enum_list[i].type,
-                                     drm_tv_subconnector_enum_list[i].name);
        dev->mode_config.tv_subconnector_property = tv_subconnector;
 
        /*
@@ -845,18 +822,14 @@ EXPORT_SYMBOL(drm_mode_create_tv_properties);
 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
 {
        struct drm_property *scaling_mode;
-       int i;
 
        if (dev->mode_config.scaling_mode_property)
                return 0;
 
        scaling_mode =
-               drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
+               drm_property_create_enum(dev, 0, "scaling mode",
+                               drm_scaling_mode_enum_list,
                                    ARRAY_SIZE(drm_scaling_mode_enum_list));
-       for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
-               drm_property_add_enum(scaling_mode, i,
-                                     drm_scaling_mode_enum_list[i].type,
-                                     drm_scaling_mode_enum_list[i].name);
 
        dev->mode_config.scaling_mode_property = scaling_mode;
 
@@ -874,18 +847,14 @@ EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
 int drm_mode_create_dithering_property(struct drm_device *dev)
 {
        struct drm_property *dithering_mode;
-       int i;
 
        if (dev->mode_config.dithering_mode_property)
                return 0;
 
        dithering_mode =
-               drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
+               drm_property_create_enum(dev, 0, "dithering",
+                               drm_dithering_mode_enum_list,
                                    ARRAY_SIZE(drm_dithering_mode_enum_list));
-       for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
-               drm_property_add_enum(dithering_mode, i,
-                                     drm_dithering_mode_enum_list[i].type,
-                                     drm_dithering_mode_enum_list[i].name);
        dev->mode_config.dithering_mode_property = dithering_mode;
 
        return 0;
@@ -902,20 +871,15 @@ EXPORT_SYMBOL(drm_mode_create_dithering_property);
 int drm_mode_create_dirty_info_property(struct drm_device *dev)
 {
        struct drm_property *dirty_info;
-       int i;
 
        if (dev->mode_config.dirty_info_property)
                return 0;
 
        dirty_info =
-               drm_property_create(dev, DRM_MODE_PROP_ENUM |
-                                   DRM_MODE_PROP_IMMUTABLE,
+               drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
                                    "dirty",
+                                   drm_dirty_info_enum_list,
                                    ARRAY_SIZE(drm_dirty_info_enum_list));
-       for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
-               drm_property_add_enum(dirty_info, i,
-                                     drm_dirty_info_enum_list[i].type,
-                                     drm_dirty_info_enum_list[i].name);
        dev->mode_config.dirty_info_property = dirty_info;
 
        return 0;
@@ -2629,6 +2593,34 @@ fail:
 }
 EXPORT_SYMBOL(drm_property_create);
 
+struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
+                                        const char *name,
+                                        const struct drm_prop_enum_list *props,
+                                        int num_values)
+{
+       struct drm_property *property;
+       int i, ret;
+
+       flags |= DRM_MODE_PROP_ENUM;
+
+       property = drm_property_create(dev, flags, name, num_values);
+       if (!property)
+               return NULL;
+
+       for (i = 0; i < num_values; i++) {
+               ret = drm_property_add_enum(property, i,
+                                     props[i].type,
+                                     props[i].name);
+               if (ret) {
+                       drm_property_destroy(dev, property);
+                       return NULL;
+               }
+       }
+
+       return property;
+}
+EXPORT_SYMBOL(drm_property_create_enum);
+
 int drm_property_add_enum(struct drm_property *property, int index,
                          uint64_t value, const char *name)
 {
index be2c6fe..961f75d 100644 (file)
@@ -83,10 +83,10 @@ int intel_ddc_get_modes(struct drm_connector *connector,
        return ret;
 }
 
-static const char *force_audio_names[] = {
-       "off",
-       "auto",
-       "on",
+static const struct drm_prop_enum_list force_audio_names[] = {
+       { -1, "off" },
+       {  0, "auto" },
+       {  1, "on" },
 };
 
 void
@@ -95,27 +95,24 @@ intel_attach_force_audio_property(struct drm_connector *connector)
        struct drm_device *dev = connector->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_property *prop;
-       int i;
 
        prop = dev_priv->force_audio_property;
        if (prop == NULL) {
-               prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
+               prop = drm_property_create_enum(dev, 0,
                                           "audio",
+                                          force_audio_names,
                                           ARRAY_SIZE(force_audio_names));
                if (prop == NULL)
                        return;
 
-               for (i = 0; i < ARRAY_SIZE(force_audio_names); i++)
-                       drm_property_add_enum(prop, i, i-1, force_audio_names[i]);
-
                dev_priv->force_audio_property = prop;
        }
        drm_connector_attach_property(connector, prop, 0);
 }
 
-static const char *broadcast_rgb_names[] = {
-       "Full",
-       "Limited 16:235",
+static const struct drm_prop_enum_list broadcast_rgb_names[] = {
+       { 0, "Full" },
+       { 1, "Limited 16:235" },
 };
 
 void
@@ -124,19 +121,16 @@ intel_attach_broadcast_rgb_property(struct drm_connector *connector)
        struct drm_device *dev = connector->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_property *prop;
-       int i;
 
        prop = dev_priv->broadcast_rgb_property;
        if (prop == NULL) {
-               prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
+               prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
                                           "Broadcast RGB",
+                                          broadcast_rgb_names,
                                           ARRAY_SIZE(broadcast_rgb_names));
                if (prop == NULL)
                        return;
 
-               for (i = 0; i < ARRAY_SIZE(broadcast_rgb_names); i++)
-                       drm_property_add_enum(prop, i, i, broadcast_rgb_names[i]);
-
                dev_priv->broadcast_rgb_property = prop;
        }
 
index 795a9e3..cc94f3c 100644 (file)
@@ -155,20 +155,20 @@ static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
 };
 
 
-struct drm_prop_enum_list {
+struct nouveau_drm_prop_enum_list {
        u8 gen_mask;
        int type;
        char *name;
 };
 
-static struct drm_prop_enum_list underscan[] = {
+static struct nouveau_drm_prop_enum_list underscan[] = {
        { 6, UNDERSCAN_AUTO, "auto" },
        { 6, UNDERSCAN_OFF, "off" },
        { 6, UNDERSCAN_ON, "on" },
        {}
 };
 
-static struct drm_prop_enum_list dither_mode[] = {
+static struct nouveau_drm_prop_enum_list dither_mode[] = {
        { 7, DITHERING_MODE_AUTO, "auto" },
        { 7, DITHERING_MODE_OFF, "off" },
        { 1, DITHERING_MODE_ON, "on" },
@@ -178,7 +178,7 @@ static struct drm_prop_enum_list dither_mode[] = {
        {}
 };
 
-static struct drm_prop_enum_list dither_depth[] = {
+static struct nouveau_drm_prop_enum_list dither_depth[] = {
        { 6, DITHERING_DEPTH_AUTO, "auto" },
        { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
        { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
@@ -186,7 +186,7 @@ static struct drm_prop_enum_list dither_depth[] = {
 };
 
 #define PROP_ENUM(p,gen,n,list) do {                                           \
-       struct drm_prop_enum_list *l = (list);                                 \
+       struct nouveau_drm_prop_enum_list *l = (list);                         \
        int c = 0;                                                             \
        while (l->gen_mask) {                                                  \
                if (l->gen_mask & (1 << (gen)))                                \
index 8c49fef..54629fa 100644 (file)
@@ -1124,11 +1124,6 @@ static const struct drm_mode_config_funcs radeon_mode_funcs = {
        .output_poll_changed = radeon_output_poll_changed
 };
 
-struct drm_prop_enum_list {
-       int type;
-       char *name;
-};
-
 static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
 {      { 0, "driver" },
        { 1, "bios" },
@@ -1153,7 +1148,7 @@ static struct drm_prop_enum_list radeon_underscan_enum_list[] =
 
 static int radeon_modeset_create_props(struct radeon_device *rdev)
 {
-       int i, sz;
+       int sz;
 
        if (rdev->is_atom_bios) {
                rdev->mode_info.coherent_mode_property =
@@ -1170,15 +1165,9 @@ static int radeon_modeset_create_props(struct radeon_device *rdev)
        if (!ASIC_IS_AVIVO(rdev)) {
                sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
                rdev->mode_info.tmds_pll_property =
-                       drm_property_create(rdev->ddev,
-                                           DRM_MODE_PROP_ENUM,
-                                           "tmds_pll", sz);
-               for (i = 0; i < sz; i++) {
-                       drm_property_add_enum(rdev->mode_info.tmds_pll_property,
-                                             i,
-                                             radeon_tmds_pll_enum_list[i].type,
-                                             radeon_tmds_pll_enum_list[i].name);
-               }
+                       drm_property_create_enum(rdev->ddev, 0,
+                                           "tmds_pll",
+                                           radeon_tmds_pll_enum_list, sz);
        }
 
        rdev->mode_info.load_detect_property =
@@ -1194,27 +1183,15 @@ static int radeon_modeset_create_props(struct radeon_device *rdev)
 
        sz = ARRAY_SIZE(radeon_tv_std_enum_list);
        rdev->mode_info.tv_std_property =
-               drm_property_create(rdev->ddev,
-                                   DRM_MODE_PROP_ENUM,
-                                   "tv standard", sz);
-       for (i = 0; i < sz; i++) {
-               drm_property_add_enum(rdev->mode_info.tv_std_property,
-                                     i,
-                                     radeon_tv_std_enum_list[i].type,
-                                     radeon_tv_std_enum_list[i].name);
-       }
+               drm_property_create_enum(rdev->ddev, 0,
+                                   "tv standard",
+                                   radeon_tv_std_enum_list, sz);
 
        sz = ARRAY_SIZE(radeon_underscan_enum_list);
        rdev->mode_info.underscan_property =
-               drm_property_create(rdev->ddev,
-                                   DRM_MODE_PROP_ENUM,
-                                   "underscan", sz);
-       for (i = 0; i < sz; i++) {
-               drm_property_add_enum(rdev->mode_info.underscan_property,
-                                     i,
-                                     radeon_underscan_enum_list[i].type,
-                                     radeon_underscan_enum_list[i].name);
-       }
+               drm_property_create_enum(rdev->ddev, 0,
+                                   "underscan",
+                                   radeon_underscan_enum_list, sz);
 
        rdev->mode_info.underscan_hborder_property =
                drm_property_create(rdev->ddev,
index 8d593ad..3b93cdc 100644 (file)
@@ -807,6 +807,10 @@ struct drm_mode_config {
 #define obj_to_blob(x) container_of(x, struct drm_property_blob, base)
 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
 
+struct drm_prop_enum_list {
+       int type;
+       char *name;
+};
 
 extern void drm_crtc_init(struct drm_device *dev,
                          struct drm_crtc *crtc,
@@ -904,6 +908,10 @@ extern int drm_connector_attach_property(struct drm_connector *connector,
                                      struct drm_property *property, uint64_t init_val);
 extern struct drm_property *drm_property_create(struct drm_device *dev, int flags,
                                                const char *name, int num_values);
+extern struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
+                                        const char *name,
+                                        const struct drm_prop_enum_list *props,
+                                        int num_values);
 extern void drm_property_destroy(struct drm_device *dev, struct drm_property *property);
 extern int drm_property_add_enum(struct drm_property *property, int index,
                                 uint64_t value, const char *name);