i2c-matroxfb.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. *
  3. * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200, G400 and G450.
  4. *
  5. * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
  6. *
  7. * Version: 1.64 2002/06/10
  8. *
  9. * See matroxfb_base.c for contributors.
  10. *
  11. */
  12. #include "matroxfb_base.h"
  13. #include "matroxfb_maven.h"
  14. #include <linux/i2c.h>
  15. #include <linux/i2c-algo-bit.h>
  16. /* MGA-TVO I2C for G200, G400 */
  17. #define MAT_CLK 0x20
  18. #define MAT_DATA 0x10
  19. /* primary head DDC for Mystique(?), G100, G200, G400 */
  20. #define DDC1_CLK 0x08
  21. #define DDC1_DATA 0x02
  22. /* primary head DDC for Millennium, Millennium II */
  23. #define DDC1B_CLK 0x10
  24. #define DDC1B_DATA 0x04
  25. /* secondary head DDC for G400 */
  26. #define DDC2_CLK 0x04
  27. #define DDC2_DATA 0x01
  28. /******************************************************/
  29. struct matroxfb_dh_maven_info {
  30. struct i2c_bit_adapter maven;
  31. struct i2c_bit_adapter ddc1;
  32. struct i2c_bit_adapter ddc2;
  33. };
  34. static int matroxfb_read_gpio(struct matrox_fb_info* minfo) {
  35. unsigned long flags;
  36. int v;
  37. matroxfb_DAC_lock_irqsave(flags);
  38. v = matroxfb_DAC_in(PMINFO DAC_XGENIODATA);
  39. matroxfb_DAC_unlock_irqrestore(flags);
  40. return v;
  41. }
  42. static void matroxfb_set_gpio(struct matrox_fb_info* minfo, int mask, int val) {
  43. unsigned long flags;
  44. int v;
  45. matroxfb_DAC_lock_irqsave(flags);
  46. v = (matroxfb_DAC_in(PMINFO DAC_XGENIOCTRL) & mask) | val;
  47. matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL, v);
  48. /* We must reset GENIODATA very often... XFree plays with this register */
  49. matroxfb_DAC_out(PMINFO DAC_XGENIODATA, 0x00);
  50. matroxfb_DAC_unlock_irqrestore(flags);
  51. }
  52. /* software I2C functions */
  53. static inline void matroxfb_i2c_set(struct matrox_fb_info* minfo, int mask, int state) {
  54. if (state)
  55. state = 0;
  56. else
  57. state = mask;
  58. matroxfb_set_gpio(minfo, ~mask, state);
  59. }
  60. static void matroxfb_gpio_setsda(void* data, int state) {
  61. struct i2c_bit_adapter* b = data;
  62. matroxfb_i2c_set(b->minfo, b->mask.data, state);
  63. }
  64. static void matroxfb_gpio_setscl(void* data, int state) {
  65. struct i2c_bit_adapter* b = data;
  66. matroxfb_i2c_set(b->minfo, b->mask.clock, state);
  67. }
  68. static int matroxfb_gpio_getsda(void* data) {
  69. struct i2c_bit_adapter* b = data;
  70. return (matroxfb_read_gpio(b->minfo) & b->mask.data) ? 1 : 0;
  71. }
  72. static int matroxfb_gpio_getscl(void* data) {
  73. struct i2c_bit_adapter* b = data;
  74. return (matroxfb_read_gpio(b->minfo) & b->mask.clock) ? 1 : 0;
  75. }
  76. static struct i2c_adapter matrox_i2c_adapter_template =
  77. {
  78. .owner = THIS_MODULE,
  79. .id = I2C_HW_B_G400,
  80. };
  81. static struct i2c_algo_bit_data matrox_i2c_algo_template =
  82. {
  83. .setsda = matroxfb_gpio_setsda,
  84. .setscl = matroxfb_gpio_setscl,
  85. .getsda = matroxfb_gpio_getsda,
  86. .getscl = matroxfb_gpio_getscl,
  87. .udelay = 10,
  88. .timeout = 100,
  89. };
  90. static int i2c_bus_reg(struct i2c_bit_adapter* b, struct matrox_fb_info* minfo,
  91. unsigned int data, unsigned int clock, const char *name,
  92. int class)
  93. {
  94. int err;
  95. b->minfo = minfo;
  96. b->mask.data = data;
  97. b->mask.clock = clock;
  98. b->adapter = matrox_i2c_adapter_template;
  99. snprintf(b->adapter.name, sizeof(b->adapter.name), name,
  100. minfo->fbcon.node);
  101. i2c_set_adapdata(&b->adapter, b);
  102. b->adapter.class = class;
  103. b->adapter.algo_data = &b->bac;
  104. b->adapter.dev.parent = &ACCESS_FBINFO(pcidev)->dev;
  105. b->bac = matrox_i2c_algo_template;
  106. b->bac.data = b;
  107. err = i2c_bit_add_bus(&b->adapter);
  108. b->initialized = !err;
  109. return err;
  110. }
  111. static void i2c_bit_bus_del(struct i2c_bit_adapter* b) {
  112. if (b->initialized) {
  113. i2c_del_adapter(&b->adapter);
  114. b->initialized = 0;
  115. }
  116. }
  117. static inline void i2c_maven_done(struct matroxfb_dh_maven_info* minfo2) {
  118. i2c_bit_bus_del(&minfo2->maven);
  119. }
  120. static inline void i2c_ddc1_done(struct matroxfb_dh_maven_info* minfo2) {
  121. i2c_bit_bus_del(&minfo2->ddc1);
  122. }
  123. static inline void i2c_ddc2_done(struct matroxfb_dh_maven_info* minfo2) {
  124. i2c_bit_bus_del(&minfo2->ddc2);
  125. }
  126. static void* i2c_matroxfb_probe(struct matrox_fb_info* minfo) {
  127. int err;
  128. unsigned long flags;
  129. struct matroxfb_dh_maven_info* m2info;
  130. m2info = kzalloc(sizeof(*m2info), GFP_KERNEL);
  131. if (!m2info)
  132. return NULL;
  133. matroxfb_DAC_lock_irqsave(flags);
  134. matroxfb_DAC_out(PMINFO DAC_XGENIODATA, 0xFF);
  135. matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL, 0x00);
  136. matroxfb_DAC_unlock_irqrestore(flags);
  137. switch (ACCESS_FBINFO(chip)) {
  138. case MGA_2064:
  139. case MGA_2164:
  140. err = i2c_bus_reg(&m2info->ddc1, minfo,
  141. DDC1B_DATA, DDC1B_CLK,
  142. "DDC:fb%u #0", I2C_CLASS_DDC);
  143. break;
  144. default:
  145. err = i2c_bus_reg(&m2info->ddc1, minfo,
  146. DDC1_DATA, DDC1_CLK,
  147. "DDC:fb%u #0", I2C_CLASS_DDC);
  148. break;
  149. }
  150. if (err)
  151. goto fail_ddc1;
  152. if (ACCESS_FBINFO(devflags.dualhead)) {
  153. err = i2c_bus_reg(&m2info->ddc2, minfo,
  154. DDC2_DATA, DDC2_CLK,
  155. "DDC:fb%u #1", I2C_CLASS_DDC);
  156. if (err == -ENODEV) {
  157. printk(KERN_INFO "i2c-matroxfb: VGA->TV plug detected, DDC unavailable.\n");
  158. } else if (err)
  159. printk(KERN_INFO "i2c-matroxfb: Could not register secondary output i2c bus. Continuing anyway.\n");
  160. /* Register maven bus even on G450/G550 */
  161. err = i2c_bus_reg(&m2info->maven, minfo,
  162. MAT_DATA, MAT_CLK, "MAVEN:fb%u", 0);
  163. if (err)
  164. printk(KERN_INFO "i2c-matroxfb: Could not register Maven i2c bus. Continuing anyway.\n");
  165. }
  166. return m2info;
  167. fail_ddc1:;
  168. kfree(m2info);
  169. printk(KERN_ERR "i2c-matroxfb: Could not register primary adapter DDC bus.\n");
  170. return NULL;
  171. }
  172. static void i2c_matroxfb_remove(struct matrox_fb_info* minfo, void* data) {
  173. struct matroxfb_dh_maven_info* m2info = data;
  174. i2c_maven_done(m2info);
  175. i2c_ddc2_done(m2info);
  176. i2c_ddc1_done(m2info);
  177. kfree(m2info);
  178. }
  179. static struct matroxfb_driver i2c_matroxfb = {
  180. .node = LIST_HEAD_INIT(i2c_matroxfb.node),
  181. .name = "i2c-matroxfb",
  182. .probe = i2c_matroxfb_probe,
  183. .remove = i2c_matroxfb_remove,
  184. };
  185. static int __init i2c_matroxfb_init(void) {
  186. if (matroxfb_register_driver(&i2c_matroxfb)) {
  187. printk(KERN_ERR "i2c-matroxfb: failed to register driver\n");
  188. return -ENXIO;
  189. }
  190. return 0;
  191. }
  192. static void __exit i2c_matroxfb_exit(void) {
  193. matroxfb_unregister_driver(&i2c_matroxfb);
  194. }
  195. MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
  196. MODULE_DESCRIPTION("Support module providing I2C buses present on Matrox videocards");
  197. module_init(i2c_matroxfb_init);
  198. module_exit(i2c_matroxfb_exit);
  199. /* no __setup required */
  200. MODULE_LICENSE("GPL");