phy-sun4i-usb.c 25 KB

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