Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1143779pxb; Wed, 10 Feb 2021 00:46:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJwXFS+99gsooia2eH4QEUu+PZ3S/UhfXp+U/bqe5P7Q16fjX9LWHZvDctUikzBQHJGUfOJp X-Received: by 2002:a05:6402:104b:: with SMTP id e11mr2210166edu.367.1612946781999; Wed, 10 Feb 2021 00:46:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612946781; cv=none; d=google.com; s=arc-20160816; b=zQBDfzHZaCKxszKo0U05VuVkSKIGEJk/j+oox6IN1MNEHAvvLrdfaPBPRnyCXX7IIg +NGIaB4fkPXKZa6ePc6XG/6orpUSjRLQPt8DA9mg/KXqaZqY2sLDNLOMsQokjJZYpAFb Enu4t9erlLuxwfYB6N5OLE6F58R5VTTQ4PE5qj9Zptpel6umCFDQiYvSLIzGtH+Kw6hV l6eVSoxkQz/nxesjfogC6EwUtlhBKoHUBrzwprRElTiBDsfOi13dg0UK26w7755Hhs+5 YgeYbx8J0Jb0E1bLutr/8GYbpkFoSUQl/MVtxq6agxFx5bsB019wrT5ZmT6TKXoV1BK8 KnXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=2mK7YnMmK+qe+iHkzBCGOZFYLKrBvUMqrDRvVqkHIMk=; b=RZ1g42NI2mvsjuv/3G9gkwH5qbwco5Dubn7Z+J9kAUdpGk0WYK+M2hW7nbXclLIsQK OvsigvxDdWRhl+xxrkMlicZ9JeLuf6Qry1V+VYWCNAlIiZbOcY83CXGtNQKqeC1RXESY eYVfFeWpSOQUJn2UYLcDSS4PfVyvXMNQoXqCQdIFCPe32mKqbB5z5Jgzn8FhNyGRRZrJ nGWYgKj5IDXNtv1Zi/WEfWTAtLJZb+SoNny2fOMUST1Ybj1QSFKYyUhE1eqWXbmsmSqa rdpTcRnEGkbqhcL82twdAKcrIi1j+tZ5N80F6M5hGcle86k+aucwHpGRwx1k1d0OiOps jbnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kRXY56OG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b12si880333edz.497.2021.02.10.00.45.58; Wed, 10 Feb 2021 00:46:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kRXY56OG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229785AbhBJIcE (ORCPT + 99 others); Wed, 10 Feb 2021 03:32:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229636AbhBJIaj (ORCPT ); Wed, 10 Feb 2021 03:30:39 -0500 Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12231C061574 for ; Wed, 10 Feb 2021 00:29:59 -0800 (PST) Received: by mail-io1-xd30.google.com with SMTP id n2so1039283iom.7 for ; Wed, 10 Feb 2021 00:29:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2mK7YnMmK+qe+iHkzBCGOZFYLKrBvUMqrDRvVqkHIMk=; b=kRXY56OGfXgnarKojbs9Mc3fMH5td6OoFbmce7m00KPyzVT/s5kYTv8Ubf43UxTBOB cfkHoA03/coAxM8W7/SvkSjVRasJjzgaXtuHqfI9hIiHqOU5es+h4VBzPP+zyHluF9D/ H3t40kPxAKZna5joythv86hFALJdeYDbRoivg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2mK7YnMmK+qe+iHkzBCGOZFYLKrBvUMqrDRvVqkHIMk=; b=lHXVKOJ8mtwuzJQul3FT1aS99fcI0gfUDInMp9toDtXxZM9uuIU5potn+hB3MylW0J hffkPz7MA2ID7WV0udSmrDdXw+5F8i2jZNdnWV8YraoPE158fv+rdlApzQjlA5rUzmI7 AgxU22x+3/D5fp8FAo3++DJpfbSYgUbKDhnWV/PImRPJC4xT9Vmt/TIvHeBkuxdBOBtv dI+fdsoQFWtyLJoNWeXZh/Gc7CKxc5r7uHPwMYQR1H7fq2oHjaf9QGm/UsJz0CNLRerz 0UvhnTDghpqFMG1qZ8akAwQs7KGNDboMxMq4xQxb2PAHUrDqLIU5diO/abHcl2gl4g0U vpkg== X-Gm-Message-State: AOAM533KKc0l5ozGzs5Fb8bcwWGwKBFE854CsJfaP0o8ajK5bVk7XoS8 86QT7P1GLiuXXOhHlsvTMPw0A2Rd2vJ1pquX8GJX1A== X-Received: by 2002:a02:2a01:: with SMTP id w1mr2114826jaw.19.1612945796038; Wed, 10 Feb 2021 00:29:56 -0800 (PST) MIME-Version: 1.0 References: <20210210025142.3609708-1-swboyd@chromium.org> <20210210025142.3609708-4-swboyd@chromium.org> In-Reply-To: <20210210025142.3609708-4-swboyd@chromium.org> From: Gwendal Grignou Date: Wed, 10 Feb 2021 00:29:45 -0800 Message-ID: Subject: Re: [PATCH v5 3/3] iio: proximity: Add a ChromeOS EC MKBP proximity driver To: Stephen Boyd Cc: Jonathan Cameron , linux-kernel , linux-iio , Dmitry Torokhov , Benson Leung , Guenter Roeck , Douglas Anderson , Enric Balletbo i Serra Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 9, 2021 at 6:51 PM Stephen Boyd wrote: > > Add support for a ChromeOS EC proximity driver that exposes a "front" > proximity sensor via the IIO subsystem. The EC decides when front > proximity is near and sets an MKBP switch 'EC_MKBP_FRONT_PROXIMITY' to > notify the kernel of proximity. Similarly, when proximity detects > something far away it sets the switch bit to 0. For now this driver > exposes a single sensor, but it could be expanded in the future via more > MKBP bits if desired. > > Cc: Dmitry Torokhov > Cc: Benson Leung > Cc: Guenter Roeck > Cc: Douglas Anderson > Cc: Gwendal Grignou > Reviewed-by: Enric Balletbo i Serra > Signed-off-by: Stephen Boyd > --- > > Changes from v4: > * Dropped of_match_ptr() > > drivers/iio/proximity/Kconfig | 11 + > drivers/iio/proximity/Makefile | 1 + > .../iio/proximity/cros_ec_mkbp_proximity.c | 242 ++++++++++++++++++ > 3 files changed, 254 insertions(+) > create mode 100644 drivers/iio/proximity/cros_ec_mkbp_proximity.c > > diff --git a/drivers/iio/proximity/Kconfig b/drivers/iio/proximity/Kconfig > index 12672a0e89ed..7c7203ca3ac6 100644 > --- a/drivers/iio/proximity/Kconfig > +++ b/drivers/iio/proximity/Kconfig > @@ -21,6 +21,17 @@ endmenu > > menu "Proximity and distance sensors" > > +config CROS_EC_MKBP_PROXIMITY > + tristate "ChromeOS EC MKBP Proximity sensor" > + depends on CROS_EC > + help > + Say Y here to enable the proximity sensor implemented via the ChromeOS EC MKBP > + switches protocol. You must enable one bus option (CROS_EC_I2C or CROS_EC_SPI) > + to use this. > + > + To compile this driver as a module, choose M here: the > + module will be called cros_ec_mkbp_proximity. > + > config ISL29501 > tristate "Intersil ISL29501 Time Of Flight sensor" > depends on I2C > diff --git a/drivers/iio/proximity/Makefile b/drivers/iio/proximity/Makefile > index 9c1aca1a8b79..cbdac09433eb 100644 > --- a/drivers/iio/proximity/Makefile > +++ b/drivers/iio/proximity/Makefile > @@ -5,6 +5,7 @@ > > # When adding new entries keep the list in alphabetical order > obj-$(CONFIG_AS3935) += as3935.o > +obj-$(CONFIG_CROS_EC_MKBP_PROXIMITY) += cros_ec_mkbp_proximity.o > obj-$(CONFIG_ISL29501) += isl29501.o > obj-$(CONFIG_LIDAR_LITE_V2) += pulsedlight-lidar-lite-v2.o > obj-$(CONFIG_MB1232) += mb1232.o > diff --git a/drivers/iio/proximity/cros_ec_mkbp_proximity.c b/drivers/iio/proximity/cros_ec_mkbp_proximity.c > new file mode 100644 > index 000000000000..2cdaf05c0ec2 > --- /dev/null > +++ b/drivers/iio/proximity/cros_ec_mkbp_proximity.c > @@ -0,0 +1,242 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Driver for cros-ec proximity sensor exposed through MKBP switch > + * > + * Copyright 2021 Google LLC. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > +#include > + > +#include > +#include > +#include > + > +#include > + > +struct cros_ec_mkbp_proximity_data { > + struct cros_ec_device *ec; > + struct iio_dev *indio_dev; > + struct mutex lock; > + struct notifier_block notifier; > + bool enabled; > +}; > + > +static const struct iio_event_spec cros_ec_mkbp_proximity_events[] = { > + { > + .type = IIO_EV_TYPE_THRESH, > + .dir = IIO_EV_DIR_EITHER, > + .mask_separate = BIT(IIO_EV_INFO_ENABLE), > + }, > +}; > + > +static const struct iio_chan_spec cros_ec_mkbp_proximity_chan_spec[] = { > + { > + .type = IIO_PROXIMITY, > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), > + .event_spec = cros_ec_mkbp_proximity_events, > + .num_event_specs = ARRAY_SIZE(cros_ec_mkbp_proximity_events), > + }, > +}; > + > +static int cros_ec_mkbp_proximity_parse_state(const void *data) > +{ > + u32 switches = get_unaligned_le32(data); > + > + return !!(switches & BIT(EC_MKBP_FRONT_PROXIMITY)); > +} > + > +static int cros_ec_mkbp_proximity_query(struct cros_ec_device *ec_dev, > + int *state) > +{ > + struct { > + struct cros_ec_command msg; > + union { > + struct ec_params_mkbp_info params; > + u32 switches; > + }; > + } __packed buf = { }; > + struct ec_params_mkbp_info *params = &buf.params; > + struct cros_ec_command *msg = &buf.msg; > + u32 *switches = &buf.switches; > + size_t insize = sizeof(*switches); > + int ret; > + > + msg->command = EC_CMD_MKBP_INFO; > + msg->version = 1; > + msg->outsize = sizeof(*params); > + msg->insize = insize; > + > + params->info_type = EC_MKBP_INFO_CURRENT; > + params->event_type = EC_MKBP_EVENT_SWITCH; > + > + ret = cros_ec_cmd_xfer_status(ec_dev, msg); > + if (ret < 0) > + return ret; > + > + if (ret != insize) { > + dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n", ret, > + insize); > + return -EPROTO; > + } > + > + *state = cros_ec_mkbp_proximity_parse_state(switches); > + return IIO_VAL_INT; > +} > + > +static int cros_ec_mkbp_proximity_notify(struct notifier_block *nb, > + unsigned long queued_during_suspend, > + void *_ec) > +{ > + struct cros_ec_mkbp_proximity_data *data; > + struct cros_ec_device *ec = _ec; > + u8 event_type = ec->event_data.event_type & EC_MKBP_EVENT_TYPE_MASK; > + void *switches = &ec->event_data.data.switches; > + struct iio_dev *indio_dev; > + s64 timestamp; > + int state, dir; > + u64 ev; > + > + if (event_type == EC_MKBP_EVENT_SWITCH) { > + data = container_of(nb, struct cros_ec_mkbp_proximity_data, > + notifier); > + indio_dev = data->indio_dev; > + > + mutex_lock(&data->lock); > + if (data->enabled) { > + timestamp = ktime_to_ns(ec->last_event_time); Note to self, ktime_to_ns is a noop, but make code cleaner: need to change other access to ec->last_event_time. > + if (iio_device_get_clock(indio_dev) != CLOCK_BOOTTIME) > + timestamp = iio_get_time_ns(indio_dev); > + state = cros_ec_mkbp_proximity_parse_state(switches); There can be several switches in the EC (lid open, tablet mode, ...), so you can get a switch event even when the proximity switch did not trigger. You can keep the current state and push an iio event only when there is a change. See cbas_ec_notify(). > + dir = state ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; > + > + ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0, > + IIO_EV_TYPE_THRESH, dir); > + iio_push_event(indio_dev, ev, timestamp); > + } > + mutex_unlock(&data->lock); > + } > + > + return NOTIFY_OK; > +} > + > +static int cros_ec_mkbp_proximity_read_raw(struct iio_dev *indio_dev, > + const struct iio_chan_spec *chan, int *val, > + int *val2, long mask) > +{ > + struct cros_ec_mkbp_proximity_data *data = iio_priv(indio_dev); > + struct cros_ec_device *ec = data->ec; > + > + if (chan->type != IIO_PROXIMITY) > + return -EINVAL; > + > + switch (mask) { A switch is not necessary here. > + case IIO_CHAN_INFO_RAW: > + return cros_ec_mkbp_proximity_query(ec, val); > + } > + > + return -EINVAL; > +} > + > +static int cros_ec_mkbp_proximity_read_event_config(struct iio_dev *indio_dev, > + const struct iio_chan_spec *chan, > + enum iio_event_type type, > + enum iio_event_direction dir) > +{ > + struct cros_ec_mkbp_proximity_data *data = iio_priv(indio_dev); > + > + return data->enabled; > +} > + > +static int cros_ec_mkbp_proximity_write_event_config(struct iio_dev *indio_dev, > + const struct iio_chan_spec *chan, > + enum iio_event_type type, > + enum iio_event_direction dir, int state) > +{ > + struct cros_ec_mkbp_proximity_data *data = iio_priv(indio_dev); > + > + mutex_lock(&data->lock); > + data->enabled = state; > + mutex_unlock(&data->lock); > + > + return 0; > +} > + > +static const struct iio_info cros_ec_mkbp_proximity_info = { > + .read_raw = cros_ec_mkbp_proximity_read_raw, > + .read_event_config = cros_ec_mkbp_proximity_read_event_config, > + .write_event_config = cros_ec_mkbp_proximity_write_event_config, > +}; > + > +static int cros_ec_mkbp_proximity_probe(struct platform_device *pdev) > +{ > + struct device *dev = &pdev->dev; > + struct cros_ec_device *ec = dev_get_drvdata(dev->parent); > + struct iio_dev *indio_dev; > + struct cros_ec_mkbp_proximity_data *data; > + int ret; > + > + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); > + if (!indio_dev) > + return -ENOMEM; > + > + data = iio_priv(indio_dev); > + data->ec = ec; > + data->indio_dev = indio_dev; > + mutex_init(&data->lock); > + platform_set_drvdata(pdev, data); > + > + indio_dev->name = dev->driver->name; > + indio_dev->info = &cros_ec_mkbp_proximity_info; > + indio_dev->modes = INDIO_DIRECT_MODE; > + indio_dev->channels = cros_ec_mkbp_proximity_chan_spec; > + indio_dev->num_channels = ARRAY_SIZE(cros_ec_mkbp_proximity_chan_spec); > + > + ret = devm_iio_device_register(dev, indio_dev); > + if (ret) > + return ret; > + > + data->notifier.notifier_call = cros_ec_mkbp_proximity_notify; > + blocking_notifier_chain_register(&ec->event_notifier, &data->notifier); > + > + return 0; > +} > + > +static int cros_ec_mkbp_proximity_remove(struct platform_device *pdev) > +{ > + struct cros_ec_mkbp_proximity_data *data = platform_get_drvdata(pdev); > + struct cros_ec_device *ec = data->ec; > + > + blocking_notifier_chain_unregister(&ec->event_notifier, > + &data->notifier); > + > + return 0; > +} > + > +static const struct of_device_id cros_ec_mkbp_proximity_of_match[] = { > + { .compatible = "google,cros-ec-mkbp-proximity" }, > + {} > +}; > +MODULE_DEVICE_TABLE(of, cros_ec_mkbp_proximity_of_match); > + > +static struct platform_driver cros_ec_mkbp_proximity_driver = { > + .driver = { > + .name = "cros-ec-mkbp-proximity", > + .of_match_table = cros_ec_mkbp_proximity_of_match, > + }, > + .probe = cros_ec_mkbp_proximity_probe, > + .remove = cros_ec_mkbp_proximity_remove, > +}; > +module_platform_driver(cros_ec_mkbp_proximity_driver); > + > +MODULE_LICENSE("GPL v2"); > +MODULE_DESCRIPTION("ChromeOS EC MKBP proximity sensor driver"); > -- > https://chromeos.dev >