mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/3] media: i2c: lc898217xc: initial driver
@ 2025-12-01 12:40 Vasiliy Doylov
  2025-12-01 12:40 ` [PATCH v3 1/3] media: dt-bindings: Add LC898217XC documentation Vasiliy Doylov
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Vasiliy Doylov @ 2025-12-01 12:40 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, david
  Cc: linux-media, devicetree, linux-kernel, phone-devel,
	Vitalii Skorkin, Antonio Rische, Vasiliy Doylov,
	Krzysztof Kozlowski

LX898217XC is a 11 bit DAC, designed for linear control
of voice coil motor. This driver creates a V4L2 subdevice
and provides control to set the desired focus.

Tested on Oneplus 6 (oneplus-enchilada)

Co-developed-by: Vitalii Skorkin <nikroks@mainlining.org>
Signed-off-by: Vitalii Skorkin <nikroks@mainlining.org>
Co-developed-by: Antonio Rische <nt8r@protonmail.com>
Signed-off-by: Antonio Rische <nt8r@protonmail.com>
Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
---
Changes in v3:
- Fixed MAINTAINERS (Krzysztof)
- Reordered commits (Krzysztof)
- Removed blank line from device-tree documentation (Krzysztof)
- Refactored to use CCI regmap
- Refactored to use dev_err_probe in probe
- Link to v2: https://lore.kernel.org/all/20250304-media-i2c-lc898217xc-initial-driver-v2-0-6a463cef3ea8@mainlining.org/
Changes in v2:
- PM functions annotated as __maybe_unused.
- Fixed dt bindings documentation commit message
- Added v4l2 events (now v4l2-compliance shows no failed tests)
- Link to v1: https://lore.kernel.org/r/20250304-media-i2c-lc898217xc-initial-driver-v1-0-e2ffd2b2fd5e@mainlining.org

---
Vasiliy Doylov (3):
      media: dt-bindings: Add LC898217XC documentation
      media: i2c: Add driver for LC898217XC VCM
      MAINTAINERS: Add entry for Onsemi LC898217XC lens voice coil driver

 .../bindings/media/i2c/onnn,lc898217xc.yaml        |  53 +++++
 MAINTAINERS                                        |   7 +
 drivers/media/i2c/Kconfig                          |   9 +
 drivers/media/i2c/Makefile                         |   1 +
 drivers/media/i2c/lc898217xc.c                     | 254 +++++++++++++++++++++
 5 files changed, 324 insertions(+)
---
base-commit: 7d31f578f3230f3b7b33b0930b08f9afd8429817
change-id: 20250227-media-i2c-lc898217xc-initial-driver-d7b50a135ce5

Best regards,
-- 
Vasiliy Doylov <nekocwd@mainlining.org>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 1/3] media: dt-bindings: Add LC898217XC documentation
  2025-12-01 12:40 [PATCH v3 0/3] media: i2c: lc898217xc: initial driver Vasiliy Doylov
@ 2025-12-01 12:40 ` Vasiliy Doylov
  2025-12-01 12:40 ` [PATCH v3 2/3] media: i2c: Add driver for LC898217XC VCM Vasiliy Doylov
  2025-12-01 12:40 ` [PATCH v3 3/3] MAINTAINERS: Add entry for Onsemi LC898217XC lens voice coil driver Vasiliy Doylov
  2 siblings, 0 replies; 6+ messages in thread
From: Vasiliy Doylov @ 2025-12-01 12:40 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, david
  Cc: linux-media, devicetree, linux-kernel, phone-devel,
	Vitalii Skorkin, Antonio Rische, Vasiliy Doylov,
	Krzysztof Kozlowski

Add device tree bindings documentation for ON Semiconductor
LC898217XC voice coil motor.

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
---
 .../bindings/media/i2c/onnn,lc898217xc.yaml        | 53 ++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/Documentation/devicetree/bindings/media/i2c/onnn,lc898217xc.yaml b/Documentation/devicetree/bindings/media/i2c/onnn,lc898217xc.yaml
new file mode 100644
index 000000000000..cadd14c4381f
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/i2c/onnn,lc898217xc.yaml
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+# Copyright 2025 Vasiliy Doylov <nekocwd@mainlining.org>
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/i2c/onnn,lc898217xc.yaml##
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: ON Semiconductor LC898217XC Voice Coil Motor (VCM) Lens
+
+maintainers:
+  - Vasiliy Doylov <nekocwd@mainlining.org>
+
+description:
+  The LC898217XC is a 11-bit digital-to-analog (DAC) converter.
+  VCM current is controlled with a linear mode driver.The DAC is controlled
+  via a 2-wire (I2C-compatible) serial interface that operates at clock
+  rates up to 1MHz. This chip integrates Advanced Actuator Control (AAC)
+  technology and is intended for driving voice coil lenses in camera modules.
+
+properties:
+  compatible:
+    enum:
+      - onnn,lc898217xc
+
+  reg:
+    maxItems: 1
+
+  vcc-supply:
+    description:
+      Definition of the regulator used as voltage supply.
+
+required:
+  - compatible
+  - reg
+  - vcc-supply
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        camera-lens@74 {
+            compatible = "onnn,lc898217xc";
+            reg = <0x74>;
+
+            vcc-supply = <&mt6358_vcamio_reg>;
+        };
+    };
+
+...

-- 
2.52.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 2/3] media: i2c: Add driver for LC898217XC VCM
  2025-12-01 12:40 [PATCH v3 0/3] media: i2c: lc898217xc: initial driver Vasiliy Doylov
  2025-12-01 12:40 ` [PATCH v3 1/3] media: dt-bindings: Add LC898217XC documentation Vasiliy Doylov
@ 2025-12-01 12:40 ` Vasiliy Doylov
  2025-12-01 14:42   ` Dave Stevenson
  2025-12-01 12:40 ` [PATCH v3 3/3] MAINTAINERS: Add entry for Onsemi LC898217XC lens voice coil driver Vasiliy Doylov
  2 siblings, 1 reply; 6+ messages in thread
From: Vasiliy Doylov @ 2025-12-01 12:40 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, david
  Cc: linux-media, devicetree, linux-kernel, phone-devel,
	Vitalii Skorkin, Antonio Rische, Vasiliy Doylov

LC898217XC is a 11 bit DAC, designed for linear control
of voice coil motor. This driver creates a V4L2 subdevice
and provides control to set the desired focus.

Tested on Oneplus 6 (oneplus-enchilada)

Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
---
 drivers/media/i2c/Kconfig      |   9 ++
 drivers/media/i2c/Makefile     |   1 +
 drivers/media/i2c/lc898217xc.c | 254 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 264 insertions(+)

diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 4b4db8c4f496..ee071505223f 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -866,6 +866,15 @@ config VIDEO_DW9807_VCM
 	  capability. This is designed for linear control of
 	  voice coil motors, controlled via I2C serial interface.
 
+config VIDEO_LC898217XC
+	tristate "LC898217XC lens voice coil support"
+	select V4L2_CCI_I2C
+	help
+	  This is a driver for the LC898217XC camera lens voice coil.
+	  LC898217XC is a 11 bit DAC with 110mA output current sink
+	  capability. This is designed for linear control of
+	  voice coil motors, controlled via I2C serial interface.
+
 endif
 
 menu "Flash devices"
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index c5f17602454f..a17774eeb43c 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -64,6 +64,7 @@ obj-$(CONFIG_VIDEO_IMX415) += imx415.o
 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
+obj-$(CONFIG_VIDEO_LC898217XC) += lc898217xc.o
 obj-$(CONFIG_VIDEO_LM3560) += lm3560.o
 obj-$(CONFIG_VIDEO_LM3646) += lm3646.o
 obj-$(CONFIG_VIDEO_LT6911UXE) += lt6911uxe.o
diff --git a/drivers/media/i2c/lc898217xc.c b/drivers/media/i2c/lc898217xc.c
new file mode 100644
index 000000000000..c2d75bd36ddb
--- /dev/null
+++ b/drivers/media/i2c/lc898217xc.c
@@ -0,0 +1,254 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// Copyright (c) 2025 Vasiliy Doylov <nekocwd@mainlining.org>
+
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-cci.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+#include <media/v4l2-event.h>
+
+#define LC898217XC_NAME "lc898217xc"
+/* Actuator has 11 bit resolution */
+#define LC898217XC_MAX_FOCUS_POS (2048 - 1)
+#define LC898217XC_MIN_FOCUS_POS 0
+#define LC898217XC_FOCUS_STEPS 1
+#define LC898217XC_DAC_ADDR CCI_REG16(0x84)
+
+static const char *const lc898217xc_supply_names[] = {
+	"vcc",
+};
+
+struct lc898217xc {
+	struct regulator_bulk_data supplies[ARRAY_SIZE(lc898217xc_supply_names)];
+	struct v4l2_ctrl_handler ctrls;
+	struct v4l2_ctrl *focus;
+	struct v4l2_subdev sd;
+	struct regmap *regmap;
+};
+
+static inline struct lc898217xc *sd_to_lc898217xc(struct v4l2_subdev *subdev)
+{
+	return container_of(subdev, struct lc898217xc, sd);
+}
+
+static int lc898217xc_set_dac(struct lc898217xc *lc898217xc, u16 val)
+{
+	struct i2c_client *client = v4l2_get_subdevdata(&lc898217xc->sd);
+	int ret;
+
+	ret = cci_write(lc898217xc->regmap, LC898217XC_DAC_ADDR, val, NULL);
+	if (ret)
+		dev_err(&client->dev, "failed to set DAC: %d\n", ret);
+
+	return ret;
+}
+
+static int __maybe_unused lc898217xc_runtime_suspend(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct lc898217xc *lc898217xc = sd_to_lc898217xc(sd);
+
+	regulator_bulk_disable(ARRAY_SIZE(lc898217xc_supply_names),
+			       lc898217xc->supplies);
+
+	return 0;
+}
+
+static int __maybe_unused lc898217xc_runtime_resume(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct lc898217xc *lc898217xc = sd_to_lc898217xc(sd);
+	int ret;
+
+	ret = regulator_bulk_enable(ARRAY_SIZE(lc898217xc_supply_names),
+				    lc898217xc->supplies);
+
+	if (ret < 0) {
+		dev_err(dev, "failed to enable regulators\n");
+		return ret;
+	}
+
+	usleep_range(8000, 10000);
+
+	return ret;
+}
+
+static int lc898217xc_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct lc898217xc *lc898217xc = container_of(ctrl->handler,
+						     struct lc898217xc, ctrls);
+
+	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
+		return lc898217xc_set_dac(lc898217xc, ctrl->val);
+
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops lc898217xc_ctrl_ops = {
+	.s_ctrl = lc898217xc_set_ctrl,
+};
+
+static int lc898217xc_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+	return pm_runtime_resume_and_get(sd->dev);
+}
+
+static int lc898217xc_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+	pm_runtime_mark_last_busy(sd->dev);
+	pm_runtime_put_autosuspend(sd->dev);
+
+	return 0;
+}
+
+static const struct v4l2_subdev_internal_ops lc898217xc_int_ops = {
+	.open = lc898217xc_open,
+	.close = lc898217xc_close,
+};
+
+static const struct v4l2_subdev_core_ops lc898217xc_core_ops = {
+	.log_status = v4l2_ctrl_subdev_log_status,
+	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
+	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
+};
+
+static const struct v4l2_subdev_ops lc898217xc_ops = {
+	.core = &lc898217xc_core_ops,
+};
+
+static int lc898217xc_init_controls(struct lc898217xc *lc898217xc)
+{
+	struct v4l2_ctrl_handler *hdl = &lc898217xc->ctrls;
+	const struct v4l2_ctrl_ops *ops = &lc898217xc_ctrl_ops;
+
+	v4l2_ctrl_handler_init(hdl, 1);
+
+	lc898217xc->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
+					      LC898217XC_MIN_FOCUS_POS,
+					      LC898217XC_MAX_FOCUS_POS,
+					      LC898217XC_FOCUS_STEPS, 0);
+
+	if (hdl->error)
+		return hdl->error;
+
+	lc898217xc->sd.ctrl_handler = hdl;
+
+	return 0;
+}
+
+static int lc898217xc_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct lc898217xc *lc898217xc;
+	unsigned int i;
+	int ret;
+
+	lc898217xc = devm_kzalloc(dev, sizeof(*lc898217xc), GFP_KERNEL);
+	if (!lc898217xc)
+		return -ENOMEM;
+
+	lc898217xc->regmap = devm_cci_regmap_init_i2c(client, 8);
+	if (IS_ERR(lc898217xc->regmap))
+		return dev_err_probe(dev, PTR_ERR(lc898217xc->regmap),
+				     "failed to initialize CCI\n");
+
+	/* Initialize subdev */
+	v4l2_i2c_subdev_init(&lc898217xc->sd, client, &lc898217xc_ops);
+
+	for (i = 0; i < ARRAY_SIZE(lc898217xc_supply_names); i++)
+		lc898217xc->supplies[i].supply = lc898217xc_supply_names[i];
+
+	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(lc898217xc_supply_names),
+				      lc898217xc->supplies);
+
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to get regulators\n");
+
+	/* Initialize controls */
+	ret = lc898217xc_init_controls(lc898217xc);
+	if (ret) {
+		dev_err_probe(dev, ret, "failed to init v4l2 controls\n");
+		goto err_free_handler;
+	}
+
+	/* Initialize subdev */
+	lc898217xc->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
+				V4L2_SUBDEV_FL_HAS_EVENTS;
+	lc898217xc->sd.internal_ops = &lc898217xc_int_ops;
+
+	ret = media_entity_pads_init(&lc898217xc->sd.entity, 0, NULL);
+	if (ret < 0) {
+		dev_err_probe(dev, ret, "failed to init media entity pads");
+		goto err_free_handler;
+	}
+
+	lc898217xc->sd.entity.function = MEDIA_ENT_F_LENS;
+
+	pm_runtime_enable(dev);
+	ret = v4l2_async_register_subdev(&lc898217xc->sd);
+
+	if (ret < 0) {
+		dev_err_probe(dev, ret, "failed to register V4L2 subdev\n");
+		goto err_power_off;
+	}
+
+	pm_runtime_set_autosuspend_delay(dev, 1000);
+	pm_runtime_use_autosuspend(dev);
+	pm_runtime_idle(dev);
+
+	return 0;
+
+err_power_off:
+	pm_runtime_disable(dev);
+	media_entity_cleanup(&lc898217xc->sd.entity);
+err_free_handler:
+	v4l2_ctrl_handler_free(&lc898217xc->ctrls);
+
+	return ret;
+}
+
+static void lc898217xc_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct lc898217xc *lc898217xc = sd_to_lc898217xc(sd);
+	struct device *dev = &client->dev;
+
+	v4l2_async_unregister_subdev(&lc898217xc->sd);
+	v4l2_ctrl_handler_free(&lc898217xc->ctrls);
+	media_entity_cleanup(&lc898217xc->sd.entity);
+	pm_runtime_disable(dev);
+}
+
+static const struct of_device_id lc898217xc_of_table[] = {
+	{ .compatible = "onnn,lc898217xc" },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, lc898217xc_of_table);
+
+static const struct dev_pm_ops lc898217xc_pm_ops = {
+	SET_RUNTIME_PM_OPS(lc898217xc_runtime_suspend,
+			   lc898217xc_runtime_resume, NULL)
+};
+
+static struct i2c_driver lc898217xc_i2c_driver = {
+	.driver = {
+		.name = LC898217XC_NAME,
+		.pm = &lc898217xc_pm_ops,
+		.of_match_table = lc898217xc_of_table,
+	},
+	.probe = lc898217xc_probe,
+	.remove = lc898217xc_remove,
+};
+module_i2c_driver(lc898217xc_i2c_driver);
+
+MODULE_AUTHOR("Vasiliy Doylov <nekocwd@mainlining.org>");
+MODULE_DESCRIPTION("Onsemi LC898217XC VCM driver");
+MODULE_LICENSE("GPL");

-- 
2.52.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 3/3] MAINTAINERS: Add entry for Onsemi LC898217XC lens voice coil driver
  2025-12-01 12:40 [PATCH v3 0/3] media: i2c: lc898217xc: initial driver Vasiliy Doylov
  2025-12-01 12:40 ` [PATCH v3 1/3] media: dt-bindings: Add LC898217XC documentation Vasiliy Doylov
  2025-12-01 12:40 ` [PATCH v3 2/3] media: i2c: Add driver for LC898217XC VCM Vasiliy Doylov
@ 2025-12-01 12:40 ` Vasiliy Doylov
  2 siblings, 0 replies; 6+ messages in thread
From: Vasiliy Doylov @ 2025-12-01 12:40 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, david
  Cc: linux-media, devicetree, linux-kernel, phone-devel,
	Vitalii Skorkin, Antonio Rische, Vasiliy Doylov

Add entry for Onsemi LC898217XC lens voice coil driver

Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 80dcd13687e6..004ff1ab535b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -19501,6 +19501,13 @@ S:	Supported
 W:	http://www.onsemi.com
 F:	drivers/net/phy/ncn*
 
+ONSEMI LC898217XC LENS VOICE COIL DRIVER
+M:	Vasiliy Doylov <nekocwd@mainlining.org>
+L:	linux-media@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/media/i2c/onnn,lc898217xc.yaml
+F:	drivers/media/i2c/lc898217xc.c
+
 OP-TEE DRIVER
 M:	Jens Wiklander <jens.wiklander@linaro.org>
 L:	op-tee@lists.trustedfirmware.org

-- 
2.52.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/3] media: i2c: Add driver for LC898217XC VCM
  2025-12-01 12:40 ` [PATCH v3 2/3] media: i2c: Add driver for LC898217XC VCM Vasiliy Doylov
@ 2025-12-01 14:42   ` Dave Stevenson
  2025-12-03  9:21     ` Vasiliy Doylov
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Stevenson @ 2025-12-01 14:42 UTC (permalink / raw)
  To: Vasiliy Doylov
  Cc: Mauro Carvalho Chehab, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, david, linux-media, devicetree, linux-kernel,
	phone-devel, Vitalii Skorkin, Antonio Rische

Hi Vasiliy

On Mon, 1 Dec 2025 at 12:42, Vasiliy Doylov <nekocwd@mainlining.org> wrote:
>
> LC898217XC is a 11 bit DAC, designed for linear control
> of voice coil motor. This driver creates a V4L2 subdevice
> and provides control to set the desired focus.
>
> Tested on Oneplus 6 (oneplus-enchilada)
>
> Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
> ---
>  drivers/media/i2c/Kconfig      |   9 ++
>  drivers/media/i2c/Makefile     |   1 +
>  drivers/media/i2c/lc898217xc.c | 254 +++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 264 insertions(+)
>
> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
> index 4b4db8c4f496..ee071505223f 100644
> --- a/drivers/media/i2c/Kconfig
> +++ b/drivers/media/i2c/Kconfig
> @@ -866,6 +866,15 @@ config VIDEO_DW9807_VCM
>           capability. This is designed for linear control of
>           voice coil motors, controlled via I2C serial interface.
>
> +config VIDEO_LC898217XC
> +       tristate "LC898217XC lens voice coil support"
> +       select V4L2_CCI_I2C
> +       help
> +         This is a driver for the LC898217XC camera lens voice coil.
> +         LC898217XC is a 11 bit DAC with 110mA output current sink
> +         capability. This is designed for linear control of
> +         voice coil motors, controlled via I2C serial interface.
> +
>  endif
>
>  menu "Flash devices"
> diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
> index c5f17602454f..a17774eeb43c 100644
> --- a/drivers/media/i2c/Makefile
> +++ b/drivers/media/i2c/Makefile
> @@ -64,6 +64,7 @@ obj-$(CONFIG_VIDEO_IMX415) += imx415.o
>  obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
>  obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
>  obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
> +obj-$(CONFIG_VIDEO_LC898217XC) += lc898217xc.o
>  obj-$(CONFIG_VIDEO_LM3560) += lm3560.o
>  obj-$(CONFIG_VIDEO_LM3646) += lm3646.o
>  obj-$(CONFIG_VIDEO_LT6911UXE) += lt6911uxe.o
> diff --git a/drivers/media/i2c/lc898217xc.c b/drivers/media/i2c/lc898217xc.c
> new file mode 100644
> index 000000000000..c2d75bd36ddb
> --- /dev/null
> +++ b/drivers/media/i2c/lc898217xc.c
> @@ -0,0 +1,254 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +// Copyright (c) 2025 Vasiliy Doylov <nekocwd@mainlining.org>
> +
> +#include <linux/delay.h>
> +#include <linux/i2c.h>
> +#include <linux/module.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/regulator/consumer.h>
> +#include <media/v4l2-async.h>
> +#include <media/v4l2-cci.h>
> +#include <media/v4l2-ctrls.h>
> +#include <media/v4l2-device.h>
> +#include <media/v4l2-fwnode.h>
> +#include <media/v4l2-subdev.h>
> +#include <media/v4l2-event.h>
> +
> +#define LC898217XC_NAME "lc898217xc"
> +/* Actuator has 11 bit resolution */
> +#define LC898217XC_MAX_FOCUS_POS (2048 - 1)
> +#define LC898217XC_MIN_FOCUS_POS 0
> +#define LC898217XC_FOCUS_STEPS 1
> +#define LC898217XC_DAC_ADDR CCI_REG16(0x84)
> +
> +static const char *const lc898217xc_supply_names[] = {
> +       "vcc",
> +};
> +
> +struct lc898217xc {
> +       struct regulator_bulk_data supplies[ARRAY_SIZE(lc898217xc_supply_names)];
> +       struct v4l2_ctrl_handler ctrls;
> +       struct v4l2_ctrl *focus;

focus is never used.

> +       struct v4l2_subdev sd;
> +       struct regmap *regmap;
> +};
> +
> +static inline struct lc898217xc *sd_to_lc898217xc(struct v4l2_subdev *subdev)
> +{
> +       return container_of(subdev, struct lc898217xc, sd);
> +}
> +
> +static int lc898217xc_set_dac(struct lc898217xc *lc898217xc, u16 val)
> +{
> +       struct i2c_client *client = v4l2_get_subdevdata(&lc898217xc->sd);
> +       int ret;
> +
> +       ret = cci_write(lc898217xc->regmap, LC898217XC_DAC_ADDR, val, NULL);
> +       if (ret)
> +               dev_err(&client->dev, "failed to set DAC: %d\n", ret);
> +
> +       return ret;
> +}
> +
> +static int __maybe_unused lc898217xc_runtime_suspend(struct device *dev)
> +{
> +       struct v4l2_subdev *sd = dev_get_drvdata(dev);
> +       struct lc898217xc *lc898217xc = sd_to_lc898217xc(sd);
> +
> +       regulator_bulk_disable(ARRAY_SIZE(lc898217xc_supply_names),
> +                              lc898217xc->supplies);
> +
> +       return 0;
> +}
> +
> +static int __maybe_unused lc898217xc_runtime_resume(struct device *dev)
> +{
> +       struct v4l2_subdev *sd = dev_get_drvdata(dev);
> +       struct lc898217xc *lc898217xc = sd_to_lc898217xc(sd);
> +       int ret;
> +
> +       ret = regulator_bulk_enable(ARRAY_SIZE(lc898217xc_supply_names),
> +                                   lc898217xc->supplies);
> +
> +       if (ret < 0) {
> +               dev_err(dev, "failed to enable regulators\n");
> +               return ret;
> +       }
> +
> +       usleep_range(8000, 10000);

Do you not want to restore the current position after resuming power?
Potentially use  __v4l2_ctrl_handler_setup() to reset all controls
should any extra ones get added.

> +
> +       return ret;
> +}
> +
> +static int lc898217xc_set_ctrl(struct v4l2_ctrl *ctrl)
> +{
> +       struct lc898217xc *lc898217xc = container_of(ctrl->handler,
> +                                                    struct lc898217xc, ctrls);
> +
> +       if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
> +               return lc898217xc_set_dac(lc898217xc, ctrl->val);
> +
> +       return 0;
> +}
> +
> +static const struct v4l2_ctrl_ops lc898217xc_ctrl_ops = {
> +       .s_ctrl = lc898217xc_set_ctrl,
> +};
> +
> +static int lc898217xc_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
> +{
> +       return pm_runtime_resume_and_get(sd->dev);
> +}
> +
> +static int lc898217xc_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
> +{
> +       pm_runtime_mark_last_busy(sd->dev);
> +       pm_runtime_put_autosuspend(sd->dev);
> +
> +       return 0;
> +}
> +
> +static const struct v4l2_subdev_internal_ops lc898217xc_int_ops = {
> +       .open = lc898217xc_open,
> +       .close = lc898217xc_close,
> +};
> +
> +static const struct v4l2_subdev_core_ops lc898217xc_core_ops = {
> +       .log_status = v4l2_ctrl_subdev_log_status,
> +       .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
> +       .unsubscribe_event = v4l2_event_subdev_unsubscribe,

subscribe and unsubscribe_event hooks shouldn't be needed if it's only
for controls since e7724e23196a ("media: v4l2-subdev: Refactor
events"). That may not have been merged when you added this in v2.

> +};
> +
> +static const struct v4l2_subdev_ops lc898217xc_ops = {
> +       .core = &lc898217xc_core_ops,
> +};
> +
> +static int lc898217xc_init_controls(struct lc898217xc *lc898217xc)
> +{
> +       struct v4l2_ctrl_handler *hdl = &lc898217xc->ctrls;
> +       const struct v4l2_ctrl_ops *ops = &lc898217xc_ctrl_ops;
> +
> +       v4l2_ctrl_handler_init(hdl, 1);
> +
> +       lc898217xc->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
> +                                             LC898217XC_MIN_FOCUS_POS,
> +                                             LC898217XC_MAX_FOCUS_POS,
> +                                             LC898217XC_FOCUS_STEPS, 0);
> +
> +       if (hdl->error)
> +               return hdl->error;
> +
> +       lc898217xc->sd.ctrl_handler = hdl;
> +
> +       return 0;
> +}
> +
> +static int lc898217xc_probe(struct i2c_client *client)
> +{
> +       struct device *dev = &client->dev;
> +       struct lc898217xc *lc898217xc;
> +       unsigned int i;
> +       int ret;
> +
> +       lc898217xc = devm_kzalloc(dev, sizeof(*lc898217xc), GFP_KERNEL);
> +       if (!lc898217xc)
> +               return -ENOMEM;
> +
> +       lc898217xc->regmap = devm_cci_regmap_init_i2c(client, 8);
> +       if (IS_ERR(lc898217xc->regmap))
> +               return dev_err_probe(dev, PTR_ERR(lc898217xc->regmap),
> +                                    "failed to initialize CCI\n");
> +
> +       /* Initialize subdev */
> +       v4l2_i2c_subdev_init(&lc898217xc->sd, client, &lc898217xc_ops);
> +
> +       for (i = 0; i < ARRAY_SIZE(lc898217xc_supply_names); i++)
> +               lc898217xc->supplies[i].supply = lc898217xc_supply_names[i];
> +
> +       ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(lc898217xc_supply_names),
> +                                     lc898217xc->supplies);
> +
> +       if (ret)
> +               return dev_err_probe(dev, ret, "failed to get regulators\n");
> +
> +       /* Initialize controls */
> +       ret = lc898217xc_init_controls(lc898217xc);
> +       if (ret) {
> +               dev_err_probe(dev, ret, "failed to init v4l2 controls\n");
> +               goto err_free_handler;
> +       }
> +
> +       /* Initialize subdev */
> +       lc898217xc->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
> +                               V4L2_SUBDEV_FL_HAS_EVENTS;

e7724e23196a  also removes the need for you to set
V4L2_SUBDEV_FL_HAS_EVENTS if you have a ctrl_handler.

  Dave

> +       lc898217xc->sd.internal_ops = &lc898217xc_int_ops;
> +
> +       ret = media_entity_pads_init(&lc898217xc->sd.entity, 0, NULL);
> +       if (ret < 0) {
> +               dev_err_probe(dev, ret, "failed to init media entity pads");
> +               goto err_free_handler;
> +       }
> +
> +       lc898217xc->sd.entity.function = MEDIA_ENT_F_LENS;
> +
> +       pm_runtime_enable(dev);
> +       ret = v4l2_async_register_subdev(&lc898217xc->sd);
> +
> +       if (ret < 0) {
> +               dev_err_probe(dev, ret, "failed to register V4L2 subdev\n");
> +               goto err_power_off;
> +       }
> +
> +       pm_runtime_set_autosuspend_delay(dev, 1000);
> +       pm_runtime_use_autosuspend(dev);
> +       pm_runtime_idle(dev);
> +
> +       return 0;
> +
> +err_power_off:
> +       pm_runtime_disable(dev);
> +       media_entity_cleanup(&lc898217xc->sd.entity);
> +err_free_handler:
> +       v4l2_ctrl_handler_free(&lc898217xc->ctrls);
> +
> +       return ret;
> +}
> +
> +static void lc898217xc_remove(struct i2c_client *client)
> +{
> +       struct v4l2_subdev *sd = i2c_get_clientdata(client);
> +       struct lc898217xc *lc898217xc = sd_to_lc898217xc(sd);
> +       struct device *dev = &client->dev;
> +
> +       v4l2_async_unregister_subdev(&lc898217xc->sd);
> +       v4l2_ctrl_handler_free(&lc898217xc->ctrls);
> +       media_entity_cleanup(&lc898217xc->sd.entity);
> +       pm_runtime_disable(dev);
> +}
> +
> +static const struct of_device_id lc898217xc_of_table[] = {
> +       { .compatible = "onnn,lc898217xc" },
> +       { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(of, lc898217xc_of_table);
> +
> +static const struct dev_pm_ops lc898217xc_pm_ops = {
> +       SET_RUNTIME_PM_OPS(lc898217xc_runtime_suspend,
> +                          lc898217xc_runtime_resume, NULL)
> +};
> +
> +static struct i2c_driver lc898217xc_i2c_driver = {
> +       .driver = {
> +               .name = LC898217XC_NAME,
> +               .pm = &lc898217xc_pm_ops,
> +               .of_match_table = lc898217xc_of_table,
> +       },
> +       .probe = lc898217xc_probe,
> +       .remove = lc898217xc_remove,
> +};
> +module_i2c_driver(lc898217xc_i2c_driver);
> +
> +MODULE_AUTHOR("Vasiliy Doylov <nekocwd@mainlining.org>");
> +MODULE_DESCRIPTION("Onsemi LC898217XC VCM driver");
> +MODULE_LICENSE("GPL");
>
> --
> 2.52.0
>
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/3] media: i2c: Add driver for LC898217XC VCM
  2025-12-01 14:42   ` Dave Stevenson
@ 2025-12-03  9:21     ` Vasiliy Doylov
  0 siblings, 0 replies; 6+ messages in thread
From: Vasiliy Doylov @ 2025-12-03  9:21 UTC (permalink / raw)
  To: Dave Stevenson
  Cc: Mauro Carvalho Chehab, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, david, linux-media, devicetree, linux-kernel,
	phone-devel, Vitalii Skorkin, Antonio Rische


On 12/1/25 5:42 PM, Dave Stevenson wrote:
> Do you not want to restore the current position after resuming power?
> Potentially use  __v4l2_ctrl_handler_setup() to reset all controls
> should any extra ones get added.

I want to.

Should lc898217xc_runtime_resume fail if lc898217xc_set_dac fail?

IMO, It should ignore this error, but other drivers returning errcode


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-12-03  9:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-01 12:40 [PATCH v3 0/3] media: i2c: lc898217xc: initial driver Vasiliy Doylov
2025-12-01 12:40 ` [PATCH v3 1/3] media: dt-bindings: Add LC898217XC documentation Vasiliy Doylov
2025-12-01 12:40 ` [PATCH v3 2/3] media: i2c: Add driver for LC898217XC VCM Vasiliy Doylov
2025-12-01 14:42   ` Dave Stevenson
2025-12-03  9:21     ` Vasiliy Doylov
2025-12-01 12:40 ` [PATCH v3 3/3] MAINTAINERS: Add entry for Onsemi LC898217XC lens voice coil driver Vasiliy Doylov

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®