^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) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/usb/otg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/usb/usb_phy_generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/usb/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "phy-am335x-control.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "phy-generic.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct am335x_phy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct usb_phy_generic usb_phy_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct phy_control *phy_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) enum usb_dr_mode dr_mode;
^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) static int am335x_init(struct usb_phy *phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct am335x_phy *am_phy = dev_get_drvdata(phy->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void am335x_shutdown(struct usb_phy *phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct am335x_phy *am_phy = dev_get_drvdata(phy->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int am335x_phy_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct am335x_phy *am_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) am_phy = devm_kzalloc(dev, sizeof(*am_phy), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!am_phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) am_phy->phy_ctrl = am335x_get_phy_control(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (!am_phy->phy_ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) am_phy->id = of_alias_get_id(pdev->dev.of_node, "phy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (am_phy->id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) dev_err(&pdev->dev, "Missing PHY id: %d\n", am_phy->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return am_phy->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) am_phy->dr_mode = of_usb_get_dr_mode_by_phy(pdev->dev.of_node, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ret = usb_phy_gen_create_phy(dev, &am_phy->usb_phy_gen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) am_phy->usb_phy_gen.phy.init = am335x_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) am_phy->usb_phy_gen.phy.shutdown = am335x_shutdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) platform_set_drvdata(pdev, am_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) device_init_wakeup(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * If we leave PHY wakeup enabled then AM33XX wakes up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * immediately from DS0. To avoid this we mark dev->power.can_wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * to false. The same is checked in suspend routine to decide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * on whether to enable PHY wakeup or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * PHY wakeup works fine in standby mode, there by allowing us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * handle remote wakeup, wakeup on disconnect and connect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) device_set_wakeup_enable(dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return usb_add_phy_dev(&am_phy->usb_phy_gen.phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int am335x_phy_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct am335x_phy *am_phy = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) usb_remove_phy(&am_phy->usb_phy_gen.phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int am335x_phy_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct am335x_phy *am_phy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Enable phy wakeup only if dev->power.can_wakeup is true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Make sure to enable wakeup to support remote wakeup in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * standby mode ( same is not supported in OFF(DS0) mode).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Enable it by doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * echo enabled > /sys/bus/platform/devices/<usb-phy-id>/power/wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int am335x_phy_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct am335x_phy *am_phy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static SIMPLE_DEV_PM_OPS(am335x_pm_ops, am335x_phy_suspend, am335x_phy_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static const struct of_device_id am335x_phy_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) { .compatible = "ti,am335x-usb-phy" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) MODULE_DEVICE_TABLE(of, am335x_phy_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static struct platform_driver am335x_phy_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .probe = am335x_phy_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .remove = am335x_phy_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .name = "am335x-phy-driver",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .pm = &am335x_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .of_match_table = am335x_phy_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) },
^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) module_platform_driver(am335x_phy_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) MODULE_LICENSE("GPL v2");