From patchwork Tue Sep 25 07:55:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 147403 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp414827lji; Tue, 25 Sep 2018 00:55:38 -0700 (PDT) X-Google-Smtp-Source: ACcGV61HbTTocFJLxfSF/cfUgnt5EWxoRe1FNuFlpFD5iu7XyVMG2a4blZpsOU4tlKgEcjvoJvRw X-Received: by 2002:a17:902:e00d:: with SMTP id ca13-v6mr2306274plb.252.1537862138142; Tue, 25 Sep 2018 00:55:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537862138; cv=none; d=google.com; s=arc-20160816; b=F0Uo8Hsfu1sJ3Aw8j2buHvXA/8U02twIgSNojhYp613VxNV/te0Z4SMU8VVvse92mt yRi6v4gXgHrKjEnEtLrK0BpGAN/nLZCew58BoAAYfbADndGV8yfyn3pdYmXrKkynVEEI WoqXGUlSE1cCMy29/Y5iu/HCDPiDbII5cKc4ypR8jUjfLMadh/RUbWeDuRq885+tx8xi tm4Njm0ijxNjxJXBFjTX8uxRf1t/dB7+gf7WGbhxtE1kjQidrGKfd8YnfGYz2v6Jqs0p m5hT7rFh0A3gTKLzhXT/Q9OjV7+8wsoMsdvTPu0uZJD3ELNe39E4ZrdIv1LCytkLK/R2 +G0Q== 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=P8e1STqohbubRAeAejBLuxewoowoKIx5xWtWT+ttKhA=; b=mdk/WNW7PDdLNrJm7dmzimzSnheR/lG1wOO4ER4DnPDmGo/LRZzHpeam9bpxehds4Z FyTUgw/c0Pk0VNfOPd1yzi9xbsCmRgh60ceXPw5Ho4zdoU5t11/zJJwmRvBT0aZvAXZ/ 97x21LpLply5DB6uGaMH8r4zS4Dz1rOlox4R75RazkUYWYBh1f07KOMHbssuurdfNf/8 OFBH6siKOYE399SUw0HqIwcmPh8wCHgD3QQJCqzrS4ShuXn8uIP19C4qzeAhZGXDJDeb 0eLVfBQY/GIZ9Msd3RyxABEkhsVDpLK2cykbobo4+cFJZ3ukO99JgVlf99JGER9lTmj7 pFpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=S1ipiUIb; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e10-v6si1871734pln.161.2018.09.25.00.55.37; Tue, 25 Sep 2018 00:55:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-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=@linaro.org header.s=google header.b=S1ipiUIb; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726033AbeIYOBz (ORCPT + 5 others); Tue, 25 Sep 2018 10:01:55 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:35030 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726030AbeIYOBz (ORCPT ); Tue, 25 Sep 2018 10:01:55 -0400 Received: by mail-lj1-f195.google.com with SMTP id j25-v6so744053ljg.2 for ; Tue, 25 Sep 2018 00:55:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=P8e1STqohbubRAeAejBLuxewoowoKIx5xWtWT+ttKhA=; b=S1ipiUIbJivEwIr2zIjzxzx0Fm5KLTqwUKCHLHFVIGDOHZuhhjhg8f/Se3XybEpMX2 FSXUB05vqBdfdb9R2yxzAS7IKsNO/iyVCmpWxU+OxAjt2UL8Ee1UmuIZuMr2uSRDVdhB NTByM9sVzDl8JWZTctw/ITP525KeCeJRZFsBg= 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=P8e1STqohbubRAeAejBLuxewoowoKIx5xWtWT+ttKhA=; b=lK6oPPmtFpEukKaDFWxGAQt0iCYzY3CRwGQIcfo/eNWc8xiVf0btuD6bTZblCQO0Ff lyy7weUJBagHk2RnalZvbenHUEvMCofmTUDVhgNZPHbTMdspV+EDWvbrhct4sRJX4nch Iar5IvSPs82rq7vQn4l69gl9wOP4oGHI5XrkyQGBeMn2+ubLi8w/03EinEVJ9oFvS6gr 6tNg550hNqw9FBz6th4v8lD2TrjDrYfABIuu5REPRJ3qwh0U0/jLrXClqM5UQ88fVh/I LWQduwpWC+IKdXqZms7O0DdQ1s2mGd1gF3w28q4ZDz3Ja8QP5l3Q8w6D4d6TDJFuysCZ nXrg== X-Gm-Message-State: ABuFfojwIaSsrelP7MyOL59x4uA5qiDcYxdPNihNoPQBjBUWYL5a1xA3 t4WXxaaTOx2gGxSl4d1h+q1WOljp3ec= X-Received: by 2002:a2e:59db:: with SMTP id g88-v6mr1640480ljf.79.1537862134722; Tue, 25 Sep 2018 00:55:34 -0700 (PDT) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id m88-v6sm273896lje.14.2018.09.25.00.55.33 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 25 Sep 2018 00:55:33 -0700 (PDT) From: Linus Walleij To: linux-gpio@vger.kernel.org Cc: Linus Walleij , Ricardo Ribalda Delgado , Bartosz Golaszewski Subject: [PATCH] gpio: Add comments on single direction chips Date: Tue, 25 Sep 2018 09:55:31 +0200 Message-Id: <20180925075531.24246-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.17.1 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org A patch from Ricardo got me thinking about some gpio chip semantics so let's drop in some comments to make things more clear around that. Cc: Ricardo Ribalda Delgado Cc: Bartosz Golaszewski Signed-off-by: Linus Walleij --- drivers/gpio/gpiolib.c | 33 +++++++++++++++++++++++++++------ include/linux/gpio/driver.h | 6 +++++- 2 files changed, 32 insertions(+), 7 deletions(-) -- 2.17.1 diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index cabe1b460458..4ce402f9fc69 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2548,20 +2548,31 @@ int gpiod_direction_input(struct gpio_desc *desc) VALIDATE_DESC(desc); chip = desc->gdev->chip; + /* + * It is legal to have no .get() and .direction_input() specified if + * the chip is output-only, but you can't specify .direction_input() + * and not support the .get() operation, that doesn't make sense. + */ if (!chip->get && chip->direction_input) { gpiod_warn(desc, - "%s: missing get() and direction_input() operations\n", - __func__); + "%s: missing get() but have direction_input()\n", + __func__); return -EIO; } + /* + * If we have a .direction_input() callback, things are simple, + * just call it. Else we are some input-only chip so try to check the + * direction (if .get_direction() is supported) else we silently + * assume we are in input mode after this. + */ 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__); + "%s: missing direction_input() operation and line is output\n", + __func__); return -EIO; } if (status == 0) @@ -2587,16 +2598,22 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) int val = !!value; int ret = 0; + /* + * It's OK not to specify .direction_output() if the gpiochip is + * output-only, but if there is then not even a .set() operation it + * is pretty tricky to drive the output line. + */ if (!gc->set && !gc->direction_output) { gpiod_warn(desc, - "%s: missing set() and direction_output() operations\n", - __func__); + "%s: missing set() and direction_output() operations\n", + __func__); return -EIO; } if (gc->direction_output) { ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); } else { + /* Check that we are in output mode if we can */ if (gc->get_direction && gc->get_direction(gc, gpio_chip_hwgpio(desc))) { gpiod_warn(desc, @@ -2604,6 +2621,10 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) __func__); return -EIO; } + /* + * If we can't actively set the direction, we are some + * output-only chip, so just drive the output as desired. + */ gc->set(gc, gpio_chip_hwgpio(desc), val); } diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index d8dcd0e44cab..f6b95734073f 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -178,9 +178,13 @@ static inline struct gpio_irq_chip *to_gpio_irq_chip(struct irq_chip *chip) * @free: optional hook for chip-specific deactivation, such as * disabling module power and clock; may sleep * @get_direction: returns direction for signal "offset", 0=out, 1=in, - * (same as GPIOF_DIR_XXX), or negative error + * (same as GPIOF_DIR_XXX), or negative error. + * It is recommended to always implement this function, even on + * input-only or output-only gpio chips. * @direction_input: configures signal "offset" as input, or returns error + * This can be omitted on input-only or output-only gpio chips. * @direction_output: configures signal "offset" as output, or returns error + * This can be omitted on input-only or output-only gpio chips. * @get: returns value for signal "offset", 0=low, 1=high, or negative error * @get_multiple: reads values for multiple signals defined by "mask" and * stores them in "bits", returns 0 on success or negative error