i810-i2c.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "../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 = (struct i810fb_i2c_chan *)data;
  42. struct i810fb_par *par = chan->par;
  43. u8 *mmio = par->mmio_start_virtual;
  44. i810_writel(mmio, GPIOB, (state ? SCL_VAL_OUT : 0) | SCL_DIR |
  45. SCL_DIR_MASK | SCL_VAL_MASK);
  46. i810_readl(mmio, GPIOB); /* flush posted write */
  47. }
  48. static void i810i2c_setsda(void *data, int state)
  49. {
  50. struct i810fb_i2c_chan *chan = (struct i810fb_i2c_chan *)data;
  51. struct i810fb_par *par = chan->par;
  52. u8 *mmio = par->mmio_start_virtual;
  53. i810_writel(mmio, GPIOB, (state ? SDA_VAL_OUT : 0) | SDA_DIR |
  54. SDA_DIR_MASK | SDA_VAL_MASK);
  55. i810_readl(mmio, GPIOB); /* flush posted write */
  56. }
  57. static int i810i2c_getscl(void *data)
  58. {
  59. struct i810fb_i2c_chan *chan = (struct i810fb_i2c_chan *)data;
  60. struct i810fb_par *par = chan->par;
  61. u8 *mmio = par->mmio_start_virtual;
  62. i810_writel(mmio, GPIOB, SCL_DIR_MASK);
  63. i810_writel(mmio, GPIOB, 0);
  64. return (0 != (i810_readl(mmio, GPIOB) & SCL_VAL_IN));
  65. }
  66. static int i810i2c_getsda(void *data)
  67. {
  68. struct i810fb_i2c_chan *chan = (struct i810fb_i2c_chan *)data;
  69. struct i810fb_par *par = chan->par;
  70. u8 *mmio = par->mmio_start_virtual;
  71. i810_writel(mmio, GPIOB, SDA_DIR_MASK);
  72. i810_writel(mmio, GPIOB, 0);
  73. return (0 != (i810_readl(mmio, GPIOB) & SDA_VAL_IN));
  74. }
  75. static void i810ddc_setscl(void *data, int state)
  76. {
  77. struct i810fb_i2c_chan *chan = (struct i810fb_i2c_chan *)data;
  78. struct i810fb_par *par = chan->par;
  79. u8 *mmio = par->mmio_start_virtual;
  80. i810_writel(mmio, GPIOA, (state ? SCL_VAL_OUT : 0) | SCL_DIR |
  81. SCL_DIR_MASK | SCL_VAL_MASK);
  82. i810_readl(mmio, GPIOA); /* flush posted write */
  83. }
  84. static void i810ddc_setsda(void *data, int state)
  85. {
  86. struct i810fb_i2c_chan *chan = (struct i810fb_i2c_chan *)data;
  87. struct i810fb_par *par = chan->par;
  88. u8 *mmio = par->mmio_start_virtual;
  89. i810_writel(mmio, GPIOA, (state ? SDA_VAL_OUT : 0) | SDA_DIR |
  90. SDA_DIR_MASK | SDA_VAL_MASK);
  91. i810_readl(mmio, GPIOA); /* flush posted write */
  92. }
  93. static int i810ddc_getscl(void *data)
  94. {
  95. struct i810fb_i2c_chan *chan = (struct i810fb_i2c_chan *)data;
  96. struct i810fb_par *par = chan->par;
  97. u8 *mmio = par->mmio_start_virtual;
  98. i810_writel(mmio, GPIOA, SCL_DIR_MASK);
  99. i810_writel(mmio, GPIOA, 0);
  100. return (0 != (i810_readl(mmio, GPIOA) & SCL_VAL_IN));
  101. }
  102. static int i810ddc_getsda(void *data)
  103. {
  104. struct i810fb_i2c_chan *chan = (struct i810fb_i2c_chan *)data;
  105. struct i810fb_par *par = chan->par;
  106. u8 *mmio = par->mmio_start_virtual;
  107. i810_writel(mmio, GPIOA, SDA_DIR_MASK);
  108. i810_writel(mmio, GPIOA, 0);
  109. return (0 != (i810_readl(mmio, GPIOA) & SDA_VAL_IN));
  110. }
  111. #define I2C_ALGO_DDC_I810 0x0e0000
  112. #define I2C_ALGO_I2C_I810 0x0f0000
  113. static int i810_setup_i2c_bus(struct i810fb_i2c_chan *chan, const char *name,
  114. int conn)
  115. {
  116. int rc;
  117. strcpy(chan->adapter.name, name);
  118. chan->adapter.owner = THIS_MODULE;
  119. chan->adapter.algo_data = &chan->algo;
  120. chan->adapter.dev.parent = &chan->par->dev->dev;
  121. switch (conn) {
  122. case 1:
  123. chan->adapter.id = I2C_ALGO_DDC_I810;
  124. chan->algo.setsda = i810ddc_setsda;
  125. chan->algo.setscl = i810ddc_setscl;
  126. chan->algo.getsda = i810ddc_getsda;
  127. chan->algo.getscl = i810ddc_getscl;
  128. break;
  129. case 2:
  130. chan->adapter.id = I2C_ALGO_I2C_I810;
  131. chan->algo.setsda = i810i2c_setsda;
  132. chan->algo.setscl = i810i2c_setscl;
  133. chan->algo.getsda = i810i2c_getsda;
  134. chan->algo.getscl = i810i2c_getscl;
  135. break;
  136. }
  137. chan->algo.udelay = 10;
  138. chan->algo.mdelay = 10;
  139. chan->algo.timeout = (HZ/2);
  140. chan->algo.data = chan;
  141. i2c_set_adapdata(&chan->adapter, chan);
  142. /* Raise SCL and SDA */
  143. chan->algo.setsda(chan, 1);
  144. chan->algo.setscl(chan, 1);
  145. udelay(20);
  146. rc = i2c_bit_add_bus(&chan->adapter);
  147. if (rc == 0)
  148. dev_dbg(&chan->par->dev->dev, "I2C bus %s registered.\n",name);
  149. else
  150. dev_warn(&chan->par->dev->dev, "Failed to register I2C bus "
  151. "%s.\n", name);
  152. return rc;
  153. }
  154. void i810_create_i2c_busses(struct i810fb_par *par)
  155. {
  156. par->chan[0].par = par;
  157. par->chan[1].par = par;
  158. i810_setup_i2c_bus(&par->chan[0], "I810-DDC", 1);
  159. i810_setup_i2c_bus(&par->chan[1], "I810-I2C", 2);
  160. }
  161. void i810_delete_i2c_busses(struct i810fb_par *par)
  162. {
  163. if (par->chan[0].par)
  164. i2c_bit_del_bus(&par->chan[0].adapter);
  165. par->chan[0].par = NULL;
  166. if (par->chan[1].par)
  167. i2c_bit_del_bus(&par->chan[1].adapter);
  168. par->chan[1].par = NULL;
  169. }
  170. static u8 *i810_do_probe_i2c_edid(struct i810fb_i2c_chan *chan)
  171. {
  172. u8 start = 0x0;
  173. struct i2c_msg msgs[] = {
  174. {
  175. .addr = I810_DDC,
  176. .len = 1,
  177. .buf = &start,
  178. }, {
  179. .addr = I810_DDC,
  180. .flags = I2C_M_RD,
  181. .len = EDID_LENGTH,
  182. },
  183. };
  184. u8 *buf;
  185. buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
  186. if (!buf) {
  187. DPRINTK("i810-i2c: Failed to allocate memory\n");
  188. return NULL;
  189. }
  190. msgs[1].buf = buf;
  191. if (i2c_transfer(&chan->adapter, msgs, 2) == 2) {
  192. DPRINTK("i810-i2c: I2C Transfer successful\n");
  193. return buf;
  194. }
  195. DPRINTK("i810-i2c: Unable to read EDID block.\n");
  196. kfree(buf);
  197. return NULL;
  198. }
  199. int i810_probe_i2c_connector(struct fb_info *info, u8 **out_edid, int conn)
  200. {
  201. struct i810fb_par *par = info->par;
  202. u8 *edid = NULL;
  203. int i;
  204. DPRINTK("i810-i2c: Probe DDC%i Bus\n", conn);
  205. if (conn < 3) {
  206. for (i = 0; i < 3; i++) {
  207. /* Do the real work */
  208. edid = i810_do_probe_i2c_edid(&par->chan[conn-1]);
  209. if (edid)
  210. break;
  211. }
  212. } else {
  213. DPRINTK("i810-i2c: Getting EDID from BIOS\n");
  214. edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  215. if (edid)
  216. memcpy(edid, fb_firmware_edid(info->device),
  217. EDID_LENGTH);
  218. }
  219. if (out_edid)
  220. *out_edid = edid;
  221. return (edid) ? 0 : 1;
  222. }