phy-rockchip-inno-usb2.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. * Rockchip USB2.0 PHY with Innosilicon IP block driver
  3. *
  4. * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/delay.h>
  19. #include <linux/extcon.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/mutex.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/phy/phy.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/power_supply.h>
  34. #include <linux/regmap.h>
  35. #include <linux/mfd/syscon.h>
  36. #include <linux/usb/of.h>
  37. #include <linux/usb/otg.h>
  38. #define BIT_WRITEABLE_SHIFT 16
  39. #define SCHEDULE_DELAY (60 * HZ)
  40. #define OTG_SCHEDULE_DELAY (2 * HZ)
  41. enum rockchip_usb2phy_port_id {
  42. USB2PHY_PORT_OTG,
  43. USB2PHY_PORT_HOST,
  44. USB2PHY_NUM_PORTS,
  45. };
  46. enum rockchip_usb2phy_host_state {
  47. PHY_STATE_HS_ONLINE = 0,
  48. PHY_STATE_DISCONNECT = 1,
  49. PHY_STATE_CONNECT = 2,
  50. PHY_STATE_FS_LS_ONLINE = 4,
  51. };
  52. /**
  53. * Different states involved in USB charger detection.
  54. * USB_CHG_STATE_UNDEFINED USB charger is not connected or detection
  55. * process is not yet started.
  56. * USB_CHG_STATE_WAIT_FOR_DCD Waiting for Data pins contact.
  57. * USB_CHG_STATE_DCD_DONE Data pin contact is detected.
  58. * USB_CHG_STATE_PRIMARY_DONE Primary detection is completed (Detects
  59. * between SDP and DCP/CDP).
  60. * USB_CHG_STATE_SECONDARY_DONE Secondary detection is completed (Detects
  61. * between DCP and CDP).
  62. * USB_CHG_STATE_DETECTED USB charger type is determined.
  63. */
  64. enum usb_chg_state {
  65. USB_CHG_STATE_UNDEFINED = 0,
  66. USB_CHG_STATE_WAIT_FOR_DCD,
  67. USB_CHG_STATE_DCD_DONE,
  68. USB_CHG_STATE_PRIMARY_DONE,
  69. USB_CHG_STATE_SECONDARY_DONE,
  70. USB_CHG_STATE_DETECTED,
  71. };
  72. static const unsigned int rockchip_usb2phy_extcon_cable[] = {
  73. EXTCON_USB,
  74. EXTCON_USB_HOST,
  75. EXTCON_CHG_USB_SDP,
  76. EXTCON_CHG_USB_CDP,
  77. EXTCON_CHG_USB_DCP,
  78. EXTCON_CHG_USB_SLOW,
  79. EXTCON_NONE,
  80. };
  81. struct usb2phy_reg {
  82. unsigned int offset;
  83. unsigned int bitend;
  84. unsigned int bitstart;
  85. unsigned int disable;
  86. unsigned int enable;
  87. };
  88. /**
  89. * struct rockchip_chg_det_reg: usb charger detect registers
  90. * @cp_det: charging port detected successfully.
  91. * @dcp_det: dedicated charging port detected successfully.
  92. * @dp_det: assert data pin connect successfully.
  93. * @idm_sink_en: open dm sink curren.
  94. * @idp_sink_en: open dp sink current.
  95. * @idp_src_en: open dm source current.
  96. * @rdm_pdwn_en: open dm pull down resistor.
  97. * @vdm_src_en: open dm voltage source.
  98. * @vdp_src_en: open dp voltage source.
  99. * @opmode: utmi operational mode.
  100. */
  101. struct rockchip_chg_det_reg {
  102. struct usb2phy_reg cp_det;
  103. struct usb2phy_reg dcp_det;
  104. struct usb2phy_reg dp_det;
  105. struct usb2phy_reg idm_sink_en;
  106. struct usb2phy_reg idp_sink_en;
  107. struct usb2phy_reg idp_src_en;
  108. struct usb2phy_reg rdm_pdwn_en;
  109. struct usb2phy_reg vdm_src_en;
  110. struct usb2phy_reg vdp_src_en;
  111. struct usb2phy_reg opmode;
  112. };
  113. /**
  114. * struct rockchip_usb2phy_port_cfg: usb-phy port configuration.
  115. * @phy_sus: phy suspend register.
  116. * @bvalid_det_en: vbus valid rise detection enable register.
  117. * @bvalid_det_st: vbus valid rise detection status register.
  118. * @bvalid_det_clr: vbus valid rise detection clear register.
  119. * @ls_det_en: linestate detection enable register.
  120. * @ls_det_st: linestate detection state register.
  121. * @ls_det_clr: linestate detection clear register.
  122. * @utmi_avalid: utmi vbus avalid status register.
  123. * @utmi_bvalid: utmi vbus bvalid status register.
  124. * @utmi_ls: utmi linestate state register.
  125. * @utmi_hstdet: utmi host disconnect register.
  126. */
  127. struct rockchip_usb2phy_port_cfg {
  128. struct usb2phy_reg phy_sus;
  129. struct usb2phy_reg bvalid_det_en;
  130. struct usb2phy_reg bvalid_det_st;
  131. struct usb2phy_reg bvalid_det_clr;
  132. struct usb2phy_reg ls_det_en;
  133. struct usb2phy_reg ls_det_st;
  134. struct usb2phy_reg ls_det_clr;
  135. struct usb2phy_reg utmi_avalid;
  136. struct usb2phy_reg utmi_bvalid;
  137. struct usb2phy_reg utmi_ls;
  138. struct usb2phy_reg utmi_hstdet;
  139. };
  140. /**
  141. * struct rockchip_usb2phy_cfg: usb-phy configuration.
  142. * @reg: the address offset of grf for usb-phy config.
  143. * @num_ports: specify how many ports that the phy has.
  144. * @clkout_ctl: keep on/turn off output clk of phy.
  145. * @chg_det: charger detection registers.
  146. */
  147. struct rockchip_usb2phy_cfg {
  148. unsigned int reg;
  149. unsigned int num_ports;
  150. struct usb2phy_reg clkout_ctl;
  151. const struct rockchip_usb2phy_port_cfg port_cfgs[USB2PHY_NUM_PORTS];
  152. const struct rockchip_chg_det_reg chg_det;
  153. };
  154. /**
  155. * struct rockchip_usb2phy_port: usb-phy port data.
  156. * @port_id: flag for otg port or host port.
  157. * @suspended: phy suspended flag.
  158. * @utmi_avalid: utmi avalid status usage flag.
  159. * true - use avalid to get vbus status
  160. * flase - use bvalid to get vbus status
  161. * @vbus_attached: otg device vbus status.
  162. * @bvalid_irq: IRQ number assigned for vbus valid rise detection.
  163. * @ls_irq: IRQ number assigned for linestate detection.
  164. * @mutex: for register updating in sm_work.
  165. * @chg_work: charge detect work.
  166. * @otg_sm_work: OTG state machine work.
  167. * @sm_work: HOST state machine work.
  168. * @phy_cfg: port register configuration, assigned by driver data.
  169. * @event_nb: hold event notification callback.
  170. * @state: define OTG enumeration states before device reset.
  171. * @mode: the dr_mode of the controller.
  172. */
  173. struct rockchip_usb2phy_port {
  174. struct phy *phy;
  175. unsigned int port_id;
  176. bool suspended;
  177. bool utmi_avalid;
  178. bool vbus_attached;
  179. int bvalid_irq;
  180. int ls_irq;
  181. struct mutex mutex;
  182. struct delayed_work chg_work;
  183. struct delayed_work otg_sm_work;
  184. struct delayed_work sm_work;
  185. const struct rockchip_usb2phy_port_cfg *port_cfg;
  186. struct notifier_block event_nb;
  187. enum usb_otg_state state;
  188. enum usb_dr_mode mode;
  189. };
  190. /**
  191. * struct rockchip_usb2phy: usb2.0 phy driver data.
  192. * @grf: General Register Files regmap.
  193. * @clk: clock struct of phy input clk.
  194. * @clk480m: clock struct of phy output clk.
  195. * @clk_hw: clock struct of phy output clk management.
  196. * @chg_state: states involved in USB charger detection.
  197. * @chg_type: USB charger types.
  198. * @dcd_retries: The retry count used to track Data contact
  199. * detection process.
  200. * @edev: extcon device for notification registration
  201. * @phy_cfg: phy register configuration, assigned by driver data.
  202. * @ports: phy port instance.
  203. */
  204. struct rockchip_usb2phy {
  205. struct device *dev;
  206. struct regmap *grf;
  207. struct clk *clk;
  208. struct clk *clk480m;
  209. struct clk_hw clk480m_hw;
  210. enum usb_chg_state chg_state;
  211. enum power_supply_type chg_type;
  212. u8 dcd_retries;
  213. struct extcon_dev *edev;
  214. const struct rockchip_usb2phy_cfg *phy_cfg;
  215. struct rockchip_usb2phy_port ports[USB2PHY_NUM_PORTS];
  216. };
  217. static inline int property_enable(struct rockchip_usb2phy *rphy,
  218. const struct usb2phy_reg *reg, bool en)
  219. {
  220. unsigned int val, mask, tmp;
  221. tmp = en ? reg->enable : reg->disable;
  222. mask = GENMASK(reg->bitend, reg->bitstart);
  223. val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
  224. return regmap_write(rphy->grf, reg->offset, val);
  225. }
  226. static inline bool property_enabled(struct rockchip_usb2phy *rphy,
  227. const struct usb2phy_reg *reg)
  228. {
  229. int ret;
  230. unsigned int tmp, orig;
  231. unsigned int mask = GENMASK(reg->bitend, reg->bitstart);
  232. ret = regmap_read(rphy->grf, reg->offset, &orig);
  233. if (ret)
  234. return false;
  235. tmp = (orig & mask) >> reg->bitstart;
  236. return tmp == reg->enable;
  237. }
  238. static int rockchip_usb2phy_clk480m_prepare(struct clk_hw *hw)
  239. {
  240. struct rockchip_usb2phy *rphy =
  241. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  242. int ret;
  243. /* turn on 480m clk output if it is off */
  244. if (!property_enabled(rphy, &rphy->phy_cfg->clkout_ctl)) {
  245. ret = property_enable(rphy, &rphy->phy_cfg->clkout_ctl, true);
  246. if (ret)
  247. return ret;
  248. /* waiting for the clk become stable */
  249. usleep_range(1200, 1300);
  250. }
  251. return 0;
  252. }
  253. static void rockchip_usb2phy_clk480m_unprepare(struct clk_hw *hw)
  254. {
  255. struct rockchip_usb2phy *rphy =
  256. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  257. /* turn off 480m clk output */
  258. property_enable(rphy, &rphy->phy_cfg->clkout_ctl, false);
  259. }
  260. static int rockchip_usb2phy_clk480m_prepared(struct clk_hw *hw)
  261. {
  262. struct rockchip_usb2phy *rphy =
  263. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  264. return property_enabled(rphy, &rphy->phy_cfg->clkout_ctl);
  265. }
  266. static unsigned long
  267. rockchip_usb2phy_clk480m_recalc_rate(struct clk_hw *hw,
  268. unsigned long parent_rate)
  269. {
  270. return 480000000;
  271. }
  272. static const struct clk_ops rockchip_usb2phy_clkout_ops = {
  273. .prepare = rockchip_usb2phy_clk480m_prepare,
  274. .unprepare = rockchip_usb2phy_clk480m_unprepare,
  275. .is_prepared = rockchip_usb2phy_clk480m_prepared,
  276. .recalc_rate = rockchip_usb2phy_clk480m_recalc_rate,
  277. };
  278. static void rockchip_usb2phy_clk480m_unregister(void *data)
  279. {
  280. struct rockchip_usb2phy *rphy = data;
  281. of_clk_del_provider(rphy->dev->of_node);
  282. clk_unregister(rphy->clk480m);
  283. }
  284. static int
  285. rockchip_usb2phy_clk480m_register(struct rockchip_usb2phy *rphy)
  286. {
  287. struct device_node *node = rphy->dev->of_node;
  288. struct clk_init_data init;
  289. const char *clk_name;
  290. int ret;
  291. init.flags = 0;
  292. init.name = "clk_usbphy_480m";
  293. init.ops = &rockchip_usb2phy_clkout_ops;
  294. /* optional override of the clockname */
  295. of_property_read_string(node, "clock-output-names", &init.name);
  296. if (rphy->clk) {
  297. clk_name = __clk_get_name(rphy->clk);
  298. init.parent_names = &clk_name;
  299. init.num_parents = 1;
  300. } else {
  301. init.parent_names = NULL;
  302. init.num_parents = 0;
  303. }
  304. rphy->clk480m_hw.init = &init;
  305. /* register the clock */
  306. rphy->clk480m = clk_register(rphy->dev, &rphy->clk480m_hw);
  307. if (IS_ERR(rphy->clk480m)) {
  308. ret = PTR_ERR(rphy->clk480m);
  309. goto err_ret;
  310. }
  311. ret = of_clk_add_provider(node, of_clk_src_simple_get, rphy->clk480m);
  312. if (ret < 0)
  313. goto err_clk_provider;
  314. ret = devm_add_action(rphy->dev, rockchip_usb2phy_clk480m_unregister,
  315. rphy);
  316. if (ret < 0)
  317. goto err_unreg_action;
  318. return 0;
  319. err_unreg_action:
  320. of_clk_del_provider(node);
  321. err_clk_provider:
  322. clk_unregister(rphy->clk480m);
  323. err_ret:
  324. return ret;
  325. }
  326. static int rockchip_usb2phy_extcon_register(struct rockchip_usb2phy *rphy)
  327. {
  328. int ret;
  329. struct device_node *node = rphy->dev->of_node;
  330. struct extcon_dev *edev;
  331. if (of_property_read_bool(node, "extcon")) {
  332. edev = extcon_get_edev_by_phandle(rphy->dev, 0);
  333. if (IS_ERR(edev)) {
  334. if (PTR_ERR(edev) != -EPROBE_DEFER)
  335. dev_err(rphy->dev, "Invalid or missing extcon\n");
  336. return PTR_ERR(edev);
  337. }
  338. } else {
  339. /* Initialize extcon device */
  340. edev = devm_extcon_dev_allocate(rphy->dev,
  341. rockchip_usb2phy_extcon_cable);
  342. if (IS_ERR(edev))
  343. return -ENOMEM;
  344. ret = devm_extcon_dev_register(rphy->dev, edev);
  345. if (ret) {
  346. dev_err(rphy->dev, "failed to register extcon device\n");
  347. return ret;
  348. }
  349. }
  350. rphy->edev = edev;
  351. return 0;
  352. }
  353. static int rockchip_usb2phy_init(struct phy *phy)
  354. {
  355. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  356. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  357. int ret = 0;
  358. mutex_lock(&rport->mutex);
  359. if (rport->port_id == USB2PHY_PORT_OTG) {
  360. if (rport->mode != USB_DR_MODE_HOST) {
  361. /* clear bvalid status and enable bvalid detect irq */
  362. ret = property_enable(rphy,
  363. &rport->port_cfg->bvalid_det_clr,
  364. true);
  365. if (ret)
  366. goto out;
  367. ret = property_enable(rphy,
  368. &rport->port_cfg->bvalid_det_en,
  369. true);
  370. if (ret)
  371. goto out;
  372. schedule_delayed_work(&rport->otg_sm_work,
  373. OTG_SCHEDULE_DELAY);
  374. } else {
  375. /* If OTG works in host only mode, do nothing. */
  376. dev_dbg(&rport->phy->dev, "mode %d\n", rport->mode);
  377. }
  378. } else if (rport->port_id == USB2PHY_PORT_HOST) {
  379. /* clear linestate and enable linestate detect irq */
  380. ret = property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
  381. if (ret)
  382. goto out;
  383. ret = property_enable(rphy, &rport->port_cfg->ls_det_en, true);
  384. if (ret)
  385. goto out;
  386. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  387. }
  388. out:
  389. mutex_unlock(&rport->mutex);
  390. return ret;
  391. }
  392. static int rockchip_usb2phy_power_on(struct phy *phy)
  393. {
  394. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  395. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  396. int ret;
  397. dev_dbg(&rport->phy->dev, "port power on\n");
  398. if (!rport->suspended)
  399. return 0;
  400. ret = clk_prepare_enable(rphy->clk480m);
  401. if (ret)
  402. return ret;
  403. ret = property_enable(rphy, &rport->port_cfg->phy_sus, false);
  404. if (ret)
  405. return ret;
  406. rport->suspended = false;
  407. return 0;
  408. }
  409. static int rockchip_usb2phy_power_off(struct phy *phy)
  410. {
  411. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  412. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  413. int ret;
  414. dev_dbg(&rport->phy->dev, "port power off\n");
  415. if (rport->suspended)
  416. return 0;
  417. ret = property_enable(rphy, &rport->port_cfg->phy_sus, true);
  418. if (ret)
  419. return ret;
  420. rport->suspended = true;
  421. clk_disable_unprepare(rphy->clk480m);
  422. return 0;
  423. }
  424. static int rockchip_usb2phy_exit(struct phy *phy)
  425. {
  426. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  427. if (rport->port_id == USB2PHY_PORT_OTG &&
  428. rport->mode != USB_DR_MODE_HOST) {
  429. cancel_delayed_work_sync(&rport->otg_sm_work);
  430. cancel_delayed_work_sync(&rport->chg_work);
  431. } else if (rport->port_id == USB2PHY_PORT_HOST)
  432. cancel_delayed_work_sync(&rport->sm_work);
  433. return 0;
  434. }
  435. static const struct phy_ops rockchip_usb2phy_ops = {
  436. .init = rockchip_usb2phy_init,
  437. .exit = rockchip_usb2phy_exit,
  438. .power_on = rockchip_usb2phy_power_on,
  439. .power_off = rockchip_usb2phy_power_off,
  440. .owner = THIS_MODULE,
  441. };
  442. static void rockchip_usb2phy_otg_sm_work(struct work_struct *work)
  443. {
  444. struct rockchip_usb2phy_port *rport =
  445. container_of(work, struct rockchip_usb2phy_port,
  446. otg_sm_work.work);
  447. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  448. static unsigned int cable;
  449. unsigned long delay;
  450. bool vbus_attach, sch_work, notify_charger;
  451. if (rport->utmi_avalid)
  452. vbus_attach =
  453. property_enabled(rphy, &rport->port_cfg->utmi_avalid);
  454. else
  455. vbus_attach =
  456. property_enabled(rphy, &rport->port_cfg->utmi_bvalid);
  457. sch_work = false;
  458. notify_charger = false;
  459. delay = OTG_SCHEDULE_DELAY;
  460. dev_dbg(&rport->phy->dev, "%s otg sm work\n",
  461. usb_otg_state_string(rport->state));
  462. switch (rport->state) {
  463. case OTG_STATE_UNDEFINED:
  464. rport->state = OTG_STATE_B_IDLE;
  465. if (!vbus_attach)
  466. rockchip_usb2phy_power_off(rport->phy);
  467. /* fall through */
  468. case OTG_STATE_B_IDLE:
  469. if (extcon_get_cable_state_(rphy->edev, EXTCON_USB_HOST) > 0) {
  470. dev_dbg(&rport->phy->dev, "usb otg host connect\n");
  471. rport->state = OTG_STATE_A_HOST;
  472. rockchip_usb2phy_power_on(rport->phy);
  473. return;
  474. } else if (vbus_attach) {
  475. dev_dbg(&rport->phy->dev, "vbus_attach\n");
  476. switch (rphy->chg_state) {
  477. case USB_CHG_STATE_UNDEFINED:
  478. schedule_delayed_work(&rport->chg_work, 0);
  479. return;
  480. case USB_CHG_STATE_DETECTED:
  481. switch (rphy->chg_type) {
  482. case POWER_SUPPLY_TYPE_USB:
  483. dev_dbg(&rport->phy->dev,
  484. "sdp cable is connecetd\n");
  485. rockchip_usb2phy_power_on(rport->phy);
  486. rport->state = OTG_STATE_B_PERIPHERAL;
  487. notify_charger = true;
  488. sch_work = true;
  489. cable = EXTCON_CHG_USB_SDP;
  490. break;
  491. case POWER_SUPPLY_TYPE_USB_DCP:
  492. dev_dbg(&rport->phy->dev,
  493. "dcp cable is connecetd\n");
  494. rockchip_usb2phy_power_off(rport->phy);
  495. notify_charger = true;
  496. sch_work = true;
  497. cable = EXTCON_CHG_USB_DCP;
  498. break;
  499. case POWER_SUPPLY_TYPE_USB_CDP:
  500. dev_dbg(&rport->phy->dev,
  501. "cdp cable is connecetd\n");
  502. rockchip_usb2phy_power_on(rport->phy);
  503. rport->state = OTG_STATE_B_PERIPHERAL;
  504. notify_charger = true;
  505. sch_work = true;
  506. cable = EXTCON_CHG_USB_CDP;
  507. break;
  508. default:
  509. break;
  510. }
  511. break;
  512. default:
  513. break;
  514. }
  515. } else {
  516. notify_charger = true;
  517. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  518. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  519. }
  520. if (rport->vbus_attached != vbus_attach) {
  521. rport->vbus_attached = vbus_attach;
  522. if (notify_charger && rphy->edev) {
  523. extcon_set_cable_state_(rphy->edev,
  524. cable, vbus_attach);
  525. if (cable == EXTCON_CHG_USB_SDP)
  526. extcon_set_state_sync(rphy->edev,
  527. EXTCON_USB,
  528. vbus_attach);
  529. }
  530. }
  531. break;
  532. case OTG_STATE_B_PERIPHERAL:
  533. if (!vbus_attach) {
  534. dev_dbg(&rport->phy->dev, "usb disconnect\n");
  535. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  536. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  537. rport->state = OTG_STATE_B_IDLE;
  538. delay = 0;
  539. rockchip_usb2phy_power_off(rport->phy);
  540. }
  541. sch_work = true;
  542. break;
  543. case OTG_STATE_A_HOST:
  544. if (extcon_get_cable_state_(rphy->edev, EXTCON_USB_HOST) == 0) {
  545. dev_dbg(&rport->phy->dev, "usb otg host disconnect\n");
  546. rport->state = OTG_STATE_B_IDLE;
  547. rockchip_usb2phy_power_off(rport->phy);
  548. }
  549. break;
  550. default:
  551. break;
  552. }
  553. if (sch_work)
  554. schedule_delayed_work(&rport->otg_sm_work, delay);
  555. }
  556. static const char *chg_to_string(enum power_supply_type chg_type)
  557. {
  558. switch (chg_type) {
  559. case POWER_SUPPLY_TYPE_USB:
  560. return "USB_SDP_CHARGER";
  561. case POWER_SUPPLY_TYPE_USB_DCP:
  562. return "USB_DCP_CHARGER";
  563. case POWER_SUPPLY_TYPE_USB_CDP:
  564. return "USB_CDP_CHARGER";
  565. default:
  566. return "INVALID_CHARGER";
  567. }
  568. }
  569. static void rockchip_chg_enable_dcd(struct rockchip_usb2phy *rphy,
  570. bool en)
  571. {
  572. property_enable(rphy, &rphy->phy_cfg->chg_det.rdm_pdwn_en, en);
  573. property_enable(rphy, &rphy->phy_cfg->chg_det.idp_src_en, en);
  574. }
  575. static void rockchip_chg_enable_primary_det(struct rockchip_usb2phy *rphy,
  576. bool en)
  577. {
  578. property_enable(rphy, &rphy->phy_cfg->chg_det.vdp_src_en, en);
  579. property_enable(rphy, &rphy->phy_cfg->chg_det.idm_sink_en, en);
  580. }
  581. static void rockchip_chg_enable_secondary_det(struct rockchip_usb2phy *rphy,
  582. bool en)
  583. {
  584. property_enable(rphy, &rphy->phy_cfg->chg_det.vdm_src_en, en);
  585. property_enable(rphy, &rphy->phy_cfg->chg_det.idp_sink_en, en);
  586. }
  587. #define CHG_DCD_POLL_TIME (100 * HZ / 1000)
  588. #define CHG_DCD_MAX_RETRIES 6
  589. #define CHG_PRIMARY_DET_TIME (40 * HZ / 1000)
  590. #define CHG_SECONDARY_DET_TIME (40 * HZ / 1000)
  591. static void rockchip_chg_detect_work(struct work_struct *work)
  592. {
  593. struct rockchip_usb2phy_port *rport =
  594. container_of(work, struct rockchip_usb2phy_port, chg_work.work);
  595. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  596. bool is_dcd, tmout, vout;
  597. unsigned long delay;
  598. dev_dbg(&rport->phy->dev, "chg detection work state = %d\n",
  599. rphy->chg_state);
  600. switch (rphy->chg_state) {
  601. case USB_CHG_STATE_UNDEFINED:
  602. if (!rport->suspended)
  603. rockchip_usb2phy_power_off(rport->phy);
  604. /* put the controller in non-driving mode */
  605. property_enable(rphy, &rphy->phy_cfg->chg_det.opmode, false);
  606. /* Start DCD processing stage 1 */
  607. rockchip_chg_enable_dcd(rphy, true);
  608. rphy->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  609. rphy->dcd_retries = 0;
  610. delay = CHG_DCD_POLL_TIME;
  611. break;
  612. case USB_CHG_STATE_WAIT_FOR_DCD:
  613. /* get data contact detection status */
  614. is_dcd = property_enabled(rphy, &rphy->phy_cfg->chg_det.dp_det);
  615. tmout = ++rphy->dcd_retries == CHG_DCD_MAX_RETRIES;
  616. /* stage 2 */
  617. if (is_dcd || tmout) {
  618. /* stage 4 */
  619. /* Turn off DCD circuitry */
  620. rockchip_chg_enable_dcd(rphy, false);
  621. /* Voltage Source on DP, Probe on DM */
  622. rockchip_chg_enable_primary_det(rphy, true);
  623. delay = CHG_PRIMARY_DET_TIME;
  624. rphy->chg_state = USB_CHG_STATE_DCD_DONE;
  625. } else {
  626. /* stage 3 */
  627. delay = CHG_DCD_POLL_TIME;
  628. }
  629. break;
  630. case USB_CHG_STATE_DCD_DONE:
  631. vout = property_enabled(rphy, &rphy->phy_cfg->chg_det.cp_det);
  632. rockchip_chg_enable_primary_det(rphy, false);
  633. if (vout) {
  634. /* Voltage Source on DM, Probe on DP */
  635. rockchip_chg_enable_secondary_det(rphy, true);
  636. delay = CHG_SECONDARY_DET_TIME;
  637. rphy->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  638. } else {
  639. if (rphy->dcd_retries == CHG_DCD_MAX_RETRIES) {
  640. /* floating charger found */
  641. rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  642. rphy->chg_state = USB_CHG_STATE_DETECTED;
  643. delay = 0;
  644. } else {
  645. rphy->chg_type = POWER_SUPPLY_TYPE_USB;
  646. rphy->chg_state = USB_CHG_STATE_DETECTED;
  647. delay = 0;
  648. }
  649. }
  650. break;
  651. case USB_CHG_STATE_PRIMARY_DONE:
  652. vout = property_enabled(rphy, &rphy->phy_cfg->chg_det.dcp_det);
  653. /* Turn off voltage source */
  654. rockchip_chg_enable_secondary_det(rphy, false);
  655. if (vout)
  656. rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  657. else
  658. rphy->chg_type = POWER_SUPPLY_TYPE_USB_CDP;
  659. /* fall through */
  660. case USB_CHG_STATE_SECONDARY_DONE:
  661. rphy->chg_state = USB_CHG_STATE_DETECTED;
  662. delay = 0;
  663. /* fall through */
  664. case USB_CHG_STATE_DETECTED:
  665. /* put the controller in normal mode */
  666. property_enable(rphy, &rphy->phy_cfg->chg_det.opmode, true);
  667. rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
  668. dev_info(&rport->phy->dev, "charger = %s\n",
  669. chg_to_string(rphy->chg_type));
  670. return;
  671. default:
  672. return;
  673. }
  674. schedule_delayed_work(&rport->chg_work, delay);
  675. }
  676. /*
  677. * The function manage host-phy port state and suspend/resume phy port
  678. * to save power.
  679. *
  680. * we rely on utmi_linestate and utmi_hostdisconnect to identify whether
  681. * devices is disconnect or not. Besides, we do not need care it is FS/LS
  682. * disconnected or HS disconnected, actually, we just only need get the
  683. * device is disconnected at last through rearm the delayed work,
  684. * to suspend the phy port in _PHY_STATE_DISCONNECT_ case.
  685. *
  686. * NOTE: It may invoke *phy_powr_off or *phy_power_on which will invoke
  687. * some clk related APIs, so do not invoke it from interrupt context directly.
  688. */
  689. static void rockchip_usb2phy_sm_work(struct work_struct *work)
  690. {
  691. struct rockchip_usb2phy_port *rport =
  692. container_of(work, struct rockchip_usb2phy_port, sm_work.work);
  693. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  694. unsigned int sh = rport->port_cfg->utmi_hstdet.bitend -
  695. rport->port_cfg->utmi_hstdet.bitstart + 1;
  696. unsigned int ul, uhd, state;
  697. unsigned int ul_mask, uhd_mask;
  698. int ret;
  699. mutex_lock(&rport->mutex);
  700. ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
  701. if (ret < 0)
  702. goto next_schedule;
  703. ret = regmap_read(rphy->grf, rport->port_cfg->utmi_hstdet.offset,
  704. &uhd);
  705. if (ret < 0)
  706. goto next_schedule;
  707. uhd_mask = GENMASK(rport->port_cfg->utmi_hstdet.bitend,
  708. rport->port_cfg->utmi_hstdet.bitstart);
  709. ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
  710. rport->port_cfg->utmi_ls.bitstart);
  711. /* stitch on utmi_ls and utmi_hstdet as phy state */
  712. state = ((uhd & uhd_mask) >> rport->port_cfg->utmi_hstdet.bitstart) |
  713. (((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << sh);
  714. switch (state) {
  715. case PHY_STATE_HS_ONLINE:
  716. dev_dbg(&rport->phy->dev, "HS online\n");
  717. break;
  718. case PHY_STATE_FS_LS_ONLINE:
  719. /*
  720. * For FS/LS device, the online state share with connect state
  721. * from utmi_ls and utmi_hstdet register, so we distinguish
  722. * them via suspended flag.
  723. *
  724. * Plus, there are two cases, one is D- Line pull-up, and D+
  725. * line pull-down, the state is 4; another is D+ line pull-up,
  726. * and D- line pull-down, the state is 2.
  727. */
  728. if (!rport->suspended) {
  729. /* D- line pull-up, D+ line pull-down */
  730. dev_dbg(&rport->phy->dev, "FS/LS online\n");
  731. break;
  732. }
  733. /* fall through */
  734. case PHY_STATE_CONNECT:
  735. if (rport->suspended) {
  736. dev_dbg(&rport->phy->dev, "Connected\n");
  737. rockchip_usb2phy_power_on(rport->phy);
  738. rport->suspended = false;
  739. } else {
  740. /* D+ line pull-up, D- line pull-down */
  741. dev_dbg(&rport->phy->dev, "FS/LS online\n");
  742. }
  743. break;
  744. case PHY_STATE_DISCONNECT:
  745. if (!rport->suspended) {
  746. dev_dbg(&rport->phy->dev, "Disconnected\n");
  747. rockchip_usb2phy_power_off(rport->phy);
  748. rport->suspended = true;
  749. }
  750. /*
  751. * activate the linestate detection to get the next device
  752. * plug-in irq.
  753. */
  754. property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
  755. property_enable(rphy, &rport->port_cfg->ls_det_en, true);
  756. /*
  757. * we don't need to rearm the delayed work when the phy port
  758. * is suspended.
  759. */
  760. mutex_unlock(&rport->mutex);
  761. return;
  762. default:
  763. dev_dbg(&rport->phy->dev, "unknown phy state\n");
  764. break;
  765. }
  766. next_schedule:
  767. mutex_unlock(&rport->mutex);
  768. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  769. }
  770. static irqreturn_t rockchip_usb2phy_linestate_irq(int irq, void *data)
  771. {
  772. struct rockchip_usb2phy_port *rport = data;
  773. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  774. if (!property_enabled(rphy, &rport->port_cfg->ls_det_st))
  775. return IRQ_NONE;
  776. mutex_lock(&rport->mutex);
  777. /* disable linestate detect irq and clear its status */
  778. property_enable(rphy, &rport->port_cfg->ls_det_en, false);
  779. property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
  780. mutex_unlock(&rport->mutex);
  781. /*
  782. * In this case for host phy port, a new device is plugged in,
  783. * meanwhile, if the phy port is suspended, we need rearm the work to
  784. * resume it and mange its states; otherwise, we do nothing about that.
  785. */
  786. if (rport->suspended && rport->port_id == USB2PHY_PORT_HOST)
  787. rockchip_usb2phy_sm_work(&rport->sm_work.work);
  788. return IRQ_HANDLED;
  789. }
  790. static irqreturn_t rockchip_usb2phy_bvalid_irq(int irq, void *data)
  791. {
  792. struct rockchip_usb2phy_port *rport = data;
  793. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  794. if (!property_enabled(rphy, &rport->port_cfg->bvalid_det_st))
  795. return IRQ_NONE;
  796. mutex_lock(&rport->mutex);
  797. /* clear bvalid detect irq pending status */
  798. property_enable(rphy, &rport->port_cfg->bvalid_det_clr, true);
  799. mutex_unlock(&rport->mutex);
  800. rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
  801. return IRQ_HANDLED;
  802. }
  803. static int rockchip_usb2phy_host_port_init(struct rockchip_usb2phy *rphy,
  804. struct rockchip_usb2phy_port *rport,
  805. struct device_node *child_np)
  806. {
  807. int ret;
  808. rport->port_id = USB2PHY_PORT_HOST;
  809. rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST];
  810. rport->suspended = true;
  811. mutex_init(&rport->mutex);
  812. INIT_DELAYED_WORK(&rport->sm_work, rockchip_usb2phy_sm_work);
  813. rport->ls_irq = of_irq_get_byname(child_np, "linestate");
  814. if (rport->ls_irq < 0) {
  815. dev_err(rphy->dev, "no linestate irq provided\n");
  816. return rport->ls_irq;
  817. }
  818. ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
  819. rockchip_usb2phy_linestate_irq,
  820. IRQF_ONESHOT,
  821. "rockchip_usb2phy", rport);
  822. if (ret) {
  823. dev_err(rphy->dev, "failed to request linestate irq handle\n");
  824. return ret;
  825. }
  826. return 0;
  827. }
  828. static int rockchip_otg_event(struct notifier_block *nb,
  829. unsigned long event, void *ptr)
  830. {
  831. struct rockchip_usb2phy_port *rport =
  832. container_of(nb, struct rockchip_usb2phy_port, event_nb);
  833. schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
  834. return NOTIFY_DONE;
  835. }
  836. static int rockchip_usb2phy_otg_port_init(struct rockchip_usb2phy *rphy,
  837. struct rockchip_usb2phy_port *rport,
  838. struct device_node *child_np)
  839. {
  840. int ret;
  841. rport->port_id = USB2PHY_PORT_OTG;
  842. rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG];
  843. rport->state = OTG_STATE_UNDEFINED;
  844. /*
  845. * set suspended flag to true, but actually don't
  846. * put phy in suspend mode, it aims to enable usb
  847. * phy and clock in power_on() called by usb controller
  848. * driver during probe.
  849. */
  850. rport->suspended = true;
  851. rport->vbus_attached = false;
  852. mutex_init(&rport->mutex);
  853. rport->mode = of_usb_get_dr_mode_by_phy(child_np, -1);
  854. if (rport->mode == USB_DR_MODE_HOST) {
  855. ret = 0;
  856. goto out;
  857. }
  858. INIT_DELAYED_WORK(&rport->chg_work, rockchip_chg_detect_work);
  859. INIT_DELAYED_WORK(&rport->otg_sm_work, rockchip_usb2phy_otg_sm_work);
  860. rport->utmi_avalid =
  861. of_property_read_bool(child_np, "rockchip,utmi-avalid");
  862. rport->bvalid_irq = of_irq_get_byname(child_np, "otg-bvalid");
  863. if (rport->bvalid_irq < 0) {
  864. dev_err(rphy->dev, "no vbus valid irq provided\n");
  865. ret = rport->bvalid_irq;
  866. goto out;
  867. }
  868. ret = devm_request_threaded_irq(rphy->dev, rport->bvalid_irq, NULL,
  869. rockchip_usb2phy_bvalid_irq,
  870. IRQF_ONESHOT,
  871. "rockchip_usb2phy_bvalid", rport);
  872. if (ret) {
  873. dev_err(rphy->dev, "failed to request otg-bvalid irq handle\n");
  874. goto out;
  875. }
  876. if (!IS_ERR(rphy->edev)) {
  877. rport->event_nb.notifier_call = rockchip_otg_event;
  878. ret = extcon_register_notifier(rphy->edev, EXTCON_USB_HOST,
  879. &rport->event_nb);
  880. if (ret)
  881. dev_err(rphy->dev, "register USB HOST notifier failed\n");
  882. }
  883. out:
  884. return ret;
  885. }
  886. static int rockchip_usb2phy_probe(struct platform_device *pdev)
  887. {
  888. struct device *dev = &pdev->dev;
  889. struct device_node *np = dev->of_node;
  890. struct device_node *child_np;
  891. struct phy_provider *provider;
  892. struct rockchip_usb2phy *rphy;
  893. const struct rockchip_usb2phy_cfg *phy_cfgs;
  894. const struct of_device_id *match;
  895. unsigned int reg;
  896. int index, ret;
  897. rphy = devm_kzalloc(dev, sizeof(*rphy), GFP_KERNEL);
  898. if (!rphy)
  899. return -ENOMEM;
  900. match = of_match_device(dev->driver->of_match_table, dev);
  901. if (!match || !match->data) {
  902. dev_err(dev, "phy configs are not assigned!\n");
  903. return -EINVAL;
  904. }
  905. if (!dev->parent || !dev->parent->of_node)
  906. return -EINVAL;
  907. rphy->grf = syscon_node_to_regmap(dev->parent->of_node);
  908. if (IS_ERR(rphy->grf))
  909. return PTR_ERR(rphy->grf);
  910. if (of_property_read_u32(np, "reg", &reg)) {
  911. dev_err(dev, "the reg property is not assigned in %s node\n",
  912. np->name);
  913. return -EINVAL;
  914. }
  915. rphy->dev = dev;
  916. phy_cfgs = match->data;
  917. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  918. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  919. platform_set_drvdata(pdev, rphy);
  920. ret = rockchip_usb2phy_extcon_register(rphy);
  921. if (ret)
  922. return ret;
  923. /* find out a proper config which can be matched with dt. */
  924. index = 0;
  925. while (phy_cfgs[index].reg) {
  926. if (phy_cfgs[index].reg == reg) {
  927. rphy->phy_cfg = &phy_cfgs[index];
  928. break;
  929. }
  930. ++index;
  931. }
  932. if (!rphy->phy_cfg) {
  933. dev_err(dev, "no phy-config can be matched with %s node\n",
  934. np->name);
  935. return -EINVAL;
  936. }
  937. rphy->clk = of_clk_get_by_name(np, "phyclk");
  938. if (!IS_ERR(rphy->clk)) {
  939. clk_prepare_enable(rphy->clk);
  940. } else {
  941. dev_info(&pdev->dev, "no phyclk specified\n");
  942. rphy->clk = NULL;
  943. }
  944. ret = rockchip_usb2phy_clk480m_register(rphy);
  945. if (ret) {
  946. dev_err(dev, "failed to register 480m output clock\n");
  947. goto disable_clks;
  948. }
  949. index = 0;
  950. for_each_available_child_of_node(np, child_np) {
  951. struct rockchip_usb2phy_port *rport = &rphy->ports[index];
  952. struct phy *phy;
  953. /* This driver aims to support both otg-port and host-port */
  954. if (of_node_cmp(child_np->name, "host-port") &&
  955. of_node_cmp(child_np->name, "otg-port"))
  956. goto next_child;
  957. phy = devm_phy_create(dev, child_np, &rockchip_usb2phy_ops);
  958. if (IS_ERR(phy)) {
  959. dev_err(dev, "failed to create phy\n");
  960. ret = PTR_ERR(phy);
  961. goto put_child;
  962. }
  963. rport->phy = phy;
  964. phy_set_drvdata(rport->phy, rport);
  965. /* initialize otg/host port separately */
  966. if (!of_node_cmp(child_np->name, "host-port")) {
  967. ret = rockchip_usb2phy_host_port_init(rphy, rport,
  968. child_np);
  969. if (ret)
  970. goto put_child;
  971. } else {
  972. ret = rockchip_usb2phy_otg_port_init(rphy, rport,
  973. child_np);
  974. if (ret)
  975. goto put_child;
  976. }
  977. next_child:
  978. /* to prevent out of boundary */
  979. if (++index >= rphy->phy_cfg->num_ports)
  980. break;
  981. }
  982. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  983. return PTR_ERR_OR_ZERO(provider);
  984. put_child:
  985. of_node_put(child_np);
  986. disable_clks:
  987. if (rphy->clk) {
  988. clk_disable_unprepare(rphy->clk);
  989. clk_put(rphy->clk);
  990. }
  991. return ret;
  992. }
  993. static const struct rockchip_usb2phy_cfg rk3366_phy_cfgs[] = {
  994. {
  995. .reg = 0x700,
  996. .num_ports = 2,
  997. .clkout_ctl = { 0x0724, 15, 15, 1, 0 },
  998. .port_cfgs = {
  999. [USB2PHY_PORT_HOST] = {
  1000. .phy_sus = { 0x0728, 15, 0, 0, 0x1d1 },
  1001. .ls_det_en = { 0x0680, 4, 4, 0, 1 },
  1002. .ls_det_st = { 0x0690, 4, 4, 0, 1 },
  1003. .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
  1004. .utmi_ls = { 0x049c, 14, 13, 0, 1 },
  1005. .utmi_hstdet = { 0x049c, 12, 12, 0, 1 }
  1006. }
  1007. },
  1008. },
  1009. { /* sentinel */ }
  1010. };
  1011. static const struct rockchip_usb2phy_cfg rk3399_phy_cfgs[] = {
  1012. {
  1013. .reg = 0xe450,
  1014. .num_ports = 2,
  1015. .clkout_ctl = { 0xe450, 4, 4, 1, 0 },
  1016. .port_cfgs = {
  1017. [USB2PHY_PORT_OTG] = {
  1018. .phy_sus = { 0xe454, 1, 0, 2, 1 },
  1019. .bvalid_det_en = { 0xe3c0, 3, 3, 0, 1 },
  1020. .bvalid_det_st = { 0xe3e0, 3, 3, 0, 1 },
  1021. .bvalid_det_clr = { 0xe3d0, 3, 3, 0, 1 },
  1022. .utmi_avalid = { 0xe2ac, 7, 7, 0, 1 },
  1023. .utmi_bvalid = { 0xe2ac, 12, 12, 0, 1 },
  1024. },
  1025. [USB2PHY_PORT_HOST] = {
  1026. .phy_sus = { 0xe458, 1, 0, 0x2, 0x1 },
  1027. .ls_det_en = { 0xe3c0, 6, 6, 0, 1 },
  1028. .ls_det_st = { 0xe3e0, 6, 6, 0, 1 },
  1029. .ls_det_clr = { 0xe3d0, 6, 6, 0, 1 },
  1030. .utmi_ls = { 0xe2ac, 22, 21, 0, 1 },
  1031. .utmi_hstdet = { 0xe2ac, 23, 23, 0, 1 }
  1032. }
  1033. },
  1034. .chg_det = {
  1035. .opmode = { 0xe454, 3, 0, 5, 1 },
  1036. .cp_det = { 0xe2ac, 2, 2, 0, 1 },
  1037. .dcp_det = { 0xe2ac, 1, 1, 0, 1 },
  1038. .dp_det = { 0xe2ac, 0, 0, 0, 1 },
  1039. .idm_sink_en = { 0xe450, 8, 8, 0, 1 },
  1040. .idp_sink_en = { 0xe450, 7, 7, 0, 1 },
  1041. .idp_src_en = { 0xe450, 9, 9, 0, 1 },
  1042. .rdm_pdwn_en = { 0xe450, 10, 10, 0, 1 },
  1043. .vdm_src_en = { 0xe450, 12, 12, 0, 1 },
  1044. .vdp_src_en = { 0xe450, 11, 11, 0, 1 },
  1045. },
  1046. },
  1047. {
  1048. .reg = 0xe460,
  1049. .num_ports = 2,
  1050. .clkout_ctl = { 0xe460, 4, 4, 1, 0 },
  1051. .port_cfgs = {
  1052. [USB2PHY_PORT_OTG] = {
  1053. .phy_sus = { 0xe464, 1, 0, 2, 1 },
  1054. .bvalid_det_en = { 0xe3c0, 8, 8, 0, 1 },
  1055. .bvalid_det_st = { 0xe3e0, 8, 8, 0, 1 },
  1056. .bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 },
  1057. .utmi_avalid = { 0xe2ac, 10, 10, 0, 1 },
  1058. .utmi_bvalid = { 0xe2ac, 16, 16, 0, 1 },
  1059. },
  1060. [USB2PHY_PORT_HOST] = {
  1061. .phy_sus = { 0xe468, 1, 0, 0x2, 0x1 },
  1062. .ls_det_en = { 0xe3c0, 11, 11, 0, 1 },
  1063. .ls_det_st = { 0xe3e0, 11, 11, 0, 1 },
  1064. .ls_det_clr = { 0xe3d0, 11, 11, 0, 1 },
  1065. .utmi_ls = { 0xe2ac, 26, 25, 0, 1 },
  1066. .utmi_hstdet = { 0xe2ac, 27, 27, 0, 1 }
  1067. }
  1068. },
  1069. },
  1070. { /* sentinel */ }
  1071. };
  1072. static const struct of_device_id rockchip_usb2phy_dt_match[] = {
  1073. { .compatible = "rockchip,rk3366-usb2phy", .data = &rk3366_phy_cfgs },
  1074. { .compatible = "rockchip,rk3399-usb2phy", .data = &rk3399_phy_cfgs },
  1075. {}
  1076. };
  1077. MODULE_DEVICE_TABLE(of, rockchip_usb2phy_dt_match);
  1078. static struct platform_driver rockchip_usb2phy_driver = {
  1079. .probe = rockchip_usb2phy_probe,
  1080. .driver = {
  1081. .name = "rockchip-usb2phy",
  1082. .of_match_table = rockchip_usb2phy_dt_match,
  1083. },
  1084. };
  1085. module_platform_driver(rockchip_usb2phy_driver);
  1086. MODULE_AUTHOR("Frank Wang <frank.wang@rock-chips.com>");
  1087. MODULE_DESCRIPTION("Rockchip USB2.0 PHY driver");
  1088. MODULE_LICENSE("GPL v2");