Skip to contentSkip to conversation

media: soc_camera: ov9640: switch driver to v4l2_async

PATCH New [email protected] 1 participant 9 messages Dec 13, 2018

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

v3 cover media: soc_camera: ov9640: switch driver to v4l2_async

From: Petr Cvek <[email protected]>

This patch series transfer the ov9640 driver from the soc_camera subsystem into a standalone v4l2 driver. There is no changes except the required v4l2_async calls, GPIO allocation, deletion of now unused variables, a change from mdelay() to msleep() and an addition of SPDX identifiers (as suggested in the v1 version RFC).

The config symbol has been changed from CONFIG_SOC_CAMERA_OV9640 to CONFIG_VIDEO_OV9640.

Also as the drivers of the soc_camera seems to be orphaned I'm volunteering as a maintainer of the driver (I own the hardware).

I've found the ov9640 seems to be used at least in (the future) HTC Magician and Palm Zire72. These will need to define power and reset GPIOs and remove the soc_camera definitions. I'm debugging it on magician now (ov9640 was unusable on them since the pxa_camera switched from the soc_camera).

Additional fixes (from v2) are: a fix of the probe error path, variables change to an unsigned type (indexes, lengths), a redefinition of formats array to const and better clarity of code near returns.

Petr Cvek (8):
media: soc_camera: ov9640: move ov9640 out of soc_camera
media: i2c: ov9640: drop soc_camera code and switch to v4l2_async
MAINTAINERS: add Petr Cvek as a maintainer for the ov9640 driver
media: i2c: ov9640: add missing SPDX identifiers
media: i2c: ov9640: change array index or length variables to unsigned
media: i2c: ov9640: add space before return for better clarity
media: i2c: ov9640: make array of supported formats constant
media: i2c: ov9640: fix missing error handling in probe

 MAINTAINERS                          |   6 +
 drivers/media/i2c/Kconfig            |   7 +
 drivers/media/i2c/Makefile           |   1 +
 drivers/media/i2c/ov9640.c           | 776 +++++++++++++++++++++++++++
 drivers/media/i2c/ov9640.h           | 207 +++++++
 drivers/media/i2c/soc_camera/Kconfig |   6 +-
 6 files changed, 1001 insertions(+), 2 deletions(-)
 create mode 100644 drivers/media/i2c/ov9640.c
 create mode 100644 drivers/media/i2c/ov9640.h
Signature · 1 line
2.20.0
author maintainer
#1 lore raw

PATCH v3 1/8 media: soc_camera: ov9640: move ov9640 out of soc_camera

From: Petr Cvek <[email protected]>

Initial part of ov9640 transition from soc_camera subsystem to a standalone v4l2 subdevice. The soc_camera version seems to be used only in Palm Zire72 and in (the future) HTC Magician. On these two devices the support is broken as pxa_camera driver doesn't use soc_camera anymore. The other mentions from git grep are "TODOs" (in board-osk.c) or chip names for unsupported sensors on HW which doesn't use soc_camera at all (irelevant).

Copy the driver files from soc_camera and mark the original ones in the Kconfig description as obsoleted.

Add config option VIDEO_OV9640 to the build files in drivers/media/i2c.

---
 drivers/media/i2c/Kconfig            |   7 +
 drivers/media/i2c/Makefile           |   1 +
 drivers/media/i2c/ov9640.c           | 738 +++++++++++++++++++++++++++
 drivers/media/i2c/ov9640.h           | 208 ++++++++
 drivers/media/i2c/soc_camera/Kconfig |   6 +-
 5 files changed, 958 insertions(+), 2 deletions(-)
 create mode 100644 drivers/media/i2c/ov9640.c
 create mode 100644 drivers/media/i2c/ov9640.h
drivers/media/i2c/Kconfig +7 -0
index 4c936e129500..119aaee5318b 100644
@@ -820,6 +820,13 @@ config VIDEO_OV7740
 	  This is a Video4Linux2 sensor driver for the OmniVision
 	  OV7740 VGA camera sensor.
 
+config VIDEO_OV9640
+	tristate "OmniVision OV9640 sensor support"
+	depends on I2C && VIDEO_V4L2
+	help
+	  This is a Video4Linux2 sensor driver for the OmniVision
+	  OV9640 camera sensor.
+
 config VIDEO_OV9650
 	tristate "OmniVision OV9650/OV9652 sensor support"
 	depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
drivers/media/i2c/Makefile +1 -0
index 65fae7732de0..5639cb2f20aa 100644
@@ -78,6 +78,7 @@ obj-$(CONFIG_VIDEO_OV7640) += ov7640.o
 obj-$(CONFIG_VIDEO_OV7670) += ov7670.o
 obj-$(CONFIG_VIDEO_OV772X) += ov772x.o
 obj-$(CONFIG_VIDEO_OV7740) += ov7740.o
+obj-$(CONFIG_VIDEO_OV9640) += ov9640.o
 obj-$(CONFIG_VIDEO_OV9650) += ov9650.o
 obj-$(CONFIG_VIDEO_OV13858) += ov13858.o
 obj-$(CONFIG_VIDEO_MT9M032) += mt9m032.o
drivers/media/i2c/ov9640.c new file +738 -0

738 lines not on this page · raw message

drivers/media/i2c/ov9640.h new file +208 -0

208 lines not on this page · raw message

drivers/media/i2c/soc_camera/Kconfig +4 -2
index 7c2aabc8a3f6..7d7b801ab2ce 100644
@@ -42,10 +42,12 @@ config SOC_CAMERA_OV772X
 	  This is a ov772x camera driver
 
 config SOC_CAMERA_OV9640
-	tristate "ov9640 camera support"
+	tristate "ov9640 camera support (OBSOLETE)"
+	default n
 	depends on SOC_CAMERA && I2C
 	help
-	  This is a ov9640 camera driver
+	  This is an obsoleted version of ov9640 camera driver. Please use
+	  the v4l2 standalone one (VIDEO_OV9640).
 
 config SOC_CAMERA_OV9740
 	tristate "ov9740 camera support"
Signature · 1 line
2.20.0
author maintainer
#2 lore raw

PATCH v3 2/8 media: i2c: ov9640: drop soc_camera code and switch to v4l2_async

In reply to [email protected]

From: Petr Cvek <[email protected]>

This patch removes the dependency on an obsoleted soc_camera from ov9640 driver and changes the code to be a standalone v4l2 async subdevice. It also adds GPIO allocations for power and reset signals (as they are not handled by soc_camera now).

The values for waiting on GPIOs (reset and power) settling down were taken from the datasheet (> 1 ms after HW/SW reset). The upper limit was chosen as an arbitrary value. Also one occurrence of mdelay() was changed to msleep(). The delays were successfully tested on a real hardware.

The patch makes ov9640 sensor again compatible with the pxa_camera driver.

---
 drivers/media/i2c/ov9640.c | 76 ++++++++++++++++++++++++++------------
 drivers/media/i2c/ov9640.h |  2 +
 2 files changed, 55 insertions(+), 23 deletions(-)
drivers/media/i2c/ov9640.c +53 -23
index eb91b8240083..1f7cf01d037f 100644
@@ -9,6 +9,7 @@
  * Kuninori Morimoto <[email protected]>
  *
  * Based on ov7670 and soc_camera_platform driver,
+ * transition from soc_camera to pxa_camera based on mt9m111
  *
  * Copyright 2006-7 Jonathan Corbet <[email protected]>
  * Copyright (C) 2008 Magnus Damm
@@ -27,10 +28,14 @@
 #include <linux/v4l2-mediabus.h>
 #include <linux/videodev2.h>
 
-#include <media/soc_camera.h>
+#include <media/v4l2-async.h>
 #include <media/v4l2-clk.h>
 #include <media/v4l2-common.h>
 #include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-event.h>
+
+#include <linux/gpio/consumer.h>
 
 #include "ov9640.h"
 
@@ -323,11 +328,23 @@ static int ov9640_set_register(struct v4l2_subdev *sd,
 
 static int ov9640_s_power(struct v4l2_subdev *sd, int on)
 {
-	struct i2c_client *client = v4l2_get_subdevdata(sd);
-	struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
 	struct ov9640_priv *priv = to_ov9640_sensor(sd);
-
-	return soc_camera_set_power(&client->dev, ssdd, priv->clk, on);
+	int ret = 0;
+
+	if (on) {
+		gpiod_set_value(priv->gpio_power, 1);
+		usleep_range(1000, 2000);
+		ret = v4l2_clk_enable(priv->clk);
+		usleep_range(1000, 2000);
+		gpiod_set_value(priv->gpio_reset, 0);
+	} else {
+		gpiod_set_value(priv->gpio_reset, 1);
+		usleep_range(1000, 2000);
+		v4l2_clk_disable(priv->clk);
+		usleep_range(1000, 2000);
+		gpiod_set_value(priv->gpio_power, 0);
+	}
+	return ret;
 }
 
 /* select nearest higher resolution for capture */
@@ -475,7 +492,7 @@ static int ov9640_prog_dflt(struct i2c_client *client)
 	}
 
 	/* wait for the changes to actually happen, 140ms are not enough yet */
-	mdelay(150);
+	msleep(150);
 
 	return 0;
 }
@@ -630,14 +647,10 @@ static const struct v4l2_subdev_core_ops ov9640_core_ops = {
 static int ov9640_g_mbus_config(struct v4l2_subdev *sd,
 				struct v4l2_mbus_config *cfg)
 {
-	struct i2c_client *client = v4l2_get_subdevdata(sd);
-	struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
-
 	cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
 		V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
 		V4L2_MBUS_DATA_ACTIVE_HIGH;
 	cfg->type = V4L2_MBUS_PARALLEL;
-	cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
 
 	return 0;
 }
@@ -666,18 +679,27 @@ static int ov9640_probe(struct i2c_client *client,
 			const struct i2c_device_id *did)
 {
 	struct ov9640_priv *priv;
-	struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
 	int ret;
 
-	if (!ssdd) {
-		dev_err(&client->dev, "Missing platform_data for driver\n");
-		return -EINVAL;
-	}
-
-	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
+	priv = devm_kzalloc(&client->dev, sizeof(*priv),
+			    GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
 
+	priv->gpio_power = devm_gpiod_get(&client->dev, "Camera power",
+					  GPIOD_OUT_LOW);
+	if (IS_ERR_OR_NULL(priv->gpio_power)) {
+		ret = PTR_ERR(priv->gpio_power);
+		return ret;
+	}
+
+	priv->gpio_reset = devm_gpiod_get(&client->dev, "Camera reset",
+					  GPIOD_OUT_HIGH);
+	if (IS_ERR_OR_NULL(priv->gpio_reset)) {
+		ret = PTR_ERR(priv->gpio_reset);
+		return ret;
+	}
+
 	v4l2_i2c_subdev_init(&priv->subdev, client, &ov9640_subdev_ops);
 
 	v4l2_ctrl_handler_init(&priv->hdl, 2);
@@ -696,12 +718,20 @@ static int ov9640_probe(struct i2c_client *client,
 	}
 
 	ret = ov9640_video_probe(client);
-	if (ret) {
-		v4l2_clk_put(priv->clk);
-eclkget:
-		v4l2_ctrl_handler_free(&priv->hdl);
-	}
+	if (ret)
+		goto eprobe;
 
+	priv->subdev.dev = &client->dev;
+	ret = v4l2_async_register_subdev(&priv->subdev);
+	if (ret)
+		goto eprobe;
+
+	return 0;
+
+eprobe:
+	v4l2_clk_put(priv->clk);
+eclkget:
+	v4l2_ctrl_handler_free(&priv->hdl);
 	return ret;
 }
 
@@ -711,7 +741,7 @@ static int ov9640_remove(struct i2c_client *client)
 	struct ov9640_priv *priv = to_ov9640_sensor(sd);
 
 	v4l2_clk_put(priv->clk);
-	v4l2_device_unregister_subdev(&priv->subdev);
+	v4l2_async_unregister_subdev(&priv->subdev);
 	v4l2_ctrl_handler_free(&priv->hdl);
 	return 0;
 }
drivers/media/i2c/ov9640.h +2 -0
index 65d13ff17536..be5e4b29ac69 100644
@@ -200,6 +200,8 @@ struct ov9640_priv {
 	struct v4l2_subdev		subdev;
 	struct v4l2_ctrl_handler	hdl;
 	struct v4l2_clk			*clk;
+	struct gpio_desc		*gpio_power;
+	struct gpio_desc		*gpio_reset;
 
 	int				model;
 	int				revision;
Signature · 1 line
2.20.0
author maintainer
#3 lore raw

PATCH v3 3/8 MAINTAINERS: add Petr Cvek as a maintainer for the ov9640 driver

In reply to [email protected]

From: Petr Cvek <[email protected]>

The soc_camera drivers are marked as orphaned. Add Petr Cvek as a new maintainer for ov9640 driver after its switch from the soc_camera.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index a2e686819ef8..eda47064ed55 100644
@@ -11146,6 +11146,12 @@ S:	Maintained
 F:	drivers/media/i2c/ov7740.c
 F:	Documentation/devicetree/bindings/media/i2c/ov7740.txt
 
+OMNIVISION OV9640 SENSOR DRIVER
+M:	Petr Cvek <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/media/i2c/ov9640.*
+
 OMNIVISION OV9650 SENSOR DRIVER
 M:	Sakari Ailus <[email protected]>
 R:	Akinobu Mita <[email protected]>
Signature · 1 line
2.20.0
author maintainer
#4 lore raw

PATCH v3 4/8 media: i2c: ov9640: add missing SPDX identifiers

In reply to [email protected]

From: Petr Cvek <[email protected]>

Add missing SPDX identifiers to .c and .h files of the sensor driver.

---
 drivers/media/i2c/ov9640.c | 5 +----
 drivers/media/i2c/ov9640.h | 5 +----
 2 files changed, 2 insertions(+), 8 deletions(-)
drivers/media/i2c/ov9640.c +1 -4
index 1f7cf01d037f..9a6fa609e8c4 100644
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * OmniVision OV96xx Camera Driver
  *
@@ -14,10 +15,6 @@
  * Copyright 2006-7 Jonathan Corbet <[email protected]>
  * Copyright (C) 2008 Magnus Damm
  * Copyright (C) 2008, Guennadi Liakhovetski <[email protected]>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
  */
 
 #include <linux/init.h>
drivers/media/i2c/ov9640.h +1 -4
index be5e4b29ac69..a8ed6992c1a8 100644
@@ -1,11 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 */
 /*
  * OmniVision OV96xx Camera Header File
  *
  * Copyright (C) 2009 Marek Vasut <[email protected]>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
  */
 
 #ifndef	__DRIVERS_MEDIA_VIDEO_OV9640_H__
Signature · 1 line
2.20.0
author maintainer
#5 lore raw

PATCH v3 5/8 media: i2c: ov9640: change array index or length variables to unsigned

In reply to [email protected]

From: Petr Cvek <[email protected]>

The driver uses variables to store frame resolutions and to indexing various arrays. These should be unsigned.

---
 drivers/media/i2c/ov9640.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/media/i2c/ov9640.c +8 -6
index 9a6fa609e8c4..08f3f8247759 100644
@@ -347,10 +347,10 @@ static int ov9640_s_power(struct v4l2_subdev *sd, int on)
 /* select nearest higher resolution for capture */
 static void ov9640_res_roundup(u32 *width, u32 *height)
 {
-	int i;
+	unsigned int i;
 	enum { QQCIF, QQVGA, QCIF, QVGA, CIF, VGA, SXGA };
-	static const int res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
-	static const int res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
+	static const u32 res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
+	static const u32 res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
 
 	for (i = 0; i < ARRAY_SIZE(res_x); i++) {
 		if (res_x[i] >= *width && res_y[i] >= *height) {
@@ -393,8 +393,9 @@ static int ov9640_write_regs(struct i2c_client *client, u32 width,
 		u32 code, struct ov9640_reg_alt *alts)
 {
 	const struct ov9640_reg	*ov9640_regs, *matrix_regs;
-	int			ov9640_regs_len, matrix_regs_len;
-	int			i, ret;
+	unsigned int		ov9640_regs_len, matrix_regs_len;
+	unsigned int		i;
+	int			ret;
 	u8			val;
 
 	/* select register configuration for given resolution */
@@ -479,7 +480,8 @@ static int ov9640_write_regs(struct i2c_client *client, u32 width,
 /* program default register values */
 static int ov9640_prog_dflt(struct i2c_client *client)
 {
-	int i, ret;
+	unsigned int i;
+	int ret;
 
 	for (i = 0; i < ARRAY_SIZE(ov9640_regs_dflt); i++) {
 		ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg,
Signature · 1 line
2.20.0
author maintainer
#6 lore raw

PATCH v3 6/8 media: i2c: ov9640: add space before return for better clarity

In reply to [email protected]

From: Petr Cvek <[email protected]>

Some returns were adjoined to unrelated code blocks. This patch adds a space inbetween.

---
 drivers/media/i2c/ov9640.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/media/i2c/ov9640.c +5 -0
index 08f3f8247759..2839aa3b4fb4 100644
@@ -286,6 +286,7 @@ static int ov9640_s_ctrl(struct v4l2_ctrl *ctrl)
 							OV9640_MVFP_H, 0);
 		return ov9640_reg_rmw(client, OV9640_MVFP, 0, OV9640_MVFP_H);
 	}
+
 	return -EINVAL;
 }
 
@@ -341,6 +342,7 @@ static int ov9640_s_power(struct v4l2_subdev *sd, int on)
 		usleep_range(1000, 2000);
 		gpiod_set_value(priv->gpio_power, 0);
 	}
+
 	return ret;
 }
 
@@ -545,6 +547,7 @@ static int ov9640_set_fmt(struct v4l2_subdev *sd,
 		return ov9640_s_fmt(sd, mf);
 
 	cfg->try_fmt = *mf;
+
 	return 0;
 }
 
@@ -556,6 +559,7 @@ static int ov9640_enum_mbus_code(struct v4l2_subdev *sd,
 		return -EINVAL;
 
 	code->code = ov9640_codes[code->index];
+
 	return 0;
 }
 
@@ -731,6 +735,7 @@ static int ov9640_probe(struct i2c_client *client,
 	v4l2_clk_put(priv->clk);
 eclkget:
 	v4l2_ctrl_handler_free(&priv->hdl);
+
 	return ret;
 }
 
Signature · 1 line
2.20.0
author maintainer
#7 lore raw

PATCH v3 7/8 media: i2c: ov9640: make array of supported formats constant

In reply to [email protected]

From: Petr Cvek <[email protected]>

An array which defines sensor's supported formats is not written anywhere, so it can be constant.

---
 drivers/media/i2c/ov9640.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/ov9640.c +1 -1
index 2839aa3b4fb4..9739fa8d433a 100644
@@ -161,7 +161,7 @@ static const struct ov9640_reg ov9640_regs_rgb[] = {
 	{ OV9640_MTXS,	0x65 },
 };
 
-static u32 ov9640_codes[] = {
+static const u32 ov9640_codes[] = {
 	MEDIA_BUS_FMT_UYVY8_2X8,
 	MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
 	MEDIA_BUS_FMT_RGB565_2X8_LE,
Signature · 1 line
2.20.0
author maintainer
#8 lore raw

PATCH v3 8/8 media: i2c: ov9640: fix missing error handling in probe

In reply to [email protected]

From: Petr Cvek <[email protected]>

Control handlers registration lacked error path with v4l2_ctrl_handler_free() call. Fix it by using goto to alread existing v4l2_ctrl_handler_free() call.

---
 drivers/media/i2c/ov9640.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/media/i2c/ov9640.c +8 -4
index 9739fa8d433a..c183273fd332 100644
@@ -710,14 +710,18 @@ static int ov9640_probe(struct i2c_client *client,
 			V4L2_CID_VFLIP, 0, 1, 1, 0);
 	v4l2_ctrl_new_std(&priv->hdl, &ov9640_ctrl_ops,
 			V4L2_CID_HFLIP, 0, 1, 1, 0);
+
+	if (priv->hdl.error) {
+		ret = priv->hdl.error;
+		goto ectrlinit;
+	}
+
 	priv->subdev.ctrl_handler = &priv->hdl;
-	if (priv->hdl.error)
-		return priv->hdl.error;
 
 	priv->clk = v4l2_clk_get(&client->dev, "mclk");
 	if (IS_ERR(priv->clk)) {
 		ret = PTR_ERR(priv->clk);
-		goto eclkget;
+		goto ectrlinit;
 	}
 
 	ret = ov9640_video_probe(client);
@@ -733,7 +737,7 @@ static int ov9640_probe(struct i2c_client *client,
 
 eprobe:
 	v4l2_clk_put(priv->clk);
-eclkget:
+ectrlinit:
 	v4l2_ctrl_handler_free(&priv->hdl);
 
 	return ret;
Signature · 1 line
2.20.0