Skip to content

Commit ac91d5c

Browse files
author
Jiri Kosina
committed
Merge branch 'for-6.15/apple' into for-linus
- support for Apple Touch Bars (Kerem Karabay, Aditya Garg)
2 parents b3cc742 + 70409f3 commit ac91d5c

File tree

6 files changed

+757
-2
lines changed

6 files changed

+757
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
What: /sys/bus/hid/drivers/hid-appletb-kbd/<dev>/mode
2+
Date: September, 2023
3+
KernelVersion: 6.5
4+
5+
Description:
6+
The set of keys displayed on the Touch Bar.
7+
Valid values are:
8+
== =================
9+
0 Escape key only
10+
1 Function keys
11+
2 Media/brightness keys
12+
3 None
13+
== =================

drivers/hid/Kconfig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,31 @@ 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+
161+
config HID_APPLETB_KBD
162+
tristate "Apple Touch Bar Keyboard Mode"
163+
depends on USB_HID
164+
depends on BACKLIGHT_CLASS_DEVICE
165+
depends on INPUT
166+
select INPUT_SPARSEKMAP
167+
select HID_APPLETB_BL
168+
help
169+
Say Y here if you want support for the keyboard mode (escape,
170+
function, media and brightness keys) of Touch Bars on x86 MacBook
171+
Pros.
172+
173+
To compile this driver as a module, choose M here: the
174+
module will be called hid-appletb-kbd.
175+
151176
config HID_ASUS
152177
tristate "Asus"
153178
depends on USB_HID

drivers/hid/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ 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
33+
obj-$(CONFIG_HID_APPLETB_KBD) += hid-appletb-kbd.o
3234
obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o
3335
obj-$(CONFIG_HID_ASUS) += hid-asus.o
3436
obj-$(CONFIG_HID_AUREAL) += hid-aureal.o

drivers/hid/hid-appletb-bl.c

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
ret = appletb_bl_set_brightness(bl,
145+
appletb_bl_brightness_map[(appletb_bl_def_brightness > 2) ? 2 : appletb_bl_def_brightness]);
146+
147+
if (ret) {
148+
dev_err_probe(dev, ret, "Failed to set default touch bar brightness to %d\n",
149+
appletb_bl_def_brightness);
150+
goto close_hw;
151+
}
152+
153+
bl_props.type = BACKLIGHT_RAW;
154+
bl_props.max_brightness = ARRAY_SIZE(appletb_bl_brightness_map) - 1;
155+
156+
bl->bdev = devm_backlight_device_register(dev, "appletb_backlight", dev, bl,
157+
&appletb_bl_backlight_ops, &bl_props);
158+
if (IS_ERR(bl->bdev)) {
159+
ret = PTR_ERR(bl->bdev);
160+
dev_err_probe(dev, ret, "Failed to register backlight device\n");
161+
goto close_hw;
162+
}
163+
164+
hid_set_drvdata(hdev, bl);
165+
166+
return 0;
167+
168+
close_hw:
169+
hid_hw_close(hdev);
170+
stop_hw:
171+
hid_hw_stop(hdev);
172+
173+
return ret;
174+
}
175+
176+
static void appletb_bl_remove(struct hid_device *hdev)
177+
{
178+
struct appletb_bl *bl = hid_get_drvdata(hdev);
179+
180+
appletb_bl_set_brightness(bl, APPLETB_BL_OFF);
181+
182+
hid_hw_close(hdev);
183+
hid_hw_stop(hdev);
184+
}
185+
186+
static const struct hid_device_id appletb_bl_hid_ids[] = {
187+
/* MacBook Pro's 2018, 2019, with T2 chip: iBridge DFR Brightness */
188+
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
189+
{ }
190+
};
191+
MODULE_DEVICE_TABLE(hid, appletb_bl_hid_ids);
192+
193+
static struct hid_driver appletb_bl_hid_driver = {
194+
.name = "hid-appletb-bl",
195+
.id_table = appletb_bl_hid_ids,
196+
.probe = appletb_bl_probe,
197+
.remove = appletb_bl_remove,
198+
};
199+
module_hid_driver(appletb_bl_hid_driver);
200+
201+
MODULE_AUTHOR("Ronald Tschalär");
202+
MODULE_AUTHOR("Kerem Karabay <[email protected]>");
203+
MODULE_DESCRIPTION("MacBook Pro Touch Bar Backlight driver");
204+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)