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 void typec_altmode_attention(struct typec_altmode *adev, u32 vdo)
188 struct typec_altmode *pdev = &to_altmode(adev)->partner->adev;
190 if (pdev->ops && pdev->ops->attention)
191 pdev->ops->attention(pdev, vdo);
193 EXPORT_SYMBOL_GPL(typec_altmode_attention);
196 * typec_altmode_vdm - Send Vendor Defined Messages (VDM) to the partner
197 * @adev: Alternate mode handle
198 * @header: VDM Header
199 * @vdo: Array of Vendor Defined Data Objects
200 * @count: Number of Data Objects
202 * The alternate mode drivers use this function for SVID specific communication
203 * with the partner. The port drivers use it to deliver the Structured VDMs
204 * received from the partners to the alternate mode drivers.
206 int typec_altmode_vdm(struct typec_altmode *adev,
207 const u32 header, const u32 *vdo, int count)
209 struct typec_altmode *pdev;
210 struct altmode *altmode;
215 altmode = to_altmode(adev);
217 if (!altmode->partner)
220 pdev = &altmode->partner->adev;
222 if (!pdev->ops || !pdev->ops->vdm)
225 return pdev->ops->vdm(pdev, header, vdo, count);
227 EXPORT_SYMBOL_GPL(typec_altmode_vdm);
229 const struct typec_altmode *
230 typec_altmode_get_partner(struct typec_altmode *adev)
232 if (!adev || !to_altmode(adev)->partner)
235 return &to_altmode(adev)->partner->adev;
237 EXPORT_SYMBOL_GPL(typec_altmode_get_partner);
239 /* -------------------------------------------------------------------------- */
240 /* API for the alternate mode drivers */
243 * typec_altmode_get_plug - Find cable plug alternate mode
244 * @adev: Handle to partner alternate mode
245 * @index: Cable plug index
247 * Increment reference count for cable plug alternate mode device. Returns
248 * handle to the cable plug alternate mode, or NULL if none is found.
250 struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
251 enum typec_plug_index index)
253 struct altmode *port = to_altmode(adev)->partner;
255 if (port->plug[index]) {
256 get_device(&port->plug[index]->adev.dev);
257 return &port->plug[index]->adev;
262 EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
265 * typec_altmode_put_plug - Decrement cable plug alternate mode reference count
266 * @plug: Handle to the cable plug alternate mode
268 void typec_altmode_put_plug(struct typec_altmode *plug)
271 put_device(&plug->dev);
273 EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
275 int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
276 struct module *module)
281 drv->driver.owner = module;
282 drv->driver.bus = &typec_bus;
284 return driver_register(&drv->driver);
286 EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
288 void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
290 driver_unregister(&drv->driver);
292 EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
294 /* -------------------------------------------------------------------------- */
295 /* API for the port drivers */
298 * typec_match_altmode - Match SVID and mode to an array of alternate modes
299 * @altmodes: Array of alternate modes
300 * @n: Number of elements in the array, or -1 for NULL terminated arrays
301 * @svid: Standard or Vendor ID to match with
302 * @mode: Mode to match with
304 * Return pointer to an alternate mode with SVID matching @svid, or NULL when no
307 struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
308 size_t n, u16 svid, u8 mode)
312 for (i = 0; i < n; i++) {
315 if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
321 EXPORT_SYMBOL_GPL(typec_match_altmode);
323 /* -------------------------------------------------------------------------- */
326 description_show(struct device *dev, struct device_attribute *attr, char *buf)
328 struct typec_altmode *alt = to_typec_altmode(dev);
330 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
332 static DEVICE_ATTR_RO(description);
334 static struct attribute *typec_attrs[] = {
335 &dev_attr_description.attr,
338 ATTRIBUTE_GROUPS(typec);
340 static int typec_match(struct device *dev, struct device_driver *driver)
342 struct typec_altmode_driver *drv = to_altmode_driver(driver);
343 struct typec_altmode *altmode = to_typec_altmode(dev);
344 const struct typec_device_id *id;
346 for (id = drv->id_table; id->svid; id++)
347 if (id->svid == altmode->svid &&
348 (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
353 static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env)
355 const struct typec_altmode *altmode = to_typec_altmode(dev);
357 if (add_uevent_var(env, "SVID=%04X", altmode->svid))
360 if (add_uevent_var(env, "MODE=%u", altmode->mode))
363 return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
364 altmode->svid, altmode->mode);
367 static int typec_altmode_create_links(struct altmode *alt)
369 struct device *port_dev = &alt->partner->adev.dev;
370 struct device *dev = &alt->adev.dev;
373 err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
377 err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
379 sysfs_remove_link(&dev->kobj, "port");
384 static void typec_altmode_remove_links(struct altmode *alt)
386 sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
387 sysfs_remove_link(&alt->adev.dev.kobj, "port");
390 static int typec_probe(struct device *dev)
392 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
393 struct typec_altmode *adev = to_typec_altmode(dev);
394 struct altmode *altmode = to_altmode(adev);
397 /* Fail if the port does not support the alternate mode */
398 if (!altmode->partner)
401 ret = typec_altmode_create_links(altmode);
403 dev_warn(dev, "failed to create symlinks\n");
407 ret = drv->probe(adev);
409 typec_altmode_remove_links(altmode);
414 static void typec_remove(struct device *dev)
416 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
417 struct typec_altmode *adev = to_typec_altmode(dev);
418 struct altmode *altmode = to_altmode(adev);
420 typec_altmode_remove_links(altmode);
423 drv->remove(to_typec_altmode(dev));
426 WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL));
427 typec_altmode_update_active(adev, false);
434 const struct bus_type typec_bus = {
436 .dev_groups = typec_groups,
437 .match = typec_match,
438 .uevent = typec_uevent,
439 .probe = typec_probe,
440 .remove = typec_remove,