i2c-versatile.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * i2c-versatile.c
  3. *
  4. * Copyright (C) 2006 ARM Ltd.
  5. * written by Russell King, Deep Blue Solutions Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/i2c-algo-bit.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/io.h>
  19. #include <linux/of_i2c.h>
  20. #define I2C_CONTROL 0x00
  21. #define I2C_CONTROLS 0x00
  22. #define I2C_CONTROLC 0x04
  23. #define SCL (1 << 0)
  24. #define SDA (1 << 1)
  25. struct i2c_versatile {
  26. struct i2c_adapter adap;
  27. struct i2c_algo_bit_data algo;
  28. void __iomem *base;
  29. };
  30. static void i2c_versatile_setsda(void *data, int state)
  31. {
  32. struct i2c_versatile *i2c = data;
  33. writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
  34. }
  35. static void i2c_versatile_setscl(void *data, int state)
  36. {
  37. struct i2c_versatile *i2c = data;
  38. writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
  39. }
  40. static int i2c_versatile_getsda(void *data)
  41. {
  42. struct i2c_versatile *i2c = data;
  43. return !!(readl(i2c->base + I2C_CONTROL) & SDA);
  44. }
  45. static int i2c_versatile_getscl(void *data)
  46. {
  47. struct i2c_versatile *i2c = data;
  48. return !!(readl(i2c->base + I2C_CONTROL) & SCL);
  49. }
  50. static struct i2c_algo_bit_data i2c_versatile_algo = {
  51. .setsda = i2c_versatile_setsda,
  52. .setscl = i2c_versatile_setscl,
  53. .getsda = i2c_versatile_getsda,
  54. .getscl = i2c_versatile_getscl,
  55. .udelay = 30,
  56. .timeout = HZ,
  57. };
  58. static int i2c_versatile_probe(struct platform_device *dev)
  59. {
  60. struct i2c_versatile *i2c;
  61. struct resource *r;
  62. int ret;
  63. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  64. if (!r) {
  65. ret = -EINVAL;
  66. goto err_out;
  67. }
  68. if (!request_mem_region(r->start, resource_size(r), "versatile-i2c")) {
  69. ret = -EBUSY;
  70. goto err_out;
  71. }
  72. i2c = kzalloc(sizeof(struct i2c_versatile), GFP_KERNEL);
  73. if (!i2c) {
  74. ret = -ENOMEM;
  75. goto err_release;
  76. }
  77. i2c->base = ioremap(r->start, resource_size(r));
  78. if (!i2c->base) {
  79. ret = -ENOMEM;
  80. goto err_free;
  81. }
  82. writel(SCL | SDA, i2c->base + I2C_CONTROLS);
  83. i2c->adap.owner = THIS_MODULE;
  84. strlcpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
  85. i2c->adap.algo_data = &i2c->algo;
  86. i2c->adap.dev.parent = &dev->dev;
  87. i2c->adap.dev.of_node = dev->dev.of_node;
  88. i2c->algo = i2c_versatile_algo;
  89. i2c->algo.data = i2c;
  90. if (dev->id >= 0) {
  91. /* static bus numbering */
  92. i2c->adap.nr = dev->id;
  93. ret = i2c_bit_add_numbered_bus(&i2c->adap);
  94. } else
  95. /* dynamic bus numbering */
  96. ret = i2c_bit_add_bus(&i2c->adap);
  97. if (ret >= 0) {
  98. platform_set_drvdata(dev, i2c);
  99. of_i2c_register_devices(&i2c->adap);
  100. return 0;
  101. }
  102. iounmap(i2c->base);
  103. err_free:
  104. kfree(i2c);
  105. err_release:
  106. release_mem_region(r->start, resource_size(r));
  107. err_out:
  108. return ret;
  109. }
  110. static int i2c_versatile_remove(struct platform_device *dev)
  111. {
  112. struct i2c_versatile *i2c = platform_get_drvdata(dev);
  113. platform_set_drvdata(dev, NULL);
  114. i2c_del_adapter(&i2c->adap);
  115. return 0;
  116. }
  117. static const struct of_device_id i2c_versatile_match[] = {
  118. { .compatible = "arm,versatile-i2c", },
  119. {},
  120. };
  121. MODULE_DEVICE_TABLE(of, i2c_versatile_match);
  122. static struct platform_driver i2c_versatile_driver = {
  123. .probe = i2c_versatile_probe,
  124. .remove = i2c_versatile_remove,
  125. .driver = {
  126. .name = "versatile-i2c",
  127. .owner = THIS_MODULE,
  128. .of_match_table = i2c_versatile_match,
  129. },
  130. };
  131. static int __init i2c_versatile_init(void)
  132. {
  133. return platform_driver_register(&i2c_versatile_driver);
  134. }
  135. static void __exit i2c_versatile_exit(void)
  136. {
  137. platform_driver_unregister(&i2c_versatile_driver);
  138. }
  139. subsys_initcall(i2c_versatile_init);
  140. module_exit(i2c_versatile_exit);
  141. MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
  142. MODULE_LICENSE("GPL");
  143. MODULE_ALIAS("platform:versatile-i2c");