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 the alternate mode drivers */
251 * typec_altmode_get_plug - Find cable plug alternate mode
252 * @adev: Handle to partner alternate mode
253 * @index: Cable plug index
255 * Increment reference count for cable plug alternate mode device. Returns
256 * handle to the cable plug alternate mode, or NULL if none is found.
258 struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
259 enum typec_plug_index index)
261 struct altmode *port = to_altmode(adev)->partner;
263 if (port->plug[index]) {
264 get_device(&port->plug[index]->adev.dev);
265 return &port->plug[index]->adev;
270 EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
273 * typec_altmode_put_plug - Decrement cable plug alternate mode reference count
274 * @plug: Handle to the cable plug alternate mode
276 void typec_altmode_put_plug(struct typec_altmode *plug)
279 put_device(&plug->dev);
281 EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
283 int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
284 struct module *module)
289 drv->driver.owner = module;
290 drv->driver.bus = &typec_bus;
292 return driver_register(&drv->driver);
294 EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
296 void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
298 driver_unregister(&drv->driver);
300 EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
302 /* -------------------------------------------------------------------------- */
303 /* API for the port drivers */
306 * typec_match_altmode - Match SVID and mode to an array of alternate modes
307 * @altmodes: Array of alternate modes
308 * @n: Number of elements in the array, or -1 for NULL terminated arrays
309 * @svid: Standard or Vendor ID to match with
310 * @mode: Mode to match with
312 * Return pointer to an alternate mode with SVID matching @svid, or NULL when no
315 struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
316 size_t n, u16 svid, u8 mode)
320 for (i = 0; i < n; i++) {
323 if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
329 EXPORT_SYMBOL_GPL(typec_match_altmode);
331 /* -------------------------------------------------------------------------- */
334 description_show(struct device *dev, struct device_attribute *attr, char *buf)
336 struct typec_altmode *alt = to_typec_altmode(dev);
338 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
340 static DEVICE_ATTR_RO(description);
342 static struct attribute *typec_attrs[] = {
343 &dev_attr_description.attr,
346 ATTRIBUTE_GROUPS(typec);
348 static int typec_match(struct device *dev, struct device_driver *driver)
350 struct typec_altmode_driver *drv = to_altmode_driver(driver);
351 struct typec_altmode *altmode = to_typec_altmode(dev);
352 const struct typec_device_id *id;
354 for (id = drv->id_table; id->svid; id++)
355 if (id->svid == altmode->svid &&
356 (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
361 static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env)
363 const struct typec_altmode *altmode = to_typec_altmode(dev);
365 if (add_uevent_var(env, "SVID=%04X", altmode->svid))
368 if (add_uevent_var(env, "MODE=%u", altmode->mode))
371 return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
372 altmode->svid, altmode->mode);
375 static int typec_altmode_create_links(struct altmode *alt)
377 struct device *port_dev = &alt->partner->adev.dev;
378 struct device *dev = &alt->adev.dev;
381 err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
385 err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
387 sysfs_remove_link(&dev->kobj, "port");
392 static void typec_altmode_remove_links(struct altmode *alt)
394 sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
395 sysfs_remove_link(&alt->adev.dev.kobj, "port");
398 static int typec_probe(struct device *dev)
400 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
401 struct typec_altmode *adev = to_typec_altmode(dev);
402 struct altmode *altmode = to_altmode(adev);
405 /* Fail if the port does not support the alternate mode */
406 if (!altmode->partner)
409 ret = typec_altmode_create_links(altmode);
411 dev_warn(dev, "failed to create symlinks\n");
415 ret = drv->probe(adev);
417 typec_altmode_remove_links(altmode);
422 static void typec_remove(struct device *dev)
424 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
425 struct typec_altmode *adev = to_typec_altmode(dev);
426 struct altmode *altmode = to_altmode(adev);
428 typec_altmode_remove_links(altmode);
431 drv->remove(to_typec_altmode(dev));
434 WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL));
435 typec_altmode_update_active(adev, false);
442 const struct bus_type typec_bus = {
444 .dev_groups = typec_groups,
445 .match = typec_match,
446 .uevent = typec_uevent,
447 .probe = typec_probe,
448 .remove = typec_remove,