radeon_i2c.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "radeonfb.h"
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/delay.h>
  5. #include <linux/fb.h>
  6. #include <linux/i2c.h>
  7. #include <linux/i2c-id.h>
  8. #include <linux/i2c-algo-bit.h>
  9. #include <asm/io.h>
  10. #include <video/radeon.h>
  11. #include "../edid.h"
  12. static void radeon_gpio_setscl(void* data, int state)
  13. {
  14. struct radeon_i2c_chan *chan = data;
  15. struct radeonfb_info *rinfo = chan->rinfo;
  16. u32 val;
  17. val = INREG(chan->ddc_reg) & ~(VGA_DDC_CLK_OUT_EN);
  18. if (!state)
  19. val |= VGA_DDC_CLK_OUT_EN;
  20. OUTREG(chan->ddc_reg, val);
  21. (void)INREG(chan->ddc_reg);
  22. }
  23. static void radeon_gpio_setsda(void* data, int state)
  24. {
  25. struct radeon_i2c_chan *chan = data;
  26. struct radeonfb_info *rinfo = chan->rinfo;
  27. u32 val;
  28. val = INREG(chan->ddc_reg) & ~(VGA_DDC_DATA_OUT_EN);
  29. if (!state)
  30. val |= VGA_DDC_DATA_OUT_EN;
  31. OUTREG(chan->ddc_reg, val);
  32. (void)INREG(chan->ddc_reg);
  33. }
  34. static int radeon_gpio_getscl(void* data)
  35. {
  36. struct radeon_i2c_chan *chan = data;
  37. struct radeonfb_info *rinfo = chan->rinfo;
  38. u32 val;
  39. val = INREG(chan->ddc_reg);
  40. return (val & VGA_DDC_CLK_INPUT) ? 1 : 0;
  41. }
  42. static int radeon_gpio_getsda(void* data)
  43. {
  44. struct radeon_i2c_chan *chan = data;
  45. struct radeonfb_info *rinfo = chan->rinfo;
  46. u32 val;
  47. val = INREG(chan->ddc_reg);
  48. return (val & VGA_DDC_DATA_INPUT) ? 1 : 0;
  49. }
  50. static int radeon_setup_i2c_bus(struct radeon_i2c_chan *chan, const char *name)
  51. {
  52. int rc;
  53. snprintf(chan->adapter.name, sizeof(chan->adapter.name),
  54. "radeonfb %s", name);
  55. chan->adapter.owner = THIS_MODULE;
  56. chan->adapter.id = I2C_HW_B_RADEON;
  57. chan->adapter.algo_data = &chan->algo;
  58. chan->adapter.dev.parent = &chan->rinfo->pdev->dev;
  59. chan->algo.setsda = radeon_gpio_setsda;
  60. chan->algo.setscl = radeon_gpio_setscl;
  61. chan->algo.getsda = radeon_gpio_getsda;
  62. chan->algo.getscl = radeon_gpio_getscl;
  63. chan->algo.udelay = 10;
  64. chan->algo.timeout = 20;
  65. chan->algo.data = chan;
  66. i2c_set_adapdata(&chan->adapter, chan);
  67. /* Raise SCL and SDA */
  68. radeon_gpio_setsda(chan, 1);
  69. radeon_gpio_setscl(chan, 1);
  70. udelay(20);
  71. rc = i2c_bit_add_bus(&chan->adapter);
  72. if (rc == 0)
  73. dev_dbg(&chan->rinfo->pdev->dev, "I2C bus %s registered.\n", name);
  74. else
  75. dev_warn(&chan->rinfo->pdev->dev, "Failed to register I2C bus %s.\n", name);
  76. return rc;
  77. }
  78. void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
  79. {
  80. rinfo->i2c[0].rinfo = rinfo;
  81. rinfo->i2c[0].ddc_reg = GPIO_MONID;
  82. radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
  83. rinfo->i2c[1].rinfo = rinfo;
  84. rinfo->i2c[1].ddc_reg = GPIO_DVI_DDC;
  85. radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
  86. rinfo->i2c[2].rinfo = rinfo;
  87. rinfo->i2c[2].ddc_reg = GPIO_VGA_DDC;
  88. radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
  89. rinfo->i2c[3].rinfo = rinfo;
  90. rinfo->i2c[3].ddc_reg = GPIO_CRT2_DDC;
  91. radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
  92. }
  93. void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
  94. {
  95. if (rinfo->i2c[0].rinfo)
  96. i2c_del_adapter(&rinfo->i2c[0].adapter);
  97. rinfo->i2c[0].rinfo = NULL;
  98. if (rinfo->i2c[1].rinfo)
  99. i2c_del_adapter(&rinfo->i2c[1].adapter);
  100. rinfo->i2c[1].rinfo = NULL;
  101. if (rinfo->i2c[2].rinfo)
  102. i2c_del_adapter(&rinfo->i2c[2].adapter);
  103. rinfo->i2c[2].rinfo = NULL;
  104. if (rinfo->i2c[3].rinfo)
  105. i2c_del_adapter(&rinfo->i2c[3].adapter);
  106. rinfo->i2c[3].rinfo = NULL;
  107. }
  108. int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn,
  109. u8 **out_edid)
  110. {
  111. u32 reg = rinfo->i2c[conn-1].ddc_reg;
  112. u8 *edid;
  113. OUTREG(reg, INREG(reg) &
  114. ~(VGA_DDC_DATA_OUTPUT | VGA_DDC_CLK_OUTPUT));
  115. edid = fb_ddc_read(&rinfo->i2c[conn-1].adapter);
  116. if (out_edid)
  117. *out_edid = edid;
  118. if (!edid) {
  119. pr_debug("radeonfb: I2C (port %d) ... not found\n", conn);
  120. return MT_NONE;
  121. }
  122. if (edid[0x14] & 0x80) {
  123. /* Fix detection using BIOS tables */
  124. if (rinfo->is_mobility /*&& conn == ddc_dvi*/ &&
  125. (INREG(LVDS_GEN_CNTL) & LVDS_ON)) {
  126. pr_debug("radeonfb: I2C (port %d) ... found LVDS panel\n", conn);
  127. return MT_LCD;
  128. } else {
  129. pr_debug("radeonfb: I2C (port %d) ... found TMDS panel\n", conn);
  130. return MT_DFP;
  131. }
  132. }
  133. pr_debug("radeonfb: I2C (port %d) ... found CRT display\n", conn);
  134. return MT_CRT;
  135. }