phy-rcar-gen3-usb2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas R-Car Gen3 for USB2.0 PHY driver
  4. *
  5. * Copyright (C) 2015-2017 Renesas Electronics Corporation
  6. *
  7. * This is based on the phy-rcar-gen2 driver:
  8. * Copyright (C) 2014 Renesas Solutions Corp.
  9. * Copyright (C) 2014 Cogent Embedded, Inc.
  10. */
  11. #include <linux/extcon-provider.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_device.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/string.h>
  24. #include <linux/usb/of.h>
  25. #include <linux/workqueue.h>
  26. /******* USB2.0 Host registers (original offset is +0x200) *******/
  27. #define USB2_INT_ENABLE 0x000
  28. #define USB2_USBCTR 0x00c
  29. #define USB2_SPD_RSM_TIMSET 0x10c
  30. #define USB2_OC_TIMSET 0x110
  31. #define USB2_COMMCTRL 0x600
  32. #define USB2_OBINTSTA 0x604
  33. #define USB2_OBINTEN 0x608
  34. #define USB2_VBCTRL 0x60c
  35. #define USB2_LINECTRL1 0x610
  36. #define USB2_ADPCTRL 0x630
  37. /* INT_ENABLE */
  38. #define USB2_INT_ENABLE_UCOM_INTEN BIT(3)
  39. #define USB2_INT_ENABLE_USBH_INTB_EN BIT(2) /* For EHCI */
  40. #define USB2_INT_ENABLE_USBH_INTA_EN BIT(1) /* For OHCI */
  41. /* USBCTR */
  42. #define USB2_USBCTR_DIRPD BIT(2)
  43. #define USB2_USBCTR_PLL_RST BIT(1)
  44. /* SPD_RSM_TIMSET */
  45. #define USB2_SPD_RSM_TIMSET_INIT 0x014e029b
  46. /* OC_TIMSET */
  47. #define USB2_OC_TIMSET_INIT 0x000209ab
  48. /* COMMCTRL */
  49. #define USB2_COMMCTRL_OTG_PERI BIT(31) /* 1 = Peripheral mode */
  50. /* OBINTSTA and OBINTEN */
  51. #define USB2_OBINT_SESSVLDCHG BIT(12)
  52. #define USB2_OBINT_IDDIGCHG BIT(11)
  53. #define USB2_OBINT_BITS (USB2_OBINT_SESSVLDCHG | \
  54. USB2_OBINT_IDDIGCHG)
  55. /* VBCTRL */
  56. #define USB2_VBCTRL_OCCLREN BIT(16)
  57. #define USB2_VBCTRL_DRVVBUSSEL BIT(8)
  58. #define USB2_VBCTRL_VBOUT BIT(0)
  59. /* LINECTRL1 */
  60. #define USB2_LINECTRL1_DPRPD_EN BIT(19)
  61. #define USB2_LINECTRL1_DP_RPD BIT(18)
  62. #define USB2_LINECTRL1_DMRPD_EN BIT(17)
  63. #define USB2_LINECTRL1_DM_RPD BIT(16)
  64. #define USB2_LINECTRL1_OPMODE_NODRV BIT(6)
  65. /* ADPCTRL */
  66. #define USB2_ADPCTRL_OTGSESSVLD BIT(20)
  67. #define USB2_ADPCTRL_IDDIG BIT(19)
  68. #define USB2_ADPCTRL_IDPULLUP BIT(5) /* 1 = ID sampling is enabled */
  69. #define USB2_ADPCTRL_DRVVBUS BIT(4)
  70. /* RZ/G2L specific */
  71. #define USB2_OBINT_IDCHG_EN BIT(0)
  72. #define USB2_LINECTRL1_USB2_IDMON BIT(0)
  73. #define NUM_OF_PHYS 4
  74. enum rcar_gen3_phy_index {
  75. PHY_INDEX_BOTH_HC,
  76. PHY_INDEX_OHCI,
  77. PHY_INDEX_EHCI,
  78. PHY_INDEX_HSUSB
  79. };
  80. static const u32 rcar_gen3_int_enable[NUM_OF_PHYS] = {
  81. USB2_INT_ENABLE_USBH_INTB_EN | USB2_INT_ENABLE_USBH_INTA_EN,
  82. USB2_INT_ENABLE_USBH_INTA_EN,
  83. USB2_INT_ENABLE_USBH_INTB_EN,
  84. 0
  85. };
  86. struct rcar_gen3_phy {
  87. struct phy *phy;
  88. struct rcar_gen3_chan *ch;
  89. u32 int_enable_bits;
  90. bool initialized;
  91. bool otg_initialized;
  92. bool powered;
  93. };
  94. struct rcar_gen3_chan {
  95. void __iomem *base;
  96. struct device *dev; /* platform_device's device */
  97. struct extcon_dev *extcon;
  98. struct rcar_gen3_phy rphys[NUM_OF_PHYS];
  99. struct regulator *vbus;
  100. struct work_struct work;
  101. struct mutex lock; /* protects rphys[...].powered */
  102. enum usb_dr_mode dr_mode;
  103. int irq;
  104. u32 obint_enable_bits;
  105. bool extcon_host;
  106. bool is_otg_channel;
  107. bool uses_otg_pins;
  108. bool soc_no_adp_ctrl;
  109. };
  110. struct rcar_gen3_phy_drv_data {
  111. const struct phy_ops *phy_usb2_ops;
  112. bool no_adp_ctrl;
  113. };
  114. /*
  115. * Combination about is_otg_channel and uses_otg_pins:
  116. *
  117. * Parameters || Behaviors
  118. * is_otg_channel | uses_otg_pins || irqs | role sysfs
  119. * ---------------------+---------------++--------------+------------
  120. * true | true || enabled | enabled
  121. * true | false || disabled | enabled
  122. * false | any || disabled | disabled
  123. */
  124. static void rcar_gen3_phy_usb2_work(struct work_struct *work)
  125. {
  126. struct rcar_gen3_chan *ch = container_of(work, struct rcar_gen3_chan,
  127. work);
  128. if (ch->extcon_host) {
  129. extcon_set_state_sync(ch->extcon, EXTCON_USB_HOST, true);
  130. extcon_set_state_sync(ch->extcon, EXTCON_USB, false);
  131. } else {
  132. extcon_set_state_sync(ch->extcon, EXTCON_USB_HOST, false);
  133. extcon_set_state_sync(ch->extcon, EXTCON_USB, true);
  134. }
  135. }
  136. static void rcar_gen3_set_host_mode(struct rcar_gen3_chan *ch, int host)
  137. {
  138. void __iomem *usb2_base = ch->base;
  139. u32 val = readl(usb2_base + USB2_COMMCTRL);
  140. dev_vdbg(ch->dev, "%s: %08x, %d\n", __func__, val, host);
  141. if (host)
  142. val &= ~USB2_COMMCTRL_OTG_PERI;
  143. else
  144. val |= USB2_COMMCTRL_OTG_PERI;
  145. writel(val, usb2_base + USB2_COMMCTRL);
  146. }
  147. static void rcar_gen3_set_linectrl(struct rcar_gen3_chan *ch, int dp, int dm)
  148. {
  149. void __iomem *usb2_base = ch->base;
  150. u32 val = readl(usb2_base + USB2_LINECTRL1);
  151. dev_vdbg(ch->dev, "%s: %08x, %d, %d\n", __func__, val, dp, dm);
  152. val &= ~(USB2_LINECTRL1_DP_RPD | USB2_LINECTRL1_DM_RPD);
  153. if (dp)
  154. val |= USB2_LINECTRL1_DP_RPD;
  155. if (dm)
  156. val |= USB2_LINECTRL1_DM_RPD;
  157. writel(val, usb2_base + USB2_LINECTRL1);
  158. }
  159. static void rcar_gen3_enable_vbus_ctrl(struct rcar_gen3_chan *ch, int vbus)
  160. {
  161. void __iomem *usb2_base = ch->base;
  162. u32 vbus_ctrl_reg = USB2_ADPCTRL;
  163. u32 vbus_ctrl_val = USB2_ADPCTRL_DRVVBUS;
  164. u32 val;
  165. dev_vdbg(ch->dev, "%s: %08x, %d\n", __func__, val, vbus);
  166. if (ch->soc_no_adp_ctrl) {
  167. vbus_ctrl_reg = USB2_VBCTRL;
  168. vbus_ctrl_val = USB2_VBCTRL_VBOUT;
  169. }
  170. val = readl(usb2_base + vbus_ctrl_reg);
  171. if (vbus)
  172. val |= vbus_ctrl_val;
  173. else
  174. val &= ~vbus_ctrl_val;
  175. writel(val, usb2_base + vbus_ctrl_reg);
  176. }
  177. static void rcar_gen3_control_otg_irq(struct rcar_gen3_chan *ch, int enable)
  178. {
  179. void __iomem *usb2_base = ch->base;
  180. u32 val = readl(usb2_base + USB2_OBINTEN);
  181. if (ch->uses_otg_pins && enable)
  182. val |= ch->obint_enable_bits;
  183. else
  184. val &= ~ch->obint_enable_bits;
  185. writel(val, usb2_base + USB2_OBINTEN);
  186. }
  187. static void rcar_gen3_init_for_host(struct rcar_gen3_chan *ch)
  188. {
  189. rcar_gen3_set_linectrl(ch, 1, 1);
  190. rcar_gen3_set_host_mode(ch, 1);
  191. rcar_gen3_enable_vbus_ctrl(ch, 1);
  192. ch->extcon_host = true;
  193. schedule_work(&ch->work);
  194. }
  195. static void rcar_gen3_init_for_peri(struct rcar_gen3_chan *ch)
  196. {
  197. rcar_gen3_set_linectrl(ch, 0, 1);
  198. rcar_gen3_set_host_mode(ch, 0);
  199. rcar_gen3_enable_vbus_ctrl(ch, 0);
  200. ch->extcon_host = false;
  201. schedule_work(&ch->work);
  202. }
  203. static void rcar_gen3_init_for_b_host(struct rcar_gen3_chan *ch)
  204. {
  205. void __iomem *usb2_base = ch->base;
  206. u32 val;
  207. val = readl(usb2_base + USB2_LINECTRL1);
  208. writel(val | USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1);
  209. rcar_gen3_set_linectrl(ch, 1, 1);
  210. rcar_gen3_set_host_mode(ch, 1);
  211. rcar_gen3_enable_vbus_ctrl(ch, 0);
  212. val = readl(usb2_base + USB2_LINECTRL1);
  213. writel(val & ~USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1);
  214. }
  215. static void rcar_gen3_init_for_a_peri(struct rcar_gen3_chan *ch)
  216. {
  217. rcar_gen3_set_linectrl(ch, 0, 1);
  218. rcar_gen3_set_host_mode(ch, 0);
  219. rcar_gen3_enable_vbus_ctrl(ch, 1);
  220. }
  221. static void rcar_gen3_init_from_a_peri_to_a_host(struct rcar_gen3_chan *ch)
  222. {
  223. rcar_gen3_control_otg_irq(ch, 0);
  224. rcar_gen3_enable_vbus_ctrl(ch, 1);
  225. rcar_gen3_init_for_host(ch);
  226. rcar_gen3_control_otg_irq(ch, 1);
  227. }
  228. static bool rcar_gen3_check_id(struct rcar_gen3_chan *ch)
  229. {
  230. if (!ch->uses_otg_pins)
  231. return (ch->dr_mode == USB_DR_MODE_HOST) ? false : true;
  232. if (ch->soc_no_adp_ctrl)
  233. return !!(readl(ch->base + USB2_LINECTRL1) & USB2_LINECTRL1_USB2_IDMON);
  234. return !!(readl(ch->base + USB2_ADPCTRL) & USB2_ADPCTRL_IDDIG);
  235. }
  236. static void rcar_gen3_device_recognition(struct rcar_gen3_chan *ch)
  237. {
  238. if (!rcar_gen3_check_id(ch))
  239. rcar_gen3_init_for_host(ch);
  240. else
  241. rcar_gen3_init_for_peri(ch);
  242. }
  243. static bool rcar_gen3_is_host(struct rcar_gen3_chan *ch)
  244. {
  245. return !(readl(ch->base + USB2_COMMCTRL) & USB2_COMMCTRL_OTG_PERI);
  246. }
  247. static enum phy_mode rcar_gen3_get_phy_mode(struct rcar_gen3_chan *ch)
  248. {
  249. if (rcar_gen3_is_host(ch))
  250. return PHY_MODE_USB_HOST;
  251. return PHY_MODE_USB_DEVICE;
  252. }
  253. static bool rcar_gen3_is_any_rphy_initialized(struct rcar_gen3_chan *ch)
  254. {
  255. int i;
  256. for (i = 0; i < NUM_OF_PHYS; i++) {
  257. if (ch->rphys[i].initialized)
  258. return true;
  259. }
  260. return false;
  261. }
  262. static bool rcar_gen3_needs_init_otg(struct rcar_gen3_chan *ch)
  263. {
  264. int i;
  265. for (i = 0; i < NUM_OF_PHYS; i++) {
  266. if (ch->rphys[i].otg_initialized)
  267. return false;
  268. }
  269. return true;
  270. }
  271. static bool rcar_gen3_are_all_rphys_power_off(struct rcar_gen3_chan *ch)
  272. {
  273. int i;
  274. for (i = 0; i < NUM_OF_PHYS; i++) {
  275. if (ch->rphys[i].powered)
  276. return false;
  277. }
  278. return true;
  279. }
  280. static ssize_t role_store(struct device *dev, struct device_attribute *attr,
  281. const char *buf, size_t count)
  282. {
  283. struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
  284. bool is_b_device;
  285. enum phy_mode cur_mode, new_mode;
  286. if (!ch->is_otg_channel || !rcar_gen3_is_any_rphy_initialized(ch))
  287. return -EIO;
  288. if (sysfs_streq(buf, "host"))
  289. new_mode = PHY_MODE_USB_HOST;
  290. else if (sysfs_streq(buf, "peripheral"))
  291. new_mode = PHY_MODE_USB_DEVICE;
  292. else
  293. return -EINVAL;
  294. /* is_b_device: true is B-Device. false is A-Device. */
  295. is_b_device = rcar_gen3_check_id(ch);
  296. cur_mode = rcar_gen3_get_phy_mode(ch);
  297. /* If current and new mode is the same, this returns the error */
  298. if (cur_mode == new_mode)
  299. return -EINVAL;
  300. if (new_mode == PHY_MODE_USB_HOST) { /* And is_host must be false */
  301. if (!is_b_device) /* A-Peripheral */
  302. rcar_gen3_init_from_a_peri_to_a_host(ch);
  303. else /* B-Peripheral */
  304. rcar_gen3_init_for_b_host(ch);
  305. } else { /* And is_host must be true */
  306. if (!is_b_device) /* A-Host */
  307. rcar_gen3_init_for_a_peri(ch);
  308. else /* B-Host */
  309. rcar_gen3_init_for_peri(ch);
  310. }
  311. return count;
  312. }
  313. static ssize_t role_show(struct device *dev, struct device_attribute *attr,
  314. char *buf)
  315. {
  316. struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
  317. if (!ch->is_otg_channel || !rcar_gen3_is_any_rphy_initialized(ch))
  318. return -EIO;
  319. return sprintf(buf, "%s\n", rcar_gen3_is_host(ch) ? "host" :
  320. "peripheral");
  321. }
  322. static DEVICE_ATTR_RW(role);
  323. static void rcar_gen3_init_otg(struct rcar_gen3_chan *ch)
  324. {
  325. void __iomem *usb2_base = ch->base;
  326. u32 val;
  327. /* Should not use functions of read-modify-write a register */
  328. val = readl(usb2_base + USB2_LINECTRL1);
  329. val = (val & ~USB2_LINECTRL1_DP_RPD) | USB2_LINECTRL1_DPRPD_EN |
  330. USB2_LINECTRL1_DMRPD_EN | USB2_LINECTRL1_DM_RPD;
  331. writel(val, usb2_base + USB2_LINECTRL1);
  332. if (!ch->soc_no_adp_ctrl) {
  333. val = readl(usb2_base + USB2_VBCTRL);
  334. val &= ~USB2_VBCTRL_OCCLREN;
  335. writel(val | USB2_VBCTRL_DRVVBUSSEL, usb2_base + USB2_VBCTRL);
  336. val = readl(usb2_base + USB2_ADPCTRL);
  337. writel(val | USB2_ADPCTRL_IDPULLUP, usb2_base + USB2_ADPCTRL);
  338. }
  339. msleep(20);
  340. writel(0xffffffff, usb2_base + USB2_OBINTSTA);
  341. writel(ch->obint_enable_bits, usb2_base + USB2_OBINTEN);
  342. rcar_gen3_device_recognition(ch);
  343. }
  344. static irqreturn_t rcar_gen3_phy_usb2_irq(int irq, void *_ch)
  345. {
  346. struct rcar_gen3_chan *ch = _ch;
  347. void __iomem *usb2_base = ch->base;
  348. u32 status = readl(usb2_base + USB2_OBINTSTA);
  349. irqreturn_t ret = IRQ_NONE;
  350. if (status & ch->obint_enable_bits) {
  351. dev_vdbg(ch->dev, "%s: %08x\n", __func__, status);
  352. writel(ch->obint_enable_bits, usb2_base + USB2_OBINTSTA);
  353. rcar_gen3_device_recognition(ch);
  354. ret = IRQ_HANDLED;
  355. }
  356. return ret;
  357. }
  358. static int rcar_gen3_phy_usb2_init(struct phy *p)
  359. {
  360. struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
  361. struct rcar_gen3_chan *channel = rphy->ch;
  362. void __iomem *usb2_base = channel->base;
  363. u32 val;
  364. int ret;
  365. if (!rcar_gen3_is_any_rphy_initialized(channel) && channel->irq >= 0) {
  366. INIT_WORK(&channel->work, rcar_gen3_phy_usb2_work);
  367. ret = request_irq(channel->irq, rcar_gen3_phy_usb2_irq,
  368. IRQF_SHARED, dev_name(channel->dev), channel);
  369. if (ret < 0) {
  370. dev_err(channel->dev, "No irq handler (%d)\n", channel->irq);
  371. return ret;
  372. }
  373. }
  374. /* Initialize USB2 part */
  375. val = readl(usb2_base + USB2_INT_ENABLE);
  376. val |= USB2_INT_ENABLE_UCOM_INTEN | rphy->int_enable_bits;
  377. writel(val, usb2_base + USB2_INT_ENABLE);
  378. writel(USB2_SPD_RSM_TIMSET_INIT, usb2_base + USB2_SPD_RSM_TIMSET);
  379. writel(USB2_OC_TIMSET_INIT, usb2_base + USB2_OC_TIMSET);
  380. /* Initialize otg part */
  381. if (channel->is_otg_channel) {
  382. if (rcar_gen3_needs_init_otg(channel))
  383. rcar_gen3_init_otg(channel);
  384. rphy->otg_initialized = true;
  385. }
  386. rphy->initialized = true;
  387. return 0;
  388. }
  389. static int rcar_gen3_phy_usb2_exit(struct phy *p)
  390. {
  391. struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
  392. struct rcar_gen3_chan *channel = rphy->ch;
  393. void __iomem *usb2_base = channel->base;
  394. u32 val;
  395. rphy->initialized = false;
  396. if (channel->is_otg_channel)
  397. rphy->otg_initialized = false;
  398. val = readl(usb2_base + USB2_INT_ENABLE);
  399. val &= ~rphy->int_enable_bits;
  400. if (!rcar_gen3_is_any_rphy_initialized(channel))
  401. val &= ~USB2_INT_ENABLE_UCOM_INTEN;
  402. writel(val, usb2_base + USB2_INT_ENABLE);
  403. if (channel->irq >= 0 && !rcar_gen3_is_any_rphy_initialized(channel))
  404. free_irq(channel->irq, channel);
  405. return 0;
  406. }
  407. static int rcar_gen3_phy_usb2_power_on(struct phy *p)
  408. {
  409. struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
  410. struct rcar_gen3_chan *channel = rphy->ch;
  411. void __iomem *usb2_base = channel->base;
  412. u32 val;
  413. int ret = 0;
  414. mutex_lock(&channel->lock);
  415. if (!rcar_gen3_are_all_rphys_power_off(channel))
  416. goto out;
  417. if (channel->vbus) {
  418. ret = regulator_enable(channel->vbus);
  419. if (ret)
  420. goto out;
  421. }
  422. val = readl(usb2_base + USB2_USBCTR);
  423. val |= USB2_USBCTR_PLL_RST;
  424. writel(val, usb2_base + USB2_USBCTR);
  425. val &= ~USB2_USBCTR_PLL_RST;
  426. writel(val, usb2_base + USB2_USBCTR);
  427. out:
  428. /* The powered flag should be set for any other phys anyway */
  429. rphy->powered = true;
  430. mutex_unlock(&channel->lock);
  431. return 0;
  432. }
  433. static int rcar_gen3_phy_usb2_power_off(struct phy *p)
  434. {
  435. struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
  436. struct rcar_gen3_chan *channel = rphy->ch;
  437. int ret = 0;
  438. mutex_lock(&channel->lock);
  439. rphy->powered = false;
  440. if (!rcar_gen3_are_all_rphys_power_off(channel))
  441. goto out;
  442. if (channel->vbus)
  443. ret = regulator_disable(channel->vbus);
  444. out:
  445. mutex_unlock(&channel->lock);
  446. return ret;
  447. }
  448. static const struct phy_ops rcar_gen3_phy_usb2_ops = {
  449. .init = rcar_gen3_phy_usb2_init,
  450. .exit = rcar_gen3_phy_usb2_exit,
  451. .power_on = rcar_gen3_phy_usb2_power_on,
  452. .power_off = rcar_gen3_phy_usb2_power_off,
  453. .owner = THIS_MODULE,
  454. };
  455. static const struct phy_ops rz_g1c_phy_usb2_ops = {
  456. .init = rcar_gen3_phy_usb2_init,
  457. .exit = rcar_gen3_phy_usb2_exit,
  458. .owner = THIS_MODULE,
  459. };
  460. static const struct rcar_gen3_phy_drv_data rcar_gen3_phy_usb2_data = {
  461. .phy_usb2_ops = &rcar_gen3_phy_usb2_ops,
  462. .no_adp_ctrl = false,
  463. };
  464. static const struct rcar_gen3_phy_drv_data rz_g1c_phy_usb2_data = {
  465. .phy_usb2_ops = &rz_g1c_phy_usb2_ops,
  466. .no_adp_ctrl = false,
  467. };
  468. static const struct rcar_gen3_phy_drv_data rz_g2l_phy_usb2_data = {
  469. .phy_usb2_ops = &rcar_gen3_phy_usb2_ops,
  470. .no_adp_ctrl = true,
  471. };
  472. static const struct of_device_id rcar_gen3_phy_usb2_match_table[] = {
  473. {
  474. .compatible = "renesas,usb2-phy-r8a77470",
  475. .data = &rz_g1c_phy_usb2_data,
  476. },
  477. {
  478. .compatible = "renesas,usb2-phy-r8a7795",
  479. .data = &rcar_gen3_phy_usb2_data,
  480. },
  481. {
  482. .compatible = "renesas,usb2-phy-r8a7796",
  483. .data = &rcar_gen3_phy_usb2_data,
  484. },
  485. {
  486. .compatible = "renesas,usb2-phy-r8a77965",
  487. .data = &rcar_gen3_phy_usb2_data,
  488. },
  489. {
  490. .compatible = "renesas,rzg2l-usb2-phy",
  491. .data = &rz_g2l_phy_usb2_data,
  492. },
  493. {
  494. .compatible = "renesas,rcar-gen3-usb2-phy",
  495. .data = &rcar_gen3_phy_usb2_data,
  496. },
  497. { /* sentinel */ },
  498. };
  499. MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb2_match_table);
  500. static const unsigned int rcar_gen3_phy_cable[] = {
  501. EXTCON_USB,
  502. EXTCON_USB_HOST,
  503. EXTCON_NONE,
  504. };
  505. static struct phy *rcar_gen3_phy_usb2_xlate(struct device *dev,
  506. struct of_phandle_args *args)
  507. {
  508. struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
  509. if (args->args_count == 0) /* For old version dts */
  510. return ch->rphys[PHY_INDEX_BOTH_HC].phy;
  511. else if (args->args_count > 1) /* Prevent invalid args count */
  512. return ERR_PTR(-ENODEV);
  513. if (args->args[0] >= NUM_OF_PHYS)
  514. return ERR_PTR(-ENODEV);
  515. return ch->rphys[args->args[0]].phy;
  516. }
  517. static enum usb_dr_mode rcar_gen3_get_dr_mode(struct device_node *np)
  518. {
  519. enum usb_dr_mode candidate = USB_DR_MODE_UNKNOWN;
  520. int i;
  521. /*
  522. * If one of device nodes has other dr_mode except UNKNOWN,
  523. * this function returns UNKNOWN. To achieve backward compatibility,
  524. * this loop starts the index as 0.
  525. */
  526. for (i = 0; i < NUM_OF_PHYS; i++) {
  527. enum usb_dr_mode mode = of_usb_get_dr_mode_by_phy(np, i);
  528. if (mode != USB_DR_MODE_UNKNOWN) {
  529. if (candidate == USB_DR_MODE_UNKNOWN)
  530. candidate = mode;
  531. else if (candidate != mode)
  532. return USB_DR_MODE_UNKNOWN;
  533. }
  534. }
  535. return candidate;
  536. }
  537. static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
  538. {
  539. const struct rcar_gen3_phy_drv_data *phy_data;
  540. struct device *dev = &pdev->dev;
  541. struct rcar_gen3_chan *channel;
  542. struct phy_provider *provider;
  543. int ret = 0, i;
  544. if (!dev->of_node) {
  545. dev_err(dev, "This driver needs device tree\n");
  546. return -EINVAL;
  547. }
  548. channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
  549. if (!channel)
  550. return -ENOMEM;
  551. channel->base = devm_platform_ioremap_resource(pdev, 0);
  552. if (IS_ERR(channel->base))
  553. return PTR_ERR(channel->base);
  554. channel->obint_enable_bits = USB2_OBINT_BITS;
  555. /* get irq number here and request_irq for OTG in phy_init */
  556. channel->irq = platform_get_irq_optional(pdev, 0);
  557. channel->dr_mode = rcar_gen3_get_dr_mode(dev->of_node);
  558. if (channel->dr_mode != USB_DR_MODE_UNKNOWN) {
  559. int ret;
  560. channel->is_otg_channel = true;
  561. channel->uses_otg_pins = !of_property_read_bool(dev->of_node,
  562. "renesas,no-otg-pins");
  563. channel->extcon = devm_extcon_dev_allocate(dev,
  564. rcar_gen3_phy_cable);
  565. if (IS_ERR(channel->extcon))
  566. return PTR_ERR(channel->extcon);
  567. ret = devm_extcon_dev_register(dev, channel->extcon);
  568. if (ret < 0) {
  569. dev_err(dev, "Failed to register extcon\n");
  570. return ret;
  571. }
  572. }
  573. /*
  574. * devm_phy_create() will call pm_runtime_enable(&phy->dev);
  575. * And then, phy-core will manage runtime pm for this device.
  576. */
  577. pm_runtime_enable(dev);
  578. phy_data = of_device_get_match_data(dev);
  579. if (!phy_data) {
  580. ret = -EINVAL;
  581. goto error;
  582. }
  583. channel->soc_no_adp_ctrl = phy_data->no_adp_ctrl;
  584. if (phy_data->no_adp_ctrl)
  585. channel->obint_enable_bits = USB2_OBINT_IDCHG_EN;
  586. mutex_init(&channel->lock);
  587. for (i = 0; i < NUM_OF_PHYS; i++) {
  588. channel->rphys[i].phy = devm_phy_create(dev, NULL,
  589. phy_data->phy_usb2_ops);
  590. if (IS_ERR(channel->rphys[i].phy)) {
  591. dev_err(dev, "Failed to create USB2 PHY\n");
  592. ret = PTR_ERR(channel->rphys[i].phy);
  593. goto error;
  594. }
  595. channel->rphys[i].ch = channel;
  596. channel->rphys[i].int_enable_bits = rcar_gen3_int_enable[i];
  597. phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]);
  598. }
  599. channel->vbus = devm_regulator_get_optional(dev, "vbus");
  600. if (IS_ERR(channel->vbus)) {
  601. if (PTR_ERR(channel->vbus) == -EPROBE_DEFER) {
  602. ret = PTR_ERR(channel->vbus);
  603. goto error;
  604. }
  605. channel->vbus = NULL;
  606. }
  607. platform_set_drvdata(pdev, channel);
  608. channel->dev = dev;
  609. provider = devm_of_phy_provider_register(dev, rcar_gen3_phy_usb2_xlate);
  610. if (IS_ERR(provider)) {
  611. dev_err(dev, "Failed to register PHY provider\n");
  612. ret = PTR_ERR(provider);
  613. goto error;
  614. } else if (channel->is_otg_channel) {
  615. int ret;
  616. ret = device_create_file(dev, &dev_attr_role);
  617. if (ret < 0)
  618. goto error;
  619. }
  620. return 0;
  621. error:
  622. pm_runtime_disable(dev);
  623. return ret;
  624. }
  625. static int rcar_gen3_phy_usb2_remove(struct platform_device *pdev)
  626. {
  627. struct rcar_gen3_chan *channel = platform_get_drvdata(pdev);
  628. if (channel->is_otg_channel)
  629. device_remove_file(&pdev->dev, &dev_attr_role);
  630. pm_runtime_disable(&pdev->dev);
  631. return 0;
  632. };
  633. static struct platform_driver rcar_gen3_phy_usb2_driver = {
  634. .driver = {
  635. .name = "phy_rcar_gen3_usb2",
  636. .of_match_table = rcar_gen3_phy_usb2_match_table,
  637. },
  638. .probe = rcar_gen3_phy_usb2_probe,
  639. .remove = rcar_gen3_phy_usb2_remove,
  640. };
  641. module_platform_driver(rcar_gen3_phy_usb2_driver);
  642. MODULE_LICENSE("GPL v2");
  643. MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 2.0 PHY");
  644. MODULE_AUTHOR("Yoshihiro Shimoda <[email protected]>");