phy-sun4i-usb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. sun8i_a33_phy,
  87. sun8i_h3_phy,
  88. };
  89. struct sun4i_usb_phy_cfg {
  90. int num_phys;
  91. enum sun4i_usb_phy_type type;
  92. u32 disc_thresh;
  93. u8 phyctl_offset;
  94. bool dedicated_clocks;
  95. };
  96. struct sun4i_usb_phy_data {
  97. void __iomem *base;
  98. const struct sun4i_usb_phy_cfg *cfg;
  99. struct mutex mutex;
  100. struct sun4i_usb_phy {
  101. struct phy *phy;
  102. void __iomem *pmu;
  103. struct regulator *vbus;
  104. struct reset_control *reset;
  105. struct clk *clk;
  106. bool regulator_on;
  107. int index;
  108. } phys[MAX_PHYS];
  109. /* phy0 / otg related variables */
  110. struct extcon_dev *extcon;
  111. bool phy0_init;
  112. bool phy0_poll;
  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 *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 int sun4i_usb_phy_power_on(struct phy *_phy)
  289. {
  290. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  291. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  292. int ret;
  293. if (!phy->vbus || phy->regulator_on)
  294. return 0;
  295. /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
  296. if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
  297. data->vbus_det)
  298. return 0;
  299. ret = regulator_enable(phy->vbus);
  300. if (ret)
  301. return ret;
  302. phy->regulator_on = true;
  303. /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
  304. if (phy->index == 0 && data->vbus_det_gpio && data->phy0_poll)
  305. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  306. return 0;
  307. }
  308. static int sun4i_usb_phy_power_off(struct phy *_phy)
  309. {
  310. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  311. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  312. if (!phy->vbus || !phy->regulator_on)
  313. return 0;
  314. regulator_disable(phy->vbus);
  315. phy->regulator_on = false;
  316. /*
  317. * phy0 vbus typically slowly discharges, sometimes this causes the
  318. * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
  319. */
  320. if (phy->index == 0 && data->vbus_det_gpio && !data->phy0_poll)
  321. mod_delayed_work(system_wq, &data->detect, POLL_TIME);
  322. return 0;
  323. }
  324. void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
  325. {
  326. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  327. sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
  328. }
  329. EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
  330. static const struct phy_ops sun4i_usb_phy_ops = {
  331. .init = sun4i_usb_phy_init,
  332. .exit = sun4i_usb_phy_exit,
  333. .power_on = sun4i_usb_phy_power_on,
  334. .power_off = sun4i_usb_phy_power_off,
  335. .owner = THIS_MODULE,
  336. };
  337. static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
  338. {
  339. struct sun4i_usb_phy_data *data =
  340. container_of(work, struct sun4i_usb_phy_data, detect.work);
  341. struct phy *phy0 = data->phys[0].phy;
  342. int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
  343. id_det = gpiod_get_value_cansleep(data->id_det_gpio);
  344. vbus_det = sun4i_usb_phy0_get_vbus_det(data);
  345. mutex_lock(&phy0->mutex);
  346. if (!data->phy0_init) {
  347. mutex_unlock(&phy0->mutex);
  348. return;
  349. }
  350. if (id_det != data->id_det) {
  351. /*
  352. * When a host cable (id == 0) gets plugged in on systems
  353. * without vbus detection report vbus low for long enough for
  354. * the musb-ip to end the current device session.
  355. */
  356. if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 0) {
  357. sun4i_usb_phy0_set_vbus_detect(phy0, 0);
  358. msleep(200);
  359. sun4i_usb_phy0_set_vbus_detect(phy0, 1);
  360. }
  361. sun4i_usb_phy0_set_id_detect(phy0, id_det);
  362. data->id_det = id_det;
  363. id_notify = 1;
  364. }
  365. if (vbus_det != data->vbus_det) {
  366. sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
  367. data->vbus_det = vbus_det;
  368. vbus_notify = 1;
  369. }
  370. mutex_unlock(&phy0->mutex);
  371. if (id_notify) {
  372. extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
  373. !id_det);
  374. /*
  375. * When a host cable gets unplugged (id == 1) on systems
  376. * without vbus detection report vbus low for long enough to
  377. * the musb-ip to end the current host session.
  378. */
  379. if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 1) {
  380. mutex_lock(&phy0->mutex);
  381. sun4i_usb_phy0_set_vbus_detect(phy0, 0);
  382. msleep(1000);
  383. sun4i_usb_phy0_set_vbus_detect(phy0, 1);
  384. mutex_unlock(&phy0->mutex);
  385. }
  386. }
  387. if (vbus_notify)
  388. extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
  389. if (data->phy0_poll)
  390. queue_delayed_work(system_wq, &data->detect, POLL_TIME);
  391. }
  392. static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
  393. {
  394. struct sun4i_usb_phy_data *data = dev_id;
  395. /* vbus or id changed, let the pins settle and then scan them */
  396. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  397. return IRQ_HANDLED;
  398. }
  399. static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
  400. unsigned long val, void *v)
  401. {
  402. struct sun4i_usb_phy_data *data =
  403. container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
  404. struct power_supply *psy = v;
  405. /* Properties on the vbus_power_supply changed, scan vbus_det */
  406. if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
  407. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  408. return NOTIFY_OK;
  409. }
  410. static struct phy *sun4i_usb_phy_xlate(struct device *dev,
  411. struct of_phandle_args *args)
  412. {
  413. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  414. if (args->args[0] >= data->cfg->num_phys)
  415. return ERR_PTR(-ENODEV);
  416. return data->phys[args->args[0]].phy;
  417. }
  418. static int sun4i_usb_phy_remove(struct platform_device *pdev)
  419. {
  420. struct device *dev = &pdev->dev;
  421. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  422. if (data->vbus_power_nb_registered)
  423. power_supply_unreg_notifier(&data->vbus_power_nb);
  424. if (data->id_det_irq >= 0)
  425. devm_free_irq(dev, data->id_det_irq, data);
  426. if (data->vbus_det_irq >= 0)
  427. devm_free_irq(dev, data->vbus_det_irq, data);
  428. cancel_delayed_work_sync(&data->detect);
  429. return 0;
  430. }
  431. static const unsigned int sun4i_usb_phy0_cable[] = {
  432. EXTCON_USB,
  433. EXTCON_USB_HOST,
  434. EXTCON_NONE,
  435. };
  436. static int sun4i_usb_phy_probe(struct platform_device *pdev)
  437. {
  438. struct sun4i_usb_phy_data *data;
  439. struct device *dev = &pdev->dev;
  440. struct device_node *np = dev->of_node;
  441. struct phy_provider *phy_provider;
  442. struct resource *res;
  443. int i, ret;
  444. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  445. if (!data)
  446. return -ENOMEM;
  447. mutex_init(&data->mutex);
  448. INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
  449. dev_set_drvdata(dev, data);
  450. data->cfg = of_device_get_match_data(dev);
  451. if (!data->cfg)
  452. return -EINVAL;
  453. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
  454. data->base = devm_ioremap_resource(dev, res);
  455. if (IS_ERR(data->base))
  456. return PTR_ERR(data->base);
  457. data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
  458. GPIOD_IN);
  459. if (IS_ERR(data->id_det_gpio))
  460. return PTR_ERR(data->id_det_gpio);
  461. data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
  462. GPIOD_IN);
  463. if (IS_ERR(data->vbus_det_gpio))
  464. return PTR_ERR(data->vbus_det_gpio);
  465. if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
  466. data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
  467. "usb0_vbus_power-supply");
  468. if (IS_ERR(data->vbus_power_supply))
  469. return PTR_ERR(data->vbus_power_supply);
  470. if (!data->vbus_power_supply)
  471. return -EPROBE_DEFER;
  472. }
  473. /* vbus_det without id_det makes no sense, and is not supported */
  474. if (sun4i_usb_phy0_have_vbus_det(data) && !data->id_det_gpio) {
  475. dev_err(dev, "usb0_id_det missing or invalid\n");
  476. return -ENODEV;
  477. }
  478. if (data->id_det_gpio) {
  479. data->extcon = devm_extcon_dev_allocate(dev,
  480. sun4i_usb_phy0_cable);
  481. if (IS_ERR(data->extcon))
  482. return PTR_ERR(data->extcon);
  483. ret = devm_extcon_dev_register(dev, data->extcon);
  484. if (ret) {
  485. dev_err(dev, "failed to register extcon: %d\n", ret);
  486. return ret;
  487. }
  488. }
  489. for (i = 0; i < data->cfg->num_phys; i++) {
  490. struct sun4i_usb_phy *phy = data->phys + i;
  491. char name[16];
  492. snprintf(name, sizeof(name), "usb%d_vbus", i);
  493. phy->vbus = devm_regulator_get_optional(dev, name);
  494. if (IS_ERR(phy->vbus)) {
  495. if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
  496. return -EPROBE_DEFER;
  497. phy->vbus = NULL;
  498. }
  499. if (data->cfg->dedicated_clocks)
  500. snprintf(name, sizeof(name), "usb%d_phy", i);
  501. else
  502. strlcpy(name, "usb_phy", sizeof(name));
  503. phy->clk = devm_clk_get(dev, name);
  504. if (IS_ERR(phy->clk)) {
  505. dev_err(dev, "failed to get clock %s\n", name);
  506. return PTR_ERR(phy->clk);
  507. }
  508. snprintf(name, sizeof(name), "usb%d_reset", i);
  509. phy->reset = devm_reset_control_get(dev, name);
  510. if (IS_ERR(phy->reset)) {
  511. dev_err(dev, "failed to get reset %s\n", name);
  512. return PTR_ERR(phy->reset);
  513. }
  514. if (i) { /* No pmu for usbc0 */
  515. snprintf(name, sizeof(name), "pmu%d", i);
  516. res = platform_get_resource_byname(pdev,
  517. IORESOURCE_MEM, name);
  518. phy->pmu = devm_ioremap_resource(dev, res);
  519. if (IS_ERR(phy->pmu))
  520. return PTR_ERR(phy->pmu);
  521. }
  522. phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
  523. if (IS_ERR(phy->phy)) {
  524. dev_err(dev, "failed to create PHY %d\n", i);
  525. return PTR_ERR(phy->phy);
  526. }
  527. phy->index = i;
  528. phy_set_drvdata(phy->phy, &data->phys[i]);
  529. }
  530. data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
  531. data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
  532. if ((data->id_det_gpio && data->id_det_irq < 0) ||
  533. (data->vbus_det_gpio && data->vbus_det_irq < 0))
  534. data->phy0_poll = true;
  535. if (data->id_det_irq >= 0) {
  536. ret = devm_request_irq(dev, data->id_det_irq,
  537. sun4i_usb_phy0_id_vbus_det_irq,
  538. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  539. "usb0-id-det", data);
  540. if (ret) {
  541. dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
  542. return ret;
  543. }
  544. }
  545. if (data->vbus_det_irq >= 0) {
  546. ret = devm_request_irq(dev, data->vbus_det_irq,
  547. sun4i_usb_phy0_id_vbus_det_irq,
  548. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  549. "usb0-vbus-det", data);
  550. if (ret) {
  551. dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
  552. data->vbus_det_irq = -1;
  553. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  554. return ret;
  555. }
  556. }
  557. if (data->vbus_power_supply) {
  558. data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
  559. data->vbus_power_nb.priority = 0;
  560. ret = power_supply_reg_notifier(&data->vbus_power_nb);
  561. if (ret) {
  562. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  563. return ret;
  564. }
  565. data->vbus_power_nb_registered = true;
  566. }
  567. phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
  568. if (IS_ERR(phy_provider)) {
  569. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  570. return PTR_ERR(phy_provider);
  571. }
  572. return 0;
  573. }
  574. static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
  575. .num_phys = 3,
  576. .type = sun4i_a10_phy,
  577. .disc_thresh = 3,
  578. .phyctl_offset = REG_PHYCTL_A10,
  579. .dedicated_clocks = false,
  580. };
  581. static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
  582. .num_phys = 2,
  583. .type = sun4i_a10_phy,
  584. .disc_thresh = 2,
  585. .phyctl_offset = REG_PHYCTL_A10,
  586. .dedicated_clocks = false,
  587. };
  588. static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
  589. .num_phys = 3,
  590. .type = sun4i_a10_phy,
  591. .disc_thresh = 3,
  592. .phyctl_offset = REG_PHYCTL_A10,
  593. .dedicated_clocks = true,
  594. };
  595. static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
  596. .num_phys = 3,
  597. .type = sun4i_a10_phy,
  598. .disc_thresh = 2,
  599. .phyctl_offset = REG_PHYCTL_A10,
  600. .dedicated_clocks = false,
  601. };
  602. static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
  603. .num_phys = 2,
  604. .type = sun4i_a10_phy,
  605. .disc_thresh = 3,
  606. .phyctl_offset = REG_PHYCTL_A10,
  607. .dedicated_clocks = true,
  608. };
  609. static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
  610. .num_phys = 2,
  611. .type = sun8i_a33_phy,
  612. .disc_thresh = 3,
  613. .phyctl_offset = REG_PHYCTL_A33,
  614. .dedicated_clocks = true,
  615. };
  616. static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
  617. .num_phys = 4,
  618. .type = sun8i_h3_phy,
  619. .disc_thresh = 3,
  620. .dedicated_clocks = true,
  621. };
  622. static const struct of_device_id sun4i_usb_phy_of_match[] = {
  623. { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
  624. { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
  625. { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
  626. { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
  627. { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
  628. { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
  629. { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
  630. { },
  631. };
  632. MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
  633. static struct platform_driver sun4i_usb_phy_driver = {
  634. .probe = sun4i_usb_phy_probe,
  635. .remove = sun4i_usb_phy_remove,
  636. .driver = {
  637. .of_match_table = sun4i_usb_phy_of_match,
  638. .name = "sun4i-usb-phy",
  639. }
  640. };
  641. module_platform_driver(sun4i_usb_phy_driver);
  642. MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
  643. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  644. MODULE_LICENSE("GPL v2");