phy-sun4i-usb.c 19 KB

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