radeon_i2c.c 3.9 KB

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