xusb.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/mailbox_client.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/reset.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <soc/tegra/fuse.h>
  26. #include "xusb.h"
  27. static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
  28. struct of_phandle_args *args)
  29. {
  30. struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
  31. struct phy *phy = NULL;
  32. unsigned int i;
  33. if (args->args_count != 0)
  34. return ERR_PTR(-EINVAL);
  35. for (i = 0; i < pad->soc->num_lanes; i++) {
  36. if (!pad->lanes[i])
  37. continue;
  38. if (pad->lanes[i]->dev.of_node == args->np) {
  39. phy = pad->lanes[i];
  40. break;
  41. }
  42. }
  43. if (phy == NULL)
  44. phy = ERR_PTR(-ENODEV);
  45. return phy;
  46. }
  47. static const struct of_device_id tegra_xusb_padctl_of_match[] = {
  48. #if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
  49. {
  50. .compatible = "nvidia,tegra124-xusb-padctl",
  51. .data = &tegra124_xusb_padctl_soc,
  52. },
  53. #endif
  54. #if defined(CONFIG_ARCH_TEGRA_210_SOC)
  55. {
  56. .compatible = "nvidia,tegra210-xusb-padctl",
  57. .data = &tegra210_xusb_padctl_soc,
  58. },
  59. #endif
  60. { }
  61. };
  62. MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
  63. static struct device_node *
  64. tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
  65. {
  66. /*
  67. * of_find_node_by_name() drops a reference, so make sure to grab one.
  68. */
  69. struct device_node *np = of_node_get(padctl->dev->of_node);
  70. np = of_find_node_by_name(np, "pads");
  71. if (np)
  72. np = of_find_node_by_name(np, name);
  73. return np;
  74. }
  75. static struct device_node *
  76. tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
  77. {
  78. /*
  79. * of_find_node_by_name() drops a reference, so make sure to grab one.
  80. */
  81. struct device_node *np = of_node_get(pad->dev.of_node);
  82. np = of_find_node_by_name(np, "lanes");
  83. if (!np)
  84. return NULL;
  85. return of_find_node_by_name(np, pad->soc->lanes[index].name);
  86. }
  87. int tegra_xusb_lane_lookup_function(struct tegra_xusb_lane *lane,
  88. const char *function)
  89. {
  90. unsigned int i;
  91. for (i = 0; i < lane->soc->num_funcs; i++)
  92. if (strcmp(function, lane->soc->funcs[i]) == 0)
  93. return i;
  94. return -EINVAL;
  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 = tegra_xusb_lane_lookup_function(lane, function);
  106. if (err < 0) {
  107. dev_err(dev, "invalid function \"%s\" for lane \"%s\"\n",
  108. function, np->name);
  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 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_find_node_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. /* choose function */
  262. value = padctl_readl(padctl, soc->offset);
  263. value &= ~(soc->mask << soc->shift);
  264. value |= lane->function << soc->shift;
  265. padctl_writel(padctl, value, soc->offset);
  266. }
  267. static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
  268. {
  269. unsigned int i;
  270. for (i = 0; i < pad->soc->num_lanes; i++) {
  271. struct tegra_xusb_lane *lane;
  272. if (pad->lanes[i]) {
  273. lane = phy_get_drvdata(pad->lanes[i]);
  274. tegra_xusb_lane_program(lane);
  275. }
  276. }
  277. }
  278. static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
  279. {
  280. struct tegra_xusb_pad *pad;
  281. unsigned int i;
  282. mutex_lock(&padctl->lock);
  283. for (i = 0; i < padctl->soc->num_pads; i++) {
  284. const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
  285. int err;
  286. pad = tegra_xusb_pad_create(padctl, soc);
  287. if (IS_ERR(pad)) {
  288. err = PTR_ERR(pad);
  289. dev_err(padctl->dev, "failed to create pad %s: %d\n",
  290. soc->name, err);
  291. __tegra_xusb_remove_pads(padctl);
  292. mutex_unlock(&padctl->lock);
  293. return err;
  294. }
  295. if (!pad)
  296. continue;
  297. list_add_tail(&pad->list, &padctl->pads);
  298. }
  299. list_for_each_entry(pad, &padctl->pads, list)
  300. tegra_xusb_pad_program(pad);
  301. mutex_unlock(&padctl->lock);
  302. return 0;
  303. }
  304. static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
  305. const char *function)
  306. {
  307. const char *func = lane->soc->funcs[lane->function];
  308. return strcmp(function, func) == 0;
  309. }
  310. struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
  311. const char *type,
  312. unsigned int index)
  313. {
  314. struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
  315. char *name;
  316. name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
  317. if (!name)
  318. return ERR_PTR(-ENOMEM);
  319. list_for_each_entry(lane, &padctl->lanes, list) {
  320. if (strcmp(lane->soc->name, name) == 0) {
  321. hit = lane;
  322. break;
  323. }
  324. }
  325. kfree(name);
  326. return hit;
  327. }
  328. struct tegra_xusb_lane *
  329. tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
  330. const struct tegra_xusb_lane_map *map,
  331. const char *function)
  332. {
  333. struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
  334. for (map = map; map->type; map++) {
  335. if (port->index != map->port)
  336. continue;
  337. lane = tegra_xusb_find_lane(port->padctl, map->type,
  338. map->index);
  339. if (IS_ERR(lane))
  340. continue;
  341. if (!tegra_xusb_lane_check(lane, function))
  342. continue;
  343. if (!IS_ERR(match))
  344. dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
  345. map->type, map->index, match->soc->name);
  346. else
  347. match = lane;
  348. }
  349. return match;
  350. }
  351. static struct device_node *
  352. tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
  353. unsigned int index)
  354. {
  355. /*
  356. * of_find_node_by_name() drops a reference, so make sure to grab one.
  357. */
  358. struct device_node *np = of_node_get(padctl->dev->of_node);
  359. np = of_find_node_by_name(np, "ports");
  360. if (np) {
  361. char *name;
  362. name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
  363. np = of_find_node_by_name(np, name);
  364. kfree(name);
  365. }
  366. return np;
  367. }
  368. struct tegra_xusb_port *
  369. tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
  370. unsigned int index)
  371. {
  372. struct tegra_xusb_port *port;
  373. struct device_node *np;
  374. np = tegra_xusb_find_port_node(padctl, type, index);
  375. if (!np)
  376. return NULL;
  377. list_for_each_entry(port, &padctl->ports, list) {
  378. if (np == port->dev.of_node) {
  379. of_node_put(np);
  380. return port;
  381. }
  382. }
  383. of_node_put(np);
  384. return NULL;
  385. }
  386. struct tegra_xusb_usb2_port *
  387. tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
  388. {
  389. struct tegra_xusb_port *port;
  390. port = tegra_xusb_find_port(padctl, "usb2", index);
  391. if (port)
  392. return to_usb2_port(port);
  393. return NULL;
  394. }
  395. struct tegra_xusb_usb3_port *
  396. tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
  397. {
  398. struct tegra_xusb_port *port;
  399. port = tegra_xusb_find_port(padctl, "usb3", index);
  400. if (port)
  401. return to_usb3_port(port);
  402. return NULL;
  403. }
  404. static void tegra_xusb_port_release(struct device *dev)
  405. {
  406. }
  407. static struct device_type tegra_xusb_port_type = {
  408. .release = tegra_xusb_port_release,
  409. };
  410. static int tegra_xusb_port_init(struct tegra_xusb_port *port,
  411. struct tegra_xusb_padctl *padctl,
  412. struct device_node *np,
  413. const char *name,
  414. unsigned int index)
  415. {
  416. int err;
  417. INIT_LIST_HEAD(&port->list);
  418. port->padctl = padctl;
  419. port->index = index;
  420. device_initialize(&port->dev);
  421. port->dev.type = &tegra_xusb_port_type;
  422. port->dev.of_node = of_node_get(np);
  423. port->dev.parent = padctl->dev;
  424. err = dev_set_name(&port->dev, "%s-%u", name, index);
  425. if (err < 0)
  426. goto unregister;
  427. err = device_add(&port->dev);
  428. if (err < 0)
  429. goto unregister;
  430. return 0;
  431. unregister:
  432. device_unregister(&port->dev);
  433. return err;
  434. }
  435. static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
  436. {
  437. device_unregister(&port->dev);
  438. }
  439. static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
  440. {
  441. struct tegra_xusb_port *port = &usb2->base;
  442. struct device_node *np = port->dev.of_node;
  443. usb2->internal = of_property_read_bool(np, "nvidia,internal");
  444. usb2->supply = devm_regulator_get(&port->dev, "vbus");
  445. if (IS_ERR(usb2->supply))
  446. return PTR_ERR(usb2->supply);
  447. return 0;
  448. }
  449. static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
  450. unsigned int index)
  451. {
  452. struct tegra_xusb_usb2_port *usb2;
  453. struct device_node *np;
  454. int err = 0;
  455. /*
  456. * USB2 ports don't require additional properties, but if the port is
  457. * marked as disabled there is no reason to register it.
  458. */
  459. np = tegra_xusb_find_port_node(padctl, "usb2", index);
  460. if (!np || !of_device_is_available(np))
  461. goto out;
  462. usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
  463. if (!usb2) {
  464. err = -ENOMEM;
  465. goto out;
  466. }
  467. err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
  468. if (err < 0)
  469. goto out;
  470. usb2->base.ops = padctl->soc->ports.usb2.ops;
  471. usb2->base.lane = usb2->base.ops->map(&usb2->base);
  472. if (IS_ERR(usb2->base.lane)) {
  473. err = PTR_ERR(usb2->base.lane);
  474. goto out;
  475. }
  476. err = tegra_xusb_usb2_port_parse_dt(usb2);
  477. if (err < 0) {
  478. tegra_xusb_port_unregister(&usb2->base);
  479. goto out;
  480. }
  481. list_add_tail(&usb2->base.list, &padctl->ports);
  482. out:
  483. of_node_put(np);
  484. return err;
  485. }
  486. static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
  487. {
  488. struct tegra_xusb_port *port = &ulpi->base;
  489. struct device_node *np = port->dev.of_node;
  490. ulpi->internal = of_property_read_bool(np, "nvidia,internal");
  491. return 0;
  492. }
  493. static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
  494. unsigned int index)
  495. {
  496. struct tegra_xusb_ulpi_port *ulpi;
  497. struct device_node *np;
  498. int err = 0;
  499. np = tegra_xusb_find_port_node(padctl, "ulpi", index);
  500. if (!np || !of_device_is_available(np))
  501. goto out;
  502. ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
  503. if (!ulpi) {
  504. err = -ENOMEM;
  505. goto out;
  506. }
  507. err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
  508. if (err < 0)
  509. goto out;
  510. ulpi->base.ops = padctl->soc->ports.ulpi.ops;
  511. ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
  512. if (IS_ERR(ulpi->base.lane)) {
  513. err = PTR_ERR(ulpi->base.lane);
  514. goto out;
  515. }
  516. err = tegra_xusb_ulpi_port_parse_dt(ulpi);
  517. if (err < 0) {
  518. tegra_xusb_port_unregister(&ulpi->base);
  519. goto out;
  520. }
  521. list_add_tail(&ulpi->base.list, &padctl->ports);
  522. out:
  523. of_node_put(np);
  524. return err;
  525. }
  526. static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
  527. {
  528. /* XXX */
  529. return 0;
  530. }
  531. static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
  532. unsigned int index)
  533. {
  534. struct tegra_xusb_hsic_port *hsic;
  535. struct device_node *np;
  536. int err = 0;
  537. np = tegra_xusb_find_port_node(padctl, "hsic", index);
  538. if (!np || !of_device_is_available(np))
  539. goto out;
  540. hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
  541. if (!hsic) {
  542. err = -ENOMEM;
  543. goto out;
  544. }
  545. err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
  546. if (err < 0)
  547. goto out;
  548. hsic->base.ops = padctl->soc->ports.hsic.ops;
  549. hsic->base.lane = hsic->base.ops->map(&hsic->base);
  550. if (IS_ERR(hsic->base.lane)) {
  551. err = PTR_ERR(hsic->base.lane);
  552. goto out;
  553. }
  554. err = tegra_xusb_hsic_port_parse_dt(hsic);
  555. if (err < 0) {
  556. tegra_xusb_port_unregister(&hsic->base);
  557. goto out;
  558. }
  559. list_add_tail(&hsic->base.list, &padctl->ports);
  560. out:
  561. of_node_put(np);
  562. return err;
  563. }
  564. static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
  565. {
  566. struct tegra_xusb_port *port = &usb3->base;
  567. struct device_node *np = port->dev.of_node;
  568. u32 value;
  569. int err;
  570. err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
  571. if (err < 0) {
  572. dev_err(&port->dev, "failed to read port: %d\n", err);
  573. return err;
  574. }
  575. usb3->port = value;
  576. usb3->internal = of_property_read_bool(np, "nvidia,internal");
  577. usb3->supply = devm_regulator_get(&port->dev, "vbus");
  578. if (IS_ERR(usb3->supply))
  579. return PTR_ERR(usb3->supply);
  580. return 0;
  581. }
  582. static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
  583. unsigned int index)
  584. {
  585. struct tegra_xusb_usb3_port *usb3;
  586. struct device_node *np;
  587. int err = 0;
  588. /*
  589. * If there is no supplemental configuration in the device tree the
  590. * port is unusable. But it is valid to configure only a single port,
  591. * hence return 0 instead of an error to allow ports to be optional.
  592. */
  593. np = tegra_xusb_find_port_node(padctl, "usb3", index);
  594. if (!np || !of_device_is_available(np))
  595. goto out;
  596. usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
  597. if (!usb3) {
  598. err = -ENOMEM;
  599. goto out;
  600. }
  601. err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
  602. if (err < 0)
  603. goto out;
  604. usb3->base.ops = padctl->soc->ports.usb3.ops;
  605. usb3->base.lane = usb3->base.ops->map(&usb3->base);
  606. if (IS_ERR(usb3->base.lane)) {
  607. err = PTR_ERR(usb3->base.lane);
  608. goto out;
  609. }
  610. err = tegra_xusb_usb3_port_parse_dt(usb3);
  611. if (err < 0) {
  612. tegra_xusb_port_unregister(&usb3->base);
  613. goto out;
  614. }
  615. list_add_tail(&usb3->base.list, &padctl->ports);
  616. out:
  617. of_node_put(np);
  618. return err;
  619. }
  620. static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
  621. {
  622. struct tegra_xusb_port *port, *tmp;
  623. list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
  624. list_del(&port->list);
  625. tegra_xusb_port_unregister(port);
  626. }
  627. }
  628. static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
  629. {
  630. struct tegra_xusb_port *port;
  631. unsigned int i;
  632. int err = 0;
  633. mutex_lock(&padctl->lock);
  634. for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
  635. err = tegra_xusb_add_usb2_port(padctl, i);
  636. if (err < 0)
  637. goto remove_ports;
  638. }
  639. for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
  640. err = tegra_xusb_add_ulpi_port(padctl, i);
  641. if (err < 0)
  642. goto remove_ports;
  643. }
  644. for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
  645. err = tegra_xusb_add_hsic_port(padctl, i);
  646. if (err < 0)
  647. goto remove_ports;
  648. }
  649. for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
  650. err = tegra_xusb_add_usb3_port(padctl, i);
  651. if (err < 0)
  652. goto remove_ports;
  653. }
  654. list_for_each_entry(port, &padctl->ports, list) {
  655. err = port->ops->enable(port);
  656. if (err < 0)
  657. dev_err(padctl->dev, "failed to enable port %s: %d\n",
  658. dev_name(&port->dev), err);
  659. }
  660. goto unlock;
  661. remove_ports:
  662. __tegra_xusb_remove_ports(padctl);
  663. unlock:
  664. mutex_unlock(&padctl->lock);
  665. return err;
  666. }
  667. static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
  668. {
  669. mutex_lock(&padctl->lock);
  670. __tegra_xusb_remove_ports(padctl);
  671. mutex_unlock(&padctl->lock);
  672. }
  673. static int tegra_xusb_padctl_probe(struct platform_device *pdev)
  674. {
  675. struct device_node *np = of_node_get(pdev->dev.of_node);
  676. const struct tegra_xusb_padctl_soc *soc;
  677. struct tegra_xusb_padctl *padctl;
  678. const struct of_device_id *match;
  679. struct resource *res;
  680. int err;
  681. /* for backwards compatibility with old device trees */
  682. np = of_find_node_by_name(np, "pads");
  683. if (!np) {
  684. dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
  685. return tegra_xusb_padctl_legacy_probe(pdev);
  686. }
  687. of_node_put(np);
  688. match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
  689. soc = match->data;
  690. padctl = soc->ops->probe(&pdev->dev, soc);
  691. if (IS_ERR(padctl))
  692. return PTR_ERR(padctl);
  693. platform_set_drvdata(pdev, padctl);
  694. INIT_LIST_HEAD(&padctl->ports);
  695. INIT_LIST_HEAD(&padctl->lanes);
  696. INIT_LIST_HEAD(&padctl->pads);
  697. mutex_init(&padctl->lock);
  698. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  699. padctl->regs = devm_ioremap_resource(&pdev->dev, res);
  700. if (IS_ERR(padctl->regs)) {
  701. err = PTR_ERR(padctl->regs);
  702. goto remove;
  703. }
  704. padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
  705. if (IS_ERR(padctl->rst)) {
  706. err = PTR_ERR(padctl->rst);
  707. goto remove;
  708. }
  709. err = reset_control_deassert(padctl->rst);
  710. if (err < 0)
  711. goto remove;
  712. err = tegra_xusb_setup_pads(padctl);
  713. if (err < 0) {
  714. dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
  715. goto reset;
  716. }
  717. err = tegra_xusb_setup_ports(padctl);
  718. if (err) {
  719. dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
  720. goto remove_pads;
  721. }
  722. return 0;
  723. remove_pads:
  724. tegra_xusb_remove_pads(padctl);
  725. reset:
  726. reset_control_assert(padctl->rst);
  727. remove:
  728. soc->ops->remove(padctl);
  729. return err;
  730. }
  731. static int tegra_xusb_padctl_remove(struct platform_device *pdev)
  732. {
  733. struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
  734. int err;
  735. tegra_xusb_remove_ports(padctl);
  736. tegra_xusb_remove_pads(padctl);
  737. err = reset_control_assert(padctl->rst);
  738. if (err < 0)
  739. dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
  740. padctl->soc->ops->remove(padctl);
  741. return err;
  742. }
  743. static struct platform_driver tegra_xusb_padctl_driver = {
  744. .driver = {
  745. .name = "tegra-xusb-padctl",
  746. .of_match_table = tegra_xusb_padctl_of_match,
  747. },
  748. .probe = tegra_xusb_padctl_probe,
  749. .remove = tegra_xusb_padctl_remove,
  750. };
  751. module_platform_driver(tegra_xusb_padctl_driver);
  752. struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
  753. {
  754. struct tegra_xusb_padctl *padctl;
  755. struct platform_device *pdev;
  756. struct device_node *np;
  757. np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
  758. if (!np)
  759. return ERR_PTR(-EINVAL);
  760. /*
  761. * This is slightly ugly. A better implementation would be to keep a
  762. * registry of pad controllers, but since there will almost certainly
  763. * only ever be one per SoC that would be a little overkill.
  764. */
  765. pdev = of_find_device_by_node(np);
  766. if (!pdev) {
  767. of_node_put(np);
  768. return ERR_PTR(-ENODEV);
  769. }
  770. of_node_put(np);
  771. padctl = platform_get_drvdata(pdev);
  772. if (!padctl) {
  773. put_device(&pdev->dev);
  774. return ERR_PTR(-EPROBE_DEFER);
  775. }
  776. return padctl;
  777. }
  778. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
  779. void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
  780. {
  781. if (padctl)
  782. put_device(padctl->dev);
  783. }
  784. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
  785. int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
  786. unsigned int port)
  787. {
  788. if (padctl->soc->ops->usb3_save_context)
  789. return padctl->soc->ops->usb3_save_context(padctl, port);
  790. return -ENOSYS;
  791. }
  792. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
  793. int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
  794. unsigned int port, bool idle)
  795. {
  796. if (padctl->soc->ops->hsic_set_idle)
  797. return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
  798. return -ENOSYS;
  799. }
  800. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
  801. int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
  802. unsigned int port, bool enable)
  803. {
  804. if (padctl->soc->ops->usb3_set_lfps_detect)
  805. return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
  806. enable);
  807. return -ENOSYS;
  808. }
  809. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
  810. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  811. MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
  812. MODULE_LICENSE("GPL v2");