radeon_i2c.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.algo_data = &chan->algo;
  57. chan->adapter.dev.parent = &chan->rinfo->pdev->dev;
  58. chan->algo.setsda = radeon_gpio_setsda;
  59. chan->algo.setscl = radeon_gpio_setscl;
  60. chan->algo.getsda = radeon_gpio_getsda;
  61. chan->algo.getscl = radeon_gpio_getscl;
  62. chan->algo.udelay = 10;
  63. chan->algo.timeout = 20;
  64. chan->algo.data = chan;
  65. i2c_set_adapdata(&chan->adapter, chan);
  66. /* Raise SCL and SDA */
  67. radeon_gpio_setsda(chan, 1);
  68. radeon_gpio_setscl(chan, 1);
  69. udelay(20);
  70. rc = i2c_bit_add_bus(&chan->adapter);
  71. if (rc == 0)
  72. dev_dbg(&chan->rinfo->pdev->dev, "I2C bus %s registered.\n", name);
  73. else
  74. dev_warn(&chan->rinfo->pdev->dev, "Failed to register I2C bus %s.\n", name);
  75. return rc;
  76. }
  77. void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
  78. {
  79. rinfo->i2c[0].rinfo = rinfo;
  80. rinfo->i2c[0].ddc_reg = GPIO_MONID;
  81. radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
  82. rinfo->i2c[1].rinfo = rinfo;
  83. rinfo->i2c[1].ddc_reg = GPIO_DVI_DDC;
  84. radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
  85. rinfo->i2c[2].rinfo = rinfo;
  86. rinfo->i2c[2].ddc_reg = GPIO_VGA_DDC;
  87. radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
  88. rinfo->i2c[3].rinfo = rinfo;
  89. rinfo->i2c[3].ddc_reg = GPIO_CRT2_DDC;
  90. radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
  91. }
  92. void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
  93. {
  94. if (rinfo->i2c[0].rinfo)
  95. i2c_del_adapter(&rinfo->i2c[0].adapter);
  96. rinfo->i2c[0].rinfo = NULL;
  97. if (rinfo->i2c[1].rinfo)
  98. i2c_del_adapter(&rinfo->i2c[1].adapter);
  99. rinfo->i2c[1].rinfo = NULL;
  100. if (rinfo->i2c[2].rinfo)
  101. i2c_del_adapter(&rinfo->i2c[2].adapter);
  102. rinfo->i2c[2].rinfo = NULL;
  103. if (rinfo->i2c[3].rinfo)
  104. i2c_del_adapter(&rinfo->i2c[3].adapter);
  105. rinfo->i2c[3].rinfo = NULL;
  106. }
  107. int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn,
  108. u8 **out_edid)
  109. {
  110. u8 *edid;
  111. edid = fb_ddc_read(&rinfo->i2c[conn-1].adapter);
  112. if (out_edid)
  113. *out_edid = edid;
  114. if (!edid) {
  115. pr_debug("radeonfb: I2C (port %d) ... not found\n", conn);
  116. return MT_NONE;
  117. }
  118. if (edid[0x14] & 0x80) {
  119. /* Fix detection using BIOS tables */
  120. if (rinfo->is_mobility /*&& conn == ddc_dvi*/ &&
  121. (INREG(LVDS_GEN_CNTL) & LVDS_ON)) {
  122. pr_debug("radeonfb: I2C (port %d) ... found LVDS panel\n", conn);
  123. return MT_LCD;
  124. } else {
  125. pr_debug("radeonfb: I2C (port %d) ... found TMDS panel\n", conn);
  126. return MT_DFP;
  127. }
  128. }
  129. pr_debug("radeonfb: I2C (port %d) ... found CRT display\n", conn);
  130. return MT_CRT;
  131. }