xusb.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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/phy/tegra/xusb.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/reset.h>
  24. #include <linux/slab.h>
  25. #include <linux/workqueue.h>
  26. #include <soc/tegra/fuse.h>
  27. #include "xusb.h"
  28. static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
  29. struct of_phandle_args *args)
  30. {
  31. struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
  32. struct phy *phy = NULL;
  33. unsigned int i;
  34. if (args->args_count != 0)
  35. return ERR_PTR(-EINVAL);
  36. for (i = 0; i < pad->soc->num_lanes; i++) {
  37. if (!pad->lanes[i])
  38. continue;
  39. if (pad->lanes[i]->dev.of_node == args->np) {
  40. phy = pad->lanes[i];
  41. break;
  42. }
  43. }
  44. if (phy == NULL)
  45. phy = ERR_PTR(-ENODEV);
  46. return phy;
  47. }
  48. static const struct of_device_id tegra_xusb_padctl_of_match[] = {
  49. #if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
  50. {
  51. .compatible = "nvidia,tegra124-xusb-padctl",
  52. .data = &tegra124_xusb_padctl_soc,
  53. },
  54. #endif
  55. #if defined(CONFIG_ARCH_TEGRA_210_SOC)
  56. {
  57. .compatible = "nvidia,tegra210-xusb-padctl",
  58. .data = &tegra210_xusb_padctl_soc,
  59. },
  60. #endif
  61. { }
  62. };
  63. MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
  64. static struct device_node *
  65. tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
  66. {
  67. struct device_node *pads, *np;
  68. pads = of_get_child_by_name(padctl->dev->of_node, "pads");
  69. if (!pads)
  70. return NULL;
  71. np = of_get_child_by_name(pads, name);
  72. of_node_put(pads);
  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. struct device_node *np, *lanes;
  79. lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
  80. if (!lanes)
  81. return NULL;
  82. np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
  83. of_node_put(lanes);
  84. return np;
  85. }
  86. int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
  87. struct device_node *np)
  88. {
  89. struct device *dev = &lane->pad->dev;
  90. const char *function;
  91. int err;
  92. err = of_property_read_string(np, "nvidia,function", &function);
  93. if (err < 0)
  94. return err;
  95. err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
  96. if (err < 0) {
  97. dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
  98. function, np);
  99. return err;
  100. }
  101. lane->function = err;
  102. return 0;
  103. }
  104. static void tegra_xusb_lane_destroy(struct phy *phy)
  105. {
  106. if (phy) {
  107. struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
  108. lane->pad->ops->remove(lane);
  109. phy_destroy(phy);
  110. }
  111. }
  112. static void tegra_xusb_pad_release(struct device *dev)
  113. {
  114. struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
  115. pad->soc->ops->remove(pad);
  116. }
  117. static struct device_type tegra_xusb_pad_type = {
  118. .release = tegra_xusb_pad_release,
  119. };
  120. int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
  121. struct tegra_xusb_padctl *padctl,
  122. struct device_node *np)
  123. {
  124. int err;
  125. device_initialize(&pad->dev);
  126. INIT_LIST_HEAD(&pad->list);
  127. pad->dev.parent = padctl->dev;
  128. pad->dev.type = &tegra_xusb_pad_type;
  129. pad->dev.of_node = np;
  130. pad->padctl = padctl;
  131. err = dev_set_name(&pad->dev, "%s", pad->soc->name);
  132. if (err < 0)
  133. goto unregister;
  134. err = device_add(&pad->dev);
  135. if (err < 0)
  136. goto unregister;
  137. return 0;
  138. unregister:
  139. device_unregister(&pad->dev);
  140. return err;
  141. }
  142. int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
  143. const struct phy_ops *ops)
  144. {
  145. struct device_node *children;
  146. struct phy *lane;
  147. unsigned int i;
  148. int err;
  149. children = of_get_child_by_name(pad->dev.of_node, "lanes");
  150. if (!children)
  151. return -ENODEV;
  152. pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
  153. GFP_KERNEL);
  154. if (!pad->lanes) {
  155. of_node_put(children);
  156. return -ENOMEM;
  157. }
  158. for (i = 0; i < pad->soc->num_lanes; i++) {
  159. struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
  160. struct tegra_xusb_lane *lane;
  161. /* skip disabled lanes */
  162. if (!np || !of_device_is_available(np)) {
  163. of_node_put(np);
  164. continue;
  165. }
  166. pad->lanes[i] = phy_create(&pad->dev, np, ops);
  167. if (IS_ERR(pad->lanes[i])) {
  168. err = PTR_ERR(pad->lanes[i]);
  169. of_node_put(np);
  170. goto remove;
  171. }
  172. lane = pad->ops->probe(pad, np, i);
  173. if (IS_ERR(lane)) {
  174. phy_destroy(pad->lanes[i]);
  175. err = PTR_ERR(lane);
  176. goto remove;
  177. }
  178. list_add_tail(&lane->list, &pad->padctl->lanes);
  179. phy_set_drvdata(pad->lanes[i], lane);
  180. }
  181. pad->provider = of_phy_provider_register_full(&pad->dev, children,
  182. tegra_xusb_pad_of_xlate);
  183. if (IS_ERR(pad->provider)) {
  184. err = PTR_ERR(pad->provider);
  185. goto remove;
  186. }
  187. return 0;
  188. remove:
  189. while (i--)
  190. tegra_xusb_lane_destroy(pad->lanes[i]);
  191. of_node_put(children);
  192. return err;
  193. }
  194. void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
  195. {
  196. unsigned int i = pad->soc->num_lanes;
  197. of_phy_provider_unregister(pad->provider);
  198. while (i--)
  199. tegra_xusb_lane_destroy(pad->lanes[i]);
  200. device_unregister(&pad->dev);
  201. }
  202. static struct tegra_xusb_pad *
  203. tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
  204. const struct tegra_xusb_pad_soc *soc)
  205. {
  206. struct tegra_xusb_pad *pad;
  207. struct device_node *np;
  208. int err;
  209. np = tegra_xusb_find_pad_node(padctl, soc->name);
  210. if (!np || !of_device_is_available(np))
  211. return NULL;
  212. pad = soc->ops->probe(padctl, soc, np);
  213. if (IS_ERR(pad)) {
  214. err = PTR_ERR(pad);
  215. dev_err(padctl->dev, "failed to create pad %s: %d\n",
  216. soc->name, err);
  217. return ERR_PTR(err);
  218. }
  219. /* XXX move this into ->probe() to avoid string comparison */
  220. if (strcmp(soc->name, "pcie") == 0)
  221. padctl->pcie = pad;
  222. if (strcmp(soc->name, "sata") == 0)
  223. padctl->sata = pad;
  224. if (strcmp(soc->name, "usb2") == 0)
  225. padctl->usb2 = pad;
  226. if (strcmp(soc->name, "ulpi") == 0)
  227. padctl->ulpi = pad;
  228. if (strcmp(soc->name, "hsic") == 0)
  229. padctl->hsic = pad;
  230. return pad;
  231. }
  232. static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
  233. {
  234. struct tegra_xusb_pad *pad, *tmp;
  235. list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
  236. list_del(&pad->list);
  237. tegra_xusb_pad_unregister(pad);
  238. }
  239. }
  240. static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
  241. {
  242. mutex_lock(&padctl->lock);
  243. __tegra_xusb_remove_pads(padctl);
  244. mutex_unlock(&padctl->lock);
  245. }
  246. static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
  247. {
  248. struct tegra_xusb_padctl *padctl = lane->pad->padctl;
  249. const struct tegra_xusb_lane_soc *soc = lane->soc;
  250. u32 value;
  251. /* choose function */
  252. value = padctl_readl(padctl, soc->offset);
  253. value &= ~(soc->mask << soc->shift);
  254. value |= lane->function << soc->shift;
  255. padctl_writel(padctl, value, soc->offset);
  256. }
  257. static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
  258. {
  259. unsigned int i;
  260. for (i = 0; i < pad->soc->num_lanes; i++) {
  261. struct tegra_xusb_lane *lane;
  262. if (pad->lanes[i]) {
  263. lane = phy_get_drvdata(pad->lanes[i]);
  264. tegra_xusb_lane_program(lane);
  265. }
  266. }
  267. }
  268. static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
  269. {
  270. struct tegra_xusb_pad *pad;
  271. unsigned int i;
  272. mutex_lock(&padctl->lock);
  273. for (i = 0; i < padctl->soc->num_pads; i++) {
  274. const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
  275. int err;
  276. pad = tegra_xusb_pad_create(padctl, soc);
  277. if (IS_ERR(pad)) {
  278. err = PTR_ERR(pad);
  279. dev_err(padctl->dev, "failed to create pad %s: %d\n",
  280. soc->name, err);
  281. __tegra_xusb_remove_pads(padctl);
  282. mutex_unlock(&padctl->lock);
  283. return err;
  284. }
  285. if (!pad)
  286. continue;
  287. list_add_tail(&pad->list, &padctl->pads);
  288. }
  289. list_for_each_entry(pad, &padctl->pads, list)
  290. tegra_xusb_pad_program(pad);
  291. mutex_unlock(&padctl->lock);
  292. return 0;
  293. }
  294. static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
  295. const char *function)
  296. {
  297. const char *func = lane->soc->funcs[lane->function];
  298. return strcmp(function, func) == 0;
  299. }
  300. struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
  301. const char *type,
  302. unsigned int index)
  303. {
  304. struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
  305. char *name;
  306. name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
  307. if (!name)
  308. return ERR_PTR(-ENOMEM);
  309. list_for_each_entry(lane, &padctl->lanes, list) {
  310. if (strcmp(lane->soc->name, name) == 0) {
  311. hit = lane;
  312. break;
  313. }
  314. }
  315. kfree(name);
  316. return hit;
  317. }
  318. struct tegra_xusb_lane *
  319. tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
  320. const struct tegra_xusb_lane_map *map,
  321. const char *function)
  322. {
  323. struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
  324. for (; map->type; map++) {
  325. if (port->index != map->port)
  326. continue;
  327. lane = tegra_xusb_find_lane(port->padctl, map->type,
  328. map->index);
  329. if (IS_ERR(lane))
  330. continue;
  331. if (!tegra_xusb_lane_check(lane, function))
  332. continue;
  333. if (!IS_ERR(match))
  334. dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
  335. map->type, map->index, match->soc->name);
  336. else
  337. match = lane;
  338. }
  339. return match;
  340. }
  341. static struct device_node *
  342. tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
  343. unsigned int index)
  344. {
  345. struct device_node *ports, *np;
  346. char *name;
  347. ports = of_get_child_by_name(padctl->dev->of_node, "ports");
  348. if (!ports)
  349. return NULL;
  350. name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
  351. if (!name) {
  352. of_node_put(ports);
  353. return ERR_PTR(-ENOMEM);
  354. }
  355. np = of_get_child_by_name(ports, name);
  356. kfree(name);
  357. of_node_put(ports);
  358. return np;
  359. }
  360. struct tegra_xusb_port *
  361. tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
  362. unsigned int index)
  363. {
  364. struct tegra_xusb_port *port;
  365. struct device_node *np;
  366. np = tegra_xusb_find_port_node(padctl, type, index);
  367. if (!np)
  368. return NULL;
  369. list_for_each_entry(port, &padctl->ports, list) {
  370. if (np == port->dev.of_node) {
  371. of_node_put(np);
  372. return port;
  373. }
  374. }
  375. of_node_put(np);
  376. return NULL;
  377. }
  378. struct tegra_xusb_usb2_port *
  379. tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
  380. {
  381. struct tegra_xusb_port *port;
  382. port = tegra_xusb_find_port(padctl, "usb2", index);
  383. if (port)
  384. return to_usb2_port(port);
  385. return NULL;
  386. }
  387. struct tegra_xusb_usb3_port *
  388. tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
  389. {
  390. struct tegra_xusb_port *port;
  391. port = tegra_xusb_find_port(padctl, "usb3", index);
  392. if (port)
  393. return to_usb3_port(port);
  394. return NULL;
  395. }
  396. static void tegra_xusb_port_release(struct device *dev)
  397. {
  398. }
  399. static struct device_type tegra_xusb_port_type = {
  400. .release = tegra_xusb_port_release,
  401. };
  402. static int tegra_xusb_port_init(struct tegra_xusb_port *port,
  403. struct tegra_xusb_padctl *padctl,
  404. struct device_node *np,
  405. const char *name,
  406. unsigned int index)
  407. {
  408. int err;
  409. INIT_LIST_HEAD(&port->list);
  410. port->padctl = padctl;
  411. port->index = index;
  412. device_initialize(&port->dev);
  413. port->dev.type = &tegra_xusb_port_type;
  414. port->dev.of_node = of_node_get(np);
  415. port->dev.parent = padctl->dev;
  416. err = dev_set_name(&port->dev, "%s-%u", name, index);
  417. if (err < 0)
  418. goto unregister;
  419. err = device_add(&port->dev);
  420. if (err < 0)
  421. goto unregister;
  422. return 0;
  423. unregister:
  424. device_unregister(&port->dev);
  425. return err;
  426. }
  427. static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
  428. {
  429. device_unregister(&port->dev);
  430. }
  431. static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
  432. {
  433. struct tegra_xusb_port *port = &usb2->base;
  434. struct device_node *np = port->dev.of_node;
  435. usb2->internal = of_property_read_bool(np, "nvidia,internal");
  436. usb2->supply = devm_regulator_get(&port->dev, "vbus");
  437. return PTR_ERR_OR_ZERO(usb2->supply);
  438. }
  439. static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
  440. unsigned int index)
  441. {
  442. struct tegra_xusb_usb2_port *usb2;
  443. struct device_node *np;
  444. int err = 0;
  445. /*
  446. * USB2 ports don't require additional properties, but if the port is
  447. * marked as disabled there is no reason to register it.
  448. */
  449. np = tegra_xusb_find_port_node(padctl, "usb2", index);
  450. if (!np || !of_device_is_available(np))
  451. goto out;
  452. usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
  453. if (!usb2) {
  454. err = -ENOMEM;
  455. goto out;
  456. }
  457. err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
  458. if (err < 0)
  459. goto out;
  460. usb2->base.ops = padctl->soc->ports.usb2.ops;
  461. usb2->base.lane = usb2->base.ops->map(&usb2->base);
  462. if (IS_ERR(usb2->base.lane)) {
  463. err = PTR_ERR(usb2->base.lane);
  464. goto out;
  465. }
  466. err = tegra_xusb_usb2_port_parse_dt(usb2);
  467. if (err < 0) {
  468. tegra_xusb_port_unregister(&usb2->base);
  469. goto out;
  470. }
  471. list_add_tail(&usb2->base.list, &padctl->ports);
  472. out:
  473. of_node_put(np);
  474. return err;
  475. }
  476. static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
  477. {
  478. struct tegra_xusb_port *port = &ulpi->base;
  479. struct device_node *np = port->dev.of_node;
  480. ulpi->internal = of_property_read_bool(np, "nvidia,internal");
  481. return 0;
  482. }
  483. static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
  484. unsigned int index)
  485. {
  486. struct tegra_xusb_ulpi_port *ulpi;
  487. struct device_node *np;
  488. int err = 0;
  489. np = tegra_xusb_find_port_node(padctl, "ulpi", index);
  490. if (!np || !of_device_is_available(np))
  491. goto out;
  492. ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
  493. if (!ulpi) {
  494. err = -ENOMEM;
  495. goto out;
  496. }
  497. err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
  498. if (err < 0)
  499. goto out;
  500. ulpi->base.ops = padctl->soc->ports.ulpi.ops;
  501. ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
  502. if (IS_ERR(ulpi->base.lane)) {
  503. err = PTR_ERR(ulpi->base.lane);
  504. goto out;
  505. }
  506. err = tegra_xusb_ulpi_port_parse_dt(ulpi);
  507. if (err < 0) {
  508. tegra_xusb_port_unregister(&ulpi->base);
  509. goto out;
  510. }
  511. list_add_tail(&ulpi->base.list, &padctl->ports);
  512. out:
  513. of_node_put(np);
  514. return err;
  515. }
  516. static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
  517. {
  518. /* XXX */
  519. return 0;
  520. }
  521. static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
  522. unsigned int index)
  523. {
  524. struct tegra_xusb_hsic_port *hsic;
  525. struct device_node *np;
  526. int err = 0;
  527. np = tegra_xusb_find_port_node(padctl, "hsic", index);
  528. if (!np || !of_device_is_available(np))
  529. goto out;
  530. hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
  531. if (!hsic) {
  532. err = -ENOMEM;
  533. goto out;
  534. }
  535. err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
  536. if (err < 0)
  537. goto out;
  538. hsic->base.ops = padctl->soc->ports.hsic.ops;
  539. hsic->base.lane = hsic->base.ops->map(&hsic->base);
  540. if (IS_ERR(hsic->base.lane)) {
  541. err = PTR_ERR(hsic->base.lane);
  542. goto out;
  543. }
  544. err = tegra_xusb_hsic_port_parse_dt(hsic);
  545. if (err < 0) {
  546. tegra_xusb_port_unregister(&hsic->base);
  547. goto out;
  548. }
  549. list_add_tail(&hsic->base.list, &padctl->ports);
  550. out:
  551. of_node_put(np);
  552. return err;
  553. }
  554. static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
  555. {
  556. struct tegra_xusb_port *port = &usb3->base;
  557. struct device_node *np = port->dev.of_node;
  558. u32 value;
  559. int err;
  560. err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
  561. if (err < 0) {
  562. dev_err(&port->dev, "failed to read port: %d\n", err);
  563. return err;
  564. }
  565. usb3->port = value;
  566. usb3->internal = of_property_read_bool(np, "nvidia,internal");
  567. usb3->supply = devm_regulator_get(&port->dev, "vbus");
  568. return PTR_ERR_OR_ZERO(usb3->supply);
  569. }
  570. static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
  571. unsigned int index)
  572. {
  573. struct tegra_xusb_usb3_port *usb3;
  574. struct device_node *np;
  575. int err = 0;
  576. /*
  577. * If there is no supplemental configuration in the device tree the
  578. * port is unusable. But it is valid to configure only a single port,
  579. * hence return 0 instead of an error to allow ports to be optional.
  580. */
  581. np = tegra_xusb_find_port_node(padctl, "usb3", index);
  582. if (!np || !of_device_is_available(np))
  583. goto out;
  584. usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
  585. if (!usb3) {
  586. err = -ENOMEM;
  587. goto out;
  588. }
  589. err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
  590. if (err < 0)
  591. goto out;
  592. usb3->base.ops = padctl->soc->ports.usb3.ops;
  593. usb3->base.lane = usb3->base.ops->map(&usb3->base);
  594. if (IS_ERR(usb3->base.lane)) {
  595. err = PTR_ERR(usb3->base.lane);
  596. goto out;
  597. }
  598. err = tegra_xusb_usb3_port_parse_dt(usb3);
  599. if (err < 0) {
  600. tegra_xusb_port_unregister(&usb3->base);
  601. goto out;
  602. }
  603. list_add_tail(&usb3->base.list, &padctl->ports);
  604. out:
  605. of_node_put(np);
  606. return err;
  607. }
  608. static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
  609. {
  610. struct tegra_xusb_port *port, *tmp;
  611. list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
  612. list_del(&port->list);
  613. tegra_xusb_port_unregister(port);
  614. }
  615. }
  616. static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
  617. {
  618. struct tegra_xusb_port *port;
  619. unsigned int i;
  620. int err = 0;
  621. mutex_lock(&padctl->lock);
  622. for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
  623. err = tegra_xusb_add_usb2_port(padctl, i);
  624. if (err < 0)
  625. goto remove_ports;
  626. }
  627. for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
  628. err = tegra_xusb_add_ulpi_port(padctl, i);
  629. if (err < 0)
  630. goto remove_ports;
  631. }
  632. for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
  633. err = tegra_xusb_add_hsic_port(padctl, i);
  634. if (err < 0)
  635. goto remove_ports;
  636. }
  637. for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
  638. err = tegra_xusb_add_usb3_port(padctl, i);
  639. if (err < 0)
  640. goto remove_ports;
  641. }
  642. list_for_each_entry(port, &padctl->ports, list) {
  643. err = port->ops->enable(port);
  644. if (err < 0)
  645. dev_err(padctl->dev, "failed to enable port %s: %d\n",
  646. dev_name(&port->dev), err);
  647. }
  648. goto unlock;
  649. remove_ports:
  650. __tegra_xusb_remove_ports(padctl);
  651. unlock:
  652. mutex_unlock(&padctl->lock);
  653. return err;
  654. }
  655. static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
  656. {
  657. mutex_lock(&padctl->lock);
  658. __tegra_xusb_remove_ports(padctl);
  659. mutex_unlock(&padctl->lock);
  660. }
  661. static int tegra_xusb_padctl_probe(struct platform_device *pdev)
  662. {
  663. struct device_node *np = pdev->dev.of_node;
  664. const struct tegra_xusb_padctl_soc *soc;
  665. struct tegra_xusb_padctl *padctl;
  666. const struct of_device_id *match;
  667. struct resource *res;
  668. int err;
  669. /* for backwards compatibility with old device trees */
  670. np = of_get_child_by_name(np, "pads");
  671. if (!np) {
  672. dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
  673. return tegra_xusb_padctl_legacy_probe(pdev);
  674. }
  675. of_node_put(np);
  676. match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
  677. soc = match->data;
  678. padctl = soc->ops->probe(&pdev->dev, soc);
  679. if (IS_ERR(padctl))
  680. return PTR_ERR(padctl);
  681. platform_set_drvdata(pdev, padctl);
  682. INIT_LIST_HEAD(&padctl->ports);
  683. INIT_LIST_HEAD(&padctl->lanes);
  684. INIT_LIST_HEAD(&padctl->pads);
  685. mutex_init(&padctl->lock);
  686. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  687. padctl->regs = devm_ioremap_resource(&pdev->dev, res);
  688. if (IS_ERR(padctl->regs)) {
  689. err = PTR_ERR(padctl->regs);
  690. goto remove;
  691. }
  692. padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
  693. if (IS_ERR(padctl->rst)) {
  694. err = PTR_ERR(padctl->rst);
  695. goto remove;
  696. }
  697. err = reset_control_deassert(padctl->rst);
  698. if (err < 0)
  699. goto remove;
  700. err = tegra_xusb_setup_pads(padctl);
  701. if (err < 0) {
  702. dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
  703. goto reset;
  704. }
  705. err = tegra_xusb_setup_ports(padctl);
  706. if (err) {
  707. dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
  708. goto remove_pads;
  709. }
  710. return 0;
  711. remove_pads:
  712. tegra_xusb_remove_pads(padctl);
  713. reset:
  714. reset_control_assert(padctl->rst);
  715. remove:
  716. soc->ops->remove(padctl);
  717. return err;
  718. }
  719. static int tegra_xusb_padctl_remove(struct platform_device *pdev)
  720. {
  721. struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
  722. int err;
  723. tegra_xusb_remove_ports(padctl);
  724. tegra_xusb_remove_pads(padctl);
  725. err = reset_control_assert(padctl->rst);
  726. if (err < 0)
  727. dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
  728. padctl->soc->ops->remove(padctl);
  729. return err;
  730. }
  731. static struct platform_driver tegra_xusb_padctl_driver = {
  732. .driver = {
  733. .name = "tegra-xusb-padctl",
  734. .of_match_table = tegra_xusb_padctl_of_match,
  735. },
  736. .probe = tegra_xusb_padctl_probe,
  737. .remove = tegra_xusb_padctl_remove,
  738. };
  739. module_platform_driver(tegra_xusb_padctl_driver);
  740. struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
  741. {
  742. struct tegra_xusb_padctl *padctl;
  743. struct platform_device *pdev;
  744. struct device_node *np;
  745. np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
  746. if (!np)
  747. return ERR_PTR(-EINVAL);
  748. /*
  749. * This is slightly ugly. A better implementation would be to keep a
  750. * registry of pad controllers, but since there will almost certainly
  751. * only ever be one per SoC that would be a little overkill.
  752. */
  753. pdev = of_find_device_by_node(np);
  754. if (!pdev) {
  755. of_node_put(np);
  756. return ERR_PTR(-ENODEV);
  757. }
  758. of_node_put(np);
  759. padctl = platform_get_drvdata(pdev);
  760. if (!padctl) {
  761. put_device(&pdev->dev);
  762. return ERR_PTR(-EPROBE_DEFER);
  763. }
  764. return padctl;
  765. }
  766. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
  767. void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
  768. {
  769. if (padctl)
  770. put_device(padctl->dev);
  771. }
  772. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
  773. int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
  774. unsigned int port)
  775. {
  776. if (padctl->soc->ops->usb3_save_context)
  777. return padctl->soc->ops->usb3_save_context(padctl, port);
  778. return -ENOSYS;
  779. }
  780. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
  781. int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
  782. unsigned int port, bool idle)
  783. {
  784. if (padctl->soc->ops->hsic_set_idle)
  785. return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
  786. return -ENOSYS;
  787. }
  788. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
  789. int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
  790. unsigned int port, bool enable)
  791. {
  792. if (padctl->soc->ops->usb3_set_lfps_detect)
  793. return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
  794. enable);
  795. return -ENOSYS;
  796. }
  797. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
  798. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  799. MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
  800. MODULE_LICENSE("GPL v2");