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 
93379bc100SJani Nikula /* pin is expected to be valid */
94379bc100SJani Nikula static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
95379bc100SJani Nikula 					     unsigned int pin)
96379bc100SJani Nikula {
975a6b7ef6SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
98379bc100SJani Nikula 		return &gmbus_pins_icp[pin];
99379bc100SJani Nikula 	else if (HAS_PCH_CNP(dev_priv))
100379bc100SJani Nikula 		return &gmbus_pins_cnp[pin];
101379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
102379bc100SJani Nikula 		return &gmbus_pins_bxt[pin];
103379bc100SJani Nikula 	else if (IS_GEN9_BC(dev_priv))
104379bc100SJani Nikula 		return &gmbus_pins_skl[pin];
105379bc100SJani Nikula 	else if (IS_BROADWELL(dev_priv))
106379bc100SJani Nikula 		return &gmbus_pins_bdw[pin];
107379bc100SJani Nikula 	else
108379bc100SJani Nikula 		return &gmbus_pins[pin];
109379bc100SJani Nikula }
110379bc100SJani Nikula 
111379bc100SJani Nikula bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
112379bc100SJani Nikula 			      unsigned int pin)
113379bc100SJani Nikula {
114379bc100SJani Nikula 	unsigned int size;
115379bc100SJani Nikula 
1165a6b7ef6SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
117379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_icp);
118379bc100SJani Nikula 	else if (HAS_PCH_CNP(dev_priv))
119379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_cnp);
120379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
121379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_bxt);
122379bc100SJani Nikula 	else if (IS_GEN9_BC(dev_priv))
123379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_skl);
124379bc100SJani Nikula 	else if (IS_BROADWELL(dev_priv))
125379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins_bdw);
126379bc100SJani Nikula 	else
127379bc100SJani Nikula 		size = ARRAY_SIZE(gmbus_pins);
128379bc100SJani Nikula 
129379bc100SJani Nikula 	return pin < size && get_gmbus_pin(dev_priv, pin)->name;
130379bc100SJani Nikula }
131379bc100SJani Nikula 
132379bc100SJani Nikula /* Intel GPIO access functions */
133379bc100SJani Nikula 
134379bc100SJani Nikula #define I2C_RISEFALL_TIME 10
135379bc100SJani Nikula 
136379bc100SJani Nikula static inline struct intel_gmbus *
137379bc100SJani Nikula to_intel_gmbus(struct i2c_adapter *i2c)
138379bc100SJani Nikula {
139379bc100SJani Nikula 	return container_of(i2c, struct intel_gmbus, adapter);
140379bc100SJani Nikula }
141379bc100SJani Nikula 
142379bc100SJani Nikula void
143379bc100SJani Nikula intel_gmbus_reset(struct drm_i915_private *dev_priv)
144379bc100SJani Nikula {
145d9053b23SJani Nikula 	intel_de_write(dev_priv, GMBUS0, 0);
146d9053b23SJani Nikula 	intel_de_write(dev_priv, GMBUS4, 0);
147379bc100SJani Nikula }
148379bc100SJani Nikula 
149379bc100SJani Nikula static void pnv_gmbus_clock_gating(struct drm_i915_private *dev_priv,
150379bc100SJani Nikula 				   bool enable)
151379bc100SJani Nikula {
152379bc100SJani Nikula 	u32 val;
153379bc100SJani Nikula 
154379bc100SJani Nikula 	/* When using bit bashing for I2C, this bit needs to be set to 1 */
155d9053b23SJani Nikula 	val = intel_de_read(dev_priv, DSPCLK_GATE_D);
156379bc100SJani Nikula 	if (!enable)
157379bc100SJani Nikula 		val |= PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
158379bc100SJani Nikula 	else
159379bc100SJani Nikula 		val &= ~PNV_GMBUSUNIT_CLOCK_GATE_DISABLE;
160d9053b23SJani Nikula 	intel_de_write(dev_priv, DSPCLK_GATE_D, val);
161379bc100SJani Nikula }
162379bc100SJani Nikula 
163379bc100SJani Nikula static void pch_gmbus_clock_gating(struct drm_i915_private *dev_priv,
164379bc100SJani Nikula 				   bool enable)
165379bc100SJani Nikula {
166379bc100SJani Nikula 	u32 val;
167379bc100SJani Nikula 
168d9053b23SJani Nikula 	val = intel_de_read(dev_priv, SOUTH_DSPCLK_GATE_D);
169379bc100SJani Nikula 	if (!enable)
170379bc100SJani Nikula 		val |= PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
171379bc100SJani Nikula 	else
172379bc100SJani Nikula 		val &= ~PCH_GMBUSUNIT_CLOCK_GATE_DISABLE;
173d9053b23SJani Nikula 	intel_de_write(dev_priv, SOUTH_DSPCLK_GATE_D, val);
174379bc100SJani Nikula }
175379bc100SJani Nikula 
176379bc100SJani Nikula static void bxt_gmbus_clock_gating(struct drm_i915_private *dev_priv,
177379bc100SJani Nikula 				   bool enable)
178379bc100SJani Nikula {
179379bc100SJani Nikula 	u32 val;
180379bc100SJani Nikula 
181d9053b23SJani Nikula 	val = intel_de_read(dev_priv, GEN9_CLKGATE_DIS_4);
182379bc100SJani Nikula 	if (!enable)
183379bc100SJani Nikula 		val |= BXT_GMBUS_GATING_DIS;
184379bc100SJani Nikula 	else
185379bc100SJani Nikula 		val &= ~BXT_GMBUS_GATING_DIS;
186d9053b23SJani Nikula 	intel_de_write(dev_priv, GEN9_CLKGATE_DIS_4, val);
187379bc100SJani Nikula }
188379bc100SJani Nikula 
189379bc100SJani Nikula static u32 get_reserved(struct intel_gmbus *bus)
190379bc100SJani Nikula {
191379bc100SJani Nikula 	struct drm_i915_private *i915 = bus->dev_priv;
192379bc100SJani Nikula 	struct intel_uncore *uncore = &i915->uncore;
193379bc100SJani Nikula 	u32 reserved = 0;
194379bc100SJani Nikula 
195379bc100SJani Nikula 	/* On most chips, these bits must be preserved in software. */
196379bc100SJani Nikula 	if (!IS_I830(i915) && !IS_I845G(i915))
197379bc100SJani Nikula 		reserved = intel_uncore_read_notrace(uncore, bus->gpio_reg) &
198379bc100SJani Nikula 			   (GPIO_DATA_PULLUP_DISABLE |
199379bc100SJani Nikula 			    GPIO_CLOCK_PULLUP_DISABLE);
200379bc100SJani Nikula 
201379bc100SJani Nikula 	return reserved;
202379bc100SJani Nikula }
203379bc100SJani Nikula 
204379bc100SJani Nikula static int get_clock(void *data)
205379bc100SJani Nikula {
206379bc100SJani Nikula 	struct intel_gmbus *bus = data;
207379bc100SJani Nikula 	struct intel_uncore *uncore = &bus->dev_priv->uncore;
208379bc100SJani Nikula 	u32 reserved = get_reserved(bus);
209379bc100SJani Nikula 
210379bc100SJani Nikula 	intel_uncore_write_notrace(uncore,
211379bc100SJani Nikula 				   bus->gpio_reg,
212379bc100SJani Nikula 				   reserved | GPIO_CLOCK_DIR_MASK);
213379bc100SJani Nikula 	intel_uncore_write_notrace(uncore, bus->gpio_reg, reserved);
214379bc100SJani Nikula 
215379bc100SJani Nikula 	return (intel_uncore_read_notrace(uncore, bus->gpio_reg) &
216379bc100SJani Nikula 		GPIO_CLOCK_VAL_IN) != 0;
217379bc100SJani Nikula }
218379bc100SJani Nikula 
219379bc100SJani Nikula static int get_data(void *data)
220379bc100SJani Nikula {
221379bc100SJani Nikula 	struct intel_gmbus *bus = data;
222379bc100SJani Nikula 	struct intel_uncore *uncore = &bus->dev_priv->uncore;
223379bc100SJani Nikula 	u32 reserved = get_reserved(bus);
224379bc100SJani Nikula 
225379bc100SJani Nikula 	intel_uncore_write_notrace(uncore,
226379bc100SJani Nikula 				   bus->gpio_reg,
227379bc100SJani Nikula 				   reserved | GPIO_DATA_DIR_MASK);
228379bc100SJani Nikula 	intel_uncore_write_notrace(uncore, bus->gpio_reg, reserved);
229379bc100SJani Nikula 
230379bc100SJani Nikula 	return (intel_uncore_read_notrace(uncore, bus->gpio_reg) &
231379bc100SJani Nikula 		GPIO_DATA_VAL_IN) != 0;
232379bc100SJani Nikula }
233379bc100SJani Nikula 
234379bc100SJani Nikula static void set_clock(void *data, int state_high)
235379bc100SJani Nikula {
236379bc100SJani Nikula 	struct intel_gmbus *bus = data;
237379bc100SJani Nikula 	struct intel_uncore *uncore = &bus->dev_priv->uncore;
238379bc100SJani Nikula 	u32 reserved = get_reserved(bus);
239379bc100SJani Nikula 	u32 clock_bits;
240379bc100SJani Nikula 
241379bc100SJani Nikula 	if (state_high)
242379bc100SJani Nikula 		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
243379bc100SJani Nikula 	else
244379bc100SJani Nikula 		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
245379bc100SJani Nikula 			     GPIO_CLOCK_VAL_MASK;
246379bc100SJani Nikula 
247379bc100SJani Nikula 	intel_uncore_write_notrace(uncore,
248379bc100SJani Nikula 				   bus->gpio_reg,
249379bc100SJani Nikula 				   reserved | clock_bits);
250379bc100SJani Nikula 	intel_uncore_posting_read(uncore, bus->gpio_reg);
251379bc100SJani Nikula }
252379bc100SJani Nikula 
253379bc100SJani Nikula static void set_data(void *data, int state_high)
254379bc100SJani Nikula {
255379bc100SJani Nikula 	struct intel_gmbus *bus = data;
256379bc100SJani Nikula 	struct intel_uncore *uncore = &bus->dev_priv->uncore;
257379bc100SJani Nikula 	u32 reserved = get_reserved(bus);
258379bc100SJani Nikula 	u32 data_bits;
259379bc100SJani Nikula 
260379bc100SJani Nikula 	if (state_high)
261379bc100SJani Nikula 		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
262379bc100SJani Nikula 	else
263379bc100SJani Nikula 		data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
264379bc100SJani Nikula 			GPIO_DATA_VAL_MASK;
265379bc100SJani Nikula 
266379bc100SJani Nikula 	intel_uncore_write_notrace(uncore, bus->gpio_reg, reserved | data_bits);
267379bc100SJani Nikula 	intel_uncore_posting_read(uncore, bus->gpio_reg);
268379bc100SJani Nikula }
269379bc100SJani Nikula 
270379bc100SJani Nikula static int
271379bc100SJani Nikula intel_gpio_pre_xfer(struct i2c_adapter *adapter)
272379bc100SJani Nikula {
273379bc100SJani Nikula 	struct intel_gmbus *bus = container_of(adapter,
274379bc100SJani Nikula 					       struct intel_gmbus,
275379bc100SJani Nikula 					       adapter);
276379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
277379bc100SJani Nikula 
278379bc100SJani Nikula 	intel_gmbus_reset(dev_priv);
279379bc100SJani Nikula 
280379bc100SJani Nikula 	if (IS_PINEVIEW(dev_priv))
281379bc100SJani Nikula 		pnv_gmbus_clock_gating(dev_priv, false);
282379bc100SJani Nikula 
283379bc100SJani Nikula 	set_data(bus, 1);
284379bc100SJani Nikula 	set_clock(bus, 1);
285379bc100SJani Nikula 	udelay(I2C_RISEFALL_TIME);
286379bc100SJani Nikula 	return 0;
287379bc100SJani Nikula }
288379bc100SJani Nikula 
289379bc100SJani Nikula static void
290379bc100SJani Nikula intel_gpio_post_xfer(struct i2c_adapter *adapter)
291379bc100SJani Nikula {
292379bc100SJani Nikula 	struct intel_gmbus *bus = container_of(adapter,
293379bc100SJani Nikula 					       struct intel_gmbus,
294379bc100SJani Nikula 					       adapter);
295379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
296379bc100SJani Nikula 
297379bc100SJani Nikula 	set_data(bus, 1);
298379bc100SJani Nikula 	set_clock(bus, 1);
299379bc100SJani Nikula 
300379bc100SJani Nikula 	if (IS_PINEVIEW(dev_priv))
301379bc100SJani Nikula 		pnv_gmbus_clock_gating(dev_priv, true);
302379bc100SJani Nikula }
303379bc100SJani Nikula 
304379bc100SJani Nikula static void
305379bc100SJani Nikula intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
306379bc100SJani Nikula {
307379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
308379bc100SJani Nikula 	struct i2c_algo_bit_data *algo;
309379bc100SJani Nikula 
310379bc100SJani Nikula 	algo = &bus->bit_algo;
311379bc100SJani Nikula 
312379bc100SJani Nikula 	bus->gpio_reg = GPIO(get_gmbus_pin(dev_priv, pin)->gpio);
313379bc100SJani Nikula 	bus->adapter.algo_data = algo;
314379bc100SJani Nikula 	algo->setsda = set_data;
315379bc100SJani Nikula 	algo->setscl = set_clock;
316379bc100SJani Nikula 	algo->getsda = get_data;
317379bc100SJani Nikula 	algo->getscl = get_clock;
318379bc100SJani Nikula 	algo->pre_xfer = intel_gpio_pre_xfer;
319379bc100SJani Nikula 	algo->post_xfer = intel_gpio_post_xfer;
320379bc100SJani Nikula 	algo->udelay = I2C_RISEFALL_TIME;
321379bc100SJani Nikula 	algo->timeout = usecs_to_jiffies(2200);
322379bc100SJani Nikula 	algo->data = bus;
323379bc100SJani Nikula }
324379bc100SJani Nikula 
325379bc100SJani Nikula static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
326379bc100SJani Nikula {
327379bc100SJani Nikula 	DEFINE_WAIT(wait);
328379bc100SJani Nikula 	u32 gmbus2;
329379bc100SJani Nikula 	int ret;
330379bc100SJani Nikula 
331379bc100SJani Nikula 	/* Important: The hw handles only the first bit, so set only one! Since
332379bc100SJani Nikula 	 * we also need to check for NAKs besides the hw ready/idle signal, we
333379bc100SJani Nikula 	 * need to wake up periodically and check that ourselves.
334379bc100SJani Nikula 	 */
335379bc100SJani Nikula 	if (!HAS_GMBUS_IRQ(dev_priv))
336379bc100SJani Nikula 		irq_en = 0;
337379bc100SJani Nikula 
338379bc100SJani Nikula 	add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
339d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS4, irq_en);
340379bc100SJani Nikula 
341379bc100SJani Nikula 	status |= GMBUS_SATOER;
342d9053b23SJani Nikula 	ret = wait_for_us((gmbus2 = intel_de_read_fw(dev_priv, GMBUS2)) & status,
343d9053b23SJani Nikula 			  2);
344379bc100SJani Nikula 	if (ret)
345d9053b23SJani Nikula 		ret = wait_for((gmbus2 = intel_de_read_fw(dev_priv, GMBUS2)) & status,
346d9053b23SJani Nikula 			       50);
347379bc100SJani Nikula 
348d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS4, 0);
349379bc100SJani Nikula 	remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
350379bc100SJani Nikula 
351379bc100SJani Nikula 	if (gmbus2 & GMBUS_SATOER)
352379bc100SJani Nikula 		return -ENXIO;
353379bc100SJani Nikula 
354379bc100SJani Nikula 	return ret;
355379bc100SJani Nikula }
356379bc100SJani Nikula 
357379bc100SJani Nikula static int
358379bc100SJani Nikula gmbus_wait_idle(struct drm_i915_private *dev_priv)
359379bc100SJani Nikula {
360379bc100SJani Nikula 	DEFINE_WAIT(wait);
361379bc100SJani Nikula 	u32 irq_enable;
362379bc100SJani Nikula 	int ret;
363379bc100SJani Nikula 
364379bc100SJani Nikula 	/* Important: The hw handles only the first bit, so set only one! */
365379bc100SJani Nikula 	irq_enable = 0;
366379bc100SJani Nikula 	if (HAS_GMBUS_IRQ(dev_priv))
367379bc100SJani Nikula 		irq_enable = GMBUS_IDLE_EN;
368379bc100SJani Nikula 
369379bc100SJani Nikula 	add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
370d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS4, irq_enable);
371379bc100SJani Nikula 
372379bc100SJani Nikula 	ret = intel_wait_for_register_fw(&dev_priv->uncore,
373379bc100SJani Nikula 					 GMBUS2, GMBUS_ACTIVE, 0,
374379bc100SJani Nikula 					 10);
375379bc100SJani Nikula 
376d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS4, 0);
377379bc100SJani Nikula 	remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
378379bc100SJani Nikula 
379379bc100SJani Nikula 	return ret;
380379bc100SJani Nikula }
381379bc100SJani Nikula 
38281b55ef1SJani Nikula static unsigned int gmbus_max_xfer_size(struct drm_i915_private *dev_priv)
383379bc100SJani Nikula {
384379bc100SJani Nikula 	return INTEL_GEN(dev_priv) >= 9 ? GEN9_GMBUS_BYTE_COUNT_MAX :
385379bc100SJani Nikula 	       GMBUS_BYTE_COUNT_MAX;
386379bc100SJani Nikula }
387379bc100SJani Nikula 
388379bc100SJani Nikula static int
389379bc100SJani Nikula gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
390379bc100SJani Nikula 		      unsigned short addr, u8 *buf, unsigned int len,
391379bc100SJani Nikula 		      u32 gmbus0_reg, u32 gmbus1_index)
392379bc100SJani Nikula {
393379bc100SJani Nikula 	unsigned int size = len;
394379bc100SJani Nikula 	bool burst_read = len > gmbus_max_xfer_size(dev_priv);
395379bc100SJani Nikula 	bool extra_byte_added = false;
396379bc100SJani Nikula 
397379bc100SJani Nikula 	if (burst_read) {
398379bc100SJani Nikula 		/*
399379bc100SJani Nikula 		 * As per HW Spec, for 512Bytes need to read extra Byte and
400379bc100SJani Nikula 		 * Ignore the extra byte read.
401379bc100SJani Nikula 		 */
402379bc100SJani Nikula 		if (len == 512) {
403379bc100SJani Nikula 			extra_byte_added = true;
404379bc100SJani Nikula 			len++;
405379bc100SJani Nikula 		}
406379bc100SJani Nikula 		size = len % 256 + 256;
407d9053b23SJani Nikula 		intel_de_write_fw(dev_priv, GMBUS0,
408d9053b23SJani Nikula 				  gmbus0_reg | GMBUS_BYTE_CNT_OVERRIDE);
409379bc100SJani Nikula 	}
410379bc100SJani Nikula 
411d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1,
412d9053b23SJani Nikula 			  gmbus1_index | GMBUS_CYCLE_WAIT | (size << GMBUS_BYTE_COUNT_SHIFT) | (addr << GMBUS_SLAVE_ADDR_SHIFT) | GMBUS_SLAVE_READ | GMBUS_SW_RDY);
413379bc100SJani Nikula 	while (len) {
414379bc100SJani Nikula 		int ret;
415379bc100SJani Nikula 		u32 val, loop = 0;
416379bc100SJani Nikula 
417379bc100SJani Nikula 		ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
418379bc100SJani Nikula 		if (ret)
419379bc100SJani Nikula 			return ret;
420379bc100SJani Nikula 
421d9053b23SJani Nikula 		val = intel_de_read_fw(dev_priv, GMBUS3);
422379bc100SJani Nikula 		do {
423379bc100SJani Nikula 			if (extra_byte_added && len == 1)
424379bc100SJani Nikula 				break;
425379bc100SJani Nikula 
426379bc100SJani Nikula 			*buf++ = val & 0xff;
427379bc100SJani Nikula 			val >>= 8;
428379bc100SJani Nikula 		} while (--len && ++loop < 4);
429379bc100SJani Nikula 
430379bc100SJani Nikula 		if (burst_read && len == size - 4)
431379bc100SJani Nikula 			/* Reset the override bit */
432d9053b23SJani Nikula 			intel_de_write_fw(dev_priv, GMBUS0, gmbus0_reg);
433379bc100SJani Nikula 	}
434379bc100SJani Nikula 
435379bc100SJani Nikula 	return 0;
436379bc100SJani Nikula }
437379bc100SJani Nikula 
438379bc100SJani Nikula /*
439379bc100SJani Nikula  * HW spec says that 512Bytes in Burst read need special treatment.
440379bc100SJani Nikula  * But it doesn't talk about other multiple of 256Bytes. And couldn't locate
441379bc100SJani Nikula  * an I2C slave, which supports such a lengthy burst read too for experiments.
442379bc100SJani Nikula  *
443379bc100SJani Nikula  * So until things get clarified on HW support, to avoid the burst read length
444379bc100SJani Nikula  * in fold of 256Bytes except 512, max burst read length is fixed at 767Bytes.
445379bc100SJani Nikula  */
446379bc100SJani Nikula #define INTEL_GMBUS_BURST_READ_MAX_LEN		767U
447379bc100SJani Nikula 
448379bc100SJani Nikula static int
449379bc100SJani Nikula gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
450379bc100SJani Nikula 		u32 gmbus0_reg, u32 gmbus1_index)
451379bc100SJani Nikula {
452379bc100SJani Nikula 	u8 *buf = msg->buf;
453379bc100SJani Nikula 	unsigned int rx_size = msg->len;
454379bc100SJani Nikula 	unsigned int len;
455379bc100SJani Nikula 	int ret;
456379bc100SJani Nikula 
457379bc100SJani Nikula 	do {
458379bc100SJani Nikula 		if (HAS_GMBUS_BURST_READ(dev_priv))
459379bc100SJani Nikula 			len = min(rx_size, INTEL_GMBUS_BURST_READ_MAX_LEN);
460379bc100SJani Nikula 		else
461379bc100SJani Nikula 			len = min(rx_size, gmbus_max_xfer_size(dev_priv));
462379bc100SJani Nikula 
463379bc100SJani Nikula 		ret = gmbus_xfer_read_chunk(dev_priv, msg->addr, buf, len,
464379bc100SJani Nikula 					    gmbus0_reg, gmbus1_index);
465379bc100SJani Nikula 		if (ret)
466379bc100SJani Nikula 			return ret;
467379bc100SJani Nikula 
468379bc100SJani Nikula 		rx_size -= len;
469379bc100SJani Nikula 		buf += len;
470379bc100SJani Nikula 	} while (rx_size != 0);
471379bc100SJani Nikula 
472379bc100SJani Nikula 	return 0;
473379bc100SJani Nikula }
474379bc100SJani Nikula 
475379bc100SJani Nikula static int
476379bc100SJani Nikula gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
477379bc100SJani Nikula 		       unsigned short addr, u8 *buf, unsigned int len,
478379bc100SJani Nikula 		       u32 gmbus1_index)
479379bc100SJani Nikula {
480379bc100SJani Nikula 	unsigned int chunk_size = len;
481379bc100SJani Nikula 	u32 val, loop;
482379bc100SJani Nikula 
483379bc100SJani Nikula 	val = loop = 0;
484379bc100SJani Nikula 	while (len && loop < 4) {
485379bc100SJani Nikula 		val |= *buf++ << (8 * loop++);
486379bc100SJani Nikula 		len -= 1;
487379bc100SJani Nikula 	}
488379bc100SJani Nikula 
489d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS3, val);
490d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1,
491d9053b23SJani Nikula 			  gmbus1_index | GMBUS_CYCLE_WAIT | (chunk_size << GMBUS_BYTE_COUNT_SHIFT) | (addr << GMBUS_SLAVE_ADDR_SHIFT) | GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
492379bc100SJani Nikula 	while (len) {
493379bc100SJani Nikula 		int ret;
494379bc100SJani Nikula 
495379bc100SJani Nikula 		val = loop = 0;
496379bc100SJani Nikula 		do {
497379bc100SJani Nikula 			val |= *buf++ << (8 * loop);
498379bc100SJani Nikula 		} while (--len && ++loop < 4);
499379bc100SJani Nikula 
500d9053b23SJani Nikula 		intel_de_write_fw(dev_priv, GMBUS3, val);
501379bc100SJani Nikula 
502379bc100SJani Nikula 		ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
503379bc100SJani Nikula 		if (ret)
504379bc100SJani Nikula 			return ret;
505379bc100SJani Nikula 	}
506379bc100SJani Nikula 
507379bc100SJani Nikula 	return 0;
508379bc100SJani Nikula }
509379bc100SJani Nikula 
510379bc100SJani Nikula static int
511379bc100SJani Nikula gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
512379bc100SJani Nikula 		 u32 gmbus1_index)
513379bc100SJani Nikula {
514379bc100SJani Nikula 	u8 *buf = msg->buf;
515379bc100SJani Nikula 	unsigned int tx_size = msg->len;
516379bc100SJani Nikula 	unsigned int len;
517379bc100SJani Nikula 	int ret;
518379bc100SJani Nikula 
519379bc100SJani Nikula 	do {
520379bc100SJani Nikula 		len = min(tx_size, gmbus_max_xfer_size(dev_priv));
521379bc100SJani Nikula 
522379bc100SJani Nikula 		ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len,
523379bc100SJani Nikula 					     gmbus1_index);
524379bc100SJani Nikula 		if (ret)
525379bc100SJani Nikula 			return ret;
526379bc100SJani Nikula 
527379bc100SJani Nikula 		buf += len;
528379bc100SJani Nikula 		tx_size -= len;
529379bc100SJani Nikula 	} while (tx_size != 0);
530379bc100SJani Nikula 
531379bc100SJani Nikula 	return 0;
532379bc100SJani Nikula }
533379bc100SJani Nikula 
534379bc100SJani Nikula /*
535379bc100SJani Nikula  * The gmbus controller can combine a 1 or 2 byte write with another read/write
536379bc100SJani Nikula  * that immediately follows it by using an "INDEX" cycle.
537379bc100SJani Nikula  */
538379bc100SJani Nikula static bool
539379bc100SJani Nikula gmbus_is_index_xfer(struct i2c_msg *msgs, int i, int num)
540379bc100SJani Nikula {
541379bc100SJani Nikula 	return (i + 1 < num &&
542379bc100SJani Nikula 		msgs[i].addr == msgs[i + 1].addr &&
543379bc100SJani Nikula 		!(msgs[i].flags & I2C_M_RD) &&
544379bc100SJani Nikula 		(msgs[i].len == 1 || msgs[i].len == 2) &&
545379bc100SJani Nikula 		msgs[i + 1].len > 0);
546379bc100SJani Nikula }
547379bc100SJani Nikula 
548379bc100SJani Nikula static int
549379bc100SJani Nikula gmbus_index_xfer(struct drm_i915_private *dev_priv, struct i2c_msg *msgs,
550379bc100SJani Nikula 		 u32 gmbus0_reg)
551379bc100SJani Nikula {
552379bc100SJani Nikula 	u32 gmbus1_index = 0;
553379bc100SJani Nikula 	u32 gmbus5 = 0;
554379bc100SJani Nikula 	int ret;
555379bc100SJani Nikula 
556379bc100SJani Nikula 	if (msgs[0].len == 2)
557379bc100SJani Nikula 		gmbus5 = GMBUS_2BYTE_INDEX_EN |
558379bc100SJani Nikula 			 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
559379bc100SJani Nikula 	if (msgs[0].len == 1)
560379bc100SJani Nikula 		gmbus1_index = GMBUS_CYCLE_INDEX |
561379bc100SJani Nikula 			       (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
562379bc100SJani Nikula 
563379bc100SJani Nikula 	/* GMBUS5 holds 16-bit index */
564379bc100SJani Nikula 	if (gmbus5)
565d9053b23SJani Nikula 		intel_de_write_fw(dev_priv, GMBUS5, gmbus5);
566379bc100SJani Nikula 
567379bc100SJani Nikula 	if (msgs[1].flags & I2C_M_RD)
568379bc100SJani Nikula 		ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus0_reg,
569379bc100SJani Nikula 				      gmbus1_index);
570379bc100SJani Nikula 	else
571379bc100SJani Nikula 		ret = gmbus_xfer_write(dev_priv, &msgs[1], gmbus1_index);
572379bc100SJani Nikula 
573379bc100SJani Nikula 	/* Clear GMBUS5 after each index transfer */
574379bc100SJani Nikula 	if (gmbus5)
575d9053b23SJani Nikula 		intel_de_write_fw(dev_priv, GMBUS5, 0);
576379bc100SJani Nikula 
577379bc100SJani Nikula 	return ret;
578379bc100SJani Nikula }
579379bc100SJani Nikula 
580379bc100SJani Nikula static int
581379bc100SJani Nikula do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num,
582379bc100SJani Nikula 	      u32 gmbus0_source)
583379bc100SJani Nikula {
584379bc100SJani Nikula 	struct intel_gmbus *bus = container_of(adapter,
585379bc100SJani Nikula 					       struct intel_gmbus,
586379bc100SJani Nikula 					       adapter);
587379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
588379bc100SJani Nikula 	int i = 0, inc, try = 0;
589379bc100SJani Nikula 	int ret = 0;
590379bc100SJani Nikula 
591379bc100SJani Nikula 	/* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
592379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
593379bc100SJani Nikula 		bxt_gmbus_clock_gating(dev_priv, false);
594379bc100SJani Nikula 	else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_CNP(dev_priv))
595379bc100SJani Nikula 		pch_gmbus_clock_gating(dev_priv, false);
596379bc100SJani Nikula 
597379bc100SJani Nikula retry:
598d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS0, gmbus0_source | bus->reg0);
599379bc100SJani Nikula 
600379bc100SJani Nikula 	for (; i < num; i += inc) {
601379bc100SJani Nikula 		inc = 1;
602379bc100SJani Nikula 		if (gmbus_is_index_xfer(msgs, i, num)) {
603379bc100SJani Nikula 			ret = gmbus_index_xfer(dev_priv, &msgs[i],
604379bc100SJani Nikula 					       gmbus0_source | bus->reg0);
605379bc100SJani Nikula 			inc = 2; /* an index transmission is two msgs */
606379bc100SJani Nikula 		} else if (msgs[i].flags & I2C_M_RD) {
607379bc100SJani Nikula 			ret = gmbus_xfer_read(dev_priv, &msgs[i],
608379bc100SJani Nikula 					      gmbus0_source | bus->reg0, 0);
609379bc100SJani Nikula 		} else {
610379bc100SJani Nikula 			ret = gmbus_xfer_write(dev_priv, &msgs[i], 0);
611379bc100SJani Nikula 		}
612379bc100SJani Nikula 
613379bc100SJani Nikula 		if (!ret)
614379bc100SJani Nikula 			ret = gmbus_wait(dev_priv,
615379bc100SJani Nikula 					 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
616379bc100SJani Nikula 		if (ret == -ETIMEDOUT)
617379bc100SJani Nikula 			goto timeout;
618379bc100SJani Nikula 		else if (ret)
619379bc100SJani Nikula 			goto clear_err;
620379bc100SJani Nikula 	}
621379bc100SJani Nikula 
622379bc100SJani Nikula 	/* Generate a STOP condition on the bus. Note that gmbus can't generata
623379bc100SJani Nikula 	 * a STOP on the very first cycle. To simplify the code we
624379bc100SJani Nikula 	 * unconditionally generate the STOP condition with an additional gmbus
625379bc100SJani Nikula 	 * cycle. */
626d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
627379bc100SJani Nikula 
628379bc100SJani Nikula 	/* Mark the GMBUS interface as disabled after waiting for idle.
629379bc100SJani Nikula 	 * We will re-enable it at the start of the next xfer,
630379bc100SJani Nikula 	 * till then let it sleep.
631379bc100SJani Nikula 	 */
632379bc100SJani Nikula 	if (gmbus_wait_idle(dev_priv)) {
6336a9cc4bfSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
6346a9cc4bfSWambui Karuga 			    "GMBUS [%s] timed out waiting for idle\n",
635379bc100SJani Nikula 			    adapter->name);
636379bc100SJani Nikula 		ret = -ETIMEDOUT;
637379bc100SJani Nikula 	}
638d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS0, 0);
639379bc100SJani Nikula 	ret = ret ?: i;
640379bc100SJani Nikula 	goto out;
641379bc100SJani Nikula 
642379bc100SJani Nikula clear_err:
643379bc100SJani Nikula 	/*
644379bc100SJani Nikula 	 * Wait for bus to IDLE before clearing NAK.
645379bc100SJani Nikula 	 * If we clear the NAK while bus is still active, then it will stay
646379bc100SJani Nikula 	 * active and the next transaction may fail.
647379bc100SJani Nikula 	 *
648379bc100SJani Nikula 	 * If no ACK is received during the address phase of a transaction, the
649379bc100SJani Nikula 	 * adapter must report -ENXIO. It is not clear what to return if no ACK
650379bc100SJani Nikula 	 * is received at other times. But we have to be careful to not return
651379bc100SJani Nikula 	 * spurious -ENXIO because that will prevent i2c and drm edid functions
652379bc100SJani Nikula 	 * from retrying. So return -ENXIO only when gmbus properly quiescents -
653379bc100SJani Nikula 	 * timing out seems to happen when there _is_ a ddc chip present, but
654379bc100SJani Nikula 	 * it's slow responding and only answers on the 2nd retry.
655379bc100SJani Nikula 	 */
656379bc100SJani Nikula 	ret = -ENXIO;
657379bc100SJani Nikula 	if (gmbus_wait_idle(dev_priv)) {
6586a9cc4bfSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
6596a9cc4bfSWambui Karuga 			    "GMBUS [%s] timed out after NAK\n",
660379bc100SJani Nikula 			    adapter->name);
661379bc100SJani Nikula 		ret = -ETIMEDOUT;
662379bc100SJani Nikula 	}
663379bc100SJani Nikula 
664379bc100SJani Nikula 	/* Toggle the Software Clear Interrupt bit. This has the effect
665379bc100SJani Nikula 	 * of resetting the GMBUS controller and so clearing the
666379bc100SJani Nikula 	 * BUS_ERROR raised by the slave's NAK.
667379bc100SJani Nikula 	 */
668d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1, GMBUS_SW_CLR_INT);
669d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS1, 0);
670d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS0, 0);
671379bc100SJani Nikula 
6726a9cc4bfSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "GMBUS [%s] NAK for addr: %04x %c(%d)\n",
673379bc100SJani Nikula 		    adapter->name, msgs[i].addr,
674379bc100SJani Nikula 		    (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
675379bc100SJani Nikula 
676379bc100SJani Nikula 	/*
677379bc100SJani Nikula 	 * Passive adapters sometimes NAK the first probe. Retry the first
678379bc100SJani Nikula 	 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
679379bc100SJani Nikula 	 * has retries internally. See also the retry loop in
680379bc100SJani Nikula 	 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
681379bc100SJani Nikula 	 */
682379bc100SJani Nikula 	if (ret == -ENXIO && i == 0 && try++ == 0) {
6836a9cc4bfSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
6846a9cc4bfSWambui Karuga 			    "GMBUS [%s] NAK on first message, retry\n",
685379bc100SJani Nikula 			    adapter->name);
686379bc100SJani Nikula 		goto retry;
687379bc100SJani Nikula 	}
688379bc100SJani Nikula 
689379bc100SJani Nikula 	goto out;
690379bc100SJani Nikula 
691379bc100SJani Nikula timeout:
6926a9cc4bfSWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
6936a9cc4bfSWambui Karuga 		    "GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
694379bc100SJani Nikula 		    bus->adapter.name, bus->reg0 & 0xff);
695d9053b23SJani Nikula 	intel_de_write_fw(dev_priv, GMBUS0, 0);
696379bc100SJani Nikula 
697379bc100SJani Nikula 	/*
698379bc100SJani Nikula 	 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
699379bc100SJani Nikula 	 * instead. Use EAGAIN to have i2c core retry.
700379bc100SJani Nikula 	 */
701379bc100SJani Nikula 	ret = -EAGAIN;
702379bc100SJani Nikula 
703379bc100SJani Nikula out:
704379bc100SJani Nikula 	/* Display WA #0868: skl,bxt,kbl,cfl,glk,cnl */
705379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
706379bc100SJani Nikula 		bxt_gmbus_clock_gating(dev_priv, true);
707379bc100SJani Nikula 	else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_CNP(dev_priv))
708379bc100SJani Nikula 		pch_gmbus_clock_gating(dev_priv, true);
709379bc100SJani Nikula 
710379bc100SJani Nikula 	return ret;
711379bc100SJani Nikula }
712379bc100SJani Nikula 
713379bc100SJani Nikula static int
714379bc100SJani Nikula gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
715379bc100SJani Nikula {
716379bc100SJani Nikula 	struct intel_gmbus *bus =
717379bc100SJani Nikula 		container_of(adapter, struct intel_gmbus, adapter);
718379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
719379bc100SJani Nikula 	intel_wakeref_t wakeref;
720379bc100SJani Nikula 	int ret;
721379bc100SJani Nikula 
722379bc100SJani Nikula 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
723379bc100SJani Nikula 
724379bc100SJani Nikula 	if (bus->force_bit) {
725379bc100SJani Nikula 		ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
726379bc100SJani Nikula 		if (ret < 0)
727379bc100SJani Nikula 			bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
728379bc100SJani Nikula 	} else {
729379bc100SJani Nikula 		ret = do_gmbus_xfer(adapter, msgs, num, 0);
730379bc100SJani Nikula 		if (ret == -EAGAIN)
731379bc100SJani Nikula 			bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
732379bc100SJani Nikula 	}
733379bc100SJani Nikula 
734379bc100SJani Nikula 	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS, wakeref);
735379bc100SJani Nikula 
736379bc100SJani Nikula 	return ret;
737379bc100SJani Nikula }
738379bc100SJani Nikula 
739379bc100SJani Nikula int intel_gmbus_output_aksv(struct i2c_adapter *adapter)
740379bc100SJani Nikula {
741379bc100SJani Nikula 	struct intel_gmbus *bus =
742379bc100SJani Nikula 		container_of(adapter, struct intel_gmbus, adapter);
743379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
744379bc100SJani Nikula 	u8 cmd = DRM_HDCP_DDC_AKSV;
745379bc100SJani Nikula 	u8 buf[DRM_HDCP_KSV_LEN] = { 0 };
746379bc100SJani Nikula 	struct i2c_msg msgs[] = {
747379bc100SJani Nikula 		{
748379bc100SJani Nikula 			.addr = DRM_HDCP_DDC_ADDR,
749379bc100SJani Nikula 			.flags = 0,
750379bc100SJani Nikula 			.len = sizeof(cmd),
751379bc100SJani Nikula 			.buf = &cmd,
752379bc100SJani Nikula 		},
753379bc100SJani Nikula 		{
754379bc100SJani Nikula 			.addr = DRM_HDCP_DDC_ADDR,
755379bc100SJani Nikula 			.flags = 0,
756379bc100SJani Nikula 			.len = sizeof(buf),
757379bc100SJani Nikula 			.buf = buf,
758379bc100SJani Nikula 		}
759379bc100SJani Nikula 	};
760379bc100SJani Nikula 	intel_wakeref_t wakeref;
761379bc100SJani Nikula 	int ret;
762379bc100SJani Nikula 
763379bc100SJani Nikula 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
764379bc100SJani Nikula 	mutex_lock(&dev_priv->gmbus_mutex);
765379bc100SJani Nikula 
766379bc100SJani Nikula 	/*
767379bc100SJani Nikula 	 * In order to output Aksv to the receiver, use an indexed write to
768379bc100SJani Nikula 	 * pass the i2c command, and tell GMBUS to use the HW-provided value
769379bc100SJani Nikula 	 * instead of sourcing GMBUS3 for the data.
770379bc100SJani Nikula 	 */
771379bc100SJani Nikula 	ret = do_gmbus_xfer(adapter, msgs, ARRAY_SIZE(msgs), GMBUS_AKSV_SELECT);
772379bc100SJani Nikula 
773379bc100SJani Nikula 	mutex_unlock(&dev_priv->gmbus_mutex);
774379bc100SJani Nikula 	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS, wakeref);
775379bc100SJani Nikula 
776379bc100SJani Nikula 	return ret;
777379bc100SJani Nikula }
778379bc100SJani Nikula 
779379bc100SJani Nikula static u32 gmbus_func(struct i2c_adapter *adapter)
780379bc100SJani Nikula {
781379bc100SJani Nikula 	return i2c_bit_algo.functionality(adapter) &
782379bc100SJani Nikula 		(I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
783379bc100SJani Nikula 		/* I2C_FUNC_10BIT_ADDR | */
784379bc100SJani Nikula 		I2C_FUNC_SMBUS_READ_BLOCK_DATA |
785379bc100SJani Nikula 		I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
786379bc100SJani Nikula }
787379bc100SJani Nikula 
788379bc100SJani Nikula static const struct i2c_algorithm gmbus_algorithm = {
789379bc100SJani Nikula 	.master_xfer	= gmbus_xfer,
790379bc100SJani Nikula 	.functionality	= gmbus_func
791379bc100SJani Nikula };
792379bc100SJani Nikula 
793379bc100SJani Nikula static void gmbus_lock_bus(struct i2c_adapter *adapter,
794379bc100SJani Nikula 			   unsigned int flags)
795379bc100SJani Nikula {
796379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
797379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
798379bc100SJani Nikula 
799379bc100SJani Nikula 	mutex_lock(&dev_priv->gmbus_mutex);
800379bc100SJani Nikula }
801379bc100SJani Nikula 
802379bc100SJani Nikula static int gmbus_trylock_bus(struct i2c_adapter *adapter,
803379bc100SJani Nikula 			     unsigned int flags)
804379bc100SJani Nikula {
805379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
806379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
807379bc100SJani Nikula 
808379bc100SJani Nikula 	return mutex_trylock(&dev_priv->gmbus_mutex);
809379bc100SJani Nikula }
810379bc100SJani Nikula 
811379bc100SJani Nikula static void gmbus_unlock_bus(struct i2c_adapter *adapter,
812379bc100SJani Nikula 			     unsigned int flags)
813379bc100SJani Nikula {
814379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
815379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
816379bc100SJani Nikula 
817379bc100SJani Nikula 	mutex_unlock(&dev_priv->gmbus_mutex);
818379bc100SJani Nikula }
819379bc100SJani Nikula 
820379bc100SJani Nikula static const struct i2c_lock_operations gmbus_lock_ops = {
821379bc100SJani Nikula 	.lock_bus =    gmbus_lock_bus,
822379bc100SJani Nikula 	.trylock_bus = gmbus_trylock_bus,
823379bc100SJani Nikula 	.unlock_bus =  gmbus_unlock_bus,
824379bc100SJani Nikula };
825379bc100SJani Nikula 
826379bc100SJani Nikula /**
827379bc100SJani Nikula  * intel_gmbus_setup - instantiate all Intel i2c GMBuses
828379bc100SJani Nikula  * @dev_priv: i915 device private
829379bc100SJani Nikula  */
830379bc100SJani Nikula int intel_gmbus_setup(struct drm_i915_private *dev_priv)
831379bc100SJani Nikula {
832379bc100SJani Nikula 	struct pci_dev *pdev = dev_priv->drm.pdev;
833379bc100SJani Nikula 	struct intel_gmbus *bus;
834379bc100SJani Nikula 	unsigned int pin;
835379bc100SJani Nikula 	int ret;
836379bc100SJani Nikula 
837da27bd41SVille Syrjälä 	if (!HAS_DISPLAY(dev_priv))
838379bc100SJani Nikula 		return 0;
839379bc100SJani Nikula 
840379bc100SJani Nikula 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
841379bc100SJani Nikula 		dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
842379bc100SJani Nikula 	else if (!HAS_GMCH(dev_priv))
843379bc100SJani Nikula 		/*
844379bc100SJani Nikula 		 * Broxton uses the same PCH offsets for South Display Engine,
845379bc100SJani Nikula 		 * even though it doesn't have a PCH.
846379bc100SJani Nikula 		 */
847379bc100SJani Nikula 		dev_priv->gpio_mmio_base = PCH_DISPLAY_BASE;
848379bc100SJani Nikula 
849379bc100SJani Nikula 	mutex_init(&dev_priv->gmbus_mutex);
850379bc100SJani Nikula 	init_waitqueue_head(&dev_priv->gmbus_wait_queue);
851379bc100SJani Nikula 
852379bc100SJani Nikula 	for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
853379bc100SJani Nikula 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
854379bc100SJani Nikula 			continue;
855379bc100SJani Nikula 
856379bc100SJani Nikula 		bus = &dev_priv->gmbus[pin];
857379bc100SJani Nikula 
858379bc100SJani Nikula 		bus->adapter.owner = THIS_MODULE;
859379bc100SJani Nikula 		bus->adapter.class = I2C_CLASS_DDC;
860379bc100SJani Nikula 		snprintf(bus->adapter.name,
861379bc100SJani Nikula 			 sizeof(bus->adapter.name),
862379bc100SJani Nikula 			 "i915 gmbus %s",
863379bc100SJani Nikula 			 get_gmbus_pin(dev_priv, pin)->name);
864379bc100SJani Nikula 
865379bc100SJani Nikula 		bus->adapter.dev.parent = &pdev->dev;
866379bc100SJani Nikula 		bus->dev_priv = dev_priv;
867379bc100SJani Nikula 
868379bc100SJani Nikula 		bus->adapter.algo = &gmbus_algorithm;
869379bc100SJani Nikula 		bus->adapter.lock_ops = &gmbus_lock_ops;
870379bc100SJani Nikula 
871379bc100SJani Nikula 		/*
872379bc100SJani Nikula 		 * We wish to retry with bit banging
873379bc100SJani Nikula 		 * after a timed out GMBUS attempt.
874379bc100SJani Nikula 		 */
875379bc100SJani Nikula 		bus->adapter.retries = 1;
876379bc100SJani Nikula 
877379bc100SJani Nikula 		/* By default use a conservative clock rate */
878379bc100SJani Nikula 		bus->reg0 = pin | GMBUS_RATE_100KHZ;
879379bc100SJani Nikula 
880379bc100SJani Nikula 		/* gmbus seems to be broken on i830 */
881379bc100SJani Nikula 		if (IS_I830(dev_priv))
882379bc100SJani Nikula 			bus->force_bit = 1;
883379bc100SJani Nikula 
884379bc100SJani Nikula 		intel_gpio_setup(bus, pin);
885379bc100SJani Nikula 
886379bc100SJani Nikula 		ret = i2c_add_adapter(&bus->adapter);
887379bc100SJani Nikula 		if (ret)
888379bc100SJani Nikula 			goto err;
889379bc100SJani Nikula 	}
890379bc100SJani Nikula 
891379bc100SJani Nikula 	intel_gmbus_reset(dev_priv);
892379bc100SJani Nikula 
893379bc100SJani Nikula 	return 0;
894379bc100SJani Nikula 
895379bc100SJani Nikula err:
896379bc100SJani Nikula 	while (pin--) {
897379bc100SJani Nikula 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
898379bc100SJani Nikula 			continue;
899379bc100SJani Nikula 
900379bc100SJani Nikula 		bus = &dev_priv->gmbus[pin];
901379bc100SJani Nikula 		i2c_del_adapter(&bus->adapter);
902379bc100SJani Nikula 	}
903379bc100SJani Nikula 	return ret;
904379bc100SJani Nikula }
905379bc100SJani Nikula 
906379bc100SJani Nikula struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
907379bc100SJani Nikula 					    unsigned int pin)
908379bc100SJani Nikula {
909f4224a4cSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
910f4224a4cSPankaj Bharadiya 			!intel_gmbus_is_valid_pin(dev_priv, pin)))
911379bc100SJani Nikula 		return NULL;
912379bc100SJani Nikula 
913379bc100SJani Nikula 	return &dev_priv->gmbus[pin].adapter;
914379bc100SJani Nikula }
915379bc100SJani Nikula 
916379bc100SJani Nikula void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
917379bc100SJani Nikula {
918379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
919379bc100SJani Nikula 
920379bc100SJani Nikula 	bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
921379bc100SJani Nikula }
922379bc100SJani Nikula 
923379bc100SJani Nikula void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
924379bc100SJani Nikula {
925379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
926379bc100SJani Nikula 	struct drm_i915_private *dev_priv = bus->dev_priv;
927379bc100SJani Nikula 
928379bc100SJani Nikula 	mutex_lock(&dev_priv->gmbus_mutex);
929379bc100SJani Nikula 
930379bc100SJani Nikula 	bus->force_bit += force_bit ? 1 : -1;
9316a9cc4bfSWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
9326a9cc4bfSWambui Karuga 		    "%sabling bit-banging on %s. force bit now %d\n",
933379bc100SJani Nikula 		    force_bit ? "en" : "dis", adapter->name,
934379bc100SJani Nikula 		    bus->force_bit);
935379bc100SJani Nikula 
936379bc100SJani Nikula 	mutex_unlock(&dev_priv->gmbus_mutex);
937379bc100SJani Nikula }
938379bc100SJani Nikula 
939379bc100SJani Nikula bool intel_gmbus_is_forced_bit(struct i2c_adapter *adapter)
940379bc100SJani Nikula {
941379bc100SJani Nikula 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
942379bc100SJani Nikula 
943379bc100SJani Nikula 	return bus->force_bit;
944379bc100SJani Nikula }
945379bc100SJani Nikula 
946379bc100SJani Nikula void intel_gmbus_teardown(struct drm_i915_private *dev_priv)
947379bc100SJani Nikula {
948379bc100SJani Nikula 	struct intel_gmbus *bus;
949379bc100SJani Nikula 	unsigned int pin;
950379bc100SJani Nikula 
951379bc100SJani Nikula 	for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
952379bc100SJani Nikula 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
953379bc100SJani Nikula 			continue;
954379bc100SJani Nikula 
955379bc100SJani Nikula 		bus = &dev_priv->gmbus[pin];
956379bc100SJani Nikula 		i2c_del_adapter(&bus->adapter);
957379bc100SJani Nikula 	}
958379bc100SJani Nikula }
959