Skip to contentSkip to conversation

iio: Add a ChromeOS EC MKBP proximity driver

PATCH Superseded Stephen Boyd 3 participants 8 messages Feb 10, 2021 – Feb 12, 2021

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover iio: Add a ChromeOS EC MKBP proximity driver

This is a different approach to [1] where I tried to add this proximity sensor logic to the input subsystem. Instead, we'll take the approach of making a small IIO proximity driver that parses the EC switch bitmap to find out if the front proximity sensor is detecting something or not. This allows us to treat proximity sensors as IIO devices all the time in userspace instead of handling this switch on the EC via the input subsystem and then other proximity sensors via IIO.

I propose this is all merged through IIO subsystem. Please ack the first patch so it can be merged that way.

Changes from v4:
* Reduced binding and moved proximity node to mfd spi example
* Dropped of_match_ptr()

Changes from v3:
* Added SPI and cros-ec wrapper nodes to yaml example
* Ignore notifier registration return code that is always zero

Changes from v2:
* Check iio clock and use IIO time if not boottime

Changes from v1:
* Driver moved location
* Put mkbp everywhere
* Fixed up DT binding to not fail and make sure is a child of cros-ec
* Simplified logic for sending a message
* Dropped CONFIG_OF usage
* Sorted includes

[1] https://lore.kernel.org/r/[email protected]

Cc Dmitry Torokhov <[email protected]>
Cc Benson Leung <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Douglas Anderson <[email protected]>
Cc Gwendal Grignou <[email protected]>
Cc Rob Herring <[email protected]>
Cc Enric Balletbo i Serra <[email protected]>

Stephen Boyd (3):
platform/chrome: cros_ec: Add SW_FRONT_PROXIMITY MKBP define
dt-bindings: iio: Add cros ec proximity yaml doc
iio: proximity: Add a ChromeOS EC MKBP proximity driver

 .../google,cros-ec-mkbp-proximity.yaml        |  37 +++
 .../bindings/mfd/google,cros-ec.yaml          |   7 +
 drivers/iio/proximity/Kconfig                 |  11 +
 drivers/iio/proximity/Makefile                |   1 +
 .../iio/proximity/cros_ec_mkbp_proximity.c    | 242 ++++++++++++++++++
 .../linux/platform_data/cros_ec_commands.h    |   1 +
 6 files changed, 299 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/proximity/google,cros-ec-mkbp-proximity.yaml
 create mode 100644 drivers/iio/proximity/cros_ec_mkbp_proximity.c

base-commit: 19c329f6808995b142b3966301f217c831e7cf31

Signature · 1 line
https://chromeos.dev
#1 lore raw

PATCH v5 1/3 platform/chrome: cros_ec: Add SW_FRONT_PROXIMITY MKBP define

Some cros ECs support a front proximity MKBP event via 'EC_MKBP_FRONT_PROXIMITY'. Add this define so it can be used in a future patch.

Cc Dmitry Torokhov <[email protected]>
Cc Benson Leung <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Douglas Anderson <[email protected]>
Cc Gwendal Grignou <[email protected]>

---

No changes from last time.

 include/linux/platform_data/cros_ec_commands.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/platform_data/cros_ec_commands.h +1 -0
index 86376779ab31..776e0b2be0e9 100644
@@ -3457,6 +3457,7 @@ struct ec_response_get_next_event_v1 {
 #define EC_MKBP_LID_OPEN	0
 #define EC_MKBP_TABLET_MODE	1
 #define EC_MKBP_BASE_ATTACHED	2
+#define EC_MKBP_FRONT_PROXIMITY	3
 
 /* Run keyboard factory test scanning */
 #define EC_CMD_KEYBOARD_FACTORY_TEST 0x0068
Signature · 1 line
https://chromeos.dev
#2 lore raw

PATCH v5 2/3 dt-bindings: iio: Add cros ec proximity yaml doc

In reply to Stephen Boyd

Some cros ECs support a front proximity MKBP event via 'EC_MKBP_FRONT_PROXIMITY'. Add a DT binding to document this feature via a node that is a child of the main cros_ec device node. Devices that have this ability will describe this in firmware.

Cc Dmitry Torokhov <[email protected]>
Cc Benson Leung <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Douglas Anderson <[email protected]>
Cc Gwendal Grignou <[email protected]>
Cc Rob Herring <[email protected]>
Cc Enric Balletbo i Serra <[email protected]>

---

Changes from v4:
* Reduced example in iio binding and moved to mfd
* Dropped unevaluatedProperties

 .../google,cros-ec-mkbp-proximity.yaml        | 37 +++++++++++++++++++
 .../bindings/mfd/google,cros-ec.yaml          |  7 ++++
 2 files changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/proximity/google,cros-ec-mkbp-proximity.yaml
Documentation/devicetree/bindings/iio/proximity/google,cros-ec-mkbp-proximity.yaml new file +37 -0
new file mode 100644
index 000000000000..099b4be927d4
@@ -0,0 +1,37 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+
+$id: http://devicetree.org/schemas/iio/proximity/google,cros-ec-mkbp-proximity.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: ChromeOS EC MKBP Proximity Sensor
+
+maintainers:
+  - Stephen Boyd <[email protected]>
+  - Benson Leung <[email protected]>
+  - Enric Balletbo i Serra <[email protected]>
+
+description: |
+  Google's ChromeOS EC sometimes has the ability to detect user proximity.
+  This is implemented on the EC as near/far logic and exposed to the OS
+  via an MKBP switch bit.
+
+properties:
+  compatible:
+    const: google,cros-ec-mkbp-proximity
+
+  label:
+    description: Name for proximity sensor
+
+required:
+  - compatible
+
+additionalProperties: false
+
+examples:
+  - |
+    proximity {
+      compatible = "google,cros-ec-mkbp-proximity";
+      label = "proximity-wifi-lte";
+    };
Documentation/devicetree/bindings/mfd/google,cros-ec.yaml +7 -0
index 76bf16ee27ec..4dfa70a013ae 100644
@@ -94,6 +94,9 @@ properties:
   keyboard-controller:
     $ref: "/schemas/input/google,cros-ec-keyb.yaml#"
 
+  proximity:
+    $ref: "/schemas/iio/proximity/google,cros-ec-mkbp-proximity.yaml#"
+
   codecs:
     type: object
     additionalProperties: false
@@ -180,6 +183,10 @@ examples:
             interrupts = <99 0>;
             interrupt-parent = <&gpio7>;
             spi-max-frequency = <5000000>;
+
+            proximity {
+                    compatible = "google,cros-ec-mkbp-proximity";
+            };
         };
     };
 
Signature · 1 line
https://chromeos.dev
maintainer Reviewed-by on v5 2/3
#3 lore raw

Re: [PATCH v5 2/3] dt-bindings: iio: Add cros ec proximity yaml doc

Quoted 16 lines · Stephen Boyd

Some cros ECs support a front proximity MKBP event via 'EC_MKBP_FRONT_PROXIMITY'. Add a DT binding to document this feature via a node that is a child of the main cros_ec device node. Devices that have this ability will describe this in firmware.

Cc Dmitry Torokhov <[email protected]>
Cc Benson Leung <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Douglas Anderson <[email protected]>
Cc Gwendal Grignou <[email protected]>
Cc Rob Herring <[email protected]>
Cc Enric Balletbo i Serra <[email protected]>

---

Quoted 9 lines

Changes from v4:
* Reduced example in iio binding and moved to mfd
* Dropped unevaluatedProperties

 .../google,cros-ec-mkbp-proximity.yaml        | 37 +++++++++++++++++++
 .../bindings/mfd/google,cros-ec.yaml          |  7 ++++
 2 files changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/proximity/google,cros-ec-mkbp-proximity.yaml
#4 lore raw

PATCH v5 3/3 iio: proximity: Add a ChromeOS EC MKBP proximity driver

In reply to Stephen Boyd

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 <[email protected]>
Cc Benson Leung <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Douglas Anderson <[email protected]>
Cc Gwendal Grignou <[email protected]>

---

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
drivers/iio/proximity/Kconfig +11 -0
index 12672a0e89ed..7c7203ca3ac6 100644
@@ -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
drivers/iio/proximity/Makefile +1 -0
index 9c1aca1a8b79..cbdac09433eb 100644
@@ -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
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +242 -0

242 lines not on this page · raw message

Signature · 1 line
https://chromeos.dev
#5 lore raw

Re: [PATCH v5 3/3] iio: proximity: Add a ChromeOS EC MKBP proximity driver

Quoted 173 lines · Stephen Boyd

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 <[email protected]>
Cc Benson Leung <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Douglas Anderson <[email protected]>
Cc Gwendal Grignou <[email protected]>

---

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
drivers/iio/proximity/Kconfig +11 -0
index 12672a0e89ed..7c7203ca3ac6 100644
@@ -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
drivers/iio/proximity/Makefile +1 -0
index 9c1aca1a8b79..cbdac09433eb 100644
@@ -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
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +106 -0
new file mode 100644
index 000000000000..2cdaf05c0ec2
@@ -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 <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/notifier.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+
+#include <linux/platform_data/cros_ec_commands.h>
+#include <linux/platform_data/cros_ec_proto.h>
+
+#include <linux/iio/events.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#include <asm/unaligned.h>
+
+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;

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 116

Quoted 10 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +10 -0
+       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.

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 119

Quoted 3 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +3 -0
+                       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().

Quoted 13 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +13 -0
+                       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,

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 142

Quoted 10 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +10 -0
+                          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.

Quoted 103 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +100 -1
+       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

#6 lore raw

Re: [PATCH v5 3/3] iio: proximity: Add a ChromeOS EC MKBP proximity driver

Quoting Gwendal Grignou (2021-02-10 00:29:45)

Quoted 16 lines · Stephen Boyd

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 116

Quoted 8 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +8 -0
+       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.

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 119

Quoted 3 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +3 -0
+                       if (iio_device_get_clock(indio_dev) != CLOCK_BOOTTIME)
+                               timestamp = iio_get_time_ns(indio_dev);
+                       state = cros_ec_mkbp_proximity_parse_state(switches);
Quoted 6 lines

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().

Ah ok. So we'll have to save a state tracking variable and poll the bit once at boot and then at resume time? What happens to events that happen across suspend/resume? We drop them? Or we need to inject the last state if it's different into IIO with the time of resume?

Quoted 17 lines
Quoted 17 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +17 -0
+                       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);
Quoted 8 lines

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 142

Quoted 6 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +6 -0
+       struct cros_ec_device *ec = data->ec;
+
+       if (chan->type != IIO_PROXIMITY)
+               return -EINVAL;
+
+       switch (mask) {

A switch is not necessary here.

Ok.

Quoted 1 line
Quoted 1 line

+ case IIO_CHAN_INFO_RAW:

#7 lore raw

Re: [PATCH v5 3/3] iio: proximity: Add a ChromeOS EC MKBP proximity driver

Quoted 25 lines · Stephen Boyd

Quoting Gwendal Grignou (2021-02-10 00:29:45)

Quoted 16 lines · Stephen Boyd

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 116

Quoted 8 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +8 -0
+       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.

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 119

Quoted 3 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +3 -0
+                       if (iio_device_get_clock(indio_dev) != CLOCK_BOOTTIME)
+                               timestamp = iio_get_time_ns(indio_dev);
+                       state = cros_ec_mkbp_proximity_parse_state(switches);
Quoted 6 lines

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().

Quoted 2 lines

Ah ok. So we'll have to save a state tracking variable and poll the bit once at boot and then at resume time?

Required at boot: There is provision in the EC to report switch events at init mkbp_report_switch_on_init(), but that's only useful when EC reboots or transitions from RO to RW while AP is up.
No need to peek at resume time: the EC will send a switch event if the mask has changed during suspend: when the AP is sleeping, EC just put the event in a FIFO. But looking at the code, the FIFO can get full, so if no switch events can be added, we lose them - see mkbp_fifo_add()).
Then cros_ec_report_events_during_suspend() will gather these events.

Quoted 1 line

What happens to events that happen across suspend/resume? We drop them?

We should not drop them: if the user gets close while the device is suspended and she resumes it, we should be able to send that info to the user space.
EV stores switches values in a single field |mkbp_switch_state|, so 2 events that nullify themselves during suspend will be ignored.

Quoted 2 lines

Or we need to inject the last state
if it's different into IIO with the time of resume?

The notifier routine will be called. Looking at the code, the ec last_event_time is not updated, I need to fix cros_ec_resume().

Quoted 30 lines
Quoted 18 lines
Quoted 18 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +17 -0
+                       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);
Quoted 8 lines

drivers/iio/proximity/cros_ec_mkbp_proximity.c line 142

Quoted 6 lines
drivers/iio/proximity/cros_ec_mkbp_proximity.c new file +6 -0
+       struct cros_ec_device *ec = data->ec;
+
+       if (chan->type != IIO_PROXIMITY)
+               return -EINVAL;
+
+       switch (mask) {

A switch is not necessary here.

Ok.

Quoted 1 line
Quoted 1 line

+ case IIO_CHAN_INFO_RAW: