radeon_i2c.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/sched.h>
  4. #include <linux/delay.h>
  5. #include <linux/pci.h>
  6. #include <linux/fb.h>
  7. #include <linux/i2c.h>
  8. #include <linux/i2c-id.h>
  9. #include <linux/i2c-algo-bit.h>
  10. #include <asm/io.h>
  11. #include <video/radeon.h>
  12. #include "radeonfb.h"
  13. #include "../edid.h"
  14. #define RADEON_DDC 0x50
  15. static void radeon_gpio_setscl(void* data, int state)
  16. {
  17. struct radeon_i2c_chan *chan = data;
  18. struct radeonfb_info *rinfo = chan->rinfo;
  19. u32 val;
  20. val = INREG(chan->ddc_reg) & ~(VGA_DDC_CLK_OUT_EN);
  21. if (!state)
  22. val |= VGA_DDC_CLK_OUT_EN;
  23. OUTREG(chan->ddc_reg, val);
  24. (void)INREG(chan->ddc_reg);
  25. }
  26. static void radeon_gpio_setsda(void* data, int state)
  27. {
  28. struct radeon_i2c_chan *chan = data;
  29. struct radeonfb_info *rinfo = chan->rinfo;
  30. u32 val;
  31. val = INREG(chan->ddc_reg) & ~(VGA_DDC_DATA_OUT_EN);
  32. if (!state)
  33. val |= VGA_DDC_DATA_OUT_EN;
  34. OUTREG(chan->ddc_reg, val);
  35. (void)INREG(chan->ddc_reg);
  36. }
  37. static int radeon_gpio_getscl(void* data)
  38. {
  39. struct radeon_i2c_chan *chan = data;
  40. struct radeonfb_info *rinfo = chan->rinfo;
  41. u32 val;
  42. val = INREG(chan->ddc_reg);
  43. return (val & VGA_DDC_CLK_INPUT) ? 1 : 0;
  44. }
  45. static int radeon_gpio_getsda(void* data)
  46. {
  47. struct radeon_i2c_chan *chan = data;
  48. struct radeonfb_info *rinfo = chan->rinfo;
  49. u32 val;
  50. val = INREG(chan->ddc_reg);
  51. return (val & VGA_DDC_DATA_INPUT) ? 1 : 0;
  52. }
  53. static int radeon_setup_i2c_bus(struct radeon_i2c_chan *chan, const char *name)
  54. {
  55. int rc;
  56. strcpy(chan->adapter.name, name);
  57. chan->adapter.owner = THIS_MODULE;
  58. chan->adapter.id = I2C_HW_B_RADEON;
  59. chan->adapter.algo_data = &chan->algo;
  60. chan->adapter.dev.parent = &chan->rinfo->pdev->dev;
  61. chan->algo.setsda = radeon_gpio_setsda;
  62. chan->algo.setscl = radeon_gpio_setscl;
  63. chan->algo.getsda = radeon_gpio_getsda;
  64. chan->algo.getscl = radeon_gpio_getscl;
  65. chan->algo.udelay = 40;
  66. chan->algo.timeout = 20;
  67. chan->algo.data = chan;
  68. i2c_set_adapdata(&chan->adapter, chan);
  69. /* Raise SCL and SDA */
  70. radeon_gpio_setsda(chan, 1);
  71. radeon_gpio_setscl(chan, 1);
  72. udelay(20);
  73. rc = i2c_bit_add_bus(&chan->adapter);
  74. if (rc == 0)
  75. dev_dbg(&chan->rinfo->pdev->dev, "I2C bus %s registered.\n", name);
  76. else
  77. dev_warn(&chan->rinfo->pdev->dev, "Failed to register I2C bus %s.\n", name);
  78. return rc;
  79. }
  80. void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
  81. {
  82. rinfo->i2c[0].rinfo = rinfo;
  83. rinfo->i2c[0].ddc_reg = GPIO_MONID;
  84. radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
  85. rinfo->i2c[1].rinfo = rinfo;
  86. rinfo->i2c[1].ddc_reg = GPIO_DVI_DDC;
  87. radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
  88. rinfo->i2c[2].rinfo = rinfo;
  89. rinfo->i2c[2].ddc_reg = GPIO_VGA_DDC;
  90. radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
  91. rinfo->i2c[3].rinfo = rinfo;
  92. rinfo->i2c[3].ddc_reg = GPIO_CRT2_DDC;
  93. radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
  94. }
  95. void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
  96. {
  97. if (rinfo->i2c[0].rinfo)
  98. i2c_bit_del_bus(&rinfo->i2c[0].adapter);
  99. rinfo->i2c[0].rinfo = NULL;
  100. if (rinfo->i2c[1].rinfo)
  101. i2c_bit_del_bus(&rinfo->i2c[1].adapter);
  102. rinfo->i2c[1].rinfo = NULL;
  103. if (rinfo->i2c[2].rinfo)
  104. i2c_bit_del_bus(&rinfo->i2c[2].adapter);
  105. rinfo->i2c[2].rinfo = NULL;
  106. if (rinfo->i2c[3].rinfo)
  107. i2c_bit_del_bus(&rinfo->i2c[3].adapter);
  108. rinfo->i2c[3].rinfo = NULL;
  109. }
  110. static u8 *radeon_do_probe_i2c_edid(struct radeon_i2c_chan *chan)
  111. {
  112. u8 start = 0x0;
  113. struct i2c_msg msgs[] = {
  114. {
  115. .addr = RADEON_DDC,
  116. .len = 1,
  117. .buf = &start,
  118. }, {
  119. .addr = RADEON_DDC,
  120. .flags = I2C_M_RD,
  121. .len = EDID_LENGTH,
  122. },
  123. };
  124. u8 *buf;
  125. buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
  126. if (!buf) {
  127. dev_warn(&chan->rinfo->pdev->dev, "Out of memory!\n");
  128. return NULL;
  129. }
  130. msgs[1].buf = buf;
  131. if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
  132. return buf;
  133. dev_dbg(&chan->rinfo->pdev->dev, "Unable to read EDID block.\n");
  134. kfree(buf);
  135. return NULL;
  136. }
  137. int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn, u8 **out_edid)
  138. {
  139. u32 reg = rinfo->i2c[conn-1].ddc_reg;
  140. u8 *edid = NULL;
  141. int i, j;
  142. OUTREG(reg, INREG(reg) &
  143. ~(VGA_DDC_DATA_OUTPUT | VGA_DDC_CLK_OUTPUT));
  144. OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
  145. (void)INREG(reg);
  146. for (i = 0; i < 3; i++) {
  147. /* For some old monitors we need the
  148. * following process to initialize/stop DDC
  149. */
  150. OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
  151. (void)INREG(reg);
  152. msleep(13);
  153. OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
  154. (void)INREG(reg);
  155. for (j = 0; j < 5; j++) {
  156. msleep(10);
  157. if (INREG(reg) & VGA_DDC_CLK_INPUT)
  158. break;
  159. }
  160. if (j == 5)
  161. continue;
  162. OUTREG(reg, INREG(reg) | VGA_DDC_DATA_OUT_EN);
  163. (void)INREG(reg);
  164. msleep(15);
  165. OUTREG(reg, INREG(reg) | VGA_DDC_CLK_OUT_EN);
  166. (void)INREG(reg);
  167. msleep(15);
  168. OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
  169. (void)INREG(reg);
  170. msleep(15);
  171. /* Do the real work */
  172. edid = radeon_do_probe_i2c_edid(&rinfo->i2c[conn-1]);
  173. OUTREG(reg, INREG(reg) |
  174. (VGA_DDC_DATA_OUT_EN | VGA_DDC_CLK_OUT_EN));
  175. (void)INREG(reg);
  176. msleep(15);
  177. OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
  178. (void)INREG(reg);
  179. for (j = 0; j < 10; j++) {
  180. msleep(10);
  181. if (INREG(reg) & VGA_DDC_CLK_INPUT)
  182. break;
  183. }
  184. OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
  185. (void)INREG(reg);
  186. msleep(15);
  187. OUTREG(reg, INREG(reg) |
  188. (VGA_DDC_DATA_OUT_EN | VGA_DDC_CLK_OUT_EN));
  189. (void)INREG(reg);
  190. if (edid)
  191. break;
  192. }
  193. /* Release the DDC lines when done or the Apple Cinema HD display
  194. * will switch off
  195. */
  196. OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN | VGA_DDC_DATA_OUT_EN));
  197. (void)INREG(reg);
  198. if (out_edid)
  199. *out_edid = edid;
  200. if (!edid) {
  201. RTRACE("radeonfb: I2C (port %d) ... not found\n", conn);
  202. return MT_NONE;
  203. }
  204. if (edid[0x14] & 0x80) {
  205. /* Fix detection using BIOS tables */
  206. if (rinfo->is_mobility /*&& conn == ddc_dvi*/ &&
  207. (INREG(LVDS_GEN_CNTL) & LVDS_ON)) {
  208. RTRACE("radeonfb: I2C (port %d) ... found LVDS panel\n", conn);
  209. return MT_LCD;
  210. } else {
  211. RTRACE("radeonfb: I2C (port %d) ... found TMDS panel\n", conn);
  212. return MT_DFP;
  213. }
  214. }
  215. RTRACE("radeonfb: I2C (port %d) ... found CRT display\n", conn);
  216. return MT_CRT;
  217. }