i810-i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/gfp.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. if (state)
  44. i810_writel(mmio, chan->ddc_base, SCL_DIR_MASK | SCL_VAL_MASK);
  45. else
  46. i810_writel(mmio, chan->ddc_base, SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK);
  47. i810_readl(mmio, chan->ddc_base); /* flush posted write */
  48. }
  49. static void i810i2c_setsda(void *data, int state)
  50. {
  51. struct i810fb_i2c_chan *chan = data;
  52. struct i810fb_par *par = chan->par;
  53. u8 __iomem *mmio = par->mmio_start_virtual;
  54. if (state)
  55. i810_writel(mmio, chan->ddc_base, SDA_DIR_MASK | SDA_VAL_MASK);
  56. else
  57. i810_writel(mmio, chan->ddc_base, SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK);
  58. i810_readl(mmio, chan->ddc_base); /* flush posted write */
  59. }
  60. static int i810i2c_getscl(void *data)
  61. {
  62. struct i810fb_i2c_chan *chan = data;
  63. struct i810fb_par *par = chan->par;
  64. u8 __iomem *mmio = par->mmio_start_virtual;
  65. i810_writel(mmio, chan->ddc_base, SCL_DIR_MASK);
  66. i810_writel(mmio, chan->ddc_base, 0);
  67. return ((i810_readl(mmio, chan->ddc_base) & SCL_VAL_IN) != 0);
  68. }
  69. static int i810i2c_getsda(void *data)
  70. {
  71. struct i810fb_i2c_chan *chan = data;
  72. struct i810fb_par *par = chan->par;
  73. u8 __iomem *mmio = par->mmio_start_virtual;
  74. i810_writel(mmio, chan->ddc_base, SDA_DIR_MASK);
  75. i810_writel(mmio, chan->ddc_base, 0);
  76. return ((i810_readl(mmio, chan->ddc_base) & SDA_VAL_IN) != 0);
  77. }
  78. static int i810_setup_i2c_bus(struct i810fb_i2c_chan *chan, const char *name)
  79. {
  80. int rc;
  81. strcpy(chan->adapter.name, name);
  82. chan->adapter.owner = THIS_MODULE;
  83. chan->adapter.algo_data = &chan->algo;
  84. chan->adapter.dev.parent = &chan->par->dev->dev;
  85. chan->algo.setsda = i810i2c_setsda;
  86. chan->algo.setscl = i810i2c_setscl;
  87. chan->algo.getsda = i810i2c_getsda;
  88. chan->algo.getscl = i810i2c_getscl;
  89. chan->algo.udelay = 10;
  90. chan->algo.timeout = (HZ/2);
  91. chan->algo.data = chan;
  92. i2c_set_adapdata(&chan->adapter, chan);
  93. /* Raise SCL and SDA */
  94. chan->algo.setsda(chan, 1);
  95. chan->algo.setscl(chan, 1);
  96. udelay(20);
  97. rc = i2c_bit_add_bus(&chan->adapter);
  98. if (rc == 0)
  99. dev_dbg(&chan->par->dev->dev, "I2C bus %s registered.\n",name);
  100. else {
  101. dev_warn(&chan->par->dev->dev, "Failed to register I2C bus "
  102. "%s.\n", name);
  103. chan->par = NULL;
  104. }
  105. return rc;
  106. }
  107. void i810_create_i2c_busses(struct i810fb_par *par)
  108. {
  109. par->chan[0].par = par;
  110. par->chan[1].par = par;
  111. par->chan[2].par = par;
  112. par->chan[0].ddc_base = GPIOA;
  113. i810_setup_i2c_bus(&par->chan[0], "I810-DDC");
  114. par->chan[1].ddc_base = GPIOB;
  115. i810_setup_i2c_bus(&par->chan[1], "I810-I2C");
  116. par->chan[2].ddc_base = GPIOC;
  117. i810_setup_i2c_bus(&par->chan[2], "I810-GPIOC");
  118. }
  119. void i810_delete_i2c_busses(struct i810fb_par *par)
  120. {
  121. if (par->chan[0].par)
  122. i2c_del_adapter(&par->chan[0].adapter);
  123. par->chan[0].par = NULL;
  124. if (par->chan[1].par)
  125. i2c_del_adapter(&par->chan[1].adapter);
  126. par->chan[1].par = NULL;
  127. if (par->chan[2].par)
  128. i2c_del_adapter(&par->chan[2].adapter);
  129. par->chan[2].par = NULL;
  130. }
  131. int i810_probe_i2c_connector(struct fb_info *info, u8 **out_edid, int conn)
  132. {
  133. struct i810fb_par *par = info->par;
  134. u8 *edid = NULL;
  135. DPRINTK("i810-i2c: Probe DDC%i Bus\n", conn+1);
  136. if (conn < par->ddc_num) {
  137. edid = fb_ddc_read(&par->chan[conn].adapter);
  138. } else {
  139. const u8 *e = fb_firmware_edid(info->device);
  140. if (e != NULL) {
  141. DPRINTK("i810-i2c: Getting EDID from BIOS\n");
  142. edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
  143. }
  144. }
  145. *out_edid = edid;
  146. return (edid) ? 0 : 1;
  147. }