savagefb-i2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * linux/drivers/video/savage/savagefb-i2c.c - S3 Savage DDC2
  3. *
  4. * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
  5. *
  6. * Based partly on rivafb-i2c.c
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file COPYING in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/delay.h>
  17. #include <linux/pci.h>
  18. #include <linux/fb.h>
  19. #include <asm/io.h>
  20. #include "savagefb.h"
  21. #define SAVAGE_DDC 0x50
  22. #define VGA_CR_IX 0x3d4
  23. #define VGA_CR_DATA 0x3d5
  24. #define CR_SERIAL1 0xa0 /* I2C serial communications interface */
  25. #define MM_SERIAL1 0xff20
  26. #define CR_SERIAL2 0xb1 /* DDC2 monitor communications interface */
  27. /* based on vt8365 documentation */
  28. #define PROSAVAGE_I2C_ENAB 0x10
  29. #define PROSAVAGE_I2C_SCL_OUT 0x01
  30. #define PROSAVAGE_I2C_SDA_OUT 0x02
  31. #define PROSAVAGE_I2C_SCL_IN 0x04
  32. #define PROSAVAGE_I2C_SDA_IN 0x08
  33. #define SAVAGE4_I2C_ENAB 0x00000020
  34. #define SAVAGE4_I2C_SCL_OUT 0x00000001
  35. #define SAVAGE4_I2C_SDA_OUT 0x00000002
  36. #define SAVAGE4_I2C_SCL_IN 0x00000008
  37. #define SAVAGE4_I2C_SDA_IN 0x00000010
  38. #define SET_CR_IX(base, val) writeb((val), base + 0x8000 + VGA_CR_IX)
  39. #define SET_CR_DATA(base, val) writeb((val), base + 0x8000 + VGA_CR_DATA)
  40. #define GET_CR_DATA(base) readb(base + 0x8000 + VGA_CR_DATA)
  41. static void savage4_gpio_setscl(void *data, int val)
  42. {
  43. struct savagefb_i2c_chan *chan = data;
  44. unsigned int r;
  45. r = readl(chan->ioaddr + chan->reg);
  46. if(val)
  47. r |= SAVAGE4_I2C_SCL_OUT;
  48. else
  49. r &= ~SAVAGE4_I2C_SCL_OUT;
  50. writel(r, chan->ioaddr + chan->reg);
  51. readl(chan->ioaddr + chan->reg); /* flush posted write */
  52. }
  53. static void savage4_gpio_setsda(void *data, int val)
  54. {
  55. struct savagefb_i2c_chan *chan = data;
  56. unsigned int r;
  57. r = readl(chan->ioaddr + chan->reg);
  58. if(val)
  59. r |= SAVAGE4_I2C_SDA_OUT;
  60. else
  61. r &= ~SAVAGE4_I2C_SDA_OUT;
  62. writel(r, chan->ioaddr + chan->reg);
  63. readl(chan->ioaddr + chan->reg); /* flush posted write */
  64. }
  65. static int savage4_gpio_getscl(void *data)
  66. {
  67. struct savagefb_i2c_chan *chan = data;
  68. return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN));
  69. }
  70. static int savage4_gpio_getsda(void *data)
  71. {
  72. struct savagefb_i2c_chan *chan = data;
  73. return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SDA_IN));
  74. }
  75. static void prosavage_gpio_setscl(void* data, int val)
  76. {
  77. struct savagefb_i2c_chan *chan = data;
  78. u32 r;
  79. SET_CR_IX(chan->ioaddr, chan->reg);
  80. r = GET_CR_DATA(chan->ioaddr);
  81. r |= PROSAVAGE_I2C_ENAB;
  82. if (val) {
  83. r |= PROSAVAGE_I2C_SCL_OUT;
  84. } else {
  85. r &= ~PROSAVAGE_I2C_SCL_OUT;
  86. }
  87. SET_CR_DATA(chan->ioaddr, r);
  88. }
  89. static void prosavage_gpio_setsda(void* data, int val)
  90. {
  91. struct savagefb_i2c_chan *chan = data;
  92. unsigned int r;
  93. SET_CR_IX(chan->ioaddr, chan->reg);
  94. r = GET_CR_DATA(chan->ioaddr);
  95. r |= PROSAVAGE_I2C_ENAB;
  96. if (val) {
  97. r |= PROSAVAGE_I2C_SDA_OUT;
  98. } else {
  99. r &= ~PROSAVAGE_I2C_SDA_OUT;
  100. }
  101. SET_CR_DATA(chan->ioaddr, r);
  102. }
  103. static int prosavage_gpio_getscl(void* data)
  104. {
  105. struct savagefb_i2c_chan *chan = data;
  106. SET_CR_IX(chan->ioaddr, chan->reg);
  107. return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SCL_IN));
  108. }
  109. static int prosavage_gpio_getsda(void* data)
  110. {
  111. struct savagefb_i2c_chan *chan = data;
  112. SET_CR_IX(chan->ioaddr, chan->reg);
  113. return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SDA_IN));
  114. }
  115. static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan,
  116. const char *name)
  117. {
  118. int rc = 0;
  119. if (chan->par) {
  120. strcpy(chan->adapter.name, name);
  121. chan->adapter.owner = THIS_MODULE;
  122. chan->adapter.id = I2C_HW_B_SAVAGE;
  123. chan->adapter.algo_data = &chan->algo;
  124. chan->adapter.dev.parent = &chan->par->pcidev->dev;
  125. chan->algo.udelay = 40;
  126. chan->algo.mdelay = 5;
  127. chan->algo.timeout = 20;
  128. chan->algo.data = chan;
  129. i2c_set_adapdata(&chan->adapter, chan);
  130. /* Raise SCL and SDA */
  131. chan->algo.setsda(chan, 1);
  132. chan->algo.setscl(chan, 1);
  133. udelay(20);
  134. rc = i2c_bit_add_bus(&chan->adapter);
  135. if (rc == 0)
  136. dev_dbg(&chan->par->pcidev->dev,
  137. "I2C bus %s registered.\n", name);
  138. else
  139. dev_warn(&chan->par->pcidev->dev,
  140. "Failed to register I2C bus %s.\n", name);
  141. } else
  142. chan->par = NULL;
  143. return rc;
  144. }
  145. void savagefb_create_i2c_busses(struct fb_info *info)
  146. {
  147. struct savagefb_par *par = info->par;
  148. par->chan.par = par;
  149. switch(info->fix.accel) {
  150. case FB_ACCEL_PROSAVAGE_DDRK:
  151. case FB_ACCEL_PROSAVAGE_PM:
  152. par->chan.reg = CR_SERIAL2;
  153. par->chan.ioaddr = par->mmio.vbase;
  154. par->chan.algo.setsda = prosavage_gpio_setsda;
  155. par->chan.algo.setscl = prosavage_gpio_setscl;
  156. par->chan.algo.getsda = prosavage_gpio_getsda;
  157. par->chan.algo.getscl = prosavage_gpio_getscl;
  158. break;
  159. case FB_ACCEL_SAVAGE4:
  160. case FB_ACCEL_SAVAGE2000:
  161. par->chan.reg = 0xff20;
  162. par->chan.ioaddr = par->mmio.vbase;
  163. par->chan.algo.setsda = savage4_gpio_setsda;
  164. par->chan.algo.setscl = savage4_gpio_setscl;
  165. par->chan.algo.getsda = savage4_gpio_getsda;
  166. par->chan.algo.getscl = savage4_gpio_getscl;
  167. break;
  168. default:
  169. par->chan.par = NULL;
  170. }
  171. savage_setup_i2c_bus(&par->chan, "SAVAGE DDC2");
  172. }
  173. void savagefb_delete_i2c_busses(struct fb_info *info)
  174. {
  175. struct savagefb_par *par = info->par;
  176. if (par->chan.par)
  177. i2c_bit_del_bus(&par->chan.adapter);
  178. par->chan.par = NULL;
  179. }
  180. static u8 *savage_do_probe_i2c_edid(struct savagefb_i2c_chan *chan)
  181. {
  182. u8 start = 0x0;
  183. struct i2c_msg msgs[] = {
  184. {
  185. .addr = SAVAGE_DDC,
  186. .len = 1,
  187. .buf = &start,
  188. }, {
  189. .addr = SAVAGE_DDC,
  190. .flags = I2C_M_RD,
  191. .len = EDID_LENGTH,
  192. },
  193. };
  194. u8 *buf = NULL;
  195. if (chan->par) {
  196. buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
  197. if (buf) {
  198. msgs[1].buf = buf;
  199. if (i2c_transfer(&chan->adapter, msgs, 2) != 2) {
  200. dev_dbg(&chan->par->pcidev->dev,
  201. "Unable to read EDID block.\n");
  202. kfree(buf);
  203. buf = NULL;
  204. }
  205. }
  206. }
  207. return buf;
  208. }
  209. int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid)
  210. {
  211. struct savagefb_par *par = info->par;
  212. u8 *edid = NULL;
  213. int i;
  214. for (i = 0; i < 3; i++) {
  215. /* Do the real work */
  216. edid = savage_do_probe_i2c_edid(&par->chan);
  217. if (edid)
  218. break;
  219. }
  220. if (!edid) {
  221. /* try to get from firmware */
  222. const u8 *e = fb_firmware_edid(info->device);
  223. if (e) {
  224. edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  225. if (edid)
  226. memcpy(edid, e, EDID_LENGTH);
  227. }
  228. }
  229. *out_edid = edid;
  230. return (edid) ? 0 : 1;
  231. }
  232. MODULE_LICENSE("GPL");