i810-i2c.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #define I810_DDC 0x50
  22. /* bit locations in the registers */
  23. #define SCL_DIR_MASK 0x0001
  24. #define SCL_DIR 0x0002
  25. #define SCL_VAL_MASK 0x0004
  26. #define SCL_VAL_OUT 0x0008
  27. #define SCL_VAL_IN 0x0010
  28. #define SDA_DIR_MASK 0x0100
  29. #define SDA_DIR 0x0200
  30. #define SDA_VAL_MASK 0x0400
  31. #define SDA_VAL_OUT 0x0800
  32. #define SDA_VAL_IN 0x1000
  33. #define DEBUG /* define this for verbose EDID parsing output */
  34. #ifdef DEBUG
  35. #define DPRINTK(fmt, args...) printk(fmt,## args)
  36. #else
  37. #define DPRINTK(fmt, args...)
  38. #endif
  39. static void i810i2c_setscl(void *data, int state)
  40. {
  41. struct i810fb_i2c_chan *chan = data;
  42. struct i810fb_par *par = chan->par;
  43. u8 __iomem *mmio = par->mmio_start_virtual;
  44. i810_writel(mmio, chan->ddc_base, (state ? SCL_VAL_OUT : 0) | SCL_DIR |
  45. SCL_DIR_MASK | SCL_VAL_MASK);
  46. i810_readl(mmio, chan->ddc_base); /* flush posted write */
  47. }
  48. static void i810i2c_setsda(void *data, int state)
  49. {
  50. struct i810fb_i2c_chan *chan = data;
  51. struct i810fb_par *par = chan->par;
  52. u8 __iomem *mmio = par->mmio_start_virtual;
  53. i810_writel(mmio, chan->ddc_base, (state ? SDA_VAL_OUT : 0) | SDA_DIR |
  54. SDA_DIR_MASK | SDA_VAL_MASK);
  55. i810_readl(mmio, chan->ddc_base); /* flush posted write */
  56. }
  57. static int i810i2c_getscl(void *data)
  58. {
  59. struct i810fb_i2c_chan *chan = data;
  60. struct i810fb_par *par = chan->par;
  61. u8 __iomem *mmio = par->mmio_start_virtual;
  62. i810_writel(mmio, chan->ddc_base, SCL_DIR_MASK);
  63. i810_writel(mmio, chan->ddc_base, 0);
  64. return ((i810_readl(mmio, chan->ddc_base) & SCL_VAL_IN) != 0);
  65. }
  66. static int i810i2c_getsda(void *data)
  67. {
  68. struct i810fb_i2c_chan *chan = data;
  69. struct i810fb_par *par = chan->par;
  70. u8 __iomem *mmio = par->mmio_start_virtual;
  71. i810_writel(mmio, chan->ddc_base, SDA_DIR_MASK);
  72. i810_writel(mmio, chan->ddc_base, 0);
  73. return ((i810_readl(mmio, chan->ddc_base) & SDA_VAL_IN) != 0);
  74. }
  75. static int i810_setup_i2c_bus(struct i810fb_i2c_chan *chan, const char *name)
  76. {
  77. int rc;
  78. strcpy(chan->adapter.name, name);
  79. chan->adapter.owner = THIS_MODULE;
  80. chan->adapter.algo_data = &chan->algo;
  81. chan->adapter.dev.parent = &chan->par->dev->dev;
  82. chan->adapter.id = I2C_HW_B_I810;
  83. chan->algo.setsda = i810i2c_setsda;
  84. chan->algo.setscl = i810i2c_setscl;
  85. chan->algo.getsda = i810i2c_getsda;
  86. chan->algo.getscl = i810i2c_getscl;
  87. chan->algo.udelay = 10;
  88. chan->algo.timeout = (HZ/2);
  89. chan->algo.data = chan;
  90. i2c_set_adapdata(&chan->adapter, chan);
  91. /* Raise SCL and SDA */
  92. chan->algo.setsda(chan, 1);
  93. chan->algo.setscl(chan, 1);
  94. udelay(20);
  95. rc = i2c_bit_add_bus(&chan->adapter);
  96. if (rc == 0)
  97. dev_dbg(&chan->par->dev->dev, "I2C bus %s registered.\n",name);
  98. else {
  99. dev_warn(&chan->par->dev->dev, "Failed to register I2C bus "
  100. "%s.\n", name);
  101. chan->par = NULL;
  102. }
  103. return rc;
  104. }
  105. void i810_create_i2c_busses(struct i810fb_par *par)
  106. {
  107. par->chan[0].par = par;
  108. par->chan[1].par = par;
  109. par->chan[2].par = par;
  110. par->chan[0].ddc_base = GPIOA;
  111. i810_setup_i2c_bus(&par->chan[0], "I810-DDC");
  112. par->chan[1].ddc_base = GPIOB;
  113. i810_setup_i2c_bus(&par->chan[1], "I810-I2C");
  114. par->chan[2].ddc_base = GPIOC;
  115. i810_setup_i2c_bus(&par->chan[2], "I810-GPIOC");
  116. }
  117. void i810_delete_i2c_busses(struct i810fb_par *par)
  118. {
  119. if (par->chan[0].par)
  120. i2c_bit_del_bus(&par->chan[0].adapter);
  121. par->chan[0].par = NULL;
  122. if (par->chan[1].par)
  123. i2c_bit_del_bus(&par->chan[1].adapter);
  124. par->chan[1].par = NULL;
  125. if (par->chan[2].par)
  126. i2c_bit_del_bus(&par->chan[2].adapter);
  127. par->chan[2].par = NULL;
  128. }
  129. static u8 *i810_do_probe_i2c_edid(struct i810fb_i2c_chan *chan)
  130. {
  131. u8 start = 0x0;
  132. struct i2c_msg msgs[] = {
  133. {
  134. .addr = I810_DDC,
  135. .len = 1,
  136. .buf = &start,
  137. }, {
  138. .addr = I810_DDC,
  139. .flags = I2C_M_RD,
  140. .len = EDID_LENGTH,
  141. },
  142. };
  143. u8 *buf;
  144. buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
  145. if (!buf) {
  146. DPRINTK("i810-i2c: Failed to allocate memory\n");
  147. return NULL;
  148. }
  149. msgs[1].buf = buf;
  150. if (i2c_transfer(&chan->adapter, msgs, 2) == 2) {
  151. DPRINTK("i810-i2c: I2C Transfer successful\n");
  152. return buf;
  153. }
  154. DPRINTK("i810-i2c: Unable to read EDID block.\n");
  155. kfree(buf);
  156. return NULL;
  157. }
  158. int i810_probe_i2c_connector(struct fb_info *info, u8 **out_edid, int conn)
  159. {
  160. struct i810fb_par *par = info->par;
  161. u8 *edid = NULL;
  162. int i;
  163. DPRINTK("i810-i2c: Probe DDC%i Bus\n", conn+1);
  164. if (conn < par->ddc_num) {
  165. for (i = 0; i < 3; i++) {
  166. /* Do the real work */
  167. edid = i810_do_probe_i2c_edid(&par->chan[conn]);
  168. if (edid)
  169. break;
  170. }
  171. } else {
  172. const u8 *e = fb_firmware_edid(info->device);
  173. if (e != NULL) {
  174. DPRINTK("i810-i2c: Getting EDID from BIOS\n");
  175. edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  176. if (edid)
  177. memcpy(edid, e, EDID_LENGTH);
  178. }
  179. }
  180. *out_edid = edid;
  181. return (edid) ? 0 : 1;
  182. }