From nobody Mon Jun 29 16:47:03 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90C84C433EF for ; Mon, 7 Feb 2022 01:37:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347526AbiBGBhT (ORCPT ); Sun, 6 Feb 2022 20:37:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235825AbiBGBhR (ORCPT ); Sun, 6 Feb 2022 20:37:17 -0500 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2227C043181 for ; Sun, 6 Feb 2022 17:37:15 -0800 (PST) Received: by mail-pf1-x42d.google.com with SMTP id y5so9422758pfe.4 for ; Sun, 06 Feb 2022 17:37:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0JGsyH4tMLUxg3vmE0xafngwBtfMpX5tDrAVYU7yt6E=; b=T7eCSHTPoHRyIEiJsdSmjuegaHoZOzHSNgpvJEcpGoJxJRSXiDzHEKNivqFGKF6FnK JNnAL+Yb/JP4w9mtaJ7IkzLKoJS47AIpzDvzWr3l3ER8fi698fesjaw9PlfWiUZkpbuI qFha2ZyuAPlcvH1qlrq8hzfNwY4bBE9vJnNcg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0JGsyH4tMLUxg3vmE0xafngwBtfMpX5tDrAVYU7yt6E=; b=zJITf6yK26kNOzEQHuoLBhHQbwRYskFryYSZUt5i1pzNfw14wrvUrh/vm1z1z8zke5 kiy7jFXB3f+mrD5V2wj0EZHgaZSgfEba9H1p3pX84F8xdfwE1ZDKWqf+xfrXcdL5d9Cc xLLCNBSMHIUFgHIG/mNkTUlKlJY6W0NiwZQBtduv0gh64VNYp1ZEagW8R2Ho/lRZE0Ie xaWkJda0f3ko6/rmrOtYd1s3hP4AoMvB1+B05JuKJK/rvXLtacOPStWsC5PcVCta65zQ IBzA9NoP/yyQepJurnkjiKjun651pwQM0L+7q1B7UCAGJw30xO3BbKpAW4HLk3iVT45H 4rIw== X-Gm-Message-State: AOAM530YyOydeDrr09icJ5obFdTDURt+/kU5WKNdo6WEeuDjaeMTMFID RclxE4rTC4HJ+e+TE57QsUGzZ4IYeaRPvQ== X-Google-Smtp-Source: ABdhPJwgVYvFurjSq+ltd/0xzgn0J9I0Fh1egPC9bP6T3rjd97Py64bhTptAv0/8aoQcjsvYDYvnRQ== X-Received: by 2002:a63:fc64:: with SMTP id r36mr7663787pgk.529.1644197834864; Sun, 06 Feb 2022 17:37:14 -0800 (PST) Received: from localhost ([2401:fa00:9:14:7544:a15c:37e1:f797]) by smtp.gmail.com with UTF8SMTPSA id 7sm3071616pjo.45.2022.02.06.17.37.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 06 Feb 2022 17:37:14 -0800 (PST) From: =?UTF-8?q?Sami=20Ky=C3=B6stil=C3=A4?= To: LKML Cc: bleung@chromium.org, arnd@arndb.de, gregkh@linuxfoundation.org, evanbenn@chromium.org, =?UTF-8?q?Sami=20Ky=C3=B6stil=C3=A4?= Subject: [PATCH v3 1/1] platform/chrome: add a driver for HPS Date: Mon, 7 Feb 2022 12:36:13 +1100 Message-Id: <20220207013613.1150783-2-skyostil@chromium.org> X-Mailer: git-send-email 2.35.0.263.gb82422642f-goog In-Reply-To: <20220207013613.1150783-1-skyostil@chromium.org> References: <20220207013613.1150783-1-skyostil@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch introduces a driver for the ChromeOS screen privacy sensor (aka. HPS). The driver supports a sensor connected to the I2C bus and identified as "GOOG0020" in the ACPI tables. When loaded, the driver exports the sensor to userspace through a character device. This device only supports power management, i.e., communication with the sensor must be done through regular I2C transmissions from userspace. Power management is implemented by enabling the respective power GPIO while at least one userspace process holds an open fd on the character device. By default, the device is powered down if there are no active clients. Note that the driver makes no effort to preserve the state of the sensor between power down and power up events. Userspace is responsible for reinitializing any needed state once power has been restored. The device firmware, I2C protocol and other documentation is available at https://chromium.googlesource.com/chromiumos/platform/hps-firmware. Signed-off-by: Sami Ky=C3=B6stil=C3=A4 --- Changes in v3: - Moved from drivers/misc to drivers/platform/chrome. Changes in v2: - Removed custom ioctl interface. - Reworked to use miscdev. MAINTAINERS | 6 + drivers/platform/chrome/Kconfig | 10 ++ drivers/platform/chrome/Makefile | 1 + drivers/platform/chrome/cros_hps_i2c.c | 184 +++++++++++++++++++++++++ 4 files changed, 201 insertions(+) create mode 100644 drivers/platform/chrome/cros_hps_i2c.c diff --git a/MAINTAINERS b/MAINTAINERS index 69a2935daf6c..94a8c178598e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8800,6 +8800,12 @@ S: Maintained W: http://artax.karlin.mff.cuni.cz/~mikulas/vyplody/hpfs/index-e.cgi F: fs/hpfs/ =20 +HPS (ChromeOS screen privacy sensor) DRIVER +M: Sami Ky=C3=B6stil=C3=A4 +R: Evan Benn +S: Maintained +F: drivers/platform/chrome/cros_hps_i2c.c + HSI SUBSYSTEM M: Sebastian Reichel S: Maintained diff --git a/drivers/platform/chrome/Kconfig b/drivers/platform/chrome/Kcon= fig index ccc23d8686e8..a20da7b638df 100644 --- a/drivers/platform/chrome/Kconfig +++ b/drivers/platform/chrome/Kconfig @@ -217,6 +217,16 @@ config CROS_EC_TYPEC To compile this driver as a module, choose M here: the module will be called cros_ec_typec. =20 +config CROS_HPS_I2C + tristate "ChromeOS HPS device" + depends on HID && I2C && PM + help + Say Y here if you want to enable support for the ChromeOS + screen privacy sensor (HPS), attached via I2C. The driver supports a + sensor connected to the I2C bus and exposes it as a character device. + To save power, the sensor is automatically powered down when no + clients are accessing it. + config CROS_USBPD_LOGGER tristate "Logging driver for USB PD charger" depends on CHARGER_CROS_USBPD diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Mak= efile index f901d2e43166..66bd745d5931 100644 --- a/drivers/platform/chrome/Makefile +++ b/drivers/platform/chrome/Makefile @@ -23,6 +23,7 @@ obj-$(CONFIG_CROS_EC_DEBUGFS) +=3D cros_ec_debugfs.o cros-ec-sensorhub-objs :=3D cros_ec_sensorhub.o cros_ec_sensorhub_ring.o= cros_ec_trace.o obj-$(CONFIG_CROS_EC_SENSORHUB) +=3D cros-ec-sensorhub.o obj-$(CONFIG_CROS_EC_SYSFS) +=3D cros_ec_sysfs.o +obj-$(CONFIG_CROS_HPS_I2C) +=3D cros_hps_i2c.o obj-$(CONFIG_CROS_USBPD_LOGGER) +=3D cros_usbpd_logger.o obj-$(CONFIG_CROS_USBPD_NOTIFY) +=3D cros_usbpd_notify.o =20 diff --git a/drivers/platform/chrome/cros_hps_i2c.c b/drivers/platform/chro= me/cros_hps_i2c.c new file mode 100644 index 000000000000..9071c9324fa7 --- /dev/null +++ b/drivers/platform/chrome/cros_hps_i2c.c @@ -0,0 +1,184 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Driver for the ChromeOS screen privacy sensor (HPS), attached via I2C. + * + * The driver exposes HPS as a character device, although currently no rea= d or + * write operations are supported. Instead, the driver only controls the p= ower + * state of the sensor, keeping it on only while userspace holds an open f= ile + * descriptor to the HPS device. + * + * Copyright 2022 Google LLC. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define HPS_ACPI_ID "GOOG0020" + +struct hps_drvdata { + struct i2c_client *client; + struct miscdevice misc_device; + struct gpio_desc *enable_gpio; +}; + +static void hps_set_power(struct hps_drvdata *hps, bool state) +{ + if (!IS_ERR_OR_NULL(hps->enable_gpio)) + gpiod_set_value_cansleep(hps->enable_gpio, state); +} + +static void hps_unload(void *drv_data) +{ + struct hps_drvdata *hps =3D drv_data; + + hps_set_power(hps, true); +} + +static int hps_open(struct inode *inode, struct file *file) +{ + struct hps_drvdata *hps =3D container_of(file->private_data, + struct hps_drvdata, misc_device); + struct device *dev =3D &hps->client->dev; + int ret; + + ret =3D pm_runtime_get_sync(dev); + if (ret < 0) + goto pm_get_fail; + return 0; + +pm_get_fail: + pm_runtime_put(dev); + pm_runtime_disable(dev); + return ret; +} + +static int hps_release(struct inode *inode, struct file *file) +{ + struct hps_drvdata *hps =3D container_of(file->private_data, + struct hps_drvdata, misc_device); + struct device *dev =3D &hps->client->dev; + int ret; + + ret =3D pm_runtime_put(dev); + if (ret < 0) + goto pm_put_fail; + return 0; + +pm_put_fail: + pm_runtime_disable(dev); + return ret; +} + +const struct file_operations hps_fops =3D { + .owner =3D THIS_MODULE, + .open =3D hps_open, + .release =3D hps_release, +}; + +static int hps_i2c_probe(struct i2c_client *client) +{ + struct hps_drvdata *hps; + int ret =3D 0; + + hps =3D devm_kzalloc(&client->dev, sizeof(*hps), GFP_KERNEL); + if (!hps) + return -ENOMEM; + + memset(&hps->misc_device, 0, sizeof(hps->misc_device)); + hps->misc_device.parent =3D &client->dev; + hps->misc_device.minor =3D MISC_DYNAMIC_MINOR; + hps->misc_device.name =3D "hps"; + hps->misc_device.fops =3D &hps_fops; + + i2c_set_clientdata(client, hps); + hps->client =3D client; + hps->enable_gpio =3D devm_gpiod_get(&client->dev, "enable", GPIOD_OUT_HIG= H); + if (IS_ERR(hps->enable_gpio)) { + ret =3D PTR_ERR(hps->enable_gpio); + dev_err(&client->dev, "failed to get enable gpio: %d\n", ret); + return ret; + } + + ret =3D devm_add_action(&client->dev, &hps_unload, hps); + if (ret) { + dev_err(&client->dev, + "failed to install unload action: %d\n", ret); + return ret; + } + + ret =3D misc_register(&hps->misc_device); + if (ret) { + dev_err(&client->dev, "failed to initialize misc device: %d\n", ret); + return ret; + } + + hps_set_power(hps, false); + pm_runtime_enable(&client->dev); + return ret; +} + +static int hps_i2c_remove(struct i2c_client *client) +{ + struct hps_drvdata *hps =3D i2c_get_clientdata(client); + + pm_runtime_disable(&client->dev); + misc_deregister(&hps->misc_device); + return 0; +} + +static int hps_suspend(struct device *dev) +{ + struct i2c_client *client =3D to_i2c_client(dev); + struct hps_drvdata *hps =3D i2c_get_clientdata(client); + + hps_set_power(hps, false); + return 0; +} + +static int hps_resume(struct device *dev) +{ + struct i2c_client *client =3D to_i2c_client(dev); + struct hps_drvdata *hps =3D i2c_get_clientdata(client); + + hps_set_power(hps, true); + return 0; +} +static UNIVERSAL_DEV_PM_OPS(hps_pm_ops, hps_suspend, hps_resume, NULL); + +static const struct i2c_device_id hps_i2c_id[] =3D { + { "hps", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, hps_i2c_id); + +#ifdef CONFIG_ACPI +static const struct acpi_device_id hps_acpi_id[] =3D { + { HPS_ACPI_ID, 0 }, + { } +}; +MODULE_DEVICE_TABLE(acpi, hps_acpi_id); +#endif /* CONFIG_ACPI */ + +static struct i2c_driver hps_i2c_driver =3D { + .probe_new =3D hps_i2c_probe, + .remove =3D hps_i2c_remove, + .id_table =3D hps_i2c_id, + .driver =3D { + .name =3D "hps", + .pm =3D &hps_pm_ops, +#ifdef CONFIG_ACPI + .acpi_match_table =3D ACPI_PTR(hps_acpi_id), +#endif + }, +}; +module_i2c_driver(hps_i2c_driver); + +MODULE_ALIAS("acpi:" HPS_ACPI_ID); +MODULE_AUTHOR("Sami Ky=C3=B6stil=C3=A4 "); +MODULE_DESCRIPTION("Driver for ChromeOS HPS"); +MODULE_LICENSE("GPL"); --=20 2.35.0.263.gb82422642f-goog