phy-bcm-ns2-usbdrd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (C) 2017 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/extcon-provider.h>
  15. #include <linux/gpio.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/irq.h>
  21. #include <linux/mfd/syscon.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/phy/phy.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/regmap.h>
  28. #include <linux/slab.h>
  29. #include <linux/workqueue.h>
  30. #define ICFG_DRD_AFE 0x0
  31. #define ICFG_MISC_STAT 0x18
  32. #define ICFG_DRD_P0CTL 0x1C
  33. #define ICFG_STRAP_CTRL 0x20
  34. #define ICFG_FSM_CTRL 0x24
  35. #define ICFG_DEV_BIT BIT(2)
  36. #define IDM_RST_BIT BIT(0)
  37. #define AFE_CORERDY_VDDC BIT(18)
  38. #define PHY_PLL_RESETB BIT(15)
  39. #define PHY_RESETB BIT(14)
  40. #define PHY_PLL_LOCK BIT(0)
  41. #define DRD_DEV_MODE BIT(20)
  42. #define OHCI_OVRCUR_POL BIT(11)
  43. #define ICFG_OFF_MODE BIT(6)
  44. #define PLL_LOCK_RETRY 1000
  45. #define EVT_DEVICE 0
  46. #define EVT_HOST 1
  47. #define DRD_HOST_MODE (BIT(2) | BIT(3))
  48. #define DRD_DEVICE_MODE (BIT(4) | BIT(5))
  49. #define DRD_HOST_VAL 0x803
  50. #define DRD_DEV_VAL 0x807
  51. #define GPIO_DELAY 20
  52. struct ns2_phy_data;
  53. struct ns2_phy_driver {
  54. void __iomem *icfgdrd_regs;
  55. void __iomem *idmdrd_rst_ctrl;
  56. void __iomem *crmu_usb2_ctrl;
  57. void __iomem *usb2h_strap_reg;
  58. struct ns2_phy_data *data;
  59. struct extcon_dev *edev;
  60. struct gpio_desc *vbus_gpiod;
  61. struct gpio_desc *id_gpiod;
  62. int id_irq;
  63. int vbus_irq;
  64. unsigned long debounce_jiffies;
  65. struct delayed_work wq_extcon;
  66. };
  67. struct ns2_phy_data {
  68. struct ns2_phy_driver *driver;
  69. struct phy *phy;
  70. int new_state;
  71. };
  72. static const unsigned int usb_extcon_cable[] = {
  73. EXTCON_USB,
  74. EXTCON_USB_HOST,
  75. EXTCON_NONE,
  76. };
  77. static inline int pll_lock_stat(u32 usb_reg, int reg_mask,
  78. struct ns2_phy_driver *driver)
  79. {
  80. int retry = PLL_LOCK_RETRY;
  81. u32 val;
  82. do {
  83. udelay(1);
  84. val = readl(driver->icfgdrd_regs + usb_reg);
  85. if (val & reg_mask)
  86. return 0;
  87. } while (--retry > 0);
  88. return -EBUSY;
  89. }
  90. static int ns2_drd_phy_init(struct phy *phy)
  91. {
  92. struct ns2_phy_data *data = phy_get_drvdata(phy);
  93. struct ns2_phy_driver *driver = data->driver;
  94. u32 val;
  95. val = readl(driver->icfgdrd_regs + ICFG_FSM_CTRL);
  96. if (data->new_state == EVT_HOST) {
  97. val &= ~DRD_DEVICE_MODE;
  98. val |= DRD_HOST_MODE;
  99. } else {
  100. val &= ~DRD_HOST_MODE;
  101. val |= DRD_DEVICE_MODE;
  102. }
  103. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  104. return 0;
  105. }
  106. static int ns2_drd_phy_poweroff(struct phy *phy)
  107. {
  108. struct ns2_phy_data *data = phy_get_drvdata(phy);
  109. struct ns2_phy_driver *driver = data->driver;
  110. u32 val;
  111. val = readl(driver->crmu_usb2_ctrl);
  112. val &= ~AFE_CORERDY_VDDC;
  113. writel(val, driver->crmu_usb2_ctrl);
  114. val = readl(driver->crmu_usb2_ctrl);
  115. val &= ~DRD_DEV_MODE;
  116. writel(val, driver->crmu_usb2_ctrl);
  117. /* Disable Host and Device Mode */
  118. val = readl(driver->icfgdrd_regs + ICFG_FSM_CTRL);
  119. val &= ~(DRD_HOST_MODE | DRD_DEVICE_MODE | ICFG_OFF_MODE);
  120. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  121. return 0;
  122. }
  123. static int ns2_drd_phy_poweron(struct phy *phy)
  124. {
  125. struct ns2_phy_data *data = phy_get_drvdata(phy);
  126. struct ns2_phy_driver *driver = data->driver;
  127. u32 extcon_event = data->new_state;
  128. int ret;
  129. u32 val;
  130. if (extcon_event == EVT_DEVICE) {
  131. writel(DRD_DEV_VAL, driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  132. val = readl(driver->idmdrd_rst_ctrl);
  133. val &= ~IDM_RST_BIT;
  134. writel(val, driver->idmdrd_rst_ctrl);
  135. val = readl(driver->crmu_usb2_ctrl);
  136. val |= (AFE_CORERDY_VDDC | DRD_DEV_MODE);
  137. writel(val, driver->crmu_usb2_ctrl);
  138. /* Bring PHY and PHY_PLL out of Reset */
  139. val = readl(driver->crmu_usb2_ctrl);
  140. val |= (PHY_PLL_RESETB | PHY_RESETB);
  141. writel(val, driver->crmu_usb2_ctrl);
  142. ret = pll_lock_stat(ICFG_MISC_STAT, PHY_PLL_LOCK, driver);
  143. if (ret < 0) {
  144. dev_err(&phy->dev, "Phy PLL lock failed\n");
  145. return ret;
  146. }
  147. } else {
  148. writel(DRD_HOST_VAL, driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  149. val = readl(driver->crmu_usb2_ctrl);
  150. val |= AFE_CORERDY_VDDC;
  151. writel(val, driver->crmu_usb2_ctrl);
  152. ret = pll_lock_stat(ICFG_MISC_STAT, PHY_PLL_LOCK, driver);
  153. if (ret < 0) {
  154. dev_err(&phy->dev, "Phy PLL lock failed\n");
  155. return ret;
  156. }
  157. val = readl(driver->idmdrd_rst_ctrl);
  158. val &= ~IDM_RST_BIT;
  159. writel(val, driver->idmdrd_rst_ctrl);
  160. /* port over current Polarity */
  161. val = readl(driver->usb2h_strap_reg);
  162. val |= OHCI_OVRCUR_POL;
  163. writel(val, driver->usb2h_strap_reg);
  164. }
  165. return 0;
  166. }
  167. static void connect_change(struct ns2_phy_driver *driver)
  168. {
  169. u32 extcon_event;
  170. u32 val;
  171. extcon_event = driver->data->new_state;
  172. val = readl(driver->icfgdrd_regs + ICFG_FSM_CTRL);
  173. switch (extcon_event) {
  174. case EVT_DEVICE:
  175. val &= ~(DRD_HOST_MODE | DRD_DEVICE_MODE);
  176. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  177. val = (val & ~DRD_HOST_MODE) | DRD_DEVICE_MODE;
  178. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  179. val = readl(driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  180. val |= ICFG_DEV_BIT;
  181. writel(val, driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  182. break;
  183. case EVT_HOST:
  184. val &= ~(DRD_HOST_MODE | DRD_DEVICE_MODE);
  185. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  186. val = (val & ~DRD_DEVICE_MODE) | DRD_HOST_MODE;
  187. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  188. val = readl(driver->usb2h_strap_reg);
  189. val |= OHCI_OVRCUR_POL;
  190. writel(val, driver->usb2h_strap_reg);
  191. val = readl(driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  192. val &= ~ICFG_DEV_BIT;
  193. writel(val, driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  194. break;
  195. default:
  196. pr_err("Invalid extcon event\n");
  197. break;
  198. }
  199. }
  200. static void extcon_work(struct work_struct *work)
  201. {
  202. struct ns2_phy_driver *driver;
  203. int vbus;
  204. int id;
  205. driver = container_of(to_delayed_work(work),
  206. struct ns2_phy_driver, wq_extcon);
  207. id = gpiod_get_value_cansleep(driver->id_gpiod);
  208. vbus = gpiod_get_value_cansleep(driver->vbus_gpiod);
  209. if (!id && vbus) { /* Host connected */
  210. extcon_set_state_sync(driver->edev, EXTCON_USB_HOST, true);
  211. pr_debug("Host cable connected\n");
  212. driver->data->new_state = EVT_HOST;
  213. connect_change(driver);
  214. } else if (id && !vbus) { /* Disconnected */
  215. extcon_set_state_sync(driver->edev, EXTCON_USB_HOST, false);
  216. extcon_set_state_sync(driver->edev, EXTCON_USB, false);
  217. pr_debug("Cable disconnected\n");
  218. } else if (id && vbus) { /* Device connected */
  219. extcon_set_state_sync(driver->edev, EXTCON_USB, true);
  220. pr_debug("Device cable connected\n");
  221. driver->data->new_state = EVT_DEVICE;
  222. connect_change(driver);
  223. }
  224. }
  225. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  226. {
  227. struct ns2_phy_driver *driver = dev_id;
  228. queue_delayed_work(system_power_efficient_wq, &driver->wq_extcon,
  229. driver->debounce_jiffies);
  230. return IRQ_HANDLED;
  231. }
  232. static struct phy_ops ops = {
  233. .init = ns2_drd_phy_init,
  234. .power_on = ns2_drd_phy_poweron,
  235. .power_off = ns2_drd_phy_poweroff,
  236. .owner = THIS_MODULE,
  237. };
  238. static const struct of_device_id ns2_drd_phy_dt_ids[] = {
  239. { .compatible = "brcm,ns2-drd-phy", },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(of, ns2_drd_phy_dt_ids);
  243. static int ns2_drd_phy_probe(struct platform_device *pdev)
  244. {
  245. struct phy_provider *phy_provider;
  246. struct device *dev = &pdev->dev;
  247. struct ns2_phy_driver *driver;
  248. struct ns2_phy_data *data;
  249. struct resource *res;
  250. int ret;
  251. u32 val;
  252. driver = devm_kzalloc(dev, sizeof(struct ns2_phy_driver),
  253. GFP_KERNEL);
  254. if (!driver)
  255. return -ENOMEM;
  256. driver->data = devm_kzalloc(dev, sizeof(struct ns2_phy_data),
  257. GFP_KERNEL);
  258. if (!driver->data)
  259. return -ENOMEM;
  260. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "icfg");
  261. driver->icfgdrd_regs = devm_ioremap_resource(dev, res);
  262. if (IS_ERR(driver->icfgdrd_regs))
  263. return PTR_ERR(driver->icfgdrd_regs);
  264. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rst-ctrl");
  265. driver->idmdrd_rst_ctrl = devm_ioremap_resource(dev, res);
  266. if (IS_ERR(driver->idmdrd_rst_ctrl))
  267. return PTR_ERR(driver->idmdrd_rst_ctrl);
  268. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "crmu-ctrl");
  269. driver->crmu_usb2_ctrl = devm_ioremap_resource(dev, res);
  270. if (IS_ERR(driver->crmu_usb2_ctrl))
  271. return PTR_ERR(driver->crmu_usb2_ctrl);
  272. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "usb2-strap");
  273. driver->usb2h_strap_reg = devm_ioremap_resource(dev, res);
  274. if (IS_ERR(driver->usb2h_strap_reg))
  275. return PTR_ERR(driver->usb2h_strap_reg);
  276. /* create extcon */
  277. driver->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
  278. if (IS_ERR(driver->id_gpiod)) {
  279. dev_err(dev, "failed to get ID GPIO\n");
  280. return PTR_ERR(driver->id_gpiod);
  281. }
  282. driver->vbus_gpiod = devm_gpiod_get(&pdev->dev, "vbus", GPIOD_IN);
  283. if (IS_ERR(driver->vbus_gpiod)) {
  284. dev_err(dev, "failed to get VBUS GPIO\n");
  285. return PTR_ERR(driver->vbus_gpiod);
  286. }
  287. driver->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
  288. if (IS_ERR(driver->edev)) {
  289. dev_err(dev, "failed to allocate extcon device\n");
  290. return -ENOMEM;
  291. }
  292. ret = devm_extcon_dev_register(dev, driver->edev);
  293. if (ret < 0) {
  294. dev_err(dev, "failed to register extcon device\n");
  295. return ret;
  296. }
  297. ret = gpiod_set_debounce(driver->id_gpiod, GPIO_DELAY * 1000);
  298. if (ret < 0)
  299. driver->debounce_jiffies = msecs_to_jiffies(GPIO_DELAY);
  300. INIT_DELAYED_WORK(&driver->wq_extcon, extcon_work);
  301. driver->id_irq = gpiod_to_irq(driver->id_gpiod);
  302. if (driver->id_irq < 0) {
  303. dev_err(dev, "failed to get ID IRQ\n");
  304. return driver->id_irq;
  305. }
  306. driver->vbus_irq = gpiod_to_irq(driver->vbus_gpiod);
  307. if (driver->vbus_irq < 0) {
  308. dev_err(dev, "failed to get ID IRQ\n");
  309. return driver->vbus_irq;
  310. }
  311. ret = devm_request_irq(dev, driver->id_irq, gpio_irq_handler,
  312. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  313. "usb_id", driver);
  314. if (ret < 0) {
  315. dev_err(dev, "failed to request handler for ID IRQ\n");
  316. return ret;
  317. }
  318. ret = devm_request_irq(dev, driver->vbus_irq, gpio_irq_handler,
  319. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  320. "usb_vbus", driver);
  321. if (ret < 0) {
  322. dev_err(dev, "failed to request handler for VBUS IRQ\n");
  323. return ret;
  324. }
  325. dev_set_drvdata(dev, driver);
  326. /* Shutdown all ports. They can be powered up as required */
  327. val = readl(driver->crmu_usb2_ctrl);
  328. val &= ~(AFE_CORERDY_VDDC | PHY_RESETB);
  329. writel(val, driver->crmu_usb2_ctrl);
  330. data = driver->data;
  331. data->phy = devm_phy_create(dev, dev->of_node, &ops);
  332. if (IS_ERR(data->phy)) {
  333. dev_err(dev, "Failed to create usb drd phy\n");
  334. return PTR_ERR(data->phy);
  335. }
  336. data->driver = driver;
  337. phy_set_drvdata(data->phy, data);
  338. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  339. if (IS_ERR(phy_provider)) {
  340. dev_err(dev, "Failed to register as phy provider\n");
  341. return PTR_ERR(phy_provider);
  342. }
  343. platform_set_drvdata(pdev, driver);
  344. dev_info(dev, "Registered NS2 DRD Phy device\n");
  345. queue_delayed_work(system_power_efficient_wq, &driver->wq_extcon,
  346. driver->debounce_jiffies);
  347. return 0;
  348. }
  349. static struct platform_driver ns2_drd_phy_driver = {
  350. .probe = ns2_drd_phy_probe,
  351. .driver = {
  352. .name = "bcm-ns2-usbphy",
  353. .of_match_table = of_match_ptr(ns2_drd_phy_dt_ids),
  354. },
  355. };
  356. module_platform_driver(ns2_drd_phy_driver);
  357. MODULE_ALIAS("platform:bcm-ns2-drd-phy");
  358. MODULE_AUTHOR("Broadcom");
  359. MODULE_DESCRIPTION("Broadcom NS2 USB2 PHY driver");
  360. MODULE_LICENSE("GPL v2");