summaryrefslogtreecommitdiffstats
path: root/drivers/platform/chrome/cros_typec_switch.c
blob: a26219e97c931f6510417df43cafa9762ff06f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright 2022 Google LLC
 *
 * This driver provides the ability to configure Type-C muxes and retimers which are controlled by
 * the ChromeOS EC.
 */

#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
#include <linux/platform_device.h>
#include <linux/usb/typec_altmode.h>
#include <linux/usb/typec_dp.h>
#include <linux/usb/typec_mux.h>
#include <linux/usb/typec_retimer.h>

/* Handles and other relevant data required for each port's switches. */
struct cros_typec_port {
	int port_num;
	struct typec_mux_dev *mode_switch;
	struct typec_retimer *retimer;
	struct cros_typec_switch_data *sdata;
};

/* Driver-specific data. */
struct cros_typec_switch_data {
	struct device *dev;
	struct cros_ec_device *ec;
	struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
};

static int cros_typec_cmd_mux_set(struct cros_typec_switch_data *sdata, int port_num, u8 index,
				  u8 state)
{
	struct ec_params_typec_control req = {
		.port = port_num,
		.command = TYPEC_CONTROL_COMMAND_USB_MUX_SET,
		.mux_params = {
			.mux_index = index,
			.mux_flags = state,
		},
	};

	return cros_ec_cmd(sdata->ec, 0, EC_CMD_TYPEC_CONTROL, &req, sizeof(req), NULL, 0);
}

static int cros_typec_get_mux_state(unsigned long mode, struct typec_altmode *alt)
{
	int ret = -EOPNOTSUPP;

	if (mode == TYPEC_STATE_SAFE)
		ret = USB_PD_MUX_SAFE_MODE;
	else if (mode == TYPEC_STATE_USB)
		ret = USB_PD_MUX_USB_ENABLED;
	else if (alt && alt->svid == USB_TYPEC_DP_SID)
		ret = USB_PD_MUX_DP_ENABLED;

	return ret;
}

static int cros_typec_send_clear_event(struct cros_typec_switch_data *sdata, int port_num,
				       u32 events_mask)
{
	struct ec_params_typec_control req = {
		.port = port_num,
		.command = TYPEC_CONTROL_COMMAND_CLEAR_EVENTS,
		.clear_events_mask = events_mask,
	};

	return cros_ec_cmd(sdata->ec, 0, EC_CMD_TYPEC_CONTROL, &req, sizeof(req), NULL, 0);
}

static bool cros_typec_check_event(struct cros_typec_switch_data *sdata, int port_num, u32 mask)
{
	struct ec_response_typec_status resp;
	struct ec_params_typec_status req = {
		.port = port_num,
	};
	int ret;

	ret = cros_ec_cmd(sdata->ec, 0, EC_CMD_TYPEC_STATUS, &req, sizeof(req),
			  &resp, sizeof(resp));
	if (ret < 0) {
		dev_warn(sdata->dev, "EC_CMD_TYPEC_STATUS failed for port: %d\n", port_num);
		return false;
	}

	if (resp.events & mask)
		return true;

	return false;
}

/*
 * The ChromeOS EC treats both mode-switches and retimers as "muxes" for the purposes of the
 * host command API. This common function configures and verifies the retimer/mode-switch
 * according to the provided setting.
 */
static int cros_typec_configure_mux(struct cros_typec_switch_data *sdata, int port_num, int index,
				    unsigned long mode, struct typec_altmode *alt)
{
	unsigned long end;
	u32 event_mask;
	u8 mux_state;
	int ret;

	ret = cros_typec_get_mux_state(mode, alt);
	if (ret < 0)
		return ret;
	mux_state = (u8)ret;

	/* Clear any old mux set done event. */
	if (index == 0)
		event_mask = PD_STATUS_EVENT_MUX_0_SET_DONE;
	else
		event_mask = PD_STATUS_EVENT_MUX_1_SET_DONE;

	ret = cros_typec_send_clear_event(sdata, port_num, event_mask);
	if (ret < 0)
		return ret;

	/* Send the set command. */
	ret = cros_typec_cmd_mux_set(sdata, port_num, index, mux_state);
	if (ret < 0)
		return ret;

	/* Check for the mux set done event. */
	end = jiffies + msecs_to_jiffies(1000);
	do {
		if (cros_typec_check_event(sdata, port_num, event_mask))
			return 0;

		usleep_range(500, 1000);
	} while (time_before(jiffies, end));

	dev_err(sdata->dev, "Timed out waiting for mux set done on index: %d, state: %d\n",
		index, mux_state);

	return -ETIMEDOUT;
}

static int cros_typec_mode_switch_set(struct typec_mux_dev *mode_switch,
				      struct typec_mux_state *state)
{
	struct cros_typec_port *port = typec_mux_get_drvdata(mode_switch);

	/* Mode switches have index 0. */
	return cros_typec_configure_mux(port->sdata, port->port_num, 0, state->mode, state->alt);
}

static int cros_typec_retimer_set(struct typec_retimer *retimer, struct typec_retimer_state *state)
{
	struct cros_typec_port *port = typec_retimer_get_drvdata(retimer);

	/* Retimers have index 1. */
	return cros_typec_configure_mux(port->sdata, port->port_num, 1, state->mode, state->alt);
}

static void cros_typec_unregister_switches(struct cros_typec_switch_data *sdata)
{
	int i;

	for (i = 0; i < EC_USB_PD_MAX_PORTS; i++) {
		if (!sdata->ports[i])
			continue;
		typec_retimer_unregister(sdata->ports[i]->retimer);
		typec_mux_unregister(sdata->ports[i]->mode_switch);
	}
}

static int cros_typec_register_mode_switch(struct cros_typec_port *port,
					   struct fwnode_handle *fwnode)
{
	struct typec_mux_desc mode_switch_desc = {
		.fwnode = fwnode,
		.drvdata = port,
		.name = fwnode_get_name(fwnode),
		.set = cros_typec_mode_switch_set,
	};

	port->mode_switch = typec_mux_register(port->sdata->dev, &mode_switch_desc);

	return PTR_ERR_OR_ZERO(port->mode_switch);
}

static int cros_typec_register_retimer(struct cros_typec_port *port, struct fwnode_handle *fwnode)
{
	struct typec_retimer_desc retimer_desc = {
		.fwnode = fwnode,
		.drvdata = port,
		.name = fwnode_get_name(fwnode),
		.set = cros_typec_retimer_set,
	};

	port->retimer = typec_retimer_register(port->sdata->dev, &retimer_desc);

	return PTR_ERR_OR_ZERO(port->retimer);
}

static int cros_typec_register_switches(struct cros_typec_switch_data *sdata)
{
	struct cros_typec_port *port;
	struct device *dev = sdata->dev;
	struct fwnode_handle *fwnode;
	struct acpi_device *adev;
	unsigned long long index;
	int nports, ret;

	nports = device_get_child_node_count(dev);
	if (nports == 0) {
		dev_err(dev, "No switch devices found.\n");
		return -ENODEV;
	}

	device_for_each_child_node(dev, fwnode) {
		port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
		if (!port) {
			ret = -ENOMEM;
			goto err_switch;
		}

		adev = to_acpi_device_node(fwnode);
		if (!adev) {
			dev_err(fwnode->dev, "Couldn't get ACPI device handle\n");
			ret = -ENODEV;
			goto err_switch;
		}

		ret = acpi_evaluate_integer(adev->handle, "_ADR", NULL, &index);
		if (ACPI_FAILURE(ret)) {
			dev_err(fwnode->dev, "_ADR wasn't evaluated\n");
			ret = -ENODATA;
			goto err_switch;
		}

		if (index >= EC_USB_PD_MAX_PORTS) {
			dev_err(fwnode->dev, "Invalid port index number: %llu\n", index);
			ret = -EINVAL;
			goto err_switch;
		}
		port->sdata = sdata;
		port->port_num = index;
		sdata->ports[index] = port;

		ret = cros_typec_register_retimer(port, fwnode);
		if (ret) {
			dev_err(dev, "Retimer switch register failed\n");
			goto err_switch;
		}

		dev_dbg(dev, "Retimer switch registered for index %llu\n", index);

		if (!device_property_present(fwnode->dev, "mode-switch"))
			continue;

		ret = cros_typec_register_mode_switch(port, fwnode);
		if (ret) {
			dev_err(dev, "Mode switch register failed\n");
			goto err_switch;
		}

		dev_dbg(dev, "Mode switch registered for index %llu\n", index);
	}

	return 0;
err_switch:
	cros_typec_unregister_switches(sdata);
	return ret;
}

static int cros_typec_switch_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct cros_typec_switch_data *sdata;

	sdata = devm_kzalloc(dev, sizeof(*sdata), GFP_KERNEL);
	if (!sdata)
		return -ENOMEM;

	sdata->dev = dev;
	sdata->ec = dev_get_drvdata(pdev->dev.parent);

	platform_set_drvdata(pdev, sdata);

	return cros_typec_register_switches(sdata);
}

static int cros_typec_switch_remove(struct platform_device *pdev)
{
	struct cros_typec_switch_data *sdata = platform_get_drvdata(pdev);

	cros_typec_unregister_switches(sdata);
	return 0;
}

#ifdef CONFIG_ACPI
static const struct acpi_device_id cros_typec_switch_acpi_id[] = {
	{ "GOOG001A", 0 },
	{}
};
MODULE_DEVICE_TABLE(acpi, cros_typec_switch_acpi_id);
#endif

static struct platform_driver cros_typec_switch_driver = {
	.driver	= {
		.name = "cros-typec-switch",
		.acpi_match_table = ACPI_PTR(cros_typec_switch_acpi_id),
	},
	.probe = cros_typec_switch_probe,
	.remove = cros_typec_switch_remove,
};

module_platform_driver(cros_typec_switch_driver);

MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
MODULE_DESCRIPTION("ChromeOS EC Type-C Switch control");
MODULE_LICENSE("GPL");