Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp3968191ybn; Fri, 27 Sep 2019 14:10:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqyIjTBMDps/RrnIJwNzuRw/ucdR4VG4NdxtwBXKOyn5D0AF2yUZ0xJrGjXJ943/CPqb/NyW X-Received: by 2002:aa7:dcd7:: with SMTP id w23mr6798695edu.170.1569618658014; Fri, 27 Sep 2019 14:10:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569618658; cv=none; d=google.com; s=arc-20160816; b=A+Q+IOnlSJ8YBCZwcf+EDvIWG9oDmBy1zFCizb3s3mt0UhAp8QaIe1PcCYCm1l9rku pfmrSd97vUYWF8rFa5AE0TxmG1GjdQxQM9HS+au62NgkWmsoNaQhs7CgNdzT96gW/iN1 bMsawynL0dlH2lXVcUBIYEvmqhotS5cbdbZRwYEobUHfzeZWmJlsldrtsyzf9V5tjnrr jbZjkYQGWNVOKILSm5WODaTg0EhiCaCyEoEFlh0prUh5OtffwKHJS1oOd35dVvgoLVEe 4rpjXyyrp+dTKZl/L3Dl3Ups9WivpGzPJUPUUU0jYJanfcLHYHXYtGT8WoUf9fIJ0cy+ xQNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=SrBJgeprlzWO9zx74wCPUtbrggQEGPmmoT/ZYf6mMxQ=; b=WQisOESOCGD43JSbE+aLOOsIUnineQ8mWClF9OIdeu8QK9YMst2mkIgJyPPwwv1RLm HpW3TlokVJ3di7QQTxfDSM3sUUIczgrTcdr7pD0huE+2qiaahbInhVsZPpcp3XytKSny mkKnw9HgEQyUckJ6GK9DLofAgo9H/onU5tzZxLz7Gh+QjcYMStlP5j8YRv6SOGxu9oes Bsa9AVStrwO0+d+uLaK/ObdvRqtB9rkhSRjc33iZy4i7AvZqrNwHqHPhfDQTMa5fSMVp nz+pJgmv0s+3lLV5S/2fgqWs2Ehb6SZaVC0zqNiBqc9mTXiTz33CnfVdNIcNteYF/7v9 8zvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=rQRp19LE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p40si2633041eda.334.2019.09.27.14.10.33; Fri, 27 Sep 2019 14:10:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=rQRp19LE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1726594AbfI0VKP (ORCPT + 99 others); Fri, 27 Sep 2019 17:10:15 -0400 Received: from mail-oi1-f194.google.com ([209.85.167.194]:39714 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725789AbfI0VKO (ORCPT ); Fri, 27 Sep 2019 17:10:14 -0400 Received: by mail-oi1-f194.google.com with SMTP id w144so6392108oia.6; Fri, 27 Sep 2019 14:10:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SrBJgeprlzWO9zx74wCPUtbrggQEGPmmoT/ZYf6mMxQ=; b=rQRp19LEZBL47xcpsMYy/ljurXoac2Q3Jv5gJpnqoANyQCful26DEzqyrgdd39hpJa 1gdPiHv8RQMKeytRBXvKm97BrTMDV+YXHXgvkN0Nm4fTFd5649WmWuytE9fIAO9lS0Y3 4cNN4ZUfYaTXxTjWQ/tCjuKVXkexwuzo+e+JgKyBu66g8lBNrePSmXZ41CKKqYgVU//M DJgGSSNXyISWjlhT7ZhWct3JDsZh25LyJNHIjMHEUAgp+eAZ5rxNWmi2Q8IQ4noIMUph Z/YSttn/8yYd5N+Ol3oylmLnEpulZWdfIebzdbfOK+BGvq7KJmkqjMAQMG0C+g948eL6 AJfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SrBJgeprlzWO9zx74wCPUtbrggQEGPmmoT/ZYf6mMxQ=; b=FnFp89yTvoPIKWYYfoJxwgDe7AloUXIg3Ov56Lm8B8PGIXNaViJLDP3o9odqCasdj7 kUnXUsxgExqbRn+bpMmvYkXpWHIwzLl8fjOfpkD9NuAGR6j5TQ1LwKh/WR4OgaA94JTG +HF4sYRes62CkdsT+t+Q7haMSDurAMuUDzuBrSFBanuqZabfnMnKQx4C6lvUy+rl6nOS pvF3fJUkNcp0r8600DTuzN7d5gvE5BcNu/MnmLwB8fhVfZ6S6EeVNVzPfgfFJYaumfqF 9j8ayGMh1iBMmqmMFIk/lQ+A40uk9R8q9u08ECLcmzU7cQlh9LVzKfmrOmqd18de7rxM QWUw== X-Gm-Message-State: APjAAAUWGUaSV+UFQ54GxlN5J/ThUMmVRQalKvu2/TXOJn5W2DaeNqDt 0TpJtouvm+2X2eQ/YQBc6KM3fUWRqGjO7M/40nK+RF+p X-Received: by 2002:aca:4e87:: with SMTP id c129mr8840475oib.7.1569618611688; Fri, 27 Sep 2019 14:10:11 -0700 (PDT) MIME-Version: 1.0 References: <20190927184722.31989-1-bparrot@ti.com> <20190927184722.31989-7-bparrot@ti.com> In-Reply-To: <20190927184722.31989-7-bparrot@ti.com> From: "Lad, Prabhakar" Date: Fri, 27 Sep 2019 22:09:45 +0100 Message-ID: Subject: Re: [Patch v4 6/8] media: i2c: ov2659: Add powerdown/reset gpio handling To: Benoit Parrot Cc: Hans Verkuil , Sakari Ailus , linux-media , devicetree@vger.kernel.org, LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Benoit, thank you for the patch. On Fri, Sep 27, 2019 at 7:49 PM Benoit Parrot wrote: > > On some board it is possible that the sensor 'powerdown' and or 'reset' > pin might be controlled by gpio instead of being tied. > > To implement we add pm_runtime support which will handle the power > up/down sequence when it is available otherwise the sensor will be > powered on at module insertion/probe and powered off at module removal. > > Now originally the driver assumed that the sensor would always stay > powered and keep its register setting. We cannot assume this anymore, so > every time we "power up" we need to re-program the initial registers > configuration first. This was previously done only at probe time. > > Signed-off-by: Benoit Parrot > --- > drivers/media/i2c/Kconfig | 2 +- > drivers/media/i2c/ov2659.c | 88 +++++++++++++++++++++++++++++++++++++- > 2 files changed, 87 insertions(+), 3 deletions(-) > > diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig > index 7eee1812bba3..315c1d8bdb7b 100644 > --- a/drivers/media/i2c/Kconfig > +++ b/drivers/media/i2c/Kconfig > @@ -634,7 +634,7 @@ config VIDEO_OV2640 > config VIDEO_OV2659 > tristate "OmniVision OV2659 sensor support" > depends on VIDEO_V4L2 && I2C > - depends on MEDIA_CAMERA_SUPPORT > + depends on MEDIA_CAMERA_SUPPORT && GPIOLIB > select V4L2_FWNODE > help > This is a Video4Linux2 sensor driver for the OmniVision > diff --git a/drivers/media/i2c/ov2659.c b/drivers/media/i2c/ov2659.c > index cd4625432264..7d0baa386644 100644 > --- a/drivers/media/i2c/ov2659.c > +++ b/drivers/media/i2c/ov2659.c > @@ -22,9 +22,11 @@ > > #include > #include > +#include > #include > #include > #include > +#include > > #include > #include > @@ -218,6 +220,11 @@ struct ov2659 { > struct sensor_register *format_ctrl_regs; > struct ov2659_pll_ctrl pll; > int streaming; > + /* used to control the sensor PWDN pin */ > + struct gpio_desc *pwdn_gpio; > + /* used to control the sensor RESETB pin */ > + struct gpio_desc *resetb_gpio; > + int on; > }; > > static const struct sensor_register ov2659_init_regs[] = { > @@ -1184,10 +1191,19 @@ static int ov2659_s_stream(struct v4l2_subdev *sd, int on) > /* Stop Streaming Sequence */ > ov2659_set_streaming(ov2659, 0); > ov2659->streaming = on; > + pm_runtime_put(&client->dev); > goto unlock; > } > > - ret = ov2659_set_pixel_clock(ov2659); > + ret = pm_runtime_get_sync(&client->dev); > + if (ret < 0) { > + pm_runtime_put_noidle(&client->dev); > + goto unlock; > + } > + > + ret = ov2659_init(sd, 0); > + if (!ret) > + ret = ov2659_set_pixel_clock(ov2659); > if (!ret) > ret = ov2659_set_frame_size(ov2659); > if (!ret) > @@ -1229,6 +1245,9 @@ static int ov2659_s_ctrl(struct v4l2_ctrl *ctrl) > struct ov2659 *ov2659 = > container_of(ctrl->handler, struct ov2659, ctrls); > > + if (!ov2659->on) > + return 0; > + why is this check only in s_ctrl() ? I think we could just drop this variable. Cheers, --Prabhakar > switch (ctrl->id) { > case V4L2_CID_TEST_PATTERN: > return ov2659_set_test_pattern(ov2659, ctrl->val); > @@ -1246,6 +1265,43 @@ static const char * const ov2659_test_pattern_menu[] = { > "Vertical Color Bars", > }; > > +static int ov2659_power_off(struct device *dev) > +{ > + struct i2c_client *client = to_i2c_client(dev); > + struct v4l2_subdev *sd = i2c_get_clientdata(client); > + struct ov2659 *ov2659 = to_ov2659(sd); > + > + dev_dbg(&client->dev, "%s:\n", __func__); > + > + gpiod_set_value(ov2659->pwdn_gpio, 1); > + > + ov2659->on = false; > + > + return 0; > +} > + > +static int ov2659_power_on(struct device *dev) > +{ > + struct i2c_client *client = to_i2c_client(dev); > + struct v4l2_subdev *sd = i2c_get_clientdata(client); > + struct ov2659 *ov2659 = to_ov2659(sd); > + > + dev_dbg(&client->dev, "%s:\n", __func__); > + > + gpiod_set_value(ov2659->pwdn_gpio, 0); > + > + if (ov2659->resetb_gpio) { > + gpiod_set_value(ov2659->resetb_gpio, 1); > + usleep_range(500, 1000); > + gpiod_set_value(ov2659->resetb_gpio, 0); > + usleep_range(3000, 5000); > + } > + > + ov2659->on = true; > + > + return 0; > +} > + > /* ----------------------------------------------------------------------------- > * V4L2 subdev internal operations > */ > @@ -1326,7 +1382,6 @@ static int ov2659_detect(struct v4l2_subdev *sd) > ret = -ENODEV; > } else { > dev_info(&client->dev, "Found OV%04X sensor\n", id); > - ret = ov2659_init(sd, 0); > } > } > > @@ -1403,6 +1458,18 @@ static int ov2659_probe(struct i2c_client *client) > ov2659->xvclk_frequency > 27000000) > return -EINVAL; > > + /* Optional gpio don't fail if not present */ > + ov2659->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown", > + GPIOD_OUT_LOW); > + if (IS_ERR(ov2659->pwdn_gpio)) > + return PTR_ERR(ov2659->pwdn_gpio); > + > + /* Optional gpio don't fail if not present */ > + ov2659->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset", > + GPIOD_OUT_HIGH); > + if (IS_ERR(ov2659->resetb_gpio)) > + return PTR_ERR(ov2659->resetb_gpio); > + > v4l2_ctrl_handler_init(&ov2659->ctrls, 2); > ov2659->link_frequency = > v4l2_ctrl_new_std(&ov2659->ctrls, &ov2659_ctrl_ops, > @@ -1448,6 +1515,8 @@ static int ov2659_probe(struct i2c_client *client) > ov2659->frame_size = &ov2659_framesizes[2]; > ov2659->format_ctrl_regs = ov2659_formats[0].format_ctrl_regs; > > + ov2659_power_on(&client->dev); > + > ret = ov2659_detect(sd); > if (ret < 0) > goto error; > @@ -1461,10 +1530,15 @@ static int ov2659_probe(struct i2c_client *client) > > dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name); > > + pm_runtime_set_active(&client->dev); > + pm_runtime_enable(&client->dev); > + pm_runtime_idle(&client->dev); > + > return 0; > > error: > v4l2_ctrl_handler_free(&ov2659->ctrls); > + ov2659_power_off(&client->dev); > media_entity_cleanup(&sd->entity); > mutex_destroy(&ov2659->lock); > return ret; > @@ -1480,9 +1554,18 @@ static int ov2659_remove(struct i2c_client *client) > media_entity_cleanup(&sd->entity); > mutex_destroy(&ov2659->lock); > > + pm_runtime_disable(&client->dev); > + if (!pm_runtime_status_suspended(&client->dev)) > + ov2659_power_off(&client->dev); > + pm_runtime_set_suspended(&client->dev); > + > return 0; > } > > +static const struct dev_pm_ops ov2659_pm_ops = { > + SET_RUNTIME_PM_OPS(ov2659_power_off, ov2659_power_on, NULL) > +}; > + > static const struct i2c_device_id ov2659_id[] = { > { "ov2659", 0 }, > { /* sentinel */ }, > @@ -1500,6 +1583,7 @@ MODULE_DEVICE_TABLE(of, ov2659_of_match); > static struct i2c_driver ov2659_i2c_driver = { > .driver = { > .name = DRIVER_NAME, > + .pm = &ov2659_pm_ops, > .of_match_table = of_match_ptr(ov2659_of_match), > }, > .probe_new = ov2659_probe, > -- > 2.17.1 >