intel_i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2006-2007 Intel Corporation
  4. *
  5. * Authors:
  6. * Eric Anholt <[email protected]>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/export.h>
  10. #include <linux/i2c-algo-bit.h>
  11. #include <linux/i2c.h>
  12. #include "psb_drv.h"
  13. #include "psb_intel_reg.h"
  14. /*
  15. * Intel GPIO access functions
  16. */
  17. #define I2C_RISEFALL_TIME 20
  18. static int get_clock(void *data)
  19. {
  20. struct gma_i2c_chan *chan = data;
  21. struct drm_device *dev = chan->drm_dev;
  22. u32 val;
  23. val = REG_READ(chan->reg);
  24. return (val & GPIO_CLOCK_VAL_IN) != 0;
  25. }
  26. static int get_data(void *data)
  27. {
  28. struct gma_i2c_chan *chan = data;
  29. struct drm_device *dev = chan->drm_dev;
  30. u32 val;
  31. val = REG_READ(chan->reg);
  32. return (val & GPIO_DATA_VAL_IN) != 0;
  33. }
  34. static void set_clock(void *data, int state_high)
  35. {
  36. struct gma_i2c_chan *chan = data;
  37. struct drm_device *dev = chan->drm_dev;
  38. u32 reserved = 0, clock_bits;
  39. /* On most chips, these bits must be preserved in software. */
  40. reserved =
  41. REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
  42. GPIO_CLOCK_PULLUP_DISABLE);
  43. if (state_high)
  44. clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
  45. else
  46. clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
  47. GPIO_CLOCK_VAL_MASK;
  48. REG_WRITE(chan->reg, reserved | clock_bits);
  49. udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
  50. }
  51. static void set_data(void *data, int state_high)
  52. {
  53. struct gma_i2c_chan *chan = data;
  54. struct drm_device *dev = chan->drm_dev;
  55. u32 reserved = 0, data_bits;
  56. /* On most chips, these bits must be preserved in software. */
  57. reserved =
  58. REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
  59. GPIO_CLOCK_PULLUP_DISABLE);
  60. if (state_high)
  61. data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
  62. else
  63. data_bits =
  64. GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
  65. GPIO_DATA_VAL_MASK;
  66. REG_WRITE(chan->reg, reserved | data_bits);
  67. udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
  68. }
  69. /**
  70. * gma_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
  71. * @dev: DRM device
  72. * @reg: GPIO reg to use
  73. * @name: name for this bus
  74. *
  75. * Creates and registers a new i2c bus with the Linux i2c layer, for use
  76. * in output probing and control (e.g. DDC or SDVO control functions).
  77. *
  78. * Possible values for @reg include:
  79. * %GPIOA
  80. * %GPIOB
  81. * %GPIOC
  82. * %GPIOD
  83. * %GPIOE
  84. * %GPIOF
  85. * %GPIOG
  86. * %GPIOH
  87. * see PRM for details on how these different busses are used.
  88. */
  89. struct gma_i2c_chan *gma_i2c_create(struct drm_device *dev, const u32 reg,
  90. const char *name)
  91. {
  92. struct gma_i2c_chan *chan;
  93. chan = kzalloc(sizeof(struct gma_i2c_chan), GFP_KERNEL);
  94. if (!chan)
  95. goto out_free;
  96. chan->drm_dev = dev;
  97. chan->reg = reg;
  98. snprintf(chan->base.name, I2C_NAME_SIZE, "intel drm %s", name);
  99. chan->base.owner = THIS_MODULE;
  100. chan->base.algo_data = &chan->algo;
  101. chan->base.dev.parent = dev->dev;
  102. chan->algo.setsda = set_data;
  103. chan->algo.setscl = set_clock;
  104. chan->algo.getsda = get_data;
  105. chan->algo.getscl = get_clock;
  106. chan->algo.udelay = 20;
  107. chan->algo.timeout = usecs_to_jiffies(2200);
  108. chan->algo.data = chan;
  109. i2c_set_adapdata(&chan->base, chan);
  110. if (i2c_bit_add_bus(&chan->base))
  111. goto out_free;
  112. /* JJJ: raise SCL and SDA? */
  113. set_data(chan, 1);
  114. set_clock(chan, 1);
  115. udelay(20);
  116. return chan;
  117. out_free:
  118. kfree(chan);
  119. return NULL;
  120. }
  121. /**
  122. * gma_i2c_destroy - unregister and free i2c bus resources
  123. * @chan: channel to free
  124. *
  125. * Unregister the adapter from the i2c layer, then free the structure.
  126. */
  127. void gma_i2c_destroy(struct gma_i2c_chan *chan)
  128. {
  129. if (!chan)
  130. return;
  131. i2c_del_adapter(&chan->base);
  132. kfree(chan);
  133. }