Skip to contentSkip to conversation

media: imx258: Add imx258 camera sensor driver

PATCH Superseded Andy Yeh 3 participants 10 messages Mar 2, 2018 – Mar 11, 2018

Subsystems (3)

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Andy Yeh

author
#0 lore raw

PATCH v6 1/1 media: imx258: Add imx258 camera sensor driver

Add a V4L2 sub-device driver for the Sony IMX258 image sensor. This is a camera sensor using the I2C bus for control and the CSI-2 bus for data.

---
since v2:
-- Update the streaming function to remove SW_STANDBY in the beginning. -- Adjust the delay time from 1ms to 12ms before set stream-on register. since v3:
-- fix the sd.entity to make code be compiled on the mainline kernel. since v4:
-- Enabled AG, DG, and Exposure time control correctly. since v5:
-- Sensor vendor provided a new setting to fix different CLK issue -- Add one more resolution for 1048x780, used for VGA streaming

 MAINTAINERS                |    7 +
 drivers/media/i2c/Kconfig  |   11 +
 drivers/media/i2c/Makefile |    1 +
 drivers/media/i2c/imx258.c | 1341 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1360 insertions(+)
 create mode 100644 drivers/media/i2c/imx258.c
MAINTAINERS +7 -0
index a339bb5..9f75510 100644
@@ -12646,6 +12646,13 @@ S:	Maintained
 F:	drivers/ssb/
 F:	include/linux/ssb/
 
+SONY IMX258 SENSOR DRIVER
+M:	Sakari Ailus <[email protected]>
+L:	[email protected]
+T:	git git://linuxtv.org/media_tree.git
+S:	Maintained
+F:	drivers/media/i2c/imx258.c
+
 SONY IMX274 SENSOR DRIVER
 M:	Leon Luo <[email protected]>
 L:	[email protected]
drivers/media/i2c/Kconfig +11 -0
index fd01842..bcd4bf1 100644
@@ -565,6 +565,17 @@ config VIDEO_APTINA_PLL
 config VIDEO_SMIAPP_PLL
 	tristate
 
+config VIDEO_IMX258
+	tristate "Sony IMX258 sensor support"
+	depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
+	depends on MEDIA_CAMERA_SUPPORT
+	---help---
+	  This is a Video4Linux2 sensor-level driver for the Sony
+	  IMX258 camera.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called imx258.
+
 config VIDEO_IMX274
 	tristate "Sony IMX274 sensor support"
 	depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
drivers/media/i2c/Makefile +1 -0
index 1b62639..4bf7d00 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_VIDEO_IR_I2C)  += ir-kbd-i2c.o
 obj-$(CONFIG_VIDEO_ML86V7667)	+= ml86v7667.o
 obj-$(CONFIG_VIDEO_OV2659)	+= ov2659.o
 obj-$(CONFIG_VIDEO_TC358743)	+= tc358743.o
+obj-$(CONFIG_VIDEO_IMX258)	+= imx258.o
 obj-$(CONFIG_VIDEO_IMX274)	+= imx274.o
 
 obj-$(CONFIG_SDR_MAX2175) += max2175.o
drivers/media/i2c/imx258.c new file +1341 -0

1,341 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw

Hi Andy,

Thanks for the patch. Let me post some comments inline.

Quoted 87 lines · Andy Yeh

Add a V4L2 sub-device driver for the Sony IMX258 image sensor. This is a camera sensor using the I2C bus for control and the CSI-2 bus for data.

---
since v2:
-- Update the streaming function to remove SW_STANDBY in the beginning. -- Adjust the delay time from 1ms to 12ms before set stream-on register. since v3:
-- fix the sd.entity to make code be compiled on the mainline kernel. since v4:
-- Enabled AG, DG, and Exposure time control correctly. since v5:
-- Sensor vendor provided a new setting to fix different CLK issue -- Add one more resolution for 1048x780, used for VGA streaming

 MAINTAINERS                |    7 +
 drivers/media/i2c/Kconfig  |   11 +
 drivers/media/i2c/Makefile |    1 +
 drivers/media/i2c/imx258.c | 1341 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1360 insertions(+)
 create mode 100644 drivers/media/i2c/imx258.c
MAINTAINERS +7 -0
index a339bb5..9f75510 100644
@@ -12646,6 +12646,13 @@ S:     Maintained
 F:     drivers/ssb/
 F:     include/linux/ssb/
 
+SONY IMX258 SENSOR DRIVER
+M:     Sakari Ailus <[email protected]>
+L:     [email protected]
+T:     git git://linuxtv.org/media_tree.git
+S:     Maintained
+F:     drivers/media/i2c/imx258.c
+
 SONY IMX274 SENSOR DRIVER
 M:     Leon Luo <[email protected]>
 L:     [email protected]
drivers/media/i2c/Kconfig +11 -0
index fd01842..bcd4bf1 100644
@@ -565,6 +565,17 @@ config VIDEO_APTINA_PLL
 config VIDEO_SMIAPP_PLL
        tristate
 
+config VIDEO_IMX258
+       tristate "Sony IMX258 sensor support"
+       depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
+       depends on MEDIA_CAMERA_SUPPORT
+       ---help---
+         This is a Video4Linux2 sensor-level driver for the Sony
+         IMX258 camera.
+
+         To compile this driver as a module, choose M here: the
+         module will be called imx258.
+
 config VIDEO_IMX274
        tristate "Sony IMX274 sensor support"
        depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
drivers/media/i2c/Makefile +1 -0
index 1b62639..4bf7d00 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_VIDEO_IR_I2C)  += ir-kbd-i2c.o
 obj-$(CONFIG_VIDEO_ML86V7667)  += ml86v7667.o
 obj-$(CONFIG_VIDEO_OV2659)     += ov2659.o
 obj-$(CONFIG_VIDEO_TC358743)   += tc358743.o
+obj-$(CONFIG_VIDEO_IMX258)     += imx258.o
 obj-$(CONFIG_VIDEO_IMX274)     += imx274.o
 
 obj-$(CONFIG_SDR_MAX2175) += max2175.o
drivers/media/i2c/imx258.c new file +4 -0
new file mode 100644
index 0000000..0418edd
@@ -0,0 +1,1341 @@
+// Copyright (C) 2018 Intel Corporation
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/acpi.h>

drivers/media/i2c/imx258.c line 14

Quoted 10 lines
drivers/media/i2c/imx258.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>
+
+#define IMX258_REG_VALUE_08BIT         1
+#define IMX258_REG_VALUE_16BIT         2
+#define IMX258_REG_VALUE_24BIT         3

Is there any need for these macros? Respective functions could just take the number of bytes as the argument directly.

Quoted 80 lines
drivers/media/i2c/imx258.c new file +80 -0
+
+#define IMX258_REG_MODE_SELECT         0x0100
+#define IMX258_MODE_STANDBY            0x00
+#define IMX258_MODE_STREAMING          0x01
+
+/* Chip ID */
+#define IMX258_REG_CHIP_ID             0x0016
+#define IMX258_CHIP_ID                 0x0258
+
+/* V_TIMING internal */
+#define IMX258_REG_VTS                 0x0340
+#define IMX258_VTS_30FPS               0x0c98
+#define IMX258_VTS_MAX                 0xffff
+
+/*Frame Length Line*/
+#define IMX258_FLL_MIN                 0x08a6
+#define IMX258_FLL_MAX                 0xffff
+#define IMX258_FLL_STEP                        1
+#define IMX258_FLL_DEFAULT             0x0c98
+
+/* HBLANK control - read only */
+#define IMX258_PPL_650MHZ              5352
+#define IMX258_PPL_325MHZ              2676
+
+/* Exposure control */
+#define IMX258_REG_EXPOSURE            0x0202
+#define IMX258_EXPOSURE_MIN            4
+#define IMX258_EXPOSURE_STEP           1
+#define IMX258_EXPOSURE_DEFAULT                0x640
+#define IMX258_EXPOSURE_MAX            65535
+
+/* Analog gain control */
+#define IMX258_REG_ANALOG_GAIN         0x0204
+#define IMX258_ANA_GAIN_MIN            0
+#define IMX258_ANA_GAIN_MAX            0x1fff
+#define IMX258_ANA_GAIN_STEP           1
+#define IMX258_ANA_GAIN_DEFAULT                0x0
+
+/* Digital gain control */
+#define IMX258_REG_GR_DIGITAL_GAIN     0x020e
+#define IMX258_REG_R_DIGITAL_GAIN      0x0210
+#define IMX258_REG_B_DIGITAL_GAIN      0x0212
+#define IMX258_REG_GB_DIGITAL_GAIN     0x0214
+#define IMX258_DGTL_GAIN_MIN           0
+#define IMX258_DGTL_GAIN_MAX           4096   /* Max = 0xFFF */
+#define IMX258_DGTL_GAIN_DEFAULT       1024
+#define IMX258_DGTL_GAIN_STEP           1
+
+/* Orientation */
+#define REG_MIRROR_FLIP_CONTROL        0x0101
+#define REG_CONFIG_MIRROR_FLIP         0x03
+
+struct imx258_reg {
+       u16 address;
+       u8 val;
+};
+
+struct imx258_reg_list {
+       u32 num_of_regs;
+       const struct imx258_reg *regs;
+};
+
+/* Link frequency config */
+struct imx258_link_freq_config {
+       u32 pixels_per_line;
+
+       /* PLL registers for this link frequency */
+       struct imx258_reg_list reg_list;
+};
+
+/* Mode : resolution and related config&values */
+struct imx258_mode {
+       /* Frame width */
+       u32 width;
+       /* Frame height */
+       u32 height;
+
+       /* V-timing */
+       u32 vts_def;
+       u32 vts_min;

drivers/media/i2c/imx258.c line 104

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+
+       /* Index of Link frequency config to be used */
+       u32 link_freq_index;
+       /* Default register values */
+       struct imx258_reg_list reg_list;
+};
+
+/* 4208x3118 needs 1300Mbps/lane, 4 lanes */
+static const struct imx258_reg mipi_data_rate_1300mbps[] = {
+       {0x0301, 0x05},

CodingStyle: Spaces inside the braces and lowercase hex, e.g.

{ 0x1234, 0xabcd },

[snip]

drivers/media/i2c/imx258.c line 255

Quoted 5 lines
drivers/media/i2c/imx258.c new file +5 -0
+       {0x951B, 0x50},
+       {0x3030, 0x00},
+       {0x3032, 0x00},
+       {0x0220, 0x00},
+};

nit: Blank line would be nice here.

Quoted 36 lines
drivers/media/i2c/imx258.c new file +36 -0
+static const char * const imx258_test_pattern_menu[] = {
+       "Disabled",
+       "Vertical Color Bar Type 1",
+       "Vertical Color Bar Type 2",
+       "Vertical Color Bar Type 3",
+       "Vertical Color Bar Type 4"
+};
+
+/* Configurations for supported link frequencies */
+#define IMX258_LINK_FREQ_650MHZ        649600000ULL
+#define IMX258_LINK_FREQ_325MHZ        324800000ULL
+
+/*
+ * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
+ * data rate => double data rate; number of lanes => 4; bits per pixel => 10
+ */
+static u64 link_freq_to_pixel_rate(u64 f)
+{
+       f *= 2 * 4;
+       do_div(f, 10);
+
+       return f;
+}
+
+/* Menu items for LINK_FREQ V4L2 control */
+static const s64 link_freq_menu_items[] = {
+       IMX258_LINK_FREQ_650MHZ,
+       IMX258_LINK_FREQ_325MHZ,
+};
+
+/* Link frequency configs */
+static const struct imx258_link_freq_config link_freq_configs[] = {
+       {
+               .pixels_per_line = IMX258_PPL_650MHZ,
+               .reg_list = {
+                       .num_of_regs = ARRAY_SIZE(mipi_data_rate_1300mbps),

drivers/media/i2c/imx258.c line 537

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+                       .regs = mipi_data_rate_1300mbps,
+               }
+       },
+       {
+               .pixels_per_line = IMX258_PPL_325MHZ,
+               .reg_list = {
+                       .num_of_regs = ARRAY_SIZE(mipi_data_rate_650mbps),
+                       .regs = mipi_data_rate_650mbps,
+               }
+       },

Could you define an enum for the entries here nd use explicit indexing? e.g.

enum {
        IMX258_LINK_FREQ_1300MBPS,
        IMX258_LINK_FREQ_650MBPS,
};
static const struct imx258_link_freq_config link_freq_configs[] = {
        [IMX258_LINK_FREQ_1300MBPS] = { ... },
        [IMX258_LINK_FREQ_650MBPS] = { ... },
};

and then... (See below.)

Quoted 4 lines
drivers/media/i2c/imx258.c new file +4 -0
+};
+
+/* Mode configs */
+static const struct imx258_mode supported_modes[] = {

drivers/media/i2c/imx258.c line 551

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       {
+               .width = 4208,
+               .height = 3118,
+               .vts_def = IMX258_VTS_30FPS,
+               .vts_min = IMX258_VTS_30FPS,
+               .reg_list = {
+                       .num_of_regs = ARRAY_SIZE(mode_4208x3118_regs),
+                       .regs = mode_4208x3118_regs,
+               },
+               .link_freq_index = 0,

...use the indices defined above here. This way we have an explicit link between both arrays and it's not possible to accidentally break it.

[snip]

drivers/media/i2c/imx258.c line 593

Quoted 3 lines
drivers/media/i2c/imx258.c new file +3 -0
+
+       /* Mutex for serialized access */
+       struct mutex mutex;

Please document what needs to be serialized.

drivers/media/i2c/imx258.c line 599

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+
+       /* Streaming on/off */
+       bool streaming;
+};
+
+#define to_imx258(_sd) container_of(_sd, struct imx258, sd)

Please use a static inline for compiler type safety.

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+
+/* Read registers up to 4 at a time */
+static int imx258_read_reg(struct imx258 *imx258, u16 reg, u32 len, u32 *val)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       struct i2c_msg msgs[2];
+       u8 *data_be_p;
+       int ret;
+       u32 data_be = 0;
+       u16 reg_addr_be = cpu_to_be16(reg);

drivers/media/i2c/imx258.c line 619

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+
+       if (len > 4)
+               return -EINVAL;
+
+       data_be_p = (u8 *)&data_be;
+       /* Write register address */
+       msgs[0].addr = client->addr;
+       msgs[0].flags = 0;
+       msgs[0].len = 2;
+       msgs[0].buf = (u8 *)&reg_addr_be;

No need for this big endian and pointer cast magic. Let's make this a bit more readable:

u8 addr_buf[2] = { reg >> 8, reg & 0xff };

[...]

msgs[0].len = ARRAY_SIZE(addr_buf);
msgs[0].buf = addr_buf;

drivers/media/i2c/imx258.c line 625

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+
+       /* Read data from register */
+       msgs[1].addr = client->addr;
+       msgs[1].flags = I2C_M_RD;
+       msgs[1].len = len;
+       msgs[1].buf = &data_be_p[4 - len];

u8 data_buf[4] = { 0, };

[...]

msgs[1].buf = &data_buf[4 - len];

drivers/media/i2c/imx258.c line 631

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+
+       ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
+       if (ret != ARRAY_SIZE(msgs))
+               return -EIO;
+
+       *val = be32_to_cpu(data_be);

*val = get_unaligned_be32(data_buf);

Quoted 14 lines
drivers/media/i2c/imx258.c new file +14 -0
+
+       return 0;
+}
+
+/* Write registers up to 4 at a time */
+static int imx258_write_reg(struct imx258 *imx258, u16 reg, u32 len, u32 val)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int buf_i, val_i;
+       u8 buf[6], *val_p;
+
+       if (len > 4)
+               return -EINVAL;
+

drivers/media/i2c/imx258.c line 655

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       buf[0] = reg >> 8;
+       buf[1] = reg & 0xff;
+
+       val = cpu_to_be32(val);
+       val_p = (u8 *)&val;
+       buf_i = 2;
+       val_i = 4 - len;
+
+       while (val_i < 4)
+               buf[buf_i++] = val_p[val_i++];

Similar thing here. The above 7 lines are the same as:

put_unaligned_be32(val << (8 * (4 - len)), buf);

Quoted 25 lines
drivers/media/i2c/imx258.c new file +25 -0
+
+       if (i2c_master_send(client, buf, len + 2) != len + 2)
+               return -EIO;
+
+       return 0;
+}
+
+/* Write a list of registers */
+static int imx258_write_regs(struct imx258 *imx258,
+                             const struct imx258_reg *regs, u32 len)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret;
+       u32 i;
+
+       for (i = 0; i < len; i++) {
+               ret = imx258_write_reg(imx258, regs[i].address, 1,
+                                       regs[i].val);
+               if (ret) {
+                       dev_err_ratelimited(
+                               &client->dev,
+                               "Failed to write reg 0x%4.4x. error = %d\n",
+                               regs[i].address, ret);
+
+                       return ret;

drivers/media/i2c/imx258.c line 690

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+               }
+       }
+
+       return 0;
+}
+
+static int imx258_write_reg_list(struct imx258 *imx258,
+                                 const struct imx258_reg_list *r_list)
+{
+       return imx258_write_regs(imx258, r_list->regs, r_list->num_of_regs);

This is the only place where imx258_write_regs() is used. Is there any need to make it a separate function?

Quoted 24 lines
drivers/media/i2c/imx258.c new file +24 -0
+}
+
+/* Open sub-device */
+static int imx258_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+       struct v4l2_mbus_framefmt *try_fmt =
+               v4l2_subdev_get_try_format(sd, fh->pad, 0);
+
+       /* Initialize try_fmt */
+       try_fmt->width = supported_modes[0].width;
+       try_fmt->height = supported_modes[0].height;
+       try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
+       try_fmt->field = V4L2_FIELD_NONE;
+
+       return 0;
+}
+
+static int imx258_i2c_write_reg_ext(struct imx258 *imx258, u16 reg,
+                                               u32 len, u32 val)
+{
+       u32 val_d, val_d2;
+       int ret;
+       u32 mask = 0xff;
+

drivers/media/i2c/imx258.c line 724

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       val_d = (val & ~mask) >> 8;
+       val_d2 = val & mask;
+
+       ret = imx258_write_reg(imx258, reg, len/2, val_d);
+       ret = imx258_write_reg(imx258, reg+1, len/2, val_d2);
+
+       if (ret)
+               return ret;
+       return 0;
+}

What is this function for? I don't see why imx258_write_reg() couldn't be called directly...

Quoted 7 lines
drivers/media/i2c/imx258.c new file +7 -0
+
+static int imx258_update_digital_gain(struct imx258 *imx258, u32 len, u32 val)
+{
+       int ret;
+
+       ret = imx258_i2c_write_reg_ext(imx258, IMX258_REG_GR_DIGITAL_GAIN,
+                               IMX258_REG_VALUE_16BIT,

drivers/media/i2c/imx258.c line 741

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+                               val);
+       ret |= imx258_i2c_write_reg_ext(imx258, IMX258_REG_GB_DIGITAL_GAIN,
+                               IMX258_REG_VALUE_16BIT,
+                               val);
+       ret |= imx258_i2c_write_reg_ext(imx258, IMX258_REG_R_DIGITAL_GAIN,
+                               IMX258_REG_VALUE_16BIT,
+                               val);
+       ret |= imx258_i2c_write_reg_ext(imx258, IMX258_REG_B_DIGITAL_GAIN,
+                               IMX258_REG_VALUE_16BIT,
+                               val);

Please don't do bitwise OR on return values. If two or more of the above calls return different non-zero values, the resulting value will make no sense.

Quoted 7 lines
drivers/media/i2c/imx258.c new file +7 -0
+       if (ret)
+               return ret;
+       return 0;
+}
+
+static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
+{

drivers/media/i2c/imx258.c line 758

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       struct imx258 *imx258 =
+               container_of(ctrl->handler, struct imx258, ctrl_handler);
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret = 0;
+       /*
+        * Applying V4L2 control value only happens
+        * when power is up for streaming
+        */
+       if (pm_runtime_get_if_in_use(&client->dev) <= 0)
+               return 0;

I thought we decided to fix this to handle disabled runtime PM properly.

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+
+       switch (ctrl->id) {
+       case V4L2_CID_ANALOGUE_GAIN:
+               ret = imx258_i2c_write_reg_ext(imx258, IMX258_REG_ANALOG_GAIN,
+                               IMX258_REG_VALUE_16BIT,
+                               ctrl->val);
+               break;
+       case V4L2_CID_EXPOSURE:
+               ret = imx258_i2c_write_reg_ext(imx258, IMX258_REG_EXPOSURE,
+                               IMX258_REG_VALUE_16BIT,

drivers/media/i2c/imx258.c line 778

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+                               ctrl->val);
+               break;
+       case V4L2_CID_DIGITAL_GAIN:
+               ret = imx258_update_digital_gain(imx258, IMX258_REG_VALUE_16BIT,
+                               ctrl->val);
+               break;
+       default:
+               dev_info(&client->dev,
+                        "ctrl(id:0x%x,val:0x%x) is not handled\n",
+                        ctrl->id, ctrl->val);

dev_err() and set ret to an error.

Quoted 7 lines
drivers/media/i2c/imx258.c new file +7 -0
+               break;
+       }
+
+       pm_runtime_put(&client->dev);
+
+       return ret;
+}

[snip]

Quoted 29 lines
drivers/media/i2c/imx258.c new file +29 -0
+/* Start streaming */
+static int imx258_start_streaming(struct imx258 *imx258)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       const struct imx258_reg_list *reg_list;
+       int ret, link_freq_index;
+
+       /* Setup PLL */
+       link_freq_index = imx258->cur_mode->link_freq_index;
+       reg_list = &link_freq_configs[link_freq_index].reg_list;
+       ret = imx258_write_reg_list(imx258, reg_list);
+       if (ret) {
+               dev_err(&client->dev, "%s failed to set plls\n", __func__);
+               return ret;
+       }
+
+       /* Apply default values of current mode */
+       reg_list = &imx258->cur_mode->reg_list;
+       ret = imx258_write_reg_list(imx258, reg_list);
+       if (ret) {
+               dev_err(&client->dev, "%s failed to set mode\n", __func__);
+               return ret;
+       }
+
+       /* Set Orientation be 180 degree */
+       ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
+                               IMX258_REG_VALUE_08BIT, REG_CONFIG_MIRROR_FLIP);
+       if (ret) {
+               dev_err(&client->dev, "%s failed to set orientation\n",

drivers/media/i2c/imx258.c line 980

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+                       __func__);
+               return ret;
+       }
+
+       /* Apply customized values from user */
+       ret =  __v4l2_ctrl_handler_setup(imx258->sd.ctrl_handler);
+       if (ret)
+               return ret;
+
+       usleep_range(12000, 13000);

Please add a comment explaining what this usleep_range() is waiting for.

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+
+       /* set stream on register */
+       return imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
+                                IMX258_REG_VALUE_08BIT,
+                                IMX258_MODE_STREAMING);
+}

[snip]

Quoted 5 lines
drivers/media/i2c/imx258.c new file +5 -0
+/* Initialize control handlers */
+static int imx258_init_controls(struct imx258 *imx258)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       struct v4l2_ctrl_handler *ctrl_hdlr;

drivers/media/i2c/imx258.c line 1141

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       s64 exposure_max;
+       s64 vblank_def;
+       s64 vblank_min;
+       s64 pixel_rate_min;
+       s64 pixel_rate_max;
+       int ret;
+
+       ctrl_hdlr = &imx258->ctrl_handler;
+       ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
+       if (ret)

Missing error message.

drivers/media/i2c/imx258.c line 1154

Quoted 13 lines
drivers/media/i2c/imx258.c new file +13 -0
+               return ret;
+
+       mutex_init(&imx258->mutex);
+       ctrl_hdlr->lock = &imx258->mutex;
+       imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
+                               &imx258_ctrl_ops,
+                               V4L2_CID_LINK_FREQ,
+                               ARRAY_SIZE(link_freq_menu_items) - 1,
+                               0,
+                               link_freq_menu_items);
+
+       if (!imx258->link_freq) {
+               ret = -EINVAL;

Missing error message.

Quoted 23 lines
drivers/media/i2c/imx258.c new file +23 -0
+               goto error;
+       }
+
+       imx258->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]);
+       /* By default, PIXEL_RATE is read only */
+       imx258->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
+                                       V4L2_CID_PIXEL_RATE,
+                                       pixel_rate_min, pixel_rate_max,
+                                       1, pixel_rate_max);
+
+
+       vblank_def = imx258->cur_mode->vts_def - imx258->cur_mode->height;
+       vblank_min = imx258->cur_mode->vts_min - imx258->cur_mode->height;
+       imx258->vblank = v4l2_ctrl_new_std(
+                               ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_VBLANK,
+                               vblank_min,
+                               IMX258_VTS_MAX - imx258->cur_mode->height, 1,
+                               vblank_def);
+
+       imx258->hblank = v4l2_ctrl_new_std(

drivers/media/i2c/imx258.c line 1187

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+                               ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_HBLANK,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width,
+                               1,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width);
+
+       if (!imx258->hblank) {
+               ret = -EINVAL;
+               goto error;
+       }

Why checking hblank, but not other controls? I think in this case just the general check for ctrl_hdlr->error should be enough.

Quoted 45 lines
drivers/media/i2c/imx258.c new file +45 -0
+
+       imx258->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+
+       exposure_max = imx258->cur_mode->vts_def - 8;
+       imx258->exposure = v4l2_ctrl_new_std(
+                               ctrl_hdlr, &imx258_ctrl_ops,
+                               V4L2_CID_EXPOSURE, IMX258_EXPOSURE_MIN,
+                               IMX258_EXPOSURE_MAX, IMX258_EXPOSURE_STEP,
+                               IMX258_EXPOSURE_DEFAULT);
+
+       v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
+                               IMX258_ANA_GAIN_MIN, IMX258_ANA_GAIN_MAX,
+                               IMX258_ANA_GAIN_STEP, IMX258_ANA_GAIN_DEFAULT);
+
+       v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
+                               IMX258_DGTL_GAIN_MIN, IMX258_DGTL_GAIN_MAX,
+                               IMX258_DGTL_GAIN_STEP, IMX258_DGTL_GAIN_DEFAULT);
+
+       v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx258_ctrl_ops,
+                                    V4L2_CID_TEST_PATTERN,
+                                    ARRAY_SIZE(imx258_test_pattern_menu) - 1,
+                                    0, 0, imx258_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;
+       }
+
+       imx258->sd.ctrl_handler = ctrl_hdlr;
+
+       return 0;
+
+error:
+       v4l2_ctrl_handler_free(ctrl_hdlr);
+       mutex_destroy(&imx258->mutex);
+
+       return ret;
+}
+
+static void imx258_free_controls(struct imx258 *imx258)
+{
+       v4l2_ctrl_handler_free(imx258->sd.ctrl_handler);
+       mutex_destroy(&imx258->mutex);

drivers/media/i2c/imx258.c line 1242

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+}
+
+static int imx258_probe(struct i2c_client *client)
+{
+       struct imx258 *imx258;
+       int ret;
+       u32 val = 0;
+
+       device_property_read_u32(&client->dev, "clock-frequency", &val);
+       if (val != 19200000)

Would be nice to print some error.

Quoted 22 lines
drivers/media/i2c/imx258.c new file +22 -0
+               return -EINVAL;
+
+       imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
+       if (!imx258)
+               return -ENOMEM;
+
+       /* Initialize subdev */
+       v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
+
+       /* Check module identity */
+       ret = imx258_identify_module(imx258);
+       if (ret)
+               return ret;
+
+       /* Set default mode to max resolution */
+       imx258->cur_mode = &supported_modes[0];
+
+       ret = imx258_init_controls(imx258);
+       if (ret)
+               return ret;
+
+       /* Initialize subdev */

drivers/media/i2c/imx258.c line 1274

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       imx258->sd.internal_ops = &imx258_internal_ops;
+       imx258->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+       imx258->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
+
+       /* Initialize source pad */
+       imx258->pad.flags = MEDIA_PAD_FL_SOURCE;
+
+       ret = media_entity_pads_init(&imx258->sd.entity, 1, &imx258->pad);
+       if (ret) {
+               dev_err(&client->dev, "%s failed:%d\n", __func__, ret);

This isn't a very useful error message. "media_entity_pads_init()
failed: %d\n" would make more sense.

Best regards,
Tomasz

maintainer
#2 lore raw

Hi Tomasz and Andy,

On Sat, Mar 03, 2018 at 12:43:59AM +0900, Tomasz Figa wrote:
...

Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct imx258 *imx258 =
+               container_of(ctrl->handler, struct imx258, ctrl_handler);
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret = 0;
Quoted 8 lines

drivers/media/i2c/imx258.c line 758

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+       /*
+        * Applying V4L2 control value only happens
+        * when power is up for streaming
+        */
+       if (pm_runtime_get_if_in_use(&client->dev) <= 0)
+               return 0;

I thought we decided to fix this to handle disabled runtime PM properly.

Good point. I bet this is a problem in a few other drivers, too. How would you fix that? Check for zero only?

...

Quoted 19 lines

[snip]

Quoted 5 lines
drivers/media/i2c/imx258.c new file +5 -0
+/* Initialize control handlers */
+static int imx258_init_controls(struct imx258 *imx258)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       struct v4l2_ctrl_handler *ctrl_hdlr;

drivers/media/i2c/imx258.c line 1141

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       s64 exposure_max;
+       s64 vblank_def;
+       s64 vblank_min;
+       s64 pixel_rate_min;
+       s64 pixel_rate_max;
+       int ret;
+
+       ctrl_hdlr = &imx258->ctrl_handler;
+       ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
+       if (ret)

Missing error message.

Quoted 15 lines

drivers/media/i2c/imx258.c line 1154

Quoted 13 lines
drivers/media/i2c/imx258.c new file +13 -0
+               return ret;
+
+       mutex_init(&imx258->mutex);
+       ctrl_hdlr->lock = &imx258->mutex;
+       imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
+                               &imx258_ctrl_ops,
+                               V4L2_CID_LINK_FREQ,
+                               ARRAY_SIZE(link_freq_menu_items) - 1,
+                               0,
+                               link_freq_menu_items);
+
+       if (!imx258->link_freq) {
+               ret = -EINVAL;

Missing error message.

I wouldn't add an error message here. Typically you'd need that information at development time only, never after that. v4l2_ctrl_new_int_menu(), as other control framework functions creating new controls, can fail due to memory allocation failure (which is already vocally reported) or due to bad parameters (that are constants).

I'd rather do:

if (!imx258->link_freq)

	... |= ...;

It simplifies error handling and removes the need for goto.

Quoted 29 lines
Quoted 29 lines
drivers/media/i2c/imx258.c new file +28 -0
+               goto error;
+       }
+
+       imx258->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]);
+       /* By default, PIXEL_RATE is read only */
+       imx258->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
+                                       V4L2_CID_PIXEL_RATE,
+                                       pixel_rate_min, pixel_rate_max,
+                                       1, pixel_rate_max);
+
+
+       vblank_def = imx258->cur_mode->vts_def - imx258->cur_mode->height;
+       vblank_min = imx258->cur_mode->vts_min - imx258->cur_mode->height;
+       imx258->vblank = v4l2_ctrl_new_std(
+                               ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_VBLANK,
+                               vblank_min,
+                               IMX258_VTS_MAX - imx258->cur_mode->height, 1,
+                               vblank_def);
+
+       imx258->hblank = v4l2_ctrl_new_std(
+                               ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_HBLANK,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width,
+                               1,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width);
Quoted 8 lines

drivers/media/i2c/imx258.c line 1187

Quoted 5 lines
drivers/media/i2c/imx258.c new file +5 -0
+
+       if (!imx258->hblank) {
+               ret = -EINVAL;
+               goto error;
+       }

Why checking hblank, but not other controls? I think in this case just the general check for ctrl_hdlr->error should be enough.

There's no need to access most other controls (except blank and link_freq). The flags field is set for hblank below, therefore the check.

Quoted 86 lines
Quoted 46 lines
drivers/media/i2c/imx258.c new file +45 -0
+
+       imx258->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+
+       exposure_max = imx258->cur_mode->vts_def - 8;
+       imx258->exposure = v4l2_ctrl_new_std(
+                               ctrl_hdlr, &imx258_ctrl_ops,
+                               V4L2_CID_EXPOSURE, IMX258_EXPOSURE_MIN,
+                               IMX258_EXPOSURE_MAX, IMX258_EXPOSURE_STEP,
+                               IMX258_EXPOSURE_DEFAULT);
+
+       v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
+                               IMX258_ANA_GAIN_MIN, IMX258_ANA_GAIN_MAX,
+                               IMX258_ANA_GAIN_STEP, IMX258_ANA_GAIN_DEFAULT);
+
+       v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
+                               IMX258_DGTL_GAIN_MIN, IMX258_DGTL_GAIN_MAX,
+                               IMX258_DGTL_GAIN_STEP, IMX258_DGTL_GAIN_DEFAULT);
+
+       v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx258_ctrl_ops,
+                                    V4L2_CID_TEST_PATTERN,
+                                    ARRAY_SIZE(imx258_test_pattern_menu) - 1,
+                                    0, 0, imx258_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;
+       }
+
+       imx258->sd.ctrl_handler = ctrl_hdlr;
+
+       return 0;
+
+error:
+       v4l2_ctrl_handler_free(ctrl_hdlr);
+       mutex_destroy(&imx258->mutex);
+
+       return ret;
+}
+
+static void imx258_free_controls(struct imx258 *imx258)
+{
+       v4l2_ctrl_handler_free(imx258->sd.ctrl_handler);
+       mutex_destroy(&imx258->mutex);

drivers/media/i2c/imx258.c line 1242

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+}
+
+static int imx258_probe(struct i2c_client *client)
+{
+       struct imx258 *imx258;
+       int ret;
+       u32 val = 0;
+
+       device_property_read_u32(&client->dev, "clock-frequency", &val);
+       if (val != 19200000)

Would be nice to print some error.

Quoted 27 lines
drivers/media/i2c/imx258.c new file +27 -0
+               return -EINVAL;
+
+       imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
+       if (!imx258)
+               return -ENOMEM;
+
+       /* Initialize subdev */
+       v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
+
+       /* Check module identity */
+       ret = imx258_identify_module(imx258);
+       if (ret)
+               return ret;
+
+       /* Set default mode to max resolution */
+       imx258->cur_mode = &supported_modes[0];
+
+       ret = imx258_init_controls(imx258);
+       if (ret)
+               return ret;
+
+       /* Initialize subdev */
+       imx258->sd.internal_ops = &imx258_internal_ops;
+       imx258->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+       imx258->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
+
+       /* Initialize source pad */
Quoted 8 lines

drivers/media/i2c/imx258.c line 1274

Quoted 5 lines
drivers/media/i2c/imx258.c new file +5 -0
+       imx258->pad.flags = MEDIA_PAD_FL_SOURCE;
+
+       ret = media_entity_pads_init(&imx258->sd.entity, 1, &imx258->pad);
+       if (ret) {
+               dev_err(&client->dev, "%s failed:%d\n", __func__, ret);

This isn't a very useful error message. "media_entity_pads_init()
failed: %d\n" would make more sense.

Considering media_entity_pads_init() only fails due to bad parameters and those would not change during the operation of the driver, IMO any error message here is not useful.

Signature · 4 lines
Kind regards,

Sakari Ailus
[email protected]
#3 lore raw
Quoted 19 lines · Sakari Ailus

Hi Tomasz and Andy,

On Sat, Mar 03, 2018 at 12:43:59AM +0900, Tomasz Figa wrote:
...

Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct imx258 *imx258 =
+               container_of(ctrl->handler, struct imx258, ctrl_handler);
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret = 0;
Quoted 8 lines

drivers/media/i2c/imx258.c line 758

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+       /*
+        * Applying V4L2 control value only happens
+        * when power is up for streaming
+        */
+       if (pm_runtime_get_if_in_use(&client->dev) <= 0)
+               return 0;

I thought we decided to fix this to handle disabled runtime PM properly.

Quoted 3 lines

Good point. I bet this is a problem in a few other drivers, too. How would you fix that? Check for zero only?

bool need_runtime_put;

ret = pm_runtime_get_if_in_use(&client->dev);
if (ret <= 0 && ret != -EINVAL)

        return ret;

need_runtime_put = ret > 0;

// Do stuff ...

if (need_runtime_put)

       pm_runtime_put(&client->dev);

I don't like how ugly it is, but it appears to be the only way to handle this correctly.

Quoted 43 lines

...

Quoted 19 lines

[snip]

Quoted 5 lines
drivers/media/i2c/imx258.c new file +5 -0
+/* Initialize control handlers */
+static int imx258_init_controls(struct imx258 *imx258)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       struct v4l2_ctrl_handler *ctrl_hdlr;

drivers/media/i2c/imx258.c line 1141

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       s64 exposure_max;
+       s64 vblank_def;
+       s64 vblank_min;
+       s64 pixel_rate_min;
+       s64 pixel_rate_max;
+       int ret;
+
+       ctrl_hdlr = &imx258->ctrl_handler;
+       ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
+       if (ret)

Missing error message.

Quoted 15 lines

drivers/media/i2c/imx258.c line 1154

Quoted 13 lines
drivers/media/i2c/imx258.c new file +13 -0
+               return ret;
+
+       mutex_init(&imx258->mutex);
+       ctrl_hdlr->lock = &imx258->mutex;
+       imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
+                               &imx258_ctrl_ops,
+                               V4L2_CID_LINK_FREQ,
+                               ARRAY_SIZE(link_freq_menu_items) - 1,
+                               0,
+                               link_freq_menu_items);
+
+       if (!imx258->link_freq) {
+               ret = -EINVAL;

Missing error message.

I wouldn't add an error message here. Typically you'd need that information at development time only, never after that. v4l2_ctrl_new_int_menu(), as other control framework functions creating new controls, can fail due to memory allocation failure (which is already vocally reported) or due to bad parameters (that are constants).

Quoted 6 lines

I'd rather do:

if (!imx258->link_freq)

        ... |= ...;

It simplifies error handling and removes the need for goto.

Hmm, I'm not sure I understand your suggestion. Do you perhaps mean

if (imx258->link_freq) {
        // Do stuff that dereferences imx258->link_freq
}

// ...

if (ctrl_hdlr->error) {
        // Check for error only here, at the end of control initialization.
}

then it would be better indeed.

Quoted 39 lines
Quoted 30 lines
Quoted 30 lines
drivers/media/i2c/imx258.c new file +28 -0
+               goto error;
+       }
+
+       imx258->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]);
+       /* By default, PIXEL_RATE is read only */
+       imx258->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
+                                       V4L2_CID_PIXEL_RATE,
+                                       pixel_rate_min, pixel_rate_max,
+                                       1, pixel_rate_max);
+
+
+       vblank_def = imx258->cur_mode->vts_def - imx258->cur_mode->height;
+       vblank_min = imx258->cur_mode->vts_min - imx258->cur_mode->height;
+       imx258->vblank = v4l2_ctrl_new_std(
+                               ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_VBLANK,
+                               vblank_min,
+                               IMX258_VTS_MAX - imx258->cur_mode->height, 1,
+                               vblank_def);
+
+       imx258->hblank = v4l2_ctrl_new_std(
+                               ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_HBLANK,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width,
+                               1,
+                               IMX258_PPL_650MHZ - imx258->cur_mode->width);
Quoted 8 lines

drivers/media/i2c/imx258.c line 1187

Quoted 5 lines
drivers/media/i2c/imx258.c new file +5 -0
+
+       if (!imx258->hblank) {
+               ret = -EINVAL;
+               goto error;
+       }

Why checking hblank, but not other controls? I think in this case just the general check for ctrl_hdlr->error should be enough.

Quoted 2 lines

There's no need to access most other controls (except blank and link_freq). The flags field is set for hblank below, therefore the check.

Ah, right, I missed the dereference.

Quoted 96 lines
Quoted 87 lines
Quoted 47 lines
drivers/media/i2c/imx258.c new file +45 -0
+
+       imx258->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+
+       exposure_max = imx258->cur_mode->vts_def - 8;
+       imx258->exposure = v4l2_ctrl_new_std(
+                               ctrl_hdlr, &imx258_ctrl_ops,
+                               V4L2_CID_EXPOSURE, IMX258_EXPOSURE_MIN,
+                               IMX258_EXPOSURE_MAX, IMX258_EXPOSURE_STEP,
+                               IMX258_EXPOSURE_DEFAULT);
+
+       v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
+                               IMX258_ANA_GAIN_MIN, IMX258_ANA_GAIN_MAX,
+                               IMX258_ANA_GAIN_STEP, IMX258_ANA_GAIN_DEFAULT);
+
+       v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
+                               IMX258_DGTL_GAIN_MIN, IMX258_DGTL_GAIN_MAX,
+                               IMX258_DGTL_GAIN_STEP, IMX258_DGTL_GAIN_DEFAULT);
+
+       v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx258_ctrl_ops,
+                                    V4L2_CID_TEST_PATTERN,
+                                    ARRAY_SIZE(imx258_test_pattern_menu) - 1,
+                                    0, 0, imx258_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;
+       }
+
+       imx258->sd.ctrl_handler = ctrl_hdlr;
+
+       return 0;
+
+error:
+       v4l2_ctrl_handler_free(ctrl_hdlr);
+       mutex_destroy(&imx258->mutex);
+
+       return ret;
+}
+
+static void imx258_free_controls(struct imx258 *imx258)
+{
+       v4l2_ctrl_handler_free(imx258->sd.ctrl_handler);
+       mutex_destroy(&imx258->mutex);

drivers/media/i2c/imx258.c line 1242

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+}
+
+static int imx258_probe(struct i2c_client *client)
+{
+       struct imx258 *imx258;
+       int ret;
+       u32 val = 0;
+
+       device_property_read_u32(&client->dev, "clock-frequency", &val);
+       if (val != 19200000)

Would be nice to print some error.

Quoted 27 lines
drivers/media/i2c/imx258.c new file +27 -0
+               return -EINVAL;
+
+       imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
+       if (!imx258)
+               return -ENOMEM;
+
+       /* Initialize subdev */
+       v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
+
+       /* Check module identity */
+       ret = imx258_identify_module(imx258);
+       if (ret)
+               return ret;
+
+       /* Set default mode to max resolution */
+       imx258->cur_mode = &supported_modes[0];
+
+       ret = imx258_init_controls(imx258);
+       if (ret)
+               return ret;
+
+       /* Initialize subdev */
+       imx258->sd.internal_ops = &imx258_internal_ops;
+       imx258->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+       imx258->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
+
+       /* Initialize source pad */
Quoted 8 lines

drivers/media/i2c/imx258.c line 1274

Quoted 5 lines
drivers/media/i2c/imx258.c new file +5 -0
+       imx258->pad.flags = MEDIA_PAD_FL_SOURCE;
+
+       ret = media_entity_pads_init(&imx258->sd.entity, 1, &imx258->pad);
+       if (ret) {
+               dev_err(&client->dev, "%s failed:%d\n", __func__, ret);

This isn't a very useful error message. "media_entity_pads_init()
failed: %d\n" would make more sense.

Quoted 3 lines

Considering media_entity_pads_init() only fails due to bad parameters and those would not change during the operation of the driver, IMO any error message here is not useful.

I guess it's a personal preference. I don't mind removing it, though.

Best regards,
Tomasz

maintainer
#4 lore raw
Quoted 32 lines · Tomasz Figa
Quoted 19 lines · Sakari Ailus

Hi Tomasz and Andy,

On Sat, Mar 03, 2018 at 12:43:59AM +0900, Tomasz Figa wrote:
...

Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct imx258 *imx258 =
+               container_of(ctrl->handler, struct imx258, ctrl_handler);
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret = 0;
Quoted 8 lines

drivers/media/i2c/imx258.c line 758

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+       /*
+        * Applying V4L2 control value only happens
+        * when power is up for streaming
+        */
+       if (pm_runtime_get_if_in_use(&client->dev) <= 0)
+               return 0;

I thought we decided to fix this to handle disabled runtime PM properly.

Quoted 3 lines

Good point. I bet this is a problem in a few other drivers, too. How would you fix that? Check for zero only?

bool need_runtime_put;

ret = pm_runtime_get_if_in_use(&client->dev);
if (ret <= 0 && ret != -EINVAL)

        return ret;

need_runtime_put = ret > 0;

Quoted 7 lines

// Do stuff ...

if (need_runtime_put)

       pm_runtime_put(&client->dev);

I don't like how ugly it is, but it appears to be the only way to handle this correctly.

The driver enables runtime PM so if runtime PM is enabled in kernel configuration, it is enabled here. In that case pm_runtime_get_if_in_use() will return either 0 or 1. So as far as I can see, changing the lines to:

	if (!pm_runtime_get_if_in_use(&client->dev))
		return 0;

is enough.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/media/i2c/imx258.c line 1141

Quoted 2 lines
drivers/media/i2c/imx258.c new file +2 -0
+       ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
+       if (ret)

Missing error message.

Same for this actually, printing an error message here isn't useful. It'd be just waiting for someone to clean it up. :-)

Quoted 36 lines
Quoted 23 lines
Quoted 16 lines
Quoted 4 lines
drivers/media/i2c/imx258.c new file +3 -0
+               return ret;
+
+       mutex_init(&imx258->mutex);

drivers/media/i2c/imx258.c line 1154

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       ctrl_hdlr->lock = &imx258->mutex;
+       imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
+                               &imx258_ctrl_ops,
+                               V4L2_CID_LINK_FREQ,
+                               ARRAY_SIZE(link_freq_menu_items) - 1,
+                               0,
+                               link_freq_menu_items);
+
+       if (!imx258->link_freq) {
+               ret = -EINVAL;

Missing error message.

I wouldn't add an error message here. Typically you'd need that information at development time only, never after that. v4l2_ctrl_new_int_menu(), as other control framework functions creating new controls, can fail due to memory allocation failure (which is already vocally reported) or due to bad parameters (that are constants).

Quoted 6 lines

I'd rather do:

if (!imx258->link_freq)

        ... |= ...;

It simplifies error handling and removes the need for goto.

Hmm, I'm not sure I understand your suggestion. Do you perhaps mean

if (imx258->link_freq) {
        // Do stuff that dereferences imx258->link_freq
}
Quoted 7 lines

// ...

if (ctrl_hdlr->error) {
        // Check for error only here, at the end of control initialization.
}

then it would be better indeed.

Yes, indeed.

Signature · 4 lines
Regards,

Sakari Ailus
[email protected]
#5 lore raw
Quoted 41 lines · Sakari Ailus
Quoted 32 lines · Tomasz Figa
Quoted 19 lines · Sakari Ailus

Hi Tomasz and Andy,

On Sat, Mar 03, 2018 at 12:43:59AM +0900, Tomasz Figa wrote:
...

Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct imx258 *imx258 =
+               container_of(ctrl->handler, struct imx258, ctrl_handler);
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret = 0;
Quoted 8 lines

drivers/media/i2c/imx258.c line 758

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+       /*
+        * Applying V4L2 control value only happens
+        * when power is up for streaming
+        */
+       if (pm_runtime_get_if_in_use(&client->dev) <= 0)
+               return 0;

I thought we decided to fix this to handle disabled runtime PM properly.

Quoted 3 lines

Good point. I bet this is a problem in a few other drivers, too. How would you fix that? Check for zero only?

bool need_runtime_put;

ret = pm_runtime_get_if_in_use(&client->dev);
if (ret <= 0 && ret != -EINVAL)

        return ret;

need_runtime_put = ret > 0;

Quoted 7 lines

// Do stuff ...

if (need_runtime_put)

       pm_runtime_put(&client->dev);

I don't like how ugly it is, but it appears to be the only way to handle this correctly.

Quoted 8 lines

The driver enables runtime PM so if runtime PM is enabled in kernel configuration, it is enabled here. In that case pm_runtime_get_if_in_use() will return either 0 or 1. So as far as I can see, changing the lines to:

        if (!pm_runtime_get_if_in_use(&client->dev))
                return 0;

is enough.

Right, my bad. Somehow I was convinced that enable status can change at runtime.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/media/i2c/imx258.c line 1141

Quoted 3 lines
drivers/media/i2c/imx258.c new file +2 -0
+       ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
+       if (ret)

Missing error message.

Same for this actually, printing an error message here isn't useful. It'd be just waiting for someone to clean it up. :-)

Fair enough.

Quoted 39 lines
Quoted 39 lines
Quoted 24 lines
Quoted 17 lines
Quoted 5 lines
drivers/media/i2c/imx258.c new file +3 -0
+               return ret;
+
+       mutex_init(&imx258->mutex);

drivers/media/i2c/imx258.c line 1154

Quoted 10 lines
drivers/media/i2c/imx258.c new file +10 -0
+       ctrl_hdlr->lock = &imx258->mutex;
+       imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
+                               &imx258_ctrl_ops,
+                               V4L2_CID_LINK_FREQ,
+                               ARRAY_SIZE(link_freq_menu_items) - 1,
+                               0,
+                               link_freq_menu_items);
+
+       if (!imx258->link_freq) {
+               ret = -EINVAL;

Missing error message.

I wouldn't add an error message here. Typically you'd need that information at development time only, never after that. v4l2_ctrl_new_int_menu(), as other control framework functions creating new controls, can fail due to memory allocation failure (which is already vocally reported) or due to bad parameters (that are constants).

Quoted 6 lines

I'd rather do:

if (!imx258->link_freq)

        ... |= ...;

It simplifies error handling and removes the need for goto.

Hmm, I'm not sure I understand your suggestion. Do you perhaps mean

if (imx258->link_freq) {
        // Do stuff that dereferences imx258->link_freq
}

// ...

Quoted 7 lines
Quoted 5 lines
if (ctrl_hdlr->error) {
        // Check for error only here, at the end of control initialization.
}

then it would be better indeed.

Yes, indeed.

SGTM.

Best regards,
Tomasz

maintainer
#6 lore raw
Quoted 47 lines · Tomasz Figa
Quoted 45 lines · Sakari Ailus
Quoted 32 lines · Tomasz Figa
Quoted 19 lines · Sakari Ailus

Hi Tomasz and Andy,

On Sat, Mar 03, 2018 at 12:43:59AM +0900, Tomasz Figa wrote:
...

Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct imx258 *imx258 =
+               container_of(ctrl->handler, struct imx258, ctrl_handler);
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret = 0;
Quoted 8 lines

drivers/media/i2c/imx258.c line 758

Quoted 6 lines
drivers/media/i2c/imx258.c new file +6 -0
+       /*
+        * Applying V4L2 control value only happens
+        * when power is up for streaming
+        */
+       if (pm_runtime_get_if_in_use(&client->dev) <= 0)
+               return 0;

I thought we decided to fix this to handle disabled runtime PM properly.

Quoted 3 lines

Good point. I bet this is a problem in a few other drivers, too. How would you fix that? Check for zero only?

bool need_runtime_put;

ret = pm_runtime_get_if_in_use(&client->dev);
if (ret <= 0 && ret != -EINVAL)

        return ret;

need_runtime_put = ret > 0;

Quoted 7 lines

// Do stuff ...

if (need_runtime_put)

       pm_runtime_put(&client->dev);

I don't like how ugly it is, but it appears to be the only way to handle this correctly.

The driver enables runtime PM so if runtime PM is enabled in kernel configuration, it is enabled here. In that case pm_runtime_get_if_in_use() will return either 0 or 1. So as far as I can see, changing the lines to:

Quoted 7 lines
Quoted 4 lines
        if (!pm_runtime_get_if_in_use(&client->dev))
                return 0;

is enough.

Right, my bad. Somehow I was convinced that enable status can change at runtime.

Good point. I guess in principle this could happen although I can't see a reason to do so, other than to break things --- quoting Documentation/power/runtime_pm.txt:

The user space can effectively disallow the driver of the device to power manage it at run time by changing the value of its /sys/devices/.../power/control attribute to "on", which causes pm_runtime_forbid() to be called. In principle, this mechanism may also be used by the driver to effectively turn off the runtime power management of the device until the user space turns it on. Namely, during the initialization the driver can make sure that the runtime PM status of the device is 'active' and call pm_runtime_forbid(). It should be noted, however, that if the user space has already intentionally changed the value of /sys/devices/.../power/control to "auto" to allow the driver to power manage the device at run time, the driver may confuse it by using pm_runtime_forbid() this way.

So that comes with a warning that things might not work well after doing so.

What comes to the driver code, I still wouldn't complicate it by attempting to make a driver work in such a case.

Signature · 2 lines
Sakari Ailus
[email protected]
#7 lore raw
Quoted 74 lines · Sakari Ailus
Quoted 47 lines · Tomasz Figa
Quoted 45 lines · Sakari Ailus
Quoted 32 lines · Tomasz Figa
Quoted 19 lines · Sakari Ailus

Hi Tomasz and Andy,

On Sat, Mar 03, 2018 at 12:43:59AM +0900, Tomasz Figa wrote:
...

Quoted 6 lines
Quoted 6 lines
(quoted diff) +6 -0
+static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct imx258 *imx258 =
+               container_of(ctrl->handler, struct imx258, ctrl_handler);
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret = 0;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+       /*
+        * Applying V4L2 control value only happens
+        * when power is up for streaming
+        */
+       if (pm_runtime_get_if_in_use(&client->dev) <= 0)
+               return 0;

I thought we decided to fix this to handle disabled runtime PM properly.

Quoted 3 lines

Good point. I bet this is a problem in a few other drivers, too. How would you fix that? Check for zero only?

bool need_runtime_put;

ret = pm_runtime_get_if_in_use(&client->dev);
if (ret <= 0 && ret != -EINVAL)

        return ret;

need_runtime_put = ret > 0;

Quoted 7 lines

// Do stuff ...

if (need_runtime_put)

       pm_runtime_put(&client->dev);

I don't like how ugly it is, but it appears to be the only way to handle this correctly.

The driver enables runtime PM so if runtime PM is enabled in kernel configuration, it is enabled here. In that case pm_runtime_get_if_in_use() will return either 0 or 1. So as far as I can see, changing the lines to:

Quoted 7 lines
Quoted 4 lines
        if (!pm_runtime_get_if_in_use(&client->dev))
                return 0;

is enough.

Right, my bad. Somehow I was convinced that enable status can change at runtime.

Good point. I guess in principle this could happen although I can't see a reason to do so, other than to break things --- quoting Documentation/power/runtime_pm.txt:

The user space can effectively disallow the driver of the device to power manage it at run time by changing the value of its /sys/devices/.../power/control attribute to "on", which causes pm_runtime_forbid() to be called. In principle, this mechanism may also be used by the driver to effectively turn off the runtime power management of the device until the user space turns it on. Namely, during the initialization the driver can make sure that the runtime PM status of the device is 'active' and call pm_runtime_forbid(). It should be noted, however, that if the user space has already intentionally changed the value of /sys/devices/.../power/control to "auto" to allow the driver to power manage the device at run time, the driver may confuse it by using pm_runtime_forbid() this way.

Quoted 5 lines

So that comes with a warning that things might not work well after doing so.

What comes to the driver code, I still wouldn't complicate it by attempting to make a driver work in such a case.

I think pm_runtime_forbid() and pm_runtime_enable() operate on complete different data. That was exactly the source of my confusion earlier. Looking at the code [1], even if runtime PM is "forbidden", it is still "enabled" and just the usage count is incremented.

https://elixir.bootlin.com/linux/latest/source/drivers/base/power/runtime.c#L1314

Best regards,
Tomasz

maintainer
#8 lore raw
Quoted 82 lines · Tomasz Figa
Quoted 74 lines · Sakari Ailus
Quoted 47 lines · Tomasz Figa
Quoted 45 lines · Sakari Ailus
Quoted 32 lines · Tomasz Figa
Quoted 19 lines · Sakari Ailus

Hi Tomasz and Andy,

On Sat, Mar 03, 2018 at 12:43:59AM +0900, Tomasz Figa wrote:
...

Quoted 6 lines
Quoted 6 lines
(quoted diff) +6 -0
+static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct imx258 *imx258 =
+               container_of(ctrl->handler, struct imx258, ctrl_handler);
+       struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
+       int ret = 0;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+       /*
+        * Applying V4L2 control value only happens
+        * when power is up for streaming
+        */
+       if (pm_runtime_get_if_in_use(&client->dev) <= 0)
+               return 0;

I thought we decided to fix this to handle disabled runtime PM properly.

Quoted 3 lines

Good point. I bet this is a problem in a few other drivers, too. How would you fix that? Check for zero only?

bool need_runtime_put;

ret = pm_runtime_get_if_in_use(&client->dev);
if (ret <= 0 && ret != -EINVAL)

        return ret;

need_runtime_put = ret > 0;

Quoted 7 lines

// Do stuff ...

if (need_runtime_put)

       pm_runtime_put(&client->dev);

I don't like how ugly it is, but it appears to be the only way to handle this correctly.

The driver enables runtime PM so if runtime PM is enabled in kernel configuration, it is enabled here. In that case pm_runtime_get_if_in_use() will return either 0 or 1. So as far as I can see, changing the lines to:

Quoted 7 lines
Quoted 4 lines
        if (!pm_runtime_get_if_in_use(&client->dev))
                return 0;

is enough.

Right, my bad. Somehow I was convinced that enable status can change at runtime.

Good point. I guess in principle this could happen although I can't see a reason to do so, other than to break things --- quoting Documentation/power/runtime_pm.txt:

The user space can effectively disallow the driver of the device to power manage it at run time by changing the value of its /sys/devices/.../power/control attribute to "on", which causes pm_runtime_forbid() to be called. In principle, this mechanism may also be used by the driver to effectively turn off the runtime power management of the device until the user space turns it on. Namely, during the initialization the driver can make sure that the runtime PM status of the device is 'active' and call pm_runtime_forbid(). It should be noted, however, that if the user space has already intentionally changed the value of /sys/devices/.../power/control to "auto" to allow the driver to power manage the device at run time, the driver may confuse it by using pm_runtime_forbid() this way.

Quoted 5 lines

So that comes with a warning that things might not work well after doing so.

What comes to the driver code, I still wouldn't complicate it by attempting to make a driver work in such a case.

Quoted 6 lines

I think pm_runtime_forbid() and pm_runtime_enable() operate on complete different data. That was exactly the source of my confusion earlier. Looking at the code [1], even if runtime PM is "forbidden", it is still "enabled" and just the usage count is incremented.

https://elixir.bootlin.com/linux/latest/source/drivers/base/power/runtime.c#L1314

Ah, right. Thanks for the correction. Then indeed this is very clear.

Signature · 2 lines
Sakari Ailus
[email protected]
author
#9 lore raw
In reply to Tomasz Figa

Okay. All comments are addressed in v7. (https://patchwork.linuxtv.org/patch/47869/)

Thanks for all the comments and suggestions.

Regards, Andy

-----Original Message-----
From: Tomasz Figa [mailto:[email protected]]
Sent: Friday, March 2, 2018 11:44 PM
To: Yeh, Andy <[email protected]>
Cc: Linux Media Mailing List <[email protected]>; Sakari Ailus <[email protected]>; Chen, JasonX Z <[email protected]>; Chiang, AlanX <[email protected]>
Subject: Re: [PATCH v6] media: imx258: Add imx258 camera sensor driver

Hi Andy,

Thanks for the patch. Let me post some comments inline.

Quoted 17 lines · Andy Yeh

Add a V4L2 sub-device driver for the Sony IMX258 image sensor. This is a camera sensor using the I2C bus for control and the CSI-2 bus for data.

---
since v2:
-- Update the streaming function to remove SW_STANDBY in the beginning. -- Adjust the delay time from 1ms to 12ms before set stream-on register. since v3:
-- fix the sd.entity to make code be compiled on the mainline kernel. since v4:
-- Enabled AG, DG, and Exposure time control correctly. since v5:
-- Sensor vendor provided a new setting to fix different CLK issue -- Add one more resolution for 1048x780, used for VGA streaming