intel_i2c.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
  3. * Copyright © 2006-2008 Intel Corporation
  4. * Jesse Barnes <jesse.barnes@intel.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. * Authors:
  26. * Eric Anholt <eric@anholt.net>
  27. */
  28. #include <linux/i2c.h>
  29. #include <linux/i2c-id.h>
  30. #include <linux/i2c-algo-bit.h>
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "intel_drv.h"
  34. #include "i915_drm.h"
  35. #include "i915_drv.h"
  36. /*
  37. * Intel GPIO access functions
  38. */
  39. #define I2C_RISEFALL_TIME 20
  40. static int get_clock(void *data)
  41. {
  42. struct intel_i2c_chan *chan = data;
  43. struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
  44. u32 val;
  45. val = I915_READ(chan->reg);
  46. return ((val & GPIO_CLOCK_VAL_IN) != 0);
  47. }
  48. static int get_data(void *data)
  49. {
  50. struct intel_i2c_chan *chan = data;
  51. struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
  52. u32 val;
  53. val = I915_READ(chan->reg);
  54. return ((val & GPIO_DATA_VAL_IN) != 0);
  55. }
  56. static void set_clock(void *data, int state_high)
  57. {
  58. struct intel_i2c_chan *chan = data;
  59. struct drm_device *dev = chan->drm_dev;
  60. struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
  61. u32 reserved = 0, clock_bits;
  62. /* On most chips, these bits must be preserved in software. */
  63. if (!IS_I830(dev) && !IS_845G(dev))
  64. reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
  65. GPIO_CLOCK_PULLUP_DISABLE);
  66. if (state_high)
  67. clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
  68. else
  69. clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
  70. GPIO_CLOCK_VAL_MASK;
  71. I915_WRITE(chan->reg, reserved | clock_bits);
  72. udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
  73. }
  74. static void set_data(void *data, int state_high)
  75. {
  76. struct intel_i2c_chan *chan = data;
  77. struct drm_device *dev = chan->drm_dev;
  78. struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
  79. u32 reserved = 0, data_bits;
  80. /* On most chips, these bits must be preserved in software. */
  81. if (!IS_I830(dev) && !IS_845G(dev))
  82. reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
  83. GPIO_CLOCK_PULLUP_DISABLE);
  84. if (state_high)
  85. data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
  86. else
  87. data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
  88. GPIO_DATA_VAL_MASK;
  89. I915_WRITE(chan->reg, reserved | data_bits);
  90. udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
  91. }
  92. /**
  93. * intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
  94. * @dev: DRM device
  95. * @output: driver specific output device
  96. * @reg: GPIO reg to use
  97. * @name: name for this bus
  98. *
  99. * Creates and registers a new i2c bus with the Linux i2c layer, for use
  100. * in output probing and control (e.g. DDC or SDVO control functions).
  101. *
  102. * Possible values for @reg include:
  103. * %GPIOA
  104. * %GPIOB
  105. * %GPIOC
  106. * %GPIOD
  107. * %GPIOE
  108. * %GPIOF
  109. * %GPIOG
  110. * %GPIOH
  111. * see PRM for details on how these different busses are used.
  112. */
  113. struct intel_i2c_chan *intel_i2c_create(struct drm_device *dev, const u32 reg,
  114. const char *name)
  115. {
  116. struct intel_i2c_chan *chan;
  117. chan = kzalloc(sizeof(struct intel_i2c_chan), GFP_KERNEL);
  118. if (!chan)
  119. goto out_free;
  120. chan->drm_dev = dev;
  121. chan->reg = reg;
  122. snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
  123. chan->adapter.owner = THIS_MODULE;
  124. #ifndef I2C_HW_B_INTELFB
  125. #define I2C_HW_B_INTELFB I2C_HW_B_I810
  126. #endif
  127. chan->adapter.id = I2C_HW_B_INTELFB;
  128. chan->adapter.algo_data = &chan->algo;
  129. chan->adapter.dev.parent = &dev->pdev->dev;
  130. chan->algo.setsda = set_data;
  131. chan->algo.setscl = set_clock;
  132. chan->algo.getsda = get_data;
  133. chan->algo.getscl = get_clock;
  134. chan->algo.udelay = 20;
  135. chan->algo.timeout = usecs_to_jiffies(2200);
  136. chan->algo.data = chan;
  137. i2c_set_adapdata(&chan->adapter, chan);
  138. if(i2c_bit_add_bus(&chan->adapter))
  139. goto out_free;
  140. /* JJJ: raise SCL and SDA? */
  141. set_data(chan, 1);
  142. set_clock(chan, 1);
  143. udelay(20);
  144. return chan;
  145. out_free:
  146. kfree(chan);
  147. return NULL;
  148. }
  149. /**
  150. * intel_i2c_destroy - unregister and free i2c bus resources
  151. * @output: channel to free
  152. *
  153. * Unregister the adapter from the i2c layer, then free the structure.
  154. */
  155. void intel_i2c_destroy(struct intel_i2c_chan *chan)
  156. {
  157. if (!chan)
  158. return;
  159. i2c_del_adapter(&chan->adapter);
  160. kfree(chan);
  161. }