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