Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp483481imm; Fri, 21 Sep 2018 03:36:31 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZsTV2UqPHWeTA5nQMiBDrrCzaAedd77RQUFoQ9s3ZxdzPjbq01IK2H79JU2+fDkLrR9p11 X-Received: by 2002:a17:902:b40c:: with SMTP id x12-v6mr43710101plr.163.1537526191501; Fri, 21 Sep 2018 03:36:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537526191; cv=none; d=google.com; s=arc-20160816; b=vPqncN5CEuLvyO5CUI2z4IrGySq/2n3gPq39CIa2bAZGKvfOTMbr2BeddZ5mAc5yvK bS1pIwh4E1hjPPpQNdJOlVHF6bf3wVOfjuFh5xvQBO3icoaVlyJSJTp5LDU89c/TQ/w4 M2JSYfZ1e3IQGmtIc7XseB4bGMnEHsELE4B3ipY/TyvXMkCev3Hf6bxyTYoJUYLPynVd kI4wRi++6fg+FeavG42yO9PjwHYiHdPAsinCVpO14yeXAyRwfph913gy9rItI/x0icMC 2699oGHxBn/Nm+gE5HMeGxLymsPkoPWDAPXCGyWsCIJKzcdjVo7PgbpRhojj656zuhNQ RNgg== 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=KRReny5VpNNTdbbaaXk16iyPudk0xjZLoIJb/EyF1q4=; b=Wg4RrHQDVGtMLSMpYQ7tTqQZLrHKW+e74oaJFmT4AOn+zLoXhWAbK+bAqbQF4+sbvW KlNVyQbv76Moy4b+wu+mh8NTwxRtZ/OAbhqkxnLASmo3FcPUnixKB0whrdaoVbNjpMkV OLA9qRbxT7T/ga0WyOINk1D11puvBJ9ovR+rlCIwhmB8/iD0fzT0cM//kx2mldruGM/T yDhQMQ4HM9oRsHO7TqCDFQcr86/gi73aGfHbZyMbZXFWTcyhIuxPFM57X0jJTrQ37AVL yuRYC+s841DqQqTCe+us+GyKyALWULHR8HHCb45YEOifTVnK2dR2XtKf10io3ikihBtC GcPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OOYif+ZM; 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 32-v6si28119459plc.475.2018.09.21.03.36.14; Fri, 21 Sep 2018 03:36:31 -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=OOYif+ZM; 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 S2389628AbeIUQYY (ORCPT + 99 others); Fri, 21 Sep 2018 12:24:24 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:43423 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727554AbeIUQYX (ORCPT ); Fri, 21 Sep 2018 12:24:23 -0400 Received: by mail-ed1-f65.google.com with SMTP id u23so1191175edx.10; Fri, 21 Sep 2018 03:36:07 -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=KRReny5VpNNTdbbaaXk16iyPudk0xjZLoIJb/EyF1q4=; b=OOYif+ZMRuVWJtkZCPtl6stUw53hCHfJ8PKmYLkj8YzuXHwPx6mJ6/7Ufoeqc7bb1H znHGP+uAUPtqPW+/Dkcp0sM1i3suHR+yvSVSWCcC08Np5XIWkzczCBVq+wOLUWt/cwKb tb4t/7k16ZXaewz64JVhd7uDiXdAacXBcAnqU/opD/bPj5pwJEOhemqwXVuq7Igj2hge enr6UxSRF78/W3YefIggVNHSQVqU5GVPwvxD37Qt6nmNegKOcuhx5xTQeDm1+qfd7W17 lvnKuxXlhfaceiwDK+qaIUcH4X74QplfT2uImGMO0Qfc6Gxqvd4zTVDQhujKzBAjkSlW iwRA== 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=KRReny5VpNNTdbbaaXk16iyPudk0xjZLoIJb/EyF1q4=; b=ECDHAVfCTeFxl1/2eaIlvjFY2NU541Qd2YhzkaDw1RUGQS1VwbO/FqITkFgOvWEAtD 7qJQbSkEKbKhyFsjQbYlKbKdCWPb36KgPInwE85UggBZ1cTrzHHDD2lWpUSf922I8wrF 0sef9d3kFg0WKS7nj/a50PNzfH6HjrWLBJXGzd/gFK/2QEFTZebqj0la2x4KjsTZjMSN SRkWhtWZPdE62Aky8uCxHsSHpGoSf+dS5Y3TMD3zb9SSjcjr00b6/Isk5lNvHHZJ2tDW N8O+kSjXAP3Jqt9jbBEwwgB3DFkt0m8GrTPwQS21QDv9A+M42N09ijr5jkzegnEt9CWA 4AhQ== X-Gm-Message-State: ABuFfojYc4OfoLfowIprSzWN1bEappf9I+64fZjf3HhZs4KtDVCHp+V/ iDSjgsjckUFgunCrFy36Zpg= X-Received: by 2002:a50:d9c6:: with SMTP id x6-v6mr2650063edj.63.1537526166941; Fri, 21 Sep 2018 03:36:06 -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 k28-v6sm1900871ede.11.2018.09.21.03.36.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 03:36:05 -0700 (PDT) From: Ricardo Ribalda Delgado To: Linus Walleij , Timur Tabi , swboyd@chromium.org, linux-gpio@vger.kernel.org, LKML Cc: Ricardo Ribalda Delgado Subject: [PATCH] gpiolib: Fix gpio_direction_* for single direction GPIOs Date: Fri, 21 Sep 2018 12:36:03 +0200 Message-Id: <20180921103604.13361-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 a57300c1d649..4b45de883ada 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2512,19 +2512,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); @@ -2546,16 +2554,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