phy-sun4i-usb.c 22 KB

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