ad714x-i2c.c 2.6 KB

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