i810-i2c.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/config.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/delay.h>
  16. #include <linux/pci.h>
  17. #include <linux/fb.h>
  18. #include "i810.h"
  19. #include "i810_regs.h"
  20. #include "i810_main.h"
  21. #include "../edid.h"
  22. #define I810_DDC 0x50
  23. /* bit locations in the registers */
  24. #define SCL_DIR_MASK 0x0001
  25. #define SCL_DIR 0x0002
  26. #define SCL_VAL_MASK 0x0004
  27. #define SCL_VAL_OUT 0x0008
  28. #define SCL_VAL_IN 0x0010
  29. #define SDA_DIR_MASK 0x0100
  30. #define SDA_DIR 0x0200
  31. #define SDA_VAL_MASK 0x0400
  32. #define SDA_VAL_OUT 0x0800
  33. #define SDA_VAL_IN 0x1000
  34. #define DEBUG /* define this for verbose EDID parsing output */
  35. #ifdef DEBUG
  36. #define DPRINTK(fmt, args...) printk(fmt,## args)
  37. #else
  38. #define DPRINTK(fmt, args...)
  39. #endif
  40. static void i810i2c_setscl(void *data, int state)
  41. {
  42. struct i810fb_i2c_chan *chan = data;
  43. struct i810fb_par *par = chan->par;
  44. u8 __iomem *mmio = par->mmio_start_virtual;
  45. i810_writel(mmio, chan->ddc_base, (state ? SCL_VAL_OUT : 0) | SCL_DIR |
  46. 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. i810_writel(mmio, chan->ddc_base, (state ? SDA_VAL_OUT : 0) | SDA_DIR |
  55. SDA_DIR_MASK | SDA_VAL_MASK);
  56. i810_readl(mmio, chan->ddc_base); /* flush posted write */
  57. }
  58. static int i810i2c_getscl(void *data)
  59. {
  60. struct i810fb_i2c_chan *chan = data;
  61. struct i810fb_par *par = chan->par;
  62. u8 __iomem *mmio = par->mmio_start_virtual;
  63. i810_writel(mmio, chan->ddc_base, SCL_DIR_MASK);
  64. i810_writel(mmio, chan->ddc_base, 0);
  65. return ((i810_readl(mmio, chan->ddc_base) & SCL_VAL_IN) != 0);
  66. }
  67. static int i810i2c_getsda(void *data)
  68. {
  69. struct i810fb_i2c_chan *chan = data;
  70. struct i810fb_par *par = chan->par;
  71. u8 __iomem *mmio = par->mmio_start_virtual;
  72. i810_writel(mmio, chan->ddc_base, SDA_DIR_MASK);
  73. i810_writel(mmio, chan->ddc_base, 0);
  74. return ((i810_readl(mmio, chan->ddc_base) & SDA_VAL_IN) != 0);
  75. }
  76. static int i810_setup_i2c_bus(struct i810fb_i2c_chan *chan, const char *name)
  77. {
  78. int rc;
  79. strcpy(chan->adapter.name, name);
  80. chan->adapter.owner = THIS_MODULE;
  81. chan->adapter.algo_data = &chan->algo;
  82. chan->adapter.dev.parent = &chan->par->dev->dev;
  83. chan->adapter.id = I2C_HW_B_I810;
  84. chan->algo.setsda = i810i2c_setsda;
  85. chan->algo.setscl = i810i2c_setscl;
  86. chan->algo.getsda = i810i2c_getsda;
  87. chan->algo.getscl = i810i2c_getscl;
  88. chan->algo.udelay = 10;
  89. chan->algo.mdelay = 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_bit_del_bus(&par->chan[0].adapter);
  123. par->chan[0].par = NULL;
  124. if (par->chan[1].par)
  125. i2c_bit_del_bus(&par->chan[1].adapter);
  126. par->chan[1].par = NULL;
  127. if (par->chan[2].par)
  128. i2c_bit_del_bus(&par->chan[2].adapter);
  129. par->chan[2].par = NULL;
  130. }
  131. static u8 *i810_do_probe_i2c_edid(struct i810fb_i2c_chan *chan)
  132. {
  133. u8 start = 0x0;
  134. struct i2c_msg msgs[] = {
  135. {
  136. .addr = I810_DDC,
  137. .len = 1,
  138. .buf = &start,
  139. }, {
  140. .addr = I810_DDC,
  141. .flags = I2C_M_RD,
  142. .len = EDID_LENGTH,
  143. },
  144. };
  145. u8 *buf;
  146. buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
  147. if (!buf) {
  148. DPRINTK("i810-i2c: Failed to allocate memory\n");
  149. return NULL;
  150. }
  151. msgs[1].buf = buf;
  152. if (i2c_transfer(&chan->adapter, msgs, 2) == 2) {
  153. DPRINTK("i810-i2c: I2C Transfer successful\n");
  154. return buf;
  155. }
  156. DPRINTK("i810-i2c: Unable to read EDID block.\n");
  157. kfree(buf);
  158. return NULL;
  159. }
  160. int i810_probe_i2c_connector(struct fb_info *info, u8 **out_edid, int conn)
  161. {
  162. struct i810fb_par *par = info->par;
  163. u8 *edid = NULL;
  164. int i;
  165. DPRINTK("i810-i2c: Probe DDC%i Bus\n", conn+1);
  166. if (conn < par->ddc_num) {
  167. for (i = 0; i < 3; i++) {
  168. /* Do the real work */
  169. edid = i810_do_probe_i2c_edid(&par->chan[conn]);
  170. if (edid)
  171. break;
  172. }
  173. } else {
  174. const u8 *e = fb_firmware_edid(info->device);
  175. if (e != NULL) {
  176. DPRINTK("i810-i2c: Getting EDID from BIOS\n");
  177. edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  178. if (edid)
  179. memcpy(edid, e, EDID_LENGTH);
  180. }
  181. }
  182. *out_edid = edid;
  183. return (edid) ? 0 : 1;
  184. }