phy-sun4i-usb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. if (phy->index != 0)
  365. return -EINVAL;
  366. switch (mode) {
  367. case PHY_MODE_USB_HOST:
  368. data->dr_mode = USB_DR_MODE_HOST;
  369. break;
  370. case PHY_MODE_USB_DEVICE:
  371. data->dr_mode = USB_DR_MODE_PERIPHERAL;
  372. break;
  373. case PHY_MODE_USB_OTG:
  374. data->dr_mode = USB_DR_MODE_OTG;
  375. break;
  376. default:
  377. return -EINVAL;
  378. }
  379. dev_info(&_phy->dev, "Changing dr_mode to %d\n", (int)data->dr_mode);
  380. data->force_session_end = true;
  381. queue_delayed_work(system_wq, &data->detect, 0);
  382. return 0;
  383. }
  384. void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
  385. {
  386. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  387. sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
  388. }
  389. EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
  390. static const struct phy_ops sun4i_usb_phy_ops = {
  391. .init = sun4i_usb_phy_init,
  392. .exit = sun4i_usb_phy_exit,
  393. .power_on = sun4i_usb_phy_power_on,
  394. .power_off = sun4i_usb_phy_power_off,
  395. .set_mode = sun4i_usb_phy_set_mode,
  396. .owner = THIS_MODULE,
  397. };
  398. static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
  399. {
  400. struct sun4i_usb_phy_data *data =
  401. container_of(work, struct sun4i_usb_phy_data, detect.work);
  402. struct phy *phy0 = data->phys[0].phy;
  403. bool force_session_end, id_notify = false, vbus_notify = false;
  404. int id_det, vbus_det;
  405. if (phy0 == NULL)
  406. return;
  407. id_det = sun4i_usb_phy0_get_id_det(data);
  408. vbus_det = sun4i_usb_phy0_get_vbus_det(data);
  409. mutex_lock(&phy0->mutex);
  410. if (!data->phy0_init) {
  411. mutex_unlock(&phy0->mutex);
  412. return;
  413. }
  414. force_session_end = data->force_session_end;
  415. data->force_session_end = false;
  416. if (id_det != data->id_det) {
  417. /* id-change, force session end if we've no vbus detection */
  418. if (data->dr_mode == USB_DR_MODE_OTG &&
  419. !sun4i_usb_phy0_have_vbus_det(data))
  420. force_session_end = true;
  421. /* When entering host mode (id = 0) force end the session now */
  422. if (force_session_end && id_det == 0) {
  423. sun4i_usb_phy0_set_vbus_detect(phy0, 0);
  424. msleep(200);
  425. sun4i_usb_phy0_set_vbus_detect(phy0, 1);
  426. }
  427. sun4i_usb_phy0_set_id_detect(phy0, id_det);
  428. data->id_det = id_det;
  429. id_notify = true;
  430. }
  431. if (vbus_det != data->vbus_det) {
  432. sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
  433. data->vbus_det = vbus_det;
  434. vbus_notify = true;
  435. }
  436. mutex_unlock(&phy0->mutex);
  437. if (id_notify) {
  438. extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
  439. !id_det);
  440. /* When leaving host mode force end the session here */
  441. if (force_session_end && id_det == 1) {
  442. mutex_lock(&phy0->mutex);
  443. sun4i_usb_phy0_set_vbus_detect(phy0, 0);
  444. msleep(1000);
  445. sun4i_usb_phy0_set_vbus_detect(phy0, 1);
  446. mutex_unlock(&phy0->mutex);
  447. }
  448. }
  449. if (vbus_notify)
  450. extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
  451. if (sun4i_usb_phy0_poll(data))
  452. queue_delayed_work(system_wq, &data->detect, POLL_TIME);
  453. }
  454. static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
  455. {
  456. struct sun4i_usb_phy_data *data = dev_id;
  457. /* vbus or id changed, let the pins settle and then scan them */
  458. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  459. return IRQ_HANDLED;
  460. }
  461. static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
  462. unsigned long val, void *v)
  463. {
  464. struct sun4i_usb_phy_data *data =
  465. container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
  466. struct power_supply *psy = v;
  467. /* Properties on the vbus_power_supply changed, scan vbus_det */
  468. if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
  469. mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
  470. return NOTIFY_OK;
  471. }
  472. static struct phy *sun4i_usb_phy_xlate(struct device *dev,
  473. struct of_phandle_args *args)
  474. {
  475. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  476. if (args->args[0] >= data->cfg->num_phys)
  477. return ERR_PTR(-ENODEV);
  478. return data->phys[args->args[0]].phy;
  479. }
  480. static int sun4i_usb_phy_remove(struct platform_device *pdev)
  481. {
  482. struct device *dev = &pdev->dev;
  483. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  484. if (data->vbus_power_nb_registered)
  485. power_supply_unreg_notifier(&data->vbus_power_nb);
  486. if (data->id_det_irq > 0)
  487. devm_free_irq(dev, data->id_det_irq, data);
  488. if (data->vbus_det_irq > 0)
  489. devm_free_irq(dev, data->vbus_det_irq, data);
  490. cancel_delayed_work_sync(&data->detect);
  491. return 0;
  492. }
  493. static const unsigned int sun4i_usb_phy0_cable[] = {
  494. EXTCON_USB,
  495. EXTCON_USB_HOST,
  496. EXTCON_NONE,
  497. };
  498. static int sun4i_usb_phy_probe(struct platform_device *pdev)
  499. {
  500. struct sun4i_usb_phy_data *data;
  501. struct device *dev = &pdev->dev;
  502. struct device_node *np = dev->of_node;
  503. struct phy_provider *phy_provider;
  504. struct resource *res;
  505. int i, ret;
  506. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  507. if (!data)
  508. return -ENOMEM;
  509. spin_lock_init(&data->reg_lock);
  510. INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
  511. dev_set_drvdata(dev, data);
  512. data->cfg = of_device_get_match_data(dev);
  513. if (!data->cfg)
  514. return -EINVAL;
  515. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
  516. data->base = devm_ioremap_resource(dev, res);
  517. if (IS_ERR(data->base))
  518. return PTR_ERR(data->base);
  519. data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
  520. GPIOD_IN);
  521. if (IS_ERR(data->id_det_gpio))
  522. return PTR_ERR(data->id_det_gpio);
  523. data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
  524. GPIOD_IN);
  525. if (IS_ERR(data->vbus_det_gpio))
  526. return PTR_ERR(data->vbus_det_gpio);
  527. if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
  528. data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
  529. "usb0_vbus_power-supply");
  530. if (IS_ERR(data->vbus_power_supply))
  531. return PTR_ERR(data->vbus_power_supply);
  532. if (!data->vbus_power_supply)
  533. return -EPROBE_DEFER;
  534. }
  535. data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0);
  536. data->extcon = devm_extcon_dev_allocate(dev, sun4i_usb_phy0_cable);
  537. if (IS_ERR(data->extcon))
  538. return PTR_ERR(data->extcon);
  539. ret = devm_extcon_dev_register(dev, data->extcon);
  540. if (ret) {
  541. dev_err(dev, "failed to register extcon: %d\n", ret);
  542. return ret;
  543. }
  544. for (i = 0; i < data->cfg->num_phys; i++) {
  545. struct sun4i_usb_phy *phy = data->phys + i;
  546. char name[16];
  547. snprintf(name, sizeof(name), "usb%d_vbus", i);
  548. phy->vbus = devm_regulator_get_optional(dev, name);
  549. if (IS_ERR(phy->vbus)) {
  550. if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
  551. return -EPROBE_DEFER;
  552. phy->vbus = NULL;
  553. }
  554. if (data->cfg->dedicated_clocks)
  555. snprintf(name, sizeof(name), "usb%d_phy", i);
  556. else
  557. strlcpy(name, "usb_phy", sizeof(name));
  558. phy->clk = devm_clk_get(dev, name);
  559. if (IS_ERR(phy->clk)) {
  560. dev_err(dev, "failed to get clock %s\n", name);
  561. return PTR_ERR(phy->clk);
  562. }
  563. snprintf(name, sizeof(name), "usb%d_reset", i);
  564. phy->reset = devm_reset_control_get(dev, name);
  565. if (IS_ERR(phy->reset)) {
  566. dev_err(dev, "failed to get reset %s\n", name);
  567. return PTR_ERR(phy->reset);
  568. }
  569. if (i) { /* No pmu for usbc0 */
  570. snprintf(name, sizeof(name), "pmu%d", i);
  571. res = platform_get_resource_byname(pdev,
  572. IORESOURCE_MEM, name);
  573. phy->pmu = devm_ioremap_resource(dev, res);
  574. if (IS_ERR(phy->pmu))
  575. return PTR_ERR(phy->pmu);
  576. }
  577. phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
  578. if (IS_ERR(phy->phy)) {
  579. dev_err(dev, "failed to create PHY %d\n", i);
  580. return PTR_ERR(phy->phy);
  581. }
  582. phy->index = i;
  583. phy_set_drvdata(phy->phy, &data->phys[i]);
  584. }
  585. data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
  586. if (data->id_det_irq > 0) {
  587. ret = devm_request_irq(dev, data->id_det_irq,
  588. sun4i_usb_phy0_id_vbus_det_irq,
  589. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  590. "usb0-id-det", data);
  591. if (ret) {
  592. dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
  593. return ret;
  594. }
  595. }
  596. data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
  597. if (data->vbus_det_irq > 0) {
  598. ret = devm_request_irq(dev, data->vbus_det_irq,
  599. sun4i_usb_phy0_id_vbus_det_irq,
  600. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  601. "usb0-vbus-det", data);
  602. if (ret) {
  603. dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
  604. data->vbus_det_irq = -1;
  605. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  606. return ret;
  607. }
  608. }
  609. if (data->vbus_power_supply) {
  610. data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
  611. data->vbus_power_nb.priority = 0;
  612. ret = power_supply_reg_notifier(&data->vbus_power_nb);
  613. if (ret) {
  614. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  615. return ret;
  616. }
  617. data->vbus_power_nb_registered = true;
  618. }
  619. phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
  620. if (IS_ERR(phy_provider)) {
  621. sun4i_usb_phy_remove(pdev); /* Stop detect work */
  622. return PTR_ERR(phy_provider);
  623. }
  624. return 0;
  625. }
  626. static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
  627. .num_phys = 3,
  628. .type = sun4i_a10_phy,
  629. .disc_thresh = 3,
  630. .phyctl_offset = REG_PHYCTL_A10,
  631. .dedicated_clocks = false,
  632. .enable_pmu_unk1 = false,
  633. };
  634. static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
  635. .num_phys = 2,
  636. .type = sun4i_a10_phy,
  637. .disc_thresh = 2,
  638. .phyctl_offset = REG_PHYCTL_A10,
  639. .dedicated_clocks = false,
  640. .enable_pmu_unk1 = false,
  641. };
  642. static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
  643. .num_phys = 3,
  644. .type = sun6i_a31_phy,
  645. .disc_thresh = 3,
  646. .phyctl_offset = REG_PHYCTL_A10,
  647. .dedicated_clocks = true,
  648. .enable_pmu_unk1 = false,
  649. };
  650. static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
  651. .num_phys = 3,
  652. .type = sun4i_a10_phy,
  653. .disc_thresh = 2,
  654. .phyctl_offset = REG_PHYCTL_A10,
  655. .dedicated_clocks = false,
  656. .enable_pmu_unk1 = false,
  657. };
  658. static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
  659. .num_phys = 2,
  660. .type = sun4i_a10_phy,
  661. .disc_thresh = 3,
  662. .phyctl_offset = REG_PHYCTL_A10,
  663. .dedicated_clocks = true,
  664. .enable_pmu_unk1 = false,
  665. };
  666. static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
  667. .num_phys = 2,
  668. .type = sun8i_a33_phy,
  669. .disc_thresh = 3,
  670. .phyctl_offset = REG_PHYCTL_A33,
  671. .dedicated_clocks = true,
  672. .enable_pmu_unk1 = false,
  673. };
  674. static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
  675. .num_phys = 4,
  676. .type = sun8i_h3_phy,
  677. .disc_thresh = 3,
  678. .dedicated_clocks = true,
  679. .enable_pmu_unk1 = true,
  680. };
  681. static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
  682. .num_phys = 2,
  683. .type = sun50i_a64_phy,
  684. .disc_thresh = 3,
  685. .phyctl_offset = REG_PHYCTL_A33,
  686. .dedicated_clocks = true,
  687. .enable_pmu_unk1 = true,
  688. };
  689. static const struct of_device_id sun4i_usb_phy_of_match[] = {
  690. { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
  691. { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
  692. { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
  693. { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
  694. { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
  695. { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
  696. { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
  697. { .compatible = "allwinner,sun50i-a64-usb-phy",
  698. .data = &sun50i_a64_cfg},
  699. { },
  700. };
  701. MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
  702. static struct platform_driver sun4i_usb_phy_driver = {
  703. .probe = sun4i_usb_phy_probe,
  704. .remove = sun4i_usb_phy_remove,
  705. .driver = {
  706. .of_match_table = sun4i_usb_phy_of_match,
  707. .name = "sun4i-usb-phy",
  708. }
  709. };
  710. module_platform_driver(sun4i_usb_phy_driver);
  711. MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
  712. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  713. MODULE_LICENSE("GPL v2");