phy-rockchip-inno-usb2.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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, "sdp cable is connected\n");
  484. rockchip_usb2phy_power_on(rport->phy);
  485. rport->state = OTG_STATE_B_PERIPHERAL;
  486. notify_charger = true;
  487. sch_work = true;
  488. cable = EXTCON_CHG_USB_SDP;
  489. break;
  490. case POWER_SUPPLY_TYPE_USB_DCP:
  491. dev_dbg(&rport->phy->dev, "dcp cable is connected\n");
  492. rockchip_usb2phy_power_off(rport->phy);
  493. notify_charger = true;
  494. sch_work = true;
  495. cable = EXTCON_CHG_USB_DCP;
  496. break;
  497. case POWER_SUPPLY_TYPE_USB_CDP:
  498. dev_dbg(&rport->phy->dev, "cdp cable is connected\n");
  499. rockchip_usb2phy_power_on(rport->phy);
  500. rport->state = OTG_STATE_B_PERIPHERAL;
  501. notify_charger = true;
  502. sch_work = true;
  503. cable = EXTCON_CHG_USB_CDP;
  504. break;
  505. default:
  506. break;
  507. }
  508. break;
  509. default:
  510. break;
  511. }
  512. } else {
  513. notify_charger = true;
  514. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  515. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  516. }
  517. if (rport->vbus_attached != vbus_attach) {
  518. rport->vbus_attached = vbus_attach;
  519. if (notify_charger && rphy->edev) {
  520. extcon_set_cable_state_(rphy->edev,
  521. cable, vbus_attach);
  522. if (cable == EXTCON_CHG_USB_SDP)
  523. extcon_set_state_sync(rphy->edev,
  524. EXTCON_USB,
  525. vbus_attach);
  526. }
  527. }
  528. break;
  529. case OTG_STATE_B_PERIPHERAL:
  530. if (!vbus_attach) {
  531. dev_dbg(&rport->phy->dev, "usb disconnect\n");
  532. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  533. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  534. rport->state = OTG_STATE_B_IDLE;
  535. delay = 0;
  536. rockchip_usb2phy_power_off(rport->phy);
  537. }
  538. sch_work = true;
  539. break;
  540. case OTG_STATE_A_HOST:
  541. if (extcon_get_cable_state_(rphy->edev, EXTCON_USB_HOST) == 0) {
  542. dev_dbg(&rport->phy->dev, "usb otg host disconnect\n");
  543. rport->state = OTG_STATE_B_IDLE;
  544. rockchip_usb2phy_power_off(rport->phy);
  545. }
  546. break;
  547. default:
  548. break;
  549. }
  550. if (sch_work)
  551. schedule_delayed_work(&rport->otg_sm_work, delay);
  552. }
  553. static const char *chg_to_string(enum power_supply_type chg_type)
  554. {
  555. switch (chg_type) {
  556. case POWER_SUPPLY_TYPE_USB:
  557. return "USB_SDP_CHARGER";
  558. case POWER_SUPPLY_TYPE_USB_DCP:
  559. return "USB_DCP_CHARGER";
  560. case POWER_SUPPLY_TYPE_USB_CDP:
  561. return "USB_CDP_CHARGER";
  562. default:
  563. return "INVALID_CHARGER";
  564. }
  565. }
  566. static void rockchip_chg_enable_dcd(struct rockchip_usb2phy *rphy,
  567. bool en)
  568. {
  569. property_enable(rphy, &rphy->phy_cfg->chg_det.rdm_pdwn_en, en);
  570. property_enable(rphy, &rphy->phy_cfg->chg_det.idp_src_en, en);
  571. }
  572. static void rockchip_chg_enable_primary_det(struct rockchip_usb2phy *rphy,
  573. bool en)
  574. {
  575. property_enable(rphy, &rphy->phy_cfg->chg_det.vdp_src_en, en);
  576. property_enable(rphy, &rphy->phy_cfg->chg_det.idm_sink_en, en);
  577. }
  578. static void rockchip_chg_enable_secondary_det(struct rockchip_usb2phy *rphy,
  579. bool en)
  580. {
  581. property_enable(rphy, &rphy->phy_cfg->chg_det.vdm_src_en, en);
  582. property_enable(rphy, &rphy->phy_cfg->chg_det.idp_sink_en, en);
  583. }
  584. #define CHG_DCD_POLL_TIME (100 * HZ / 1000)
  585. #define CHG_DCD_MAX_RETRIES 6
  586. #define CHG_PRIMARY_DET_TIME (40 * HZ / 1000)
  587. #define CHG_SECONDARY_DET_TIME (40 * HZ / 1000)
  588. static void rockchip_chg_detect_work(struct work_struct *work)
  589. {
  590. struct rockchip_usb2phy_port *rport =
  591. container_of(work, struct rockchip_usb2phy_port, chg_work.work);
  592. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  593. bool is_dcd, tmout, vout;
  594. unsigned long delay;
  595. dev_dbg(&rport->phy->dev, "chg detection work state = %d\n",
  596. rphy->chg_state);
  597. switch (rphy->chg_state) {
  598. case USB_CHG_STATE_UNDEFINED:
  599. if (!rport->suspended)
  600. rockchip_usb2phy_power_off(rport->phy);
  601. /* put the controller in non-driving mode */
  602. property_enable(rphy, &rphy->phy_cfg->chg_det.opmode, false);
  603. /* Start DCD processing stage 1 */
  604. rockchip_chg_enable_dcd(rphy, true);
  605. rphy->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  606. rphy->dcd_retries = 0;
  607. delay = CHG_DCD_POLL_TIME;
  608. break;
  609. case USB_CHG_STATE_WAIT_FOR_DCD:
  610. /* get data contact detection status */
  611. is_dcd = property_enabled(rphy, &rphy->phy_cfg->chg_det.dp_det);
  612. tmout = ++rphy->dcd_retries == CHG_DCD_MAX_RETRIES;
  613. /* stage 2 */
  614. if (is_dcd || tmout) {
  615. /* stage 4 */
  616. /* Turn off DCD circuitry */
  617. rockchip_chg_enable_dcd(rphy, false);
  618. /* Voltage Source on DP, Probe on DM */
  619. rockchip_chg_enable_primary_det(rphy, true);
  620. delay = CHG_PRIMARY_DET_TIME;
  621. rphy->chg_state = USB_CHG_STATE_DCD_DONE;
  622. } else {
  623. /* stage 3 */
  624. delay = CHG_DCD_POLL_TIME;
  625. }
  626. break;
  627. case USB_CHG_STATE_DCD_DONE:
  628. vout = property_enabled(rphy, &rphy->phy_cfg->chg_det.cp_det);
  629. rockchip_chg_enable_primary_det(rphy, false);
  630. if (vout) {
  631. /* Voltage Source on DM, Probe on DP */
  632. rockchip_chg_enable_secondary_det(rphy, true);
  633. delay = CHG_SECONDARY_DET_TIME;
  634. rphy->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  635. } else {
  636. if (rphy->dcd_retries == CHG_DCD_MAX_RETRIES) {
  637. /* floating charger found */
  638. rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  639. rphy->chg_state = USB_CHG_STATE_DETECTED;
  640. delay = 0;
  641. } else {
  642. rphy->chg_type = POWER_SUPPLY_TYPE_USB;
  643. rphy->chg_state = USB_CHG_STATE_DETECTED;
  644. delay = 0;
  645. }
  646. }
  647. break;
  648. case USB_CHG_STATE_PRIMARY_DONE:
  649. vout = property_enabled(rphy, &rphy->phy_cfg->chg_det.dcp_det);
  650. /* Turn off voltage source */
  651. rockchip_chg_enable_secondary_det(rphy, false);
  652. if (vout)
  653. rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  654. else
  655. rphy->chg_type = POWER_SUPPLY_TYPE_USB_CDP;
  656. /* fall through */
  657. case USB_CHG_STATE_SECONDARY_DONE:
  658. rphy->chg_state = USB_CHG_STATE_DETECTED;
  659. delay = 0;
  660. /* fall through */
  661. case USB_CHG_STATE_DETECTED:
  662. /* put the controller in normal mode */
  663. property_enable(rphy, &rphy->phy_cfg->chg_det.opmode, true);
  664. rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
  665. dev_info(&rport->phy->dev, "charger = %s\n",
  666. chg_to_string(rphy->chg_type));
  667. return;
  668. default:
  669. return;
  670. }
  671. schedule_delayed_work(&rport->chg_work, delay);
  672. }
  673. /*
  674. * The function manage host-phy port state and suspend/resume phy port
  675. * to save power.
  676. *
  677. * we rely on utmi_linestate and utmi_hostdisconnect to identify whether
  678. * devices is disconnect or not. Besides, we do not need care it is FS/LS
  679. * disconnected or HS disconnected, actually, we just only need get the
  680. * device is disconnected at last through rearm the delayed work,
  681. * to suspend the phy port in _PHY_STATE_DISCONNECT_ case.
  682. *
  683. * NOTE: It may invoke *phy_powr_off or *phy_power_on which will invoke
  684. * some clk related APIs, so do not invoke it from interrupt context directly.
  685. */
  686. static void rockchip_usb2phy_sm_work(struct work_struct *work)
  687. {
  688. struct rockchip_usb2phy_port *rport =
  689. container_of(work, struct rockchip_usb2phy_port, sm_work.work);
  690. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  691. unsigned int sh = rport->port_cfg->utmi_hstdet.bitend -
  692. rport->port_cfg->utmi_hstdet.bitstart + 1;
  693. unsigned int ul, uhd, state;
  694. unsigned int ul_mask, uhd_mask;
  695. int ret;
  696. mutex_lock(&rport->mutex);
  697. ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
  698. if (ret < 0)
  699. goto next_schedule;
  700. ret = regmap_read(rphy->grf, rport->port_cfg->utmi_hstdet.offset,
  701. &uhd);
  702. if (ret < 0)
  703. goto next_schedule;
  704. uhd_mask = GENMASK(rport->port_cfg->utmi_hstdet.bitend,
  705. rport->port_cfg->utmi_hstdet.bitstart);
  706. ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
  707. rport->port_cfg->utmi_ls.bitstart);
  708. /* stitch on utmi_ls and utmi_hstdet as phy state */
  709. state = ((uhd & uhd_mask) >> rport->port_cfg->utmi_hstdet.bitstart) |
  710. (((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << sh);
  711. switch (state) {
  712. case PHY_STATE_HS_ONLINE:
  713. dev_dbg(&rport->phy->dev, "HS online\n");
  714. break;
  715. case PHY_STATE_FS_LS_ONLINE:
  716. /*
  717. * For FS/LS device, the online state share with connect state
  718. * from utmi_ls and utmi_hstdet register, so we distinguish
  719. * them via suspended flag.
  720. *
  721. * Plus, there are two cases, one is D- Line pull-up, and D+
  722. * line pull-down, the state is 4; another is D+ line pull-up,
  723. * and D- line pull-down, the state is 2.
  724. */
  725. if (!rport->suspended) {
  726. /* D- line pull-up, D+ line pull-down */
  727. dev_dbg(&rport->phy->dev, "FS/LS online\n");
  728. break;
  729. }
  730. /* fall through */
  731. case PHY_STATE_CONNECT:
  732. if (rport->suspended) {
  733. dev_dbg(&rport->phy->dev, "Connected\n");
  734. rockchip_usb2phy_power_on(rport->phy);
  735. rport->suspended = false;
  736. } else {
  737. /* D+ line pull-up, D- line pull-down */
  738. dev_dbg(&rport->phy->dev, "FS/LS online\n");
  739. }
  740. break;
  741. case PHY_STATE_DISCONNECT:
  742. if (!rport->suspended) {
  743. dev_dbg(&rport->phy->dev, "Disconnected\n");
  744. rockchip_usb2phy_power_off(rport->phy);
  745. rport->suspended = true;
  746. }
  747. /*
  748. * activate the linestate detection to get the next device
  749. * plug-in irq.
  750. */
  751. property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
  752. property_enable(rphy, &rport->port_cfg->ls_det_en, true);
  753. /*
  754. * we don't need to rearm the delayed work when the phy port
  755. * is suspended.
  756. */
  757. mutex_unlock(&rport->mutex);
  758. return;
  759. default:
  760. dev_dbg(&rport->phy->dev, "unknown phy state\n");
  761. break;
  762. }
  763. next_schedule:
  764. mutex_unlock(&rport->mutex);
  765. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  766. }
  767. static irqreturn_t rockchip_usb2phy_linestate_irq(int irq, void *data)
  768. {
  769. struct rockchip_usb2phy_port *rport = data;
  770. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  771. if (!property_enabled(rphy, &rport->port_cfg->ls_det_st))
  772. return IRQ_NONE;
  773. mutex_lock(&rport->mutex);
  774. /* disable linestate detect irq and clear its status */
  775. property_enable(rphy, &rport->port_cfg->ls_det_en, false);
  776. property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
  777. mutex_unlock(&rport->mutex);
  778. /*
  779. * In this case for host phy port, a new device is plugged in,
  780. * meanwhile, if the phy port is suspended, we need rearm the work to
  781. * resume it and mange its states; otherwise, we do nothing about that.
  782. */
  783. if (rport->suspended && rport->port_id == USB2PHY_PORT_HOST)
  784. rockchip_usb2phy_sm_work(&rport->sm_work.work);
  785. return IRQ_HANDLED;
  786. }
  787. static irqreturn_t rockchip_usb2phy_bvalid_irq(int irq, void *data)
  788. {
  789. struct rockchip_usb2phy_port *rport = data;
  790. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  791. if (!property_enabled(rphy, &rport->port_cfg->bvalid_det_st))
  792. return IRQ_NONE;
  793. mutex_lock(&rport->mutex);
  794. /* clear bvalid detect irq pending status */
  795. property_enable(rphy, &rport->port_cfg->bvalid_det_clr, true);
  796. mutex_unlock(&rport->mutex);
  797. rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
  798. return IRQ_HANDLED;
  799. }
  800. static int rockchip_usb2phy_host_port_init(struct rockchip_usb2phy *rphy,
  801. struct rockchip_usb2phy_port *rport,
  802. struct device_node *child_np)
  803. {
  804. int ret;
  805. rport->port_id = USB2PHY_PORT_HOST;
  806. rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST];
  807. rport->suspended = true;
  808. mutex_init(&rport->mutex);
  809. INIT_DELAYED_WORK(&rport->sm_work, rockchip_usb2phy_sm_work);
  810. rport->ls_irq = of_irq_get_byname(child_np, "linestate");
  811. if (rport->ls_irq < 0) {
  812. dev_err(rphy->dev, "no linestate irq provided\n");
  813. return rport->ls_irq;
  814. }
  815. ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
  816. rockchip_usb2phy_linestate_irq,
  817. IRQF_ONESHOT,
  818. "rockchip_usb2phy", rport);
  819. if (ret) {
  820. dev_err(rphy->dev, "failed to request linestate irq handle\n");
  821. return ret;
  822. }
  823. return 0;
  824. }
  825. static int rockchip_otg_event(struct notifier_block *nb,
  826. unsigned long event, void *ptr)
  827. {
  828. struct rockchip_usb2phy_port *rport =
  829. container_of(nb, struct rockchip_usb2phy_port, event_nb);
  830. schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
  831. return NOTIFY_DONE;
  832. }
  833. static int rockchip_usb2phy_otg_port_init(struct rockchip_usb2phy *rphy,
  834. struct rockchip_usb2phy_port *rport,
  835. struct device_node *child_np)
  836. {
  837. int ret;
  838. rport->port_id = USB2PHY_PORT_OTG;
  839. rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG];
  840. rport->state = OTG_STATE_UNDEFINED;
  841. /*
  842. * set suspended flag to true, but actually don't
  843. * put phy in suspend mode, it aims to enable usb
  844. * phy and clock in power_on() called by usb controller
  845. * driver during probe.
  846. */
  847. rport->suspended = true;
  848. rport->vbus_attached = false;
  849. mutex_init(&rport->mutex);
  850. rport->mode = of_usb_get_dr_mode_by_phy(child_np, -1);
  851. if (rport->mode == USB_DR_MODE_HOST) {
  852. ret = 0;
  853. goto out;
  854. }
  855. INIT_DELAYED_WORK(&rport->chg_work, rockchip_chg_detect_work);
  856. INIT_DELAYED_WORK(&rport->otg_sm_work, rockchip_usb2phy_otg_sm_work);
  857. rport->utmi_avalid =
  858. of_property_read_bool(child_np, "rockchip,utmi-avalid");
  859. rport->bvalid_irq = of_irq_get_byname(child_np, "otg-bvalid");
  860. if (rport->bvalid_irq < 0) {
  861. dev_err(rphy->dev, "no vbus valid irq provided\n");
  862. ret = rport->bvalid_irq;
  863. goto out;
  864. }
  865. ret = devm_request_threaded_irq(rphy->dev, rport->bvalid_irq, NULL,
  866. rockchip_usb2phy_bvalid_irq,
  867. IRQF_ONESHOT,
  868. "rockchip_usb2phy_bvalid", rport);
  869. if (ret) {
  870. dev_err(rphy->dev, "failed to request otg-bvalid irq handle\n");
  871. goto out;
  872. }
  873. if (!IS_ERR(rphy->edev)) {
  874. rport->event_nb.notifier_call = rockchip_otg_event;
  875. ret = extcon_register_notifier(rphy->edev, EXTCON_USB_HOST,
  876. &rport->event_nb);
  877. if (ret)
  878. dev_err(rphy->dev, "register USB HOST notifier failed\n");
  879. }
  880. out:
  881. return ret;
  882. }
  883. static int rockchip_usb2phy_probe(struct platform_device *pdev)
  884. {
  885. struct device *dev = &pdev->dev;
  886. struct device_node *np = dev->of_node;
  887. struct device_node *child_np;
  888. struct phy_provider *provider;
  889. struct rockchip_usb2phy *rphy;
  890. const struct rockchip_usb2phy_cfg *phy_cfgs;
  891. const struct of_device_id *match;
  892. unsigned int reg;
  893. int index, ret;
  894. rphy = devm_kzalloc(dev, sizeof(*rphy), GFP_KERNEL);
  895. if (!rphy)
  896. return -ENOMEM;
  897. match = of_match_device(dev->driver->of_match_table, dev);
  898. if (!match || !match->data) {
  899. dev_err(dev, "phy configs are not assigned!\n");
  900. return -EINVAL;
  901. }
  902. if (!dev->parent || !dev->parent->of_node)
  903. return -EINVAL;
  904. rphy->grf = syscon_node_to_regmap(dev->parent->of_node);
  905. if (IS_ERR(rphy->grf))
  906. return PTR_ERR(rphy->grf);
  907. if (of_property_read_u32(np, "reg", &reg)) {
  908. dev_err(dev, "the reg property is not assigned in %s node\n",
  909. np->name);
  910. return -EINVAL;
  911. }
  912. rphy->dev = dev;
  913. phy_cfgs = match->data;
  914. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  915. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  916. platform_set_drvdata(pdev, rphy);
  917. ret = rockchip_usb2phy_extcon_register(rphy);
  918. if (ret)
  919. return ret;
  920. /* find out a proper config which can be matched with dt. */
  921. index = 0;
  922. while (phy_cfgs[index].reg) {
  923. if (phy_cfgs[index].reg == reg) {
  924. rphy->phy_cfg = &phy_cfgs[index];
  925. break;
  926. }
  927. ++index;
  928. }
  929. if (!rphy->phy_cfg) {
  930. dev_err(dev, "no phy-config can be matched with %s node\n",
  931. np->name);
  932. return -EINVAL;
  933. }
  934. rphy->clk = of_clk_get_by_name(np, "phyclk");
  935. if (!IS_ERR(rphy->clk)) {
  936. clk_prepare_enable(rphy->clk);
  937. } else {
  938. dev_info(&pdev->dev, "no phyclk specified\n");
  939. rphy->clk = NULL;
  940. }
  941. ret = rockchip_usb2phy_clk480m_register(rphy);
  942. if (ret) {
  943. dev_err(dev, "failed to register 480m output clock\n");
  944. goto disable_clks;
  945. }
  946. index = 0;
  947. for_each_available_child_of_node(np, child_np) {
  948. struct rockchip_usb2phy_port *rport = &rphy->ports[index];
  949. struct phy *phy;
  950. /* This driver aims to support both otg-port and host-port */
  951. if (of_node_cmp(child_np->name, "host-port") &&
  952. of_node_cmp(child_np->name, "otg-port"))
  953. goto next_child;
  954. phy = devm_phy_create(dev, child_np, &rockchip_usb2phy_ops);
  955. if (IS_ERR(phy)) {
  956. dev_err(dev, "failed to create phy\n");
  957. ret = PTR_ERR(phy);
  958. goto put_child;
  959. }
  960. rport->phy = phy;
  961. phy_set_drvdata(rport->phy, rport);
  962. /* initialize otg/host port separately */
  963. if (!of_node_cmp(child_np->name, "host-port")) {
  964. ret = rockchip_usb2phy_host_port_init(rphy, rport,
  965. child_np);
  966. if (ret)
  967. goto put_child;
  968. } else {
  969. ret = rockchip_usb2phy_otg_port_init(rphy, rport,
  970. child_np);
  971. if (ret)
  972. goto put_child;
  973. }
  974. next_child:
  975. /* to prevent out of boundary */
  976. if (++index >= rphy->phy_cfg->num_ports)
  977. break;
  978. }
  979. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  980. return PTR_ERR_OR_ZERO(provider);
  981. put_child:
  982. of_node_put(child_np);
  983. disable_clks:
  984. if (rphy->clk) {
  985. clk_disable_unprepare(rphy->clk);
  986. clk_put(rphy->clk);
  987. }
  988. return ret;
  989. }
  990. static const struct rockchip_usb2phy_cfg rk3328_phy_cfgs[] = {
  991. {
  992. .reg = 0x100,
  993. .num_ports = 2,
  994. .clkout_ctl = { 0x108, 4, 4, 1, 0 },
  995. .port_cfgs = {
  996. [USB2PHY_PORT_OTG] = {
  997. .phy_sus = { 0x0100, 15, 0, 0, 0x1d1 },
  998. .bvalid_det_en = { 0x0110, 2, 2, 0, 1 },
  999. .bvalid_det_st = { 0x0114, 2, 2, 0, 1 },
  1000. .bvalid_det_clr = { 0x0118, 2, 2, 0, 1 },
  1001. .ls_det_en = { 0x0110, 0, 0, 0, 1 },
  1002. .ls_det_st = { 0x0114, 0, 0, 0, 1 },
  1003. .ls_det_clr = { 0x0118, 0, 0, 0, 1 },
  1004. .utmi_avalid = { 0x0120, 10, 10, 0, 1 },
  1005. .utmi_bvalid = { 0x0120, 9, 9, 0, 1 },
  1006. .utmi_ls = { 0x0120, 5, 4, 0, 1 },
  1007. },
  1008. [USB2PHY_PORT_HOST] = {
  1009. .phy_sus = { 0x104, 15, 0, 0, 0x1d1 },
  1010. .ls_det_en = { 0x110, 1, 1, 0, 1 },
  1011. .ls_det_st = { 0x114, 1, 1, 0, 1 },
  1012. .ls_det_clr = { 0x118, 1, 1, 0, 1 },
  1013. .utmi_ls = { 0x120, 17, 16, 0, 1 },
  1014. .utmi_hstdet = { 0x120, 19, 19, 0, 1 }
  1015. }
  1016. },
  1017. .chg_det = {
  1018. .opmode = { 0x0100, 3, 0, 5, 1 },
  1019. .cp_det = { 0x0120, 24, 24, 0, 1 },
  1020. .dcp_det = { 0x0120, 23, 23, 0, 1 },
  1021. .dp_det = { 0x0120, 25, 25, 0, 1 },
  1022. .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
  1023. .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
  1024. .idp_src_en = { 0x0108, 9, 9, 0, 1 },
  1025. .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
  1026. .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
  1027. .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
  1028. },
  1029. },
  1030. { /* sentinel */ }
  1031. };
  1032. static const struct rockchip_usb2phy_cfg rk3366_phy_cfgs[] = {
  1033. {
  1034. .reg = 0x700,
  1035. .num_ports = 2,
  1036. .clkout_ctl = { 0x0724, 15, 15, 1, 0 },
  1037. .port_cfgs = {
  1038. [USB2PHY_PORT_HOST] = {
  1039. .phy_sus = { 0x0728, 15, 0, 0, 0x1d1 },
  1040. .ls_det_en = { 0x0680, 4, 4, 0, 1 },
  1041. .ls_det_st = { 0x0690, 4, 4, 0, 1 },
  1042. .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
  1043. .utmi_ls = { 0x049c, 14, 13, 0, 1 },
  1044. .utmi_hstdet = { 0x049c, 12, 12, 0, 1 }
  1045. }
  1046. },
  1047. },
  1048. { /* sentinel */ }
  1049. };
  1050. static const struct rockchip_usb2phy_cfg rk3399_phy_cfgs[] = {
  1051. {
  1052. .reg = 0xe450,
  1053. .num_ports = 2,
  1054. .clkout_ctl = { 0xe450, 4, 4, 1, 0 },
  1055. .port_cfgs = {
  1056. [USB2PHY_PORT_OTG] = {
  1057. .phy_sus = { 0xe454, 1, 0, 2, 1 },
  1058. .bvalid_det_en = { 0xe3c0, 3, 3, 0, 1 },
  1059. .bvalid_det_st = { 0xe3e0, 3, 3, 0, 1 },
  1060. .bvalid_det_clr = { 0xe3d0, 3, 3, 0, 1 },
  1061. .utmi_avalid = { 0xe2ac, 7, 7, 0, 1 },
  1062. .utmi_bvalid = { 0xe2ac, 12, 12, 0, 1 },
  1063. },
  1064. [USB2PHY_PORT_HOST] = {
  1065. .phy_sus = { 0xe458, 1, 0, 0x2, 0x1 },
  1066. .ls_det_en = { 0xe3c0, 6, 6, 0, 1 },
  1067. .ls_det_st = { 0xe3e0, 6, 6, 0, 1 },
  1068. .ls_det_clr = { 0xe3d0, 6, 6, 0, 1 },
  1069. .utmi_ls = { 0xe2ac, 22, 21, 0, 1 },
  1070. .utmi_hstdet = { 0xe2ac, 23, 23, 0, 1 }
  1071. }
  1072. },
  1073. .chg_det = {
  1074. .opmode = { 0xe454, 3, 0, 5, 1 },
  1075. .cp_det = { 0xe2ac, 2, 2, 0, 1 },
  1076. .dcp_det = { 0xe2ac, 1, 1, 0, 1 },
  1077. .dp_det = { 0xe2ac, 0, 0, 0, 1 },
  1078. .idm_sink_en = { 0xe450, 8, 8, 0, 1 },
  1079. .idp_sink_en = { 0xe450, 7, 7, 0, 1 },
  1080. .idp_src_en = { 0xe450, 9, 9, 0, 1 },
  1081. .rdm_pdwn_en = { 0xe450, 10, 10, 0, 1 },
  1082. .vdm_src_en = { 0xe450, 12, 12, 0, 1 },
  1083. .vdp_src_en = { 0xe450, 11, 11, 0, 1 },
  1084. },
  1085. },
  1086. {
  1087. .reg = 0xe460,
  1088. .num_ports = 2,
  1089. .clkout_ctl = { 0xe460, 4, 4, 1, 0 },
  1090. .port_cfgs = {
  1091. [USB2PHY_PORT_OTG] = {
  1092. .phy_sus = { 0xe464, 1, 0, 2, 1 },
  1093. .bvalid_det_en = { 0xe3c0, 8, 8, 0, 1 },
  1094. .bvalid_det_st = { 0xe3e0, 8, 8, 0, 1 },
  1095. .bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 },
  1096. .utmi_avalid = { 0xe2ac, 10, 10, 0, 1 },
  1097. .utmi_bvalid = { 0xe2ac, 16, 16, 0, 1 },
  1098. },
  1099. [USB2PHY_PORT_HOST] = {
  1100. .phy_sus = { 0xe468, 1, 0, 0x2, 0x1 },
  1101. .ls_det_en = { 0xe3c0, 11, 11, 0, 1 },
  1102. .ls_det_st = { 0xe3e0, 11, 11, 0, 1 },
  1103. .ls_det_clr = { 0xe3d0, 11, 11, 0, 1 },
  1104. .utmi_ls = { 0xe2ac, 26, 25, 0, 1 },
  1105. .utmi_hstdet = { 0xe2ac, 27, 27, 0, 1 }
  1106. }
  1107. },
  1108. },
  1109. { /* sentinel */ }
  1110. };
  1111. static const struct of_device_id rockchip_usb2phy_dt_match[] = {
  1112. { .compatible = "rockchip,rk3328-usb2phy", .data = &rk3328_phy_cfgs },
  1113. { .compatible = "rockchip,rk3366-usb2phy", .data = &rk3366_phy_cfgs },
  1114. { .compatible = "rockchip,rk3399-usb2phy", .data = &rk3399_phy_cfgs },
  1115. {}
  1116. };
  1117. MODULE_DEVICE_TABLE(of, rockchip_usb2phy_dt_match);
  1118. static struct platform_driver rockchip_usb2phy_driver = {
  1119. .probe = rockchip_usb2phy_probe,
  1120. .driver = {
  1121. .name = "rockchip-usb2phy",
  1122. .of_match_table = rockchip_usb2phy_dt_match,
  1123. },
  1124. };
  1125. module_platform_driver(rockchip_usb2phy_driver);
  1126. MODULE_AUTHOR("Frank Wang <frank.wang@rock-chips.com>");
  1127. MODULE_DESCRIPTION("Rockchip USB2.0 PHY driver");
  1128. MODULE_LICENSE("GPL v2");