i2c-pxa-pci.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * The CE4100's I2C device is more or less the same one as found on PXA.
  3. * It does not support slave mode, the register slightly moved. This PCI
  4. * device provides three bars, every contains a single I2C controller.
  5. */
  6. #include <linux/pci.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/i2c/pxa-i2c.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_address.h>
  12. #define CE4100_PCI_I2C_DEVS 3
  13. struct ce4100_devices {
  14. struct platform_device *pdev[CE4100_PCI_I2C_DEVS];
  15. };
  16. static struct platform_device *add_i2c_device(struct pci_dev *dev, int bar)
  17. {
  18. struct platform_device *pdev;
  19. struct i2c_pxa_platform_data pdata;
  20. struct resource res[2];
  21. struct device_node *child;
  22. static int devnum;
  23. int ret;
  24. memset(&pdata, 0, sizeof(struct i2c_pxa_platform_data));
  25. memset(&res, 0, sizeof(res));
  26. res[0].flags = IORESOURCE_MEM;
  27. res[0].start = pci_resource_start(dev, bar);
  28. res[0].end = pci_resource_end(dev, bar);
  29. res[1].flags = IORESOURCE_IRQ;
  30. res[1].start = dev->irq;
  31. res[1].end = dev->irq;
  32. for_each_child_of_node(dev->dev.of_node, child) {
  33. const void *prop;
  34. struct resource r;
  35. int ret;
  36. ret = of_address_to_resource(child, 0, &r);
  37. if (ret < 0)
  38. continue;
  39. if (r.start != res[0].start)
  40. continue;
  41. if (r.end != res[0].end)
  42. continue;
  43. if (r.flags != res[0].flags)
  44. continue;
  45. prop = of_get_property(child, "fast-mode", NULL);
  46. if (prop)
  47. pdata.fast_mode = 1;
  48. break;
  49. }
  50. if (!child) {
  51. dev_err(&dev->dev, "failed to match a DT node for bar %d.\n",
  52. bar);
  53. ret = -EINVAL;
  54. goto out;
  55. }
  56. pdev = platform_device_alloc("ce4100-i2c", devnum);
  57. if (!pdev) {
  58. of_node_put(child);
  59. ret = -ENOMEM;
  60. goto out;
  61. }
  62. pdev->dev.parent = &dev->dev;
  63. pdev->dev.of_node = child;
  64. ret = platform_device_add_resources(pdev, res, ARRAY_SIZE(res));
  65. if (ret)
  66. goto err;
  67. ret = platform_device_add_data(pdev, &pdata, sizeof(pdata));
  68. if (ret)
  69. goto err;
  70. ret = platform_device_add(pdev);
  71. if (ret)
  72. goto err;
  73. devnum++;
  74. return pdev;
  75. err:
  76. platform_device_put(pdev);
  77. out:
  78. return ERR_PTR(ret);
  79. }
  80. static int __devinit ce4100_i2c_probe(struct pci_dev *dev,
  81. const struct pci_device_id *ent)
  82. {
  83. int ret;
  84. int i;
  85. struct ce4100_devices *sds;
  86. ret = pci_enable_device_mem(dev);
  87. if (ret)
  88. return ret;
  89. if (!dev->dev.of_node) {
  90. dev_err(&dev->dev, "Missing device tree node.\n");
  91. return -EINVAL;
  92. }
  93. sds = kzalloc(sizeof(*sds), GFP_KERNEL);
  94. if (!sds) {
  95. ret = -ENOMEM;
  96. goto err_mem;
  97. }
  98. for (i = 0; i < ARRAY_SIZE(sds->pdev); i++) {
  99. sds->pdev[i] = add_i2c_device(dev, i);
  100. if (IS_ERR(sds->pdev[i])) {
  101. ret = PTR_ERR(sds->pdev[i]);
  102. while (--i >= 0)
  103. platform_device_unregister(sds->pdev[i]);
  104. goto err_dev_add;
  105. }
  106. }
  107. pci_set_drvdata(dev, sds);
  108. return 0;
  109. err_dev_add:
  110. pci_set_drvdata(dev, NULL);
  111. kfree(sds);
  112. err_mem:
  113. pci_disable_device(dev);
  114. return ret;
  115. }
  116. static void __devexit ce4100_i2c_remove(struct pci_dev *dev)
  117. {
  118. struct ce4100_devices *sds;
  119. unsigned int i;
  120. sds = pci_get_drvdata(dev);
  121. pci_set_drvdata(dev, NULL);
  122. for (i = 0; i < ARRAY_SIZE(sds->pdev); i++)
  123. platform_device_unregister(sds->pdev[i]);
  124. pci_disable_device(dev);
  125. kfree(sds);
  126. }
  127. static struct pci_device_id ce4100_i2c_devices[] __devinitdata = {
  128. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2e68)},
  129. { },
  130. };
  131. MODULE_DEVICE_TABLE(pci, ce4100_i2c_devices);
  132. static struct pci_driver ce4100_i2c_driver = {
  133. .name = "ce4100_i2c",
  134. .id_table = ce4100_i2c_devices,
  135. .probe = ce4100_i2c_probe,
  136. .remove = __devexit_p(ce4100_i2c_remove),
  137. };
  138. static int __init ce4100_i2c_init(void)
  139. {
  140. return pci_register_driver(&ce4100_i2c_driver);
  141. }
  142. module_init(ce4100_i2c_init);
  143. static void __exit ce4100_i2c_exit(void)
  144. {
  145. pci_unregister_driver(&ce4100_i2c_driver);
  146. }
  147. module_exit(ce4100_i2c_exit);
  148. MODULE_DESCRIPTION("CE4100 PCI-I2C glue code for PXA's driver");
  149. MODULE_LICENSE("GPL v2");
  150. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");