i2c-matroxfb.c 6.2 KB

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