intel_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
  3. * Copyright © 2006-2008,2010 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. * Chris Wilson <chris@chris-wilson.co.uk>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/i2c-algo-bit.h>
  31. #include <linux/export.h>
  32. #include "drmP.h"
  33. #include "drm.h"
  34. #include "intel_drv.h"
  35. #include "i915_drm.h"
  36. #include "i915_drv.h"
  37. /* Intel GPIO access functions */
  38. #define I2C_RISEFALL_TIME 10
  39. static inline struct intel_gmbus *
  40. to_intel_gmbus(struct i2c_adapter *i2c)
  41. {
  42. return container_of(i2c, struct intel_gmbus, adapter);
  43. }
  44. void
  45. intel_i2c_reset(struct drm_device *dev)
  46. {
  47. struct drm_i915_private *dev_priv = dev->dev_private;
  48. if (HAS_PCH_SPLIT(dev))
  49. I915_WRITE(PCH_GMBUS0, 0);
  50. else
  51. I915_WRITE(GMBUS0, 0);
  52. }
  53. static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
  54. {
  55. u32 val;
  56. /* When using bit bashing for I2C, this bit needs to be set to 1 */
  57. if (!IS_PINEVIEW(dev_priv->dev))
  58. return;
  59. val = I915_READ(DSPCLK_GATE_D);
  60. if (enable)
  61. val |= DPCUNIT_CLOCK_GATE_DISABLE;
  62. else
  63. val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
  64. I915_WRITE(DSPCLK_GATE_D, val);
  65. }
  66. static u32 get_reserved(struct intel_gmbus *bus)
  67. {
  68. struct drm_i915_private *dev_priv = bus->dev_priv;
  69. struct drm_device *dev = dev_priv->dev;
  70. u32 reserved = 0;
  71. /* On most chips, these bits must be preserved in software. */
  72. if (!IS_I830(dev) && !IS_845G(dev))
  73. reserved = I915_READ_NOTRACE(bus->gpio_reg) &
  74. (GPIO_DATA_PULLUP_DISABLE |
  75. GPIO_CLOCK_PULLUP_DISABLE);
  76. return reserved;
  77. }
  78. static int get_clock(void *data)
  79. {
  80. struct intel_gmbus *bus = data;
  81. struct drm_i915_private *dev_priv = bus->dev_priv;
  82. u32 reserved = get_reserved(bus);
  83. I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
  84. I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
  85. return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
  86. }
  87. static int get_data(void *data)
  88. {
  89. struct intel_gmbus *bus = data;
  90. struct drm_i915_private *dev_priv = bus->dev_priv;
  91. u32 reserved = get_reserved(bus);
  92. I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
  93. I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
  94. return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
  95. }
  96. static void set_clock(void *data, int state_high)
  97. {
  98. struct intel_gmbus *bus = data;
  99. struct drm_i915_private *dev_priv = bus->dev_priv;
  100. u32 reserved = get_reserved(bus);
  101. u32 clock_bits;
  102. if (state_high)
  103. clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
  104. else
  105. clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
  106. GPIO_CLOCK_VAL_MASK;
  107. I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
  108. POSTING_READ(bus->gpio_reg);
  109. }
  110. static void set_data(void *data, int state_high)
  111. {
  112. struct intel_gmbus *bus = data;
  113. struct drm_i915_private *dev_priv = bus->dev_priv;
  114. u32 reserved = get_reserved(bus);
  115. u32 data_bits;
  116. if (state_high)
  117. data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
  118. else
  119. data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
  120. GPIO_DATA_VAL_MASK;
  121. I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
  122. POSTING_READ(bus->gpio_reg);
  123. }
  124. static bool
  125. intel_gpio_setup(struct intel_gmbus *bus, u32 pin)
  126. {
  127. struct drm_i915_private *dev_priv = bus->dev_priv;
  128. static const int map_pin_to_reg[] = {
  129. 0,
  130. GPIOB,
  131. GPIOA,
  132. GPIOC,
  133. GPIOD,
  134. GPIOE,
  135. 0,
  136. GPIOF,
  137. };
  138. struct i2c_algo_bit_data *algo;
  139. if (pin >= ARRAY_SIZE(map_pin_to_reg) || !map_pin_to_reg[pin])
  140. return false;
  141. algo = &bus->bit_algo;
  142. bus->gpio_reg = map_pin_to_reg[pin];
  143. if (HAS_PCH_SPLIT(dev_priv->dev))
  144. bus->gpio_reg += PCH_GPIOA - GPIOA;
  145. bus->adapter.algo_data = algo;
  146. algo->setsda = set_data;
  147. algo->setscl = set_clock;
  148. algo->getsda = get_data;
  149. algo->getscl = get_clock;
  150. algo->udelay = I2C_RISEFALL_TIME;
  151. algo->timeout = usecs_to_jiffies(2200);
  152. algo->data = bus;
  153. return true;
  154. }
  155. static int
  156. intel_i2c_quirk_xfer(struct intel_gmbus *bus,
  157. struct i2c_msg *msgs,
  158. int num)
  159. {
  160. struct drm_i915_private *dev_priv = bus->dev_priv;
  161. int ret;
  162. intel_i2c_reset(dev_priv->dev);
  163. intel_i2c_quirk_set(dev_priv, true);
  164. set_data(bus, 1);
  165. set_clock(bus, 1);
  166. udelay(I2C_RISEFALL_TIME);
  167. ret = i2c_bit_algo.master_xfer(&bus->adapter, msgs, num);
  168. set_data(bus, 1);
  169. set_clock(bus, 1);
  170. intel_i2c_quirk_set(dev_priv, false);
  171. return ret;
  172. }
  173. static int
  174. gmbus_xfer(struct i2c_adapter *adapter,
  175. struct i2c_msg *msgs,
  176. int num)
  177. {
  178. struct intel_gmbus *bus = container_of(adapter,
  179. struct intel_gmbus,
  180. adapter);
  181. struct drm_i915_private *dev_priv = bus->dev_priv;
  182. int i, reg_offset, ret;
  183. mutex_lock(&dev_priv->gmbus_mutex);
  184. if (bus->force_bit) {
  185. ret = intel_i2c_quirk_xfer(bus, msgs, num);
  186. goto out;
  187. }
  188. reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
  189. I915_WRITE(GMBUS0 + reg_offset, bus->reg0);
  190. for (i = 0; i < num; i++) {
  191. u16 len = msgs[i].len;
  192. u8 *buf = msgs[i].buf;
  193. if (msgs[i].flags & I2C_M_RD) {
  194. I915_WRITE(GMBUS1 + reg_offset,
  195. GMBUS_CYCLE_WAIT |
  196. (i + 1 == num ? GMBUS_CYCLE_STOP : 0) |
  197. (len << GMBUS_BYTE_COUNT_SHIFT) |
  198. (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |
  199. GMBUS_SLAVE_READ | GMBUS_SW_RDY);
  200. POSTING_READ(GMBUS2+reg_offset);
  201. do {
  202. u32 val, loop = 0;
  203. if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))
  204. goto timeout;
  205. if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
  206. goto clear_err;
  207. val = I915_READ(GMBUS3 + reg_offset);
  208. do {
  209. *buf++ = val & 0xff;
  210. val >>= 8;
  211. } while (--len && ++loop < 4);
  212. } while (len);
  213. } else {
  214. u32 val, loop;
  215. val = loop = 0;
  216. do {
  217. val |= *buf++ << (8 * loop);
  218. } while (--len && ++loop < 4);
  219. I915_WRITE(GMBUS3 + reg_offset, val);
  220. I915_WRITE(GMBUS1 + reg_offset,
  221. GMBUS_CYCLE_WAIT |
  222. (i + 1 == num ? GMBUS_CYCLE_STOP : 0) |
  223. (msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) |
  224. (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |
  225. GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
  226. POSTING_READ(GMBUS2+reg_offset);
  227. while (len) {
  228. if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))
  229. goto timeout;
  230. if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
  231. goto clear_err;
  232. val = loop = 0;
  233. do {
  234. val |= *buf++ << (8 * loop);
  235. } while (--len && ++loop < 4);
  236. I915_WRITE(GMBUS3 + reg_offset, val);
  237. POSTING_READ(GMBUS2+reg_offset);
  238. }
  239. }
  240. if (i + 1 < num && wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_WAIT_PHASE), 50))
  241. goto timeout;
  242. if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
  243. goto clear_err;
  244. }
  245. goto done;
  246. clear_err:
  247. /* Toggle the Software Clear Interrupt bit. This has the effect
  248. * of resetting the GMBUS controller and so clearing the
  249. * BUS_ERROR raised by the slave's NAK.
  250. */
  251. I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
  252. I915_WRITE(GMBUS1 + reg_offset, 0);
  253. done:
  254. /* Mark the GMBUS interface as disabled after waiting for idle.
  255. * We will re-enable it at the start of the next xfer,
  256. * till then let it sleep.
  257. */
  258. if (wait_for((I915_READ(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0, 10))
  259. DRM_INFO("GMBUS timed out waiting for idle\n");
  260. I915_WRITE(GMBUS0 + reg_offset, 0);
  261. ret = i;
  262. goto out;
  263. timeout:
  264. DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n",
  265. bus->reg0 & 0xff, bus->adapter.name);
  266. I915_WRITE(GMBUS0 + reg_offset, 0);
  267. /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
  268. if (!bus->has_gpio) {
  269. ret = -EIO;
  270. } else {
  271. bus->force_bit = true;
  272. ret = intel_i2c_quirk_xfer(bus, msgs, num);
  273. }
  274. out:
  275. mutex_unlock(&dev_priv->gmbus_mutex);
  276. return ret;
  277. }
  278. static u32 gmbus_func(struct i2c_adapter *adapter)
  279. {
  280. return i2c_bit_algo.functionality(adapter) &
  281. (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  282. /* I2C_FUNC_10BIT_ADDR | */
  283. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  284. I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
  285. }
  286. static const struct i2c_algorithm gmbus_algorithm = {
  287. .master_xfer = gmbus_xfer,
  288. .functionality = gmbus_func
  289. };
  290. /**
  291. * intel_gmbus_setup - instantiate all Intel i2c GMBuses
  292. * @dev: DRM device
  293. */
  294. int intel_setup_gmbus(struct drm_device *dev)
  295. {
  296. static const char *names[GMBUS_NUM_PORTS] = {
  297. "disabled",
  298. "ssc",
  299. "vga",
  300. "panel",
  301. "dpc",
  302. "dpb",
  303. "reserved",
  304. "dpd",
  305. };
  306. struct drm_i915_private *dev_priv = dev->dev_private;
  307. int ret, i;
  308. dev_priv->gmbus = kcalloc(GMBUS_NUM_PORTS, sizeof(struct intel_gmbus),
  309. GFP_KERNEL);
  310. if (dev_priv->gmbus == NULL)
  311. return -ENOMEM;
  312. mutex_init(&dev_priv->gmbus_mutex);
  313. for (i = 0; i < GMBUS_NUM_PORTS; i++) {
  314. struct intel_gmbus *bus = &dev_priv->gmbus[i];
  315. bus->adapter.owner = THIS_MODULE;
  316. bus->adapter.class = I2C_CLASS_DDC;
  317. snprintf(bus->adapter.name,
  318. sizeof(bus->adapter.name),
  319. "i915 gmbus %s",
  320. names[i]);
  321. bus->adapter.dev.parent = &dev->pdev->dev;
  322. bus->dev_priv = dev_priv;
  323. bus->adapter.algo = &gmbus_algorithm;
  324. ret = i2c_add_adapter(&bus->adapter);
  325. if (ret)
  326. goto err;
  327. /* By default use a conservative clock rate */
  328. bus->reg0 = i | GMBUS_RATE_100KHZ;
  329. bus->has_gpio = intel_gpio_setup(bus, i);
  330. /* XXX force bit banging until GMBUS is fully debugged */
  331. if (bus->has_gpio)
  332. bus->force_bit = true;
  333. }
  334. intel_i2c_reset(dev_priv->dev);
  335. return 0;
  336. err:
  337. while (--i) {
  338. struct intel_gmbus *bus = &dev_priv->gmbus[i];
  339. i2c_del_adapter(&bus->adapter);
  340. }
  341. kfree(dev_priv->gmbus);
  342. dev_priv->gmbus = NULL;
  343. return ret;
  344. }
  345. void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
  346. {
  347. struct intel_gmbus *bus = to_intel_gmbus(adapter);
  348. bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
  349. }
  350. void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
  351. {
  352. struct intel_gmbus *bus = to_intel_gmbus(adapter);
  353. if (bus->has_gpio)
  354. bus->force_bit = force_bit;
  355. }
  356. void intel_teardown_gmbus(struct drm_device *dev)
  357. {
  358. struct drm_i915_private *dev_priv = dev->dev_private;
  359. int i;
  360. if (dev_priv->gmbus == NULL)
  361. return;
  362. for (i = 0; i < GMBUS_NUM_PORTS; i++) {
  363. struct intel_gmbus *bus = &dev_priv->gmbus[i];
  364. i2c_del_adapter(&bus->adapter);
  365. }
  366. kfree(dev_priv->gmbus);
  367. dev_priv->gmbus = NULL;
  368. }