phy-sun4i-usb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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/workqueue.h>
  43. #define REG_ISCR 0x00
  44. #define REG_PHYCTL_A10 0x04
  45. #define REG_PHYBIST 0x08
  46. #define REG_PHYTUNE 0x0c
  47. #define REG_PHYCTL_A33 0x10
  48. #define REG_PHY_UNK_H3 0x20
  49. #define REG_PMU_UNK_H3 0x10
  50. #define PHYCTL_DATA BIT(7)
  51. #define SUNXI_AHB_ICHR8_EN BIT(10)
  52. #define SUNXI_AHB_INCR4_BURST_EN BIT(9)
  53. #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
  54. #define SUNXI_ULPI_BYPASS_EN BIT(0)
  55. /* ISCR, Interface Status and Control bits */
  56. #define ISCR_ID_PULLUP_EN (1 << 17)
  57. #define ISCR_DPDM_PULLUP_EN (1 << 16)
  58. /* sunxi has the phy id/vbus pins not connected, so we use the force bits */
  59. #define ISCR_FORCE_ID_MASK (3 << 14)
  60. #define ISCR_FORCE_ID_LOW (2 << 14)
  61. #define ISCR_FORCE_ID_HIGH (3 << 14)
  62. #define ISCR_FORCE_VBUS_MASK (3 << 12)
  63. #define ISCR_FORCE_VBUS_LOW (2 << 12)
  64. #define ISCR_FORCE_VBUS_HIGH (3 << 12)
  65. /* Common Control Bits for Both PHYs */
  66. #define PHY_PLL_BW 0x03
  67. #define PHY_RES45_CAL_EN 0x0c
  68. /* Private Control Bits for Each PHY */
  69. #define PHY_TX_AMPLITUDE_TUNE 0x20
  70. #define PHY_TX_SLEWRATE_TUNE 0x22
  71. #define PHY_VBUSVALID_TH_SEL 0x25
  72. #define PHY_PULLUP_RES_SEL 0x27
  73. #define PHY_OTG_FUNC_EN 0x28
  74. #define PHY_VBUS_DET_EN 0x29
  75. #define PHY_DISCON_TH_SEL 0x2a
  76. #define PHY_SQUELCH_DETECT 0x3c
  77. #define MAX_PHYS 4
  78. /*
  79. * Note do not raise the debounce time, we must report Vusb high within 100ms
  80. * otherwise we get Vbus errors
  81. */
  82. #define DEBOUNCE_TIME msecs_to_jiffies(50)
  83. #define POLL_TIME msecs_to_jiffies(250)
  84. enum sun4i_usb_phy_type {
  85. sun4i_a10_phy,
  86. sun6i_a31_phy,
  87. sun8i_a33_phy,
  88. sun8i_h3_phy,
  89. };
  90. struct sun4i_usb_phy_cfg {
  91. int num_phys;
  92. enum sun4i_usb_phy_type type;
  93. u32 disc_thresh;
  94. u8 phyctl_offset;
  95. bool dedicated_clocks;
  96. };
  97. struct sun4i_usb_phy_data {
  98. void __iomem *base;
  99. const struct sun4i_usb_phy_cfg *cfg;
  100. struct mutex mutex;
  101. struct sun4i_usb_phy {
  102. struct phy *phy;
  103. void __iomem *pmu;
  104. struct regulator *vbus;
  105. struct reset_control *reset;
  106. struct clk *clk;
  107. bool regulator_on;
  108. int index;
  109. } phys[MAX_PHYS];
  110. /* phy0 / otg related variables */
  111. struct extcon_dev *extcon;
  112. bool phy0_init;
  113. struct gpio_desc *id_det_gpio;
  114. struct gpio_desc *vbus_det_gpio;
  115. struct power_supply *vbus_power_supply;
  116. struct notifier_block vbus_power_nb;
  117. bool vbus_power_nb_registered;
  118. int id_det_irq;
  119. int vbus_det_irq;
  120. int id_det;
  121. int vbus_det;
  122. struct delayed_work detect;
  123. };
  124. #define to_sun4i_usb_phy_data(phy) \
  125. container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
  126. static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
  127. {
  128. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  129. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  130. u32 iscr;
  131. iscr = readl(data->base + REG_ISCR);
  132. iscr &= ~clr;
  133. iscr |= set;
  134. writel(iscr, data->base + REG_ISCR);
  135. }
  136. static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
  137. {
  138. if (val)
  139. val = ISCR_FORCE_ID_HIGH;
  140. else
  141. val = ISCR_FORCE_ID_LOW;
  142. sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
  143. }
  144. static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
  145. {
  146. if (val)
  147. val = ISCR_FORCE_VBUS_HIGH;
  148. else
  149. val = ISCR_FORCE_VBUS_LOW;
  150. sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
  151. }
  152. static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
  153. int len)
  154. {
  155. struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
  156. u32 temp, usbc_bit = BIT(phy->index * 2);
  157. void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
  158. int i;
  159. mutex_lock(&phy_data->mutex);
  160. if (phy_data->cfg->type == sun8i_a33_phy) {
  161. /* A33 needs us to set phyctl to 0 explicitly */
  162. writel(0, phyctl);
  163. }
  164. for (i = 0; i < len; i++) {
  165. temp = readl(phyctl);
  166. /* clear the address portion */
  167. temp &= ~(0xff << 8);
  168. /* set the address */
  169. temp |= ((addr + i) << 8);
  170. writel(temp, phyctl);
  171. /* set the data bit and clear usbc bit*/
  172. temp = readb(phyctl);
  173. if (data & 0x1)
  174. temp |= PHYCTL_DATA;
  175. else
  176. temp &= ~PHYCTL_DATA;
  177. temp &= ~usbc_bit;
  178. writeb(temp, phyctl);
  179. /* pulse usbc_bit */
  180. temp = readb(phyctl);
  181. temp |= usbc_bit;
  182. writeb(temp, phyctl);
  183. temp = readb(phyctl);
  184. temp &= ~usbc_bit;
  185. writeb(temp, phyctl);
  186. data >>= 1;
  187. }
  188. mutex_unlock(&phy_data->mutex);
  189. }
  190. static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
  191. {
  192. u32 bits, reg_value;
  193. if (!phy->pmu)
  194. return;
  195. bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
  196. SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
  197. reg_value = readl(phy->pmu);
  198. if (enable)
  199. reg_value |= bits;
  200. else
  201. reg_value &= ~bits;
  202. writel(reg_value, phy->pmu);
  203. }
  204. static int sun4i_usb_phy_init(struct phy *_phy)
  205. {
  206. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  207. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  208. int ret;
  209. u32 val;
  210. ret = clk_prepare_enable(phy->clk);
  211. if (ret)
  212. return ret;
  213. ret = reset_control_deassert(phy->reset);
  214. if (ret) {
  215. clk_disable_unprepare(phy->clk);
  216. return ret;
  217. }
  218. if (data->cfg->type == sun8i_h3_phy) {
  219. if (phy->index == 0) {
  220. val = readl(data->base + REG_PHY_UNK_H3);
  221. writel(val & ~1, data->base + REG_PHY_UNK_H3);
  222. }
  223. val = readl(phy->pmu + REG_PMU_UNK_H3);
  224. writel(val & ~2, phy->pmu + REG_PMU_UNK_H3);
  225. } else {
  226. /* Enable USB 45 Ohm resistor calibration */
  227. if (phy->index == 0)
  228. sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
  229. /* Adjust PHY's magnitude and rate */
  230. sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
  231. /* Disconnect threshold adjustment */
  232. sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
  233. data->cfg->disc_thresh, 2);
  234. }
  235. sun4i_usb_phy_passby(phy, 1);
  236. if (phy->index == 0) {
  237. data->phy0_init = true;
  238. /* Enable pull-ups */
  239. sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
  240. sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
  241. if (data->id_det_gpio) {
  242. /* OTG mode, force ISCR and cable state updates */
  243. data->id_det = -1;
  244. data->vbus_det = -1;
  245. queue_delayed_work(system_wq, &data->detect, 0);
  246. } else {
  247. /* Host only mode */
  248. sun4i_usb_phy0_set_id_detect(_phy, 0);
  249. sun4i_usb_phy0_set_vbus_detect(_phy, 1);
  250. }
  251. }
  252. return 0;
  253. }
  254. static int sun4i_usb_phy_exit(struct phy *_phy)
  255. {
  256. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  257. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  258. if (phy->index == 0) {
  259. /* Disable pull-ups */
  260. sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
  261. sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
  262. data->phy0_init = false;
  263. }
  264. sun4i_usb_phy_passby(phy, 0);
  265. reset_control_assert(phy->reset);
  266. clk_disable_unprepare(phy->clk);
  267. return 0;
  268. }
  269. static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
  270. {
  271. if (data->vbus_det_gpio)
  272. return gpiod_get_value_cansleep(data->vbus_det_gpio);
  273. if (data->vbus_power_supply) {
  274. union power_supply_propval val;
  275. int r;
  276. r = power_supply_get_property(data->vbus_power_supply,
  277. POWER_SUPPLY_PROP_PRESENT, &val);
  278. if (r == 0)
  279. return val.intval;
  280. }
  281. /* Fallback: report vbus as high */
  282. return 1;
  283. }
  284. static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
  285. {
  286. return data->vbus_det_gpio || data->vbus_power_supply;
  287. }
  288. static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data)
  289. {
  290. if ((data->id_det_gpio && data->id_det_irq <= 0) ||
  291. (data->vbus_det_gpio && data->vbus_det_irq <= 0))
  292. return true;
  293. /*
  294. * The A31 companion pmic (axp221) does not generate vbus change
  295. * interrupts when the board is driving vbus, so we must poll
  296. * when using the pmic for vbus-det _and_ we're driving vbus.
  297. */
  298. if (data->cfg->type == sun6i_a31_phy &&
  299. data->vbus_power_supply && data->phys[0].regulator_on)
  300. return true;
  301. return false;
  302. }
  303. static int sun4i_usb_phy_power_on(struct phy *_phy)
  304. {
  305. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  306. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  307. int ret;
  308. if (!phy->vbus || phy->regulator_on)
  309. return 0;
  310. /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
  311. if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
  312. data->vbus_det)
  313. return 0;
  314. ret = regulator_enable(phy->vbus);
  315. if (ret)
  316. return ret;
  317. phy->regulator_on = true;
  318. /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
  319. if (phy->index == 0 && sun4i_usb_phy0_poll(data))
  320. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  321. return 0;
  322. }
  323. static int sun4i_usb_phy_power_off(struct phy *_phy)
  324. {
  325. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  326. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  327. if (!phy->vbus || !phy->regulator_on)
  328. return 0;
  329. regulator_disable(phy->vbus);
  330. phy->regulator_on = false;
  331. /*
  332. * phy0 vbus typically slowly discharges, sometimes this causes the
  333. * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
  334. */
  335. if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
  336. mod_delayed_work(system_wq, &data->detect, POLL_TIME);
  337. return 0;
  338. }
  339. void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
  340. {
  341. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  342. sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
  343. }
  344. EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
  345. static const struct phy_ops sun4i_usb_phy_ops = {
  346. .init = sun4i_usb_phy_init,
  347. .exit = sun4i_usb_phy_exit,
  348. .power_on = sun4i_usb_phy_power_on,
  349. .power_off = sun4i_usb_phy_power_off,
  350. .owner = THIS_MODULE,
  351. };
  352. static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
  353. {
  354. struct sun4i_usb_phy_data *data =
  355. container_of(work, struct sun4i_usb_phy_data, detect.work);
  356. struct phy *phy0 = data->phys[0].phy;
  357. int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
  358. id_det = gpiod_get_value_cansleep(data->id_det_gpio);
  359. vbus_det = sun4i_usb_phy0_get_vbus_det(data);
  360. mutex_lock(&phy0->mutex);
  361. if (!data->phy0_init) {
  362. mutex_unlock(&phy0->mutex);
  363. return;
  364. }
  365. if (id_det != data->id_det) {
  366. /*
  367. * When a host cable (id == 0) gets plugged in on systems
  368. * without vbus detection report vbus low for long enough for
  369. * the musb-ip to end the current device session.
  370. */
  371. if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 0) {
  372. sun4i_usb_phy0_set_vbus_detect(phy0, 0);
  373. msleep(200);
  374. sun4i_usb_phy0_set_vbus_detect(phy0, 1);
  375. }
  376. sun4i_usb_phy0_set_id_detect(phy0, id_det);
  377. data->id_det = id_det;
  378. id_notify = 1;
  379. }
  380. if (vbus_det != data->vbus_det) {
  381. sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
  382. data->vbus_det = vbus_det;
  383. vbus_notify = 1;
  384. }
  385. mutex_unlock(&phy0->mutex);
  386. if (id_notify) {
  387. extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
  388. !id_det);
  389. /*
  390. * When a host cable gets unplugged (id == 1) on systems
  391. * without vbus detection report vbus low for long enough to
  392. * the musb-ip to end the current host session.
  393. */
  394. if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 1) {
  395. mutex_lock(&phy0->mutex);
  396. sun4i_usb_phy0_set_vbus_detect(phy0, 0);
  397. msleep(1000);
  398. sun4i_usb_phy0_set_vbus_detect(phy0, 1);
  399. mutex_unlock(&phy0->mutex);
  400. }
  401. }
  402. if (vbus_notify)
  403. extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
  404. if (sun4i_usb_phy0_poll(data))
  405. queue_delayed_work(system_wq, &data->detect, POLL_TIME);
  406. }
  407. static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
  408. {
  409. struct sun4i_usb_phy_data *data = dev_id;
  410. /* vbus or id changed, let the pins settle and then scan them */
  411. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  412. return IRQ_HANDLED;
  413. }
  414. static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
  415. unsigned long val, void *v)
  416. {
  417. struct sun4i_usb_phy_data *data =
  418. container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
  419. struct power_supply *psy = v;
  420. /* Properties on the vbus_power_supply changed, scan vbus_det */
  421. if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
  422. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  423. return NOTIFY_OK;
  424. }
  425. static struct phy *sun4i_usb_phy_xlate(struct device *dev,
  426. struct of_phandle_args *args)
  427. {
  428. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  429. if (args->args[0] >= data->cfg->num_phys)
  430. return ERR_PTR(-ENODEV);
  431. return data->phys[args->args[0]].phy;
  432. }
  433. static int sun4i_usb_phy_remove(struct platform_device *pdev)
  434. {
  435. struct device *dev = &pdev->dev;
  436. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  437. if (data->vbus_power_nb_registered)
  438. power_supply_unreg_notifier(&data->vbus_power_nb);
  439. if (data->id_det_irq > 0)
  440. devm_free_irq(dev, data->id_det_irq, data);
  441. if (data->vbus_det_irq > 0)
  442. devm_free_irq(dev, data->vbus_det_irq, data);
  443. cancel_delayed_work_sync(&data->detect);
  444. return 0;
  445. }
  446. static const unsigned int sun4i_usb_phy0_cable[] = {
  447. EXTCON_USB,
  448. EXTCON_USB_HOST,
  449. EXTCON_NONE,
  450. };
  451. static int sun4i_usb_phy_probe(struct platform_device *pdev)
  452. {
  453. struct sun4i_usb_phy_data *data;
  454. struct device *dev = &pdev->dev;
  455. struct device_node *np = dev->of_node;
  456. struct phy_provider *phy_provider;
  457. struct resource *res;
  458. int i, ret;
  459. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  460. if (!data)
  461. return -ENOMEM;
  462. mutex_init(&data->mutex);
  463. INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
  464. dev_set_drvdata(dev, data);
  465. data->cfg = of_device_get_match_data(dev);
  466. if (!data->cfg)
  467. return -EINVAL;
  468. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
  469. data->base = devm_ioremap_resource(dev, res);
  470. if (IS_ERR(data->base))
  471. return PTR_ERR(data->base);
  472. data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
  473. GPIOD_IN);
  474. if (IS_ERR(data->id_det_gpio))
  475. return PTR_ERR(data->id_det_gpio);
  476. data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
  477. GPIOD_IN);
  478. if (IS_ERR(data->vbus_det_gpio))
  479. return PTR_ERR(data->vbus_det_gpio);
  480. if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
  481. data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
  482. "usb0_vbus_power-supply");
  483. if (IS_ERR(data->vbus_power_supply))
  484. return PTR_ERR(data->vbus_power_supply);
  485. if (!data->vbus_power_supply)
  486. return -EPROBE_DEFER;
  487. }
  488. /* vbus_det without id_det makes no sense, and is not supported */
  489. if (sun4i_usb_phy0_have_vbus_det(data) && !data->id_det_gpio) {
  490. dev_err(dev, "usb0_id_det missing or invalid\n");
  491. return -ENODEV;
  492. }
  493. if (data->id_det_gpio) {
  494. data->extcon = devm_extcon_dev_allocate(dev,
  495. sun4i_usb_phy0_cable);
  496. if (IS_ERR(data->extcon))
  497. return PTR_ERR(data->extcon);
  498. ret = devm_extcon_dev_register(dev, data->extcon);
  499. if (ret) {
  500. dev_err(dev, "failed to register extcon: %d\n", ret);
  501. return ret;
  502. }
  503. }
  504. for (i = 0; i < data->cfg->num_phys; i++) {
  505. struct sun4i_usb_phy *phy = data->phys + i;
  506. char name[16];
  507. snprintf(name, sizeof(name), "usb%d_vbus", i);
  508. phy->vbus = devm_regulator_get_optional(dev, name);
  509. if (IS_ERR(phy->vbus)) {
  510. if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
  511. return -EPROBE_DEFER;
  512. phy->vbus = NULL;
  513. }
  514. if (data->cfg->dedicated_clocks)
  515. snprintf(name, sizeof(name), "usb%d_phy", i);
  516. else
  517. strlcpy(name, "usb_phy", sizeof(name));
  518. phy->clk = devm_clk_get(dev, name);
  519. if (IS_ERR(phy->clk)) {
  520. dev_err(dev, "failed to get clock %s\n", name);
  521. return PTR_ERR(phy->clk);
  522. }
  523. snprintf(name, sizeof(name), "usb%d_reset", i);
  524. phy->reset = devm_reset_control_get(dev, name);
  525. if (IS_ERR(phy->reset)) {
  526. dev_err(dev, "failed to get reset %s\n", name);
  527. return PTR_ERR(phy->reset);
  528. }
  529. if (i) { /* No pmu for usbc0 */
  530. snprintf(name, sizeof(name), "pmu%d", i);
  531. res = platform_get_resource_byname(pdev,
  532. IORESOURCE_MEM, name);
  533. phy->pmu = devm_ioremap_resource(dev, res);
  534. if (IS_ERR(phy->pmu))
  535. return PTR_ERR(phy->pmu);
  536. }
  537. phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
  538. if (IS_ERR(phy->phy)) {
  539. dev_err(dev, "failed to create PHY %d\n", i);
  540. return PTR_ERR(phy->phy);
  541. }
  542. phy->index = i;
  543. phy_set_drvdata(phy->phy, &data->phys[i]);
  544. }
  545. data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
  546. if (data->id_det_irq > 0) {
  547. ret = devm_request_irq(dev, data->id_det_irq,
  548. sun4i_usb_phy0_id_vbus_det_irq,
  549. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  550. "usb0-id-det", data);
  551. if (ret) {
  552. dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
  553. return ret;
  554. }
  555. }
  556. data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
  557. if (data->vbus_det_irq > 0) {
  558. ret = devm_request_irq(dev, data->vbus_det_irq,
  559. sun4i_usb_phy0_id_vbus_det_irq,
  560. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  561. "usb0-vbus-det", data);
  562. if (ret) {
  563. dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
  564. data->vbus_det_irq = -1;
  565. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  566. return ret;
  567. }
  568. }
  569. if (data->vbus_power_supply) {
  570. data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
  571. data->vbus_power_nb.priority = 0;
  572. ret = power_supply_reg_notifier(&data->vbus_power_nb);
  573. if (ret) {
  574. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  575. return ret;
  576. }
  577. data->vbus_power_nb_registered = true;
  578. }
  579. phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
  580. if (IS_ERR(phy_provider)) {
  581. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  582. return PTR_ERR(phy_provider);
  583. }
  584. return 0;
  585. }
  586. static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
  587. .num_phys = 3,
  588. .type = sun4i_a10_phy,
  589. .disc_thresh = 3,
  590. .phyctl_offset = REG_PHYCTL_A10,
  591. .dedicated_clocks = false,
  592. };
  593. static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
  594. .num_phys = 2,
  595. .type = sun4i_a10_phy,
  596. .disc_thresh = 2,
  597. .phyctl_offset = REG_PHYCTL_A10,
  598. .dedicated_clocks = false,
  599. };
  600. static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
  601. .num_phys = 3,
  602. .type = sun6i_a31_phy,
  603. .disc_thresh = 3,
  604. .phyctl_offset = REG_PHYCTL_A10,
  605. .dedicated_clocks = true,
  606. };
  607. static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
  608. .num_phys = 3,
  609. .type = sun4i_a10_phy,
  610. .disc_thresh = 2,
  611. .phyctl_offset = REG_PHYCTL_A10,
  612. .dedicated_clocks = false,
  613. };
  614. static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
  615. .num_phys = 2,
  616. .type = sun4i_a10_phy,
  617. .disc_thresh = 3,
  618. .phyctl_offset = REG_PHYCTL_A10,
  619. .dedicated_clocks = true,
  620. };
  621. static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
  622. .num_phys = 2,
  623. .type = sun8i_a33_phy,
  624. .disc_thresh = 3,
  625. .phyctl_offset = REG_PHYCTL_A33,
  626. .dedicated_clocks = true,
  627. };
  628. static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
  629. .num_phys = 4,
  630. .type = sun8i_h3_phy,
  631. .disc_thresh = 3,
  632. .dedicated_clocks = true,
  633. };
  634. static const struct of_device_id sun4i_usb_phy_of_match[] = {
  635. { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
  636. { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
  637. { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
  638. { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
  639. { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
  640. { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
  641. { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
  642. { },
  643. };
  644. MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
  645. static struct platform_driver sun4i_usb_phy_driver = {
  646. .probe = sun4i_usb_phy_probe,
  647. .remove = sun4i_usb_phy_remove,
  648. .driver = {
  649. .of_match_table = sun4i_usb_phy_of_match,
  650. .name = "sun4i-usb-phy",
  651. }
  652. };
  653. module_platform_driver(sun4i_usb_phy_driver);
  654. MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
  655. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  656. MODULE_LICENSE("GPL v2");