mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Elgin Perumbilly <elgin.perumbilly@siliconsignals.io>
To: Kennedy Cheskaki <kkituyi@hotmail.com>
Cc: "platform-driver-x86@vger.kernel.org"
	<platform-driver-x86@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-media@vger.kernel.org" <linux-media@vger.kernel.org>,
	Tarang Raval <tarang.raval@siliconsignals.io>
Subject: Re: [PATCH 6/6] media: i2c: Add Himax HM2051 camera sensor driver
Date: Tue, 1 Dec 2026 05:03:48 +0000	[thread overview]
Message-ID: <MA0P287MB2178D157B3B18CC9A967EAC4884D2@MA0P287MB2178.INDP287.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <PA1P192MB326066D4E2A55F925936C1F6B74E2@PA1P192MB3260.EURP192.PROD.OUTLOOK.COM>

Hi Kennedy,

>Add V4L2 subdevice driver for the Himax HM2051 2MP CMOS image sensor
>(ACPI HID HIMX2051), supporting 1616x1216 and 1296x736 at 30 fps.
>Signed-off-by: Kennedy Cheskaki <kkituyi@hotmail.com>
>---
> drivers/media/i2c/hm2051.c        | 614 ++++++++++++++++++++++++++++++
> drivers/media/i2c/hm2051_tables.h | 304 +++++++++++++++
> 2 files changed, 918 insertions(+)
> create mode 100644 drivers/media/i2c/hm2051.c
> create mode 100644 drivers/media/i2c/hm2051_tables.h
>
>diff --git a/drivers/media/i2c/hm2051.c b/drivers/media/i2c/hm2051.c
>new file mode 100644
>index 0000000..9ce0995
>--- /dev/null
>+++ b/drivers/media/i2c/hm2051.c
>@@ -0,0 +1,614 @@
>+// SPDX-License-Identifier: GPL-2.0
>+/*
>+ * Himax HM2051 CMOS image sensor V4L2 subdevice driver
>+ * Register tables and mode parameters extracted from Intel Unicam Windows driver.
>+ */
>+
>+#include <linux/acpi.h>
>+#include <linux/clk.h>
>+#include <linux/delay.h>
>+#include <linux/gpio/consumer.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 <media/v4l2-fwnode.h>
>+#include <media/v4l2-subdev.h>
>+
>+#include "hm2051_tables.h"
>+

You can keep the register tables in the same file.

>+#define HM2051_CHIP_ID_H_REG		0x0001
>+#define HM2051_CHIP_ID_L_REG		0x0002
>+#define HM2051_CHIP_ID			0x2051
>+
>+#define HM2051_REG_EXPOSURE_H		0x0015
>+#define HM2051_REG_EXPOSURE_L		0x0016
>+#define HM2051_REG_GAIN			0x0018
>+#define HM2051_REG_CMU_UPDATE		0x0000
>+
>+#define HM2051_EXPOSURE_MIN		1
>+#define HM2051_EXPOSURE_MAX		0x1fff
>+#define HM2051_EXPOSURE_DEFAULT		4000
>+
>+#define HM2051_GAIN_MIN			0
>+#define HM2051_GAIN_MAX			0xff
>+#define HM2051_GAIN_DEFAULT		200
>+
>+#define HM2051_PIXEL_RATE		48000000ULL
>+#define HM2051_LINK_FREQ		240000000ULL
>+
>+#define HM2051_NATIVE_WIDTH		1616
>+#define HM2051_NATIVE_HEIGHT		1216
>+
>+struct hm2051_mode {
>+	u32 width;
>+	u32 height;
>+	u32 fps;
>+	u32 hts;
>+	u32 vts;
>+	const struct hm2051_reg *regs;
>+	unsigned int num_regs;
>+};
>+
>+static const struct hm2051_mode supported_modes[] = {
>+	{
>+		.width = 1616,
>+		.height = 1216,
>+		.fps = 30,
>+		.hts = 1800,
>+		.vts = 1260,
>+		.regs = hm2051_1616x1216_regs,
>+		.num_regs = ARRAY_SIZE(hm2051_1616x1216_regs),
>+	},
>+	{
>+		.width = 1296,
>+		.height = 736,
>+		.fps = 30,
>+		.hts = 1800,
>+		.vts = 800,
>+		.regs = hm2051_1296x736_regs,
>+		.num_regs = ARRAY_SIZE(hm2051_1296x736_regs),
>+	},
>+};
>+
>+static const s64 link_freq_menu_items[] = {
>+	HM2051_LINK_FREQ,
>+};
>+
>+struct hm2051 {
>+	struct v4l2_subdev sd;
>+	struct media_pad pad;
>+	struct v4l2_ctrl_handler ctrl_handler;
>+	struct clk *xvclk;
>+	struct gpio_desc *reset_gpio;
>+	struct gpio_desc *powerdown_gpio;
>+	struct v4l2_ctrl *pixel_rate;
>+	struct v4l2_ctrl *link_freq;
>+	struct v4l2_ctrl *hblank;
>+	struct v4l2_ctrl *vblank;
>+	struct v4l2_ctrl *exposure;
>+	struct v4l2_ctrl *gain;
>+	const struct hm2051_mode *cur_mode;
>+	struct mutex lock;
>+	bool streaming;
>+};
>+
>+static inline struct hm2051 *to_hm2051(struct v4l2_subdev *sd)
>+{
>+	return container_of(sd, struct hm2051, sd);
>+}

Please use container_of_const() here.

>+
>+static int hm2051_read_reg(struct i2c_client *client, u16 reg, u8 *val)
>+{
>+	u8 buf[2] = { reg >> 8, reg & 0xff };
>+	struct i2c_msg msgs[2] = {
>+		{
>+			.addr = client->addr,
>+			.flags = 0,
>+			.len = sizeof(buf),
>+			.buf = buf,
>+		},
>+		{
>+			.addr = client->addr,
>+			.flags = I2C_M_RD,
>+			.len = 1,
>+			.buf = val,
>+		},
>+	};
>+	int ret;
>+
>+	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
>+	if (ret != ARRAY_SIZE(msgs))
>+		return ret < 0 ? ret : -EIO;
>+
>+	return 0;
>+}
>+
>+static int hm2051_write_reg(struct i2c_client *client, u16 reg, u8 val)
>+{
>+	u8 buf[3] = { reg >> 8, reg & 0xff, val };
>+	int ret;
>+
>+	ret = i2c_master_send(client, buf, sizeof(buf));
>+	if (ret != sizeof(buf))
>+		return ret < 0 ? ret : -EIO;
>+
>+	return 0;
>+}
>+
>+static int hm2051_write_regs(struct i2c_client *client,
>+			     const struct hm2051_reg *regs, unsigned int len)
>+{
>+	unsigned int i;
>+	int ret;
>+
>+	for (i = 0; i < len; i++) {
>+		if (regs[i].reg == HM2051_REG_DELAY) {
>+			msleep(regs[i].val);
>+			continue;
>+		}
>+		ret = hm2051_write_reg(client, regs[i].reg, regs[i].val);
>+		if (ret)
>+			return ret;
>+	}
>+
>+	return 0;
>+}
>+

Use the CCI interface for register read and write operations

>+static int hm2051_power_on(struct hm2051 *hm2051)
>+{
>+	int ret;
>+
>+	ret = clk_prepare_enable(hm2051->xvclk);
>+	if (ret)
>+		return ret;
>+
>+	gpiod_set_value_cansleep(hm2051->powerdown_gpio, 0);
>+	gpiod_set_value_cansleep(hm2051->reset_gpio, 0);
>+	usleep_range(5000, 10000);
>+	return 0;
>+}
>+
>+static void hm2051_power_off(struct hm2051 *hm2051)
>+{
>+	gpiod_set_value_cansleep(hm2051->reset_gpio, 1);
>+	gpiod_set_value_cansleep(hm2051->powerdown_gpio, 1);
>+	clk_disable_unprepare(hm2051->xvclk);
>+}
>+
>+static int hm2051_identify(struct i2c_client *client)
>+{
>+	u8 id_h = 0, id_l = 0;
>+	u16 id;
>+	int ret;
>+
>+	ret = hm2051_read_reg(client, HM2051_CHIP_ID_H_REG, &id_h);
>+	if (ret)
>+		return ret;
>+
>+	ret = hm2051_read_reg(client, HM2051_CHIP_ID_L_REG, &id_l);
>+	if (ret)
>+		return ret;
>+
>+	id = ((u16)id_h << 8) | id_l;
>+	if (id != HM2051_CHIP_ID) {
>+		dev_err(&client->dev, "chip id mismatch: 0x%04x (expected 0x%04x)\n",
>+			id, HM2051_CHIP_ID);
>+		return -ENODEV;
>+	}
>+
>+	dev_info(&client->dev, "found HM2051 sensor (chip id 0x%04x)\n", id);
>+	return 0;
>+}
>+
>+static int hm2051_set_ctrl(struct v4l2_ctrl *ctrl)
>+{
>+	struct hm2051 *hm2051 = container_of(ctrl->handler, struct hm2051,
>+					     ctrl_handler);
>+	struct i2c_client *client = v4l2_get_subdevdata(&hm2051->sd);
>+	int ret = 0;
>+
>+	if (pm_runtime_get_if_in_use(&client->dev) <= 0)
>+		return 0;
>+
>+	switch (ctrl->id) {
>+	case V4L2_CID_EXPOSURE:
>+		ret = hm2051_write_reg(client, HM2051_REG_EXPOSURE_H,
>+				       (ctrl->val >> 8) & 0xff);
>+		if (!ret)
>+			ret = hm2051_write_reg(client, HM2051_REG_EXPOSURE_L,
>+					       ctrl->val & 0xff);
>+		if (!ret)
>+			ret = hm2051_write_reg(client, HM2051_REG_CMU_UPDATE, 0x01);
>+		break;
>+	case V4L2_CID_ANALOGUE_GAIN:
>+		ret = hm2051_write_reg(client, HM2051_REG_GAIN, ctrl->val & 0xff);
>+		if (!ret)
>+			ret = hm2051_write_reg(client, HM2051_REG_CMU_UPDATE, 0x01);
>+		break;
>+	default:
>+		ret = -EINVAL;
>+		break;
>+	}
>+
>+	pm_runtime_put(&client->dev);
>+	return ret;
>+}
>+
>+static const struct v4l2_ctrl_ops hm2051_ctrl_ops = {
>+	.s_ctrl = hm2051_set_ctrl,
>+};
>+
>+static int hm2051_init_controls(struct hm2051 *hm2051)
>+{
>+	struct v4l2_ctrl_handler *hdl = &hm2051->ctrl_handler;
>+	const struct hm2051_mode *mode = hm2051->cur_mode;
>+	s64 hblank, vblank;
>+	int ret;
>+
>+	ret = v4l2_ctrl_handler_init(hdl, 6);
>+	if (ret)
>+		return ret;
>+
>+	hm2051->pixel_rate = v4l2_ctrl_new_std(hdl, &hm2051_ctrl_ops,
>+					       V4L2_CID_PIXEL_RATE,
>+					       HM2051_PIXEL_RATE,
>+					       HM2051_PIXEL_RATE, 1,
>+					       HM2051_PIXEL_RATE);
>+
>+	hm2051->link_freq = v4l2_ctrl_new_int_menu(hdl, &hm2051_ctrl_ops,
>+						   V4L2_CID_LINK_FREQ,
>+						   ARRAY_SIZE(link_freq_menu_items) - 1,
>+						   0, link_freq_menu_items);
>+	if (hm2051->link_freq)
>+		hm2051->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
>+
>+	hblank = mode->hts - mode->width;
>+	hm2051->hblank = v4l2_ctrl_new_std(hdl, &hm2051_ctrl_ops,
>+					   V4L2_CID_HBLANK, hblank, hblank, 1,
>+					   hblank);
>+	if (hm2051->hblank)
>+		hm2051->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
>+
>+	vblank = mode->vts - mode->height;
>+	hm2051->vblank = v4l2_ctrl_new_std(hdl, &hm2051_ctrl_ops,
>+					   V4L2_CID_VBLANK, vblank, vblank, 1,
>+					   vblank);
>+	if (hm2051->vblank)
>+		hm2051->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
>+
>+	hm2051->exposure = v4l2_ctrl_new_std(hdl, &hm2051_ctrl_ops,
>+					     V4L2_CID_EXPOSURE,
>+					     HM2051_EXPOSURE_MIN,
>+					     HM2051_EXPOSURE_MAX, 1,
>+					     HM2051_EXPOSURE_DEFAULT);
>+
>+	hm2051->gain = v4l2_ctrl_new_std(hdl, &hm2051_ctrl_ops,
>+					 V4L2_CID_ANALOGUE_GAIN,
>+					 HM2051_GAIN_MIN, HM2051_GAIN_MAX, 1,
>+					 HM2051_GAIN_DEFAULT);
>+
>+	if (hdl->error) {
>+		ret = hdl->error;
>+		v4l2_ctrl_handler_free(hdl);
>+		return ret;
>+	}
>+
>+	hm2051->sd.ctrl_handler = hdl;
>+	return 0;
>+}
>+
>+static int hm2051_enum_mbus_code(struct v4l2_subdev *sd,
>+				 struct v4l2_subdev_state *sd_state,
>+				 struct v4l2_subdev_mbus_code_enum *code)
>+{
>+	if (code->index > 0)
>+		return -EINVAL;
>+
>+	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
>+	return 0;
>+}
>+
>+static int hm2051_enum_frame_size(struct v4l2_subdev *sd,
>+				  struct v4l2_subdev_state *sd_state,
>+				  struct v4l2_subdev_frame_size_enum *fse)
>+{
>+	if (fse->index >= ARRAY_SIZE(supported_modes))
>+		return -EINVAL;
>+
>+	if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
>+		return -EINVAL;
>+
>+	fse->min_width = supported_modes[fse->index].width;
>+	fse->max_width = fse->min_width;
>+	fse->min_height = supported_modes[fse->index].height;
>+	fse->max_height = fse->min_height;
>+	return 0;
>+}
>+
>+static int hm2051_enum_frame_interval(struct v4l2_subdev *sd,
>+				      struct v4l2_subdev_state *sd_state,
>+				      struct v4l2_subdev_frame_interval_enum *fie)
>+{
>+	unsigned int i;
>+
>+	if (fie->index > 0 || fie->code != MEDIA_BUS_FMT_SBGGR10_1X10)
>+		return -EINVAL;
>+
>+	for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
>+		if (fie->width == supported_modes[i].width &&
>+		    fie->height == supported_modes[i].height) {
>+			fie->interval.numerator = 1;
>+			fie->interval.denominator = supported_modes[i].fps;
>+			return 0;
>+		}
>+	}
>+
>+	return -EINVAL;
>+}
>+
>+static void hm2051_fill_pad_format(const struct hm2051_mode *mode,
>+				   struct v4l2_mbus_framefmt *fmt)
>+{
>+	fmt->width = mode->width;
>+	fmt->height = mode->height;
>+	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
>+	fmt->field = V4L2_FIELD_NONE;
>+	fmt->colorspace = V4L2_COLORSPACE_RAW;
>+}
>+
>+static int hm2051_init_state(struct v4l2_subdev *sd,
>+			     struct v4l2_subdev_state *sd_state)
>+{
>+	struct v4l2_mbus_framefmt *fmt =
>+		v4l2_subdev_state_get_format(sd_state, 0);
>+
>+	hm2051_fill_pad_format(&supported_modes[0], fmt);
>+	return 0;
>+}
>+
>+static int hm2051_set_pad_format(struct v4l2_subdev *sd,
>+				 struct v4l2_subdev_state *sd_state,
>+				 struct v4l2_subdev_format *fmt)
>+{
>+	struct hm2051 *hm2051 = to_hm2051(sd);
>+	const struct hm2051_mode *mode;
>+	struct v4l2_mbus_framefmt *framefmt;
>+
>+	mode = v4l2_find_nearest_size(supported_modes,
>+				      ARRAY_SIZE(supported_modes),
>+				      width, height,
>+				      fmt->format.width, fmt->format.height);
>+
>+	hm2051_fill_pad_format(mode, &fmt->format);
>+
>+	framefmt = v4l2_subdev_state_get_format(sd_state, 0);
>+	*framefmt = fmt->format;
>+
>+	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
>+		mutex_lock(&hm2051->lock);
>+		hm2051->cur_mode = mode;
>+		mutex_unlock(&hm2051->lock);
>+	}
>+
>+	return 0;
>+}
>+
>+static int hm2051_get_selection(struct v4l2_subdev *sd,
>+				struct v4l2_subdev_state *sd_state,
>+				struct v4l2_subdev_selection *sel)
>+{
>+	switch (sel->target) {
>+	case V4L2_SEL_TGT_NATIVE_SIZE:
>+	case V4L2_SEL_TGT_CROP_BOUNDS:
>+	case V4L2_SEL_TGT_CROP_DEFAULT:
>+		sel->r.left = 0;
>+		sel->r.top = 0;
>+		sel->r.width = HM2051_NATIVE_WIDTH;
>+		sel->r.height = HM2051_NATIVE_HEIGHT;
>+		return 0;
>+	default:
>+		return -EINVAL;
>+	}
>+}
>+
>+static int hm2051_s_stream(struct v4l2_subdev *sd, int enable)
>+{
>+	struct hm2051 *hm2051 = to_hm2051(sd);
>+	struct i2c_client *client = v4l2_get_subdevdata(sd);
>+	int ret = 0;
>+

Use the enable_streams and disable_streams pad ops.

>+	mutex_lock(&hm2051->lock);
>+
>+	if (hm2051->streaming == !!enable)
>+		goto unlock;
>+
>+	if (enable) {
>+		ret = pm_runtime_resume_and_get(&client->dev);
>+		if (ret < 0)
>+			goto unlock;
>+
>+		ret = hm2051_write_regs(client, hm2051_init_regs,
>+					ARRAY_SIZE(hm2051_init_regs));
>+		if (ret)
>+			goto err_rpm;
>+
>+		ret = hm2051_write_regs(client, hm2051->cur_mode->regs,
>+					hm2051->cur_mode->num_regs);
>+		if (ret)
>+			goto err_rpm;
>+
>+		ret = __v4l2_ctrl_handler_setup(&hm2051->ctrl_handler);
>+		if (ret)
>+			goto err_rpm;
>+
>+		ret = hm2051_write_regs(client, hm2051_stream_on_regs,
>+					ARRAY_SIZE(hm2051_stream_on_regs));
>+		if (ret)
>+			goto err_rpm;
>+
>+		hm2051->streaming = true;
>+	} else {
>+		hm2051_write_regs(client, hm2051_stream_off_regs,
>+				  ARRAY_SIZE(hm2051_stream_off_regs));
>+		pm_runtime_put(&client->dev);
>+		hm2051->streaming = false;
>+	}
>+
>+	mutex_unlock(&hm2051->lock);
>+	return 0;
>+
>+err_rpm:
>+	pm_runtime_put(&client->dev);
>+unlock:
>+	mutex_unlock(&hm2051->lock);
>+	return ret;
>+}
>+

Best Regards,
Elgin

      reply	other threads:[~2026-12-01  5:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261130100835.12189-1-kkituyi@hotmail.com>
2026-11-30 10:08 ` [PATCH 4/6] media: atomisp: Add Athesi RT80 sensors and 180-deg rotation quirk Kennedy Cheskaki
2026-11-30 10:08 ` [PATCH 5/6] media: ov8858: Add ACPI ID INT3477 Kennedy Cheskaki
2026-11-30 10:08 ` [PATCH 6/6] media: i2c: Add Himax HM2051 camera sensor driver Kennedy Cheskaki
2026-12-01  5:03   ` Elgin Perumbilly [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=MA0P287MB2178D157B3B18CC9A967EAC4884D2@MA0P287MB2178.INDP287.PROD.OUTLOOK.COM \
    --to=elgin.perumbilly@siliconsignals.io \
    --cc=kkituyi@hotmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=tarang.raval@siliconsignals.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®