savagefb-i2c.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/delay.h>
  15. #include <linux/gfp.h>
  16. #include <linux/pci.h>
  17. #include <linux/fb.h>
  18. #include <asm/io.h>
  19. #include "savagefb.h"
  20. #define SAVAGE_DDC 0x50
  21. #define VGA_CR_IX 0x3d4
  22. #define VGA_CR_DATA 0x3d5
  23. #define CR_SERIAL1 0xa0 /* I2C serial communications interface */
  24. #define MM_SERIAL1 0xff20
  25. #define CR_SERIAL2 0xb1 /* DDC2 monitor communications interface */
  26. /* based on vt8365 documentation */
  27. #define PROSAVAGE_I2C_ENAB 0x10
  28. #define PROSAVAGE_I2C_SCL_OUT 0x01
  29. #define PROSAVAGE_I2C_SDA_OUT 0x02
  30. #define PROSAVAGE_I2C_SCL_IN 0x04
  31. #define PROSAVAGE_I2C_SDA_IN 0x08
  32. #define SAVAGE4_I2C_ENAB 0x00000020
  33. #define SAVAGE4_I2C_SCL_OUT 0x00000001
  34. #define SAVAGE4_I2C_SDA_OUT 0x00000002
  35. #define SAVAGE4_I2C_SCL_IN 0x00000008
  36. #define SAVAGE4_I2C_SDA_IN 0x00000010
  37. static void savage4_gpio_setscl(void *data, int val)
  38. {
  39. struct savagefb_i2c_chan *chan = data;
  40. unsigned int r;
  41. r = readl(chan->ioaddr + chan->reg);
  42. if(val)
  43. r |= SAVAGE4_I2C_SCL_OUT;
  44. else
  45. r &= ~SAVAGE4_I2C_SCL_OUT;
  46. writel(r, chan->ioaddr + chan->reg);
  47. readl(chan->ioaddr + chan->reg); /* flush posted write */
  48. }
  49. static void savage4_gpio_setsda(void *data, int val)
  50. {
  51. struct savagefb_i2c_chan *chan = data;
  52. unsigned int r;
  53. r = readl(chan->ioaddr + chan->reg);
  54. if(val)
  55. r |= SAVAGE4_I2C_SDA_OUT;
  56. else
  57. r &= ~SAVAGE4_I2C_SDA_OUT;
  58. writel(r, chan->ioaddr + chan->reg);
  59. readl(chan->ioaddr + chan->reg); /* flush posted write */
  60. }
  61. static int savage4_gpio_getscl(void *data)
  62. {
  63. struct savagefb_i2c_chan *chan = data;
  64. return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN));
  65. }
  66. static int savage4_gpio_getsda(void *data)
  67. {
  68. struct savagefb_i2c_chan *chan = data;
  69. return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SDA_IN));
  70. }
  71. static void prosavage_gpio_setscl(void* data, int val)
  72. {
  73. struct savagefb_i2c_chan *chan = data;
  74. u32 r;
  75. r = VGArCR(chan->reg, chan->par);
  76. r |= PROSAVAGE_I2C_ENAB;
  77. if (val) {
  78. r |= PROSAVAGE_I2C_SCL_OUT;
  79. } else {
  80. r &= ~PROSAVAGE_I2C_SCL_OUT;
  81. }
  82. VGAwCR(chan->reg, r, chan->par);
  83. }
  84. static void prosavage_gpio_setsda(void* data, int val)
  85. {
  86. struct savagefb_i2c_chan *chan = data;
  87. unsigned int r;
  88. r = VGArCR(chan->reg, chan->par);
  89. r |= PROSAVAGE_I2C_ENAB;
  90. if (val) {
  91. r |= PROSAVAGE_I2C_SDA_OUT;
  92. } else {
  93. r &= ~PROSAVAGE_I2C_SDA_OUT;
  94. }
  95. VGAwCR(chan->reg, r, chan->par);
  96. }
  97. static int prosavage_gpio_getscl(void* data)
  98. {
  99. struct savagefb_i2c_chan *chan = data;
  100. return (VGArCR(chan->reg, chan->par) & PROSAVAGE_I2C_SCL_IN) ? 1 : 0;
  101. }
  102. static int prosavage_gpio_getsda(void* data)
  103. {
  104. struct savagefb_i2c_chan *chan = data;
  105. return (VGArCR(chan->reg, chan->par) & PROSAVAGE_I2C_SDA_IN) ? 1 : 0;
  106. }
  107. static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan,
  108. const char *name)
  109. {
  110. int rc = 0;
  111. if (chan->par) {
  112. strcpy(chan->adapter.name, name);
  113. chan->adapter.owner = THIS_MODULE;
  114. chan->adapter.algo_data = &chan->algo;
  115. chan->adapter.dev.parent = &chan->par->pcidev->dev;
  116. chan->algo.udelay = 10;
  117. chan->algo.timeout = 20;
  118. chan->algo.data = chan;
  119. i2c_set_adapdata(&chan->adapter, chan);
  120. /* Raise SCL and SDA */
  121. chan->algo.setsda(chan, 1);
  122. chan->algo.setscl(chan, 1);
  123. udelay(20);
  124. rc = i2c_bit_add_bus(&chan->adapter);
  125. if (rc == 0)
  126. dev_dbg(&chan->par->pcidev->dev,
  127. "I2C bus %s registered.\n", name);
  128. else
  129. dev_warn(&chan->par->pcidev->dev,
  130. "Failed to register I2C bus %s.\n", name);
  131. }
  132. return rc;
  133. }
  134. void savagefb_create_i2c_busses(struct fb_info *info)
  135. {
  136. struct savagefb_par *par = info->par;
  137. par->chan.par = par;
  138. switch (par->chip) {
  139. case S3_PROSAVAGE:
  140. case S3_PROSAVAGEDDR:
  141. case S3_TWISTER:
  142. par->chan.reg = CR_SERIAL2;
  143. par->chan.ioaddr = par->mmio.vbase;
  144. par->chan.algo.setsda = prosavage_gpio_setsda;
  145. par->chan.algo.setscl = prosavage_gpio_setscl;
  146. par->chan.algo.getsda = prosavage_gpio_getsda;
  147. par->chan.algo.getscl = prosavage_gpio_getscl;
  148. break;
  149. case S3_SAVAGE4:
  150. par->chan.reg = CR_SERIAL1;
  151. if (par->pcidev->revision > 1 && !(VGArCR(0xa6, par) & 0x40))
  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 S3_SAVAGE2000:
  160. par->chan.reg = MM_SERIAL1;
  161. par->chan.ioaddr = par->mmio.vbase;
  162. par->chan.algo.setsda = savage4_gpio_setsda;
  163. par->chan.algo.setscl = savage4_gpio_setscl;
  164. par->chan.algo.getsda = savage4_gpio_getsda;
  165. par->chan.algo.getscl = savage4_gpio_getscl;
  166. break;
  167. default:
  168. par->chan.par = NULL;
  169. }
  170. savage_setup_i2c_bus(&par->chan, "SAVAGE DDC2");
  171. }
  172. void savagefb_delete_i2c_busses(struct fb_info *info)
  173. {
  174. struct savagefb_par *par = info->par;
  175. if (par->chan.par)
  176. i2c_del_adapter(&par->chan.adapter);
  177. par->chan.par = NULL;
  178. }
  179. int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid)
  180. {
  181. struct savagefb_par *par = info->par;
  182. u8 *edid;
  183. if (par->chan.par)
  184. edid = fb_ddc_read(&par->chan.adapter);
  185. else
  186. edid = NULL;
  187. if (!edid) {
  188. /* try to get from firmware */
  189. const u8 *e = fb_firmware_edid(info->device);
  190. if (e)
  191. edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
  192. }
  193. *out_edid = edid;
  194. return (edid) ? 0 : 1;
  195. }
  196. MODULE_LICENSE("GPL");