intel_i2c.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 <drm/drmP.h>
  33. #include "intel_drv.h"
  34. #include <drm/i915_drm.h>
  35. #include "i915_drv.h"
  36. struct gmbus_pin {
  37. const char *name;
  38. int reg;
  39. };
  40. /* Map gmbus pin pairs to names and registers. */
  41. static const struct gmbus_pin gmbus_pins[] = {
  42. [GMBUS_PIN_SSC] = { "ssc", GPIOB },
  43. [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
  44. [GMBUS_PIN_PANEL] = { "panel", GPIOC },
  45. [GMBUS_PIN_DPC] = { "dpc", GPIOD },
  46. [GMBUS_PIN_DPB] = { "dpb", GPIOE },
  47. [GMBUS_PIN_DPD] = { "dpd", GPIOF },
  48. };
  49. static const struct gmbus_pin gmbus_pins_bdw[] = {
  50. [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
  51. [GMBUS_PIN_DPC] = { "dpc", GPIOD },
  52. [GMBUS_PIN_DPB] = { "dpb", GPIOE },
  53. [GMBUS_PIN_DPD] = { "dpd", GPIOF },
  54. };
  55. static const struct gmbus_pin gmbus_pins_skl[] = {
  56. [GMBUS_PIN_DPC] = { "dpc", GPIOD },
  57. [GMBUS_PIN_DPB] = { "dpb", GPIOE },
  58. [GMBUS_PIN_DPD] = { "dpd", GPIOF },
  59. };
  60. static const struct gmbus_pin gmbus_pins_bxt[] = {
  61. [GMBUS_PIN_1_BXT] = { "dpb", PCH_GPIOB },
  62. [GMBUS_PIN_2_BXT] = { "dpc", PCH_GPIOC },
  63. [GMBUS_PIN_3_BXT] = { "misc", PCH_GPIOD },
  64. };
  65. /* pin is expected to be valid */
  66. static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
  67. unsigned int pin)
  68. {
  69. if (IS_BROXTON(dev_priv))
  70. return &gmbus_pins_bxt[pin];
  71. else if (IS_SKYLAKE(dev_priv))
  72. return &gmbus_pins_skl[pin];
  73. else if (IS_BROADWELL(dev_priv))
  74. return &gmbus_pins_bdw[pin];
  75. else
  76. return &gmbus_pins[pin];
  77. }
  78. bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
  79. unsigned int pin)
  80. {
  81. unsigned int size;
  82. if (IS_BROXTON(dev_priv))
  83. size = ARRAY_SIZE(gmbus_pins_bxt);
  84. else if (IS_SKYLAKE(dev_priv))
  85. size = ARRAY_SIZE(gmbus_pins_skl);
  86. else if (IS_BROADWELL(dev_priv))
  87. size = ARRAY_SIZE(gmbus_pins_bdw);
  88. else
  89. size = ARRAY_SIZE(gmbus_pins);
  90. return pin < size && get_gmbus_pin(dev_priv, pin)->reg;
  91. }
  92. /* Intel GPIO access functions */
  93. #define I2C_RISEFALL_TIME 10
  94. static inline struct intel_gmbus *
  95. to_intel_gmbus(struct i2c_adapter *i2c)
  96. {
  97. return container_of(i2c, struct intel_gmbus, adapter);
  98. }
  99. void
  100. intel_i2c_reset(struct drm_device *dev)
  101. {
  102. struct drm_i915_private *dev_priv = dev->dev_private;
  103. I915_WRITE(GMBUS0, 0);
  104. I915_WRITE(GMBUS4, 0);
  105. }
  106. static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
  107. {
  108. u32 val;
  109. /* When using bit bashing for I2C, this bit needs to be set to 1 */
  110. if (!IS_PINEVIEW(dev_priv->dev))
  111. return;
  112. val = I915_READ(DSPCLK_GATE_D);
  113. if (enable)
  114. val |= DPCUNIT_CLOCK_GATE_DISABLE;
  115. else
  116. val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
  117. I915_WRITE(DSPCLK_GATE_D, val);
  118. }
  119. static u32 get_reserved(struct intel_gmbus *bus)
  120. {
  121. struct drm_i915_private *dev_priv = bus->dev_priv;
  122. struct drm_device *dev = dev_priv->dev;
  123. u32 reserved = 0;
  124. /* On most chips, these bits must be preserved in software. */
  125. if (!IS_I830(dev) && !IS_845G(dev))
  126. reserved = I915_READ_NOTRACE(bus->gpio_reg) &
  127. (GPIO_DATA_PULLUP_DISABLE |
  128. GPIO_CLOCK_PULLUP_DISABLE);
  129. return reserved;
  130. }
  131. static int get_clock(void *data)
  132. {
  133. struct intel_gmbus *bus = data;
  134. struct drm_i915_private *dev_priv = bus->dev_priv;
  135. u32 reserved = get_reserved(bus);
  136. I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
  137. I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
  138. return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
  139. }
  140. static int get_data(void *data)
  141. {
  142. struct intel_gmbus *bus = data;
  143. struct drm_i915_private *dev_priv = bus->dev_priv;
  144. u32 reserved = get_reserved(bus);
  145. I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
  146. I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
  147. return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
  148. }
  149. static void set_clock(void *data, int state_high)
  150. {
  151. struct intel_gmbus *bus = data;
  152. struct drm_i915_private *dev_priv = bus->dev_priv;
  153. u32 reserved = get_reserved(bus);
  154. u32 clock_bits;
  155. if (state_high)
  156. clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
  157. else
  158. clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
  159. GPIO_CLOCK_VAL_MASK;
  160. I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
  161. POSTING_READ(bus->gpio_reg);
  162. }
  163. static void set_data(void *data, int state_high)
  164. {
  165. struct intel_gmbus *bus = data;
  166. struct drm_i915_private *dev_priv = bus->dev_priv;
  167. u32 reserved = get_reserved(bus);
  168. u32 data_bits;
  169. if (state_high)
  170. data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
  171. else
  172. data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
  173. GPIO_DATA_VAL_MASK;
  174. I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
  175. POSTING_READ(bus->gpio_reg);
  176. }
  177. static int
  178. intel_gpio_pre_xfer(struct i2c_adapter *adapter)
  179. {
  180. struct intel_gmbus *bus = container_of(adapter,
  181. struct intel_gmbus,
  182. adapter);
  183. struct drm_i915_private *dev_priv = bus->dev_priv;
  184. intel_i2c_reset(dev_priv->dev);
  185. intel_i2c_quirk_set(dev_priv, true);
  186. set_data(bus, 1);
  187. set_clock(bus, 1);
  188. udelay(I2C_RISEFALL_TIME);
  189. return 0;
  190. }
  191. static void
  192. intel_gpio_post_xfer(struct i2c_adapter *adapter)
  193. {
  194. struct intel_gmbus *bus = container_of(adapter,
  195. struct intel_gmbus,
  196. adapter);
  197. struct drm_i915_private *dev_priv = bus->dev_priv;
  198. set_data(bus, 1);
  199. set_clock(bus, 1);
  200. intel_i2c_quirk_set(dev_priv, false);
  201. }
  202. static void
  203. intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
  204. {
  205. struct drm_i915_private *dev_priv = bus->dev_priv;
  206. struct i2c_algo_bit_data *algo;
  207. algo = &bus->bit_algo;
  208. bus->gpio_reg = dev_priv->gpio_mmio_base +
  209. get_gmbus_pin(dev_priv, pin)->reg;
  210. bus->adapter.algo_data = algo;
  211. algo->setsda = set_data;
  212. algo->setscl = set_clock;
  213. algo->getsda = get_data;
  214. algo->getscl = get_clock;
  215. algo->pre_xfer = intel_gpio_pre_xfer;
  216. algo->post_xfer = intel_gpio_post_xfer;
  217. algo->udelay = I2C_RISEFALL_TIME;
  218. algo->timeout = usecs_to_jiffies(2200);
  219. algo->data = bus;
  220. }
  221. static int
  222. gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
  223. u32 gmbus2_status,
  224. u32 gmbus4_irq_en)
  225. {
  226. int i;
  227. u32 gmbus2 = 0;
  228. DEFINE_WAIT(wait);
  229. if (!HAS_GMBUS_IRQ(dev_priv->dev))
  230. gmbus4_irq_en = 0;
  231. /* Important: The hw handles only the first bit, so set only one! Since
  232. * we also need to check for NAKs besides the hw ready/idle signal, we
  233. * need to wake up periodically and check that ourselves. */
  234. I915_WRITE(GMBUS4, gmbus4_irq_en);
  235. for (i = 0; i < msecs_to_jiffies_timeout(50); i++) {
  236. prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
  237. TASK_UNINTERRUPTIBLE);
  238. gmbus2 = I915_READ_NOTRACE(GMBUS2);
  239. if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
  240. break;
  241. schedule_timeout(1);
  242. }
  243. finish_wait(&dev_priv->gmbus_wait_queue, &wait);
  244. I915_WRITE(GMBUS4, 0);
  245. if (gmbus2 & GMBUS_SATOER)
  246. return -ENXIO;
  247. if (gmbus2 & gmbus2_status)
  248. return 0;
  249. return -ETIMEDOUT;
  250. }
  251. static int
  252. gmbus_wait_idle(struct drm_i915_private *dev_priv)
  253. {
  254. int ret;
  255. #define C ((I915_READ_NOTRACE(GMBUS2) & GMBUS_ACTIVE) == 0)
  256. if (!HAS_GMBUS_IRQ(dev_priv->dev))
  257. return wait_for(C, 10);
  258. /* Important: The hw handles only the first bit, so set only one! */
  259. I915_WRITE(GMBUS4, GMBUS_IDLE_EN);
  260. ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
  261. msecs_to_jiffies_timeout(10));
  262. I915_WRITE(GMBUS4, 0);
  263. if (ret)
  264. return 0;
  265. else
  266. return -ETIMEDOUT;
  267. #undef C
  268. }
  269. static int
  270. gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
  271. unsigned short addr, u8 *buf, unsigned int len,
  272. u32 gmbus1_index)
  273. {
  274. I915_WRITE(GMBUS1,
  275. gmbus1_index |
  276. GMBUS_CYCLE_WAIT |
  277. (len << GMBUS_BYTE_COUNT_SHIFT) |
  278. (addr << GMBUS_SLAVE_ADDR_SHIFT) |
  279. GMBUS_SLAVE_READ | GMBUS_SW_RDY);
  280. while (len) {
  281. int ret;
  282. u32 val, loop = 0;
  283. ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
  284. GMBUS_HW_RDY_EN);
  285. if (ret)
  286. return ret;
  287. val = I915_READ(GMBUS3);
  288. do {
  289. *buf++ = val & 0xff;
  290. val >>= 8;
  291. } while (--len && ++loop < 4);
  292. }
  293. return 0;
  294. }
  295. static int
  296. gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
  297. u32 gmbus1_index)
  298. {
  299. u8 *buf = msg->buf;
  300. unsigned int rx_size = msg->len;
  301. unsigned int len;
  302. int ret;
  303. do {
  304. len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
  305. ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
  306. buf, len, gmbus1_index);
  307. if (ret)
  308. return ret;
  309. rx_size -= len;
  310. buf += len;
  311. } while (rx_size != 0);
  312. return 0;
  313. }
  314. static int
  315. gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
  316. unsigned short addr, u8 *buf, unsigned int len)
  317. {
  318. unsigned int chunk_size = len;
  319. u32 val, loop;
  320. val = loop = 0;
  321. while (len && loop < 4) {
  322. val |= *buf++ << (8 * loop++);
  323. len -= 1;
  324. }
  325. I915_WRITE(GMBUS3, val);
  326. I915_WRITE(GMBUS1,
  327. GMBUS_CYCLE_WAIT |
  328. (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
  329. (addr << GMBUS_SLAVE_ADDR_SHIFT) |
  330. GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
  331. while (len) {
  332. int ret;
  333. val = loop = 0;
  334. do {
  335. val |= *buf++ << (8 * loop);
  336. } while (--len && ++loop < 4);
  337. I915_WRITE(GMBUS3, val);
  338. ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
  339. GMBUS_HW_RDY_EN);
  340. if (ret)
  341. return ret;
  342. }
  343. return 0;
  344. }
  345. static int
  346. gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
  347. {
  348. u8 *buf = msg->buf;
  349. unsigned int tx_size = msg->len;
  350. unsigned int len;
  351. int ret;
  352. do {
  353. len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
  354. ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len);
  355. if (ret)
  356. return ret;
  357. buf += len;
  358. tx_size -= len;
  359. } while (tx_size != 0);
  360. return 0;
  361. }
  362. /*
  363. * The gmbus controller can combine a 1 or 2 byte write with a read that
  364. * immediately follows it by using an "INDEX" cycle.
  365. */
  366. static bool
  367. gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
  368. {
  369. return (i + 1 < num &&
  370. msgs[i].addr == msgs[i + 1].addr &&
  371. !(msgs[i].flags & I2C_M_RD) &&
  372. (msgs[i].len == 1 || msgs[i].len == 2) &&
  373. (msgs[i + 1].flags & I2C_M_RD));
  374. }
  375. static int
  376. gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
  377. {
  378. u32 gmbus1_index = 0;
  379. u32 gmbus5 = 0;
  380. int ret;
  381. if (msgs[0].len == 2)
  382. gmbus5 = GMBUS_2BYTE_INDEX_EN |
  383. msgs[0].buf[1] | (msgs[0].buf[0] << 8);
  384. if (msgs[0].len == 1)
  385. gmbus1_index = GMBUS_CYCLE_INDEX |
  386. (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
  387. /* GMBUS5 holds 16-bit index */
  388. if (gmbus5)
  389. I915_WRITE(GMBUS5, gmbus5);
  390. ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
  391. /* Clear GMBUS5 after each index transfer */
  392. if (gmbus5)
  393. I915_WRITE(GMBUS5, 0);
  394. return ret;
  395. }
  396. static int
  397. gmbus_xfer(struct i2c_adapter *adapter,
  398. struct i2c_msg *msgs,
  399. int num)
  400. {
  401. struct intel_gmbus *bus = container_of(adapter,
  402. struct intel_gmbus,
  403. adapter);
  404. struct drm_i915_private *dev_priv = bus->dev_priv;
  405. int i = 0, inc, try = 0;
  406. int ret = 0;
  407. intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
  408. mutex_lock(&dev_priv->gmbus_mutex);
  409. if (bus->force_bit) {
  410. ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
  411. goto out;
  412. }
  413. retry:
  414. I915_WRITE(GMBUS0, bus->reg0);
  415. for (; i < num; i += inc) {
  416. inc = 1;
  417. if (gmbus_is_index_read(msgs, i, num)) {
  418. ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
  419. inc = 2; /* an index read is two msgs */
  420. } else if (msgs[i].flags & I2C_M_RD) {
  421. ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
  422. } else {
  423. ret = gmbus_xfer_write(dev_priv, &msgs[i]);
  424. }
  425. if (ret == -ETIMEDOUT)
  426. goto timeout;
  427. if (ret == -ENXIO)
  428. goto clear_err;
  429. ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
  430. GMBUS_HW_WAIT_EN);
  431. if (ret == -ENXIO)
  432. goto clear_err;
  433. if (ret)
  434. goto timeout;
  435. }
  436. /* Generate a STOP condition on the bus. Note that gmbus can't generata
  437. * a STOP on the very first cycle. To simplify the code we
  438. * unconditionally generate the STOP condition with an additional gmbus
  439. * cycle. */
  440. I915_WRITE(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
  441. /* Mark the GMBUS interface as disabled after waiting for idle.
  442. * We will re-enable it at the start of the next xfer,
  443. * till then let it sleep.
  444. */
  445. if (gmbus_wait_idle(dev_priv)) {
  446. DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
  447. adapter->name);
  448. ret = -ETIMEDOUT;
  449. }
  450. I915_WRITE(GMBUS0, 0);
  451. ret = ret ?: i;
  452. goto out;
  453. clear_err:
  454. /*
  455. * Wait for bus to IDLE before clearing NAK.
  456. * If we clear the NAK while bus is still active, then it will stay
  457. * active and the next transaction may fail.
  458. *
  459. * If no ACK is received during the address phase of a transaction, the
  460. * adapter must report -ENXIO. It is not clear what to return if no ACK
  461. * is received at other times. But we have to be careful to not return
  462. * spurious -ENXIO because that will prevent i2c and drm edid functions
  463. * from retrying. So return -ENXIO only when gmbus properly quiescents -
  464. * timing out seems to happen when there _is_ a ddc chip present, but
  465. * it's slow responding and only answers on the 2nd retry.
  466. */
  467. ret = -ENXIO;
  468. if (gmbus_wait_idle(dev_priv)) {
  469. DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
  470. adapter->name);
  471. ret = -ETIMEDOUT;
  472. }
  473. /* Toggle the Software Clear Interrupt bit. This has the effect
  474. * of resetting the GMBUS controller and so clearing the
  475. * BUS_ERROR raised by the slave's NAK.
  476. */
  477. I915_WRITE(GMBUS1, GMBUS_SW_CLR_INT);
  478. I915_WRITE(GMBUS1, 0);
  479. I915_WRITE(GMBUS0, 0);
  480. DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
  481. adapter->name, msgs[i].addr,
  482. (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
  483. /*
  484. * Passive adapters sometimes NAK the first probe. Retry the first
  485. * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
  486. * has retries internally. See also the retry loop in
  487. * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
  488. */
  489. if (ret == -ENXIO && i == 0 && try++ == 0) {
  490. DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
  491. adapter->name);
  492. goto retry;
  493. }
  494. goto out;
  495. timeout:
  496. DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
  497. bus->adapter.name, bus->reg0 & 0xff);
  498. I915_WRITE(GMBUS0, 0);
  499. /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
  500. bus->force_bit = 1;
  501. ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
  502. out:
  503. mutex_unlock(&dev_priv->gmbus_mutex);
  504. intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
  505. return ret;
  506. }
  507. static u32 gmbus_func(struct i2c_adapter *adapter)
  508. {
  509. return i2c_bit_algo.functionality(adapter) &
  510. (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  511. /* I2C_FUNC_10BIT_ADDR | */
  512. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  513. I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
  514. }
  515. static const struct i2c_algorithm gmbus_algorithm = {
  516. .master_xfer = gmbus_xfer,
  517. .functionality = gmbus_func
  518. };
  519. /**
  520. * intel_gmbus_setup - instantiate all Intel i2c GMBuses
  521. * @dev: DRM device
  522. */
  523. int intel_setup_gmbus(struct drm_device *dev)
  524. {
  525. struct drm_i915_private *dev_priv = dev->dev_private;
  526. struct intel_gmbus *bus;
  527. unsigned int pin;
  528. int ret;
  529. if (HAS_PCH_NOP(dev))
  530. return 0;
  531. else if (HAS_PCH_SPLIT(dev))
  532. dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
  533. else if (IS_VALLEYVIEW(dev))
  534. dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
  535. else
  536. dev_priv->gpio_mmio_base = 0;
  537. mutex_init(&dev_priv->gmbus_mutex);
  538. init_waitqueue_head(&dev_priv->gmbus_wait_queue);
  539. for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
  540. if (!intel_gmbus_is_valid_pin(dev_priv, pin))
  541. continue;
  542. bus = &dev_priv->gmbus[pin];
  543. bus->adapter.owner = THIS_MODULE;
  544. bus->adapter.class = I2C_CLASS_DDC;
  545. snprintf(bus->adapter.name,
  546. sizeof(bus->adapter.name),
  547. "i915 gmbus %s",
  548. get_gmbus_pin(dev_priv, pin)->name);
  549. bus->adapter.dev.parent = &dev->pdev->dev;
  550. bus->dev_priv = dev_priv;
  551. bus->adapter.algo = &gmbus_algorithm;
  552. /* By default use a conservative clock rate */
  553. bus->reg0 = pin | GMBUS_RATE_100KHZ;
  554. /* gmbus seems to be broken on i830 */
  555. if (IS_I830(dev))
  556. bus->force_bit = 1;
  557. intel_gpio_setup(bus, pin);
  558. ret = i2c_add_adapter(&bus->adapter);
  559. if (ret)
  560. goto err;
  561. }
  562. intel_i2c_reset(dev_priv->dev);
  563. return 0;
  564. err:
  565. while (pin--) {
  566. if (!intel_gmbus_is_valid_pin(dev_priv, pin))
  567. continue;
  568. bus = &dev_priv->gmbus[pin];
  569. i2c_del_adapter(&bus->adapter);
  570. }
  571. return ret;
  572. }
  573. struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
  574. unsigned int pin)
  575. {
  576. if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
  577. return NULL;
  578. return &dev_priv->gmbus[pin].adapter;
  579. }
  580. void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
  581. {
  582. struct intel_gmbus *bus = to_intel_gmbus(adapter);
  583. bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
  584. }
  585. void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
  586. {
  587. struct intel_gmbus *bus = to_intel_gmbus(adapter);
  588. bus->force_bit += force_bit ? 1 : -1;
  589. DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
  590. force_bit ? "en" : "dis", adapter->name,
  591. bus->force_bit);
  592. }
  593. void intel_teardown_gmbus(struct drm_device *dev)
  594. {
  595. struct drm_i915_private *dev_priv = dev->dev_private;
  596. struct intel_gmbus *bus;
  597. unsigned int pin;
  598. for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
  599. if (!intel_gmbus_is_valid_pin(dev_priv, pin))
  600. continue;
  601. bus = &dev_priv->gmbus[pin];
  602. i2c_del_adapter(&bus->adapter);
  603. }
  604. }