1 // SPDX-License-Identifier: GPL-2.0
3 * Bus for USB Type-C Alternate Modes
5 * Copyright (C) 2018 Intel Corporation
9 #include <linux/usb/pd_vdo.h>
17 typec_altmode_set_retimer(struct altmode *alt, unsigned long conf, void *data)
19 struct typec_retimer_state state;
24 state.alt = &alt->adev;
28 return typec_retimer_set(alt->retimer, &state);
32 typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
34 struct typec_mux_state state;
39 state.alt = &alt->adev;
43 return typec_mux_set(alt->mux, &state);
46 /* Wrapper to set various Type-C port switches together. */
48 typec_altmode_set_switches(struct altmode *alt, unsigned long conf, void *data)
52 ret = typec_altmode_set_retimer(alt, conf, data);
56 return typec_altmode_set_mux(alt, conf, data);
59 static int typec_altmode_set_state(struct typec_altmode *adev,
60 unsigned long conf, void *data)
62 bool is_port = is_typec_port(adev->dev.parent);
63 struct altmode *port_altmode;
65 port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;
67 return typec_altmode_set_switches(port_altmode, conf, data);
70 /* -------------------------------------------------------------------------- */
74 * typec_altmode_notify - Communication between the OS and alternate mode driver
75 * @adev: Handle to the alternate mode
76 * @conf: Alternate mode specific configuration value
77 * @data: Alternate mode specific data
79 * The primary purpose for this function is to allow the alternate mode drivers
80 * to tell which pin configuration has been negotiated with the partner. That
81 * information will then be used for example to configure the muxes.
82 * Communication to the other direction is also possible, and low level device
83 * drivers can also send notifications to the alternate mode drivers. The actual
84 * communication will be specific for every SVID.
86 int typec_altmode_notify(struct typec_altmode *adev,
87 unsigned long conf, void *data)
90 struct altmode *altmode;
91 struct altmode *partner;
97 altmode = to_altmode(adev);
99 if (!altmode->partner)
102 is_port = is_typec_port(adev->dev.parent);
103 partner = altmode->partner;
105 ret = typec_altmode_set_switches(is_port ? altmode : partner, conf, data);
109 if (partner->adev.ops && partner->adev.ops->notify)
110 return partner->adev.ops->notify(&partner->adev, conf, data);
114 EXPORT_SYMBOL_GPL(typec_altmode_notify);
117 * typec_altmode_enter - Enter Mode
118 * @adev: The alternate mode
119 * @vdo: VDO for the Enter Mode command
121 * The alternate mode drivers use this function to enter mode. The port drivers
122 * use this to inform the alternate mode drivers that the partner has initiated
123 * Enter Mode command. If the alternate mode does not require VDO, @vdo must be
126 int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)
128 struct altmode *partner = to_altmode(adev)->partner;
129 struct typec_altmode *pdev = &partner->adev;
132 if (!adev || adev->active)
135 if (!pdev->ops || !pdev->ops->enter)
138 if (is_typec_port(pdev->dev.parent) && !pdev->active)
141 /* Moving to USB Safe State */
142 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL);
147 return pdev->ops->enter(pdev, vdo);
149 EXPORT_SYMBOL_GPL(typec_altmode_enter);
152 * typec_altmode_exit - Exit Mode
153 * @adev: The alternate mode
155 * The partner of @adev has initiated Exit Mode command.
157 int typec_altmode_exit(struct typec_altmode *adev)
159 struct altmode *partner = to_altmode(adev)->partner;
160 struct typec_altmode *pdev = &partner->adev;
163 if (!adev || !adev->active)
166 if (!pdev->ops || !pdev->ops->exit)
169 /* Moving to USB Safe State */
170 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL);
174 /* Exit Mode command */
175 return pdev->ops->exit(pdev);
177 EXPORT_SYMBOL_GPL(typec_altmode_exit);
180 * typec_altmode_attention - Attention command
181 * @adev: The alternate mode
182 * @vdo: VDO for the Attention command
184 * Notifies the partner of @adev about Attention command.
186 int typec_altmode_attention(struct typec_altmode *adev, u32 vdo)
188 struct altmode *partner = to_altmode(adev)->partner;
189 struct typec_altmode *pdev;
194 pdev = &partner->adev;
196 if (pdev->ops && pdev->ops->attention)
197 pdev->ops->attention(pdev, vdo);
201 EXPORT_SYMBOL_GPL(typec_altmode_attention);
204 * typec_altmode_vdm - Send Vendor Defined Messages (VDM) to the partner
205 * @adev: Alternate mode handle
206 * @header: VDM Header
207 * @vdo: Array of Vendor Defined Data Objects
208 * @count: Number of Data Objects
210 * The alternate mode drivers use this function for SVID specific communication
211 * with the partner. The port drivers use it to deliver the Structured VDMs
212 * received from the partners to the alternate mode drivers.
214 int typec_altmode_vdm(struct typec_altmode *adev,
215 const u32 header, const u32 *vdo, int count)
217 struct typec_altmode *pdev;
218 struct altmode *altmode;
223 altmode = to_altmode(adev);
225 if (!altmode->partner)
228 pdev = &altmode->partner->adev;
230 if (!pdev->ops || !pdev->ops->vdm)
233 return pdev->ops->vdm(pdev, header, vdo, count);
235 EXPORT_SYMBOL_GPL(typec_altmode_vdm);
237 const struct typec_altmode *
238 typec_altmode_get_partner(struct typec_altmode *adev)
240 if (!adev || !to_altmode(adev)->partner)
243 return &to_altmode(adev)->partner->adev;
245 EXPORT_SYMBOL_GPL(typec_altmode_get_partner);
247 /* -------------------------------------------------------------------------- */
248 /* API for cable alternate modes */
251 * typec_cable_altmode_enter - Enter Mode
252 * @adev: The alternate mode
253 * @sop: Cable plug target for Enter Mode command
254 * @vdo: VDO for the Enter Mode command
256 * Alternate mode drivers use this function to enter mode on the cable plug.
257 * If the alternate mode does not require VDO, @vdo must be NULL.
259 int typec_cable_altmode_enter(struct typec_altmode *adev, enum typec_plug_index sop, u32 *vdo)
261 struct altmode *partner = to_altmode(adev)->partner;
262 struct typec_altmode *pdev;
264 if (!adev || adev->active)
270 pdev = &partner->adev;
275 if (!pdev->cable_ops || !pdev->cable_ops->enter)
278 return pdev->cable_ops->enter(pdev, sop, vdo);
280 EXPORT_SYMBOL_GPL(typec_cable_altmode_enter);
283 * typec_cable_altmode_exit - Exit Mode
284 * @adev: The alternate mode
285 * @sop: Cable plug target for Exit Mode command
287 * The alternate mode drivers use this function to exit mode on the cable plug.
289 int typec_cable_altmode_exit(struct typec_altmode *adev, enum typec_plug_index sop)
291 struct altmode *partner = to_altmode(adev)->partner;
292 struct typec_altmode *pdev;
294 if (!adev || !adev->active)
300 pdev = &partner->adev;
302 if (!pdev->cable_ops || !pdev->cable_ops->exit)
305 return pdev->cable_ops->exit(pdev, sop);
307 EXPORT_SYMBOL_GPL(typec_cable_altmode_exit);
310 * typec_cable_altmode_vdm - Send Vendor Defined Messages (VDM) between the cable plug and port.
311 * @adev: Alternate mode handle
312 * @sop: Cable plug target for VDM
313 * @header: VDM Header
314 * @vdo: Array of Vendor Defined Data Objects
315 * @count: Number of Data Objects
317 * The alternate mode drivers use this function for SVID specific communication
318 * with the cable plugs. The port drivers use it to deliver the Structured VDMs
319 * received from the cable plugs to the alternate mode drivers.
321 int typec_cable_altmode_vdm(struct typec_altmode *adev, enum typec_plug_index sop,
322 const u32 header, const u32 *vdo, int count)
324 struct altmode *altmode;
325 struct typec_altmode *pdev;
330 altmode = to_altmode(adev);
332 if (is_typec_plug(adev->dev.parent)) {
333 if (!altmode->partner)
335 pdev = &altmode->partner->adev;
337 if (!altmode->plug[sop])
339 pdev = &altmode->plug[sop]->adev;
342 if (!pdev->cable_ops || !pdev->cable_ops->vdm)
345 return pdev->cable_ops->vdm(pdev, sop, header, vdo, count);
347 EXPORT_SYMBOL_GPL(typec_cable_altmode_vdm);
349 /* -------------------------------------------------------------------------- */
350 /* API for the alternate mode drivers */
353 * typec_altmode_get_plug - Find cable plug alternate mode
354 * @adev: Handle to partner alternate mode
355 * @index: Cable plug index
357 * Increment reference count for cable plug alternate mode device. Returns
358 * handle to the cable plug alternate mode, or NULL if none is found.
360 struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
361 enum typec_plug_index index)
363 struct altmode *port = to_altmode(adev)->partner;
365 if (port->plug[index]) {
366 get_device(&port->plug[index]->adev.dev);
367 return &port->plug[index]->adev;
372 EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
375 * typec_altmode_put_plug - Decrement cable plug alternate mode reference count
376 * @plug: Handle to the cable plug alternate mode
378 void typec_altmode_put_plug(struct typec_altmode *plug)
381 put_device(&plug->dev);
383 EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
385 int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
386 struct module *module)
391 drv->driver.owner = module;
392 drv->driver.bus = &typec_bus;
394 return driver_register(&drv->driver);
396 EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
398 void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
400 driver_unregister(&drv->driver);
402 EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
404 /* -------------------------------------------------------------------------- */
405 /* API for the port drivers */
408 * typec_match_altmode - Match SVID and mode to an array of alternate modes
409 * @altmodes: Array of alternate modes
410 * @n: Number of elements in the array, or -1 for NULL terminated arrays
411 * @svid: Standard or Vendor ID to match with
412 * @mode: Mode to match with
414 * Return pointer to an alternate mode with SVID matching @svid, or NULL when no
417 struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
418 size_t n, u16 svid, u8 mode)
422 for (i = 0; i < n; i++) {
425 if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
431 EXPORT_SYMBOL_GPL(typec_match_altmode);
433 /* -------------------------------------------------------------------------- */
436 description_show(struct device *dev, struct device_attribute *attr, char *buf)
438 struct typec_altmode *alt = to_typec_altmode(dev);
440 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
442 static DEVICE_ATTR_RO(description);
444 static struct attribute *typec_attrs[] = {
445 &dev_attr_description.attr,
448 ATTRIBUTE_GROUPS(typec);
450 static int typec_match(struct device *dev, const struct device_driver *driver)
452 struct typec_altmode_driver *drv = to_altmode_driver(driver);
453 struct typec_altmode *altmode = to_typec_altmode(dev);
454 const struct typec_device_id *id;
456 for (id = drv->id_table; id->svid; id++)
457 if (id->svid == altmode->svid &&
458 (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
463 static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env)
465 const struct typec_altmode *altmode = to_typec_altmode(dev);
467 if (add_uevent_var(env, "SVID=%04X", altmode->svid))
470 if (add_uevent_var(env, "MODE=%u", altmode->mode))
473 return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
474 altmode->svid, altmode->mode);
477 static int typec_altmode_create_links(struct altmode *alt)
479 struct device *port_dev = &alt->partner->adev.dev;
480 struct device *dev = &alt->adev.dev;
483 err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
487 err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
489 sysfs_remove_link(&dev->kobj, "port");
494 static void typec_altmode_remove_links(struct altmode *alt)
496 sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
497 sysfs_remove_link(&alt->adev.dev.kobj, "port");
500 static int typec_probe(struct device *dev)
502 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
503 struct typec_altmode *adev = to_typec_altmode(dev);
504 struct altmode *altmode = to_altmode(adev);
507 /* Fail if the port does not support the alternate mode */
508 if (!altmode->partner)
511 ret = typec_altmode_create_links(altmode);
513 dev_warn(dev, "failed to create symlinks\n");
517 ret = drv->probe(adev);
519 typec_altmode_remove_links(altmode);
524 static void typec_remove(struct device *dev)
526 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
527 struct typec_altmode *adev = to_typec_altmode(dev);
528 struct altmode *altmode = to_altmode(adev);
530 typec_altmode_remove_links(altmode);
533 drv->remove(to_typec_altmode(dev));
536 WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL));
537 typec_altmode_update_active(adev, false);
544 const struct bus_type typec_bus = {
546 .dev_groups = typec_groups,
547 .match = typec_match,
548 .uevent = typec_uevent,
549 .probe = typec_probe,
550 .remove = typec_remove,