i2c-matroxfb.c 6.1 KB

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