162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * gpio-vbus.c - simple GPIO VBUS sensing driver for B peripheral devices
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2008 Philipp Zabel <philipp.zabel@gmail.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/kernel.h>
962306a36Sopenharmony_ci#include <linux/platform_device.h>
1062306a36Sopenharmony_ci#include <linux/gpio/consumer.h>
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/slab.h>
1362306a36Sopenharmony_ci#include <linux/interrupt.h>
1462306a36Sopenharmony_ci#include <linux/usb.h>
1562306a36Sopenharmony_ci#include <linux/workqueue.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/regulator/consumer.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include <linux/usb/gadget.h>
2062306a36Sopenharmony_ci#include <linux/usb/otg.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/*
2462306a36Sopenharmony_ci * A simple GPIO VBUS sensing driver for B peripheral only devices
2562306a36Sopenharmony_ci * with internal transceivers. It can control a D+ pullup GPIO and
2662306a36Sopenharmony_ci * a regulator to limit the current drawn from VBUS.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * Needs to be loaded before the UDC driver that will use it.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_cistruct gpio_vbus_data {
3162306a36Sopenharmony_ci	struct gpio_desc	*vbus_gpiod;
3262306a36Sopenharmony_ci	struct gpio_desc	*pullup_gpiod;
3362306a36Sopenharmony_ci	struct usb_phy		phy;
3462306a36Sopenharmony_ci	struct device          *dev;
3562306a36Sopenharmony_ci	struct regulator       *vbus_draw;
3662306a36Sopenharmony_ci	int			vbus_draw_enabled;
3762306a36Sopenharmony_ci	unsigned		mA;
3862306a36Sopenharmony_ci	struct delayed_work	work;
3962306a36Sopenharmony_ci	int			vbus;
4062306a36Sopenharmony_ci	int			irq;
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/*
4562306a36Sopenharmony_ci * This driver relies on "both edges" triggering.  VBUS has 100 msec to
4662306a36Sopenharmony_ci * stabilize, so the peripheral controller driver may need to cope with
4762306a36Sopenharmony_ci * some bouncing due to current surges (e.g. charging local capacitance)
4862306a36Sopenharmony_ci * and contact chatter.
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * REVISIT in desperate straits, toggling between rising and falling
5162306a36Sopenharmony_ci * edges might be workable.
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_ci#define VBUS_IRQ_FLAGS \
5462306a36Sopenharmony_ci	(IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/* interface to regulator framework */
5862306a36Sopenharmony_cistatic void set_vbus_draw(struct gpio_vbus_data *gpio_vbus, unsigned mA)
5962306a36Sopenharmony_ci{
6062306a36Sopenharmony_ci	struct regulator *vbus_draw = gpio_vbus->vbus_draw;
6162306a36Sopenharmony_ci	int enabled;
6262306a36Sopenharmony_ci	int ret;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	if (!vbus_draw)
6562306a36Sopenharmony_ci		return;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	enabled = gpio_vbus->vbus_draw_enabled;
6862306a36Sopenharmony_ci	if (mA) {
6962306a36Sopenharmony_ci		regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
7062306a36Sopenharmony_ci		if (!enabled) {
7162306a36Sopenharmony_ci			ret = regulator_enable(vbus_draw);
7262306a36Sopenharmony_ci			if (ret < 0)
7362306a36Sopenharmony_ci				return;
7462306a36Sopenharmony_ci			gpio_vbus->vbus_draw_enabled = 1;
7562306a36Sopenharmony_ci		}
7662306a36Sopenharmony_ci	} else {
7762306a36Sopenharmony_ci		if (enabled) {
7862306a36Sopenharmony_ci			ret = regulator_disable(vbus_draw);
7962306a36Sopenharmony_ci			if (ret < 0)
8062306a36Sopenharmony_ci				return;
8162306a36Sopenharmony_ci			gpio_vbus->vbus_draw_enabled = 0;
8262306a36Sopenharmony_ci		}
8362306a36Sopenharmony_ci	}
8462306a36Sopenharmony_ci	gpio_vbus->mA = mA;
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_cistatic int is_vbus_powered(struct gpio_vbus_data *gpio_vbus)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	return gpiod_get_value(gpio_vbus->vbus_gpiod);
9062306a36Sopenharmony_ci}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_cistatic void gpio_vbus_work(struct work_struct *work)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus =
9562306a36Sopenharmony_ci		container_of(work, struct gpio_vbus_data, work.work);
9662306a36Sopenharmony_ci	int status, vbus;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	if (!gpio_vbus->phy.otg->gadget)
9962306a36Sopenharmony_ci		return;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	vbus = is_vbus_powered(gpio_vbus);
10262306a36Sopenharmony_ci	if ((vbus ^ gpio_vbus->vbus) == 0)
10362306a36Sopenharmony_ci		return;
10462306a36Sopenharmony_ci	gpio_vbus->vbus = vbus;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	/* Peripheral controllers which manage the pullup themselves won't have
10762306a36Sopenharmony_ci	 * a pullup GPIO configured here.  If it's configured here, we'll do
10862306a36Sopenharmony_ci	 * what isp1301_omap::b_peripheral() does and enable the pullup here...
10962306a36Sopenharmony_ci	 * although that may complicate usb_gadget_{,dis}connect() support.
11062306a36Sopenharmony_ci	 */
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	if (vbus) {
11362306a36Sopenharmony_ci		status = USB_EVENT_VBUS;
11462306a36Sopenharmony_ci		gpio_vbus->phy.otg->state = OTG_STATE_B_PERIPHERAL;
11562306a36Sopenharmony_ci		gpio_vbus->phy.last_event = status;
11662306a36Sopenharmony_ci		usb_gadget_vbus_connect(gpio_vbus->phy.otg->gadget);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci		/* drawing a "unit load" is *always* OK, except for OTG */
11962306a36Sopenharmony_ci		set_vbus_draw(gpio_vbus, 100);
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci		/* optionally enable D+ pullup */
12262306a36Sopenharmony_ci		if (gpio_vbus->pullup_gpiod)
12362306a36Sopenharmony_ci			gpiod_set_value(gpio_vbus->pullup_gpiod, 1);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci		atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
12662306a36Sopenharmony_ci					   status, gpio_vbus->phy.otg->gadget);
12762306a36Sopenharmony_ci		usb_phy_set_event(&gpio_vbus->phy, USB_EVENT_ENUMERATED);
12862306a36Sopenharmony_ci	} else {
12962306a36Sopenharmony_ci		/* optionally disable D+ pullup */
13062306a36Sopenharmony_ci		if (gpio_vbus->pullup_gpiod)
13162306a36Sopenharmony_ci			gpiod_set_value(gpio_vbus->pullup_gpiod, 0);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci		set_vbus_draw(gpio_vbus, 0);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci		usb_gadget_vbus_disconnect(gpio_vbus->phy.otg->gadget);
13662306a36Sopenharmony_ci		status = USB_EVENT_NONE;
13762306a36Sopenharmony_ci		gpio_vbus->phy.otg->state = OTG_STATE_B_IDLE;
13862306a36Sopenharmony_ci		gpio_vbus->phy.last_event = status;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci		atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
14162306a36Sopenharmony_ci					   status, gpio_vbus->phy.otg->gadget);
14262306a36Sopenharmony_ci		usb_phy_set_event(&gpio_vbus->phy, USB_EVENT_NONE);
14362306a36Sopenharmony_ci	}
14462306a36Sopenharmony_ci}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci/* VBUS change IRQ handler */
14762306a36Sopenharmony_cistatic irqreturn_t gpio_vbus_irq(int irq, void *data)
14862306a36Sopenharmony_ci{
14962306a36Sopenharmony_ci	struct platform_device *pdev = data;
15062306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
15162306a36Sopenharmony_ci	struct usb_otg *otg = gpio_vbus->phy.otg;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
15462306a36Sopenharmony_ci		is_vbus_powered(gpio_vbus) ? "supplied" : "inactive",
15562306a36Sopenharmony_ci		otg->gadget ? otg->gadget->name : "none");
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	if (otg->gadget)
15862306a36Sopenharmony_ci		schedule_delayed_work(&gpio_vbus->work, msecs_to_jiffies(100));
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	return IRQ_HANDLED;
16162306a36Sopenharmony_ci}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci/* OTG transceiver interface */
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci/* bind/unbind the peripheral controller */
16662306a36Sopenharmony_cistatic int gpio_vbus_set_peripheral(struct usb_otg *otg,
16762306a36Sopenharmony_ci					struct usb_gadget *gadget)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus;
17062306a36Sopenharmony_ci	struct platform_device *pdev;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	gpio_vbus = container_of(otg->usb_phy, struct gpio_vbus_data, phy);
17362306a36Sopenharmony_ci	pdev = to_platform_device(gpio_vbus->dev);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	if (!gadget) {
17662306a36Sopenharmony_ci		dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
17762306a36Sopenharmony_ci			otg->gadget->name);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci		/* optionally disable D+ pullup */
18062306a36Sopenharmony_ci		if (gpio_vbus->pullup_gpiod)
18162306a36Sopenharmony_ci			gpiod_set_value(gpio_vbus->pullup_gpiod, 0);
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci		set_vbus_draw(gpio_vbus, 0);
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci		usb_gadget_vbus_disconnect(otg->gadget);
18662306a36Sopenharmony_ci		otg->state = OTG_STATE_UNDEFINED;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci		otg->gadget = NULL;
18962306a36Sopenharmony_ci		return 0;
19062306a36Sopenharmony_ci	}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	otg->gadget = gadget;
19362306a36Sopenharmony_ci	dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	/* initialize connection state */
19662306a36Sopenharmony_ci	gpio_vbus->vbus = 0; /* start with disconnected */
19762306a36Sopenharmony_ci	gpio_vbus_irq(gpio_vbus->irq, pdev);
19862306a36Sopenharmony_ci	return 0;
19962306a36Sopenharmony_ci}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci/* effective for B devices, ignored for A-peripheral */
20262306a36Sopenharmony_cistatic int gpio_vbus_set_power(struct usb_phy *phy, unsigned mA)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	if (phy->otg->state == OTG_STATE_B_PERIPHERAL)
20962306a36Sopenharmony_ci		set_vbus_draw(gpio_vbus, mA);
21062306a36Sopenharmony_ci	return 0;
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/* for non-OTG B devices: set/clear transceiver suspend mode */
21462306a36Sopenharmony_cistatic int gpio_vbus_set_suspend(struct usb_phy *phy, int suspend)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	/* draw max 0 mA from vbus in suspend mode; or the previously
22162306a36Sopenharmony_ci	 * recorded amount of current if not suspended
22262306a36Sopenharmony_ci	 *
22362306a36Sopenharmony_ci	 * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
22462306a36Sopenharmony_ci	 * if they're wake-enabled ... we don't handle that yet.
22562306a36Sopenharmony_ci	 */
22662306a36Sopenharmony_ci	return gpio_vbus_set_power(phy, suspend ? 0 : gpio_vbus->mA);
22762306a36Sopenharmony_ci}
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci/* platform driver interface */
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_cistatic int gpio_vbus_probe(struct platform_device *pdev)
23262306a36Sopenharmony_ci{
23362306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus;
23462306a36Sopenharmony_ci	struct resource *res;
23562306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
23662306a36Sopenharmony_ci	int err, irq;
23762306a36Sopenharmony_ci	unsigned long irqflags;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	gpio_vbus = devm_kzalloc(&pdev->dev, sizeof(struct gpio_vbus_data),
24062306a36Sopenharmony_ci				 GFP_KERNEL);
24162306a36Sopenharmony_ci	if (!gpio_vbus)
24262306a36Sopenharmony_ci		return -ENOMEM;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	gpio_vbus->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
24562306a36Sopenharmony_ci					  GFP_KERNEL);
24662306a36Sopenharmony_ci	if (!gpio_vbus->phy.otg)
24762306a36Sopenharmony_ci		return -ENOMEM;
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	platform_set_drvdata(pdev, gpio_vbus);
25062306a36Sopenharmony_ci	gpio_vbus->dev = &pdev->dev;
25162306a36Sopenharmony_ci	gpio_vbus->phy.label = "gpio-vbus";
25262306a36Sopenharmony_ci	gpio_vbus->phy.dev = gpio_vbus->dev;
25362306a36Sopenharmony_ci	gpio_vbus->phy.set_power = gpio_vbus_set_power;
25462306a36Sopenharmony_ci	gpio_vbus->phy.set_suspend = gpio_vbus_set_suspend;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	gpio_vbus->phy.otg->state = OTG_STATE_UNDEFINED;
25762306a36Sopenharmony_ci	gpio_vbus->phy.otg->usb_phy = &gpio_vbus->phy;
25862306a36Sopenharmony_ci	gpio_vbus->phy.otg->set_peripheral = gpio_vbus_set_peripheral;
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	/* Look up the VBUS sensing GPIO */
26162306a36Sopenharmony_ci	gpio_vbus->vbus_gpiod = devm_gpiod_get(dev, "vbus", GPIOD_IN);
26262306a36Sopenharmony_ci	if (IS_ERR(gpio_vbus->vbus_gpiod)) {
26362306a36Sopenharmony_ci		err = PTR_ERR(gpio_vbus->vbus_gpiod);
26462306a36Sopenharmony_ci		dev_err(&pdev->dev, "can't request vbus gpio, err: %d\n", err);
26562306a36Sopenharmony_ci		return err;
26662306a36Sopenharmony_ci	}
26762306a36Sopenharmony_ci	gpiod_set_consumer_name(gpio_vbus->vbus_gpiod, "vbus_detect");
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
27062306a36Sopenharmony_ci	if (res) {
27162306a36Sopenharmony_ci		irq = res->start;
27262306a36Sopenharmony_ci		irqflags = (res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
27362306a36Sopenharmony_ci	} else {
27462306a36Sopenharmony_ci		irq = gpiod_to_irq(gpio_vbus->vbus_gpiod);
27562306a36Sopenharmony_ci		irqflags = VBUS_IRQ_FLAGS;
27662306a36Sopenharmony_ci	}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	gpio_vbus->irq = irq;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	/*
28162306a36Sopenharmony_ci	 * The VBUS sensing GPIO should have a pulldown, which will normally be
28262306a36Sopenharmony_ci	 * part of a resistor ladder turning a 4.0V-5.25V level on VBUS into a
28362306a36Sopenharmony_ci	 * value the GPIO detects as active. Some systems will use comparators.
28462306a36Sopenharmony_ci	 * Get the optional D+ or D- pullup GPIO. If the data line pullup is
28562306a36Sopenharmony_ci	 * in use, initialize it to "not pulling up"
28662306a36Sopenharmony_ci	 */
28762306a36Sopenharmony_ci	gpio_vbus->pullup_gpiod = devm_gpiod_get_optional(dev, "pullup",
28862306a36Sopenharmony_ci							  GPIOD_OUT_LOW);
28962306a36Sopenharmony_ci	if (IS_ERR(gpio_vbus->pullup_gpiod)) {
29062306a36Sopenharmony_ci		err = PTR_ERR(gpio_vbus->pullup_gpiod);
29162306a36Sopenharmony_ci		dev_err(&pdev->dev, "can't request pullup gpio, err: %d\n",
29262306a36Sopenharmony_ci			err);
29362306a36Sopenharmony_ci		return err;
29462306a36Sopenharmony_ci	}
29562306a36Sopenharmony_ci	if (gpio_vbus->pullup_gpiod)
29662306a36Sopenharmony_ci		gpiod_set_consumer_name(gpio_vbus->pullup_gpiod, "udc_pullup");
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	err = devm_request_irq(&pdev->dev, irq, gpio_vbus_irq, irqflags,
29962306a36Sopenharmony_ci			       "vbus_detect", pdev);
30062306a36Sopenharmony_ci	if (err) {
30162306a36Sopenharmony_ci		dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
30262306a36Sopenharmony_ci			irq, err);
30362306a36Sopenharmony_ci		return err;
30462306a36Sopenharmony_ci	}
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	INIT_DELAYED_WORK(&gpio_vbus->work, gpio_vbus_work);
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	gpio_vbus->vbus_draw = devm_regulator_get(&pdev->dev, "vbus_draw");
30962306a36Sopenharmony_ci	if (IS_ERR(gpio_vbus->vbus_draw)) {
31062306a36Sopenharmony_ci		dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
31162306a36Sopenharmony_ci			PTR_ERR(gpio_vbus->vbus_draw));
31262306a36Sopenharmony_ci		gpio_vbus->vbus_draw = NULL;
31362306a36Sopenharmony_ci	}
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	/* only active when a gadget is registered */
31662306a36Sopenharmony_ci	err = usb_add_phy(&gpio_vbus->phy, USB_PHY_TYPE_USB2);
31762306a36Sopenharmony_ci	if (err) {
31862306a36Sopenharmony_ci		dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
31962306a36Sopenharmony_ci			err);
32062306a36Sopenharmony_ci		return err;
32162306a36Sopenharmony_ci	}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	/* TODO: wakeup could be enabled here with device_init_wakeup(dev, 1) */
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	return 0;
32662306a36Sopenharmony_ci}
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_cistatic void gpio_vbus_remove(struct platform_device *pdev)
32962306a36Sopenharmony_ci{
33062306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	device_init_wakeup(&pdev->dev, 0);
33362306a36Sopenharmony_ci	cancel_delayed_work_sync(&gpio_vbus->work);
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci	usb_remove_phy(&gpio_vbus->phy);
33662306a36Sopenharmony_ci}
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci#ifdef CONFIG_PM
33962306a36Sopenharmony_cistatic int gpio_vbus_pm_suspend(struct device *dev)
34062306a36Sopenharmony_ci{
34162306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus = dev_get_drvdata(dev);
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	if (device_may_wakeup(dev))
34462306a36Sopenharmony_ci		enable_irq_wake(gpio_vbus->irq);
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	return 0;
34762306a36Sopenharmony_ci}
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_cistatic int gpio_vbus_pm_resume(struct device *dev)
35062306a36Sopenharmony_ci{
35162306a36Sopenharmony_ci	struct gpio_vbus_data *gpio_vbus = dev_get_drvdata(dev);
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	if (device_may_wakeup(dev))
35462306a36Sopenharmony_ci		disable_irq_wake(gpio_vbus->irq);
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	return 0;
35762306a36Sopenharmony_ci}
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_cistatic const struct dev_pm_ops gpio_vbus_dev_pm_ops = {
36062306a36Sopenharmony_ci	.suspend	= gpio_vbus_pm_suspend,
36162306a36Sopenharmony_ci	.resume		= gpio_vbus_pm_resume,
36262306a36Sopenharmony_ci};
36362306a36Sopenharmony_ci#endif
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ciMODULE_ALIAS("platform:gpio-vbus");
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci/*
36862306a36Sopenharmony_ci * NOTE: this driver matches against "gpio-usb-b-connector" for
36962306a36Sopenharmony_ci * devices that do NOT support role switch.
37062306a36Sopenharmony_ci */
37162306a36Sopenharmony_cistatic const struct of_device_id gpio_vbus_of_match[] = {
37262306a36Sopenharmony_ci	{
37362306a36Sopenharmony_ci		.compatible = "gpio-usb-b-connector",
37462306a36Sopenharmony_ci	},
37562306a36Sopenharmony_ci	{},
37662306a36Sopenharmony_ci};
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_cistatic struct platform_driver gpio_vbus_driver = {
37962306a36Sopenharmony_ci	.driver = {
38062306a36Sopenharmony_ci		.name  = "gpio-vbus",
38162306a36Sopenharmony_ci#ifdef CONFIG_PM
38262306a36Sopenharmony_ci		.pm = &gpio_vbus_dev_pm_ops,
38362306a36Sopenharmony_ci#endif
38462306a36Sopenharmony_ci		.of_match_table = gpio_vbus_of_match,
38562306a36Sopenharmony_ci	},
38662306a36Sopenharmony_ci	.probe		= gpio_vbus_probe,
38762306a36Sopenharmony_ci	.remove_new	= gpio_vbus_remove,
38862306a36Sopenharmony_ci};
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_cimodule_platform_driver(gpio_vbus_driver);
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ciMODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
39362306a36Sopenharmony_ciMODULE_AUTHOR("Philipp Zabel");
39462306a36Sopenharmony_ciMODULE_LICENSE("GPL");
395