i2c-matroxfb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. NULL,
  84. matroxfb_gpio_setsda,
  85. matroxfb_gpio_setscl,
  86. matroxfb_gpio_getsda,
  87. matroxfb_gpio_getscl,
  88. 10, 10, 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 err;
  93. b->minfo = minfo;
  94. b->mask.data = data;
  95. b->mask.clock = clock;
  96. b->adapter = matrox_i2c_adapter_template;
  97. snprintf(b->adapter.name, I2C_NAME_SIZE, name,
  98. minfo->fbcon.node);
  99. i2c_set_adapdata(&b->adapter, b);
  100. b->adapter.algo_data = &b->bac;
  101. b->bac = matrox_i2c_algo_template;
  102. b->bac.data = b;
  103. err = i2c_bit_add_bus(&b->adapter);
  104. b->initialized = !err;
  105. return err;
  106. }
  107. static void i2c_bit_bus_del(struct i2c_bit_adapter* b) {
  108. if (b->initialized) {
  109. i2c_bit_del_bus(&b->adapter);
  110. b->initialized = 0;
  111. }
  112. }
  113. static inline void i2c_maven_done(struct matroxfb_dh_maven_info* minfo2) {
  114. i2c_bit_bus_del(&minfo2->maven);
  115. }
  116. static inline void i2c_ddc1_done(struct matroxfb_dh_maven_info* minfo2) {
  117. i2c_bit_bus_del(&minfo2->ddc1);
  118. }
  119. static inline void i2c_ddc2_done(struct matroxfb_dh_maven_info* minfo2) {
  120. i2c_bit_bus_del(&minfo2->ddc2);
  121. }
  122. static void* i2c_matroxfb_probe(struct matrox_fb_info* minfo) {
  123. int err;
  124. unsigned long flags;
  125. struct matroxfb_dh_maven_info* m2info;
  126. m2info = (struct matroxfb_dh_maven_info*)kmalloc(sizeof(*m2info), GFP_KERNEL);
  127. if (!m2info)
  128. return NULL;
  129. matroxfb_DAC_lock_irqsave(flags);
  130. matroxfb_DAC_out(PMINFO DAC_XGENIODATA, 0xFF);
  131. matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL, 0x00);
  132. matroxfb_DAC_unlock_irqrestore(flags);
  133. memset(m2info, 0, sizeof(*m2info));
  134. switch (ACCESS_FBINFO(chip)) {
  135. case MGA_2064:
  136. case MGA_2164:
  137. err = i2c_bus_reg(&m2info->ddc1, minfo, DDC1B_DATA, DDC1B_CLK, "DDC:fb%u #0");
  138. break;
  139. default:
  140. err = i2c_bus_reg(&m2info->ddc1, minfo, DDC1_DATA, DDC1_CLK, "DDC:fb%u #0");
  141. break;
  142. }
  143. if (err)
  144. goto fail_ddc1;
  145. if (ACCESS_FBINFO(devflags.dualhead)) {
  146. err = i2c_bus_reg(&m2info->ddc2, minfo, DDC2_DATA, DDC2_CLK, "DDC:fb%u #1");
  147. if (err == -ENODEV) {
  148. printk(KERN_INFO "i2c-matroxfb: VGA->TV plug detected, DDC unavailable.\n");
  149. } else if (err)
  150. printk(KERN_INFO "i2c-matroxfb: Could not register secondary output i2c bus. Continuing anyway.\n");
  151. /* Register maven bus even on G450/G550 */
  152. err = i2c_bus_reg(&m2info->maven, minfo, MAT_DATA, MAT_CLK, "MAVEN:fb%u");
  153. if (err)
  154. printk(KERN_INFO "i2c-matroxfb: Could not register Maven i2c bus. Continuing anyway.\n");
  155. }
  156. return m2info;
  157. fail_ddc1:;
  158. kfree(m2info);
  159. printk(KERN_ERR "i2c-matroxfb: Could not register primary adapter DDC bus.\n");
  160. return NULL;
  161. }
  162. static void i2c_matroxfb_remove(struct matrox_fb_info* minfo, void* data) {
  163. struct matroxfb_dh_maven_info* m2info = data;
  164. i2c_maven_done(m2info);
  165. i2c_ddc2_done(m2info);
  166. i2c_ddc1_done(m2info);
  167. kfree(m2info);
  168. }
  169. static struct matroxfb_driver i2c_matroxfb = {
  170. .node = LIST_HEAD_INIT(i2c_matroxfb.node),
  171. .name = "i2c-matroxfb",
  172. .probe = i2c_matroxfb_probe,
  173. .remove = i2c_matroxfb_remove,
  174. };
  175. static int __init i2c_matroxfb_init(void) {
  176. if (matroxfb_register_driver(&i2c_matroxfb)) {
  177. printk(KERN_ERR "i2c-matroxfb: failed to register driver\n");
  178. return -ENXIO;
  179. }
  180. return 0;
  181. }
  182. static void __exit i2c_matroxfb_exit(void) {
  183. matroxfb_unregister_driver(&i2c_matroxfb);
  184. }
  185. MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
  186. MODULE_DESCRIPTION("Support module providing I2C buses present on Matrox videocards");
  187. module_init(i2c_matroxfb_init);
  188. module_exit(i2c_matroxfb_exit);
  189. /* no __setup required */
  190. MODULE_LICENSE("GPL");