i810-i2c.c 5.0 KB

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