i2c-versatile.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. i2c->adap.nr = dev->id;
  91. ret = i2c_bit_add_numbered_bus(&i2c->adap);
  92. if (ret >= 0) {
  93. platform_set_drvdata(dev, i2c);
  94. of_i2c_register_devices(&i2c->adap);
  95. return 0;
  96. }
  97. iounmap(i2c->base);
  98. err_free:
  99. kfree(i2c);
  100. err_release:
  101. release_mem_region(r->start, resource_size(r));
  102. err_out:
  103. return ret;
  104. }
  105. static int i2c_versatile_remove(struct platform_device *dev)
  106. {
  107. struct i2c_versatile *i2c = platform_get_drvdata(dev);
  108. i2c_del_adapter(&i2c->adap);
  109. return 0;
  110. }
  111. static const struct of_device_id i2c_versatile_match[] = {
  112. { .compatible = "arm,versatile-i2c", },
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, i2c_versatile_match);
  116. static struct platform_driver i2c_versatile_driver = {
  117. .probe = i2c_versatile_probe,
  118. .remove = i2c_versatile_remove,
  119. .driver = {
  120. .name = "versatile-i2c",
  121. .owner = THIS_MODULE,
  122. .of_match_table = i2c_versatile_match,
  123. },
  124. };
  125. static int __init i2c_versatile_init(void)
  126. {
  127. return platform_driver_register(&i2c_versatile_driver);
  128. }
  129. static void __exit i2c_versatile_exit(void)
  130. {
  131. platform_driver_unregister(&i2c_versatile_driver);
  132. }
  133. subsys_initcall(i2c_versatile_init);
  134. module_exit(i2c_versatile_exit);
  135. MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
  136. MODULE_LICENSE("GPL");
  137. MODULE_ALIAS("platform:versatile-i2c");