Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * gpio-vbus.c - simple GPIO VBUS sensing driver for B peripheral devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2008 Philipp Zabel <philipp.zabel@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/usb/gadget.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/usb/otg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * A simple GPIO VBUS sensing driver for B peripheral only devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * with internal transceivers. It can control a D+ pullup GPIO and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * a regulator to limit the current drawn from VBUS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * Needs to be loaded before the UDC driver that will use it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) struct gpio_vbus_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct gpio_desc	*vbus_gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct gpio_desc	*pullup_gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct usb_phy		phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct device          *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct regulator       *vbus_draw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int			vbus_draw_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	unsigned		mA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct delayed_work	work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int			vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int			irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * This driver relies on "both edges" triggering.  VBUS has 100 msec to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * stabilize, so the peripheral controller driver may need to cope with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * some bouncing due to current surges (e.g. charging local capacitance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * and contact chatter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * REVISIT in desperate straits, toggling between rising and falling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * edges might be workable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define VBUS_IRQ_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	(IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /* interface to regulator framework */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static void set_vbus_draw(struct gpio_vbus_data *gpio_vbus, unsigned mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct regulator *vbus_draw = gpio_vbus->vbus_draw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (!vbus_draw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	enabled = gpio_vbus->vbus_draw_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (mA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		if (!enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			ret = regulator_enable(vbus_draw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			gpio_vbus->vbus_draw_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			ret = regulator_disable(vbus_draw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			gpio_vbus->vbus_draw_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	gpio_vbus->mA = mA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static int is_vbus_powered(struct gpio_vbus_data *gpio_vbus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return gpiod_get_value(gpio_vbus->vbus_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static void gpio_vbus_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct gpio_vbus_data *gpio_vbus =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		container_of(work, struct gpio_vbus_data, work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int status, vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (!gpio_vbus->phy.otg->gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	vbus = is_vbus_powered(gpio_vbus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if ((vbus ^ gpio_vbus->vbus) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	gpio_vbus->vbus = vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* Peripheral controllers which manage the pullup themselves won't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 * a pullup GPIO configured here.  If it's configured here, we'll do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	 * what isp1301_omap::b_peripheral() does and enable the pullup here...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	 * although that may complicate usb_gadget_{,dis}connect() support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (vbus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		status = USB_EVENT_VBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		gpio_vbus->phy.otg->state = OTG_STATE_B_PERIPHERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		gpio_vbus->phy.last_event = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		usb_gadget_vbus_connect(gpio_vbus->phy.otg->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		/* drawing a "unit load" is *always* OK, except for OTG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		set_vbus_draw(gpio_vbus, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		/* optionally enable D+ pullup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (gpio_vbus->pullup_gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			gpiod_set_value(gpio_vbus->pullup_gpiod, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 					   status, gpio_vbus->phy.otg->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		usb_phy_set_event(&gpio_vbus->phy, USB_EVENT_ENUMERATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		/* optionally disable D+ pullup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (gpio_vbus->pullup_gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			gpiod_set_value(gpio_vbus->pullup_gpiod, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		set_vbus_draw(gpio_vbus, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		usb_gadget_vbus_disconnect(gpio_vbus->phy.otg->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		status = USB_EVENT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		gpio_vbus->phy.otg->state = OTG_STATE_B_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		gpio_vbus->phy.last_event = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 					   status, gpio_vbus->phy.otg->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		usb_phy_set_event(&gpio_vbus->phy, USB_EVENT_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* VBUS change IRQ handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static irqreturn_t gpio_vbus_irq(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	struct platform_device *pdev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct usb_otg *otg = gpio_vbus->phy.otg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		is_vbus_powered(gpio_vbus) ? "supplied" : "inactive",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		otg->gadget ? otg->gadget->name : "none");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (otg->gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		schedule_delayed_work(&gpio_vbus->work, msecs_to_jiffies(100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* OTG transceiver interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* bind/unbind the peripheral controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int gpio_vbus_set_peripheral(struct usb_otg *otg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 					struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct gpio_vbus_data *gpio_vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	gpio_vbus = container_of(otg->usb_phy, struct gpio_vbus_data, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	pdev = to_platform_device(gpio_vbus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (!gadget) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			otg->gadget->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		/* optionally disable D+ pullup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		if (gpio_vbus->pullup_gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			gpiod_set_value(gpio_vbus->pullup_gpiod, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		set_vbus_draw(gpio_vbus, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		usb_gadget_vbus_disconnect(otg->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		otg->state = OTG_STATE_UNDEFINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		otg->gadget = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	otg->gadget = gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	/* initialize connection state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	gpio_vbus->vbus = 0; /* start with disconnected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	gpio_vbus_irq(gpio_vbus->irq, pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* effective for B devices, ignored for A-peripheral */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int gpio_vbus_set_power(struct usb_phy *phy, unsigned mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	struct gpio_vbus_data *gpio_vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (phy->otg->state == OTG_STATE_B_PERIPHERAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		set_vbus_draw(gpio_vbus, mA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* for non-OTG B devices: set/clear transceiver suspend mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int gpio_vbus_set_suspend(struct usb_phy *phy, int suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct gpio_vbus_data *gpio_vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	/* draw max 0 mA from vbus in suspend mode; or the previously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	 * recorded amount of current if not suspended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	 * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	 * if they're wake-enabled ... we don't handle that yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return gpio_vbus_set_power(phy, suspend ? 0 : gpio_vbus->mA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /* platform driver interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int gpio_vbus_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct gpio_vbus_data *gpio_vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	int err, irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	gpio_vbus = devm_kzalloc(&pdev->dev, sizeof(struct gpio_vbus_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 				 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (!gpio_vbus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	gpio_vbus->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 					  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (!gpio_vbus->phy.otg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	platform_set_drvdata(pdev, gpio_vbus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	gpio_vbus->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	gpio_vbus->phy.label = "gpio-vbus";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	gpio_vbus->phy.dev = gpio_vbus->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	gpio_vbus->phy.set_power = gpio_vbus_set_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	gpio_vbus->phy.set_suspend = gpio_vbus_set_suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	gpio_vbus->phy.otg->state = OTG_STATE_UNDEFINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	gpio_vbus->phy.otg->usb_phy = &gpio_vbus->phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	gpio_vbus->phy.otg->set_peripheral = gpio_vbus_set_peripheral;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/* Look up the VBUS sensing GPIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	gpio_vbus->vbus_gpiod = devm_gpiod_get(dev, "vbus", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (IS_ERR(gpio_vbus->vbus_gpiod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		err = PTR_ERR(gpio_vbus->vbus_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		dev_err(&pdev->dev, "can't request vbus gpio, err: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	gpiod_set_consumer_name(gpio_vbus->vbus_gpiod, "vbus_detect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		irq = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		irqflags = (res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		irq = gpiod_to_irq(gpio_vbus->vbus_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		irqflags = VBUS_IRQ_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	gpio_vbus->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	 * The VBUS sensing GPIO should have a pulldown, which will normally be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	 * part of a resistor ladder turning a 4.0V-5.25V level on VBUS into a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * value the GPIO detects as active. Some systems will use comparators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * Get the optional D+ or D- pullup GPIO. If the data line pullup is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * in use, initialize it to "not pulling up"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	gpio_vbus->pullup_gpiod = devm_gpiod_get_optional(dev, "pullup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 							  GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (IS_ERR(gpio_vbus->pullup_gpiod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		err = PTR_ERR(gpio_vbus->pullup_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		dev_err(&pdev->dev, "can't request pullup gpio, err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (gpio_vbus->pullup_gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		gpiod_set_consumer_name(gpio_vbus->pullup_gpiod, "udc_pullup");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	err = devm_request_irq(&pdev->dev, irq, gpio_vbus_irq, irqflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			       "vbus_detect", pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			irq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	INIT_DELAYED_WORK(&gpio_vbus->work, gpio_vbus_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	gpio_vbus->vbus_draw = devm_regulator_get(&pdev->dev, "vbus_draw");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (IS_ERR(gpio_vbus->vbus_draw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			PTR_ERR(gpio_vbus->vbus_draw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		gpio_vbus->vbus_draw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	/* only active when a gadget is registered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	err = usb_add_phy(&gpio_vbus->phy, USB_PHY_TYPE_USB2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	/* TODO: wakeup could be enabled here with device_init_wakeup(dev, 1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int gpio_vbus_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	device_init_wakeup(&pdev->dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	cancel_delayed_work_sync(&gpio_vbus->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	usb_remove_phy(&gpio_vbus->phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static int gpio_vbus_pm_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct gpio_vbus_data *gpio_vbus = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		enable_irq_wake(gpio_vbus->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static int gpio_vbus_pm_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	struct gpio_vbus_data *gpio_vbus = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		disable_irq_wake(gpio_vbus->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static const struct dev_pm_ops gpio_vbus_dev_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	.suspend	= gpio_vbus_pm_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	.resume		= gpio_vbus_pm_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) MODULE_ALIAS("platform:gpio-vbus");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static struct platform_driver gpio_vbus_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		.name  = "gpio-vbus",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		.pm = &gpio_vbus_dev_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	.probe		= gpio_vbus_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	.remove		= gpio_vbus_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) module_platform_driver(gpio_vbus_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) MODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) MODULE_AUTHOR("Philipp Zabel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) MODULE_LICENSE("GPL");