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)  * NOP USB transceiver for all USB transceiver which are either built-in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * into USB IP or which are mostly autonomous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2009 Texas Instruments Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Current status:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *	This provides a "nop" transceiver for PHYs which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *	autonomous such as isp1504, isp1707, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/usb/gadget.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/usb/otg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/usb/usb_phy_generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "phy-generic.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define VBUS_IRQ_FLAGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	(IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		IRQF_ONESHOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct platform_device *usb_phy_generic_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return platform_device_register_simple("usb_phy_generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 			PLATFORM_DEVID_AUTO, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) EXPORT_SYMBOL_GPL(usb_phy_generic_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) void usb_phy_generic_unregister(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	platform_device_unregister(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) EXPORT_SYMBOL_GPL(usb_phy_generic_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int nop_set_suspend(struct usb_phy *x, int suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct usb_phy_generic *nop = dev_get_drvdata(x->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (!IS_ERR(nop->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		if (suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			clk_disable_unprepare(nop->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			clk_prepare_enable(nop->clk);
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static void nop_reset(struct usb_phy_generic *nop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (!nop->gpiod_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	gpiod_set_value_cansleep(nop->gpiod_reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	usleep_range(10000, 20000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	gpiod_set_value_cansleep(nop->gpiod_reset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /* interface to regulator framework */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static void nop_set_vbus_draw(struct usb_phy_generic *nop, unsigned mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct regulator *vbus_draw = nop->vbus_draw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (!vbus_draw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	enabled = nop->vbus_draw_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (mA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (!enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			ret = regulator_enable(vbus_draw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			nop->vbus_draw_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			ret = regulator_disable(vbus_draw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			nop->vbus_draw_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	nop->mA = mA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static irqreturn_t nop_gpio_vbus_thread(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct usb_phy_generic *nop = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct usb_otg *otg = nop->phy.otg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	int vbus, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	vbus = gpiod_get_value(nop->gpiod_vbus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if ((vbus ^ nop->vbus) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	nop->vbus = vbus;
^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) 		otg->state = OTG_STATE_B_PERIPHERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		nop->phy.last_event = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		/* drawing a "unit load" is *always* OK, except for OTG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		nop_set_vbus_draw(nop, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		atomic_notifier_call_chain(&nop->phy.notifier, status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 					   otg->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		nop_set_vbus_draw(nop, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		status = USB_EVENT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		otg->state = OTG_STATE_B_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		nop->phy.last_event = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		atomic_notifier_call_chain(&nop->phy.notifier, status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 					   otg->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int usb_gen_phy_init(struct usb_phy *phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (!IS_ERR(nop->vcc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (regulator_enable(nop->vcc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			dev_err(phy->dev, "Failed to enable power\n");
^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) 	if (!IS_ERR(nop->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		ret = clk_prepare_enable(nop->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	nop_reset(nop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) EXPORT_SYMBOL_GPL(usb_gen_phy_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) void usb_gen_phy_shutdown(struct usb_phy *phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	gpiod_set_value_cansleep(nop->gpiod_reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (!IS_ERR(nop->clk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		clk_disable_unprepare(nop->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (!IS_ERR(nop->vcc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (regulator_disable(nop->vcc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			dev_err(phy->dev, "Failed to disable power\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (!otg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (!gadget) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		otg->gadget = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	otg->gadget = gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (otg->state == OTG_STATE_B_PERIPHERAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		atomic_notifier_call_chain(&otg->usb_phy->notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 					   USB_EVENT_VBUS, otg->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		otg->state = OTG_STATE_B_IDLE;
^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) static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (!otg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (!host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		otg->host = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	otg->host = host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	enum usb_phy_type type = USB_PHY_TYPE_USB2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	u32 clk_rate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	bool needs_vcc = false, needs_clk = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (dev->of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		struct device_node *node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (of_property_read_u32(node, "clock-frequency", &clk_rate))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			clk_rate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		needs_vcc = of_property_read_bool(node, "vcc-supply");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		needs_clk = of_property_read_bool(node, "clocks");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	nop->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 						   GPIOD_ASIS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	err = PTR_ERR_OR_ZERO(nop->gpiod_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		nop->gpiod_vbus = devm_gpiod_get_optional(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 						 "vbus-detect",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 						 GPIOD_ASIS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		err = PTR_ERR_OR_ZERO(nop->gpiod_vbus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (err == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		dev_err(dev, "Error requesting RESET or VBUS GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (nop->gpiod_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		gpiod_direction_output(nop->gpiod_reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (!nop->phy.otg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	nop->clk = devm_clk_get(dev, "main_clk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (IS_ERR(nop->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		dev_dbg(dev, "Can't get phy clock: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 					PTR_ERR(nop->clk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (needs_clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return PTR_ERR(nop->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (!IS_ERR(nop->clk) && clk_rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		err = clk_set_rate(nop->clk, clk_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			dev_err(dev, "Error setting clock rate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	nop->vcc = devm_regulator_get(dev, "vcc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (IS_ERR(nop->vcc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		dev_dbg(dev, "Error getting vcc regulator: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 					PTR_ERR(nop->vcc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		if (needs_vcc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	nop->dev		= dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	nop->phy.dev		= nop->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	nop->phy.label		= "nop-xceiv";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	nop->phy.set_suspend	= nop_set_suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	nop->phy.type		= type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	nop->phy.otg->state		= OTG_STATE_UNDEFINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	nop->phy.otg->usb_phy		= &nop->phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	nop->phy.otg->set_host		= nop_set_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	nop->phy.otg->set_peripheral	= nop_set_peripheral;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int usb_phy_generic_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct usb_phy_generic	*nop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (!nop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	err = usb_phy_gen_create_phy(dev, nop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (nop->gpiod_vbus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		err = devm_request_threaded_irq(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 						gpiod_to_irq(nop->gpiod_vbus),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 						NULL, nop_gpio_vbus_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 						VBUS_IRQ_FLAGS, "vbus_detect",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 						nop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 				gpiod_to_irq(nop->gpiod_vbus), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		nop->phy.otg->state = gpiod_get_value(nop->gpiod_vbus) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			OTG_STATE_B_PERIPHERAL : OTG_STATE_B_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	nop->phy.init		= usb_gen_phy_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	nop->phy.shutdown	= usb_gen_phy_shutdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	err = usb_add_phy_dev(&nop->phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	platform_set_drvdata(pdev, nop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static int usb_phy_generic_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct usb_phy_generic *nop = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	usb_remove_phy(&nop->phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static const struct of_device_id nop_xceiv_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	{ .compatible = "usb-nop-xceiv" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static struct platform_driver usb_phy_generic_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	.probe		= usb_phy_generic_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	.remove		= usb_phy_generic_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		.name	= "usb_phy_generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		.of_match_table = nop_xceiv_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static int __init usb_phy_generic_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return platform_driver_register(&usb_phy_generic_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) subsys_initcall(usb_phy_generic_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static void __exit usb_phy_generic_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	platform_driver_unregister(&usb_phy_generic_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) module_exit(usb_phy_generic_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) MODULE_ALIAS("platform:usb_phy_generic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) MODULE_AUTHOR("Texas Instruments Inc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) MODULE_DESCRIPTION("NOP USB Transceiver driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) MODULE_LICENSE("GPL");