From patchwork Sun Sep 1 21:18:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 13786609 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DBA556B7C for ; Sun, 1 Sep 2024 21:18:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725225528; cv=none; b=sPq65woLLooyG05AK/cwBpZw113q0LG9IknGR/VSNf6xyCa2G2iDf8L62i3GXPg2QMOka4vb+QbtvVePiVyD/QGIVb3I/50j0Y1MI8D+a9fNvZRROIAU6qPjK8oDpGqoSt7YktyAGy3YaPZm82Tr+Yl8xIdrUYAsxT4fuS/0aT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725225528; c=relaxed/simple; bh=ILjG5hChGpco5dpJh0wPTWa39drvRORG1RTgz/ISpEw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B4HbcM9aC7f9R2k/+omL/BIIw/9UTNRL9te1BrvChI7+htnRuMUMV40uXVRXUDDCDLjdw8t8qFNmfcNiJ03osTUNHcUGn6UZzuC43Er2Jqsoc/t7jrij7QHE2yElSYs8w/P7dm2FdagNKczaeLfFlcn4+fCPvsS8Xedp1tP8MYc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=E/5ssvi1; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="E/5ssvi1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725225525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CUIyeIlUznQulBuCKWnaUtTqVDRvXFl2dD4Cxl3d2t8=; b=E/5ssvi19asvWPSIkynBG80dpwOj1uizqduLZ6PFhXUWf/rhft3ugWML8lR0h+uQefxZ6n abb6AnMfhhzy+Y1us1V7KCW3YAS0shhM+uGDVAaqFTx8A5b0GHrVb4CRNyJXml8lhkVi6r ZqeOLXNJMRva7V19/cTr9ejOwc5JuqI= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-421-IK7AKuZGOUisObvn7G_MWg-1; Sun, 01 Sep 2024 17:18:40 -0400 X-MC-Unique: IK7AKuZGOUisObvn7G_MWg-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2B19C1955D50; Sun, 1 Sep 2024 21:18:39 +0000 (UTC) Received: from shalem.redhat.com (unknown [10.39.192.24]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 37F541955F1B; Sun, 1 Sep 2024 21:18:36 +0000 (UTC) From: Hans de Goede To: Sakari Ailus , Laurent Pinchart , Mauro Carvalho Chehab Cc: Hans de Goede , linux-media@vger.kernel.org Subject: [PATCH 1/2] media: v4l: Call s_stream() on VCM when it is called on the associated sensor Date: Sun, 1 Sep 2024 23:18:33 +0200 Message-ID: <20240901211834.145186-2-hdegoede@redhat.com> In-Reply-To: <20240901211834.145186-1-hdegoede@redhat.com> References: <20240901211834.145186-1-hdegoede@redhat.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Currently VCM drivers power-up the VCM as soon as the VCM's /dev node is opened and through the runtime-pm device-link to the sensor this also powers up the sensor. Powering up the VCM and sensor when the /dev node is opened is undesirable, without a VCM sensors delay powering up until s_stream(1) is called. This allows querying / negotiating capabilities without powering things up. Sometimes a long running daemon like pipewire may keep the /dev node open all the time. The kernel should still be able to powerdown the VCM + sensor in this scenario. VCM drivers should be able to do the same as sensor drivers and only power-up the VCM when s_stream(1) is called on the VCM subdev, but this requires that s_stream() is actually called on the VCM when the sensor starts / stops streaming. The s_stream() call on sensor subdevs is done by CSI-receiver/ISP drivers. To avoid needing to change all these call sites to also call s_stream() on the VCM (if there is one) handle the VCM in the v4l2-core similar to how the core takes care of turning on/off the privacy LED. Signed-off-by: Hans de Goede --- drivers/media/v4l2-core/v4l2-async.c | 20 +++++++++++++++++++ drivers/media/v4l2-core/v4l2-subdev.c | 28 +++++++++++++++++++++------ include/media/v4l2-subdev.h | 2 ++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/drivers/media/v4l2-core/v4l2-async.c b/drivers/media/v4l2-core/v4l2-async.c index ee884a8221fb..9b854f1d1051 100644 --- a/drivers/media/v4l2-core/v4l2-async.c +++ b/drivers/media/v4l2-core/v4l2-async.c @@ -330,6 +330,11 @@ static int v4l2_async_create_ancillary_links(struct v4l2_async_notifier *n, return 0; } + if (sd->entity.function == MEDIA_ENT_F_LENS) { + dev_dbg(n->sd->dev, "Using %s VCM\n", dev_name(sd->dev)); + n->sd->vcm = sd; + } + link = media_create_ancillary_link(&n->sd->entity, &sd->entity); return IS_ERR(link) ? PTR_ERR(link) : 0; @@ -871,6 +876,21 @@ void v4l2_async_unregister_subdev(struct v4l2_subdev *sd) if (!sd->async_list.next) return; +#if defined(CONFIG_MEDIA_CONTROLLER) + if (sd->entity.function == MEDIA_ENT_F_LENS && sd->v4l2_dev && sd->v4l2_dev->mdev) { + struct media_entity *entity; + + media_device_for_each_entity(entity, sd->v4l2_dev->mdev) { + struct v4l2_subdev *it = media_entity_to_v4l2_subdev(entity); + + if (it->vcm == sd) { + dev_dbg(it->dev, "Clearing VCM\n"); + it->vcm = NULL; + } + } + } +#endif + v4l2_subdev_put_privacy_led(sd); mutex_lock(&list_lock); diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c index 7c5812d55315..24a68d90f686 100644 --- a/drivers/media/v4l2-core/v4l2-subdev.c +++ b/drivers/media/v4l2-core/v4l2-subdev.c @@ -148,17 +148,33 @@ static int subdev_close(struct file *file) } #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ -static void v4l2_subdev_enable_privacy_led(struct v4l2_subdev *sd) +static void v4l2_subdev_enable_privacy_led_and_vcm(struct v4l2_subdev *sd) { #if IS_REACHABLE(CONFIG_LEDS_CLASS) if (!IS_ERR_OR_NULL(sd->privacy_led)) led_set_brightness(sd->privacy_led, sd->privacy_led->max_brightness); #endif + + if (sd->vcm && !sd->vcm_enabled && + v4l2_subdev_has_op(sd->vcm, video, s_stream)) { + int ret; + + ret = v4l2_subdev_call(sd->vcm, video, s_stream, 1); + if (ret) + dev_err(sd->vcm->dev, "Error powering on VCM: %d\n", ret); + else + sd->vcm_enabled = true; + } } -static void v4l2_subdev_disable_privacy_led(struct v4l2_subdev *sd) +static void v4l2_subdev_disable_privacy_led_and_vcm(struct v4l2_subdev *sd) { + if (sd->vcm && sd->vcm_enabled) { + v4l2_subdev_call(sd->vcm, video, s_stream, 0); + sd->vcm_enabled = false; + } + #if IS_REACHABLE(CONFIG_LEDS_CLASS) if (!IS_ERR_OR_NULL(sd->privacy_led)) led_set_brightness(sd->privacy_led, 0); @@ -466,9 +482,9 @@ static int call_s_stream(struct v4l2_subdev *sd, int enable) sd->s_stream_enabled = enable; if (enable) - v4l2_subdev_enable_privacy_led(sd); + v4l2_subdev_enable_privacy_led_and_vcm(sd); else - v4l2_subdev_disable_privacy_led(sd); + v4l2_subdev_disable_privacy_led_and_vcm(sd); } return ret; @@ -2289,7 +2305,7 @@ int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad, * for all cases. */ if (!use_s_stream && !already_streaming) - v4l2_subdev_enable_privacy_led(sd); + v4l2_subdev_enable_privacy_led_and_vcm(sd); done: if (!use_s_stream) @@ -2382,7 +2398,7 @@ int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad, done: if (!use_s_stream) { if (!v4l2_subdev_is_streaming(sd)) - v4l2_subdev_disable_privacy_led(sd); + v4l2_subdev_disable_privacy_led_and_vcm(sd); v4l2_subdev_unlock_state(state); } diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h index bd235d325ff9..6568a0cc070b 100644 --- a/include/media/v4l2-subdev.h +++ b/include/media/v4l2-subdev.h @@ -1094,6 +1094,7 @@ struct v4l2_subdev { */ struct led_classdev *privacy_led; + struct v4l2_subdev *vcm; /* * TODO: active_state should most likely be changed from a pointer to an @@ -1104,6 +1105,7 @@ struct v4l2_subdev { struct v4l2_subdev_state *active_state; u64 enabled_pads; bool s_stream_enabled; + bool vcm_enabled; }; From patchwork Sun Sep 1 21:18:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 13786610 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2A15139D15 for ; Sun, 1 Sep 2024 21:18:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725225528; cv=none; b=NxxSX0kQsJkjRzDTOAxh/Cu6U+Vej6NkK88GhrixHrNRp4IzW4BTR4s1Xyq2aFYm0puabGD0lLgUDbth9NUUZMCIDijvGJTRXWoX0Q9BDk+rOkPoA2bDSsr6xdLwZxxF6ChqSF7923JQwa0oj7GLL6Jsw7DzlQLzCVU8Zc2nals= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725225528; c=relaxed/simple; bh=ZCDMIU198JcaKJcgwModOxVbMTK1SIgcIoQkLt+kxnA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Mo8y1j+1t0SaBoWOmuwcBmiFmz8uFNyN+ALXAc1+JEW4FQRyAR3mGLZ+hi7yfdDmMBh1l/FC9DSRKZiL459rJcpPaH0+Rm3X1YAFxLIEGo2eWKVQtr/syDJeB7WX5pppoIZOqt2VaNXszaDS43oFrqhi65+gQ3yaxDcuhwC1o4U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dqhjXD8E; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dqhjXD8E" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725225525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R0D6ZmlSwPMTLLZk9gGNibE27rGQJtznYisRAcN2bBQ=; b=dqhjXD8EOqKYIbEp7szV/qg39HtPTkJLwwqcPQNQn0g5L7bbDaIGiLZ0yXVtjzJdZ7oifp FsSRAHM8hqj4wKvhSJbfSppHJvoHHGB6QJSjqVt7HzGaOHfvUFJY6aYOc0mKg9kphiif5h w0WGB9AF1tUClYRvwCxOSuqojLAucCY= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-47-bN_bH-ctMqyILb_fugTqeQ-1; Sun, 01 Sep 2024 17:18:42 -0400 X-MC-Unique: bN_bH-ctMqyILb_fugTqeQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 22BEA1955D48; Sun, 1 Sep 2024 21:18:41 +0000 (UTC) Received: from shalem.redhat.com (unknown [10.39.192.24]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 448E51955F1B; Sun, 1 Sep 2024 21:18:39 +0000 (UTC) From: Hans de Goede To: Sakari Ailus , Laurent Pinchart , Mauro Carvalho Chehab Cc: Hans de Goede , linux-media@vger.kernel.org Subject: [PATCH 2/2] media: i2c: Add driver for AD5823 VCM Date: Sun, 1 Sep 2024 23:18:34 +0200 Message-ID: <20240901211834.145186-3-hdegoede@redhat.com> In-Reply-To: <20240901211834.145186-1-hdegoede@redhat.com> References: <20240901211834.145186-1-hdegoede@redhat.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 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 --- 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 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 + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#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 "); +MODULE_DESCRIPTION("AD5823 VCM Driver"); +MODULE_LICENSE("GPL");