xusb.c 35 KB

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