Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3365009imw; Mon, 11 Jul 2022 07:19:52 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v300tWkJSDF75JYOZoHor9x+RtPfC9ExByiqfgHXdXuf8utcx99F/jS/y7LIhwjHmBdEAy X-Received: by 2002:a17:90b:268f:b0:1ef:ba7e:3fe4 with SMTP id pl15-20020a17090b268f00b001efba7e3fe4mr18599804pjb.23.1657549192640; Mon, 11 Jul 2022 07:19:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657549192; cv=none; d=google.com; s=arc-20160816; b=ocomjNkeY7CfWnvPIMYAa96aV4wYH4XbPi9g6Uby9lUFnx3lBtnZVsxMLiXTEgFjIn MiQru6Gxpd9Qno0S0Fkm4Cvy5MTvunp50afn0jkmkfGDV0KomC6c3abDlG2ZWqA8ZzOQ 2OiwibUjpHh2RGuZ0VyE4cYeX6ECcrIKt8GPnktGusMjpIQgiMQRsQhpRykJtyb8rika uXW4xM+c/W9zz462Anf7BCp14zC94pKDjMhhfa1H6lwpuAFY4KOBUPMaBB60VKY+pT9T 0Z+t7kiQrxKDM2gw2wC3OBeTy89MDlG7ComwC88r1z2ymsJS/sJFl6MmCE3jncs2GCQc pQJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :cc:to:subject:from:date:dkim-signature; bh=x2FwgOdiviKc21na7ZeEL1STsNQh/sVL41XUf3gedcc=; b=UWkQxNeRcMZxleHgI/6ReLeSzFryk2CVtPHGJUFEPcBLdl6TAFnGs8/CH1vb0W+M2g F07LWvKSrgdPDQY6lYplbLEvfr7Wr+rQkAKlzJlIKTDGS923tzp7xY5l7oHgqGWtfTqU me9gxmhSiW42OrRZJVnuKgiPmK/EMOZZPaDu/sxYK9vT/khqHvtHcCjmc04ByFhEzHHP q2/ZrlwLcLfM+SYpF03jb2YvT47iP4TlgffJbk18NrQW+PLeRFMoLPAPAMhjTt4YBJ23 LnMvGQvMwJymEXwsteTROTdsQbhgM7XUpSMVjdfP4mTvdPsWpcXRyEdkGfBXPFvsPqS+ qRmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TPlU+M9o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g35-20020a635663000000b00411bf36f550si10670730pgm.595.2022.07.11.07.19.39; Mon, 11 Jul 2022 07:19:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TPlU+M9o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230045AbiGKOJK (ORCPT + 99 others); Mon, 11 Jul 2022 10:09:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229654AbiGKOJI (ORCPT ); Mon, 11 Jul 2022 10:09:08 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 252B261B23; Mon, 11 Jul 2022 07:09:07 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id r14so7201575wrg.1; Mon, 11 Jul 2022 07:09:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:subject:to:cc:message-id:in-reply-to:references :mime-version; bh=x2FwgOdiviKc21na7ZeEL1STsNQh/sVL41XUf3gedcc=; b=TPlU+M9oGx1mze+WdbVSqIDnwy5LAOrTd/lbEjl69f1UAbD0Lw9Z9veZYduWYlkmyM dUfs2b6mm4OIW0Mzlu38PN0B7+P9jZMT9mtaUZoZn5q4ohIQLAvEapxKDLBkAYkH3cSz 6yrnWDpmDWhUeEY65i0qh1b9ZHh/eYEb5GWTc/9ySXeTivvBfXOLIyG8NmCNmU2rvuKF 2PH4oyawrJPOxlWB3osQ7Wqld83OepdKkSDAzq2x7GdjaRqhcCT/ZcNJjswxDJMv0Cbe C/G2LtXF1lk2E6k6Uoluf+CailgftMMSYJJc7bDM55NoRrsx/DWO7wkabKLi8A5I7YvU 4N1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:subject:to:cc:message-id:in-reply-to :references:mime-version; bh=x2FwgOdiviKc21na7ZeEL1STsNQh/sVL41XUf3gedcc=; b=ByBv9ZT01cvTLYduqa0L2gcS8i4YLfpdld15ByasT9JAUnPvs4mAWMApUigQ3zPrNS tx5lU4kuaqDnZPmgV8MZhzWKDsn+gypBYoN+QtIi1Zc5AtfkI7qRjxnWev0Bq61EgiaA h8HnL6iuXl54G52A77XOHHwV3kZQHwMbT1ZHVJtj4rG49V6ANOSJCMdVMMU/5BVZAiTV Yi3ICW3lXhp9Fdk+TAaGcaAont5S69om87yagy1gRH9hvKfr8k+UmxgrERGLFDxtEamE utGD/5kduovma5X2vEKFrViJzalQnRkPCdXa99CfMPZCiS/Sr9JPlWX3nsAgM9v/2UvK yo/Q== X-Gm-Message-State: AJIora9ea15J6WHhPBlW7xZxmIcHuKS2VRPBTtcIT2BGEq8CBE8GQOU/ 4N5D2EUW/M4cw+yxYj78sxk= X-Received: by 2002:adf:eb48:0:b0:21d:7ffc:4919 with SMTP id u8-20020adfeb48000000b0021d7ffc4919mr17595386wrn.537.1657548545552; Mon, 11 Jul 2022 07:09:05 -0700 (PDT) Received: from [192.168.4.247] (89-39-107-113.hosted-by-worldstream.net. [89.39.107.113]) by smtp.gmail.com with ESMTPSA id h15-20020a1ccc0f000000b0039c54bb28f2sm7085334wmb.36.2022.07.11.07.09.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Jul 2022 07:09:05 -0700 (PDT) Date: Mon, 11 Jul 2022 18:08:53 +0400 From: Yassine Oudjana Subject: Re: [PATCH 3/3] media: i2c: ak7375: Add regulator management To: Jacopo Mondi Cc: Mauro Carvalho Chehab , Rob Herring , Krzysztof Kozlowski , Tianshu Qiu , Bingbu Cao , Yassine Oudjana , linux-media@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Message-Id: In-Reply-To: <20220711133423.w6yzbcfqwv2muu34@uno.localdomain> References: <20220711042838.213351-1-y.oudjana@protonmail.com> <20220711042838.213351-4-y.oudjana@protonmail.com> <20220711133423.w6yzbcfqwv2muu34@uno.localdomain> X-Mailer: geary/40.0 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 11 2022 at 15:34:23 +0200, Jacopo Mondi wrote: > Hello Yassine > > On Mon, Jul 11, 2022 at 08:28:39AM +0400, Yassine Oudjana wrote: >> From: Yassine Oudjana >> >> Make the driver get needed regulators on probe and enable/disable >> them on runtime PM callbacks. >> >> Signed-off-by: Yassine Oudjana >> --- >> drivers/media/i2c/ak7375.c | 39 >> ++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 39 insertions(+) >> >> diff --git a/drivers/media/i2c/ak7375.c b/drivers/media/i2c/ak7375.c >> index 40b1a4aa846c..59d5cb00e3ba 100644 >> --- a/drivers/media/i2c/ak7375.c >> +++ b/drivers/media/i2c/ak7375.c >> @@ -6,6 +6,7 @@ >> #include >> #include >> #include >> +#include >> #include >> #include >> >> @@ -23,17 +24,32 @@ >> */ >> #define AK7375_CTRL_STEPS 64 >> #define AK7375_CTRL_DELAY_US 1000 >> +/* >> + * The vcm takes around 3 ms to power on and start taking >> + * I2C messages. This value was found experimentally due to >> + * lack of documentation. 2 ms is added as a safety margin. >> + */ >> +#define AK7375_POWER_DELAY_US 5000 >> >> #define AK7375_REG_POSITION 0x0 >> #define AK7375_REG_CONT 0x2 >> #define AK7375_MODE_ACTIVE 0x0 >> #define AK7375_MODE_STANDBY 0x40 >> >> +static const char * const ak7375_supply_names[] = { >> + "vdd", >> + "vio", >> +}; >> + >> +#define AK7375_NUM_SUPPLIES ARRAY_SIZE(ak7375_supply_names) >> + >> /* ak7375 device structure */ >> struct ak7375_device { >> struct v4l2_ctrl_handler ctrls_vcm; >> struct v4l2_subdev sd; >> struct v4l2_ctrl *focus; >> + struct regulator_bulk_data supplies[AK7375_NUM_SUPPLIES]; >> + >> /* active or standby mode */ >> bool active; >> }; >> @@ -132,6 +148,7 @@ static int ak7375_init_controls(struct >> ak7375_device *dev_vcm) >> static int ak7375_probe(struct i2c_client *client) >> { >> struct ak7375_device *ak7375_dev; >> + int i; > > I would have moved this one down to maintain variable declaration > in the in-famous reverse-xmas-tree ordering. Up to you. I'm used to declaring variables in the order of first use, but I don't really mind it either way. I'll move it down. > >> int ret; >> >> ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev), >> @@ -139,6 +156,17 @@ static int ak7375_probe(struct i2c_client >> *client) >> if (!ak7375_dev) >> return -ENOMEM; >> >> + for (i = 0; i < AK7375_NUM_SUPPLIES; i++) >> + ak7375_dev->supplies[i].supply = ak7375_supply_names[i]; >> + >> + ret = devm_regulator_bulk_get(&client->dev, AK7375_NUM_SUPPLIES, >> + ak7375_dev->supplies); >> + if (ret) { >> + dev_err(&client->dev, "Failed to get regulators: %pe", >> + ERR_PTR(ret)); >> + return ret; >> + } >> + >> v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops); >> ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; >> ak7375_dev->sd.internal_ops = &ak7375_int_ops; >> @@ -210,6 +238,10 @@ static int __maybe_unused >> ak7375_vcm_suspend(struct device *dev) >> if (ret) >> dev_err(dev, "%s I2C failure: %d\n", __func__, ret); >> >> + ret = regulator_bulk_disable(AK7375_NUM_SUPPLIES, >> ak7375_dev->supplies); >> + if (ret) >> + return ret; >> + >> ak7375_dev->active = false; >> >> return 0; >> @@ -230,6 +262,13 @@ static int __maybe_unused >> ak7375_vcm_resume(struct device *dev) >> if (ak7375_dev->active) >> return 0; >> >> + ret = regulator_bulk_enable(AK7375_NUM_SUPPLIES, >> ak7375_dev->supplies); >> + if (ret) >> + return ret; >> + >> + /* Wait for vcm to become ready */ >> + usleep_range(AK7375_POWER_DELAY_US, AK7375_POWER_DELAY_US + 10); >> + > > Isn't 10usec a very small delay to be given to usleep_range() for a > delay of at least 3msec ? Also assuming 5msec just to be safe seems a > little arbitrary. Adding 2 milliseconds in the wakeup path introduces > a non-negligible delay. I must admit that I didn't give it too much thought. I just did it similar to the other delay used in this driver (AK7375_CTRL_DELAY_US). As for adding 2ms, I don't know what the worst case wake-up time is since I don't have a datasheet on hand, so I just wanted to stay safe. Also, this driver doesn't really recover if it fails to resume (which is what used to happen before adding a delay). Rounding up to 5ms felt good enough. > > It's likely a detail, but according to > Documentation/timers/timers-howto.rst > > Since usleep_range is built on top of hrtimers, the > wakeup will be very precise (ish), thus a simple > usleep function would likely introduce a large number > of undesired interrupts. > > With the introduction of a range, the scheduler is > free to coalesce your wakeup with any other wakeup > that may have happened for other reasons, or at the > worst case, fire an interrupt for your upper bound. > > The larger a range you supply, the greater a chance > that you will not trigger an interrupt; this should > be balanced with what is an acceptable upper bound on > delay / performance for your specific code path. Exact > tolerances here are very situation specific, thus it > is left to the caller to determine a reasonable range. > > If you have a min of 3msec I would try with a range of (3000, 3500). > What do you think ? Seems good. I haven't yet had it fail to power on within 3ms of turning on regulators so I guess there is no reason to worry about it. >> >> ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT, >> AK7375_MODE_ACTIVE, 1); >> if (ret) { >> -- >> 2.37.0 > Thanks for the review, Yassine