drm_scdc_helper.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2015 NVIDIA Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the
  12. * next paragraph) shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/slab.h>
  24. #include <drm/drm_scdc_helper.h>
  25. /**
  26. * DOC: scdc helpers
  27. *
  28. * Status and Control Data Channel (SCDC) is a mechanism introduced by the
  29. * HDMI 2.0 specification. It is a point-to-point protocol that allows the
  30. * HDMI source and HDMI sink to exchange data. The same I2C interface that
  31. * is used to access EDID serves as the transport mechanism for SCDC.
  32. */
  33. #define SCDC_I2C_SLAVE_ADDRESS 0x54
  34. /**
  35. * drm_scdc_read - read a block of data from SCDC
  36. * @adapter: I2C controller
  37. * @offset: start offset of block to read
  38. * @buffer: return location for the block to read
  39. * @size: size of the block to read
  40. *
  41. * Reads a block of data from SCDC, starting at a given offset.
  42. *
  43. * Returns:
  44. * 0 on success, negative error code on failure.
  45. */
  46. ssize_t drm_scdc_read(struct i2c_adapter *adapter, u8 offset, void *buffer,
  47. size_t size)
  48. {
  49. int ret;
  50. struct i2c_msg msgs[2] = {
  51. {
  52. .addr = SCDC_I2C_SLAVE_ADDRESS,
  53. .flags = 0,
  54. .len = 1,
  55. .buf = &offset,
  56. }, {
  57. .addr = SCDC_I2C_SLAVE_ADDRESS,
  58. .flags = I2C_M_RD,
  59. .len = size,
  60. .buf = buffer,
  61. }
  62. };
  63. ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
  64. if (ret < 0)
  65. return ret;
  66. if (ret != ARRAY_SIZE(msgs))
  67. return -EPROTO;
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(drm_scdc_read);
  71. /**
  72. * drm_scdc_write - write a block of data to SCDC
  73. * @adapter: I2C controller
  74. * @offset: start offset of block to write
  75. * @buffer: block of data to write
  76. * @size: size of the block to write
  77. *
  78. * Writes a block of data to SCDC, starting at a given offset.
  79. *
  80. * Returns:
  81. * 0 on success, negative error code on failure.
  82. */
  83. ssize_t drm_scdc_write(struct i2c_adapter *adapter, u8 offset,
  84. const void *buffer, size_t size)
  85. {
  86. struct i2c_msg msg = {
  87. .addr = SCDC_I2C_SLAVE_ADDRESS,
  88. .flags = 0,
  89. .len = 1 + size,
  90. .buf = NULL,
  91. };
  92. void *data;
  93. int err;
  94. data = kmalloc(1 + size, GFP_TEMPORARY);
  95. if (!data)
  96. return -ENOMEM;
  97. msg.buf = data;
  98. memcpy(data, &offset, sizeof(offset));
  99. memcpy(data + 1, buffer, size);
  100. err = i2c_transfer(adapter, &msg, 1);
  101. kfree(data);
  102. if (err < 0)
  103. return err;
  104. if (err != 1)
  105. return -EPROTO;
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(drm_scdc_write);