media: v4l2-fwnode: Add helper to parse device properties
authorJacopo Mondi <jacopo@jmondi.org>
Sat, 9 May 2020 09:04:49 +0000 (11:04 +0200)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Mon, 18 May 2020 13:34:56 +0000 (15:34 +0200)
Add an helper function to parse common device properties in the same
way as v4l2_fwnode_endpoint_parse() parses common endpoint properties.

Parse the 'rotation' and 'orientation' properties from the firmware
interface.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
drivers/media/v4l2-core/v4l2-fwnode.c
include/media/v4l2-fwnode.h

index e1f273ae85223628a4ac9ea07e88456656aed75f..6cd52e1ef5e4746bd891c12ed4144a66f45fcf85 100644 (file)
@@ -756,6 +756,48 @@ err:
 }
 EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_add_link);
 
+int v4l2_fwnode_device_parse(struct device *dev,
+                            struct v4l2_fwnode_device_properties *props)
+{
+       struct fwnode_handle *fwnode = dev_fwnode(dev);
+       u32 val;
+       int ret;
+
+       memset(props, 0, sizeof(*props));
+
+       props->orientation = V4L2_FWNODE_PROPERTY_UNSET;
+       ret = fwnode_property_read_u32(fwnode, "orientation", &val);
+       if (!ret) {
+               switch (val) {
+               case V4L2_FWNODE_ORIENTATION_FRONT:
+               case V4L2_FWNODE_ORIENTATION_BACK:
+               case V4L2_FWNODE_ORIENTATION_EXTERNAL:
+                       break;
+               default:
+                       dev_warn(dev, "Unsupported device orientation: %u\n", val);
+                       return -EINVAL;
+               }
+
+               props->orientation = val;
+               dev_dbg(dev, "device orientation: %u\n", val);
+       }
+
+       props->rotation = V4L2_FWNODE_PROPERTY_UNSET;
+       ret = fwnode_property_read_u32(fwnode, "rotation", &val);
+       if (!ret) {
+               if (val >= 360) {
+                       dev_warn(dev, "Unsupported device rotation: %u\n", val);
+                       return -EINVAL;
+               }
+
+               props->rotation = val;
+               dev_dbg(dev, "device rotation: %u\n", val);
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_fwnode_device_parse);
+
 static int
 v4l2_async_notifier_fwnode_parse_endpoint(struct device *dev,
                                          struct v4l2_async_notifier *notifier,
index fad7a6480bf94b9326bad4b2da5b4722341618d0..c47b70636e4293ef9efe3f4243ab754882c5f660 100644 (file)
@@ -108,6 +108,36 @@ struct v4l2_fwnode_endpoint {
        unsigned int nr_of_link_frequencies;
 };
 
+/**
+ * V4L2_FWNODE_PROPERTY_UNSET - identify a non initialized property
+ *
+ * All properties in &struct v4l2_fwnode_device_properties are initialized
+ * to this value.
+ */
+#define V4L2_FWNODE_PROPERTY_UNSET   (-1U)
+
+/**
+ * enum v4l2_fwnode_orientation - possible device orientation
+ * @V4L2_FWNODE_ORIENTATION_FRONT: device installed on the front side
+ * @V4L2_FWNODE_ORIENTATION_BACK: device installed on the back side
+ * @V4L2_FWNODE_ORIENTATION_EXTERNAL: device externally located
+ */
+enum v4l2_fwnode_orientation {
+       V4L2_FWNODE_ORIENTATION_FRONT,
+       V4L2_FWNODE_ORIENTATION_BACK,
+       V4L2_FWNODE_ORIENTATION_EXTERNAL
+};
+
+/**
+ * struct v4l2_fwnode_device_properties - fwnode device properties
+ * @orientation: device orientation. See &enum v4l2_fwnode_orientation
+ * @rotation: device rotation
+ */
+struct v4l2_fwnode_device_properties {
+       enum v4l2_fwnode_orientation orientation;
+       unsigned int rotation;
+};
+
 /**
  * struct v4l2_fwnode_link - a link between two endpoints
  * @local_node: pointer to device_node of this endpoint
@@ -353,6 +383,23 @@ int v4l2_fwnode_connector_parse(struct fwnode_handle *fwnode,
 int v4l2_fwnode_connector_add_link(struct fwnode_handle *fwnode,
                                   struct v4l2_fwnode_connector *connector);
 
+/**
+ * v4l2_fwnode_device_parse() - parse fwnode device properties
+ * @dev: pointer to &struct device
+ * @props: pointer to &struct v4l2_fwnode_device_properties where to store the
+ *        parsed properties values
+ *
+ * This function parses and validates the V4L2 fwnode device properties from the
+ * firmware interface, and fills the @struct v4l2_fwnode_device_properties
+ * provided by the caller.
+ *
+ * Return:
+ *     % 0 on success
+ *     %-EINVAL if a parsed property value is not valid
+ */
+int v4l2_fwnode_device_parse(struct device *dev,
+                            struct v4l2_fwnode_device_properties *props);
+
 /**
  * typedef parse_endpoint_func - Driver's callback function to be called on
  *     each V4L2 fwnode endpoint.