stmpe-i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ST Microelectronics MFD: stmpe's i2c client specific driver
  4. *
  5. * Copyright (C) ST-Ericsson SA 2010
  6. * Copyright (C) ST Microelectronics SA 2011
  7. *
  8. * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  9. * Author: Viresh Kumar <vireshk@kernel.org> for ST Microelectronics
  10. */
  11. #include <linux/i2c.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/of_device.h>
  17. #include "stmpe.h"
  18. static int i2c_reg_read(struct stmpe *stmpe, u8 reg)
  19. {
  20. struct i2c_client *i2c = stmpe->client;
  21. return i2c_smbus_read_byte_data(i2c, reg);
  22. }
  23. static int i2c_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
  24. {
  25. struct i2c_client *i2c = stmpe->client;
  26. return i2c_smbus_write_byte_data(i2c, reg, val);
  27. }
  28. static int i2c_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
  29. {
  30. struct i2c_client *i2c = stmpe->client;
  31. return i2c_smbus_read_i2c_block_data(i2c, reg, length, values);
  32. }
  33. static int i2c_block_write(struct stmpe *stmpe, u8 reg, u8 length,
  34. const u8 *values)
  35. {
  36. struct i2c_client *i2c = stmpe->client;
  37. return i2c_smbus_write_i2c_block_data(i2c, reg, length, values);
  38. }
  39. static struct stmpe_client_info i2c_ci = {
  40. .read_byte = i2c_reg_read,
  41. .write_byte = i2c_reg_write,
  42. .read_block = i2c_block_read,
  43. .write_block = i2c_block_write,
  44. };
  45. static const struct of_device_id stmpe_of_match[] = {
  46. { .compatible = "st,stmpe610", .data = (void *)STMPE610, },
  47. { .compatible = "st,stmpe801", .data = (void *)STMPE801, },
  48. { .compatible = "st,stmpe811", .data = (void *)STMPE811, },
  49. { .compatible = "st,stmpe1600", .data = (void *)STMPE1600, },
  50. { .compatible = "st,stmpe1601", .data = (void *)STMPE1601, },
  51. { .compatible = "st,stmpe1801", .data = (void *)STMPE1801, },
  52. { .compatible = "st,stmpe2401", .data = (void *)STMPE2401, },
  53. { .compatible = "st,stmpe2403", .data = (void *)STMPE2403, },
  54. {},
  55. };
  56. MODULE_DEVICE_TABLE(of, stmpe_of_match);
  57. static int
  58. stmpe_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  59. {
  60. enum stmpe_partnum partnum;
  61. const struct of_device_id *of_id;
  62. i2c_ci.data = (void *)id;
  63. i2c_ci.irq = i2c->irq;
  64. i2c_ci.client = i2c;
  65. i2c_ci.dev = &i2c->dev;
  66. of_id = of_match_device(stmpe_of_match, &i2c->dev);
  67. if (!of_id) {
  68. /*
  69. * This happens when the I2C ID matches the node name
  70. * but no real compatible string has been given.
  71. */
  72. dev_info(&i2c->dev, "matching on node name, compatible is preferred\n");
  73. partnum = id->driver_data;
  74. } else
  75. partnum = (enum stmpe_partnum)of_id->data;
  76. return stmpe_probe(&i2c_ci, partnum);
  77. }
  78. static int stmpe_i2c_remove(struct i2c_client *i2c)
  79. {
  80. struct stmpe *stmpe = dev_get_drvdata(&i2c->dev);
  81. return stmpe_remove(stmpe);
  82. }
  83. static const struct i2c_device_id stmpe_i2c_id[] = {
  84. { "stmpe610", STMPE610 },
  85. { "stmpe801", STMPE801 },
  86. { "stmpe811", STMPE811 },
  87. { "stmpe1600", STMPE1600 },
  88. { "stmpe1601", STMPE1601 },
  89. { "stmpe1801", STMPE1801 },
  90. { "stmpe2401", STMPE2401 },
  91. { "stmpe2403", STMPE2403 },
  92. { }
  93. };
  94. MODULE_DEVICE_TABLE(i2c, stmpe_i2c_id);
  95. static struct i2c_driver stmpe_i2c_driver = {
  96. .driver = {
  97. .name = "stmpe-i2c",
  98. #ifdef CONFIG_PM
  99. .pm = &stmpe_dev_pm_ops,
  100. #endif
  101. .of_match_table = stmpe_of_match,
  102. },
  103. .probe = stmpe_i2c_probe,
  104. .remove = stmpe_i2c_remove,
  105. .id_table = stmpe_i2c_id,
  106. };
  107. static int __init stmpe_init(void)
  108. {
  109. return i2c_add_driver(&stmpe_i2c_driver);
  110. }
  111. subsys_initcall(stmpe_init);
  112. static void __exit stmpe_exit(void)
  113. {
  114. i2c_del_driver(&stmpe_i2c_driver);
  115. }
  116. module_exit(stmpe_exit);
  117. MODULE_LICENSE("GPL v2");
  118. MODULE_DESCRIPTION("STMPE MFD I2C Interface Driver");
  119. MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");