From patchwork Tue Dec 31 17:37:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 854781 Received: from MA0PR01CU009.outbound.protection.outlook.com (mail-southindiaazolkn19010000.outbound.protection.outlook.com [52.103.67.0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E0B11A4E77; Tue, 31 Dec 2024 17:37:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.67.0 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735666643; cv=fail; b=feLX5cSkqb/HCRXHoSpGAb2qtIAm3l81jEQ3Fio6ZzePee7tQI1jMKuRZNc9r7Awy6BMURFm5EZY3NNx1XSMjhRhncmN1xIuW7xIDr2sAH3pg/nDTHQxYwToOOAcUdtdzSFUhOVVgcZ87kY+dXxH3KOkCJPWeSStv6bMhoWjVk0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735666643; c=relaxed/simple; bh=EB3/WAvxa7z8KPZBJE4SWavdtXmW2xBFffjI3oE8F1U=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=CtiD5tgEKSPDMq6WKBvYaIb+UOnV6ZNijSgm+8eX8sMV9Jvh2QwSyU13ok8jRHzX/u/H3c1neti6D5bK+n7+gQaKw/4fwFZDXXZQhl1RcUlxh2Hdtgn1IUfm+18BDamXbRGz5qHsRA4Eo0w70F3stuYEu99YUEOsFwU4pOw4zrY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com; spf=pass smtp.mailfrom=live.com; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b=bv6gNxio; arc=fail smtp.client-ip=52.103.67.0 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=live.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b="bv6gNxio" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=y1GpPTUZ0pgQvGjMgKBC2fuLSsG8hcoQlGi1YOM1vNoIvKdZhG4NitNJt6wLUU3gkzKEDN5oLk8/ZHEKFLEf4gs72NncHIoJzz09xa3jTyYUK+3nH1JQZiFO69pCdfxyfF/cVeW2AVGf/zza+9SZmPfNGc8Tgb+a+z+raB92BRLD8NTdwLF8oZVbQmvHVNBvMK/ITsW5meitVrtKiukgzMe2p5OCSOJe/7ZT1EdgS0JHwfjcpHHHVlZPm2b2kQ0HHw5flYVzj+Spce+MU+SUpUVmZWKtsdJB2kGcuQ0SxMJ7SL85JX+ZX0B3iv1O8StrN3vnUhTge9pHKO5gEnpHRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=EB3/WAvxa7z8KPZBJE4SWavdtXmW2xBFffjI3oE8F1U=; b=ospmkjKifk5QDV3V+M+T5mXi9nQVUcwNUuAgMbZ3+b1EVLxGoXEGFSyH4uF2tCWpOeLiNJ/jMeetMeSMVfrVWcsSAcfe4a5vZQwDyny4xC/jm5Y+WNodqdqNdCsczfNYgB1nPL4yhVXxYqQJGRanc8BSZpti1h86Ss3XsZpAM5jRU7Q7fcOJEOv7awpds/BHj+UgPii6RJ5T5ptlbogRmW0WVCkGRhDtzq6/9t2Vs2lzm2VGdmkdltq0SAAyThm0/BQmgM/pUlBRBlUdle4g+1bDgyIZkgklUYcd+p0uQ2Ig9Y0unvBvuE7abYIzcBeZ0vOaV8HQzSrtjMJ1e0DyLQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EB3/WAvxa7z8KPZBJE4SWavdtXmW2xBFffjI3oE8F1U=; b=bv6gNxiow3l269++J508FT1RlnlvxfyXfDqh9SZDj+zQ//52A4VF4O0vbyfyNZRbuidv15J5BeJWN/W1q/XmvujYEPqxdqrfostBdjjxpKn/YHY9rgR9o7kz/gLGQMvFo5n1MgkvuCD7HjaHaWC9JK+BWZfw189ogPUtWV2hwPnCGsDXOVlZHjRqoWRPHWPDRl9RwZfTk2dJeUSPuW9V/TXCvQyltyb6R+5p7aqL0bXdVDifGe+tC3OBx1w6Pl9GGbyLA5vh7koBZEutMne+tb9Koew61qrbszODuMHCFtRrlDVVl8i9SwEFW1kSk36Eb2z3PYNmXjWnoKxyV4cVgA== Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:cd::12) by MAXPR01MB4261.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:8::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8293.20; Tue, 31 Dec 2024 17:37:17 +0000 Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06]) by PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06%4]) with mapi id 15.20.8314.012; Tue, 31 Dec 2024 17:37:17 +0000 From: Aditya Garg To: "jkosina@suse.cz" , Jiri Kosina , Benjamin Tissoires , "bentiss@kernel.org" CC: Kerem Karabay , Orlando Chamberlain , "linux-input@vger.kernel.org" , Linux Kernel Mailing List Subject: [PATCH v2 4/4] HID: hid-appletb-kbd: add support for automatic brightness control while using the touchbar Thread-Topic: [PATCH v2 4/4] HID: hid-appletb-kbd: add support for automatic brightness control while using the touchbar Thread-Index: AQHbW6qiH3CE2+eB+UqAojbreyuqGQ== Date: Tue, 31 Dec 2024 17:37:17 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-IN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PN3PR01MB7728:EE_|MAXPR01MB4261:EE_ x-ms-office365-filtering-correlation-id: eff42b84-a7e4-4b2a-05c5-08dd29c1c535 x-microsoft-antispam: BCL:0; ARA:14566002|19110799003|15080799006|8062599003|7092599003|8060799006|461199028|440099028|3412199025|102099032; x-microsoft-antispam-message-info: =?utf-8?q?9BxD7+LWDEECaVrYz9dRruKFHSTAw2Y?= =?utf-8?q?gJx71KtvKfjZGZLwMkqKjMa4I9oYRBqwRMn6e5tNuh4GGlgdi1H5NyYhcioqzccOj?= =?utf-8?q?mthrGS9L2qJtqn5ZO7VBvZG7wIr+Dw3VD1exVe83VQuWB/c3N/NWTc+wkyi5FC0+A?= =?utf-8?q?yEngMkv9Urf9nzehaKrJdDWEOuzWmgLRwWWjr2v5IUrywjRyVRf0BLJVa5znEoE2s?= =?utf-8?q?8nPM2cdWFPJDgUYMqdxpgd1HW1dBbqgTKNC3ye8jyHqgoxJD6lddSyOgFTgzCnZ3P?= =?utf-8?q?dts4sZX8cHpCdcl622RCFbYSpJC7zOQiMcxuYkRU+atcBJ/uk3Kra985wu2oCz6so?= =?utf-8?q?dcUudwX60hrR7mK2D3OHL29lo/MmZqML3HUX7dvPewqHIy5nGzNGGYM92LOvSetrb?= =?utf-8?q?SlJJcwPWsmaRghjejHiHxI07Sd+LgnnM944TiWyN9DxSXijrQ6MS5+asKbOcBWXh1?= =?utf-8?q?a3FYozesAA4R+TkwpnNaYkzuFfrQqAQ0rPs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FJvwWZbl2YlL8F9MlNW8hGi5DqCZW?= =?utf-8?q?ULk5uzQyjAafkN8DzMw09Gwg4JFbn12gbQZGaX4CP5vP11yvf5cNNGQvNxQ22Vf5Y?= =?utf-8?q?rNNYe6JvCl9WVj5MxKBpSNVWt6Zb5toWeP9E6wQjJebhLqyMtA7S2yFPnx9Oh7EyS?= =?utf-8?q?KCPA1hf6//SFzMc6?= Content-ID: Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-7719-20-msonline-outlook-ae5c4.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: eff42b84-a7e4-4b2a-05c5-08dd29c1c535 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 31 Dec 2024 17:37:17.0067 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MAXPR01MB4261 From: Aditya Garg On Windows, if there is no input by the user for 60 sec, the Touch Bar dims automatically, and after further 15 sec, it turns off. On receiving input, the Touch Bar resets the timer and goes back to full brightness. This patch implements the same functionality. Signed-off-by: Aditya Garg --- drivers/hid/Kconfig | 2 + drivers/hid/hid-appletb-kbd.c | 83 +++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 5b16bfadc..4dadf42a2 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -161,8 +161,10 @@ config HID_APPLETB_BL config HID_APPLETB_KBD tristate "Apple Touch Bar Keyboard Mode" depends on USB_HID + depends on BACKLIGHT_CLASS_DEVICE depends on INPUT select INPUT_SPARSEKMAP + select HID_APPLETB_BL help Say Y here if you want support for the keyboard mode (escape, function, media and brightness keys) of Touch Bars on x86 MacBook diff --git a/drivers/hid/hid-appletb-kbd.c b/drivers/hid/hid-appletb-kbd.c index de5770500..fa28a691d 100644 --- a/drivers/hid/hid-appletb-kbd.c +++ b/drivers/hid/hid-appletb-kbd.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include "hid-ids.h" @@ -27,6 +29,7 @@ #define APPLETB_KBD_MODE_MAX APPLETB_KBD_MODE_OFF #define APPLETB_DEVID_KEYBOARD 1 +#define APPLETB_DEVID_TRACKPAD 2 #define HID_USAGE_MODE 0x00ff0004 @@ -41,14 +44,29 @@ static bool appletb_tb_fn_toggle = true; module_param_named(fntoggle, appletb_tb_fn_toggle, bool, 0644); MODULE_PARM_DESC(fntoggle, "Switch between Fn and media controls on pressing Fn key"); +static bool appletb_tb_autodim = true; +module_param_named(autodim, appletb_tb_autodim, bool, 0644); +MODULE_PARM_DESC(autodim, "Automatically dim and turn off the Touch Bar after some time"); + +static int appletb_tb_dim_timeout = 60; +module_param_named(dim_timeout, appletb_tb_dim_timeout, int, 0644); +MODULE_PARM_DESC(dim_timeout, "Dim timeout in sec"); + +static int appletb_tb_idle_timeout = 15; +module_param_named(idle_timeout, appletb_tb_idle_timeout, int, 0644); +MODULE_PARM_DESC(idle_timeout, "Idle timeout in sec"); + struct appletb_kbd { struct hid_field *mode_field; - - u8 saved_mode; - u8 current_mode; struct input_handler inp_handler; struct input_handle kbd_handle; - + struct input_handle tpd_handle; + struct backlight_device *backlight_dev; + struct timer_list inactivity_timer; + bool has_dimmed; + bool has_turned_off; + u8 saved_mode; + u8 current_mode; }; static const struct key_entry appletb_kbd_keymap[] = { @@ -146,6 +164,34 @@ static int appletb_tb_key_to_slot(unsigned int code) } } +static void appletb_inactivity_timer(struct timer_list *t) +{ + struct appletb_kbd *kbd = from_timer(kbd, t, inactivity_timer); + + if (kbd->backlight_dev && appletb_tb_autodim) { + if (!kbd->has_dimmed) { + backlight_device_set_brightness(kbd->backlight_dev, 1); + kbd->has_dimmed = true; + mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_idle_timeout * 1000)); + } else if (!kbd->has_turned_off) { + backlight_device_set_brightness(kbd->backlight_dev, 0); + kbd->has_turned_off = true; + } + } +} + +static void reset_inactivity_timer(struct appletb_kbd *kbd) +{ + if (kbd->backlight_dev && appletb_tb_autodim) { + if (kbd->has_dimmed || kbd->has_turned_off) { + backlight_device_set_brightness(kbd->backlight_dev, 2); + kbd->has_dimmed = false; + kbd->has_turned_off = false; + } + mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_dim_timeout * 1000)); + } +} + static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *field, struct hid_usage *usage, __s32 value) { @@ -170,6 +216,8 @@ static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *fiel if (slot < 0) return 0; + reset_inactivity_timer(kbd); + translation = sparse_keymap_entry_from_scancode(input, usage->code); if (translation && kbd->current_mode == APPLETB_KBD_MODE_SPCL) { @@ -186,6 +234,8 @@ static void appletb_kbd_inp_event(struct input_handle *handle, unsigned int type { struct appletb_kbd *kbd = handle->private; + reset_inactivity_timer(kbd); + if (type == EV_KEY && code == KEY_FN && appletb_tb_fn_toggle) { if (value == 1) { kbd->saved_mode = kbd->current_mode; @@ -211,6 +261,9 @@ static int appletb_kbd_inp_connect(struct input_handler *handler, if (id->driver_info == APPLETB_DEVID_KEYBOARD) { handle = &kbd->kbd_handle; handle->name = "tbkbd"; + } else if (id->driver_info == APPLETB_DEVID_TRACKPAD) { + handle = &kbd->tpd_handle; + handle->name = "tbtpd"; } else { return -ENOENT; } @@ -283,6 +336,15 @@ static const struct input_device_id appletb_kbd_input_devices[] = { .keybit = { [BIT_WORD(KEY_FN)] = BIT_MASK(KEY_FN) }, .driver_info = APPLETB_DEVID_KEYBOARD, }, + { + .flags = INPUT_DEVICE_ID_MATCH_BUS | + INPUT_DEVICE_ID_MATCH_VENDOR | + INPUT_DEVICE_ID_MATCH_KEYBIT, + .bustype = BUS_USB, + .vendor = USB_VENDOR_ID_APPLE, + .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) }, + .driver_info = APPLETB_DEVID_TRACKPAD, + }, { } }; @@ -339,6 +401,15 @@ static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id goto stop_hw; } + kbd->backlight_dev = backlight_device_get_by_name("appletb_backlight"); + if (!kbd->backlight_dev) + dev_err_probe(dev, ret, "Failed to get backlight device\n"); + else { + backlight_device_set_brightness(kbd->backlight_dev, 2); + timer_setup(&kbd->inactivity_timer, appletb_inactivity_timer, 0); + mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_dim_timeout * 1000)); + } + kbd->inp_handler.event = appletb_kbd_inp_event; kbd->inp_handler.connect = appletb_kbd_inp_connect; kbd->inp_handler.disconnect = appletb_kbd_inp_disconnect; @@ -377,6 +448,7 @@ static void appletb_kbd_remove(struct hid_device *hdev) appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF); input_unregister_handler(&kbd->inp_handler); + del_timer_sync(&kbd->inactivity_timer); hid_hw_close(hdev); hid_hw_stop(hdev); @@ -425,6 +497,9 @@ static struct hid_driver appletb_kbd_hid_driver = { }; module_hid_driver(appletb_kbd_hid_driver); +/* The backlight driver should be loaded before the keyboard driver is initialised*/ +MODULE_SOFTDEP("pre: hid_appletb_bl"); + MODULE_AUTHOR("Ronald Tschalär"); MODULE_AUTHOR("Kerem Karabay "); MODULE_DESCRIPTION("MacBookPro Touch Bar Keyboard Mode Driver");