i2c-versatile.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. int ret;
  58. i2c = devm_kzalloc(&dev->dev, sizeof(struct i2c_versatile), GFP_KERNEL);
  59. if (!i2c)
  60. return -ENOMEM;
  61. i2c->base = devm_platform_get_and_ioremap_resource(dev, 0, NULL);
  62. if (IS_ERR(i2c->base))
  63. return PTR_ERR(i2c->base);
  64. writel(SCL | SDA, i2c->base + I2C_CONTROLS);
  65. i2c->adap.owner = THIS_MODULE;
  66. strscpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
  67. i2c->adap.algo_data = &i2c->algo;
  68. i2c->adap.dev.parent = &dev->dev;
  69. i2c->adap.dev.of_node = dev->dev.of_node;
  70. i2c->algo = i2c_versatile_algo;
  71. i2c->algo.data = i2c;
  72. i2c->adap.nr = dev->id;
  73. ret = i2c_bit_add_numbered_bus(&i2c->adap);
  74. if (ret < 0)
  75. return ret;
  76. platform_set_drvdata(dev, i2c);
  77. return 0;
  78. }
  79. static void i2c_versatile_remove(struct platform_device *dev)
  80. {
  81. struct i2c_versatile *i2c = platform_get_drvdata(dev);
  82. i2c_del_adapter(&i2c->adap);
  83. }
  84. static const struct of_device_id i2c_versatile_match[] = {
  85. { .compatible = "arm,versatile-i2c", },
  86. {},
  87. };
  88. MODULE_DEVICE_TABLE(of, i2c_versatile_match);
  89. static struct platform_driver i2c_versatile_driver = {
  90. .probe = i2c_versatile_probe,
  91. .remove_new = i2c_versatile_remove,
  92. .driver = {
  93. .name = "versatile-i2c",
  94. .of_match_table = i2c_versatile_match,
  95. },
  96. };
  97. static int __init i2c_versatile_init(void)
  98. {
  99. return platform_driver_register(&i2c_versatile_driver);
  100. }
  101. static void __exit i2c_versatile_exit(void)
  102. {
  103. platform_driver_unregister(&i2c_versatile_driver);
  104. }
  105. subsys_initcall(i2c_versatile_init);
  106. module_exit(i2c_versatile_exit);
  107. MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
  108. MODULE_LICENSE("GPL");
  109. MODULE_ALIAS("platform:versatile-i2c");