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
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.
26 * Eric Anholt <eric@anholt.net>
27 * Chris Wilson <chris@chris-wilson.co.uk>
30 #include <linux/export.h>
31 #include <linux/i2c-algo-bit.h>
32 #include <linux/i2c.h>
34 #include <drm/drm_hdcp.h>
35 #include <drm/i915_drm.h>
38 #include "intel_display_types.h"
39 #include "intel_gmbus.h"
46 /* Map gmbus pin pairs to names and registers. */
47 static const struct gmbus_pin gmbus_pins
[] = {
48 [GMBUS_PIN_SSC
] = { "ssc", GPIOB
},
49 [GMBUS_PIN_VGADDC
] = { "vga", GPIOA
},
50 [GMBUS_PIN_PANEL
] = { "panel", GPIOC
},
51 [GMBUS_PIN_DPC
] = { "dpc", GPIOD
},
52 [GMBUS_PIN_DPB
] = { "dpb", GPIOE
},
53 [GMBUS_PIN_DPD
] = { "dpd", GPIOF
},
56 static const struct gmbus_pin gmbus_pins_bdw
[] = {
57 [GMBUS_PIN_VGADDC
] = { "vga", GPIOA
},
58 [GMBUS_PIN_DPC
] = { "dpc", GPIOD
},
59 [GMBUS_PIN_DPB
] = { "dpb", GPIOE
},
60 [GMBUS_PIN_DPD
] = { "dpd", GPIOF
},
63 static const struct gmbus_pin gmbus_pins_skl
[] = {
64 [GMBUS_PIN_DPC
] = { "dpc", GPIOD
},
65 [GMBUS_PIN_DPB
] = { "dpb", GPIOE
},
66 [GMBUS_PIN_DPD
] = { "dpd", GPIOF
},
69 static const struct gmbus_pin gmbus_pins_bxt
[] = {
70 [GMBUS_PIN_1_BXT
] = { "dpb", GPIOB
},
71 [GMBUS_PIN_2_BXT
] = { "dpc", GPIOC
},
72 [GMBUS_PIN_3_BXT
] = { "misc", GPIOD
},
75 static const struct gmbus_pin gmbus_pins_cnp
[] = {
76 [GMBUS_PIN_1_BXT
] = { "dpb", GPIOB
},
77 [GMBUS_PIN_2_BXT
] = { "dpc", GPIOC
},
78 [GMBUS_PIN_3_BXT
] = { "misc", GPIOD
},
79 [GMBUS_PIN_4_CNP
] = { "dpd", GPIOE
},
82 static const struct gmbus_pin gmbus_pins_icp
[] = {
83 [GMBUS_PIN_1_BXT
] = { "dpa", GPIOB
},
84 [GMBUS_PIN_2_BXT
] = { "dpb", GPIOC
},
85 [GMBUS_PIN_3_BXT
] = { "dpc", GPIOD
},
86 [GMBUS_PIN_9_TC1_ICP
] = { "tc1", GPIOJ
},
87 [GMBUS_PIN_10_TC2_ICP
] = { "tc2", GPIOK
},
88 [GMBUS_PIN_11_TC3_ICP
] = { "tc3", GPIOL
},
89 [GMBUS_PIN_12_TC4_ICP
] = { "tc4", GPIOM
},
90 [GMBUS_PIN_13_TC5_TGP
] = { "tc5", GPION
},
91 [GMBUS_PIN_14_TC6_TGP
] = { "tc6", GPIOO
},
94 /* pin is expected to be valid */
95 static const struct gmbus_pin
*get_gmbus_pin(struct drm_i915_private
*dev_priv
,
98 if (INTEL_PCH_TYPE(dev_priv
) >= PCH_ICP
)
99 return &gmbus_pins_icp
[pin
];
100 else if (HAS_PCH_CNP(dev_priv
))
101 return &gmbus_pins_cnp
[pin
];
102 else if (IS_GEN9_LP(dev_priv
))
103 return &gmbus_pins_bxt
[pin
];
104 else if (IS_GEN9_BC(dev_priv
))
105 return &gmbus_pins_skl
[pin
];
106 else if (IS_BROADWELL(dev_priv
))
107 return &gmbus_pins_bdw
[pin
];
109 return &gmbus_pins
[pin
];
112 bool intel_gmbus_is_valid_pin(struct drm_i915_private
*dev_priv
,
117 if (INTEL_PCH_TYPE(dev_priv
) >= PCH_ICP
)
118 size
= ARRAY_SIZE(gmbus_pins_icp
);
119 else if (HAS_PCH_CNP(dev_priv
))
120 size
= ARRAY_SIZE(gmbus_pins_cnp
);
121 else if (IS_GEN9_LP(dev_priv
))
122 size
= ARRAY_SIZE(gmbus_pins_bxt
);
123 else if (IS_GEN9_BC(dev_priv
))
124 size
= ARRAY_SIZE(gmbus_pins_skl
);
125 else if (IS_BROADWELL(dev_priv
))
126 size
= ARRAY_SIZE(gmbus_pins_bdw
);
128 size
= ARRAY_SIZE(gmbus_pins
);
130 return pin
< size
&& get_gmbus_pin(dev_priv
, pin
)->name
;
133 /* Intel GPIO access functions */
135 #define I2C_RISEFALL_TIME 10
137 static inline struct intel_gmbus
*
138 to_intel_gmbus(struct i2c_adapter
*i2c
)
140 return container_of(i2c
, struct intel_gmbus
, adapter
);
144 intel_gmbus_reset(struct drm_i915_private
*dev_priv
)
146 I915_WRITE(GMBUS0
, 0);
147 I915_WRITE(GMBUS4
, 0);
150 static void pnv_gmbus_clock_gating(struct drm_i915_private
*dev_priv
,
155 /* When using bit bashing for I2C, this bit needs to be set to 1 */
156 val
= I915_READ(DSPCLK_GATE_D
);
158 val
|= PNV_GMBUSUNIT_CLOCK_GATE_DISABLE
;
160 val
&= ~PNV_GMBUSUNIT_CLOCK_GATE_DISABLE
;
161 I915_WRITE(DSPCLK_GATE_D
, val
);
164 static void pch_gmbus_clock_gating(struct drm_i915_private
*dev_priv
,
169 val
= I915_READ(SOUTH_DSPCLK_GATE_D
);
171 val
|= PCH_GMBUSUNIT_CLOCK_GATE_DISABLE
;
173 val
&= ~PCH_GMBUSUNIT_CLOCK_GATE_DISABLE
;
174 I915_WRITE(SOUTH_DSPCLK_GATE_D
, val
);
177 static void bxt_gmbus_clock_gating(struct drm_i915_private
*dev_priv
,
182 val
= I915_READ(GEN9_CLKGATE_DIS_4
);
184 val
|= BXT_GMBUS_GATING_DIS
;
186 val
&= ~BXT_GMBUS_GATING_DIS
;
187 I915_WRITE(GEN9_CLKGATE_DIS_4
, val
);
190 static u32
get_reserved(struct intel_gmbus
*bus
)
192 struct drm_i915_private
*i915
= bus
->dev_priv
;
193 struct intel_uncore
*uncore
= &i915
->uncore
;
196 /* On most chips, these bits must be preserved in software. */
197 if (!IS_I830(i915
) && !IS_I845G(i915
))
198 reserved
= intel_uncore_read_notrace(uncore
, bus
->gpio_reg
) &
199 (GPIO_DATA_PULLUP_DISABLE
|
200 GPIO_CLOCK_PULLUP_DISABLE
);
205 static int get_clock(void *data
)
207 struct intel_gmbus
*bus
= data
;
208 struct intel_uncore
*uncore
= &bus
->dev_priv
->uncore
;
209 u32 reserved
= get_reserved(bus
);
211 intel_uncore_write_notrace(uncore
,
213 reserved
| GPIO_CLOCK_DIR_MASK
);
214 intel_uncore_write_notrace(uncore
, bus
->gpio_reg
, reserved
);
216 return (intel_uncore_read_notrace(uncore
, bus
->gpio_reg
) &
217 GPIO_CLOCK_VAL_IN
) != 0;
220 static int get_data(void *data
)
222 struct intel_gmbus
*bus
= data
;
223 struct intel_uncore
*uncore
= &bus
->dev_priv
->uncore
;
224 u32 reserved
= get_reserved(bus
);
226 intel_uncore_write_notrace(uncore
,
228 reserved
| GPIO_DATA_DIR_MASK
);
229 intel_uncore_write_notrace(uncore
, bus
->gpio_reg
, reserved
);
231 return (intel_uncore_read_notrace(uncore
, bus
->gpio_reg
) &
232 GPIO_DATA_VAL_IN
) != 0;
235 static void set_clock(void *data
, int state_high
)
237 struct intel_gmbus
*bus
= data
;
238 struct intel_uncore
*uncore
= &bus
->dev_priv
->uncore
;
239 u32 reserved
= get_reserved(bus
);
243 clock_bits
= GPIO_CLOCK_DIR_IN
| GPIO_CLOCK_DIR_MASK
;
245 clock_bits
= GPIO_CLOCK_DIR_OUT
| GPIO_CLOCK_DIR_MASK
|
248 intel_uncore_write_notrace(uncore
,
250 reserved
| clock_bits
);
251 intel_uncore_posting_read(uncore
, bus
->gpio_reg
);
254 static void set_data(void *data
, int state_high
)
256 struct intel_gmbus
*bus
= data
;
257 struct intel_uncore
*uncore
= &bus
->dev_priv
->uncore
;
258 u32 reserved
= get_reserved(bus
);
262 data_bits
= GPIO_DATA_DIR_IN
| GPIO_DATA_DIR_MASK
;
264 data_bits
= GPIO_DATA_DIR_OUT
| GPIO_DATA_DIR_MASK
|
267 intel_uncore_write_notrace(uncore
, bus
->gpio_reg
, reserved
| data_bits
);
268 intel_uncore_posting_read(uncore
, bus
->gpio_reg
);
272 intel_gpio_pre_xfer(struct i2c_adapter
*adapter
)
274 struct intel_gmbus
*bus
= container_of(adapter
,
277 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
279 intel_gmbus_reset(dev_priv
);
281 if (IS_PINEVIEW(dev_priv
))
282 pnv_gmbus_clock_gating(dev_priv
, false);
286 udelay(I2C_RISEFALL_TIME
);
291 intel_gpio_post_xfer(struct i2c_adapter
*adapter
)
293 struct intel_gmbus
*bus
= container_of(adapter
,
296 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
301 if (IS_PINEVIEW(dev_priv
))
302 pnv_gmbus_clock_gating(dev_priv
, true);
306 intel_gpio_setup(struct intel_gmbus
*bus
, unsigned int pin
)
308 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
309 struct i2c_algo_bit_data
*algo
;
311 algo
= &bus
->bit_algo
;
313 bus
->gpio_reg
= GPIO(get_gmbus_pin(dev_priv
, pin
)->gpio
);
314 bus
->adapter
.algo_data
= algo
;
315 algo
->setsda
= set_data
;
316 algo
->setscl
= set_clock
;
317 algo
->getsda
= get_data
;
318 algo
->getscl
= get_clock
;
319 algo
->pre_xfer
= intel_gpio_pre_xfer
;
320 algo
->post_xfer
= intel_gpio_post_xfer
;
321 algo
->udelay
= I2C_RISEFALL_TIME
;
322 algo
->timeout
= usecs_to_jiffies(2200);
326 static int gmbus_wait(struct drm_i915_private
*dev_priv
, u32 status
, u32 irq_en
)
332 /* Important: The hw handles only the first bit, so set only one! Since
333 * we also need to check for NAKs besides the hw ready/idle signal, we
334 * need to wake up periodically and check that ourselves.
336 if (!HAS_GMBUS_IRQ(dev_priv
))
339 add_wait_queue(&dev_priv
->gmbus_wait_queue
, &wait
);
340 I915_WRITE_FW(GMBUS4
, irq_en
);
342 status
|= GMBUS_SATOER
;
343 ret
= wait_for_us((gmbus2
= I915_READ_FW(GMBUS2
)) & status
, 2);
345 ret
= wait_for((gmbus2
= I915_READ_FW(GMBUS2
)) & status
, 50);
347 I915_WRITE_FW(GMBUS4
, 0);
348 remove_wait_queue(&dev_priv
->gmbus_wait_queue
, &wait
);
350 if (gmbus2
& GMBUS_SATOER
)
357 gmbus_wait_idle(struct drm_i915_private
*dev_priv
)
363 /* Important: The hw handles only the first bit, so set only one! */
365 if (HAS_GMBUS_IRQ(dev_priv
))
366 irq_enable
= GMBUS_IDLE_EN
;
368 add_wait_queue(&dev_priv
->gmbus_wait_queue
, &wait
);
369 I915_WRITE_FW(GMBUS4
, irq_enable
);
371 ret
= intel_wait_for_register_fw(&dev_priv
->uncore
,
372 GMBUS2
, GMBUS_ACTIVE
, 0,
375 I915_WRITE_FW(GMBUS4
, 0);
376 remove_wait_queue(&dev_priv
->gmbus_wait_queue
, &wait
);
382 unsigned int gmbus_max_xfer_size(struct drm_i915_private
*dev_priv
)
384 return INTEL_GEN(dev_priv
) >= 9 ? GEN9_GMBUS_BYTE_COUNT_MAX
:
385 GMBUS_BYTE_COUNT_MAX
;
389 gmbus_xfer_read_chunk(struct drm_i915_private
*dev_priv
,
390 unsigned short addr
, u8
*buf
, unsigned int len
,
391 u32 gmbus0_reg
, u32 gmbus1_index
)
393 unsigned int size
= len
;
394 bool burst_read
= len
> gmbus_max_xfer_size(dev_priv
);
395 bool extra_byte_added
= false;
399 * As per HW Spec, for 512Bytes need to read extra Byte and
400 * Ignore the extra byte read.
403 extra_byte_added
= true;
406 size
= len
% 256 + 256;
407 I915_WRITE_FW(GMBUS0
, gmbus0_reg
| GMBUS_BYTE_CNT_OVERRIDE
);
410 I915_WRITE_FW(GMBUS1
,
413 (size
<< GMBUS_BYTE_COUNT_SHIFT
) |
414 (addr
<< GMBUS_SLAVE_ADDR_SHIFT
) |
415 GMBUS_SLAVE_READ
| GMBUS_SW_RDY
);
420 ret
= gmbus_wait(dev_priv
, GMBUS_HW_RDY
, GMBUS_HW_RDY_EN
);
424 val
= I915_READ_FW(GMBUS3
);
426 if (extra_byte_added
&& len
== 1)
431 } while (--len
&& ++loop
< 4);
433 if (burst_read
&& len
== size
- 4)
434 /* Reset the override bit */
435 I915_WRITE_FW(GMBUS0
, gmbus0_reg
);
442 * HW spec says that 512Bytes in Burst read need special treatment.
443 * But it doesn't talk about other multiple of 256Bytes. And couldn't locate
444 * an I2C slave, which supports such a lengthy burst read too for experiments.
446 * So until things get clarified on HW support, to avoid the burst read length
447 * in fold of 256Bytes except 512, max burst read length is fixed at 767Bytes.
449 #define INTEL_GMBUS_BURST_READ_MAX_LEN 767U
452 gmbus_xfer_read(struct drm_i915_private
*dev_priv
, struct i2c_msg
*msg
,
453 u32 gmbus0_reg
, u32 gmbus1_index
)
456 unsigned int rx_size
= msg
->len
;
461 if (HAS_GMBUS_BURST_READ(dev_priv
))
462 len
= min(rx_size
, INTEL_GMBUS_BURST_READ_MAX_LEN
);
464 len
= min(rx_size
, gmbus_max_xfer_size(dev_priv
));
466 ret
= gmbus_xfer_read_chunk(dev_priv
, msg
->addr
, buf
, len
,
467 gmbus0_reg
, gmbus1_index
);
473 } while (rx_size
!= 0);
479 gmbus_xfer_write_chunk(struct drm_i915_private
*dev_priv
,
480 unsigned short addr
, u8
*buf
, unsigned int len
,
483 unsigned int chunk_size
= len
;
487 while (len
&& loop
< 4) {
488 val
|= *buf
++ << (8 * loop
++);
492 I915_WRITE_FW(GMBUS3
, val
);
493 I915_WRITE_FW(GMBUS1
,
494 gmbus1_index
| GMBUS_CYCLE_WAIT
|
495 (chunk_size
<< GMBUS_BYTE_COUNT_SHIFT
) |
496 (addr
<< GMBUS_SLAVE_ADDR_SHIFT
) |
497 GMBUS_SLAVE_WRITE
| GMBUS_SW_RDY
);
503 val
|= *buf
++ << (8 * loop
);
504 } while (--len
&& ++loop
< 4);
506 I915_WRITE_FW(GMBUS3
, val
);
508 ret
= gmbus_wait(dev_priv
, GMBUS_HW_RDY
, GMBUS_HW_RDY_EN
);
517 gmbus_xfer_write(struct drm_i915_private
*dev_priv
, struct i2c_msg
*msg
,
521 unsigned int tx_size
= msg
->len
;
526 len
= min(tx_size
, gmbus_max_xfer_size(dev_priv
));
528 ret
= gmbus_xfer_write_chunk(dev_priv
, msg
->addr
, buf
, len
,
535 } while (tx_size
!= 0);
541 * The gmbus controller can combine a 1 or 2 byte write with another read/write
542 * that immediately follows it by using an "INDEX" cycle.
545 gmbus_is_index_xfer(struct i2c_msg
*msgs
, int i
, int num
)
547 return (i
+ 1 < num
&&
548 msgs
[i
].addr
== msgs
[i
+ 1].addr
&&
549 !(msgs
[i
].flags
& I2C_M_RD
) &&
550 (msgs
[i
].len
== 1 || msgs
[i
].len
== 2) &&
551 msgs
[i
+ 1].len
> 0);
555 gmbus_index_xfer(struct drm_i915_private
*dev_priv
, struct i2c_msg
*msgs
,
558 u32 gmbus1_index
= 0;
562 if (msgs
[0].len
== 2)
563 gmbus5
= GMBUS_2BYTE_INDEX_EN
|
564 msgs
[0].buf
[1] | (msgs
[0].buf
[0] << 8);
565 if (msgs
[0].len
== 1)
566 gmbus1_index
= GMBUS_CYCLE_INDEX
|
567 (msgs
[0].buf
[0] << GMBUS_SLAVE_INDEX_SHIFT
);
569 /* GMBUS5 holds 16-bit index */
571 I915_WRITE_FW(GMBUS5
, gmbus5
);
573 if (msgs
[1].flags
& I2C_M_RD
)
574 ret
= gmbus_xfer_read(dev_priv
, &msgs
[1], gmbus0_reg
,
577 ret
= gmbus_xfer_write(dev_priv
, &msgs
[1], gmbus1_index
);
579 /* Clear GMBUS5 after each index transfer */
581 I915_WRITE_FW(GMBUS5
, 0);
587 do_gmbus_xfer(struct i2c_adapter
*adapter
, struct i2c_msg
*msgs
, int num
,
590 struct intel_gmbus
*bus
= container_of(adapter
,
593 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
594 int i
= 0, inc
, try = 0;
597 /* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
598 if (IS_GEN9_LP(dev_priv
))
599 bxt_gmbus_clock_gating(dev_priv
, false);
600 else if (HAS_PCH_SPT(dev_priv
) || HAS_PCH_CNP(dev_priv
))
601 pch_gmbus_clock_gating(dev_priv
, false);
604 I915_WRITE_FW(GMBUS0
, gmbus0_source
| bus
->reg0
);
606 for (; i
< num
; i
+= inc
) {
608 if (gmbus_is_index_xfer(msgs
, i
, num
)) {
609 ret
= gmbus_index_xfer(dev_priv
, &msgs
[i
],
610 gmbus0_source
| bus
->reg0
);
611 inc
= 2; /* an index transmission is two msgs */
612 } else if (msgs
[i
].flags
& I2C_M_RD
) {
613 ret
= gmbus_xfer_read(dev_priv
, &msgs
[i
],
614 gmbus0_source
| bus
->reg0
, 0);
616 ret
= gmbus_xfer_write(dev_priv
, &msgs
[i
], 0);
620 ret
= gmbus_wait(dev_priv
,
621 GMBUS_HW_WAIT_PHASE
, GMBUS_HW_WAIT_EN
);
622 if (ret
== -ETIMEDOUT
)
628 /* Generate a STOP condition on the bus. Note that gmbus can't generata
629 * a STOP on the very first cycle. To simplify the code we
630 * unconditionally generate the STOP condition with an additional gmbus
632 I915_WRITE_FW(GMBUS1
, GMBUS_CYCLE_STOP
| GMBUS_SW_RDY
);
634 /* Mark the GMBUS interface as disabled after waiting for idle.
635 * We will re-enable it at the start of the next xfer,
636 * till then let it sleep.
638 if (gmbus_wait_idle(dev_priv
)) {
639 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
643 I915_WRITE_FW(GMBUS0
, 0);
649 * Wait for bus to IDLE before clearing NAK.
650 * If we clear the NAK while bus is still active, then it will stay
651 * active and the next transaction may fail.
653 * If no ACK is received during the address phase of a transaction, the
654 * adapter must report -ENXIO. It is not clear what to return if no ACK
655 * is received at other times. But we have to be careful to not return
656 * spurious -ENXIO because that will prevent i2c and drm edid functions
657 * from retrying. So return -ENXIO only when gmbus properly quiescents -
658 * timing out seems to happen when there _is_ a ddc chip present, but
659 * it's slow responding and only answers on the 2nd retry.
662 if (gmbus_wait_idle(dev_priv
)) {
663 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
668 /* Toggle the Software Clear Interrupt bit. This has the effect
669 * of resetting the GMBUS controller and so clearing the
670 * BUS_ERROR raised by the slave's NAK.
672 I915_WRITE_FW(GMBUS1
, GMBUS_SW_CLR_INT
);
673 I915_WRITE_FW(GMBUS1
, 0);
674 I915_WRITE_FW(GMBUS0
, 0);
676 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
677 adapter
->name
, msgs
[i
].addr
,
678 (msgs
[i
].flags
& I2C_M_RD
) ? 'r' : 'w', msgs
[i
].len
);
681 * Passive adapters sometimes NAK the first probe. Retry the first
682 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
683 * has retries internally. See also the retry loop in
684 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
686 if (ret
== -ENXIO
&& i
== 0 && try++ == 0) {
687 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
695 DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
696 bus
->adapter
.name
, bus
->reg0
& 0xff);
697 I915_WRITE_FW(GMBUS0
, 0);
700 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
701 * instead. Use EAGAIN to have i2c core retry.
706 /* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
707 if (IS_GEN9_LP(dev_priv
))
708 bxt_gmbus_clock_gating(dev_priv
, true);
709 else if (HAS_PCH_SPT(dev_priv
) || HAS_PCH_CNP(dev_priv
))
710 pch_gmbus_clock_gating(dev_priv
, true);
716 gmbus_xfer(struct i2c_adapter
*adapter
, struct i2c_msg
*msgs
, int num
)
718 struct intel_gmbus
*bus
=
719 container_of(adapter
, struct intel_gmbus
, adapter
);
720 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
721 intel_wakeref_t wakeref
;
724 wakeref
= intel_display_power_get(dev_priv
, POWER_DOMAIN_GMBUS
);
726 if (bus
->force_bit
) {
727 ret
= i2c_bit_algo
.master_xfer(adapter
, msgs
, num
);
729 bus
->force_bit
&= ~GMBUS_FORCE_BIT_RETRY
;
731 ret
= do_gmbus_xfer(adapter
, msgs
, num
, 0);
733 bus
->force_bit
|= GMBUS_FORCE_BIT_RETRY
;
736 intel_display_power_put(dev_priv
, POWER_DOMAIN_GMBUS
, wakeref
);
741 int intel_gmbus_output_aksv(struct i2c_adapter
*adapter
)
743 struct intel_gmbus
*bus
=
744 container_of(adapter
, struct intel_gmbus
, adapter
);
745 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
746 u8 cmd
= DRM_HDCP_DDC_AKSV
;
747 u8 buf
[DRM_HDCP_KSV_LEN
] = { 0 };
748 struct i2c_msg msgs
[] = {
750 .addr
= DRM_HDCP_DDC_ADDR
,
756 .addr
= DRM_HDCP_DDC_ADDR
,
762 intel_wakeref_t wakeref
;
765 wakeref
= intel_display_power_get(dev_priv
, POWER_DOMAIN_GMBUS
);
766 mutex_lock(&dev_priv
->gmbus_mutex
);
769 * In order to output Aksv to the receiver, use an indexed write to
770 * pass the i2c command, and tell GMBUS to use the HW-provided value
771 * instead of sourcing GMBUS3 for the data.
773 ret
= do_gmbus_xfer(adapter
, msgs
, ARRAY_SIZE(msgs
), GMBUS_AKSV_SELECT
);
775 mutex_unlock(&dev_priv
->gmbus_mutex
);
776 intel_display_power_put(dev_priv
, POWER_DOMAIN_GMBUS
, wakeref
);
781 static u32
gmbus_func(struct i2c_adapter
*adapter
)
783 return i2c_bit_algo
.functionality(adapter
) &
784 (I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
785 /* I2C_FUNC_10BIT_ADDR | */
786 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
787 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
);
790 static const struct i2c_algorithm gmbus_algorithm
= {
791 .master_xfer
= gmbus_xfer
,
792 .functionality
= gmbus_func
795 static void gmbus_lock_bus(struct i2c_adapter
*adapter
,
798 struct intel_gmbus
*bus
= to_intel_gmbus(adapter
);
799 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
801 mutex_lock(&dev_priv
->gmbus_mutex
);
804 static int gmbus_trylock_bus(struct i2c_adapter
*adapter
,
807 struct intel_gmbus
*bus
= to_intel_gmbus(adapter
);
808 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
810 return mutex_trylock(&dev_priv
->gmbus_mutex
);
813 static void gmbus_unlock_bus(struct i2c_adapter
*adapter
,
816 struct intel_gmbus
*bus
= to_intel_gmbus(adapter
);
817 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
819 mutex_unlock(&dev_priv
->gmbus_mutex
);
822 static const struct i2c_lock_operations gmbus_lock_ops
= {
823 .lock_bus
= gmbus_lock_bus
,
824 .trylock_bus
= gmbus_trylock_bus
,
825 .unlock_bus
= gmbus_unlock_bus
,
829 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
830 * @dev_priv: i915 device private
832 int intel_gmbus_setup(struct drm_i915_private
*dev_priv
)
834 struct pci_dev
*pdev
= dev_priv
->drm
.pdev
;
835 struct intel_gmbus
*bus
;
839 if (!HAS_DISPLAY(dev_priv
) || !INTEL_DISPLAY_ENABLED(dev_priv
))
842 if (IS_VALLEYVIEW(dev_priv
) || IS_CHERRYVIEW(dev_priv
))
843 dev_priv
->gpio_mmio_base
= VLV_DISPLAY_BASE
;
844 else if (!HAS_GMCH(dev_priv
))
846 * Broxton uses the same PCH offsets for South Display Engine,
847 * even though it doesn't have a PCH.
849 dev_priv
->gpio_mmio_base
= PCH_DISPLAY_BASE
;
851 mutex_init(&dev_priv
->gmbus_mutex
);
852 init_waitqueue_head(&dev_priv
->gmbus_wait_queue
);
854 for (pin
= 0; pin
< ARRAY_SIZE(dev_priv
->gmbus
); pin
++) {
855 if (!intel_gmbus_is_valid_pin(dev_priv
, pin
))
858 bus
= &dev_priv
->gmbus
[pin
];
860 bus
->adapter
.owner
= THIS_MODULE
;
861 bus
->adapter
.class = I2C_CLASS_DDC
;
862 snprintf(bus
->adapter
.name
,
863 sizeof(bus
->adapter
.name
),
865 get_gmbus_pin(dev_priv
, pin
)->name
);
867 bus
->adapter
.dev
.parent
= &pdev
->dev
;
868 bus
->dev_priv
= dev_priv
;
870 bus
->adapter
.algo
= &gmbus_algorithm
;
871 bus
->adapter
.lock_ops
= &gmbus_lock_ops
;
874 * We wish to retry with bit banging
875 * after a timed out GMBUS attempt.
877 bus
->adapter
.retries
= 1;
879 /* By default use a conservative clock rate */
880 bus
->reg0
= pin
| GMBUS_RATE_100KHZ
;
882 /* gmbus seems to be broken on i830 */
883 if (IS_I830(dev_priv
))
886 intel_gpio_setup(bus
, pin
);
888 ret
= i2c_add_adapter(&bus
->adapter
);
893 intel_gmbus_reset(dev_priv
);
899 if (!intel_gmbus_is_valid_pin(dev_priv
, pin
))
902 bus
= &dev_priv
->gmbus
[pin
];
903 i2c_del_adapter(&bus
->adapter
);
908 struct i2c_adapter
*intel_gmbus_get_adapter(struct drm_i915_private
*dev_priv
,
911 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv
, pin
)))
914 return &dev_priv
->gmbus
[pin
].adapter
;
917 void intel_gmbus_set_speed(struct i2c_adapter
*adapter
, int speed
)
919 struct intel_gmbus
*bus
= to_intel_gmbus(adapter
);
921 bus
->reg0
= (bus
->reg0
& ~(0x3 << 8)) | speed
;
924 void intel_gmbus_force_bit(struct i2c_adapter
*adapter
, bool force_bit
)
926 struct intel_gmbus
*bus
= to_intel_gmbus(adapter
);
927 struct drm_i915_private
*dev_priv
= bus
->dev_priv
;
929 mutex_lock(&dev_priv
->gmbus_mutex
);
931 bus
->force_bit
+= force_bit
? 1 : -1;
932 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
933 force_bit
? "en" : "dis", adapter
->name
,
936 mutex_unlock(&dev_priv
->gmbus_mutex
);
939 bool intel_gmbus_is_forced_bit(struct i2c_adapter
*adapter
)
941 struct intel_gmbus
*bus
= to_intel_gmbus(adapter
);
943 return bus
->force_bit
;
946 void intel_gmbus_teardown(struct drm_i915_private
*dev_priv
)
948 struct intel_gmbus
*bus
;
951 for (pin
= 0; pin
< ARRAY_SIZE(dev_priv
->gmbus
); pin
++) {
952 if (!intel_gmbus_is_valid_pin(dev_priv
, pin
))
955 bus
= &dev_priv
->gmbus
[pin
];
956 i2c_del_adapter(&bus
->adapter
);