[PATCH 2/2] media: i2c: Add GC08A3 image sensor driver

Krzysztof Kozlowski krzysztof.kozlowski at linaro.org
Fri Nov 24 00:09:48 PST 2023


On 23/11/2023 12:51, Zhi Mao wrote:
> Add a V4L2 sub-device driver for Galaxycore GC08A3 image sensor.
> 
> Signed-off-by: Zhi Mao <zhi.mao at mediatek.com>
> ---
>  drivers/media/i2c/Kconfig  |   14 +
>  drivers/media/i2c/Makefile |    1 +
>  drivers/media/i2c/gc08a3.c | 2046 ++++++++++++++++++++++++++++++++++++
>  3 files changed, 2061 insertions(+)
>  create mode 100644 drivers/media/i2c/gc08a3.c


> +static int gc08a3_start_streaming(struct gc08a3 *gc08a3)
> +{
> +	const struct gc08a3_mode *mode;
> +	const struct gc08a3_reg_list *reg_list;
> +	int link_freq_index;
> +	int ret;
> +
> +	dev_info(gc08a3->dev, "%s ++\n", __func__);

Drop

> +
> +	mutex_lock(&gc08a3->mutex);
> +
> +	link_freq_index = gc08a3->cur_mode->link_freq_index;
> +	dev_info(gc08a3->dev, "----link_freq_index = %d ", link_freq_index);
> +
> +	reg_list = &link_freq_configs[link_freq_index].reg_list;
> +	ret = gc08a3_write_reg_list(gc08a3, reg_list);
> +	if (ret) {
> +		dev_err(gc08a3->dev, "could not sent common table %d\n", ret);
> +		goto error;
> +	}
> +
> +	mode = gc08a3->cur_mode;
> +	dev_info(gc08a3->dev, "----write regtbl: mode(id:%d, WxH:%dx%d)",
> +		 mode->mode_id, mode->width, mode->height);
> +	reg_list = &mode->reg_list;
> +
> +	ret = gc08a3_write_reg_list(gc08a3, reg_list);
> +	if (ret < 0) {
> +		dev_err(gc08a3->dev, "could not sent mode table %d\n", ret);
> +		goto error;
> +	}
> +	ret = __v4l2_ctrl_handler_setup(&gc08a3->ctrls);
> +	if (ret < 0) {
> +		dev_err(gc08a3->dev, "could not sync v4l2 controls\n");
> +		goto error;
> +	}
> +
> +	ret = gc08a3_write_reg(gc08a3, GC08A3_STRAEMING_REG,
> +			       GC08A3_REG_VALUE_08BIT, 1);
> +	if (ret < 0) {
> +		dev_err(gc08a3->dev, "write STRAEMING_REG failed: %d\n", ret);
> +		goto error;
> +	}
> +
> +	mutex_unlock(&gc08a3->mutex);
> +
> +	dev_info(gc08a3->dev, "%s --\n", __func__);

Drop


> +
> +	return 0;
> +
> +error:
> +	mutex_unlock(&gc08a3->mutex);
> +	return ret;
> +}
> +
> +static int gc08a3_stop_streaming(struct gc08a3 *gc08a3)
> +{
> +	int ret;
> +
> +	dev_info(gc08a3->dev, "%s ++\n", __func__);

Drop

> +
> +	ret = gc08a3_write_reg(gc08a3, GC08A3_STRAEMING_REG,
> +			       GC08A3_REG_VALUE_08BIT, 0);
> +	if (ret < 0)
> +		dev_err(gc08a3->dev, "could not sent stop streaming %d\n", ret);
> +
> +	dev_info(gc08a3->dev, "%s --\n", __func__);
> +

Drop

...

> +
> +static int gc08a3_probe(struct i2c_client *client)
> +{
> +	struct device *dev = &client->dev;
> +	struct gc08a3 *gc08a3;
> +	int ret;
> +
> +	dev_info(dev, "--- %s +", __func__);


No, drop such silly debug messages. Everywhere. Really everywhere.

> +
> +	ret = gc08a3_parse_fwnode(dev);
> +	if (ret)
> +		return ret;
> +
> +	gc08a3 = devm_kzalloc(dev, sizeof(*gc08a3), GFP_KERNEL);
> +	if (!gc08a3)
> +		return -ENOMEM;
> +
> +	gc08a3->dev = dev;
> +
> +	gc08a3->xclk = devm_clk_get(dev, NULL);
> +	if (IS_ERR(gc08a3->xclk)) {
> +		dev_err(dev, "could not get xclk\n");

Syntax is:
return dev_err_probe()

> +		return PTR_ERR(gc08a3->xclk);
> +	}
> +
> +	ret = clk_set_rate(gc08a3->xclk, GC08A3_DEFAULT_CLK_FREQ);
> +	if (ret) {
> +		dev_err(dev, "could not set xclk frequency\n");
> +		return ret;
> +	}
> +
> +	ret = gc08a3_get_regulators(dev, gc08a3);
> +	if (ret < 0) {
> +		dev_err(dev, "cannot get regulators\n");

return dev_err_probe()

> +		return ret;
> +	}
> +
> +	gc08a3->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
> +	if (IS_ERR(gc08a3->enable_gpio)) {
> +		dev_err(dev, "cannot get enable gpio\n");

return dev_err_probe()

> +		return PTR_ERR(gc08a3->enable_gpio);
> +	}
> +
> +	gc08a3->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
> +	if (IS_ERR(gc08a3->regmap)) {
> +		dev_err(dev, "regmap init failed\n");

return dev_err_probe()

> +		return PTR_ERR(gc08a3->regmap);
> +	}
> +
> +	v4l2_i2c_subdev_init(&gc08a3->sd, client, &gc08a3_subdev_ops);
> +
> +	gc08a3_power_on(gc08a3->dev);
> +
> +	ret = gc08a3_identify_module(gc08a3);
> +	if (ret) {
> +		dev_err(&client->dev, "failed to find sensor: %d\n", ret);
> +		gc08a3_power_off(gc08a3->dev);
> +		return ret;
> +	}
> +
> +	mutex_init(&gc08a3->mutex);
> +	gc08a3->cur_mode = &gc08a3_modes[0];
> +
> +	ret = gc08a3_init_controls(gc08a3);
> +	if (ret) {
> +		dev_err(&client->dev, "failed to init controls: %d", ret);

No power off?

> +		goto free_ctrl;
> +	}
> +
> +	gc08a3->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> +	gc08a3->pad.flags = MEDIA_PAD_FL_SOURCE;
> +	gc08a3->sd.dev = &client->dev;
> +	gc08a3->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
> +
> +	dev_dbg(&client->dev, "gc08a3->sd.name: %s, dev->of_node->name: %s\n",
> +		gc08a3->sd.name, dev->of_node->name);
> +	if (V4L2_SUBDEV_NAME_SIZE - strlen(gc08a3->sd.name) - 2 <
> +	    strlen(dev->of_node->name)) {
> +		dev_err(&client->dev,
> +			"the string length of (sd.name + of_node->name) is too long.\n");

This looks like random error handling. You had goto in previous cases.

> +		return -EINVAL;
> +	}
> +	strncat(gc08a3->sd.name, " ", 1);
> +	strncat(gc08a3->sd.name, dev->of_node->name,
> +		V4L2_SUBDEV_NAME_SIZE - strlen(gc08a3->sd.name) - 2);
> +	dev_dbg(&client->dev, "after: gc08a3->sd.name: %s\n", gc08a3->sd.name);
> +
> +	ret = media_entity_pads_init(&gc08a3->sd.entity, 1, &gc08a3->pad);
> +	if (ret < 0) {
> +		dev_err(dev, "could not register media entity\n");
> +		goto free_ctrl;
> +	}
> +
> +	ret = v4l2_async_register_subdev_sensor(&gc08a3->sd);
> +	if (ret < 0) {
> +		dev_err(dev, "could not register v4l2 device\n");
> +		goto free_entity;
> +	}
> +
> +	pm_runtime_set_active(gc08a3->dev);
> +	pm_runtime_enable(gc08a3->dev);
> +	pm_runtime_idle(gc08a3->dev);
> +
> +	dev_info(dev, "--- %s -", __func__);

No, drop such silly debug messages.

> +
> +	return 0;
> +
> +free_entity:
> +	media_entity_cleanup(&gc08a3->sd.entity);
> +free_ctrl:
> +	mutex_destroy(&gc08a3->mutex);
> +	v4l2_ctrl_handler_free(&gc08a3->ctrls);
> +	pm_runtime_disable(gc08a3->dev);
> +
> +	return ret;
> +}
> +
> +static void gc08a3_remove(struct i2c_client *client)
> +{
> +	struct v4l2_subdev *sd = i2c_get_clientdata(client);
> +	struct gc08a3 *gc08a3 = to_gc08a3(sd);
> +
> +	v4l2_async_unregister_subdev(&gc08a3->sd);
> +	media_entity_cleanup(&gc08a3->sd.entity);
> +	v4l2_ctrl_handler_free(&gc08a3->ctrls);
> +
> +	pm_runtime_disable(&client->dev);
> +	pm_runtime_set_suspended(&client->dev);
> +
> +	mutex_destroy(&gc08a3->mutex);
> +}
> +
> +static const struct of_device_id gc08a3_of_match[] = {
> +	{ .compatible = "GalaxyCore,gc08a3" },
> +	{}
> +};
> +MODULE_DEVICE_TABLE(of, gc08a3_of_match);
> +
> +static const struct dev_pm_ops gc08a3_pm_ops = {
> +	SET_SYSTEM_SLEEP_PM_OPS(gc08a3_suspend, gc08a3_resume)
> +		SET_RUNTIME_PM_OPS(gc08a3_power_off, gc08a3_power_on, NULL)

Fix indentation.

This code has very pooro quality. I suggest you to do first internal
review to avoid commenting on trivial errors and using community
resources for this.


Best regards,
Krzysztof




More information about the linux-arm-kernel mailing list