fsl-mph-dr-of.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Setup platform devices needed by the Freescale multi-port host
  3. * and/or dual-role USB controller modules based on the description
  4. * in flat device tree.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/fsl_devices.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/of_platform.h>
  17. struct fsl_usb2_dev_data {
  18. char *dr_mode; /* controller mode */
  19. char *drivers[3]; /* drivers to instantiate for this mode */
  20. enum fsl_usb2_operating_modes op_mode; /* operating mode */
  21. };
  22. struct fsl_usb2_dev_data dr_mode_data[] __devinitdata = {
  23. {
  24. .dr_mode = "host",
  25. .drivers = { "fsl-ehci", NULL, NULL, },
  26. .op_mode = FSL_USB2_DR_HOST,
  27. },
  28. {
  29. .dr_mode = "otg",
  30. .drivers = { "fsl-usb2-otg", "fsl-ehci", "fsl-usb2-udc", },
  31. .op_mode = FSL_USB2_DR_OTG,
  32. },
  33. {
  34. .dr_mode = "peripheral",
  35. .drivers = { "fsl-usb2-udc", NULL, NULL, },
  36. .op_mode = FSL_USB2_DR_DEVICE,
  37. },
  38. };
  39. struct fsl_usb2_dev_data * __devinit get_dr_mode_data(struct device_node *np)
  40. {
  41. const unsigned char *prop;
  42. int i;
  43. prop = of_get_property(np, "dr_mode", NULL);
  44. if (prop) {
  45. for (i = 0; i < ARRAY_SIZE(dr_mode_data); i++) {
  46. if (!strcmp(prop, dr_mode_data[i].dr_mode))
  47. return &dr_mode_data[i];
  48. }
  49. }
  50. pr_warn("%s: Invalid 'dr_mode' property, fallback to host mode\n",
  51. np->full_name);
  52. return &dr_mode_data[0]; /* mode not specified, use host */
  53. }
  54. static enum fsl_usb2_phy_modes __devinit determine_usb_phy(const char *phy_type)
  55. {
  56. if (!phy_type)
  57. return FSL_USB2_PHY_NONE;
  58. if (!strcasecmp(phy_type, "ulpi"))
  59. return FSL_USB2_PHY_ULPI;
  60. if (!strcasecmp(phy_type, "utmi"))
  61. return FSL_USB2_PHY_UTMI;
  62. if (!strcasecmp(phy_type, "utmi_wide"))
  63. return FSL_USB2_PHY_UTMI_WIDE;
  64. if (!strcasecmp(phy_type, "serial"))
  65. return FSL_USB2_PHY_SERIAL;
  66. return FSL_USB2_PHY_NONE;
  67. }
  68. struct platform_device * __devinit fsl_usb2_device_register(
  69. struct platform_device *ofdev,
  70. struct fsl_usb2_platform_data *pdata,
  71. const char *name, int id)
  72. {
  73. struct platform_device *pdev;
  74. const struct resource *res = ofdev->resource;
  75. unsigned int num = ofdev->num_resources;
  76. int retval;
  77. pdev = platform_device_alloc(name, id);
  78. if (!pdev) {
  79. retval = -ENOMEM;
  80. goto error;
  81. }
  82. pdev->dev.parent = &ofdev->dev;
  83. pdev->dev.coherent_dma_mask = ofdev->dev.coherent_dma_mask;
  84. pdev->dev.dma_mask = &pdev->archdata.dma_mask;
  85. *pdev->dev.dma_mask = *ofdev->dev.dma_mask;
  86. retval = platform_device_add_data(pdev, pdata, sizeof(*pdata));
  87. if (retval)
  88. goto error;
  89. if (num) {
  90. retval = platform_device_add_resources(pdev, res, num);
  91. if (retval)
  92. goto error;
  93. }
  94. retval = platform_device_add(pdev);
  95. if (retval)
  96. goto error;
  97. return pdev;
  98. error:
  99. platform_device_put(pdev);
  100. return ERR_PTR(retval);
  101. }
  102. static const struct of_device_id fsl_usb2_mph_dr_of_match[];
  103. static int __devinit fsl_usb2_mph_dr_of_probe(struct platform_device *ofdev)
  104. {
  105. struct device_node *np = ofdev->dev.of_node;
  106. struct platform_device *usb_dev;
  107. struct fsl_usb2_platform_data data, *pdata;
  108. struct fsl_usb2_dev_data *dev_data;
  109. const struct of_device_id *match;
  110. const unsigned char *prop;
  111. static unsigned int idx;
  112. int i;
  113. if (!of_device_is_available(np))
  114. return -ENODEV;
  115. match = of_match_device(fsl_usb2_mph_dr_of_match, &ofdev->dev);
  116. if (!match)
  117. return -ENODEV;
  118. pdata = &data;
  119. if (match->data)
  120. memcpy(pdata, match->data, sizeof(data));
  121. else
  122. memset(pdata, 0, sizeof(data));
  123. dev_data = get_dr_mode_data(np);
  124. if (of_device_is_compatible(np, "fsl-usb2-mph")) {
  125. if (of_get_property(np, "port0", NULL))
  126. pdata->port_enables |= FSL_USB2_PORT0_ENABLED;
  127. if (of_get_property(np, "port1", NULL))
  128. pdata->port_enables |= FSL_USB2_PORT1_ENABLED;
  129. pdata->operating_mode = FSL_USB2_MPH_HOST;
  130. } else {
  131. /* setup mode selected in the device tree */
  132. pdata->operating_mode = dev_data->op_mode;
  133. }
  134. prop = of_get_property(np, "phy_type", NULL);
  135. pdata->phy_mode = determine_usb_phy(prop);
  136. for (i = 0; i < ARRAY_SIZE(dev_data->drivers); i++) {
  137. if (!dev_data->drivers[i])
  138. continue;
  139. usb_dev = fsl_usb2_device_register(ofdev, pdata,
  140. dev_data->drivers[i], idx);
  141. if (IS_ERR(usb_dev)) {
  142. dev_err(&ofdev->dev, "Can't register usb device\n");
  143. return PTR_ERR(usb_dev);
  144. }
  145. }
  146. idx++;
  147. return 0;
  148. }
  149. static int __devexit __unregister_subdev(struct device *dev, void *d)
  150. {
  151. platform_device_unregister(to_platform_device(dev));
  152. return 0;
  153. }
  154. static int __devexit fsl_usb2_mph_dr_of_remove(struct platform_device *ofdev)
  155. {
  156. device_for_each_child(&ofdev->dev, NULL, __unregister_subdev);
  157. return 0;
  158. }
  159. static const struct of_device_id fsl_usb2_mph_dr_of_match[] = {
  160. { .compatible = "fsl-usb2-mph", },
  161. { .compatible = "fsl-usb2-dr", },
  162. {},
  163. };
  164. static struct platform_driver fsl_usb2_mph_dr_driver = {
  165. .driver = {
  166. .name = "fsl-usb2-mph-dr",
  167. .owner = THIS_MODULE,
  168. .of_match_table = fsl_usb2_mph_dr_of_match,
  169. },
  170. .probe = fsl_usb2_mph_dr_of_probe,
  171. .remove = __devexit_p(fsl_usb2_mph_dr_of_remove),
  172. };
  173. static int __init fsl_usb2_mph_dr_init(void)
  174. {
  175. return platform_driver_register(&fsl_usb2_mph_dr_driver);
  176. }
  177. module_init(fsl_usb2_mph_dr_init);
  178. static void __exit fsl_usb2_mph_dr_exit(void)
  179. {
  180. platform_driver_unregister(&fsl_usb2_mph_dr_driver);
  181. }
  182. module_exit(fsl_usb2_mph_dr_exit);
  183. MODULE_DESCRIPTION("FSL MPH DR OF devices driver");
  184. MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
  185. MODULE_LICENSE("GPL");