xref: /openbmc/linux/drivers/platform/surface/surface_aggregator_tabletsw.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
19f794056SMaximilian Luz // SPDX-License-Identifier: GPL-2.0+
29f794056SMaximilian Luz /*
39f794056SMaximilian Luz  * Surface System Aggregator Module (SSAM) tablet mode switch driver.
49f794056SMaximilian Luz  *
59f794056SMaximilian Luz  * Copyright (C) 2022 Maximilian Luz <luzmaximilian@gmail.com>
69f794056SMaximilian Luz  */
79f794056SMaximilian Luz 
89f794056SMaximilian Luz #include <asm/unaligned.h>
99f794056SMaximilian Luz #include <linux/input.h>
109f794056SMaximilian Luz #include <linux/kernel.h>
119f794056SMaximilian Luz #include <linux/module.h>
129f794056SMaximilian Luz #include <linux/types.h>
139f794056SMaximilian Luz #include <linux/workqueue.h>
149f794056SMaximilian Luz 
159f794056SMaximilian Luz #include <linux/surface_aggregator/controller.h>
169f794056SMaximilian Luz #include <linux/surface_aggregator/device.h>
179f794056SMaximilian Luz 
189f794056SMaximilian Luz 
199f794056SMaximilian Luz /* -- SSAM generic tablet switch driver framework. -------------------------- */
209f794056SMaximilian Luz 
219f794056SMaximilian Luz struct ssam_tablet_sw;
229f794056SMaximilian Luz 
23b58a444dSMaximilian Luz struct ssam_tablet_sw_state {
24b58a444dSMaximilian Luz 	u32 source;
25b58a444dSMaximilian Luz 	u32 state;
26b58a444dSMaximilian Luz };
27b58a444dSMaximilian Luz 
289f794056SMaximilian Luz struct ssam_tablet_sw_ops {
29b58a444dSMaximilian Luz 	int (*get_state)(struct ssam_tablet_sw *sw, struct ssam_tablet_sw_state *state);
30b58a444dSMaximilian Luz 	const char *(*state_name)(struct ssam_tablet_sw *sw,
31b58a444dSMaximilian Luz 				  const struct ssam_tablet_sw_state *state);
32b58a444dSMaximilian Luz 	bool (*state_is_tablet_mode)(struct ssam_tablet_sw *sw,
33b58a444dSMaximilian Luz 				     const struct ssam_tablet_sw_state *state);
349f794056SMaximilian Luz };
359f794056SMaximilian Luz 
369f794056SMaximilian Luz struct ssam_tablet_sw {
379f794056SMaximilian Luz 	struct ssam_device *sdev;
389f794056SMaximilian Luz 
39b58a444dSMaximilian Luz 	struct ssam_tablet_sw_state state;
409f794056SMaximilian Luz 	struct work_struct update_work;
419f794056SMaximilian Luz 	struct input_dev *mode_switch;
429f794056SMaximilian Luz 
439f794056SMaximilian Luz 	struct ssam_tablet_sw_ops ops;
449f794056SMaximilian Luz 	struct ssam_event_notifier notif;
459f794056SMaximilian Luz };
469f794056SMaximilian Luz 
479f794056SMaximilian Luz struct ssam_tablet_sw_desc {
489f794056SMaximilian Luz 	struct {
499f794056SMaximilian Luz 		const char *name;
509f794056SMaximilian Luz 		const char *phys;
519f794056SMaximilian Luz 	} dev;
529f794056SMaximilian Luz 
539f794056SMaximilian Luz 	struct {
549f794056SMaximilian Luz 		u32 (*notify)(struct ssam_event_notifier *nf, const struct ssam_event *event);
55b58a444dSMaximilian Luz 		int (*get_state)(struct ssam_tablet_sw *sw, struct ssam_tablet_sw_state *state);
56b58a444dSMaximilian Luz 		const char *(*state_name)(struct ssam_tablet_sw *sw,
57b58a444dSMaximilian Luz 					  const struct ssam_tablet_sw_state *state);
58b58a444dSMaximilian Luz 		bool (*state_is_tablet_mode)(struct ssam_tablet_sw *sw,
59b58a444dSMaximilian Luz 					     const struct ssam_tablet_sw_state *state);
609f794056SMaximilian Luz 	} ops;
619f794056SMaximilian Luz 
629f794056SMaximilian Luz 	struct {
639f794056SMaximilian Luz 		struct ssam_event_registry reg;
649f794056SMaximilian Luz 		struct ssam_event_id id;
659f794056SMaximilian Luz 		enum ssam_event_mask mask;
669f794056SMaximilian Luz 		u8 flags;
679f794056SMaximilian Luz 	} event;
689f794056SMaximilian Luz };
699f794056SMaximilian Luz 
state_show(struct device * dev,struct device_attribute * attr,char * buf)709f794056SMaximilian Luz static ssize_t state_show(struct device *dev, struct device_attribute *attr, char *buf)
719f794056SMaximilian Luz {
729f794056SMaximilian Luz 	struct ssam_tablet_sw *sw = dev_get_drvdata(dev);
73b58a444dSMaximilian Luz 	const char *state = sw->ops.state_name(sw, &sw->state);
749f794056SMaximilian Luz 
759f794056SMaximilian Luz 	return sysfs_emit(buf, "%s\n", state);
769f794056SMaximilian Luz }
779f794056SMaximilian Luz static DEVICE_ATTR_RO(state);
789f794056SMaximilian Luz 
799f794056SMaximilian Luz static struct attribute *ssam_tablet_sw_attrs[] = {
809f794056SMaximilian Luz 	&dev_attr_state.attr,
819f794056SMaximilian Luz 	NULL,
829f794056SMaximilian Luz };
839f794056SMaximilian Luz 
849f794056SMaximilian Luz static const struct attribute_group ssam_tablet_sw_group = {
859f794056SMaximilian Luz 	.attrs = ssam_tablet_sw_attrs,
869f794056SMaximilian Luz };
879f794056SMaximilian Luz 
ssam_tablet_sw_update_workfn(struct work_struct * work)889f794056SMaximilian Luz static void ssam_tablet_sw_update_workfn(struct work_struct *work)
899f794056SMaximilian Luz {
909f794056SMaximilian Luz 	struct ssam_tablet_sw *sw = container_of(work, struct ssam_tablet_sw, update_work);
91b58a444dSMaximilian Luz 	struct ssam_tablet_sw_state state;
929f794056SMaximilian Luz 	int tablet, status;
939f794056SMaximilian Luz 
949f794056SMaximilian Luz 	status = sw->ops.get_state(sw, &state);
959f794056SMaximilian Luz 	if (status)
969f794056SMaximilian Luz 		return;
979f794056SMaximilian Luz 
98b58a444dSMaximilian Luz 	if (sw->state.source == state.source && sw->state.state == state.state)
999f794056SMaximilian Luz 		return;
1009f794056SMaximilian Luz 	sw->state = state;
1019f794056SMaximilian Luz 
1029f794056SMaximilian Luz 	/* Send SW_TABLET_MODE event. */
103b58a444dSMaximilian Luz 	tablet = sw->ops.state_is_tablet_mode(sw, &state);
1049f794056SMaximilian Luz 	input_report_switch(sw->mode_switch, SW_TABLET_MODE, tablet);
1059f794056SMaximilian Luz 	input_sync(sw->mode_switch);
1069f794056SMaximilian Luz }
1079f794056SMaximilian Luz 
ssam_tablet_sw_resume(struct device * dev)1089f794056SMaximilian Luz static int __maybe_unused ssam_tablet_sw_resume(struct device *dev)
1099f794056SMaximilian Luz {
1109f794056SMaximilian Luz 	struct ssam_tablet_sw *sw = dev_get_drvdata(dev);
1119f794056SMaximilian Luz 
1129f794056SMaximilian Luz 	schedule_work(&sw->update_work);
1139f794056SMaximilian Luz 	return 0;
1149f794056SMaximilian Luz }
1159f794056SMaximilian Luz static SIMPLE_DEV_PM_OPS(ssam_tablet_sw_pm_ops, NULL, ssam_tablet_sw_resume);
1169f794056SMaximilian Luz 
ssam_tablet_sw_probe(struct ssam_device * sdev)1179f794056SMaximilian Luz static int ssam_tablet_sw_probe(struct ssam_device *sdev)
1189f794056SMaximilian Luz {
1199f794056SMaximilian Luz 	const struct ssam_tablet_sw_desc *desc;
1209f794056SMaximilian Luz 	struct ssam_tablet_sw *sw;
1219f794056SMaximilian Luz 	int tablet, status;
1229f794056SMaximilian Luz 
1239f794056SMaximilian Luz 	desc = ssam_device_get_match_data(sdev);
1249f794056SMaximilian Luz 	if (!desc) {
1259f794056SMaximilian Luz 		WARN(1, "no driver match data specified");
1269f794056SMaximilian Luz 		return -EINVAL;
1279f794056SMaximilian Luz 	}
1289f794056SMaximilian Luz 
1299f794056SMaximilian Luz 	sw = devm_kzalloc(&sdev->dev, sizeof(*sw), GFP_KERNEL);
1309f794056SMaximilian Luz 	if (!sw)
1319f794056SMaximilian Luz 		return -ENOMEM;
1329f794056SMaximilian Luz 
1339f794056SMaximilian Luz 	sw->sdev = sdev;
1349f794056SMaximilian Luz 
1359f794056SMaximilian Luz 	sw->ops.get_state = desc->ops.get_state;
1369f794056SMaximilian Luz 	sw->ops.state_name = desc->ops.state_name;
1379f794056SMaximilian Luz 	sw->ops.state_is_tablet_mode = desc->ops.state_is_tablet_mode;
1389f794056SMaximilian Luz 
1399f794056SMaximilian Luz 	INIT_WORK(&sw->update_work, ssam_tablet_sw_update_workfn);
1409f794056SMaximilian Luz 
1419f794056SMaximilian Luz 	ssam_device_set_drvdata(sdev, sw);
1429f794056SMaximilian Luz 
1439f794056SMaximilian Luz 	/* Get initial state. */
1449f794056SMaximilian Luz 	status = sw->ops.get_state(sw, &sw->state);
1459f794056SMaximilian Luz 	if (status)
1469f794056SMaximilian Luz 		return status;
1479f794056SMaximilian Luz 
1489f794056SMaximilian Luz 	/* Set up tablet mode switch. */
1499f794056SMaximilian Luz 	sw->mode_switch = devm_input_allocate_device(&sdev->dev);
1509f794056SMaximilian Luz 	if (!sw->mode_switch)
1519f794056SMaximilian Luz 		return -ENOMEM;
1529f794056SMaximilian Luz 
1539f794056SMaximilian Luz 	sw->mode_switch->name = desc->dev.name;
1549f794056SMaximilian Luz 	sw->mode_switch->phys = desc->dev.phys;
1559f794056SMaximilian Luz 	sw->mode_switch->id.bustype = BUS_HOST;
1569f794056SMaximilian Luz 	sw->mode_switch->dev.parent = &sdev->dev;
1579f794056SMaximilian Luz 
158b58a444dSMaximilian Luz 	tablet = sw->ops.state_is_tablet_mode(sw, &sw->state);
1599f794056SMaximilian Luz 	input_set_capability(sw->mode_switch, EV_SW, SW_TABLET_MODE);
1609f794056SMaximilian Luz 	input_report_switch(sw->mode_switch, SW_TABLET_MODE, tablet);
1619f794056SMaximilian Luz 
1629f794056SMaximilian Luz 	status = input_register_device(sw->mode_switch);
1639f794056SMaximilian Luz 	if (status)
1649f794056SMaximilian Luz 		return status;
1659f794056SMaximilian Luz 
1669f794056SMaximilian Luz 	/* Set up notifier. */
1679f794056SMaximilian Luz 	sw->notif.base.priority = 0;
1689f794056SMaximilian Luz 	sw->notif.base.fn = desc->ops.notify;
1699f794056SMaximilian Luz 	sw->notif.event.reg = desc->event.reg;
1709f794056SMaximilian Luz 	sw->notif.event.id = desc->event.id;
1719f794056SMaximilian Luz 	sw->notif.event.mask = desc->event.mask;
1729f794056SMaximilian Luz 	sw->notif.event.flags = SSAM_EVENT_SEQUENCED;
1739f794056SMaximilian Luz 
1749f794056SMaximilian Luz 	status = ssam_device_notifier_register(sdev, &sw->notif);
1759f794056SMaximilian Luz 	if (status)
1769f794056SMaximilian Luz 		return status;
1779f794056SMaximilian Luz 
1789f794056SMaximilian Luz 	status = sysfs_create_group(&sdev->dev.kobj, &ssam_tablet_sw_group);
1799f794056SMaximilian Luz 	if (status)
1809f794056SMaximilian Luz 		goto err;
1819f794056SMaximilian Luz 
1829f794056SMaximilian Luz 	/* We might have missed events during setup, so check again. */
1839f794056SMaximilian Luz 	schedule_work(&sw->update_work);
1849f794056SMaximilian Luz 	return 0;
1859f794056SMaximilian Luz 
1869f794056SMaximilian Luz err:
1879f794056SMaximilian Luz 	ssam_device_notifier_unregister(sdev, &sw->notif);
1889f794056SMaximilian Luz 	cancel_work_sync(&sw->update_work);
1899f794056SMaximilian Luz 	return status;
1909f794056SMaximilian Luz }
1919f794056SMaximilian Luz 
ssam_tablet_sw_remove(struct ssam_device * sdev)1929f794056SMaximilian Luz static void ssam_tablet_sw_remove(struct ssam_device *sdev)
1939f794056SMaximilian Luz {
1949f794056SMaximilian Luz 	struct ssam_tablet_sw *sw = ssam_device_get_drvdata(sdev);
1959f794056SMaximilian Luz 
1969f794056SMaximilian Luz 	sysfs_remove_group(&sdev->dev.kobj, &ssam_tablet_sw_group);
1979f794056SMaximilian Luz 
1989f794056SMaximilian Luz 	ssam_device_notifier_unregister(sdev, &sw->notif);
1999f794056SMaximilian Luz 	cancel_work_sync(&sw->update_work);
2009f794056SMaximilian Luz }
2019f794056SMaximilian Luz 
2029f794056SMaximilian Luz 
2039f794056SMaximilian Luz /* -- SSAM KIP tablet switch implementation. -------------------------------- */
2049f794056SMaximilian Luz 
2059f794056SMaximilian Luz #define SSAM_EVENT_KIP_CID_COVER_STATE_CHANGED	0x1d
2069f794056SMaximilian Luz 
2079f794056SMaximilian Luz enum ssam_kip_cover_state {
2089f794056SMaximilian Luz 	SSAM_KIP_COVER_STATE_DISCONNECTED  = 0x01,
2099f794056SMaximilian Luz 	SSAM_KIP_COVER_STATE_CLOSED        = 0x02,
2109f794056SMaximilian Luz 	SSAM_KIP_COVER_STATE_LAPTOP        = 0x03,
2119f794056SMaximilian Luz 	SSAM_KIP_COVER_STATE_FOLDED_CANVAS = 0x04,
2129f794056SMaximilian Luz 	SSAM_KIP_COVER_STATE_FOLDED_BACK   = 0x05,
2139bed6670SMaximilian Luz 	SSAM_KIP_COVER_STATE_BOOK          = 0x06,
2149f794056SMaximilian Luz };
2159f794056SMaximilian Luz 
ssam_kip_cover_state_name(struct ssam_tablet_sw * sw,const struct ssam_tablet_sw_state * state)216b58a444dSMaximilian Luz static const char *ssam_kip_cover_state_name(struct ssam_tablet_sw *sw,
217b58a444dSMaximilian Luz 					     const struct ssam_tablet_sw_state *state)
2189f794056SMaximilian Luz {
219b58a444dSMaximilian Luz 	switch (state->state) {
2209f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_DISCONNECTED:
2219f794056SMaximilian Luz 		return "disconnected";
2229f794056SMaximilian Luz 
2239f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_CLOSED:
2249f794056SMaximilian Luz 		return "closed";
2259f794056SMaximilian Luz 
2269f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_LAPTOP:
2279f794056SMaximilian Luz 		return "laptop";
2289f794056SMaximilian Luz 
2299f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_FOLDED_CANVAS:
2309f794056SMaximilian Luz 		return "folded-canvas";
2319f794056SMaximilian Luz 
2329f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_FOLDED_BACK:
2339f794056SMaximilian Luz 		return "folded-back";
2349f794056SMaximilian Luz 
2359bed6670SMaximilian Luz 	case SSAM_KIP_COVER_STATE_BOOK:
2369bed6670SMaximilian Luz 		return "book";
2379bed6670SMaximilian Luz 
2389f794056SMaximilian Luz 	default:
239b58a444dSMaximilian Luz 		dev_warn(&sw->sdev->dev, "unknown KIP cover state: %u\n", state->state);
2409f794056SMaximilian Luz 		return "<unknown>";
2419f794056SMaximilian Luz 	}
2429f794056SMaximilian Luz }
2439f794056SMaximilian Luz 
ssam_kip_cover_state_is_tablet_mode(struct ssam_tablet_sw * sw,const struct ssam_tablet_sw_state * state)244b58a444dSMaximilian Luz static bool ssam_kip_cover_state_is_tablet_mode(struct ssam_tablet_sw *sw,
245b58a444dSMaximilian Luz 						const struct ssam_tablet_sw_state *state)
2469f794056SMaximilian Luz {
247b58a444dSMaximilian Luz 	switch (state->state) {
2489f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_DISCONNECTED:
2499f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_FOLDED_CANVAS:
2509f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_FOLDED_BACK:
2519bed6670SMaximilian Luz 	case SSAM_KIP_COVER_STATE_BOOK:
2529f794056SMaximilian Luz 		return true;
2539f794056SMaximilian Luz 
2549f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_CLOSED:
2559f794056SMaximilian Luz 	case SSAM_KIP_COVER_STATE_LAPTOP:
2569f794056SMaximilian Luz 		return false;
2579f794056SMaximilian Luz 
2589f794056SMaximilian Luz 	default:
259b58a444dSMaximilian Luz 		dev_warn(&sw->sdev->dev, "unknown KIP cover state: %d\n", state->state);
2609f794056SMaximilian Luz 		return true;
2619f794056SMaximilian Luz 	}
2629f794056SMaximilian Luz }
2639f794056SMaximilian Luz 
2649f794056SMaximilian Luz SSAM_DEFINE_SYNC_REQUEST_R(__ssam_kip_get_cover_state, u8, {
2659f794056SMaximilian Luz 	.target_category = SSAM_SSH_TC_KIP,
26636f672a4SMaximilian Luz 	.target_id       = SSAM_SSH_TID_SAM,
2679f794056SMaximilian Luz 	.command_id      = 0x1d,
2689f794056SMaximilian Luz 	.instance_id     = 0x00,
2699f794056SMaximilian Luz });
2709f794056SMaximilian Luz 
ssam_kip_get_cover_state(struct ssam_tablet_sw * sw,struct ssam_tablet_sw_state * state)271b58a444dSMaximilian Luz static int ssam_kip_get_cover_state(struct ssam_tablet_sw *sw, struct ssam_tablet_sw_state *state)
2729f794056SMaximilian Luz {
2739f794056SMaximilian Luz 	int status;
2749f794056SMaximilian Luz 	u8 raw;
2759f794056SMaximilian Luz 
2769f794056SMaximilian Luz 	status = ssam_retry(__ssam_kip_get_cover_state, sw->sdev->ctrl, &raw);
2779f794056SMaximilian Luz 	if (status < 0) {
2789f794056SMaximilian Luz 		dev_err(&sw->sdev->dev, "failed to query KIP lid state: %d\n", status);
2799f794056SMaximilian Luz 		return status;
2809f794056SMaximilian Luz 	}
2819f794056SMaximilian Luz 
282b58a444dSMaximilian Luz 	state->source = 0;	/* Unused for KIP switch. */
283b58a444dSMaximilian Luz 	state->state = raw;
2849f794056SMaximilian Luz 	return 0;
2859f794056SMaximilian Luz }
2869f794056SMaximilian Luz 
ssam_kip_sw_notif(struct ssam_event_notifier * nf,const struct ssam_event * event)2879f794056SMaximilian Luz static u32 ssam_kip_sw_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
2889f794056SMaximilian Luz {
2899f794056SMaximilian Luz 	struct ssam_tablet_sw *sw = container_of(nf, struct ssam_tablet_sw, notif);
2909f794056SMaximilian Luz 
2919f794056SMaximilian Luz 	if (event->command_id != SSAM_EVENT_KIP_CID_COVER_STATE_CHANGED)
2929f794056SMaximilian Luz 		return 0;	/* Return "unhandled". */
2939f794056SMaximilian Luz 
2949f794056SMaximilian Luz 	if (event->length < 1)
2959f794056SMaximilian Luz 		dev_warn(&sw->sdev->dev, "unexpected payload size: %u\n", event->length);
2969f794056SMaximilian Luz 
2979f794056SMaximilian Luz 	schedule_work(&sw->update_work);
2989f794056SMaximilian Luz 	return SSAM_NOTIF_HANDLED;
2999f794056SMaximilian Luz }
3009f794056SMaximilian Luz 
3019f794056SMaximilian Luz static const struct ssam_tablet_sw_desc ssam_kip_sw_desc = {
3029f794056SMaximilian Luz 	.dev = {
3039f794056SMaximilian Luz 		.name = "Microsoft Surface KIP Tablet Mode Switch",
3049f794056SMaximilian Luz 		.phys = "ssam/01:0e:01:00:01/input0",
3059f794056SMaximilian Luz 	},
3069f794056SMaximilian Luz 	.ops = {
3079f794056SMaximilian Luz 		.notify = ssam_kip_sw_notif,
3089f794056SMaximilian Luz 		.get_state = ssam_kip_get_cover_state,
3099f794056SMaximilian Luz 		.state_name = ssam_kip_cover_state_name,
3109f794056SMaximilian Luz 		.state_is_tablet_mode = ssam_kip_cover_state_is_tablet_mode,
3119f794056SMaximilian Luz 	},
3129f794056SMaximilian Luz 	.event = {
3139f794056SMaximilian Luz 		.reg = SSAM_EVENT_REGISTRY_SAM,
3149f794056SMaximilian Luz 		.id = {
3159f794056SMaximilian Luz 			.target_category = SSAM_SSH_TC_KIP,
3169f794056SMaximilian Luz 			.instance = 0,
3179f794056SMaximilian Luz 		},
3189f794056SMaximilian Luz 		.mask = SSAM_EVENT_MASK_TARGET,
3199f794056SMaximilian Luz 	},
3209f794056SMaximilian Luz };
3219f794056SMaximilian Luz 
3229f794056SMaximilian Luz 
3239f794056SMaximilian Luz /* -- SSAM POS tablet switch implementation. -------------------------------- */
3249f794056SMaximilian Luz 
3259f794056SMaximilian Luz static bool tablet_mode_in_slate_state = true;
3269f794056SMaximilian Luz module_param(tablet_mode_in_slate_state, bool, 0644);
3279f794056SMaximilian Luz MODULE_PARM_DESC(tablet_mode_in_slate_state, "Enable tablet mode in slate device posture, default is 'true'");
3289f794056SMaximilian Luz 
3299f794056SMaximilian Luz #define SSAM_EVENT_POS_CID_POSTURE_CHANGED	0x03
3309f794056SMaximilian Luz #define SSAM_POS_MAX_SOURCES			4
3319f794056SMaximilian Luz 
332b58a444dSMaximilian Luz enum ssam_pos_source_id {
33337ff64cdSMaximilian Luz 	SSAM_POS_SOURCE_COVER = 0x00,
334b58a444dSMaximilian Luz 	SSAM_POS_SOURCE_SLS   = 0x03,
335b58a444dSMaximilian Luz };
336b58a444dSMaximilian Luz 
33737ff64cdSMaximilian Luz enum ssam_pos_state_cover {
33837ff64cdSMaximilian Luz 	SSAM_POS_COVER_DISCONNECTED  = 0x01,
33937ff64cdSMaximilian Luz 	SSAM_POS_COVER_CLOSED        = 0x02,
34037ff64cdSMaximilian Luz 	SSAM_POS_COVER_LAPTOP        = 0x03,
34137ff64cdSMaximilian Luz 	SSAM_POS_COVER_FOLDED_CANVAS = 0x04,
34237ff64cdSMaximilian Luz 	SSAM_POS_COVER_FOLDED_BACK   = 0x05,
343*061c2289SMaximilian Luz 	SSAM_POS_COVER_BOOK          = 0x06,
34437ff64cdSMaximilian Luz };
34537ff64cdSMaximilian Luz 
346b58a444dSMaximilian Luz enum ssam_pos_state_sls {
347b58a444dSMaximilian Luz 	SSAM_POS_SLS_LID_CLOSED = 0x00,
348b58a444dSMaximilian Luz 	SSAM_POS_SLS_LAPTOP     = 0x01,
349b58a444dSMaximilian Luz 	SSAM_POS_SLS_SLATE      = 0x02,
350b58a444dSMaximilian Luz 	SSAM_POS_SLS_TABLET     = 0x03,
3519f794056SMaximilian Luz };
3529f794056SMaximilian Luz 
3539f794056SMaximilian Luz struct ssam_sources_list {
3549f794056SMaximilian Luz 	__le32 count;
3559f794056SMaximilian Luz 	__le32 id[SSAM_POS_MAX_SOURCES];
3569f794056SMaximilian Luz } __packed;
3579f794056SMaximilian Luz 
ssam_pos_state_name_cover(struct ssam_tablet_sw * sw,u32 state)35837ff64cdSMaximilian Luz static const char *ssam_pos_state_name_cover(struct ssam_tablet_sw *sw, u32 state)
35937ff64cdSMaximilian Luz {
36037ff64cdSMaximilian Luz 	switch (state) {
36137ff64cdSMaximilian Luz 	case SSAM_POS_COVER_DISCONNECTED:
36237ff64cdSMaximilian Luz 		return "disconnected";
36337ff64cdSMaximilian Luz 
36437ff64cdSMaximilian Luz 	case SSAM_POS_COVER_CLOSED:
36537ff64cdSMaximilian Luz 		return "closed";
36637ff64cdSMaximilian Luz 
36737ff64cdSMaximilian Luz 	case SSAM_POS_COVER_LAPTOP:
36837ff64cdSMaximilian Luz 		return "laptop";
36937ff64cdSMaximilian Luz 
37037ff64cdSMaximilian Luz 	case SSAM_POS_COVER_FOLDED_CANVAS:
37137ff64cdSMaximilian Luz 		return "folded-canvas";
37237ff64cdSMaximilian Luz 
37337ff64cdSMaximilian Luz 	case SSAM_POS_COVER_FOLDED_BACK:
37437ff64cdSMaximilian Luz 		return "folded-back";
37537ff64cdSMaximilian Luz 
376*061c2289SMaximilian Luz 	case SSAM_POS_COVER_BOOK:
377*061c2289SMaximilian Luz 		return "book";
378*061c2289SMaximilian Luz 
37937ff64cdSMaximilian Luz 	default:
38037ff64cdSMaximilian Luz 		dev_warn(&sw->sdev->dev, "unknown device posture for type-cover: %u\n", state);
38137ff64cdSMaximilian Luz 		return "<unknown>";
38237ff64cdSMaximilian Luz 	}
38337ff64cdSMaximilian Luz }
38437ff64cdSMaximilian Luz 
ssam_pos_state_name_sls(struct ssam_tablet_sw * sw,u32 state)385b58a444dSMaximilian Luz static const char *ssam_pos_state_name_sls(struct ssam_tablet_sw *sw, u32 state)
3869f794056SMaximilian Luz {
3879f794056SMaximilian Luz 	switch (state) {
388b58a444dSMaximilian Luz 	case SSAM_POS_SLS_LID_CLOSED:
3899f794056SMaximilian Luz 		return "closed";
3909f794056SMaximilian Luz 
391b58a444dSMaximilian Luz 	case SSAM_POS_SLS_LAPTOP:
3929f794056SMaximilian Luz 		return "laptop";
3939f794056SMaximilian Luz 
394b58a444dSMaximilian Luz 	case SSAM_POS_SLS_SLATE:
3959f794056SMaximilian Luz 		return "slate";
3969f794056SMaximilian Luz 
397b58a444dSMaximilian Luz 	case SSAM_POS_SLS_TABLET:
3989f794056SMaximilian Luz 		return "tablet";
3999f794056SMaximilian Luz 
4009f794056SMaximilian Luz 	default:
401b58a444dSMaximilian Luz 		dev_warn(&sw->sdev->dev, "unknown device posture for SLS: %u\n", state);
4029f794056SMaximilian Luz 		return "<unknown>";
4039f794056SMaximilian Luz 	}
4049f794056SMaximilian Luz }
4059f794056SMaximilian Luz 
ssam_pos_state_name(struct ssam_tablet_sw * sw,const struct ssam_tablet_sw_state * state)406b58a444dSMaximilian Luz static const char *ssam_pos_state_name(struct ssam_tablet_sw *sw,
407b58a444dSMaximilian Luz 				       const struct ssam_tablet_sw_state *state)
408b58a444dSMaximilian Luz {
409b58a444dSMaximilian Luz 	switch (state->source) {
41037ff64cdSMaximilian Luz 	case SSAM_POS_SOURCE_COVER:
41137ff64cdSMaximilian Luz 		return ssam_pos_state_name_cover(sw, state->state);
41237ff64cdSMaximilian Luz 
413b58a444dSMaximilian Luz 	case SSAM_POS_SOURCE_SLS:
414b58a444dSMaximilian Luz 		return ssam_pos_state_name_sls(sw, state->state);
415b58a444dSMaximilian Luz 
416b58a444dSMaximilian Luz 	default:
417b58a444dSMaximilian Luz 		dev_warn(&sw->sdev->dev, "unknown device posture source: %u\n", state->source);
418b58a444dSMaximilian Luz 		return "<unknown>";
419b58a444dSMaximilian Luz 	}
420b58a444dSMaximilian Luz }
421b58a444dSMaximilian Luz 
ssam_pos_state_is_tablet_mode_cover(struct ssam_tablet_sw * sw,u32 state)42237ff64cdSMaximilian Luz static bool ssam_pos_state_is_tablet_mode_cover(struct ssam_tablet_sw *sw, u32 state)
42337ff64cdSMaximilian Luz {
42437ff64cdSMaximilian Luz 	switch (state) {
42537ff64cdSMaximilian Luz 	case SSAM_POS_COVER_DISCONNECTED:
42637ff64cdSMaximilian Luz 	case SSAM_POS_COVER_FOLDED_CANVAS:
42737ff64cdSMaximilian Luz 	case SSAM_POS_COVER_FOLDED_BACK:
428*061c2289SMaximilian Luz 	case SSAM_POS_COVER_BOOK:
42937ff64cdSMaximilian Luz 		return true;
43037ff64cdSMaximilian Luz 
43137ff64cdSMaximilian Luz 	case SSAM_POS_COVER_CLOSED:
43237ff64cdSMaximilian Luz 	case SSAM_POS_COVER_LAPTOP:
43337ff64cdSMaximilian Luz 		return false;
43437ff64cdSMaximilian Luz 
43537ff64cdSMaximilian Luz 	default:
43637ff64cdSMaximilian Luz 		dev_warn(&sw->sdev->dev, "unknown device posture for type-cover: %u\n", state);
43737ff64cdSMaximilian Luz 		return true;
43837ff64cdSMaximilian Luz 	}
43937ff64cdSMaximilian Luz }
44037ff64cdSMaximilian Luz 
ssam_pos_state_is_tablet_mode_sls(struct ssam_tablet_sw * sw,u32 state)441b58a444dSMaximilian Luz static bool ssam_pos_state_is_tablet_mode_sls(struct ssam_tablet_sw *sw, u32 state)
4429f794056SMaximilian Luz {
4439f794056SMaximilian Luz 	switch (state) {
444b58a444dSMaximilian Luz 	case SSAM_POS_SLS_LAPTOP:
445b58a444dSMaximilian Luz 	case SSAM_POS_SLS_LID_CLOSED:
4469f794056SMaximilian Luz 		return false;
4479f794056SMaximilian Luz 
448b58a444dSMaximilian Luz 	case SSAM_POS_SLS_SLATE:
4499f794056SMaximilian Luz 		return tablet_mode_in_slate_state;
4509f794056SMaximilian Luz 
451b58a444dSMaximilian Luz 	case SSAM_POS_SLS_TABLET:
4529f794056SMaximilian Luz 		return true;
4539f794056SMaximilian Luz 
4549f794056SMaximilian Luz 	default:
455b58a444dSMaximilian Luz 		dev_warn(&sw->sdev->dev, "unknown device posture for SLS: %u\n", state);
456b58a444dSMaximilian Luz 		return true;
457b58a444dSMaximilian Luz 	}
458b58a444dSMaximilian Luz }
459b58a444dSMaximilian Luz 
ssam_pos_state_is_tablet_mode(struct ssam_tablet_sw * sw,const struct ssam_tablet_sw_state * state)460b58a444dSMaximilian Luz static bool ssam_pos_state_is_tablet_mode(struct ssam_tablet_sw *sw,
461b58a444dSMaximilian Luz 					  const struct ssam_tablet_sw_state *state)
462b58a444dSMaximilian Luz {
463b58a444dSMaximilian Luz 	switch (state->source) {
46437ff64cdSMaximilian Luz 	case SSAM_POS_SOURCE_COVER:
46537ff64cdSMaximilian Luz 		return ssam_pos_state_is_tablet_mode_cover(sw, state->state);
46637ff64cdSMaximilian Luz 
467b58a444dSMaximilian Luz 	case SSAM_POS_SOURCE_SLS:
468b58a444dSMaximilian Luz 		return ssam_pos_state_is_tablet_mode_sls(sw, state->state);
469b58a444dSMaximilian Luz 
470b58a444dSMaximilian Luz 	default:
471b58a444dSMaximilian Luz 		dev_warn(&sw->sdev->dev, "unknown device posture source: %u\n", state->source);
4729f794056SMaximilian Luz 		return true;
4739f794056SMaximilian Luz 	}
4749f794056SMaximilian Luz }
4759f794056SMaximilian Luz 
ssam_pos_get_sources_list(struct ssam_tablet_sw * sw,struct ssam_sources_list * sources)4769f794056SMaximilian Luz static int ssam_pos_get_sources_list(struct ssam_tablet_sw *sw, struct ssam_sources_list *sources)
4779f794056SMaximilian Luz {
4789f794056SMaximilian Luz 	struct ssam_request rqst;
4799f794056SMaximilian Luz 	struct ssam_response rsp;
4809f794056SMaximilian Luz 	int status;
4819f794056SMaximilian Luz 
4829f794056SMaximilian Luz 	rqst.target_category = SSAM_SSH_TC_POS;
48336f672a4SMaximilian Luz 	rqst.target_id = SSAM_SSH_TID_SAM;
4849f794056SMaximilian Luz 	rqst.command_id = 0x01;
4859f794056SMaximilian Luz 	rqst.instance_id = 0x00;
4869f794056SMaximilian Luz 	rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
4879f794056SMaximilian Luz 	rqst.length = 0;
4889f794056SMaximilian Luz 	rqst.payload = NULL;
4899f794056SMaximilian Luz 
4909f794056SMaximilian Luz 	rsp.capacity = sizeof(*sources);
4919f794056SMaximilian Luz 	rsp.length = 0;
4929f794056SMaximilian Luz 	rsp.pointer = (u8 *)sources;
4939f794056SMaximilian Luz 
494b09ee1cdSMaximilian Luz 	status = ssam_retry(ssam_request_do_sync_onstack, sw->sdev->ctrl, &rqst, &rsp, 0);
4959f794056SMaximilian Luz 	if (status)
4969f794056SMaximilian Luz 		return status;
4979f794056SMaximilian Luz 
4989f794056SMaximilian Luz 	/* We need at least the 'sources->count' field. */
4999f794056SMaximilian Luz 	if (rsp.length < sizeof(__le32)) {
5009f794056SMaximilian Luz 		dev_err(&sw->sdev->dev, "received source list response is too small\n");
5019f794056SMaximilian Luz 		return -EPROTO;
5029f794056SMaximilian Luz 	}
5039f794056SMaximilian Luz 
5049f794056SMaximilian Luz 	/* Make sure 'sources->count' matches with the response length. */
5059f794056SMaximilian Luz 	if (get_unaligned_le32(&sources->count) * sizeof(__le32) + sizeof(__le32) != rsp.length) {
5069f794056SMaximilian Luz 		dev_err(&sw->sdev->dev, "mismatch between number of sources and response size\n");
5079f794056SMaximilian Luz 		return -EPROTO;
5089f794056SMaximilian Luz 	}
5099f794056SMaximilian Luz 
5109f794056SMaximilian Luz 	return 0;
5119f794056SMaximilian Luz }
5129f794056SMaximilian Luz 
ssam_pos_get_source(struct ssam_tablet_sw * sw,u32 * source_id)5139f794056SMaximilian Luz static int ssam_pos_get_source(struct ssam_tablet_sw *sw, u32 *source_id)
5149f794056SMaximilian Luz {
5159f794056SMaximilian Luz 	struct ssam_sources_list sources = {};
5169f794056SMaximilian Luz 	int status;
5179f794056SMaximilian Luz 
5189f794056SMaximilian Luz 	status = ssam_pos_get_sources_list(sw, &sources);
5199f794056SMaximilian Luz 	if (status)
5209f794056SMaximilian Luz 		return status;
5219f794056SMaximilian Luz 
5227a4a04f4SMaximilian Luz 	if (get_unaligned_le32(&sources.count) == 0) {
5239f794056SMaximilian Luz 		dev_err(&sw->sdev->dev, "no posture sources found\n");
5249f794056SMaximilian Luz 		return -ENODEV;
5259f794056SMaximilian Luz 	}
5269f794056SMaximilian Luz 
5279f794056SMaximilian Luz 	/*
5289f794056SMaximilian Luz 	 * We currently don't know what to do with more than one posture
5299f794056SMaximilian Luz 	 * source. At the moment, only one source seems to be used/provided.
5309f794056SMaximilian Luz 	 * The WARN_ON() here should hopefully let us know quickly once there
5319f794056SMaximilian Luz 	 * is a device that provides multiple sources, at which point we can
5329f794056SMaximilian Luz 	 * then try to figure out how to handle them.
5339f794056SMaximilian Luz 	 */
5347a4a04f4SMaximilian Luz 	WARN_ON(get_unaligned_le32(&sources.count) > 1);
5359f794056SMaximilian Luz 
5369f794056SMaximilian Luz 	*source_id = get_unaligned_le32(&sources.id[0]);
5379f794056SMaximilian Luz 	return 0;
5389f794056SMaximilian Luz }
5399f794056SMaximilian Luz 
5409f794056SMaximilian Luz SSAM_DEFINE_SYNC_REQUEST_WR(__ssam_pos_get_posture_for_source, __le32, __le32, {
5419f794056SMaximilian Luz 	.target_category = SSAM_SSH_TC_POS,
54236f672a4SMaximilian Luz 	.target_id       = SSAM_SSH_TID_SAM,
5439f794056SMaximilian Luz 	.command_id      = 0x02,
5449f794056SMaximilian Luz 	.instance_id     = 0x00,
5459f794056SMaximilian Luz });
5469f794056SMaximilian Luz 
ssam_pos_get_posture_for_source(struct ssam_tablet_sw * sw,u32 source_id,u32 * posture)5479f794056SMaximilian Luz static int ssam_pos_get_posture_for_source(struct ssam_tablet_sw *sw, u32 source_id, u32 *posture)
5489f794056SMaximilian Luz {
5499f794056SMaximilian Luz 	__le32 source_le = cpu_to_le32(source_id);
5509f794056SMaximilian Luz 	__le32 rspval_le = 0;
5519f794056SMaximilian Luz 	int status;
5529f794056SMaximilian Luz 
5539f794056SMaximilian Luz 	status = ssam_retry(__ssam_pos_get_posture_for_source, sw->sdev->ctrl,
5549f794056SMaximilian Luz 			    &source_le, &rspval_le);
5559f794056SMaximilian Luz 	if (status)
5569f794056SMaximilian Luz 		return status;
5579f794056SMaximilian Luz 
5589f794056SMaximilian Luz 	*posture = le32_to_cpu(rspval_le);
5599f794056SMaximilian Luz 	return 0;
5609f794056SMaximilian Luz }
5619f794056SMaximilian Luz 
ssam_pos_get_posture(struct ssam_tablet_sw * sw,struct ssam_tablet_sw_state * state)562b58a444dSMaximilian Luz static int ssam_pos_get_posture(struct ssam_tablet_sw *sw, struct ssam_tablet_sw_state *state)
5639f794056SMaximilian Luz {
5649f794056SMaximilian Luz 	u32 source_id;
565b58a444dSMaximilian Luz 	u32 source_state;
5669f794056SMaximilian Luz 	int status;
5679f794056SMaximilian Luz 
5689f794056SMaximilian Luz 	status = ssam_pos_get_source(sw, &source_id);
5699f794056SMaximilian Luz 	if (status) {
5709f794056SMaximilian Luz 		dev_err(&sw->sdev->dev, "failed to get posture source ID: %d\n", status);
5719f794056SMaximilian Luz 		return status;
5729f794056SMaximilian Luz 	}
5739f794056SMaximilian Luz 
574b58a444dSMaximilian Luz 	status = ssam_pos_get_posture_for_source(sw, source_id, &source_state);
5759f794056SMaximilian Luz 	if (status) {
5769f794056SMaximilian Luz 		dev_err(&sw->sdev->dev, "failed to get posture value for source %u: %d\n",
5779f794056SMaximilian Luz 			source_id, status);
5789f794056SMaximilian Luz 		return status;
5799f794056SMaximilian Luz 	}
5809f794056SMaximilian Luz 
581b58a444dSMaximilian Luz 	state->source = source_id;
582b58a444dSMaximilian Luz 	state->state = source_state;
5839f794056SMaximilian Luz 	return 0;
5849f794056SMaximilian Luz }
5859f794056SMaximilian Luz 
ssam_pos_sw_notif(struct ssam_event_notifier * nf,const struct ssam_event * event)5869f794056SMaximilian Luz static u32 ssam_pos_sw_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
5879f794056SMaximilian Luz {
5889f794056SMaximilian Luz 	struct ssam_tablet_sw *sw = container_of(nf, struct ssam_tablet_sw, notif);
5899f794056SMaximilian Luz 
5909f794056SMaximilian Luz 	if (event->command_id != SSAM_EVENT_POS_CID_POSTURE_CHANGED)
5919f794056SMaximilian Luz 		return 0;	/* Return "unhandled". */
5929f794056SMaximilian Luz 
5939f794056SMaximilian Luz 	if (event->length != sizeof(__le32) * 3)
5949f794056SMaximilian Luz 		dev_warn(&sw->sdev->dev, "unexpected payload size: %u\n", event->length);
5959f794056SMaximilian Luz 
5969f794056SMaximilian Luz 	schedule_work(&sw->update_work);
5979f794056SMaximilian Luz 	return SSAM_NOTIF_HANDLED;
5989f794056SMaximilian Luz }
5999f794056SMaximilian Luz 
6009f794056SMaximilian Luz static const struct ssam_tablet_sw_desc ssam_pos_sw_desc = {
6019f794056SMaximilian Luz 	.dev = {
6029f794056SMaximilian Luz 		.name = "Microsoft Surface POS Tablet Mode Switch",
6039f794056SMaximilian Luz 		.phys = "ssam/01:26:01:00:01/input0",
6049f794056SMaximilian Luz 	},
6059f794056SMaximilian Luz 	.ops = {
6069f794056SMaximilian Luz 		.notify = ssam_pos_sw_notif,
6079f794056SMaximilian Luz 		.get_state = ssam_pos_get_posture,
6089f794056SMaximilian Luz 		.state_name = ssam_pos_state_name,
6099f794056SMaximilian Luz 		.state_is_tablet_mode = ssam_pos_state_is_tablet_mode,
6109f794056SMaximilian Luz 	},
6119f794056SMaximilian Luz 	.event = {
6129f794056SMaximilian Luz 		.reg = SSAM_EVENT_REGISTRY_SAM,
6139f794056SMaximilian Luz 		.id = {
6149f794056SMaximilian Luz 			.target_category = SSAM_SSH_TC_POS,
6159f794056SMaximilian Luz 			.instance = 0,
6169f794056SMaximilian Luz 		},
6179f794056SMaximilian Luz 		.mask = SSAM_EVENT_MASK_TARGET,
6189f794056SMaximilian Luz 	},
6199f794056SMaximilian Luz };
6209f794056SMaximilian Luz 
6219f794056SMaximilian Luz 
6229f794056SMaximilian Luz /* -- Driver registration. -------------------------------------------------- */
6239f794056SMaximilian Luz 
6249f794056SMaximilian Luz static const struct ssam_device_id ssam_tablet_sw_match[] = {
62578abf1b5SMaximilian Luz 	{ SSAM_SDEV(KIP, SAM, 0x00, 0x01), (unsigned long)&ssam_kip_sw_desc },
62678abf1b5SMaximilian Luz 	{ SSAM_SDEV(POS, SAM, 0x00, 0x01), (unsigned long)&ssam_pos_sw_desc },
6279f794056SMaximilian Luz 	{ },
6289f794056SMaximilian Luz };
6299f794056SMaximilian Luz MODULE_DEVICE_TABLE(ssam, ssam_tablet_sw_match);
6309f794056SMaximilian Luz 
6319f794056SMaximilian Luz static struct ssam_device_driver ssam_tablet_sw_driver = {
6329f794056SMaximilian Luz 	.probe = ssam_tablet_sw_probe,
6339f794056SMaximilian Luz 	.remove = ssam_tablet_sw_remove,
6349f794056SMaximilian Luz 	.match_table = ssam_tablet_sw_match,
6359f794056SMaximilian Luz 	.driver = {
6369f794056SMaximilian Luz 		.name = "surface_aggregator_tablet_mode_switch",
6379f794056SMaximilian Luz 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
6389f794056SMaximilian Luz 		.pm = &ssam_tablet_sw_pm_ops,
6399f794056SMaximilian Luz 	},
6409f794056SMaximilian Luz };
6419f794056SMaximilian Luz module_ssam_device_driver(ssam_tablet_sw_driver);
6429f794056SMaximilian Luz 
6439f794056SMaximilian Luz MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
6449f794056SMaximilian Luz MODULE_DESCRIPTION("Tablet mode switch driver for Surface devices using the Surface Aggregator Module");
6459f794056SMaximilian Luz MODULE_LICENSE("GPL");
646