On Mon, Jun 29, 2020 at 9:54 AM Dongchun Zhu <dongchun.zhu@xxxxxxxxxxxx> wrote: > > Hi Tomasz, > > On Thu, 2020-06-18 at 19:10 +0000, Tomasz Figa wrote: > > Hi Dongchun, > > > > On Mon, Jun 15, 2020 at 08:29:37PM +0800, Dongchun Zhu wrote: > > > Add a V4L2 sub-device driver for OV02A10 image sensor. > > > > > > Signed-off-by: Dongchun Zhu <dongchun.zhu@xxxxxxxxxxxx> > > > --- > > > MAINTAINERS | 1 + > > > drivers/media/i2c/Kconfig | 13 + > > > drivers/media/i2c/Makefile | 1 + > > > drivers/media/i2c/ov02a10.c | 1042 +++++++++++++++++++++++++++++++++++++++++++ > > > 4 files changed, 1057 insertions(+) > > > create mode 100644 drivers/media/i2c/ov02a10.c > > > > > > > Thank you for the patch. Please see my comments inline. > > > > [snip] > > > > +static int ov02a10_probe(struct i2c_client *client) > > > +{ > > > + struct device *dev = &client->dev; > > > + struct ov02a10 *ov02a10; > > > + unsigned int rotation; > > > + unsigned int clock_lane_tx_speed; > > > + unsigned int i; > > > + int ret; > > > + > > > + ov02a10 = devm_kzalloc(dev, sizeof(*ov02a10), GFP_KERNEL); > > > + if (!ov02a10) > > > + return -ENOMEM; > > > + > > > + ret = ov02a10_check_hwcfg(dev, ov02a10); > > > + if (ret) { > > > + dev_err(dev, "failed to check HW configuration: %d", ret); > > > + return ret; > > > + } > > > + > > > + v4l2_i2c_subdev_init(&ov02a10->subdev, client, &ov02a10_subdev_ops); > > > + ov02a10->mipi_clock_tx_speed = OV02A10_MIPI_TX_SPEED_DEFAULT; > > > + ov02a10->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10; > > > + > > > + /* Optional indication of physical rotation of sensor */ > > > + ret = fwnode_property_read_u32(dev_fwnode(dev), "rotation", &rotation); > > > + if (!ret && rotation == 180) { > > > + ov02a10->upside_down = true; > > > + ov02a10->fmt.code = MEDIA_BUS_FMT_SRGGB10_1X10; > > > + } > > > + > > > + /* Optional indication of mipi TX speed */ > > > + ret = fwnode_property_read_u32(dev_fwnode(dev), "ovti,mipi-tx-speed", > > > + &clock_lane_tx_speed); > > > + > > > + if (!ret) > > > + ov02a10->mipi_clock_tx_speed = clock_lane_tx_speed; > > > + > > > + /* Get system clock (eclk) */ > > > + ov02a10->eclk = devm_clk_get(dev, "eclk"); > > > + if (IS_ERR(ov02a10->eclk)) { > > > + ret = PTR_ERR(ov02a10->eclk); > > > + dev_err(dev, "failed to get eclk %d\n", ret); > > > + return ret; > > > + } > > > + > > > + ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", > > > + &ov02a10->eclk_freq); > > > + if (ret) { > > > + dev_err(dev, "failed to get eclk frequency\n"); > > > + return ret; > > > + } > > > + > > > + ret = clk_set_rate(ov02a10->eclk, ov02a10->eclk_freq); > > > + if (ret) { > > > + dev_err(dev, "failed to set eclk frequency (24MHz)\n"); > > > + return ret; > > > + } > > > + > > > + if (clk_get_rate(ov02a10->eclk) != OV02A10_ECLK_FREQ) { > > > + dev_warn(dev, "wrong eclk frequency %d Hz, expected: %d Hz\n", > > > + ov02a10->eclk_freq, OV02A10_ECLK_FREQ); > > > + return -EINVAL; > > > + } > > > + > > > + ov02a10->pd_gpio = devm_gpiod_get(dev, "powerdown", GPIOD_OUT_HIGH); > > > + if (IS_ERR(ov02a10->pd_gpio)) { > > > + ret = PTR_ERR(ov02a10->pd_gpio); > > > + dev_err(dev, "failed to get powerdown-gpios %d\n", ret); > > > + return ret; > > > + } > > > + > > > + ov02a10->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); > > > + if (IS_ERR(ov02a10->rst_gpio)) { > > > + ret = PTR_ERR(ov02a10->rst_gpio); > > > + dev_err(dev, "failed to get reset-gpios %d\n", ret); > > > + return ret; > > > + } > > > + > > > + for (i = 0; i < ARRAY_SIZE(ov02a10_supply_names); i++) > > > + ov02a10->supplies[i].supply = ov02a10_supply_names[i]; > > > + > > > + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov02a10_supply_names), > > > + ov02a10->supplies); > > > + if (ret) { > > > + dev_err(dev, "failed to get regulators\n"); > > > + return ret; > > > + } > > > + > > > + mutex_init(&ov02a10->mutex); > > > + ov02a10->cur_mode = &supported_modes[0]; > > > + ret = ov02a10_initialize_controls(ov02a10); > > > + if (ret) { > > > + dev_err(dev, "failed to initialize controls\n"); > > > + goto err_destroy_mutex; > > > + } > > > + > > > + ov02a10->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; > > > + ov02a10->subdev.entity.ops = &ov02a10_subdev_entity_ops; > > > + ov02a10->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; > > > + ov02a10->pad.flags = MEDIA_PAD_FL_SOURCE; > > > + ret = media_entity_pads_init(&ov02a10->subdev.entity, 1, &ov02a10->pad); > > > + if (ret < 0) { > > > + dev_err(dev, "failed to init entity pads: %d", ret); > > > + goto err_free_handler; > > > + } > > > + > > > + pm_runtime_enable(dev); > > > + if (!pm_runtime_enabled(dev)) { > > > + ret = ov02a10_power_on(dev); > > > + if (ret < 0) { > > > + dev_err(dev, "failed to power on: %d\n", ret); > > > + goto err_clean_entity; > > > + } > > > + } > > > + > > > + ret = v4l2_async_register_subdev(&ov02a10->subdev); > > > + if (ret) { > > > + dev_err(dev, "failed to register V4L2 subdev: %d", ret); > > > + goto err_power_off; > > > + } > > > + > > > + return 0; > > > + > > > +err_power_off: > > > + pm_runtime_disable(dev); > > > + if (!pm_runtime_enabled(dev)) > > > > This would be always true, resulting in unbalanced power off. Moving > > pm_runtime_disable() after this if should work better. > > > > Pardon, do you mean that we shall use like this: > err_power_off: > if (!pm_runtime_enabled(dev)) { > pm_runtime_disable(dev); > if (!pm_runtime_status_suspended(dev) > ov02a10_power_off(dev); > } Hmm, that wouldn't really work, because there is no reason to disable runtime PM if it's disabled already. I also noticed that we don't need to check pm_runtime_status_suspended() in the error path in probe, because we only ever attempt to power it on when runtime PM is disabled in kernel config. This would make the end result as: if (pm_runtime_enabled(dev)) pm_runtime_disable(dev); else ov02a10_power_off(dev); Best regards, Tomasz