i2c-matroxfb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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(minfo, 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(minfo, DAC_XGENIOCTRL) & mask) | val;
  47. matroxfb_DAC_out(minfo, DAC_XGENIOCTRL, v);
  48. /* We must reset GENIODATA very often... XFree plays with this register */
  49. matroxfb_DAC_out(minfo, 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 const struct i2c_algo_bit_data matrox_i2c_algo_template =
  77. {
  78. .setsda = matroxfb_gpio_setsda,
  79. .setscl = matroxfb_gpio_setscl,
  80. .getsda = matroxfb_gpio_getsda,
  81. .getscl = matroxfb_gpio_getscl,
  82. .udelay = 10,
  83. .timeout = 100,
  84. };
  85. static int i2c_bus_reg(struct i2c_bit_adapter* b, struct matrox_fb_info* minfo,
  86. unsigned int data, unsigned int clock, const char *name,
  87. int class)
  88. {
  89. int err;
  90. b->minfo = minfo;
  91. b->mask.data = data;
  92. b->mask.clock = clock;
  93. b->adapter.owner = THIS_MODULE;
  94. snprintf(b->adapter.name, sizeof(b->adapter.name), name,
  95. minfo->fbcon.node);
  96. i2c_set_adapdata(&b->adapter, b);
  97. b->adapter.class = class;
  98. b->adapter.algo_data = &b->bac;
  99. b->adapter.dev.parent = &minfo->pcidev->dev;
  100. b->bac = matrox_i2c_algo_template;
  101. b->bac.data = b;
  102. err = i2c_bit_add_bus(&b->adapter);
  103. b->initialized = !err;
  104. return err;
  105. }
  106. static void i2c_bit_bus_del(struct i2c_bit_adapter* b) {
  107. if (b->initialized) {
  108. i2c_del_adapter(&b->adapter);
  109. b->initialized = 0;
  110. }
  111. }
  112. static inline void i2c_maven_done(struct matroxfb_dh_maven_info* minfo2) {
  113. i2c_bit_bus_del(&minfo2->maven);
  114. }
  115. static inline void i2c_ddc1_done(struct matroxfb_dh_maven_info* minfo2) {
  116. i2c_bit_bus_del(&minfo2->ddc1);
  117. }
  118. static inline void i2c_ddc2_done(struct matroxfb_dh_maven_info* minfo2) {
  119. i2c_bit_bus_del(&minfo2->ddc2);
  120. }
  121. static void* i2c_matroxfb_probe(struct matrox_fb_info* minfo) {
  122. int err;
  123. unsigned long flags;
  124. struct matroxfb_dh_maven_info* m2info;
  125. m2info = kzalloc(sizeof(*m2info), GFP_KERNEL);
  126. if (!m2info)
  127. return NULL;
  128. matroxfb_DAC_lock_irqsave(flags);
  129. matroxfb_DAC_out(minfo, DAC_XGENIODATA, 0xFF);
  130. matroxfb_DAC_out(minfo, DAC_XGENIOCTRL, 0x00);
  131. matroxfb_DAC_unlock_irqrestore(flags);
  132. switch (minfo->chip) {
  133. case MGA_2064:
  134. case MGA_2164:
  135. err = i2c_bus_reg(&m2info->ddc1, minfo,
  136. DDC1B_DATA, DDC1B_CLK,
  137. "DDC:fb%u #0", I2C_CLASS_DDC);
  138. break;
  139. default:
  140. err = i2c_bus_reg(&m2info->ddc1, minfo,
  141. DDC1_DATA, DDC1_CLK,
  142. "DDC:fb%u #0", I2C_CLASS_DDC);
  143. break;
  144. }
  145. if (err)
  146. goto fail_ddc1;
  147. if (minfo->devflags.dualhead) {
  148. err = i2c_bus_reg(&m2info->ddc2, minfo,
  149. DDC2_DATA, DDC2_CLK,
  150. "DDC:fb%u #1", I2C_CLASS_DDC);
  151. if (err == -ENODEV) {
  152. printk(KERN_INFO "i2c-matroxfb: VGA->TV plug detected, DDC unavailable.\n");
  153. } else if (err)
  154. printk(KERN_INFO "i2c-matroxfb: Could not register secondary output i2c bus. Continuing anyway.\n");
  155. /* Register maven bus even on G450/G550 */
  156. err = i2c_bus_reg(&m2info->maven, minfo,
  157. MAT_DATA, MAT_CLK, "MAVEN:fb%u", 0);
  158. if (err)
  159. printk(KERN_INFO "i2c-matroxfb: Could not register Maven i2c bus. Continuing anyway.\n");
  160. else {
  161. struct i2c_board_info maven_info = {
  162. I2C_BOARD_INFO("maven", 0x1b),
  163. };
  164. unsigned short const addr_list[2] = {
  165. 0x1b, I2C_CLIENT_END
  166. };
  167. i2c_new_probed_device(&m2info->maven.adapter,
  168. &maven_info, addr_list);
  169. }
  170. }
  171. return m2info;
  172. fail_ddc1:;
  173. kfree(m2info);
  174. printk(KERN_ERR "i2c-matroxfb: Could not register primary adapter DDC bus.\n");
  175. return NULL;
  176. }
  177. static void i2c_matroxfb_remove(struct matrox_fb_info* minfo, void* data) {
  178. struct matroxfb_dh_maven_info* m2info = data;
  179. i2c_maven_done(m2info);
  180. i2c_ddc2_done(m2info);
  181. i2c_ddc1_done(m2info);
  182. kfree(m2info);
  183. }
  184. static struct matroxfb_driver i2c_matroxfb = {
  185. .node = LIST_HEAD_INIT(i2c_matroxfb.node),
  186. .name = "i2c-matroxfb",
  187. .probe = i2c_matroxfb_probe,
  188. .remove = i2c_matroxfb_remove,
  189. };
  190. static int __init i2c_matroxfb_init(void) {
  191. if (matroxfb_register_driver(&i2c_matroxfb)) {
  192. printk(KERN_ERR "i2c-matroxfb: failed to register driver\n");
  193. return -ENXIO;
  194. }
  195. return 0;
  196. }
  197. static void __exit i2c_matroxfb_exit(void) {
  198. matroxfb_unregister_driver(&i2c_matroxfb);
  199. }
  200. MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
  201. MODULE_DESCRIPTION("Support module providing I2C buses present on Matrox videocards");
  202. module_init(i2c_matroxfb_init);
  203. module_exit(i2c_matroxfb_exit);
  204. /* no __setup required */
  205. MODULE_LICENSE("GPL");