i2c-matroxfb.c 6.3 KB

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