Add a driver for the ChromeOS screen privacy sensor (HPS)
Sami Kyöstilä
reviewerv3 cover Add a driver for the ChromeOS screen privacy sensor (HPS)
This series adds a driver for the ChromeOS screen privacy sensor (a.k.a. HPS) device, attached on the I2C bus. The sensor is a hardware-isolated module which reports a high-level presence signal, e.g., whether there is person in front of the computer or not.
The driver exposes the device to userspace as a character device, which can be used to control the power state of the device. The driver automatically turns power off to the sensor unless a process is using it.
More information about HPS, its firmware and communication protocol can be found at
https://chromium.googlesource.com/chromiumos/platform/hps-firmware.
Changes in v3:
- Moved from drivers/misc to drivers/platform/chrome.
Changes in v2:
- Removed custom ioctl interface.
- Reworked to use miscdev.
Sami Kyöstilä (1):
platform/chrome: add a driver for HPS
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
Signature · 1 line
2.35.0.263.gb82422642f-goog
Sami Kyöstilä
reviewerPATCH v3 1/1 platform/chrome: add a driver for HPS
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.
---
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
MAINTAINERS +6 -0
@@ -8800,6 +8800,12 @@ S: Maintained W: http://artax.karlin.mff.cuni.cz/~mikulas/vyplody/hpfs/index-e.cgi F: fs/hpfs/ +HPS (ChromeOS screen privacy sensor) DRIVER +M: Sami Kyöstilä <[email protected]> +R: Evan Benn <[email protected]> +S: Maintained +F: drivers/platform/chrome/cros_hps_i2c.c + HSI SUBSYSTEM M: Sebastian Reichel <[email protected]> S: Maintained
drivers/platform/chrome/Kconfig +10 -0
@@ -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. +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
drivers/platform/chrome/Makefile +1 -0
@@ -23,6 +23,7 @@ obj-$(CONFIG_CROS_EC_DEBUGFS) += cros_ec_debugfs.o cros-ec-sensorhub-objs := cros_ec_sensorhub.o cros_ec_sensorhub_ring.o cros_ec_trace.o obj-$(CONFIG_CROS_EC_SENSORHUB) += cros-ec-sensorhub.o obj-$(CONFIG_CROS_EC_SYSFS) += cros_ec_sysfs.o +obj-$(CONFIG_CROS_HPS_I2C) += cros_hps_i2c.o obj-$(CONFIG_CROS_USBPD_LOGGER) += cros_usbpd_logger.o obj-$(CONFIG_CROS_USBPD_NOTIFY) += cros_usbpd_notify.o
drivers/platform/chrome/cros_hps_i2c.c new file +184 -0
184 lines not on this page · raw message
Signature · 1 line
2.35.0.263.gb82422642f-goog
Re: [PATCH v3 1/1] platform/chrome: add a driver for HPS
Quoted 3 lines · Sami Kyöstilä
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.
The patch uses HPS instead of SPS everywhere. Would you consider to use "human presence sensor" when referring it?
Quoted 5 lines
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.
Quoted 8 lines
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.
It's weird. If most of the thing is done by userspace programs, couldn't it set the power GPIO via userspace interface (e.g. [1]) too?
[1]: https://embeddedbits.org/new-linux-kernel-gpio-user-space-interface/
Quoted 1 line
MAINTAINERS +0
-0
[...]
Quoted 1 line
+HPS (ChromeOS screen privacy sensor) DRIVER
Does it make more sense to use "CHROMEOS HPS DRIVER" title?
Quoted 1 line
drivers/platform/chrome/cros_hps_i2c.c +0
-0
[...]
drivers/platform/chrome/cros_hps_i2c.c line 31
Quoted 3 lines
drivers/platform/chrome/cros_hps_i2c.c +3
-0+static void hps_set_power (struct hps_drvdata *hps,bool state) +{ +if (!IS_ERR_OR_NULL (hps->enable_gpio))
Could it get rid of the check? Does the function get called if device probe fails?
drivers/platform/chrome/cros_hps_i2c.c line 39
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c +5
-0+static void hps_unload (void *drv_data) +{ +struct hps_drvdata *hps = drv_data; + +hps_set_power (hps, true);
Why does it need to set to true when device removing?
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c +5
-0+static int hps_open (struct inode *inode,struct file *file) +{ +struct hps_drvdata *hps =container_of (file->private_data, + struct hps_drvdata, misc_device); +struct device *dev = &hps->client->dev;
drivers/platform/chrome/cros_hps_i2c.c line 56
Quoted 10 lines
drivers/platform/chrome/cros_hps_i2c.c +10
-0+int ret; + + ret =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);
The two functions are not effectively symmetric if pm_runtime_get_sync() fails.
- It doesn't need to call pm_runtime_put() if pm_runtime_get_sync() fails.
- I guess it wouldn't want to pm_runtime_disable() here. The capability is controlled when the device probing and removing.
Quoted 4 lines
drivers/platform/chrome/cros_hps_i2c.c +4
-0+static int hps_release (struct inode *inode,struct file *file) +{ +struct hps_drvdata *hps =container_of (file->private_data, + struct hps_drvdata, misc_device);
drivers/platform/chrome/cros_hps_i2c.c line 73
Quoted 10 lines
drivers/platform/chrome/cros_hps_i2c.c +10
-0+struct device *dev = &hps->client->dev; +int ret; + + ret =pm_runtime_put (dev); +if (ret <0 ) + goto pm_put_fail; +return 0 ; + +pm_put_fail: +pm_runtime_disable (dev);
Same here.
drivers/platform/chrome/cros_hps_i2c.c line 81
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c +5
-0+const struct file_operations hps_fops = { + .owner =THIS_MODULE , + .open = hps_open, + .release = hps_release, +};
The struct can be static.
drivers/platform/chrome/cros_hps_i2c.c line 86
Quoted 4 lines
drivers/platform/chrome/cros_hps_i2c.c +4
-0+static int hps_i2c_probe (struct i2c_client *client) +{ +struct hps_drvdata *hps; +int ret =0 ;
It doesn't need to be initialized. It's going to be overridden soon.
drivers/platform/chrome/cros_hps_i2c.c line 95
Quoted 4 lines
drivers/platform/chrome/cros_hps_i2c.c +4
-0+memset (&hps->misc_device,0 ,sizeof (hps->misc_device)); + hps->misc_device.parent = &client->dev; + hps->misc_device.minor =MISC_DYNAMIC_MINOR ; + hps->misc_device.name ="hps" ;
Does "cros_hps_i2c" or "cros_hps" make more sense?
drivers/platform/chrome/cros_hps_i2c.c line 112
Quoted 6 lines
drivers/platform/chrome/cros_hps_i2c.c +6
-0+ ret =devm_add_action (&client->dev, &hps_unload, hps); +if (ret) { +dev_err (&client->dev, +"failed to install unload action: %d\n" , ret); +return ret; + }
Why does it need to call hps_unload() when device removing? Couldn't it put the code in hps_i2c_remove()?
drivers/platform/chrome/cros_hps_i2c.c line 122
Quoted 3 lines
drivers/platform/chrome/cros_hps_i2c.c +3
-0+hps_set_power (hps, false); +pm_runtime_enable (&client->dev); +return ret;
Using return 0; makes it clear.
Quoted 7 lines
drivers/platform/chrome/cros_hps_i2c.c +7
-0+static int hps_suspend (struct device *dev) +{ +struct i2c_client *client =to_i2c_client (dev); +struct hps_drvdata *hps =i2c_get_clientdata (client); + +hps_set_power (hps, false); +return 0 ;
drivers/platform/chrome/cros_hps_i2c.c line 150
Quoted 10 lines
drivers/platform/chrome/cros_hps_i2c.c +10
-0+} + +static int hps_resume (struct device *dev) +{ +struct i2c_client *client =to_i2c_client (dev); +struct hps_drvdata *hps =i2c_get_clientdata (client); + +hps_set_power (hps, true); +return 0 ; +}
Does it need to save the old state before suspending? Instead of turning on the power after every resumes.
drivers/platform/chrome/cros_hps_i2c.c line 154
Quoted 2 lines
drivers/platform/chrome/cros_hps_i2c.c +2
-0+static const struct i2c_device_id hps_i2c_id[] = { + {"hps" ,0 },
Does "cros_hps_i2c" or "cros_hps" make more sense?
drivers/platform/chrome/cros_hps_i2c.c line 172
Quoted 6 lines
drivers/platform/chrome/cros_hps_i2c.c +6
-0+static struct i2c_driver hps_i2c_driver = { + .probe_new = hps_i2c_probe, + .remove = hps_i2c_remove, + .id_table = hps_i2c_id, + .driver = { + .name ="hps" ,
Does "cros_hps_i2c" or "cros_hps" make more sense?
drivers/platform/chrome/cros_hps_i2c.c line 176
Quoted 3 lines
drivers/platform/chrome/cros_hps_i2c.c +3
-0+#ifdef CONFIG_ACPI + .acpi_match_table = ACPI_PTR(hps_acpi_id), +#endif
It doesn't need the guard as ACPI_PTR() already does.
Sami Kyostila
reviewerRe: [PATCH v3 1/1] platform/chrome: add a driver for HPS
( to 10. helmik. 2022 klo 16.41 Tzung-Bi Shih ([email protected]) kirjoitti:
Quoted 5 lines · Sami Kyöstilä
Quoted 3 lines
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.
Thanks for the review!
Quoted 2 lines
The patch uses HPS instead of SPS everywhere. Would you consider to use "human presence sensor" when referring it?
Sure thing, I'll unify the terminology. There are a few different names going around for the user-facing features powered by the sensor which makes this a bit confusing.
Quoted 10 lines
Quoted 10 lines
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.
Quoted 6 lines
Quoted 3 lines
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.
It's weird. If most of the thing is done by userspace programs, couldn't it set the power GPIO via userspace interface (e.g. [1]) too?
I agree that's a little unusual, but there are some good reasons for this to be in the kernel. First, it lets us turn HPS off during system suspend -- which I'm not sure how you'd do from userspace. Second, it avoids the need to give write access to the entire GPIO chip to the hpsd userspace daemon. We just need a single line, while the controller in this case has a total of 360 gpios. Finally, HPS also has an interrupt line, and we're planning to let it wake up the host, which I also believe needs to be done in the kernel.
Quoted 8 lines
[1]: https://embeddedbits.org/new-linux-kernel-gpio-user-space-interface/
Quoted 1 line
MAINTAINERS +0
-0[...]
Quoted 1 line
+HPS (ChromeOS screen privacy sensor) DRIVER
Does it make more sense to use "CHROMEOS HPS DRIVER" title?
Good idea, done.
Quoted 8 lines
Quoted 1 line
drivers/platform/chrome/cros_hps_i2c.c +0
-0[...]
drivers/platform/chrome/cros_hps_i2c.c line 31
Quoted 3 lines
drivers/platform/chrome/cros_hps_i2c.c +3
-0+static void hps_set_power (struct hps_drvdata *hps,bool state) +{ +if (!IS_ERR_OR_NULL (hps->enable_gpio))Could it get rid of the check? Does the function get called if device probe fails?
True, done.
Quoted 7 lines
drivers/platform/chrome/cros_hps_i2c.c line 39
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c +5
-0+static void hps_unload (void *drv_data) +{ +struct hps_drvdata *hps = drv_data; + +hps_set_power (hps, true);Why does it need to set to true when device removing?
By default, HPS is powered on when the system starts and before the driver is loaded. We want to restore it to that default state here. This is needed for example for automated testing, where we can unbind the driver to make sure HPS stays powered.
Quoted 13 lines
Quoted 13 lines
drivers/platform/chrome/cros_hps_i2c.c +13
-0+static int hps_open (struct inode *inode,struct file *file) +{ +struct hps_drvdata *hps =container_of (file->private_data, + struct hps_drvdata, misc_device); +struct device *dev = &hps->client->dev; +int ret; + + ret =pm_runtime_get_sync (dev); +if (ret <0 ) + goto pm_get_fail; +return 0 ; + +pm_get_fail:
Quoted 8 lines
drivers/platform/chrome/cros_hps_i2c.c
Quoted 2 lines
drivers/platform/chrome/cros_hps_i2c.c +2
-0+pm_runtime_put (dev); +pm_runtime_disable (dev);The two functions are not effectively symmetric if
pm_runtime_get_sync()fails.
- It doesn't need to callpm_runtime_put()ifpm_runtime_get_sync()fails.
- I guess it wouldn't want topm_runtime_disable()here. The capability is controlled when the device probing and removing.
According to the documentation, pm_runtime_get_sync() always bumps the usage count, including in failure cases. However there's pm_runtime_resume_and_get() which doesn't increment the counter for failures, so I've switched to that so that we don't have to handle the error case here. I agree that pm_runtime_disable() isn't really necessary here -- removed.
Quoted 8 lines
Quoted 8 lines
drivers/platform/chrome/cros_hps_i2c.c +8
-0+static int hps_release (struct inode *inode,struct file *file) +{ +struct hps_drvdata *hps =container_of (file->private_data, + struct hps_drvdata, misc_device); +struct device *dev = &hps->client->dev; +int ret; + + ret =pm_runtime_put (dev);
Quoted 8 lines
drivers/platform/chrome/cros_hps_i2c.c line 73
Quoted 6 lines
drivers/platform/chrome/cros_hps_i2c.c +6
-0+if (ret <0 ) + goto pm_put_fail; +return 0 ; + +pm_put_fail: +pm_runtime_disable (dev);Same here.
Removed.
Quoted 8 lines
drivers/platform/chrome/cros_hps_i2c.c line 81
Quoted 6 lines
drivers/platform/chrome/cros_hps_i2c.c +5
-0+const struct file_operations hps_fops = { + .owner =THIS_MODULE , + .open = hps_open, + .release = hps_release, +};The struct can be static.
Done.
Quoted 7 lines
drivers/platform/chrome/cros_hps_i2c.c line 86
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c +4
-0+static int hps_i2c_probe (struct i2c_client *client) +{ +struct hps_drvdata *hps; +int ret =0 ;It doesn't need to be initialized. It's going to be overridden soon.
Fixed.
Quoted 6 lines
drivers/platform/chrome/cros_hps_i2c.c line 95
Quoted 4 lines
drivers/platform/chrome/cros_hps_i2c.c +4
-0+memset (&hps->misc_device,0 ,sizeof (hps->misc_device)); + hps->misc_device.parent = &client->dev; + hps->misc_device.minor =MISC_DYNAMIC_MINOR ; + hps->misc_device.name ="hps" ;Does "
cros_hps_i2c" or "cros_hps" make more sense?
Changed to "cros-hps" to better match the driver's name (and the naming convention of other Chrome OS drivers).
Quoted 9 lines
drivers/platform/chrome/cros_hps_i2c.c line 112
Quoted 6 lines
drivers/platform/chrome/cros_hps_i2c.c +6
-0+ ret =devm_add_action (&client->dev, &hps_unload, hps); +if (ret) { +dev_err (&client->dev, +"failed to install unload action: %d\n" , ret); +return ret; + }Why does it need to call
hps_unload()when device removing? Couldn't it put the code inhps_i2c_remove()?
Ah, this was left over from an earlier version where the setup/teardown sequence was a little more complex. Agreed and moved to hps_i2c_remove.
Quoted 6 lines
drivers/platform/chrome/cros_hps_i2c.c line 122
Quoted 4 lines
drivers/platform/chrome/cros_hps_i2c.c +3
-0+hps_set_power (hps, false); +pm_runtime_enable (&client->dev); +return ret;Using
return 0;makes it clear.
Done.
Quoted 12 lines
Quoted 12 lines
drivers/platform/chrome/cros_hps_i2c.c +12
-0+static int hps_suspend (struct device *dev) +{ +struct i2c_client *client =to_i2c_client (dev); +struct hps_drvdata *hps =i2c_get_clientdata (client); + +hps_set_power (hps, false); +return 0 ; +} + +static int hps_resume (struct device *dev) +{ +struct i2c_client *client =to_i2c_client (dev);
Quoted 8 lines
drivers/platform/chrome/cros_hps_i2c.c line 150
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c +5
-0+struct hps_drvdata *hps =i2c_get_clientdata (client); + +hps_set_power (hps, true); +return 0 ; +}Does it need to save the old state before suspending? Instead of turning on the power after every resumes.
No, the runtime pm system makes sure suspend and resume are only called when needed. For example, if someone has an open reference to the device when the system goes to sleep, suspend and resume are called appropriately. If HPS was already suspended, then neither entrypoint gets called when going to sleep or waking up.
Quoted 4 lines
drivers/platform/chrome/cros_hps_i2c.c line 154
Quoted 2 lines
drivers/platform/chrome/cros_hps_i2c.c +2
-0+static const struct i2c_device_id hps_i2c_id[] = { + {"hps" ,0 },Does "
cros_hps_i2c" or "cros_hps" make more sense?
Went with "cros-hps".
Quoted 8 lines
drivers/platform/chrome/cros_hps_i2c.c line 172
Quoted 6 lines
drivers/platform/chrome/cros_hps_i2c.c +6
-0+static struct i2c_driver hps_i2c_driver = { + .probe_new = hps_i2c_probe, + .remove = hps_i2c_remove, + .id_table = hps_i2c_id, + .driver = { + .name ="hps" ,Does "
cros_hps_i2c" or "cros_hps" make more sense?
Ditto.
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c line 176
Quoted 3 lines
drivers/platform/chrome/cros_hps_i2c.c +3
-0+#ifdef CONFIG_ACPI + .acpi_match_table = ACPI_PTR(hps_acpi_id), +#endif It doesn't need the guard as
ACPI_PTR()already does.
Ah, good to know!
- Sami
Re: [PATCH v3 1/1] platform/chrome: add a driver for HPS
Hi,
Left some follow-up questions before going for the v4. Please also cc the following patches to <[email protected]> in case we would overlook them from LKML.
Quoted 20 lines · Sami Kyostila
( to 10. helmik. 2022 klo 16.41 Tzung-Bi Shih ([email protected]) kirjoitti:
Quoted 12 lines · Sami Kyöstilä
Quoted 10 lines
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.
Quoted 6 lines
Quoted 3 lines
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.
It's weird. If most of the thing is done by userspace programs, couldn't it set the power GPIO via userspace interface (e.g. [1]) too?
Quoted 8 lines
I agree that's a little unusual, but there are some good reasons for this to be in the kernel. First, it lets us turn HPS off during system suspend -- which I'm not sure how you'd do from userspace. Second, it avoids the need to give write access to the entire GPIO chip to the hpsd userspace daemon. We just need a single line, while the controller in this case has a total of 360 gpios. Finally, HPS also has an interrupt line, and we're planning to let it wake up the host, which I also believe needs to be done in the kernel.
What prevents it from implementing the I2C communication in kernel?
Did you investigate if there are any existing frameworks for HPS (e.g. drivers/iio/)? I am wondering if kernel already has some abstract code for HPS.
Only found one via:
$ git grep 'human presence sensor' drivers/hid/amd-sfh-hid/hid_descriptor/amd_sfh_hid_desc.h: [...]
Quoted 8 lines
Quoted 7 lines
drivers/platform/chrome/cros_hps_i2c.c line 39
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c new file +5
-0+static void hps_unload (void *drv_data) +{ +struct hps_drvdata *hps = drv_data; + +hps_set_power (hps, true);Why does it need to set to true when device removing?
Quoted 4 lines
By default, HPS is powered on when the system starts and before the driver is loaded. We want to restore it to that default state here. This is needed for example for automated testing, where we can unbind the driver to make sure HPS stays powered.
Echo to the idea of previous comment: does kernel have any frameworks for HPS? "HPS is powered on when the system starts" sounds like a chip specific implementation.
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/platform/chrome/cros_hps_i2c.c new file +12
-0+static int hps_suspend (struct device *dev) +{ +struct i2c_client *client =to_i2c_client (dev); +struct hps_drvdata *hps =i2c_get_clientdata (client); + +hps_set_power (hps, false); +return 0 ; +} + +static int hps_resume (struct device *dev) +{ +struct i2c_client *client =to_i2c_client (dev);Quoted 8 lines
drivers/platform/chrome/cros_hps_i2c.c line 150
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c new file +5
-0+struct hps_drvdata *hps =i2c_get_clientdata (client); + +hps_set_power (hps, true); +return 0 ; +}Does it need to save the old state before suspending? Instead of turning on the power after every resumes.
Quoted 5 lines
No, the runtime pm system makes sure suspend and resume are only called when needed. For example, if someone has an open reference to the device when the system goes to sleep, suspend and resume are called appropriately. If HPS was already suspended, then neither entrypoint gets called when going to sleep or waking up.
It uses UNIVERSAL_DEV_PM_OPS() which also sets for .suspend() and .resume(). OTOH, the documentation suggests it has deprecated (see include/linux/pm.h).
Sami Kyostila
reviewerRe: [PATCH v3 1/1] platform/chrome: add a driver for HPS
ma 14. helmik. 2022 klo 12.48 Tzung-Bi Shih ([email protected]) kirjoitti:
Quoted 6 lines
Hi,
Left some follow-up questions before going for the v4. Please also cc the following patches to <[email protected]> in case we would overlook them from LKML.
Thank you, I appreciate the feedback!
Quoted 33 lines · Sami Kyostila
Quoted 20 lines
( to 10. helmik. 2022 klo 16.41 Tzung-Bi Shih ([email protected]) kirjoitti:
Quoted 12 lines · Sami Kyöstilä
Quoted 10 lines
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.
Quoted 6 lines
Quoted 3 lines
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.
It's weird. If most of the thing is done by userspace programs, couldn't it set the power GPIO via userspace interface (e.g. [1]) too?
Quoted 8 lines
I agree that's a little unusual, but there are some good reasons for this to be in the kernel. First, it lets us turn HPS off during system suspend -- which I'm not sure how you'd do from userspace. Second, it avoids the need to give write access to the entire GPIO chip to the hpsd userspace daemon. We just need a single line, while the controller in this case has a total of 360 gpios. Finally, HPS also has an interrupt line, and we're planning to let it wake up the host, which I also believe needs to be done in the kernel.
What prevents it from implementing the I2C communication in kernel?
Quoted 7 lines
Did you investigate if there are any existing frameworks for HPS (e.g.
drivers/iio/)? I am wondering if kernel already has some abstract code for HPS.Only found one via:
$ git grep 'human presence sensor' drivers/hid/amd-sfh-hid/hid_descriptor/amd_sfh_hid_desc.h: [...]
Yes, we did look into some existing mechanisms in the kernel this could fit into. HID was probably the closest fit since the spec has been recently expanded with human presence sensing (in fact, the first revision of this driver was HID-based[1]), but a few reasons made it a poor fit:
1. Our HPS hardware doesn't use the HID-I2C wire protocol, so we'd still need a bespoke I2C communication layer.
2. HPS uses a somewhat complex multi-stage firmware update process, and we didn't feel comfortable trying to push that down into the kernel at this point.
3. The feature set of HPS is still evolving, so this doesn't seem like the right time to bake specific functionality into a syscall interface.
I also had a quick look at iio, but it felt a bit overkill for the very small amount of data HPS generates.
My feeling is that HID-I2C is probably the right long term fit, but it'll take some time (and HW iterations) to get there. Earlier in the patch series we discussed an intermediate step where we make it possible to expose individual I2C devices to userspace through separate device nodes, which will help reduce the attack surface of giving userspace write access to the entire I2C bus just for HPS.
[1] go/hps-kernel-driver (sorry for the internal link)
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/platform/chrome/cros_hps_i2c.c line 39
Quoted 5 lines
drivers/platform/chrome/cros_hps_i2c.c new file +5
-0+static void hps_unload (void *drv_data) +{ +struct hps_drvdata *hps = drv_data; + +hps_set_power (hps, true);Why does it need to set to true when device removing?
Quoted 8 lines
Quoted 4 lines
By default, HPS is powered on when the system starts and before the driver is loaded. We want to restore it to that default state here. This is needed for example for automated testing, where we can unbind the driver to make sure HPS stays powered.
Echo to the idea of previous comment: does kernel have any frameworks for HPS? "HPS is powered on when the system starts" sounds like a chip specific implementation.
True, that's more of an implementation detail of our system rather than any hard guarantee. I'll change this to save and restore the power state that was there before the driver got loaded.
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 7 lines
drivers/platform/chrome/cros_hps_i2c.c new file +7
-0+static int hps_suspend (struct device *dev) +{ +struct i2c_client *client =to_i2c_client (dev); +struct hps_drvdata *hps =i2c_get_clientdata (client); + +hps_set_power (hps, false); +return 0 ;drivers/platform/chrome/cros_hps_i2c.c line 150
Quoted 10 lines
drivers/platform/chrome/cros_hps_i2c.c new file +10
-0+} + +static int hps_resume (struct device *dev) +{ +struct i2c_client *client =to_i2c_client (dev); +struct hps_drvdata *hps =i2c_get_clientdata (client); + +hps_set_power (hps, true); +return 0 ; +}Does it need to save the old state before suspending? Instead of turning on the power after every resumes.
Quoted 8 lines
Quoted 5 lines
No, the runtime pm system makes sure suspend and resume are only called when needed. For example, if someone has an open reference to the device when the system goes to sleep, suspend and resume are called appropriately. If HPS was already suspended, then neither entrypoint gets called when going to sleep or waking up.
It uses
UNIVERSAL_DEV_PM_OPS()which also sets for .suspend() and .resume(). OTOH, the documentation suggests it has deprecated (seeinclude/linux/pm.h).
Thanks, I think I missed this distinction between .suspend/resume() and .runtime_suspend/resume(). As far as I can tell it shouldn't make a difference in practice since we are just toggling a GPIO pin, but I'll nevertheless fix it for consistency.
- Sami