Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1072808pxb; Thu, 15 Apr 2021 13:19:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzePHJv+IGy60a/ayvCy3NW3EY72a3N/xljlfg9rgQqiGW4/RFa44RxcFGrZwswM2+N9K9i X-Received: by 2002:a17:902:7589:b029:e8:c011:1f28 with SMTP id j9-20020a1709027589b02900e8c0111f28mr5659719pll.35.1618517978186; Thu, 15 Apr 2021 13:19:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618517978; cv=none; d=google.com; s=arc-20160816; b=BeJ+SBmW1oBAWAvRRodKZS7U6jr6jQNmKmdefVokWdwPabs4o3+0nzI47TohLfukgF ku0IN0dA+epIdl1ixXLAIVdB8Id7XeKVc1rMf7JKxraN1QTsQ4DxWw751xXmVsg5z9Ly 8Y5oyJDcSUHgZ0lw5OquhGfzmLRQH9fbTIVGgDGRojl3VP7G6Tr1nXk982MyLYKS3L8r ptdlROvpjB9z0HCBm8STSYDSMVMrrogG24IlMdcj7l6zS0kumW3bnNXe/SIwCDcmJryW Uoxw11R97ZsejUsEGnx/Yer60FTjE0CIn9RtoGxWCwMl9KCTpHwZv95WIF/XFMfgvAvf 7f3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=MYexkm8MglMgY5gKtzPriL0PVyMV7KvuXsvk0JXPVkE=; b=CkufMnw828R3cawlSxkCvISm6bmNoryUUvvGyniapy9meLjj4GEd+GE2O9HojBWbtR R9icHwlDHvfVLyEILb+QXnIoznbt4bk3ZNTMrcM+AyhTWkRfCY4CJ1L27n91U/kNCjda azMykp7/xcMaZGFDryCwsBJ7GUDcwU0pVaehrrrqliADdR/tytvm08ZzCU+VWlidmz4M emhKZ6vgufmDNJx14AxGGvOr9Cn6oIhf742RqfUqAvHqfwQzlVdJ0Um8KlDKd21B93Yl /njs3yPDZfYPGcQxuZ7gX65FeX9pyccFP9gCNQkomfgT5EDWRkMwtyf0cLZT7wmtSEbU U74Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=OGFZsgoC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h12si4791303plr.246.2021.04.15.13.19.23; Thu, 15 Apr 2021 13:19:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=OGFZsgoC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234806AbhDOTOc (ORCPT + 99 others); Thu, 15 Apr 2021 15:14:32 -0400 Received: from perceval.ideasonboard.com ([213.167.242.64]:50908 "EHLO perceval.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234701AbhDOTOc (ORCPT ); Thu, 15 Apr 2021 15:14:32 -0400 Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 398A389A; Thu, 15 Apr 2021 21:14:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1618514047; bh=JtK/AlUL0+jty7sGcYDG83XVQxotjC9OrYzlUAE0xsI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=OGFZsgoCQea8EQfKNO7uEdjj8HCHTnQor4xxjybYKTNhSYrItVl35Iu38c2KhppC7 6vT4WdEeyv4MBC6CUi/yGMD2KZjgn42ayJTriUMDodCPP9v2ujD2Cp9+LXy/RJKbfR 4+L/lMbv7eAZTLJm9I5tC7tM+JbP0MQ8Av44cHbQ= Date: Thu, 15 Apr 2021 22:14:05 +0300 From: Laurent Pinchart To: Jacopo Mondi Cc: Jacopo Mondi , Geert Uytterhoeven , Magnus Damm , Kieran Bingham , Rob Herring , linux-renesas-soc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 2/5] media: i2c: max9286: Use "maxim,gpio-poc" property Message-ID: References: <20210414135128.180980-1-jacopo+renesas@jmondi.org> <20210414135128.180980-3-jacopo+renesas@jmondi.org> <20210415065848.xgisi5cpcxvnxzb4@uno.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210415065848.xgisi5cpcxvnxzb4@uno.localdomain> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jacopo, On Thu, Apr 15, 2021 at 08:58:48AM +0200, Jacopo Mondi wrote: > On Thu, Apr 15, 2021 at 03:00:56AM +0300, Laurent Pinchart wrote: > > On Wed, Apr 14, 2021 at 03:51:25PM +0200, Jacopo Mondi wrote: > > > The 'maxim,gpio-poc' property is used when the remote camera > > > power-over-coax is controlled by one of the MAX9286 gpio lines, > > > to instruct the driver about which line to use and what the line > > > polarity is. > > > > > > Add to the max9286 driver support for parsing the newly introduce > > > > s/introduce/introduced/ > > > > > property and use it if available in place of the usual supply, as it is > > > not possible to establish one as consumer of the max9286 gpio > > > controller. > > > > > > If the new property is present, no gpio controller is registered and > > > 'poc-supply' is ignored. > > > > > > In order to maximize code re-use, break out the max9286 gpio handling > > > function so that they can be used by the gpio controller through the > > > gpio-consumer API, or directly by the driver code. > > > > > > Wrap the power up and power down routines to their own function to > > > be able to use either the gpio line directly or the supply. This will > > > make it easier to control the remote camera power at run time. > > > > I would have split the patch in two, with a first patch that refactors > > the code, and a second one that extends it, but that's no big deal. > > > > > Signed-off-by: Jacopo Mondi > > > --- > > > drivers/media/i2c/max9286.c | 125 +++++++++++++++++++++++++++--------- > > > 1 file changed, 96 insertions(+), 29 deletions(-) > > > > > > diff --git a/drivers/media/i2c/max9286.c b/drivers/media/i2c/max9286.c > > > index 6fd4d59fcc72..0c125f7b3d9b 100644 > > > --- a/drivers/media/i2c/max9286.c > > > +++ b/drivers/media/i2c/max9286.c > > > @@ -15,6 +15,7 @@ > > > #include > > > #include > > > #include > > > +#include > > > #include > > > #include > > > #include > > > @@ -165,6 +166,9 @@ struct max9286_priv { > > > > > > u32 reverse_channel_mv; > > > > > > + u32 gpio_poc; > > > + u32 gpio_poc_flags; > > > + > > > struct v4l2_ctrl_handler ctrls; > > > struct v4l2_ctrl *pixelrate; > > > > > > @@ -1022,20 +1026,27 @@ static int max9286_setup(struct max9286_priv *priv) > > > return 0; > > > } > > > > > > -static void max9286_gpio_set(struct gpio_chip *chip, > > > - unsigned int offset, int value) > > > +static int max9286_gpio_set(struct max9286_priv *priv, unsigned int offset, > > > + int value) > > > { > > > - struct max9286_priv *priv = gpiochip_get_data(chip); > > > - > > > if (value) > > > priv->gpio_state |= BIT(offset); > > > else > > > priv->gpio_state &= ~BIT(offset); > > > > > > - max9286_write(priv, 0x0f, MAX9286_0X0F_RESERVED | priv->gpio_state); > > > + return max9286_write(priv, 0x0f, > > > + MAX9286_0X0F_RESERVED | priv->gpio_state); > > > +} > > > + > > > +static void max9286_gpiochip_set(struct gpio_chip *chip, > > > + unsigned int offset, int value) > > > +{ > > > + struct max9286_priv *priv = gpiochip_get_data(chip); > > > + > > > + max9286_gpio_set(priv, offset, value); > > > } > > > > > > -static int max9286_gpio_get(struct gpio_chip *chip, unsigned int offset) > > > +static int max9286_gpiochip_get(struct gpio_chip *chip, unsigned int offset) > > > { > > > struct max9286_priv *priv = gpiochip_get_data(chip); > > > > > > @@ -1055,8 +1066,8 @@ static int max9286_register_gpio(struct max9286_priv *priv) > > > gpio->of_node = dev->of_node; > > > gpio->ngpio = 2; > > > gpio->base = -1; > > > - gpio->set = max9286_gpio_set; > > > - gpio->get = max9286_gpio_get; > > > + gpio->set = max9286_gpiochip_set; > > > + gpio->get = max9286_gpiochip_get; > > > gpio->can_sleep = true; > > > > > > /* GPIO values default to high */ > > > @@ -1069,6 +1080,75 @@ static int max9286_register_gpio(struct max9286_priv *priv) > > > return ret; > > > } > > > > > > +static int max9286_parse_gpios(struct max9286_priv *priv) > > > +{ > > > + struct device *dev = &priv->client->dev; > > > + u32 gpio_poc[2]; > > > + int ret; > > > + > > > + /* > > > + * Parse the "gpio-poc" vendor property. If the camera power is > > > + * controlled by one of the MAX9286 gpio lines, do not register > > > + * the gpio controller and ignore 'poc-supply'. > > > + */ > > > + ret = of_property_read_u32_array(dev->of_node, > > > + "maxim,gpio-poc", gpio_poc, 2); > > > + if (!ret) { > > > + priv->gpio_poc = gpio_poc[0]; > > > + priv->gpio_poc_flags = gpio_poc[1]; > > > + if ((priv->gpio_poc != 0 && priv->gpio_poc != 1) || > > > > You could simply test priv->gpio_poc > 1. > > > > > + (priv->gpio_poc_flags != GPIO_ACTIVE_HIGH && > > > + priv->gpio_poc_flags != GPIO_ACTIVE_LOW)) { > > > + dev_err(dev, "Invalid 'gpio-poc': (%u %u)\n", > > > + priv->gpio_poc, priv->gpio_poc_flags); > > > + return -EINVAL; > > > + } > > > + > > > + /* GPIO values default to high */ > > > + priv->gpio_state = BIT(0) | BIT(1); > > > > Why is that ? > > > As the set/get functions of gpiochip use the gpio_state and I wanted > to use the same functions for the internal gpio handling I used > gpio_state in gpio_set(). My thinking was that in this way altering > the gpio line would be visibile to gpio consumers... which we don't > have as I won't register the gpio-controller :) My question was why they default to high here, when they default to low when there's a gpio-controller property. > > > + priv->regulator = NULL; > > > > As priv is initialized to 0, you can skip this. > > Yes, I liked it explicit as it is used as flag, but it is not > required... > > > > + > > > + return 0; > > > + } > > > + > > > + ret = max9286_register_gpio(priv); > > > + if (ret) > > > + return ret; > > > + > > > + priv->regulator = devm_regulator_get(dev, "poc"); > > > + if (IS_ERR(priv->regulator)) { > > > + if (PTR_ERR(priv->regulator) != -EPROBE_DEFER) > > > + dev_err(dev, "Unable to get PoC regulator (%ld)\n", > > > + PTR_ERR(priv->regulator)); > > > + return PTR_ERR(priv->regulator); > > > + } > > > + > > > + return 0; > > > +} > > > + > > > +static int max9286_poc_enable(struct max9286_priv *priv, bool enable) > > > +{ > > > + int ret; > > > + > > > + /* If "poc-gpio" is used, toggle the line and do not use regulator. */ > > > + if (!priv->regulator) > > > + return max9286_gpio_set(priv, priv->gpio_poc, > > > + enable ^ priv->gpio_poc_flags); > > > + > > > + /* Otherwise PoC is controlled using a regulator. */ > > > + if (enable) { > > > + ret = regulator_enable(priv->regulator); > > > + if (ret < 0) { > > > + dev_err(&priv->client->dev, "Unable to turn PoC on\n"); > > > > As error message when max9286_gpio_set() fails (at least in the enable > > case) would be good too. Bonus points if there's a single dev_err() > > call. > > I'll see how it looks like > > > > + return ret; > > > + } > > > + > > > + return 0; > > > + } > > > + > > > + return regulator_disable(priv->regulator); > > > +} > > > + > > > static int max9286_init(struct device *dev) > > > { > > > struct max9286_priv *priv; > > > @@ -1078,17 +1158,14 @@ static int max9286_init(struct device *dev) > > > client = to_i2c_client(dev); > > > priv = i2c_get_clientdata(client); > > > > > > - /* Enable the bus power. */ > > > - ret = regulator_enable(priv->regulator); > > > - if (ret < 0) { > > > - dev_err(&client->dev, "Unable to turn PoC on\n"); > > > + ret = max9286_poc_enable(priv, true); > > > + if (ret) > > > return ret; > > > - } > > > > > > ret = max9286_setup(priv); > > > if (ret) { > > > dev_err(dev, "Unable to setup max9286\n"); > > > - goto err_regulator; > > > + goto err_poc_disable; > > > } > > > > > > /* > > > @@ -1098,7 +1175,7 @@ static int max9286_init(struct device *dev) > > > ret = max9286_v4l2_register(priv); > > > if (ret) { > > > dev_err(dev, "Failed to register with V4L2\n"); > > > - goto err_regulator; > > > + goto err_poc_disable; > > > } > > > > > > ret = max9286_i2c_mux_init(priv); > > > @@ -1114,8 +1191,8 @@ static int max9286_init(struct device *dev) > > > > > > err_v4l2_register: > > > max9286_v4l2_unregister(priv); > > > -err_regulator: > > > - regulator_disable(priv->regulator); > > > +err_poc_disable: > > > + max9286_poc_enable(priv, false); > > > > > > return ret; > > > } > > > @@ -1286,20 +1363,10 @@ static int max9286_probe(struct i2c_client *client) > > > */ > > > max9286_configure_i2c(priv, false); > > > > > > - ret = max9286_register_gpio(priv); > > > + ret = max9286_parse_gpios(priv); > > > if (ret) > > > goto err_powerdown; > > > > > > - priv->regulator = devm_regulator_get(&client->dev, "poc"); > > > - if (IS_ERR(priv->regulator)) { > > > - if (PTR_ERR(priv->regulator) != -EPROBE_DEFER) > > > - dev_err(&client->dev, > > > - "Unable to get PoC regulator (%ld)\n", > > > - PTR_ERR(priv->regulator)); > > > - ret = PTR_ERR(priv->regulator); > > > - goto err_powerdown; > > > - } > > > - > > > ret = max9286_parse_dt(priv); > > > if (ret) > > > goto err_powerdown; > > > @@ -1326,7 +1393,7 @@ static int max9286_remove(struct i2c_client *client) > > > > > > max9286_v4l2_unregister(priv); > > > > > > - regulator_disable(priv->regulator); > > > + max9286_poc_enable(priv, false); > > > > > > gpiod_set_value_cansleep(priv->gpiod_pwdn, 0); > > > -- Regards, Laurent Pinchart