xusb.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2022, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/io.h>
  7. #include <linux/mailbox_client.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/phy/tegra/xusb.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/reset.h>
  16. #include <linux/slab.h>
  17. #include <linux/workqueue.h>
  18. #include <soc/tegra/fuse.h>
  19. #include "xusb.h"
  20. static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
  21. struct of_phandle_args *args)
  22. {
  23. struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
  24. struct phy *phy = NULL;
  25. unsigned int i;
  26. if (args->args_count != 0)
  27. return ERR_PTR(-EINVAL);
  28. for (i = 0; i < pad->soc->num_lanes; i++) {
  29. if (!pad->lanes[i])
  30. continue;
  31. if (pad->lanes[i]->dev.of_node == args->np) {
  32. phy = pad->lanes[i];
  33. break;
  34. }
  35. }
  36. if (phy == NULL)
  37. phy = ERR_PTR(-ENODEV);
  38. return phy;
  39. }
  40. static const struct of_device_id tegra_xusb_padctl_of_match[] = {
  41. #if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
  42. {
  43. .compatible = "nvidia,tegra124-xusb-padctl",
  44. .data = &tegra124_xusb_padctl_soc,
  45. },
  46. #endif
  47. #if defined(CONFIG_ARCH_TEGRA_210_SOC)
  48. {
  49. .compatible = "nvidia,tegra210-xusb-padctl",
  50. .data = &tegra210_xusb_padctl_soc,
  51. },
  52. #endif
  53. #if defined(CONFIG_ARCH_TEGRA_186_SOC)
  54. {
  55. .compatible = "nvidia,tegra186-xusb-padctl",
  56. .data = &tegra186_xusb_padctl_soc,
  57. },
  58. #endif
  59. #if defined(CONFIG_ARCH_TEGRA_194_SOC)
  60. {
  61. .compatible = "nvidia,tegra194-xusb-padctl",
  62. .data = &tegra194_xusb_padctl_soc,
  63. },
  64. #endif
  65. { }
  66. };
  67. MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
  68. static struct device_node *
  69. tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
  70. {
  71. struct device_node *pads, *np;
  72. pads = of_get_child_by_name(padctl->dev->of_node, "pads");
  73. if (!pads)
  74. return NULL;
  75. np = of_get_child_by_name(pads, name);
  76. of_node_put(pads);
  77. return np;
  78. }
  79. static struct device_node *
  80. tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
  81. {
  82. struct device_node *np, *lanes;
  83. lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
  84. if (!lanes)
  85. return NULL;
  86. np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
  87. of_node_put(lanes);
  88. return np;
  89. }
  90. int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
  91. struct device_node *np)
  92. {
  93. struct device *dev = &lane->pad->dev;
  94. const char *function;
  95. int err;
  96. err = of_property_read_string(np, "nvidia,function", &function);
  97. if (err < 0)
  98. return err;
  99. err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
  100. if (err < 0) {
  101. dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
  102. function, np);
  103. return err;
  104. }
  105. lane->function = err;
  106. return 0;
  107. }
  108. static void tegra_xusb_lane_destroy(struct phy *phy)
  109. {
  110. if (phy) {
  111. struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
  112. lane->pad->ops->remove(lane);
  113. phy_destroy(phy);
  114. }
  115. }
  116. static void tegra_xusb_pad_release(struct device *dev)
  117. {
  118. struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
  119. pad->soc->ops->remove(pad);
  120. }
  121. static const struct device_type tegra_xusb_pad_type = {
  122. .release = tegra_xusb_pad_release,
  123. };
  124. int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
  125. struct tegra_xusb_padctl *padctl,
  126. struct device_node *np)
  127. {
  128. int err;
  129. device_initialize(&pad->dev);
  130. INIT_LIST_HEAD(&pad->list);
  131. pad->dev.parent = padctl->dev;
  132. pad->dev.type = &tegra_xusb_pad_type;
  133. pad->dev.of_node = np;
  134. pad->padctl = padctl;
  135. err = dev_set_name(&pad->dev, "%s", pad->soc->name);
  136. if (err < 0)
  137. goto unregister;
  138. err = device_add(&pad->dev);
  139. if (err < 0)
  140. goto unregister;
  141. return 0;
  142. unregister:
  143. device_unregister(&pad->dev);
  144. return err;
  145. }
  146. int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
  147. const struct phy_ops *ops)
  148. {
  149. struct device_node *children;
  150. struct phy *lane;
  151. unsigned int i;
  152. int err;
  153. children = of_get_child_by_name(pad->dev.of_node, "lanes");
  154. if (!children)
  155. return -ENODEV;
  156. pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
  157. GFP_KERNEL);
  158. if (!pad->lanes) {
  159. of_node_put(children);
  160. return -ENOMEM;
  161. }
  162. for (i = 0; i < pad->soc->num_lanes; i++) {
  163. struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
  164. struct tegra_xusb_lane *lane;
  165. /* skip disabled lanes */
  166. if (!np || !of_device_is_available(np)) {
  167. of_node_put(np);
  168. continue;
  169. }
  170. pad->lanes[i] = phy_create(&pad->dev, np, ops);
  171. if (IS_ERR(pad->lanes[i])) {
  172. err = PTR_ERR(pad->lanes[i]);
  173. of_node_put(np);
  174. goto remove;
  175. }
  176. lane = pad->ops->probe(pad, np, i);
  177. if (IS_ERR(lane)) {
  178. phy_destroy(pad->lanes[i]);
  179. err = PTR_ERR(lane);
  180. goto remove;
  181. }
  182. list_add_tail(&lane->list, &pad->padctl->lanes);
  183. phy_set_drvdata(pad->lanes[i], lane);
  184. }
  185. pad->provider = of_phy_provider_register_full(&pad->dev, children,
  186. tegra_xusb_pad_of_xlate);
  187. if (IS_ERR(pad->provider)) {
  188. err = PTR_ERR(pad->provider);
  189. goto remove;
  190. }
  191. return 0;
  192. remove:
  193. while (i--)
  194. tegra_xusb_lane_destroy(pad->lanes[i]);
  195. of_node_put(children);
  196. return err;
  197. }
  198. void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
  199. {
  200. unsigned int i = pad->soc->num_lanes;
  201. of_phy_provider_unregister(pad->provider);
  202. while (i--)
  203. tegra_xusb_lane_destroy(pad->lanes[i]);
  204. device_unregister(&pad->dev);
  205. }
  206. static struct tegra_xusb_pad *
  207. tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
  208. const struct tegra_xusb_pad_soc *soc)
  209. {
  210. struct tegra_xusb_pad *pad;
  211. struct device_node *np;
  212. int err;
  213. np = tegra_xusb_find_pad_node(padctl, soc->name);
  214. if (!np || !of_device_is_available(np))
  215. return NULL;
  216. pad = soc->ops->probe(padctl, soc, np);
  217. if (IS_ERR(pad)) {
  218. err = PTR_ERR(pad);
  219. dev_err(padctl->dev, "failed to create pad %s: %d\n",
  220. soc->name, err);
  221. return ERR_PTR(err);
  222. }
  223. /* XXX move this into ->probe() to avoid string comparison */
  224. if (strcmp(soc->name, "pcie") == 0)
  225. padctl->pcie = pad;
  226. if (strcmp(soc->name, "sata") == 0)
  227. padctl->sata = pad;
  228. if (strcmp(soc->name, "usb2") == 0)
  229. padctl->usb2 = pad;
  230. if (strcmp(soc->name, "ulpi") == 0)
  231. padctl->ulpi = pad;
  232. if (strcmp(soc->name, "hsic") == 0)
  233. padctl->hsic = pad;
  234. return pad;
  235. }
  236. static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
  237. {
  238. struct tegra_xusb_pad *pad, *tmp;
  239. list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
  240. list_del(&pad->list);
  241. tegra_xusb_pad_unregister(pad);
  242. }
  243. }
  244. static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
  245. {
  246. mutex_lock(&padctl->lock);
  247. __tegra_xusb_remove_pads(padctl);
  248. mutex_unlock(&padctl->lock);
  249. }
  250. static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
  251. {
  252. struct tegra_xusb_padctl *padctl = lane->pad->padctl;
  253. const struct tegra_xusb_lane_soc *soc = lane->soc;
  254. u32 value;
  255. /* skip single function lanes */
  256. if (soc->num_funcs < 2)
  257. return;
  258. if (lane->pad->ops->iddq_enable)
  259. lane->pad->ops->iddq_enable(lane);
  260. /* choose function */
  261. value = padctl_readl(padctl, soc->offset);
  262. value &= ~(soc->mask << soc->shift);
  263. value |= lane->function << soc->shift;
  264. padctl_writel(padctl, value, soc->offset);
  265. if (lane->pad->ops->iddq_disable)
  266. lane->pad->ops->iddq_disable(lane);
  267. }
  268. static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
  269. {
  270. unsigned int i;
  271. for (i = 0; i < pad->soc->num_lanes; i++) {
  272. struct tegra_xusb_lane *lane;
  273. if (pad->lanes[i]) {
  274. lane = phy_get_drvdata(pad->lanes[i]);
  275. tegra_xusb_lane_program(lane);
  276. }
  277. }
  278. }
  279. static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
  280. {
  281. struct tegra_xusb_pad *pad;
  282. unsigned int i;
  283. mutex_lock(&padctl->lock);
  284. for (i = 0; i < padctl->soc->num_pads; i++) {
  285. const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
  286. int err;
  287. pad = tegra_xusb_pad_create(padctl, soc);
  288. if (IS_ERR(pad)) {
  289. err = PTR_ERR(pad);
  290. dev_err(padctl->dev, "failed to create pad %s: %d\n",
  291. soc->name, err);
  292. __tegra_xusb_remove_pads(padctl);
  293. mutex_unlock(&padctl->lock);
  294. return err;
  295. }
  296. if (!pad)
  297. continue;
  298. list_add_tail(&pad->list, &padctl->pads);
  299. }
  300. list_for_each_entry(pad, &padctl->pads, list)
  301. tegra_xusb_pad_program(pad);
  302. mutex_unlock(&padctl->lock);
  303. return 0;
  304. }
  305. bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
  306. const char *function)
  307. {
  308. const char *func = lane->soc->funcs[lane->function];
  309. return strcmp(function, func) == 0;
  310. }
  311. struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
  312. const char *type,
  313. unsigned int index)
  314. {
  315. struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
  316. char *name;
  317. name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
  318. if (!name)
  319. return ERR_PTR(-ENOMEM);
  320. list_for_each_entry(lane, &padctl->lanes, list) {
  321. if (strcmp(lane->soc->name, name) == 0) {
  322. hit = lane;
  323. break;
  324. }
  325. }
  326. kfree(name);
  327. return hit;
  328. }
  329. struct tegra_xusb_lane *
  330. tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
  331. const struct tegra_xusb_lane_map *map,
  332. const char *function)
  333. {
  334. struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
  335. for (; map->type; map++) {
  336. if (port->index != map->port)
  337. continue;
  338. lane = tegra_xusb_find_lane(port->padctl, map->type,
  339. map->index);
  340. if (IS_ERR(lane))
  341. continue;
  342. if (!tegra_xusb_lane_check(lane, function))
  343. continue;
  344. if (!IS_ERR(match))
  345. dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
  346. map->type, map->index, match->soc->name);
  347. else
  348. match = lane;
  349. }
  350. return match;
  351. }
  352. static struct device_node *
  353. tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
  354. unsigned int index)
  355. {
  356. struct device_node *ports, *np;
  357. char *name;
  358. ports = of_get_child_by_name(padctl->dev->of_node, "ports");
  359. if (!ports)
  360. return NULL;
  361. name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
  362. if (!name) {
  363. of_node_put(ports);
  364. return NULL;
  365. }
  366. np = of_get_child_by_name(ports, name);
  367. kfree(name);
  368. of_node_put(ports);
  369. return np;
  370. }
  371. struct tegra_xusb_port *
  372. tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
  373. unsigned int index)
  374. {
  375. struct tegra_xusb_port *port;
  376. struct device_node *np;
  377. np = tegra_xusb_find_port_node(padctl, type, index);
  378. if (!np)
  379. return NULL;
  380. list_for_each_entry(port, &padctl->ports, list) {
  381. if (np == port->dev.of_node) {
  382. of_node_put(np);
  383. return port;
  384. }
  385. }
  386. of_node_put(np);
  387. return NULL;
  388. }
  389. struct tegra_xusb_usb2_port *
  390. tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
  391. {
  392. struct tegra_xusb_port *port;
  393. port = tegra_xusb_find_port(padctl, "usb2", index);
  394. if (port)
  395. return to_usb2_port(port);
  396. return NULL;
  397. }
  398. struct tegra_xusb_usb3_port *
  399. tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
  400. {
  401. struct tegra_xusb_port *port;
  402. port = tegra_xusb_find_port(padctl, "usb3", index);
  403. if (port)
  404. return to_usb3_port(port);
  405. return NULL;
  406. }
  407. static void tegra_xusb_port_release(struct device *dev)
  408. {
  409. struct tegra_xusb_port *port = to_tegra_xusb_port(dev);
  410. if (port->ops->release)
  411. port->ops->release(port);
  412. }
  413. static const struct device_type tegra_xusb_port_type = {
  414. .release = tegra_xusb_port_release,
  415. };
  416. static int tegra_xusb_port_init(struct tegra_xusb_port *port,
  417. struct tegra_xusb_padctl *padctl,
  418. struct device_node *np,
  419. const char *name,
  420. unsigned int index)
  421. {
  422. int err;
  423. INIT_LIST_HEAD(&port->list);
  424. port->padctl = padctl;
  425. port->index = index;
  426. device_initialize(&port->dev);
  427. port->dev.type = &tegra_xusb_port_type;
  428. port->dev.of_node = of_node_get(np);
  429. port->dev.parent = padctl->dev;
  430. err = dev_set_name(&port->dev, "%s-%u", name, index);
  431. if (err < 0)
  432. goto unregister;
  433. err = device_add(&port->dev);
  434. if (err < 0)
  435. goto unregister;
  436. return 0;
  437. unregister:
  438. device_unregister(&port->dev);
  439. return err;
  440. }
  441. static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
  442. {
  443. if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
  444. of_platform_depopulate(&port->dev);
  445. usb_role_switch_unregister(port->usb_role_sw);
  446. cancel_work_sync(&port->usb_phy_work);
  447. usb_remove_phy(&port->usb_phy);
  448. port->usb_phy.dev->driver = NULL;
  449. }
  450. if (port->ops->remove)
  451. port->ops->remove(port);
  452. device_unregister(&port->dev);
  453. }
  454. static const char *const modes[] = {
  455. [USB_DR_MODE_UNKNOWN] = "",
  456. [USB_DR_MODE_HOST] = "host",
  457. [USB_DR_MODE_PERIPHERAL] = "peripheral",
  458. [USB_DR_MODE_OTG] = "otg",
  459. };
  460. static const char * const usb_roles[] = {
  461. [USB_ROLE_NONE] = "none",
  462. [USB_ROLE_HOST] = "host",
  463. [USB_ROLE_DEVICE] = "device",
  464. };
  465. static enum usb_phy_events to_usb_phy_event(enum usb_role role)
  466. {
  467. switch (role) {
  468. case USB_ROLE_DEVICE:
  469. return USB_EVENT_VBUS;
  470. case USB_ROLE_HOST:
  471. return USB_EVENT_ID;
  472. default:
  473. return USB_EVENT_NONE;
  474. }
  475. }
  476. static void tegra_xusb_usb_phy_work(struct work_struct *work)
  477. {
  478. struct tegra_xusb_port *port = container_of(work,
  479. struct tegra_xusb_port,
  480. usb_phy_work);
  481. enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
  482. usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
  483. dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
  484. usb_roles[role]);
  485. atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
  486. }
  487. static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
  488. enum usb_role role)
  489. {
  490. struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
  491. dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
  492. schedule_work(&port->usb_phy_work);
  493. return 0;
  494. }
  495. static int tegra_xusb_set_peripheral(struct usb_otg *otg,
  496. struct usb_gadget *gadget)
  497. {
  498. struct tegra_xusb_port *port = container_of(otg->usb_phy,
  499. struct tegra_xusb_port,
  500. usb_phy);
  501. if (gadget != NULL)
  502. schedule_work(&port->usb_phy_work);
  503. return 0;
  504. }
  505. static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
  506. {
  507. struct tegra_xusb_port *port = container_of(otg->usb_phy,
  508. struct tegra_xusb_port,
  509. usb_phy);
  510. if (host != NULL)
  511. schedule_work(&port->usb_phy_work);
  512. return 0;
  513. }
  514. static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
  515. {
  516. struct tegra_xusb_lane *lane;
  517. struct usb_role_switch_desc role_sx_desc = {
  518. .fwnode = dev_fwnode(&port->dev),
  519. .set = tegra_xusb_role_sw_set,
  520. .allow_userspace_control = true,
  521. };
  522. int err = 0;
  523. /*
  524. * USB role switch driver needs parent driver owner info. This is a
  525. * suboptimal solution. TODO: Need to revisit this in a follow-up patch
  526. * where an optimal solution is possible with changes to USB role
  527. * switch driver.
  528. */
  529. port->dev.driver = devm_kzalloc(&port->dev,
  530. sizeof(struct device_driver),
  531. GFP_KERNEL);
  532. if (!port->dev.driver)
  533. return -ENOMEM;
  534. port->dev.driver->owner = THIS_MODULE;
  535. port->usb_role_sw = usb_role_switch_register(&port->dev,
  536. &role_sx_desc);
  537. if (IS_ERR(port->usb_role_sw)) {
  538. err = PTR_ERR(port->usb_role_sw);
  539. dev_err(&port->dev, "failed to register USB role switch: %d",
  540. err);
  541. return err;
  542. }
  543. INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
  544. usb_role_switch_set_drvdata(port->usb_role_sw, port);
  545. port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
  546. GFP_KERNEL);
  547. if (!port->usb_phy.otg)
  548. return -ENOMEM;
  549. lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
  550. /*
  551. * Assign phy dev to usb-phy dev. Host/device drivers can use phy
  552. * reference to retrieve usb-phy details.
  553. */
  554. port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
  555. port->usb_phy.dev->driver = port->dev.driver;
  556. port->usb_phy.otg->usb_phy = &port->usb_phy;
  557. port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
  558. port->usb_phy.otg->set_host = tegra_xusb_set_host;
  559. err = usb_add_phy_dev(&port->usb_phy);
  560. if (err < 0) {
  561. dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
  562. return err;
  563. }
  564. /* populate connector entry */
  565. of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
  566. return err;
  567. }
  568. static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
  569. {
  570. struct tegra_xusb_port *port = &usb2->base;
  571. struct device_node *np = port->dev.of_node;
  572. const char *mode;
  573. int err;
  574. usb2->internal = of_property_read_bool(np, "nvidia,internal");
  575. if (!of_property_read_string(np, "mode", &mode)) {
  576. int err = match_string(modes, ARRAY_SIZE(modes), mode);
  577. if (err < 0) {
  578. dev_err(&port->dev, "invalid value %s for \"mode\"\n",
  579. mode);
  580. usb2->mode = USB_DR_MODE_UNKNOWN;
  581. } else {
  582. usb2->mode = err;
  583. }
  584. } else {
  585. usb2->mode = USB_DR_MODE_HOST;
  586. }
  587. /* usb-role-switch property is mandatory for OTG/Peripheral modes */
  588. if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
  589. usb2->mode == USB_DR_MODE_OTG) {
  590. if (of_property_read_bool(np, "usb-role-switch")) {
  591. err = tegra_xusb_setup_usb_role_switch(port);
  592. if (err < 0)
  593. return err;
  594. } else {
  595. dev_err(&port->dev, "usb-role-switch not found for %s mode",
  596. modes[usb2->mode]);
  597. return -EINVAL;
  598. }
  599. }
  600. usb2->supply = regulator_get(&port->dev, "vbus");
  601. return PTR_ERR_OR_ZERO(usb2->supply);
  602. }
  603. static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
  604. unsigned int index)
  605. {
  606. struct tegra_xusb_usb2_port *usb2;
  607. struct device_node *np;
  608. int err = 0;
  609. /*
  610. * USB2 ports don't require additional properties, but if the port is
  611. * marked as disabled there is no reason to register it.
  612. */
  613. np = tegra_xusb_find_port_node(padctl, "usb2", index);
  614. if (!np || !of_device_is_available(np))
  615. goto out;
  616. usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
  617. if (!usb2) {
  618. err = -ENOMEM;
  619. goto out;
  620. }
  621. err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
  622. if (err < 0)
  623. goto out;
  624. usb2->base.ops = padctl->soc->ports.usb2.ops;
  625. usb2->base.lane = usb2->base.ops->map(&usb2->base);
  626. if (IS_ERR(usb2->base.lane)) {
  627. err = PTR_ERR(usb2->base.lane);
  628. tegra_xusb_port_unregister(&usb2->base);
  629. goto out;
  630. }
  631. err = tegra_xusb_usb2_port_parse_dt(usb2);
  632. if (err < 0) {
  633. tegra_xusb_port_unregister(&usb2->base);
  634. goto out;
  635. }
  636. list_add_tail(&usb2->base.list, &padctl->ports);
  637. out:
  638. of_node_put(np);
  639. return err;
  640. }
  641. void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
  642. {
  643. struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
  644. kfree(usb2);
  645. }
  646. void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
  647. {
  648. struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
  649. regulator_put(usb2->supply);
  650. }
  651. static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
  652. {
  653. struct tegra_xusb_port *port = &ulpi->base;
  654. struct device_node *np = port->dev.of_node;
  655. ulpi->internal = of_property_read_bool(np, "nvidia,internal");
  656. return 0;
  657. }
  658. static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
  659. unsigned int index)
  660. {
  661. struct tegra_xusb_ulpi_port *ulpi;
  662. struct device_node *np;
  663. int err = 0;
  664. np = tegra_xusb_find_port_node(padctl, "ulpi", index);
  665. if (!np || !of_device_is_available(np))
  666. goto out;
  667. ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
  668. if (!ulpi) {
  669. err = -ENOMEM;
  670. goto out;
  671. }
  672. err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
  673. if (err < 0)
  674. goto out;
  675. ulpi->base.ops = padctl->soc->ports.ulpi.ops;
  676. ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
  677. if (IS_ERR(ulpi->base.lane)) {
  678. err = PTR_ERR(ulpi->base.lane);
  679. tegra_xusb_port_unregister(&ulpi->base);
  680. goto out;
  681. }
  682. err = tegra_xusb_ulpi_port_parse_dt(ulpi);
  683. if (err < 0) {
  684. tegra_xusb_port_unregister(&ulpi->base);
  685. goto out;
  686. }
  687. list_add_tail(&ulpi->base.list, &padctl->ports);
  688. out:
  689. of_node_put(np);
  690. return err;
  691. }
  692. void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
  693. {
  694. struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
  695. kfree(ulpi);
  696. }
  697. static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
  698. {
  699. /* XXX */
  700. return 0;
  701. }
  702. static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
  703. unsigned int index)
  704. {
  705. struct tegra_xusb_hsic_port *hsic;
  706. struct device_node *np;
  707. int err = 0;
  708. np = tegra_xusb_find_port_node(padctl, "hsic", index);
  709. if (!np || !of_device_is_available(np))
  710. goto out;
  711. hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
  712. if (!hsic) {
  713. err = -ENOMEM;
  714. goto out;
  715. }
  716. err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
  717. if (err < 0)
  718. goto out;
  719. hsic->base.ops = padctl->soc->ports.hsic.ops;
  720. hsic->base.lane = hsic->base.ops->map(&hsic->base);
  721. if (IS_ERR(hsic->base.lane)) {
  722. err = PTR_ERR(hsic->base.lane);
  723. goto out;
  724. }
  725. err = tegra_xusb_hsic_port_parse_dt(hsic);
  726. if (err < 0) {
  727. tegra_xusb_port_unregister(&hsic->base);
  728. goto out;
  729. }
  730. list_add_tail(&hsic->base.list, &padctl->ports);
  731. out:
  732. of_node_put(np);
  733. return err;
  734. }
  735. void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
  736. {
  737. struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
  738. kfree(hsic);
  739. }
  740. static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
  741. {
  742. struct tegra_xusb_port *port = &usb3->base;
  743. struct device_node *np = port->dev.of_node;
  744. enum usb_device_speed maximum_speed;
  745. u32 value;
  746. int err;
  747. err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
  748. if (err < 0) {
  749. dev_err(&port->dev, "failed to read port: %d\n", err);
  750. return err;
  751. }
  752. usb3->port = value;
  753. usb3->internal = of_property_read_bool(np, "nvidia,internal");
  754. if (device_property_present(&port->dev, "maximum-speed")) {
  755. maximum_speed = usb_get_maximum_speed(&port->dev);
  756. if (maximum_speed == USB_SPEED_SUPER)
  757. usb3->disable_gen2 = true;
  758. else if (maximum_speed == USB_SPEED_SUPER_PLUS)
  759. usb3->disable_gen2 = false;
  760. else
  761. return -EINVAL;
  762. }
  763. usb3->supply = regulator_get(&port->dev, "vbus");
  764. return PTR_ERR_OR_ZERO(usb3->supply);
  765. }
  766. static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
  767. unsigned int index)
  768. {
  769. struct tegra_xusb_usb3_port *usb3;
  770. struct device_node *np;
  771. int err = 0;
  772. /*
  773. * If there is no supplemental configuration in the device tree the
  774. * port is unusable. But it is valid to configure only a single port,
  775. * hence return 0 instead of an error to allow ports to be optional.
  776. */
  777. np = tegra_xusb_find_port_node(padctl, "usb3", index);
  778. if (!np || !of_device_is_available(np))
  779. goto out;
  780. usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
  781. if (!usb3) {
  782. err = -ENOMEM;
  783. goto out;
  784. }
  785. err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
  786. if (err < 0)
  787. goto out;
  788. usb3->base.ops = padctl->soc->ports.usb3.ops;
  789. usb3->base.lane = usb3->base.ops->map(&usb3->base);
  790. if (IS_ERR(usb3->base.lane)) {
  791. err = PTR_ERR(usb3->base.lane);
  792. goto out;
  793. }
  794. err = tegra_xusb_usb3_port_parse_dt(usb3);
  795. if (err < 0) {
  796. tegra_xusb_port_unregister(&usb3->base);
  797. goto out;
  798. }
  799. list_add_tail(&usb3->base.list, &padctl->ports);
  800. out:
  801. of_node_put(np);
  802. return err;
  803. }
  804. void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
  805. {
  806. struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
  807. kfree(usb3);
  808. }
  809. void tegra_xusb_usb3_port_remove(struct tegra_xusb_port *port)
  810. {
  811. struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
  812. regulator_put(usb3->supply);
  813. }
  814. static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
  815. {
  816. struct tegra_xusb_port *port, *tmp;
  817. list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
  818. list_del(&port->list);
  819. tegra_xusb_port_unregister(port);
  820. }
  821. }
  822. static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
  823. {
  824. struct device_node *np;
  825. unsigned int i;
  826. for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
  827. np = tegra_xusb_find_port_node(padctl, "usb3", i);
  828. if (!np || !of_device_is_available(np))
  829. return i;
  830. }
  831. return -ENODEV;
  832. }
  833. static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
  834. {
  835. unsigned int i;
  836. struct tegra_xusb_usb3_port *usb3;
  837. struct tegra_xusb_padctl *padctl = usb2->base.padctl;
  838. for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
  839. usb3 = tegra_xusb_find_usb3_port(padctl, i);
  840. if (usb3 && usb3->port == usb2->base.index)
  841. return true;
  842. }
  843. return false;
  844. }
  845. static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
  846. {
  847. int fake;
  848. /* Disable usb3_port_fake usage by default and assign if needed */
  849. usb2->usb3_port_fake = -1;
  850. if ((usb2->mode == USB_DR_MODE_OTG ||
  851. usb2->mode == USB_DR_MODE_PERIPHERAL) &&
  852. !tegra_xusb_port_is_companion(usb2)) {
  853. fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
  854. if (fake < 0) {
  855. dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
  856. return -ENODEV;
  857. }
  858. dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
  859. usb2->usb3_port_fake = fake;
  860. }
  861. return 0;
  862. }
  863. static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
  864. {
  865. struct tegra_xusb_port *port;
  866. struct tegra_xusb_usb2_port *usb2;
  867. unsigned int i;
  868. int err = 0;
  869. mutex_lock(&padctl->lock);
  870. for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
  871. err = tegra_xusb_add_usb2_port(padctl, i);
  872. if (err < 0)
  873. goto remove_ports;
  874. }
  875. for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
  876. err = tegra_xusb_add_ulpi_port(padctl, i);
  877. if (err < 0)
  878. goto remove_ports;
  879. }
  880. for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
  881. err = tegra_xusb_add_hsic_port(padctl, i);
  882. if (err < 0)
  883. goto remove_ports;
  884. }
  885. for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
  886. err = tegra_xusb_add_usb3_port(padctl, i);
  887. if (err < 0)
  888. goto remove_ports;
  889. }
  890. if (padctl->soc->need_fake_usb3_port) {
  891. for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
  892. usb2 = tegra_xusb_find_usb2_port(padctl, i);
  893. if (!usb2)
  894. continue;
  895. err = tegra_xusb_update_usb3_fake_port(usb2);
  896. if (err < 0)
  897. goto remove_ports;
  898. }
  899. }
  900. list_for_each_entry(port, &padctl->ports, list) {
  901. err = port->ops->enable(port);
  902. if (err < 0)
  903. dev_err(padctl->dev, "failed to enable port %s: %d\n",
  904. dev_name(&port->dev), err);
  905. }
  906. goto unlock;
  907. remove_ports:
  908. __tegra_xusb_remove_ports(padctl);
  909. unlock:
  910. mutex_unlock(&padctl->lock);
  911. return err;
  912. }
  913. static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
  914. {
  915. mutex_lock(&padctl->lock);
  916. __tegra_xusb_remove_ports(padctl);
  917. mutex_unlock(&padctl->lock);
  918. }
  919. static int tegra_xusb_padctl_probe(struct platform_device *pdev)
  920. {
  921. struct device_node *np = pdev->dev.of_node;
  922. const struct tegra_xusb_padctl_soc *soc;
  923. struct tegra_xusb_padctl *padctl;
  924. const struct of_device_id *match;
  925. int err;
  926. /* for backwards compatibility with old device trees */
  927. np = of_get_child_by_name(np, "pads");
  928. if (!np) {
  929. dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
  930. return tegra_xusb_padctl_legacy_probe(pdev);
  931. }
  932. of_node_put(np);
  933. match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
  934. soc = match->data;
  935. padctl = soc->ops->probe(&pdev->dev, soc);
  936. if (IS_ERR(padctl))
  937. return PTR_ERR(padctl);
  938. platform_set_drvdata(pdev, padctl);
  939. INIT_LIST_HEAD(&padctl->ports);
  940. INIT_LIST_HEAD(&padctl->lanes);
  941. INIT_LIST_HEAD(&padctl->pads);
  942. mutex_init(&padctl->lock);
  943. padctl->regs = devm_platform_ioremap_resource(pdev, 0);
  944. if (IS_ERR(padctl->regs)) {
  945. err = PTR_ERR(padctl->regs);
  946. goto remove;
  947. }
  948. padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
  949. if (IS_ERR(padctl->rst)) {
  950. err = PTR_ERR(padctl->rst);
  951. goto remove;
  952. }
  953. padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
  954. sizeof(*padctl->supplies), GFP_KERNEL);
  955. if (!padctl->supplies) {
  956. err = -ENOMEM;
  957. goto remove;
  958. }
  959. regulator_bulk_set_supply_names(padctl->supplies,
  960. padctl->soc->supply_names,
  961. padctl->soc->num_supplies);
  962. err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
  963. padctl->supplies);
  964. if (err < 0) {
  965. dev_err_probe(&pdev->dev, err, "failed to get regulators\n");
  966. goto remove;
  967. }
  968. err = reset_control_deassert(padctl->rst);
  969. if (err < 0)
  970. goto remove;
  971. err = regulator_bulk_enable(padctl->soc->num_supplies,
  972. padctl->supplies);
  973. if (err < 0) {
  974. dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
  975. goto reset;
  976. }
  977. err = tegra_xusb_setup_pads(padctl);
  978. if (err < 0) {
  979. dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
  980. goto power_down;
  981. }
  982. err = tegra_xusb_setup_ports(padctl);
  983. if (err) {
  984. const char *level = KERN_ERR;
  985. if (err == -EPROBE_DEFER)
  986. level = KERN_DEBUG;
  987. dev_printk(level, &pdev->dev,
  988. dev_fmt("failed to setup XUSB ports: %d\n"), err);
  989. goto remove_pads;
  990. }
  991. return 0;
  992. remove_pads:
  993. tegra_xusb_remove_pads(padctl);
  994. power_down:
  995. regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
  996. reset:
  997. reset_control_assert(padctl->rst);
  998. remove:
  999. platform_set_drvdata(pdev, NULL);
  1000. soc->ops->remove(padctl);
  1001. return err;
  1002. }
  1003. static int tegra_xusb_padctl_remove(struct platform_device *pdev)
  1004. {
  1005. struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
  1006. int err;
  1007. tegra_xusb_remove_ports(padctl);
  1008. tegra_xusb_remove_pads(padctl);
  1009. err = regulator_bulk_disable(padctl->soc->num_supplies,
  1010. padctl->supplies);
  1011. if (err < 0)
  1012. dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
  1013. err = reset_control_assert(padctl->rst);
  1014. if (err < 0)
  1015. dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
  1016. padctl->soc->ops->remove(padctl);
  1017. return 0;
  1018. }
  1019. static __maybe_unused int tegra_xusb_padctl_suspend_noirq(struct device *dev)
  1020. {
  1021. struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
  1022. if (padctl->soc && padctl->soc->ops && padctl->soc->ops->suspend_noirq)
  1023. return padctl->soc->ops->suspend_noirq(padctl);
  1024. return 0;
  1025. }
  1026. static __maybe_unused int tegra_xusb_padctl_resume_noirq(struct device *dev)
  1027. {
  1028. struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
  1029. if (padctl->soc && padctl->soc->ops && padctl->soc->ops->resume_noirq)
  1030. return padctl->soc->ops->resume_noirq(padctl);
  1031. return 0;
  1032. }
  1033. static const struct dev_pm_ops tegra_xusb_padctl_pm_ops = {
  1034. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_xusb_padctl_suspend_noirq,
  1035. tegra_xusb_padctl_resume_noirq)
  1036. };
  1037. static struct platform_driver tegra_xusb_padctl_driver = {
  1038. .driver = {
  1039. .name = "tegra-xusb-padctl",
  1040. .of_match_table = tegra_xusb_padctl_of_match,
  1041. .pm = &tegra_xusb_padctl_pm_ops,
  1042. },
  1043. .probe = tegra_xusb_padctl_probe,
  1044. .remove = tegra_xusb_padctl_remove,
  1045. };
  1046. module_platform_driver(tegra_xusb_padctl_driver);
  1047. struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
  1048. {
  1049. struct tegra_xusb_padctl *padctl;
  1050. struct platform_device *pdev;
  1051. struct device_node *np;
  1052. np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
  1053. if (!np)
  1054. return ERR_PTR(-EINVAL);
  1055. /*
  1056. * This is slightly ugly. A better implementation would be to keep a
  1057. * registry of pad controllers, but since there will almost certainly
  1058. * only ever be one per SoC that would be a little overkill.
  1059. */
  1060. pdev = of_find_device_by_node(np);
  1061. if (!pdev) {
  1062. of_node_put(np);
  1063. return ERR_PTR(-ENODEV);
  1064. }
  1065. of_node_put(np);
  1066. padctl = platform_get_drvdata(pdev);
  1067. if (!padctl) {
  1068. put_device(&pdev->dev);
  1069. return ERR_PTR(-EPROBE_DEFER);
  1070. }
  1071. return padctl;
  1072. }
  1073. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
  1074. void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
  1075. {
  1076. if (padctl)
  1077. put_device(padctl->dev);
  1078. }
  1079. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
  1080. int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
  1081. unsigned int port)
  1082. {
  1083. if (padctl->soc->ops->usb3_save_context)
  1084. return padctl->soc->ops->usb3_save_context(padctl, port);
  1085. return -ENOSYS;
  1086. }
  1087. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
  1088. int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
  1089. unsigned int port, bool idle)
  1090. {
  1091. if (padctl->soc->ops->hsic_set_idle)
  1092. return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
  1093. return -ENOSYS;
  1094. }
  1095. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
  1096. int tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy,
  1097. enum usb_device_speed speed)
  1098. {
  1099. struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
  1100. if (lane->pad->ops->enable_phy_sleepwalk)
  1101. return lane->pad->ops->enable_phy_sleepwalk(lane, speed);
  1102. return -EOPNOTSUPP;
  1103. }
  1104. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_sleepwalk);
  1105. int tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy)
  1106. {
  1107. struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
  1108. if (lane->pad->ops->disable_phy_sleepwalk)
  1109. return lane->pad->ops->disable_phy_sleepwalk(lane);
  1110. return -EOPNOTSUPP;
  1111. }
  1112. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_sleepwalk);
  1113. int tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
  1114. {
  1115. struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
  1116. if (lane->pad->ops->enable_phy_wake)
  1117. return lane->pad->ops->enable_phy_wake(lane);
  1118. return -EOPNOTSUPP;
  1119. }
  1120. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_wake);
  1121. int tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
  1122. {
  1123. struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
  1124. if (lane->pad->ops->disable_phy_wake)
  1125. return lane->pad->ops->disable_phy_wake(lane);
  1126. return -EOPNOTSUPP;
  1127. }
  1128. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_wake);
  1129. bool tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl *padctl, struct phy *phy)
  1130. {
  1131. struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
  1132. if (lane->pad->ops->remote_wake_detected)
  1133. return lane->pad->ops->remote_wake_detected(lane);
  1134. return false;
  1135. }
  1136. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_remote_wake_detected);
  1137. int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
  1138. unsigned int port, bool enable)
  1139. {
  1140. if (padctl->soc->ops->usb3_set_lfps_detect)
  1141. return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
  1142. enable);
  1143. return -ENOSYS;
  1144. }
  1145. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
  1146. int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
  1147. bool val)
  1148. {
  1149. if (padctl->soc->ops->vbus_override)
  1150. return padctl->soc->ops->vbus_override(padctl, val);
  1151. return -ENOTSUPP;
  1152. }
  1153. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
  1154. int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
  1155. {
  1156. struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
  1157. struct tegra_xusb_padctl *padctl = lane->pad->padctl;
  1158. if (padctl->soc->ops->utmi_port_reset)
  1159. return padctl->soc->ops->utmi_port_reset(phy);
  1160. return -ENOTSUPP;
  1161. }
  1162. EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
  1163. void tegra_phy_xusb_utmi_pad_power_on(struct phy *phy)
  1164. {
  1165. struct tegra_xusb_lane *lane;
  1166. struct tegra_xusb_padctl *padctl;
  1167. if (!phy)
  1168. return;
  1169. lane = phy_get_drvdata(phy);
  1170. padctl = lane->pad->padctl;
  1171. if (padctl->soc->ops->utmi_pad_power_on)
  1172. padctl->soc->ops->utmi_pad_power_on(phy);
  1173. }
  1174. EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_on);
  1175. void tegra_phy_xusb_utmi_pad_power_down(struct phy *phy)
  1176. {
  1177. struct tegra_xusb_lane *lane;
  1178. struct tegra_xusb_padctl *padctl;
  1179. if (!phy)
  1180. return;
  1181. lane = phy_get_drvdata(phy);
  1182. padctl = lane->pad->padctl;
  1183. if (padctl->soc->ops->utmi_pad_power_down)
  1184. padctl->soc->ops->utmi_pad_power_down(phy);
  1185. }
  1186. EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_down);
  1187. int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
  1188. unsigned int port)
  1189. {
  1190. struct tegra_xusb_usb2_port *usb2;
  1191. struct tegra_xusb_usb3_port *usb3;
  1192. int i;
  1193. usb2 = tegra_xusb_find_usb2_port(padctl, port);
  1194. if (!usb2)
  1195. return -EINVAL;
  1196. for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
  1197. usb3 = tegra_xusb_find_usb3_port(padctl, i);
  1198. if (usb3 && usb3->port == usb2->base.index)
  1199. return usb3->base.index;
  1200. }
  1201. return -ENODEV;
  1202. }
  1203. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
  1204. MODULE_AUTHOR("Thierry Reding <[email protected]>");
  1205. MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
  1206. MODULE_LICENSE("GPL v2");