Re: [PATCH v3] media: add imx355 camera sensor driver

2018-10-01 Thread Sakari Ailus
Hi Bingbu,

On Sat, Sep 29, 2018 at 06:03:54PM +0800, bingbu@intel.com wrote:
> From: Bingbu Cao 
> 
> Add a v4l2 sub-device driver for the Sony imx355 image sensor.
> This is a camera sensor using the i2c bus for control and the
> csi-2 bus for data.
> 
> This driver supports following features:
> 
> - manual exposure and analog/digital gain control support
> - vblank/hblank control support
> - 4 test patterns control support
> - vflip/hflip control support (will impact the output bayer order)
> - support following resolutions:
> - 3268x2448, 3264x2448, 3280x2464 @ 30fps
> - 1940x1096, 1936x1096, 1924x1080, 1920x1080 @ 60fps
> - 1640x1232, 1640x922, 1300x736, 1296x736,
>   1284x720, 1280x720 820x616 @ 120fps
> - support 4 bayer orders output (via change v/hflip)
> - SRGGB10(default), SGRBG10, SGBRG10, SBGGR10
> 
> Cc: Tomasz Figa 
> Cc: Sakari Ailus 
> Signed-off-by: Tianshu Qiu 
> Signed-off-by: Bingbu Cao 
> 
> ---
> 
> This patch is based on sakari's media-tree git:
> https://git.linuxtv.org/sailus/media_tree.git/log/?h=for-4.20-8
> 
> changes since v2:
>  - get CSI-2 link frequencies and external clock
>from firmware
>  - select digital gain mode when streaming
> 
> changes v1 -> v2:
>  - fix some coding style issues - line breaks
>  - add v4l2_ctrl_grab() to prevent v/hflip change
>during streaming
>  - add v4l2 ctrl event (un)subscribe support
>  - correct link frequency
>  - add more info into commit message

Thanks for the update.

I've applied the patch with the following diff, to make it work on systems
without 64-bit division:

diff --git a/drivers/media/i2c/imx355.c b/drivers/media/i2c/imx355.c
index 858cd0f8bc10..6878b444ea78 100644
--- a/drivers/media/i2c/imx355.c
+++ b/drivers/media/i2c/imx355.c
@@ -1360,7 +1360,8 @@ imx355_set_pad_format(struct v4l2_subdev *sd,
*framefmt = fmt->format;
} else {
imx355->cur_mode = mode;
-   pixel_rate = (imx355->link_def_freq * 2 * 4) / 10;
+   pixel_rate = imx355->link_def_freq * 2 * 4;
+   do_div(pixel_rate, 10);
__v4l2_ctrl_s_ctrl_int64(imx355->pixel_rate, pixel_rate);
/* Update limits and set FPS to default */
height = imx355->cur_mode->height;
@@ -1587,7 +1588,8 @@ static int imx355_init_controls(struct imx355 *imx355)
imx355->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
 
/* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
-   pixel_rate = (imx355->link_def_freq * 2 * 4) / 10;
+   pixel_rate = imx355->link_def_freq * 2 * 4;
+   do_div(pixel_rate, 10);
/* By default, PIXEL_RATE is read only */
imx355->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops,
   V4L2_CID_PIXEL_RATE, pixel_rate,

-- 
Kind regards,

Sakari Ailus
sakari.ai...@linux.intel.com


[PATCH v3] media: add imx355 camera sensor driver

2018-09-29 Thread bingbu . cao
From: Bingbu Cao 

Add a v4l2 sub-device driver for the Sony imx355 image sensor.
This is a camera sensor using the i2c bus for control and the
csi-2 bus for data.

This driver supports following features:

- manual exposure and analog/digital gain control support
- vblank/hblank control support
- 4 test patterns control support
- vflip/hflip control support (will impact the output bayer order)
- support following resolutions:
- 3268x2448, 3264x2448, 3280x2464 @ 30fps
- 1940x1096, 1936x1096, 1924x1080, 1920x1080 @ 60fps
- 1640x1232, 1640x922, 1300x736, 1296x736,
  1284x720, 1280x720 820x616 @ 120fps
- support 4 bayer orders output (via change v/hflip)
- SRGGB10(default), SGRBG10, SGBRG10, SBGGR10

Cc: Tomasz Figa 
Cc: Sakari Ailus 
Signed-off-by: Tianshu Qiu 
Signed-off-by: Bingbu Cao 

---

This patch is based on sakari's media-tree git:
https://git.linuxtv.org/sailus/media_tree.git/log/?h=for-4.20-8

changes since v2:
 - get CSI-2 link frequencies and external clock
   from firmware
 - select digital gain mode when streaming

changes v1 -> v2:
 - fix some coding style issues - line breaks
 - add v4l2_ctrl_grab() to prevent v/hflip change
   during streaming
 - add v4l2 ctrl event (un)subscribe support
 - correct link frequency
 - add more info into commit message

---

Conflicts:
MAINTAINERS
drivers/media/i2c/Kconfig
drivers/media/i2c/Makefile
---
 MAINTAINERS|7 +
 drivers/media/i2c/Kconfig  |   11 +
 drivers/media/i2c/Makefile |1 +
 drivers/media/i2c/imx355.c | 1856 
 4 files changed, 1875 insertions(+)
 create mode 100644 drivers/media/i2c/imx355.c

diff --git a/MAINTAINERS b/MAINTAINERS
index f274a9d883bb..4880a9068f1b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13543,6 +13543,13 @@ T: git git://linuxtv.org/media_tree.git
 S: Maintained
 F: drivers/media/i2c/imx319.c
 
+SONY IMX355 SENSOR DRIVER
+M: Tianshu Qiu 
+L: linux-media@vger.kernel.org
+T: git git://linuxtv.org/media_tree.git
+S: Maintained
+F: drivers/media/i2c/imx355.c
+
 SONY MEMORYSTICK CARD SUPPORT
 M: Alex Dubov 
 W: http://tifmxx.berlios.de/
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 603ac087975b..704af210e270 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -625,6 +625,17 @@ config VIDEO_IMX319
  To compile this driver as a module, choose M here: the
  module will be called imx319.
 
+config VIDEO_IMX355
+   tristate "Sony IMX355 sensor support"
+   depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
+   depends on MEDIA_CAMERA_SUPPORT
+   help
+ This is a Video4Linux2 sensor driver for the Sony
+ IMX355 camera.
+
+ To compile this driver as a module, choose M here: the
+ module will be called imx355.
+
 config VIDEO_OV2640
tristate "OmniVision OV2640 sensor support"
depends on VIDEO_V4L2 && I2C
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index d10b438577be..260d4d9ec2a1 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -109,5 +109,6 @@ obj-$(CONFIG_VIDEO_TC358743)+= tc358743.o
 obj-$(CONFIG_VIDEO_IMX258) += imx258.o
 obj-$(CONFIG_VIDEO_IMX274) += imx274.o
 obj-$(CONFIG_VIDEO_IMX319) += imx319.o
+obj-$(CONFIG_VIDEO_IMX355) += imx355.o
 
 obj-$(CONFIG_SDR_MAX2175) += max2175.o
diff --git a/drivers/media/i2c/imx355.c b/drivers/media/i2c/imx355.c
new file mode 100644
index ..858cd0f8bc10
--- /dev/null
+++ b/drivers/media/i2c/imx355.c
@@ -0,0 +1,1856 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2018 Intel Corporation
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define IMX355_REG_MODE_SELECT 0x0100
+#define IMX355_MODE_STANDBY0x00
+#define IMX355_MODE_STREAMING  0x01
+
+/* Chip ID */
+#define IMX355_REG_CHIP_ID 0x0016
+#define IMX355_CHIP_ID 0x0355
+
+/* V_TIMING internal */
+#define IMX355_REG_FLL 0x0340
+#define IMX355_FLL_MAX 0x
+
+/* Exposure control */
+#define IMX355_REG_EXPOSURE0x0202
+#define IMX355_EXPOSURE_MIN1
+#define IMX355_EXPOSURE_STEP   1
+#define IMX355_EXPOSURE_DEFAULT0x0282
+
+/* Analog gain control */
+#define IMX355_REG_ANALOG_GAIN 0x0204
+#define IMX355_ANA_GAIN_MIN0
+#define IMX355_ANA_GAIN_MAX960
+#define IMX355_ANA_GAIN_STEP   1
+#define IMX355_ANA_GAIN_DEFAULT0
+
+/* Digital gain control */
+#define IMX355_REG_DPGA_USE_GLOBAL_GAIN0x3070
+#define IMX355_REG_DIG_GAIN_GLOBAL 0x020e
+#define IMX355_DGTL_GAIN_MIN   256
+#define IMX355_DGTL_GAIN_MAX   4095
+#define IMX355_DGTL_GAIN_STEP  1
+#define IMX355_DGTL_GAIN_DEFAULT   256
+
+/* Test Patter