xusb.c 22 KB

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