i2c-versatile.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * i2c-versatile.c
  4. *
  5. * Copyright (C) 2006 ARM Ltd.
  6. * written by Russell King, Deep Blue Solutions Ltd.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/i2c.h>
  11. #include <linux/i2c-algo-bit.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #define I2C_CONTROL 0x00
  17. #define I2C_CONTROLS 0x00
  18. #define I2C_CONTROLC 0x04
  19. #define SCL (1 << 0)
  20. #define SDA (1 << 1)
  21. struct i2c_versatile {
  22. struct i2c_adapter adap;
  23. struct i2c_algo_bit_data algo;
  24. void __iomem *base;
  25. };
  26. static void i2c_versatile_setsda(void *data, int state)
  27. {
  28. struct i2c_versatile *i2c = data;
  29. writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
  30. }
  31. static void i2c_versatile_setscl(void *data, int state)
  32. {
  33. struct i2c_versatile *i2c = data;
  34. writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
  35. }
  36. static int i2c_versatile_getsda(void *data)
  37. {
  38. struct i2c_versatile *i2c = data;
  39. return !!(readl(i2c->base + I2C_CONTROL) & SDA);
  40. }
  41. static int i2c_versatile_getscl(void *data)
  42. {
  43. struct i2c_versatile *i2c = data;
  44. return !!(readl(i2c->base + I2C_CONTROL) & SCL);
  45. }
  46. static const struct i2c_algo_bit_data i2c_versatile_algo = {
  47. .setsda = i2c_versatile_setsda,
  48. .setscl = i2c_versatile_setscl,
  49. .getsda = i2c_versatile_getsda,
  50. .getscl = i2c_versatile_getscl,
  51. .udelay = 30,
  52. .timeout = HZ,
  53. };
  54. static int i2c_versatile_probe(struct platform_device *dev)
  55. {
  56. struct i2c_versatile *i2c;
  57. struct resource *r;
  58. int ret;
  59. i2c = devm_kzalloc(&dev->dev, sizeof(struct i2c_versatile), GFP_KERNEL);
  60. if (!i2c)
  61. return -ENOMEM;
  62. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  63. i2c->base = devm_ioremap_resource(&dev->dev, r);
  64. if (IS_ERR(i2c->base))
  65. return PTR_ERR(i2c->base);
  66. writel(SCL | SDA, i2c->base + I2C_CONTROLS);
  67. i2c->adap.owner = THIS_MODULE;
  68. strscpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
  69. i2c->adap.algo_data = &i2c->algo;
  70. i2c->adap.dev.parent = &dev->dev;
  71. i2c->adap.dev.of_node = dev->dev.of_node;
  72. i2c->algo = i2c_versatile_algo;
  73. i2c->algo.data = i2c;
  74. i2c->adap.nr = dev->id;
  75. ret = i2c_bit_add_numbered_bus(&i2c->adap);
  76. if (ret < 0)
  77. return ret;
  78. platform_set_drvdata(dev, i2c);
  79. return 0;
  80. }
  81. static int i2c_versatile_remove(struct platform_device *dev)
  82. {
  83. struct i2c_versatile *i2c = platform_get_drvdata(dev);
  84. i2c_del_adapter(&i2c->adap);
  85. return 0;
  86. }
  87. static const struct of_device_id i2c_versatile_match[] = {
  88. { .compatible = "arm,versatile-i2c", },
  89. {},
  90. };
  91. MODULE_DEVICE_TABLE(of, i2c_versatile_match);
  92. static struct platform_driver i2c_versatile_driver = {
  93. .probe = i2c_versatile_probe,
  94. .remove = i2c_versatile_remove,
  95. .driver = {
  96. .name = "versatile-i2c",
  97. .of_match_table = i2c_versatile_match,
  98. },
  99. };
  100. static int __init i2c_versatile_init(void)
  101. {
  102. return platform_driver_register(&i2c_versatile_driver);
  103. }
  104. static void __exit i2c_versatile_exit(void)
  105. {
  106. platform_driver_unregister(&i2c_versatile_driver);
  107. }
  108. subsys_initcall(i2c_versatile_init);
  109. module_exit(i2c_versatile_exit);
  110. MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
  111. MODULE_LICENSE("GPL");
  112. MODULE_ALIAS("platform:versatile-i2c");