Skip to content

Commit 1fd41e5

Browse files
kekrbyJiri Kosina
authored andcommitted
HID: hid-appletb-bl: add driver for the backlight of Apple Touch Bars
This commit adds a driver for the backlight of Apple Touch Bars on x86 Macs. Note that currently only T2 Macs are supported. This driver is based on previous work done by Ronald Tschalär <[email protected]>. Signed-off-by: Kerem Karabay <[email protected]> Co-developed-by: Aditya Garg <[email protected]> Signed-off-by: Aditya Garg <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 27c0278 commit 1fd41e5

File tree

4 files changed

+221
-1
lines changed

4 files changed

+221
-1
lines changed

drivers/hid/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ config HID_APPLEIR
148148

149149
Say Y here if you want support for Apple infrared remote control.
150150

151+
config HID_APPLETB_BL
152+
tristate "Apple Touch Bar Backlight"
153+
depends on BACKLIGHT_CLASS_DEVICE
154+
help
155+
Say Y here if you want support for the backlight of Touch Bars on x86
156+
MacBook Pros.
157+
158+
To compile this driver as a module, choose M here: the
159+
module will be called hid-appletb-bl.
160+
151161
config HID_ASUS
152162
tristate "Asus"
153163
depends on USB_HID

drivers/hid/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ obj-$(CONFIG_HID_ALPS) += hid-alps.o
2929
obj-$(CONFIG_HID_ACRUX) += hid-axff.o
3030
obj-$(CONFIG_HID_APPLE) += hid-apple.o
3131
obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o
32+
obj-$(CONFIG_HID_APPLETB_BL) += hid-appletb-bl.o
3233
obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o
3334
obj-$(CONFIG_HID_ASUS) += hid-asus.o
3435
obj-$(CONFIG_HID_AUREAL) += hid-aureal.o

drivers/hid/hid-appletb-bl.c

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Apple Touch Bar Backlight Driver
4+
*
5+
* Copyright (c) 2017-2018 Ronald Tschalär
6+
* Copyright (c) 2022-2023 Kerem Karabay <[email protected]>
7+
*/
8+
9+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10+
11+
#include <linux/hid.h>
12+
#include <linux/backlight.h>
13+
#include <linux/device.h>
14+
15+
#include "hid-ids.h"
16+
17+
#define APPLETB_BL_ON 1
18+
#define APPLETB_BL_DIM 3
19+
#define APPLETB_BL_OFF 4
20+
21+
#define HID_UP_APPLEVENDOR_TB_BL 0xff120000
22+
23+
#define HID_VD_APPLE_TB_BRIGHTNESS 0xff120001
24+
#define HID_USAGE_AUX1 0xff120020
25+
#define HID_USAGE_BRIGHTNESS 0xff120021
26+
27+
static int appletb_bl_def_brightness = 2;
28+
module_param_named(brightness, appletb_bl_def_brightness, int, 0444);
29+
MODULE_PARM_DESC(brightness, "Default brightness:\n"
30+
" 0 - Touchbar is off\n"
31+
" 1 - Dim brightness\n"
32+
" [2] - Full brightness");
33+
34+
struct appletb_bl {
35+
struct hid_field *aux1_field, *brightness_field;
36+
struct backlight_device *bdev;
37+
38+
bool full_on;
39+
};
40+
41+
static const u8 appletb_bl_brightness_map[] = {
42+
APPLETB_BL_OFF,
43+
APPLETB_BL_DIM,
44+
APPLETB_BL_ON,
45+
};
46+
47+
static int appletb_bl_set_brightness(struct appletb_bl *bl, u8 brightness)
48+
{
49+
struct hid_report *report = bl->brightness_field->report;
50+
struct hid_device *hdev = report->device;
51+
int ret;
52+
53+
ret = hid_set_field(bl->aux1_field, 0, 1);
54+
if (ret) {
55+
hid_err(hdev, "Failed to set auxiliary field (%pe)\n", ERR_PTR(ret));
56+
return ret;
57+
}
58+
59+
ret = hid_set_field(bl->brightness_field, 0, brightness);
60+
if (ret) {
61+
hid_err(hdev, "Failed to set brightness field (%pe)\n", ERR_PTR(ret));
62+
return ret;
63+
}
64+
65+
if (!bl->full_on) {
66+
ret = hid_hw_power(hdev, PM_HINT_FULLON);
67+
if (ret < 0) {
68+
hid_err(hdev, "Device didn't power on (%pe)\n", ERR_PTR(ret));
69+
return ret;
70+
}
71+
72+
bl->full_on = true;
73+
}
74+
75+
hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
76+
77+
if (brightness == APPLETB_BL_OFF) {
78+
hid_hw_power(hdev, PM_HINT_NORMAL);
79+
bl->full_on = false;
80+
}
81+
82+
return 0;
83+
}
84+
85+
static int appletb_bl_update_status(struct backlight_device *bdev)
86+
{
87+
struct appletb_bl *bl = bl_get_data(bdev);
88+
u8 brightness;
89+
90+
if (backlight_is_blank(bdev))
91+
brightness = APPLETB_BL_OFF;
92+
else
93+
brightness = appletb_bl_brightness_map[backlight_get_brightness(bdev)];
94+
95+
return appletb_bl_set_brightness(bl, brightness);
96+
}
97+
98+
static const struct backlight_ops appletb_bl_backlight_ops = {
99+
.options = BL_CORE_SUSPENDRESUME,
100+
.update_status = appletb_bl_update_status,
101+
};
102+
103+
static int appletb_bl_probe(struct hid_device *hdev, const struct hid_device_id *id)
104+
{
105+
struct hid_field *aux1_field, *brightness_field;
106+
struct backlight_properties bl_props = { 0 };
107+
struct device *dev = &hdev->dev;
108+
struct appletb_bl *bl;
109+
int ret;
110+
111+
ret = hid_parse(hdev);
112+
if (ret)
113+
return dev_err_probe(dev, ret, "HID parse failed\n");
114+
115+
aux1_field = hid_find_field(hdev, HID_FEATURE_REPORT,
116+
HID_VD_APPLE_TB_BRIGHTNESS, HID_USAGE_AUX1);
117+
118+
brightness_field = hid_find_field(hdev, HID_FEATURE_REPORT,
119+
HID_VD_APPLE_TB_BRIGHTNESS, HID_USAGE_BRIGHTNESS);
120+
121+
if (!aux1_field || !brightness_field)
122+
return -ENODEV;
123+
124+
if (aux1_field->report != brightness_field->report)
125+
return dev_err_probe(dev, -ENODEV, "Encountered unexpected report structure\n");
126+
127+
bl = devm_kzalloc(dev, sizeof(*bl), GFP_KERNEL);
128+
if (!bl)
129+
return -ENOMEM;
130+
131+
ret = hid_hw_start(hdev, HID_CONNECT_DRIVER);
132+
if (ret)
133+
return dev_err_probe(dev, ret, "HID hardware start failed\n");
134+
135+
ret = hid_hw_open(hdev);
136+
if (ret) {
137+
dev_err_probe(dev, ret, "HID hardware open failed\n");
138+
goto stop_hw;
139+
}
140+
141+
bl->aux1_field = aux1_field;
142+
bl->brightness_field = brightness_field;
143+
144+
if (appletb_bl_def_brightness == 0)
145+
ret = appletb_bl_set_brightness(bl, APPLETB_BL_OFF);
146+
else if (appletb_bl_def_brightness == 1)
147+
ret = appletb_bl_set_brightness(bl, APPLETB_BL_DIM);
148+
else
149+
ret = appletb_bl_set_brightness(bl, APPLETB_BL_ON);
150+
151+
if (ret) {
152+
dev_err_probe(dev, ret, "Failed to set touch bar brightness to off\n");
153+
goto close_hw;
154+
}
155+
156+
bl_props.type = BACKLIGHT_RAW;
157+
bl_props.max_brightness = ARRAY_SIZE(appletb_bl_brightness_map) - 1;
158+
159+
bl->bdev = devm_backlight_device_register(dev, "appletb_backlight", dev, bl,
160+
&appletb_bl_backlight_ops, &bl_props);
161+
if (IS_ERR(bl->bdev)) {
162+
ret = PTR_ERR(bl->bdev);
163+
dev_err_probe(dev, ret, "Failed to register backlight device\n");
164+
goto close_hw;
165+
}
166+
167+
hid_set_drvdata(hdev, bl);
168+
169+
return 0;
170+
171+
close_hw:
172+
hid_hw_close(hdev);
173+
stop_hw:
174+
hid_hw_stop(hdev);
175+
176+
return ret;
177+
}
178+
179+
static void appletb_bl_remove(struct hid_device *hdev)
180+
{
181+
struct appletb_bl *bl = hid_get_drvdata(hdev);
182+
183+
appletb_bl_set_brightness(bl, APPLETB_BL_OFF);
184+
185+
hid_hw_close(hdev);
186+
hid_hw_stop(hdev);
187+
}
188+
189+
static const struct hid_device_id appletb_bl_hid_ids[] = {
190+
/* MacBook Pro's 2018, 2019, with T2 chip: iBridge DFR Brightness */
191+
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
192+
{ }
193+
};
194+
MODULE_DEVICE_TABLE(hid, appletb_bl_hid_ids);
195+
196+
static struct hid_driver appletb_bl_hid_driver = {
197+
.name = "hid-appletb-bl",
198+
.id_table = appletb_bl_hid_ids,
199+
.probe = appletb_bl_probe,
200+
.remove = appletb_bl_remove,
201+
};
202+
module_hid_driver(appletb_bl_hid_driver);
203+
204+
MODULE_AUTHOR("Ronald Tschalär");
205+
MODULE_AUTHOR("Kerem Karabay <[email protected]>");
206+
MODULE_DESCRIPTION("MacBookPro Touch Bar Backlight Driver");
207+
MODULE_LICENSE("GPL");

drivers/hid/hid-quirks.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ static const struct hid_device_id hid_have_special_driver[] = {
328328
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
329329
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021) },
330330
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021) },
331-
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
332331
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
333332
#endif
334333
#if IS_ENABLED(CONFIG_HID_APPLEIR)
@@ -338,6 +337,9 @@ static const struct hid_device_id hid_have_special_driver[] = {
338337
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
339338
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL5) },
340339
#endif
340+
#if IS_ENABLED(CONFIG_HID_APPLETB_BL)
341+
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
342+
#endif
341343
#if IS_ENABLED(CONFIG_HID_ASUS)
342344
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD) },
343345
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD) },

0 commit comments

Comments
 (0)