rivafb-i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * linux/drivers/video/riva/fbdev-i2c.c - nVidia i2c
  3. *
  4. * Maintained by Ani Joshi <ajoshi@shell.unixbox.com>
  5. *
  6. * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
  7. *
  8. * Based on radeonfb-i2c.c
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file COPYING in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/delay.h>
  17. #include <linux/pci.h>
  18. #include <linux/fb.h>
  19. #include <linux/jiffies.h>
  20. #include <asm/io.h>
  21. #include "rivafb.h"
  22. #include "../edid.h"
  23. static void riva_gpio_setscl(void* data, int state)
  24. {
  25. struct riva_i2c_chan *chan = data;
  26. struct riva_par *par = chan->par;
  27. u32 val;
  28. VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
  29. val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
  30. if (state)
  31. val |= 0x20;
  32. else
  33. val &= ~0x20;
  34. VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
  35. VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
  36. }
  37. static void riva_gpio_setsda(void* data, int state)
  38. {
  39. struct riva_i2c_chan *chan = data;
  40. struct riva_par *par = chan->par;
  41. u32 val;
  42. VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
  43. val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
  44. if (state)
  45. val |= 0x10;
  46. else
  47. val &= ~0x10;
  48. VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
  49. VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
  50. }
  51. static int riva_gpio_getscl(void* data)
  52. {
  53. struct riva_i2c_chan *chan = data;
  54. struct riva_par *par = chan->par;
  55. u32 val = 0;
  56. VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
  57. if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04)
  58. val = 1;
  59. return val;
  60. }
  61. static int riva_gpio_getsda(void* data)
  62. {
  63. struct riva_i2c_chan *chan = data;
  64. struct riva_par *par = chan->par;
  65. u32 val = 0;
  66. VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
  67. if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08)
  68. val = 1;
  69. return val;
  70. }
  71. static int __devinit riva_setup_i2c_bus(struct riva_i2c_chan *chan,
  72. const char *name,
  73. unsigned int i2c_class)
  74. {
  75. int rc;
  76. strcpy(chan->adapter.name, name);
  77. chan->adapter.owner = THIS_MODULE;
  78. chan->adapter.class = i2c_class;
  79. chan->adapter.algo_data = &chan->algo;
  80. chan->adapter.dev.parent = &chan->par->pdev->dev;
  81. chan->algo.setsda = riva_gpio_setsda;
  82. chan->algo.setscl = riva_gpio_setscl;
  83. chan->algo.getsda = riva_gpio_getsda;
  84. chan->algo.getscl = riva_gpio_getscl;
  85. chan->algo.udelay = 40;
  86. chan->algo.timeout = msecs_to_jiffies(2);
  87. chan->algo.data = chan;
  88. i2c_set_adapdata(&chan->adapter, chan);
  89. /* Raise SCL and SDA */
  90. riva_gpio_setsda(chan, 1);
  91. riva_gpio_setscl(chan, 1);
  92. udelay(20);
  93. rc = i2c_bit_add_bus(&chan->adapter);
  94. if (rc == 0)
  95. dev_dbg(&chan->par->pdev->dev, "I2C bus %s registered.\n", name);
  96. else {
  97. dev_warn(&chan->par->pdev->dev,
  98. "Failed to register I2C bus %s.\n", name);
  99. chan->par = NULL;
  100. }
  101. return rc;
  102. }
  103. void __devinit riva_create_i2c_busses(struct riva_par *par)
  104. {
  105. par->chan[0].par = par;
  106. par->chan[1].par = par;
  107. par->chan[2].par = par;
  108. par->chan[0].ddc_base = 0x36;
  109. par->chan[1].ddc_base = 0x3e;
  110. par->chan[2].ddc_base = 0x50;
  111. riva_setup_i2c_bus(&par->chan[0], "BUS1", I2C_CLASS_HWMON);
  112. riva_setup_i2c_bus(&par->chan[1], "BUS2", 0);
  113. riva_setup_i2c_bus(&par->chan[2], "BUS3", 0);
  114. }
  115. void riva_delete_i2c_busses(struct riva_par *par)
  116. {
  117. int i;
  118. for (i = 0; i < 3; i++) {
  119. if (!par->chan[i].par)
  120. continue;
  121. i2c_del_adapter(&par->chan[i].adapter);
  122. par->chan[i].par = NULL;
  123. }
  124. }
  125. int __devinit riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
  126. {
  127. u8 *edid = NULL;
  128. if (par->chan[conn].par)
  129. edid = fb_ddc_read(&par->chan[conn].adapter);
  130. if (out_edid)
  131. *out_edid = edid;
  132. if (!edid)
  133. return 1;
  134. return 0;
  135. }