phy-sun4i-usb.c 25 KB

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