cyttsp_i2c_common.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cyttsp_i2c_common.c
  4. * Cypress TrueTouch(TM) Standard Product (TTSP) I2C touchscreen driver.
  5. * For use with Cypress Txx3xx and Txx4xx parts.
  6. * Supported parts include:
  7. * CY8CTST341
  8. * CY8CTMA340
  9. * TMA4XX
  10. * TMA1036
  11. *
  12. * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
  13. * Copyright (C) 2012 Javier Martinez Canillas <[email protected]>
  14. *
  15. * Contact Cypress Semiconductor at www.cypress.com <[email protected]>
  16. */
  17. #include <linux/device.h>
  18. #include <linux/export.h>
  19. #include <linux/i2c.h>
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include "cyttsp4_core.h"
  23. int cyttsp_i2c_read_block_data(struct device *dev, u8 *xfer_buf,
  24. u16 addr, u8 length, void *values)
  25. {
  26. struct i2c_client *client = to_i2c_client(dev);
  27. u8 client_addr = client->addr | ((addr >> 8) & 0x1);
  28. u8 addr_lo = addr & 0xFF;
  29. struct i2c_msg msgs[] = {
  30. {
  31. .addr = client_addr,
  32. .flags = 0,
  33. .len = 1,
  34. .buf = &addr_lo,
  35. },
  36. {
  37. .addr = client_addr,
  38. .flags = I2C_M_RD,
  39. .len = length,
  40. .buf = values,
  41. },
  42. };
  43. int retval;
  44. retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  45. if (retval < 0)
  46. return retval;
  47. return retval != ARRAY_SIZE(msgs) ? -EIO : 0;
  48. }
  49. EXPORT_SYMBOL_GPL(cyttsp_i2c_read_block_data);
  50. int cyttsp_i2c_write_block_data(struct device *dev, u8 *xfer_buf,
  51. u16 addr, u8 length, const void *values)
  52. {
  53. struct i2c_client *client = to_i2c_client(dev);
  54. u8 client_addr = client->addr | ((addr >> 8) & 0x1);
  55. u8 addr_lo = addr & 0xFF;
  56. struct i2c_msg msgs[] = {
  57. {
  58. .addr = client_addr,
  59. .flags = 0,
  60. .len = length + 1,
  61. .buf = xfer_buf,
  62. },
  63. };
  64. int retval;
  65. xfer_buf[0] = addr_lo;
  66. memcpy(&xfer_buf[1], values, length);
  67. retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  68. if (retval < 0)
  69. return retval;
  70. return retval != ARRAY_SIZE(msgs) ? -EIO : 0;
  71. }
  72. EXPORT_SYMBOL_GPL(cyttsp_i2c_write_block_data);
  73. MODULE_LICENSE("GPL");
  74. MODULE_AUTHOR("Cypress");