i2c-versatile.c 3.5 KB

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