rivafb-i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 riva_setup_i2c_bus(struct riva_i2c_chan *chan, const char *name,
  72. unsigned int i2c_class)
  73. {
  74. int rc;
  75. strcpy(chan->adapter.name, name);
  76. chan->adapter.owner = THIS_MODULE;
  77. chan->adapter.class = i2c_class;
  78. chan->adapter.algo_data = &chan->algo;
  79. chan->adapter.dev.parent = &chan->par->pdev->dev;
  80. chan->algo.setsda = riva_gpio_setsda;
  81. chan->algo.setscl = riva_gpio_setscl;
  82. chan->algo.getsda = riva_gpio_getsda;
  83. chan->algo.getscl = riva_gpio_getscl;
  84. chan->algo.udelay = 40;
  85. chan->algo.timeout = msecs_to_jiffies(2);
  86. chan->algo.data = chan;
  87. i2c_set_adapdata(&chan->adapter, chan);
  88. /* Raise SCL and SDA */
  89. riva_gpio_setsda(chan, 1);
  90. riva_gpio_setscl(chan, 1);
  91. udelay(20);
  92. rc = i2c_bit_add_bus(&chan->adapter);
  93. if (rc == 0)
  94. dev_dbg(&chan->par->pdev->dev, "I2C bus %s registered.\n", name);
  95. else {
  96. dev_warn(&chan->par->pdev->dev,
  97. "Failed to register I2C bus %s.\n", name);
  98. chan->par = NULL;
  99. }
  100. return rc;
  101. }
  102. void riva_create_i2c_busses(struct riva_par *par)
  103. {
  104. par->chan[0].par = par;
  105. par->chan[1].par = par;
  106. par->chan[2].par = par;
  107. par->chan[0].ddc_base = 0x36;
  108. par->chan[1].ddc_base = 0x3e;
  109. par->chan[2].ddc_base = 0x50;
  110. riva_setup_i2c_bus(&par->chan[0], "BUS1", I2C_CLASS_HWMON);
  111. riva_setup_i2c_bus(&par->chan[1], "BUS2", 0);
  112. riva_setup_i2c_bus(&par->chan[2], "BUS3", 0);
  113. }
  114. void riva_delete_i2c_busses(struct riva_par *par)
  115. {
  116. int i;
  117. for (i = 0; i < 3; i++) {
  118. if (!par->chan[i].par)
  119. continue;
  120. i2c_del_adapter(&par->chan[i].adapter);
  121. par->chan[i].par = NULL;
  122. }
  123. }
  124. int riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
  125. {
  126. u8 *edid = NULL;
  127. if (par->chan[conn].par)
  128. edid = fb_ddc_read(&par->chan[conn].adapter);
  129. if (out_edid)
  130. *out_edid = edid;
  131. if (!edid)
  132. return 1;
  133. return 0;
  134. }