phy-sun4i-usb.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Allwinner sun4i USB phy driver
  4. *
  5. * Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com>
  6. *
  7. * Based on code from
  8. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  9. *
  10. * Modelled after: Samsung S5P/Exynos SoC series MIPI CSIS/DSIM DPHY driver
  11. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  12. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/extcon-provider.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/io.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/of.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/phy/phy.h>
  27. #include <linux/phy/phy-sun4i-usb.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/power_supply.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <linux/reset.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/usb/of.h>
  34. #include <linux/workqueue.h>
  35. #define REG_ISCR 0x00
  36. #define REG_PHYCTL_A10 0x04
  37. #define REG_PHYBIST 0x08
  38. #define REG_PHYTUNE 0x0c
  39. #define REG_PHYCTL_A33 0x10
  40. #define REG_PHY_OTGCTL 0x20
  41. #define REG_HCI_PHY_CTL 0x10
  42. #define PHYCTL_DATA BIT(7)
  43. #define OTGCTL_ROUTE_MUSB BIT(0)
  44. #define SUNXI_AHB_ICHR8_EN BIT(10)
  45. #define SUNXI_AHB_INCR4_BURST_EN BIT(9)
  46. #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
  47. #define SUNXI_ULPI_BYPASS_EN BIT(0)
  48. /* ISCR, Interface Status and Control bits */
  49. #define ISCR_ID_PULLUP_EN (1 << 17)
  50. #define ISCR_DPDM_PULLUP_EN (1 << 16)
  51. /* sunxi has the phy id/vbus pins not connected, so we use the force bits */
  52. #define ISCR_FORCE_ID_MASK (3 << 14)
  53. #define ISCR_FORCE_ID_LOW (2 << 14)
  54. #define ISCR_FORCE_ID_HIGH (3 << 14)
  55. #define ISCR_FORCE_VBUS_MASK (3 << 12)
  56. #define ISCR_FORCE_VBUS_LOW (2 << 12)
  57. #define ISCR_FORCE_VBUS_HIGH (3 << 12)
  58. /* Common Control Bits for Both PHYs */
  59. #define PHY_PLL_BW 0x03
  60. #define PHY_RES45_CAL_EN 0x0c
  61. /* Private Control Bits for Each PHY */
  62. #define PHY_TX_AMPLITUDE_TUNE 0x20
  63. #define PHY_TX_SLEWRATE_TUNE 0x22
  64. #define PHY_VBUSVALID_TH_SEL 0x25
  65. #define PHY_PULLUP_RES_SEL 0x27
  66. #define PHY_OTG_FUNC_EN 0x28
  67. #define PHY_VBUS_DET_EN 0x29
  68. #define PHY_DISCON_TH_SEL 0x2a
  69. #define PHY_SQUELCH_DETECT 0x3c
  70. /* A83T specific control bits for PHY0 */
  71. #define PHY_CTL_VBUSVLDEXT BIT(5)
  72. #define PHY_CTL_SIDDQ BIT(3)
  73. #define PHY_CTL_H3_SIDDQ BIT(1)
  74. /* A83T specific control bits for PHY2 HSIC */
  75. #define SUNXI_EHCI_HS_FORCE BIT(20)
  76. #define SUNXI_HSIC_CONNECT_DET BIT(17)
  77. #define SUNXI_HSIC_CONNECT_INT BIT(16)
  78. #define SUNXI_HSIC BIT(1)
  79. #define MAX_PHYS 4
  80. /*
  81. * Note do not raise the debounce time, we must report Vusb high within 100ms
  82. * otherwise we get Vbus errors
  83. */
  84. #define DEBOUNCE_TIME msecs_to_jiffies(50)
  85. #define POLL_TIME msecs_to_jiffies(250)
  86. struct sun4i_usb_phy_cfg {
  87. int num_phys;
  88. int hsic_index;
  89. u32 disc_thresh;
  90. u32 hci_phy_ctl_clear;
  91. u8 phyctl_offset;
  92. bool dedicated_clocks;
  93. bool phy0_dual_route;
  94. bool needs_phy2_siddq;
  95. bool siddq_in_base;
  96. bool poll_vbusen;
  97. int missing_phys;
  98. };
  99. struct sun4i_usb_phy_data {
  100. void __iomem *base;
  101. const struct sun4i_usb_phy_cfg *cfg;
  102. enum usb_dr_mode dr_mode;
  103. spinlock_t reg_lock; /* guard access to phyctl reg */
  104. struct sun4i_usb_phy {
  105. struct phy *phy;
  106. void __iomem *pmu;
  107. struct regulator *vbus;
  108. struct reset_control *reset;
  109. struct clk *clk;
  110. struct clk *clk2;
  111. bool regulator_on;
  112. int index;
  113. } phys[MAX_PHYS];
  114. /* phy0 / otg related variables */
  115. struct extcon_dev *extcon;
  116. bool phy0_init;
  117. struct gpio_desc *id_det_gpio;
  118. struct gpio_desc *vbus_det_gpio;
  119. struct power_supply *vbus_power_supply;
  120. struct notifier_block vbus_power_nb;
  121. bool vbus_power_nb_registered;
  122. bool force_session_end;
  123. int id_det_irq;
  124. int vbus_det_irq;
  125. int id_det;
  126. int vbus_det;
  127. struct delayed_work detect;
  128. };
  129. #define to_sun4i_usb_phy_data(phy) \
  130. container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
  131. static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
  132. {
  133. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  134. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  135. u32 iscr;
  136. iscr = readl(data->base + REG_ISCR);
  137. iscr &= ~clr;
  138. iscr |= set;
  139. writel(iscr, data->base + REG_ISCR);
  140. }
  141. static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
  142. {
  143. if (val)
  144. val = ISCR_FORCE_ID_HIGH;
  145. else
  146. val = ISCR_FORCE_ID_LOW;
  147. sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
  148. }
  149. static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
  150. {
  151. if (val)
  152. val = ISCR_FORCE_VBUS_HIGH;
  153. else
  154. val = ISCR_FORCE_VBUS_LOW;
  155. sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
  156. }
  157. static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
  158. int len)
  159. {
  160. struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
  161. u32 temp, usbc_bit = BIT(phy->index * 2);
  162. void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
  163. unsigned long flags;
  164. int i;
  165. spin_lock_irqsave(&phy_data->reg_lock, flags);
  166. if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
  167. /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
  168. writel(0, phyctl);
  169. }
  170. for (i = 0; i < len; i++) {
  171. temp = readl(phyctl);
  172. /* clear the address portion */
  173. temp &= ~(0xff << 8);
  174. /* set the address */
  175. temp |= ((addr + i) << 8);
  176. writel(temp, phyctl);
  177. /* set the data bit and clear usbc bit*/
  178. temp = readb(phyctl);
  179. if (data & 0x1)
  180. temp |= PHYCTL_DATA;
  181. else
  182. temp &= ~PHYCTL_DATA;
  183. temp &= ~usbc_bit;
  184. writeb(temp, phyctl);
  185. /* pulse usbc_bit */
  186. temp = readb(phyctl);
  187. temp |= usbc_bit;
  188. writeb(temp, phyctl);
  189. temp = readb(phyctl);
  190. temp &= ~usbc_bit;
  191. writeb(temp, phyctl);
  192. data >>= 1;
  193. }
  194. spin_unlock_irqrestore(&phy_data->reg_lock, flags);
  195. }
  196. static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
  197. {
  198. struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
  199. u32 bits, reg_value;
  200. if (!phy->pmu)
  201. return;
  202. bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
  203. SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
  204. /* A83T USB2 is HSIC */
  205. if (phy_data->cfg->hsic_index &&
  206. phy->index == phy_data->cfg->hsic_index)
  207. bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
  208. SUNXI_HSIC;
  209. reg_value = readl(phy->pmu);
  210. if (enable)
  211. reg_value |= bits;
  212. else
  213. reg_value &= ~bits;
  214. writel(reg_value, phy->pmu);
  215. }
  216. static int sun4i_usb_phy_init(struct phy *_phy)
  217. {
  218. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  219. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  220. int ret;
  221. u32 val;
  222. ret = clk_prepare_enable(phy->clk);
  223. if (ret)
  224. return ret;
  225. ret = clk_prepare_enable(phy->clk2);
  226. if (ret) {
  227. clk_disable_unprepare(phy->clk);
  228. return ret;
  229. }
  230. ret = reset_control_deassert(phy->reset);
  231. if (ret) {
  232. clk_disable_unprepare(phy->clk2);
  233. clk_disable_unprepare(phy->clk);
  234. return ret;
  235. }
  236. /* Some PHYs on some SoCs need the help of PHY2 to work. */
  237. if (data->cfg->needs_phy2_siddq && phy->index != 2) {
  238. struct sun4i_usb_phy *phy2 = &data->phys[2];
  239. ret = clk_prepare_enable(phy2->clk);
  240. if (ret) {
  241. reset_control_assert(phy->reset);
  242. clk_disable_unprepare(phy->clk2);
  243. clk_disable_unprepare(phy->clk);
  244. return ret;
  245. }
  246. ret = reset_control_deassert(phy2->reset);
  247. if (ret) {
  248. clk_disable_unprepare(phy2->clk);
  249. reset_control_assert(phy->reset);
  250. clk_disable_unprepare(phy->clk2);
  251. clk_disable_unprepare(phy->clk);
  252. return ret;
  253. }
  254. /*
  255. * This extra clock is just needed to access the
  256. * REG_HCI_PHY_CTL PMU register for PHY2.
  257. */
  258. ret = clk_prepare_enable(phy2->clk2);
  259. if (ret) {
  260. reset_control_assert(phy2->reset);
  261. clk_disable_unprepare(phy2->clk);
  262. reset_control_assert(phy->reset);
  263. clk_disable_unprepare(phy->clk2);
  264. clk_disable_unprepare(phy->clk);
  265. return ret;
  266. }
  267. if (phy2->pmu && data->cfg->hci_phy_ctl_clear) {
  268. val = readl(phy2->pmu + REG_HCI_PHY_CTL);
  269. val &= ~data->cfg->hci_phy_ctl_clear;
  270. writel(val, phy2->pmu + REG_HCI_PHY_CTL);
  271. }
  272. clk_disable_unprepare(phy->clk2);
  273. }
  274. if (phy->pmu && data->cfg->hci_phy_ctl_clear) {
  275. val = readl(phy->pmu + REG_HCI_PHY_CTL);
  276. val &= ~data->cfg->hci_phy_ctl_clear;
  277. writel(val, phy->pmu + REG_HCI_PHY_CTL);
  278. }
  279. if (data->cfg->siddq_in_base) {
  280. if (phy->index == 0) {
  281. val = readl(data->base + data->cfg->phyctl_offset);
  282. val |= PHY_CTL_VBUSVLDEXT;
  283. val &= ~PHY_CTL_SIDDQ;
  284. writel(val, data->base + data->cfg->phyctl_offset);
  285. }
  286. } else {
  287. /* Enable USB 45 Ohm resistor calibration */
  288. if (phy->index == 0)
  289. sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
  290. /* Adjust PHY's magnitude and rate */
  291. sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
  292. /* Disconnect threshold adjustment */
  293. sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
  294. data->cfg->disc_thresh, 2);
  295. }
  296. sun4i_usb_phy_passby(phy, 1);
  297. if (phy->index == 0) {
  298. data->phy0_init = true;
  299. /* Enable pull-ups */
  300. sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
  301. sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
  302. /* Force ISCR and cable state updates */
  303. data->id_det = -1;
  304. data->vbus_det = -1;
  305. queue_delayed_work(system_wq, &data->detect, 0);
  306. }
  307. return 0;
  308. }
  309. static int sun4i_usb_phy_exit(struct phy *_phy)
  310. {
  311. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  312. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  313. if (phy->index == 0) {
  314. if (data->cfg->siddq_in_base) {
  315. void __iomem *phyctl = data->base +
  316. data->cfg->phyctl_offset;
  317. writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
  318. }
  319. /* Disable pull-ups */
  320. sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
  321. sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
  322. data->phy0_init = false;
  323. }
  324. if (data->cfg->needs_phy2_siddq && phy->index != 2) {
  325. struct sun4i_usb_phy *phy2 = &data->phys[2];
  326. clk_disable_unprepare(phy2->clk);
  327. reset_control_assert(phy2->reset);
  328. }
  329. sun4i_usb_phy_passby(phy, 0);
  330. reset_control_assert(phy->reset);
  331. clk_disable_unprepare(phy->clk2);
  332. clk_disable_unprepare(phy->clk);
  333. return 0;
  334. }
  335. static int sun4i_usb_phy0_get_id_det(struct sun4i_usb_phy_data *data)
  336. {
  337. switch (data->dr_mode) {
  338. case USB_DR_MODE_OTG:
  339. if (data->id_det_gpio)
  340. return gpiod_get_value_cansleep(data->id_det_gpio);
  341. else
  342. return 1; /* Fallback to peripheral mode */
  343. case USB_DR_MODE_HOST:
  344. return 0;
  345. case USB_DR_MODE_PERIPHERAL:
  346. default:
  347. return 1;
  348. }
  349. }
  350. static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
  351. {
  352. if (data->vbus_det_gpio)
  353. return gpiod_get_value_cansleep(data->vbus_det_gpio);
  354. if (data->vbus_power_supply) {
  355. union power_supply_propval val;
  356. int r;
  357. r = power_supply_get_property(data->vbus_power_supply,
  358. POWER_SUPPLY_PROP_PRESENT, &val);
  359. if (r == 0)
  360. return val.intval;
  361. }
  362. /* Fallback: report vbus as high */
  363. return 1;
  364. }
  365. static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
  366. {
  367. return data->vbus_det_gpio || data->vbus_power_supply;
  368. }
  369. static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data)
  370. {
  371. if ((data->id_det_gpio && data->id_det_irq <= 0) ||
  372. (data->vbus_det_gpio && data->vbus_det_irq <= 0))
  373. return true;
  374. /*
  375. * The A31/A23/A33 companion pmics (AXP221/AXP223) do not
  376. * generate vbus change interrupts when the board is driving
  377. * vbus using the N_VBUSEN pin on the pmic, so we must poll
  378. * when using the pmic for vbus-det _and_ we're driving vbus.
  379. */
  380. if (data->cfg->poll_vbusen && data->vbus_power_supply &&
  381. data->phys[0].regulator_on)
  382. return true;
  383. return false;
  384. }
  385. static int sun4i_usb_phy_power_on(struct phy *_phy)
  386. {
  387. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  388. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  389. int ret;
  390. if (!phy->vbus || phy->regulator_on)
  391. return 0;
  392. /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
  393. if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
  394. data->vbus_det) {
  395. dev_warn(&_phy->dev, "External vbus detected, not enabling our own vbus\n");
  396. return 0;
  397. }
  398. ret = regulator_enable(phy->vbus);
  399. if (ret)
  400. return ret;
  401. phy->regulator_on = true;
  402. /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
  403. if (phy->index == 0 && sun4i_usb_phy0_poll(data))
  404. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  405. return 0;
  406. }
  407. static int sun4i_usb_phy_power_off(struct phy *_phy)
  408. {
  409. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  410. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  411. if (!phy->vbus || !phy->regulator_on)
  412. return 0;
  413. regulator_disable(phy->vbus);
  414. phy->regulator_on = false;
  415. /*
  416. * phy0 vbus typically slowly discharges, sometimes this causes the
  417. * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
  418. */
  419. if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
  420. mod_delayed_work(system_wq, &data->detect, POLL_TIME);
  421. return 0;
  422. }
  423. static int sun4i_usb_phy_set_mode(struct phy *_phy,
  424. enum phy_mode mode, int submode)
  425. {
  426. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  427. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  428. int new_mode;
  429. if (phy->index != 0) {
  430. if (mode == PHY_MODE_USB_HOST)
  431. return 0;
  432. return -EINVAL;
  433. }
  434. switch (mode) {
  435. case PHY_MODE_USB_HOST:
  436. new_mode = USB_DR_MODE_HOST;
  437. break;
  438. case PHY_MODE_USB_DEVICE:
  439. new_mode = USB_DR_MODE_PERIPHERAL;
  440. break;
  441. case PHY_MODE_USB_OTG:
  442. new_mode = USB_DR_MODE_OTG;
  443. break;
  444. default:
  445. return -EINVAL;
  446. }
  447. if (new_mode != data->dr_mode) {
  448. dev_info(&_phy->dev, "Changing dr_mode to %d\n", new_mode);
  449. data->dr_mode = new_mode;
  450. }
  451. data->id_det = -1; /* Force reprocessing of id */
  452. data->force_session_end = true;
  453. queue_delayed_work(system_wq, &data->detect, 0);
  454. return 0;
  455. }
  456. void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
  457. {
  458. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  459. sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
  460. }
  461. EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
  462. static const struct phy_ops sun4i_usb_phy_ops = {
  463. .init = sun4i_usb_phy_init,
  464. .exit = sun4i_usb_phy_exit,
  465. .power_on = sun4i_usb_phy_power_on,
  466. .power_off = sun4i_usb_phy_power_off,
  467. .set_mode = sun4i_usb_phy_set_mode,
  468. .owner = THIS_MODULE,
  469. };
  470. static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, int id_det)
  471. {
  472. u32 regval;
  473. regval = readl(data->base + REG_PHY_OTGCTL);
  474. if (id_det == 0) {
  475. /* Host mode. Route phy0 to EHCI/OHCI */
  476. regval &= ~OTGCTL_ROUTE_MUSB;
  477. } else {
  478. /* Peripheral mode. Route phy0 to MUSB */
  479. regval |= OTGCTL_ROUTE_MUSB;
  480. }
  481. writel(regval, data->base + REG_PHY_OTGCTL);
  482. }
  483. static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
  484. {
  485. struct sun4i_usb_phy_data *data =
  486. container_of(work, struct sun4i_usb_phy_data, detect.work);
  487. struct phy *phy0 = data->phys[0].phy;
  488. struct sun4i_usb_phy *phy;
  489. bool force_session_end, id_notify = false, vbus_notify = false;
  490. int id_det, vbus_det;
  491. if (!phy0)
  492. return;
  493. phy = phy_get_drvdata(phy0);
  494. id_det = sun4i_usb_phy0_get_id_det(data);
  495. vbus_det = sun4i_usb_phy0_get_vbus_det(data);
  496. mutex_lock(&phy0->mutex);
  497. if (!data->phy0_init) {
  498. mutex_unlock(&phy0->mutex);
  499. return;
  500. }
  501. force_session_end = data->force_session_end;
  502. data->force_session_end = false;
  503. if (id_det != data->id_det) {
  504. /* id-change, force session end if we've no vbus detection */
  505. if (data->dr_mode == USB_DR_MODE_OTG &&
  506. !sun4i_usb_phy0_have_vbus_det(data))
  507. force_session_end = true;
  508. /* When entering host mode (id = 0) force end the session now */
  509. if (force_session_end && id_det == 0) {
  510. sun4i_usb_phy0_set_vbus_detect(phy0, 0);
  511. msleep(200);
  512. sun4i_usb_phy0_set_vbus_detect(phy0, 1);
  513. }
  514. sun4i_usb_phy0_set_id_detect(phy0, id_det);
  515. data->id_det = id_det;
  516. id_notify = true;
  517. }
  518. if (vbus_det != data->vbus_det) {
  519. sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
  520. data->vbus_det = vbus_det;
  521. vbus_notify = true;
  522. }
  523. mutex_unlock(&phy0->mutex);
  524. if (id_notify) {
  525. extcon_set_state_sync(data->extcon, EXTCON_USB_HOST,
  526. !id_det);
  527. /* When leaving host mode force end the session here */
  528. if (force_session_end && id_det == 1) {
  529. mutex_lock(&phy0->mutex);
  530. sun4i_usb_phy0_set_vbus_detect(phy0, 0);
  531. msleep(1000);
  532. sun4i_usb_phy0_set_vbus_detect(phy0, 1);
  533. mutex_unlock(&phy0->mutex);
  534. }
  535. /* Enable PHY0 passby for host mode only. */
  536. sun4i_usb_phy_passby(phy, !id_det);
  537. /* Re-route PHY0 if necessary */
  538. if (data->cfg->phy0_dual_route)
  539. sun4i_usb_phy0_reroute(data, id_det);
  540. }
  541. if (vbus_notify)
  542. extcon_set_state_sync(data->extcon, EXTCON_USB, vbus_det);
  543. if (sun4i_usb_phy0_poll(data))
  544. queue_delayed_work(system_wq, &data->detect, POLL_TIME);
  545. }
  546. static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
  547. {
  548. struct sun4i_usb_phy_data *data = dev_id;
  549. /* vbus or id changed, let the pins settle and then scan them */
  550. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  551. return IRQ_HANDLED;
  552. }
  553. static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
  554. unsigned long val, void *v)
  555. {
  556. struct sun4i_usb_phy_data *data =
  557. container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
  558. struct power_supply *psy = v;
  559. /* Properties on the vbus_power_supply changed, scan vbus_det */
  560. if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
  561. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  562. return NOTIFY_OK;
  563. }
  564. static struct phy *sun4i_usb_phy_xlate(struct device *dev,
  565. const struct of_phandle_args *args)
  566. {
  567. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  568. if (args->args[0] >= data->cfg->num_phys)
  569. return ERR_PTR(-ENODEV);
  570. if (data->cfg->missing_phys & BIT(args->args[0]))
  571. return ERR_PTR(-ENODEV);
  572. return data->phys[args->args[0]].phy;
  573. }
  574. static void sun4i_usb_phy_remove(struct platform_device *pdev)
  575. {
  576. struct device *dev = &pdev->dev;
  577. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  578. if (data->vbus_power_nb_registered)
  579. power_supply_unreg_notifier(&data->vbus_power_nb);
  580. if (data->id_det_irq > 0)
  581. devm_free_irq(dev, data->id_det_irq, data);
  582. if (data->vbus_det_irq > 0)
  583. devm_free_irq(dev, data->vbus_det_irq, data);
  584. cancel_delayed_work_sync(&data->detect);
  585. }
  586. static const unsigned int sun4i_usb_phy0_cable[] = {
  587. EXTCON_USB,
  588. EXTCON_USB_HOST,
  589. EXTCON_NONE,
  590. };
  591. static int sun4i_usb_phy_probe(struct platform_device *pdev)
  592. {
  593. struct sun4i_usb_phy_data *data;
  594. struct device *dev = &pdev->dev;
  595. struct device_node *np = dev->of_node;
  596. struct phy_provider *phy_provider;
  597. int i, ret;
  598. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  599. if (!data)
  600. return -ENOMEM;
  601. spin_lock_init(&data->reg_lock);
  602. INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
  603. dev_set_drvdata(dev, data);
  604. data->cfg = of_device_get_match_data(dev);
  605. if (!data->cfg)
  606. return -EINVAL;
  607. data->base = devm_platform_ioremap_resource_byname(pdev, "phy_ctrl");
  608. if (IS_ERR(data->base))
  609. return PTR_ERR(data->base);
  610. data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
  611. GPIOD_IN);
  612. if (IS_ERR(data->id_det_gpio)) {
  613. dev_err(dev, "Couldn't request ID GPIO\n");
  614. return PTR_ERR(data->id_det_gpio);
  615. }
  616. data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
  617. GPIOD_IN);
  618. if (IS_ERR(data->vbus_det_gpio)) {
  619. dev_err(dev, "Couldn't request VBUS detect GPIO\n");
  620. return PTR_ERR(data->vbus_det_gpio);
  621. }
  622. if (of_property_present(np, "usb0_vbus_power-supply")) {
  623. data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
  624. "usb0_vbus_power-supply");
  625. if (IS_ERR(data->vbus_power_supply)) {
  626. dev_err(dev, "Couldn't get the VBUS power supply\n");
  627. return PTR_ERR(data->vbus_power_supply);
  628. }
  629. if (!data->vbus_power_supply)
  630. return -EPROBE_DEFER;
  631. }
  632. data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0);
  633. data->extcon = devm_extcon_dev_allocate(dev, sun4i_usb_phy0_cable);
  634. if (IS_ERR(data->extcon)) {
  635. dev_err(dev, "Couldn't allocate our extcon device\n");
  636. return PTR_ERR(data->extcon);
  637. }
  638. ret = devm_extcon_dev_register(dev, data->extcon);
  639. if (ret) {
  640. dev_err(dev, "failed to register extcon: %d\n", ret);
  641. return ret;
  642. }
  643. for (i = 0; i < data->cfg->num_phys; i++) {
  644. struct sun4i_usb_phy *phy = data->phys + i;
  645. char name[32];
  646. if (data->cfg->missing_phys & BIT(i))
  647. continue;
  648. snprintf(name, sizeof(name), "usb%d_vbus", i);
  649. phy->vbus = devm_regulator_get_optional(dev, name);
  650. if (IS_ERR(phy->vbus)) {
  651. if (PTR_ERR(phy->vbus) == -EPROBE_DEFER) {
  652. dev_err(dev,
  653. "Couldn't get regulator %s... Deferring probe\n",
  654. name);
  655. return -EPROBE_DEFER;
  656. }
  657. phy->vbus = NULL;
  658. }
  659. if (data->cfg->dedicated_clocks)
  660. snprintf(name, sizeof(name), "usb%d_phy", i);
  661. else
  662. strscpy(name, "usb_phy", sizeof(name));
  663. phy->clk = devm_clk_get(dev, name);
  664. if (IS_ERR(phy->clk)) {
  665. dev_err(dev, "failed to get clock %s\n", name);
  666. return PTR_ERR(phy->clk);
  667. }
  668. /* The first PHY is always tied to OTG, and never HSIC */
  669. if (data->cfg->hsic_index && i == data->cfg->hsic_index) {
  670. /* HSIC needs secondary clock */
  671. snprintf(name, sizeof(name), "usb%d_hsic_12M", i);
  672. phy->clk2 = devm_clk_get(dev, name);
  673. if (IS_ERR(phy->clk2)) {
  674. dev_err(dev, "failed to get clock %s\n", name);
  675. return PTR_ERR(phy->clk2);
  676. }
  677. } else {
  678. snprintf(name, sizeof(name), "pmu%d_clk", i);
  679. phy->clk2 = devm_clk_get_optional(dev, name);
  680. if (IS_ERR(phy->clk2)) {
  681. dev_err(dev, "failed to get clock %s\n", name);
  682. return PTR_ERR(phy->clk2);
  683. }
  684. }
  685. snprintf(name, sizeof(name), "usb%d_reset", i);
  686. phy->reset = devm_reset_control_get(dev, name);
  687. if (IS_ERR(phy->reset)) {
  688. dev_err(dev, "failed to get reset %s\n", name);
  689. return PTR_ERR(phy->reset);
  690. }
  691. if (i || data->cfg->phy0_dual_route) { /* No pmu for musb */
  692. snprintf(name, sizeof(name), "pmu%d", i);
  693. phy->pmu = devm_platform_ioremap_resource_byname(pdev, name);
  694. if (IS_ERR(phy->pmu))
  695. return PTR_ERR(phy->pmu);
  696. }
  697. phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
  698. if (IS_ERR(phy->phy)) {
  699. dev_err(dev, "failed to create PHY %d\n", i);
  700. return PTR_ERR(phy->phy);
  701. }
  702. phy->index = i;
  703. phy_set_drvdata(phy->phy, &data->phys[i]);
  704. }
  705. data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
  706. if (data->id_det_irq > 0) {
  707. ret = devm_request_irq(dev, data->id_det_irq,
  708. sun4i_usb_phy0_id_vbus_det_irq,
  709. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  710. "usb0-id-det", data);
  711. if (ret) {
  712. dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
  713. return ret;
  714. }
  715. }
  716. data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
  717. if (data->vbus_det_irq > 0) {
  718. ret = devm_request_irq(dev, data->vbus_det_irq,
  719. sun4i_usb_phy0_id_vbus_det_irq,
  720. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  721. "usb0-vbus-det", data);
  722. if (ret) {
  723. dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
  724. data->vbus_det_irq = -1;
  725. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  726. return ret;
  727. }
  728. }
  729. if (data->vbus_power_supply) {
  730. data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
  731. data->vbus_power_nb.priority = 0;
  732. ret = power_supply_reg_notifier(&data->vbus_power_nb);
  733. if (ret) {
  734. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  735. return ret;
  736. }
  737. data->vbus_power_nb_registered = true;
  738. }
  739. phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
  740. if (IS_ERR(phy_provider)) {
  741. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  742. return PTR_ERR(phy_provider);
  743. }
  744. dev_dbg(dev, "successfully loaded\n");
  745. return 0;
  746. }
  747. static const struct sun4i_usb_phy_cfg suniv_f1c100s_cfg = {
  748. .num_phys = 1,
  749. .disc_thresh = 3,
  750. .phyctl_offset = REG_PHYCTL_A10,
  751. .dedicated_clocks = true,
  752. };
  753. static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
  754. .num_phys = 3,
  755. .disc_thresh = 3,
  756. .phyctl_offset = REG_PHYCTL_A10,
  757. .dedicated_clocks = false,
  758. };
  759. static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
  760. .num_phys = 2,
  761. .disc_thresh = 2,
  762. .phyctl_offset = REG_PHYCTL_A10,
  763. .dedicated_clocks = false,
  764. };
  765. static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
  766. .num_phys = 3,
  767. .disc_thresh = 3,
  768. .phyctl_offset = REG_PHYCTL_A10,
  769. .dedicated_clocks = true,
  770. .poll_vbusen = true,
  771. };
  772. static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
  773. .num_phys = 3,
  774. .disc_thresh = 2,
  775. .phyctl_offset = REG_PHYCTL_A10,
  776. .dedicated_clocks = false,
  777. };
  778. static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
  779. .num_phys = 2,
  780. .disc_thresh = 3,
  781. .phyctl_offset = REG_PHYCTL_A10,
  782. .dedicated_clocks = true,
  783. .poll_vbusen = true,
  784. };
  785. static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
  786. .num_phys = 2,
  787. .disc_thresh = 3,
  788. .phyctl_offset = REG_PHYCTL_A33,
  789. .dedicated_clocks = true,
  790. .poll_vbusen = true,
  791. };
  792. static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
  793. .num_phys = 3,
  794. .hsic_index = 2,
  795. .phyctl_offset = REG_PHYCTL_A33,
  796. .dedicated_clocks = true,
  797. .siddq_in_base = true,
  798. };
  799. static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
  800. .num_phys = 4,
  801. .disc_thresh = 3,
  802. .phyctl_offset = REG_PHYCTL_A33,
  803. .dedicated_clocks = true,
  804. .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
  805. .phy0_dual_route = true,
  806. };
  807. static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
  808. .num_phys = 3,
  809. .disc_thresh = 3,
  810. .phyctl_offset = REG_PHYCTL_A33,
  811. .dedicated_clocks = true,
  812. .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
  813. .phy0_dual_route = true,
  814. };
  815. static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
  816. .num_phys = 1,
  817. .disc_thresh = 3,
  818. .phyctl_offset = REG_PHYCTL_A33,
  819. .dedicated_clocks = true,
  820. .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
  821. .phy0_dual_route = true,
  822. };
  823. static const struct sun4i_usb_phy_cfg sun20i_d1_cfg = {
  824. .num_phys = 2,
  825. .phyctl_offset = REG_PHYCTL_A33,
  826. .dedicated_clocks = true,
  827. .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
  828. .phy0_dual_route = true,
  829. .siddq_in_base = true,
  830. };
  831. static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
  832. .num_phys = 2,
  833. .disc_thresh = 3,
  834. .phyctl_offset = REG_PHYCTL_A33,
  835. .dedicated_clocks = true,
  836. .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
  837. .phy0_dual_route = true,
  838. };
  839. static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
  840. .num_phys = 4,
  841. .phyctl_offset = REG_PHYCTL_A33,
  842. .dedicated_clocks = true,
  843. .phy0_dual_route = true,
  844. .missing_phys = BIT(1) | BIT(2),
  845. .siddq_in_base = true,
  846. };
  847. static const struct sun4i_usb_phy_cfg sun50i_h616_cfg = {
  848. .num_phys = 4,
  849. .disc_thresh = 3,
  850. .phyctl_offset = REG_PHYCTL_A33,
  851. .dedicated_clocks = true,
  852. .phy0_dual_route = true,
  853. .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
  854. .needs_phy2_siddq = true,
  855. .siddq_in_base = true,
  856. };
  857. static const struct of_device_id sun4i_usb_phy_of_match[] = {
  858. { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
  859. { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
  860. { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
  861. { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
  862. { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
  863. { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
  864. { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = &sun8i_a83t_cfg },
  865. { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
  866. { .compatible = "allwinner,sun8i-r40-usb-phy", .data = &sun8i_r40_cfg },
  867. { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = &sun8i_v3s_cfg },
  868. { .compatible = "allwinner,sun20i-d1-usb-phy", .data = &sun20i_d1_cfg },
  869. { .compatible = "allwinner,sun50i-a64-usb-phy",
  870. .data = &sun50i_a64_cfg},
  871. { .compatible = "allwinner,sun50i-h6-usb-phy", .data = &sun50i_h6_cfg },
  872. { .compatible = "allwinner,sun50i-h616-usb-phy", .data = &sun50i_h616_cfg },
  873. { .compatible = "allwinner,suniv-f1c100s-usb-phy",
  874. .data = &suniv_f1c100s_cfg },
  875. { },
  876. };
  877. MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
  878. static struct platform_driver sun4i_usb_phy_driver = {
  879. .probe = sun4i_usb_phy_probe,
  880. .remove_new = sun4i_usb_phy_remove,
  881. .driver = {
  882. .of_match_table = sun4i_usb_phy_of_match,
  883. .name = "sun4i-usb-phy",
  884. }
  885. };
  886. module_platform_driver(sun4i_usb_phy_driver);
  887. MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
  888. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  889. MODULE_LICENSE("GPL v2");