diff mbox series

[17/19] platform/x86: lenovo-yogabook: Add platform driver support

Message ID 20230429181551.98201-18-hdegoede@redhat.com
State Superseded
Headers show
Series platform/x86: lenovo-yogabook: Modify to also work on Android version | expand

Commit Message

Hans de Goede April 29, 2023, 6:15 p.m. UTC
The Lenovo Yoga Book 1 comes in 2 versions.

Version 1: The yb1-x91f/l currently supported by lenovo-yogabook-wmi, which
has a WMI interface to deal with toggling the keyboard half between
touch-keyboard and wacom-digitizer mode.

Version 2: The yb1-x90f/l which is the same hardware shipping with Android
as factory OS. This version has a very different BIOS and ACPI tables which
lack the WMI interface.

Instead the x86-android-tablets.ko code which does devices instantiation
for devices missing from ACPI on various x86 Android tablets will
instantiate a platform device for the keyboard half touch-kbd/digitizer
toggle functionality.

This patch adds a platform driver to the lenovo-yogabook code which binds
to the platform device instantiated by x86-android-tablets.ko offering
touch-kbd/digitizer toggle functionality on the Android model.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/platform/x86/lenovo-yogabook-wmi.c | 160 ++++++++++++++++++++-
 1 file changed, 156 insertions(+), 4 deletions(-)

Comments

Andy Shevchenko April 30, 2023, 10:49 a.m. UTC | #1
On Sat, Apr 29, 2023 at 9:16 PM Hans de Goede <hdegoede@redhat.com> wrote:
>
> The Lenovo Yoga Book 1 comes in 2 versions.
>
> Version 1: The yb1-x91f/l currently supported by lenovo-yogabook-wmi, which
> has a WMI interface to deal with toggling the keyboard half between
> touch-keyboard and wacom-digitizer mode.
>
> Version 2: The yb1-x90f/l which is the same hardware shipping with Android
> as factory OS. This version has a very different BIOS and ACPI tables which
> lack the WMI interface.
>
> Instead the x86-android-tablets.ko code which does devices instantiation
> for devices missing from ACPI on various x86 Android tablets will
> instantiate a platform device for the keyboard half touch-kbd/digitizer
> toggle functionality.
>
> This patch adds a platform driver to the lenovo-yogabook code which binds
> to the platform device instantiated by x86-android-tablets.ko offering
> touch-kbd/digitizer toggle functionality on the Android model.

...

> +/*
> + * Platform driver for Lenovo Yoga Book YB1-X90F/L tablets (Android model)
> + * WMI driver for Lenovo Yoga Book YB1-X91F/L tablets (Windows model)
> + *
> + * The keyboard half of the YB1 models can function as both a capactive touch

capacitive ?

> + * keyboard or as a Wacom digitizer, but not at the same time.
> + *
> + * This driver takes care of switching between the 2 functions.
> + *
> + * Copyright 2023 Hans de Goede <hansg@kernel.org>
> + */

...

>  #include <linux/devm-helpers.h>

Hmm.. TIL!

...

> +       if (IS_ERR(data->pen_touch_event)) {
> +               r = PTR_ERR(data->pen_touch_event);
> +               dev_err_probe(dev, r, "Getting pen_touch_event GPIO\n");

Combine them

  r = dev_err_probe(...);

> +               goto error_put_devs;
> +       }
> +
> +       if (IS_ERR(data->kbd_bl_led_enable)) {
> +               r = PTR_ERR(data->kbd_bl_led_enable);
> +               dev_err_probe(dev, r, "Getting enable_keyboard_led GPIO\n");

Ditto.

> +               goto error_put_devs;
> +       }

...

> +       r = gpiod_to_irq(data->pen_touch_event);
> +       if (r < 0) {
> +               dev_err_probe(dev, r, "Getting pen_touch_event IRQ\n");
> +               return r;

return dev_err_probe();

> +       }
> +       data->pen_touch_irq = r;
> +
> +       r = request_irq(data->pen_touch_irq, yogabook_pen_touch_irq, IRQF_TRIGGER_FALLING,
> +                       "pen_touch_event", data);
> +       if (r) {
> +               dev_err_probe(dev, r, "Requesting backside_hall_sw IRQ\n");
> +               return r;

Ditto.

> +       }

...

> +MODULE_ALIAS("platform:" YB_PDEV_NAME);

Hmm... Do we need this?
Hans de Goede April 30, 2023, 3:39 p.m. UTC | #2
Hi Andy,

Thank you for all the reviews.

I agree with all your comments and I will fix them
all for version 2 of this series.


On 4/30/23 12:49, Andy Shevchenko wrote:

<snip>

>> +       r = gpiod_to_irq(data->pen_touch_event);
>> +       if (r < 0) {
>> +               dev_err_probe(dev, r, "Getting pen_touch_event IRQ\n");
>> +               return r;
> 
> return dev_err_probe();
> 
>> +       }
>> +       data->pen_touch_irq = r;
>> +
>> +       r = request_irq(data->pen_touch_irq, yogabook_pen_touch_irq, IRQF_TRIGGER_FALLING,
>> +                       "pen_touch_event", data);
>> +       if (r) {
>> +               dev_err_probe(dev, r, "Requesting backside_hall_sw IRQ\n");
>> +               return r;
> 
> Ditto.

Actually this block has 2 copy and paste errors:

1. The second error message
2. Both "return r;" statements need to be replaced with goto error_put_devs.

I'll fix this both for version 2.

> 
>> +       }
> 
> ...
> 
>> +MODULE_ALIAS("platform:" YB_PDEV_NAME);
> 
> Hmm... Do we need this?

Yes simple platform_drivers like this one typically don't have
a device-id table (they simply match by name) and thus they also
don't have a MODULE_DEVICE_TABLE(platform, ...); statement so
they need an explicit MODULE_ALIAS to correctly auto-load.

Regards,

Hans


>
diff mbox series

Patch

diff --git a/drivers/platform/x86/lenovo-yogabook-wmi.c b/drivers/platform/x86/lenovo-yogabook-wmi.c
index 0c5475127ac2..afb11d25abc8 100644
--- a/drivers/platform/x86/lenovo-yogabook-wmi.c
+++ b/drivers/platform/x86/lenovo-yogabook-wmi.c
@@ -1,13 +1,25 @@ 
 // SPDX-License-Identifier: GPL-2.0
-/* WMI driver for Lenovo Yoga Book YB1-X90* / -X91* tablets */
+/*
+ * Platform driver for Lenovo Yoga Book YB1-X90F/L tablets (Android model)
+ * WMI driver for Lenovo Yoga Book YB1-X91F/L tablets (Windows model)
+ *
+ * The keyboard half of the YB1 models can function as both a capactive touch
+ * keyboard or as a Wacom digitizer, but not at the same time.
+ *
+ * This driver takes care of switching between the 2 functions.
+ *
+ * Copyright 2023 Hans de Goede <hansg@kernel.org>
+ */
 
 #include <linux/acpi.h>
 #include <linux/devm-helpers.h>
 #include <linux/gpio/consumer.h>
 #include <linux/gpio/machine.h>
+#include <linux/i2c.h>
 #include <linux/interrupt.h>
-#include <linux/module.h>
 #include <linux/leds.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
 #include <linux/wmi.h>
 #include <linux/workqueue.h>
 
@@ -16,6 +28,8 @@ 
 #define YB_KBD_BL_DEFAULT	128
 #define YB_KBD_BL_MAX		255
 
+#define YB_PDEV_NAME		"yogabook-touch-kbd-digitizer-switch"
+
 /* flags */
 enum {
 	YB_KBD_IS_ON,
@@ -32,8 +46,11 @@  struct yogabook_data {
 	struct device *kbd_dev;
 	struct device *dig_dev;
 	struct led_classdev *pen_led;
+	struct gpio_desc *pen_touch_event;
+	struct gpio_desc *kbd_bl_led_enable;
 	struct gpio_desc *backside_hall_gpio;
 	int (*set_kbd_backlight)(struct yogabook_data *data, u8 level);
+	int pen_touch_irq;
 	int backside_hall_irq;
 	struct work_struct work;
 	struct led_classdev kbd_bl_led;
@@ -282,6 +299,8 @@  static int yogabook_resume(struct device *dev)
 
 static DEFINE_SIMPLE_DEV_PM_OPS(yogabook_pm_ops, yogabook_suspend, yogabook_resume);
 
+/********** WMI driver code **********/
+
 /*
  * To control keyboard backlight, call the method KBLC() of the TCS1 ACPI
  * device (Goodix touchpad acts as virtual sensor keyboard).
@@ -402,8 +421,141 @@  static struct wmi_driver yogabook_wmi_driver = {
 	.remove = yogabook_wmi_remove,
 	.notify = yogabook_wmi_notify,
 };
-module_wmi_driver(yogabook_wmi_driver);
 
+/********** platform driver code **********/
+
+static struct gpiod_lookup_table yogabook_pdev_gpios = {
+	.dev_id = YB_PDEV_NAME,
+	.table = {
+		GPIO_LOOKUP("INT33FF:00", 95, "pen_touch_event", GPIO_ACTIVE_HIGH),
+		GPIO_LOOKUP("INT33FF:03", 52, "enable_keyboard_led", GPIO_ACTIVE_HIGH),
+		{}
+	},
+};
+
+static int yogabook_pdev_set_kbd_backlight(struct yogabook_data *data, u8 level)
+{
+	gpiod_set_value(data->kbd_bl_led_enable, level ? 1 : 0);
+	return 0;
+}
+
+static irqreturn_t yogabook_pen_touch_irq(int irq, void *data)
+{
+	yogabook_toggle_digitizer_mode(data);
+	return IRQ_HANDLED;
+}
+
+static int yogabook_pdev_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct yogabook_data *data;
+	int r;
+
+	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
+	if (data == NULL)
+		return -ENOMEM;
+
+	data->kbd_dev = bus_find_device_by_name(&i2c_bus_type, NULL, "i2c-goodix_ts");
+	if (!data->kbd_dev || !data->kbd_dev->driver) {
+		r = -EPROBE_DEFER;
+		goto error_put_devs;
+	}
+
+	data->dig_dev = bus_find_device_by_name(&i2c_bus_type, NULL, "i2c-wacom");
+	if (!data->dig_dev || !data->dig_dev->driver) {
+		r = -EPROBE_DEFER;
+		goto error_put_devs;
+	}
+
+	gpiod_add_lookup_table(&yogabook_pdev_gpios);
+	data->pen_touch_event = devm_gpiod_get(dev, "pen_touch_event", GPIOD_IN);
+	data->kbd_bl_led_enable = devm_gpiod_get(dev, "enable_keyboard_led", GPIOD_OUT_HIGH);
+	gpiod_remove_lookup_table(&yogabook_pdev_gpios);
+
+	if (IS_ERR(data->pen_touch_event)) {
+		r = PTR_ERR(data->pen_touch_event);
+		dev_err_probe(dev, r, "Getting pen_touch_event GPIO\n");
+		goto error_put_devs;
+	}
+
+	if (IS_ERR(data->kbd_bl_led_enable)) {
+		r = PTR_ERR(data->kbd_bl_led_enable);
+		dev_err_probe(dev, r, "Getting enable_keyboard_led GPIO\n");
+		goto error_put_devs;
+	}
+
+	r = gpiod_to_irq(data->pen_touch_event);
+	if (r < 0) {
+		dev_err_probe(dev, r, "Getting pen_touch_event IRQ\n");
+		return r;
+	}
+	data->pen_touch_irq = r;
+
+	r = request_irq(data->pen_touch_irq, yogabook_pen_touch_irq, IRQF_TRIGGER_FALLING,
+			"pen_touch_event", data);
+	if (r) {
+		dev_err_probe(dev, r, "Requesting backside_hall_sw IRQ\n");
+		return r;
+	}
+
+	data->set_kbd_backlight = yogabook_pdev_set_kbd_backlight;
+
+	r = yogabook_probe(dev, data, "yogabook::kbd_backlight");
+	if (r)
+		goto error_free_irq;
+
+	return 0;
+
+error_free_irq:
+	free_irq(data->pen_touch_irq, data);
+	cancel_work_sync(&data->work);
+error_put_devs:
+	put_device(data->dig_dev);
+	put_device(data->kbd_dev);
+	return r;
+}
+
+static void yogabook_pdev_remove(struct platform_device *pdev)
+{
+	struct yogabook_data *data = platform_get_drvdata(pdev);
+
+	yogabook_remove(data);
+	free_irq(data->pen_touch_irq, data);
+	cancel_work_sync(&data->work);
+	put_device(data->dig_dev);
+	put_device(data->kbd_dev);
+}
+
+static struct platform_driver yogabook_pdev_driver = {
+	.probe = yogabook_pdev_probe,
+	.remove_new = yogabook_pdev_remove,
+	.driver = {
+		.name = YB_PDEV_NAME,
+		.pm = pm_sleep_ptr(&yogabook_pm_ops),
+	},
+};
+
+static int __init yogabook_module_init(void)
+{
+	int r;
+
+	r = wmi_driver_register(&yogabook_wmi_driver);
+	if (r)
+		return r;
+
+	return platform_driver_register(&yogabook_pdev_driver);
+}
+
+static void __exit yogabook_module_exit(void)
+{
+	platform_driver_unregister(&yogabook_pdev_driver);
+	wmi_driver_unregister(&yogabook_wmi_driver);
+}
+
+module_init(yogabook_module_init);
+module_exit(yogabook_module_exit);
+
+MODULE_ALIAS("platform:" YB_PDEV_NAME);
 MODULE_AUTHOR("Yauhen Kharuzhy");
-MODULE_DESCRIPTION("Lenovo Yoga Book WMI driver");
+MODULE_DESCRIPTION("Lenovo Yoga Book driver");
 MODULE_LICENSE("GPL v2");