phy-rockchip-inno-usb2.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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-provider.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. * @otg_mux_irq: IRQ number which multiplex otg-id/otg-bvalid/linestate
  165. * irqs to one irq in otg-port.
  166. * @mutex: for register updating in sm_work.
  167. * @chg_work: charge detect work.
  168. * @otg_sm_work: OTG state machine work.
  169. * @sm_work: HOST state machine work.
  170. * @phy_cfg: port register configuration, assigned by driver data.
  171. * @event_nb: hold event notification callback.
  172. * @state: define OTG enumeration states before device reset.
  173. * @mode: the dr_mode of the controller.
  174. */
  175. struct rockchip_usb2phy_port {
  176. struct phy *phy;
  177. unsigned int port_id;
  178. bool suspended;
  179. bool utmi_avalid;
  180. bool vbus_attached;
  181. int bvalid_irq;
  182. int ls_irq;
  183. int otg_mux_irq;
  184. struct mutex mutex;
  185. struct delayed_work chg_work;
  186. struct delayed_work otg_sm_work;
  187. struct delayed_work sm_work;
  188. const struct rockchip_usb2phy_port_cfg *port_cfg;
  189. struct notifier_block event_nb;
  190. enum usb_otg_state state;
  191. enum usb_dr_mode mode;
  192. };
  193. /**
  194. * struct rockchip_usb2phy: usb2.0 phy driver data.
  195. * @grf: General Register Files regmap.
  196. * @usbgrf: USB General Register Files regmap.
  197. * @clk: clock struct of phy input clk.
  198. * @clk480m: clock struct of phy output clk.
  199. * @clk_hw: clock struct of phy output clk management.
  200. * @chg_state: states involved in USB charger detection.
  201. * @chg_type: USB charger types.
  202. * @dcd_retries: The retry count used to track Data contact
  203. * detection process.
  204. * @edev: extcon device for notification registration
  205. * @phy_cfg: phy register configuration, assigned by driver data.
  206. * @ports: phy port instance.
  207. */
  208. struct rockchip_usb2phy {
  209. struct device *dev;
  210. struct regmap *grf;
  211. struct regmap *usbgrf;
  212. struct clk *clk;
  213. struct clk *clk480m;
  214. struct clk_hw clk480m_hw;
  215. enum usb_chg_state chg_state;
  216. enum power_supply_type chg_type;
  217. u8 dcd_retries;
  218. struct extcon_dev *edev;
  219. const struct rockchip_usb2phy_cfg *phy_cfg;
  220. struct rockchip_usb2phy_port ports[USB2PHY_NUM_PORTS];
  221. };
  222. static inline struct regmap *get_reg_base(struct rockchip_usb2phy *rphy)
  223. {
  224. return rphy->usbgrf == NULL ? rphy->grf : rphy->usbgrf;
  225. }
  226. static inline int property_enable(struct regmap *base,
  227. const struct usb2phy_reg *reg, bool en)
  228. {
  229. unsigned int val, mask, tmp;
  230. tmp = en ? reg->enable : reg->disable;
  231. mask = GENMASK(reg->bitend, reg->bitstart);
  232. val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
  233. return regmap_write(base, reg->offset, val);
  234. }
  235. static inline bool property_enabled(struct regmap *base,
  236. const struct usb2phy_reg *reg)
  237. {
  238. int ret;
  239. unsigned int tmp, orig;
  240. unsigned int mask = GENMASK(reg->bitend, reg->bitstart);
  241. ret = regmap_read(base, reg->offset, &orig);
  242. if (ret)
  243. return false;
  244. tmp = (orig & mask) >> reg->bitstart;
  245. return tmp == reg->enable;
  246. }
  247. static int rockchip_usb2phy_clk480m_prepare(struct clk_hw *hw)
  248. {
  249. struct rockchip_usb2phy *rphy =
  250. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  251. struct regmap *base = get_reg_base(rphy);
  252. int ret;
  253. /* turn on 480m clk output if it is off */
  254. if (!property_enabled(base, &rphy->phy_cfg->clkout_ctl)) {
  255. ret = property_enable(base, &rphy->phy_cfg->clkout_ctl, true);
  256. if (ret)
  257. return ret;
  258. /* waiting for the clk become stable */
  259. usleep_range(1200, 1300);
  260. }
  261. return 0;
  262. }
  263. static void rockchip_usb2phy_clk480m_unprepare(struct clk_hw *hw)
  264. {
  265. struct rockchip_usb2phy *rphy =
  266. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  267. struct regmap *base = get_reg_base(rphy);
  268. /* turn off 480m clk output */
  269. property_enable(base, &rphy->phy_cfg->clkout_ctl, false);
  270. }
  271. static int rockchip_usb2phy_clk480m_prepared(struct clk_hw *hw)
  272. {
  273. struct rockchip_usb2phy *rphy =
  274. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  275. struct regmap *base = get_reg_base(rphy);
  276. return property_enabled(base, &rphy->phy_cfg->clkout_ctl);
  277. }
  278. static unsigned long
  279. rockchip_usb2phy_clk480m_recalc_rate(struct clk_hw *hw,
  280. unsigned long parent_rate)
  281. {
  282. return 480000000;
  283. }
  284. static const struct clk_ops rockchip_usb2phy_clkout_ops = {
  285. .prepare = rockchip_usb2phy_clk480m_prepare,
  286. .unprepare = rockchip_usb2phy_clk480m_unprepare,
  287. .is_prepared = rockchip_usb2phy_clk480m_prepared,
  288. .recalc_rate = rockchip_usb2phy_clk480m_recalc_rate,
  289. };
  290. static void rockchip_usb2phy_clk480m_unregister(void *data)
  291. {
  292. struct rockchip_usb2phy *rphy = data;
  293. of_clk_del_provider(rphy->dev->of_node);
  294. clk_unregister(rphy->clk480m);
  295. }
  296. static int
  297. rockchip_usb2phy_clk480m_register(struct rockchip_usb2phy *rphy)
  298. {
  299. struct device_node *node = rphy->dev->of_node;
  300. struct clk_init_data init;
  301. const char *clk_name;
  302. int ret;
  303. init.flags = 0;
  304. init.name = "clk_usbphy_480m";
  305. init.ops = &rockchip_usb2phy_clkout_ops;
  306. /* optional override of the clockname */
  307. of_property_read_string(node, "clock-output-names", &init.name);
  308. if (rphy->clk) {
  309. clk_name = __clk_get_name(rphy->clk);
  310. init.parent_names = &clk_name;
  311. init.num_parents = 1;
  312. } else {
  313. init.parent_names = NULL;
  314. init.num_parents = 0;
  315. }
  316. rphy->clk480m_hw.init = &init;
  317. /* register the clock */
  318. rphy->clk480m = clk_register(rphy->dev, &rphy->clk480m_hw);
  319. if (IS_ERR(rphy->clk480m)) {
  320. ret = PTR_ERR(rphy->clk480m);
  321. goto err_ret;
  322. }
  323. ret = of_clk_add_provider(node, of_clk_src_simple_get, rphy->clk480m);
  324. if (ret < 0)
  325. goto err_clk_provider;
  326. ret = devm_add_action(rphy->dev, rockchip_usb2phy_clk480m_unregister,
  327. rphy);
  328. if (ret < 0)
  329. goto err_unreg_action;
  330. return 0;
  331. err_unreg_action:
  332. of_clk_del_provider(node);
  333. err_clk_provider:
  334. clk_unregister(rphy->clk480m);
  335. err_ret:
  336. return ret;
  337. }
  338. static int rockchip_usb2phy_extcon_register(struct rockchip_usb2phy *rphy)
  339. {
  340. int ret;
  341. struct device_node *node = rphy->dev->of_node;
  342. struct extcon_dev *edev;
  343. if (of_property_read_bool(node, "extcon")) {
  344. edev = extcon_get_edev_by_phandle(rphy->dev, 0);
  345. if (IS_ERR(edev)) {
  346. if (PTR_ERR(edev) != -EPROBE_DEFER)
  347. dev_err(rphy->dev, "Invalid or missing extcon\n");
  348. return PTR_ERR(edev);
  349. }
  350. } else {
  351. /* Initialize extcon device */
  352. edev = devm_extcon_dev_allocate(rphy->dev,
  353. rockchip_usb2phy_extcon_cable);
  354. if (IS_ERR(edev))
  355. return -ENOMEM;
  356. ret = devm_extcon_dev_register(rphy->dev, edev);
  357. if (ret) {
  358. dev_err(rphy->dev, "failed to register extcon device\n");
  359. return ret;
  360. }
  361. }
  362. rphy->edev = edev;
  363. return 0;
  364. }
  365. static int rockchip_usb2phy_init(struct phy *phy)
  366. {
  367. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  368. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  369. int ret = 0;
  370. mutex_lock(&rport->mutex);
  371. if (rport->port_id == USB2PHY_PORT_OTG) {
  372. if (rport->mode != USB_DR_MODE_HOST &&
  373. rport->mode != USB_DR_MODE_UNKNOWN) {
  374. /* clear bvalid status and enable bvalid detect irq */
  375. ret = property_enable(rphy->grf,
  376. &rport->port_cfg->bvalid_det_clr,
  377. true);
  378. if (ret)
  379. goto out;
  380. ret = property_enable(rphy->grf,
  381. &rport->port_cfg->bvalid_det_en,
  382. true);
  383. if (ret)
  384. goto out;
  385. schedule_delayed_work(&rport->otg_sm_work,
  386. OTG_SCHEDULE_DELAY * 3);
  387. } else {
  388. /* If OTG works in host only mode, do nothing. */
  389. dev_dbg(&rport->phy->dev, "mode %d\n", rport->mode);
  390. }
  391. } else if (rport->port_id == USB2PHY_PORT_HOST) {
  392. /* clear linestate and enable linestate detect irq */
  393. ret = property_enable(rphy->grf,
  394. &rport->port_cfg->ls_det_clr, true);
  395. if (ret)
  396. goto out;
  397. ret = property_enable(rphy->grf,
  398. &rport->port_cfg->ls_det_en, true);
  399. if (ret)
  400. goto out;
  401. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  402. }
  403. out:
  404. mutex_unlock(&rport->mutex);
  405. return ret;
  406. }
  407. static int rockchip_usb2phy_power_on(struct phy *phy)
  408. {
  409. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  410. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  411. struct regmap *base = get_reg_base(rphy);
  412. int ret;
  413. dev_dbg(&rport->phy->dev, "port power on\n");
  414. if (!rport->suspended)
  415. return 0;
  416. ret = clk_prepare_enable(rphy->clk480m);
  417. if (ret)
  418. return ret;
  419. ret = property_enable(base, &rport->port_cfg->phy_sus, false);
  420. if (ret)
  421. return ret;
  422. /* waiting for the utmi_clk to become stable */
  423. usleep_range(1500, 2000);
  424. rport->suspended = false;
  425. return 0;
  426. }
  427. static int rockchip_usb2phy_power_off(struct phy *phy)
  428. {
  429. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  430. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  431. struct regmap *base = get_reg_base(rphy);
  432. int ret;
  433. dev_dbg(&rport->phy->dev, "port power off\n");
  434. if (rport->suspended)
  435. return 0;
  436. ret = property_enable(base, &rport->port_cfg->phy_sus, true);
  437. if (ret)
  438. return ret;
  439. rport->suspended = true;
  440. clk_disable_unprepare(rphy->clk480m);
  441. return 0;
  442. }
  443. static int rockchip_usb2phy_exit(struct phy *phy)
  444. {
  445. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  446. if (rport->port_id == USB2PHY_PORT_OTG &&
  447. rport->mode != USB_DR_MODE_HOST &&
  448. rport->mode != USB_DR_MODE_UNKNOWN) {
  449. cancel_delayed_work_sync(&rport->otg_sm_work);
  450. cancel_delayed_work_sync(&rport->chg_work);
  451. } else if (rport->port_id == USB2PHY_PORT_HOST)
  452. cancel_delayed_work_sync(&rport->sm_work);
  453. return 0;
  454. }
  455. static const struct phy_ops rockchip_usb2phy_ops = {
  456. .init = rockchip_usb2phy_init,
  457. .exit = rockchip_usb2phy_exit,
  458. .power_on = rockchip_usb2phy_power_on,
  459. .power_off = rockchip_usb2phy_power_off,
  460. .owner = THIS_MODULE,
  461. };
  462. static void rockchip_usb2phy_otg_sm_work(struct work_struct *work)
  463. {
  464. struct rockchip_usb2phy_port *rport =
  465. container_of(work, struct rockchip_usb2phy_port,
  466. otg_sm_work.work);
  467. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  468. static unsigned int cable;
  469. unsigned long delay;
  470. bool vbus_attach, sch_work, notify_charger;
  471. if (rport->utmi_avalid)
  472. vbus_attach = property_enabled(rphy->grf,
  473. &rport->port_cfg->utmi_avalid);
  474. else
  475. vbus_attach = property_enabled(rphy->grf,
  476. &rport->port_cfg->utmi_bvalid);
  477. sch_work = false;
  478. notify_charger = false;
  479. delay = OTG_SCHEDULE_DELAY;
  480. dev_dbg(&rport->phy->dev, "%s otg sm work\n",
  481. usb_otg_state_string(rport->state));
  482. switch (rport->state) {
  483. case OTG_STATE_UNDEFINED:
  484. rport->state = OTG_STATE_B_IDLE;
  485. if (!vbus_attach)
  486. rockchip_usb2phy_power_off(rport->phy);
  487. /* fall through */
  488. case OTG_STATE_B_IDLE:
  489. if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) > 0) {
  490. dev_dbg(&rport->phy->dev, "usb otg host connect\n");
  491. rport->state = OTG_STATE_A_HOST;
  492. rockchip_usb2phy_power_on(rport->phy);
  493. return;
  494. } else if (vbus_attach) {
  495. dev_dbg(&rport->phy->dev, "vbus_attach\n");
  496. switch (rphy->chg_state) {
  497. case USB_CHG_STATE_UNDEFINED:
  498. schedule_delayed_work(&rport->chg_work, 0);
  499. return;
  500. case USB_CHG_STATE_DETECTED:
  501. switch (rphy->chg_type) {
  502. case POWER_SUPPLY_TYPE_USB:
  503. dev_dbg(&rport->phy->dev, "sdp cable is connected\n");
  504. rockchip_usb2phy_power_on(rport->phy);
  505. rport->state = OTG_STATE_B_PERIPHERAL;
  506. notify_charger = true;
  507. sch_work = true;
  508. cable = EXTCON_CHG_USB_SDP;
  509. break;
  510. case POWER_SUPPLY_TYPE_USB_DCP:
  511. dev_dbg(&rport->phy->dev, "dcp cable is connected\n");
  512. rockchip_usb2phy_power_off(rport->phy);
  513. notify_charger = true;
  514. sch_work = true;
  515. cable = EXTCON_CHG_USB_DCP;
  516. break;
  517. case POWER_SUPPLY_TYPE_USB_CDP:
  518. dev_dbg(&rport->phy->dev, "cdp cable is connected\n");
  519. rockchip_usb2phy_power_on(rport->phy);
  520. rport->state = OTG_STATE_B_PERIPHERAL;
  521. notify_charger = true;
  522. sch_work = true;
  523. cable = EXTCON_CHG_USB_CDP;
  524. break;
  525. default:
  526. break;
  527. }
  528. break;
  529. default:
  530. break;
  531. }
  532. } else {
  533. notify_charger = true;
  534. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  535. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  536. }
  537. if (rport->vbus_attached != vbus_attach) {
  538. rport->vbus_attached = vbus_attach;
  539. if (notify_charger && rphy->edev) {
  540. extcon_set_state_sync(rphy->edev,
  541. cable, vbus_attach);
  542. if (cable == EXTCON_CHG_USB_SDP)
  543. extcon_set_state_sync(rphy->edev,
  544. EXTCON_USB,
  545. vbus_attach);
  546. }
  547. }
  548. break;
  549. case OTG_STATE_B_PERIPHERAL:
  550. if (!vbus_attach) {
  551. dev_dbg(&rport->phy->dev, "usb disconnect\n");
  552. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  553. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  554. rport->state = OTG_STATE_B_IDLE;
  555. delay = 0;
  556. rockchip_usb2phy_power_off(rport->phy);
  557. }
  558. sch_work = true;
  559. break;
  560. case OTG_STATE_A_HOST:
  561. if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) == 0) {
  562. dev_dbg(&rport->phy->dev, "usb otg host disconnect\n");
  563. rport->state = OTG_STATE_B_IDLE;
  564. rockchip_usb2phy_power_off(rport->phy);
  565. }
  566. break;
  567. default:
  568. break;
  569. }
  570. if (sch_work)
  571. schedule_delayed_work(&rport->otg_sm_work, delay);
  572. }
  573. static const char *chg_to_string(enum power_supply_type chg_type)
  574. {
  575. switch (chg_type) {
  576. case POWER_SUPPLY_TYPE_USB:
  577. return "USB_SDP_CHARGER";
  578. case POWER_SUPPLY_TYPE_USB_DCP:
  579. return "USB_DCP_CHARGER";
  580. case POWER_SUPPLY_TYPE_USB_CDP:
  581. return "USB_CDP_CHARGER";
  582. default:
  583. return "INVALID_CHARGER";
  584. }
  585. }
  586. static void rockchip_chg_enable_dcd(struct rockchip_usb2phy *rphy,
  587. bool en)
  588. {
  589. struct regmap *base = get_reg_base(rphy);
  590. property_enable(base, &rphy->phy_cfg->chg_det.rdm_pdwn_en, en);
  591. property_enable(base, &rphy->phy_cfg->chg_det.idp_src_en, en);
  592. }
  593. static void rockchip_chg_enable_primary_det(struct rockchip_usb2phy *rphy,
  594. bool en)
  595. {
  596. struct regmap *base = get_reg_base(rphy);
  597. property_enable(base, &rphy->phy_cfg->chg_det.vdp_src_en, en);
  598. property_enable(base, &rphy->phy_cfg->chg_det.idm_sink_en, en);
  599. }
  600. static void rockchip_chg_enable_secondary_det(struct rockchip_usb2phy *rphy,
  601. bool en)
  602. {
  603. struct regmap *base = get_reg_base(rphy);
  604. property_enable(base, &rphy->phy_cfg->chg_det.vdm_src_en, en);
  605. property_enable(base, &rphy->phy_cfg->chg_det.idp_sink_en, en);
  606. }
  607. #define CHG_DCD_POLL_TIME (100 * HZ / 1000)
  608. #define CHG_DCD_MAX_RETRIES 6
  609. #define CHG_PRIMARY_DET_TIME (40 * HZ / 1000)
  610. #define CHG_SECONDARY_DET_TIME (40 * HZ / 1000)
  611. static void rockchip_chg_detect_work(struct work_struct *work)
  612. {
  613. struct rockchip_usb2phy_port *rport =
  614. container_of(work, struct rockchip_usb2phy_port, chg_work.work);
  615. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  616. struct regmap *base = get_reg_base(rphy);
  617. bool is_dcd, tmout, vout;
  618. unsigned long delay;
  619. dev_dbg(&rport->phy->dev, "chg detection work state = %d\n",
  620. rphy->chg_state);
  621. switch (rphy->chg_state) {
  622. case USB_CHG_STATE_UNDEFINED:
  623. if (!rport->suspended)
  624. rockchip_usb2phy_power_off(rport->phy);
  625. /* put the controller in non-driving mode */
  626. property_enable(base, &rphy->phy_cfg->chg_det.opmode, false);
  627. /* Start DCD processing stage 1 */
  628. rockchip_chg_enable_dcd(rphy, true);
  629. rphy->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  630. rphy->dcd_retries = 0;
  631. delay = CHG_DCD_POLL_TIME;
  632. break;
  633. case USB_CHG_STATE_WAIT_FOR_DCD:
  634. /* get data contact detection status */
  635. is_dcd = property_enabled(rphy->grf,
  636. &rphy->phy_cfg->chg_det.dp_det);
  637. tmout = ++rphy->dcd_retries == CHG_DCD_MAX_RETRIES;
  638. /* stage 2 */
  639. if (is_dcd || tmout) {
  640. /* stage 4 */
  641. /* Turn off DCD circuitry */
  642. rockchip_chg_enable_dcd(rphy, false);
  643. /* Voltage Source on DP, Probe on DM */
  644. rockchip_chg_enable_primary_det(rphy, true);
  645. delay = CHG_PRIMARY_DET_TIME;
  646. rphy->chg_state = USB_CHG_STATE_DCD_DONE;
  647. } else {
  648. /* stage 3 */
  649. delay = CHG_DCD_POLL_TIME;
  650. }
  651. break;
  652. case USB_CHG_STATE_DCD_DONE:
  653. vout = property_enabled(rphy->grf,
  654. &rphy->phy_cfg->chg_det.cp_det);
  655. rockchip_chg_enable_primary_det(rphy, false);
  656. if (vout) {
  657. /* Voltage Source on DM, Probe on DP */
  658. rockchip_chg_enable_secondary_det(rphy, true);
  659. delay = CHG_SECONDARY_DET_TIME;
  660. rphy->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  661. } else {
  662. if (rphy->dcd_retries == CHG_DCD_MAX_RETRIES) {
  663. /* floating charger found */
  664. rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  665. rphy->chg_state = USB_CHG_STATE_DETECTED;
  666. delay = 0;
  667. } else {
  668. rphy->chg_type = POWER_SUPPLY_TYPE_USB;
  669. rphy->chg_state = USB_CHG_STATE_DETECTED;
  670. delay = 0;
  671. }
  672. }
  673. break;
  674. case USB_CHG_STATE_PRIMARY_DONE:
  675. vout = property_enabled(rphy->grf,
  676. &rphy->phy_cfg->chg_det.dcp_det);
  677. /* Turn off voltage source */
  678. rockchip_chg_enable_secondary_det(rphy, false);
  679. if (vout)
  680. rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  681. else
  682. rphy->chg_type = POWER_SUPPLY_TYPE_USB_CDP;
  683. /* fall through */
  684. case USB_CHG_STATE_SECONDARY_DONE:
  685. rphy->chg_state = USB_CHG_STATE_DETECTED;
  686. delay = 0;
  687. /* fall through */
  688. case USB_CHG_STATE_DETECTED:
  689. /* put the controller in normal mode */
  690. property_enable(base, &rphy->phy_cfg->chg_det.opmode, true);
  691. rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
  692. dev_info(&rport->phy->dev, "charger = %s\n",
  693. chg_to_string(rphy->chg_type));
  694. return;
  695. default:
  696. return;
  697. }
  698. schedule_delayed_work(&rport->chg_work, delay);
  699. }
  700. /*
  701. * The function manage host-phy port state and suspend/resume phy port
  702. * to save power.
  703. *
  704. * we rely on utmi_linestate and utmi_hostdisconnect to identify whether
  705. * devices is disconnect or not. Besides, we do not need care it is FS/LS
  706. * disconnected or HS disconnected, actually, we just only need get the
  707. * device is disconnected at last through rearm the delayed work,
  708. * to suspend the phy port in _PHY_STATE_DISCONNECT_ case.
  709. *
  710. * NOTE: It may invoke *phy_powr_off or *phy_power_on which will invoke
  711. * some clk related APIs, so do not invoke it from interrupt context directly.
  712. */
  713. static void rockchip_usb2phy_sm_work(struct work_struct *work)
  714. {
  715. struct rockchip_usb2phy_port *rport =
  716. container_of(work, struct rockchip_usb2phy_port, sm_work.work);
  717. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  718. unsigned int sh = rport->port_cfg->utmi_hstdet.bitend -
  719. rport->port_cfg->utmi_hstdet.bitstart + 1;
  720. unsigned int ul, uhd, state;
  721. unsigned int ul_mask, uhd_mask;
  722. int ret;
  723. mutex_lock(&rport->mutex);
  724. ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
  725. if (ret < 0)
  726. goto next_schedule;
  727. ret = regmap_read(rphy->grf, rport->port_cfg->utmi_hstdet.offset, &uhd);
  728. if (ret < 0)
  729. goto next_schedule;
  730. uhd_mask = GENMASK(rport->port_cfg->utmi_hstdet.bitend,
  731. rport->port_cfg->utmi_hstdet.bitstart);
  732. ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
  733. rport->port_cfg->utmi_ls.bitstart);
  734. /* stitch on utmi_ls and utmi_hstdet as phy state */
  735. state = ((uhd & uhd_mask) >> rport->port_cfg->utmi_hstdet.bitstart) |
  736. (((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << sh);
  737. switch (state) {
  738. case PHY_STATE_HS_ONLINE:
  739. dev_dbg(&rport->phy->dev, "HS online\n");
  740. break;
  741. case PHY_STATE_FS_LS_ONLINE:
  742. /*
  743. * For FS/LS device, the online state share with connect state
  744. * from utmi_ls and utmi_hstdet register, so we distinguish
  745. * them via suspended flag.
  746. *
  747. * Plus, there are two cases, one is D- Line pull-up, and D+
  748. * line pull-down, the state is 4; another is D+ line pull-up,
  749. * and D- line pull-down, the state is 2.
  750. */
  751. if (!rport->suspended) {
  752. /* D- line pull-up, D+ line pull-down */
  753. dev_dbg(&rport->phy->dev, "FS/LS online\n");
  754. break;
  755. }
  756. /* fall through */
  757. case PHY_STATE_CONNECT:
  758. if (rport->suspended) {
  759. dev_dbg(&rport->phy->dev, "Connected\n");
  760. rockchip_usb2phy_power_on(rport->phy);
  761. rport->suspended = false;
  762. } else {
  763. /* D+ line pull-up, D- line pull-down */
  764. dev_dbg(&rport->phy->dev, "FS/LS online\n");
  765. }
  766. break;
  767. case PHY_STATE_DISCONNECT:
  768. if (!rport->suspended) {
  769. dev_dbg(&rport->phy->dev, "Disconnected\n");
  770. rockchip_usb2phy_power_off(rport->phy);
  771. rport->suspended = true;
  772. }
  773. /*
  774. * activate the linestate detection to get the next device
  775. * plug-in irq.
  776. */
  777. property_enable(rphy->grf, &rport->port_cfg->ls_det_clr, true);
  778. property_enable(rphy->grf, &rport->port_cfg->ls_det_en, true);
  779. /*
  780. * we don't need to rearm the delayed work when the phy port
  781. * is suspended.
  782. */
  783. mutex_unlock(&rport->mutex);
  784. return;
  785. default:
  786. dev_dbg(&rport->phy->dev, "unknown phy state\n");
  787. break;
  788. }
  789. next_schedule:
  790. mutex_unlock(&rport->mutex);
  791. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  792. }
  793. static irqreturn_t rockchip_usb2phy_linestate_irq(int irq, void *data)
  794. {
  795. struct rockchip_usb2phy_port *rport = data;
  796. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  797. if (!property_enabled(rphy->grf, &rport->port_cfg->ls_det_st))
  798. return IRQ_NONE;
  799. mutex_lock(&rport->mutex);
  800. /* disable linestate detect irq and clear its status */
  801. property_enable(rphy->grf, &rport->port_cfg->ls_det_en, false);
  802. property_enable(rphy->grf, &rport->port_cfg->ls_det_clr, true);
  803. mutex_unlock(&rport->mutex);
  804. /*
  805. * In this case for host phy port, a new device is plugged in,
  806. * meanwhile, if the phy port is suspended, we need rearm the work to
  807. * resume it and mange its states; otherwise, we do nothing about that.
  808. */
  809. if (rport->suspended && rport->port_id == USB2PHY_PORT_HOST)
  810. rockchip_usb2phy_sm_work(&rport->sm_work.work);
  811. return IRQ_HANDLED;
  812. }
  813. static irqreturn_t rockchip_usb2phy_bvalid_irq(int irq, void *data)
  814. {
  815. struct rockchip_usb2phy_port *rport = data;
  816. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  817. if (!property_enabled(rphy->grf, &rport->port_cfg->bvalid_det_st))
  818. return IRQ_NONE;
  819. mutex_lock(&rport->mutex);
  820. /* clear bvalid detect irq pending status */
  821. property_enable(rphy->grf, &rport->port_cfg->bvalid_det_clr, true);
  822. mutex_unlock(&rport->mutex);
  823. rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
  824. return IRQ_HANDLED;
  825. }
  826. static irqreturn_t rockchip_usb2phy_otg_mux_irq(int irq, void *data)
  827. {
  828. struct rockchip_usb2phy_port *rport = data;
  829. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  830. if (property_enabled(rphy->grf, &rport->port_cfg->bvalid_det_st))
  831. return rockchip_usb2phy_bvalid_irq(irq, data);
  832. else
  833. return IRQ_NONE;
  834. }
  835. static int rockchip_usb2phy_host_port_init(struct rockchip_usb2phy *rphy,
  836. struct rockchip_usb2phy_port *rport,
  837. struct device_node *child_np)
  838. {
  839. int ret;
  840. rport->port_id = USB2PHY_PORT_HOST;
  841. rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST];
  842. rport->suspended = true;
  843. mutex_init(&rport->mutex);
  844. INIT_DELAYED_WORK(&rport->sm_work, rockchip_usb2phy_sm_work);
  845. rport->ls_irq = of_irq_get_byname(child_np, "linestate");
  846. if (rport->ls_irq < 0) {
  847. dev_err(rphy->dev, "no linestate irq provided\n");
  848. return rport->ls_irq;
  849. }
  850. ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
  851. rockchip_usb2phy_linestate_irq,
  852. IRQF_ONESHOT,
  853. "rockchip_usb2phy", rport);
  854. if (ret) {
  855. dev_err(rphy->dev, "failed to request linestate irq handle\n");
  856. return ret;
  857. }
  858. return 0;
  859. }
  860. static int rockchip_otg_event(struct notifier_block *nb,
  861. unsigned long event, void *ptr)
  862. {
  863. struct rockchip_usb2phy_port *rport =
  864. container_of(nb, struct rockchip_usb2phy_port, event_nb);
  865. schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
  866. return NOTIFY_DONE;
  867. }
  868. static int rockchip_usb2phy_otg_port_init(struct rockchip_usb2phy *rphy,
  869. struct rockchip_usb2phy_port *rport,
  870. struct device_node *child_np)
  871. {
  872. int ret;
  873. rport->port_id = USB2PHY_PORT_OTG;
  874. rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG];
  875. rport->state = OTG_STATE_UNDEFINED;
  876. /*
  877. * set suspended flag to true, but actually don't
  878. * put phy in suspend mode, it aims to enable usb
  879. * phy and clock in power_on() called by usb controller
  880. * driver during probe.
  881. */
  882. rport->suspended = true;
  883. rport->vbus_attached = false;
  884. mutex_init(&rport->mutex);
  885. rport->mode = of_usb_get_dr_mode_by_phy(child_np, -1);
  886. if (rport->mode == USB_DR_MODE_HOST ||
  887. rport->mode == USB_DR_MODE_UNKNOWN) {
  888. ret = 0;
  889. goto out;
  890. }
  891. INIT_DELAYED_WORK(&rport->chg_work, rockchip_chg_detect_work);
  892. INIT_DELAYED_WORK(&rport->otg_sm_work, rockchip_usb2phy_otg_sm_work);
  893. rport->utmi_avalid =
  894. of_property_read_bool(child_np, "rockchip,utmi-avalid");
  895. /*
  896. * Some SoCs use one interrupt with otg-id/otg-bvalid/linestate
  897. * interrupts muxed together, so probe the otg-mux interrupt first,
  898. * if not found, then look for the regular interrupts one by one.
  899. */
  900. rport->otg_mux_irq = of_irq_get_byname(child_np, "otg-mux");
  901. if (rport->otg_mux_irq > 0) {
  902. ret = devm_request_threaded_irq(rphy->dev, rport->otg_mux_irq,
  903. NULL,
  904. rockchip_usb2phy_otg_mux_irq,
  905. IRQF_ONESHOT,
  906. "rockchip_usb2phy_otg",
  907. rport);
  908. if (ret) {
  909. dev_err(rphy->dev,
  910. "failed to request otg-mux irq handle\n");
  911. goto out;
  912. }
  913. } else {
  914. rport->bvalid_irq = of_irq_get_byname(child_np, "otg-bvalid");
  915. if (rport->bvalid_irq < 0) {
  916. dev_err(rphy->dev, "no vbus valid irq provided\n");
  917. ret = rport->bvalid_irq;
  918. goto out;
  919. }
  920. ret = devm_request_threaded_irq(rphy->dev, rport->bvalid_irq,
  921. NULL,
  922. rockchip_usb2phy_bvalid_irq,
  923. IRQF_ONESHOT,
  924. "rockchip_usb2phy_bvalid",
  925. rport);
  926. if (ret) {
  927. dev_err(rphy->dev,
  928. "failed to request otg-bvalid irq handle\n");
  929. goto out;
  930. }
  931. }
  932. if (!IS_ERR(rphy->edev)) {
  933. rport->event_nb.notifier_call = rockchip_otg_event;
  934. ret = devm_extcon_register_notifier(rphy->dev, rphy->edev,
  935. EXTCON_USB_HOST, &rport->event_nb);
  936. if (ret)
  937. dev_err(rphy->dev, "register USB HOST notifier failed\n");
  938. }
  939. out:
  940. return ret;
  941. }
  942. static int rockchip_usb2phy_probe(struct platform_device *pdev)
  943. {
  944. struct device *dev = &pdev->dev;
  945. struct device_node *np = dev->of_node;
  946. struct device_node *child_np;
  947. struct phy_provider *provider;
  948. struct rockchip_usb2phy *rphy;
  949. const struct rockchip_usb2phy_cfg *phy_cfgs;
  950. const struct of_device_id *match;
  951. unsigned int reg;
  952. int index, ret;
  953. rphy = devm_kzalloc(dev, sizeof(*rphy), GFP_KERNEL);
  954. if (!rphy)
  955. return -ENOMEM;
  956. match = of_match_device(dev->driver->of_match_table, dev);
  957. if (!match || !match->data) {
  958. dev_err(dev, "phy configs are not assigned!\n");
  959. return -EINVAL;
  960. }
  961. if (!dev->parent || !dev->parent->of_node)
  962. return -EINVAL;
  963. rphy->grf = syscon_node_to_regmap(dev->parent->of_node);
  964. if (IS_ERR(rphy->grf))
  965. return PTR_ERR(rphy->grf);
  966. if (of_device_is_compatible(np, "rockchip,rv1108-usb2phy")) {
  967. rphy->usbgrf =
  968. syscon_regmap_lookup_by_phandle(dev->of_node,
  969. "rockchip,usbgrf");
  970. if (IS_ERR(rphy->usbgrf))
  971. return PTR_ERR(rphy->usbgrf);
  972. } else {
  973. rphy->usbgrf = NULL;
  974. }
  975. if (of_property_read_u32(np, "reg", &reg)) {
  976. dev_err(dev, "the reg property is not assigned in %s node\n",
  977. np->name);
  978. return -EINVAL;
  979. }
  980. rphy->dev = dev;
  981. phy_cfgs = match->data;
  982. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  983. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  984. platform_set_drvdata(pdev, rphy);
  985. ret = rockchip_usb2phy_extcon_register(rphy);
  986. if (ret)
  987. return ret;
  988. /* find out a proper config which can be matched with dt. */
  989. index = 0;
  990. while (phy_cfgs[index].reg) {
  991. if (phy_cfgs[index].reg == reg) {
  992. rphy->phy_cfg = &phy_cfgs[index];
  993. break;
  994. }
  995. ++index;
  996. }
  997. if (!rphy->phy_cfg) {
  998. dev_err(dev, "no phy-config can be matched with %s node\n",
  999. np->name);
  1000. return -EINVAL;
  1001. }
  1002. rphy->clk = of_clk_get_by_name(np, "phyclk");
  1003. if (!IS_ERR(rphy->clk)) {
  1004. clk_prepare_enable(rphy->clk);
  1005. } else {
  1006. dev_info(&pdev->dev, "no phyclk specified\n");
  1007. rphy->clk = NULL;
  1008. }
  1009. ret = rockchip_usb2phy_clk480m_register(rphy);
  1010. if (ret) {
  1011. dev_err(dev, "failed to register 480m output clock\n");
  1012. goto disable_clks;
  1013. }
  1014. index = 0;
  1015. for_each_available_child_of_node(np, child_np) {
  1016. struct rockchip_usb2phy_port *rport = &rphy->ports[index];
  1017. struct phy *phy;
  1018. /* This driver aims to support both otg-port and host-port */
  1019. if (of_node_cmp(child_np->name, "host-port") &&
  1020. of_node_cmp(child_np->name, "otg-port"))
  1021. goto next_child;
  1022. phy = devm_phy_create(dev, child_np, &rockchip_usb2phy_ops);
  1023. if (IS_ERR(phy)) {
  1024. dev_err(dev, "failed to create phy\n");
  1025. ret = PTR_ERR(phy);
  1026. goto put_child;
  1027. }
  1028. rport->phy = phy;
  1029. phy_set_drvdata(rport->phy, rport);
  1030. /* initialize otg/host port separately */
  1031. if (!of_node_cmp(child_np->name, "host-port")) {
  1032. ret = rockchip_usb2phy_host_port_init(rphy, rport,
  1033. child_np);
  1034. if (ret)
  1035. goto put_child;
  1036. } else {
  1037. ret = rockchip_usb2phy_otg_port_init(rphy, rport,
  1038. child_np);
  1039. if (ret)
  1040. goto put_child;
  1041. }
  1042. next_child:
  1043. /* to prevent out of boundary */
  1044. if (++index >= rphy->phy_cfg->num_ports)
  1045. break;
  1046. }
  1047. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  1048. return PTR_ERR_OR_ZERO(provider);
  1049. put_child:
  1050. of_node_put(child_np);
  1051. disable_clks:
  1052. if (rphy->clk) {
  1053. clk_disable_unprepare(rphy->clk);
  1054. clk_put(rphy->clk);
  1055. }
  1056. return ret;
  1057. }
  1058. static const struct rockchip_usb2phy_cfg rk3228_phy_cfgs[] = {
  1059. {
  1060. .reg = 0x760,
  1061. .num_ports = 2,
  1062. .clkout_ctl = { 0x0768, 4, 4, 1, 0 },
  1063. .port_cfgs = {
  1064. [USB2PHY_PORT_OTG] = {
  1065. .phy_sus = { 0x0760, 15, 0, 0, 0x1d1 },
  1066. .bvalid_det_en = { 0x0680, 3, 3, 0, 1 },
  1067. .bvalid_det_st = { 0x0690, 3, 3, 0, 1 },
  1068. .bvalid_det_clr = { 0x06a0, 3, 3, 0, 1 },
  1069. .ls_det_en = { 0x0680, 2, 2, 0, 1 },
  1070. .ls_det_st = { 0x0690, 2, 2, 0, 1 },
  1071. .ls_det_clr = { 0x06a0, 2, 2, 0, 1 },
  1072. .utmi_bvalid = { 0x0480, 4, 4, 0, 1 },
  1073. .utmi_ls = { 0x0480, 3, 2, 0, 1 },
  1074. },
  1075. [USB2PHY_PORT_HOST] = {
  1076. .phy_sus = { 0x0764, 15, 0, 0, 0x1d1 },
  1077. .ls_det_en = { 0x0680, 4, 4, 0, 1 },
  1078. .ls_det_st = { 0x0690, 4, 4, 0, 1 },
  1079. .ls_det_clr = { 0x06a0, 4, 4, 0, 1 }
  1080. }
  1081. },
  1082. .chg_det = {
  1083. .opmode = { 0x0760, 3, 0, 5, 1 },
  1084. .cp_det = { 0x0884, 4, 4, 0, 1 },
  1085. .dcp_det = { 0x0884, 3, 3, 0, 1 },
  1086. .dp_det = { 0x0884, 5, 5, 0, 1 },
  1087. .idm_sink_en = { 0x0768, 8, 8, 0, 1 },
  1088. .idp_sink_en = { 0x0768, 7, 7, 0, 1 },
  1089. .idp_src_en = { 0x0768, 9, 9, 0, 1 },
  1090. .rdm_pdwn_en = { 0x0768, 10, 10, 0, 1 },
  1091. .vdm_src_en = { 0x0768, 12, 12, 0, 1 },
  1092. .vdp_src_en = { 0x0768, 11, 11, 0, 1 },
  1093. },
  1094. },
  1095. {
  1096. .reg = 0x800,
  1097. .num_ports = 2,
  1098. .clkout_ctl = { 0x0808, 4, 4, 1, 0 },
  1099. .port_cfgs = {
  1100. [USB2PHY_PORT_OTG] = {
  1101. .phy_sus = { 0x800, 15, 0, 0, 0x1d1 },
  1102. .ls_det_en = { 0x0684, 0, 0, 0, 1 },
  1103. .ls_det_st = { 0x0694, 0, 0, 0, 1 },
  1104. .ls_det_clr = { 0x06a4, 0, 0, 0, 1 }
  1105. },
  1106. [USB2PHY_PORT_HOST] = {
  1107. .phy_sus = { 0x804, 15, 0, 0, 0x1d1 },
  1108. .ls_det_en = { 0x0684, 1, 1, 0, 1 },
  1109. .ls_det_st = { 0x0694, 1, 1, 0, 1 },
  1110. .ls_det_clr = { 0x06a4, 1, 1, 0, 1 }
  1111. }
  1112. },
  1113. },
  1114. { /* sentinel */ }
  1115. };
  1116. static const struct rockchip_usb2phy_cfg rk3328_phy_cfgs[] = {
  1117. {
  1118. .reg = 0x100,
  1119. .num_ports = 2,
  1120. .clkout_ctl = { 0x108, 4, 4, 1, 0 },
  1121. .port_cfgs = {
  1122. [USB2PHY_PORT_OTG] = {
  1123. .phy_sus = { 0x0100, 15, 0, 0, 0x1d1 },
  1124. .bvalid_det_en = { 0x0110, 2, 2, 0, 1 },
  1125. .bvalid_det_st = { 0x0114, 2, 2, 0, 1 },
  1126. .bvalid_det_clr = { 0x0118, 2, 2, 0, 1 },
  1127. .ls_det_en = { 0x0110, 0, 0, 0, 1 },
  1128. .ls_det_st = { 0x0114, 0, 0, 0, 1 },
  1129. .ls_det_clr = { 0x0118, 0, 0, 0, 1 },
  1130. .utmi_avalid = { 0x0120, 10, 10, 0, 1 },
  1131. .utmi_bvalid = { 0x0120, 9, 9, 0, 1 },
  1132. .utmi_ls = { 0x0120, 5, 4, 0, 1 },
  1133. },
  1134. [USB2PHY_PORT_HOST] = {
  1135. .phy_sus = { 0x104, 15, 0, 0, 0x1d1 },
  1136. .ls_det_en = { 0x110, 1, 1, 0, 1 },
  1137. .ls_det_st = { 0x114, 1, 1, 0, 1 },
  1138. .ls_det_clr = { 0x118, 1, 1, 0, 1 },
  1139. .utmi_ls = { 0x120, 17, 16, 0, 1 },
  1140. .utmi_hstdet = { 0x120, 19, 19, 0, 1 }
  1141. }
  1142. },
  1143. .chg_det = {
  1144. .opmode = { 0x0100, 3, 0, 5, 1 },
  1145. .cp_det = { 0x0120, 24, 24, 0, 1 },
  1146. .dcp_det = { 0x0120, 23, 23, 0, 1 },
  1147. .dp_det = { 0x0120, 25, 25, 0, 1 },
  1148. .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
  1149. .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
  1150. .idp_src_en = { 0x0108, 9, 9, 0, 1 },
  1151. .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
  1152. .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
  1153. .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
  1154. },
  1155. },
  1156. { /* sentinel */ }
  1157. };
  1158. static const struct rockchip_usb2phy_cfg rk3366_phy_cfgs[] = {
  1159. {
  1160. .reg = 0x700,
  1161. .num_ports = 2,
  1162. .clkout_ctl = { 0x0724, 15, 15, 1, 0 },
  1163. .port_cfgs = {
  1164. [USB2PHY_PORT_HOST] = {
  1165. .phy_sus = { 0x0728, 15, 0, 0, 0x1d1 },
  1166. .ls_det_en = { 0x0680, 4, 4, 0, 1 },
  1167. .ls_det_st = { 0x0690, 4, 4, 0, 1 },
  1168. .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
  1169. .utmi_ls = { 0x049c, 14, 13, 0, 1 },
  1170. .utmi_hstdet = { 0x049c, 12, 12, 0, 1 }
  1171. }
  1172. },
  1173. },
  1174. { /* sentinel */ }
  1175. };
  1176. static const struct rockchip_usb2phy_cfg rk3399_phy_cfgs[] = {
  1177. {
  1178. .reg = 0xe450,
  1179. .num_ports = 2,
  1180. .clkout_ctl = { 0xe450, 4, 4, 1, 0 },
  1181. .port_cfgs = {
  1182. [USB2PHY_PORT_OTG] = {
  1183. .phy_sus = { 0xe454, 1, 0, 2, 1 },
  1184. .bvalid_det_en = { 0xe3c0, 3, 3, 0, 1 },
  1185. .bvalid_det_st = { 0xe3e0, 3, 3, 0, 1 },
  1186. .bvalid_det_clr = { 0xe3d0, 3, 3, 0, 1 },
  1187. .utmi_avalid = { 0xe2ac, 7, 7, 0, 1 },
  1188. .utmi_bvalid = { 0xe2ac, 12, 12, 0, 1 },
  1189. },
  1190. [USB2PHY_PORT_HOST] = {
  1191. .phy_sus = { 0xe458, 1, 0, 0x2, 0x1 },
  1192. .ls_det_en = { 0xe3c0, 6, 6, 0, 1 },
  1193. .ls_det_st = { 0xe3e0, 6, 6, 0, 1 },
  1194. .ls_det_clr = { 0xe3d0, 6, 6, 0, 1 },
  1195. .utmi_ls = { 0xe2ac, 22, 21, 0, 1 },
  1196. .utmi_hstdet = { 0xe2ac, 23, 23, 0, 1 }
  1197. }
  1198. },
  1199. .chg_det = {
  1200. .opmode = { 0xe454, 3, 0, 5, 1 },
  1201. .cp_det = { 0xe2ac, 2, 2, 0, 1 },
  1202. .dcp_det = { 0xe2ac, 1, 1, 0, 1 },
  1203. .dp_det = { 0xe2ac, 0, 0, 0, 1 },
  1204. .idm_sink_en = { 0xe450, 8, 8, 0, 1 },
  1205. .idp_sink_en = { 0xe450, 7, 7, 0, 1 },
  1206. .idp_src_en = { 0xe450, 9, 9, 0, 1 },
  1207. .rdm_pdwn_en = { 0xe450, 10, 10, 0, 1 },
  1208. .vdm_src_en = { 0xe450, 12, 12, 0, 1 },
  1209. .vdp_src_en = { 0xe450, 11, 11, 0, 1 },
  1210. },
  1211. },
  1212. {
  1213. .reg = 0xe460,
  1214. .num_ports = 2,
  1215. .clkout_ctl = { 0xe460, 4, 4, 1, 0 },
  1216. .port_cfgs = {
  1217. [USB2PHY_PORT_OTG] = {
  1218. .phy_sus = { 0xe464, 1, 0, 2, 1 },
  1219. .bvalid_det_en = { 0xe3c0, 8, 8, 0, 1 },
  1220. .bvalid_det_st = { 0xe3e0, 8, 8, 0, 1 },
  1221. .bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 },
  1222. .utmi_avalid = { 0xe2ac, 10, 10, 0, 1 },
  1223. .utmi_bvalid = { 0xe2ac, 16, 16, 0, 1 },
  1224. },
  1225. [USB2PHY_PORT_HOST] = {
  1226. .phy_sus = { 0xe468, 1, 0, 0x2, 0x1 },
  1227. .ls_det_en = { 0xe3c0, 11, 11, 0, 1 },
  1228. .ls_det_st = { 0xe3e0, 11, 11, 0, 1 },
  1229. .ls_det_clr = { 0xe3d0, 11, 11, 0, 1 },
  1230. .utmi_ls = { 0xe2ac, 26, 25, 0, 1 },
  1231. .utmi_hstdet = { 0xe2ac, 27, 27, 0, 1 }
  1232. }
  1233. },
  1234. },
  1235. { /* sentinel */ }
  1236. };
  1237. static const struct rockchip_usb2phy_cfg rv1108_phy_cfgs[] = {
  1238. {
  1239. .reg = 0x100,
  1240. .num_ports = 2,
  1241. .clkout_ctl = { 0x108, 4, 4, 1, 0 },
  1242. .port_cfgs = {
  1243. [USB2PHY_PORT_OTG] = {
  1244. .phy_sus = { 0x0100, 15, 0, 0, 0x1d1 },
  1245. .bvalid_det_en = { 0x0680, 3, 3, 0, 1 },
  1246. .bvalid_det_st = { 0x0690, 3, 3, 0, 1 },
  1247. .bvalid_det_clr = { 0x06a0, 3, 3, 0, 1 },
  1248. .ls_det_en = { 0x0680, 2, 2, 0, 1 },
  1249. .ls_det_st = { 0x0690, 2, 2, 0, 1 },
  1250. .ls_det_clr = { 0x06a0, 2, 2, 0, 1 },
  1251. .utmi_bvalid = { 0x0804, 10, 10, 0, 1 },
  1252. .utmi_ls = { 0x0804, 13, 12, 0, 1 },
  1253. },
  1254. [USB2PHY_PORT_HOST] = {
  1255. .phy_sus = { 0x0104, 15, 0, 0, 0x1d1 },
  1256. .ls_det_en = { 0x0680, 4, 4, 0, 1 },
  1257. .ls_det_st = { 0x0690, 4, 4, 0, 1 },
  1258. .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
  1259. .utmi_ls = { 0x0804, 9, 8, 0, 1 },
  1260. .utmi_hstdet = { 0x0804, 7, 7, 0, 1 }
  1261. }
  1262. },
  1263. .chg_det = {
  1264. .opmode = { 0x0100, 3, 0, 5, 1 },
  1265. .cp_det = { 0x0804, 1, 1, 0, 1 },
  1266. .dcp_det = { 0x0804, 0, 0, 0, 1 },
  1267. .dp_det = { 0x0804, 2, 2, 0, 1 },
  1268. .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
  1269. .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
  1270. .idp_src_en = { 0x0108, 9, 9, 0, 1 },
  1271. .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
  1272. .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
  1273. .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
  1274. },
  1275. },
  1276. { /* sentinel */ }
  1277. };
  1278. static const struct of_device_id rockchip_usb2phy_dt_match[] = {
  1279. { .compatible = "rockchip,rk3228-usb2phy", .data = &rk3228_phy_cfgs },
  1280. { .compatible = "rockchip,rk3328-usb2phy", .data = &rk3328_phy_cfgs },
  1281. { .compatible = "rockchip,rk3366-usb2phy", .data = &rk3366_phy_cfgs },
  1282. { .compatible = "rockchip,rk3399-usb2phy", .data = &rk3399_phy_cfgs },
  1283. { .compatible = "rockchip,rv1108-usb2phy", .data = &rv1108_phy_cfgs },
  1284. {}
  1285. };
  1286. MODULE_DEVICE_TABLE(of, rockchip_usb2phy_dt_match);
  1287. static struct platform_driver rockchip_usb2phy_driver = {
  1288. .probe = rockchip_usb2phy_probe,
  1289. .driver = {
  1290. .name = "rockchip-usb2phy",
  1291. .of_match_table = rockchip_usb2phy_dt_match,
  1292. },
  1293. };
  1294. module_platform_driver(rockchip_usb2phy_driver);
  1295. MODULE_AUTHOR("Frank Wang <frank.wang@rock-chips.com>");
  1296. MODULE_DESCRIPTION("Rockchip USB2.0 PHY driver");
  1297. MODULE_LICENSE("GPL v2");