nv_i2c.c 3.7 KB

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