media: imx208: Add imx208 camera sensor driver
PATCH v2 1/1 media: imx208: Add imx208 camera sensor driver
From: "Chen, Ping-chung" <[email protected]>
Add a V4L2 sub-device driver for the Sony IMX208 image sensor. This is a camera sensor using the I2C bus for control and the CSI-2 bus for data.
---
since v1:
-- Update the function media_entity_pads_init for upstreaming. -- Change the structure name mutex as imx208_mx.
-- Refine the control flow of test pattern function.
-- vflip/hflip control support (will impact the output bayer order) -- support 4 bayer orders output (via change v/hflip)
- SRGGB10(default), SGRBG10, SGBRG10, SBGGR10
-- Simplify error handling in the set_stream function.
MAINTAINERS | 7 + drivers/media/i2c/Kconfig | 11 + drivers/media/i2c/Makefile | 1 + drivers/media/i2c/imx208.c | 984 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1003 insertions(+) create mode 100644 drivers/media/i2c/imx208.c
MAINTAINERS +7 -0
@@ -13268,6 +13268,13 @@ S: Maintained F: drivers/ssb/ F: include/linux/ssb/ +SONY IMX208 SENSOR DRIVER +M: Sakari Ailus <[email protected]> +L: [email protected] +T: git git://linuxtv.org/media_tree.git +S: Maintained +F: drivers/media/i2c/imx208.c + SONY IMX258 SENSOR DRIVER M: Sakari Ailus <[email protected]> L: [email protected]
drivers/media/i2c/Kconfig +11 -0
@@ -595,6 +595,17 @@ config VIDEO_APTINA_PLL config VIDEO_SMIAPP_PLL tristate +config VIDEO_IMX208 + tristate "Sony IMX208 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 + IMX208 camera. + + To compile this driver as a module, choose M here: the + module will be called imx208. + config VIDEO_IMX258 tristate "Sony IMX258 sensor support" depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
drivers/media/i2c/Makefile +1 -0
@@ -104,6 +104,7 @@ obj-$(CONFIG_VIDEO_I2C) += video-i2c.o obj-$(CONFIG_VIDEO_ML86V7667) += ml86v7667.o obj-$(CONFIG_VIDEO_OV2659) += ov2659.o obj-$(CONFIG_VIDEO_TC358743) += tc358743.o +obj-$(CONFIG_VIDEO_IMX208) += imx208.o obj-$(CONFIG_VIDEO_IMX258) += imx258.o obj-$(CONFIG_VIDEO_IMX274) += imx274.o
drivers/media/i2c/imx208.c new file +984 -0
984 lines not on this page · raw message
Signature · 1 line
1.9.1
Hi Ping-chung,
Quoted 7 lines · Ping-chung Chen
From: "Chen, Ping-chung" <[email protected]>
Add a V4L2 sub-device driver for the Sony IMX208 image sensor. This is a camera sensor using the I2C bus for control and the CSI-2 bus for data.
Please see my comments inline.
[snip]
Quoted 10 lines
drivers/media/i2c/imx208.c new file +4
-0@@ -0,0 +1,984 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2018 Intel Corporation + +#include <linux/acpi.h>
drivers/media/i2c/imx208.c line 14
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+#include <linux/delay.h> +#include <linux/i2c.h> +#include <linux/module.h> +#include <linux/pm_runtime.h> +#include <media/v4l2-ctrls.h> +#include <media/v4l2-device.h> +#include <asm/unaligned.h> + +#define IMX208_REG_VALUE_08BIT 1 +#define IMX208_REG_VALUE_16BIT 2
We don't need to define these. It's obvious that 8 bits is 1 byte and 16 bits are 2 bytes.
Quoted 4 lines
drivers/media/i2c/imx208.c new file +4
-0+ +#define IMX208_REG_MODE_SELECT 0x0100 +#define IMX208_MODE_STANDBY 0x00 +#define IMX208_MODE_STREAMING 0x01
[snip]
drivers/media/i2c/imx208.c line 69
Quoted 7 lines
drivers/media/i2c/imx208.c new file +7
-0+/* Test Pattern Control */ +#define IMX208_REG_TEST_PATTERN_MODE 0x0600 +#define IMX208_TEST_PATTERN_DISABLE 0 +#define IMX208_TEST_PATTERN_SOLID_COLOR 1 +#define IMX208_TEST_PATTERN_COLOR_BARS 2 +#define IMX208_TEST_PATTERN_GREY_COLOR 3 +#define IMX208_TEST_PATTERN_PN9 4
Please use hexadecimal notation for register values (as already done below).
Quoted 6 lines
drivers/media/i2c/imx208.c new file +6
-0+#define IMX208_TEST_PATTERN_FIX_1 0x100 +#define IMX208_TEST_PATTERN_FIX_2 0x101 +#define IMX208_TEST_PATTERN_FIX_3 0x102 +#define IMX208_TEST_PATTERN_FIX_4 0x103 +#define IMX208_TEST_PATTERN_FIX_5 0x104 +#define IMX208_TEST_PATTERN_FIX_6 0x105
[snip]
Quoted 7 lines
drivers/media/i2c/imx208.c new file +7
-0+static const int imx208_test_pattern_val[] = { +IMX208_TEST_PATTERN_DISABLE , +IMX208_TEST_PATTERN_SOLID_COLOR , +IMX208_TEST_PATTERN_COLOR_BARS , +IMX208_TEST_PATTERN_GREY_COLOR , +IMX208_TEST_PATTERN_PN9 , +IMX208_TEST_PATTERN_FIX_1 ,
drivers/media/i2c/imx208.c line 194
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+IMX208_TEST_PATTERN_FIX_2 , +IMX208_TEST_PATTERN_FIX_3 , +IMX208_TEST_PATTERN_FIX_4 , +IMX208_TEST_PATTERN_FIX_5 , + IMX208_TEST_PATTERN_FIX_6, +}; + +/* Configurations for supported link frequencies */ +#define IMX208_LINK_FREQ_384MHZ 384000000ULL +#define IMX208_LINK_FREQ_96MHZ 96000000ULL
nit: If we really need defines for these, then at least they should be somehow useful, e.g.
#define MHZ (1000*1000ULL)
#define IMX208_LINK_FREQ_384MHZ (384ULL * MHZ)
This at least makes it easy to see that there are no mistakes in the number, e.g. wrong number of zeroes.
Quoted 4 lines
drivers/media/i2c/imx208.c new file +4
-0+ +enum { +IMX208_LINK_FREQ_384MHZ_INDEX , +IMX208_LINK_FREQ_96MHZ_INDEX ,
drivers/media/i2c/imx208.c line 208
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+}; + +/* +* pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample +* data rate => double data rate; number of lanes => 2; bits per pixel => 10 +*/ +static u64 link_freq_to_pixel_rate (u64 f) +{ + f *=2 *2 ; +do_div (f,10 );
Please add macros for those magic numbers.
drivers/media/i2c/imx208.c line 216
Quoted 8 lines
drivers/media/i2c/imx208.c new file +8
-0+ +return f; +} + +/* Menu items for LINK_FREQ V4L2 control */ +static const s64 link_freq_menu_items[] = { +IMX208_LINK_FREQ_384MHZ , +IMX208_LINK_FREQ_96MHZ ,
Since we have an enum already, please use it for explicit indices, to ensure things are consistent and actually easier to read, i.e.
[IMX208_LINK_FREQ_384MHZ_INDEX] = IMX208_LINK_FREQ_384MHZ,
drivers/media/i2c/imx208.c line 221
Quoted 5 lines
drivers/media/i2c/imx208.c new file +5
-0+}; + +/* Link frequency configs */ +static const struct imx208_link_freq_config link_freq_configs[] = { + {
Explicit indices, i.e.
[IMX208_LINK_FREQ_384MHZ_INDEX] = {
drivers/media/i2c/imx208.c line 228
Quoted 7 lines
drivers/media/i2c/imx208.c new file +7
-0+ .pixels_per_line = IMX208_PPL_384MHZ, + .reg_list = { + .num_of_regs = ARRAY_SIZE(mipi_data_rate), + .regs = mipi_data_rate, + } + }, + {
Ditto.
drivers/media/i2c/imx208.c line 232
Quoted 4 lines
drivers/media/i2c/imx208.c new file +4
-0+ .pixels_per_line = IMX208_PPL_96MHZ, + .reg_list = { + .num_of_regs = ARRAY_SIZE(mipi_data_rate), + .regs = mipi_data_rate,
How comes that both link frequencies have the same register values for MIPI data rate?
Quoted 6 lines
drivers/media/i2c/imx208.c new file +6
-0+ } + }, +}; + +/* Mode configs */ +static const struct imx208_mode supported_modes[] = {
drivers/media/i2c/imx208.c line 248
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+ { + .width = 1936, + .height = 1096, + .vts_def = IMX208_VTS_60FPS, + .vts_min = IMX208_VTS_60FPS_MIN, + .reg_list = { + .num_of_regs = ARRAY_SIZE(mode_1936x1096_60fps_regs), + .regs = mode_1936x1096_60fps_regs, + }, + .link_freq_index = 0,
Please use the index that was defined before - IMX208_LINK_FREQ_384MHZ_INDEX.
Quoted 21 lines
drivers/media/i2c/imx208.c new file +21
-0+ }, + { + .width = 968, + .height = 548, + .vts_def = IMX208_VTS_BINNING, + .vts_min = IMX208_VTS_BINNING_MIN, + .reg_list = { + .num_of_regs = ARRAY_SIZE(mode_968_548_60fps_regs), + .regs = mode_968_548_60fps_regs, + }, + .link_freq_index = IMX208_LINK_FREQ_96MHZ_INDEX, + }, +}; + +struct imx208 { + struct v4l2_subdev sd; + struct media_pad pad; + + struct v4l2_ctrl_handler ctrl_handler; + /* V4L2 Controls */ + struct v4l2_ctrl *link_freq;
drivers/media/i2c/imx208.c line 279
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+struct v4l2_ctrl *pixel_rate; +struct v4l2_ctrl *vblank; +struct v4l2_ctrl *hblank; +struct v4l2_ctrl *vflip; +struct v4l2_ctrl *hflip; + +/* Current mode */ +const struct imx208_mode *cur_mode; + +/* Mutex for serialized access */
The comment doesn't say access to what is serialized.
drivers/media/i2c/imx208.c line 292
Quoted 13 lines
drivers/media/i2c/imx208.c new file +13
-0+struct mutex imx208_mx; + +/* Streaming on/off */ +bool streaming; +}; + +static inline struct imx208 *to_imx208 (struct v4l2_subdev *_sd) +{ + return container_of(_sd, struct imx208, sd); +} + +/* Get bayer order based on flip setting. */ +static __u32 imx208_get_format_code (struct imx208 *imx208)
Why not just "u32"?
drivers/media/i2c/imx208.c line 298
Quoted 6 lines
drivers/media/i2c/imx208.c new file +6
-0+{ +/* +* Only one bayer order is supported. +* It depends on the flip settings. +*/ +static const __u32 codes[2 ][2 ] = {
Ditto.
drivers/media/i2c/imx208.c line 306
Quoted 8 lines
drivers/media/i2c/imx208.c new file +8
-0+ { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, }, + { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, }, + }; + +return codes[imx208->vflip->val][imx208->hflip->val]; +} + +/* Read registers up to 2 at a time */
The function seems to be handling up to 4 (which I guess is okay, but the comment is off).
Quoted 31 lines
drivers/media/i2c/imx208.c new file +31
-0+static int imx208_read_reg (struct imx208 *imx208,u16 reg,u32 len,u32 *val) +{ +struct i2c_client *client =v4l2_get_subdevdata (&imx208->sd); +struct i2c_msg msgs[2 ]; +u8 addr_buf[2 ] = { reg >>8 , reg &0xff }; +u8 data_buf[4 ] = {0 , }; +int ret; + +if (len >4 ) +return -EINVAL ; + +/* Write register address */ + msgs[0 ].addr = client->addr; + msgs[0 ].flags =0 ; + msgs[0 ].len =ARRAY_SIZE (addr_buf); + msgs[0 ].buf = addr_buf; + +/* Read data from register */ + msgs[1 ].addr = client->addr; + msgs[1 ].flags =I2C_M_RD ; + msgs[1 ].len = len; + msgs[1 ].buf = &data_buf[4 - len]; + + ret =i2c_transfer (client->adapter, msgs,ARRAY_SIZE (msgs)); +if (ret !=ARRAY_SIZE (msgs)) +return -EIO ; + + *val =get_unaligned_be32 (data_buf); + +return 0 ; +}
[snip]
drivers/media/i2c/imx208.c line 430
Quoted 12 lines
drivers/media/i2c/imx208.c new file +12
-0+static int imx208_set_ctrl (struct v4l2_ctrl *ctrl) +{ +struct imx208 *imx208 = + container_of(ctrl->handler, struct imx208, ctrl_handler); +struct i2c_client *client =v4l2_get_subdevdata (&imx208->sd); +int ret; + +/* +* Applying V4L2 control value only happens +* when power is up for streaming +*/ +if (pm_runtime_get_if_in_use (&client->dev) <=0 )
This is buggy, because it won't handle the case of runtime PM disabled in kernel config. The check should be (!pm_runtime_get_if_in_use(&client->dev)).
Quoted 4 lines
drivers/media/i2c/imx208.c new file +4
-0+return 0 ; + +switch (ctrl->id) { +case V4L2_CID_ANALOGUE_GAIN :
drivers/media/i2c/imx208.c line 444
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+ ret =imx208_write_reg (imx208,IMX208_REG_ANALOG_GAIN , +IMX208_REG_VALUE_16BIT , ctrl->val); +break ; +case V4L2_CID_EXPOSURE : + ret =imx208_write_reg (imx208,IMX208_REG_EXPOSURE , +IMX208_REG_VALUE_16BIT , ctrl->val); +break ; +case V4L2_CID_DIGITAL_GAIN : + ret =imx208_update_digital_gain (imx208,IMX208_REG_VALUE_16BIT , + ctrl->val);
nit: The line looks misaligned.
Quoted 12 lines
drivers/media/i2c/imx208.c new file +12
-0+break ; +case V4L2_CID_VBLANK : +/* Update VTS that meets expected vertical blanking */ + ret =imx208_write_reg (imx208,IMX208_REG_VTS , +IMX208_REG_VALUE_16BIT , + imx208->cur_mode->height + ctrl->val); +break ; +case V4L2_CID_TEST_PATTERN : + ret =imx208_write_reg (imx208,IMX208_REG_TEST_PATTERN_MODE , +IMX208_REG_VALUE_16BIT , + imx208_test_pattern_val[ctrl->val]); +break ;
drivers/media/i2c/imx208.c line 466
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+case V4L2_CID_HFLIP : +case V4L2_CID_VFLIP : + ret =imx208_write_reg (imx208,IMX208_REG_ORIENTATION_CONTROL , +IMX208_REG_VALUE_08BIT , + imx208->hflip->val | + imx208->vflip->val <<1 ); +break ; +default : + ret = -EINVAL ; + dev_info(&client->dev,
This is an error, so dev_err().
Quoted 9 lines
drivers/media/i2c/imx208.c new file +9
-0+"ctrl(id:0x%x,val:0x%x) is not handled\n" , + ctrl->id, ctrl->val); +break ; + } + +pm_runtime_put (&client->dev); + +return ret; +}
[snip]
Quoted 20 lines
drivers/media/i2c/imx208.c new file +20
-0+/* Initialize control handlers */ +static int imx208_init_controls (struct imx208 *imx208) +{ +struct i2c_client *client =v4l2_get_subdevdata (&imx208->sd); +struct v4l2_ctrl_handler *ctrl_hdlr = &imx208->ctrl_handler; +s64 exposure_max; +s64 vblank_def; +s64 vblank_min; +s64 pixel_rate_min; +s64 pixel_rate_max; +int ret; + + ret =v4l2_ctrl_handler_init (ctrl_hdlr,8 ); +if (ret) +return ret; + +mutex_init (&imx208->imx208_mx); + ctrl_hdlr->lock = &imx208->imx208_mx; + imx208->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, + &imx208_ctrl_ops,
drivers/media/i2c/imx208.c line 807
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+V4L2_CID_LINK_FREQ , +ARRAY_SIZE (link_freq_menu_items) -1 , +0 , + link_freq_menu_items); + +if (imx208->link_freq) + imx208->link_freq->flags |=V4L2_CTRL_FLAG_READ_ONLY ; + + pixel_rate_max =link_freq_to_pixel_rate (link_freq_menu_items[0 ]); + pixel_rate_min =link_freq_to_pixel_rate (link_freq_menu_items[1 ]);
Please replace [1] with [ARRAY_SIZE(link_freq_menu_items) - 1]. Also please add a comment saying that link_freq_menu_items[] must be sorted by link freq descending, above the array.
Quoted 60 lines
drivers/media/i2c/imx208.c new file +60
-0+/* By default, PIXEL_RATE is read only */ + imx208->pixel_rate =v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops, +V4L2_CID_PIXEL_RATE , + pixel_rate_min, pixel_rate_max, +1 , pixel_rate_max); + + vblank_def = imx208->cur_mode->vts_def - imx208->cur_mode->height; + vblank_min = imx208->cur_mode->vts_min - imx208->cur_mode->height; + imx208->vblank =v4l2_ctrl_new_std ( + ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_VBLANK , + vblank_min, +IMX208_VTS_MAX - imx208->cur_mode->height,1 , + vblank_def); + + imx208->hblank =v4l2_ctrl_new_std ( + ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_HBLANK , +IMX208_PPL_384MHZ - imx208->cur_mode->width, +IMX208_PPL_384MHZ - imx208->cur_mode->width, +1 , +IMX208_PPL_384MHZ - imx208->cur_mode->width); + +if (imx208->hblank) + imx208->hblank->flags |=V4L2_CTRL_FLAG_READ_ONLY ; + + exposure_max = imx208->cur_mode->vts_def -8 ; +v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_EXPOSURE , +IMX208_EXPOSURE_MIN ,IMX208_EXPOSURE_MAX , +IMX208_EXPOSURE_STEP ,IMX208_EXPOSURE_DEFAULT ); + + imx208->hflip =v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops, +V4L2_CID_HFLIP ,0 ,1 ,1 ,0 ); + imx208->vflip =v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops, +V4L2_CID_VFLIP ,0 ,1 ,1 ,0 ); + +v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_ANALOGUE_GAIN , +IMX208_ANA_GAIN_MIN ,IMX208_ANA_GAIN_MAX , +IMX208_ANA_GAIN_STEP ,IMX208_ANA_GAIN_DEFAULT ); + +v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_DIGITAL_GAIN , +IMX208_DGTL_GAIN_MIN ,IMX208_DGTL_GAIN_MAX , +IMX208_DGTL_GAIN_STEP , +IMX208_DGTL_GAIN_DEFAULT ); + +v4l2_ctrl_new_std_menu_items (ctrl_hdlr, &imx208_ctrl_ops, +V4L2_CID_TEST_PATTERN , +ARRAY_SIZE (imx208_test_pattern_menu) -1 , +0 ,0 , imx208_test_pattern_menu); + +if (ctrl_hdlr->error) { + ret = ctrl_hdlr->error; +dev_err (&client->dev,"%s control init failed (%d)\n" , + __func__, ret); + goto error; + } + + imx208->sd.ctrl_handler = ctrl_hdlr; + +return 0 ; + +error:
drivers/media/i2c/imx208.c line 877
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+v4l2_ctrl_handler_free (ctrl_hdlr); +mutex_destroy (&imx208->imx208_mx); + +return ret; +} + +static void imx208_free_controls (struct imx208 *imx208) +{ +v4l2_ctrl_handler_free (imx208->sd.ctrl_handler); +mutex_destroy (&imx208->imx208_mx);
This mutex is not related to controls. Please just paste the 2 lines above to where they are called, as this function adds more lines than it saves.
drivers/media/i2c/imx208.c line 887
Quoted 10 lines
drivers/media/i2c/imx208.c new file +10
-0+} + +static int imx208_probe (struct i2c_client *client) +{ +struct imx208 *imx208; +int ret; +u32 val =0 ; + +device_property_read_u32 (&client->dev,"clock-frequency" , &val); +if (val !=19200000 )
Please print an error message, e.g.
dev_err(&client->dev, "Unsupported value of 'clock-frequency' (%u). Expected 19200000.\n",
val);
P.S. Please use my @chromium.org address in the future, if posting upstream.
Best regards,
Tomasz
Sakari Ailus
maintainerHi Tomasz,
On Mon, Jul 30, 2018 at 07:19:56PM +0900, Tomasz Figa wrote:
...
Quoted 8 lines
Quoted 8 lines
drivers/media/i2c/imx208.c new file +8
-0+static int imx208_set_ctrl (struct v4l2_ctrl *ctrl) +{ +struct imx208 *imx208 = + container_of(ctrl->handler, struct imx208, ctrl_handler); +struct i2c_client *client =v4l2_get_subdevdata (&imx208->sd); +int ret; + +/*
Quoted 8 lines
drivers/media/i2c/imx208.c line 430
Quoted 4 lines
drivers/media/i2c/imx208.c new file +4
-0+* Applying V4L2 control value only happens +* when power is up for streaming +*/ +if (pm_runtime_get_if_in_use (&client->dev) <=0 )This is buggy, because it won't handle the case of runtime PM disabled in kernel config. The check should be (!pm_runtime_get_if_in_use(&client->dev)).
Good find. This seems to be the case for most other sensor drivers that make use of the function. I can submit a fix for those as well.
I suppose most people use these with runtime PM enabled as this hasn't been spotted previously.
Signature · 2 lines
Sakari Ailus [email protected]
Quoted 22 lines · Sakari Ailus
Hi Tomasz,
On Mon, Jul 30, 2018 at 07:19:56PM +0900, Tomasz Figa wrote:
...Quoted 8 lines
Quoted 8 lines
drivers/media/i2c/imx208.c new file +8
-0+static int imx208_set_ctrl (struct v4l2_ctrl *ctrl) +{ +struct imx208 *imx208 = + container_of(ctrl->handler, struct imx208, ctrl_handler); +struct i2c_client *client =v4l2_get_subdevdata (&imx208->sd); +int ret; + +/* Quoted 8 lines
drivers/media/i2c/imx208.c line 430
Quoted 4 lines
drivers/media/i2c/imx208.c new file +4
-0+* Applying V4L2 control value only happens +* when power is up for streaming +*/ +if (pm_runtime_get_if_in_use (&client->dev) <=0 )This is buggy, because it won't handle the case of runtime PM disabled in kernel config. The check should be (!pm_runtime_get_if_in_use(&client->dev)).
Quoted 5 lines
Good find. This seems to be the case for most other sensor drivers that make use of the function. I can submit a fix for those as well.
I suppose most people use these with runtime PM enabled as this hasn't been spotted previously.
Yeah, I spotted it first with imx258 and it took us few emails to get to the right code. :)
These drivers probably don't have too many users yet in general, so I guess this didn't manage to cause any problems yet, but it became a good example of bug propagation via copy/paste. ;)
Fixing would be appreciated indeed!
Best regards,
Tomasz
Hi Tomasz,
Quoted 5 lines
-----Original Message-----
(quoted diff) +2
-0+/* Get bayer order based on flip setting. */ static __u32 +imx208_get_format_code(struct imx208 *imx208)Why not just "u32"?
Its return value will be assigned to the variable code which belongs to the structure v4l2_subdev_mbus_code_enum, and the type of this variable is __u32.
struct v4l2_subdev_mbus_code_enum {
__u32 pad;
__u32 index;
__u32 code;
__u32 which;
__u32 reserved[8];
};Quoted 16 lines
drivers/media/i2c/imx208.c new file +6
-0+{ +/* +* Only one bayer order is supported. +* It depends on the flip settings. +*/ +static const __u32 codes[2 ][2 ] = {Ditto.
drivers/media/i2c/imx208.c new file +7
-0+ { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, }, + { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, }, + }; + +return codes[imx208->vflip->val][imx208->hflip->val]; +} +
Quoted 28 lines · Ping-chung
Hi Tomasz,
Quoted 5 lines
-----Original Message-----
(quoted diff) +2
-0+/* Get bayer order based on flip setting. */ static __u32 +imx208_get_format_code(struct imx208 *imx208)Why not just "u32"?
Its return value will be assigned to the variable code which belongs to the structure
v4l2_subdev_mbus_code_enum, and the type of this variable is__u32.struct v4l2_subdev_mbus_code_enum { __u32 pad; __u32 index; __u32 code; __u32 which; __u32 reserved[8]; };Quoted 8 lines
drivers/media/i2c/imx208.c new file +6
-0+{ +/* +* Only one bayer order is supported. +* It depends on the flip settings. +*/ +static const __u32 codes[2 ][2 ] = {Ditto.
Quoted 7 lines
drivers/media/i2c/imx208.c line 305
Quoted 7 lines
drivers/media/i2c/imx208.c new file +7
-0+ { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, }, + { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, }, + }; + +return codes[imx208->vflip->val][imx208->hflip->val]; +} +
That's okay. __u32 is an equivalent of u32 defined to be used in UAPI headers. Inside of kernel-only code, u32 should be used.
Best regards,
Tomasz
Hi Tomasz,
Please check my comments below.
Quoted 34 lines
Hi Ping-chung,
On Mon, Jul 30, 2018 at 6:19 PM Ping-chung Chen <[email protected]> wrote:
From: "Chen, Ping-chung" <[email protected]>
Add a V4L2 sub-device driver for the Sony IMX208 image sensor. This is a camera sensor using the I2C bus for control and the CSI-2 bus for data.
Please see my comments inline.
[snip]
drivers/media/i2c/imx208.c new file +14
-0@@ -0,0 +1,984 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2018 Intel Corporation + +#include <linux/acpi.h> +#include <linux/delay.h> +#include <linux/i2c.h> +#include <linux/module.h> +#include <linux/pm_runtime.h> +#include <media/v4l2-ctrls.h> +#include <media/v4l2-device.h> +#include <asm/unaligned.h> + +#define IMX208_REG_VALUE_08BIT 1 +#define IMX208_REG_VALUE_16BIT 2
Quoted 2 lines
We don't need to define these. It's obvious that 8 bits is 1 byte and 16 bits are 2 bytes.
Done.
Quoted 13 lines
drivers/media/i2c/imx208.c new file +11
-0+ +#define IMX208_REG_MODE_SELECT 0x0100 +#define IMX208_MODE_STANDBY 0x00 +#define IMX208_MODE_STREAMING 0x01 +/* Test Pattern Control */ +#define IMX208_REG_TEST_PATTERN_MODE 0x0600 +#define IMX208_TEST_PATTERN_DISABLE 0 +#define IMX208_TEST_PATTERN_SOLID_COLOR 1 +#define IMX208_TEST_PATTERN_COLOR_BARS 2 #define +IMX208_TEST_PATTERN_GREY_COLOR 3 +#define IMX208_TEST_PATTERN_PN9 4
Quoted 1 line
Please use hexadecimal notation for register values (as already done below).
Done.
Quoted 25 lines
drivers/media/i2c/imx208.c new file +23
-0+#define IMX208_TEST_PATTERN_FIX_1 0x100 +#define IMX208_TEST_PATTERN_FIX_2 0x101 +#define IMX208_TEST_PATTERN_FIX_3 0x102 +#define IMX208_TEST_PATTERN_FIX_4 0x103 +#define IMX208_TEST_PATTERN_FIX_5 0x104 +#define IMX208_TEST_PATTERN_FIX_6 0x105 +static const int imx208_test_pattern_val[] = { +IMX208_TEST_PATTERN_DISABLE , +IMX208_TEST_PATTERN_SOLID_COLOR , +IMX208_TEST_PATTERN_COLOR_BARS , +IMX208_TEST_PATTERN_GREY_COLOR , +IMX208_TEST_PATTERN_PN9 , +IMX208_TEST_PATTERN_FIX_1 , +IMX208_TEST_PATTERN_FIX_2 , +IMX208_TEST_PATTERN_FIX_3 , +IMX208_TEST_PATTERN_FIX_4 , +IMX208_TEST_PATTERN_FIX_5 , +IMX208_TEST_PATTERN_FIX_6 , +}; + +/* Configurations for supported link frequencies */ +#define IMX208_LINK_FREQ_384MHZ 384000000ULL +#define IMX208_LINK_FREQ_96MHZ 96000000ULL
Quoted 6 lines
nit: If we really need defines for these, then at least they should be somehow useful, e.g.
#define MHZ (1000*1000ULL)
#defineIMX208_LINK_FREQ_384MHZ(384ULL * MHZ)This at least makes it easy to see that there are no mistakes in the number, e.g. wrong number of zeroes.
Sure, done.
Quoted 13 lines
drivers/media/i2c/imx208.c new file +12
-0+ +enum { +IMX208_LINK_FREQ_384MHZ_INDEX , +IMX208_LINK_FREQ_96MHZ_INDEX , +}; + +/* +* pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample +* data rate => double data rate; number of lanes => 2; bits per +pixel => 10 */ static u64 link_freq_to_pixel_rate (u64 f) { + f *=2 *2 ; +do_div (f,10 );
Quoted 1 line
Please add macros for those magic numbers.
Done.
Quoted 9 lines
drivers/media/i2c/imx208.c new file +8
-0+ +return f; +} + +/* Menu items for LINK_FREQ V4L2 control */ static const s64 +link_freq_menu_items[] = { +IMX208_LINK_FREQ_384MHZ , +IMX208_LINK_FREQ_96MHZ ,
Quoted 3 lines
Since we have an enum already, please use it for explicit indices, to ensure things are consistent and actually easier to read, i.e.
[
IMX208_LINK_FREQ_384MHZ_INDEX] =IMX208_LINK_FREQ_384MHZ,
Done.
Quoted 9 lines
drivers/media/i2c/imx208.c new file +5
-0+}; + +/* Link frequency configs */ +static const struct imx208_link_freq_config link_freq_configs[] = { + {Explicit indices, i.e.
[
IMX208_LINK_FREQ_384MHZ_INDEX] = {
Done.
Quoted 15 lines
drivers/media/i2c/imx208.c new file +7
-0+ .pixels_per_line = IMX208_PPL_384MHZ, + .reg_list = { + .num_of_regs = ARRAY_SIZE(mipi_data_rate), + .regs = mipi_data_rate, + } + }, + {Ditto.
drivers/media/i2c/imx208.c new file +4
-0+ .pixels_per_line = IMX208_PPL_96MHZ, + .reg_list = { + .num_of_regs = ARRAY_SIZE(mipi_data_rate), + .regs = mipi_data_rate,
Quoted 1 line
How comes that both link frequencies have the same register values for MIPI data rate?
We have renamed it as pll_ctrl_reg.
Quoted 17 lines
drivers/media/i2c/imx208.c new file +16
-0+ } + }, +}; + +/* Mode configs */ +static const struct imx208_mode supported_modes[] = { + { + .width =1936 , + .height =1096 , + .vts_def =IMX208_VTS_60FPS , + .vts_min =IMX208_VTS_60FPS_MIN , + .reg_list = { + .num_of_regs =ARRAY_SIZE (mode_1936x1096_60fps_regs), + .regs = mode_1936x1096_60fps_regs, + }, + .link_freq_index =0 ,
Quoted 1 line
Please use the index that was defined before -
IMX208_LINK_FREQ_384MHZ_INDEX.
Done.
Quoted 32 lines
drivers/media/i2c/imx208.c new file +31
-0+ }, + { + .width = 968, + .height = 548, + .vts_def = IMX208_VTS_BINNING, + .vts_min = IMX208_VTS_BINNING_MIN, + .reg_list = { + .num_of_regs = ARRAY_SIZE(mode_968_548_60fps_regs), + .regs = mode_968_548_60fps_regs, + }, + .link_freq_index = IMX208_LINK_FREQ_96MHZ_INDEX, + }, +}; + +struct imx208 { + struct v4l2_subdev sd; + struct media_pad pad; + + struct v4l2_ctrl_handler ctrl_handler; + /* V4L2 Controls */ + struct v4l2_ctrl *link_freq; + struct v4l2_ctrl *pixel_rate; + struct v4l2_ctrl *vblank; + struct v4l2_ctrl *hblank; + struct v4l2_ctrl *vflip; + struct v4l2_ctrl *hflip; + + /* Current mode */ + const struct imx208_mode *cur_mode; + + /* Mutex for serialized access */
Quoted 1 line
The comment doesn't say access to what is serialized.
We will add more comments.
/*
* Mutex for serialized access:
* Protect sensor set pad format and start/stop streaming safely.
* Protect access to sensor v4l2 controls.
*/
Quoted 12 lines
drivers/media/i2c/imx208.c new file +11
-0+struct mutex imx208_mx; + +/* Streaming on/off */ +bool streaming; +}; + +static inline struct imx208 *to_imx208 (struct v4l2_subdev *_sd) { + return container_of(_sd, struct imx208, sd); } + +/* Get bayer order based on flip setting. */ static __u32 +imx208_get_format_code (struct imx208 *imx208)
Quoted 1 line
Why not just "u32"?
Fixed.
Quoted 18 lines
drivers/media/i2c/imx208.c new file +6
-0+{ +/* +* Only one bayer order is supported. +* It depends on the flip settings. +*/ +static const __u32 codes[2 ][2 ] = {Ditto.
drivers/media/i2c/imx208.c new file +8
-0+ { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, }, + { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, }, + }; + +return codes[imx208->vflip->val][imx208->hflip->val]; +} + +/* Read registers up to 2 at a time */
Quoted 1 line
The function seems to be handling up to 4 (which I guess is okay, but the comment is off).
Fixed.
Quoted 44 lines
drivers/media/i2c/imx208.c new file +42
-0+static int imx208_read_reg (struct imx208 *imx208,u16 reg,u32 len, +u32 *val) { +struct i2c_client *client =v4l2_get_subdevdata (&imx208->sd); +struct i2c_msg msgs[2 ]; +u8 addr_buf[2 ] = { reg >>8 , reg &0xff }; +u8 data_buf[4 ] = {0 , }; +int ret; + +if (len >4 ) +return -EINVAL ; + +/* Write register address */ + msgs[0 ].addr = client->addr; + msgs[0 ].flags =0 ; + msgs[0 ].len =ARRAY_SIZE (addr_buf); + msgs[0 ].buf = addr_buf; + +/* Read data from register */ + msgs[1 ].addr = client->addr; + msgs[1 ].flags =I2C_M_RD ; + msgs[1 ].len = len; + msgs[1 ].buf = &data_buf[4 - len]; + + ret =i2c_transfer (client->adapter, msgs,ARRAY_SIZE (msgs)); +if (ret !=ARRAY_SIZE (msgs)) +return -EIO ; + + *val =get_unaligned_be32 (data_buf); + +return 0 ; +} +static int imx208_set_ctrl (struct v4l2_ctrl *ctrl) { +struct imx208 *imx208 = + container_of(ctrl->handler, struct imx208, ctrl_handler); +struct i2c_client *client =v4l2_get_subdevdata (&imx208->sd); +int ret; + +/* +* Applying V4L2 control value only happens +* when power is up for streaming +*/ +if (pm_runtime_get_if_in_use (&client->dev) <=0 )
Quoted 1 line
This is buggy, because it won't handle the case of runtime PM disabled in kernel config. The check should be (!pm_runtime_get_if_in_use(&client->dev)).
Fixed it by your suggestion.
Quoted 15 lines
drivers/media/i2c/imx208.c new file +14
-0+return 0 ; + +switch (ctrl->id) { +case V4L2_CID_ANALOGUE_GAIN : + ret =imx208_write_reg (imx208,IMX208_REG_ANALOG_GAIN , +IMX208_REG_VALUE_16BIT , ctrl->val); +break ; +case V4L2_CID_EXPOSURE : + ret =imx208_write_reg (imx208,IMX208_REG_EXPOSURE , +IMX208_REG_VALUE_16BIT , ctrl->val); +break ; +case V4L2_CID_DIGITAL_GAIN : + ret =imx208_update_digital_gain (imx208,IMX208_REG_VALUE_16BIT , + ctrl->val);
Quoted 1 line
nit: The line looks misaligned.
Fixed.
Quoted 23 lines
drivers/media/i2c/imx208.c new file +22
-0+break ; +case V4L2_CID_VBLANK : +/* Update VTS that meets expected vertical blanking */ + ret =imx208_write_reg (imx208,IMX208_REG_VTS , +IMX208_REG_VALUE_16BIT , + imx208->cur_mode->height + ctrl->val); +break ; +case V4L2_CID_TEST_PATTERN : + ret =imx208_write_reg (imx208,IMX208_REG_TEST_PATTERN_MODE , +IMX208_REG_VALUE_16BIT , + imx208_test_pattern_val[ctrl->val]); +break ; +case V4L2_CID_HFLIP : +case V4L2_CID_VFLIP : + ret =imx208_write_reg (imx208,IMX208_REG_ORIENTATION_CONTROL , +IMX208_REG_VALUE_08BIT , + imx208->hflip->val | + imx208->vflip->val <<1 ); +break ; +default : + ret = -EINVAL ; + dev_info(&client->dev,
Quoted 1 line
This is an error, so
dev_err().
Done.
Quoted 41 lines
drivers/media/i2c/imx208.c new file +39
-0+"ctrl(id:0x%x,val:0x%x) is not handled\n" , + ctrl->id, ctrl->val); +break ; + } + +pm_runtime_put (&client->dev); + +return ret; +} +/* Initialize control handlers */ +static int imx208_init_controls (struct imx208 *imx208) { +struct i2c_client *client =v4l2_get_subdevdata (&imx208->sd); +struct v4l2_ctrl_handler *ctrl_hdlr = &imx208->ctrl_handler; +s64 exposure_max; +s64 vblank_def; +s64 vblank_min; +s64 pixel_rate_min; +s64 pixel_rate_max; +int ret; + + ret =v4l2_ctrl_handler_init (ctrl_hdlr,8 ); +if (ret) +return ret; + +mutex_init (&imx208->imx208_mx); + ctrl_hdlr->lock = &imx208->imx208_mx; + imx208->link_freq =v4l2_ctrl_new_int_menu (ctrl_hdlr, + &imx208_ctrl_ops, +V4L2_CID_LINK_FREQ , +ARRAY_SIZE (link_freq_menu_items) -1 , +0 , + link_freq_menu_items); + +if (imx208->link_freq) + imx208->link_freq->flags |=V4L2_CTRL_FLAG_READ_ONLY ; + + pixel_rate_max =link_freq_to_pixel_rate (link_freq_menu_items[0 ]); + pixel_rate_min = +link_freq_to_pixel_rate (link_freq_menu_items[1 ]);
Quoted 1 line
Please replace [1] with [
ARRAY_SIZE(link_freq_menu_items)- 1]. Also please add a comment saying that link_freq_menu_items[] must be sorted by link freq descending, above the array.
Done.
Quoted 73 lines
drivers/media/i2c/imx208.c new file +72
-0+/* By default, PIXEL_RATE is read only */ + imx208->pixel_rate =v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops, +V4L2_CID_PIXEL_RATE , + pixel_rate_min, pixel_rate_max, +1 , pixel_rate_max); + + vblank_def = imx208->cur_mode->vts_def - imx208->cur_mode->height; + vblank_min = imx208->cur_mode->vts_min - imx208->cur_mode->height; + imx208->vblank =v4l2_ctrl_new_std ( + ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_VBLANK , + vblank_min, +IMX208_VTS_MAX - imx208->cur_mode->height,1 , + vblank_def); + + imx208->hblank =v4l2_ctrl_new_std ( + ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_HBLANK , +IMX208_PPL_384MHZ - imx208->cur_mode->width, +IMX208_PPL_384MHZ - imx208->cur_mode->width, +1 , +IMX208_PPL_384MHZ - + imx208->cur_mode->width); + +if (imx208->hblank) + imx208->hblank->flags |=V4L2_CTRL_FLAG_READ_ONLY ; + + exposure_max = imx208->cur_mode->vts_def -8 ; +v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_EXPOSURE , +IMX208_EXPOSURE_MIN ,IMX208_EXPOSURE_MAX , +IMX208_EXPOSURE_STEP , +IMX208_EXPOSURE_DEFAULT ); + + imx208->hflip =v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops, +V4L2_CID_HFLIP ,0 ,1 ,1 ,0 ); + imx208->vflip =v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops, +V4L2_CID_VFLIP ,0 ,1 ,1 ,0 ); + +v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_ANALOGUE_GAIN , +IMX208_ANA_GAIN_MIN ,IMX208_ANA_GAIN_MAX , +IMX208_ANA_GAIN_STEP , +IMX208_ANA_GAIN_DEFAULT ); + +v4l2_ctrl_new_std (ctrl_hdlr, &imx208_ctrl_ops,V4L2_CID_DIGITAL_GAIN , +IMX208_DGTL_GAIN_MIN ,IMX208_DGTL_GAIN_MAX , +IMX208_DGTL_GAIN_STEP , +IMX208_DGTL_GAIN_DEFAULT ); + +v4l2_ctrl_new_std_menu_items (ctrl_hdlr, &imx208_ctrl_ops, +V4L2_CID_TEST_PATTERN , +ARRAY_SIZE (imx208_test_pattern_menu) -1 , +0 ,0 , imx208_test_pattern_menu); + +if (ctrl_hdlr->error) { + ret = ctrl_hdlr->error; +dev_err (&client->dev,"%s control init failed (%d)\n" , + __func__, ret); + goto error; + } + + imx208->sd.ctrl_handler = ctrl_hdlr; + +return 0 ; + +error: +v4l2_ctrl_handler_free (ctrl_hdlr); +mutex_destroy (&imx208->imx208_mx); + +return ret; +} + +static void imx208_free_controls (struct imx208 *imx208) { +v4l2_ctrl_handler_free (imx208->sd.ctrl_handler); +mutex_destroy (&imx208->imx208_mx);
Quoted 1 line
This mutex is not related to controls. Please just paste the 2 lines above to where they are called, as this function adds more lines than it saves.
mutex_destroy() will be removed here. We will call this function after calling imx208_free_controls().
Quoted 10 lines
drivers/media/i2c/imx208.c new file +9
-0+} + +static int imx208_probe (struct i2c_client *client) { +struct imx208 *imx208; +int ret; +u32 val =0 ; + +device_property_read_u32 (&client->dev,"clock-frequency" , &val); +if (val !=19200000 )
Quoted 7 lines
Please print an error message, e.g.
dev_err(&client->dev, "Unsupported value of 'clock-frequency' (%u). Expected 19200000.\n",
val);
P.S. Please use my @chromium.org address in the future, if posting upstream.
Okay.
Best regards,
Tomasz
Sakari Ailus
maintainerHi Ping-chung,
Quoted 7 lines · Ping-chung Chen
From: "Chen, Ping-chung" <[email protected]>
Add a V4L2 sub-device driver for the Sony IMX208 image sensor. This is a camera sensor using the I2C bus for control and the CSI-2 bus for data.
Signed-off-by Ping-Chung Chen <[email protected]>
Could you add obtaining the valid link frequencies from the firmware, i.e. use v4l2_fwnode_endpoint_alloc_parse()?
Signature · 4 lines
Kind regards, Sakari Ailus [email protected]
Hi Sakari,
We will test the new flow on Atlas.
If everything is fine we will merge the code into patch v3 or v4.
Thanks,
PC Chen
-----Original Message-----
From: Sakari Ailus [mailto:[email protected]]
Sent: Monday, July 30, 2018 6:39 PM
To: Chen, Ping-chung <[email protected]>
Cc: [email protected]; Yeh, Andy <[email protected]>; Lai, Jim <[email protected]>; [email protected]; [email protected]; Mani, Rajmohan <[email protected]>
Subject: Re: [PATCH v2] media: imx208: Add imx208 camera sensor driver
Hi Ping-chung,
Quoted 7 lines · Ping-chung Chen
From: "Chen, Ping-chung" <[email protected]>
Add a V4L2 sub-device driver for the Sony IMX208 image sensor. This is a camera sensor using the I2C bus for control and the CSI-2 bus for data.
Signed-off-by Ping-Chung Chen <[email protected]>
Could you add obtaining the valid link frequencies from the firmware, i.e. use v4l2_fwnode_endpoint_alloc_parse()?
Signature · 4 lines
Kind regards, Sakari Ailus [email protected]