Skip to content

Commit 96a37bf

Browse files
committed
HID: i2c-hid: Support being a panel follower
As talked about in the patch ("drm/panel: Add a way for other devices to follow panel state"), we really want to keep the power states of a touchscreen and the panel it's attached to in sync with each other. In that spirit, add support to i2c-hid to be a panel follower. This will let the i2c-hid driver get informed when the panel is powered on and off. From there we can match the i2c-hid device's power state to that of the panel. NOTE: this patch specifically _doesn't_ use pm_runtime to keep track of / manage the power state of the i2c-hid device, even though my first instinct said that would be the way to go. Specific problems with using pm_runtime(): * The initial power up couldn't happen in a runtime resume function since it create sub-devices and, apparently, that's not good to do in your resume function. * Managing our power state with pm_runtime meant fighting to make the right thing happen at system suspend to prevent the system from trying to resume us only to suspend us again. While this might be able to be solved, it added complexity. Overall the code without pm_runtime() ended up being smaller and easier to understand. Reviewed-by: Maxime Ripard <[email protected]> Reviewed-by: Benjamin Tissoires <[email protected]> Acked-by: Benjamin Tissoires <[email protected]> Signed-off-by: Douglas Anderson <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/20230727101636.v4.9.Ib1a98309c455cd7e26b931c69993d4fba33bbe15@changeid
1 parent 5f8838e commit 96a37bf

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

drivers/hid/i2c-hid/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@ config I2C_HID_OF_GOODIX
7070

7171
config I2C_HID_CORE
7272
tristate
73+
# We need to call into panel code so if DRM=m, this can't be 'y'
74+
depends on DRM || !DRM
7375
endif
7476

drivers/hid/i2c-hid/i2c-hid-core.c

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include <linux/mutex.h>
3939
#include <asm/unaligned.h>
4040

41+
#include <drm/drm_panel.h>
42+
4143
#include "../hid-ids.h"
4244
#include "i2c-hid.h"
4345

@@ -107,6 +109,8 @@ struct i2c_hid {
107109
struct mutex reset_lock;
108110

109111
struct i2chid_ops *ops;
112+
struct drm_panel_follower panel_follower;
113+
bool is_panel_follower;
110114
};
111115

112116
static const struct i2c_hid_quirks {
@@ -993,7 +997,7 @@ static int i2c_hid_core_resume(struct i2c_hid *ihid)
993997
}
994998

995999
/**
996-
* i2c_hid_core_initial_power_up() - First time power up of the i2c-hid device.
1000+
* __do_i2c_hid_core_initial_power_up() - First time power up of the i2c-hid device.
9971001
* @ihid: The ihid object created during probe.
9981002
*
9991003
* This function is called at probe time.
@@ -1004,7 +1008,7 @@ static int i2c_hid_core_resume(struct i2c_hid *ihid)
10041008
*
10051009
* Return: 0 or error code.
10061010
*/
1007-
static int i2c_hid_core_initial_power_up(struct i2c_hid *ihid)
1011+
static int __do_i2c_hid_core_initial_power_up(struct i2c_hid *ihid)
10081012
{
10091013
struct i2c_client *client = ihid->client;
10101014
struct hid_device *hid = ihid->hid;
@@ -1058,6 +1062,83 @@ static int i2c_hid_core_initial_power_up(struct i2c_hid *ihid)
10581062
return ret;
10591063
}
10601064

1065+
static int i2c_hid_core_panel_prepared(struct drm_panel_follower *follower)
1066+
{
1067+
struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
1068+
struct hid_device *hid = ihid->hid;
1069+
1070+
/*
1071+
* hid->version is set on the first power up. If it's still zero then
1072+
* this is the first power on so we should perform initial power up
1073+
* steps.
1074+
*/
1075+
if (!hid->version)
1076+
return __do_i2c_hid_core_initial_power_up(ihid);
1077+
1078+
return i2c_hid_core_resume(ihid);
1079+
}
1080+
1081+
static int i2c_hid_core_panel_unpreparing(struct drm_panel_follower *follower)
1082+
{
1083+
struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
1084+
1085+
return i2c_hid_core_suspend(ihid, true);
1086+
}
1087+
1088+
static const struct drm_panel_follower_funcs i2c_hid_core_panel_follower_funcs = {
1089+
.panel_prepared = i2c_hid_core_panel_prepared,
1090+
.panel_unpreparing = i2c_hid_core_panel_unpreparing,
1091+
};
1092+
1093+
static int i2c_hid_core_register_panel_follower(struct i2c_hid *ihid)
1094+
{
1095+
struct device *dev = &ihid->client->dev;
1096+
int ret;
1097+
1098+
ihid->is_panel_follower = true;
1099+
ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_funcs;
1100+
1101+
/*
1102+
* If we're not in control of our own power up/power down then we can't
1103+
* do the logic to manage wakeups. Give a warning if a user thought
1104+
* that was possible then force the capability off.
1105+
*/
1106+
if (device_can_wakeup(dev)) {
1107+
dev_warn(dev, "Can't wakeup if following panel\n");
1108+
device_set_wakeup_capable(dev, false);
1109+
}
1110+
1111+
ret = drm_panel_add_follower(dev, &ihid->panel_follower);
1112+
if (ret)
1113+
return ret;
1114+
1115+
return 0;
1116+
}
1117+
1118+
static int i2c_hid_core_initial_power_up(struct i2c_hid *ihid)
1119+
{
1120+
/*
1121+
* If we're a panel follower, we'll register and do our initial power
1122+
* up when the panel turns on; otherwise we do it right away.
1123+
*/
1124+
if (drm_is_panel_follower(&ihid->client->dev))
1125+
return i2c_hid_core_register_panel_follower(ihid);
1126+
else
1127+
return __do_i2c_hid_core_initial_power_up(ihid);
1128+
}
1129+
1130+
static void i2c_hid_core_final_power_down(struct i2c_hid *ihid)
1131+
{
1132+
/*
1133+
* If we're a follower, the act of unfollowing will cause us to be
1134+
* powered down. Otherwise we need to manually do it.
1135+
*/
1136+
if (ihid->is_panel_follower)
1137+
drm_panel_remove_follower(&ihid->panel_follower);
1138+
else
1139+
i2c_hid_core_suspend(ihid, true);
1140+
}
1141+
10611142
int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
10621143
u16 hid_descriptor_address, u32 quirks)
10631144
{
@@ -1143,7 +1224,7 @@ void i2c_hid_core_remove(struct i2c_client *client)
11431224
struct i2c_hid *ihid = i2c_get_clientdata(client);
11441225
struct hid_device *hid;
11451226

1146-
i2c_hid_core_suspend(ihid, true);
1227+
i2c_hid_core_final_power_down(ihid);
11471228

11481229
hid = ihid->hid;
11491230
hid_destroy_device(hid);
@@ -1171,6 +1252,9 @@ static int i2c_hid_core_pm_suspend(struct device *dev)
11711252
struct i2c_client *client = to_i2c_client(dev);
11721253
struct i2c_hid *ihid = i2c_get_clientdata(client);
11731254

1255+
if (ihid->is_panel_follower)
1256+
return 0;
1257+
11741258
return i2c_hid_core_suspend(ihid, false);
11751259
}
11761260

@@ -1179,6 +1263,9 @@ static int i2c_hid_core_pm_resume(struct device *dev)
11791263
struct i2c_client *client = to_i2c_client(dev);
11801264
struct i2c_hid *ihid = i2c_get_clientdata(client);
11811265

1266+
if (ihid->is_panel_follower)
1267+
return 0;
1268+
11821269
return i2c_hid_core_resume(ihid);
11831270
}
11841271

0 commit comments

Comments
 (0)