From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9A6737D12D for ; Sun, 19 Apr 2026 09:34:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776591284; cv=none; b=AV3pp32cc43QNY5Ik+FYIRuC/uxFqZDAe6urOVrpKJf4dtZc89orFdc+Y4fkDvRld3TgXyzdq7DymNKRAIPl3R8dI5dLchBK5l/1QaFiaOKBXQhofgZDMv+CnrMWRH/ivVqUGr5czL6er807fTbQn2sRvRvY+Hbwz5OLdpOSAe4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776591284; c=relaxed/simple; bh=ZK2PZapdmsACBN8s8mYncIJi9NrYgSvG33Qhg9WudsU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RXTV8eyxNpoPzu4tyeK8EN+x2JCBZrvNYw/XdT+6otnE2mU09bF/R31IfyOumx2+su9f0JQ0DKsefqrEYCMRBMxEDV7UhiIzg7Cfdk91zJ+hhbyThuh6IkNBp6g1L/h/u0WNelywGWokItbfwNn6jy7ftjttwOIs/9r3OJ//XLk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=D5192/ac; arc=none smtp.client-ip=209.85.167.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D5192/ac" Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-59e4989dacdso2209778e87.1 for ; Sun, 19 Apr 2026 02:34:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776591281; x=1777196081; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=duqXMxrwVP9xmPVimr0jG3GpsUd0+k6xNRj5bMFCO5o=; b=D5192/acvcXgXI5a5s4MTKLlL+BGC4bK9KiHO9UOyo6wJRZG5WbpQXuUCJEE2JfPeA ae0i1RwQCEtI3m8sKR5GdLBlGaOoYp/U7j9vjF5MgBsiiFk9FXIJwl5WLpH4vvMgfa6d oips6zI18dmNpfDKATr/QVbsRwSREmJ8UoBsNtjeNW3KrHaoBKgg6Fii1FhYAUMhda5a 9gmXWxcUaL9F2DPdB77cc9UTOmEgiNxoJNdKZDZRInbjfoPm7cnbrDLgFRvNzwCLX6Tg N83k+em9U7lztjX4TP+Pfg/DQqO6zGKjrG0JyG5lp0hy73iJEfU8Dbb9VsHdz8oMpCGf /y/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776591281; x=1777196081; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=duqXMxrwVP9xmPVimr0jG3GpsUd0+k6xNRj5bMFCO5o=; b=T9CPEroveOjU9jdDyh89EvABi9XzA0XEwHx9JSMtYuxELjbGARkbioU4sIbKHYmXed K2u2tUGmRGT9xinXwn18FAQetPZmLhgj/A8mSIqGeiIZmUevC9AJjrnNJrtZvhf1xF3K nnX85lAvLjL0LWSYYs8tvS8cM46IWTx5PQ6oWIYYzvrpI80YQMW41EZyHBLV7e479nGL rOQcdv79br880NGxTx8j0LZKS8nHANQvAbACPS00sPp6NZBR+fDerHJsoriXT+TNQZ/G WUYRNtNtvrIgPFYbttYUVnywXs6T57dVoThv2ew/trtSbfJJICWuy3bt3gzloatWFmQk AnQA== X-Gm-Message-State: AOJu0YwQOZZhng52uGs+h0GXtwIdRHM/bExOgPn3xDkDElizTEIF4FX+ OURV/9vW9oqmHpXjM73tYCXEklZ2lh79sLNWQJvHZcqlICSkq1GoWolQAlgCrwYv X-Gm-Gg: AeBDievxekA35WHA8EyegF2KqJvXSLk9VKgvkKfn7+vygYzeEOOruup/Cbg0FeDK6lJ IHmVstZYt/VLQA/ivTQaUmAAOeRRD27etvjoSTlAGE/mE7M7lw05QPuXjDre5u8Wa9xJFKp/oFR hZtJ8pUtW/zPxZrIqGODTBl6AHEvvzo46VD7a0nxvznKAiRnL8yQy3pimtKa+/YgrYgOFCx/hK0 6cvfU0JWdxwISFiqDChQRIZHHY3ZAcOcC1DDFV5jH8744Uf9Cqx+psXYve1+Ht0Dp+tEpDEQIT8 G3BZQQ8awr32ca1DD65wj8bVP8AVBSFG8iunesW7HQTCOqtaGo0sURYV+PFR/pXVe4RFzpAsma0 otlVvEsvzHrOyeAup1tceFbn+ZAsgVQC4ShzPr9aRBwROCZi+s/wstjTsYlVczPVJ99PxeQ0hqk 37JI5/RDI6qVjQtGqkalbI9aw= X-Received: by 2002:a05:6512:159f:b0:5a3:f0f7:7da1 with SMTP id 2adb3069b0e04-5a4172cd17amr3379372e87.17.1776591280877; Sun, 19 Apr 2026 02:34:40 -0700 (PDT) Received: from xeon ([188.163.112.56]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-38ecb7613a9sm18221671fa.41.2026.04.19.02.34.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2026 02:34:40 -0700 (PDT) From: Svyatoslav Ryhel To: Lee Jones , Pavel Machek , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Sakari Ailus , Mauro Carvalho Chehab , Svyatoslav Ryhel Cc: linux-leds@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org Subject: [PATCH v2 5/5] media: i2c: lm3560: Add support for PM features Date: Sun, 19 Apr 2026 12:34:12 +0300 Message-ID: <20260419093412.40796-6-clamor95@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260419093412.40796-1-clamor95@gmail.com> References: <20260419093412.40796-1-clamor95@gmail.com> Precedence: bulk X-Mailing-List: linux-leds@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add support for power management features to better control the LM3560 within the media framework. To achieve the desired PM support, the HWEN GPIO and VIN power supply were added and configured into power on/off sequences. Media device deregistration helpers were grouped into a separate function to simplify the probe/remove process. Added PM operations along with the PM configuration setup. Signed-off-by: Svyatoslav Ryhel --- drivers/media/i2c/lm3560.c | 135 +++++++++++++++++++++++++++++++++---- 1 file changed, 123 insertions(+), 12 deletions(-) diff --git a/drivers/media/i2c/lm3560.c b/drivers/media/i2c/lm3560.c index 022a6a76befb..8f5156a80a99 100644 --- a/drivers/media/i2c/lm3560.c +++ b/drivers/media/i2c/lm3560.c @@ -11,12 +11,15 @@ #include #include +#include #include #include #include #include +#include #include #include +#include #include #include #include @@ -47,6 +50,8 @@ enum led_enable { * @dev: pointer to &struct device * @regmap: reg. map for i2c * @lock: muxtex for serial access. + * @hwen_gpio: line connected to HWEN pin + * @vin_supply: line connected to IN supply (2.5V - 5.5V) * @led_mode: V4L2 LED mode * @ctrls_led: V4L2 controls * @subdev_led: V4L2 subdev @@ -60,6 +65,9 @@ struct lm3560_flash { struct regmap *regmap; struct mutex lock; + struct gpio_desc *hwen_gpio; + struct regulator *vin_supply; + enum v4l2_flash_led_mode led_mode; struct v4l2_ctrl_handler ctrls_led[LM3560_LED_MAX]; struct v4l2_subdev subdev_led[LM3560_LED_MAX]; @@ -172,12 +180,17 @@ static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no) struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no); int rval = -EINVAL; + if (!pm_runtime_get_if_in_use(flash->dev)) + return 0; + if (ctrl->id == V4L2_CID_FLASH_FAULT) { s32 fault = 0; unsigned int reg_val; rval = regmap_read(flash->regmap, REG_FLAG, ®_val); - if (rval < 0) + if (rval < 0) { + pm_runtime_put(flash->dev); return rval; + } if (reg_val & FAULT_SHORT_CIRCUIT) fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT; if (reg_val & FAULT_OVERTEMP) @@ -187,6 +200,8 @@ static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no) ctrl->cur.val = fault; } + pm_runtime_put(flash->dev); + return rval; } @@ -196,6 +211,9 @@ static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no) u8 tout_bits; int rval = -EINVAL; + if (!pm_runtime_get_if_in_use(flash->dev)) + return 0; + switch (ctrl->id) { case V4L2_CID_FLASH_LED_MODE: flash->led_mode = ctrl->val; @@ -241,6 +259,8 @@ static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no) break; } + pm_runtime_put(flash->dev); + return rval; } @@ -403,6 +423,60 @@ static int lm3560_init_device(struct lm3560_flash *flash) return rval; } +static void lm3560_power_off(struct lm3560_flash *flash) +{ + gpiod_set_value_cansleep(flash->hwen_gpio, 0); + regulator_disable(flash->vin_supply); +} + +static int lm3560_power_on(struct lm3560_flash *flash) +{ + int rval; + + rval = regulator_enable(flash->vin_supply); + if (rval < 0) { + dev_err(flash->dev, "failed to enable vin power supply\n"); + return rval; + } + + gpiod_set_value_cansleep(flash->hwen_gpio, 1); + + rval = lm3560_init_device(flash); + if (rval < 0) { + lm3560_power_off(flash); + return rval; + } + + return 0; +} + +static int __maybe_unused lm3560_pm_runtime_resume(struct device *dev) +{ + struct lm3560_flash *flash = dev_get_drvdata(dev); + + return lm3560_power_on(flash); +} + +static int __maybe_unused lm3560_pm_runtime_suspend(struct device *dev) +{ + struct lm3560_flash *flash = dev_get_drvdata(dev); + + lm3560_power_off(flash); + + return 0; +} + +static void lm3560_subdev_cleanup(struct lm3560_flash *flash) +{ + unsigned int i; + + for (i = LM3560_LED0; i < LM3560_LED_MAX; i++) { + v4l2_device_unregister_subdev(&flash->subdev_led[i]); + v4l2_ctrl_handler_free(&flash->ctrls_led[i]); + media_entity_cleanup(&flash->subdev_led[i].entity); + } +} + static int lm3560_probe(struct i2c_client *client) { struct lm3560_flash *flash; @@ -423,6 +497,17 @@ static int lm3560_probe(struct i2c_client *client) flash->dev = &client->dev; mutex_init(&flash->lock); + flash->hwen_gpio = devm_gpiod_get_optional(&client->dev, "enable", + GPIOD_OUT_LOW); + if (IS_ERR(flash->hwen_gpio)) + return dev_err_probe(&client->dev, PTR_ERR(flash->hwen_gpio), + "failed to get hwen gpio\n"); + + flash->vin_supply = devm_regulator_get(&client->dev, "vin"); + if (IS_ERR(flash->vin_supply)) + return dev_err_probe(&client->dev, PTR_ERR(flash->vin_supply), + "failed to get vin-supply\n"); + flash->peak = LM3560_PEAK_1600mA; rval = device_property_read_u32(flash->dev, "ti,peak-current-microamp", &peak_ua); @@ -450,10 +535,13 @@ static int lm3560_probe(struct i2c_client *client) &flash->max_flash_timeout); flash->max_flash_timeout /= 1000; - rval = lm3560_init_device(flash); + rval = lm3560_power_on(flash); if (rval < 0) return rval; + pm_runtime_set_active(flash->dev); + pm_runtime_enable(flash->dev); + device_for_each_child_node(flash->dev, node) { fwnode_property_read_u32(node, "reg", ®); @@ -467,30 +555,53 @@ static int lm3560_probe(struct i2c_client *client) &flash->max_torch_brt[reg]); rval = lm3560_subdev_init(flash, reg, node); - if (rval < 0) - return dev_err_probe(flash->dev, rval, - "failed to register led%d\n", - reg); + if (rval < 0) { + dev_err(flash->dev, + "failed to register led%d\n", reg); + goto error_clean; + } } } i2c_set_clientdata(client, flash); + pm_runtime_set_autosuspend_delay(flash->dev, 1000); + pm_runtime_use_autosuspend(flash->dev); + pm_runtime_idle(flash->dev); + return 0; + +error_clean: + pm_runtime_disable(flash->dev); + pm_runtime_set_suspended(flash->dev); + lm3560_subdev_cleanup(flash); + lm3560_power_off(flash); + + return rval; } static void lm3560_remove(struct i2c_client *client) { struct lm3560_flash *flash = i2c_get_clientdata(client); - unsigned int i; - for (i = LM3560_LED0; i < LM3560_LED_MAX; i++) { - v4l2_device_unregister_subdev(&flash->subdev_led[i]); - v4l2_ctrl_handler_free(&flash->ctrls_led[i]); - media_entity_cleanup(&flash->subdev_led[i].entity); + lm3560_subdev_cleanup(flash); + + /* + * Disable runtime PM. In case runtime PM is disabled in the kernel, + * make sure to turn power off manually. + */ + pm_runtime_disable(&client->dev); + if (!pm_runtime_status_suspended(&client->dev)) { + lm3560_power_off(flash); + pm_runtime_set_suspended(&client->dev); } } +static const struct dev_pm_ops lm3560_pm_ops = { + SET_RUNTIME_PM_OPS(lm3560_pm_runtime_suspend, + lm3560_pm_runtime_resume, NULL) +}; + static const struct of_device_id lm3560_of_match[] = { { .compatible = "ti,lm3559" }, { .compatible = "ti,lm3560" }, @@ -509,7 +620,7 @@ MODULE_DEVICE_TABLE(i2c, lm3560_id_table); static struct i2c_driver lm3560_i2c_driver = { .driver = { .name = LM3560_NAME, - .pm = NULL, + .pm = &lm3560_pm_ops, .of_match_table = lm3560_of_match, }, .probe = lm3560_probe, -- 2.51.0