Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp423693imm; Fri, 14 Sep 2018 00:09:45 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaOFi1XI1v2fLkQNLaZ0b39r4dY9P0F4XP4KwgxcD3X0/3aVmS/bI6X7sD+XaZn0o0gj9pi X-Received: by 2002:a17:902:900a:: with SMTP id a10-v6mr10678118plp.143.1536908985525; Fri, 14 Sep 2018 00:09:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536908985; cv=none; d=google.com; s=arc-20160816; b=e+3aZazJu2cQ0bnfXEj7k8BczbBtUv3LHZc+T57NHOJoY908ZGnhopLzJLB9vfsM8H 72Fp0vn8z5KIbfc+XTa2Z1+vs6x/HZcxGutJVbBRDxTMfF3ikMPBLlukPma/0/WnhMHy AgtPFs58tLHF1qL2ncAPMvaU1zoQPnXYfPScfk+EENuOwCzmLym8g+iWBJeu0AAd2v4l lJHMSqMWUZax2CUEM4Ndn5rPgKrM3pCg+4uhSeHj8Y5UEaVtkqtrBP+zBJBeRLWvrAmQ JGvnXPrxK8HPzgWN4AXhdmWw843ER5TIoqD6gSR05r+RTQBzoIByqbYAO+CChYY12ABC vRmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=8yNP+JxRFnZBfKqIuPnkwG1FT1w7tmRFz4KB2VwboKk=; b=HWC8WyOsmemQvbxR1XKSDgLOIwe6kQJdyW5w1ZH1/HOoXoM89dsMm+CadMLBwLmqPe pukFIkDqkaMHtms4e7H/KaORwTzMfMnSstozaLi2Heo/+Vwggk6ex5xql1Sxr67IKE2D iyjpJbpC412Lb+2tXjyzaGuXcD+m0clt06u9vrnZ5DTTw/FeNS4R1XP46e5pgxG41V9v WZozwpovtSdLr9Haj7UagAQz0RmgBLam0I4fW1BL1uqXEvn1kcfDD+DPyCiWEfvSsG/o U4EhsPR0W9YE41PxlrD8iuWWE/fEp7hueens9vfgM8Ot6QxQuk4z6ESNVqGLtElLo1XZ 0CrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="n/peedcc"; 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 t13-v6si6267662pgl.461.2018.09.14.00.09.10; Fri, 14 Sep 2018 00:09:45 -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="n/peedcc"; 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 S1727669AbeINMVv (ORCPT + 99 others); Fri, 14 Sep 2018 08:21:51 -0400 Received: from mail-ed1-f66.google.com ([209.85.208.66]:35729 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726822AbeINMVu (ORCPT ); Fri, 14 Sep 2018 08:21:50 -0400 Received: by mail-ed1-f66.google.com with SMTP id y20-v6so6609508edq.2; Fri, 14 Sep 2018 00:08:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=8yNP+JxRFnZBfKqIuPnkwG1FT1w7tmRFz4KB2VwboKk=; b=n/peedccgoSRjPHkssR2gD/2KpzDbYWKKf17b11eQ5qBaIBvoaYXp2dqzjXTQFhzBV Z3sJ32YJuJzQh86de5EEm6SHui6WDKH6GB4DaRV5X3iw5NCpoLjfa+8GVWrg2DdeYxwi 25qA5D7Jt6BryaBefBn94vKseGGb8vtbkjAmX/EIdPVlrcitNFwEYg99C+/MpoBgFyDL q72S+Avdp4sT8tHbeUZQOgmpzSwloZAqQFaM9NUW0GV5d4dOoom2waabGtLhEAHrbfwi nsVwNB9Utp71+6PAIpWPUIE5QPPbWiW0UZxo4eE162TXl0o2m7l9fYQCv6EBSBhMIWIQ holg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=8yNP+JxRFnZBfKqIuPnkwG1FT1w7tmRFz4KB2VwboKk=; b=nPrSQdPMQMC2uSwM7AJCJGhC3QhkCwegA3O8Rv8U+0ytlQrvn7ZWTRznVDxTa79O41 3lZwR3vOBu33nVOUvsStL8aaWpdi1865UGB4RQxQf+vJ/damVqYiYVlWU7n3HuEb9+kQ lzmqqkXossSMs8zLI7gZKNqyIPkd13/X8gjw/HjLpYHFP3mcki4zykcB2WriDnlp3vek HWsUUsQJGKSAxyB0xuU8FLSRrhoTxvlMIQ2z+9bHvtiG4awGbOEOrFJxNoKGA6Vc0v5e 7IFAqlaGhRnERsXnBa/EunQXa5qpXXgVC//PmpoWZEdor/12AkBMoEf6wCZTVD7yxCl3 akow== X-Gm-Message-State: APzg51Cc6HE31j4PAvrs6GQCrYctts1uRGptuVLHRhr3jueKYVhLD+Qs qo8UIQmsazOn+X6z1vZluOu6F8yZ X-Received: by 2002:a05:6402:1652:: with SMTP id s18mr17602703edx.35.1536908923074; Fri, 14 Sep 2018 00:08:43 -0700 (PDT) Received: from neopili.qtec.com (cpe.xe-3-0-1-778.vbrnqe10.dk.customer.tdc.net. [80.197.57.18]) by smtp.gmail.com with ESMTPSA id x48-v6sm4186111edm.18.2018.09.14.00.08.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Sep 2018 00:08:41 -0700 (PDT) From: Ricardo Ribalda Delgado To: Linus Walleij , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ricardo Ribalda Delgado Subject: [RFC] gpiolib: Fix gpio_direction_* for single direction GPIOs Date: Fri, 14 Sep 2018 09:08:38 +0200 Message-Id: <20180914070839.4667-1-ricardo.ribalda@gmail.com> X-Mailer: git-send-email 2.18.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org GPIOs with no programmable direction are not required to implement direction_output nor direction_input. If we try to set an output direction on an output-only GPIO or input direction on an input-only GPIO simply return 0. This allows this single direction GPIO to be used by libgpiod. Signed-off-by: Ricardo Ribalda Delgado --- drivers/gpio/gpiolib.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index af853749e0bb..3c09bf70e7ab 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2517,19 +2517,27 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); int gpiod_direction_input(struct gpio_desc *desc) { struct gpio_chip *chip; - int status = -EINVAL; + int status = 0; VALIDATE_DESC(desc); chip = desc->gdev->chip; - if (!chip->get || !chip->direction_input) { + if (!chip->get && chip->direction_input) { gpiod_warn(desc, - "%s: missing get() or direction_input() operations\n", + "%s: missing get() and direction_input() operations\n", __func__); return -EIO; } - status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); + if (chip->direction_input) { + status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); + } else if (chip->get_direction && + (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) { + gpiod_warn(desc, + "%s: missing direction_input() operation\n", + __func__); + return -EIO; + } if (status == 0) clear_bit(FLAG_IS_OUT, &desc->flags); @@ -2551,16 +2559,28 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) { struct gpio_chip *gc = desc->gdev->chip; int val = !!value; - int ret; + int ret = 0; - if (!gc->set || !gc->direction_output) { + if (!gc->set && !gc->direction_output) { gpiod_warn(desc, - "%s: missing set() or direction_output() operations\n", + "%s: missing set() and direction_output() operations\n", __func__); return -EIO; } - ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); + if (gc->direction_output) { + ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); + } else { + if (gc->get_direction && + gc->get_direction(gc, gpio_chip_hwgpio(desc))) { + gpiod_warn(desc, + "%s: missing direction_output() operation\n", + __func__); + return -EIO; + } + gc->set(gc, gpio_chip_hwgpio(desc), val); + } + if (!ret) set_bit(FLAG_IS_OUT, &desc->flags); trace_gpio_value(desc_to_gpio(desc), 0, val); -- 2.18.0