xref: /linux/drivers/platform/surface/surface_aggregator_registry.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Surface System Aggregator Module (SSAM) client device registry.
4  *
5  * Registry for non-platform/non-ACPI SSAM client devices, i.e. devices that
6  * cannot be auto-detected. Provides device-hubs and performs instantiation
7  * for these devices.
8  *
9  * Copyright (C) 2020-2022 Maximilian Luz <luzmaximilian@gmail.com>
10  */
11 
12 #include <linux/acpi.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/types.h>
18 
19 #include <linux/surface_aggregator/device.h>
20 
21 
22 /* -- Device registry. ------------------------------------------------------ */
23 
24 /*
25  * SSAM device names follow the SSAM module alias, meaning they are prefixed
26  * with 'ssam:', followed by domain, category, target ID, instance ID, and
27  * function, each encoded as two-digit hexadecimal, separated by ':'. In other
28  * words, it follows the scheme
29  *
30  *      ssam:dd:cc:tt:ii:ff
31  *
32  * Where, 'dd', 'cc', 'tt', 'ii', and 'ff' are the two-digit hexadecimal
33  * values mentioned above, respectively.
34  */
35 
36 /* Root node. */
37 static const struct software_node ssam_node_root = {
38 	.name = "ssam_platform_hub",
39 };
40 
41 /* KIP device hub (connects keyboard cover devices on Surface Pro 8). */
42 static const struct software_node ssam_node_hub_kip = {
43 	.name = "ssam:00:00:01:0e:00",
44 	.parent = &ssam_node_root,
45 };
46 
47 /* Base device hub (devices attached to Surface Book 3 base). */
48 static const struct software_node ssam_node_hub_base = {
49 	.name = "ssam:00:00:01:11:00",
50 	.parent = &ssam_node_root,
51 };
52 
53 /* AC adapter. */
54 static const struct software_node ssam_node_bat_ac = {
55 	.name = "ssam:01:02:01:01:01",
56 	.parent = &ssam_node_root,
57 };
58 
59 /* Primary battery. */
60 static const struct software_node ssam_node_bat_main = {
61 	.name = "ssam:01:02:01:01:00",
62 	.parent = &ssam_node_root,
63 };
64 
65 /* Secondary battery (Surface Book 3). */
66 static const struct software_node ssam_node_bat_sb3base = {
67 	.name = "ssam:01:02:02:01:00",
68 	.parent = &ssam_node_hub_base,
69 };
70 
71 /* Platform profile / performance-mode device without a fan. */
72 static const struct software_node ssam_node_tmp_perf_profile = {
73 	.name = "ssam:01:03:01:00:01",
74 	.parent = &ssam_node_root,
75 };
76 
77 /* Platform profile / performance-mode device with a fan, such that
78  * the fan controller profile can also be switched.
79  */
80 static const struct property_entry ssam_node_tmp_perf_profile_has_fan[] = {
81 	PROPERTY_ENTRY_BOOL("has_fan"),
82 	{ }
83 };
84 
85 static const struct software_node ssam_node_tmp_perf_profile_with_fan = {
86 	.name = "ssam:01:03:01:00:01",
87 	.parent = &ssam_node_root,
88 	.properties = ssam_node_tmp_perf_profile_has_fan,
89 };
90 
91 /* Thermal sensors. */
92 static const struct software_node ssam_node_tmp_sensors = {
93 	.name = "ssam:01:03:01:00:02",
94 	.parent = &ssam_node_root,
95 };
96 
97 /* Fan speed function. */
98 static const struct software_node ssam_node_fan_speed = {
99 	.name = "ssam:01:05:01:01:01",
100 	.parent = &ssam_node_root,
101 };
102 
103 /* Tablet-mode switch via KIP subsystem. */
104 static const struct software_node ssam_node_kip_tablet_switch = {
105 	.name = "ssam:01:0e:01:00:01",
106 	.parent = &ssam_node_root,
107 };
108 
109 /* DTX / detachment-system device (Surface Book 3). */
110 static const struct software_node ssam_node_bas_dtx = {
111 	.name = "ssam:01:11:01:00:00",
112 	.parent = &ssam_node_root,
113 };
114 
115 /* HID keyboard (SAM, TID=1). */
116 static const struct software_node ssam_node_hid_sam_keyboard = {
117 	.name = "ssam:01:15:01:01:00",
118 	.parent = &ssam_node_root,
119 };
120 
121 /* HID pen stash (SAM, TID=1; pen taken / stashed away evens). */
122 static const struct software_node ssam_node_hid_sam_penstash = {
123 	.name = "ssam:01:15:01:02:00",
124 	.parent = &ssam_node_root,
125 };
126 
127 /* HID touchpad (SAM, TID=1). */
128 static const struct software_node ssam_node_hid_sam_touchpad = {
129 	.name = "ssam:01:15:01:03:00",
130 	.parent = &ssam_node_root,
131 };
132 
133 /* HID device instance 6 (SAM, TID=1, HID sensor collection). */
134 static const struct software_node ssam_node_hid_sam_sensors = {
135 	.name = "ssam:01:15:01:06:00",
136 	.parent = &ssam_node_root,
137 };
138 
139 /* HID device instance 7 (SAM, TID=1, UCM UCSI HID client). */
140 static const struct software_node ssam_node_hid_sam_ucm_ucsi = {
141 	.name = "ssam:01:15:01:07:00",
142 	.parent = &ssam_node_root,
143 };
144 
145 /* HID system controls (SAM, TID=1). */
146 static const struct software_node ssam_node_hid_sam_sysctrl = {
147 	.name = "ssam:01:15:01:08:00",
148 	.parent = &ssam_node_root,
149 };
150 
151 /* HID keyboard. */
152 static const struct software_node ssam_node_hid_main_keyboard = {
153 	.name = "ssam:01:15:02:01:00",
154 	.parent = &ssam_node_root,
155 };
156 
157 /* HID touchpad. */
158 static const struct software_node ssam_node_hid_main_touchpad = {
159 	.name = "ssam:01:15:02:03:00",
160 	.parent = &ssam_node_root,
161 };
162 
163 /* HID device instance 5 (unknown HID device). */
164 static const struct software_node ssam_node_hid_main_iid5 = {
165 	.name = "ssam:01:15:02:05:00",
166 	.parent = &ssam_node_root,
167 };
168 
169 /* HID keyboard (base hub). */
170 static const struct software_node ssam_node_hid_base_keyboard = {
171 	.name = "ssam:01:15:02:01:00",
172 	.parent = &ssam_node_hub_base,
173 };
174 
175 /* HID touchpad (base hub). */
176 static const struct software_node ssam_node_hid_base_touchpad = {
177 	.name = "ssam:01:15:02:03:00",
178 	.parent = &ssam_node_hub_base,
179 };
180 
181 /* HID device instance 5 (unknown HID device, base hub). */
182 static const struct software_node ssam_node_hid_base_iid5 = {
183 	.name = "ssam:01:15:02:05:00",
184 	.parent = &ssam_node_hub_base,
185 };
186 
187 /* HID device instance 6 (unknown HID device, base hub). */
188 static const struct software_node ssam_node_hid_base_iid6 = {
189 	.name = "ssam:01:15:02:06:00",
190 	.parent = &ssam_node_hub_base,
191 };
192 
193 /* HID keyboard (KIP hub). */
194 static const struct software_node ssam_node_hid_kip_keyboard = {
195 	.name = "ssam:01:15:02:01:00",
196 	.parent = &ssam_node_hub_kip,
197 };
198 
199 /* HID pen stash (KIP hub; pen taken / stashed away evens). */
200 static const struct software_node ssam_node_hid_kip_penstash = {
201 	.name = "ssam:01:15:02:02:00",
202 	.parent = &ssam_node_hub_kip,
203 };
204 
205 /* HID touchpad (KIP hub). */
206 static const struct software_node ssam_node_hid_kip_touchpad = {
207 	.name = "ssam:01:15:02:03:00",
208 	.parent = &ssam_node_hub_kip,
209 };
210 
211 /* HID device instance 5 (KIP hub, type-cover firmware update). */
212 static const struct software_node ssam_node_hid_kip_fwupd = {
213 	.name = "ssam:01:15:02:05:00",
214 	.parent = &ssam_node_hub_kip,
215 };
216 
217 /* Tablet-mode switch via POS subsystem. */
218 static const struct software_node ssam_node_pos_tablet_switch = {
219 	.name = "ssam:01:26:01:00:01",
220 	.parent = &ssam_node_root,
221 };
222 
223 /*
224  * Devices for 5th- and 6th-generations models:
225  * - Surface Book 2,
226  * - Surface Laptop 1 and 2,
227  * - Surface Pro 5 and 6.
228  */
229 static const struct software_node *ssam_node_group_gen5[] = {
230 	&ssam_node_root,
231 	&ssam_node_tmp_perf_profile,
232 	NULL,
233 };
234 
235 /* Devices for Surface Book 3. */
236 static const struct software_node *ssam_node_group_sb3[] = {
237 	&ssam_node_root,
238 	&ssam_node_hub_base,
239 	&ssam_node_bat_ac,
240 	&ssam_node_bat_main,
241 	&ssam_node_bat_sb3base,
242 	&ssam_node_tmp_perf_profile,
243 	&ssam_node_bas_dtx,
244 	&ssam_node_hid_base_keyboard,
245 	&ssam_node_hid_base_touchpad,
246 	&ssam_node_hid_base_iid5,
247 	&ssam_node_hid_base_iid6,
248 	NULL,
249 };
250 
251 /* Devices for Surface Laptop 3 and 4. */
252 static const struct software_node *ssam_node_group_sl3[] = {
253 	&ssam_node_root,
254 	&ssam_node_bat_ac,
255 	&ssam_node_bat_main,
256 	&ssam_node_tmp_perf_profile,
257 	&ssam_node_hid_main_keyboard,
258 	&ssam_node_hid_main_touchpad,
259 	&ssam_node_hid_main_iid5,
260 	NULL,
261 };
262 
263 /* Devices for Surface Laptop 5. */
264 static const struct software_node *ssam_node_group_sl5[] = {
265 	&ssam_node_root,
266 	&ssam_node_bat_ac,
267 	&ssam_node_bat_main,
268 	&ssam_node_tmp_perf_profile_with_fan,
269 	&ssam_node_tmp_sensors,
270 	&ssam_node_fan_speed,
271 	&ssam_node_hid_main_keyboard,
272 	&ssam_node_hid_main_touchpad,
273 	&ssam_node_hid_main_iid5,
274 	&ssam_node_hid_sam_ucm_ucsi,
275 	NULL,
276 };
277 
278 /* Devices for Surface Laptop 6. */
279 static const struct software_node *ssam_node_group_sl6[] = {
280 	&ssam_node_root,
281 	&ssam_node_bat_ac,
282 	&ssam_node_bat_main,
283 	&ssam_node_tmp_perf_profile_with_fan,
284 	&ssam_node_tmp_sensors,
285 	&ssam_node_fan_speed,
286 	&ssam_node_hid_main_keyboard,
287 	&ssam_node_hid_main_touchpad,
288 	&ssam_node_hid_main_iid5,
289 	&ssam_node_hid_sam_sensors,
290 	&ssam_node_hid_sam_ucm_ucsi,
291 	NULL,
292 };
293 
294 /* Devices for Surface Laptop Studio 1. */
295 static const struct software_node *ssam_node_group_sls1[] = {
296 	&ssam_node_root,
297 	&ssam_node_bat_ac,
298 	&ssam_node_bat_main,
299 	&ssam_node_tmp_perf_profile,
300 	&ssam_node_pos_tablet_switch,
301 	&ssam_node_hid_sam_keyboard,
302 	&ssam_node_hid_sam_penstash,
303 	&ssam_node_hid_sam_touchpad,
304 	&ssam_node_hid_sam_sensors,
305 	&ssam_node_hid_sam_ucm_ucsi,
306 	&ssam_node_hid_sam_sysctrl,
307 	NULL,
308 };
309 
310 /* Devices for Surface Laptop Studio 2. */
311 static const struct software_node *ssam_node_group_sls2[] = {
312 	&ssam_node_root,
313 	&ssam_node_bat_ac,
314 	&ssam_node_bat_main,
315 	&ssam_node_tmp_perf_profile_with_fan,
316 	&ssam_node_tmp_sensors,
317 	&ssam_node_fan_speed,
318 	&ssam_node_pos_tablet_switch,
319 	&ssam_node_hid_sam_keyboard,
320 	&ssam_node_hid_sam_penstash,
321 	&ssam_node_hid_sam_sensors,
322 	&ssam_node_hid_sam_ucm_ucsi,
323 	NULL,
324 };
325 
326 /* Devices for Surface Laptop Go. */
327 static const struct software_node *ssam_node_group_slg1[] = {
328 	&ssam_node_root,
329 	&ssam_node_bat_ac,
330 	&ssam_node_bat_main,
331 	&ssam_node_tmp_perf_profile,
332 	NULL,
333 };
334 
335 /* Devices for Surface Pro 7 and Surface Pro 7+. */
336 static const struct software_node *ssam_node_group_sp7[] = {
337 	&ssam_node_root,
338 	&ssam_node_bat_ac,
339 	&ssam_node_bat_main,
340 	&ssam_node_tmp_perf_profile,
341 	NULL,
342 };
343 
344 /* Devices for Surface Pro 8 */
345 static const struct software_node *ssam_node_group_sp8[] = {
346 	&ssam_node_root,
347 	&ssam_node_hub_kip,
348 	&ssam_node_bat_ac,
349 	&ssam_node_bat_main,
350 	&ssam_node_tmp_perf_profile,
351 	&ssam_node_kip_tablet_switch,
352 	&ssam_node_hid_kip_keyboard,
353 	&ssam_node_hid_kip_penstash,
354 	&ssam_node_hid_kip_touchpad,
355 	&ssam_node_hid_kip_fwupd,
356 	&ssam_node_hid_sam_sensors,
357 	&ssam_node_hid_sam_ucm_ucsi,
358 	NULL,
359 };
360 
361 /* Devices for Surface Pro 9 and 10 */
362 static const struct software_node *ssam_node_group_sp9[] = {
363 	&ssam_node_root,
364 	&ssam_node_hub_kip,
365 	&ssam_node_bat_ac,
366 	&ssam_node_bat_main,
367 	&ssam_node_tmp_perf_profile_with_fan,
368 	&ssam_node_tmp_sensors,
369 	&ssam_node_fan_speed,
370 	&ssam_node_pos_tablet_switch,
371 	&ssam_node_hid_kip_keyboard,
372 	&ssam_node_hid_kip_penstash,
373 	&ssam_node_hid_kip_touchpad,
374 	&ssam_node_hid_kip_fwupd,
375 	&ssam_node_hid_sam_sensors,
376 	&ssam_node_hid_sam_ucm_ucsi,
377 	NULL,
378 };
379 
380 
381 /* -- SSAM platform/meta-hub driver. ---------------------------------------- */
382 
383 static const struct acpi_device_id ssam_platform_hub_match[] = {
384 	/* Surface Pro 4, 5, and 6 (OMBR < 0x10) */
385 	{ "MSHW0081", (unsigned long)ssam_node_group_gen5 },
386 
387 	/* Surface Pro 6 (OMBR >= 0x10) */
388 	{ "MSHW0111", (unsigned long)ssam_node_group_gen5 },
389 
390 	/* Surface Pro 7 */
391 	{ "MSHW0116", (unsigned long)ssam_node_group_sp7 },
392 
393 	/* Surface Pro 7+ */
394 	{ "MSHW0119", (unsigned long)ssam_node_group_sp7 },
395 
396 	/* Surface Pro 8 */
397 	{ "MSHW0263", (unsigned long)ssam_node_group_sp8 },
398 
399 	/* Surface Pro 9 */
400 	{ "MSHW0343", (unsigned long)ssam_node_group_sp9 },
401 
402 	/* Surface Pro 10 */
403 	{ "MSHW0510", (unsigned long)ssam_node_group_sp9 },
404 
405 	/* Surface Book 2 */
406 	{ "MSHW0107", (unsigned long)ssam_node_group_gen5 },
407 
408 	/* Surface Book 3 */
409 	{ "MSHW0117", (unsigned long)ssam_node_group_sb3 },
410 
411 	/* Surface Laptop 1 */
412 	{ "MSHW0086", (unsigned long)ssam_node_group_gen5 },
413 
414 	/* Surface Laptop 2 */
415 	{ "MSHW0112", (unsigned long)ssam_node_group_gen5 },
416 
417 	/* Surface Laptop 3 (13", Intel) */
418 	{ "MSHW0114", (unsigned long)ssam_node_group_sl3 },
419 
420 	/* Surface Laptop 3 (15", AMD) and 4 (15", AMD) */
421 	{ "MSHW0110", (unsigned long)ssam_node_group_sl3 },
422 
423 	/* Surface Laptop 4 (13", Intel) */
424 	{ "MSHW0250", (unsigned long)ssam_node_group_sl3 },
425 
426 	/* Surface Laptop 5 */
427 	{ "MSHW0350", (unsigned long)ssam_node_group_sl5 },
428 
429 	/* Surface Laptop 6 */
430 	{ "MSHW0530", (unsigned long)ssam_node_group_sl6 },
431 
432 	/* Surface Laptop Go 1 */
433 	{ "MSHW0118", (unsigned long)ssam_node_group_slg1 },
434 
435 	/* Surface Laptop Go 2 */
436 	{ "MSHW0290", (unsigned long)ssam_node_group_slg1 },
437 
438 	/* Surface Laptop Go 3 */
439 	{ "MSHW0440", (unsigned long)ssam_node_group_slg1 },
440 
441 	/* Surface Laptop Studio 1 */
442 	{ "MSHW0123", (unsigned long)ssam_node_group_sls1 },
443 
444 	/* Surface Laptop Studio 2 */
445 	{ "MSHW0360", (unsigned long)ssam_node_group_sls2 },
446 
447 	{ },
448 };
449 MODULE_DEVICE_TABLE(acpi, ssam_platform_hub_match);
450 
451 static int ssam_platform_hub_probe(struct platform_device *pdev)
452 {
453 	const struct software_node **nodes;
454 	struct ssam_controller *ctrl;
455 	struct fwnode_handle *root;
456 	int status;
457 
458 	nodes = (const struct software_node **)acpi_device_get_match_data(&pdev->dev);
459 	if (!nodes)
460 		return -ENODEV;
461 
462 	/*
463 	 * As we're adding the SSAM client devices as children under this device
464 	 * and not the SSAM controller, we need to add a device link to the
465 	 * controller to ensure that we remove all of our devices before the
466 	 * controller is removed. This also guarantees proper ordering for
467 	 * suspend/resume of the devices on this hub.
468 	 */
469 	ctrl = ssam_client_bind(&pdev->dev);
470 	if (IS_ERR(ctrl))
471 		return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
472 
473 	status = software_node_register_node_group(nodes);
474 	if (status)
475 		return status;
476 
477 	root = software_node_fwnode(&ssam_node_root);
478 	if (!root) {
479 		software_node_unregister_node_group(nodes);
480 		return -ENOENT;
481 	}
482 
483 	set_secondary_fwnode(&pdev->dev, root);
484 
485 	status = __ssam_register_clients(&pdev->dev, ctrl, root);
486 	if (status) {
487 		set_secondary_fwnode(&pdev->dev, NULL);
488 		software_node_unregister_node_group(nodes);
489 	}
490 
491 	platform_set_drvdata(pdev, nodes);
492 	return status;
493 }
494 
495 static void ssam_platform_hub_remove(struct platform_device *pdev)
496 {
497 	const struct software_node **nodes = platform_get_drvdata(pdev);
498 
499 	ssam_remove_clients(&pdev->dev);
500 	set_secondary_fwnode(&pdev->dev, NULL);
501 	software_node_unregister_node_group(nodes);
502 }
503 
504 static struct platform_driver ssam_platform_hub_driver = {
505 	.probe = ssam_platform_hub_probe,
506 	.remove_new = ssam_platform_hub_remove,
507 	.driver = {
508 		.name = "surface_aggregator_platform_hub",
509 		.acpi_match_table = ssam_platform_hub_match,
510 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
511 	},
512 };
513 module_platform_driver(ssam_platform_hub_driver);
514 
515 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
516 MODULE_DESCRIPTION("Device-registry for Surface System Aggregator Module");
517 MODULE_LICENSE("GPL");
518