ad714x-i2c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AD714X CapTouch Programmable Controller driver (I2C bus)
  4. *
  5. * Copyright 2009-2011 Analog Devices Inc.
  6. */
  7. #include <linux/input.h> /* BUS_I2C */
  8. #include <linux/i2c.h>
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/pm.h>
  12. #include "ad714x.h"
  13. static int __maybe_unused ad714x_i2c_suspend(struct device *dev)
  14. {
  15. return ad714x_disable(i2c_get_clientdata(to_i2c_client(dev)));
  16. }
  17. static int __maybe_unused ad714x_i2c_resume(struct device *dev)
  18. {
  19. return ad714x_enable(i2c_get_clientdata(to_i2c_client(dev)));
  20. }
  21. static SIMPLE_DEV_PM_OPS(ad714x_i2c_pm, ad714x_i2c_suspend, ad714x_i2c_resume);
  22. static int ad714x_i2c_write(struct ad714x_chip *chip,
  23. unsigned short reg, unsigned short data)
  24. {
  25. struct i2c_client *client = to_i2c_client(chip->dev);
  26. int error;
  27. chip->xfer_buf[0] = cpu_to_be16(reg);
  28. chip->xfer_buf[1] = cpu_to_be16(data);
  29. error = i2c_master_send(client, (u8 *)chip->xfer_buf,
  30. 2 * sizeof(*chip->xfer_buf));
  31. if (unlikely(error < 0)) {
  32. dev_err(&client->dev, "I2C write error: %d\n", error);
  33. return error;
  34. }
  35. return 0;
  36. }
  37. static int ad714x_i2c_read(struct ad714x_chip *chip,
  38. unsigned short reg, unsigned short *data, size_t len)
  39. {
  40. struct i2c_client *client = to_i2c_client(chip->dev);
  41. int i;
  42. int error;
  43. chip->xfer_buf[0] = cpu_to_be16(reg);
  44. error = i2c_master_send(client, (u8 *)chip->xfer_buf,
  45. sizeof(*chip->xfer_buf));
  46. if (error >= 0)
  47. error = i2c_master_recv(client, (u8 *)chip->xfer_buf,
  48. len * sizeof(*chip->xfer_buf));
  49. if (unlikely(error < 0)) {
  50. dev_err(&client->dev, "I2C read error: %d\n", error);
  51. return error;
  52. }
  53. for (i = 0; i < len; i++)
  54. data[i] = be16_to_cpu(chip->xfer_buf[i]);
  55. return 0;
  56. }
  57. static int ad714x_i2c_probe(struct i2c_client *client,
  58. const struct i2c_device_id *id)
  59. {
  60. struct ad714x_chip *chip;
  61. chip = ad714x_probe(&client->dev, BUS_I2C, client->irq,
  62. ad714x_i2c_read, ad714x_i2c_write);
  63. if (IS_ERR(chip))
  64. return PTR_ERR(chip);
  65. i2c_set_clientdata(client, chip);
  66. return 0;
  67. }
  68. static const struct i2c_device_id ad714x_id[] = {
  69. { "ad7142_captouch", 0 },
  70. { "ad7143_captouch", 0 },
  71. { "ad7147_captouch", 0 },
  72. { "ad7147a_captouch", 0 },
  73. { "ad7148_captouch", 0 },
  74. { }
  75. };
  76. MODULE_DEVICE_TABLE(i2c, ad714x_id);
  77. static struct i2c_driver ad714x_i2c_driver = {
  78. .driver = {
  79. .name = "ad714x_captouch",
  80. .pm = &ad714x_i2c_pm,
  81. },
  82. .probe = ad714x_i2c_probe,
  83. .id_table = ad714x_id,
  84. };
  85. module_i2c_driver(ad714x_i2c_driver);
  86. MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor I2C Bus Driver");
  87. MODULE_AUTHOR("Barry Song <[email protected]>");
  88. MODULE_LICENSE("GPL");