intel_i2c.c 21 KB

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