Lines Matching +full:data +full:- +full:role

1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018-2019 Cadence.
6 * Copyright (C) 2017-2018 NXP
14 #include <linux/dma-mapping.h>
24 #include "host-export.h"
29 static int cdns_role_start(struct cdns *cdns, enum usb_role role) in cdns_role_start() argument
33 if (WARN_ON(role > USB_ROLE_DEVICE)) in cdns_role_start()
36 mutex_lock(&cdns->mutex); in cdns_role_start()
37 cdns->role = role; in cdns_role_start()
38 mutex_unlock(&cdns->mutex); in cdns_role_start()
40 if (!cdns->roles[role]) in cdns_role_start()
41 return -ENXIO; in cdns_role_start()
43 if (cdns->roles[role]->state == CDNS_ROLE_STATE_ACTIVE) in cdns_role_start()
46 mutex_lock(&cdns->mutex); in cdns_role_start()
47 ret = cdns->roles[role]->start(cdns); in cdns_role_start()
49 cdns->roles[role]->state = CDNS_ROLE_STATE_ACTIVE; in cdns_role_start()
50 mutex_unlock(&cdns->mutex); in cdns_role_start()
57 enum usb_role role = cdns->role; in cdns_role_stop() local
59 if (WARN_ON(role > USB_ROLE_DEVICE)) in cdns_role_stop()
62 if (cdns->roles[role]->state == CDNS_ROLE_STATE_INACTIVE) in cdns_role_stop()
65 mutex_lock(&cdns->mutex); in cdns_role_stop()
66 cdns->roles[role]->stop(cdns); in cdns_role_stop()
67 cdns->roles[role]->state = CDNS_ROLE_STATE_INACTIVE; in cdns_role_stop()
68 mutex_unlock(&cdns->mutex); in cdns_role_stop()
78 * cdns_core_init_role - initialize role of operation
85 struct device *dev = cdns->dev; in cdns_core_init_role()
91 cdns->role = USB_ROLE_NONE; in cdns_core_init_role()
99 if (cdns->version == CDNSP_CONTROLLER_V2) { in cdns_core_init_role()
119 * At this point cdns->dr_mode contains strap configuration. in cdns_core_init_role()
122 best_dr_mode = cdns->dr_mode; in cdns_core_init_role()
129 best_dr_mode = cdns->dr_mode; in cdns_core_init_role()
130 } else if (cdns->dr_mode == USB_DR_MODE_OTG) { in cdns_core_init_role()
132 } else if (cdns->dr_mode != dr_mode) { in cdns_core_init_role()
134 return -EINVAL; in cdns_core_init_role()
140 if ((cdns->version == CDNSP_CONTROLLER_V2 && in cdns_core_init_role()
142 (cdns->version < CDNSP_CONTROLLER_V2 && in cdns_core_init_role()
146 ret = -ENXIO; in cdns_core_init_role()
156 if (cdns->gadget_init) in cdns_core_init_role()
157 ret = cdns->gadget_init(cdns); in cdns_core_init_role()
159 ret = -ENXIO; in cdns_core_init_role()
168 cdns->dr_mode = dr_mode; in cdns_core_init_role()
174 /* Initialize idle role to start with */ in cdns_core_init_role()
179 switch (cdns->dr_mode) { in cdns_core_init_role()
196 ret = -EINVAL; in cdns_core_init_role()
207 * cdns_hw_role_state_machine - role switch state machine based on hw events.
210 * Returns next role to be entered based on hw events.
214 enum usb_role role = USB_ROLE_NONE; in cdns_hw_role_state_machine() local
217 if (cdns->dr_mode != USB_DR_MODE_OTG) { in cdns_hw_role_state_machine()
219 role = USB_ROLE_HOST; in cdns_hw_role_state_machine()
221 role = USB_ROLE_DEVICE; in cdns_hw_role_state_machine()
223 return role; in cdns_hw_role_state_machine()
230 * Role change state machine in cdns_hw_role_state_machine()
232 * Previous state: cdns->role in cdns_hw_role_state_machine()
233 * Next state: role in cdns_hw_role_state_machine()
235 role = cdns->role; in cdns_hw_role_state_machine()
237 switch (role) { in cdns_hw_role_state_machine()
244 role = USB_ROLE_HOST; in cdns_hw_role_state_machine()
246 role = USB_ROLE_DEVICE; in cdns_hw_role_state_machine()
250 role = USB_ROLE_NONE; in cdns_hw_role_state_machine()
254 role = USB_ROLE_NONE; in cdns_hw_role_state_machine()
258 dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role); in cdns_hw_role_state_machine()
260 return role; in cdns_hw_role_state_machine()
271 phy_reset(cdns->usb3_phy); in cdns_idle_role_stop()
278 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL); in cdns_idle_init()
280 return -ENOMEM; in cdns_idle_init()
282 rdrv->start = cdns_idle_role_start; in cdns_idle_init()
283 rdrv->stop = cdns_idle_role_stop; in cdns_idle_init()
284 rdrv->state = CDNS_ROLE_STATE_INACTIVE; in cdns_idle_init()
285 rdrv->suspend = NULL; in cdns_idle_init()
286 rdrv->resume = NULL; in cdns_idle_init()
287 rdrv->name = "idle"; in cdns_idle_init()
289 cdns->roles[USB_ROLE_NONE] = rdrv; in cdns_idle_init()
295 * cdns_hw_role_switch - switch roles based on HW state
303 /* Depends on role switch class */ in cdns_hw_role_switch()
304 if (cdns->role_sw) in cdns_hw_role_switch()
307 pm_runtime_get_sync(cdns->dev); in cdns_hw_role_switch()
309 current_role = cdns->role; in cdns_hw_role_switch()
318 dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role); in cdns_hw_role_switch()
322 /* Back to current role */ in cdns_hw_role_switch()
323 dev_err(cdns->dev, "set %d has failed, back to %d\n", in cdns_hw_role_switch()
327 dev_err(cdns->dev, "back to %d failed too\n", in cdns_hw_role_switch()
331 pm_runtime_put_sync(cdns->dev); in cdns_hw_role_switch()
336 * cdns_role_get - get current role of controller.
338 * @sw: pointer to USB role switch structure
340 * Returns role
346 return cdns->role; in cdns_role_get()
350 * cdns_role_set - set current role of controller.
352 * @sw: pointer to USB role switch structure
353 * @role: the previous role
355 * - Role switch for dual-role devices
356 * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices
358 static int cdns_role_set(struct usb_role_switch *sw, enum usb_role role) in cdns_role_set() argument
363 pm_runtime_get_sync(cdns->dev); in cdns_role_set()
365 if (cdns->role == role) in cdns_role_set()
368 if (cdns->dr_mode == USB_DR_MODE_HOST) { in cdns_role_set()
369 switch (role) { in cdns_role_set()
378 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) { in cdns_role_set()
379 switch (role) { in cdns_role_set()
389 ret = cdns_role_start(cdns, role); in cdns_role_set()
391 dev_err(cdns->dev, "set role %d has failed\n", role); in cdns_role_set()
394 pm_runtime_put_sync(cdns->dev); in cdns_role_set()
399 * cdns_wakeup_irq - interrupt handler for wakeup events
401 * @data: structure of cdns
405 static irqreturn_t cdns_wakeup_irq(int irq, void *data) in cdns_wakeup_irq() argument
407 struct cdns *cdns = data; in cdns_wakeup_irq()
409 if (cdns->in_lpm) { in cdns_wakeup_irq()
411 cdns->wakeup_pending = true; in cdns_wakeup_irq()
412 if ((cdns->role == USB_ROLE_HOST) && cdns->host_dev) in cdns_wakeup_irq()
413 pm_request_resume(&cdns->host_dev->dev); in cdns_wakeup_irq()
422 * cdns_init - probe for cdns3/cdnsp core device
429 struct device *dev = cdns->dev; in cdns_init()
438 mutex_init(&cdns->mutex); in cdns_init()
440 if (device_property_read_bool(dev, "usb-role-switch")) { in cdns_init()
447 sw_desc.fwnode = dev->fwnode; in cdns_init()
449 cdns->role_sw = usb_role_switch_register(dev, &sw_desc); in cdns_init()
450 if (IS_ERR(cdns->role_sw)) { in cdns_init()
451 dev_warn(dev, "Unable to register Role Switch\n"); in cdns_init()
452 return PTR_ERR(cdns->role_sw); in cdns_init()
456 if (cdns->wakeup_irq) { in cdns_init()
457 ret = devm_request_irq(cdns->dev, cdns->wakeup_irq, in cdns_init()
460 dev_name(cdns->dev), cdns); in cdns_init()
463 dev_err(cdns->dev, "couldn't register wakeup irq handler\n"); in cdns_init()
476 spin_lock_init(&cdns->lock); in cdns_init()
484 if (cdns->role_sw) in cdns_init()
485 usb_role_switch_unregister(cdns->role_sw); in cdns_init()
492 * cdns_remove - unbind drd driver and clean up
500 usb_role_switch_unregister(cdns->role_sw); in cdns_remove()
509 struct device *dev = cdns->dev; in cdns_suspend()
515 if (cdns->roles[cdns->role]->suspend) { in cdns_suspend()
516 spin_lock_irqsave(&cdns->lock, flags); in cdns_suspend()
517 cdns->roles[cdns->role]->suspend(cdns, false); in cdns_suspend()
518 spin_unlock_irqrestore(&cdns->lock, flags); in cdns_suspend()
532 if (cdns->role_sw) { in cdns_resume()
533 cdns->role = cdns_role_get(cdns->role_sw); in cdns_resume()
536 if (real_role != cdns->role) { in cdns_resume()
545 if (cdns->role == USB_ROLE_HOST) in cdns_resume()
547 else if (cdns->role == USB_ROLE_DEVICE) in cdns_resume()
555 if (cdns->roles[cdns->role]->resume) in cdns_resume()
556 cdns->roles[cdns->role]->resume(cdns, cdns_power_is_lost(cdns)); in cdns_resume()
564 struct device *dev = cdns->dev; in cdns_set_active()