i810-i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*-*- linux-c -*-
  2. * linux/drivers/video/i810-i2c.c -- Intel 810/815 I2C support
  3. *
  4. * Copyright (C) 2004 Antonino Daplas<adaplas@pol.net>
  5. * All Rights Reserved
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive for
  9. * more details.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/delay.h>
  14. #include <linux/pci.h>
  15. #include <linux/fb.h>
  16. #include "i810.h"
  17. #include "i810_regs.h"
  18. #include "i810_main.h"
  19. #include "../edid.h"
  20. /* bit locations in the registers */
  21. #define SCL_DIR_MASK 0x0001
  22. #define SCL_DIR 0x0002
  23. #define SCL_VAL_MASK 0x0004
  24. #define SCL_VAL_OUT 0x0008
  25. #define SCL_VAL_IN 0x0010
  26. #define SDA_DIR_MASK 0x0100
  27. #define SDA_DIR 0x0200
  28. #define SDA_VAL_MASK 0x0400
  29. #define SDA_VAL_OUT 0x0800
  30. #define SDA_VAL_IN 0x1000
  31. #define DEBUG /* define this for verbose EDID parsing output */
  32. #ifdef DEBUG
  33. #define DPRINTK(fmt, args...) printk(fmt,## args)
  34. #else
  35. #define DPRINTK(fmt, args...)
  36. #endif
  37. static void i810i2c_setscl(void *data, int state)
  38. {
  39. struct i810fb_i2c_chan *chan = data;
  40. struct i810fb_par *par = chan->par;
  41. u8 __iomem *mmio = par->mmio_start_virtual;
  42. i810_writel(mmio, chan->ddc_base, (state ? SCL_VAL_OUT : 0) | SCL_DIR |
  43. SCL_DIR_MASK | SCL_VAL_MASK);
  44. i810_readl(mmio, chan->ddc_base); /* flush posted write */
  45. }
  46. static void i810i2c_setsda(void *data, int state)
  47. {
  48. struct i810fb_i2c_chan *chan = data;
  49. struct i810fb_par *par = chan->par;
  50. u8 __iomem *mmio = par->mmio_start_virtual;
  51. i810_writel(mmio, chan->ddc_base, (state ? SDA_VAL_OUT : 0) | SDA_DIR |
  52. SDA_DIR_MASK | SDA_VAL_MASK);
  53. i810_readl(mmio, chan->ddc_base); /* flush posted write */
  54. }
  55. static int i810i2c_getscl(void *data)
  56. {
  57. struct i810fb_i2c_chan *chan = data;
  58. struct i810fb_par *par = chan->par;
  59. u8 __iomem *mmio = par->mmio_start_virtual;
  60. i810_writel(mmio, chan->ddc_base, SCL_DIR_MASK);
  61. i810_writel(mmio, chan->ddc_base, 0);
  62. return ((i810_readl(mmio, chan->ddc_base) & SCL_VAL_IN) != 0);
  63. }
  64. static int i810i2c_getsda(void *data)
  65. {
  66. struct i810fb_i2c_chan *chan = data;
  67. struct i810fb_par *par = chan->par;
  68. u8 __iomem *mmio = par->mmio_start_virtual;
  69. i810_writel(mmio, chan->ddc_base, SDA_DIR_MASK);
  70. i810_writel(mmio, chan->ddc_base, 0);
  71. return ((i810_readl(mmio, chan->ddc_base) & SDA_VAL_IN) != 0);
  72. }
  73. static int i810_setup_i2c_bus(struct i810fb_i2c_chan *chan, const char *name)
  74. {
  75. int rc;
  76. strcpy(chan->adapter.name, name);
  77. chan->adapter.owner = THIS_MODULE;
  78. chan->adapter.algo_data = &chan->algo;
  79. chan->adapter.dev.parent = &chan->par->dev->dev;
  80. chan->adapter.id = I2C_HW_B_I810;
  81. chan->algo.setsda = i810i2c_setsda;
  82. chan->algo.setscl = i810i2c_setscl;
  83. chan->algo.getsda = i810i2c_getsda;
  84. chan->algo.getscl = i810i2c_getscl;
  85. chan->algo.udelay = 10;
  86. chan->algo.timeout = (HZ/2);
  87. chan->algo.data = chan;
  88. i2c_set_adapdata(&chan->adapter, chan);
  89. /* Raise SCL and SDA */
  90. chan->algo.setsda(chan, 1);
  91. chan->algo.setscl(chan, 1);
  92. udelay(20);
  93. rc = i2c_bit_add_bus(&chan->adapter);
  94. if (rc == 0)
  95. dev_dbg(&chan->par->dev->dev, "I2C bus %s registered.\n",name);
  96. else {
  97. dev_warn(&chan->par->dev->dev, "Failed to register I2C bus "
  98. "%s.\n", name);
  99. chan->par = NULL;
  100. }
  101. return rc;
  102. }
  103. void i810_create_i2c_busses(struct i810fb_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 = GPIOA;
  109. i810_setup_i2c_bus(&par->chan[0], "I810-DDC");
  110. par->chan[1].ddc_base = GPIOB;
  111. i810_setup_i2c_bus(&par->chan[1], "I810-I2C");
  112. par->chan[2].ddc_base = GPIOC;
  113. i810_setup_i2c_bus(&par->chan[2], "I810-GPIOC");
  114. }
  115. void i810_delete_i2c_busses(struct i810fb_par *par)
  116. {
  117. if (par->chan[0].par)
  118. i2c_del_adapter(&par->chan[0].adapter);
  119. par->chan[0].par = NULL;
  120. if (par->chan[1].par)
  121. i2c_del_adapter(&par->chan[1].adapter);
  122. par->chan[1].par = NULL;
  123. if (par->chan[2].par)
  124. i2c_del_adapter(&par->chan[2].adapter);
  125. par->chan[2].par = NULL;
  126. }
  127. int i810_probe_i2c_connector(struct fb_info *info, u8 **out_edid, int conn)
  128. {
  129. struct i810fb_par *par = info->par;
  130. u8 *edid = NULL;
  131. DPRINTK("i810-i2c: Probe DDC%i Bus\n", conn+1);
  132. if (conn < par->ddc_num) {
  133. edid = fb_ddc_read(&par->chan[conn].adapter);
  134. } else {
  135. const u8 *e = fb_firmware_edid(info->device);
  136. if (e != NULL) {
  137. DPRINTK("i810-i2c: Getting EDID from BIOS\n");
  138. edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
  139. }
  140. }
  141. *out_edid = edid;
  142. return (edid) ? 0 : 1;
  143. }