phy-rockchip-inno-usb2.c 34 KB

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