phy-sun4i-usb.c 26 KB

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