intel_i2c.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Authors:
  18. * Eric Anholt <eric@anholt.net>
  19. */
  20. #include <linux/export.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c-algo-bit.h>
  23. #include "psb_drv.h"
  24. #include "psb_intel_reg.h"
  25. /*
  26. * Intel GPIO access functions
  27. */
  28. #define I2C_RISEFALL_TIME 20
  29. static int get_clock(void *data)
  30. {
  31. struct psb_intel_i2c_chan *chan = data;
  32. struct drm_device *dev = chan->drm_dev;
  33. u32 val;
  34. val = REG_READ(chan->reg);
  35. return (val & GPIO_CLOCK_VAL_IN) != 0;
  36. }
  37. static int get_data(void *data)
  38. {
  39. struct psb_intel_i2c_chan *chan = data;
  40. struct drm_device *dev = chan->drm_dev;
  41. u32 val;
  42. val = REG_READ(chan->reg);
  43. return (val & GPIO_DATA_VAL_IN) != 0;
  44. }
  45. static void set_clock(void *data, int state_high)
  46. {
  47. struct psb_intel_i2c_chan *chan = data;
  48. struct drm_device *dev = chan->drm_dev;
  49. u32 reserved = 0, clock_bits;
  50. /* On most chips, these bits must be preserved in software. */
  51. reserved =
  52. REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
  53. GPIO_CLOCK_PULLUP_DISABLE);
  54. if (state_high)
  55. clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
  56. else
  57. clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
  58. GPIO_CLOCK_VAL_MASK;
  59. REG_WRITE(chan->reg, reserved | clock_bits);
  60. udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
  61. }
  62. static void set_data(void *data, int state_high)
  63. {
  64. struct psb_intel_i2c_chan *chan = data;
  65. struct drm_device *dev = chan->drm_dev;
  66. u32 reserved = 0, data_bits;
  67. /* On most chips, these bits must be preserved in software. */
  68. reserved =
  69. REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
  70. GPIO_CLOCK_PULLUP_DISABLE);
  71. if (state_high)
  72. data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
  73. else
  74. data_bits =
  75. GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
  76. GPIO_DATA_VAL_MASK;
  77. REG_WRITE(chan->reg, reserved | data_bits);
  78. udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
  79. }
  80. /**
  81. * psb_intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
  82. * @dev: DRM device
  83. * @output: driver specific output device
  84. * @reg: GPIO reg to use
  85. * @name: name for this bus
  86. *
  87. * Creates and registers a new i2c bus with the Linux i2c layer, for use
  88. * in output probing and control (e.g. DDC or SDVO control functions).
  89. *
  90. * Possible values for @reg include:
  91. * %GPIOA
  92. * %GPIOB
  93. * %GPIOC
  94. * %GPIOD
  95. * %GPIOE
  96. * %GPIOF
  97. * %GPIOG
  98. * %GPIOH
  99. * see PRM for details on how these different busses are used.
  100. */
  101. struct psb_intel_i2c_chan *psb_intel_i2c_create(struct drm_device *dev,
  102. const u32 reg, const char *name)
  103. {
  104. struct psb_intel_i2c_chan *chan;
  105. chan = kzalloc(sizeof(struct psb_intel_i2c_chan), GFP_KERNEL);
  106. if (!chan)
  107. goto out_free;
  108. chan->drm_dev = dev;
  109. chan->reg = reg;
  110. snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
  111. chan->adapter.owner = THIS_MODULE;
  112. chan->adapter.algo_data = &chan->algo;
  113. chan->adapter.dev.parent = &dev->pdev->dev;
  114. chan->algo.setsda = set_data;
  115. chan->algo.setscl = set_clock;
  116. chan->algo.getsda = get_data;
  117. chan->algo.getscl = get_clock;
  118. chan->algo.udelay = 20;
  119. chan->algo.timeout = usecs_to_jiffies(2200);
  120. chan->algo.data = chan;
  121. i2c_set_adapdata(&chan->adapter, chan);
  122. if (i2c_bit_add_bus(&chan->adapter))
  123. goto out_free;
  124. /* JJJ: raise SCL and SDA? */
  125. set_data(chan, 1);
  126. set_clock(chan, 1);
  127. udelay(20);
  128. return chan;
  129. out_free:
  130. kfree(chan);
  131. return NULL;
  132. }
  133. /**
  134. * psb_intel_i2c_destroy - unregister and free i2c bus resources
  135. * @output: channel to free
  136. *
  137. * Unregister the adapter from the i2c layer, then free the structure.
  138. */
  139. void psb_intel_i2c_destroy(struct psb_intel_i2c_chan *chan)
  140. {
  141. if (!chan)
  142. return;
  143. i2c_del_adapter(&chan->adapter);
  144. kfree(chan);
  145. }