diff mbox series

[v3] drm/fb-helper: Detect when lid is closed during initialization

Message ID 20240613051700.1112-1-mario.limonciello@amd.com (mailing list archive)
State New, archived
Headers show
Series [v3] drm/fb-helper: Detect when lid is closed during initialization | expand

Commit Message

Mario Limonciello June 13, 2024, 5:17 a.m. UTC
If the lid on a laptop is closed when eDP connectors are populated
then it remains enabled when the initial framebuffer configuration
is built.

When creating the initial framebuffer configuration detect the
lid status and if it's closed disable any eDP connectors.

Also set up a workqueue to monitor for any future lid events.

Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Reported-by: Chris Bainbridge <chris.bainbridge@gmail.com>
Closes: https://gitlab.freedesktop.org/drm/amd/-/issues/3349
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
v2->v3:
 * Use input device instead of ACPI device
 * Detect lid open/close events
---
 drivers/gpu/drm/drm_client_modeset.c |  29 ++++++
 drivers/gpu/drm/drm_fb_helper.c      | 132 +++++++++++++++++++++++++++
 include/drm/drm_device.h             |   6 ++
 include/drm/drm_fb_helper.h          |   2 +
 4 files changed, 169 insertions(+)

Comments

kernel test robot June 14, 2024, 7:28 a.m. UTC | #1
Hi Mario,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm-misc/drm-misc-next]
[also build test ERROR on linus/master v6.10-rc3 next-20240613]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mario-Limonciello/drm-fb-helper-Detect-when-lid-is-closed-during-initialization/20240613-132009
base:   git://anongit.freedesktop.org/drm/drm-misc drm-misc-next
patch link:    https://lore.kernel.org/r/20240613051700.1112-1-mario.limonciello%40amd.com
patch subject: [PATCH v3] drm/fb-helper: Detect when lid is closed during initialization
config: sparc-randconfig-001-20240614 (https://download.01.org/0day-ci/archive/20240614/202406141545.9xBa6XY4-lkp@intel.com/config)
compiler: sparc64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240614/202406141545.9xBa6XY4-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202406141545.9xBa6XY4-lkp@intel.com/

All errors (new ones prefixed by >>):

   sparc64-linux-ld: drivers/gpu/drm/drm_fb_helper.o: in function `drm_fb_helper_lid_disconnect':
>> drm_fb_helper.c:(.text+0x76c): undefined reference to `input_close_device'
>> sparc64-linux-ld: drm_fb_helper.c:(.text+0x774): undefined reference to `input_unregister_handle'
   sparc64-linux-ld: drivers/gpu/drm/drm_fb_helper.o: in function `drm_fb_helper_lid_connect':
>> drm_fb_helper.c:(.text+0xed8): undefined reference to `input_register_handle'
>> sparc64-linux-ld: drm_fb_helper.c:(.text+0xf24): undefined reference to `input_open_device'
   sparc64-linux-ld: drm_fb_helper.c:(.text+0xf9c): undefined reference to `input_unregister_handle'
   sparc64-linux-ld: drivers/gpu/drm/drm_fb_helper.o: in function `__drm_fb_helper_initial_config_and_unlock':
>> drm_fb_helper.c:(.text+0x1d2c): undefined reference to `input_register_handler'
   sparc64-linux-ld: drivers/gpu/drm/drm_fb_helper.o: in function `drm_fb_helper_fini':
>> drm_fb_helper.c:(.text+0x2ce4): undefined reference to `input_unregister_handler'
Thomas Zimmermann June 14, 2024, 8:15 a.m. UTC | #2
Hi Mario

Am 13.06.24 um 07:17 schrieb Mario Limonciello:
> If the lid on a laptop is closed when eDP connectors are populated
> then it remains enabled when the initial framebuffer configuration
> is built.
>
> When creating the initial framebuffer configuration detect the
> lid status and if it's closed disable any eDP connectors.
>
> Also set up a workqueue to monitor for any future lid events.

After reading through this patchset, I think fbdev emulation is not the 
right place for this code, as lid state is global.

You could put this into drm_client_modeset.c and track lid state per 
client. drm_fb_helper_lid_work() would call the client's hotplug 
callback. But preferable, lid state should be tracked per DRM device in 
struct drm_mode_config and call drm_client_dev_hotplug() on each 
lid-state event. Thoughts? Best regards Thomas
>
> Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> Reported-by: Chris Bainbridge <chris.bainbridge@gmail.com>
> Closes: https://gitlab.freedesktop.org/drm/amd/-/issues/3349
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
> v2->v3:
>   * Use input device instead of ACPI device
>   * Detect lid open/close events
> ---
>   drivers/gpu/drm/drm_client_modeset.c |  29 ++++++
>   drivers/gpu/drm/drm_fb_helper.c      | 132 +++++++++++++++++++++++++++
>   include/drm/drm_device.h             |   6 ++
>   include/drm/drm_fb_helper.h          |   2 +
>   4 files changed, 169 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_client_modeset.c b/drivers/gpu/drm/drm_client_modeset.c
> index 31af5cf37a09..b8adfe87334b 100644
> --- a/drivers/gpu/drm/drm_client_modeset.c
> +++ b/drivers/gpu/drm/drm_client_modeset.c
> @@ -257,6 +257,34 @@ static void drm_client_connectors_enabled(struct drm_connector **connectors,
>   		enabled[i] = drm_connector_enabled(connectors[i], false);
>   }
>   
> +static void drm_client_match_edp_lid(struct drm_device *dev,
> +				     struct drm_connector **connectors,
> +				     unsigned int connector_count,
> +				     bool *enabled)
> +{
> +	int i;
> +
> +	for (i = 0; i < connector_count; i++) {
> +		struct drm_connector *connector = connectors[i];
> +
> +		switch (connector->connector_type) {
> +		case DRM_MODE_CONNECTOR_LVDS:
> +		case DRM_MODE_CONNECTOR_eDP:
> +			if (!enabled[i])
> +				continue;
> +			break;
> +		default:
> +			continue;
> +		}
> +
> +		if (dev->lid_closed) {
> +			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] lid is closed, disabling\n",
> +				    connector->base.id, connector->name);
> +			enabled[i] = false;
> +		}
> +	}
> +}
> +
>   static bool drm_client_target_cloned(struct drm_device *dev,
>   				     struct drm_connector **connectors,
>   				     unsigned int connector_count,
> @@ -844,6 +872,7 @@ int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width,
>   		memset(crtcs, 0, connector_count * sizeof(*crtcs));
>   		memset(offsets, 0, connector_count * sizeof(*offsets));
>   
> +		drm_client_match_edp_lid(dev, connectors, connector_count, enabled);
>   		if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
>   					      offsets, enabled, width, height) &&
>   		    !drm_client_target_preferred(dev, connectors, connector_count, modes,
> diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c
> index d612133e2cf7..41dd5887599a 100644
> --- a/drivers/gpu/drm/drm_fb_helper.c
> +++ b/drivers/gpu/drm/drm_fb_helper.c
> @@ -30,6 +30,8 @@
>   #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>   
>   #include <linux/console.h>
> +#include <linux/input.h>
> +#include <linux/mod_devicetable.h>
>   #include <linux/pci.h>
>   #include <linux/sysrq.h>
>   #include <linux/vga_switcheroo.h>
> @@ -413,6 +415,128 @@ static void drm_fb_helper_damage_work(struct work_struct *work)
>   	drm_fb_helper_fb_dirty(helper);
>   }
>   
> +static void drm_fb_helper_lid_event(struct input_handle *handle, unsigned int type,
> +				    unsigned int code, int value)
> +{
> +	if (type == EV_SW && code == SW_LID) {
> +		struct drm_fb_helper *fb_helper = handle->handler->private;
> +
> +		if (value != fb_helper->dev->lid_closed) {
> +			fb_helper->dev->lid_closed = value;
> +			queue_work(fb_helper->input_wq, &fb_helper->lid_work);
> +		}
> +	}
> +}
> +
> +struct drm_fb_lid {
> +	struct input_handle handle;
> +};
> +
> +static int drm_fb_helper_lid_connect(struct input_handler *handler,
> +				     struct input_dev *dev,
> +				     const struct input_device_id *id)
> +{
> +	struct drm_fb_helper *fb_helper = handler->private;
> +	struct drm_fb_lid *lid;
> +	char *name;
> +	int error;
> +
> +	lid = kzalloc(sizeof(*lid), GFP_KERNEL);
> +	if (!lid)
> +		return -ENOMEM;
> +
> +	name = kasprintf(GFP_KERNEL, "drm-fb-helper-lid-%s", dev_name(&dev->dev));
> +	if (!name) {
> +		error = -ENOMEM;
> +		goto err_free_lid;
> +	}
> +
> +	lid->handle.dev = dev;
> +	lid->handle.handler = handler;
> +	lid->handle.name = name;
> +	lid->handle.private = lid;
> +
> +	error = input_register_handle(&lid->handle);
> +	if (error)
> +		goto err_free_name;
> +
> +	error = input_open_device(&lid->handle);
> +	if (error)
> +		goto err_unregister_handle;
> +
> +	fb_helper->dev->lid_closed = dev->sw[SW_LID];
> +	drm_dbg_kms(fb_helper->dev, "initial lid state is set to %d\n", fb_helper->dev->lid_closed);
> +
> +	return 0;
> +
> +err_unregister_handle:
> +	input_unregister_handle(&lid->handle);
> +err_free_name:
> +	kfree(name);
> +err_free_lid:
> +	kfree(lid);
> +	return error;
> +}
> +
> +static void drm_fb_helper_lid_disconnect(struct input_handle *handle)
> +{
> +	struct drm_fb_lid *lid = handle->private;
> +
> +	input_close_device(handle);
> +	input_unregister_handle(handle);
> +
> +	kfree(handle->name);
> +	kfree(lid);
> +}
> +
> +static const struct input_device_id drm_fb_helper_lid_ids[] = {
> +	{
> +		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
> +		.evbit = { BIT_MASK(EV_SW) },
> +		.swbit = { [BIT_WORD(SW_LID)] = BIT_MASK(SW_LID) },
> +	},
> +	{ },
> +};
> +
> +static struct input_handler drm_fb_helper_lid_handler = {
> +	.event =	drm_fb_helper_lid_event,
> +	.connect =	drm_fb_helper_lid_connect,
> +	.disconnect =	drm_fb_helper_lid_disconnect,
> +	.name =		"drm-fb-helper-lid",
> +	.id_table =	drm_fb_helper_lid_ids,
> +};
> +
> +static void drm_fb_helper_lid_work(struct work_struct *work)
> +{
> +	struct drm_fb_helper *fb_helper = container_of(work, struct drm_fb_helper,
> +						       lid_work);
> +	drm_fb_helper_hotplug_event(fb_helper);
> +}
> +
> +static int drm_fb_helper_create_lid_handler(struct drm_fb_helper *fb_helper)
> +{
> +	int ret = 0;
> +
> +	if (fb_helper->deferred_setup)
> +		return 0;
> +
> +	fb_helper->input_wq = create_singlethread_workqueue("drm-fb-lid");
> +	if (fb_helper->input_wq == NULL)
> +		return -ENOMEM;
> +
> +	drm_fb_helper_lid_handler.private = fb_helper;
> +	ret = input_register_handler(&drm_fb_helper_lid_handler);
> +	if (ret)
> +		goto remove_wq;
> +
> +	return 0;
> +
> +remove_wq:
> +	destroy_workqueue(fb_helper->input_wq);
> +	fb_helper->input_wq = NULL;
> +	return ret;
> +}
> +
>   /**
>    * drm_fb_helper_prepare - setup a drm_fb_helper structure
>    * @dev: DRM device
> @@ -445,6 +569,7 @@ void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
>   	spin_lock_init(&helper->damage_lock);
>   	INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
>   	INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
> +	INIT_WORK(&helper->lid_work, drm_fb_helper_lid_work);
>   	helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
>   	mutex_init(&helper->lock);
>   	helper->funcs = funcs;
> @@ -593,6 +718,9 @@ void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
>   	if (!drm_fbdev_emulation)
>   		return;
>   
> +	input_unregister_handler(&drm_fb_helper_lid_handler);
> +	destroy_workqueue(fb_helper->input_wq);
> +
>   	cancel_work_sync(&fb_helper->resume_work);
>   	cancel_work_sync(&fb_helper->damage_work);
>   
> @@ -1842,6 +1970,10 @@ __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper)
>   	width = dev->mode_config.max_width;
>   	height = dev->mode_config.max_height;
>   
> +	ret = drm_fb_helper_create_lid_handler(fb_helper);
> +	if (ret)
> +		return ret;
> +
>   	drm_client_modeset_probe(&fb_helper->client, width, height);
>   	ret = drm_fb_helper_single_fb_probe(fb_helper);
>   	if (ret < 0) {
> diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h
> index 63767cf24371..619af597784c 100644
> --- a/include/drm/drm_device.h
> +++ b/include/drm/drm_device.h
> @@ -316,6 +316,12 @@ struct drm_device {
>   	 * Root directory for debugfs files.
>   	 */
>   	struct dentry *debugfs_root;
> +
> +	/**
> +	 * @lid_closed: Flag to tell the lid switch state
> +	 */
> +	bool lid_closed;
> +
>   };
>   
>   #endif
> diff --git a/include/drm/drm_fb_helper.h b/include/drm/drm_fb_helper.h
> index 375737fd6c36..7fb36c10299d 100644
> --- a/include/drm/drm_fb_helper.h
> +++ b/include/drm/drm_fb_helper.h
> @@ -143,6 +143,8 @@ struct drm_fb_helper {
>   	spinlock_t damage_lock;
>   	struct work_struct damage_work;
>   	struct work_struct resume_work;
> +	struct work_struct lid_work;
> +	struct workqueue_struct *input_wq;
>   
>   	/**
>   	 * @lock:
Mario Limonciello June 14, 2024, 1:47 p.m. UTC | #3
On 6/14/2024 03:15, Thomas Zimmermann wrote:
> Hi Mario
> 
> Am 13.06.24 um 07:17 schrieb Mario Limonciello:
>> If the lid on a laptop is closed when eDP connectors are populated
>> then it remains enabled when the initial framebuffer configuration
>> is built.
>>
>> When creating the initial framebuffer configuration detect the
>> lid status and if it's closed disable any eDP connectors.
>>
>> Also set up a workqueue to monitor for any future lid events.
> 
> After reading through this patchset, I think fbdev emulation is not the 
> right place for this code, as lid state is global.
> 
> You could put this into drm_client_modeset.c and track lid state per 
> client. drm_fb_helper_lid_work() would call the client's hotplug 
> callback. But preferable, lid state should be tracked per DRM device in 
> struct drm_mode_config and call drm_client_dev_hotplug() on each 
> lid-state event. Thoughts? Best regards Thomas

This is pretty similar to what I first did when moving from ACPI over to 
generic input switch.

It works for the initial configuration.  But I don't believe it makes 
sense for the lid switch events because not all DRM clients will "want" 
to respond to the lid switch events.  By leaving it up to the client for 
everything except fbdev emulation they can also track the lid switch and 
decide the policy.

I also worry about what happens if the kernel does a hotplug callback on 
lid events as well at the client choosing to do it.  Don't we end up 
with two modesets?  So then I would think you need a handshake of some 
sort to decide whether to do it for a given client where fbdev emulation 
would opt in and then all other clients can choose to opt in or not.

>>
>> Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
>> Reported-by: Chris Bainbridge <chris.bainbridge@gmail.com>
>> Closes: https://gitlab.freedesktop.org/drm/amd/-/issues/3349
>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>> ---
>> v2->v3:
>>   * Use input device instead of ACPI device
>>   * Detect lid open/close events
>> ---
>>   drivers/gpu/drm/drm_client_modeset.c |  29 ++++++
>>   drivers/gpu/drm/drm_fb_helper.c      | 132 +++++++++++++++++++++++++++
>>   include/drm/drm_device.h             |   6 ++
>>   include/drm/drm_fb_helper.h          |   2 +
>>   4 files changed, 169 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/drm_client_modeset.c 
>> b/drivers/gpu/drm/drm_client_modeset.c
>> index 31af5cf37a09..b8adfe87334b 100644
>> --- a/drivers/gpu/drm/drm_client_modeset.c
>> +++ b/drivers/gpu/drm/drm_client_modeset.c
>> @@ -257,6 +257,34 @@ static void drm_client_connectors_enabled(struct 
>> drm_connector **connectors,
>>           enabled[i] = drm_connector_enabled(connectors[i], false);
>>   }
>> +static void drm_client_match_edp_lid(struct drm_device *dev,
>> +                     struct drm_connector **connectors,
>> +                     unsigned int connector_count,
>> +                     bool *enabled)
>> +{
>> +    int i;
>> +
>> +    for (i = 0; i < connector_count; i++) {
>> +        struct drm_connector *connector = connectors[i];
>> +
>> +        switch (connector->connector_type) {
>> +        case DRM_MODE_CONNECTOR_LVDS:
>> +        case DRM_MODE_CONNECTOR_eDP:
>> +            if (!enabled[i])
>> +                continue;
>> +            break;
>> +        default:
>> +            continue;
>> +        }
>> +
>> +        if (dev->lid_closed) {
>> +            drm_dbg_kms(dev, "[CONNECTOR:%d:%s] lid is closed, 
>> disabling\n",
>> +                    connector->base.id, connector->name);
>> +            enabled[i] = false;
>> +        }
>> +    }
>> +}
>> +
>>   static bool drm_client_target_cloned(struct drm_device *dev,
>>                        struct drm_connector **connectors,
>>                        unsigned int connector_count,
>> @@ -844,6 +872,7 @@ int drm_client_modeset_probe(struct drm_client_dev 
>> *client, unsigned int width,
>>           memset(crtcs, 0, connector_count * sizeof(*crtcs));
>>           memset(offsets, 0, connector_count * sizeof(*offsets));
>> +        drm_client_match_edp_lid(dev, connectors, connector_count, 
>> enabled);
>>           if (!drm_client_target_cloned(dev, connectors, 
>> connector_count, modes,
>>                             offsets, enabled, width, height) &&
>>               !drm_client_target_preferred(dev, connectors, 
>> connector_count, modes,
>> diff --git a/drivers/gpu/drm/drm_fb_helper.c 
>> b/drivers/gpu/drm/drm_fb_helper.c
>> index d612133e2cf7..41dd5887599a 100644
>> --- a/drivers/gpu/drm/drm_fb_helper.c
>> +++ b/drivers/gpu/drm/drm_fb_helper.c
>> @@ -30,6 +30,8 @@
>>   #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>>   #include <linux/console.h>
>> +#include <linux/input.h>
>> +#include <linux/mod_devicetable.h>
>>   #include <linux/pci.h>
>>   #include <linux/sysrq.h>
>>   #include <linux/vga_switcheroo.h>
>> @@ -413,6 +415,128 @@ static void drm_fb_helper_damage_work(struct 
>> work_struct *work)
>>       drm_fb_helper_fb_dirty(helper);
>>   }
>> +static void drm_fb_helper_lid_event(struct input_handle *handle, 
>> unsigned int type,
>> +                    unsigned int code, int value)
>> +{
>> +    if (type == EV_SW && code == SW_LID) {
>> +        struct drm_fb_helper *fb_helper = handle->handler->private;
>> +
>> +        if (value != fb_helper->dev->lid_closed) {
>> +            fb_helper->dev->lid_closed = value;
>> +            queue_work(fb_helper->input_wq, &fb_helper->lid_work);
>> +        }
>> +    }
>> +}
>> +
>> +struct drm_fb_lid {
>> +    struct input_handle handle;
>> +};
>> +
>> +static int drm_fb_helper_lid_connect(struct input_handler *handler,
>> +                     struct input_dev *dev,
>> +                     const struct input_device_id *id)
>> +{
>> +    struct drm_fb_helper *fb_helper = handler->private;
>> +    struct drm_fb_lid *lid;
>> +    char *name;
>> +    int error;
>> +
>> +    lid = kzalloc(sizeof(*lid), GFP_KERNEL);
>> +    if (!lid)
>> +        return -ENOMEM;
>> +
>> +    name = kasprintf(GFP_KERNEL, "drm-fb-helper-lid-%s", 
>> dev_name(&dev->dev));
>> +    if (!name) {
>> +        error = -ENOMEM;
>> +        goto err_free_lid;
>> +    }
>> +
>> +    lid->handle.dev = dev;
>> +    lid->handle.handler = handler;
>> +    lid->handle.name = name;
>> +    lid->handle.private = lid;
>> +
>> +    error = input_register_handle(&lid->handle);
>> +    if (error)
>> +        goto err_free_name;
>> +
>> +    error = input_open_device(&lid->handle);
>> +    if (error)
>> +        goto err_unregister_handle;
>> +
>> +    fb_helper->dev->lid_closed = dev->sw[SW_LID];
>> +    drm_dbg_kms(fb_helper->dev, "initial lid state is set to %d\n", 
>> fb_helper->dev->lid_closed);
>> +
>> +    return 0;
>> +
>> +err_unregister_handle:
>> +    input_unregister_handle(&lid->handle);
>> +err_free_name:
>> +    kfree(name);
>> +err_free_lid:
>> +    kfree(lid);
>> +    return error;
>> +}
>> +
>> +static void drm_fb_helper_lid_disconnect(struct input_handle *handle)
>> +{
>> +    struct drm_fb_lid *lid = handle->private;
>> +
>> +    input_close_device(handle);
>> +    input_unregister_handle(handle);
>> +
>> +    kfree(handle->name);
>> +    kfree(lid);
>> +}
>> +
>> +static const struct input_device_id drm_fb_helper_lid_ids[] = {
>> +    {
>> +        .flags = INPUT_DEVICE_ID_MATCH_EVBIT | 
>> INPUT_DEVICE_ID_MATCH_SWBIT,
>> +        .evbit = { BIT_MASK(EV_SW) },
>> +        .swbit = { [BIT_WORD(SW_LID)] = BIT_MASK(SW_LID) },
>> +    },
>> +    { },
>> +};
>> +
>> +static struct input_handler drm_fb_helper_lid_handler = {
>> +    .event =    drm_fb_helper_lid_event,
>> +    .connect =    drm_fb_helper_lid_connect,
>> +    .disconnect =    drm_fb_helper_lid_disconnect,
>> +    .name =        "drm-fb-helper-lid",
>> +    .id_table =    drm_fb_helper_lid_ids,
>> +};
>> +
>> +static void drm_fb_helper_lid_work(struct work_struct *work)
>> +{
>> +    struct drm_fb_helper *fb_helper = container_of(work, struct 
>> drm_fb_helper,
>> +                               lid_work);
>> +    drm_fb_helper_hotplug_event(fb_helper);
>> +}
>> +
>> +static int drm_fb_helper_create_lid_handler(struct drm_fb_helper 
>> *fb_helper)
>> +{
>> +    int ret = 0;
>> +
>> +    if (fb_helper->deferred_setup)
>> +        return 0;
>> +
>> +    fb_helper->input_wq = create_singlethread_workqueue("drm-fb-lid");
>> +    if (fb_helper->input_wq == NULL)
>> +        return -ENOMEM;
>> +
>> +    drm_fb_helper_lid_handler.private = fb_helper;
>> +    ret = input_register_handler(&drm_fb_helper_lid_handler);
>> +    if (ret)
>> +        goto remove_wq;
>> +
>> +    return 0;
>> +
>> +remove_wq:
>> +    destroy_workqueue(fb_helper->input_wq);
>> +    fb_helper->input_wq = NULL;
>> +    return ret;
>> +}
>> +
>>   /**
>>    * drm_fb_helper_prepare - setup a drm_fb_helper structure
>>    * @dev: DRM device
>> @@ -445,6 +569,7 @@ void drm_fb_helper_prepare(struct drm_device *dev, 
>> struct drm_fb_helper *helper,
>>       spin_lock_init(&helper->damage_lock);
>>       INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
>>       INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
>> +    INIT_WORK(&helper->lid_work, drm_fb_helper_lid_work);
>>       helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
>>       mutex_init(&helper->lock);
>>       helper->funcs = funcs;
>> @@ -593,6 +718,9 @@ void drm_fb_helper_fini(struct drm_fb_helper 
>> *fb_helper)
>>       if (!drm_fbdev_emulation)
>>           return;
>> +    input_unregister_handler(&drm_fb_helper_lid_handler);
>> +    destroy_workqueue(fb_helper->input_wq);
>> +
>>       cancel_work_sync(&fb_helper->resume_work);
>>       cancel_work_sync(&fb_helper->damage_work);
>> @@ -1842,6 +1970,10 @@ 
>> __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper 
>> *fb_helper)
>>       width = dev->mode_config.max_width;
>>       height = dev->mode_config.max_height;
>> +    ret = drm_fb_helper_create_lid_handler(fb_helper);
>> +    if (ret)
>> +        return ret;
>> +
>>       drm_client_modeset_probe(&fb_helper->client, width, height);
>>       ret = drm_fb_helper_single_fb_probe(fb_helper);
>>       if (ret < 0) {
>> diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h
>> index 63767cf24371..619af597784c 100644
>> --- a/include/drm/drm_device.h
>> +++ b/include/drm/drm_device.h
>> @@ -316,6 +316,12 @@ struct drm_device {
>>        * Root directory for debugfs files.
>>        */
>>       struct dentry *debugfs_root;
>> +
>> +    /**
>> +     * @lid_closed: Flag to tell the lid switch state
>> +     */
>> +    bool lid_closed;
>> +
>>   };
>>   #endif
>> diff --git a/include/drm/drm_fb_helper.h b/include/drm/drm_fb_helper.h
>> index 375737fd6c36..7fb36c10299d 100644
>> --- a/include/drm/drm_fb_helper.h
>> +++ b/include/drm/drm_fb_helper.h
>> @@ -143,6 +143,8 @@ struct drm_fb_helper {
>>       spinlock_t damage_lock;
>>       struct work_struct damage_work;
>>       struct work_struct resume_work;
>> +    struct work_struct lid_work;
>> +    struct workqueue_struct *input_wq;
>>       /**
>>        * @lock:
>
Thomas Zimmermann June 14, 2024, 2:17 p.m. UTC | #4
Hi

Am 14.06.24 um 15:47 schrieb Mario Limonciello:
> On 6/14/2024 03:15, Thomas Zimmermann wrote:
>> Hi Mario
>>
>> Am 13.06.24 um 07:17 schrieb Mario Limonciello:
>>> If the lid on a laptop is closed when eDP connectors are populated
>>> then it remains enabled when the initial framebuffer configuration
>>> is built.
>>>
>>> When creating the initial framebuffer configuration detect the
>>> lid status and if it's closed disable any eDP connectors.
>>>
>>> Also set up a workqueue to monitor for any future lid events.
>>
>> After reading through this patchset, I think fbdev emulation is not 
>> the right place for this code, as lid state is global.
>>
>> You could put this into drm_client_modeset.c and track lid state per 
>> client. drm_fb_helper_lid_work() would call the client's hotplug 
>> callback. But preferable, lid state should be tracked per DRM device 
>> in struct drm_mode_config and call drm_client_dev_hotplug() on each 
>> lid-state event. Thoughts? Best regards Thomas
>
> This is pretty similar to what I first did when moving from ACPI over 
> to generic input switch.
>
> It works for the initial configuration.  But I don't believe it makes 
> sense for the lid switch events because not all DRM clients will 
> "want" to respond to the lid switch events.  By leaving it up to the 
> client for everything except fbdev emulation they can also track the 
> lid switch and decide the policy.


All our current clients do fbdev emulation, possibly others would be the 
panic screen and a boot-up logo. A panic screen doesn't do actual mode 
setting, but any other client would most likely want enable and disable 
the display depending on the lid state. Having this code in the DRM 
client helpers make perfect sense. But as it's global state, it makes no 
sense to set this up per client. Hence the suggestion to manage this in 
per DRM device.

It would also make sense to try to integrate this into the probe 
helpers. When the lid state changes, the probe helpers would invoke the 
driver's regular hotplugging code.


>
> I also worry about what happens if the kernel does a hotplug callback 
> on lid events as well at the client choosing to do it. Don't we end up 
> with two modesets?  So then I would think you need a handshake of some 
> sort to decide whether to do it for a given client where fbdev 
> emulation would opt in and then all other clients can choose to opt in 
> or not.


What do you mean by the kernel does a hotplug event and the client does 
one? There should really only be one place to handle all of this. If we 
end up with two modesets, we'd get an additional flicker when the lid 
gets opened.


Best regards
Thomas

>
>>>
>>> Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
>>> Reported-by: Chris Bainbridge <chris.bainbridge@gmail.com>
>>> Closes: https://gitlab.freedesktop.org/drm/amd/-/issues/3349
>>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>>> ---
>>> v2->v3:
>>>   * Use input device instead of ACPI device
>>>   * Detect lid open/close events
>>> ---
>>>   drivers/gpu/drm/drm_client_modeset.c |  29 ++++++
>>>   drivers/gpu/drm/drm_fb_helper.c      | 132 
>>> +++++++++++++++++++++++++++
>>>   include/drm/drm_device.h             |   6 ++
>>>   include/drm/drm_fb_helper.h          |   2 +
>>>   4 files changed, 169 insertions(+)
>>>
>>> diff --git a/drivers/gpu/drm/drm_client_modeset.c 
>>> b/drivers/gpu/drm/drm_client_modeset.c
>>> index 31af5cf37a09..b8adfe87334b 100644
>>> --- a/drivers/gpu/drm/drm_client_modeset.c
>>> +++ b/drivers/gpu/drm/drm_client_modeset.c
>>> @@ -257,6 +257,34 @@ static void 
>>> drm_client_connectors_enabled(struct drm_connector **connectors,
>>>           enabled[i] = drm_connector_enabled(connectors[i], false);
>>>   }
>>> +static void drm_client_match_edp_lid(struct drm_device *dev,
>>> +                     struct drm_connector **connectors,
>>> +                     unsigned int connector_count,
>>> +                     bool *enabled)
>>> +{
>>> +    int i;
>>> +
>>> +    for (i = 0; i < connector_count; i++) {
>>> +        struct drm_connector *connector = connectors[i];
>>> +
>>> +        switch (connector->connector_type) {
>>> +        case DRM_MODE_CONNECTOR_LVDS:
>>> +        case DRM_MODE_CONNECTOR_eDP:
>>> +            if (!enabled[i])
>>> +                continue;
>>> +            break;
>>> +        default:
>>> +            continue;
>>> +        }
>>> +
>>> +        if (dev->lid_closed) {
>>> +            drm_dbg_kms(dev, "[CONNECTOR:%d:%s] lid is closed, 
>>> disabling\n",
>>> +                    connector->base.id, connector->name);
>>> +            enabled[i] = false;
>>> +        }
>>> +    }
>>> +}
>>> +
>>>   static bool drm_client_target_cloned(struct drm_device *dev,
>>>                        struct drm_connector **connectors,
>>>                        unsigned int connector_count,
>>> @@ -844,6 +872,7 @@ int drm_client_modeset_probe(struct 
>>> drm_client_dev *client, unsigned int width,
>>>           memset(crtcs, 0, connector_count * sizeof(*crtcs));
>>>           memset(offsets, 0, connector_count * sizeof(*offsets));
>>> +        drm_client_match_edp_lid(dev, connectors, connector_count, 
>>> enabled);
>>>           if (!drm_client_target_cloned(dev, connectors, 
>>> connector_count, modes,
>>>                             offsets, enabled, width, height) &&
>>>               !drm_client_target_preferred(dev, connectors, 
>>> connector_count, modes,
>>> diff --git a/drivers/gpu/drm/drm_fb_helper.c 
>>> b/drivers/gpu/drm/drm_fb_helper.c
>>> index d612133e2cf7..41dd5887599a 100644
>>> --- a/drivers/gpu/drm/drm_fb_helper.c
>>> +++ b/drivers/gpu/drm/drm_fb_helper.c
>>> @@ -30,6 +30,8 @@
>>>   #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>>>   #include <linux/console.h>
>>> +#include <linux/input.h>
>>> +#include <linux/mod_devicetable.h>
>>>   #include <linux/pci.h>
>>>   #include <linux/sysrq.h>
>>>   #include <linux/vga_switcheroo.h>
>>> @@ -413,6 +415,128 @@ static void drm_fb_helper_damage_work(struct 
>>> work_struct *work)
>>>       drm_fb_helper_fb_dirty(helper);
>>>   }
>>> +static void drm_fb_helper_lid_event(struct input_handle *handle, 
>>> unsigned int type,
>>> +                    unsigned int code, int value)
>>> +{
>>> +    if (type == EV_SW && code == SW_LID) {
>>> +        struct drm_fb_helper *fb_helper = handle->handler->private;
>>> +
>>> +        if (value != fb_helper->dev->lid_closed) {
>>> +            fb_helper->dev->lid_closed = value;
>>> +            queue_work(fb_helper->input_wq, &fb_helper->lid_work);
>>> +        }
>>> +    }
>>> +}
>>> +
>>> +struct drm_fb_lid {
>>> +    struct input_handle handle;
>>> +};
>>> +
>>> +static int drm_fb_helper_lid_connect(struct input_handler *handler,
>>> +                     struct input_dev *dev,
>>> +                     const struct input_device_id *id)
>>> +{
>>> +    struct drm_fb_helper *fb_helper = handler->private;
>>> +    struct drm_fb_lid *lid;
>>> +    char *name;
>>> +    int error;
>>> +
>>> +    lid = kzalloc(sizeof(*lid), GFP_KERNEL);
>>> +    if (!lid)
>>> +        return -ENOMEM;
>>> +
>>> +    name = kasprintf(GFP_KERNEL, "drm-fb-helper-lid-%s", 
>>> dev_name(&dev->dev));
>>> +    if (!name) {
>>> +        error = -ENOMEM;
>>> +        goto err_free_lid;
>>> +    }
>>> +
>>> +    lid->handle.dev = dev;
>>> +    lid->handle.handler = handler;
>>> +    lid->handle.name = name;
>>> +    lid->handle.private = lid;
>>> +
>>> +    error = input_register_handle(&lid->handle);
>>> +    if (error)
>>> +        goto err_free_name;
>>> +
>>> +    error = input_open_device(&lid->handle);
>>> +    if (error)
>>> +        goto err_unregister_handle;
>>> +
>>> +    fb_helper->dev->lid_closed = dev->sw[SW_LID];
>>> +    drm_dbg_kms(fb_helper->dev, "initial lid state is set to %d\n", 
>>> fb_helper->dev->lid_closed);
>>> +
>>> +    return 0;
>>> +
>>> +err_unregister_handle:
>>> +    input_unregister_handle(&lid->handle);
>>> +err_free_name:
>>> +    kfree(name);
>>> +err_free_lid:
>>> +    kfree(lid);
>>> +    return error;
>>> +}
>>> +
>>> +static void drm_fb_helper_lid_disconnect(struct input_handle *handle)
>>> +{
>>> +    struct drm_fb_lid *lid = handle->private;
>>> +
>>> +    input_close_device(handle);
>>> +    input_unregister_handle(handle);
>>> +
>>> +    kfree(handle->name);
>>> +    kfree(lid);
>>> +}
>>> +
>>> +static const struct input_device_id drm_fb_helper_lid_ids[] = {
>>> +    {
>>> +        .flags = INPUT_DEVICE_ID_MATCH_EVBIT | 
>>> INPUT_DEVICE_ID_MATCH_SWBIT,
>>> +        .evbit = { BIT_MASK(EV_SW) },
>>> +        .swbit = { [BIT_WORD(SW_LID)] = BIT_MASK(SW_LID) },
>>> +    },
>>> +    { },
>>> +};
>>> +
>>> +static struct input_handler drm_fb_helper_lid_handler = {
>>> +    .event =    drm_fb_helper_lid_event,
>>> +    .connect =    drm_fb_helper_lid_connect,
>>> +    .disconnect =    drm_fb_helper_lid_disconnect,
>>> +    .name =        "drm-fb-helper-lid",
>>> +    .id_table =    drm_fb_helper_lid_ids,
>>> +};
>>> +
>>> +static void drm_fb_helper_lid_work(struct work_struct *work)
>>> +{
>>> +    struct drm_fb_helper *fb_helper = container_of(work, struct 
>>> drm_fb_helper,
>>> +                               lid_work);
>>> +    drm_fb_helper_hotplug_event(fb_helper);
>>> +}
>>> +
>>> +static int drm_fb_helper_create_lid_handler(struct drm_fb_helper 
>>> *fb_helper)
>>> +{
>>> +    int ret = 0;
>>> +
>>> +    if (fb_helper->deferred_setup)
>>> +        return 0;
>>> +
>>> +    fb_helper->input_wq = create_singlethread_workqueue("drm-fb-lid");
>>> +    if (fb_helper->input_wq == NULL)
>>> +        return -ENOMEM;
>>> +
>>> +    drm_fb_helper_lid_handler.private = fb_helper;
>>> +    ret = input_register_handler(&drm_fb_helper_lid_handler);
>>> +    if (ret)
>>> +        goto remove_wq;
>>> +
>>> +    return 0;
>>> +
>>> +remove_wq:
>>> +    destroy_workqueue(fb_helper->input_wq);
>>> +    fb_helper->input_wq = NULL;
>>> +    return ret;
>>> +}
>>> +
>>>   /**
>>>    * drm_fb_helper_prepare - setup a drm_fb_helper structure
>>>    * @dev: DRM device
>>> @@ -445,6 +569,7 @@ void drm_fb_helper_prepare(struct drm_device 
>>> *dev, struct drm_fb_helper *helper,
>>>       spin_lock_init(&helper->damage_lock);
>>>       INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
>>>       INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
>>> +    INIT_WORK(&helper->lid_work, drm_fb_helper_lid_work);
>>>       helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
>>>       mutex_init(&helper->lock);
>>>       helper->funcs = funcs;
>>> @@ -593,6 +718,9 @@ void drm_fb_helper_fini(struct drm_fb_helper 
>>> *fb_helper)
>>>       if (!drm_fbdev_emulation)
>>>           return;
>>> +    input_unregister_handler(&drm_fb_helper_lid_handler);
>>> +    destroy_workqueue(fb_helper->input_wq);
>>> +
>>>       cancel_work_sync(&fb_helper->resume_work);
>>>       cancel_work_sync(&fb_helper->damage_work);
>>> @@ -1842,6 +1970,10 @@ 
>>> __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper 
>>> *fb_helper)
>>>       width = dev->mode_config.max_width;
>>>       height = dev->mode_config.max_height;
>>> +    ret = drm_fb_helper_create_lid_handler(fb_helper);
>>> +    if (ret)
>>> +        return ret;
>>> +
>>>       drm_client_modeset_probe(&fb_helper->client, width, height);
>>>       ret = drm_fb_helper_single_fb_probe(fb_helper);
>>>       if (ret < 0) {
>>> diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h
>>> index 63767cf24371..619af597784c 100644
>>> --- a/include/drm/drm_device.h
>>> +++ b/include/drm/drm_device.h
>>> @@ -316,6 +316,12 @@ struct drm_device {
>>>        * Root directory for debugfs files.
>>>        */
>>>       struct dentry *debugfs_root;
>>> +
>>> +    /**
>>> +     * @lid_closed: Flag to tell the lid switch state
>>> +     */
>>> +    bool lid_closed;
>>> +
>>>   };
>>>   #endif
>>> diff --git a/include/drm/drm_fb_helper.h b/include/drm/drm_fb_helper.h
>>> index 375737fd6c36..7fb36c10299d 100644
>>> --- a/include/drm/drm_fb_helper.h
>>> +++ b/include/drm/drm_fb_helper.h
>>> @@ -143,6 +143,8 @@ struct drm_fb_helper {
>>>       spinlock_t damage_lock;
>>>       struct work_struct damage_work;
>>>       struct work_struct resume_work;
>>> +    struct work_struct lid_work;
>>> +    struct workqueue_struct *input_wq;
>>>       /**
>>>        * @lock:
>>
>
Mario Limonciello June 14, 2024, 2:20 p.m. UTC | #5
On 6/14/2024 09:17, Thomas Zimmermann wrote:
> Hi
> 
> Am 14.06.24 um 15:47 schrieb Mario Limonciello:
>> On 6/14/2024 03:15, Thomas Zimmermann wrote:
>>> Hi Mario
>>>
>>> Am 13.06.24 um 07:17 schrieb Mario Limonciello:
>>>> If the lid on a laptop is closed when eDP connectors are populated
>>>> then it remains enabled when the initial framebuffer configuration
>>>> is built.
>>>>
>>>> When creating the initial framebuffer configuration detect the
>>>> lid status and if it's closed disable any eDP connectors.
>>>>
>>>> Also set up a workqueue to monitor for any future lid events.
>>>
>>> After reading through this patchset, I think fbdev emulation is not 
>>> the right place for this code, as lid state is global.
>>>
>>> You could put this into drm_client_modeset.c and track lid state per 
>>> client. drm_fb_helper_lid_work() would call the client's hotplug 
>>> callback. But preferable, lid state should be tracked per DRM device 
>>> in struct drm_mode_config and call drm_client_dev_hotplug() on each 
>>> lid-state event. Thoughts? Best regards Thomas
>>
>> This is pretty similar to what I first did when moving from ACPI over 
>> to generic input switch.
>>
>> It works for the initial configuration.  But I don't believe it makes 
>> sense for the lid switch events because not all DRM clients will 
>> "want" to respond to the lid switch events.  By leaving it up to the 
>> client for everything except fbdev emulation they can also track the 
>> lid switch and decide the policy.
> 
> 
> All our current clients do fbdev emulation, possibly others would be the 
> panic screen and a boot-up logo. A panic screen doesn't do actual mode 
> setting, but any other client would most likely want enable and disable 
> the display depending on the lid state. Having this code in the DRM 
> client helpers make perfect sense. But as it's global state, it makes no 
> sense to set this up per client. Hence the suggestion to manage this in 
> per DRM device.
> 
> It would also make sense to try to integrate this into the probe 
> helpers. When the lid state changes, the probe helpers would invoke the 
> driver's regular hotplugging code.

Got it; I'll do some experimentation with this change, thanks for the 
feedback.

> 
> 
>>
>> I also worry about what happens if the kernel does a hotplug callback 
>> on lid events as well at the client choosing to do it. Don't we end up 
>> with two modesets?  So then I would think you need a handshake of some 
>> sort to decide whether to do it for a given client where fbdev 
>> emulation would opt in and then all other clients can choose to opt in 
>> or not.
> 
> 
> What do you mean by the kernel does a hotplug event and the client does 
> one? There should really only be one place to handle all of this. If we 
> end up with two modesets, we'd get an additional flicker when the lid 
> gets opened.
> 

I'll see if my worry is founded after I move it all over.

> 
> Best regards
> Thomas
> 
>>
>>>>
>>>> Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
>>>> Reported-by: Chris Bainbridge <chris.bainbridge@gmail.com>
>>>> Closes: https://gitlab.freedesktop.org/drm/amd/-/issues/3349
>>>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>>>> ---
>>>> v2->v3:
>>>>   * Use input device instead of ACPI device
>>>>   * Detect lid open/close events
>>>> ---
>>>>   drivers/gpu/drm/drm_client_modeset.c |  29 ++++++
>>>>   drivers/gpu/drm/drm_fb_helper.c      | 132 
>>>> +++++++++++++++++++++++++++
>>>>   include/drm/drm_device.h             |   6 ++
>>>>   include/drm/drm_fb_helper.h          |   2 +
>>>>   4 files changed, 169 insertions(+)
>>>>
>>>> diff --git a/drivers/gpu/drm/drm_client_modeset.c 
>>>> b/drivers/gpu/drm/drm_client_modeset.c
>>>> index 31af5cf37a09..b8adfe87334b 100644
>>>> --- a/drivers/gpu/drm/drm_client_modeset.c
>>>> +++ b/drivers/gpu/drm/drm_client_modeset.c
>>>> @@ -257,6 +257,34 @@ static void 
>>>> drm_client_connectors_enabled(struct drm_connector **connectors,
>>>>           enabled[i] = drm_connector_enabled(connectors[i], false);
>>>>   }
>>>> +static void drm_client_match_edp_lid(struct drm_device *dev,
>>>> +                     struct drm_connector **connectors,
>>>> +                     unsigned int connector_count,
>>>> +                     bool *enabled)
>>>> +{
>>>> +    int i;
>>>> +
>>>> +    for (i = 0; i < connector_count; i++) {
>>>> +        struct drm_connector *connector = connectors[i];
>>>> +
>>>> +        switch (connector->connector_type) {
>>>> +        case DRM_MODE_CONNECTOR_LVDS:
>>>> +        case DRM_MODE_CONNECTOR_eDP:
>>>> +            if (!enabled[i])
>>>> +                continue;
>>>> +            break;
>>>> +        default:
>>>> +            continue;
>>>> +        }
>>>> +
>>>> +        if (dev->lid_closed) {
>>>> +            drm_dbg_kms(dev, "[CONNECTOR:%d:%s] lid is closed, 
>>>> disabling\n",
>>>> +                    connector->base.id, connector->name);
>>>> +            enabled[i] = false;
>>>> +        }
>>>> +    }
>>>> +}
>>>> +
>>>>   static bool drm_client_target_cloned(struct drm_device *dev,
>>>>                        struct drm_connector **connectors,
>>>>                        unsigned int connector_count,
>>>> @@ -844,6 +872,7 @@ int drm_client_modeset_probe(struct 
>>>> drm_client_dev *client, unsigned int width,
>>>>           memset(crtcs, 0, connector_count * sizeof(*crtcs));
>>>>           memset(offsets, 0, connector_count * sizeof(*offsets));
>>>> +        drm_client_match_edp_lid(dev, connectors, connector_count, 
>>>> enabled);
>>>>           if (!drm_client_target_cloned(dev, connectors, 
>>>> connector_count, modes,
>>>>                             offsets, enabled, width, height) &&
>>>>               !drm_client_target_preferred(dev, connectors, 
>>>> connector_count, modes,
>>>> diff --git a/drivers/gpu/drm/drm_fb_helper.c 
>>>> b/drivers/gpu/drm/drm_fb_helper.c
>>>> index d612133e2cf7..41dd5887599a 100644
>>>> --- a/drivers/gpu/drm/drm_fb_helper.c
>>>> +++ b/drivers/gpu/drm/drm_fb_helper.c
>>>> @@ -30,6 +30,8 @@
>>>>   #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>>>>   #include <linux/console.h>
>>>> +#include <linux/input.h>
>>>> +#include <linux/mod_devicetable.h>
>>>>   #include <linux/pci.h>
>>>>   #include <linux/sysrq.h>
>>>>   #include <linux/vga_switcheroo.h>
>>>> @@ -413,6 +415,128 @@ static void drm_fb_helper_damage_work(struct 
>>>> work_struct *work)
>>>>       drm_fb_helper_fb_dirty(helper);
>>>>   }
>>>> +static void drm_fb_helper_lid_event(struct input_handle *handle, 
>>>> unsigned int type,
>>>> +                    unsigned int code, int value)
>>>> +{
>>>> +    if (type == EV_SW && code == SW_LID) {
>>>> +        struct drm_fb_helper *fb_helper = handle->handler->private;
>>>> +
>>>> +        if (value != fb_helper->dev->lid_closed) {
>>>> +            fb_helper->dev->lid_closed = value;
>>>> +            queue_work(fb_helper->input_wq, &fb_helper->lid_work);
>>>> +        }
>>>> +    }
>>>> +}
>>>> +
>>>> +struct drm_fb_lid {
>>>> +    struct input_handle handle;
>>>> +};
>>>> +
>>>> +static int drm_fb_helper_lid_connect(struct input_handler *handler,
>>>> +                     struct input_dev *dev,
>>>> +                     const struct input_device_id *id)
>>>> +{
>>>> +    struct drm_fb_helper *fb_helper = handler->private;
>>>> +    struct drm_fb_lid *lid;
>>>> +    char *name;
>>>> +    int error;
>>>> +
>>>> +    lid = kzalloc(sizeof(*lid), GFP_KERNEL);
>>>> +    if (!lid)
>>>> +        return -ENOMEM;
>>>> +
>>>> +    name = kasprintf(GFP_KERNEL, "drm-fb-helper-lid-%s", 
>>>> dev_name(&dev->dev));
>>>> +    if (!name) {
>>>> +        error = -ENOMEM;
>>>> +        goto err_free_lid;
>>>> +    }
>>>> +
>>>> +    lid->handle.dev = dev;
>>>> +    lid->handle.handler = handler;
>>>> +    lid->handle.name = name;
>>>> +    lid->handle.private = lid;
>>>> +
>>>> +    error = input_register_handle(&lid->handle);
>>>> +    if (error)
>>>> +        goto err_free_name;
>>>> +
>>>> +    error = input_open_device(&lid->handle);
>>>> +    if (error)
>>>> +        goto err_unregister_handle;
>>>> +
>>>> +    fb_helper->dev->lid_closed = dev->sw[SW_LID];
>>>> +    drm_dbg_kms(fb_helper->dev, "initial lid state is set to %d\n", 
>>>> fb_helper->dev->lid_closed);
>>>> +
>>>> +    return 0;
>>>> +
>>>> +err_unregister_handle:
>>>> +    input_unregister_handle(&lid->handle);
>>>> +err_free_name:
>>>> +    kfree(name);
>>>> +err_free_lid:
>>>> +    kfree(lid);
>>>> +    return error;
>>>> +}
>>>> +
>>>> +static void drm_fb_helper_lid_disconnect(struct input_handle *handle)
>>>> +{
>>>> +    struct drm_fb_lid *lid = handle->private;
>>>> +
>>>> +    input_close_device(handle);
>>>> +    input_unregister_handle(handle);
>>>> +
>>>> +    kfree(handle->name);
>>>> +    kfree(lid);
>>>> +}
>>>> +
>>>> +static const struct input_device_id drm_fb_helper_lid_ids[] = {
>>>> +    {
>>>> +        .flags = INPUT_DEVICE_ID_MATCH_EVBIT | 
>>>> INPUT_DEVICE_ID_MATCH_SWBIT,
>>>> +        .evbit = { BIT_MASK(EV_SW) },
>>>> +        .swbit = { [BIT_WORD(SW_LID)] = BIT_MASK(SW_LID) },
>>>> +    },
>>>> +    { },
>>>> +};
>>>> +
>>>> +static struct input_handler drm_fb_helper_lid_handler = {
>>>> +    .event =    drm_fb_helper_lid_event,
>>>> +    .connect =    drm_fb_helper_lid_connect,
>>>> +    .disconnect =    drm_fb_helper_lid_disconnect,
>>>> +    .name =        "drm-fb-helper-lid",
>>>> +    .id_table =    drm_fb_helper_lid_ids,
>>>> +};
>>>> +
>>>> +static void drm_fb_helper_lid_work(struct work_struct *work)
>>>> +{
>>>> +    struct drm_fb_helper *fb_helper = container_of(work, struct 
>>>> drm_fb_helper,
>>>> +                               lid_work);
>>>> +    drm_fb_helper_hotplug_event(fb_helper);
>>>> +}
>>>> +
>>>> +static int drm_fb_helper_create_lid_handler(struct drm_fb_helper 
>>>> *fb_helper)
>>>> +{
>>>> +    int ret = 0;
>>>> +
>>>> +    if (fb_helper->deferred_setup)
>>>> +        return 0;
>>>> +
>>>> +    fb_helper->input_wq = create_singlethread_workqueue("drm-fb-lid");
>>>> +    if (fb_helper->input_wq == NULL)
>>>> +        return -ENOMEM;
>>>> +
>>>> +    drm_fb_helper_lid_handler.private = fb_helper;
>>>> +    ret = input_register_handler(&drm_fb_helper_lid_handler);
>>>> +    if (ret)
>>>> +        goto remove_wq;
>>>> +
>>>> +    return 0;
>>>> +
>>>> +remove_wq:
>>>> +    destroy_workqueue(fb_helper->input_wq);
>>>> +    fb_helper->input_wq = NULL;
>>>> +    return ret;
>>>> +}
>>>> +
>>>>   /**
>>>>    * drm_fb_helper_prepare - setup a drm_fb_helper structure
>>>>    * @dev: DRM device
>>>> @@ -445,6 +569,7 @@ void drm_fb_helper_prepare(struct drm_device 
>>>> *dev, struct drm_fb_helper *helper,
>>>>       spin_lock_init(&helper->damage_lock);
>>>>       INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
>>>>       INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
>>>> +    INIT_WORK(&helper->lid_work, drm_fb_helper_lid_work);
>>>>       helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
>>>>       mutex_init(&helper->lock);
>>>>       helper->funcs = funcs;
>>>> @@ -593,6 +718,9 @@ void drm_fb_helper_fini(struct drm_fb_helper 
>>>> *fb_helper)
>>>>       if (!drm_fbdev_emulation)
>>>>           return;
>>>> +    input_unregister_handler(&drm_fb_helper_lid_handler);
>>>> +    destroy_workqueue(fb_helper->input_wq);
>>>> +
>>>>       cancel_work_sync(&fb_helper->resume_work);
>>>>       cancel_work_sync(&fb_helper->damage_work);
>>>> @@ -1842,6 +1970,10 @@ 
>>>> __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper 
>>>> *fb_helper)
>>>>       width = dev->mode_config.max_width;
>>>>       height = dev->mode_config.max_height;
>>>> +    ret = drm_fb_helper_create_lid_handler(fb_helper);
>>>> +    if (ret)
>>>> +        return ret;
>>>> +
>>>>       drm_client_modeset_probe(&fb_helper->client, width, height);
>>>>       ret = drm_fb_helper_single_fb_probe(fb_helper);
>>>>       if (ret < 0) {
>>>> diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h
>>>> index 63767cf24371..619af597784c 100644
>>>> --- a/include/drm/drm_device.h
>>>> +++ b/include/drm/drm_device.h
>>>> @@ -316,6 +316,12 @@ struct drm_device {
>>>>        * Root directory for debugfs files.
>>>>        */
>>>>       struct dentry *debugfs_root;
>>>> +
>>>> +    /**
>>>> +     * @lid_closed: Flag to tell the lid switch state
>>>> +     */
>>>> +    bool lid_closed;
>>>> +
>>>>   };
>>>>   #endif
>>>> diff --git a/include/drm/drm_fb_helper.h b/include/drm/drm_fb_helper.h
>>>> index 375737fd6c36..7fb36c10299d 100644
>>>> --- a/include/drm/drm_fb_helper.h
>>>> +++ b/include/drm/drm_fb_helper.h
>>>> @@ -143,6 +143,8 @@ struct drm_fb_helper {
>>>>       spinlock_t damage_lock;
>>>>       struct work_struct damage_work;
>>>>       struct work_struct resume_work;
>>>> +    struct work_struct lid_work;
>>>> +    struct workqueue_struct *input_wq;
>>>>       /**
>>>>        * @lock:
>>>
>>
>
kernel test robot June 16, 2024, 12:24 p.m. UTC | #6
Hi Mario,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm-misc/drm-misc-next]
[also build test ERROR on linus/master v6.10-rc3 next-20240613]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mario-Limonciello/drm-fb-helper-Detect-when-lid-is-closed-during-initialization/20240613-132009
base:   git://anongit.freedesktop.org/drm/drm-misc drm-misc-next
patch link:    https://lore.kernel.org/r/20240613051700.1112-1-mario.limonciello%40amd.com
patch subject: [PATCH v3] drm/fb-helper: Detect when lid is closed during initialization
config: x86_64-buildonly-randconfig-003-20240614 (https://download.01.org/0day-ci/archive/20240616/202406161923.hGiUV7wr-lkp@intel.com/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240616/202406161923.hGiUV7wr-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202406161923.hGiUV7wr-lkp@intel.com/

All errors (new ones prefixed by >>, old ones prefixed by <<):

WARNING: modpost: missing MODULE_DESCRIPTION() in vmlinux.o
WARNING: modpost: missing MODULE_DESCRIPTION() in arch/x86/events/rapl.o
WARNING: modpost: missing MODULE_DESCRIPTION() in kernel/locking/test-ww_mutex.o
WARNING: modpost: missing MODULE_DESCRIPTION() in kernel/rcu/rcutorture.o
WARNING: modpost: missing MODULE_DESCRIPTION() in kernel/time/time_test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in mm/kasan/kasan_test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in mm/kasan/kasan_test_module.o
WARNING: modpost: missing MODULE_DESCRIPTION() in mm/kfence/kfence_test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_cp737.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_cp775.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_cp850.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_cp852.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_cp861.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_cp863.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_cp1251.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_ascii.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_iso8859-3.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_iso8859-5.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_iso8859-7.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_iso8859-15.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/nls_koi8-r.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/nls/mac-inuit.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/binfmt_script.o
WARNING: modpost: missing MODULE_DESCRIPTION() in fs/isofs/isofs.o
WARNING: modpost: missing MODULE_DESCRIPTION() in security/keys/trusted-keys/trusted.o
WARNING: modpost: missing MODULE_DESCRIPTION() in lib/kunit/kunit.o
WARNING: modpost: missing MODULE_DESCRIPTION() in lib/kunit/kunit-example-test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in lib/math/prime_numbers.o
WARNING: modpost: missing MODULE_DESCRIPTION() in lib/crypto/libdes.o
WARNING: modpost: missing MODULE_DESCRIPTION() in lib/test_ubsan.o
WARNING: modpost: missing MODULE_DESCRIPTION() in lib/asn1_encoder.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/video/backlight/platform_lcd.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/video/fbdev/macmodes.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/clk/clk_test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/clk/clk-gate_test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/virtio/virtio.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/virtio/virtio_ring.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/regulator/max20411-regulator.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/char/agp/amd64-agp.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/char/agp/intel-agp.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/char/agp/intel-gtt.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/char/nvram.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/gpu/drm/tests/drm_hdmi_state_helper_test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/base/regmap/regmap-kunit.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/base/regmap/regmap-ram.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/base/regmap/regmap-raw-ram.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/base/regmap/regmap-sccb.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/dax/device_dax.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/cxl/core/cxl_core.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/cxl/cxl_pci.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/cxl/cxl_port.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/scsi/advansys.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/scsi/atp870u.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/mtd/chips/cfi_util.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/firewire/packet-serdes-test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/pcmcia/yenta_socket.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/media/tuners/tda9887.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/media/v4l2-core/v4l2-async.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/media/v4l2-core/v4l2-fwnode.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/leds/simple/simatic-ipc-leds.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/platform/x86/siemens/simatic-ipc.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/platform/x86/siemens/simatic-ipc-batt.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/devfreq/governor_performance.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hwmon/mr75203.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/iio/buffer/kfifo_buf.o
>> ERROR: modpost: "input_unregister_handler" [drivers/gpu/drm/drm_kms_helper.ko] undefined!
>> ERROR: modpost: "input_register_handler" [drivers/gpu/drm/drm_kms_helper.ko] undefined!
>> ERROR: modpost: "input_register_handle" [drivers/gpu/drm/drm_kms_helper.ko] undefined!
>> ERROR: modpost: "input_open_device" [drivers/gpu/drm/drm_kms_helper.ko] undefined!
>> ERROR: modpost: "input_unregister_handle" [drivers/gpu/drm/drm_kms_helper.ko] undefined!
>> ERROR: modpost: "input_close_device" [drivers/gpu/drm/drm_kms_helper.ko] undefined!
Daniel Vetter June 17, 2024, 2:31 p.m. UTC | #7
On Thu, Jun 13, 2024 at 12:17:00AM -0500, Mario Limonciello wrote:
> If the lid on a laptop is closed when eDP connectors are populated
> then it remains enabled when the initial framebuffer configuration
> is built.
> 
> When creating the initial framebuffer configuration detect the
> lid status and if it's closed disable any eDP connectors.
> 
> Also set up a workqueue to monitor for any future lid events.
> 
> Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> Reported-by: Chris Bainbridge <chris.bainbridge@gmail.com>
> Closes: https://gitlab.freedesktop.org/drm/amd/-/issues/3349
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>

More fundamental question: Do we even want to solve this issues?

This is fbdev we're talking about, if people use this actually in
production, they don't care much about the fallout.

Plus this has the potential to cause regressions, like we might not be
able to enable edp after having enabled and external screen. Which would
be bad.

Imo this needs a lot more justification than "one random user asked for
it". Especially since it's been the behaviour for like well over a decade
for any integrated panel that we just light them always up when using
fbdev/fbcon.

If people want full control over all this, they can use a display manager
(or a userspace console).

Cheers, Sima
> ---
> v2->v3:
>  * Use input device instead of ACPI device
>  * Detect lid open/close events
> ---
>  drivers/gpu/drm/drm_client_modeset.c |  29 ++++++
>  drivers/gpu/drm/drm_fb_helper.c      | 132 +++++++++++++++++++++++++++
>  include/drm/drm_device.h             |   6 ++
>  include/drm/drm_fb_helper.h          |   2 +
>  4 files changed, 169 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_client_modeset.c b/drivers/gpu/drm/drm_client_modeset.c
> index 31af5cf37a09..b8adfe87334b 100644
> --- a/drivers/gpu/drm/drm_client_modeset.c
> +++ b/drivers/gpu/drm/drm_client_modeset.c
> @@ -257,6 +257,34 @@ static void drm_client_connectors_enabled(struct drm_connector **connectors,
>  		enabled[i] = drm_connector_enabled(connectors[i], false);
>  }
>  
> +static void drm_client_match_edp_lid(struct drm_device *dev,
> +				     struct drm_connector **connectors,
> +				     unsigned int connector_count,
> +				     bool *enabled)
> +{
> +	int i;
> +
> +	for (i = 0; i < connector_count; i++) {
> +		struct drm_connector *connector = connectors[i];
> +
> +		switch (connector->connector_type) {
> +		case DRM_MODE_CONNECTOR_LVDS:
> +		case DRM_MODE_CONNECTOR_eDP:
> +			if (!enabled[i])
> +				continue;
> +			break;
> +		default:
> +			continue;
> +		}
> +
> +		if (dev->lid_closed) {
> +			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] lid is closed, disabling\n",
> +				    connector->base.id, connector->name);
> +			enabled[i] = false;
> +		}
> +	}
> +}
> +
>  static bool drm_client_target_cloned(struct drm_device *dev,
>  				     struct drm_connector **connectors,
>  				     unsigned int connector_count,
> @@ -844,6 +872,7 @@ int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width,
>  		memset(crtcs, 0, connector_count * sizeof(*crtcs));
>  		memset(offsets, 0, connector_count * sizeof(*offsets));
>  
> +		drm_client_match_edp_lid(dev, connectors, connector_count, enabled);
>  		if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
>  					      offsets, enabled, width, height) &&
>  		    !drm_client_target_preferred(dev, connectors, connector_count, modes,
> diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c
> index d612133e2cf7..41dd5887599a 100644
> --- a/drivers/gpu/drm/drm_fb_helper.c
> +++ b/drivers/gpu/drm/drm_fb_helper.c
> @@ -30,6 +30,8 @@
>  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>  
>  #include <linux/console.h>
> +#include <linux/input.h>
> +#include <linux/mod_devicetable.h>
>  #include <linux/pci.h>
>  #include <linux/sysrq.h>
>  #include <linux/vga_switcheroo.h>
> @@ -413,6 +415,128 @@ static void drm_fb_helper_damage_work(struct work_struct *work)
>  	drm_fb_helper_fb_dirty(helper);
>  }
>  
> +static void drm_fb_helper_lid_event(struct input_handle *handle, unsigned int type,
> +				    unsigned int code, int value)
> +{
> +	if (type == EV_SW && code == SW_LID) {
> +		struct drm_fb_helper *fb_helper = handle->handler->private;
> +
> +		if (value != fb_helper->dev->lid_closed) {
> +			fb_helper->dev->lid_closed = value;
> +			queue_work(fb_helper->input_wq, &fb_helper->lid_work);
> +		}
> +	}
> +}
> +
> +struct drm_fb_lid {
> +	struct input_handle handle;
> +};
> +
> +static int drm_fb_helper_lid_connect(struct input_handler *handler,
> +				     struct input_dev *dev,
> +				     const struct input_device_id *id)
> +{
> +	struct drm_fb_helper *fb_helper = handler->private;
> +	struct drm_fb_lid *lid;
> +	char *name;
> +	int error;
> +
> +	lid = kzalloc(sizeof(*lid), GFP_KERNEL);
> +	if (!lid)
> +		return -ENOMEM;
> +
> +	name = kasprintf(GFP_KERNEL, "drm-fb-helper-lid-%s", dev_name(&dev->dev));
> +	if (!name) {
> +		error = -ENOMEM;
> +		goto err_free_lid;
> +	}
> +
> +	lid->handle.dev = dev;
> +	lid->handle.handler = handler;
> +	lid->handle.name = name;
> +	lid->handle.private = lid;
> +
> +	error = input_register_handle(&lid->handle);
> +	if (error)
> +		goto err_free_name;
> +
> +	error = input_open_device(&lid->handle);
> +	if (error)
> +		goto err_unregister_handle;
> +
> +	fb_helper->dev->lid_closed = dev->sw[SW_LID];
> +	drm_dbg_kms(fb_helper->dev, "initial lid state is set to %d\n", fb_helper->dev->lid_closed);
> +
> +	return 0;
> +
> +err_unregister_handle:
> +	input_unregister_handle(&lid->handle);
> +err_free_name:
> +	kfree(name);
> +err_free_lid:
> +	kfree(lid);
> +	return error;
> +}
> +
> +static void drm_fb_helper_lid_disconnect(struct input_handle *handle)
> +{
> +	struct drm_fb_lid *lid = handle->private;
> +
> +	input_close_device(handle);
> +	input_unregister_handle(handle);
> +
> +	kfree(handle->name);
> +	kfree(lid);
> +}
> +
> +static const struct input_device_id drm_fb_helper_lid_ids[] = {
> +	{
> +		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
> +		.evbit = { BIT_MASK(EV_SW) },
> +		.swbit = { [BIT_WORD(SW_LID)] = BIT_MASK(SW_LID) },
> +	},
> +	{ },
> +};
> +
> +static struct input_handler drm_fb_helper_lid_handler = {
> +	.event =	drm_fb_helper_lid_event,
> +	.connect =	drm_fb_helper_lid_connect,
> +	.disconnect =	drm_fb_helper_lid_disconnect,
> +	.name =		"drm-fb-helper-lid",
> +	.id_table =	drm_fb_helper_lid_ids,
> +};
> +
> +static void drm_fb_helper_lid_work(struct work_struct *work)
> +{
> +	struct drm_fb_helper *fb_helper = container_of(work, struct drm_fb_helper,
> +						       lid_work);
> +	drm_fb_helper_hotplug_event(fb_helper);
> +}
> +
> +static int drm_fb_helper_create_lid_handler(struct drm_fb_helper *fb_helper)
> +{
> +	int ret = 0;
> +
> +	if (fb_helper->deferred_setup)
> +		return 0;
> +
> +	fb_helper->input_wq = create_singlethread_workqueue("drm-fb-lid");
> +	if (fb_helper->input_wq == NULL)
> +		return -ENOMEM;
> +
> +	drm_fb_helper_lid_handler.private = fb_helper;
> +	ret = input_register_handler(&drm_fb_helper_lid_handler);
> +	if (ret)
> +		goto remove_wq;
> +
> +	return 0;
> +
> +remove_wq:
> +	destroy_workqueue(fb_helper->input_wq);
> +	fb_helper->input_wq = NULL;
> +	return ret;
> +}
> +
>  /**
>   * drm_fb_helper_prepare - setup a drm_fb_helper structure
>   * @dev: DRM device
> @@ -445,6 +569,7 @@ void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
>  	spin_lock_init(&helper->damage_lock);
>  	INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
>  	INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
> +	INIT_WORK(&helper->lid_work, drm_fb_helper_lid_work);
>  	helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
>  	mutex_init(&helper->lock);
>  	helper->funcs = funcs;
> @@ -593,6 +718,9 @@ void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
>  	if (!drm_fbdev_emulation)
>  		return;
>  
> +	input_unregister_handler(&drm_fb_helper_lid_handler);
> +	destroy_workqueue(fb_helper->input_wq);
> +
>  	cancel_work_sync(&fb_helper->resume_work);
>  	cancel_work_sync(&fb_helper->damage_work);
>  
> @@ -1842,6 +1970,10 @@ __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper)
>  	width = dev->mode_config.max_width;
>  	height = dev->mode_config.max_height;
>  
> +	ret = drm_fb_helper_create_lid_handler(fb_helper);
> +	if (ret)
> +		return ret;
> +
>  	drm_client_modeset_probe(&fb_helper->client, width, height);
>  	ret = drm_fb_helper_single_fb_probe(fb_helper);
>  	if (ret < 0) {
> diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h
> index 63767cf24371..619af597784c 100644
> --- a/include/drm/drm_device.h
> +++ b/include/drm/drm_device.h
> @@ -316,6 +316,12 @@ struct drm_device {
>  	 * Root directory for debugfs files.
>  	 */
>  	struct dentry *debugfs_root;
> +
> +	/**
> +	 * @lid_closed: Flag to tell the lid switch state
> +	 */
> +	bool lid_closed;
> +
>  };
>  
>  #endif
> diff --git a/include/drm/drm_fb_helper.h b/include/drm/drm_fb_helper.h
> index 375737fd6c36..7fb36c10299d 100644
> --- a/include/drm/drm_fb_helper.h
> +++ b/include/drm/drm_fb_helper.h
> @@ -143,6 +143,8 @@ struct drm_fb_helper {
>  	spinlock_t damage_lock;
>  	struct work_struct damage_work;
>  	struct work_struct resume_work;
> +	struct work_struct lid_work;
> +	struct workqueue_struct *input_wq;
>  
>  	/**
>  	 * @lock:
> -- 
> 2.43.0
>
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_client_modeset.c b/drivers/gpu/drm/drm_client_modeset.c
index 31af5cf37a09..b8adfe87334b 100644
--- a/drivers/gpu/drm/drm_client_modeset.c
+++ b/drivers/gpu/drm/drm_client_modeset.c
@@ -257,6 +257,34 @@  static void drm_client_connectors_enabled(struct drm_connector **connectors,
 		enabled[i] = drm_connector_enabled(connectors[i], false);
 }
 
+static void drm_client_match_edp_lid(struct drm_device *dev,
+				     struct drm_connector **connectors,
+				     unsigned int connector_count,
+				     bool *enabled)
+{
+	int i;
+
+	for (i = 0; i < connector_count; i++) {
+		struct drm_connector *connector = connectors[i];
+
+		switch (connector->connector_type) {
+		case DRM_MODE_CONNECTOR_LVDS:
+		case DRM_MODE_CONNECTOR_eDP:
+			if (!enabled[i])
+				continue;
+			break;
+		default:
+			continue;
+		}
+
+		if (dev->lid_closed) {
+			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] lid is closed, disabling\n",
+				    connector->base.id, connector->name);
+			enabled[i] = false;
+		}
+	}
+}
+
 static bool drm_client_target_cloned(struct drm_device *dev,
 				     struct drm_connector **connectors,
 				     unsigned int connector_count,
@@ -844,6 +872,7 @@  int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width,
 		memset(crtcs, 0, connector_count * sizeof(*crtcs));
 		memset(offsets, 0, connector_count * sizeof(*offsets));
 
+		drm_client_match_edp_lid(dev, connectors, connector_count, enabled);
 		if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
 					      offsets, enabled, width, height) &&
 		    !drm_client_target_preferred(dev, connectors, connector_count, modes,
diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c
index d612133e2cf7..41dd5887599a 100644
--- a/drivers/gpu/drm/drm_fb_helper.c
+++ b/drivers/gpu/drm/drm_fb_helper.c
@@ -30,6 +30,8 @@ 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/console.h>
+#include <linux/input.h>
+#include <linux/mod_devicetable.h>
 #include <linux/pci.h>
 #include <linux/sysrq.h>
 #include <linux/vga_switcheroo.h>
@@ -413,6 +415,128 @@  static void drm_fb_helper_damage_work(struct work_struct *work)
 	drm_fb_helper_fb_dirty(helper);
 }
 
+static void drm_fb_helper_lid_event(struct input_handle *handle, unsigned int type,
+				    unsigned int code, int value)
+{
+	if (type == EV_SW && code == SW_LID) {
+		struct drm_fb_helper *fb_helper = handle->handler->private;
+
+		if (value != fb_helper->dev->lid_closed) {
+			fb_helper->dev->lid_closed = value;
+			queue_work(fb_helper->input_wq, &fb_helper->lid_work);
+		}
+	}
+}
+
+struct drm_fb_lid {
+	struct input_handle handle;
+};
+
+static int drm_fb_helper_lid_connect(struct input_handler *handler,
+				     struct input_dev *dev,
+				     const struct input_device_id *id)
+{
+	struct drm_fb_helper *fb_helper = handler->private;
+	struct drm_fb_lid *lid;
+	char *name;
+	int error;
+
+	lid = kzalloc(sizeof(*lid), GFP_KERNEL);
+	if (!lid)
+		return -ENOMEM;
+
+	name = kasprintf(GFP_KERNEL, "drm-fb-helper-lid-%s", dev_name(&dev->dev));
+	if (!name) {
+		error = -ENOMEM;
+		goto err_free_lid;
+	}
+
+	lid->handle.dev = dev;
+	lid->handle.handler = handler;
+	lid->handle.name = name;
+	lid->handle.private = lid;
+
+	error = input_register_handle(&lid->handle);
+	if (error)
+		goto err_free_name;
+
+	error = input_open_device(&lid->handle);
+	if (error)
+		goto err_unregister_handle;
+
+	fb_helper->dev->lid_closed = dev->sw[SW_LID];
+	drm_dbg_kms(fb_helper->dev, "initial lid state is set to %d\n", fb_helper->dev->lid_closed);
+
+	return 0;
+
+err_unregister_handle:
+	input_unregister_handle(&lid->handle);
+err_free_name:
+	kfree(name);
+err_free_lid:
+	kfree(lid);
+	return error;
+}
+
+static void drm_fb_helper_lid_disconnect(struct input_handle *handle)
+{
+	struct drm_fb_lid *lid = handle->private;
+
+	input_close_device(handle);
+	input_unregister_handle(handle);
+
+	kfree(handle->name);
+	kfree(lid);
+}
+
+static const struct input_device_id drm_fb_helper_lid_ids[] = {
+	{
+		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
+		.evbit = { BIT_MASK(EV_SW) },
+		.swbit = { [BIT_WORD(SW_LID)] = BIT_MASK(SW_LID) },
+	},
+	{ },
+};
+
+static struct input_handler drm_fb_helper_lid_handler = {
+	.event =	drm_fb_helper_lid_event,
+	.connect =	drm_fb_helper_lid_connect,
+	.disconnect =	drm_fb_helper_lid_disconnect,
+	.name =		"drm-fb-helper-lid",
+	.id_table =	drm_fb_helper_lid_ids,
+};
+
+static void drm_fb_helper_lid_work(struct work_struct *work)
+{
+	struct drm_fb_helper *fb_helper = container_of(work, struct drm_fb_helper,
+						       lid_work);
+	drm_fb_helper_hotplug_event(fb_helper);
+}
+
+static int drm_fb_helper_create_lid_handler(struct drm_fb_helper *fb_helper)
+{
+	int ret = 0;
+
+	if (fb_helper->deferred_setup)
+		return 0;
+
+	fb_helper->input_wq = create_singlethread_workqueue("drm-fb-lid");
+	if (fb_helper->input_wq == NULL)
+		return -ENOMEM;
+
+	drm_fb_helper_lid_handler.private = fb_helper;
+	ret = input_register_handler(&drm_fb_helper_lid_handler);
+	if (ret)
+		goto remove_wq;
+
+	return 0;
+
+remove_wq:
+	destroy_workqueue(fb_helper->input_wq);
+	fb_helper->input_wq = NULL;
+	return ret;
+}
+
 /**
  * drm_fb_helper_prepare - setup a drm_fb_helper structure
  * @dev: DRM device
@@ -445,6 +569,7 @@  void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
 	spin_lock_init(&helper->damage_lock);
 	INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
 	INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
+	INIT_WORK(&helper->lid_work, drm_fb_helper_lid_work);
 	helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
 	mutex_init(&helper->lock);
 	helper->funcs = funcs;
@@ -593,6 +718,9 @@  void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
 	if (!drm_fbdev_emulation)
 		return;
 
+	input_unregister_handler(&drm_fb_helper_lid_handler);
+	destroy_workqueue(fb_helper->input_wq);
+
 	cancel_work_sync(&fb_helper->resume_work);
 	cancel_work_sync(&fb_helper->damage_work);
 
@@ -1842,6 +1970,10 @@  __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper)
 	width = dev->mode_config.max_width;
 	height = dev->mode_config.max_height;
 
+	ret = drm_fb_helper_create_lid_handler(fb_helper);
+	if (ret)
+		return ret;
+
 	drm_client_modeset_probe(&fb_helper->client, width, height);
 	ret = drm_fb_helper_single_fb_probe(fb_helper);
 	if (ret < 0) {
diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h
index 63767cf24371..619af597784c 100644
--- a/include/drm/drm_device.h
+++ b/include/drm/drm_device.h
@@ -316,6 +316,12 @@  struct drm_device {
 	 * Root directory for debugfs files.
 	 */
 	struct dentry *debugfs_root;
+
+	/**
+	 * @lid_closed: Flag to tell the lid switch state
+	 */
+	bool lid_closed;
+
 };
 
 #endif
diff --git a/include/drm/drm_fb_helper.h b/include/drm/drm_fb_helper.h
index 375737fd6c36..7fb36c10299d 100644
--- a/include/drm/drm_fb_helper.h
+++ b/include/drm/drm_fb_helper.h
@@ -143,6 +143,8 @@  struct drm_fb_helper {
 	spinlock_t damage_lock;
 	struct work_struct damage_work;
 	struct work_struct resume_work;
+	struct work_struct lid_work;
+	struct workqueue_struct *input_wq;
 
 	/**
 	 * @lock: