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/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/errno.h>
  28. #include <linux/sched.h>
  29. #include <linux/i2c.h>
  30. #include "drm_dp_helper.h"
  31. #include "drmP.h"
  32. /* Run a single AUX_CH I2C transaction, writing/reading data as necessary */
  33. static int
  34. i2c_algo_dp_aux_transaction(struct i2c_adapter *adapter, int mode,
  35. uint8_t write_byte, uint8_t *read_byte)
  36. {
  37. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  38. int ret;
  39. ret = (*algo_data->aux_ch)(adapter, mode,
  40. write_byte, read_byte);
  41. return ret;
  42. }
  43. /*
  44. * I2C over AUX CH
  45. */
  46. /*
  47. * Send the address. If the I2C link is running, this 'restarts'
  48. * the connection with the new address, this is used for doing
  49. * a write followed by a read (as needed for DDC)
  50. */
  51. static int
  52. i2c_algo_dp_aux_address(struct i2c_adapter *adapter, u16 address, bool reading)
  53. {
  54. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  55. int mode = MODE_I2C_START;
  56. int ret;
  57. if (reading)
  58. mode |= MODE_I2C_READ;
  59. else
  60. mode |= MODE_I2C_WRITE;
  61. algo_data->address = address;
  62. algo_data->running = true;
  63. ret = i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
  64. return ret;
  65. }
  66. /*
  67. * Stop the I2C transaction. This closes out the link, sending
  68. * a bare address packet with the MOT bit turned off
  69. */
  70. static void
  71. i2c_algo_dp_aux_stop(struct i2c_adapter *adapter, bool reading)
  72. {
  73. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  74. int mode = MODE_I2C_STOP;
  75. if (reading)
  76. mode |= MODE_I2C_READ;
  77. else
  78. mode |= MODE_I2C_WRITE;
  79. if (algo_data->running) {
  80. (void) i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
  81. algo_data->running = false;
  82. }
  83. }
  84. /*
  85. * Write a single byte to the current I2C address, the
  86. * the I2C link must be running or this returns -EIO
  87. */
  88. static int
  89. i2c_algo_dp_aux_put_byte(struct i2c_adapter *adapter, u8 byte)
  90. {
  91. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  92. int ret;
  93. if (!algo_data->running)
  94. return -EIO;
  95. ret = i2c_algo_dp_aux_transaction(adapter, MODE_I2C_WRITE, byte, NULL);
  96. return ret;
  97. }
  98. /*
  99. * Read a single byte from the current I2C address, the
  100. * I2C link must be running or this returns -EIO
  101. */
  102. static int
  103. i2c_algo_dp_aux_get_byte(struct i2c_adapter *adapter, u8 *byte_ret)
  104. {
  105. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  106. int ret;
  107. if (!algo_data->running)
  108. return -EIO;
  109. ret = i2c_algo_dp_aux_transaction(adapter, MODE_I2C_READ, 0, byte_ret);
  110. return ret;
  111. }
  112. static int
  113. i2c_algo_dp_aux_xfer(struct i2c_adapter *adapter,
  114. struct i2c_msg *msgs,
  115. int num)
  116. {
  117. int ret = 0;
  118. bool reading = false;
  119. int m;
  120. int b;
  121. for (m = 0; m < num; m++) {
  122. u16 len = msgs[m].len;
  123. u8 *buf = msgs[m].buf;
  124. reading = (msgs[m].flags & I2C_M_RD) != 0;
  125. ret = i2c_algo_dp_aux_address(adapter, msgs[m].addr, reading);
  126. if (ret < 0)
  127. break;
  128. if (reading) {
  129. for (b = 0; b < len; b++) {
  130. ret = i2c_algo_dp_aux_get_byte(adapter, &buf[b]);
  131. if (ret < 0)
  132. break;
  133. }
  134. } else {
  135. for (b = 0; b < len; b++) {
  136. ret = i2c_algo_dp_aux_put_byte(adapter, buf[b]);
  137. if (ret < 0)
  138. break;
  139. }
  140. }
  141. if (ret < 0)
  142. break;
  143. }
  144. if (ret >= 0)
  145. ret = num;
  146. i2c_algo_dp_aux_stop(adapter, reading);
  147. DRM_DEBUG_KMS("dp_aux_xfer return %d\n", ret);
  148. return ret;
  149. }
  150. static u32
  151. i2c_algo_dp_aux_functionality(struct i2c_adapter *adapter)
  152. {
  153. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  154. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  155. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  156. I2C_FUNC_10BIT_ADDR;
  157. }
  158. static const struct i2c_algorithm i2c_dp_aux_algo = {
  159. .master_xfer = i2c_algo_dp_aux_xfer,
  160. .functionality = i2c_algo_dp_aux_functionality,
  161. };
  162. static void
  163. i2c_dp_aux_reset_bus(struct i2c_adapter *adapter)
  164. {
  165. (void) i2c_algo_dp_aux_address(adapter, 0, false);
  166. (void) i2c_algo_dp_aux_stop(adapter, false);
  167. }
  168. static int
  169. i2c_dp_aux_prepare_bus(struct i2c_adapter *adapter)
  170. {
  171. adapter->algo = &i2c_dp_aux_algo;
  172. adapter->retries = 3;
  173. i2c_dp_aux_reset_bus(adapter);
  174. return 0;
  175. }
  176. int
  177. i2c_dp_aux_add_bus(struct i2c_adapter *adapter)
  178. {
  179. int error;
  180. error = i2c_dp_aux_prepare_bus(adapter);
  181. if (error)
  182. return error;
  183. error = i2c_add_adapter(adapter);
  184. return error;
  185. }
  186. EXPORT_SYMBOL(i2c_dp_aux_add_bus);