mgag200_i2c.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <linux/export.h>
  29. #include <linux/i2c.h>
  30. #include <linux/i2c-algo-bit.h>
  31. #include <drm/drmP.h>
  32. #include "mgag200_drv.h"
  33. static int mga_i2c_read_gpio(struct mga_device *mdev)
  34. {
  35. WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
  36. return RREG8(DAC_DATA);
  37. }
  38. static void mga_i2c_set_gpio(struct mga_device *mdev, int mask, int val)
  39. {
  40. int tmp;
  41. WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
  42. tmp = (RREG8(DAC_DATA) & mask) | val;
  43. WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
  44. WREG_DAC(MGA1064_GEN_IO_DATA, 0);
  45. }
  46. static inline void mga_i2c_set(struct mga_device *mdev, int mask, int state)
  47. {
  48. if (state)
  49. state = 0;
  50. else
  51. state = mask;
  52. mga_i2c_set_gpio(mdev, ~mask, state);
  53. }
  54. static void mga_gpio_setsda(void *data, int state)
  55. {
  56. struct mga_i2c_chan *i2c = data;
  57. struct mga_device *mdev = i2c->dev->dev_private;
  58. mga_i2c_set(mdev, i2c->data, state);
  59. }
  60. static void mga_gpio_setscl(void *data, int state)
  61. {
  62. struct mga_i2c_chan *i2c = data;
  63. struct mga_device *mdev = i2c->dev->dev_private;
  64. mga_i2c_set(mdev, i2c->clock, state);
  65. }
  66. static int mga_gpio_getsda(void *data)
  67. {
  68. struct mga_i2c_chan *i2c = data;
  69. struct mga_device *mdev = i2c->dev->dev_private;
  70. return (mga_i2c_read_gpio(mdev) & i2c->data) ? 1 : 0;
  71. }
  72. static int mga_gpio_getscl(void *data)
  73. {
  74. struct mga_i2c_chan *i2c = data;
  75. struct mga_device *mdev = i2c->dev->dev_private;
  76. return (mga_i2c_read_gpio(mdev) & i2c->clock) ? 1 : 0;
  77. }
  78. struct mga_i2c_chan *mgag200_i2c_create(struct drm_device *dev)
  79. {
  80. struct mga_device *mdev = dev->dev_private;
  81. struct mga_i2c_chan *i2c;
  82. int ret;
  83. int data, clock;
  84. WREG_DAC(MGA1064_GEN_IO_CTL2, 1);
  85. WREG_DAC(MGA1064_GEN_IO_DATA, 0xff);
  86. WREG_DAC(MGA1064_GEN_IO_CTL, 0);
  87. switch (mdev->type) {
  88. case G200_SE_A:
  89. case G200_SE_B:
  90. case G200_EV:
  91. case G200_WB:
  92. case G200_EW3:
  93. data = 1;
  94. clock = 2;
  95. break;
  96. case G200_EH:
  97. case G200_ER:
  98. data = 2;
  99. clock = 1;
  100. break;
  101. default:
  102. data = 2;
  103. clock = 8;
  104. break;
  105. }
  106. i2c = kzalloc(sizeof(struct mga_i2c_chan), GFP_KERNEL);
  107. if (!i2c)
  108. return NULL;
  109. i2c->data = data;
  110. i2c->clock = clock;
  111. i2c->adapter.owner = THIS_MODULE;
  112. i2c->adapter.class = I2C_CLASS_DDC;
  113. i2c->adapter.dev.parent = &dev->pdev->dev;
  114. i2c->dev = dev;
  115. i2c_set_adapdata(&i2c->adapter, i2c);
  116. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name), "mga i2c");
  117. i2c->adapter.algo_data = &i2c->bit;
  118. i2c->bit.udelay = 10;
  119. i2c->bit.timeout = 2;
  120. i2c->bit.data = i2c;
  121. i2c->bit.setsda = mga_gpio_setsda;
  122. i2c->bit.setscl = mga_gpio_setscl;
  123. i2c->bit.getsda = mga_gpio_getsda;
  124. i2c->bit.getscl = mga_gpio_getscl;
  125. ret = i2c_bit_add_bus(&i2c->adapter);
  126. if (ret) {
  127. kfree(i2c);
  128. i2c = NULL;
  129. }
  130. return i2c;
  131. }
  132. void mgag200_i2c_destroy(struct mga_i2c_chan *i2c)
  133. {
  134. if (!i2c)
  135. return;
  136. i2c_del_adapter(&i2c->adapter);
  137. kfree(i2c);
  138. }