bit.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  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, sublicense,
  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 shall be included in
  12. * all copies or substantial busions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "bus.h"
  25. #ifdef CONFIG_NOUVEAU_I2C_INTERNAL
  26. #define T_TIMEOUT 2200000
  27. #define T_RISEFALL 1000
  28. #define T_HOLD 5000
  29. static inline void
  30. nvkm_i2c_drive_scl(struct nvkm_i2c_bus *bus, int state)
  31. {
  32. bus->func->drive_scl(bus, state);
  33. }
  34. static inline void
  35. nvkm_i2c_drive_sda(struct nvkm_i2c_bus *bus, int state)
  36. {
  37. bus->func->drive_sda(bus, state);
  38. }
  39. static inline int
  40. nvkm_i2c_sense_scl(struct nvkm_i2c_bus *bus)
  41. {
  42. return bus->func->sense_scl(bus);
  43. }
  44. static inline int
  45. nvkm_i2c_sense_sda(struct nvkm_i2c_bus *bus)
  46. {
  47. return bus->func->sense_sda(bus);
  48. }
  49. static void
  50. nvkm_i2c_delay(struct nvkm_i2c_bus *bus, u32 nsec)
  51. {
  52. udelay((nsec + 500) / 1000);
  53. }
  54. static bool
  55. nvkm_i2c_raise_scl(struct nvkm_i2c_bus *bus)
  56. {
  57. u32 timeout = T_TIMEOUT / T_RISEFALL;
  58. nvkm_i2c_drive_scl(bus, 1);
  59. do {
  60. nvkm_i2c_delay(bus, T_RISEFALL);
  61. } while (!nvkm_i2c_sense_scl(bus) && --timeout);
  62. return timeout != 0;
  63. }
  64. static int
  65. i2c_start(struct nvkm_i2c_bus *bus)
  66. {
  67. int ret = 0;
  68. if (!nvkm_i2c_sense_scl(bus) ||
  69. !nvkm_i2c_sense_sda(bus)) {
  70. nvkm_i2c_drive_scl(bus, 0);
  71. nvkm_i2c_drive_sda(bus, 1);
  72. if (!nvkm_i2c_raise_scl(bus))
  73. ret = -EBUSY;
  74. }
  75. nvkm_i2c_drive_sda(bus, 0);
  76. nvkm_i2c_delay(bus, T_HOLD);
  77. nvkm_i2c_drive_scl(bus, 0);
  78. nvkm_i2c_delay(bus, T_HOLD);
  79. return ret;
  80. }
  81. static void
  82. i2c_stop(struct nvkm_i2c_bus *bus)
  83. {
  84. nvkm_i2c_drive_scl(bus, 0);
  85. nvkm_i2c_drive_sda(bus, 0);
  86. nvkm_i2c_delay(bus, T_RISEFALL);
  87. nvkm_i2c_drive_scl(bus, 1);
  88. nvkm_i2c_delay(bus, T_HOLD);
  89. nvkm_i2c_drive_sda(bus, 1);
  90. nvkm_i2c_delay(bus, T_HOLD);
  91. }
  92. static int
  93. i2c_bitw(struct nvkm_i2c_bus *bus, int sda)
  94. {
  95. nvkm_i2c_drive_sda(bus, sda);
  96. nvkm_i2c_delay(bus, T_RISEFALL);
  97. if (!nvkm_i2c_raise_scl(bus))
  98. return -ETIMEDOUT;
  99. nvkm_i2c_delay(bus, T_HOLD);
  100. nvkm_i2c_drive_scl(bus, 0);
  101. nvkm_i2c_delay(bus, T_HOLD);
  102. return 0;
  103. }
  104. static int
  105. i2c_bitr(struct nvkm_i2c_bus *bus)
  106. {
  107. int sda;
  108. nvkm_i2c_drive_sda(bus, 1);
  109. nvkm_i2c_delay(bus, T_RISEFALL);
  110. if (!nvkm_i2c_raise_scl(bus))
  111. return -ETIMEDOUT;
  112. nvkm_i2c_delay(bus, T_HOLD);
  113. sda = nvkm_i2c_sense_sda(bus);
  114. nvkm_i2c_drive_scl(bus, 0);
  115. nvkm_i2c_delay(bus, T_HOLD);
  116. return sda;
  117. }
  118. static int
  119. nvkm_i2c_get_byte(struct nvkm_i2c_bus *bus, u8 *byte, bool last)
  120. {
  121. int i, bit;
  122. *byte = 0;
  123. for (i = 7; i >= 0; i--) {
  124. bit = i2c_bitr(bus);
  125. if (bit < 0)
  126. return bit;
  127. *byte |= bit << i;
  128. }
  129. return i2c_bitw(bus, last ? 1 : 0);
  130. }
  131. static int
  132. nvkm_i2c_put_byte(struct nvkm_i2c_bus *bus, u8 byte)
  133. {
  134. int i, ret;
  135. for (i = 7; i >= 0; i--) {
  136. ret = i2c_bitw(bus, !!(byte & (1 << i)));
  137. if (ret < 0)
  138. return ret;
  139. }
  140. ret = i2c_bitr(bus);
  141. if (ret == 1) /* nack */
  142. ret = -EIO;
  143. return ret;
  144. }
  145. static int
  146. i2c_addr(struct nvkm_i2c_bus *bus, struct i2c_msg *msg)
  147. {
  148. u32 addr = msg->addr << 1;
  149. if (msg->flags & I2C_M_RD)
  150. addr |= 1;
  151. return nvkm_i2c_put_byte(bus, addr);
  152. }
  153. int
  154. nvkm_i2c_bit_xfer(struct nvkm_i2c_bus *bus, struct i2c_msg *msgs, int num)
  155. {
  156. struct i2c_msg *msg = msgs;
  157. int ret = 0, mcnt = num;
  158. while (!ret && mcnt--) {
  159. u8 remaining = msg->len;
  160. u8 *ptr = msg->buf;
  161. ret = i2c_start(bus);
  162. if (ret == 0)
  163. ret = i2c_addr(bus, msg);
  164. if (msg->flags & I2C_M_RD) {
  165. while (!ret && remaining--)
  166. ret = nvkm_i2c_get_byte(bus, ptr++, !remaining);
  167. } else {
  168. while (!ret && remaining--)
  169. ret = nvkm_i2c_put_byte(bus, *ptr++);
  170. }
  171. msg++;
  172. }
  173. i2c_stop(bus);
  174. return (ret < 0) ? ret : num;
  175. }
  176. #else
  177. int
  178. nvkm_i2c_bit_xfer(struct nvkm_i2c_bus *bus, struct i2c_msg *msgs, int num)
  179. {
  180. return -ENODEV;
  181. }
  182. #endif