From patchwork Thu Feb 11 08:47:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 380893 Delivered-To: patch@linaro.org Received: by 2002:a02:b18a:0:0:0:0:0 with SMTP id t10csp1885327jah; Thu, 11 Feb 2021 00:49:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUXEkzEuf/3pwod/ADqwGQgtSLu3HSEkToEtPT2n062qoK1aOUZl2CQI3RRo3Cz64GIPV+ X-Received: by 2002:a17:906:547:: with SMTP id k7mr7361786eja.273.1613033354216; Thu, 11 Feb 2021 00:49:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613033354; cv=none; d=google.com; s=arc-20160816; b=d0NRmNRq7dOZf+ZXYZuDjJL1SZ4xoZCOduRZwgwE6Zxg0C8R6xsv2KK/CrANCeLdgK OhYZlOBQQlCu1+w0LmGNZDdaJWlSCpAe6iPIGXrLb2NXKpfDyhLlAusZc7M2kWiCvyHb b9c4d4izf8gd+O/NHaakcEhEaZbr7eDTU6ZeEtC6puJJbpBYxiObSzCNzF2swbTV3sw/ iUNV0gksok09dxoePokMHZPvM7cexIML+EOeGMXngmNUjeH3nlpakPW4ezX9dJQU5sx2 cuoQHxLPnzSh819BTY+tX+gtRLjwbw+LNdyTwLeXA4+0XEJn07UDoURBluWr1+F5HglU fv0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:references:cms-type:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-filter; bh=oQfjYXHghB4xbJZAiJsoSIqJafJmtfhm4dNf5R1O3pU=; b=QBbWDpQVChFFW/ZIlpX09pYjSvDPwGlwSVaJiVobaXPoaof1rij3UViTHn0UGWxIil W++i0qpBLktucDHRMSMxoZdpRhVvHBWihOyxgsRdQL3Y9pCl6VgBCbg9gmJqONCNK9iu kDLK38reNxaZJB+Ep3CwSVmVKphlkoSzQkM+IpMhTfiqSsdB8wZi/kXbwdfQeQYhSdfl ARnwy9lujQiYEo27HhiqaSpCXIxpPiZZtvvYCOJtL1WHCqUB+8pFBLoNXqskIMs1xuqf uAjtG1zbHIxsa6FpeA5ydH153a9iJuEBFZWgeb+66DT3k4FemZEiraDuUDDBb5XI5Oet Lgvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=IU8zgorh; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id f5si3107041ejd.104.2021.02.11.00.49.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Feb 2021 00:49:14 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=IU8zgorh; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0EED1826D4; Thu, 11 Feb 2021 09:48:32 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=samsung.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=samsung.com header.i=@samsung.com header.b="IU8zgorh"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 840B88268D; Thu, 11 Feb 2021 09:48:16 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mailout1.w1.samsung.com (mailout1.w1.samsung.com [210.118.77.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1239082604 for ; Thu, 11 Feb 2021 09:48:06 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=samsung.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=m.szyprowski@samsung.com Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20210211084806euoutp01c9af1e436a9e33221b9fbcc5ea797626~ipTpQJg9e1444414444euoutp01k for ; Thu, 11 Feb 2021 08:48:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20210211084806euoutp01c9af1e436a9e33221b9fbcc5ea797626~ipTpQJg9e1444414444euoutp01k DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1613033286; bh=oQfjYXHghB4xbJZAiJsoSIqJafJmtfhm4dNf5R1O3pU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IU8zgorhFW2mHdXHxedD+ByEcB13mhujrbAbV0mrPhIenHznktHpuRlQvsT6oqj+4 uIc2DzBU3YRzfePWJ5JgYZMIIlGpQkWhRHFNFTaJz3R1VbNvKm310j/tk2NA2TSe3M uh8T2ju9rOX06EpAiIPELee9/IO58yDVFCrj1r98= Received: from eusmges1new.samsung.com (unknown [203.254.199.242]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20210211084806eucas1p16def12e6390419e660be0b7bf79039a1~ipTo_ksAX1433514335eucas1p1p; Thu, 11 Feb 2021 08:48:06 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges1new.samsung.com (EUCPMTA) with SMTP id 1D.11.27958.64FE4206; Thu, 11 Feb 2021 08:48:06 +0000 (GMT) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20210211084805eucas1p19dd298b9466769d0ea8827b6d239d4d8~ipTopI0by1433514335eucas1p1o; Thu, 11 Feb 2021 08:48:05 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20210211084805eusmtrp20afeb14ea39b80c9c75fdb402e2fdd14~ipTooXOJC2424724247eusmtrp2X; Thu, 11 Feb 2021 08:48:05 +0000 (GMT) X-AuditID: cbfec7f2-efdff70000006d36-9d-6024ef46b135 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 91.75.21957.54FE4206; Thu, 11 Feb 2021 08:48:05 +0000 (GMT) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20210211084805eusmtip136f0f30df8b9bb7d2900ee3062dd9b75~ipToJiKNa2024620246eusmtip1a; Thu, 11 Feb 2021 08:48:05 +0000 (GMT) From: Marek Szyprowski To: u-boot@lists.denx.de, u-boot-amlogic@groups.io Cc: Marek Szyprowski , Neil Armstrong , Lukasz Majewski , Philippe Reynes , Simon Glass , Heinrich Schuchardt , Jaehoon Chung , Bartlomiej Zolnierkiewicz Subject: [PATCH v6 2/5] button: add a simple Analog to Digital Converter device based button driver Date: Thu, 11 Feb 2021 09:47:54 +0100 Message-Id: <20210211084757.22759-3-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210211084757.22759-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupmleLIzCtJLcpLzFFi42LZduznOV239yoJBtN3M1tsnLGe1eLGrzZW i+fvLjNZrD1yl93iUF+0RcdhZYtvW7YxWkya8IvN4u3eTnaLw1M/MDpweby/0cruMbvhIovH vFknWDw+fIzzmN/5ncnj7J0djB59W1YxemxcdoA5gCOKyyYlNSezLLVI3y6BK+PzbOGCrxoV D17eYWxg7FHsYuTgkBAwkbj5zb+LkYtDSGAFo8Tv/R/YIZwvjBLHFp5ihXA+M0p0rD/J2MXI Cdax6/cDNojEckaJ5W+OMyO0LNvNAlLFJmAo0fW2iw3EFhEwkrjUtIMFpIhZ4A6TRNfkBrAi YYFUiZlHe9lBbBYBVYk5M16CNfAK2Eoc+jGVHWKdvMTqDQeYQWxOATuJfXvugN0kIXCAQ2LL wmPMEEUuEm+uNbFA2MISr45vgWqWkfi/cz4TREMzo8TDc2vZIZweRonLTTOgPrKWuHPuFxso PJgFNCXW79KHCDtKbDx2jB0STHwSN94KgoSZgcxJ26YzQ4R5JTrahCCq1SRmHV8Ht/bghUtQ p3lInJ91HBpcExkl5l58zjSBUX4WwrIFjIyrGMVTS4tz01OLDfNSy/WKE3OLS/PS9ZLzczcx AtPL6X/HP+1gnPvqo94hRiYOxkOMEhzMSiK8/K0qCUK8KYmVValF+fFFpTmpxYcYpTlYlMR5 V81eEy8kkJ5YkpqdmlqQWgSTZeLglGpgsrmgmFAe6mwm8qJr0Ybjahd31a5sft8xL+XkZm/L Pxs0O+t74yXebr7v2JQVEvDP1Vv4M/dNjUcRZRY8ctJN6UXRK0VXdmVpfJhnGH3pauldpYDb vdemTOVcqXHfJdJj2h4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p+ZlP2PLE5N/9N+xgcGK2r4v6CU64rZUPRS9dZnKrea/Yv Pbb6B4PQPwOj7HPlzMGv9KJnBypW7mVhctKZvb7NwkRAfs7RprN7Gl/96lSpkXm+8MZy9TW3 2Vi8tVytpZc2VP0QvMzFzNK7OOnHMo3lNpFFLY/8uZuVuX6auh7LOsWwwkaJpTgj0VCLuag4 EQDJmclT/wIAAA== X-CMS-MailID: 20210211084805eucas1p19dd298b9466769d0ea8827b6d239d4d8 X-Msg-Generator: CA X-RootMTR: 20210211084805eucas1p19dd298b9466769d0ea8827b6d239d4d8 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20210211084805eucas1p19dd298b9466769d0ea8827b6d239d4d8 References: <20210211084757.22759-1-m.szyprowski@samsung.com> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean Add a simple Analog to Digital Converter device based button driver. This driver binds to the 'adc-keys' device tree node. Signed-off-by: Marek Szyprowski --- drivers/button/Kconfig | 8 ++ drivers/button/Makefile | 1 + drivers/button/button-adc.c | 146 ++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 drivers/button/button-adc.c -- 2.17.1 Reviewed-by: Simon Glass diff --git a/drivers/button/Kconfig b/drivers/button/Kconfig index 6b3ec7e55d..6db3c5e93a 100644 --- a/drivers/button/Kconfig +++ b/drivers/button/Kconfig @@ -9,6 +9,14 @@ config BUTTON can provide access to board-specific buttons. Use of the device tree for configuration is encouraged. +config BUTTON_ADC + bool "Button adc" + depends on BUTTON + help + Enable support for buttons which are connected to Analog to Digital + Converter device. The ADC driver must use driver model. Buttons are + configured using the device tree. + config BUTTON_GPIO bool "Button gpio" depends on BUTTON diff --git a/drivers/button/Makefile b/drivers/button/Makefile index fcc10ebe8d..bbd18af149 100644 --- a/drivers/button/Makefile +++ b/drivers/button/Makefile @@ -3,4 +3,5 @@ # Copyright (C) 2020 Philippe Reynes obj-$(CONFIG_BUTTON) += button-uclass.o +obj-$(CONFIG_BUTTON_ADC) += button-adc.o obj-$(CONFIG_BUTTON_GPIO) += button-gpio.o diff --git a/drivers/button/button-adc.c b/drivers/button/button-adc.c new file mode 100644 index 0000000000..eed86564fb --- /dev/null +++ b/drivers/button/button-adc.c @@ -0,0 +1,146 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2021 Samsung Electronics Co., Ltd. + * http://www.samsung.com + * Author: Marek Szyprowski + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * struct button_adc_priv - private data for button-adc driver. + * + * @adc: Analog to Digital Converter device to which button is connected. + * @channel: channel of the ADC device to probe the button state. + * @min: minimal uV value to consider button as pressed. + * @max: maximal uV value to consider button as pressed. + */ +struct button_adc_priv { + struct udevice *adc; + int channel; + int min; + int max; +}; + +static enum button_state_t button_adc_get_state(struct udevice *dev) +{ + struct button_adc_priv *priv = dev_get_priv(dev); + unsigned int val; + int ret, uV; + + ret = adc_start_channel(priv->adc, priv->channel); + if (ret) + return ret; + + ret = adc_channel_data(priv->adc, priv->channel, &val); + if (ret) + return ret; + + ret = adc_raw_to_uV(priv->adc, val, &uV); + if (ret) + return ret; + + return (uV >= priv->min && uV < priv->max) ? BUTTON_ON : BUTTON_OFF; +} + +static int button_adc_of_to_plat(struct udevice *dev) +{ + struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev); + struct button_adc_priv *priv = dev_get_priv(dev); + struct ofnode_phandle_args args; + u32 treshold, up_treshold, t; + ofnode node; + int ret; + + /* Ignore the top-level button node */ + if (!uc_plat->label) + return 0; + + ret = dev_read_phandle_with_args(dev->parent, "io-channels", + "#io-channel-cells", 0, 0, &args); + if (ret) + return ret; + + ret = uclass_get_device_by_ofnode(UCLASS_ADC, args.node, &priv->adc); + if (ret) + return ret; + + ret = ofnode_read_u32(dev_ofnode(dev->parent), + "keyup-threshold-microvolt", &up_treshold); + if (ret) + return ret; + + ret = ofnode_read_u32(dev_ofnode(dev), "press-threshold-microvolt", + &treshold); + if (ret) + return ret; + + dev_for_each_subnode(node, dev->parent) { + ret = ofnode_read_u32(node, "press-threshold-microvolt", &t); + if (ret) + return ret; + + if (t > treshold) + up_treshold = t; + } + + priv->channel = args.args[0]; + priv->min = treshold; + priv->max = up_treshold; + + return ret; +} + +static int button_adc_bind(struct udevice *parent) +{ + struct udevice *dev; + ofnode node; + int ret; + + dev_for_each_subnode(node, parent) { + struct button_uc_plat *uc_plat; + const char *label; + + label = ofnode_read_string(node, "label"); + if (!label) { + debug("%s: node %s has no label\n", __func__, + ofnode_get_name(node)); + return -EINVAL; + } + ret = device_bind_driver_to_node(parent, "button_adc", + ofnode_get_name(node), + node, &dev); + if (ret) + return ret; + uc_plat = dev_get_uclass_plat(dev); + uc_plat->label = label; + } + + return 0; +} + +static const struct button_ops button_adc_ops = { + .get_state = button_adc_get_state, +}; + +static const struct udevice_id button_adc_ids[] = { + { .compatible = "adc-keys" }, + { } +}; + +U_BOOT_DRIVER(button_adc) = { + .name = "button_adc", + .id = UCLASS_BUTTON, + .of_match = button_adc_ids, + .ops = &button_adc_ops, + .priv_auto = sizeof(struct button_adc_priv), + .bind = button_adc_bind, + .of_to_plat = button_adc_of_to_plat, +};