i2c-pxa-pci.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. goto err_mem;
  96. for (i = 0; i < ARRAY_SIZE(sds->pdev); i++) {
  97. sds->pdev[i] = add_i2c_device(dev, i);
  98. if (IS_ERR(sds->pdev[i])) {
  99. while (--i >= 0)
  100. platform_device_unregister(sds->pdev[i]);
  101. goto err_dev_add;
  102. }
  103. }
  104. pci_set_drvdata(dev, sds);
  105. return 0;
  106. err_dev_add:
  107. pci_set_drvdata(dev, NULL);
  108. kfree(sds);
  109. err_mem:
  110. pci_disable_device(dev);
  111. return ret;
  112. }
  113. static void __devexit ce4100_i2c_remove(struct pci_dev *dev)
  114. {
  115. struct ce4100_devices *sds;
  116. unsigned int i;
  117. sds = pci_get_drvdata(dev);
  118. pci_set_drvdata(dev, NULL);
  119. for (i = 0; i < ARRAY_SIZE(sds->pdev); i++)
  120. platform_device_unregister(sds->pdev[i]);
  121. pci_disable_device(dev);
  122. kfree(sds);
  123. }
  124. static struct pci_device_id ce4100_i2c_devices[] __devinitdata = {
  125. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2e68)},
  126. { },
  127. };
  128. MODULE_DEVICE_TABLE(pci, ce4100_i2c_devices);
  129. static struct pci_driver ce4100_i2c_driver = {
  130. .name = "ce4100_i2c",
  131. .id_table = ce4100_i2c_devices,
  132. .probe = ce4100_i2c_probe,
  133. .remove = __devexit_p(ce4100_i2c_remove),
  134. };
  135. static int __init ce4100_i2c_init(void)
  136. {
  137. return pci_register_driver(&ce4100_i2c_driver);
  138. }
  139. module_init(ce4100_i2c_init);
  140. static void __exit ce4100_i2c_exit(void)
  141. {
  142. pci_unregister_driver(&ce4100_i2c_driver);
  143. }
  144. module_exit(ce4100_i2c_exit);
  145. MODULE_DESCRIPTION("CE4100 PCI-I2C glue code for PXA's driver");
  146. MODULE_LICENSE("GPL v2");
  147. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");