radeon_i2c.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 (rec->hw_capable) {
  67. if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
  68. if (rec->a_clk_reg == RADEON_GPIO_MONID) {
  69. WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
  70. R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
  71. } else {
  72. WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
  73. R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
  74. }
  75. }
  76. }
  77. /* clear the output pin values */
  78. temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
  79. WREG32(rec->a_clk_reg, temp);
  80. temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
  81. WREG32(rec->a_data_reg, temp);
  82. /* set the pins to input */
  83. temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
  84. WREG32(rec->en_clk_reg, temp);
  85. temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
  86. WREG32(rec->en_data_reg, temp);
  87. /* mask the gpio pins for software use */
  88. temp = RREG32(rec->mask_clk_reg);
  89. if (lock_state)
  90. temp |= rec->mask_clk_mask;
  91. else
  92. temp &= ~rec->mask_clk_mask;
  93. WREG32(rec->mask_clk_reg, temp);
  94. temp = RREG32(rec->mask_clk_reg);
  95. temp = RREG32(rec->mask_data_reg);
  96. if (lock_state)
  97. temp |= rec->mask_data_mask;
  98. else
  99. temp &= ~rec->mask_data_mask;
  100. WREG32(rec->mask_data_reg, temp);
  101. temp = RREG32(rec->mask_data_reg);
  102. }
  103. static int get_clock(void *i2c_priv)
  104. {
  105. struct radeon_i2c_chan *i2c = i2c_priv;
  106. struct radeon_device *rdev = i2c->dev->dev_private;
  107. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  108. uint32_t val;
  109. /* read the value off the pin */
  110. val = RREG32(rec->y_clk_reg);
  111. val &= rec->y_clk_mask;
  112. return (val != 0);
  113. }
  114. static int get_data(void *i2c_priv)
  115. {
  116. struct radeon_i2c_chan *i2c = i2c_priv;
  117. struct radeon_device *rdev = i2c->dev->dev_private;
  118. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  119. uint32_t val;
  120. /* read the value off the pin */
  121. val = RREG32(rec->y_data_reg);
  122. val &= rec->y_data_mask;
  123. return (val != 0);
  124. }
  125. static void set_clock(void *i2c_priv, int clock)
  126. {
  127. struct radeon_i2c_chan *i2c = i2c_priv;
  128. struct radeon_device *rdev = i2c->dev->dev_private;
  129. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  130. uint32_t val;
  131. /* set pin direction */
  132. val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
  133. val |= clock ? 0 : rec->en_clk_mask;
  134. WREG32(rec->en_clk_reg, val);
  135. }
  136. static void set_data(void *i2c_priv, int data)
  137. {
  138. struct radeon_i2c_chan *i2c = i2c_priv;
  139. struct radeon_device *rdev = i2c->dev->dev_private;
  140. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  141. uint32_t val;
  142. /* set pin direction */
  143. val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
  144. val |= data ? 0 : rec->en_data_mask;
  145. WREG32(rec->en_data_reg, val);
  146. }
  147. struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
  148. struct radeon_i2c_bus_rec *rec,
  149. const char *name)
  150. {
  151. struct radeon_i2c_chan *i2c;
  152. int ret;
  153. i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
  154. if (i2c == NULL)
  155. return NULL;
  156. i2c->adapter.owner = THIS_MODULE;
  157. i2c->dev = dev;
  158. i2c_set_adapdata(&i2c->adapter, i2c);
  159. i2c->adapter.algo_data = &i2c->algo.bit;
  160. i2c->algo.bit.setsda = set_data;
  161. i2c->algo.bit.setscl = set_clock;
  162. i2c->algo.bit.getsda = get_data;
  163. i2c->algo.bit.getscl = get_clock;
  164. i2c->algo.bit.udelay = 20;
  165. /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
  166. * make this, 2 jiffies is a lot more reliable */
  167. i2c->algo.bit.timeout = 2;
  168. i2c->algo.bit.data = i2c;
  169. i2c->rec = *rec;
  170. ret = i2c_bit_add_bus(&i2c->adapter);
  171. if (ret) {
  172. DRM_INFO("Failed to register i2c %s\n", name);
  173. goto out_free;
  174. }
  175. return i2c;
  176. out_free:
  177. kfree(i2c);
  178. return NULL;
  179. }
  180. struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
  181. struct radeon_i2c_bus_rec *rec,
  182. const char *name)
  183. {
  184. struct radeon_i2c_chan *i2c;
  185. int ret;
  186. i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
  187. if (i2c == NULL)
  188. return NULL;
  189. i2c->rec = *rec;
  190. i2c->adapter.owner = THIS_MODULE;
  191. i2c->dev = dev;
  192. i2c_set_adapdata(&i2c->adapter, i2c);
  193. i2c->adapter.algo_data = &i2c->algo.dp;
  194. i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
  195. i2c->algo.dp.address = 0;
  196. ret = i2c_dp_aux_add_bus(&i2c->adapter);
  197. if (ret) {
  198. DRM_INFO("Failed to register i2c %s\n", name);
  199. goto out_free;
  200. }
  201. return i2c;
  202. out_free:
  203. kfree(i2c);
  204. return NULL;
  205. }
  206. void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
  207. {
  208. if (!i2c)
  209. return;
  210. i2c_del_adapter(&i2c->adapter);
  211. kfree(i2c);
  212. }
  213. struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
  214. {
  215. return NULL;
  216. }
  217. void radeon_i2c_sw_get_byte(struct radeon_i2c_chan *i2c_bus,
  218. u8 slave_addr,
  219. u8 addr,
  220. u8 *val)
  221. {
  222. u8 out_buf[2];
  223. u8 in_buf[2];
  224. struct i2c_msg msgs[] = {
  225. {
  226. .addr = slave_addr,
  227. .flags = 0,
  228. .len = 1,
  229. .buf = out_buf,
  230. },
  231. {
  232. .addr = slave_addr,
  233. .flags = I2C_M_RD,
  234. .len = 1,
  235. .buf = in_buf,
  236. }
  237. };
  238. out_buf[0] = addr;
  239. out_buf[1] = 0;
  240. if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
  241. *val = in_buf[0];
  242. DRM_DEBUG("val = 0x%02x\n", *val);
  243. } else {
  244. DRM_ERROR("i2c 0x%02x 0x%02x read failed\n",
  245. addr, *val);
  246. }
  247. }
  248. void radeon_i2c_sw_put_byte(struct radeon_i2c_chan *i2c_bus,
  249. u8 slave_addr,
  250. u8 addr,
  251. u8 val)
  252. {
  253. uint8_t out_buf[2];
  254. struct i2c_msg msg = {
  255. .addr = slave_addr,
  256. .flags = 0,
  257. .len = 2,
  258. .buf = out_buf,
  259. };
  260. out_buf[0] = addr;
  261. out_buf[1] = val;
  262. if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
  263. DRM_ERROR("i2c 0x%02x 0x%02x write failed\n",
  264. addr, val);
  265. }