1379bc100SJani Nikula /*
2379bc100SJani Nikula  * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3379bc100SJani Nikula  * Copyright © 2006-2008,2010 Intel Corporation
4379bc100SJani Nikula  *   Jesse Barnes <jesse.barnes@intel.com>
5379bc100SJani Nikula  *
6379bc100SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
7379bc100SJani Nikula  * copy of this software and associated documentation files (the "Software"),
8379bc100SJani Nikula  * to deal in the Software without restriction, including without limitation
9379bc100SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10379bc100SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
11379bc100SJani Nikula  * Software is furnished to do so, subject to the following conditions:
12379bc100SJani Nikula  *
13379bc100SJani Nikula  * The above copyright notice and this permission notice (including the next
14379bc100SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
15379bc100SJani Nikula  * Software.
16379bc100SJani Nikula  *
17379bc100SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18379bc100SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19379bc100SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20379bc100SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21379bc100SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22379bc100SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23379bc100SJani Nikula  * DEALINGS IN THE SOFTWARE.
24379bc100SJani Nikula  *
25379bc100SJani Nikula  * Authors:
26379bc100SJani Nikula  *	Eric Anholt <eric@anholt.net>
27379bc100SJani Nikula  *	Chris Wilson <chris@chris-wilson.co.uk>
28379bc100SJani Nikula  */
29379bc100SJani Nikula 
30379bc100SJani Nikula #include <linux/export.h>
31379bc100SJani Nikula #include <linux/i2c-algo-bit.h>
32379bc100SJani Nikula #include <linux/i2c.h>
33379bc100SJani Nikula 
34379bc100SJani Nikula #include <drm/drm_hdcp.h>
35379bc100SJani Nikula 
36379bc100SJani Nikula #include "i915_drv.h"
371d455f8dSJani Nikula #include "intel_display_types.h"
38379bc100SJani Nikula #include "intel_gmbus.h"
39379bc100SJani Nikula 
40379bc100SJani Nikula struct gmbus_pin {
41379bc100SJani Nikula 	const char *name;
42379bc100SJani Nikula 	enum i915_gpio gpio;
43379bc100SJani Nikula };
44379bc100SJani Nikula 
45379bc100SJani Nikula /* Map gmbus pin pairs to names and registers. */
46379bc100SJani Nikula static const struct gmbus_pin gmbus_pins[] = {
47379bc100SJani Nikula 	[GMBUS_PIN_SSC] = { "ssc", GPIOB },
48379bc100SJani Nikula 	[GMBUS_PIN_VGADDC] = { "vga", GPIOA },
49379bc100SJani Nikula 	[GMBUS_PIN_PANEL] = { "panel", GPIOC },
50379bc100SJani Nikula 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
51379bc100SJani Nikula 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
52379bc100SJani Nikula 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
53379bc100SJani Nikula };
54379bc100SJani Nikula 
55379bc100SJani Nikula static const struct gmbus_pin gmbus_pins_bdw[] = {
56379bc100SJani Nikula 	[GMBUS_PIN_VGADDC] = { "vga", GPIOA },
57379bc100SJani Nikula 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
58379bc100SJani Nikula 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
59379bc100SJani Nikula 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
60379bc100SJani Nikula };
61379bc100SJani Nikula 
62379bc100SJani Nikula static const struct gmbus_pin gmbus_pins_skl[] = {
63379bc100SJani Nikula 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
64379bc100SJani Nikula 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
65379bc100SJani Nikula 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
66379bc100SJani Nikula };
67379bc100SJani Nikula 
68379bc100SJani Nikula static const struct gmbus_pin gmbus_pins_bxt[] = {
69379bc100SJani Nikula 	[GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
70379bc100SJani Nikula 	[GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
71379bc100SJani Nikula 	[GMBUS_PIN_3_BXT] = { "misc", GPIOD },
72379bc100SJani Nikula };
73379bc100SJani Nikula 
74379bc100SJani Nikula static const struct gmbus_pin gmbus_pins_cnp[] = {
75379bc100SJani Nikula 	[GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
76379bc100SJani Nikula 	[GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
77379bc100SJani Nikula 	[GMBUS_PIN_3_BXT] = { "misc", GPIOD },
78379bc100SJani Nikula 	[GMBUS_PIN_4_CNP] = { "dpd", GPIOE },
79379bc100SJani Nikula };
80379bc100SJani Nikula 
81379bc100SJani Nikula static const struct gmbus_pin gmbus_pins_icp[] = {
82379bc100SJani Nikula 	[GMBUS_PIN_1_BXT] = { "dpa", GPIOB },
83379bc100SJani Nikula 	[GMBUS_PIN_2_BXT] = { "dpb", GPIOC },
843fd53262SMahesh Kumar 	[GMBUS_PIN_3_BXT] = { "dpc", GPIOD },
853fd53262SMahesh Kumar 	[GMBUS_PIN_9_TC1_ICP] = { "tc1", GPIOJ },
863fd53262SMahesh Kumar 	[GMBUS_PIN_10_TC2_ICP] = { "tc2", GPIOK },
873fd53262SMahesh Kumar 	[GMBUS_PIN_11_TC3_ICP] = { "tc3", GPIOL },
883fd53262SMahesh Kumar 	[GMBUS_PIN_12_TC4_ICP] = { "tc4", GPIOM },
893fd53262SMahesh Kumar 	[GMBUS_PIN_13_TC5_TGP] = { "tc5", GPION },
903fd53262SMahesh Kumar 	[GMBUS_PIN_14_TC6_TGP] = { "tc6", GPIOO },
913fd53262SMahesh Kumar };
923fd53262SMahesh Kumar 
93*fb7318c3SLucas De Marchi static const struct gmbus_pin gmbus_pins_dg1[] = {
94*fb7318c3SLucas De Marchi 	[GMBUS_PIN_1_BXT] = { "dpa", GPIOB },
95*fb7318c3SLucas De Marchi 	[GMBUS_PIN_2_BXT] = { "dpb", GPIOC },
96*fb7318c3SLucas De Marchi 	[GMBUS_PIN_3_BXT] = { "dpc", GPIOD },
97*fb7318c3SLucas De Marchi 	[GMBUS_PIN_4_CNP] = { "dpd", GPIOE },
98*fb7318c3SLucas De Marchi };
99*fb7318c3SLucas De Marchi 
100379bc100SJani Nikula /* pin is expected to be valid */
101379bc100SJani Nikula static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
102379bc100SJani Nikula 					     unsigned int pin)
103379bc100SJani Nikula {
104*fb7318c3SLucas De Marchi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_DG1)
105*fb7318c3SLucas De Marchi 		return &gmbus_pins_dg1[pin];
106*fb7318c3SLucas De Marchi 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
107379bc100SJani Nikula 		return &gmbus_pins_icp[pin];
108379bc100SJani Nikula 	else if (HAS_PCH_CNP(dev_priv))
109379bc100SJani Nikula 		return &gmbus_pins_cnp[pin];
110379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
111379bc100SJani Nikula 		return &gmbus_pins_bxt[pin];
112379bc100SJani Nikula 	else if (IS_GEN9_BC(dev_priv))
113379bc100SJani Nikula 		return &gmbus_pins_skl[pin];
114379bc100SJani Nikula 	else if (IS_BROADWELL(dev_priv))
115379bc100SJani Nikula 		return &gmbus_pins_bdw[pin];
116379bc100SJani Nikula 	else
117379bc100SJani Nikula 		return &gmbus_pins[pin];
118379bc100SJani Nikula }
119379bc100SJani Nikula 
120379bc100SJani Nikula bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
121379bc100SJani Nikula 			      unsigned int pin)
122379bc100SJani Nikula {
123379bc100SJani Nikula 	unsigned int size;
124379bc100SJani Nikula 
125*fb7318c3SLucas De Marchi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_DG1)
126*fb7318c3SLucas De Marchi 		size = ARRAY_SIZE(gmbus_pins_dg1);
127*fb7318c3SLucas De Marchi 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
128379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_icp);
129379bc100SJani Nikula 	else if (HAS_PCH_CNP(dev_priv))
130379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_cnp);
131379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
132379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_bxt);
133379bc100SJani Nikula 	else if (IS_GEN9_BC(dev_priv))
134379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_skl);
135379bc100SJani Nikula 	else if (IS_BROADWELL(dev_priv))
136379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_bdw);
137379bc100SJani Nikula 	else
138379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins);
139379bc100SJani Nikula 
140379bc100SJani Nikula 	return pin < size && get_gmbus_pin(dev_priv, pin)->name;
141379bc100SJani Nikula }
142379bc100SJani Nikula 
143379bc100SJani Nikula /* Intel GPIO access functions */
144379bc100SJani Nikula 
145379bc100SJani Nikula #define I2C_RISEFALL_TIME 10
146379bc100SJani Nikula 
147379bc100SJani Nikula static inline struct intel_gmbus *
148379bc100SJani Nikula to_intel_gmbus(struct i2c_adapter *i2c)
149379bc100SJani Nikula {
150379bc100SJani Nikula 	return container_of(i2c, struct intel_gmbus, adapter);
151379bc100SJani Nikula }
152379bc100SJani Nikula 
153379bc100SJani Nikula void
154379bc100SJani Nikula intel_gmbus_reset(struct drm_i915_private *dev_priv)
155379bc100SJani Nikula {
156d9053b23SJani Nikula 	intel_de_write(dev_priv, GMBUS0, 0);
157d9053b23SJani Nikula 	intel_de_write(dev_priv, GMBUS4, 0);
158379bc100SJani Nikula }
159379bc100SJani Nikula 
160379bc100SJani Nikula static void pnv_gmbus_clock_gating(struct drm_i915_private *dev_priv,
161379bc100SJani Nikula 				   bool enable)
162379bc100SJani Nikula {
163379bc100SJani Nikula 	u32 val;
164379bc100SJani Nikula 
165379bc100SJani Nikula 	/* When using bit bashing for I2C, this bit needs to be set to 1 */
166d9053b23SJani Nikula 	val = intel_de_read(dev_priv, DSPCLK_GATE_D);
167379bc100SJani Nikula 	if (!enable)
168379bc100SJani Nikula 		val |= PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
169379bc100SJani Nikula 	else
170379bc100SJani Nikula 		val &= ~PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
171d9053b23SJani Nikula 	intel_de_write(dev_priv, DSPCLK_GATE_D, val);
172379bc100SJani Nikula }
173379bc100SJani Nikula 
174379bc100SJani Nikula static void pch_gmbus_clock_gating(struct drm_i915_private *dev_priv,
175379bc100SJani Nikula 				   bool enable)
176379bc100SJani Nikula {
177379bc100SJani Nikula 	u32 val;
178379bc100SJani Nikula 
179d9053b23SJani Nikula 	val = intel_de_read(dev_priv, SOUTH_DSPCLK_GATE_D);
180379bc100SJani Nikula 	if (!enable)
181379bc100SJani Nikula 		val |= PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
182379bc100SJani Nikula 	else
183379bc100SJani Nikula 		val &= ~PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
184d9053b23SJani Nikula 	intel_de_write(dev_priv, SOUTH_DSPCLK_GATE_D, val);
185379bc100SJani Nikula }
186379bc100SJani Nikula 
187379bc100SJani Nikula static void bxt_gmbus_clock_gating(struct drm_i915_private *dev_priv,
188379bc100SJani Nikula 				   bool enable)
189379bc100SJani Nikula {
190379bc100SJani Nikula 	u32 val;
191379bc100SJani Nikula 
192d9053b23SJani Nikula 	val = intel_de_read(dev_priv, GEN9_CLKGATE_DIS_4);
193379bc100SJani Nikula 	if (!enable)
194379bc100SJani Nikula 		val |= BXT_GMBUS_GATING_DIS;
195379bc100SJani Nikula 	else
196379bc100SJani Nikula 		val &= ~BXT_GMBUS_GATING_DIS;
197d9053b23SJani Nikula 	intel_de_write(dev_priv, GEN9_CLKGATE_DIS_4, val);
198379bc100SJani Nikula }
199379bc100SJani Nikula 
200379bc100SJani Nikula static u32 get_reserved(struct intel_gmbus *bus)
201379bc100SJani Nikula {
202379bc100SJani Nikula 	struct drm_i915_private *i915 = bus->dev_priv;
203379bc100SJani Nikula 	struct intel_uncore *uncore = &i915->uncore;
204379bc100SJani Nikula 	u32 reserved = 0;
205379bc100SJani Nikula 
206379bc100SJani Nikula 	/* On most chips, these bits must be preserved in software. */
207379bc100SJani Nikula 	if (!IS_I830(i915) && !IS_I845G(i915))
208379bc100SJani Nikula 		reserved = intel_uncore_read_notrace(uncore, bus->gpio_reg) &
209379bc100SJani Nikula 			   (GPIO_DATA_PULLUP_DISABLE |
210379bc100SJani Nikula 			    GPIO_CLOCK_PULLUP_DISABLE);
211379bc100SJani Nikula 
212379bc100SJani Nikula 	return reserved;
213379bc100SJani Nikula }
214379bc100SJani Nikula 
215379bc100SJani Nikula static int get_clock(void *data)
216379bc100SJani Nikula {
217379bc100SJani Nikula 	struct intel_gmbus *bus = data;
218379bc100SJani Nikula 	struct intel_uncore *uncore = &bus->dev_priv->uncore;
219379bc100SJani Nikula 	u32 reserved = get_reserved(bus);
220379bc100SJani Nikula 
221379bc100SJani Nikula 	intel_uncore_write_notrace(uncore,
222379bc100SJani Nikula 				   bus->gpio_reg,
223379bc100SJani Nikula 				   reserved | GPIO_CLOCK_DIR_MASK);
224379bc100SJani Nikula 	intel_uncore_write_notrace(uncore, bus->gpio_reg, reserved);
225379bc100SJani Nikula 
226379bc100SJani Nikula 	return (intel_uncore_read_notrace(uncore, bus->gpio_reg) &
227379bc100SJani Nikula 		GPIO_CLOCK_VAL_IN) != 0;
228379bc100SJani Nikula }
229379bc100SJani Nikula 
230379bc100SJani Nikula static int get_data(void *data)
231379bc100SJani Nikula {
232379bc100SJani Nikula 	struct intel_gmbus *bus = data;
233379bc100SJani Nikula 	struct intel_uncore *uncore = &bus->dev_priv->uncore;
234379bc100SJani Nikula 	u32 reserved = get_reserved(bus);
235379bc100SJani Nikula 
236379bc100SJani Nikula 	intel_uncore_write_notrace(uncore,
237379bc100SJani Nikula 				   bus->gpio_reg,
238379bc100SJani Nikula 				   reserved | GPIO_DATA_DIR_MASK);
239379bc100SJani Nikula 	intel_uncore_write_notrace(uncore, bus->gpio_reg, reserved);
240379bc100SJani Nikula 
241379bc100SJani Nikula 	return (intel_uncore_read_notrace(uncore, bus->gpio_reg) &
242379bc100SJani Nikula 		GPIO_DATA_VAL_IN) != 0;
243379bc100SJani Nikula }
244379bc100SJani Nikula 
245379bc100SJani Nikula static void set_clock(void *data, int state_high)
246379bc100SJani Nikula {
247379bc100SJani Nikula 	struct intel_gmbus *bus = data;
248379bc100SJani Nikula 	struct intel_uncore *uncore = &bus->dev_priv->uncore;
249379bc100SJani Nikula 	u32 reserved = get_reserved(bus);
250379bc100SJani Nikula 	u32 clock_bits;
251379bc100SJani Nikula 
252379bc100SJani Nikula 	if (state_high)
253379bc100SJani Nikula 		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
254379bc100SJani Nikula 	else
255379bc100SJani Nikula 		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
256379bc100SJani Nikula 			     GPIO_CLOCK_VAL_MASK;
257379bc100SJani Nikula 
258379bc100SJani Nikula 	intel_uncore_write_notrace(uncore,
259379bc100SJani Nikula 				   bus->gpio_reg,
260379bc100SJani Nikula 				   reserved | clock_bits);
261379bc100SJani Nikula 	intel_uncore_posting_read(uncore, bus->gpio_reg);
262379bc100SJani Nikula }
263379bc100SJani Nikula 
264379bc100SJani Nikula static void set_data(void *data, int state_high)
265379bc100SJani Nikula {
266379bc100SJani Nikula 	struct intel_gmbus *bus = data;
267379bc100SJani Nikula 	struct intel_uncore *uncore = &bus->dev_priv->uncore;
268379bc100SJani Nikula 	u32 reserved = get_reserved(bus);
269379bc100SJani Nikula 	u32 data_bits;
270379bc100SJani Nikula 
271379bc100SJani Nikula 	if (state_high)
272379bc100SJani Nikula 		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
273379bc100SJani Nikula 	else
274379bc100SJani Nikula 		data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
275379bc100SJani Nikula 			GPIO_DATA_VAL_MASK;
276379bc100SJani Nikula 
277379bc100SJani Nikula 	intel_uncore_write_notrace(uncore, bus->gpio_reg, reserved | data_bits);
278379bc100SJani Nikula 	intel_uncore_posting_read(uncore, bus->gpio_reg);
279379bc100SJani Nikula }
280379bc100SJani Nikula 
281379bc100SJani Nikula static int
282379bc100SJani Nikula intel_gpio_pre_xfer(struct i2c_adapter *adapter)
283379bc100SJani Nikula {
284379bc100SJani Nikula 	struct intel_gmbus *bus = container_of(adapter,
285379bc100SJani Nikula 					       struct intel_gmbus,
286379bc100SJani Nikula 					       adapter);
287379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
288379bc100SJani Nikula 
289379bc100SJani Nikula 	intel_gmbus_reset(dev_priv);
290379bc100SJani Nikula 
291379bc100SJani Nikula 	if (IS_PINEVIEW(dev_priv))
292379bc100SJani Nikula 		pnv_gmbus_clock_gating(dev_priv, false);
293379bc100SJani Nikula 
294379bc100SJani Nikula 	set_data(bus, 1);
295379bc100SJani Nikula 	set_clock(bus, 1);
296379bc100SJani Nikula 	udelay(I2C_RISEFALL_TIME);
297379bc100SJani Nikula 	return 0;
298379bc100SJani Nikula }
299379bc100SJani Nikula 
300379bc100SJani Nikula static void
301379bc100SJani Nikula intel_gpio_post_xfer(struct i2c_adapter *adapter)
302379bc100SJani Nikula {
303379bc100SJani Nikula 	struct intel_gmbus *bus = container_of(adapter,
304379bc100SJani Nikula 					       struct intel_gmbus,
305379bc100SJani Nikula 					       adapter);
306379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
307379bc100SJani Nikula 
308379bc100SJani Nikula 	set_data(bus, 1);
309379bc100SJani Nikula 	set_clock(bus, 1);
310379bc100SJani Nikula 
311379bc100SJani Nikula 	if (IS_PINEVIEW(dev_priv))
312379bc100SJani Nikula 		pnv_gmbus_clock_gating(dev_priv, true);
313379bc100SJani Nikula }
314379bc100SJani Nikula 
315379bc100SJani Nikula static void
316379bc100SJani Nikula intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
317379bc100SJani Nikula {
318379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
319379bc100SJani Nikula 	struct i2c_algo_bit_data *algo;
320379bc100SJani Nikula 
321379bc100SJani Nikula 	algo = &bus->bit_algo;
322379bc100SJani Nikula 
323379bc100SJani Nikula 	bus->gpio_reg = GPIO(get_gmbus_pin(dev_priv, pin)->gpio);
324379bc100SJani Nikula 	bus->adapter.algo_data = algo;
325379bc100SJani Nikula 	algo->setsda = set_data;
326379bc100SJani Nikula 	algo->setscl = set_clock;
327379bc100SJani Nikula 	algo->getsda = get_data;
328379bc100SJani Nikula 	algo->getscl = get_clock;
329379bc100SJani Nikula 	algo->pre_xfer = intel_gpio_pre_xfer;
330379bc100SJani Nikula 	algo->post_xfer = intel_gpio_post_xfer;
331379bc100SJani Nikula 	algo->udelay = I2C_RISEFALL_TIME;
332379bc100SJani Nikula 	algo->timeout = usecs_to_jiffies(2200);
333379bc100SJani Nikula 	algo->data = bus;
334379bc100SJani Nikula }
335379bc100SJani Nikula 
336379bc100SJani Nikula static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
337379bc100SJani Nikula {
338379bc100SJani Nikula 	DEFINE_WAIT(wait);
339379bc100SJani Nikula 	u32 gmbus2;
340379bc100SJani Nikula 	int ret;
341379bc100SJani Nikula 
342379bc100SJani Nikula 	/* Important: The hw handles only the first bit, so set only one! Since
343379bc100SJani Nikula 	 * we also need to check for NAKs besides the hw ready/idle signal, we
344379bc100SJani Nikula 	 * need to wake up periodically and check that ourselves.
345379bc100SJani Nikula 	 */
346379bc100SJani Nikula 	if (!HAS_GMBUS_IRQ(dev_priv))
347379bc100SJani Nikula 		irq_en = 0;
348379bc100SJani Nikula 
349379bc100SJani Nikula 	add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
350d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS4, irq_en);
351379bc100SJani Nikula 
352379bc100SJani Nikula 	status |= GMBUS_SATOER;
353d9053b23SJani Nikula 	ret = wait_for_us((gmbus2 = intel_de_read_fw(dev_priv, GMBUS2)) & status,
354d9053b23SJani Nikula 			  2);
355379bc100SJani Nikula 	if (ret)
356d9053b23SJani Nikula 		ret = wait_for((gmbus2 = intel_de_read_fw(dev_priv, GMBUS2)) & status,
357d9053b23SJani Nikula 			       50);
358379bc100SJani Nikula 
359d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS4, 0);
360379bc100SJani Nikula 	remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
361379bc100SJani Nikula 
362379bc100SJani Nikula 	if (gmbus2 & GMBUS_SATOER)
363379bc100SJani Nikula 		return -ENXIO;
364379bc100SJani Nikula 
365379bc100SJani Nikula 	return ret;
366379bc100SJani Nikula }
367379bc100SJani Nikula 
368379bc100SJani Nikula static int
369379bc100SJani Nikula gmbus_wait_idle(struct drm_i915_private *dev_priv)
370379bc100SJani Nikula {
371379bc100SJani Nikula 	DEFINE_WAIT(wait);
372379bc100SJani Nikula 	u32 irq_enable;
373379bc100SJani Nikula 	int ret;
374379bc100SJani Nikula 
375379bc100SJani Nikula 	/* Important: The hw handles only the first bit, so set only one! */
376379bc100SJani Nikula 	irq_enable = 0;
377379bc100SJani Nikula 	if (HAS_GMBUS_IRQ(dev_priv))
378379bc100SJani Nikula 		irq_enable = GMBUS_IDLE_EN;
379379bc100SJani Nikula 
380379bc100SJani Nikula 	add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
381d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS4, irq_enable);
382379bc100SJani Nikula 
383379bc100SJani Nikula 	ret = intel_wait_for_register_fw(&dev_priv->uncore,
384379bc100SJani Nikula 					 GMBUS2, GMBUS_ACTIVE, 0,
385379bc100SJani Nikula 					 10);
386379bc100SJani Nikula 
387d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS4, 0);
388379bc100SJani Nikula 	remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
389379bc100SJani Nikula 
390379bc100SJani Nikula 	return ret;
391379bc100SJani Nikula }
392379bc100SJani Nikula 
39381b55ef1SJani Nikula static unsigned int gmbus_max_xfer_size(struct drm_i915_private *dev_priv)
394379bc100SJani Nikula {
395379bc100SJani Nikula 	return INTEL_GEN(dev_priv) >= 9 ? GEN9_GMBUS_BYTE_COUNT_MAX :
396379bc100SJani Nikula 	       GMBUS_BYTE_COUNT_MAX;
397379bc100SJani Nikula }
398379bc100SJani Nikula 
399379bc100SJani Nikula static int
400379bc100SJani Nikula gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
401379bc100SJani Nikula 		      unsigned short addr, u8 *buf, unsigned int len,
402379bc100SJani Nikula 		      u32 gmbus0_reg, u32 gmbus1_index)
403379bc100SJani Nikula {
404379bc100SJani Nikula 	unsigned int size = len;
405379bc100SJani Nikula 	bool burst_read = len > gmbus_max_xfer_size(dev_priv);
406379bc100SJani Nikula 	bool extra_byte_added = false;
407379bc100SJani Nikula 
408379bc100SJani Nikula 	if (burst_read) {
409379bc100SJani Nikula 		/*
410379bc100SJani Nikula 		 * As per HW Spec, for 512Bytes need to read extra Byte and
411379bc100SJani Nikula 		 * Ignore the extra byte read.
412379bc100SJani Nikula 		 */
413379bc100SJani Nikula 		if (len == 512) {
414379bc100SJani Nikula 			extra_byte_added = true;
415379bc100SJani Nikula 			len++;
416379bc100SJani Nikula 		}
417379bc100SJani Nikula 		size = len % 256 + 256;
418d9053b23SJani Nikula 		intel_de_write_fw(dev_priv, GMBUS0,
419d9053b23SJani Nikula 				  gmbus0_reg | GMBUS_BYTE_CNT_OVERRIDE);
420379bc100SJani Nikula 	}
421379bc100SJani Nikula 
422d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1,
423d9053b23SJani Nikula 			  gmbus1_index | GMBUS_CYCLE_WAIT | (size << GMBUS_BYTE_COUNT_SHIFT) | (addr << GMBUS_SLAVE_ADDR_SHIFT) | GMBUS_SLAVE_READ | GMBUS_SW_RDY);
424379bc100SJani Nikula 	while (len) {
425379bc100SJani Nikula 		int ret;
426379bc100SJani Nikula 		u32 val, loop = 0;
427379bc100SJani Nikula 
428379bc100SJani Nikula 		ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
429379bc100SJani Nikula 		if (ret)
430379bc100SJani Nikula 			return ret;
431379bc100SJani Nikula 
432d9053b23SJani Nikula 		val = intel_de_read_fw(dev_priv, GMBUS3);
433379bc100SJani Nikula 		do {
434379bc100SJani Nikula 			if (extra_byte_added && len == 1)
435379bc100SJani Nikula 				break;
436379bc100SJani Nikula 
437379bc100SJani Nikula 			*buf++ = val & 0xff;
438379bc100SJani Nikula 			val >>= 8;
439379bc100SJani Nikula 		} while (--len && ++loop < 4);
440379bc100SJani Nikula 
441379bc100SJani Nikula 		if (burst_read && len == size - 4)
442379bc100SJani Nikula 			/* Reset the override bit */
443d9053b23SJani Nikula 			intel_de_write_fw(dev_priv, GMBUS0, gmbus0_reg);
444379bc100SJani Nikula 	}
445379bc100SJani Nikula 
446379bc100SJani Nikula 	return 0;
447379bc100SJani Nikula }
448379bc100SJani Nikula 
449379bc100SJani Nikula /*
450379bc100SJani Nikula  * HW spec says that 512Bytes in Burst read need special treatment.
451379bc100SJani Nikula  * But it doesn't talk about other multiple of 256Bytes. And couldn't locate
452379bc100SJani Nikula  * an I2C slave, which supports such a lengthy burst read too for experiments.
453379bc100SJani Nikula  *
454379bc100SJani Nikula  * So until things get clarified on HW support, to avoid the burst read length
455379bc100SJani Nikula  * in fold of 256Bytes except 512, max burst read length is fixed at 767Bytes.
456379bc100SJani Nikula  */
457379bc100SJani Nikula #define INTEL_GMBUS_BURST_READ_MAX_LEN		767U
458379bc100SJani Nikula 
459379bc100SJani Nikula static int
460379bc100SJani Nikula gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
461379bc100SJani Nikula 		u32 gmbus0_reg, u32 gmbus1_index)
462379bc100SJani Nikula {
463379bc100SJani Nikula 	u8 *buf = msg->buf;
464379bc100SJani Nikula 	unsigned int rx_size = msg->len;
465379bc100SJani Nikula 	unsigned int len;
466379bc100SJani Nikula 	int ret;
467379bc100SJani Nikula 
468379bc100SJani Nikula 	do {
469379bc100SJani Nikula 		if (HAS_GMBUS_BURST_READ(dev_priv))
470379bc100SJani Nikula 			len = min(rx_size, INTEL_GMBUS_BURST_READ_MAX_LEN);
471379bc100SJani Nikula 		else
472379bc100SJani Nikula 			len = min(rx_size, gmbus_max_xfer_size(dev_priv));
473379bc100SJani Nikula 
474379bc100SJani Nikula 		ret = gmbus_xfer_read_chunk(dev_priv, msg->addr, buf, len,
475379bc100SJani Nikula 					    gmbus0_reg, gmbus1_index);
476379bc100SJani Nikula 		if (ret)
477379bc100SJani Nikula 			return ret;
478379bc100SJani Nikula 
479379bc100SJani Nikula 		rx_size -= len;
480379bc100SJani Nikula 		buf += len;
481379bc100SJani Nikula 	} while (rx_size != 0);
482379bc100SJani Nikula 
483379bc100SJani Nikula 	return 0;
484379bc100SJani Nikula }
485379bc100SJani Nikula 
486379bc100SJani Nikula static int
487379bc100SJani Nikula gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
488379bc100SJani Nikula 		       unsigned short addr, u8 *buf, unsigned int len,
489379bc100SJani Nikula 		       u32 gmbus1_index)
490379bc100SJani Nikula {
491379bc100SJani Nikula 	unsigned int chunk_size = len;
492379bc100SJani Nikula 	u32 val, loop;
493379bc100SJani Nikula 
494379bc100SJani Nikula 	val = loop = 0;
495379bc100SJani Nikula 	while (len && loop < 4) {
496379bc100SJani Nikula 		val |= *buf++ << (8 * loop++);
497379bc100SJani Nikula 		len -= 1;
498379bc100SJani Nikula 	}
499379bc100SJani Nikula 
500d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS3, val);
501d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1,
502d9053b23SJani Nikula 			  gmbus1_index | GMBUS_CYCLE_WAIT | (chunk_size << GMBUS_BYTE_COUNT_SHIFT) | (addr << GMBUS_SLAVE_ADDR_SHIFT) | GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
503379bc100SJani Nikula 	while (len) {
504379bc100SJani Nikula 		int ret;
505379bc100SJani Nikula 
506379bc100SJani Nikula 		val = loop = 0;
507379bc100SJani Nikula 		do {
508379bc100SJani Nikula 			val |= *buf++ << (8 * loop);
509379bc100SJani Nikula 		} while (--len && ++loop < 4);
510379bc100SJani Nikula 
511d9053b23SJani Nikula 		intel_de_write_fw(dev_priv, GMBUS3, val);
512379bc100SJani Nikula 
513379bc100SJani Nikula 		ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
514379bc100SJani Nikula 		if (ret)
515379bc100SJani Nikula 			return ret;
516379bc100SJani Nikula 	}
517379bc100SJani Nikula 
518379bc100SJani Nikula 	return 0;
519379bc100SJani Nikula }
520379bc100SJani Nikula 
521379bc100SJani Nikula static int
522379bc100SJani Nikula gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
523379bc100SJani Nikula 		 u32 gmbus1_index)
524379bc100SJani Nikula {
525379bc100SJani Nikula 	u8 *buf = msg->buf;
526379bc100SJani Nikula 	unsigned int tx_size = msg->len;
527379bc100SJani Nikula 	unsigned int len;
528379bc100SJani Nikula 	int ret;
529379bc100SJani Nikula 
530379bc100SJani Nikula 	do {
531379bc100SJani Nikula 		len = min(tx_size, gmbus_max_xfer_size(dev_priv));
532379bc100SJani Nikula 
533379bc100SJani Nikula 		ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len,
534379bc100SJani Nikula 					     gmbus1_index);
535379bc100SJani Nikula 		if (ret)
536379bc100SJani Nikula 			return ret;
537379bc100SJani Nikula 
538379bc100SJani Nikula 		buf += len;
539379bc100SJani Nikula 		tx_size -= len;
540379bc100SJani Nikula 	} while (tx_size != 0);
541379bc100SJani Nikula 
542379bc100SJani Nikula 	return 0;
543379bc100SJani Nikula }
544379bc100SJani Nikula 
545379bc100SJani Nikula /*
546379bc100SJani Nikula  * The gmbus controller can combine a 1 or 2 byte write with another read/write
547379bc100SJani Nikula  * that immediately follows it by using an "INDEX" cycle.
548379bc100SJani Nikula  */
549379bc100SJani Nikula static bool
550379bc100SJani Nikula gmbus_is_index_xfer(struct i2c_msg *msgs, int i, int num)
551379bc100SJani Nikula {
552379bc100SJani Nikula 	return (i + 1 < num &&
553379bc100SJani Nikula 		msgs[i].addr == msgs[i + 1].addr &&
554379bc100SJani Nikula 		!(msgs[i].flags & I2C_M_RD) &&
555379bc100SJani Nikula 		(msgs[i].len == 1 || msgs[i].len == 2) &&
556379bc100SJani Nikula 		msgs[i + 1].len > 0);
557379bc100SJani Nikula }
558379bc100SJani Nikula 
559379bc100SJani Nikula static int
560379bc100SJani Nikula gmbus_index_xfer(struct drm_i915_private *dev_priv, struct i2c_msg *msgs,
561379bc100SJani Nikula 		 u32 gmbus0_reg)
562379bc100SJani Nikula {
563379bc100SJani Nikula 	u32 gmbus1_index = 0;
564379bc100SJani Nikula 	u32 gmbus5 = 0;
565379bc100SJani Nikula 	int ret;
566379bc100SJani Nikula 
567379bc100SJani Nikula 	if (msgs[0].len == 2)
568379bc100SJani Nikula 		gmbus5 = GMBUS_2BYTE_INDEX_EN |
569379bc100SJani Nikula 			 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
570379bc100SJani Nikula 	if (msgs[0].len == 1)
571379bc100SJani Nikula 		gmbus1_index = GMBUS_CYCLE_INDEX |
572379bc100SJani Nikula 			       (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
573379bc100SJani Nikula 
574379bc100SJani Nikula 	/* GMBUS5 holds 16-bit index */
575379bc100SJani Nikula 	if (gmbus5)
576d9053b23SJani Nikula 		intel_de_write_fw(dev_priv, GMBUS5, gmbus5);
577379bc100SJani Nikula 
578379bc100SJani Nikula 	if (msgs[1].flags & I2C_M_RD)
579379bc100SJani Nikula 		ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus0_reg,
580379bc100SJani Nikula 				      gmbus1_index);
581379bc100SJani Nikula 	else
582379bc100SJani Nikula 		ret = gmbus_xfer_write(dev_priv, &msgs[1], gmbus1_index);
583379bc100SJani Nikula 
584379bc100SJani Nikula 	/* Clear GMBUS5 after each index transfer */
585379bc100SJani Nikula 	if (gmbus5)
586d9053b23SJani Nikula 		intel_de_write_fw(dev_priv, GMBUS5, 0);
587379bc100SJani Nikula 
588379bc100SJani Nikula 	return ret;
589379bc100SJani Nikula }
590379bc100SJani Nikula 
591379bc100SJani Nikula static int
592379bc100SJani Nikula do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num,
593379bc100SJani Nikula 	      u32 gmbus0_source)
594379bc100SJani Nikula {
595379bc100SJani Nikula 	struct intel_gmbus *bus = container_of(adapter,
596379bc100SJani Nikula 					       struct intel_gmbus,
597379bc100SJani Nikula 					       adapter);
598379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
599379bc100SJani Nikula 	int i = 0, inc, try = 0;
600379bc100SJani Nikula 	int ret = 0;
601379bc100SJani Nikula 
602379bc100SJani Nikula 	/* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
603379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
604379bc100SJani Nikula 		bxt_gmbus_clock_gating(dev_priv, false);
605379bc100SJani Nikula 	else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_CNP(dev_priv))
606379bc100SJani Nikula 		pch_gmbus_clock_gating(dev_priv, false);
607379bc100SJani Nikula 
608379bc100SJani Nikula retry:
609d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS0, gmbus0_source | bus->reg0);
610379bc100SJani Nikula 
611379bc100SJani Nikula 	for (; i < num; i += inc) {
612379bc100SJani Nikula 		inc = 1;
613379bc100SJani Nikula 		if (gmbus_is_index_xfer(msgs, i, num)) {
614379bc100SJani Nikula 			ret = gmbus_index_xfer(dev_priv, &msgs[i],
615379bc100SJani Nikula 					       gmbus0_source | bus->reg0);
616379bc100SJani Nikula 			inc = 2; /* an index transmission is two msgs */
617379bc100SJani Nikula 		} else if (msgs[i].flags & I2C_M_RD) {
618379bc100SJani Nikula 			ret = gmbus_xfer_read(dev_priv, &msgs[i],
619379bc100SJani Nikula 					      gmbus0_source | bus->reg0, 0);
620379bc100SJani Nikula 		} else {
621379bc100SJani Nikula 			ret = gmbus_xfer_write(dev_priv, &msgs[i], 0);
622379bc100SJani Nikula 		}
623379bc100SJani Nikula 
624379bc100SJani Nikula 		if (!ret)
625379bc100SJani Nikula 			ret = gmbus_wait(dev_priv,
626379bc100SJani Nikula 					 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
627379bc100SJani Nikula 		if (ret == -ETIMEDOUT)
628379bc100SJani Nikula 			goto timeout;
629379bc100SJani Nikula 		else if (ret)
630379bc100SJani Nikula 			goto clear_err;
631379bc100SJani Nikula 	}
632379bc100SJani Nikula 
633379bc100SJani Nikula 	/* Generate a STOP condition on the bus. Note that gmbus can't generata
634379bc100SJani Nikula 	 * a STOP on the very first cycle. To simplify the code we
635379bc100SJani Nikula 	 * unconditionally generate the STOP condition with an additional gmbus
636379bc100SJani Nikula 	 * cycle. */
637d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
638379bc100SJani Nikula 
639379bc100SJani Nikula 	/* Mark the GMBUS interface as disabled after waiting for idle.
640379bc100SJani Nikula 	 * We will re-enable it at the start of the next xfer,
641379bc100SJani Nikula 	 * till then let it sleep.
642379bc100SJani Nikula 	 */
643379bc100SJani Nikula 	if (gmbus_wait_idle(dev_priv)) {
6446a9cc4bfSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
6456a9cc4bfSWambui Karuga 			    "GMBUS [%s] timed out waiting for idle\n",
646379bc100SJani Nikula 			    adapter->name);
647379bc100SJani Nikula 		ret = -ETIMEDOUT;
648379bc100SJani Nikula 	}
649d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS0, 0);
650379bc100SJani Nikula 	ret = ret ?: i;
651379bc100SJani Nikula 	goto out;
652379bc100SJani Nikula 
653379bc100SJani Nikula clear_err:
654379bc100SJani Nikula 	/*
655379bc100SJani Nikula 	 * Wait for bus to IDLE before clearing NAK.
656379bc100SJani Nikula 	 * If we clear the NAK while bus is still active, then it will stay
657379bc100SJani Nikula 	 * active and the next transaction may fail.
658379bc100SJani Nikula 	 *
659379bc100SJani Nikula 	 * If no ACK is received during the address phase of a transaction, the
660379bc100SJani Nikula 	 * adapter must report -ENXIO. It is not clear what to return if no ACK
661379bc100SJani Nikula 	 * is received at other times. But we have to be careful to not return
662379bc100SJani Nikula 	 * spurious -ENXIO because that will prevent i2c and drm edid functions
663379bc100SJani Nikula 	 * from retrying. So return -ENXIO only when gmbus properly quiescents -
664379bc100SJani Nikula 	 * timing out seems to happen when there _is_ a ddc chip present, but
665379bc100SJani Nikula 	 * it's slow responding and only answers on the 2nd retry.
666379bc100SJani Nikula 	 */
667379bc100SJani Nikula 	ret = -ENXIO;
668379bc100SJani Nikula 	if (gmbus_wait_idle(dev_priv)) {
6696a9cc4bfSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
6706a9cc4bfSWambui Karuga 			    "GMBUS [%s] timed out after NAK\n",
671379bc100SJani Nikula 			    adapter->name);
672379bc100SJani Nikula 		ret = -ETIMEDOUT;
673379bc100SJani Nikula 	}
674379bc100SJani Nikula 
675379bc100SJani Nikula 	/* Toggle the Software Clear Interrupt bit. This has the effect
676379bc100SJani Nikula 	 * of resetting the GMBUS controller and so clearing the
677379bc100SJani Nikula 	 * BUS_ERROR raised by the slave's NAK.
678379bc100SJani Nikula 	 */
679d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1, GMBUS_SW_CLR_INT);
680d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1, 0);
681d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS0, 0);
682379bc100SJani Nikula 
6836a9cc4bfSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "GMBUS [%s] NAK for addr: %04x %c(%d)\n",
684379bc100SJani Nikula 		    adapter->name, msgs[i].addr,
685379bc100SJani Nikula 		    (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
686379bc100SJani Nikula 
687379bc100SJani Nikula 	/*
688379bc100SJani Nikula 	 * Passive adapters sometimes NAK the first probe. Retry the first
689379bc100SJani Nikula 	 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
690379bc100SJani Nikula 	 * has retries internally. See also the retry loop in
691379bc100SJani Nikula 	 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
692379bc100SJani Nikula 	 */
693379bc100SJani Nikula 	if (ret == -ENXIO && i == 0 && try++ == 0) {
6946a9cc4bfSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
6956a9cc4bfSWambui Karuga 			    "GMBUS [%s] NAK on first message, retry\n",
696379bc100SJani Nikula 			    adapter->name);
697379bc100SJani Nikula 		goto retry;
698379bc100SJani Nikula 	}
699379bc100SJani Nikula 
700379bc100SJani Nikula 	goto out;
701379bc100SJani Nikula 
702379bc100SJani Nikula timeout:
7036a9cc4bfSWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
7046a9cc4bfSWambui Karuga 		    "GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
705379bc100SJani Nikula 		    bus->adapter.name, bus->reg0 & 0xff);
706d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS0, 0);
707379bc100SJani Nikula 
708379bc100SJani Nikula 	/*
709379bc100SJani Nikula 	 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
710379bc100SJani Nikula 	 * instead. Use EAGAIN to have i2c core retry.
711379bc100SJani Nikula 	 */
712379bc100SJani Nikula 	ret = -EAGAIN;
713379bc100SJani Nikula 
714379bc100SJani Nikula out:
715379bc100SJani Nikula 	/* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
716379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
717379bc100SJani Nikula 		bxt_gmbus_clock_gating(dev_priv, true);
718379bc100SJani Nikula 	else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_CNP(dev_priv))
719379bc100SJani Nikula 		pch_gmbus_clock_gating(dev_priv, true);
720379bc100SJani Nikula 
721379bc100SJani Nikula 	return ret;
722379bc100SJani Nikula }
723379bc100SJani Nikula 
724379bc100SJani Nikula static int
725379bc100SJani Nikula gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
726379bc100SJani Nikula {
727379bc100SJani Nikula 	struct intel_gmbus *bus =
728379bc100SJani Nikula 		container_of(adapter, struct intel_gmbus, adapter);
729379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
730379bc100SJani Nikula 	intel_wakeref_t wakeref;
731379bc100SJani Nikula 	int ret;
732379bc100SJani Nikula 
733379bc100SJani Nikula 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
734379bc100SJani Nikula 
735379bc100SJani Nikula 	if (bus->force_bit) {
736379bc100SJani Nikula 		ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
737379bc100SJani Nikula 		if (ret < 0)
738379bc100SJani Nikula 			bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
739379bc100SJani Nikula 	} else {
740379bc100SJani Nikula 		ret = do_gmbus_xfer(adapter, msgs, num, 0);
741379bc100SJani Nikula 		if (ret == -EAGAIN)
742379bc100SJani Nikula 			bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
743379bc100SJani Nikula 	}
744379bc100SJani Nikula 
745379bc100SJani Nikula 	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS, wakeref);
746379bc100SJani Nikula 
747379bc100SJani Nikula 	return ret;
748379bc100SJani Nikula }
749379bc100SJani Nikula 
750379bc100SJani Nikula int intel_gmbus_output_aksv(struct i2c_adapter *adapter)
751379bc100SJani Nikula {
752379bc100SJani Nikula 	struct intel_gmbus *bus =
753379bc100SJani Nikula 		container_of(adapter, struct intel_gmbus, adapter);
754379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
755379bc100SJani Nikula 	u8 cmd = DRM_HDCP_DDC_AKSV;
756379bc100SJani Nikula 	u8 buf[DRM_HDCP_KSV_LEN] = { 0 };
757379bc100SJani Nikula 	struct i2c_msg msgs[] = {
758379bc100SJani Nikula 		{
759379bc100SJani Nikula 			.addr = DRM_HDCP_DDC_ADDR,
760379bc100SJani Nikula 			.flags = 0,
761379bc100SJani Nikula 			.len = sizeof(cmd),
762379bc100SJani Nikula 			.buf = &cmd,
763379bc100SJani Nikula 		},
764379bc100SJani Nikula 		{
765379bc100SJani Nikula 			.addr = DRM_HDCP_DDC_ADDR,
766379bc100SJani Nikula 			.flags = 0,
767379bc100SJani Nikula 			.len = sizeof(buf),
768379bc100SJani Nikula 			.buf = buf,
769379bc100SJani Nikula 		}
770379bc100SJani Nikula 	};
771379bc100SJani Nikula 	intel_wakeref_t wakeref;
772379bc100SJani Nikula 	int ret;
773379bc100SJani Nikula 
774379bc100SJani Nikula 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
775379bc100SJani Nikula 	mutex_lock(&dev_priv->gmbus_mutex);
776379bc100SJani Nikula 
777379bc100SJani Nikula 	/*
778379bc100SJani Nikula 	 * In order to output Aksv to the receiver, use an indexed write to
779379bc100SJani Nikula 	 * pass the i2c command, and tell GMBUS to use the HW-provided value
780379bc100SJani Nikula 	 * instead of sourcing GMBUS3 for the data.
781379bc100SJani Nikula 	 */
782379bc100SJani Nikula 	ret = do_gmbus_xfer(adapter, msgs, ARRAY_SIZE(msgs), GMBUS_AKSV_SELECT);
783379bc100SJani Nikula 
784379bc100SJani Nikula 	mutex_unlock(&dev_priv->gmbus_mutex);
785379bc100SJani Nikula 	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS, wakeref);
786379bc100SJani Nikula 
787379bc100SJani Nikula 	return ret;
788379bc100SJani Nikula }
789379bc100SJani Nikula 
790379bc100SJani Nikula static u32 gmbus_func(struct i2c_adapter *adapter)
791379bc100SJani Nikula {
792379bc100SJani Nikula 	return i2c_bit_algo.functionality(adapter) &
793379bc100SJani Nikula 		(I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
794379bc100SJani Nikula 		/* I2C_FUNC_10BIT_ADDR | */
795379bc100SJani Nikula 		I2C_FUNC_SMBUS_READ_BLOCK_DATA |
796379bc100SJani Nikula 		I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
797379bc100SJani Nikula }
798379bc100SJani Nikula 
799379bc100SJani Nikula static const struct i2c_algorithm gmbus_algorithm = {
800379bc100SJani Nikula 	.master_xfer	= gmbus_xfer,
801379bc100SJani Nikula 	.functionality	= gmbus_func
802379bc100SJani Nikula };
803379bc100SJani Nikula 
804379bc100SJani Nikula static void gmbus_lock_bus(struct i2c_adapter *adapter,
805379bc100SJani Nikula 			   unsigned int flags)
806379bc100SJani Nikula {
807379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
808379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
809379bc100SJani Nikula 
810379bc100SJani Nikula 	mutex_lock(&dev_priv->gmbus_mutex);
811379bc100SJani Nikula }
812379bc100SJani Nikula 
813379bc100SJani Nikula static int gmbus_trylock_bus(struct i2c_adapter *adapter,
814379bc100SJani Nikula 			     unsigned int flags)
815379bc100SJani Nikula {
816379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
817379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
818379bc100SJani Nikula 
819379bc100SJani Nikula 	return mutex_trylock(&dev_priv->gmbus_mutex);
820379bc100SJani Nikula }
821379bc100SJani Nikula 
822379bc100SJani Nikula static void gmbus_unlock_bus(struct i2c_adapter *adapter,
823379bc100SJani Nikula 			     unsigned int flags)
824379bc100SJani Nikula {
825379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
826379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
827379bc100SJani Nikula 
828379bc100SJani Nikula 	mutex_unlock(&dev_priv->gmbus_mutex);
829379bc100SJani Nikula }
830379bc100SJani Nikula 
831379bc100SJani Nikula static const struct i2c_lock_operations gmbus_lock_ops = {
832379bc100SJani Nikula 	.lock_bus =    gmbus_lock_bus,
833379bc100SJani Nikula 	.trylock_bus = gmbus_trylock_bus,
834379bc100SJani Nikula 	.unlock_bus =  gmbus_unlock_bus,
835379bc100SJani Nikula };
836379bc100SJani Nikula 
837379bc100SJani Nikula /**
838379bc100SJani Nikula  * intel_gmbus_setup - instantiate all Intel i2c GMBuses
839379bc100SJani Nikula  * @dev_priv: i915 device private
840379bc100SJani Nikula  */
841379bc100SJani Nikula int intel_gmbus_setup(struct drm_i915_private *dev_priv)
842379bc100SJani Nikula {
843379bc100SJani Nikula 	struct pci_dev *pdev = dev_priv->drm.pdev;
844379bc100SJani Nikula 	struct intel_gmbus *bus;
845379bc100SJani Nikula 	unsigned int pin;
846379bc100SJani Nikula 	int ret;
847379bc100SJani Nikula 
848da27bd41SVille Syrjälä 	if (!HAS_DISPLAY(dev_priv))
849379bc100SJani Nikula 		return 0;
850379bc100SJani Nikula 
851379bc100SJani Nikula 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
852379bc100SJani Nikula 		dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
853379bc100SJani Nikula 	else if (!HAS_GMCH(dev_priv))
854379bc100SJani Nikula 		/*
855379bc100SJani Nikula 		 * Broxton uses the same PCH offsets for South Display Engine,
856379bc100SJani Nikula 		 * even though it doesn't have a PCH.
857379bc100SJani Nikula 		 */
858379bc100SJani Nikula 		dev_priv->gpio_mmio_base = PCH_DISPLAY_BASE;
859379bc100SJani Nikula 
860379bc100SJani Nikula 	mutex_init(&dev_priv->gmbus_mutex);
861379bc100SJani Nikula 	init_waitqueue_head(&dev_priv->gmbus_wait_queue);
862379bc100SJani Nikula 
863379bc100SJani Nikula 	for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
864379bc100SJani Nikula 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
865379bc100SJani Nikula 			continue;
866379bc100SJani Nikula 
867379bc100SJani Nikula 		bus = &dev_priv->gmbus[pin];
868379bc100SJani Nikula 
869379bc100SJani Nikula 		bus->adapter.owner = THIS_MODULE;
870379bc100SJani Nikula 		bus->adapter.class = I2C_CLASS_DDC;
871379bc100SJani Nikula 		snprintf(bus->adapter.name,
872379bc100SJani Nikula 			 sizeof(bus->adapter.name),
873379bc100SJani Nikula 			 "i915 gmbus %s",
874379bc100SJani Nikula 			 get_gmbus_pin(dev_priv, pin)->name);
875379bc100SJani Nikula 
876379bc100SJani Nikula 		bus->adapter.dev.parent = &pdev->dev;
877379bc100SJani Nikula 		bus->dev_priv = dev_priv;
878379bc100SJani Nikula 
879379bc100SJani Nikula 		bus->adapter.algo = &gmbus_algorithm;
880379bc100SJani Nikula 		bus->adapter.lock_ops = &gmbus_lock_ops;
881379bc100SJani Nikula 
882379bc100SJani Nikula 		/*
883379bc100SJani Nikula 		 * We wish to retry with bit banging
884379bc100SJani Nikula 		 * after a timed out GMBUS attempt.
885379bc100SJani Nikula 		 */
886379bc100SJani Nikula 		bus->adapter.retries = 1;
887379bc100SJani Nikula 
888379bc100SJani Nikula 		/* By default use a conservative clock rate */
889379bc100SJani Nikula 		bus->reg0 = pin | GMBUS_RATE_100KHZ;
890379bc100SJani Nikula 
891379bc100SJani Nikula 		/* gmbus seems to be broken on i830 */
892379bc100SJani Nikula 		if (IS_I830(dev_priv))
893379bc100SJani Nikula 			bus->force_bit = 1;
894379bc100SJani Nikula 
895379bc100SJani Nikula 		intel_gpio_setup(bus, pin);
896379bc100SJani Nikula 
897379bc100SJani Nikula 		ret = i2c_add_adapter(&bus->adapter);
898379bc100SJani Nikula 		if (ret)
899379bc100SJani Nikula 			goto err;
900379bc100SJani Nikula 	}
901379bc100SJani Nikula 
902379bc100SJani Nikula 	intel_gmbus_reset(dev_priv);
903379bc100SJani Nikula 
904379bc100SJani Nikula 	return 0;
905379bc100SJani Nikula 
906379bc100SJani Nikula err:
907379bc100SJani Nikula 	while (pin--) {
908379bc100SJani Nikula 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
909379bc100SJani Nikula 			continue;
910379bc100SJani Nikula 
911379bc100SJani Nikula 		bus = &dev_priv->gmbus[pin];
912379bc100SJani Nikula 		i2c_del_adapter(&bus->adapter);
913379bc100SJani Nikula 	}
914379bc100SJani Nikula 	return ret;
915379bc100SJani Nikula }
916379bc100SJani Nikula 
917379bc100SJani Nikula struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
918379bc100SJani Nikula 					    unsigned int pin)
919379bc100SJani Nikula {
920f4224a4cSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
921f4224a4cSPankaj Bharadiya 			!intel_gmbus_is_valid_pin(dev_priv, pin)))
922379bc100SJani Nikula 		return NULL;
923379bc100SJani Nikula 
924379bc100SJani Nikula 	return &dev_priv->gmbus[pin].adapter;
925379bc100SJani Nikula }
926379bc100SJani Nikula 
927379bc100SJani Nikula void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
928379bc100SJani Nikula {
929379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
930379bc100SJani Nikula 
931379bc100SJani Nikula 	bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
932379bc100SJani Nikula }
933379bc100SJani Nikula 
934379bc100SJani Nikula void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
935379bc100SJani Nikula {
936379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
937379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
938379bc100SJani Nikula 
939379bc100SJani Nikula 	mutex_lock(&dev_priv->gmbus_mutex);
940379bc100SJani Nikula 
941379bc100SJani Nikula 	bus->force_bit += force_bit ? 1 : -1;
9426a9cc4bfSWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
9436a9cc4bfSWambui Karuga 		    "%sabling bit-banging on %s. force bit now %d\n",
944379bc100SJani Nikula 		    force_bit ? "en" : "dis", adapter->name,
945379bc100SJani Nikula 		    bus->force_bit);
946379bc100SJani Nikula 
947379bc100SJani Nikula 	mutex_unlock(&dev_priv->gmbus_mutex);
948379bc100SJani Nikula }
949379bc100SJani Nikula 
950379bc100SJani Nikula bool intel_gmbus_is_forced_bit(struct i2c_adapter *adapter)
951379bc100SJani Nikula {
952379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
953379bc100SJani Nikula 
954379bc100SJani Nikula 	return bus->force_bit;
955379bc100SJani Nikula }
956379bc100SJani Nikula 
957379bc100SJani Nikula void intel_gmbus_teardown(struct drm_i915_private *dev_priv)
958379bc100SJani Nikula {
959379bc100SJani Nikula 	struct intel_gmbus *bus;
960379bc100SJani Nikula 	unsigned int pin;
961379bc100SJani Nikula 
962379bc100SJani Nikula 	for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
963379bc100SJani Nikula 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
964379bc100SJani Nikula 			continue;
965379bc100SJani Nikula 
966379bc100SJani Nikula 		bus = &dev_priv->gmbus[pin];
967379bc100SJani Nikula 		i2c_del_adapter(&bus->adapter);
968379bc100SJani Nikula 	}
969379bc100SJani Nikula }
970