phy-sun4i-usb.c 22 KB

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