radeon_i2c.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_connector *radeon_connector, int lock_state)
  58. {
  59. struct radeon_device *rdev = radeon_connector->base.dev->dev_private;
  60. uint32_t temp;
  61. struct radeon_i2c_bus_rec *rec = &radeon_connector->ddc_bus->rec;
  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. if (lock_state) {
  76. temp = RREG32(rec->a_clk_reg);
  77. temp &= ~(rec->a_clk_mask);
  78. WREG32(rec->a_clk_reg, temp);
  79. temp = RREG32(rec->a_data_reg);
  80. temp &= ~(rec->a_data_mask);
  81. WREG32(rec->a_data_reg, temp);
  82. }
  83. temp = RREG32(rec->mask_clk_reg);
  84. if (lock_state)
  85. temp |= rec->mask_clk_mask;
  86. else
  87. temp &= ~rec->mask_clk_mask;
  88. WREG32(rec->mask_clk_reg, temp);
  89. temp = RREG32(rec->mask_clk_reg);
  90. temp = RREG32(rec->mask_data_reg);
  91. if (lock_state)
  92. temp |= rec->mask_data_mask;
  93. else
  94. temp &= ~rec->mask_data_mask;
  95. WREG32(rec->mask_data_reg, temp);
  96. temp = RREG32(rec->mask_data_reg);
  97. }
  98. static int get_clock(void *i2c_priv)
  99. {
  100. struct radeon_i2c_chan *i2c = i2c_priv;
  101. struct radeon_device *rdev = i2c->dev->dev_private;
  102. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  103. uint32_t val;
  104. val = RREG32(rec->get_clk_reg);
  105. val &= rec->get_clk_mask;
  106. return (val != 0);
  107. }
  108. static int get_data(void *i2c_priv)
  109. {
  110. struct radeon_i2c_chan *i2c = i2c_priv;
  111. struct radeon_device *rdev = i2c->dev->dev_private;
  112. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  113. uint32_t val;
  114. val = RREG32(rec->get_data_reg);
  115. val &= rec->get_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. val = RREG32(rec->put_clk_reg) & (uint32_t)~(rec->put_clk_mask);
  125. val |= clock ? 0 : rec->put_clk_mask;
  126. WREG32(rec->put_clk_reg, val);
  127. }
  128. static void set_data(void *i2c_priv, int data)
  129. {
  130. struct radeon_i2c_chan *i2c = i2c_priv;
  131. struct radeon_device *rdev = i2c->dev->dev_private;
  132. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  133. uint32_t val;
  134. val = RREG32(rec->put_data_reg) & (uint32_t)~(rec->put_data_mask);
  135. val |= data ? 0 : rec->put_data_mask;
  136. WREG32(rec->put_data_reg, val);
  137. }
  138. struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
  139. struct radeon_i2c_bus_rec *rec,
  140. const char *name)
  141. {
  142. struct radeon_i2c_chan *i2c;
  143. int ret;
  144. i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
  145. if (i2c == NULL)
  146. return NULL;
  147. i2c->adapter.owner = THIS_MODULE;
  148. i2c->adapter.algo_data = &i2c->algo;
  149. i2c->dev = dev;
  150. i2c->algo.setsda = set_data;
  151. i2c->algo.setscl = set_clock;
  152. i2c->algo.getsda = get_data;
  153. i2c->algo.getscl = get_clock;
  154. i2c->algo.udelay = 20;
  155. /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
  156. * make this, 2 jiffies is a lot more reliable */
  157. i2c->algo.timeout = 2;
  158. i2c->algo.data = i2c;
  159. i2c->rec = *rec;
  160. i2c_set_adapdata(&i2c->adapter, i2c);
  161. ret = i2c_bit_add_bus(&i2c->adapter);
  162. if (ret) {
  163. DRM_INFO("Failed to register i2c %s\n", name);
  164. goto out_free;
  165. }
  166. return i2c;
  167. out_free:
  168. kfree(i2c);
  169. return NULL;
  170. }
  171. void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
  172. {
  173. if (!i2c)
  174. return;
  175. i2c_del_adapter(&i2c->adapter);
  176. kfree(i2c);
  177. }
  178. struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
  179. {
  180. return NULL;
  181. }