drm_dp_i2c_helper.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright © 2009 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/errno.h>
  27. #include <linux/sched.h>
  28. #include <linux/i2c.h>
  29. #include "drm_dp_helper.h"
  30. #include "drmP.h"
  31. /* Run a single AUX_CH I2C transaction, writing/reading data as necessary */
  32. static int
  33. i2c_algo_dp_aux_transaction(struct i2c_adapter *adapter, int mode,
  34. uint8_t write_byte, uint8_t *read_byte)
  35. {
  36. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  37. int ret;
  38. ret = (*algo_data->aux_ch)(adapter, mode,
  39. write_byte, read_byte);
  40. return ret;
  41. }
  42. /*
  43. * I2C over AUX CH
  44. */
  45. /*
  46. * Send the address. If the I2C link is running, this 'restarts'
  47. * the connection with the new address, this is used for doing
  48. * a write followed by a read (as needed for DDC)
  49. */
  50. static int
  51. i2c_algo_dp_aux_address(struct i2c_adapter *adapter, u16 address, bool reading)
  52. {
  53. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  54. int mode = MODE_I2C_START;
  55. int ret;
  56. if (reading)
  57. mode |= MODE_I2C_READ;
  58. else
  59. mode |= MODE_I2C_WRITE;
  60. algo_data->address = address;
  61. algo_data->running = true;
  62. ret = i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
  63. return ret;
  64. }
  65. /*
  66. * Stop the I2C transaction. This closes out the link, sending
  67. * a bare address packet with the MOT bit turned off
  68. */
  69. static void
  70. i2c_algo_dp_aux_stop(struct i2c_adapter *adapter, bool reading)
  71. {
  72. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  73. int mode = MODE_I2C_STOP;
  74. if (reading)
  75. mode |= MODE_I2C_READ;
  76. else
  77. mode |= MODE_I2C_WRITE;
  78. if (algo_data->running) {
  79. (void) i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
  80. algo_data->running = false;
  81. }
  82. }
  83. /*
  84. * Write a single byte to the current I2C address, the
  85. * the I2C link must be running or this returns -EIO
  86. */
  87. static int
  88. i2c_algo_dp_aux_put_byte(struct i2c_adapter *adapter, u8 byte)
  89. {
  90. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  91. int ret;
  92. if (!algo_data->running)
  93. return -EIO;
  94. ret = i2c_algo_dp_aux_transaction(adapter, MODE_I2C_WRITE, byte, NULL);
  95. return ret;
  96. }
  97. /*
  98. * Read a single byte from the current I2C address, the
  99. * I2C link must be running or this returns -EIO
  100. */
  101. static int
  102. i2c_algo_dp_aux_get_byte(struct i2c_adapter *adapter, u8 *byte_ret)
  103. {
  104. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  105. int ret;
  106. if (!algo_data->running)
  107. return -EIO;
  108. ret = i2c_algo_dp_aux_transaction(adapter, MODE_I2C_READ, 0, byte_ret);
  109. return ret;
  110. }
  111. static int
  112. i2c_algo_dp_aux_xfer(struct i2c_adapter *adapter,
  113. struct i2c_msg *msgs,
  114. int num)
  115. {
  116. int ret = 0;
  117. bool reading = false;
  118. int m;
  119. int b;
  120. for (m = 0; m < num; m++) {
  121. u16 len = msgs[m].len;
  122. u8 *buf = msgs[m].buf;
  123. reading = (msgs[m].flags & I2C_M_RD) != 0;
  124. ret = i2c_algo_dp_aux_address(adapter, msgs[m].addr, reading);
  125. if (ret < 0)
  126. break;
  127. if (reading) {
  128. for (b = 0; b < len; b++) {
  129. ret = i2c_algo_dp_aux_get_byte(adapter, &buf[b]);
  130. if (ret < 0)
  131. break;
  132. }
  133. } else {
  134. for (b = 0; b < len; b++) {
  135. ret = i2c_algo_dp_aux_put_byte(adapter, buf[b]);
  136. if (ret < 0)
  137. break;
  138. }
  139. }
  140. if (ret < 0)
  141. break;
  142. }
  143. if (ret >= 0)
  144. ret = num;
  145. i2c_algo_dp_aux_stop(adapter, reading);
  146. DRM_DEBUG_KMS("dp_aux_xfer return %d\n", ret);
  147. return ret;
  148. }
  149. static u32
  150. i2c_algo_dp_aux_functionality(struct i2c_adapter *adapter)
  151. {
  152. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  153. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  154. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  155. I2C_FUNC_10BIT_ADDR;
  156. }
  157. static const struct i2c_algorithm i2c_dp_aux_algo = {
  158. .master_xfer = i2c_algo_dp_aux_xfer,
  159. .functionality = i2c_algo_dp_aux_functionality,
  160. };
  161. static void
  162. i2c_dp_aux_reset_bus(struct i2c_adapter *adapter)
  163. {
  164. (void) i2c_algo_dp_aux_address(adapter, 0, false);
  165. (void) i2c_algo_dp_aux_stop(adapter, false);
  166. }
  167. static int
  168. i2c_dp_aux_prepare_bus(struct i2c_adapter *adapter)
  169. {
  170. adapter->algo = &i2c_dp_aux_algo;
  171. adapter->retries = 3;
  172. i2c_dp_aux_reset_bus(adapter);
  173. return 0;
  174. }
  175. int
  176. i2c_dp_aux_add_bus(struct i2c_adapter *adapter)
  177. {
  178. int error;
  179. error = i2c_dp_aux_prepare_bus(adapter);
  180. if (error)
  181. return error;
  182. error = i2c_add_adapter(adapter);
  183. return error;
  184. }
  185. EXPORT_SYMBOL(i2c_dp_aux_add_bus);