From patchwork Thu Feb 18 10:33:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 384385 Delivered-To: patch@linaro.org Received: by 2002:a02:c80e:0:0:0:0:0 with SMTP id p14csp3559249jao; Thu, 18 Feb 2021 02:34:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyPpY3ifvfqWucecVHr5M18/++3s6rudaQ4r2ge0Ttwtmpf3j6nBQz0mWaNcR4mpjAVuOoN X-Received: by 2002:a05:6402:541:: with SMTP id i1mr3522924edx.36.1613644462506; Thu, 18 Feb 2021 02:34:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613644462; cv=none; d=google.com; s=arc-20160816; b=cdlJXyXr8GqhEkX2Pi2/n58HKo4Q0CxMp9CdIatUft7K7H7aIsHMhWTvt3bEfvvqNf +k3GyUfDpmZVgrCrwjCHIPyAmLQSP+y9jNnM1lWhl7VlUcWq2/2TU1I6xvKPDeH3j/c4 pzuzNcl6yTxrrcCY/ECxs4+wYJQ8ip2MFTTzn10+TX5yGVVXzSNRuTv5r447NpRpyjWf Ss02Bkmlcs108w5Xo9cAjbg247CGZi+EZDq4UaEnf9weEVXLgJqxxpTuBVfKWIzWg51a +quJOdsYlgomlG5xRp4MrFyuYtwOU81xllHTJOgdtYbK8vbr8S4IYDhQxrrUrIbSSOB5 mGPA== 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=rnt/EdI0agJZ1KatsV6fzxiwEiTIekgMqXPesRPDTyI=; b=F+pK5cBribw/h/ic534RcfjhBUtZieuFImSVLjUmnSlr4ZLWis2GOD7MZ2KUO7hI49 xbCJBBoBTp+P0ebbLnZFwIfCnfhWS6uv8iK9xaAtRf+uNxYqmmrqcXrqVR6WE60S+w3W /4x+9plzPacfzA4wKj+8Kh7RHk1mXeFRZO7mU26Vu3bWotmIgftO/I56YlC5nvexeIh9 nH6IATU7uqVOFsRsapmYPRVQAYMmhU3WD8cTUb3cEF41NJjj2F28o61OE1Rrz5DsPOVL xq527GzDe/a4oKqz42uNE8pDz8o/bIOS5Yetnshmio9/GWmG8zlxJzIx6fRLRAf/r9/i 8y9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b="I/h3s3PR"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id j10si3926735edj.128.2021.02.18.02.34.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Feb 2021 02:34:22 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b="I/h3s3PR"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 17E60826FB; Thu, 18 Feb 2021 11:34:11 +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="I/h3s3PR"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 45B09826D4; Thu, 18 Feb 2021 11:33:54 +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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,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 BA61882540 for ; Thu, 18 Feb 2021 11:33:44 +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 20210218103334euoutp01d8d9640ef5cde65f11ab16c89bff0ea8~k0QukUSGN1175211752euoutp01i for ; Thu, 18 Feb 2021 10:33:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20210218103334euoutp01d8d9640ef5cde65f11ab16c89bff0ea8~k0QukUSGN1175211752euoutp01i DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1613644414; bh=rnt/EdI0agJZ1KatsV6fzxiwEiTIekgMqXPesRPDTyI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I/h3s3PRB54AHh80uo0DVQDXNCasaU2wCrq+8EdDW3yKuDwsT5yd7l/l7TJE0I9fC UGy2vXVuv/WU0qRSs9lrec2gNKKPCvi7Tm4yZr9/K2UucZ7wWAC1JlBZK0Bvt47/hZ S+ORP3LMSdnrbGqkUmvTcTDsCuDh3eGzrI4GL9oI= Received: from eusmges1new.samsung.com (unknown [203.254.199.242]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20210218103329eucas1p146678e6e3a5aa984a30751ebe0f5ee27~k0QppDNyJ2707327073eucas1p1O; Thu, 18 Feb 2021 10:33:29 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges1new.samsung.com (EUCPMTA) with SMTP id 7D.69.27958.8724E206; Thu, 18 Feb 2021 10:33:29 +0000 (GMT) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20210218103328eucas1p2cade8a776557736d66c3d9b67ada64cb~k0QpHhJkL2030720307eucas1p23; Thu, 18 Feb 2021 10:33:28 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20210218103328eusmtrp242c19881fa08a539ec6c20de4f01da20~k0QpGJ46S2025120251eusmtrp2o; Thu, 18 Feb 2021 10:33:28 +0000 (GMT) X-AuditID: cbfec7f2-efdff70000006d36-c7-602e427849fa Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id D5.73.21957.8724E206; Thu, 18 Feb 2021 10:33:28 +0000 (GMT) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20210218103327eusmtip274ab80db964ed0d0fe920b3093349c7b~k0QokB8Kk0363403634eusmtip2j; Thu, 18 Feb 2021 10:33:27 +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 v7 2/5] button: add a simple Analog to Digital Converter device based button driver Date: Thu, 18 Feb 2021 11:33:15 +0100 Message-Id: <20210218103318.18915-3-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210218103318.18915-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupmleLIzCtJLcpLzFFi42LZduznOd1KJ70Eg2ZHi40z1rNa3PjVxmrx /N1lJou1R+6yWxzqi7boOKxs8W3LNkaLSRN+sVm83dvJbnF46gdGBy6P9zda2T1mN1xk8Zg3 6wSLx4ePcR7zO78zeZy9s4PRo2/LKkaPjcsOMAdwRHHZpKTmZJalFunbJXBlHFnwlLVgkWbF gRNTmBsYtyh2MXJySAiYSEy9/42xi5GLQ0hgBaPEj7vL2CGcL4wS67u7mCCcz4wSt2+uZoVp mdd5hgUisZxRYlFjKxNcy+vm42wgVWwChhJdb7vAbBEBI4lLTTvAOpgF7jBJdE1uYAFJCAuk SkyZsIUdxGYRUJXoam1kArF5BWwlDrRNYYNYJy+xesMBZhCbU8BO4snZ+SwQ8SMcEosPpUPY LhJXrm9ggrCFJV4dh5gpISAj8X/nfLDrJASaGSUenlvLDuH0MEpcbprBCFFlLXHn3C+gbRxA 52lKrN+lDxF2lNh//gEzSFhCgE/ixltBkDAzkDlp23SoMK9ER5sQRLWaxKzj6+DWHrxwiRnC 9pBY+GofNIAmMkocm/eZcQKj/CyEZQsYGVcxiqeWFuempxYb5qWW6xUn5haX5qXrJefnbmIE ppfT/45/2sE499VHvUOMTByMhxglOJiVRHjZP2slCPGmJFZWpRblxxeV5qQWH2KU5mBREudd NXtNvJBAemJJanZqakFqEUyWiYNTqoEpMPh9r8SsBc+qF1yXWDXt5EwO89LfHzhjwqY7zdj0 sqvoK6thUdXP6V7hsx7apk/kEw5bEKJdpX/v0kqOaRdMXBKCui9/nbrk0P1936eEXpHOuqj5 Nugvg6Jo/BeBoGV9n74ULVpkzLnkh8mfLjeVY48WLxf9u//astvVS3+v4mP0mWk8f8E2uUNK DJvld85c33xedIV8zJ+3QY+mLhRbNFnz9lpJpa8F/yftlgt4k3K36nAY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/u2PW99J5K ldGMMIeowOklbY/neKbN6f+b/195Q50dY8zeV1N0WX7IO5rbn13fsksj2I3n777LSizFGYmG WsxFxYkA9DM0dgADAAA= X-CMS-MailID: 20210218103328eucas1p2cade8a776557736d66c3d9b67ada64cb X-Msg-Generator: CA X-RootMTR: 20210218103328eucas1p2cade8a776557736d66c3d9b67ada64cb X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20210218103328eucas1p2cade8a776557736d66c3d9b67ada64cb References: <20210218103318.18915-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 Reviewed-by: Simon Glass --- 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 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, +};