From patchwork Tue Jan 26 09:50:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 370591 Delivered-To: patch@linaro.org Received: by 2002:a02:a60d:0:0:0:0:0 with SMTP id c13csp4802590jam; Tue, 26 Jan 2021 01:52:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJx1+lc2SxM7erPHnwlUNiTk+e8wi0FAN1PhyFgqfZ7nMR6E4Pg69PZdvhOyRwRdnYCE55aV X-Received: by 2002:aa7:c399:: with SMTP id k25mr4015949edq.305.1611654740126; Tue, 26 Jan 2021 01:52:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611654740; cv=none; d=google.com; s=arc-20160816; b=edWSm1r8vQ1oAqaEhTJm/IkKSRSKR2WOIEG8+afUQN6xcH8/zdNbbw2CQcgRcQzPJJ DH9IFMr7YDAsuV3+XQlTN3u3D4ZyrXRrB0XBcHbgELm3XrCQsCPAkYhVTGH0jhO11Sqb +nMLGqSIYdr39YQcMOq4vedbyaeIcSWM0O3yOXCuuqyMrh1dLGS7HBkKw7E/9eQAC3SU i/ptM7zcRvDhUlGKFP5tB4zF3AhWIodE9sEnCV16TZ84yyAqPUIPATqONuLH2NlbZiXb BvpTh9zevsjsBKLZHRhGPAYP0pLwqJvmOOr7/UJC/HvidpaLg7uN6Cmrx+SMd2b4mxVe qQpA== 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=mSTenG0qvtejMC6XKx66WSlcX9pHGUqeB9GA4T22ZBc=; b=i1+b7P4wKFsWvzEBIgmmSlBLQ1HWP60Twj6UWhdjcA1uYqddtX5kEbWhOG9+W/0fVG Cy22jMzLgQlzCELg49mYx+ZziCP7cEn199NdN8HCP1jr/BJb9p/VCpmYHip4NIXx3qqT WrOYYOJZnBlchcbMb+j6jD4IVQbkfG7hk6mvH5g+GYMgtWw1EQszLsXG26vTX5V2UEtI eho1mAiFcMtsgnP6JD54xZB7I1XKxxCLuOhW9T6s3BwMf2Zp2TgfMnxZ4LPupVbIJoID YXLGQ8tlFeWzX8/SFQj150saF7YCOW0IGaqq7wvSRT2WIT5fR+wPX2JpM8Ecgy+aps69 7rfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=X2yD8rkc; 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 lk20si6917260ejb.599.2021.01.26.01.52.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Jan 2021 01:52:20 -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=X2yD8rkc; 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 60E098268A; Tue, 26 Jan 2021 10:51:52 +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="X2yD8rkc"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E69D8825A6; Tue, 26 Jan 2021 10:51:29 +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.3 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 mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 00E2A825AE for ; Tue, 26 Jan 2021 10:51:13 +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 eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20210126095113euoutp0297b168b2f9fc4701a3e71a8655d50616~dv2Lr38UV3275432754euoutp02M for ; Tue, 26 Jan 2021 09:51:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20210126095113euoutp0297b168b2f9fc4701a3e71a8655d50616~dv2Lr38UV3275432754euoutp02M DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1611654673; bh=mSTenG0qvtejMC6XKx66WSlcX9pHGUqeB9GA4T22ZBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X2yD8rkcSVFyj7h4BRp+pnSjw1+mXCx4/GkP+AOaO0XqX2XgH/jzD2W3FqFTLUXl8 /z4E1kD9VK4bIwoIQ8Gym5Q0Adfxg/sM9J+V58Gk12Qp4vavFaxdnv9Sj2+JBvjjve 9p+QKE8xR35GEvdeakSzD/u9XsdNPYr3mRbk9tQ0= Received: from eusmges3new.samsung.com (unknown [203.254.199.245]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20210126095112eucas1p118e15e52cbd9069b8737eedaba7d1271~dv2LYXIdi2162521625eucas1p1x; Tue, 26 Jan 2021 09:51:12 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges3new.samsung.com (EUCPMTA) with SMTP id 7B.D2.45488.016EF006; Tue, 26 Jan 2021 09:51:12 +0000 (GMT) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20210126095112eucas1p1af8ac76eaede4baf2b0cfeac561a6a06~dv2LDq-li0290402904eucas1p1_; Tue, 26 Jan 2021 09:51:12 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20210126095112eusmtrp1875eac55cc2d784a4852c61fb752ea13~dv2LC9wnK0825108251eusmtrp1E; Tue, 26 Jan 2021 09:51:12 +0000 (GMT) X-AuditID: cbfec7f5-c77ff7000000b1b0-6c-600fe6104365 Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id FA.E7.16282.016EF006; Tue, 26 Jan 2021 09:51:12 +0000 (GMT) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20210126095112eusmtip2b7e44d96f9f4fdded388017f0f69fe0c~dv2KiNrCS0504205042eusmtip2G; Tue, 26 Jan 2021 09:51:11 +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 v5 2/4] button: add a simple Analog to Digital Converter device based button driver Date: Tue, 26 Jan 2021 10:50:34 +0100 Message-Id: <20210126095036.6429-3-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210126095036.6429-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA0WSbUhTYRTHee69267La7dN8smK2crEqFnr7YImhVL3g5B96cME28iLSm7K rmYZxNJatWwso5Qt863UnHNaukzTdFZapjmLstSoEEVJs6yklZW328u33/9/zv85h8ODoxKT IBBP1qUzep0mRS4UY677X3vXkaML1esvVSupugKngBrwGgXU2NQThHLcHRZRbnMcdapjJfWl 3gWoPItXSE22nBZRHRemwXYx/X7ghIi2GTwYfdnahdHTH+LpotOzCN0z1Ahoc30VoOvK29BY XCWOSGBSkg8y+rBItTiptOSZIG1wzSHrsTxgANVyE/DBIbkJXh26ITABMS4hKwE0lLUjvPgE YH5lO+DFDIC5Bhf4Gxl77BbxhQoAx282IVzhd+SiU8ixkNwATZOm3+xPKmF/diPGBVByCIGm 84Z5geNSkoE91zQcYmQwfPpiM9dOkBHQWdiK8LNk0F7bhnLsQ26DpYOVKPcMJLtw+LzhJMY3 RcOSwpE/y0nhRGe9iOdlsPt8LsYHcgB80+sQ8SIXwCfZBX8S4XCo1yvktkDJUOhsCuPtHfCM s1jE2ZD0gwOTizgbncc8Vz7K2wQ8ZZTw3auhtbPm39j2vn6UZxpOnfWg/K0sAFa9GAUWILP+ H1YMQBUIYDJYbSLDbtQxmQpWo2UzdImK/ana62D+u3T/6PzcCConPijcAMGBG0AclfsTzQpf tYRI0BzOYvSp+/QZKQzrBktxTB5ANDVU75OQiZp05gDDpDH6v1UE9wk0INnfHLVbwn+W1dsR S9+KVeFR8Q6jYsm9249ZK5jRhNRK7h/5KOveG/t8VqheUOrbZrWt9qqLW9KD4u89yB/etcuu Hg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LS rLo7hu1CHdYvjr0r9/HrPXm58/eR858dCm8+LhcPX+hwQ7n1is2CF6K7mYTNhTkzL/4s4p26 7Yvr5teCQUXroo/rH3mw3pnnteSb6nOx19We1B6OkI7Y9KwlKFy/8K/r/J/7fJ/mtudf3uY7 T6n8trPDWweRq8JiPx+/tZ8ctbJomv69yd+MeRrllrlv3Nm16dH0Z+u+20zY+2RqyzclluKM REMt5qLiRABmO4VL/wIAAA== X-CMS-MailID: 20210126095112eucas1p1af8ac76eaede4baf2b0cfeac561a6a06 X-Msg-Generator: CA X-RootMTR: 20210126095112eucas1p1af8ac76eaede4baf2b0cfeac561a6a06 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20210126095112eucas1p1af8ac76eaede4baf2b0cfeac561a6a06 References: <20210126095036.6429-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 | 156 ++++++++++++++++++++++++++++++++++++ 3 files changed, 165 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..1901d59a0e --- /dev/null +++ b/drivers/button/button-adc.c @@ -0,0 +1,156 @@ +// 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 raw ADC sample value to consider button as pressed. + * @max: maximal raw ADC sample 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; + + 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; + + if (ret == 0) + return (val >= priv->min && val < priv->max) ? + BUTTON_ON : BUTTON_OFF; + + return ret; +} + +static int button_adc_probe(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; + unsigned int mask; + ofnode node; + int ret, vdd; + + /* 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(dev_ofnode(dev), + "press-threshold-microvolt", &t); + if (ret) + return ret; + + if (t > treshold) + up_treshold = t; + } + + ret = adc_vdd_value(priv->adc, &vdd); + if (ret) + return ret; + + ret = adc_data_mask(priv->adc, &mask); + if (ret) + return ret; + + priv->channel = args.args[0]; + priv->min = mask * (treshold / 1000) / (vdd / 1000); + priv->max = mask * (up_treshold / 1000) / (vdd / 1000); + + 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, + .probe = button_adc_probe, +};