phy-rockchip-inno-usb2.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Rockchip USB2.0 PHY with Innosilicon IP block driver
  4. *
  5. * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/delay.h>
  10. #include <linux/extcon-provider.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/power_supply.h>
  25. #include <linux/regmap.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <linux/usb/of.h>
  28. #include <linux/usb/otg.h>
  29. #define BIT_WRITEABLE_SHIFT 16
  30. #define SCHEDULE_DELAY (60 * HZ)
  31. #define OTG_SCHEDULE_DELAY (2 * HZ)
  32. enum rockchip_usb2phy_port_id {
  33. USB2PHY_PORT_OTG,
  34. USB2PHY_PORT_HOST,
  35. USB2PHY_NUM_PORTS,
  36. };
  37. enum rockchip_usb2phy_host_state {
  38. PHY_STATE_HS_ONLINE = 0,
  39. PHY_STATE_DISCONNECT = 1,
  40. PHY_STATE_CONNECT = 2,
  41. PHY_STATE_FS_LS_ONLINE = 4,
  42. };
  43. /**
  44. * enum usb_chg_state - Different states involved in USB charger detection.
  45. * @USB_CHG_STATE_UNDEFINED: USB charger is not connected or detection
  46. * process is not yet started.
  47. * @USB_CHG_STATE_WAIT_FOR_DCD: Waiting for Data pins contact.
  48. * @USB_CHG_STATE_DCD_DONE: Data pin contact is detected.
  49. * @USB_CHG_STATE_PRIMARY_DONE: Primary detection is completed (Detects
  50. * between SDP and DCP/CDP).
  51. * @USB_CHG_STATE_SECONDARY_DONE: Secondary detection is completed (Detects
  52. * between DCP and CDP).
  53. * @USB_CHG_STATE_DETECTED: USB charger type is determined.
  54. */
  55. enum usb_chg_state {
  56. USB_CHG_STATE_UNDEFINED = 0,
  57. USB_CHG_STATE_WAIT_FOR_DCD,
  58. USB_CHG_STATE_DCD_DONE,
  59. USB_CHG_STATE_PRIMARY_DONE,
  60. USB_CHG_STATE_SECONDARY_DONE,
  61. USB_CHG_STATE_DETECTED,
  62. };
  63. static const unsigned int rockchip_usb2phy_extcon_cable[] = {
  64. EXTCON_USB,
  65. EXTCON_USB_HOST,
  66. EXTCON_CHG_USB_SDP,
  67. EXTCON_CHG_USB_CDP,
  68. EXTCON_CHG_USB_DCP,
  69. EXTCON_CHG_USB_SLOW,
  70. EXTCON_NONE,
  71. };
  72. struct usb2phy_reg {
  73. unsigned int offset;
  74. unsigned int bitend;
  75. unsigned int bitstart;
  76. unsigned int disable;
  77. unsigned int enable;
  78. };
  79. /**
  80. * struct rockchip_chg_det_reg - usb charger detect registers
  81. * @cp_det: charging port detected successfully.
  82. * @dcp_det: dedicated charging port detected successfully.
  83. * @dp_det: assert data pin connect successfully.
  84. * @idm_sink_en: open dm sink curren.
  85. * @idp_sink_en: open dp sink current.
  86. * @idp_src_en: open dm source current.
  87. * @rdm_pdwn_en: open dm pull down resistor.
  88. * @vdm_src_en: open dm voltage source.
  89. * @vdp_src_en: open dp voltage source.
  90. * @opmode: utmi operational mode.
  91. */
  92. struct rockchip_chg_det_reg {
  93. struct usb2phy_reg cp_det;
  94. struct usb2phy_reg dcp_det;
  95. struct usb2phy_reg dp_det;
  96. struct usb2phy_reg idm_sink_en;
  97. struct usb2phy_reg idp_sink_en;
  98. struct usb2phy_reg idp_src_en;
  99. struct usb2phy_reg rdm_pdwn_en;
  100. struct usb2phy_reg vdm_src_en;
  101. struct usb2phy_reg vdp_src_en;
  102. struct usb2phy_reg opmode;
  103. };
  104. /**
  105. * struct rockchip_usb2phy_port_cfg - usb-phy port configuration.
  106. * @phy_sus: phy suspend register.
  107. * @bvalid_det_en: vbus valid rise detection enable register.
  108. * @bvalid_det_st: vbus valid rise detection status register.
  109. * @bvalid_det_clr: vbus valid rise detection clear register.
  110. * @id_det_en: id detection enable register.
  111. * @id_det_st: id detection state register.
  112. * @id_det_clr: id detection clear register.
  113. * @ls_det_en: linestate detection enable register.
  114. * @ls_det_st: linestate detection state register.
  115. * @ls_det_clr: linestate detection clear register.
  116. * @utmi_avalid: utmi vbus avalid status register.
  117. * @utmi_bvalid: utmi vbus bvalid status register.
  118. * @utmi_id: utmi id state register.
  119. * @utmi_ls: utmi linestate state register.
  120. * @utmi_hstdet: utmi host disconnect register.
  121. */
  122. struct rockchip_usb2phy_port_cfg {
  123. struct usb2phy_reg phy_sus;
  124. struct usb2phy_reg bvalid_det_en;
  125. struct usb2phy_reg bvalid_det_st;
  126. struct usb2phy_reg bvalid_det_clr;
  127. struct usb2phy_reg id_det_en;
  128. struct usb2phy_reg id_det_st;
  129. struct usb2phy_reg id_det_clr;
  130. struct usb2phy_reg ls_det_en;
  131. struct usb2phy_reg ls_det_st;
  132. struct usb2phy_reg ls_det_clr;
  133. struct usb2phy_reg utmi_avalid;
  134. struct usb2phy_reg utmi_bvalid;
  135. struct usb2phy_reg utmi_id;
  136. struct usb2phy_reg utmi_ls;
  137. struct usb2phy_reg utmi_hstdet;
  138. };
  139. /**
  140. * struct rockchip_usb2phy_cfg - usb-phy configuration.
  141. * @reg: the address offset of grf for usb-phy config.
  142. * @num_ports: specify how many ports that the phy has.
  143. * @clkout_ctl: keep on/turn off output clk of phy.
  144. * @port_cfgs: usb-phy port configurations.
  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. * @phy: generic phy.
  157. * @port_id: flag for otg port or host port.
  158. * @suspended: phy suspended flag.
  159. * @vbus_attached: otg device vbus status.
  160. * @bvalid_irq: IRQ number assigned for vbus valid rise detection.
  161. * @id_irq: IRQ number assigned for ID pin detection.
  162. * @ls_irq: IRQ number assigned for linestate detection.
  163. * @otg_mux_irq: IRQ number which multiplex otg-id/otg-bvalid/linestate
  164. * irqs to one irq in otg-port.
  165. * @mutex: for register updating in sm_work.
  166. * @chg_work: charge detect work.
  167. * @otg_sm_work: OTG state machine work.
  168. * @sm_work: HOST state machine work.
  169. * @port_cfg: port register configuration, assigned by driver data.
  170. * @event_nb: hold event notification callback.
  171. * @state: define OTG enumeration states before device reset.
  172. * @mode: the dr_mode of the controller.
  173. */
  174. struct rockchip_usb2phy_port {
  175. struct phy *phy;
  176. unsigned int port_id;
  177. bool suspended;
  178. bool vbus_attached;
  179. int bvalid_irq;
  180. int id_irq;
  181. int ls_irq;
  182. int otg_mux_irq;
  183. struct mutex mutex;
  184. struct delayed_work chg_work;
  185. struct delayed_work otg_sm_work;
  186. struct delayed_work sm_work;
  187. const struct rockchip_usb2phy_port_cfg *port_cfg;
  188. struct notifier_block event_nb;
  189. enum usb_otg_state state;
  190. enum usb_dr_mode mode;
  191. };
  192. /**
  193. * struct rockchip_usb2phy - usb2.0 phy driver data.
  194. * @dev: pointer to device.
  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. * @clk480m_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. * @irq: muxed interrupt for single irq configuration
  206. * @phy_cfg: phy register configuration, assigned by driver data.
  207. * @ports: phy port instance.
  208. */
  209. struct rockchip_usb2phy {
  210. struct device *dev;
  211. struct regmap *grf;
  212. struct regmap *usbgrf;
  213. struct clk *clk;
  214. struct clk *clk480m;
  215. struct clk_hw clk480m_hw;
  216. enum usb_chg_state chg_state;
  217. enum power_supply_type chg_type;
  218. u8 dcd_retries;
  219. struct extcon_dev *edev;
  220. int irq;
  221. const struct rockchip_usb2phy_cfg *phy_cfg;
  222. struct rockchip_usb2phy_port ports[USB2PHY_NUM_PORTS];
  223. };
  224. static inline struct regmap *get_reg_base(struct rockchip_usb2phy *rphy)
  225. {
  226. return rphy->usbgrf == NULL ? rphy->grf : rphy->usbgrf;
  227. }
  228. static inline int property_enable(struct regmap *base,
  229. const struct usb2phy_reg *reg, bool en)
  230. {
  231. unsigned int val, mask, tmp;
  232. tmp = en ? reg->enable : reg->disable;
  233. mask = GENMASK(reg->bitend, reg->bitstart);
  234. val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
  235. return regmap_write(base, reg->offset, val);
  236. }
  237. static inline bool property_enabled(struct regmap *base,
  238. const struct usb2phy_reg *reg)
  239. {
  240. int ret;
  241. unsigned int tmp, orig;
  242. unsigned int mask = GENMASK(reg->bitend, reg->bitstart);
  243. ret = regmap_read(base, reg->offset, &orig);
  244. if (ret)
  245. return false;
  246. tmp = (orig & mask) >> reg->bitstart;
  247. return tmp != reg->disable;
  248. }
  249. static int rockchip_usb2phy_clk480m_prepare(struct clk_hw *hw)
  250. {
  251. struct rockchip_usb2phy *rphy =
  252. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  253. struct regmap *base = get_reg_base(rphy);
  254. int ret;
  255. /* turn on 480m clk output if it is off */
  256. if (!property_enabled(base, &rphy->phy_cfg->clkout_ctl)) {
  257. ret = property_enable(base, &rphy->phy_cfg->clkout_ctl, true);
  258. if (ret)
  259. return ret;
  260. /* waiting for the clk become stable */
  261. usleep_range(1200, 1300);
  262. }
  263. return 0;
  264. }
  265. static void rockchip_usb2phy_clk480m_unprepare(struct clk_hw *hw)
  266. {
  267. struct rockchip_usb2phy *rphy =
  268. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  269. struct regmap *base = get_reg_base(rphy);
  270. /* turn off 480m clk output */
  271. property_enable(base, &rphy->phy_cfg->clkout_ctl, false);
  272. }
  273. static int rockchip_usb2phy_clk480m_prepared(struct clk_hw *hw)
  274. {
  275. struct rockchip_usb2phy *rphy =
  276. container_of(hw, struct rockchip_usb2phy, clk480m_hw);
  277. struct regmap *base = get_reg_base(rphy);
  278. return property_enabled(base, &rphy->phy_cfg->clkout_ctl);
  279. }
  280. static unsigned long
  281. rockchip_usb2phy_clk480m_recalc_rate(struct clk_hw *hw,
  282. unsigned long parent_rate)
  283. {
  284. return 480000000;
  285. }
  286. static const struct clk_ops rockchip_usb2phy_clkout_ops = {
  287. .prepare = rockchip_usb2phy_clk480m_prepare,
  288. .unprepare = rockchip_usb2phy_clk480m_unprepare,
  289. .is_prepared = rockchip_usb2phy_clk480m_prepared,
  290. .recalc_rate = rockchip_usb2phy_clk480m_recalc_rate,
  291. };
  292. static void rockchip_usb2phy_clk480m_unregister(void *data)
  293. {
  294. struct rockchip_usb2phy *rphy = data;
  295. of_clk_del_provider(rphy->dev->of_node);
  296. clk_unregister(rphy->clk480m);
  297. }
  298. static int
  299. rockchip_usb2phy_clk480m_register(struct rockchip_usb2phy *rphy)
  300. {
  301. struct device_node *node = rphy->dev->of_node;
  302. struct clk_init_data init;
  303. const char *clk_name;
  304. int ret = 0;
  305. init.flags = 0;
  306. init.name = "clk_usbphy_480m";
  307. init.ops = &rockchip_usb2phy_clkout_ops;
  308. /* optional override of the clockname */
  309. of_property_read_string(node, "clock-output-names", &init.name);
  310. if (rphy->clk) {
  311. clk_name = __clk_get_name(rphy->clk);
  312. init.parent_names = &clk_name;
  313. init.num_parents = 1;
  314. } else {
  315. init.parent_names = NULL;
  316. init.num_parents = 0;
  317. }
  318. rphy->clk480m_hw.init = &init;
  319. /* register the clock */
  320. rphy->clk480m = clk_register(rphy->dev, &rphy->clk480m_hw);
  321. if (IS_ERR(rphy->clk480m)) {
  322. ret = PTR_ERR(rphy->clk480m);
  323. goto err_ret;
  324. }
  325. ret = of_clk_add_provider(node, of_clk_src_simple_get, rphy->clk480m);
  326. if (ret < 0)
  327. goto err_clk_provider;
  328. return devm_add_action_or_reset(rphy->dev, rockchip_usb2phy_clk480m_unregister, rphy);
  329. err_clk_provider:
  330. clk_unregister(rphy->clk480m);
  331. err_ret:
  332. return ret;
  333. }
  334. static int rockchip_usb2phy_extcon_register(struct rockchip_usb2phy *rphy)
  335. {
  336. int ret;
  337. struct device_node *node = rphy->dev->of_node;
  338. struct extcon_dev *edev;
  339. if (of_property_read_bool(node, "extcon")) {
  340. edev = extcon_get_edev_by_phandle(rphy->dev, 0);
  341. if (IS_ERR(edev)) {
  342. if (PTR_ERR(edev) != -EPROBE_DEFER)
  343. dev_err(rphy->dev, "Invalid or missing extcon\n");
  344. return PTR_ERR(edev);
  345. }
  346. } else {
  347. /* Initialize extcon device */
  348. edev = devm_extcon_dev_allocate(rphy->dev,
  349. rockchip_usb2phy_extcon_cable);
  350. if (IS_ERR(edev))
  351. return -ENOMEM;
  352. ret = devm_extcon_dev_register(rphy->dev, edev);
  353. if (ret) {
  354. dev_err(rphy->dev, "failed to register extcon device\n");
  355. return ret;
  356. }
  357. }
  358. rphy->edev = edev;
  359. return 0;
  360. }
  361. static int rockchip_usb2phy_init(struct phy *phy)
  362. {
  363. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  364. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  365. int ret = 0;
  366. mutex_lock(&rport->mutex);
  367. if (rport->port_id == USB2PHY_PORT_OTG) {
  368. if (rport->mode != USB_DR_MODE_HOST &&
  369. rport->mode != USB_DR_MODE_UNKNOWN) {
  370. /* clear bvalid status and enable bvalid detect irq */
  371. ret = property_enable(rphy->grf,
  372. &rport->port_cfg->bvalid_det_clr,
  373. true);
  374. if (ret)
  375. goto out;
  376. ret = property_enable(rphy->grf,
  377. &rport->port_cfg->bvalid_det_en,
  378. true);
  379. if (ret)
  380. goto out;
  381. /* clear id status and enable id detect irq */
  382. ret = property_enable(rphy->grf,
  383. &rport->port_cfg->id_det_clr,
  384. true);
  385. if (ret)
  386. goto out;
  387. ret = property_enable(rphy->grf,
  388. &rport->port_cfg->id_det_en,
  389. true);
  390. if (ret)
  391. goto out;
  392. schedule_delayed_work(&rport->otg_sm_work,
  393. OTG_SCHEDULE_DELAY * 3);
  394. } else {
  395. /* If OTG works in host only mode, do nothing. */
  396. dev_dbg(&rport->phy->dev, "mode %d\n", rport->mode);
  397. }
  398. } else if (rport->port_id == USB2PHY_PORT_HOST) {
  399. /* clear linestate and enable linestate detect irq */
  400. ret = property_enable(rphy->grf,
  401. &rport->port_cfg->ls_det_clr, true);
  402. if (ret)
  403. goto out;
  404. ret = property_enable(rphy->grf,
  405. &rport->port_cfg->ls_det_en, true);
  406. if (ret)
  407. goto out;
  408. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  409. }
  410. out:
  411. mutex_unlock(&rport->mutex);
  412. return ret;
  413. }
  414. static int rockchip_usb2phy_power_on(struct phy *phy)
  415. {
  416. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  417. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  418. struct regmap *base = get_reg_base(rphy);
  419. int ret;
  420. dev_dbg(&rport->phy->dev, "port power on\n");
  421. if (!rport->suspended)
  422. return 0;
  423. ret = clk_prepare_enable(rphy->clk480m);
  424. if (ret)
  425. return ret;
  426. ret = property_enable(base, &rport->port_cfg->phy_sus, false);
  427. if (ret) {
  428. clk_disable_unprepare(rphy->clk480m);
  429. return ret;
  430. }
  431. /* waiting for the utmi_clk to become stable */
  432. usleep_range(1500, 2000);
  433. rport->suspended = false;
  434. return 0;
  435. }
  436. static int rockchip_usb2phy_power_off(struct phy *phy)
  437. {
  438. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  439. struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
  440. struct regmap *base = get_reg_base(rphy);
  441. int ret;
  442. dev_dbg(&rport->phy->dev, "port power off\n");
  443. if (rport->suspended)
  444. return 0;
  445. ret = property_enable(base, &rport->port_cfg->phy_sus, true);
  446. if (ret)
  447. return ret;
  448. rport->suspended = true;
  449. clk_disable_unprepare(rphy->clk480m);
  450. return 0;
  451. }
  452. static int rockchip_usb2phy_exit(struct phy *phy)
  453. {
  454. struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
  455. if (rport->port_id == USB2PHY_PORT_OTG &&
  456. rport->mode != USB_DR_MODE_HOST &&
  457. rport->mode != USB_DR_MODE_UNKNOWN) {
  458. cancel_delayed_work_sync(&rport->otg_sm_work);
  459. cancel_delayed_work_sync(&rport->chg_work);
  460. } else if (rport->port_id == USB2PHY_PORT_HOST)
  461. cancel_delayed_work_sync(&rport->sm_work);
  462. return 0;
  463. }
  464. static const struct phy_ops rockchip_usb2phy_ops = {
  465. .init = rockchip_usb2phy_init,
  466. .exit = rockchip_usb2phy_exit,
  467. .power_on = rockchip_usb2phy_power_on,
  468. .power_off = rockchip_usb2phy_power_off,
  469. .owner = THIS_MODULE,
  470. };
  471. static void rockchip_usb2phy_otg_sm_work(struct work_struct *work)
  472. {
  473. struct rockchip_usb2phy_port *rport =
  474. container_of(work, struct rockchip_usb2phy_port,
  475. otg_sm_work.work);
  476. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  477. static unsigned int cable;
  478. unsigned long delay;
  479. bool vbus_attach, sch_work, notify_charger;
  480. vbus_attach = property_enabled(rphy->grf,
  481. &rport->port_cfg->utmi_bvalid);
  482. sch_work = false;
  483. notify_charger = false;
  484. delay = OTG_SCHEDULE_DELAY;
  485. dev_dbg(&rport->phy->dev, "%s otg sm work\n",
  486. usb_otg_state_string(rport->state));
  487. switch (rport->state) {
  488. case OTG_STATE_UNDEFINED:
  489. rport->state = OTG_STATE_B_IDLE;
  490. if (!vbus_attach)
  491. rockchip_usb2phy_power_off(rport->phy);
  492. fallthrough;
  493. case OTG_STATE_B_IDLE:
  494. if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) > 0) {
  495. dev_dbg(&rport->phy->dev, "usb otg host connect\n");
  496. rport->state = OTG_STATE_A_HOST;
  497. rockchip_usb2phy_power_on(rport->phy);
  498. return;
  499. } else if (vbus_attach) {
  500. dev_dbg(&rport->phy->dev, "vbus_attach\n");
  501. switch (rphy->chg_state) {
  502. case USB_CHG_STATE_UNDEFINED:
  503. schedule_delayed_work(&rport->chg_work, 0);
  504. return;
  505. case USB_CHG_STATE_DETECTED:
  506. switch (rphy->chg_type) {
  507. case POWER_SUPPLY_TYPE_USB:
  508. dev_dbg(&rport->phy->dev, "sdp cable is connected\n");
  509. rockchip_usb2phy_power_on(rport->phy);
  510. rport->state = OTG_STATE_B_PERIPHERAL;
  511. notify_charger = true;
  512. sch_work = true;
  513. cable = EXTCON_CHG_USB_SDP;
  514. break;
  515. case POWER_SUPPLY_TYPE_USB_DCP:
  516. dev_dbg(&rport->phy->dev, "dcp cable is connected\n");
  517. rockchip_usb2phy_power_off(rport->phy);
  518. notify_charger = true;
  519. sch_work = true;
  520. cable = EXTCON_CHG_USB_DCP;
  521. break;
  522. case POWER_SUPPLY_TYPE_USB_CDP:
  523. dev_dbg(&rport->phy->dev, "cdp cable is connected\n");
  524. rockchip_usb2phy_power_on(rport->phy);
  525. rport->state = OTG_STATE_B_PERIPHERAL;
  526. notify_charger = true;
  527. sch_work = true;
  528. cable = EXTCON_CHG_USB_CDP;
  529. break;
  530. default:
  531. break;
  532. }
  533. break;
  534. default:
  535. break;
  536. }
  537. } else {
  538. notify_charger = true;
  539. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  540. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  541. }
  542. if (rport->vbus_attached != vbus_attach) {
  543. rport->vbus_attached = vbus_attach;
  544. if (notify_charger && rphy->edev) {
  545. extcon_set_state_sync(rphy->edev,
  546. cable, vbus_attach);
  547. if (cable == EXTCON_CHG_USB_SDP)
  548. extcon_set_state_sync(rphy->edev,
  549. EXTCON_USB,
  550. vbus_attach);
  551. }
  552. }
  553. break;
  554. case OTG_STATE_B_PERIPHERAL:
  555. if (!vbus_attach) {
  556. dev_dbg(&rport->phy->dev, "usb disconnect\n");
  557. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  558. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  559. rport->state = OTG_STATE_B_IDLE;
  560. delay = 0;
  561. rockchip_usb2phy_power_off(rport->phy);
  562. }
  563. sch_work = true;
  564. break;
  565. case OTG_STATE_A_HOST:
  566. if (extcon_get_state(rphy->edev, EXTCON_USB_HOST) == 0) {
  567. dev_dbg(&rport->phy->dev, "usb otg host disconnect\n");
  568. rport->state = OTG_STATE_B_IDLE;
  569. rockchip_usb2phy_power_off(rport->phy);
  570. }
  571. break;
  572. default:
  573. break;
  574. }
  575. if (sch_work)
  576. schedule_delayed_work(&rport->otg_sm_work, delay);
  577. }
  578. static const char *chg_to_string(enum power_supply_type chg_type)
  579. {
  580. switch (chg_type) {
  581. case POWER_SUPPLY_TYPE_USB:
  582. return "USB_SDP_CHARGER";
  583. case POWER_SUPPLY_TYPE_USB_DCP:
  584. return "USB_DCP_CHARGER";
  585. case POWER_SUPPLY_TYPE_USB_CDP:
  586. return "USB_CDP_CHARGER";
  587. default:
  588. return "INVALID_CHARGER";
  589. }
  590. }
  591. static void rockchip_chg_enable_dcd(struct rockchip_usb2phy *rphy,
  592. bool en)
  593. {
  594. struct regmap *base = get_reg_base(rphy);
  595. property_enable(base, &rphy->phy_cfg->chg_det.rdm_pdwn_en, en);
  596. property_enable(base, &rphy->phy_cfg->chg_det.idp_src_en, en);
  597. }
  598. static void rockchip_chg_enable_primary_det(struct rockchip_usb2phy *rphy,
  599. bool en)
  600. {
  601. struct regmap *base = get_reg_base(rphy);
  602. property_enable(base, &rphy->phy_cfg->chg_det.vdp_src_en, en);
  603. property_enable(base, &rphy->phy_cfg->chg_det.idm_sink_en, en);
  604. }
  605. static void rockchip_chg_enable_secondary_det(struct rockchip_usb2phy *rphy,
  606. bool en)
  607. {
  608. struct regmap *base = get_reg_base(rphy);
  609. property_enable(base, &rphy->phy_cfg->chg_det.vdm_src_en, en);
  610. property_enable(base, &rphy->phy_cfg->chg_det.idp_sink_en, en);
  611. }
  612. #define CHG_DCD_POLL_TIME (100 * HZ / 1000)
  613. #define CHG_DCD_MAX_RETRIES 6
  614. #define CHG_PRIMARY_DET_TIME (40 * HZ / 1000)
  615. #define CHG_SECONDARY_DET_TIME (40 * HZ / 1000)
  616. static void rockchip_chg_detect_work(struct work_struct *work)
  617. {
  618. struct rockchip_usb2phy_port *rport =
  619. container_of(work, struct rockchip_usb2phy_port, chg_work.work);
  620. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  621. struct regmap *base = get_reg_base(rphy);
  622. bool is_dcd, tmout, vout;
  623. unsigned long delay;
  624. dev_dbg(&rport->phy->dev, "chg detection work state = %d\n",
  625. rphy->chg_state);
  626. switch (rphy->chg_state) {
  627. case USB_CHG_STATE_UNDEFINED:
  628. if (!rport->suspended)
  629. rockchip_usb2phy_power_off(rport->phy);
  630. /* put the controller in non-driving mode */
  631. property_enable(base, &rphy->phy_cfg->chg_det.opmode, false);
  632. /* Start DCD processing stage 1 */
  633. rockchip_chg_enable_dcd(rphy, true);
  634. rphy->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  635. rphy->dcd_retries = 0;
  636. delay = CHG_DCD_POLL_TIME;
  637. break;
  638. case USB_CHG_STATE_WAIT_FOR_DCD:
  639. /* get data contact detection status */
  640. is_dcd = property_enabled(rphy->grf,
  641. &rphy->phy_cfg->chg_det.dp_det);
  642. tmout = ++rphy->dcd_retries == CHG_DCD_MAX_RETRIES;
  643. /* stage 2 */
  644. if (is_dcd || tmout) {
  645. /* stage 4 */
  646. /* Turn off DCD circuitry */
  647. rockchip_chg_enable_dcd(rphy, false);
  648. /* Voltage Source on DP, Probe on DM */
  649. rockchip_chg_enable_primary_det(rphy, true);
  650. delay = CHG_PRIMARY_DET_TIME;
  651. rphy->chg_state = USB_CHG_STATE_DCD_DONE;
  652. } else {
  653. /* stage 3 */
  654. delay = CHG_DCD_POLL_TIME;
  655. }
  656. break;
  657. case USB_CHG_STATE_DCD_DONE:
  658. vout = property_enabled(rphy->grf,
  659. &rphy->phy_cfg->chg_det.cp_det);
  660. rockchip_chg_enable_primary_det(rphy, false);
  661. if (vout) {
  662. /* Voltage Source on DM, Probe on DP */
  663. rockchip_chg_enable_secondary_det(rphy, true);
  664. delay = CHG_SECONDARY_DET_TIME;
  665. rphy->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  666. } else {
  667. if (rphy->dcd_retries == CHG_DCD_MAX_RETRIES) {
  668. /* floating charger found */
  669. rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  670. rphy->chg_state = USB_CHG_STATE_DETECTED;
  671. delay = 0;
  672. } else {
  673. rphy->chg_type = POWER_SUPPLY_TYPE_USB;
  674. rphy->chg_state = USB_CHG_STATE_DETECTED;
  675. delay = 0;
  676. }
  677. }
  678. break;
  679. case USB_CHG_STATE_PRIMARY_DONE:
  680. vout = property_enabled(rphy->grf,
  681. &rphy->phy_cfg->chg_det.dcp_det);
  682. /* Turn off voltage source */
  683. rockchip_chg_enable_secondary_det(rphy, false);
  684. if (vout)
  685. rphy->chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  686. else
  687. rphy->chg_type = POWER_SUPPLY_TYPE_USB_CDP;
  688. fallthrough;
  689. case USB_CHG_STATE_SECONDARY_DONE:
  690. rphy->chg_state = USB_CHG_STATE_DETECTED;
  691. fallthrough;
  692. case USB_CHG_STATE_DETECTED:
  693. /* put the controller in normal mode */
  694. property_enable(base, &rphy->phy_cfg->chg_det.opmode, true);
  695. rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
  696. dev_dbg(&rport->phy->dev, "charger = %s\n",
  697. chg_to_string(rphy->chg_type));
  698. return;
  699. default:
  700. return;
  701. }
  702. schedule_delayed_work(&rport->chg_work, delay);
  703. }
  704. /*
  705. * The function manage host-phy port state and suspend/resume phy port
  706. * to save power.
  707. *
  708. * we rely on utmi_linestate and utmi_hostdisconnect to identify whether
  709. * devices is disconnect or not. Besides, we do not need care it is FS/LS
  710. * disconnected or HS disconnected, actually, we just only need get the
  711. * device is disconnected at last through rearm the delayed work,
  712. * to suspend the phy port in _PHY_STATE_DISCONNECT_ case.
  713. *
  714. * NOTE: It may invoke *phy_powr_off or *phy_power_on which will invoke
  715. * some clk related APIs, so do not invoke it from interrupt context directly.
  716. */
  717. static void rockchip_usb2phy_sm_work(struct work_struct *work)
  718. {
  719. struct rockchip_usb2phy_port *rport =
  720. container_of(work, struct rockchip_usb2phy_port, sm_work.work);
  721. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  722. unsigned int sh = rport->port_cfg->utmi_hstdet.bitend -
  723. rport->port_cfg->utmi_hstdet.bitstart + 1;
  724. unsigned int ul, uhd, state;
  725. unsigned int ul_mask, uhd_mask;
  726. int ret;
  727. mutex_lock(&rport->mutex);
  728. ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
  729. if (ret < 0)
  730. goto next_schedule;
  731. ret = regmap_read(rphy->grf, rport->port_cfg->utmi_hstdet.offset, &uhd);
  732. if (ret < 0)
  733. goto next_schedule;
  734. uhd_mask = GENMASK(rport->port_cfg->utmi_hstdet.bitend,
  735. rport->port_cfg->utmi_hstdet.bitstart);
  736. ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
  737. rport->port_cfg->utmi_ls.bitstart);
  738. /* stitch on utmi_ls and utmi_hstdet as phy state */
  739. state = ((uhd & uhd_mask) >> rport->port_cfg->utmi_hstdet.bitstart) |
  740. (((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << sh);
  741. switch (state) {
  742. case PHY_STATE_HS_ONLINE:
  743. dev_dbg(&rport->phy->dev, "HS online\n");
  744. break;
  745. case PHY_STATE_FS_LS_ONLINE:
  746. /*
  747. * For FS/LS device, the online state share with connect state
  748. * from utmi_ls and utmi_hstdet register, so we distinguish
  749. * them via suspended flag.
  750. *
  751. * Plus, there are two cases, one is D- Line pull-up, and D+
  752. * line pull-down, the state is 4; another is D+ line pull-up,
  753. * and D- line pull-down, the state is 2.
  754. */
  755. if (!rport->suspended) {
  756. /* D- line pull-up, D+ line pull-down */
  757. dev_dbg(&rport->phy->dev, "FS/LS online\n");
  758. break;
  759. }
  760. fallthrough;
  761. case PHY_STATE_CONNECT:
  762. if (rport->suspended) {
  763. dev_dbg(&rport->phy->dev, "Connected\n");
  764. rockchip_usb2phy_power_on(rport->phy);
  765. rport->suspended = false;
  766. } else {
  767. /* D+ line pull-up, D- line pull-down */
  768. dev_dbg(&rport->phy->dev, "FS/LS online\n");
  769. }
  770. break;
  771. case PHY_STATE_DISCONNECT:
  772. if (!rport->suspended) {
  773. dev_dbg(&rport->phy->dev, "Disconnected\n");
  774. rockchip_usb2phy_power_off(rport->phy);
  775. rport->suspended = true;
  776. }
  777. /*
  778. * activate the linestate detection to get the next device
  779. * plug-in irq.
  780. */
  781. property_enable(rphy->grf, &rport->port_cfg->ls_det_clr, true);
  782. property_enable(rphy->grf, &rport->port_cfg->ls_det_en, true);
  783. /*
  784. * we don't need to rearm the delayed work when the phy port
  785. * is suspended.
  786. */
  787. mutex_unlock(&rport->mutex);
  788. return;
  789. default:
  790. dev_dbg(&rport->phy->dev, "unknown phy state\n");
  791. break;
  792. }
  793. next_schedule:
  794. mutex_unlock(&rport->mutex);
  795. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  796. }
  797. static irqreturn_t rockchip_usb2phy_linestate_irq(int irq, void *data)
  798. {
  799. struct rockchip_usb2phy_port *rport = data;
  800. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  801. if (!property_enabled(rphy->grf, &rport->port_cfg->ls_det_st))
  802. return IRQ_NONE;
  803. mutex_lock(&rport->mutex);
  804. /* disable linestate detect irq and clear its status */
  805. property_enable(rphy->grf, &rport->port_cfg->ls_det_en, false);
  806. property_enable(rphy->grf, &rport->port_cfg->ls_det_clr, true);
  807. mutex_unlock(&rport->mutex);
  808. /*
  809. * In this case for host phy port, a new device is plugged in,
  810. * meanwhile, if the phy port is suspended, we need rearm the work to
  811. * resume it and mange its states; otherwise, we do nothing about that.
  812. */
  813. if (rport->suspended && rport->port_id == USB2PHY_PORT_HOST)
  814. rockchip_usb2phy_sm_work(&rport->sm_work.work);
  815. return IRQ_HANDLED;
  816. }
  817. static irqreturn_t rockchip_usb2phy_bvalid_irq(int irq, void *data)
  818. {
  819. struct rockchip_usb2phy_port *rport = data;
  820. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  821. if (!property_enabled(rphy->grf, &rport->port_cfg->bvalid_det_st))
  822. return IRQ_NONE;
  823. /* clear bvalid detect irq pending status */
  824. property_enable(rphy->grf, &rport->port_cfg->bvalid_det_clr, true);
  825. rockchip_usb2phy_otg_sm_work(&rport->otg_sm_work.work);
  826. return IRQ_HANDLED;
  827. }
  828. static irqreturn_t rockchip_usb2phy_id_irq(int irq, void *data)
  829. {
  830. struct rockchip_usb2phy_port *rport = data;
  831. struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
  832. bool id;
  833. if (!property_enabled(rphy->grf, &rport->port_cfg->id_det_st))
  834. return IRQ_NONE;
  835. /* clear id detect irq pending status */
  836. property_enable(rphy->grf, &rport->port_cfg->id_det_clr, true);
  837. id = property_enabled(rphy->grf, &rport->port_cfg->utmi_id);
  838. extcon_set_state_sync(rphy->edev, EXTCON_USB_HOST, !id);
  839. return IRQ_HANDLED;
  840. }
  841. static irqreturn_t rockchip_usb2phy_otg_mux_irq(int irq, void *data)
  842. {
  843. irqreturn_t ret = IRQ_NONE;
  844. ret |= rockchip_usb2phy_bvalid_irq(irq, data);
  845. ret |= rockchip_usb2phy_id_irq(irq, data);
  846. return ret;
  847. }
  848. static irqreturn_t rockchip_usb2phy_irq(int irq, void *data)
  849. {
  850. struct rockchip_usb2phy *rphy = data;
  851. struct rockchip_usb2phy_port *rport;
  852. irqreturn_t ret = IRQ_NONE;
  853. unsigned int index;
  854. for (index = 0; index < rphy->phy_cfg->num_ports; index++) {
  855. rport = &rphy->ports[index];
  856. if (!rport->phy)
  857. continue;
  858. switch (rport->port_id) {
  859. case USB2PHY_PORT_OTG:
  860. if (rport->mode != USB_DR_MODE_HOST &&
  861. rport->mode != USB_DR_MODE_UNKNOWN)
  862. ret |= rockchip_usb2phy_otg_mux_irq(irq, rport);
  863. break;
  864. case USB2PHY_PORT_HOST:
  865. ret |= rockchip_usb2phy_linestate_irq(irq, rport);
  866. break;
  867. }
  868. }
  869. return ret;
  870. }
  871. static int rockchip_usb2phy_port_irq_init(struct rockchip_usb2phy *rphy,
  872. struct rockchip_usb2phy_port *rport,
  873. struct device_node *child_np)
  874. {
  875. int ret;
  876. /*
  877. * If the usb2 phy used combined irq for otg and host port,
  878. * don't need to init otg and host port irq separately.
  879. */
  880. if (rphy->irq > 0)
  881. return 0;
  882. switch (rport->port_id) {
  883. case USB2PHY_PORT_HOST:
  884. rport->ls_irq = of_irq_get_byname(child_np, "linestate");
  885. if (rport->ls_irq < 0) {
  886. dev_err(rphy->dev, "no linestate irq provided\n");
  887. return rport->ls_irq;
  888. }
  889. ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
  890. rockchip_usb2phy_linestate_irq,
  891. IRQF_ONESHOT,
  892. "rockchip_usb2phy", rport);
  893. if (ret) {
  894. dev_err(rphy->dev, "failed to request linestate irq handle\n");
  895. return ret;
  896. }
  897. break;
  898. case USB2PHY_PORT_OTG:
  899. /*
  900. * Some SoCs use one interrupt with otg-id/otg-bvalid/linestate
  901. * interrupts muxed together, so probe the otg-mux interrupt first,
  902. * if not found, then look for the regular interrupts one by one.
  903. */
  904. rport->otg_mux_irq = of_irq_get_byname(child_np, "otg-mux");
  905. if (rport->otg_mux_irq > 0) {
  906. ret = devm_request_threaded_irq(rphy->dev, rport->otg_mux_irq,
  907. NULL,
  908. rockchip_usb2phy_otg_mux_irq,
  909. IRQF_ONESHOT,
  910. "rockchip_usb2phy_otg",
  911. rport);
  912. if (ret) {
  913. dev_err(rphy->dev,
  914. "failed to request otg-mux irq handle\n");
  915. return ret;
  916. }
  917. } else {
  918. rport->bvalid_irq = of_irq_get_byname(child_np, "otg-bvalid");
  919. if (rport->bvalid_irq < 0) {
  920. dev_err(rphy->dev, "no vbus valid irq provided\n");
  921. ret = rport->bvalid_irq;
  922. return ret;
  923. }
  924. ret = devm_request_threaded_irq(rphy->dev, rport->bvalid_irq,
  925. NULL,
  926. rockchip_usb2phy_bvalid_irq,
  927. IRQF_ONESHOT,
  928. "rockchip_usb2phy_bvalid",
  929. rport);
  930. if (ret) {
  931. dev_err(rphy->dev,
  932. "failed to request otg-bvalid irq handle\n");
  933. return ret;
  934. }
  935. rport->id_irq = of_irq_get_byname(child_np, "otg-id");
  936. if (rport->id_irq < 0) {
  937. dev_err(rphy->dev, "no otg-id irq provided\n");
  938. ret = rport->id_irq;
  939. return ret;
  940. }
  941. ret = devm_request_threaded_irq(rphy->dev, rport->id_irq,
  942. NULL,
  943. rockchip_usb2phy_id_irq,
  944. IRQF_ONESHOT,
  945. "rockchip_usb2phy_id",
  946. rport);
  947. if (ret) {
  948. dev_err(rphy->dev,
  949. "failed to request otg-id irq handle\n");
  950. return ret;
  951. }
  952. }
  953. break;
  954. default:
  955. return -EINVAL;
  956. }
  957. return 0;
  958. }
  959. static int rockchip_usb2phy_host_port_init(struct rockchip_usb2phy *rphy,
  960. struct rockchip_usb2phy_port *rport,
  961. struct device_node *child_np)
  962. {
  963. int ret;
  964. rport->port_id = USB2PHY_PORT_HOST;
  965. rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST];
  966. rport->suspended = true;
  967. mutex_init(&rport->mutex);
  968. INIT_DELAYED_WORK(&rport->sm_work, rockchip_usb2phy_sm_work);
  969. ret = rockchip_usb2phy_port_irq_init(rphy, rport, child_np);
  970. if (ret) {
  971. dev_err(rphy->dev, "failed to setup host irq\n");
  972. return ret;
  973. }
  974. return 0;
  975. }
  976. static int rockchip_otg_event(struct notifier_block *nb,
  977. unsigned long event, void *ptr)
  978. {
  979. struct rockchip_usb2phy_port *rport =
  980. container_of(nb, struct rockchip_usb2phy_port, event_nb);
  981. schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
  982. return NOTIFY_DONE;
  983. }
  984. static int rockchip_usb2phy_otg_port_init(struct rockchip_usb2phy *rphy,
  985. struct rockchip_usb2phy_port *rport,
  986. struct device_node *child_np)
  987. {
  988. int ret, id;
  989. rport->port_id = USB2PHY_PORT_OTG;
  990. rport->port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG];
  991. rport->state = OTG_STATE_UNDEFINED;
  992. /*
  993. * set suspended flag to true, but actually don't
  994. * put phy in suspend mode, it aims to enable usb
  995. * phy and clock in power_on() called by usb controller
  996. * driver during probe.
  997. */
  998. rport->suspended = true;
  999. rport->vbus_attached = false;
  1000. mutex_init(&rport->mutex);
  1001. rport->mode = of_usb_get_dr_mode_by_phy(child_np, -1);
  1002. if (rport->mode == USB_DR_MODE_HOST ||
  1003. rport->mode == USB_DR_MODE_UNKNOWN) {
  1004. ret = 0;
  1005. goto out;
  1006. }
  1007. INIT_DELAYED_WORK(&rport->chg_work, rockchip_chg_detect_work);
  1008. INIT_DELAYED_WORK(&rport->otg_sm_work, rockchip_usb2phy_otg_sm_work);
  1009. ret = rockchip_usb2phy_port_irq_init(rphy, rport, child_np);
  1010. if (ret) {
  1011. dev_err(rphy->dev, "failed to init irq for host port\n");
  1012. goto out;
  1013. }
  1014. if (!IS_ERR(rphy->edev)) {
  1015. rport->event_nb.notifier_call = rockchip_otg_event;
  1016. ret = devm_extcon_register_notifier(rphy->dev, rphy->edev,
  1017. EXTCON_USB_HOST, &rport->event_nb);
  1018. if (ret) {
  1019. dev_err(rphy->dev, "register USB HOST notifier failed\n");
  1020. goto out;
  1021. }
  1022. if (!of_property_read_bool(rphy->dev->of_node, "extcon")) {
  1023. /* do initial sync of usb state */
  1024. id = property_enabled(rphy->grf, &rport->port_cfg->utmi_id);
  1025. extcon_set_state_sync(rphy->edev, EXTCON_USB_HOST, !id);
  1026. }
  1027. }
  1028. out:
  1029. return ret;
  1030. }
  1031. static int rockchip_usb2phy_probe(struct platform_device *pdev)
  1032. {
  1033. struct device *dev = &pdev->dev;
  1034. struct device_node *np = dev->of_node;
  1035. struct device_node *child_np;
  1036. struct phy_provider *provider;
  1037. struct rockchip_usb2phy *rphy;
  1038. const struct rockchip_usb2phy_cfg *phy_cfgs;
  1039. const struct of_device_id *match;
  1040. unsigned int reg;
  1041. int index, ret;
  1042. rphy = devm_kzalloc(dev, sizeof(*rphy), GFP_KERNEL);
  1043. if (!rphy)
  1044. return -ENOMEM;
  1045. match = of_match_device(dev->driver->of_match_table, dev);
  1046. if (!match || !match->data) {
  1047. dev_err(dev, "phy configs are not assigned!\n");
  1048. return -EINVAL;
  1049. }
  1050. if (!dev->parent || !dev->parent->of_node) {
  1051. rphy->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,usbgrf");
  1052. if (IS_ERR(rphy->grf)) {
  1053. dev_err(dev, "failed to locate usbgrf\n");
  1054. return PTR_ERR(rphy->grf);
  1055. }
  1056. }
  1057. else {
  1058. rphy->grf = syscon_node_to_regmap(dev->parent->of_node);
  1059. if (IS_ERR(rphy->grf))
  1060. return PTR_ERR(rphy->grf);
  1061. }
  1062. if (of_device_is_compatible(np, "rockchip,rv1108-usb2phy")) {
  1063. rphy->usbgrf =
  1064. syscon_regmap_lookup_by_phandle(dev->of_node,
  1065. "rockchip,usbgrf");
  1066. if (IS_ERR(rphy->usbgrf))
  1067. return PTR_ERR(rphy->usbgrf);
  1068. } else {
  1069. rphy->usbgrf = NULL;
  1070. }
  1071. if (of_property_read_u32_index(np, "reg", 0, &reg)) {
  1072. dev_err(dev, "the reg property is not assigned in %pOFn node\n",
  1073. np);
  1074. return -EINVAL;
  1075. }
  1076. /* support address_cells=2 */
  1077. if (reg == 0) {
  1078. if (of_property_read_u32_index(np, "reg", 1, &reg)) {
  1079. dev_err(dev, "the reg property is not assigned in %pOFn node\n",
  1080. np);
  1081. return -EINVAL;
  1082. }
  1083. }
  1084. rphy->dev = dev;
  1085. phy_cfgs = match->data;
  1086. rphy->chg_state = USB_CHG_STATE_UNDEFINED;
  1087. rphy->chg_type = POWER_SUPPLY_TYPE_UNKNOWN;
  1088. rphy->irq = platform_get_irq_optional(pdev, 0);
  1089. platform_set_drvdata(pdev, rphy);
  1090. ret = rockchip_usb2phy_extcon_register(rphy);
  1091. if (ret)
  1092. return ret;
  1093. /* find out a proper config which can be matched with dt. */
  1094. index = 0;
  1095. while (phy_cfgs[index].reg) {
  1096. if (phy_cfgs[index].reg == reg) {
  1097. rphy->phy_cfg = &phy_cfgs[index];
  1098. break;
  1099. }
  1100. ++index;
  1101. }
  1102. if (!rphy->phy_cfg) {
  1103. dev_err(dev, "no phy-config can be matched with %pOFn node\n",
  1104. np);
  1105. return -EINVAL;
  1106. }
  1107. rphy->clk = of_clk_get_by_name(np, "phyclk");
  1108. if (!IS_ERR(rphy->clk)) {
  1109. clk_prepare_enable(rphy->clk);
  1110. } else {
  1111. dev_info(&pdev->dev, "no phyclk specified\n");
  1112. rphy->clk = NULL;
  1113. }
  1114. ret = rockchip_usb2phy_clk480m_register(rphy);
  1115. if (ret) {
  1116. dev_err(dev, "failed to register 480m output clock\n");
  1117. goto disable_clks;
  1118. }
  1119. index = 0;
  1120. for_each_available_child_of_node(np, child_np) {
  1121. struct rockchip_usb2phy_port *rport = &rphy->ports[index];
  1122. struct phy *phy;
  1123. /* This driver aims to support both otg-port and host-port */
  1124. if (!of_node_name_eq(child_np, "host-port") &&
  1125. !of_node_name_eq(child_np, "otg-port"))
  1126. goto next_child;
  1127. phy = devm_phy_create(dev, child_np, &rockchip_usb2phy_ops);
  1128. if (IS_ERR(phy)) {
  1129. dev_err_probe(dev, PTR_ERR(phy), "failed to create phy\n");
  1130. ret = PTR_ERR(phy);
  1131. goto put_child;
  1132. }
  1133. rport->phy = phy;
  1134. phy_set_drvdata(rport->phy, rport);
  1135. /* initialize otg/host port separately */
  1136. if (of_node_name_eq(child_np, "host-port")) {
  1137. ret = rockchip_usb2phy_host_port_init(rphy, rport,
  1138. child_np);
  1139. if (ret)
  1140. goto put_child;
  1141. } else {
  1142. ret = rockchip_usb2phy_otg_port_init(rphy, rport,
  1143. child_np);
  1144. if (ret)
  1145. goto put_child;
  1146. }
  1147. next_child:
  1148. /* to prevent out of boundary */
  1149. if (++index >= rphy->phy_cfg->num_ports) {
  1150. of_node_put(child_np);
  1151. break;
  1152. }
  1153. }
  1154. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  1155. if (rphy->irq > 0) {
  1156. ret = devm_request_threaded_irq(rphy->dev, rphy->irq, NULL,
  1157. rockchip_usb2phy_irq,
  1158. IRQF_ONESHOT,
  1159. "rockchip_usb2phy",
  1160. rphy);
  1161. if (ret) {
  1162. dev_err(rphy->dev,
  1163. "failed to request usb2phy irq handle\n");
  1164. goto put_child;
  1165. }
  1166. }
  1167. return PTR_ERR_OR_ZERO(provider);
  1168. put_child:
  1169. of_node_put(child_np);
  1170. disable_clks:
  1171. if (rphy->clk) {
  1172. clk_disable_unprepare(rphy->clk);
  1173. clk_put(rphy->clk);
  1174. }
  1175. return ret;
  1176. }
  1177. static const struct rockchip_usb2phy_cfg rk3228_phy_cfgs[] = {
  1178. {
  1179. .reg = 0x760,
  1180. .num_ports = 2,
  1181. .clkout_ctl = { 0x0768, 4, 4, 1, 0 },
  1182. .port_cfgs = {
  1183. [USB2PHY_PORT_OTG] = {
  1184. .phy_sus = { 0x0760, 15, 0, 0, 0x1d1 },
  1185. .bvalid_det_en = { 0x0680, 3, 3, 0, 1 },
  1186. .bvalid_det_st = { 0x0690, 3, 3, 0, 1 },
  1187. .bvalid_det_clr = { 0x06a0, 3, 3, 0, 1 },
  1188. .id_det_en = { 0x0680, 6, 5, 0, 3 },
  1189. .id_det_st = { 0x0690, 6, 5, 0, 3 },
  1190. .id_det_clr = { 0x06a0, 6, 5, 0, 3 },
  1191. .ls_det_en = { 0x0680, 2, 2, 0, 1 },
  1192. .ls_det_st = { 0x0690, 2, 2, 0, 1 },
  1193. .ls_det_clr = { 0x06a0, 2, 2, 0, 1 },
  1194. .utmi_bvalid = { 0x0480, 4, 4, 0, 1 },
  1195. .utmi_id = { 0x0480, 1, 1, 0, 1 },
  1196. .utmi_ls = { 0x0480, 3, 2, 0, 1 },
  1197. },
  1198. [USB2PHY_PORT_HOST] = {
  1199. .phy_sus = { 0x0764, 15, 0, 0, 0x1d1 },
  1200. .ls_det_en = { 0x0680, 4, 4, 0, 1 },
  1201. .ls_det_st = { 0x0690, 4, 4, 0, 1 },
  1202. .ls_det_clr = { 0x06a0, 4, 4, 0, 1 }
  1203. }
  1204. },
  1205. .chg_det = {
  1206. .opmode = { 0x0760, 3, 0, 5, 1 },
  1207. .cp_det = { 0x0884, 4, 4, 0, 1 },
  1208. .dcp_det = { 0x0884, 3, 3, 0, 1 },
  1209. .dp_det = { 0x0884, 5, 5, 0, 1 },
  1210. .idm_sink_en = { 0x0768, 8, 8, 0, 1 },
  1211. .idp_sink_en = { 0x0768, 7, 7, 0, 1 },
  1212. .idp_src_en = { 0x0768, 9, 9, 0, 1 },
  1213. .rdm_pdwn_en = { 0x0768, 10, 10, 0, 1 },
  1214. .vdm_src_en = { 0x0768, 12, 12, 0, 1 },
  1215. .vdp_src_en = { 0x0768, 11, 11, 0, 1 },
  1216. },
  1217. },
  1218. {
  1219. .reg = 0x800,
  1220. .num_ports = 2,
  1221. .clkout_ctl = { 0x0808, 4, 4, 1, 0 },
  1222. .port_cfgs = {
  1223. [USB2PHY_PORT_OTG] = {
  1224. .phy_sus = { 0x800, 15, 0, 0, 0x1d1 },
  1225. .ls_det_en = { 0x0684, 0, 0, 0, 1 },
  1226. .ls_det_st = { 0x0694, 0, 0, 0, 1 },
  1227. .ls_det_clr = { 0x06a4, 0, 0, 0, 1 }
  1228. },
  1229. [USB2PHY_PORT_HOST] = {
  1230. .phy_sus = { 0x804, 15, 0, 0, 0x1d1 },
  1231. .ls_det_en = { 0x0684, 1, 1, 0, 1 },
  1232. .ls_det_st = { 0x0694, 1, 1, 0, 1 },
  1233. .ls_det_clr = { 0x06a4, 1, 1, 0, 1 }
  1234. }
  1235. },
  1236. },
  1237. { /* sentinel */ }
  1238. };
  1239. static const struct rockchip_usb2phy_cfg rk3308_phy_cfgs[] = {
  1240. {
  1241. .reg = 0x100,
  1242. .num_ports = 2,
  1243. .clkout_ctl = { 0x108, 4, 4, 1, 0 },
  1244. .port_cfgs = {
  1245. [USB2PHY_PORT_OTG] = {
  1246. .phy_sus = { 0x0100, 8, 0, 0, 0x1d1 },
  1247. .bvalid_det_en = { 0x3020, 3, 2, 0, 3 },
  1248. .bvalid_det_st = { 0x3024, 3, 2, 0, 3 },
  1249. .bvalid_det_clr = { 0x3028, 3, 2, 0, 3 },
  1250. .id_det_en = { 0x3020, 5, 4, 0, 3 },
  1251. .id_det_st = { 0x3024, 5, 4, 0, 3 },
  1252. .id_det_clr = { 0x3028, 5, 4, 0, 3 },
  1253. .ls_det_en = { 0x3020, 0, 0, 0, 1 },
  1254. .ls_det_st = { 0x3024, 0, 0, 0, 1 },
  1255. .ls_det_clr = { 0x3028, 0, 0, 0, 1 },
  1256. .utmi_avalid = { 0x0120, 10, 10, 0, 1 },
  1257. .utmi_bvalid = { 0x0120, 9, 9, 0, 1 },
  1258. .utmi_id = { 0x0120, 6, 6, 0, 1 },
  1259. .utmi_ls = { 0x0120, 5, 4, 0, 1 },
  1260. },
  1261. [USB2PHY_PORT_HOST] = {
  1262. .phy_sus = { 0x0104, 8, 0, 0, 0x1d1 },
  1263. .ls_det_en = { 0x3020, 1, 1, 0, 1 },
  1264. .ls_det_st = { 0x3024, 1, 1, 0, 1 },
  1265. .ls_det_clr = { 0x3028, 1, 1, 0, 1 },
  1266. .utmi_ls = { 0x0120, 17, 16, 0, 1 },
  1267. .utmi_hstdet = { 0x0120, 19, 19, 0, 1 }
  1268. }
  1269. },
  1270. .chg_det = {
  1271. .opmode = { 0x0100, 3, 0, 5, 1 },
  1272. .cp_det = { 0x0120, 24, 24, 0, 1 },
  1273. .dcp_det = { 0x0120, 23, 23, 0, 1 },
  1274. .dp_det = { 0x0120, 25, 25, 0, 1 },
  1275. .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
  1276. .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
  1277. .idp_src_en = { 0x0108, 9, 9, 0, 1 },
  1278. .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
  1279. .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
  1280. .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
  1281. },
  1282. },
  1283. { /* sentinel */ }
  1284. };
  1285. static const struct rockchip_usb2phy_cfg rk3328_phy_cfgs[] = {
  1286. {
  1287. .reg = 0x100,
  1288. .num_ports = 2,
  1289. .clkout_ctl = { 0x108, 4, 4, 1, 0 },
  1290. .port_cfgs = {
  1291. [USB2PHY_PORT_OTG] = {
  1292. .phy_sus = { 0x0100, 15, 0, 0, 0x1d1 },
  1293. .bvalid_det_en = { 0x0110, 3, 2, 0, 3 },
  1294. .bvalid_det_st = { 0x0114, 3, 2, 0, 3 },
  1295. .bvalid_det_clr = { 0x0118, 3, 2, 0, 3 },
  1296. .id_det_en = { 0x0110, 5, 4, 0, 3 },
  1297. .id_det_st = { 0x0114, 5, 4, 0, 3 },
  1298. .id_det_clr = { 0x0118, 5, 4, 0, 3 },
  1299. .ls_det_en = { 0x0110, 0, 0, 0, 1 },
  1300. .ls_det_st = { 0x0114, 0, 0, 0, 1 },
  1301. .ls_det_clr = { 0x0118, 0, 0, 0, 1 },
  1302. .utmi_avalid = { 0x0120, 10, 10, 0, 1 },
  1303. .utmi_bvalid = { 0x0120, 9, 9, 0, 1 },
  1304. .utmi_id = { 0x0120, 6, 6, 0, 1 },
  1305. .utmi_ls = { 0x0120, 5, 4, 0, 1 },
  1306. },
  1307. [USB2PHY_PORT_HOST] = {
  1308. .phy_sus = { 0x104, 15, 0, 0, 0x1d1 },
  1309. .ls_det_en = { 0x110, 1, 1, 0, 1 },
  1310. .ls_det_st = { 0x114, 1, 1, 0, 1 },
  1311. .ls_det_clr = { 0x118, 1, 1, 0, 1 },
  1312. .utmi_ls = { 0x120, 17, 16, 0, 1 },
  1313. .utmi_hstdet = { 0x120, 19, 19, 0, 1 }
  1314. }
  1315. },
  1316. .chg_det = {
  1317. .opmode = { 0x0100, 3, 0, 5, 1 },
  1318. .cp_det = { 0x0120, 24, 24, 0, 1 },
  1319. .dcp_det = { 0x0120, 23, 23, 0, 1 },
  1320. .dp_det = { 0x0120, 25, 25, 0, 1 },
  1321. .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
  1322. .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
  1323. .idp_src_en = { 0x0108, 9, 9, 0, 1 },
  1324. .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
  1325. .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
  1326. .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
  1327. },
  1328. },
  1329. { /* sentinel */ }
  1330. };
  1331. static const struct rockchip_usb2phy_cfg rk3366_phy_cfgs[] = {
  1332. {
  1333. .reg = 0x700,
  1334. .num_ports = 2,
  1335. .clkout_ctl = { 0x0724, 15, 15, 1, 0 },
  1336. .port_cfgs = {
  1337. [USB2PHY_PORT_HOST] = {
  1338. .phy_sus = { 0x0728, 15, 0, 0, 0x1d1 },
  1339. .ls_det_en = { 0x0680, 4, 4, 0, 1 },
  1340. .ls_det_st = { 0x0690, 4, 4, 0, 1 },
  1341. .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
  1342. .utmi_ls = { 0x049c, 14, 13, 0, 1 },
  1343. .utmi_hstdet = { 0x049c, 12, 12, 0, 1 }
  1344. }
  1345. },
  1346. },
  1347. { /* sentinel */ }
  1348. };
  1349. static const struct rockchip_usb2phy_cfg rk3399_phy_cfgs[] = {
  1350. {
  1351. .reg = 0xe450,
  1352. .num_ports = 2,
  1353. .clkout_ctl = { 0xe450, 4, 4, 1, 0 },
  1354. .port_cfgs = {
  1355. [USB2PHY_PORT_OTG] = {
  1356. .phy_sus = { 0xe454, 1, 0, 2, 1 },
  1357. .bvalid_det_en = { 0xe3c0, 3, 3, 0, 1 },
  1358. .bvalid_det_st = { 0xe3e0, 3, 3, 0, 1 },
  1359. .bvalid_det_clr = { 0xe3d0, 3, 3, 0, 1 },
  1360. .id_det_en = { 0xe3c0, 5, 4, 0, 3 },
  1361. .id_det_st = { 0xe3e0, 5, 4, 0, 3 },
  1362. .id_det_clr = { 0xe3d0, 5, 4, 0, 3 },
  1363. .utmi_avalid = { 0xe2ac, 7, 7, 0, 1 },
  1364. .utmi_bvalid = { 0xe2ac, 12, 12, 0, 1 },
  1365. .utmi_id = { 0xe2ac, 8, 8, 0, 1 },
  1366. },
  1367. [USB2PHY_PORT_HOST] = {
  1368. .phy_sus = { 0xe458, 1, 0, 0x2, 0x1 },
  1369. .ls_det_en = { 0xe3c0, 6, 6, 0, 1 },
  1370. .ls_det_st = { 0xe3e0, 6, 6, 0, 1 },
  1371. .ls_det_clr = { 0xe3d0, 6, 6, 0, 1 },
  1372. .utmi_ls = { 0xe2ac, 22, 21, 0, 1 },
  1373. .utmi_hstdet = { 0xe2ac, 23, 23, 0, 1 }
  1374. }
  1375. },
  1376. .chg_det = {
  1377. .opmode = { 0xe454, 3, 0, 5, 1 },
  1378. .cp_det = { 0xe2ac, 2, 2, 0, 1 },
  1379. .dcp_det = { 0xe2ac, 1, 1, 0, 1 },
  1380. .dp_det = { 0xe2ac, 0, 0, 0, 1 },
  1381. .idm_sink_en = { 0xe450, 8, 8, 0, 1 },
  1382. .idp_sink_en = { 0xe450, 7, 7, 0, 1 },
  1383. .idp_src_en = { 0xe450, 9, 9, 0, 1 },
  1384. .rdm_pdwn_en = { 0xe450, 10, 10, 0, 1 },
  1385. .vdm_src_en = { 0xe450, 12, 12, 0, 1 },
  1386. .vdp_src_en = { 0xe450, 11, 11, 0, 1 },
  1387. },
  1388. },
  1389. {
  1390. .reg = 0xe460,
  1391. .num_ports = 2,
  1392. .clkout_ctl = { 0xe460, 4, 4, 1, 0 },
  1393. .port_cfgs = {
  1394. [USB2PHY_PORT_OTG] = {
  1395. .phy_sus = { 0xe464, 1, 0, 2, 1 },
  1396. .bvalid_det_en = { 0xe3c0, 8, 8, 0, 1 },
  1397. .bvalid_det_st = { 0xe3e0, 8, 8, 0, 1 },
  1398. .bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 },
  1399. .id_det_en = { 0xe3c0, 10, 9, 0, 3 },
  1400. .id_det_st = { 0xe3e0, 10, 9, 0, 3 },
  1401. .id_det_clr = { 0xe3d0, 10, 9, 0, 3 },
  1402. .utmi_avalid = { 0xe2ac, 10, 10, 0, 1 },
  1403. .utmi_bvalid = { 0xe2ac, 16, 16, 0, 1 },
  1404. .utmi_id = { 0xe2ac, 11, 11, 0, 1 },
  1405. },
  1406. [USB2PHY_PORT_HOST] = {
  1407. .phy_sus = { 0xe468, 1, 0, 0x2, 0x1 },
  1408. .ls_det_en = { 0xe3c0, 11, 11, 0, 1 },
  1409. .ls_det_st = { 0xe3e0, 11, 11, 0, 1 },
  1410. .ls_det_clr = { 0xe3d0, 11, 11, 0, 1 },
  1411. .utmi_ls = { 0xe2ac, 26, 25, 0, 1 },
  1412. .utmi_hstdet = { 0xe2ac, 27, 27, 0, 1 }
  1413. }
  1414. },
  1415. },
  1416. { /* sentinel */ }
  1417. };
  1418. static const struct rockchip_usb2phy_cfg rk3568_phy_cfgs[] = {
  1419. {
  1420. .reg = 0xfe8a0000,
  1421. .num_ports = 2,
  1422. .clkout_ctl = { 0x0008, 4, 4, 1, 0 },
  1423. .port_cfgs = {
  1424. [USB2PHY_PORT_OTG] = {
  1425. .phy_sus = { 0x0000, 8, 0, 0, 0x1d1 },
  1426. .bvalid_det_en = { 0x0080, 3, 2, 0, 3 },
  1427. .bvalid_det_st = { 0x0084, 3, 2, 0, 3 },
  1428. .bvalid_det_clr = { 0x0088, 3, 2, 0, 3 },
  1429. .id_det_en = { 0x0080, 5, 4, 0, 3 },
  1430. .id_det_st = { 0x0084, 5, 4, 0, 3 },
  1431. .id_det_clr = { 0x0088, 5, 4, 0, 3 },
  1432. .utmi_avalid = { 0x00c0, 10, 10, 0, 1 },
  1433. .utmi_bvalid = { 0x00c0, 9, 9, 0, 1 },
  1434. .utmi_id = { 0x00c0, 6, 6, 0, 1 },
  1435. },
  1436. [USB2PHY_PORT_HOST] = {
  1437. /* Select suspend control from controller */
  1438. .phy_sus = { 0x0004, 8, 0, 0x1d2, 0x1d2 },
  1439. .ls_det_en = { 0x0080, 1, 1, 0, 1 },
  1440. .ls_det_st = { 0x0084, 1, 1, 0, 1 },
  1441. .ls_det_clr = { 0x0088, 1, 1, 0, 1 },
  1442. .utmi_ls = { 0x00c0, 17, 16, 0, 1 },
  1443. .utmi_hstdet = { 0x00c0, 19, 19, 0, 1 }
  1444. }
  1445. },
  1446. .chg_det = {
  1447. .opmode = { 0x0000, 3, 0, 5, 1 },
  1448. .cp_det = { 0x00c0, 24, 24, 0, 1 },
  1449. .dcp_det = { 0x00c0, 23, 23, 0, 1 },
  1450. .dp_det = { 0x00c0, 25, 25, 0, 1 },
  1451. .idm_sink_en = { 0x0008, 8, 8, 0, 1 },
  1452. .idp_sink_en = { 0x0008, 7, 7, 0, 1 },
  1453. .idp_src_en = { 0x0008, 9, 9, 0, 1 },
  1454. .rdm_pdwn_en = { 0x0008, 10, 10, 0, 1 },
  1455. .vdm_src_en = { 0x0008, 12, 12, 0, 1 },
  1456. .vdp_src_en = { 0x0008, 11, 11, 0, 1 },
  1457. },
  1458. },
  1459. {
  1460. .reg = 0xfe8b0000,
  1461. .num_ports = 2,
  1462. .clkout_ctl = { 0x0008, 4, 4, 1, 0 },
  1463. .port_cfgs = {
  1464. [USB2PHY_PORT_OTG] = {
  1465. .phy_sus = { 0x0000, 8, 0, 0x1d2, 0x1d1 },
  1466. .ls_det_en = { 0x0080, 0, 0, 0, 1 },
  1467. .ls_det_st = { 0x0084, 0, 0, 0, 1 },
  1468. .ls_det_clr = { 0x0088, 0, 0, 0, 1 },
  1469. .utmi_ls = { 0x00c0, 5, 4, 0, 1 },
  1470. .utmi_hstdet = { 0x00c0, 7, 7, 0, 1 }
  1471. },
  1472. [USB2PHY_PORT_HOST] = {
  1473. .phy_sus = { 0x0004, 8, 0, 0x1d2, 0x1d1 },
  1474. .ls_det_en = { 0x0080, 1, 1, 0, 1 },
  1475. .ls_det_st = { 0x0084, 1, 1, 0, 1 },
  1476. .ls_det_clr = { 0x0088, 1, 1, 0, 1 },
  1477. .utmi_ls = { 0x00c0, 17, 16, 0, 1 },
  1478. .utmi_hstdet = { 0x00c0, 19, 19, 0, 1 }
  1479. }
  1480. },
  1481. },
  1482. { /* sentinel */ }
  1483. };
  1484. static const struct rockchip_usb2phy_cfg rv1108_phy_cfgs[] = {
  1485. {
  1486. .reg = 0x100,
  1487. .num_ports = 2,
  1488. .clkout_ctl = { 0x108, 4, 4, 1, 0 },
  1489. .port_cfgs = {
  1490. [USB2PHY_PORT_OTG] = {
  1491. .phy_sus = { 0x0100, 15, 0, 0, 0x1d1 },
  1492. .bvalid_det_en = { 0x0680, 3, 3, 0, 1 },
  1493. .bvalid_det_st = { 0x0690, 3, 3, 0, 1 },
  1494. .bvalid_det_clr = { 0x06a0, 3, 3, 0, 1 },
  1495. .ls_det_en = { 0x0680, 2, 2, 0, 1 },
  1496. .ls_det_st = { 0x0690, 2, 2, 0, 1 },
  1497. .ls_det_clr = { 0x06a0, 2, 2, 0, 1 },
  1498. .utmi_bvalid = { 0x0804, 10, 10, 0, 1 },
  1499. .utmi_ls = { 0x0804, 13, 12, 0, 1 },
  1500. },
  1501. [USB2PHY_PORT_HOST] = {
  1502. .phy_sus = { 0x0104, 15, 0, 0, 0x1d1 },
  1503. .ls_det_en = { 0x0680, 4, 4, 0, 1 },
  1504. .ls_det_st = { 0x0690, 4, 4, 0, 1 },
  1505. .ls_det_clr = { 0x06a0, 4, 4, 0, 1 },
  1506. .utmi_ls = { 0x0804, 9, 8, 0, 1 },
  1507. .utmi_hstdet = { 0x0804, 7, 7, 0, 1 }
  1508. }
  1509. },
  1510. .chg_det = {
  1511. .opmode = { 0x0100, 3, 0, 5, 1 },
  1512. .cp_det = { 0x0804, 1, 1, 0, 1 },
  1513. .dcp_det = { 0x0804, 0, 0, 0, 1 },
  1514. .dp_det = { 0x0804, 2, 2, 0, 1 },
  1515. .idm_sink_en = { 0x0108, 8, 8, 0, 1 },
  1516. .idp_sink_en = { 0x0108, 7, 7, 0, 1 },
  1517. .idp_src_en = { 0x0108, 9, 9, 0, 1 },
  1518. .rdm_pdwn_en = { 0x0108, 10, 10, 0, 1 },
  1519. .vdm_src_en = { 0x0108, 12, 12, 0, 1 },
  1520. .vdp_src_en = { 0x0108, 11, 11, 0, 1 },
  1521. },
  1522. },
  1523. { /* sentinel */ }
  1524. };
  1525. static const struct of_device_id rockchip_usb2phy_dt_match[] = {
  1526. { .compatible = "rockchip,px30-usb2phy", .data = &rk3328_phy_cfgs },
  1527. { .compatible = "rockchip,rk3228-usb2phy", .data = &rk3228_phy_cfgs },
  1528. { .compatible = "rockchip,rk3308-usb2phy", .data = &rk3308_phy_cfgs },
  1529. { .compatible = "rockchip,rk3328-usb2phy", .data = &rk3328_phy_cfgs },
  1530. { .compatible = "rockchip,rk3366-usb2phy", .data = &rk3366_phy_cfgs },
  1531. { .compatible = "rockchip,rk3399-usb2phy", .data = &rk3399_phy_cfgs },
  1532. { .compatible = "rockchip,rk3568-usb2phy", .data = &rk3568_phy_cfgs },
  1533. { .compatible = "rockchip,rv1108-usb2phy", .data = &rv1108_phy_cfgs },
  1534. {}
  1535. };
  1536. MODULE_DEVICE_TABLE(of, rockchip_usb2phy_dt_match);
  1537. static struct platform_driver rockchip_usb2phy_driver = {
  1538. .probe = rockchip_usb2phy_probe,
  1539. .driver = {
  1540. .name = "rockchip-usb2phy",
  1541. .of_match_table = rockchip_usb2phy_dt_match,
  1542. },
  1543. };
  1544. module_platform_driver(rockchip_usb2phy_driver);
  1545. MODULE_AUTHOR("Frank Wang <[email protected]>");
  1546. MODULE_DESCRIPTION("Rockchip USB2.0 PHY driver");
  1547. MODULE_LICENSE("GPL v2");