vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / intel_i2c.c
blobbef32b7c248e0bdbe42eec64f36e0e3c64e5e5a7
1 /*
2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2008,2010 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
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:
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.
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.
25 * Authors:
26 * Eric Anholt <eric@anholt.net>
27 * Chris Wilson <chris@chris-wilson.co.uk>
29 #include <linux/i2c.h>
30 #include <linux/i2c-algo-bit.h>
31 #include <linux/export.h>
32 #include <drm/drmP.h>
33 #include <drm/drm_hdcp.h>
34 #include "intel_drv.h"
35 #include <drm/i915_drm.h>
36 #include "i915_drv.h"
38 struct gmbus_pin {
39 const char *name;
40 i915_reg_t reg;
43 /* Map gmbus pin pairs to names and registers. */
44 static const struct gmbus_pin gmbus_pins[] = {
45 [GMBUS_PIN_SSC] = { "ssc", GPIOB },
46 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
47 [GMBUS_PIN_PANEL] = { "panel", GPIOC },
48 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
49 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
50 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
53 static const struct gmbus_pin gmbus_pins_bdw[] = {
54 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
55 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
56 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
57 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
60 static const struct gmbus_pin gmbus_pins_skl[] = {
61 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
62 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
63 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
66 static const struct gmbus_pin gmbus_pins_bxt[] = {
67 [GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
68 [GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
69 [GMBUS_PIN_3_BXT] = { "misc", GPIOD },
72 static const struct gmbus_pin gmbus_pins_cnp[] = {
73 [GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
74 [GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
75 [GMBUS_PIN_3_BXT] = { "misc", GPIOD },
76 [GMBUS_PIN_4_CNP] = { "dpd", GPIOE },
79 static const struct gmbus_pin gmbus_pins_icp[] = {
80 [GMBUS_PIN_1_BXT] = { "dpa", GPIOB },
81 [GMBUS_PIN_2_BXT] = { "dpb", GPIOC },
82 [GMBUS_PIN_9_TC1_ICP] = { "tc1", GPIOJ },
83 [GMBUS_PIN_10_TC2_ICP] = { "tc2", GPIOK },
84 [GMBUS_PIN_11_TC3_ICP] = { "tc3", GPIOL },
85 [GMBUS_PIN_12_TC4_ICP] = { "tc4", GPIOM },
88 /* pin is expected to be valid */
89 static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
90 unsigned int pin)
92 if (HAS_PCH_ICP(dev_priv))
93 return &gmbus_pins_icp[pin];
94 else if (HAS_PCH_CNP(dev_priv))
95 return &gmbus_pins_cnp[pin];
96 else if (IS_GEN9_LP(dev_priv))
97 return &gmbus_pins_bxt[pin];
98 else if (IS_GEN9_BC(dev_priv))
99 return &gmbus_pins_skl[pin];
100 else if (IS_BROADWELL(dev_priv))
101 return &gmbus_pins_bdw[pin];
102 else
103 return &gmbus_pins[pin];
106 bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
107 unsigned int pin)
109 unsigned int size;
111 if (HAS_PCH_ICP(dev_priv))
112 size = ARRAY_SIZE(gmbus_pins_icp);
113 else if (HAS_PCH_CNP(dev_priv))
114 size = ARRAY_SIZE(gmbus_pins_cnp);
115 else if (IS_GEN9_LP(dev_priv))
116 size = ARRAY_SIZE(gmbus_pins_bxt);
117 else if (IS_GEN9_BC(dev_priv))
118 size = ARRAY_SIZE(gmbus_pins_skl);
119 else if (IS_BROADWELL(dev_priv))
120 size = ARRAY_SIZE(gmbus_pins_bdw);
121 else
122 size = ARRAY_SIZE(gmbus_pins);
124 return pin < size &&
125 i915_mmio_reg_valid(get_gmbus_pin(dev_priv, pin)->reg);
128 /* Intel GPIO access functions */
130 #define I2C_RISEFALL_TIME 10
132 static inline struct intel_gmbus *
133 to_intel_gmbus(struct i2c_adapter *i2c)
135 return container_of(i2c, struct intel_gmbus, adapter);
138 void
139 intel_i2c_reset(struct drm_i915_private *dev_priv)
141 I915_WRITE(GMBUS0, 0);
142 I915_WRITE(GMBUS4, 0);
145 static void pnv_gmbus_clock_gating(struct drm_i915_private *dev_priv,
146 bool enable)
148 u32 val;
150 /* When using bit bashing for I2C, this bit needs to be set to 1 */
151 val = I915_READ(DSPCLK_GATE_D);
152 if (!enable)
153 val |= PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
154 else
155 val &= ~PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
156 I915_WRITE(DSPCLK_GATE_D, val);
159 static void pch_gmbus_clock_gating(struct drm_i915_private *dev_priv,
160 bool enable)
162 u32 val;
164 val = I915_READ(SOUTH_DSPCLK_GATE_D);
165 if (!enable)
166 val |= PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
167 else
168 val &= ~PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
169 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
172 static void bxt_gmbus_clock_gating(struct drm_i915_private *dev_priv,
173 bool enable)
175 u32 val;
177 val = I915_READ(GEN9_CLKGATE_DIS_4);
178 if (!enable)
179 val |= BXT_GMBUS_GATING_DIS;
180 else
181 val &= ~BXT_GMBUS_GATING_DIS;
182 I915_WRITE(GEN9_CLKGATE_DIS_4, val);
185 static u32 get_reserved(struct intel_gmbus *bus)
187 struct drm_i915_private *dev_priv = bus->dev_priv;
188 u32 reserved = 0;
190 /* On most chips, these bits must be preserved in software. */
191 if (!IS_I830(dev_priv) && !IS_I845G(dev_priv))
192 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
193 (GPIO_DATA_PULLUP_DISABLE |
194 GPIO_CLOCK_PULLUP_DISABLE);
196 return reserved;
199 static int get_clock(void *data)
201 struct intel_gmbus *bus = data;
202 struct drm_i915_private *dev_priv = bus->dev_priv;
203 u32 reserved = get_reserved(bus);
204 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
205 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
206 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
209 static int get_data(void *data)
211 struct intel_gmbus *bus = data;
212 struct drm_i915_private *dev_priv = bus->dev_priv;
213 u32 reserved = get_reserved(bus);
214 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
215 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
216 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
219 static void set_clock(void *data, int state_high)
221 struct intel_gmbus *bus = data;
222 struct drm_i915_private *dev_priv = bus->dev_priv;
223 u32 reserved = get_reserved(bus);
224 u32 clock_bits;
226 if (state_high)
227 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
228 else
229 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
230 GPIO_CLOCK_VAL_MASK;
232 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
233 POSTING_READ(bus->gpio_reg);
236 static void set_data(void *data, int state_high)
238 struct intel_gmbus *bus = data;
239 struct drm_i915_private *dev_priv = bus->dev_priv;
240 u32 reserved = get_reserved(bus);
241 u32 data_bits;
243 if (state_high)
244 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
245 else
246 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
247 GPIO_DATA_VAL_MASK;
249 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
250 POSTING_READ(bus->gpio_reg);
253 static int
254 intel_gpio_pre_xfer(struct i2c_adapter *adapter)
256 struct intel_gmbus *bus = container_of(adapter,
257 struct intel_gmbus,
258 adapter);
259 struct drm_i915_private *dev_priv = bus->dev_priv;
261 intel_i2c_reset(dev_priv);
263 if (IS_PINEVIEW(dev_priv))
264 pnv_gmbus_clock_gating(dev_priv, false);
266 set_data(bus, 1);
267 set_clock(bus, 1);
268 udelay(I2C_RISEFALL_TIME);
269 return 0;
272 static void
273 intel_gpio_post_xfer(struct i2c_adapter *adapter)
275 struct intel_gmbus *bus = container_of(adapter,
276 struct intel_gmbus,
277 adapter);
278 struct drm_i915_private *dev_priv = bus->dev_priv;
280 set_data(bus, 1);
281 set_clock(bus, 1);
283 if (IS_PINEVIEW(dev_priv))
284 pnv_gmbus_clock_gating(dev_priv, true);
287 static void
288 intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
290 struct drm_i915_private *dev_priv = bus->dev_priv;
291 struct i2c_algo_bit_data *algo;
293 algo = &bus->bit_algo;
295 bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base +
296 i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg));
297 bus->adapter.algo_data = algo;
298 algo->setsda = set_data;
299 algo->setscl = set_clock;
300 algo->getsda = get_data;
301 algo->getscl = get_clock;
302 algo->pre_xfer = intel_gpio_pre_xfer;
303 algo->post_xfer = intel_gpio_post_xfer;
304 algo->udelay = I2C_RISEFALL_TIME;
305 algo->timeout = usecs_to_jiffies(2200);
306 algo->data = bus;
309 static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
311 DEFINE_WAIT(wait);
312 u32 gmbus2;
313 int ret;
315 /* Important: The hw handles only the first bit, so set only one! Since
316 * we also need to check for NAKs besides the hw ready/idle signal, we
317 * need to wake up periodically and check that ourselves.
319 if (!HAS_GMBUS_IRQ(dev_priv))
320 irq_en = 0;
322 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
323 I915_WRITE_FW(GMBUS4, irq_en);
325 status |= GMBUS_SATOER;
326 ret = wait_for_us((gmbus2 = I915_READ_FW(GMBUS2)) & status, 2);
327 if (ret)
328 ret = wait_for((gmbus2 = I915_READ_FW(GMBUS2)) & status, 50);
330 I915_WRITE_FW(GMBUS4, 0);
331 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
333 if (gmbus2 & GMBUS_SATOER)
334 return -ENXIO;
336 return ret;
339 static int
340 gmbus_wait_idle(struct drm_i915_private *dev_priv)
342 DEFINE_WAIT(wait);
343 u32 irq_enable;
344 int ret;
346 /* Important: The hw handles only the first bit, so set only one! */
347 irq_enable = 0;
348 if (HAS_GMBUS_IRQ(dev_priv))
349 irq_enable = GMBUS_IDLE_EN;
351 add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
352 I915_WRITE_FW(GMBUS4, irq_enable);
354 ret = intel_wait_for_register_fw(dev_priv,
355 GMBUS2, GMBUS_ACTIVE, 0,
356 10);
358 I915_WRITE_FW(GMBUS4, 0);
359 remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
361 return ret;
364 static inline
365 unsigned int gmbus_max_xfer_size(struct drm_i915_private *dev_priv)
367 return INTEL_GEN(dev_priv) >= 9 ? GEN9_GMBUS_BYTE_COUNT_MAX :
368 GMBUS_BYTE_COUNT_MAX;
371 static int
372 gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
373 unsigned short addr, u8 *buf, unsigned int len,
374 u32 gmbus0_reg, u32 gmbus1_index)
376 unsigned int size = len;
377 bool burst_read = len > gmbus_max_xfer_size(dev_priv);
378 bool extra_byte_added = false;
380 if (burst_read) {
382 * As per HW Spec, for 512Bytes need to read extra Byte and
383 * Ignore the extra byte read.
385 if (len == 512) {
386 extra_byte_added = true;
387 len++;
389 size = len % 256 + 256;
390 I915_WRITE_FW(GMBUS0, gmbus0_reg | GMBUS_BYTE_CNT_OVERRIDE);
393 I915_WRITE_FW(GMBUS1,
394 gmbus1_index |
395 GMBUS_CYCLE_WAIT |
396 (size << GMBUS_BYTE_COUNT_SHIFT) |
397 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
398 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
399 while (len) {
400 int ret;
401 u32 val, loop = 0;
403 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
404 if (ret)
405 return ret;
407 val = I915_READ_FW(GMBUS3);
408 do {
409 if (extra_byte_added && len == 1)
410 break;
412 *buf++ = val & 0xff;
413 val >>= 8;
414 } while (--len && ++loop < 4);
416 if (burst_read && len == size - 4)
417 /* Reset the override bit */
418 I915_WRITE_FW(GMBUS0, gmbus0_reg);
421 return 0;
425 * HW spec says that 512Bytes in Burst read need special treatment.
426 * But it doesn't talk about other multiple of 256Bytes. And couldn't locate
427 * an I2C slave, which supports such a lengthy burst read too for experiments.
429 * So until things get clarified on HW support, to avoid the burst read length
430 * in fold of 256Bytes except 512, max burst read length is fixed at 767Bytes.
432 #define INTEL_GMBUS_BURST_READ_MAX_LEN 767U
434 static int
435 gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
436 u32 gmbus0_reg, u32 gmbus1_index)
438 u8 *buf = msg->buf;
439 unsigned int rx_size = msg->len;
440 unsigned int len;
441 int ret;
443 do {
444 if (HAS_GMBUS_BURST_READ(dev_priv))
445 len = min(rx_size, INTEL_GMBUS_BURST_READ_MAX_LEN);
446 else
447 len = min(rx_size, gmbus_max_xfer_size(dev_priv));
449 ret = gmbus_xfer_read_chunk(dev_priv, msg->addr, buf, len,
450 gmbus0_reg, gmbus1_index);
451 if (ret)
452 return ret;
454 rx_size -= len;
455 buf += len;
456 } while (rx_size != 0);
458 return 0;
461 static int
462 gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
463 unsigned short addr, u8 *buf, unsigned int len,
464 u32 gmbus1_index)
466 unsigned int chunk_size = len;
467 u32 val, loop;
469 val = loop = 0;
470 while (len && loop < 4) {
471 val |= *buf++ << (8 * loop++);
472 len -= 1;
475 I915_WRITE_FW(GMBUS3, val);
476 I915_WRITE_FW(GMBUS1,
477 gmbus1_index | GMBUS_CYCLE_WAIT |
478 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
479 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
480 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
481 while (len) {
482 int ret;
484 val = loop = 0;
485 do {
486 val |= *buf++ << (8 * loop);
487 } while (--len && ++loop < 4);
489 I915_WRITE_FW(GMBUS3, val);
491 ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
492 if (ret)
493 return ret;
496 return 0;
499 static int
500 gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
501 u32 gmbus1_index)
503 u8 *buf = msg->buf;
504 unsigned int tx_size = msg->len;
505 unsigned int len;
506 int ret;
508 do {
509 len = min(tx_size, gmbus_max_xfer_size(dev_priv));
511 ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len,
512 gmbus1_index);
513 if (ret)
514 return ret;
516 buf += len;
517 tx_size -= len;
518 } while (tx_size != 0);
520 return 0;
524 * The gmbus controller can combine a 1 or 2 byte write with another read/write
525 * that immediately follows it by using an "INDEX" cycle.
527 static bool
528 gmbus_is_index_xfer(struct i2c_msg *msgs, int i, int num)
530 return (i + 1 < num &&
531 msgs[i].addr == msgs[i + 1].addr &&
532 !(msgs[i].flags & I2C_M_RD) &&
533 (msgs[i].len == 1 || msgs[i].len == 2) &&
534 msgs[i + 1].len > 0);
537 static int
538 gmbus_index_xfer(struct drm_i915_private *dev_priv, struct i2c_msg *msgs,
539 u32 gmbus0_reg)
541 u32 gmbus1_index = 0;
542 u32 gmbus5 = 0;
543 int ret;
545 if (msgs[0].len == 2)
546 gmbus5 = GMBUS_2BYTE_INDEX_EN |
547 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
548 if (msgs[0].len == 1)
549 gmbus1_index = GMBUS_CYCLE_INDEX |
550 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
552 /* GMBUS5 holds 16-bit index */
553 if (gmbus5)
554 I915_WRITE_FW(GMBUS5, gmbus5);
556 if (msgs[1].flags & I2C_M_RD)
557 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus0_reg,
558 gmbus1_index);
559 else
560 ret = gmbus_xfer_write(dev_priv, &msgs[1], gmbus1_index);
562 /* Clear GMBUS5 after each index transfer */
563 if (gmbus5)
564 I915_WRITE_FW(GMBUS5, 0);
566 return ret;
569 static int
570 do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num,
571 u32 gmbus0_source)
573 struct intel_gmbus *bus = container_of(adapter,
574 struct intel_gmbus,
575 adapter);
576 struct drm_i915_private *dev_priv = bus->dev_priv;
577 int i = 0, inc, try = 0;
578 int ret = 0;
580 /* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
581 if (IS_GEN9_LP(dev_priv))
582 bxt_gmbus_clock_gating(dev_priv, false);
583 else if (HAS_PCH_SPT(dev_priv) ||
584 HAS_PCH_KBP(dev_priv) || HAS_PCH_CNP(dev_priv))
585 pch_gmbus_clock_gating(dev_priv, false);
587 retry:
588 I915_WRITE_FW(GMBUS0, gmbus0_source | bus->reg0);
590 for (; i < num; i += inc) {
591 inc = 1;
592 if (gmbus_is_index_xfer(msgs, i, num)) {
593 ret = gmbus_index_xfer(dev_priv, &msgs[i],
594 gmbus0_source | bus->reg0);
595 inc = 2; /* an index transmission is two msgs */
596 } else if (msgs[i].flags & I2C_M_RD) {
597 ret = gmbus_xfer_read(dev_priv, &msgs[i],
598 gmbus0_source | bus->reg0, 0);
599 } else {
600 ret = gmbus_xfer_write(dev_priv, &msgs[i], 0);
603 if (!ret)
604 ret = gmbus_wait(dev_priv,
605 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
606 if (ret == -ETIMEDOUT)
607 goto timeout;
608 else if (ret)
609 goto clear_err;
612 /* Generate a STOP condition on the bus. Note that gmbus can't generata
613 * a STOP on the very first cycle. To simplify the code we
614 * unconditionally generate the STOP condition with an additional gmbus
615 * cycle. */
616 I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
618 /* Mark the GMBUS interface as disabled after waiting for idle.
619 * We will re-enable it at the start of the next xfer,
620 * till then let it sleep.
622 if (gmbus_wait_idle(dev_priv)) {
623 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
624 adapter->name);
625 ret = -ETIMEDOUT;
627 I915_WRITE_FW(GMBUS0, 0);
628 ret = ret ?: i;
629 goto out;
631 clear_err:
633 * Wait for bus to IDLE before clearing NAK.
634 * If we clear the NAK while bus is still active, then it will stay
635 * active and the next transaction may fail.
637 * If no ACK is received during the address phase of a transaction, the
638 * adapter must report -ENXIO. It is not clear what to return if no ACK
639 * is received at other times. But we have to be careful to not return
640 * spurious -ENXIO because that will prevent i2c and drm edid functions
641 * from retrying. So return -ENXIO only when gmbus properly quiescents -
642 * timing out seems to happen when there _is_ a ddc chip present, but
643 * it's slow responding and only answers on the 2nd retry.
645 ret = -ENXIO;
646 if (gmbus_wait_idle(dev_priv)) {
647 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
648 adapter->name);
649 ret = -ETIMEDOUT;
652 /* Toggle the Software Clear Interrupt bit. This has the effect
653 * of resetting the GMBUS controller and so clearing the
654 * BUS_ERROR raised by the slave's NAK.
656 I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT);
657 I915_WRITE_FW(GMBUS1, 0);
658 I915_WRITE_FW(GMBUS0, 0);
660 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
661 adapter->name, msgs[i].addr,
662 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
665 * Passive adapters sometimes NAK the first probe. Retry the first
666 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
667 * has retries internally. See also the retry loop in
668 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
670 if (ret == -ENXIO && i == 0 && try++ == 0) {
671 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
672 adapter->name);
673 goto retry;
676 goto out;
678 timeout:
679 DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
680 bus->adapter.name, bus->reg0 & 0xff);
681 I915_WRITE_FW(GMBUS0, 0);
684 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
685 * instead. Use EAGAIN to have i2c core retry.
687 ret = -EAGAIN;
689 out:
690 /* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
691 if (IS_GEN9_LP(dev_priv))
692 bxt_gmbus_clock_gating(dev_priv, true);
693 else if (HAS_PCH_SPT(dev_priv) ||
694 HAS_PCH_KBP(dev_priv) || HAS_PCH_CNP(dev_priv))
695 pch_gmbus_clock_gating(dev_priv, true);
697 return ret;
700 static int
701 gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
703 struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
704 adapter);
705 struct drm_i915_private *dev_priv = bus->dev_priv;
706 int ret;
708 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
710 if (bus->force_bit) {
711 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
712 if (ret < 0)
713 bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
714 } else {
715 ret = do_gmbus_xfer(adapter, msgs, num, 0);
716 if (ret == -EAGAIN)
717 bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
720 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
722 return ret;
725 int intel_gmbus_output_aksv(struct i2c_adapter *adapter)
727 struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
728 adapter);
729 struct drm_i915_private *dev_priv = bus->dev_priv;
730 int ret;
731 u8 cmd = DRM_HDCP_DDC_AKSV;
732 u8 buf[DRM_HDCP_KSV_LEN] = { 0 };
733 struct i2c_msg msgs[] = {
735 .addr = DRM_HDCP_DDC_ADDR,
736 .flags = 0,
737 .len = sizeof(cmd),
738 .buf = &cmd,
741 .addr = DRM_HDCP_DDC_ADDR,
742 .flags = 0,
743 .len = sizeof(buf),
744 .buf = buf,
748 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
749 mutex_lock(&dev_priv->gmbus_mutex);
752 * In order to output Aksv to the receiver, use an indexed write to
753 * pass the i2c command, and tell GMBUS to use the HW-provided value
754 * instead of sourcing GMBUS3 for the data.
756 ret = do_gmbus_xfer(adapter, msgs, ARRAY_SIZE(msgs), GMBUS_AKSV_SELECT);
758 mutex_unlock(&dev_priv->gmbus_mutex);
759 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
761 return ret;
764 static u32 gmbus_func(struct i2c_adapter *adapter)
766 return i2c_bit_algo.functionality(adapter) &
767 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
768 /* I2C_FUNC_10BIT_ADDR | */
769 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
770 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
773 static const struct i2c_algorithm gmbus_algorithm = {
774 .master_xfer = gmbus_xfer,
775 .functionality = gmbus_func
778 static void gmbus_lock_bus(struct i2c_adapter *adapter,
779 unsigned int flags)
781 struct intel_gmbus *bus = to_intel_gmbus(adapter);
782 struct drm_i915_private *dev_priv = bus->dev_priv;
784 mutex_lock(&dev_priv->gmbus_mutex);
787 static int gmbus_trylock_bus(struct i2c_adapter *adapter,
788 unsigned int flags)
790 struct intel_gmbus *bus = to_intel_gmbus(adapter);
791 struct drm_i915_private *dev_priv = bus->dev_priv;
793 return mutex_trylock(&dev_priv->gmbus_mutex);
796 static void gmbus_unlock_bus(struct i2c_adapter *adapter,
797 unsigned int flags)
799 struct intel_gmbus *bus = to_intel_gmbus(adapter);
800 struct drm_i915_private *dev_priv = bus->dev_priv;
802 mutex_unlock(&dev_priv->gmbus_mutex);
805 static const struct i2c_lock_operations gmbus_lock_ops = {
806 .lock_bus = gmbus_lock_bus,
807 .trylock_bus = gmbus_trylock_bus,
808 .unlock_bus = gmbus_unlock_bus,
812 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
813 * @dev_priv: i915 device private
815 int intel_setup_gmbus(struct drm_i915_private *dev_priv)
817 struct pci_dev *pdev = dev_priv->drm.pdev;
818 struct intel_gmbus *bus;
819 unsigned int pin;
820 int ret;
822 if (INTEL_INFO(dev_priv)->num_pipes == 0)
823 return 0;
825 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
826 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
827 else if (!HAS_GMCH_DISPLAY(dev_priv))
828 dev_priv->gpio_mmio_base =
829 i915_mmio_reg_offset(PCH_GPIOA) -
830 i915_mmio_reg_offset(GPIOA);
832 mutex_init(&dev_priv->gmbus_mutex);
833 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
835 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
836 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
837 continue;
839 bus = &dev_priv->gmbus[pin];
841 bus->adapter.owner = THIS_MODULE;
842 bus->adapter.class = I2C_CLASS_DDC;
843 snprintf(bus->adapter.name,
844 sizeof(bus->adapter.name),
845 "i915 gmbus %s",
846 get_gmbus_pin(dev_priv, pin)->name);
848 bus->adapter.dev.parent = &pdev->dev;
849 bus->dev_priv = dev_priv;
851 bus->adapter.algo = &gmbus_algorithm;
852 bus->adapter.lock_ops = &gmbus_lock_ops;
855 * We wish to retry with bit banging
856 * after a timed out GMBUS attempt.
858 bus->adapter.retries = 1;
860 /* By default use a conservative clock rate */
861 bus->reg0 = pin | GMBUS_RATE_100KHZ;
863 /* gmbus seems to be broken on i830 */
864 if (IS_I830(dev_priv))
865 bus->force_bit = 1;
867 intel_gpio_setup(bus, pin);
869 ret = i2c_add_adapter(&bus->adapter);
870 if (ret)
871 goto err;
874 intel_i2c_reset(dev_priv);
876 return 0;
878 err:
879 while (pin--) {
880 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
881 continue;
883 bus = &dev_priv->gmbus[pin];
884 i2c_del_adapter(&bus->adapter);
886 return ret;
889 struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
890 unsigned int pin)
892 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
893 return NULL;
895 return &dev_priv->gmbus[pin].adapter;
898 void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
900 struct intel_gmbus *bus = to_intel_gmbus(adapter);
902 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
905 void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
907 struct intel_gmbus *bus = to_intel_gmbus(adapter);
908 struct drm_i915_private *dev_priv = bus->dev_priv;
910 mutex_lock(&dev_priv->gmbus_mutex);
912 bus->force_bit += force_bit ? 1 : -1;
913 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
914 force_bit ? "en" : "dis", adapter->name,
915 bus->force_bit);
917 mutex_unlock(&dev_priv->gmbus_mutex);
920 void intel_teardown_gmbus(struct drm_i915_private *dev_priv)
922 struct intel_gmbus *bus;
923 unsigned int pin;
925 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
926 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
927 continue;
929 bus = &dev_priv->gmbus[pin];
930 i2c_del_adapter(&bus->adapter);