diff mbox series

[2/2] media: i2c: Add driver for AD5823 VCM

Message ID 20240901211834.145186-3-hdegoede@redhat.com (mailing list archive)
State New
Headers show
Series media: v4l: Call s_stream() on VCM when it is called on the associated sensor | expand

Commit Message

Hans de Goede Sept. 1, 2024, 9:18 p.m. UTC
Add a driver for the AD5823 VCM. The driver creates a v4l2 subdevice
and registers a control to set the desired focus.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/Kconfig  |   5 +
 drivers/media/i2c/Makefile |   1 +
 drivers/media/i2c/ad5823.c | 311 +++++++++++++++++++++++++++++++++++++
 3 files changed, 317 insertions(+)
 create mode 100644 drivers/media/i2c/ad5823.c

Comments

kernel test robot Sept. 3, 2024, 11:59 p.m. UTC | #1
Hi Hans,

kernel test robot noticed the following build errors:

[auto build test ERROR on media-tree/master]
[also build test ERROR on linuxtv-media-stage/master sailus-media-tree/master linus/master v6.11-rc6 next-20240903]
[cannot apply to sailus-media-tree/streams]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Hans-de-Goede/media-v4l-Call-s_stream-on-VCM-when-it-is-called-on-the-associated-sensor/20240902-052000
base:   git://linuxtv.org/media_tree.git master
patch link:    https://lore.kernel.org/r/20240901211834.145186-3-hdegoede%40redhat.com
patch subject: [PATCH 2/2] media: i2c: Add driver for AD5823 VCM
config: x86_64-randconfig-121-20240904 (https://download.01.org/0day-ci/archive/20240904/202409040751.A1Dm1pgG-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240904/202409040751.A1Dm1pgG-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409040751.A1Dm1pgG-lkp@intel.com/

All error/warnings (new ones prefixed by >>):

   drivers/media/i2c/ad5823.c: In function 'ad5823_probe':
>> drivers/media/i2c/ad5823.c:203:26: error: implicit declaration of function 'devm_cci_regmap_init_i2c' [-Werror=implicit-function-declaration]
     203 |         ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
         |                          ^~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/media/i2c/ad5823.c:203:24: warning: assignment to 'struct regmap *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
     203 |         ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
         |                        ^
   cc1: some warnings being treated as errors


vim +/devm_cci_regmap_init_i2c +203 drivers/media/i2c/ad5823.c

   193	
   194	static int ad5823_probe(struct i2c_client *client)
   195	{
   196		struct ad5823_device *ad5823;
   197		int ret;
   198	
   199		ad5823 = devm_kzalloc(&client->dev, sizeof(*ad5823), GFP_KERNEL);
   200		if (!ad5823)
   201			return -ENOMEM;
   202	
 > 203		ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
   204		if (IS_ERR(ad5823->regmap))
   205			return PTR_ERR(ad5823->regmap);
   206	
   207		ad5823->arc_mode = AD5823_ARC_RES1;
   208		ad5823->resonance_period = AD5823_RESONANCE_PERIOD;
   209	
   210		/* Optional indication of ARC mode select */
   211		device_property_read_u32(&client->dev, "adi,arc-mode",
   212					 &ad5823->arc_mode);
   213	
   214		/* Optional indication of VCM resonance period */
   215		device_property_read_u32(&client->dev, "adi,resonance-period",
   216					 &ad5823->resonance_period);
   217	
   218		ad5823->regulator = devm_regulator_get(&client->dev, "vdd");
   219		if (IS_ERR(ad5823->regulator))
   220			return dev_err_probe(&client->dev, PTR_ERR(ad5823->regulator),
   221					     "getting regulator\n");
   222	
   223		v4l2_i2c_subdev_init(&ad5823->sd, client, &ad5823_ops);
   224		ad5823->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
   225	
   226		ret = ad5823_init_controls(ad5823);
   227		if (ret)
   228			return ret;
   229	
   230		ret = media_entity_pads_init(&ad5823->sd.entity, 0, NULL);
   231		if (ret < 0)
   232			goto err_free_ctrl_handler;
   233	
   234		ad5823->sd.entity.function = MEDIA_ENT_F_LENS;
   235	
   236		/*
   237		 * We need the driver to work in the event that pm runtime is disable in
   238		 * the kernel, so power up and verify the chip now. In the event that
   239		 * runtime pm is disabled this will leave the chip on, so that the lens
   240		 * will work.
   241		 */
   242	
   243		ret = ad5823_power_up(ad5823, true);
   244		if (ret)
   245			goto err_cleanup_media;
   246	
   247		pm_runtime_set_active(&client->dev);
   248		pm_runtime_get_noresume(&client->dev);
   249		pm_runtime_enable(&client->dev);
   250	
   251		ret = v4l2_async_register_subdev(&ad5823->sd);
   252		if (ret < 0)
   253			goto err_pm_runtime;
   254	
   255		pm_runtime_set_autosuspend_delay(&client->dev, 1000);
   256		pm_runtime_use_autosuspend(&client->dev);
   257		pm_runtime_put_autosuspend(&client->dev);
   258	
   259		return ret;
   260	
   261	err_pm_runtime:
   262		pm_runtime_disable(&client->dev);
   263		pm_runtime_put_noidle(&client->dev);
   264		ad5823_power_down(ad5823);
   265	err_cleanup_media:
   266		media_entity_cleanup(&ad5823->sd.entity);
   267	err_free_ctrl_handler:
   268		v4l2_ctrl_handler_free(&ad5823->ctrls.handler);
   269	
   270		return ret;
   271	}
   272
kernel test robot Sept. 4, 2024, 3:25 a.m. UTC | #2
Hi Hans,

kernel test robot noticed the following build errors:

[auto build test ERROR on media-tree/master]
[also build test ERROR on linuxtv-media-stage/master sailus-media-tree/master linus/master v6.11-rc6 next-20240903]
[cannot apply to sailus-media-tree/streams]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Hans-de-Goede/media-v4l-Call-s_stream-on-VCM-when-it-is-called-on-the-associated-sensor/20240902-052000
base:   git://linuxtv.org/media_tree.git master
patch link:    https://lore.kernel.org/r/20240901211834.145186-3-hdegoede%40redhat.com
patch subject: [PATCH 2/2] media: i2c: Add driver for AD5823 VCM
config: x86_64-randconfig-076-20240904 (https://download.01.org/0day-ci/archive/20240904/202409041123.VAFdMjn2-lkp@intel.com/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240904/202409041123.VAFdMjn2-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409041123.VAFdMjn2-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/media/i2c/ad5823.c:203:19: error: call to undeclared function 'devm_cci_regmap_init_i2c'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     203 |         ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
         |                          ^
>> drivers/media/i2c/ad5823.c:203:17: error: incompatible integer to pointer conversion assigning to 'struct regmap *' from 'int' [-Wint-conversion]
     203 |         ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
         |                        ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   2 errors generated.


vim +/devm_cci_regmap_init_i2c +203 drivers/media/i2c/ad5823.c

   193	
   194	static int ad5823_probe(struct i2c_client *client)
   195	{
   196		struct ad5823_device *ad5823;
   197		int ret;
   198	
   199		ad5823 = devm_kzalloc(&client->dev, sizeof(*ad5823), GFP_KERNEL);
   200		if (!ad5823)
   201			return -ENOMEM;
   202	
 > 203		ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
   204		if (IS_ERR(ad5823->regmap))
   205			return PTR_ERR(ad5823->regmap);
   206	
   207		ad5823->arc_mode = AD5823_ARC_RES1;
   208		ad5823->resonance_period = AD5823_RESONANCE_PERIOD;
   209	
   210		/* Optional indication of ARC mode select */
   211		device_property_read_u32(&client->dev, "adi,arc-mode",
   212					 &ad5823->arc_mode);
   213	
   214		/* Optional indication of VCM resonance period */
   215		device_property_read_u32(&client->dev, "adi,resonance-period",
   216					 &ad5823->resonance_period);
   217	
   218		ad5823->regulator = devm_regulator_get(&client->dev, "vdd");
   219		if (IS_ERR(ad5823->regulator))
   220			return dev_err_probe(&client->dev, PTR_ERR(ad5823->regulator),
   221					     "getting regulator\n");
   222	
   223		v4l2_i2c_subdev_init(&ad5823->sd, client, &ad5823_ops);
   224		ad5823->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
   225	
   226		ret = ad5823_init_controls(ad5823);
   227		if (ret)
   228			return ret;
   229	
   230		ret = media_entity_pads_init(&ad5823->sd.entity, 0, NULL);
   231		if (ret < 0)
   232			goto err_free_ctrl_handler;
   233	
   234		ad5823->sd.entity.function = MEDIA_ENT_F_LENS;
   235	
   236		/*
   237		 * We need the driver to work in the event that pm runtime is disable in
   238		 * the kernel, so power up and verify the chip now. In the event that
   239		 * runtime pm is disabled this will leave the chip on, so that the lens
   240		 * will work.
   241		 */
   242	
   243		ret = ad5823_power_up(ad5823, true);
   244		if (ret)
   245			goto err_cleanup_media;
   246	
   247		pm_runtime_set_active(&client->dev);
   248		pm_runtime_get_noresume(&client->dev);
   249		pm_runtime_enable(&client->dev);
   250	
   251		ret = v4l2_async_register_subdev(&ad5823->sd);
   252		if (ret < 0)
   253			goto err_pm_runtime;
   254	
   255		pm_runtime_set_autosuspend_delay(&client->dev, 1000);
   256		pm_runtime_use_autosuspend(&client->dev);
   257		pm_runtime_put_autosuspend(&client->dev);
   258	
   259		return ret;
   260	
   261	err_pm_runtime:
   262		pm_runtime_disable(&client->dev);
   263		pm_runtime_put_noidle(&client->dev);
   264		ad5823_power_down(ad5823);
   265	err_cleanup_media:
   266		media_entity_cleanup(&ad5823->sd.entity);
   267	err_free_ctrl_handler:
   268		v4l2_ctrl_handler_free(&ad5823->ctrls.handler);
   269	
   270		return ret;
   271	}
   272
Hans de Goede Sept. 4, 2024, 7:12 p.m. UTC | #3
Hi,

On 9/4/24 1:59 AM, kernel test robot wrote:
> Hi Hans,
> 
> kernel test robot noticed the following build errors:
> 
> [auto build test ERROR on media-tree/master]
> [also build test ERROR on linuxtv-media-stage/master sailus-media-tree/master linus/master v6.11-rc6 next-20240903]
> [cannot apply to sailus-media-tree/streams]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
> 
> url:    https://github.com/intel-lab-lkp/linux/commits/Hans-de-Goede/media-v4l-Call-s_stream-on-VCM-when-it-is-called-on-the-associated-sensor/20240902-052000
> base:   git://linuxtv.org/media_tree.git master
> patch link:    https://lore.kernel.org/r/20240901211834.145186-3-hdegoede%40redhat.com
> patch subject: [PATCH 2/2] media: i2c: Add driver for AD5823 VCM
> config: x86_64-randconfig-121-20240904 (https://download.01.org/0day-ci/archive/20240904/202409040751.A1Dm1pgG-lkp@intel.com/config)
> compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240904/202409040751.A1Dm1pgG-lkp@intel.com/reproduce)
> 
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202409040751.A1Dm1pgG-lkp@intel.com/
> 
> All error/warnings (new ones prefixed by >>):
> 
>    drivers/media/i2c/ad5823.c: In function 'ad5823_probe':
>>> drivers/media/i2c/ad5823.c:203:26: error: implicit declaration of function 'devm_cci_regmap_init_i2c' [-Werror=implicit-function-declaration]
>      203 |         ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
>          |                          ^~~~~~~~~~~~~~~~~~~~~~~~
>>> drivers/media/i2c/ad5823.c:203:24: warning: assignment to 'struct regmap *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
>      203 |         ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
>          |                        ^
>    cc1: some warnings being treated as errors


This is caused by a missing "select V4L2_CCI_I2C" I have fixed this
in my local tree and this fix will be included in v2 when I get
around to posting v2 (which also depends on the discussion
surrounding patch 1/2).

Regards,

Hans
diff mbox series

Patch

diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 70254f0cca67..4c0b198b7c31 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -742,6 +742,11 @@  config VIDEO_AD5820
 	  This is a driver for the AD5820 camera lens voice coil.
 	  It is used for example in Nokia N900 (RX-51).
 
+config VIDEO_AD5823
+	tristate "AD5823 lens voice coil support"
+	help
+	  This is a driver for the AD5823 camera lens voice coil / VCM.
+
 config VIDEO_AK7375
 	tristate "AK7375 lens voice coil support"
 	depends on I2C && VIDEO_DEV
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index babc1bc2393e..34d3e7a2abbb 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -4,6 +4,7 @@  msp3400-objs	:=	msp3400-driver.o msp3400-kthreads.o
 
 obj-$(CONFIG_SDR_MAX2175) += max2175.o
 obj-$(CONFIG_VIDEO_AD5820) += ad5820.o
+obj-$(CONFIG_VIDEO_AD5823) += ad5823.o
 obj-$(CONFIG_VIDEO_ADP1653) += adp1653.o
 obj-$(CONFIG_VIDEO_ADV7170) += adv7170.o
 obj-$(CONFIG_VIDEO_ADV7175) += adv7175.o
diff --git a/drivers/media/i2c/ad5823.c b/drivers/media/i2c/ad5823.c
new file mode 100644
index 000000000000..83de4ab38399
--- /dev/null
+++ b/drivers/media/i2c/ad5823.c
@@ -0,0 +1,311 @@ 
+/*
+ * Analog Devices AD5823 VCM driver
+ * Copyright 2023 Hans de Goede <hansg@kernel.org>
+ */
+
+#include <linux/i2c.h>
+#include <linux/pm_runtime.h>
+#include <linux/regulator/consumer.h>
+#include <linux/types.h>
+
+#include <media/v4l2-cci.h>
+#include <media/v4l2-common.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-subdev.h>
+
+#define AD5823_MAX_FOCUS_POS		1023
+
+#define AD5823_RESET			CCI_REG8(1)
+#define AD5823_RESET_RESET		BIT(0)
+
+#define AD5823_MODE			CCI_REG8(2)
+#define AD5823_ARC_RES1			0x01
+
+#define AD5823_VCM_MOVE_TIME		CCI_REG8(3)
+#define AD5823_VCM_MOVE_TIME_DEFAULT	0x80
+#define AD5823_RESONANCE_PERIOD		100000	/* in 0.1 us units */
+#define AD5823_RESONANCE_COEF		512	/* in 0.1 us units */
+
+#define AD5823_RESONANCE_OFFSET		0x80	/* for reg 0x02 bit 5 == 0 */
+
+#define AD5823_VCM_CODE			CCI_REG16(4)
+#define AD5823_VCM_CODE_RING_CTRL	BIT(10)
+
+#define AD5823_VCM_THRESHOLD		CCI_REG16(6)
+#define AD5823_VCM_THRESHOLD_DEFAULT	0x10
+
+#define to_ad5823_device(x) container_of(x, struct ad5823_device, sd)
+
+struct ad5823_device {
+	struct v4l2_subdev sd;
+	struct regmap *regmap;
+	struct regulator *regulator;
+	u32 arc_mode;
+	u32 resonance_period;	/* in 0.1 us units */
+
+	struct ad5823_v4l2_ctrls {
+		struct v4l2_ctrl_handler handler;
+		struct v4l2_ctrl *focus;
+	} ctrls;
+};
+
+static int ad5823_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct ad5823_device *ad5823 = container_of(ctrl->handler,
+						    struct ad5823_device,
+						    ctrls.handler);
+	int ret;
+
+	/* Only apply changes to the controls if the device is powered up */
+	if (!pm_runtime_get_if_in_use(ad5823->sd.dev))
+		return 0;
+
+	switch (ctrl->id) {
+	case V4L2_CID_FOCUS_ABSOLUTE:
+		ret = cci_write(ad5823->regmap, AD5823_VCM_CODE,
+				AD5823_VCM_CODE_RING_CTRL | ctrl->val, NULL);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+
+	pm_runtime_put(ad5823->sd.dev);
+	return ret;
+}
+
+static const struct v4l2_ctrl_ops ad5823_ctrl_ops = {
+	.s_ctrl = ad5823_set_ctrl,
+};
+
+static int ad5823_power_down(struct ad5823_device *ad5823)
+{
+	return regulator_disable(ad5823->regulator);
+}
+
+static int ad5823_power_up(struct ad5823_device *ad5823, bool detect)
+{
+	u64 vcm_move_time, vcm_threshold;
+	int ret;
+
+	ret = regulator_enable(ad5823->regulator);
+	if (ret)
+		return ret;
+
+	cci_write(ad5823->regmap, AD5823_RESET, BIT(0), &ret);
+
+	if (detect) {
+		/* There is no id register, check for default reg values. */
+		cci_read(ad5823->regmap, AD5823_VCM_MOVE_TIME, &vcm_move_time, &ret);
+		cci_read(ad5823->regmap, AD5823_VCM_THRESHOLD, &vcm_threshold, &ret);
+
+		if (!ret && (vcm_move_time != AD5823_VCM_MOVE_TIME_DEFAULT ||
+			     vcm_threshold != AD5823_VCM_THRESHOLD_DEFAULT)) {
+			dev_err(ad5823->sd.dev, "Failed to detect AD5823 got move-time 0x%02llx vcm-threshold 0x%02llx\n",
+				vcm_move_time, vcm_threshold);
+			ret = -ENXIO;
+		}
+	}
+
+	vcm_move_time = ad5823->resonance_period / AD5823_RESONANCE_COEF -
+			AD5823_RESONANCE_OFFSET;
+
+	dev_dbg(ad5823->sd.dev, "mode 0x%02x move-time 0x%02llx\n", ad5823->arc_mode, vcm_move_time);
+
+	cci_write(ad5823->regmap, AD5823_MODE, ad5823->arc_mode, &ret);
+	cci_write(ad5823->regmap, AD5823_VCM_MOVE_TIME, vcm_move_time, &ret);
+	if (ret)
+		ad5823_power_down(ad5823);
+
+	return ret;
+}
+
+static int ad5823_suspend(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct ad5823_device *ad5823 = to_ad5823_device(sd);
+
+	return ad5823_power_down(ad5823);
+}
+
+static int ad5823_resume(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct ad5823_device *ad5823 = to_ad5823_device(sd);
+
+	return ad5823_power_up(ad5823, false);
+}
+
+static int ad5823_s_stream(struct v4l2_subdev *sd, int enable)
+{
+	struct ad5823_device *ad5823 = to_ad5823_device(sd);
+	int ret;
+
+	if (enable) {
+		ret = pm_runtime_resume_and_get(sd->dev);
+		if (ret < 0)
+			return ret;
+
+		/* Restore value of ctrls */
+		ret = v4l2_ctrl_handler_setup(&ad5823->ctrls.handler);
+		if (ret < 0)
+			pm_runtime_put(sd->dev);
+
+		return ret;
+	} else {
+		pm_runtime_put(sd->dev);
+		return 0;
+	}
+}
+
+static const struct v4l2_subdev_video_ops ad5823_video_ops = {
+	.s_stream = ad5823_s_stream,
+};
+
+static const struct v4l2_subdev_ops ad5823_ops = {
+	.video	= &ad5823_video_ops,
+};
+
+static int ad5823_init_controls(struct ad5823_device *ad5823)
+{
+	const struct v4l2_ctrl_ops *ops = &ad5823_ctrl_ops;
+	int ret;
+
+	v4l2_ctrl_handler_init(&ad5823->ctrls.handler, 1);
+
+	ad5823->ctrls.focus = v4l2_ctrl_new_std(&ad5823->ctrls.handler, ops,
+						V4L2_CID_FOCUS_ABSOLUTE, 0,
+						AD5823_MAX_FOCUS_POS, 1, 0);
+
+	if (ad5823->ctrls.handler.error) {
+		dev_err(ad5823->sd.dev, "Error initialising v4l2 ctrls\n");
+		ret = ad5823->ctrls.handler.error;
+		goto err_free_handler;
+	}
+
+	ad5823->sd.ctrl_handler = &ad5823->ctrls.handler;
+	return 0;
+
+err_free_handler:
+	v4l2_ctrl_handler_free(&ad5823->ctrls.handler);
+	return ret;
+}
+
+static int ad5823_probe(struct i2c_client *client)
+{
+	struct ad5823_device *ad5823;
+	int ret;
+
+	ad5823 = devm_kzalloc(&client->dev, sizeof(*ad5823), GFP_KERNEL);
+	if (!ad5823)
+		return -ENOMEM;
+
+	ad5823->regmap = devm_cci_regmap_init_i2c(client, 8);
+	if (IS_ERR(ad5823->regmap))
+		return PTR_ERR(ad5823->regmap);
+
+	ad5823->arc_mode = AD5823_ARC_RES1;
+	ad5823->resonance_period = AD5823_RESONANCE_PERIOD;
+
+	/* Optional indication of ARC mode select */
+	device_property_read_u32(&client->dev, "adi,arc-mode",
+				 &ad5823->arc_mode);
+
+	/* Optional indication of VCM resonance period */
+	device_property_read_u32(&client->dev, "adi,resonance-period",
+				 &ad5823->resonance_period);
+
+	ad5823->regulator = devm_regulator_get(&client->dev, "vdd");
+	if (IS_ERR(ad5823->regulator))
+		return dev_err_probe(&client->dev, PTR_ERR(ad5823->regulator),
+				     "getting regulator\n");
+
+	v4l2_i2c_subdev_init(&ad5823->sd, client, &ad5823_ops);
+	ad5823->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+
+	ret = ad5823_init_controls(ad5823);
+	if (ret)
+		return ret;
+
+	ret = media_entity_pads_init(&ad5823->sd.entity, 0, NULL);
+	if (ret < 0)
+		goto err_free_ctrl_handler;
+
+	ad5823->sd.entity.function = MEDIA_ENT_F_LENS;
+
+	/*
+	 * We need the driver to work in the event that pm runtime is disable in
+	 * the kernel, so power up and verify the chip now. In the event that
+	 * runtime pm is disabled this will leave the chip on, so that the lens
+	 * will work.
+	 */
+
+	ret = ad5823_power_up(ad5823, true);
+	if (ret)
+		goto err_cleanup_media;
+
+	pm_runtime_set_active(&client->dev);
+	pm_runtime_get_noresume(&client->dev);
+	pm_runtime_enable(&client->dev);
+
+	ret = v4l2_async_register_subdev(&ad5823->sd);
+	if (ret < 0)
+		goto err_pm_runtime;
+
+	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
+	pm_runtime_use_autosuspend(&client->dev);
+	pm_runtime_put_autosuspend(&client->dev);
+
+	return ret;
+
+err_pm_runtime:
+	pm_runtime_disable(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+	ad5823_power_down(ad5823);
+err_cleanup_media:
+	media_entity_cleanup(&ad5823->sd.entity);
+err_free_ctrl_handler:
+	v4l2_ctrl_handler_free(&ad5823->ctrls.handler);
+
+	return ret;
+}
+
+static void ad5823_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct ad5823_device *ad5823 =
+		container_of(sd, struct ad5823_device, sd);
+
+	v4l2_async_unregister_subdev(sd);
+	v4l2_ctrl_handler_free(&ad5823->ctrls.handler);
+	media_entity_cleanup(&ad5823->sd.entity);
+
+	pm_runtime_disable(&client->dev);
+	if (!pm_runtime_status_suspended(&client->dev))
+		ad5823_power_down(ad5823);
+	pm_runtime_set_suspended(&client->dev);
+}
+
+static const struct i2c_device_id ad5823_id_table[] = {
+	{ "ad5823" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, ad5823_id_table);
+
+static DEFINE_RUNTIME_DEV_PM_OPS(ad5823_pm_ops, ad5823_suspend, ad5823_resume,
+				 NULL);
+
+static struct i2c_driver ad5823_i2c_driver = {
+	.driver = {
+		.name = "ad5823",
+		.pm = pm_sleep_ptr(&ad5823_pm_ops),
+	},
+	.probe = ad5823_probe,
+	.remove = ad5823_remove,
+	.id_table = ad5823_id_table,
+};
+module_i2c_driver(ad5823_i2c_driver);
+
+MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>");
+MODULE_DESCRIPTION("AD5823 VCM Driver");
+MODULE_LICENSE("GPL");