radeon_i2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions 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 NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include "drmP.h"
  27. #include "radeon_drm.h"
  28. #include "radeon.h"
  29. /**
  30. * radeon_ddc_probe
  31. *
  32. */
  33. bool radeon_ddc_probe(struct radeon_connector *radeon_connector)
  34. {
  35. u8 out_buf[] = { 0x0, 0x0};
  36. u8 buf[2];
  37. int ret;
  38. struct i2c_msg msgs[] = {
  39. {
  40. .addr = 0x50,
  41. .flags = 0,
  42. .len = 1,
  43. .buf = out_buf,
  44. },
  45. {
  46. .addr = 0x50,
  47. .flags = I2C_M_RD,
  48. .len = 1,
  49. .buf = buf,
  50. }
  51. };
  52. ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
  53. if (ret == 2)
  54. return true;
  55. return false;
  56. }
  57. void radeon_i2c_do_lock(struct radeon_i2c_chan *i2c, int lock_state)
  58. {
  59. struct radeon_device *rdev = i2c->dev->dev_private;
  60. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  61. uint32_t temp;
  62. /* RV410 appears to have a bug where the hw i2c in reset
  63. * holds the i2c port in a bad state - switch hw i2c away before
  64. * doing DDC - do this for all r200s/r300s/r400s for safety sake
  65. */
  66. if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
  67. if (rec->a_clk_reg == RADEON_GPIO_MONID) {
  68. WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
  69. R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
  70. } else {
  71. WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
  72. R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
  73. }
  74. }
  75. /* clear the output pin values */
  76. temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
  77. WREG32(rec->a_clk_reg, temp);
  78. temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
  79. WREG32(rec->a_data_reg, temp);
  80. /* mask the gpio pins for software use */
  81. temp = RREG32(rec->mask_clk_reg);
  82. if (lock_state)
  83. temp |= rec->mask_clk_mask;
  84. else
  85. temp &= ~rec->mask_clk_mask;
  86. WREG32(rec->mask_clk_reg, temp);
  87. temp = RREG32(rec->mask_clk_reg);
  88. temp = RREG32(rec->mask_data_reg);
  89. if (lock_state)
  90. temp |= rec->mask_data_mask;
  91. else
  92. temp &= ~rec->mask_data_mask;
  93. WREG32(rec->mask_data_reg, temp);
  94. temp = RREG32(rec->mask_data_reg);
  95. }
  96. static int get_clock(void *i2c_priv)
  97. {
  98. struct radeon_i2c_chan *i2c = i2c_priv;
  99. struct radeon_device *rdev = i2c->dev->dev_private;
  100. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  101. uint32_t val;
  102. /* read the value off the pin */
  103. val = RREG32(rec->y_clk_reg);
  104. val &= rec->y_clk_mask;
  105. return (val != 0);
  106. }
  107. static int get_data(void *i2c_priv)
  108. {
  109. struct radeon_i2c_chan *i2c = i2c_priv;
  110. struct radeon_device *rdev = i2c->dev->dev_private;
  111. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  112. uint32_t val;
  113. /* read the value off the pin */
  114. val = RREG32(rec->y_data_reg);
  115. val &= rec->y_data_mask;
  116. return (val != 0);
  117. }
  118. static void set_clock(void *i2c_priv, int clock)
  119. {
  120. struct radeon_i2c_chan *i2c = i2c_priv;
  121. struct radeon_device *rdev = i2c->dev->dev_private;
  122. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  123. uint32_t val;
  124. /* set pin direction */
  125. val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
  126. val |= clock ? 0 : rec->en_clk_mask;
  127. WREG32(rec->en_clk_reg, val);
  128. }
  129. static void set_data(void *i2c_priv, int data)
  130. {
  131. struct radeon_i2c_chan *i2c = i2c_priv;
  132. struct radeon_device *rdev = i2c->dev->dev_private;
  133. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  134. uint32_t val;
  135. /* set pin direction */
  136. val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
  137. val |= data ? 0 : rec->en_data_mask;
  138. WREG32(rec->en_data_reg, val);
  139. }
  140. struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
  141. struct radeon_i2c_bus_rec *rec,
  142. const char *name)
  143. {
  144. struct radeon_i2c_chan *i2c;
  145. int ret;
  146. i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
  147. if (i2c == NULL)
  148. return NULL;
  149. i2c->adapter.owner = THIS_MODULE;
  150. i2c->adapter.algo_data = &i2c->algo;
  151. i2c->dev = dev;
  152. i2c->algo.setsda = set_data;
  153. i2c->algo.setscl = set_clock;
  154. i2c->algo.getsda = get_data;
  155. i2c->algo.getscl = get_clock;
  156. i2c->algo.udelay = 20;
  157. /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
  158. * make this, 2 jiffies is a lot more reliable */
  159. i2c->algo.timeout = 2;
  160. i2c->algo.data = i2c;
  161. i2c->rec = *rec;
  162. i2c_set_adapdata(&i2c->adapter, i2c);
  163. ret = i2c_bit_add_bus(&i2c->adapter);
  164. if (ret) {
  165. DRM_INFO("Failed to register i2c %s\n", name);
  166. goto out_free;
  167. }
  168. return i2c;
  169. out_free:
  170. kfree(i2c);
  171. return NULL;
  172. }
  173. void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
  174. {
  175. if (!i2c)
  176. return;
  177. i2c_del_adapter(&i2c->adapter);
  178. kfree(i2c);
  179. }
  180. struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
  181. {
  182. return NULL;
  183. }
  184. void radeon_i2c_sw_get_byte(struct radeon_i2c_chan *i2c_bus,
  185. u8 slave_addr,
  186. u8 addr,
  187. u8 *val)
  188. {
  189. u8 out_buf[2];
  190. u8 in_buf[2];
  191. struct i2c_msg msgs[] = {
  192. {
  193. .addr = slave_addr,
  194. .flags = 0,
  195. .len = 1,
  196. .buf = out_buf,
  197. },
  198. {
  199. .addr = slave_addr,
  200. .flags = I2C_M_RD,
  201. .len = 1,
  202. .buf = in_buf,
  203. }
  204. };
  205. out_buf[0] = addr;
  206. out_buf[1] = 0;
  207. if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
  208. *val = in_buf[0];
  209. DRM_DEBUG("val = 0x%02x\n", *val);
  210. } else {
  211. DRM_ERROR("i2c 0x%02x 0x%02x read failed\n",
  212. addr, *val);
  213. }
  214. }
  215. void radeon_i2c_sw_put_byte(struct radeon_i2c_chan *i2c_bus,
  216. u8 slave_addr,
  217. u8 addr,
  218. u8 val)
  219. {
  220. uint8_t out_buf[2];
  221. struct i2c_msg msg = {
  222. .addr = slave_addr,
  223. .flags = 0,
  224. .len = 2,
  225. .buf = out_buf,
  226. };
  227. out_buf[0] = addr;
  228. out_buf[1] = val;
  229. if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
  230. DRM_ERROR("i2c 0x%02x 0x%02x write failed\n",
  231. addr, val);
  232. }