xusb.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. static int
  87. 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_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. /* 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->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. struct device_node *ports, *np;
  356. char *name;
  357. ports = of_get_child_by_name(padctl->dev->of_node, "ports");
  358. if (!ports)
  359. return NULL;
  360. name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
  361. if (!name) {
  362. of_node_put(ports);
  363. return ERR_PTR(-ENOMEM);
  364. }
  365. np = of_get_child_by_name(ports, name);
  366. kfree(name);
  367. of_node_put(ports);
  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. return PTR_ERR_OR_ZERO(usb2->supply);
  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. return PTR_ERR_OR_ZERO(usb3->supply);
  579. }
  580. static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
  581. unsigned int index)
  582. {
  583. struct tegra_xusb_usb3_port *usb3;
  584. struct device_node *np;
  585. int err = 0;
  586. /*
  587. * If there is no supplemental configuration in the device tree the
  588. * port is unusable. But it is valid to configure only a single port,
  589. * hence return 0 instead of an error to allow ports to be optional.
  590. */
  591. np = tegra_xusb_find_port_node(padctl, "usb3", index);
  592. if (!np || !of_device_is_available(np))
  593. goto out;
  594. usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
  595. if (!usb3) {
  596. err = -ENOMEM;
  597. goto out;
  598. }
  599. err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
  600. if (err < 0)
  601. goto out;
  602. usb3->base.ops = padctl->soc->ports.usb3.ops;
  603. usb3->base.lane = usb3->base.ops->map(&usb3->base);
  604. if (IS_ERR(usb3->base.lane)) {
  605. err = PTR_ERR(usb3->base.lane);
  606. goto out;
  607. }
  608. err = tegra_xusb_usb3_port_parse_dt(usb3);
  609. if (err < 0) {
  610. tegra_xusb_port_unregister(&usb3->base);
  611. goto out;
  612. }
  613. list_add_tail(&usb3->base.list, &padctl->ports);
  614. out:
  615. of_node_put(np);
  616. return err;
  617. }
  618. static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
  619. {
  620. struct tegra_xusb_port *port, *tmp;
  621. list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
  622. list_del(&port->list);
  623. tegra_xusb_port_unregister(port);
  624. }
  625. }
  626. static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
  627. {
  628. struct tegra_xusb_port *port;
  629. unsigned int i;
  630. int err = 0;
  631. mutex_lock(&padctl->lock);
  632. for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
  633. err = tegra_xusb_add_usb2_port(padctl, i);
  634. if (err < 0)
  635. goto remove_ports;
  636. }
  637. for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
  638. err = tegra_xusb_add_ulpi_port(padctl, i);
  639. if (err < 0)
  640. goto remove_ports;
  641. }
  642. for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
  643. err = tegra_xusb_add_hsic_port(padctl, i);
  644. if (err < 0)
  645. goto remove_ports;
  646. }
  647. for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
  648. err = tegra_xusb_add_usb3_port(padctl, i);
  649. if (err < 0)
  650. goto remove_ports;
  651. }
  652. list_for_each_entry(port, &padctl->ports, list) {
  653. err = port->ops->enable(port);
  654. if (err < 0)
  655. dev_err(padctl->dev, "failed to enable port %s: %d\n",
  656. dev_name(&port->dev), err);
  657. }
  658. goto unlock;
  659. remove_ports:
  660. __tegra_xusb_remove_ports(padctl);
  661. unlock:
  662. mutex_unlock(&padctl->lock);
  663. return err;
  664. }
  665. static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
  666. {
  667. mutex_lock(&padctl->lock);
  668. __tegra_xusb_remove_ports(padctl);
  669. mutex_unlock(&padctl->lock);
  670. }
  671. static int tegra_xusb_padctl_probe(struct platform_device *pdev)
  672. {
  673. struct device_node *np = pdev->dev.of_node;
  674. const struct tegra_xusb_padctl_soc *soc;
  675. struct tegra_xusb_padctl *padctl;
  676. const struct of_device_id *match;
  677. struct resource *res;
  678. int err;
  679. /* for backwards compatibility with old device trees */
  680. np = of_get_child_by_name(np, "pads");
  681. if (!np) {
  682. dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
  683. return tegra_xusb_padctl_legacy_probe(pdev);
  684. }
  685. of_node_put(np);
  686. match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
  687. soc = match->data;
  688. padctl = soc->ops->probe(&pdev->dev, soc);
  689. if (IS_ERR(padctl))
  690. return PTR_ERR(padctl);
  691. platform_set_drvdata(pdev, padctl);
  692. INIT_LIST_HEAD(&padctl->ports);
  693. INIT_LIST_HEAD(&padctl->lanes);
  694. INIT_LIST_HEAD(&padctl->pads);
  695. mutex_init(&padctl->lock);
  696. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  697. padctl->regs = devm_ioremap_resource(&pdev->dev, res);
  698. if (IS_ERR(padctl->regs)) {
  699. err = PTR_ERR(padctl->regs);
  700. goto remove;
  701. }
  702. padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
  703. if (IS_ERR(padctl->rst)) {
  704. err = PTR_ERR(padctl->rst);
  705. goto remove;
  706. }
  707. err = reset_control_deassert(padctl->rst);
  708. if (err < 0)
  709. goto remove;
  710. err = tegra_xusb_setup_pads(padctl);
  711. if (err < 0) {
  712. dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
  713. goto reset;
  714. }
  715. err = tegra_xusb_setup_ports(padctl);
  716. if (err) {
  717. dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
  718. goto remove_pads;
  719. }
  720. return 0;
  721. remove_pads:
  722. tegra_xusb_remove_pads(padctl);
  723. reset:
  724. reset_control_assert(padctl->rst);
  725. remove:
  726. soc->ops->remove(padctl);
  727. return err;
  728. }
  729. static int tegra_xusb_padctl_remove(struct platform_device *pdev)
  730. {
  731. struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
  732. int err;
  733. tegra_xusb_remove_ports(padctl);
  734. tegra_xusb_remove_pads(padctl);
  735. err = reset_control_assert(padctl->rst);
  736. if (err < 0)
  737. dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
  738. padctl->soc->ops->remove(padctl);
  739. return err;
  740. }
  741. static struct platform_driver tegra_xusb_padctl_driver = {
  742. .driver = {
  743. .name = "tegra-xusb-padctl",
  744. .of_match_table = tegra_xusb_padctl_of_match,
  745. },
  746. .probe = tegra_xusb_padctl_probe,
  747. .remove = tegra_xusb_padctl_remove,
  748. };
  749. module_platform_driver(tegra_xusb_padctl_driver);
  750. struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
  751. {
  752. struct tegra_xusb_padctl *padctl;
  753. struct platform_device *pdev;
  754. struct device_node *np;
  755. np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
  756. if (!np)
  757. return ERR_PTR(-EINVAL);
  758. /*
  759. * This is slightly ugly. A better implementation would be to keep a
  760. * registry of pad controllers, but since there will almost certainly
  761. * only ever be one per SoC that would be a little overkill.
  762. */
  763. pdev = of_find_device_by_node(np);
  764. if (!pdev) {
  765. of_node_put(np);
  766. return ERR_PTR(-ENODEV);
  767. }
  768. of_node_put(np);
  769. padctl = platform_get_drvdata(pdev);
  770. if (!padctl) {
  771. put_device(&pdev->dev);
  772. return ERR_PTR(-EPROBE_DEFER);
  773. }
  774. return padctl;
  775. }
  776. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
  777. void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
  778. {
  779. if (padctl)
  780. put_device(padctl->dev);
  781. }
  782. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
  783. int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
  784. unsigned int port)
  785. {
  786. if (padctl->soc->ops->usb3_save_context)
  787. return padctl->soc->ops->usb3_save_context(padctl, port);
  788. return -ENOSYS;
  789. }
  790. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
  791. int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
  792. unsigned int port, bool idle)
  793. {
  794. if (padctl->soc->ops->hsic_set_idle)
  795. return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
  796. return -ENOSYS;
  797. }
  798. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
  799. int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
  800. unsigned int port, bool enable)
  801. {
  802. if (padctl->soc->ops->usb3_set_lfps_detect)
  803. return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
  804. enable);
  805. return -ENOSYS;
  806. }
  807. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
  808. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  809. MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
  810. MODULE_LICENSE("GPL v2");