i2c-versatile.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <asm/io.h>
  18. #define I2C_CONTROL 0x00
  19. #define I2C_CONTROLS 0x00
  20. #define I2C_CONTROLC 0x04
  21. #define SCL (1 << 0)
  22. #define SDA (1 << 1)
  23. struct i2c_versatile {
  24. struct i2c_adapter adap;
  25. struct i2c_algo_bit_data algo;
  26. void __iomem *base;
  27. };
  28. static void i2c_versatile_setsda(void *data, int state)
  29. {
  30. struct i2c_versatile *i2c = data;
  31. writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
  32. }
  33. static void i2c_versatile_setscl(void *data, int state)
  34. {
  35. struct i2c_versatile *i2c = data;
  36. writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
  37. }
  38. static int i2c_versatile_getsda(void *data)
  39. {
  40. struct i2c_versatile *i2c = data;
  41. return !!(readl(i2c->base + I2C_CONTROL) & SDA);
  42. }
  43. static int i2c_versatile_getscl(void *data)
  44. {
  45. struct i2c_versatile *i2c = data;
  46. return !!(readl(i2c->base + I2C_CONTROL) & SCL);
  47. }
  48. static struct i2c_algo_bit_data i2c_versatile_algo = {
  49. .setsda = i2c_versatile_setsda,
  50. .setscl = i2c_versatile_setscl,
  51. .getsda = i2c_versatile_getsda,
  52. .getscl = i2c_versatile_getscl,
  53. .udelay = 30,
  54. .timeout = HZ,
  55. };
  56. static int i2c_versatile_probe(struct platform_device *dev)
  57. {
  58. struct i2c_versatile *i2c;
  59. struct resource *r;
  60. int ret;
  61. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  62. if (!r) {
  63. ret = -EINVAL;
  64. goto err_out;
  65. }
  66. if (!request_mem_region(r->start, r->end - r->start + 1, "versatile-i2c")) {
  67. ret = -EBUSY;
  68. goto err_out;
  69. }
  70. i2c = kzalloc(sizeof(struct i2c_versatile), GFP_KERNEL);
  71. if (!i2c) {
  72. ret = -ENOMEM;
  73. goto err_release;
  74. }
  75. i2c->base = ioremap(r->start, r->end - r->start + 1);
  76. if (!i2c->base) {
  77. ret = -ENOMEM;
  78. goto err_free;
  79. }
  80. writel(SCL | SDA, i2c->base + I2C_CONTROLS);
  81. i2c->adap.owner = THIS_MODULE;
  82. strlcpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
  83. i2c->adap.algo_data = &i2c->algo;
  84. i2c->adap.dev.parent = &dev->dev;
  85. i2c->algo = i2c_versatile_algo;
  86. i2c->algo.data = i2c;
  87. ret = i2c_bit_add_bus(&i2c->adap);
  88. if (ret >= 0) {
  89. platform_set_drvdata(dev, i2c);
  90. return 0;
  91. }
  92. iounmap(i2c->base);
  93. err_free:
  94. kfree(i2c);
  95. err_release:
  96. release_mem_region(r->start, r->end - r->start + 1);
  97. err_out:
  98. return ret;
  99. }
  100. static int i2c_versatile_remove(struct platform_device *dev)
  101. {
  102. struct i2c_versatile *i2c = platform_get_drvdata(dev);
  103. platform_set_drvdata(dev, NULL);
  104. i2c_del_adapter(&i2c->adap);
  105. return 0;
  106. }
  107. static struct platform_driver i2c_versatile_driver = {
  108. .probe = i2c_versatile_probe,
  109. .remove = i2c_versatile_remove,
  110. .driver = {
  111. .name = "versatile-i2c",
  112. .owner = THIS_MODULE,
  113. },
  114. };
  115. static int __init i2c_versatile_init(void)
  116. {
  117. return platform_driver_register(&i2c_versatile_driver);
  118. }
  119. static void __exit i2c_versatile_exit(void)
  120. {
  121. platform_driver_unregister(&i2c_versatile_driver);
  122. }
  123. module_init(i2c_versatile_init);
  124. module_exit(i2c_versatile_exit);
  125. MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
  126. MODULE_LICENSE("GPL");
  127. MODULE_ALIAS("platform:versatile-i2c");